chiark / gitweb /
udev: expose network interfaces by default
[elogind.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 #include <fcntl.h>
36 #include <dirent.h>
37 #include <sys/ioctl.h>
38 #include <linux/vt.h>
39 #include <linux/tiocl.h>
40 #include <termios.h>
41 #include <stdarg.h>
42 #include <sys/inotify.h>
43 #include <sys/poll.h>
44 #include <libgen.h>
45
46 #include "macro.h"
47 #include "util.h"
48 #include "ioprio.h"
49 #include "missing.h"
50 #include "log.h"
51 #include "strv.h"
52
53 bool streq_ptr(const char *a, const char *b) {
54
55         /* Like streq(), but tries to make sense of NULL pointers */
56
57         if (a && b)
58                 return streq(a, b);
59
60         if (!a && !b)
61                 return true;
62
63         return false;
64 }
65
66 usec_t now(clockid_t clock_id) {
67         struct timespec ts;
68
69         assert_se(clock_gettime(clock_id, &ts) == 0);
70
71         return timespec_load(&ts);
72 }
73
74 usec_t timespec_load(const struct timespec *ts) {
75         assert(ts);
76
77         return
78                 (usec_t) ts->tv_sec * USEC_PER_SEC +
79                 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
80 }
81
82 struct timespec *timespec_store(struct timespec *ts, usec_t u)  {
83         assert(ts);
84
85         ts->tv_sec = (time_t) (u / USEC_PER_SEC);
86         ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
87
88         return ts;
89 }
90
91 usec_t timeval_load(const struct timeval *tv) {
92         assert(tv);
93
94         return
95                 (usec_t) tv->tv_sec * USEC_PER_SEC +
96                 (usec_t) tv->tv_usec;
97 }
98
99 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
100         assert(tv);
101
102         tv->tv_sec = (time_t) (u / USEC_PER_SEC);
103         tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
104
105         return tv;
106 }
107
108 bool endswith(const char *s, const char *postfix) {
109         size_t sl, pl;
110
111         assert(s);
112         assert(postfix);
113
114         sl = strlen(s);
115         pl = strlen(postfix);
116
117         if (sl < pl)
118                 return false;
119
120         return memcmp(s + sl - pl, postfix, pl) == 0;
121 }
122
123 bool startswith(const char *s, const char *prefix) {
124         size_t sl, pl;
125
126         assert(s);
127         assert(prefix);
128
129         sl = strlen(s);
130         pl = strlen(prefix);
131
132         if (sl < pl)
133                 return false;
134
135         return memcmp(s, prefix, pl) == 0;
136 }
137
138 bool first_word(const char *s, const char *word) {
139         size_t sl, wl;
140
141         assert(s);
142         assert(word);
143
144         sl = strlen(s);
145         wl = strlen(word);
146
147         if (sl < wl)
148                 return false;
149
150         if (memcmp(s, word, wl) != 0)
151                 return false;
152
153         return (s[wl] == 0 ||
154                 strchr(WHITESPACE, s[wl]));
155 }
156
157 int close_nointr(int fd) {
158         assert(fd >= 0);
159
160         for (;;) {
161                 int r;
162
163                 if ((r = close(fd)) >= 0)
164                         return r;
165
166                 if (errno != EINTR)
167                         return r;
168         }
169 }
170
171 void close_nointr_nofail(int fd) {
172         int saved_errno = errno;
173
174         /* like close_nointr() but cannot fail, and guarantees errno
175          * is unchanged */
176
177         assert_se(close_nointr(fd) == 0);
178
179         errno = saved_errno;
180 }
181
182 int parse_boolean(const char *v) {
183         assert(v);
184
185         if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
186                 return 1;
187         else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
188                 return 0;
189
190         return -EINVAL;
191 }
192
193 int safe_atou(const char *s, unsigned *ret_u) {
194         char *x = NULL;
195         unsigned long l;
196
197         assert(s);
198         assert(ret_u);
199
200         errno = 0;
201         l = strtoul(s, &x, 0);
202
203         if (!x || *x || errno)
204                 return errno ? -errno : -EINVAL;
205
206         if ((unsigned long) (unsigned) l != l)
207                 return -ERANGE;
208
209         *ret_u = (unsigned) l;
210         return 0;
211 }
212
213 int safe_atoi(const char *s, int *ret_i) {
214         char *x = NULL;
215         long l;
216
217         assert(s);
218         assert(ret_i);
219
220         errno = 0;
221         l = strtol(s, &x, 0);
222
223         if (!x || *x || errno)
224                 return errno ? -errno : -EINVAL;
225
226         if ((long) (int) l != l)
227                 return -ERANGE;
228
229         *ret_i = (int) l;
230         return 0;
231 }
232
233 int safe_atolu(const char *s, long unsigned *ret_lu) {
234         char *x = NULL;
235         unsigned long l;
236
237         assert(s);
238         assert(ret_lu);
239
240         errno = 0;
241         l = strtoul(s, &x, 0);
242
243         if (!x || *x || errno)
244                 return errno ? -errno : -EINVAL;
245
246         *ret_lu = l;
247         return 0;
248 }
249
250 int safe_atoli(const char *s, long int *ret_li) {
251         char *x = NULL;
252         long l;
253
254         assert(s);
255         assert(ret_li);
256
257         errno = 0;
258         l = strtol(s, &x, 0);
259
260         if (!x || *x || errno)
261                 return errno ? -errno : -EINVAL;
262
263         *ret_li = l;
264         return 0;
265 }
266
267 int safe_atollu(const char *s, long long unsigned *ret_llu) {
268         char *x = NULL;
269         unsigned long long l;
270
271         assert(s);
272         assert(ret_llu);
273
274         errno = 0;
275         l = strtoull(s, &x, 0);
276
277         if (!x || *x || errno)
278                 return errno ? -errno : -EINVAL;
279
280         *ret_llu = l;
281         return 0;
282 }
283
284 int safe_atolli(const char *s, long long int *ret_lli) {
285         char *x = NULL;
286         long long l;
287
288         assert(s);
289         assert(ret_lli);
290
291         errno = 0;
292         l = strtoll(s, &x, 0);
293
294         if (!x || *x || errno)
295                 return errno ? -errno : -EINVAL;
296
297         *ret_lli = l;
298         return 0;
299 }
300
301 /* Split a string into words. */
302 char *split(const char *c, size_t *l, const char *separator, char **state) {
303         char *current;
304
305         current = *state ? *state : (char*) c;
306
307         if (!*current || *c == 0)
308                 return NULL;
309
310         current += strspn(current, separator);
311         *l = strcspn(current, separator);
312         *state = current+*l;
313
314         return (char*) current;
315 }
316
317 /* Split a string into words, but consider strings enclosed in '' and
318  * "" as words even if they include spaces. */
319 char *split_quoted(const char *c, size_t *l, char **state) {
320         char *current;
321
322         current = *state ? *state : (char*) c;
323
324         if (!*current || *c == 0)
325                 return NULL;
326
327         current += strspn(current, WHITESPACE);
328
329         if (*current == '\'') {
330                 current ++;
331                 *l = strcspn(current, "'");
332                 *state = current+*l;
333
334                 if (**state == '\'')
335                         (*state)++;
336         } else if (*current == '\"') {
337                 current ++;
338                 *l = strcspn(current, "\"");
339                 *state = current+*l;
340
341                 if (**state == '\"')
342                         (*state)++;
343         } else {
344                 *l = strcspn(current, WHITESPACE);
345                 *state = current+*l;
346         }
347
348         /* FIXME: Cannot deal with strings that have spaces AND ticks
349          * in them */
350
351         return (char*) current;
352 }
353
354 char **split_path_and_make_absolute(const char *p) {
355         char **l;
356         assert(p);
357
358         if (!(l = strv_split(p, ":")))
359                 return NULL;
360
361         if (!strv_path_make_absolute_cwd(l)) {
362                 strv_free(l);
363                 return NULL;
364         }
365
366         return l;
367 }
368
369 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
370         int r;
371         FILE *f;
372         char fn[132], line[256], *p;
373         long long unsigned ppid;
374
375         assert(pid >= 0);
376         assert(_ppid);
377
378         assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%llu/stat", (unsigned long long) pid) < (int) (sizeof(fn)-1));
379         fn[sizeof(fn)-1] = 0;
380
381         if (!(f = fopen(fn, "r")))
382                 return -errno;
383
384         if (!(fgets(line, sizeof(line), f))) {
385                 r = -errno;
386                 fclose(f);
387                 return r;
388         }
389
390         fclose(f);
391
392         /* Let's skip the pid and comm fields. The latter is enclosed
393          * in () but does not escape any () in its value, so let's
394          * skip over it manually */
395
396         if (!(p = strrchr(line, ')')))
397                 return -EIO;
398
399         p++;
400
401         if (sscanf(p, " "
402                    "%*c "  /* state */
403                    "%llu ", /* ppid */
404                    &ppid) != 1)
405                 return -EIO;
406
407         if ((long long unsigned) (pid_t) ppid != ppid)
408                 return -ERANGE;
409
410         *_ppid = (pid_t) ppid;
411
412         return 0;
413 }
414
415 int write_one_line_file(const char *fn, const char *line) {
416         FILE *f;
417         int r;
418
419         assert(fn);
420         assert(line);
421
422         if (!(f = fopen(fn, "we")))
423                 return -errno;
424
425         if (fputs(line, f) < 0) {
426                 r = -errno;
427                 goto finish;
428         }
429
430         r = 0;
431 finish:
432         fclose(f);
433         return r;
434 }
435
436 int read_one_line_file(const char *fn, char **line) {
437         FILE *f;
438         int r;
439         char t[2048], *c;
440
441         assert(fn);
442         assert(line);
443
444         if (!(f = fopen(fn, "re")))
445                 return -errno;
446
447         if (!(fgets(t, sizeof(t), f))) {
448                 r = -errno;
449                 goto finish;
450         }
451
452         if (!(c = strdup(t))) {
453                 r = -ENOMEM;
454                 goto finish;
455         }
456
457         *line = c;
458         r = 0;
459
460 finish:
461         fclose(f);
462         return r;
463 }
464
465 char *truncate_nl(char *s) {
466         assert(s);
467
468         s[strcspn(s, NEWLINE)] = 0;
469         return s;
470 }
471
472 int get_process_name(pid_t pid, char **name) {
473         char *p;
474         int r;
475
476         assert(pid >= 1);
477         assert(name);
478
479         if (asprintf(&p, "/proc/%llu/comm", (unsigned long long) pid) < 0)
480                 return -ENOMEM;
481
482         r = read_one_line_file(p, name);
483         free(p);
484
485         if (r < 0)
486                 return r;
487
488         truncate_nl(*name);
489         return 0;
490 }
491
492 char *strappend(const char *s, const char *suffix) {
493         size_t a, b;
494         char *r;
495
496         assert(s);
497         assert(suffix);
498
499         a = strlen(s);
500         b = strlen(suffix);
501
502         if (!(r = new(char, a+b+1)))
503                 return NULL;
504
505         memcpy(r, s, a);
506         memcpy(r+a, suffix, b);
507         r[a+b] = 0;
508
509         return r;
510 }
511
512 int readlink_malloc(const char *p, char **r) {
513         size_t l = 100;
514
515         assert(p);
516         assert(r);
517
518         for (;;) {
519                 char *c;
520                 ssize_t n;
521
522                 if (!(c = new(char, l)))
523                         return -ENOMEM;
524
525                 if ((n = readlink(p, c, l-1)) < 0) {
526                         int ret = -errno;
527                         free(c);
528                         return ret;
529                 }
530
531                 if ((size_t) n < l-1) {
532                         c[n] = 0;
533                         *r = c;
534                         return 0;
535                 }
536
537                 free(c);
538                 l *= 2;
539         }
540 }
541
542 char *file_name_from_path(const char *p) {
543         char *r;
544
545         assert(p);
546
547         if ((r = strrchr(p, '/')))
548                 return r + 1;
549
550         return (char*) p;
551 }
552
553 bool path_is_absolute(const char *p) {
554         assert(p);
555
556         return p[0] == '/';
557 }
558
559 bool is_path(const char *p) {
560
561         return !!strchr(p, '/');
562 }
563
564 char *path_make_absolute(const char *p, const char *prefix) {
565         char *r;
566
567         assert(p);
568
569         /* Makes every item in the list an absolute path by prepending
570          * the prefix, if specified and necessary */
571
572         if (path_is_absolute(p) || !prefix)
573                 return strdup(p);
574
575         if (asprintf(&r, "%s/%s", prefix, p) < 0)
576                 return NULL;
577
578         return r;
579 }
580
581 char *path_make_absolute_cwd(const char *p) {
582         char *cwd, *r;
583
584         assert(p);
585
586         /* Similar to path_make_absolute(), but prefixes with the
587          * current working directory. */
588
589         if (path_is_absolute(p))
590                 return strdup(p);
591
592         if (!(cwd = get_current_dir_name()))
593                 return NULL;
594
595         r = path_make_absolute(p, cwd);
596         free(cwd);
597
598         return r;
599 }
600
601 char **strv_path_make_absolute_cwd(char **l) {
602         char **s;
603
604         /* Goes through every item in the string list and makes it
605          * absolute. This works in place and won't rollback any
606          * changes on failure. */
607
608         STRV_FOREACH(s, l) {
609                 char *t;
610
611                 if (!(t = path_make_absolute_cwd(*s)))
612                         return NULL;
613
614                 free(*s);
615                 *s = t;
616         }
617
618         return l;
619 }
620
621 int reset_all_signal_handlers(void) {
622         int sig;
623
624         for (sig = 1; sig < _NSIG; sig++) {
625                 struct sigaction sa;
626
627                 if (sig == SIGKILL || sig == SIGSTOP)
628                         continue;
629
630                 zero(sa);
631                 sa.sa_handler = SIG_DFL;
632                 sa.sa_flags = SA_RESTART;
633
634                 /* On Linux the first two RT signals are reserved by
635                  * glibc, and sigaction() will return EINVAL for them. */
636                 if ((sigaction(sig, &sa, NULL) < 0))
637                         if (errno != EINVAL)
638                                 return -errno;
639         }
640
641         return 0;
642 }
643
644 char *strstrip(char *s) {
645         char *e, *l = NULL;
646
647         /* Drops trailing whitespace. Modifies the string in
648          * place. Returns pointer to first non-space character */
649
650         s += strspn(s, WHITESPACE);
651
652         for (e = s; *e; e++)
653                 if (!strchr(WHITESPACE, *e))
654                         l = e;
655
656         if (l)
657                 *(l+1) = 0;
658         else
659                 *s = 0;
660
661         return s;
662 }
663
664 char *delete_chars(char *s, const char *bad) {
665         char *f, *t;
666
667         /* Drops all whitespace, regardless where in the string */
668
669         for (f = s, t = s; *f; f++) {
670                 if (strchr(bad, *f))
671                         continue;
672
673                 *(t++) = *f;
674         }
675
676         *t = 0;
677
678         return s;
679 }
680
681 char *file_in_same_dir(const char *path, const char *filename) {
682         char *e, *r;
683         size_t k;
684
685         assert(path);
686         assert(filename);
687
688         /* This removes the last component of path and appends
689          * filename, unless the latter is absolute anyway or the
690          * former isn't */
691
692         if (path_is_absolute(filename))
693                 return strdup(filename);
694
695         if (!(e = strrchr(path, '/')))
696                 return strdup(filename);
697
698         k = strlen(filename);
699         if (!(r = new(char, e-path+1+k+1)))
700                 return NULL;
701
702         memcpy(r, path, e-path+1);
703         memcpy(r+(e-path)+1, filename, k+1);
704
705         return r;
706 }
707
708 int mkdir_parents(const char *path, mode_t mode) {
709         const char *p, *e;
710
711         assert(path);
712
713         /* Creates every parent directory in the path except the last
714          * component. */
715
716         p = path + strspn(path, "/");
717         for (;;) {
718                 int r;
719                 char *t;
720
721                 e = p + strcspn(p, "/");
722                 p = e + strspn(e, "/");
723
724                 /* Is this the last component? If so, then we're
725                  * done */
726                 if (*p == 0)
727                         return 0;
728
729                 if (!(t = strndup(path, e - path)))
730                         return -ENOMEM;
731
732                 r = mkdir(t, mode);
733
734                 free(t);
735
736                 if (r < 0 && errno != EEXIST)
737                         return -errno;
738         }
739 }
740
741 int mkdir_p(const char *path, mode_t mode) {
742         int r;
743
744         /* Like mkdir -p */
745
746         if ((r = mkdir_parents(path, mode)) < 0)
747                 return r;
748
749         if (mkdir(path, mode) < 0)
750                 return -errno;
751
752         return 0;
753 }
754
755 char hexchar(int x) {
756         static const char table[16] = "0123456789abcdef";
757
758         return table[x & 15];
759 }
760
761 int unhexchar(char c) {
762
763         if (c >= '0' && c <= '9')
764                 return c - '0';
765
766         if (c >= 'a' && c <= 'f')
767                 return c - 'a' + 10;
768
769         if (c >= 'A' && c <= 'F')
770                 return c - 'A' + 10;
771
772         return -1;
773 }
774
775 char octchar(int x) {
776         return '0' + (x & 7);
777 }
778
779 int unoctchar(char c) {
780
781         if (c >= '0' && c <= '7')
782                 return c - '0';
783
784         return -1;
785 }
786
787 char decchar(int x) {
788         return '0' + (x % 10);
789 }
790
791 int undecchar(char c) {
792
793         if (c >= '0' && c <= '9')
794                 return c - '0';
795
796         return -1;
797 }
798
799 char *cescape(const char *s) {
800         char *r, *t;
801         const char *f;
802
803         assert(s);
804
805         /* Does C style string escaping. */
806
807         if (!(r = new(char, strlen(s)*4 + 1)))
808                 return NULL;
809
810         for (f = s, t = r; *f; f++)
811
812                 switch (*f) {
813
814                 case '\a':
815                         *(t++) = '\\';
816                         *(t++) = 'a';
817                         break;
818                 case '\b':
819                         *(t++) = '\\';
820                         *(t++) = 'b';
821                         break;
822                 case '\f':
823                         *(t++) = '\\';
824                         *(t++) = 'f';
825                         break;
826                 case '\n':
827                         *(t++) = '\\';
828                         *(t++) = 'n';
829                         break;
830                 case '\r':
831                         *(t++) = '\\';
832                         *(t++) = 'r';
833                         break;
834                 case '\t':
835                         *(t++) = '\\';
836                         *(t++) = 't';
837                         break;
838                 case '\v':
839                         *(t++) = '\\';
840                         *(t++) = 'v';
841                         break;
842                 case '\\':
843                         *(t++) = '\\';
844                         *(t++) = '\\';
845                         break;
846                 case '"':
847                         *(t++) = '\\';
848                         *(t++) = '"';
849                         break;
850                 case '\'':
851                         *(t++) = '\\';
852                         *(t++) = '\'';
853                         break;
854
855                 default:
856                         /* For special chars we prefer octal over
857                          * hexadecimal encoding, simply because glib's
858                          * g_strescape() does the same */
859                         if ((*f < ' ') || (*f >= 127)) {
860                                 *(t++) = '\\';
861                                 *(t++) = octchar((unsigned char) *f >> 6);
862                                 *(t++) = octchar((unsigned char) *f >> 3);
863                                 *(t++) = octchar((unsigned char) *f);
864                         } else
865                                 *(t++) = *f;
866                         break;
867                 }
868
869         *t = 0;
870
871         return r;
872 }
873
874 char *cunescape(const char *s) {
875         char *r, *t;
876         const char *f;
877
878         assert(s);
879
880         /* Undoes C style string escaping */
881
882         if (!(r = new(char, strlen(s)+1)))
883                 return r;
884
885         for (f = s, t = r; *f; f++) {
886
887                 if (*f != '\\') {
888                         *(t++) = *f;
889                         continue;
890                 }
891
892                 f++;
893
894                 switch (*f) {
895
896                 case 'a':
897                         *(t++) = '\a';
898                         break;
899                 case 'b':
900                         *(t++) = '\b';
901                         break;
902                 case 'f':
903                         *(t++) = '\f';
904                         break;
905                 case 'n':
906                         *(t++) = '\n';
907                         break;
908                 case 'r':
909                         *(t++) = '\r';
910                         break;
911                 case 't':
912                         *(t++) = '\t';
913                         break;
914                 case 'v':
915                         *(t++) = '\v';
916                         break;
917                 case '\\':
918                         *(t++) = '\\';
919                         break;
920                 case '"':
921                         *(t++) = '"';
922                         break;
923                 case '\'':
924                         *(t++) = '\'';
925                         break;
926
927                 case 'x': {
928                         /* hexadecimal encoding */
929                         int a, b;
930
931                         if ((a = unhexchar(f[1])) < 0 ||
932                             (b = unhexchar(f[2])) < 0) {
933                                 /* Invalid escape code, let's take it literal then */
934                                 *(t++) = '\\';
935                                 *(t++) = 'x';
936                         } else {
937                                 *(t++) = (char) ((a << 4) | b);
938                                 f += 2;
939                         }
940
941                         break;
942                 }
943
944                 case '0':
945                 case '1':
946                 case '2':
947                 case '3':
948                 case '4':
949                 case '5':
950                 case '6':
951                 case '7': {
952                         /* octal encoding */
953                         int a, b, c;
954
955                         if ((a = unoctchar(f[0])) < 0 ||
956                             (b = unoctchar(f[1])) < 0 ||
957                             (c = unoctchar(f[2])) < 0) {
958                                 /* Invalid escape code, let's take it literal then */
959                                 *(t++) = '\\';
960                                 *(t++) = f[0];
961                         } else {
962                                 *(t++) = (char) ((a << 6) | (b << 3) | c);
963                                 f += 2;
964                         }
965
966                         break;
967                 }
968
969                 case 0:
970                         /* premature end of string.*/
971                         *(t++) = '\\';
972                         goto finish;
973
974                 default:
975                         /* Invalid escape code, let's take it literal then */
976                         *(t++) = '\\';
977                         *(t++) = 'f';
978                         break;
979                 }
980         }
981
982 finish:
983         *t = 0;
984         return r;
985 }
986
987
988 char *xescape(const char *s, const char *bad) {
989         char *r, *t;
990         const char *f;
991
992         /* Escapes all chars in bad, in addition to \ and all special
993          * chars, in \xFF style escaping. May be reversed with
994          * cunescape. */
995
996         if (!(r = new(char, strlen(s)*4+1)))
997                 return NULL;
998
999         for (f = s, t = r; *f; f++) {
1000
1001                 if ((*f < ' ') || (*f >= 127) ||
1002                     (*f == '\\') || strchr(bad, *f)) {
1003                         *(t++) = '\\';
1004                         *(t++) = 'x';
1005                         *(t++) = hexchar(*f >> 4);
1006                         *(t++) = hexchar(*f);
1007                 } else
1008                         *(t++) = *f;
1009         }
1010
1011         *t = 0;
1012
1013         return r;
1014 }
1015
1016 char *bus_path_escape(const char *s) {
1017         char *r, *t;
1018         const char *f;
1019
1020         assert(s);
1021
1022         /* Escapes all chars that D-Bus' object path cannot deal
1023          * with. Can be reverse with bus_path_unescape() */
1024
1025         if (!(r = new(char, strlen(s)*3+1)))
1026                 return NULL;
1027
1028         for (f = s, t = r; *f; f++) {
1029
1030                 if (!(*f >= 'A' && *f <= 'Z') &&
1031                     !(*f >= 'a' && *f <= 'z') &&
1032                     !(*f >= '0' && *f <= '9')) {
1033                         *(t++) = '_';
1034                         *(t++) = hexchar(*f >> 4);
1035                         *(t++) = hexchar(*f);
1036                 } else
1037                         *(t++) = *f;
1038         }
1039
1040         *t = 0;
1041
1042         return r;
1043 }
1044
1045 char *bus_path_unescape(const char *f) {
1046         char *r, *t;
1047
1048         assert(f);
1049
1050         if (!(r = strdup(f)))
1051                 return NULL;
1052
1053         for (t = r; *f; f++) {
1054
1055                 if (*f == '_') {
1056                         int a, b;
1057
1058                         if ((a = unhexchar(f[1])) < 0 ||
1059                             (b = unhexchar(f[2])) < 0) {
1060                                 /* Invalid escape code, let's take it literal then */
1061                                 *(t++) = '_';
1062                         } else {
1063                                 *(t++) = (char) ((a << 4) | b);
1064                                 f += 2;
1065                         }
1066                 } else
1067                         *(t++) = *f;
1068         }
1069
1070         *t = 0;
1071
1072         return r;
1073 }
1074
1075 char *path_kill_slashes(char *path) {
1076         char *f, *t;
1077         bool slash = false;
1078
1079         /* Removes redundant inner and trailing slashes. Modifies the
1080          * passed string in-place.
1081          *
1082          * ///foo///bar/ becomes /foo/bar
1083          */
1084
1085         for (f = path, t = path; *f; f++) {
1086
1087                 if (*f == '/') {
1088                         slash = true;
1089                         continue;
1090                 }
1091
1092                 if (slash) {
1093                         slash = false;
1094                         *(t++) = '/';
1095                 }
1096
1097                 *(t++) = *f;
1098         }
1099
1100         /* Special rule, if we are talking of the root directory, a
1101         trailing slash is good */
1102
1103         if (t == path && slash)
1104                 *(t++) = '/';
1105
1106         *t = 0;
1107         return path;
1108 }
1109
1110 bool path_startswith(const char *path, const char *prefix) {
1111         assert(path);
1112         assert(prefix);
1113
1114         if ((path[0] == '/') != (prefix[0] == '/'))
1115                 return false;
1116
1117         for (;;) {
1118                 size_t a, b;
1119
1120                 path += strspn(path, "/");
1121                 prefix += strspn(prefix, "/");
1122
1123                 if (*prefix == 0)
1124                         return true;
1125
1126                 if (*path == 0)
1127                         return false;
1128
1129                 a = strcspn(path, "/");
1130                 b = strcspn(prefix, "/");
1131
1132                 if (a != b)
1133                         return false;
1134
1135                 if (memcmp(path, prefix, a) != 0)
1136                         return false;
1137
1138                 path += a;
1139                 prefix += b;
1140         }
1141 }
1142
1143 char *ascii_strlower(char *t) {
1144         char *p;
1145
1146         assert(t);
1147
1148         for (p = t; *p; p++)
1149                 if (*p >= 'A' && *p <= 'Z')
1150                         *p = *p - 'A' + 'a';
1151
1152         return t;
1153 }
1154
1155 bool ignore_file(const char *filename) {
1156         assert(filename);
1157
1158         return
1159                 filename[0] == '.' ||
1160                 endswith(filename, "~") ||
1161                 endswith(filename, ".rpmnew") ||
1162                 endswith(filename, ".rpmsave") ||
1163                 endswith(filename, ".rpmorig") ||
1164                 endswith(filename, ".dpkg-old") ||
1165                 endswith(filename, ".dpkg-new") ||
1166                 endswith(filename, ".swp");
1167 }
1168
1169 int fd_nonblock(int fd, bool nonblock) {
1170         int flags;
1171
1172         assert(fd >= 0);
1173
1174         if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
1175                 return -errno;
1176
1177         if (nonblock)
1178                 flags |= O_NONBLOCK;
1179         else
1180                 flags &= ~O_NONBLOCK;
1181
1182         if (fcntl(fd, F_SETFL, flags) < 0)
1183                 return -errno;
1184
1185         return 0;
1186 }
1187
1188 int fd_cloexec(int fd, bool cloexec) {
1189         int flags;
1190
1191         assert(fd >= 0);
1192
1193         if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
1194                 return -errno;
1195
1196         if (cloexec)
1197                 flags |= FD_CLOEXEC;
1198         else
1199                 flags &= ~FD_CLOEXEC;
1200
1201         if (fcntl(fd, F_SETFD, flags) < 0)
1202                 return -errno;
1203
1204         return 0;
1205 }
1206
1207 int close_all_fds(const int except[], unsigned n_except) {
1208         DIR *d;
1209         struct dirent *de;
1210         int r = 0;
1211
1212         if (!(d = opendir("/proc/self/fd")))
1213                 return -errno;
1214
1215         while ((de = readdir(d))) {
1216                 int fd = -1;
1217
1218                 if (de->d_name[0] == '.')
1219                         continue;
1220
1221                 if ((r = safe_atoi(de->d_name, &fd)) < 0)
1222                         goto finish;
1223
1224                 if (fd < 3)
1225                         continue;
1226
1227                 if (fd == dirfd(d))
1228                         continue;
1229
1230                 if (except) {
1231                         bool found;
1232                         unsigned i;
1233
1234                         found = false;
1235                         for (i = 0; i < n_except; i++)
1236                                 if (except[i] == fd) {
1237                                         found = true;
1238                                         break;
1239                                 }
1240
1241                         if (found)
1242                                 continue;
1243                 }
1244
1245                 if ((r = close_nointr(fd)) < 0) {
1246                         /* Valgrind has its own FD and doesn't want to have it closed */
1247                         if (errno != EBADF)
1248                                 goto finish;
1249                 }
1250         }
1251
1252         r = 0;
1253
1254 finish:
1255         closedir(d);
1256         return r;
1257 }
1258
1259 bool chars_intersect(const char *a, const char *b) {
1260         const char *p;
1261
1262         /* Returns true if any of the chars in a are in b. */
1263         for (p = a; *p; p++)
1264                 if (strchr(b, *p))
1265                         return true;
1266
1267         return false;
1268 }
1269
1270 char *format_timestamp(char *buf, size_t l, usec_t t) {
1271         struct tm tm;
1272         time_t sec;
1273
1274         assert(buf);
1275         assert(l > 0);
1276
1277         if (t <= 0)
1278                 return NULL;
1279
1280         sec = (time_t) t / USEC_PER_SEC;
1281
1282         if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0)
1283                 return NULL;
1284
1285         return buf;
1286 }
1287
1288 bool fstype_is_network(const char *fstype) {
1289         static const char * const table[] = {
1290                 "cifs",
1291                 "smbfs",
1292                 "ncpfs",
1293                 "nfs",
1294                 "nfs4"
1295         };
1296
1297         unsigned i;
1298
1299         for (i = 0; i < ELEMENTSOF(table); i++)
1300                 if (streq(table[i], fstype))
1301                         return true;
1302
1303         return false;
1304 }
1305
1306 int chvt(int vt) {
1307         int fd, r = 0;
1308
1309         if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
1310                 return -errno;
1311
1312         if (vt < 0) {
1313                 int tiocl[2] = {
1314                         TIOCL_GETKMSGREDIRECT,
1315                         0
1316                 };
1317
1318                 if (ioctl(fd, TIOCLINUX, tiocl) < 0)
1319                         return -errno;
1320
1321                 vt = tiocl[0] <= 0 ? 1 : tiocl[0];
1322         }
1323
1324         if (ioctl(fd, VT_ACTIVATE, vt) < 0)
1325                 r = -errno;
1326
1327         close_nointr(r);
1328         return r;
1329 }
1330
1331 int read_one_char(FILE *f, char *ret, bool *need_nl) {
1332         struct termios old_termios, new_termios;
1333         char c;
1334         char line[1024];
1335
1336         assert(f);
1337         assert(ret);
1338
1339         if (tcgetattr(fileno(f), &old_termios) >= 0) {
1340                 new_termios = old_termios;
1341
1342                 new_termios.c_lflag &= ~ICANON;
1343                 new_termios.c_cc[VMIN] = 1;
1344                 new_termios.c_cc[VTIME] = 0;
1345
1346                 if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
1347                         size_t k;
1348
1349                         k = fread(&c, 1, 1, f);
1350
1351                         tcsetattr(fileno(f), TCSADRAIN, &old_termios);
1352
1353                         if (k <= 0)
1354                                 return -EIO;
1355
1356                         if (need_nl)
1357                                 *need_nl = c != '\n';
1358
1359                         *ret = c;
1360                         return 0;
1361                 }
1362         }
1363
1364         if (!(fgets(line, sizeof(line), f)))
1365                 return -EIO;
1366
1367         truncate_nl(line);
1368
1369         if (strlen(line) != 1)
1370                 return -EBADMSG;
1371
1372         if (need_nl)
1373                 *need_nl = false;
1374
1375         *ret = line[0];
1376         return 0;
1377 }
1378
1379 int ask(char *ret, const char *replies, const char *text, ...) {
1380         assert(ret);
1381         assert(replies);
1382         assert(text);
1383
1384         for (;;) {
1385                 va_list ap;
1386                 char c;
1387                 int r;
1388                 bool need_nl = true;
1389
1390                 va_start(ap, text);
1391                 vprintf(text, ap);
1392                 va_end(ap);
1393
1394                 fflush(stdout);
1395
1396                 if ((r = read_one_char(stdin, &c, &need_nl)) < 0) {
1397
1398                         if (r == -EBADMSG) {
1399                                 puts("Bad input, please try again.");
1400                                 continue;
1401                         }
1402
1403                         putchar('\n');
1404                         return r;
1405                 }
1406
1407                 if (need_nl)
1408                         putchar('\n');
1409
1410                 if (strchr(replies, c)) {
1411                         *ret = c;
1412                         return 0;
1413                 }
1414
1415                 puts("Read unexpected character, please try again.");
1416         }
1417 }
1418
1419 int reset_terminal(int fd) {
1420         struct termios termios;
1421         int r = 0;
1422
1423         assert(fd >= 0);
1424
1425         /* Set terminal to some sane defaults */
1426
1427         if (tcgetattr(fd, &termios) < 0) {
1428                 r = -errno;
1429                 goto finish;
1430         }
1431
1432         /* We only reset the stuff that matters to the software. How
1433          * hardware is set up we don't touch assuming that somebody
1434          * else will do that for us */
1435
1436         termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
1437         termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
1438         termios.c_oflag |= ONLCR;
1439         termios.c_cflag |= CREAD;
1440         termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
1441
1442         termios.c_cc[VINTR]    =   03;  /* ^C */
1443         termios.c_cc[VQUIT]    =  034;  /* ^\ */
1444         termios.c_cc[VERASE]   = 0177;
1445         termios.c_cc[VKILL]    =  025;  /* ^X */
1446         termios.c_cc[VEOF]     =   04;  /* ^D */
1447         termios.c_cc[VSTART]   =  021;  /* ^Q */
1448         termios.c_cc[VSTOP]    =  023;  /* ^S */
1449         termios.c_cc[VSUSP]    =  032;  /* ^Z */
1450         termios.c_cc[VLNEXT]   =  026;  /* ^V */
1451         termios.c_cc[VWERASE]  =  027;  /* ^W */
1452         termios.c_cc[VREPRINT] =  022;  /* ^R */
1453         termios.c_cc[VEOL]     =    0;
1454         termios.c_cc[VEOL2]    =    0;
1455
1456         termios.c_cc[VTIME]  = 0;
1457         termios.c_cc[VMIN]   = 1;
1458
1459         if (tcsetattr(fd, TCSANOW, &termios) < 0)
1460                 r = -errno;
1461
1462 finish:
1463         /* Just in case, flush all crap out */
1464         tcflush(fd, TCIOFLUSH);
1465
1466         return r;
1467 }
1468
1469 int open_terminal(const char *name, int mode) {
1470         int fd, r;
1471
1472         if ((fd = open(name, mode)) < 0)
1473                 return -errno;
1474
1475         if ((r = isatty(fd)) < 0) {
1476                 close_nointr_nofail(fd);
1477                 return -errno;
1478         }
1479
1480         if (!r) {
1481                 close_nointr_nofail(fd);
1482                 return -ENOTTY;
1483         }
1484
1485         return fd;
1486 }
1487
1488 int flush_fd(int fd) {
1489         struct pollfd pollfd;
1490
1491         zero(pollfd);
1492         pollfd.fd = fd;
1493         pollfd.events = POLLIN;
1494
1495         for (;;) {
1496                 char buf[1024];
1497                 ssize_t l;
1498                 int r;
1499
1500                 if ((r = poll(&pollfd, 1, 0)) < 0) {
1501
1502                         if (errno == EINTR)
1503                                 continue;
1504
1505                         return -errno;
1506                 }
1507
1508                 if (r == 0)
1509                         return 0;
1510
1511                 if ((l = read(fd, buf, sizeof(buf))) < 0) {
1512
1513                         if (errno == EINTR)
1514                                 continue;
1515
1516                         if (errno == EAGAIN)
1517                                 return 0;
1518
1519                         return -errno;
1520                 }
1521
1522                 if (l <= 0)
1523                         return 0;
1524         }
1525 }
1526
1527 int acquire_terminal(const char *name, bool fail, bool force) {
1528         int fd = -1, notify = -1, r, wd;
1529
1530         assert(name);
1531
1532         /* We use inotify to be notified when the tty is closed. We
1533          * create the watch before checking if we can actually acquire
1534          * it, so that we don't lose any event.
1535          *
1536          * Note: strictly speaking this actually watches for the
1537          * device being closed, it does *not* really watch whether a
1538          * tty loses its controlling process. However, unless some
1539          * rogue process uses TIOCNOTTY on /dev/tty *after* closing
1540          * its tty otherwise this will not become a problem. As long
1541          * as the administrator makes sure not configure any service
1542          * on the same tty as an untrusted user this should not be a
1543          * problem. (Which he probably should not do anyway.) */
1544
1545         if (!fail && !force) {
1546                 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
1547                         r = -errno;
1548                         goto fail;
1549                 }
1550
1551                 if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) {
1552                         r = -errno;
1553                         goto fail;
1554                 }
1555         }
1556
1557         for (;;) {
1558                 if ((r = flush_fd(notify)) < 0)
1559                         goto fail;
1560
1561                 /* We pass here O_NOCTTY only so that we can check the return
1562                  * value TIOCSCTTY and have a reliable way to figure out if we
1563                  * successfully became the controlling process of the tty */
1564                 if ((fd = open_terminal(name, O_RDWR|O_NOCTTY)) < 0)
1565                         return -errno;
1566
1567                 /* First, try to get the tty */
1568                 if ((r = ioctl(fd, TIOCSCTTY, force)) < 0 &&
1569                     (force || fail || errno != EPERM)) {
1570                         r = -errno;
1571                         goto fail;
1572                 }
1573
1574                 if (r >= 0)
1575                         break;
1576
1577                 assert(!fail);
1578                 assert(!force);
1579                 assert(notify >= 0);
1580
1581                 for (;;) {
1582                         struct inotify_event e;
1583                         ssize_t l;
1584
1585                         if ((l = read(notify, &e, sizeof(e))) != sizeof(e)) {
1586
1587                                 if (l < 0) {
1588
1589                                         if (errno == EINTR)
1590                                                 continue;
1591
1592                                         r = -errno;
1593                                 } else
1594                                         r = -EIO;
1595
1596                                 goto fail;
1597                         }
1598
1599                         if (e.wd != wd || !(e.mask & IN_CLOSE)) {
1600                                 r = -errno;
1601                                 goto fail;
1602                         }
1603
1604                         break;
1605                 }
1606
1607                 /* We close the tty fd here since if the old session
1608                  * ended our handle will be dead. It's important that
1609                  * we do this after sleeping, so that we don't enter
1610                  * an endless loop. */
1611                 close_nointr_nofail(fd);
1612         }
1613
1614         if (notify >= 0)
1615                 close_nointr(notify);
1616
1617         if ((r = reset_terminal(fd)) < 0)
1618                 log_warning("Failed to reset terminal: %s", strerror(-r));
1619
1620         return fd;
1621
1622 fail:
1623         if (fd >= 0)
1624                 close_nointr(fd);
1625
1626         if (notify >= 0)
1627                 close_nointr(notify);
1628
1629         return r;
1630 }
1631
1632 int release_terminal(void) {
1633         int r = 0, fd;
1634
1635         if ((fd = open("/dev/tty", O_RDWR)) < 0)
1636                 return -errno;
1637
1638         if (ioctl(fd, TIOCNOTTY) < 0)
1639                 r = -errno;
1640
1641         close_nointr_nofail(fd);
1642         return r;
1643 }
1644
1645 int ignore_signal(int sig) {
1646         struct sigaction sa;
1647
1648         zero(sa);
1649         sa.sa_handler = SIG_IGN;
1650         sa.sa_flags = SA_RESTART;
1651
1652         return sigaction(sig, &sa, NULL);
1653 }
1654
1655 int close_pipe(int p[]) {
1656         int a = 0, b = 0;
1657
1658         assert(p);
1659
1660         if (p[0] >= 0) {
1661                 a = close_nointr(p[0]);
1662                 p[0] = -1;
1663         }
1664
1665         if (p[1] >= 0) {
1666                 b = close_nointr(p[1]);
1667                 p[1] = -1;
1668         }
1669
1670         return a < 0 ? a : b;
1671 }
1672
1673 ssize_t loop_read(int fd, void *buf, size_t nbytes) {
1674         uint8_t *p;
1675         ssize_t n = 0;
1676
1677         assert(fd >= 0);
1678         assert(buf);
1679
1680         p = buf;
1681
1682         while (nbytes > 0) {
1683                 ssize_t k;
1684
1685                 if ((k = read(fd, p, nbytes)) <= 0) {
1686
1687                         if (errno == EINTR)
1688                                 continue;
1689
1690                         if (errno == EAGAIN) {
1691                                 struct pollfd pollfd;
1692
1693                                 zero(pollfd);
1694                                 pollfd.fd = fd;
1695                                 pollfd.events = POLLIN;
1696
1697                                 if (poll(&pollfd, 1, -1) < 0) {
1698                                         if (errno == EINTR)
1699                                                 continue;
1700
1701                                         return n > 0 ? n : -errno;
1702                                 }
1703
1704                                 if (pollfd.revents != POLLIN)
1705                                         return n > 0 ? n : -EIO;
1706
1707                                 continue;
1708                         }
1709
1710                         return n > 0 ? n : (k < 0 ? -errno : 0);
1711                 }
1712
1713                 p += k;
1714                 nbytes -= k;
1715                 n += k;
1716         }
1717
1718         return n;
1719 }
1720
1721 int path_is_mount_point(const char *t) {
1722         struct stat a, b;
1723         char *copy;
1724
1725         if (lstat(t, &a) < 0) {
1726
1727                 if (errno == ENOENT)
1728                         return 0;
1729
1730                 return -errno;
1731         }
1732
1733         if (!(copy = strdup(t)))
1734                 return -ENOMEM;
1735
1736         if (lstat(dirname(copy), &b) < 0) {
1737                 free(copy);
1738                 return -errno;
1739         }
1740
1741         free(copy);
1742
1743         return a.st_dev != b.st_dev;
1744 }
1745
1746 static const char *const ioprio_class_table[] = {
1747         [IOPRIO_CLASS_NONE] = "none",
1748         [IOPRIO_CLASS_RT] = "realtime",
1749         [IOPRIO_CLASS_BE] = "best-effort",
1750         [IOPRIO_CLASS_IDLE] = "idle"
1751 };
1752
1753 DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
1754
1755 static const char *const sigchld_code_table[] = {
1756         [CLD_EXITED] = "exited",
1757         [CLD_KILLED] = "killed",
1758         [CLD_DUMPED] = "dumped",
1759         [CLD_TRAPPED] = "trapped",
1760         [CLD_STOPPED] = "stopped",
1761         [CLD_CONTINUED] = "continued",
1762 };
1763
1764 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
1765
1766 static const char *const log_facility_table[LOG_NFACILITIES] = {
1767         [LOG_FAC(LOG_KERN)] = "kern",
1768         [LOG_FAC(LOG_USER)] = "user",
1769         [LOG_FAC(LOG_MAIL)] = "mail",
1770         [LOG_FAC(LOG_DAEMON)] = "daemon",
1771         [LOG_FAC(LOG_AUTH)] = "auth",
1772         [LOG_FAC(LOG_SYSLOG)] = "syslog",
1773         [LOG_FAC(LOG_LPR)] = "lpr",
1774         [LOG_FAC(LOG_NEWS)] = "news",
1775         [LOG_FAC(LOG_UUCP)] = "uucp",
1776         [LOG_FAC(LOG_CRON)] = "cron",
1777         [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
1778         [LOG_FAC(LOG_FTP)] = "ftp",
1779         [LOG_FAC(LOG_LOCAL0)] = "local0",
1780         [LOG_FAC(LOG_LOCAL1)] = "local1",
1781         [LOG_FAC(LOG_LOCAL2)] = "local2",
1782         [LOG_FAC(LOG_LOCAL3)] = "local3",
1783         [LOG_FAC(LOG_LOCAL4)] = "local4",
1784         [LOG_FAC(LOG_LOCAL5)] = "local5",
1785         [LOG_FAC(LOG_LOCAL6)] = "local6",
1786         [LOG_FAC(LOG_LOCAL7)] = "local7"
1787 };
1788
1789 DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
1790
1791 static const char *const log_level_table[] = {
1792         [LOG_EMERG] = "emerg",
1793         [LOG_ALERT] = "alert",
1794         [LOG_CRIT] = "crit",
1795         [LOG_ERR] = "err",
1796         [LOG_WARNING] = "warning",
1797         [LOG_NOTICE] = "notice",
1798         [LOG_INFO] = "info",
1799         [LOG_DEBUG] = "debug"
1800 };
1801
1802 DEFINE_STRING_TABLE_LOOKUP(log_level, int);
1803
1804 static const char* const sched_policy_table[] = {
1805         [SCHED_OTHER] = "other",
1806         [SCHED_BATCH] = "batch",
1807         [SCHED_IDLE] = "idle",
1808         [SCHED_FIFO] = "fifo",
1809         [SCHED_RR] = "rr"
1810 };
1811
1812 DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
1813
1814 static const char* const rlimit_table[] = {
1815         [RLIMIT_CPU] = "LimitCPU",
1816         [RLIMIT_FSIZE] = "LimitFSIZE",
1817         [RLIMIT_DATA] = "LimitDATA",
1818         [RLIMIT_STACK] = "LimitSTACK",
1819         [RLIMIT_CORE] = "LimitCORE",
1820         [RLIMIT_RSS] = "LimitRSS",
1821         [RLIMIT_NOFILE] = "LimitNOFILE",
1822         [RLIMIT_AS] = "LimitAS",
1823         [RLIMIT_NPROC] = "LimitNPROC",
1824         [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
1825         [RLIMIT_LOCKS] = "LimitLOCKS",
1826         [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
1827         [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
1828         [RLIMIT_NICE] = "LimitNICE",
1829         [RLIMIT_RTPRIO] = "LimitRTPRIO",
1830         [RLIMIT_RTTIME] = "LimitRTTIME"
1831 };
1832
1833 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);