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"
30 static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
34 return unit_name_to_prefix_and_instance(u->id);
37 static char *specifier_prefix(char specifier, void *data, void *userdata) {
41 return unit_name_to_prefix(u->id);
44 static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) {
50 p = unit_name_to_prefix(u->id);
54 r = unit_name_unescape(p);
60 static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) {
65 return unit_name_unescape(u->instance);
70 static char *specifier_filename(char specifier, void *data, void *userdata) {
75 return unit_name_path_unescape(u->instance);
77 return unit_name_to_path(u->id);
80 static char *specifier_cgroup(char specifier, void *data, void *userdata) {
84 return unit_default_cgroup_path(u);
87 static char *specifier_cgroup_root(char specifier, void *data, void *userdata) {
93 return strdup(u->manager->cgroup_hierarchy);
95 if (path_get_parent(u->manager->cgroup_hierarchy, &p) < 0)
106 static char *specifier_runtime(char specifier, void *data, void *userdata) {
110 if (u->manager->running_as == SYSTEMD_USER) {
113 e = getenv("XDG_RUNTIME_DIR");
118 return strdup("/run");
121 static char *specifier_user_name(char specifier, void *data, void *userdata) {
125 const char *username;
129 c = unit_get_exec_context(u);
131 /* get USER env from our own env if set */
133 return getusername_malloc();
135 /* fish username from passwd */
137 r = get_user_creds(&username, NULL, NULL, NULL, NULL);
141 return strdup(username);
144 static char *specifier_user_home(char specifier, void *data, void *userdata) {
148 const char *username, *home;
152 c = unit_get_exec_context(u);
154 /* return HOME if set, otherwise from passwd */
155 if (!c || !c->user) {
158 r = get_home_dir(&h);
166 r = get_user_creds(&username, NULL, NULL, &home, NULL);
173 static char *specifier_user_shell(char specifier, void *data, void *userdata) {
177 const char *username, *shell;
181 c = unit_get_exec_context(u);
183 /* return HOME if set, otherwise from passwd */
184 if (!c || !c->user) {
189 return strdup("/bin/sh");
195 r = get_user_creds(&username, NULL, NULL, NULL, &shell);
197 return strdup("/bin/sh");
199 return strdup(shell);
202 static char *specifier_machine_id(char specifier, void *data, void *userdata) {
207 r = sd_id128_get_machine(&id);
215 return sd_id128_to_string(id, buf);
218 static char *specifier_boot_id(char specifier, void *data, void *userdata) {
223 r = sd_id128_get_boot(&id);
231 return sd_id128_to_string(id, buf);
234 static char *specifier_host_name(char specifier, void *data, void *userdata) {
235 return gethostname_malloc();
238 char *unit_name_printf(Unit *u, const char* format) {
241 * This will use the passed string as format string and
242 * replace the following specifiers:
244 * %n: the full id of the unit (foo@bar.waldo)
245 * %N: the id of the unit without the suffix (foo@bar)
246 * %p: the prefix (foo)
247 * %i: the instance (bar)
250 const Specifier table[] = {
251 { 'n', specifier_string, u->id },
252 { 'N', specifier_prefix_and_instance, NULL },
253 { 'p', specifier_prefix, NULL },
254 { 'i', specifier_string, u->instance },
261 return specifier_printf(format, table, u);
264 char *unit_full_printf(Unit *u, const char *format) {
266 /* This is similar to unit_name_printf() but also supports
267 * unescaping. Also, adds a couple of additional codes:
269 * %f the the instance if set, otherwise the id
270 * %c cgroup path of unit
271 * %r root cgroup path of this systemd instance (e.g. "/user/lennart/shared/systemd-4711")
272 * %R parent of root cgroup path (e.g. "/usr/lennart/shared")
273 * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR)
274 * %u the username of the configured user or running user
275 * %h the homedir of the configured user or running user
276 * %s the shell of the configured user or running user
277 * %m the machine ID of the running system
278 * %b the boot ID of the running system
279 * %H the host name of the running system
282 const Specifier table[] = {
283 { 'n', specifier_string, u->id },
284 { 'N', specifier_prefix_and_instance, NULL },
285 { 'p', specifier_prefix, NULL },
286 { 'P', specifier_prefix_unescaped, NULL },
287 { 'i', specifier_string, u->instance },
288 { 'I', specifier_instance_unescaped, NULL },
290 { 'f', specifier_filename, NULL },
291 { 'c', specifier_cgroup, NULL },
292 { 'r', specifier_cgroup_root, NULL },
293 { 'R', specifier_cgroup_root, NULL },
294 { 't', specifier_runtime, NULL },
295 { 'u', specifier_user_name, NULL },
296 { 'h', specifier_user_home, NULL },
297 { 's', specifier_user_shell, NULL },
299 { 'm', specifier_machine_id, NULL },
300 { 'H', specifier_host_name, NULL },
301 { 'b', specifier_boot_id, NULL },
308 return specifier_printf(format, table, u);
311 char **unit_full_printf_strv(Unit *u, char **l) {
315 /* Applies unit_full_printf to every entry in l */
324 for (i = l, j = r; *i; i++, j++) {
325 *j = unit_full_printf(u, *i);
334 for (j--; j >= r; j--)