1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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>
46 #include <sys/prctl.h>
47 #include <sys/utsname.h>
49 #include <netinet/ip.h>
61 #include "exit-status.h"
63 bool streq_ptr(const char *a, const char *b) {
65 /* Like streq(), but tries to make sense of NULL pointers */
76 usec_t now(clockid_t clock_id) {
79 assert_se(clock_gettime(clock_id, &ts) == 0);
81 return timespec_load(&ts);
84 dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
87 ts->realtime = now(CLOCK_REALTIME);
88 ts->monotonic = now(CLOCK_MONOTONIC);
93 usec_t timespec_load(const struct timespec *ts) {
97 (usec_t) ts->tv_sec * USEC_PER_SEC +
98 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
101 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
104 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
105 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
110 usec_t timeval_load(const struct timeval *tv) {
114 (usec_t) tv->tv_sec * USEC_PER_SEC +
115 (usec_t) tv->tv_usec;
118 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
121 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
122 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
127 bool endswith(const char *s, const char *postfix) {
134 pl = strlen(postfix);
142 return memcmp(s + sl - pl, postfix, pl) == 0;
145 bool startswith(const char *s, const char *prefix) {
160 return memcmp(s, prefix, pl) == 0;
163 bool startswith_no_case(const char *s, const char *prefix) {
179 for(i = 0; i < pl; ++i) {
180 if (tolower(s[i]) != tolower(prefix[i]))
187 bool first_word(const char *s, const char *word) {
202 if (memcmp(s, word, wl) != 0)
206 strchr(WHITESPACE, s[wl]);
209 int close_nointr(int fd) {
215 if ((r = close(fd)) >= 0)
223 void close_nointr_nofail(int fd) {
224 int saved_errno = errno;
226 /* like close_nointr() but cannot fail, and guarantees errno
229 assert_se(close_nointr(fd) == 0);
234 void close_many(const int fds[], unsigned n_fd) {
237 for (i = 0; i < n_fd; i++)
238 close_nointr_nofail(fds[i]);
241 int parse_boolean(const char *v) {
244 if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
246 else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
252 int parse_pid(const char *s, pid_t* ret_pid) {
260 if ((r = safe_atolu(s, &ul)) < 0)
265 if ((unsigned long) pid != ul)
275 int safe_atou(const char *s, unsigned *ret_u) {
283 l = strtoul(s, &x, 0);
285 if (!x || *x || errno)
286 return errno ? -errno : -EINVAL;
288 if ((unsigned long) (unsigned) l != l)
291 *ret_u = (unsigned) l;
295 int safe_atoi(const char *s, int *ret_i) {
303 l = strtol(s, &x, 0);
305 if (!x || *x || errno)
306 return errno ? -errno : -EINVAL;
308 if ((long) (int) l != l)
315 int safe_atollu(const char *s, long long unsigned *ret_llu) {
317 unsigned long long l;
323 l = strtoull(s, &x, 0);
325 if (!x || *x || errno)
326 return errno ? -errno : -EINVAL;
332 int safe_atolli(const char *s, long long int *ret_lli) {
340 l = strtoll(s, &x, 0);
342 if (!x || *x || errno)
343 return errno ? -errno : -EINVAL;
349 /* Split a string into words. */
350 char *split(const char *c, size_t *l, const char *separator, char **state) {
353 current = *state ? *state : (char*) c;
355 if (!*current || *c == 0)
358 current += strspn(current, separator);
359 *l = strcspn(current, separator);
362 return (char*) current;
365 /* Split a string into words, but consider strings enclosed in '' and
366 * "" as words even if they include spaces. */
367 char *split_quoted(const char *c, size_t *l, char **state) {
369 bool escaped = false;
371 current = *state ? *state : (char*) c;
373 if (!*current || *c == 0)
376 current += strspn(current, WHITESPACE);
378 if (*current == '\'') {
381 for (e = current; *e; e++) {
391 *state = *e == 0 ? e : e+1;
392 } else if (*current == '\"') {
395 for (e = current; *e; e++) {
405 *state = *e == 0 ? e : e+1;
407 for (e = current; *e; e++) {
412 else if (strchr(WHITESPACE, *e))
419 return (char*) current;
422 char **split_path_and_make_absolute(const char *p) {
426 if (!(l = strv_split(p, ":")))
429 if (!strv_path_make_absolute_cwd(l)) {
437 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
440 char fn[132], line[256], *p;
446 assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
447 fn[sizeof(fn)-1] = 0;
449 if (!(f = fopen(fn, "r")))
452 if (!(fgets(line, sizeof(line), f))) {
460 /* Let's skip the pid and comm fields. The latter is enclosed
461 * in () but does not escape any () in its value, so let's
462 * skip over it manually */
464 if (!(p = strrchr(line, ')')))
475 if ((long unsigned) (pid_t) ppid != ppid)
478 *_ppid = (pid_t) ppid;
483 int write_one_line_file(const char *fn, const char *line) {
490 if (!(f = fopen(fn, "we")))
493 if (fputs(line, f) < 0) {
504 int read_one_line_file(const char *fn, char **line) {
507 char t[LINE_MAX], *c;
512 if (!(f = fopen(fn, "re")))
515 if (!(fgets(t, sizeof(t), f))) {
520 if (!(c = strdup(t))) {
533 int read_full_file(const char *fn, char **contents) {
540 if (!(f = fopen(fn, "re")))
543 if (fstat(fileno(f), &st) < 0) {
548 n = st.st_size > 0 ? st.st_size : LINE_MAX;
555 if (!(t = realloc(buf, n+1))) {
561 k = fread(buf + l, 1, n - l, f);
576 if (n > 4*1024*1024) {
584 else if (!(buf = calloc(1, 1))) {
603 const char *seperator, ...) {
611 if ((r = read_full_file(fname, &contents)) < 0)
616 const char *key = NULL;
618 p += strspn(p, seperator);
619 p += strspn(p, WHITESPACE);
624 if (!strchr(COMMENTS, *p)) {
628 va_start(ap, seperator);
629 while ((key = va_arg(ap, char *))) {
633 value = va_arg(ap, char **);
636 if (strncmp(p, key, n) != 0 ||
641 n = strcspn(p, seperator);
644 strchr(QUOTES, p[0]) &&
646 v = strndup(p+1, n-2);
668 p += strcspn(p, seperator);
676 char *truncate_nl(char *s) {
679 s[strcspn(s, NEWLINE)] = 0;
683 int get_process_name(pid_t pid, char **name) {
690 if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
693 r = read_one_line_file(p, name);
703 int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
711 assert(max_length > 0);
714 if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
723 if (!(r = new(char, max_length))) {
730 while ((c = getc(f)) != EOF) {
752 size_t n = MIN(left-1, 3U);
760 /* Kernel threads have no argv[] */
767 if ((h = get_process_name(pid, &t)) < 0)
770 h = asprintf(&r, "[%s]", t);
781 char *strnappend(const char *s, const char *suffix, size_t b) {
789 return strndup(suffix, b);
799 if (!(r = new(char, a+b+1)))
803 memcpy(r+a, suffix, b);
809 char *strappend(const char *s, const char *suffix) {
810 return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
813 int readlink_malloc(const char *p, char **r) {
823 if (!(c = new(char, l)))
826 if ((n = readlink(p, c, l-1)) < 0) {
832 if ((size_t) n < l-1) {
843 int readlink_and_make_absolute(const char *p, char **r) {
850 if ((j = readlink_malloc(p, &target)) < 0)
853 k = file_in_same_dir(p, target);
863 int parent_of_path(const char *path, char **_r) {
864 const char *e, *a = NULL, *b = NULL, *p;
874 for (e = path; *e; e++) {
876 if (!slash && *e == '/') {
880 } else if (slash && *e != '/')
895 r = strndup(path, p-path);
905 char *file_name_from_path(const char *p) {
910 if ((r = strrchr(p, '/')))
916 bool path_is_absolute(const char *p) {
922 bool is_path(const char *p) {
924 return !!strchr(p, '/');
927 char *path_make_absolute(const char *p, const char *prefix) {
932 /* Makes every item in the list an absolute path by prepending
933 * the prefix, if specified and necessary */
935 if (path_is_absolute(p) || !prefix)
938 if (asprintf(&r, "%s/%s", prefix, p) < 0)
944 char *path_make_absolute_cwd(const char *p) {
949 /* Similar to path_make_absolute(), but prefixes with the
950 * current working directory. */
952 if (path_is_absolute(p))
955 if (!(cwd = get_current_dir_name()))
958 r = path_make_absolute(p, cwd);
964 char **strv_path_make_absolute_cwd(char **l) {
967 /* Goes through every item in the string list and makes it
968 * absolute. This works in place and won't rollback any
969 * changes on failure. */
974 if (!(t = path_make_absolute_cwd(*s)))
984 char **strv_path_canonicalize(char **l) {
992 /* Goes through every item in the string list and canonicalize
993 * the path. This works in place and won't rollback any
994 * changes on failure. */
999 t = path_make_absolute_cwd(*s);
1008 u = canonicalize_file_name(t);
1012 if (errno == ENOMEM || !errno)
1029 int reset_all_signal_handlers(void) {
1032 for (sig = 1; sig < _NSIG; sig++) {
1033 struct sigaction sa;
1035 if (sig == SIGKILL || sig == SIGSTOP)
1039 sa.sa_handler = SIG_DFL;
1040 sa.sa_flags = SA_RESTART;
1042 /* On Linux the first two RT signals are reserved by
1043 * glibc, and sigaction() will return EINVAL for them. */
1044 if ((sigaction(sig, &sa, NULL) < 0))
1045 if (errno != EINVAL)
1052 char *strstrip(char *s) {
1055 /* Drops trailing whitespace. Modifies the string in
1056 * place. Returns pointer to first non-space character */
1058 s += strspn(s, WHITESPACE);
1060 for (e = s; *e; e++)
1061 if (!strchr(WHITESPACE, *e))
1072 char *delete_chars(char *s, const char *bad) {
1075 /* Drops all whitespace, regardless where in the string */
1077 for (f = s, t = s; *f; f++) {
1078 if (strchr(bad, *f))
1089 char *file_in_same_dir(const char *path, const char *filename) {
1096 /* This removes the last component of path and appends
1097 * filename, unless the latter is absolute anyway or the
1100 if (path_is_absolute(filename))
1101 return strdup(filename);
1103 if (!(e = strrchr(path, '/')))
1104 return strdup(filename);
1106 k = strlen(filename);
1107 if (!(r = new(char, e-path+1+k+1)))
1110 memcpy(r, path, e-path+1);
1111 memcpy(r+(e-path)+1, filename, k+1);
1116 int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
1119 if (label_mkdir(path, mode) >= 0)
1120 if (chmod_and_chown(path, mode, uid, gid) < 0)
1123 if (lstat(path, &st) < 0)
1126 if ((st.st_mode & 0777) != mode ||
1129 !S_ISDIR(st.st_mode)) {
1138 int mkdir_parents(const char *path, mode_t mode) {
1143 /* Creates every parent directory in the path except the last
1146 p = path + strspn(path, "/");
1151 e = p + strcspn(p, "/");
1152 p = e + strspn(e, "/");
1154 /* Is this the last component? If so, then we're
1159 if (!(t = strndup(path, e - path)))
1162 r = label_mkdir(t, mode);
1165 if (r < 0 && errno != EEXIST)
1170 int mkdir_p(const char *path, mode_t mode) {
1175 if ((r = mkdir_parents(path, mode)) < 0)
1178 if (label_mkdir(path, mode) < 0 && errno != EEXIST)
1184 int rmdir_parents(const char *path, const char *stop) {
1193 /* Skip trailing slashes */
1194 while (l > 0 && path[l-1] == '/')
1200 /* Skip last component */
1201 while (l > 0 && path[l-1] != '/')
1204 /* Skip trailing slashes */
1205 while (l > 0 && path[l-1] == '/')
1211 if (!(t = strndup(path, l)))
1214 if (path_startswith(stop, t)) {
1223 if (errno != ENOENT)
1231 char hexchar(int x) {
1232 static const char table[16] = "0123456789abcdef";
1234 return table[x & 15];
1237 int unhexchar(char c) {
1239 if (c >= '0' && c <= '9')
1242 if (c >= 'a' && c <= 'f')
1243 return c - 'a' + 10;
1245 if (c >= 'A' && c <= 'F')
1246 return c - 'A' + 10;
1251 char octchar(int x) {
1252 return '0' + (x & 7);
1255 int unoctchar(char c) {
1257 if (c >= '0' && c <= '7')
1263 char decchar(int x) {
1264 return '0' + (x % 10);
1267 int undecchar(char c) {
1269 if (c >= '0' && c <= '9')
1275 char *cescape(const char *s) {
1281 /* Does C style string escaping. */
1283 if (!(r = new(char, strlen(s)*4 + 1)))
1286 for (f = s, t = r; *f; f++)
1332 /* For special chars we prefer octal over
1333 * hexadecimal encoding, simply because glib's
1334 * g_strescape() does the same */
1335 if ((*f < ' ') || (*f >= 127)) {
1337 *(t++) = octchar((unsigned char) *f >> 6);
1338 *(t++) = octchar((unsigned char) *f >> 3);
1339 *(t++) = octchar((unsigned char) *f);
1350 char *cunescape_length(const char *s, size_t length) {
1356 /* Undoes C style string escaping */
1358 if (!(r = new(char, length+1)))
1361 for (f = s, t = r; f < s + length; f++) {
1404 /* This is an extension of the XDG syntax files */
1409 /* hexadecimal encoding */
1412 if ((a = unhexchar(f[1])) < 0 ||
1413 (b = unhexchar(f[2])) < 0) {
1414 /* Invalid escape code, let's take it literal then */
1418 *(t++) = (char) ((a << 4) | b);
1433 /* octal encoding */
1436 if ((a = unoctchar(f[0])) < 0 ||
1437 (b = unoctchar(f[1])) < 0 ||
1438 (c = unoctchar(f[2])) < 0) {
1439 /* Invalid escape code, let's take it literal then */
1443 *(t++) = (char) ((a << 6) | (b << 3) | c);
1451 /* premature end of string.*/
1456 /* Invalid escape code, let's take it literal then */
1468 char *cunescape(const char *s) {
1469 return cunescape_length(s, strlen(s));
1472 char *xescape(const char *s, const char *bad) {
1476 /* Escapes all chars in bad, in addition to \ and all special
1477 * chars, in \xFF style escaping. May be reversed with
1480 if (!(r = new(char, strlen(s)*4+1)))
1483 for (f = s, t = r; *f; f++) {
1485 if ((*f < ' ') || (*f >= 127) ||
1486 (*f == '\\') || strchr(bad, *f)) {
1489 *(t++) = hexchar(*f >> 4);
1490 *(t++) = hexchar(*f);
1500 char *bus_path_escape(const char *s) {
1506 /* Escapes all chars that D-Bus' object path cannot deal
1507 * with. Can be reverse with bus_path_unescape() */
1509 if (!(r = new(char, strlen(s)*3+1)))
1512 for (f = s, t = r; *f; f++) {
1514 if (!(*f >= 'A' && *f <= 'Z') &&
1515 !(*f >= 'a' && *f <= 'z') &&
1516 !(*f >= '0' && *f <= '9')) {
1518 *(t++) = hexchar(*f >> 4);
1519 *(t++) = hexchar(*f);
1529 char *bus_path_unescape(const char *f) {
1534 if (!(r = strdup(f)))
1537 for (t = r; *f; f++) {
1542 if ((a = unhexchar(f[1])) < 0 ||
1543 (b = unhexchar(f[2])) < 0) {
1544 /* Invalid escape code, let's take it literal then */
1547 *(t++) = (char) ((a << 4) | b);
1559 char *path_kill_slashes(char *path) {
1563 /* Removes redundant inner and trailing slashes. Modifies the
1564 * passed string in-place.
1566 * ///foo///bar/ becomes /foo/bar
1569 for (f = path, t = path; *f; f++) {
1584 /* Special rule, if we are talking of the root directory, a
1585 trailing slash is good */
1587 if (t == path && slash)
1594 bool path_startswith(const char *path, const char *prefix) {
1598 if ((path[0] == '/') != (prefix[0] == '/'))
1604 path += strspn(path, "/");
1605 prefix += strspn(prefix, "/");
1613 a = strcspn(path, "/");
1614 b = strcspn(prefix, "/");
1619 if (memcmp(path, prefix, a) != 0)
1627 bool path_equal(const char *a, const char *b) {
1631 if ((a[0] == '/') != (b[0] == '/'))
1637 a += strspn(a, "/");
1638 b += strspn(b, "/");
1640 if (*a == 0 && *b == 0)
1643 if (*a == 0 || *b == 0)
1646 j = strcspn(a, "/");
1647 k = strcspn(b, "/");
1652 if (memcmp(a, b, j) != 0)
1660 char *ascii_strlower(char *t) {
1665 for (p = t; *p; p++)
1666 if (*p >= 'A' && *p <= 'Z')
1667 *p = *p - 'A' + 'a';
1672 bool ignore_file(const char *filename) {
1676 filename[0] == '.' ||
1677 streq(filename, "lost+found") ||
1678 endswith(filename, "~") ||
1679 endswith(filename, ".rpmnew") ||
1680 endswith(filename, ".rpmsave") ||
1681 endswith(filename, ".rpmorig") ||
1682 endswith(filename, ".dpkg-old") ||
1683 endswith(filename, ".dpkg-new") ||
1684 endswith(filename, ".swp");
1687 int fd_nonblock(int fd, bool nonblock) {
1692 if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
1696 flags |= O_NONBLOCK;
1698 flags &= ~O_NONBLOCK;
1700 if (fcntl(fd, F_SETFL, flags) < 0)
1706 int fd_cloexec(int fd, bool cloexec) {
1711 if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
1715 flags |= FD_CLOEXEC;
1717 flags &= ~FD_CLOEXEC;
1719 if (fcntl(fd, F_SETFD, flags) < 0)
1725 int close_all_fds(const int except[], unsigned n_except) {
1730 if (!(d = opendir("/proc/self/fd")))
1733 while ((de = readdir(d))) {
1736 if (ignore_file(de->d_name))
1739 if ((r = safe_atoi(de->d_name, &fd)) < 0)
1753 for (i = 0; i < n_except; i++)
1754 if (except[i] == fd) {
1763 if ((r = close_nointr(fd)) < 0) {
1764 /* Valgrind has its own FD and doesn't want to have it closed */
1777 bool chars_intersect(const char *a, const char *b) {
1780 /* Returns true if any of the chars in a are in b. */
1781 for (p = a; *p; p++)
1788 char *format_timestamp(char *buf, size_t l, usec_t t) {
1798 sec = (time_t) (t / USEC_PER_SEC);
1800 if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0)
1806 char *format_timestamp_pretty(char *buf, size_t l, usec_t t) {
1809 n = now(CLOCK_REALTIME);
1811 if (t <= 0 || t > n || t + USEC_PER_DAY*7 <= t)
1816 if (d >= USEC_PER_YEAR)
1817 snprintf(buf, l, "%llu years and %llu months ago",
1818 (unsigned long long) (d / USEC_PER_YEAR),
1819 (unsigned long long) ((d % USEC_PER_YEAR) / USEC_PER_MONTH));
1820 else if (d >= USEC_PER_MONTH)
1821 snprintf(buf, l, "%llu months and %llu days ago",
1822 (unsigned long long) (d / USEC_PER_MONTH),
1823 (unsigned long long) ((d % USEC_PER_MONTH) / USEC_PER_DAY));
1824 else if (d >= USEC_PER_WEEK)
1825 snprintf(buf, l, "%llu weeks and %llu days ago",
1826 (unsigned long long) (d / USEC_PER_WEEK),
1827 (unsigned long long) ((d % USEC_PER_WEEK) / USEC_PER_DAY));
1828 else if (d >= 2*USEC_PER_DAY)
1829 snprintf(buf, l, "%llu days ago", (unsigned long long) (d / USEC_PER_DAY));
1830 else if (d >= 25*USEC_PER_HOUR)
1831 snprintf(buf, l, "1 day and %lluh ago",
1832 (unsigned long long) ((d - USEC_PER_DAY) / USEC_PER_HOUR));
1833 else if (d >= 6*USEC_PER_HOUR)
1834 snprintf(buf, l, "%lluh ago",
1835 (unsigned long long) (d / USEC_PER_HOUR));
1836 else if (d >= USEC_PER_HOUR)
1837 snprintf(buf, l, "%lluh %llumin ago",
1838 (unsigned long long) (d / USEC_PER_HOUR),
1839 (unsigned long long) ((d % USEC_PER_HOUR) / USEC_PER_MINUTE));
1840 else if (d >= 5*USEC_PER_MINUTE)
1841 snprintf(buf, l, "%llumin ago",
1842 (unsigned long long) (d / USEC_PER_MINUTE));
1843 else if (d >= USEC_PER_MINUTE)
1844 snprintf(buf, l, "%llumin %llus ago",
1845 (unsigned long long) (d / USEC_PER_MINUTE),
1846 (unsigned long long) ((d % USEC_PER_MINUTE) / USEC_PER_SEC));
1847 else if (d >= USEC_PER_SEC)
1848 snprintf(buf, l, "%llus ago",
1849 (unsigned long long) (d / USEC_PER_SEC));
1850 else if (d >= USEC_PER_MSEC)
1851 snprintf(buf, l, "%llums ago",
1852 (unsigned long long) (d / USEC_PER_MSEC));
1854 snprintf(buf, l, "%lluus ago",
1855 (unsigned long long) d);
1857 snprintf(buf, l, "now");
1863 char *format_timespan(char *buf, size_t l, usec_t t) {
1864 static const struct {
1868 { "w", USEC_PER_WEEK },
1869 { "d", USEC_PER_DAY },
1870 { "h", USEC_PER_HOUR },
1871 { "min", USEC_PER_MINUTE },
1872 { "s", USEC_PER_SEC },
1873 { "ms", USEC_PER_MSEC },
1883 if (t == (usec_t) -1)
1887 snprintf(p, l, "0");
1892 /* The result of this function can be parsed with parse_usec */
1894 for (i = 0; i < ELEMENTSOF(table); i++) {
1898 if (t < table[i].usec)
1904 k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix);
1905 n = MIN((size_t) k, l);
1918 bool fstype_is_network(const char *fstype) {
1919 static const char * const table[] = {
1931 for (i = 0; i < ELEMENTSOF(table); i++)
1932 if (streq(table[i], fstype))
1941 if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
1946 TIOCL_GETKMSGREDIRECT,
1950 if (ioctl(fd, TIOCLINUX, tiocl) < 0)
1953 vt = tiocl[0] <= 0 ? 1 : tiocl[0];
1956 if (ioctl(fd, VT_ACTIVATE, vt) < 0)
1959 close_nointr_nofail(r);
1963 int read_one_char(FILE *f, char *ret, bool *need_nl) {
1964 struct termios old_termios, new_termios;
1971 if (tcgetattr(fileno(f), &old_termios) >= 0) {
1972 new_termios = old_termios;
1974 new_termios.c_lflag &= ~ICANON;
1975 new_termios.c_cc[VMIN] = 1;
1976 new_termios.c_cc[VTIME] = 0;
1978 if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
1981 k = fread(&c, 1, 1, f);
1983 tcsetattr(fileno(f), TCSADRAIN, &old_termios);
1989 *need_nl = c != '\n';
1996 if (!(fgets(line, sizeof(line), f)))
2001 if (strlen(line) != 1)
2011 int ask(char *ret, const char *replies, const char *text, ...) {
2018 on_tty = isatty(STDOUT_FILENO);
2024 bool need_nl = true;
2027 fputs("\x1B[1m", stdout);
2034 fputs("\x1B[0m", stdout);
2038 if ((r = read_one_char(stdin, &c, &need_nl)) < 0) {
2040 if (r == -EBADMSG) {
2041 puts("Bad input, please try again.");
2052 if (strchr(replies, c)) {
2057 puts("Read unexpected character, please try again.");
2061 int reset_terminal(int fd) {
2062 struct termios termios;
2066 /* Set terminal to some sane defaults */
2070 /* We leave locked terminal attributes untouched, so that
2071 * Plymouth may set whatever it wants to set, and we don't
2072 * interfere with that. */
2074 /* Disable exclusive mode, just in case */
2075 ioctl(fd, TIOCNXCL);
2077 /* Enable console unicode mode */
2079 ioctl(fd, KDSKBMODE, &arg);
2081 if (tcgetattr(fd, &termios) < 0) {
2086 /* We only reset the stuff that matters to the software. How
2087 * hardware is set up we don't touch assuming that somebody
2088 * else will do that for us */
2090 termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
2091 termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
2092 termios.c_oflag |= ONLCR;
2093 termios.c_cflag |= CREAD;
2094 termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
2096 termios.c_cc[VINTR] = 03; /* ^C */
2097 termios.c_cc[VQUIT] = 034; /* ^\ */
2098 termios.c_cc[VERASE] = 0177;
2099 termios.c_cc[VKILL] = 025; /* ^X */
2100 termios.c_cc[VEOF] = 04; /* ^D */
2101 termios.c_cc[VSTART] = 021; /* ^Q */
2102 termios.c_cc[VSTOP] = 023; /* ^S */
2103 termios.c_cc[VSUSP] = 032; /* ^Z */
2104 termios.c_cc[VLNEXT] = 026; /* ^V */
2105 termios.c_cc[VWERASE] = 027; /* ^W */
2106 termios.c_cc[VREPRINT] = 022; /* ^R */
2107 termios.c_cc[VEOL] = 0;
2108 termios.c_cc[VEOL2] = 0;
2110 termios.c_cc[VTIME] = 0;
2111 termios.c_cc[VMIN] = 1;
2113 if (tcsetattr(fd, TCSANOW, &termios) < 0)
2117 /* Just in case, flush all crap out */
2118 tcflush(fd, TCIOFLUSH);
2123 int open_terminal(const char *name, int mode) {
2126 if ((fd = open(name, mode)) < 0)
2129 if ((r = isatty(fd)) < 0) {
2130 close_nointr_nofail(fd);
2135 close_nointr_nofail(fd);
2142 int flush_fd(int fd) {
2143 struct pollfd pollfd;
2147 pollfd.events = POLLIN;
2154 if ((r = poll(&pollfd, 1, 0)) < 0) {
2165 if ((l = read(fd, buf, sizeof(buf))) < 0) {
2170 if (errno == EAGAIN)
2181 int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) {
2182 int fd = -1, notify = -1, r, wd = -1;
2186 /* We use inotify to be notified when the tty is closed. We
2187 * create the watch before checking if we can actually acquire
2188 * it, so that we don't lose any event.
2190 * Note: strictly speaking this actually watches for the
2191 * device being closed, it does *not* really watch whether a
2192 * tty loses its controlling process. However, unless some
2193 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
2194 * its tty otherwise this will not become a problem. As long
2195 * as the administrator makes sure not configure any service
2196 * on the same tty as an untrusted user this should not be a
2197 * problem. (Which he probably should not do anyway.) */
2199 if (!fail && !force) {
2200 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
2205 if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) {
2213 if ((r = flush_fd(notify)) < 0)
2216 /* We pass here O_NOCTTY only so that we can check the return
2217 * value TIOCSCTTY and have a reliable way to figure out if we
2218 * successfully became the controlling process of the tty */
2219 if ((fd = open_terminal(name, O_RDWR|O_NOCTTY)) < 0)
2222 /* First, try to get the tty */
2223 r = ioctl(fd, TIOCSCTTY, force);
2225 /* Sometimes it makes sense to ignore TIOCSCTTY
2226 * returning EPERM, i.e. when very likely we already
2227 * are have this controlling terminal. */
2228 if (r < 0 && errno == EPERM && ignore_tiocstty_eperm)
2231 if (r < 0 && (force || fail || errno != EPERM)) {
2241 assert(notify >= 0);
2244 struct inotify_event e;
2247 if ((l = read(notify, &e, sizeof(e))) != sizeof(e)) {
2261 if (e.wd != wd || !(e.mask & IN_CLOSE)) {
2269 /* We close the tty fd here since if the old session
2270 * ended our handle will be dead. It's important that
2271 * we do this after sleeping, so that we don't enter
2272 * an endless loop. */
2273 close_nointr_nofail(fd);
2277 close_nointr_nofail(notify);
2279 if ((r = reset_terminal(fd)) < 0)
2280 log_warning("Failed to reset terminal: %s", strerror(-r));
2286 close_nointr_nofail(fd);
2289 close_nointr_nofail(notify);
2294 int release_terminal(void) {
2296 struct sigaction sa_old, sa_new;
2298 if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
2301 /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
2302 * by our own TIOCNOTTY */
2305 sa_new.sa_handler = SIG_IGN;
2306 sa_new.sa_flags = SA_RESTART;
2307 assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
2309 if (ioctl(fd, TIOCNOTTY) < 0)
2312 assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
2314 close_nointr_nofail(fd);
2318 int sigaction_many(const struct sigaction *sa, ...) {
2323 while ((sig = va_arg(ap, int)) > 0)
2324 if (sigaction(sig, sa, NULL) < 0)
2331 int ignore_signals(int sig, ...) {
2332 struct sigaction sa;
2337 sa.sa_handler = SIG_IGN;
2338 sa.sa_flags = SA_RESTART;
2340 if (sigaction(sig, &sa, NULL) < 0)
2344 while ((sig = va_arg(ap, int)) > 0)
2345 if (sigaction(sig, &sa, NULL) < 0)
2352 int default_signals(int sig, ...) {
2353 struct sigaction sa;
2358 sa.sa_handler = SIG_DFL;
2359 sa.sa_flags = SA_RESTART;
2361 if (sigaction(sig, &sa, NULL) < 0)
2365 while ((sig = va_arg(ap, int)) > 0)
2366 if (sigaction(sig, &sa, NULL) < 0)
2373 int close_pipe(int p[]) {
2379 a = close_nointr(p[0]);
2384 b = close_nointr(p[1]);
2388 return a < 0 ? a : b;
2391 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
2400 while (nbytes > 0) {
2403 if ((k = read(fd, p, nbytes)) <= 0) {
2405 if (k < 0 && errno == EINTR)
2408 if (k < 0 && errno == EAGAIN && do_poll) {
2409 struct pollfd pollfd;
2413 pollfd.events = POLLIN;
2415 if (poll(&pollfd, 1, -1) < 0) {
2419 return n > 0 ? n : -errno;
2422 if (pollfd.revents != POLLIN)
2423 return n > 0 ? n : -EIO;
2428 return n > 0 ? n : (k < 0 ? -errno : 0);
2439 ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
2448 while (nbytes > 0) {
2451 if ((k = write(fd, p, nbytes)) <= 0) {
2453 if (k < 0 && errno == EINTR)
2456 if (k < 0 && errno == EAGAIN && do_poll) {
2457 struct pollfd pollfd;
2461 pollfd.events = POLLOUT;
2463 if (poll(&pollfd, 1, -1) < 0) {
2467 return n > 0 ? n : -errno;
2470 if (pollfd.revents != POLLOUT)
2471 return n > 0 ? n : -EIO;
2476 return n > 0 ? n : (k < 0 ? -errno : 0);
2487 int path_is_mount_point(const char *t) {
2492 if (lstat(t, &a) < 0) {
2493 if (errno == ENOENT)
2499 if ((r = parent_of_path(t, &parent)) < 0)
2502 r = lstat(parent, &b);
2508 return a.st_dev != b.st_dev;
2511 int parse_usec(const char *t, usec_t *usec) {
2512 static const struct {
2516 { "sec", USEC_PER_SEC },
2517 { "s", USEC_PER_SEC },
2518 { "min", USEC_PER_MINUTE },
2519 { "hr", USEC_PER_HOUR },
2520 { "h", USEC_PER_HOUR },
2521 { "d", USEC_PER_DAY },
2522 { "w", USEC_PER_WEEK },
2523 { "msec", USEC_PER_MSEC },
2524 { "ms", USEC_PER_MSEC },
2525 { "m", USEC_PER_MINUTE },
2528 { "", USEC_PER_SEC },
2544 l = strtoll(p, &e, 10);
2555 e += strspn(e, WHITESPACE);
2557 for (i = 0; i < ELEMENTSOF(table); i++)
2558 if (startswith(e, table[i].suffix)) {
2559 r += (usec_t) l * table[i].usec;
2560 p = e + strlen(table[i].suffix);
2564 if (i >= ELEMENTSOF(table))
2574 int make_stdio(int fd) {
2579 r = dup2(fd, STDIN_FILENO);
2580 s = dup2(fd, STDOUT_FILENO);
2581 t = dup2(fd, STDERR_FILENO);
2584 close_nointr_nofail(fd);
2586 if (r < 0 || s < 0 || t < 0)
2592 bool is_clean_exit(int code, int status) {
2594 if (code == CLD_EXITED)
2597 /* If a daemon does not implement handlers for some of the
2598 * signals that's not considered an unclean shutdown */
2599 if (code == CLD_KILLED)
2603 status == SIGTERM ||
2609 bool is_clean_exit_lsb(int code, int status) {
2611 if (is_clean_exit(code, status))
2615 code == CLD_EXITED &&
2616 (status == EXIT_NOTINSTALLED || status == EXIT_NOTCONFIGURED);
2619 bool is_device_path(const char *path) {
2621 /* Returns true on paths that refer to a device, either in
2622 * sysfs or in /dev */
2625 path_startswith(path, "/dev/") ||
2626 path_startswith(path, "/sys/");
2629 int dir_is_empty(const char *path) {
2632 struct dirent buf, *de;
2634 if (!(d = opendir(path)))
2638 if ((r = readdir_r(d, &buf, &de)) > 0) {
2648 if (!ignore_file(de->d_name)) {
2658 unsigned long long random_ull(void) {
2663 if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0)
2666 r = loop_read(fd, &ull, sizeof(ull), true);
2667 close_nointr_nofail(fd);
2669 if (r != sizeof(ull))
2675 return random() * RAND_MAX + random();
2678 void rename_process(const char name[8]) {
2681 prctl(PR_SET_NAME, name);
2683 /* This is a like a poor man's setproctitle(). The string
2684 * passed should fit in 7 chars (i.e. the length of
2687 if (program_invocation_name)
2688 strncpy(program_invocation_name, name, strlen(program_invocation_name));
2691 void sigset_add_many(sigset_t *ss, ...) {
2698 while ((sig = va_arg(ap, int)) > 0)
2699 assert_se(sigaddset(ss, sig) == 0);
2703 char* gethostname_malloc(void) {
2706 assert_se(uname(&u) >= 0);
2709 return strdup(u.nodename);
2711 return strdup(u.sysname);
2714 char* getlogname_malloc(void) {
2718 struct passwd pwbuf, *pw = NULL;
2721 if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
2726 /* Shortcut things to avoid NSS lookups */
2728 return strdup("root");
2730 if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0)
2733 if (!(buf = malloc(bufsize)))
2736 if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) {
2737 name = strdup(pw->pw_name);
2744 if (asprintf(&name, "%lu", (unsigned long) uid) < 0)
2750 int getttyname_malloc(char **r) {
2751 char path[PATH_MAX], *p, *c;
2756 if ((k = ttyname_r(STDIN_FILENO, path, sizeof(path))) != 0)
2762 if (startswith(path, "/dev/"))
2765 if (!(c = strdup(p)))
2772 static int rm_rf_children(int fd, bool only_dirs) {
2778 /* This returns the first error we run into, but nevertheless
2781 if (!(d = fdopendir(fd))) {
2782 close_nointr_nofail(fd);
2784 return errno == ENOENT ? 0 : -errno;
2788 struct dirent buf, *de;
2792 if ((r = readdir_r(d, &buf, &de)) != 0) {
2801 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
2804 if (de->d_type == DT_UNKNOWN) {
2807 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
2808 if (ret == 0 && errno != ENOENT)
2813 is_dir = S_ISDIR(st.st_mode);
2815 is_dir = de->d_type == DT_DIR;
2820 if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
2821 if (ret == 0 && errno != ENOENT)
2826 if ((r = rm_rf_children(subdir_fd, only_dirs)) < 0) {
2831 if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
2832 if (ret == 0 && errno != ENOENT)
2835 } else if (!only_dirs) {
2837 if (unlinkat(fd, de->d_name, 0) < 0) {
2838 if (ret == 0 && errno != ENOENT)
2849 int rm_rf(const char *path, bool only_dirs, bool delete_root) {
2855 if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
2857 if (errno != ENOTDIR)
2860 if (delete_root && !only_dirs)
2861 if (unlink(path) < 0)
2867 r = rm_rf_children(fd, only_dirs);
2870 if (rmdir(path) < 0) {
2878 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
2881 /* Under the assumption that we are running privileged we
2882 * first change the access mode and only then hand out
2883 * ownership to avoid a window where access is too open. */
2885 if (chmod(path, mode) < 0)
2888 if (chown(path, uid, gid) < 0)
2894 cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
2898 /* Allocates the cpuset in the right size */
2901 if (!(r = CPU_ALLOC(n)))
2904 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) {
2905 CPU_ZERO_S(CPU_ALLOC_SIZE(n), r);
2915 if (errno != EINVAL)
2922 void status_vprintf(const char *format, va_list ap) {
2928 /* This independent of logging, as status messages are
2929 * optional and go exclusively to the console. */
2931 if (vasprintf(&s, format, ap) < 0)
2934 if ((fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
2937 write(fd, s, strlen(s));
2943 close_nointr_nofail(fd);
2946 void status_printf(const char *format, ...) {
2951 va_start(ap, format);
2952 status_vprintf(format, ap);
2956 void status_welcome(void) {
2958 #if defined(TARGET_FEDORA)
2961 if (read_one_line_file("/etc/system-release", &r) < 0)
2966 /* This tries to mimic the color magic the old Red Hat sysinit
2969 if (startswith(r, "Red Hat"))
2970 status_printf("Welcome to \x1B[0;31m%s\x1B[0m!\n", r); /* Red for RHEL */
2971 else if (startswith(r, "Fedora"))
2972 status_printf("Welcome to \x1B[0;34m%s\x1B[0m!\n", r); /* Blue for Fedora */
2974 status_printf("Welcome to %s!\n", r);
2978 #elif defined(TARGET_SUSE)
2981 if (read_one_line_file("/etc/SuSE-release", &r) < 0)
2986 status_printf("Welcome to \x1B[0;32m%s\x1B[0m!\n", r); /* Green for SUSE */
2989 #elif defined(TARGET_GENTOO)
2992 if (read_one_line_file("/etc/gentoo-release", &r) < 0)
2997 status_printf("Welcome to \x1B[1;34m%s\x1B[0m!\n", r); /* Light Blue for Gentoo */
3001 #elif defined(TARGET_DEBIAN)
3004 if (read_one_line_file("/etc/debian_version", &r) < 0)
3009 status_printf("Welcome to Debian \x1B[1;31m%s\x1B[0m!\n", r); /* Light Red for Debian */
3012 #elif defined(TARGET_ARCH)
3013 status_printf("Welcome to \x1B[1;36mArch Linux\x1B[0m!\n"); /* Cyan for Arch */
3015 #warning "You probably should add a welcome text logic here."
3019 char *replace_env(const char *format, char **env) {
3026 const char *e, *word = format;
3031 for (e = format; *e; e ++) {
3042 if (!(k = strnappend(r, word, e-word-1)))
3051 } else if (*e == '$') {
3052 if (!(k = strnappend(r, word, e-word)))
3068 if (!(t = strv_env_get_with_length(env, word+2, e-word-2)))
3071 if (!(k = strappend(r, t)))
3084 if (!(k = strnappend(r, word, e-word)))
3095 char **replace_env_argv(char **argv, char **env) {
3097 unsigned k = 0, l = 0;
3099 l = strv_length(argv);
3101 if (!(r = new(char*, l+1)))
3104 STRV_FOREACH(i, argv) {
3106 /* If $FOO appears as single word, replace it by the split up variable */
3107 if ((*i)[0] == '$' && (*i)[1] != '{') {
3112 if ((e = strv_env_get(env, *i+1))) {
3114 if (!(m = strv_split_quoted(e))) {
3125 if (!(w = realloc(r, sizeof(char*) * (l+1)))) {
3134 memcpy(r + k, m, q * sizeof(char*));
3142 /* If ${FOO} appears as part of a word, replace it by the variable as-is */
3143 if (!(r[k++] = replace_env(*i, env))) {
3154 static __thread int parsed_columns = 0;
3157 if (parsed_columns > 0)
3158 return parsed_columns;
3160 if ((e = getenv("COLUMNS")))
3161 parsed_columns = atoi(e);
3163 if (parsed_columns <= 0) {
3167 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
3168 parsed_columns = ws.ws_col;
3171 if (parsed_columns <= 0)
3172 parsed_columns = 80;
3174 return parsed_columns;
3177 int running_in_chroot(void) {
3183 /* Only works as root */
3185 if (stat("/proc/1/root", &a) < 0)
3188 if (stat("/", &b) < 0)
3192 a.st_dev != b.st_dev ||
3193 a.st_ino != b.st_ino;
3196 char *ellipsize(const char *s, unsigned length, unsigned percent) {
3201 assert(percent <= 100);
3202 assert(length >= 3);
3206 if (l <= 3 || l <= length)
3209 if (!(r = new0(char, length+1)))
3212 x = (length * percent) / 100;
3222 s + l - (length - x - 3),
3228 int touch(const char *path) {
3233 if ((fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0666)) < 0)
3236 close_nointr_nofail(fd);
3240 char *unquote(const char *s, const char* quotes) {
3244 if ((l = strlen(s)) < 2)
3247 if (strchr(quotes, s[0]) && s[l-1] == s[0])
3248 return strndup(s+1, l-2);
3253 int wait_for_terminate(pid_t pid, siginfo_t *status) {
3260 if (waitid(P_PID, pid, status, WEXITED) < 0) {
3272 int wait_for_terminate_and_warn(const char *name, pid_t pid) {
3279 if ((r = wait_for_terminate(pid, &status)) < 0) {
3280 log_warning("Failed to wait for %s: %s", name, strerror(-r));
3284 if (status.si_code == CLD_EXITED) {
3285 if (status.si_status != 0) {
3286 log_warning("%s failed with error code %i.", name, status.si_status);
3290 log_debug("%s succeeded.", name);
3293 } else if (status.si_code == CLD_KILLED ||
3294 status.si_code == CLD_DUMPED) {
3296 log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
3300 log_warning("%s failed due to unknown reason.", name);
3305 static const char *const ioprio_class_table[] = {
3306 [IOPRIO_CLASS_NONE] = "none",
3307 [IOPRIO_CLASS_RT] = "realtime",
3308 [IOPRIO_CLASS_BE] = "best-effort",
3309 [IOPRIO_CLASS_IDLE] = "idle"
3312 DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
3314 static const char *const sigchld_code_table[] = {
3315 [CLD_EXITED] = "exited",
3316 [CLD_KILLED] = "killed",
3317 [CLD_DUMPED] = "dumped",
3318 [CLD_TRAPPED] = "trapped",
3319 [CLD_STOPPED] = "stopped",
3320 [CLD_CONTINUED] = "continued",
3323 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
3325 static const char *const log_facility_table[LOG_NFACILITIES] = {
3326 [LOG_FAC(LOG_KERN)] = "kern",
3327 [LOG_FAC(LOG_USER)] = "user",
3328 [LOG_FAC(LOG_MAIL)] = "mail",
3329 [LOG_FAC(LOG_DAEMON)] = "daemon",
3330 [LOG_FAC(LOG_AUTH)] = "auth",
3331 [LOG_FAC(LOG_SYSLOG)] = "syslog",
3332 [LOG_FAC(LOG_LPR)] = "lpr",
3333 [LOG_FAC(LOG_NEWS)] = "news",
3334 [LOG_FAC(LOG_UUCP)] = "uucp",
3335 [LOG_FAC(LOG_CRON)] = "cron",
3336 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
3337 [LOG_FAC(LOG_FTP)] = "ftp",
3338 [LOG_FAC(LOG_LOCAL0)] = "local0",
3339 [LOG_FAC(LOG_LOCAL1)] = "local1",
3340 [LOG_FAC(LOG_LOCAL2)] = "local2",
3341 [LOG_FAC(LOG_LOCAL3)] = "local3",
3342 [LOG_FAC(LOG_LOCAL4)] = "local4",
3343 [LOG_FAC(LOG_LOCAL5)] = "local5",
3344 [LOG_FAC(LOG_LOCAL6)] = "local6",
3345 [LOG_FAC(LOG_LOCAL7)] = "local7"
3348 DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
3350 static const char *const log_level_table[] = {
3351 [LOG_EMERG] = "emerg",
3352 [LOG_ALERT] = "alert",
3353 [LOG_CRIT] = "crit",
3355 [LOG_WARNING] = "warning",
3356 [LOG_NOTICE] = "notice",
3357 [LOG_INFO] = "info",
3358 [LOG_DEBUG] = "debug"
3361 DEFINE_STRING_TABLE_LOOKUP(log_level, int);
3363 static const char* const sched_policy_table[] = {
3364 [SCHED_OTHER] = "other",
3365 [SCHED_BATCH] = "batch",
3366 [SCHED_IDLE] = "idle",
3367 [SCHED_FIFO] = "fifo",
3371 DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
3373 static const char* const rlimit_table[] = {
3374 [RLIMIT_CPU] = "LimitCPU",
3375 [RLIMIT_FSIZE] = "LimitFSIZE",
3376 [RLIMIT_DATA] = "LimitDATA",
3377 [RLIMIT_STACK] = "LimitSTACK",
3378 [RLIMIT_CORE] = "LimitCORE",
3379 [RLIMIT_RSS] = "LimitRSS",
3380 [RLIMIT_NOFILE] = "LimitNOFILE",
3381 [RLIMIT_AS] = "LimitAS",
3382 [RLIMIT_NPROC] = "LimitNPROC",
3383 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
3384 [RLIMIT_LOCKS] = "LimitLOCKS",
3385 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
3386 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
3387 [RLIMIT_NICE] = "LimitNICE",
3388 [RLIMIT_RTPRIO] = "LimitRTPRIO",
3389 [RLIMIT_RTTIME] = "LimitRTTIME"
3392 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
3394 static const char* const ip_tos_table[] = {
3395 [IPTOS_LOWDELAY] = "low-delay",
3396 [IPTOS_THROUGHPUT] = "throughput",
3397 [IPTOS_RELIABILITY] = "reliability",
3398 [IPTOS_LOWCOST] = "low-cost",
3401 DEFINE_STRING_TABLE_LOOKUP(ip_tos, int);
3403 static const char *const signal_table[] = {
3420 [SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */
3431 [SIGVTALRM] = "VTALRM",
3433 [SIGWINCH] = "WINCH",
3439 DEFINE_STRING_TABLE_LOOKUP(signal, int);