]>
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_timestamp_pretty(char *buf
, size_t l
, usec_t t
) {
1665 n
= now(CLOCK_REALTIME
);
1667 if (t
<= 0 || t
> n
|| t
+ USEC_PER_DAY
*7 <= t
)
1672 if (d
>= USEC_PER_YEAR
)
1673 snprintf(buf
, l
, "%llu years and %llu months ago",
1674 (unsigned long long) (d
/ USEC_PER_YEAR
),
1675 (unsigned long long) ((d
% USEC_PER_YEAR
) / USEC_PER_MONTH
));
1676 else if (d
>= USEC_PER_MONTH
)
1677 snprintf(buf
, l
, "%llu months and %llu days ago",
1678 (unsigned long long) (d
/ USEC_PER_MONTH
),
1679 (unsigned long long) ((d
% USEC_PER_MONTH
) / USEC_PER_DAY
));
1680 else if (d
>= USEC_PER_WEEK
)
1681 snprintf(buf
, l
, "%llu weeks and %llu days ago",
1682 (unsigned long long) (d
/ USEC_PER_WEEK
),
1683 (unsigned long long) ((d
% USEC_PER_WEEK
) / USEC_PER_DAY
));
1684 else if (d
>= 2*USEC_PER_DAY
)
1685 snprintf(buf
, l
, "%llu days ago", (unsigned long long) (d
/ USEC_PER_DAY
));
1686 else if (d
>= 25*USEC_PER_HOUR
)
1687 snprintf(buf
, l
, "1 day and %lluh ago",
1688 (unsigned long long) ((d
- USEC_PER_DAY
) / USEC_PER_HOUR
));
1689 else if (d
>= 6*USEC_PER_HOUR
)
1690 snprintf(buf
, l
, "%lluh ago",
1691 (unsigned long long) (d
/ USEC_PER_HOUR
));
1692 else if (d
>= USEC_PER_HOUR
)
1693 snprintf(buf
, l
, "%lluh %llumin ago",
1694 (unsigned long long) (d
/ USEC_PER_HOUR
),
1695 (unsigned long long) ((d
% USEC_PER_HOUR
) / USEC_PER_MINUTE
));
1696 else if (d
>= 5*USEC_PER_MINUTE
)
1697 snprintf(buf
, l
, "%llumin ago",
1698 (unsigned long long) (d
/ USEC_PER_MINUTE
));
1699 else if (d
>= USEC_PER_MINUTE
)
1700 snprintf(buf
, l
, "%llumin %llus ago",
1701 (unsigned long long) (d
/ USEC_PER_MINUTE
),
1702 (unsigned long long) ((d
% USEC_PER_MINUTE
) / USEC_PER_SEC
));
1703 else if (d
>= USEC_PER_SEC
)
1704 snprintf(buf
, l
, "%llus ago",
1705 (unsigned long long) (d
/ USEC_PER_SEC
));
1706 else if (d
>= USEC_PER_MSEC
)
1707 snprintf(buf
, l
, "%llums ago",
1708 (unsigned long long) (d
/ USEC_PER_MSEC
));
1710 snprintf(buf
, l
, "%lluus ago",
1711 (unsigned long long) d
);
1713 snprintf(buf
, l
, "now");
1719 char *format_timespan(char *buf
, size_t l
, usec_t t
) {
1720 static const struct {
1724 { "w", USEC_PER_WEEK
},
1725 { "d", USEC_PER_DAY
},
1726 { "h", USEC_PER_HOUR
},
1727 { "min", USEC_PER_MINUTE
},
1728 { "s", USEC_PER_SEC
},
1729 { "ms", USEC_PER_MSEC
},
1739 if (t
== (usec_t
) -1)
1743 snprintf(p
, l
, "0");
1748 /* The result of this function can be parsed with parse_usec */
1750 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
1754 if (t
< table
[i
].usec
)
1760 k
= snprintf(p
, l
, "%s%llu%s", p
> buf
? " " : "", (unsigned long long) (t
/ table
[i
].usec
), table
[i
].suffix
);
1761 n
= MIN((size_t) k
, l
);
1774 bool fstype_is_network(const char *fstype
) {
1775 static const char * const table
[] = {
1787 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1788 if (streq(table
[i
], fstype
))
1797 if ((fd
= open("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
)) < 0)
1802 TIOCL_GETKMSGREDIRECT
,
1806 if (ioctl(fd
, TIOCLINUX
, tiocl
) < 0)
1809 vt
= tiocl
[0] <= 0 ? 1 : tiocl
[0];
1812 if (ioctl(fd
, VT_ACTIVATE
, vt
) < 0)
1815 close_nointr_nofail(r
);
1819 int read_one_char(FILE *f
, char *ret
, bool *need_nl
) {
1820 struct termios old_termios
, new_termios
;
1827 if (tcgetattr(fileno(f
), &old_termios
) >= 0) {
1828 new_termios
= old_termios
;
1830 new_termios
.c_lflag
&= ~ICANON
;
1831 new_termios
.c_cc
[VMIN
] = 1;
1832 new_termios
.c_cc
[VTIME
] = 0;
1834 if (tcsetattr(fileno(f
), TCSADRAIN
, &new_termios
) >= 0) {
1837 k
= fread(&c
, 1, 1, f
);
1839 tcsetattr(fileno(f
), TCSADRAIN
, &old_termios
);
1845 *need_nl
= c
!= '\n';
1852 if (!(fgets(line
, sizeof(line
), f
)))
1857 if (strlen(line
) != 1)
1867 int ask(char *ret
, const char *replies
, const char *text
, ...) {
1876 bool need_nl
= true;
1878 fputs("\x1B[1m", stdout
);
1884 fputs("\x1B[0m", stdout
);
1888 if ((r
= read_one_char(stdin
, &c
, &need_nl
)) < 0) {
1890 if (r
== -EBADMSG
) {
1891 puts("Bad input, please try again.");
1902 if (strchr(replies
, c
)) {
1907 puts("Read unexpected character, please try again.");
1911 int reset_terminal(int fd
) {
1912 struct termios termios
;
1916 /* Set terminal to some sane defaults */
1920 /* First, unlock termios */
1922 ioctl(fd
, TIOCSLCKTRMIOS
, &termios
);
1924 /* Disable exclusive mode, just in case */
1925 ioctl(fd
, TIOCNXCL
);
1927 /* Enable console unicode mode */
1929 ioctl(fd
, KDSKBMODE
, &arg
);
1931 if (tcgetattr(fd
, &termios
) < 0) {
1936 /* We only reset the stuff that matters to the software. How
1937 * hardware is set up we don't touch assuming that somebody
1938 * else will do that for us */
1940 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| ISTRIP
| INLCR
| IGNCR
| IUCLC
);
1941 termios
.c_iflag
|= ICRNL
| IMAXBEL
| IUTF8
;
1942 termios
.c_oflag
|= ONLCR
;
1943 termios
.c_cflag
|= CREAD
;
1944 termios
.c_lflag
= ISIG
| ICANON
| IEXTEN
| ECHO
| ECHOE
| ECHOK
| ECHOCTL
| ECHOPRT
| ECHOKE
;
1946 termios
.c_cc
[VINTR
] = 03; /* ^C */
1947 termios
.c_cc
[VQUIT
] = 034; /* ^\ */
1948 termios
.c_cc
[VERASE
] = 0177;
1949 termios
.c_cc
[VKILL
] = 025; /* ^X */
1950 termios
.c_cc
[VEOF
] = 04; /* ^D */
1951 termios
.c_cc
[VSTART
] = 021; /* ^Q */
1952 termios
.c_cc
[VSTOP
] = 023; /* ^S */
1953 termios
.c_cc
[VSUSP
] = 032; /* ^Z */
1954 termios
.c_cc
[VLNEXT
] = 026; /* ^V */
1955 termios
.c_cc
[VWERASE
] = 027; /* ^W */
1956 termios
.c_cc
[VREPRINT
] = 022; /* ^R */
1957 termios
.c_cc
[VEOL
] = 0;
1958 termios
.c_cc
[VEOL2
] = 0;
1960 termios
.c_cc
[VTIME
] = 0;
1961 termios
.c_cc
[VMIN
] = 1;
1963 if (tcsetattr(fd
, TCSANOW
, &termios
) < 0)
1967 /* Just in case, flush all crap out */
1968 tcflush(fd
, TCIOFLUSH
);
1973 int open_terminal(const char *name
, int mode
) {
1976 if ((fd
= open(name
, mode
)) < 0)
1979 if ((r
= isatty(fd
)) < 0) {
1980 close_nointr_nofail(fd
);
1985 close_nointr_nofail(fd
);
1992 int flush_fd(int fd
) {
1993 struct pollfd pollfd
;
1997 pollfd
.events
= POLLIN
;
2004 if ((r
= poll(&pollfd
, 1, 0)) < 0) {
2015 if ((l
= read(fd
, buf
, sizeof(buf
))) < 0) {
2020 if (errno
== EAGAIN
)
2031 int acquire_terminal(const char *name
, bool fail
, bool force
, bool ignore_tiocstty_eperm
) {
2032 int fd
= -1, notify
= -1, r
, wd
= -1;
2036 /* We use inotify to be notified when the tty is closed. We
2037 * create the watch before checking if we can actually acquire
2038 * it, so that we don't lose any event.
2040 * Note: strictly speaking this actually watches for the
2041 * device being closed, it does *not* really watch whether a
2042 * tty loses its controlling process. However, unless some
2043 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
2044 * its tty otherwise this will not become a problem. As long
2045 * as the administrator makes sure not configure any service
2046 * on the same tty as an untrusted user this should not be a
2047 * problem. (Which he probably should not do anyway.) */
2049 if (!fail
&& !force
) {
2050 if ((notify
= inotify_init1(IN_CLOEXEC
)) < 0) {
2055 if ((wd
= inotify_add_watch(notify
, name
, IN_CLOSE
)) < 0) {
2063 if ((r
= flush_fd(notify
)) < 0)
2066 /* We pass here O_NOCTTY only so that we can check the return
2067 * value TIOCSCTTY and have a reliable way to figure out if we
2068 * successfully became the controlling process of the tty */
2069 if ((fd
= open_terminal(name
, O_RDWR
|O_NOCTTY
)) < 0)
2072 /* First, try to get the tty */
2073 r
= ioctl(fd
, TIOCSCTTY
, force
);
2075 /* Sometimes it makes sense to ignore TIOCSCTTY
2076 * returning EPERM, i.e. when very likely we already
2077 * are have this controlling terminal. */
2078 if (r
< 0 && errno
== EPERM
&& ignore_tiocstty_eperm
)
2081 if (r
< 0 && (force
|| fail
|| errno
!= EPERM
)) {
2091 assert(notify
>= 0);
2094 struct inotify_event e
;
2097 if ((l
= read(notify
, &e
, sizeof(e
))) != sizeof(e
)) {
2111 if (e
.wd
!= wd
|| !(e
.mask
& IN_CLOSE
)) {
2119 /* We close the tty fd here since if the old session
2120 * ended our handle will be dead. It's important that
2121 * we do this after sleeping, so that we don't enter
2122 * an endless loop. */
2123 close_nointr_nofail(fd
);
2127 close_nointr_nofail(notify
);
2129 if ((r
= reset_terminal(fd
)) < 0)
2130 log_warning("Failed to reset terminal: %s", strerror(-r
));
2136 close_nointr_nofail(fd
);
2139 close_nointr_nofail(notify
);
2144 int release_terminal(void) {
2146 struct sigaction sa_old
, sa_new
;
2148 if ((fd
= open("/dev/tty", O_RDWR
|O_NOCTTY
|O_NDELAY
)) < 0)
2151 /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
2152 * by our own TIOCNOTTY */
2155 sa_new
.sa_handler
= SIG_IGN
;
2156 sa_new
.sa_flags
= SA_RESTART
;
2157 assert_se(sigaction(SIGHUP
, &sa_new
, &sa_old
) == 0);
2159 if (ioctl(fd
, TIOCNOTTY
) < 0)
2162 assert_se(sigaction(SIGHUP
, &sa_old
, NULL
) == 0);
2164 close_nointr_nofail(fd
);
2168 int sigaction_many(const struct sigaction
*sa
, ...) {
2173 while ((sig
= va_arg(ap
, int)) > 0)
2174 if (sigaction(sig
, sa
, NULL
) < 0)
2181 int ignore_signals(int sig
, ...) {
2182 struct sigaction sa
;
2187 sa
.sa_handler
= SIG_IGN
;
2188 sa
.sa_flags
= SA_RESTART
;
2190 if (sigaction(sig
, &sa
, NULL
) < 0)
2194 while ((sig
= va_arg(ap
, int)) > 0)
2195 if (sigaction(sig
, &sa
, NULL
) < 0)
2202 int default_signals(int sig
, ...) {
2203 struct sigaction sa
;
2208 sa
.sa_handler
= SIG_DFL
;
2209 sa
.sa_flags
= SA_RESTART
;
2211 if (sigaction(sig
, &sa
, NULL
) < 0)
2215 while ((sig
= va_arg(ap
, int)) > 0)
2216 if (sigaction(sig
, &sa
, NULL
) < 0)
2223 int close_pipe(int p
[]) {
2229 a
= close_nointr(p
[0]);
2234 b
= close_nointr(p
[1]);
2238 return a
< 0 ? a
: b
;
2241 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
, bool do_poll
) {
2250 while (nbytes
> 0) {
2253 if ((k
= read(fd
, p
, nbytes
)) <= 0) {
2255 if (k
< 0 && errno
== EINTR
)
2258 if (k
< 0 && errno
== EAGAIN
&& do_poll
) {
2259 struct pollfd pollfd
;
2263 pollfd
.events
= POLLIN
;
2265 if (poll(&pollfd
, 1, -1) < 0) {
2269 return n
> 0 ? n
: -errno
;
2272 if (pollfd
.revents
!= POLLIN
)
2273 return n
> 0 ? n
: -EIO
;
2278 return n
> 0 ? n
: (k
< 0 ? -errno
: 0);
2289 ssize_t
loop_write(int fd
, const void *buf
, size_t nbytes
, bool do_poll
) {
2298 while (nbytes
> 0) {
2301 if ((k
= write(fd
, p
, nbytes
)) <= 0) {
2303 if (k
< 0 && errno
== EINTR
)
2306 if (k
< 0 && errno
== EAGAIN
&& do_poll
) {
2307 struct pollfd pollfd
;
2311 pollfd
.events
= POLLOUT
;
2313 if (poll(&pollfd
, 1, -1) < 0) {
2317 return n
> 0 ? n
: -errno
;
2320 if (pollfd
.revents
!= POLLOUT
)
2321 return n
> 0 ? n
: -EIO
;
2326 return n
> 0 ? n
: (k
< 0 ? -errno
: 0);
2337 int path_is_mount_point(const char *t
) {
2342 if (lstat(t
, &a
) < 0) {
2343 if (errno
== ENOENT
)
2349 if ((r
= parent_of_path(t
, &parent
)) < 0)
2352 r
= lstat(parent
, &b
);
2358 return a
.st_dev
!= b
.st_dev
;
2361 int parse_usec(const char *t
, usec_t
*usec
) {
2362 static const struct {
2366 { "sec", USEC_PER_SEC
},
2367 { "s", USEC_PER_SEC
},
2368 { "min", USEC_PER_MINUTE
},
2369 { "hr", USEC_PER_HOUR
},
2370 { "h", USEC_PER_HOUR
},
2371 { "d", USEC_PER_DAY
},
2372 { "w", USEC_PER_WEEK
},
2373 { "msec", USEC_PER_MSEC
},
2374 { "ms", USEC_PER_MSEC
},
2375 { "m", USEC_PER_MINUTE
},
2378 { "", USEC_PER_SEC
},
2394 l
= strtoll(p
, &e
, 10);
2405 e
+= strspn(e
, WHITESPACE
);
2407 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
2408 if (startswith(e
, table
[i
].suffix
)) {
2409 r
+= (usec_t
) l
* table
[i
].usec
;
2410 p
= e
+ strlen(table
[i
].suffix
);
2414 if (i
>= ELEMENTSOF(table
))
2424 int make_stdio(int fd
) {
2429 r
= dup2(fd
, STDIN_FILENO
);
2430 s
= dup2(fd
, STDOUT_FILENO
);
2431 t
= dup2(fd
, STDERR_FILENO
);
2434 close_nointr_nofail(fd
);
2436 if (r
< 0 || s
< 0 || t
< 0)
2442 bool is_clean_exit(int code
, int status
) {
2444 if (code
== CLD_EXITED
)
2447 /* If a daemon does not implement handlers for some of the
2448 * signals that's not considered an unclean shutdown */
2449 if (code
== CLD_KILLED
)
2453 status
== SIGTERM
||
2459 bool is_clean_exit_lsb(int code
, int status
) {
2461 if (is_clean_exit(code
, status
))
2465 code
== CLD_EXITED
&&
2466 (status
== EXIT_NOTINSTALLED
|| status
== EXIT_NOTCONFIGURED
);
2469 bool is_device_path(const char *path
) {
2471 /* Returns true on paths that refer to a device, either in
2472 * sysfs or in /dev */
2475 path_startswith(path
, "/dev/") ||
2476 path_startswith(path
, "/sys/");
2479 int dir_is_empty(const char *path
) {
2482 struct dirent buf
, *de
;
2484 if (!(d
= opendir(path
)))
2488 if ((r
= readdir_r(d
, &buf
, &de
)) > 0) {
2498 if (!ignore_file(de
->d_name
)) {
2508 unsigned long long random_ull(void) {
2513 if ((fd
= open("/dev/urandom", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
)) < 0)
2516 r
= loop_read(fd
, &ull
, sizeof(ull
), true);
2517 close_nointr_nofail(fd
);
2519 if (r
!= sizeof(ull
))
2525 return random() * RAND_MAX
+ random();
2528 void rename_process(const char name
[8]) {
2531 prctl(PR_SET_NAME
, name
);
2533 /* This is a like a poor man's setproctitle(). The string
2534 * passed should fit in 7 chars (i.e. the length of
2537 if (program_invocation_name
)
2538 strncpy(program_invocation_name
, name
, strlen(program_invocation_name
));
2541 void sigset_add_many(sigset_t
*ss
, ...) {
2548 while ((sig
= va_arg(ap
, int)) > 0)
2549 assert_se(sigaddset(ss
, sig
) == 0);
2553 char* gethostname_malloc(void) {
2556 assert_se(uname(&u
) >= 0);
2559 return strdup(u
.nodename
);
2561 return strdup(u
.sysname
);
2564 char* getlogname_malloc(void) {
2568 struct passwd pwbuf
, *pw
= NULL
;
2571 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
2576 /* Shortcut things to avoid NSS lookups */
2578 return strdup("root");
2580 if ((bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
)) <= 0)
2583 if (!(buf
= malloc(bufsize
)))
2586 if (getpwuid_r(uid
, &pwbuf
, buf
, bufsize
, &pw
) == 0 && pw
) {
2587 name
= strdup(pw
->pw_name
);
2594 if (asprintf(&name
, "%lu", (unsigned long) uid
) < 0)
2600 int getttyname_malloc(char **r
) {
2601 char path
[PATH_MAX
], *p
, *c
;
2606 if ((k
= ttyname_r(STDIN_FILENO
, path
, sizeof(path
))) != 0)
2612 if (startswith(path
, "/dev/"))
2615 if (!(c
= strdup(p
)))
2622 static int rm_rf_children(int fd
, bool only_dirs
) {
2628 /* This returns the first error we run into, but nevertheless
2631 if (!(d
= fdopendir(fd
))) {
2632 close_nointr_nofail(fd
);
2634 return errno
== ENOENT
? 0 : -errno
;
2638 struct dirent buf
, *de
;
2642 if ((r
= readdir_r(d
, &buf
, &de
)) != 0) {
2651 if (streq(de
->d_name
, ".") || streq(de
->d_name
, ".."))
2654 if (de
->d_type
== DT_UNKNOWN
) {
2657 if (fstatat(fd
, de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0) {
2658 if (ret
== 0 && errno
!= ENOENT
)
2663 is_dir
= S_ISDIR(st
.st_mode
);
2665 is_dir
= de
->d_type
== DT_DIR
;
2670 if ((subdir_fd
= openat(fd
, de
->d_name
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
)) < 0) {
2671 if (ret
== 0 && errno
!= ENOENT
)
2676 if ((r
= rm_rf_children(subdir_fd
, only_dirs
)) < 0) {
2681 if (unlinkat(fd
, de
->d_name
, AT_REMOVEDIR
) < 0) {
2682 if (ret
== 0 && errno
!= ENOENT
)
2685 } else if (!only_dirs
) {
2687 if (unlinkat(fd
, de
->d_name
, 0) < 0) {
2688 if (ret
== 0 && errno
!= ENOENT
)
2699 int rm_rf(const char *path
, bool only_dirs
, bool delete_root
) {
2705 if ((fd
= open(path
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
)) < 0) {
2707 if (errno
!= ENOTDIR
)
2710 if (delete_root
&& !only_dirs
)
2711 if (unlink(path
) < 0)
2717 r
= rm_rf_children(fd
, only_dirs
);
2720 if (rmdir(path
) < 0) {
2728 int chmod_and_chown(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
) {
2731 /* Under the assumption that we are running privileged we
2732 * first change the access mode and only then hand out
2733 * ownership to avoid a window where access is too open. */
2735 if (chmod(path
, mode
) < 0)
2738 if (chown(path
, uid
, gid
) < 0)
2744 cpu_set_t
* cpu_set_malloc(unsigned *ncpus
) {
2748 /* Allocates the cpuset in the right size */
2751 if (!(r
= CPU_ALLOC(n
)))
2754 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n
), r
) >= 0) {
2755 CPU_ZERO_S(CPU_ALLOC_SIZE(n
), r
);
2765 if (errno
!= EINVAL
)
2772 void status_vprintf(const char *format
, va_list ap
) {
2778 /* This independent of logging, as status messages are
2779 * optional and go exclusively to the console. */
2781 if (vasprintf(&s
, format
, ap
) < 0)
2784 if ((fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
)) < 0)
2787 write(fd
, s
, strlen(s
));
2793 close_nointr_nofail(fd
);
2796 void status_printf(const char *format
, ...) {
2801 va_start(ap
, format
);
2802 status_vprintf(format
, ap
);
2806 void status_welcome(void) {
2808 #if defined(TARGET_FEDORA)
2811 if (read_one_line_file("/etc/system-release", &r
) < 0)
2816 /* This tries to mimic the color magic the old Red Hat sysinit
2819 if (startswith(r
, "Red Hat"))
2820 status_printf("Welcome to \x1B[0;31m%s\x1B[0m!\n", r
); /* Red for RHEL */
2821 else if (startswith(r
, "Fedora"))
2822 status_printf("Welcome to \x1B[0;34m%s\x1B[0m!\n", r
); /* Blue for Fedora */
2824 status_printf("Welcome to %s!\n", r
);
2828 #elif defined(TARGET_SUSE)
2831 if (read_one_line_file("/etc/SuSE-release", &r
) < 0)
2836 status_printf("Welcome to \x1B[0;32m%s\x1B[0m!\n", r
); /* Green for SUSE */
2839 #warning "You probably should add a welcome text logic here."
2843 char *replace_env(const char *format
, char **env
) {
2850 const char *e
, *word
= format
;
2855 for (e
= format
; *e
; e
++) {
2866 if (!(k
= strnappend(r
, word
, e
-word
-1)))
2875 } else if (*e
== '$') {
2876 if (!(k
= strnappend(r
, word
, e
-word
)))
2892 if (!(t
= strv_env_get_with_length(env
, word
+2, e
-word
-2)))
2895 if (!(k
= strappend(r
, t
)))
2908 if (!(k
= strnappend(r
, word
, e
-word
)))
2919 char **replace_env_argv(char **argv
, char **env
) {
2921 unsigned k
= 0, l
= 0;
2923 l
= strv_length(argv
);
2925 if (!(r
= new(char*, l
+1)))
2928 STRV_FOREACH(i
, argv
) {
2930 /* If $FOO appears as single word, replace it by the split up variable */
2931 if ((*i
)[0] == '$' && (*i
)[1] != '{') {
2936 if ((e
= strv_env_get(env
, *i
+1))) {
2938 if (!(m
= strv_split_quoted(e
))) {
2949 if (!(w
= realloc(r
, sizeof(char*) * (l
+1)))) {
2958 memcpy(r
+ k
, m
, q
* sizeof(char*));
2966 /* If ${FOO} appears as part of a word, replace it by the variable as-is */
2967 if (!(r
[k
++] = replace_env(*i
, env
))) {
2978 static __thread
int parsed_columns
= 0;
2981 if (parsed_columns
> 0)
2982 return parsed_columns
;
2984 if ((e
= getenv("COLUMNS")))
2985 parsed_columns
= atoi(e
);
2987 if (parsed_columns
<= 0) {
2991 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &ws
) >= 0)
2992 parsed_columns
= ws
.ws_col
;
2995 if (parsed_columns
<= 0)
2996 parsed_columns
= 80;
2998 return parsed_columns
;
3001 int running_in_chroot(void) {
3007 /* Only works as root */
3009 if (stat("/proc/1/root", &a
) < 0)
3012 if (stat("/", &b
) < 0)
3016 a
.st_dev
!= b
.st_dev
||
3017 a
.st_ino
!= b
.st_ino
;
3020 char *ellipsize(const char *s
, unsigned length
, unsigned percent
) {
3025 assert(percent
<= 100);
3026 assert(length
>= 3);
3030 if (l
<= 3 || l
<= length
)
3033 if (!(r
= new0(char, length
+1)))
3036 x
= (length
* percent
) / 100;
3046 s
+ l
- (length
- x
- 3),
3052 int touch(const char *path
) {
3057 if ((fd
= open(path
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
, 0666)) < 0)
3060 close_nointr_nofail(fd
);
3064 char *unquote(const char *s
, const char quote
) {
3068 if ((l
= strlen(s
)) < 2)
3071 if (s
[0] == quote
&& s
[l
-1] == quote
)
3072 return strndup(s
+1, l
-2);
3077 static const char *const ioprio_class_table
[] = {
3078 [IOPRIO_CLASS_NONE
] = "none",
3079 [IOPRIO_CLASS_RT
] = "realtime",
3080 [IOPRIO_CLASS_BE
] = "best-effort",
3081 [IOPRIO_CLASS_IDLE
] = "idle"
3084 DEFINE_STRING_TABLE_LOOKUP(ioprio_class
, int);
3086 static const char *const sigchld_code_table
[] = {
3087 [CLD_EXITED
] = "exited",
3088 [CLD_KILLED
] = "killed",
3089 [CLD_DUMPED
] = "dumped",
3090 [CLD_TRAPPED
] = "trapped",
3091 [CLD_STOPPED
] = "stopped",
3092 [CLD_CONTINUED
] = "continued",
3095 DEFINE_STRING_TABLE_LOOKUP(sigchld_code
, int);
3097 static const char *const log_facility_table
[LOG_NFACILITIES
] = {
3098 [LOG_FAC(LOG_KERN
)] = "kern",
3099 [LOG_FAC(LOG_USER
)] = "user",
3100 [LOG_FAC(LOG_MAIL
)] = "mail",
3101 [LOG_FAC(LOG_DAEMON
)] = "daemon",
3102 [LOG_FAC(LOG_AUTH
)] = "auth",
3103 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
3104 [LOG_FAC(LOG_LPR
)] = "lpr",
3105 [LOG_FAC(LOG_NEWS
)] = "news",
3106 [LOG_FAC(LOG_UUCP
)] = "uucp",
3107 [LOG_FAC(LOG_CRON
)] = "cron",
3108 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
3109 [LOG_FAC(LOG_FTP
)] = "ftp",
3110 [LOG_FAC(LOG_LOCAL0
)] = "local0",
3111 [LOG_FAC(LOG_LOCAL1
)] = "local1",
3112 [LOG_FAC(LOG_LOCAL2
)] = "local2",
3113 [LOG_FAC(LOG_LOCAL3
)] = "local3",
3114 [LOG_FAC(LOG_LOCAL4
)] = "local4",
3115 [LOG_FAC(LOG_LOCAL5
)] = "local5",
3116 [LOG_FAC(LOG_LOCAL6
)] = "local6",
3117 [LOG_FAC(LOG_LOCAL7
)] = "local7"
3120 DEFINE_STRING_TABLE_LOOKUP(log_facility
, int);
3122 static const char *const log_level_table
[] = {
3123 [LOG_EMERG
] = "emerg",
3124 [LOG_ALERT
] = "alert",
3125 [LOG_CRIT
] = "crit",
3127 [LOG_WARNING
] = "warning",
3128 [LOG_NOTICE
] = "notice",
3129 [LOG_INFO
] = "info",
3130 [LOG_DEBUG
] = "debug"
3133 DEFINE_STRING_TABLE_LOOKUP(log_level
, int);
3135 static const char* const sched_policy_table
[] = {
3136 [SCHED_OTHER
] = "other",
3137 [SCHED_BATCH
] = "batch",
3138 [SCHED_IDLE
] = "idle",
3139 [SCHED_FIFO
] = "fifo",
3143 DEFINE_STRING_TABLE_LOOKUP(sched_policy
, int);
3145 static const char* const rlimit_table
[] = {
3146 [RLIMIT_CPU
] = "LimitCPU",
3147 [RLIMIT_FSIZE
] = "LimitFSIZE",
3148 [RLIMIT_DATA
] = "LimitDATA",
3149 [RLIMIT_STACK
] = "LimitSTACK",
3150 [RLIMIT_CORE
] = "LimitCORE",
3151 [RLIMIT_RSS
] = "LimitRSS",
3152 [RLIMIT_NOFILE
] = "LimitNOFILE",
3153 [RLIMIT_AS
] = "LimitAS",
3154 [RLIMIT_NPROC
] = "LimitNPROC",
3155 [RLIMIT_MEMLOCK
] = "LimitMEMLOCK",
3156 [RLIMIT_LOCKS
] = "LimitLOCKS",
3157 [RLIMIT_SIGPENDING
] = "LimitSIGPENDING",
3158 [RLIMIT_MSGQUEUE
] = "LimitMSGQUEUE",
3159 [RLIMIT_NICE
] = "LimitNICE",
3160 [RLIMIT_RTPRIO
] = "LimitRTPRIO",
3161 [RLIMIT_RTTIME
] = "LimitRTTIME"
3164 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);
3166 static const char* const ip_tos_table
[] = {
3167 [IPTOS_LOWDELAY
] = "low-delay",
3168 [IPTOS_THROUGHPUT
] = "throughput",
3169 [IPTOS_RELIABILITY
] = "reliability",
3170 [IPTOS_LOWCOST
] = "low-cost",
3173 DEFINE_STRING_TABLE_LOOKUP(ip_tos
, int);
3175 static const char *const signal_table
[] = {
3191 [SIGSTKFLT
] = "STKFLT",
3201 [SIGVTALRM
] = "VTALRM",
3203 [SIGWINCH
] = "WINCH",
3209 DEFINE_STRING_TABLE_LOOKUP(signal
, int);