]>
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>
53 bool streq_ptr(const char *a
, const char *b
) {
55 /* Like streq(), but tries to make sense of NULL pointers */
66 usec_t
now(clockid_t clock_id
) {
69 assert_se(clock_gettime(clock_id
, &ts
) == 0);
71 return timespec_load(&ts
);
74 usec_t
timespec_load(const struct timespec
*ts
) {
78 (usec_t
) ts
->tv_sec
* USEC_PER_SEC
+
79 (usec_t
) ts
->tv_nsec
/ NSEC_PER_USEC
;
82 struct timespec
*timespec_store(struct timespec
*ts
, usec_t u
) {
85 ts
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
86 ts
->tv_nsec
= (long int) ((u
% USEC_PER_SEC
) * NSEC_PER_USEC
);
91 usec_t
timeval_load(const struct timeval
*tv
) {
95 (usec_t
) tv
->tv_sec
* USEC_PER_SEC
+
99 struct timeval
*timeval_store(struct timeval
*tv
, usec_t u
) {
102 tv
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
103 tv
->tv_usec
= (suseconds_t
) (u
% USEC_PER_SEC
);
108 bool endswith(const char *s
, const char *postfix
) {
115 pl
= strlen(postfix
);
120 return memcmp(s
+ sl
- pl
, postfix
, pl
) == 0;
123 bool startswith(const char *s
, const char *prefix
) {
135 return memcmp(s
, prefix
, pl
) == 0;
138 bool first_word(const char *s
, const char *word
) {
150 if (memcmp(s
, word
, wl
) != 0)
153 return (s
[wl
] == 0 ||
154 strchr(WHITESPACE
, s
[wl
]));
157 int close_nointr(int fd
) {
163 if ((r
= close(fd
)) >= 0)
171 void close_nointr_nofail(int fd
) {
172 int saved_errno
= errno
;
174 /* like close_nointr() but cannot fail, and guarantees errno
177 assert_se(close_nointr(fd
) == 0);
182 int parse_boolean(const char *v
) {
185 if (streq(v
, "1") || v
[0] == 'y' || v
[0] == 'Y' || v
[0] == 't' || v
[0] == 'T' || !strcasecmp(v
, "on"))
187 else if (streq(v
, "0") || v
[0] == 'n' || v
[0] == 'N' || v
[0] == 'f' || v
[0] == 'F' || !strcasecmp(v
, "off"))
193 int safe_atou(const char *s
, unsigned *ret_u
) {
201 l
= strtoul(s
, &x
, 0);
203 if (!x
|| *x
|| errno
)
204 return errno
? -errno
: -EINVAL
;
206 if ((unsigned long) (unsigned) l
!= l
)
209 *ret_u
= (unsigned) l
;
213 int safe_atoi(const char *s
, int *ret_i
) {
221 l
= strtol(s
, &x
, 0);
223 if (!x
|| *x
|| errno
)
224 return errno
? -errno
: -EINVAL
;
226 if ((long) (int) l
!= l
)
233 int safe_atolu(const char *s
, long unsigned *ret_lu
) {
241 l
= strtoul(s
, &x
, 0);
243 if (!x
|| *x
|| errno
)
244 return errno
? -errno
: -EINVAL
;
250 int safe_atoli(const char *s
, long int *ret_li
) {
258 l
= strtol(s
, &x
, 0);
260 if (!x
|| *x
|| errno
)
261 return errno
? -errno
: -EINVAL
;
267 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
269 unsigned long long l
;
275 l
= strtoull(s
, &x
, 0);
277 if (!x
|| *x
|| errno
)
278 return errno
? -errno
: -EINVAL
;
284 int safe_atolli(const char *s
, long long int *ret_lli
) {
292 l
= strtoll(s
, &x
, 0);
294 if (!x
|| *x
|| errno
)
295 return errno
? -errno
: -EINVAL
;
301 /* Split a string into words. */
302 char *split(const char *c
, size_t *l
, const char *separator
, char **state
) {
305 current
= *state
? *state
: (char*) c
;
307 if (!*current
|| *c
== 0)
310 current
+= strspn(current
, separator
);
311 *l
= strcspn(current
, separator
);
314 return (char*) current
;
317 /* Split a string into words, but consider strings enclosed in '' and
318 * "" as words even if they include spaces. */
319 char *split_quoted(const char *c
, size_t *l
, char **state
) {
322 current
= *state
? *state
: (char*) c
;
324 if (!*current
|| *c
== 0)
327 current
+= strspn(current
, WHITESPACE
);
329 if (*current
== '\'') {
331 *l
= strcspn(current
, "'");
336 } else if (*current
== '\"') {
338 *l
= strcspn(current
, "\"");
344 *l
= strcspn(current
, WHITESPACE
);
348 /* FIXME: Cannot deal with strings that have spaces AND ticks
351 return (char*) current
;
354 char **split_path_and_make_absolute(const char *p
) {
358 if (!(l
= strv_split(p
, ":")))
361 if (!strv_path_make_absolute_cwd(l
)) {
369 int get_parent_of_pid(pid_t pid
, pid_t
*_ppid
) {
372 char fn
[132], line
[256], *p
;
373 long long unsigned ppid
;
378 assert_se(snprintf(fn
, sizeof(fn
)-1, "/proc/%llu/stat", (unsigned long long) pid
) < (int) (sizeof(fn
)-1));
379 fn
[sizeof(fn
)-1] = 0;
381 if (!(f
= fopen(fn
, "r")))
384 if (!(fgets(line
, sizeof(line
), f
))) {
392 /* Let's skip the pid and comm fields. The latter is enclosed
393 * in () but does not escape any () in its value, so let's
394 * skip over it manually */
396 if (!(p
= strrchr(line
, ')')))
407 if ((long long unsigned) (pid_t
) ppid
!= ppid
)
410 *_ppid
= (pid_t
) ppid
;
415 int write_one_line_file(const char *fn
, const char *line
) {
422 if (!(f
= fopen(fn
, "we")))
425 if (fputs(line
, f
) < 0) {
436 int read_one_line_file(const char *fn
, char **line
) {
444 if (!(f
= fopen(fn
, "re")))
447 if (!(fgets(t
, sizeof(t
), f
))) {
452 if (!(c
= strdup(t
))) {
465 char *truncate_nl(char *s
) {
468 s
[strcspn(s
, NEWLINE
)] = 0;
472 int get_process_name(pid_t pid
, char **name
) {
479 if (asprintf(&p
, "/proc/%llu/comm", (unsigned long long) pid
) < 0)
482 r
= read_one_line_file(p
, name
);
492 char *strappend(const char *s
, const char *suffix
) {
502 if (!(r
= new(char, a
+b
+1)))
506 memcpy(r
+a
, suffix
, b
);
512 int readlink_malloc(const char *p
, char **r
) {
522 if (!(c
= new(char, l
)))
525 if ((n
= readlink(p
, c
, l
-1)) < 0) {
531 if ((size_t) n
< l
-1) {
542 char *file_name_from_path(const char *p
) {
547 if ((r
= strrchr(p
, '/')))
553 bool path_is_absolute(const char *p
) {
559 bool is_path(const char *p
) {
561 return !!strchr(p
, '/');
564 char *path_make_absolute(const char *p
, const char *prefix
) {
569 /* Makes every item in the list an absolute path by prepending
570 * the prefix, if specified and necessary */
572 if (path_is_absolute(p
) || !prefix
)
575 if (asprintf(&r
, "%s/%s", prefix
, p
) < 0)
581 char *path_make_absolute_cwd(const char *p
) {
586 /* Similar to path_make_absolute(), but prefixes with the
587 * current working directory. */
589 if (path_is_absolute(p
))
592 if (!(cwd
= get_current_dir_name()))
595 r
= path_make_absolute(p
, cwd
);
601 char **strv_path_make_absolute_cwd(char **l
) {
604 /* Goes through every item in the string list and makes it
605 * absolute. This works in place and won't rollback any
606 * changes on failure. */
611 if (!(t
= path_make_absolute_cwd(*s
)))
621 int reset_all_signal_handlers(void) {
624 for (sig
= 1; sig
< _NSIG
; sig
++) {
627 if (sig
== SIGKILL
|| sig
== SIGSTOP
)
631 sa
.sa_handler
= SIG_DFL
;
632 sa
.sa_flags
= SA_RESTART
;
634 /* On Linux the first two RT signals are reserved by
635 * glibc, and sigaction() will return EINVAL for them. */
636 if ((sigaction(sig
, &sa
, NULL
) < 0))
644 char *strstrip(char *s
) {
647 /* Drops trailing whitespace. Modifies the string in
648 * place. Returns pointer to first non-space character */
650 s
+= strspn(s
, WHITESPACE
);
653 if (!strchr(WHITESPACE
, *e
))
664 char *delete_chars(char *s
, const char *bad
) {
667 /* Drops all whitespace, regardless where in the string */
669 for (f
= s
, t
= s
; *f
; f
++) {
681 char *file_in_same_dir(const char *path
, const char *filename
) {
688 /* This removes the last component of path and appends
689 * filename, unless the latter is absolute anyway or the
692 if (path_is_absolute(filename
))
693 return strdup(filename
);
695 if (!(e
= strrchr(path
, '/')))
696 return strdup(filename
);
698 k
= strlen(filename
);
699 if (!(r
= new(char, e
-path
+1+k
+1)))
702 memcpy(r
, path
, e
-path
+1);
703 memcpy(r
+(e
-path
)+1, filename
, k
+1);
708 int mkdir_parents(const char *path
, mode_t mode
) {
713 /* Creates every parent directory in the path except the last
716 p
= path
+ strspn(path
, "/");
721 e
= p
+ strcspn(p
, "/");
722 p
= e
+ strspn(e
, "/");
724 /* Is this the last component? If so, then we're
729 if (!(t
= strndup(path
, e
- path
)))
736 if (r
< 0 && errno
!= EEXIST
)
741 int mkdir_p(const char *path
, mode_t mode
) {
746 if ((r
= mkdir_parents(path
, mode
)) < 0)
749 if (mkdir(path
, mode
) < 0)
755 char hexchar(int x
) {
756 static const char table
[16] = "0123456789abcdef";
758 return table
[x
& 15];
761 int unhexchar(char c
) {
763 if (c
>= '0' && c
<= '9')
766 if (c
>= 'a' && c
<= 'f')
769 if (c
>= 'A' && c
<= 'F')
775 char octchar(int x
) {
776 return '0' + (x
& 7);
779 int unoctchar(char c
) {
781 if (c
>= '0' && c
<= '7')
787 char decchar(int x
) {
788 return '0' + (x
% 10);
791 int undecchar(char c
) {
793 if (c
>= '0' && c
<= '9')
799 char *cescape(const char *s
) {
805 /* Does C style string escaping. */
807 if (!(r
= new(char, strlen(s
)*4 + 1)))
810 for (f
= s
, t
= r
; *f
; f
++)
856 /* For special chars we prefer octal over
857 * hexadecimal encoding, simply because glib's
858 * g_strescape() does the same */
859 if ((*f
< ' ') || (*f
>= 127)) {
861 *(t
++) = octchar((unsigned char) *f
>> 6);
862 *(t
++) = octchar((unsigned char) *f
>> 3);
863 *(t
++) = octchar((unsigned char) *f
);
874 char *cunescape(const char *s
) {
880 /* Undoes C style string escaping */
882 if (!(r
= new(char, strlen(s
)+1)))
885 for (f
= s
, t
= r
; *f
; f
++) {
928 /* hexadecimal encoding */
931 if ((a
= unhexchar(f
[1])) < 0 ||
932 (b
= unhexchar(f
[2])) < 0) {
933 /* Invalid escape code, let's take it literal then */
937 *(t
++) = (char) ((a
<< 4) | b
);
955 if ((a
= unoctchar(f
[0])) < 0 ||
956 (b
= unoctchar(f
[1])) < 0 ||
957 (c
= unoctchar(f
[2])) < 0) {
958 /* Invalid escape code, let's take it literal then */
962 *(t
++) = (char) ((a
<< 6) | (b
<< 3) | c
);
970 /* premature end of string.*/
975 /* Invalid escape code, let's take it literal then */
988 char *xescape(const char *s
, const char *bad
) {
992 /* Escapes all chars in bad, in addition to \ and all special
993 * chars, in \xFF style escaping. May be reversed with
996 if (!(r
= new(char, strlen(s
)*4+1)))
999 for (f
= s
, t
= r
; *f
; f
++) {
1001 if ((*f
< ' ') || (*f
>= 127) ||
1002 (*f
== '\\') || strchr(bad
, *f
)) {
1005 *(t
++) = hexchar(*f
>> 4);
1006 *(t
++) = hexchar(*f
);
1016 char *bus_path_escape(const char *s
) {
1022 /* Escapes all chars that D-Bus' object path cannot deal
1023 * with. Can be reverse with bus_path_unescape() */
1025 if (!(r
= new(char, strlen(s
)*3+1)))
1028 for (f
= s
, t
= r
; *f
; f
++) {
1030 if (!(*f
>= 'A' && *f
<= 'Z') &&
1031 !(*f
>= 'a' && *f
<= 'z') &&
1032 !(*f
>= '0' && *f
<= '9')) {
1034 *(t
++) = hexchar(*f
>> 4);
1035 *(t
++) = hexchar(*f
);
1045 char *bus_path_unescape(const char *f
) {
1050 if (!(r
= strdup(f
)))
1053 for (t
= r
; *f
; f
++) {
1058 if ((a
= unhexchar(f
[1])) < 0 ||
1059 (b
= unhexchar(f
[2])) < 0) {
1060 /* Invalid escape code, let's take it literal then */
1063 *(t
++) = (char) ((a
<< 4) | b
);
1075 char *path_kill_slashes(char *path
) {
1079 /* Removes redundant inner and trailing slashes. Modifies the
1080 * passed string in-place.
1082 * ///foo///bar/ becomes /foo/bar
1085 for (f
= path
, t
= path
; *f
; f
++) {
1100 /* Special rule, if we are talking of the root directory, a
1101 trailing slash is good */
1103 if (t
== path
&& slash
)
1110 bool path_startswith(const char *path
, const char *prefix
) {
1114 if ((path
[0] == '/') != (prefix
[0] == '/'))
1120 path
+= strspn(path
, "/");
1121 prefix
+= strspn(prefix
, "/");
1129 a
= strcspn(path
, "/");
1130 b
= strcspn(prefix
, "/");
1135 if (memcmp(path
, prefix
, a
) != 0)
1143 char *ascii_strlower(char *t
) {
1148 for (p
= t
; *p
; p
++)
1149 if (*p
>= 'A' && *p
<= 'Z')
1150 *p
= *p
- 'A' + 'a';
1155 bool ignore_file(const char *filename
) {
1159 filename
[0] == '.' ||
1160 endswith(filename
, "~") ||
1161 endswith(filename
, ".rpmnew") ||
1162 endswith(filename
, ".rpmsave") ||
1163 endswith(filename
, ".rpmorig") ||
1164 endswith(filename
, ".dpkg-old") ||
1165 endswith(filename
, ".dpkg-new") ||
1166 endswith(filename
, ".swp");
1169 int fd_nonblock(int fd
, bool nonblock
) {
1174 if ((flags
= fcntl(fd
, F_GETFL
, 0)) < 0)
1178 flags
|= O_NONBLOCK
;
1180 flags
&= ~O_NONBLOCK
;
1182 if (fcntl(fd
, F_SETFL
, flags
) < 0)
1188 int fd_cloexec(int fd
, bool cloexec
) {
1193 if ((flags
= fcntl(fd
, F_GETFD
, 0)) < 0)
1197 flags
|= FD_CLOEXEC
;
1199 flags
&= ~FD_CLOEXEC
;
1201 if (fcntl(fd
, F_SETFD
, flags
) < 0)
1207 int close_all_fds(const int except
[], unsigned n_except
) {
1212 if (!(d
= opendir("/proc/self/fd")))
1215 while ((de
= readdir(d
))) {
1218 if (de
->d_name
[0] == '.')
1221 if ((r
= safe_atoi(de
->d_name
, &fd
)) < 0)
1235 for (i
= 0; i
< n_except
; i
++)
1236 if (except
[i
] == fd
) {
1245 if ((r
= close_nointr(fd
)) < 0) {
1246 /* Valgrind has its own FD and doesn't want to have it closed */
1259 bool chars_intersect(const char *a
, const char *b
) {
1262 /* Returns true if any of the chars in a are in b. */
1263 for (p
= a
; *p
; p
++)
1270 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
1280 sec
= (time_t) t
/ USEC_PER_SEC
;
1282 if (strftime(buf
, l
, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec
, &tm
)) <= 0)
1288 bool fstype_is_network(const char *fstype
) {
1289 static const char * const table
[] = {
1299 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1300 if (streq(table
[i
], fstype
))
1309 if ((fd
= open("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
)) < 0)
1314 TIOCL_GETKMSGREDIRECT
,
1318 if (ioctl(fd
, TIOCLINUX
, tiocl
) < 0)
1321 vt
= tiocl
[0] <= 0 ? 1 : tiocl
[0];
1324 if (ioctl(fd
, VT_ACTIVATE
, vt
) < 0)
1331 int read_one_char(FILE *f
, char *ret
, bool *need_nl
) {
1332 struct termios old_termios
, new_termios
;
1339 if (tcgetattr(fileno(f
), &old_termios
) >= 0) {
1340 new_termios
= old_termios
;
1342 new_termios
.c_lflag
&= ~ICANON
;
1343 new_termios
.c_cc
[VMIN
] = 1;
1344 new_termios
.c_cc
[VTIME
] = 0;
1346 if (tcsetattr(fileno(f
), TCSADRAIN
, &new_termios
) >= 0) {
1349 k
= fread(&c
, 1, 1, f
);
1351 tcsetattr(fileno(f
), TCSADRAIN
, &old_termios
);
1357 *need_nl
= c
!= '\n';
1364 if (!(fgets(line
, sizeof(line
), f
)))
1369 if (strlen(line
) != 1)
1379 int ask(char *ret
, const char *replies
, const char *text
, ...) {
1388 bool need_nl
= true;
1396 if ((r
= read_one_char(stdin
, &c
, &need_nl
)) < 0) {
1398 if (r
== -EBADMSG
) {
1399 puts("Bad input, please try again.");
1410 if (strchr(replies
, c
)) {
1415 puts("Read unexpected character, please try again.");
1419 int reset_terminal(int fd
) {
1420 struct termios termios
;
1425 /* Set terminal to some sane defaults */
1427 if (tcgetattr(fd
, &termios
) < 0) {
1432 /* We only reset the stuff that matters to the software. How
1433 * hardware is set up we don't touch assuming that somebody
1434 * else will do that for us */
1436 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| ISTRIP
| INLCR
| IGNCR
| IUCLC
);
1437 termios
.c_iflag
|= ICRNL
| IMAXBEL
| IUTF8
;
1438 termios
.c_oflag
|= ONLCR
;
1439 termios
.c_cflag
|= CREAD
;
1440 termios
.c_lflag
= ISIG
| ICANON
| IEXTEN
| ECHO
| ECHOE
| ECHOK
| ECHOCTL
| ECHOPRT
| ECHOKE
;
1442 termios
.c_cc
[VINTR
] = 03; /* ^C */
1443 termios
.c_cc
[VQUIT
] = 034; /* ^\ */
1444 termios
.c_cc
[VERASE
] = 0177;
1445 termios
.c_cc
[VKILL
] = 025; /* ^X */
1446 termios
.c_cc
[VEOF
] = 04; /* ^D */
1447 termios
.c_cc
[VSTART
] = 021; /* ^Q */
1448 termios
.c_cc
[VSTOP
] = 023; /* ^S */
1449 termios
.c_cc
[VSUSP
] = 032; /* ^Z */
1450 termios
.c_cc
[VLNEXT
] = 026; /* ^V */
1451 termios
.c_cc
[VWERASE
] = 027; /* ^W */
1452 termios
.c_cc
[VREPRINT
] = 022; /* ^R */
1453 termios
.c_cc
[VEOL
] = 0;
1454 termios
.c_cc
[VEOL2
] = 0;
1456 termios
.c_cc
[VTIME
] = 0;
1457 termios
.c_cc
[VMIN
] = 1;
1459 if (tcsetattr(fd
, TCSANOW
, &termios
) < 0)
1463 /* Just in case, flush all crap out */
1464 tcflush(fd
, TCIOFLUSH
);
1469 int open_terminal(const char *name
, int mode
) {
1472 if ((fd
= open(name
, mode
)) < 0)
1475 if ((r
= isatty(fd
)) < 0) {
1476 close_nointr_nofail(fd
);
1481 close_nointr_nofail(fd
);
1488 int flush_fd(int fd
) {
1489 struct pollfd pollfd
;
1493 pollfd
.events
= POLLIN
;
1500 if ((r
= poll(&pollfd
, 1, 0)) < 0) {
1511 if ((l
= read(fd
, buf
, sizeof(buf
))) < 0) {
1516 if (errno
== EAGAIN
)
1527 int acquire_terminal(const char *name
, bool fail
, bool force
) {
1528 int fd
= -1, notify
= -1, r
, wd
;
1532 /* We use inotify to be notified when the tty is closed. We
1533 * create the watch before checking if we can actually acquire
1534 * it, so that we don't lose any event.
1536 * Note: strictly speaking this actually watches for the
1537 * device being closed, it does *not* really watch whether a
1538 * tty loses its controlling process. However, unless some
1539 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
1540 * its tty otherwise this will not become a problem. As long
1541 * as the administrator makes sure not configure any service
1542 * on the same tty as an untrusted user this should not be a
1543 * problem. (Which he probably should not do anyway.) */
1545 if (!fail
&& !force
) {
1546 if ((notify
= inotify_init1(IN_CLOEXEC
)) < 0) {
1551 if ((wd
= inotify_add_watch(notify
, name
, IN_CLOSE
)) < 0) {
1558 if ((r
= flush_fd(notify
)) < 0)
1561 /* We pass here O_NOCTTY only so that we can check the return
1562 * value TIOCSCTTY and have a reliable way to figure out if we
1563 * successfully became the controlling process of the tty */
1564 if ((fd
= open_terminal(name
, O_RDWR
|O_NOCTTY
)) < 0)
1567 /* First, try to get the tty */
1568 if ((r
= ioctl(fd
, TIOCSCTTY
, force
)) < 0 &&
1569 (force
|| fail
|| errno
!= EPERM
)) {
1579 assert(notify
>= 0);
1582 struct inotify_event e
;
1585 if ((l
= read(notify
, &e
, sizeof(e
))) != sizeof(e
)) {
1599 if (e
.wd
!= wd
|| !(e
.mask
& IN_CLOSE
)) {
1607 /* We close the tty fd here since if the old session
1608 * ended our handle will be dead. It's important that
1609 * we do this after sleeping, so that we don't enter
1610 * an endless loop. */
1611 close_nointr_nofail(fd
);
1615 close_nointr(notify
);
1617 if ((r
= reset_terminal(fd
)) < 0)
1618 log_warning("Failed to reset terminal: %s", strerror(-r
));
1627 close_nointr(notify
);
1632 int release_terminal(void) {
1635 if ((fd
= open("/dev/tty", O_RDWR
)) < 0)
1638 if (ioctl(fd
, TIOCNOTTY
) < 0)
1641 close_nointr_nofail(fd
);
1645 int ignore_signal(int sig
) {
1646 struct sigaction sa
;
1649 sa
.sa_handler
= SIG_IGN
;
1650 sa
.sa_flags
= SA_RESTART
;
1652 return sigaction(sig
, &sa
, NULL
);
1655 int close_pipe(int p
[]) {
1661 a
= close_nointr(p
[0]);
1666 b
= close_nointr(p
[1]);
1670 return a
< 0 ? a
: b
;
1673 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
) {
1682 while (nbytes
> 0) {
1685 if ((k
= read(fd
, p
, nbytes
)) <= 0) {
1690 if (errno
== EAGAIN
) {
1691 struct pollfd pollfd
;
1695 pollfd
.events
= POLLIN
;
1697 if (poll(&pollfd
, 1, -1) < 0) {
1701 return n
> 0 ? n
: -errno
;
1704 if (pollfd
.revents
!= POLLIN
)
1705 return n
> 0 ? n
: -EIO
;
1710 return n
> 0 ? n
: (k
< 0 ? -errno
: 0);
1721 int path_is_mount_point(const char *t
) {
1725 if (lstat(t
, &a
) < 0) {
1727 if (errno
== ENOENT
)
1733 if (!(copy
= strdup(t
)))
1736 if (lstat(dirname(copy
), &b
) < 0) {
1743 return a
.st_dev
!= b
.st_dev
;
1746 static const char *const ioprio_class_table
[] = {
1747 [IOPRIO_CLASS_NONE
] = "none",
1748 [IOPRIO_CLASS_RT
] = "realtime",
1749 [IOPRIO_CLASS_BE
] = "best-effort",
1750 [IOPRIO_CLASS_IDLE
] = "idle"
1753 DEFINE_STRING_TABLE_LOOKUP(ioprio_class
, int);
1755 static const char *const sigchld_code_table
[] = {
1756 [CLD_EXITED
] = "exited",
1757 [CLD_KILLED
] = "killed",
1758 [CLD_DUMPED
] = "dumped",
1759 [CLD_TRAPPED
] = "trapped",
1760 [CLD_STOPPED
] = "stopped",
1761 [CLD_CONTINUED
] = "continued",
1764 DEFINE_STRING_TABLE_LOOKUP(sigchld_code
, int);
1766 static const char *const log_facility_table
[LOG_NFACILITIES
] = {
1767 [LOG_FAC(LOG_KERN
)] = "kern",
1768 [LOG_FAC(LOG_USER
)] = "user",
1769 [LOG_FAC(LOG_MAIL
)] = "mail",
1770 [LOG_FAC(LOG_DAEMON
)] = "daemon",
1771 [LOG_FAC(LOG_AUTH
)] = "auth",
1772 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
1773 [LOG_FAC(LOG_LPR
)] = "lpr",
1774 [LOG_FAC(LOG_NEWS
)] = "news",
1775 [LOG_FAC(LOG_UUCP
)] = "uucp",
1776 [LOG_FAC(LOG_CRON
)] = "cron",
1777 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
1778 [LOG_FAC(LOG_FTP
)] = "ftp",
1779 [LOG_FAC(LOG_LOCAL0
)] = "local0",
1780 [LOG_FAC(LOG_LOCAL1
)] = "local1",
1781 [LOG_FAC(LOG_LOCAL2
)] = "local2",
1782 [LOG_FAC(LOG_LOCAL3
)] = "local3",
1783 [LOG_FAC(LOG_LOCAL4
)] = "local4",
1784 [LOG_FAC(LOG_LOCAL5
)] = "local5",
1785 [LOG_FAC(LOG_LOCAL6
)] = "local6",
1786 [LOG_FAC(LOG_LOCAL7
)] = "local7"
1789 DEFINE_STRING_TABLE_LOOKUP(log_facility
, int);
1791 static const char *const log_level_table
[] = {
1792 [LOG_EMERG
] = "emerg",
1793 [LOG_ALERT
] = "alert",
1794 [LOG_CRIT
] = "crit",
1796 [LOG_WARNING
] = "warning",
1797 [LOG_NOTICE
] = "notice",
1798 [LOG_INFO
] = "info",
1799 [LOG_DEBUG
] = "debug"
1802 DEFINE_STRING_TABLE_LOOKUP(log_level
, int);
1804 static const char* const sched_policy_table
[] = {
1805 [SCHED_OTHER
] = "other",
1806 [SCHED_BATCH
] = "batch",
1807 [SCHED_IDLE
] = "idle",
1808 [SCHED_FIFO
] = "fifo",
1812 DEFINE_STRING_TABLE_LOOKUP(sched_policy
, int);
1814 static const char* const rlimit_table
[] = {
1815 [RLIMIT_CPU
] = "LimitCPU",
1816 [RLIMIT_FSIZE
] = "LimitFSIZE",
1817 [RLIMIT_DATA
] = "LimitDATA",
1818 [RLIMIT_STACK
] = "LimitSTACK",
1819 [RLIMIT_CORE
] = "LimitCORE",
1820 [RLIMIT_RSS
] = "LimitRSS",
1821 [RLIMIT_NOFILE
] = "LimitNOFILE",
1822 [RLIMIT_AS
] = "LimitAS",
1823 [RLIMIT_NPROC
] = "LimitNPROC",
1824 [RLIMIT_MEMLOCK
] = "LimitMEMLOCK",
1825 [RLIMIT_LOCKS
] = "LimitLOCKS",
1826 [RLIMIT_SIGPENDING
] = "LimitSIGPENDING",
1827 [RLIMIT_MSGQUEUE
] = "LimitMSGQUEUE",
1828 [RLIMIT_NICE
] = "LimitNICE",
1829 [RLIMIT_RTPRIO
] = "LimitRTPRIO",
1830 [RLIMIT_RTTIME
] = "LimitRTTIME"
1833 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);