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/>.
22 #include "systemd/sd-id128.h"
24 #include "specifier.h"
25 #include "path-util.h"
27 #include "unit-name.h"
28 #include "unit-printf.h"
31 static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
35 return unit_name_to_prefix_and_instance(u->id);
38 static char *specifier_prefix(char specifier, void *data, void *userdata) {
42 return unit_name_to_prefix(u->id);
45 static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) {
51 p = unit_name_to_prefix(u->id);
55 r = unit_name_unescape(p);
61 static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) {
66 return unit_name_unescape(u->instance);
71 static char *specifier_filename(char specifier, void *data, void *userdata) {
76 return unit_name_path_unescape(u->instance);
78 return unit_name_to_path(u->id);
81 static char *specifier_cgroup(char specifier, void *data, void *userdata) {
85 return unit_default_cgroup_path(u);
88 static char *specifier_cgroup_root(char specifier, void *data, void *userdata) {
94 return strdup(u->manager->cgroup_hierarchy);
96 if (path_get_parent(u->manager->cgroup_hierarchy, &p) < 0)
107 static char *specifier_runtime(char specifier, void *data, void *userdata) {
111 if (u->manager->running_as == SYSTEMD_USER) {
114 e = getenv("XDG_RUNTIME_DIR");
119 return strdup("/run");
122 static char *specifier_user_name(char specifier, void *data, void *userdata) {
126 const char *username;
127 char _cleanup_free_ *tmp = NULL;
129 char *printed = NULL;
133 c = unit_get_exec_context(u);
138 /* get USER env from env or our own uid */
139 username = tmp = getusername_malloc();
141 /* fish username from passwd */
142 r = get_user_creds(&username, &uid, NULL, NULL, NULL);
148 if (asprintf(&printed, "%d", uid) < 0)
152 printed = strdup(username);
159 static char *specifier_user_home(char specifier, void *data, void *userdata) {
163 const char *username, *home;
167 c = unit_get_exec_context(u);
169 /* return HOME if set, otherwise from passwd */
170 if (!c || !c->user) {
173 r = get_home_dir(&h);
181 r = get_user_creds(&username, NULL, NULL, &home, NULL);
188 static char *specifier_user_shell(char specifier, void *data, void *userdata) {
192 const char *username, *shell;
196 c = unit_get_exec_context(u);
198 /* return HOME if set, otherwise from passwd */
199 if (!c || !c->user) {
204 return strdup("/bin/sh");
210 r = get_user_creds(&username, NULL, NULL, NULL, &shell);
212 return strdup("/bin/sh");
214 return strdup(shell);
217 char *unit_name_printf(Unit *u, const char* format) {
220 * This will use the passed string as format string and
221 * replace the following specifiers:
223 * %n: the full id of the unit (foo@bar.waldo)
224 * %N: the id of the unit without the suffix (foo@bar)
225 * %p: the prefix (foo)
226 * %i: the instance (bar)
229 const Specifier table[] = {
230 { 'n', specifier_string, u->id },
231 { 'N', specifier_prefix_and_instance, NULL },
232 { 'p', specifier_prefix, NULL },
233 { 'i', specifier_string, u->instance },
240 return specifier_printf(format, table, u);
243 char *unit_full_printf(Unit *u, const char *format) {
245 /* This is similar to unit_name_printf() but also supports
246 * unescaping. Also, adds a couple of additional codes:
248 * %f the the instance if set, otherwise the id
249 * %c cgroup path of unit
250 * %r root cgroup path of this systemd instance (e.g. "/user/lennart/shared/systemd-4711")
251 * %R parent of root cgroup path (e.g. "/usr/lennart/shared")
252 * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR)
253 * %U the UID of the configured user or running user
254 * %u the username of the configured user or running user
255 * %h the homedir of the configured user or running user
256 * %s the shell of the configured user or running user
257 * %m the machine ID of the running system
258 * %H the host name of the running system
259 * %b the boot ID of the running system
262 const Specifier table[] = {
263 { 'n', specifier_string, u->id },
264 { 'N', specifier_prefix_and_instance, NULL },
265 { 'p', specifier_prefix, NULL },
266 { 'P', specifier_prefix_unescaped, NULL },
267 { 'i', specifier_string, u->instance },
268 { 'I', specifier_instance_unescaped, NULL },
270 { 'f', specifier_filename, NULL },
271 { 'c', specifier_cgroup, NULL },
272 { 'r', specifier_cgroup_root, NULL },
273 { 'R', specifier_cgroup_root, NULL },
274 { 't', specifier_runtime, NULL },
275 { 'U', specifier_user_name, NULL },
276 { 'u', specifier_user_name, NULL },
277 { 'h', specifier_user_home, NULL },
278 { 's', specifier_user_shell, NULL },
280 { 'm', specifier_machine_id, NULL },
281 { 'H', specifier_host_name, NULL },
282 { 'b', specifier_boot_id, NULL },
288 return specifier_printf(format, table, u);
291 char **unit_full_printf_strv(Unit *u, char **l) {
295 /* Applies unit_full_printf to every entry in l */
304 for (i = l, j = r; *i; i++, j++) {
305 *j = unit_full_printf(u, *i);
314 for (j--; j >= r; j--)