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