1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "cgroup-util.h"
5 #include "format-util.h"
8 #include "string-util.h"
10 #include "unit-name.h"
11 #include "unit-printf.h"
13 #include "user-util.h"
15 static int specifier_prefix_and_instance(char specifier
, const void *data
, const void *userdata
, char **ret
) {
16 const Unit
*u
= userdata
;
20 return unit_name_to_prefix_and_instance(u
->id
, ret
);
23 static int specifier_prefix(char specifier
, const void *data
, const void *userdata
, char **ret
) {
24 const Unit
*u
= userdata
;
28 return unit_name_to_prefix(u
->id
, ret
);
31 static int specifier_prefix_unescaped(char specifier
, const void *data
, const void *userdata
, char **ret
) {
32 _cleanup_free_
char *p
= NULL
;
33 const Unit
*u
= userdata
;
38 r
= unit_name_to_prefix(u
->id
, &p
);
42 return unit_name_unescape(p
, ret
);
45 static int specifier_instance_unescaped(char specifier
, const void *data
, const void *userdata
, char **ret
) {
46 const Unit
*u
= userdata
;
50 return unit_name_unescape(strempty(u
->instance
), ret
);
53 static int specifier_last_component(char specifier
, const void *data
, const void *userdata
, char **ret
) {
54 const Unit
*u
= userdata
;
55 _cleanup_free_
char *prefix
= NULL
;
61 r
= unit_name_to_prefix(u
->id
, &prefix
);
65 dash
= strrchr(prefix
, '-');
67 return specifier_string(specifier
, dash
+ 1, userdata
, ret
);
69 *ret
= TAKE_PTR(prefix
);
73 static int specifier_last_component_unescaped(char specifier
, const void *data
, const void *userdata
, char **ret
) {
74 _cleanup_free_
char *p
= NULL
;
77 r
= specifier_last_component(specifier
, data
, userdata
, &p
);
81 return unit_name_unescape(p
, ret
);
84 static int specifier_filename(char specifier
, const void *data
, const void *userdata
, char **ret
) {
85 const Unit
*u
= userdata
;
90 return unit_name_path_unescape(u
->instance
, ret
);
92 return unit_name_to_path(u
->id
, ret
);
95 static void bad_specifier(const Unit
*u
, char specifier
) {
96 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
);
99 static int specifier_cgroup(char specifier
, const void *data
, const void *userdata
, char **ret
) {
100 const Unit
*u
= userdata
;
105 bad_specifier(u
, specifier
);
108 n
= strdup(u
->cgroup_path
);
110 n
= unit_default_cgroup_path(u
);
118 static int specifier_cgroup_root(char specifier
, const void *data
, const void *userdata
, char **ret
) {
119 const Unit
*u
= userdata
;
124 bad_specifier(u
, specifier
);
126 n
= strdup(u
->manager
->cgroup_root
);
134 static int specifier_cgroup_slice(char specifier
, const void *data
, const void *userdata
, char **ret
) {
135 const Unit
*u
= userdata
;
140 bad_specifier(u
, specifier
);
142 if (UNIT_ISSET(u
->slice
)) {
145 slice
= UNIT_DEREF(u
->slice
);
147 if (slice
->cgroup_path
)
148 n
= strdup(slice
->cgroup_path
);
150 n
= unit_default_cgroup_path(slice
);
152 n
= strdup(u
->manager
->cgroup_root
);
160 static int specifier_special_directory(char specifier
, const void *data
, const void *userdata
, char **ret
) {
161 const Unit
*u
= userdata
;
166 n
= strdup(u
->manager
->prefix
[PTR_TO_UINT(data
)]);
174 int unit_name_printf(Unit
*u
, const char* format
, char **ret
) {
177 * This will use the passed string as format string and replace the following specifiers (which should all be
178 * safe for inclusion in unit names):
180 * %n: the full id of the unit (foo@bar.waldo)
181 * %N: the id of the unit without the suffix (foo@bar)
182 * %p: the prefix (foo)
183 * %i: the instance (bar)
185 * %U: the UID of the running user
186 * %u: the username of the running user
188 * %m: the machine ID of the running system
189 * %H: the host name of the running system
190 * %b: the boot ID of the running system
193 const Specifier table
[] = {
194 { 'n', specifier_string
, u
->id
},
195 { 'N', specifier_prefix_and_instance
, NULL
},
196 { 'p', specifier_prefix
, NULL
},
197 { 'i', specifier_string
, u
->instance
},
199 { 'g', specifier_group_name
, NULL
},
200 { 'G', specifier_group_id
, NULL
},
201 { 'U', specifier_user_id
, NULL
},
202 { 'u', specifier_user_name
, NULL
},
204 { 'm', specifier_machine_id
, NULL
},
205 { 'H', specifier_host_name
, NULL
},
206 { 'b', specifier_boot_id
, NULL
},
214 return specifier_printf(format
, table
, u
, ret
);
217 int unit_full_printf(Unit
*u
, const char *format
, char **ret
) {
219 /* This is similar to unit_name_printf() but also supports unescaping. Also, adds a couple of additional codes
220 * (which are likely not suitable for unescaped inclusion in unit names):
222 * %f: the unescaped instance if set, otherwise the id unescaped as path
224 * %c: cgroup path of unit (deprecated)
225 * %r: where units in this slice are placed in the cgroup tree (deprecated)
226 * %R: the root of this systemd's instance tree (deprecated)
228 * %t: the runtime directory root (e.g. /run or $XDG_RUNTIME_DIR)
229 * %S: the state directory root (e.g. /var/lib or $XDG_CONFIG_HOME)
230 * %C: the cache directory root (e.g. /var/cache or $XDG_CACHE_HOME)
231 * %L: the log directory root (e.g. /var/log or $XDG_CONFIG_HOME/log)
232 * %E: the configuration directory root (e.g. /etc or $XDG_CONFIG_HOME)
233 * %T: the temporary directory (e.g. /tmp, or $TMPDIR, $TEMP, $TMP)
234 * %V: the temporary directory for large, persistent stuff (e.g. /var/tmp, or $TMPDIR, $TEMP, $TMP)
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
},
261 { 't', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_RUNTIME
) },
262 { 'S', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_STATE
) },
263 { 'C', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_CACHE
) },
264 { 'L', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_LOGS
) },
265 { 'E', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_CONFIGURATION
) },
266 { 'T', specifier_tmp_dir
, NULL
},
267 { 'V', specifier_var_tmp_dir
, NULL
},
269 { 'g', specifier_group_name
, NULL
},
270 { 'G', specifier_group_id
, NULL
},
271 { 'U', specifier_user_id
, NULL
},
272 { 'u', specifier_user_name
, NULL
},
273 { 'h', specifier_user_home
, NULL
},
274 { 's', specifier_user_shell
, NULL
},
276 { 'm', specifier_machine_id
, NULL
},
277 { 'H', specifier_host_name
, NULL
},
278 { 'b', specifier_boot_id
, NULL
},
279 { 'v', specifier_kernel_release
, NULL
},
287 return specifier_printf(format
, table
, u
, ret
);