]>
Commit | Line | Data |
---|---|---|
60918275 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
60918275 LP |
22 | #include <assert.h> |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <errno.h> | |
85261803 | 26 | #include <stdlib.h> |
034c6ed7 LP |
27 | #include <signal.h> |
28 | #include <stdio.h> | |
1dccbe19 LP |
29 | #include <syslog.h> |
30 | #include <sched.h> | |
31 | #include <sys/resource.h> | |
ef886c6a | 32 | #include <linux/sched.h> |
a9f5d454 LP |
33 | #include <sys/types.h> |
34 | #include <sys/stat.h> | |
3a0ecb08 | 35 | #include <fcntl.h> |
a0d40ac5 | 36 | #include <dirent.h> |
601f6a1e LP |
37 | #include <sys/ioctl.h> |
38 | #include <linux/vt.h> | |
39 | #include <linux/tiocl.h> | |
80876c20 LP |
40 | #include <termios.h> |
41 | #include <stdarg.h> | |
42 | #include <sys/inotify.h> | |
43 | #include <sys/poll.h> | |
8d567588 | 44 | #include <libgen.h> |
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> |
60918275 LP |
50 | |
51 | #include "macro.h" | |
52 | #include "util.h" | |
1dccbe19 LP |
53 | #include "ioprio.h" |
54 | #include "missing.h" | |
a9f5d454 | 55 | #include "log.h" |
65d2ebdc | 56 | #include "strv.h" |
60918275 | 57 | |
e05797fb LP |
58 | bool streq_ptr(const char *a, const char *b) { |
59 | ||
60 | /* Like streq(), but tries to make sense of NULL pointers */ | |
61 | ||
62 | if (a && b) | |
63 | return streq(a, b); | |
64 | ||
65 | if (!a && !b) | |
66 | return true; | |
67 | ||
68 | return false; | |
69 | } | |
70 | ||
47be870b | 71 | usec_t now(clockid_t clock_id) { |
60918275 LP |
72 | struct timespec ts; |
73 | ||
47be870b | 74 | assert_se(clock_gettime(clock_id, &ts) == 0); |
60918275 LP |
75 | |
76 | return timespec_load(&ts); | |
77 | } | |
78 | ||
63983207 | 79 | dual_timestamp* dual_timestamp_get(dual_timestamp *ts) { |
871d7de4 LP |
80 | assert(ts); |
81 | ||
82 | ts->realtime = now(CLOCK_REALTIME); | |
83 | ts->monotonic = now(CLOCK_MONOTONIC); | |
84 | ||
85 | return ts; | |
86 | } | |
87 | ||
60918275 LP |
88 | usec_t timespec_load(const struct timespec *ts) { |
89 | assert(ts); | |
90 | ||
91 | return | |
92 | (usec_t) ts->tv_sec * USEC_PER_SEC + | |
93 | (usec_t) ts->tv_nsec / NSEC_PER_USEC; | |
94 | } | |
95 | ||
96 | struct timespec *timespec_store(struct timespec *ts, usec_t u) { | |
97 | assert(ts); | |
98 | ||
99 | ts->tv_sec = (time_t) (u / USEC_PER_SEC); | |
100 | ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC); | |
101 | ||
102 | return ts; | |
103 | } | |
104 | ||
105 | usec_t timeval_load(const struct timeval *tv) { | |
106 | assert(tv); | |
107 | ||
108 | return | |
109 | (usec_t) tv->tv_sec * USEC_PER_SEC + | |
110 | (usec_t) tv->tv_usec; | |
111 | } | |
112 | ||
113 | struct timeval *timeval_store(struct timeval *tv, usec_t u) { | |
114 | assert(tv); | |
115 | ||
116 | tv->tv_sec = (time_t) (u / USEC_PER_SEC); | |
117 | tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC); | |
118 | ||
119 | return tv; | |
120 | } | |
121 | ||
122 | bool endswith(const char *s, const char *postfix) { | |
123 | size_t sl, pl; | |
124 | ||
125 | assert(s); | |
126 | assert(postfix); | |
127 | ||
128 | sl = strlen(s); | |
129 | pl = strlen(postfix); | |
130 | ||
d4d0d4db LP |
131 | if (pl == 0) |
132 | return true; | |
133 | ||
60918275 LP |
134 | if (sl < pl) |
135 | return false; | |
136 | ||
137 | return memcmp(s + sl - pl, postfix, pl) == 0; | |
138 | } | |
139 | ||
140 | bool startswith(const char *s, const char *prefix) { | |
141 | size_t sl, pl; | |
142 | ||
143 | assert(s); | |
144 | assert(prefix); | |
145 | ||
146 | sl = strlen(s); | |
147 | pl = strlen(prefix); | |
148 | ||
d4d0d4db LP |
149 | if (pl == 0) |
150 | return true; | |
151 | ||
60918275 LP |
152 | if (sl < pl) |
153 | return false; | |
154 | ||
155 | return memcmp(s, prefix, pl) == 0; | |
156 | } | |
157 | ||
3177a7fa MAP |
158 | bool startswith_no_case(const char *s, const char *prefix) { |
159 | size_t sl, pl; | |
160 | unsigned i; | |
161 | ||
162 | assert(s); | |
163 | assert(prefix); | |
164 | ||
165 | sl = strlen(s); | |
166 | pl = strlen(prefix); | |
167 | ||
168 | if (pl == 0) | |
169 | return true; | |
170 | ||
171 | if (sl < pl) | |
172 | return false; | |
173 | ||
174 | for(i = 0; i < pl; ++i) { | |
175 | if (tolower(s[i]) != tolower(prefix[i])) | |
176 | return false; | |
177 | } | |
178 | ||
179 | return true; | |
180 | } | |
181 | ||
79d6d816 LP |
182 | bool first_word(const char *s, const char *word) { |
183 | size_t sl, wl; | |
184 | ||
185 | assert(s); | |
186 | assert(word); | |
187 | ||
188 | sl = strlen(s); | |
189 | wl = strlen(word); | |
190 | ||
191 | if (sl < wl) | |
192 | return false; | |
193 | ||
d4d0d4db LP |
194 | if (wl == 0) |
195 | return true; | |
196 | ||
79d6d816 LP |
197 | if (memcmp(s, word, wl) != 0) |
198 | return false; | |
199 | ||
d4d0d4db LP |
200 | return s[wl] == 0 || |
201 | strchr(WHITESPACE, s[wl]); | |
79d6d816 LP |
202 | } |
203 | ||
42f4e3c4 | 204 | int close_nointr(int fd) { |
60918275 LP |
205 | assert(fd >= 0); |
206 | ||
207 | for (;;) { | |
208 | int r; | |
209 | ||
210 | if ((r = close(fd)) >= 0) | |
211 | return r; | |
212 | ||
213 | if (errno != EINTR) | |
214 | return r; | |
215 | } | |
216 | } | |
85261803 | 217 | |
85f136b5 | 218 | void close_nointr_nofail(int fd) { |
80876c20 | 219 | int saved_errno = errno; |
85f136b5 LP |
220 | |
221 | /* like close_nointr() but cannot fail, and guarantees errno | |
222 | * is unchanged */ | |
223 | ||
224 | assert_se(close_nointr(fd) == 0); | |
80876c20 LP |
225 | |
226 | errno = saved_errno; | |
85f136b5 LP |
227 | } |
228 | ||
5b6319dc LP |
229 | void close_many(const int fds[], unsigned n_fd) { |
230 | unsigned i; | |
231 | ||
232 | for (i = 0; i < n_fd; i++) | |
233 | close_nointr_nofail(fds[i]); | |
234 | } | |
235 | ||
85261803 LP |
236 | int parse_boolean(const char *v) { |
237 | assert(v); | |
238 | ||
44d8db9e | 239 | if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on")) |
85261803 | 240 | return 1; |
44d8db9e | 241 | else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off")) |
85261803 LP |
242 | return 0; |
243 | ||
244 | return -EINVAL; | |
245 | } | |
246 | ||
3ba686c1 LP |
247 | int parse_pid(const char *s, pid_t* ret_pid) { |
248 | unsigned long ul; | |
249 | pid_t pid; | |
250 | int r; | |
251 | ||
252 | assert(s); | |
253 | assert(ret_pid); | |
254 | ||
255 | if ((r = safe_atolu(s, &ul)) < 0) | |
256 | return r; | |
257 | ||
258 | pid = (pid_t) ul; | |
259 | ||
260 | if ((unsigned long) pid != ul) | |
261 | return -ERANGE; | |
262 | ||
263 | if (pid <= 0) | |
264 | return -ERANGE; | |
265 | ||
266 | *ret_pid = pid; | |
267 | return 0; | |
268 | } | |
269 | ||
85261803 LP |
270 | int safe_atou(const char *s, unsigned *ret_u) { |
271 | char *x = NULL; | |
034c6ed7 | 272 | unsigned long l; |
85261803 LP |
273 | |
274 | assert(s); | |
275 | assert(ret_u); | |
276 | ||
277 | errno = 0; | |
278 | l = strtoul(s, &x, 0); | |
279 | ||
280 | if (!x || *x || errno) | |
281 | return errno ? -errno : -EINVAL; | |
282 | ||
034c6ed7 | 283 | if ((unsigned long) (unsigned) l != l) |
85261803 LP |
284 | return -ERANGE; |
285 | ||
286 | *ret_u = (unsigned) l; | |
287 | return 0; | |
288 | } | |
289 | ||
290 | int safe_atoi(const char *s, int *ret_i) { | |
291 | char *x = NULL; | |
034c6ed7 | 292 | long l; |
85261803 LP |
293 | |
294 | assert(s); | |
295 | assert(ret_i); | |
296 | ||
297 | errno = 0; | |
298 | l = strtol(s, &x, 0); | |
299 | ||
300 | if (!x || *x || errno) | |
301 | return errno ? -errno : -EINVAL; | |
302 | ||
034c6ed7 | 303 | if ((long) (int) l != l) |
85261803 LP |
304 | return -ERANGE; |
305 | ||
034c6ed7 LP |
306 | *ret_i = (int) l; |
307 | return 0; | |
308 | } | |
309 | ||
034c6ed7 LP |
310 | int safe_atollu(const char *s, long long unsigned *ret_llu) { |
311 | char *x = NULL; | |
312 | unsigned long long l; | |
313 | ||
314 | assert(s); | |
315 | assert(ret_llu); | |
316 | ||
317 | errno = 0; | |
318 | l = strtoull(s, &x, 0); | |
319 | ||
320 | if (!x || *x || errno) | |
321 | return errno ? -errno : -EINVAL; | |
322 | ||
323 | *ret_llu = l; | |
324 | return 0; | |
325 | } | |
326 | ||
327 | int safe_atolli(const char *s, long long int *ret_lli) { | |
328 | char *x = NULL; | |
329 | long long l; | |
330 | ||
331 | assert(s); | |
332 | assert(ret_lli); | |
333 | ||
334 | errno = 0; | |
335 | l = strtoll(s, &x, 0); | |
336 | ||
337 | if (!x || *x || errno) | |
338 | return errno ? -errno : -EINVAL; | |
339 | ||
340 | *ret_lli = l; | |
85261803 LP |
341 | return 0; |
342 | } | |
a41e8209 | 343 | |
a41e8209 | 344 | /* Split a string into words. */ |
65d2ebdc | 345 | char *split(const char *c, size_t *l, const char *separator, char **state) { |
a41e8209 LP |
346 | char *current; |
347 | ||
348 | current = *state ? *state : (char*) c; | |
349 | ||
350 | if (!*current || *c == 0) | |
351 | return NULL; | |
352 | ||
65d2ebdc LP |
353 | current += strspn(current, separator); |
354 | *l = strcspn(current, separator); | |
82919e3d LP |
355 | *state = current+*l; |
356 | ||
357 | return (char*) current; | |
358 | } | |
359 | ||
034c6ed7 LP |
360 | /* Split a string into words, but consider strings enclosed in '' and |
361 | * "" as words even if they include spaces. */ | |
362 | char *split_quoted(const char *c, size_t *l, char **state) { | |
0bab36f2 LP |
363 | char *current, *e; |
364 | bool escaped = false; | |
034c6ed7 LP |
365 | |
366 | current = *state ? *state : (char*) c; | |
367 | ||
368 | if (!*current || *c == 0) | |
369 | return NULL; | |
370 | ||
371 | current += strspn(current, WHITESPACE); | |
372 | ||
373 | if (*current == '\'') { | |
374 | current ++; | |
034c6ed7 | 375 | |
0bab36f2 LP |
376 | for (e = current; *e; e++) { |
377 | if (escaped) | |
378 | escaped = false; | |
379 | else if (*e == '\\') | |
380 | escaped = true; | |
381 | else if (*e == '\'') | |
382 | break; | |
383 | } | |
384 | ||
385 | *l = e-current; | |
386 | *state = *e == 0 ? e : e+1; | |
034c6ed7 LP |
387 | } else if (*current == '\"') { |
388 | current ++; | |
034c6ed7 | 389 | |
0bab36f2 LP |
390 | for (e = current; *e; e++) { |
391 | if (escaped) | |
392 | escaped = false; | |
393 | else if (*e == '\\') | |
394 | escaped = true; | |
395 | else if (*e == '\"') | |
396 | break; | |
397 | } | |
398 | ||
399 | *l = e-current; | |
400 | *state = *e == 0 ? e : e+1; | |
034c6ed7 | 401 | } else { |
0bab36f2 LP |
402 | for (e = current; *e; e++) { |
403 | if (escaped) | |
404 | escaped = false; | |
405 | else if (*e == '\\') | |
406 | escaped = true; | |
407 | else if (strchr(WHITESPACE, *e)) | |
408 | break; | |
409 | } | |
410 | *l = e-current; | |
411 | *state = e; | |
034c6ed7 LP |
412 | } |
413 | ||
414 | return (char*) current; | |
415 | } | |
416 | ||
65d2ebdc LP |
417 | char **split_path_and_make_absolute(const char *p) { |
418 | char **l; | |
419 | assert(p); | |
420 | ||
421 | if (!(l = strv_split(p, ":"))) | |
422 | return NULL; | |
423 | ||
424 | if (!strv_path_make_absolute_cwd(l)) { | |
425 | strv_free(l); | |
426 | return NULL; | |
427 | } | |
428 | ||
429 | return l; | |
430 | } | |
431 | ||
034c6ed7 LP |
432 | int get_parent_of_pid(pid_t pid, pid_t *_ppid) { |
433 | int r; | |
434 | FILE *f; | |
435 | char fn[132], line[256], *p; | |
bb00e604 | 436 | long unsigned ppid; |
034c6ed7 LP |
437 | |
438 | assert(pid >= 0); | |
439 | assert(_ppid); | |
440 | ||
bb00e604 | 441 | assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1)); |
034c6ed7 LP |
442 | fn[sizeof(fn)-1] = 0; |
443 | ||
444 | if (!(f = fopen(fn, "r"))) | |
445 | return -errno; | |
446 | ||
447 | if (!(fgets(line, sizeof(line), f))) { | |
448 | r = -errno; | |
449 | fclose(f); | |
450 | return r; | |
451 | } | |
452 | ||
453 | fclose(f); | |
454 | ||
455 | /* Let's skip the pid and comm fields. The latter is enclosed | |
456 | * in () but does not escape any () in its value, so let's | |
457 | * skip over it manually */ | |
458 | ||
459 | if (!(p = strrchr(line, ')'))) | |
460 | return -EIO; | |
461 | ||
462 | p++; | |
463 | ||
464 | if (sscanf(p, " " | |
465 | "%*c " /* state */ | |
bb00e604 | 466 | "%lu ", /* ppid */ |
034c6ed7 LP |
467 | &ppid) != 1) |
468 | return -EIO; | |
469 | ||
bb00e604 | 470 | if ((long unsigned) (pid_t) ppid != ppid) |
034c6ed7 LP |
471 | return -ERANGE; |
472 | ||
473 | *_ppid = (pid_t) ppid; | |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
478 | int write_one_line_file(const char *fn, const char *line) { | |
479 | FILE *f; | |
480 | int r; | |
481 | ||
482 | assert(fn); | |
483 | assert(line); | |
484 | ||
485 | if (!(f = fopen(fn, "we"))) | |
486 | return -errno; | |
487 | ||
488 | if (fputs(line, f) < 0) { | |
489 | r = -errno; | |
490 | goto finish; | |
491 | } | |
492 | ||
493 | r = 0; | |
494 | finish: | |
495 | fclose(f); | |
496 | return r; | |
497 | } | |
498 | ||
499 | int read_one_line_file(const char *fn, char **line) { | |
500 | FILE *f; | |
501 | int r; | |
11316633 | 502 | char t[2048], *c; |
034c6ed7 LP |
503 | |
504 | assert(fn); | |
505 | assert(line); | |
506 | ||
507 | if (!(f = fopen(fn, "re"))) | |
508 | return -errno; | |
509 | ||
510 | if (!(fgets(t, sizeof(t), f))) { | |
511 | r = -errno; | |
512 | goto finish; | |
513 | } | |
514 | ||
515 | if (!(c = strdup(t))) { | |
516 | r = -ENOMEM; | |
517 | goto finish; | |
518 | } | |
519 | ||
520 | *line = c; | |
521 | r = 0; | |
522 | ||
523 | finish: | |
524 | fclose(f); | |
525 | return r; | |
526 | } | |
44d8db9e | 527 | |
7072ced8 LP |
528 | char *truncate_nl(char *s) { |
529 | assert(s); | |
530 | ||
531 | s[strcspn(s, NEWLINE)] = 0; | |
532 | return s; | |
533 | } | |
534 | ||
535 | int get_process_name(pid_t pid, char **name) { | |
536 | char *p; | |
537 | int r; | |
538 | ||
539 | assert(pid >= 1); | |
540 | assert(name); | |
541 | ||
bb00e604 | 542 | if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0) |
7072ced8 LP |
543 | return -ENOMEM; |
544 | ||
545 | r = read_one_line_file(p, name); | |
546 | free(p); | |
547 | ||
548 | if (r < 0) | |
549 | return r; | |
550 | ||
551 | truncate_nl(*name); | |
552 | return 0; | |
553 | } | |
554 | ||
c59760ee LP |
555 | int get_process_cmdline(pid_t pid, size_t max_length, char **line) { |
556 | char *p, *r, *k; | |
557 | int c; | |
558 | bool space = false; | |
559 | size_t left; | |
560 | FILE *f; | |
561 | ||
562 | assert(pid >= 1); | |
563 | assert(max_length > 0); | |
564 | assert(line); | |
565 | ||
566 | if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0) | |
567 | return -ENOMEM; | |
568 | ||
569 | f = fopen(p, "r"); | |
570 | free(p); | |
571 | ||
572 | if (!f) | |
573 | return -errno; | |
574 | ||
575 | if (!(r = new(char, max_length))) { | |
576 | fclose(f); | |
577 | return -ENOMEM; | |
578 | } | |
579 | ||
580 | k = r; | |
581 | left = max_length; | |
582 | while ((c = getc(f)) != EOF) { | |
583 | ||
584 | if (isprint(c)) { | |
585 | if (space) { | |
586 | if (left <= 4) | |
587 | break; | |
588 | ||
589 | *(k++) = ' '; | |
057fbb58 | 590 | left--; |
c59760ee LP |
591 | space = false; |
592 | } | |
593 | ||
594 | if (left <= 4) | |
595 | break; | |
596 | ||
597 | *(k++) = (char) c; | |
057fbb58 | 598 | left--; |
c59760ee LP |
599 | } else |
600 | space = true; | |
601 | } | |
602 | ||
603 | if (left <= 4) { | |
604 | size_t n = MIN(left-1, 3U); | |
605 | memcpy(k, "...", n); | |
606 | k[n] = 0; | |
607 | } else | |
608 | *k = 0; | |
609 | ||
610 | fclose(f); | |
611 | ||
612 | *line = r; | |
613 | return 0; | |
614 | } | |
615 | ||
44d8db9e LP |
616 | char *strappend(const char *s, const char *suffix) { |
617 | size_t a, b; | |
618 | char *r; | |
619 | ||
620 | assert(s); | |
621 | assert(suffix); | |
622 | ||
623 | a = strlen(s); | |
624 | b = strlen(suffix); | |
625 | ||
626 | if (!(r = new(char, a+b+1))) | |
627 | return NULL; | |
628 | ||
629 | memcpy(r, s, a); | |
630 | memcpy(r+a, suffix, b); | |
631 | r[a+b] = 0; | |
632 | ||
633 | return r; | |
634 | } | |
87f0e418 LP |
635 | |
636 | int readlink_malloc(const char *p, char **r) { | |
637 | size_t l = 100; | |
638 | ||
639 | assert(p); | |
640 | assert(r); | |
641 | ||
642 | for (;;) { | |
643 | char *c; | |
644 | ssize_t n; | |
645 | ||
646 | if (!(c = new(char, l))) | |
647 | return -ENOMEM; | |
648 | ||
649 | if ((n = readlink(p, c, l-1)) < 0) { | |
650 | int ret = -errno; | |
651 | free(c); | |
652 | return ret; | |
653 | } | |
654 | ||
655 | if ((size_t) n < l-1) { | |
656 | c[n] = 0; | |
657 | *r = c; | |
658 | return 0; | |
659 | } | |
660 | ||
661 | free(c); | |
662 | l *= 2; | |
663 | } | |
664 | } | |
665 | ||
2c7108c4 LP |
666 | int readlink_and_make_absolute(const char *p, char **r) { |
667 | char *target, *k; | |
668 | int j; | |
669 | ||
670 | assert(p); | |
671 | assert(r); | |
672 | ||
673 | if ((j = readlink_malloc(p, &target)) < 0) | |
674 | return j; | |
675 | ||
676 | k = file_in_same_dir(p, target); | |
677 | free(target); | |
678 | ||
679 | if (!k) | |
680 | return -ENOMEM; | |
681 | ||
682 | *r = k; | |
683 | return 0; | |
684 | } | |
685 | ||
87f0e418 LP |
686 | char *file_name_from_path(const char *p) { |
687 | char *r; | |
688 | ||
689 | assert(p); | |
690 | ||
691 | if ((r = strrchr(p, '/'))) | |
692 | return r + 1; | |
693 | ||
694 | return (char*) p; | |
695 | } | |
0301abf4 LP |
696 | |
697 | bool path_is_absolute(const char *p) { | |
698 | assert(p); | |
699 | ||
700 | return p[0] == '/'; | |
701 | } | |
702 | ||
703 | bool is_path(const char *p) { | |
704 | ||
705 | return !!strchr(p, '/'); | |
706 | } | |
707 | ||
708 | char *path_make_absolute(const char *p, const char *prefix) { | |
709 | char *r; | |
710 | ||
711 | assert(p); | |
712 | ||
65d2ebdc LP |
713 | /* Makes every item in the list an absolute path by prepending |
714 | * the prefix, if specified and necessary */ | |
715 | ||
0301abf4 LP |
716 | if (path_is_absolute(p) || !prefix) |
717 | return strdup(p); | |
718 | ||
719 | if (asprintf(&r, "%s/%s", prefix, p) < 0) | |
720 | return NULL; | |
721 | ||
722 | return r; | |
723 | } | |
2a987ee8 | 724 | |
65d2ebdc LP |
725 | char *path_make_absolute_cwd(const char *p) { |
726 | char *cwd, *r; | |
727 | ||
728 | assert(p); | |
729 | ||
730 | /* Similar to path_make_absolute(), but prefixes with the | |
731 | * current working directory. */ | |
732 | ||
733 | if (path_is_absolute(p)) | |
734 | return strdup(p); | |
735 | ||
736 | if (!(cwd = get_current_dir_name())) | |
737 | return NULL; | |
738 | ||
739 | r = path_make_absolute(p, cwd); | |
740 | free(cwd); | |
741 | ||
742 | return r; | |
743 | } | |
744 | ||
745 | char **strv_path_make_absolute_cwd(char **l) { | |
746 | char **s; | |
747 | ||
748 | /* Goes through every item in the string list and makes it | |
749 | * absolute. This works in place and won't rollback any | |
750 | * changes on failure. */ | |
751 | ||
752 | STRV_FOREACH(s, l) { | |
753 | char *t; | |
754 | ||
755 | if (!(t = path_make_absolute_cwd(*s))) | |
756 | return NULL; | |
757 | ||
758 | free(*s); | |
759 | *s = t; | |
760 | } | |
761 | ||
762 | return l; | |
763 | } | |
764 | ||
c3f6d675 LP |
765 | char **strv_path_canonicalize(char **l) { |
766 | char **s; | |
767 | unsigned k = 0; | |
768 | bool enomem = false; | |
769 | ||
770 | if (strv_isempty(l)) | |
771 | return l; | |
772 | ||
773 | /* Goes through every item in the string list and canonicalize | |
774 | * the path. This works in place and won't rollback any | |
775 | * changes on failure. */ | |
776 | ||
777 | STRV_FOREACH(s, l) { | |
778 | char *t, *u; | |
779 | ||
780 | t = path_make_absolute_cwd(*s); | |
781 | free(*s); | |
782 | ||
783 | if (!t) { | |
784 | enomem = true; | |
785 | continue; | |
786 | } | |
787 | ||
788 | errno = 0; | |
789 | u = canonicalize_file_name(t); | |
790 | free(t); | |
791 | ||
792 | if (!u) { | |
793 | if (errno == ENOMEM || !errno) | |
794 | enomem = true; | |
795 | ||
796 | continue; | |
797 | } | |
798 | ||
799 | l[k++] = u; | |
800 | } | |
801 | ||
802 | l[k] = NULL; | |
803 | ||
804 | if (enomem) | |
805 | return NULL; | |
806 | ||
807 | return l; | |
808 | } | |
809 | ||
2a987ee8 LP |
810 | int reset_all_signal_handlers(void) { |
811 | int sig; | |
812 | ||
813 | for (sig = 1; sig < _NSIG; sig++) { | |
814 | struct sigaction sa; | |
815 | ||
816 | if (sig == SIGKILL || sig == SIGSTOP) | |
817 | continue; | |
818 | ||
819 | zero(sa); | |
820 | sa.sa_handler = SIG_DFL; | |
431c32bf | 821 | sa.sa_flags = SA_RESTART; |
2a987ee8 LP |
822 | |
823 | /* On Linux the first two RT signals are reserved by | |
824 | * glibc, and sigaction() will return EINVAL for them. */ | |
825 | if ((sigaction(sig, &sa, NULL) < 0)) | |
826 | if (errno != EINVAL) | |
827 | return -errno; | |
828 | } | |
829 | ||
8e274523 | 830 | return 0; |
2a987ee8 | 831 | } |
4a72ff34 LP |
832 | |
833 | char *strstrip(char *s) { | |
834 | char *e, *l = NULL; | |
835 | ||
836 | /* Drops trailing whitespace. Modifies the string in | |
837 | * place. Returns pointer to first non-space character */ | |
838 | ||
839 | s += strspn(s, WHITESPACE); | |
840 | ||
841 | for (e = s; *e; e++) | |
842 | if (!strchr(WHITESPACE, *e)) | |
843 | l = e; | |
844 | ||
845 | if (l) | |
846 | *(l+1) = 0; | |
847 | else | |
848 | *s = 0; | |
849 | ||
850 | return s; | |
4a72ff34 LP |
851 | } |
852 | ||
ee9b5e01 LP |
853 | char *delete_chars(char *s, const char *bad) { |
854 | char *f, *t; | |
855 | ||
856 | /* Drops all whitespace, regardless where in the string */ | |
857 | ||
858 | for (f = s, t = s; *f; f++) { | |
859 | if (strchr(bad, *f)) | |
860 | continue; | |
861 | ||
862 | *(t++) = *f; | |
863 | } | |
864 | ||
865 | *t = 0; | |
866 | ||
867 | return s; | |
868 | } | |
869 | ||
4a72ff34 LP |
870 | char *file_in_same_dir(const char *path, const char *filename) { |
871 | char *e, *r; | |
872 | size_t k; | |
873 | ||
874 | assert(path); | |
875 | assert(filename); | |
876 | ||
877 | /* This removes the last component of path and appends | |
878 | * filename, unless the latter is absolute anyway or the | |
879 | * former isn't */ | |
880 | ||
881 | if (path_is_absolute(filename)) | |
882 | return strdup(filename); | |
883 | ||
884 | if (!(e = strrchr(path, '/'))) | |
885 | return strdup(filename); | |
886 | ||
887 | k = strlen(filename); | |
888 | if (!(r = new(char, e-path+1+k+1))) | |
889 | return NULL; | |
890 | ||
891 | memcpy(r, path, e-path+1); | |
892 | memcpy(r+(e-path)+1, filename, k+1); | |
893 | ||
894 | return r; | |
895 | } | |
fb624d04 | 896 | |
8c6db833 LP |
897 | int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) { |
898 | struct stat st; | |
899 | ||
900 | if (mkdir(path, mode) >= 0) | |
901 | if (chmod_and_chown(path, mode, uid, gid) < 0) | |
902 | return -errno; | |
903 | ||
904 | if (lstat(path, &st) < 0) | |
905 | return -errno; | |
906 | ||
907 | if ((st.st_mode & 0777) != mode || | |
908 | st.st_uid != uid || | |
909 | st.st_gid != gid || | |
910 | !S_ISDIR(st.st_mode)) { | |
911 | errno = EEXIST; | |
912 | return -errno; | |
913 | } | |
914 | ||
915 | return 0; | |
916 | } | |
917 | ||
918 | ||
a9f5d454 LP |
919 | int mkdir_parents(const char *path, mode_t mode) { |
920 | const char *p, *e; | |
921 | ||
922 | assert(path); | |
923 | ||
924 | /* Creates every parent directory in the path except the last | |
925 | * component. */ | |
926 | ||
927 | p = path + strspn(path, "/"); | |
928 | for (;;) { | |
929 | int r; | |
930 | char *t; | |
931 | ||
932 | e = p + strcspn(p, "/"); | |
933 | p = e + strspn(e, "/"); | |
934 | ||
935 | /* Is this the last component? If so, then we're | |
936 | * done */ | |
937 | if (*p == 0) | |
938 | return 0; | |
939 | ||
940 | if (!(t = strndup(path, e - path))) | |
941 | return -ENOMEM; | |
942 | ||
943 | r = mkdir(t, mode); | |
944 | ||
945 | free(t); | |
946 | ||
947 | if (r < 0 && errno != EEXIST) | |
948 | return -errno; | |
949 | } | |
950 | } | |
951 | ||
bbd67135 LP |
952 | int mkdir_p(const char *path, mode_t mode) { |
953 | int r; | |
954 | ||
955 | /* Like mkdir -p */ | |
956 | ||
957 | if ((r = mkdir_parents(path, mode)) < 0) | |
958 | return r; | |
959 | ||
960 | if (mkdir(path, mode) < 0) | |
961 | return -errno; | |
962 | ||
963 | return 0; | |
964 | } | |
965 | ||
c32dd69b LP |
966 | int rmdir_parents(const char *path, const char *stop) { |
967 | size_t l; | |
968 | int r = 0; | |
969 | ||
970 | assert(path); | |
971 | assert(stop); | |
972 | ||
973 | l = strlen(path); | |
974 | ||
975 | /* Skip trailing slashes */ | |
976 | while (l > 0 && path[l-1] == '/') | |
977 | l--; | |
978 | ||
979 | while (l > 0) { | |
980 | char *t; | |
981 | ||
982 | /* Skip last component */ | |
983 | while (l > 0 && path[l-1] != '/') | |
984 | l--; | |
985 | ||
986 | /* Skip trailing slashes */ | |
987 | while (l > 0 && path[l-1] == '/') | |
988 | l--; | |
989 | ||
990 | if (l <= 0) | |
991 | break; | |
992 | ||
993 | if (!(t = strndup(path, l))) | |
994 | return -ENOMEM; | |
995 | ||
996 | if (path_startswith(stop, t)) { | |
997 | free(t); | |
998 | return 0; | |
999 | } | |
1000 | ||
1001 | r = rmdir(t); | |
1002 | free(t); | |
1003 | ||
1004 | if (r < 0) | |
1005 | if (errno != ENOENT) | |
1006 | return -errno; | |
1007 | } | |
1008 | ||
1009 | return 0; | |
1010 | } | |
1011 | ||
1012 | ||
fb624d04 LP |
1013 | char hexchar(int x) { |
1014 | static const char table[16] = "0123456789abcdef"; | |
1015 | ||
1016 | return table[x & 15]; | |
1017 | } | |
4fe88d28 LP |
1018 | |
1019 | int unhexchar(char c) { | |
1020 | ||
1021 | if (c >= '0' && c <= '9') | |
1022 | return c - '0'; | |
1023 | ||
1024 | if (c >= 'a' && c <= 'f') | |
ea430986 | 1025 | return c - 'a' + 10; |
4fe88d28 LP |
1026 | |
1027 | if (c >= 'A' && c <= 'F') | |
ea430986 | 1028 | return c - 'A' + 10; |
4fe88d28 LP |
1029 | |
1030 | return -1; | |
1031 | } | |
1032 | ||
1033 | char octchar(int x) { | |
1034 | return '0' + (x & 7); | |
1035 | } | |
1036 | ||
1037 | int unoctchar(char c) { | |
1038 | ||
1039 | if (c >= '0' && c <= '7') | |
1040 | return c - '0'; | |
1041 | ||
1042 | return -1; | |
1043 | } | |
1044 | ||
5af98f82 LP |
1045 | char decchar(int x) { |
1046 | return '0' + (x % 10); | |
1047 | } | |
1048 | ||
1049 | int undecchar(char c) { | |
1050 | ||
1051 | if (c >= '0' && c <= '9') | |
1052 | return c - '0'; | |
1053 | ||
1054 | return -1; | |
1055 | } | |
1056 | ||
4fe88d28 LP |
1057 | char *cescape(const char *s) { |
1058 | char *r, *t; | |
1059 | const char *f; | |
1060 | ||
1061 | assert(s); | |
1062 | ||
1063 | /* Does C style string escaping. */ | |
1064 | ||
1065 | if (!(r = new(char, strlen(s)*4 + 1))) | |
1066 | return NULL; | |
1067 | ||
1068 | for (f = s, t = r; *f; f++) | |
1069 | ||
1070 | switch (*f) { | |
1071 | ||
1072 | case '\a': | |
1073 | *(t++) = '\\'; | |
1074 | *(t++) = 'a'; | |
1075 | break; | |
1076 | case '\b': | |
1077 | *(t++) = '\\'; | |
1078 | *(t++) = 'b'; | |
1079 | break; | |
1080 | case '\f': | |
1081 | *(t++) = '\\'; | |
1082 | *(t++) = 'f'; | |
1083 | break; | |
1084 | case '\n': | |
1085 | *(t++) = '\\'; | |
1086 | *(t++) = 'n'; | |
1087 | break; | |
1088 | case '\r': | |
1089 | *(t++) = '\\'; | |
1090 | *(t++) = 'r'; | |
1091 | break; | |
1092 | case '\t': | |
1093 | *(t++) = '\\'; | |
1094 | *(t++) = 't'; | |
1095 | break; | |
1096 | case '\v': | |
1097 | *(t++) = '\\'; | |
1098 | *(t++) = 'v'; | |
1099 | break; | |
1100 | case '\\': | |
1101 | *(t++) = '\\'; | |
1102 | *(t++) = '\\'; | |
1103 | break; | |
1104 | case '"': | |
1105 | *(t++) = '\\'; | |
1106 | *(t++) = '"'; | |
1107 | break; | |
1108 | case '\'': | |
1109 | *(t++) = '\\'; | |
1110 | *(t++) = '\''; | |
1111 | break; | |
1112 | ||
1113 | default: | |
1114 | /* For special chars we prefer octal over | |
1115 | * hexadecimal encoding, simply because glib's | |
1116 | * g_strescape() does the same */ | |
1117 | if ((*f < ' ') || (*f >= 127)) { | |
1118 | *(t++) = '\\'; | |
1119 | *(t++) = octchar((unsigned char) *f >> 6); | |
1120 | *(t++) = octchar((unsigned char) *f >> 3); | |
1121 | *(t++) = octchar((unsigned char) *f); | |
1122 | } else | |
1123 | *(t++) = *f; | |
1124 | break; | |
1125 | } | |
1126 | ||
1127 | *t = 0; | |
1128 | ||
1129 | return r; | |
1130 | } | |
1131 | ||
1132 | char *cunescape(const char *s) { | |
1133 | char *r, *t; | |
1134 | const char *f; | |
1135 | ||
1136 | assert(s); | |
1137 | ||
1138 | /* Undoes C style string escaping */ | |
1139 | ||
1140 | if (!(r = new(char, strlen(s)+1))) | |
1141 | return r; | |
1142 | ||
1143 | for (f = s, t = r; *f; f++) { | |
1144 | ||
1145 | if (*f != '\\') { | |
1146 | *(t++) = *f; | |
1147 | continue; | |
1148 | } | |
1149 | ||
1150 | f++; | |
1151 | ||
1152 | switch (*f) { | |
1153 | ||
1154 | case 'a': | |
1155 | *(t++) = '\a'; | |
1156 | break; | |
1157 | case 'b': | |
1158 | *(t++) = '\b'; | |
1159 | break; | |
1160 | case 'f': | |
1161 | *(t++) = '\f'; | |
1162 | break; | |
1163 | case 'n': | |
1164 | *(t++) = '\n'; | |
1165 | break; | |
1166 | case 'r': | |
1167 | *(t++) = '\r'; | |
1168 | break; | |
1169 | case 't': | |
1170 | *(t++) = '\t'; | |
1171 | break; | |
1172 | case 'v': | |
1173 | *(t++) = '\v'; | |
1174 | break; | |
1175 | case '\\': | |
1176 | *(t++) = '\\'; | |
1177 | break; | |
1178 | case '"': | |
1179 | *(t++) = '"'; | |
1180 | break; | |
1181 | case '\'': | |
1182 | *(t++) = '\''; | |
1183 | break; | |
1184 | ||
1185 | case 'x': { | |
1186 | /* hexadecimal encoding */ | |
1187 | int a, b; | |
1188 | ||
1189 | if ((a = unhexchar(f[1])) < 0 || | |
1190 | (b = unhexchar(f[2])) < 0) { | |
1191 | /* Invalid escape code, let's take it literal then */ | |
1192 | *(t++) = '\\'; | |
1193 | *(t++) = 'x'; | |
1194 | } else { | |
1195 | *(t++) = (char) ((a << 4) | b); | |
1196 | f += 2; | |
1197 | } | |
1198 | ||
1199 | break; | |
1200 | } | |
1201 | ||
1202 | case '0': | |
1203 | case '1': | |
1204 | case '2': | |
1205 | case '3': | |
1206 | case '4': | |
1207 | case '5': | |
1208 | case '6': | |
1209 | case '7': { | |
1210 | /* octal encoding */ | |
1211 | int a, b, c; | |
1212 | ||
1213 | if ((a = unoctchar(f[0])) < 0 || | |
1214 | (b = unoctchar(f[1])) < 0 || | |
1215 | (c = unoctchar(f[2])) < 0) { | |
1216 | /* Invalid escape code, let's take it literal then */ | |
1217 | *(t++) = '\\'; | |
1218 | *(t++) = f[0]; | |
1219 | } else { | |
1220 | *(t++) = (char) ((a << 6) | (b << 3) | c); | |
1221 | f += 2; | |
1222 | } | |
1223 | ||
1224 | break; | |
1225 | } | |
1226 | ||
1227 | case 0: | |
1228 | /* premature end of string.*/ | |
1229 | *(t++) = '\\'; | |
1230 | goto finish; | |
1231 | ||
1232 | default: | |
1233 | /* Invalid escape code, let's take it literal then */ | |
1234 | *(t++) = '\\'; | |
f3d4cc01 | 1235 | *(t++) = *f; |
4fe88d28 LP |
1236 | break; |
1237 | } | |
1238 | } | |
1239 | ||
1240 | finish: | |
1241 | *t = 0; | |
1242 | return r; | |
1243 | } | |
1244 | ||
1245 | ||
1246 | char *xescape(const char *s, const char *bad) { | |
1247 | char *r, *t; | |
1248 | const char *f; | |
1249 | ||
1250 | /* Escapes all chars in bad, in addition to \ and all special | |
1251 | * chars, in \xFF style escaping. May be reversed with | |
1252 | * cunescape. */ | |
1253 | ||
1254 | if (!(r = new(char, strlen(s)*4+1))) | |
1255 | return NULL; | |
1256 | ||
1257 | for (f = s, t = r; *f; f++) { | |
1258 | ||
b866264a LP |
1259 | if ((*f < ' ') || (*f >= 127) || |
1260 | (*f == '\\') || strchr(bad, *f)) { | |
4fe88d28 LP |
1261 | *(t++) = '\\'; |
1262 | *(t++) = 'x'; | |
1263 | *(t++) = hexchar(*f >> 4); | |
1264 | *(t++) = hexchar(*f); | |
1265 | } else | |
1266 | *(t++) = *f; | |
1267 | } | |
1268 | ||
1269 | *t = 0; | |
1270 | ||
1271 | return r; | |
1272 | } | |
1273 | ||
ea430986 | 1274 | char *bus_path_escape(const char *s) { |
ea430986 LP |
1275 | char *r, *t; |
1276 | const char *f; | |
1277 | ||
47be870b LP |
1278 | assert(s); |
1279 | ||
ea430986 LP |
1280 | /* Escapes all chars that D-Bus' object path cannot deal |
1281 | * with. Can be reverse with bus_path_unescape() */ | |
1282 | ||
1283 | if (!(r = new(char, strlen(s)*3+1))) | |
1284 | return NULL; | |
1285 | ||
1286 | for (f = s, t = r; *f; f++) { | |
1287 | ||
1288 | if (!(*f >= 'A' && *f <= 'Z') && | |
1289 | !(*f >= 'a' && *f <= 'z') && | |
1290 | !(*f >= '0' && *f <= '9')) { | |
1291 | *(t++) = '_'; | |
1292 | *(t++) = hexchar(*f >> 4); | |
1293 | *(t++) = hexchar(*f); | |
1294 | } else | |
1295 | *(t++) = *f; | |
1296 | } | |
1297 | ||
1298 | *t = 0; | |
1299 | ||
1300 | return r; | |
1301 | } | |
1302 | ||
9e2f7c11 | 1303 | char *bus_path_unescape(const char *f) { |
ea430986 | 1304 | char *r, *t; |
ea430986 | 1305 | |
9e2f7c11 | 1306 | assert(f); |
47be870b | 1307 | |
9e2f7c11 | 1308 | if (!(r = strdup(f))) |
ea430986 LP |
1309 | return NULL; |
1310 | ||
9e2f7c11 | 1311 | for (t = r; *f; f++) { |
ea430986 LP |
1312 | |
1313 | if (*f == '_') { | |
1314 | int a, b; | |
1315 | ||
1316 | if ((a = unhexchar(f[1])) < 0 || | |
1317 | (b = unhexchar(f[2])) < 0) { | |
1318 | /* Invalid escape code, let's take it literal then */ | |
1319 | *(t++) = '_'; | |
1320 | } else { | |
1321 | *(t++) = (char) ((a << 4) | b); | |
1322 | f += 2; | |
1323 | } | |
1324 | } else | |
1325 | *(t++) = *f; | |
1326 | } | |
1327 | ||
1328 | *t = 0; | |
1329 | ||
1330 | return r; | |
1331 | } | |
1332 | ||
4fe88d28 LP |
1333 | char *path_kill_slashes(char *path) { |
1334 | char *f, *t; | |
1335 | bool slash = false; | |
1336 | ||
1337 | /* Removes redundant inner and trailing slashes. Modifies the | |
1338 | * passed string in-place. | |
1339 | * | |
1340 | * ///foo///bar/ becomes /foo/bar | |
1341 | */ | |
1342 | ||
1343 | for (f = path, t = path; *f; f++) { | |
1344 | ||
1345 | if (*f == '/') { | |
1346 | slash = true; | |
1347 | continue; | |
1348 | } | |
1349 | ||
1350 | if (slash) { | |
1351 | slash = false; | |
1352 | *(t++) = '/'; | |
1353 | } | |
1354 | ||
1355 | *(t++) = *f; | |
1356 | } | |
1357 | ||
1358 | /* Special rule, if we are talking of the root directory, a | |
1359 | trailing slash is good */ | |
1360 | ||
1361 | if (t == path && slash) | |
1362 | *(t++) = '/'; | |
1363 | ||
1364 | *t = 0; | |
1365 | return path; | |
1366 | } | |
1367 | ||
1368 | bool path_startswith(const char *path, const char *prefix) { | |
1369 | assert(path); | |
1370 | assert(prefix); | |
1371 | ||
1372 | if ((path[0] == '/') != (prefix[0] == '/')) | |
1373 | return false; | |
1374 | ||
1375 | for (;;) { | |
1376 | size_t a, b; | |
1377 | ||
1378 | path += strspn(path, "/"); | |
1379 | prefix += strspn(prefix, "/"); | |
1380 | ||
1381 | if (*prefix == 0) | |
1382 | return true; | |
1383 | ||
1384 | if (*path == 0) | |
1385 | return false; | |
1386 | ||
1387 | a = strcspn(path, "/"); | |
1388 | b = strcspn(prefix, "/"); | |
1389 | ||
1390 | if (a != b) | |
1391 | return false; | |
1392 | ||
1393 | if (memcmp(path, prefix, a) != 0) | |
1394 | return false; | |
1395 | ||
1396 | path += a; | |
1397 | prefix += b; | |
1398 | } | |
1399 | } | |
1400 | ||
15ae422b LP |
1401 | bool path_equal(const char *a, const char *b) { |
1402 | assert(a); | |
1403 | assert(b); | |
1404 | ||
1405 | if ((a[0] == '/') != (b[0] == '/')) | |
1406 | return false; | |
1407 | ||
1408 | for (;;) { | |
1409 | size_t j, k; | |
1410 | ||
1411 | a += strspn(a, "/"); | |
1412 | b += strspn(b, "/"); | |
1413 | ||
1414 | if (*a == 0 && *b == 0) | |
1415 | return true; | |
1416 | ||
1417 | if (*a == 0 || *b == 0) | |
1418 | return false; | |
1419 | ||
1420 | j = strcspn(a, "/"); | |
1421 | k = strcspn(b, "/"); | |
1422 | ||
1423 | if (j != k) | |
1424 | return false; | |
1425 | ||
1426 | if (memcmp(a, b, j) != 0) | |
1427 | return false; | |
1428 | ||
1429 | a += j; | |
1430 | b += k; | |
1431 | } | |
1432 | } | |
1433 | ||
67d51650 | 1434 | char *ascii_strlower(char *t) { |
4fe88d28 LP |
1435 | char *p; |
1436 | ||
67d51650 | 1437 | assert(t); |
4fe88d28 | 1438 | |
67d51650 | 1439 | for (p = t; *p; p++) |
4fe88d28 LP |
1440 | if (*p >= 'A' && *p <= 'Z') |
1441 | *p = *p - 'A' + 'a'; | |
1442 | ||
67d51650 | 1443 | return t; |
4fe88d28 | 1444 | } |
1dccbe19 | 1445 | |
c85dc17b LP |
1446 | bool ignore_file(const char *filename) { |
1447 | assert(filename); | |
1448 | ||
1449 | return | |
1450 | filename[0] == '.' || | |
6c78be3c | 1451 | streq(filename, "lost+found") || |
c85dc17b LP |
1452 | endswith(filename, "~") || |
1453 | endswith(filename, ".rpmnew") || | |
1454 | endswith(filename, ".rpmsave") || | |
1455 | endswith(filename, ".rpmorig") || | |
1456 | endswith(filename, ".dpkg-old") || | |
1457 | endswith(filename, ".dpkg-new") || | |
1458 | endswith(filename, ".swp"); | |
1459 | } | |
1460 | ||
3a0ecb08 LP |
1461 | int fd_nonblock(int fd, bool nonblock) { |
1462 | int flags; | |
1463 | ||
1464 | assert(fd >= 0); | |
1465 | ||
1466 | if ((flags = fcntl(fd, F_GETFL, 0)) < 0) | |
1467 | return -errno; | |
1468 | ||
1469 | if (nonblock) | |
1470 | flags |= O_NONBLOCK; | |
1471 | else | |
1472 | flags &= ~O_NONBLOCK; | |
1473 | ||
1474 | if (fcntl(fd, F_SETFL, flags) < 0) | |
1475 | return -errno; | |
1476 | ||
1477 | return 0; | |
1478 | } | |
1479 | ||
1480 | int fd_cloexec(int fd, bool cloexec) { | |
1481 | int flags; | |
1482 | ||
1483 | assert(fd >= 0); | |
1484 | ||
1485 | if ((flags = fcntl(fd, F_GETFD, 0)) < 0) | |
1486 | return -errno; | |
1487 | ||
1488 | if (cloexec) | |
1489 | flags |= FD_CLOEXEC; | |
1490 | else | |
1491 | flags &= ~FD_CLOEXEC; | |
1492 | ||
1493 | if (fcntl(fd, F_SETFD, flags) < 0) | |
1494 | return -errno; | |
1495 | ||
1496 | return 0; | |
1497 | } | |
1498 | ||
a0d40ac5 LP |
1499 | int close_all_fds(const int except[], unsigned n_except) { |
1500 | DIR *d; | |
1501 | struct dirent *de; | |
1502 | int r = 0; | |
1503 | ||
1504 | if (!(d = opendir("/proc/self/fd"))) | |
1505 | return -errno; | |
1506 | ||
1507 | while ((de = readdir(d))) { | |
a7610064 | 1508 | int fd = -1; |
a0d40ac5 | 1509 | |
a16e1123 | 1510 | if (ignore_file(de->d_name)) |
a0d40ac5 LP |
1511 | continue; |
1512 | ||
1513 | if ((r = safe_atoi(de->d_name, &fd)) < 0) | |
1514 | goto finish; | |
1515 | ||
1516 | if (fd < 3) | |
1517 | continue; | |
1518 | ||
1519 | if (fd == dirfd(d)) | |
1520 | continue; | |
1521 | ||
1522 | if (except) { | |
1523 | bool found; | |
1524 | unsigned i; | |
1525 | ||
1526 | found = false; | |
1527 | for (i = 0; i < n_except; i++) | |
1528 | if (except[i] == fd) { | |
1529 | found = true; | |
1530 | break; | |
1531 | } | |
1532 | ||
1533 | if (found) | |
1534 | continue; | |
1535 | } | |
1536 | ||
2f357920 LP |
1537 | if ((r = close_nointr(fd)) < 0) { |
1538 | /* Valgrind has its own FD and doesn't want to have it closed */ | |
1539 | if (errno != EBADF) | |
1540 | goto finish; | |
1541 | } | |
a0d40ac5 LP |
1542 | } |
1543 | ||
2f357920 LP |
1544 | r = 0; |
1545 | ||
a0d40ac5 LP |
1546 | finish: |
1547 | closedir(d); | |
1548 | return r; | |
1549 | } | |
1550 | ||
db12775d LP |
1551 | bool chars_intersect(const char *a, const char *b) { |
1552 | const char *p; | |
1553 | ||
1554 | /* Returns true if any of the chars in a are in b. */ | |
1555 | for (p = a; *p; p++) | |
1556 | if (strchr(b, *p)) | |
1557 | return true; | |
1558 | ||
1559 | return false; | |
1560 | } | |
1561 | ||
8b6c7120 LP |
1562 | char *format_timestamp(char *buf, size_t l, usec_t t) { |
1563 | struct tm tm; | |
1564 | time_t sec; | |
1565 | ||
1566 | assert(buf); | |
1567 | assert(l > 0); | |
1568 | ||
1569 | if (t <= 0) | |
1570 | return NULL; | |
1571 | ||
f872ec33 | 1572 | sec = (time_t) (t / USEC_PER_SEC); |
8b6c7120 LP |
1573 | |
1574 | if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0) | |
1575 | return NULL; | |
1576 | ||
1577 | return buf; | |
1578 | } | |
1579 | ||
871d7de4 LP |
1580 | char *format_timespan(char *buf, size_t l, usec_t t) { |
1581 | static const struct { | |
1582 | const char *suffix; | |
1583 | usec_t usec; | |
1584 | } table[] = { | |
1585 | { "w", USEC_PER_WEEK }, | |
1586 | { "d", USEC_PER_DAY }, | |
1587 | { "h", USEC_PER_HOUR }, | |
1588 | { "min", USEC_PER_MINUTE }, | |
1589 | { "s", USEC_PER_SEC }, | |
1590 | { "ms", USEC_PER_MSEC }, | |
1591 | { "us", 1 }, | |
1592 | }; | |
1593 | ||
1594 | unsigned i; | |
1595 | char *p = buf; | |
1596 | ||
1597 | assert(buf); | |
1598 | assert(l > 0); | |
1599 | ||
1600 | if (t == (usec_t) -1) | |
1601 | return NULL; | |
1602 | ||
1603 | /* The result of this function can be parsed with parse_usec */ | |
1604 | ||
1605 | for (i = 0; i < ELEMENTSOF(table); i++) { | |
1606 | int k; | |
1607 | size_t n; | |
1608 | ||
1609 | if (t < table[i].usec) | |
1610 | continue; | |
1611 | ||
1612 | if (l <= 1) | |
1613 | break; | |
1614 | ||
1615 | k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix); | |
1616 | n = MIN((size_t) k, l); | |
1617 | ||
1618 | l -= n; | |
1619 | p += n; | |
1620 | ||
1621 | t %= table[i].usec; | |
1622 | } | |
1623 | ||
1624 | *p = 0; | |
1625 | ||
1626 | return buf; | |
1627 | } | |
1628 | ||
42856c10 LP |
1629 | bool fstype_is_network(const char *fstype) { |
1630 | static const char * const table[] = { | |
1631 | "cifs", | |
1632 | "smbfs", | |
1633 | "ncpfs", | |
1634 | "nfs", | |
ca139f94 LP |
1635 | "nfs4", |
1636 | "gfs", | |
1637 | "gfs2" | |
42856c10 LP |
1638 | }; |
1639 | ||
1640 | unsigned i; | |
1641 | ||
1642 | for (i = 0; i < ELEMENTSOF(table); i++) | |
1643 | if (streq(table[i], fstype)) | |
1644 | return true; | |
1645 | ||
1646 | return false; | |
1647 | } | |
1648 | ||
601f6a1e LP |
1649 | int chvt(int vt) { |
1650 | int fd, r = 0; | |
1651 | ||
1652 | if ((fd = open("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0) | |
1653 | return -errno; | |
1654 | ||
1655 | if (vt < 0) { | |
1656 | int tiocl[2] = { | |
1657 | TIOCL_GETKMSGREDIRECT, | |
1658 | 0 | |
1659 | }; | |
1660 | ||
1661 | if (ioctl(fd, TIOCLINUX, tiocl) < 0) | |
1662 | return -errno; | |
1663 | ||
1664 | vt = tiocl[0] <= 0 ? 1 : tiocl[0]; | |
1665 | } | |
1666 | ||
1667 | if (ioctl(fd, VT_ACTIVATE, vt) < 0) | |
1668 | r = -errno; | |
1669 | ||
a16e1123 | 1670 | close_nointr_nofail(r); |
601f6a1e LP |
1671 | return r; |
1672 | } | |
1673 | ||
80876c20 LP |
1674 | int read_one_char(FILE *f, char *ret, bool *need_nl) { |
1675 | struct termios old_termios, new_termios; | |
1676 | char c; | |
1677 | char line[1024]; | |
1678 | ||
1679 | assert(f); | |
1680 | assert(ret); | |
1681 | ||
1682 | if (tcgetattr(fileno(f), &old_termios) >= 0) { | |
1683 | new_termios = old_termios; | |
1684 | ||
1685 | new_termios.c_lflag &= ~ICANON; | |
1686 | new_termios.c_cc[VMIN] = 1; | |
1687 | new_termios.c_cc[VTIME] = 0; | |
1688 | ||
1689 | if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) { | |
1690 | size_t k; | |
1691 | ||
1692 | k = fread(&c, 1, 1, f); | |
1693 | ||
1694 | tcsetattr(fileno(f), TCSADRAIN, &old_termios); | |
1695 | ||
1696 | if (k <= 0) | |
1697 | return -EIO; | |
1698 | ||
1699 | if (need_nl) | |
1700 | *need_nl = c != '\n'; | |
1701 | ||
1702 | *ret = c; | |
1703 | return 0; | |
1704 | } | |
1705 | } | |
1706 | ||
1707 | if (!(fgets(line, sizeof(line), f))) | |
1708 | return -EIO; | |
1709 | ||
1710 | truncate_nl(line); | |
1711 | ||
1712 | if (strlen(line) != 1) | |
1713 | return -EBADMSG; | |
1714 | ||
1715 | if (need_nl) | |
1716 | *need_nl = false; | |
1717 | ||
1718 | *ret = line[0]; | |
1719 | return 0; | |
1720 | } | |
1721 | ||
1722 | int ask(char *ret, const char *replies, const char *text, ...) { | |
1723 | assert(ret); | |
1724 | assert(replies); | |
1725 | assert(text); | |
1726 | ||
1727 | for (;;) { | |
1728 | va_list ap; | |
1729 | char c; | |
1730 | int r; | |
1731 | bool need_nl = true; | |
1732 | ||
b1b2dc0c LP |
1733 | fputs("\x1B[1m", stdout); |
1734 | ||
80876c20 LP |
1735 | va_start(ap, text); |
1736 | vprintf(text, ap); | |
1737 | va_end(ap); | |
1738 | ||
b1b2dc0c LP |
1739 | fputs("\x1B[0m", stdout); |
1740 | ||
80876c20 LP |
1741 | fflush(stdout); |
1742 | ||
1743 | if ((r = read_one_char(stdin, &c, &need_nl)) < 0) { | |
1744 | ||
1745 | if (r == -EBADMSG) { | |
1746 | puts("Bad input, please try again."); | |
1747 | continue; | |
1748 | } | |
1749 | ||
1750 | putchar('\n'); | |
1751 | return r; | |
1752 | } | |
1753 | ||
1754 | if (need_nl) | |
1755 | putchar('\n'); | |
1756 | ||
1757 | if (strchr(replies, c)) { | |
1758 | *ret = c; | |
1759 | return 0; | |
1760 | } | |
1761 | ||
1762 | puts("Read unexpected character, please try again."); | |
1763 | } | |
1764 | } | |
1765 | ||
1766 | int reset_terminal(int fd) { | |
1767 | struct termios termios; | |
1768 | int r = 0; | |
1769 | ||
1770 | assert(fd >= 0); | |
1771 | ||
aaf694ca | 1772 | /* Set terminal to some sane defaults */ |
80876c20 LP |
1773 | |
1774 | if (tcgetattr(fd, &termios) < 0) { | |
1775 | r = -errno; | |
1776 | goto finish; | |
1777 | } | |
1778 | ||
aaf694ca LP |
1779 | /* We only reset the stuff that matters to the software. How |
1780 | * hardware is set up we don't touch assuming that somebody | |
1781 | * else will do that for us */ | |
1782 | ||
1783 | termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC); | |
80876c20 LP |
1784 | termios.c_iflag |= ICRNL | IMAXBEL | IUTF8; |
1785 | termios.c_oflag |= ONLCR; | |
1786 | termios.c_cflag |= CREAD; | |
1787 | termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE; | |
1788 | ||
1789 | termios.c_cc[VINTR] = 03; /* ^C */ | |
1790 | termios.c_cc[VQUIT] = 034; /* ^\ */ | |
1791 | termios.c_cc[VERASE] = 0177; | |
1792 | termios.c_cc[VKILL] = 025; /* ^X */ | |
1793 | termios.c_cc[VEOF] = 04; /* ^D */ | |
1794 | termios.c_cc[VSTART] = 021; /* ^Q */ | |
1795 | termios.c_cc[VSTOP] = 023; /* ^S */ | |
1796 | termios.c_cc[VSUSP] = 032; /* ^Z */ | |
1797 | termios.c_cc[VLNEXT] = 026; /* ^V */ | |
1798 | termios.c_cc[VWERASE] = 027; /* ^W */ | |
1799 | termios.c_cc[VREPRINT] = 022; /* ^R */ | |
aaf694ca LP |
1800 | termios.c_cc[VEOL] = 0; |
1801 | termios.c_cc[VEOL2] = 0; | |
80876c20 LP |
1802 | |
1803 | termios.c_cc[VTIME] = 0; | |
1804 | termios.c_cc[VMIN] = 1; | |
1805 | ||
1806 | if (tcsetattr(fd, TCSANOW, &termios) < 0) | |
1807 | r = -errno; | |
1808 | ||
1809 | finish: | |
1810 | /* Just in case, flush all crap out */ | |
1811 | tcflush(fd, TCIOFLUSH); | |
1812 | ||
1813 | return r; | |
1814 | } | |
1815 | ||
1816 | int open_terminal(const char *name, int mode) { | |
1817 | int fd, r; | |
1818 | ||
1819 | if ((fd = open(name, mode)) < 0) | |
1820 | return -errno; | |
1821 | ||
1822 | if ((r = isatty(fd)) < 0) { | |
1823 | close_nointr_nofail(fd); | |
1824 | return -errno; | |
1825 | } | |
1826 | ||
1827 | if (!r) { | |
1828 | close_nointr_nofail(fd); | |
1829 | return -ENOTTY; | |
1830 | } | |
1831 | ||
1832 | return fd; | |
1833 | } | |
1834 | ||
1835 | int flush_fd(int fd) { | |
1836 | struct pollfd pollfd; | |
1837 | ||
1838 | zero(pollfd); | |
1839 | pollfd.fd = fd; | |
1840 | pollfd.events = POLLIN; | |
1841 | ||
1842 | for (;;) { | |
1843 | char buf[1024]; | |
1844 | ssize_t l; | |
1845 | int r; | |
1846 | ||
1847 | if ((r = poll(&pollfd, 1, 0)) < 0) { | |
1848 | ||
1849 | if (errno == EINTR) | |
1850 | continue; | |
1851 | ||
1852 | return -errno; | |
1853 | } | |
1854 | ||
1855 | if (r == 0) | |
1856 | return 0; | |
1857 | ||
1858 | if ((l = read(fd, buf, sizeof(buf))) < 0) { | |
1859 | ||
1860 | if (errno == EINTR) | |
1861 | continue; | |
1862 | ||
1863 | if (errno == EAGAIN) | |
1864 | return 0; | |
1865 | ||
1866 | return -errno; | |
1867 | } | |
1868 | ||
1869 | if (l <= 0) | |
1870 | return 0; | |
1871 | } | |
1872 | } | |
1873 | ||
21de3988 | 1874 | int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) { |
bab45044 | 1875 | int fd = -1, notify = -1, r, wd = -1; |
80876c20 LP |
1876 | |
1877 | assert(name); | |
1878 | ||
1879 | /* We use inotify to be notified when the tty is closed. We | |
1880 | * create the watch before checking if we can actually acquire | |
1881 | * it, so that we don't lose any event. | |
1882 | * | |
1883 | * Note: strictly speaking this actually watches for the | |
1884 | * device being closed, it does *not* really watch whether a | |
1885 | * tty loses its controlling process. However, unless some | |
1886 | * rogue process uses TIOCNOTTY on /dev/tty *after* closing | |
1887 | * its tty otherwise this will not become a problem. As long | |
1888 | * as the administrator makes sure not configure any service | |
1889 | * on the same tty as an untrusted user this should not be a | |
1890 | * problem. (Which he probably should not do anyway.) */ | |
1891 | ||
1892 | if (!fail && !force) { | |
1893 | if ((notify = inotify_init1(IN_CLOEXEC)) < 0) { | |
1894 | r = -errno; | |
1895 | goto fail; | |
1896 | } | |
1897 | ||
1898 | if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) { | |
1899 | r = -errno; | |
1900 | goto fail; | |
1901 | } | |
1902 | } | |
1903 | ||
1904 | for (;;) { | |
e3d1855b LP |
1905 | if (notify >= 0) |
1906 | if ((r = flush_fd(notify)) < 0) | |
1907 | goto fail; | |
80876c20 LP |
1908 | |
1909 | /* We pass here O_NOCTTY only so that we can check the return | |
1910 | * value TIOCSCTTY and have a reliable way to figure out if we | |
1911 | * successfully became the controlling process of the tty */ | |
1912 | if ((fd = open_terminal(name, O_RDWR|O_NOCTTY)) < 0) | |
1913 | return -errno; | |
1914 | ||
1915 | /* First, try to get the tty */ | |
21de3988 LP |
1916 | r = ioctl(fd, TIOCSCTTY, force); |
1917 | ||
1918 | /* Sometimes it makes sense to ignore TIOCSCTTY | |
1919 | * returning EPERM, i.e. when very likely we already | |
1920 | * are have this controlling terminal. */ | |
1921 | if (r < 0 && errno == EPERM && ignore_tiocstty_eperm) | |
1922 | r = 0; | |
1923 | ||
1924 | if (r < 0 && (force || fail || errno != EPERM)) { | |
80876c20 LP |
1925 | r = -errno; |
1926 | goto fail; | |
1927 | } | |
1928 | ||
1929 | if (r >= 0) | |
1930 | break; | |
1931 | ||
1932 | assert(!fail); | |
1933 | assert(!force); | |
1934 | assert(notify >= 0); | |
1935 | ||
1936 | for (;;) { | |
1937 | struct inotify_event e; | |
1938 | ssize_t l; | |
1939 | ||
1940 | if ((l = read(notify, &e, sizeof(e))) != sizeof(e)) { | |
1941 | ||
1942 | if (l < 0) { | |
1943 | ||
1944 | if (errno == EINTR) | |
1945 | continue; | |
1946 | ||
1947 | r = -errno; | |
1948 | } else | |
1949 | r = -EIO; | |
1950 | ||
1951 | goto fail; | |
1952 | } | |
1953 | ||
1954 | if (e.wd != wd || !(e.mask & IN_CLOSE)) { | |
1955 | r = -errno; | |
1956 | goto fail; | |
1957 | } | |
1958 | ||
1959 | break; | |
1960 | } | |
1961 | ||
1962 | /* We close the tty fd here since if the old session | |
1963 | * ended our handle will be dead. It's important that | |
1964 | * we do this after sleeping, so that we don't enter | |
1965 | * an endless loop. */ | |
1966 | close_nointr_nofail(fd); | |
1967 | } | |
1968 | ||
1969 | if (notify >= 0) | |
a16e1123 | 1970 | close_nointr_nofail(notify); |
80876c20 LP |
1971 | |
1972 | if ((r = reset_terminal(fd)) < 0) | |
1973 | log_warning("Failed to reset terminal: %s", strerror(-r)); | |
1974 | ||
1975 | return fd; | |
1976 | ||
1977 | fail: | |
1978 | if (fd >= 0) | |
a16e1123 | 1979 | close_nointr_nofail(fd); |
80876c20 LP |
1980 | |
1981 | if (notify >= 0) | |
a16e1123 | 1982 | close_nointr_nofail(notify); |
80876c20 LP |
1983 | |
1984 | return r; | |
1985 | } | |
1986 | ||
1987 | int release_terminal(void) { | |
1988 | int r = 0, fd; | |
57cd2192 | 1989 | struct sigaction sa_old, sa_new; |
80876c20 | 1990 | |
57cd2192 | 1991 | if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0) |
80876c20 LP |
1992 | return -errno; |
1993 | ||
57cd2192 LP |
1994 | /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed |
1995 | * by our own TIOCNOTTY */ | |
1996 | ||
1997 | zero(sa_new); | |
1998 | sa_new.sa_handler = SIG_IGN; | |
1999 | sa_new.sa_flags = SA_RESTART; | |
2000 | assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0); | |
2001 | ||
80876c20 LP |
2002 | if (ioctl(fd, TIOCNOTTY) < 0) |
2003 | r = -errno; | |
2004 | ||
57cd2192 LP |
2005 | assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0); |
2006 | ||
80876c20 LP |
2007 | close_nointr_nofail(fd); |
2008 | return r; | |
2009 | } | |
2010 | ||
9a34ec5f LP |
2011 | int sigaction_many(const struct sigaction *sa, ...) { |
2012 | va_list ap; | |
2013 | int r = 0, sig; | |
2014 | ||
2015 | va_start(ap, sa); | |
2016 | while ((sig = va_arg(ap, int)) > 0) | |
2017 | if (sigaction(sig, sa, NULL) < 0) | |
2018 | r = -errno; | |
2019 | va_end(ap); | |
2020 | ||
2021 | return r; | |
2022 | } | |
2023 | ||
2024 | int ignore_signals(int sig, ...) { | |
a337c6fc | 2025 | struct sigaction sa; |
9a34ec5f LP |
2026 | va_list ap; |
2027 | int r = 0; | |
a337c6fc LP |
2028 | |
2029 | zero(sa); | |
2030 | sa.sa_handler = SIG_IGN; | |
2031 | sa.sa_flags = SA_RESTART; | |
2032 | ||
9a34ec5f LP |
2033 | if (sigaction(sig, &sa, NULL) < 0) |
2034 | r = -errno; | |
2035 | ||
2036 | va_start(ap, sig); | |
2037 | while ((sig = va_arg(ap, int)) > 0) | |
2038 | if (sigaction(sig, &sa, NULL) < 0) | |
2039 | r = -errno; | |
2040 | va_end(ap); | |
2041 | ||
2042 | return r; | |
2043 | } | |
2044 | ||
2045 | int default_signals(int sig, ...) { | |
2046 | struct sigaction sa; | |
2047 | va_list ap; | |
2048 | int r = 0; | |
2049 | ||
2050 | zero(sa); | |
2051 | sa.sa_handler = SIG_DFL; | |
2052 | sa.sa_flags = SA_RESTART; | |
2053 | ||
2054 | if (sigaction(sig, &sa, NULL) < 0) | |
2055 | r = -errno; | |
2056 | ||
2057 | va_start(ap, sig); | |
2058 | while ((sig = va_arg(ap, int)) > 0) | |
2059 | if (sigaction(sig, &sa, NULL) < 0) | |
2060 | r = -errno; | |
2061 | va_end(ap); | |
2062 | ||
2063 | return r; | |
a337c6fc LP |
2064 | } |
2065 | ||
8d567588 LP |
2066 | int close_pipe(int p[]) { |
2067 | int a = 0, b = 0; | |
2068 | ||
2069 | assert(p); | |
2070 | ||
2071 | if (p[0] >= 0) { | |
2072 | a = close_nointr(p[0]); | |
2073 | p[0] = -1; | |
2074 | } | |
2075 | ||
2076 | if (p[1] >= 0) { | |
2077 | b = close_nointr(p[1]); | |
2078 | p[1] = -1; | |
2079 | } | |
2080 | ||
2081 | return a < 0 ? a : b; | |
2082 | } | |
2083 | ||
eb22ac37 | 2084 | ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) { |
8d567588 LP |
2085 | uint8_t *p; |
2086 | ssize_t n = 0; | |
2087 | ||
2088 | assert(fd >= 0); | |
2089 | assert(buf); | |
2090 | ||
2091 | p = buf; | |
2092 | ||
2093 | while (nbytes > 0) { | |
2094 | ssize_t k; | |
2095 | ||
2096 | if ((k = read(fd, p, nbytes)) <= 0) { | |
2097 | ||
eb22ac37 | 2098 | if (k < 0 && errno == EINTR) |
8d567588 LP |
2099 | continue; |
2100 | ||
eb22ac37 | 2101 | if (k < 0 && errno == EAGAIN && do_poll) { |
8d567588 LP |
2102 | struct pollfd pollfd; |
2103 | ||
2104 | zero(pollfd); | |
2105 | pollfd.fd = fd; | |
2106 | pollfd.events = POLLIN; | |
2107 | ||
2108 | if (poll(&pollfd, 1, -1) < 0) { | |
2109 | if (errno == EINTR) | |
2110 | continue; | |
2111 | ||
2112 | return n > 0 ? n : -errno; | |
2113 | } | |
2114 | ||
2115 | if (pollfd.revents != POLLIN) | |
2116 | return n > 0 ? n : -EIO; | |
2117 | ||
2118 | continue; | |
2119 | } | |
2120 | ||
2121 | return n > 0 ? n : (k < 0 ? -errno : 0); | |
2122 | } | |
2123 | ||
2124 | p += k; | |
2125 | nbytes -= k; | |
2126 | n += k; | |
2127 | } | |
2128 | ||
2129 | return n; | |
2130 | } | |
2131 | ||
eb22ac37 LP |
2132 | ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) { |
2133 | const uint8_t *p; | |
2134 | ssize_t n = 0; | |
2135 | ||
2136 | assert(fd >= 0); | |
2137 | assert(buf); | |
2138 | ||
2139 | p = buf; | |
2140 | ||
2141 | while (nbytes > 0) { | |
2142 | ssize_t k; | |
2143 | ||
2144 | if ((k = write(fd, p, nbytes)) <= 0) { | |
2145 | ||
2146 | if (k < 0 && errno == EINTR) | |
2147 | continue; | |
2148 | ||
2149 | if (k < 0 && errno == EAGAIN && do_poll) { | |
2150 | struct pollfd pollfd; | |
2151 | ||
2152 | zero(pollfd); | |
2153 | pollfd.fd = fd; | |
2154 | pollfd.events = POLLOUT; | |
2155 | ||
2156 | if (poll(&pollfd, 1, -1) < 0) { | |
2157 | if (errno == EINTR) | |
2158 | continue; | |
2159 | ||
2160 | return n > 0 ? n : -errno; | |
2161 | } | |
2162 | ||
2163 | if (pollfd.revents != POLLOUT) | |
2164 | return n > 0 ? n : -EIO; | |
2165 | ||
2166 | continue; | |
2167 | } | |
2168 | ||
2169 | return n > 0 ? n : (k < 0 ? -errno : 0); | |
2170 | } | |
2171 | ||
2172 | p += k; | |
2173 | nbytes -= k; | |
2174 | n += k; | |
2175 | } | |
2176 | ||
2177 | return n; | |
2178 | } | |
2179 | ||
8d567588 LP |
2180 | int path_is_mount_point(const char *t) { |
2181 | struct stat a, b; | |
2182 | char *copy; | |
2183 | ||
2184 | if (lstat(t, &a) < 0) { | |
2185 | ||
2186 | if (errno == ENOENT) | |
2187 | return 0; | |
2188 | ||
2189 | return -errno; | |
2190 | } | |
2191 | ||
2192 | if (!(copy = strdup(t))) | |
2193 | return -ENOMEM; | |
2194 | ||
2195 | if (lstat(dirname(copy), &b) < 0) { | |
2196 | free(copy); | |
2197 | return -errno; | |
2198 | } | |
2199 | ||
2200 | free(copy); | |
2201 | ||
2202 | return a.st_dev != b.st_dev; | |
2203 | } | |
2204 | ||
24a6e4a4 LP |
2205 | int parse_usec(const char *t, usec_t *usec) { |
2206 | static const struct { | |
2207 | const char *suffix; | |
2208 | usec_t usec; | |
2209 | } table[] = { | |
2210 | { "sec", USEC_PER_SEC }, | |
2211 | { "s", USEC_PER_SEC }, | |
2212 | { "min", USEC_PER_MINUTE }, | |
2213 | { "hr", USEC_PER_HOUR }, | |
2214 | { "h", USEC_PER_HOUR }, | |
2215 | { "d", USEC_PER_DAY }, | |
2216 | { "w", USEC_PER_WEEK }, | |
2217 | { "msec", USEC_PER_MSEC }, | |
2218 | { "ms", USEC_PER_MSEC }, | |
2219 | { "m", USEC_PER_MINUTE }, | |
2220 | { "usec", 1ULL }, | |
2221 | { "us", 1ULL }, | |
2222 | { "", USEC_PER_SEC }, | |
2223 | }; | |
2224 | ||
2225 | const char *p; | |
2226 | usec_t r = 0; | |
2227 | ||
2228 | assert(t); | |
2229 | assert(usec); | |
2230 | ||
2231 | p = t; | |
2232 | do { | |
2233 | long long l; | |
2234 | char *e; | |
2235 | unsigned i; | |
2236 | ||
2237 | errno = 0; | |
2238 | l = strtoll(p, &e, 10); | |
2239 | ||
2240 | if (errno != 0) | |
2241 | return -errno; | |
2242 | ||
2243 | if (l < 0) | |
2244 | return -ERANGE; | |
2245 | ||
2246 | if (e == p) | |
2247 | return -EINVAL; | |
2248 | ||
2249 | e += strspn(e, WHITESPACE); | |
2250 | ||
2251 | for (i = 0; i < ELEMENTSOF(table); i++) | |
2252 | if (startswith(e, table[i].suffix)) { | |
2253 | r += (usec_t) l * table[i].usec; | |
2254 | p = e + strlen(table[i].suffix); | |
2255 | break; | |
2256 | } | |
2257 | ||
2258 | if (i >= ELEMENTSOF(table)) | |
2259 | return -EINVAL; | |
2260 | ||
2261 | } while (*p != 0); | |
2262 | ||
2263 | *usec = r; | |
2264 | ||
2265 | return 0; | |
2266 | } | |
2267 | ||
843d2643 LP |
2268 | int make_stdio(int fd) { |
2269 | int r, s, t; | |
2270 | ||
2271 | assert(fd >= 0); | |
2272 | ||
2273 | r = dup2(fd, STDIN_FILENO); | |
2274 | s = dup2(fd, STDOUT_FILENO); | |
2275 | t = dup2(fd, STDERR_FILENO); | |
2276 | ||
2277 | if (fd >= 3) | |
2278 | close_nointr_nofail(fd); | |
2279 | ||
2280 | if (r < 0 || s < 0 || t < 0) | |
2281 | return -errno; | |
2282 | ||
2283 | return 0; | |
2284 | } | |
2285 | ||
cb8a8f78 LP |
2286 | bool is_clean_exit(int code, int status) { |
2287 | ||
2288 | if (code == CLD_EXITED) | |
2289 | return status == 0; | |
2290 | ||
2291 | /* If a daemon does not implement handlers for some of the | |
2292 | * signals that's not considered an unclean shutdown */ | |
2293 | if (code == CLD_KILLED) | |
2294 | return | |
2295 | status == SIGHUP || | |
2296 | status == SIGINT || | |
2297 | status == SIGTERM || | |
2298 | status == SIGPIPE; | |
2299 | ||
2300 | return false; | |
2301 | } | |
2302 | ||
8407a5d0 LP |
2303 | bool is_device_path(const char *path) { |
2304 | ||
2305 | /* Returns true on paths that refer to a device, either in | |
2306 | * sysfs or in /dev */ | |
2307 | ||
2308 | return | |
2309 | path_startswith(path, "/dev/") || | |
2310 | path_startswith(path, "/sys/"); | |
2311 | } | |
2312 | ||
01f78473 LP |
2313 | int dir_is_empty(const char *path) { |
2314 | DIR *d; | |
2315 | int r; | |
2316 | struct dirent buf, *de; | |
2317 | ||
2318 | if (!(d = opendir(path))) | |
2319 | return -errno; | |
2320 | ||
2321 | for (;;) { | |
2322 | if ((r = readdir_r(d, &buf, &de)) > 0) { | |
2323 | r = -r; | |
2324 | break; | |
2325 | } | |
2326 | ||
2327 | if (!de) { | |
2328 | r = 1; | |
2329 | break; | |
2330 | } | |
2331 | ||
2332 | if (!ignore_file(de->d_name)) { | |
2333 | r = 0; | |
2334 | break; | |
2335 | } | |
2336 | } | |
2337 | ||
2338 | closedir(d); | |
2339 | return r; | |
2340 | } | |
2341 | ||
d3782d60 LP |
2342 | unsigned long long random_ull(void) { |
2343 | int fd; | |
2344 | uint64_t ull; | |
2345 | ssize_t r; | |
2346 | ||
2347 | if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) | |
2348 | goto fallback; | |
2349 | ||
eb22ac37 | 2350 | r = loop_read(fd, &ull, sizeof(ull), true); |
d3782d60 LP |
2351 | close_nointr_nofail(fd); |
2352 | ||
2353 | if (r != sizeof(ull)) | |
2354 | goto fallback; | |
2355 | ||
2356 | return ull; | |
2357 | ||
2358 | fallback: | |
2359 | return random() * RAND_MAX + random(); | |
2360 | } | |
2361 | ||
5b6319dc LP |
2362 | void rename_process(const char name[8]) { |
2363 | assert(name); | |
2364 | ||
2365 | prctl(PR_SET_NAME, name); | |
2366 | ||
2367 | /* This is a like a poor man's setproctitle(). The string | |
2368 | * passed should fit in 7 chars (i.e. the length of | |
2369 | * "systemd") */ | |
2370 | ||
2371 | if (program_invocation_name) | |
2372 | strncpy(program_invocation_name, name, strlen(program_invocation_name)); | |
2373 | } | |
2374 | ||
7d793605 LP |
2375 | void sigset_add_many(sigset_t *ss, ...) { |
2376 | va_list ap; | |
2377 | int sig; | |
2378 | ||
2379 | assert(ss); | |
2380 | ||
2381 | va_start(ap, ss); | |
2382 | while ((sig = va_arg(ap, int)) > 0) | |
2383 | assert_se(sigaddset(ss, sig) == 0); | |
2384 | va_end(ap); | |
2385 | } | |
2386 | ||
ef2f1067 LP |
2387 | char* gethostname_malloc(void) { |
2388 | struct utsname u; | |
2389 | ||
2390 | assert_se(uname(&u) >= 0); | |
2391 | ||
2392 | if (u.nodename[0]) | |
2393 | return strdup(u.nodename); | |
2394 | ||
2395 | return strdup(u.sysname); | |
2396 | } | |
2397 | ||
8c6db833 LP |
2398 | int getmachineid_malloc(char **b) { |
2399 | int r; | |
2400 | ||
2401 | assert(b); | |
2402 | ||
2403 | if ((r = read_one_line_file("/var/lib/dbus/machine-id", b)) < 0) | |
2404 | return r; | |
2405 | ||
2406 | strstrip(*b); | |
2407 | return 0; | |
2408 | } | |
2409 | ||
ef2f1067 LP |
2410 | char* getlogname_malloc(void) { |
2411 | uid_t uid; | |
2412 | long bufsize; | |
2413 | char *buf, *name; | |
2414 | struct passwd pwbuf, *pw = NULL; | |
2415 | struct stat st; | |
2416 | ||
2417 | if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0) | |
2418 | uid = st.st_uid; | |
2419 | else | |
2420 | uid = getuid(); | |
2421 | ||
2422 | /* Shortcut things to avoid NSS lookups */ | |
2423 | if (uid == 0) | |
2424 | return strdup("root"); | |
2425 | ||
2426 | if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0) | |
2427 | bufsize = 4096; | |
2428 | ||
2429 | if (!(buf = malloc(bufsize))) | |
2430 | return NULL; | |
2431 | ||
2432 | if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) { | |
2433 | name = strdup(pw->pw_name); | |
2434 | free(buf); | |
2435 | return name; | |
2436 | } | |
2437 | ||
2438 | free(buf); | |
2439 | ||
2440 | if (asprintf(&name, "%lu", (unsigned long) uid) < 0) | |
2441 | return NULL; | |
2442 | ||
2443 | return name; | |
2444 | } | |
2445 | ||
8c6db833 LP |
2446 | int getttyname_malloc(char **r) { |
2447 | char path[PATH_MAX], *p, *c; | |
2448 | ||
2449 | assert(r); | |
ef2f1067 LP |
2450 | |
2451 | if (ttyname_r(STDIN_FILENO, path, sizeof(path)) < 0) | |
8c6db833 | 2452 | return -errno; |
ef2f1067 LP |
2453 | |
2454 | char_array_0(path); | |
2455 | ||
2456 | p = path; | |
2457 | if (startswith(path, "/dev/")) | |
2458 | p += 5; | |
2459 | ||
8c6db833 LP |
2460 | if (!(c = strdup(p))) |
2461 | return -ENOMEM; | |
2462 | ||
2463 | *r = c; | |
2464 | return 0; | |
2465 | } | |
2466 | ||
2467 | static int rm_rf_children(int fd, bool only_dirs) { | |
2468 | DIR *d; | |
2469 | int ret = 0; | |
2470 | ||
2471 | assert(fd >= 0); | |
2472 | ||
2473 | /* This returns the first error we run into, but nevertheless | |
2474 | * tries to go on */ | |
2475 | ||
2476 | if (!(d = fdopendir(fd))) { | |
2477 | close_nointr_nofail(fd); | |
2478 | return -errno; | |
2479 | } | |
2480 | ||
2481 | for (;;) { | |
2482 | struct dirent buf, *de; | |
2483 | bool is_dir; | |
2484 | int r; | |
2485 | ||
2486 | if ((r = readdir_r(d, &buf, &de)) != 0) { | |
2487 | if (ret == 0) | |
2488 | ret = -r; | |
2489 | break; | |
2490 | } | |
2491 | ||
2492 | if (!de) | |
2493 | break; | |
2494 | ||
2495 | if (streq(de->d_name, ".") || streq(de->d_name, "..")) | |
2496 | continue; | |
2497 | ||
2498 | if (de->d_type == DT_UNKNOWN) { | |
2499 | struct stat st; | |
2500 | ||
2501 | if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { | |
2502 | if (ret == 0) | |
2503 | ret = -errno; | |
2504 | continue; | |
2505 | } | |
2506 | ||
2507 | is_dir = S_ISDIR(st.st_mode); | |
2508 | } else | |
2509 | is_dir = de->d_type == DT_DIR; | |
2510 | ||
2511 | if (is_dir) { | |
2512 | int subdir_fd; | |
2513 | ||
2514 | if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) { | |
2515 | if (ret == 0) | |
2516 | ret = -errno; | |
2517 | continue; | |
2518 | } | |
2519 | ||
2520 | if ((r = rm_rf_children(subdir_fd, only_dirs)) < 0) { | |
2521 | if (ret == 0) | |
2522 | ret = r; | |
2523 | } | |
2524 | ||
2525 | if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) { | |
2526 | if (ret == 0) | |
2527 | ret = -errno; | |
2528 | } | |
2529 | } else if (!only_dirs) { | |
2530 | ||
2531 | if (unlinkat(fd, de->d_name, 0) < 0) { | |
2532 | if (ret == 0) | |
2533 | ret = -errno; | |
2534 | } | |
2535 | } | |
2536 | } | |
2537 | ||
2538 | closedir(d); | |
2539 | ||
2540 | return ret; | |
2541 | } | |
2542 | ||
2543 | int rm_rf(const char *path, bool only_dirs, bool delete_root) { | |
2544 | int fd; | |
2545 | int r; | |
2546 | ||
2547 | assert(path); | |
2548 | ||
2549 | if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) { | |
2550 | ||
2551 | if (errno != ENOTDIR) | |
2552 | return -errno; | |
2553 | ||
2554 | if (delete_root && !only_dirs) | |
2555 | if (unlink(path) < 0) | |
2556 | return -errno; | |
2557 | ||
2558 | return 0; | |
2559 | } | |
2560 | ||
2561 | r = rm_rf_children(fd, only_dirs); | |
2562 | ||
2563 | if (delete_root) | |
2564 | if (rmdir(path) < 0) { | |
2565 | if (r == 0) | |
2566 | r = -errno; | |
2567 | } | |
2568 | ||
2569 | return r; | |
2570 | } | |
2571 | ||
2572 | int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { | |
2573 | assert(path); | |
2574 | ||
2575 | /* Under the assumption that we are running privileged we | |
2576 | * first change the access mode and only then hand out | |
2577 | * ownership to avoid a window where access is too open. */ | |
2578 | ||
2579 | if (chmod(path, mode) < 0) | |
2580 | return -errno; | |
2581 | ||
2582 | if (chown(path, uid, gid) < 0) | |
2583 | return -errno; | |
2584 | ||
2585 | return 0; | |
ef2f1067 LP |
2586 | } |
2587 | ||
82c121a4 LP |
2588 | cpu_set_t* cpu_set_malloc(unsigned *ncpus) { |
2589 | cpu_set_t *r; | |
2590 | unsigned n = 1024; | |
2591 | ||
2592 | /* Allocates the cpuset in the right size */ | |
2593 | ||
2594 | for (;;) { | |
2595 | if (!(r = CPU_ALLOC(n))) | |
2596 | return NULL; | |
2597 | ||
2598 | if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) { | |
2599 | CPU_ZERO_S(CPU_ALLOC_SIZE(n), r); | |
2600 | ||
2601 | if (ncpus) | |
2602 | *ncpus = n; | |
2603 | ||
2604 | return r; | |
2605 | } | |
2606 | ||
2607 | CPU_FREE(r); | |
2608 | ||
2609 | if (errno != EINVAL) | |
2610 | return NULL; | |
2611 | ||
2612 | n *= 2; | |
2613 | } | |
2614 | } | |
2615 | ||
9e58ff9c LP |
2616 | void status_vprintf(const char *format, va_list ap) { |
2617 | char *s = NULL; | |
2618 | int fd = -1; | |
2619 | ||
2620 | assert(format); | |
2621 | ||
2622 | /* This independent of logging, as status messages are | |
2623 | * optional and go exclusively to the console. */ | |
2624 | ||
2625 | if (vasprintf(&s, format, ap) < 0) | |
2626 | goto finish; | |
2627 | ||
2628 | if ((fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) | |
2629 | goto finish; | |
2630 | ||
2631 | write(fd, s, strlen(s)); | |
2632 | ||
2633 | finish: | |
2634 | free(s); | |
2635 | ||
2636 | if (fd >= 0) | |
2637 | close_nointr_nofail(fd); | |
2638 | } | |
2639 | ||
c846ff47 LP |
2640 | void status_printf(const char *format, ...) { |
2641 | va_list ap; | |
2642 | ||
2643 | assert(format); | |
2644 | ||
2645 | va_start(ap, format); | |
2646 | status_vprintf(format, ap); | |
2647 | va_end(ap); | |
2648 | } | |
2649 | ||
2650 | void status_welcome(void) { | |
2651 | ||
2652 | #if defined(TARGET_FEDORA) | |
2653 | char *r; | |
2654 | ||
2655 | if (read_one_line_file("/etc/system-release", &r) < 0) | |
2656 | return; | |
2657 | ||
2658 | truncate_nl(r); | |
2659 | ||
2660 | /* This tries to mimic the color magic the old Red Hat sysinit | |
2661 | * script did. */ | |
2662 | ||
2663 | if (startswith(r, "Red Hat")) | |
2e54424d | 2664 | status_printf("Welcome to \x1B[0;31m%s\x1B[0m!\n", r); /* Red for RHEL */ |
c846ff47 | 2665 | else if (startswith(r, "Fedora")) |
2e54424d | 2666 | status_printf("Welcome to \x1B[0;34m%s\x1B[0m!\n", r); /* Blue for Fedora */ |
c846ff47 | 2667 | else |
2e54424d | 2668 | status_printf("Welcome to %s!\n", r); |
c846ff47 LP |
2669 | |
2670 | free(r); | |
2671 | ||
2672 | #elif defined(TARGET_SUSE) | |
2673 | char *r; | |
2674 | ||
2675 | if (read_one_line_file("/etc/SuSE-release", &r) < 0) | |
2676 | return; | |
2677 | ||
2678 | truncate_nl(r); | |
2679 | ||
2e54424d | 2680 | status_printf("Welcome to \x1B[0;32m%s\x1B[0m!\n", r); /* Green for SUSE */ |
c846ff47 LP |
2681 | free(r); |
2682 | #else | |
2683 | #warning "You probably should add a welcome text logic here." | |
2684 | #endif | |
2685 | } | |
2686 | ||
1dccbe19 LP |
2687 | static const char *const ioprio_class_table[] = { |
2688 | [IOPRIO_CLASS_NONE] = "none", | |
2689 | [IOPRIO_CLASS_RT] = "realtime", | |
2690 | [IOPRIO_CLASS_BE] = "best-effort", | |
2691 | [IOPRIO_CLASS_IDLE] = "idle" | |
2692 | }; | |
2693 | ||
2694 | DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int); | |
2695 | ||
2696 | static const char *const sigchld_code_table[] = { | |
2697 | [CLD_EXITED] = "exited", | |
2698 | [CLD_KILLED] = "killed", | |
2699 | [CLD_DUMPED] = "dumped", | |
2700 | [CLD_TRAPPED] = "trapped", | |
2701 | [CLD_STOPPED] = "stopped", | |
2702 | [CLD_CONTINUED] = "continued", | |
2703 | }; | |
2704 | ||
2705 | DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int); | |
2706 | ||
2707 | static const char *const log_facility_table[LOG_NFACILITIES] = { | |
2708 | [LOG_FAC(LOG_KERN)] = "kern", | |
2709 | [LOG_FAC(LOG_USER)] = "user", | |
2710 | [LOG_FAC(LOG_MAIL)] = "mail", | |
2711 | [LOG_FAC(LOG_DAEMON)] = "daemon", | |
2712 | [LOG_FAC(LOG_AUTH)] = "auth", | |
2713 | [LOG_FAC(LOG_SYSLOG)] = "syslog", | |
2714 | [LOG_FAC(LOG_LPR)] = "lpr", | |
2715 | [LOG_FAC(LOG_NEWS)] = "news", | |
2716 | [LOG_FAC(LOG_UUCP)] = "uucp", | |
2717 | [LOG_FAC(LOG_CRON)] = "cron", | |
2718 | [LOG_FAC(LOG_AUTHPRIV)] = "authpriv", | |
2719 | [LOG_FAC(LOG_FTP)] = "ftp", | |
2720 | [LOG_FAC(LOG_LOCAL0)] = "local0", | |
2721 | [LOG_FAC(LOG_LOCAL1)] = "local1", | |
2722 | [LOG_FAC(LOG_LOCAL2)] = "local2", | |
2723 | [LOG_FAC(LOG_LOCAL3)] = "local3", | |
2724 | [LOG_FAC(LOG_LOCAL4)] = "local4", | |
2725 | [LOG_FAC(LOG_LOCAL5)] = "local5", | |
2726 | [LOG_FAC(LOG_LOCAL6)] = "local6", | |
2727 | [LOG_FAC(LOG_LOCAL7)] = "local7" | |
2728 | }; | |
2729 | ||
2730 | DEFINE_STRING_TABLE_LOOKUP(log_facility, int); | |
2731 | ||
2732 | static const char *const log_level_table[] = { | |
2733 | [LOG_EMERG] = "emerg", | |
2734 | [LOG_ALERT] = "alert", | |
2735 | [LOG_CRIT] = "crit", | |
2736 | [LOG_ERR] = "err", | |
2737 | [LOG_WARNING] = "warning", | |
2738 | [LOG_NOTICE] = "notice", | |
2739 | [LOG_INFO] = "info", | |
2740 | [LOG_DEBUG] = "debug" | |
2741 | }; | |
2742 | ||
2743 | DEFINE_STRING_TABLE_LOOKUP(log_level, int); | |
2744 | ||
2745 | static const char* const sched_policy_table[] = { | |
2746 | [SCHED_OTHER] = "other", | |
2747 | [SCHED_BATCH] = "batch", | |
2748 | [SCHED_IDLE] = "idle", | |
2749 | [SCHED_FIFO] = "fifo", | |
2750 | [SCHED_RR] = "rr" | |
2751 | }; | |
2752 | ||
2753 | DEFINE_STRING_TABLE_LOOKUP(sched_policy, int); | |
2754 | ||
2755 | static const char* const rlimit_table[] = { | |
2756 | [RLIMIT_CPU] = "LimitCPU", | |
2757 | [RLIMIT_FSIZE] = "LimitFSIZE", | |
2758 | [RLIMIT_DATA] = "LimitDATA", | |
2759 | [RLIMIT_STACK] = "LimitSTACK", | |
2760 | [RLIMIT_CORE] = "LimitCORE", | |
2761 | [RLIMIT_RSS] = "LimitRSS", | |
2762 | [RLIMIT_NOFILE] = "LimitNOFILE", | |
2763 | [RLIMIT_AS] = "LimitAS", | |
2764 | [RLIMIT_NPROC] = "LimitNPROC", | |
2765 | [RLIMIT_MEMLOCK] = "LimitMEMLOCK", | |
2766 | [RLIMIT_LOCKS] = "LimitLOCKS", | |
2767 | [RLIMIT_SIGPENDING] = "LimitSIGPENDING", | |
2768 | [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE", | |
2769 | [RLIMIT_NICE] = "LimitNICE", | |
2770 | [RLIMIT_RTPRIO] = "LimitRTPRIO", | |
2771 | [RLIMIT_RTTIME] = "LimitRTTIME" | |
2772 | }; | |
2773 | ||
2774 | DEFINE_STRING_TABLE_LOOKUP(rlimit, int); | |
4fd5948e LP |
2775 | |
2776 | static const char* const ip_tos_table[] = { | |
2777 | [IPTOS_LOWDELAY] = "low-delay", | |
2778 | [IPTOS_THROUGHPUT] = "throughput", | |
2779 | [IPTOS_RELIABILITY] = "reliability", | |
2780 | [IPTOS_LOWCOST] = "low-cost", | |
2781 | }; | |
2782 | ||
2783 | DEFINE_STRING_TABLE_LOOKUP(ip_tos, int); |