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