chiark / gitweb /
shutdown: don't chdir to /, since init is running with / as cwd anyway
[elogind.git] / src / unit-name.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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <string.h>
24 #include <assert.h>
25
26 #include "util.h"
27 #include "unit-name.h"
28
29 #define VALID_CHARS                             \
30         "0123456789"                            \
31         "abcdefghijklmnopqrstuvwxyz"            \
32         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"            \
33         ":-_.\\"
34
35 bool unit_name_is_valid_no_type(const char *n, bool template_ok) {
36         const char *e, *i, *at;
37
38         /* Valid formats:
39          *
40          *         string@instance.suffix
41          *         string.suffix
42          */
43
44         assert(n);
45
46         if (strlen(n) >= UNIT_NAME_MAX)
47                 return false;
48
49         e = strrchr(n, '.');
50         if (!e || e == n)
51                 return false;
52
53         for (i = n, at = NULL; i < e; i++) {
54
55                 if (*i == '@' && !at)
56                         at = i;
57
58                 if (!strchr("@" VALID_CHARS, *i))
59                         return false;
60         }
61
62         if (at) {
63                 if (at == n)
64                         return false;
65
66                 if (!template_ok && at+1 == e)
67                         return false;
68         }
69
70         return true;
71 }
72
73 bool unit_instance_is_valid(const char *i) {
74         assert(i);
75
76         /* The max length depends on the length of the string, so we
77          * don't really check this here. */
78
79         if (i[0] == 0)
80                 return false;
81
82         /* We allow additional @ in the instance string, we do not
83          * allow them in the prefix! */
84
85         for (; *i; i++)
86                 if (!strchr("@" VALID_CHARS, *i))
87                         return false;
88
89         return true;
90 }
91
92 bool unit_prefix_is_valid(const char *p) {
93
94         /* We don't allow additional @ in the instance string */
95
96         if (p[0] == 0)
97                 return false;
98
99         for (; *p; p++)
100                 if (!strchr(VALID_CHARS, *p))
101                         return false;
102
103         return true;
104 }
105
106 int unit_name_to_instance(const char *n, char **instance) {
107         const char *p, *d;
108         char *i;
109
110         assert(n);
111         assert(instance);
112
113         /* Everything past the first @ and before the last . is the instance */
114         if (!(p = strchr(n, '@'))) {
115                 *instance = NULL;
116                 return 0;
117         }
118
119         assert_se(d = strrchr(n, '.'));
120         assert(p < d);
121
122         if (!(i = strndup(p+1, d-p-1)))
123                 return -ENOMEM;
124
125         *instance = i;
126         return 0;
127 }
128
129 char *unit_name_to_prefix_and_instance(const char *n) {
130         const char *d;
131
132         assert(n);
133
134         assert_se(d = strrchr(n, '.'));
135
136         return strndup(n, d - n);
137 }
138
139 char *unit_name_to_prefix(const char *n) {
140         const char *p;
141
142         if ((p = strchr(n, '@')))
143                 return strndup(n, p - n);
144
145         return unit_name_to_prefix_and_instance(n);
146 }
147
148 char *unit_name_change_suffix(const char *n, const char *suffix) {
149         char *e, *r;
150         size_t a, b;
151
152         assert(n);
153         assert(unit_name_is_valid_no_type(n, true));
154         assert(suffix);
155
156         assert_se(e = strrchr(n, '.'));
157         a = e - n;
158         b = strlen(suffix);
159
160         if (!(r = new(char, a + b + 1)))
161                 return NULL;
162
163         memcpy(r, n, a);
164         memcpy(r+a, suffix, b+1);
165
166         return r;
167 }
168
169 char *unit_name_build(const char *prefix, const char *instance, const char *suffix) {
170         char *r;
171
172         assert(prefix);
173         assert(unit_prefix_is_valid(prefix));
174         assert(!instance || unit_instance_is_valid(instance));
175         assert(suffix);
176
177         if (!instance)
178                 return strappend(prefix, suffix);
179
180         if (asprintf(&r, "%s@%s%s", prefix, instance, suffix) < 0)
181                 return NULL;
182
183         return r;
184 }
185
186 static char* do_escape(const char *f, char *t) {
187         assert(f);
188         assert(t);
189
190         for (; *f; f++) {
191                 if (*f == '/')
192                         *(t++) = '-';
193                 else if (*f == '-' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
194                         *(t++) = '\\';
195                         *(t++) = 'x';
196                         *(t++) = hexchar(*f >> 4);
197                         *(t++) = hexchar(*f);
198                 } else
199                         *(t++) = *f;
200         }
201
202         return t;
203 }
204
205 char *unit_name_build_escape(const char *prefix, const char *instance, const char *suffix) {
206         char *r, *t;
207         size_t a, b, c;
208
209         assert(prefix);
210         assert(suffix);
211
212         /* Takes a arbitrary string for prefix and instance plus a
213          * suffix and makes a nice string suitable as unit name of it,
214          * escaping all weird chars on the way.
215          *
216          * / becomes ., and all chars not alloweed in a unit name get
217          * escaped as \xFF, including \ and ., of course. This
218          * escaping is hence reversible.
219          *
220          * This is primarily useful to make nice unit names from
221          * strings, but is actually useful for any kind of string.
222          */
223
224         a = strlen(prefix);
225         c = strlen(suffix);
226
227         if (instance) {
228                 b = strlen(instance);
229
230                 if (!(r = new(char, a*4 + 1 + b*4 + c + 1)))
231                         return NULL;
232
233                 t = do_escape(prefix, r);
234                 *(t++) = '@';
235                 t = do_escape(instance, t);
236         } else {
237
238                 if (!(r = new(char, a*4 + c + 1)))
239                         return NULL;
240
241                 t = do_escape(prefix, r);
242         }
243
244         strcpy(t, suffix);
245         return r;
246 }
247
248 char *unit_name_escape(const char *f) {
249         char *r, *t;
250
251         if (!(r = new(char, strlen(f)*4+1)))
252                 return NULL;
253
254         t = do_escape(f, r);
255         *t = 0;
256
257         return r;
258
259 }
260
261 char *unit_name_unescape(const char *f) {
262         char *r, *t;
263
264         assert(f);
265
266         if (!(r = strdup(f)))
267                 return NULL;
268
269         for (t = r; *f; f++) {
270                 if (*f == '-')
271                         *(t++) = '/';
272                 else if (*f == '\\') {
273                         int a, b;
274
275                         if ((a = unhexchar(f[1])) < 0 ||
276                             (b = unhexchar(f[2])) < 0) {
277                                 /* Invalid escape code, let's take it literal then */
278                                 *(t++) = '\\';
279                         } else {
280                                 *(t++) = (char) ((a << 4) | b);
281                                 f += 2;
282                         }
283                 } else
284                         *(t++) = *f;
285         }
286
287         *t = 0;
288
289         return r;
290 }
291
292 bool unit_name_is_template(const char *n) {
293         const char *p;
294
295         assert(n);
296
297         if (!(p = strchr(n, '@')))
298                 return false;
299
300         return p[1] == '.';
301 }
302
303 char *unit_name_replace_instance(const char *f, const char *i) {
304         const char *p, *e;
305         char *r, *k;
306         size_t a;
307
308         assert(f);
309
310         p = strchr(f, '@');
311         assert_se(e = strrchr(f, '.'));
312
313         a = p - f;
314
315         if (p) {
316                 size_t b;
317
318                 b = strlen(i);
319
320                 if (!(r = new(char, a + 1 + b + strlen(e) + 1)))
321                         return NULL;
322
323                 k = mempcpy(r, f, a + 1);
324                 k = mempcpy(k, i, b);
325         } else {
326
327                 if (!(r = new(char, a + strlen(e) + 1)))
328                         return NULL;
329
330                 k = mempcpy(r, f, a);
331         }
332
333         strcpy(k, e);
334         return r;
335 }
336
337 char *unit_name_template(const char *f) {
338         const char *p, *e;
339         char *r;
340         size_t a;
341
342         if (!(p = strchr(f, '@')))
343                 return strdup(f);
344
345         assert_se(e = strrchr(f, '.'));
346         a = p - f + 1;
347
348         if (!(r = new(char, a + strlen(e) + 1)))
349                 return NULL;
350
351         strcpy(mempcpy(r, f, a), e);
352         return r;
353
354 }
355
356 char *unit_name_from_path(const char *path, const char *suffix) {
357         char *p, *r;
358
359         assert(path);
360         assert(suffix);
361
362         if (!(p = strdup(path)))
363                 return NULL;
364
365         path_kill_slashes(p);
366
367         path = p[0] == '/' ? p + 1 : p;
368
369         if (path[0] == 0) {
370                 free(p);
371                 return strappend("-", suffix);
372         }
373
374         r = unit_name_build_escape(path, NULL, suffix);
375         free(p);
376
377         return r;
378 }
379
380 char *unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix) {
381         char *p, *r;
382
383         assert(path);
384         assert(suffix);
385
386         if (!(p = strdup(path)))
387                 return NULL;
388
389         path_kill_slashes(p);
390
391         path = p[0] == '/' ? p + 1 : p;
392
393         if (path[0] == 0) {
394                 free(p);
395                 return unit_name_build_escape(prefix, "-", suffix);
396         }
397
398         r = unit_name_build_escape(prefix, path, suffix);
399         free(p);
400
401         return r;
402 }
403
404 char *unit_name_to_path(const char *name) {
405         char *w, *e;
406
407         assert(name);
408
409         if (!(w = unit_name_to_prefix(name)))
410                 return NULL;
411
412         e = unit_name_unescape(w);
413         free(w);
414
415         if (!e)
416                 return NULL;
417
418         if (e[0] != '/') {
419                 w = strappend("/", e);
420                 free(e);
421
422                 if (!w)
423                         return NULL;
424
425                 e = w;
426         }
427
428         return e;
429 }