]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/unit-printf.c
Merge pull request #7599 from keszybz/slice-templates
[thirdparty/systemd.git] / src / core / unit-printf.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include "alloc-util.h"
9 #include "cgroup-util.h"
10 #include "format-util.h"
11 #include "macro.h"
12 #include "specifier.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "unit-name.h"
16 #include "unit-printf.h"
17 #include "unit.h"
18 #include "user-util.h"
19
20 static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
21 Unit *u = userdata;
22
23 assert(u);
24
25 return unit_name_to_prefix_and_instance(u->id, ret);
26 }
27
28 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
29 Unit *u = userdata;
30
31 assert(u);
32
33 return unit_name_to_prefix(u->id, ret);
34 }
35
36 static int specifier_prefix_unescaped(char specifier, void *data, void *userdata, char **ret) {
37 _cleanup_free_ char *p = NULL;
38 Unit *u = userdata;
39 int r;
40
41 assert(u);
42
43 r = unit_name_to_prefix(u->id, &p);
44 if (r < 0)
45 return r;
46
47 return unit_name_unescape(p, ret);
48 }
49
50 static int specifier_instance_unescaped(char specifier, void *data, void *userdata, char **ret) {
51 Unit *u = userdata;
52
53 assert(u);
54
55 return unit_name_unescape(strempty(u->instance), ret);
56 }
57
58 static int specifier_last_component(char specifier, void *data, void *userdata, char **ret) {
59 Unit *u = userdata;
60 _cleanup_free_ char *prefix = NULL;
61 char *dash;
62 int r;
63
64 assert(u);
65
66 r = unit_name_to_prefix(u->id, &prefix);
67 if (r < 0)
68 return r;
69
70 dash = strrchr(prefix, '-');
71 if (dash)
72 return specifier_string(specifier, dash + 1, userdata, ret);
73
74 *ret = TAKE_PTR(prefix);
75 return 0;
76 }
77
78 static int specifier_last_component_unescaped(char specifier, void *data, void *userdata, char **ret) {
79 _cleanup_free_ char *p = NULL;
80 int r;
81
82 r = specifier_last_component(specifier, data, userdata, &p);
83 if (r < 0)
84 return r;
85
86 return unit_name_unescape(p, ret);
87 }
88
89 static int specifier_filename(char specifier, void *data, void *userdata, char **ret) {
90 Unit *u = userdata;
91
92 assert(u);
93
94 if (u->instance)
95 return unit_name_path_unescape(u->instance, ret);
96 else
97 return unit_name_to_path(u->id, ret);
98 }
99
100 static void bad_specifier(Unit *u, char specifier) {
101 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);
102 }
103
104 static int specifier_cgroup(char specifier, void *data, void *userdata, char **ret) {
105 Unit *u = userdata;
106 char *n;
107
108 assert(u);
109
110 bad_specifier(u, specifier);
111
112 if (u->cgroup_path)
113 n = strdup(u->cgroup_path);
114 else
115 n = unit_default_cgroup_path(u);
116 if (!n)
117 return -ENOMEM;
118
119 *ret = n;
120 return 0;
121 }
122
123 static int specifier_cgroup_root(char specifier, void *data, void *userdata, char **ret) {
124 Unit *u = userdata;
125 char *n;
126
127 assert(u);
128
129 bad_specifier(u, specifier);
130
131 n = strdup(u->manager->cgroup_root);
132 if (!n)
133 return -ENOMEM;
134
135 *ret = n;
136 return 0;
137 }
138
139 static int specifier_cgroup_slice(char specifier, void *data, void *userdata, char **ret) {
140 Unit *u = userdata;
141 char *n;
142
143 assert(u);
144
145 bad_specifier(u, specifier);
146
147 if (UNIT_ISSET(u->slice)) {
148 Unit *slice;
149
150 slice = UNIT_DEREF(u->slice);
151
152 if (slice->cgroup_path)
153 n = strdup(slice->cgroup_path);
154 else
155 n = unit_default_cgroup_path(slice);
156 } else
157 n = strdup(u->manager->cgroup_root);
158 if (!n)
159 return -ENOMEM;
160
161 *ret = n;
162 return 0;
163 }
164
165 static int specifier_special_directory(char specifier, void *data, void *userdata, char **ret) {
166 Unit *u = userdata;
167 char *n = NULL;
168
169 assert(u);
170
171 n = strdup(u->manager->prefix[PTR_TO_UINT(data)]);
172 if (!n)
173 return -ENOMEM;
174
175 *ret = n;
176 return 0;
177 }
178
179 int unit_name_printf(Unit *u, const char* format, char **ret) {
180
181 /*
182 * This will use the passed string as format string and replace the following specifiers (which should all be
183 * safe for inclusion in unit names):
184 *
185 * %n: the full id of the unit (foo@bar.waldo)
186 * %N: the id of the unit without the suffix (foo@bar)
187 * %p: the prefix (foo)
188 * %i: the instance (bar)
189 *
190 * %U: the UID of the running user
191 * %u: the username of the running user
192 *
193 * %m: the machine ID of the running system
194 * %H: the host name of the running system
195 * %b: the boot ID of the running system
196 */
197
198 const Specifier table[] = {
199 { 'n', specifier_string, u->id },
200 { 'N', specifier_prefix_and_instance, NULL },
201 { 'p', specifier_prefix, NULL },
202 { 'i', specifier_string, u->instance },
203
204 { 'U', specifier_user_id, NULL },
205 { 'u', specifier_user_name, NULL },
206
207 { 'm', specifier_machine_id, NULL },
208 { 'H', specifier_host_name, NULL },
209 { 'b', specifier_boot_id, NULL },
210 {}
211 };
212
213 assert(u);
214 assert(format);
215 assert(ret);
216
217 return specifier_printf(format, table, u, ret);
218 }
219
220 int unit_full_printf(Unit *u, const char *format, char **ret) {
221
222 /* This is similar to unit_name_printf() but also supports unescaping. Also, adds a couple of additional codes
223 * (which are likely not suitable for unescaped inclusion in unit names):
224 *
225 * %f: the unescaped instance if set, otherwise the id unescaped as path
226 *
227 * %c: cgroup path of unit (deprecated)
228 * %r: where units in this slice are placed in the cgroup tree (deprecated)
229 * %R: the root of this systemd's instance tree (deprecated)
230 *
231 * %t: the runtime directory root (e.g. /run or $XDG_RUNTIME_DIR)
232 * %S: the state directory root (e.g. /var/lib or $XDG_CONFIG_HOME)
233 * %C: the cache directory root (e.g. /var/cache or $XDG_CACHE_HOME)
234 * %L: the log directory root (e.g. /var/log or $XDG_CONFIG_HOME/log)
235 *
236 * %h: the homedir of the running user
237 * %s: the shell of the running user
238 *
239 * %v: `uname -r` of the running system
240 *
241 * NOTICE: When you add new entries here, please be careful: specifiers which depend on settings of the unit
242 * file itself are broken by design, as they would resolve differently depending on whether they are used
243 * before or after the relevant configuration setting. Hence: don't add them.
244 */
245
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 },
253 { 'j', specifier_last_component, NULL },
254 { 'J', specifier_last_component_unescaped, NULL },
255
256 { 'f', specifier_filename, NULL },
257 { 'c', specifier_cgroup, NULL },
258 { 'r', specifier_cgroup_slice, NULL },
259 { 'R', specifier_cgroup_root, NULL },
260 { 't', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_RUNTIME) },
261 { 'S', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_STATE) },
262 { 'C', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_CACHE) },
263 { 'L', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_LOGS) },
264
265 { 'U', specifier_user_id, NULL },
266 { 'u', specifier_user_name, NULL },
267 { 'h', specifier_user_home, NULL },
268 { 's', specifier_user_shell, NULL },
269
270 { 'm', specifier_machine_id, NULL },
271 { 'H', specifier_host_name, NULL },
272 { 'b', specifier_boot_id, NULL },
273 { 'v', specifier_kernel_release, NULL },
274 {}
275 };
276
277 assert(u);
278 assert(format);
279 assert(ret);
280
281 return specifier_printf(format, table, u, ret);
282 }
283
284 int unit_full_printf_strv(Unit *u, char **l, char ***ret) {
285 size_t n;
286 char **r, **i, **j;
287 int q;
288
289 /* Applies unit_full_printf to every entry in l */
290
291 assert(u);
292
293 n = strv_length(l);
294 r = new(char*, n+1);
295 if (!r)
296 return -ENOMEM;
297
298 for (i = l, j = r; *i; i++, j++) {
299 q = unit_full_printf(u, *i, j);
300 if (q < 0)
301 goto fail;
302 }
303
304 *j = NULL;
305 *ret = r;
306 return 0;
307
308 fail:
309 for (j--; j >= r; j--)
310 free(*j);
311
312 free(r);
313 return q;
314 }