]>
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 streq(filename
, "lost+found") ||
1203 endswith(filename
, "~") ||
1204 endswith(filename
, ".rpmnew") ||
1205 endswith(filename
, ".rpmsave") ||
1206 endswith(filename
, ".rpmorig") ||
1207 endswith(filename
, ".dpkg-old") ||
1208 endswith(filename
, ".dpkg-new") ||
1209 endswith(filename
, ".swp");
1212 int fd_nonblock(int fd
, bool nonblock
) {
1217 if ((flags
= fcntl(fd
, F_GETFL
, 0)) < 0)
1221 flags
|= O_NONBLOCK
;
1223 flags
&= ~O_NONBLOCK
;
1225 if (fcntl(fd
, F_SETFL
, flags
) < 0)
1231 int fd_cloexec(int fd
, bool cloexec
) {
1236 if ((flags
= fcntl(fd
, F_GETFD
, 0)) < 0)
1240 flags
|= FD_CLOEXEC
;
1242 flags
&= ~FD_CLOEXEC
;
1244 if (fcntl(fd
, F_SETFD
, flags
) < 0)
1250 int close_all_fds(const int except
[], unsigned n_except
) {
1255 if (!(d
= opendir("/proc/self/fd")))
1258 while ((de
= readdir(d
))) {
1261 if (ignore_file(de
->d_name
))
1264 if ((r
= safe_atoi(de
->d_name
, &fd
)) < 0)
1278 for (i
= 0; i
< n_except
; i
++)
1279 if (except
[i
] == fd
) {
1288 if ((r
= close_nointr(fd
)) < 0) {
1289 /* Valgrind has its own FD and doesn't want to have it closed */
1302 bool chars_intersect(const char *a
, const char *b
) {
1305 /* Returns true if any of the chars in a are in b. */
1306 for (p
= a
; *p
; p
++)
1313 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
1323 sec
= (time_t) t
/ USEC_PER_SEC
;
1325 if (strftime(buf
, l
, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec
, &tm
)) <= 0)
1331 bool fstype_is_network(const char *fstype
) {
1332 static const char * const table
[] = {
1342 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1343 if (streq(table
[i
], fstype
))
1352 if ((fd
= open("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
)) < 0)
1357 TIOCL_GETKMSGREDIRECT
,
1361 if (ioctl(fd
, TIOCLINUX
, tiocl
) < 0)
1364 vt
= tiocl
[0] <= 0 ? 1 : tiocl
[0];
1367 if (ioctl(fd
, VT_ACTIVATE
, vt
) < 0)
1370 close_nointr_nofail(r
);
1374 int read_one_char(FILE *f
, char *ret
, bool *need_nl
) {
1375 struct termios old_termios
, new_termios
;
1382 if (tcgetattr(fileno(f
), &old_termios
) >= 0) {
1383 new_termios
= old_termios
;
1385 new_termios
.c_lflag
&= ~ICANON
;
1386 new_termios
.c_cc
[VMIN
] = 1;
1387 new_termios
.c_cc
[VTIME
] = 0;
1389 if (tcsetattr(fileno(f
), TCSADRAIN
, &new_termios
) >= 0) {
1392 k
= fread(&c
, 1, 1, f
);
1394 tcsetattr(fileno(f
), TCSADRAIN
, &old_termios
);
1400 *need_nl
= c
!= '\n';
1407 if (!(fgets(line
, sizeof(line
), f
)))
1412 if (strlen(line
) != 1)
1422 int ask(char *ret
, const char *replies
, const char *text
, ...) {
1431 bool need_nl
= true;
1433 fputs("\x1B[1m", stdout
);
1439 fputs("\x1B[0m", stdout
);
1443 if ((r
= read_one_char(stdin
, &c
, &need_nl
)) < 0) {
1445 if (r
== -EBADMSG
) {
1446 puts("Bad input, please try again.");
1457 if (strchr(replies
, c
)) {
1462 puts("Read unexpected character, please try again.");
1466 int reset_terminal(int fd
) {
1467 struct termios termios
;
1472 /* Set terminal to some sane defaults */
1474 if (tcgetattr(fd
, &termios
) < 0) {
1479 /* We only reset the stuff that matters to the software. How
1480 * hardware is set up we don't touch assuming that somebody
1481 * else will do that for us */
1483 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| ISTRIP
| INLCR
| IGNCR
| IUCLC
);
1484 termios
.c_iflag
|= ICRNL
| IMAXBEL
| IUTF8
;
1485 termios
.c_oflag
|= ONLCR
;
1486 termios
.c_cflag
|= CREAD
;
1487 termios
.c_lflag
= ISIG
| ICANON
| IEXTEN
| ECHO
| ECHOE
| ECHOK
| ECHOCTL
| ECHOPRT
| ECHOKE
;
1489 termios
.c_cc
[VINTR
] = 03; /* ^C */
1490 termios
.c_cc
[VQUIT
] = 034; /* ^\ */
1491 termios
.c_cc
[VERASE
] = 0177;
1492 termios
.c_cc
[VKILL
] = 025; /* ^X */
1493 termios
.c_cc
[VEOF
] = 04; /* ^D */
1494 termios
.c_cc
[VSTART
] = 021; /* ^Q */
1495 termios
.c_cc
[VSTOP
] = 023; /* ^S */
1496 termios
.c_cc
[VSUSP
] = 032; /* ^Z */
1497 termios
.c_cc
[VLNEXT
] = 026; /* ^V */
1498 termios
.c_cc
[VWERASE
] = 027; /* ^W */
1499 termios
.c_cc
[VREPRINT
] = 022; /* ^R */
1500 termios
.c_cc
[VEOL
] = 0;
1501 termios
.c_cc
[VEOL2
] = 0;
1503 termios
.c_cc
[VTIME
] = 0;
1504 termios
.c_cc
[VMIN
] = 1;
1506 if (tcsetattr(fd
, TCSANOW
, &termios
) < 0)
1510 /* Just in case, flush all crap out */
1511 tcflush(fd
, TCIOFLUSH
);
1516 int open_terminal(const char *name
, int mode
) {
1519 if ((fd
= open(name
, mode
)) < 0)
1522 if ((r
= isatty(fd
)) < 0) {
1523 close_nointr_nofail(fd
);
1528 close_nointr_nofail(fd
);
1535 int flush_fd(int fd
) {
1536 struct pollfd pollfd
;
1540 pollfd
.events
= POLLIN
;
1547 if ((r
= poll(&pollfd
, 1, 0)) < 0) {
1558 if ((l
= read(fd
, buf
, sizeof(buf
))) < 0) {
1563 if (errno
== EAGAIN
)
1574 int acquire_terminal(const char *name
, bool fail
, bool force
) {
1575 int fd
= -1, notify
= -1, r
, wd
;
1579 /* We use inotify to be notified when the tty is closed. We
1580 * create the watch before checking if we can actually acquire
1581 * it, so that we don't lose any event.
1583 * Note: strictly speaking this actually watches for the
1584 * device being closed, it does *not* really watch whether a
1585 * tty loses its controlling process. However, unless some
1586 * rogue process uses TIOCNOTTY on /dev/tty *after* closing
1587 * its tty otherwise this will not become a problem. As long
1588 * as the administrator makes sure not configure any service
1589 * on the same tty as an untrusted user this should not be a
1590 * problem. (Which he probably should not do anyway.) */
1592 if (!fail
&& !force
) {
1593 if ((notify
= inotify_init1(IN_CLOEXEC
)) < 0) {
1598 if ((wd
= inotify_add_watch(notify
, name
, IN_CLOSE
)) < 0) {
1605 if ((r
= flush_fd(notify
)) < 0)
1608 /* We pass here O_NOCTTY only so that we can check the return
1609 * value TIOCSCTTY and have a reliable way to figure out if we
1610 * successfully became the controlling process of the tty */
1611 if ((fd
= open_terminal(name
, O_RDWR
|O_NOCTTY
)) < 0)
1614 /* First, try to get the tty */
1615 if ((r
= ioctl(fd
, TIOCSCTTY
, force
)) < 0 &&
1616 (force
|| fail
|| errno
!= EPERM
)) {
1626 assert(notify
>= 0);
1629 struct inotify_event e
;
1632 if ((l
= read(notify
, &e
, sizeof(e
))) != sizeof(e
)) {
1646 if (e
.wd
!= wd
|| !(e
.mask
& IN_CLOSE
)) {
1654 /* We close the tty fd here since if the old session
1655 * ended our handle will be dead. It's important that
1656 * we do this after sleeping, so that we don't enter
1657 * an endless loop. */
1658 close_nointr_nofail(fd
);
1662 close_nointr_nofail(notify
);
1664 if ((r
= reset_terminal(fd
)) < 0)
1665 log_warning("Failed to reset terminal: %s", strerror(-r
));
1671 close_nointr_nofail(fd
);
1674 close_nointr_nofail(notify
);
1679 int release_terminal(void) {
1681 struct sigaction sa_old
, sa_new
;
1683 if ((fd
= open("/dev/tty", O_RDWR
|O_NOCTTY
|O_NDELAY
)) < 0)
1686 /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
1687 * by our own TIOCNOTTY */
1690 sa_new
.sa_handler
= SIG_IGN
;
1691 sa_new
.sa_flags
= SA_RESTART
;
1692 assert_se(sigaction(SIGHUP
, &sa_new
, &sa_old
) == 0);
1694 if (ioctl(fd
, TIOCNOTTY
) < 0)
1697 assert_se(sigaction(SIGHUP
, &sa_old
, NULL
) == 0);
1699 close_nointr_nofail(fd
);
1703 int ignore_signal(int sig
) {
1704 struct sigaction sa
;
1707 sa
.sa_handler
= SIG_IGN
;
1708 sa
.sa_flags
= SA_RESTART
;
1710 return sigaction(sig
, &sa
, NULL
);
1713 int close_pipe(int p
[]) {
1719 a
= close_nointr(p
[0]);
1724 b
= close_nointr(p
[1]);
1728 return a
< 0 ? a
: b
;
1731 ssize_t
loop_read(int fd
, void *buf
, size_t nbytes
) {
1740 while (nbytes
> 0) {
1743 if ((k
= read(fd
, p
, nbytes
)) <= 0) {
1748 if (errno
== EAGAIN
) {
1749 struct pollfd pollfd
;
1753 pollfd
.events
= POLLIN
;
1755 if (poll(&pollfd
, 1, -1) < 0) {
1759 return n
> 0 ? n
: -errno
;
1762 if (pollfd
.revents
!= POLLIN
)
1763 return n
> 0 ? n
: -EIO
;
1768 return n
> 0 ? n
: (k
< 0 ? -errno
: 0);
1779 int path_is_mount_point(const char *t
) {
1783 if (lstat(t
, &a
) < 0) {
1785 if (errno
== ENOENT
)
1791 if (!(copy
= strdup(t
)))
1794 if (lstat(dirname(copy
), &b
) < 0) {
1801 return a
.st_dev
!= b
.st_dev
;
1804 int parse_usec(const char *t
, usec_t
*usec
) {
1805 static const struct {
1809 { "sec", USEC_PER_SEC
},
1810 { "s", USEC_PER_SEC
},
1811 { "min", USEC_PER_MINUTE
},
1812 { "hr", USEC_PER_HOUR
},
1813 { "h", USEC_PER_HOUR
},
1814 { "d", USEC_PER_DAY
},
1815 { "w", USEC_PER_WEEK
},
1816 { "msec", USEC_PER_MSEC
},
1817 { "ms", USEC_PER_MSEC
},
1818 { "m", USEC_PER_MINUTE
},
1821 { "", USEC_PER_SEC
},
1837 l
= strtoll(p
, &e
, 10);
1848 e
+= strspn(e
, WHITESPACE
);
1850 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1851 if (startswith(e
, table
[i
].suffix
)) {
1852 r
+= (usec_t
) l
* table
[i
].usec
;
1853 p
= e
+ strlen(table
[i
].suffix
);
1857 if (i
>= ELEMENTSOF(table
))
1867 static const char *const ioprio_class_table
[] = {
1868 [IOPRIO_CLASS_NONE
] = "none",
1869 [IOPRIO_CLASS_RT
] = "realtime",
1870 [IOPRIO_CLASS_BE
] = "best-effort",
1871 [IOPRIO_CLASS_IDLE
] = "idle"
1874 DEFINE_STRING_TABLE_LOOKUP(ioprio_class
, int);
1876 static const char *const sigchld_code_table
[] = {
1877 [CLD_EXITED
] = "exited",
1878 [CLD_KILLED
] = "killed",
1879 [CLD_DUMPED
] = "dumped",
1880 [CLD_TRAPPED
] = "trapped",
1881 [CLD_STOPPED
] = "stopped",
1882 [CLD_CONTINUED
] = "continued",
1885 DEFINE_STRING_TABLE_LOOKUP(sigchld_code
, int);
1887 static const char *const log_facility_table
[LOG_NFACILITIES
] = {
1888 [LOG_FAC(LOG_KERN
)] = "kern",
1889 [LOG_FAC(LOG_USER
)] = "user",
1890 [LOG_FAC(LOG_MAIL
)] = "mail",
1891 [LOG_FAC(LOG_DAEMON
)] = "daemon",
1892 [LOG_FAC(LOG_AUTH
)] = "auth",
1893 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
1894 [LOG_FAC(LOG_LPR
)] = "lpr",
1895 [LOG_FAC(LOG_NEWS
)] = "news",
1896 [LOG_FAC(LOG_UUCP
)] = "uucp",
1897 [LOG_FAC(LOG_CRON
)] = "cron",
1898 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
1899 [LOG_FAC(LOG_FTP
)] = "ftp",
1900 [LOG_FAC(LOG_LOCAL0
)] = "local0",
1901 [LOG_FAC(LOG_LOCAL1
)] = "local1",
1902 [LOG_FAC(LOG_LOCAL2
)] = "local2",
1903 [LOG_FAC(LOG_LOCAL3
)] = "local3",
1904 [LOG_FAC(LOG_LOCAL4
)] = "local4",
1905 [LOG_FAC(LOG_LOCAL5
)] = "local5",
1906 [LOG_FAC(LOG_LOCAL6
)] = "local6",
1907 [LOG_FAC(LOG_LOCAL7
)] = "local7"
1910 DEFINE_STRING_TABLE_LOOKUP(log_facility
, int);
1912 static const char *const log_level_table
[] = {
1913 [LOG_EMERG
] = "emerg",
1914 [LOG_ALERT
] = "alert",
1915 [LOG_CRIT
] = "crit",
1917 [LOG_WARNING
] = "warning",
1918 [LOG_NOTICE
] = "notice",
1919 [LOG_INFO
] = "info",
1920 [LOG_DEBUG
] = "debug"
1923 DEFINE_STRING_TABLE_LOOKUP(log_level
, int);
1925 static const char* const sched_policy_table
[] = {
1926 [SCHED_OTHER
] = "other",
1927 [SCHED_BATCH
] = "batch",
1928 [SCHED_IDLE
] = "idle",
1929 [SCHED_FIFO
] = "fifo",
1933 DEFINE_STRING_TABLE_LOOKUP(sched_policy
, int);
1935 static const char* const rlimit_table
[] = {
1936 [RLIMIT_CPU
] = "LimitCPU",
1937 [RLIMIT_FSIZE
] = "LimitFSIZE",
1938 [RLIMIT_DATA
] = "LimitDATA",
1939 [RLIMIT_STACK
] = "LimitSTACK",
1940 [RLIMIT_CORE
] = "LimitCORE",
1941 [RLIMIT_RSS
] = "LimitRSS",
1942 [RLIMIT_NOFILE
] = "LimitNOFILE",
1943 [RLIMIT_AS
] = "LimitAS",
1944 [RLIMIT_NPROC
] = "LimitNPROC",
1945 [RLIMIT_MEMLOCK
] = "LimitMEMLOCK",
1946 [RLIMIT_LOCKS
] = "LimitLOCKS",
1947 [RLIMIT_SIGPENDING
] = "LimitSIGPENDING",
1948 [RLIMIT_MSGQUEUE
] = "LimitMSGQUEUE",
1949 [RLIMIT_NICE
] = "LimitNICE",
1950 [RLIMIT_RTPRIO
] = "LimitRTPRIO",
1951 [RLIMIT_RTTIME
] = "LimitRTTIME"
1954 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);