]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/unit-printf.c
io-util: add new IOVEC_INIT/IOVEC_MAKE macros
[thirdparty/systemd.git] / src / core / unit-printf.c
CommitLineData
41f9172f
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
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.
10
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.
15
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/>.
18***/
19
b5efdb8a 20#include "alloc-util.h"
07630cea 21#include "cgroup-util.h"
f97b34a6 22#include "format-util.h"
07630cea 23#include "macro.h"
41f9172f 24#include "specifier.h"
07630cea 25#include "string-util.h"
41f9172f
LP
26#include "strv.h"
27#include "unit-name.h"
28#include "unit-printf.h"
b1d4f8e1
LP
29#include "unit.h"
30#include "user-util.h"
41f9172f 31
19f6d710 32static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
41f9172f 33 Unit *u = userdata;
19f6d710 34
41f9172f
LP
35 assert(u);
36
7410616c 37 return unit_name_to_prefix_and_instance(u->id, ret);
41f9172f
LP
38}
39
19f6d710 40static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
41f9172f 41 Unit *u = userdata;
19f6d710 42
41f9172f
LP
43 assert(u);
44
7410616c 45 return unit_name_to_prefix(u->id, ret);
41f9172f
LP
46}
47
19f6d710 48static int specifier_prefix_unescaped(char specifier, void *data, void *userdata, char **ret) {
19f6d710 49 _cleanup_free_ char *p = NULL;
7410616c
LP
50 Unit *u = userdata;
51 int r;
41f9172f
LP
52
53 assert(u);
54
7410616c
LP
55 r = unit_name_to_prefix(u->id, &p);
56 if (r < 0)
57 return r;
41f9172f 58
7410616c 59 return unit_name_unescape(p, ret);
41f9172f
LP
60}
61
19f6d710 62static int specifier_instance_unescaped(char specifier, void *data, void *userdata, char **ret) {
41f9172f 63 Unit *u = userdata;
19f6d710 64
41f9172f
LP
65 assert(u);
66
115cbf7e 67 return unit_name_unescape(strempty(u->instance), ret);
41f9172f
LP
68}
69
19f6d710 70static int specifier_filename(char specifier, void *data, void *userdata, char **ret) {
41f9172f 71 Unit *u = userdata;
19f6d710 72
41f9172f
LP
73 assert(u);
74
75 if (u->instance)
7410616c 76 return unit_name_path_unescape(u->instance, ret);
19f6d710 77 else
7410616c 78 return unit_name_to_path(u->id, ret);
41f9172f
LP
79}
80
1b89b0c4
LP
81static void bad_specifier(Unit *u, char specifier) {
82 log_unit_warning(u, "Specifier '%%%c' used in unit configuration, which is deprecated. Please update your unit file, as it does not work as intended.", specifier);
83}
84
19f6d710 85static int specifier_cgroup(char specifier, void *data, void *userdata, char **ret) {
41f9172f 86 Unit *u = userdata;
19f6d710
LP
87 char *n;
88
41f9172f
LP
89 assert(u);
90
1b89b0c4
LP
91 bad_specifier(u, specifier);
92
2cfbd749
LP
93 if (u->cgroup_path)
94 n = strdup(u->cgroup_path);
95 else
96 n = unit_default_cgroup_path(u);
19f6d710
LP
97 if (!n)
98 return -ENOMEM;
99
100 *ret = n;
101 return 0;
41f9172f
LP
102}
103
19f6d710 104static int specifier_cgroup_root(char specifier, void *data, void *userdata, char **ret) {
41f9172f 105 Unit *u = userdata;
19f6d710 106 char *n;
41f9172f 107
9444b1f2 108 assert(u);
41f9172f 109
1b89b0c4
LP
110 bad_specifier(u, specifier);
111
696fd1ef
LP
112 n = strdup(u->manager->cgroup_root);
113 if (!n)
114 return -ENOMEM;
41f9172f 115
696fd1ef
LP
116 *ret = n;
117 return 0;
118}
19f6d710 119
696fd1ef
LP
120static int specifier_cgroup_slice(char specifier, void *data, void *userdata, char **ret) {
121 Unit *u = userdata;
122 char *n;
123
124 assert(u);
125
1b89b0c4
LP
126 bad_specifier(u, specifier);
127
696fd1ef
LP
128 if (UNIT_ISSET(u->slice)) {
129 Unit *slice;
130
131 slice = UNIT_DEREF(u->slice);
132
133 if (slice->cgroup_path)
134 n = strdup(slice->cgroup_path);
135 else
136 n = unit_default_cgroup_path(slice);
137 } else
138 n = strdup(u->manager->cgroup_root);
115cbf7e
EV
139 if (!n)
140 return -ENOMEM;
41f9172f 141
19f6d710
LP
142 *ret = n;
143 return 0;
41f9172f
LP
144}
145
19f6d710 146static int specifier_runtime(char specifier, void *data, void *userdata, char **ret) {
41f9172f 147 Unit *u = userdata;
19f6d710
LP
148 char *n = NULL;
149
41f9172f
LP
150 assert(u);
151
3536f49e 152 n = strdup(u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
2cfbd749
LP
153 if (!n)
154 return -ENOMEM;
19f6d710
LP
155
156 *ret = n;
157 return 0;
41f9172f
LP
158}
159
19f6d710 160static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
79413b67 161 char *t;
2cfbd749 162
79413b67
LP
163 /* If we are UID 0 (root), this will not result in NSS,
164 * otherwise it might. This is good, as we want to be able to
165 * run this in PID 1, where our user ID is 0, but where NSS
166 * lookups are not allowed. */
067d851d 167
79413b67
LP
168 t = getusername_malloc();
169 if (!t)
19f6d710
LP
170 return -ENOMEM;
171
79413b67 172 *ret = t;
19f6d710 173 return 0;
41f9172f
LP
174}
175
79413b67 176static int specifier_user_id(char specifier, void *data, void *userdata, char **ret) {
41f9172f 177
79413b67 178 if (asprintf(ret, UID_FMT, getuid()) < 0)
19f6d710
LP
179 return -ENOMEM;
180
19f6d710 181 return 0;
41f9172f
LP
182}
183
79413b67 184static int specifier_user_home(char specifier, void *data, void *userdata, char **ret) {
2cfbd749 185
79413b67
LP
186 /* On PID 1 (which runs as root) this will not result in NSS,
187 * which is good. See above */
2cfbd749 188
79413b67
LP
189 return get_home_dir(ret);
190}
2cfbd749 191
79413b67 192static int specifier_user_shell(char specifier, void *data, void *userdata, char **ret) {
41f9172f 193
79413b67
LP
194 /* On PID 1 (which runs as root) this will not result in NSS,
195 * which is good. See above */
41f9172f 196
79413b67 197 return get_shell(ret);
41f9172f
LP
198}
199
19f6d710 200int unit_name_printf(Unit *u, const char* format, char **ret) {
41f9172f
LP
201
202 /*
b1801e64
LP
203 * This will use the passed string as format string and replace the following specifiers (which should all be
204 * safe for inclusion in unit names):
41f9172f
LP
205 *
206 * %n: the full id of the unit (foo@bar.waldo)
207 * %N: the id of the unit without the suffix (foo@bar)
208 * %p: the prefix (foo)
209 * %i: the instance (bar)
b1801e64
LP
210 *
211 * %U: the UID of the running user
212 * %u: the username of the running user
213 *
214 * %m: the machine ID of the running system
215 * %H: the host name of the running system
216 * %b: the boot ID of the running system
41f9172f
LP
217 */
218
219 const Specifier table[] = {
220 { 'n', specifier_string, u->id },
221 { 'N', specifier_prefix_and_instance, NULL },
222 { 'p', specifier_prefix, NULL },
223 { 'i', specifier_string, u->instance },
b1801e64
LP
224
225 { 'U', specifier_user_id, NULL },
226 { 'u', specifier_user_name, NULL },
227
228 { 'm', specifier_machine_id, NULL },
229 { 'H', specifier_host_name, NULL },
230 { 'b', specifier_boot_id, NULL },
231 {}
41f9172f
LP
232 };
233
234 assert(u);
235 assert(format);
19f6d710 236 assert(ret);
41f9172f 237
19f6d710 238 return specifier_printf(format, table, u, ret);
41f9172f
LP
239}
240
19f6d710 241int unit_full_printf(Unit *u, const char *format, char **ret) {
41f9172f 242
b1801e64
LP
243 /* This is similar to unit_name_printf() but also supports unescaping. Also, adds a couple of additional codes
244 * (which are likely not suitable for unescaped inclusion in unit names):
245 *
246 * %f: the unescaped instance if set, otherwise the id unescaped as path
1b89b0c4
LP
247 * %c: cgroup path of unit (deprecated)
248 * %r: where units in this slice are placed in the cgroup tree (deprecated)
249 * %R: the root of this systemd's instance tree (deprecated)
b1801e64
LP
250 * %t: the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR)
251 *
252 * %h: the homedir of the running user
253 * %s: the shell of the running user
41f9172f 254 *
b1801e64 255 * %v: `uname -r` of the running system
03fc9c72
LP
256 *
257 * NOTICE: When you add new entries here, please be careful: specifiers which depend on settings of the unit
258 * file itself are broken by design, as they would resolve differently depending on whether they are used
259 * before or after the relevant configuration setting. Hence: don't add them.
41f9172f
LP
260 */
261
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 },
269
270 { 'f', specifier_filename, NULL },
271 { 'c', specifier_cgroup, NULL },
696fd1ef 272 { 'r', specifier_cgroup_slice, NULL },
41f9172f
LP
273 { 'R', specifier_cgroup_root, NULL },
274 { 't', specifier_runtime, NULL },
79413b67
LP
275
276 { 'U', specifier_user_id, NULL },
41f9172f
LP
277 { 'u', specifier_user_name, NULL },
278 { 'h', specifier_user_home, NULL },
279 { 's', specifier_user_shell, NULL },
6569cae1
LP
280
281 { 'm', specifier_machine_id, NULL },
282 { 'H', specifier_host_name, NULL },
283 { 'b', specifier_boot_id, NULL },
6aaa8c2f
ZJS
284 { 'v', specifier_kernel_release, NULL },
285 {}
41f9172f
LP
286 };
287
19f6d710 288 assert(u);
41f9172f 289 assert(format);
19f6d710 290 assert(ret);
41f9172f 291
19f6d710 292 return specifier_printf(format, table, u, ret);
41f9172f
LP
293}
294
19f6d710 295int unit_full_printf_strv(Unit *u, char **l, char ***ret) {
41f9172f
LP
296 size_t n;
297 char **r, **i, **j;
19f6d710 298 int q;
41f9172f
LP
299
300 /* Applies unit_full_printf to every entry in l */
301
302 assert(u);
303
304 n = strv_length(l);
305 r = new(char*, n+1);
306 if (!r)
19f6d710 307 return -ENOMEM;
41f9172f
LP
308
309 for (i = l, j = r; *i; i++, j++) {
19f6d710
LP
310 q = unit_full_printf(u, *i, j);
311 if (q < 0)
41f9172f
LP
312 goto fail;
313 }
314
315 *j = NULL;
19f6d710
LP
316 *ret = r;
317 return 0;
41f9172f
LP
318
319fail:
320 for (j--; j >= r; j--)
321 free(*j);
322
323 free(r);
19f6d710 324 return q;
41f9172f 325}