]> git.ipfire.org Git - people/ms/systemd.git/blame - util.c
job: fix pulldown-on-failure logic
[people/ms/systemd.git] / util.c
CommitLineData
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 45usec_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
53usec_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
61struct 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
70usec_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
78struct 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
87bool 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
102bool 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
117bool 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 136int 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
150void 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
158int 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
169int 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
189int 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
209int 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
226int 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
243int 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
260int 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 278char *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. */
295char *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
330char **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
345int 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
391int 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;
407finish:
408 fclose(f);
409 return r;
410}
411
412int read_one_line_file(const char *fn, char **line) {
413 FILE *f;
414 int r;
415 char t[64], *c;
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
436finish:
437 fclose(f);
438 return r;
439}
44d8db9e
LP
440
441char *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
461int 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
491char *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
502bool path_is_absolute(const char *p) {
503 assert(p);
504
505 return p[0] == '/';
506}
507
508bool is_path(const char *p) {
509
510 return !!strchr(p, '/');
511}
512
513char *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
530char *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
550char **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
570int 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
593char *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
614char *file_in_same_dir(const char *path, const char *filename) {
615 char *e, *r;
616 size_t k;
617
618 assert(path);
619 assert(filename);
620
621 /* This removes the last component of path and appends
622 * filename, unless the latter is absolute anyway or the
623 * former isn't */
624
625 if (path_is_absolute(filename))
626 return strdup(filename);
627
628 if (!(e = strrchr(path, '/')))
629 return strdup(filename);
630
631 k = strlen(filename);
632 if (!(r = new(char, e-path+1+k+1)))
633 return NULL;
634
635 memcpy(r, path, e-path+1);
636 memcpy(r+(e-path)+1, filename, k+1);
637
638 return r;
639}
fb624d04 640
a9f5d454
LP
641int mkdir_parents(const char *path, mode_t mode) {
642 const char *p, *e;
643
644 assert(path);
645
646 /* Creates every parent directory in the path except the last
647 * component. */
648
649 p = path + strspn(path, "/");
650 for (;;) {
651 int r;
652 char *t;
653
654 e = p + strcspn(p, "/");
655 p = e + strspn(e, "/");
656
657 /* Is this the last component? If so, then we're
658 * done */
659 if (*p == 0)
660 return 0;
661
662 if (!(t = strndup(path, e - path)))
663 return -ENOMEM;
664
665 r = mkdir(t, mode);
666
667 free(t);
668
669 if (r < 0 && errno != EEXIST)
670 return -errno;
671 }
672}
673
fb624d04
LP
674char hexchar(int x) {
675 static const char table[16] = "0123456789abcdef";
676
677 return table[x & 15];
678}
4fe88d28
LP
679
680int unhexchar(char c) {
681
682 if (c >= '0' && c <= '9')
683 return c - '0';
684
685 if (c >= 'a' && c <= 'f')
ea430986 686 return c - 'a' + 10;
4fe88d28
LP
687
688 if (c >= 'A' && c <= 'F')
ea430986 689 return c - 'A' + 10;
4fe88d28
LP
690
691 return -1;
692}
693
694char octchar(int x) {
695 return '0' + (x & 7);
696}
697
698int unoctchar(char c) {
699
700 if (c >= '0' && c <= '7')
701 return c - '0';
702
703 return -1;
704}
705
5af98f82
LP
706char decchar(int x) {
707 return '0' + (x % 10);
708}
709
710int undecchar(char c) {
711
712 if (c >= '0' && c <= '9')
713 return c - '0';
714
715 return -1;
716}
717
4fe88d28
LP
718char *cescape(const char *s) {
719 char *r, *t;
720 const char *f;
721
722 assert(s);
723
724 /* Does C style string escaping. */
725
726 if (!(r = new(char, strlen(s)*4 + 1)))
727 return NULL;
728
729 for (f = s, t = r; *f; f++)
730
731 switch (*f) {
732
733 case '\a':
734 *(t++) = '\\';
735 *(t++) = 'a';
736 break;
737 case '\b':
738 *(t++) = '\\';
739 *(t++) = 'b';
740 break;
741 case '\f':
742 *(t++) = '\\';
743 *(t++) = 'f';
744 break;
745 case '\n':
746 *(t++) = '\\';
747 *(t++) = 'n';
748 break;
749 case '\r':
750 *(t++) = '\\';
751 *(t++) = 'r';
752 break;
753 case '\t':
754 *(t++) = '\\';
755 *(t++) = 't';
756 break;
757 case '\v':
758 *(t++) = '\\';
759 *(t++) = 'v';
760 break;
761 case '\\':
762 *(t++) = '\\';
763 *(t++) = '\\';
764 break;
765 case '"':
766 *(t++) = '\\';
767 *(t++) = '"';
768 break;
769 case '\'':
770 *(t++) = '\\';
771 *(t++) = '\'';
772 break;
773
774 default:
775 /* For special chars we prefer octal over
776 * hexadecimal encoding, simply because glib's
777 * g_strescape() does the same */
778 if ((*f < ' ') || (*f >= 127)) {
779 *(t++) = '\\';
780 *(t++) = octchar((unsigned char) *f >> 6);
781 *(t++) = octchar((unsigned char) *f >> 3);
782 *(t++) = octchar((unsigned char) *f);
783 } else
784 *(t++) = *f;
785 break;
786 }
787
788 *t = 0;
789
790 return r;
791}
792
793char *cunescape(const char *s) {
794 char *r, *t;
795 const char *f;
796
797 assert(s);
798
799 /* Undoes C style string escaping */
800
801 if (!(r = new(char, strlen(s)+1)))
802 return r;
803
804 for (f = s, t = r; *f; f++) {
805
806 if (*f != '\\') {
807 *(t++) = *f;
808 continue;
809 }
810
811 f++;
812
813 switch (*f) {
814
815 case 'a':
816 *(t++) = '\a';
817 break;
818 case 'b':
819 *(t++) = '\b';
820 break;
821 case 'f':
822 *(t++) = '\f';
823 break;
824 case 'n':
825 *(t++) = '\n';
826 break;
827 case 'r':
828 *(t++) = '\r';
829 break;
830 case 't':
831 *(t++) = '\t';
832 break;
833 case 'v':
834 *(t++) = '\v';
835 break;
836 case '\\':
837 *(t++) = '\\';
838 break;
839 case '"':
840 *(t++) = '"';
841 break;
842 case '\'':
843 *(t++) = '\'';
844 break;
845
846 case 'x': {
847 /* hexadecimal encoding */
848 int a, b;
849
850 if ((a = unhexchar(f[1])) < 0 ||
851 (b = unhexchar(f[2])) < 0) {
852 /* Invalid escape code, let's take it literal then */
853 *(t++) = '\\';
854 *(t++) = 'x';
855 } else {
856 *(t++) = (char) ((a << 4) | b);
857 f += 2;
858 }
859
860 break;
861 }
862
863 case '0':
864 case '1':
865 case '2':
866 case '3':
867 case '4':
868 case '5':
869 case '6':
870 case '7': {
871 /* octal encoding */
872 int a, b, c;
873
874 if ((a = unoctchar(f[0])) < 0 ||
875 (b = unoctchar(f[1])) < 0 ||
876 (c = unoctchar(f[2])) < 0) {
877 /* Invalid escape code, let's take it literal then */
878 *(t++) = '\\';
879 *(t++) = f[0];
880 } else {
881 *(t++) = (char) ((a << 6) | (b << 3) | c);
882 f += 2;
883 }
884
885 break;
886 }
887
888 case 0:
889 /* premature end of string.*/
890 *(t++) = '\\';
891 goto finish;
892
893 default:
894 /* Invalid escape code, let's take it literal then */
895 *(t++) = '\\';
896 *(t++) = 'f';
897 break;
898 }
899 }
900
901finish:
902 *t = 0;
903 return r;
904}
905
906
907char *xescape(const char *s, const char *bad) {
908 char *r, *t;
909 const char *f;
910
911 /* Escapes all chars in bad, in addition to \ and all special
912 * chars, in \xFF style escaping. May be reversed with
913 * cunescape. */
914
915 if (!(r = new(char, strlen(s)*4+1)))
916 return NULL;
917
918 for (f = s, t = r; *f; f++) {
919
b866264a
LP
920 if ((*f < ' ') || (*f >= 127) ||
921 (*f == '\\') || strchr(bad, *f)) {
4fe88d28
LP
922 *(t++) = '\\';
923 *(t++) = 'x';
924 *(t++) = hexchar(*f >> 4);
925 *(t++) = hexchar(*f);
926 } else
927 *(t++) = *f;
928 }
929
930 *t = 0;
931
932 return r;
933}
934
ea430986 935char *bus_path_escape(const char *s) {
ea430986
LP
936 char *r, *t;
937 const char *f;
938
47be870b
LP
939 assert(s);
940
ea430986
LP
941 /* Escapes all chars that D-Bus' object path cannot deal
942 * with. Can be reverse with bus_path_unescape() */
943
944 if (!(r = new(char, strlen(s)*3+1)))
945 return NULL;
946
947 for (f = s, t = r; *f; f++) {
948
949 if (!(*f >= 'A' && *f <= 'Z') &&
950 !(*f >= 'a' && *f <= 'z') &&
951 !(*f >= '0' && *f <= '9')) {
952 *(t++) = '_';
953 *(t++) = hexchar(*f >> 4);
954 *(t++) = hexchar(*f);
955 } else
956 *(t++) = *f;
957 }
958
959 *t = 0;
960
961 return r;
962}
963
964char *bus_path_unescape(const char *s) {
ea430986
LP
965 char *r, *t;
966 const char *f;
967
47be870b
LP
968 assert(s);
969
ea430986
LP
970 if (!(r = new(char, strlen(s)+1)))
971 return NULL;
972
973 for (f = s, t = r; *f; f++) {
974
975 if (*f == '_') {
976 int a, b;
977
978 if ((a = unhexchar(f[1])) < 0 ||
979 (b = unhexchar(f[2])) < 0) {
980 /* Invalid escape code, let's take it literal then */
981 *(t++) = '_';
982 } else {
983 *(t++) = (char) ((a << 4) | b);
984 f += 2;
985 }
986 } else
987 *(t++) = *f;
988 }
989
990 *t = 0;
991
992 return r;
993}
994
4fe88d28
LP
995char *path_kill_slashes(char *path) {
996 char *f, *t;
997 bool slash = false;
998
999 /* Removes redundant inner and trailing slashes. Modifies the
1000 * passed string in-place.
1001 *
1002 * ///foo///bar/ becomes /foo/bar
1003 */
1004
1005 for (f = path, t = path; *f; f++) {
1006
1007 if (*f == '/') {
1008 slash = true;
1009 continue;
1010 }
1011
1012 if (slash) {
1013 slash = false;
1014 *(t++) = '/';
1015 }
1016
1017 *(t++) = *f;
1018 }
1019
1020 /* Special rule, if we are talking of the root directory, a
1021 trailing slash is good */
1022
1023 if (t == path && slash)
1024 *(t++) = '/';
1025
1026 *t = 0;
1027 return path;
1028}
1029
1030bool path_startswith(const char *path, const char *prefix) {
1031 assert(path);
1032 assert(prefix);
1033
1034 if ((path[0] == '/') != (prefix[0] == '/'))
1035 return false;
1036
1037 for (;;) {
1038 size_t a, b;
1039
1040 path += strspn(path, "/");
1041 prefix += strspn(prefix, "/");
1042
1043 if (*prefix == 0)
1044 return true;
1045
1046 if (*path == 0)
1047 return false;
1048
1049 a = strcspn(path, "/");
1050 b = strcspn(prefix, "/");
1051
1052 if (a != b)
1053 return false;
1054
1055 if (memcmp(path, prefix, a) != 0)
1056 return false;
1057
1058 path += a;
1059 prefix += b;
1060 }
1061}
1062
67d51650 1063char *ascii_strlower(char *t) {
4fe88d28
LP
1064 char *p;
1065
67d51650 1066 assert(t);
4fe88d28 1067
67d51650 1068 for (p = t; *p; p++)
4fe88d28
LP
1069 if (*p >= 'A' && *p <= 'Z')
1070 *p = *p - 'A' + 'a';
1071
67d51650 1072 return t;
4fe88d28 1073}
1dccbe19 1074
c85dc17b
LP
1075bool ignore_file(const char *filename) {
1076 assert(filename);
1077
1078 return
1079 filename[0] == '.' ||
1080 endswith(filename, "~") ||
1081 endswith(filename, ".rpmnew") ||
1082 endswith(filename, ".rpmsave") ||
1083 endswith(filename, ".rpmorig") ||
1084 endswith(filename, ".dpkg-old") ||
1085 endswith(filename, ".dpkg-new") ||
1086 endswith(filename, ".swp");
1087}
1088
3a0ecb08
LP
1089int fd_nonblock(int fd, bool nonblock) {
1090 int flags;
1091
1092 assert(fd >= 0);
1093
1094 if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
1095 return -errno;
1096
1097 if (nonblock)
1098 flags |= O_NONBLOCK;
1099 else
1100 flags &= ~O_NONBLOCK;
1101
1102 if (fcntl(fd, F_SETFL, flags) < 0)
1103 return -errno;
1104
1105 return 0;
1106}
1107
1108int fd_cloexec(int fd, bool cloexec) {
1109 int flags;
1110
1111 assert(fd >= 0);
1112
1113 if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
1114 return -errno;
1115
1116 if (cloexec)
1117 flags |= FD_CLOEXEC;
1118 else
1119 flags &= ~FD_CLOEXEC;
1120
1121 if (fcntl(fd, F_SETFD, flags) < 0)
1122 return -errno;
1123
1124 return 0;
1125}
1126
a0d40ac5
LP
1127int close_all_fds(const int except[], unsigned n_except) {
1128 DIR *d;
1129 struct dirent *de;
1130 int r = 0;
1131
1132 if (!(d = opendir("/proc/self/fd")))
1133 return -errno;
1134
1135 while ((de = readdir(d))) {
1136 int fd;
1137
1138 if (de->d_name[0] == '.')
1139 continue;
1140
1141 if ((r = safe_atoi(de->d_name, &fd)) < 0)
1142 goto finish;
1143
1144 if (fd < 3)
1145 continue;
1146
1147 if (fd == dirfd(d))
1148 continue;
1149
1150 if (except) {
1151 bool found;
1152 unsigned i;
1153
1154 found = false;
1155 for (i = 0; i < n_except; i++)
1156 if (except[i] == fd) {
1157 found = true;
1158 break;
1159 }
1160
1161 if (found)
1162 continue;
1163 }
1164
2f357920
LP
1165 if ((r = close_nointr(fd)) < 0) {
1166 /* Valgrind has its own FD and doesn't want to have it closed */
1167 if (errno != EBADF)
1168 goto finish;
1169 }
a0d40ac5
LP
1170 }
1171
2f357920
LP
1172 r = 0;
1173
a0d40ac5
LP
1174finish:
1175 closedir(d);
1176 return r;
1177}
1178
1dccbe19
LP
1179static const char *const ioprio_class_table[] = {
1180 [IOPRIO_CLASS_NONE] = "none",
1181 [IOPRIO_CLASS_RT] = "realtime",
1182 [IOPRIO_CLASS_BE] = "best-effort",
1183 [IOPRIO_CLASS_IDLE] = "idle"
1184};
1185
1186DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
1187
1188static const char *const sigchld_code_table[] = {
1189 [CLD_EXITED] = "exited",
1190 [CLD_KILLED] = "killed",
1191 [CLD_DUMPED] = "dumped",
1192 [CLD_TRAPPED] = "trapped",
1193 [CLD_STOPPED] = "stopped",
1194 [CLD_CONTINUED] = "continued",
1195};
1196
1197DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
1198
1199static const char *const log_facility_table[LOG_NFACILITIES] = {
1200 [LOG_FAC(LOG_KERN)] = "kern",
1201 [LOG_FAC(LOG_USER)] = "user",
1202 [LOG_FAC(LOG_MAIL)] = "mail",
1203 [LOG_FAC(LOG_DAEMON)] = "daemon",
1204 [LOG_FAC(LOG_AUTH)] = "auth",
1205 [LOG_FAC(LOG_SYSLOG)] = "syslog",
1206 [LOG_FAC(LOG_LPR)] = "lpr",
1207 [LOG_FAC(LOG_NEWS)] = "news",
1208 [LOG_FAC(LOG_UUCP)] = "uucp",
1209 [LOG_FAC(LOG_CRON)] = "cron",
1210 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
1211 [LOG_FAC(LOG_FTP)] = "ftp",
1212 [LOG_FAC(LOG_LOCAL0)] = "local0",
1213 [LOG_FAC(LOG_LOCAL1)] = "local1",
1214 [LOG_FAC(LOG_LOCAL2)] = "local2",
1215 [LOG_FAC(LOG_LOCAL3)] = "local3",
1216 [LOG_FAC(LOG_LOCAL4)] = "local4",
1217 [LOG_FAC(LOG_LOCAL5)] = "local5",
1218 [LOG_FAC(LOG_LOCAL6)] = "local6",
1219 [LOG_FAC(LOG_LOCAL7)] = "local7"
1220};
1221
1222DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
1223
1224static const char *const log_level_table[] = {
1225 [LOG_EMERG] = "emerg",
1226 [LOG_ALERT] = "alert",
1227 [LOG_CRIT] = "crit",
1228 [LOG_ERR] = "err",
1229 [LOG_WARNING] = "warning",
1230 [LOG_NOTICE] = "notice",
1231 [LOG_INFO] = "info",
1232 [LOG_DEBUG] = "debug"
1233};
1234
1235DEFINE_STRING_TABLE_LOOKUP(log_level, int);
1236
1237static const char* const sched_policy_table[] = {
1238 [SCHED_OTHER] = "other",
1239 [SCHED_BATCH] = "batch",
1240 [SCHED_IDLE] = "idle",
1241 [SCHED_FIFO] = "fifo",
1242 [SCHED_RR] = "rr"
1243};
1244
1245DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
1246
1247static const char* const rlimit_table[] = {
1248 [RLIMIT_CPU] = "LimitCPU",
1249 [RLIMIT_FSIZE] = "LimitFSIZE",
1250 [RLIMIT_DATA] = "LimitDATA",
1251 [RLIMIT_STACK] = "LimitSTACK",
1252 [RLIMIT_CORE] = "LimitCORE",
1253 [RLIMIT_RSS] = "LimitRSS",
1254 [RLIMIT_NOFILE] = "LimitNOFILE",
1255 [RLIMIT_AS] = "LimitAS",
1256 [RLIMIT_NPROC] = "LimitNPROC",
1257 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
1258 [RLIMIT_LOCKS] = "LimitLOCKS",
1259 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
1260 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
1261 [RLIMIT_NICE] = "LimitNICE",
1262 [RLIMIT_RTPRIO] = "LimitRTPRIO",
1263 [RLIMIT_RTTIME] = "LimitRTTIME"
1264};
1265
1266DEFINE_STRING_TABLE_LOOKUP(rlimit, int);