]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/unit-printf.c
Merge pull request #1695 from evverx/fix-cap-bounding-merging
[thirdparty/systemd.git] / src / core / unit-printf.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include "alloc-util.h"
23 #include "cgroup-util.h"
24 #include "formats-util.h"
25 #include "macro.h"
26 #include "specifier.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "unit-name.h"
30 #include "unit-printf.h"
31 #include "unit.h"
32 #include "user-util.h"
33
34 static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
35 Unit *u = userdata;
36
37 assert(u);
38
39 return unit_name_to_prefix_and_instance(u->id, ret);
40 }
41
42 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
43 Unit *u = userdata;
44
45 assert(u);
46
47 return unit_name_to_prefix(u->id, ret);
48 }
49
50 static int specifier_prefix_unescaped(char specifier, void *data, void *userdata, char **ret) {
51 _cleanup_free_ char *p = NULL;
52 Unit *u = userdata;
53 int r;
54
55 assert(u);
56
57 r = unit_name_to_prefix(u->id, &p);
58 if (r < 0)
59 return r;
60
61 return unit_name_unescape(p, ret);
62 }
63
64 static int specifier_instance_unescaped(char specifier, void *data, void *userdata, char **ret) {
65 Unit *u = userdata;
66
67 assert(u);
68
69 return unit_name_unescape(strempty(u->instance), ret);
70 }
71
72 static int specifier_filename(char specifier, void *data, void *userdata, char **ret) {
73 Unit *u = userdata;
74
75 assert(u);
76
77 if (u->instance)
78 return unit_name_path_unescape(u->instance, ret);
79 else
80 return unit_name_to_path(u->id, ret);
81 }
82
83 static int specifier_cgroup(char specifier, void *data, void *userdata, char **ret) {
84 Unit *u = userdata;
85 char *n;
86
87 assert(u);
88
89 if (u->cgroup_path)
90 n = strdup(u->cgroup_path);
91 else
92 n = unit_default_cgroup_path(u);
93 if (!n)
94 return -ENOMEM;
95
96 *ret = n;
97 return 0;
98 }
99
100 static int specifier_cgroup_root(char specifier, void *data, void *userdata, char **ret) {
101 Unit *u = userdata;
102 char *n;
103
104 assert(u);
105
106 n = strdup(u->manager->cgroup_root);
107 if (!n)
108 return -ENOMEM;
109
110 *ret = n;
111 return 0;
112 }
113
114 static int specifier_cgroup_slice(char specifier, void *data, void *userdata, char **ret) {
115 Unit *u = userdata;
116 char *n;
117
118 assert(u);
119
120 if (UNIT_ISSET(u->slice)) {
121 Unit *slice;
122
123 slice = UNIT_DEREF(u->slice);
124
125 if (slice->cgroup_path)
126 n = strdup(slice->cgroup_path);
127 else
128 n = unit_default_cgroup_path(slice);
129 } else
130 n = strdup(u->manager->cgroup_root);
131 if (!n)
132 return -ENOMEM;
133
134 *ret = n;
135 return 0;
136 }
137
138 static int specifier_runtime(char specifier, void *data, void *userdata, char **ret) {
139 Unit *u = userdata;
140 const char *e;
141 char *n = NULL;
142
143 assert(u);
144
145 if (u->manager->running_as == MANAGER_SYSTEM)
146 e = "/run";
147 else {
148 e = getenv("XDG_RUNTIME_DIR");
149 if (!e)
150 return -EOPNOTSUPP;
151 }
152
153 n = strdup(e);
154 if (!n)
155 return -ENOMEM;
156
157 *ret = n;
158 return 0;
159 }
160
161 static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
162 char *printed = NULL;
163 Unit *u = userdata;
164 ExecContext *c;
165 int r = 0;
166
167 assert(u);
168
169 c = unit_get_exec_context(u);
170 if (!c)
171 return -EOPNOTSUPP;
172
173 if (u->manager->running_as == MANAGER_SYSTEM) {
174
175 /* We cannot use NSS from PID 1, hence try to make the
176 * best of it in that case, and fail if we can't help
177 * it */
178
179 if (!c->user || streq(c->user, "root") || streq(c->user, "0"))
180 printed = strdup(specifier == 'u' ? "root" : "0");
181 else {
182 if (specifier == 'u')
183 printed = strdup(c->user);
184 else {
185 uid_t uid;
186
187 r = parse_uid(c->user, &uid);
188 if (r < 0)
189 return -ENODATA;
190
191 r = asprintf(&printed, UID_FMT, uid);
192 }
193 }
194
195 } else {
196 _cleanup_free_ char *tmp = NULL;
197 const char *username = NULL;
198 uid_t uid;
199
200 if (c->user)
201 username = c->user;
202 else
203 /* get USER env from env or our own uid */
204 username = tmp = getusername_malloc();
205
206 /* fish username from passwd */
207 r = get_user_creds(&username, &uid, NULL, NULL, NULL);
208 if (r < 0)
209 return r;
210
211 if (specifier == 'u')
212 printed = strdup(username);
213 else
214 r = asprintf(&printed, UID_FMT, uid);
215 }
216
217 if (r < 0 || !printed)
218 return -ENOMEM;
219
220 *ret = printed;
221 return 0;
222 }
223
224 static int specifier_user_home(char specifier, void *data, void *userdata, char **ret) {
225 Unit *u = userdata;
226 ExecContext *c;
227 char *n;
228 int r;
229
230 assert(u);
231
232 c = unit_get_exec_context(u);
233 if (!c)
234 return -EOPNOTSUPP;
235
236 if (u->manager->running_as == MANAGER_SYSTEM) {
237
238 /* We cannot use NSS from PID 1, hence try to make the
239 * best of it if we can, but fail if we can't */
240
241 if (!c->user || streq(c->user, "root") || streq(c->user, "0"))
242 n = strdup("/root");
243 else
244 return -EOPNOTSUPP;
245
246 } else {
247
248 /* return HOME if set, otherwise from passwd */
249 if (!c || !c->user) {
250 r = get_home_dir(&n);
251 if (r < 0)
252 return r;
253 } else {
254 const char *username, *home;
255
256 username = c->user;
257 r = get_user_creds(&username, NULL, NULL, &home, NULL);
258 if (r < 0)
259 return r;
260
261 n = strdup(home);
262 }
263 }
264
265 if (!n)
266 return -ENOMEM;
267
268 *ret = n;
269 return 0;
270 }
271
272 static int specifier_user_shell(char specifier, void *data, void *userdata, char **ret) {
273 Unit *u = userdata;
274 ExecContext *c;
275 char *n;
276 int r;
277
278 assert(u);
279
280 c = unit_get_exec_context(u);
281 if (!c)
282 return -EOPNOTSUPP;
283
284 if (u->manager->running_as == MANAGER_SYSTEM) {
285
286 /* We cannot use NSS from PID 1, hence try to make the
287 * best of it if we can, but fail if we can't */
288
289 if (!c->user || streq(c->user, "root") || streq(c->user, "0"))
290 n = strdup("/bin/sh");
291 else
292 return -EOPNOTSUPP;
293
294 } else {
295
296 /* return /bin/sh for root, otherwise the value from passwd */
297 if (!c->user) {
298 r = get_shell(&n);
299 if (r < 0)
300 return r;
301 } else {
302 const char *username, *shell;
303
304 username = c->user;
305 r = get_user_creds(&username, NULL, NULL, NULL, &shell);
306 if (r < 0)
307 return r;
308
309 n = strdup(shell);
310 }
311 }
312
313 if (!n)
314 return -ENOMEM;
315
316 *ret = n;
317 return 0;
318 }
319
320 int unit_name_printf(Unit *u, const char* format, char **ret) {
321
322 /*
323 * This will use the passed string as format string and
324 * replace the following specifiers:
325 *
326 * %n: the full id of the unit (foo@bar.waldo)
327 * %N: the id of the unit without the suffix (foo@bar)
328 * %p: the prefix (foo)
329 * %i: the instance (bar)
330 */
331
332 const Specifier table[] = {
333 { 'n', specifier_string, u->id },
334 { 'N', specifier_prefix_and_instance, NULL },
335 { 'p', specifier_prefix, NULL },
336 { 'i', specifier_string, u->instance },
337 { 0, NULL, NULL }
338 };
339
340 assert(u);
341 assert(format);
342 assert(ret);
343
344 return specifier_printf(format, table, u, ret);
345 }
346
347 int unit_full_printf(Unit *u, const char *format, char **ret) {
348
349 /* This is similar to unit_name_printf() but also supports
350 * unescaping. Also, adds a couple of additional codes:
351 *
352 * %f the instance if set, otherwise the id
353 * %c cgroup path of unit
354 * %r where units in this slice are placed in the cgroup tree
355 * %R the root of this systemd's instance tree
356 * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR)
357 * %U the UID of the configured user or running user
358 * %u the username of the configured user or running user
359 * %h the homedir of the configured user or running user
360 * %s the shell of the configured user or running user
361 * %m the machine ID of the running system
362 * %H the host name of the running system
363 * %b the boot ID of the running system
364 * %v `uname -r` of the running system
365 */
366
367 const Specifier table[] = {
368 { 'n', specifier_string, u->id },
369 { 'N', specifier_prefix_and_instance, NULL },
370 { 'p', specifier_prefix, NULL },
371 { 'P', specifier_prefix_unescaped, NULL },
372 { 'i', specifier_string, u->instance },
373 { 'I', specifier_instance_unescaped, NULL },
374
375 { 'f', specifier_filename, NULL },
376 { 'c', specifier_cgroup, NULL },
377 { 'r', specifier_cgroup_slice, NULL },
378 { 'R', specifier_cgroup_root, NULL },
379 { 't', specifier_runtime, NULL },
380 { 'U', specifier_user_name, NULL },
381 { 'u', specifier_user_name, NULL },
382 { 'h', specifier_user_home, NULL },
383 { 's', specifier_user_shell, NULL },
384
385 { 'm', specifier_machine_id, NULL },
386 { 'H', specifier_host_name, NULL },
387 { 'b', specifier_boot_id, NULL },
388 { 'v', specifier_kernel_release, NULL },
389 {}
390 };
391
392 assert(u);
393 assert(format);
394 assert(ret);
395
396 return specifier_printf(format, table, u, ret);
397 }
398
399 int unit_full_printf_strv(Unit *u, char **l, char ***ret) {
400 size_t n;
401 char **r, **i, **j;
402 int q;
403
404 /* Applies unit_full_printf to every entry in l */
405
406 assert(u);
407
408 n = strv_length(l);
409 r = new(char*, n+1);
410 if (!r)
411 return -ENOMEM;
412
413 for (i = l, j = r; *i; i++, j++) {
414 q = unit_full_printf(u, *i, j);
415 if (q < 0)
416 goto fail;
417 }
418
419 *j = NULL;
420 *ret = r;
421 return 0;
422
423 fail:
424 for (j--; j >= r; j--)
425 free(*j);
426
427 free(r);
428 return q;
429 }