chiark / gitweb /
install: add new installer implementation
[elogind.git] / src / util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 #include <ctype.h>
46 #include <sys/prctl.h>
47 #include <sys/utsname.h>
48 #include <pwd.h>
49 #include <netinet/ip.h>
50 #include <linux/kd.h>
51 #include <dlfcn.h>
52 #include <sys/wait.h>
53 #include <sys/capability.h>
54 #include <sys/time.h>
55 #include <linux/rtc.h>
56 #include <glob.h>
57
58 #include "macro.h"
59 #include "util.h"
60 #include "ioprio.h"
61 #include "missing.h"
62 #include "log.h"
63 #include "strv.h"
64 #include "label.h"
65 #include "exit-status.h"
66 #include "hashmap.h"
67
68 int saved_argc = 0;
69 char **saved_argv = NULL;
70
71 size_t page_size(void) {
72         static __thread size_t pgsz = 0;
73         long r;
74
75         if (pgsz)
76                 return pgsz;
77
78         assert_se((r = sysconf(_SC_PAGESIZE)) > 0);
79
80         pgsz = (size_t) r;
81
82         return pgsz;
83 }
84
85 bool streq_ptr(const char *a, const char *b) {
86
87         /* Like streq(), but tries to make sense of NULL pointers */
88
89         if (a && b)
90                 return streq(a, b);
91
92         if (!a && !b)
93                 return true;
94
95         return false;
96 }
97
98 usec_t now(clockid_t clock_id) {
99         struct timespec ts;
100
101         assert_se(clock_gettime(clock_id, &ts) == 0);
102
103         return timespec_load(&ts);
104 }
105
106 dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
107         assert(ts);
108
109         ts->realtime = now(CLOCK_REALTIME);
110         ts->monotonic = now(CLOCK_MONOTONIC);
111
112         return ts;
113 }
114
115 dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
116         int64_t delta;
117         assert(ts);
118
119         ts->realtime = u;
120
121         if (u == 0)
122                 ts->monotonic = 0;
123         else {
124                 delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
125
126                 ts->monotonic = now(CLOCK_MONOTONIC);
127
128                 if ((int64_t) ts->monotonic > delta)
129                         ts->monotonic -= delta;
130                 else
131                         ts->monotonic = 0;
132         }
133
134         return ts;
135 }
136
137 usec_t timespec_load(const struct timespec *ts) {
138         assert(ts);
139
140         return
141                 (usec_t) ts->tv_sec * USEC_PER_SEC +
142                 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
143 }
144
145 struct timespec *timespec_store(struct timespec *ts, usec_t u)  {
146         assert(ts);
147
148         ts->tv_sec = (time_t) (u / USEC_PER_SEC);
149         ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
150
151         return ts;
152 }
153
154 usec_t timeval_load(const struct timeval *tv) {
155         assert(tv);
156
157         return
158                 (usec_t) tv->tv_sec * USEC_PER_SEC +
159                 (usec_t) tv->tv_usec;
160 }
161
162 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
163         assert(tv);
164
165         tv->tv_sec = (time_t) (u / USEC_PER_SEC);
166         tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
167
168         return tv;
169 }
170
171 bool endswith(const char *s, const char *postfix) {
172         size_t sl, pl;
173
174         assert(s);
175         assert(postfix);
176
177         sl = strlen(s);
178         pl = strlen(postfix);
179
180         if (pl == 0)
181                 return true;
182
183         if (sl < pl)
184                 return false;
185
186         return memcmp(s + sl - pl, postfix, pl) == 0;
187 }
188
189 bool startswith(const char *s, const char *prefix) {
190         size_t sl, pl;
191
192         assert(s);
193         assert(prefix);
194
195         sl = strlen(s);
196         pl = strlen(prefix);
197
198         if (pl == 0)
199                 return true;
200
201         if (sl < pl)
202                 return false;
203
204         return memcmp(s, prefix, pl) == 0;
205 }
206
207 bool startswith_no_case(const char *s, const char *prefix) {
208         size_t sl, pl;
209         unsigned i;
210
211         assert(s);
212         assert(prefix);
213
214         sl = strlen(s);
215         pl = strlen(prefix);
216
217         if (pl == 0)
218                 return true;
219
220         if (sl < pl)
221                 return false;
222
223         for(i = 0; i < pl; ++i) {
224                 if (tolower(s[i]) != tolower(prefix[i]))
225                         return false;
226         }
227
228         return true;
229 }
230
231 bool first_word(const char *s, const char *word) {
232         size_t sl, wl;
233
234         assert(s);
235         assert(word);
236
237         sl = strlen(s);
238         wl = strlen(word);
239
240         if (sl < wl)
241                 return false;
242
243         if (wl == 0)
244                 return true;
245
246         if (memcmp(s, word, wl) != 0)
247                 return false;
248
249         return s[wl] == 0 ||
250                 strchr(WHITESPACE, s[wl]);
251 }
252
253 int close_nointr(int fd) {
254         assert(fd >= 0);
255
256         for (;;) {
257                 int r;
258
259                 r = close(fd);
260                 if (r >= 0)
261                         return r;
262
263                 if (errno != EINTR)
264                         return -errno;
265         }
266 }
267
268 void close_nointr_nofail(int fd) {
269         int saved_errno = errno;
270
271         /* like close_nointr() but cannot fail, and guarantees errno
272          * is unchanged */
273
274         assert_se(close_nointr(fd) == 0);
275
276         errno = saved_errno;
277 }
278
279 void close_many(const int fds[], unsigned n_fd) {
280         unsigned i;
281
282         for (i = 0; i < n_fd; i++)
283                 close_nointr_nofail(fds[i]);
284 }
285
286 int parse_boolean(const char *v) {
287         assert(v);
288
289         if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
290                 return 1;
291         else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
292                 return 0;
293
294         return -EINVAL;
295 }
296
297 int parse_pid(const char *s, pid_t* ret_pid) {
298         unsigned long ul = 0;
299         pid_t pid;
300         int r;
301
302         assert(s);
303         assert(ret_pid);
304
305         if ((r = safe_atolu(s, &ul)) < 0)
306                 return r;
307
308         pid = (pid_t) ul;
309
310         if ((unsigned long) pid != ul)
311                 return -ERANGE;
312
313         if (pid <= 0)
314                 return -ERANGE;
315
316         *ret_pid = pid;
317         return 0;
318 }
319
320 int safe_atou(const char *s, unsigned *ret_u) {
321         char *x = NULL;
322         unsigned long l;
323
324         assert(s);
325         assert(ret_u);
326
327         errno = 0;
328         l = strtoul(s, &x, 0);
329
330         if (!x || *x || errno)
331                 return errno ? -errno : -EINVAL;
332
333         if ((unsigned long) (unsigned) l != l)
334                 return -ERANGE;
335
336         *ret_u = (unsigned) l;
337         return 0;
338 }
339
340 int safe_atoi(const char *s, int *ret_i) {
341         char *x = NULL;
342         long l;
343
344         assert(s);
345         assert(ret_i);
346
347         errno = 0;
348         l = strtol(s, &x, 0);
349
350         if (!x || *x || errno)
351                 return errno ? -errno : -EINVAL;
352
353         if ((long) (int) l != l)
354                 return -ERANGE;
355
356         *ret_i = (int) l;
357         return 0;
358 }
359
360 int safe_atollu(const char *s, long long unsigned *ret_llu) {
361         char *x = NULL;
362         unsigned long long l;
363
364         assert(s);
365         assert(ret_llu);
366
367         errno = 0;
368         l = strtoull(s, &x, 0);
369
370         if (!x || *x || errno)
371                 return errno ? -errno : -EINVAL;
372
373         *ret_llu = l;
374         return 0;
375 }
376
377 int safe_atolli(const char *s, long long int *ret_lli) {
378         char *x = NULL;
379         long long l;
380
381         assert(s);
382         assert(ret_lli);
383
384         errno = 0;
385         l = strtoll(s, &x, 0);
386
387         if (!x || *x || errno)
388                 return errno ? -errno : -EINVAL;
389
390         *ret_lli = l;
391         return 0;
392 }
393
394 /* Split a string into words. */
395 char *split(const char *c, size_t *l, const char *separator, char **state) {
396         char *current;
397
398         current = *state ? *state : (char*) c;
399
400         if (!*current || *c == 0)
401                 return NULL;
402
403         current += strspn(current, separator);
404         *l = strcspn(current, separator);
405         *state = current+*l;
406
407         return (char*) current;
408 }
409
410 /* Split a string into words, but consider strings enclosed in '' and
411  * "" as words even if they include spaces. */
412 char *split_quoted(const char *c, size_t *l, char **state) {
413         char *current, *e;
414         bool escaped = false;
415
416         current = *state ? *state : (char*) c;
417
418         if (!*current || *c == 0)
419                 return NULL;
420
421         current += strspn(current, WHITESPACE);
422
423         if (*current == '\'') {
424                 current ++;
425
426                 for (e = current; *e; e++) {
427                         if (escaped)
428                                 escaped = false;
429                         else if (*e == '\\')
430                                 escaped = true;
431                         else if (*e == '\'')
432                                 break;
433                 }
434
435                 *l = e-current;
436                 *state = *e == 0 ? e : e+1;
437         } else if (*current == '\"') {
438                 current ++;
439
440                 for (e = current; *e; e++) {
441                         if (escaped)
442                                 escaped = false;
443                         else if (*e == '\\')
444                                 escaped = true;
445                         else if (*e == '\"')
446                                 break;
447                 }
448
449                 *l = e-current;
450                 *state = *e == 0 ? e : e+1;
451         } else {
452                 for (e = current; *e; e++) {
453                         if (escaped)
454                                 escaped = false;
455                         else if (*e == '\\')
456                                 escaped = true;
457                         else if (strchr(WHITESPACE, *e))
458                                 break;
459                 }
460                 *l = e-current;
461                 *state = e;
462         }
463
464         return (char*) current;
465 }
466
467 char **split_path_and_make_absolute(const char *p) {
468         char **l;
469         assert(p);
470
471         if (!(l = strv_split(p, ":")))
472                 return NULL;
473
474         if (!strv_path_make_absolute_cwd(l)) {
475                 strv_free(l);
476                 return NULL;
477         }
478
479         return l;
480 }
481
482 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
483         int r;
484         FILE *f;
485         char fn[PATH_MAX], line[LINE_MAX], *p;
486         long unsigned ppid;
487
488         assert(pid > 0);
489         assert(_ppid);
490
491         assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
492         char_array_0(fn);
493
494         if (!(f = fopen(fn, "re")))
495                 return -errno;
496
497         if (!(fgets(line, sizeof(line), f))) {
498                 r = -errno;
499                 fclose(f);
500                 return r;
501         }
502
503         fclose(f);
504
505         /* Let's skip the pid and comm fields. The latter is enclosed
506          * in () but does not escape any () in its value, so let's
507          * skip over it manually */
508
509         if (!(p = strrchr(line, ')')))
510                 return -EIO;
511
512         p++;
513
514         if (sscanf(p, " "
515                    "%*c "  /* state */
516                    "%lu ", /* ppid */
517                    &ppid) != 1)
518                 return -EIO;
519
520         if ((long unsigned) (pid_t) ppid != ppid)
521                 return -ERANGE;
522
523         *_ppid = (pid_t) ppid;
524
525         return 0;
526 }
527
528 int get_starttime_of_pid(pid_t pid, unsigned long long *st) {
529         int r;
530         FILE *f;
531         char fn[PATH_MAX], line[LINE_MAX], *p;
532
533         assert(pid > 0);
534         assert(st);
535
536         assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
537         char_array_0(fn);
538
539         if (!(f = fopen(fn, "re")))
540                 return -errno;
541
542         if (!(fgets(line, sizeof(line), f))) {
543                 r = -errno;
544                 fclose(f);
545                 return r;
546         }
547
548         fclose(f);
549
550         /* Let's skip the pid and comm fields. The latter is enclosed
551          * in () but does not escape any () in its value, so let's
552          * skip over it manually */
553
554         if (!(p = strrchr(line, ')')))
555                 return -EIO;
556
557         p++;
558
559         if (sscanf(p, " "
560                    "%*c "  /* state */
561                    "%*d "  /* ppid */
562                    "%*d "  /* pgrp */
563                    "%*d "  /* session */
564                    "%*d "  /* tty_nr */
565                    "%*d "  /* tpgid */
566                    "%*u "  /* flags */
567                    "%*u "  /* minflt */
568                    "%*u "  /* cminflt */
569                    "%*u "  /* majflt */
570                    "%*u "  /* cmajflt */
571                    "%*u "  /* utime */
572                    "%*u "  /* stime */
573                    "%*d "  /* cutime */
574                    "%*d "  /* cstime */
575                    "%*d "  /* priority */
576                    "%*d "  /* nice */
577                    "%*d "  /* num_threads */
578                    "%*d "  /* itrealvalue */
579                    "%llu "  /* starttime */,
580                    st) != 1)
581                 return -EIO;
582
583         return 0;
584 }
585
586 int write_one_line_file(const char *fn, const char *line) {
587         FILE *f;
588         int r;
589
590         assert(fn);
591         assert(line);
592
593         if (!(f = fopen(fn, "we")))
594                 return -errno;
595
596         errno = 0;
597         if (fputs(line, f) < 0) {
598                 r = -errno;
599                 goto finish;
600         }
601
602         if (!endswith(line, "\n"))
603                 fputc('\n', f);
604
605         fflush(f);
606
607         if (ferror(f)) {
608                 if (errno != 0)
609                         r = -errno;
610                 else
611                         r = -EIO;
612         } else
613                 r = 0;
614
615 finish:
616         fclose(f);
617         return r;
618 }
619
620 int fchmod_umask(int fd, mode_t m) {
621         mode_t u;
622         int r;
623
624         u = umask(0777);
625         r = fchmod(fd, m & (~u)) < 0 ? -errno : 0;
626         umask(u);
627
628         return r;
629 }
630
631 int write_one_line_file_atomic(const char *fn, const char *line) {
632         FILE *f;
633         int r;
634         char *p;
635
636         assert(fn);
637         assert(line);
638
639         r = fopen_temporary(fn, &f, &p);
640         if (r < 0)
641                 return r;
642
643         fchmod_umask(fileno(f), 0644);
644
645         errno = 0;
646         if (fputs(line, f) < 0) {
647                 r = -errno;
648                 goto finish;
649         }
650
651         if (!endswith(line, "\n"))
652                 fputc('\n', f);
653
654         fflush(f);
655
656         if (ferror(f)) {
657                 if (errno != 0)
658                         r = -errno;
659                 else
660                         r = -EIO;
661         } else {
662                 if (rename(p, fn) < 0)
663                         r = -errno;
664                 else
665                         r = 0;
666         }
667
668 finish:
669         if (r < 0)
670                 unlink(p);
671
672         fclose(f);
673         free(p);
674
675         return r;
676 }
677
678 int read_one_line_file(const char *fn, char **line) {
679         FILE *f;
680         int r;
681         char t[LINE_MAX], *c;
682
683         assert(fn);
684         assert(line);
685
686         if (!(f = fopen(fn, "re")))
687                 return -errno;
688
689         if (!(fgets(t, sizeof(t), f))) {
690                 r = -errno;
691                 goto finish;
692         }
693
694         if (!(c = strdup(t))) {
695                 r = -ENOMEM;
696                 goto finish;
697         }
698
699         truncate_nl(c);
700
701         *line = c;
702         r = 0;
703
704 finish:
705         fclose(f);
706         return r;
707 }
708
709 int read_full_file(const char *fn, char **contents, size_t *size) {
710         FILE *f;
711         int r;
712         size_t n, l;
713         char *buf = NULL;
714         struct stat st;
715
716         if (!(f = fopen(fn, "re")))
717                 return -errno;
718
719         if (fstat(fileno(f), &st) < 0) {
720                 r = -errno;
721                 goto finish;
722         }
723
724         /* Safety check */
725         if (st.st_size > 4*1024*1024) {
726                 r = -E2BIG;
727                 goto finish;
728         }
729
730         n = st.st_size > 0 ? st.st_size : LINE_MAX;
731         l = 0;
732
733         for (;;) {
734                 char *t;
735                 size_t k;
736
737                 if (!(t = realloc(buf, n+1))) {
738                         r = -ENOMEM;
739                         goto finish;
740                 }
741
742                 buf = t;
743                 k = fread(buf + l, 1, n - l, f);
744
745                 if (k <= 0) {
746                         if (ferror(f)) {
747                                 r = -errno;
748                                 goto finish;
749                         }
750
751                         break;
752                 }
753
754                 l += k;
755                 n *= 2;
756
757                 /* Safety check */
758                 if (n > 4*1024*1024) {
759                         r = -E2BIG;
760                         goto finish;
761                 }
762         }
763
764         if (buf)
765                 buf[l] = 0;
766         else if (!(buf = calloc(1, 1))) {
767                 r = -errno;
768                 goto finish;
769         }
770
771         *contents = buf;
772         buf = NULL;
773
774         if (size)
775                 *size = l;
776
777         r = 0;
778
779 finish:
780         fclose(f);
781         free(buf);
782
783         return r;
784 }
785
786 int parse_env_file(
787                 const char *fname,
788                 const char *separator, ...) {
789
790         int r = 0;
791         char *contents, *p;
792
793         assert(fname);
794         assert(separator);
795
796         if ((r = read_full_file(fname, &contents, NULL)) < 0)
797                 return r;
798
799         p = contents;
800         for (;;) {
801                 const char *key = NULL;
802
803                 p += strspn(p, separator);
804                 p += strspn(p, WHITESPACE);
805
806                 if (!*p)
807                         break;
808
809                 if (!strchr(COMMENTS, *p)) {
810                         va_list ap;
811                         char **value;
812
813                         va_start(ap, separator);
814                         while ((key = va_arg(ap, char *))) {
815                                 size_t n;
816                                 char *v;
817
818                                 value = va_arg(ap, char **);
819
820                                 n = strlen(key);
821                                 if (strncmp(p, key, n) != 0 ||
822                                     p[n] != '=')
823                                         continue;
824
825                                 p += n + 1;
826                                 n = strcspn(p, separator);
827
828                                 if (n >= 2 &&
829                                     strchr(QUOTES, p[0]) &&
830                                     p[n-1] == p[0])
831                                         v = strndup(p+1, n-2);
832                                 else
833                                         v = strndup(p, n);
834
835                                 if (!v) {
836                                         r = -ENOMEM;
837                                         va_end(ap);
838                                         goto fail;
839                                 }
840
841                                 if (v[0] == '\0') {
842                                         /* return empty value strings as NULL */
843                                         free(v);
844                                         v = NULL;
845                                 }
846
847                                 free(*value);
848                                 *value = v;
849
850                                 p += n;
851
852                                 r ++;
853                                 break;
854                         }
855                         va_end(ap);
856                 }
857
858                 if (!key)
859                         p += strcspn(p, separator);
860         }
861
862 fail:
863         free(contents);
864         return r;
865 }
866
867 int load_env_file(
868                 const char *fname,
869                 char ***rl) {
870
871         FILE *f;
872         char **m = 0;
873         int r;
874
875         assert(fname);
876         assert(rl);
877
878         if (!(f = fopen(fname, "re")))
879                 return -errno;
880
881         while (!feof(f)) {
882                 char l[LINE_MAX], *p, *u;
883                 char **t;
884
885                 if (!fgets(l, sizeof(l), f)) {
886                         if (feof(f))
887                                 break;
888
889                         r = -errno;
890                         goto finish;
891                 }
892
893                 p = strstrip(l);
894
895                 if (!*p)
896                         continue;
897
898                 if (strchr(COMMENTS, *p))
899                         continue;
900
901                 if (!(u = normalize_env_assignment(p))) {
902                         log_error("Out of memory");
903                         r = -ENOMEM;
904                         goto finish;
905                 }
906
907                 t = strv_append(m, u);
908                 free(u);
909
910                 if (!t) {
911                         log_error("Out of memory");
912                         r = -ENOMEM;
913                         goto finish;
914                 }
915
916                 strv_free(m);
917                 m = t;
918         }
919
920         r = 0;
921
922         *rl = m;
923         m = NULL;
924
925 finish:
926         if (f)
927                 fclose(f);
928
929         strv_free(m);
930
931         return r;
932 }
933
934 int write_env_file(const char *fname, char **l) {
935         char **i, *p;
936         FILE *f;
937         int r;
938
939         r = fopen_temporary(fname, &f, &p);
940         if (r < 0)
941                 return r;
942
943         fchmod_umask(fileno(f), 0644);
944
945         errno = 0;
946         STRV_FOREACH(i, l) {
947                 fputs(*i, f);
948                 fputc('\n', f);
949         }
950
951         fflush(f);
952
953         if (ferror(f)) {
954                 if (errno != 0)
955                         r = -errno;
956                 else
957                         r = -EIO;
958         } else {
959                 if (rename(p, fname) < 0)
960                         r = -errno;
961                 else
962                         r = 0;
963         }
964
965         if (r < 0)
966                 unlink(p);
967
968         fclose(f);
969         free(p);
970
971         return r;
972 }
973
974 char *truncate_nl(char *s) {
975         assert(s);
976
977         s[strcspn(s, NEWLINE)] = 0;
978         return s;
979 }
980
981 int get_process_name(pid_t pid, char **name) {
982         char *p;
983         int r;
984
985         assert(pid >= 1);
986         assert(name);
987
988         if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
989                 return -ENOMEM;
990
991         r = read_one_line_file(p, name);
992         free(p);
993
994         if (r < 0)
995                 return r;
996
997         return 0;
998 }
999
1000 int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
1001         char *p, *r, *k;
1002         int c;
1003         bool space = false;
1004         size_t left;
1005         FILE *f;
1006
1007         assert(pid >= 1);
1008         assert(max_length > 0);
1009         assert(line);
1010
1011         if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
1012                 return -ENOMEM;
1013
1014         f = fopen(p, "re");
1015         free(p);
1016
1017         if (!f)
1018                 return -errno;
1019
1020         if (!(r = new(char, max_length))) {
1021                 fclose(f);
1022                 return -ENOMEM;
1023         }
1024
1025         k = r;
1026         left = max_length;
1027         while ((c = getc(f)) != EOF) {
1028
1029                 if (isprint(c)) {
1030                         if (space) {
1031                                 if (left <= 4)
1032                                         break;
1033
1034                                 *(k++) = ' ';
1035                                 left--;
1036                                 space = false;
1037                         }
1038
1039                         if (left <= 4)
1040                                 break;
1041
1042                         *(k++) = (char) c;
1043                         left--;
1044                 }  else
1045                         space = true;
1046         }
1047
1048         if (left <= 4) {
1049                 size_t n = MIN(left-1, 3U);
1050                 memcpy(k, "...", n);
1051                 k[n] = 0;
1052         } else
1053                 *k = 0;
1054
1055         fclose(f);
1056
1057         /* Kernel threads have no argv[] */
1058         if (r[0] == 0) {
1059                 char *t;
1060                 int h;
1061
1062                 free(r);
1063
1064                 if ((h = get_process_name(pid, &t)) < 0)
1065                         return h;
1066
1067                 h = asprintf(&r, "[%s]", t);
1068                 free(t);
1069
1070                 if (h < 0)
1071                         return -ENOMEM;
1072         }
1073
1074         *line = r;
1075         return 0;
1076 }
1077
1078 char *strnappend(const char *s, const char *suffix, size_t b) {
1079         size_t a;
1080         char *r;
1081
1082         if (!s && !suffix)
1083                 return strdup("");
1084
1085         if (!s)
1086                 return strndup(suffix, b);
1087
1088         if (!suffix)
1089                 return strdup(s);
1090
1091         assert(s);
1092         assert(suffix);
1093
1094         a = strlen(s);
1095
1096         if (!(r = new(char, a+b+1)))
1097                 return NULL;
1098
1099         memcpy(r, s, a);
1100         memcpy(r+a, suffix, b);
1101         r[a+b] = 0;
1102
1103         return r;
1104 }
1105
1106 char *strappend(const char *s, const char *suffix) {
1107         return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
1108 }
1109
1110 int readlink_malloc(const char *p, char **r) {
1111         size_t l = 100;
1112
1113         assert(p);
1114         assert(r);
1115
1116         for (;;) {
1117                 char *c;
1118                 ssize_t n;
1119
1120                 if (!(c = new(char, l)))
1121                         return -ENOMEM;
1122
1123                 if ((n = readlink(p, c, l-1)) < 0) {
1124                         int ret = -errno;
1125                         free(c);
1126                         return ret;
1127                 }
1128
1129                 if ((size_t) n < l-1) {
1130                         c[n] = 0;
1131                         *r = c;
1132                         return 0;
1133                 }
1134
1135                 free(c);
1136                 l *= 2;
1137         }
1138 }
1139
1140 int readlink_and_make_absolute(const char *p, char **r) {
1141         char *target, *k;
1142         int j;
1143
1144         assert(p);
1145         assert(r);
1146
1147         if ((j = readlink_malloc(p, &target)) < 0)
1148                 return j;
1149
1150         k = file_in_same_dir(p, target);
1151         free(target);
1152
1153         if (!k)
1154                 return -ENOMEM;
1155
1156         *r = k;
1157         return 0;
1158 }
1159
1160 int readlink_and_canonicalize(const char *p, char **r) {
1161         char *t, *s;
1162         int j;
1163
1164         assert(p);
1165         assert(r);
1166
1167         j = readlink_and_make_absolute(p, &t);
1168         if (j < 0)
1169                 return j;
1170
1171         s = canonicalize_file_name(t);
1172         if (s) {
1173                 free(t);
1174                 *r = s;
1175         } else
1176                 *r = t;
1177
1178         path_kill_slashes(*r);
1179
1180         return 0;
1181 }
1182
1183 int parent_of_path(const char *path, char **_r) {
1184         const char *e, *a = NULL, *b = NULL, *p;
1185         char *r;
1186         bool slash = false;
1187
1188         assert(path);
1189         assert(_r);
1190
1191         if (!*path)
1192                 return -EINVAL;
1193
1194         for (e = path; *e; e++) {
1195
1196                 if (!slash && *e == '/') {
1197                         a = b;
1198                         b = e;
1199                         slash = true;
1200                 } else if (slash && *e != '/')
1201                         slash = false;
1202         }
1203
1204         if (*(e-1) == '/')
1205                 p = a;
1206         else
1207                 p = b;
1208
1209         if (!p)
1210                 return -EINVAL;
1211
1212         if (p == path)
1213                 r = strdup("/");
1214         else
1215                 r = strndup(path, p-path);
1216
1217         if (!r)
1218                 return -ENOMEM;
1219
1220         *_r = r;
1221         return 0;
1222 }
1223
1224
1225 char *file_name_from_path(const char *p) {
1226         char *r;
1227
1228         assert(p);
1229
1230         if ((r = strrchr(p, '/')))
1231                 return r + 1;
1232
1233         return (char*) p;
1234 }
1235
1236 bool path_is_absolute(const char *p) {
1237         assert(p);
1238
1239         return p[0] == '/';
1240 }
1241
1242 bool is_path(const char *p) {
1243
1244         return !!strchr(p, '/');
1245 }
1246
1247 char *path_make_absolute(const char *p, const char *prefix) {
1248         char *r;
1249
1250         assert(p);
1251
1252         /* Makes every item in the list an absolute path by prepending
1253          * the prefix, if specified and necessary */
1254
1255         if (path_is_absolute(p) || !prefix)
1256                 return strdup(p);
1257
1258         if (asprintf(&r, "%s/%s", prefix, p) < 0)
1259                 return NULL;
1260
1261         return r;
1262 }
1263
1264 char *path_make_absolute_cwd(const char *p) {
1265         char *cwd, *r;
1266
1267         assert(p);
1268
1269         /* Similar to path_make_absolute(), but prefixes with the
1270          * current working directory. */
1271
1272         if (path_is_absolute(p))
1273                 return strdup(p);
1274
1275         if (!(cwd = get_current_dir_name()))
1276                 return NULL;
1277
1278         r = path_make_absolute(p, cwd);
1279         free(cwd);
1280
1281         return r;
1282 }
1283
1284 char **strv_path_make_absolute_cwd(char **l) {
1285         char **s;
1286
1287         /* Goes through every item in the string list and makes it
1288          * absolute. This works in place and won't rollback any
1289          * changes on failure. */
1290
1291         STRV_FOREACH(s, l) {
1292                 char *t;
1293
1294                 if (!(t = path_make_absolute_cwd(*s)))
1295                         return NULL;
1296
1297                 free(*s);
1298                 *s = t;
1299         }
1300
1301         return l;
1302 }
1303
1304 char **strv_path_canonicalize(char **l) {
1305         char **s;
1306         unsigned k = 0;
1307         bool enomem = false;
1308
1309         if (strv_isempty(l))
1310                 return l;
1311
1312         /* Goes through every item in the string list and canonicalize
1313          * the path. This works in place and won't rollback any
1314          * changes on failure. */
1315
1316         STRV_FOREACH(s, l) {
1317                 char *t, *u;
1318
1319                 t = path_make_absolute_cwd(*s);
1320                 free(*s);
1321
1322                 if (!t) {
1323                         enomem = true;
1324                         continue;
1325                 }
1326
1327                 errno = 0;
1328                 u = canonicalize_file_name(t);
1329                 free(t);
1330
1331                 if (!u) {
1332                         if (errno == ENOMEM || !errno)
1333                                 enomem = true;
1334
1335                         continue;
1336                 }
1337
1338                 l[k++] = u;
1339         }
1340
1341         l[k] = NULL;
1342
1343         if (enomem)
1344                 return NULL;
1345
1346         return l;
1347 }
1348
1349 char **strv_path_remove_empty(char **l) {
1350         char **f, **t;
1351
1352         if (!l)
1353                 return NULL;
1354
1355         for (f = t = l; *f; f++) {
1356
1357                 if (dir_is_empty(*f) > 0) {
1358                         free(*f);
1359                         continue;
1360                 }
1361
1362                 *(t++) = *f;
1363         }
1364
1365         *t = NULL;
1366         return l;
1367 }
1368
1369 int reset_all_signal_handlers(void) {
1370         int sig;
1371
1372         for (sig = 1; sig < _NSIG; sig++) {
1373                 struct sigaction sa;
1374
1375                 if (sig == SIGKILL || sig == SIGSTOP)
1376                         continue;
1377
1378                 zero(sa);
1379                 sa.sa_handler = SIG_DFL;
1380                 sa.sa_flags = SA_RESTART;
1381
1382                 /* On Linux the first two RT signals are reserved by
1383                  * glibc, and sigaction() will return EINVAL for them. */
1384                 if ((sigaction(sig, &sa, NULL) < 0))
1385                         if (errno != EINVAL)
1386                                 return -errno;
1387         }
1388
1389         return 0;
1390 }
1391
1392 char *strstrip(char *s) {
1393         char *e, *l = NULL;
1394
1395         /* Drops trailing whitespace. Modifies the string in
1396          * place. Returns pointer to first non-space character */
1397
1398         s += strspn(s, WHITESPACE);
1399
1400         for (e = s; *e; e++)
1401                 if (!strchr(WHITESPACE, *e))
1402                         l = e;
1403
1404         if (l)
1405                 *(l+1) = 0;
1406         else
1407                 *s = 0;
1408
1409         return s;
1410 }
1411
1412 char *delete_chars(char *s, const char *bad) {
1413         char *f, *t;
1414
1415         /* Drops all whitespace, regardless where in the string */
1416
1417         for (f = s, t = s; *f; f++) {
1418                 if (strchr(bad, *f))
1419                         continue;
1420
1421                 *(t++) = *f;
1422         }
1423
1424         *t = 0;
1425
1426         return s;
1427 }
1428
1429 char *file_in_same_dir(const char *path, const char *filename) {
1430         char *e, *r;
1431         size_t k;
1432
1433         assert(path);
1434         assert(filename);
1435
1436         /* This removes the last component of path and appends
1437          * filename, unless the latter is absolute anyway or the
1438          * former isn't */
1439
1440         if (path_is_absolute(filename))
1441                 return strdup(filename);
1442
1443         if (!(e = strrchr(path, '/')))
1444                 return strdup(filename);
1445
1446         k = strlen(filename);
1447         if (!(r = new(char, e-path+1+k+1)))
1448                 return NULL;
1449
1450         memcpy(r, path, e-path+1);
1451         memcpy(r+(e-path)+1, filename, k+1);
1452
1453         return r;
1454 }
1455
1456 int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
1457         struct stat st;
1458
1459         if (label_mkdir(path, mode) >= 0)
1460                 if (chmod_and_chown(path, mode, uid, gid) < 0)
1461                         return -errno;
1462
1463         if (lstat(path, &st) < 0)
1464                 return -errno;
1465
1466         if ((st.st_mode & 0777) != mode ||
1467             st.st_uid != uid ||
1468             st.st_gid != gid ||
1469             !S_ISDIR(st.st_mode)) {
1470                 errno = EEXIST;
1471                 return -errno;
1472         }
1473
1474         return 0;
1475 }
1476
1477
1478 int mkdir_parents(const char *path, mode_t mode) {
1479         const char *p, *e;
1480
1481         assert(path);
1482
1483         /* Creates every parent directory in the path except the last
1484          * component. */
1485
1486         p = path + strspn(path, "/");
1487         for (;;) {
1488                 int r;
1489                 char *t;
1490
1491                 e = p + strcspn(p, "/");
1492                 p = e + strspn(e, "/");
1493
1494                 /* Is this the last component? If so, then we're
1495                  * done */
1496                 if (*p == 0)
1497                         return 0;
1498
1499                 if (!(t = strndup(path, e - path)))
1500                         return -ENOMEM;
1501
1502                 r = label_mkdir(t, mode);
1503                 free(t);
1504
1505                 if (r < 0 && errno != EEXIST)
1506                         return -errno;
1507         }
1508 }
1509
1510 int mkdir_p(const char *path, mode_t mode) {
1511         int r;
1512
1513         /* Like mkdir -p */
1514
1515         if ((r = mkdir_parents(path, mode)) < 0)
1516                 return r;
1517
1518         if (label_mkdir(path, mode) < 0 && errno != EEXIST)
1519                 return -errno;
1520
1521         return 0;
1522 }
1523
1524 int rmdir_parents(const char *path, const char *stop) {
1525         size_t l;
1526         int r = 0;
1527
1528         assert(path);
1529         assert(stop);
1530
1531         l = strlen(path);
1532
1533         /* Skip trailing slashes */
1534         while (l > 0 && path[l-1] == '/')
1535                 l--;
1536
1537         while (l > 0) {
1538                 char *t;
1539
1540                 /* Skip last component */
1541                 while (l > 0 && path[l-1] != '/')
1542                         l--;
1543
1544                 /* Skip trailing slashes */
1545                 while (l > 0 && path[l-1] == '/')
1546                         l--;
1547
1548                 if (l <= 0)
1549                         break;
1550
1551                 if (!(t = strndup(path, l)))
1552                         return -ENOMEM;
1553
1554                 if (path_startswith(stop, t)) {
1555                         free(t);
1556                         return 0;
1557                 }
1558
1559                 r = rmdir(t);
1560                 free(t);
1561
1562                 if (r < 0)
1563                         if (errno != ENOENT)
1564                                 return -errno;
1565         }
1566
1567         return 0;
1568 }
1569
1570
1571 char hexchar(int x) {
1572         static const char table[16] = "0123456789abcdef";
1573
1574         return table[x & 15];
1575 }
1576
1577 int unhexchar(char c) {
1578
1579         if (c >= '0' && c <= '9')
1580                 return c - '0';
1581
1582         if (c >= 'a' && c <= 'f')
1583                 return c - 'a' + 10;
1584
1585         if (c >= 'A' && c <= 'F')
1586                 return c - 'A' + 10;
1587
1588         return -1;
1589 }
1590
1591 char octchar(int x) {
1592         return '0' + (x & 7);
1593 }
1594
1595 int unoctchar(char c) {
1596
1597         if (c >= '0' && c <= '7')
1598                 return c - '0';
1599
1600         return -1;
1601 }
1602
1603 char decchar(int x) {
1604         return '0' + (x % 10);
1605 }
1606
1607 int undecchar(char c) {
1608
1609         if (c >= '0' && c <= '9')
1610                 return c - '0';
1611
1612         return -1;
1613 }
1614
1615 char *cescape(const char *s) {
1616         char *r, *t;
1617         const char *f;
1618
1619         assert(s);
1620
1621         /* Does C style string escaping. */
1622
1623         if (!(r = new(char, strlen(s)*4 + 1)))
1624                 return NULL;
1625
1626         for (f = s, t = r; *f; f++)
1627
1628                 switch (*f) {
1629
1630                 case '\a':
1631                         *(t++) = '\\';
1632                         *(t++) = 'a';
1633                         break;
1634                 case '\b':
1635                         *(t++) = '\\';
1636                         *(t++) = 'b';
1637                         break;
1638                 case '\f':
1639                         *(t++) = '\\';
1640                         *(t++) = 'f';
1641                         break;
1642                 case '\n':
1643                         *(t++) = '\\';
1644                         *(t++) = 'n';
1645                         break;
1646                 case '\r':
1647                         *(t++) = '\\';
1648                         *(t++) = 'r';
1649                         break;
1650                 case '\t':
1651                         *(t++) = '\\';
1652                         *(t++) = 't';
1653                         break;
1654                 case '\v':
1655                         *(t++) = '\\';
1656                         *(t++) = 'v';
1657                         break;
1658                 case '\\':
1659                         *(t++) = '\\';
1660                         *(t++) = '\\';
1661                         break;
1662                 case '"':
1663                         *(t++) = '\\';
1664                         *(t++) = '"';
1665                         break;
1666                 case '\'':
1667                         *(t++) = '\\';
1668                         *(t++) = '\'';
1669                         break;
1670
1671                 default:
1672                         /* For special chars we prefer octal over
1673                          * hexadecimal encoding, simply because glib's
1674                          * g_strescape() does the same */
1675                         if ((*f < ' ') || (*f >= 127)) {
1676                                 *(t++) = '\\';
1677                                 *(t++) = octchar((unsigned char) *f >> 6);
1678                                 *(t++) = octchar((unsigned char) *f >> 3);
1679                                 *(t++) = octchar((unsigned char) *f);
1680                         } else
1681                                 *(t++) = *f;
1682                         break;
1683                 }
1684
1685         *t = 0;
1686
1687         return r;
1688 }
1689
1690 char *cunescape_length(const char *s, size_t length) {
1691         char *r, *t;
1692         const char *f;
1693
1694         assert(s);
1695
1696         /* Undoes C style string escaping */
1697
1698         if (!(r = new(char, length+1)))
1699                 return r;
1700
1701         for (f = s, t = r; f < s + length; f++) {
1702
1703                 if (*f != '\\') {
1704                         *(t++) = *f;
1705                         continue;
1706                 }
1707
1708                 f++;
1709
1710                 switch (*f) {
1711
1712                 case 'a':
1713                         *(t++) = '\a';
1714                         break;
1715                 case 'b':
1716                         *(t++) = '\b';
1717                         break;
1718                 case 'f':
1719                         *(t++) = '\f';
1720                         break;
1721                 case 'n':
1722                         *(t++) = '\n';
1723                         break;
1724                 case 'r':
1725                         *(t++) = '\r';
1726                         break;
1727                 case 't':
1728                         *(t++) = '\t';
1729                         break;
1730                 case 'v':
1731                         *(t++) = '\v';
1732                         break;
1733                 case '\\':
1734                         *(t++) = '\\';
1735                         break;
1736                 case '"':
1737                         *(t++) = '"';
1738                         break;
1739                 case '\'':
1740                         *(t++) = '\'';
1741                         break;
1742
1743                 case 's':
1744                         /* This is an extension of the XDG syntax files */
1745                         *(t++) = ' ';
1746                         break;
1747
1748                 case 'x': {
1749                         /* hexadecimal encoding */
1750                         int a, b;
1751
1752                         if ((a = unhexchar(f[1])) < 0 ||
1753                             (b = unhexchar(f[2])) < 0) {
1754                                 /* Invalid escape code, let's take it literal then */
1755                                 *(t++) = '\\';
1756                                 *(t++) = 'x';
1757                         } else {
1758                                 *(t++) = (char) ((a << 4) | b);
1759                                 f += 2;
1760                         }
1761
1762                         break;
1763                 }
1764
1765                 case '0':
1766                 case '1':
1767                 case '2':
1768                 case '3':
1769                 case '4':
1770                 case '5':
1771                 case '6':
1772                 case '7': {
1773                         /* octal encoding */
1774                         int a, b, c;
1775
1776                         if ((a = unoctchar(f[0])) < 0 ||
1777                             (b = unoctchar(f[1])) < 0 ||
1778                             (c = unoctchar(f[2])) < 0) {
1779                                 /* Invalid escape code, let's take it literal then */
1780                                 *(t++) = '\\';
1781                                 *(t++) = f[0];
1782                         } else {
1783                                 *(t++) = (char) ((a << 6) | (b << 3) | c);
1784                                 f += 2;
1785                         }
1786
1787                         break;
1788                 }
1789
1790                 case 0:
1791                         /* premature end of string.*/
1792                         *(t++) = '\\';
1793                         goto finish;
1794
1795                 default:
1796                         /* Invalid escape code, let's take it literal then */
1797                         *(t++) = '\\';
1798                         *(t++) = *f;
1799                         break;
1800                 }
1801         }
1802
1803 finish:
1804         *t = 0;
1805         return r;
1806 }
1807
1808 char *cunescape(const char *s) {
1809         return cunescape_length(s, strlen(s));
1810 }
1811
1812 char *xescape(const char *s, const char *bad) {
1813         char *r, *t;
1814         const char *f;
1815
1816         /* Escapes all chars in bad, in addition to \ and all special
1817          * chars, in \xFF style escaping. May be reversed with
1818          * cunescape. */
1819
1820         if (!(r = new(char, strlen(s)*4+1)))
1821                 return NULL;
1822
1823         for (f = s, t = r; *f; f++) {
1824
1825                 if ((*f < ' ') || (*f >= 127) ||
1826                     (*f == '\\') || strchr(bad, *f)) {
1827                         *(t++) = '\\';
1828                         *(t++) = 'x';
1829                         *(t++) = hexchar(*f >> 4);
1830                         *(t++) = hexchar(*f);
1831                 } else
1832                         *(t++) = *f;
1833         }
1834
1835         *t = 0;
1836
1837         return r;
1838 }
1839
1840 char *bus_path_escape(const char *s) {
1841         char *r, *t;
1842         const char *f;
1843
1844         assert(s);
1845
1846         /* Escapes all chars that D-Bus' object path cannot deal
1847          * with. Can be reverse with bus_path_unescape() */
1848
1849         if (!(r = new(char, strlen(s)*3+1)))
1850                 return NULL;
1851
1852         for (f = s, t = r; *f; f++) {
1853
1854                 if (!(*f >= 'A' && *f <= 'Z') &&
1855                     !(*f >= 'a' && *f <= 'z') &&
1856                     !(*f >= '0' && *f <= '9')) {
1857                         *(t++) = '_';
1858                         *(t++) = hexchar(*f >> 4);
1859                         *(t++) = hexchar(*f);
1860                 } else
1861                         *(t++) = *f;
1862         }
1863
1864         *t = 0;
1865
1866         return r;
1867 }
1868
1869 char *bus_path_unescape(const char *f) {
1870         char *r, *t;
1871
1872         assert(f);
1873
1874         if (!(r = strdup(f)))
1875                 return NULL;
1876
1877         for (t = r; *f; f++) {
1878
1879                 if (*f == '_') {
1880                         int a, b;
1881
1882                         if ((a = unhexchar(f[1])) < 0 ||
1883                             (b = unhexchar(f[2])) < 0) {
1884                                 /* Invalid escape code, let's take it literal then */
1885                                 *(t++) = '_';
1886                         } else {
1887                                 *(t++) = (char) ((a << 4) | b);
1888                                 f += 2;
1889                         }
1890                 } else
1891                         *(t++) = *f;
1892         }
1893
1894         *t = 0;
1895
1896         return r;
1897 }
1898
1899 char *path_kill_slashes(char *path) {
1900         char *f, *t;
1901         bool slash = false;
1902
1903         /* Removes redundant inner and trailing slashes. Modifies the
1904          * passed string in-place.
1905          *
1906          * ///foo///bar/ becomes /foo/bar
1907          */
1908
1909         for (f = path, t = path; *f; f++) {
1910
1911                 if (*f == '/') {
1912                         slash = true;
1913                         continue;
1914                 }
1915
1916                 if (slash) {
1917                         slash = false;
1918                         *(t++) = '/';
1919                 }
1920
1921                 *(t++) = *f;
1922         }
1923
1924         /* Special rule, if we are talking of the root directory, a
1925         trailing slash is good */
1926
1927         if (t == path && slash)
1928                 *(t++) = '/';
1929
1930         *t = 0;
1931         return path;
1932 }
1933
1934 bool path_startswith(const char *path, const char *prefix) {
1935         assert(path);
1936         assert(prefix);
1937
1938         if ((path[0] == '/') != (prefix[0] == '/'))
1939                 return false;
1940
1941         for (;;) {
1942                 size_t a, b;
1943
1944                 path += strspn(path, "/");
1945                 prefix += strspn(prefix, "/");
1946
1947                 if (*prefix == 0)
1948                         return true;
1949
1950                 if (*path == 0)
1951                         return false;
1952
1953                 a = strcspn(path, "/");
1954                 b = strcspn(prefix, "/");
1955
1956                 if (a != b)
1957                         return false;
1958
1959                 if (memcmp(path, prefix, a) != 0)
1960                         return false;
1961
1962                 path += a;
1963                 prefix += b;
1964         }
1965 }
1966
1967 bool path_equal(const char *a, const char *b) {
1968         assert(a);
1969         assert(b);
1970
1971         if ((a[0] == '/') != (b[0] == '/'))
1972                 return false;
1973
1974         for (;;) {
1975                 size_t j, k;
1976
1977                 a += strspn(a, "/");
1978                 b += strspn(b, "/");
1979
1980                 if (*a == 0 && *b == 0)
1981                         return true;
1982
1983                 if (*a == 0 || *b == 0)
1984                         return false;
1985
1986                 j = strcspn(a, "/");
1987                 k = strcspn(b, "/");
1988
1989                 if (j != k)
1990                         return false;
1991
1992                 if (memcmp(a, b, j) != 0)
1993                         return false;
1994
1995                 a += j;
1996                 b += k;
1997         }
1998 }
1999
2000 char *ascii_strlower(char *t) {
2001         char *p;
2002
2003         assert(t);
2004
2005         for (p = t; *p; p++)
2006                 if (*p >= 'A' && *p <= 'Z')
2007                         *p = *p - 'A' + 'a';
2008
2009         return t;
2010 }
2011
2012 bool ignore_file(const char *filename) {
2013         assert(filename);
2014
2015         return
2016                 filename[0] == '.' ||
2017                 streq(filename, "lost+found") ||
2018                 streq(filename, "aquota.user") ||
2019                 streq(filename, "aquota.group") ||
2020                 endswith(filename, "~") ||
2021                 endswith(filename, ".rpmnew") ||
2022                 endswith(filename, ".rpmsave") ||
2023                 endswith(filename, ".rpmorig") ||
2024                 endswith(filename, ".dpkg-old") ||
2025                 endswith(filename, ".dpkg-new") ||
2026                 endswith(filename, ".swp");
2027 }
2028
2029 int fd_nonblock(int fd, bool nonblock) {
2030         int flags;
2031
2032         assert(fd >= 0);
2033
2034         if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
2035                 return -errno;
2036
2037         if (nonblock)
2038                 flags |= O_NONBLOCK;
2039         else
2040                 flags &= ~O_NONBLOCK;
2041
2042         if (fcntl(fd, F_SETFL, flags) < 0)
2043                 return -errno;
2044
2045         return 0;
2046 }
2047
2048 int fd_cloexec(int fd, bool cloexec) {
2049         int flags;
2050
2051         assert(fd >= 0);
2052
2053         if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
2054                 return -errno;
2055
2056         if (cloexec)
2057                 flags |= FD_CLOEXEC;
2058         else
2059                 flags &= ~FD_CLOEXEC;
2060
2061         if (fcntl(fd, F_SETFD, flags) < 0)
2062                 return -errno;
2063
2064         return 0;
2065 }
2066
2067 int close_all_fds(const int except[], unsigned n_except) {
2068         DIR *d;
2069         struct dirent *de;
2070         int r = 0;
2071
2072         if (!(d = opendir("/proc/self/fd")))
2073                 return -errno;
2074
2075         while ((de = readdir(d))) {
2076                 int fd = -1;
2077
2078                 if (ignore_file(de->d_name))
2079                         continue;
2080
2081                 if (safe_atoi(de->d_name, &fd) < 0)
2082                         /* Let's better ignore this, just in case */
2083                         continue;
2084
2085                 if (fd < 3)
2086                         continue;
2087
2088                 if (fd == dirfd(d))
2089                         continue;
2090
2091                 if (except) {
2092                         bool found;
2093                         unsigned i;
2094
2095                         found = false;
2096                         for (i = 0; i < n_except; i++)
2097                                 if (except[i] == fd) {
2098                                         found = true;
2099                                         break;
2100                                 }
2101
2102                         if (found)
2103                                 continue;
2104                 }
2105
2106                 if (close_nointr(fd) < 0) {
2107                         /* Valgrind has its own FD and doesn't want to have it closed */
2108                         if (errno != EBADF && r == 0)
2109                                 r = -errno;
2110                 }
2111         }
2112
2113         closedir(d);
2114         return r;
2115 }
2116
2117 bool chars_intersect(const char *a, const char *b) {
2118         const char *p;
2119
2120         /* Returns true if any of the chars in a are in b. */
2121         for (p = a; *p; p++)
2122                 if (strchr(b, *p))
2123                         return true;
2124
2125         return false;
2126 }
2127
2128 char *format_timestamp(char *buf, size_t l, usec_t t) {
2129         struct tm tm;
2130         time_t sec;
2131
2132         assert(buf);
2133         assert(l > 0);
2134
2135         if (t <= 0)
2136                 return NULL;
2137
2138         sec = (time_t) (t / USEC_PER_SEC);
2139
2140         if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0)
2141                 return NULL;
2142
2143         return buf;
2144 }
2145
2146 char *format_timestamp_pretty(char *buf, size_t l, usec_t t) {
2147         usec_t n, d;
2148
2149         n = now(CLOCK_REALTIME);
2150
2151         if (t <= 0 || t > n || t + USEC_PER_DAY*7 <= t)
2152                 return NULL;
2153
2154         d = n - t;
2155
2156         if (d >= USEC_PER_YEAR)
2157                 snprintf(buf, l, "%llu years and %llu months ago",
2158                          (unsigned long long) (d / USEC_PER_YEAR),
2159                          (unsigned long long) ((d % USEC_PER_YEAR) / USEC_PER_MONTH));
2160         else if (d >= USEC_PER_MONTH)
2161                 snprintf(buf, l, "%llu months and %llu days ago",
2162                          (unsigned long long) (d / USEC_PER_MONTH),
2163                          (unsigned long long) ((d % USEC_PER_MONTH) / USEC_PER_DAY));
2164         else if (d >= USEC_PER_WEEK)
2165                 snprintf(buf, l, "%llu weeks and %llu days ago",
2166                          (unsigned long long) (d / USEC_PER_WEEK),
2167                          (unsigned long long) ((d % USEC_PER_WEEK) / USEC_PER_DAY));
2168         else if (d >= 2*USEC_PER_DAY)
2169                 snprintf(buf, l, "%llu days ago", (unsigned long long) (d / USEC_PER_DAY));
2170         else if (d >= 25*USEC_PER_HOUR)
2171                 snprintf(buf, l, "1 day and %lluh ago",
2172                          (unsigned long long) ((d - USEC_PER_DAY) / USEC_PER_HOUR));
2173         else if (d >= 6*USEC_PER_HOUR)
2174                 snprintf(buf, l, "%lluh ago",
2175                          (unsigned long long) (d / USEC_PER_HOUR));
2176         else if (d >= USEC_PER_HOUR)
2177                 snprintf(buf, l, "%lluh %llumin ago",
2178                          (unsigned long long) (d / USEC_PER_HOUR),
2179                          (unsigned long long) ((d % USEC_PER_HOUR) / USEC_PER_MINUTE));
2180         else if (d >= 5*USEC_PER_MINUTE)
2181                 snprintf(buf, l, "%llumin ago",
2182                          (unsigned long long) (d / USEC_PER_MINUTE));
2183         else if (d >= USEC_PER_MINUTE)
2184                 snprintf(buf, l, "%llumin %llus ago",
2185                          (unsigned long long) (d / USEC_PER_MINUTE),
2186                          (unsigned long long) ((d % USEC_PER_MINUTE) / USEC_PER_SEC));
2187         else if (d >= USEC_PER_SEC)
2188                 snprintf(buf, l, "%llus ago",
2189                          (unsigned long long) (d / USEC_PER_SEC));
2190         else if (d >= USEC_PER_MSEC)
2191                 snprintf(buf, l, "%llums ago",
2192                          (unsigned long long) (d / USEC_PER_MSEC));
2193         else if (d > 0)
2194                 snprintf(buf, l, "%lluus ago",
2195                          (unsigned long long) d);
2196         else
2197                 snprintf(buf, l, "now");
2198
2199         buf[l-1] = 0;
2200         return buf;
2201 }
2202
2203 char *format_timespan(char *buf, size_t l, usec_t t) {
2204         static const struct {
2205                 const char *suffix;
2206                 usec_t usec;
2207         } table[] = {
2208                 { "w", USEC_PER_WEEK },
2209                 { "d", USEC_PER_DAY },
2210                 { "h", USEC_PER_HOUR },
2211                 { "min", USEC_PER_MINUTE },
2212                 { "s", USEC_PER_SEC },
2213                 { "ms", USEC_PER_MSEC },
2214                 { "us", 1 },
2215         };
2216
2217         unsigned i;
2218         char *p = buf;
2219
2220         assert(buf);
2221         assert(l > 0);
2222
2223         if (t == (usec_t) -1)
2224                 return NULL;
2225
2226         if (t == 0) {
2227                 snprintf(p, l, "0");
2228                 p[l-1] = 0;
2229                 return p;
2230         }
2231
2232         /* The result of this function can be parsed with parse_usec */
2233
2234         for (i = 0; i < ELEMENTSOF(table); i++) {
2235                 int k;
2236                 size_t n;
2237
2238                 if (t < table[i].usec)
2239                         continue;
2240
2241                 if (l <= 1)
2242                         break;
2243
2244                 k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix);
2245                 n = MIN((size_t) k, l);
2246
2247                 l -= n;
2248                 p += n;
2249
2250                 t %= table[i].usec;
2251         }
2252
2253         *p = 0;
2254
2255         return buf;
2256 }
2257
2258 bool fstype_is_network(const char *fstype) {
2259         static const char * const table[] = {
2260                 "cifs",
2261                 "smbfs",
2262                 "ncpfs",
2263                 "nfs",
2264                 "nfs4",
2265                 "gfs",
2266                 "gfs2"
2267         };
2268
2269         unsigned i;
2270
2271         for (i = 0; i < ELEMENTSOF(table); i++)
2272                 if (streq(table[i], fstype))
2273                         return true;
2274
2275         return false;
2276 }
2277
2278 int chvt(int vt) {
2279         int fd, r = 0;
2280
2281         if ((fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
2282                 return -errno;
2283
2284         if (vt < 0) {
2285                 int tiocl[2] = {
2286                         TIOCL_GETKMSGREDIRECT,
2287                         0
2288                 };
2289
2290                 if (ioctl(fd, TIOCLINUX, tiocl) < 0)
2291                         return -errno;
2292
2293                 vt = tiocl[0] <= 0 ? 1 : tiocl[0];
2294         }
2295
2296         if (ioctl(fd, VT_ACTIVATE, vt) < 0)
2297                 r = -errno;
2298
2299         close_nointr_nofail(r);
2300         return r;
2301 }
2302
2303 int read_one_char(FILE *f, char *ret, bool *need_nl) {
2304         struct termios old_termios, new_termios;
2305         char c;
2306         char line[LINE_MAX];
2307
2308         assert(f);
2309         assert(ret);
2310
2311         if (tcgetattr(fileno(f), &old_termios) >= 0) {
2312                 new_termios = old_termios;
2313
2314                 new_termios.c_lflag &= ~ICANON;
2315                 new_termios.c_cc[VMIN] = 1;
2316                 new_termios.c_cc[VTIME] = 0;
2317
2318                 if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
2319                         size_t k;
2320
2321                         k = fread(&c, 1, 1, f);
2322
2323                         tcsetattr(fileno(f), TCSADRAIN, &old_termios);
2324
2325                         if (k <= 0)
2326                                 return -EIO;
2327
2328                         if (need_nl)
2329                                 *need_nl = c != '\n';
2330
2331                         *ret = c;
2332                         return 0;
2333                 }
2334         }
2335
2336         if (!(fgets(line, sizeof(line), f)))
2337                 return -EIO;
2338
2339         truncate_nl(line);
2340
2341         if (strlen(line) != 1)
2342                 return -EBADMSG;
2343
2344         if (need_nl)
2345                 *need_nl = false;
2346
2347         *ret = line[0];
2348         return 0;
2349 }
2350
2351 int ask(char *ret, const char *replies, const char *text, ...) {
2352         bool on_tty;
2353
2354         assert(ret);
2355         assert(replies);
2356         assert(text);
2357
2358         on_tty = isatty(STDOUT_FILENO);
2359
2360         for (;;) {
2361                 va_list ap;
2362                 char c;
2363                 int r;
2364                 bool need_nl = true;
2365
2366                 if (on_tty)
2367                         fputs("\x1B[1m", stdout);
2368
2369                 va_start(ap, text);
2370                 vprintf(text, ap);
2371                 va_end(ap);
2372
2373                 if (on_tty)
2374                         fputs("\x1B[0m", stdout);
2375
2376                 fflush(stdout);
2377
2378                 if ((r = read_one_char(stdin, &c, &need_nl)) < 0) {
2379
2380                         if (r == -EBADMSG) {
2381                                 puts("Bad input, please try again.");
2382                                 continue;
2383                         }
2384
2385                         putchar('\n');
2386                         return r;
2387                 }
2388
2389                 if (need_nl)
2390                         putchar('\n');
2391
2392                 if (strchr(replies, c)) {
2393                         *ret = c;
2394                         return 0;
2395                 }
2396
2397                 puts("Read unexpected character, please try again.");
2398         }
2399 }
2400
2401 int reset_terminal_fd(int fd) {
2402         struct termios termios;
2403         int r = 0;
2404         long arg;
2405
2406         /* Set terminal to some sane defaults */
2407
2408         assert(fd >= 0);
2409
2410         /* We leave locked terminal attributes untouched, so that
2411          * Plymouth may set whatever it wants to set, and we don't
2412          * interfere with that. */
2413
2414         /* Disable exclusive mode, just in case */
2415         ioctl(fd, TIOCNXCL);
2416
2417         /* Enable console unicode mode */
2418         arg = K_UNICODE;
2419         ioctl(fd, KDSKBMODE, &arg);
2420
2421         if (tcgetattr(fd, &termios) < 0) {
2422                 r = -errno;
2423                 goto finish;
2424         }
2425
2426         /* We only reset the stuff that matters to the software. How
2427          * hardware is set up we don't touch assuming that somebody
2428          * else will do that for us */
2429
2430         termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
2431         termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
2432         termios.c_oflag |= ONLCR;
2433         termios.c_cflag |= CREAD;
2434         termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
2435
2436         termios.c_cc[VINTR]    =   03;  /* ^C */
2437         termios.c_cc[VQUIT]    =  034;  /* ^\ */
2438         termios.c_cc[VERASE]   = 0177;
2439         termios.c_cc[VKILL]    =  025;  /* ^X */
2440         termios.c_cc[VEOF]     =   04;  /* ^D */
2441         termios.c_cc[VSTART]   =  021;  /* ^Q */
2442         termios.c_cc[VSTOP]    =  023;  /* ^S */
2443         termios.c_cc[VSUSP]    =  032;  /* ^Z */
2444         termios.c_cc[VLNEXT]   =  026;  /* ^V */
2445         termios.c_cc[VWERASE]  =  027;  /* ^W */
2446         termios.c_cc[VREPRINT] =  022;  /* ^R */
2447         termios.c_cc[VEOL]     =    0;
2448         termios.c_cc[VEOL2]    =    0;
2449
2450         termios.c_cc[VTIME]  = 0;
2451         termios.c_cc[VMIN]   = 1;
2452
2453         if (tcsetattr(fd, TCSANOW, &termios) < 0)
2454                 r = -errno;
2455
2456 finish:
2457         /* Just in case, flush all crap out */
2458         tcflush(fd, TCIOFLUSH);
2459
2460         return r;
2461 }
2462
2463 int reset_terminal(const char *name) {
2464         int fd, r;
2465
2466         fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
2467         if (fd < 0)
2468                 return fd;
2469
2470         r = reset_terminal_fd(fd);
2471         close_nointr_nofail(fd);
2472
2473         return r;
2474 }
2475
2476 int open_terminal(const char *name, int mode) {
2477         int fd, r;
2478         unsigned c = 0;
2479
2480         /*
2481          * If a TTY is in the process of being closed opening it might
2482          * cause EIO. This is horribly awful, but unlikely to be
2483          * changed in the kernel. Hence we work around this problem by
2484          * retrying a couple of times.
2485          *
2486          * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245
2487          */
2488
2489         for (;;) {
2490                 if ((fd = open(name, mode)) >= 0)
2491                         break;
2492
2493                 if (errno != EIO)
2494                         return -errno;
2495
2496                 if (c >= 20)
2497                         return -errno;
2498
2499                 usleep(50 * USEC_PER_MSEC);
2500                 c++;
2501         }
2502
2503         if (fd < 0)
2504                 return -errno;
2505
2506         if ((r = isatty(fd)) < 0) {
2507                 close_nointr_nofail(fd);
2508                 return -errno;
2509         }
2510
2511         if (!r) {
2512                 close_nointr_nofail(fd);
2513                 return -ENOTTY;
2514         }
2515
2516         return fd;
2517 }
2518
2519 int flush_fd(int fd) {
2520         struct pollfd pollfd;
2521
2522         zero(pollfd);
2523         pollfd.fd = fd;
2524         pollfd.events = POLLIN;
2525
2526         for (;;) {
2527                 char buf[LINE_MAX];
2528                 ssize_t l;
2529                 int r;
2530
2531                 if ((r = poll(&pollfd, 1, 0)) < 0) {
2532
2533                         if (errno == EINTR)
2534                                 continue;
2535
2536                         return -errno;
2537                 }
2538
2539                 if (r == 0)
2540                         return 0;
2541
2542                 if ((l = read(fd, buf, sizeof(buf))) < 0) {
2543
2544                         if (errno == EINTR)
2545                                 continue;
2546
2547                         if (errno == EAGAIN)
2548                                 return 0;
2549
2550                         return -errno;
2551                 }
2552
2553                 if (l <= 0)
2554                         return 0;
2555         }
2556 }
2557
2558 int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) {
2559         int fd = -1, notify = -1, r, wd = -1;
2560
2561         assert(name);
2562
2563         /* We use inotify to be notified when the tty is closed. We
2564          * create the watch before checking if we can actually acquire
2565          * it, so that we don't lose any event.
2566          *
2567          * Note: strictly speaking this actually watches for the
2568          * device being closed, it does *not* really watch whether a
2569          * tty loses its controlling process. However, unless some
2570          * rogue process uses TIOCNOTTY on /dev/tty *after* closing
2571          * its tty otherwise this will not become a problem. As long
2572          * as the administrator makes sure not configure any service
2573          * on the same tty as an untrusted user this should not be a
2574          * problem. (Which he probably should not do anyway.) */
2575
2576         if (!fail && !force) {
2577                 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
2578                         r = -errno;
2579                         goto fail;
2580                 }
2581
2582                 if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) {
2583                         r = -errno;
2584                         goto fail;
2585                 }
2586         }
2587
2588         for (;;) {
2589                 if (notify >= 0)
2590                         if ((r = flush_fd(notify)) < 0)
2591                                 goto fail;
2592
2593                 /* We pass here O_NOCTTY only so that we can check the return
2594                  * value TIOCSCTTY and have a reliable way to figure out if we
2595                  * successfully became the controlling process of the tty */
2596                 if ((fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
2597                         return fd;
2598
2599                 /* First, try to get the tty */
2600                 r = ioctl(fd, TIOCSCTTY, force);
2601
2602                 /* Sometimes it makes sense to ignore TIOCSCTTY
2603                  * returning EPERM, i.e. when very likely we already
2604                  * are have this controlling terminal. */
2605                 if (r < 0 && errno == EPERM && ignore_tiocstty_eperm)
2606                         r = 0;
2607
2608                 if (r < 0 && (force || fail || errno != EPERM)) {
2609                         r = -errno;
2610                         goto fail;
2611                 }
2612
2613                 if (r >= 0)
2614                         break;
2615
2616                 assert(!fail);
2617                 assert(!force);
2618                 assert(notify >= 0);
2619
2620                 for (;;) {
2621                         uint8_t inotify_buffer[sizeof(struct inotify_event) + FILENAME_MAX];
2622                         ssize_t l;
2623                         struct inotify_event *e;
2624
2625                         if ((l = read(notify, &inotify_buffer, sizeof(inotify_buffer))) < 0) {
2626
2627                                 if (errno == EINTR)
2628                                         continue;
2629
2630                                 r = -errno;
2631                                 goto fail;
2632                         }
2633
2634                         e = (struct inotify_event*) inotify_buffer;
2635
2636                         while (l > 0) {
2637                                 size_t step;
2638
2639                                 if (e->wd != wd || !(e->mask & IN_CLOSE)) {
2640                                         r = -EIO;
2641                                         goto fail;
2642                                 }
2643
2644                                 step = sizeof(struct inotify_event) + e->len;
2645                                 assert(step <= (size_t) l);
2646
2647                                 e = (struct inotify_event*) ((uint8_t*) e + step);
2648                                 l -= step;
2649                         }
2650
2651                         break;
2652                 }
2653
2654                 /* We close the tty fd here since if the old session
2655                  * ended our handle will be dead. It's important that
2656                  * we do this after sleeping, so that we don't enter
2657                  * an endless loop. */
2658                 close_nointr_nofail(fd);
2659         }
2660
2661         if (notify >= 0)
2662                 close_nointr_nofail(notify);
2663
2664         if ((r = reset_terminal_fd(fd)) < 0)
2665                 log_warning("Failed to reset terminal: %s", strerror(-r));
2666
2667         return fd;
2668
2669 fail:
2670         if (fd >= 0)
2671                 close_nointr_nofail(fd);
2672
2673         if (notify >= 0)
2674                 close_nointr_nofail(notify);
2675
2676         return r;
2677 }
2678
2679 int release_terminal(void) {
2680         int r = 0, fd;
2681         struct sigaction sa_old, sa_new;
2682
2683         if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC)) < 0)
2684                 return -errno;
2685
2686         /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
2687          * by our own TIOCNOTTY */
2688
2689         zero(sa_new);
2690         sa_new.sa_handler = SIG_IGN;
2691         sa_new.sa_flags = SA_RESTART;
2692         assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
2693
2694         if (ioctl(fd, TIOCNOTTY) < 0)
2695                 r = -errno;
2696
2697         assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
2698
2699         close_nointr_nofail(fd);
2700         return r;
2701 }
2702
2703 int sigaction_many(const struct sigaction *sa, ...) {
2704         va_list ap;
2705         int r = 0, sig;
2706
2707         va_start(ap, sa);
2708         while ((sig = va_arg(ap, int)) > 0)
2709                 if (sigaction(sig, sa, NULL) < 0)
2710                         r = -errno;
2711         va_end(ap);
2712
2713         return r;
2714 }
2715
2716 int ignore_signals(int sig, ...) {
2717         struct sigaction sa;
2718         va_list ap;
2719         int r = 0;
2720
2721         zero(sa);
2722         sa.sa_handler = SIG_IGN;
2723         sa.sa_flags = SA_RESTART;
2724
2725         if (sigaction(sig, &sa, NULL) < 0)
2726                 r = -errno;
2727
2728         va_start(ap, sig);
2729         while ((sig = va_arg(ap, int)) > 0)
2730                 if (sigaction(sig, &sa, NULL) < 0)
2731                         r = -errno;
2732         va_end(ap);
2733
2734         return r;
2735 }
2736
2737 int default_signals(int sig, ...) {
2738         struct sigaction sa;
2739         va_list ap;
2740         int r = 0;
2741
2742         zero(sa);
2743         sa.sa_handler = SIG_DFL;
2744         sa.sa_flags = SA_RESTART;
2745
2746         if (sigaction(sig, &sa, NULL) < 0)
2747                 r = -errno;
2748
2749         va_start(ap, sig);
2750         while ((sig = va_arg(ap, int)) > 0)
2751                 if (sigaction(sig, &sa, NULL) < 0)
2752                         r = -errno;
2753         va_end(ap);
2754
2755         return r;
2756 }
2757
2758 int close_pipe(int p[]) {
2759         int a = 0, b = 0;
2760
2761         assert(p);
2762
2763         if (p[0] >= 0) {
2764                 a = close_nointr(p[0]);
2765                 p[0] = -1;
2766         }
2767
2768         if (p[1] >= 0) {
2769                 b = close_nointr(p[1]);
2770                 p[1] = -1;
2771         }
2772
2773         return a < 0 ? a : b;
2774 }
2775
2776 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
2777         uint8_t *p;
2778         ssize_t n = 0;
2779
2780         assert(fd >= 0);
2781         assert(buf);
2782
2783         p = buf;
2784
2785         while (nbytes > 0) {
2786                 ssize_t k;
2787
2788                 if ((k = read(fd, p, nbytes)) <= 0) {
2789
2790                         if (k < 0 && errno == EINTR)
2791                                 continue;
2792
2793                         if (k < 0 && errno == EAGAIN && do_poll) {
2794                                 struct pollfd pollfd;
2795
2796                                 zero(pollfd);
2797                                 pollfd.fd = fd;
2798                                 pollfd.events = POLLIN;
2799
2800                                 if (poll(&pollfd, 1, -1) < 0) {
2801                                         if (errno == EINTR)
2802                                                 continue;
2803
2804                                         return n > 0 ? n : -errno;
2805                                 }
2806
2807                                 if (pollfd.revents != POLLIN)
2808                                         return n > 0 ? n : -EIO;
2809
2810                                 continue;
2811                         }
2812
2813                         return n > 0 ? n : (k < 0 ? -errno : 0);
2814                 }
2815
2816                 p += k;
2817                 nbytes -= k;
2818                 n += k;
2819         }
2820
2821         return n;
2822 }
2823
2824 ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
2825         const uint8_t *p;
2826         ssize_t n = 0;
2827
2828         assert(fd >= 0);
2829         assert(buf);
2830
2831         p = buf;
2832
2833         while (nbytes > 0) {
2834                 ssize_t k;
2835
2836                 if ((k = write(fd, p, nbytes)) <= 0) {
2837
2838                         if (k < 0 && errno == EINTR)
2839                                 continue;
2840
2841                         if (k < 0 && errno == EAGAIN && do_poll) {
2842                                 struct pollfd pollfd;
2843
2844                                 zero(pollfd);
2845                                 pollfd.fd = fd;
2846                                 pollfd.events = POLLOUT;
2847
2848                                 if (poll(&pollfd, 1, -1) < 0) {
2849                                         if (errno == EINTR)
2850                                                 continue;
2851
2852                                         return n > 0 ? n : -errno;
2853                                 }
2854
2855                                 if (pollfd.revents != POLLOUT)
2856                                         return n > 0 ? n : -EIO;
2857
2858                                 continue;
2859                         }
2860
2861                         return n > 0 ? n : (k < 0 ? -errno : 0);
2862                 }
2863
2864                 p += k;
2865                 nbytes -= k;
2866                 n += k;
2867         }
2868
2869         return n;
2870 }
2871
2872 int path_is_mount_point(const char *t) {
2873         struct stat a, b;
2874         char *parent;
2875         int r;
2876
2877         if (lstat(t, &a) < 0) {
2878                 if (errno == ENOENT)
2879                         return 0;
2880
2881                 return -errno;
2882         }
2883
2884         if ((r = parent_of_path(t, &parent)) < 0)
2885                 return r;
2886
2887         r = lstat(parent, &b);
2888         free(parent);
2889
2890         if (r < 0)
2891                 return -errno;
2892
2893         return a.st_dev != b.st_dev;
2894 }
2895
2896 int parse_usec(const char *t, usec_t *usec) {
2897         static const struct {
2898                 const char *suffix;
2899                 usec_t usec;
2900         } table[] = {
2901                 { "sec", USEC_PER_SEC },
2902                 { "s", USEC_PER_SEC },
2903                 { "min", USEC_PER_MINUTE },
2904                 { "hr", USEC_PER_HOUR },
2905                 { "h", USEC_PER_HOUR },
2906                 { "d", USEC_PER_DAY },
2907                 { "w", USEC_PER_WEEK },
2908                 { "msec", USEC_PER_MSEC },
2909                 { "ms", USEC_PER_MSEC },
2910                 { "m", USEC_PER_MINUTE },
2911                 { "usec", 1ULL },
2912                 { "us", 1ULL },
2913                 { "", USEC_PER_SEC },
2914         };
2915
2916         const char *p;
2917         usec_t r = 0;
2918
2919         assert(t);
2920         assert(usec);
2921
2922         p = t;
2923         do {
2924                 long long l;
2925                 char *e;
2926                 unsigned i;
2927
2928                 errno = 0;
2929                 l = strtoll(p, &e, 10);
2930
2931                 if (errno != 0)
2932                         return -errno;
2933
2934                 if (l < 0)
2935                         return -ERANGE;
2936
2937                 if (e == p)
2938                         return -EINVAL;
2939
2940                 e += strspn(e, WHITESPACE);
2941
2942                 for (i = 0; i < ELEMENTSOF(table); i++)
2943                         if (startswith(e, table[i].suffix)) {
2944                                 r += (usec_t) l * table[i].usec;
2945                                 p = e + strlen(table[i].suffix);
2946                                 break;
2947                         }
2948
2949                 if (i >= ELEMENTSOF(table))
2950                         return -EINVAL;
2951
2952         } while (*p != 0);
2953
2954         *usec = r;
2955
2956         return 0;
2957 }
2958
2959 int make_stdio(int fd) {
2960         int r, s, t;
2961
2962         assert(fd >= 0);
2963
2964         r = dup2(fd, STDIN_FILENO);
2965         s = dup2(fd, STDOUT_FILENO);
2966         t = dup2(fd, STDERR_FILENO);
2967
2968         if (fd >= 3)
2969                 close_nointr_nofail(fd);
2970
2971         if (r < 0 || s < 0 || t < 0)
2972                 return -errno;
2973
2974         fd_cloexec(STDIN_FILENO, false);
2975         fd_cloexec(STDOUT_FILENO, false);
2976         fd_cloexec(STDERR_FILENO, false);
2977
2978         return 0;
2979 }
2980
2981 int make_null_stdio(void) {
2982         int null_fd;
2983
2984         if ((null_fd = open("/dev/null", O_RDWR|O_NOCTTY)) < 0)
2985                 return -errno;
2986
2987         return make_stdio(null_fd);
2988 }
2989
2990 bool is_device_path(const char *path) {
2991
2992         /* Returns true on paths that refer to a device, either in
2993          * sysfs or in /dev */
2994
2995         return
2996                 path_startswith(path, "/dev/") ||
2997                 path_startswith(path, "/sys/");
2998 }
2999
3000 int dir_is_empty(const char *path) {
3001         DIR *d;
3002         int r;
3003         struct dirent buf, *de;
3004
3005         if (!(d = opendir(path)))
3006                 return -errno;
3007
3008         for (;;) {
3009                 if ((r = readdir_r(d, &buf, &de)) > 0) {
3010                         r = -r;
3011                         break;
3012                 }
3013
3014                 if (!de) {
3015                         r = 1;
3016                         break;
3017                 }
3018
3019                 if (!ignore_file(de->d_name)) {
3020                         r = 0;
3021                         break;
3022                 }
3023         }
3024
3025         closedir(d);
3026         return r;
3027 }
3028
3029 unsigned long long random_ull(void) {
3030         int fd;
3031         uint64_t ull;
3032         ssize_t r;
3033
3034         if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0)
3035                 goto fallback;
3036
3037         r = loop_read(fd, &ull, sizeof(ull), true);
3038         close_nointr_nofail(fd);
3039
3040         if (r != sizeof(ull))
3041                 goto fallback;
3042
3043         return ull;
3044
3045 fallback:
3046         return random() * RAND_MAX + random();
3047 }
3048
3049 void rename_process(const char name[8]) {
3050         assert(name);
3051
3052         prctl(PR_SET_NAME, name);
3053
3054         /* This is a like a poor man's setproctitle(). The string
3055          * passed should fit in 7 chars (i.e. the length of
3056          * "systemd") */
3057
3058         if (program_invocation_name)
3059                 strncpy(program_invocation_name, name, strlen(program_invocation_name));
3060
3061         if (saved_argc > 0) {
3062                 int i;
3063
3064                 if (saved_argv[0])
3065                         strncpy(saved_argv[0], name, strlen(saved_argv[0]));
3066
3067                 for (i = 1; i < saved_argc; i++) {
3068                         if (!saved_argv[i])
3069                                 break;
3070
3071                         memset(saved_argv[i], 0, strlen(saved_argv[i]));
3072                 }
3073         }
3074 }
3075
3076 void sigset_add_many(sigset_t *ss, ...) {
3077         va_list ap;
3078         int sig;
3079
3080         assert(ss);
3081
3082         va_start(ap, ss);
3083         while ((sig = va_arg(ap, int)) > 0)
3084                 assert_se(sigaddset(ss, sig) == 0);
3085         va_end(ap);
3086 }
3087
3088 char* gethostname_malloc(void) {
3089         struct utsname u;
3090
3091         assert_se(uname(&u) >= 0);
3092
3093         if (u.nodename[0])
3094                 return strdup(u.nodename);
3095
3096         return strdup(u.sysname);
3097 }
3098
3099 char* getlogname_malloc(void) {
3100         uid_t uid;
3101         long bufsize;
3102         char *buf, *name;
3103         struct passwd pwbuf, *pw = NULL;
3104         struct stat st;
3105
3106         if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
3107                 uid = st.st_uid;
3108         else
3109                 uid = getuid();
3110
3111         /* Shortcut things to avoid NSS lookups */
3112         if (uid == 0)
3113                 return strdup("root");
3114
3115         if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0)
3116                 bufsize = 4096;
3117
3118         if (!(buf = malloc(bufsize)))
3119                 return NULL;
3120
3121         if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) {
3122                 name = strdup(pw->pw_name);
3123                 free(buf);
3124                 return name;
3125         }
3126
3127         free(buf);
3128
3129         if (asprintf(&name, "%lu", (unsigned long) uid) < 0)
3130                 return NULL;
3131
3132         return name;
3133 }
3134
3135 int getttyname_malloc(int fd, char **r) {
3136         char path[PATH_MAX], *c;
3137         int k;
3138
3139         assert(r);
3140
3141         if ((k = ttyname_r(fd, path, sizeof(path))) != 0)
3142                 return -k;
3143
3144         char_array_0(path);
3145
3146         if (!(c = strdup(startswith(path, "/dev/") ? path + 5 : path)))
3147                 return -ENOMEM;
3148
3149         *r = c;
3150         return 0;
3151 }
3152
3153 int getttyname_harder(int fd, char **r) {
3154         int k;
3155         char *s;
3156
3157         if ((k = getttyname_malloc(fd, &s)) < 0)
3158                 return k;
3159
3160         if (streq(s, "tty")) {
3161                 free(s);
3162                 return get_ctty(0, NULL, r);
3163         }
3164
3165         *r = s;
3166         return 0;
3167 }
3168
3169 int get_ctty_devnr(pid_t pid, dev_t *d) {
3170         int k;
3171         char line[LINE_MAX], *p, *fn;
3172         unsigned long ttynr;
3173         FILE *f;
3174
3175         if (asprintf(&fn, "/proc/%lu/stat", (unsigned long) (pid <= 0 ? getpid() : pid)) < 0)
3176                 return -ENOMEM;
3177
3178         f = fopen(fn, "re");
3179         free(fn);
3180         if (!f)
3181                 return -errno;
3182
3183         if (!fgets(line, sizeof(line), f)) {
3184                 k = -errno;
3185                 fclose(f);
3186                 return k;
3187         }
3188
3189         fclose(f);
3190
3191         p = strrchr(line, ')');
3192         if (!p)
3193                 return -EIO;
3194
3195         p++;
3196
3197         if (sscanf(p, " "
3198                    "%*c "  /* state */
3199                    "%*d "  /* ppid */
3200                    "%*d "  /* pgrp */
3201                    "%*d "  /* session */
3202                    "%lu ", /* ttynr */
3203                    &ttynr) != 1)
3204                 return -EIO;
3205
3206         *d = (dev_t) ttynr;
3207         return 0;
3208 }
3209
3210 int get_ctty(pid_t pid, dev_t *_devnr, char **r) {
3211         int k;
3212         char fn[PATH_MAX], *s, *b, *p;
3213         dev_t devnr;
3214
3215         assert(r);
3216
3217         k = get_ctty_devnr(pid, &devnr);
3218         if (k < 0)
3219                 return k;
3220
3221         snprintf(fn, sizeof(fn), "/dev/char/%u:%u", major(devnr), minor(devnr));
3222         char_array_0(fn);
3223
3224         if ((k = readlink_malloc(fn, &s)) < 0) {
3225
3226                 if (k != -ENOENT)
3227                         return k;
3228
3229                 /* This is an ugly hack */
3230                 if (major(devnr) == 136) {
3231                         if (asprintf(&b, "pts/%lu", (unsigned long) minor(devnr)) < 0)
3232                                 return -ENOMEM;
3233
3234                         *r = b;
3235                         if (_devnr)
3236                                 *_devnr = devnr;
3237
3238                         return 0;
3239                 }
3240
3241                 /* Probably something like the ptys which have no
3242                  * symlink in /dev/char. Let's return something
3243                  * vaguely useful. */
3244
3245                 if (!(b = strdup(fn + 5)))
3246                         return -ENOMEM;
3247
3248                 *r = b;
3249                 if (_devnr)
3250                         *_devnr = devnr;
3251
3252                 return 0;
3253         }
3254
3255         if (startswith(s, "/dev/"))
3256                 p = s + 5;
3257         else if (startswith(s, "../"))
3258                 p = s + 3;
3259         else
3260                 p = s;
3261
3262         b = strdup(p);
3263         free(s);
3264
3265         if (!b)
3266                 return -ENOMEM;
3267
3268         *r = b;
3269         if (_devnr)
3270                 *_devnr = devnr;
3271
3272         return 0;
3273 }
3274
3275 static int rm_rf_children(int fd, bool only_dirs) {
3276         DIR *d;
3277         int ret = 0;
3278
3279         assert(fd >= 0);
3280
3281         /* This returns the first error we run into, but nevertheless
3282          * tries to go on */
3283
3284         if (!(d = fdopendir(fd))) {
3285                 close_nointr_nofail(fd);
3286
3287                 return errno == ENOENT ? 0 : -errno;
3288         }
3289
3290         for (;;) {
3291                 struct dirent buf, *de;
3292                 bool is_dir;
3293                 int r;
3294
3295                 if ((r = readdir_r(d, &buf, &de)) != 0) {
3296                         if (ret == 0)
3297                                 ret = -r;
3298                         break;
3299                 }
3300
3301                 if (!de)
3302                         break;
3303
3304                 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
3305                         continue;
3306
3307                 if (de->d_type == DT_UNKNOWN) {
3308                         struct stat st;
3309
3310                         if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3311                                 if (ret == 0 && errno != ENOENT)
3312                                         ret = -errno;
3313                                 continue;
3314                         }
3315
3316                         is_dir = S_ISDIR(st.st_mode);
3317                 } else
3318                         is_dir = de->d_type == DT_DIR;
3319
3320                 if (is_dir) {
3321                         int subdir_fd;
3322
3323                         if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
3324                                 if (ret == 0 && errno != ENOENT)
3325                                         ret = -errno;
3326                                 continue;
3327                         }
3328
3329                         if ((r = rm_rf_children(subdir_fd, only_dirs)) < 0) {
3330                                 if (ret == 0)
3331                                         ret = r;
3332                         }
3333
3334                         if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
3335                                 if (ret == 0 && errno != ENOENT)
3336                                         ret = -errno;
3337                         }
3338                 } else  if (!only_dirs) {
3339
3340                         if (unlinkat(fd, de->d_name, 0) < 0) {
3341                                 if (ret == 0 && errno != ENOENT)
3342                                         ret = -errno;
3343                         }
3344                 }
3345         }
3346
3347         closedir(d);
3348
3349         return ret;
3350 }
3351
3352 int rm_rf(const char *path, bool only_dirs, bool delete_root) {
3353         int fd;
3354         int r;
3355
3356         assert(path);
3357
3358         if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
3359
3360                 if (errno != ENOTDIR)
3361                         return -errno;
3362
3363                 if (delete_root && !only_dirs)
3364                         if (unlink(path) < 0)
3365                                 return -errno;
3366
3367                 return 0;
3368         }
3369
3370         r = rm_rf_children(fd, only_dirs);
3371
3372         if (delete_root)
3373                 if (rmdir(path) < 0) {
3374                         if (r == 0)
3375                                 r = -errno;
3376                 }
3377
3378         return r;
3379 }
3380
3381 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
3382         assert(path);
3383
3384         /* Under the assumption that we are running privileged we
3385          * first change the access mode and only then hand out
3386          * ownership to avoid a window where access is too open. */
3387
3388         if (chmod(path, mode) < 0)
3389                 return -errno;
3390
3391         if (chown(path, uid, gid) < 0)
3392                 return -errno;
3393
3394         return 0;
3395 }
3396
3397 cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
3398         cpu_set_t *r;
3399         unsigned n = 1024;
3400
3401         /* Allocates the cpuset in the right size */
3402
3403         for (;;) {
3404                 if (!(r = CPU_ALLOC(n)))
3405                         return NULL;
3406
3407                 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) {
3408                         CPU_ZERO_S(CPU_ALLOC_SIZE(n), r);
3409
3410                         if (ncpus)
3411                                 *ncpus = n;
3412
3413                         return r;
3414                 }
3415
3416                 CPU_FREE(r);
3417
3418                 if (errno != EINVAL)
3419                         return NULL;
3420
3421                 n *= 2;
3422         }
3423 }
3424
3425 void status_vprintf(const char *format, va_list ap) {
3426         char *s = NULL;
3427         int fd = -1;
3428
3429         assert(format);
3430
3431         /* This independent of logging, as status messages are
3432          * optional and go exclusively to the console. */
3433
3434         if (vasprintf(&s, format, ap) < 0)
3435                 goto finish;
3436
3437         if ((fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
3438                 goto finish;
3439
3440         write(fd, s, strlen(s));
3441
3442 finish:
3443         free(s);
3444
3445         if (fd >= 0)
3446                 close_nointr_nofail(fd);
3447 }
3448
3449 void status_printf(const char *format, ...) {
3450         va_list ap;
3451
3452         assert(format);
3453
3454         va_start(ap, format);
3455         status_vprintf(format, ap);
3456         va_end(ap);
3457 }
3458
3459 void status_welcome(void) {
3460         char *pretty_name = NULL, *ansi_color = NULL;
3461         const char *const_pretty = NULL, *const_color = NULL;
3462         int r;
3463
3464         if ((r = parse_env_file("/etc/os-release", NEWLINE,
3465                                 "PRETTY_NAME", &pretty_name,
3466                                 "ANSI_COLOR", &ansi_color,
3467                                 NULL)) < 0) {
3468
3469                 if (r != -ENOENT)
3470                         log_warning("Failed to read /etc/os-release: %s", strerror(-r));
3471         }
3472
3473 #if defined(TARGET_FEDORA)
3474         if (!pretty_name) {
3475                 if ((r = read_one_line_file("/etc/system-release", &pretty_name)) < 0) {
3476
3477                         if (r != -ENOENT)
3478                                 log_warning("Failed to read /etc/system-release: %s", strerror(-r));
3479                 }
3480         }
3481
3482         if (!ansi_color && pretty_name) {
3483
3484                 /* This tries to mimic the color magic the old Red Hat sysinit
3485                  * script did. */
3486
3487                 if (startswith(pretty_name, "Red Hat"))
3488                         const_color = "0;31"; /* Red for RHEL */
3489                 else if (startswith(pretty_name, "Fedora"))
3490                         const_color = "0;34"; /* Blue for Fedora */
3491         }
3492
3493 #elif defined(TARGET_SUSE)
3494
3495         if (!pretty_name) {
3496                 if ((r = read_one_line_file("/etc/SuSE-release", &pretty_name)) < 0) {
3497
3498                         if (r != -ENOENT)
3499                                 log_warning("Failed to read /etc/SuSE-release: %s", strerror(-r));
3500                 }
3501         }
3502
3503         if (!ansi_color)
3504                 const_color = "0;32"; /* Green for openSUSE */
3505
3506 #elif defined(TARGET_GENTOO)
3507
3508         if (!pretty_name) {
3509                 if ((r = read_one_line_file("/etc/gentoo-release", &pretty_name)) < 0) {
3510
3511                         if (r != -ENOENT)
3512                                 log_warning("Failed to read /etc/gentoo-release: %s", strerror(-r));
3513                 }
3514         }
3515
3516         if (!ansi_color)
3517                 const_color = "1;34"; /* Light Blue for Gentoo */
3518
3519 #elif defined(TARGET_ALTLINUX)
3520
3521         if (!pretty_name) {
3522                 if ((r = read_one_line_file("/etc/altlinux-release", &pretty_name)) < 0) {
3523
3524                         if (r != -ENOENT)
3525                                 log_warning("Failed to read /etc/altlinux-release: %s", strerror(-r));
3526                 }
3527         }
3528
3529         if (!ansi_color)
3530                 const_color = "0;36"; /* Cyan for ALTLinux */
3531
3532
3533 #elif defined(TARGET_DEBIAN)
3534
3535         if (!pretty_name) {
3536                 char *version;
3537
3538                 if ((r = read_one_line_file("/etc/debian_version", &version)) < 0) {
3539
3540                         if (r != -ENOENT)
3541                                 log_warning("Failed to read /etc/debian_version: %s", strerror(-r));
3542                 } else {
3543                         pretty_name = strappend("Debian ", version);
3544                         free(version);
3545
3546                         if (!pretty_name)
3547                                 log_warning("Failed to allocate Debian version string.");
3548                 }
3549         }
3550
3551         if (!ansi_color)
3552                 const_color = "1;31"; /* Light Red for Debian */
3553
3554 #elif defined(TARGET_UBUNTU)
3555
3556         if ((r = parse_env_file("/etc/lsb-release", NEWLINE,
3557                                 "DISTRIB_DESCRIPTION", &pretty_name,
3558                                 NULL)) < 0) {
3559
3560                 if (r != -ENOENT)
3561                         log_warning("Failed to read /etc/lsb-release: %s", strerror(-r));
3562         }
3563
3564         if (!ansi_color)
3565                 const_color = "0;33"; /* Orange/Brown for Ubuntu */
3566
3567 #elif defined(TARGET_MANDRIVA)
3568
3569         if (!pretty_name) {
3570                 char *s, *p;
3571
3572                 if ((r = read_one_line_file("/etc/mandriva-release", &s) < 0)) {
3573                         if (r != -ENOENT)
3574                                 log_warning("Failed to read /etc/mandriva-release: %s", strerror(-r));
3575                 } else {
3576                         p = strstr(s, " release ");
3577                         if (p) {
3578                                 *p = '\0';
3579                                 p += 9;
3580                                 p[strcspn(p, " ")] = '\0';
3581
3582                                 /* This corresponds to standard rc.sysinit */
3583                                 if (asprintf(&pretty_name, "%s\x1B[0;39m %s", s, p) > 0)
3584                                         const_color = "1;36";
3585                                 else
3586                                         log_warning("Failed to allocate Mandriva version string.");
3587                         } else
3588                                 log_warning("Failed to parse /etc/mandriva-release");
3589                         free(s);
3590                 }
3591         }
3592 #elif defined(TARGET_MEEGO)
3593
3594         if (!pretty_name) {
3595                 if ((r = read_one_line_file("/etc/meego-release", &pretty_name)) < 0) {
3596
3597                         if (r != -ENOENT)
3598                                 log_warning("Failed to read /etc/meego-release: %s", strerror(-r));
3599                 }
3600         }
3601
3602        if (!ansi_color)
3603                const_color = "1;35"; /* Bright Magenta for MeeGo */
3604 #endif
3605
3606         if (!pretty_name && !const_pretty)
3607                 const_pretty = "Linux";
3608
3609         if (!ansi_color && !const_color)
3610                 const_color = "1";
3611
3612         status_printf("\nWelcome to \x1B[%sm%s\x1B[0m!\n\n",
3613                       const_color ? const_color : ansi_color,
3614                       const_pretty ? const_pretty : pretty_name);
3615
3616         free(ansi_color);
3617         free(pretty_name);
3618 }
3619
3620 char *replace_env(const char *format, char **env) {
3621         enum {
3622                 WORD,
3623                 CURLY,
3624                 VARIABLE
3625         } state = WORD;
3626
3627         const char *e, *word = format;
3628         char *r = NULL, *k;
3629
3630         assert(format);
3631
3632         for (e = format; *e; e ++) {
3633
3634                 switch (state) {
3635
3636                 case WORD:
3637                         if (*e == '$')
3638                                 state = CURLY;
3639                         break;
3640
3641                 case CURLY:
3642                         if (*e == '{') {
3643                                 if (!(k = strnappend(r, word, e-word-1)))
3644                                         goto fail;
3645
3646                                 free(r);
3647                                 r = k;
3648
3649                                 word = e-1;
3650                                 state = VARIABLE;
3651
3652                         } else if (*e == '$') {
3653                                 if (!(k = strnappend(r, word, e-word)))
3654                                         goto fail;
3655
3656                                 free(r);
3657                                 r = k;
3658
3659                                 word = e+1;
3660                                 state = WORD;
3661                         } else
3662                                 state = WORD;
3663                         break;
3664
3665                 case VARIABLE:
3666                         if (*e == '}') {
3667                                 const char *t;
3668
3669                                 if (!(t = strv_env_get_with_length(env, word+2, e-word-2)))
3670                                         t = "";
3671
3672                                 if (!(k = strappend(r, t)))
3673                                         goto fail;
3674
3675                                 free(r);
3676                                 r = k;
3677
3678                                 word = e+1;
3679                                 state = WORD;
3680                         }
3681                         break;
3682                 }
3683         }
3684
3685         if (!(k = strnappend(r, word, e-word)))
3686                 goto fail;
3687
3688         free(r);
3689         return k;
3690
3691 fail:
3692         free(r);
3693         return NULL;
3694 }
3695
3696 char **replace_env_argv(char **argv, char **env) {
3697         char **r, **i;
3698         unsigned k = 0, l = 0;
3699
3700         l = strv_length(argv);
3701
3702         if (!(r = new(char*, l+1)))
3703                 return NULL;
3704
3705         STRV_FOREACH(i, argv) {
3706
3707                 /* If $FOO appears as single word, replace it by the split up variable */
3708                 if ((*i)[0] == '$' && (*i)[1] != '{') {
3709                         char *e;
3710                         char **w, **m;
3711                         unsigned q;
3712
3713                         if ((e = strv_env_get(env, *i+1))) {
3714
3715                                 if (!(m = strv_split_quoted(e))) {
3716                                         r[k] = NULL;
3717                                         strv_free(r);
3718                                         return NULL;
3719                                 }
3720                         } else
3721                                 m = NULL;
3722
3723                         q = strv_length(m);
3724                         l = l + q - 1;
3725
3726                         if (!(w = realloc(r, sizeof(char*) * (l+1)))) {
3727                                 r[k] = NULL;
3728                                 strv_free(r);
3729                                 strv_free(m);
3730                                 return NULL;
3731                         }
3732
3733                         r = w;
3734                         if (m) {
3735                                 memcpy(r + k, m, q * sizeof(char*));
3736                                 free(m);
3737                         }
3738
3739                         k += q;
3740                         continue;
3741                 }
3742
3743                 /* If ${FOO} appears as part of a word, replace it by the variable as-is */
3744                 if (!(r[k++] = replace_env(*i, env))) {
3745                         strv_free(r);
3746                         return NULL;
3747                 }
3748         }
3749
3750         r[k] = NULL;
3751         return r;
3752 }
3753
3754 int columns(void) {
3755         static __thread int parsed_columns = 0;
3756         const char *e;
3757
3758         if (parsed_columns > 0)
3759                 return parsed_columns;
3760
3761         if ((e = getenv("COLUMNS")))
3762                 parsed_columns = atoi(e);
3763
3764         if (parsed_columns <= 0) {
3765                 struct winsize ws;
3766                 zero(ws);
3767
3768                 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
3769                         parsed_columns = ws.ws_col;
3770         }
3771
3772         if (parsed_columns <= 0)
3773                 parsed_columns = 80;
3774
3775         return parsed_columns;
3776 }
3777
3778 int running_in_chroot(void) {
3779         struct stat a, b;
3780
3781         zero(a);
3782         zero(b);
3783
3784         /* Only works as root */
3785
3786         if (stat("/proc/1/root", &a) < 0)
3787                 return -errno;
3788
3789         if (stat("/", &b) < 0)
3790                 return -errno;
3791
3792         return
3793                 a.st_dev != b.st_dev ||
3794                 a.st_ino != b.st_ino;
3795 }
3796
3797 char *ellipsize(const char *s, unsigned length, unsigned percent) {
3798         size_t l, x;
3799         char *r;
3800
3801         assert(s);
3802         assert(percent <= 100);
3803         assert(length >= 3);
3804
3805         l = strlen(s);
3806
3807         if (l <= 3 || l <= length)
3808                 return strdup(s);
3809
3810         if (!(r = new0(char, length+1)))
3811                 return r;
3812
3813         x = (length * percent) / 100;
3814
3815         if (x > length - 3)
3816                 x = length - 3;
3817
3818         memcpy(r, s, x);
3819         r[x] = '.';
3820         r[x+1] = '.';
3821         r[x+2] = '.';
3822         memcpy(r + x + 3,
3823                s + l - (length - x - 3),
3824                length - x - 3);
3825
3826         return r;
3827 }
3828
3829 int touch(const char *path) {
3830         int fd;
3831
3832         assert(path);
3833
3834         if ((fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644)) < 0)
3835                 return -errno;
3836
3837         close_nointr_nofail(fd);
3838         return 0;
3839 }
3840
3841 char *unquote(const char *s, const char* quotes) {
3842         size_t l;
3843         assert(s);
3844
3845         if ((l = strlen(s)) < 2)
3846                 return strdup(s);
3847
3848         if (strchr(quotes, s[0]) && s[l-1] == s[0])
3849                 return strndup(s+1, l-2);
3850
3851         return strdup(s);
3852 }
3853
3854 char *normalize_env_assignment(const char *s) {
3855         char *name, *value, *p, *r;
3856
3857         p = strchr(s, '=');
3858
3859         if (!p) {
3860                 if (!(r = strdup(s)))
3861                         return NULL;
3862
3863                 return strstrip(r);
3864         }
3865
3866         if (!(name = strndup(s, p - s)))
3867                 return NULL;
3868
3869         if (!(p = strdup(p+1))) {
3870                 free(name);
3871                 return NULL;
3872         }
3873
3874         value = unquote(strstrip(p), QUOTES);
3875         free(p);
3876
3877         if (!value) {
3878                 free(name);
3879                 return NULL;
3880         }
3881
3882         if (asprintf(&r, "%s=%s", name, value) < 0)
3883                 r = NULL;
3884
3885         free(value);
3886         free(name);
3887
3888         return r;
3889 }
3890
3891 int wait_for_terminate(pid_t pid, siginfo_t *status) {
3892         siginfo_t dummy;
3893
3894         assert(pid >= 1);
3895
3896         if (!status)
3897                 status = &dummy;
3898
3899         for (;;) {
3900                 zero(*status);
3901
3902                 if (waitid(P_PID, pid, status, WEXITED) < 0) {
3903
3904                         if (errno == EINTR)
3905                                 continue;
3906
3907                         return -errno;
3908                 }
3909
3910                 return 0;
3911         }
3912 }
3913
3914 int wait_for_terminate_and_warn(const char *name, pid_t pid) {
3915         int r;
3916         siginfo_t status;
3917
3918         assert(name);
3919         assert(pid > 1);
3920
3921         if ((r = wait_for_terminate(pid, &status)) < 0) {
3922                 log_warning("Failed to wait for %s: %s", name, strerror(-r));
3923                 return r;
3924         }
3925
3926         if (status.si_code == CLD_EXITED) {
3927                 if (status.si_status != 0) {
3928                         log_warning("%s failed with error code %i.", name, status.si_status);
3929                         return status.si_status;
3930                 }
3931
3932                 log_debug("%s succeeded.", name);
3933                 return 0;
3934
3935         } else if (status.si_code == CLD_KILLED ||
3936                    status.si_code == CLD_DUMPED) {
3937
3938                 log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
3939                 return -EPROTO;
3940         }
3941
3942         log_warning("%s failed due to unknown reason.", name);
3943         return -EPROTO;
3944
3945 }
3946
3947 void freeze(void) {
3948
3949         /* Make sure nobody waits for us on a socket anymore */
3950         close_all_fds(NULL, 0);
3951
3952         sync();
3953
3954         for (;;)
3955                 pause();
3956 }
3957
3958 bool null_or_empty(struct stat *st) {
3959         assert(st);
3960
3961         if (S_ISREG(st->st_mode) && st->st_size <= 0)
3962                 return true;
3963
3964         if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
3965                 return true;
3966
3967         return false;
3968 }
3969
3970 int null_or_empty_path(const char *fn) {
3971         struct stat st;
3972
3973         assert(fn);
3974
3975         if (stat(fn, &st) < 0)
3976                 return -errno;
3977
3978         return null_or_empty(&st);
3979 }
3980
3981 DIR *xopendirat(int fd, const char *name, int flags) {
3982         int nfd;
3983         DIR *d;
3984
3985         if ((nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags)) < 0)
3986                 return NULL;
3987
3988         if (!(d = fdopendir(nfd))) {
3989                 close_nointr_nofail(nfd);
3990                 return NULL;
3991         }
3992
3993         return d;
3994 }
3995
3996 int signal_from_string_try_harder(const char *s) {
3997         int signo;
3998         assert(s);
3999
4000         if ((signo = signal_from_string(s)) <= 0)
4001                 if (startswith(s, "SIG"))
4002                         return signal_from_string(s+3);
4003
4004         return signo;
4005 }
4006
4007 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
4008
4009         assert(f);
4010         assert(name);
4011         assert(t);
4012
4013         if (!dual_timestamp_is_set(t))
4014                 return;
4015
4016         fprintf(f, "%s=%llu %llu\n",
4017                 name,
4018                 (unsigned long long) t->realtime,
4019                 (unsigned long long) t->monotonic);
4020 }
4021
4022 void dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
4023         unsigned long long a, b;
4024
4025         assert(value);
4026         assert(t);
4027
4028         if (sscanf(value, "%lli %llu", &a, &b) != 2)
4029                 log_debug("Failed to parse finish timestamp value %s", value);
4030         else {
4031                 t->realtime = a;
4032                 t->monotonic = b;
4033         }
4034 }
4035
4036 char *fstab_node_to_udev_node(const char *p) {
4037         char *dn, *t, *u;
4038         int r;
4039
4040         /* FIXME: to follow udev's logic 100% we need to leave valid
4041          * UTF8 chars unescaped */
4042
4043         if (startswith(p, "LABEL=")) {
4044
4045                 if (!(u = unquote(p+6, "\"\'")))
4046                         return NULL;
4047
4048                 t = xescape(u, "/ ");
4049                 free(u);
4050
4051                 if (!t)
4052                         return NULL;
4053
4054                 r = asprintf(&dn, "/dev/disk/by-label/%s", t);
4055                 free(t);
4056
4057                 if (r < 0)
4058                         return NULL;
4059
4060                 return dn;
4061         }
4062
4063         if (startswith(p, "UUID=")) {
4064
4065                 if (!(u = unquote(p+5, "\"\'")))
4066                         return NULL;
4067
4068                 t = xescape(u, "/ ");
4069                 free(u);
4070
4071                 if (!t)
4072                         return NULL;
4073
4074                 r = asprintf(&dn, "/dev/disk/by-uuid/%s", t);
4075                 free(t);
4076
4077                 if (r < 0)
4078                         return NULL;
4079
4080                 return dn;
4081         }
4082
4083         return strdup(p);
4084 }
4085
4086 void filter_environ(const char *prefix) {
4087         int i, j;
4088         assert(prefix);
4089
4090         if (!environ)
4091                 return;
4092
4093         for (i = 0, j = 0; environ[i]; i++) {
4094
4095                 if (startswith(environ[i], prefix))
4096                         continue;
4097
4098                 environ[j++] = environ[i];
4099         }
4100
4101         environ[j] = NULL;
4102 }
4103
4104 bool tty_is_vc(const char *tty) {
4105         assert(tty);
4106
4107         if (startswith(tty, "/dev/"))
4108                 tty += 5;
4109
4110         return vtnr_from_tty(tty) >= 0;
4111 }
4112
4113 int vtnr_from_tty(const char *tty) {
4114         int i, r;
4115
4116         assert(tty);
4117
4118         if (startswith(tty, "/dev/"))
4119                 tty += 5;
4120
4121         if (!startswith(tty, "tty") )
4122                 return -EINVAL;
4123
4124         if (tty[3] < '0' || tty[3] > '9')
4125                 return -EINVAL;
4126
4127         r = safe_atoi(tty+3, &i);
4128         if (r < 0)
4129                 return r;
4130
4131         if (i < 0 || i > 63)
4132                 return -EINVAL;
4133
4134         return i;
4135 }
4136
4137 const char *default_term_for_tty(const char *tty) {
4138         char *active = NULL;
4139         const char *term;
4140
4141         assert(tty);
4142
4143         if (startswith(tty, "/dev/"))
4144                 tty += 5;
4145
4146         /* Resolve where /dev/console is pointing when determining
4147          * TERM */
4148         if (streq(tty, "console"))
4149                 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
4150                         /* If multiple log outputs are configured the
4151                          * last one is what /dev/console points to */
4152                         if ((tty = strrchr(active, ' ')))
4153                                 tty++;
4154                         else
4155                                 tty = active;
4156                 }
4157
4158         term = tty_is_vc(tty) ? "TERM=linux" : "TERM=vt100";
4159         free(active);
4160
4161         return term;
4162 }
4163
4164 /* Returns a short identifier for the various VM implementations */
4165 int detect_vm(const char **id) {
4166
4167 #if defined(__i386__) || defined(__x86_64__)
4168
4169         /* Both CPUID and DMI are x86 specific interfaces... */
4170
4171         static const char *const dmi_vendors[] = {
4172                 "/sys/class/dmi/id/sys_vendor",
4173                 "/sys/class/dmi/id/board_vendor",
4174                 "/sys/class/dmi/id/bios_vendor"
4175         };
4176
4177         static const char dmi_vendor_table[] =
4178                 "QEMU\0"                  "qemu\0"
4179                 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
4180                 "VMware\0"                "vmware\0"
4181                 "VMW\0"                   "vmware\0"
4182                 "Microsoft Corporation\0" "microsoft\0"
4183                 "innotek GmbH\0"          "oracle\0"
4184                 "Xen\0"                   "xen\0"
4185                 "Bochs\0"                 "bochs\0";
4186
4187         static const char cpuid_vendor_table[] =
4188                 "XenVMMXenVMM\0"          "xen\0"
4189                 "KVMKVMKVM\0"             "kvm\0"
4190                 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
4191                 "VMwareVMware\0"          "vmware\0"
4192                 /* http://msdn.microsoft.com/en-us/library/ff542428.aspx */
4193                 "Microsoft Hv\0"          "microsoft\0";
4194
4195         uint32_t eax, ecx;
4196         union {
4197                 uint32_t sig32[3];
4198                 char text[13];
4199         } sig;
4200         unsigned i;
4201         const char *j, *k;
4202         bool hypervisor;
4203
4204         /* http://lwn.net/Articles/301888/ */
4205         zero(sig);
4206
4207 #if defined (__i386__)
4208 #define REG_a "eax"
4209 #define REG_b "ebx"
4210 #elif defined (__amd64__)
4211 #define REG_a "rax"
4212 #define REG_b "rbx"
4213 #endif
4214
4215         /* First detect whether there is a hypervisor */
4216         eax = 1;
4217         __asm__ __volatile__ (
4218                 /* ebx/rbx is being used for PIC! */
4219                 "  push %%"REG_b"         \n\t"
4220                 "  cpuid                  \n\t"
4221                 "  pop %%"REG_b"          \n\t"
4222
4223                 : "=a" (eax), "=c" (ecx)
4224                 : "0" (eax)
4225         );
4226
4227         hypervisor = !!(ecx & 0x80000000U);
4228
4229         if (hypervisor) {
4230
4231                 /* There is a hypervisor, see what it is */
4232                 eax = 0x40000000U;
4233                 __asm__ __volatile__ (
4234                         /* ebx/rbx is being used for PIC! */
4235                         "  push %%"REG_b"         \n\t"
4236                         "  cpuid                  \n\t"
4237                         "  mov %%ebx, %1          \n\t"
4238                         "  pop %%"REG_b"          \n\t"
4239
4240                         : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2])
4241                         : "0" (eax)
4242                 );
4243
4244                 NULSTR_FOREACH_PAIR(j, k, cpuid_vendor_table)
4245                         if (streq(sig.text, j)) {
4246
4247                                 if (id)
4248                                         *id = k;
4249
4250                                 return 1;
4251                         }
4252         }
4253
4254         for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
4255                 char *s;
4256                 int r;
4257                 const char *found = NULL;
4258
4259                 if ((r = read_one_line_file(dmi_vendors[i], &s)) < 0) {
4260                         if (r != -ENOENT)
4261                                 return r;
4262
4263                         continue;
4264                 }
4265
4266                 NULSTR_FOREACH_PAIR(j, k, dmi_vendor_table)
4267                         if (startswith(s, j))
4268                                 found = k;
4269                 free(s);
4270
4271                 if (found) {
4272                         if (id)
4273                                 *id = found;
4274
4275                         return 1;
4276                 }
4277         }
4278
4279         if (hypervisor) {
4280                 if (id)
4281                         *id = "other";
4282
4283                 return 1;
4284         }
4285
4286 #endif
4287         return 0;
4288 }
4289
4290 int detect_container(const char **id) {
4291         FILE *f;
4292
4293         /* Unfortunately many of these operations require root access
4294          * in one way or another */
4295
4296         if (geteuid() != 0)
4297                 return -EPERM;
4298
4299         if (running_in_chroot() > 0) {
4300
4301                 if (id)
4302                         *id = "chroot";
4303
4304                 return 1;
4305         }
4306
4307         /* /proc/vz exists in container and outside of the container,
4308          * /proc/bc only outside of the container. */
4309         if (access("/proc/vz", F_OK) >= 0 &&
4310             access("/proc/bc", F_OK) < 0) {
4311
4312                 if (id)
4313                         *id = "openvz";
4314
4315                 return 1;
4316         }
4317
4318         if ((f = fopen("/proc/self/cgroup", "re"))) {
4319
4320                 for (;;) {
4321                         char line[LINE_MAX], *p;
4322
4323                         if (!fgets(line, sizeof(line), f))
4324                                 break;
4325
4326                         if (!(p = strchr(strstrip(line), ':')))
4327                                 continue;
4328
4329                         if (strncmp(p, ":ns:", 4))
4330                                 continue;
4331
4332                         if (!streq(p, ":ns:/")) {
4333                                 fclose(f);
4334
4335                                 if (id)
4336                                         *id = "pidns";
4337
4338                                 return 1;
4339                         }
4340                 }
4341
4342                 fclose(f);
4343         }
4344
4345         return 0;
4346 }
4347
4348 /* Returns a short identifier for the various VM/container implementations */
4349 int detect_virtualization(const char **id) {
4350         static __thread const char *cached_id = NULL;
4351         const char *_id;
4352         int r;
4353
4354         if (cached_id) {
4355
4356                 if (cached_id == (const char*) -1)
4357                         return 0;
4358
4359                 if (id)
4360                         *id = cached_id;
4361
4362                 return 1;
4363         }
4364
4365         if ((r = detect_container(&_id)) != 0)
4366                 goto finish;
4367
4368         r = detect_vm(&_id);
4369
4370 finish:
4371         if (r > 0) {
4372                 cached_id = _id;
4373
4374                 if (id)
4375                         *id = _id;
4376         } else if (r == 0)
4377                 cached_id = (const char*) -1;
4378
4379         return r;
4380 }
4381
4382 bool dirent_is_file(struct dirent *de) {
4383         assert(de);
4384
4385         if (ignore_file(de->d_name))
4386                 return false;
4387
4388         if (de->d_type != DT_REG &&
4389             de->d_type != DT_LNK &&
4390             de->d_type != DT_UNKNOWN)
4391                 return false;
4392
4393         return true;
4394 }
4395
4396 void execute_directory(const char *directory, DIR *d, char *argv[]) {
4397         DIR *_d = NULL;
4398         struct dirent *de;
4399         Hashmap *pids = NULL;
4400
4401         assert(directory);
4402
4403         /* Executes all binaries in a directory in parallel and waits
4404          * until all they all finished. */
4405
4406         if (!d) {
4407                 if (!(_d = opendir(directory))) {
4408
4409                         if (errno == ENOENT)
4410                                 return;
4411
4412                         log_error("Failed to enumerate directory %s: %m", directory);
4413                         return;
4414                 }
4415
4416                 d = _d;
4417         }
4418
4419         if (!(pids = hashmap_new(trivial_hash_func, trivial_compare_func))) {
4420                 log_error("Failed to allocate set.");
4421                 goto finish;
4422         }
4423
4424         while ((de = readdir(d))) {
4425                 char *path;
4426                 pid_t pid;
4427                 int k;
4428
4429                 if (!dirent_is_file(de))
4430                         continue;
4431
4432                 if (asprintf(&path, "%s/%s", directory, de->d_name) < 0) {
4433                         log_error("Out of memory");
4434                         continue;
4435                 }
4436
4437                 if ((pid = fork()) < 0) {
4438                         log_error("Failed to fork: %m");
4439                         free(path);
4440                         continue;
4441                 }
4442
4443                 if (pid == 0) {
4444                         char *_argv[2];
4445                         /* Child */
4446
4447                         if (!argv) {
4448                                 _argv[0] = path;
4449                                 _argv[1] = NULL;
4450                                 argv = _argv;
4451                         } else
4452                                 if (!argv[0])
4453                                         argv[0] = path;
4454
4455                         execv(path, argv);
4456
4457                         log_error("Failed to execute %s: %m", path);
4458                         _exit(EXIT_FAILURE);
4459                 }
4460
4461                 log_debug("Spawned %s as %lu", path, (unsigned long) pid);
4462
4463                 if ((k = hashmap_put(pids, UINT_TO_PTR(pid), path)) < 0) {
4464                         log_error("Failed to add PID to set: %s", strerror(-k));
4465                         free(path);
4466                 }
4467         }
4468
4469         while (!hashmap_isempty(pids)) {
4470                 siginfo_t si;
4471                 char *path;
4472
4473                 zero(si);
4474                 if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
4475
4476                         if (errno == EINTR)
4477                                 continue;
4478
4479                         log_error("waitid() failed: %m");
4480                         goto finish;
4481                 }
4482
4483                 if ((path = hashmap_remove(pids, UINT_TO_PTR(si.si_pid)))) {
4484                         if (!is_clean_exit(si.si_code, si.si_status)) {
4485                                 if (si.si_code == CLD_EXITED)
4486                                         log_error("%s exited with exit status %i.", path, si.si_status);
4487                                 else
4488                                         log_error("%s terminated by signal %s.", path, signal_to_string(si.si_status));
4489                         } else
4490                                 log_debug("%s exited successfully.", path);
4491
4492                         free(path);
4493                 }
4494         }
4495
4496 finish:
4497         if (_d)
4498                 closedir(_d);
4499
4500         if (pids)
4501                 hashmap_free_free(pids);
4502 }
4503
4504 int kill_and_sigcont(pid_t pid, int sig) {
4505         int r;
4506
4507         r = kill(pid, sig) < 0 ? -errno : 0;
4508
4509         if (r >= 0)
4510                 kill(pid, SIGCONT);
4511
4512         return r;
4513 }
4514
4515 bool nulstr_contains(const char*nulstr, const char *needle) {
4516         const char *i;
4517
4518         if (!nulstr)
4519                 return false;
4520
4521         NULSTR_FOREACH(i, nulstr)
4522                 if (streq(i, needle))
4523                         return true;
4524
4525         return false;
4526 }
4527
4528 bool plymouth_running(void) {
4529         return access("/run/plymouth/pid", F_OK) >= 0;
4530 }
4531
4532 void parse_syslog_priority(char **p, int *priority) {
4533         int a = 0, b = 0, c = 0;
4534         int k;
4535
4536         assert(p);
4537         assert(*p);
4538         assert(priority);
4539
4540         if ((*p)[0] != '<')
4541                 return;
4542
4543         if (!strchr(*p, '>'))
4544                 return;
4545
4546         if ((*p)[2] == '>') {
4547                 c = undecchar((*p)[1]);
4548                 k = 3;
4549         } else if ((*p)[3] == '>') {
4550                 b = undecchar((*p)[1]);
4551                 c = undecchar((*p)[2]);
4552                 k = 4;
4553         } else if ((*p)[4] == '>') {
4554                 a = undecchar((*p)[1]);
4555                 b = undecchar((*p)[2]);
4556                 c = undecchar((*p)[3]);
4557                 k = 5;
4558         } else
4559                 return;
4560
4561         if (a < 0 || b < 0 || c < 0)
4562                 return;
4563
4564         *priority = a*100+b*10+c;
4565         *p += k;
4566 }
4567
4568 int have_effective_cap(int value) {
4569         cap_t cap;
4570         cap_flag_value_t fv;
4571         int r;
4572
4573         if (!(cap = cap_get_proc()))
4574                 return -errno;
4575
4576         if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0)
4577                 r = -errno;
4578         else
4579                 r = fv == CAP_SET;
4580
4581         cap_free(cap);
4582         return r;
4583 }
4584
4585 char* strshorten(char *s, size_t l) {
4586         assert(s);
4587
4588         if (l < strlen(s))
4589                 s[l] = 0;
4590
4591         return s;
4592 }
4593
4594 static bool hostname_valid_char(char c) {
4595         return
4596                 (c >= 'a' && c <= 'z') ||
4597                 (c >= 'A' && c <= 'Z') ||
4598                 (c >= '0' && c <= '9') ||
4599                 c == '-' ||
4600                 c == '_' ||
4601                 c == '.';
4602 }
4603
4604 bool hostname_is_valid(const char *s) {
4605         const char *p;
4606
4607         if (isempty(s))
4608                 return false;
4609
4610         for (p = s; *p; p++)
4611                 if (!hostname_valid_char(*p))
4612                         return false;
4613
4614         if (p-s > HOST_NAME_MAX)
4615                 return false;
4616
4617         return true;
4618 }
4619
4620 char* hostname_cleanup(char *s) {
4621         char *p, *d;
4622
4623         for (p = s, d = s; *p; p++)
4624                 if ((*p >= 'a' && *p <= 'z') ||
4625                     (*p >= 'A' && *p <= 'Z') ||
4626                     (*p >= '0' && *p <= '9') ||
4627                     *p == '-' ||
4628                     *p == '_' ||
4629                     *p == '.')
4630                         *(d++) = *p;
4631
4632         *d = 0;
4633
4634         strshorten(s, HOST_NAME_MAX);
4635         return s;
4636 }
4637
4638 int pipe_eof(int fd) {
4639         struct pollfd pollfd;
4640         int r;
4641
4642         zero(pollfd);
4643         pollfd.fd = fd;
4644         pollfd.events = POLLIN|POLLHUP;
4645
4646         r = poll(&pollfd, 1, 0);
4647         if (r < 0)
4648                 return -errno;
4649
4650         if (r == 0)
4651                 return 0;
4652
4653         return pollfd.revents & POLLHUP;
4654 }
4655
4656 int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
4657         FILE *f;
4658         char *t;
4659         const char *fn;
4660         size_t k;
4661         int fd;
4662
4663         assert(path);
4664         assert(_f);
4665         assert(_temp_path);
4666
4667         t = new(char, strlen(path) + 1 + 6 + 1);
4668         if (!t)
4669                 return -ENOMEM;
4670
4671         fn = file_name_from_path(path);
4672         k = fn-path;
4673         memcpy(t, path, k);
4674         t[k] = '.';
4675         stpcpy(stpcpy(t+k+1, fn), "XXXXXX");
4676
4677         fd = mkostemp(t, O_WRONLY|O_CLOEXEC);
4678         if (fd < 0) {
4679                 free(t);
4680                 return -errno;
4681         }
4682
4683         f = fdopen(fd, "we");
4684         if (!f) {
4685                 unlink(t);
4686                 free(t);
4687                 return -errno;
4688         }
4689
4690         *_f = f;
4691         *_temp_path = t;
4692
4693         return 0;
4694 }
4695
4696 int terminal_vhangup_fd(int fd) {
4697         assert(fd >= 0);
4698
4699         if (ioctl(fd, TIOCVHANGUP) < 0)
4700                 return -errno;
4701
4702         return 0;
4703 }
4704
4705 int terminal_vhangup(const char *name) {
4706         int fd, r;
4707
4708         fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
4709         if (fd < 0)
4710                 return fd;
4711
4712         r = terminal_vhangup_fd(fd);
4713         close_nointr_nofail(fd);
4714
4715         return r;
4716 }
4717
4718 int vt_disallocate(const char *name) {
4719         int fd, r;
4720         unsigned u;
4721
4722         /* Deallocate the VT if possible. If not possible
4723          * (i.e. because it is the active one), at least clear it
4724          * entirely (including the scrollback buffer) */
4725
4726         if (!startswith(name, "/dev/"))
4727                 return -EINVAL;
4728
4729         if (!tty_is_vc(name)) {
4730                 /* So this is not a VT. I guess we cannot deallocate
4731                  * it then. But let's at least clear the screen */
4732
4733                 fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
4734                 if (fd < 0)
4735                         return fd;
4736
4737                 loop_write(fd,
4738                            "\033[r"    /* clear scrolling region */
4739                            "\033[H"    /* move home */
4740                            "\033[2J",  /* clear screen */
4741                            10, false);
4742                 close_nointr_nofail(fd);
4743
4744                 return 0;
4745         }
4746
4747         if (!startswith(name, "/dev/tty"))
4748                 return -EINVAL;
4749
4750         r = safe_atou(name+8, &u);
4751         if (r < 0)
4752                 return r;
4753
4754         if (u <= 0)
4755                 return -EINVAL;
4756
4757         /* Try to deallocate */
4758         fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
4759         if (fd < 0)
4760                 return fd;
4761
4762         r = ioctl(fd, VT_DISALLOCATE, u);
4763         close_nointr_nofail(fd);
4764
4765         if (r >= 0)
4766                 return 0;
4767
4768         if (errno != EBUSY)
4769                 return -errno;
4770
4771         /* Couldn't deallocate, so let's clear it fully with
4772          * scrollback */
4773         fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
4774         if (fd < 0)
4775                 return fd;
4776
4777         loop_write(fd,
4778                    "\033[r"   /* clear scrolling region */
4779                    "\033[H"   /* move home */
4780                    "\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */
4781                    10, false);
4782         close_nointr_nofail(fd);
4783
4784         return 0;
4785 }
4786
4787
4788 static int file_is_conf(const struct dirent *d, const char *suffix) {
4789         assert(d);
4790
4791         if (ignore_file(d->d_name))
4792                 return 0;
4793
4794         if (d->d_type != DT_REG &&
4795             d->d_type != DT_LNK &&
4796             d->d_type != DT_UNKNOWN)
4797                 return 0;
4798
4799         return endswith(d->d_name, suffix);
4800 }
4801
4802 static int files_add(Hashmap *h, const char *path, const char *suffix) {
4803         DIR *dir;
4804         struct dirent *de;
4805         int r = 0;
4806
4807         dir = opendir(path);
4808         if (!dir) {
4809                 if (errno == ENOENT)
4810                         return 0;
4811                 return -errno;
4812         }
4813
4814         for (de = readdir(dir); de; de = readdir(dir)) {
4815                 char *p, *f;
4816                 const char *base;
4817
4818                 if (!file_is_conf(de, suffix))
4819                         continue;
4820
4821                 if (asprintf(&p, "%s/%s", path, de->d_name) < 0) {
4822                         r = -ENOMEM;
4823                         goto finish;
4824                 }
4825
4826                 f = canonicalize_file_name(p);
4827                 if (!f) {
4828                         log_error("Failed to canonicalize file name '%s': %m", p);
4829                         free(p);
4830                         continue;
4831                 }
4832                 free(p);
4833
4834                 log_debug("found: %s\n", f);
4835                 base = f + strlen(path) + 1;
4836                 if (hashmap_put(h, base, f) <= 0)
4837                         free(f);
4838         }
4839
4840 finish:
4841         closedir(dir);
4842         return r;
4843 }
4844
4845 static int base_cmp(const void *a, const void *b) {
4846         const char *s1, *s2;
4847
4848         s1 = *(char * const *)a;
4849         s2 = *(char * const *)b;
4850         return strcmp(file_name_from_path(s1), file_name_from_path(s2));
4851 }
4852
4853 int conf_files_list(char ***strv, const char *suffix, const char *dir, ...) {
4854         Hashmap *fh = NULL;
4855         char **dirs = NULL;
4856         char **files = NULL;
4857         char **p;
4858         va_list ap;
4859         int r = 0;
4860
4861         va_start(ap, dir);
4862         dirs = strv_new_ap(dir, ap);
4863         va_end(ap);
4864         if (!dirs) {
4865                 r = -ENOMEM;
4866                 goto finish;
4867         }
4868         if (!strv_path_canonicalize(dirs)) {
4869                 r = -ENOMEM;
4870                 goto finish;
4871         }
4872         if (!strv_uniq(dirs)) {
4873                 r = -ENOMEM;
4874                 goto finish;
4875         }
4876
4877         fh = hashmap_new(string_hash_func, string_compare_func);
4878         if (!fh) {
4879                 r = -ENOMEM;
4880                 goto finish;
4881         }
4882
4883         STRV_FOREACH(p, dirs) {
4884                 if (files_add(fh, *p, suffix) < 0) {
4885                         log_error("Failed to search for files.");
4886                         r = -EINVAL;
4887                         goto finish;
4888                 }
4889         }
4890
4891         files = hashmap_get_strv(fh);
4892         if (files == NULL) {
4893                 log_error("Failed to compose list of files.");
4894                 r = -ENOMEM;
4895                 goto finish;
4896         }
4897
4898         qsort(files, hashmap_size(fh), sizeof(char *), base_cmp);
4899
4900 finish:
4901         strv_free(dirs);
4902         hashmap_free(fh);
4903         *strv = files;
4904         return r;
4905 }
4906
4907 int hwclock_is_localtime(void) {
4908         FILE *f;
4909         bool local = false;
4910
4911         /*
4912          * The third line of adjtime is "UTC" or "LOCAL" or nothing.
4913          *   # /etc/adjtime
4914          *   0.0 0 0
4915          *   0
4916          *   UTC
4917          */
4918         f = fopen("/etc/adjtime", "re");
4919         if (f) {
4920                 char line[LINE_MAX];
4921                 bool b;
4922
4923                 b = fgets(line, sizeof(line), f) &&
4924                         fgets(line, sizeof(line), f) &&
4925                         fgets(line, sizeof(line), f);
4926
4927                 fclose(f);
4928
4929                 if (!b)
4930                         return -EIO;
4931
4932
4933                 truncate_nl(line);
4934                 local = streq(line, "LOCAL");
4935
4936         } else if (errno != -ENOENT)
4937                 return -errno;
4938
4939         return local;
4940 }
4941
4942 int hwclock_apply_localtime_delta(int *min) {
4943         const struct timeval *tv_null = NULL;
4944         struct timespec ts;
4945         struct tm *tm;
4946         int minuteswest;
4947         struct timezone tz;
4948
4949         assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
4950         assert_se(tm = localtime(&ts.tv_sec));
4951         minuteswest = tm->tm_gmtoff / 60;
4952
4953         tz.tz_minuteswest = -minuteswest;
4954         tz.tz_dsttime = 0; /* DST_NONE*/
4955
4956         /*
4957          * If the hardware clock does not run in UTC, but in local time:
4958          * The very first time we set the kernel's timezone, it will warp
4959          * the clock so that it runs in UTC instead of local time.
4960          */
4961         if (settimeofday(tv_null, &tz) < 0)
4962                 return -errno;
4963         if (min)
4964                 *min = minuteswest;
4965         return 0;
4966 }
4967
4968 int hwclock_reset_localtime_delta(void) {
4969         const struct timeval *tv_null = NULL;
4970         struct timezone tz;
4971
4972         tz.tz_minuteswest = 0;
4973         tz.tz_dsttime = 0; /* DST_NONE*/
4974
4975         if (settimeofday(tv_null, &tz) < 0)
4976                 return -errno;
4977
4978         return 0;
4979 }
4980
4981 int hwclock_get_time(struct tm *tm) {
4982         int fd;
4983         int err = 0;
4984
4985         assert(tm);
4986
4987         fd = open("/dev/rtc0", O_RDONLY|O_CLOEXEC);
4988         if (fd < 0)
4989                 return -errno;
4990
4991         /* This leaves the timezone fields of struct tm
4992          * uninitialized! */
4993         if (ioctl(fd, RTC_RD_TIME, tm) < 0)
4994                 err = -errno;
4995
4996         /* We don't now daylight saving, so we reset this in order not
4997          * to confused mktime(). */
4998         tm->tm_isdst = -1;
4999
5000         close_nointr_nofail(fd);
5001
5002         return err;
5003 }
5004
5005 int hwclock_set_time(const struct tm *tm) {
5006         int fd;
5007         int err = 0;
5008
5009         assert(tm);
5010
5011         fd = open("/dev/rtc0", O_RDONLY|O_CLOEXEC);
5012         if (fd < 0)
5013                 return -errno;
5014
5015         if (ioctl(fd, RTC_SET_TIME, tm) < 0)
5016                 err = -errno;
5017
5018         close_nointr_nofail(fd);
5019
5020         return err;
5021 }
5022
5023 int copy_file(const char *from, const char *to) {
5024         int r, fdf, fdt;
5025
5026         assert(from);
5027         assert(to);
5028
5029         fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
5030         if (fdf < 0)
5031                 return -errno;
5032
5033         fdt = open(to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY, 0644);
5034         if (fdt < 0) {
5035                 close_nointr_nofail(fdf);
5036                 return -errno;
5037         }
5038
5039         for (;;) {
5040                 char buf[PIPE_BUF];
5041                 ssize_t n, k;
5042
5043                 n = read(fdf, buf, sizeof(buf));
5044                 if (n < 0) {
5045                         r = -errno;
5046
5047                         close_nointr_nofail(fdf);
5048                         close_nointr(fdt);
5049                         unlink(to);
5050
5051                         return r;
5052                 }
5053
5054                 if (n == 0)
5055                         break;
5056
5057                 errno = 0;
5058                 k = loop_write(fdt, buf, n, false);
5059                 if (n != k) {
5060                         r = k < 0 ? k : (errno ? -errno : -EIO);
5061
5062                         close_nointr_nofail(fdf);
5063                         close_nointr(fdt);
5064
5065                         unlink(to);
5066                         return r;
5067                 }
5068         }
5069
5070         close_nointr_nofail(fdf);
5071         r = close_nointr(fdt);
5072
5073         if (r < 0) {
5074                 unlink(to);
5075                 return r;
5076         }
5077
5078         return 0;
5079 }
5080
5081 int symlink_or_copy(const char *from, const char *to) {
5082         char *pf = NULL, *pt = NULL;
5083         struct stat a, b;
5084         int r;
5085
5086         assert(from);
5087         assert(to);
5088
5089         if (parent_of_path(from, &pf) < 0 ||
5090             parent_of_path(to, &pt) < 0) {
5091                 r = -ENOMEM;
5092                 goto finish;
5093         }
5094
5095         if (stat(pf, &a) < 0 ||
5096             stat(pt, &b) < 0) {
5097                 r = -errno;
5098                 goto finish;
5099         }
5100
5101         if (a.st_dev != b.st_dev) {
5102                 free(pf);
5103                 free(pt);
5104
5105                 return copy_file(from, to);
5106         }
5107
5108         if (symlink(from, to) < 0) {
5109                 r = -errno;
5110                 goto finish;
5111         }
5112
5113         r = 0;
5114
5115 finish:
5116         free(pf);
5117         free(pt);
5118
5119         return r;
5120 }
5121
5122 int symlink_or_copy_atomic(const char *from, const char *to) {
5123         char *t, *x;
5124         const char *fn;
5125         size_t k;
5126         unsigned long long ull;
5127         unsigned i;
5128         int r;
5129
5130         assert(from);
5131         assert(to);
5132
5133         t = new(char, strlen(to) + 1 + 16 + 1);
5134         if (!t)
5135                 return -ENOMEM;
5136
5137         fn = file_name_from_path(to);
5138         k = fn-to;
5139         memcpy(t, to, k);
5140         t[k] = '.';
5141         x = stpcpy(t+k+1, fn);
5142
5143         ull = random_ull();
5144         for (i = 0; i < 16; i++) {
5145                 *(x++) = hexchar(ull & 0xF);
5146                 ull >>= 4;
5147         }
5148
5149         *x = 0;
5150
5151         r = symlink_or_copy(from, t);
5152         if (r < 0) {
5153                 unlink(t);
5154                 free(t);
5155                 return r;
5156         }
5157
5158         if (rename(t, to) < 0) {
5159                 r = -errno;
5160                 unlink(t);
5161                 free(t);
5162                 return r;
5163         }
5164
5165         free(t);
5166         return r;
5167 }
5168
5169 int audit_session_from_pid(pid_t pid, uint32_t *id) {
5170         char *p, *s;
5171         uint32_t u;
5172         int r;
5173
5174         assert(pid >= 1);
5175         assert(id);
5176
5177         if (have_effective_cap(CAP_AUDIT_CONTROL) <= 0)
5178                 return -ENOENT;
5179
5180         if (asprintf(&p, "/proc/%lu/sessionid", (unsigned long) pid) < 0)
5181                 return -ENOMEM;
5182
5183         r = read_one_line_file(p, &s);
5184         free(p);
5185         if (r < 0)
5186                 return r;
5187
5188         r = safe_atou32(s, &u);
5189         free(s);
5190
5191         if (r < 0)
5192                 return r;
5193
5194         if (u == (uint32_t) -1 || u <= 0)
5195                 return -ENOENT;
5196
5197         *id = u;
5198         return 0;
5199 }
5200
5201 bool display_is_local(const char *display) {
5202         assert(display);
5203
5204         return
5205                 display[0] == ':' &&
5206                 display[1] >= '0' &&
5207                 display[1] <= '9';
5208 }
5209
5210 int socket_from_display(const char *display, char **path) {
5211         size_t k;
5212         char *f, *c;
5213
5214         assert(display);
5215         assert(path);
5216
5217         if (!display_is_local(display))
5218                 return -EINVAL;
5219
5220         k = strspn(display+1, "0123456789");
5221
5222         f = new(char, sizeof("/tmp/.X11-unix/X") + k);
5223         if (!f)
5224                 return -ENOMEM;
5225
5226         c = stpcpy(f, "/tmp/.X11-unix/X");
5227         memcpy(c, display+1, k);
5228         c[k] = 0;
5229
5230         *path = f;
5231
5232         return 0;
5233 }
5234
5235 int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) {
5236         struct passwd *p;
5237         unsigned long lu;
5238
5239         assert(username);
5240         assert(*username);
5241         assert(uid);
5242         assert(gid);
5243         assert(home);
5244
5245         /* We enforce some special rules for uid=0: in order to avoid
5246          * NSS lookups for root we hardcode its data. */
5247
5248         if (streq(*username, "root") || streq(*username, "0")) {
5249                 *username = "root";
5250                 *uid = 0;
5251                 *gid = 0;
5252                 *home = "/root";
5253                 return 0;
5254         }
5255
5256         if (safe_atolu(*username, &lu) >= 0) {
5257                 errno = 0;
5258                 p = getpwuid((uid_t) lu);
5259
5260                 /* If there are multiple users with the same id, make
5261                  * sure to leave $USER to the configured value instead
5262                  * of the first occurrence in the database. However if
5263                  * the uid was configured by a numeric uid, then let's
5264                  * pick the real username from /etc/passwd. */
5265                 if (p)
5266                         *username = p->pw_name;
5267         } else {
5268                 errno = 0;
5269                 p = getpwnam(*username);
5270         }
5271
5272         if (!p)
5273                 return errno != 0 ? -errno : -ESRCH;
5274
5275         *uid = p->pw_uid;
5276         *gid = p->pw_gid;
5277         *home = p->pw_dir;
5278         return 0;
5279 }
5280
5281 int glob_exists(const char *path) {
5282         glob_t g;
5283         int r, k;
5284
5285         assert(path);
5286
5287         zero(g);
5288         errno = 0;
5289         k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
5290
5291         if (k == GLOB_NOMATCH)
5292                 r = 0;
5293         else if (k == GLOB_NOSPACE)
5294                 r = -ENOMEM;
5295         else if (k == 0)
5296                 r = !strv_isempty(g.gl_pathv);
5297         else
5298                 r = errno ? -errno : -EIO;
5299
5300         globfree(&g);
5301
5302         return r;
5303 }
5304
5305 int dirent_ensure_type(DIR *d, struct dirent *de) {
5306         struct stat st;
5307
5308         assert(d);
5309         assert(de);
5310
5311         if (de->d_type != DT_UNKNOWN)
5312                 return 0;
5313
5314         if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
5315                 return -errno;
5316
5317         de->d_type =
5318                 S_ISREG(st.st_mode)  ? DT_REG  :
5319                 S_ISDIR(st.st_mode)  ? DT_DIR  :
5320                 S_ISLNK(st.st_mode)  ? DT_LNK  :
5321                 S_ISFIFO(st.st_mode) ? DT_FIFO :
5322                 S_ISSOCK(st.st_mode) ? DT_SOCK :
5323                 S_ISCHR(st.st_mode)  ? DT_CHR  :
5324                 S_ISBLK(st.st_mode)  ? DT_BLK  :
5325                                        DT_UNKNOWN;
5326
5327         return 0;
5328 }
5329
5330 int in_search_path(const char *path, char **search) {
5331         char **i, *parent;
5332         int r;
5333
5334         r = parent_of_path(path, &parent);
5335         if (r < 0)
5336                 return r;
5337
5338         r = 0;
5339
5340         STRV_FOREACH(i, search) {
5341                 if (path_equal(parent, *i)) {
5342                         r = 1;
5343                         break;
5344                 }
5345         }
5346
5347         free(parent);
5348
5349         return r;
5350 }
5351
5352 static const char *const ioprio_class_table[] = {
5353         [IOPRIO_CLASS_NONE] = "none",
5354         [IOPRIO_CLASS_RT] = "realtime",
5355         [IOPRIO_CLASS_BE] = "best-effort",
5356         [IOPRIO_CLASS_IDLE] = "idle"
5357 };
5358
5359 DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
5360
5361 static const char *const sigchld_code_table[] = {
5362         [CLD_EXITED] = "exited",
5363         [CLD_KILLED] = "killed",
5364         [CLD_DUMPED] = "dumped",
5365         [CLD_TRAPPED] = "trapped",
5366         [CLD_STOPPED] = "stopped",
5367         [CLD_CONTINUED] = "continued",
5368 };
5369
5370 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
5371
5372 static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
5373         [LOG_FAC(LOG_KERN)] = "kern",
5374         [LOG_FAC(LOG_USER)] = "user",
5375         [LOG_FAC(LOG_MAIL)] = "mail",
5376         [LOG_FAC(LOG_DAEMON)] = "daemon",
5377         [LOG_FAC(LOG_AUTH)] = "auth",
5378         [LOG_FAC(LOG_SYSLOG)] = "syslog",
5379         [LOG_FAC(LOG_LPR)] = "lpr",
5380         [LOG_FAC(LOG_NEWS)] = "news",
5381         [LOG_FAC(LOG_UUCP)] = "uucp",
5382         [LOG_FAC(LOG_CRON)] = "cron",
5383         [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
5384         [LOG_FAC(LOG_FTP)] = "ftp",
5385         [LOG_FAC(LOG_LOCAL0)] = "local0",
5386         [LOG_FAC(LOG_LOCAL1)] = "local1",
5387         [LOG_FAC(LOG_LOCAL2)] = "local2",
5388         [LOG_FAC(LOG_LOCAL3)] = "local3",
5389         [LOG_FAC(LOG_LOCAL4)] = "local4",
5390         [LOG_FAC(LOG_LOCAL5)] = "local5",
5391         [LOG_FAC(LOG_LOCAL6)] = "local6",
5392         [LOG_FAC(LOG_LOCAL7)] = "local7"
5393 };
5394
5395 DEFINE_STRING_TABLE_LOOKUP(log_facility_unshifted, int);
5396
5397 static const char *const log_level_table[] = {
5398         [LOG_EMERG] = "emerg",
5399         [LOG_ALERT] = "alert",
5400         [LOG_CRIT] = "crit",
5401         [LOG_ERR] = "err",
5402         [LOG_WARNING] = "warning",
5403         [LOG_NOTICE] = "notice",
5404         [LOG_INFO] = "info",
5405         [LOG_DEBUG] = "debug"
5406 };
5407
5408 DEFINE_STRING_TABLE_LOOKUP(log_level, int);
5409
5410 static const char* const sched_policy_table[] = {
5411         [SCHED_OTHER] = "other",
5412         [SCHED_BATCH] = "batch",
5413         [SCHED_IDLE] = "idle",
5414         [SCHED_FIFO] = "fifo",
5415         [SCHED_RR] = "rr"
5416 };
5417
5418 DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
5419
5420 static const char* const rlimit_table[] = {
5421         [RLIMIT_CPU] = "LimitCPU",
5422         [RLIMIT_FSIZE] = "LimitFSIZE",
5423         [RLIMIT_DATA] = "LimitDATA",
5424         [RLIMIT_STACK] = "LimitSTACK",
5425         [RLIMIT_CORE] = "LimitCORE",
5426         [RLIMIT_RSS] = "LimitRSS",
5427         [RLIMIT_NOFILE] = "LimitNOFILE",
5428         [RLIMIT_AS] = "LimitAS",
5429         [RLIMIT_NPROC] = "LimitNPROC",
5430         [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
5431         [RLIMIT_LOCKS] = "LimitLOCKS",
5432         [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
5433         [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
5434         [RLIMIT_NICE] = "LimitNICE",
5435         [RLIMIT_RTPRIO] = "LimitRTPRIO",
5436         [RLIMIT_RTTIME] = "LimitRTTIME"
5437 };
5438
5439 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
5440
5441 static const char* const ip_tos_table[] = {
5442         [IPTOS_LOWDELAY] = "low-delay",
5443         [IPTOS_THROUGHPUT] = "throughput",
5444         [IPTOS_RELIABILITY] = "reliability",
5445         [IPTOS_LOWCOST] = "low-cost",
5446 };
5447
5448 DEFINE_STRING_TABLE_LOOKUP(ip_tos, int);
5449
5450 static const char *const signal_table[] = {
5451         [SIGHUP] = "HUP",
5452         [SIGINT] = "INT",
5453         [SIGQUIT] = "QUIT",
5454         [SIGILL] = "ILL",
5455         [SIGTRAP] = "TRAP",
5456         [SIGABRT] = "ABRT",
5457         [SIGBUS] = "BUS",
5458         [SIGFPE] = "FPE",
5459         [SIGKILL] = "KILL",
5460         [SIGUSR1] = "USR1",
5461         [SIGSEGV] = "SEGV",
5462         [SIGUSR2] = "USR2",
5463         [SIGPIPE] = "PIPE",
5464         [SIGALRM] = "ALRM",
5465         [SIGTERM] = "TERM",
5466 #ifdef SIGSTKFLT
5467         [SIGSTKFLT] = "STKFLT",  /* Linux on SPARC doesn't know SIGSTKFLT */
5468 #endif
5469         [SIGCHLD] = "CHLD",
5470         [SIGCONT] = "CONT",
5471         [SIGSTOP] = "STOP",
5472         [SIGTSTP] = "TSTP",
5473         [SIGTTIN] = "TTIN",
5474         [SIGTTOU] = "TTOU",
5475         [SIGURG] = "URG",
5476         [SIGXCPU] = "XCPU",
5477         [SIGXFSZ] = "XFSZ",
5478         [SIGVTALRM] = "VTALRM",
5479         [SIGPROF] = "PROF",
5480         [SIGWINCH] = "WINCH",
5481         [SIGIO] = "IO",
5482         [SIGPWR] = "PWR",
5483         [SIGSYS] = "SYS"
5484 };
5485
5486 DEFINE_STRING_TABLE_LOOKUP(signal, int);