]>
Commit | Line | Data |
---|---|---|
60918275 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
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 | ||
60918275 LP |
22 | #include <assert.h> |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <errno.h> | |
85261803 | 26 | #include <stdlib.h> |
034c6ed7 LP |
27 | #include <signal.h> |
28 | #include <stdio.h> | |
1dccbe19 LP |
29 | #include <syslog.h> |
30 | #include <sched.h> | |
31 | #include <sys/resource.h> | |
ef886c6a | 32 | #include <linux/sched.h> |
60918275 LP |
33 | |
34 | #include "macro.h" | |
35 | #include "util.h" | |
1dccbe19 LP |
36 | #include "ioprio.h" |
37 | #include "missing.h" | |
60918275 | 38 | |
47be870b | 39 | usec_t now(clockid_t clock_id) { |
60918275 LP |
40 | struct timespec ts; |
41 | ||
47be870b | 42 | assert_se(clock_gettime(clock_id, &ts) == 0); |
60918275 LP |
43 | |
44 | return timespec_load(&ts); | |
45 | } | |
46 | ||
47 | usec_t timespec_load(const struct timespec *ts) { | |
48 | assert(ts); | |
49 | ||
50 | return | |
51 | (usec_t) ts->tv_sec * USEC_PER_SEC + | |
52 | (usec_t) ts->tv_nsec / NSEC_PER_USEC; | |
53 | } | |
54 | ||
55 | struct timespec *timespec_store(struct timespec *ts, usec_t u) { | |
56 | assert(ts); | |
57 | ||
58 | ts->tv_sec = (time_t) (u / USEC_PER_SEC); | |
59 | ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC); | |
60 | ||
61 | return ts; | |
62 | } | |
63 | ||
64 | usec_t timeval_load(const struct timeval *tv) { | |
65 | assert(tv); | |
66 | ||
67 | return | |
68 | (usec_t) tv->tv_sec * USEC_PER_SEC + | |
69 | (usec_t) tv->tv_usec; | |
70 | } | |
71 | ||
72 | struct timeval *timeval_store(struct timeval *tv, usec_t u) { | |
73 | assert(tv); | |
74 | ||
75 | tv->tv_sec = (time_t) (u / USEC_PER_SEC); | |
76 | tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC); | |
77 | ||
78 | return tv; | |
79 | } | |
80 | ||
81 | bool endswith(const char *s, const char *postfix) { | |
82 | size_t sl, pl; | |
83 | ||
84 | assert(s); | |
85 | assert(postfix); | |
86 | ||
87 | sl = strlen(s); | |
88 | pl = strlen(postfix); | |
89 | ||
90 | if (sl < pl) | |
91 | return false; | |
92 | ||
93 | return memcmp(s + sl - pl, postfix, pl) == 0; | |
94 | } | |
95 | ||
96 | bool startswith(const char *s, const char *prefix) { | |
97 | size_t sl, pl; | |
98 | ||
99 | assert(s); | |
100 | assert(prefix); | |
101 | ||
102 | sl = strlen(s); | |
103 | pl = strlen(prefix); | |
104 | ||
105 | if (sl < pl) | |
106 | return false; | |
107 | ||
108 | return memcmp(s, prefix, pl) == 0; | |
109 | } | |
110 | ||
79d6d816 LP |
111 | bool first_word(const char *s, const char *word) { |
112 | size_t sl, wl; | |
113 | ||
114 | assert(s); | |
115 | assert(word); | |
116 | ||
117 | sl = strlen(s); | |
118 | wl = strlen(word); | |
119 | ||
120 | if (sl < wl) | |
121 | return false; | |
122 | ||
123 | if (memcmp(s, word, wl) != 0) | |
124 | return false; | |
125 | ||
126 | return (s[wl] == 0 || | |
127 | strchr(WHITESPACE, s[wl])); | |
128 | } | |
129 | ||
42f4e3c4 | 130 | int close_nointr(int fd) { |
60918275 LP |
131 | assert(fd >= 0); |
132 | ||
133 | for (;;) { | |
134 | int r; | |
135 | ||
136 | if ((r = close(fd)) >= 0) | |
137 | return r; | |
138 | ||
139 | if (errno != EINTR) | |
140 | return r; | |
141 | } | |
142 | } | |
85261803 | 143 | |
85f136b5 LP |
144 | void close_nointr_nofail(int fd) { |
145 | ||
146 | /* like close_nointr() but cannot fail, and guarantees errno | |
147 | * is unchanged */ | |
148 | ||
149 | assert_se(close_nointr(fd) == 0); | |
150 | } | |
151 | ||
85261803 LP |
152 | int parse_boolean(const char *v) { |
153 | assert(v); | |
154 | ||
44d8db9e | 155 | if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on")) |
85261803 | 156 | return 1; |
44d8db9e | 157 | else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off")) |
85261803 LP |
158 | return 0; |
159 | ||
160 | return -EINVAL; | |
161 | } | |
162 | ||
163 | int safe_atou(const char *s, unsigned *ret_u) { | |
164 | char *x = NULL; | |
034c6ed7 | 165 | unsigned long l; |
85261803 LP |
166 | |
167 | assert(s); | |
168 | assert(ret_u); | |
169 | ||
170 | errno = 0; | |
171 | l = strtoul(s, &x, 0); | |
172 | ||
173 | if (!x || *x || errno) | |
174 | return errno ? -errno : -EINVAL; | |
175 | ||
034c6ed7 | 176 | if ((unsigned long) (unsigned) l != l) |
85261803 LP |
177 | return -ERANGE; |
178 | ||
179 | *ret_u = (unsigned) l; | |
180 | return 0; | |
181 | } | |
182 | ||
183 | int safe_atoi(const char *s, int *ret_i) { | |
184 | char *x = NULL; | |
034c6ed7 | 185 | long l; |
85261803 LP |
186 | |
187 | assert(s); | |
188 | assert(ret_i); | |
189 | ||
190 | errno = 0; | |
191 | l = strtol(s, &x, 0); | |
192 | ||
193 | if (!x || *x || errno) | |
194 | return errno ? -errno : -EINVAL; | |
195 | ||
034c6ed7 | 196 | if ((long) (int) l != l) |
85261803 LP |
197 | return -ERANGE; |
198 | ||
034c6ed7 LP |
199 | *ret_i = (int) l; |
200 | return 0; | |
201 | } | |
202 | ||
203 | int safe_atolu(const char *s, long unsigned *ret_lu) { | |
204 | char *x = NULL; | |
205 | unsigned long l; | |
206 | ||
207 | assert(s); | |
208 | assert(ret_lu); | |
209 | ||
210 | errno = 0; | |
211 | l = strtoul(s, &x, 0); | |
212 | ||
213 | if (!x || *x || errno) | |
214 | return errno ? -errno : -EINVAL; | |
215 | ||
216 | *ret_lu = l; | |
217 | return 0; | |
218 | } | |
219 | ||
220 | int safe_atoli(const char *s, long int *ret_li) { | |
221 | char *x = NULL; | |
222 | long l; | |
223 | ||
224 | assert(s); | |
225 | assert(ret_li); | |
226 | ||
227 | errno = 0; | |
228 | l = strtol(s, &x, 0); | |
229 | ||
230 | if (!x || *x || errno) | |
231 | return errno ? -errno : -EINVAL; | |
232 | ||
233 | *ret_li = l; | |
234 | return 0; | |
235 | } | |
236 | ||
237 | int safe_atollu(const char *s, long long unsigned *ret_llu) { | |
238 | char *x = NULL; | |
239 | unsigned long long l; | |
240 | ||
241 | assert(s); | |
242 | assert(ret_llu); | |
243 | ||
244 | errno = 0; | |
245 | l = strtoull(s, &x, 0); | |
246 | ||
247 | if (!x || *x || errno) | |
248 | return errno ? -errno : -EINVAL; | |
249 | ||
250 | *ret_llu = l; | |
251 | return 0; | |
252 | } | |
253 | ||
254 | int safe_atolli(const char *s, long long int *ret_lli) { | |
255 | char *x = NULL; | |
256 | long long l; | |
257 | ||
258 | assert(s); | |
259 | assert(ret_lli); | |
260 | ||
261 | errno = 0; | |
262 | l = strtoll(s, &x, 0); | |
263 | ||
264 | if (!x || *x || errno) | |
265 | return errno ? -errno : -EINVAL; | |
266 | ||
267 | *ret_lli = l; | |
85261803 LP |
268 | return 0; |
269 | } | |
a41e8209 | 270 | |
a41e8209 LP |
271 | /* Split a string into words. */ |
272 | char *split_spaces(const char *c, size_t *l, char **state) { | |
273 | char *current; | |
274 | ||
275 | current = *state ? *state : (char*) c; | |
276 | ||
277 | if (!*current || *c == 0) | |
278 | return NULL; | |
279 | ||
280 | current += strspn(current, WHITESPACE); | |
281 | *l = strcspn(current, WHITESPACE); | |
282 | *state = current+*l; | |
283 | ||
284 | return (char*) current; | |
285 | } | |
034c6ed7 | 286 | |
82919e3d LP |
287 | /* Split a path into filenames. */ |
288 | char *split_slash(const char *c, size_t *l, char **state) { | |
289 | char *current; | |
290 | ||
291 | current = *state ? *state : (char*) c; | |
292 | ||
293 | if (!*current || *c == 0) | |
294 | return NULL; | |
295 | ||
296 | current += strspn(current, "/"); | |
297 | *l = strcspn(current, "/"); | |
298 | *state = current+*l; | |
299 | ||
300 | return (char*) current; | |
301 | } | |
302 | ||
034c6ed7 LP |
303 | /* Split a string into words, but consider strings enclosed in '' and |
304 | * "" as words even if they include spaces. */ | |
305 | char *split_quoted(const char *c, size_t *l, char **state) { | |
306 | char *current; | |
307 | ||
308 | current = *state ? *state : (char*) c; | |
309 | ||
310 | if (!*current || *c == 0) | |
311 | return NULL; | |
312 | ||
313 | current += strspn(current, WHITESPACE); | |
314 | ||
315 | if (*current == '\'') { | |
316 | current ++; | |
317 | *l = strcspn(current, "'"); | |
318 | *state = current+*l; | |
319 | ||
320 | if (**state == '\'') | |
321 | (*state)++; | |
322 | } else if (*current == '\"') { | |
323 | current ++; | |
b858b600 | 324 | *l = strcspn(current, "\""); |
034c6ed7 LP |
325 | *state = current+*l; |
326 | ||
327 | if (**state == '\"') | |
328 | (*state)++; | |
329 | } else { | |
330 | *l = strcspn(current, WHITESPACE); | |
331 | *state = current+*l; | |
332 | } | |
333 | ||
44d8db9e LP |
334 | /* FIXME: Cannot deal with strings that have spaces AND ticks |
335 | * in them */ | |
336 | ||
034c6ed7 LP |
337 | return (char*) current; |
338 | } | |
339 | ||
034c6ed7 LP |
340 | int get_parent_of_pid(pid_t pid, pid_t *_ppid) { |
341 | int r; | |
342 | FILE *f; | |
343 | char fn[132], line[256], *p; | |
344 | long long unsigned ppid; | |
345 | ||
346 | assert(pid >= 0); | |
347 | assert(_ppid); | |
348 | ||
349 | assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%llu/stat", (unsigned long long) pid) < (int) (sizeof(fn)-1)); | |
350 | fn[sizeof(fn)-1] = 0; | |
351 | ||
352 | if (!(f = fopen(fn, "r"))) | |
353 | return -errno; | |
354 | ||
355 | if (!(fgets(line, sizeof(line), f))) { | |
356 | r = -errno; | |
357 | fclose(f); | |
358 | return r; | |
359 | } | |
360 | ||
361 | fclose(f); | |
362 | ||
363 | /* Let's skip the pid and comm fields. The latter is enclosed | |
364 | * in () but does not escape any () in its value, so let's | |
365 | * skip over it manually */ | |
366 | ||
367 | if (!(p = strrchr(line, ')'))) | |
368 | return -EIO; | |
369 | ||
370 | p++; | |
371 | ||
372 | if (sscanf(p, " " | |
373 | "%*c " /* state */ | |
374 | "%llu ", /* ppid */ | |
375 | &ppid) != 1) | |
376 | return -EIO; | |
377 | ||
378 | if ((long long unsigned) (pid_t) ppid != ppid) | |
379 | return -ERANGE; | |
380 | ||
381 | *_ppid = (pid_t) ppid; | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
386 | int write_one_line_file(const char *fn, const char *line) { | |
387 | FILE *f; | |
388 | int r; | |
389 | ||
390 | assert(fn); | |
391 | assert(line); | |
392 | ||
393 | if (!(f = fopen(fn, "we"))) | |
394 | return -errno; | |
395 | ||
396 | if (fputs(line, f) < 0) { | |
397 | r = -errno; | |
398 | goto finish; | |
399 | } | |
400 | ||
401 | r = 0; | |
402 | finish: | |
403 | fclose(f); | |
404 | return r; | |
405 | } | |
406 | ||
407 | int read_one_line_file(const char *fn, char **line) { | |
408 | FILE *f; | |
409 | int r; | |
410 | char t[64], *c; | |
411 | ||
412 | assert(fn); | |
413 | assert(line); | |
414 | ||
415 | if (!(f = fopen(fn, "re"))) | |
416 | return -errno; | |
417 | ||
418 | if (!(fgets(t, sizeof(t), f))) { | |
419 | r = -errno; | |
420 | goto finish; | |
421 | } | |
422 | ||
423 | if (!(c = strdup(t))) { | |
424 | r = -ENOMEM; | |
425 | goto finish; | |
426 | } | |
427 | ||
428 | *line = c; | |
429 | r = 0; | |
430 | ||
431 | finish: | |
432 | fclose(f); | |
433 | return r; | |
434 | } | |
44d8db9e LP |
435 | |
436 | char *strappend(const char *s, const char *suffix) { | |
437 | size_t a, b; | |
438 | char *r; | |
439 | ||
440 | assert(s); | |
441 | assert(suffix); | |
442 | ||
443 | a = strlen(s); | |
444 | b = strlen(suffix); | |
445 | ||
446 | if (!(r = new(char, a+b+1))) | |
447 | return NULL; | |
448 | ||
449 | memcpy(r, s, a); | |
450 | memcpy(r+a, suffix, b); | |
451 | r[a+b] = 0; | |
452 | ||
453 | return r; | |
454 | } | |
87f0e418 LP |
455 | |
456 | int readlink_malloc(const char *p, char **r) { | |
457 | size_t l = 100; | |
458 | ||
459 | assert(p); | |
460 | assert(r); | |
461 | ||
462 | for (;;) { | |
463 | char *c; | |
464 | ssize_t n; | |
465 | ||
466 | if (!(c = new(char, l))) | |
467 | return -ENOMEM; | |
468 | ||
469 | if ((n = readlink(p, c, l-1)) < 0) { | |
470 | int ret = -errno; | |
471 | free(c); | |
472 | return ret; | |
473 | } | |
474 | ||
475 | if ((size_t) n < l-1) { | |
476 | c[n] = 0; | |
477 | *r = c; | |
478 | return 0; | |
479 | } | |
480 | ||
481 | free(c); | |
482 | l *= 2; | |
483 | } | |
484 | } | |
485 | ||
486 | char *file_name_from_path(const char *p) { | |
487 | char *r; | |
488 | ||
489 | assert(p); | |
490 | ||
491 | if ((r = strrchr(p, '/'))) | |
492 | return r + 1; | |
493 | ||
494 | return (char*) p; | |
495 | } | |
0301abf4 LP |
496 | |
497 | bool path_is_absolute(const char *p) { | |
498 | assert(p); | |
499 | ||
500 | return p[0] == '/'; | |
501 | } | |
502 | ||
503 | bool is_path(const char *p) { | |
504 | ||
505 | return !!strchr(p, '/'); | |
506 | } | |
507 | ||
508 | char *path_make_absolute(const char *p, const char *prefix) { | |
509 | char *r; | |
510 | ||
511 | assert(p); | |
512 | ||
513 | if (path_is_absolute(p) || !prefix) | |
514 | return strdup(p); | |
515 | ||
516 | if (asprintf(&r, "%s/%s", prefix, p) < 0) | |
517 | return NULL; | |
518 | ||
519 | return r; | |
520 | } | |
2a987ee8 LP |
521 | |
522 | int reset_all_signal_handlers(void) { | |
523 | int sig; | |
524 | ||
525 | for (sig = 1; sig < _NSIG; sig++) { | |
526 | struct sigaction sa; | |
527 | ||
528 | if (sig == SIGKILL || sig == SIGSTOP) | |
529 | continue; | |
530 | ||
531 | zero(sa); | |
532 | sa.sa_handler = SIG_DFL; | |
431c32bf | 533 | sa.sa_flags = SA_RESTART; |
2a987ee8 LP |
534 | |
535 | /* On Linux the first two RT signals are reserved by | |
536 | * glibc, and sigaction() will return EINVAL for them. */ | |
537 | if ((sigaction(sig, &sa, NULL) < 0)) | |
538 | if (errno != EINVAL) | |
539 | return -errno; | |
540 | } | |
541 | ||
542 | return 0; | |
543 | } | |
4a72ff34 LP |
544 | |
545 | char *strstrip(char *s) { | |
546 | char *e, *l = NULL; | |
547 | ||
548 | /* Drops trailing whitespace. Modifies the string in | |
549 | * place. Returns pointer to first non-space character */ | |
550 | ||
551 | s += strspn(s, WHITESPACE); | |
552 | ||
553 | for (e = s; *e; e++) | |
554 | if (!strchr(WHITESPACE, *e)) | |
555 | l = e; | |
556 | ||
557 | if (l) | |
558 | *(l+1) = 0; | |
559 | else | |
560 | *s = 0; | |
561 | ||
562 | return s; | |
563 | ||
564 | } | |
565 | ||
566 | char *file_in_same_dir(const char *path, const char *filename) { | |
567 | char *e, *r; | |
568 | size_t k; | |
569 | ||
570 | assert(path); | |
571 | assert(filename); | |
572 | ||
573 | /* This removes the last component of path and appends | |
574 | * filename, unless the latter is absolute anyway or the | |
575 | * former isn't */ | |
576 | ||
577 | if (path_is_absolute(filename)) | |
578 | return strdup(filename); | |
579 | ||
580 | if (!(e = strrchr(path, '/'))) | |
581 | return strdup(filename); | |
582 | ||
583 | k = strlen(filename); | |
584 | if (!(r = new(char, e-path+1+k+1))) | |
585 | return NULL; | |
586 | ||
587 | memcpy(r, path, e-path+1); | |
588 | memcpy(r+(e-path)+1, filename, k+1); | |
589 | ||
590 | return r; | |
591 | } | |
fb624d04 LP |
592 | |
593 | char hexchar(int x) { | |
594 | static const char table[16] = "0123456789abcdef"; | |
595 | ||
596 | return table[x & 15]; | |
597 | } | |
4fe88d28 LP |
598 | |
599 | int unhexchar(char c) { | |
600 | ||
601 | if (c >= '0' && c <= '9') | |
602 | return c - '0'; | |
603 | ||
604 | if (c >= 'a' && c <= 'f') | |
ea430986 | 605 | return c - 'a' + 10; |
4fe88d28 LP |
606 | |
607 | if (c >= 'A' && c <= 'F') | |
ea430986 | 608 | return c - 'A' + 10; |
4fe88d28 LP |
609 | |
610 | return -1; | |
611 | } | |
612 | ||
613 | char octchar(int x) { | |
614 | return '0' + (x & 7); | |
615 | } | |
616 | ||
617 | int unoctchar(char c) { | |
618 | ||
619 | if (c >= '0' && c <= '7') | |
620 | return c - '0'; | |
621 | ||
622 | return -1; | |
623 | } | |
624 | ||
625 | char *cescape(const char *s) { | |
626 | char *r, *t; | |
627 | const char *f; | |
628 | ||
629 | assert(s); | |
630 | ||
631 | /* Does C style string escaping. */ | |
632 | ||
633 | if (!(r = new(char, strlen(s)*4 + 1))) | |
634 | return NULL; | |
635 | ||
636 | for (f = s, t = r; *f; f++) | |
637 | ||
638 | switch (*f) { | |
639 | ||
640 | case '\a': | |
641 | *(t++) = '\\'; | |
642 | *(t++) = 'a'; | |
643 | break; | |
644 | case '\b': | |
645 | *(t++) = '\\'; | |
646 | *(t++) = 'b'; | |
647 | break; | |
648 | case '\f': | |
649 | *(t++) = '\\'; | |
650 | *(t++) = 'f'; | |
651 | break; | |
652 | case '\n': | |
653 | *(t++) = '\\'; | |
654 | *(t++) = 'n'; | |
655 | break; | |
656 | case '\r': | |
657 | *(t++) = '\\'; | |
658 | *(t++) = 'r'; | |
659 | break; | |
660 | case '\t': | |
661 | *(t++) = '\\'; | |
662 | *(t++) = 't'; | |
663 | break; | |
664 | case '\v': | |
665 | *(t++) = '\\'; | |
666 | *(t++) = 'v'; | |
667 | break; | |
668 | case '\\': | |
669 | *(t++) = '\\'; | |
670 | *(t++) = '\\'; | |
671 | break; | |
672 | case '"': | |
673 | *(t++) = '\\'; | |
674 | *(t++) = '"'; | |
675 | break; | |
676 | case '\'': | |
677 | *(t++) = '\\'; | |
678 | *(t++) = '\''; | |
679 | break; | |
680 | ||
681 | default: | |
682 | /* For special chars we prefer octal over | |
683 | * hexadecimal encoding, simply because glib's | |
684 | * g_strescape() does the same */ | |
685 | if ((*f < ' ') || (*f >= 127)) { | |
686 | *(t++) = '\\'; | |
687 | *(t++) = octchar((unsigned char) *f >> 6); | |
688 | *(t++) = octchar((unsigned char) *f >> 3); | |
689 | *(t++) = octchar((unsigned char) *f); | |
690 | } else | |
691 | *(t++) = *f; | |
692 | break; | |
693 | } | |
694 | ||
695 | *t = 0; | |
696 | ||
697 | return r; | |
698 | } | |
699 | ||
700 | char *cunescape(const char *s) { | |
701 | char *r, *t; | |
702 | const char *f; | |
703 | ||
704 | assert(s); | |
705 | ||
706 | /* Undoes C style string escaping */ | |
707 | ||
708 | if (!(r = new(char, strlen(s)+1))) | |
709 | return r; | |
710 | ||
711 | for (f = s, t = r; *f; f++) { | |
712 | ||
713 | if (*f != '\\') { | |
714 | *(t++) = *f; | |
715 | continue; | |
716 | } | |
717 | ||
718 | f++; | |
719 | ||
720 | switch (*f) { | |
721 | ||
722 | case 'a': | |
723 | *(t++) = '\a'; | |
724 | break; | |
725 | case 'b': | |
726 | *(t++) = '\b'; | |
727 | break; | |
728 | case 'f': | |
729 | *(t++) = '\f'; | |
730 | break; | |
731 | case 'n': | |
732 | *(t++) = '\n'; | |
733 | break; | |
734 | case 'r': | |
735 | *(t++) = '\r'; | |
736 | break; | |
737 | case 't': | |
738 | *(t++) = '\t'; | |
739 | break; | |
740 | case 'v': | |
741 | *(t++) = '\v'; | |
742 | break; | |
743 | case '\\': | |
744 | *(t++) = '\\'; | |
745 | break; | |
746 | case '"': | |
747 | *(t++) = '"'; | |
748 | break; | |
749 | case '\'': | |
750 | *(t++) = '\''; | |
751 | break; | |
752 | ||
753 | case 'x': { | |
754 | /* hexadecimal encoding */ | |
755 | int a, b; | |
756 | ||
757 | if ((a = unhexchar(f[1])) < 0 || | |
758 | (b = unhexchar(f[2])) < 0) { | |
759 | /* Invalid escape code, let's take it literal then */ | |
760 | *(t++) = '\\'; | |
761 | *(t++) = 'x'; | |
762 | } else { | |
763 | *(t++) = (char) ((a << 4) | b); | |
764 | f += 2; | |
765 | } | |
766 | ||
767 | break; | |
768 | } | |
769 | ||
770 | case '0': | |
771 | case '1': | |
772 | case '2': | |
773 | case '3': | |
774 | case '4': | |
775 | case '5': | |
776 | case '6': | |
777 | case '7': { | |
778 | /* octal encoding */ | |
779 | int a, b, c; | |
780 | ||
781 | if ((a = unoctchar(f[0])) < 0 || | |
782 | (b = unoctchar(f[1])) < 0 || | |
783 | (c = unoctchar(f[2])) < 0) { | |
784 | /* Invalid escape code, let's take it literal then */ | |
785 | *(t++) = '\\'; | |
786 | *(t++) = f[0]; | |
787 | } else { | |
788 | *(t++) = (char) ((a << 6) | (b << 3) | c); | |
789 | f += 2; | |
790 | } | |
791 | ||
792 | break; | |
793 | } | |
794 | ||
795 | case 0: | |
796 | /* premature end of string.*/ | |
797 | *(t++) = '\\'; | |
798 | goto finish; | |
799 | ||
800 | default: | |
801 | /* Invalid escape code, let's take it literal then */ | |
802 | *(t++) = '\\'; | |
803 | *(t++) = 'f'; | |
804 | break; | |
805 | } | |
806 | } | |
807 | ||
808 | finish: | |
809 | *t = 0; | |
810 | return r; | |
811 | } | |
812 | ||
813 | ||
814 | char *xescape(const char *s, const char *bad) { | |
815 | char *r, *t; | |
816 | const char *f; | |
817 | ||
818 | /* Escapes all chars in bad, in addition to \ and all special | |
819 | * chars, in \xFF style escaping. May be reversed with | |
820 | * cunescape. */ | |
821 | ||
822 | if (!(r = new(char, strlen(s)*4+1))) | |
823 | return NULL; | |
824 | ||
825 | for (f = s, t = r; *f; f++) { | |
826 | ||
827 | if (*f < ' ' || *f >= 127 || | |
828 | *f == '\\' || strchr(bad, *f)) { | |
829 | *(t++) = '\\'; | |
830 | *(t++) = 'x'; | |
831 | *(t++) = hexchar(*f >> 4); | |
832 | *(t++) = hexchar(*f); | |
833 | } else | |
834 | *(t++) = *f; | |
835 | } | |
836 | ||
837 | *t = 0; | |
838 | ||
839 | return r; | |
840 | } | |
841 | ||
ea430986 | 842 | char *bus_path_escape(const char *s) { |
ea430986 LP |
843 | char *r, *t; |
844 | const char *f; | |
845 | ||
47be870b LP |
846 | assert(s); |
847 | ||
ea430986 LP |
848 | /* Escapes all chars that D-Bus' object path cannot deal |
849 | * with. Can be reverse with bus_path_unescape() */ | |
850 | ||
851 | if (!(r = new(char, strlen(s)*3+1))) | |
852 | return NULL; | |
853 | ||
854 | for (f = s, t = r; *f; f++) { | |
855 | ||
856 | if (!(*f >= 'A' && *f <= 'Z') && | |
857 | !(*f >= 'a' && *f <= 'z') && | |
858 | !(*f >= '0' && *f <= '9')) { | |
859 | *(t++) = '_'; | |
860 | *(t++) = hexchar(*f >> 4); | |
861 | *(t++) = hexchar(*f); | |
862 | } else | |
863 | *(t++) = *f; | |
864 | } | |
865 | ||
866 | *t = 0; | |
867 | ||
868 | return r; | |
869 | } | |
870 | ||
871 | char *bus_path_unescape(const char *s) { | |
ea430986 LP |
872 | char *r, *t; |
873 | const char *f; | |
874 | ||
47be870b LP |
875 | assert(s); |
876 | ||
ea430986 LP |
877 | if (!(r = new(char, strlen(s)+1))) |
878 | return NULL; | |
879 | ||
880 | for (f = s, t = r; *f; f++) { | |
881 | ||
882 | if (*f == '_') { | |
883 | int a, b; | |
884 | ||
885 | if ((a = unhexchar(f[1])) < 0 || | |
886 | (b = unhexchar(f[2])) < 0) { | |
887 | /* Invalid escape code, let's take it literal then */ | |
888 | *(t++) = '_'; | |
889 | } else { | |
890 | *(t++) = (char) ((a << 4) | b); | |
891 | f += 2; | |
892 | } | |
893 | } else | |
894 | *(t++) = *f; | |
895 | } | |
896 | ||
897 | *t = 0; | |
898 | ||
899 | return r; | |
900 | } | |
901 | ||
4fe88d28 LP |
902 | char *path_kill_slashes(char *path) { |
903 | char *f, *t; | |
904 | bool slash = false; | |
905 | ||
906 | /* Removes redundant inner and trailing slashes. Modifies the | |
907 | * passed string in-place. | |
908 | * | |
909 | * ///foo///bar/ becomes /foo/bar | |
910 | */ | |
911 | ||
912 | for (f = path, t = path; *f; f++) { | |
913 | ||
914 | if (*f == '/') { | |
915 | slash = true; | |
916 | continue; | |
917 | } | |
918 | ||
919 | if (slash) { | |
920 | slash = false; | |
921 | *(t++) = '/'; | |
922 | } | |
923 | ||
924 | *(t++) = *f; | |
925 | } | |
926 | ||
927 | /* Special rule, if we are talking of the root directory, a | |
928 | trailing slash is good */ | |
929 | ||
930 | if (t == path && slash) | |
931 | *(t++) = '/'; | |
932 | ||
933 | *t = 0; | |
934 | return path; | |
935 | } | |
936 | ||
937 | bool path_startswith(const char *path, const char *prefix) { | |
938 | assert(path); | |
939 | assert(prefix); | |
940 | ||
941 | if ((path[0] == '/') != (prefix[0] == '/')) | |
942 | return false; | |
943 | ||
944 | for (;;) { | |
945 | size_t a, b; | |
946 | ||
947 | path += strspn(path, "/"); | |
948 | prefix += strspn(prefix, "/"); | |
949 | ||
950 | if (*prefix == 0) | |
951 | return true; | |
952 | ||
953 | if (*path == 0) | |
954 | return false; | |
955 | ||
956 | a = strcspn(path, "/"); | |
957 | b = strcspn(prefix, "/"); | |
958 | ||
959 | if (a != b) | |
960 | return false; | |
961 | ||
962 | if (memcmp(path, prefix, a) != 0) | |
963 | return false; | |
964 | ||
965 | path += a; | |
966 | prefix += b; | |
967 | } | |
968 | } | |
969 | ||
970 | char *ascii_strlower(char *path) { | |
971 | char *p; | |
972 | ||
973 | assert(path); | |
974 | ||
975 | for (p = path; *p; p++) | |
976 | if (*p >= 'A' && *p <= 'Z') | |
977 | *p = *p - 'A' + 'a'; | |
978 | ||
979 | return p; | |
980 | } | |
1dccbe19 LP |
981 | |
982 | static const char *const ioprio_class_table[] = { | |
983 | [IOPRIO_CLASS_NONE] = "none", | |
984 | [IOPRIO_CLASS_RT] = "realtime", | |
985 | [IOPRIO_CLASS_BE] = "best-effort", | |
986 | [IOPRIO_CLASS_IDLE] = "idle" | |
987 | }; | |
988 | ||
989 | DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int); | |
990 | ||
991 | static const char *const sigchld_code_table[] = { | |
992 | [CLD_EXITED] = "exited", | |
993 | [CLD_KILLED] = "killed", | |
994 | [CLD_DUMPED] = "dumped", | |
995 | [CLD_TRAPPED] = "trapped", | |
996 | [CLD_STOPPED] = "stopped", | |
997 | [CLD_CONTINUED] = "continued", | |
998 | }; | |
999 | ||
1000 | DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int); | |
1001 | ||
1002 | static const char *const log_facility_table[LOG_NFACILITIES] = { | |
1003 | [LOG_FAC(LOG_KERN)] = "kern", | |
1004 | [LOG_FAC(LOG_USER)] = "user", | |
1005 | [LOG_FAC(LOG_MAIL)] = "mail", | |
1006 | [LOG_FAC(LOG_DAEMON)] = "daemon", | |
1007 | [LOG_FAC(LOG_AUTH)] = "auth", | |
1008 | [LOG_FAC(LOG_SYSLOG)] = "syslog", | |
1009 | [LOG_FAC(LOG_LPR)] = "lpr", | |
1010 | [LOG_FAC(LOG_NEWS)] = "news", | |
1011 | [LOG_FAC(LOG_UUCP)] = "uucp", | |
1012 | [LOG_FAC(LOG_CRON)] = "cron", | |
1013 | [LOG_FAC(LOG_AUTHPRIV)] = "authpriv", | |
1014 | [LOG_FAC(LOG_FTP)] = "ftp", | |
1015 | [LOG_FAC(LOG_LOCAL0)] = "local0", | |
1016 | [LOG_FAC(LOG_LOCAL1)] = "local1", | |
1017 | [LOG_FAC(LOG_LOCAL2)] = "local2", | |
1018 | [LOG_FAC(LOG_LOCAL3)] = "local3", | |
1019 | [LOG_FAC(LOG_LOCAL4)] = "local4", | |
1020 | [LOG_FAC(LOG_LOCAL5)] = "local5", | |
1021 | [LOG_FAC(LOG_LOCAL6)] = "local6", | |
1022 | [LOG_FAC(LOG_LOCAL7)] = "local7" | |
1023 | }; | |
1024 | ||
1025 | DEFINE_STRING_TABLE_LOOKUP(log_facility, int); | |
1026 | ||
1027 | static const char *const log_level_table[] = { | |
1028 | [LOG_EMERG] = "emerg", | |
1029 | [LOG_ALERT] = "alert", | |
1030 | [LOG_CRIT] = "crit", | |
1031 | [LOG_ERR] = "err", | |
1032 | [LOG_WARNING] = "warning", | |
1033 | [LOG_NOTICE] = "notice", | |
1034 | [LOG_INFO] = "info", | |
1035 | [LOG_DEBUG] = "debug" | |
1036 | }; | |
1037 | ||
1038 | DEFINE_STRING_TABLE_LOOKUP(log_level, int); | |
1039 | ||
1040 | static const char* const sched_policy_table[] = { | |
1041 | [SCHED_OTHER] = "other", | |
1042 | [SCHED_BATCH] = "batch", | |
1043 | [SCHED_IDLE] = "idle", | |
1044 | [SCHED_FIFO] = "fifo", | |
1045 | [SCHED_RR] = "rr" | |
1046 | }; | |
1047 | ||
1048 | DEFINE_STRING_TABLE_LOOKUP(sched_policy, int); | |
1049 | ||
1050 | static const char* const rlimit_table[] = { | |
1051 | [RLIMIT_CPU] = "LimitCPU", | |
1052 | [RLIMIT_FSIZE] = "LimitFSIZE", | |
1053 | [RLIMIT_DATA] = "LimitDATA", | |
1054 | [RLIMIT_STACK] = "LimitSTACK", | |
1055 | [RLIMIT_CORE] = "LimitCORE", | |
1056 | [RLIMIT_RSS] = "LimitRSS", | |
1057 | [RLIMIT_NOFILE] = "LimitNOFILE", | |
1058 | [RLIMIT_AS] = "LimitAS", | |
1059 | [RLIMIT_NPROC] = "LimitNPROC", | |
1060 | [RLIMIT_MEMLOCK] = "LimitMEMLOCK", | |
1061 | [RLIMIT_LOCKS] = "LimitLOCKS", | |
1062 | [RLIMIT_SIGPENDING] = "LimitSIGPENDING", | |
1063 | [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE", | |
1064 | [RLIMIT_NICE] = "LimitNICE", | |
1065 | [RLIMIT_RTPRIO] = "LimitRTPRIO", | |
1066 | [RLIMIT_RTTIME] = "LimitRTTIME" | |
1067 | }; | |
1068 | ||
1069 | DEFINE_STRING_TABLE_LOOKUP(rlimit, int); |