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 bool use_selinux(void) {
66 static int use_selinux_ind = -1;
68 if (use_selinux_ind < 0)
69 use_selinux_ind = is_selinux_enabled() > 0;
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);
89 if ((sclass = string_to_security_class(class)) != 0)
90 r = security_compute_create((security_context_t) label, dir_con, sclass, fcon);
101 int label_init(void) {
109 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
111 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
112 "Failed to initialize SELinux context: %m");
113 r = (security_getenforce() == 1) ? -errno : 0;
120 int label_fix(const char *path) {
125 security_context_t fcon;
127 if (!use_selinux() || !label_hnd)
130 r = lstat(path, &st);
132 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
135 r = setfilecon(path, fcon);
140 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
141 "Unable to fix label of %s: %m", path);
142 r = (security_getenforce() == 1) ? -errno : 0;
149 void label_finish(void) {
152 if (use_selinux() && label_hnd)
153 selabel_close(label_hnd);
157 int label_get_socket_label_from_exe(const char *exe, char **label) {
162 security_context_t mycon = NULL, fcon = NULL;
163 security_class_t sclass;
165 if (!use_selinux()) {
174 r = getfilecon(exe, &fcon);
178 sclass = string_to_security_class("process");
179 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
181 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
184 if (r < 0 && security_getenforce() == 1)
194 int label_fifofile_set(const char *label, const char *path) {
198 security_context_t filecon = NULL;
200 if (!use_selinux() || !label)
203 if (((r = label_get_file_label_from_path(label, path, "fifo_file", &filecon)) == 0)) {
204 if ((r = setfscreatecon(filecon)) < 0) {
205 log_error("Failed to set SELinux file context (%s) on %s: %m", label, path);
212 if (r < 0 && security_getenforce() == 0)
219 int label_socket_set(const char *label) {
225 if (setsockcreatecon((security_context_t) label) < 0) {
226 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
227 "Failed to set SELinux context (%s) on socket: %m", label);
229 if (security_getenforce() == 1)
237 void label_file_clear(void) {
243 setfscreatecon(NULL);
247 void label_socket_clear(void) {
253 setsockcreatecon(NULL);
257 void label_free(const char *label) {
263 freecon((security_context_t) label);
267 static int label_mkdir(
273 security_context_t fcon = NULL;
275 if (use_selinux() && label_hnd) {
276 if (path[0] == '/') {
277 r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
281 char *newpath = NULL;
282 cwd = getcwd(NULL,0);
283 if ((! cwd) || (asprintf(&newpath, "%s/%s",cwd,path) < 0)) {
287 r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
293 r = setfscreatecon(fcon);
295 if ((r < 0) && (errno != ENOENT)) {
296 log_error("Failed to set security context %s for %s", fcon, path);
298 if (security_getenforce() == 1)
302 r = mkdir(path, mode);
305 if (use_selinux() && label_hnd) {
306 setfscreatecon(NULL);
312 return mkdir(path, mode);
316 bool streq_ptr(const char *a, const char *b) {
318 /* Like streq(), but tries to make sense of NULL pointers */
329 usec_t now(clockid_t clock_id) {
332 assert_se(clock_gettime(clock_id, &ts) == 0);
334 return timespec_load(&ts);
337 dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
340 ts->realtime = now(CLOCK_REALTIME);
341 ts->monotonic = now(CLOCK_MONOTONIC);
346 usec_t timespec_load(const struct timespec *ts) {
350 (usec_t) ts->tv_sec * USEC_PER_SEC +
351 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
354 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
357 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
358 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
363 usec_t timeval_load(const struct timeval *tv) {
367 (usec_t) tv->tv_sec * USEC_PER_SEC +
368 (usec_t) tv->tv_usec;
371 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
374 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
375 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
380 bool endswith(const char *s, const char *postfix) {
387 pl = strlen(postfix);
395 return memcmp(s + sl - pl, postfix, pl) == 0;
398 bool startswith(const char *s, const char *prefix) {
413 return memcmp(s, prefix, pl) == 0;
416 bool startswith_no_case(const char *s, const char *prefix) {
432 for(i = 0; i < pl; ++i) {
433 if (tolower(s[i]) != tolower(prefix[i]))
440 bool first_word(const char *s, const char *word) {
455 if (memcmp(s, word, wl) != 0)
459 strchr(WHITESPACE, s[wl]);
462 int close_nointr(int fd) {
468 if ((r = close(fd)) >= 0)
476 void close_nointr_nofail(int fd) {
477 int saved_errno = errno;
479 /* like close_nointr() but cannot fail, and guarantees errno
482 assert_se(close_nointr(fd) == 0);
487 void close_many(const int fds[], unsigned n_fd) {
490 for (i = 0; i < n_fd; i++)
491 close_nointr_nofail(fds[i]);
494 int parse_boolean(const char *v) {
497 if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
499 else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
505 int parse_pid(const char *s, pid_t* ret_pid) {
513 if ((r = safe_atolu(s, &ul)) < 0)
518 if ((unsigned long) pid != ul)
528 int safe_atou(const char *s, unsigned *ret_u) {
536 l = strtoul(s, &x, 0);
538 if (!x || *x || errno)
539 return errno ? -errno : -EINVAL;
541 if ((unsigned long) (unsigned) l != l)
544 *ret_u = (unsigned) l;
548 int safe_atoi(const char *s, int *ret_i) {
556 l = strtol(s, &x, 0);
558 if (!x || *x || errno)
559 return errno ? -errno : -EINVAL;
561 if ((long) (int) l != l)
568 int safe_atollu(const char *s, long long unsigned *ret_llu) {
570 unsigned long long l;
576 l = strtoull(s, &x, 0);
578 if (!x || *x || errno)
579 return errno ? -errno : -EINVAL;
585 int safe_atolli(const char *s, long long int *ret_lli) {
593 l = strtoll(s, &x, 0);
595 if (!x || *x || errno)
596 return errno ? -errno : -EINVAL;
602 /* Split a string into words. */
603 char *split(const char *c, size_t *l, const char *separator, char **state) {
606 current = *state ? *state : (char*) c;
608 if (!*current || *c == 0)
611 current += strspn(current, separator);
612 *l = strcspn(current, separator);
615 return (char*) current;
618 /* Split a string into words, but consider strings enclosed in '' and
619 * "" as words even if they include spaces. */
620 char *split_quoted(const char *c, size_t *l, char **state) {
622 bool escaped = false;
624 current = *state ? *state : (char*) c;
626 if (!*current || *c == 0)
629 current += strspn(current, WHITESPACE);
631 if (*current == '\'') {
634 for (e = current; *e; e++) {
644 *state = *e == 0 ? e : e+1;
645 } else if (*current == '\"') {
648 for (e = current; *e; e++) {
658 *state = *e == 0 ? e : e+1;
660 for (e = current; *e; e++) {
665 else if (strchr(WHITESPACE, *e))
672 return (char*) current;
675 char **split_path_and_make_absolute(const char *p) {
679 if (!(l = strv_split(p, ":")))
682 if (!strv_path_make_absolute_cwd(l)) {
690 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
693 char fn[132], line[256], *p;
699 assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
700 fn[sizeof(fn)-1] = 0;
702 if (!(f = fopen(fn, "r")))
705 if (!(fgets(line, sizeof(line), f))) {
713 /* Let's skip the pid and comm fields. The latter is enclosed
714 * in () but does not escape any () in its value, so let's
715 * skip over it manually */
717 if (!(p = strrchr(line, ')')))
728 if ((long unsigned) (pid_t) ppid != ppid)
731 *_ppid = (pid_t) ppid;
736 int write_one_line_file(const char *fn, const char *line) {
743 if (!(f = fopen(fn, "we")))
746 if (fputs(line, f) < 0) {
757 int read_one_line_file(const char *fn, char **line) {
765 if (!(f = fopen(fn, "re")))
768 if (!(fgets(t, sizeof(t), f))) {
773 if (!(c = strdup(t))) {
786 char *truncate_nl(char *s) {
789 s[strcspn(s, NEWLINE)] = 0;
793 int get_process_name(pid_t pid, char **name) {
800 if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
803 r = read_one_line_file(p, name);
813 int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
821 assert(max_length > 0);
824 if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
833 if (!(r = new(char, max_length))) {
840 while ((c = getc(f)) != EOF) {
862 size_t n = MIN(left-1, 3U);
870 /* Kernel threads have no argv[] */
877 if ((h = get_process_name(pid, &t)) < 0)
880 h = asprintf(&r, "[%s]", t);
891 char *strnappend(const char *s, const char *suffix, size_t b) {
899 return strndup(suffix, b);
909 if (!(r = new(char, a+b+1)))
913 memcpy(r+a, suffix, b);
919 char *strappend(const char *s, const char *suffix) {
920 return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
923 int readlink_malloc(const char *p, char **r) {
933 if (!(c = new(char, l)))
936 if ((n = readlink(p, c, l-1)) < 0) {
942 if ((size_t) n < l-1) {
953 int readlink_and_make_absolute(const char *p, char **r) {
960 if ((j = readlink_malloc(p, &target)) < 0)
963 k = file_in_same_dir(p, target);
973 int parent_of_path(const char *path, char **_r) {
974 const char *e, *a = NULL, *b = NULL, *p;
984 for (e = path; *e; e++) {
986 if (!slash && *e == '/') {
990 } else if (slash && *e != '/')
1005 r = strndup(path, p-path);
1015 char *file_name_from_path(const char *p) {
1020 if ((r = strrchr(p, '/')))
1026 bool path_is_absolute(const char *p) {
1032 bool is_path(const char *p) {
1034 return !!strchr(p, '/');
1037 char *path_make_absolute(const char *p, const char *prefix) {
1042 /* Makes every item in the list an absolute path by prepending
1043 * the prefix, if specified and necessary */
1045 if (path_is_absolute(p) || !prefix)
1048 if (asprintf(&r, "%s/%s", prefix, p) < 0)
1054 char *path_make_absolute_cwd(const char *p) {
1059 /* Similar to path_make_absolute(), but prefixes with the
1060 * current working directory. */
1062 if (path_is_absolute(p))
1065 if (!(cwd = get_current_dir_name()))
1068 r = path_make_absolute(p, cwd);
1074 char **strv_path_make_absolute_cwd(char **l) {
1077 /* Goes through every item in the string list and makes it
1078 * absolute. This works in place and won't rollback any
1079 * changes on failure. */
1081 STRV_FOREACH(s, l) {
1084 if (!(t = path_make_absolute_cwd(*s)))
1094 char **strv_path_canonicalize(char **l) {
1097 bool enomem = false;
1099 if (strv_isempty(l))
1102 /* Goes through every item in the string list and canonicalize
1103 * the path. This works in place and won't rollback any
1104 * changes on failure. */
1106 STRV_FOREACH(s, l) {
1109 t = path_make_absolute_cwd(*s);
1118 u = canonicalize_file_name(t);
1122 if (errno == ENOMEM || !errno)
1139 int reset_all_signal_handlers(void) {
1142 for (sig = 1; sig < _NSIG; sig++) {
1143 struct sigaction sa;
1145 if (sig == SIGKILL || sig == SIGSTOP)
1149 sa.sa_handler = SIG_DFL;
1150 sa.sa_flags = SA_RESTART;
1152 /* On Linux the first two RT signals are reserved by
1153 * glibc, and sigaction() will return EINVAL for them. */
1154 if ((sigaction(sig, &sa, NULL) < 0))
1155 if (errno != EINVAL)
1162 char *strstrip(char *s) {
1165 /* Drops trailing whitespace. Modifies the string in
1166 * place. Returns pointer to first non-space character */
1168 s += strspn(s, WHITESPACE);
1170 for (e = s; *e; e++)
1171 if (!strchr(WHITESPACE, *e))
1182 char *delete_chars(char *s, const char *bad) {
1185 /* Drops all whitespace, regardless where in the string */
1187 for (f = s, t = s; *f; f++) {
1188 if (strchr(bad, *f))
1199 char *file_in_same_dir(const char *path, const char *filename) {
1206 /* This removes the last component of path and appends
1207 * filename, unless the latter is absolute anyway or the
1210 if (path_is_absolute(filename))
1211 return strdup(filename);
1213 if (!(e = strrchr(path, '/')))
1214 return strdup(filename);
1216 k = strlen(filename);
1217 if (!(r = new(char, e-path+1+k+1)))
1220 memcpy(r, path, e-path+1);
1221 memcpy(r+(e-path)+1, filename, k+1);
1226 int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
1229 if (label_mkdir(path, mode) >= 0)
1230 if (chmod_and_chown(path, mode, uid, gid) < 0)
1233 if (lstat(path, &st) < 0)
1236 if ((st.st_mode & 0777) != mode ||
1239 !S_ISDIR(st.st_mode)) {
1248 int mkdir_parents(const char *path, mode_t mode) {
1253 /* Creates every parent directory in the path except the last
1256 p = path + strspn(path, "/");
1261 e = p + strcspn(p, "/");
1262 p = e + strspn(e, "/");
1264 /* Is this the last component? If so, then we're
1269 if (!(t = strndup(path, e - path)))
1272 r = label_mkdir(t, mode);
1275 if (r < 0 && errno != EEXIST)
1280 int mkdir_p(const char *path, mode_t mode) {
1285 if ((r = mkdir_parents(path, mode)) < 0)
1288 if (label_mkdir(path, mode) < 0 && errno != EEXIST)
1294 int rmdir_parents(const char *path, const char *stop) {
1303 /* Skip trailing slashes */
1304 while (l > 0 && path[l-1] == '/')
1310 /* Skip last component */
1311 while (l > 0 && path[l-1] != '/')
1314 /* Skip trailing slashes */
1315 while (l > 0 && path[l-1] == '/')
1321 if (!(t = strndup(path, l)))
1324 if (path_startswith(stop, t)) {
1333 if (errno != ENOENT)
1341 char hexchar(int x) {
1342 static const char table[16] = "0123456789abcdef";
1344 return table[x & 15];
1347 int unhexchar(char c) {
1349 if (c >= '0' && c <= '9')
1352 if (c >= 'a' && c <= 'f')
1353 return c - 'a' + 10;
1355 if (c >= 'A' && c <= 'F')
1356 return c - 'A' + 10;
1361 char octchar(int x) {
1362 return '0' + (x & 7);
1365 int unoctchar(char c) {
1367 if (c >= '0' && c <= '7')
1373 char decchar(int x) {
1374 return '0' + (x % 10);
1377 int undecchar(char c) {
1379 if (c >= '0' && c <= '9')
1385 char *cescape(const char *s) {
1391 /* Does C style string escaping. */
1393 if (!(r = new(char, strlen(s)*4 + 1)))
1396 for (f = s, t = r; *f; f++)
1442 /* For special chars we prefer octal over
1443 * hexadecimal encoding, simply because glib's
1444 * g_strescape() does the same */
1445 if ((*f < ' ') || (*f >= 127)) {
1447 *(t++) = octchar((unsigned char) *f >> 6);
1448 *(t++) = octchar((unsigned char) *f >> 3);
1449 *(t++) = octchar((unsigned char) *f);
1460 char *cunescape_length(const char *s, size_t length) {
1466 /* Undoes C style string escaping */
1468 if (!(r = new(char, length+1)))
1471 for (f = s, t = r; f < s + length; f++) {
1514 /* This is an extension of the XDG syntax files */
1519 /* hexadecimal encoding */
1522 if ((a = unhexchar(f[1])) < 0 ||
1523 (b = unhexchar(f[2])) < 0) {
1524 /* Invalid escape code, let's take it literal then */
1528 *(t++) = (char) ((a << 4) | b);
1543 /* octal encoding */
1546 if ((a = unoctchar(f[0])) < 0 ||
1547 (b = unoctchar(f[1])) < 0 ||
1548 (c = unoctchar(f[2])) < 0) {
1549 /* Invalid escape code, let's take it literal then */
1553 *(t++) = (char) ((a << 6) | (b << 3) | c);
1561 /* premature end of string.*/
1566 /* Invalid escape code, let's take it literal then */
1578 char *cunescape(const char *s) {
1579 return cunescape_length(s, strlen(s));
1582 char *xescape(const char *s, const char *bad) {
1586 /* Escapes all chars in bad, in addition to \ and all special
1587 * chars, in \xFF style escaping. May be reversed with
1590 if (!(r = new(char, strlen(s)*4+1)))
1593 for (f = s, t = r; *f; f++) {
1595 if ((*f < ' ') || (*f >= 127) ||
1596 (*f == '\\') || strchr(bad, *f)) {
1599 *(t++) = hexchar(*f >> 4);
1600 *(t++) = hexchar(*f);
1610 char *bus_path_escape(const char *s) {
1616 /* Escapes all chars that D-Bus' object path cannot deal
1617 * with. Can be reverse with bus_path_unescape() */
1619 if (!(r = new(char, strlen(s)*3+1)))
1622 for (f = s, t = r; *f; f++) {
1624 if (!(*f >= 'A' && *f <= 'Z') &&
1625 !(*f >= 'a' && *f <= 'z') &&
1626 !(*f >= '0' && *f <= '9')) {
1628 *(t++) = hexchar(*f >> 4);
1629 *(t++) = hexchar(*f);
1639 char *bus_path_unescape(const char *f) {
1644 if (!(r = strdup(f)))
1647 for (t = r; *f; f++) {
1652 if ((a = unhexchar(f[1])) < 0 ||
1653 (b = unhexchar(f[2])) < 0) {
1654 /* Invalid escape code, let's take it literal then */
1657 *(t++) = (char) ((a << 4) | b);
1669 char *path_kill_slashes(char *path) {
1673 /* Removes redundant inner and trailing slashes. Modifies the
1674 * passed string in-place.
1676 * ///foo///bar/ becomes /foo/bar
1679 for (f = path, t = path; *f; f++) {
1694 /* Special rule, if we are talking of the root directory, a
1695 trailing slash is good */
1697 if (t == path && slash)
1704 bool path_startswith(const char *path, const char *prefix) {
1708 if ((path[0] == '/') != (prefix[0] == '/'))
1714 path += strspn(path, "/");
1715 prefix += strspn(prefix, "/");
1723 a = strcspn(path, "/");
1724 b = strcspn(prefix, "/");
1729 if (memcmp(path, prefix, a) != 0)
1737 bool path_equal(const char *a, const char *b) {
1741 if ((a[0] == '/') != (b[0] == '/'))
1747 a += strspn(a, "/");
1748 b += strspn(b, "/");
1750 if (*a == 0 && *b == 0)
1753 if (*a == 0 || *b == 0)
1756 j = strcspn(a, "/");
1757 k = strcspn(b, "/");
1762 if (memcmp(a, b, j) != 0)
1770 char *ascii_strlower(char *t) {
1775 for (p = t; *p; p++)
1776 if (*p >= 'A' && *p <= 'Z')
1777 *p = *p - 'A' + 'a';
1782 bool ignore_file(const char *filename) {
1786 filename[0] == '.' ||
1787 streq(filename, "lost+found") ||
1788 endswith(filename, "~") ||
1789 endswith(filename, ".rpmnew") ||
1790 endswith(filename, ".rpmsave") ||
1791 endswith(filename, ".rpmorig") ||
1792 endswith(filename, ".dpkg-old") ||
1793 endswith(filename, ".dpkg-new") ||
1794 endswith(filename, ".swp");
1797 int fd_nonblock(int fd, bool nonblock) {
1802 if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
1806 flags |= O_NONBLOCK;
1808 flags &= ~O_NONBLOCK;
1810 if (fcntl(fd, F_SETFL, flags) < 0)
1816 int fd_cloexec(int fd, bool cloexec) {
1821 if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
1825 flags |= FD_CLOEXEC;
1827 flags &= ~FD_CLOEXEC;
1829 if (fcntl(fd, F_SETFD, flags) < 0)
1835 int close_all_fds(const int except[], unsigned n_except) {
1840 if (!(d = opendir("/proc/self/fd")))
1843 while ((de = readdir(d))) {
1846 if (ignore_file(de->d_name))
1849 if ((r = safe_atoi(de->d_name, &fd)) < 0)
1863 for (i = 0; i < n_except; i++)
1864 if (except[i] == fd) {
1873 if ((r = close_nointr(fd)) < 0) {
1874 /* Valgrind has its own FD and doesn't want to have it closed */
1887 bool chars_intersect(const char *a, const char *b) {
1890 /* Returns true if any of the chars in a are in b. */
1891 for (p = a; *p; p++)
1898 char *format_timestamp(char *buf, size_t l, usec_t t) {
1908 sec = (time_t) (t / USEC_PER_SEC);
1910 if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0)
1916 char *format_timespan(char *buf, size_t l, usec_t t) {
1917 static const struct {
1921 { "w", USEC_PER_WEEK },
1922 { "d", USEC_PER_DAY },
1923 { "h", USEC_PER_HOUR },
1924 { "min", USEC_PER_MINUTE },
1925 { "s", USEC_PER_SEC },
1926 { "ms", USEC_PER_MSEC },
1936 if (t == (usec_t) -1)
1940 snprintf(p, l, "0");
1945 /* The result of this function can be parsed with parse_usec */
1947 for (i = 0; i < ELEMENTSOF(table); i++) {
1951 if (t < table[i].usec)
1957 k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix);
1958 n = MIN((size_t) k, l);
1971 bool fstype_is_network(const char *fstype) {
1972 static const char * const table[] = {
1984 for (i = 0; i < ELEMENTSOF(table); i++)
1985 if (streq(table[i], fstype))
1994 if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
1999 TIOCL_GETKMSGREDIRECT,
2003 if (ioctl(fd, TIOCLINUX, tiocl) < 0)
2006 vt = tiocl[0] <= 0 ? 1 : tiocl[0];
2009 if (ioctl(fd, VT_ACTIVATE, vt) < 0)
2012 close_nointr_nofail(r);
2016 int read_one_char(FILE *f, char *ret, bool *need_nl) {
2017 struct termios old_termios, new_termios;
2024 if (tcgetattr(fileno(f), &old_termios) >= 0) {
2025 new_termios = old_termios;
2027 new_termios.c_lflag &= ~ICANON;
2028 new_termios.c_cc[VMIN] = 1;
2029 new_termios.c_cc[VTIME] = 0;
2031 if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
2034 k = fread(&c, 1, 1, f);
2036 tcsetattr(fileno(f), TCSADRAIN, &old_termios);
2042 *need_nl = c != '\n';
2049 if (!(fgets(line, sizeof(line), f)))
2054 if (strlen(line) != 1)
2064 int ask(char *ret, const char *replies, const char *text, ...) {
2073 bool need_nl = true;
2075 fputs("\x1B[1m", stdout);
2081 fputs("\x1B[0m", stdout);
2085 if ((r = read_one_char(stdin, &c, &need_nl)) < 0) {
2087 if (r == -EBADMSG) {
2088 puts("Bad input, please try again.");
2099 if (strchr(replies, c)) {
2104 puts("Read unexpected character, please try again.");
2108 int reset_terminal(int fd) {
2109 struct termios termios;
2113 /* Set terminal to some sane defaults */
2117 /* First, unlock termios */
2119 ioctl(fd, TIOCSLCKTRMIOS, &termios);
2121 /* Disable exclusive mode, just in case */
2122 ioctl(fd, TIOCNXCL);
2124 /* Enable console unicode mode */
2126 ioctl(fd, KDSKBMODE, &arg);
2128 if (tcgetattr(fd, &termios) < 0) {
2133 /* We only reset the stuff that matters to the software. How
2134 * hardware is set up we don't touch assuming that somebody
2135 * else will do that for us */
2137 termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
2138 termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
2139 termios.c_oflag |= ONLCR;
2140 termios.c_cflag |= CREAD;
2141 termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
2143 termios.c_cc[VINTR] = 03; /* ^C */
2144 termios.c_cc[VQUIT] = 034; /* ^\ */
2145 termios.c_cc[VERASE] = 0177;
2146 termios.c_cc[VKILL] = 025; /* ^X */
2147 termios.c_cc[VEOF] = 04; /* ^D */
2148 termios.c_cc[VSTART] = 021; /* ^Q */
2149 termios.c_cc[VSTOP] = 023; /* ^S */
2150 termios.c_cc[VSUSP] = 032; /* ^Z */
2151 termios.c_cc[VLNEXT] = 026; /* ^V */
2152 termios.c_cc[VWERASE] = 027; /* ^W */
2153 termios.c_cc[VREPRINT] = 022; /* ^R */
2154 termios.c_cc[VEOL] = 0;
2155 termios.c_cc[VEOL2] = 0;
2157 termios.c_cc[VTIME] = 0;
2158 termios.c_cc[VMIN] = 1;
2160 if (tcsetattr(fd, TCSANOW, &termios) < 0)
2164 /* Just in case, flush all crap out */
2165 tcflush(fd, TCIOFLUSH);
2170 int open_terminal(const char *name, int mode) {
2173 if ((fd = open(name, mode)) < 0)
2176 if ((r = isatty(fd)) < 0) {
2177 close_nointr_nofail(fd);
2182 close_nointr_nofail(fd);
2189 int flush_fd(int fd) {
2190 struct pollfd pollfd;
2194 pollfd.events = POLLIN;
2201 if ((r = poll(&pollfd, 1, 0)) < 0) {
2212 if ((l = read(fd, buf, sizeof(buf))) < 0) {
2217 if (errno == EAGAIN)
2228 int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) {
2229 int fd = -1, notify = -1, r, wd = -1;
2233 /* We use inotify to be notified when the tty is closed. We
2234 * create the watch before checking if we can actually acquire
2235 * it, so that we don't lose any event.
2237 * Note: strictly speaking this actually watches for the
2238 * device being closed, it does *not* really watch whether a
2239 * tty loses its controlling process. However, unless some
2240 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
2241 * its tty otherwise this will not become a problem. As long
2242 * as the administrator makes sure not configure any service
2243 * on the same tty as an untrusted user this should not be a
2244 * problem. (Which he probably should not do anyway.) */
2246 if (!fail && !force) {
2247 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
2252 if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) {
2260 if ((r = flush_fd(notify)) < 0)
2263 /* We pass here O_NOCTTY only so that we can check the return
2264 * value TIOCSCTTY and have a reliable way to figure out if we
2265 * successfully became the controlling process of the tty */
2266 if ((fd = open_terminal(name, O_RDWR|O_NOCTTY)) < 0)
2269 /* First, try to get the tty */
2270 r = ioctl(fd, TIOCSCTTY, force);
2272 /* Sometimes it makes sense to ignore TIOCSCTTY
2273 * returning EPERM, i.e. when very likely we already
2274 * are have this controlling terminal. */
2275 if (r < 0 && errno == EPERM && ignore_tiocstty_eperm)
2278 if (r < 0 && (force || fail || errno != EPERM)) {
2288 assert(notify >= 0);
2291 struct inotify_event e;
2294 if ((l = read(notify, &e, sizeof(e))) != sizeof(e)) {
2308 if (e.wd != wd || !(e.mask & IN_CLOSE)) {
2316 /* We close the tty fd here since if the old session
2317 * ended our handle will be dead. It's important that
2318 * we do this after sleeping, so that we don't enter
2319 * an endless loop. */
2320 close_nointr_nofail(fd);
2324 close_nointr_nofail(notify);
2326 if ((r = reset_terminal(fd)) < 0)
2327 log_warning("Failed to reset terminal: %s", strerror(-r));
2333 close_nointr_nofail(fd);
2336 close_nointr_nofail(notify);
2341 int release_terminal(void) {
2343 struct sigaction sa_old, sa_new;
2345 if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
2348 /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
2349 * by our own TIOCNOTTY */
2352 sa_new.sa_handler = SIG_IGN;
2353 sa_new.sa_flags = SA_RESTART;
2354 assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
2356 if (ioctl(fd, TIOCNOTTY) < 0)
2359 assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
2361 close_nointr_nofail(fd);
2365 int sigaction_many(const struct sigaction *sa, ...) {
2370 while ((sig = va_arg(ap, int)) > 0)
2371 if (sigaction(sig, sa, NULL) < 0)
2378 int ignore_signals(int sig, ...) {
2379 struct sigaction sa;
2384 sa.sa_handler = SIG_IGN;
2385 sa.sa_flags = SA_RESTART;
2387 if (sigaction(sig, &sa, NULL) < 0)
2391 while ((sig = va_arg(ap, int)) > 0)
2392 if (sigaction(sig, &sa, NULL) < 0)
2399 int default_signals(int sig, ...) {
2400 struct sigaction sa;
2405 sa.sa_handler = SIG_DFL;
2406 sa.sa_flags = SA_RESTART;
2408 if (sigaction(sig, &sa, NULL) < 0)
2412 while ((sig = va_arg(ap, int)) > 0)
2413 if (sigaction(sig, &sa, NULL) < 0)
2420 int close_pipe(int p[]) {
2426 a = close_nointr(p[0]);
2431 b = close_nointr(p[1]);
2435 return a < 0 ? a : b;
2438 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
2447 while (nbytes > 0) {
2450 if ((k = read(fd, p, nbytes)) <= 0) {
2452 if (k < 0 && errno == EINTR)
2455 if (k < 0 && errno == EAGAIN && do_poll) {
2456 struct pollfd pollfd;
2460 pollfd.events = POLLIN;
2462 if (poll(&pollfd, 1, -1) < 0) {
2466 return n > 0 ? n : -errno;
2469 if (pollfd.revents != POLLIN)
2470 return n > 0 ? n : -EIO;
2475 return n > 0 ? n : (k < 0 ? -errno : 0);
2486 ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
2495 while (nbytes > 0) {
2498 if ((k = write(fd, p, nbytes)) <= 0) {
2500 if (k < 0 && errno == EINTR)
2503 if (k < 0 && errno == EAGAIN && do_poll) {
2504 struct pollfd pollfd;
2508 pollfd.events = POLLOUT;
2510 if (poll(&pollfd, 1, -1) < 0) {
2514 return n > 0 ? n : -errno;
2517 if (pollfd.revents != POLLOUT)
2518 return n > 0 ? n : -EIO;
2523 return n > 0 ? n : (k < 0 ? -errno : 0);
2534 int path_is_mount_point(const char *t) {
2539 if (lstat(t, &a) < 0) {
2540 if (errno == ENOENT)
2546 if ((r = parent_of_path(t, &parent)) < 0)
2549 r = lstat(parent, &b);
2555 return a.st_dev != b.st_dev;
2558 int parse_usec(const char *t, usec_t *usec) {
2559 static const struct {
2563 { "sec", USEC_PER_SEC },
2564 { "s", USEC_PER_SEC },
2565 { "min", USEC_PER_MINUTE },
2566 { "hr", USEC_PER_HOUR },
2567 { "h", USEC_PER_HOUR },
2568 { "d", USEC_PER_DAY },
2569 { "w", USEC_PER_WEEK },
2570 { "msec", USEC_PER_MSEC },
2571 { "ms", USEC_PER_MSEC },
2572 { "m", USEC_PER_MINUTE },
2575 { "", USEC_PER_SEC },
2591 l = strtoll(p, &e, 10);
2602 e += strspn(e, WHITESPACE);
2604 for (i = 0; i < ELEMENTSOF(table); i++)
2605 if (startswith(e, table[i].suffix)) {
2606 r += (usec_t) l * table[i].usec;
2607 p = e + strlen(table[i].suffix);
2611 if (i >= ELEMENTSOF(table))
2621 int make_stdio(int fd) {
2626 r = dup2(fd, STDIN_FILENO);
2627 s = dup2(fd, STDOUT_FILENO);
2628 t = dup2(fd, STDERR_FILENO);
2631 close_nointr_nofail(fd);
2633 if (r < 0 || s < 0 || t < 0)
2639 bool is_clean_exit(int code, int status) {
2641 if (code == CLD_EXITED)
2644 /* If a daemon does not implement handlers for some of the
2645 * signals that's not considered an unclean shutdown */
2646 if (code == CLD_KILLED)
2650 status == SIGTERM ||
2656 bool is_device_path(const char *path) {
2658 /* Returns true on paths that refer to a device, either in
2659 * sysfs or in /dev */
2662 path_startswith(path, "/dev/") ||
2663 path_startswith(path, "/sys/");
2666 int dir_is_empty(const char *path) {
2669 struct dirent buf, *de;
2671 if (!(d = opendir(path)))
2675 if ((r = readdir_r(d, &buf, &de)) > 0) {
2685 if (!ignore_file(de->d_name)) {
2695 unsigned long long random_ull(void) {
2700 if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0)
2703 r = loop_read(fd, &ull, sizeof(ull), true);
2704 close_nointr_nofail(fd);
2706 if (r != sizeof(ull))
2712 return random() * RAND_MAX + random();
2715 void rename_process(const char name[8]) {
2718 prctl(PR_SET_NAME, name);
2720 /* This is a like a poor man's setproctitle(). The string
2721 * passed should fit in 7 chars (i.e. the length of
2724 if (program_invocation_name)
2725 strncpy(program_invocation_name, name, strlen(program_invocation_name));
2728 void sigset_add_many(sigset_t *ss, ...) {
2735 while ((sig = va_arg(ap, int)) > 0)
2736 assert_se(sigaddset(ss, sig) == 0);
2740 char* gethostname_malloc(void) {
2743 assert_se(uname(&u) >= 0);
2746 return strdup(u.nodename);
2748 return strdup(u.sysname);
2751 int getmachineid_malloc(char **b) {
2756 if ((r = read_one_line_file("/var/lib/dbus/machine-id", b)) < 0)
2763 char* getlogname_malloc(void) {
2767 struct passwd pwbuf, *pw = NULL;
2770 if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
2775 /* Shortcut things to avoid NSS lookups */
2777 return strdup("root");
2779 if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0)
2782 if (!(buf = malloc(bufsize)))
2785 if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) {
2786 name = strdup(pw->pw_name);
2793 if (asprintf(&name, "%lu", (unsigned long) uid) < 0)
2799 int getttyname_malloc(char **r) {
2800 char path[PATH_MAX], *p, *c;
2804 if (ttyname_r(STDIN_FILENO, path, sizeof(path)) < 0)
2810 if (startswith(path, "/dev/"))
2813 if (!(c = strdup(p)))
2820 static int rm_rf_children(int fd, bool only_dirs) {
2826 /* This returns the first error we run into, but nevertheless
2829 if (!(d = fdopendir(fd))) {
2830 close_nointr_nofail(fd);
2832 return errno == ENOENT ? 0 : -errno;
2836 struct dirent buf, *de;
2840 if ((r = readdir_r(d, &buf, &de)) != 0) {
2849 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
2852 if (de->d_type == DT_UNKNOWN) {
2855 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
2856 if (ret == 0 && errno != ENOENT)
2861 is_dir = S_ISDIR(st.st_mode);
2863 is_dir = de->d_type == DT_DIR;
2868 if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
2869 if (ret == 0 && errno != ENOENT)
2874 if ((r = rm_rf_children(subdir_fd, only_dirs)) < 0) {
2879 if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
2880 if (ret == 0 && errno != ENOENT)
2883 } else if (!only_dirs) {
2885 if (unlinkat(fd, de->d_name, 0) < 0) {
2886 if (ret == 0 && errno != ENOENT)
2897 int rm_rf(const char *path, bool only_dirs, bool delete_root) {
2903 if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
2905 if (errno != ENOTDIR)
2908 if (delete_root && !only_dirs)
2909 if (unlink(path) < 0)
2915 r = rm_rf_children(fd, only_dirs);
2918 if (rmdir(path) < 0) {
2926 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
2929 /* Under the assumption that we are running privileged we
2930 * first change the access mode and only then hand out
2931 * ownership to avoid a window where access is too open. */
2933 if (chmod(path, mode) < 0)
2936 if (chown(path, uid, gid) < 0)
2942 cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
2946 /* Allocates the cpuset in the right size */
2949 if (!(r = CPU_ALLOC(n)))
2952 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) {
2953 CPU_ZERO_S(CPU_ALLOC_SIZE(n), r);
2963 if (errno != EINVAL)
2970 void status_vprintf(const char *format, va_list ap) {
2976 /* This independent of logging, as status messages are
2977 * optional and go exclusively to the console. */
2979 if (vasprintf(&s, format, ap) < 0)
2982 if ((fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
2985 write(fd, s, strlen(s));
2991 close_nointr_nofail(fd);
2994 void status_printf(const char *format, ...) {
2999 va_start(ap, format);
3000 status_vprintf(format, ap);
3004 void status_welcome(void) {
3006 #if defined(TARGET_FEDORA)
3009 if (read_one_line_file("/etc/system-release", &r) < 0)
3014 /* This tries to mimic the color magic the old Red Hat sysinit
3017 if (startswith(r, "Red Hat"))
3018 status_printf("Welcome to \x1B[0;31m%s\x1B[0m!\n", r); /* Red for RHEL */
3019 else if (startswith(r, "Fedora"))
3020 status_printf("Welcome to \x1B[0;34m%s\x1B[0m!\n", r); /* Blue for Fedora */
3022 status_printf("Welcome to %s!\n", r);
3026 #elif defined(TARGET_SUSE)
3029 if (read_one_line_file("/etc/SuSE-release", &r) < 0)
3034 status_printf("Welcome to \x1B[0;32m%s\x1B[0m!\n", r); /* Green for SUSE */
3037 #warning "You probably should add a welcome text logic here."
3041 char *replace_env(const char *format, char **env) {
3048 const char *e, *word = format;
3053 for (e = format; *e; e ++) {
3064 if (!(k = strnappend(r, word, e-word-1)))
3073 } else if (*e == '$') {
3074 if (!(k = strnappend(r, word, e-word)))
3090 if ((t = strv_env_get_with_length(env, word+2, e-word-2))) {
3091 if (!(k = strappend(r, t)))
3106 if (!(k = strnappend(r, word, e-word)))
3117 char **replace_env_argv(char **argv, char **env) {
3119 unsigned k = 0, l = 0;
3121 l = strv_length(argv);
3123 if (!(r = new(char*, l+1)))
3126 STRV_FOREACH(i, argv) {
3128 /* If $FOO appears as single word, replace it by the split up variable */
3129 if ((*i)[0] == '$') {
3130 char *e = strv_env_get(env, *i+1);
3136 if (!(m = strv_split_quoted(e))) {
3145 if (!(w = realloc(r, sizeof(char*) * (l+1)))) {
3153 memcpy(r + k, m, q * sizeof(char*));
3161 /* If ${FOO} appears as part of a word, replace it by the variable as-is */
3162 if (!(r[k++] = replace_env(*i, env))) {
3173 static __thread int parsed_columns = 0;
3176 if (parsed_columns > 0)
3177 return parsed_columns;
3179 if ((e = getenv("COLUMNS")))
3180 parsed_columns = atoi(e);
3182 if (parsed_columns <= 0) {
3186 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
3187 parsed_columns = ws.ws_col;
3190 if (parsed_columns <= 0)
3191 parsed_columns = 80;
3193 return parsed_columns;
3196 int running_in_chroot(void) {
3202 /* Only works as root */
3204 if (stat("/proc/1/root", &a) < 0)
3207 if (stat("/", &b) < 0)
3211 a.st_dev != b.st_dev ||
3212 a.st_ino != b.st_ino;
3215 char *ellipsize(const char *s, unsigned length, unsigned percent) {
3220 assert(percent <= 100);
3221 assert(length >= 3);
3225 if (l <= 3 || l <= length)
3228 if (!(r = new0(char, length+1)))
3231 x = (length * percent) / 100;
3241 s + l - (length - x - 3),
3247 static const char *const ioprio_class_table[] = {
3248 [IOPRIO_CLASS_NONE] = "none",
3249 [IOPRIO_CLASS_RT] = "realtime",
3250 [IOPRIO_CLASS_BE] = "best-effort",
3251 [IOPRIO_CLASS_IDLE] = "idle"
3254 DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
3256 static const char *const sigchld_code_table[] = {
3257 [CLD_EXITED] = "exited",
3258 [CLD_KILLED] = "killed",
3259 [CLD_DUMPED] = "dumped",
3260 [CLD_TRAPPED] = "trapped",
3261 [CLD_STOPPED] = "stopped",
3262 [CLD_CONTINUED] = "continued",
3265 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
3267 static const char *const log_facility_table[LOG_NFACILITIES] = {
3268 [LOG_FAC(LOG_KERN)] = "kern",
3269 [LOG_FAC(LOG_USER)] = "user",
3270 [LOG_FAC(LOG_MAIL)] = "mail",
3271 [LOG_FAC(LOG_DAEMON)] = "daemon",
3272 [LOG_FAC(LOG_AUTH)] = "auth",
3273 [LOG_FAC(LOG_SYSLOG)] = "syslog",
3274 [LOG_FAC(LOG_LPR)] = "lpr",
3275 [LOG_FAC(LOG_NEWS)] = "news",
3276 [LOG_FAC(LOG_UUCP)] = "uucp",
3277 [LOG_FAC(LOG_CRON)] = "cron",
3278 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
3279 [LOG_FAC(LOG_FTP)] = "ftp",
3280 [LOG_FAC(LOG_LOCAL0)] = "local0",
3281 [LOG_FAC(LOG_LOCAL1)] = "local1",
3282 [LOG_FAC(LOG_LOCAL2)] = "local2",
3283 [LOG_FAC(LOG_LOCAL3)] = "local3",
3284 [LOG_FAC(LOG_LOCAL4)] = "local4",
3285 [LOG_FAC(LOG_LOCAL5)] = "local5",
3286 [LOG_FAC(LOG_LOCAL6)] = "local6",
3287 [LOG_FAC(LOG_LOCAL7)] = "local7"
3290 DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
3292 static const char *const log_level_table[] = {
3293 [LOG_EMERG] = "emerg",
3294 [LOG_ALERT] = "alert",
3295 [LOG_CRIT] = "crit",
3297 [LOG_WARNING] = "warning",
3298 [LOG_NOTICE] = "notice",
3299 [LOG_INFO] = "info",
3300 [LOG_DEBUG] = "debug"
3303 DEFINE_STRING_TABLE_LOOKUP(log_level, int);
3305 static const char* const sched_policy_table[] = {
3306 [SCHED_OTHER] = "other",
3307 [SCHED_BATCH] = "batch",
3308 [SCHED_IDLE] = "idle",
3309 [SCHED_FIFO] = "fifo",
3313 DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
3315 static const char* const rlimit_table[] = {
3316 [RLIMIT_CPU] = "LimitCPU",
3317 [RLIMIT_FSIZE] = "LimitFSIZE",
3318 [RLIMIT_DATA] = "LimitDATA",
3319 [RLIMIT_STACK] = "LimitSTACK",
3320 [RLIMIT_CORE] = "LimitCORE",
3321 [RLIMIT_RSS] = "LimitRSS",
3322 [RLIMIT_NOFILE] = "LimitNOFILE",
3323 [RLIMIT_AS] = "LimitAS",
3324 [RLIMIT_NPROC] = "LimitNPROC",
3325 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
3326 [RLIMIT_LOCKS] = "LimitLOCKS",
3327 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
3328 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
3329 [RLIMIT_NICE] = "LimitNICE",
3330 [RLIMIT_RTPRIO] = "LimitRTPRIO",
3331 [RLIMIT_RTTIME] = "LimitRTTIME"
3334 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
3336 static const char* const ip_tos_table[] = {
3337 [IPTOS_LOWDELAY] = "low-delay",
3338 [IPTOS_THROUGHPUT] = "throughput",
3339 [IPTOS_RELIABILITY] = "reliability",
3340 [IPTOS_LOWCOST] = "low-cost",
3343 DEFINE_STRING_TABLE_LOOKUP(ip_tos, int);
3345 static const char *const signal_table[] = {
3361 [SIGSTKFLT] = "STKFLT",
3371 [SIGVTALRM] = "VTALRM",
3373 [SIGWINCH] = "WINCH",
3379 DEFINE_STRING_TABLE_LOOKUP(signal, int);