]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/util.c
units: wording fix
[thirdparty/systemd.git] / src / util.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
60918275 2
a7334b09
LP
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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
60918275
LP
22#include <assert.h>
23#include <string.h>
24#include <unistd.h>
25#include <errno.h>
85261803 26#include <stdlib.h>
034c6ed7
LP
27#include <signal.h>
28#include <stdio.h>
1dccbe19
LP
29#include <syslog.h>
30#include <sched.h>
31#include <sys/resource.h>
ef886c6a 32#include <linux/sched.h>
a9f5d454
LP
33#include <sys/types.h>
34#include <sys/stat.h>
3a0ecb08 35#include <fcntl.h>
a0d40ac5 36#include <dirent.h>
601f6a1e
LP
37#include <sys/ioctl.h>
38#include <linux/vt.h>
39#include <linux/tiocl.h>
80876c20
LP
40#include <termios.h>
41#include <stdarg.h>
42#include <sys/inotify.h>
43#include <sys/poll.h>
8d567588 44#include <libgen.h>
3177a7fa 45#include <ctype.h>
5b6319dc 46#include <sys/prctl.h>
ef2f1067
LP
47#include <sys/utsname.h>
48#include <pwd.h>
4fd5948e 49#include <netinet/ip.h>
3fe5e5d4 50#include <linux/kd.h>
afea26ad 51#include <dlfcn.h>
2e78aa99 52#include <sys/wait.h>
ac123445 53#include <sys/capability.h>
60918275
LP
54
55#include "macro.h"
56#include "util.h"
1dccbe19
LP
57#include "ioprio.h"
58#include "missing.h"
a9f5d454 59#include "log.h"
65d2ebdc 60#include "strv.h"
e51bc1a2 61#include "label.h"
d06dacd0 62#include "exit-status.h"
83cc030f 63#include "hashmap.h"
56cf987f 64
37f85e66 65size_t page_size(void) {
66 static __thread size_t pgsz = 0;
67 long r;
68
69 if (pgsz)
70 return pgsz;
71
72 assert_se((r = sysconf(_SC_PAGESIZE)) > 0);
73
74 pgsz = (size_t) r;
75
76 return pgsz;
77}
78
e05797fb
LP
79bool streq_ptr(const char *a, const char *b) {
80
81 /* Like streq(), but tries to make sense of NULL pointers */
82
83 if (a && b)
84 return streq(a, b);
85
86 if (!a && !b)
87 return true;
88
89 return false;
90}
91
47be870b 92usec_t now(clockid_t clock_id) {
60918275
LP
93 struct timespec ts;
94
47be870b 95 assert_se(clock_gettime(clock_id, &ts) == 0);
60918275
LP
96
97 return timespec_load(&ts);
98}
99
63983207 100dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
871d7de4
LP
101 assert(ts);
102
103 ts->realtime = now(CLOCK_REALTIME);
104 ts->monotonic = now(CLOCK_MONOTONIC);
105
106 return ts;
107}
108
60918275
LP
109usec_t timespec_load(const struct timespec *ts) {
110 assert(ts);
111
112 return
113 (usec_t) ts->tv_sec * USEC_PER_SEC +
114 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
115}
116
117struct timespec *timespec_store(struct timespec *ts, usec_t u) {
118 assert(ts);
119
120 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
121 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
122
123 return ts;
124}
125
126usec_t timeval_load(const struct timeval *tv) {
127 assert(tv);
128
129 return
130 (usec_t) tv->tv_sec * USEC_PER_SEC +
131 (usec_t) tv->tv_usec;
132}
133
134struct timeval *timeval_store(struct timeval *tv, usec_t u) {
135 assert(tv);
136
137 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
138 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
139
140 return tv;
141}
142
143bool endswith(const char *s, const char *postfix) {
144 size_t sl, pl;
145
146 assert(s);
147 assert(postfix);
148
149 sl = strlen(s);
150 pl = strlen(postfix);
151
d4d0d4db
LP
152 if (pl == 0)
153 return true;
154
60918275
LP
155 if (sl < pl)
156 return false;
157
158 return memcmp(s + sl - pl, postfix, pl) == 0;
159}
160
161bool startswith(const char *s, const char *prefix) {
162 size_t sl, pl;
163
164 assert(s);
165 assert(prefix);
166
167 sl = strlen(s);
168 pl = strlen(prefix);
169
d4d0d4db
LP
170 if (pl == 0)
171 return true;
172
60918275
LP
173 if (sl < pl)
174 return false;
175
176 return memcmp(s, prefix, pl) == 0;
177}
178
3177a7fa
MAP
179bool startswith_no_case(const char *s, const char *prefix) {
180 size_t sl, pl;
181 unsigned i;
182
183 assert(s);
184 assert(prefix);
185
186 sl = strlen(s);
187 pl = strlen(prefix);
188
189 if (pl == 0)
190 return true;
191
192 if (sl < pl)
193 return false;
194
195 for(i = 0; i < pl; ++i) {
196 if (tolower(s[i]) != tolower(prefix[i]))
197 return false;
198 }
199
200 return true;
201}
202
79d6d816
LP
203bool first_word(const char *s, const char *word) {
204 size_t sl, wl;
205
206 assert(s);
207 assert(word);
208
209 sl = strlen(s);
210 wl = strlen(word);
211
212 if (sl < wl)
213 return false;
214
d4d0d4db
LP
215 if (wl == 0)
216 return true;
217
79d6d816
LP
218 if (memcmp(s, word, wl) != 0)
219 return false;
220
d4d0d4db
LP
221 return s[wl] == 0 ||
222 strchr(WHITESPACE, s[wl]);
79d6d816
LP
223}
224
42f4e3c4 225int close_nointr(int fd) {
60918275
LP
226 assert(fd >= 0);
227
228 for (;;) {
229 int r;
230
231 if ((r = close(fd)) >= 0)
232 return r;
233
234 if (errno != EINTR)
235 return r;
236 }
237}
85261803 238
85f136b5 239void close_nointr_nofail(int fd) {
80876c20 240 int saved_errno = errno;
85f136b5
LP
241
242 /* like close_nointr() but cannot fail, and guarantees errno
243 * is unchanged */
244
245 assert_se(close_nointr(fd) == 0);
80876c20
LP
246
247 errno = saved_errno;
85f136b5
LP
248}
249
5b6319dc
LP
250void close_many(const int fds[], unsigned n_fd) {
251 unsigned i;
252
253 for (i = 0; i < n_fd; i++)
254 close_nointr_nofail(fds[i]);
255}
256
85261803
LP
257int parse_boolean(const char *v) {
258 assert(v);
259
44d8db9e 260 if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
85261803 261 return 1;
44d8db9e 262 else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
85261803
LP
263 return 0;
264
265 return -EINVAL;
266}
267
3ba686c1 268int parse_pid(const char *s, pid_t* ret_pid) {
0b172489 269 unsigned long ul = 0;
3ba686c1
LP
270 pid_t pid;
271 int r;
272
273 assert(s);
274 assert(ret_pid);
275
276 if ((r = safe_atolu(s, &ul)) < 0)
277 return r;
278
279 pid = (pid_t) ul;
280
281 if ((unsigned long) pid != ul)
282 return -ERANGE;
283
284 if (pid <= 0)
285 return -ERANGE;
286
287 *ret_pid = pid;
288 return 0;
289}
290
85261803
LP
291int safe_atou(const char *s, unsigned *ret_u) {
292 char *x = NULL;
034c6ed7 293 unsigned long l;
85261803
LP
294
295 assert(s);
296 assert(ret_u);
297
298 errno = 0;
299 l = strtoul(s, &x, 0);
300
301 if (!x || *x || errno)
302 return errno ? -errno : -EINVAL;
303
034c6ed7 304 if ((unsigned long) (unsigned) l != l)
85261803
LP
305 return -ERANGE;
306
307 *ret_u = (unsigned) l;
308 return 0;
309}
310
311int safe_atoi(const char *s, int *ret_i) {
312 char *x = NULL;
034c6ed7 313 long l;
85261803
LP
314
315 assert(s);
316 assert(ret_i);
317
318 errno = 0;
319 l = strtol(s, &x, 0);
320
321 if (!x || *x || errno)
322 return errno ? -errno : -EINVAL;
323
034c6ed7 324 if ((long) (int) l != l)
85261803
LP
325 return -ERANGE;
326
034c6ed7
LP
327 *ret_i = (int) l;
328 return 0;
329}
330
034c6ed7
LP
331int safe_atollu(const char *s, long long unsigned *ret_llu) {
332 char *x = NULL;
333 unsigned long long l;
334
335 assert(s);
336 assert(ret_llu);
337
338 errno = 0;
339 l = strtoull(s, &x, 0);
340
341 if (!x || *x || errno)
342 return errno ? -errno : -EINVAL;
343
344 *ret_llu = l;
345 return 0;
346}
347
348int safe_atolli(const char *s, long long int *ret_lli) {
349 char *x = NULL;
350 long long l;
351
352 assert(s);
353 assert(ret_lli);
354
355 errno = 0;
356 l = strtoll(s, &x, 0);
357
358 if (!x || *x || errno)
359 return errno ? -errno : -EINVAL;
360
361 *ret_lli = l;
85261803
LP
362 return 0;
363}
a41e8209 364
a41e8209 365/* Split a string into words. */
65d2ebdc 366char *split(const char *c, size_t *l, const char *separator, char **state) {
a41e8209
LP
367 char *current;
368
369 current = *state ? *state : (char*) c;
370
371 if (!*current || *c == 0)
372 return NULL;
373
65d2ebdc
LP
374 current += strspn(current, separator);
375 *l = strcspn(current, separator);
82919e3d
LP
376 *state = current+*l;
377
378 return (char*) current;
379}
380
034c6ed7
LP
381/* Split a string into words, but consider strings enclosed in '' and
382 * "" as words even if they include spaces. */
383char *split_quoted(const char *c, size_t *l, char **state) {
0bab36f2
LP
384 char *current, *e;
385 bool escaped = false;
034c6ed7
LP
386
387 current = *state ? *state : (char*) c;
388
389 if (!*current || *c == 0)
390 return NULL;
391
392 current += strspn(current, WHITESPACE);
393
394 if (*current == '\'') {
395 current ++;
034c6ed7 396
0bab36f2
LP
397 for (e = current; *e; e++) {
398 if (escaped)
399 escaped = false;
400 else if (*e == '\\')
401 escaped = true;
402 else if (*e == '\'')
403 break;
404 }
405
406 *l = e-current;
407 *state = *e == 0 ? e : e+1;
034c6ed7
LP
408 } else if (*current == '\"') {
409 current ++;
034c6ed7 410
0bab36f2
LP
411 for (e = current; *e; e++) {
412 if (escaped)
413 escaped = false;
414 else if (*e == '\\')
415 escaped = true;
416 else if (*e == '\"')
417 break;
418 }
419
420 *l = e-current;
421 *state = *e == 0 ? e : e+1;
034c6ed7 422 } else {
0bab36f2
LP
423 for (e = current; *e; e++) {
424 if (escaped)
425 escaped = false;
426 else if (*e == '\\')
427 escaped = true;
428 else if (strchr(WHITESPACE, *e))
429 break;
430 }
431 *l = e-current;
432 *state = e;
034c6ed7
LP
433 }
434
435 return (char*) current;
436}
437
65d2ebdc
LP
438char **split_path_and_make_absolute(const char *p) {
439 char **l;
440 assert(p);
441
442 if (!(l = strv_split(p, ":")))
443 return NULL;
444
445 if (!strv_path_make_absolute_cwd(l)) {
446 strv_free(l);
447 return NULL;
448 }
449
450 return l;
451}
452
034c6ed7
LP
453int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
454 int r;
455 FILE *f;
20c03b7b 456 char fn[PATH_MAX], line[LINE_MAX], *p;
bb00e604 457 long unsigned ppid;
034c6ed7 458
8480e784 459 assert(pid > 0);
034c6ed7
LP
460 assert(_ppid);
461
bb00e604 462 assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
8480e784 463 char_array_0(fn);
034c6ed7
LP
464
465 if (!(f = fopen(fn, "r")))
466 return -errno;
467
468 if (!(fgets(line, sizeof(line), f))) {
469 r = -errno;
470 fclose(f);
471 return r;
472 }
473
474 fclose(f);
475
476 /* Let's skip the pid and comm fields. The latter is enclosed
477 * in () but does not escape any () in its value, so let's
478 * skip over it manually */
479
480 if (!(p = strrchr(line, ')')))
481 return -EIO;
482
483 p++;
484
485 if (sscanf(p, " "
486 "%*c " /* state */
bb00e604 487 "%lu ", /* ppid */
034c6ed7
LP
488 &ppid) != 1)
489 return -EIO;
490
bb00e604 491 if ((long unsigned) (pid_t) ppid != ppid)
034c6ed7
LP
492 return -ERANGE;
493
494 *_ppid = (pid_t) ppid;
495
496 return 0;
497}
498
499int write_one_line_file(const char *fn, const char *line) {
500 FILE *f;
501 int r;
502
503 assert(fn);
504 assert(line);
505
506 if (!(f = fopen(fn, "we")))
507 return -errno;
508
509 if (fputs(line, f) < 0) {
510 r = -errno;
511 goto finish;
512 }
513
8e1bd70d
LP
514 if (!endswith(line, "\n"))
515 fputc('\n', f);
516
151b190e
LP
517 fflush(f);
518
519 if (ferror(f)) {
520 if (errno != 0)
521 r = -errno;
522 else
523 r = -EIO;
524 } else
525 r = 0;
526
034c6ed7
LP
527finish:
528 fclose(f);
529 return r;
530}
531
532int read_one_line_file(const char *fn, char **line) {
533 FILE *f;
534 int r;
97c4a07d 535 char t[LINE_MAX], *c;
034c6ed7
LP
536
537 assert(fn);
538 assert(line);
539
540 if (!(f = fopen(fn, "re")))
541 return -errno;
542
543 if (!(fgets(t, sizeof(t), f))) {
544 r = -errno;
545 goto finish;
546 }
547
548 if (!(c = strdup(t))) {
549 r = -ENOMEM;
550 goto finish;
551 }
552
6f9a471a
LP
553 truncate_nl(c);
554
034c6ed7
LP
555 *line = c;
556 r = 0;
557
558finish:
559 fclose(f);
560 return r;
561}
44d8db9e 562
97c4a07d
LP
563int read_full_file(const char *fn, char **contents) {
564 FILE *f;
565 int r;
566 size_t n, l;
567 char *buf = NULL;
568 struct stat st;
569
570 if (!(f = fopen(fn, "re")))
571 return -errno;
572
573 if (fstat(fileno(f), &st) < 0) {
574 r = -errno;
575 goto finish;
576 }
577
578 n = st.st_size > 0 ? st.st_size : LINE_MAX;
579 l = 0;
580
581 for (;;) {
582 char *t;
583 size_t k;
584
585 if (!(t = realloc(buf, n+1))) {
586 r = -ENOMEM;
587 goto finish;
588 }
589
590 buf = t;
591 k = fread(buf + l, 1, n - l, f);
592
593 if (k <= 0) {
594 if (ferror(f)) {
595 r = -errno;
596 goto finish;
597 }
598
599 break;
600 }
601
602 l += k;
603 n *= 2;
604
605 /* Safety check */
606 if (n > 4*1024*1024) {
607 r = -E2BIG;
608 goto finish;
609 }
610 }
611
612 if (buf)
613 buf[l] = 0;
614 else if (!(buf = calloc(1, 1))) {
615 r = -errno;
616 goto finish;
617 }
618
619 *contents = buf;
620 buf = NULL;
621
622 r = 0;
623
624finish:
625 fclose(f);
626 free(buf);
627
628 return r;
629}
630
631int parse_env_file(
632 const char *fname,
c899f8c6 633 const char *separator, ...) {
97c4a07d 634
ce8a6aa1 635 int r = 0;
97c4a07d
LP
636 char *contents, *p;
637
638 assert(fname);
c899f8c6 639 assert(separator);
97c4a07d
LP
640
641 if ((r = read_full_file(fname, &contents)) < 0)
642 return r;
643
644 p = contents;
645 for (;;) {
646 const char *key = NULL;
647
c899f8c6 648 p += strspn(p, separator);
97c4a07d
LP
649 p += strspn(p, WHITESPACE);
650
651 if (!*p)
652 break;
653
654 if (!strchr(COMMENTS, *p)) {
655 va_list ap;
656 char **value;
657
c899f8c6 658 va_start(ap, separator);
97c4a07d
LP
659 while ((key = va_arg(ap, char *))) {
660 size_t n;
661 char *v;
662
663 value = va_arg(ap, char **);
664
665 n = strlen(key);
666 if (strncmp(p, key, n) != 0 ||
667 p[n] != '=')
668 continue;
669
670 p += n + 1;
c899f8c6 671 n = strcspn(p, separator);
97c4a07d
LP
672
673 if (n >= 2 &&
e7db37dd
LP
674 strchr(QUOTES, p[0]) &&
675 p[n-1] == p[0])
97c4a07d
LP
676 v = strndup(p+1, n-2);
677 else
678 v = strndup(p, n);
679
680 if (!v) {
681 r = -ENOMEM;
682 va_end(ap);
683 goto fail;
684 }
685
dd36de4d
KS
686 if (v[0] == '\0') {
687 /* return empty value strings as NULL */
688 free(v);
689 v = NULL;
690 }
691
97c4a07d
LP
692 free(*value);
693 *value = v;
694
695 p += n;
ce8a6aa1
LP
696
697 r ++;
97c4a07d
LP
698 break;
699 }
700 va_end(ap);
701 }
702
703 if (!key)
c899f8c6 704 p += strcspn(p, separator);
97c4a07d
LP
705 }
706
97c4a07d
LP
707fail:
708 free(contents);
709 return r;
710}
711
8c7be95e
LP
712int load_env_file(
713 const char *fname,
714 char ***rl) {
715
716 FILE *f;
717 char **m = 0;
718 int r;
719
720 assert(fname);
721 assert(rl);
722
723 if (!(f = fopen(fname, "re")))
724 return -errno;
725
726 while (!feof(f)) {
727 char l[LINE_MAX], *p, *u;
728 char **t;
729
730 if (!fgets(l, sizeof(l), f)) {
731 if (feof(f))
732 break;
733
734 r = -errno;
735 goto finish;
736 }
737
738 p = strstrip(l);
739
740 if (!*p)
741 continue;
742
743 if (strchr(COMMENTS, *p))
744 continue;
745
746 if (!(u = normalize_env_assignment(p))) {
747 log_error("Out of memory");
748 r = -ENOMEM;
749 goto finish;
750 }
751
752 t = strv_append(m, u);
753 free(u);
754
755 if (!t) {
756 log_error("Out of memory");
757 r = -ENOMEM;
758 goto finish;
759 }
760
761 strv_free(m);
762 m = t;
763 }
764
765 r = 0;
766
767 *rl = m;
768 m = NULL;
769
770finish:
771 if (f)
772 fclose(f);
773
774 strv_free(m);
775
776 return r;
777}
778
7072ced8
LP
779char *truncate_nl(char *s) {
780 assert(s);
781
782 s[strcspn(s, NEWLINE)] = 0;
783 return s;
784}
785
786int get_process_name(pid_t pid, char **name) {
787 char *p;
788 int r;
789
790 assert(pid >= 1);
791 assert(name);
792
bb00e604 793 if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
7072ced8
LP
794 return -ENOMEM;
795
796 r = read_one_line_file(p, name);
797 free(p);
798
799 if (r < 0)
800 return r;
801
7072ced8
LP
802 return 0;
803}
804
c59760ee
LP
805int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
806 char *p, *r, *k;
807 int c;
808 bool space = false;
809 size_t left;
810 FILE *f;
811
812 assert(pid >= 1);
813 assert(max_length > 0);
814 assert(line);
815
816 if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
817 return -ENOMEM;
818
819 f = fopen(p, "r");
820 free(p);
821
822 if (!f)
823 return -errno;
824
825 if (!(r = new(char, max_length))) {
826 fclose(f);
827 return -ENOMEM;
828 }
829
830 k = r;
831 left = max_length;
832 while ((c = getc(f)) != EOF) {
833
834 if (isprint(c)) {
835 if (space) {
836 if (left <= 4)
837 break;
838
839 *(k++) = ' ';
057fbb58 840 left--;
c59760ee
LP
841 space = false;
842 }
843
844 if (left <= 4)
845 break;
846
847 *(k++) = (char) c;
057fbb58 848 left--;
c59760ee
LP
849 } else
850 space = true;
851 }
852
853 if (left <= 4) {
854 size_t n = MIN(left-1, 3U);
855 memcpy(k, "...", n);
856 k[n] = 0;
857 } else
858 *k = 0;
859
860 fclose(f);
861
35d2e7ec
LP
862 /* Kernel threads have no argv[] */
863 if (r[0] == 0) {
864 char *t;
865 int h;
866
867 free(r);
868
869 if ((h = get_process_name(pid, &t)) < 0)
870 return h;
871
872 h = asprintf(&r, "[%s]", t);
873 free(t);
874
875 if (h < 0)
876 return -ENOMEM;
877 }
fa776d8e 878
c59760ee
LP
879 *line = r;
880 return 0;
881}
882
fab56fc5
LP
883char *strnappend(const char *s, const char *suffix, size_t b) {
884 size_t a;
44d8db9e
LP
885 char *r;
886
fab56fc5
LP
887 if (!s && !suffix)
888 return strdup("");
889
890 if (!s)
891 return strndup(suffix, b);
892
893 if (!suffix)
894 return strdup(s);
895
44d8db9e
LP
896 assert(s);
897 assert(suffix);
898
899 a = strlen(s);
44d8db9e
LP
900
901 if (!(r = new(char, a+b+1)))
902 return NULL;
903
904 memcpy(r, s, a);
905 memcpy(r+a, suffix, b);
906 r[a+b] = 0;
907
908 return r;
909}
87f0e418 910
fab56fc5
LP
911char *strappend(const char *s, const char *suffix) {
912 return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
913}
914
87f0e418
LP
915int readlink_malloc(const char *p, char **r) {
916 size_t l = 100;
917
918 assert(p);
919 assert(r);
920
921 for (;;) {
922 char *c;
923 ssize_t n;
924
925 if (!(c = new(char, l)))
926 return -ENOMEM;
927
928 if ((n = readlink(p, c, l-1)) < 0) {
929 int ret = -errno;
930 free(c);
931 return ret;
932 }
933
934 if ((size_t) n < l-1) {
935 c[n] = 0;
936 *r = c;
937 return 0;
938 }
939
940 free(c);
941 l *= 2;
942 }
943}
944
2c7108c4
LP
945int readlink_and_make_absolute(const char *p, char **r) {
946 char *target, *k;
947 int j;
948
949 assert(p);
950 assert(r);
951
952 if ((j = readlink_malloc(p, &target)) < 0)
953 return j;
954
955 k = file_in_same_dir(p, target);
956 free(target);
957
958 if (!k)
959 return -ENOMEM;
960
961 *r = k;
962 return 0;
963}
964
35d2e7ec
LP
965int parent_of_path(const char *path, char **_r) {
966 const char *e, *a = NULL, *b = NULL, *p;
967 char *r;
968 bool slash = false;
969
970 assert(path);
971 assert(_r);
972
973 if (!*path)
974 return -EINVAL;
975
976 for (e = path; *e; e++) {
977
978 if (!slash && *e == '/') {
979 a = b;
980 b = e;
981 slash = true;
982 } else if (slash && *e != '/')
983 slash = false;
984 }
985
986 if (*(e-1) == '/')
987 p = a;
988 else
989 p = b;
990
991 if (!p)
992 return -EINVAL;
993
994 if (p == path)
995 r = strdup("/");
996 else
997 r = strndup(path, p-path);
998
999 if (!r)
1000 return -ENOMEM;
1001
1002 *_r = r;
1003 return 0;
1004}
1005
1006
87f0e418
LP
1007char *file_name_from_path(const char *p) {
1008 char *r;
1009
1010 assert(p);
1011
1012 if ((r = strrchr(p, '/')))
1013 return r + 1;
1014
1015 return (char*) p;
1016}
0301abf4
LP
1017
1018bool path_is_absolute(const char *p) {
1019 assert(p);
1020
1021 return p[0] == '/';
1022}
1023
1024bool is_path(const char *p) {
1025
1026 return !!strchr(p, '/');
1027}
1028
1029char *path_make_absolute(const char *p, const char *prefix) {
1030 char *r;
1031
1032 assert(p);
1033
65d2ebdc
LP
1034 /* Makes every item in the list an absolute path by prepending
1035 * the prefix, if specified and necessary */
1036
0301abf4
LP
1037 if (path_is_absolute(p) || !prefix)
1038 return strdup(p);
1039
1040 if (asprintf(&r, "%s/%s", prefix, p) < 0)
1041 return NULL;
1042
1043 return r;
1044}
2a987ee8 1045
65d2ebdc
LP
1046char *path_make_absolute_cwd(const char *p) {
1047 char *cwd, *r;
1048
1049 assert(p);
1050
1051 /* Similar to path_make_absolute(), but prefixes with the
1052 * current working directory. */
1053
1054 if (path_is_absolute(p))
1055 return strdup(p);
1056
1057 if (!(cwd = get_current_dir_name()))
1058 return NULL;
1059
1060 r = path_make_absolute(p, cwd);
1061 free(cwd);
1062
1063 return r;
1064}
1065
1066char **strv_path_make_absolute_cwd(char **l) {
1067 char **s;
1068
1069 /* Goes through every item in the string list and makes it
1070 * absolute. This works in place and won't rollback any
1071 * changes on failure. */
1072
1073 STRV_FOREACH(s, l) {
1074 char *t;
1075
1076 if (!(t = path_make_absolute_cwd(*s)))
1077 return NULL;
1078
1079 free(*s);
1080 *s = t;
1081 }
1082
1083 return l;
1084}
1085
c3f6d675
LP
1086char **strv_path_canonicalize(char **l) {
1087 char **s;
1088 unsigned k = 0;
1089 bool enomem = false;
1090
1091 if (strv_isempty(l))
1092 return l;
1093
1094 /* Goes through every item in the string list and canonicalize
1095 * the path. This works in place and won't rollback any
1096 * changes on failure. */
1097
1098 STRV_FOREACH(s, l) {
1099 char *t, *u;
1100
1101 t = path_make_absolute_cwd(*s);
1102 free(*s);
1103
1104 if (!t) {
1105 enomem = true;
1106 continue;
1107 }
1108
1109 errno = 0;
1110 u = canonicalize_file_name(t);
1111 free(t);
1112
1113 if (!u) {
1114 if (errno == ENOMEM || !errno)
1115 enomem = true;
1116
1117 continue;
1118 }
1119
1120 l[k++] = u;
1121 }
1122
1123 l[k] = NULL;
1124
1125 if (enomem)
1126 return NULL;
1127
1128 return l;
1129}
1130
2a987ee8
LP
1131int reset_all_signal_handlers(void) {
1132 int sig;
1133
1134 for (sig = 1; sig < _NSIG; sig++) {
1135 struct sigaction sa;
1136
1137 if (sig == SIGKILL || sig == SIGSTOP)
1138 continue;
1139
1140 zero(sa);
1141 sa.sa_handler = SIG_DFL;
431c32bf 1142 sa.sa_flags = SA_RESTART;
2a987ee8
LP
1143
1144 /* On Linux the first two RT signals are reserved by
1145 * glibc, and sigaction() will return EINVAL for them. */
1146 if ((sigaction(sig, &sa, NULL) < 0))
1147 if (errno != EINVAL)
1148 return -errno;
1149 }
1150
8e274523 1151 return 0;
2a987ee8 1152}
4a72ff34
LP
1153
1154char *strstrip(char *s) {
1155 char *e, *l = NULL;
1156
1157 /* Drops trailing whitespace. Modifies the string in
1158 * place. Returns pointer to first non-space character */
1159
1160 s += strspn(s, WHITESPACE);
1161
1162 for (e = s; *e; e++)
1163 if (!strchr(WHITESPACE, *e))
1164 l = e;
1165
1166 if (l)
1167 *(l+1) = 0;
1168 else
1169 *s = 0;
1170
1171 return s;
4a72ff34
LP
1172}
1173
ee9b5e01
LP
1174char *delete_chars(char *s, const char *bad) {
1175 char *f, *t;
1176
1177 /* Drops all whitespace, regardless where in the string */
1178
1179 for (f = s, t = s; *f; f++) {
1180 if (strchr(bad, *f))
1181 continue;
1182
1183 *(t++) = *f;
1184 }
1185
1186 *t = 0;
1187
1188 return s;
1189}
1190
4a72ff34
LP
1191char *file_in_same_dir(const char *path, const char *filename) {
1192 char *e, *r;
1193 size_t k;
1194
1195 assert(path);
1196 assert(filename);
1197
1198 /* This removes the last component of path and appends
1199 * filename, unless the latter is absolute anyway or the
1200 * former isn't */
1201
1202 if (path_is_absolute(filename))
1203 return strdup(filename);
1204
1205 if (!(e = strrchr(path, '/')))
1206 return strdup(filename);
1207
1208 k = strlen(filename);
1209 if (!(r = new(char, e-path+1+k+1)))
1210 return NULL;
1211
1212 memcpy(r, path, e-path+1);
1213 memcpy(r+(e-path)+1, filename, k+1);
1214
1215 return r;
1216}
fb624d04 1217
8c6db833
LP
1218int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
1219 struct stat st;
1220
56cf987f 1221 if (label_mkdir(path, mode) >= 0)
8c6db833
LP
1222 if (chmod_and_chown(path, mode, uid, gid) < 0)
1223 return -errno;
1224
1225 if (lstat(path, &st) < 0)
1226 return -errno;
1227
1228 if ((st.st_mode & 0777) != mode ||
1229 st.st_uid != uid ||
1230 st.st_gid != gid ||
1231 !S_ISDIR(st.st_mode)) {
1232 errno = EEXIST;
1233 return -errno;
1234 }
1235
1236 return 0;
1237}
1238
1239
a9f5d454
LP
1240int mkdir_parents(const char *path, mode_t mode) {
1241 const char *p, *e;
1242
1243 assert(path);
1244
1245 /* Creates every parent directory in the path except the last
1246 * component. */
1247
1248 p = path + strspn(path, "/");
1249 for (;;) {
1250 int r;
1251 char *t;
1252
1253 e = p + strcspn(p, "/");
1254 p = e + strspn(e, "/");
1255
1256 /* Is this the last component? If so, then we're
1257 * done */
1258 if (*p == 0)
1259 return 0;
1260
1261 if (!(t = strndup(path, e - path)))
1262 return -ENOMEM;
1263
56cf987f 1264 r = label_mkdir(t, mode);
a9f5d454
LP
1265 free(t);
1266
1267 if (r < 0 && errno != EEXIST)
1268 return -errno;
1269 }
1270}
1271
bbd67135
LP
1272int mkdir_p(const char *path, mode_t mode) {
1273 int r;
1274
1275 /* Like mkdir -p */
1276
1277 if ((r = mkdir_parents(path, mode)) < 0)
1278 return r;
1279
56cf987f 1280 if (label_mkdir(path, mode) < 0 && errno != EEXIST)
bbd67135
LP
1281 return -errno;
1282
1283 return 0;
1284}
1285
c32dd69b
LP
1286int rmdir_parents(const char *path, const char *stop) {
1287 size_t l;
1288 int r = 0;
1289
1290 assert(path);
1291 assert(stop);
1292
1293 l = strlen(path);
1294
1295 /* Skip trailing slashes */
1296 while (l > 0 && path[l-1] == '/')
1297 l--;
1298
1299 while (l > 0) {
1300 char *t;
1301
1302 /* Skip last component */
1303 while (l > 0 && path[l-1] != '/')
1304 l--;
1305
1306 /* Skip trailing slashes */
1307 while (l > 0 && path[l-1] == '/')
1308 l--;
1309
1310 if (l <= 0)
1311 break;
1312
1313 if (!(t = strndup(path, l)))
1314 return -ENOMEM;
1315
1316 if (path_startswith(stop, t)) {
1317 free(t);
1318 return 0;
1319 }
1320
1321 r = rmdir(t);
1322 free(t);
1323
1324 if (r < 0)
1325 if (errno != ENOENT)
1326 return -errno;
1327 }
1328
1329 return 0;
1330}
1331
1332
fb624d04
LP
1333char hexchar(int x) {
1334 static const char table[16] = "0123456789abcdef";
1335
1336 return table[x & 15];
1337}
4fe88d28
LP
1338
1339int unhexchar(char c) {
1340
1341 if (c >= '0' && c <= '9')
1342 return c - '0';
1343
1344 if (c >= 'a' && c <= 'f')
ea430986 1345 return c - 'a' + 10;
4fe88d28
LP
1346
1347 if (c >= 'A' && c <= 'F')
ea430986 1348 return c - 'A' + 10;
4fe88d28
LP
1349
1350 return -1;
1351}
1352
1353char octchar(int x) {
1354 return '0' + (x & 7);
1355}
1356
1357int unoctchar(char c) {
1358
1359 if (c >= '0' && c <= '7')
1360 return c - '0';
1361
1362 return -1;
1363}
1364
5af98f82
LP
1365char decchar(int x) {
1366 return '0' + (x % 10);
1367}
1368
1369int undecchar(char c) {
1370
1371 if (c >= '0' && c <= '9')
1372 return c - '0';
1373
1374 return -1;
1375}
1376
4fe88d28
LP
1377char *cescape(const char *s) {
1378 char *r, *t;
1379 const char *f;
1380
1381 assert(s);
1382
1383 /* Does C style string escaping. */
1384
1385 if (!(r = new(char, strlen(s)*4 + 1)))
1386 return NULL;
1387
1388 for (f = s, t = r; *f; f++)
1389
1390 switch (*f) {
1391
1392 case '\a':
1393 *(t++) = '\\';
1394 *(t++) = 'a';
1395 break;
1396 case '\b':
1397 *(t++) = '\\';
1398 *(t++) = 'b';
1399 break;
1400 case '\f':
1401 *(t++) = '\\';
1402 *(t++) = 'f';
1403 break;
1404 case '\n':
1405 *(t++) = '\\';
1406 *(t++) = 'n';
1407 break;
1408 case '\r':
1409 *(t++) = '\\';
1410 *(t++) = 'r';
1411 break;
1412 case '\t':
1413 *(t++) = '\\';
1414 *(t++) = 't';
1415 break;
1416 case '\v':
1417 *(t++) = '\\';
1418 *(t++) = 'v';
1419 break;
1420 case '\\':
1421 *(t++) = '\\';
1422 *(t++) = '\\';
1423 break;
1424 case '"':
1425 *(t++) = '\\';
1426 *(t++) = '"';
1427 break;
1428 case '\'':
1429 *(t++) = '\\';
1430 *(t++) = '\'';
1431 break;
1432
1433 default:
1434 /* For special chars we prefer octal over
1435 * hexadecimal encoding, simply because glib's
1436 * g_strescape() does the same */
1437 if ((*f < ' ') || (*f >= 127)) {
1438 *(t++) = '\\';
1439 *(t++) = octchar((unsigned char) *f >> 6);
1440 *(t++) = octchar((unsigned char) *f >> 3);
1441 *(t++) = octchar((unsigned char) *f);
1442 } else
1443 *(t++) = *f;
1444 break;
1445 }
1446
1447 *t = 0;
1448
1449 return r;
1450}
1451
6febfd0d 1452char *cunescape_length(const char *s, size_t length) {
4fe88d28
LP
1453 char *r, *t;
1454 const char *f;
1455
1456 assert(s);
1457
1458 /* Undoes C style string escaping */
1459
6febfd0d 1460 if (!(r = new(char, length+1)))
4fe88d28
LP
1461 return r;
1462
6febfd0d 1463 for (f = s, t = r; f < s + length; f++) {
4fe88d28
LP
1464
1465 if (*f != '\\') {
1466 *(t++) = *f;
1467 continue;
1468 }
1469
1470 f++;
1471
1472 switch (*f) {
1473
1474 case 'a':
1475 *(t++) = '\a';
1476 break;
1477 case 'b':
1478 *(t++) = '\b';
1479 break;
1480 case 'f':
1481 *(t++) = '\f';
1482 break;
1483 case 'n':
1484 *(t++) = '\n';
1485 break;
1486 case 'r':
1487 *(t++) = '\r';
1488 break;
1489 case 't':
1490 *(t++) = '\t';
1491 break;
1492 case 'v':
1493 *(t++) = '\v';
1494 break;
1495 case '\\':
1496 *(t++) = '\\';
1497 break;
1498 case '"':
1499 *(t++) = '"';
1500 break;
1501 case '\'':
1502 *(t++) = '\'';
1503 break;
1504
e167fb86
LP
1505 case 's':
1506 /* This is an extension of the XDG syntax files */
1507 *(t++) = ' ';
1508 break;
1509
4fe88d28
LP
1510 case 'x': {
1511 /* hexadecimal encoding */
1512 int a, b;
1513
1514 if ((a = unhexchar(f[1])) < 0 ||
1515 (b = unhexchar(f[2])) < 0) {
1516 /* Invalid escape code, let's take it literal then */
1517 *(t++) = '\\';
1518 *(t++) = 'x';
1519 } else {
1520 *(t++) = (char) ((a << 4) | b);
1521 f += 2;
1522 }
1523
1524 break;
1525 }
1526
1527 case '0':
1528 case '1':
1529 case '2':
1530 case '3':
1531 case '4':
1532 case '5':
1533 case '6':
1534 case '7': {
1535 /* octal encoding */
1536 int a, b, c;
1537
1538 if ((a = unoctchar(f[0])) < 0 ||
1539 (b = unoctchar(f[1])) < 0 ||
1540 (c = unoctchar(f[2])) < 0) {
1541 /* Invalid escape code, let's take it literal then */
1542 *(t++) = '\\';
1543 *(t++) = f[0];
1544 } else {
1545 *(t++) = (char) ((a << 6) | (b << 3) | c);
1546 f += 2;
1547 }
1548
1549 break;
1550 }
1551
1552 case 0:
1553 /* premature end of string.*/
1554 *(t++) = '\\';
1555 goto finish;
1556
1557 default:
1558 /* Invalid escape code, let's take it literal then */
1559 *(t++) = '\\';
f3d4cc01 1560 *(t++) = *f;
4fe88d28
LP
1561 break;
1562 }
1563 }
1564
1565finish:
1566 *t = 0;
1567 return r;
1568}
1569
6febfd0d
LP
1570char *cunescape(const char *s) {
1571 return cunescape_length(s, strlen(s));
1572}
4fe88d28
LP
1573
1574char *xescape(const char *s, const char *bad) {
1575 char *r, *t;
1576 const char *f;
1577
1578 /* Escapes all chars in bad, in addition to \ and all special
1579 * chars, in \xFF style escaping. May be reversed with
1580 * cunescape. */
1581
1582 if (!(r = new(char, strlen(s)*4+1)))
1583 return NULL;
1584
1585 for (f = s, t = r; *f; f++) {
1586
b866264a
LP
1587 if ((*f < ' ') || (*f >= 127) ||
1588 (*f == '\\') || strchr(bad, *f)) {
4fe88d28
LP
1589 *(t++) = '\\';
1590 *(t++) = 'x';
1591 *(t++) = hexchar(*f >> 4);
1592 *(t++) = hexchar(*f);
1593 } else
1594 *(t++) = *f;
1595 }
1596
1597 *t = 0;
1598
1599 return r;
1600}
1601
ea430986 1602char *bus_path_escape(const char *s) {
ea430986
LP
1603 char *r, *t;
1604 const char *f;
1605
47be870b
LP
1606 assert(s);
1607
ea430986
LP
1608 /* Escapes all chars that D-Bus' object path cannot deal
1609 * with. Can be reverse with bus_path_unescape() */
1610
1611 if (!(r = new(char, strlen(s)*3+1)))
1612 return NULL;
1613
1614 for (f = s, t = r; *f; f++) {
1615
1616 if (!(*f >= 'A' && *f <= 'Z') &&
1617 !(*f >= 'a' && *f <= 'z') &&
1618 !(*f >= '0' && *f <= '9')) {
1619 *(t++) = '_';
1620 *(t++) = hexchar(*f >> 4);
1621 *(t++) = hexchar(*f);
1622 } else
1623 *(t++) = *f;
1624 }
1625
1626 *t = 0;
1627
1628 return r;
1629}
1630
9e2f7c11 1631char *bus_path_unescape(const char *f) {
ea430986 1632 char *r, *t;
ea430986 1633
9e2f7c11 1634 assert(f);
47be870b 1635
9e2f7c11 1636 if (!(r = strdup(f)))
ea430986
LP
1637 return NULL;
1638
9e2f7c11 1639 for (t = r; *f; f++) {
ea430986
LP
1640
1641 if (*f == '_') {
1642 int a, b;
1643
1644 if ((a = unhexchar(f[1])) < 0 ||
1645 (b = unhexchar(f[2])) < 0) {
1646 /* Invalid escape code, let's take it literal then */
1647 *(t++) = '_';
1648 } else {
1649 *(t++) = (char) ((a << 4) | b);
1650 f += 2;
1651 }
1652 } else
1653 *(t++) = *f;
1654 }
1655
1656 *t = 0;
1657
1658 return r;
1659}
1660
4fe88d28
LP
1661char *path_kill_slashes(char *path) {
1662 char *f, *t;
1663 bool slash = false;
1664
1665 /* Removes redundant inner and trailing slashes. Modifies the
1666 * passed string in-place.
1667 *
1668 * ///foo///bar/ becomes /foo/bar
1669 */
1670
1671 for (f = path, t = path; *f; f++) {
1672
1673 if (*f == '/') {
1674 slash = true;
1675 continue;
1676 }
1677
1678 if (slash) {
1679 slash = false;
1680 *(t++) = '/';
1681 }
1682
1683 *(t++) = *f;
1684 }
1685
1686 /* Special rule, if we are talking of the root directory, a
1687 trailing slash is good */
1688
1689 if (t == path && slash)
1690 *(t++) = '/';
1691
1692 *t = 0;
1693 return path;
1694}
1695
1696bool path_startswith(const char *path, const char *prefix) {
1697 assert(path);
1698 assert(prefix);
1699
1700 if ((path[0] == '/') != (prefix[0] == '/'))
1701 return false;
1702
1703 for (;;) {
1704 size_t a, b;
1705
1706 path += strspn(path, "/");
1707 prefix += strspn(prefix, "/");
1708
1709 if (*prefix == 0)
1710 return true;
1711
1712 if (*path == 0)
1713 return false;
1714
1715 a = strcspn(path, "/");
1716 b = strcspn(prefix, "/");
1717
1718 if (a != b)
1719 return false;
1720
1721 if (memcmp(path, prefix, a) != 0)
1722 return false;
1723
1724 path += a;
1725 prefix += b;
1726 }
1727}
1728
15ae422b
LP
1729bool path_equal(const char *a, const char *b) {
1730 assert(a);
1731 assert(b);
1732
1733 if ((a[0] == '/') != (b[0] == '/'))
1734 return false;
1735
1736 for (;;) {
1737 size_t j, k;
1738
1739 a += strspn(a, "/");
1740 b += strspn(b, "/");
1741
1742 if (*a == 0 && *b == 0)
1743 return true;
1744
1745 if (*a == 0 || *b == 0)
1746 return false;
1747
1748 j = strcspn(a, "/");
1749 k = strcspn(b, "/");
1750
1751 if (j != k)
1752 return false;
1753
1754 if (memcmp(a, b, j) != 0)
1755 return false;
1756
1757 a += j;
1758 b += k;
1759 }
1760}
1761
67d51650 1762char *ascii_strlower(char *t) {
4fe88d28
LP
1763 char *p;
1764
67d51650 1765 assert(t);
4fe88d28 1766
67d51650 1767 for (p = t; *p; p++)
4fe88d28
LP
1768 if (*p >= 'A' && *p <= 'Z')
1769 *p = *p - 'A' + 'a';
1770
67d51650 1771 return t;
4fe88d28 1772}
1dccbe19 1773
c85dc17b
LP
1774bool ignore_file(const char *filename) {
1775 assert(filename);
1776
1777 return
1778 filename[0] == '.' ||
6c78be3c 1779 streq(filename, "lost+found") ||
e472d476
LP
1780 streq(filename, "aquota.user") ||
1781 streq(filename, "aquota.group") ||
c85dc17b
LP
1782 endswith(filename, "~") ||
1783 endswith(filename, ".rpmnew") ||
1784 endswith(filename, ".rpmsave") ||
1785 endswith(filename, ".rpmorig") ||
1786 endswith(filename, ".dpkg-old") ||
1787 endswith(filename, ".dpkg-new") ||
1788 endswith(filename, ".swp");
1789}
1790
3a0ecb08
LP
1791int fd_nonblock(int fd, bool nonblock) {
1792 int flags;
1793
1794 assert(fd >= 0);
1795
1796 if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
1797 return -errno;
1798
1799 if (nonblock)
1800 flags |= O_NONBLOCK;
1801 else
1802 flags &= ~O_NONBLOCK;
1803
1804 if (fcntl(fd, F_SETFL, flags) < 0)
1805 return -errno;
1806
1807 return 0;
1808}
1809
1810int fd_cloexec(int fd, bool cloexec) {
1811 int flags;
1812
1813 assert(fd >= 0);
1814
1815 if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
1816 return -errno;
1817
1818 if (cloexec)
1819 flags |= FD_CLOEXEC;
1820 else
1821 flags &= ~FD_CLOEXEC;
1822
1823 if (fcntl(fd, F_SETFD, flags) < 0)
1824 return -errno;
1825
1826 return 0;
1827}
1828
a0d40ac5
LP
1829int close_all_fds(const int except[], unsigned n_except) {
1830 DIR *d;
1831 struct dirent *de;
1832 int r = 0;
1833
1834 if (!(d = opendir("/proc/self/fd")))
1835 return -errno;
1836
1837 while ((de = readdir(d))) {
a7610064 1838 int fd = -1;
a0d40ac5 1839
a16e1123 1840 if (ignore_file(de->d_name))
a0d40ac5
LP
1841 continue;
1842
720ce21d
LP
1843 if (safe_atoi(de->d_name, &fd) < 0)
1844 /* Let's better ignore this, just in case */
1845 continue;
a0d40ac5
LP
1846
1847 if (fd < 3)
1848 continue;
1849
1850 if (fd == dirfd(d))
1851 continue;
1852
1853 if (except) {
1854 bool found;
1855 unsigned i;
1856
1857 found = false;
1858 for (i = 0; i < n_except; i++)
1859 if (except[i] == fd) {
1860 found = true;
1861 break;
1862 }
1863
1864 if (found)
1865 continue;
1866 }
1867
720ce21d 1868 if (close_nointr(fd) < 0) {
2f357920 1869 /* Valgrind has its own FD and doesn't want to have it closed */
720ce21d
LP
1870 if (errno != EBADF && r == 0)
1871 r = -errno;
2f357920 1872 }
a0d40ac5
LP
1873 }
1874
a0d40ac5
LP
1875 closedir(d);
1876 return r;
1877}
1878
db12775d
LP
1879bool chars_intersect(const char *a, const char *b) {
1880 const char *p;
1881
1882 /* Returns true if any of the chars in a are in b. */
1883 for (p = a; *p; p++)
1884 if (strchr(b, *p))
1885 return true;
1886
1887 return false;
1888}
1889
8b6c7120
LP
1890char *format_timestamp(char *buf, size_t l, usec_t t) {
1891 struct tm tm;
1892 time_t sec;
1893
1894 assert(buf);
1895 assert(l > 0);
1896
1897 if (t <= 0)
1898 return NULL;
1899
f872ec33 1900 sec = (time_t) (t / USEC_PER_SEC);
8b6c7120
LP
1901
1902 if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0)
1903 return NULL;
1904
1905 return buf;
1906}
1907
584be568
LP
1908char *format_timestamp_pretty(char *buf, size_t l, usec_t t) {
1909 usec_t n, d;
1910
1911 n = now(CLOCK_REALTIME);
1912
1913 if (t <= 0 || t > n || t + USEC_PER_DAY*7 <= t)
1914 return NULL;
1915
1916 d = n - t;
1917
1918 if (d >= USEC_PER_YEAR)
1919 snprintf(buf, l, "%llu years and %llu months ago",
1920 (unsigned long long) (d / USEC_PER_YEAR),
1921 (unsigned long long) ((d % USEC_PER_YEAR) / USEC_PER_MONTH));
1922 else if (d >= USEC_PER_MONTH)
1923 snprintf(buf, l, "%llu months and %llu days ago",
1924 (unsigned long long) (d / USEC_PER_MONTH),
1925 (unsigned long long) ((d % USEC_PER_MONTH) / USEC_PER_DAY));
1926 else if (d >= USEC_PER_WEEK)
1927 snprintf(buf, l, "%llu weeks and %llu days ago",
1928 (unsigned long long) (d / USEC_PER_WEEK),
1929 (unsigned long long) ((d % USEC_PER_WEEK) / USEC_PER_DAY));
1930 else if (d >= 2*USEC_PER_DAY)
1931 snprintf(buf, l, "%llu days ago", (unsigned long long) (d / USEC_PER_DAY));
1932 else if (d >= 25*USEC_PER_HOUR)
1933 snprintf(buf, l, "1 day and %lluh ago",
1934 (unsigned long long) ((d - USEC_PER_DAY) / USEC_PER_HOUR));
1935 else if (d >= 6*USEC_PER_HOUR)
1936 snprintf(buf, l, "%lluh ago",
1937 (unsigned long long) (d / USEC_PER_HOUR));
1938 else if (d >= USEC_PER_HOUR)
1939 snprintf(buf, l, "%lluh %llumin ago",
1940 (unsigned long long) (d / USEC_PER_HOUR),
1941 (unsigned long long) ((d % USEC_PER_HOUR) / USEC_PER_MINUTE));
1942 else if (d >= 5*USEC_PER_MINUTE)
1943 snprintf(buf, l, "%llumin ago",
1944 (unsigned long long) (d / USEC_PER_MINUTE));
1945 else if (d >= USEC_PER_MINUTE)
1946 snprintf(buf, l, "%llumin %llus ago",
1947 (unsigned long long) (d / USEC_PER_MINUTE),
1948 (unsigned long long) ((d % USEC_PER_MINUTE) / USEC_PER_SEC));
1949 else if (d >= USEC_PER_SEC)
1950 snprintf(buf, l, "%llus ago",
1951 (unsigned long long) (d / USEC_PER_SEC));
1952 else if (d >= USEC_PER_MSEC)
1953 snprintf(buf, l, "%llums ago",
1954 (unsigned long long) (d / USEC_PER_MSEC));
1955 else if (d > 0)
1956 snprintf(buf, l, "%lluus ago",
1957 (unsigned long long) d);
1958 else
1959 snprintf(buf, l, "now");
1960
1961 buf[l-1] = 0;
1962 return buf;
1963}
1964
871d7de4
LP
1965char *format_timespan(char *buf, size_t l, usec_t t) {
1966 static const struct {
1967 const char *suffix;
1968 usec_t usec;
1969 } table[] = {
1970 { "w", USEC_PER_WEEK },
1971 { "d", USEC_PER_DAY },
1972 { "h", USEC_PER_HOUR },
1973 { "min", USEC_PER_MINUTE },
1974 { "s", USEC_PER_SEC },
1975 { "ms", USEC_PER_MSEC },
1976 { "us", 1 },
1977 };
1978
1979 unsigned i;
1980 char *p = buf;
1981
1982 assert(buf);
1983 assert(l > 0);
1984
1985 if (t == (usec_t) -1)
1986 return NULL;
1987
4502d22c
LP
1988 if (t == 0) {
1989 snprintf(p, l, "0");
1990 p[l-1] = 0;
1991 return p;
1992 }
1993
871d7de4
LP
1994 /* The result of this function can be parsed with parse_usec */
1995
1996 for (i = 0; i < ELEMENTSOF(table); i++) {
1997 int k;
1998 size_t n;
1999
2000 if (t < table[i].usec)
2001 continue;
2002
2003 if (l <= 1)
2004 break;
2005
2006 k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix);
2007 n = MIN((size_t) k, l);
2008
2009 l -= n;
2010 p += n;
2011
2012 t %= table[i].usec;
2013 }
2014
2015 *p = 0;
2016
2017 return buf;
2018}
2019
42856c10
LP
2020bool fstype_is_network(const char *fstype) {
2021 static const char * const table[] = {
2022 "cifs",
2023 "smbfs",
2024 "ncpfs",
2025 "nfs",
ca139f94
LP
2026 "nfs4",
2027 "gfs",
2028 "gfs2"
42856c10
LP
2029 };
2030
2031 unsigned i;
2032
2033 for (i = 0; i < ELEMENTSOF(table); i++)
2034 if (streq(table[i], fstype))
2035 return true;
2036
2037 return false;
2038}
2039
601f6a1e
LP
2040int chvt(int vt) {
2041 int fd, r = 0;
2042
2043 if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
2044 return -errno;
2045
2046 if (vt < 0) {
2047 int tiocl[2] = {
2048 TIOCL_GETKMSGREDIRECT,
2049 0
2050 };
2051
2052 if (ioctl(fd, TIOCLINUX, tiocl) < 0)
2053 return -errno;
2054
2055 vt = tiocl[0] <= 0 ? 1 : tiocl[0];
2056 }
2057
2058 if (ioctl(fd, VT_ACTIVATE, vt) < 0)
2059 r = -errno;
2060
a16e1123 2061 close_nointr_nofail(r);
601f6a1e
LP
2062 return r;
2063}
2064
80876c20
LP
2065int read_one_char(FILE *f, char *ret, bool *need_nl) {
2066 struct termios old_termios, new_termios;
2067 char c;
20c03b7b 2068 char line[LINE_MAX];
80876c20
LP
2069
2070 assert(f);
2071 assert(ret);
2072
2073 if (tcgetattr(fileno(f), &old_termios) >= 0) {
2074 new_termios = old_termios;
2075
2076 new_termios.c_lflag &= ~ICANON;
2077 new_termios.c_cc[VMIN] = 1;
2078 new_termios.c_cc[VTIME] = 0;
2079
2080 if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
2081 size_t k;
2082
2083 k = fread(&c, 1, 1, f);
2084
2085 tcsetattr(fileno(f), TCSADRAIN, &old_termios);
2086
2087 if (k <= 0)
2088 return -EIO;
2089
2090 if (need_nl)
2091 *need_nl = c != '\n';
2092
2093 *ret = c;
2094 return 0;
2095 }
2096 }
2097
2098 if (!(fgets(line, sizeof(line), f)))
2099 return -EIO;
2100
2101 truncate_nl(line);
2102
2103 if (strlen(line) != 1)
2104 return -EBADMSG;
2105
2106 if (need_nl)
2107 *need_nl = false;
2108
2109 *ret = line[0];
2110 return 0;
2111}
2112
2113int ask(char *ret, const char *replies, const char *text, ...) {
1b39d4b9
LP
2114 bool on_tty;
2115
80876c20
LP
2116 assert(ret);
2117 assert(replies);
2118 assert(text);
2119
1b39d4b9
LP
2120 on_tty = isatty(STDOUT_FILENO);
2121
80876c20
LP
2122 for (;;) {
2123 va_list ap;
2124 char c;
2125 int r;
2126 bool need_nl = true;
2127
1b39d4b9
LP
2128 if (on_tty)
2129 fputs("\x1B[1m", stdout);
b1b2dc0c 2130
80876c20
LP
2131 va_start(ap, text);
2132 vprintf(text, ap);
2133 va_end(ap);
2134
1b39d4b9
LP
2135 if (on_tty)
2136 fputs("\x1B[0m", stdout);
b1b2dc0c 2137
80876c20
LP
2138 fflush(stdout);
2139
2140 if ((r = read_one_char(stdin, &c, &need_nl)) < 0) {
2141
2142 if (r == -EBADMSG) {
2143 puts("Bad input, please try again.");
2144 continue;
2145 }
2146
2147 putchar('\n');
2148 return r;
2149 }
2150
2151 if (need_nl)
2152 putchar('\n');
2153
2154 if (strchr(replies, c)) {
2155 *ret = c;
2156 return 0;
2157 }
2158
2159 puts("Read unexpected character, please try again.");
2160 }
2161}
2162
2163int reset_terminal(int fd) {
2164 struct termios termios;
2165 int r = 0;
3fe5e5d4
LP
2166 long arg;
2167
2168 /* Set terminal to some sane defaults */
80876c20
LP
2169
2170 assert(fd >= 0);
2171
eed1d0e3
LP
2172 /* We leave locked terminal attributes untouched, so that
2173 * Plymouth may set whatever it wants to set, and we don't
2174 * interfere with that. */
3fe5e5d4
LP
2175
2176 /* Disable exclusive mode, just in case */
2177 ioctl(fd, TIOCNXCL);
2178
2179 /* Enable console unicode mode */
2180 arg = K_UNICODE;
2181 ioctl(fd, KDSKBMODE, &arg);
80876c20
LP
2182
2183 if (tcgetattr(fd, &termios) < 0) {
2184 r = -errno;
2185 goto finish;
2186 }
2187
aaf694ca
LP
2188 /* We only reset the stuff that matters to the software. How
2189 * hardware is set up we don't touch assuming that somebody
2190 * else will do that for us */
2191
2192 termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
80876c20
LP
2193 termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
2194 termios.c_oflag |= ONLCR;
2195 termios.c_cflag |= CREAD;
2196 termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
2197
2198 termios.c_cc[VINTR] = 03; /* ^C */
2199 termios.c_cc[VQUIT] = 034; /* ^\ */
2200 termios.c_cc[VERASE] = 0177;
2201 termios.c_cc[VKILL] = 025; /* ^X */
2202 termios.c_cc[VEOF] = 04; /* ^D */
2203 termios.c_cc[VSTART] = 021; /* ^Q */
2204 termios.c_cc[VSTOP] = 023; /* ^S */
2205 termios.c_cc[VSUSP] = 032; /* ^Z */
2206 termios.c_cc[VLNEXT] = 026; /* ^V */
2207 termios.c_cc[VWERASE] = 027; /* ^W */
2208 termios.c_cc[VREPRINT] = 022; /* ^R */
aaf694ca
LP
2209 termios.c_cc[VEOL] = 0;
2210 termios.c_cc[VEOL2] = 0;
80876c20
LP
2211
2212 termios.c_cc[VTIME] = 0;
2213 termios.c_cc[VMIN] = 1;
2214
2215 if (tcsetattr(fd, TCSANOW, &termios) < 0)
2216 r = -errno;
2217
2218finish:
2219 /* Just in case, flush all crap out */
2220 tcflush(fd, TCIOFLUSH);
2221
2222 return r;
2223}
2224
2225int open_terminal(const char *name, int mode) {
2226 int fd, r;
f73f76ac 2227 unsigned c = 0;
80876c20 2228
f73f76ac
LP
2229 /*
2230 * If a TTY is in the process of being closed opening it might
2231 * cause EIO. This is horribly awful, but unlikely to be
2232 * changed in the kernel. Hence we work around this problem by
2233 * retrying a couple of times.
2234 *
2235 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245
2236 */
2237
2238 for (;;) {
2239 if ((fd = open(name, mode)) >= 0)
2240 break;
2241
2242 if (errno != EIO)
2243 return -errno;
2244
2245 if (c >= 20)
2246 return -errno;
2247
2248 usleep(50 * USEC_PER_MSEC);
2249 c++;
2250 }
2251
2252 if (fd < 0)
80876c20
LP
2253 return -errno;
2254
2255 if ((r = isatty(fd)) < 0) {
2256 close_nointr_nofail(fd);
2257 return -errno;
2258 }
2259
2260 if (!r) {
2261 close_nointr_nofail(fd);
2262 return -ENOTTY;
2263 }
2264
2265 return fd;
2266}
2267
2268int flush_fd(int fd) {
2269 struct pollfd pollfd;
2270
2271 zero(pollfd);
2272 pollfd.fd = fd;
2273 pollfd.events = POLLIN;
2274
2275 for (;;) {
20c03b7b 2276 char buf[LINE_MAX];
80876c20
LP
2277 ssize_t l;
2278 int r;
2279
2280 if ((r = poll(&pollfd, 1, 0)) < 0) {
2281
2282 if (errno == EINTR)
2283 continue;
2284
2285 return -errno;
2286 }
2287
2288 if (r == 0)
2289 return 0;
2290
2291 if ((l = read(fd, buf, sizeof(buf))) < 0) {
2292
2293 if (errno == EINTR)
2294 continue;
2295
2296 if (errno == EAGAIN)
2297 return 0;
2298
2299 return -errno;
2300 }
2301
2302 if (l <= 0)
2303 return 0;
2304 }
2305}
2306
21de3988 2307int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) {
bab45044 2308 int fd = -1, notify = -1, r, wd = -1;
80876c20
LP
2309
2310 assert(name);
2311
2312 /* We use inotify to be notified when the tty is closed. We
2313 * create the watch before checking if we can actually acquire
2314 * it, so that we don't lose any event.
2315 *
2316 * Note: strictly speaking this actually watches for the
2317 * device being closed, it does *not* really watch whether a
2318 * tty loses its controlling process. However, unless some
2319 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
2320 * its tty otherwise this will not become a problem. As long
2321 * as the administrator makes sure not configure any service
2322 * on the same tty as an untrusted user this should not be a
2323 * problem. (Which he probably should not do anyway.) */
2324
2325 if (!fail && !force) {
2326 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
2327 r = -errno;
2328 goto fail;
2329 }
2330
2331 if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) {
2332 r = -errno;
2333 goto fail;
2334 }
2335 }
2336
2337 for (;;) {
e3d1855b
LP
2338 if (notify >= 0)
2339 if ((r = flush_fd(notify)) < 0)
2340 goto fail;
80876c20
LP
2341
2342 /* We pass here O_NOCTTY only so that we can check the return
2343 * value TIOCSCTTY and have a reliable way to figure out if we
2344 * successfully became the controlling process of the tty */
2345 if ((fd = open_terminal(name, O_RDWR|O_NOCTTY)) < 0)
2346 return -errno;
2347
2348 /* First, try to get the tty */
21de3988
LP
2349 r = ioctl(fd, TIOCSCTTY, force);
2350
2351 /* Sometimes it makes sense to ignore TIOCSCTTY
2352 * returning EPERM, i.e. when very likely we already
2353 * are have this controlling terminal. */
2354 if (r < 0 && errno == EPERM && ignore_tiocstty_eperm)
2355 r = 0;
2356
2357 if (r < 0 && (force || fail || errno != EPERM)) {
80876c20
LP
2358 r = -errno;
2359 goto fail;
2360 }
2361
2362 if (r >= 0)
2363 break;
2364
2365 assert(!fail);
2366 assert(!force);
2367 assert(notify >= 0);
2368
2369 for (;;) {
f601daa7 2370 uint8_t inotify_buffer[sizeof(struct inotify_event) + FILENAME_MAX];
80876c20 2371 ssize_t l;
f601daa7 2372 struct inotify_event *e;
80876c20 2373
f601daa7 2374 if ((l = read(notify, &inotify_buffer, sizeof(inotify_buffer))) < 0) {
80876c20 2375
f601daa7
LP
2376 if (errno == EINTR)
2377 continue;
2378
2379 r = -errno;
2380 goto fail;
2381 }
2382
2383 e = (struct inotify_event*) inotify_buffer;
80876c20 2384
f601daa7
LP
2385 while (l > 0) {
2386 size_t step;
80876c20 2387
f601daa7 2388 if (e->wd != wd || !(e->mask & IN_CLOSE)) {
80876c20 2389 r = -EIO;
f601daa7
LP
2390 goto fail;
2391 }
80876c20 2392
f601daa7
LP
2393 step = sizeof(struct inotify_event) + e->len;
2394 assert(step <= (size_t) l);
80876c20 2395
f601daa7
LP
2396 e = (struct inotify_event*) ((uint8_t*) e + step);
2397 l -= step;
80876c20
LP
2398 }
2399
2400 break;
2401 }
2402
2403 /* We close the tty fd here since if the old session
2404 * ended our handle will be dead. It's important that
2405 * we do this after sleeping, so that we don't enter
2406 * an endless loop. */
2407 close_nointr_nofail(fd);
2408 }
2409
2410 if (notify >= 0)
a16e1123 2411 close_nointr_nofail(notify);
80876c20
LP
2412
2413 if ((r = reset_terminal(fd)) < 0)
2414 log_warning("Failed to reset terminal: %s", strerror(-r));
2415
2416 return fd;
2417
2418fail:
2419 if (fd >= 0)
a16e1123 2420 close_nointr_nofail(fd);
80876c20
LP
2421
2422 if (notify >= 0)
a16e1123 2423 close_nointr_nofail(notify);
80876c20
LP
2424
2425 return r;
2426}
2427
2428int release_terminal(void) {
2429 int r = 0, fd;
57cd2192 2430 struct sigaction sa_old, sa_new;
80876c20 2431
57cd2192 2432 if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
80876c20
LP
2433 return -errno;
2434
57cd2192
LP
2435 /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
2436 * by our own TIOCNOTTY */
2437
2438 zero(sa_new);
2439 sa_new.sa_handler = SIG_IGN;
2440 sa_new.sa_flags = SA_RESTART;
2441 assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
2442
80876c20
LP
2443 if (ioctl(fd, TIOCNOTTY) < 0)
2444 r = -errno;
2445
57cd2192
LP
2446 assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
2447
80876c20
LP
2448 close_nointr_nofail(fd);
2449 return r;
2450}
2451
9a34ec5f
LP
2452int sigaction_many(const struct sigaction *sa, ...) {
2453 va_list ap;
2454 int r = 0, sig;
2455
2456 va_start(ap, sa);
2457 while ((sig = va_arg(ap, int)) > 0)
2458 if (sigaction(sig, sa, NULL) < 0)
2459 r = -errno;
2460 va_end(ap);
2461
2462 return r;
2463}
2464
2465int ignore_signals(int sig, ...) {
a337c6fc 2466 struct sigaction sa;
9a34ec5f
LP
2467 va_list ap;
2468 int r = 0;
a337c6fc
LP
2469
2470 zero(sa);
2471 sa.sa_handler = SIG_IGN;
2472 sa.sa_flags = SA_RESTART;
2473
9a34ec5f
LP
2474 if (sigaction(sig, &sa, NULL) < 0)
2475 r = -errno;
2476
2477 va_start(ap, sig);
2478 while ((sig = va_arg(ap, int)) > 0)
2479 if (sigaction(sig, &sa, NULL) < 0)
2480 r = -errno;
2481 va_end(ap);
2482
2483 return r;
2484}
2485
2486int default_signals(int sig, ...) {
2487 struct sigaction sa;
2488 va_list ap;
2489 int r = 0;
2490
2491 zero(sa);
2492 sa.sa_handler = SIG_DFL;
2493 sa.sa_flags = SA_RESTART;
2494
2495 if (sigaction(sig, &sa, NULL) < 0)
2496 r = -errno;
2497
2498 va_start(ap, sig);
2499 while ((sig = va_arg(ap, int)) > 0)
2500 if (sigaction(sig, &sa, NULL) < 0)
2501 r = -errno;
2502 va_end(ap);
2503
2504 return r;
a337c6fc
LP
2505}
2506
8d567588
LP
2507int close_pipe(int p[]) {
2508 int a = 0, b = 0;
2509
2510 assert(p);
2511
2512 if (p[0] >= 0) {
2513 a = close_nointr(p[0]);
2514 p[0] = -1;
2515 }
2516
2517 if (p[1] >= 0) {
2518 b = close_nointr(p[1]);
2519 p[1] = -1;
2520 }
2521
2522 return a < 0 ? a : b;
2523}
2524
eb22ac37 2525ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
8d567588
LP
2526 uint8_t *p;
2527 ssize_t n = 0;
2528
2529 assert(fd >= 0);
2530 assert(buf);
2531
2532 p = buf;
2533
2534 while (nbytes > 0) {
2535 ssize_t k;
2536
2537 if ((k = read(fd, p, nbytes)) <= 0) {
2538
eb22ac37 2539 if (k < 0 && errno == EINTR)
8d567588
LP
2540 continue;
2541
eb22ac37 2542 if (k < 0 && errno == EAGAIN && do_poll) {
8d567588
LP
2543 struct pollfd pollfd;
2544
2545 zero(pollfd);
2546 pollfd.fd = fd;
2547 pollfd.events = POLLIN;
2548
2549 if (poll(&pollfd, 1, -1) < 0) {
2550 if (errno == EINTR)
2551 continue;
2552
2553 return n > 0 ? n : -errno;
2554 }
2555
2556 if (pollfd.revents != POLLIN)
2557 return n > 0 ? n : -EIO;
2558
2559 continue;
2560 }
2561
2562 return n > 0 ? n : (k < 0 ? -errno : 0);
2563 }
2564
2565 p += k;
2566 nbytes -= k;
2567 n += k;
2568 }
2569
2570 return n;
2571}
2572
eb22ac37
LP
2573ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
2574 const uint8_t *p;
2575 ssize_t n = 0;
2576
2577 assert(fd >= 0);
2578 assert(buf);
2579
2580 p = buf;
2581
2582 while (nbytes > 0) {
2583 ssize_t k;
2584
2585 if ((k = write(fd, p, nbytes)) <= 0) {
2586
2587 if (k < 0 && errno == EINTR)
2588 continue;
2589
2590 if (k < 0 && errno == EAGAIN && do_poll) {
2591 struct pollfd pollfd;
2592
2593 zero(pollfd);
2594 pollfd.fd = fd;
2595 pollfd.events = POLLOUT;
2596
2597 if (poll(&pollfd, 1, -1) < 0) {
2598 if (errno == EINTR)
2599 continue;
2600
2601 return n > 0 ? n : -errno;
2602 }
2603
2604 if (pollfd.revents != POLLOUT)
2605 return n > 0 ? n : -EIO;
2606
2607 continue;
2608 }
2609
2610 return n > 0 ? n : (k < 0 ? -errno : 0);
2611 }
2612
2613 p += k;
2614 nbytes -= k;
2615 n += k;
2616 }
2617
2618 return n;
2619}
2620
8d567588
LP
2621int path_is_mount_point(const char *t) {
2622 struct stat a, b;
35d2e7ec
LP
2623 char *parent;
2624 int r;
8d567588
LP
2625
2626 if (lstat(t, &a) < 0) {
8d567588
LP
2627 if (errno == ENOENT)
2628 return 0;
2629
2630 return -errno;
2631 }
2632
35d2e7ec
LP
2633 if ((r = parent_of_path(t, &parent)) < 0)
2634 return r;
8d567588 2635
35d2e7ec
LP
2636 r = lstat(parent, &b);
2637 free(parent);
8d567588 2638
35d2e7ec
LP
2639 if (r < 0)
2640 return -errno;
8d567588
LP
2641
2642 return a.st_dev != b.st_dev;
2643}
2644
24a6e4a4
LP
2645int parse_usec(const char *t, usec_t *usec) {
2646 static const struct {
2647 const char *suffix;
2648 usec_t usec;
2649 } table[] = {
2650 { "sec", USEC_PER_SEC },
2651 { "s", USEC_PER_SEC },
2652 { "min", USEC_PER_MINUTE },
2653 { "hr", USEC_PER_HOUR },
2654 { "h", USEC_PER_HOUR },
2655 { "d", USEC_PER_DAY },
2656 { "w", USEC_PER_WEEK },
2657 { "msec", USEC_PER_MSEC },
2658 { "ms", USEC_PER_MSEC },
2659 { "m", USEC_PER_MINUTE },
2660 { "usec", 1ULL },
2661 { "us", 1ULL },
2662 { "", USEC_PER_SEC },
2663 };
2664
2665 const char *p;
2666 usec_t r = 0;
2667
2668 assert(t);
2669 assert(usec);
2670
2671 p = t;
2672 do {
2673 long long l;
2674 char *e;
2675 unsigned i;
2676
2677 errno = 0;
2678 l = strtoll(p, &e, 10);
2679
2680 if (errno != 0)
2681 return -errno;
2682
2683 if (l < 0)
2684 return -ERANGE;
2685
2686 if (e == p)
2687 return -EINVAL;
2688
2689 e += strspn(e, WHITESPACE);
2690
2691 for (i = 0; i < ELEMENTSOF(table); i++)
2692 if (startswith(e, table[i].suffix)) {
2693 r += (usec_t) l * table[i].usec;
2694 p = e + strlen(table[i].suffix);
2695 break;
2696 }
2697
2698 if (i >= ELEMENTSOF(table))
2699 return -EINVAL;
2700
2701 } while (*p != 0);
2702
2703 *usec = r;
2704
2705 return 0;
2706}
2707
843d2643
LP
2708int make_stdio(int fd) {
2709 int r, s, t;
2710
2711 assert(fd >= 0);
2712
2713 r = dup2(fd, STDIN_FILENO);
2714 s = dup2(fd, STDOUT_FILENO);
2715 t = dup2(fd, STDERR_FILENO);
2716
2717 if (fd >= 3)
2718 close_nointr_nofail(fd);
2719
2720 if (r < 0 || s < 0 || t < 0)
2721 return -errno;
2722
2723 return 0;
2724}
2725
ade509ce
LP
2726int make_null_stdio(void) {
2727 int null_fd;
2728
2729 if ((null_fd = open("/dev/null", O_RDWR|O_NOCTTY)) < 0)
2730 return -errno;
2731
2732 return make_stdio(null_fd);
2733}
2734
8407a5d0
LP
2735bool is_device_path(const char *path) {
2736
2737 /* Returns true on paths that refer to a device, either in
2738 * sysfs or in /dev */
2739
2740 return
2741 path_startswith(path, "/dev/") ||
2742 path_startswith(path, "/sys/");
2743}
2744
01f78473
LP
2745int dir_is_empty(const char *path) {
2746 DIR *d;
2747 int r;
2748 struct dirent buf, *de;
2749
2750 if (!(d = opendir(path)))
2751 return -errno;
2752
2753 for (;;) {
2754 if ((r = readdir_r(d, &buf, &de)) > 0) {
2755 r = -r;
2756 break;
2757 }
2758
2759 if (!de) {
2760 r = 1;
2761 break;
2762 }
2763
2764 if (!ignore_file(de->d_name)) {
2765 r = 0;
2766 break;
2767 }
2768 }
2769
2770 closedir(d);
2771 return r;
2772}
2773
d3782d60
LP
2774unsigned long long random_ull(void) {
2775 int fd;
2776 uint64_t ull;
2777 ssize_t r;
2778
2779 if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0)
2780 goto fallback;
2781
eb22ac37 2782 r = loop_read(fd, &ull, sizeof(ull), true);
d3782d60
LP
2783 close_nointr_nofail(fd);
2784
2785 if (r != sizeof(ull))
2786 goto fallback;
2787
2788 return ull;
2789
2790fallback:
2791 return random() * RAND_MAX + random();
2792}
2793
5b6319dc
LP
2794void rename_process(const char name[8]) {
2795 assert(name);
2796
2797 prctl(PR_SET_NAME, name);
2798
2799 /* This is a like a poor man's setproctitle(). The string
2800 * passed should fit in 7 chars (i.e. the length of
2801 * "systemd") */
2802
2803 if (program_invocation_name)
2804 strncpy(program_invocation_name, name, strlen(program_invocation_name));
2805}
2806
7d793605
LP
2807void sigset_add_many(sigset_t *ss, ...) {
2808 va_list ap;
2809 int sig;
2810
2811 assert(ss);
2812
2813 va_start(ap, ss);
2814 while ((sig = va_arg(ap, int)) > 0)
2815 assert_se(sigaddset(ss, sig) == 0);
2816 va_end(ap);
2817}
2818
ef2f1067
LP
2819char* gethostname_malloc(void) {
2820 struct utsname u;
2821
2822 assert_se(uname(&u) >= 0);
2823
2824 if (u.nodename[0])
2825 return strdup(u.nodename);
2826
2827 return strdup(u.sysname);
2828}
2829
2830char* getlogname_malloc(void) {
2831 uid_t uid;
2832 long bufsize;
2833 char *buf, *name;
2834 struct passwd pwbuf, *pw = NULL;
2835 struct stat st;
2836
2837 if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
2838 uid = st.st_uid;
2839 else
2840 uid = getuid();
2841
2842 /* Shortcut things to avoid NSS lookups */
2843 if (uid == 0)
2844 return strdup("root");
2845
2846 if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0)
2847 bufsize = 4096;
2848
2849 if (!(buf = malloc(bufsize)))
2850 return NULL;
2851
2852 if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) {
2853 name = strdup(pw->pw_name);
2854 free(buf);
2855 return name;
2856 }
2857
2858 free(buf);
2859
2860 if (asprintf(&name, "%lu", (unsigned long) uid) < 0)
2861 return NULL;
2862
2863 return name;
2864}
2865
fc116c6a
LP
2866int getttyname_malloc(int fd, char **r) {
2867 char path[PATH_MAX], *c;
618e02c7 2868 int k;
8c6db833
LP
2869
2870 assert(r);
ef2f1067 2871
fc116c6a 2872 if ((k = ttyname_r(fd, path, sizeof(path))) != 0)
618e02c7 2873 return -k;
ef2f1067
LP
2874
2875 char_array_0(path);
2876
fc116c6a 2877 if (!(c = strdup(startswith(path, "/dev/") ? path + 5 : path)))
8c6db833
LP
2878 return -ENOMEM;
2879
2880 *r = c;
2881 return 0;
2882}
2883
fc116c6a
LP
2884int getttyname_harder(int fd, char **r) {
2885 int k;
2886 char *s;
2887
2888 if ((k = getttyname_malloc(fd, &s)) < 0)
2889 return k;
2890
2891 if (streq(s, "tty")) {
2892 free(s);
46824d0e 2893 return get_ctty(r, NULL);
fc116c6a
LP
2894 }
2895
2896 *r = s;
2897 return 0;
2898}
2899
2900int get_ctty_devnr(dev_t *d) {
2901 int k;
20c03b7b 2902 char line[LINE_MAX], *p;
fc116c6a
LP
2903 unsigned long ttynr;
2904 FILE *f;
2905
2906 if (!(f = fopen("/proc/self/stat", "r")))
2907 return -errno;
2908
2909 if (!(fgets(line, sizeof(line), f))) {
2910 k = -errno;
2911 fclose(f);
2912 return k;
2913 }
2914
2915 fclose(f);
2916
2917 if (!(p = strrchr(line, ')')))
2918 return -EIO;
2919
2920 p++;
2921
2922 if (sscanf(p, " "
2923 "%*c " /* state */
2924 "%*d " /* ppid */
2925 "%*d " /* pgrp */
2926 "%*d " /* session */
2927 "%lu ", /* ttynr */
2928 &ttynr) != 1)
2929 return -EIO;
2930
2931 *d = (dev_t) ttynr;
2932 return 0;
2933}
2934
46824d0e 2935int get_ctty(char **r, dev_t *_devnr) {
fc116c6a 2936 int k;
20c03b7b 2937 char fn[PATH_MAX], *s, *b, *p;
fc116c6a
LP
2938 dev_t devnr;
2939
2940 assert(r);
2941
2942 if ((k = get_ctty_devnr(&devnr)) < 0)
2943 return k;
2944
2945 snprintf(fn, sizeof(fn), "/dev/char/%u:%u", major(devnr), minor(devnr));
2946 char_array_0(fn);
2947
2948 if ((k = readlink_malloc(fn, &s)) < 0) {
2949
2950 if (k != -ENOENT)
2951 return k;
2952
46824d0e
LP
2953 /* This is an ugly hack */
2954 if (major(devnr) == 136) {
2955 if (asprintf(&b, "pts/%lu", (unsigned long) minor(devnr)) < 0)
2956 return -ENOMEM;
2957
2958 *r = b;
2959 if (_devnr)
2960 *_devnr = devnr;
2961
2962 return 0;
2963 }
2964
fc116c6a
LP
2965 /* Probably something like the ptys which have no
2966 * symlink in /dev/char. Let's return something
2967 * vaguely useful. */
2968
2969 if (!(b = strdup(fn + 5)))
2970 return -ENOMEM;
2971
2972 *r = b;
46824d0e
LP
2973 if (_devnr)
2974 *_devnr = devnr;
2975
fc116c6a
LP
2976 return 0;
2977 }
2978
2979 if (startswith(s, "/dev/"))
2980 p = s + 5;
2981 else if (startswith(s, "../"))
2982 p = s + 3;
2983 else
2984 p = s;
2985
2986 b = strdup(p);
2987 free(s);
2988
2989 if (!b)
2990 return -ENOMEM;
2991
2992 *r = b;
46824d0e
LP
2993 if (_devnr)
2994 *_devnr = devnr;
2995
fc116c6a
LP
2996 return 0;
2997}
2998
8c6db833
LP
2999static int rm_rf_children(int fd, bool only_dirs) {
3000 DIR *d;
3001 int ret = 0;
3002
3003 assert(fd >= 0);
3004
3005 /* This returns the first error we run into, but nevertheless
3006 * tries to go on */
3007
3008 if (!(d = fdopendir(fd))) {
3009 close_nointr_nofail(fd);
4c633005
LP
3010
3011 return errno == ENOENT ? 0 : -errno;
8c6db833
LP
3012 }
3013
3014 for (;;) {
3015 struct dirent buf, *de;
3016 bool is_dir;
3017 int r;
3018
3019 if ((r = readdir_r(d, &buf, &de)) != 0) {
3020 if (ret == 0)
3021 ret = -r;
3022 break;
3023 }
3024
3025 if (!de)
3026 break;
3027
3028 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
3029 continue;
3030
3031 if (de->d_type == DT_UNKNOWN) {
3032 struct stat st;
3033
3034 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
4c633005 3035 if (ret == 0 && errno != ENOENT)
8c6db833
LP
3036 ret = -errno;
3037 continue;
3038 }
3039
3040 is_dir = S_ISDIR(st.st_mode);
3041 } else
3042 is_dir = de->d_type == DT_DIR;
3043
3044 if (is_dir) {
3045 int subdir_fd;
3046
3047 if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
4c633005 3048 if (ret == 0 && errno != ENOENT)
8c6db833
LP
3049 ret = -errno;
3050 continue;
3051 }
3052
3053 if ((r = rm_rf_children(subdir_fd, only_dirs)) < 0) {
3054 if (ret == 0)
3055 ret = r;
3056 }
3057
3058 if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
4c633005 3059 if (ret == 0 && errno != ENOENT)
8c6db833
LP
3060 ret = -errno;
3061 }
3062 } else if (!only_dirs) {
3063
3064 if (unlinkat(fd, de->d_name, 0) < 0) {
4c633005 3065 if (ret == 0 && errno != ENOENT)
8c6db833
LP
3066 ret = -errno;
3067 }
3068 }
3069 }
3070
3071 closedir(d);
3072
3073 return ret;
3074}
3075
3076int rm_rf(const char *path, bool only_dirs, bool delete_root) {
3077 int fd;
3078 int r;
3079
3080 assert(path);
3081
3082 if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
3083
3084 if (errno != ENOTDIR)
3085 return -errno;
3086
3087 if (delete_root && !only_dirs)
3088 if (unlink(path) < 0)
3089 return -errno;
3090
3091 return 0;
3092 }
3093
3094 r = rm_rf_children(fd, only_dirs);
3095
3096 if (delete_root)
3097 if (rmdir(path) < 0) {
3098 if (r == 0)
3099 r = -errno;
3100 }
3101
3102 return r;
3103}
3104
3105int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
3106 assert(path);
3107
3108 /* Under the assumption that we are running privileged we
3109 * first change the access mode and only then hand out
3110 * ownership to avoid a window where access is too open. */
3111
3112 if (chmod(path, mode) < 0)
3113 return -errno;
3114
3115 if (chown(path, uid, gid) < 0)
3116 return -errno;
3117
3118 return 0;
ef2f1067
LP
3119}
3120
82c121a4
LP
3121cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
3122 cpu_set_t *r;
3123 unsigned n = 1024;
3124
3125 /* Allocates the cpuset in the right size */
3126
3127 for (;;) {
3128 if (!(r = CPU_ALLOC(n)))
3129 return NULL;
3130
3131 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) {
3132 CPU_ZERO_S(CPU_ALLOC_SIZE(n), r);
3133
3134 if (ncpus)
3135 *ncpus = n;
3136
3137 return r;
3138 }
3139
3140 CPU_FREE(r);
3141
3142 if (errno != EINVAL)
3143 return NULL;
3144
3145 n *= 2;
3146 }
3147}
3148
9e58ff9c
LP
3149void status_vprintf(const char *format, va_list ap) {
3150 char *s = NULL;
3151 int fd = -1;
3152
3153 assert(format);
3154
3155 /* This independent of logging, as status messages are
3156 * optional and go exclusively to the console. */
3157
3158 if (vasprintf(&s, format, ap) < 0)
3159 goto finish;
3160
3161 if ((fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
3162 goto finish;
3163
3164 write(fd, s, strlen(s));
3165
3166finish:
3167 free(s);
3168
3169 if (fd >= 0)
3170 close_nointr_nofail(fd);
3171}
3172
c846ff47
LP
3173void status_printf(const char *format, ...) {
3174 va_list ap;
3175
3176 assert(format);
3177
3178 va_start(ap, format);
3179 status_vprintf(format, ap);
3180 va_end(ap);
3181}
3182
3183void status_welcome(void) {
10aa7034
LP
3184 char *pretty_name = NULL, *ansi_color = NULL;
3185 const char *const_pretty = NULL, *const_color = NULL;
3186 int r;
c846ff47 3187
10aa7034
LP
3188 if ((r = parse_env_file("/etc/os-release", NEWLINE,
3189 "PRETTY_NAME", &pretty_name,
3190 "ANSI_COLOR", &ansi_color,
3191 NULL)) < 0) {
c846ff47 3192
10aa7034
LP
3193 if (r != -ENOENT)
3194 log_warning("Failed to read /etc/os-release: %s", strerror(-r));
3195 }
c846ff47 3196
10aa7034
LP
3197#if defined(TARGET_FEDORA)
3198 if (!pretty_name) {
3199 if ((r = read_one_line_file("/etc/system-release", &pretty_name)) < 0) {
c846ff47 3200
10aa7034
LP
3201 if (r != -ENOENT)
3202 log_warning("Failed to read /etc/system-release: %s", strerror(-r));
6f9a471a 3203 }
10aa7034 3204 }
c846ff47 3205
10aa7034 3206 if (!ansi_color && pretty_name) {
c846ff47 3207
10aa7034
LP
3208 /* This tries to mimic the color magic the old Red Hat sysinit
3209 * script did. */
3210
3211 if (startswith(pretty_name, "Red Hat"))
3212 const_color = "0;31"; /* Red for RHEL */
3213 else if (startswith(pretty_name, "Fedora"))
3214 const_color = "0;34"; /* Blue for Fedora */
3215 }
c846ff47
LP
3216
3217#elif defined(TARGET_SUSE)
c846ff47 3218
10aa7034
LP
3219 if (!pretty_name) {
3220 if ((r = read_one_line_file("/etc/SuSE-release", &pretty_name)) < 0) {
c846ff47 3221
10aa7034
LP
3222 if (r != -ENOENT)
3223 log_warning("Failed to read /etc/SuSE-release: %s", strerror(-r));
6f9a471a 3224 }
10aa7034 3225 }
c846ff47 3226
10aa7034
LP
3227 if (!ansi_color)
3228 const_color = "0;32"; /* Green for openSUSE */
5a6225fd 3229
0d37b36b 3230#elif defined(TARGET_GENTOO)
0d37b36b 3231
10aa7034
LP
3232 if (!pretty_name) {
3233 if ((r = read_one_line_file("/etc/gentoo-release", &pretty_name)) < 0) {
0d37b36b 3234
10aa7034
LP
3235 if (r != -ENOENT)
3236 log_warning("Failed to read /etc/gentoo-release: %s", strerror(-r));
6f9a471a 3237 }
10aa7034 3238 }
0d37b36b 3239
10aa7034
LP
3240 if (!ansi_color)
3241 const_color = "1;34"; /* Light Blue for Gentoo */
5a6225fd 3242
a338bab5
AS
3243#elif defined(TARGET_ALTLINUX)
3244
3245 if (!pretty_name) {
3246 if ((r = read_one_line_file("/etc/altlinux-release", &pretty_name)) < 0) {
3247
3248 if (r != -ENOENT)
3249 log_warning("Failed to read /etc/altlinux-release: %s", strerror(-r));
6f9a471a 3250 }
a338bab5
AS
3251 }
3252
3253 if (!ansi_color)
3254 const_color = "0;36"; /* Cyan for ALTLinux */
3255
3256
5a6225fd 3257#elif defined(TARGET_DEBIAN)
5a6225fd 3258
10aa7034 3259 if (!pretty_name) {
22927a36 3260 char *version;
c8bffa43 3261
22927a36 3262 if ((r = read_one_line_file("/etc/debian_version", &version)) < 0) {
5a6225fd 3263
10aa7034
LP
3264 if (r != -ENOENT)
3265 log_warning("Failed to read /etc/debian_version: %s", strerror(-r));
22927a36 3266 } else {
22927a36
MB
3267 pretty_name = strappend("Debian ", version);
3268 free(version);
c8bffa43
LP
3269
3270 if (!pretty_name)
3271 log_warning("Failed to allocate Debian version string.");
22927a36 3272 }
10aa7034 3273 }
5a6225fd 3274
10aa7034
LP
3275 if (!ansi_color)
3276 const_color = "1;31"; /* Light Red for Debian */
5a6225fd 3277
274914f9 3278#elif defined(TARGET_UBUNTU)
10aa7034
LP
3279
3280 if ((r = parse_env_file("/etc/lsb-release", NEWLINE,
3281 "DISTRIB_DESCRIPTION", &pretty_name,
3282 NULL)) < 0) {
3283
3284 if (r != -ENOENT)
3285 log_warning("Failed to read /etc/lsb-release: %s", strerror(-r));
3286 }
3287
3288 if (!ansi_color)
3289 const_color = "0;33"; /* Orange/Brown for Ubuntu */
3290
1de4d79b
AB
3291#elif defined(TARGET_MANDRIVA)
3292
3293 if (!pretty_name) {
3294 char *s, *p;
3295
3296 if ((r = read_one_line_file("/etc/mandriva-release", &s) < 0)) {
3297 if (r != -ENOENT)
3298 log_warning("Failed to read /etc/mandriva-release: %s", strerror(-r));
3299 } else {
3300 p = strstr(s, " release ");
3301 if (p) {
3302 *p = '\0';
3303 p += 9;
3304 p[strcspn(p, " ")] = '\0';
3305
3306 /* This corresponds to standard rc.sysinit */
3307 if (asprintf(&pretty_name, "%s\x1B[0;39m %s", s, p) > 0)
3308 const_color = "1;36";
3309 else
3310 log_warning("Failed to allocate Mandriva version string.");
3311 } else
3312 log_warning("Failed to parse /etc/mandriva-release");
3313 free(s);
3314 }
3315 }
3316
c846ff47 3317#endif
10aa7034
LP
3318
3319 if (!pretty_name && !const_pretty)
3320 const_pretty = "Linux";
3321
3322 if (!ansi_color && !const_color)
3323 const_color = "1";
3324
da71f23c 3325 status_printf("\nWelcome to \x1B[%sm%s\x1B[0m!\n\n",
10aa7034
LP
3326 const_color ? const_color : ansi_color,
3327 const_pretty ? const_pretty : pretty_name);
86a3475b
LP
3328
3329 free(ansi_color);
3330 free(pretty_name);
c846ff47
LP
3331}
3332
fab56fc5
LP
3333char *replace_env(const char *format, char **env) {
3334 enum {
3335 WORD,
c24eb49e 3336 CURLY,
fab56fc5
LP
3337 VARIABLE
3338 } state = WORD;
3339
3340 const char *e, *word = format;
3341 char *r = NULL, *k;
3342
3343 assert(format);
3344
3345 for (e = format; *e; e ++) {
3346
3347 switch (state) {
3348
3349 case WORD:
3350 if (*e == '$')
c24eb49e 3351 state = CURLY;
fab56fc5
LP
3352 break;
3353
c24eb49e
LP
3354 case CURLY:
3355 if (*e == '{') {
fab56fc5
LP
3356 if (!(k = strnappend(r, word, e-word-1)))
3357 goto fail;
3358
3359 free(r);
3360 r = k;
3361
3362 word = e-1;
3363 state = VARIABLE;
3364
3365 } else if (*e == '$') {
3366 if (!(k = strnappend(r, word, e-word)))
3367 goto fail;
3368
3369 free(r);
3370 r = k;
3371
3372 word = e+1;
3373 state = WORD;
3374 } else
3375 state = WORD;
3376 break;
3377
3378 case VARIABLE:
c24eb49e 3379 if (*e == '}') {
b95cf362 3380 const char *t;
fab56fc5 3381
b95cf362
LP
3382 if (!(t = strv_env_get_with_length(env, word+2, e-word-2)))
3383 t = "";
fab56fc5 3384
b95cf362
LP
3385 if (!(k = strappend(r, t)))
3386 goto fail;
fab56fc5 3387
b95cf362
LP
3388 free(r);
3389 r = k;
fab56fc5 3390
b95cf362 3391 word = e+1;
fab56fc5
LP
3392 state = WORD;
3393 }
3394 break;
3395 }
3396 }
3397
3398 if (!(k = strnappend(r, word, e-word)))
3399 goto fail;
3400
3401 free(r);
3402 return k;
3403
3404fail:
3405 free(r);
3406 return NULL;
3407}
3408
3409char **replace_env_argv(char **argv, char **env) {
3410 char **r, **i;
c24eb49e
LP
3411 unsigned k = 0, l = 0;
3412
3413 l = strv_length(argv);
fab56fc5 3414
c24eb49e 3415 if (!(r = new(char*, l+1)))
fab56fc5
LP
3416 return NULL;
3417
3418 STRV_FOREACH(i, argv) {
c24eb49e
LP
3419
3420 /* If $FOO appears as single word, replace it by the split up variable */
b95cf362
LP
3421 if ((*i)[0] == '$' && (*i)[1] != '{') {
3422 char *e;
3423 char **w, **m;
3424 unsigned q;
c24eb49e 3425
b95cf362 3426 if ((e = strv_env_get(env, *i+1))) {
c24eb49e
LP
3427
3428 if (!(m = strv_split_quoted(e))) {
3429 r[k] = NULL;
3430 strv_free(r);
3431 return NULL;
3432 }
b95cf362
LP
3433 } else
3434 m = NULL;
c24eb49e 3435
b95cf362
LP
3436 q = strv_length(m);
3437 l = l + q - 1;
c24eb49e 3438
b95cf362
LP
3439 if (!(w = realloc(r, sizeof(char*) * (l+1)))) {
3440 r[k] = NULL;
3441 strv_free(r);
3442 strv_free(m);
3443 return NULL;
3444 }
c24eb49e 3445
b95cf362
LP
3446 r = w;
3447 if (m) {
c24eb49e
LP
3448 memcpy(r + k, m, q * sizeof(char*));
3449 free(m);
c24eb49e 3450 }
b95cf362
LP
3451
3452 k += q;
3453 continue;
c24eb49e
LP
3454 }
3455
3456 /* If ${FOO} appears as part of a word, replace it by the variable as-is */
fab56fc5
LP
3457 if (!(r[k++] = replace_env(*i, env))) {
3458 strv_free(r);
3459 return NULL;
3460 }
3461 }
3462
3463 r[k] = NULL;
3464 return r;
3465}
3466
fa776d8e
LP
3467int columns(void) {
3468 static __thread int parsed_columns = 0;
3469 const char *e;
3470
3471 if (parsed_columns > 0)
3472 return parsed_columns;
3473
3474 if ((e = getenv("COLUMNS")))
3475 parsed_columns = atoi(e);
3476
3477 if (parsed_columns <= 0) {
3478 struct winsize ws;
3479 zero(ws);
3480
9ed95f43 3481 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
fa776d8e
LP
3482 parsed_columns = ws.ws_col;
3483 }
3484
3485 if (parsed_columns <= 0)
3486 parsed_columns = 80;
3487
3488 return parsed_columns;
3489}
3490
b4f10a5e
LP
3491int running_in_chroot(void) {
3492 struct stat a, b;
3493
3494 zero(a);
3495 zero(b);
3496
3497 /* Only works as root */
3498
3499 if (stat("/proc/1/root", &a) < 0)
3500 return -errno;
3501
3502 if (stat("/", &b) < 0)
3503 return -errno;
3504
3505 return
3506 a.st_dev != b.st_dev ||
3507 a.st_ino != b.st_ino;
3508}
3509
8fe914ec
LP
3510char *ellipsize(const char *s, unsigned length, unsigned percent) {
3511 size_t l, x;
3512 char *r;
3513
3514 assert(s);
3515 assert(percent <= 100);
3516 assert(length >= 3);
3517
3518 l = strlen(s);
3519
3520 if (l <= 3 || l <= length)
3521 return strdup(s);
3522
3523 if (!(r = new0(char, length+1)))
3524 return r;
3525
3526 x = (length * percent) / 100;
3527
3528 if (x > length - 3)
3529 x = length - 3;
3530
3531 memcpy(r, s, x);
3532 r[x] = '.';
3533 r[x+1] = '.';
3534 r[x+2] = '.';
3535 memcpy(r + x + 3,
3536 s + l - (length - x - 3),
3537 length - x - 3);
3538
3539 return r;
3540}
3541
f6144808
LP
3542int touch(const char *path) {
3543 int fd;
3544
3545 assert(path);
3546
14f3c825 3547 if ((fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644)) < 0)
f6144808
LP
3548 return -errno;
3549
3550 close_nointr_nofail(fd);
3551 return 0;
3552}
afea26ad 3553
97c4a07d 3554char *unquote(const char *s, const char* quotes) {
11ce3427
LP
3555 size_t l;
3556 assert(s);
3557
3558 if ((l = strlen(s)) < 2)
3559 return strdup(s);
3560
97c4a07d 3561 if (strchr(quotes, s[0]) && s[l-1] == s[0])
11ce3427
LP
3562 return strndup(s+1, l-2);
3563
3564 return strdup(s);
3565}
3566
5f7c426e
LP
3567char *normalize_env_assignment(const char *s) {
3568 char *name, *value, *p, *r;
3569
3570 p = strchr(s, '=');
3571
3572 if (!p) {
3573 if (!(r = strdup(s)))
3574 return NULL;
3575
3576 return strstrip(r);
3577 }
3578
3579 if (!(name = strndup(s, p - s)))
3580 return NULL;
3581
3582 if (!(p = strdup(p+1))) {
3583 free(name);
3584 return NULL;
3585 }
3586
3587 value = unquote(strstrip(p), QUOTES);
3588 free(p);
3589
3590 if (!value) {
5f7c426e
LP
3591 free(name);
3592 return NULL;
3593 }
3594
3595 if (asprintf(&r, "%s=%s", name, value) < 0)
3596 r = NULL;
3597
3598 free(value);
3599 free(name);
3600
3601 return r;
3602}
3603
8e12a6ae 3604int wait_for_terminate(pid_t pid, siginfo_t *status) {
2e78aa99
LP
3605 assert(pid >= 1);
3606 assert(status);
3607
3608 for (;;) {
8e12a6ae
LP
3609 zero(*status);
3610
3611 if (waitid(P_PID, pid, status, WEXITED) < 0) {
2e78aa99
LP
3612
3613 if (errno == EINTR)
3614 continue;
3615
3616 return -errno;
3617 }
3618
3619 return 0;
3620 }
3621}
3622
97c4a07d
LP
3623int wait_for_terminate_and_warn(const char *name, pid_t pid) {
3624 int r;
3625 siginfo_t status;
3626
3627 assert(name);
3628 assert(pid > 1);
3629
3630 if ((r = wait_for_terminate(pid, &status)) < 0) {
3631 log_warning("Failed to wait for %s: %s", name, strerror(-r));
3632 return r;
3633 }
3634
3635 if (status.si_code == CLD_EXITED) {
3636 if (status.si_status != 0) {
3637 log_warning("%s failed with error code %i.", name, status.si_status);
0a27cf3f 3638 return status.si_status;
97c4a07d
LP
3639 }
3640
3641 log_debug("%s succeeded.", name);
3642 return 0;
3643
3644 } else if (status.si_code == CLD_KILLED ||
3645 status.si_code == CLD_DUMPED) {
3646
3647 log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
3648 return -EPROTO;
3649 }
3650
3651 log_warning("%s failed due to unknown reason.", name);
3652 return -EPROTO;
3653
3654}
3655
3c14d26c 3656void freeze(void) {
720ce21d
LP
3657
3658 /* Make sure nobody waits for us on a socket anymore */
3659 close_all_fds(NULL, 0);
3660
c29597a1
LP
3661 sync();
3662
3c14d26c
LP
3663 for (;;)
3664 pause();
3665}
3666
00dc5d76
LP
3667bool null_or_empty(struct stat *st) {
3668 assert(st);
3669
3670 if (S_ISREG(st->st_mode) && st->st_size <= 0)
3671 return true;
3672
c8f26f42 3673 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
00dc5d76
LP
3674 return true;
3675
3676 return false;
3677}
3678
a247755d 3679DIR *xopendirat(int fd, const char *name, int flags) {
c4731d11
LP
3680 int nfd;
3681 DIR *d;
3682
3683 if ((nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags)) < 0)
3684 return NULL;
3685
3686 if (!(d = fdopendir(nfd))) {
3687 close_nointr_nofail(nfd);
3688 return NULL;
3689 }
3690
3691 return d;
3b63d2d3
LP
3692}
3693
8a0867d6
LP
3694int signal_from_string_try_harder(const char *s) {
3695 int signo;
3696 assert(s);
3697
3698 if ((signo = signal_from_string(s)) <= 0)
3699 if (startswith(s, "SIG"))
3700 return signal_from_string(s+3);
3701
3702 return signo;
3703}
3704
10717a1a
LP
3705void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
3706
3707 assert(f);
3708 assert(name);
3709 assert(t);
3710
3711 if (!dual_timestamp_is_set(t))
3712 return;
3713
3714 fprintf(f, "%s=%llu %llu\n",
3715 name,
3716 (unsigned long long) t->realtime,
3717 (unsigned long long) t->monotonic);
3718}
3719
799fd0fd 3720void dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
10717a1a
LP
3721 unsigned long long a, b;
3722
10717a1a
LP
3723 assert(value);
3724 assert(t);
3725
3726 if (sscanf(value, "%lli %llu", &a, &b) != 2)
3727 log_debug("Failed to parse finish timestamp value %s", value);
3728 else {
3729 t->realtime = a;
3730 t->monotonic = b;
3731 }
3732}
3733
e23a0ce8
LP
3734char *fstab_node_to_udev_node(const char *p) {
3735 char *dn, *t, *u;
3736 int r;
3737
3738 /* FIXME: to follow udev's logic 100% we need to leave valid
3739 * UTF8 chars unescaped */
3740
3741 if (startswith(p, "LABEL=")) {
3742
3743 if (!(u = unquote(p+6, "\"\'")))
3744 return NULL;
3745
3746 t = xescape(u, "/ ");
3747 free(u);
3748
3749 if (!t)
3750 return NULL;
3751
3752 r = asprintf(&dn, "/dev/disk/by-label/%s", t);
3753 free(t);
3754
3755 if (r < 0)
3756 return NULL;
3757
3758 return dn;
3759 }
3760
3761 if (startswith(p, "UUID=")) {
3762
3763 if (!(u = unquote(p+5, "\"\'")))
3764 return NULL;
3765
3766 t = xescape(u, "/ ");
3767 free(u);
3768
3769 if (!t)
3770 return NULL;
3771
0058d7b9 3772 r = asprintf(&dn, "/dev/disk/by-uuid/%s", t);
e23a0ce8
LP
3773 free(t);
3774
3775 if (r < 0)
3776 return NULL;
3777
3778 return dn;
3779 }
3780
3781 return strdup(p);
3782}
3783
e9ddabc2
LP
3784void filter_environ(const char *prefix) {
3785 int i, j;
3786 assert(prefix);
3787
3788 if (!environ)
3789 return;
3790
3791 for (i = 0, j = 0; environ[i]; i++) {
3792
3793 if (startswith(environ[i], prefix))
3794 continue;
3795
3796 environ[j++] = environ[i];
3797 }
3798
3799 environ[j] = NULL;
3800}
3801
f212ac12
LP
3802bool tty_is_vc(const char *tty) {
3803 assert(tty);
3804
3805 if (startswith(tty, "/dev/"))
3806 tty += 5;
3807
3808 return startswith(tty, "tty") &&
3809 tty[3] >= '0' && tty[3] <= '9';
3810}
3811
e3aa71c3 3812const char *default_term_for_tty(const char *tty) {
3030ccd7
LP
3813 char *active = NULL;
3814 const char *term;
3815
e3aa71c3
LP
3816 assert(tty);
3817
3818 if (startswith(tty, "/dev/"))
3819 tty += 5;
3820
3030ccd7
LP
3821 /* Resolve where /dev/console is pointing when determining
3822 * TERM */
3823 if (streq(tty, "console"))
3824 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
079a09fb
LP
3825 /* If multiple log outputs are configured the
3826 * last one is what /dev/console points to */
3827 if ((tty = strrchr(active, ' ')))
3828 tty++;
3829 else
3830 tty = active;
3030ccd7
LP
3831 }
3832
f212ac12 3833 term = tty_is_vc(tty) ? "TERM=linux" : "TERM=vt100";
3030ccd7 3834 free(active);
e3aa71c3 3835
3030ccd7 3836 return term;
e3aa71c3
LP
3837}
3838
07faed4f
LP
3839/* Returns a short identifier for the various VM implementations */
3840int detect_vm(const char **id) {
46a08e38
LP
3841
3842#if defined(__i386__) || defined(__x86_64__)
3843
3844 /* Both CPUID and DMI are x86 specific interfaces... */
3845
721bca57 3846 static const char *const dmi_vendors[] = {
46a08e38
LP
3847 "/sys/class/dmi/id/sys_vendor",
3848 "/sys/class/dmi/id/board_vendor",
3849 "/sys/class/dmi/id/bios_vendor"
3850 };
3851
4e08da90 3852 static const char dmi_vendor_table[] =
07faed4f
LP
3853 "QEMU\0" "qemu\0"
3854 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
3855 "VMware\0" "vmware\0"
3856 "VMW\0" "vmware\0"
3857 "Microsoft Corporation\0" "microsoft\0"
3858 "innotek GmbH\0" "oracle\0"
3859 "Xen\0" "xen\0"
34df5a34 3860 "Bochs\0" "bochs\0";
07faed4f 3861
4e08da90 3862 static const char cpuid_vendor_table[] =
07faed4f
LP
3863 "XenVMMXenVMM\0" "xen\0"
3864 "KVMKVMKVM\0" "kvm\0"
3865 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
3866 "VMwareVMware\0" "vmware\0"
3867 /* http://msdn.microsoft.com/en-us/library/ff542428.aspx */
34df5a34 3868 "Microsoft Hv\0" "microsoft\0";
07faed4f
LP
3869
3870 uint32_t eax, ecx;
46a08e38
LP
3871 union {
3872 uint32_t sig32[3];
3873 char text[13];
3874 } sig;
46a08e38 3875 unsigned i;
07faed4f 3876 const char *j, *k;
721bca57 3877 bool hypervisor;
46a08e38
LP
3878
3879 /* http://lwn.net/Articles/301888/ */
3880 zero(sig);
3881
46a08e38
LP
3882#if defined (__i386__)
3883#define REG_a "eax"
3884#define REG_b "ebx"
3885#elif defined (__amd64__)
3886#define REG_a "rax"
3887#define REG_b "rbx"
3888#endif
3889
07faed4f
LP
3890 /* First detect whether there is a hypervisor */
3891 eax = 1;
46a08e38
LP
3892 __asm__ __volatile__ (
3893 /* ebx/rbx is being used for PIC! */
3894 " push %%"REG_b" \n\t"
3895 " cpuid \n\t"
46a08e38
LP
3896 " pop %%"REG_b" \n\t"
3897
07faed4f 3898 : "=a" (eax), "=c" (ecx)
46a08e38
LP
3899 : "0" (eax)
3900 );
3901
721bca57
LP
3902 hypervisor = !!(ecx & ecx & 0x80000000U);
3903
3904 if (hypervisor) {
07faed4f
LP
3905
3906 /* There is a hypervisor, see what it is */
3907 eax = 0x40000000U;
3908 __asm__ __volatile__ (
3909 /* ebx/rbx is being used for PIC! */
3910 " push %%"REG_b" \n\t"
3911 " cpuid \n\t"
3912 " mov %%ebx, %1 \n\t"
3913 " pop %%"REG_b" \n\t"
3914
3915 : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2])
3916 : "0" (eax)
3917 );
3918
3919 NULSTR_FOREACH_PAIR(j, k, cpuid_vendor_table)
3920 if (streq(sig.text, j)) {
3921
3922 if (id)
3923 *id = k;
3924
3925 return 1;
3926 }
721bca57 3927 }
07faed4f 3928
721bca57
LP
3929 for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
3930 char *s;
3931 int r;
3932 const char *found = NULL;
3933
3934 if ((r = read_one_line_file(dmi_vendors[i], &s)) < 0) {
3935 if (r != -ENOENT)
3936 return r;
3937
3938 continue;
3939 }
3940
3941 NULSTR_FOREACH_PAIR(j, k, dmi_vendor_table)
3942 if (startswith(s, j))
3943 found = k;
3944 free(s);
3945
3946 if (found) {
3947 if (id)
3948 *id = found;
3949
3950 return 1;
3951 }
3952 }
3953
3954 if (hypervisor) {
07faed4f
LP
3955 if (id)
3956 *id = "other";
3957
3958 return 1;
3959 }
46a08e38 3960
721bca57 3961#endif
07faed4f
LP
3962 return 0;
3963}
3964
ef2df9f4 3965int detect_container(const char **id) {
f9b9232b
LP
3966 FILE *f;
3967
ef2df9f4
LP
3968 /* Unfortunately many of these operations require root access
3969 * in one way or another */
f9b9232b 3970
ef2df9f4
LP
3971 if (geteuid() != 0)
3972 return -EPERM;
3973
3974 if (running_in_chroot() > 0) {
f9b9232b
LP
3975
3976 if (id)
ef2df9f4 3977 *id = "chroot";
f9b9232b
LP
3978
3979 return 1;
3980 }
07faed4f 3981
ef2df9f4
LP
3982 /* /proc/vz exists in container and outside of the container,
3983 * /proc/bc only outside of the container. */
3984 if (access("/proc/vz", F_OK) >= 0 &&
3985 access("/proc/bc", F_OK) < 0) {
07faed4f 3986
ef2df9f4
LP
3987 if (id)
3988 *id = "openvz";
3989
3990 return 1;
f9b9232b 3991 }
07faed4f 3992
f9b9232b
LP
3993 if ((f = fopen("/proc/self/cgroup", "r"))) {
3994
3995 for (;;) {
3996 char line[LINE_MAX], *p;
3997
3998 if (!fgets(line, sizeof(line), f))
3999 break;
4000
4001 if (!(p = strchr(strstrip(line), ':')))
4002 continue;
4003
4004 if (strncmp(p, ":ns:", 4))
4005 continue;
4006
4007 if (!streq(p, ":ns:/")) {
4008 fclose(f);
4009
ef2df9f4 4010 if (id)
28cf382a 4011 *id = "pidns";
ef2df9f4
LP
4012
4013 return 1;
f9b9232b
LP
4014 }
4015 }
4016
4017 fclose(f);
07faed4f
LP
4018 }
4019
ef2df9f4
LP
4020 return 0;
4021}
4022
4023/* Returns a short identifier for the various VM/container implementations */
4024int detect_virtualization(const char **id) {
4025 static __thread const char *cached_id = NULL;
4026 const char *_id;
4027 int r;
4028
4029 if (cached_id) {
4030
4031 if (cached_id == (const char*) -1)
4032 return 0;
4033
4034 if (id)
4035 *id = cached_id;
4036
4037 return 1;
f9b9232b 4038 }
07faed4f 4039
ef2df9f4
LP
4040 if ((r = detect_container(&_id)) != 0)
4041 goto finish;
4042
f9b9232b 4043 r = detect_vm(&_id);
07faed4f 4044
f9b9232b 4045finish:
ef2df9f4 4046 if (r > 0) {
f9b9232b 4047 cached_id = _id;
07faed4f 4048
ef2df9f4
LP
4049 if (id)
4050 *id = _id;
4051 } else if (r == 0)
4052 cached_id = (const char*) -1;
f9b9232b
LP
4053
4054 return r;
46a08e38
LP
4055}
4056
83cc030f
LP
4057void execute_directory(const char *directory, DIR *d, char *argv[]) {
4058 DIR *_d = NULL;
4059 struct dirent *de;
4060 Hashmap *pids = NULL;
4061
4062 assert(directory);
4063
4064 /* Executes all binaries in a directory in parallel and waits
4065 * until all they all finished. */
4066
4067 if (!d) {
4068 if (!(_d = opendir(directory))) {
4069
4070 if (errno == ENOENT)
4071 return;
4072
4073 log_error("Failed to enumerate directory %s: %m", directory);
4074 return;
4075 }
4076
4077 d = _d;
4078 }
4079
4080 if (!(pids = hashmap_new(trivial_hash_func, trivial_compare_func))) {
4081 log_error("Failed to allocate set.");
4082 goto finish;
4083 }
4084
4085 while ((de = readdir(d))) {
4086 char *path;
4087 pid_t pid;
4088 int k;
4089
4090 if (ignore_file(de->d_name))
4091 continue;
4092
4093 if (de->d_type != DT_REG &&
4094 de->d_type != DT_LNK &&
4095 de->d_type != DT_UNKNOWN)
4096 continue;
4097
4098 if (asprintf(&path, "%s/%s", directory, de->d_name) < 0) {
4099 log_error("Out of memory");
4100 continue;
4101 }
4102
4103 if ((pid = fork()) < 0) {
4104 log_error("Failed to fork: %m");
4105 free(path);
4106 continue;
4107 }
4108
4109 if (pid == 0) {
4110 char *_argv[2];
4111 /* Child */
4112
4113 if (!argv) {
4114 _argv[0] = path;
4115 _argv[1] = NULL;
4116 argv = _argv;
4117 } else
4118 if (!argv[0])
4119 argv[0] = path;
4120
4121 execv(path, argv);
4122
4123 log_error("Failed to execute %s: %m", path);
4124 _exit(EXIT_FAILURE);
4125 }
4126
4127 log_debug("Spawned %s as %lu", path, (unsigned long) pid);
4128
4129 if ((k = hashmap_put(pids, UINT_TO_PTR(pid), path)) < 0) {
4130 log_error("Failed to add PID to set: %s", strerror(-k));
4131 free(path);
4132 }
4133 }
4134
4135 while (!hashmap_isempty(pids)) {
4136 siginfo_t si;
4137 char *path;
4138
4139 zero(si);
4140 if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
4141
4142 if (errno == EINTR)
4143 continue;
4144
4145 log_error("waitid() failed: %m");
4146 goto finish;
4147 }
4148
4149 if ((path = hashmap_remove(pids, UINT_TO_PTR(si.si_pid)))) {
4150 if (!is_clean_exit(si.si_code, si.si_status)) {
4151 if (si.si_code == CLD_EXITED)
4152 log_error("%s exited with exit status %i.", path, si.si_status);
4153 else
4154 log_error("%s terminated by signal %s.", path, signal_to_string(si.si_status));
4155 } else
4156 log_debug("%s exited successfully.", path);
4157
4158 free(path);
4159 }
4160 }
4161
4162finish:
4163 if (_d)
4164 closedir(_d);
4165
4166 if (pids)
4167 hashmap_free_free(pids);
4168}
4169
430c18ed
LP
4170int kill_and_sigcont(pid_t pid, int sig) {
4171 int r;
4172
4173 r = kill(pid, sig) < 0 ? -errno : 0;
4174
4175 if (r >= 0)
4176 kill(pid, SIGCONT);
4177
4178 return r;
4179}
4180
05feefe0
LP
4181bool nulstr_contains(const char*nulstr, const char *needle) {
4182 const char *i;
4183
4184 if (!nulstr)
4185 return false;
4186
4187 NULSTR_FOREACH(i, nulstr)
4188 if (streq(i, needle))
4189 return true;
4190
4191 return false;
4192}
4193
6faa1114 4194bool plymouth_running(void) {
9408a2d2 4195 return access("/run/plymouth/pid", F_OK) >= 0;
6faa1114
LP
4196}
4197
7c3b203c
LP
4198void parse_syslog_priority(char **p, int *priority) {
4199 int a = 0, b = 0, c = 0;
4200 int k;
4201
4202 assert(p);
4203 assert(*p);
4204 assert(priority);
4205
4206 if ((*p)[0] != '<')
4207 return;
4208
4209 if (!strchr(*p, '>'))
4210 return;
4211
4212 if ((*p)[2] == '>') {
4213 c = undecchar((*p)[1]);
4214 k = 3;
4215 } else if ((*p)[3] == '>') {
4216 b = undecchar((*p)[1]);
4217 c = undecchar((*p)[2]);
4218 k = 4;
4219 } else if ((*p)[4] == '>') {
4220 a = undecchar((*p)[1]);
4221 b = undecchar((*p)[2]);
4222 c = undecchar((*p)[3]);
4223 k = 5;
4224 } else
4225 return;
4226
4227 if (a < 0 || b < 0 || c < 0)
4228 return;
4229
4230 *priority = a*100+b*10+c;
4231 *p += k;
4232}
4233
ac123445
LP
4234int have_effective_cap(int value) {
4235 cap_t cap;
4236 cap_flag_value_t fv;
4237 int r;
4238
4239 if (!(cap = cap_get_proc()))
4240 return -errno;
4241
4242 if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0)
4243 r = -errno;
4244 else
4245 r = fv == CAP_SET;
4246
4247 cap_free(cap);
4248 return r;
4249}
4250
9beb3f4d
LP
4251char* strshorten(char *s, size_t l) {
4252 assert(s);
4253
4254 if (l < strlen(s))
4255 s[l] = 0;
4256
4257 return s;
4258}
4259
4260static bool hostname_valid_char(char c) {
4261 return
4262 (c >= 'a' && c <= 'z') ||
4263 (c >= 'A' && c <= 'Z') ||
4264 (c >= '0' && c <= '9') ||
4265 c == '-' ||
4266 c == '_' ||
4267 c == '.';
4268}
4269
4270bool hostname_is_valid(const char *s) {
4271 const char *p;
4272
4273 if (isempty(s))
4274 return false;
4275
4276 for (p = s; *p; p++)
4277 if (!hostname_valid_char(*p))
4278 return false;
4279
4280 if (p-s > HOST_NAME_MAX)
4281 return false;
4282
4283 return true;
4284}
4285
4286char* hostname_cleanup(char *s) {
4287 char *p, *d;
4288
4289 for (p = s, d = s; *p; p++)
4290 if ((*p >= 'a' && *p <= 'z') ||
4291 (*p >= 'A' && *p <= 'Z') ||
4292 (*p >= '0' && *p <= '9') ||
4293 *p == '-' ||
4294 *p == '_' ||
4295 *p == '.')
4296 *(d++) = *p;
4297
4298 *d = 0;
4299
4300 strshorten(s, HOST_NAME_MAX);
4301 return s;
4302}
4303
1dccbe19
LP
4304static const char *const ioprio_class_table[] = {
4305 [IOPRIO_CLASS_NONE] = "none",
4306 [IOPRIO_CLASS_RT] = "realtime",
4307 [IOPRIO_CLASS_BE] = "best-effort",
4308 [IOPRIO_CLASS_IDLE] = "idle"
4309};
4310
4311DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
4312
4313static const char *const sigchld_code_table[] = {
4314 [CLD_EXITED] = "exited",
4315 [CLD_KILLED] = "killed",
4316 [CLD_DUMPED] = "dumped",
4317 [CLD_TRAPPED] = "trapped",
4318 [CLD_STOPPED] = "stopped",
4319 [CLD_CONTINUED] = "continued",
4320};
4321
4322DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
4323
7d76f312 4324static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
1dccbe19
LP
4325 [LOG_FAC(LOG_KERN)] = "kern",
4326 [LOG_FAC(LOG_USER)] = "user",
4327 [LOG_FAC(LOG_MAIL)] = "mail",
4328 [LOG_FAC(LOG_DAEMON)] = "daemon",
4329 [LOG_FAC(LOG_AUTH)] = "auth",
4330 [LOG_FAC(LOG_SYSLOG)] = "syslog",
4331 [LOG_FAC(LOG_LPR)] = "lpr",
4332 [LOG_FAC(LOG_NEWS)] = "news",
4333 [LOG_FAC(LOG_UUCP)] = "uucp",
4334 [LOG_FAC(LOG_CRON)] = "cron",
4335 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
4336 [LOG_FAC(LOG_FTP)] = "ftp",
4337 [LOG_FAC(LOG_LOCAL0)] = "local0",
4338 [LOG_FAC(LOG_LOCAL1)] = "local1",
4339 [LOG_FAC(LOG_LOCAL2)] = "local2",
4340 [LOG_FAC(LOG_LOCAL3)] = "local3",
4341 [LOG_FAC(LOG_LOCAL4)] = "local4",
4342 [LOG_FAC(LOG_LOCAL5)] = "local5",
4343 [LOG_FAC(LOG_LOCAL6)] = "local6",
4344 [LOG_FAC(LOG_LOCAL7)] = "local7"
4345};
4346
7d76f312 4347DEFINE_STRING_TABLE_LOOKUP(log_facility_unshifted, int);
1dccbe19
LP
4348
4349static const char *const log_level_table[] = {
4350 [LOG_EMERG] = "emerg",
4351 [LOG_ALERT] = "alert",
4352 [LOG_CRIT] = "crit",
4353 [LOG_ERR] = "err",
4354 [LOG_WARNING] = "warning",
4355 [LOG_NOTICE] = "notice",
4356 [LOG_INFO] = "info",
4357 [LOG_DEBUG] = "debug"
4358};
4359
4360DEFINE_STRING_TABLE_LOOKUP(log_level, int);
4361
4362static const char* const sched_policy_table[] = {
4363 [SCHED_OTHER] = "other",
4364 [SCHED_BATCH] = "batch",
4365 [SCHED_IDLE] = "idle",
4366 [SCHED_FIFO] = "fifo",
4367 [SCHED_RR] = "rr"
4368};
4369
4370DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
4371
4372static const char* const rlimit_table[] = {
4373 [RLIMIT_CPU] = "LimitCPU",
4374 [RLIMIT_FSIZE] = "LimitFSIZE",
4375 [RLIMIT_DATA] = "LimitDATA",
4376 [RLIMIT_STACK] = "LimitSTACK",
4377 [RLIMIT_CORE] = "LimitCORE",
4378 [RLIMIT_RSS] = "LimitRSS",
4379 [RLIMIT_NOFILE] = "LimitNOFILE",
4380 [RLIMIT_AS] = "LimitAS",
4381 [RLIMIT_NPROC] = "LimitNPROC",
4382 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
4383 [RLIMIT_LOCKS] = "LimitLOCKS",
4384 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
4385 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
4386 [RLIMIT_NICE] = "LimitNICE",
4387 [RLIMIT_RTPRIO] = "LimitRTPRIO",
4388 [RLIMIT_RTTIME] = "LimitRTTIME"
4389};
4390
4391DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
4fd5948e
LP
4392
4393static const char* const ip_tos_table[] = {
4394 [IPTOS_LOWDELAY] = "low-delay",
4395 [IPTOS_THROUGHPUT] = "throughput",
4396 [IPTOS_RELIABILITY] = "reliability",
4397 [IPTOS_LOWCOST] = "low-cost",
4398};
4399
4400DEFINE_STRING_TABLE_LOOKUP(ip_tos, int);
2e22afe9
LP
4401
4402static const char *const signal_table[] = {
4403 [SIGHUP] = "HUP",
4404 [SIGINT] = "INT",
4405 [SIGQUIT] = "QUIT",
4406 [SIGILL] = "ILL",
4407 [SIGTRAP] = "TRAP",
4408 [SIGABRT] = "ABRT",
4409 [SIGBUS] = "BUS",
4410 [SIGFPE] = "FPE",
4411 [SIGKILL] = "KILL",
4412 [SIGUSR1] = "USR1",
4413 [SIGSEGV] = "SEGV",
4414 [SIGUSR2] = "USR2",
4415 [SIGPIPE] = "PIPE",
4416 [SIGALRM] = "ALRM",
4417 [SIGTERM] = "TERM",
f26ee0b9
LP
4418#ifdef SIGSTKFLT
4419 [SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */
4420#endif
2e22afe9
LP
4421 [SIGCHLD] = "CHLD",
4422 [SIGCONT] = "CONT",
4423 [SIGSTOP] = "STOP",
4424 [SIGTSTP] = "TSTP",
4425 [SIGTTIN] = "TTIN",
4426 [SIGTTOU] = "TTOU",
4427 [SIGURG] = "URG",
4428 [SIGXCPU] = "XCPU",
4429 [SIGXFSZ] = "XFSZ",
4430 [SIGVTALRM] = "VTALRM",
4431 [SIGPROF] = "PROF",
4432 [SIGWINCH] = "WINCH",
4433 [SIGIO] = "IO",
4434 [SIGPWR] = "PWR",
4435 [SIGSYS] = "SYS"
4436};
4437
4438DEFINE_STRING_TABLE_LOOKUP(signal, int);