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