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