]>
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
);
123 return memcmp(s
+ sl
- pl
, postfix
, pl
) == 0;
126 bool startswith(const char *s
, const char *prefix
) {
141 return memcmp(s
, prefix
, pl
) == 0;
144 bool first_word(const char *s
, const char *word
) {
159 if (memcmp(s
, word
, wl
) != 0)
163 strchr(WHITESPACE
, s
[wl
]);
166 int close_nointr(int fd
) {
172 if ((r
= close(fd
)) >= 0)
180 void close_nointr_nofail(int fd
) {
181 int saved_errno
= errno
;
183 /* like close_nointr() but cannot fail, and guarantees errno
186 assert_se(close_nointr(fd
) == 0);
191 int parse_boolean(const char *v
) {
194 if (streq(v
, "1") || v
[0] == 'y' || v
[0] == 'Y' || v
[0] == 't' || v
[0] == 'T' || !strcasecmp(v
, "on"))
196 else if (streq(v
, "0") || v
[0] == 'n' || v
[0] == 'N' || v
[0] == 'f' || v
[0] == 'F' || !strcasecmp(v
, "off"))
202 int safe_atou(const char *s
, unsigned *ret_u
) {
210 l
= strtoul(s
, &x
, 0);
212 if (!x
|| *x
|| errno
)
213 return errno
? -errno
: -EINVAL
;
215 if ((unsigned long) (unsigned) l
!= l
)
218 *ret_u
= (unsigned) l
;
222 int safe_atoi(const char *s
, int *ret_i
) {
230 l
= strtol(s
, &x
, 0);
232 if (!x
|| *x
|| errno
)
233 return errno
? -errno
: -EINVAL
;
235 if ((long) (int) l
!= l
)
242 int safe_atolu(const char *s
, long unsigned *ret_lu
) {
250 l
= strtoul(s
, &x
, 0);
252 if (!x
|| *x
|| errno
)
253 return errno
? -errno
: -EINVAL
;
259 int safe_atoli(const char *s
, long int *ret_li
) {
267 l
= strtol(s
, &x
, 0);
269 if (!x
|| *x
|| errno
)
270 return errno
? -errno
: -EINVAL
;
276 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
278 unsigned long long l
;
284 l
= strtoull(s
, &x
, 0);
286 if (!x
|| *x
|| errno
)
287 return errno
? -errno
: -EINVAL
;
293 int safe_atolli(const char *s
, long long int *ret_lli
) {
301 l
= strtoll(s
, &x
, 0);
303 if (!x
|| *x
|| errno
)
304 return errno
? -errno
: -EINVAL
;
310 /* Split a string into words. */
311 char *split(const char *c
, size_t *l
, const char *separator
, char **state
) {
314 current
= *state
? *state
: (char*) c
;
316 if (!*current
|| *c
== 0)
319 current
+= strspn(current
, separator
);
320 *l
= strcspn(current
, separator
);
323 return (char*) current
;
326 /* Split a string into words, but consider strings enclosed in '' and
327 * "" as words even if they include spaces. */
328 char *split_quoted(const char *c
, size_t *l
, char **state
) {
331 current
= *state
? *state
: (char*) c
;
333 if (!*current
|| *c
== 0)
336 current
+= strspn(current
, WHITESPACE
);
338 if (*current
== '\'') {
340 *l
= strcspn(current
, "'");
345 } else if (*current
== '\"') {
347 *l
= strcspn(current
, "\"");
353 *l
= strcspn(current
, WHITESPACE
);
357 /* FIXME: Cannot deal with strings that have spaces AND ticks
360 return (char*) current
;
363 char **split_path_and_make_absolute(const char *p
) {
367 if (!(l
= strv_split(p
, ":")))
370 if (!strv_path_make_absolute_cwd(l
)) {
378 int get_parent_of_pid(pid_t pid
, pid_t
*_ppid
) {
381 char fn
[132], line
[256], *p
;
382 long long unsigned ppid
;
387 assert_se(snprintf(fn
, sizeof(fn
)-1, "/proc/%llu/stat", (unsigned long long) pid
) < (int) (sizeof(fn
)-1));
388 fn
[sizeof(fn
)-1] = 0;
390 if (!(f
= fopen(fn
, "r")))
393 if (!(fgets(line
, sizeof(line
), f
))) {
401 /* Let's skip the pid and comm fields. The latter is enclosed
402 * in () but does not escape any () in its value, so let's
403 * skip over it manually */
405 if (!(p
= strrchr(line
, ')')))
416 if ((long long unsigned) (pid_t
) ppid
!= ppid
)
419 *_ppid
= (pid_t
) ppid
;
424 int write_one_line_file(const char *fn
, const char *line
) {
431 if (!(f
= fopen(fn
, "we")))
434 if (fputs(line
, f
) < 0) {
445 int read_one_line_file(const char *fn
, char **line
) {
453 if (!(f
= fopen(fn
, "re")))
456 if (!(fgets(t
, sizeof(t
), f
))) {
461 if (!(c
= strdup(t
))) {
474 char *truncate_nl(char *s
) {
477 s
[strcspn(s
, NEWLINE
)] = 0;
481 int get_process_name(pid_t pid
, char **name
) {
488 if (asprintf(&p
, "/proc/%llu/comm", (unsigned long long) pid
) < 0)
491 r
= read_one_line_file(p
, name
);
501 char *strappend(const char *s
, const char *suffix
) {
511 if (!(r
= new(char, a
+b
+1)))
515 memcpy(r
+a
, suffix
, b
);
521 int readlink_malloc(const char *p
, char **r
) {
531 if (!(c
= new(char, l
)))
534 if ((n
= readlink(p
, c
, l
-1)) < 0) {
540 if ((size_t) n
< l
-1) {
551 char *file_name_from_path(const char *p
) {
556 if ((r
= strrchr(p
, '/')))
562 bool path_is_absolute(const char *p
) {
568 bool is_path(const char *p
) {
570 return !!strchr(p
, '/');
573 char *path_make_absolute(const char *p
, const char *prefix
) {
578 /* Makes every item in the list an absolute path by prepending
579 * the prefix, if specified and necessary */
581 if (path_is_absolute(p
) || !prefix
)
584 if (asprintf(&r
, "%s/%s", prefix
, p
) < 0)
590 char *path_make_absolute_cwd(const char *p
) {
595 /* Similar to path_make_absolute(), but prefixes with the
596 * current working directory. */
598 if (path_is_absolute(p
))
601 if (!(cwd
= get_current_dir_name()))
604 r
= path_make_absolute(p
, cwd
);
610 char **strv_path_make_absolute_cwd(char **l
) {
613 /* Goes through every item in the string list and makes it
614 * absolute. This works in place and won't rollback any
615 * changes on failure. */
620 if (!(t
= path_make_absolute_cwd(*s
)))
630 int reset_all_signal_handlers(void) {
633 for (sig
= 1; sig
< _NSIG
; sig
++) {
636 if (sig
== SIGKILL
|| sig
== SIGSTOP
)
640 sa
.sa_handler
= SIG_DFL
;
641 sa
.sa_flags
= SA_RESTART
;
643 /* On Linux the first two RT signals are reserved by
644 * glibc, and sigaction() will return EINVAL for them. */
645 if ((sigaction(sig
, &sa
, NULL
) < 0))
653 char *strstrip(char *s
) {
656 /* Drops trailing whitespace. Modifies the string in
657 * place. Returns pointer to first non-space character */
659 s
+= strspn(s
, WHITESPACE
);
662 if (!strchr(WHITESPACE
, *e
))
673 char *delete_chars(char *s
, const char *bad
) {
676 /* Drops all whitespace, regardless where in the string */
678 for (f
= s
, t
= s
; *f
; f
++) {
690 char *file_in_same_dir(const char *path
, const char *filename
) {
697 /* This removes the last component of path and appends
698 * filename, unless the latter is absolute anyway or the
701 if (path_is_absolute(filename
))
702 return strdup(filename
);
704 if (!(e
= strrchr(path
, '/')))
705 return strdup(filename
);
707 k
= strlen(filename
);
708 if (!(r
= new(char, e
-path
+1+k
+1)))
711 memcpy(r
, path
, e
-path
+1);
712 memcpy(r
+(e
-path
)+1, filename
, k
+1);
717 int mkdir_parents(const char *path
, mode_t mode
) {
722 /* Creates every parent directory in the path except the last
725 p
= path
+ strspn(path
, "/");
730 e
= p
+ strcspn(p
, "/");
731 p
= e
+ strspn(e
, "/");
733 /* Is this the last component? If so, then we're
738 if (!(t
= strndup(path
, e
- path
)))
745 if (r
< 0 && errno
!= EEXIST
)
750 int mkdir_p(const char *path
, mode_t mode
) {
755 if ((r
= mkdir_parents(path
, mode
)) < 0)
758 if (mkdir(path
, mode
) < 0)
764 char hexchar(int x
) {
765 static const char table
[16] = "0123456789abcdef";
767 return table
[x
& 15];
770 int unhexchar(char c
) {
772 if (c
>= '0' && c
<= '9')
775 if (c
>= 'a' && c
<= 'f')
778 if (c
>= 'A' && c
<= 'F')
784 char octchar(int x
) {
785 return '0' + (x
& 7);
788 int unoctchar(char c
) {
790 if (c
>= '0' && c
<= '7')
796 char decchar(int x
) {
797 return '0' + (x
% 10);
800 int undecchar(char c
) {
802 if (c
>= '0' && c
<= '9')
808 char *cescape(const char *s
) {
814 /* Does C style string escaping. */
816 if (!(r
= new(char, strlen(s
)*4 + 1)))
819 for (f
= s
, t
= r
; *f
; f
++)
865 /* For special chars we prefer octal over
866 * hexadecimal encoding, simply because glib's
867 * g_strescape() does the same */
868 if ((*f
< ' ') || (*f
>= 127)) {
870 *(t
++) = octchar((unsigned char) *f
>> 6);
871 *(t
++) = octchar((unsigned char) *f
>> 3);
872 *(t
++) = octchar((unsigned char) *f
);
883 char *cunescape(const char *s
) {
889 /* Undoes C style string escaping */
891 if (!(r
= new(char, strlen(s
)+1)))
894 for (f
= s
, t
= r
; *f
; f
++) {
937 /* hexadecimal encoding */
940 if ((a
= unhexchar(f
[1])) < 0 ||
941 (b
= unhexchar(f
[2])) < 0) {
942 /* Invalid escape code, let's take it literal then */
946 *(t
++) = (char) ((a
<< 4) | b
);
964 if ((a
= unoctchar(f
[0])) < 0 ||
965 (b
= unoctchar(f
[1])) < 0 ||
966 (c
= unoctchar(f
[2])) < 0) {
967 /* Invalid escape code, let's take it literal then */
971 *(t
++) = (char) ((a
<< 6) | (b
<< 3) | c
);
979 /* premature end of string.*/
984 /* Invalid escape code, let's take it literal then */
997 char *xescape(const char *s
, const char *bad
) {
1001 /* Escapes all chars in bad, in addition to \ and all special
1002 * chars, in \xFF style escaping. May be reversed with
1005 if (!(r
= new(char, strlen(s
)*4+1)))
1008 for (f
= s
, t
= r
; *f
; f
++) {
1010 if ((*f
< ' ') || (*f
>= 127) ||
1011 (*f
== '\\') || strchr(bad
, *f
)) {
1014 *(t
++) = hexchar(*f
>> 4);
1015 *(t
++) = hexchar(*f
);
1025 char *bus_path_escape(const char *s
) {
1031 /* Escapes all chars that D-Bus' object path cannot deal
1032 * with. Can be reverse with bus_path_unescape() */
1034 if (!(r
= new(char, strlen(s
)*3+1)))
1037 for (f
= s
, t
= r
; *f
; f
++) {
1039 if (!(*f
>= 'A' && *f
<= 'Z') &&
1040 !(*f
>= 'a' && *f
<= 'z') &&
1041 !(*f
>= '0' && *f
<= '9')) {
1043 *(t
++) = hexchar(*f
>> 4);
1044 *(t
++) = hexchar(*f
);
1054 char *bus_path_unescape(const char *f
) {
1059 if (!(r
= strdup(f
)))
1062 for (t
= r
; *f
; f
++) {
1067 if ((a
= unhexchar(f
[1])) < 0 ||
1068 (b
= unhexchar(f
[2])) < 0) {
1069 /* Invalid escape code, let's take it literal then */
1072 *(t
++) = (char) ((a
<< 4) | b
);
1084 char *path_kill_slashes(char *path
) {
1088 /* Removes redundant inner and trailing slashes. Modifies the
1089 * passed string in-place.
1091 * ///foo///bar/ becomes /foo/bar
1094 for (f
= path
, t
= path
; *f
; f
++) {
1109 /* Special rule, if we are talking of the root directory, a
1110 trailing slash is good */
1112 if (t
== path
&& slash
)
1119 bool path_startswith(const char *path
, const char *prefix
) {
1123 if ((path
[0] == '/') != (prefix
[0] == '/'))
1129 path
+= strspn(path
, "/");
1130 prefix
+= strspn(prefix
, "/");
1138 a
= strcspn(path
, "/");
1139 b
= strcspn(prefix
, "/");
1144 if (memcmp(path
, prefix
, a
) != 0)
1152 bool path_equal(const char *a
, const char *b
) {
1156 if ((a
[0] == '/') != (b
[0] == '/'))
1162 a
+= strspn(a
, "/");
1163 b
+= strspn(b
, "/");
1165 if (*a
== 0 && *b
== 0)
1168 if (*a
== 0 || *b
== 0)
1171 j
= strcspn(a
, "/");
1172 k
= strcspn(b
, "/");
1177 if (memcmp(a
, b
, j
) != 0)
1185 char *ascii_strlower(char *t
) {
1190 for (p
= t
; *p
; p
++)
1191 if (*p
>= 'A' && *p
<= 'Z')
1192 *p
= *p
- 'A' + 'a';
1197 bool ignore_file(const char *filename
) {
1201 filename
[0] == '.' ||
1202 endswith(filename
, "~") ||
1203 endswith(filename
, ".rpmnew") ||
1204 endswith(filename
, ".rpmsave") ||
1205 endswith(filename
, ".rpmorig") ||
1206 endswith(filename
, ".dpkg-old") ||
1207 endswith(filename
, ".dpkg-new") ||
1208 endswith(filename
, ".swp");
1211 int fd_nonblock(int fd
, bool nonblock
) {
1216 if ((flags
= fcntl(fd
, F_GETFL
, 0)) < 0)
1220 flags
|= O_NONBLOCK
;
1222 flags
&= ~O_NONBLOCK
;
1224 if (fcntl(fd
, F_SETFL
, flags
) < 0)
1230 int fd_cloexec(int fd
, bool cloexec
) {
1235 if ((flags
= fcntl(fd
, F_GETFD
, 0)) < 0)
1239 flags
|= FD_CLOEXEC
;
1241 flags
&= ~FD_CLOEXEC
;
1243 if (fcntl(fd
, F_SETFD
, flags
) < 0)
1249 int close_all_fds(const int except
[], unsigned n_except
) {
1254 if (!(d
= opendir("/proc/self/fd")))
1257 while ((de
= readdir(d
))) {
1260 if (ignore_file(de
->d_name
))
1263 if ((r
= safe_atoi(de
->d_name
, &fd
)) < 0)
1277 for (i
= 0; i
< n_except
; i
++)
1278 if (except
[i
] == fd
) {
1287 if ((r
= close_nointr(fd
)) < 0) {
1288 /* Valgrind has its own FD and doesn't want to have it closed */
1301 bool chars_intersect(const char *a
, const char *b
) {
1304 /* Returns true if any of the chars in a are in b. */
1305 for (p
= a
; *p
; p
++)
1312 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
1322 sec
= (time_t) t
/ USEC_PER_SEC
;
1324 if (strftime(buf
, l
, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec
, &tm
)) <= 0)
1330 bool fstype_is_network(const char *fstype
) {
1331 static const char * const table
[] = {
1341 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1342 if (streq(table
[i
], fstype
))
1351 if ((fd
= open("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
)) < 0)
1356 TIOCL_GETKMSGREDIRECT
,
1360 if (ioctl(fd
, TIOCLINUX
, tiocl
) < 0)
1363 vt
= tiocl
[0] <= 0 ? 1 : tiocl
[0];
1366 if (ioctl(fd
, VT_ACTIVATE
, vt
) < 0)
1369 close_nointr_nofail(r
);
1373 int read_one_char(FILE *f
, char *ret
, bool *need_nl
) {
1374 struct termios old_termios
, new_termios
;
1381 if (tcgetattr(fileno(f
), &old_termios
) >= 0) {
1382 new_termios
= old_termios
;
1384 new_termios
.c_lflag
&= ~ICANON
;
1385 new_termios
.c_cc
[VMIN
] = 1;
1386 new_termios
.c_cc
[VTIME
] = 0;
1388 if (tcsetattr(fileno(f
), TCSADRAIN
, &new_termios
) >= 0) {
1391 k
= fread(&c
, 1, 1, f
);
1393 tcsetattr(fileno(f
), TCSADRAIN
, &old_termios
);
1399 *need_nl
= c
!= '\n';
1406 if (!(fgets(line
, sizeof(line
), f
)))
1411 if (strlen(line
) != 1)
1421 int ask(char *ret
, const char *replies
, const char *text
, ...) {
1430 bool need_nl
= true;
1432 fputs("\x1B[1m", stdout
);
1438 fputs("\x1B[0m", stdout
);
1442 if ((r
= read_one_char(stdin
, &c
, &need_nl
)) < 0) {
1444 if (r
== -EBADMSG
) {
1445 puts("Bad input, please try again.");
1456 if (strchr(replies
, c
)) {
1461 puts("Read unexpected character, please try again.");
1465 int reset_terminal(int fd
) {
1466 struct termios termios
;
1471 /* Set terminal to some sane defaults */
1473 if (tcgetattr(fd
, &termios
) < 0) {
1478 /* We only reset the stuff that matters to the software. How
1479 * hardware is set up we don't touch assuming that somebody
1480 * else will do that for us */
1482 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| ISTRIP
| INLCR
| IGNCR
| IUCLC
);
1483 termios
.c_iflag
|= ICRNL
| IMAXBEL
| IUTF8
;
1484 termios
.c_oflag
|= ONLCR
;
1485 termios
.c_cflag
|= CREAD
;
1486 termios
.c_lflag
= ISIG
| ICANON
| IEXTEN
| ECHO
| ECHOE
| ECHOK
| ECHOCTL
| ECHOPRT
| ECHOKE
;
1488 termios
.c_cc
[VINTR
] = 03; /* ^C */
1489 termios
.c_cc
[VQUIT
] = 034; /* ^\ */
1490 termios
.c_cc
[VERASE
] = 0177;
1491 termios
.c_cc
[VKILL
] = 025; /* ^X */
1492 termios
.c_cc
[VEOF
] = 04; /* ^D */
1493 termios
.c_cc
[VSTART
] = 021; /* ^Q */
1494 termios
.c_cc
[VSTOP
] = 023; /* ^S */
1495 termios
.c_cc
[VSUSP
] = 032; /* ^Z */
1496 termios
.c_cc
[VLNEXT
] = 026; /* ^V */
1497 termios
.c_cc
[VWERASE
] = 027; /* ^W */
1498 termios
.c_cc
[VREPRINT
] = 022; /* ^R */
1499 termios
.c_cc
[VEOL
] = 0;
1500 termios
.c_cc
[VEOL2
] = 0;
1502 termios
.c_cc
[VTIME
] = 0;
1503 termios
.c_cc
[VMIN
] = 1;
1505 if (tcsetattr(fd
, TCSANOW
, &termios
) < 0)
1509 /* Just in case, flush all crap out */
1510 tcflush(fd
, TCIOFLUSH
);
1515 int open_terminal(const char *name
, int mode
) {
1518 if ((fd
= open(name
, mode
)) < 0)
1521 if ((r
= isatty(fd
)) < 0) {
1522 close_nointr_nofail(fd
);
1527 close_nointr_nofail(fd
);
1534 int flush_fd(int fd
) {
1535 struct pollfd pollfd
;
1539 pollfd
.events
= POLLIN
;
1546 if ((r
= poll(&pollfd
, 1, 0)) < 0) {
1557 if ((l
= read(fd
, buf
, sizeof(buf
))) < 0) {
1562 if (errno
== EAGAIN
)
1573 int acquire_terminal(const char *name
, bool fail
, bool force
) {
1574 int fd
= -1, notify
= -1, r
, wd
;
1578 /* We use inotify to be notified when the tty is closed. We
1579 * create the watch before checking if we can actually acquire
1580 * it, so that we don't lose any event.
1582 * Note: strictly speaking this actually watches for the
1583 * device being closed, it does *not* really watch whether a
1584 * tty loses its controlling process. However, unless some
1585 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
1586 * its tty otherwise this will not become a problem. As long
1587 * as the administrator makes sure not configure any service
1588 * on the same tty as an untrusted user this should not be a
1589 * problem. (Which he probably should not do anyway.) */
1591 if (!fail
&& !force
) {
1592 if ((notify
= inotify_init1(IN_CLOEXEC
)) < 0) {
1597 if ((wd
= inotify_add_watch(notify
, name
, IN_CLOSE
)) < 0) {
1604 if ((r
= flush_fd(notify
)) < 0)
1607 /* We pass here O_NOCTTY only so that we can check the return
1608 * value TIOCSCTTY and have a reliable way to figure out if we
1609 * successfully became the controlling process of the tty */
1610 if ((fd
= open_terminal(name
, O_RDWR
|O_NOCTTY
)) < 0)
1613 /* First, try to get the tty */
1614 if ((r
= ioctl(fd
, TIOCSCTTY
, force
)) < 0 &&
1615 (force
|| fail
|| errno
!= EPERM
)) {
1625 assert(notify
>= 0);
1628 struct inotify_event e
;
1631 if ((l
= read(notify
, &e
, sizeof(e
))) != sizeof(e
)) {
1645 if (e
.wd
!= wd
|| !(e
.mask
& IN_CLOSE
)) {
1653 /* We close the tty fd here since if the old session
1654 * ended our handle will be dead. It's important that
1655 * we do this after sleeping, so that we don't enter
1656 * an endless loop. */
1657 close_nointr_nofail(fd
);
1661 close_nointr_nofail(notify
);
1663 if ((r
= reset_terminal(fd
)) < 0)
1664 log_warning("Failed to reset terminal: %s", strerror(-r
));
1670 close_nointr_nofail(fd
);
1673 close_nointr_nofail(notify
);
1678 int release_terminal(void) {
1681 if ((fd
= open("/dev/tty", O_RDWR
)) < 0)
1684 if (ioctl(fd
, TIOCNOTTY
) < 0)
1687 close_nointr_nofail(fd
);
1691 int ignore_signal(int sig
) {
1692 struct sigaction sa
;
1695 sa
.sa_handler
= SIG_IGN
;
1696 sa
.sa_flags
= SA_RESTART
;
1698 return sigaction(sig
, &sa
, NULL
);
1701 int close_pipe(int p
[]) {
1707 a
= close_nointr(p
[0]);
1712 b
= close_nointr(p
[1]);
1716 return a
< 0 ? a
: b
;
1719 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
) {
1728 while (nbytes
> 0) {
1731 if ((k
= read(fd
, p
, nbytes
)) <= 0) {
1736 if (errno
== EAGAIN
) {
1737 struct pollfd pollfd
;
1741 pollfd
.events
= POLLIN
;
1743 if (poll(&pollfd
, 1, -1) < 0) {
1747 return n
> 0 ? n
: -errno
;
1750 if (pollfd
.revents
!= POLLIN
)
1751 return n
> 0 ? n
: -EIO
;
1756 return n
> 0 ? n
: (k
< 0 ? -errno
: 0);
1767 int path_is_mount_point(const char *t
) {
1771 if (lstat(t
, &a
) < 0) {
1773 if (errno
== ENOENT
)
1779 if (!(copy
= strdup(t
)))
1782 if (lstat(dirname(copy
), &b
) < 0) {
1789 return a
.st_dev
!= b
.st_dev
;
1792 int parse_usec(const char *t
, usec_t
*usec
) {
1793 static const struct {
1797 { "sec", USEC_PER_SEC
},
1798 { "s", USEC_PER_SEC
},
1799 { "min", USEC_PER_MINUTE
},
1800 { "hr", USEC_PER_HOUR
},
1801 { "h", USEC_PER_HOUR
},
1802 { "d", USEC_PER_DAY
},
1803 { "w", USEC_PER_WEEK
},
1804 { "msec", USEC_PER_MSEC
},
1805 { "ms", USEC_PER_MSEC
},
1806 { "m", USEC_PER_MINUTE
},
1809 { "", USEC_PER_SEC
},
1825 l
= strtoll(p
, &e
, 10);
1836 e
+= strspn(e
, WHITESPACE
);
1838 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1839 if (startswith(e
, table
[i
].suffix
)) {
1840 r
+= (usec_t
) l
* table
[i
].usec
;
1841 p
= e
+ strlen(table
[i
].suffix
);
1845 if (i
>= ELEMENTSOF(table
))
1855 static const char *const ioprio_class_table
[] = {
1856 [IOPRIO_CLASS_NONE
] = "none",
1857 [IOPRIO_CLASS_RT
] = "realtime",
1858 [IOPRIO_CLASS_BE
] = "best-effort",
1859 [IOPRIO_CLASS_IDLE
] = "idle"
1862 DEFINE_STRING_TABLE_LOOKUP(ioprio_class
, int);
1864 static const char *const sigchld_code_table
[] = {
1865 [CLD_EXITED
] = "exited",
1866 [CLD_KILLED
] = "killed",
1867 [CLD_DUMPED
] = "dumped",
1868 [CLD_TRAPPED
] = "trapped",
1869 [CLD_STOPPED
] = "stopped",
1870 [CLD_CONTINUED
] = "continued",
1873 DEFINE_STRING_TABLE_LOOKUP(sigchld_code
, int);
1875 static const char *const log_facility_table
[LOG_NFACILITIES
] = {
1876 [LOG_FAC(LOG_KERN
)] = "kern",
1877 [LOG_FAC(LOG_USER
)] = "user",
1878 [LOG_FAC(LOG_MAIL
)] = "mail",
1879 [LOG_FAC(LOG_DAEMON
)] = "daemon",
1880 [LOG_FAC(LOG_AUTH
)] = "auth",
1881 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
1882 [LOG_FAC(LOG_LPR
)] = "lpr",
1883 [LOG_FAC(LOG_NEWS
)] = "news",
1884 [LOG_FAC(LOG_UUCP
)] = "uucp",
1885 [LOG_FAC(LOG_CRON
)] = "cron",
1886 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
1887 [LOG_FAC(LOG_FTP
)] = "ftp",
1888 [LOG_FAC(LOG_LOCAL0
)] = "local0",
1889 [LOG_FAC(LOG_LOCAL1
)] = "local1",
1890 [LOG_FAC(LOG_LOCAL2
)] = "local2",
1891 [LOG_FAC(LOG_LOCAL3
)] = "local3",
1892 [LOG_FAC(LOG_LOCAL4
)] = "local4",
1893 [LOG_FAC(LOG_LOCAL5
)] = "local5",
1894 [LOG_FAC(LOG_LOCAL6
)] = "local6",
1895 [LOG_FAC(LOG_LOCAL7
)] = "local7"
1898 DEFINE_STRING_TABLE_LOOKUP(log_facility
, int);
1900 static const char *const log_level_table
[] = {
1901 [LOG_EMERG
] = "emerg",
1902 [LOG_ALERT
] = "alert",
1903 [LOG_CRIT
] = "crit",
1905 [LOG_WARNING
] = "warning",
1906 [LOG_NOTICE
] = "notice",
1907 [LOG_INFO
] = "info",
1908 [LOG_DEBUG
] = "debug"
1911 DEFINE_STRING_TABLE_LOOKUP(log_level
, int);
1913 static const char* const sched_policy_table
[] = {
1914 [SCHED_OTHER
] = "other",
1915 [SCHED_BATCH
] = "batch",
1916 [SCHED_IDLE
] = "idle",
1917 [SCHED_FIFO
] = "fifo",
1921 DEFINE_STRING_TABLE_LOOKUP(sched_policy
, int);
1923 static const char* const rlimit_table
[] = {
1924 [RLIMIT_CPU
] = "LimitCPU",
1925 [RLIMIT_FSIZE
] = "LimitFSIZE",
1926 [RLIMIT_DATA
] = "LimitDATA",
1927 [RLIMIT_STACK
] = "LimitSTACK",
1928 [RLIMIT_CORE
] = "LimitCORE",
1929 [RLIMIT_RSS
] = "LimitRSS",
1930 [RLIMIT_NOFILE
] = "LimitNOFILE",
1931 [RLIMIT_AS
] = "LimitAS",
1932 [RLIMIT_NPROC
] = "LimitNPROC",
1933 [RLIMIT_MEMLOCK
] = "LimitMEMLOCK",
1934 [RLIMIT_LOCKS
] = "LimitLOCKS",
1935 [RLIMIT_SIGPENDING
] = "LimitSIGPENDING",
1936 [RLIMIT_MSGQUEUE
] = "LimitMSGQUEUE",
1937 [RLIMIT_NICE
] = "LimitNICE",
1938 [RLIMIT_RTPRIO
] = "LimitRTPRIO",
1939 [RLIMIT_RTTIME
] = "LimitRTTIME"
1942 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);