1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
8 #include "alloc-util.h"
9 #include "cgroup-util.h"
10 #include "format-util.h"
12 #include "specifier.h"
13 #include "string-util.h"
15 #include "unit-name.h"
16 #include "unit-printf.h"
18 #include "user-util.h"
20 static int specifier_prefix_and_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
25 return unit_name_to_prefix_and_instance(u
->id
, ret
);
28 static int specifier_prefix(char specifier
, void *data
, void *userdata
, char **ret
) {
33 return unit_name_to_prefix(u
->id
, ret
);
36 static int specifier_prefix_unescaped(char specifier
, void *data
, void *userdata
, char **ret
) {
37 _cleanup_free_
char *p
= NULL
;
43 r
= unit_name_to_prefix(u
->id
, &p
);
47 return unit_name_unescape(p
, ret
);
50 static int specifier_instance_unescaped(char specifier
, void *data
, void *userdata
, char **ret
) {
55 return unit_name_unescape(strempty(u
->instance
), ret
);
58 static int specifier_last_component(char specifier
, void *data
, void *userdata
, char **ret
) {
60 _cleanup_free_
char *prefix
= NULL
;
66 r
= unit_name_to_prefix(u
->id
, &prefix
);
70 dash
= strrchr(prefix
, '-');
72 return specifier_string(specifier
, dash
+ 1, userdata
, ret
);
74 *ret
= TAKE_PTR(prefix
);
78 static int specifier_last_component_unescaped(char specifier
, void *data
, void *userdata
, char **ret
) {
79 _cleanup_free_
char *p
= NULL
;
82 r
= specifier_last_component(specifier
, data
, userdata
, &p
);
86 return unit_name_unescape(p
, ret
);
89 static int specifier_filename(char specifier
, void *data
, void *userdata
, char **ret
) {
95 return unit_name_path_unescape(u
->instance
, ret
);
97 return unit_name_to_path(u
->id
, ret
);
100 static void bad_specifier(Unit
*u
, char specifier
) {
101 log_unit_warning(u
, "Specifier '%%%c' used in unit configuration, which is deprecated. Please update your unit file, as it does not work as intended.", specifier
);
104 static int specifier_cgroup(char specifier
, void *data
, void *userdata
, char **ret
) {
110 bad_specifier(u
, specifier
);
113 n
= strdup(u
->cgroup_path
);
115 n
= unit_default_cgroup_path(u
);
123 static int specifier_cgroup_root(char specifier
, void *data
, void *userdata
, char **ret
) {
129 bad_specifier(u
, specifier
);
131 n
= strdup(u
->manager
->cgroup_root
);
139 static int specifier_cgroup_slice(char specifier
, void *data
, void *userdata
, char **ret
) {
145 bad_specifier(u
, specifier
);
147 if (UNIT_ISSET(u
->slice
)) {
150 slice
= UNIT_DEREF(u
->slice
);
152 if (slice
->cgroup_path
)
153 n
= strdup(slice
->cgroup_path
);
155 n
= unit_default_cgroup_path(slice
);
157 n
= strdup(u
->manager
->cgroup_root
);
165 static int specifier_special_directory(char specifier
, void *data
, void *userdata
, char **ret
) {
171 n
= strdup(u
->manager
->prefix
[PTR_TO_UINT(data
)]);
179 int unit_name_printf(Unit
*u
, const char* format
, char **ret
) {
182 * This will use the passed string as format string and replace the following specifiers (which should all be
183 * safe for inclusion in unit names):
185 * %n: the full id of the unit (foo@bar.waldo)
186 * %N: the id of the unit without the suffix (foo@bar)
187 * %p: the prefix (foo)
188 * %i: the instance (bar)
190 * %U: the UID of the running user
191 * %u: the username of the running user
193 * %m: the machine ID of the running system
194 * %H: the host name of the running system
195 * %b: the boot ID of the running system
198 const Specifier table
[] = {
199 { 'n', specifier_string
, u
->id
},
200 { 'N', specifier_prefix_and_instance
, NULL
},
201 { 'p', specifier_prefix
, NULL
},
202 { 'i', specifier_string
, u
->instance
},
204 { 'U', specifier_user_id
, NULL
},
205 { 'u', specifier_user_name
, NULL
},
207 { 'm', specifier_machine_id
, NULL
},
208 { 'H', specifier_host_name
, NULL
},
209 { 'b', specifier_boot_id
, NULL
},
217 return specifier_printf(format
, table
, u
, ret
);
220 int unit_full_printf(Unit
*u
, const char *format
, char **ret
) {
222 /* This is similar to unit_name_printf() but also supports unescaping. Also, adds a couple of additional codes
223 * (which are likely not suitable for unescaped inclusion in unit names):
225 * %f: the unescaped instance if set, otherwise the id unescaped as path
227 * %c: cgroup path of unit (deprecated)
228 * %r: where units in this slice are placed in the cgroup tree (deprecated)
229 * %R: the root of this systemd's instance tree (deprecated)
231 * %t: the runtime directory root (e.g. /run or $XDG_RUNTIME_DIR)
232 * %S: the state directory root (e.g. /var/lib or $XDG_CONFIG_HOME)
233 * %C: the cache directory root (e.g. /var/cache or $XDG_CACHE_HOME)
234 * %L: the log directory root (e.g. /var/log or $XDG_CONFIG_HOME/log)
236 * %h: the homedir of the running user
237 * %s: the shell of the running user
239 * %v: `uname -r` of the running system
241 * NOTICE: When you add new entries here, please be careful: specifiers which depend on settings of the unit
242 * file itself are broken by design, as they would resolve differently depending on whether they are used
243 * before or after the relevant configuration setting. Hence: don't add them.
246 const Specifier table
[] = {
247 { 'n', specifier_string
, u
->id
},
248 { 'N', specifier_prefix_and_instance
, NULL
},
249 { 'p', specifier_prefix
, NULL
},
250 { 'P', specifier_prefix_unescaped
, NULL
},
251 { 'i', specifier_string
, u
->instance
},
252 { 'I', specifier_instance_unescaped
, NULL
},
253 { 'j', specifier_last_component
, NULL
},
254 { 'J', specifier_last_component_unescaped
, NULL
},
256 { 'f', specifier_filename
, NULL
},
257 { 'c', specifier_cgroup
, NULL
},
258 { 'r', specifier_cgroup_slice
, NULL
},
259 { 'R', specifier_cgroup_root
, NULL
},
260 { 't', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_RUNTIME
) },
261 { 'S', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_STATE
) },
262 { 'C', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_CACHE
) },
263 { 'L', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_LOGS
) },
265 { 'U', specifier_user_id
, NULL
},
266 { 'u', specifier_user_name
, NULL
},
267 { 'h', specifier_user_home
, NULL
},
268 { 's', specifier_user_shell
, NULL
},
270 { 'm', specifier_machine_id
, NULL
},
271 { 'H', specifier_host_name
, NULL
},
272 { 'b', specifier_boot_id
, NULL
},
273 { 'v', specifier_kernel_release
, NULL
},
281 return specifier_printf(format
, table
, u
, ret
);
284 int unit_full_printf_strv(Unit
*u
, char **l
, char ***ret
) {
289 /* Applies unit_full_printf to every entry in l */
298 for (i
= l
, j
= r
; *i
; i
++, j
++) {
299 q
= unit_full_printf(u
, *i
, j
);
309 for (j
--; j
>= r
; j
--)