1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "specifier.h"
25 #include "unit-name.h"
26 #include "unit-printf.h"
28 #include "cgroup-util.h"
29 #include "formats-util.h"
31 static int specifier_prefix_and_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
36 return unit_name_to_prefix_and_instance(u
->id
, ret
);
39 static int specifier_prefix(char specifier
, void *data
, void *userdata
, char **ret
) {
44 return unit_name_to_prefix(u
->id
, ret
);
47 static int specifier_prefix_unescaped(char specifier
, void *data
, void *userdata
, char **ret
) {
48 _cleanup_free_
char *p
= NULL
;
54 r
= unit_name_to_prefix(u
->id
, &p
);
58 return unit_name_unescape(p
, ret
);
61 static int specifier_instance_unescaped(char specifier
, void *data
, void *userdata
, char **ret
) {
66 return unit_name_unescape(strempty(u
->instance
), ret
);
69 static int specifier_filename(char specifier
, void *data
, void *userdata
, char **ret
) {
75 return unit_name_path_unescape(u
->instance
, ret
);
77 return unit_name_to_path(u
->id
, ret
);
80 static int specifier_cgroup(char specifier
, void *data
, void *userdata
, char **ret
) {
87 n
= strdup(u
->cgroup_path
);
89 n
= unit_default_cgroup_path(u
);
97 static int specifier_cgroup_root(char specifier
, void *data
, void *userdata
, char **ret
) {
103 n
= strdup(u
->manager
->cgroup_root
);
111 static int specifier_cgroup_slice(char specifier
, void *data
, void *userdata
, char **ret
) {
117 if (UNIT_ISSET(u
->slice
)) {
120 slice
= UNIT_DEREF(u
->slice
);
122 if (slice
->cgroup_path
)
123 n
= strdup(slice
->cgroup_path
);
125 n
= unit_default_cgroup_path(slice
);
127 n
= strdup(u
->manager
->cgroup_root
);
135 static int specifier_runtime(char specifier
, void *data
, void *userdata
, char **ret
) {
142 if (u
->manager
->running_as
== MANAGER_SYSTEM
)
145 e
= getenv("XDG_RUNTIME_DIR");
158 static int specifier_user_name(char specifier
, void *data
, void *userdata
, char **ret
) {
159 char *printed
= NULL
;
166 c
= unit_get_exec_context(u
);
170 if (u
->manager
->running_as
== MANAGER_SYSTEM
) {
172 /* We cannot use NSS from PID 1, hence try to make the
173 * best of it in that case, and fail if we can't help
176 if (!c
->user
|| streq(c
->user
, "root") || streq(c
->user
, "0"))
177 printed
= strdup(specifier
== 'u' ? "root" : "0");
179 if (specifier
== 'u')
180 printed
= strdup(c
->user
);
184 r
= parse_uid(c
->user
, &uid
);
188 r
= asprintf(&printed
, UID_FMT
, uid
);
193 _cleanup_free_
char *tmp
= NULL
;
194 const char *username
= NULL
;
200 /* get USER env from env or our own uid */
201 username
= tmp
= getusername_malloc();
203 /* fish username from passwd */
204 r
= get_user_creds(&username
, &uid
, NULL
, NULL
, NULL
);
208 if (specifier
== 'u')
209 printed
= strdup(username
);
211 r
= asprintf(&printed
, UID_FMT
, uid
);
214 if (r
< 0 || !printed
)
221 static int specifier_user_home(char specifier
, void *data
, void *userdata
, char **ret
) {
229 c
= unit_get_exec_context(u
);
233 if (u
->manager
->running_as
== MANAGER_SYSTEM
) {
235 /* We cannot use NSS from PID 1, hence try to make the
236 * best of it if we can, but fail if we can't */
238 if (!c
->user
|| streq(c
->user
, "root") || streq(c
->user
, "0"))
245 /* return HOME if set, otherwise from passwd */
246 if (!c
|| !c
->user
) {
247 r
= get_home_dir(&n
);
251 const char *username
, *home
;
254 r
= get_user_creds(&username
, NULL
, NULL
, &home
, NULL
);
269 static int specifier_user_shell(char specifier
, void *data
, void *userdata
, char **ret
) {
277 c
= unit_get_exec_context(u
);
281 if (u
->manager
->running_as
== MANAGER_SYSTEM
) {
283 /* We cannot use NSS from PID 1, hence try to make the
284 * best of it if we can, but fail if we can't */
286 if (!c
->user
|| streq(c
->user
, "root") || streq(c
->user
, "0"))
287 n
= strdup("/bin/sh");
293 /* return /bin/sh for root, otherwise the value from passwd */
299 const char *username
, *shell
;
302 r
= get_user_creds(&username
, NULL
, NULL
, NULL
, &shell
);
317 int unit_name_printf(Unit
*u
, const char* format
, char **ret
) {
320 * This will use the passed string as format string and
321 * replace the following specifiers:
323 * %n: the full id of the unit (foo@bar.waldo)
324 * %N: the id of the unit without the suffix (foo@bar)
325 * %p: the prefix (foo)
326 * %i: the instance (bar)
329 const Specifier table
[] = {
330 { 'n', specifier_string
, u
->id
},
331 { 'N', specifier_prefix_and_instance
, NULL
},
332 { 'p', specifier_prefix
, NULL
},
333 { 'i', specifier_string
, u
->instance
},
341 return specifier_printf(format
, table
, u
, ret
);
344 int unit_full_printf(Unit
*u
, const char *format
, char **ret
) {
346 /* This is similar to unit_name_printf() but also supports
347 * unescaping. Also, adds a couple of additional codes:
349 * %f the instance if set, otherwise the id
350 * %c cgroup path of unit
351 * %r where units in this slice are placed in the cgroup tree
352 * %R the root of this systemd's instance tree
353 * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR)
354 * %U the UID of the configured user or running user
355 * %u the username of the configured user or running user
356 * %h the homedir of the configured user or running user
357 * %s the shell of the configured user or running user
358 * %m the machine ID of the running system
359 * %H the host name of the running system
360 * %b the boot ID of the running system
361 * %v `uname -r` of the running system
364 const Specifier table
[] = {
365 { 'n', specifier_string
, u
->id
},
366 { 'N', specifier_prefix_and_instance
, NULL
},
367 { 'p', specifier_prefix
, NULL
},
368 { 'P', specifier_prefix_unescaped
, NULL
},
369 { 'i', specifier_string
, u
->instance
},
370 { 'I', specifier_instance_unescaped
, NULL
},
372 { 'f', specifier_filename
, NULL
},
373 { 'c', specifier_cgroup
, NULL
},
374 { 'r', specifier_cgroup_slice
, NULL
},
375 { 'R', specifier_cgroup_root
, NULL
},
376 { 't', specifier_runtime
, NULL
},
377 { 'U', specifier_user_name
, NULL
},
378 { 'u', specifier_user_name
, NULL
},
379 { 'h', specifier_user_home
, NULL
},
380 { 's', specifier_user_shell
, NULL
},
382 { 'm', specifier_machine_id
, NULL
},
383 { 'H', specifier_host_name
, NULL
},
384 { 'b', specifier_boot_id
, NULL
},
385 { 'v', specifier_kernel_release
, NULL
},
393 return specifier_printf(format
, table
, u
, ret
);
396 int unit_full_printf_strv(Unit
*u
, char **l
, char ***ret
) {
401 /* Applies unit_full_printf to every entry in l */
410 for (i
= l
, j
= r
; *i
; i
++, j
++) {
411 q
= unit_full_printf(u
, *i
, j
);
421 for (j
--; j
>= r
; j
--)