]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/util.c
dbus: shut down bus connection cleanly and fully when a direct client disconnects
[thirdparty/systemd.git] / src / util.c
CommitLineData
60918275
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
60918275
LP
22#include <assert.h>
23#include <string.h>
24#include <unistd.h>
25#include <errno.h>
85261803 26#include <stdlib.h>
034c6ed7
LP
27#include <signal.h>
28#include <stdio.h>
1dccbe19
LP
29#include <syslog.h>
30#include <sched.h>
31#include <sys/resource.h>
ef886c6a 32#include <linux/sched.h>
a9f5d454
LP
33#include <sys/types.h>
34#include <sys/stat.h>
3a0ecb08 35#include <fcntl.h>
a0d40ac5 36#include <dirent.h>
601f6a1e
LP
37#include <sys/ioctl.h>
38#include <linux/vt.h>
39#include <linux/tiocl.h>
80876c20
LP
40#include <termios.h>
41#include <stdarg.h>
42#include <sys/inotify.h>
43#include <sys/poll.h>
8d567588 44#include <libgen.h>
3177a7fa 45#include <ctype.h>
5b6319dc 46#include <sys/prctl.h>
ef2f1067
LP
47#include <sys/utsname.h>
48#include <pwd.h>
4fd5948e 49#include <netinet/ip.h>
60918275
LP
50
51#include "macro.h"
52#include "util.h"
1dccbe19
LP
53#include "ioprio.h"
54#include "missing.h"
a9f5d454 55#include "log.h"
65d2ebdc 56#include "strv.h"
60918275 57
e05797fb
LP
58bool streq_ptr(const char *a, const char *b) {
59
60 /* Like streq(), but tries to make sense of NULL pointers */
61
62 if (a && b)
63 return streq(a, b);
64
65 if (!a && !b)
66 return true;
67
68 return false;
69}
70
47be870b 71usec_t now(clockid_t clock_id) {
60918275
LP
72 struct timespec ts;
73
47be870b 74 assert_se(clock_gettime(clock_id, &ts) == 0);
60918275
LP
75
76 return timespec_load(&ts);
77}
78
63983207 79dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
871d7de4
LP
80 assert(ts);
81
82 ts->realtime = now(CLOCK_REALTIME);
83 ts->monotonic = now(CLOCK_MONOTONIC);
84
85 return ts;
86}
87
60918275
LP
88usec_t timespec_load(const struct timespec *ts) {
89 assert(ts);
90
91 return
92 (usec_t) ts->tv_sec * USEC_PER_SEC +
93 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
94}
95
96struct timespec *timespec_store(struct timespec *ts, usec_t u) {
97 assert(ts);
98
99 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
100 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
101
102 return ts;
103}
104
105usec_t timeval_load(const struct timeval *tv) {
106 assert(tv);
107
108 return
109 (usec_t) tv->tv_sec * USEC_PER_SEC +
110 (usec_t) tv->tv_usec;
111}
112
113struct timeval *timeval_store(struct timeval *tv, usec_t u) {
114 assert(tv);
115
116 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
117 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
118
119 return tv;
120}
121
122bool endswith(const char *s, const char *postfix) {
123 size_t sl, pl;
124
125 assert(s);
126 assert(postfix);
127
128 sl = strlen(s);
129 pl = strlen(postfix);
130
d4d0d4db
LP
131 if (pl == 0)
132 return true;
133
60918275
LP
134 if (sl < pl)
135 return false;
136
137 return memcmp(s + sl - pl, postfix, pl) == 0;
138}
139
140bool startswith(const char *s, const char *prefix) {
141 size_t sl, pl;
142
143 assert(s);
144 assert(prefix);
145
146 sl = strlen(s);
147 pl = strlen(prefix);
148
d4d0d4db
LP
149 if (pl == 0)
150 return true;
151
60918275
LP
152 if (sl < pl)
153 return false;
154
155 return memcmp(s, prefix, pl) == 0;
156}
157
3177a7fa
MAP
158bool startswith_no_case(const char *s, const char *prefix) {
159 size_t sl, pl;
160 unsigned i;
161
162 assert(s);
163 assert(prefix);
164
165 sl = strlen(s);
166 pl = strlen(prefix);
167
168 if (pl == 0)
169 return true;
170
171 if (sl < pl)
172 return false;
173
174 for(i = 0; i < pl; ++i) {
175 if (tolower(s[i]) != tolower(prefix[i]))
176 return false;
177 }
178
179 return true;
180}
181
79d6d816
LP
182bool first_word(const char *s, const char *word) {
183 size_t sl, wl;
184
185 assert(s);
186 assert(word);
187
188 sl = strlen(s);
189 wl = strlen(word);
190
191 if (sl < wl)
192 return false;
193
d4d0d4db
LP
194 if (wl == 0)
195 return true;
196
79d6d816
LP
197 if (memcmp(s, word, wl) != 0)
198 return false;
199
d4d0d4db
LP
200 return s[wl] == 0 ||
201 strchr(WHITESPACE, s[wl]);
79d6d816
LP
202}
203
42f4e3c4 204int close_nointr(int fd) {
60918275
LP
205 assert(fd >= 0);
206
207 for (;;) {
208 int r;
209
210 if ((r = close(fd)) >= 0)
211 return r;
212
213 if (errno != EINTR)
214 return r;
215 }
216}
85261803 217
85f136b5 218void close_nointr_nofail(int fd) {
80876c20 219 int saved_errno = errno;
85f136b5
LP
220
221 /* like close_nointr() but cannot fail, and guarantees errno
222 * is unchanged */
223
224 assert_se(close_nointr(fd) == 0);
80876c20
LP
225
226 errno = saved_errno;
85f136b5
LP
227}
228
5b6319dc
LP
229void close_many(const int fds[], unsigned n_fd) {
230 unsigned i;
231
232 for (i = 0; i < n_fd; i++)
233 close_nointr_nofail(fds[i]);
234}
235
85261803
LP
236int parse_boolean(const char *v) {
237 assert(v);
238
44d8db9e 239 if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
85261803 240 return 1;
44d8db9e 241 else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
85261803
LP
242 return 0;
243
244 return -EINVAL;
245}
246
3ba686c1
LP
247int parse_pid(const char *s, pid_t* ret_pid) {
248 unsigned long ul;
249 pid_t pid;
250 int r;
251
252 assert(s);
253 assert(ret_pid);
254
255 if ((r = safe_atolu(s, &ul)) < 0)
256 return r;
257
258 pid = (pid_t) ul;
259
260 if ((unsigned long) pid != ul)
261 return -ERANGE;
262
263 if (pid <= 0)
264 return -ERANGE;
265
266 *ret_pid = pid;
267 return 0;
268}
269
85261803
LP
270int safe_atou(const char *s, unsigned *ret_u) {
271 char *x = NULL;
034c6ed7 272 unsigned long l;
85261803
LP
273
274 assert(s);
275 assert(ret_u);
276
277 errno = 0;
278 l = strtoul(s, &x, 0);
279
280 if (!x || *x || errno)
281 return errno ? -errno : -EINVAL;
282
034c6ed7 283 if ((unsigned long) (unsigned) l != l)
85261803
LP
284 return -ERANGE;
285
286 *ret_u = (unsigned) l;
287 return 0;
288}
289
290int safe_atoi(const char *s, int *ret_i) {
291 char *x = NULL;
034c6ed7 292 long l;
85261803
LP
293
294 assert(s);
295 assert(ret_i);
296
297 errno = 0;
298 l = strtol(s, &x, 0);
299
300 if (!x || *x || errno)
301 return errno ? -errno : -EINVAL;
302
034c6ed7 303 if ((long) (int) l != l)
85261803
LP
304 return -ERANGE;
305
034c6ed7
LP
306 *ret_i = (int) l;
307 return 0;
308}
309
034c6ed7
LP
310int safe_atollu(const char *s, long long unsigned *ret_llu) {
311 char *x = NULL;
312 unsigned long long l;
313
314 assert(s);
315 assert(ret_llu);
316
317 errno = 0;
318 l = strtoull(s, &x, 0);
319
320 if (!x || *x || errno)
321 return errno ? -errno : -EINVAL;
322
323 *ret_llu = l;
324 return 0;
325}
326
327int safe_atolli(const char *s, long long int *ret_lli) {
328 char *x = NULL;
329 long long l;
330
331 assert(s);
332 assert(ret_lli);
333
334 errno = 0;
335 l = strtoll(s, &x, 0);
336
337 if (!x || *x || errno)
338 return errno ? -errno : -EINVAL;
339
340 *ret_lli = l;
85261803
LP
341 return 0;
342}
a41e8209 343
a41e8209 344/* Split a string into words. */
65d2ebdc 345char *split(const char *c, size_t *l, const char *separator, char **state) {
a41e8209
LP
346 char *current;
347
348 current = *state ? *state : (char*) c;
349
350 if (!*current || *c == 0)
351 return NULL;
352
65d2ebdc
LP
353 current += strspn(current, separator);
354 *l = strcspn(current, separator);
82919e3d
LP
355 *state = current+*l;
356
357 return (char*) current;
358}
359
034c6ed7
LP
360/* Split a string into words, but consider strings enclosed in '' and
361 * "" as words even if they include spaces. */
362char *split_quoted(const char *c, size_t *l, char **state) {
0bab36f2
LP
363 char *current, *e;
364 bool escaped = false;
034c6ed7
LP
365
366 current = *state ? *state : (char*) c;
367
368 if (!*current || *c == 0)
369 return NULL;
370
371 current += strspn(current, WHITESPACE);
372
373 if (*current == '\'') {
374 current ++;
034c6ed7 375
0bab36f2
LP
376 for (e = current; *e; e++) {
377 if (escaped)
378 escaped = false;
379 else if (*e == '\\')
380 escaped = true;
381 else if (*e == '\'')
382 break;
383 }
384
385 *l = e-current;
386 *state = *e == 0 ? e : e+1;
034c6ed7
LP
387 } else if (*current == '\"') {
388 current ++;
034c6ed7 389
0bab36f2
LP
390 for (e = current; *e; e++) {
391 if (escaped)
392 escaped = false;
393 else if (*e == '\\')
394 escaped = true;
395 else if (*e == '\"')
396 break;
397 }
398
399 *l = e-current;
400 *state = *e == 0 ? e : e+1;
034c6ed7 401 } else {
0bab36f2
LP
402 for (e = current; *e; e++) {
403 if (escaped)
404 escaped = false;
405 else if (*e == '\\')
406 escaped = true;
407 else if (strchr(WHITESPACE, *e))
408 break;
409 }
410 *l = e-current;
411 *state = e;
034c6ed7
LP
412 }
413
414 return (char*) current;
415}
416
65d2ebdc
LP
417char **split_path_and_make_absolute(const char *p) {
418 char **l;
419 assert(p);
420
421 if (!(l = strv_split(p, ":")))
422 return NULL;
423
424 if (!strv_path_make_absolute_cwd(l)) {
425 strv_free(l);
426 return NULL;
427 }
428
429 return l;
430}
431
034c6ed7
LP
432int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
433 int r;
434 FILE *f;
435 char fn[132], line[256], *p;
bb00e604 436 long unsigned ppid;
034c6ed7
LP
437
438 assert(pid >= 0);
439 assert(_ppid);
440
bb00e604 441 assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
034c6ed7
LP
442 fn[sizeof(fn)-1] = 0;
443
444 if (!(f = fopen(fn, "r")))
445 return -errno;
446
447 if (!(fgets(line, sizeof(line), f))) {
448 r = -errno;
449 fclose(f);
450 return r;
451 }
452
453 fclose(f);
454
455 /* Let's skip the pid and comm fields. The latter is enclosed
456 * in () but does not escape any () in its value, so let's
457 * skip over it manually */
458
459 if (!(p = strrchr(line, ')')))
460 return -EIO;
461
462 p++;
463
464 if (sscanf(p, " "
465 "%*c " /* state */
bb00e604 466 "%lu ", /* ppid */
034c6ed7
LP
467 &ppid) != 1)
468 return -EIO;
469
bb00e604 470 if ((long unsigned) (pid_t) ppid != ppid)
034c6ed7
LP
471 return -ERANGE;
472
473 *_ppid = (pid_t) ppid;
474
475 return 0;
476}
477
478int write_one_line_file(const char *fn, const char *line) {
479 FILE *f;
480 int r;
481
482 assert(fn);
483 assert(line);
484
485 if (!(f = fopen(fn, "we")))
486 return -errno;
487
488 if (fputs(line, f) < 0) {
489 r = -errno;
490 goto finish;
491 }
492
493 r = 0;
494finish:
495 fclose(f);
496 return r;
497}
498
499int read_one_line_file(const char *fn, char **line) {
500 FILE *f;
501 int r;
11316633 502 char t[2048], *c;
034c6ed7
LP
503
504 assert(fn);
505 assert(line);
506
507 if (!(f = fopen(fn, "re")))
508 return -errno;
509
510 if (!(fgets(t, sizeof(t), f))) {
511 r = -errno;
512 goto finish;
513 }
514
515 if (!(c = strdup(t))) {
516 r = -ENOMEM;
517 goto finish;
518 }
519
520 *line = c;
521 r = 0;
522
523finish:
524 fclose(f);
525 return r;
526}
44d8db9e 527
7072ced8
LP
528char *truncate_nl(char *s) {
529 assert(s);
530
531 s[strcspn(s, NEWLINE)] = 0;
532 return s;
533}
534
535int get_process_name(pid_t pid, char **name) {
536 char *p;
537 int r;
538
539 assert(pid >= 1);
540 assert(name);
541
bb00e604 542 if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
7072ced8
LP
543 return -ENOMEM;
544
545 r = read_one_line_file(p, name);
546 free(p);
547
548 if (r < 0)
549 return r;
550
551 truncate_nl(*name);
552 return 0;
553}
554
c59760ee
LP
555int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
556 char *p, *r, *k;
557 int c;
558 bool space = false;
559 size_t left;
560 FILE *f;
561
562 assert(pid >= 1);
563 assert(max_length > 0);
564 assert(line);
565
566 if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
567 return -ENOMEM;
568
569 f = fopen(p, "r");
570 free(p);
571
572 if (!f)
573 return -errno;
574
575 if (!(r = new(char, max_length))) {
576 fclose(f);
577 return -ENOMEM;
578 }
579
580 k = r;
581 left = max_length;
582 while ((c = getc(f)) != EOF) {
583
584 if (isprint(c)) {
585 if (space) {
586 if (left <= 4)
587 break;
588
589 *(k++) = ' ';
057fbb58 590 left--;
c59760ee
LP
591 space = false;
592 }
593
594 if (left <= 4)
595 break;
596
597 *(k++) = (char) c;
057fbb58 598 left--;
c59760ee
LP
599 } else
600 space = true;
601 }
602
603 if (left <= 4) {
604 size_t n = MIN(left-1, 3U);
605 memcpy(k, "...", n);
606 k[n] = 0;
607 } else
608 *k = 0;
609
610 fclose(f);
611
612 *line = r;
613 return 0;
614}
615
44d8db9e
LP
616char *strappend(const char *s, const char *suffix) {
617 size_t a, b;
618 char *r;
619
620 assert(s);
621 assert(suffix);
622
623 a = strlen(s);
624 b = strlen(suffix);
625
626 if (!(r = new(char, a+b+1)))
627 return NULL;
628
629 memcpy(r, s, a);
630 memcpy(r+a, suffix, b);
631 r[a+b] = 0;
632
633 return r;
634}
87f0e418
LP
635
636int readlink_malloc(const char *p, char **r) {
637 size_t l = 100;
638
639 assert(p);
640 assert(r);
641
642 for (;;) {
643 char *c;
644 ssize_t n;
645
646 if (!(c = new(char, l)))
647 return -ENOMEM;
648
649 if ((n = readlink(p, c, l-1)) < 0) {
650 int ret = -errno;
651 free(c);
652 return ret;
653 }
654
655 if ((size_t) n < l-1) {
656 c[n] = 0;
657 *r = c;
658 return 0;
659 }
660
661 free(c);
662 l *= 2;
663 }
664}
665
2c7108c4
LP
666int readlink_and_make_absolute(const char *p, char **r) {
667 char *target, *k;
668 int j;
669
670 assert(p);
671 assert(r);
672
673 if ((j = readlink_malloc(p, &target)) < 0)
674 return j;
675
676 k = file_in_same_dir(p, target);
677 free(target);
678
679 if (!k)
680 return -ENOMEM;
681
682 *r = k;
683 return 0;
684}
685
87f0e418
LP
686char *file_name_from_path(const char *p) {
687 char *r;
688
689 assert(p);
690
691 if ((r = strrchr(p, '/')))
692 return r + 1;
693
694 return (char*) p;
695}
0301abf4
LP
696
697bool path_is_absolute(const char *p) {
698 assert(p);
699
700 return p[0] == '/';
701}
702
703bool is_path(const char *p) {
704
705 return !!strchr(p, '/');
706}
707
708char *path_make_absolute(const char *p, const char *prefix) {
709 char *r;
710
711 assert(p);
712
65d2ebdc
LP
713 /* Makes every item in the list an absolute path by prepending
714 * the prefix, if specified and necessary */
715
0301abf4
LP
716 if (path_is_absolute(p) || !prefix)
717 return strdup(p);
718
719 if (asprintf(&r, "%s/%s", prefix, p) < 0)
720 return NULL;
721
722 return r;
723}
2a987ee8 724
65d2ebdc
LP
725char *path_make_absolute_cwd(const char *p) {
726 char *cwd, *r;
727
728 assert(p);
729
730 /* Similar to path_make_absolute(), but prefixes with the
731 * current working directory. */
732
733 if (path_is_absolute(p))
734 return strdup(p);
735
736 if (!(cwd = get_current_dir_name()))
737 return NULL;
738
739 r = path_make_absolute(p, cwd);
740 free(cwd);
741
742 return r;
743}
744
745char **strv_path_make_absolute_cwd(char **l) {
746 char **s;
747
748 /* Goes through every item in the string list and makes it
749 * absolute. This works in place and won't rollback any
750 * changes on failure. */
751
752 STRV_FOREACH(s, l) {
753 char *t;
754
755 if (!(t = path_make_absolute_cwd(*s)))
756 return NULL;
757
758 free(*s);
759 *s = t;
760 }
761
762 return l;
763}
764
c3f6d675
LP
765char **strv_path_canonicalize(char **l) {
766 char **s;
767 unsigned k = 0;
768 bool enomem = false;
769
770 if (strv_isempty(l))
771 return l;
772
773 /* Goes through every item in the string list and canonicalize
774 * the path. This works in place and won't rollback any
775 * changes on failure. */
776
777 STRV_FOREACH(s, l) {
778 char *t, *u;
779
780 t = path_make_absolute_cwd(*s);
781 free(*s);
782
783 if (!t) {
784 enomem = true;
785 continue;
786 }
787
788 errno = 0;
789 u = canonicalize_file_name(t);
790 free(t);
791
792 if (!u) {
793 if (errno == ENOMEM || !errno)
794 enomem = true;
795
796 continue;
797 }
798
799 l[k++] = u;
800 }
801
802 l[k] = NULL;
803
804 if (enomem)
805 return NULL;
806
807 return l;
808}
809
2a987ee8
LP
810int reset_all_signal_handlers(void) {
811 int sig;
812
813 for (sig = 1; sig < _NSIG; sig++) {
814 struct sigaction sa;
815
816 if (sig == SIGKILL || sig == SIGSTOP)
817 continue;
818
819 zero(sa);
820 sa.sa_handler = SIG_DFL;
431c32bf 821 sa.sa_flags = SA_RESTART;
2a987ee8
LP
822
823 /* On Linux the first two RT signals are reserved by
824 * glibc, and sigaction() will return EINVAL for them. */
825 if ((sigaction(sig, &sa, NULL) < 0))
826 if (errno != EINVAL)
827 return -errno;
828 }
829
8e274523 830 return 0;
2a987ee8 831}
4a72ff34
LP
832
833char *strstrip(char *s) {
834 char *e, *l = NULL;
835
836 /* Drops trailing whitespace. Modifies the string in
837 * place. Returns pointer to first non-space character */
838
839 s += strspn(s, WHITESPACE);
840
841 for (e = s; *e; e++)
842 if (!strchr(WHITESPACE, *e))
843 l = e;
844
845 if (l)
846 *(l+1) = 0;
847 else
848 *s = 0;
849
850 return s;
4a72ff34
LP
851}
852
ee9b5e01
LP
853char *delete_chars(char *s, const char *bad) {
854 char *f, *t;
855
856 /* Drops all whitespace, regardless where in the string */
857
858 for (f = s, t = s; *f; f++) {
859 if (strchr(bad, *f))
860 continue;
861
862 *(t++) = *f;
863 }
864
865 *t = 0;
866
867 return s;
868}
869
4a72ff34
LP
870char *file_in_same_dir(const char *path, const char *filename) {
871 char *e, *r;
872 size_t k;
873
874 assert(path);
875 assert(filename);
876
877 /* This removes the last component of path and appends
878 * filename, unless the latter is absolute anyway or the
879 * former isn't */
880
881 if (path_is_absolute(filename))
882 return strdup(filename);
883
884 if (!(e = strrchr(path, '/')))
885 return strdup(filename);
886
887 k = strlen(filename);
888 if (!(r = new(char, e-path+1+k+1)))
889 return NULL;
890
891 memcpy(r, path, e-path+1);
892 memcpy(r+(e-path)+1, filename, k+1);
893
894 return r;
895}
fb624d04 896
8c6db833
LP
897int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
898 struct stat st;
899
900 if (mkdir(path, mode) >= 0)
901 if (chmod_and_chown(path, mode, uid, gid) < 0)
902 return -errno;
903
904 if (lstat(path, &st) < 0)
905 return -errno;
906
907 if ((st.st_mode & 0777) != mode ||
908 st.st_uid != uid ||
909 st.st_gid != gid ||
910 !S_ISDIR(st.st_mode)) {
911 errno = EEXIST;
912 return -errno;
913 }
914
915 return 0;
916}
917
918
a9f5d454
LP
919int mkdir_parents(const char *path, mode_t mode) {
920 const char *p, *e;
921
922 assert(path);
923
924 /* Creates every parent directory in the path except the last
925 * component. */
926
927 p = path + strspn(path, "/");
928 for (;;) {
929 int r;
930 char *t;
931
932 e = p + strcspn(p, "/");
933 p = e + strspn(e, "/");
934
935 /* Is this the last component? If so, then we're
936 * done */
937 if (*p == 0)
938 return 0;
939
940 if (!(t = strndup(path, e - path)))
941 return -ENOMEM;
942
943 r = mkdir(t, mode);
944
945 free(t);
946
947 if (r < 0 && errno != EEXIST)
948 return -errno;
949 }
950}
951
bbd67135
LP
952int mkdir_p(const char *path, mode_t mode) {
953 int r;
954
955 /* Like mkdir -p */
956
957 if ((r = mkdir_parents(path, mode)) < 0)
958 return r;
959
960 if (mkdir(path, mode) < 0)
961 return -errno;
962
963 return 0;
964}
965
c32dd69b
LP
966int rmdir_parents(const char *path, const char *stop) {
967 size_t l;
968 int r = 0;
969
970 assert(path);
971 assert(stop);
972
973 l = strlen(path);
974
975 /* Skip trailing slashes */
976 while (l > 0 && path[l-1] == '/')
977 l--;
978
979 while (l > 0) {
980 char *t;
981
982 /* Skip last component */
983 while (l > 0 && path[l-1] != '/')
984 l--;
985
986 /* Skip trailing slashes */
987 while (l > 0 && path[l-1] == '/')
988 l--;
989
990 if (l <= 0)
991 break;
992
993 if (!(t = strndup(path, l)))
994 return -ENOMEM;
995
996 if (path_startswith(stop, t)) {
997 free(t);
998 return 0;
999 }
1000
1001 r = rmdir(t);
1002 free(t);
1003
1004 if (r < 0)
1005 if (errno != ENOENT)
1006 return -errno;
1007 }
1008
1009 return 0;
1010}
1011
1012
fb624d04
LP
1013char hexchar(int x) {
1014 static const char table[16] = "0123456789abcdef";
1015
1016 return table[x & 15];
1017}
4fe88d28
LP
1018
1019int unhexchar(char c) {
1020
1021 if (c >= '0' && c <= '9')
1022 return c - '0';
1023
1024 if (c >= 'a' && c <= 'f')
ea430986 1025 return c - 'a' + 10;
4fe88d28
LP
1026
1027 if (c >= 'A' && c <= 'F')
ea430986 1028 return c - 'A' + 10;
4fe88d28
LP
1029
1030 return -1;
1031}
1032
1033char octchar(int x) {
1034 return '0' + (x & 7);
1035}
1036
1037int unoctchar(char c) {
1038
1039 if (c >= '0' && c <= '7')
1040 return c - '0';
1041
1042 return -1;
1043}
1044
5af98f82
LP
1045char decchar(int x) {
1046 return '0' + (x % 10);
1047}
1048
1049int undecchar(char c) {
1050
1051 if (c >= '0' && c <= '9')
1052 return c - '0';
1053
1054 return -1;
1055}
1056
4fe88d28
LP
1057char *cescape(const char *s) {
1058 char *r, *t;
1059 const char *f;
1060
1061 assert(s);
1062
1063 /* Does C style string escaping. */
1064
1065 if (!(r = new(char, strlen(s)*4 + 1)))
1066 return NULL;
1067
1068 for (f = s, t = r; *f; f++)
1069
1070 switch (*f) {
1071
1072 case '\a':
1073 *(t++) = '\\';
1074 *(t++) = 'a';
1075 break;
1076 case '\b':
1077 *(t++) = '\\';
1078 *(t++) = 'b';
1079 break;
1080 case '\f':
1081 *(t++) = '\\';
1082 *(t++) = 'f';
1083 break;
1084 case '\n':
1085 *(t++) = '\\';
1086 *(t++) = 'n';
1087 break;
1088 case '\r':
1089 *(t++) = '\\';
1090 *(t++) = 'r';
1091 break;
1092 case '\t':
1093 *(t++) = '\\';
1094 *(t++) = 't';
1095 break;
1096 case '\v':
1097 *(t++) = '\\';
1098 *(t++) = 'v';
1099 break;
1100 case '\\':
1101 *(t++) = '\\';
1102 *(t++) = '\\';
1103 break;
1104 case '"':
1105 *(t++) = '\\';
1106 *(t++) = '"';
1107 break;
1108 case '\'':
1109 *(t++) = '\\';
1110 *(t++) = '\'';
1111 break;
1112
1113 default:
1114 /* For special chars we prefer octal over
1115 * hexadecimal encoding, simply because glib's
1116 * g_strescape() does the same */
1117 if ((*f < ' ') || (*f >= 127)) {
1118 *(t++) = '\\';
1119 *(t++) = octchar((unsigned char) *f >> 6);
1120 *(t++) = octchar((unsigned char) *f >> 3);
1121 *(t++) = octchar((unsigned char) *f);
1122 } else
1123 *(t++) = *f;
1124 break;
1125 }
1126
1127 *t = 0;
1128
1129 return r;
1130}
1131
6febfd0d 1132char *cunescape_length(const char *s, size_t length) {
4fe88d28
LP
1133 char *r, *t;
1134 const char *f;
1135
1136 assert(s);
1137
1138 /* Undoes C style string escaping */
1139
6febfd0d 1140 if (!(r = new(char, length+1)))
4fe88d28
LP
1141 return r;
1142
6febfd0d 1143 for (f = s, t = r; f < s + length; f++) {
4fe88d28
LP
1144
1145 if (*f != '\\') {
1146 *(t++) = *f;
1147 continue;
1148 }
1149
1150 f++;
1151
1152 switch (*f) {
1153
1154 case 'a':
1155 *(t++) = '\a';
1156 break;
1157 case 'b':
1158 *(t++) = '\b';
1159 break;
1160 case 'f':
1161 *(t++) = '\f';
1162 break;
1163 case 'n':
1164 *(t++) = '\n';
1165 break;
1166 case 'r':
1167 *(t++) = '\r';
1168 break;
1169 case 't':
1170 *(t++) = '\t';
1171 break;
1172 case 'v':
1173 *(t++) = '\v';
1174 break;
1175 case '\\':
1176 *(t++) = '\\';
1177 break;
1178 case '"':
1179 *(t++) = '"';
1180 break;
1181 case '\'':
1182 *(t++) = '\'';
1183 break;
1184
1185 case 'x': {
1186 /* hexadecimal encoding */
1187 int a, b;
1188
1189 if ((a = unhexchar(f[1])) < 0 ||
1190 (b = unhexchar(f[2])) < 0) {
1191 /* Invalid escape code, let's take it literal then */
1192 *(t++) = '\\';
1193 *(t++) = 'x';
1194 } else {
1195 *(t++) = (char) ((a << 4) | b);
1196 f += 2;
1197 }
1198
1199 break;
1200 }
1201
1202 case '0':
1203 case '1':
1204 case '2':
1205 case '3':
1206 case '4':
1207 case '5':
1208 case '6':
1209 case '7': {
1210 /* octal encoding */
1211 int a, b, c;
1212
1213 if ((a = unoctchar(f[0])) < 0 ||
1214 (b = unoctchar(f[1])) < 0 ||
1215 (c = unoctchar(f[2])) < 0) {
1216 /* Invalid escape code, let's take it literal then */
1217 *(t++) = '\\';
1218 *(t++) = f[0];
1219 } else {
1220 *(t++) = (char) ((a << 6) | (b << 3) | c);
1221 f += 2;
1222 }
1223
1224 break;
1225 }
1226
1227 case 0:
1228 /* premature end of string.*/
1229 *(t++) = '\\';
1230 goto finish;
1231
1232 default:
1233 /* Invalid escape code, let's take it literal then */
1234 *(t++) = '\\';
f3d4cc01 1235 *(t++) = *f;
4fe88d28
LP
1236 break;
1237 }
1238 }
1239
1240finish:
1241 *t = 0;
1242 return r;
1243}
1244
6febfd0d
LP
1245char *cunescape(const char *s) {
1246 return cunescape_length(s, strlen(s));
1247}
4fe88d28
LP
1248
1249char *xescape(const char *s, const char *bad) {
1250 char *r, *t;
1251 const char *f;
1252
1253 /* Escapes all chars in bad, in addition to \ and all special
1254 * chars, in \xFF style escaping. May be reversed with
1255 * cunescape. */
1256
1257 if (!(r = new(char, strlen(s)*4+1)))
1258 return NULL;
1259
1260 for (f = s, t = r; *f; f++) {
1261
b866264a
LP
1262 if ((*f < ' ') || (*f >= 127) ||
1263 (*f == '\\') || strchr(bad, *f)) {
4fe88d28
LP
1264 *(t++) = '\\';
1265 *(t++) = 'x';
1266 *(t++) = hexchar(*f >> 4);
1267 *(t++) = hexchar(*f);
1268 } else
1269 *(t++) = *f;
1270 }
1271
1272 *t = 0;
1273
1274 return r;
1275}
1276
ea430986 1277char *bus_path_escape(const char *s) {
ea430986
LP
1278 char *r, *t;
1279 const char *f;
1280
47be870b
LP
1281 assert(s);
1282
ea430986
LP
1283 /* Escapes all chars that D-Bus' object path cannot deal
1284 * with. Can be reverse with bus_path_unescape() */
1285
1286 if (!(r = new(char, strlen(s)*3+1)))
1287 return NULL;
1288
1289 for (f = s, t = r; *f; f++) {
1290
1291 if (!(*f >= 'A' && *f <= 'Z') &&
1292 !(*f >= 'a' && *f <= 'z') &&
1293 !(*f >= '0' && *f <= '9')) {
1294 *(t++) = '_';
1295 *(t++) = hexchar(*f >> 4);
1296 *(t++) = hexchar(*f);
1297 } else
1298 *(t++) = *f;
1299 }
1300
1301 *t = 0;
1302
1303 return r;
1304}
1305
9e2f7c11 1306char *bus_path_unescape(const char *f) {
ea430986 1307 char *r, *t;
ea430986 1308
9e2f7c11 1309 assert(f);
47be870b 1310
9e2f7c11 1311 if (!(r = strdup(f)))
ea430986
LP
1312 return NULL;
1313
9e2f7c11 1314 for (t = r; *f; f++) {
ea430986
LP
1315
1316 if (*f == '_') {
1317 int a, b;
1318
1319 if ((a = unhexchar(f[1])) < 0 ||
1320 (b = unhexchar(f[2])) < 0) {
1321 /* Invalid escape code, let's take it literal then */
1322 *(t++) = '_';
1323 } else {
1324 *(t++) = (char) ((a << 4) | b);
1325 f += 2;
1326 }
1327 } else
1328 *(t++) = *f;
1329 }
1330
1331 *t = 0;
1332
1333 return r;
1334}
1335
4fe88d28
LP
1336char *path_kill_slashes(char *path) {
1337 char *f, *t;
1338 bool slash = false;
1339
1340 /* Removes redundant inner and trailing slashes. Modifies the
1341 * passed string in-place.
1342 *
1343 * ///foo///bar/ becomes /foo/bar
1344 */
1345
1346 for (f = path, t = path; *f; f++) {
1347
1348 if (*f == '/') {
1349 slash = true;
1350 continue;
1351 }
1352
1353 if (slash) {
1354 slash = false;
1355 *(t++) = '/';
1356 }
1357
1358 *(t++) = *f;
1359 }
1360
1361 /* Special rule, if we are talking of the root directory, a
1362 trailing slash is good */
1363
1364 if (t == path && slash)
1365 *(t++) = '/';
1366
1367 *t = 0;
1368 return path;
1369}
1370
1371bool path_startswith(const char *path, const char *prefix) {
1372 assert(path);
1373 assert(prefix);
1374
1375 if ((path[0] == '/') != (prefix[0] == '/'))
1376 return false;
1377
1378 for (;;) {
1379 size_t a, b;
1380
1381 path += strspn(path, "/");
1382 prefix += strspn(prefix, "/");
1383
1384 if (*prefix == 0)
1385 return true;
1386
1387 if (*path == 0)
1388 return false;
1389
1390 a = strcspn(path, "/");
1391 b = strcspn(prefix, "/");
1392
1393 if (a != b)
1394 return false;
1395
1396 if (memcmp(path, prefix, a) != 0)
1397 return false;
1398
1399 path += a;
1400 prefix += b;
1401 }
1402}
1403
15ae422b
LP
1404bool path_equal(const char *a, const char *b) {
1405 assert(a);
1406 assert(b);
1407
1408 if ((a[0] == '/') != (b[0] == '/'))
1409 return false;
1410
1411 for (;;) {
1412 size_t j, k;
1413
1414 a += strspn(a, "/");
1415 b += strspn(b, "/");
1416
1417 if (*a == 0 && *b == 0)
1418 return true;
1419
1420 if (*a == 0 || *b == 0)
1421 return false;
1422
1423 j = strcspn(a, "/");
1424 k = strcspn(b, "/");
1425
1426 if (j != k)
1427 return false;
1428
1429 if (memcmp(a, b, j) != 0)
1430 return false;
1431
1432 a += j;
1433 b += k;
1434 }
1435}
1436
67d51650 1437char *ascii_strlower(char *t) {
4fe88d28
LP
1438 char *p;
1439
67d51650 1440 assert(t);
4fe88d28 1441
67d51650 1442 for (p = t; *p; p++)
4fe88d28
LP
1443 if (*p >= 'A' && *p <= 'Z')
1444 *p = *p - 'A' + 'a';
1445
67d51650 1446 return t;
4fe88d28 1447}
1dccbe19 1448
c85dc17b
LP
1449bool ignore_file(const char *filename) {
1450 assert(filename);
1451
1452 return
1453 filename[0] == '.' ||
6c78be3c 1454 streq(filename, "lost+found") ||
c85dc17b
LP
1455 endswith(filename, "~") ||
1456 endswith(filename, ".rpmnew") ||
1457 endswith(filename, ".rpmsave") ||
1458 endswith(filename, ".rpmorig") ||
1459 endswith(filename, ".dpkg-old") ||
1460 endswith(filename, ".dpkg-new") ||
1461 endswith(filename, ".swp");
1462}
1463
3a0ecb08
LP
1464int fd_nonblock(int fd, bool nonblock) {
1465 int flags;
1466
1467 assert(fd >= 0);
1468
1469 if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
1470 return -errno;
1471
1472 if (nonblock)
1473 flags |= O_NONBLOCK;
1474 else
1475 flags &= ~O_NONBLOCK;
1476
1477 if (fcntl(fd, F_SETFL, flags) < 0)
1478 return -errno;
1479
1480 return 0;
1481}
1482
1483int fd_cloexec(int fd, bool cloexec) {
1484 int flags;
1485
1486 assert(fd >= 0);
1487
1488 if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
1489 return -errno;
1490
1491 if (cloexec)
1492 flags |= FD_CLOEXEC;
1493 else
1494 flags &= ~FD_CLOEXEC;
1495
1496 if (fcntl(fd, F_SETFD, flags) < 0)
1497 return -errno;
1498
1499 return 0;
1500}
1501
a0d40ac5
LP
1502int close_all_fds(const int except[], unsigned n_except) {
1503 DIR *d;
1504 struct dirent *de;
1505 int r = 0;
1506
1507 if (!(d = opendir("/proc/self/fd")))
1508 return -errno;
1509
1510 while ((de = readdir(d))) {
a7610064 1511 int fd = -1;
a0d40ac5 1512
a16e1123 1513 if (ignore_file(de->d_name))
a0d40ac5
LP
1514 continue;
1515
1516 if ((r = safe_atoi(de->d_name, &fd)) < 0)
1517 goto finish;
1518
1519 if (fd < 3)
1520 continue;
1521
1522 if (fd == dirfd(d))
1523 continue;
1524
1525 if (except) {
1526 bool found;
1527 unsigned i;
1528
1529 found = false;
1530 for (i = 0; i < n_except; i++)
1531 if (except[i] == fd) {
1532 found = true;
1533 break;
1534 }
1535
1536 if (found)
1537 continue;
1538 }
1539
2f357920
LP
1540 if ((r = close_nointr(fd)) < 0) {
1541 /* Valgrind has its own FD and doesn't want to have it closed */
1542 if (errno != EBADF)
1543 goto finish;
1544 }
a0d40ac5
LP
1545 }
1546
2f357920
LP
1547 r = 0;
1548
a0d40ac5
LP
1549finish:
1550 closedir(d);
1551 return r;
1552}
1553
db12775d
LP
1554bool chars_intersect(const char *a, const char *b) {
1555 const char *p;
1556
1557 /* Returns true if any of the chars in a are in b. */
1558 for (p = a; *p; p++)
1559 if (strchr(b, *p))
1560 return true;
1561
1562 return false;
1563}
1564
8b6c7120
LP
1565char *format_timestamp(char *buf, size_t l, usec_t t) {
1566 struct tm tm;
1567 time_t sec;
1568
1569 assert(buf);
1570 assert(l > 0);
1571
1572 if (t <= 0)
1573 return NULL;
1574
f872ec33 1575 sec = (time_t) (t / USEC_PER_SEC);
8b6c7120
LP
1576
1577 if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0)
1578 return NULL;
1579
1580 return buf;
1581}
1582
871d7de4
LP
1583char *format_timespan(char *buf, size_t l, usec_t t) {
1584 static const struct {
1585 const char *suffix;
1586 usec_t usec;
1587 } table[] = {
1588 { "w", USEC_PER_WEEK },
1589 { "d", USEC_PER_DAY },
1590 { "h", USEC_PER_HOUR },
1591 { "min", USEC_PER_MINUTE },
1592 { "s", USEC_PER_SEC },
1593 { "ms", USEC_PER_MSEC },
1594 { "us", 1 },
1595 };
1596
1597 unsigned i;
1598 char *p = buf;
1599
1600 assert(buf);
1601 assert(l > 0);
1602
1603 if (t == (usec_t) -1)
1604 return NULL;
1605
1606 /* The result of this function can be parsed with parse_usec */
1607
1608 for (i = 0; i < ELEMENTSOF(table); i++) {
1609 int k;
1610 size_t n;
1611
1612 if (t < table[i].usec)
1613 continue;
1614
1615 if (l <= 1)
1616 break;
1617
1618 k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix);
1619 n = MIN((size_t) k, l);
1620
1621 l -= n;
1622 p += n;
1623
1624 t %= table[i].usec;
1625 }
1626
1627 *p = 0;
1628
1629 return buf;
1630}
1631
42856c10
LP
1632bool fstype_is_network(const char *fstype) {
1633 static const char * const table[] = {
1634 "cifs",
1635 "smbfs",
1636 "ncpfs",
1637 "nfs",
ca139f94
LP
1638 "nfs4",
1639 "gfs",
1640 "gfs2"
42856c10
LP
1641 };
1642
1643 unsigned i;
1644
1645 for (i = 0; i < ELEMENTSOF(table); i++)
1646 if (streq(table[i], fstype))
1647 return true;
1648
1649 return false;
1650}
1651
601f6a1e
LP
1652int chvt(int vt) {
1653 int fd, r = 0;
1654
1655 if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
1656 return -errno;
1657
1658 if (vt < 0) {
1659 int tiocl[2] = {
1660 TIOCL_GETKMSGREDIRECT,
1661 0
1662 };
1663
1664 if (ioctl(fd, TIOCLINUX, tiocl) < 0)
1665 return -errno;
1666
1667 vt = tiocl[0] <= 0 ? 1 : tiocl[0];
1668 }
1669
1670 if (ioctl(fd, VT_ACTIVATE, vt) < 0)
1671 r = -errno;
1672
a16e1123 1673 close_nointr_nofail(r);
601f6a1e
LP
1674 return r;
1675}
1676
80876c20
LP
1677int read_one_char(FILE *f, char *ret, bool *need_nl) {
1678 struct termios old_termios, new_termios;
1679 char c;
1680 char line[1024];
1681
1682 assert(f);
1683 assert(ret);
1684
1685 if (tcgetattr(fileno(f), &old_termios) >= 0) {
1686 new_termios = old_termios;
1687
1688 new_termios.c_lflag &= ~ICANON;
1689 new_termios.c_cc[VMIN] = 1;
1690 new_termios.c_cc[VTIME] = 0;
1691
1692 if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
1693 size_t k;
1694
1695 k = fread(&c, 1, 1, f);
1696
1697 tcsetattr(fileno(f), TCSADRAIN, &old_termios);
1698
1699 if (k <= 0)
1700 return -EIO;
1701
1702 if (need_nl)
1703 *need_nl = c != '\n';
1704
1705 *ret = c;
1706 return 0;
1707 }
1708 }
1709
1710 if (!(fgets(line, sizeof(line), f)))
1711 return -EIO;
1712
1713 truncate_nl(line);
1714
1715 if (strlen(line) != 1)
1716 return -EBADMSG;
1717
1718 if (need_nl)
1719 *need_nl = false;
1720
1721 *ret = line[0];
1722 return 0;
1723}
1724
1725int ask(char *ret, const char *replies, const char *text, ...) {
1726 assert(ret);
1727 assert(replies);
1728 assert(text);
1729
1730 for (;;) {
1731 va_list ap;
1732 char c;
1733 int r;
1734 bool need_nl = true;
1735
b1b2dc0c
LP
1736 fputs("\x1B[1m", stdout);
1737
80876c20
LP
1738 va_start(ap, text);
1739 vprintf(text, ap);
1740 va_end(ap);
1741
b1b2dc0c
LP
1742 fputs("\x1B[0m", stdout);
1743
80876c20
LP
1744 fflush(stdout);
1745
1746 if ((r = read_one_char(stdin, &c, &need_nl)) < 0) {
1747
1748 if (r == -EBADMSG) {
1749 puts("Bad input, please try again.");
1750 continue;
1751 }
1752
1753 putchar('\n');
1754 return r;
1755 }
1756
1757 if (need_nl)
1758 putchar('\n');
1759
1760 if (strchr(replies, c)) {
1761 *ret = c;
1762 return 0;
1763 }
1764
1765 puts("Read unexpected character, please try again.");
1766 }
1767}
1768
1769int reset_terminal(int fd) {
1770 struct termios termios;
1771 int r = 0;
1772
1773 assert(fd >= 0);
1774
aaf694ca 1775 /* Set terminal to some sane defaults */
80876c20
LP
1776
1777 if (tcgetattr(fd, &termios) < 0) {
1778 r = -errno;
1779 goto finish;
1780 }
1781
aaf694ca
LP
1782 /* We only reset the stuff that matters to the software. How
1783 * hardware is set up we don't touch assuming that somebody
1784 * else will do that for us */
1785
1786 termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
80876c20
LP
1787 termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
1788 termios.c_oflag |= ONLCR;
1789 termios.c_cflag |= CREAD;
1790 termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
1791
1792 termios.c_cc[VINTR] = 03; /* ^C */
1793 termios.c_cc[VQUIT] = 034; /* ^\ */
1794 termios.c_cc[VERASE] = 0177;
1795 termios.c_cc[VKILL] = 025; /* ^X */
1796 termios.c_cc[VEOF] = 04; /* ^D */
1797 termios.c_cc[VSTART] = 021; /* ^Q */
1798 termios.c_cc[VSTOP] = 023; /* ^S */
1799 termios.c_cc[VSUSP] = 032; /* ^Z */
1800 termios.c_cc[VLNEXT] = 026; /* ^V */
1801 termios.c_cc[VWERASE] = 027; /* ^W */
1802 termios.c_cc[VREPRINT] = 022; /* ^R */
aaf694ca
LP
1803 termios.c_cc[VEOL] = 0;
1804 termios.c_cc[VEOL2] = 0;
80876c20
LP
1805
1806 termios.c_cc[VTIME] = 0;
1807 termios.c_cc[VMIN] = 1;
1808
1809 if (tcsetattr(fd, TCSANOW, &termios) < 0)
1810 r = -errno;
1811
1812finish:
1813 /* Just in case, flush all crap out */
1814 tcflush(fd, TCIOFLUSH);
1815
1816 return r;
1817}
1818
1819int open_terminal(const char *name, int mode) {
1820 int fd, r;
1821
1822 if ((fd = open(name, mode)) < 0)
1823 return -errno;
1824
1825 if ((r = isatty(fd)) < 0) {
1826 close_nointr_nofail(fd);
1827 return -errno;
1828 }
1829
1830 if (!r) {
1831 close_nointr_nofail(fd);
1832 return -ENOTTY;
1833 }
1834
1835 return fd;
1836}
1837
1838int flush_fd(int fd) {
1839 struct pollfd pollfd;
1840
1841 zero(pollfd);
1842 pollfd.fd = fd;
1843 pollfd.events = POLLIN;
1844
1845 for (;;) {
1846 char buf[1024];
1847 ssize_t l;
1848 int r;
1849
1850 if ((r = poll(&pollfd, 1, 0)) < 0) {
1851
1852 if (errno == EINTR)
1853 continue;
1854
1855 return -errno;
1856 }
1857
1858 if (r == 0)
1859 return 0;
1860
1861 if ((l = read(fd, buf, sizeof(buf))) < 0) {
1862
1863 if (errno == EINTR)
1864 continue;
1865
1866 if (errno == EAGAIN)
1867 return 0;
1868
1869 return -errno;
1870 }
1871
1872 if (l <= 0)
1873 return 0;
1874 }
1875}
1876
21de3988 1877int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) {
bab45044 1878 int fd = -1, notify = -1, r, wd = -1;
80876c20
LP
1879
1880 assert(name);
1881
1882 /* We use inotify to be notified when the tty is closed. We
1883 * create the watch before checking if we can actually acquire
1884 * it, so that we don't lose any event.
1885 *
1886 * Note: strictly speaking this actually watches for the
1887 * device being closed, it does *not* really watch whether a
1888 * tty loses its controlling process. However, unless some
1889 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
1890 * its tty otherwise this will not become a problem. As long
1891 * as the administrator makes sure not configure any service
1892 * on the same tty as an untrusted user this should not be a
1893 * problem. (Which he probably should not do anyway.) */
1894
1895 if (!fail && !force) {
1896 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
1897 r = -errno;
1898 goto fail;
1899 }
1900
1901 if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) {
1902 r = -errno;
1903 goto fail;
1904 }
1905 }
1906
1907 for (;;) {
e3d1855b
LP
1908 if (notify >= 0)
1909 if ((r = flush_fd(notify)) < 0)
1910 goto fail;
80876c20
LP
1911
1912 /* We pass here O_NOCTTY only so that we can check the return
1913 * value TIOCSCTTY and have a reliable way to figure out if we
1914 * successfully became the controlling process of the tty */
1915 if ((fd = open_terminal(name, O_RDWR|O_NOCTTY)) < 0)
1916 return -errno;
1917
1918 /* First, try to get the tty */
21de3988
LP
1919 r = ioctl(fd, TIOCSCTTY, force);
1920
1921 /* Sometimes it makes sense to ignore TIOCSCTTY
1922 * returning EPERM, i.e. when very likely we already
1923 * are have this controlling terminal. */
1924 if (r < 0 && errno == EPERM && ignore_tiocstty_eperm)
1925 r = 0;
1926
1927 if (r < 0 && (force || fail || errno != EPERM)) {
80876c20
LP
1928 r = -errno;
1929 goto fail;
1930 }
1931
1932 if (r >= 0)
1933 break;
1934
1935 assert(!fail);
1936 assert(!force);
1937 assert(notify >= 0);
1938
1939 for (;;) {
1940 struct inotify_event e;
1941 ssize_t l;
1942
1943 if ((l = read(notify, &e, sizeof(e))) != sizeof(e)) {
1944
1945 if (l < 0) {
1946
1947 if (errno == EINTR)
1948 continue;
1949
1950 r = -errno;
1951 } else
1952 r = -EIO;
1953
1954 goto fail;
1955 }
1956
1957 if (e.wd != wd || !(e.mask & IN_CLOSE)) {
1958 r = -errno;
1959 goto fail;
1960 }
1961
1962 break;
1963 }
1964
1965 /* We close the tty fd here since if the old session
1966 * ended our handle will be dead. It's important that
1967 * we do this after sleeping, so that we don't enter
1968 * an endless loop. */
1969 close_nointr_nofail(fd);
1970 }
1971
1972 if (notify >= 0)
a16e1123 1973 close_nointr_nofail(notify);
80876c20
LP
1974
1975 if ((r = reset_terminal(fd)) < 0)
1976 log_warning("Failed to reset terminal: %s", strerror(-r));
1977
1978 return fd;
1979
1980fail:
1981 if (fd >= 0)
a16e1123 1982 close_nointr_nofail(fd);
80876c20
LP
1983
1984 if (notify >= 0)
a16e1123 1985 close_nointr_nofail(notify);
80876c20
LP
1986
1987 return r;
1988}
1989
1990int release_terminal(void) {
1991 int r = 0, fd;
57cd2192 1992 struct sigaction sa_old, sa_new;
80876c20 1993
57cd2192 1994 if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
80876c20
LP
1995 return -errno;
1996
57cd2192
LP
1997 /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
1998 * by our own TIOCNOTTY */
1999
2000 zero(sa_new);
2001 sa_new.sa_handler = SIG_IGN;
2002 sa_new.sa_flags = SA_RESTART;
2003 assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
2004
80876c20
LP
2005 if (ioctl(fd, TIOCNOTTY) < 0)
2006 r = -errno;
2007
57cd2192
LP
2008 assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
2009
80876c20
LP
2010 close_nointr_nofail(fd);
2011 return r;
2012}
2013
9a34ec5f
LP
2014int sigaction_many(const struct sigaction *sa, ...) {
2015 va_list ap;
2016 int r = 0, sig;
2017
2018 va_start(ap, sa);
2019 while ((sig = va_arg(ap, int)) > 0)
2020 if (sigaction(sig, sa, NULL) < 0)
2021 r = -errno;
2022 va_end(ap);
2023
2024 return r;
2025}
2026
2027int ignore_signals(int sig, ...) {
a337c6fc 2028 struct sigaction sa;
9a34ec5f
LP
2029 va_list ap;
2030 int r = 0;
a337c6fc
LP
2031
2032 zero(sa);
2033 sa.sa_handler = SIG_IGN;
2034 sa.sa_flags = SA_RESTART;
2035
9a34ec5f
LP
2036 if (sigaction(sig, &sa, NULL) < 0)
2037 r = -errno;
2038
2039 va_start(ap, sig);
2040 while ((sig = va_arg(ap, int)) > 0)
2041 if (sigaction(sig, &sa, NULL) < 0)
2042 r = -errno;
2043 va_end(ap);
2044
2045 return r;
2046}
2047
2048int default_signals(int sig, ...) {
2049 struct sigaction sa;
2050 va_list ap;
2051 int r = 0;
2052
2053 zero(sa);
2054 sa.sa_handler = SIG_DFL;
2055 sa.sa_flags = SA_RESTART;
2056
2057 if (sigaction(sig, &sa, NULL) < 0)
2058 r = -errno;
2059
2060 va_start(ap, sig);
2061 while ((sig = va_arg(ap, int)) > 0)
2062 if (sigaction(sig, &sa, NULL) < 0)
2063 r = -errno;
2064 va_end(ap);
2065
2066 return r;
a337c6fc
LP
2067}
2068
8d567588
LP
2069int close_pipe(int p[]) {
2070 int a = 0, b = 0;
2071
2072 assert(p);
2073
2074 if (p[0] >= 0) {
2075 a = close_nointr(p[0]);
2076 p[0] = -1;
2077 }
2078
2079 if (p[1] >= 0) {
2080 b = close_nointr(p[1]);
2081 p[1] = -1;
2082 }
2083
2084 return a < 0 ? a : b;
2085}
2086
eb22ac37 2087ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
8d567588
LP
2088 uint8_t *p;
2089 ssize_t n = 0;
2090
2091 assert(fd >= 0);
2092 assert(buf);
2093
2094 p = buf;
2095
2096 while (nbytes > 0) {
2097 ssize_t k;
2098
2099 if ((k = read(fd, p, nbytes)) <= 0) {
2100
eb22ac37 2101 if (k < 0 && errno == EINTR)
8d567588
LP
2102 continue;
2103
eb22ac37 2104 if (k < 0 && errno == EAGAIN && do_poll) {
8d567588
LP
2105 struct pollfd pollfd;
2106
2107 zero(pollfd);
2108 pollfd.fd = fd;
2109 pollfd.events = POLLIN;
2110
2111 if (poll(&pollfd, 1, -1) < 0) {
2112 if (errno == EINTR)
2113 continue;
2114
2115 return n > 0 ? n : -errno;
2116 }
2117
2118 if (pollfd.revents != POLLIN)
2119 return n > 0 ? n : -EIO;
2120
2121 continue;
2122 }
2123
2124 return n > 0 ? n : (k < 0 ? -errno : 0);
2125 }
2126
2127 p += k;
2128 nbytes -= k;
2129 n += k;
2130 }
2131
2132 return n;
2133}
2134
eb22ac37
LP
2135ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
2136 const uint8_t *p;
2137 ssize_t n = 0;
2138
2139 assert(fd >= 0);
2140 assert(buf);
2141
2142 p = buf;
2143
2144 while (nbytes > 0) {
2145 ssize_t k;
2146
2147 if ((k = write(fd, p, nbytes)) <= 0) {
2148
2149 if (k < 0 && errno == EINTR)
2150 continue;
2151
2152 if (k < 0 && errno == EAGAIN && do_poll) {
2153 struct pollfd pollfd;
2154
2155 zero(pollfd);
2156 pollfd.fd = fd;
2157 pollfd.events = POLLOUT;
2158
2159 if (poll(&pollfd, 1, -1) < 0) {
2160 if (errno == EINTR)
2161 continue;
2162
2163 return n > 0 ? n : -errno;
2164 }
2165
2166 if (pollfd.revents != POLLOUT)
2167 return n > 0 ? n : -EIO;
2168
2169 continue;
2170 }
2171
2172 return n > 0 ? n : (k < 0 ? -errno : 0);
2173 }
2174
2175 p += k;
2176 nbytes -= k;
2177 n += k;
2178 }
2179
2180 return n;
2181}
2182
8d567588
LP
2183int path_is_mount_point(const char *t) {
2184 struct stat a, b;
2185 char *copy;
2186
2187 if (lstat(t, &a) < 0) {
2188
2189 if (errno == ENOENT)
2190 return 0;
2191
2192 return -errno;
2193 }
2194
2195 if (!(copy = strdup(t)))
2196 return -ENOMEM;
2197
2198 if (lstat(dirname(copy), &b) < 0) {
2199 free(copy);
2200 return -errno;
2201 }
2202
2203 free(copy);
2204
2205 return a.st_dev != b.st_dev;
2206}
2207
24a6e4a4
LP
2208int parse_usec(const char *t, usec_t *usec) {
2209 static const struct {
2210 const char *suffix;
2211 usec_t usec;
2212 } table[] = {
2213 { "sec", USEC_PER_SEC },
2214 { "s", USEC_PER_SEC },
2215 { "min", USEC_PER_MINUTE },
2216 { "hr", USEC_PER_HOUR },
2217 { "h", USEC_PER_HOUR },
2218 { "d", USEC_PER_DAY },
2219 { "w", USEC_PER_WEEK },
2220 { "msec", USEC_PER_MSEC },
2221 { "ms", USEC_PER_MSEC },
2222 { "m", USEC_PER_MINUTE },
2223 { "usec", 1ULL },
2224 { "us", 1ULL },
2225 { "", USEC_PER_SEC },
2226 };
2227
2228 const char *p;
2229 usec_t r = 0;
2230
2231 assert(t);
2232 assert(usec);
2233
2234 p = t;
2235 do {
2236 long long l;
2237 char *e;
2238 unsigned i;
2239
2240 errno = 0;
2241 l = strtoll(p, &e, 10);
2242
2243 if (errno != 0)
2244 return -errno;
2245
2246 if (l < 0)
2247 return -ERANGE;
2248
2249 if (e == p)
2250 return -EINVAL;
2251
2252 e += strspn(e, WHITESPACE);
2253
2254 for (i = 0; i < ELEMENTSOF(table); i++)
2255 if (startswith(e, table[i].suffix)) {
2256 r += (usec_t) l * table[i].usec;
2257 p = e + strlen(table[i].suffix);
2258 break;
2259 }
2260
2261 if (i >= ELEMENTSOF(table))
2262 return -EINVAL;
2263
2264 } while (*p != 0);
2265
2266 *usec = r;
2267
2268 return 0;
2269}
2270
843d2643
LP
2271int make_stdio(int fd) {
2272 int r, s, t;
2273
2274 assert(fd >= 0);
2275
2276 r = dup2(fd, STDIN_FILENO);
2277 s = dup2(fd, STDOUT_FILENO);
2278 t = dup2(fd, STDERR_FILENO);
2279
2280 if (fd >= 3)
2281 close_nointr_nofail(fd);
2282
2283 if (r < 0 || s < 0 || t < 0)
2284 return -errno;
2285
2286 return 0;
2287}
2288
cb8a8f78
LP
2289bool is_clean_exit(int code, int status) {
2290
2291 if (code == CLD_EXITED)
2292 return status == 0;
2293
2294 /* If a daemon does not implement handlers for some of the
2295 * signals that's not considered an unclean shutdown */
2296 if (code == CLD_KILLED)
2297 return
2298 status == SIGHUP ||
2299 status == SIGINT ||
2300 status == SIGTERM ||
2301 status == SIGPIPE;
2302
2303 return false;
2304}
2305
8407a5d0
LP
2306bool is_device_path(const char *path) {
2307
2308 /* Returns true on paths that refer to a device, either in
2309 * sysfs or in /dev */
2310
2311 return
2312 path_startswith(path, "/dev/") ||
2313 path_startswith(path, "/sys/");
2314}
2315
01f78473
LP
2316int dir_is_empty(const char *path) {
2317 DIR *d;
2318 int r;
2319 struct dirent buf, *de;
2320
2321 if (!(d = opendir(path)))
2322 return -errno;
2323
2324 for (;;) {
2325 if ((r = readdir_r(d, &buf, &de)) > 0) {
2326 r = -r;
2327 break;
2328 }
2329
2330 if (!de) {
2331 r = 1;
2332 break;
2333 }
2334
2335 if (!ignore_file(de->d_name)) {
2336 r = 0;
2337 break;
2338 }
2339 }
2340
2341 closedir(d);
2342 return r;
2343}
2344
d3782d60
LP
2345unsigned long long random_ull(void) {
2346 int fd;
2347 uint64_t ull;
2348 ssize_t r;
2349
2350 if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0)
2351 goto fallback;
2352
eb22ac37 2353 r = loop_read(fd, &ull, sizeof(ull), true);
d3782d60
LP
2354 close_nointr_nofail(fd);
2355
2356 if (r != sizeof(ull))
2357 goto fallback;
2358
2359 return ull;
2360
2361fallback:
2362 return random() * RAND_MAX + random();
2363}
2364
5b6319dc
LP
2365void rename_process(const char name[8]) {
2366 assert(name);
2367
2368 prctl(PR_SET_NAME, name);
2369
2370 /* This is a like a poor man's setproctitle(). The string
2371 * passed should fit in 7 chars (i.e. the length of
2372 * "systemd") */
2373
2374 if (program_invocation_name)
2375 strncpy(program_invocation_name, name, strlen(program_invocation_name));
2376}
2377
7d793605
LP
2378void sigset_add_many(sigset_t *ss, ...) {
2379 va_list ap;
2380 int sig;
2381
2382 assert(ss);
2383
2384 va_start(ap, ss);
2385 while ((sig = va_arg(ap, int)) > 0)
2386 assert_se(sigaddset(ss, sig) == 0);
2387 va_end(ap);
2388}
2389
ef2f1067
LP
2390char* gethostname_malloc(void) {
2391 struct utsname u;
2392
2393 assert_se(uname(&u) >= 0);
2394
2395 if (u.nodename[0])
2396 return strdup(u.nodename);
2397
2398 return strdup(u.sysname);
2399}
2400
8c6db833
LP
2401int getmachineid_malloc(char **b) {
2402 int r;
2403
2404 assert(b);
2405
2406 if ((r = read_one_line_file("/var/lib/dbus/machine-id", b)) < 0)
2407 return r;
2408
2409 strstrip(*b);
2410 return 0;
2411}
2412
ef2f1067
LP
2413char* getlogname_malloc(void) {
2414 uid_t uid;
2415 long bufsize;
2416 char *buf, *name;
2417 struct passwd pwbuf, *pw = NULL;
2418 struct stat st;
2419
2420 if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
2421 uid = st.st_uid;
2422 else
2423 uid = getuid();
2424
2425 /* Shortcut things to avoid NSS lookups */
2426 if (uid == 0)
2427 return strdup("root");
2428
2429 if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0)
2430 bufsize = 4096;
2431
2432 if (!(buf = malloc(bufsize)))
2433 return NULL;
2434
2435 if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) {
2436 name = strdup(pw->pw_name);
2437 free(buf);
2438 return name;
2439 }
2440
2441 free(buf);
2442
2443 if (asprintf(&name, "%lu", (unsigned long) uid) < 0)
2444 return NULL;
2445
2446 return name;
2447}
2448
8c6db833
LP
2449int getttyname_malloc(char **r) {
2450 char path[PATH_MAX], *p, *c;
2451
2452 assert(r);
ef2f1067
LP
2453
2454 if (ttyname_r(STDIN_FILENO, path, sizeof(path)) < 0)
8c6db833 2455 return -errno;
ef2f1067
LP
2456
2457 char_array_0(path);
2458
2459 p = path;
2460 if (startswith(path, "/dev/"))
2461 p += 5;
2462
8c6db833
LP
2463 if (!(c = strdup(p)))
2464 return -ENOMEM;
2465
2466 *r = c;
2467 return 0;
2468}
2469
2470static int rm_rf_children(int fd, bool only_dirs) {
2471 DIR *d;
2472 int ret = 0;
2473
2474 assert(fd >= 0);
2475
2476 /* This returns the first error we run into, but nevertheless
2477 * tries to go on */
2478
2479 if (!(d = fdopendir(fd))) {
2480 close_nointr_nofail(fd);
2481 return -errno;
2482 }
2483
2484 for (;;) {
2485 struct dirent buf, *de;
2486 bool is_dir;
2487 int r;
2488
2489 if ((r = readdir_r(d, &buf, &de)) != 0) {
2490 if (ret == 0)
2491 ret = -r;
2492 break;
2493 }
2494
2495 if (!de)
2496 break;
2497
2498 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
2499 continue;
2500
2501 if (de->d_type == DT_UNKNOWN) {
2502 struct stat st;
2503
2504 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
2505 if (ret == 0)
2506 ret = -errno;
2507 continue;
2508 }
2509
2510 is_dir = S_ISDIR(st.st_mode);
2511 } else
2512 is_dir = de->d_type == DT_DIR;
2513
2514 if (is_dir) {
2515 int subdir_fd;
2516
2517 if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
2518 if (ret == 0)
2519 ret = -errno;
2520 continue;
2521 }
2522
2523 if ((r = rm_rf_children(subdir_fd, only_dirs)) < 0) {
2524 if (ret == 0)
2525 ret = r;
2526 }
2527
2528 if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
2529 if (ret == 0)
2530 ret = -errno;
2531 }
2532 } else if (!only_dirs) {
2533
2534 if (unlinkat(fd, de->d_name, 0) < 0) {
2535 if (ret == 0)
2536 ret = -errno;
2537 }
2538 }
2539 }
2540
2541 closedir(d);
2542
2543 return ret;
2544}
2545
2546int rm_rf(const char *path, bool only_dirs, bool delete_root) {
2547 int fd;
2548 int r;
2549
2550 assert(path);
2551
2552 if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
2553
2554 if (errno != ENOTDIR)
2555 return -errno;
2556
2557 if (delete_root && !only_dirs)
2558 if (unlink(path) < 0)
2559 return -errno;
2560
2561 return 0;
2562 }
2563
2564 r = rm_rf_children(fd, only_dirs);
2565
2566 if (delete_root)
2567 if (rmdir(path) < 0) {
2568 if (r == 0)
2569 r = -errno;
2570 }
2571
2572 return r;
2573}
2574
2575int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
2576 assert(path);
2577
2578 /* Under the assumption that we are running privileged we
2579 * first change the access mode and only then hand out
2580 * ownership to avoid a window where access is too open. */
2581
2582 if (chmod(path, mode) < 0)
2583 return -errno;
2584
2585 if (chown(path, uid, gid) < 0)
2586 return -errno;
2587
2588 return 0;
ef2f1067
LP
2589}
2590
82c121a4
LP
2591cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
2592 cpu_set_t *r;
2593 unsigned n = 1024;
2594
2595 /* Allocates the cpuset in the right size */
2596
2597 for (;;) {
2598 if (!(r = CPU_ALLOC(n)))
2599 return NULL;
2600
2601 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) {
2602 CPU_ZERO_S(CPU_ALLOC_SIZE(n), r);
2603
2604 if (ncpus)
2605 *ncpus = n;
2606
2607 return r;
2608 }
2609
2610 CPU_FREE(r);
2611
2612 if (errno != EINVAL)
2613 return NULL;
2614
2615 n *= 2;
2616 }
2617}
2618
9e58ff9c
LP
2619void status_vprintf(const char *format, va_list ap) {
2620 char *s = NULL;
2621 int fd = -1;
2622
2623 assert(format);
2624
2625 /* This independent of logging, as status messages are
2626 * optional and go exclusively to the console. */
2627
2628 if (vasprintf(&s, format, ap) < 0)
2629 goto finish;
2630
2631 if ((fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
2632 goto finish;
2633
2634 write(fd, s, strlen(s));
2635
2636finish:
2637 free(s);
2638
2639 if (fd >= 0)
2640 close_nointr_nofail(fd);
2641}
2642
c846ff47
LP
2643void status_printf(const char *format, ...) {
2644 va_list ap;
2645
2646 assert(format);
2647
2648 va_start(ap, format);
2649 status_vprintf(format, ap);
2650 va_end(ap);
2651}
2652
2653void status_welcome(void) {
2654
2655#if defined(TARGET_FEDORA)
2656 char *r;
2657
2658 if (read_one_line_file("/etc/system-release", &r) < 0)
2659 return;
2660
2661 truncate_nl(r);
2662
2663 /* This tries to mimic the color magic the old Red Hat sysinit
2664 * script did. */
2665
2666 if (startswith(r, "Red Hat"))
2e54424d 2667 status_printf("Welcome to \x1B[0;31m%s\x1B[0m!\n", r); /* Red for RHEL */
c846ff47 2668 else if (startswith(r, "Fedora"))
2e54424d 2669 status_printf("Welcome to \x1B[0;34m%s\x1B[0m!\n", r); /* Blue for Fedora */
c846ff47 2670 else
2e54424d 2671 status_printf("Welcome to %s!\n", r);
c846ff47
LP
2672
2673 free(r);
2674
2675#elif defined(TARGET_SUSE)
2676 char *r;
2677
2678 if (read_one_line_file("/etc/SuSE-release", &r) < 0)
2679 return;
2680
2681 truncate_nl(r);
2682
2e54424d 2683 status_printf("Welcome to \x1B[0;32m%s\x1B[0m!\n", r); /* Green for SUSE */
c846ff47
LP
2684 free(r);
2685#else
2686#warning "You probably should add a welcome text logic here."
2687#endif
2688}
2689
1dccbe19
LP
2690static const char *const ioprio_class_table[] = {
2691 [IOPRIO_CLASS_NONE] = "none",
2692 [IOPRIO_CLASS_RT] = "realtime",
2693 [IOPRIO_CLASS_BE] = "best-effort",
2694 [IOPRIO_CLASS_IDLE] = "idle"
2695};
2696
2697DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
2698
2699static const char *const sigchld_code_table[] = {
2700 [CLD_EXITED] = "exited",
2701 [CLD_KILLED] = "killed",
2702 [CLD_DUMPED] = "dumped",
2703 [CLD_TRAPPED] = "trapped",
2704 [CLD_STOPPED] = "stopped",
2705 [CLD_CONTINUED] = "continued",
2706};
2707
2708DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
2709
2710static const char *const log_facility_table[LOG_NFACILITIES] = {
2711 [LOG_FAC(LOG_KERN)] = "kern",
2712 [LOG_FAC(LOG_USER)] = "user",
2713 [LOG_FAC(LOG_MAIL)] = "mail",
2714 [LOG_FAC(LOG_DAEMON)] = "daemon",
2715 [LOG_FAC(LOG_AUTH)] = "auth",
2716 [LOG_FAC(LOG_SYSLOG)] = "syslog",
2717 [LOG_FAC(LOG_LPR)] = "lpr",
2718 [LOG_FAC(LOG_NEWS)] = "news",
2719 [LOG_FAC(LOG_UUCP)] = "uucp",
2720 [LOG_FAC(LOG_CRON)] = "cron",
2721 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
2722 [LOG_FAC(LOG_FTP)] = "ftp",
2723 [LOG_FAC(LOG_LOCAL0)] = "local0",
2724 [LOG_FAC(LOG_LOCAL1)] = "local1",
2725 [LOG_FAC(LOG_LOCAL2)] = "local2",
2726 [LOG_FAC(LOG_LOCAL3)] = "local3",
2727 [LOG_FAC(LOG_LOCAL4)] = "local4",
2728 [LOG_FAC(LOG_LOCAL5)] = "local5",
2729 [LOG_FAC(LOG_LOCAL6)] = "local6",
2730 [LOG_FAC(LOG_LOCAL7)] = "local7"
2731};
2732
2733DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
2734
2735static const char *const log_level_table[] = {
2736 [LOG_EMERG] = "emerg",
2737 [LOG_ALERT] = "alert",
2738 [LOG_CRIT] = "crit",
2739 [LOG_ERR] = "err",
2740 [LOG_WARNING] = "warning",
2741 [LOG_NOTICE] = "notice",
2742 [LOG_INFO] = "info",
2743 [LOG_DEBUG] = "debug"
2744};
2745
2746DEFINE_STRING_TABLE_LOOKUP(log_level, int);
2747
2748static const char* const sched_policy_table[] = {
2749 [SCHED_OTHER] = "other",
2750 [SCHED_BATCH] = "batch",
2751 [SCHED_IDLE] = "idle",
2752 [SCHED_FIFO] = "fifo",
2753 [SCHED_RR] = "rr"
2754};
2755
2756DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
2757
2758static const char* const rlimit_table[] = {
2759 [RLIMIT_CPU] = "LimitCPU",
2760 [RLIMIT_FSIZE] = "LimitFSIZE",
2761 [RLIMIT_DATA] = "LimitDATA",
2762 [RLIMIT_STACK] = "LimitSTACK",
2763 [RLIMIT_CORE] = "LimitCORE",
2764 [RLIMIT_RSS] = "LimitRSS",
2765 [RLIMIT_NOFILE] = "LimitNOFILE",
2766 [RLIMIT_AS] = "LimitAS",
2767 [RLIMIT_NPROC] = "LimitNPROC",
2768 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
2769 [RLIMIT_LOCKS] = "LimitLOCKS",
2770 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
2771 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
2772 [RLIMIT_NICE] = "LimitNICE",
2773 [RLIMIT_RTPRIO] = "LimitRTPRIO",
2774 [RLIMIT_RTTIME] = "LimitRTTIME"
2775};
2776
2777DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
4fd5948e
LP
2778
2779static const char* const ip_tos_table[] = {
2780 [IPTOS_LOWDELAY] = "low-delay",
2781 [IPTOS_THROUGHPUT] = "throughput",
2782 [IPTOS_RELIABILITY] = "reliability",
2783 [IPTOS_LOWCOST] = "low-cost",
2784};
2785
2786DEFINE_STRING_TABLE_LOOKUP(ip_tos, int);