]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/util.c
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>
60 #include "exit-status.h"
62 bool streq_ptr(const char *a
, const char *b
) {
64 /* Like streq(), but tries to make sense of NULL pointers */
75 usec_t
now(clockid_t clock_id
) {
78 assert_se(clock_gettime(clock_id
, &ts
) == 0);
80 return timespec_load(&ts
);
83 dual_timestamp
* dual_timestamp_get(dual_timestamp
*ts
) {
86 ts
->realtime
= now(CLOCK_REALTIME
);
87 ts
->monotonic
= now(CLOCK_MONOTONIC
);
92 usec_t
timespec_load(const struct timespec
*ts
) {
96 (usec_t
) ts
->tv_sec
* USEC_PER_SEC
+
97 (usec_t
) ts
->tv_nsec
/ NSEC_PER_USEC
;
100 struct timespec
*timespec_store(struct timespec
*ts
, usec_t u
) {
103 ts
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
104 ts
->tv_nsec
= (long int) ((u
% USEC_PER_SEC
) * NSEC_PER_USEC
);
109 usec_t
timeval_load(const struct timeval
*tv
) {
113 (usec_t
) tv
->tv_sec
* USEC_PER_SEC
+
114 (usec_t
) tv
->tv_usec
;
117 struct timeval
*timeval_store(struct timeval
*tv
, usec_t u
) {
120 tv
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
121 tv
->tv_usec
= (suseconds_t
) (u
% USEC_PER_SEC
);
126 bool endswith(const char *s
, const char *postfix
) {
133 pl
= strlen(postfix
);
141 return memcmp(s
+ sl
- pl
, postfix
, pl
) == 0;
144 bool startswith(const char *s
, const char *prefix
) {
159 return memcmp(s
, prefix
, pl
) == 0;
162 bool startswith_no_case(const char *s
, const char *prefix
) {
178 for(i
= 0; i
< pl
; ++i
) {
179 if (tolower(s
[i
]) != tolower(prefix
[i
]))
186 bool first_word(const char *s
, const char *word
) {
201 if (memcmp(s
, word
, wl
) != 0)
205 strchr(WHITESPACE
, s
[wl
]);
208 int close_nointr(int fd
) {
214 if ((r
= close(fd
)) >= 0)
222 void close_nointr_nofail(int fd
) {
223 int saved_errno
= errno
;
225 /* like close_nointr() but cannot fail, and guarantees errno
228 assert_se(close_nointr(fd
) == 0);
233 void close_many(const int fds
[], unsigned n_fd
) {
236 for (i
= 0; i
< n_fd
; i
++)
237 close_nointr_nofail(fds
[i
]);
240 int parse_boolean(const char *v
) {
243 if (streq(v
, "1") || v
[0] == 'y' || v
[0] == 'Y' || v
[0] == 't' || v
[0] == 'T' || !strcasecmp(v
, "on"))
245 else if (streq(v
, "0") || v
[0] == 'n' || v
[0] == 'N' || v
[0] == 'f' || v
[0] == 'F' || !strcasecmp(v
, "off"))
251 int parse_pid(const char *s
, pid_t
* ret_pid
) {
259 if ((r
= safe_atolu(s
, &ul
)) < 0)
264 if ((unsigned long) pid
!= ul
)
274 int safe_atou(const char *s
, unsigned *ret_u
) {
282 l
= strtoul(s
, &x
, 0);
284 if (!x
|| *x
|| errno
)
285 return errno
? -errno
: -EINVAL
;
287 if ((unsigned long) (unsigned) l
!= l
)
290 *ret_u
= (unsigned) l
;
294 int safe_atoi(const char *s
, int *ret_i
) {
302 l
= strtol(s
, &x
, 0);
304 if (!x
|| *x
|| errno
)
305 return errno
? -errno
: -EINVAL
;
307 if ((long) (int) l
!= l
)
314 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
316 unsigned long long l
;
322 l
= strtoull(s
, &x
, 0);
324 if (!x
|| *x
|| errno
)
325 return errno
? -errno
: -EINVAL
;
331 int safe_atolli(const char *s
, long long int *ret_lli
) {
339 l
= strtoll(s
, &x
, 0);
341 if (!x
|| *x
|| errno
)
342 return errno
? -errno
: -EINVAL
;
348 /* Split a string into words. */
349 char *split(const char *c
, size_t *l
, const char *separator
, char **state
) {
352 current
= *state
? *state
: (char*) c
;
354 if (!*current
|| *c
== 0)
357 current
+= strspn(current
, separator
);
358 *l
= strcspn(current
, separator
);
361 return (char*) current
;
364 /* Split a string into words, but consider strings enclosed in '' and
365 * "" as words even if they include spaces. */
366 char *split_quoted(const char *c
, size_t *l
, char **state
) {
368 bool escaped
= false;
370 current
= *state
? *state
: (char*) c
;
372 if (!*current
|| *c
== 0)
375 current
+= strspn(current
, WHITESPACE
);
377 if (*current
== '\'') {
380 for (e
= current
; *e
; e
++) {
390 *state
= *e
== 0 ? e
: e
+1;
391 } else if (*current
== '\"') {
394 for (e
= current
; *e
; e
++) {
404 *state
= *e
== 0 ? e
: e
+1;
406 for (e
= current
; *e
; e
++) {
411 else if (strchr(WHITESPACE
, *e
))
418 return (char*) current
;
421 char **split_path_and_make_absolute(const char *p
) {
425 if (!(l
= strv_split(p
, ":")))
428 if (!strv_path_make_absolute_cwd(l
)) {
436 int get_parent_of_pid(pid_t pid
, pid_t
*_ppid
) {
439 char fn
[132], line
[256], *p
;
445 assert_se(snprintf(fn
, sizeof(fn
)-1, "/proc/%lu/stat", (unsigned long) pid
) < (int) (sizeof(fn
)-1));
446 fn
[sizeof(fn
)-1] = 0;
448 if (!(f
= fopen(fn
, "r")))
451 if (!(fgets(line
, sizeof(line
), f
))) {
459 /* Let's skip the pid and comm fields. The latter is enclosed
460 * in () but does not escape any () in its value, so let's
461 * skip over it manually */
463 if (!(p
= strrchr(line
, ')')))
474 if ((long unsigned) (pid_t
) ppid
!= ppid
)
477 *_ppid
= (pid_t
) ppid
;
482 int write_one_line_file(const char *fn
, const char *line
) {
489 if (!(f
= fopen(fn
, "we")))
492 if (fputs(line
, f
) < 0) {
503 int read_one_line_file(const char *fn
, char **line
) {
511 if (!(f
= fopen(fn
, "re")))
514 if (!(fgets(t
, sizeof(t
), f
))) {
519 if (!(c
= strdup(t
))) {
532 char *truncate_nl(char *s
) {
535 s
[strcspn(s
, NEWLINE
)] = 0;
539 int get_process_name(pid_t pid
, char **name
) {
546 if (asprintf(&p
, "/proc/%lu/comm", (unsigned long) pid
) < 0)
549 r
= read_one_line_file(p
, name
);
559 int get_process_cmdline(pid_t pid
, size_t max_length
, char **line
) {
567 assert(max_length
> 0);
570 if (asprintf(&p
, "/proc/%lu/cmdline", (unsigned long) pid
) < 0)
579 if (!(r
= new(char, max_length
))) {
586 while ((c
= getc(f
)) != EOF
) {
608 size_t n
= MIN(left
-1, 3U);
616 /* Kernel threads have no argv[] */
623 if ((h
= get_process_name(pid
, &t
)) < 0)
626 h
= asprintf(&r
, "[%s]", t
);
637 char *strnappend(const char *s
, const char *suffix
, size_t b
) {
645 return strndup(suffix
, b
);
655 if (!(r
= new(char, a
+b
+1)))
659 memcpy(r
+a
, suffix
, b
);
665 char *strappend(const char *s
, const char *suffix
) {
666 return strnappend(s
, suffix
, suffix
? strlen(suffix
) : 0);
669 int readlink_malloc(const char *p
, char **r
) {
679 if (!(c
= new(char, l
)))
682 if ((n
= readlink(p
, c
, l
-1)) < 0) {
688 if ((size_t) n
< l
-1) {
699 int readlink_and_make_absolute(const char *p
, char **r
) {
706 if ((j
= readlink_malloc(p
, &target
)) < 0)
709 k
= file_in_same_dir(p
, target
);
719 int parent_of_path(const char *path
, char **_r
) {
720 const char *e
, *a
= NULL
, *b
= NULL
, *p
;
730 for (e
= path
; *e
; e
++) {
732 if (!slash
&& *e
== '/') {
736 } else if (slash
&& *e
!= '/')
751 r
= strndup(path
, p
-path
);
761 char *file_name_from_path(const char *p
) {
766 if ((r
= strrchr(p
, '/')))
772 bool path_is_absolute(const char *p
) {
778 bool is_path(const char *p
) {
780 return !!strchr(p
, '/');
783 char *path_make_absolute(const char *p
, const char *prefix
) {
788 /* Makes every item in the list an absolute path by prepending
789 * the prefix, if specified and necessary */
791 if (path_is_absolute(p
) || !prefix
)
794 if (asprintf(&r
, "%s/%s", prefix
, p
) < 0)
800 char *path_make_absolute_cwd(const char *p
) {
805 /* Similar to path_make_absolute(), but prefixes with the
806 * current working directory. */
808 if (path_is_absolute(p
))
811 if (!(cwd
= get_current_dir_name()))
814 r
= path_make_absolute(p
, cwd
);
820 char **strv_path_make_absolute_cwd(char **l
) {
823 /* Goes through every item in the string list and makes it
824 * absolute. This works in place and won't rollback any
825 * changes on failure. */
830 if (!(t
= path_make_absolute_cwd(*s
)))
840 char **strv_path_canonicalize(char **l
) {
848 /* Goes through every item in the string list and canonicalize
849 * the path. This works in place and won't rollback any
850 * changes on failure. */
855 t
= path_make_absolute_cwd(*s
);
864 u
= canonicalize_file_name(t
);
868 if (errno
== ENOMEM
|| !errno
)
885 int reset_all_signal_handlers(void) {
888 for (sig
= 1; sig
< _NSIG
; sig
++) {
891 if (sig
== SIGKILL
|| sig
== SIGSTOP
)
895 sa
.sa_handler
= SIG_DFL
;
896 sa
.sa_flags
= SA_RESTART
;
898 /* On Linux the first two RT signals are reserved by
899 * glibc, and sigaction() will return EINVAL for them. */
900 if ((sigaction(sig
, &sa
, NULL
) < 0))
908 char *strstrip(char *s
) {
911 /* Drops trailing whitespace. Modifies the string in
912 * place. Returns pointer to first non-space character */
914 s
+= strspn(s
, WHITESPACE
);
917 if (!strchr(WHITESPACE
, *e
))
928 char *delete_chars(char *s
, const char *bad
) {
931 /* Drops all whitespace, regardless where in the string */
933 for (f
= s
, t
= s
; *f
; f
++) {
945 char *file_in_same_dir(const char *path
, const char *filename
) {
952 /* This removes the last component of path and appends
953 * filename, unless the latter is absolute anyway or the
956 if (path_is_absolute(filename
))
957 return strdup(filename
);
959 if (!(e
= strrchr(path
, '/')))
960 return strdup(filename
);
962 k
= strlen(filename
);
963 if (!(r
= new(char, e
-path
+1+k
+1)))
966 memcpy(r
, path
, e
-path
+1);
967 memcpy(r
+(e
-path
)+1, filename
, k
+1);
972 int safe_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
) {
975 if (label_mkdir(path
, mode
) >= 0)
976 if (chmod_and_chown(path
, mode
, uid
, gid
) < 0)
979 if (lstat(path
, &st
) < 0)
982 if ((st
.st_mode
& 0777) != mode
||
985 !S_ISDIR(st
.st_mode
)) {
994 int mkdir_parents(const char *path
, mode_t mode
) {
999 /* Creates every parent directory in the path except the last
1002 p
= path
+ strspn(path
, "/");
1007 e
= p
+ strcspn(p
, "/");
1008 p
= e
+ strspn(e
, "/");
1010 /* Is this the last component? If so, then we're
1015 if (!(t
= strndup(path
, e
- path
)))
1018 r
= label_mkdir(t
, mode
);
1021 if (r
< 0 && errno
!= EEXIST
)
1026 int mkdir_p(const char *path
, mode_t mode
) {
1031 if ((r
= mkdir_parents(path
, mode
)) < 0)
1034 if (label_mkdir(path
, mode
) < 0 && errno
!= EEXIST
)
1040 int rmdir_parents(const char *path
, const char *stop
) {
1049 /* Skip trailing slashes */
1050 while (l
> 0 && path
[l
-1] == '/')
1056 /* Skip last component */
1057 while (l
> 0 && path
[l
-1] != '/')
1060 /* Skip trailing slashes */
1061 while (l
> 0 && path
[l
-1] == '/')
1067 if (!(t
= strndup(path
, l
)))
1070 if (path_startswith(stop
, t
)) {
1079 if (errno
!= ENOENT
)
1087 char hexchar(int x
) {
1088 static const char table
[16] = "0123456789abcdef";
1090 return table
[x
& 15];
1093 int unhexchar(char c
) {
1095 if (c
>= '0' && c
<= '9')
1098 if (c
>= 'a' && c
<= 'f')
1099 return c
- 'a' + 10;
1101 if (c
>= 'A' && c
<= 'F')
1102 return c
- 'A' + 10;
1107 char octchar(int x
) {
1108 return '0' + (x
& 7);
1111 int unoctchar(char c
) {
1113 if (c
>= '0' && c
<= '7')
1119 char decchar(int x
) {
1120 return '0' + (x
% 10);
1123 int undecchar(char c
) {
1125 if (c
>= '0' && c
<= '9')
1131 char *cescape(const char *s
) {
1137 /* Does C style string escaping. */
1139 if (!(r
= new(char, strlen(s
)*4 + 1)))
1142 for (f
= s
, t
= r
; *f
; f
++)
1188 /* For special chars we prefer octal over
1189 * hexadecimal encoding, simply because glib's
1190 * g_strescape() does the same */
1191 if ((*f
< ' ') || (*f
>= 127)) {
1193 *(t
++) = octchar((unsigned char) *f
>> 6);
1194 *(t
++) = octchar((unsigned char) *f
>> 3);
1195 *(t
++) = octchar((unsigned char) *f
);
1206 char *cunescape_length(const char *s
, size_t length
) {
1212 /* Undoes C style string escaping */
1214 if (!(r
= new(char, length
+1)))
1217 for (f
= s
, t
= r
; f
< s
+ length
; f
++) {
1260 /* This is an extension of the XDG syntax files */
1265 /* hexadecimal encoding */
1268 if ((a
= unhexchar(f
[1])) < 0 ||
1269 (b
= unhexchar(f
[2])) < 0) {
1270 /* Invalid escape code, let's take it literal then */
1274 *(t
++) = (char) ((a
<< 4) | b
);
1289 /* octal encoding */
1292 if ((a
= unoctchar(f
[0])) < 0 ||
1293 (b
= unoctchar(f
[1])) < 0 ||
1294 (c
= unoctchar(f
[2])) < 0) {
1295 /* Invalid escape code, let's take it literal then */
1299 *(t
++) = (char) ((a
<< 6) | (b
<< 3) | c
);
1307 /* premature end of string.*/
1312 /* Invalid escape code, let's take it literal then */
1324 char *cunescape(const char *s
) {
1325 return cunescape_length(s
, strlen(s
));
1328 char *xescape(const char *s
, const char *bad
) {
1332 /* Escapes all chars in bad, in addition to \ and all special
1333 * chars, in \xFF style escaping. May be reversed with
1336 if (!(r
= new(char, strlen(s
)*4+1)))
1339 for (f
= s
, t
= r
; *f
; f
++) {
1341 if ((*f
< ' ') || (*f
>= 127) ||
1342 (*f
== '\\') || strchr(bad
, *f
)) {
1345 *(t
++) = hexchar(*f
>> 4);
1346 *(t
++) = hexchar(*f
);
1356 char *bus_path_escape(const char *s
) {
1362 /* Escapes all chars that D-Bus' object path cannot deal
1363 * with. Can be reverse with bus_path_unescape() */
1365 if (!(r
= new(char, strlen(s
)*3+1)))
1368 for (f
= s
, t
= r
; *f
; f
++) {
1370 if (!(*f
>= 'A' && *f
<= 'Z') &&
1371 !(*f
>= 'a' && *f
<= 'z') &&
1372 !(*f
>= '0' && *f
<= '9')) {
1374 *(t
++) = hexchar(*f
>> 4);
1375 *(t
++) = hexchar(*f
);
1385 char *bus_path_unescape(const char *f
) {
1390 if (!(r
= strdup(f
)))
1393 for (t
= r
; *f
; f
++) {
1398 if ((a
= unhexchar(f
[1])) < 0 ||
1399 (b
= unhexchar(f
[2])) < 0) {
1400 /* Invalid escape code, let's take it literal then */
1403 *(t
++) = (char) ((a
<< 4) | b
);
1415 char *path_kill_slashes(char *path
) {
1419 /* Removes redundant inner and trailing slashes. Modifies the
1420 * passed string in-place.
1422 * ///foo///bar/ becomes /foo/bar
1425 for (f
= path
, t
= path
; *f
; f
++) {
1440 /* Special rule, if we are talking of the root directory, a
1441 trailing slash is good */
1443 if (t
== path
&& slash
)
1450 bool path_startswith(const char *path
, const char *prefix
) {
1454 if ((path
[0] == '/') != (prefix
[0] == '/'))
1460 path
+= strspn(path
, "/");
1461 prefix
+= strspn(prefix
, "/");
1469 a
= strcspn(path
, "/");
1470 b
= strcspn(prefix
, "/");
1475 if (memcmp(path
, prefix
, a
) != 0)
1483 bool path_equal(const char *a
, const char *b
) {
1487 if ((a
[0] == '/') != (b
[0] == '/'))
1493 a
+= strspn(a
, "/");
1494 b
+= strspn(b
, "/");
1496 if (*a
== 0 && *b
== 0)
1499 if (*a
== 0 || *b
== 0)
1502 j
= strcspn(a
, "/");
1503 k
= strcspn(b
, "/");
1508 if (memcmp(a
, b
, j
) != 0)
1516 char *ascii_strlower(char *t
) {
1521 for (p
= t
; *p
; p
++)
1522 if (*p
>= 'A' && *p
<= 'Z')
1523 *p
= *p
- 'A' + 'a';
1528 bool ignore_file(const char *filename
) {
1532 filename
[0] == '.' ||
1533 streq(filename
, "lost+found") ||
1534 endswith(filename
, "~") ||
1535 endswith(filename
, ".rpmnew") ||
1536 endswith(filename
, ".rpmsave") ||
1537 endswith(filename
, ".rpmorig") ||
1538 endswith(filename
, ".dpkg-old") ||
1539 endswith(filename
, ".dpkg-new") ||
1540 endswith(filename
, ".swp");
1543 int fd_nonblock(int fd
, bool nonblock
) {
1548 if ((flags
= fcntl(fd
, F_GETFL
, 0)) < 0)
1552 flags
|= O_NONBLOCK
;
1554 flags
&= ~O_NONBLOCK
;
1556 if (fcntl(fd
, F_SETFL
, flags
) < 0)
1562 int fd_cloexec(int fd
, bool cloexec
) {
1567 if ((flags
= fcntl(fd
, F_GETFD
, 0)) < 0)
1571 flags
|= FD_CLOEXEC
;
1573 flags
&= ~FD_CLOEXEC
;
1575 if (fcntl(fd
, F_SETFD
, flags
) < 0)
1581 int close_all_fds(const int except
[], unsigned n_except
) {
1586 if (!(d
= opendir("/proc/self/fd")))
1589 while ((de
= readdir(d
))) {
1592 if (ignore_file(de
->d_name
))
1595 if ((r
= safe_atoi(de
->d_name
, &fd
)) < 0)
1609 for (i
= 0; i
< n_except
; i
++)
1610 if (except
[i
] == fd
) {
1619 if ((r
= close_nointr(fd
)) < 0) {
1620 /* Valgrind has its own FD and doesn't want to have it closed */
1633 bool chars_intersect(const char *a
, const char *b
) {
1636 /* Returns true if any of the chars in a are in b. */
1637 for (p
= a
; *p
; p
++)
1644 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
1654 sec
= (time_t) (t
/ USEC_PER_SEC
);
1656 if (strftime(buf
, l
, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec
, &tm
)) <= 0)
1662 char *format_timespan(char *buf
, size_t l
, usec_t t
) {
1663 static const struct {
1667 { "w", USEC_PER_WEEK
},
1668 { "d", USEC_PER_DAY
},
1669 { "h", USEC_PER_HOUR
},
1670 { "min", USEC_PER_MINUTE
},
1671 { "s", USEC_PER_SEC
},
1672 { "ms", USEC_PER_MSEC
},
1682 if (t
== (usec_t
) -1)
1686 snprintf(p
, l
, "0");
1691 /* The result of this function can be parsed with parse_usec */
1693 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
1697 if (t
< table
[i
].usec
)
1703 k
= snprintf(p
, l
, "%s%llu%s", p
> buf
? " " : "", (unsigned long long) (t
/ table
[i
].usec
), table
[i
].suffix
);
1704 n
= MIN((size_t) k
, l
);
1717 bool fstype_is_network(const char *fstype
) {
1718 static const char * const table
[] = {
1730 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1731 if (streq(table
[i
], fstype
))
1740 if ((fd
= open("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
)) < 0)
1745 TIOCL_GETKMSGREDIRECT
,
1749 if (ioctl(fd
, TIOCLINUX
, tiocl
) < 0)
1752 vt
= tiocl
[0] <= 0 ? 1 : tiocl
[0];
1755 if (ioctl(fd
, VT_ACTIVATE
, vt
) < 0)
1758 close_nointr_nofail(r
);
1762 int read_one_char(FILE *f
, char *ret
, bool *need_nl
) {
1763 struct termios old_termios
, new_termios
;
1770 if (tcgetattr(fileno(f
), &old_termios
) >= 0) {
1771 new_termios
= old_termios
;
1773 new_termios
.c_lflag
&= ~ICANON
;
1774 new_termios
.c_cc
[VMIN
] = 1;
1775 new_termios
.c_cc
[VTIME
] = 0;
1777 if (tcsetattr(fileno(f
), TCSADRAIN
, &new_termios
) >= 0) {
1780 k
= fread(&c
, 1, 1, f
);
1782 tcsetattr(fileno(f
), TCSADRAIN
, &old_termios
);
1788 *need_nl
= c
!= '\n';
1795 if (!(fgets(line
, sizeof(line
), f
)))
1800 if (strlen(line
) != 1)
1810 int ask(char *ret
, const char *replies
, const char *text
, ...) {
1819 bool need_nl
= true;
1821 fputs("\x1B[1m", stdout
);
1827 fputs("\x1B[0m", stdout
);
1831 if ((r
= read_one_char(stdin
, &c
, &need_nl
)) < 0) {
1833 if (r
== -EBADMSG
) {
1834 puts("Bad input, please try again.");
1845 if (strchr(replies
, c
)) {
1850 puts("Read unexpected character, please try again.");
1854 int reset_terminal(int fd
) {
1855 struct termios termios
;
1859 /* Set terminal to some sane defaults */
1863 /* First, unlock termios */
1865 ioctl(fd
, TIOCSLCKTRMIOS
, &termios
);
1867 /* Disable exclusive mode, just in case */
1868 ioctl(fd
, TIOCNXCL
);
1870 /* Enable console unicode mode */
1872 ioctl(fd
, KDSKBMODE
, &arg
);
1874 if (tcgetattr(fd
, &termios
) < 0) {
1879 /* We only reset the stuff that matters to the software. How
1880 * hardware is set up we don't touch assuming that somebody
1881 * else will do that for us */
1883 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| ISTRIP
| INLCR
| IGNCR
| IUCLC
);
1884 termios
.c_iflag
|= ICRNL
| IMAXBEL
| IUTF8
;
1885 termios
.c_oflag
|= ONLCR
;
1886 termios
.c_cflag
|= CREAD
;
1887 termios
.c_lflag
= ISIG
| ICANON
| IEXTEN
| ECHO
| ECHOE
| ECHOK
| ECHOCTL
| ECHOPRT
| ECHOKE
;
1889 termios
.c_cc
[VINTR
] = 03; /* ^C */
1890 termios
.c_cc
[VQUIT
] = 034; /* ^\ */
1891 termios
.c_cc
[VERASE
] = 0177;
1892 termios
.c_cc
[VKILL
] = 025; /* ^X */
1893 termios
.c_cc
[VEOF
] = 04; /* ^D */
1894 termios
.c_cc
[VSTART
] = 021; /* ^Q */
1895 termios
.c_cc
[VSTOP
] = 023; /* ^S */
1896 termios
.c_cc
[VSUSP
] = 032; /* ^Z */
1897 termios
.c_cc
[VLNEXT
] = 026; /* ^V */
1898 termios
.c_cc
[VWERASE
] = 027; /* ^W */
1899 termios
.c_cc
[VREPRINT
] = 022; /* ^R */
1900 termios
.c_cc
[VEOL
] = 0;
1901 termios
.c_cc
[VEOL2
] = 0;
1903 termios
.c_cc
[VTIME
] = 0;
1904 termios
.c_cc
[VMIN
] = 1;
1906 if (tcsetattr(fd
, TCSANOW
, &termios
) < 0)
1910 /* Just in case, flush all crap out */
1911 tcflush(fd
, TCIOFLUSH
);
1916 int open_terminal(const char *name
, int mode
) {
1919 if ((fd
= open(name
, mode
)) < 0)
1922 if ((r
= isatty(fd
)) < 0) {
1923 close_nointr_nofail(fd
);
1928 close_nointr_nofail(fd
);
1935 int flush_fd(int fd
) {
1936 struct pollfd pollfd
;
1940 pollfd
.events
= POLLIN
;
1947 if ((r
= poll(&pollfd
, 1, 0)) < 0) {
1958 if ((l
= read(fd
, buf
, sizeof(buf
))) < 0) {
1963 if (errno
== EAGAIN
)
1974 int acquire_terminal(const char *name
, bool fail
, bool force
, bool ignore_tiocstty_eperm
) {
1975 int fd
= -1, notify
= -1, r
, wd
= -1;
1979 /* We use inotify to be notified when the tty is closed. We
1980 * create the watch before checking if we can actually acquire
1981 * it, so that we don't lose any event.
1983 * Note: strictly speaking this actually watches for the
1984 * device being closed, it does *not* really watch whether a
1985 * tty loses its controlling process. However, unless some
1986 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
1987 * its tty otherwise this will not become a problem. As long
1988 * as the administrator makes sure not configure any service
1989 * on the same tty as an untrusted user this should not be a
1990 * problem. (Which he probably should not do anyway.) */
1992 if (!fail
&& !force
) {
1993 if ((notify
= inotify_init1(IN_CLOEXEC
)) < 0) {
1998 if ((wd
= inotify_add_watch(notify
, name
, IN_CLOSE
)) < 0) {
2006 if ((r
= flush_fd(notify
)) < 0)
2009 /* We pass here O_NOCTTY only so that we can check the return
2010 * value TIOCSCTTY and have a reliable way to figure out if we
2011 * successfully became the controlling process of the tty */
2012 if ((fd
= open_terminal(name
, O_RDWR
|O_NOCTTY
)) < 0)
2015 /* First, try to get the tty */
2016 r
= ioctl(fd
, TIOCSCTTY
, force
);
2018 /* Sometimes it makes sense to ignore TIOCSCTTY
2019 * returning EPERM, i.e. when very likely we already
2020 * are have this controlling terminal. */
2021 if (r
< 0 && errno
== EPERM
&& ignore_tiocstty_eperm
)
2024 if (r
< 0 && (force
|| fail
|| errno
!= EPERM
)) {
2034 assert(notify
>= 0);
2037 struct inotify_event e
;
2040 if ((l
= read(notify
, &e
, sizeof(e
))) != sizeof(e
)) {
2054 if (e
.wd
!= wd
|| !(e
.mask
& IN_CLOSE
)) {
2062 /* We close the tty fd here since if the old session
2063 * ended our handle will be dead. It's important that
2064 * we do this after sleeping, so that we don't enter
2065 * an endless loop. */
2066 close_nointr_nofail(fd
);
2070 close_nointr_nofail(notify
);
2072 if ((r
= reset_terminal(fd
)) < 0)
2073 log_warning("Failed to reset terminal: %s", strerror(-r
));
2079 close_nointr_nofail(fd
);
2082 close_nointr_nofail(notify
);
2087 int release_terminal(void) {
2089 struct sigaction sa_old
, sa_new
;
2091 if ((fd
= open("/dev/tty", O_RDWR
|O_NOCTTY
|O_NDELAY
)) < 0)
2094 /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
2095 * by our own TIOCNOTTY */
2098 sa_new
.sa_handler
= SIG_IGN
;
2099 sa_new
.sa_flags
= SA_RESTART
;
2100 assert_se(sigaction(SIGHUP
, &sa_new
, &sa_old
) == 0);
2102 if (ioctl(fd
, TIOCNOTTY
) < 0)
2105 assert_se(sigaction(SIGHUP
, &sa_old
, NULL
) == 0);
2107 close_nointr_nofail(fd
);
2111 int sigaction_many(const struct sigaction
*sa
, ...) {
2116 while ((sig
= va_arg(ap
, int)) > 0)
2117 if (sigaction(sig
, sa
, NULL
) < 0)
2124 int ignore_signals(int sig
, ...) {
2125 struct sigaction sa
;
2130 sa
.sa_handler
= SIG_IGN
;
2131 sa
.sa_flags
= SA_RESTART
;
2133 if (sigaction(sig
, &sa
, NULL
) < 0)
2137 while ((sig
= va_arg(ap
, int)) > 0)
2138 if (sigaction(sig
, &sa
, NULL
) < 0)
2145 int default_signals(int sig
, ...) {
2146 struct sigaction sa
;
2151 sa
.sa_handler
= SIG_DFL
;
2152 sa
.sa_flags
= SA_RESTART
;
2154 if (sigaction(sig
, &sa
, NULL
) < 0)
2158 while ((sig
= va_arg(ap
, int)) > 0)
2159 if (sigaction(sig
, &sa
, NULL
) < 0)
2166 int close_pipe(int p
[]) {
2172 a
= close_nointr(p
[0]);
2177 b
= close_nointr(p
[1]);
2181 return a
< 0 ? a
: b
;
2184 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
2193 while (nbytes
> 0) {
2196 if ((k
= read(fd
, p
, nbytes
)) <= 0) {
2198 if (k
< 0 && errno
== EINTR
)
2201 if (k
< 0 && errno
== EAGAIN
&& do_poll
) {
2202 struct pollfd pollfd
;
2206 pollfd
.events
= POLLIN
;
2208 if (poll(&pollfd
, 1, -1) < 0) {
2212 return n
> 0 ? n
: -errno
;
2215 if (pollfd
.revents
!= POLLIN
)
2216 return n
> 0 ? n
: -EIO
;
2221 return n
> 0 ? n
: (k
< 0 ? -errno
: 0);
2232 ssize_t
loop_write(int fd
, const void *buf
, size_t nbytes
, bool do_poll
) {
2241 while (nbytes
> 0) {
2244 if ((k
= write(fd
, p
, nbytes
)) <= 0) {
2246 if (k
< 0 && errno
== EINTR
)
2249 if (k
< 0 && errno
== EAGAIN
&& do_poll
) {
2250 struct pollfd pollfd
;
2254 pollfd
.events
= POLLOUT
;
2256 if (poll(&pollfd
, 1, -1) < 0) {
2260 return n
> 0 ? n
: -errno
;
2263 if (pollfd
.revents
!= POLLOUT
)
2264 return n
> 0 ? n
: -EIO
;
2269 return n
> 0 ? n
: (k
< 0 ? -errno
: 0);
2280 int path_is_mount_point(const char *t
) {
2285 if (lstat(t
, &a
) < 0) {
2286 if (errno
== ENOENT
)
2292 if ((r
= parent_of_path(t
, &parent
)) < 0)
2295 r
= lstat(parent
, &b
);
2301 return a
.st_dev
!= b
.st_dev
;
2304 int parse_usec(const char *t
, usec_t
*usec
) {
2305 static const struct {
2309 { "sec", USEC_PER_SEC
},
2310 { "s", USEC_PER_SEC
},
2311 { "min", USEC_PER_MINUTE
},
2312 { "hr", USEC_PER_HOUR
},
2313 { "h", USEC_PER_HOUR
},
2314 { "d", USEC_PER_DAY
},
2315 { "w", USEC_PER_WEEK
},
2316 { "msec", USEC_PER_MSEC
},
2317 { "ms", USEC_PER_MSEC
},
2318 { "m", USEC_PER_MINUTE
},
2321 { "", USEC_PER_SEC
},
2337 l
= strtoll(p
, &e
, 10);
2348 e
+= strspn(e
, WHITESPACE
);
2350 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
2351 if (startswith(e
, table
[i
].suffix
)) {
2352 r
+= (usec_t
) l
* table
[i
].usec
;
2353 p
= e
+ strlen(table
[i
].suffix
);
2357 if (i
>= ELEMENTSOF(table
))
2367 int make_stdio(int fd
) {
2372 r
= dup2(fd
, STDIN_FILENO
);
2373 s
= dup2(fd
, STDOUT_FILENO
);
2374 t
= dup2(fd
, STDERR_FILENO
);
2377 close_nointr_nofail(fd
);
2379 if (r
< 0 || s
< 0 || t
< 0)
2385 bool is_clean_exit(int code
, int status
) {
2387 if (code
== CLD_EXITED
)
2390 /* If a daemon does not implement handlers for some of the
2391 * signals that's not considered an unclean shutdown */
2392 if (code
== CLD_KILLED
)
2396 status
== SIGTERM
||
2402 bool is_clean_exit_lsb(int code
, int status
) {
2404 if (is_clean_exit(code
, status
))
2408 code
== CLD_EXITED
&&
2409 (status
== EXIT_NOTINSTALLED
|| status
== EXIT_NOTCONFIGURED
);
2412 bool is_device_path(const char *path
) {
2414 /* Returns true on paths that refer to a device, either in
2415 * sysfs or in /dev */
2418 path_startswith(path
, "/dev/") ||
2419 path_startswith(path
, "/sys/");
2422 int dir_is_empty(const char *path
) {
2425 struct dirent buf
, *de
;
2427 if (!(d
= opendir(path
)))
2431 if ((r
= readdir_r(d
, &buf
, &de
)) > 0) {
2441 if (!ignore_file(de
->d_name
)) {
2451 unsigned long long random_ull(void) {
2456 if ((fd
= open("/dev/urandom", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
)) < 0)
2459 r
= loop_read(fd
, &ull
, sizeof(ull
), true);
2460 close_nointr_nofail(fd
);
2462 if (r
!= sizeof(ull
))
2468 return random() * RAND_MAX
+ random();
2471 void rename_process(const char name
[8]) {
2474 prctl(PR_SET_NAME
, name
);
2476 /* This is a like a poor man's setproctitle(). The string
2477 * passed should fit in 7 chars (i.e. the length of
2480 if (program_invocation_name
)
2481 strncpy(program_invocation_name
, name
, strlen(program_invocation_name
));
2484 void sigset_add_many(sigset_t
*ss
, ...) {
2491 while ((sig
= va_arg(ap
, int)) > 0)
2492 assert_se(sigaddset(ss
, sig
) == 0);
2496 char* gethostname_malloc(void) {
2499 assert_se(uname(&u
) >= 0);
2502 return strdup(u
.nodename
);
2504 return strdup(u
.sysname
);
2507 char* getlogname_malloc(void) {
2511 struct passwd pwbuf
, *pw
= NULL
;
2514 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
2519 /* Shortcut things to avoid NSS lookups */
2521 return strdup("root");
2523 if ((bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
)) <= 0)
2526 if (!(buf
= malloc(bufsize
)))
2529 if (getpwuid_r(uid
, &pwbuf
, buf
, bufsize
, &pw
) == 0 && pw
) {
2530 name
= strdup(pw
->pw_name
);
2537 if (asprintf(&name
, "%lu", (unsigned long) uid
) < 0)
2543 int getttyname_malloc(char **r
) {
2544 char path
[PATH_MAX
], *p
, *c
;
2549 if ((k
= ttyname_r(STDIN_FILENO
, path
, sizeof(path
))) != 0)
2555 if (startswith(path
, "/dev/"))
2558 if (!(c
= strdup(p
)))
2565 static int rm_rf_children(int fd
, bool only_dirs
) {
2571 /* This returns the first error we run into, but nevertheless
2574 if (!(d
= fdopendir(fd
))) {
2575 close_nointr_nofail(fd
);
2577 return errno
== ENOENT
? 0 : -errno
;
2581 struct dirent buf
, *de
;
2585 if ((r
= readdir_r(d
, &buf
, &de
)) != 0) {
2594 if (streq(de
->d_name
, ".") || streq(de
->d_name
, ".."))
2597 if (de
->d_type
== DT_UNKNOWN
) {
2600 if (fstatat(fd
, de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0) {
2601 if (ret
== 0 && errno
!= ENOENT
)
2606 is_dir
= S_ISDIR(st
.st_mode
);
2608 is_dir
= de
->d_type
== DT_DIR
;
2613 if ((subdir_fd
= openat(fd
, de
->d_name
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
)) < 0) {
2614 if (ret
== 0 && errno
!= ENOENT
)
2619 if ((r
= rm_rf_children(subdir_fd
, only_dirs
)) < 0) {
2624 if (unlinkat(fd
, de
->d_name
, AT_REMOVEDIR
) < 0) {
2625 if (ret
== 0 && errno
!= ENOENT
)
2628 } else if (!only_dirs
) {
2630 if (unlinkat(fd
, de
->d_name
, 0) < 0) {
2631 if (ret
== 0 && errno
!= ENOENT
)
2642 int rm_rf(const char *path
, bool only_dirs
, bool delete_root
) {
2648 if ((fd
= open(path
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
)) < 0) {
2650 if (errno
!= ENOTDIR
)
2653 if (delete_root
&& !only_dirs
)
2654 if (unlink(path
) < 0)
2660 r
= rm_rf_children(fd
, only_dirs
);
2663 if (rmdir(path
) < 0) {
2671 int chmod_and_chown(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
) {
2674 /* Under the assumption that we are running privileged we
2675 * first change the access mode and only then hand out
2676 * ownership to avoid a window where access is too open. */
2678 if (chmod(path
, mode
) < 0)
2681 if (chown(path
, uid
, gid
) < 0)
2687 cpu_set_t
* cpu_set_malloc(unsigned *ncpus
) {
2691 /* Allocates the cpuset in the right size */
2694 if (!(r
= CPU_ALLOC(n
)))
2697 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n
), r
) >= 0) {
2698 CPU_ZERO_S(CPU_ALLOC_SIZE(n
), r
);
2708 if (errno
!= EINVAL
)
2715 void status_vprintf(const char *format
, va_list ap
) {
2721 /* This independent of logging, as status messages are
2722 * optional and go exclusively to the console. */
2724 if (vasprintf(&s
, format
, ap
) < 0)
2727 if ((fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
)) < 0)
2730 write(fd
, s
, strlen(s
));
2736 close_nointr_nofail(fd
);
2739 void status_printf(const char *format
, ...) {
2744 va_start(ap
, format
);
2745 status_vprintf(format
, ap
);
2749 void status_welcome(void) {
2751 #if defined(TARGET_FEDORA)
2754 if (read_one_line_file("/etc/system-release", &r
) < 0)
2759 /* This tries to mimic the color magic the old Red Hat sysinit
2762 if (startswith(r
, "Red Hat"))
2763 status_printf("Welcome to \x1B[0;31m%s\x1B[0m!\n", r
); /* Red for RHEL */
2764 else if (startswith(r
, "Fedora"))
2765 status_printf("Welcome to \x1B[0;34m%s\x1B[0m!\n", r
); /* Blue for Fedora */
2767 status_printf("Welcome to %s!\n", r
);
2771 #elif defined(TARGET_SUSE)
2774 if (read_one_line_file("/etc/SuSE-release", &r
) < 0)
2779 status_printf("Welcome to \x1B[0;32m%s\x1B[0m!\n", r
); /* Green for SUSE */
2782 #warning "You probably should add a welcome text logic here."
2786 char *replace_env(const char *format
, char **env
) {
2793 const char *e
, *word
= format
;
2798 for (e
= format
; *e
; e
++) {
2809 if (!(k
= strnappend(r
, word
, e
-word
-1)))
2818 } else if (*e
== '$') {
2819 if (!(k
= strnappend(r
, word
, e
-word
)))
2835 if (!(t
= strv_env_get_with_length(env
, word
+2, e
-word
-2)))
2838 if (!(k
= strappend(r
, t
)))
2851 if (!(k
= strnappend(r
, word
, e
-word
)))
2862 char **replace_env_argv(char **argv
, char **env
) {
2864 unsigned k
= 0, l
= 0;
2866 l
= strv_length(argv
);
2868 if (!(r
= new(char*, l
+1)))
2871 STRV_FOREACH(i
, argv
) {
2873 /* If $FOO appears as single word, replace it by the split up variable */
2874 if ((*i
)[0] == '$' && (*i
)[1] != '{') {
2879 if ((e
= strv_env_get(env
, *i
+1))) {
2881 if (!(m
= strv_split_quoted(e
))) {
2892 if (!(w
= realloc(r
, sizeof(char*) * (l
+1)))) {
2901 memcpy(r
+ k
, m
, q
* sizeof(char*));
2909 /* If ${FOO} appears as part of a word, replace it by the variable as-is */
2910 if (!(r
[k
++] = replace_env(*i
, env
))) {
2921 static __thread
int parsed_columns
= 0;
2924 if (parsed_columns
> 0)
2925 return parsed_columns
;
2927 if ((e
= getenv("COLUMNS")))
2928 parsed_columns
= atoi(e
);
2930 if (parsed_columns
<= 0) {
2934 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &ws
) >= 0)
2935 parsed_columns
= ws
.ws_col
;
2938 if (parsed_columns
<= 0)
2939 parsed_columns
= 80;
2941 return parsed_columns
;
2944 int running_in_chroot(void) {
2950 /* Only works as root */
2952 if (stat("/proc/1/root", &a
) < 0)
2955 if (stat("/", &b
) < 0)
2959 a
.st_dev
!= b
.st_dev
||
2960 a
.st_ino
!= b
.st_ino
;
2963 char *ellipsize(const char *s
, unsigned length
, unsigned percent
) {
2968 assert(percent
<= 100);
2969 assert(length
>= 3);
2973 if (l
<= 3 || l
<= length
)
2976 if (!(r
= new0(char, length
+1)))
2979 x
= (length
* percent
) / 100;
2989 s
+ l
- (length
- x
- 3),
2995 int touch(const char *path
) {
3000 if ((fd
= open(path
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
, 0666)) < 0)
3003 close_nointr_nofail(fd
);
3007 char *unquote(const char *s
, const char quote
) {
3011 if ((l
= strlen(s
)) < 2)
3014 if (s
[0] == quote
&& s
[l
-1] == quote
)
3015 return strndup(s
+1, l
-2);
3020 static const char *const ioprio_class_table
[] = {
3021 [IOPRIO_CLASS_NONE
] = "none",
3022 [IOPRIO_CLASS_RT
] = "realtime",
3023 [IOPRIO_CLASS_BE
] = "best-effort",
3024 [IOPRIO_CLASS_IDLE
] = "idle"
3027 DEFINE_STRING_TABLE_LOOKUP(ioprio_class
, int);
3029 static const char *const sigchld_code_table
[] = {
3030 [CLD_EXITED
] = "exited",
3031 [CLD_KILLED
] = "killed",
3032 [CLD_DUMPED
] = "dumped",
3033 [CLD_TRAPPED
] = "trapped",
3034 [CLD_STOPPED
] = "stopped",
3035 [CLD_CONTINUED
] = "continued",
3038 DEFINE_STRING_TABLE_LOOKUP(sigchld_code
, int);
3040 static const char *const log_facility_table
[LOG_NFACILITIES
] = {
3041 [LOG_FAC(LOG_KERN
)] = "kern",
3042 [LOG_FAC(LOG_USER
)] = "user",
3043 [LOG_FAC(LOG_MAIL
)] = "mail",
3044 [LOG_FAC(LOG_DAEMON
)] = "daemon",
3045 [LOG_FAC(LOG_AUTH
)] = "auth",
3046 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
3047 [LOG_FAC(LOG_LPR
)] = "lpr",
3048 [LOG_FAC(LOG_NEWS
)] = "news",
3049 [LOG_FAC(LOG_UUCP
)] = "uucp",
3050 [LOG_FAC(LOG_CRON
)] = "cron",
3051 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
3052 [LOG_FAC(LOG_FTP
)] = "ftp",
3053 [LOG_FAC(LOG_LOCAL0
)] = "local0",
3054 [LOG_FAC(LOG_LOCAL1
)] = "local1",
3055 [LOG_FAC(LOG_LOCAL2
)] = "local2",
3056 [LOG_FAC(LOG_LOCAL3
)] = "local3",
3057 [LOG_FAC(LOG_LOCAL4
)] = "local4",
3058 [LOG_FAC(LOG_LOCAL5
)] = "local5",
3059 [LOG_FAC(LOG_LOCAL6
)] = "local6",
3060 [LOG_FAC(LOG_LOCAL7
)] = "local7"
3063 DEFINE_STRING_TABLE_LOOKUP(log_facility
, int);
3065 static const char *const log_level_table
[] = {
3066 [LOG_EMERG
] = "emerg",
3067 [LOG_ALERT
] = "alert",
3068 [LOG_CRIT
] = "crit",
3070 [LOG_WARNING
] = "warning",
3071 [LOG_NOTICE
] = "notice",
3072 [LOG_INFO
] = "info",
3073 [LOG_DEBUG
] = "debug"
3076 DEFINE_STRING_TABLE_LOOKUP(log_level
, int);
3078 static const char* const sched_policy_table
[] = {
3079 [SCHED_OTHER
] = "other",
3080 [SCHED_BATCH
] = "batch",
3081 [SCHED_IDLE
] = "idle",
3082 [SCHED_FIFO
] = "fifo",
3086 DEFINE_STRING_TABLE_LOOKUP(sched_policy
, int);
3088 static const char* const rlimit_table
[] = {
3089 [RLIMIT_CPU
] = "LimitCPU",
3090 [RLIMIT_FSIZE
] = "LimitFSIZE",
3091 [RLIMIT_DATA
] = "LimitDATA",
3092 [RLIMIT_STACK
] = "LimitSTACK",
3093 [RLIMIT_CORE
] = "LimitCORE",
3094 [RLIMIT_RSS
] = "LimitRSS",
3095 [RLIMIT_NOFILE
] = "LimitNOFILE",
3096 [RLIMIT_AS
] = "LimitAS",
3097 [RLIMIT_NPROC
] = "LimitNPROC",
3098 [RLIMIT_MEMLOCK
] = "LimitMEMLOCK",
3099 [RLIMIT_LOCKS
] = "LimitLOCKS",
3100 [RLIMIT_SIGPENDING
] = "LimitSIGPENDING",
3101 [RLIMIT_MSGQUEUE
] = "LimitMSGQUEUE",
3102 [RLIMIT_NICE
] = "LimitNICE",
3103 [RLIMIT_RTPRIO
] = "LimitRTPRIO",
3104 [RLIMIT_RTTIME
] = "LimitRTTIME"
3107 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);
3109 static const char* const ip_tos_table
[] = {
3110 [IPTOS_LOWDELAY
] = "low-delay",
3111 [IPTOS_THROUGHPUT
] = "throughput",
3112 [IPTOS_RELIABILITY
] = "reliability",
3113 [IPTOS_LOWCOST
] = "low-cost",
3116 DEFINE_STRING_TABLE_LOOKUP(ip_tos
, int);
3118 static const char *const signal_table
[] = {
3134 [SIGSTKFLT
] = "STKFLT",
3144 [SIGVTALRM
] = "VTALRM",
3146 [SIGWINCH
] = "WINCH",
3152 DEFINE_STRING_TABLE_LOOKUP(signal
, int);