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