]>
git.ipfire.org Git - people/ms/systemd.git/blob - util.c
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>
52 bool streq_ptr(const char *a
, const char *b
) {
54 /* Like streq(), but tries to make sense of NULL pointers */
65 usec_t
now(clockid_t clock_id
) {
68 assert_se(clock_gettime(clock_id
, &ts
) == 0);
70 return timespec_load(&ts
);
73 usec_t
timespec_load(const struct timespec
*ts
) {
77 (usec_t
) ts
->tv_sec
* USEC_PER_SEC
+
78 (usec_t
) ts
->tv_nsec
/ NSEC_PER_USEC
;
81 struct timespec
*timespec_store(struct timespec
*ts
, usec_t u
) {
84 ts
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
85 ts
->tv_nsec
= (long int) ((u
% USEC_PER_SEC
) * NSEC_PER_USEC
);
90 usec_t
timeval_load(const struct timeval
*tv
) {
94 (usec_t
) tv
->tv_sec
* USEC_PER_SEC
+
98 struct timeval
*timeval_store(struct timeval
*tv
, usec_t u
) {
101 tv
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
102 tv
->tv_usec
= (suseconds_t
) (u
% USEC_PER_SEC
);
107 bool endswith(const char *s
, const char *postfix
) {
114 pl
= strlen(postfix
);
119 return memcmp(s
+ sl
- pl
, postfix
, pl
) == 0;
122 bool startswith(const char *s
, const char *prefix
) {
134 return memcmp(s
, prefix
, pl
) == 0;
137 bool first_word(const char *s
, const char *word
) {
149 if (memcmp(s
, word
, wl
) != 0)
152 return (s
[wl
] == 0 ||
153 strchr(WHITESPACE
, s
[wl
]));
156 int close_nointr(int fd
) {
162 if ((r
= close(fd
)) >= 0)
170 void close_nointr_nofail(int fd
) {
171 int saved_errno
= errno
;
173 /* like close_nointr() but cannot fail, and guarantees errno
176 assert_se(close_nointr(fd
) == 0);
181 int parse_boolean(const char *v
) {
184 if (streq(v
, "1") || v
[0] == 'y' || v
[0] == 'Y' || v
[0] == 't' || v
[0] == 'T' || !strcasecmp(v
, "on"))
186 else if (streq(v
, "0") || v
[0] == 'n' || v
[0] == 'N' || v
[0] == 'f' || v
[0] == 'F' || !strcasecmp(v
, "off"))
192 int safe_atou(const char *s
, unsigned *ret_u
) {
200 l
= strtoul(s
, &x
, 0);
202 if (!x
|| *x
|| errno
)
203 return errno
? -errno
: -EINVAL
;
205 if ((unsigned long) (unsigned) l
!= l
)
208 *ret_u
= (unsigned) l
;
212 int safe_atoi(const char *s
, int *ret_i
) {
220 l
= strtol(s
, &x
, 0);
222 if (!x
|| *x
|| errno
)
223 return errno
? -errno
: -EINVAL
;
225 if ((long) (int) l
!= l
)
232 int safe_atolu(const char *s
, long unsigned *ret_lu
) {
240 l
= strtoul(s
, &x
, 0);
242 if (!x
|| *x
|| errno
)
243 return errno
? -errno
: -EINVAL
;
249 int safe_atoli(const char *s
, long int *ret_li
) {
257 l
= strtol(s
, &x
, 0);
259 if (!x
|| *x
|| errno
)
260 return errno
? -errno
: -EINVAL
;
266 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
268 unsigned long long l
;
274 l
= strtoull(s
, &x
, 0);
276 if (!x
|| *x
|| errno
)
277 return errno
? -errno
: -EINVAL
;
283 int safe_atolli(const char *s
, long long int *ret_lli
) {
291 l
= strtoll(s
, &x
, 0);
293 if (!x
|| *x
|| errno
)
294 return errno
? -errno
: -EINVAL
;
300 /* Split a string into words. */
301 char *split(const char *c
, size_t *l
, const char *separator
, char **state
) {
304 current
= *state
? *state
: (char*) c
;
306 if (!*current
|| *c
== 0)
309 current
+= strspn(current
, separator
);
310 *l
= strcspn(current
, separator
);
313 return (char*) current
;
316 /* Split a string into words, but consider strings enclosed in '' and
317 * "" as words even if they include spaces. */
318 char *split_quoted(const char *c
, size_t *l
, char **state
) {
321 current
= *state
? *state
: (char*) c
;
323 if (!*current
|| *c
== 0)
326 current
+= strspn(current
, WHITESPACE
);
328 if (*current
== '\'') {
330 *l
= strcspn(current
, "'");
335 } else if (*current
== '\"') {
337 *l
= strcspn(current
, "\"");
343 *l
= strcspn(current
, WHITESPACE
);
347 /* FIXME: Cannot deal with strings that have spaces AND ticks
350 return (char*) current
;
353 char **split_path_and_make_absolute(const char *p
) {
357 if (!(l
= strv_split(p
, ":")))
360 if (!strv_path_make_absolute_cwd(l
)) {
368 int get_parent_of_pid(pid_t pid
, pid_t
*_ppid
) {
371 char fn
[132], line
[256], *p
;
372 long long unsigned ppid
;
377 assert_se(snprintf(fn
, sizeof(fn
)-1, "/proc/%llu/stat", (unsigned long long) pid
) < (int) (sizeof(fn
)-1));
378 fn
[sizeof(fn
)-1] = 0;
380 if (!(f
= fopen(fn
, "r")))
383 if (!(fgets(line
, sizeof(line
), f
))) {
391 /* Let's skip the pid and comm fields. The latter is enclosed
392 * in () but does not escape any () in its value, so let's
393 * skip over it manually */
395 if (!(p
= strrchr(line
, ')')))
406 if ((long long unsigned) (pid_t
) ppid
!= ppid
)
409 *_ppid
= (pid_t
) ppid
;
414 int write_one_line_file(const char *fn
, const char *line
) {
421 if (!(f
= fopen(fn
, "we")))
424 if (fputs(line
, f
) < 0) {
435 int read_one_line_file(const char *fn
, char **line
) {
443 if (!(f
= fopen(fn
, "re")))
446 if (!(fgets(t
, sizeof(t
), f
))) {
451 if (!(c
= strdup(t
))) {
464 char *truncate_nl(char *s
) {
467 s
[strcspn(s
, NEWLINE
)] = 0;
471 int get_process_name(pid_t pid
, char **name
) {
478 if (asprintf(&p
, "/proc/%llu/comm", (unsigned long long) pid
) < 0)
481 r
= read_one_line_file(p
, name
);
491 char *strappend(const char *s
, const char *suffix
) {
501 if (!(r
= new(char, a
+b
+1)))
505 memcpy(r
+a
, suffix
, b
);
511 int readlink_malloc(const char *p
, char **r
) {
521 if (!(c
= new(char, l
)))
524 if ((n
= readlink(p
, c
, l
-1)) < 0) {
530 if ((size_t) n
< l
-1) {
541 char *file_name_from_path(const char *p
) {
546 if ((r
= strrchr(p
, '/')))
552 bool path_is_absolute(const char *p
) {
558 bool is_path(const char *p
) {
560 return !!strchr(p
, '/');
563 char *path_make_absolute(const char *p
, const char *prefix
) {
568 /* Makes every item in the list an absolute path by prepending
569 * the prefix, if specified and necessary */
571 if (path_is_absolute(p
) || !prefix
)
574 if (asprintf(&r
, "%s/%s", prefix
, p
) < 0)
580 char *path_make_absolute_cwd(const char *p
) {
585 /* Similar to path_make_absolute(), but prefixes with the
586 * current working directory. */
588 if (path_is_absolute(p
))
591 if (!(cwd
= get_current_dir_name()))
594 r
= path_make_absolute(p
, cwd
);
600 char **strv_path_make_absolute_cwd(char **l
) {
603 /* Goes through every item in the string list and makes it
604 * absolute. This works in place and won't rollback any
605 * changes on failure. */
610 if (!(t
= path_make_absolute_cwd(*s
)))
620 int reset_all_signal_handlers(void) {
623 for (sig
= 1; sig
< _NSIG
; sig
++) {
626 if (sig
== SIGKILL
|| sig
== SIGSTOP
)
630 sa
.sa_handler
= SIG_DFL
;
631 sa
.sa_flags
= SA_RESTART
;
633 /* On Linux the first two RT signals are reserved by
634 * glibc, and sigaction() will return EINVAL for them. */
635 if ((sigaction(sig
, &sa
, NULL
) < 0))
643 char *strstrip(char *s
) {
646 /* Drops trailing whitespace. Modifies the string in
647 * place. Returns pointer to first non-space character */
649 s
+= strspn(s
, WHITESPACE
);
652 if (!strchr(WHITESPACE
, *e
))
663 char *delete_chars(char *s
, const char *bad
) {
666 /* Drops all whitespace, regardless where in the string */
668 for (f
= s
, t
= s
; *f
; f
++) {
680 char *file_in_same_dir(const char *path
, const char *filename
) {
687 /* This removes the last component of path and appends
688 * filename, unless the latter is absolute anyway or the
691 if (path_is_absolute(filename
))
692 return strdup(filename
);
694 if (!(e
= strrchr(path
, '/')))
695 return strdup(filename
);
697 k
= strlen(filename
);
698 if (!(r
= new(char, e
-path
+1+k
+1)))
701 memcpy(r
, path
, e
-path
+1);
702 memcpy(r
+(e
-path
)+1, filename
, k
+1);
707 int mkdir_parents(const char *path
, mode_t mode
) {
712 /* Creates every parent directory in the path except the last
715 p
= path
+ strspn(path
, "/");
720 e
= p
+ strcspn(p
, "/");
721 p
= e
+ strspn(e
, "/");
723 /* Is this the last component? If so, then we're
728 if (!(t
= strndup(path
, e
- path
)))
735 if (r
< 0 && errno
!= EEXIST
)
740 int mkdir_p(const char *path
, mode_t mode
) {
745 if ((r
= mkdir_parents(path
, mode
)) < 0)
748 if (mkdir(path
, mode
) < 0)
754 char hexchar(int x
) {
755 static const char table
[16] = "0123456789abcdef";
757 return table
[x
& 15];
760 int unhexchar(char c
) {
762 if (c
>= '0' && c
<= '9')
765 if (c
>= 'a' && c
<= 'f')
768 if (c
>= 'A' && c
<= 'F')
774 char octchar(int x
) {
775 return '0' + (x
& 7);
778 int unoctchar(char c
) {
780 if (c
>= '0' && c
<= '7')
786 char decchar(int x
) {
787 return '0' + (x
% 10);
790 int undecchar(char c
) {
792 if (c
>= '0' && c
<= '9')
798 char *cescape(const char *s
) {
804 /* Does C style string escaping. */
806 if (!(r
= new(char, strlen(s
)*4 + 1)))
809 for (f
= s
, t
= r
; *f
; f
++)
855 /* For special chars we prefer octal over
856 * hexadecimal encoding, simply because glib's
857 * g_strescape() does the same */
858 if ((*f
< ' ') || (*f
>= 127)) {
860 *(t
++) = octchar((unsigned char) *f
>> 6);
861 *(t
++) = octchar((unsigned char) *f
>> 3);
862 *(t
++) = octchar((unsigned char) *f
);
873 char *cunescape(const char *s
) {
879 /* Undoes C style string escaping */
881 if (!(r
= new(char, strlen(s
)+1)))
884 for (f
= s
, t
= r
; *f
; f
++) {
927 /* hexadecimal encoding */
930 if ((a
= unhexchar(f
[1])) < 0 ||
931 (b
= unhexchar(f
[2])) < 0) {
932 /* Invalid escape code, let's take it literal then */
936 *(t
++) = (char) ((a
<< 4) | b
);
954 if ((a
= unoctchar(f
[0])) < 0 ||
955 (b
= unoctchar(f
[1])) < 0 ||
956 (c
= unoctchar(f
[2])) < 0) {
957 /* Invalid escape code, let's take it literal then */
961 *(t
++) = (char) ((a
<< 6) | (b
<< 3) | c
);
969 /* premature end of string.*/
974 /* Invalid escape code, let's take it literal then */
987 char *xescape(const char *s
, const char *bad
) {
991 /* Escapes all chars in bad, in addition to \ and all special
992 * chars, in \xFF style escaping. May be reversed with
995 if (!(r
= new(char, strlen(s
)*4+1)))
998 for (f
= s
, t
= r
; *f
; f
++) {
1000 if ((*f
< ' ') || (*f
>= 127) ||
1001 (*f
== '\\') || strchr(bad
, *f
)) {
1004 *(t
++) = hexchar(*f
>> 4);
1005 *(t
++) = hexchar(*f
);
1015 char *bus_path_escape(const char *s
) {
1021 /* Escapes all chars that D-Bus' object path cannot deal
1022 * with. Can be reverse with bus_path_unescape() */
1024 if (!(r
= new(char, strlen(s
)*3+1)))
1027 for (f
= s
, t
= r
; *f
; f
++) {
1029 if (!(*f
>= 'A' && *f
<= 'Z') &&
1030 !(*f
>= 'a' && *f
<= 'z') &&
1031 !(*f
>= '0' && *f
<= '9')) {
1033 *(t
++) = hexchar(*f
>> 4);
1034 *(t
++) = hexchar(*f
);
1044 char *bus_path_unescape(const char *f
) {
1049 if (!(r
= strdup(f
)))
1052 for (t
= r
; *f
; f
++) {
1057 if ((a
= unhexchar(f
[1])) < 0 ||
1058 (b
= unhexchar(f
[2])) < 0) {
1059 /* Invalid escape code, let's take it literal then */
1062 *(t
++) = (char) ((a
<< 4) | b
);
1074 char *path_kill_slashes(char *path
) {
1078 /* Removes redundant inner and trailing slashes. Modifies the
1079 * passed string in-place.
1081 * ///foo///bar/ becomes /foo/bar
1084 for (f
= path
, t
= path
; *f
; f
++) {
1099 /* Special rule, if we are talking of the root directory, a
1100 trailing slash is good */
1102 if (t
== path
&& slash
)
1109 bool path_startswith(const char *path
, const char *prefix
) {
1113 if ((path
[0] == '/') != (prefix
[0] == '/'))
1119 path
+= strspn(path
, "/");
1120 prefix
+= strspn(prefix
, "/");
1128 a
= strcspn(path
, "/");
1129 b
= strcspn(prefix
, "/");
1134 if (memcmp(path
, prefix
, a
) != 0)
1142 char *ascii_strlower(char *t
) {
1147 for (p
= t
; *p
; p
++)
1148 if (*p
>= 'A' && *p
<= 'Z')
1149 *p
= *p
- 'A' + 'a';
1154 bool ignore_file(const char *filename
) {
1158 filename
[0] == '.' ||
1159 endswith(filename
, "~") ||
1160 endswith(filename
, ".rpmnew") ||
1161 endswith(filename
, ".rpmsave") ||
1162 endswith(filename
, ".rpmorig") ||
1163 endswith(filename
, ".dpkg-old") ||
1164 endswith(filename
, ".dpkg-new") ||
1165 endswith(filename
, ".swp");
1168 int fd_nonblock(int fd
, bool nonblock
) {
1173 if ((flags
= fcntl(fd
, F_GETFL
, 0)) < 0)
1177 flags
|= O_NONBLOCK
;
1179 flags
&= ~O_NONBLOCK
;
1181 if (fcntl(fd
, F_SETFL
, flags
) < 0)
1187 int fd_cloexec(int fd
, bool cloexec
) {
1192 if ((flags
= fcntl(fd
, F_GETFD
, 0)) < 0)
1196 flags
|= FD_CLOEXEC
;
1198 flags
&= ~FD_CLOEXEC
;
1200 if (fcntl(fd
, F_SETFD
, flags
) < 0)
1206 int close_all_fds(const int except
[], unsigned n_except
) {
1211 if (!(d
= opendir("/proc/self/fd")))
1214 while ((de
= readdir(d
))) {
1217 if (de
->d_name
[0] == '.')
1220 if ((r
= safe_atoi(de
->d_name
, &fd
)) < 0)
1234 for (i
= 0; i
< n_except
; i
++)
1235 if (except
[i
] == fd
) {
1244 if ((r
= close_nointr(fd
)) < 0) {
1245 /* Valgrind has its own FD and doesn't want to have it closed */
1258 bool chars_intersect(const char *a
, const char *b
) {
1261 /* Returns true if any of the chars in a are in b. */
1262 for (p
= a
; *p
; p
++)
1269 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
1279 sec
= (time_t) t
/ USEC_PER_SEC
;
1281 if (strftime(buf
, l
, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec
, &tm
)) <= 0)
1287 bool fstype_is_network(const char *fstype
) {
1288 static const char * const table
[] = {
1298 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1299 if (streq(table
[i
], fstype
))
1308 if ((fd
= open("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
)) < 0)
1313 TIOCL_GETKMSGREDIRECT
,
1317 if (ioctl(fd
, TIOCLINUX
, tiocl
) < 0)
1320 vt
= tiocl
[0] <= 0 ? 1 : tiocl
[0];
1323 if (ioctl(fd
, VT_ACTIVATE
, vt
) < 0)
1330 int read_one_char(FILE *f
, char *ret
, bool *need_nl
) {
1331 struct termios old_termios
, new_termios
;
1338 if (tcgetattr(fileno(f
), &old_termios
) >= 0) {
1339 new_termios
= old_termios
;
1341 new_termios
.c_lflag
&= ~ICANON
;
1342 new_termios
.c_cc
[VMIN
] = 1;
1343 new_termios
.c_cc
[VTIME
] = 0;
1345 if (tcsetattr(fileno(f
), TCSADRAIN
, &new_termios
) >= 0) {
1348 k
= fread(&c
, 1, 1, f
);
1350 tcsetattr(fileno(f
), TCSADRAIN
, &old_termios
);
1356 *need_nl
= c
!= '\n';
1363 if (!(fgets(line
, sizeof(line
), f
)))
1368 if (strlen(line
) != 1)
1378 int ask(char *ret
, const char *replies
, const char *text
, ...) {
1387 bool need_nl
= true;
1395 if ((r
= read_one_char(stdin
, &c
, &need_nl
)) < 0) {
1397 if (r
== -EBADMSG
) {
1398 puts("Bad input, please try again.");
1409 if (strchr(replies
, c
)) {
1414 puts("Read unexpected character, please try again.");
1418 int reset_terminal(int fd
) {
1419 struct termios termios
;
1424 /* Set terminal to some sane defaults */
1426 if (tcgetattr(fd
, &termios
) < 0) {
1431 /* We only reset the stuff that matters to the software. How
1432 * hardware is set up we don't touch assuming that somebody
1433 * else will do that for us */
1435 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| ISTRIP
| INLCR
| IGNCR
| IUCLC
);
1436 termios
.c_iflag
|= ICRNL
| IMAXBEL
| IUTF8
;
1437 termios
.c_oflag
|= ONLCR
;
1438 termios
.c_cflag
|= CREAD
;
1439 termios
.c_lflag
= ISIG
| ICANON
| IEXTEN
| ECHO
| ECHOE
| ECHOK
| ECHOCTL
| ECHOPRT
| ECHOKE
;
1441 termios
.c_cc
[VINTR
] = 03; /* ^C */
1442 termios
.c_cc
[VQUIT
] = 034; /* ^\ */
1443 termios
.c_cc
[VERASE
] = 0177;
1444 termios
.c_cc
[VKILL
] = 025; /* ^X */
1445 termios
.c_cc
[VEOF
] = 04; /* ^D */
1446 termios
.c_cc
[VSTART
] = 021; /* ^Q */
1447 termios
.c_cc
[VSTOP
] = 023; /* ^S */
1448 termios
.c_cc
[VSUSP
] = 032; /* ^Z */
1449 termios
.c_cc
[VLNEXT
] = 026; /* ^V */
1450 termios
.c_cc
[VWERASE
] = 027; /* ^W */
1451 termios
.c_cc
[VREPRINT
] = 022; /* ^R */
1452 termios
.c_cc
[VEOL
] = 0;
1453 termios
.c_cc
[VEOL2
] = 0;
1455 termios
.c_cc
[VTIME
] = 0;
1456 termios
.c_cc
[VMIN
] = 1;
1458 if (tcsetattr(fd
, TCSANOW
, &termios
) < 0)
1462 /* Just in case, flush all crap out */
1463 tcflush(fd
, TCIOFLUSH
);
1468 int open_terminal(const char *name
, int mode
) {
1471 if ((fd
= open(name
, mode
)) < 0)
1474 if ((r
= isatty(fd
)) < 0) {
1475 close_nointr_nofail(fd
);
1480 close_nointr_nofail(fd
);
1487 int flush_fd(int fd
) {
1488 struct pollfd pollfd
;
1492 pollfd
.events
= POLLIN
;
1499 if ((r
= poll(&pollfd
, 1, 0)) < 0) {
1510 if ((l
= read(fd
, buf
, sizeof(buf
))) < 0) {
1515 if (errno
== EAGAIN
)
1526 int acquire_terminal(const char *name
, bool fail
, bool force
) {
1527 int fd
= -1, notify
= -1, r
, wd
;
1531 /* We use inotify to be notified when the tty is closed. We
1532 * create the watch before checking if we can actually acquire
1533 * it, so that we don't lose any event.
1535 * Note: strictly speaking this actually watches for the
1536 * device being closed, it does *not* really watch whether a
1537 * tty loses its controlling process. However, unless some
1538 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
1539 * its tty otherwise this will not become a problem. As long
1540 * as the administrator makes sure not configure any service
1541 * on the same tty as an untrusted user this should not be a
1542 * problem. (Which he probably should not do anyway.) */
1544 if (!fail
&& !force
) {
1545 if ((notify
= inotify_init1(IN_CLOEXEC
)) < 0) {
1550 if ((wd
= inotify_add_watch(notify
, name
, IN_CLOSE
)) < 0) {
1557 if ((r
= flush_fd(notify
)) < 0)
1560 /* We pass here O_NOCTTY only so that we can check the return
1561 * value TIOCSCTTY and have a reliable way to figure out if we
1562 * successfully became the controlling process of the tty */
1563 if ((fd
= open_terminal(name
, O_RDWR
|O_NOCTTY
)) < 0)
1566 /* First, try to get the tty */
1567 if ((r
= ioctl(fd
, TIOCSCTTY
, force
)) < 0 &&
1568 (force
|| fail
|| errno
!= EPERM
)) {
1578 assert(notify
>= 0);
1581 struct inotify_event e
;
1584 if ((l
= read(notify
, &e
, sizeof(e
))) != sizeof(e
)) {
1598 if (e
.wd
!= wd
|| !(e
.mask
& IN_CLOSE
)) {
1606 /* We close the tty fd here since if the old session
1607 * ended our handle will be dead. It's important that
1608 * we do this after sleeping, so that we don't enter
1609 * an endless loop. */
1610 close_nointr_nofail(fd
);
1614 close_nointr(notify
);
1616 if ((r
= reset_terminal(fd
)) < 0)
1617 log_warning("Failed to reset terminal: %s", strerror(-r
));
1626 close_nointr(notify
);
1631 int release_terminal(void) {
1634 if ((fd
= open("/dev/tty", O_RDWR
)) < 0)
1637 if (ioctl(fd
, TIOCNOTTY
) < 0)
1640 close_nointr_nofail(fd
);
1644 int ignore_signal(int sig
) {
1645 struct sigaction sa
;
1648 sa
.sa_handler
= SIG_IGN
;
1649 sa
.sa_flags
= SA_RESTART
;
1651 return sigaction(sig
, &sa
, NULL
);
1654 static const char *const ioprio_class_table
[] = {
1655 [IOPRIO_CLASS_NONE
] = "none",
1656 [IOPRIO_CLASS_RT
] = "realtime",
1657 [IOPRIO_CLASS_BE
] = "best-effort",
1658 [IOPRIO_CLASS_IDLE
] = "idle"
1661 DEFINE_STRING_TABLE_LOOKUP(ioprio_class
, int);
1663 static const char *const sigchld_code_table
[] = {
1664 [CLD_EXITED
] = "exited",
1665 [CLD_KILLED
] = "killed",
1666 [CLD_DUMPED
] = "dumped",
1667 [CLD_TRAPPED
] = "trapped",
1668 [CLD_STOPPED
] = "stopped",
1669 [CLD_CONTINUED
] = "continued",
1672 DEFINE_STRING_TABLE_LOOKUP(sigchld_code
, int);
1674 static const char *const log_facility_table
[LOG_NFACILITIES
] = {
1675 [LOG_FAC(LOG_KERN
)] = "kern",
1676 [LOG_FAC(LOG_USER
)] = "user",
1677 [LOG_FAC(LOG_MAIL
)] = "mail",
1678 [LOG_FAC(LOG_DAEMON
)] = "daemon",
1679 [LOG_FAC(LOG_AUTH
)] = "auth",
1680 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
1681 [LOG_FAC(LOG_LPR
)] = "lpr",
1682 [LOG_FAC(LOG_NEWS
)] = "news",
1683 [LOG_FAC(LOG_UUCP
)] = "uucp",
1684 [LOG_FAC(LOG_CRON
)] = "cron",
1685 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
1686 [LOG_FAC(LOG_FTP
)] = "ftp",
1687 [LOG_FAC(LOG_LOCAL0
)] = "local0",
1688 [LOG_FAC(LOG_LOCAL1
)] = "local1",
1689 [LOG_FAC(LOG_LOCAL2
)] = "local2",
1690 [LOG_FAC(LOG_LOCAL3
)] = "local3",
1691 [LOG_FAC(LOG_LOCAL4
)] = "local4",
1692 [LOG_FAC(LOG_LOCAL5
)] = "local5",
1693 [LOG_FAC(LOG_LOCAL6
)] = "local6",
1694 [LOG_FAC(LOG_LOCAL7
)] = "local7"
1697 DEFINE_STRING_TABLE_LOOKUP(log_facility
, int);
1699 static const char *const log_level_table
[] = {
1700 [LOG_EMERG
] = "emerg",
1701 [LOG_ALERT
] = "alert",
1702 [LOG_CRIT
] = "crit",
1704 [LOG_WARNING
] = "warning",
1705 [LOG_NOTICE
] = "notice",
1706 [LOG_INFO
] = "info",
1707 [LOG_DEBUG
] = "debug"
1710 DEFINE_STRING_TABLE_LOOKUP(log_level
, int);
1712 static const char* const sched_policy_table
[] = {
1713 [SCHED_OTHER
] = "other",
1714 [SCHED_BATCH
] = "batch",
1715 [SCHED_IDLE
] = "idle",
1716 [SCHED_FIFO
] = "fifo",
1720 DEFINE_STRING_TABLE_LOOKUP(sched_policy
, int);
1722 static const char* const rlimit_table
[] = {
1723 [RLIMIT_CPU
] = "LimitCPU",
1724 [RLIMIT_FSIZE
] = "LimitFSIZE",
1725 [RLIMIT_DATA
] = "LimitDATA",
1726 [RLIMIT_STACK
] = "LimitSTACK",
1727 [RLIMIT_CORE
] = "LimitCORE",
1728 [RLIMIT_RSS
] = "LimitRSS",
1729 [RLIMIT_NOFILE
] = "LimitNOFILE",
1730 [RLIMIT_AS
] = "LimitAS",
1731 [RLIMIT_NPROC
] = "LimitNPROC",
1732 [RLIMIT_MEMLOCK
] = "LimitMEMLOCK",
1733 [RLIMIT_LOCKS
] = "LimitLOCKS",
1734 [RLIMIT_SIGPENDING
] = "LimitSIGPENDING",
1735 [RLIMIT_MSGQUEUE
] = "LimitMSGQUEUE",
1736 [RLIMIT_NICE
] = "LimitNICE",
1737 [RLIMIT_RTPRIO
] = "LimitRTPRIO",
1738 [RLIMIT_RTTIME
] = "LimitRTTIME"
1741 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);