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"
30 static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
36 n = unit_name_to_prefix_and_instance(u->id);
44 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
50 n = unit_name_to_prefix(u->id);
58 static int specifier_prefix_unescaped(char specifier, void *data, void *userdata, char **ret) {
60 _cleanup_free_ char *p = NULL;
65 p = unit_name_to_prefix(u->id);
69 n = unit_name_unescape(p);
77 static int specifier_instance_unescaped(char specifier, void *data, void *userdata, char **ret) {
86 n = unit_name_unescape(u->instance);
94 static int specifier_filename(char specifier, void *data, void *userdata, char **ret) {
101 n = unit_name_path_unescape(u->instance);
103 n = unit_name_to_path(u->id);
111 static int specifier_cgroup(char specifier, void *data, void *userdata, char **ret) {
118 n = strdup(u->cgroup_path);
120 n = unit_default_cgroup_path(u);
128 static int specifier_cgroup_root(char specifier, void *data, void *userdata, char **ret) {
136 slice = unit_slice_name(u);
137 if (specifier == 'R' || !slice)
138 n = strdup(u->manager->cgroup_root);
140 _cleanup_free_ char *p = NULL;
142 r = cg_slice_to_path(slice, &p);
146 n = strjoin(u->manager->cgroup_root, "/", p, NULL);
155 static int specifier_runtime(char specifier, void *data, void *userdata, char **ret) {
162 if (u->manager->running_as == SYSTEMD_SYSTEM)
165 e = getenv("XDG_RUNTIME_DIR");
178 static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
179 char *printed = NULL;
186 c = unit_get_exec_context(u);
190 if (u->manager->running_as == SYSTEMD_SYSTEM) {
192 /* We cannot use NSS from PID 1, hence try to make the
193 * best of it in that case, and fail if we can't help
196 if (!c->user || streq(c->user, "root") || streq(c->user, "0"))
197 printed = strdup(specifier == 'u' ? "root" : "0");
199 if (specifier == 'u')
200 printed = strdup(c->user);
204 r = parse_uid(c->user, &uid);
208 r = asprintf(&printed, UID_FMT, uid);
213 _cleanup_free_ char *tmp = NULL;
214 const char *username = NULL;
220 /* get USER env from env or our own uid */
221 username = tmp = getusername_malloc();
223 /* fish username from passwd */
224 r = get_user_creds(&username, &uid, NULL, NULL, NULL);
228 if (specifier == 'u')
229 printed = strdup(username);
231 r = asprintf(&printed, UID_FMT, uid);
234 if (r < 0 || !printed)
241 static int specifier_user_home(char specifier, void *data, void *userdata, char **ret) {
249 c = unit_get_exec_context(u);
253 if (u->manager->running_as == SYSTEMD_SYSTEM) {
255 /* We cannot use NSS from PID 1, hence try to make the
256 * best of it if we can, but fail if we can't */
258 if (!c->user || streq(c->user, "root") || streq(c->user, "0"))
265 /* return HOME if set, otherwise from passwd */
266 if (!c || !c->user) {
267 r = get_home_dir(&n);
271 const char *username, *home;
274 r = get_user_creds(&username, NULL, NULL, &home, NULL);
289 static int specifier_user_shell(char specifier, void *data, void *userdata, char **ret) {
297 c = unit_get_exec_context(u);
301 if (u->manager->running_as == SYSTEMD_SYSTEM) {
303 /* We cannot use NSS from PID 1, hence try to make the
304 * best of it if we can, but fail if we can't */
306 if (!c->user || streq(c->user, "root") || streq(c->user, "0"))
307 n = strdup("/bin/sh");
313 /* return /bin/sh for root, otherwise the value from passwd */
319 const char *username, *shell;
322 r = get_user_creds(&username, NULL, NULL, NULL, &shell);
337 int unit_name_printf(Unit *u, const char* format, char **ret) {
340 * This will use the passed string as format string and
341 * replace the following specifiers:
343 * %n: the full id of the unit (foo@bar.waldo)
344 * %N: the id of the unit without the suffix (foo@bar)
345 * %p: the prefix (foo)
346 * %i: the instance (bar)
349 const Specifier table[] = {
350 { 'n', specifier_string, u->id },
351 { 'N', specifier_prefix_and_instance, NULL },
352 { 'p', specifier_prefix, NULL },
353 { 'i', specifier_string, u->instance },
361 return specifier_printf(format, table, u, ret);
364 int unit_full_printf(Unit *u, const char *format, char **ret) {
366 /* This is similar to unit_name_printf() but also supports
367 * unescaping. Also, adds a couple of additional codes:
369 * %f the instance if set, otherwise the id
370 * %c cgroup path of unit
371 * %r where units in this slice are placed in the cgroup tree
372 * %R the root of this systemd's instance tree
373 * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR)
374 * %U the UID of the configured user or running user
375 * %u the username of the configured user or running user
376 * %h the homedir of the configured user or running user
377 * %s the shell of the configured user or running user
378 * %m the machine ID of the running system
379 * %H the host name of the running system
380 * %b the boot ID of the running system
381 * %v `uname -r` of the running system
384 const Specifier table[] = {
385 { 'n', specifier_string, u->id },
386 { 'N', specifier_prefix_and_instance, NULL },
387 { 'p', specifier_prefix, NULL },
388 { 'P', specifier_prefix_unescaped, NULL },
389 { 'i', specifier_string, u->instance },
390 { 'I', specifier_instance_unescaped, NULL },
392 { 'f', specifier_filename, NULL },
393 { 'c', specifier_cgroup, NULL },
394 { 'r', specifier_cgroup_root, NULL },
395 { 'R', specifier_cgroup_root, NULL },
396 { 't', specifier_runtime, NULL },
397 { 'U', specifier_user_name, NULL },
398 { 'u', specifier_user_name, NULL },
399 { 'h', specifier_user_home, NULL },
400 { 's', specifier_user_shell, NULL },
402 { 'm', specifier_machine_id, NULL },
403 { 'H', specifier_host_name, NULL },
404 { 'b', specifier_boot_id, NULL },
405 { 'v', specifier_kernel_release, NULL },
413 return specifier_printf(format, table, u, ret);
416 int unit_full_printf_strv(Unit *u, char **l, char ***ret) {
421 /* Applies unit_full_printf to every entry in l */
430 for (i = l, j = r; *i; i++, j++) {
431 q = unit_full_printf(u, *i, j);
441 for (j--; j >= r; j--)