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