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