1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
31 #include <sys/resource.h>
32 #include <linux/sched.h>
33 #include <sys/types.h>
37 #include <sys/ioctl.h>
39 #include <linux/tiocl.h>
42 #include <sys/inotify.h>
54 bool streq_ptr(const char *a, const char *b) {
56 /* Like streq(), but tries to make sense of NULL pointers */
67 usec_t now(clockid_t clock_id) {
70 assert_se(clock_gettime(clock_id, &ts) == 0);
72 return timespec_load(&ts);
75 usec_t timespec_load(const struct timespec *ts) {
79 (usec_t) ts->tv_sec * USEC_PER_SEC +
80 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
83 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
86 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
87 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
92 usec_t timeval_load(const struct timeval *tv) {
96 (usec_t) tv->tv_sec * USEC_PER_SEC +
100 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
103 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
104 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
109 bool endswith(const char *s, const char *postfix) {
116 pl = strlen(postfix);
124 return memcmp(s + sl - pl, postfix, pl) == 0;
127 bool startswith(const char *s, const char *prefix) {
142 return memcmp(s, prefix, pl) == 0;
145 bool startswith_no_case(const char *s, const char *prefix) {
161 for(i = 0; i < pl; ++i) {
162 if (tolower(s[i]) != tolower(prefix[i]))
169 bool first_word(const char *s, const char *word) {
184 if (memcmp(s, word, wl) != 0)
188 strchr(WHITESPACE, s[wl]);
191 int close_nointr(int fd) {
197 if ((r = close(fd)) >= 0)
205 void close_nointr_nofail(int fd) {
206 int saved_errno = errno;
208 /* like close_nointr() but cannot fail, and guarantees errno
211 assert_se(close_nointr(fd) == 0);
216 int parse_boolean(const char *v) {
219 if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
221 else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
227 int safe_atou(const char *s, unsigned *ret_u) {
235 l = strtoul(s, &x, 0);
237 if (!x || *x || errno)
238 return errno ? -errno : -EINVAL;
240 if ((unsigned long) (unsigned) l != l)
243 *ret_u = (unsigned) l;
247 int safe_atoi(const char *s, int *ret_i) {
255 l = strtol(s, &x, 0);
257 if (!x || *x || errno)
258 return errno ? -errno : -EINVAL;
260 if ((long) (int) l != l)
267 int safe_atolu(const char *s, long unsigned *ret_lu) {
275 l = strtoul(s, &x, 0);
277 if (!x || *x || errno)
278 return errno ? -errno : -EINVAL;
284 int safe_atoli(const char *s, long int *ret_li) {
292 l = strtol(s, &x, 0);
294 if (!x || *x || errno)
295 return errno ? -errno : -EINVAL;
301 int safe_atollu(const char *s, long long unsigned *ret_llu) {
303 unsigned long long l;
309 l = strtoull(s, &x, 0);
311 if (!x || *x || errno)
312 return errno ? -errno : -EINVAL;
318 int safe_atolli(const char *s, long long int *ret_lli) {
326 l = strtoll(s, &x, 0);
328 if (!x || *x || errno)
329 return errno ? -errno : -EINVAL;
335 /* Split a string into words. */
336 char *split(const char *c, size_t *l, const char *separator, char **state) {
339 current = *state ? *state : (char*) c;
341 if (!*current || *c == 0)
344 current += strspn(current, separator);
345 *l = strcspn(current, separator);
348 return (char*) current;
351 /* Split a string into words, but consider strings enclosed in '' and
352 * "" as words even if they include spaces. */
353 char *split_quoted(const char *c, size_t *l, char **state) {
356 current = *state ? *state : (char*) c;
358 if (!*current || *c == 0)
361 current += strspn(current, WHITESPACE);
363 if (*current == '\'') {
365 *l = strcspn(current, "'");
370 } else if (*current == '\"') {
372 *l = strcspn(current, "\"");
378 *l = strcspn(current, WHITESPACE);
382 /* FIXME: Cannot deal with strings that have spaces AND ticks
385 return (char*) current;
388 char **split_path_and_make_absolute(const char *p) {
392 if (!(l = strv_split(p, ":")))
395 if (!strv_path_make_absolute_cwd(l)) {
403 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
406 char fn[132], line[256], *p;
407 long long unsigned ppid;
412 assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%llu/stat", (unsigned long long) pid) < (int) (sizeof(fn)-1));
413 fn[sizeof(fn)-1] = 0;
415 if (!(f = fopen(fn, "r")))
418 if (!(fgets(line, sizeof(line), f))) {
426 /* Let's skip the pid and comm fields. The latter is enclosed
427 * in () but does not escape any () in its value, so let's
428 * skip over it manually */
430 if (!(p = strrchr(line, ')')))
441 if ((long long unsigned) (pid_t) ppid != ppid)
444 *_ppid = (pid_t) ppid;
449 int write_one_line_file(const char *fn, const char *line) {
456 if (!(f = fopen(fn, "we")))
459 if (fputs(line, f) < 0) {
470 int read_one_line_file(const char *fn, char **line) {
478 if (!(f = fopen(fn, "re")))
481 if (!(fgets(t, sizeof(t), f))) {
486 if (!(c = strdup(t))) {
499 char *truncate_nl(char *s) {
502 s[strcspn(s, NEWLINE)] = 0;
506 int get_process_name(pid_t pid, char **name) {
513 if (asprintf(&p, "/proc/%llu/comm", (unsigned long long) pid) < 0)
516 r = read_one_line_file(p, name);
526 char *strappend(const char *s, const char *suffix) {
536 if (!(r = new(char, a+b+1)))
540 memcpy(r+a, suffix, b);
546 int readlink_malloc(const char *p, char **r) {
556 if (!(c = new(char, l)))
559 if ((n = readlink(p, c, l-1)) < 0) {
565 if ((size_t) n < l-1) {
576 char *file_name_from_path(const char *p) {
581 if ((r = strrchr(p, '/')))
587 bool path_is_absolute(const char *p) {
593 bool is_path(const char *p) {
595 return !!strchr(p, '/');
598 char *path_make_absolute(const char *p, const char *prefix) {
603 /* Makes every item in the list an absolute path by prepending
604 * the prefix, if specified and necessary */
606 if (path_is_absolute(p) || !prefix)
609 if (asprintf(&r, "%s/%s", prefix, p) < 0)
615 char *path_make_absolute_cwd(const char *p) {
620 /* Similar to path_make_absolute(), but prefixes with the
621 * current working directory. */
623 if (path_is_absolute(p))
626 if (!(cwd = get_current_dir_name()))
629 r = path_make_absolute(p, cwd);
635 char **strv_path_make_absolute_cwd(char **l) {
638 /* Goes through every item in the string list and makes it
639 * absolute. This works in place and won't rollback any
640 * changes on failure. */
645 if (!(t = path_make_absolute_cwd(*s)))
655 int reset_all_signal_handlers(void) {
658 for (sig = 1; sig < _NSIG; sig++) {
661 if (sig == SIGKILL || sig == SIGSTOP)
665 sa.sa_handler = SIG_DFL;
666 sa.sa_flags = SA_RESTART;
668 /* On Linux the first two RT signals are reserved by
669 * glibc, and sigaction() will return EINVAL for them. */
670 if ((sigaction(sig, &sa, NULL) < 0))
678 char *strstrip(char *s) {
681 /* Drops trailing whitespace. Modifies the string in
682 * place. Returns pointer to first non-space character */
684 s += strspn(s, WHITESPACE);
687 if (!strchr(WHITESPACE, *e))
698 char *delete_chars(char *s, const char *bad) {
701 /* Drops all whitespace, regardless where in the string */
703 for (f = s, t = s; *f; f++) {
715 char *file_in_same_dir(const char *path, const char *filename) {
722 /* This removes the last component of path and appends
723 * filename, unless the latter is absolute anyway or the
726 if (path_is_absolute(filename))
727 return strdup(filename);
729 if (!(e = strrchr(path, '/')))
730 return strdup(filename);
732 k = strlen(filename);
733 if (!(r = new(char, e-path+1+k+1)))
736 memcpy(r, path, e-path+1);
737 memcpy(r+(e-path)+1, filename, k+1);
742 int mkdir_parents(const char *path, mode_t mode) {
747 /* Creates every parent directory in the path except the last
750 p = path + strspn(path, "/");
755 e = p + strcspn(p, "/");
756 p = e + strspn(e, "/");
758 /* Is this the last component? If so, then we're
763 if (!(t = strndup(path, e - path)))
770 if (r < 0 && errno != EEXIST)
775 int mkdir_p(const char *path, mode_t mode) {
780 if ((r = mkdir_parents(path, mode)) < 0)
783 if (mkdir(path, mode) < 0)
789 char hexchar(int x) {
790 static const char table[16] = "0123456789abcdef";
792 return table[x & 15];
795 int unhexchar(char c) {
797 if (c >= '0' && c <= '9')
800 if (c >= 'a' && c <= 'f')
803 if (c >= 'A' && c <= 'F')
809 char octchar(int x) {
810 return '0' + (x & 7);
813 int unoctchar(char c) {
815 if (c >= '0' && c <= '7')
821 char decchar(int x) {
822 return '0' + (x % 10);
825 int undecchar(char c) {
827 if (c >= '0' && c <= '9')
833 char *cescape(const char *s) {
839 /* Does C style string escaping. */
841 if (!(r = new(char, strlen(s)*4 + 1)))
844 for (f = s, t = r; *f; f++)
890 /* For special chars we prefer octal over
891 * hexadecimal encoding, simply because glib's
892 * g_strescape() does the same */
893 if ((*f < ' ') || (*f >= 127)) {
895 *(t++) = octchar((unsigned char) *f >> 6);
896 *(t++) = octchar((unsigned char) *f >> 3);
897 *(t++) = octchar((unsigned char) *f);
908 char *cunescape(const char *s) {
914 /* Undoes C style string escaping */
916 if (!(r = new(char, strlen(s)+1)))
919 for (f = s, t = r; *f; f++) {
962 /* hexadecimal encoding */
965 if ((a = unhexchar(f[1])) < 0 ||
966 (b = unhexchar(f[2])) < 0) {
967 /* Invalid escape code, let's take it literal then */
971 *(t++) = (char) ((a << 4) | b);
989 if ((a = unoctchar(f[0])) < 0 ||
990 (b = unoctchar(f[1])) < 0 ||
991 (c = unoctchar(f[2])) < 0) {
992 /* Invalid escape code, let's take it literal then */
996 *(t++) = (char) ((a << 6) | (b << 3) | c);
1004 /* premature end of string.*/
1009 /* Invalid escape code, let's take it literal then */
1022 char *xescape(const char *s, const char *bad) {
1026 /* Escapes all chars in bad, in addition to \ and all special
1027 * chars, in \xFF style escaping. May be reversed with
1030 if (!(r = new(char, strlen(s)*4+1)))
1033 for (f = s, t = r; *f; f++) {
1035 if ((*f < ' ') || (*f >= 127) ||
1036 (*f == '\\') || strchr(bad, *f)) {
1039 *(t++) = hexchar(*f >> 4);
1040 *(t++) = hexchar(*f);
1050 char *bus_path_escape(const char *s) {
1056 /* Escapes all chars that D-Bus' object path cannot deal
1057 * with. Can be reverse with bus_path_unescape() */
1059 if (!(r = new(char, strlen(s)*3+1)))
1062 for (f = s, t = r; *f; f++) {
1064 if (!(*f >= 'A' && *f <= 'Z') &&
1065 !(*f >= 'a' && *f <= 'z') &&
1066 !(*f >= '0' && *f <= '9')) {
1068 *(t++) = hexchar(*f >> 4);
1069 *(t++) = hexchar(*f);
1079 char *bus_path_unescape(const char *f) {
1084 if (!(r = strdup(f)))
1087 for (t = r; *f; f++) {
1092 if ((a = unhexchar(f[1])) < 0 ||
1093 (b = unhexchar(f[2])) < 0) {
1094 /* Invalid escape code, let's take it literal then */
1097 *(t++) = (char) ((a << 4) | b);
1109 char *path_kill_slashes(char *path) {
1113 /* Removes redundant inner and trailing slashes. Modifies the
1114 * passed string in-place.
1116 * ///foo///bar/ becomes /foo/bar
1119 for (f = path, t = path; *f; f++) {
1134 /* Special rule, if we are talking of the root directory, a
1135 trailing slash is good */
1137 if (t == path && slash)
1144 bool path_startswith(const char *path, const char *prefix) {
1148 if ((path[0] == '/') != (prefix[0] == '/'))
1154 path += strspn(path, "/");
1155 prefix += strspn(prefix, "/");
1163 a = strcspn(path, "/");
1164 b = strcspn(prefix, "/");
1169 if (memcmp(path, prefix, a) != 0)
1177 bool path_equal(const char *a, const char *b) {
1181 if ((a[0] == '/') != (b[0] == '/'))
1187 a += strspn(a, "/");
1188 b += strspn(b, "/");
1190 if (*a == 0 && *b == 0)
1193 if (*a == 0 || *b == 0)
1196 j = strcspn(a, "/");
1197 k = strcspn(b, "/");
1202 if (memcmp(a, b, j) != 0)
1210 char *ascii_strlower(char *t) {
1215 for (p = t; *p; p++)
1216 if (*p >= 'A' && *p <= 'Z')
1217 *p = *p - 'A' + 'a';
1222 bool ignore_file(const char *filename) {
1226 filename[0] == '.' ||
1227 streq(filename, "lost+found") ||
1228 endswith(filename, "~") ||
1229 endswith(filename, ".rpmnew") ||
1230 endswith(filename, ".rpmsave") ||
1231 endswith(filename, ".rpmorig") ||
1232 endswith(filename, ".dpkg-old") ||
1233 endswith(filename, ".dpkg-new") ||
1234 endswith(filename, ".swp");
1237 int fd_nonblock(int fd, bool nonblock) {
1242 if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
1246 flags |= O_NONBLOCK;
1248 flags &= ~O_NONBLOCK;
1250 if (fcntl(fd, F_SETFL, flags) < 0)
1256 int fd_cloexec(int fd, bool cloexec) {
1261 if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
1265 flags |= FD_CLOEXEC;
1267 flags &= ~FD_CLOEXEC;
1269 if (fcntl(fd, F_SETFD, flags) < 0)
1275 int close_all_fds(const int except[], unsigned n_except) {
1280 if (!(d = opendir("/proc/self/fd")))
1283 while ((de = readdir(d))) {
1286 if (ignore_file(de->d_name))
1289 if ((r = safe_atoi(de->d_name, &fd)) < 0)
1303 for (i = 0; i < n_except; i++)
1304 if (except[i] == fd) {
1313 if ((r = close_nointr(fd)) < 0) {
1314 /* Valgrind has its own FD and doesn't want to have it closed */
1327 bool chars_intersect(const char *a, const char *b) {
1330 /* Returns true if any of the chars in a are in b. */
1331 for (p = a; *p; p++)
1338 char *format_timestamp(char *buf, size_t l, usec_t t) {
1348 sec = (time_t) t / USEC_PER_SEC;
1350 if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0)
1356 bool fstype_is_network(const char *fstype) {
1357 static const char * const table[] = {
1369 for (i = 0; i < ELEMENTSOF(table); i++)
1370 if (streq(table[i], fstype))
1379 if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
1384 TIOCL_GETKMSGREDIRECT,
1388 if (ioctl(fd, TIOCLINUX, tiocl) < 0)
1391 vt = tiocl[0] <= 0 ? 1 : tiocl[0];
1394 if (ioctl(fd, VT_ACTIVATE, vt) < 0)
1397 close_nointr_nofail(r);
1401 int read_one_char(FILE *f, char *ret, bool *need_nl) {
1402 struct termios old_termios, new_termios;
1409 if (tcgetattr(fileno(f), &old_termios) >= 0) {
1410 new_termios = old_termios;
1412 new_termios.c_lflag &= ~ICANON;
1413 new_termios.c_cc[VMIN] = 1;
1414 new_termios.c_cc[VTIME] = 0;
1416 if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
1419 k = fread(&c, 1, 1, f);
1421 tcsetattr(fileno(f), TCSADRAIN, &old_termios);
1427 *need_nl = c != '\n';
1434 if (!(fgets(line, sizeof(line), f)))
1439 if (strlen(line) != 1)
1449 int ask(char *ret, const char *replies, const char *text, ...) {
1458 bool need_nl = true;
1460 fputs("\x1B[1m", stdout);
1466 fputs("\x1B[0m", stdout);
1470 if ((r = read_one_char(stdin, &c, &need_nl)) < 0) {
1472 if (r == -EBADMSG) {
1473 puts("Bad input, please try again.");
1484 if (strchr(replies, c)) {
1489 puts("Read unexpected character, please try again.");
1493 int reset_terminal(int fd) {
1494 struct termios termios;
1499 /* Set terminal to some sane defaults */
1501 if (tcgetattr(fd, &termios) < 0) {
1506 /* We only reset the stuff that matters to the software. How
1507 * hardware is set up we don't touch assuming that somebody
1508 * else will do that for us */
1510 termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
1511 termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
1512 termios.c_oflag |= ONLCR;
1513 termios.c_cflag |= CREAD;
1514 termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
1516 termios.c_cc[VINTR] = 03; /* ^C */
1517 termios.c_cc[VQUIT] = 034; /* ^\ */
1518 termios.c_cc[VERASE] = 0177;
1519 termios.c_cc[VKILL] = 025; /* ^X */
1520 termios.c_cc[VEOF] = 04; /* ^D */
1521 termios.c_cc[VSTART] = 021; /* ^Q */
1522 termios.c_cc[VSTOP] = 023; /* ^S */
1523 termios.c_cc[VSUSP] = 032; /* ^Z */
1524 termios.c_cc[VLNEXT] = 026; /* ^V */
1525 termios.c_cc[VWERASE] = 027; /* ^W */
1526 termios.c_cc[VREPRINT] = 022; /* ^R */
1527 termios.c_cc[VEOL] = 0;
1528 termios.c_cc[VEOL2] = 0;
1530 termios.c_cc[VTIME] = 0;
1531 termios.c_cc[VMIN] = 1;
1533 if (tcsetattr(fd, TCSANOW, &termios) < 0)
1537 /* Just in case, flush all crap out */
1538 tcflush(fd, TCIOFLUSH);
1543 int open_terminal(const char *name, int mode) {
1546 if ((fd = open(name, mode)) < 0)
1549 if ((r = isatty(fd)) < 0) {
1550 close_nointr_nofail(fd);
1555 close_nointr_nofail(fd);
1562 int flush_fd(int fd) {
1563 struct pollfd pollfd;
1567 pollfd.events = POLLIN;
1574 if ((r = poll(&pollfd, 1, 0)) < 0) {
1585 if ((l = read(fd, buf, sizeof(buf))) < 0) {
1590 if (errno == EAGAIN)
1601 int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) {
1602 int fd = -1, notify = -1, r, wd = -1;
1606 /* We use inotify to be notified when the tty is closed. We
1607 * create the watch before checking if we can actually acquire
1608 * it, so that we don't lose any event.
1610 * Note: strictly speaking this actually watches for the
1611 * device being closed, it does *not* really watch whether a
1612 * tty loses its controlling process. However, unless some
1613 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
1614 * its tty otherwise this will not become a problem. As long
1615 * as the administrator makes sure not configure any service
1616 * on the same tty as an untrusted user this should not be a
1617 * problem. (Which he probably should not do anyway.) */
1619 if (!fail && !force) {
1620 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
1625 if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) {
1633 if ((r = flush_fd(notify)) < 0)
1636 /* We pass here O_NOCTTY only so that we can check the return
1637 * value TIOCSCTTY and have a reliable way to figure out if we
1638 * successfully became the controlling process of the tty */
1639 if ((fd = open_terminal(name, O_RDWR|O_NOCTTY)) < 0)
1642 /* First, try to get the tty */
1643 r = ioctl(fd, TIOCSCTTY, force);
1645 /* Sometimes it makes sense to ignore TIOCSCTTY
1646 * returning EPERM, i.e. when very likely we already
1647 * are have this controlling terminal. */
1648 if (r < 0 && errno == EPERM && ignore_tiocstty_eperm)
1651 if (r < 0 && (force || fail || errno != EPERM)) {
1661 assert(notify >= 0);
1664 struct inotify_event e;
1667 if ((l = read(notify, &e, sizeof(e))) != sizeof(e)) {
1681 if (e.wd != wd || !(e.mask & IN_CLOSE)) {
1689 /* We close the tty fd here since if the old session
1690 * ended our handle will be dead. It's important that
1691 * we do this after sleeping, so that we don't enter
1692 * an endless loop. */
1693 close_nointr_nofail(fd);
1697 close_nointr_nofail(notify);
1699 if ((r = reset_terminal(fd)) < 0)
1700 log_warning("Failed to reset terminal: %s", strerror(-r));
1706 close_nointr_nofail(fd);
1709 close_nointr_nofail(notify);
1714 int release_terminal(void) {
1716 struct sigaction sa_old, sa_new;
1718 if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
1721 /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
1722 * by our own TIOCNOTTY */
1725 sa_new.sa_handler = SIG_IGN;
1726 sa_new.sa_flags = SA_RESTART;
1727 assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
1729 if (ioctl(fd, TIOCNOTTY) < 0)
1732 assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
1734 close_nointr_nofail(fd);
1738 int ignore_signal(int sig) {
1739 struct sigaction sa;
1742 sa.sa_handler = SIG_IGN;
1743 sa.sa_flags = SA_RESTART;
1745 return sigaction(sig, &sa, NULL);
1748 int close_pipe(int p[]) {
1754 a = close_nointr(p[0]);
1759 b = close_nointr(p[1]);
1763 return a < 0 ? a : b;
1766 ssize_t loop_read(int fd, void *buf, size_t nbytes) {
1775 while (nbytes > 0) {
1778 if ((k = read(fd, p, nbytes)) <= 0) {
1783 if (errno == EAGAIN) {
1784 struct pollfd pollfd;
1788 pollfd.events = POLLIN;
1790 if (poll(&pollfd, 1, -1) < 0) {
1794 return n > 0 ? n : -errno;
1797 if (pollfd.revents != POLLIN)
1798 return n > 0 ? n : -EIO;
1803 return n > 0 ? n : (k < 0 ? -errno : 0);
1814 int path_is_mount_point(const char *t) {
1818 if (lstat(t, &a) < 0) {
1820 if (errno == ENOENT)
1826 if (!(copy = strdup(t)))
1829 if (lstat(dirname(copy), &b) < 0) {
1836 return a.st_dev != b.st_dev;
1839 int parse_usec(const char *t, usec_t *usec) {
1840 static const struct {
1844 { "sec", USEC_PER_SEC },
1845 { "s", USEC_PER_SEC },
1846 { "min", USEC_PER_MINUTE },
1847 { "hr", USEC_PER_HOUR },
1848 { "h", USEC_PER_HOUR },
1849 { "d", USEC_PER_DAY },
1850 { "w", USEC_PER_WEEK },
1851 { "msec", USEC_PER_MSEC },
1852 { "ms", USEC_PER_MSEC },
1853 { "m", USEC_PER_MINUTE },
1856 { "", USEC_PER_SEC },
1872 l = strtoll(p, &e, 10);
1883 e += strspn(e, WHITESPACE);
1885 for (i = 0; i < ELEMENTSOF(table); i++)
1886 if (startswith(e, table[i].suffix)) {
1887 r += (usec_t) l * table[i].usec;
1888 p = e + strlen(table[i].suffix);
1892 if (i >= ELEMENTSOF(table))
1902 int make_stdio(int fd) {
1907 r = dup2(fd, STDIN_FILENO);
1908 s = dup2(fd, STDOUT_FILENO);
1909 t = dup2(fd, STDERR_FILENO);
1912 close_nointr_nofail(fd);
1914 if (r < 0 || s < 0 || t < 0)
1920 bool is_clean_exit(int code, int status) {
1922 if (code == CLD_EXITED)
1925 /* If a daemon does not implement handlers for some of the
1926 * signals that's not considered an unclean shutdown */
1927 if (code == CLD_KILLED)
1931 status == SIGTERM ||
1937 bool is_device_path(const char *path) {
1939 /* Returns true on paths that refer to a device, either in
1940 * sysfs or in /dev */
1943 path_startswith(path, "/dev/") ||
1944 path_startswith(path, "/sys/");
1947 static const char *const ioprio_class_table[] = {
1948 [IOPRIO_CLASS_NONE] = "none",
1949 [IOPRIO_CLASS_RT] = "realtime",
1950 [IOPRIO_CLASS_BE] = "best-effort",
1951 [IOPRIO_CLASS_IDLE] = "idle"
1954 DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
1956 static const char *const sigchld_code_table[] = {
1957 [CLD_EXITED] = "exited",
1958 [CLD_KILLED] = "killed",
1959 [CLD_DUMPED] = "dumped",
1960 [CLD_TRAPPED] = "trapped",
1961 [CLD_STOPPED] = "stopped",
1962 [CLD_CONTINUED] = "continued",
1965 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
1967 static const char *const log_facility_table[LOG_NFACILITIES] = {
1968 [LOG_FAC(LOG_KERN)] = "kern",
1969 [LOG_FAC(LOG_USER)] = "user",
1970 [LOG_FAC(LOG_MAIL)] = "mail",
1971 [LOG_FAC(LOG_DAEMON)] = "daemon",
1972 [LOG_FAC(LOG_AUTH)] = "auth",
1973 [LOG_FAC(LOG_SYSLOG)] = "syslog",
1974 [LOG_FAC(LOG_LPR)] = "lpr",
1975 [LOG_FAC(LOG_NEWS)] = "news",
1976 [LOG_FAC(LOG_UUCP)] = "uucp",
1977 [LOG_FAC(LOG_CRON)] = "cron",
1978 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
1979 [LOG_FAC(LOG_FTP)] = "ftp",
1980 [LOG_FAC(LOG_LOCAL0)] = "local0",
1981 [LOG_FAC(LOG_LOCAL1)] = "local1",
1982 [LOG_FAC(LOG_LOCAL2)] = "local2",
1983 [LOG_FAC(LOG_LOCAL3)] = "local3",
1984 [LOG_FAC(LOG_LOCAL4)] = "local4",
1985 [LOG_FAC(LOG_LOCAL5)] = "local5",
1986 [LOG_FAC(LOG_LOCAL6)] = "local6",
1987 [LOG_FAC(LOG_LOCAL7)] = "local7"
1990 DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
1992 static const char *const log_level_table[] = {
1993 [LOG_EMERG] = "emerg",
1994 [LOG_ALERT] = "alert",
1995 [LOG_CRIT] = "crit",
1997 [LOG_WARNING] = "warning",
1998 [LOG_NOTICE] = "notice",
1999 [LOG_INFO] = "info",
2000 [LOG_DEBUG] = "debug"
2003 DEFINE_STRING_TABLE_LOOKUP(log_level, int);
2005 static const char* const sched_policy_table[] = {
2006 [SCHED_OTHER] = "other",
2007 [SCHED_BATCH] = "batch",
2008 [SCHED_IDLE] = "idle",
2009 [SCHED_FIFO] = "fifo",
2013 DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
2015 static const char* const rlimit_table[] = {
2016 [RLIMIT_CPU] = "LimitCPU",
2017 [RLIMIT_FSIZE] = "LimitFSIZE",
2018 [RLIMIT_DATA] = "LimitDATA",
2019 [RLIMIT_STACK] = "LimitSTACK",
2020 [RLIMIT_CORE] = "LimitCORE",
2021 [RLIMIT_RSS] = "LimitRSS",
2022 [RLIMIT_NOFILE] = "LimitNOFILE",
2023 [RLIMIT_AS] = "LimitAS",
2024 [RLIMIT_NPROC] = "LimitNPROC",
2025 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
2026 [RLIMIT_LOCKS] = "LimitLOCKS",
2027 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
2028 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
2029 [RLIMIT_NICE] = "LimitNICE",
2030 [RLIMIT_RTPRIO] = "LimitRTPRIO",
2031 [RLIMIT_RTTIME] = "LimitRTTIME"
2034 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);