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[] = {
1367 for (i = 0; i < ELEMENTSOF(table); i++)
1368 if (streq(table[i], fstype))
1377 if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
1382 TIOCL_GETKMSGREDIRECT,
1386 if (ioctl(fd, TIOCLINUX, tiocl) < 0)
1389 vt = tiocl[0] <= 0 ? 1 : tiocl[0];
1392 if (ioctl(fd, VT_ACTIVATE, vt) < 0)
1395 close_nointr_nofail(r);
1399 int read_one_char(FILE *f, char *ret, bool *need_nl) {
1400 struct termios old_termios, new_termios;
1407 if (tcgetattr(fileno(f), &old_termios) >= 0) {
1408 new_termios = old_termios;
1410 new_termios.c_lflag &= ~ICANON;
1411 new_termios.c_cc[VMIN] = 1;
1412 new_termios.c_cc[VTIME] = 0;
1414 if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
1417 k = fread(&c, 1, 1, f);
1419 tcsetattr(fileno(f), TCSADRAIN, &old_termios);
1425 *need_nl = c != '\n';
1432 if (!(fgets(line, sizeof(line), f)))
1437 if (strlen(line) != 1)
1447 int ask(char *ret, const char *replies, const char *text, ...) {
1456 bool need_nl = true;
1458 fputs("\x1B[1m", stdout);
1464 fputs("\x1B[0m", stdout);
1468 if ((r = read_one_char(stdin, &c, &need_nl)) < 0) {
1470 if (r == -EBADMSG) {
1471 puts("Bad input, please try again.");
1482 if (strchr(replies, c)) {
1487 puts("Read unexpected character, please try again.");
1491 int reset_terminal(int fd) {
1492 struct termios termios;
1497 /* Set terminal to some sane defaults */
1499 if (tcgetattr(fd, &termios) < 0) {
1504 /* We only reset the stuff that matters to the software. How
1505 * hardware is set up we don't touch assuming that somebody
1506 * else will do that for us */
1508 termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
1509 termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
1510 termios.c_oflag |= ONLCR;
1511 termios.c_cflag |= CREAD;
1512 termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
1514 termios.c_cc[VINTR] = 03; /* ^C */
1515 termios.c_cc[VQUIT] = 034; /* ^\ */
1516 termios.c_cc[VERASE] = 0177;
1517 termios.c_cc[VKILL] = 025; /* ^X */
1518 termios.c_cc[VEOF] = 04; /* ^D */
1519 termios.c_cc[VSTART] = 021; /* ^Q */
1520 termios.c_cc[VSTOP] = 023; /* ^S */
1521 termios.c_cc[VSUSP] = 032; /* ^Z */
1522 termios.c_cc[VLNEXT] = 026; /* ^V */
1523 termios.c_cc[VWERASE] = 027; /* ^W */
1524 termios.c_cc[VREPRINT] = 022; /* ^R */
1525 termios.c_cc[VEOL] = 0;
1526 termios.c_cc[VEOL2] = 0;
1528 termios.c_cc[VTIME] = 0;
1529 termios.c_cc[VMIN] = 1;
1531 if (tcsetattr(fd, TCSANOW, &termios) < 0)
1535 /* Just in case, flush all crap out */
1536 tcflush(fd, TCIOFLUSH);
1541 int open_terminal(const char *name, int mode) {
1544 if ((fd = open(name, mode)) < 0)
1547 if ((r = isatty(fd)) < 0) {
1548 close_nointr_nofail(fd);
1553 close_nointr_nofail(fd);
1560 int flush_fd(int fd) {
1561 struct pollfd pollfd;
1565 pollfd.events = POLLIN;
1572 if ((r = poll(&pollfd, 1, 0)) < 0) {
1583 if ((l = read(fd, buf, sizeof(buf))) < 0) {
1588 if (errno == EAGAIN)
1599 int acquire_terminal(const char *name, bool fail, bool force) {
1600 int fd = -1, notify = -1, r, wd = -1;
1604 /* We use inotify to be notified when the tty is closed. We
1605 * create the watch before checking if we can actually acquire
1606 * it, so that we don't lose any event.
1608 * Note: strictly speaking this actually watches for the
1609 * device being closed, it does *not* really watch whether a
1610 * tty loses its controlling process. However, unless some
1611 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
1612 * its tty otherwise this will not become a problem. As long
1613 * as the administrator makes sure not configure any service
1614 * on the same tty as an untrusted user this should not be a
1615 * problem. (Which he probably should not do anyway.) */
1617 if (!fail && !force) {
1618 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
1623 if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) {
1630 if ((r = flush_fd(notify)) < 0)
1633 /* We pass here O_NOCTTY only so that we can check the return
1634 * value TIOCSCTTY and have a reliable way to figure out if we
1635 * successfully became the controlling process of the tty */
1636 if ((fd = open_terminal(name, O_RDWR|O_NOCTTY)) < 0)
1639 /* First, try to get the tty */
1640 if ((r = ioctl(fd, TIOCSCTTY, force)) < 0 &&
1641 (force || fail || errno != EPERM)) {
1651 assert(notify >= 0);
1654 struct inotify_event e;
1657 if ((l = read(notify, &e, sizeof(e))) != sizeof(e)) {
1671 if (e.wd != wd || !(e.mask & IN_CLOSE)) {
1679 /* We close the tty fd here since if the old session
1680 * ended our handle will be dead. It's important that
1681 * we do this after sleeping, so that we don't enter
1682 * an endless loop. */
1683 close_nointr_nofail(fd);
1687 close_nointr_nofail(notify);
1689 if ((r = reset_terminal(fd)) < 0)
1690 log_warning("Failed to reset terminal: %s", strerror(-r));
1696 close_nointr_nofail(fd);
1699 close_nointr_nofail(notify);
1704 int release_terminal(void) {
1706 struct sigaction sa_old, sa_new;
1708 if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
1711 /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
1712 * by our own TIOCNOTTY */
1715 sa_new.sa_handler = SIG_IGN;
1716 sa_new.sa_flags = SA_RESTART;
1717 assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
1719 if (ioctl(fd, TIOCNOTTY) < 0)
1722 assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
1724 close_nointr_nofail(fd);
1728 int ignore_signal(int sig) {
1729 struct sigaction sa;
1732 sa.sa_handler = SIG_IGN;
1733 sa.sa_flags = SA_RESTART;
1735 return sigaction(sig, &sa, NULL);
1738 int close_pipe(int p[]) {
1744 a = close_nointr(p[0]);
1749 b = close_nointr(p[1]);
1753 return a < 0 ? a : b;
1756 ssize_t loop_read(int fd, void *buf, size_t nbytes) {
1765 while (nbytes > 0) {
1768 if ((k = read(fd, p, nbytes)) <= 0) {
1773 if (errno == EAGAIN) {
1774 struct pollfd pollfd;
1778 pollfd.events = POLLIN;
1780 if (poll(&pollfd, 1, -1) < 0) {
1784 return n > 0 ? n : -errno;
1787 if (pollfd.revents != POLLIN)
1788 return n > 0 ? n : -EIO;
1793 return n > 0 ? n : (k < 0 ? -errno : 0);
1804 int path_is_mount_point(const char *t) {
1808 if (lstat(t, &a) < 0) {
1810 if (errno == ENOENT)
1816 if (!(copy = strdup(t)))
1819 if (lstat(dirname(copy), &b) < 0) {
1826 return a.st_dev != b.st_dev;
1829 int parse_usec(const char *t, usec_t *usec) {
1830 static const struct {
1834 { "sec", USEC_PER_SEC },
1835 { "s", USEC_PER_SEC },
1836 { "min", USEC_PER_MINUTE },
1837 { "hr", USEC_PER_HOUR },
1838 { "h", USEC_PER_HOUR },
1839 { "d", USEC_PER_DAY },
1840 { "w", USEC_PER_WEEK },
1841 { "msec", USEC_PER_MSEC },
1842 { "ms", USEC_PER_MSEC },
1843 { "m", USEC_PER_MINUTE },
1846 { "", USEC_PER_SEC },
1862 l = strtoll(p, &e, 10);
1873 e += strspn(e, WHITESPACE);
1875 for (i = 0; i < ELEMENTSOF(table); i++)
1876 if (startswith(e, table[i].suffix)) {
1877 r += (usec_t) l * table[i].usec;
1878 p = e + strlen(table[i].suffix);
1882 if (i >= ELEMENTSOF(table))
1892 static const char *const ioprio_class_table[] = {
1893 [IOPRIO_CLASS_NONE] = "none",
1894 [IOPRIO_CLASS_RT] = "realtime",
1895 [IOPRIO_CLASS_BE] = "best-effort",
1896 [IOPRIO_CLASS_IDLE] = "idle"
1899 DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
1901 static const char *const sigchld_code_table[] = {
1902 [CLD_EXITED] = "exited",
1903 [CLD_KILLED] = "killed",
1904 [CLD_DUMPED] = "dumped",
1905 [CLD_TRAPPED] = "trapped",
1906 [CLD_STOPPED] = "stopped",
1907 [CLD_CONTINUED] = "continued",
1910 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
1912 static const char *const log_facility_table[LOG_NFACILITIES] = {
1913 [LOG_FAC(LOG_KERN)] = "kern",
1914 [LOG_FAC(LOG_USER)] = "user",
1915 [LOG_FAC(LOG_MAIL)] = "mail",
1916 [LOG_FAC(LOG_DAEMON)] = "daemon",
1917 [LOG_FAC(LOG_AUTH)] = "auth",
1918 [LOG_FAC(LOG_SYSLOG)] = "syslog",
1919 [LOG_FAC(LOG_LPR)] = "lpr",
1920 [LOG_FAC(LOG_NEWS)] = "news",
1921 [LOG_FAC(LOG_UUCP)] = "uucp",
1922 [LOG_FAC(LOG_CRON)] = "cron",
1923 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
1924 [LOG_FAC(LOG_FTP)] = "ftp",
1925 [LOG_FAC(LOG_LOCAL0)] = "local0",
1926 [LOG_FAC(LOG_LOCAL1)] = "local1",
1927 [LOG_FAC(LOG_LOCAL2)] = "local2",
1928 [LOG_FAC(LOG_LOCAL3)] = "local3",
1929 [LOG_FAC(LOG_LOCAL4)] = "local4",
1930 [LOG_FAC(LOG_LOCAL5)] = "local5",
1931 [LOG_FAC(LOG_LOCAL6)] = "local6",
1932 [LOG_FAC(LOG_LOCAL7)] = "local7"
1935 DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
1937 static const char *const log_level_table[] = {
1938 [LOG_EMERG] = "emerg",
1939 [LOG_ALERT] = "alert",
1940 [LOG_CRIT] = "crit",
1942 [LOG_WARNING] = "warning",
1943 [LOG_NOTICE] = "notice",
1944 [LOG_INFO] = "info",
1945 [LOG_DEBUG] = "debug"
1948 DEFINE_STRING_TABLE_LOOKUP(log_level, int);
1950 static const char* const sched_policy_table[] = {
1951 [SCHED_OTHER] = "other",
1952 [SCHED_BATCH] = "batch",
1953 [SCHED_IDLE] = "idle",
1954 [SCHED_FIFO] = "fifo",
1958 DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
1960 static const char* const rlimit_table[] = {
1961 [RLIMIT_CPU] = "LimitCPU",
1962 [RLIMIT_FSIZE] = "LimitFSIZE",
1963 [RLIMIT_DATA] = "LimitDATA",
1964 [RLIMIT_STACK] = "LimitSTACK",
1965 [RLIMIT_CORE] = "LimitCORE",
1966 [RLIMIT_RSS] = "LimitRSS",
1967 [RLIMIT_NOFILE] = "LimitNOFILE",
1968 [RLIMIT_AS] = "LimitAS",
1969 [RLIMIT_NPROC] = "LimitNPROC",
1970 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
1971 [RLIMIT_LOCKS] = "LimitLOCKS",
1972 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
1973 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
1974 [RLIMIT_NICE] = "LimitNICE",
1975 [RLIMIT_RTPRIO] = "LimitRTPRIO",
1976 [RLIMIT_RTTIME] = "LimitRTTIME"
1979 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);