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