2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
21 #include "cgroup-util.h"
22 #include "formats-util.h"
24 #include "specifier.h"
25 #include "string-util.h"
27 #include "unit-name.h"
28 #include "unit-printf.h"
30 #include "user-util.h"
32 static int specifier_prefix_and_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
37 return unit_name_to_prefix_and_instance(u
->id
, ret
);
40 static int specifier_prefix(char specifier
, void *data
, void *userdata
, char **ret
) {
45 return unit_name_to_prefix(u
->id
, ret
);
48 static int specifier_prefix_unescaped(char specifier
, void *data
, void *userdata
, char **ret
) {
49 _cleanup_free_
char *p
= NULL
;
55 r
= unit_name_to_prefix(u
->id
, &p
);
59 return unit_name_unescape(p
, ret
);
62 static int specifier_instance_unescaped(char specifier
, void *data
, void *userdata
, char **ret
) {
67 return unit_name_unescape(strempty(u
->instance
), ret
);
70 static int specifier_filename(char specifier
, void *data
, void *userdata
, char **ret
) {
76 return unit_name_path_unescape(u
->instance
, ret
);
78 return unit_name_to_path(u
->id
, ret
);
81 static int specifier_cgroup(char specifier
, void *data
, void *userdata
, char **ret
) {
88 n
= strdup(u
->cgroup_path
);
90 n
= unit_default_cgroup_path(u
);
98 static int specifier_cgroup_root(char specifier
, void *data
, void *userdata
, char **ret
) {
104 n
= strdup(u
->manager
->cgroup_root
);
112 static int specifier_cgroup_slice(char specifier
, void *data
, void *userdata
, char **ret
) {
118 if (UNIT_ISSET(u
->slice
)) {
121 slice
= UNIT_DEREF(u
->slice
);
123 if (slice
->cgroup_path
)
124 n
= strdup(slice
->cgroup_path
);
126 n
= unit_default_cgroup_path(slice
);
128 n
= strdup(u
->manager
->cgroup_root
);
136 static int specifier_runtime(char specifier
, void *data
, void *userdata
, char **ret
) {
143 if (u
->manager
->running_as
== MANAGER_SYSTEM
)
146 e
= getenv("XDG_RUNTIME_DIR");
159 static int specifier_user_name(char specifier
, void *data
, void *userdata
, char **ret
) {
162 /* If we are UID 0 (root), this will not result in NSS,
163 * otherwise it might. This is good, as we want to be able to
164 * run this in PID 1, where our user ID is 0, but where NSS
165 * lookups are not allowed. */
167 t
= getusername_malloc();
175 static int specifier_user_id(char specifier
, void *data
, void *userdata
, char **ret
) {
177 if (asprintf(ret
, UID_FMT
, getuid()) < 0)
183 static int specifier_user_home(char specifier
, void *data
, void *userdata
, char **ret
) {
185 /* On PID 1 (which runs as root) this will not result in NSS,
186 * which is good. See above */
188 return get_home_dir(ret
);
191 static int specifier_user_shell(char specifier
, void *data
, void *userdata
, char **ret
) {
193 /* On PID 1 (which runs as root) this will not result in NSS,
194 * which is good. See above */
196 return get_shell(ret
);
199 int unit_name_printf(Unit
*u
, const char* format
, char **ret
) {
202 * This will use the passed string as format string and
203 * replace the following specifiers:
205 * %n: the full id of the unit (foo@bar.waldo)
206 * %N: the id of the unit without the suffix (foo@bar)
207 * %p: the prefix (foo)
208 * %i: the instance (bar)
211 const Specifier table
[] = {
212 { 'n', specifier_string
, u
->id
},
213 { 'N', specifier_prefix_and_instance
, NULL
},
214 { 'p', specifier_prefix
, NULL
},
215 { 'i', specifier_string
, u
->instance
},
223 return specifier_printf(format
, table
, u
, ret
);
226 int unit_full_printf(Unit
*u
, const char *format
, char **ret
) {
228 /* This is similar to unit_name_printf() but also supports
229 * unescaping. Also, adds a couple of additional codes:
231 * %f the instance if set, otherwise the id
232 * %c cgroup path of unit
233 * %r where units in this slice are placed in the cgroup tree
234 * %R the root of this systemd's instance tree
235 * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR)
236 * %U the UID of the running user
237 * %u the username of the running user
238 * %h the homedir of the running user
239 * %s the shell of the running user
240 * %m the machine ID of the running system
241 * %H the host name of the running system
242 * %b the boot ID of the running system
243 * %v `uname -r` of the running system
246 const Specifier table
[] = {
247 { 'n', specifier_string
, u
->id
},
248 { 'N', specifier_prefix_and_instance
, NULL
},
249 { 'p', specifier_prefix
, NULL
},
250 { 'P', specifier_prefix_unescaped
, NULL
},
251 { 'i', specifier_string
, u
->instance
},
252 { 'I', specifier_instance_unescaped
, NULL
},
254 { 'f', specifier_filename
, NULL
},
255 { 'c', specifier_cgroup
, NULL
},
256 { 'r', specifier_cgroup_slice
, NULL
},
257 { 'R', specifier_cgroup_root
, NULL
},
258 { 't', specifier_runtime
, NULL
},
260 { 'U', specifier_user_id
, NULL
},
261 { 'u', specifier_user_name
, NULL
},
262 { 'h', specifier_user_home
, NULL
},
263 { 's', specifier_user_shell
, NULL
},
265 { 'm', specifier_machine_id
, NULL
},
266 { 'H', specifier_host_name
, NULL
},
267 { 'b', specifier_boot_id
, NULL
},
268 { 'v', specifier_kernel_release
, NULL
},
276 return specifier_printf(format
, table
, u
, ret
);
279 int unit_full_printf_strv(Unit
*u
, char **l
, char ***ret
) {
284 /* Applies unit_full_printf to every entry in l */
293 for (i
= l
, j
= r
; *i
; i
++, j
++) {
294 q
= unit_full_printf(u
, *i
, j
);
304 for (j
--; j
>= r
; j
--)