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