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