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