]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/unit-printf.c
Merge pull request #8417 from brauner/2018-03-09/add_bind_mount_fallback_to_private_d...
[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_filename(char specifier, void *data, void *userdata, char **ret) {
59 Unit *u = userdata;
60
61 assert(u);
62
63 if (u->instance)
64 return unit_name_path_unescape(u->instance, ret);
65 else
66 return unit_name_to_path(u->id, ret);
67 }
68
69 static void bad_specifier(Unit *u, char specifier) {
70 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);
71 }
72
73 static int specifier_cgroup(char specifier, void *data, void *userdata, char **ret) {
74 Unit *u = userdata;
75 char *n;
76
77 assert(u);
78
79 bad_specifier(u, specifier);
80
81 if (u->cgroup_path)
82 n = strdup(u->cgroup_path);
83 else
84 n = unit_default_cgroup_path(u);
85 if (!n)
86 return -ENOMEM;
87
88 *ret = n;
89 return 0;
90 }
91
92 static int specifier_cgroup_root(char specifier, void *data, void *userdata, char **ret) {
93 Unit *u = userdata;
94 char *n;
95
96 assert(u);
97
98 bad_specifier(u, specifier);
99
100 n = strdup(u->manager->cgroup_root);
101 if (!n)
102 return -ENOMEM;
103
104 *ret = n;
105 return 0;
106 }
107
108 static int specifier_cgroup_slice(char specifier, void *data, void *userdata, char **ret) {
109 Unit *u = userdata;
110 char *n;
111
112 assert(u);
113
114 bad_specifier(u, specifier);
115
116 if (UNIT_ISSET(u->slice)) {
117 Unit *slice;
118
119 slice = UNIT_DEREF(u->slice);
120
121 if (slice->cgroup_path)
122 n = strdup(slice->cgroup_path);
123 else
124 n = unit_default_cgroup_path(slice);
125 } else
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_special_directory(char specifier, void *data, void *userdata, char **ret) {
135 Unit *u = userdata;
136 char *n = NULL;
137
138 assert(u);
139
140 n = strdup(u->manager->prefix[PTR_TO_UINT(data)]);
141 if (!n)
142 return -ENOMEM;
143
144 *ret = n;
145 return 0;
146 }
147
148 int unit_name_printf(Unit *u, const char* format, char **ret) {
149
150 /*
151 * This will use the passed string as format string and replace the following specifiers (which should all be
152 * safe for inclusion in unit names):
153 *
154 * %n: the full id of the unit (foo@bar.waldo)
155 * %N: the id of the unit without the suffix (foo@bar)
156 * %p: the prefix (foo)
157 * %i: the instance (bar)
158 *
159 * %U: the UID of the running user
160 * %u: the username of the running user
161 *
162 * %m: the machine ID of the running system
163 * %H: the host name of the running system
164 * %b: the boot ID of the running system
165 */
166
167 const Specifier table[] = {
168 { 'n', specifier_string, u->id },
169 { 'N', specifier_prefix_and_instance, NULL },
170 { 'p', specifier_prefix, NULL },
171 { 'i', specifier_string, u->instance },
172
173 { 'U', specifier_user_id, NULL },
174 { 'u', specifier_user_name, NULL },
175
176 { 'm', specifier_machine_id, NULL },
177 { 'H', specifier_host_name, NULL },
178 { 'b', specifier_boot_id, NULL },
179 {}
180 };
181
182 assert(u);
183 assert(format);
184 assert(ret);
185
186 return specifier_printf(format, table, u, ret);
187 }
188
189 int unit_full_printf(Unit *u, const char *format, char **ret) {
190
191 /* This is similar to unit_name_printf() but also supports unescaping. Also, adds a couple of additional codes
192 * (which are likely not suitable for unescaped inclusion in unit names):
193 *
194 * %f: the unescaped instance if set, otherwise the id unescaped as path
195 *
196 * %c: cgroup path of unit (deprecated)
197 * %r: where units in this slice are placed in the cgroup tree (deprecated)
198 * %R: the root of this systemd's instance tree (deprecated)
199 *
200 * %t: the runtime directory root (e.g. /run or $XDG_RUNTIME_DIR)
201 * %S: the state directory root (e.g. /var/lib or $XDG_CONFIG_HOME)
202 * %C: the cache directory root (e.g. /var/cache or $XDG_CACHE_HOME)
203 * %L: the log directory root (e.g. /var/log or $XDG_CONFIG_HOME/log)
204 *
205 * %h: the homedir of the running user
206 * %s: the shell of the running user
207 *
208 * %v: `uname -r` of the running system
209 *
210 * NOTICE: When you add new entries here, please be careful: specifiers which depend on settings of the unit
211 * file itself are broken by design, as they would resolve differently depending on whether they are used
212 * before or after the relevant configuration setting. Hence: don't add them.
213 */
214
215 const Specifier table[] = {
216 { 'n', specifier_string, u->id },
217 { 'N', specifier_prefix_and_instance, NULL },
218 { 'p', specifier_prefix, NULL },
219 { 'P', specifier_prefix_unescaped, NULL },
220 { 'i', specifier_string, u->instance },
221 { 'I', specifier_instance_unescaped, NULL },
222
223 { 'f', specifier_filename, NULL },
224 { 'c', specifier_cgroup, NULL },
225 { 'r', specifier_cgroup_slice, NULL },
226 { 'R', specifier_cgroup_root, NULL },
227 { 't', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_RUNTIME) },
228 { 'S', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_STATE) },
229 { 'C', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_CACHE) },
230 { 'L', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_LOGS) },
231
232 { 'U', specifier_user_id, NULL },
233 { 'u', specifier_user_name, NULL },
234 { 'h', specifier_user_home, NULL },
235 { 's', specifier_user_shell, NULL },
236
237 { 'm', specifier_machine_id, NULL },
238 { 'H', specifier_host_name, NULL },
239 { 'b', specifier_boot_id, NULL },
240 { 'v', specifier_kernel_release, NULL },
241 {}
242 };
243
244 assert(u);
245 assert(format);
246 assert(ret);
247
248 return specifier_printf(format, table, u, ret);
249 }
250
251 int unit_full_printf_strv(Unit *u, char **l, char ***ret) {
252 size_t n;
253 char **r, **i, **j;
254 int q;
255
256 /* Applies unit_full_printf to every entry in l */
257
258 assert(u);
259
260 n = strv_length(l);
261 r = new(char*, n+1);
262 if (!r)
263 return -ENOMEM;
264
265 for (i = l, j = r; *i; i++, j++) {
266 q = unit_full_printf(u, *i, j);
267 if (q < 0)
268 goto fail;
269 }
270
271 *j = NULL;
272 *ret = r;
273 return 0;
274
275 fail:
276 for (j--; j >= r; j--)
277 free(*j);
278
279 free(r);
280 return q;
281 }