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