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