]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
60918275 | 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> |
3177a7fa | 45 | #include <ctype.h> |
5b6319dc | 46 | #include <sys/prctl.h> |
ef2f1067 LP |
47 | #include <sys/utsname.h> |
48 | #include <pwd.h> | |
4fd5948e | 49 | #include <netinet/ip.h> |
3fe5e5d4 | 50 | #include <linux/kd.h> |
afea26ad | 51 | #include <dlfcn.h> |
2e78aa99 | 52 | #include <sys/wait.h> |
ac123445 | 53 | #include <sys/capability.h> |
60918275 LP |
54 | |
55 | #include "macro.h" | |
56 | #include "util.h" | |
1dccbe19 LP |
57 | #include "ioprio.h" |
58 | #include "missing.h" | |
a9f5d454 | 59 | #include "log.h" |
65d2ebdc | 60 | #include "strv.h" |
e51bc1a2 | 61 | #include "label.h" |
d06dacd0 | 62 | #include "exit-status.h" |
83cc030f | 63 | #include "hashmap.h" |
56cf987f | 64 | |
37f85e66 | 65 | size_t page_size(void) { |
66 | static __thread size_t pgsz = 0; | |
67 | long r; | |
68 | ||
69 | if (pgsz) | |
70 | return pgsz; | |
71 | ||
72 | assert_se((r = sysconf(_SC_PAGESIZE)) > 0); | |
73 | ||
74 | pgsz = (size_t) r; | |
75 | ||
76 | return pgsz; | |
77 | } | |
78 | ||
e05797fb LP |
79 | bool streq_ptr(const char *a, const char *b) { |
80 | ||
81 | /* Like streq(), but tries to make sense of NULL pointers */ | |
82 | ||
83 | if (a && b) | |
84 | return streq(a, b); | |
85 | ||
86 | if (!a && !b) | |
87 | return true; | |
88 | ||
89 | return false; | |
90 | } | |
91 | ||
47be870b | 92 | usec_t now(clockid_t clock_id) { |
60918275 LP |
93 | struct timespec ts; |
94 | ||
47be870b | 95 | assert_se(clock_gettime(clock_id, &ts) == 0); |
60918275 LP |
96 | |
97 | return timespec_load(&ts); | |
98 | } | |
99 | ||
63983207 | 100 | dual_timestamp* dual_timestamp_get(dual_timestamp *ts) { |
871d7de4 LP |
101 | assert(ts); |
102 | ||
103 | ts->realtime = now(CLOCK_REALTIME); | |
104 | ts->monotonic = now(CLOCK_MONOTONIC); | |
105 | ||
106 | return ts; | |
107 | } | |
108 | ||
60918275 LP |
109 | usec_t timespec_load(const struct timespec *ts) { |
110 | assert(ts); | |
111 | ||
112 | return | |
113 | (usec_t) ts->tv_sec * USEC_PER_SEC + | |
114 | (usec_t) ts->tv_nsec / NSEC_PER_USEC; | |
115 | } | |
116 | ||
117 | struct timespec *timespec_store(struct timespec *ts, usec_t u) { | |
118 | assert(ts); | |
119 | ||
120 | ts->tv_sec = (time_t) (u / USEC_PER_SEC); | |
121 | ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC); | |
122 | ||
123 | return ts; | |
124 | } | |
125 | ||
126 | usec_t timeval_load(const struct timeval *tv) { | |
127 | assert(tv); | |
128 | ||
129 | return | |
130 | (usec_t) tv->tv_sec * USEC_PER_SEC + | |
131 | (usec_t) tv->tv_usec; | |
132 | } | |
133 | ||
134 | struct timeval *timeval_store(struct timeval *tv, usec_t u) { | |
135 | assert(tv); | |
136 | ||
137 | tv->tv_sec = (time_t) (u / USEC_PER_SEC); | |
138 | tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC); | |
139 | ||
140 | return tv; | |
141 | } | |
142 | ||
143 | bool endswith(const char *s, const char *postfix) { | |
144 | size_t sl, pl; | |
145 | ||
146 | assert(s); | |
147 | assert(postfix); | |
148 | ||
149 | sl = strlen(s); | |
150 | pl = strlen(postfix); | |
151 | ||
d4d0d4db LP |
152 | if (pl == 0) |
153 | return true; | |
154 | ||
60918275 LP |
155 | if (sl < pl) |
156 | return false; | |
157 | ||
158 | return memcmp(s + sl - pl, postfix, pl) == 0; | |
159 | } | |
160 | ||
161 | bool startswith(const char *s, const char *prefix) { | |
162 | size_t sl, pl; | |
163 | ||
164 | assert(s); | |
165 | assert(prefix); | |
166 | ||
167 | sl = strlen(s); | |
168 | pl = strlen(prefix); | |
169 | ||
d4d0d4db LP |
170 | if (pl == 0) |
171 | return true; | |
172 | ||
60918275 LP |
173 | if (sl < pl) |
174 | return false; | |
175 | ||
176 | return memcmp(s, prefix, pl) == 0; | |
177 | } | |
178 | ||
3177a7fa MAP |
179 | bool startswith_no_case(const char *s, const char *prefix) { |
180 | size_t sl, pl; | |
181 | unsigned i; | |
182 | ||
183 | assert(s); | |
184 | assert(prefix); | |
185 | ||
186 | sl = strlen(s); | |
187 | pl = strlen(prefix); | |
188 | ||
189 | if (pl == 0) | |
190 | return true; | |
191 | ||
192 | if (sl < pl) | |
193 | return false; | |
194 | ||
195 | for(i = 0; i < pl; ++i) { | |
196 | if (tolower(s[i]) != tolower(prefix[i])) | |
197 | return false; | |
198 | } | |
199 | ||
200 | return true; | |
201 | } | |
202 | ||
79d6d816 LP |
203 | bool first_word(const char *s, const char *word) { |
204 | size_t sl, wl; | |
205 | ||
206 | assert(s); | |
207 | assert(word); | |
208 | ||
209 | sl = strlen(s); | |
210 | wl = strlen(word); | |
211 | ||
212 | if (sl < wl) | |
213 | return false; | |
214 | ||
d4d0d4db LP |
215 | if (wl == 0) |
216 | return true; | |
217 | ||
79d6d816 LP |
218 | if (memcmp(s, word, wl) != 0) |
219 | return false; | |
220 | ||
d4d0d4db LP |
221 | return s[wl] == 0 || |
222 | strchr(WHITESPACE, s[wl]); | |
79d6d816 LP |
223 | } |
224 | ||
42f4e3c4 | 225 | int close_nointr(int fd) { |
60918275 LP |
226 | assert(fd >= 0); |
227 | ||
228 | for (;;) { | |
229 | int r; | |
230 | ||
231 | if ((r = close(fd)) >= 0) | |
232 | return r; | |
233 | ||
234 | if (errno != EINTR) | |
235 | return r; | |
236 | } | |
237 | } | |
85261803 | 238 | |
85f136b5 | 239 | void close_nointr_nofail(int fd) { |
80876c20 | 240 | int saved_errno = errno; |
85f136b5 LP |
241 | |
242 | /* like close_nointr() but cannot fail, and guarantees errno | |
243 | * is unchanged */ | |
244 | ||
245 | assert_se(close_nointr(fd) == 0); | |
80876c20 LP |
246 | |
247 | errno = saved_errno; | |
85f136b5 LP |
248 | } |
249 | ||
5b6319dc LP |
250 | void close_many(const int fds[], unsigned n_fd) { |
251 | unsigned i; | |
252 | ||
253 | for (i = 0; i < n_fd; i++) | |
254 | close_nointr_nofail(fds[i]); | |
255 | } | |
256 | ||
85261803 LP |
257 | int parse_boolean(const char *v) { |
258 | assert(v); | |
259 | ||
44d8db9e | 260 | if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on")) |
85261803 | 261 | return 1; |
44d8db9e | 262 | else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off")) |
85261803 LP |
263 | return 0; |
264 | ||
265 | return -EINVAL; | |
266 | } | |
267 | ||
3ba686c1 | 268 | int parse_pid(const char *s, pid_t* ret_pid) { |
0b172489 | 269 | unsigned long ul = 0; |
3ba686c1 LP |
270 | pid_t pid; |
271 | int r; | |
272 | ||
273 | assert(s); | |
274 | assert(ret_pid); | |
275 | ||
276 | if ((r = safe_atolu(s, &ul)) < 0) | |
277 | return r; | |
278 | ||
279 | pid = (pid_t) ul; | |
280 | ||
281 | if ((unsigned long) pid != ul) | |
282 | return -ERANGE; | |
283 | ||
284 | if (pid <= 0) | |
285 | return -ERANGE; | |
286 | ||
287 | *ret_pid = pid; | |
288 | return 0; | |
289 | } | |
290 | ||
85261803 LP |
291 | int safe_atou(const char *s, unsigned *ret_u) { |
292 | char *x = NULL; | |
034c6ed7 | 293 | unsigned long l; |
85261803 LP |
294 | |
295 | assert(s); | |
296 | assert(ret_u); | |
297 | ||
298 | errno = 0; | |
299 | l = strtoul(s, &x, 0); | |
300 | ||
301 | if (!x || *x || errno) | |
302 | return errno ? -errno : -EINVAL; | |
303 | ||
034c6ed7 | 304 | if ((unsigned long) (unsigned) l != l) |
85261803 LP |
305 | return -ERANGE; |
306 | ||
307 | *ret_u = (unsigned) l; | |
308 | return 0; | |
309 | } | |
310 | ||
311 | int safe_atoi(const char *s, int *ret_i) { | |
312 | char *x = NULL; | |
034c6ed7 | 313 | long l; |
85261803 LP |
314 | |
315 | assert(s); | |
316 | assert(ret_i); | |
317 | ||
318 | errno = 0; | |
319 | l = strtol(s, &x, 0); | |
320 | ||
321 | if (!x || *x || errno) | |
322 | return errno ? -errno : -EINVAL; | |
323 | ||
034c6ed7 | 324 | if ((long) (int) l != l) |
85261803 LP |
325 | return -ERANGE; |
326 | ||
034c6ed7 LP |
327 | *ret_i = (int) l; |
328 | return 0; | |
329 | } | |
330 | ||
034c6ed7 LP |
331 | int safe_atollu(const char *s, long long unsigned *ret_llu) { |
332 | char *x = NULL; | |
333 | unsigned long long l; | |
334 | ||
335 | assert(s); | |
336 | assert(ret_llu); | |
337 | ||
338 | errno = 0; | |
339 | l = strtoull(s, &x, 0); | |
340 | ||
341 | if (!x || *x || errno) | |
342 | return errno ? -errno : -EINVAL; | |
343 | ||
344 | *ret_llu = l; | |
345 | return 0; | |
346 | } | |
347 | ||
348 | int safe_atolli(const char *s, long long int *ret_lli) { | |
349 | char *x = NULL; | |
350 | long long l; | |
351 | ||
352 | assert(s); | |
353 | assert(ret_lli); | |
354 | ||
355 | errno = 0; | |
356 | l = strtoll(s, &x, 0); | |
357 | ||
358 | if (!x || *x || errno) | |
359 | return errno ? -errno : -EINVAL; | |
360 | ||
361 | *ret_lli = l; | |
85261803 LP |
362 | return 0; |
363 | } | |
a41e8209 | 364 | |
a41e8209 | 365 | /* Split a string into words. */ |
65d2ebdc | 366 | char *split(const char *c, size_t *l, const char *separator, char **state) { |
a41e8209 LP |
367 | char *current; |
368 | ||
369 | current = *state ? *state : (char*) c; | |
370 | ||
371 | if (!*current || *c == 0) | |
372 | return NULL; | |
373 | ||
65d2ebdc LP |
374 | current += strspn(current, separator); |
375 | *l = strcspn(current, separator); | |
82919e3d LP |
376 | *state = current+*l; |
377 | ||
378 | return (char*) current; | |
379 | } | |
380 | ||
034c6ed7 LP |
381 | /* Split a string into words, but consider strings enclosed in '' and |
382 | * "" as words even if they include spaces. */ | |
383 | char *split_quoted(const char *c, size_t *l, char **state) { | |
0bab36f2 LP |
384 | char *current, *e; |
385 | bool escaped = false; | |
034c6ed7 LP |
386 | |
387 | current = *state ? *state : (char*) c; | |
388 | ||
389 | if (!*current || *c == 0) | |
390 | return NULL; | |
391 | ||
392 | current += strspn(current, WHITESPACE); | |
393 | ||
394 | if (*current == '\'') { | |
395 | current ++; | |
034c6ed7 | 396 | |
0bab36f2 LP |
397 | for (e = current; *e; e++) { |
398 | if (escaped) | |
399 | escaped = false; | |
400 | else if (*e == '\\') | |
401 | escaped = true; | |
402 | else if (*e == '\'') | |
403 | break; | |
404 | } | |
405 | ||
406 | *l = e-current; | |
407 | *state = *e == 0 ? e : e+1; | |
034c6ed7 LP |
408 | } else if (*current == '\"') { |
409 | current ++; | |
034c6ed7 | 410 | |
0bab36f2 LP |
411 | for (e = current; *e; e++) { |
412 | if (escaped) | |
413 | escaped = false; | |
414 | else if (*e == '\\') | |
415 | escaped = true; | |
416 | else if (*e == '\"') | |
417 | break; | |
418 | } | |
419 | ||
420 | *l = e-current; | |
421 | *state = *e == 0 ? e : e+1; | |
034c6ed7 | 422 | } else { |
0bab36f2 LP |
423 | for (e = current; *e; e++) { |
424 | if (escaped) | |
425 | escaped = false; | |
426 | else if (*e == '\\') | |
427 | escaped = true; | |
428 | else if (strchr(WHITESPACE, *e)) | |
429 | break; | |
430 | } | |
431 | *l = e-current; | |
432 | *state = e; | |
034c6ed7 LP |
433 | } |
434 | ||
435 | return (char*) current; | |
436 | } | |
437 | ||
65d2ebdc LP |
438 | char **split_path_and_make_absolute(const char *p) { |
439 | char **l; | |
440 | assert(p); | |
441 | ||
442 | if (!(l = strv_split(p, ":"))) | |
443 | return NULL; | |
444 | ||
445 | if (!strv_path_make_absolute_cwd(l)) { | |
446 | strv_free(l); | |
447 | return NULL; | |
448 | } | |
449 | ||
450 | return l; | |
451 | } | |
452 | ||
034c6ed7 LP |
453 | int get_parent_of_pid(pid_t pid, pid_t *_ppid) { |
454 | int r; | |
455 | FILE *f; | |
20c03b7b | 456 | char fn[PATH_MAX], line[LINE_MAX], *p; |
bb00e604 | 457 | long unsigned ppid; |
034c6ed7 | 458 | |
8480e784 | 459 | assert(pid > 0); |
034c6ed7 LP |
460 | assert(_ppid); |
461 | ||
bb00e604 | 462 | assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1)); |
8480e784 | 463 | char_array_0(fn); |
034c6ed7 LP |
464 | |
465 | if (!(f = fopen(fn, "r"))) | |
466 | return -errno; | |
467 | ||
468 | if (!(fgets(line, sizeof(line), f))) { | |
469 | r = -errno; | |
470 | fclose(f); | |
471 | return r; | |
472 | } | |
473 | ||
474 | fclose(f); | |
475 | ||
476 | /* Let's skip the pid and comm fields. The latter is enclosed | |
477 | * in () but does not escape any () in its value, so let's | |
478 | * skip over it manually */ | |
479 | ||
480 | if (!(p = strrchr(line, ')'))) | |
481 | return -EIO; | |
482 | ||
483 | p++; | |
484 | ||
485 | if (sscanf(p, " " | |
486 | "%*c " /* state */ | |
bb00e604 | 487 | "%lu ", /* ppid */ |
034c6ed7 LP |
488 | &ppid) != 1) |
489 | return -EIO; | |
490 | ||
bb00e604 | 491 | if ((long unsigned) (pid_t) ppid != ppid) |
034c6ed7 LP |
492 | return -ERANGE; |
493 | ||
494 | *_ppid = (pid_t) ppid; | |
495 | ||
496 | return 0; | |
497 | } | |
498 | ||
7640a5de LP |
499 | int get_starttime_of_pid(pid_t pid, unsigned long long *st) { |
500 | int r; | |
501 | FILE *f; | |
502 | char fn[PATH_MAX], line[LINE_MAX], *p; | |
503 | ||
504 | assert(pid > 0); | |
505 | assert(st); | |
506 | ||
507 | assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1)); | |
508 | char_array_0(fn); | |
509 | ||
510 | if (!(f = fopen(fn, "r"))) | |
511 | return -errno; | |
512 | ||
513 | if (!(fgets(line, sizeof(line), f))) { | |
514 | r = -errno; | |
515 | fclose(f); | |
516 | return r; | |
517 | } | |
518 | ||
519 | fclose(f); | |
520 | ||
521 | /* Let's skip the pid and comm fields. The latter is enclosed | |
522 | * in () but does not escape any () in its value, so let's | |
523 | * skip over it manually */ | |
524 | ||
525 | if (!(p = strrchr(line, ')'))) | |
526 | return -EIO; | |
527 | ||
528 | p++; | |
529 | ||
530 | if (sscanf(p, " " | |
531 | "%*c " /* state */ | |
532 | "%*d " /* ppid */ | |
533 | "%*d " /* pgrp */ | |
534 | "%*d " /* session */ | |
535 | "%*d " /* tty_nr */ | |
536 | "%*d " /* tpgid */ | |
537 | "%*u " /* flags */ | |
538 | "%*u " /* minflt */ | |
539 | "%*u " /* cminflt */ | |
540 | "%*u " /* majflt */ | |
541 | "%*u " /* cmajflt */ | |
542 | "%*u " /* utime */ | |
543 | "%*u " /* stime */ | |
544 | "%*d " /* cutime */ | |
545 | "%*d " /* cstime */ | |
546 | "%*d " /* priority */ | |
547 | "%*d " /* nice */ | |
548 | "%*d " /* num_threads */ | |
549 | "%*d " /* itrealvalue */ | |
550 | "%llu " /* starttime */, | |
551 | st) != 1) | |
552 | return -EIO; | |
553 | ||
554 | return 0; | |
555 | } | |
556 | ||
034c6ed7 LP |
557 | int write_one_line_file(const char *fn, const char *line) { |
558 | FILE *f; | |
559 | int r; | |
560 | ||
561 | assert(fn); | |
562 | assert(line); | |
563 | ||
564 | if (!(f = fopen(fn, "we"))) | |
565 | return -errno; | |
566 | ||
567 | if (fputs(line, f) < 0) { | |
568 | r = -errno; | |
569 | goto finish; | |
570 | } | |
571 | ||
8e1bd70d LP |
572 | if (!endswith(line, "\n")) |
573 | fputc('\n', f); | |
574 | ||
151b190e LP |
575 | fflush(f); |
576 | ||
577 | if (ferror(f)) { | |
578 | if (errno != 0) | |
579 | r = -errno; | |
580 | else | |
581 | r = -EIO; | |
582 | } else | |
583 | r = 0; | |
584 | ||
034c6ed7 LP |
585 | finish: |
586 | fclose(f); | |
587 | return r; | |
588 | } | |
589 | ||
590 | int read_one_line_file(const char *fn, char **line) { | |
591 | FILE *f; | |
592 | int r; | |
97c4a07d | 593 | char t[LINE_MAX], *c; |
034c6ed7 LP |
594 | |
595 | assert(fn); | |
596 | assert(line); | |
597 | ||
598 | if (!(f = fopen(fn, "re"))) | |
599 | return -errno; | |
600 | ||
601 | if (!(fgets(t, sizeof(t), f))) { | |
602 | r = -errno; | |
603 | goto finish; | |
604 | } | |
605 | ||
606 | if (!(c = strdup(t))) { | |
607 | r = -ENOMEM; | |
608 | goto finish; | |
609 | } | |
610 | ||
6f9a471a LP |
611 | truncate_nl(c); |
612 | ||
034c6ed7 LP |
613 | *line = c; |
614 | r = 0; | |
615 | ||
616 | finish: | |
617 | fclose(f); | |
618 | return r; | |
619 | } | |
44d8db9e | 620 | |
97c4a07d LP |
621 | int read_full_file(const char *fn, char **contents) { |
622 | FILE *f; | |
623 | int r; | |
624 | size_t n, l; | |
625 | char *buf = NULL; | |
626 | struct stat st; | |
627 | ||
628 | if (!(f = fopen(fn, "re"))) | |
629 | return -errno; | |
630 | ||
631 | if (fstat(fileno(f), &st) < 0) { | |
632 | r = -errno; | |
633 | goto finish; | |
634 | } | |
635 | ||
636 | n = st.st_size > 0 ? st.st_size : LINE_MAX; | |
637 | l = 0; | |
638 | ||
639 | for (;;) { | |
640 | char *t; | |
641 | size_t k; | |
642 | ||
643 | if (!(t = realloc(buf, n+1))) { | |
644 | r = -ENOMEM; | |
645 | goto finish; | |
646 | } | |
647 | ||
648 | buf = t; | |
649 | k = fread(buf + l, 1, n - l, f); | |
650 | ||
651 | if (k <= 0) { | |
652 | if (ferror(f)) { | |
653 | r = -errno; | |
654 | goto finish; | |
655 | } | |
656 | ||
657 | break; | |
658 | } | |
659 | ||
660 | l += k; | |
661 | n *= 2; | |
662 | ||
663 | /* Safety check */ | |
664 | if (n > 4*1024*1024) { | |
665 | r = -E2BIG; | |
666 | goto finish; | |
667 | } | |
668 | } | |
669 | ||
670 | if (buf) | |
671 | buf[l] = 0; | |
672 | else if (!(buf = calloc(1, 1))) { | |
673 | r = -errno; | |
674 | goto finish; | |
675 | } | |
676 | ||
677 | *contents = buf; | |
678 | buf = NULL; | |
679 | ||
680 | r = 0; | |
681 | ||
682 | finish: | |
683 | fclose(f); | |
684 | free(buf); | |
685 | ||
686 | return r; | |
687 | } | |
688 | ||
689 | int parse_env_file( | |
690 | const char *fname, | |
c899f8c6 | 691 | const char *separator, ...) { |
97c4a07d | 692 | |
ce8a6aa1 | 693 | int r = 0; |
97c4a07d LP |
694 | char *contents, *p; |
695 | ||
696 | assert(fname); | |
c899f8c6 | 697 | assert(separator); |
97c4a07d LP |
698 | |
699 | if ((r = read_full_file(fname, &contents)) < 0) | |
700 | return r; | |
701 | ||
702 | p = contents; | |
703 | for (;;) { | |
704 | const char *key = NULL; | |
705 | ||
c899f8c6 | 706 | p += strspn(p, separator); |
97c4a07d LP |
707 | p += strspn(p, WHITESPACE); |
708 | ||
709 | if (!*p) | |
710 | break; | |
711 | ||
712 | if (!strchr(COMMENTS, *p)) { | |
713 | va_list ap; | |
714 | char **value; | |
715 | ||
c899f8c6 | 716 | va_start(ap, separator); |
97c4a07d LP |
717 | while ((key = va_arg(ap, char *))) { |
718 | size_t n; | |
719 | char *v; | |
720 | ||
721 | value = va_arg(ap, char **); | |
722 | ||
723 | n = strlen(key); | |
724 | if (strncmp(p, key, n) != 0 || | |
725 | p[n] != '=') | |
726 | continue; | |
727 | ||
728 | p += n + 1; | |
c899f8c6 | 729 | n = strcspn(p, separator); |
97c4a07d LP |
730 | |
731 | if (n >= 2 && | |
e7db37dd LP |
732 | strchr(QUOTES, p[0]) && |
733 | p[n-1] == p[0]) | |
97c4a07d LP |
734 | v = strndup(p+1, n-2); |
735 | else | |
736 | v = strndup(p, n); | |
737 | ||
738 | if (!v) { | |
739 | r = -ENOMEM; | |
740 | va_end(ap); | |
741 | goto fail; | |
742 | } | |
743 | ||
dd36de4d KS |
744 | if (v[0] == '\0') { |
745 | /* return empty value strings as NULL */ | |
746 | free(v); | |
747 | v = NULL; | |
748 | } | |
749 | ||
97c4a07d LP |
750 | free(*value); |
751 | *value = v; | |
752 | ||
753 | p += n; | |
ce8a6aa1 LP |
754 | |
755 | r ++; | |
97c4a07d LP |
756 | break; |
757 | } | |
758 | va_end(ap); | |
759 | } | |
760 | ||
761 | if (!key) | |
c899f8c6 | 762 | p += strcspn(p, separator); |
97c4a07d LP |
763 | } |
764 | ||
97c4a07d LP |
765 | fail: |
766 | free(contents); | |
767 | return r; | |
768 | } | |
769 | ||
8c7be95e LP |
770 | int load_env_file( |
771 | const char *fname, | |
772 | char ***rl) { | |
773 | ||
774 | FILE *f; | |
775 | char **m = 0; | |
776 | int r; | |
777 | ||
778 | assert(fname); | |
779 | assert(rl); | |
780 | ||
781 | if (!(f = fopen(fname, "re"))) | |
782 | return -errno; | |
783 | ||
784 | while (!feof(f)) { | |
785 | char l[LINE_MAX], *p, *u; | |
786 | char **t; | |
787 | ||
788 | if (!fgets(l, sizeof(l), f)) { | |
789 | if (feof(f)) | |
790 | break; | |
791 | ||
792 | r = -errno; | |
793 | goto finish; | |
794 | } | |
795 | ||
796 | p = strstrip(l); | |
797 | ||
798 | if (!*p) | |
799 | continue; | |
800 | ||
801 | if (strchr(COMMENTS, *p)) | |
802 | continue; | |
803 | ||
804 | if (!(u = normalize_env_assignment(p))) { | |
805 | log_error("Out of memory"); | |
806 | r = -ENOMEM; | |
807 | goto finish; | |
808 | } | |
809 | ||
810 | t = strv_append(m, u); | |
811 | free(u); | |
812 | ||
813 | if (!t) { | |
814 | log_error("Out of memory"); | |
815 | r = -ENOMEM; | |
816 | goto finish; | |
817 | } | |
818 | ||
819 | strv_free(m); | |
820 | m = t; | |
821 | } | |
822 | ||
823 | r = 0; | |
824 | ||
825 | *rl = m; | |
826 | m = NULL; | |
827 | ||
828 | finish: | |
829 | if (f) | |
830 | fclose(f); | |
831 | ||
832 | strv_free(m); | |
833 | ||
834 | return r; | |
835 | } | |
836 | ||
7640a5de LP |
837 | int write_env_file(const char *fname, char **l) { |
838 | ||
839 | char **i; | |
840 | FILE *f; | |
841 | int r; | |
842 | ||
843 | f = fopen(fname, "we"); | |
844 | if (!f) | |
845 | return -errno; | |
846 | ||
847 | STRV_FOREACH(i, l) { | |
848 | fputs(*i, f); | |
849 | fputc('\n', f); | |
850 | } | |
851 | ||
852 | fflush(f); | |
853 | ||
854 | r = ferror(f) ? -errno : 0; | |
855 | fclose(f); | |
856 | ||
857 | return r; | |
858 | } | |
859 | ||
7072ced8 LP |
860 | char *truncate_nl(char *s) { |
861 | assert(s); | |
862 | ||
863 | s[strcspn(s, NEWLINE)] = 0; | |
864 | return s; | |
865 | } | |
866 | ||
867 | int get_process_name(pid_t pid, char **name) { | |
868 | char *p; | |
869 | int r; | |
870 | ||
871 | assert(pid >= 1); | |
872 | assert(name); | |
873 | ||
bb00e604 | 874 | if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0) |
7072ced8 LP |
875 | return -ENOMEM; |
876 | ||
877 | r = read_one_line_file(p, name); | |
878 | free(p); | |
879 | ||
880 | if (r < 0) | |
881 | return r; | |
882 | ||
7072ced8 LP |
883 | return 0; |
884 | } | |
885 | ||
c59760ee LP |
886 | int get_process_cmdline(pid_t pid, size_t max_length, char **line) { |
887 | char *p, *r, *k; | |
888 | int c; | |
889 | bool space = false; | |
890 | size_t left; | |
891 | FILE *f; | |
892 | ||
893 | assert(pid >= 1); | |
894 | assert(max_length > 0); | |
895 | assert(line); | |
896 | ||
897 | if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0) | |
898 | return -ENOMEM; | |
899 | ||
900 | f = fopen(p, "r"); | |
901 | free(p); | |
902 | ||
903 | if (!f) | |
904 | return -errno; | |
905 | ||
906 | if (!(r = new(char, max_length))) { | |
907 | fclose(f); | |
908 | return -ENOMEM; | |
909 | } | |
910 | ||
911 | k = r; | |
912 | left = max_length; | |
913 | while ((c = getc(f)) != EOF) { | |
914 | ||
915 | if (isprint(c)) { | |
916 | if (space) { | |
917 | if (left <= 4) | |
918 | break; | |
919 | ||
920 | *(k++) = ' '; | |
057fbb58 | 921 | left--; |
c59760ee LP |
922 | space = false; |
923 | } | |
924 | ||
925 | if (left <= 4) | |
926 | break; | |
927 | ||
928 | *(k++) = (char) c; | |
057fbb58 | 929 | left--; |
c59760ee LP |
930 | } else |
931 | space = true; | |
932 | } | |
933 | ||
934 | if (left <= 4) { | |
935 | size_t n = MIN(left-1, 3U); | |
936 | memcpy(k, "...", n); | |
937 | k[n] = 0; | |
938 | } else | |
939 | *k = 0; | |
940 | ||
941 | fclose(f); | |
942 | ||
35d2e7ec LP |
943 | /* Kernel threads have no argv[] */ |
944 | if (r[0] == 0) { | |
945 | char *t; | |
946 | int h; | |
947 | ||
948 | free(r); | |
949 | ||
950 | if ((h = get_process_name(pid, &t)) < 0) | |
951 | return h; | |
952 | ||
953 | h = asprintf(&r, "[%s]", t); | |
954 | free(t); | |
955 | ||
956 | if (h < 0) | |
957 | return -ENOMEM; | |
958 | } | |
fa776d8e | 959 | |
c59760ee LP |
960 | *line = r; |
961 | return 0; | |
962 | } | |
963 | ||
fab56fc5 LP |
964 | char *strnappend(const char *s, const char *suffix, size_t b) { |
965 | size_t a; | |
44d8db9e LP |
966 | char *r; |
967 | ||
fab56fc5 LP |
968 | if (!s && !suffix) |
969 | return strdup(""); | |
970 | ||
971 | if (!s) | |
972 | return strndup(suffix, b); | |
973 | ||
974 | if (!suffix) | |
975 | return strdup(s); | |
976 | ||
44d8db9e LP |
977 | assert(s); |
978 | assert(suffix); | |
979 | ||
980 | a = strlen(s); | |
44d8db9e LP |
981 | |
982 | if (!(r = new(char, a+b+1))) | |
983 | return NULL; | |
984 | ||
985 | memcpy(r, s, a); | |
986 | memcpy(r+a, suffix, b); | |
987 | r[a+b] = 0; | |
988 | ||
989 | return r; | |
990 | } | |
87f0e418 | 991 | |
fab56fc5 LP |
992 | char *strappend(const char *s, const char *suffix) { |
993 | return strnappend(s, suffix, suffix ? strlen(suffix) : 0); | |
994 | } | |
995 | ||
87f0e418 LP |
996 | int readlink_malloc(const char *p, char **r) { |
997 | size_t l = 100; | |
998 | ||
999 | assert(p); | |
1000 | assert(r); | |
1001 | ||
1002 | for (;;) { | |
1003 | char *c; | |
1004 | ssize_t n; | |
1005 | ||
1006 | if (!(c = new(char, l))) | |
1007 | return -ENOMEM; | |
1008 | ||
1009 | if ((n = readlink(p, c, l-1)) < 0) { | |
1010 | int ret = -errno; | |
1011 | free(c); | |
1012 | return ret; | |
1013 | } | |
1014 | ||
1015 | if ((size_t) n < l-1) { | |
1016 | c[n] = 0; | |
1017 | *r = c; | |
1018 | return 0; | |
1019 | } | |
1020 | ||
1021 | free(c); | |
1022 | l *= 2; | |
1023 | } | |
1024 | } | |
1025 | ||
2c7108c4 LP |
1026 | int readlink_and_make_absolute(const char *p, char **r) { |
1027 | char *target, *k; | |
1028 | int j; | |
1029 | ||
1030 | assert(p); | |
1031 | assert(r); | |
1032 | ||
1033 | if ((j = readlink_malloc(p, &target)) < 0) | |
1034 | return j; | |
1035 | ||
1036 | k = file_in_same_dir(p, target); | |
1037 | free(target); | |
1038 | ||
1039 | if (!k) | |
1040 | return -ENOMEM; | |
1041 | ||
1042 | *r = k; | |
1043 | return 0; | |
1044 | } | |
1045 | ||
35d2e7ec LP |
1046 | int parent_of_path(const char *path, char **_r) { |
1047 | const char *e, *a = NULL, *b = NULL, *p; | |
1048 | char *r; | |
1049 | bool slash = false; | |
1050 | ||
1051 | assert(path); | |
1052 | assert(_r); | |
1053 | ||
1054 | if (!*path) | |
1055 | return -EINVAL; | |
1056 | ||
1057 | for (e = path; *e; e++) { | |
1058 | ||
1059 | if (!slash && *e == '/') { | |
1060 | a = b; | |
1061 | b = e; | |
1062 | slash = true; | |
1063 | } else if (slash && *e != '/') | |
1064 | slash = false; | |
1065 | } | |
1066 | ||
1067 | if (*(e-1) == '/') | |
1068 | p = a; | |
1069 | else | |
1070 | p = b; | |
1071 | ||
1072 | if (!p) | |
1073 | return -EINVAL; | |
1074 | ||
1075 | if (p == path) | |
1076 | r = strdup("/"); | |
1077 | else | |
1078 | r = strndup(path, p-path); | |
1079 | ||
1080 | if (!r) | |
1081 | return -ENOMEM; | |
1082 | ||
1083 | *_r = r; | |
1084 | return 0; | |
1085 | } | |
1086 | ||
1087 | ||
87f0e418 LP |
1088 | char *file_name_from_path(const char *p) { |
1089 | char *r; | |
1090 | ||
1091 | assert(p); | |
1092 | ||
1093 | if ((r = strrchr(p, '/'))) | |
1094 | return r + 1; | |
1095 | ||
1096 | return (char*) p; | |
1097 | } | |
0301abf4 LP |
1098 | |
1099 | bool path_is_absolute(const char *p) { | |
1100 | assert(p); | |
1101 | ||
1102 | return p[0] == '/'; | |
1103 | } | |
1104 | ||
1105 | bool is_path(const char *p) { | |
1106 | ||
1107 | return !!strchr(p, '/'); | |
1108 | } | |
1109 | ||
1110 | char *path_make_absolute(const char *p, const char *prefix) { | |
1111 | char *r; | |
1112 | ||
1113 | assert(p); | |
1114 | ||
65d2ebdc LP |
1115 | /* Makes every item in the list an absolute path by prepending |
1116 | * the prefix, if specified and necessary */ | |
1117 | ||
0301abf4 LP |
1118 | if (path_is_absolute(p) || !prefix) |
1119 | return strdup(p); | |
1120 | ||
1121 | if (asprintf(&r, "%s/%s", prefix, p) < 0) | |
1122 | return NULL; | |
1123 | ||
1124 | return r; | |
1125 | } | |
2a987ee8 | 1126 | |
65d2ebdc LP |
1127 | char *path_make_absolute_cwd(const char *p) { |
1128 | char *cwd, *r; | |
1129 | ||
1130 | assert(p); | |
1131 | ||
1132 | /* Similar to path_make_absolute(), but prefixes with the | |
1133 | * current working directory. */ | |
1134 | ||
1135 | if (path_is_absolute(p)) | |
1136 | return strdup(p); | |
1137 | ||
1138 | if (!(cwd = get_current_dir_name())) | |
1139 | return NULL; | |
1140 | ||
1141 | r = path_make_absolute(p, cwd); | |
1142 | free(cwd); | |
1143 | ||
1144 | return r; | |
1145 | } | |
1146 | ||
1147 | char **strv_path_make_absolute_cwd(char **l) { | |
1148 | char **s; | |
1149 | ||
1150 | /* Goes through every item in the string list and makes it | |
1151 | * absolute. This works in place and won't rollback any | |
1152 | * changes on failure. */ | |
1153 | ||
1154 | STRV_FOREACH(s, l) { | |
1155 | char *t; | |
1156 | ||
1157 | if (!(t = path_make_absolute_cwd(*s))) | |
1158 | return NULL; | |
1159 | ||
1160 | free(*s); | |
1161 | *s = t; | |
1162 | } | |
1163 | ||
1164 | return l; | |
1165 | } | |
1166 | ||
c3f6d675 LP |
1167 | char **strv_path_canonicalize(char **l) { |
1168 | char **s; | |
1169 | unsigned k = 0; | |
1170 | bool enomem = false; | |
1171 | ||
1172 | if (strv_isempty(l)) | |
1173 | return l; | |
1174 | ||
1175 | /* Goes through every item in the string list and canonicalize | |
1176 | * the path. This works in place and won't rollback any | |
1177 | * changes on failure. */ | |
1178 | ||
1179 | STRV_FOREACH(s, l) { | |
1180 | char *t, *u; | |
1181 | ||
1182 | t = path_make_absolute_cwd(*s); | |
1183 | free(*s); | |
1184 | ||
1185 | if (!t) { | |
1186 | enomem = true; | |
1187 | continue; | |
1188 | } | |
1189 | ||
1190 | errno = 0; | |
1191 | u = canonicalize_file_name(t); | |
1192 | free(t); | |
1193 | ||
1194 | if (!u) { | |
1195 | if (errno == ENOMEM || !errno) | |
1196 | enomem = true; | |
1197 | ||
1198 | continue; | |
1199 | } | |
1200 | ||
1201 | l[k++] = u; | |
1202 | } | |
1203 | ||
1204 | l[k] = NULL; | |
1205 | ||
1206 | if (enomem) | |
1207 | return NULL; | |
1208 | ||
1209 | return l; | |
a9dd2082 LP |
1210 | } |
1211 | ||
1212 | char **strv_path_remove_empty(char **l) { | |
1213 | char **f, **t; | |
1214 | ||
1215 | if (!l) | |
1216 | return NULL; | |
1217 | ||
1218 | for (f = t = l; *f; f++) { | |
1219 | ||
1220 | if (dir_is_empty(*f) > 0) { | |
1221 | free(*f); | |
1222 | continue; | |
1223 | } | |
1224 | ||
1225 | *(t++) = *f; | |
1226 | } | |
1227 | ||
1228 | *t = NULL; | |
1229 | return l; | |
c3f6d675 LP |
1230 | } |
1231 | ||
2a987ee8 LP |
1232 | int reset_all_signal_handlers(void) { |
1233 | int sig; | |
1234 | ||
1235 | for (sig = 1; sig < _NSIG; sig++) { | |
1236 | struct sigaction sa; | |
1237 | ||
1238 | if (sig == SIGKILL || sig == SIGSTOP) | |
1239 | continue; | |
1240 | ||
1241 | zero(sa); | |
1242 | sa.sa_handler = SIG_DFL; | |
431c32bf | 1243 | sa.sa_flags = SA_RESTART; |
2a987ee8 LP |
1244 | |
1245 | /* On Linux the first two RT signals are reserved by | |
1246 | * glibc, and sigaction() will return EINVAL for them. */ | |
1247 | if ((sigaction(sig, &sa, NULL) < 0)) | |
1248 | if (errno != EINVAL) | |
1249 | return -errno; | |
1250 | } | |
1251 | ||
8e274523 | 1252 | return 0; |
2a987ee8 | 1253 | } |
4a72ff34 LP |
1254 | |
1255 | char *strstrip(char *s) { | |
1256 | char *e, *l = NULL; | |
1257 | ||
1258 | /* Drops trailing whitespace. Modifies the string in | |
1259 | * place. Returns pointer to first non-space character */ | |
1260 | ||
1261 | s += strspn(s, WHITESPACE); | |
1262 | ||
1263 | for (e = s; *e; e++) | |
1264 | if (!strchr(WHITESPACE, *e)) | |
1265 | l = e; | |
1266 | ||
1267 | if (l) | |
1268 | *(l+1) = 0; | |
1269 | else | |
1270 | *s = 0; | |
1271 | ||
1272 | return s; | |
4a72ff34 LP |
1273 | } |
1274 | ||
ee9b5e01 LP |
1275 | char *delete_chars(char *s, const char *bad) { |
1276 | char *f, *t; | |
1277 | ||
1278 | /* Drops all whitespace, regardless where in the string */ | |
1279 | ||
1280 | for (f = s, t = s; *f; f++) { | |
1281 | if (strchr(bad, *f)) | |
1282 | continue; | |
1283 | ||
1284 | *(t++) = *f; | |
1285 | } | |
1286 | ||
1287 | *t = 0; | |
1288 | ||
1289 | return s; | |
1290 | } | |
1291 | ||
4a72ff34 LP |
1292 | char *file_in_same_dir(const char *path, const char *filename) { |
1293 | char *e, *r; | |
1294 | size_t k; | |
1295 | ||
1296 | assert(path); | |
1297 | assert(filename); | |
1298 | ||
1299 | /* This removes the last component of path and appends | |
1300 | * filename, unless the latter is absolute anyway or the | |
1301 | * former isn't */ | |
1302 | ||
1303 | if (path_is_absolute(filename)) | |
1304 | return strdup(filename); | |
1305 | ||
1306 | if (!(e = strrchr(path, '/'))) | |
1307 | return strdup(filename); | |
1308 | ||
1309 | k = strlen(filename); | |
1310 | if (!(r = new(char, e-path+1+k+1))) | |
1311 | return NULL; | |
1312 | ||
1313 | memcpy(r, path, e-path+1); | |
1314 | memcpy(r+(e-path)+1, filename, k+1); | |
1315 | ||
1316 | return r; | |
1317 | } | |
fb624d04 | 1318 | |
8c6db833 LP |
1319 | int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) { |
1320 | struct stat st; | |
1321 | ||
56cf987f | 1322 | if (label_mkdir(path, mode) >= 0) |
8c6db833 LP |
1323 | if (chmod_and_chown(path, mode, uid, gid) < 0) |
1324 | return -errno; | |
1325 | ||
1326 | if (lstat(path, &st) < 0) | |
1327 | return -errno; | |
1328 | ||
1329 | if ((st.st_mode & 0777) != mode || | |
1330 | st.st_uid != uid || | |
1331 | st.st_gid != gid || | |
1332 | !S_ISDIR(st.st_mode)) { | |
1333 | errno = EEXIST; | |
1334 | return -errno; | |
1335 | } | |
1336 | ||
1337 | return 0; | |
1338 | } | |
1339 | ||
1340 | ||
a9f5d454 LP |
1341 | int mkdir_parents(const char *path, mode_t mode) { |
1342 | const char *p, *e; | |
1343 | ||
1344 | assert(path); | |
1345 | ||
1346 | /* Creates every parent directory in the path except the last | |
1347 | * component. */ | |
1348 | ||
1349 | p = path + strspn(path, "/"); | |
1350 | for (;;) { | |
1351 | int r; | |
1352 | char *t; | |
1353 | ||
1354 | e = p + strcspn(p, "/"); | |
1355 | p = e + strspn(e, "/"); | |
1356 | ||
1357 | /* Is this the last component? If so, then we're | |
1358 | * done */ | |
1359 | if (*p == 0) | |
1360 | return 0; | |
1361 | ||
1362 | if (!(t = strndup(path, e - path))) | |
1363 | return -ENOMEM; | |
1364 | ||
56cf987f | 1365 | r = label_mkdir(t, mode); |
a9f5d454 LP |
1366 | free(t); |
1367 | ||
1368 | if (r < 0 && errno != EEXIST) | |
1369 | return -errno; | |
1370 | } | |
1371 | } | |
1372 | ||
bbd67135 LP |
1373 | int mkdir_p(const char *path, mode_t mode) { |
1374 | int r; | |
1375 | ||
1376 | /* Like mkdir -p */ | |
1377 | ||
1378 | if ((r = mkdir_parents(path, mode)) < 0) | |
1379 | return r; | |
1380 | ||
56cf987f | 1381 | if (label_mkdir(path, mode) < 0 && errno != EEXIST) |
bbd67135 LP |
1382 | return -errno; |
1383 | ||
1384 | return 0; | |
1385 | } | |
1386 | ||
c32dd69b LP |
1387 | int rmdir_parents(const char *path, const char *stop) { |
1388 | size_t l; | |
1389 | int r = 0; | |
1390 | ||
1391 | assert(path); | |
1392 | assert(stop); | |
1393 | ||
1394 | l = strlen(path); | |
1395 | ||
1396 | /* Skip trailing slashes */ | |
1397 | while (l > 0 && path[l-1] == '/') | |
1398 | l--; | |
1399 | ||
1400 | while (l > 0) { | |
1401 | char *t; | |
1402 | ||
1403 | /* Skip last component */ | |
1404 | while (l > 0 && path[l-1] != '/') | |
1405 | l--; | |
1406 | ||
1407 | /* Skip trailing slashes */ | |
1408 | while (l > 0 && path[l-1] == '/') | |
1409 | l--; | |
1410 | ||
1411 | if (l <= 0) | |
1412 | break; | |
1413 | ||
1414 | if (!(t = strndup(path, l))) | |
1415 | return -ENOMEM; | |
1416 | ||
1417 | if (path_startswith(stop, t)) { | |
1418 | free(t); | |
1419 | return 0; | |
1420 | } | |
1421 | ||
1422 | r = rmdir(t); | |
1423 | free(t); | |
1424 | ||
1425 | if (r < 0) | |
1426 | if (errno != ENOENT) | |
1427 | return -errno; | |
1428 | } | |
1429 | ||
1430 | return 0; | |
1431 | } | |
1432 | ||
1433 | ||
fb624d04 LP |
1434 | char hexchar(int x) { |
1435 | static const char table[16] = "0123456789abcdef"; | |
1436 | ||
1437 | return table[x & 15]; | |
1438 | } | |
4fe88d28 LP |
1439 | |
1440 | int unhexchar(char c) { | |
1441 | ||
1442 | if (c >= '0' && c <= '9') | |
1443 | return c - '0'; | |
1444 | ||
1445 | if (c >= 'a' && c <= 'f') | |
ea430986 | 1446 | return c - 'a' + 10; |
4fe88d28 LP |
1447 | |
1448 | if (c >= 'A' && c <= 'F') | |
ea430986 | 1449 | return c - 'A' + 10; |
4fe88d28 LP |
1450 | |
1451 | return -1; | |
1452 | } | |
1453 | ||
1454 | char octchar(int x) { | |
1455 | return '0' + (x & 7); | |
1456 | } | |
1457 | ||
1458 | int unoctchar(char c) { | |
1459 | ||
1460 | if (c >= '0' && c <= '7') | |
1461 | return c - '0'; | |
1462 | ||
1463 | return -1; | |
1464 | } | |
1465 | ||
5af98f82 LP |
1466 | char decchar(int x) { |
1467 | return '0' + (x % 10); | |
1468 | } | |
1469 | ||
1470 | int undecchar(char c) { | |
1471 | ||
1472 | if (c >= '0' && c <= '9') | |
1473 | return c - '0'; | |
1474 | ||
1475 | return -1; | |
1476 | } | |
1477 | ||
4fe88d28 LP |
1478 | char *cescape(const char *s) { |
1479 | char *r, *t; | |
1480 | const char *f; | |
1481 | ||
1482 | assert(s); | |
1483 | ||
1484 | /* Does C style string escaping. */ | |
1485 | ||
1486 | if (!(r = new(char, strlen(s)*4 + 1))) | |
1487 | return NULL; | |
1488 | ||
1489 | for (f = s, t = r; *f; f++) | |
1490 | ||
1491 | switch (*f) { | |
1492 | ||
1493 | case '\a': | |
1494 | *(t++) = '\\'; | |
1495 | *(t++) = 'a'; | |
1496 | break; | |
1497 | case '\b': | |
1498 | *(t++) = '\\'; | |
1499 | *(t++) = 'b'; | |
1500 | break; | |
1501 | case '\f': | |
1502 | *(t++) = '\\'; | |
1503 | *(t++) = 'f'; | |
1504 | break; | |
1505 | case '\n': | |
1506 | *(t++) = '\\'; | |
1507 | *(t++) = 'n'; | |
1508 | break; | |
1509 | case '\r': | |
1510 | *(t++) = '\\'; | |
1511 | *(t++) = 'r'; | |
1512 | break; | |
1513 | case '\t': | |
1514 | *(t++) = '\\'; | |
1515 | *(t++) = 't'; | |
1516 | break; | |
1517 | case '\v': | |
1518 | *(t++) = '\\'; | |
1519 | *(t++) = 'v'; | |
1520 | break; | |
1521 | case '\\': | |
1522 | *(t++) = '\\'; | |
1523 | *(t++) = '\\'; | |
1524 | break; | |
1525 | case '"': | |
1526 | *(t++) = '\\'; | |
1527 | *(t++) = '"'; | |
1528 | break; | |
1529 | case '\'': | |
1530 | *(t++) = '\\'; | |
1531 | *(t++) = '\''; | |
1532 | break; | |
1533 | ||
1534 | default: | |
1535 | /* For special chars we prefer octal over | |
1536 | * hexadecimal encoding, simply because glib's | |
1537 | * g_strescape() does the same */ | |
1538 | if ((*f < ' ') || (*f >= 127)) { | |
1539 | *(t++) = '\\'; | |
1540 | *(t++) = octchar((unsigned char) *f >> 6); | |
1541 | *(t++) = octchar((unsigned char) *f >> 3); | |
1542 | *(t++) = octchar((unsigned char) *f); | |
1543 | } else | |
1544 | *(t++) = *f; | |
1545 | break; | |
1546 | } | |
1547 | ||
1548 | *t = 0; | |
1549 | ||
1550 | return r; | |
1551 | } | |
1552 | ||
6febfd0d | 1553 | char *cunescape_length(const char *s, size_t length) { |
4fe88d28 LP |
1554 | char *r, *t; |
1555 | const char *f; | |
1556 | ||
1557 | assert(s); | |
1558 | ||
1559 | /* Undoes C style string escaping */ | |
1560 | ||
6febfd0d | 1561 | if (!(r = new(char, length+1))) |
4fe88d28 LP |
1562 | return r; |
1563 | ||
6febfd0d | 1564 | for (f = s, t = r; f < s + length; f++) { |
4fe88d28 LP |
1565 | |
1566 | if (*f != '\\') { | |
1567 | *(t++) = *f; | |
1568 | continue; | |
1569 | } | |
1570 | ||
1571 | f++; | |
1572 | ||
1573 | switch (*f) { | |
1574 | ||
1575 | case 'a': | |
1576 | *(t++) = '\a'; | |
1577 | break; | |
1578 | case 'b': | |
1579 | *(t++) = '\b'; | |
1580 | break; | |
1581 | case 'f': | |
1582 | *(t++) = '\f'; | |
1583 | break; | |
1584 | case 'n': | |
1585 | *(t++) = '\n'; | |
1586 | break; | |
1587 | case 'r': | |
1588 | *(t++) = '\r'; | |
1589 | break; | |
1590 | case 't': | |
1591 | *(t++) = '\t'; | |
1592 | break; | |
1593 | case 'v': | |
1594 | *(t++) = '\v'; | |
1595 | break; | |
1596 | case '\\': | |
1597 | *(t++) = '\\'; | |
1598 | break; | |
1599 | case '"': | |
1600 | *(t++) = '"'; | |
1601 | break; | |
1602 | case '\'': | |
1603 | *(t++) = '\''; | |
1604 | break; | |
1605 | ||
e167fb86 LP |
1606 | case 's': |
1607 | /* This is an extension of the XDG syntax files */ | |
1608 | *(t++) = ' '; | |
1609 | break; | |
1610 | ||
4fe88d28 LP |
1611 | case 'x': { |
1612 | /* hexadecimal encoding */ | |
1613 | int a, b; | |
1614 | ||
1615 | if ((a = unhexchar(f[1])) < 0 || | |
1616 | (b = unhexchar(f[2])) < 0) { | |
1617 | /* Invalid escape code, let's take it literal then */ | |
1618 | *(t++) = '\\'; | |
1619 | *(t++) = 'x'; | |
1620 | } else { | |
1621 | *(t++) = (char) ((a << 4) | b); | |
1622 | f += 2; | |
1623 | } | |
1624 | ||
1625 | break; | |
1626 | } | |
1627 | ||
1628 | case '0': | |
1629 | case '1': | |
1630 | case '2': | |
1631 | case '3': | |
1632 | case '4': | |
1633 | case '5': | |
1634 | case '6': | |
1635 | case '7': { | |
1636 | /* octal encoding */ | |
1637 | int a, b, c; | |
1638 | ||
1639 | if ((a = unoctchar(f[0])) < 0 || | |
1640 | (b = unoctchar(f[1])) < 0 || | |
1641 | (c = unoctchar(f[2])) < 0) { | |
1642 | /* Invalid escape code, let's take it literal then */ | |
1643 | *(t++) = '\\'; | |
1644 | *(t++) = f[0]; | |
1645 | } else { | |
1646 | *(t++) = (char) ((a << 6) | (b << 3) | c); | |
1647 | f += 2; | |
1648 | } | |
1649 | ||
1650 | break; | |
1651 | } | |
1652 | ||
1653 | case 0: | |
1654 | /* premature end of string.*/ | |
1655 | *(t++) = '\\'; | |
1656 | goto finish; | |
1657 | ||
1658 | default: | |
1659 | /* Invalid escape code, let's take it literal then */ | |
1660 | *(t++) = '\\'; | |
f3d4cc01 | 1661 | *(t++) = *f; |
4fe88d28 LP |
1662 | break; |
1663 | } | |
1664 | } | |
1665 | ||
1666 | finish: | |
1667 | *t = 0; | |
1668 | return r; | |
1669 | } | |
1670 | ||
6febfd0d LP |
1671 | char *cunescape(const char *s) { |
1672 | return cunescape_length(s, strlen(s)); | |
1673 | } | |
4fe88d28 LP |
1674 | |
1675 | char *xescape(const char *s, const char *bad) { | |
1676 | char *r, *t; | |
1677 | const char *f; | |
1678 | ||
1679 | /* Escapes all chars in bad, in addition to \ and all special | |
1680 | * chars, in \xFF style escaping. May be reversed with | |
1681 | * cunescape. */ | |
1682 | ||
1683 | if (!(r = new(char, strlen(s)*4+1))) | |
1684 | return NULL; | |
1685 | ||
1686 | for (f = s, t = r; *f; f++) { | |
1687 | ||
b866264a LP |
1688 | if ((*f < ' ') || (*f >= 127) || |
1689 | (*f == '\\') || strchr(bad, *f)) { | |
4fe88d28 LP |
1690 | *(t++) = '\\'; |
1691 | *(t++) = 'x'; | |
1692 | *(t++) = hexchar(*f >> 4); | |
1693 | *(t++) = hexchar(*f); | |
1694 | } else | |
1695 | *(t++) = *f; | |
1696 | } | |
1697 | ||
1698 | *t = 0; | |
1699 | ||
1700 | return r; | |
1701 | } | |
1702 | ||
ea430986 | 1703 | char *bus_path_escape(const char *s) { |
ea430986 LP |
1704 | char *r, *t; |
1705 | const char *f; | |
1706 | ||
47be870b LP |
1707 | assert(s); |
1708 | ||
ea430986 LP |
1709 | /* Escapes all chars that D-Bus' object path cannot deal |
1710 | * with. Can be reverse with bus_path_unescape() */ | |
1711 | ||
1712 | if (!(r = new(char, strlen(s)*3+1))) | |
1713 | return NULL; | |
1714 | ||
1715 | for (f = s, t = r; *f; f++) { | |
1716 | ||
1717 | if (!(*f >= 'A' && *f <= 'Z') && | |
1718 | !(*f >= 'a' && *f <= 'z') && | |
1719 | !(*f >= '0' && *f <= '9')) { | |
1720 | *(t++) = '_'; | |
1721 | *(t++) = hexchar(*f >> 4); | |
1722 | *(t++) = hexchar(*f); | |
1723 | } else | |
1724 | *(t++) = *f; | |
1725 | } | |
1726 | ||
1727 | *t = 0; | |
1728 | ||
1729 | return r; | |
1730 | } | |
1731 | ||
9e2f7c11 | 1732 | char *bus_path_unescape(const char *f) { |
ea430986 | 1733 | char *r, *t; |
ea430986 | 1734 | |
9e2f7c11 | 1735 | assert(f); |
47be870b | 1736 | |
9e2f7c11 | 1737 | if (!(r = strdup(f))) |
ea430986 LP |
1738 | return NULL; |
1739 | ||
9e2f7c11 | 1740 | for (t = r; *f; f++) { |
ea430986 LP |
1741 | |
1742 | if (*f == '_') { | |
1743 | int a, b; | |
1744 | ||
1745 | if ((a = unhexchar(f[1])) < 0 || | |
1746 | (b = unhexchar(f[2])) < 0) { | |
1747 | /* Invalid escape code, let's take it literal then */ | |
1748 | *(t++) = '_'; | |
1749 | } else { | |
1750 | *(t++) = (char) ((a << 4) | b); | |
1751 | f += 2; | |
1752 | } | |
1753 | } else | |
1754 | *(t++) = *f; | |
1755 | } | |
1756 | ||
1757 | *t = 0; | |
1758 | ||
1759 | return r; | |
1760 | } | |
1761 | ||
4fe88d28 LP |
1762 | char *path_kill_slashes(char *path) { |
1763 | char *f, *t; | |
1764 | bool slash = false; | |
1765 | ||
1766 | /* Removes redundant inner and trailing slashes. Modifies the | |
1767 | * passed string in-place. | |
1768 | * | |
1769 | * ///foo///bar/ becomes /foo/bar | |
1770 | */ | |
1771 | ||
1772 | for (f = path, t = path; *f; f++) { | |
1773 | ||
1774 | if (*f == '/') { | |
1775 | slash = true; | |
1776 | continue; | |
1777 | } | |
1778 | ||
1779 | if (slash) { | |
1780 | slash = false; | |
1781 | *(t++) = '/'; | |
1782 | } | |
1783 | ||
1784 | *(t++) = *f; | |
1785 | } | |
1786 | ||
1787 | /* Special rule, if we are talking of the root directory, a | |
1788 | trailing slash is good */ | |
1789 | ||
1790 | if (t == path && slash) | |
1791 | *(t++) = '/'; | |
1792 | ||
1793 | *t = 0; | |
1794 | return path; | |
1795 | } | |
1796 | ||
1797 | bool path_startswith(const char *path, const char *prefix) { | |
1798 | assert(path); | |
1799 | assert(prefix); | |
1800 | ||
1801 | if ((path[0] == '/') != (prefix[0] == '/')) | |
1802 | return false; | |
1803 | ||
1804 | for (;;) { | |
1805 | size_t a, b; | |
1806 | ||
1807 | path += strspn(path, "/"); | |
1808 | prefix += strspn(prefix, "/"); | |
1809 | ||
1810 | if (*prefix == 0) | |
1811 | return true; | |
1812 | ||
1813 | if (*path == 0) | |
1814 | return false; | |
1815 | ||
1816 | a = strcspn(path, "/"); | |
1817 | b = strcspn(prefix, "/"); | |
1818 | ||
1819 | if (a != b) | |
1820 | return false; | |
1821 | ||
1822 | if (memcmp(path, prefix, a) != 0) | |
1823 | return false; | |
1824 | ||
1825 | path += a; | |
1826 | prefix += b; | |
1827 | } | |
1828 | } | |
1829 | ||
15ae422b LP |
1830 | bool path_equal(const char *a, const char *b) { |
1831 | assert(a); | |
1832 | assert(b); | |
1833 | ||
1834 | if ((a[0] == '/') != (b[0] == '/')) | |
1835 | return false; | |
1836 | ||
1837 | for (;;) { | |
1838 | size_t j, k; | |
1839 | ||
1840 | a += strspn(a, "/"); | |
1841 | b += strspn(b, "/"); | |
1842 | ||
1843 | if (*a == 0 && *b == 0) | |
1844 | return true; | |
1845 | ||
1846 | if (*a == 0 || *b == 0) | |
1847 | return false; | |
1848 | ||
1849 | j = strcspn(a, "/"); | |
1850 | k = strcspn(b, "/"); | |
1851 | ||
1852 | if (j != k) | |
1853 | return false; | |
1854 | ||
1855 | if (memcmp(a, b, j) != 0) | |
1856 | return false; | |
1857 | ||
1858 | a += j; | |
1859 | b += k; | |
1860 | } | |
1861 | } | |
1862 | ||
67d51650 | 1863 | char *ascii_strlower(char *t) { |
4fe88d28 LP |
1864 | char *p; |
1865 | ||
67d51650 | 1866 | assert(t); |
4fe88d28 | 1867 | |
67d51650 | 1868 | for (p = t; *p; p++) |
4fe88d28 LP |
1869 | if (*p >= 'A' && *p <= 'Z') |
1870 | *p = *p - 'A' + 'a'; | |
1871 | ||
67d51650 | 1872 | return t; |
4fe88d28 | 1873 | } |
1dccbe19 | 1874 | |
c85dc17b LP |
1875 | bool ignore_file(const char *filename) { |
1876 | assert(filename); | |
1877 | ||
1878 | return | |
1879 | filename[0] == '.' || | |
6c78be3c | 1880 | streq(filename, "lost+found") || |
e472d476 LP |
1881 | streq(filename, "aquota.user") || |
1882 | streq(filename, "aquota.group") || | |
c85dc17b LP |
1883 | endswith(filename, "~") || |
1884 | endswith(filename, ".rpmnew") || | |
1885 | endswith(filename, ".rpmsave") || | |
1886 | endswith(filename, ".rpmorig") || | |
1887 | endswith(filename, ".dpkg-old") || | |
1888 | endswith(filename, ".dpkg-new") || | |
1889 | endswith(filename, ".swp"); | |
1890 | } | |
1891 | ||
3a0ecb08 LP |
1892 | int fd_nonblock(int fd, bool nonblock) { |
1893 | int flags; | |
1894 | ||
1895 | assert(fd >= 0); | |
1896 | ||
1897 | if ((flags = fcntl(fd, F_GETFL, 0)) < 0) | |
1898 | return -errno; | |
1899 | ||
1900 | if (nonblock) | |
1901 | flags |= O_NONBLOCK; | |
1902 | else | |
1903 | flags &= ~O_NONBLOCK; | |
1904 | ||
1905 | if (fcntl(fd, F_SETFL, flags) < 0) | |
1906 | return -errno; | |
1907 | ||
1908 | return 0; | |
1909 | } | |
1910 | ||
1911 | int fd_cloexec(int fd, bool cloexec) { | |
1912 | int flags; | |
1913 | ||
1914 | assert(fd >= 0); | |
1915 | ||
1916 | if ((flags = fcntl(fd, F_GETFD, 0)) < 0) | |
1917 | return -errno; | |
1918 | ||
1919 | if (cloexec) | |
1920 | flags |= FD_CLOEXEC; | |
1921 | else | |
1922 | flags &= ~FD_CLOEXEC; | |
1923 | ||
1924 | if (fcntl(fd, F_SETFD, flags) < 0) | |
1925 | return -errno; | |
1926 | ||
1927 | return 0; | |
1928 | } | |
1929 | ||
a0d40ac5 LP |
1930 | int close_all_fds(const int except[], unsigned n_except) { |
1931 | DIR *d; | |
1932 | struct dirent *de; | |
1933 | int r = 0; | |
1934 | ||
1935 | if (!(d = opendir("/proc/self/fd"))) | |
1936 | return -errno; | |
1937 | ||
1938 | while ((de = readdir(d))) { | |
a7610064 | 1939 | int fd = -1; |
a0d40ac5 | 1940 | |
a16e1123 | 1941 | if (ignore_file(de->d_name)) |
a0d40ac5 LP |
1942 | continue; |
1943 | ||
720ce21d LP |
1944 | if (safe_atoi(de->d_name, &fd) < 0) |
1945 | /* Let's better ignore this, just in case */ | |
1946 | continue; | |
a0d40ac5 LP |
1947 | |
1948 | if (fd < 3) | |
1949 | continue; | |
1950 | ||
1951 | if (fd == dirfd(d)) | |
1952 | continue; | |
1953 | ||
1954 | if (except) { | |
1955 | bool found; | |
1956 | unsigned i; | |
1957 | ||
1958 | found = false; | |
1959 | for (i = 0; i < n_except; i++) | |
1960 | if (except[i] == fd) { | |
1961 | found = true; | |
1962 | break; | |
1963 | } | |
1964 | ||
1965 | if (found) | |
1966 | continue; | |
1967 | } | |
1968 | ||
720ce21d | 1969 | if (close_nointr(fd) < 0) { |
2f357920 | 1970 | /* Valgrind has its own FD and doesn't want to have it closed */ |
720ce21d LP |
1971 | if (errno != EBADF && r == 0) |
1972 | r = -errno; | |
2f357920 | 1973 | } |
a0d40ac5 LP |
1974 | } |
1975 | ||
a0d40ac5 LP |
1976 | closedir(d); |
1977 | return r; | |
1978 | } | |
1979 | ||
db12775d LP |
1980 | bool chars_intersect(const char *a, const char *b) { |
1981 | const char *p; | |
1982 | ||
1983 | /* Returns true if any of the chars in a are in b. */ | |
1984 | for (p = a; *p; p++) | |
1985 | if (strchr(b, *p)) | |
1986 | return true; | |
1987 | ||
1988 | return false; | |
1989 | } | |
1990 | ||
8b6c7120 LP |
1991 | char *format_timestamp(char *buf, size_t l, usec_t t) { |
1992 | struct tm tm; | |
1993 | time_t sec; | |
1994 | ||
1995 | assert(buf); | |
1996 | assert(l > 0); | |
1997 | ||
1998 | if (t <= 0) | |
1999 | return NULL; | |
2000 | ||
f872ec33 | 2001 | sec = (time_t) (t / USEC_PER_SEC); |
8b6c7120 LP |
2002 | |
2003 | if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0) | |
2004 | return NULL; | |
2005 | ||
2006 | return buf; | |
2007 | } | |
2008 | ||
584be568 LP |
2009 | char *format_timestamp_pretty(char *buf, size_t l, usec_t t) { |
2010 | usec_t n, d; | |
2011 | ||
2012 | n = now(CLOCK_REALTIME); | |
2013 | ||
2014 | if (t <= 0 || t > n || t + USEC_PER_DAY*7 <= t) | |
2015 | return NULL; | |
2016 | ||
2017 | d = n - t; | |
2018 | ||
2019 | if (d >= USEC_PER_YEAR) | |
2020 | snprintf(buf, l, "%llu years and %llu months ago", | |
2021 | (unsigned long long) (d / USEC_PER_YEAR), | |
2022 | (unsigned long long) ((d % USEC_PER_YEAR) / USEC_PER_MONTH)); | |
2023 | else if (d >= USEC_PER_MONTH) | |
2024 | snprintf(buf, l, "%llu months and %llu days ago", | |
2025 | (unsigned long long) (d / USEC_PER_MONTH), | |
2026 | (unsigned long long) ((d % USEC_PER_MONTH) / USEC_PER_DAY)); | |
2027 | else if (d >= USEC_PER_WEEK) | |
2028 | snprintf(buf, l, "%llu weeks and %llu days ago", | |
2029 | (unsigned long long) (d / USEC_PER_WEEK), | |
2030 | (unsigned long long) ((d % USEC_PER_WEEK) / USEC_PER_DAY)); | |
2031 | else if (d >= 2*USEC_PER_DAY) | |
2032 | snprintf(buf, l, "%llu days ago", (unsigned long long) (d / USEC_PER_DAY)); | |
2033 | else if (d >= 25*USEC_PER_HOUR) | |
2034 | snprintf(buf, l, "1 day and %lluh ago", | |
2035 | (unsigned long long) ((d - USEC_PER_DAY) / USEC_PER_HOUR)); | |
2036 | else if (d >= 6*USEC_PER_HOUR) | |
2037 | snprintf(buf, l, "%lluh ago", | |
2038 | (unsigned long long) (d / USEC_PER_HOUR)); | |
2039 | else if (d >= USEC_PER_HOUR) | |
2040 | snprintf(buf, l, "%lluh %llumin ago", | |
2041 | (unsigned long long) (d / USEC_PER_HOUR), | |
2042 | (unsigned long long) ((d % USEC_PER_HOUR) / USEC_PER_MINUTE)); | |
2043 | else if (d >= 5*USEC_PER_MINUTE) | |
2044 | snprintf(buf, l, "%llumin ago", | |
2045 | (unsigned long long) (d / USEC_PER_MINUTE)); | |
2046 | else if (d >= USEC_PER_MINUTE) | |
2047 | snprintf(buf, l, "%llumin %llus ago", | |
2048 | (unsigned long long) (d / USEC_PER_MINUTE), | |
2049 | (unsigned long long) ((d % USEC_PER_MINUTE) / USEC_PER_SEC)); | |
2050 | else if (d >= USEC_PER_SEC) | |
2051 | snprintf(buf, l, "%llus ago", | |
2052 | (unsigned long long) (d / USEC_PER_SEC)); | |
2053 | else if (d >= USEC_PER_MSEC) | |
2054 | snprintf(buf, l, "%llums ago", | |
2055 | (unsigned long long) (d / USEC_PER_MSEC)); | |
2056 | else if (d > 0) | |
2057 | snprintf(buf, l, "%lluus ago", | |
2058 | (unsigned long long) d); | |
2059 | else | |
2060 | snprintf(buf, l, "now"); | |
2061 | ||
2062 | buf[l-1] = 0; | |
2063 | return buf; | |
2064 | } | |
2065 | ||
871d7de4 LP |
2066 | char *format_timespan(char *buf, size_t l, usec_t t) { |
2067 | static const struct { | |
2068 | const char *suffix; | |
2069 | usec_t usec; | |
2070 | } table[] = { | |
2071 | { "w", USEC_PER_WEEK }, | |
2072 | { "d", USEC_PER_DAY }, | |
2073 | { "h", USEC_PER_HOUR }, | |
2074 | { "min", USEC_PER_MINUTE }, | |
2075 | { "s", USEC_PER_SEC }, | |
2076 | { "ms", USEC_PER_MSEC }, | |
2077 | { "us", 1 }, | |
2078 | }; | |
2079 | ||
2080 | unsigned i; | |
2081 | char *p = buf; | |
2082 | ||
2083 | assert(buf); | |
2084 | assert(l > 0); | |
2085 | ||
2086 | if (t == (usec_t) -1) | |
2087 | return NULL; | |
2088 | ||
4502d22c LP |
2089 | if (t == 0) { |
2090 | snprintf(p, l, "0"); | |
2091 | p[l-1] = 0; | |
2092 | return p; | |
2093 | } | |
2094 | ||
871d7de4 LP |
2095 | /* The result of this function can be parsed with parse_usec */ |
2096 | ||
2097 | for (i = 0; i < ELEMENTSOF(table); i++) { | |
2098 | int k; | |
2099 | size_t n; | |
2100 | ||
2101 | if (t < table[i].usec) | |
2102 | continue; | |
2103 | ||
2104 | if (l <= 1) | |
2105 | break; | |
2106 | ||
2107 | k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix); | |
2108 | n = MIN((size_t) k, l); | |
2109 | ||
2110 | l -= n; | |
2111 | p += n; | |
2112 | ||
2113 | t %= table[i].usec; | |
2114 | } | |
2115 | ||
2116 | *p = 0; | |
2117 | ||
2118 | return buf; | |
2119 | } | |
2120 | ||
42856c10 LP |
2121 | bool fstype_is_network(const char *fstype) { |
2122 | static const char * const table[] = { | |
2123 | "cifs", | |
2124 | "smbfs", | |
2125 | "ncpfs", | |
2126 | "nfs", | |
ca139f94 LP |
2127 | "nfs4", |
2128 | "gfs", | |
2129 | "gfs2" | |
42856c10 LP |
2130 | }; |
2131 | ||
2132 | unsigned i; | |
2133 | ||
2134 | for (i = 0; i < ELEMENTSOF(table); i++) | |
2135 | if (streq(table[i], fstype)) | |
2136 | return true; | |
2137 | ||
2138 | return false; | |
2139 | } | |
2140 | ||
601f6a1e LP |
2141 | int chvt(int vt) { |
2142 | int fd, r = 0; | |
2143 | ||
2144 | if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0) | |
2145 | return -errno; | |
2146 | ||
2147 | if (vt < 0) { | |
2148 | int tiocl[2] = { | |
2149 | TIOCL_GETKMSGREDIRECT, | |
2150 | 0 | |
2151 | }; | |
2152 | ||
2153 | if (ioctl(fd, TIOCLINUX, tiocl) < 0) | |
2154 | return -errno; | |
2155 | ||
2156 | vt = tiocl[0] <= 0 ? 1 : tiocl[0]; | |
2157 | } | |
2158 | ||
2159 | if (ioctl(fd, VT_ACTIVATE, vt) < 0) | |
2160 | r = -errno; | |
2161 | ||
a16e1123 | 2162 | close_nointr_nofail(r); |
601f6a1e LP |
2163 | return r; |
2164 | } | |
2165 | ||
80876c20 LP |
2166 | int read_one_char(FILE *f, char *ret, bool *need_nl) { |
2167 | struct termios old_termios, new_termios; | |
2168 | char c; | |
20c03b7b | 2169 | char line[LINE_MAX]; |
80876c20 LP |
2170 | |
2171 | assert(f); | |
2172 | assert(ret); | |
2173 | ||
2174 | if (tcgetattr(fileno(f), &old_termios) >= 0) { | |
2175 | new_termios = old_termios; | |
2176 | ||
2177 | new_termios.c_lflag &= ~ICANON; | |
2178 | new_termios.c_cc[VMIN] = 1; | |
2179 | new_termios.c_cc[VTIME] = 0; | |
2180 | ||
2181 | if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) { | |
2182 | size_t k; | |
2183 | ||
2184 | k = fread(&c, 1, 1, f); | |
2185 | ||
2186 | tcsetattr(fileno(f), TCSADRAIN, &old_termios); | |
2187 | ||
2188 | if (k <= 0) | |
2189 | return -EIO; | |
2190 | ||
2191 | if (need_nl) | |
2192 | *need_nl = c != '\n'; | |
2193 | ||
2194 | *ret = c; | |
2195 | return 0; | |
2196 | } | |
2197 | } | |
2198 | ||
2199 | if (!(fgets(line, sizeof(line), f))) | |
2200 | return -EIO; | |
2201 | ||
2202 | truncate_nl(line); | |
2203 | ||
2204 | if (strlen(line) != 1) | |
2205 | return -EBADMSG; | |
2206 | ||
2207 | if (need_nl) | |
2208 | *need_nl = false; | |
2209 | ||
2210 | *ret = line[0]; | |
2211 | return 0; | |
2212 | } | |
2213 | ||
2214 | int ask(char *ret, const char *replies, const char *text, ...) { | |
1b39d4b9 LP |
2215 | bool on_tty; |
2216 | ||
80876c20 LP |
2217 | assert(ret); |
2218 | assert(replies); | |
2219 | assert(text); | |
2220 | ||
1b39d4b9 LP |
2221 | on_tty = isatty(STDOUT_FILENO); |
2222 | ||
80876c20 LP |
2223 | for (;;) { |
2224 | va_list ap; | |
2225 | char c; | |
2226 | int r; | |
2227 | bool need_nl = true; | |
2228 | ||
1b39d4b9 LP |
2229 | if (on_tty) |
2230 | fputs("\x1B[1m", stdout); | |
b1b2dc0c | 2231 | |
80876c20 LP |
2232 | va_start(ap, text); |
2233 | vprintf(text, ap); | |
2234 | va_end(ap); | |
2235 | ||
1b39d4b9 LP |
2236 | if (on_tty) |
2237 | fputs("\x1B[0m", stdout); | |
b1b2dc0c | 2238 | |
80876c20 LP |
2239 | fflush(stdout); |
2240 | ||
2241 | if ((r = read_one_char(stdin, &c, &need_nl)) < 0) { | |
2242 | ||
2243 | if (r == -EBADMSG) { | |
2244 | puts("Bad input, please try again."); | |
2245 | continue; | |
2246 | } | |
2247 | ||
2248 | putchar('\n'); | |
2249 | return r; | |
2250 | } | |
2251 | ||
2252 | if (need_nl) | |
2253 | putchar('\n'); | |
2254 | ||
2255 | if (strchr(replies, c)) { | |
2256 | *ret = c; | |
2257 | return 0; | |
2258 | } | |
2259 | ||
2260 | puts("Read unexpected character, please try again."); | |
2261 | } | |
2262 | } | |
2263 | ||
2264 | int reset_terminal(int fd) { | |
2265 | struct termios termios; | |
2266 | int r = 0; | |
3fe5e5d4 LP |
2267 | long arg; |
2268 | ||
2269 | /* Set terminal to some sane defaults */ | |
80876c20 LP |
2270 | |
2271 | assert(fd >= 0); | |
2272 | ||
eed1d0e3 LP |
2273 | /* We leave locked terminal attributes untouched, so that |
2274 | * Plymouth may set whatever it wants to set, and we don't | |
2275 | * interfere with that. */ | |
3fe5e5d4 LP |
2276 | |
2277 | /* Disable exclusive mode, just in case */ | |
2278 | ioctl(fd, TIOCNXCL); | |
2279 | ||
2280 | /* Enable console unicode mode */ | |
2281 | arg = K_UNICODE; | |
2282 | ioctl(fd, KDSKBMODE, &arg); | |
80876c20 LP |
2283 | |
2284 | if (tcgetattr(fd, &termios) < 0) { | |
2285 | r = -errno; | |
2286 | goto finish; | |
2287 | } | |
2288 | ||
aaf694ca LP |
2289 | /* We only reset the stuff that matters to the software. How |
2290 | * hardware is set up we don't touch assuming that somebody | |
2291 | * else will do that for us */ | |
2292 | ||
2293 | termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC); | |
80876c20 LP |
2294 | termios.c_iflag |= ICRNL | IMAXBEL | IUTF8; |
2295 | termios.c_oflag |= ONLCR; | |
2296 | termios.c_cflag |= CREAD; | |
2297 | termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE; | |
2298 | ||
2299 | termios.c_cc[VINTR] = 03; /* ^C */ | |
2300 | termios.c_cc[VQUIT] = 034; /* ^\ */ | |
2301 | termios.c_cc[VERASE] = 0177; | |
2302 | termios.c_cc[VKILL] = 025; /* ^X */ | |
2303 | termios.c_cc[VEOF] = 04; /* ^D */ | |
2304 | termios.c_cc[VSTART] = 021; /* ^Q */ | |
2305 | termios.c_cc[VSTOP] = 023; /* ^S */ | |
2306 | termios.c_cc[VSUSP] = 032; /* ^Z */ | |
2307 | termios.c_cc[VLNEXT] = 026; /* ^V */ | |
2308 | termios.c_cc[VWERASE] = 027; /* ^W */ | |
2309 | termios.c_cc[VREPRINT] = 022; /* ^R */ | |
aaf694ca LP |
2310 | termios.c_cc[VEOL] = 0; |
2311 | termios.c_cc[VEOL2] = 0; | |
80876c20 LP |
2312 | |
2313 | termios.c_cc[VTIME] = 0; | |
2314 | termios.c_cc[VMIN] = 1; | |
2315 | ||
2316 | if (tcsetattr(fd, TCSANOW, &termios) < 0) | |
2317 | r = -errno; | |
2318 | ||
2319 | finish: | |
2320 | /* Just in case, flush all crap out */ | |
2321 | tcflush(fd, TCIOFLUSH); | |
2322 | ||
2323 | return r; | |
2324 | } | |
2325 | ||
2326 | int open_terminal(const char *name, int mode) { | |
2327 | int fd, r; | |
f73f76ac | 2328 | unsigned c = 0; |
80876c20 | 2329 | |
f73f76ac LP |
2330 | /* |
2331 | * If a TTY is in the process of being closed opening it might | |
2332 | * cause EIO. This is horribly awful, but unlikely to be | |
2333 | * changed in the kernel. Hence we work around this problem by | |
2334 | * retrying a couple of times. | |
2335 | * | |
2336 | * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245 | |
2337 | */ | |
2338 | ||
2339 | for (;;) { | |
2340 | if ((fd = open(name, mode)) >= 0) | |
2341 | break; | |
2342 | ||
2343 | if (errno != EIO) | |
2344 | return -errno; | |
2345 | ||
2346 | if (c >= 20) | |
2347 | return -errno; | |
2348 | ||
2349 | usleep(50 * USEC_PER_MSEC); | |
2350 | c++; | |
2351 | } | |
2352 | ||
2353 | if (fd < 0) | |
80876c20 LP |
2354 | return -errno; |
2355 | ||
2356 | if ((r = isatty(fd)) < 0) { | |
2357 | close_nointr_nofail(fd); | |
2358 | return -errno; | |
2359 | } | |
2360 | ||
2361 | if (!r) { | |
2362 | close_nointr_nofail(fd); | |
2363 | return -ENOTTY; | |
2364 | } | |
2365 | ||
2366 | return fd; | |
2367 | } | |
2368 | ||
2369 | int flush_fd(int fd) { | |
2370 | struct pollfd pollfd; | |
2371 | ||
2372 | zero(pollfd); | |
2373 | pollfd.fd = fd; | |
2374 | pollfd.events = POLLIN; | |
2375 | ||
2376 | for (;;) { | |
20c03b7b | 2377 | char buf[LINE_MAX]; |
80876c20 LP |
2378 | ssize_t l; |
2379 | int r; | |
2380 | ||
2381 | if ((r = poll(&pollfd, 1, 0)) < 0) { | |
2382 | ||
2383 | if (errno == EINTR) | |
2384 | continue; | |
2385 | ||
2386 | return -errno; | |
2387 | } | |
2388 | ||
2389 | if (r == 0) | |
2390 | return 0; | |
2391 | ||
2392 | if ((l = read(fd, buf, sizeof(buf))) < 0) { | |
2393 | ||
2394 | if (errno == EINTR) | |
2395 | continue; | |
2396 | ||
2397 | if (errno == EAGAIN) | |
2398 | return 0; | |
2399 | ||
2400 | return -errno; | |
2401 | } | |
2402 | ||
2403 | if (l <= 0) | |
2404 | return 0; | |
2405 | } | |
2406 | } | |
2407 | ||
21de3988 | 2408 | int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) { |
bab45044 | 2409 | int fd = -1, notify = -1, r, wd = -1; |
80876c20 LP |
2410 | |
2411 | assert(name); | |
2412 | ||
2413 | /* We use inotify to be notified when the tty is closed. We | |
2414 | * create the watch before checking if we can actually acquire | |
2415 | * it, so that we don't lose any event. | |
2416 | * | |
2417 | * Note: strictly speaking this actually watches for the | |
2418 | * device being closed, it does *not* really watch whether a | |
2419 | * tty loses its controlling process. However, unless some | |
2420 | * rogue process uses TIOCNOTTY on /dev/tty *after* closing | |
2421 | * its tty otherwise this will not become a problem. As long | |
2422 | * as the administrator makes sure not configure any service | |
2423 | * on the same tty as an untrusted user this should not be a | |
2424 | * problem. (Which he probably should not do anyway.) */ | |
2425 | ||
2426 | if (!fail && !force) { | |
2427 | if ((notify = inotify_init1(IN_CLOEXEC)) < 0) { | |
2428 | r = -errno; | |
2429 | goto fail; | |
2430 | } | |
2431 | ||
2432 | if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) { | |
2433 | r = -errno; | |
2434 | goto fail; | |
2435 | } | |
2436 | } | |
2437 | ||
2438 | for (;;) { | |
e3d1855b LP |
2439 | if (notify >= 0) |
2440 | if ((r = flush_fd(notify)) < 0) | |
2441 | goto fail; | |
80876c20 LP |
2442 | |
2443 | /* We pass here O_NOCTTY only so that we can check the return | |
2444 | * value TIOCSCTTY and have a reliable way to figure out if we | |
2445 | * successfully became the controlling process of the tty */ | |
2446 | if ((fd = open_terminal(name, O_RDWR|O_NOCTTY)) < 0) | |
2447 | return -errno; | |
2448 | ||
2449 | /* First, try to get the tty */ | |
21de3988 LP |
2450 | r = ioctl(fd, TIOCSCTTY, force); |
2451 | ||
2452 | /* Sometimes it makes sense to ignore TIOCSCTTY | |
2453 | * returning EPERM, i.e. when very likely we already | |
2454 | * are have this controlling terminal. */ | |
2455 | if (r < 0 && errno == EPERM && ignore_tiocstty_eperm) | |
2456 | r = 0; | |
2457 | ||
2458 | if (r < 0 && (force || fail || errno != EPERM)) { | |
80876c20 LP |
2459 | r = -errno; |
2460 | goto fail; | |
2461 | } | |
2462 | ||
2463 | if (r >= 0) | |
2464 | break; | |
2465 | ||
2466 | assert(!fail); | |
2467 | assert(!force); | |
2468 | assert(notify >= 0); | |
2469 | ||
2470 | for (;;) { | |
f601daa7 | 2471 | uint8_t inotify_buffer[sizeof(struct inotify_event) + FILENAME_MAX]; |
80876c20 | 2472 | ssize_t l; |
f601daa7 | 2473 | struct inotify_event *e; |
80876c20 | 2474 | |
f601daa7 | 2475 | if ((l = read(notify, &inotify_buffer, sizeof(inotify_buffer))) < 0) { |
80876c20 | 2476 | |
f601daa7 LP |
2477 | if (errno == EINTR) |
2478 | continue; | |
2479 | ||
2480 | r = -errno; | |
2481 | goto fail; | |
2482 | } | |
2483 | ||
2484 | e = (struct inotify_event*) inotify_buffer; | |
80876c20 | 2485 | |
f601daa7 LP |
2486 | while (l > 0) { |
2487 | size_t step; | |
80876c20 | 2488 | |
f601daa7 | 2489 | if (e->wd != wd || !(e->mask & IN_CLOSE)) { |
80876c20 | 2490 | r = -EIO; |
f601daa7 LP |
2491 | goto fail; |
2492 | } | |
80876c20 | 2493 | |
f601daa7 LP |
2494 | step = sizeof(struct inotify_event) + e->len; |
2495 | assert(step <= (size_t) l); | |
80876c20 | 2496 | |
f601daa7 LP |
2497 | e = (struct inotify_event*) ((uint8_t*) e + step); |
2498 | l -= step; | |
80876c20 LP |
2499 | } |
2500 | ||
2501 | break; | |
2502 | } | |
2503 | ||
2504 | /* We close the tty fd here since if the old session | |
2505 | * ended our handle will be dead. It's important that | |
2506 | * we do this after sleeping, so that we don't enter | |
2507 | * an endless loop. */ | |
2508 | close_nointr_nofail(fd); | |
2509 | } | |
2510 | ||
2511 | if (notify >= 0) | |
a16e1123 | 2512 | close_nointr_nofail(notify); |
80876c20 LP |
2513 | |
2514 | if ((r = reset_terminal(fd)) < 0) | |
2515 | log_warning("Failed to reset terminal: %s", strerror(-r)); | |
2516 | ||
2517 | return fd; | |
2518 | ||
2519 | fail: | |
2520 | if (fd >= 0) | |
a16e1123 | 2521 | close_nointr_nofail(fd); |
80876c20 LP |
2522 | |
2523 | if (notify >= 0) | |
a16e1123 | 2524 | close_nointr_nofail(notify); |
80876c20 LP |
2525 | |
2526 | return r; | |
2527 | } | |
2528 | ||
2529 | int release_terminal(void) { | |
2530 | int r = 0, fd; | |
57cd2192 | 2531 | struct sigaction sa_old, sa_new; |
80876c20 | 2532 | |
57cd2192 | 2533 | if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0) |
80876c20 LP |
2534 | return -errno; |
2535 | ||
57cd2192 LP |
2536 | /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed |
2537 | * by our own TIOCNOTTY */ | |
2538 | ||
2539 | zero(sa_new); | |
2540 | sa_new.sa_handler = SIG_IGN; | |
2541 | sa_new.sa_flags = SA_RESTART; | |
2542 | assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0); | |
2543 | ||
80876c20 LP |
2544 | if (ioctl(fd, TIOCNOTTY) < 0) |
2545 | r = -errno; | |
2546 | ||
57cd2192 LP |
2547 | assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0); |
2548 | ||
80876c20 LP |
2549 | close_nointr_nofail(fd); |
2550 | return r; | |
2551 | } | |
2552 | ||
9a34ec5f LP |
2553 | int sigaction_many(const struct sigaction *sa, ...) { |
2554 | va_list ap; | |
2555 | int r = 0, sig; | |
2556 | ||
2557 | va_start(ap, sa); | |
2558 | while ((sig = va_arg(ap, int)) > 0) | |
2559 | if (sigaction(sig, sa, NULL) < 0) | |
2560 | r = -errno; | |
2561 | va_end(ap); | |
2562 | ||
2563 | return r; | |
2564 | } | |
2565 | ||
2566 | int ignore_signals(int sig, ...) { | |
a337c6fc | 2567 | struct sigaction sa; |
9a34ec5f LP |
2568 | va_list ap; |
2569 | int r = 0; | |
a337c6fc LP |
2570 | |
2571 | zero(sa); | |
2572 | sa.sa_handler = SIG_IGN; | |
2573 | sa.sa_flags = SA_RESTART; | |
2574 | ||
9a34ec5f LP |
2575 | if (sigaction(sig, &sa, NULL) < 0) |
2576 | r = -errno; | |
2577 | ||
2578 | va_start(ap, sig); | |
2579 | while ((sig = va_arg(ap, int)) > 0) | |
2580 | if (sigaction(sig, &sa, NULL) < 0) | |
2581 | r = -errno; | |
2582 | va_end(ap); | |
2583 | ||
2584 | return r; | |
2585 | } | |
2586 | ||
2587 | int default_signals(int sig, ...) { | |
2588 | struct sigaction sa; | |
2589 | va_list ap; | |
2590 | int r = 0; | |
2591 | ||
2592 | zero(sa); | |
2593 | sa.sa_handler = SIG_DFL; | |
2594 | sa.sa_flags = SA_RESTART; | |
2595 | ||
2596 | if (sigaction(sig, &sa, NULL) < 0) | |
2597 | r = -errno; | |
2598 | ||
2599 | va_start(ap, sig); | |
2600 | while ((sig = va_arg(ap, int)) > 0) | |
2601 | if (sigaction(sig, &sa, NULL) < 0) | |
2602 | r = -errno; | |
2603 | va_end(ap); | |
2604 | ||
2605 | return r; | |
a337c6fc LP |
2606 | } |
2607 | ||
8d567588 LP |
2608 | int close_pipe(int p[]) { |
2609 | int a = 0, b = 0; | |
2610 | ||
2611 | assert(p); | |
2612 | ||
2613 | if (p[0] >= 0) { | |
2614 | a = close_nointr(p[0]); | |
2615 | p[0] = -1; | |
2616 | } | |
2617 | ||
2618 | if (p[1] >= 0) { | |
2619 | b = close_nointr(p[1]); | |
2620 | p[1] = -1; | |
2621 | } | |
2622 | ||
2623 | return a < 0 ? a : b; | |
2624 | } | |
2625 | ||
eb22ac37 | 2626 | ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) { |
8d567588 LP |
2627 | uint8_t *p; |
2628 | ssize_t n = 0; | |
2629 | ||
2630 | assert(fd >= 0); | |
2631 | assert(buf); | |
2632 | ||
2633 | p = buf; | |
2634 | ||
2635 | while (nbytes > 0) { | |
2636 | ssize_t k; | |
2637 | ||
2638 | if ((k = read(fd, p, nbytes)) <= 0) { | |
2639 | ||
eb22ac37 | 2640 | if (k < 0 && errno == EINTR) |
8d567588 LP |
2641 | continue; |
2642 | ||
eb22ac37 | 2643 | if (k < 0 && errno == EAGAIN && do_poll) { |
8d567588 LP |
2644 | struct pollfd pollfd; |
2645 | ||
2646 | zero(pollfd); | |
2647 | pollfd.fd = fd; | |
2648 | pollfd.events = POLLIN; | |
2649 | ||
2650 | if (poll(&pollfd, 1, -1) < 0) { | |
2651 | if (errno == EINTR) | |
2652 | continue; | |
2653 | ||
2654 | return n > 0 ? n : -errno; | |
2655 | } | |
2656 | ||
2657 | if (pollfd.revents != POLLIN) | |
2658 | return n > 0 ? n : -EIO; | |
2659 | ||
2660 | continue; | |
2661 | } | |
2662 | ||
2663 | return n > 0 ? n : (k < 0 ? -errno : 0); | |
2664 | } | |
2665 | ||
2666 | p += k; | |
2667 | nbytes -= k; | |
2668 | n += k; | |
2669 | } | |
2670 | ||
2671 | return n; | |
2672 | } | |
2673 | ||
eb22ac37 LP |
2674 | ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) { |
2675 | const uint8_t *p; | |
2676 | ssize_t n = 0; | |
2677 | ||
2678 | assert(fd >= 0); | |
2679 | assert(buf); | |
2680 | ||
2681 | p = buf; | |
2682 | ||
2683 | while (nbytes > 0) { | |
2684 | ssize_t k; | |
2685 | ||
2686 | if ((k = write(fd, p, nbytes)) <= 0) { | |
2687 | ||
2688 | if (k < 0 && errno == EINTR) | |
2689 | continue; | |
2690 | ||
2691 | if (k < 0 && errno == EAGAIN && do_poll) { | |
2692 | struct pollfd pollfd; | |
2693 | ||
2694 | zero(pollfd); | |
2695 | pollfd.fd = fd; | |
2696 | pollfd.events = POLLOUT; | |
2697 | ||
2698 | if (poll(&pollfd, 1, -1) < 0) { | |
2699 | if (errno == EINTR) | |
2700 | continue; | |
2701 | ||
2702 | return n > 0 ? n : -errno; | |
2703 | } | |
2704 | ||
2705 | if (pollfd.revents != POLLOUT) | |
2706 | return n > 0 ? n : -EIO; | |
2707 | ||
2708 | continue; | |
2709 | } | |
2710 | ||
2711 | return n > 0 ? n : (k < 0 ? -errno : 0); | |
2712 | } | |
2713 | ||
2714 | p += k; | |
2715 | nbytes -= k; | |
2716 | n += k; | |
2717 | } | |
2718 | ||
2719 | return n; | |
2720 | } | |
2721 | ||
8d567588 LP |
2722 | int path_is_mount_point(const char *t) { |
2723 | struct stat a, b; | |
35d2e7ec LP |
2724 | char *parent; |
2725 | int r; | |
8d567588 LP |
2726 | |
2727 | if (lstat(t, &a) < 0) { | |
8d567588 LP |
2728 | if (errno == ENOENT) |
2729 | return 0; | |
2730 | ||
2731 | return -errno; | |
2732 | } | |
2733 | ||
35d2e7ec LP |
2734 | if ((r = parent_of_path(t, &parent)) < 0) |
2735 | return r; | |
8d567588 | 2736 | |
35d2e7ec LP |
2737 | r = lstat(parent, &b); |
2738 | free(parent); | |
8d567588 | 2739 | |
35d2e7ec LP |
2740 | if (r < 0) |
2741 | return -errno; | |
8d567588 LP |
2742 | |
2743 | return a.st_dev != b.st_dev; | |
2744 | } | |
2745 | ||
24a6e4a4 LP |
2746 | int parse_usec(const char *t, usec_t *usec) { |
2747 | static const struct { | |
2748 | const char *suffix; | |
2749 | usec_t usec; | |
2750 | } table[] = { | |
2751 | { "sec", USEC_PER_SEC }, | |
2752 | { "s", USEC_PER_SEC }, | |
2753 | { "min", USEC_PER_MINUTE }, | |
2754 | { "hr", USEC_PER_HOUR }, | |
2755 | { "h", USEC_PER_HOUR }, | |
2756 | { "d", USEC_PER_DAY }, | |
2757 | { "w", USEC_PER_WEEK }, | |
2758 | { "msec", USEC_PER_MSEC }, | |
2759 | { "ms", USEC_PER_MSEC }, | |
2760 | { "m", USEC_PER_MINUTE }, | |
2761 | { "usec", 1ULL }, | |
2762 | { "us", 1ULL }, | |
2763 | { "", USEC_PER_SEC }, | |
2764 | }; | |
2765 | ||
2766 | const char *p; | |
2767 | usec_t r = 0; | |
2768 | ||
2769 | assert(t); | |
2770 | assert(usec); | |
2771 | ||
2772 | p = t; | |
2773 | do { | |
2774 | long long l; | |
2775 | char *e; | |
2776 | unsigned i; | |
2777 | ||
2778 | errno = 0; | |
2779 | l = strtoll(p, &e, 10); | |
2780 | ||
2781 | if (errno != 0) | |
2782 | return -errno; | |
2783 | ||
2784 | if (l < 0) | |
2785 | return -ERANGE; | |
2786 | ||
2787 | if (e == p) | |
2788 | return -EINVAL; | |
2789 | ||
2790 | e += strspn(e, WHITESPACE); | |
2791 | ||
2792 | for (i = 0; i < ELEMENTSOF(table); i++) | |
2793 | if (startswith(e, table[i].suffix)) { | |
2794 | r += (usec_t) l * table[i].usec; | |
2795 | p = e + strlen(table[i].suffix); | |
2796 | break; | |
2797 | } | |
2798 | ||
2799 | if (i >= ELEMENTSOF(table)) | |
2800 | return -EINVAL; | |
2801 | ||
2802 | } while (*p != 0); | |
2803 | ||
2804 | *usec = r; | |
2805 | ||
2806 | return 0; | |
2807 | } | |
2808 | ||
843d2643 LP |
2809 | int make_stdio(int fd) { |
2810 | int r, s, t; | |
2811 | ||
2812 | assert(fd >= 0); | |
2813 | ||
2814 | r = dup2(fd, STDIN_FILENO); | |
2815 | s = dup2(fd, STDOUT_FILENO); | |
2816 | t = dup2(fd, STDERR_FILENO); | |
2817 | ||
2818 | if (fd >= 3) | |
2819 | close_nointr_nofail(fd); | |
2820 | ||
2821 | if (r < 0 || s < 0 || t < 0) | |
2822 | return -errno; | |
2823 | ||
2824 | return 0; | |
2825 | } | |
2826 | ||
ade509ce LP |
2827 | int make_null_stdio(void) { |
2828 | int null_fd; | |
2829 | ||
2830 | if ((null_fd = open("/dev/null", O_RDWR|O_NOCTTY)) < 0) | |
2831 | return -errno; | |
2832 | ||
2833 | return make_stdio(null_fd); | |
2834 | } | |
2835 | ||
8407a5d0 LP |
2836 | bool is_device_path(const char *path) { |
2837 | ||
2838 | /* Returns true on paths that refer to a device, either in | |
2839 | * sysfs or in /dev */ | |
2840 | ||
2841 | return | |
2842 | path_startswith(path, "/dev/") || | |
2843 | path_startswith(path, "/sys/"); | |
2844 | } | |
2845 | ||
01f78473 LP |
2846 | int dir_is_empty(const char *path) { |
2847 | DIR *d; | |
2848 | int r; | |
2849 | struct dirent buf, *de; | |
2850 | ||
2851 | if (!(d = opendir(path))) | |
2852 | return -errno; | |
2853 | ||
2854 | for (;;) { | |
2855 | if ((r = readdir_r(d, &buf, &de)) > 0) { | |
2856 | r = -r; | |
2857 | break; | |
2858 | } | |
2859 | ||
2860 | if (!de) { | |
2861 | r = 1; | |
2862 | break; | |
2863 | } | |
2864 | ||
2865 | if (!ignore_file(de->d_name)) { | |
2866 | r = 0; | |
2867 | break; | |
2868 | } | |
2869 | } | |
2870 | ||
2871 | closedir(d); | |
2872 | return r; | |
2873 | } | |
2874 | ||
d3782d60 LP |
2875 | unsigned long long random_ull(void) { |
2876 | int fd; | |
2877 | uint64_t ull; | |
2878 | ssize_t r; | |
2879 | ||
2880 | if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) | |
2881 | goto fallback; | |
2882 | ||
eb22ac37 | 2883 | r = loop_read(fd, &ull, sizeof(ull), true); |
d3782d60 LP |
2884 | close_nointr_nofail(fd); |
2885 | ||
2886 | if (r != sizeof(ull)) | |
2887 | goto fallback; | |
2888 | ||
2889 | return ull; | |
2890 | ||
2891 | fallback: | |
2892 | return random() * RAND_MAX + random(); | |
2893 | } | |
2894 | ||
5b6319dc LP |
2895 | void rename_process(const char name[8]) { |
2896 | assert(name); | |
2897 | ||
2898 | prctl(PR_SET_NAME, name); | |
2899 | ||
2900 | /* This is a like a poor man's setproctitle(). The string | |
2901 | * passed should fit in 7 chars (i.e. the length of | |
2902 | * "systemd") */ | |
2903 | ||
2904 | if (program_invocation_name) | |
2905 | strncpy(program_invocation_name, name, strlen(program_invocation_name)); | |
2906 | } | |
2907 | ||
7d793605 LP |
2908 | void sigset_add_many(sigset_t *ss, ...) { |
2909 | va_list ap; | |
2910 | int sig; | |
2911 | ||
2912 | assert(ss); | |
2913 | ||
2914 | va_start(ap, ss); | |
2915 | while ((sig = va_arg(ap, int)) > 0) | |
2916 | assert_se(sigaddset(ss, sig) == 0); | |
2917 | va_end(ap); | |
2918 | } | |
2919 | ||
ef2f1067 LP |
2920 | char* gethostname_malloc(void) { |
2921 | struct utsname u; | |
2922 | ||
2923 | assert_se(uname(&u) >= 0); | |
2924 | ||
2925 | if (u.nodename[0]) | |
2926 | return strdup(u.nodename); | |
2927 | ||
2928 | return strdup(u.sysname); | |
2929 | } | |
2930 | ||
2931 | char* getlogname_malloc(void) { | |
2932 | uid_t uid; | |
2933 | long bufsize; | |
2934 | char *buf, *name; | |
2935 | struct passwd pwbuf, *pw = NULL; | |
2936 | struct stat st; | |
2937 | ||
2938 | if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0) | |
2939 | uid = st.st_uid; | |
2940 | else | |
2941 | uid = getuid(); | |
2942 | ||
2943 | /* Shortcut things to avoid NSS lookups */ | |
2944 | if (uid == 0) | |
2945 | return strdup("root"); | |
2946 | ||
2947 | if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0) | |
2948 | bufsize = 4096; | |
2949 | ||
2950 | if (!(buf = malloc(bufsize))) | |
2951 | return NULL; | |
2952 | ||
2953 | if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) { | |
2954 | name = strdup(pw->pw_name); | |
2955 | free(buf); | |
2956 | return name; | |
2957 | } | |
2958 | ||
2959 | free(buf); | |
2960 | ||
2961 | if (asprintf(&name, "%lu", (unsigned long) uid) < 0) | |
2962 | return NULL; | |
2963 | ||
2964 | return name; | |
2965 | } | |
2966 | ||
fc116c6a LP |
2967 | int getttyname_malloc(int fd, char **r) { |
2968 | char path[PATH_MAX], *c; | |
618e02c7 | 2969 | int k; |
8c6db833 LP |
2970 | |
2971 | assert(r); | |
ef2f1067 | 2972 | |
fc116c6a | 2973 | if ((k = ttyname_r(fd, path, sizeof(path))) != 0) |
618e02c7 | 2974 | return -k; |
ef2f1067 LP |
2975 | |
2976 | char_array_0(path); | |
2977 | ||
fc116c6a | 2978 | if (!(c = strdup(startswith(path, "/dev/") ? path + 5 : path))) |
8c6db833 LP |
2979 | return -ENOMEM; |
2980 | ||
2981 | *r = c; | |
2982 | return 0; | |
2983 | } | |
2984 | ||
fc116c6a LP |
2985 | int getttyname_harder(int fd, char **r) { |
2986 | int k; | |
2987 | char *s; | |
2988 | ||
2989 | if ((k = getttyname_malloc(fd, &s)) < 0) | |
2990 | return k; | |
2991 | ||
2992 | if (streq(s, "tty")) { | |
2993 | free(s); | |
46824d0e | 2994 | return get_ctty(r, NULL); |
fc116c6a LP |
2995 | } |
2996 | ||
2997 | *r = s; | |
2998 | return 0; | |
2999 | } | |
3000 | ||
3001 | int get_ctty_devnr(dev_t *d) { | |
3002 | int k; | |
20c03b7b | 3003 | char line[LINE_MAX], *p; |
fc116c6a LP |
3004 | unsigned long ttynr; |
3005 | FILE *f; | |
3006 | ||
3007 | if (!(f = fopen("/proc/self/stat", "r"))) | |
3008 | return -errno; | |
3009 | ||
3010 | if (!(fgets(line, sizeof(line), f))) { | |
3011 | k = -errno; | |
3012 | fclose(f); | |
3013 | return k; | |
3014 | } | |
3015 | ||
3016 | fclose(f); | |
3017 | ||
3018 | if (!(p = strrchr(line, ')'))) | |
3019 | return -EIO; | |
3020 | ||
3021 | p++; | |
3022 | ||
3023 | if (sscanf(p, " " | |
3024 | "%*c " /* state */ | |
3025 | "%*d " /* ppid */ | |
3026 | "%*d " /* pgrp */ | |
3027 | "%*d " /* session */ | |
3028 | "%lu ", /* ttynr */ | |
3029 | &ttynr) != 1) | |
3030 | return -EIO; | |
3031 | ||
3032 | *d = (dev_t) ttynr; | |
3033 | return 0; | |
3034 | } | |
3035 | ||
46824d0e | 3036 | int get_ctty(char **r, dev_t *_devnr) { |
fc116c6a | 3037 | int k; |
20c03b7b | 3038 | char fn[PATH_MAX], *s, *b, *p; |
fc116c6a LP |
3039 | dev_t devnr; |
3040 | ||
3041 | assert(r); | |
3042 | ||
3043 | if ((k = get_ctty_devnr(&devnr)) < 0) | |
3044 | return k; | |
3045 | ||
3046 | snprintf(fn, sizeof(fn), "/dev/char/%u:%u", major(devnr), minor(devnr)); | |
3047 | char_array_0(fn); | |
3048 | ||
3049 | if ((k = readlink_malloc(fn, &s)) < 0) { | |
3050 | ||
3051 | if (k != -ENOENT) | |
3052 | return k; | |
3053 | ||
46824d0e LP |
3054 | /* This is an ugly hack */ |
3055 | if (major(devnr) == 136) { | |
3056 | if (asprintf(&b, "pts/%lu", (unsigned long) minor(devnr)) < 0) | |
3057 | return -ENOMEM; | |
3058 | ||
3059 | *r = b; | |
3060 | if (_devnr) | |
3061 | *_devnr = devnr; | |
3062 | ||
3063 | return 0; | |
3064 | } | |
3065 | ||
fc116c6a LP |
3066 | /* Probably something like the ptys which have no |
3067 | * symlink in /dev/char. Let's return something | |
3068 | * vaguely useful. */ | |
3069 | ||
3070 | if (!(b = strdup(fn + 5))) | |
3071 | return -ENOMEM; | |
3072 | ||
3073 | *r = b; | |
46824d0e LP |
3074 | if (_devnr) |
3075 | *_devnr = devnr; | |
3076 | ||
fc116c6a LP |
3077 | return 0; |
3078 | } | |
3079 | ||
3080 | if (startswith(s, "/dev/")) | |
3081 | p = s + 5; | |
3082 | else if (startswith(s, "../")) | |
3083 | p = s + 3; | |
3084 | else | |
3085 | p = s; | |
3086 | ||
3087 | b = strdup(p); | |
3088 | free(s); | |
3089 | ||
3090 | if (!b) | |
3091 | return -ENOMEM; | |
3092 | ||
3093 | *r = b; | |
46824d0e LP |
3094 | if (_devnr) |
3095 | *_devnr = devnr; | |
3096 | ||
fc116c6a LP |
3097 | return 0; |
3098 | } | |
3099 | ||
8c6db833 LP |
3100 | static int rm_rf_children(int fd, bool only_dirs) { |
3101 | DIR *d; | |
3102 | int ret = 0; | |
3103 | ||
3104 | assert(fd >= 0); | |
3105 | ||
3106 | /* This returns the first error we run into, but nevertheless | |
3107 | * tries to go on */ | |
3108 | ||
3109 | if (!(d = fdopendir(fd))) { | |
3110 | close_nointr_nofail(fd); | |
4c633005 LP |
3111 | |
3112 | return errno == ENOENT ? 0 : -errno; | |
8c6db833 LP |
3113 | } |
3114 | ||
3115 | for (;;) { | |
3116 | struct dirent buf, *de; | |
3117 | bool is_dir; | |
3118 | int r; | |
3119 | ||
3120 | if ((r = readdir_r(d, &buf, &de)) != 0) { | |
3121 | if (ret == 0) | |
3122 | ret = -r; | |
3123 | break; | |
3124 | } | |
3125 | ||
3126 | if (!de) | |
3127 | break; | |
3128 | ||
3129 | if (streq(de->d_name, ".") || streq(de->d_name, "..")) | |
3130 | continue; | |
3131 | ||
3132 | if (de->d_type == DT_UNKNOWN) { | |
3133 | struct stat st; | |
3134 | ||
3135 | if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { | |
4c633005 | 3136 | if (ret == 0 && errno != ENOENT) |
8c6db833 LP |
3137 | ret = -errno; |
3138 | continue; | |
3139 | } | |
3140 | ||
3141 | is_dir = S_ISDIR(st.st_mode); | |
3142 | } else | |
3143 | is_dir = de->d_type == DT_DIR; | |
3144 | ||
3145 | if (is_dir) { | |
3146 | int subdir_fd; | |
3147 | ||
3148 | if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) { | |
4c633005 | 3149 | if (ret == 0 && errno != ENOENT) |
8c6db833 LP |
3150 | ret = -errno; |
3151 | continue; | |
3152 | } | |
3153 | ||
3154 | if ((r = rm_rf_children(subdir_fd, only_dirs)) < 0) { | |
3155 | if (ret == 0) | |
3156 | ret = r; | |
3157 | } | |
3158 | ||
3159 | if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) { | |
4c633005 | 3160 | if (ret == 0 && errno != ENOENT) |
8c6db833 LP |
3161 | ret = -errno; |
3162 | } | |
3163 | } else if (!only_dirs) { | |
3164 | ||
3165 | if (unlinkat(fd, de->d_name, 0) < 0) { | |
4c633005 | 3166 | if (ret == 0 && errno != ENOENT) |
8c6db833 LP |
3167 | ret = -errno; |
3168 | } | |
3169 | } | |
3170 | } | |
3171 | ||
3172 | closedir(d); | |
3173 | ||
3174 | return ret; | |
3175 | } | |
3176 | ||
3177 | int rm_rf(const char *path, bool only_dirs, bool delete_root) { | |
3178 | int fd; | |
3179 | int r; | |
3180 | ||
3181 | assert(path); | |
3182 | ||
3183 | if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) { | |
3184 | ||
3185 | if (errno != ENOTDIR) | |
3186 | return -errno; | |
3187 | ||
3188 | if (delete_root && !only_dirs) | |
3189 | if (unlink(path) < 0) | |
3190 | return -errno; | |
3191 | ||
3192 | return 0; | |
3193 | } | |
3194 | ||
3195 | r = rm_rf_children(fd, only_dirs); | |
3196 | ||
3197 | if (delete_root) | |
3198 | if (rmdir(path) < 0) { | |
3199 | if (r == 0) | |
3200 | r = -errno; | |
3201 | } | |
3202 | ||
3203 | return r; | |
3204 | } | |
3205 | ||
3206 | int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { | |
3207 | assert(path); | |
3208 | ||
3209 | /* Under the assumption that we are running privileged we | |
3210 | * first change the access mode and only then hand out | |
3211 | * ownership to avoid a window where access is too open. */ | |
3212 | ||
3213 | if (chmod(path, mode) < 0) | |
3214 | return -errno; | |
3215 | ||
3216 | if (chown(path, uid, gid) < 0) | |
3217 | return -errno; | |
3218 | ||
3219 | return 0; | |
ef2f1067 LP |
3220 | } |
3221 | ||
82c121a4 LP |
3222 | cpu_set_t* cpu_set_malloc(unsigned *ncpus) { |
3223 | cpu_set_t *r; | |
3224 | unsigned n = 1024; | |
3225 | ||
3226 | /* Allocates the cpuset in the right size */ | |
3227 | ||
3228 | for (;;) { | |
3229 | if (!(r = CPU_ALLOC(n))) | |
3230 | return NULL; | |
3231 | ||
3232 | if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) { | |
3233 | CPU_ZERO_S(CPU_ALLOC_SIZE(n), r); | |
3234 | ||
3235 | if (ncpus) | |
3236 | *ncpus = n; | |
3237 | ||
3238 | return r; | |
3239 | } | |
3240 | ||
3241 | CPU_FREE(r); | |
3242 | ||
3243 | if (errno != EINVAL) | |
3244 | return NULL; | |
3245 | ||
3246 | n *= 2; | |
3247 | } | |
3248 | } | |
3249 | ||
9e58ff9c LP |
3250 | void status_vprintf(const char *format, va_list ap) { |
3251 | char *s = NULL; | |
3252 | int fd = -1; | |
3253 | ||
3254 | assert(format); | |
3255 | ||
3256 | /* This independent of logging, as status messages are | |
3257 | * optional and go exclusively to the console. */ | |
3258 | ||
3259 | if (vasprintf(&s, format, ap) < 0) | |
3260 | goto finish; | |
3261 | ||
3262 | if ((fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) | |
3263 | goto finish; | |
3264 | ||
3265 | write(fd, s, strlen(s)); | |
3266 | ||
3267 | finish: | |
3268 | free(s); | |
3269 | ||
3270 | if (fd >= 0) | |
3271 | close_nointr_nofail(fd); | |
3272 | } | |
3273 | ||
c846ff47 LP |
3274 | void status_printf(const char *format, ...) { |
3275 | va_list ap; | |
3276 | ||
3277 | assert(format); | |
3278 | ||
3279 | va_start(ap, format); | |
3280 | status_vprintf(format, ap); | |
3281 | va_end(ap); | |
3282 | } | |
3283 | ||
3284 | void status_welcome(void) { | |
10aa7034 LP |
3285 | char *pretty_name = NULL, *ansi_color = NULL; |
3286 | const char *const_pretty = NULL, *const_color = NULL; | |
3287 | int r; | |
c846ff47 | 3288 | |
10aa7034 LP |
3289 | if ((r = parse_env_file("/etc/os-release", NEWLINE, |
3290 | "PRETTY_NAME", &pretty_name, | |
3291 | "ANSI_COLOR", &ansi_color, | |
3292 | NULL)) < 0) { | |
c846ff47 | 3293 | |
10aa7034 LP |
3294 | if (r != -ENOENT) |
3295 | log_warning("Failed to read /etc/os-release: %s", strerror(-r)); | |
3296 | } | |
c846ff47 | 3297 | |
10aa7034 LP |
3298 | #if defined(TARGET_FEDORA) |
3299 | if (!pretty_name) { | |
3300 | if ((r = read_one_line_file("/etc/system-release", &pretty_name)) < 0) { | |
c846ff47 | 3301 | |
10aa7034 LP |
3302 | if (r != -ENOENT) |
3303 | log_warning("Failed to read /etc/system-release: %s", strerror(-r)); | |
6f9a471a | 3304 | } |
10aa7034 | 3305 | } |
c846ff47 | 3306 | |
10aa7034 | 3307 | if (!ansi_color && pretty_name) { |
c846ff47 | 3308 | |
10aa7034 LP |
3309 | /* This tries to mimic the color magic the old Red Hat sysinit |
3310 | * script did. */ | |
3311 | ||
3312 | if (startswith(pretty_name, "Red Hat")) | |
3313 | const_color = "0;31"; /* Red for RHEL */ | |
3314 | else if (startswith(pretty_name, "Fedora")) | |
3315 | const_color = "0;34"; /* Blue for Fedora */ | |
3316 | } | |
c846ff47 LP |
3317 | |
3318 | #elif defined(TARGET_SUSE) | |
c846ff47 | 3319 | |
10aa7034 LP |
3320 | if (!pretty_name) { |
3321 | if ((r = read_one_line_file("/etc/SuSE-release", &pretty_name)) < 0) { | |
c846ff47 | 3322 | |
10aa7034 LP |
3323 | if (r != -ENOENT) |
3324 | log_warning("Failed to read /etc/SuSE-release: %s", strerror(-r)); | |
6f9a471a | 3325 | } |
10aa7034 | 3326 | } |
c846ff47 | 3327 | |
10aa7034 LP |
3328 | if (!ansi_color) |
3329 | const_color = "0;32"; /* Green for openSUSE */ | |
5a6225fd | 3330 | |
0d37b36b | 3331 | #elif defined(TARGET_GENTOO) |
0d37b36b | 3332 | |
10aa7034 LP |
3333 | if (!pretty_name) { |
3334 | if ((r = read_one_line_file("/etc/gentoo-release", &pretty_name)) < 0) { | |
0d37b36b | 3335 | |
10aa7034 LP |
3336 | if (r != -ENOENT) |
3337 | log_warning("Failed to read /etc/gentoo-release: %s", strerror(-r)); | |
6f9a471a | 3338 | } |
10aa7034 | 3339 | } |
0d37b36b | 3340 | |
10aa7034 LP |
3341 | if (!ansi_color) |
3342 | const_color = "1;34"; /* Light Blue for Gentoo */ | |
5a6225fd | 3343 | |
a338bab5 AS |
3344 | #elif defined(TARGET_ALTLINUX) |
3345 | ||
3346 | if (!pretty_name) { | |
3347 | if ((r = read_one_line_file("/etc/altlinux-release", &pretty_name)) < 0) { | |
3348 | ||
3349 | if (r != -ENOENT) | |
3350 | log_warning("Failed to read /etc/altlinux-release: %s", strerror(-r)); | |
6f9a471a | 3351 | } |
a338bab5 AS |
3352 | } |
3353 | ||
3354 | if (!ansi_color) | |
3355 | const_color = "0;36"; /* Cyan for ALTLinux */ | |
3356 | ||
3357 | ||
5a6225fd | 3358 | #elif defined(TARGET_DEBIAN) |
5a6225fd | 3359 | |
10aa7034 | 3360 | if (!pretty_name) { |
22927a36 | 3361 | char *version; |
c8bffa43 | 3362 | |
22927a36 | 3363 | if ((r = read_one_line_file("/etc/debian_version", &version)) < 0) { |
5a6225fd | 3364 | |
10aa7034 LP |
3365 | if (r != -ENOENT) |
3366 | log_warning("Failed to read /etc/debian_version: %s", strerror(-r)); | |
22927a36 | 3367 | } else { |
22927a36 MB |
3368 | pretty_name = strappend("Debian ", version); |
3369 | free(version); | |
c8bffa43 LP |
3370 | |
3371 | if (!pretty_name) | |
3372 | log_warning("Failed to allocate Debian version string."); | |
22927a36 | 3373 | } |
10aa7034 | 3374 | } |
5a6225fd | 3375 | |
10aa7034 LP |
3376 | if (!ansi_color) |
3377 | const_color = "1;31"; /* Light Red for Debian */ | |
5a6225fd | 3378 | |
274914f9 | 3379 | #elif defined(TARGET_UBUNTU) |
10aa7034 LP |
3380 | |
3381 | if ((r = parse_env_file("/etc/lsb-release", NEWLINE, | |
3382 | "DISTRIB_DESCRIPTION", &pretty_name, | |
3383 | NULL)) < 0) { | |
3384 | ||
3385 | if (r != -ENOENT) | |
3386 | log_warning("Failed to read /etc/lsb-release: %s", strerror(-r)); | |
3387 | } | |
3388 | ||
3389 | if (!ansi_color) | |
3390 | const_color = "0;33"; /* Orange/Brown for Ubuntu */ | |
3391 | ||
1de4d79b AB |
3392 | #elif defined(TARGET_MANDRIVA) |
3393 | ||
3394 | if (!pretty_name) { | |
3395 | char *s, *p; | |
3396 | ||
3397 | if ((r = read_one_line_file("/etc/mandriva-release", &s) < 0)) { | |
3398 | if (r != -ENOENT) | |
3399 | log_warning("Failed to read /etc/mandriva-release: %s", strerror(-r)); | |
3400 | } else { | |
3401 | p = strstr(s, " release "); | |
3402 | if (p) { | |
3403 | *p = '\0'; | |
3404 | p += 9; | |
3405 | p[strcspn(p, " ")] = '\0'; | |
3406 | ||
3407 | /* This corresponds to standard rc.sysinit */ | |
3408 | if (asprintf(&pretty_name, "%s\x1B[0;39m %s", s, p) > 0) | |
3409 | const_color = "1;36"; | |
3410 | else | |
3411 | log_warning("Failed to allocate Mandriva version string."); | |
3412 | } else | |
3413 | log_warning("Failed to parse /etc/mandriva-release"); | |
3414 | free(s); | |
3415 | } | |
3416 | } | |
54e4fdef | 3417 | #elif defined(TARGET_MEEGO) |
1de4d79b | 3418 | |
54e4fdef CF |
3419 | if (!pretty_name) { |
3420 | if ((r = read_one_line_file("/etc/meego-release", &pretty_name)) < 0) { | |
3421 | ||
3422 | if (r != -ENOENT) | |
3423 | log_warning("Failed to read /etc/meego-release: %s", strerror(-r)); | |
3424 | } | |
3425 | } | |
3426 | ||
3427 | if (!ansi_color) | |
3428 | const_color = "1;35"; /* Bright Magenta for MeeGo */ | |
c846ff47 | 3429 | #endif |
10aa7034 LP |
3430 | |
3431 | if (!pretty_name && !const_pretty) | |
3432 | const_pretty = "Linux"; | |
3433 | ||
3434 | if (!ansi_color && !const_color) | |
3435 | const_color = "1"; | |
3436 | ||
da71f23c | 3437 | status_printf("\nWelcome to \x1B[%sm%s\x1B[0m!\n\n", |
10aa7034 LP |
3438 | const_color ? const_color : ansi_color, |
3439 | const_pretty ? const_pretty : pretty_name); | |
86a3475b LP |
3440 | |
3441 | free(ansi_color); | |
3442 | free(pretty_name); | |
c846ff47 LP |
3443 | } |
3444 | ||
fab56fc5 LP |
3445 | char *replace_env(const char *format, char **env) { |
3446 | enum { | |
3447 | WORD, | |
c24eb49e | 3448 | CURLY, |
fab56fc5 LP |
3449 | VARIABLE |
3450 | } state = WORD; | |
3451 | ||
3452 | const char *e, *word = format; | |
3453 | char *r = NULL, *k; | |
3454 | ||
3455 | assert(format); | |
3456 | ||
3457 | for (e = format; *e; e ++) { | |
3458 | ||
3459 | switch (state) { | |
3460 | ||
3461 | case WORD: | |
3462 | if (*e == '$') | |
c24eb49e | 3463 | state = CURLY; |
fab56fc5 LP |
3464 | break; |
3465 | ||
c24eb49e LP |
3466 | case CURLY: |
3467 | if (*e == '{') { | |
fab56fc5 LP |
3468 | if (!(k = strnappend(r, word, e-word-1))) |
3469 | goto fail; | |
3470 | ||
3471 | free(r); | |
3472 | r = k; | |
3473 | ||
3474 | word = e-1; | |
3475 | state = VARIABLE; | |
3476 | ||
3477 | } else if (*e == '$') { | |
3478 | if (!(k = strnappend(r, word, e-word))) | |
3479 | goto fail; | |
3480 | ||
3481 | free(r); | |
3482 | r = k; | |
3483 | ||
3484 | word = e+1; | |
3485 | state = WORD; | |
3486 | } else | |
3487 | state = WORD; | |
3488 | break; | |
3489 | ||
3490 | case VARIABLE: | |
c24eb49e | 3491 | if (*e == '}') { |
b95cf362 | 3492 | const char *t; |
fab56fc5 | 3493 | |
b95cf362 LP |
3494 | if (!(t = strv_env_get_with_length(env, word+2, e-word-2))) |
3495 | t = ""; | |
fab56fc5 | 3496 | |
b95cf362 LP |
3497 | if (!(k = strappend(r, t))) |
3498 | goto fail; | |
fab56fc5 | 3499 | |
b95cf362 LP |
3500 | free(r); |
3501 | r = k; | |
fab56fc5 | 3502 | |
b95cf362 | 3503 | word = e+1; |
fab56fc5 LP |
3504 | state = WORD; |
3505 | } | |
3506 | break; | |
3507 | } | |
3508 | } | |
3509 | ||
3510 | if (!(k = strnappend(r, word, e-word))) | |
3511 | goto fail; | |
3512 | ||
3513 | free(r); | |
3514 | return k; | |
3515 | ||
3516 | fail: | |
3517 | free(r); | |
3518 | return NULL; | |
3519 | } | |
3520 | ||
3521 | char **replace_env_argv(char **argv, char **env) { | |
3522 | char **r, **i; | |
c24eb49e LP |
3523 | unsigned k = 0, l = 0; |
3524 | ||
3525 | l = strv_length(argv); | |
fab56fc5 | 3526 | |
c24eb49e | 3527 | if (!(r = new(char*, l+1))) |
fab56fc5 LP |
3528 | return NULL; |
3529 | ||
3530 | STRV_FOREACH(i, argv) { | |
c24eb49e LP |
3531 | |
3532 | /* If $FOO appears as single word, replace it by the split up variable */ | |
b95cf362 LP |
3533 | if ((*i)[0] == '$' && (*i)[1] != '{') { |
3534 | char *e; | |
3535 | char **w, **m; | |
3536 | unsigned q; | |
c24eb49e | 3537 | |
b95cf362 | 3538 | if ((e = strv_env_get(env, *i+1))) { |
c24eb49e LP |
3539 | |
3540 | if (!(m = strv_split_quoted(e))) { | |
3541 | r[k] = NULL; | |
3542 | strv_free(r); | |
3543 | return NULL; | |
3544 | } | |
b95cf362 LP |
3545 | } else |
3546 | m = NULL; | |
c24eb49e | 3547 | |
b95cf362 LP |
3548 | q = strv_length(m); |
3549 | l = l + q - 1; | |
c24eb49e | 3550 | |
b95cf362 LP |
3551 | if (!(w = realloc(r, sizeof(char*) * (l+1)))) { |
3552 | r[k] = NULL; | |
3553 | strv_free(r); | |
3554 | strv_free(m); | |
3555 | return NULL; | |
3556 | } | |
c24eb49e | 3557 | |
b95cf362 LP |
3558 | r = w; |
3559 | if (m) { | |
c24eb49e LP |
3560 | memcpy(r + k, m, q * sizeof(char*)); |
3561 | free(m); | |
c24eb49e | 3562 | } |
b95cf362 LP |
3563 | |
3564 | k += q; | |
3565 | continue; | |
c24eb49e LP |
3566 | } |
3567 | ||
3568 | /* If ${FOO} appears as part of a word, replace it by the variable as-is */ | |
fab56fc5 LP |
3569 | if (!(r[k++] = replace_env(*i, env))) { |
3570 | strv_free(r); | |
3571 | return NULL; | |
3572 | } | |
3573 | } | |
3574 | ||
3575 | r[k] = NULL; | |
3576 | return r; | |
3577 | } | |
3578 | ||
fa776d8e LP |
3579 | int columns(void) { |
3580 | static __thread int parsed_columns = 0; | |
3581 | const char *e; | |
3582 | ||
3583 | if (parsed_columns > 0) | |
3584 | return parsed_columns; | |
3585 | ||
3586 | if ((e = getenv("COLUMNS"))) | |
3587 | parsed_columns = atoi(e); | |
3588 | ||
3589 | if (parsed_columns <= 0) { | |
3590 | struct winsize ws; | |
3591 | zero(ws); | |
3592 | ||
9ed95f43 | 3593 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0) |
fa776d8e LP |
3594 | parsed_columns = ws.ws_col; |
3595 | } | |
3596 | ||
3597 | if (parsed_columns <= 0) | |
3598 | parsed_columns = 80; | |
3599 | ||
3600 | return parsed_columns; | |
3601 | } | |
3602 | ||
b4f10a5e LP |
3603 | int running_in_chroot(void) { |
3604 | struct stat a, b; | |
3605 | ||
3606 | zero(a); | |
3607 | zero(b); | |
3608 | ||
3609 | /* Only works as root */ | |
3610 | ||
3611 | if (stat("/proc/1/root", &a) < 0) | |
3612 | return -errno; | |
3613 | ||
3614 | if (stat("/", &b) < 0) | |
3615 | return -errno; | |
3616 | ||
3617 | return | |
3618 | a.st_dev != b.st_dev || | |
3619 | a.st_ino != b.st_ino; | |
3620 | } | |
3621 | ||
8fe914ec LP |
3622 | char *ellipsize(const char *s, unsigned length, unsigned percent) { |
3623 | size_t l, x; | |
3624 | char *r; | |
3625 | ||
3626 | assert(s); | |
3627 | assert(percent <= 100); | |
3628 | assert(length >= 3); | |
3629 | ||
3630 | l = strlen(s); | |
3631 | ||
3632 | if (l <= 3 || l <= length) | |
3633 | return strdup(s); | |
3634 | ||
3635 | if (!(r = new0(char, length+1))) | |
3636 | return r; | |
3637 | ||
3638 | x = (length * percent) / 100; | |
3639 | ||
3640 | if (x > length - 3) | |
3641 | x = length - 3; | |
3642 | ||
3643 | memcpy(r, s, x); | |
3644 | r[x] = '.'; | |
3645 | r[x+1] = '.'; | |
3646 | r[x+2] = '.'; | |
3647 | memcpy(r + x + 3, | |
3648 | s + l - (length - x - 3), | |
3649 | length - x - 3); | |
3650 | ||
3651 | return r; | |
3652 | } | |
3653 | ||
f6144808 LP |
3654 | int touch(const char *path) { |
3655 | int fd; | |
3656 | ||
3657 | assert(path); | |
3658 | ||
14f3c825 | 3659 | if ((fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644)) < 0) |
f6144808 LP |
3660 | return -errno; |
3661 | ||
3662 | close_nointr_nofail(fd); | |
3663 | return 0; | |
3664 | } | |
afea26ad | 3665 | |
97c4a07d | 3666 | char *unquote(const char *s, const char* quotes) { |
11ce3427 LP |
3667 | size_t l; |
3668 | assert(s); | |
3669 | ||
3670 | if ((l = strlen(s)) < 2) | |
3671 | return strdup(s); | |
3672 | ||
97c4a07d | 3673 | if (strchr(quotes, s[0]) && s[l-1] == s[0]) |
11ce3427 LP |
3674 | return strndup(s+1, l-2); |
3675 | ||
3676 | return strdup(s); | |
3677 | } | |
3678 | ||
5f7c426e LP |
3679 | char *normalize_env_assignment(const char *s) { |
3680 | char *name, *value, *p, *r; | |
3681 | ||
3682 | p = strchr(s, '='); | |
3683 | ||
3684 | if (!p) { | |
3685 | if (!(r = strdup(s))) | |
3686 | return NULL; | |
3687 | ||
3688 | return strstrip(r); | |
3689 | } | |
3690 | ||
3691 | if (!(name = strndup(s, p - s))) | |
3692 | return NULL; | |
3693 | ||
3694 | if (!(p = strdup(p+1))) { | |
3695 | free(name); | |
3696 | return NULL; | |
3697 | } | |
3698 | ||
3699 | value = unquote(strstrip(p), QUOTES); | |
3700 | free(p); | |
3701 | ||
3702 | if (!value) { | |
5f7c426e LP |
3703 | free(name); |
3704 | return NULL; | |
3705 | } | |
3706 | ||
3707 | if (asprintf(&r, "%s=%s", name, value) < 0) | |
3708 | r = NULL; | |
3709 | ||
3710 | free(value); | |
3711 | free(name); | |
3712 | ||
3713 | return r; | |
3714 | } | |
3715 | ||
8e12a6ae | 3716 | int wait_for_terminate(pid_t pid, siginfo_t *status) { |
2e78aa99 LP |
3717 | assert(pid >= 1); |
3718 | assert(status); | |
3719 | ||
3720 | for (;;) { | |
8e12a6ae LP |
3721 | zero(*status); |
3722 | ||
3723 | if (waitid(P_PID, pid, status, WEXITED) < 0) { | |
2e78aa99 LP |
3724 | |
3725 | if (errno == EINTR) | |
3726 | continue; | |
3727 | ||
3728 | return -errno; | |
3729 | } | |
3730 | ||
3731 | return 0; | |
3732 | } | |
3733 | } | |
3734 | ||
97c4a07d LP |
3735 | int wait_for_terminate_and_warn(const char *name, pid_t pid) { |
3736 | int r; | |
3737 | siginfo_t status; | |
3738 | ||
3739 | assert(name); | |
3740 | assert(pid > 1); | |
3741 | ||
3742 | if ((r = wait_for_terminate(pid, &status)) < 0) { | |
3743 | log_warning("Failed to wait for %s: %s", name, strerror(-r)); | |
3744 | return r; | |
3745 | } | |
3746 | ||
3747 | if (status.si_code == CLD_EXITED) { | |
3748 | if (status.si_status != 0) { | |
3749 | log_warning("%s failed with error code %i.", name, status.si_status); | |
0a27cf3f | 3750 | return status.si_status; |
97c4a07d LP |
3751 | } |
3752 | ||
3753 | log_debug("%s succeeded.", name); | |
3754 | return 0; | |
3755 | ||
3756 | } else if (status.si_code == CLD_KILLED || | |
3757 | status.si_code == CLD_DUMPED) { | |
3758 | ||
3759 | log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status)); | |
3760 | return -EPROTO; | |
3761 | } | |
3762 | ||
3763 | log_warning("%s failed due to unknown reason.", name); | |
3764 | return -EPROTO; | |
3765 | ||
3766 | } | |
3767 | ||
3c14d26c | 3768 | void freeze(void) { |
720ce21d LP |
3769 | |
3770 | /* Make sure nobody waits for us on a socket anymore */ | |
3771 | close_all_fds(NULL, 0); | |
3772 | ||
c29597a1 LP |
3773 | sync(); |
3774 | ||
3c14d26c LP |
3775 | for (;;) |
3776 | pause(); | |
3777 | } | |
3778 | ||
00dc5d76 LP |
3779 | bool null_or_empty(struct stat *st) { |
3780 | assert(st); | |
3781 | ||
3782 | if (S_ISREG(st->st_mode) && st->st_size <= 0) | |
3783 | return true; | |
3784 | ||
c8f26f42 | 3785 | if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) |
00dc5d76 LP |
3786 | return true; |
3787 | ||
3788 | return false; | |
3789 | } | |
3790 | ||
a247755d | 3791 | DIR *xopendirat(int fd, const char *name, int flags) { |
c4731d11 LP |
3792 | int nfd; |
3793 | DIR *d; | |
3794 | ||
3795 | if ((nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags)) < 0) | |
3796 | return NULL; | |
3797 | ||
3798 | if (!(d = fdopendir(nfd))) { | |
3799 | close_nointr_nofail(nfd); | |
3800 | return NULL; | |
3801 | } | |
3802 | ||
3803 | return d; | |
3b63d2d3 LP |
3804 | } |
3805 | ||
8a0867d6 LP |
3806 | int signal_from_string_try_harder(const char *s) { |
3807 | int signo; | |
3808 | assert(s); | |
3809 | ||
3810 | if ((signo = signal_from_string(s)) <= 0) | |
3811 | if (startswith(s, "SIG")) | |
3812 | return signal_from_string(s+3); | |
3813 | ||
3814 | return signo; | |
3815 | } | |
3816 | ||
10717a1a LP |
3817 | void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) { |
3818 | ||
3819 | assert(f); | |
3820 | assert(name); | |
3821 | assert(t); | |
3822 | ||
3823 | if (!dual_timestamp_is_set(t)) | |
3824 | return; | |
3825 | ||
3826 | fprintf(f, "%s=%llu %llu\n", | |
3827 | name, | |
3828 | (unsigned long long) t->realtime, | |
3829 | (unsigned long long) t->monotonic); | |
3830 | } | |
3831 | ||
799fd0fd | 3832 | void dual_timestamp_deserialize(const char *value, dual_timestamp *t) { |
10717a1a LP |
3833 | unsigned long long a, b; |
3834 | ||
10717a1a LP |
3835 | assert(value); |
3836 | assert(t); | |
3837 | ||
3838 | if (sscanf(value, "%lli %llu", &a, &b) != 2) | |
3839 | log_debug("Failed to parse finish timestamp value %s", value); | |
3840 | else { | |
3841 | t->realtime = a; | |
3842 | t->monotonic = b; | |
3843 | } | |
3844 | } | |
3845 | ||
e23a0ce8 LP |
3846 | char *fstab_node_to_udev_node(const char *p) { |
3847 | char *dn, *t, *u; | |
3848 | int r; | |
3849 | ||
3850 | /* FIXME: to follow udev's logic 100% we need to leave valid | |
3851 | * UTF8 chars unescaped */ | |
3852 | ||
3853 | if (startswith(p, "LABEL=")) { | |
3854 | ||
3855 | if (!(u = unquote(p+6, "\"\'"))) | |
3856 | return NULL; | |
3857 | ||
3858 | t = xescape(u, "/ "); | |
3859 | free(u); | |
3860 | ||
3861 | if (!t) | |
3862 | return NULL; | |
3863 | ||
3864 | r = asprintf(&dn, "/dev/disk/by-label/%s", t); | |
3865 | free(t); | |
3866 | ||
3867 | if (r < 0) | |
3868 | return NULL; | |
3869 | ||
3870 | return dn; | |
3871 | } | |
3872 | ||
3873 | if (startswith(p, "UUID=")) { | |
3874 | ||
3875 | if (!(u = unquote(p+5, "\"\'"))) | |
3876 | return NULL; | |
3877 | ||
3878 | t = xescape(u, "/ "); | |
3879 | free(u); | |
3880 | ||
3881 | if (!t) | |
3882 | return NULL; | |
3883 | ||
0058d7b9 | 3884 | r = asprintf(&dn, "/dev/disk/by-uuid/%s", t); |
e23a0ce8 LP |
3885 | free(t); |
3886 | ||
3887 | if (r < 0) | |
3888 | return NULL; | |
3889 | ||
3890 | return dn; | |
3891 | } | |
3892 | ||
3893 | return strdup(p); | |
3894 | } | |
3895 | ||
e9ddabc2 LP |
3896 | void filter_environ(const char *prefix) { |
3897 | int i, j; | |
3898 | assert(prefix); | |
3899 | ||
3900 | if (!environ) | |
3901 | return; | |
3902 | ||
3903 | for (i = 0, j = 0; environ[i]; i++) { | |
3904 | ||
3905 | if (startswith(environ[i], prefix)) | |
3906 | continue; | |
3907 | ||
3908 | environ[j++] = environ[i]; | |
3909 | } | |
3910 | ||
3911 | environ[j] = NULL; | |
3912 | } | |
3913 | ||
f212ac12 LP |
3914 | bool tty_is_vc(const char *tty) { |
3915 | assert(tty); | |
3916 | ||
3917 | if (startswith(tty, "/dev/")) | |
3918 | tty += 5; | |
3919 | ||
3920 | return startswith(tty, "tty") && | |
3921 | tty[3] >= '0' && tty[3] <= '9'; | |
3922 | } | |
3923 | ||
e3aa71c3 | 3924 | const char *default_term_for_tty(const char *tty) { |
3030ccd7 LP |
3925 | char *active = NULL; |
3926 | const char *term; | |
3927 | ||
e3aa71c3 LP |
3928 | assert(tty); |
3929 | ||
3930 | if (startswith(tty, "/dev/")) | |
3931 | tty += 5; | |
3932 | ||
3030ccd7 LP |
3933 | /* Resolve where /dev/console is pointing when determining |
3934 | * TERM */ | |
3935 | if (streq(tty, "console")) | |
3936 | if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) { | |
079a09fb LP |
3937 | /* If multiple log outputs are configured the |
3938 | * last one is what /dev/console points to */ | |
3939 | if ((tty = strrchr(active, ' '))) | |
3940 | tty++; | |
3941 | else | |
3942 | tty = active; | |
3030ccd7 LP |
3943 | } |
3944 | ||
f212ac12 | 3945 | term = tty_is_vc(tty) ? "TERM=linux" : "TERM=vt100"; |
3030ccd7 | 3946 | free(active); |
e3aa71c3 | 3947 | |
3030ccd7 | 3948 | return term; |
e3aa71c3 LP |
3949 | } |
3950 | ||
07faed4f LP |
3951 | /* Returns a short identifier for the various VM implementations */ |
3952 | int detect_vm(const char **id) { | |
46a08e38 LP |
3953 | |
3954 | #if defined(__i386__) || defined(__x86_64__) | |
3955 | ||
3956 | /* Both CPUID and DMI are x86 specific interfaces... */ | |
3957 | ||
721bca57 | 3958 | static const char *const dmi_vendors[] = { |
46a08e38 LP |
3959 | "/sys/class/dmi/id/sys_vendor", |
3960 | "/sys/class/dmi/id/board_vendor", | |
3961 | "/sys/class/dmi/id/bios_vendor" | |
3962 | }; | |
3963 | ||
4e08da90 | 3964 | static const char dmi_vendor_table[] = |
07faed4f LP |
3965 | "QEMU\0" "qemu\0" |
3966 | /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */ | |
3967 | "VMware\0" "vmware\0" | |
3968 | "VMW\0" "vmware\0" | |
3969 | "Microsoft Corporation\0" "microsoft\0" | |
3970 | "innotek GmbH\0" "oracle\0" | |
3971 | "Xen\0" "xen\0" | |
34df5a34 | 3972 | "Bochs\0" "bochs\0"; |
07faed4f | 3973 | |
4e08da90 | 3974 | static const char cpuid_vendor_table[] = |
07faed4f LP |
3975 | "XenVMMXenVMM\0" "xen\0" |
3976 | "KVMKVMKVM\0" "kvm\0" | |
3977 | /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */ | |
3978 | "VMwareVMware\0" "vmware\0" | |
3979 | /* http://msdn.microsoft.com/en-us/library/ff542428.aspx */ | |
34df5a34 | 3980 | "Microsoft Hv\0" "microsoft\0"; |
07faed4f LP |
3981 | |
3982 | uint32_t eax, ecx; | |
46a08e38 LP |
3983 | union { |
3984 | uint32_t sig32[3]; | |
3985 | char text[13]; | |
3986 | } sig; | |
46a08e38 | 3987 | unsigned i; |
07faed4f | 3988 | const char *j, *k; |
721bca57 | 3989 | bool hypervisor; |
46a08e38 LP |
3990 | |
3991 | /* http://lwn.net/Articles/301888/ */ | |
3992 | zero(sig); | |
3993 | ||
46a08e38 LP |
3994 | #if defined (__i386__) |
3995 | #define REG_a "eax" | |
3996 | #define REG_b "ebx" | |
3997 | #elif defined (__amd64__) | |
3998 | #define REG_a "rax" | |
3999 | #define REG_b "rbx" | |
4000 | #endif | |
4001 | ||
07faed4f LP |
4002 | /* First detect whether there is a hypervisor */ |
4003 | eax = 1; | |
46a08e38 LP |
4004 | __asm__ __volatile__ ( |
4005 | /* ebx/rbx is being used for PIC! */ | |
4006 | " push %%"REG_b" \n\t" | |
4007 | " cpuid \n\t" | |
46a08e38 LP |
4008 | " pop %%"REG_b" \n\t" |
4009 | ||
07faed4f | 4010 | : "=a" (eax), "=c" (ecx) |
46a08e38 LP |
4011 | : "0" (eax) |
4012 | ); | |
4013 | ||
721bca57 LP |
4014 | hypervisor = !!(ecx & ecx & 0x80000000U); |
4015 | ||
4016 | if (hypervisor) { | |
07faed4f LP |
4017 | |
4018 | /* There is a hypervisor, see what it is */ | |
4019 | eax = 0x40000000U; | |
4020 | __asm__ __volatile__ ( | |
4021 | /* ebx/rbx is being used for PIC! */ | |
4022 | " push %%"REG_b" \n\t" | |
4023 | " cpuid \n\t" | |
4024 | " mov %%ebx, %1 \n\t" | |
4025 | " pop %%"REG_b" \n\t" | |
4026 | ||
4027 | : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2]) | |
4028 | : "0" (eax) | |
4029 | ); | |
4030 | ||
4031 | NULSTR_FOREACH_PAIR(j, k, cpuid_vendor_table) | |
4032 | if (streq(sig.text, j)) { | |
4033 | ||
4034 | if (id) | |
4035 | *id = k; | |
4036 | ||
4037 | return 1; | |
4038 | } | |
721bca57 | 4039 | } |
07faed4f | 4040 | |
721bca57 LP |
4041 | for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) { |
4042 | char *s; | |
4043 | int r; | |
4044 | const char *found = NULL; | |
4045 | ||
4046 | if ((r = read_one_line_file(dmi_vendors[i], &s)) < 0) { | |
4047 | if (r != -ENOENT) | |
4048 | return r; | |
4049 | ||
4050 | continue; | |
4051 | } | |
4052 | ||
4053 | NULSTR_FOREACH_PAIR(j, k, dmi_vendor_table) | |
4054 | if (startswith(s, j)) | |
4055 | found = k; | |
4056 | free(s); | |
4057 | ||
4058 | if (found) { | |
4059 | if (id) | |
4060 | *id = found; | |
4061 | ||
4062 | return 1; | |
4063 | } | |
4064 | } | |
4065 | ||
4066 | if (hypervisor) { | |
07faed4f LP |
4067 | if (id) |
4068 | *id = "other"; | |
4069 | ||
4070 | return 1; | |
4071 | } | |
46a08e38 | 4072 | |
721bca57 | 4073 | #endif |
07faed4f LP |
4074 | return 0; |
4075 | } | |
4076 | ||
ef2df9f4 | 4077 | int detect_container(const char **id) { |
f9b9232b LP |
4078 | FILE *f; |
4079 | ||
ef2df9f4 LP |
4080 | /* Unfortunately many of these operations require root access |
4081 | * in one way or another */ | |
f9b9232b | 4082 | |
ef2df9f4 LP |
4083 | if (geteuid() != 0) |
4084 | return -EPERM; | |
4085 | ||
4086 | if (running_in_chroot() > 0) { | |
f9b9232b LP |
4087 | |
4088 | if (id) | |
ef2df9f4 | 4089 | *id = "chroot"; |
f9b9232b LP |
4090 | |
4091 | return 1; | |
4092 | } | |
07faed4f | 4093 | |
ef2df9f4 LP |
4094 | /* /proc/vz exists in container and outside of the container, |
4095 | * /proc/bc only outside of the container. */ | |
4096 | if (access("/proc/vz", F_OK) >= 0 && | |
4097 | access("/proc/bc", F_OK) < 0) { | |
07faed4f | 4098 | |
ef2df9f4 LP |
4099 | if (id) |
4100 | *id = "openvz"; | |
4101 | ||
4102 | return 1; | |
f9b9232b | 4103 | } |
07faed4f | 4104 | |
f9b9232b LP |
4105 | if ((f = fopen("/proc/self/cgroup", "r"))) { |
4106 | ||
4107 | for (;;) { | |
4108 | char line[LINE_MAX], *p; | |
4109 | ||
4110 | if (!fgets(line, sizeof(line), f)) | |
4111 | break; | |
4112 | ||
4113 | if (!(p = strchr(strstrip(line), ':'))) | |
4114 | continue; | |
4115 | ||
4116 | if (strncmp(p, ":ns:", 4)) | |
4117 | continue; | |
4118 | ||
4119 | if (!streq(p, ":ns:/")) { | |
4120 | fclose(f); | |
4121 | ||
ef2df9f4 | 4122 | if (id) |
28cf382a | 4123 | *id = "pidns"; |
ef2df9f4 LP |
4124 | |
4125 | return 1; | |
f9b9232b LP |
4126 | } |
4127 | } | |
4128 | ||
4129 | fclose(f); | |
07faed4f LP |
4130 | } |
4131 | ||
ef2df9f4 LP |
4132 | return 0; |
4133 | } | |
4134 | ||
4135 | /* Returns a short identifier for the various VM/container implementations */ | |
4136 | int detect_virtualization(const char **id) { | |
4137 | static __thread const char *cached_id = NULL; | |
4138 | const char *_id; | |
4139 | int r; | |
4140 | ||
4141 | if (cached_id) { | |
4142 | ||
4143 | if (cached_id == (const char*) -1) | |
4144 | return 0; | |
4145 | ||
4146 | if (id) | |
4147 | *id = cached_id; | |
4148 | ||
4149 | return 1; | |
f9b9232b | 4150 | } |
07faed4f | 4151 | |
ef2df9f4 LP |
4152 | if ((r = detect_container(&_id)) != 0) |
4153 | goto finish; | |
4154 | ||
f9b9232b | 4155 | r = detect_vm(&_id); |
07faed4f | 4156 | |
f9b9232b | 4157 | finish: |
ef2df9f4 | 4158 | if (r > 0) { |
f9b9232b | 4159 | cached_id = _id; |
07faed4f | 4160 | |
ef2df9f4 LP |
4161 | if (id) |
4162 | *id = _id; | |
4163 | } else if (r == 0) | |
4164 | cached_id = (const char*) -1; | |
f9b9232b LP |
4165 | |
4166 | return r; | |
46a08e38 LP |
4167 | } |
4168 | ||
83cc030f LP |
4169 | void execute_directory(const char *directory, DIR *d, char *argv[]) { |
4170 | DIR *_d = NULL; | |
4171 | struct dirent *de; | |
4172 | Hashmap *pids = NULL; | |
4173 | ||
4174 | assert(directory); | |
4175 | ||
4176 | /* Executes all binaries in a directory in parallel and waits | |
4177 | * until all they all finished. */ | |
4178 | ||
4179 | if (!d) { | |
4180 | if (!(_d = opendir(directory))) { | |
4181 | ||
4182 | if (errno == ENOENT) | |
4183 | return; | |
4184 | ||
4185 | log_error("Failed to enumerate directory %s: %m", directory); | |
4186 | return; | |
4187 | } | |
4188 | ||
4189 | d = _d; | |
4190 | } | |
4191 | ||
4192 | if (!(pids = hashmap_new(trivial_hash_func, trivial_compare_func))) { | |
4193 | log_error("Failed to allocate set."); | |
4194 | goto finish; | |
4195 | } | |
4196 | ||
4197 | while ((de = readdir(d))) { | |
4198 | char *path; | |
4199 | pid_t pid; | |
4200 | int k; | |
4201 | ||
4202 | if (ignore_file(de->d_name)) | |
4203 | continue; | |
4204 | ||
4205 | if (de->d_type != DT_REG && | |
4206 | de->d_type != DT_LNK && | |
4207 | de->d_type != DT_UNKNOWN) | |
4208 | continue; | |
4209 | ||
4210 | if (asprintf(&path, "%s/%s", directory, de->d_name) < 0) { | |
4211 | log_error("Out of memory"); | |
4212 | continue; | |
4213 | } | |
4214 | ||
4215 | if ((pid = fork()) < 0) { | |
4216 | log_error("Failed to fork: %m"); | |
4217 | free(path); | |
4218 | continue; | |
4219 | } | |
4220 | ||
4221 | if (pid == 0) { | |
4222 | char *_argv[2]; | |
4223 | /* Child */ | |
4224 | ||
4225 | if (!argv) { | |
4226 | _argv[0] = path; | |
4227 | _argv[1] = NULL; | |
4228 | argv = _argv; | |
4229 | } else | |
4230 | if (!argv[0]) | |
4231 | argv[0] = path; | |
4232 | ||
4233 | execv(path, argv); | |
4234 | ||
4235 | log_error("Failed to execute %s: %m", path); | |
4236 | _exit(EXIT_FAILURE); | |
4237 | } | |
4238 | ||
4239 | log_debug("Spawned %s as %lu", path, (unsigned long) pid); | |
4240 | ||
4241 | if ((k = hashmap_put(pids, UINT_TO_PTR(pid), path)) < 0) { | |
4242 | log_error("Failed to add PID to set: %s", strerror(-k)); | |
4243 | free(path); | |
4244 | } | |
4245 | } | |
4246 | ||
4247 | while (!hashmap_isempty(pids)) { | |
4248 | siginfo_t si; | |
4249 | char *path; | |
4250 | ||
4251 | zero(si); | |
4252 | if (waitid(P_ALL, 0, &si, WEXITED) < 0) { | |
4253 | ||
4254 | if (errno == EINTR) | |
4255 | continue; | |
4256 | ||
4257 | log_error("waitid() failed: %m"); | |
4258 | goto finish; | |
4259 | } | |
4260 | ||
4261 | if ((path = hashmap_remove(pids, UINT_TO_PTR(si.si_pid)))) { | |
4262 | if (!is_clean_exit(si.si_code, si.si_status)) { | |
4263 | if (si.si_code == CLD_EXITED) | |
4264 | log_error("%s exited with exit status %i.", path, si.si_status); | |
4265 | else | |
4266 | log_error("%s terminated by signal %s.", path, signal_to_string(si.si_status)); | |
4267 | } else | |
4268 | log_debug("%s exited successfully.", path); | |
4269 | ||
4270 | free(path); | |
4271 | } | |
4272 | } | |
4273 | ||
4274 | finish: | |
4275 | if (_d) | |
4276 | closedir(_d); | |
4277 | ||
4278 | if (pids) | |
4279 | hashmap_free_free(pids); | |
4280 | } | |
4281 | ||
430c18ed LP |
4282 | int kill_and_sigcont(pid_t pid, int sig) { |
4283 | int r; | |
4284 | ||
4285 | r = kill(pid, sig) < 0 ? -errno : 0; | |
4286 | ||
4287 | if (r >= 0) | |
4288 | kill(pid, SIGCONT); | |
4289 | ||
4290 | return r; | |
4291 | } | |
4292 | ||
05feefe0 LP |
4293 | bool nulstr_contains(const char*nulstr, const char *needle) { |
4294 | const char *i; | |
4295 | ||
4296 | if (!nulstr) | |
4297 | return false; | |
4298 | ||
4299 | NULSTR_FOREACH(i, nulstr) | |
4300 | if (streq(i, needle)) | |
4301 | return true; | |
4302 | ||
4303 | return false; | |
4304 | } | |
4305 | ||
6faa1114 | 4306 | bool plymouth_running(void) { |
9408a2d2 | 4307 | return access("/run/plymouth/pid", F_OK) >= 0; |
6faa1114 LP |
4308 | } |
4309 | ||
7c3b203c LP |
4310 | void parse_syslog_priority(char **p, int *priority) { |
4311 | int a = 0, b = 0, c = 0; | |
4312 | int k; | |
4313 | ||
4314 | assert(p); | |
4315 | assert(*p); | |
4316 | assert(priority); | |
4317 | ||
4318 | if ((*p)[0] != '<') | |
4319 | return; | |
4320 | ||
4321 | if (!strchr(*p, '>')) | |
4322 | return; | |
4323 | ||
4324 | if ((*p)[2] == '>') { | |
4325 | c = undecchar((*p)[1]); | |
4326 | k = 3; | |
4327 | } else if ((*p)[3] == '>') { | |
4328 | b = undecchar((*p)[1]); | |
4329 | c = undecchar((*p)[2]); | |
4330 | k = 4; | |
4331 | } else if ((*p)[4] == '>') { | |
4332 | a = undecchar((*p)[1]); | |
4333 | b = undecchar((*p)[2]); | |
4334 | c = undecchar((*p)[3]); | |
4335 | k = 5; | |
4336 | } else | |
4337 | return; | |
4338 | ||
4339 | if (a < 0 || b < 0 || c < 0) | |
4340 | return; | |
4341 | ||
4342 | *priority = a*100+b*10+c; | |
4343 | *p += k; | |
4344 | } | |
4345 | ||
ac123445 LP |
4346 | int have_effective_cap(int value) { |
4347 | cap_t cap; | |
4348 | cap_flag_value_t fv; | |
4349 | int r; | |
4350 | ||
4351 | if (!(cap = cap_get_proc())) | |
4352 | return -errno; | |
4353 | ||
4354 | if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0) | |
4355 | r = -errno; | |
4356 | else | |
4357 | r = fv == CAP_SET; | |
4358 | ||
4359 | cap_free(cap); | |
4360 | return r; | |
4361 | } | |
4362 | ||
9beb3f4d LP |
4363 | char* strshorten(char *s, size_t l) { |
4364 | assert(s); | |
4365 | ||
4366 | if (l < strlen(s)) | |
4367 | s[l] = 0; | |
4368 | ||
4369 | return s; | |
4370 | } | |
4371 | ||
4372 | static bool hostname_valid_char(char c) { | |
4373 | return | |
4374 | (c >= 'a' && c <= 'z') || | |
4375 | (c >= 'A' && c <= 'Z') || | |
4376 | (c >= '0' && c <= '9') || | |
4377 | c == '-' || | |
4378 | c == '_' || | |
4379 | c == '.'; | |
4380 | } | |
4381 | ||
4382 | bool hostname_is_valid(const char *s) { | |
4383 | const char *p; | |
4384 | ||
4385 | if (isempty(s)) | |
4386 | return false; | |
4387 | ||
4388 | for (p = s; *p; p++) | |
4389 | if (!hostname_valid_char(*p)) | |
4390 | return false; | |
4391 | ||
4392 | if (p-s > HOST_NAME_MAX) | |
4393 | return false; | |
4394 | ||
4395 | return true; | |
4396 | } | |
4397 | ||
4398 | char* hostname_cleanup(char *s) { | |
4399 | char *p, *d; | |
4400 | ||
4401 | for (p = s, d = s; *p; p++) | |
4402 | if ((*p >= 'a' && *p <= 'z') || | |
4403 | (*p >= 'A' && *p <= 'Z') || | |
4404 | (*p >= '0' && *p <= '9') || | |
4405 | *p == '-' || | |
4406 | *p == '_' || | |
4407 | *p == '.') | |
4408 | *(d++) = *p; | |
4409 | ||
4410 | *d = 0; | |
4411 | ||
4412 | strshorten(s, HOST_NAME_MAX); | |
4413 | return s; | |
4414 | } | |
4415 | ||
1dccbe19 LP |
4416 | static const char *const ioprio_class_table[] = { |
4417 | [IOPRIO_CLASS_NONE] = "none", | |
4418 | [IOPRIO_CLASS_RT] = "realtime", | |
4419 | [IOPRIO_CLASS_BE] = "best-effort", | |
4420 | [IOPRIO_CLASS_IDLE] = "idle" | |
4421 | }; | |
4422 | ||
4423 | DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int); | |
4424 | ||
4425 | static const char *const sigchld_code_table[] = { | |
4426 | [CLD_EXITED] = "exited", | |
4427 | [CLD_KILLED] = "killed", | |
4428 | [CLD_DUMPED] = "dumped", | |
4429 | [CLD_TRAPPED] = "trapped", | |
4430 | [CLD_STOPPED] = "stopped", | |
4431 | [CLD_CONTINUED] = "continued", | |
4432 | }; | |
4433 | ||
4434 | DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int); | |
4435 | ||
7d76f312 | 4436 | static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = { |
1dccbe19 LP |
4437 | [LOG_FAC(LOG_KERN)] = "kern", |
4438 | [LOG_FAC(LOG_USER)] = "user", | |
4439 | [LOG_FAC(LOG_MAIL)] = "mail", | |
4440 | [LOG_FAC(LOG_DAEMON)] = "daemon", | |
4441 | [LOG_FAC(LOG_AUTH)] = "auth", | |
4442 | [LOG_FAC(LOG_SYSLOG)] = "syslog", | |
4443 | [LOG_FAC(LOG_LPR)] = "lpr", | |
4444 | [LOG_FAC(LOG_NEWS)] = "news", | |
4445 | [LOG_FAC(LOG_UUCP)] = "uucp", | |
4446 | [LOG_FAC(LOG_CRON)] = "cron", | |
4447 | [LOG_FAC(LOG_AUTHPRIV)] = "authpriv", | |
4448 | [LOG_FAC(LOG_FTP)] = "ftp", | |
4449 | [LOG_FAC(LOG_LOCAL0)] = "local0", | |
4450 | [LOG_FAC(LOG_LOCAL1)] = "local1", | |
4451 | [LOG_FAC(LOG_LOCAL2)] = "local2", | |
4452 | [LOG_FAC(LOG_LOCAL3)] = "local3", | |
4453 | [LOG_FAC(LOG_LOCAL4)] = "local4", | |
4454 | [LOG_FAC(LOG_LOCAL5)] = "local5", | |
4455 | [LOG_FAC(LOG_LOCAL6)] = "local6", | |
4456 | [LOG_FAC(LOG_LOCAL7)] = "local7" | |
4457 | }; | |
4458 | ||
7d76f312 | 4459 | DEFINE_STRING_TABLE_LOOKUP(log_facility_unshifted, int); |
1dccbe19 LP |
4460 | |
4461 | static const char *const log_level_table[] = { | |
4462 | [LOG_EMERG] = "emerg", | |
4463 | [LOG_ALERT] = "alert", | |
4464 | [LOG_CRIT] = "crit", | |
4465 | [LOG_ERR] = "err", | |
4466 | [LOG_WARNING] = "warning", | |
4467 | [LOG_NOTICE] = "notice", | |
4468 | [LOG_INFO] = "info", | |
4469 | [LOG_DEBUG] = "debug" | |
4470 | }; | |
4471 | ||
4472 | DEFINE_STRING_TABLE_LOOKUP(log_level, int); | |
4473 | ||
4474 | static const char* const sched_policy_table[] = { | |
4475 | [SCHED_OTHER] = "other", | |
4476 | [SCHED_BATCH] = "batch", | |
4477 | [SCHED_IDLE] = "idle", | |
4478 | [SCHED_FIFO] = "fifo", | |
4479 | [SCHED_RR] = "rr" | |
4480 | }; | |
4481 | ||
4482 | DEFINE_STRING_TABLE_LOOKUP(sched_policy, int); | |
4483 | ||
4484 | static const char* const rlimit_table[] = { | |
4485 | [RLIMIT_CPU] = "LimitCPU", | |
4486 | [RLIMIT_FSIZE] = "LimitFSIZE", | |
4487 | [RLIMIT_DATA] = "LimitDATA", | |
4488 | [RLIMIT_STACK] = "LimitSTACK", | |
4489 | [RLIMIT_CORE] = "LimitCORE", | |
4490 | [RLIMIT_RSS] = "LimitRSS", | |
4491 | [RLIMIT_NOFILE] = "LimitNOFILE", | |
4492 | [RLIMIT_AS] = "LimitAS", | |
4493 | [RLIMIT_NPROC] = "LimitNPROC", | |
4494 | [RLIMIT_MEMLOCK] = "LimitMEMLOCK", | |
4495 | [RLIMIT_LOCKS] = "LimitLOCKS", | |
4496 | [RLIMIT_SIGPENDING] = "LimitSIGPENDING", | |
4497 | [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE", | |
4498 | [RLIMIT_NICE] = "LimitNICE", | |
4499 | [RLIMIT_RTPRIO] = "LimitRTPRIO", | |
4500 | [RLIMIT_RTTIME] = "LimitRTTIME" | |
4501 | }; | |
4502 | ||
4503 | DEFINE_STRING_TABLE_LOOKUP(rlimit, int); | |
4fd5948e LP |
4504 | |
4505 | static const char* const ip_tos_table[] = { | |
4506 | [IPTOS_LOWDELAY] = "low-delay", | |
4507 | [IPTOS_THROUGHPUT] = "throughput", | |
4508 | [IPTOS_RELIABILITY] = "reliability", | |
4509 | [IPTOS_LOWCOST] = "low-cost", | |
4510 | }; | |
4511 | ||
4512 | DEFINE_STRING_TABLE_LOOKUP(ip_tos, int); | |
2e22afe9 LP |
4513 | |
4514 | static const char *const signal_table[] = { | |
4515 | [SIGHUP] = "HUP", | |
4516 | [SIGINT] = "INT", | |
4517 | [SIGQUIT] = "QUIT", | |
4518 | [SIGILL] = "ILL", | |
4519 | [SIGTRAP] = "TRAP", | |
4520 | [SIGABRT] = "ABRT", | |
4521 | [SIGBUS] = "BUS", | |
4522 | [SIGFPE] = "FPE", | |
4523 | [SIGKILL] = "KILL", | |
4524 | [SIGUSR1] = "USR1", | |
4525 | [SIGSEGV] = "SEGV", | |
4526 | [SIGUSR2] = "USR2", | |
4527 | [SIGPIPE] = "PIPE", | |
4528 | [SIGALRM] = "ALRM", | |
4529 | [SIGTERM] = "TERM", | |
f26ee0b9 LP |
4530 | #ifdef SIGSTKFLT |
4531 | [SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */ | |
4532 | #endif | |
2e22afe9 LP |
4533 | [SIGCHLD] = "CHLD", |
4534 | [SIGCONT] = "CONT", | |
4535 | [SIGSTOP] = "STOP", | |
4536 | [SIGTSTP] = "TSTP", | |
4537 | [SIGTTIN] = "TTIN", | |
4538 | [SIGTTOU] = "TTOU", | |
4539 | [SIGURG] = "URG", | |
4540 | [SIGXCPU] = "XCPU", | |
4541 | [SIGXFSZ] = "XFSZ", | |
4542 | [SIGVTALRM] = "VTALRM", | |
4543 | [SIGPROF] = "PROF", | |
4544 | [SIGWINCH] = "WINCH", | |
4545 | [SIGIO] = "IO", | |
4546 | [SIGPWR] = "PWR", | |
4547 | [SIGSYS] = "SYS" | |
4548 | }; | |
4549 | ||
4550 | DEFINE_STRING_TABLE_LOOKUP(signal, int); | |
db1413d7 KS |
4551 | |
4552 | static int file_is_conf(const struct dirent *d, const char *suffix) { | |
4553 | assert(d); | |
4554 | ||
4555 | if (ignore_file(d->d_name)) | |
4556 | return 0; | |
4557 | ||
4558 | if (d->d_type != DT_REG && | |
4559 | d->d_type != DT_LNK && | |
4560 | d->d_type != DT_UNKNOWN) | |
4561 | return 0; | |
4562 | ||
4563 | return endswith(d->d_name, suffix); | |
4564 | } | |
4565 | ||
4566 | static int files_add(Hashmap *h, const char *path, const char *suffix) { | |
4567 | DIR *dir; | |
4568 | struct dirent *de; | |
4569 | int r = 0; | |
4570 | ||
4571 | dir = opendir(path); | |
4572 | if (!dir) { | |
4573 | if (errno == ENOENT) | |
4574 | return 0; | |
4575 | return -errno; | |
4576 | } | |
4577 | ||
4578 | for (de = readdir(dir); de; de = readdir(dir)) { | |
4579 | char *f; | |
4580 | const char *base; | |
4581 | ||
4582 | if (!file_is_conf(de, suffix)) | |
4583 | continue; | |
4584 | ||
4585 | if (asprintf(&f, "%s/%s", path, de->d_name) < 0) { | |
4586 | r = -ENOMEM; | |
4587 | goto finish; | |
4588 | } | |
4589 | ||
4590 | log_debug("found: %s\n", f); | |
4591 | base = f + strlen(path) + 1; | |
4592 | if (hashmap_put(h, base, f) <= 0) | |
4593 | free(f); | |
4594 | } | |
4595 | ||
4596 | finish: | |
4597 | closedir(dir); | |
4598 | return r; | |
4599 | } | |
4600 | ||
4601 | static int base_cmp(const void *a, const void *b) { | |
4602 | const char *s1, *s2; | |
4603 | ||
4604 | s1 = *(char * const *)a; | |
4605 | s2 = *(char * const *)b; | |
4606 | return strcmp(file_name_from_path(s1), file_name_from_path(s2)); | |
4607 | } | |
4608 | ||
44143309 | 4609 | int conf_files_list(char ***strv, const char *suffix, const char *dir, ...) { |
db1413d7 KS |
4610 | Hashmap *fh; |
4611 | char **files = NULL; | |
4612 | va_list ap; | |
44143309 | 4613 | int r = 0; |
db1413d7 KS |
4614 | |
4615 | fh = hashmap_new(string_hash_func, string_compare_func); | |
4616 | if (!fh) { | |
44143309 | 4617 | r = -ENOMEM; |
db1413d7 KS |
4618 | goto finish; |
4619 | } | |
4620 | ||
4621 | va_start(ap, dir); | |
4622 | while (dir) { | |
4623 | if (files_add(fh, dir, suffix) < 0) { | |
4624 | log_error("Failed to search for files."); | |
44143309 | 4625 | r = -EINVAL; |
db1413d7 KS |
4626 | goto finish; |
4627 | } | |
4628 | dir = va_arg(ap, const char *); | |
4629 | } | |
4630 | va_end(ap); | |
4631 | ||
4632 | files = hashmap_get_strv(fh); | |
4633 | if (files == NULL) { | |
4634 | log_error("Failed to compose list of files."); | |
44143309 | 4635 | r = -ENOMEM; |
db1413d7 KS |
4636 | goto finish; |
4637 | } | |
4638 | ||
4639 | qsort(files, hashmap_size(fh), sizeof(char *), base_cmp); | |
4640 | finish: | |
4641 | hashmap_free(fh); | |
44143309 KS |
4642 | *strv = files; |
4643 | return r; | |
db1413d7 | 4644 | } |