]>
git.ipfire.org Git - thirdparty/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 bool path_equal(const char *a
, const char *b
) {
1147 if ((a
[0] == '/') != (b
[0] == '/'))
1153 a
+= strspn(a
, "/");
1154 b
+= strspn(b
, "/");
1156 if (*a
== 0 && *b
== 0)
1159 if (*a
== 0 || *b
== 0)
1162 j
= strcspn(a
, "/");
1163 k
= strcspn(b
, "/");
1168 if (memcmp(a
, b
, j
) != 0)
1176 char *ascii_strlower(char *t
) {
1181 for (p
= t
; *p
; p
++)
1182 if (*p
>= 'A' && *p
<= 'Z')
1183 *p
= *p
- 'A' + 'a';
1188 bool ignore_file(const char *filename
) {
1192 filename
[0] == '.' ||
1193 endswith(filename
, "~") ||
1194 endswith(filename
, ".rpmnew") ||
1195 endswith(filename
, ".rpmsave") ||
1196 endswith(filename
, ".rpmorig") ||
1197 endswith(filename
, ".dpkg-old") ||
1198 endswith(filename
, ".dpkg-new") ||
1199 endswith(filename
, ".swp");
1202 int fd_nonblock(int fd
, bool nonblock
) {
1207 if ((flags
= fcntl(fd
, F_GETFL
, 0)) < 0)
1211 flags
|= O_NONBLOCK
;
1213 flags
&= ~O_NONBLOCK
;
1215 if (fcntl(fd
, F_SETFL
, flags
) < 0)
1221 int fd_cloexec(int fd
, bool cloexec
) {
1226 if ((flags
= fcntl(fd
, F_GETFD
, 0)) < 0)
1230 flags
|= FD_CLOEXEC
;
1232 flags
&= ~FD_CLOEXEC
;
1234 if (fcntl(fd
, F_SETFD
, flags
) < 0)
1240 int close_all_fds(const int except
[], unsigned n_except
) {
1245 if (!(d
= opendir("/proc/self/fd")))
1248 while ((de
= readdir(d
))) {
1251 if (ignore_file(de
->d_name
))
1254 if ((r
= safe_atoi(de
->d_name
, &fd
)) < 0)
1268 for (i
= 0; i
< n_except
; i
++)
1269 if (except
[i
] == fd
) {
1278 if ((r
= close_nointr(fd
)) < 0) {
1279 /* Valgrind has its own FD and doesn't want to have it closed */
1292 bool chars_intersect(const char *a
, const char *b
) {
1295 /* Returns true if any of the chars in a are in b. */
1296 for (p
= a
; *p
; p
++)
1303 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
1313 sec
= (time_t) t
/ USEC_PER_SEC
;
1315 if (strftime(buf
, l
, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec
, &tm
)) <= 0)
1321 bool fstype_is_network(const char *fstype
) {
1322 static const char * const table
[] = {
1332 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1333 if (streq(table
[i
], fstype
))
1342 if ((fd
= open("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
)) < 0)
1347 TIOCL_GETKMSGREDIRECT
,
1351 if (ioctl(fd
, TIOCLINUX
, tiocl
) < 0)
1354 vt
= tiocl
[0] <= 0 ? 1 : tiocl
[0];
1357 if (ioctl(fd
, VT_ACTIVATE
, vt
) < 0)
1360 close_nointr_nofail(r
);
1364 int read_one_char(FILE *f
, char *ret
, bool *need_nl
) {
1365 struct termios old_termios
, new_termios
;
1372 if (tcgetattr(fileno(f
), &old_termios
) >= 0) {
1373 new_termios
= old_termios
;
1375 new_termios
.c_lflag
&= ~ICANON
;
1376 new_termios
.c_cc
[VMIN
] = 1;
1377 new_termios
.c_cc
[VTIME
] = 0;
1379 if (tcsetattr(fileno(f
), TCSADRAIN
, &new_termios
) >= 0) {
1382 k
= fread(&c
, 1, 1, f
);
1384 tcsetattr(fileno(f
), TCSADRAIN
, &old_termios
);
1390 *need_nl
= c
!= '\n';
1397 if (!(fgets(line
, sizeof(line
), f
)))
1402 if (strlen(line
) != 1)
1412 int ask(char *ret
, const char *replies
, const char *text
, ...) {
1421 bool need_nl
= true;
1429 if ((r
= read_one_char(stdin
, &c
, &need_nl
)) < 0) {
1431 if (r
== -EBADMSG
) {
1432 puts("Bad input, please try again.");
1443 if (strchr(replies
, c
)) {
1448 puts("Read unexpected character, please try again.");
1452 int reset_terminal(int fd
) {
1453 struct termios termios
;
1458 /* Set terminal to some sane defaults */
1460 if (tcgetattr(fd
, &termios
) < 0) {
1465 /* We only reset the stuff that matters to the software. How
1466 * hardware is set up we don't touch assuming that somebody
1467 * else will do that for us */
1469 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| ISTRIP
| INLCR
| IGNCR
| IUCLC
);
1470 termios
.c_iflag
|= ICRNL
| IMAXBEL
| IUTF8
;
1471 termios
.c_oflag
|= ONLCR
;
1472 termios
.c_cflag
|= CREAD
;
1473 termios
.c_lflag
= ISIG
| ICANON
| IEXTEN
| ECHO
| ECHOE
| ECHOK
| ECHOCTL
| ECHOPRT
| ECHOKE
;
1475 termios
.c_cc
[VINTR
] = 03; /* ^C */
1476 termios
.c_cc
[VQUIT
] = 034; /* ^\ */
1477 termios
.c_cc
[VERASE
] = 0177;
1478 termios
.c_cc
[VKILL
] = 025; /* ^X */
1479 termios
.c_cc
[VEOF
] = 04; /* ^D */
1480 termios
.c_cc
[VSTART
] = 021; /* ^Q */
1481 termios
.c_cc
[VSTOP
] = 023; /* ^S */
1482 termios
.c_cc
[VSUSP
] = 032; /* ^Z */
1483 termios
.c_cc
[VLNEXT
] = 026; /* ^V */
1484 termios
.c_cc
[VWERASE
] = 027; /* ^W */
1485 termios
.c_cc
[VREPRINT
] = 022; /* ^R */
1486 termios
.c_cc
[VEOL
] = 0;
1487 termios
.c_cc
[VEOL2
] = 0;
1489 termios
.c_cc
[VTIME
] = 0;
1490 termios
.c_cc
[VMIN
] = 1;
1492 if (tcsetattr(fd
, TCSANOW
, &termios
) < 0)
1496 /* Just in case, flush all crap out */
1497 tcflush(fd
, TCIOFLUSH
);
1502 int open_terminal(const char *name
, int mode
) {
1505 if ((fd
= open(name
, mode
)) < 0)
1508 if ((r
= isatty(fd
)) < 0) {
1509 close_nointr_nofail(fd
);
1514 close_nointr_nofail(fd
);
1521 int flush_fd(int fd
) {
1522 struct pollfd pollfd
;
1526 pollfd
.events
= POLLIN
;
1533 if ((r
= poll(&pollfd
, 1, 0)) < 0) {
1544 if ((l
= read(fd
, buf
, sizeof(buf
))) < 0) {
1549 if (errno
== EAGAIN
)
1560 int acquire_terminal(const char *name
, bool fail
, bool force
) {
1561 int fd
= -1, notify
= -1, r
, wd
;
1565 /* We use inotify to be notified when the tty is closed. We
1566 * create the watch before checking if we can actually acquire
1567 * it, so that we don't lose any event.
1569 * Note: strictly speaking this actually watches for the
1570 * device being closed, it does *not* really watch whether a
1571 * tty loses its controlling process. However, unless some
1572 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
1573 * its tty otherwise this will not become a problem. As long
1574 * as the administrator makes sure not configure any service
1575 * on the same tty as an untrusted user this should not be a
1576 * problem. (Which he probably should not do anyway.) */
1578 if (!fail
&& !force
) {
1579 if ((notify
= inotify_init1(IN_CLOEXEC
)) < 0) {
1584 if ((wd
= inotify_add_watch(notify
, name
, IN_CLOSE
)) < 0) {
1591 if ((r
= flush_fd(notify
)) < 0)
1594 /* We pass here O_NOCTTY only so that we can check the return
1595 * value TIOCSCTTY and have a reliable way to figure out if we
1596 * successfully became the controlling process of the tty */
1597 if ((fd
= open_terminal(name
, O_RDWR
|O_NOCTTY
)) < 0)
1600 /* First, try to get the tty */
1601 if ((r
= ioctl(fd
, TIOCSCTTY
, force
)) < 0 &&
1602 (force
|| fail
|| errno
!= EPERM
)) {
1612 assert(notify
>= 0);
1615 struct inotify_event e
;
1618 if ((l
= read(notify
, &e
, sizeof(e
))) != sizeof(e
)) {
1632 if (e
.wd
!= wd
|| !(e
.mask
& IN_CLOSE
)) {
1640 /* We close the tty fd here since if the old session
1641 * ended our handle will be dead. It's important that
1642 * we do this after sleeping, so that we don't enter
1643 * an endless loop. */
1644 close_nointr_nofail(fd
);
1648 close_nointr_nofail(notify
);
1650 if ((r
= reset_terminal(fd
)) < 0)
1651 log_warning("Failed to reset terminal: %s", strerror(-r
));
1657 close_nointr_nofail(fd
);
1660 close_nointr_nofail(notify
);
1665 int release_terminal(void) {
1668 if ((fd
= open("/dev/tty", O_RDWR
)) < 0)
1671 if (ioctl(fd
, TIOCNOTTY
) < 0)
1674 close_nointr_nofail(fd
);
1678 int ignore_signal(int sig
) {
1679 struct sigaction sa
;
1682 sa
.sa_handler
= SIG_IGN
;
1683 sa
.sa_flags
= SA_RESTART
;
1685 return sigaction(sig
, &sa
, NULL
);
1688 int close_pipe(int p
[]) {
1694 a
= close_nointr(p
[0]);
1699 b
= close_nointr(p
[1]);
1703 return a
< 0 ? a
: b
;
1706 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
) {
1715 while (nbytes
> 0) {
1718 if ((k
= read(fd
, p
, nbytes
)) <= 0) {
1723 if (errno
== EAGAIN
) {
1724 struct pollfd pollfd
;
1728 pollfd
.events
= POLLIN
;
1730 if (poll(&pollfd
, 1, -1) < 0) {
1734 return n
> 0 ? n
: -errno
;
1737 if (pollfd
.revents
!= POLLIN
)
1738 return n
> 0 ? n
: -EIO
;
1743 return n
> 0 ? n
: (k
< 0 ? -errno
: 0);
1754 int path_is_mount_point(const char *t
) {
1758 if (lstat(t
, &a
) < 0) {
1760 if (errno
== ENOENT
)
1766 if (!(copy
= strdup(t
)))
1769 if (lstat(dirname(copy
), &b
) < 0) {
1776 return a
.st_dev
!= b
.st_dev
;
1779 static const char *const ioprio_class_table
[] = {
1780 [IOPRIO_CLASS_NONE
] = "none",
1781 [IOPRIO_CLASS_RT
] = "realtime",
1782 [IOPRIO_CLASS_BE
] = "best-effort",
1783 [IOPRIO_CLASS_IDLE
] = "idle"
1786 DEFINE_STRING_TABLE_LOOKUP(ioprio_class
, int);
1788 static const char *const sigchld_code_table
[] = {
1789 [CLD_EXITED
] = "exited",
1790 [CLD_KILLED
] = "killed",
1791 [CLD_DUMPED
] = "dumped",
1792 [CLD_TRAPPED
] = "trapped",
1793 [CLD_STOPPED
] = "stopped",
1794 [CLD_CONTINUED
] = "continued",
1797 DEFINE_STRING_TABLE_LOOKUP(sigchld_code
, int);
1799 static const char *const log_facility_table
[LOG_NFACILITIES
] = {
1800 [LOG_FAC(LOG_KERN
)] = "kern",
1801 [LOG_FAC(LOG_USER
)] = "user",
1802 [LOG_FAC(LOG_MAIL
)] = "mail",
1803 [LOG_FAC(LOG_DAEMON
)] = "daemon",
1804 [LOG_FAC(LOG_AUTH
)] = "auth",
1805 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
1806 [LOG_FAC(LOG_LPR
)] = "lpr",
1807 [LOG_FAC(LOG_NEWS
)] = "news",
1808 [LOG_FAC(LOG_UUCP
)] = "uucp",
1809 [LOG_FAC(LOG_CRON
)] = "cron",
1810 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
1811 [LOG_FAC(LOG_FTP
)] = "ftp",
1812 [LOG_FAC(LOG_LOCAL0
)] = "local0",
1813 [LOG_FAC(LOG_LOCAL1
)] = "local1",
1814 [LOG_FAC(LOG_LOCAL2
)] = "local2",
1815 [LOG_FAC(LOG_LOCAL3
)] = "local3",
1816 [LOG_FAC(LOG_LOCAL4
)] = "local4",
1817 [LOG_FAC(LOG_LOCAL5
)] = "local5",
1818 [LOG_FAC(LOG_LOCAL6
)] = "local6",
1819 [LOG_FAC(LOG_LOCAL7
)] = "local7"
1822 DEFINE_STRING_TABLE_LOOKUP(log_facility
, int);
1824 static const char *const log_level_table
[] = {
1825 [LOG_EMERG
] = "emerg",
1826 [LOG_ALERT
] = "alert",
1827 [LOG_CRIT
] = "crit",
1829 [LOG_WARNING
] = "warning",
1830 [LOG_NOTICE
] = "notice",
1831 [LOG_INFO
] = "info",
1832 [LOG_DEBUG
] = "debug"
1835 DEFINE_STRING_TABLE_LOOKUP(log_level
, int);
1837 static const char* const sched_policy_table
[] = {
1838 [SCHED_OTHER
] = "other",
1839 [SCHED_BATCH
] = "batch",
1840 [SCHED_IDLE
] = "idle",
1841 [SCHED_FIFO
] = "fifo",
1845 DEFINE_STRING_TABLE_LOOKUP(sched_policy
, int);
1847 static const char* const rlimit_table
[] = {
1848 [RLIMIT_CPU
] = "LimitCPU",
1849 [RLIMIT_FSIZE
] = "LimitFSIZE",
1850 [RLIMIT_DATA
] = "LimitDATA",
1851 [RLIMIT_STACK
] = "LimitSTACK",
1852 [RLIMIT_CORE
] = "LimitCORE",
1853 [RLIMIT_RSS
] = "LimitRSS",
1854 [RLIMIT_NOFILE
] = "LimitNOFILE",
1855 [RLIMIT_AS
] = "LimitAS",
1856 [RLIMIT_NPROC
] = "LimitNPROC",
1857 [RLIMIT_MEMLOCK
] = "LimitMEMLOCK",
1858 [RLIMIT_LOCKS
] = "LimitLOCKS",
1859 [RLIMIT_SIGPENDING
] = "LimitSIGPENDING",
1860 [RLIMIT_MSGQUEUE
] = "LimitMSGQUEUE",
1861 [RLIMIT_NICE
] = "LimitNICE",
1862 [RLIMIT_RTPRIO
] = "LimitRTPRIO",
1863 [RLIMIT_RTTIME
] = "LimitRTTIME"
1866 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);