1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "alloc-util.h"
22 #include "cgroup-util.h"
23 #include "format-util.h"
25 #include "specifier.h"
26 #include "string-util.h"
28 #include "unit-name.h"
29 #include "unit-printf.h"
31 #include "user-util.h"
33 static int specifier_prefix_and_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
38 return unit_name_to_prefix_and_instance(u
->id
, ret
);
41 static int specifier_prefix(char specifier
, void *data
, void *userdata
, char **ret
) {
46 return unit_name_to_prefix(u
->id
, ret
);
49 static int specifier_prefix_unescaped(char specifier
, void *data
, void *userdata
, char **ret
) {
50 _cleanup_free_
char *p
= NULL
;
56 r
= unit_name_to_prefix(u
->id
, &p
);
60 return unit_name_unescape(p
, ret
);
63 static int specifier_instance_unescaped(char specifier
, void *data
, void *userdata
, char **ret
) {
68 return unit_name_unescape(strempty(u
->instance
), ret
);
71 static int specifier_filename(char specifier
, void *data
, void *userdata
, char **ret
) {
77 return unit_name_path_unescape(u
->instance
, ret
);
79 return unit_name_to_path(u
->id
, ret
);
82 static void bad_specifier(Unit
*u
, char specifier
) {
83 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
);
86 static int specifier_cgroup(char specifier
, void *data
, void *userdata
, char **ret
) {
92 bad_specifier(u
, specifier
);
95 n
= strdup(u
->cgroup_path
);
97 n
= unit_default_cgroup_path(u
);
105 static int specifier_cgroup_root(char specifier
, void *data
, void *userdata
, char **ret
) {
111 bad_specifier(u
, specifier
);
113 n
= strdup(u
->manager
->cgroup_root
);
121 static int specifier_cgroup_slice(char specifier
, void *data
, void *userdata
, char **ret
) {
127 bad_specifier(u
, specifier
);
129 if (UNIT_ISSET(u
->slice
)) {
132 slice
= UNIT_DEREF(u
->slice
);
134 if (slice
->cgroup_path
)
135 n
= strdup(slice
->cgroup_path
);
137 n
= unit_default_cgroup_path(slice
);
139 n
= strdup(u
->manager
->cgroup_root
);
147 static int specifier_special_directory(char specifier
, void *data
, void *userdata
, char **ret
) {
153 n
= strdup(u
->manager
->prefix
[PTR_TO_UINT(data
)]);
161 static int specifier_user_name(char specifier
, void *data
, void *userdata
, char **ret
) {
164 /* If we are UID 0 (root), this will not result in NSS,
165 * otherwise it might. This is good, as we want to be able to
166 * run this in PID 1, where our user ID is 0, but where NSS
167 * lookups are not allowed. */
169 t
= getusername_malloc();
177 static int specifier_user_id(char specifier
, void *data
, void *userdata
, char **ret
) {
179 if (asprintf(ret
, UID_FMT
, getuid()) < 0)
185 static int specifier_user_home(char specifier
, void *data
, void *userdata
, char **ret
) {
187 /* On PID 1 (which runs as root) this will not result in NSS,
188 * which is good. See above */
190 return get_home_dir(ret
);
193 static int specifier_user_shell(char specifier
, void *data
, void *userdata
, char **ret
) {
195 /* On PID 1 (which runs as root) this will not result in NSS,
196 * which is good. See above */
198 return get_shell(ret
);
201 int unit_name_printf(Unit
*u
, const char* format
, char **ret
) {
204 * This will use the passed string as format string and replace the following specifiers (which should all be
205 * safe for inclusion in unit names):
207 * %n: the full id of the unit (foo@bar.waldo)
208 * %N: the id of the unit without the suffix (foo@bar)
209 * %p: the prefix (foo)
210 * %i: the instance (bar)
212 * %U: the UID of the running user
213 * %u: the username of the running user
215 * %m: the machine ID of the running system
216 * %H: the host name of the running system
217 * %b: the boot ID of the running system
220 const Specifier table
[] = {
221 { 'n', specifier_string
, u
->id
},
222 { 'N', specifier_prefix_and_instance
, NULL
},
223 { 'p', specifier_prefix
, NULL
},
224 { 'i', specifier_string
, u
->instance
},
226 { 'U', specifier_user_id
, NULL
},
227 { 'u', specifier_user_name
, NULL
},
229 { 'm', specifier_machine_id
, NULL
},
230 { 'H', specifier_host_name
, NULL
},
231 { 'b', specifier_boot_id
, NULL
},
239 return specifier_printf(format
, table
, u
, ret
);
242 int unit_full_printf(Unit
*u
, const char *format
, char **ret
) {
244 /* This is similar to unit_name_printf() but also supports unescaping. Also, adds a couple of additional codes
245 * (which are likely not suitable for unescaped inclusion in unit names):
247 * %f: the unescaped instance if set, otherwise the id unescaped as path
249 * %c: cgroup path of unit (deprecated)
250 * %r: where units in this slice are placed in the cgroup tree (deprecated)
251 * %R: the root of this systemd's instance tree (deprecated)
253 * %t: the runtime directory root (e.g. /run or $XDG_RUNTIME_DIR)
254 * %S: the state directory root (e.g. /var/lib or $XDG_CONFIG_HOME)
255 * %C: the cache directory root (e.g. /var/cache or $XDG_CACHE_HOME)
256 * %L: the log directory root (e.g. /var/log or $XDG_CONFIG_HOME/log)
258 * %h: the homedir of the running user
259 * %s: the shell of the running user
261 * %v: `uname -r` of the running system
263 * NOTICE: When you add new entries here, please be careful: specifiers which depend on settings of the unit
264 * file itself are broken by design, as they would resolve differently depending on whether they are used
265 * before or after the relevant configuration setting. Hence: don't add them.
268 const Specifier table
[] = {
269 { 'n', specifier_string
, u
->id
},
270 { 'N', specifier_prefix_and_instance
, NULL
},
271 { 'p', specifier_prefix
, NULL
},
272 { 'P', specifier_prefix_unescaped
, NULL
},
273 { 'i', specifier_string
, u
->instance
},
274 { 'I', specifier_instance_unescaped
, NULL
},
276 { 'f', specifier_filename
, NULL
},
277 { 'c', specifier_cgroup
, NULL
},
278 { 'r', specifier_cgroup_slice
, NULL
},
279 { 'R', specifier_cgroup_root
, NULL
},
280 { 't', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_RUNTIME
) },
281 { 'S', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_STATE
) },
282 { 'C', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_CACHE
) },
283 { 'L', specifier_special_directory
, UINT_TO_PTR(EXEC_DIRECTORY_LOGS
) },
285 { 'U', specifier_user_id
, NULL
},
286 { 'u', specifier_user_name
, NULL
},
287 { 'h', specifier_user_home
, NULL
},
288 { 's', specifier_user_shell
, NULL
},
290 { 'm', specifier_machine_id
, NULL
},
291 { 'H', specifier_host_name
, NULL
},
292 { 'b', specifier_boot_id
, NULL
},
293 { 'v', specifier_kernel_release
, NULL
},
301 return specifier_printf(format
, table
, u
, ret
);
304 int unit_full_printf_strv(Unit
*u
, char **l
, char ***ret
) {
309 /* Applies unit_full_printf to every entry in l */
318 for (i
= l
, j
= r
; *i
; i
++, j
++) {
319 q
= unit_full_printf(u
, *i
, j
);
329 for (j
--; j
>= r
; j
--)