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>
46 #include <sys/prctl.h>
47 #include <sys/utsname.h>
49 #include <netinet/ip.h>
60 #include <selinux/selinux.h>
61 #include <selinux/label.h>
63 static struct selabel_handle *label_hnd = NULL;
65 static inline int use_selinux(void) {
66 static int use_selinux_ind = -1;
68 if (use_selinux_ind == -1)
69 use_selinux_ind = (is_selinux_enabled() == 1);
71 return use_selinux_ind;
74 static int label_get_file_label_from_path(
78 security_context_t *fcon) {
80 security_context_t dir_con = NULL;
81 security_class_t sclass;
84 r = getfilecon(path, &dir_con);
87 if ((sclass = string_to_security_class(class)) != 0)
88 r = security_compute_create((security_context_t) label, dir_con, sclass, fcon);
99 int label_init(void) {
107 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
109 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
110 "Failed to initialize SELinux context: %m");
111 r = (security_getenforce() == 1) ? -errno : 0;
118 int label_fix(const char *path) {
123 security_context_t fcon;
128 r = lstat(path, &st);
130 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
133 r = setfilecon(path, fcon);
138 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
139 "Unable to fix label of %s: %m", path);
140 r = (security_getenforce() == 1) ? -errno : 0;
147 void label_finish(void) {
151 selabel_close(label_hnd);
155 int label_get_socket_label_from_exe(const char *exe, char **label) {
160 security_context_t mycon = NULL, fcon = NULL;
161 security_class_t sclass;
163 if (!use_selinux()) {
172 r = getfilecon(exe, &fcon);
176 sclass = string_to_security_class("process");
177 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
179 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
182 if (r < 0 && security_getenforce() == 1)
192 int label_fifofile_set(const char *label, const char *path) {
196 security_context_t filecon = NULL;
198 if (!use_selinux() || !label)
201 if (((r = label_get_file_label_from_path(label, path, "fifo_file", &filecon)) == 0)) {
202 if ((r = setfscreatecon(filecon)) < 0) {
203 log_error("Failed to set SELinux file context (%s) on %s: %m", label, path);
210 if (r < 0 && security_getenforce() == 0)
217 int label_socket_set(const char *label) {
223 if (setsockcreatecon((security_context_t) label) < 0) {
224 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
225 "Failed to set SELinux context (%s) on socket: %m", label);
227 if (security_getenforce() == 1)
235 void label_file_clear(void) {
241 setfscreatecon(NULL);
245 void label_socket_clear(void) {
251 setsockcreatecon(NULL);
255 void label_free(const char *label) {
261 freecon((security_context_t) label);
265 static int label_mkdir(
271 security_context_t fcon = NULL;
274 if (path[0] == '/') {
275 r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
279 char *newpath = NULL;
280 cwd = getcwd(NULL,0);
281 if ((! cwd) || (asprintf(&newpath, "%s/%s",cwd,path) < 0)) {
285 r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
291 r = setfscreatecon(fcon);
293 if ((r < 0) && (errno != ENOENT)) {
294 log_error("Failed to set security context %s for %s", fcon, path);
296 if (security_getenforce() == 1)
300 r = mkdir(path, mode);
304 setfscreatecon(NULL);
310 return mkdir(path, mode);
314 bool streq_ptr(const char *a, const char *b) {
316 /* Like streq(), but tries to make sense of NULL pointers */
327 usec_t now(clockid_t clock_id) {
330 assert_se(clock_gettime(clock_id, &ts) == 0);
332 return timespec_load(&ts);
335 dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
338 ts->realtime = now(CLOCK_REALTIME);
339 ts->monotonic = now(CLOCK_MONOTONIC);
344 usec_t timespec_load(const struct timespec *ts) {
348 (usec_t) ts->tv_sec * USEC_PER_SEC +
349 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
352 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
355 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
356 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
361 usec_t timeval_load(const struct timeval *tv) {
365 (usec_t) tv->tv_sec * USEC_PER_SEC +
366 (usec_t) tv->tv_usec;
369 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
372 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
373 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
378 bool endswith(const char *s, const char *postfix) {
385 pl = strlen(postfix);
393 return memcmp(s + sl - pl, postfix, pl) == 0;
396 bool startswith(const char *s, const char *prefix) {
411 return memcmp(s, prefix, pl) == 0;
414 bool startswith_no_case(const char *s, const char *prefix) {
430 for(i = 0; i < pl; ++i) {
431 if (tolower(s[i]) != tolower(prefix[i]))
438 bool first_word(const char *s, const char *word) {
453 if (memcmp(s, word, wl) != 0)
457 strchr(WHITESPACE, s[wl]);
460 int close_nointr(int fd) {
466 if ((r = close(fd)) >= 0)
474 void close_nointr_nofail(int fd) {
475 int saved_errno = errno;
477 /* like close_nointr() but cannot fail, and guarantees errno
480 assert_se(close_nointr(fd) == 0);
485 void close_many(const int fds[], unsigned n_fd) {
488 for (i = 0; i < n_fd; i++)
489 close_nointr_nofail(fds[i]);
492 int parse_boolean(const char *v) {
495 if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
497 else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
503 int parse_pid(const char *s, pid_t* ret_pid) {
511 if ((r = safe_atolu(s, &ul)) < 0)
516 if ((unsigned long) pid != ul)
526 int safe_atou(const char *s, unsigned *ret_u) {
534 l = strtoul(s, &x, 0);
536 if (!x || *x || errno)
537 return errno ? -errno : -EINVAL;
539 if ((unsigned long) (unsigned) l != l)
542 *ret_u = (unsigned) l;
546 int safe_atoi(const char *s, int *ret_i) {
554 l = strtol(s, &x, 0);
556 if (!x || *x || errno)
557 return errno ? -errno : -EINVAL;
559 if ((long) (int) l != l)
566 int safe_atollu(const char *s, long long unsigned *ret_llu) {
568 unsigned long long l;
574 l = strtoull(s, &x, 0);
576 if (!x || *x || errno)
577 return errno ? -errno : -EINVAL;
583 int safe_atolli(const char *s, long long int *ret_lli) {
591 l = strtoll(s, &x, 0);
593 if (!x || *x || errno)
594 return errno ? -errno : -EINVAL;
600 /* Split a string into words. */
601 char *split(const char *c, size_t *l, const char *separator, char **state) {
604 current = *state ? *state : (char*) c;
606 if (!*current || *c == 0)
609 current += strspn(current, separator);
610 *l = strcspn(current, separator);
613 return (char*) current;
616 /* Split a string into words, but consider strings enclosed in '' and
617 * "" as words even if they include spaces. */
618 char *split_quoted(const char *c, size_t *l, char **state) {
620 bool escaped = false;
622 current = *state ? *state : (char*) c;
624 if (!*current || *c == 0)
627 current += strspn(current, WHITESPACE);
629 if (*current == '\'') {
632 for (e = current; *e; e++) {
642 *state = *e == 0 ? e : e+1;
643 } else if (*current == '\"') {
646 for (e = current; *e; e++) {
656 *state = *e == 0 ? e : e+1;
658 for (e = current; *e; e++) {
663 else if (strchr(WHITESPACE, *e))
670 return (char*) current;
673 char **split_path_and_make_absolute(const char *p) {
677 if (!(l = strv_split(p, ":")))
680 if (!strv_path_make_absolute_cwd(l)) {
688 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
691 char fn[132], line[256], *p;
697 assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
698 fn[sizeof(fn)-1] = 0;
700 if (!(f = fopen(fn, "r")))
703 if (!(fgets(line, sizeof(line), f))) {
711 /* Let's skip the pid and comm fields. The latter is enclosed
712 * in () but does not escape any () in its value, so let's
713 * skip over it manually */
715 if (!(p = strrchr(line, ')')))
726 if ((long unsigned) (pid_t) ppid != ppid)
729 *_ppid = (pid_t) ppid;
734 int write_one_line_file(const char *fn, const char *line) {
741 if (!(f = fopen(fn, "we")))
744 if (fputs(line, f) < 0) {
755 int read_one_line_file(const char *fn, char **line) {
763 if (!(f = fopen(fn, "re")))
766 if (!(fgets(t, sizeof(t), f))) {
771 if (!(c = strdup(t))) {
784 char *truncate_nl(char *s) {
787 s[strcspn(s, NEWLINE)] = 0;
791 int get_process_name(pid_t pid, char **name) {
798 if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
801 r = read_one_line_file(p, name);
811 int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
819 assert(max_length > 0);
822 if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
831 if (!(r = new(char, max_length))) {
838 while ((c = getc(f)) != EOF) {
860 size_t n = MIN(left-1, 3U);
868 /* Kernel threads have no argv[] */
875 if ((h = get_process_name(pid, &t)) < 0)
878 h = asprintf(&r, "[%s]", t);
889 char *strnappend(const char *s, const char *suffix, size_t b) {
897 return strndup(suffix, b);
907 if (!(r = new(char, a+b+1)))
911 memcpy(r+a, suffix, b);
917 char *strappend(const char *s, const char *suffix) {
918 return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
921 int readlink_malloc(const char *p, char **r) {
931 if (!(c = new(char, l)))
934 if ((n = readlink(p, c, l-1)) < 0) {
940 if ((size_t) n < l-1) {
951 int readlink_and_make_absolute(const char *p, char **r) {
958 if ((j = readlink_malloc(p, &target)) < 0)
961 k = file_in_same_dir(p, target);
971 int parent_of_path(const char *path, char **_r) {
972 const char *e, *a = NULL, *b = NULL, *p;
982 for (e = path; *e; e++) {
984 if (!slash && *e == '/') {
988 } else if (slash && *e != '/')
1003 r = strndup(path, p-path);
1013 char *file_name_from_path(const char *p) {
1018 if ((r = strrchr(p, '/')))
1024 bool path_is_absolute(const char *p) {
1030 bool is_path(const char *p) {
1032 return !!strchr(p, '/');
1035 char *path_make_absolute(const char *p, const char *prefix) {
1040 /* Makes every item in the list an absolute path by prepending
1041 * the prefix, if specified and necessary */
1043 if (path_is_absolute(p) || !prefix)
1046 if (asprintf(&r, "%s/%s", prefix, p) < 0)
1052 char *path_make_absolute_cwd(const char *p) {
1057 /* Similar to path_make_absolute(), but prefixes with the
1058 * current working directory. */
1060 if (path_is_absolute(p))
1063 if (!(cwd = get_current_dir_name()))
1066 r = path_make_absolute(p, cwd);
1072 char **strv_path_make_absolute_cwd(char **l) {
1075 /* Goes through every item in the string list and makes it
1076 * absolute. This works in place and won't rollback any
1077 * changes on failure. */
1079 STRV_FOREACH(s, l) {
1082 if (!(t = path_make_absolute_cwd(*s)))
1092 char **strv_path_canonicalize(char **l) {
1095 bool enomem = false;
1097 if (strv_isempty(l))
1100 /* Goes through every item in the string list and canonicalize
1101 * the path. This works in place and won't rollback any
1102 * changes on failure. */
1104 STRV_FOREACH(s, l) {
1107 t = path_make_absolute_cwd(*s);
1116 u = canonicalize_file_name(t);
1120 if (errno == ENOMEM || !errno)
1137 int reset_all_signal_handlers(void) {
1140 for (sig = 1; sig < _NSIG; sig++) {
1141 struct sigaction sa;
1143 if (sig == SIGKILL || sig == SIGSTOP)
1147 sa.sa_handler = SIG_DFL;
1148 sa.sa_flags = SA_RESTART;
1150 /* On Linux the first two RT signals are reserved by
1151 * glibc, and sigaction() will return EINVAL for them. */
1152 if ((sigaction(sig, &sa, NULL) < 0))
1153 if (errno != EINVAL)
1160 char *strstrip(char *s) {
1163 /* Drops trailing whitespace. Modifies the string in
1164 * place. Returns pointer to first non-space character */
1166 s += strspn(s, WHITESPACE);
1168 for (e = s; *e; e++)
1169 if (!strchr(WHITESPACE, *e))
1180 char *delete_chars(char *s, const char *bad) {
1183 /* Drops all whitespace, regardless where in the string */
1185 for (f = s, t = s; *f; f++) {
1186 if (strchr(bad, *f))
1197 char *file_in_same_dir(const char *path, const char *filename) {
1204 /* This removes the last component of path and appends
1205 * filename, unless the latter is absolute anyway or the
1208 if (path_is_absolute(filename))
1209 return strdup(filename);
1211 if (!(e = strrchr(path, '/')))
1212 return strdup(filename);
1214 k = strlen(filename);
1215 if (!(r = new(char, e-path+1+k+1)))
1218 memcpy(r, path, e-path+1);
1219 memcpy(r+(e-path)+1, filename, k+1);
1224 int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
1227 if (label_mkdir(path, mode) >= 0)
1228 if (chmod_and_chown(path, mode, uid, gid) < 0)
1231 if (lstat(path, &st) < 0)
1234 if ((st.st_mode & 0777) != mode ||
1237 !S_ISDIR(st.st_mode)) {
1246 int mkdir_parents(const char *path, mode_t mode) {
1251 /* Creates every parent directory in the path except the last
1254 p = path + strspn(path, "/");
1259 e = p + strcspn(p, "/");
1260 p = e + strspn(e, "/");
1262 /* Is this the last component? If so, then we're
1267 if (!(t = strndup(path, e - path)))
1270 r = label_mkdir(t, mode);
1273 if (r < 0 && errno != EEXIST)
1278 int mkdir_p(const char *path, mode_t mode) {
1283 if ((r = mkdir_parents(path, mode)) < 0)
1286 if (label_mkdir(path, mode) < 0 && errno != EEXIST)
1292 int rmdir_parents(const char *path, const char *stop) {
1301 /* Skip trailing slashes */
1302 while (l > 0 && path[l-1] == '/')
1308 /* Skip last component */
1309 while (l > 0 && path[l-1] != '/')
1312 /* Skip trailing slashes */
1313 while (l > 0 && path[l-1] == '/')
1319 if (!(t = strndup(path, l)))
1322 if (path_startswith(stop, t)) {
1331 if (errno != ENOENT)
1339 char hexchar(int x) {
1340 static const char table[16] = "0123456789abcdef";
1342 return table[x & 15];
1345 int unhexchar(char c) {
1347 if (c >= '0' && c <= '9')
1350 if (c >= 'a' && c <= 'f')
1351 return c - 'a' + 10;
1353 if (c >= 'A' && c <= 'F')
1354 return c - 'A' + 10;
1359 char octchar(int x) {
1360 return '0' + (x & 7);
1363 int unoctchar(char c) {
1365 if (c >= '0' && c <= '7')
1371 char decchar(int x) {
1372 return '0' + (x % 10);
1375 int undecchar(char c) {
1377 if (c >= '0' && c <= '9')
1383 char *cescape(const char *s) {
1389 /* Does C style string escaping. */
1391 if (!(r = new(char, strlen(s)*4 + 1)))
1394 for (f = s, t = r; *f; f++)
1440 /* For special chars we prefer octal over
1441 * hexadecimal encoding, simply because glib's
1442 * g_strescape() does the same */
1443 if ((*f < ' ') || (*f >= 127)) {
1445 *(t++) = octchar((unsigned char) *f >> 6);
1446 *(t++) = octchar((unsigned char) *f >> 3);
1447 *(t++) = octchar((unsigned char) *f);
1458 char *cunescape_length(const char *s, size_t length) {
1464 /* Undoes C style string escaping */
1466 if (!(r = new(char, length+1)))
1469 for (f = s, t = r; f < s + length; f++) {
1512 /* This is an extension of the XDG syntax files */
1517 /* hexadecimal encoding */
1520 if ((a = unhexchar(f[1])) < 0 ||
1521 (b = unhexchar(f[2])) < 0) {
1522 /* Invalid escape code, let's take it literal then */
1526 *(t++) = (char) ((a << 4) | b);
1541 /* octal encoding */
1544 if ((a = unoctchar(f[0])) < 0 ||
1545 (b = unoctchar(f[1])) < 0 ||
1546 (c = unoctchar(f[2])) < 0) {
1547 /* Invalid escape code, let's take it literal then */
1551 *(t++) = (char) ((a << 6) | (b << 3) | c);
1559 /* premature end of string.*/
1564 /* Invalid escape code, let's take it literal then */
1576 char *cunescape(const char *s) {
1577 return cunescape_length(s, strlen(s));
1580 char *xescape(const char *s, const char *bad) {
1584 /* Escapes all chars in bad, in addition to \ and all special
1585 * chars, in \xFF style escaping. May be reversed with
1588 if (!(r = new(char, strlen(s)*4+1)))
1591 for (f = s, t = r; *f; f++) {
1593 if ((*f < ' ') || (*f >= 127) ||
1594 (*f == '\\') || strchr(bad, *f)) {
1597 *(t++) = hexchar(*f >> 4);
1598 *(t++) = hexchar(*f);
1608 char *bus_path_escape(const char *s) {
1614 /* Escapes all chars that D-Bus' object path cannot deal
1615 * with. Can be reverse with bus_path_unescape() */
1617 if (!(r = new(char, strlen(s)*3+1)))
1620 for (f = s, t = r; *f; f++) {
1622 if (!(*f >= 'A' && *f <= 'Z') &&
1623 !(*f >= 'a' && *f <= 'z') &&
1624 !(*f >= '0' && *f <= '9')) {
1626 *(t++) = hexchar(*f >> 4);
1627 *(t++) = hexchar(*f);
1637 char *bus_path_unescape(const char *f) {
1642 if (!(r = strdup(f)))
1645 for (t = r; *f; f++) {
1650 if ((a = unhexchar(f[1])) < 0 ||
1651 (b = unhexchar(f[2])) < 0) {
1652 /* Invalid escape code, let's take it literal then */
1655 *(t++) = (char) ((a << 4) | b);
1667 char *path_kill_slashes(char *path) {
1671 /* Removes redundant inner and trailing slashes. Modifies the
1672 * passed string in-place.
1674 * ///foo///bar/ becomes /foo/bar
1677 for (f = path, t = path; *f; f++) {
1692 /* Special rule, if we are talking of the root directory, a
1693 trailing slash is good */
1695 if (t == path && slash)
1702 bool path_startswith(const char *path, const char *prefix) {
1706 if ((path[0] == '/') != (prefix[0] == '/'))
1712 path += strspn(path, "/");
1713 prefix += strspn(prefix, "/");
1721 a = strcspn(path, "/");
1722 b = strcspn(prefix, "/");
1727 if (memcmp(path, prefix, a) != 0)
1735 bool path_equal(const char *a, const char *b) {
1739 if ((a[0] == '/') != (b[0] == '/'))
1745 a += strspn(a, "/");
1746 b += strspn(b, "/");
1748 if (*a == 0 && *b == 0)
1751 if (*a == 0 || *b == 0)
1754 j = strcspn(a, "/");
1755 k = strcspn(b, "/");
1760 if (memcmp(a, b, j) != 0)
1768 char *ascii_strlower(char *t) {
1773 for (p = t; *p; p++)
1774 if (*p >= 'A' && *p <= 'Z')
1775 *p = *p - 'A' + 'a';
1780 bool ignore_file(const char *filename) {
1784 filename[0] == '.' ||
1785 streq(filename, "lost+found") ||
1786 endswith(filename, "~") ||
1787 endswith(filename, ".rpmnew") ||
1788 endswith(filename, ".rpmsave") ||
1789 endswith(filename, ".rpmorig") ||
1790 endswith(filename, ".dpkg-old") ||
1791 endswith(filename, ".dpkg-new") ||
1792 endswith(filename, ".swp");
1795 int fd_nonblock(int fd, bool nonblock) {
1800 if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
1804 flags |= O_NONBLOCK;
1806 flags &= ~O_NONBLOCK;
1808 if (fcntl(fd, F_SETFL, flags) < 0)
1814 int fd_cloexec(int fd, bool cloexec) {
1819 if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
1823 flags |= FD_CLOEXEC;
1825 flags &= ~FD_CLOEXEC;
1827 if (fcntl(fd, F_SETFD, flags) < 0)
1833 int close_all_fds(const int except[], unsigned n_except) {
1838 if (!(d = opendir("/proc/self/fd")))
1841 while ((de = readdir(d))) {
1844 if (ignore_file(de->d_name))
1847 if ((r = safe_atoi(de->d_name, &fd)) < 0)
1861 for (i = 0; i < n_except; i++)
1862 if (except[i] == fd) {
1871 if ((r = close_nointr(fd)) < 0) {
1872 /* Valgrind has its own FD and doesn't want to have it closed */
1885 bool chars_intersect(const char *a, const char *b) {
1888 /* Returns true if any of the chars in a are in b. */
1889 for (p = a; *p; p++)
1896 char *format_timestamp(char *buf, size_t l, usec_t t) {
1906 sec = (time_t) (t / USEC_PER_SEC);
1908 if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0)
1914 char *format_timespan(char *buf, size_t l, usec_t t) {
1915 static const struct {
1919 { "w", USEC_PER_WEEK },
1920 { "d", USEC_PER_DAY },
1921 { "h", USEC_PER_HOUR },
1922 { "min", USEC_PER_MINUTE },
1923 { "s", USEC_PER_SEC },
1924 { "ms", USEC_PER_MSEC },
1934 if (t == (usec_t) -1)
1937 /* The result of this function can be parsed with parse_usec */
1939 for (i = 0; i < ELEMENTSOF(table); i++) {
1943 if (t < table[i].usec)
1949 k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix);
1950 n = MIN((size_t) k, l);
1963 bool fstype_is_network(const char *fstype) {
1964 static const char * const table[] = {
1976 for (i = 0; i < ELEMENTSOF(table); i++)
1977 if (streq(table[i], fstype))
1986 if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
1991 TIOCL_GETKMSGREDIRECT,
1995 if (ioctl(fd, TIOCLINUX, tiocl) < 0)
1998 vt = tiocl[0] <= 0 ? 1 : tiocl[0];
2001 if (ioctl(fd, VT_ACTIVATE, vt) < 0)
2004 close_nointr_nofail(r);
2008 int read_one_char(FILE *f, char *ret, bool *need_nl) {
2009 struct termios old_termios, new_termios;
2016 if (tcgetattr(fileno(f), &old_termios) >= 0) {
2017 new_termios = old_termios;
2019 new_termios.c_lflag &= ~ICANON;
2020 new_termios.c_cc[VMIN] = 1;
2021 new_termios.c_cc[VTIME] = 0;
2023 if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
2026 k = fread(&c, 1, 1, f);
2028 tcsetattr(fileno(f), TCSADRAIN, &old_termios);
2034 *need_nl = c != '\n';
2041 if (!(fgets(line, sizeof(line), f)))
2046 if (strlen(line) != 1)
2056 int ask(char *ret, const char *replies, const char *text, ...) {
2065 bool need_nl = true;
2067 fputs("\x1B[1m", stdout);
2073 fputs("\x1B[0m", stdout);
2077 if ((r = read_one_char(stdin, &c, &need_nl)) < 0) {
2079 if (r == -EBADMSG) {
2080 puts("Bad input, please try again.");
2091 if (strchr(replies, c)) {
2096 puts("Read unexpected character, please try again.");
2100 int reset_terminal(int fd) {
2101 struct termios termios;
2105 /* Set terminal to some sane defaults */
2109 /* First, unlock termios */
2111 ioctl(fd, TIOCSLCKTRMIOS, &termios);
2113 /* Disable exclusive mode, just in case */
2114 ioctl(fd, TIOCNXCL);
2116 /* Enable console unicode mode */
2118 ioctl(fd, KDSKBMODE, &arg);
2120 if (tcgetattr(fd, &termios) < 0) {
2125 /* We only reset the stuff that matters to the software. How
2126 * hardware is set up we don't touch assuming that somebody
2127 * else will do that for us */
2129 termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
2130 termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
2131 termios.c_oflag |= ONLCR;
2132 termios.c_cflag |= CREAD;
2133 termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
2135 termios.c_cc[VINTR] = 03; /* ^C */
2136 termios.c_cc[VQUIT] = 034; /* ^\ */
2137 termios.c_cc[VERASE] = 0177;
2138 termios.c_cc[VKILL] = 025; /* ^X */
2139 termios.c_cc[VEOF] = 04; /* ^D */
2140 termios.c_cc[VSTART] = 021; /* ^Q */
2141 termios.c_cc[VSTOP] = 023; /* ^S */
2142 termios.c_cc[VSUSP] = 032; /* ^Z */
2143 termios.c_cc[VLNEXT] = 026; /* ^V */
2144 termios.c_cc[VWERASE] = 027; /* ^W */
2145 termios.c_cc[VREPRINT] = 022; /* ^R */
2146 termios.c_cc[VEOL] = 0;
2147 termios.c_cc[VEOL2] = 0;
2149 termios.c_cc[VTIME] = 0;
2150 termios.c_cc[VMIN] = 1;
2152 if (tcsetattr(fd, TCSANOW, &termios) < 0)
2156 /* Just in case, flush all crap out */
2157 tcflush(fd, TCIOFLUSH);
2162 int open_terminal(const char *name, int mode) {
2165 if ((fd = open(name, mode)) < 0)
2168 if ((r = isatty(fd)) < 0) {
2169 close_nointr_nofail(fd);
2174 close_nointr_nofail(fd);
2181 int flush_fd(int fd) {
2182 struct pollfd pollfd;
2186 pollfd.events = POLLIN;
2193 if ((r = poll(&pollfd, 1, 0)) < 0) {
2204 if ((l = read(fd, buf, sizeof(buf))) < 0) {
2209 if (errno == EAGAIN)
2220 int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) {
2221 int fd = -1, notify = -1, r, wd = -1;
2225 /* We use inotify to be notified when the tty is closed. We
2226 * create the watch before checking if we can actually acquire
2227 * it, so that we don't lose any event.
2229 * Note: strictly speaking this actually watches for the
2230 * device being closed, it does *not* really watch whether a
2231 * tty loses its controlling process. However, unless some
2232 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
2233 * its tty otherwise this will not become a problem. As long
2234 * as the administrator makes sure not configure any service
2235 * on the same tty as an untrusted user this should not be a
2236 * problem. (Which he probably should not do anyway.) */
2238 if (!fail && !force) {
2239 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
2244 if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) {
2252 if ((r = flush_fd(notify)) < 0)
2255 /* We pass here O_NOCTTY only so that we can check the return
2256 * value TIOCSCTTY and have a reliable way to figure out if we
2257 * successfully became the controlling process of the tty */
2258 if ((fd = open_terminal(name, O_RDWR|O_NOCTTY)) < 0)
2261 /* First, try to get the tty */
2262 r = ioctl(fd, TIOCSCTTY, force);
2264 /* Sometimes it makes sense to ignore TIOCSCTTY
2265 * returning EPERM, i.e. when very likely we already
2266 * are have this controlling terminal. */
2267 if (r < 0 && errno == EPERM && ignore_tiocstty_eperm)
2270 if (r < 0 && (force || fail || errno != EPERM)) {
2280 assert(notify >= 0);
2283 struct inotify_event e;
2286 if ((l = read(notify, &e, sizeof(e))) != sizeof(e)) {
2300 if (e.wd != wd || !(e.mask & IN_CLOSE)) {
2308 /* We close the tty fd here since if the old session
2309 * ended our handle will be dead. It's important that
2310 * we do this after sleeping, so that we don't enter
2311 * an endless loop. */
2312 close_nointr_nofail(fd);
2316 close_nointr_nofail(notify);
2318 if ((r = reset_terminal(fd)) < 0)
2319 log_warning("Failed to reset terminal: %s", strerror(-r));
2325 close_nointr_nofail(fd);
2328 close_nointr_nofail(notify);
2333 int release_terminal(void) {
2335 struct sigaction sa_old, sa_new;
2337 if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
2340 /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
2341 * by our own TIOCNOTTY */
2344 sa_new.sa_handler = SIG_IGN;
2345 sa_new.sa_flags = SA_RESTART;
2346 assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
2348 if (ioctl(fd, TIOCNOTTY) < 0)
2351 assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
2353 close_nointr_nofail(fd);
2357 int sigaction_many(const struct sigaction *sa, ...) {
2362 while ((sig = va_arg(ap, int)) > 0)
2363 if (sigaction(sig, sa, NULL) < 0)
2370 int ignore_signals(int sig, ...) {
2371 struct sigaction sa;
2376 sa.sa_handler = SIG_IGN;
2377 sa.sa_flags = SA_RESTART;
2379 if (sigaction(sig, &sa, NULL) < 0)
2383 while ((sig = va_arg(ap, int)) > 0)
2384 if (sigaction(sig, &sa, NULL) < 0)
2391 int default_signals(int sig, ...) {
2392 struct sigaction sa;
2397 sa.sa_handler = SIG_DFL;
2398 sa.sa_flags = SA_RESTART;
2400 if (sigaction(sig, &sa, NULL) < 0)
2404 while ((sig = va_arg(ap, int)) > 0)
2405 if (sigaction(sig, &sa, NULL) < 0)
2412 int close_pipe(int p[]) {
2418 a = close_nointr(p[0]);
2423 b = close_nointr(p[1]);
2427 return a < 0 ? a : b;
2430 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
2439 while (nbytes > 0) {
2442 if ((k = read(fd, p, nbytes)) <= 0) {
2444 if (k < 0 && errno == EINTR)
2447 if (k < 0 && errno == EAGAIN && do_poll) {
2448 struct pollfd pollfd;
2452 pollfd.events = POLLIN;
2454 if (poll(&pollfd, 1, -1) < 0) {
2458 return n > 0 ? n : -errno;
2461 if (pollfd.revents != POLLIN)
2462 return n > 0 ? n : -EIO;
2467 return n > 0 ? n : (k < 0 ? -errno : 0);
2478 ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
2487 while (nbytes > 0) {
2490 if ((k = write(fd, p, nbytes)) <= 0) {
2492 if (k < 0 && errno == EINTR)
2495 if (k < 0 && errno == EAGAIN && do_poll) {
2496 struct pollfd pollfd;
2500 pollfd.events = POLLOUT;
2502 if (poll(&pollfd, 1, -1) < 0) {
2506 return n > 0 ? n : -errno;
2509 if (pollfd.revents != POLLOUT)
2510 return n > 0 ? n : -EIO;
2515 return n > 0 ? n : (k < 0 ? -errno : 0);
2526 int path_is_mount_point(const char *t) {
2531 if (lstat(t, &a) < 0) {
2532 if (errno == ENOENT)
2538 if ((r = parent_of_path(t, &parent)) < 0)
2541 r = lstat(parent, &b);
2547 return a.st_dev != b.st_dev;
2550 int parse_usec(const char *t, usec_t *usec) {
2551 static const struct {
2555 { "sec", USEC_PER_SEC },
2556 { "s", USEC_PER_SEC },
2557 { "min", USEC_PER_MINUTE },
2558 { "hr", USEC_PER_HOUR },
2559 { "h", USEC_PER_HOUR },
2560 { "d", USEC_PER_DAY },
2561 { "w", USEC_PER_WEEK },
2562 { "msec", USEC_PER_MSEC },
2563 { "ms", USEC_PER_MSEC },
2564 { "m", USEC_PER_MINUTE },
2567 { "", USEC_PER_SEC },
2583 l = strtoll(p, &e, 10);
2594 e += strspn(e, WHITESPACE);
2596 for (i = 0; i < ELEMENTSOF(table); i++)
2597 if (startswith(e, table[i].suffix)) {
2598 r += (usec_t) l * table[i].usec;
2599 p = e + strlen(table[i].suffix);
2603 if (i >= ELEMENTSOF(table))
2613 int make_stdio(int fd) {
2618 r = dup2(fd, STDIN_FILENO);
2619 s = dup2(fd, STDOUT_FILENO);
2620 t = dup2(fd, STDERR_FILENO);
2623 close_nointr_nofail(fd);
2625 if (r < 0 || s < 0 || t < 0)
2631 bool is_clean_exit(int code, int status) {
2633 if (code == CLD_EXITED)
2636 /* If a daemon does not implement handlers for some of the
2637 * signals that's not considered an unclean shutdown */
2638 if (code == CLD_KILLED)
2642 status == SIGTERM ||
2648 bool is_device_path(const char *path) {
2650 /* Returns true on paths that refer to a device, either in
2651 * sysfs or in /dev */
2654 path_startswith(path, "/dev/") ||
2655 path_startswith(path, "/sys/");
2658 int dir_is_empty(const char *path) {
2661 struct dirent buf, *de;
2663 if (!(d = opendir(path)))
2667 if ((r = readdir_r(d, &buf, &de)) > 0) {
2677 if (!ignore_file(de->d_name)) {
2687 unsigned long long random_ull(void) {
2692 if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0)
2695 r = loop_read(fd, &ull, sizeof(ull), true);
2696 close_nointr_nofail(fd);
2698 if (r != sizeof(ull))
2704 return random() * RAND_MAX + random();
2707 void rename_process(const char name[8]) {
2710 prctl(PR_SET_NAME, name);
2712 /* This is a like a poor man's setproctitle(). The string
2713 * passed should fit in 7 chars (i.e. the length of
2716 if (program_invocation_name)
2717 strncpy(program_invocation_name, name, strlen(program_invocation_name));
2720 void sigset_add_many(sigset_t *ss, ...) {
2727 while ((sig = va_arg(ap, int)) > 0)
2728 assert_se(sigaddset(ss, sig) == 0);
2732 char* gethostname_malloc(void) {
2735 assert_se(uname(&u) >= 0);
2738 return strdup(u.nodename);
2740 return strdup(u.sysname);
2743 int getmachineid_malloc(char **b) {
2748 if ((r = read_one_line_file("/var/lib/dbus/machine-id", b)) < 0)
2755 char* getlogname_malloc(void) {
2759 struct passwd pwbuf, *pw = NULL;
2762 if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
2767 /* Shortcut things to avoid NSS lookups */
2769 return strdup("root");
2771 if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0)
2774 if (!(buf = malloc(bufsize)))
2777 if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) {
2778 name = strdup(pw->pw_name);
2785 if (asprintf(&name, "%lu", (unsigned long) uid) < 0)
2791 int getttyname_malloc(char **r) {
2792 char path[PATH_MAX], *p, *c;
2796 if (ttyname_r(STDIN_FILENO, path, sizeof(path)) < 0)
2802 if (startswith(path, "/dev/"))
2805 if (!(c = strdup(p)))
2812 static int rm_rf_children(int fd, bool only_dirs) {
2818 /* This returns the first error we run into, but nevertheless
2821 if (!(d = fdopendir(fd))) {
2822 close_nointr_nofail(fd);
2824 return errno == ENOENT ? 0 : -errno;
2828 struct dirent buf, *de;
2832 if ((r = readdir_r(d, &buf, &de)) != 0) {
2841 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
2844 if (de->d_type == DT_UNKNOWN) {
2847 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
2848 if (ret == 0 && errno != ENOENT)
2853 is_dir = S_ISDIR(st.st_mode);
2855 is_dir = de->d_type == DT_DIR;
2860 if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
2861 if (ret == 0 && errno != ENOENT)
2866 if ((r = rm_rf_children(subdir_fd, only_dirs)) < 0) {
2871 if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
2872 if (ret == 0 && errno != ENOENT)
2875 } else if (!only_dirs) {
2877 if (unlinkat(fd, de->d_name, 0) < 0) {
2878 if (ret == 0 && errno != ENOENT)
2889 int rm_rf(const char *path, bool only_dirs, bool delete_root) {
2895 if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
2897 if (errno != ENOTDIR)
2900 if (delete_root && !only_dirs)
2901 if (unlink(path) < 0)
2907 r = rm_rf_children(fd, only_dirs);
2910 if (rmdir(path) < 0) {
2918 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
2921 /* Under the assumption that we are running privileged we
2922 * first change the access mode and only then hand out
2923 * ownership to avoid a window where access is too open. */
2925 if (chmod(path, mode) < 0)
2928 if (chown(path, uid, gid) < 0)
2934 cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
2938 /* Allocates the cpuset in the right size */
2941 if (!(r = CPU_ALLOC(n)))
2944 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) {
2945 CPU_ZERO_S(CPU_ALLOC_SIZE(n), r);
2955 if (errno != EINVAL)
2962 void status_vprintf(const char *format, va_list ap) {
2968 /* This independent of logging, as status messages are
2969 * optional and go exclusively to the console. */
2971 if (vasprintf(&s, format, ap) < 0)
2974 if ((fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
2977 write(fd, s, strlen(s));
2983 close_nointr_nofail(fd);
2986 void status_printf(const char *format, ...) {
2991 va_start(ap, format);
2992 status_vprintf(format, ap);
2996 void status_welcome(void) {
2998 #if defined(TARGET_FEDORA)
3001 if (read_one_line_file("/etc/system-release", &r) < 0)
3006 /* This tries to mimic the color magic the old Red Hat sysinit
3009 if (startswith(r, "Red Hat"))
3010 status_printf("Welcome to \x1B[0;31m%s\x1B[0m!\n", r); /* Red for RHEL */
3011 else if (startswith(r, "Fedora"))
3012 status_printf("Welcome to \x1B[0;34m%s\x1B[0m!\n", r); /* Blue for Fedora */
3014 status_printf("Welcome to %s!\n", r);
3018 #elif defined(TARGET_SUSE)
3021 if (read_one_line_file("/etc/SuSE-release", &r) < 0)
3026 status_printf("Welcome to \x1B[0;32m%s\x1B[0m!\n", r); /* Green for SUSE */
3029 #warning "You probably should add a welcome text logic here."
3033 char *replace_env(const char *format, char **env) {
3040 const char *e, *word = format;
3045 for (e = format; *e; e ++) {
3056 if (!(k = strnappend(r, word, e-word-1)))
3065 } else if (*e == '$') {
3066 if (!(k = strnappend(r, word, e-word)))
3082 if ((t = strv_env_get_with_length(env, word+2, e-word-2))) {
3083 if (!(k = strappend(r, t)))
3098 if (!(k = strnappend(r, word, e-word)))
3109 char **replace_env_argv(char **argv, char **env) {
3111 unsigned k = 0, l = 0;
3113 l = strv_length(argv);
3115 if (!(r = new(char*, l+1)))
3118 STRV_FOREACH(i, argv) {
3120 /* If $FOO appears as single word, replace it by the split up variable */
3121 if ((*i)[0] == '$') {
3122 char *e = strv_env_get(env, *i+1);
3128 if (!(m = strv_split_quoted(e))) {
3137 if (!(w = realloc(r, sizeof(char*) * (l+1)))) {
3145 memcpy(r + k, m, q * sizeof(char*));
3153 /* If ${FOO} appears as part of a word, replace it by the variable as-is */
3154 if (!(r[k++] = replace_env(*i, env))) {
3165 static __thread int parsed_columns = 0;
3168 if (parsed_columns > 0)
3169 return parsed_columns;
3171 if ((e = getenv("COLUMNS")))
3172 parsed_columns = atoi(e);
3174 if (parsed_columns <= 0) {
3178 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
3179 parsed_columns = ws.ws_col;
3182 if (parsed_columns <= 0)
3183 parsed_columns = 80;
3185 return parsed_columns;
3188 int running_in_chroot(void) {
3194 /* Only works as root */
3196 if (stat("/proc/1/root", &a) < 0)
3199 if (stat("/", &b) < 0)
3203 a.st_dev != b.st_dev ||
3204 a.st_ino != b.st_ino;
3207 char *ellipsize(const char *s, unsigned length, unsigned percent) {
3212 assert(percent <= 100);
3213 assert(length >= 3);
3217 if (l <= 3 || l <= length)
3220 if (!(r = new0(char, length+1)))
3223 x = (length * percent) / 100;
3233 s + l - (length - x - 3),
3239 static const char *const ioprio_class_table[] = {
3240 [IOPRIO_CLASS_NONE] = "none",
3241 [IOPRIO_CLASS_RT] = "realtime",
3242 [IOPRIO_CLASS_BE] = "best-effort",
3243 [IOPRIO_CLASS_IDLE] = "idle"
3246 DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
3248 static const char *const sigchld_code_table[] = {
3249 [CLD_EXITED] = "exited",
3250 [CLD_KILLED] = "killed",
3251 [CLD_DUMPED] = "dumped",
3252 [CLD_TRAPPED] = "trapped",
3253 [CLD_STOPPED] = "stopped",
3254 [CLD_CONTINUED] = "continued",
3257 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
3259 static const char *const log_facility_table[LOG_NFACILITIES] = {
3260 [LOG_FAC(LOG_KERN)] = "kern",
3261 [LOG_FAC(LOG_USER)] = "user",
3262 [LOG_FAC(LOG_MAIL)] = "mail",
3263 [LOG_FAC(LOG_DAEMON)] = "daemon",
3264 [LOG_FAC(LOG_AUTH)] = "auth",
3265 [LOG_FAC(LOG_SYSLOG)] = "syslog",
3266 [LOG_FAC(LOG_LPR)] = "lpr",
3267 [LOG_FAC(LOG_NEWS)] = "news",
3268 [LOG_FAC(LOG_UUCP)] = "uucp",
3269 [LOG_FAC(LOG_CRON)] = "cron",
3270 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
3271 [LOG_FAC(LOG_FTP)] = "ftp",
3272 [LOG_FAC(LOG_LOCAL0)] = "local0",
3273 [LOG_FAC(LOG_LOCAL1)] = "local1",
3274 [LOG_FAC(LOG_LOCAL2)] = "local2",
3275 [LOG_FAC(LOG_LOCAL3)] = "local3",
3276 [LOG_FAC(LOG_LOCAL4)] = "local4",
3277 [LOG_FAC(LOG_LOCAL5)] = "local5",
3278 [LOG_FAC(LOG_LOCAL6)] = "local6",
3279 [LOG_FAC(LOG_LOCAL7)] = "local7"
3282 DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
3284 static const char *const log_level_table[] = {
3285 [LOG_EMERG] = "emerg",
3286 [LOG_ALERT] = "alert",
3287 [LOG_CRIT] = "crit",
3289 [LOG_WARNING] = "warning",
3290 [LOG_NOTICE] = "notice",
3291 [LOG_INFO] = "info",
3292 [LOG_DEBUG] = "debug"
3295 DEFINE_STRING_TABLE_LOOKUP(log_level, int);
3297 static const char* const sched_policy_table[] = {
3298 [SCHED_OTHER] = "other",
3299 [SCHED_BATCH] = "batch",
3300 [SCHED_IDLE] = "idle",
3301 [SCHED_FIFO] = "fifo",
3305 DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
3307 static const char* const rlimit_table[] = {
3308 [RLIMIT_CPU] = "LimitCPU",
3309 [RLIMIT_FSIZE] = "LimitFSIZE",
3310 [RLIMIT_DATA] = "LimitDATA",
3311 [RLIMIT_STACK] = "LimitSTACK",
3312 [RLIMIT_CORE] = "LimitCORE",
3313 [RLIMIT_RSS] = "LimitRSS",
3314 [RLIMIT_NOFILE] = "LimitNOFILE",
3315 [RLIMIT_AS] = "LimitAS",
3316 [RLIMIT_NPROC] = "LimitNPROC",
3317 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
3318 [RLIMIT_LOCKS] = "LimitLOCKS",
3319 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
3320 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
3321 [RLIMIT_NICE] = "LimitNICE",
3322 [RLIMIT_RTPRIO] = "LimitRTPRIO",
3323 [RLIMIT_RTTIME] = "LimitRTTIME"
3326 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
3328 static const char* const ip_tos_table[] = {
3329 [IPTOS_LOWDELAY] = "low-delay",
3330 [IPTOS_THROUGHPUT] = "throughput",
3331 [IPTOS_RELIABILITY] = "reliability",
3332 [IPTOS_LOWCOST] = "low-cost",
3335 DEFINE_STRING_TABLE_LOOKUP(ip_tos, int);
3337 static const char *const signal_table[] = {
3353 [SIGSTKFLT] = "STKFLT",
3363 [SIGVTALRM] = "VTALRM",
3365 [SIGWINCH] = "WINCH",
3371 DEFINE_STRING_TABLE_LOOKUP(signal, int);