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_filename(char specifier
, void *data
, void *userdata
, char **ret
) {
64 return unit_name_path_unescape(u
->instance
, ret
);
66 return unit_name_to_path(u
->id
, ret
);
69 static void bad_specifier(Unit
*u
, char specifier
) {
70 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
);
73 static int specifier_cgroup(char specifier
, void *data
, void *userdata
, char **ret
) {
79 bad_specifier(u
, specifier
);
82 n
= strdup(u
->cgroup_path
);
84 n
= unit_default_cgroup_path(u
);
92 static int specifier_cgroup_root(char specifier
, void *data
, void *userdata
, char **ret
) {
98 bad_specifier(u
, specifier
);
100 n
= strdup(u
->manager
->cgroup_root
);
108 static int specifier_cgroup_slice(char specifier
, void *data
, void *userdata
, char **ret
) {
114 bad_specifier(u
, specifier
);
116 if (UNIT_ISSET(u
->slice
)) {
119 slice
= UNIT_DEREF(u
->slice
);
121 if (slice
->cgroup_path
)
122 n
= strdup(slice
->cgroup_path
);
124 n
= unit_default_cgroup_path(slice
);
126 n
= strdup(u
->manager
->cgroup_root
);
134 static int specifier_special_directory(char specifier
, void *data
, void *userdata
, char **ret
) {
140 n
= strdup(u
->manager
->prefix
[PTR_TO_UINT(data
)]);
148 int unit_name_printf(Unit
*u
, const char* format
, char **ret
) {
151 * This will use the passed string as format string and replace the following specifiers (which should all be
152 * safe for inclusion in unit names):
154 * %n: the full id of the unit (foo@bar.waldo)
155 * %N: the id of the unit without the suffix (foo@bar)
156 * %p: the prefix (foo)
157 * %i: the instance (bar)
159 * %U: the UID of the running user
160 * %u: the username of the running user
162 * %m: the machine ID of the running system
163 * %H: the host name of the running system
164 * %b: the boot ID of the running system
167 const Specifier table
[] = {
168 { 'n', specifier_string
, u
->id
},
169 { 'N', specifier_prefix_and_instance
, NULL
},
170 { 'p', specifier_prefix
, NULL
},
171 { 'i', specifier_string
, u
->instance
},
173 { 'U', specifier_user_id
, NULL
},
174 { 'u', specifier_user_name
, NULL
},
176 { 'm', specifier_machine_id
, NULL
},
177 { 'H', specifier_host_name
, NULL
},
178 { 'b', specifier_boot_id
, NULL
},
186 return specifier_printf(format
, table
, u
, ret
);
189 int unit_full_printf(Unit
*u
, const char *format
, char **ret
) {
191 /* This is similar to unit_name_printf() but also supports unescaping. Also, adds a couple of additional codes
192 * (which are likely not suitable for unescaped inclusion in unit names):
194 * %f: the unescaped instance if set, otherwise the id unescaped as path
196 * %c: cgroup path of unit (deprecated)
197 * %r: where units in this slice are placed in the cgroup tree (deprecated)
198 * %R: the root of this systemd's instance tree (deprecated)
200 * %t: the runtime directory root (e.g. /run or $XDG_RUNTIME_DIR)
201 * %S: the state directory root (e.g. /var/lib or $XDG_CONFIG_HOME)
202 * %C: the cache directory root (e.g. /var/cache or $XDG_CACHE_HOME)
203 * %L: the log directory root (e.g. /var/log or $XDG_CONFIG_HOME/log)
205 * %h: the homedir of the running user
206 * %s: the shell of the running user
208 * %v: `uname -r` of the running system
210 * NOTICE: When you add new entries here, please be careful: specifiers which depend on settings of the unit
211 * file itself are broken by design, as they would resolve differently depending on whether they are used
212 * before or after the relevant configuration setting. Hence: don't add them.
215 const Specifier table
[] = {
216 { 'n', specifier_string
, u
->id
},
217 { 'N', specifier_prefix_and_instance
, NULL
},
218 { 'p', specifier_prefix
, NULL
},
219 { 'P', specifier_prefix_unescaped
, NULL
},
220 { 'i', specifier_string
, u
->instance
},
221 { 'I', specifier_instance_unescaped
, NULL
},
223 { 'f', specifier_filename
, NULL
},
224 { 'c', specifier_cgroup
, NULL
},
225 { 'r', specifier_cgroup_slice
, NULL
},
226 { 'R', specifier_cgroup_root
, NULL
},
227 { 't', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_RUNTIME
) },
228 { 'S', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_STATE
) },
229 { 'C', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_CACHE
) },
230 { 'L', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_LOGS
) },
232 { 'U', specifier_user_id
, NULL
},
233 { 'u', specifier_user_name
, NULL
},
234 { 'h', specifier_user_home
, NULL
},
235 { 's', specifier_user_shell
, NULL
},
237 { 'm', specifier_machine_id
, NULL
},
238 { 'H', specifier_host_name
, NULL
},
239 { 'b', specifier_boot_id
, NULL
},
240 { 'v', specifier_kernel_release
, NULL
},
248 return specifier_printf(format
, table
, u
, ret
);
251 int unit_full_printf_strv(Unit
*u
, char **l
, char ***ret
) {
256 /* Applies unit_full_printf to every entry in l */
265 for (i
= l
, j
= r
; *i
; i
++, j
++) {
266 q
= unit_full_printf(u
, *i
, j
);
276 for (j
--; j
>= r
; j
--)