]>
Commit | Line | Data |
---|---|---|
60918275 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. | |
20 | ***/ | |
21 | ||
60918275 LP |
22 | #include <assert.h> |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <errno.h> | |
85261803 | 26 | #include <stdlib.h> |
034c6ed7 LP |
27 | #include <signal.h> |
28 | #include <stdio.h> | |
1dccbe19 LP |
29 | #include <syslog.h> |
30 | #include <sched.h> | |
31 | #include <sys/resource.h> | |
ef886c6a | 32 | #include <linux/sched.h> |
a9f5d454 LP |
33 | #include <sys/types.h> |
34 | #include <sys/stat.h> | |
3a0ecb08 | 35 | #include <fcntl.h> |
a0d40ac5 | 36 | #include <dirent.h> |
601f6a1e LP |
37 | #include <sys/ioctl.h> |
38 | #include <linux/vt.h> | |
39 | #include <linux/tiocl.h> | |
80876c20 LP |
40 | #include <termios.h> |
41 | #include <stdarg.h> | |
42 | #include <sys/inotify.h> | |
43 | #include <sys/poll.h> | |
8d567588 | 44 | #include <libgen.h> |
60918275 LP |
45 | |
46 | #include "macro.h" | |
47 | #include "util.h" | |
1dccbe19 LP |
48 | #include "ioprio.h" |
49 | #include "missing.h" | |
a9f5d454 | 50 | #include "log.h" |
65d2ebdc | 51 | #include "strv.h" |
60918275 | 52 | |
e05797fb LP |
53 | bool streq_ptr(const char *a, const char *b) { |
54 | ||
55 | /* Like streq(), but tries to make sense of NULL pointers */ | |
56 | ||
57 | if (a && b) | |
58 | return streq(a, b); | |
59 | ||
60 | if (!a && !b) | |
61 | return true; | |
62 | ||
63 | return false; | |
64 | } | |
65 | ||
47be870b | 66 | usec_t now(clockid_t clock_id) { |
60918275 LP |
67 | struct timespec ts; |
68 | ||
47be870b | 69 | assert_se(clock_gettime(clock_id, &ts) == 0); |
60918275 LP |
70 | |
71 | return timespec_load(&ts); | |
72 | } | |
73 | ||
74 | usec_t timespec_load(const struct timespec *ts) { | |
75 | assert(ts); | |
76 | ||
77 | return | |
78 | (usec_t) ts->tv_sec * USEC_PER_SEC + | |
79 | (usec_t) ts->tv_nsec / NSEC_PER_USEC; | |
80 | } | |
81 | ||
82 | struct timespec *timespec_store(struct timespec *ts, usec_t u) { | |
83 | assert(ts); | |
84 | ||
85 | ts->tv_sec = (time_t) (u / USEC_PER_SEC); | |
86 | ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC); | |
87 | ||
88 | return ts; | |
89 | } | |
90 | ||
91 | usec_t timeval_load(const struct timeval *tv) { | |
92 | assert(tv); | |
93 | ||
94 | return | |
95 | (usec_t) tv->tv_sec * USEC_PER_SEC + | |
96 | (usec_t) tv->tv_usec; | |
97 | } | |
98 | ||
99 | struct timeval *timeval_store(struct timeval *tv, usec_t u) { | |
100 | assert(tv); | |
101 | ||
102 | tv->tv_sec = (time_t) (u / USEC_PER_SEC); | |
103 | tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC); | |
104 | ||
105 | return tv; | |
106 | } | |
107 | ||
108 | bool endswith(const char *s, const char *postfix) { | |
109 | size_t sl, pl; | |
110 | ||
111 | assert(s); | |
112 | assert(postfix); | |
113 | ||
114 | sl = strlen(s); | |
115 | pl = strlen(postfix); | |
116 | ||
d4d0d4db LP |
117 | if (pl == 0) |
118 | return true; | |
119 | ||
60918275 LP |
120 | if (sl < pl) |
121 | return false; | |
122 | ||
123 | return memcmp(s + sl - pl, postfix, pl) == 0; | |
124 | } | |
125 | ||
126 | bool startswith(const char *s, const char *prefix) { | |
127 | size_t sl, pl; | |
128 | ||
129 | assert(s); | |
130 | assert(prefix); | |
131 | ||
132 | sl = strlen(s); | |
133 | pl = strlen(prefix); | |
134 | ||
d4d0d4db LP |
135 | if (pl == 0) |
136 | return true; | |
137 | ||
60918275 LP |
138 | if (sl < pl) |
139 | return false; | |
140 | ||
141 | return memcmp(s, prefix, pl) == 0; | |
142 | } | |
143 | ||
79d6d816 LP |
144 | bool first_word(const char *s, const char *word) { |
145 | size_t sl, wl; | |
146 | ||
147 | assert(s); | |
148 | assert(word); | |
149 | ||
150 | sl = strlen(s); | |
151 | wl = strlen(word); | |
152 | ||
153 | if (sl < wl) | |
154 | return false; | |
155 | ||
d4d0d4db LP |
156 | if (wl == 0) |
157 | return true; | |
158 | ||
79d6d816 LP |
159 | if (memcmp(s, word, wl) != 0) |
160 | return false; | |
161 | ||
d4d0d4db LP |
162 | return s[wl] == 0 || |
163 | strchr(WHITESPACE, s[wl]); | |
79d6d816 LP |
164 | } |
165 | ||
42f4e3c4 | 166 | int close_nointr(int fd) { |
60918275 LP |
167 | assert(fd >= 0); |
168 | ||
169 | for (;;) { | |
170 | int r; | |
171 | ||
172 | if ((r = close(fd)) >= 0) | |
173 | return r; | |
174 | ||
175 | if (errno != EINTR) | |
176 | return r; | |
177 | } | |
178 | } | |
85261803 | 179 | |
85f136b5 | 180 | void close_nointr_nofail(int fd) { |
80876c20 | 181 | int saved_errno = errno; |
85f136b5 LP |
182 | |
183 | /* like close_nointr() but cannot fail, and guarantees errno | |
184 | * is unchanged */ | |
185 | ||
186 | assert_se(close_nointr(fd) == 0); | |
80876c20 LP |
187 | |
188 | errno = saved_errno; | |
85f136b5 LP |
189 | } |
190 | ||
85261803 LP |
191 | int parse_boolean(const char *v) { |
192 | assert(v); | |
193 | ||
44d8db9e | 194 | if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on")) |
85261803 | 195 | return 1; |
44d8db9e | 196 | else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off")) |
85261803 LP |
197 | return 0; |
198 | ||
199 | return -EINVAL; | |
200 | } | |
201 | ||
202 | int safe_atou(const char *s, unsigned *ret_u) { | |
203 | char *x = NULL; | |
034c6ed7 | 204 | unsigned long l; |
85261803 LP |
205 | |
206 | assert(s); | |
207 | assert(ret_u); | |
208 | ||
209 | errno = 0; | |
210 | l = strtoul(s, &x, 0); | |
211 | ||
212 | if (!x || *x || errno) | |
213 | return errno ? -errno : -EINVAL; | |
214 | ||
034c6ed7 | 215 | if ((unsigned long) (unsigned) l != l) |
85261803 LP |
216 | return -ERANGE; |
217 | ||
218 | *ret_u = (unsigned) l; | |
219 | return 0; | |
220 | } | |
221 | ||
222 | int safe_atoi(const char *s, int *ret_i) { | |
223 | char *x = NULL; | |
034c6ed7 | 224 | long l; |
85261803 LP |
225 | |
226 | assert(s); | |
227 | assert(ret_i); | |
228 | ||
229 | errno = 0; | |
230 | l = strtol(s, &x, 0); | |
231 | ||
232 | if (!x || *x || errno) | |
233 | return errno ? -errno : -EINVAL; | |
234 | ||
034c6ed7 | 235 | if ((long) (int) l != l) |
85261803 LP |
236 | return -ERANGE; |
237 | ||
034c6ed7 LP |
238 | *ret_i = (int) l; |
239 | return 0; | |
240 | } | |
241 | ||
242 | int safe_atolu(const char *s, long unsigned *ret_lu) { | |
243 | char *x = NULL; | |
244 | unsigned long l; | |
245 | ||
246 | assert(s); | |
247 | assert(ret_lu); | |
248 | ||
249 | errno = 0; | |
250 | l = strtoul(s, &x, 0); | |
251 | ||
252 | if (!x || *x || errno) | |
253 | return errno ? -errno : -EINVAL; | |
254 | ||
255 | *ret_lu = l; | |
256 | return 0; | |
257 | } | |
258 | ||
259 | int safe_atoli(const char *s, long int *ret_li) { | |
260 | char *x = NULL; | |
261 | long l; | |
262 | ||
263 | assert(s); | |
264 | assert(ret_li); | |
265 | ||
266 | errno = 0; | |
267 | l = strtol(s, &x, 0); | |
268 | ||
269 | if (!x || *x || errno) | |
270 | return errno ? -errno : -EINVAL; | |
271 | ||
272 | *ret_li = l; | |
273 | return 0; | |
274 | } | |
275 | ||
276 | int safe_atollu(const char *s, long long unsigned *ret_llu) { | |
277 | char *x = NULL; | |
278 | unsigned long long l; | |
279 | ||
280 | assert(s); | |
281 | assert(ret_llu); | |
282 | ||
283 | errno = 0; | |
284 | l = strtoull(s, &x, 0); | |
285 | ||
286 | if (!x || *x || errno) | |
287 | return errno ? -errno : -EINVAL; | |
288 | ||
289 | *ret_llu = l; | |
290 | return 0; | |
291 | } | |
292 | ||
293 | int safe_atolli(const char *s, long long int *ret_lli) { | |
294 | char *x = NULL; | |
295 | long long l; | |
296 | ||
297 | assert(s); | |
298 | assert(ret_lli); | |
299 | ||
300 | errno = 0; | |
301 | l = strtoll(s, &x, 0); | |
302 | ||
303 | if (!x || *x || errno) | |
304 | return errno ? -errno : -EINVAL; | |
305 | ||
306 | *ret_lli = l; | |
85261803 LP |
307 | return 0; |
308 | } | |
a41e8209 | 309 | |
a41e8209 | 310 | /* Split a string into words. */ |
65d2ebdc | 311 | char *split(const char *c, size_t *l, const char *separator, char **state) { |
a41e8209 LP |
312 | char *current; |
313 | ||
314 | current = *state ? *state : (char*) c; | |
315 | ||
316 | if (!*current || *c == 0) | |
317 | return NULL; | |
318 | ||
65d2ebdc LP |
319 | current += strspn(current, separator); |
320 | *l = strcspn(current, separator); | |
82919e3d LP |
321 | *state = current+*l; |
322 | ||
323 | return (char*) current; | |
324 | } | |
325 | ||
034c6ed7 LP |
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) { | |
329 | char *current; | |
330 | ||
331 | current = *state ? *state : (char*) c; | |
332 | ||
333 | if (!*current || *c == 0) | |
334 | return NULL; | |
335 | ||
336 | current += strspn(current, WHITESPACE); | |
337 | ||
338 | if (*current == '\'') { | |
339 | current ++; | |
340 | *l = strcspn(current, "'"); | |
341 | *state = current+*l; | |
342 | ||
343 | if (**state == '\'') | |
344 | (*state)++; | |
345 | } else if (*current == '\"') { | |
346 | current ++; | |
b858b600 | 347 | *l = strcspn(current, "\""); |
034c6ed7 LP |
348 | *state = current+*l; |
349 | ||
350 | if (**state == '\"') | |
351 | (*state)++; | |
352 | } else { | |
353 | *l = strcspn(current, WHITESPACE); | |
354 | *state = current+*l; | |
355 | } | |
356 | ||
44d8db9e LP |
357 | /* FIXME: Cannot deal with strings that have spaces AND ticks |
358 | * in them */ | |
359 | ||
034c6ed7 LP |
360 | return (char*) current; |
361 | } | |
362 | ||
65d2ebdc LP |
363 | char **split_path_and_make_absolute(const char *p) { |
364 | char **l; | |
365 | assert(p); | |
366 | ||
367 | if (!(l = strv_split(p, ":"))) | |
368 | return NULL; | |
369 | ||
370 | if (!strv_path_make_absolute_cwd(l)) { | |
371 | strv_free(l); | |
372 | return NULL; | |
373 | } | |
374 | ||
375 | return l; | |
376 | } | |
377 | ||
034c6ed7 LP |
378 | int get_parent_of_pid(pid_t pid, pid_t *_ppid) { |
379 | int r; | |
380 | FILE *f; | |
381 | char fn[132], line[256], *p; | |
382 | long long unsigned ppid; | |
383 | ||
384 | assert(pid >= 0); | |
385 | assert(_ppid); | |
386 | ||
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; | |
389 | ||
390 | if (!(f = fopen(fn, "r"))) | |
391 | return -errno; | |
392 | ||
393 | if (!(fgets(line, sizeof(line), f))) { | |
394 | r = -errno; | |
395 | fclose(f); | |
396 | return r; | |
397 | } | |
398 | ||
399 | fclose(f); | |
400 | ||
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 */ | |
404 | ||
405 | if (!(p = strrchr(line, ')'))) | |
406 | return -EIO; | |
407 | ||
408 | p++; | |
409 | ||
410 | if (sscanf(p, " " | |
411 | "%*c " /* state */ | |
412 | "%llu ", /* ppid */ | |
413 | &ppid) != 1) | |
414 | return -EIO; | |
415 | ||
416 | if ((long long unsigned) (pid_t) ppid != ppid) | |
417 | return -ERANGE; | |
418 | ||
419 | *_ppid = (pid_t) ppid; | |
420 | ||
421 | return 0; | |
422 | } | |
423 | ||
424 | int write_one_line_file(const char *fn, const char *line) { | |
425 | FILE *f; | |
426 | int r; | |
427 | ||
428 | assert(fn); | |
429 | assert(line); | |
430 | ||
431 | if (!(f = fopen(fn, "we"))) | |
432 | return -errno; | |
433 | ||
434 | if (fputs(line, f) < 0) { | |
435 | r = -errno; | |
436 | goto finish; | |
437 | } | |
438 | ||
439 | r = 0; | |
440 | finish: | |
441 | fclose(f); | |
442 | return r; | |
443 | } | |
444 | ||
445 | int read_one_line_file(const char *fn, char **line) { | |
446 | FILE *f; | |
447 | int r; | |
11316633 | 448 | char t[2048], *c; |
034c6ed7 LP |
449 | |
450 | assert(fn); | |
451 | assert(line); | |
452 | ||
453 | if (!(f = fopen(fn, "re"))) | |
454 | return -errno; | |
455 | ||
456 | if (!(fgets(t, sizeof(t), f))) { | |
457 | r = -errno; | |
458 | goto finish; | |
459 | } | |
460 | ||
461 | if (!(c = strdup(t))) { | |
462 | r = -ENOMEM; | |
463 | goto finish; | |
464 | } | |
465 | ||
466 | *line = c; | |
467 | r = 0; | |
468 | ||
469 | finish: | |
470 | fclose(f); | |
471 | return r; | |
472 | } | |
44d8db9e | 473 | |
7072ced8 LP |
474 | char *truncate_nl(char *s) { |
475 | assert(s); | |
476 | ||
477 | s[strcspn(s, NEWLINE)] = 0; | |
478 | return s; | |
479 | } | |
480 | ||
481 | int get_process_name(pid_t pid, char **name) { | |
482 | char *p; | |
483 | int r; | |
484 | ||
485 | assert(pid >= 1); | |
486 | assert(name); | |
487 | ||
488 | if (asprintf(&p, "/proc/%llu/comm", (unsigned long long) pid) < 0) | |
489 | return -ENOMEM; | |
490 | ||
491 | r = read_one_line_file(p, name); | |
492 | free(p); | |
493 | ||
494 | if (r < 0) | |
495 | return r; | |
496 | ||
497 | truncate_nl(*name); | |
498 | return 0; | |
499 | } | |
500 | ||
44d8db9e LP |
501 | char *strappend(const char *s, const char *suffix) { |
502 | size_t a, b; | |
503 | char *r; | |
504 | ||
505 | assert(s); | |
506 | assert(suffix); | |
507 | ||
508 | a = strlen(s); | |
509 | b = strlen(suffix); | |
510 | ||
511 | if (!(r = new(char, a+b+1))) | |
512 | return NULL; | |
513 | ||
514 | memcpy(r, s, a); | |
515 | memcpy(r+a, suffix, b); | |
516 | r[a+b] = 0; | |
517 | ||
518 | return r; | |
519 | } | |
87f0e418 LP |
520 | |
521 | int readlink_malloc(const char *p, char **r) { | |
522 | size_t l = 100; | |
523 | ||
524 | assert(p); | |
525 | assert(r); | |
526 | ||
527 | for (;;) { | |
528 | char *c; | |
529 | ssize_t n; | |
530 | ||
531 | if (!(c = new(char, l))) | |
532 | return -ENOMEM; | |
533 | ||
534 | if ((n = readlink(p, c, l-1)) < 0) { | |
535 | int ret = -errno; | |
536 | free(c); | |
537 | return ret; | |
538 | } | |
539 | ||
540 | if ((size_t) n < l-1) { | |
541 | c[n] = 0; | |
542 | *r = c; | |
543 | return 0; | |
544 | } | |
545 | ||
546 | free(c); | |
547 | l *= 2; | |
548 | } | |
549 | } | |
550 | ||
551 | char *file_name_from_path(const char *p) { | |
552 | char *r; | |
553 | ||
554 | assert(p); | |
555 | ||
556 | if ((r = strrchr(p, '/'))) | |
557 | return r + 1; | |
558 | ||
559 | return (char*) p; | |
560 | } | |
0301abf4 LP |
561 | |
562 | bool path_is_absolute(const char *p) { | |
563 | assert(p); | |
564 | ||
565 | return p[0] == '/'; | |
566 | } | |
567 | ||
568 | bool is_path(const char *p) { | |
569 | ||
570 | return !!strchr(p, '/'); | |
571 | } | |
572 | ||
573 | char *path_make_absolute(const char *p, const char *prefix) { | |
574 | char *r; | |
575 | ||
576 | assert(p); | |
577 | ||
65d2ebdc LP |
578 | /* Makes every item in the list an absolute path by prepending |
579 | * the prefix, if specified and necessary */ | |
580 | ||
0301abf4 LP |
581 | if (path_is_absolute(p) || !prefix) |
582 | return strdup(p); | |
583 | ||
584 | if (asprintf(&r, "%s/%s", prefix, p) < 0) | |
585 | return NULL; | |
586 | ||
587 | return r; | |
588 | } | |
2a987ee8 | 589 | |
65d2ebdc LP |
590 | char *path_make_absolute_cwd(const char *p) { |
591 | char *cwd, *r; | |
592 | ||
593 | assert(p); | |
594 | ||
595 | /* Similar to path_make_absolute(), but prefixes with the | |
596 | * current working directory. */ | |
597 | ||
598 | if (path_is_absolute(p)) | |
599 | return strdup(p); | |
600 | ||
601 | if (!(cwd = get_current_dir_name())) | |
602 | return NULL; | |
603 | ||
604 | r = path_make_absolute(p, cwd); | |
605 | free(cwd); | |
606 | ||
607 | return r; | |
608 | } | |
609 | ||
610 | char **strv_path_make_absolute_cwd(char **l) { | |
611 | char **s; | |
612 | ||
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. */ | |
616 | ||
617 | STRV_FOREACH(s, l) { | |
618 | char *t; | |
619 | ||
620 | if (!(t = path_make_absolute_cwd(*s))) | |
621 | return NULL; | |
622 | ||
623 | free(*s); | |
624 | *s = t; | |
625 | } | |
626 | ||
627 | return l; | |
628 | } | |
629 | ||
2a987ee8 LP |
630 | int reset_all_signal_handlers(void) { |
631 | int sig; | |
632 | ||
633 | for (sig = 1; sig < _NSIG; sig++) { | |
634 | struct sigaction sa; | |
635 | ||
636 | if (sig == SIGKILL || sig == SIGSTOP) | |
637 | continue; | |
638 | ||
639 | zero(sa); | |
640 | sa.sa_handler = SIG_DFL; | |
431c32bf | 641 | sa.sa_flags = SA_RESTART; |
2a987ee8 LP |
642 | |
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)) | |
646 | if (errno != EINVAL) | |
647 | return -errno; | |
648 | } | |
649 | ||
8e274523 | 650 | return 0; |
2a987ee8 | 651 | } |
4a72ff34 LP |
652 | |
653 | char *strstrip(char *s) { | |
654 | char *e, *l = NULL; | |
655 | ||
656 | /* Drops trailing whitespace. Modifies the string in | |
657 | * place. Returns pointer to first non-space character */ | |
658 | ||
659 | s += strspn(s, WHITESPACE); | |
660 | ||
661 | for (e = s; *e; e++) | |
662 | if (!strchr(WHITESPACE, *e)) | |
663 | l = e; | |
664 | ||
665 | if (l) | |
666 | *(l+1) = 0; | |
667 | else | |
668 | *s = 0; | |
669 | ||
670 | return s; | |
4a72ff34 LP |
671 | } |
672 | ||
ee9b5e01 LP |
673 | char *delete_chars(char *s, const char *bad) { |
674 | char *f, *t; | |
675 | ||
676 | /* Drops all whitespace, regardless where in the string */ | |
677 | ||
678 | for (f = s, t = s; *f; f++) { | |
679 | if (strchr(bad, *f)) | |
680 | continue; | |
681 | ||
682 | *(t++) = *f; | |
683 | } | |
684 | ||
685 | *t = 0; | |
686 | ||
687 | return s; | |
688 | } | |
689 | ||
4a72ff34 LP |
690 | char *file_in_same_dir(const char *path, const char *filename) { |
691 | char *e, *r; | |
692 | size_t k; | |
693 | ||
694 | assert(path); | |
695 | assert(filename); | |
696 | ||
697 | /* This removes the last component of path and appends | |
698 | * filename, unless the latter is absolute anyway or the | |
699 | * former isn't */ | |
700 | ||
701 | if (path_is_absolute(filename)) | |
702 | return strdup(filename); | |
703 | ||
704 | if (!(e = strrchr(path, '/'))) | |
705 | return strdup(filename); | |
706 | ||
707 | k = strlen(filename); | |
708 | if (!(r = new(char, e-path+1+k+1))) | |
709 | return NULL; | |
710 | ||
711 | memcpy(r, path, e-path+1); | |
712 | memcpy(r+(e-path)+1, filename, k+1); | |
713 | ||
714 | return r; | |
715 | } | |
fb624d04 | 716 | |
a9f5d454 LP |
717 | int mkdir_parents(const char *path, mode_t mode) { |
718 | const char *p, *e; | |
719 | ||
720 | assert(path); | |
721 | ||
722 | /* Creates every parent directory in the path except the last | |
723 | * component. */ | |
724 | ||
725 | p = path + strspn(path, "/"); | |
726 | for (;;) { | |
727 | int r; | |
728 | char *t; | |
729 | ||
730 | e = p + strcspn(p, "/"); | |
731 | p = e + strspn(e, "/"); | |
732 | ||
733 | /* Is this the last component? If so, then we're | |
734 | * done */ | |
735 | if (*p == 0) | |
736 | return 0; | |
737 | ||
738 | if (!(t = strndup(path, e - path))) | |
739 | return -ENOMEM; | |
740 | ||
741 | r = mkdir(t, mode); | |
742 | ||
743 | free(t); | |
744 | ||
745 | if (r < 0 && errno != EEXIST) | |
746 | return -errno; | |
747 | } | |
748 | } | |
749 | ||
bbd67135 LP |
750 | int mkdir_p(const char *path, mode_t mode) { |
751 | int r; | |
752 | ||
753 | /* Like mkdir -p */ | |
754 | ||
755 | if ((r = mkdir_parents(path, mode)) < 0) | |
756 | return r; | |
757 | ||
758 | if (mkdir(path, mode) < 0) | |
759 | return -errno; | |
760 | ||
761 | return 0; | |
762 | } | |
763 | ||
fb624d04 LP |
764 | char hexchar(int x) { |
765 | static const char table[16] = "0123456789abcdef"; | |
766 | ||
767 | return table[x & 15]; | |
768 | } | |
4fe88d28 LP |
769 | |
770 | int unhexchar(char c) { | |
771 | ||
772 | if (c >= '0' && c <= '9') | |
773 | return c - '0'; | |
774 | ||
775 | if (c >= 'a' && c <= 'f') | |
ea430986 | 776 | return c - 'a' + 10; |
4fe88d28 LP |
777 | |
778 | if (c >= 'A' && c <= 'F') | |
ea430986 | 779 | return c - 'A' + 10; |
4fe88d28 LP |
780 | |
781 | return -1; | |
782 | } | |
783 | ||
784 | char octchar(int x) { | |
785 | return '0' + (x & 7); | |
786 | } | |
787 | ||
788 | int unoctchar(char c) { | |
789 | ||
790 | if (c >= '0' && c <= '7') | |
791 | return c - '0'; | |
792 | ||
793 | return -1; | |
794 | } | |
795 | ||
5af98f82 LP |
796 | char decchar(int x) { |
797 | return '0' + (x % 10); | |
798 | } | |
799 | ||
800 | int undecchar(char c) { | |
801 | ||
802 | if (c >= '0' && c <= '9') | |
803 | return c - '0'; | |
804 | ||
805 | return -1; | |
806 | } | |
807 | ||
4fe88d28 LP |
808 | char *cescape(const char *s) { |
809 | char *r, *t; | |
810 | const char *f; | |
811 | ||
812 | assert(s); | |
813 | ||
814 | /* Does C style string escaping. */ | |
815 | ||
816 | if (!(r = new(char, strlen(s)*4 + 1))) | |
817 | return NULL; | |
818 | ||
819 | for (f = s, t = r; *f; f++) | |
820 | ||
821 | switch (*f) { | |
822 | ||
823 | case '\a': | |
824 | *(t++) = '\\'; | |
825 | *(t++) = 'a'; | |
826 | break; | |
827 | case '\b': | |
828 | *(t++) = '\\'; | |
829 | *(t++) = 'b'; | |
830 | break; | |
831 | case '\f': | |
832 | *(t++) = '\\'; | |
833 | *(t++) = 'f'; | |
834 | break; | |
835 | case '\n': | |
836 | *(t++) = '\\'; | |
837 | *(t++) = 'n'; | |
838 | break; | |
839 | case '\r': | |
840 | *(t++) = '\\'; | |
841 | *(t++) = 'r'; | |
842 | break; | |
843 | case '\t': | |
844 | *(t++) = '\\'; | |
845 | *(t++) = 't'; | |
846 | break; | |
847 | case '\v': | |
848 | *(t++) = '\\'; | |
849 | *(t++) = 'v'; | |
850 | break; | |
851 | case '\\': | |
852 | *(t++) = '\\'; | |
853 | *(t++) = '\\'; | |
854 | break; | |
855 | case '"': | |
856 | *(t++) = '\\'; | |
857 | *(t++) = '"'; | |
858 | break; | |
859 | case '\'': | |
860 | *(t++) = '\\'; | |
861 | *(t++) = '\''; | |
862 | break; | |
863 | ||
864 | default: | |
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)) { | |
869 | *(t++) = '\\'; | |
870 | *(t++) = octchar((unsigned char) *f >> 6); | |
871 | *(t++) = octchar((unsigned char) *f >> 3); | |
872 | *(t++) = octchar((unsigned char) *f); | |
873 | } else | |
874 | *(t++) = *f; | |
875 | break; | |
876 | } | |
877 | ||
878 | *t = 0; | |
879 | ||
880 | return r; | |
881 | } | |
882 | ||
883 | char *cunescape(const char *s) { | |
884 | char *r, *t; | |
885 | const char *f; | |
886 | ||
887 | assert(s); | |
888 | ||
889 | /* Undoes C style string escaping */ | |
890 | ||
891 | if (!(r = new(char, strlen(s)+1))) | |
892 | return r; | |
893 | ||
894 | for (f = s, t = r; *f; f++) { | |
895 | ||
896 | if (*f != '\\') { | |
897 | *(t++) = *f; | |
898 | continue; | |
899 | } | |
900 | ||
901 | f++; | |
902 | ||
903 | switch (*f) { | |
904 | ||
905 | case 'a': | |
906 | *(t++) = '\a'; | |
907 | break; | |
908 | case 'b': | |
909 | *(t++) = '\b'; | |
910 | break; | |
911 | case 'f': | |
912 | *(t++) = '\f'; | |
913 | break; | |
914 | case 'n': | |
915 | *(t++) = '\n'; | |
916 | break; | |
917 | case 'r': | |
918 | *(t++) = '\r'; | |
919 | break; | |
920 | case 't': | |
921 | *(t++) = '\t'; | |
922 | break; | |
923 | case 'v': | |
924 | *(t++) = '\v'; | |
925 | break; | |
926 | case '\\': | |
927 | *(t++) = '\\'; | |
928 | break; | |
929 | case '"': | |
930 | *(t++) = '"'; | |
931 | break; | |
932 | case '\'': | |
933 | *(t++) = '\''; | |
934 | break; | |
935 | ||
936 | case 'x': { | |
937 | /* hexadecimal encoding */ | |
938 | int a, b; | |
939 | ||
940 | if ((a = unhexchar(f[1])) < 0 || | |
941 | (b = unhexchar(f[2])) < 0) { | |
942 | /* Invalid escape code, let's take it literal then */ | |
943 | *(t++) = '\\'; | |
944 | *(t++) = 'x'; | |
945 | } else { | |
946 | *(t++) = (char) ((a << 4) | b); | |
947 | f += 2; | |
948 | } | |
949 | ||
950 | break; | |
951 | } | |
952 | ||
953 | case '0': | |
954 | case '1': | |
955 | case '2': | |
956 | case '3': | |
957 | case '4': | |
958 | case '5': | |
959 | case '6': | |
960 | case '7': { | |
961 | /* octal encoding */ | |
962 | int a, b, c; | |
963 | ||
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 */ | |
968 | *(t++) = '\\'; | |
969 | *(t++) = f[0]; | |
970 | } else { | |
971 | *(t++) = (char) ((a << 6) | (b << 3) | c); | |
972 | f += 2; | |
973 | } | |
974 | ||
975 | break; | |
976 | } | |
977 | ||
978 | case 0: | |
979 | /* premature end of string.*/ | |
980 | *(t++) = '\\'; | |
981 | goto finish; | |
982 | ||
983 | default: | |
984 | /* Invalid escape code, let's take it literal then */ | |
985 | *(t++) = '\\'; | |
986 | *(t++) = 'f'; | |
987 | break; | |
988 | } | |
989 | } | |
990 | ||
991 | finish: | |
992 | *t = 0; | |
993 | return r; | |
994 | } | |
995 | ||
996 | ||
997 | char *xescape(const char *s, const char *bad) { | |
998 | char *r, *t; | |
999 | const char *f; | |
1000 | ||
1001 | /* Escapes all chars in bad, in addition to \ and all special | |
1002 | * chars, in \xFF style escaping. May be reversed with | |
1003 | * cunescape. */ | |
1004 | ||
1005 | if (!(r = new(char, strlen(s)*4+1))) | |
1006 | return NULL; | |
1007 | ||
1008 | for (f = s, t = r; *f; f++) { | |
1009 | ||
b866264a LP |
1010 | if ((*f < ' ') || (*f >= 127) || |
1011 | (*f == '\\') || strchr(bad, *f)) { | |
4fe88d28 LP |
1012 | *(t++) = '\\'; |
1013 | *(t++) = 'x'; | |
1014 | *(t++) = hexchar(*f >> 4); | |
1015 | *(t++) = hexchar(*f); | |
1016 | } else | |
1017 | *(t++) = *f; | |
1018 | } | |
1019 | ||
1020 | *t = 0; | |
1021 | ||
1022 | return r; | |
1023 | } | |
1024 | ||
ea430986 | 1025 | char *bus_path_escape(const char *s) { |
ea430986 LP |
1026 | char *r, *t; |
1027 | const char *f; | |
1028 | ||
47be870b LP |
1029 | assert(s); |
1030 | ||
ea430986 LP |
1031 | /* Escapes all chars that D-Bus' object path cannot deal |
1032 | * with. Can be reverse with bus_path_unescape() */ | |
1033 | ||
1034 | if (!(r = new(char, strlen(s)*3+1))) | |
1035 | return NULL; | |
1036 | ||
1037 | for (f = s, t = r; *f; f++) { | |
1038 | ||
1039 | if (!(*f >= 'A' && *f <= 'Z') && | |
1040 | !(*f >= 'a' && *f <= 'z') && | |
1041 | !(*f >= '0' && *f <= '9')) { | |
1042 | *(t++) = '_'; | |
1043 | *(t++) = hexchar(*f >> 4); | |
1044 | *(t++) = hexchar(*f); | |
1045 | } else | |
1046 | *(t++) = *f; | |
1047 | } | |
1048 | ||
1049 | *t = 0; | |
1050 | ||
1051 | return r; | |
1052 | } | |
1053 | ||
9e2f7c11 | 1054 | char *bus_path_unescape(const char *f) { |
ea430986 | 1055 | char *r, *t; |
ea430986 | 1056 | |
9e2f7c11 | 1057 | assert(f); |
47be870b | 1058 | |
9e2f7c11 | 1059 | if (!(r = strdup(f))) |
ea430986 LP |
1060 | return NULL; |
1061 | ||
9e2f7c11 | 1062 | for (t = r; *f; f++) { |
ea430986 LP |
1063 | |
1064 | if (*f == '_') { | |
1065 | int a, b; | |
1066 | ||
1067 | if ((a = unhexchar(f[1])) < 0 || | |
1068 | (b = unhexchar(f[2])) < 0) { | |
1069 | /* Invalid escape code, let's take it literal then */ | |
1070 | *(t++) = '_'; | |
1071 | } else { | |
1072 | *(t++) = (char) ((a << 4) | b); | |
1073 | f += 2; | |
1074 | } | |
1075 | } else | |
1076 | *(t++) = *f; | |
1077 | } | |
1078 | ||
1079 | *t = 0; | |
1080 | ||
1081 | return r; | |
1082 | } | |
1083 | ||
4fe88d28 LP |
1084 | char *path_kill_slashes(char *path) { |
1085 | char *f, *t; | |
1086 | bool slash = false; | |
1087 | ||
1088 | /* Removes redundant inner and trailing slashes. Modifies the | |
1089 | * passed string in-place. | |
1090 | * | |
1091 | * ///foo///bar/ becomes /foo/bar | |
1092 | */ | |
1093 | ||
1094 | for (f = path, t = path; *f; f++) { | |
1095 | ||
1096 | if (*f == '/') { | |
1097 | slash = true; | |
1098 | continue; | |
1099 | } | |
1100 | ||
1101 | if (slash) { | |
1102 | slash = false; | |
1103 | *(t++) = '/'; | |
1104 | } | |
1105 | ||
1106 | *(t++) = *f; | |
1107 | } | |
1108 | ||
1109 | /* Special rule, if we are talking of the root directory, a | |
1110 | trailing slash is good */ | |
1111 | ||
1112 | if (t == path && slash) | |
1113 | *(t++) = '/'; | |
1114 | ||
1115 | *t = 0; | |
1116 | return path; | |
1117 | } | |
1118 | ||
1119 | bool path_startswith(const char *path, const char *prefix) { | |
1120 | assert(path); | |
1121 | assert(prefix); | |
1122 | ||
1123 | if ((path[0] == '/') != (prefix[0] == '/')) | |
1124 | return false; | |
1125 | ||
1126 | for (;;) { | |
1127 | size_t a, b; | |
1128 | ||
1129 | path += strspn(path, "/"); | |
1130 | prefix += strspn(prefix, "/"); | |
1131 | ||
1132 | if (*prefix == 0) | |
1133 | return true; | |
1134 | ||
1135 | if (*path == 0) | |
1136 | return false; | |
1137 | ||
1138 | a = strcspn(path, "/"); | |
1139 | b = strcspn(prefix, "/"); | |
1140 | ||
1141 | if (a != b) | |
1142 | return false; | |
1143 | ||
1144 | if (memcmp(path, prefix, a) != 0) | |
1145 | return false; | |
1146 | ||
1147 | path += a; | |
1148 | prefix += b; | |
1149 | } | |
1150 | } | |
1151 | ||
15ae422b LP |
1152 | bool path_equal(const char *a, const char *b) { |
1153 | assert(a); | |
1154 | assert(b); | |
1155 | ||
1156 | if ((a[0] == '/') != (b[0] == '/')) | |
1157 | return false; | |
1158 | ||
1159 | for (;;) { | |
1160 | size_t j, k; | |
1161 | ||
1162 | a += strspn(a, "/"); | |
1163 | b += strspn(b, "/"); | |
1164 | ||
1165 | if (*a == 0 && *b == 0) | |
1166 | return true; | |
1167 | ||
1168 | if (*a == 0 || *b == 0) | |
1169 | return false; | |
1170 | ||
1171 | j = strcspn(a, "/"); | |
1172 | k = strcspn(b, "/"); | |
1173 | ||
1174 | if (j != k) | |
1175 | return false; | |
1176 | ||
1177 | if (memcmp(a, b, j) != 0) | |
1178 | return false; | |
1179 | ||
1180 | a += j; | |
1181 | b += k; | |
1182 | } | |
1183 | } | |
1184 | ||
67d51650 | 1185 | char *ascii_strlower(char *t) { |
4fe88d28 LP |
1186 | char *p; |
1187 | ||
67d51650 | 1188 | assert(t); |
4fe88d28 | 1189 | |
67d51650 | 1190 | for (p = t; *p; p++) |
4fe88d28 LP |
1191 | if (*p >= 'A' && *p <= 'Z') |
1192 | *p = *p - 'A' + 'a'; | |
1193 | ||
67d51650 | 1194 | return t; |
4fe88d28 | 1195 | } |
1dccbe19 | 1196 | |
c85dc17b LP |
1197 | bool ignore_file(const char *filename) { |
1198 | assert(filename); | |
1199 | ||
1200 | return | |
1201 | filename[0] == '.' || | |
6c78be3c | 1202 | streq(filename, "lost+found") || |
c85dc17b LP |
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"); | |
1210 | } | |
1211 | ||
3a0ecb08 LP |
1212 | int fd_nonblock(int fd, bool nonblock) { |
1213 | int flags; | |
1214 | ||
1215 | assert(fd >= 0); | |
1216 | ||
1217 | if ((flags = fcntl(fd, F_GETFL, 0)) < 0) | |
1218 | return -errno; | |
1219 | ||
1220 | if (nonblock) | |
1221 | flags |= O_NONBLOCK; | |
1222 | else | |
1223 | flags &= ~O_NONBLOCK; | |
1224 | ||
1225 | if (fcntl(fd, F_SETFL, flags) < 0) | |
1226 | return -errno; | |
1227 | ||
1228 | return 0; | |
1229 | } | |
1230 | ||
1231 | int fd_cloexec(int fd, bool cloexec) { | |
1232 | int flags; | |
1233 | ||
1234 | assert(fd >= 0); | |
1235 | ||
1236 | if ((flags = fcntl(fd, F_GETFD, 0)) < 0) | |
1237 | return -errno; | |
1238 | ||
1239 | if (cloexec) | |
1240 | flags |= FD_CLOEXEC; | |
1241 | else | |
1242 | flags &= ~FD_CLOEXEC; | |
1243 | ||
1244 | if (fcntl(fd, F_SETFD, flags) < 0) | |
1245 | return -errno; | |
1246 | ||
1247 | return 0; | |
1248 | } | |
1249 | ||
a0d40ac5 LP |
1250 | int close_all_fds(const int except[], unsigned n_except) { |
1251 | DIR *d; | |
1252 | struct dirent *de; | |
1253 | int r = 0; | |
1254 | ||
1255 | if (!(d = opendir("/proc/self/fd"))) | |
1256 | return -errno; | |
1257 | ||
1258 | while ((de = readdir(d))) { | |
a7610064 | 1259 | int fd = -1; |
a0d40ac5 | 1260 | |
a16e1123 | 1261 | if (ignore_file(de->d_name)) |
a0d40ac5 LP |
1262 | continue; |
1263 | ||
1264 | if ((r = safe_atoi(de->d_name, &fd)) < 0) | |
1265 | goto finish; | |
1266 | ||
1267 | if (fd < 3) | |
1268 | continue; | |
1269 | ||
1270 | if (fd == dirfd(d)) | |
1271 | continue; | |
1272 | ||
1273 | if (except) { | |
1274 | bool found; | |
1275 | unsigned i; | |
1276 | ||
1277 | found = false; | |
1278 | for (i = 0; i < n_except; i++) | |
1279 | if (except[i] == fd) { | |
1280 | found = true; | |
1281 | break; | |
1282 | } | |
1283 | ||
1284 | if (found) | |
1285 | continue; | |
1286 | } | |
1287 | ||
2f357920 LP |
1288 | if ((r = close_nointr(fd)) < 0) { |
1289 | /* Valgrind has its own FD and doesn't want to have it closed */ | |
1290 | if (errno != EBADF) | |
1291 | goto finish; | |
1292 | } | |
a0d40ac5 LP |
1293 | } |
1294 | ||
2f357920 LP |
1295 | r = 0; |
1296 | ||
a0d40ac5 LP |
1297 | finish: |
1298 | closedir(d); | |
1299 | return r; | |
1300 | } | |
1301 | ||
db12775d LP |
1302 | bool chars_intersect(const char *a, const char *b) { |
1303 | const char *p; | |
1304 | ||
1305 | /* Returns true if any of the chars in a are in b. */ | |
1306 | for (p = a; *p; p++) | |
1307 | if (strchr(b, *p)) | |
1308 | return true; | |
1309 | ||
1310 | return false; | |
1311 | } | |
1312 | ||
8b6c7120 LP |
1313 | char *format_timestamp(char *buf, size_t l, usec_t t) { |
1314 | struct tm tm; | |
1315 | time_t sec; | |
1316 | ||
1317 | assert(buf); | |
1318 | assert(l > 0); | |
1319 | ||
1320 | if (t <= 0) | |
1321 | return NULL; | |
1322 | ||
1323 | sec = (time_t) t / USEC_PER_SEC; | |
1324 | ||
1325 | if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0) | |
1326 | return NULL; | |
1327 | ||
1328 | return buf; | |
1329 | } | |
1330 | ||
42856c10 LP |
1331 | bool fstype_is_network(const char *fstype) { |
1332 | static const char * const table[] = { | |
1333 | "cifs", | |
1334 | "smbfs", | |
1335 | "ncpfs", | |
1336 | "nfs", | |
1337 | "nfs4" | |
1338 | }; | |
1339 | ||
1340 | unsigned i; | |
1341 | ||
1342 | for (i = 0; i < ELEMENTSOF(table); i++) | |
1343 | if (streq(table[i], fstype)) | |
1344 | return true; | |
1345 | ||
1346 | return false; | |
1347 | } | |
1348 | ||
601f6a1e LP |
1349 | int chvt(int vt) { |
1350 | int fd, r = 0; | |
1351 | ||
1352 | if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0) | |
1353 | return -errno; | |
1354 | ||
1355 | if (vt < 0) { | |
1356 | int tiocl[2] = { | |
1357 | TIOCL_GETKMSGREDIRECT, | |
1358 | 0 | |
1359 | }; | |
1360 | ||
1361 | if (ioctl(fd, TIOCLINUX, tiocl) < 0) | |
1362 | return -errno; | |
1363 | ||
1364 | vt = tiocl[0] <= 0 ? 1 : tiocl[0]; | |
1365 | } | |
1366 | ||
1367 | if (ioctl(fd, VT_ACTIVATE, vt) < 0) | |
1368 | r = -errno; | |
1369 | ||
a16e1123 | 1370 | close_nointr_nofail(r); |
601f6a1e LP |
1371 | return r; |
1372 | } | |
1373 | ||
80876c20 LP |
1374 | int read_one_char(FILE *f, char *ret, bool *need_nl) { |
1375 | struct termios old_termios, new_termios; | |
1376 | char c; | |
1377 | char line[1024]; | |
1378 | ||
1379 | assert(f); | |
1380 | assert(ret); | |
1381 | ||
1382 | if (tcgetattr(fileno(f), &old_termios) >= 0) { | |
1383 | new_termios = old_termios; | |
1384 | ||
1385 | new_termios.c_lflag &= ~ICANON; | |
1386 | new_termios.c_cc[VMIN] = 1; | |
1387 | new_termios.c_cc[VTIME] = 0; | |
1388 | ||
1389 | if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) { | |
1390 | size_t k; | |
1391 | ||
1392 | k = fread(&c, 1, 1, f); | |
1393 | ||
1394 | tcsetattr(fileno(f), TCSADRAIN, &old_termios); | |
1395 | ||
1396 | if (k <= 0) | |
1397 | return -EIO; | |
1398 | ||
1399 | if (need_nl) | |
1400 | *need_nl = c != '\n'; | |
1401 | ||
1402 | *ret = c; | |
1403 | return 0; | |
1404 | } | |
1405 | } | |
1406 | ||
1407 | if (!(fgets(line, sizeof(line), f))) | |
1408 | return -EIO; | |
1409 | ||
1410 | truncate_nl(line); | |
1411 | ||
1412 | if (strlen(line) != 1) | |
1413 | return -EBADMSG; | |
1414 | ||
1415 | if (need_nl) | |
1416 | *need_nl = false; | |
1417 | ||
1418 | *ret = line[0]; | |
1419 | return 0; | |
1420 | } | |
1421 | ||
1422 | int ask(char *ret, const char *replies, const char *text, ...) { | |
1423 | assert(ret); | |
1424 | assert(replies); | |
1425 | assert(text); | |
1426 | ||
1427 | for (;;) { | |
1428 | va_list ap; | |
1429 | char c; | |
1430 | int r; | |
1431 | bool need_nl = true; | |
1432 | ||
b1b2dc0c LP |
1433 | fputs("\x1B[1m", stdout); |
1434 | ||
80876c20 LP |
1435 | va_start(ap, text); |
1436 | vprintf(text, ap); | |
1437 | va_end(ap); | |
1438 | ||
b1b2dc0c LP |
1439 | fputs("\x1B[0m", stdout); |
1440 | ||
80876c20 LP |
1441 | fflush(stdout); |
1442 | ||
1443 | if ((r = read_one_char(stdin, &c, &need_nl)) < 0) { | |
1444 | ||
1445 | if (r == -EBADMSG) { | |
1446 | puts("Bad input, please try again."); | |
1447 | continue; | |
1448 | } | |
1449 | ||
1450 | putchar('\n'); | |
1451 | return r; | |
1452 | } | |
1453 | ||
1454 | if (need_nl) | |
1455 | putchar('\n'); | |
1456 | ||
1457 | if (strchr(replies, c)) { | |
1458 | *ret = c; | |
1459 | return 0; | |
1460 | } | |
1461 | ||
1462 | puts("Read unexpected character, please try again."); | |
1463 | } | |
1464 | } | |
1465 | ||
1466 | int reset_terminal(int fd) { | |
1467 | struct termios termios; | |
1468 | int r = 0; | |
1469 | ||
1470 | assert(fd >= 0); | |
1471 | ||
aaf694ca | 1472 | /* Set terminal to some sane defaults */ |
80876c20 LP |
1473 | |
1474 | if (tcgetattr(fd, &termios) < 0) { | |
1475 | r = -errno; | |
1476 | goto finish; | |
1477 | } | |
1478 | ||
aaf694ca LP |
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 */ | |
1482 | ||
1483 | termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC); | |
80876c20 LP |
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; | |
1488 | ||
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 */ | |
aaf694ca LP |
1500 | termios.c_cc[VEOL] = 0; |
1501 | termios.c_cc[VEOL2] = 0; | |
80876c20 LP |
1502 | |
1503 | termios.c_cc[VTIME] = 0; | |
1504 | termios.c_cc[VMIN] = 1; | |
1505 | ||
1506 | if (tcsetattr(fd, TCSANOW, &termios) < 0) | |
1507 | r = -errno; | |
1508 | ||
1509 | finish: | |
1510 | /* Just in case, flush all crap out */ | |
1511 | tcflush(fd, TCIOFLUSH); | |
1512 | ||
1513 | return r; | |
1514 | } | |
1515 | ||
1516 | int open_terminal(const char *name, int mode) { | |
1517 | int fd, r; | |
1518 | ||
1519 | if ((fd = open(name, mode)) < 0) | |
1520 | return -errno; | |
1521 | ||
1522 | if ((r = isatty(fd)) < 0) { | |
1523 | close_nointr_nofail(fd); | |
1524 | return -errno; | |
1525 | } | |
1526 | ||
1527 | if (!r) { | |
1528 | close_nointr_nofail(fd); | |
1529 | return -ENOTTY; | |
1530 | } | |
1531 | ||
1532 | return fd; | |
1533 | } | |
1534 | ||
1535 | int flush_fd(int fd) { | |
1536 | struct pollfd pollfd; | |
1537 | ||
1538 | zero(pollfd); | |
1539 | pollfd.fd = fd; | |
1540 | pollfd.events = POLLIN; | |
1541 | ||
1542 | for (;;) { | |
1543 | char buf[1024]; | |
1544 | ssize_t l; | |
1545 | int r; | |
1546 | ||
1547 | if ((r = poll(&pollfd, 1, 0)) < 0) { | |
1548 | ||
1549 | if (errno == EINTR) | |
1550 | continue; | |
1551 | ||
1552 | return -errno; | |
1553 | } | |
1554 | ||
1555 | if (r == 0) | |
1556 | return 0; | |
1557 | ||
1558 | if ((l = read(fd, buf, sizeof(buf))) < 0) { | |
1559 | ||
1560 | if (errno == EINTR) | |
1561 | continue; | |
1562 | ||
1563 | if (errno == EAGAIN) | |
1564 | return 0; | |
1565 | ||
1566 | return -errno; | |
1567 | } | |
1568 | ||
1569 | if (l <= 0) | |
1570 | return 0; | |
1571 | } | |
1572 | } | |
1573 | ||
1574 | int acquire_terminal(const char *name, bool fail, bool force) { | |
1575 | int fd = -1, notify = -1, r, wd; | |
1576 | ||
1577 | assert(name); | |
1578 | ||
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. | |
1582 | * | |
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.) */ | |
1591 | ||
1592 | if (!fail && !force) { | |
1593 | if ((notify = inotify_init1(IN_CLOEXEC)) < 0) { | |
1594 | r = -errno; | |
1595 | goto fail; | |
1596 | } | |
1597 | ||
1598 | if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) { | |
1599 | r = -errno; | |
1600 | goto fail; | |
1601 | } | |
1602 | } | |
1603 | ||
1604 | for (;;) { | |
1605 | if ((r = flush_fd(notify)) < 0) | |
1606 | goto fail; | |
1607 | ||
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) | |
1612 | return -errno; | |
1613 | ||
1614 | /* First, try to get the tty */ | |
1615 | if ((r = ioctl(fd, TIOCSCTTY, force)) < 0 && | |
1616 | (force || fail || errno != EPERM)) { | |
1617 | r = -errno; | |
1618 | goto fail; | |
1619 | } | |
1620 | ||
1621 | if (r >= 0) | |
1622 | break; | |
1623 | ||
1624 | assert(!fail); | |
1625 | assert(!force); | |
1626 | assert(notify >= 0); | |
1627 | ||
1628 | for (;;) { | |
1629 | struct inotify_event e; | |
1630 | ssize_t l; | |
1631 | ||
1632 | if ((l = read(notify, &e, sizeof(e))) != sizeof(e)) { | |
1633 | ||
1634 | if (l < 0) { | |
1635 | ||
1636 | if (errno == EINTR) | |
1637 | continue; | |
1638 | ||
1639 | r = -errno; | |
1640 | } else | |
1641 | r = -EIO; | |
1642 | ||
1643 | goto fail; | |
1644 | } | |
1645 | ||
1646 | if (e.wd != wd || !(e.mask & IN_CLOSE)) { | |
1647 | r = -errno; | |
1648 | goto fail; | |
1649 | } | |
1650 | ||
1651 | break; | |
1652 | } | |
1653 | ||
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); | |
1659 | } | |
1660 | ||
1661 | if (notify >= 0) | |
a16e1123 | 1662 | close_nointr_nofail(notify); |
80876c20 LP |
1663 | |
1664 | if ((r = reset_terminal(fd)) < 0) | |
1665 | log_warning("Failed to reset terminal: %s", strerror(-r)); | |
1666 | ||
1667 | return fd; | |
1668 | ||
1669 | fail: | |
1670 | if (fd >= 0) | |
a16e1123 | 1671 | close_nointr_nofail(fd); |
80876c20 LP |
1672 | |
1673 | if (notify >= 0) | |
a16e1123 | 1674 | close_nointr_nofail(notify); |
80876c20 LP |
1675 | |
1676 | return r; | |
1677 | } | |
1678 | ||
1679 | int release_terminal(void) { | |
1680 | int r = 0, fd; | |
57cd2192 | 1681 | struct sigaction sa_old, sa_new; |
80876c20 | 1682 | |
57cd2192 | 1683 | if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0) |
80876c20 LP |
1684 | return -errno; |
1685 | ||
57cd2192 LP |
1686 | /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed |
1687 | * by our own TIOCNOTTY */ | |
1688 | ||
1689 | zero(sa_new); | |
1690 | sa_new.sa_handler = SIG_IGN; | |
1691 | sa_new.sa_flags = SA_RESTART; | |
1692 | assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0); | |
1693 | ||
80876c20 LP |
1694 | if (ioctl(fd, TIOCNOTTY) < 0) |
1695 | r = -errno; | |
1696 | ||
57cd2192 LP |
1697 | assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0); |
1698 | ||
80876c20 LP |
1699 | close_nointr_nofail(fd); |
1700 | return r; | |
1701 | } | |
1702 | ||
a337c6fc LP |
1703 | int ignore_signal(int sig) { |
1704 | struct sigaction sa; | |
1705 | ||
1706 | zero(sa); | |
1707 | sa.sa_handler = SIG_IGN; | |
1708 | sa.sa_flags = SA_RESTART; | |
1709 | ||
1710 | return sigaction(sig, &sa, NULL); | |
1711 | } | |
1712 | ||
8d567588 LP |
1713 | int close_pipe(int p[]) { |
1714 | int a = 0, b = 0; | |
1715 | ||
1716 | assert(p); | |
1717 | ||
1718 | if (p[0] >= 0) { | |
1719 | a = close_nointr(p[0]); | |
1720 | p[0] = -1; | |
1721 | } | |
1722 | ||
1723 | if (p[1] >= 0) { | |
1724 | b = close_nointr(p[1]); | |
1725 | p[1] = -1; | |
1726 | } | |
1727 | ||
1728 | return a < 0 ? a : b; | |
1729 | } | |
1730 | ||
1731 | ssize_t loop_read(int fd, void *buf, size_t nbytes) { | |
1732 | uint8_t *p; | |
1733 | ssize_t n = 0; | |
1734 | ||
1735 | assert(fd >= 0); | |
1736 | assert(buf); | |
1737 | ||
1738 | p = buf; | |
1739 | ||
1740 | while (nbytes > 0) { | |
1741 | ssize_t k; | |
1742 | ||
1743 | if ((k = read(fd, p, nbytes)) <= 0) { | |
1744 | ||
1745 | if (errno == EINTR) | |
1746 | continue; | |
1747 | ||
1748 | if (errno == EAGAIN) { | |
1749 | struct pollfd pollfd; | |
1750 | ||
1751 | zero(pollfd); | |
1752 | pollfd.fd = fd; | |
1753 | pollfd.events = POLLIN; | |
1754 | ||
1755 | if (poll(&pollfd, 1, -1) < 0) { | |
1756 | if (errno == EINTR) | |
1757 | continue; | |
1758 | ||
1759 | return n > 0 ? n : -errno; | |
1760 | } | |
1761 | ||
1762 | if (pollfd.revents != POLLIN) | |
1763 | return n > 0 ? n : -EIO; | |
1764 | ||
1765 | continue; | |
1766 | } | |
1767 | ||
1768 | return n > 0 ? n : (k < 0 ? -errno : 0); | |
1769 | } | |
1770 | ||
1771 | p += k; | |
1772 | nbytes -= k; | |
1773 | n += k; | |
1774 | } | |
1775 | ||
1776 | return n; | |
1777 | } | |
1778 | ||
1779 | int path_is_mount_point(const char *t) { | |
1780 | struct stat a, b; | |
1781 | char *copy; | |
1782 | ||
1783 | if (lstat(t, &a) < 0) { | |
1784 | ||
1785 | if (errno == ENOENT) | |
1786 | return 0; | |
1787 | ||
1788 | return -errno; | |
1789 | } | |
1790 | ||
1791 | if (!(copy = strdup(t))) | |
1792 | return -ENOMEM; | |
1793 | ||
1794 | if (lstat(dirname(copy), &b) < 0) { | |
1795 | free(copy); | |
1796 | return -errno; | |
1797 | } | |
1798 | ||
1799 | free(copy); | |
1800 | ||
1801 | return a.st_dev != b.st_dev; | |
1802 | } | |
1803 | ||
24a6e4a4 LP |
1804 | int parse_usec(const char *t, usec_t *usec) { |
1805 | static const struct { | |
1806 | const char *suffix; | |
1807 | usec_t usec; | |
1808 | } table[] = { | |
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 }, | |
1819 | { "usec", 1ULL }, | |
1820 | { "us", 1ULL }, | |
1821 | { "", USEC_PER_SEC }, | |
1822 | }; | |
1823 | ||
1824 | const char *p; | |
1825 | usec_t r = 0; | |
1826 | ||
1827 | assert(t); | |
1828 | assert(usec); | |
1829 | ||
1830 | p = t; | |
1831 | do { | |
1832 | long long l; | |
1833 | char *e; | |
1834 | unsigned i; | |
1835 | ||
1836 | errno = 0; | |
1837 | l = strtoll(p, &e, 10); | |
1838 | ||
1839 | if (errno != 0) | |
1840 | return -errno; | |
1841 | ||
1842 | if (l < 0) | |
1843 | return -ERANGE; | |
1844 | ||
1845 | if (e == p) | |
1846 | return -EINVAL; | |
1847 | ||
1848 | e += strspn(e, WHITESPACE); | |
1849 | ||
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); | |
1854 | break; | |
1855 | } | |
1856 | ||
1857 | if (i >= ELEMENTSOF(table)) | |
1858 | return -EINVAL; | |
1859 | ||
1860 | } while (*p != 0); | |
1861 | ||
1862 | *usec = r; | |
1863 | ||
1864 | return 0; | |
1865 | } | |
1866 | ||
1dccbe19 LP |
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" | |
1872 | }; | |
1873 | ||
1874 | DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int); | |
1875 | ||
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", | |
1883 | }; | |
1884 | ||
1885 | DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int); | |
1886 | ||
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" | |
1908 | }; | |
1909 | ||
1910 | DEFINE_STRING_TABLE_LOOKUP(log_facility, int); | |
1911 | ||
1912 | static const char *const log_level_table[] = { | |
1913 | [LOG_EMERG] = "emerg", | |
1914 | [LOG_ALERT] = "alert", | |
1915 | [LOG_CRIT] = "crit", | |
1916 | [LOG_ERR] = "err", | |
1917 | [LOG_WARNING] = "warning", | |
1918 | [LOG_NOTICE] = "notice", | |
1919 | [LOG_INFO] = "info", | |
1920 | [LOG_DEBUG] = "debug" | |
1921 | }; | |
1922 | ||
1923 | DEFINE_STRING_TABLE_LOOKUP(log_level, int); | |
1924 | ||
1925 | static const char* const sched_policy_table[] = { | |
1926 | [SCHED_OTHER] = "other", | |
1927 | [SCHED_BATCH] = "batch", | |
1928 | [SCHED_IDLE] = "idle", | |
1929 | [SCHED_FIFO] = "fifo", | |
1930 | [SCHED_RR] = "rr" | |
1931 | }; | |
1932 | ||
1933 | DEFINE_STRING_TABLE_LOOKUP(sched_policy, int); | |
1934 | ||
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" | |
1952 | }; | |
1953 | ||
1954 | DEFINE_STRING_TABLE_LOOKUP(rlimit, int); |