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