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