]>
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> |
7948c4df KS |
54 | #include <sys/time.h> |
55 | #include <linux/rtc.h> | |
8092a428 | 56 | #include <glob.h> |
4b67834e | 57 | #include <grp.h> |
87d2c1ff | 58 | #include <sys/mman.h> |
60918275 LP |
59 | |
60 | #include "macro.h" | |
61 | #include "util.h" | |
1dccbe19 LP |
62 | #include "ioprio.h" |
63 | #include "missing.h" | |
a9f5d454 | 64 | #include "log.h" |
65d2ebdc | 65 | #include "strv.h" |
e51bc1a2 | 66 | #include "label.h" |
d06dacd0 | 67 | #include "exit-status.h" |
83cc030f | 68 | #include "hashmap.h" |
56cf987f | 69 | |
9a0e6896 LP |
70 | int saved_argc = 0; |
71 | char **saved_argv = NULL; | |
72 | ||
37f85e66 | 73 | size_t page_size(void) { |
74 | static __thread size_t pgsz = 0; | |
75 | long r; | |
76 | ||
87d2c1ff | 77 | if (_likely_(pgsz > 0)) |
37f85e66 | 78 | return pgsz; |
79 | ||
80 | assert_se((r = sysconf(_SC_PAGESIZE)) > 0); | |
81 | ||
82 | pgsz = (size_t) r; | |
83 | ||
84 | return pgsz; | |
85 | } | |
86 | ||
e05797fb LP |
87 | bool streq_ptr(const char *a, const char *b) { |
88 | ||
89 | /* Like streq(), but tries to make sense of NULL pointers */ | |
90 | ||
91 | if (a && b) | |
92 | return streq(a, b); | |
93 | ||
94 | if (!a && !b) | |
95 | return true; | |
96 | ||
97 | return false; | |
98 | } | |
99 | ||
47be870b | 100 | usec_t now(clockid_t clock_id) { |
60918275 LP |
101 | struct timespec ts; |
102 | ||
47be870b | 103 | assert_se(clock_gettime(clock_id, &ts) == 0); |
60918275 LP |
104 | |
105 | return timespec_load(&ts); | |
106 | } | |
107 | ||
63983207 | 108 | dual_timestamp* dual_timestamp_get(dual_timestamp *ts) { |
871d7de4 LP |
109 | assert(ts); |
110 | ||
111 | ts->realtime = now(CLOCK_REALTIME); | |
112 | ts->monotonic = now(CLOCK_MONOTONIC); | |
113 | ||
114 | return ts; | |
115 | } | |
116 | ||
a185c5aa LP |
117 | dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) { |
118 | int64_t delta; | |
119 | assert(ts); | |
120 | ||
121 | ts->realtime = u; | |
122 | ||
123 | if (u == 0) | |
124 | ts->monotonic = 0; | |
125 | else { | |
126 | delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u; | |
127 | ||
128 | ts->monotonic = now(CLOCK_MONOTONIC); | |
129 | ||
130 | if ((int64_t) ts->monotonic > delta) | |
131 | ts->monotonic -= delta; | |
132 | else | |
133 | ts->monotonic = 0; | |
134 | } | |
135 | ||
136 | return ts; | |
137 | } | |
138 | ||
60918275 LP |
139 | usec_t timespec_load(const struct timespec *ts) { |
140 | assert(ts); | |
141 | ||
142 | return | |
143 | (usec_t) ts->tv_sec * USEC_PER_SEC + | |
144 | (usec_t) ts->tv_nsec / NSEC_PER_USEC; | |
145 | } | |
146 | ||
147 | struct timespec *timespec_store(struct timespec *ts, usec_t u) { | |
148 | assert(ts); | |
149 | ||
150 | ts->tv_sec = (time_t) (u / USEC_PER_SEC); | |
151 | ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC); | |
152 | ||
153 | return ts; | |
154 | } | |
155 | ||
156 | usec_t timeval_load(const struct timeval *tv) { | |
157 | assert(tv); | |
158 | ||
159 | return | |
160 | (usec_t) tv->tv_sec * USEC_PER_SEC + | |
161 | (usec_t) tv->tv_usec; | |
162 | } | |
163 | ||
164 | struct timeval *timeval_store(struct timeval *tv, usec_t u) { | |
165 | assert(tv); | |
166 | ||
167 | tv->tv_sec = (time_t) (u / USEC_PER_SEC); | |
168 | tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC); | |
169 | ||
170 | return tv; | |
171 | } | |
172 | ||
173 | bool endswith(const char *s, const char *postfix) { | |
174 | size_t sl, pl; | |
175 | ||
176 | assert(s); | |
177 | assert(postfix); | |
178 | ||
179 | sl = strlen(s); | |
180 | pl = strlen(postfix); | |
181 | ||
d4d0d4db LP |
182 | if (pl == 0) |
183 | return true; | |
184 | ||
60918275 LP |
185 | if (sl < pl) |
186 | return false; | |
187 | ||
188 | return memcmp(s + sl - pl, postfix, pl) == 0; | |
189 | } | |
190 | ||
191 | bool startswith(const char *s, const char *prefix) { | |
192 | size_t sl, pl; | |
193 | ||
194 | assert(s); | |
195 | assert(prefix); | |
196 | ||
197 | sl = strlen(s); | |
198 | pl = strlen(prefix); | |
199 | ||
d4d0d4db LP |
200 | if (pl == 0) |
201 | return true; | |
202 | ||
60918275 LP |
203 | if (sl < pl) |
204 | return false; | |
205 | ||
206 | return memcmp(s, prefix, pl) == 0; | |
207 | } | |
208 | ||
3177a7fa MAP |
209 | bool startswith_no_case(const char *s, const char *prefix) { |
210 | size_t sl, pl; | |
211 | unsigned i; | |
212 | ||
213 | assert(s); | |
214 | assert(prefix); | |
215 | ||
216 | sl = strlen(s); | |
217 | pl = strlen(prefix); | |
218 | ||
219 | if (pl == 0) | |
220 | return true; | |
221 | ||
222 | if (sl < pl) | |
223 | return false; | |
224 | ||
225 | for(i = 0; i < pl; ++i) { | |
226 | if (tolower(s[i]) != tolower(prefix[i])) | |
227 | return false; | |
228 | } | |
229 | ||
230 | return true; | |
231 | } | |
232 | ||
79d6d816 LP |
233 | bool first_word(const char *s, const char *word) { |
234 | size_t sl, wl; | |
235 | ||
236 | assert(s); | |
237 | assert(word); | |
238 | ||
239 | sl = strlen(s); | |
240 | wl = strlen(word); | |
241 | ||
242 | if (sl < wl) | |
243 | return false; | |
244 | ||
d4d0d4db LP |
245 | if (wl == 0) |
246 | return true; | |
247 | ||
79d6d816 LP |
248 | if (memcmp(s, word, wl) != 0) |
249 | return false; | |
250 | ||
d4d0d4db LP |
251 | return s[wl] == 0 || |
252 | strchr(WHITESPACE, s[wl]); | |
79d6d816 LP |
253 | } |
254 | ||
42f4e3c4 | 255 | int close_nointr(int fd) { |
60918275 LP |
256 | assert(fd >= 0); |
257 | ||
258 | for (;;) { | |
259 | int r; | |
260 | ||
48f82119 LP |
261 | r = close(fd); |
262 | if (r >= 0) | |
60918275 LP |
263 | return r; |
264 | ||
265 | if (errno != EINTR) | |
48f82119 | 266 | return -errno; |
60918275 LP |
267 | } |
268 | } | |
85261803 | 269 | |
85f136b5 | 270 | void close_nointr_nofail(int fd) { |
80876c20 | 271 | int saved_errno = errno; |
85f136b5 LP |
272 | |
273 | /* like close_nointr() but cannot fail, and guarantees errno | |
274 | * is unchanged */ | |
275 | ||
276 | assert_se(close_nointr(fd) == 0); | |
80876c20 LP |
277 | |
278 | errno = saved_errno; | |
85f136b5 LP |
279 | } |
280 | ||
5b6319dc LP |
281 | void close_many(const int fds[], unsigned n_fd) { |
282 | unsigned i; | |
283 | ||
284 | for (i = 0; i < n_fd; i++) | |
285 | close_nointr_nofail(fds[i]); | |
286 | } | |
287 | ||
85261803 LP |
288 | int parse_boolean(const char *v) { |
289 | assert(v); | |
290 | ||
44d8db9e | 291 | if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on")) |
85261803 | 292 | return 1; |
44d8db9e | 293 | else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off")) |
85261803 LP |
294 | return 0; |
295 | ||
296 | return -EINVAL; | |
297 | } | |
298 | ||
3ba686c1 | 299 | int parse_pid(const char *s, pid_t* ret_pid) { |
0b172489 | 300 | unsigned long ul = 0; |
3ba686c1 LP |
301 | pid_t pid; |
302 | int r; | |
303 | ||
304 | assert(s); | |
305 | assert(ret_pid); | |
306 | ||
307 | if ((r = safe_atolu(s, &ul)) < 0) | |
308 | return r; | |
309 | ||
310 | pid = (pid_t) ul; | |
311 | ||
312 | if ((unsigned long) pid != ul) | |
313 | return -ERANGE; | |
314 | ||
315 | if (pid <= 0) | |
316 | return -ERANGE; | |
317 | ||
318 | *ret_pid = pid; | |
319 | return 0; | |
320 | } | |
321 | ||
034a2a52 LP |
322 | int parse_uid(const char *s, uid_t* ret_uid) { |
323 | unsigned long ul = 0; | |
324 | uid_t uid; | |
325 | int r; | |
326 | ||
327 | assert(s); | |
328 | assert(ret_uid); | |
329 | ||
330 | if ((r = safe_atolu(s, &ul)) < 0) | |
331 | return r; | |
332 | ||
333 | uid = (uid_t) ul; | |
334 | ||
335 | if ((unsigned long) uid != ul) | |
336 | return -ERANGE; | |
337 | ||
338 | *ret_uid = uid; | |
339 | return 0; | |
340 | } | |
341 | ||
85261803 LP |
342 | int safe_atou(const char *s, unsigned *ret_u) { |
343 | char *x = NULL; | |
034c6ed7 | 344 | unsigned long l; |
85261803 LP |
345 | |
346 | assert(s); | |
347 | assert(ret_u); | |
348 | ||
349 | errno = 0; | |
350 | l = strtoul(s, &x, 0); | |
351 | ||
352 | if (!x || *x || errno) | |
353 | return errno ? -errno : -EINVAL; | |
354 | ||
034c6ed7 | 355 | if ((unsigned long) (unsigned) l != l) |
85261803 LP |
356 | return -ERANGE; |
357 | ||
358 | *ret_u = (unsigned) l; | |
359 | return 0; | |
360 | } | |
361 | ||
362 | int safe_atoi(const char *s, int *ret_i) { | |
363 | char *x = NULL; | |
034c6ed7 | 364 | long l; |
85261803 LP |
365 | |
366 | assert(s); | |
367 | assert(ret_i); | |
368 | ||
369 | errno = 0; | |
370 | l = strtol(s, &x, 0); | |
371 | ||
372 | if (!x || *x || errno) | |
373 | return errno ? -errno : -EINVAL; | |
374 | ||
034c6ed7 | 375 | if ((long) (int) l != l) |
85261803 LP |
376 | return -ERANGE; |
377 | ||
034c6ed7 LP |
378 | *ret_i = (int) l; |
379 | return 0; | |
380 | } | |
381 | ||
034c6ed7 LP |
382 | int safe_atollu(const char *s, long long unsigned *ret_llu) { |
383 | char *x = NULL; | |
384 | unsigned long long l; | |
385 | ||
386 | assert(s); | |
387 | assert(ret_llu); | |
388 | ||
389 | errno = 0; | |
390 | l = strtoull(s, &x, 0); | |
391 | ||
392 | if (!x || *x || errno) | |
393 | return errno ? -errno : -EINVAL; | |
394 | ||
395 | *ret_llu = l; | |
396 | return 0; | |
397 | } | |
398 | ||
399 | int safe_atolli(const char *s, long long int *ret_lli) { | |
400 | char *x = NULL; | |
401 | long long l; | |
402 | ||
403 | assert(s); | |
404 | assert(ret_lli); | |
405 | ||
406 | errno = 0; | |
407 | l = strtoll(s, &x, 0); | |
408 | ||
409 | if (!x || *x || errno) | |
410 | return errno ? -errno : -EINVAL; | |
411 | ||
412 | *ret_lli = l; | |
85261803 LP |
413 | return 0; |
414 | } | |
a41e8209 | 415 | |
a41e8209 | 416 | /* Split a string into words. */ |
65d2ebdc | 417 | char *split(const char *c, size_t *l, const char *separator, char **state) { |
a41e8209 LP |
418 | char *current; |
419 | ||
420 | current = *state ? *state : (char*) c; | |
421 | ||
422 | if (!*current || *c == 0) | |
423 | return NULL; | |
424 | ||
65d2ebdc LP |
425 | current += strspn(current, separator); |
426 | *l = strcspn(current, separator); | |
82919e3d LP |
427 | *state = current+*l; |
428 | ||
429 | return (char*) current; | |
430 | } | |
431 | ||
034c6ed7 LP |
432 | /* Split a string into words, but consider strings enclosed in '' and |
433 | * "" as words even if they include spaces. */ | |
434 | char *split_quoted(const char *c, size_t *l, char **state) { | |
0bab36f2 LP |
435 | char *current, *e; |
436 | bool escaped = false; | |
034c6ed7 LP |
437 | |
438 | current = *state ? *state : (char*) c; | |
439 | ||
440 | if (!*current || *c == 0) | |
441 | return NULL; | |
442 | ||
443 | current += strspn(current, WHITESPACE); | |
444 | ||
445 | if (*current == '\'') { | |
446 | current ++; | |
034c6ed7 | 447 | |
0bab36f2 LP |
448 | for (e = current; *e; e++) { |
449 | if (escaped) | |
450 | escaped = false; | |
451 | else if (*e == '\\') | |
452 | escaped = true; | |
453 | else if (*e == '\'') | |
454 | break; | |
455 | } | |
456 | ||
457 | *l = e-current; | |
458 | *state = *e == 0 ? e : e+1; | |
034c6ed7 LP |
459 | } else if (*current == '\"') { |
460 | current ++; | |
034c6ed7 | 461 | |
0bab36f2 LP |
462 | for (e = current; *e; e++) { |
463 | if (escaped) | |
464 | escaped = false; | |
465 | else if (*e == '\\') | |
466 | escaped = true; | |
467 | else if (*e == '\"') | |
468 | break; | |
469 | } | |
470 | ||
471 | *l = e-current; | |
472 | *state = *e == 0 ? e : e+1; | |
034c6ed7 | 473 | } else { |
0bab36f2 LP |
474 | for (e = current; *e; e++) { |
475 | if (escaped) | |
476 | escaped = false; | |
477 | else if (*e == '\\') | |
478 | escaped = true; | |
479 | else if (strchr(WHITESPACE, *e)) | |
480 | break; | |
481 | } | |
482 | *l = e-current; | |
483 | *state = e; | |
034c6ed7 LP |
484 | } |
485 | ||
486 | return (char*) current; | |
487 | } | |
488 | ||
65d2ebdc LP |
489 | char **split_path_and_make_absolute(const char *p) { |
490 | char **l; | |
491 | assert(p); | |
492 | ||
493 | if (!(l = strv_split(p, ":"))) | |
494 | return NULL; | |
495 | ||
496 | if (!strv_path_make_absolute_cwd(l)) { | |
497 | strv_free(l); | |
498 | return NULL; | |
499 | } | |
500 | ||
501 | return l; | |
502 | } | |
503 | ||
034c6ed7 LP |
504 | int get_parent_of_pid(pid_t pid, pid_t *_ppid) { |
505 | int r; | |
506 | FILE *f; | |
20c03b7b | 507 | char fn[PATH_MAX], line[LINE_MAX], *p; |
bb00e604 | 508 | long unsigned ppid; |
034c6ed7 | 509 | |
8480e784 | 510 | assert(pid > 0); |
034c6ed7 LP |
511 | assert(_ppid); |
512 | ||
bb00e604 | 513 | assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1)); |
8480e784 | 514 | char_array_0(fn); |
034c6ed7 | 515 | |
ccaa6149 | 516 | if (!(f = fopen(fn, "re"))) |
034c6ed7 LP |
517 | return -errno; |
518 | ||
519 | if (!(fgets(line, sizeof(line), f))) { | |
520 | r = -errno; | |
521 | fclose(f); | |
522 | return r; | |
523 | } | |
524 | ||
525 | fclose(f); | |
526 | ||
527 | /* Let's skip the pid and comm fields. The latter is enclosed | |
528 | * in () but does not escape any () in its value, so let's | |
529 | * skip over it manually */ | |
530 | ||
531 | if (!(p = strrchr(line, ')'))) | |
532 | return -EIO; | |
533 | ||
534 | p++; | |
535 | ||
536 | if (sscanf(p, " " | |
537 | "%*c " /* state */ | |
bb00e604 | 538 | "%lu ", /* ppid */ |
034c6ed7 LP |
539 | &ppid) != 1) |
540 | return -EIO; | |
541 | ||
bb00e604 | 542 | if ((long unsigned) (pid_t) ppid != ppid) |
034c6ed7 LP |
543 | return -ERANGE; |
544 | ||
545 | *_ppid = (pid_t) ppid; | |
546 | ||
547 | return 0; | |
548 | } | |
549 | ||
7640a5de LP |
550 | int get_starttime_of_pid(pid_t pid, unsigned long long *st) { |
551 | int r; | |
552 | FILE *f; | |
553 | char fn[PATH_MAX], line[LINE_MAX], *p; | |
554 | ||
555 | assert(pid > 0); | |
556 | assert(st); | |
557 | ||
558 | assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1)); | |
559 | char_array_0(fn); | |
560 | ||
ccaa6149 | 561 | if (!(f = fopen(fn, "re"))) |
7640a5de LP |
562 | return -errno; |
563 | ||
564 | if (!(fgets(line, sizeof(line), f))) { | |
565 | r = -errno; | |
566 | fclose(f); | |
567 | return r; | |
568 | } | |
569 | ||
570 | fclose(f); | |
571 | ||
572 | /* Let's skip the pid and comm fields. The latter is enclosed | |
573 | * in () but does not escape any () in its value, so let's | |
574 | * skip over it manually */ | |
575 | ||
576 | if (!(p = strrchr(line, ')'))) | |
577 | return -EIO; | |
578 | ||
579 | p++; | |
580 | ||
581 | if (sscanf(p, " " | |
582 | "%*c " /* state */ | |
583 | "%*d " /* ppid */ | |
584 | "%*d " /* pgrp */ | |
585 | "%*d " /* session */ | |
586 | "%*d " /* tty_nr */ | |
587 | "%*d " /* tpgid */ | |
588 | "%*u " /* flags */ | |
589 | "%*u " /* minflt */ | |
590 | "%*u " /* cminflt */ | |
591 | "%*u " /* majflt */ | |
592 | "%*u " /* cmajflt */ | |
593 | "%*u " /* utime */ | |
594 | "%*u " /* stime */ | |
595 | "%*d " /* cutime */ | |
596 | "%*d " /* cstime */ | |
597 | "%*d " /* priority */ | |
598 | "%*d " /* nice */ | |
599 | "%*d " /* num_threads */ | |
600 | "%*d " /* itrealvalue */ | |
601 | "%llu " /* starttime */, | |
602 | st) != 1) | |
603 | return -EIO; | |
604 | ||
605 | return 0; | |
606 | } | |
607 | ||
034c6ed7 LP |
608 | int write_one_line_file(const char *fn, const char *line) { |
609 | FILE *f; | |
610 | int r; | |
611 | ||
612 | assert(fn); | |
613 | assert(line); | |
614 | ||
615 | if (!(f = fopen(fn, "we"))) | |
616 | return -errno; | |
617 | ||
34ca941c | 618 | errno = 0; |
034c6ed7 LP |
619 | if (fputs(line, f) < 0) { |
620 | r = -errno; | |
621 | goto finish; | |
622 | } | |
623 | ||
8e1bd70d LP |
624 | if (!endswith(line, "\n")) |
625 | fputc('\n', f); | |
626 | ||
151b190e LP |
627 | fflush(f); |
628 | ||
629 | if (ferror(f)) { | |
630 | if (errno != 0) | |
631 | r = -errno; | |
632 | else | |
633 | r = -EIO; | |
634 | } else | |
635 | r = 0; | |
636 | ||
034c6ed7 LP |
637 | finish: |
638 | fclose(f); | |
639 | return r; | |
640 | } | |
641 | ||
34ca941c LP |
642 | int fchmod_umask(int fd, mode_t m) { |
643 | mode_t u; | |
644 | int r; | |
645 | ||
646 | u = umask(0777); | |
647 | r = fchmod(fd, m & (~u)) < 0 ? -errno : 0; | |
648 | umask(u); | |
649 | ||
650 | return r; | |
651 | } | |
652 | ||
653 | int write_one_line_file_atomic(const char *fn, const char *line) { | |
654 | FILE *f; | |
655 | int r; | |
656 | char *p; | |
657 | ||
658 | assert(fn); | |
659 | assert(line); | |
660 | ||
661 | r = fopen_temporary(fn, &f, &p); | |
662 | if (r < 0) | |
663 | return r; | |
664 | ||
665 | fchmod_umask(fileno(f), 0644); | |
666 | ||
667 | errno = 0; | |
668 | if (fputs(line, f) < 0) { | |
669 | r = -errno; | |
670 | goto finish; | |
671 | } | |
672 | ||
673 | if (!endswith(line, "\n")) | |
674 | fputc('\n', f); | |
675 | ||
676 | fflush(f); | |
677 | ||
678 | if (ferror(f)) { | |
679 | if (errno != 0) | |
680 | r = -errno; | |
681 | else | |
682 | r = -EIO; | |
683 | } else { | |
684 | if (rename(p, fn) < 0) | |
685 | r = -errno; | |
686 | else | |
687 | r = 0; | |
688 | } | |
689 | ||
690 | finish: | |
691 | if (r < 0) | |
692 | unlink(p); | |
693 | ||
694 | fclose(f); | |
695 | free(p); | |
696 | ||
697 | return r; | |
698 | } | |
699 | ||
034c6ed7 LP |
700 | int read_one_line_file(const char *fn, char **line) { |
701 | FILE *f; | |
702 | int r; | |
97c4a07d | 703 | char t[LINE_MAX], *c; |
034c6ed7 LP |
704 | |
705 | assert(fn); | |
706 | assert(line); | |
707 | ||
708 | if (!(f = fopen(fn, "re"))) | |
709 | return -errno; | |
710 | ||
711 | if (!(fgets(t, sizeof(t), f))) { | |
712 | r = -errno; | |
713 | goto finish; | |
714 | } | |
715 | ||
716 | if (!(c = strdup(t))) { | |
717 | r = -ENOMEM; | |
718 | goto finish; | |
719 | } | |
720 | ||
6f9a471a LP |
721 | truncate_nl(c); |
722 | ||
034c6ed7 LP |
723 | *line = c; |
724 | r = 0; | |
725 | ||
726 | finish: | |
727 | fclose(f); | |
728 | return r; | |
729 | } | |
44d8db9e | 730 | |
34ca941c | 731 | int read_full_file(const char *fn, char **contents, size_t *size) { |
97c4a07d LP |
732 | FILE *f; |
733 | int r; | |
734 | size_t n, l; | |
735 | char *buf = NULL; | |
736 | struct stat st; | |
737 | ||
738 | if (!(f = fopen(fn, "re"))) | |
739 | return -errno; | |
740 | ||
741 | if (fstat(fileno(f), &st) < 0) { | |
742 | r = -errno; | |
743 | goto finish; | |
744 | } | |
745 | ||
34ca941c LP |
746 | /* Safety check */ |
747 | if (st.st_size > 4*1024*1024) { | |
748 | r = -E2BIG; | |
749 | goto finish; | |
750 | } | |
751 | ||
97c4a07d LP |
752 | n = st.st_size > 0 ? st.st_size : LINE_MAX; |
753 | l = 0; | |
754 | ||
755 | for (;;) { | |
756 | char *t; | |
757 | size_t k; | |
758 | ||
759 | if (!(t = realloc(buf, n+1))) { | |
760 | r = -ENOMEM; | |
761 | goto finish; | |
762 | } | |
763 | ||
764 | buf = t; | |
765 | k = fread(buf + l, 1, n - l, f); | |
766 | ||
767 | if (k <= 0) { | |
768 | if (ferror(f)) { | |
769 | r = -errno; | |
770 | goto finish; | |
771 | } | |
772 | ||
773 | break; | |
774 | } | |
775 | ||
776 | l += k; | |
777 | n *= 2; | |
778 | ||
779 | /* Safety check */ | |
780 | if (n > 4*1024*1024) { | |
781 | r = -E2BIG; | |
782 | goto finish; | |
783 | } | |
784 | } | |
785 | ||
f8440af5 | 786 | buf[l] = 0; |
97c4a07d LP |
787 | *contents = buf; |
788 | buf = NULL; | |
789 | ||
34ca941c LP |
790 | if (size) |
791 | *size = l; | |
792 | ||
97c4a07d LP |
793 | r = 0; |
794 | ||
795 | finish: | |
796 | fclose(f); | |
797 | free(buf); | |
798 | ||
799 | return r; | |
800 | } | |
801 | ||
802 | int parse_env_file( | |
803 | const char *fname, | |
c899f8c6 | 804 | const char *separator, ...) { |
97c4a07d | 805 | |
ce8a6aa1 | 806 | int r = 0; |
44d91056 | 807 | char *contents = NULL, *p; |
97c4a07d LP |
808 | |
809 | assert(fname); | |
c899f8c6 | 810 | assert(separator); |
97c4a07d | 811 | |
34ca941c | 812 | if ((r = read_full_file(fname, &contents, NULL)) < 0) |
97c4a07d LP |
813 | return r; |
814 | ||
815 | p = contents; | |
816 | for (;;) { | |
817 | const char *key = NULL; | |
818 | ||
c899f8c6 | 819 | p += strspn(p, separator); |
97c4a07d LP |
820 | p += strspn(p, WHITESPACE); |
821 | ||
822 | if (!*p) | |
823 | break; | |
824 | ||
825 | if (!strchr(COMMENTS, *p)) { | |
826 | va_list ap; | |
827 | char **value; | |
828 | ||
c899f8c6 | 829 | va_start(ap, separator); |
97c4a07d LP |
830 | while ((key = va_arg(ap, char *))) { |
831 | size_t n; | |
832 | char *v; | |
833 | ||
834 | value = va_arg(ap, char **); | |
835 | ||
836 | n = strlen(key); | |
837 | if (strncmp(p, key, n) != 0 || | |
838 | p[n] != '=') | |
839 | continue; | |
840 | ||
841 | p += n + 1; | |
c899f8c6 | 842 | n = strcspn(p, separator); |
97c4a07d LP |
843 | |
844 | if (n >= 2 && | |
e7db37dd LP |
845 | strchr(QUOTES, p[0]) && |
846 | p[n-1] == p[0]) | |
97c4a07d LP |
847 | v = strndup(p+1, n-2); |
848 | else | |
849 | v = strndup(p, n); | |
850 | ||
851 | if (!v) { | |
852 | r = -ENOMEM; | |
853 | va_end(ap); | |
854 | goto fail; | |
855 | } | |
856 | ||
dd36de4d KS |
857 | if (v[0] == '\0') { |
858 | /* return empty value strings as NULL */ | |
859 | free(v); | |
860 | v = NULL; | |
861 | } | |
862 | ||
97c4a07d LP |
863 | free(*value); |
864 | *value = v; | |
865 | ||
866 | p += n; | |
ce8a6aa1 LP |
867 | |
868 | r ++; | |
97c4a07d LP |
869 | break; |
870 | } | |
871 | va_end(ap); | |
872 | } | |
873 | ||
874 | if (!key) | |
c899f8c6 | 875 | p += strcspn(p, separator); |
97c4a07d LP |
876 | } |
877 | ||
97c4a07d LP |
878 | fail: |
879 | free(contents); | |
880 | return r; | |
881 | } | |
882 | ||
8c7be95e LP |
883 | int load_env_file( |
884 | const char *fname, | |
885 | char ***rl) { | |
886 | ||
887 | FILE *f; | |
888 | char **m = 0; | |
889 | int r; | |
890 | ||
891 | assert(fname); | |
892 | assert(rl); | |
893 | ||
894 | if (!(f = fopen(fname, "re"))) | |
895 | return -errno; | |
896 | ||
897 | while (!feof(f)) { | |
898 | char l[LINE_MAX], *p, *u; | |
899 | char **t; | |
900 | ||
901 | if (!fgets(l, sizeof(l), f)) { | |
902 | if (feof(f)) | |
903 | break; | |
904 | ||
905 | r = -errno; | |
906 | goto finish; | |
907 | } | |
908 | ||
909 | p = strstrip(l); | |
910 | ||
911 | if (!*p) | |
912 | continue; | |
913 | ||
914 | if (strchr(COMMENTS, *p)) | |
915 | continue; | |
916 | ||
917 | if (!(u = normalize_env_assignment(p))) { | |
918 | log_error("Out of memory"); | |
919 | r = -ENOMEM; | |
920 | goto finish; | |
921 | } | |
922 | ||
923 | t = strv_append(m, u); | |
924 | free(u); | |
925 | ||
926 | if (!t) { | |
927 | log_error("Out of memory"); | |
928 | r = -ENOMEM; | |
929 | goto finish; | |
930 | } | |
931 | ||
932 | strv_free(m); | |
933 | m = t; | |
934 | } | |
935 | ||
936 | r = 0; | |
937 | ||
938 | *rl = m; | |
939 | m = NULL; | |
940 | ||
941 | finish: | |
942 | if (f) | |
943 | fclose(f); | |
944 | ||
945 | strv_free(m); | |
946 | ||
947 | return r; | |
948 | } | |
949 | ||
7640a5de | 950 | int write_env_file(const char *fname, char **l) { |
34ca941c | 951 | char **i, *p; |
7640a5de LP |
952 | FILE *f; |
953 | int r; | |
954 | ||
34ca941c LP |
955 | r = fopen_temporary(fname, &f, &p); |
956 | if (r < 0) | |
957 | return r; | |
7640a5de | 958 | |
34ca941c LP |
959 | fchmod_umask(fileno(f), 0644); |
960 | ||
961 | errno = 0; | |
7640a5de LP |
962 | STRV_FOREACH(i, l) { |
963 | fputs(*i, f); | |
964 | fputc('\n', f); | |
965 | } | |
966 | ||
967 | fflush(f); | |
968 | ||
34ca941c LP |
969 | if (ferror(f)) { |
970 | if (errno != 0) | |
971 | r = -errno; | |
972 | else | |
973 | r = -EIO; | |
974 | } else { | |
975 | if (rename(p, fname) < 0) | |
976 | r = -errno; | |
977 | else | |
978 | r = 0; | |
979 | } | |
980 | ||
981 | if (r < 0) | |
982 | unlink(p); | |
983 | ||
7640a5de | 984 | fclose(f); |
34ca941c | 985 | free(p); |
7640a5de LP |
986 | |
987 | return r; | |
988 | } | |
989 | ||
7072ced8 LP |
990 | char *truncate_nl(char *s) { |
991 | assert(s); | |
992 | ||
993 | s[strcspn(s, NEWLINE)] = 0; | |
994 | return s; | |
995 | } | |
996 | ||
87d2c1ff | 997 | int get_process_comm(pid_t pid, char **name) { |
7072ced8 LP |
998 | int r; |
999 | ||
7072ced8 LP |
1000 | assert(name); |
1001 | ||
87d2c1ff LP |
1002 | if (pid == 0) |
1003 | r = read_one_line_file("/proc/self/comm", name); | |
1004 | else { | |
1005 | char *p; | |
1006 | if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0) | |
1007 | return -ENOMEM; | |
7072ced8 | 1008 | |
87d2c1ff LP |
1009 | r = read_one_line_file(p, name); |
1010 | free(p); | |
1011 | } | |
7072ced8 | 1012 | |
87d2c1ff | 1013 | return r; |
7072ced8 LP |
1014 | } |
1015 | ||
87d2c1ff LP |
1016 | int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) { |
1017 | char *r, *k; | |
c59760ee LP |
1018 | int c; |
1019 | bool space = false; | |
1020 | size_t left; | |
1021 | FILE *f; | |
1022 | ||
c59760ee LP |
1023 | assert(max_length > 0); |
1024 | assert(line); | |
1025 | ||
87d2c1ff LP |
1026 | if (pid == 0) |
1027 | f = fopen("/proc/self/cmdline", "re"); | |
1028 | else { | |
1029 | char *p; | |
1030 | if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0) | |
1031 | return -ENOMEM; | |
c59760ee | 1032 | |
87d2c1ff LP |
1033 | f = fopen(p, "re"); |
1034 | free(p); | |
1035 | } | |
c59760ee LP |
1036 | |
1037 | if (!f) | |
1038 | return -errno; | |
1039 | ||
87d2c1ff LP |
1040 | r = new(char, max_length); |
1041 | if (!r) { | |
c59760ee LP |
1042 | fclose(f); |
1043 | return -ENOMEM; | |
1044 | } | |
1045 | ||
1046 | k = r; | |
1047 | left = max_length; | |
1048 | while ((c = getc(f)) != EOF) { | |
1049 | ||
1050 | if (isprint(c)) { | |
1051 | if (space) { | |
1052 | if (left <= 4) | |
1053 | break; | |
1054 | ||
1055 | *(k++) = ' '; | |
057fbb58 | 1056 | left--; |
c59760ee LP |
1057 | space = false; |
1058 | } | |
1059 | ||
1060 | if (left <= 4) | |
1061 | break; | |
1062 | ||
1063 | *(k++) = (char) c; | |
057fbb58 | 1064 | left--; |
c59760ee LP |
1065 | } else |
1066 | space = true; | |
1067 | } | |
1068 | ||
1069 | if (left <= 4) { | |
1070 | size_t n = MIN(left-1, 3U); | |
1071 | memcpy(k, "...", n); | |
1072 | k[n] = 0; | |
1073 | } else | |
1074 | *k = 0; | |
1075 | ||
1076 | fclose(f); | |
1077 | ||
35d2e7ec LP |
1078 | /* Kernel threads have no argv[] */ |
1079 | if (r[0] == 0) { | |
1080 | char *t; | |
1081 | int h; | |
1082 | ||
1083 | free(r); | |
1084 | ||
87d2c1ff LP |
1085 | if (!comm_fallback) |
1086 | return -ENOENT; | |
1087 | ||
1088 | h = get_process_comm(pid, &t); | |
1089 | if (h < 0) | |
35d2e7ec LP |
1090 | return h; |
1091 | ||
87d2c1ff | 1092 | r = join("[", t, "]", NULL); |
35d2e7ec LP |
1093 | free(t); |
1094 | ||
87d2c1ff | 1095 | if (!r) |
35d2e7ec LP |
1096 | return -ENOMEM; |
1097 | } | |
fa776d8e | 1098 | |
c59760ee LP |
1099 | *line = r; |
1100 | return 0; | |
1101 | } | |
1102 | ||
87d2c1ff LP |
1103 | int get_process_exe(pid_t pid, char **name) { |
1104 | int r; | |
1105 | ||
1106 | assert(name); | |
1107 | ||
1108 | if (pid == 0) | |
1109 | r = readlink_malloc("/proc/self/exe", name); | |
1110 | else { | |
1111 | char *p; | |
1112 | if (asprintf(&p, "/proc/%lu/exe", (unsigned long) pid) < 0) | |
1113 | return -ENOMEM; | |
1114 | ||
1115 | r = readlink_malloc(p, name); | |
1116 | free(p); | |
1117 | } | |
1118 | ||
1119 | return r; | |
1120 | } | |
1121 | ||
fab56fc5 LP |
1122 | char *strnappend(const char *s, const char *suffix, size_t b) { |
1123 | size_t a; | |
44d8db9e LP |
1124 | char *r; |
1125 | ||
fab56fc5 LP |
1126 | if (!s && !suffix) |
1127 | return strdup(""); | |
1128 | ||
1129 | if (!s) | |
1130 | return strndup(suffix, b); | |
1131 | ||
1132 | if (!suffix) | |
1133 | return strdup(s); | |
1134 | ||
44d8db9e LP |
1135 | assert(s); |
1136 | assert(suffix); | |
1137 | ||
1138 | a = strlen(s); | |
44d8db9e LP |
1139 | |
1140 | if (!(r = new(char, a+b+1))) | |
1141 | return NULL; | |
1142 | ||
1143 | memcpy(r, s, a); | |
1144 | memcpy(r+a, suffix, b); | |
1145 | r[a+b] = 0; | |
1146 | ||
1147 | return r; | |
1148 | } | |
87f0e418 | 1149 | |
fab56fc5 LP |
1150 | char *strappend(const char *s, const char *suffix) { |
1151 | return strnappend(s, suffix, suffix ? strlen(suffix) : 0); | |
1152 | } | |
1153 | ||
87f0e418 LP |
1154 | int readlink_malloc(const char *p, char **r) { |
1155 | size_t l = 100; | |
1156 | ||
1157 | assert(p); | |
1158 | assert(r); | |
1159 | ||
1160 | for (;;) { | |
1161 | char *c; | |
1162 | ssize_t n; | |
1163 | ||
1164 | if (!(c = new(char, l))) | |
1165 | return -ENOMEM; | |
1166 | ||
1167 | if ((n = readlink(p, c, l-1)) < 0) { | |
1168 | int ret = -errno; | |
1169 | free(c); | |
1170 | return ret; | |
1171 | } | |
1172 | ||
1173 | if ((size_t) n < l-1) { | |
1174 | c[n] = 0; | |
1175 | *r = c; | |
1176 | return 0; | |
1177 | } | |
1178 | ||
1179 | free(c); | |
1180 | l *= 2; | |
1181 | } | |
1182 | } | |
1183 | ||
2c7108c4 LP |
1184 | int readlink_and_make_absolute(const char *p, char **r) { |
1185 | char *target, *k; | |
1186 | int j; | |
1187 | ||
1188 | assert(p); | |
1189 | assert(r); | |
1190 | ||
1191 | if ((j = readlink_malloc(p, &target)) < 0) | |
1192 | return j; | |
1193 | ||
1194 | k = file_in_same_dir(p, target); | |
1195 | free(target); | |
1196 | ||
1197 | if (!k) | |
1198 | return -ENOMEM; | |
1199 | ||
1200 | *r = k; | |
1201 | return 0; | |
1202 | } | |
1203 | ||
83096483 LP |
1204 | int readlink_and_canonicalize(const char *p, char **r) { |
1205 | char *t, *s; | |
1206 | int j; | |
1207 | ||
1208 | assert(p); | |
1209 | assert(r); | |
1210 | ||
1211 | j = readlink_and_make_absolute(p, &t); | |
1212 | if (j < 0) | |
1213 | return j; | |
1214 | ||
1215 | s = canonicalize_file_name(t); | |
1216 | if (s) { | |
1217 | free(t); | |
1218 | *r = s; | |
1219 | } else | |
1220 | *r = t; | |
1221 | ||
1222 | path_kill_slashes(*r); | |
1223 | ||
1224 | return 0; | |
1225 | } | |
1226 | ||
35d2e7ec LP |
1227 | int parent_of_path(const char *path, char **_r) { |
1228 | const char *e, *a = NULL, *b = NULL, *p; | |
1229 | char *r; | |
1230 | bool slash = false; | |
1231 | ||
1232 | assert(path); | |
1233 | assert(_r); | |
1234 | ||
1235 | if (!*path) | |
1236 | return -EINVAL; | |
1237 | ||
1238 | for (e = path; *e; e++) { | |
1239 | ||
1240 | if (!slash && *e == '/') { | |
1241 | a = b; | |
1242 | b = e; | |
1243 | slash = true; | |
1244 | } else if (slash && *e != '/') | |
1245 | slash = false; | |
1246 | } | |
1247 | ||
1248 | if (*(e-1) == '/') | |
1249 | p = a; | |
1250 | else | |
1251 | p = b; | |
1252 | ||
1253 | if (!p) | |
1254 | return -EINVAL; | |
1255 | ||
1256 | if (p == path) | |
1257 | r = strdup("/"); | |
1258 | else | |
1259 | r = strndup(path, p-path); | |
1260 | ||
1261 | if (!r) | |
1262 | return -ENOMEM; | |
1263 | ||
1264 | *_r = r; | |
1265 | return 0; | |
1266 | } | |
1267 | ||
1268 | ||
87f0e418 LP |
1269 | char *file_name_from_path(const char *p) { |
1270 | char *r; | |
1271 | ||
1272 | assert(p); | |
1273 | ||
1274 | if ((r = strrchr(p, '/'))) | |
1275 | return r + 1; | |
1276 | ||
1277 | return (char*) p; | |
1278 | } | |
0301abf4 LP |
1279 | |
1280 | bool path_is_absolute(const char *p) { | |
1281 | assert(p); | |
1282 | ||
1283 | return p[0] == '/'; | |
1284 | } | |
1285 | ||
1286 | bool is_path(const char *p) { | |
1287 | ||
1288 | return !!strchr(p, '/'); | |
1289 | } | |
1290 | ||
1291 | char *path_make_absolute(const char *p, const char *prefix) { | |
0301abf4 LP |
1292 | assert(p); |
1293 | ||
65d2ebdc LP |
1294 | /* Makes every item in the list an absolute path by prepending |
1295 | * the prefix, if specified and necessary */ | |
1296 | ||
0301abf4 LP |
1297 | if (path_is_absolute(p) || !prefix) |
1298 | return strdup(p); | |
1299 | ||
44d91056 | 1300 | return join(prefix, "/", p, NULL); |
0301abf4 | 1301 | } |
2a987ee8 | 1302 | |
65d2ebdc LP |
1303 | char *path_make_absolute_cwd(const char *p) { |
1304 | char *cwd, *r; | |
1305 | ||
1306 | assert(p); | |
1307 | ||
1308 | /* Similar to path_make_absolute(), but prefixes with the | |
1309 | * current working directory. */ | |
1310 | ||
1311 | if (path_is_absolute(p)) | |
1312 | return strdup(p); | |
1313 | ||
1314 | if (!(cwd = get_current_dir_name())) | |
1315 | return NULL; | |
1316 | ||
1317 | r = path_make_absolute(p, cwd); | |
1318 | free(cwd); | |
1319 | ||
1320 | return r; | |
1321 | } | |
1322 | ||
1323 | char **strv_path_make_absolute_cwd(char **l) { | |
1324 | char **s; | |
1325 | ||
1326 | /* Goes through every item in the string list and makes it | |
1327 | * absolute. This works in place and won't rollback any | |
1328 | * changes on failure. */ | |
1329 | ||
1330 | STRV_FOREACH(s, l) { | |
1331 | char *t; | |
1332 | ||
1333 | if (!(t = path_make_absolute_cwd(*s))) | |
1334 | return NULL; | |
1335 | ||
1336 | free(*s); | |
1337 | *s = t; | |
1338 | } | |
1339 | ||
1340 | return l; | |
1341 | } | |
1342 | ||
c3f6d675 LP |
1343 | char **strv_path_canonicalize(char **l) { |
1344 | char **s; | |
1345 | unsigned k = 0; | |
1346 | bool enomem = false; | |
1347 | ||
1348 | if (strv_isempty(l)) | |
1349 | return l; | |
1350 | ||
1351 | /* Goes through every item in the string list and canonicalize | |
1352 | * the path. This works in place and won't rollback any | |
1353 | * changes on failure. */ | |
1354 | ||
1355 | STRV_FOREACH(s, l) { | |
1356 | char *t, *u; | |
1357 | ||
1358 | t = path_make_absolute_cwd(*s); | |
1359 | free(*s); | |
1360 | ||
1361 | if (!t) { | |
1362 | enomem = true; | |
1363 | continue; | |
1364 | } | |
1365 | ||
1366 | errno = 0; | |
1367 | u = canonicalize_file_name(t); | |
1368 | free(t); | |
1369 | ||
1370 | if (!u) { | |
1371 | if (errno == ENOMEM || !errno) | |
1372 | enomem = true; | |
1373 | ||
1374 | continue; | |
1375 | } | |
1376 | ||
1377 | l[k++] = u; | |
1378 | } | |
1379 | ||
1380 | l[k] = NULL; | |
1381 | ||
1382 | if (enomem) | |
1383 | return NULL; | |
1384 | ||
1385 | return l; | |
a9dd2082 LP |
1386 | } |
1387 | ||
1388 | char **strv_path_remove_empty(char **l) { | |
1389 | char **f, **t; | |
1390 | ||
1391 | if (!l) | |
1392 | return NULL; | |
1393 | ||
1394 | for (f = t = l; *f; f++) { | |
1395 | ||
1396 | if (dir_is_empty(*f) > 0) { | |
1397 | free(*f); | |
1398 | continue; | |
1399 | } | |
1400 | ||
1401 | *(t++) = *f; | |
1402 | } | |
1403 | ||
1404 | *t = NULL; | |
1405 | return l; | |
c3f6d675 LP |
1406 | } |
1407 | ||
2a987ee8 LP |
1408 | int reset_all_signal_handlers(void) { |
1409 | int sig; | |
1410 | ||
1411 | for (sig = 1; sig < _NSIG; sig++) { | |
1412 | struct sigaction sa; | |
1413 | ||
1414 | if (sig == SIGKILL || sig == SIGSTOP) | |
1415 | continue; | |
1416 | ||
1417 | zero(sa); | |
1418 | sa.sa_handler = SIG_DFL; | |
431c32bf | 1419 | sa.sa_flags = SA_RESTART; |
2a987ee8 LP |
1420 | |
1421 | /* On Linux the first two RT signals are reserved by | |
1422 | * glibc, and sigaction() will return EINVAL for them. */ | |
1423 | if ((sigaction(sig, &sa, NULL) < 0)) | |
1424 | if (errno != EINVAL) | |
1425 | return -errno; | |
1426 | } | |
1427 | ||
8e274523 | 1428 | return 0; |
2a987ee8 | 1429 | } |
4a72ff34 LP |
1430 | |
1431 | char *strstrip(char *s) { | |
57a8eca8 | 1432 | char *e; |
4a72ff34 LP |
1433 | |
1434 | /* Drops trailing whitespace. Modifies the string in | |
1435 | * place. Returns pointer to first non-space character */ | |
1436 | ||
1437 | s += strspn(s, WHITESPACE); | |
1438 | ||
57a8eca8 LP |
1439 | for (e = strchr(s, 0); e > s; e --) |
1440 | if (!strchr(WHITESPACE, e[-1])) | |
1441 | break; | |
4a72ff34 | 1442 | |
57a8eca8 | 1443 | *e = 0; |
4a72ff34 LP |
1444 | |
1445 | return s; | |
4a72ff34 LP |
1446 | } |
1447 | ||
ee9b5e01 LP |
1448 | char *delete_chars(char *s, const char *bad) { |
1449 | char *f, *t; | |
1450 | ||
1451 | /* Drops all whitespace, regardless where in the string */ | |
1452 | ||
1453 | for (f = s, t = s; *f; f++) { | |
1454 | if (strchr(bad, *f)) | |
1455 | continue; | |
1456 | ||
1457 | *(t++) = *f; | |
1458 | } | |
1459 | ||
1460 | *t = 0; | |
1461 | ||
1462 | return s; | |
1463 | } | |
1464 | ||
ab1f0633 LP |
1465 | bool in_charset(const char *s, const char* charset) { |
1466 | const char *i; | |
1467 | ||
1468 | assert(s); | |
1469 | assert(charset); | |
1470 | ||
1471 | for (i = s; *i; i++) | |
1472 | if (!strchr(charset, *i)) | |
1473 | return false; | |
1474 | ||
1475 | return true; | |
1476 | } | |
1477 | ||
4a72ff34 LP |
1478 | char *file_in_same_dir(const char *path, const char *filename) { |
1479 | char *e, *r; | |
1480 | size_t k; | |
1481 | ||
1482 | assert(path); | |
1483 | assert(filename); | |
1484 | ||
1485 | /* This removes the last component of path and appends | |
1486 | * filename, unless the latter is absolute anyway or the | |
1487 | * former isn't */ | |
1488 | ||
1489 | if (path_is_absolute(filename)) | |
1490 | return strdup(filename); | |
1491 | ||
1492 | if (!(e = strrchr(path, '/'))) | |
1493 | return strdup(filename); | |
1494 | ||
1495 | k = strlen(filename); | |
1496 | if (!(r = new(char, e-path+1+k+1))) | |
1497 | return NULL; | |
1498 | ||
1499 | memcpy(r, path, e-path+1); | |
1500 | memcpy(r+(e-path)+1, filename, k+1); | |
1501 | ||
1502 | return r; | |
1503 | } | |
fb624d04 | 1504 | |
8c6db833 LP |
1505 | int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) { |
1506 | struct stat st; | |
1507 | ||
56cf987f | 1508 | if (label_mkdir(path, mode) >= 0) |
8c6db833 LP |
1509 | if (chmod_and_chown(path, mode, uid, gid) < 0) |
1510 | return -errno; | |
1511 | ||
1512 | if (lstat(path, &st) < 0) | |
1513 | return -errno; | |
1514 | ||
1515 | if ((st.st_mode & 0777) != mode || | |
1516 | st.st_uid != uid || | |
1517 | st.st_gid != gid || | |
1518 | !S_ISDIR(st.st_mode)) { | |
1519 | errno = EEXIST; | |
1520 | return -errno; | |
1521 | } | |
1522 | ||
1523 | return 0; | |
1524 | } | |
1525 | ||
1526 | ||
a9f5d454 LP |
1527 | int mkdir_parents(const char *path, mode_t mode) { |
1528 | const char *p, *e; | |
1529 | ||
1530 | assert(path); | |
1531 | ||
1532 | /* Creates every parent directory in the path except the last | |
1533 | * component. */ | |
1534 | ||
1535 | p = path + strspn(path, "/"); | |
1536 | for (;;) { | |
1537 | int r; | |
1538 | char *t; | |
1539 | ||
1540 | e = p + strcspn(p, "/"); | |
1541 | p = e + strspn(e, "/"); | |
1542 | ||
1543 | /* Is this the last component? If so, then we're | |
1544 | * done */ | |
1545 | if (*p == 0) | |
1546 | return 0; | |
1547 | ||
1548 | if (!(t = strndup(path, e - path))) | |
1549 | return -ENOMEM; | |
1550 | ||
56cf987f | 1551 | r = label_mkdir(t, mode); |
a9f5d454 LP |
1552 | free(t); |
1553 | ||
1554 | if (r < 0 && errno != EEXIST) | |
1555 | return -errno; | |
1556 | } | |
1557 | } | |
1558 | ||
bbd67135 LP |
1559 | int mkdir_p(const char *path, mode_t mode) { |
1560 | int r; | |
1561 | ||
1562 | /* Like mkdir -p */ | |
1563 | ||
1564 | if ((r = mkdir_parents(path, mode)) < 0) | |
1565 | return r; | |
1566 | ||
56cf987f | 1567 | if (label_mkdir(path, mode) < 0 && errno != EEXIST) |
bbd67135 LP |
1568 | return -errno; |
1569 | ||
1570 | return 0; | |
1571 | } | |
1572 | ||
c32dd69b LP |
1573 | int rmdir_parents(const char *path, const char *stop) { |
1574 | size_t l; | |
1575 | int r = 0; | |
1576 | ||
1577 | assert(path); | |
1578 | assert(stop); | |
1579 | ||
1580 | l = strlen(path); | |
1581 | ||
1582 | /* Skip trailing slashes */ | |
1583 | while (l > 0 && path[l-1] == '/') | |
1584 | l--; | |
1585 | ||
1586 | while (l > 0) { | |
1587 | char *t; | |
1588 | ||
1589 | /* Skip last component */ | |
1590 | while (l > 0 && path[l-1] != '/') | |
1591 | l--; | |
1592 | ||
1593 | /* Skip trailing slashes */ | |
1594 | while (l > 0 && path[l-1] == '/') | |
1595 | l--; | |
1596 | ||
1597 | if (l <= 0) | |
1598 | break; | |
1599 | ||
1600 | if (!(t = strndup(path, l))) | |
1601 | return -ENOMEM; | |
1602 | ||
1603 | if (path_startswith(stop, t)) { | |
1604 | free(t); | |
1605 | return 0; | |
1606 | } | |
1607 | ||
1608 | r = rmdir(t); | |
1609 | free(t); | |
1610 | ||
1611 | if (r < 0) | |
1612 | if (errno != ENOENT) | |
1613 | return -errno; | |
1614 | } | |
1615 | ||
1616 | return 0; | |
1617 | } | |
1618 | ||
1619 | ||
fb624d04 LP |
1620 | char hexchar(int x) { |
1621 | static const char table[16] = "0123456789abcdef"; | |
1622 | ||
1623 | return table[x & 15]; | |
1624 | } | |
4fe88d28 LP |
1625 | |
1626 | int unhexchar(char c) { | |
1627 | ||
1628 | if (c >= '0' && c <= '9') | |
1629 | return c - '0'; | |
1630 | ||
1631 | if (c >= 'a' && c <= 'f') | |
ea430986 | 1632 | return c - 'a' + 10; |
4fe88d28 LP |
1633 | |
1634 | if (c >= 'A' && c <= 'F') | |
ea430986 | 1635 | return c - 'A' + 10; |
4fe88d28 LP |
1636 | |
1637 | return -1; | |
1638 | } | |
1639 | ||
1640 | char octchar(int x) { | |
1641 | return '0' + (x & 7); | |
1642 | } | |
1643 | ||
1644 | int unoctchar(char c) { | |
1645 | ||
1646 | if (c >= '0' && c <= '7') | |
1647 | return c - '0'; | |
1648 | ||
1649 | return -1; | |
1650 | } | |
1651 | ||
5af98f82 LP |
1652 | char decchar(int x) { |
1653 | return '0' + (x % 10); | |
1654 | } | |
1655 | ||
1656 | int undecchar(char c) { | |
1657 | ||
1658 | if (c >= '0' && c <= '9') | |
1659 | return c - '0'; | |
1660 | ||
1661 | return -1; | |
1662 | } | |
1663 | ||
4fe88d28 LP |
1664 | char *cescape(const char *s) { |
1665 | char *r, *t; | |
1666 | const char *f; | |
1667 | ||
1668 | assert(s); | |
1669 | ||
1670 | /* Does C style string escaping. */ | |
1671 | ||
1672 | if (!(r = new(char, strlen(s)*4 + 1))) | |
1673 | return NULL; | |
1674 | ||
1675 | for (f = s, t = r; *f; f++) | |
1676 | ||
1677 | switch (*f) { | |
1678 | ||
1679 | case '\a': | |
1680 | *(t++) = '\\'; | |
1681 | *(t++) = 'a'; | |
1682 | break; | |
1683 | case '\b': | |
1684 | *(t++) = '\\'; | |
1685 | *(t++) = 'b'; | |
1686 | break; | |
1687 | case '\f': | |
1688 | *(t++) = '\\'; | |
1689 | *(t++) = 'f'; | |
1690 | break; | |
1691 | case '\n': | |
1692 | *(t++) = '\\'; | |
1693 | *(t++) = 'n'; | |
1694 | break; | |
1695 | case '\r': | |
1696 | *(t++) = '\\'; | |
1697 | *(t++) = 'r'; | |
1698 | break; | |
1699 | case '\t': | |
1700 | *(t++) = '\\'; | |
1701 | *(t++) = 't'; | |
1702 | break; | |
1703 | case '\v': | |
1704 | *(t++) = '\\'; | |
1705 | *(t++) = 'v'; | |
1706 | break; | |
1707 | case '\\': | |
1708 | *(t++) = '\\'; | |
1709 | *(t++) = '\\'; | |
1710 | break; | |
1711 | case '"': | |
1712 | *(t++) = '\\'; | |
1713 | *(t++) = '"'; | |
1714 | break; | |
1715 | case '\'': | |
1716 | *(t++) = '\\'; | |
1717 | *(t++) = '\''; | |
1718 | break; | |
1719 | ||
1720 | default: | |
1721 | /* For special chars we prefer octal over | |
1722 | * hexadecimal encoding, simply because glib's | |
1723 | * g_strescape() does the same */ | |
1724 | if ((*f < ' ') || (*f >= 127)) { | |
1725 | *(t++) = '\\'; | |
1726 | *(t++) = octchar((unsigned char) *f >> 6); | |
1727 | *(t++) = octchar((unsigned char) *f >> 3); | |
1728 | *(t++) = octchar((unsigned char) *f); | |
1729 | } else | |
1730 | *(t++) = *f; | |
1731 | break; | |
1732 | } | |
1733 | ||
1734 | *t = 0; | |
1735 | ||
1736 | return r; | |
1737 | } | |
1738 | ||
6febfd0d | 1739 | char *cunescape_length(const char *s, size_t length) { |
4fe88d28 LP |
1740 | char *r, *t; |
1741 | const char *f; | |
1742 | ||
1743 | assert(s); | |
1744 | ||
1745 | /* Undoes C style string escaping */ | |
1746 | ||
6febfd0d | 1747 | if (!(r = new(char, length+1))) |
4fe88d28 LP |
1748 | return r; |
1749 | ||
6febfd0d | 1750 | for (f = s, t = r; f < s + length; f++) { |
4fe88d28 LP |
1751 | |
1752 | if (*f != '\\') { | |
1753 | *(t++) = *f; | |
1754 | continue; | |
1755 | } | |
1756 | ||
1757 | f++; | |
1758 | ||
1759 | switch (*f) { | |
1760 | ||
1761 | case 'a': | |
1762 | *(t++) = '\a'; | |
1763 | break; | |
1764 | case 'b': | |
1765 | *(t++) = '\b'; | |
1766 | break; | |
1767 | case 'f': | |
1768 | *(t++) = '\f'; | |
1769 | break; | |
1770 | case 'n': | |
1771 | *(t++) = '\n'; | |
1772 | break; | |
1773 | case 'r': | |
1774 | *(t++) = '\r'; | |
1775 | break; | |
1776 | case 't': | |
1777 | *(t++) = '\t'; | |
1778 | break; | |
1779 | case 'v': | |
1780 | *(t++) = '\v'; | |
1781 | break; | |
1782 | case '\\': | |
1783 | *(t++) = '\\'; | |
1784 | break; | |
1785 | case '"': | |
1786 | *(t++) = '"'; | |
1787 | break; | |
1788 | case '\'': | |
1789 | *(t++) = '\''; | |
1790 | break; | |
1791 | ||
e167fb86 LP |
1792 | case 's': |
1793 | /* This is an extension of the XDG syntax files */ | |
1794 | *(t++) = ' '; | |
1795 | break; | |
1796 | ||
4fe88d28 LP |
1797 | case 'x': { |
1798 | /* hexadecimal encoding */ | |
1799 | int a, b; | |
1800 | ||
1801 | if ((a = unhexchar(f[1])) < 0 || | |
1802 | (b = unhexchar(f[2])) < 0) { | |
1803 | /* Invalid escape code, let's take it literal then */ | |
1804 | *(t++) = '\\'; | |
1805 | *(t++) = 'x'; | |
1806 | } else { | |
1807 | *(t++) = (char) ((a << 4) | b); | |
1808 | f += 2; | |
1809 | } | |
1810 | ||
1811 | break; | |
1812 | } | |
1813 | ||
1814 | case '0': | |
1815 | case '1': | |
1816 | case '2': | |
1817 | case '3': | |
1818 | case '4': | |
1819 | case '5': | |
1820 | case '6': | |
1821 | case '7': { | |
1822 | /* octal encoding */ | |
1823 | int a, b, c; | |
1824 | ||
1825 | if ((a = unoctchar(f[0])) < 0 || | |
1826 | (b = unoctchar(f[1])) < 0 || | |
1827 | (c = unoctchar(f[2])) < 0) { | |
1828 | /* Invalid escape code, let's take it literal then */ | |
1829 | *(t++) = '\\'; | |
1830 | *(t++) = f[0]; | |
1831 | } else { | |
1832 | *(t++) = (char) ((a << 6) | (b << 3) | c); | |
1833 | f += 2; | |
1834 | } | |
1835 | ||
1836 | break; | |
1837 | } | |
1838 | ||
1839 | case 0: | |
1840 | /* premature end of string.*/ | |
1841 | *(t++) = '\\'; | |
1842 | goto finish; | |
1843 | ||
1844 | default: | |
1845 | /* Invalid escape code, let's take it literal then */ | |
1846 | *(t++) = '\\'; | |
f3d4cc01 | 1847 | *(t++) = *f; |
4fe88d28 LP |
1848 | break; |
1849 | } | |
1850 | } | |
1851 | ||
1852 | finish: | |
1853 | *t = 0; | |
1854 | return r; | |
1855 | } | |
1856 | ||
6febfd0d LP |
1857 | char *cunescape(const char *s) { |
1858 | return cunescape_length(s, strlen(s)); | |
1859 | } | |
4fe88d28 LP |
1860 | |
1861 | char *xescape(const char *s, const char *bad) { | |
1862 | char *r, *t; | |
1863 | const char *f; | |
1864 | ||
1865 | /* Escapes all chars in bad, in addition to \ and all special | |
1866 | * chars, in \xFF style escaping. May be reversed with | |
1867 | * cunescape. */ | |
1868 | ||
1869 | if (!(r = new(char, strlen(s)*4+1))) | |
1870 | return NULL; | |
1871 | ||
1872 | for (f = s, t = r; *f; f++) { | |
1873 | ||
b866264a LP |
1874 | if ((*f < ' ') || (*f >= 127) || |
1875 | (*f == '\\') || strchr(bad, *f)) { | |
4fe88d28 LP |
1876 | *(t++) = '\\'; |
1877 | *(t++) = 'x'; | |
1878 | *(t++) = hexchar(*f >> 4); | |
1879 | *(t++) = hexchar(*f); | |
1880 | } else | |
1881 | *(t++) = *f; | |
1882 | } | |
1883 | ||
1884 | *t = 0; | |
1885 | ||
1886 | return r; | |
1887 | } | |
1888 | ||
ea430986 | 1889 | char *bus_path_escape(const char *s) { |
ea430986 LP |
1890 | char *r, *t; |
1891 | const char *f; | |
1892 | ||
47be870b LP |
1893 | assert(s); |
1894 | ||
ea430986 LP |
1895 | /* Escapes all chars that D-Bus' object path cannot deal |
1896 | * with. Can be reverse with bus_path_unescape() */ | |
1897 | ||
1898 | if (!(r = new(char, strlen(s)*3+1))) | |
1899 | return NULL; | |
1900 | ||
1901 | for (f = s, t = r; *f; f++) { | |
1902 | ||
1903 | if (!(*f >= 'A' && *f <= 'Z') && | |
1904 | !(*f >= 'a' && *f <= 'z') && | |
1905 | !(*f >= '0' && *f <= '9')) { | |
1906 | *(t++) = '_'; | |
1907 | *(t++) = hexchar(*f >> 4); | |
1908 | *(t++) = hexchar(*f); | |
1909 | } else | |
1910 | *(t++) = *f; | |
1911 | } | |
1912 | ||
1913 | *t = 0; | |
1914 | ||
1915 | return r; | |
1916 | } | |
1917 | ||
9e2f7c11 | 1918 | char *bus_path_unescape(const char *f) { |
ea430986 | 1919 | char *r, *t; |
ea430986 | 1920 | |
9e2f7c11 | 1921 | assert(f); |
47be870b | 1922 | |
9e2f7c11 | 1923 | if (!(r = strdup(f))) |
ea430986 LP |
1924 | return NULL; |
1925 | ||
9e2f7c11 | 1926 | for (t = r; *f; f++) { |
ea430986 LP |
1927 | |
1928 | if (*f == '_') { | |
1929 | int a, b; | |
1930 | ||
1931 | if ((a = unhexchar(f[1])) < 0 || | |
1932 | (b = unhexchar(f[2])) < 0) { | |
1933 | /* Invalid escape code, let's take it literal then */ | |
1934 | *(t++) = '_'; | |
1935 | } else { | |
1936 | *(t++) = (char) ((a << 4) | b); | |
1937 | f += 2; | |
1938 | } | |
1939 | } else | |
1940 | *(t++) = *f; | |
1941 | } | |
1942 | ||
1943 | *t = 0; | |
1944 | ||
1945 | return r; | |
1946 | } | |
1947 | ||
4fe88d28 LP |
1948 | char *path_kill_slashes(char *path) { |
1949 | char *f, *t; | |
1950 | bool slash = false; | |
1951 | ||
1952 | /* Removes redundant inner and trailing slashes. Modifies the | |
1953 | * passed string in-place. | |
1954 | * | |
1955 | * ///foo///bar/ becomes /foo/bar | |
1956 | */ | |
1957 | ||
1958 | for (f = path, t = path; *f; f++) { | |
1959 | ||
1960 | if (*f == '/') { | |
1961 | slash = true; | |
1962 | continue; | |
1963 | } | |
1964 | ||
1965 | if (slash) { | |
1966 | slash = false; | |
1967 | *(t++) = '/'; | |
1968 | } | |
1969 | ||
1970 | *(t++) = *f; | |
1971 | } | |
1972 | ||
1973 | /* Special rule, if we are talking of the root directory, a | |
1974 | trailing slash is good */ | |
1975 | ||
1976 | if (t == path && slash) | |
1977 | *(t++) = '/'; | |
1978 | ||
1979 | *t = 0; | |
1980 | return path; | |
1981 | } | |
1982 | ||
1983 | bool path_startswith(const char *path, const char *prefix) { | |
1984 | assert(path); | |
1985 | assert(prefix); | |
1986 | ||
1987 | if ((path[0] == '/') != (prefix[0] == '/')) | |
1988 | return false; | |
1989 | ||
1990 | for (;;) { | |
1991 | size_t a, b; | |
1992 | ||
1993 | path += strspn(path, "/"); | |
1994 | prefix += strspn(prefix, "/"); | |
1995 | ||
1996 | if (*prefix == 0) | |
1997 | return true; | |
1998 | ||
1999 | if (*path == 0) | |
2000 | return false; | |
2001 | ||
2002 | a = strcspn(path, "/"); | |
2003 | b = strcspn(prefix, "/"); | |
2004 | ||
2005 | if (a != b) | |
2006 | return false; | |
2007 | ||
2008 | if (memcmp(path, prefix, a) != 0) | |
2009 | return false; | |
2010 | ||
2011 | path += a; | |
2012 | prefix += b; | |
2013 | } | |
2014 | } | |
2015 | ||
15ae422b LP |
2016 | bool path_equal(const char *a, const char *b) { |
2017 | assert(a); | |
2018 | assert(b); | |
2019 | ||
2020 | if ((a[0] == '/') != (b[0] == '/')) | |
2021 | return false; | |
2022 | ||
2023 | for (;;) { | |
2024 | size_t j, k; | |
2025 | ||
2026 | a += strspn(a, "/"); | |
2027 | b += strspn(b, "/"); | |
2028 | ||
2029 | if (*a == 0 && *b == 0) | |
2030 | return true; | |
2031 | ||
2032 | if (*a == 0 || *b == 0) | |
2033 | return false; | |
2034 | ||
2035 | j = strcspn(a, "/"); | |
2036 | k = strcspn(b, "/"); | |
2037 | ||
2038 | if (j != k) | |
2039 | return false; | |
2040 | ||
2041 | if (memcmp(a, b, j) != 0) | |
2042 | return false; | |
2043 | ||
2044 | a += j; | |
2045 | b += k; | |
2046 | } | |
2047 | } | |
2048 | ||
67d51650 | 2049 | char *ascii_strlower(char *t) { |
4fe88d28 LP |
2050 | char *p; |
2051 | ||
67d51650 | 2052 | assert(t); |
4fe88d28 | 2053 | |
67d51650 | 2054 | for (p = t; *p; p++) |
4fe88d28 LP |
2055 | if (*p >= 'A' && *p <= 'Z') |
2056 | *p = *p - 'A' + 'a'; | |
2057 | ||
67d51650 | 2058 | return t; |
4fe88d28 | 2059 | } |
1dccbe19 | 2060 | |
c85dc17b LP |
2061 | bool ignore_file(const char *filename) { |
2062 | assert(filename); | |
2063 | ||
2064 | return | |
2065 | filename[0] == '.' || | |
6c78be3c | 2066 | streq(filename, "lost+found") || |
e472d476 LP |
2067 | streq(filename, "aquota.user") || |
2068 | streq(filename, "aquota.group") || | |
c85dc17b LP |
2069 | endswith(filename, "~") || |
2070 | endswith(filename, ".rpmnew") || | |
2071 | endswith(filename, ".rpmsave") || | |
2072 | endswith(filename, ".rpmorig") || | |
2073 | endswith(filename, ".dpkg-old") || | |
2074 | endswith(filename, ".dpkg-new") || | |
2075 | endswith(filename, ".swp"); | |
2076 | } | |
2077 | ||
3a0ecb08 LP |
2078 | int fd_nonblock(int fd, bool nonblock) { |
2079 | int flags; | |
2080 | ||
2081 | assert(fd >= 0); | |
2082 | ||
2083 | if ((flags = fcntl(fd, F_GETFL, 0)) < 0) | |
2084 | return -errno; | |
2085 | ||
2086 | if (nonblock) | |
2087 | flags |= O_NONBLOCK; | |
2088 | else | |
2089 | flags &= ~O_NONBLOCK; | |
2090 | ||
2091 | if (fcntl(fd, F_SETFL, flags) < 0) | |
2092 | return -errno; | |
2093 | ||
2094 | return 0; | |
2095 | } | |
2096 | ||
2097 | int fd_cloexec(int fd, bool cloexec) { | |
2098 | int flags; | |
2099 | ||
2100 | assert(fd >= 0); | |
2101 | ||
2102 | if ((flags = fcntl(fd, F_GETFD, 0)) < 0) | |
2103 | return -errno; | |
2104 | ||
2105 | if (cloexec) | |
2106 | flags |= FD_CLOEXEC; | |
2107 | else | |
2108 | flags &= ~FD_CLOEXEC; | |
2109 | ||
2110 | if (fcntl(fd, F_SETFD, flags) < 0) | |
2111 | return -errno; | |
2112 | ||
2113 | return 0; | |
2114 | } | |
2115 | ||
a0d40ac5 LP |
2116 | int close_all_fds(const int except[], unsigned n_except) { |
2117 | DIR *d; | |
2118 | struct dirent *de; | |
2119 | int r = 0; | |
2120 | ||
2121 | if (!(d = opendir("/proc/self/fd"))) | |
2122 | return -errno; | |
2123 | ||
2124 | while ((de = readdir(d))) { | |
a7610064 | 2125 | int fd = -1; |
a0d40ac5 | 2126 | |
a16e1123 | 2127 | if (ignore_file(de->d_name)) |
a0d40ac5 LP |
2128 | continue; |
2129 | ||
720ce21d LP |
2130 | if (safe_atoi(de->d_name, &fd) < 0) |
2131 | /* Let's better ignore this, just in case */ | |
2132 | continue; | |
a0d40ac5 LP |
2133 | |
2134 | if (fd < 3) | |
2135 | continue; | |
2136 | ||
2137 | if (fd == dirfd(d)) | |
2138 | continue; | |
2139 | ||
2140 | if (except) { | |
2141 | bool found; | |
2142 | unsigned i; | |
2143 | ||
2144 | found = false; | |
2145 | for (i = 0; i < n_except; i++) | |
2146 | if (except[i] == fd) { | |
2147 | found = true; | |
2148 | break; | |
2149 | } | |
2150 | ||
2151 | if (found) | |
2152 | continue; | |
2153 | } | |
2154 | ||
720ce21d | 2155 | if (close_nointr(fd) < 0) { |
2f357920 | 2156 | /* Valgrind has its own FD and doesn't want to have it closed */ |
720ce21d LP |
2157 | if (errno != EBADF && r == 0) |
2158 | r = -errno; | |
2f357920 | 2159 | } |
a0d40ac5 LP |
2160 | } |
2161 | ||
a0d40ac5 LP |
2162 | closedir(d); |
2163 | return r; | |
2164 | } | |
2165 | ||
db12775d LP |
2166 | bool chars_intersect(const char *a, const char *b) { |
2167 | const char *p; | |
2168 | ||
2169 | /* Returns true if any of the chars in a are in b. */ | |
2170 | for (p = a; *p; p++) | |
2171 | if (strchr(b, *p)) | |
2172 | return true; | |
2173 | ||
2174 | return false; | |
2175 | } | |
2176 | ||
8b6c7120 LP |
2177 | char *format_timestamp(char *buf, size_t l, usec_t t) { |
2178 | struct tm tm; | |
2179 | time_t sec; | |
2180 | ||
2181 | assert(buf); | |
2182 | assert(l > 0); | |
2183 | ||
2184 | if (t <= 0) | |
2185 | return NULL; | |
2186 | ||
f872ec33 | 2187 | sec = (time_t) (t / USEC_PER_SEC); |
8b6c7120 LP |
2188 | |
2189 | if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0) | |
2190 | return NULL; | |
2191 | ||
2192 | return buf; | |
2193 | } | |
2194 | ||
584be568 LP |
2195 | char *format_timestamp_pretty(char *buf, size_t l, usec_t t) { |
2196 | usec_t n, d; | |
2197 | ||
2198 | n = now(CLOCK_REALTIME); | |
2199 | ||
2200 | if (t <= 0 || t > n || t + USEC_PER_DAY*7 <= t) | |
2201 | return NULL; | |
2202 | ||
2203 | d = n - t; | |
2204 | ||
2205 | if (d >= USEC_PER_YEAR) | |
2206 | snprintf(buf, l, "%llu years and %llu months ago", | |
2207 | (unsigned long long) (d / USEC_PER_YEAR), | |
2208 | (unsigned long long) ((d % USEC_PER_YEAR) / USEC_PER_MONTH)); | |
2209 | else if (d >= USEC_PER_MONTH) | |
2210 | snprintf(buf, l, "%llu months and %llu days ago", | |
2211 | (unsigned long long) (d / USEC_PER_MONTH), | |
2212 | (unsigned long long) ((d % USEC_PER_MONTH) / USEC_PER_DAY)); | |
2213 | else if (d >= USEC_PER_WEEK) | |
2214 | snprintf(buf, l, "%llu weeks and %llu days ago", | |
2215 | (unsigned long long) (d / USEC_PER_WEEK), | |
2216 | (unsigned long long) ((d % USEC_PER_WEEK) / USEC_PER_DAY)); | |
2217 | else if (d >= 2*USEC_PER_DAY) | |
2218 | snprintf(buf, l, "%llu days ago", (unsigned long long) (d / USEC_PER_DAY)); | |
2219 | else if (d >= 25*USEC_PER_HOUR) | |
2220 | snprintf(buf, l, "1 day and %lluh ago", | |
2221 | (unsigned long long) ((d - USEC_PER_DAY) / USEC_PER_HOUR)); | |
2222 | else if (d >= 6*USEC_PER_HOUR) | |
2223 | snprintf(buf, l, "%lluh ago", | |
2224 | (unsigned long long) (d / USEC_PER_HOUR)); | |
2225 | else if (d >= USEC_PER_HOUR) | |
2226 | snprintf(buf, l, "%lluh %llumin ago", | |
2227 | (unsigned long long) (d / USEC_PER_HOUR), | |
2228 | (unsigned long long) ((d % USEC_PER_HOUR) / USEC_PER_MINUTE)); | |
2229 | else if (d >= 5*USEC_PER_MINUTE) | |
2230 | snprintf(buf, l, "%llumin ago", | |
2231 | (unsigned long long) (d / USEC_PER_MINUTE)); | |
2232 | else if (d >= USEC_PER_MINUTE) | |
2233 | snprintf(buf, l, "%llumin %llus ago", | |
2234 | (unsigned long long) (d / USEC_PER_MINUTE), | |
2235 | (unsigned long long) ((d % USEC_PER_MINUTE) / USEC_PER_SEC)); | |
2236 | else if (d >= USEC_PER_SEC) | |
2237 | snprintf(buf, l, "%llus ago", | |
2238 | (unsigned long long) (d / USEC_PER_SEC)); | |
2239 | else if (d >= USEC_PER_MSEC) | |
2240 | snprintf(buf, l, "%llums ago", | |
2241 | (unsigned long long) (d / USEC_PER_MSEC)); | |
2242 | else if (d > 0) | |
2243 | snprintf(buf, l, "%lluus ago", | |
2244 | (unsigned long long) d); | |
2245 | else | |
2246 | snprintf(buf, l, "now"); | |
2247 | ||
2248 | buf[l-1] = 0; | |
2249 | return buf; | |
2250 | } | |
2251 | ||
871d7de4 LP |
2252 | char *format_timespan(char *buf, size_t l, usec_t t) { |
2253 | static const struct { | |
2254 | const char *suffix; | |
2255 | usec_t usec; | |
2256 | } table[] = { | |
2257 | { "w", USEC_PER_WEEK }, | |
2258 | { "d", USEC_PER_DAY }, | |
2259 | { "h", USEC_PER_HOUR }, | |
2260 | { "min", USEC_PER_MINUTE }, | |
2261 | { "s", USEC_PER_SEC }, | |
2262 | { "ms", USEC_PER_MSEC }, | |
2263 | { "us", 1 }, | |
2264 | }; | |
2265 | ||
2266 | unsigned i; | |
2267 | char *p = buf; | |
2268 | ||
2269 | assert(buf); | |
2270 | assert(l > 0); | |
2271 | ||
2272 | if (t == (usec_t) -1) | |
2273 | return NULL; | |
2274 | ||
4502d22c LP |
2275 | if (t == 0) { |
2276 | snprintf(p, l, "0"); | |
2277 | p[l-1] = 0; | |
2278 | return p; | |
2279 | } | |
2280 | ||
871d7de4 LP |
2281 | /* The result of this function can be parsed with parse_usec */ |
2282 | ||
2283 | for (i = 0; i < ELEMENTSOF(table); i++) { | |
2284 | int k; | |
2285 | size_t n; | |
2286 | ||
2287 | if (t < table[i].usec) | |
2288 | continue; | |
2289 | ||
2290 | if (l <= 1) | |
2291 | break; | |
2292 | ||
2293 | k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix); | |
2294 | n = MIN((size_t) k, l); | |
2295 | ||
2296 | l -= n; | |
2297 | p += n; | |
2298 | ||
2299 | t %= table[i].usec; | |
2300 | } | |
2301 | ||
2302 | *p = 0; | |
2303 | ||
2304 | return buf; | |
2305 | } | |
2306 | ||
42856c10 LP |
2307 | bool fstype_is_network(const char *fstype) { |
2308 | static const char * const table[] = { | |
2309 | "cifs", | |
2310 | "smbfs", | |
2311 | "ncpfs", | |
2312 | "nfs", | |
ca139f94 LP |
2313 | "nfs4", |
2314 | "gfs", | |
2315 | "gfs2" | |
42856c10 LP |
2316 | }; |
2317 | ||
2318 | unsigned i; | |
2319 | ||
2320 | for (i = 0; i < ELEMENTSOF(table); i++) | |
2321 | if (streq(table[i], fstype)) | |
2322 | return true; | |
2323 | ||
2324 | return false; | |
2325 | } | |
2326 | ||
601f6a1e LP |
2327 | int chvt(int vt) { |
2328 | int fd, r = 0; | |
2329 | ||
74bc3bdc | 2330 | if ((fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0) |
601f6a1e LP |
2331 | return -errno; |
2332 | ||
2333 | if (vt < 0) { | |
2334 | int tiocl[2] = { | |
2335 | TIOCL_GETKMSGREDIRECT, | |
2336 | 0 | |
2337 | }; | |
2338 | ||
678abaf9 TJ |
2339 | if (ioctl(fd, TIOCLINUX, tiocl) < 0) { |
2340 | r = -errno; | |
2341 | goto fail; | |
2342 | } | |
601f6a1e LP |
2343 | |
2344 | vt = tiocl[0] <= 0 ? 1 : tiocl[0]; | |
2345 | } | |
2346 | ||
2347 | if (ioctl(fd, VT_ACTIVATE, vt) < 0) | |
2348 | r = -errno; | |
2349 | ||
678abaf9 TJ |
2350 | fail: |
2351 | close_nointr_nofail(fd); | |
601f6a1e LP |
2352 | return r; |
2353 | } | |
2354 | ||
80876c20 LP |
2355 | int read_one_char(FILE *f, char *ret, bool *need_nl) { |
2356 | struct termios old_termios, new_termios; | |
2357 | char c; | |
20c03b7b | 2358 | char line[LINE_MAX]; |
80876c20 LP |
2359 | |
2360 | assert(f); | |
2361 | assert(ret); | |
2362 | ||
2363 | if (tcgetattr(fileno(f), &old_termios) >= 0) { | |
2364 | new_termios = old_termios; | |
2365 | ||
2366 | new_termios.c_lflag &= ~ICANON; | |
2367 | new_termios.c_cc[VMIN] = 1; | |
2368 | new_termios.c_cc[VTIME] = 0; | |
2369 | ||
2370 | if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) { | |
2371 | size_t k; | |
2372 | ||
2373 | k = fread(&c, 1, 1, f); | |
2374 | ||
2375 | tcsetattr(fileno(f), TCSADRAIN, &old_termios); | |
2376 | ||
2377 | if (k <= 0) | |
2378 | return -EIO; | |
2379 | ||
2380 | if (need_nl) | |
2381 | *need_nl = c != '\n'; | |
2382 | ||
2383 | *ret = c; | |
2384 | return 0; | |
2385 | } | |
2386 | } | |
2387 | ||
2388 | if (!(fgets(line, sizeof(line), f))) | |
2389 | return -EIO; | |
2390 | ||
2391 | truncate_nl(line); | |
2392 | ||
2393 | if (strlen(line) != 1) | |
2394 | return -EBADMSG; | |
2395 | ||
2396 | if (need_nl) | |
2397 | *need_nl = false; | |
2398 | ||
2399 | *ret = line[0]; | |
2400 | return 0; | |
2401 | } | |
2402 | ||
2403 | int ask(char *ret, const char *replies, const char *text, ...) { | |
1b39d4b9 LP |
2404 | bool on_tty; |
2405 | ||
80876c20 LP |
2406 | assert(ret); |
2407 | assert(replies); | |
2408 | assert(text); | |
2409 | ||
1b39d4b9 LP |
2410 | on_tty = isatty(STDOUT_FILENO); |
2411 | ||
80876c20 LP |
2412 | for (;;) { |
2413 | va_list ap; | |
2414 | char c; | |
2415 | int r; | |
2416 | bool need_nl = true; | |
2417 | ||
1b39d4b9 LP |
2418 | if (on_tty) |
2419 | fputs("\x1B[1m", stdout); | |
b1b2dc0c | 2420 | |
80876c20 LP |
2421 | va_start(ap, text); |
2422 | vprintf(text, ap); | |
2423 | va_end(ap); | |
2424 | ||
1b39d4b9 LP |
2425 | if (on_tty) |
2426 | fputs("\x1B[0m", stdout); | |
b1b2dc0c | 2427 | |
80876c20 LP |
2428 | fflush(stdout); |
2429 | ||
2430 | if ((r = read_one_char(stdin, &c, &need_nl)) < 0) { | |
2431 | ||
2432 | if (r == -EBADMSG) { | |
2433 | puts("Bad input, please try again."); | |
2434 | continue; | |
2435 | } | |
2436 | ||
2437 | putchar('\n'); | |
2438 | return r; | |
2439 | } | |
2440 | ||
2441 | if (need_nl) | |
2442 | putchar('\n'); | |
2443 | ||
2444 | if (strchr(replies, c)) { | |
2445 | *ret = c; | |
2446 | return 0; | |
2447 | } | |
2448 | ||
2449 | puts("Read unexpected character, please try again."); | |
2450 | } | |
2451 | } | |
2452 | ||
6ea832a2 | 2453 | int reset_terminal_fd(int fd) { |
80876c20 LP |
2454 | struct termios termios; |
2455 | int r = 0; | |
3fe5e5d4 LP |
2456 | long arg; |
2457 | ||
2458 | /* Set terminal to some sane defaults */ | |
80876c20 LP |
2459 | |
2460 | assert(fd >= 0); | |
2461 | ||
eed1d0e3 LP |
2462 | /* We leave locked terminal attributes untouched, so that |
2463 | * Plymouth may set whatever it wants to set, and we don't | |
2464 | * interfere with that. */ | |
3fe5e5d4 LP |
2465 | |
2466 | /* Disable exclusive mode, just in case */ | |
2467 | ioctl(fd, TIOCNXCL); | |
2468 | ||
2469 | /* Enable console unicode mode */ | |
2470 | arg = K_UNICODE; | |
2471 | ioctl(fd, KDSKBMODE, &arg); | |
80876c20 LP |
2472 | |
2473 | if (tcgetattr(fd, &termios) < 0) { | |
2474 | r = -errno; | |
2475 | goto finish; | |
2476 | } | |
2477 | ||
aaf694ca LP |
2478 | /* We only reset the stuff that matters to the software. How |
2479 | * hardware is set up we don't touch assuming that somebody | |
2480 | * else will do that for us */ | |
2481 | ||
2482 | termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC); | |
80876c20 LP |
2483 | termios.c_iflag |= ICRNL | IMAXBEL | IUTF8; |
2484 | termios.c_oflag |= ONLCR; | |
2485 | termios.c_cflag |= CREAD; | |
2486 | termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE; | |
2487 | ||
2488 | termios.c_cc[VINTR] = 03; /* ^C */ | |
2489 | termios.c_cc[VQUIT] = 034; /* ^\ */ | |
2490 | termios.c_cc[VERASE] = 0177; | |
2491 | termios.c_cc[VKILL] = 025; /* ^X */ | |
2492 | termios.c_cc[VEOF] = 04; /* ^D */ | |
2493 | termios.c_cc[VSTART] = 021; /* ^Q */ | |
2494 | termios.c_cc[VSTOP] = 023; /* ^S */ | |
2495 | termios.c_cc[VSUSP] = 032; /* ^Z */ | |
2496 | termios.c_cc[VLNEXT] = 026; /* ^V */ | |
2497 | termios.c_cc[VWERASE] = 027; /* ^W */ | |
2498 | termios.c_cc[VREPRINT] = 022; /* ^R */ | |
aaf694ca LP |
2499 | termios.c_cc[VEOL] = 0; |
2500 | termios.c_cc[VEOL2] = 0; | |
80876c20 LP |
2501 | |
2502 | termios.c_cc[VTIME] = 0; | |
2503 | termios.c_cc[VMIN] = 1; | |
2504 | ||
2505 | if (tcsetattr(fd, TCSANOW, &termios) < 0) | |
2506 | r = -errno; | |
2507 | ||
2508 | finish: | |
2509 | /* Just in case, flush all crap out */ | |
2510 | tcflush(fd, TCIOFLUSH); | |
2511 | ||
2512 | return r; | |
2513 | } | |
2514 | ||
6ea832a2 LP |
2515 | int reset_terminal(const char *name) { |
2516 | int fd, r; | |
2517 | ||
2518 | fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC); | |
2519 | if (fd < 0) | |
2520 | return fd; | |
2521 | ||
2522 | r = reset_terminal_fd(fd); | |
2523 | close_nointr_nofail(fd); | |
2524 | ||
2525 | return r; | |
2526 | } | |
2527 | ||
80876c20 LP |
2528 | int open_terminal(const char *name, int mode) { |
2529 | int fd, r; | |
f73f76ac | 2530 | unsigned c = 0; |
80876c20 | 2531 | |
f73f76ac LP |
2532 | /* |
2533 | * If a TTY is in the process of being closed opening it might | |
2534 | * cause EIO. This is horribly awful, but unlikely to be | |
2535 | * changed in the kernel. Hence we work around this problem by | |
2536 | * retrying a couple of times. | |
2537 | * | |
2538 | * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245 | |
2539 | */ | |
2540 | ||
2541 | for (;;) { | |
2542 | if ((fd = open(name, mode)) >= 0) | |
2543 | break; | |
2544 | ||
2545 | if (errno != EIO) | |
2546 | return -errno; | |
2547 | ||
2548 | if (c >= 20) | |
2549 | return -errno; | |
2550 | ||
2551 | usleep(50 * USEC_PER_MSEC); | |
2552 | c++; | |
2553 | } | |
2554 | ||
2555 | if (fd < 0) | |
80876c20 LP |
2556 | return -errno; |
2557 | ||
2558 | if ((r = isatty(fd)) < 0) { | |
2559 | close_nointr_nofail(fd); | |
2560 | return -errno; | |
2561 | } | |
2562 | ||
2563 | if (!r) { | |
2564 | close_nointr_nofail(fd); | |
2565 | return -ENOTTY; | |
2566 | } | |
2567 | ||
2568 | return fd; | |
2569 | } | |
2570 | ||
2571 | int flush_fd(int fd) { | |
2572 | struct pollfd pollfd; | |
2573 | ||
2574 | zero(pollfd); | |
2575 | pollfd.fd = fd; | |
2576 | pollfd.events = POLLIN; | |
2577 | ||
2578 | for (;;) { | |
20c03b7b | 2579 | char buf[LINE_MAX]; |
80876c20 LP |
2580 | ssize_t l; |
2581 | int r; | |
2582 | ||
2583 | if ((r = poll(&pollfd, 1, 0)) < 0) { | |
2584 | ||
2585 | if (errno == EINTR) | |
2586 | continue; | |
2587 | ||
2588 | return -errno; | |
2589 | } | |
2590 | ||
2591 | if (r == 0) | |
2592 | return 0; | |
2593 | ||
2594 | if ((l = read(fd, buf, sizeof(buf))) < 0) { | |
2595 | ||
2596 | if (errno == EINTR) | |
2597 | continue; | |
2598 | ||
2599 | if (errno == EAGAIN) | |
2600 | return 0; | |
2601 | ||
2602 | return -errno; | |
2603 | } | |
2604 | ||
2605 | if (l <= 0) | |
2606 | return 0; | |
2607 | } | |
2608 | } | |
2609 | ||
21de3988 | 2610 | int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) { |
bab45044 | 2611 | int fd = -1, notify = -1, r, wd = -1; |
80876c20 LP |
2612 | |
2613 | assert(name); | |
2614 | ||
2615 | /* We use inotify to be notified when the tty is closed. We | |
2616 | * create the watch before checking if we can actually acquire | |
2617 | * it, so that we don't lose any event. | |
2618 | * | |
2619 | * Note: strictly speaking this actually watches for the | |
2620 | * device being closed, it does *not* really watch whether a | |
2621 | * tty loses its controlling process. However, unless some | |
2622 | * rogue process uses TIOCNOTTY on /dev/tty *after* closing | |
2623 | * its tty otherwise this will not become a problem. As long | |
2624 | * as the administrator makes sure not configure any service | |
2625 | * on the same tty as an untrusted user this should not be a | |
2626 | * problem. (Which he probably should not do anyway.) */ | |
2627 | ||
2628 | if (!fail && !force) { | |
2629 | if ((notify = inotify_init1(IN_CLOEXEC)) < 0) { | |
2630 | r = -errno; | |
2631 | goto fail; | |
2632 | } | |
2633 | ||
2634 | if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) { | |
2635 | r = -errno; | |
2636 | goto fail; | |
2637 | } | |
2638 | } | |
2639 | ||
2640 | for (;;) { | |
e3d1855b LP |
2641 | if (notify >= 0) |
2642 | if ((r = flush_fd(notify)) < 0) | |
2643 | goto fail; | |
80876c20 LP |
2644 | |
2645 | /* We pass here O_NOCTTY only so that we can check the return | |
2646 | * value TIOCSCTTY and have a reliable way to figure out if we | |
2647 | * successfully became the controlling process of the tty */ | |
6ea832a2 LP |
2648 | if ((fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0) |
2649 | return fd; | |
80876c20 LP |
2650 | |
2651 | /* First, try to get the tty */ | |
21de3988 LP |
2652 | r = ioctl(fd, TIOCSCTTY, force); |
2653 | ||
2654 | /* Sometimes it makes sense to ignore TIOCSCTTY | |
2655 | * returning EPERM, i.e. when very likely we already | |
2656 | * are have this controlling terminal. */ | |
2657 | if (r < 0 && errno == EPERM && ignore_tiocstty_eperm) | |
2658 | r = 0; | |
2659 | ||
2660 | if (r < 0 && (force || fail || errno != EPERM)) { | |
80876c20 LP |
2661 | r = -errno; |
2662 | goto fail; | |
2663 | } | |
2664 | ||
2665 | if (r >= 0) | |
2666 | break; | |
2667 | ||
2668 | assert(!fail); | |
2669 | assert(!force); | |
2670 | assert(notify >= 0); | |
2671 | ||
2672 | for (;;) { | |
f601daa7 | 2673 | uint8_t inotify_buffer[sizeof(struct inotify_event) + FILENAME_MAX]; |
80876c20 | 2674 | ssize_t l; |
f601daa7 | 2675 | struct inotify_event *e; |
80876c20 | 2676 | |
f601daa7 | 2677 | if ((l = read(notify, &inotify_buffer, sizeof(inotify_buffer))) < 0) { |
80876c20 | 2678 | |
f601daa7 LP |
2679 | if (errno == EINTR) |
2680 | continue; | |
2681 | ||
2682 | r = -errno; | |
2683 | goto fail; | |
2684 | } | |
2685 | ||
2686 | e = (struct inotify_event*) inotify_buffer; | |
80876c20 | 2687 | |
f601daa7 LP |
2688 | while (l > 0) { |
2689 | size_t step; | |
80876c20 | 2690 | |
f601daa7 | 2691 | if (e->wd != wd || !(e->mask & IN_CLOSE)) { |
80876c20 | 2692 | r = -EIO; |
f601daa7 LP |
2693 | goto fail; |
2694 | } | |
80876c20 | 2695 | |
f601daa7 LP |
2696 | step = sizeof(struct inotify_event) + e->len; |
2697 | assert(step <= (size_t) l); | |
80876c20 | 2698 | |
f601daa7 LP |
2699 | e = (struct inotify_event*) ((uint8_t*) e + step); |
2700 | l -= step; | |
80876c20 LP |
2701 | } |
2702 | ||
2703 | break; | |
2704 | } | |
2705 | ||
2706 | /* We close the tty fd here since if the old session | |
2707 | * ended our handle will be dead. It's important that | |
2708 | * we do this after sleeping, so that we don't enter | |
2709 | * an endless loop. */ | |
2710 | close_nointr_nofail(fd); | |
2711 | } | |
2712 | ||
2713 | if (notify >= 0) | |
a16e1123 | 2714 | close_nointr_nofail(notify); |
80876c20 | 2715 | |
6ea832a2 | 2716 | if ((r = reset_terminal_fd(fd)) < 0) |
80876c20 LP |
2717 | log_warning("Failed to reset terminal: %s", strerror(-r)); |
2718 | ||
2719 | return fd; | |
2720 | ||
2721 | fail: | |
2722 | if (fd >= 0) | |
a16e1123 | 2723 | close_nointr_nofail(fd); |
80876c20 LP |
2724 | |
2725 | if (notify >= 0) | |
a16e1123 | 2726 | close_nointr_nofail(notify); |
80876c20 LP |
2727 | |
2728 | return r; | |
2729 | } | |
2730 | ||
2731 | int release_terminal(void) { | |
2732 | int r = 0, fd; | |
57cd2192 | 2733 | struct sigaction sa_old, sa_new; |
80876c20 | 2734 | |
ccaa6149 | 2735 | if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC)) < 0) |
80876c20 LP |
2736 | return -errno; |
2737 | ||
57cd2192 LP |
2738 | /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed |
2739 | * by our own TIOCNOTTY */ | |
2740 | ||
2741 | zero(sa_new); | |
2742 | sa_new.sa_handler = SIG_IGN; | |
2743 | sa_new.sa_flags = SA_RESTART; | |
2744 | assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0); | |
2745 | ||
80876c20 LP |
2746 | if (ioctl(fd, TIOCNOTTY) < 0) |
2747 | r = -errno; | |
2748 | ||
57cd2192 LP |
2749 | assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0); |
2750 | ||
80876c20 LP |
2751 | close_nointr_nofail(fd); |
2752 | return r; | |
2753 | } | |
2754 | ||
9a34ec5f LP |
2755 | int sigaction_many(const struct sigaction *sa, ...) { |
2756 | va_list ap; | |
2757 | int r = 0, sig; | |
2758 | ||
2759 | va_start(ap, sa); | |
2760 | while ((sig = va_arg(ap, int)) > 0) | |
2761 | if (sigaction(sig, sa, NULL) < 0) | |
2762 | r = -errno; | |
2763 | va_end(ap); | |
2764 | ||
2765 | return r; | |
2766 | } | |
2767 | ||
2768 | int ignore_signals(int sig, ...) { | |
a337c6fc | 2769 | struct sigaction sa; |
9a34ec5f LP |
2770 | va_list ap; |
2771 | int r = 0; | |
a337c6fc LP |
2772 | |
2773 | zero(sa); | |
2774 | sa.sa_handler = SIG_IGN; | |
2775 | sa.sa_flags = SA_RESTART; | |
2776 | ||
9a34ec5f LP |
2777 | if (sigaction(sig, &sa, NULL) < 0) |
2778 | r = -errno; | |
2779 | ||
2780 | va_start(ap, sig); | |
2781 | while ((sig = va_arg(ap, int)) > 0) | |
2782 | if (sigaction(sig, &sa, NULL) < 0) | |
2783 | r = -errno; | |
2784 | va_end(ap); | |
2785 | ||
2786 | return r; | |
2787 | } | |
2788 | ||
2789 | int default_signals(int sig, ...) { | |
2790 | struct sigaction sa; | |
2791 | va_list ap; | |
2792 | int r = 0; | |
2793 | ||
2794 | zero(sa); | |
2795 | sa.sa_handler = SIG_DFL; | |
2796 | sa.sa_flags = SA_RESTART; | |
2797 | ||
2798 | if (sigaction(sig, &sa, NULL) < 0) | |
2799 | r = -errno; | |
2800 | ||
2801 | va_start(ap, sig); | |
2802 | while ((sig = va_arg(ap, int)) > 0) | |
2803 | if (sigaction(sig, &sa, NULL) < 0) | |
2804 | r = -errno; | |
2805 | va_end(ap); | |
2806 | ||
2807 | return r; | |
a337c6fc LP |
2808 | } |
2809 | ||
8d567588 LP |
2810 | int close_pipe(int p[]) { |
2811 | int a = 0, b = 0; | |
2812 | ||
2813 | assert(p); | |
2814 | ||
2815 | if (p[0] >= 0) { | |
2816 | a = close_nointr(p[0]); | |
2817 | p[0] = -1; | |
2818 | } | |
2819 | ||
2820 | if (p[1] >= 0) { | |
2821 | b = close_nointr(p[1]); | |
2822 | p[1] = -1; | |
2823 | } | |
2824 | ||
2825 | return a < 0 ? a : b; | |
2826 | } | |
2827 | ||
eb22ac37 | 2828 | ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) { |
8d567588 LP |
2829 | uint8_t *p; |
2830 | ssize_t n = 0; | |
2831 | ||
2832 | assert(fd >= 0); | |
2833 | assert(buf); | |
2834 | ||
2835 | p = buf; | |
2836 | ||
2837 | while (nbytes > 0) { | |
2838 | ssize_t k; | |
2839 | ||
2840 | if ((k = read(fd, p, nbytes)) <= 0) { | |
2841 | ||
eb22ac37 | 2842 | if (k < 0 && errno == EINTR) |
8d567588 LP |
2843 | continue; |
2844 | ||
eb22ac37 | 2845 | if (k < 0 && errno == EAGAIN && do_poll) { |
8d567588 LP |
2846 | struct pollfd pollfd; |
2847 | ||
2848 | zero(pollfd); | |
2849 | pollfd.fd = fd; | |
2850 | pollfd.events = POLLIN; | |
2851 | ||
2852 | if (poll(&pollfd, 1, -1) < 0) { | |
2853 | if (errno == EINTR) | |
2854 | continue; | |
2855 | ||
2856 | return n > 0 ? n : -errno; | |
2857 | } | |
2858 | ||
2859 | if (pollfd.revents != POLLIN) | |
2860 | return n > 0 ? n : -EIO; | |
2861 | ||
2862 | continue; | |
2863 | } | |
2864 | ||
2865 | return n > 0 ? n : (k < 0 ? -errno : 0); | |
2866 | } | |
2867 | ||
2868 | p += k; | |
2869 | nbytes -= k; | |
2870 | n += k; | |
2871 | } | |
2872 | ||
2873 | return n; | |
2874 | } | |
2875 | ||
eb22ac37 LP |
2876 | ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) { |
2877 | const uint8_t *p; | |
2878 | ssize_t n = 0; | |
2879 | ||
2880 | assert(fd >= 0); | |
2881 | assert(buf); | |
2882 | ||
2883 | p = buf; | |
2884 | ||
2885 | while (nbytes > 0) { | |
2886 | ssize_t k; | |
2887 | ||
2888 | if ((k = write(fd, p, nbytes)) <= 0) { | |
2889 | ||
2890 | if (k < 0 && errno == EINTR) | |
2891 | continue; | |
2892 | ||
2893 | if (k < 0 && errno == EAGAIN && do_poll) { | |
2894 | struct pollfd pollfd; | |
2895 | ||
2896 | zero(pollfd); | |
2897 | pollfd.fd = fd; | |
2898 | pollfd.events = POLLOUT; | |
2899 | ||
2900 | if (poll(&pollfd, 1, -1) < 0) { | |
2901 | if (errno == EINTR) | |
2902 | continue; | |
2903 | ||
2904 | return n > 0 ? n : -errno; | |
2905 | } | |
2906 | ||
2907 | if (pollfd.revents != POLLOUT) | |
2908 | return n > 0 ? n : -EIO; | |
2909 | ||
2910 | continue; | |
2911 | } | |
2912 | ||
2913 | return n > 0 ? n : (k < 0 ? -errno : 0); | |
2914 | } | |
2915 | ||
2916 | p += k; | |
2917 | nbytes -= k; | |
2918 | n += k; | |
2919 | } | |
2920 | ||
2921 | return n; | |
2922 | } | |
2923 | ||
0c85a4f3 | 2924 | int path_is_mount_point(const char *t, bool allow_symlink) { |
8d567588 | 2925 | struct stat a, b; |
35d2e7ec LP |
2926 | char *parent; |
2927 | int r; | |
8d567588 | 2928 | |
0c85a4f3 LP |
2929 | if (allow_symlink) |
2930 | r = stat(t, &a); | |
2931 | else | |
2932 | r = lstat(t, &a); | |
2933 | ||
2934 | if (r < 0) { | |
8d567588 LP |
2935 | if (errno == ENOENT) |
2936 | return 0; | |
2937 | ||
2938 | return -errno; | |
2939 | } | |
2940 | ||
0c85a4f3 LP |
2941 | r = parent_of_path(t, &parent); |
2942 | if (r < 0) | |
35d2e7ec | 2943 | return r; |
8d567588 | 2944 | |
35d2e7ec LP |
2945 | r = lstat(parent, &b); |
2946 | free(parent); | |
8d567588 | 2947 | |
35d2e7ec LP |
2948 | if (r < 0) |
2949 | return -errno; | |
8d567588 LP |
2950 | |
2951 | return a.st_dev != b.st_dev; | |
2952 | } | |
2953 | ||
24a6e4a4 LP |
2954 | int parse_usec(const char *t, usec_t *usec) { |
2955 | static const struct { | |
2956 | const char *suffix; | |
2957 | usec_t usec; | |
2958 | } table[] = { | |
2959 | { "sec", USEC_PER_SEC }, | |
2960 | { "s", USEC_PER_SEC }, | |
2961 | { "min", USEC_PER_MINUTE }, | |
2962 | { "hr", USEC_PER_HOUR }, | |
2963 | { "h", USEC_PER_HOUR }, | |
2964 | { "d", USEC_PER_DAY }, | |
2965 | { "w", USEC_PER_WEEK }, | |
2966 | { "msec", USEC_PER_MSEC }, | |
2967 | { "ms", USEC_PER_MSEC }, | |
2968 | { "m", USEC_PER_MINUTE }, | |
2969 | { "usec", 1ULL }, | |
2970 | { "us", 1ULL }, | |
2971 | { "", USEC_PER_SEC }, | |
2972 | }; | |
2973 | ||
2974 | const char *p; | |
2975 | usec_t r = 0; | |
2976 | ||
2977 | assert(t); | |
2978 | assert(usec); | |
2979 | ||
2980 | p = t; | |
2981 | do { | |
2982 | long long l; | |
2983 | char *e; | |
2984 | unsigned i; | |
2985 | ||
2986 | errno = 0; | |
2987 | l = strtoll(p, &e, 10); | |
2988 | ||
2989 | if (errno != 0) | |
2990 | return -errno; | |
2991 | ||
2992 | if (l < 0) | |
2993 | return -ERANGE; | |
2994 | ||
2995 | if (e == p) | |
2996 | return -EINVAL; | |
2997 | ||
2998 | e += strspn(e, WHITESPACE); | |
2999 | ||
3000 | for (i = 0; i < ELEMENTSOF(table); i++) | |
3001 | if (startswith(e, table[i].suffix)) { | |
3002 | r += (usec_t) l * table[i].usec; | |
3003 | p = e + strlen(table[i].suffix); | |
3004 | break; | |
3005 | } | |
3006 | ||
3007 | if (i >= ELEMENTSOF(table)) | |
3008 | return -EINVAL; | |
3009 | ||
3010 | } while (*p != 0); | |
3011 | ||
3012 | *usec = r; | |
3013 | ||
3014 | return 0; | |
3015 | } | |
3016 | ||
ab1f0633 LP |
3017 | int parse_bytes(const char *t, off_t *bytes) { |
3018 | static const struct { | |
3019 | const char *suffix; | |
3020 | off_t factor; | |
3021 | } table[] = { | |
3022 | { "B", 1 }, | |
3023 | { "K", 1024ULL }, | |
3024 | { "M", 1024ULL*1024ULL }, | |
3025 | { "G", 1024ULL*1024ULL*1024ULL }, | |
3026 | { "T", 1024ULL*1024ULL*1024ULL*1024ULL }, | |
3027 | { "", 1 }, | |
3028 | }; | |
3029 | ||
3030 | const char *p; | |
3031 | off_t r = 0; | |
3032 | ||
3033 | assert(t); | |
3034 | assert(bytes); | |
3035 | ||
3036 | p = t; | |
3037 | do { | |
3038 | long long l; | |
3039 | char *e; | |
3040 | unsigned i; | |
3041 | ||
3042 | errno = 0; | |
3043 | l = strtoll(p, &e, 10); | |
3044 | ||
3045 | if (errno != 0) | |
3046 | return -errno; | |
3047 | ||
3048 | if (l < 0) | |
3049 | return -ERANGE; | |
3050 | ||
3051 | if (e == p) | |
3052 | return -EINVAL; | |
3053 | ||
3054 | e += strspn(e, WHITESPACE); | |
3055 | ||
3056 | for (i = 0; i < ELEMENTSOF(table); i++) | |
3057 | if (startswith(e, table[i].suffix)) { | |
3058 | r += (off_t) l * table[i].factor; | |
3059 | p = e + strlen(table[i].suffix); | |
3060 | break; | |
3061 | } | |
3062 | ||
3063 | if (i >= ELEMENTSOF(table)) | |
3064 | return -EINVAL; | |
3065 | ||
3066 | } while (*p != 0); | |
3067 | ||
3068 | *bytes = r; | |
3069 | ||
3070 | return 0; | |
3071 | } | |
3072 | ||
843d2643 LP |
3073 | int make_stdio(int fd) { |
3074 | int r, s, t; | |
3075 | ||
3076 | assert(fd >= 0); | |
3077 | ||
3078 | r = dup2(fd, STDIN_FILENO); | |
3079 | s = dup2(fd, STDOUT_FILENO); | |
3080 | t = dup2(fd, STDERR_FILENO); | |
3081 | ||
3082 | if (fd >= 3) | |
3083 | close_nointr_nofail(fd); | |
3084 | ||
3085 | if (r < 0 || s < 0 || t < 0) | |
3086 | return -errno; | |
3087 | ||
7862f62d LP |
3088 | fd_cloexec(STDIN_FILENO, false); |
3089 | fd_cloexec(STDOUT_FILENO, false); | |
3090 | fd_cloexec(STDERR_FILENO, false); | |
3091 | ||
843d2643 LP |
3092 | return 0; |
3093 | } | |
3094 | ||
ade509ce LP |
3095 | int make_null_stdio(void) { |
3096 | int null_fd; | |
3097 | ||
3098 | if ((null_fd = open("/dev/null", O_RDWR|O_NOCTTY)) < 0) | |
3099 | return -errno; | |
3100 | ||
3101 | return make_stdio(null_fd); | |
3102 | } | |
3103 | ||
8407a5d0 LP |
3104 | bool is_device_path(const char *path) { |
3105 | ||
3106 | /* Returns true on paths that refer to a device, either in | |
3107 | * sysfs or in /dev */ | |
3108 | ||
3109 | return | |
3110 | path_startswith(path, "/dev/") || | |
3111 | path_startswith(path, "/sys/"); | |
3112 | } | |
3113 | ||
01f78473 LP |
3114 | int dir_is_empty(const char *path) { |
3115 | DIR *d; | |
3116 | int r; | |
3117 | struct dirent buf, *de; | |
3118 | ||
3119 | if (!(d = opendir(path))) | |
3120 | return -errno; | |
3121 | ||
3122 | for (;;) { | |
3123 | if ((r = readdir_r(d, &buf, &de)) > 0) { | |
3124 | r = -r; | |
3125 | break; | |
3126 | } | |
3127 | ||
3128 | if (!de) { | |
3129 | r = 1; | |
3130 | break; | |
3131 | } | |
3132 | ||
3133 | if (!ignore_file(de->d_name)) { | |
3134 | r = 0; | |
3135 | break; | |
3136 | } | |
3137 | } | |
3138 | ||
3139 | closedir(d); | |
3140 | return r; | |
3141 | } | |
3142 | ||
d3782d60 LP |
3143 | unsigned long long random_ull(void) { |
3144 | int fd; | |
3145 | uint64_t ull; | |
3146 | ssize_t r; | |
3147 | ||
3148 | if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) | |
3149 | goto fallback; | |
3150 | ||
eb22ac37 | 3151 | r = loop_read(fd, &ull, sizeof(ull), true); |
d3782d60 LP |
3152 | close_nointr_nofail(fd); |
3153 | ||
3154 | if (r != sizeof(ull)) | |
3155 | goto fallback; | |
3156 | ||
3157 | return ull; | |
3158 | ||
3159 | fallback: | |
3160 | return random() * RAND_MAX + random(); | |
3161 | } | |
3162 | ||
5b6319dc LP |
3163 | void rename_process(const char name[8]) { |
3164 | assert(name); | |
3165 | ||
3166 | prctl(PR_SET_NAME, name); | |
3167 | ||
3168 | /* This is a like a poor man's setproctitle(). The string | |
3169 | * passed should fit in 7 chars (i.e. the length of | |
3170 | * "systemd") */ | |
3171 | ||
3172 | if (program_invocation_name) | |
3173 | strncpy(program_invocation_name, name, strlen(program_invocation_name)); | |
9a0e6896 LP |
3174 | |
3175 | if (saved_argc > 0) { | |
3176 | int i; | |
3177 | ||
3178 | if (saved_argv[0]) | |
3179 | strncpy(saved_argv[0], name, strlen(saved_argv[0])); | |
3180 | ||
3181 | for (i = 1; i < saved_argc; i++) { | |
3182 | if (!saved_argv[i]) | |
3183 | break; | |
3184 | ||
3185 | memset(saved_argv[i], 0, strlen(saved_argv[i])); | |
3186 | } | |
3187 | } | |
5b6319dc LP |
3188 | } |
3189 | ||
7d793605 LP |
3190 | void sigset_add_many(sigset_t *ss, ...) { |
3191 | va_list ap; | |
3192 | int sig; | |
3193 | ||
3194 | assert(ss); | |
3195 | ||
3196 | va_start(ap, ss); | |
3197 | while ((sig = va_arg(ap, int)) > 0) | |
3198 | assert_se(sigaddset(ss, sig) == 0); | |
3199 | va_end(ap); | |
3200 | } | |
3201 | ||
ef2f1067 LP |
3202 | char* gethostname_malloc(void) { |
3203 | struct utsname u; | |
3204 | ||
3205 | assert_se(uname(&u) >= 0); | |
3206 | ||
3207 | if (u.nodename[0]) | |
3208 | return strdup(u.nodename); | |
3209 | ||
3210 | return strdup(u.sysname); | |
3211 | } | |
3212 | ||
3213 | char* getlogname_malloc(void) { | |
3214 | uid_t uid; | |
3215 | long bufsize; | |
3216 | char *buf, *name; | |
3217 | struct passwd pwbuf, *pw = NULL; | |
3218 | struct stat st; | |
3219 | ||
3220 | if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0) | |
3221 | uid = st.st_uid; | |
3222 | else | |
3223 | uid = getuid(); | |
3224 | ||
3225 | /* Shortcut things to avoid NSS lookups */ | |
3226 | if (uid == 0) | |
3227 | return strdup("root"); | |
3228 | ||
3229 | if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0) | |
3230 | bufsize = 4096; | |
3231 | ||
3232 | if (!(buf = malloc(bufsize))) | |
3233 | return NULL; | |
3234 | ||
3235 | if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) { | |
3236 | name = strdup(pw->pw_name); | |
3237 | free(buf); | |
3238 | return name; | |
3239 | } | |
3240 | ||
3241 | free(buf); | |
3242 | ||
3243 | if (asprintf(&name, "%lu", (unsigned long) uid) < 0) | |
3244 | return NULL; | |
3245 | ||
3246 | return name; | |
3247 | } | |
3248 | ||
fc116c6a LP |
3249 | int getttyname_malloc(int fd, char **r) { |
3250 | char path[PATH_MAX], *c; | |
618e02c7 | 3251 | int k; |
8c6db833 LP |
3252 | |
3253 | assert(r); | |
ef2f1067 | 3254 | |
fc116c6a | 3255 | if ((k = ttyname_r(fd, path, sizeof(path))) != 0) |
618e02c7 | 3256 | return -k; |
ef2f1067 LP |
3257 | |
3258 | char_array_0(path); | |
3259 | ||
fc116c6a | 3260 | if (!(c = strdup(startswith(path, "/dev/") ? path + 5 : path))) |
8c6db833 LP |
3261 | return -ENOMEM; |
3262 | ||
3263 | *r = c; | |
3264 | return 0; | |
3265 | } | |
3266 | ||
fc116c6a LP |
3267 | int getttyname_harder(int fd, char **r) { |
3268 | int k; | |
3269 | char *s; | |
3270 | ||
3271 | if ((k = getttyname_malloc(fd, &s)) < 0) | |
3272 | return k; | |
3273 | ||
3274 | if (streq(s, "tty")) { | |
3275 | free(s); | |
4d6d6518 | 3276 | return get_ctty(0, NULL, r); |
fc116c6a LP |
3277 | } |
3278 | ||
3279 | *r = s; | |
3280 | return 0; | |
3281 | } | |
3282 | ||
4d6d6518 | 3283 | int get_ctty_devnr(pid_t pid, dev_t *d) { |
fc116c6a | 3284 | int k; |
4d6d6518 | 3285 | char line[LINE_MAX], *p, *fn; |
fc116c6a LP |
3286 | unsigned long ttynr; |
3287 | FILE *f; | |
3288 | ||
4d6d6518 LP |
3289 | if (asprintf(&fn, "/proc/%lu/stat", (unsigned long) (pid <= 0 ? getpid() : pid)) < 0) |
3290 | return -ENOMEM; | |
3291 | ||
3292 | f = fopen(fn, "re"); | |
3293 | free(fn); | |
3294 | if (!f) | |
fc116c6a LP |
3295 | return -errno; |
3296 | ||
4d6d6518 | 3297 | if (!fgets(line, sizeof(line), f)) { |
fc116c6a LP |
3298 | k = -errno; |
3299 | fclose(f); | |
3300 | return k; | |
3301 | } | |
3302 | ||
3303 | fclose(f); | |
3304 | ||
4d6d6518 LP |
3305 | p = strrchr(line, ')'); |
3306 | if (!p) | |
fc116c6a LP |
3307 | return -EIO; |
3308 | ||
3309 | p++; | |
3310 | ||
3311 | if (sscanf(p, " " | |
3312 | "%*c " /* state */ | |
3313 | "%*d " /* ppid */ | |
3314 | "%*d " /* pgrp */ | |
3315 | "%*d " /* session */ | |
3316 | "%lu ", /* ttynr */ | |
3317 | &ttynr) != 1) | |
3318 | return -EIO; | |
3319 | ||
3320 | *d = (dev_t) ttynr; | |
3321 | return 0; | |
3322 | } | |
3323 | ||
4d6d6518 | 3324 | int get_ctty(pid_t pid, dev_t *_devnr, char **r) { |
fc116c6a | 3325 | int k; |
20c03b7b | 3326 | char fn[PATH_MAX], *s, *b, *p; |
fc116c6a LP |
3327 | dev_t devnr; |
3328 | ||
3329 | assert(r); | |
3330 | ||
4d6d6518 LP |
3331 | k = get_ctty_devnr(pid, &devnr); |
3332 | if (k < 0) | |
fc116c6a LP |
3333 | return k; |
3334 | ||
3335 | snprintf(fn, sizeof(fn), "/dev/char/%u:%u", major(devnr), minor(devnr)); | |
3336 | char_array_0(fn); | |
3337 | ||
3338 | if ((k = readlink_malloc(fn, &s)) < 0) { | |
3339 | ||
3340 | if (k != -ENOENT) | |
3341 | return k; | |
3342 | ||
46824d0e LP |
3343 | /* This is an ugly hack */ |
3344 | if (major(devnr) == 136) { | |
3345 | if (asprintf(&b, "pts/%lu", (unsigned long) minor(devnr)) < 0) | |
3346 | return -ENOMEM; | |
3347 | ||
3348 | *r = b; | |
3349 | if (_devnr) | |
3350 | *_devnr = devnr; | |
3351 | ||
3352 | return 0; | |
3353 | } | |
3354 | ||
fc116c6a LP |
3355 | /* Probably something like the ptys which have no |
3356 | * symlink in /dev/char. Let's return something | |
3357 | * vaguely useful. */ | |
3358 | ||
3359 | if (!(b = strdup(fn + 5))) | |
3360 | return -ENOMEM; | |
3361 | ||
3362 | *r = b; | |
46824d0e LP |
3363 | if (_devnr) |
3364 | *_devnr = devnr; | |
3365 | ||
fc116c6a LP |
3366 | return 0; |
3367 | } | |
3368 | ||
3369 | if (startswith(s, "/dev/")) | |
3370 | p = s + 5; | |
3371 | else if (startswith(s, "../")) | |
3372 | p = s + 3; | |
3373 | else | |
3374 | p = s; | |
3375 | ||
3376 | b = strdup(p); | |
3377 | free(s); | |
3378 | ||
3379 | if (!b) | |
3380 | return -ENOMEM; | |
3381 | ||
3382 | *r = b; | |
46824d0e LP |
3383 | if (_devnr) |
3384 | *_devnr = devnr; | |
3385 | ||
fc116c6a LP |
3386 | return 0; |
3387 | } | |
3388 | ||
ad293f5a | 3389 | static int rm_rf_children(int fd, bool only_dirs, bool honour_sticky) { |
8c6db833 LP |
3390 | DIR *d; |
3391 | int ret = 0; | |
3392 | ||
3393 | assert(fd >= 0); | |
3394 | ||
3395 | /* This returns the first error we run into, but nevertheless | |
3396 | * tries to go on */ | |
3397 | ||
3398 | if (!(d = fdopendir(fd))) { | |
3399 | close_nointr_nofail(fd); | |
4c633005 LP |
3400 | |
3401 | return errno == ENOENT ? 0 : -errno; | |
8c6db833 LP |
3402 | } |
3403 | ||
3404 | for (;;) { | |
3405 | struct dirent buf, *de; | |
ad293f5a | 3406 | bool is_dir, keep_around = false; |
8c6db833 LP |
3407 | int r; |
3408 | ||
3409 | if ((r = readdir_r(d, &buf, &de)) != 0) { | |
3410 | if (ret == 0) | |
3411 | ret = -r; | |
3412 | break; | |
3413 | } | |
3414 | ||
3415 | if (!de) | |
3416 | break; | |
3417 | ||
3418 | if (streq(de->d_name, ".") || streq(de->d_name, "..")) | |
3419 | continue; | |
3420 | ||
3421 | if (de->d_type == DT_UNKNOWN) { | |
3422 | struct stat st; | |
3423 | ||
3424 | if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { | |
4c633005 | 3425 | if (ret == 0 && errno != ENOENT) |
8c6db833 LP |
3426 | ret = -errno; |
3427 | continue; | |
3428 | } | |
3429 | ||
ad293f5a LP |
3430 | if (honour_sticky) |
3431 | keep_around = st.st_uid == 0 && (st.st_mode & S_ISVTX); | |
3432 | ||
8c6db833 | 3433 | is_dir = S_ISDIR(st.st_mode); |
ad293f5a LP |
3434 | |
3435 | } else { | |
3436 | if (honour_sticky) { | |
3437 | struct stat st; | |
3438 | ||
3439 | if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { | |
3440 | if (ret == 0 && errno != ENOENT) | |
3441 | ret = -errno; | |
3442 | continue; | |
3443 | } | |
3444 | ||
3445 | keep_around = st.st_uid == 0 && (st.st_mode & S_ISVTX); | |
3446 | } | |
3447 | ||
8c6db833 | 3448 | is_dir = de->d_type == DT_DIR; |
ad293f5a | 3449 | } |
8c6db833 LP |
3450 | |
3451 | if (is_dir) { | |
3452 | int subdir_fd; | |
3453 | ||
3454 | if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) { | |
4c633005 | 3455 | if (ret == 0 && errno != ENOENT) |
8c6db833 LP |
3456 | ret = -errno; |
3457 | continue; | |
3458 | } | |
3459 | ||
ad293f5a | 3460 | if ((r = rm_rf_children(subdir_fd, only_dirs, honour_sticky)) < 0) { |
8c6db833 LP |
3461 | if (ret == 0) |
3462 | ret = r; | |
3463 | } | |
3464 | ||
ad293f5a LP |
3465 | if (!keep_around) |
3466 | if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) { | |
3467 | if (ret == 0 && errno != ENOENT) | |
3468 | ret = -errno; | |
3469 | } | |
3470 | ||
3471 | } else if (!only_dirs && !keep_around) { | |
8c6db833 LP |
3472 | |
3473 | if (unlinkat(fd, de->d_name, 0) < 0) { | |
4c633005 | 3474 | if (ret == 0 && errno != ENOENT) |
8c6db833 LP |
3475 | ret = -errno; |
3476 | } | |
3477 | } | |
3478 | } | |
3479 | ||
3480 | closedir(d); | |
3481 | ||
3482 | return ret; | |
3483 | } | |
3484 | ||
ad293f5a | 3485 | int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky) { |
8c6db833 LP |
3486 | int fd; |
3487 | int r; | |
3488 | ||
3489 | assert(path); | |
3490 | ||
3491 | if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) { | |
3492 | ||
3493 | if (errno != ENOTDIR) | |
3494 | return -errno; | |
3495 | ||
3496 | if (delete_root && !only_dirs) | |
3497 | if (unlink(path) < 0) | |
3498 | return -errno; | |
3499 | ||
3500 | return 0; | |
3501 | } | |
3502 | ||
ad293f5a LP |
3503 | r = rm_rf_children(fd, only_dirs, honour_sticky); |
3504 | ||
3505 | if (delete_root) { | |
3506 | ||
3507 | if (honour_sticky && file_is_sticky(path) > 0) | |
3508 | return r; | |
8c6db833 | 3509 | |
e27796a0 | 3510 | if (rmdir(path) < 0 && errno != ENOENT) { |
8c6db833 LP |
3511 | if (r == 0) |
3512 | r = -errno; | |
3513 | } | |
ad293f5a | 3514 | } |
8c6db833 LP |
3515 | |
3516 | return r; | |
3517 | } | |
3518 | ||
3519 | int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { | |
3520 | assert(path); | |
3521 | ||
3522 | /* Under the assumption that we are running privileged we | |
3523 | * first change the access mode and only then hand out | |
3524 | * ownership to avoid a window where access is too open. */ | |
3525 | ||
3526 | if (chmod(path, mode) < 0) | |
3527 | return -errno; | |
3528 | ||
3529 | if (chown(path, uid, gid) < 0) | |
3530 | return -errno; | |
3531 | ||
3532 | return 0; | |
ef2f1067 LP |
3533 | } |
3534 | ||
f4b47811 LP |
3535 | int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid) { |
3536 | assert(fd >= 0); | |
3537 | ||
3538 | /* Under the assumption that we are running privileged we | |
3539 | * first change the access mode and only then hand out | |
3540 | * ownership to avoid a window where access is too open. */ | |
3541 | ||
3542 | if (fchmod(fd, mode) < 0) | |
3543 | return -errno; | |
3544 | ||
3545 | if (fchown(fd, uid, gid) < 0) | |
3546 | return -errno; | |
3547 | ||
3548 | return 0; | |
3549 | } | |
3550 | ||
82c121a4 LP |
3551 | cpu_set_t* cpu_set_malloc(unsigned *ncpus) { |
3552 | cpu_set_t *r; | |
3553 | unsigned n = 1024; | |
3554 | ||
3555 | /* Allocates the cpuset in the right size */ | |
3556 | ||
3557 | for (;;) { | |
3558 | if (!(r = CPU_ALLOC(n))) | |
3559 | return NULL; | |
3560 | ||
3561 | if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) { | |
3562 | CPU_ZERO_S(CPU_ALLOC_SIZE(n), r); | |
3563 | ||
3564 | if (ncpus) | |
3565 | *ncpus = n; | |
3566 | ||
3567 | return r; | |
3568 | } | |
3569 | ||
3570 | CPU_FREE(r); | |
3571 | ||
3572 | if (errno != EINVAL) | |
3573 | return NULL; | |
3574 | ||
3575 | n *= 2; | |
3576 | } | |
3577 | } | |
3578 | ||
9e58ff9c LP |
3579 | void status_vprintf(const char *format, va_list ap) { |
3580 | char *s = NULL; | |
3581 | int fd = -1; | |
3582 | ||
3583 | assert(format); | |
3584 | ||
3585 | /* This independent of logging, as status messages are | |
3586 | * optional and go exclusively to the console. */ | |
3587 | ||
3588 | if (vasprintf(&s, format, ap) < 0) | |
3589 | goto finish; | |
3590 | ||
3591 | if ((fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) | |
3592 | goto finish; | |
3593 | ||
3594 | write(fd, s, strlen(s)); | |
3595 | ||
3596 | finish: | |
3597 | free(s); | |
3598 | ||
3599 | if (fd >= 0) | |
3600 | close_nointr_nofail(fd); | |
3601 | } | |
3602 | ||
c846ff47 LP |
3603 | void status_printf(const char *format, ...) { |
3604 | va_list ap; | |
3605 | ||
3606 | assert(format); | |
3607 | ||
3608 | va_start(ap, format); | |
3609 | status_vprintf(format, ap); | |
3610 | va_end(ap); | |
3611 | } | |
3612 | ||
3613 | void status_welcome(void) { | |
10aa7034 LP |
3614 | char *pretty_name = NULL, *ansi_color = NULL; |
3615 | const char *const_pretty = NULL, *const_color = NULL; | |
3616 | int r; | |
c846ff47 | 3617 | |
10aa7034 LP |
3618 | if ((r = parse_env_file("/etc/os-release", NEWLINE, |
3619 | "PRETTY_NAME", &pretty_name, | |
3620 | "ANSI_COLOR", &ansi_color, | |
3621 | NULL)) < 0) { | |
c846ff47 | 3622 | |
10aa7034 LP |
3623 | if (r != -ENOENT) |
3624 | log_warning("Failed to read /etc/os-release: %s", strerror(-r)); | |
3625 | } | |
c846ff47 | 3626 | |
10aa7034 LP |
3627 | #if defined(TARGET_FEDORA) |
3628 | if (!pretty_name) { | |
3629 | if ((r = read_one_line_file("/etc/system-release", &pretty_name)) < 0) { | |
c846ff47 | 3630 | |
10aa7034 LP |
3631 | if (r != -ENOENT) |
3632 | log_warning("Failed to read /etc/system-release: %s", strerror(-r)); | |
6f9a471a | 3633 | } |
10aa7034 | 3634 | } |
c846ff47 | 3635 | |
10aa7034 | 3636 | if (!ansi_color && pretty_name) { |
c846ff47 | 3637 | |
10aa7034 LP |
3638 | /* This tries to mimic the color magic the old Red Hat sysinit |
3639 | * script did. */ | |
3640 | ||
3641 | if (startswith(pretty_name, "Red Hat")) | |
3642 | const_color = "0;31"; /* Red for RHEL */ | |
3643 | else if (startswith(pretty_name, "Fedora")) | |
3644 | const_color = "0;34"; /* Blue for Fedora */ | |
3645 | } | |
c846ff47 LP |
3646 | |
3647 | #elif defined(TARGET_SUSE) | |
c846ff47 | 3648 | |
10aa7034 LP |
3649 | if (!pretty_name) { |
3650 | if ((r = read_one_line_file("/etc/SuSE-release", &pretty_name)) < 0) { | |
c846ff47 | 3651 | |
10aa7034 LP |
3652 | if (r != -ENOENT) |
3653 | log_warning("Failed to read /etc/SuSE-release: %s", strerror(-r)); | |
6f9a471a | 3654 | } |
10aa7034 | 3655 | } |
c846ff47 | 3656 | |
10aa7034 LP |
3657 | if (!ansi_color) |
3658 | const_color = "0;32"; /* Green for openSUSE */ | |
5a6225fd | 3659 | |
0d37b36b | 3660 | #elif defined(TARGET_GENTOO) |
0d37b36b | 3661 | |
10aa7034 LP |
3662 | if (!pretty_name) { |
3663 | if ((r = read_one_line_file("/etc/gentoo-release", &pretty_name)) < 0) { | |
0d37b36b | 3664 | |
10aa7034 LP |
3665 | if (r != -ENOENT) |
3666 | log_warning("Failed to read /etc/gentoo-release: %s", strerror(-r)); | |
6f9a471a | 3667 | } |
10aa7034 | 3668 | } |
0d37b36b | 3669 | |
10aa7034 LP |
3670 | if (!ansi_color) |
3671 | const_color = "1;34"; /* Light Blue for Gentoo */ | |
5a6225fd | 3672 | |
a338bab5 AS |
3673 | #elif defined(TARGET_ALTLINUX) |
3674 | ||
3675 | if (!pretty_name) { | |
3676 | if ((r = read_one_line_file("/etc/altlinux-release", &pretty_name)) < 0) { | |
3677 | ||
3678 | if (r != -ENOENT) | |
3679 | log_warning("Failed to read /etc/altlinux-release: %s", strerror(-r)); | |
6f9a471a | 3680 | } |
a338bab5 AS |
3681 | } |
3682 | ||
3683 | if (!ansi_color) | |
3684 | const_color = "0;36"; /* Cyan for ALTLinux */ | |
3685 | ||
3686 | ||
5a6225fd | 3687 | #elif defined(TARGET_DEBIAN) |
5a6225fd | 3688 | |
10aa7034 | 3689 | if (!pretty_name) { |
22927a36 | 3690 | char *version; |
c8bffa43 | 3691 | |
22927a36 | 3692 | if ((r = read_one_line_file("/etc/debian_version", &version)) < 0) { |
5a6225fd | 3693 | |
10aa7034 LP |
3694 | if (r != -ENOENT) |
3695 | log_warning("Failed to read /etc/debian_version: %s", strerror(-r)); | |
22927a36 | 3696 | } else { |
22927a36 MB |
3697 | pretty_name = strappend("Debian ", version); |
3698 | free(version); | |
c8bffa43 LP |
3699 | |
3700 | if (!pretty_name) | |
3701 | log_warning("Failed to allocate Debian version string."); | |
22927a36 | 3702 | } |
10aa7034 | 3703 | } |
5a6225fd | 3704 | |
10aa7034 LP |
3705 | if (!ansi_color) |
3706 | const_color = "1;31"; /* Light Red for Debian */ | |
5a6225fd | 3707 | |
274914f9 | 3708 | #elif defined(TARGET_UBUNTU) |
10aa7034 LP |
3709 | |
3710 | if ((r = parse_env_file("/etc/lsb-release", NEWLINE, | |
3711 | "DISTRIB_DESCRIPTION", &pretty_name, | |
3712 | NULL)) < 0) { | |
3713 | ||
3714 | if (r != -ENOENT) | |
3715 | log_warning("Failed to read /etc/lsb-release: %s", strerror(-r)); | |
3716 | } | |
3717 | ||
3718 | if (!ansi_color) | |
3719 | const_color = "0;33"; /* Orange/Brown for Ubuntu */ | |
3720 | ||
1de4d79b AB |
3721 | #elif defined(TARGET_MANDRIVA) |
3722 | ||
3723 | if (!pretty_name) { | |
3724 | char *s, *p; | |
3725 | ||
3726 | if ((r = read_one_line_file("/etc/mandriva-release", &s) < 0)) { | |
3727 | if (r != -ENOENT) | |
3728 | log_warning("Failed to read /etc/mandriva-release: %s", strerror(-r)); | |
3729 | } else { | |
3730 | p = strstr(s, " release "); | |
3731 | if (p) { | |
3732 | *p = '\0'; | |
3733 | p += 9; | |
3734 | p[strcspn(p, " ")] = '\0'; | |
3735 | ||
3736 | /* This corresponds to standard rc.sysinit */ | |
3737 | if (asprintf(&pretty_name, "%s\x1B[0;39m %s", s, p) > 0) | |
3738 | const_color = "1;36"; | |
3739 | else | |
3740 | log_warning("Failed to allocate Mandriva version string."); | |
3741 | } else | |
3742 | log_warning("Failed to parse /etc/mandriva-release"); | |
3743 | free(s); | |
3744 | } | |
3745 | } | |
54e4fdef | 3746 | #elif defined(TARGET_MEEGO) |
1de4d79b | 3747 | |
54e4fdef CF |
3748 | if (!pretty_name) { |
3749 | if ((r = read_one_line_file("/etc/meego-release", &pretty_name)) < 0) { | |
3750 | ||
3751 | if (r != -ENOENT) | |
3752 | log_warning("Failed to read /etc/meego-release: %s", strerror(-r)); | |
3753 | } | |
3754 | } | |
3755 | ||
3756 | if (!ansi_color) | |
3757 | const_color = "1;35"; /* Bright Magenta for MeeGo */ | |
c846ff47 | 3758 | #endif |
10aa7034 LP |
3759 | |
3760 | if (!pretty_name && !const_pretty) | |
3761 | const_pretty = "Linux"; | |
3762 | ||
3763 | if (!ansi_color && !const_color) | |
3764 | const_color = "1"; | |
3765 | ||
da71f23c | 3766 | status_printf("\nWelcome to \x1B[%sm%s\x1B[0m!\n\n", |
10aa7034 LP |
3767 | const_color ? const_color : ansi_color, |
3768 | const_pretty ? const_pretty : pretty_name); | |
86a3475b LP |
3769 | |
3770 | free(ansi_color); | |
3771 | free(pretty_name); | |
c846ff47 LP |
3772 | } |
3773 | ||
fab56fc5 LP |
3774 | char *replace_env(const char *format, char **env) { |
3775 | enum { | |
3776 | WORD, | |
c24eb49e | 3777 | CURLY, |
fab56fc5 LP |
3778 | VARIABLE |
3779 | } state = WORD; | |
3780 | ||
3781 | const char *e, *word = format; | |
3782 | char *r = NULL, *k; | |
3783 | ||
3784 | assert(format); | |
3785 | ||
3786 | for (e = format; *e; e ++) { | |
3787 | ||
3788 | switch (state) { | |
3789 | ||
3790 | case WORD: | |
3791 | if (*e == '$') | |
c24eb49e | 3792 | state = CURLY; |
fab56fc5 LP |
3793 | break; |
3794 | ||
c24eb49e LP |
3795 | case CURLY: |
3796 | if (*e == '{') { | |
fab56fc5 LP |
3797 | if (!(k = strnappend(r, word, e-word-1))) |
3798 | goto fail; | |
3799 | ||
3800 | free(r); | |
3801 | r = k; | |
3802 | ||
3803 | word = e-1; | |
3804 | state = VARIABLE; | |
3805 | ||
3806 | } else if (*e == '$') { | |
3807 | if (!(k = strnappend(r, word, e-word))) | |
3808 | goto fail; | |
3809 | ||
3810 | free(r); | |
3811 | r = k; | |
3812 | ||
3813 | word = e+1; | |
3814 | state = WORD; | |
3815 | } else | |
3816 | state = WORD; | |
3817 | break; | |
3818 | ||
3819 | case VARIABLE: | |
c24eb49e | 3820 | if (*e == '}') { |
b95cf362 | 3821 | const char *t; |
fab56fc5 | 3822 | |
b95cf362 LP |
3823 | if (!(t = strv_env_get_with_length(env, word+2, e-word-2))) |
3824 | t = ""; | |
fab56fc5 | 3825 | |
b95cf362 LP |
3826 | if (!(k = strappend(r, t))) |
3827 | goto fail; | |
fab56fc5 | 3828 | |
b95cf362 LP |
3829 | free(r); |
3830 | r = k; | |
fab56fc5 | 3831 | |
b95cf362 | 3832 | word = e+1; |
fab56fc5 LP |
3833 | state = WORD; |
3834 | } | |
3835 | break; | |
3836 | } | |
3837 | } | |
3838 | ||
3839 | if (!(k = strnappend(r, word, e-word))) | |
3840 | goto fail; | |
3841 | ||
3842 | free(r); | |
3843 | return k; | |
3844 | ||
3845 | fail: | |
3846 | free(r); | |
3847 | return NULL; | |
3848 | } | |
3849 | ||
3850 | char **replace_env_argv(char **argv, char **env) { | |
3851 | char **r, **i; | |
c24eb49e LP |
3852 | unsigned k = 0, l = 0; |
3853 | ||
3854 | l = strv_length(argv); | |
fab56fc5 | 3855 | |
c24eb49e | 3856 | if (!(r = new(char*, l+1))) |
fab56fc5 LP |
3857 | return NULL; |
3858 | ||
3859 | STRV_FOREACH(i, argv) { | |
c24eb49e LP |
3860 | |
3861 | /* If $FOO appears as single word, replace it by the split up variable */ | |
b95cf362 LP |
3862 | if ((*i)[0] == '$' && (*i)[1] != '{') { |
3863 | char *e; | |
3864 | char **w, **m; | |
3865 | unsigned q; | |
c24eb49e | 3866 | |
b95cf362 | 3867 | if ((e = strv_env_get(env, *i+1))) { |
c24eb49e LP |
3868 | |
3869 | if (!(m = strv_split_quoted(e))) { | |
3870 | r[k] = NULL; | |
3871 | strv_free(r); | |
3872 | return NULL; | |
3873 | } | |
b95cf362 LP |
3874 | } else |
3875 | m = NULL; | |
c24eb49e | 3876 | |
b95cf362 LP |
3877 | q = strv_length(m); |
3878 | l = l + q - 1; | |
c24eb49e | 3879 | |
b95cf362 LP |
3880 | if (!(w = realloc(r, sizeof(char*) * (l+1)))) { |
3881 | r[k] = NULL; | |
3882 | strv_free(r); | |
3883 | strv_free(m); | |
3884 | return NULL; | |
3885 | } | |
c24eb49e | 3886 | |
b95cf362 LP |
3887 | r = w; |
3888 | if (m) { | |
c24eb49e LP |
3889 | memcpy(r + k, m, q * sizeof(char*)); |
3890 | free(m); | |
c24eb49e | 3891 | } |
b95cf362 LP |
3892 | |
3893 | k += q; | |
3894 | continue; | |
c24eb49e LP |
3895 | } |
3896 | ||
3897 | /* If ${FOO} appears as part of a word, replace it by the variable as-is */ | |
fab56fc5 LP |
3898 | if (!(r[k++] = replace_env(*i, env))) { |
3899 | strv_free(r); | |
3900 | return NULL; | |
3901 | } | |
3902 | } | |
3903 | ||
3904 | r[k] = NULL; | |
3905 | return r; | |
3906 | } | |
3907 | ||
fa776d8e LP |
3908 | int columns(void) { |
3909 | static __thread int parsed_columns = 0; | |
3910 | const char *e; | |
3911 | ||
3bfc7184 | 3912 | if (_likely_(parsed_columns > 0)) |
fa776d8e LP |
3913 | return parsed_columns; |
3914 | ||
3915 | if ((e = getenv("COLUMNS"))) | |
3916 | parsed_columns = atoi(e); | |
3917 | ||
3918 | if (parsed_columns <= 0) { | |
3919 | struct winsize ws; | |
3920 | zero(ws); | |
3921 | ||
9ed95f43 | 3922 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0) |
fa776d8e LP |
3923 | parsed_columns = ws.ws_col; |
3924 | } | |
3925 | ||
3926 | if (parsed_columns <= 0) | |
3927 | parsed_columns = 80; | |
3928 | ||
3929 | return parsed_columns; | |
3930 | } | |
3931 | ||
b4f10a5e LP |
3932 | int running_in_chroot(void) { |
3933 | struct stat a, b; | |
3934 | ||
3935 | zero(a); | |
3936 | zero(b); | |
3937 | ||
3938 | /* Only works as root */ | |
3939 | ||
3940 | if (stat("/proc/1/root", &a) < 0) | |
3941 | return -errno; | |
3942 | ||
3943 | if (stat("/", &b) < 0) | |
3944 | return -errno; | |
3945 | ||
3946 | return | |
3947 | a.st_dev != b.st_dev || | |
3948 | a.st_ino != b.st_ino; | |
3949 | } | |
3950 | ||
8fe914ec LP |
3951 | char *ellipsize(const char *s, unsigned length, unsigned percent) { |
3952 | size_t l, x; | |
3953 | char *r; | |
3954 | ||
3955 | assert(s); | |
3956 | assert(percent <= 100); | |
3957 | assert(length >= 3); | |
3958 | ||
3959 | l = strlen(s); | |
3960 | ||
3961 | if (l <= 3 || l <= length) | |
3962 | return strdup(s); | |
3963 | ||
3964 | if (!(r = new0(char, length+1))) | |
3965 | return r; | |
3966 | ||
3967 | x = (length * percent) / 100; | |
3968 | ||
3969 | if (x > length - 3) | |
3970 | x = length - 3; | |
3971 | ||
3972 | memcpy(r, s, x); | |
3973 | r[x] = '.'; | |
3974 | r[x+1] = '.'; | |
3975 | r[x+2] = '.'; | |
3976 | memcpy(r + x + 3, | |
3977 | s + l - (length - x - 3), | |
3978 | length - x - 3); | |
3979 | ||
3980 | return r; | |
3981 | } | |
3982 | ||
f6144808 LP |
3983 | int touch(const char *path) { |
3984 | int fd; | |
3985 | ||
3986 | assert(path); | |
3987 | ||
14f3c825 | 3988 | if ((fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644)) < 0) |
f6144808 LP |
3989 | return -errno; |
3990 | ||
3991 | close_nointr_nofail(fd); | |
3992 | return 0; | |
3993 | } | |
afea26ad | 3994 | |
97c4a07d | 3995 | char *unquote(const char *s, const char* quotes) { |
11ce3427 LP |
3996 | size_t l; |
3997 | assert(s); | |
3998 | ||
3999 | if ((l = strlen(s)) < 2) | |
4000 | return strdup(s); | |
4001 | ||
97c4a07d | 4002 | if (strchr(quotes, s[0]) && s[l-1] == s[0]) |
11ce3427 LP |
4003 | return strndup(s+1, l-2); |
4004 | ||
4005 | return strdup(s); | |
4006 | } | |
4007 | ||
5f7c426e LP |
4008 | char *normalize_env_assignment(const char *s) { |
4009 | char *name, *value, *p, *r; | |
4010 | ||
4011 | p = strchr(s, '='); | |
4012 | ||
4013 | if (!p) { | |
4014 | if (!(r = strdup(s))) | |
4015 | return NULL; | |
4016 | ||
4017 | return strstrip(r); | |
4018 | } | |
4019 | ||
4020 | if (!(name = strndup(s, p - s))) | |
4021 | return NULL; | |
4022 | ||
4023 | if (!(p = strdup(p+1))) { | |
4024 | free(name); | |
4025 | return NULL; | |
4026 | } | |
4027 | ||
4028 | value = unquote(strstrip(p), QUOTES); | |
4029 | free(p); | |
4030 | ||
4031 | if (!value) { | |
5f7c426e LP |
4032 | free(name); |
4033 | return NULL; | |
4034 | } | |
4035 | ||
4036 | if (asprintf(&r, "%s=%s", name, value) < 0) | |
4037 | r = NULL; | |
4038 | ||
4039 | free(value); | |
4040 | free(name); | |
4041 | ||
4042 | return r; | |
4043 | } | |
4044 | ||
8e12a6ae | 4045 | int wait_for_terminate(pid_t pid, siginfo_t *status) { |
1968a360 LP |
4046 | siginfo_t dummy; |
4047 | ||
2e78aa99 | 4048 | assert(pid >= 1); |
1968a360 LP |
4049 | |
4050 | if (!status) | |
4051 | status = &dummy; | |
2e78aa99 LP |
4052 | |
4053 | for (;;) { | |
8e12a6ae LP |
4054 | zero(*status); |
4055 | ||
4056 | if (waitid(P_PID, pid, status, WEXITED) < 0) { | |
2e78aa99 LP |
4057 | |
4058 | if (errno == EINTR) | |
4059 | continue; | |
4060 | ||
4061 | return -errno; | |
4062 | } | |
4063 | ||
4064 | return 0; | |
4065 | } | |
4066 | } | |
4067 | ||
97c4a07d LP |
4068 | int wait_for_terminate_and_warn(const char *name, pid_t pid) { |
4069 | int r; | |
4070 | siginfo_t status; | |
4071 | ||
4072 | assert(name); | |
4073 | assert(pid > 1); | |
4074 | ||
4075 | if ((r = wait_for_terminate(pid, &status)) < 0) { | |
4076 | log_warning("Failed to wait for %s: %s", name, strerror(-r)); | |
4077 | return r; | |
4078 | } | |
4079 | ||
4080 | if (status.si_code == CLD_EXITED) { | |
4081 | if (status.si_status != 0) { | |
4082 | log_warning("%s failed with error code %i.", name, status.si_status); | |
0a27cf3f | 4083 | return status.si_status; |
97c4a07d LP |
4084 | } |
4085 | ||
4086 | log_debug("%s succeeded.", name); | |
4087 | return 0; | |
4088 | ||
4089 | } else if (status.si_code == CLD_KILLED || | |
4090 | status.si_code == CLD_DUMPED) { | |
4091 | ||
4092 | log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status)); | |
4093 | return -EPROTO; | |
4094 | } | |
4095 | ||
4096 | log_warning("%s failed due to unknown reason.", name); | |
4097 | return -EPROTO; | |
4098 | ||
4099 | } | |
4100 | ||
3c14d26c | 4101 | void freeze(void) { |
720ce21d LP |
4102 | |
4103 | /* Make sure nobody waits for us on a socket anymore */ | |
4104 | close_all_fds(NULL, 0); | |
4105 | ||
c29597a1 LP |
4106 | sync(); |
4107 | ||
3c14d26c LP |
4108 | for (;;) |
4109 | pause(); | |
4110 | } | |
4111 | ||
00dc5d76 LP |
4112 | bool null_or_empty(struct stat *st) { |
4113 | assert(st); | |
4114 | ||
4115 | if (S_ISREG(st->st_mode) && st->st_size <= 0) | |
4116 | return true; | |
4117 | ||
c8f26f42 | 4118 | if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) |
00dc5d76 LP |
4119 | return true; |
4120 | ||
4121 | return false; | |
4122 | } | |
4123 | ||
83096483 LP |
4124 | int null_or_empty_path(const char *fn) { |
4125 | struct stat st; | |
4126 | ||
4127 | assert(fn); | |
4128 | ||
4129 | if (stat(fn, &st) < 0) | |
4130 | return -errno; | |
4131 | ||
4132 | return null_or_empty(&st); | |
4133 | } | |
4134 | ||
a247755d | 4135 | DIR *xopendirat(int fd, const char *name, int flags) { |
c4731d11 LP |
4136 | int nfd; |
4137 | DIR *d; | |
4138 | ||
4139 | if ((nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags)) < 0) | |
4140 | return NULL; | |
4141 | ||
4142 | if (!(d = fdopendir(nfd))) { | |
4143 | close_nointr_nofail(nfd); | |
4144 | return NULL; | |
4145 | } | |
4146 | ||
4147 | return d; | |
3b63d2d3 LP |
4148 | } |
4149 | ||
8a0867d6 LP |
4150 | int signal_from_string_try_harder(const char *s) { |
4151 | int signo; | |
4152 | assert(s); | |
4153 | ||
4154 | if ((signo = signal_from_string(s)) <= 0) | |
4155 | if (startswith(s, "SIG")) | |
4156 | return signal_from_string(s+3); | |
4157 | ||
4158 | return signo; | |
4159 | } | |
4160 | ||
10717a1a LP |
4161 | void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) { |
4162 | ||
4163 | assert(f); | |
4164 | assert(name); | |
4165 | assert(t); | |
4166 | ||
4167 | if (!dual_timestamp_is_set(t)) | |
4168 | return; | |
4169 | ||
4170 | fprintf(f, "%s=%llu %llu\n", | |
4171 | name, | |
4172 | (unsigned long long) t->realtime, | |
4173 | (unsigned long long) t->monotonic); | |
4174 | } | |
4175 | ||
799fd0fd | 4176 | void dual_timestamp_deserialize(const char *value, dual_timestamp *t) { |
10717a1a LP |
4177 | unsigned long long a, b; |
4178 | ||
10717a1a LP |
4179 | assert(value); |
4180 | assert(t); | |
4181 | ||
4182 | if (sscanf(value, "%lli %llu", &a, &b) != 2) | |
4183 | log_debug("Failed to parse finish timestamp value %s", value); | |
4184 | else { | |
4185 | t->realtime = a; | |
4186 | t->monotonic = b; | |
4187 | } | |
4188 | } | |
4189 | ||
e23a0ce8 LP |
4190 | char *fstab_node_to_udev_node(const char *p) { |
4191 | char *dn, *t, *u; | |
4192 | int r; | |
4193 | ||
4194 | /* FIXME: to follow udev's logic 100% we need to leave valid | |
4195 | * UTF8 chars unescaped */ | |
4196 | ||
4197 | if (startswith(p, "LABEL=")) { | |
4198 | ||
4199 | if (!(u = unquote(p+6, "\"\'"))) | |
4200 | return NULL; | |
4201 | ||
4202 | t = xescape(u, "/ "); | |
4203 | free(u); | |
4204 | ||
4205 | if (!t) | |
4206 | return NULL; | |
4207 | ||
4208 | r = asprintf(&dn, "/dev/disk/by-label/%s", t); | |
4209 | free(t); | |
4210 | ||
4211 | if (r < 0) | |
4212 | return NULL; | |
4213 | ||
4214 | return dn; | |
4215 | } | |
4216 | ||
4217 | if (startswith(p, "UUID=")) { | |
4218 | ||
4219 | if (!(u = unquote(p+5, "\"\'"))) | |
4220 | return NULL; | |
4221 | ||
4222 | t = xescape(u, "/ "); | |
4223 | free(u); | |
4224 | ||
4225 | if (!t) | |
4226 | return NULL; | |
4227 | ||
0058d7b9 | 4228 | r = asprintf(&dn, "/dev/disk/by-uuid/%s", t); |
e23a0ce8 LP |
4229 | free(t); |
4230 | ||
4231 | if (r < 0) | |
4232 | return NULL; | |
4233 | ||
4234 | return dn; | |
4235 | } | |
4236 | ||
4237 | return strdup(p); | |
4238 | } | |
4239 | ||
e9ddabc2 LP |
4240 | void filter_environ(const char *prefix) { |
4241 | int i, j; | |
4242 | assert(prefix); | |
4243 | ||
4244 | if (!environ) | |
4245 | return; | |
4246 | ||
4247 | for (i = 0, j = 0; environ[i]; i++) { | |
4248 | ||
4249 | if (startswith(environ[i], prefix)) | |
4250 | continue; | |
4251 | ||
4252 | environ[j++] = environ[i]; | |
4253 | } | |
4254 | ||
4255 | environ[j] = NULL; | |
4256 | } | |
4257 | ||
f212ac12 LP |
4258 | bool tty_is_vc(const char *tty) { |
4259 | assert(tty); | |
4260 | ||
4261 | if (startswith(tty, "/dev/")) | |
4262 | tty += 5; | |
4263 | ||
98a28fef LP |
4264 | return vtnr_from_tty(tty) >= 0; |
4265 | } | |
4266 | ||
4267 | int vtnr_from_tty(const char *tty) { | |
4268 | int i, r; | |
4269 | ||
4270 | assert(tty); | |
4271 | ||
4272 | if (startswith(tty, "/dev/")) | |
4273 | tty += 5; | |
4274 | ||
4275 | if (!startswith(tty, "tty") ) | |
4276 | return -EINVAL; | |
4277 | ||
4278 | if (tty[3] < '0' || tty[3] > '9') | |
4279 | return -EINVAL; | |
4280 | ||
4281 | r = safe_atoi(tty+3, &i); | |
4282 | if (r < 0) | |
4283 | return r; | |
4284 | ||
4285 | if (i < 0 || i > 63) | |
4286 | return -EINVAL; | |
4287 | ||
4288 | return i; | |
f212ac12 LP |
4289 | } |
4290 | ||
e3aa71c3 | 4291 | const char *default_term_for_tty(const char *tty) { |
3030ccd7 LP |
4292 | char *active = NULL; |
4293 | const char *term; | |
4294 | ||
e3aa71c3 LP |
4295 | assert(tty); |
4296 | ||
4297 | if (startswith(tty, "/dev/")) | |
4298 | tty += 5; | |
4299 | ||
3030ccd7 LP |
4300 | /* Resolve where /dev/console is pointing when determining |
4301 | * TERM */ | |
4302 | if (streq(tty, "console")) | |
4303 | if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) { | |
079a09fb LP |
4304 | /* If multiple log outputs are configured the |
4305 | * last one is what /dev/console points to */ | |
4306 | if ((tty = strrchr(active, ' '))) | |
4307 | tty++; | |
4308 | else | |
4309 | tty = active; | |
3030ccd7 LP |
4310 | } |
4311 | ||
f212ac12 | 4312 | term = tty_is_vc(tty) ? "TERM=linux" : "TERM=vt100"; |
3030ccd7 | 4313 | free(active); |
e3aa71c3 | 4314 | |
3030ccd7 | 4315 | return term; |
e3aa71c3 LP |
4316 | } |
4317 | ||
87d2c1ff | 4318 | bool dirent_is_file(const struct dirent *de) { |
fb19a739 LP |
4319 | assert(de); |
4320 | ||
4321 | if (ignore_file(de->d_name)) | |
4322 | return false; | |
4323 | ||
4324 | if (de->d_type != DT_REG && | |
4325 | de->d_type != DT_LNK && | |
4326 | de->d_type != DT_UNKNOWN) | |
4327 | return false; | |
4328 | ||
4329 | return true; | |
4330 | } | |
4331 | ||
87d2c1ff LP |
4332 | bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) { |
4333 | assert(de); | |
4334 | ||
4335 | if (!dirent_is_file(de)) | |
4336 | return false; | |
4337 | ||
4338 | return endswith(de->d_name, suffix); | |
4339 | } | |
4340 | ||
83cc030f LP |
4341 | void execute_directory(const char *directory, DIR *d, char *argv[]) { |
4342 | DIR *_d = NULL; | |
4343 | struct dirent *de; | |
4344 | Hashmap *pids = NULL; | |
4345 | ||
4346 | assert(directory); | |
4347 | ||
4348 | /* Executes all binaries in a directory in parallel and waits | |
4349 | * until all they all finished. */ | |
4350 | ||
4351 | if (!d) { | |
4352 | if (!(_d = opendir(directory))) { | |
4353 | ||
4354 | if (errno == ENOENT) | |
4355 | return; | |
4356 | ||
4357 | log_error("Failed to enumerate directory %s: %m", directory); | |
4358 | return; | |
4359 | } | |
4360 | ||
4361 | d = _d; | |
4362 | } | |
4363 | ||
4364 | if (!(pids = hashmap_new(trivial_hash_func, trivial_compare_func))) { | |
4365 | log_error("Failed to allocate set."); | |
4366 | goto finish; | |
4367 | } | |
4368 | ||
4369 | while ((de = readdir(d))) { | |
4370 | char *path; | |
4371 | pid_t pid; | |
4372 | int k; | |
4373 | ||
fb19a739 | 4374 | if (!dirent_is_file(de)) |
83cc030f LP |
4375 | continue; |
4376 | ||
4377 | if (asprintf(&path, "%s/%s", directory, de->d_name) < 0) { | |
4378 | log_error("Out of memory"); | |
4379 | continue; | |
4380 | } | |
4381 | ||
4382 | if ((pid = fork()) < 0) { | |
4383 | log_error("Failed to fork: %m"); | |
4384 | free(path); | |
4385 | continue; | |
4386 | } | |
4387 | ||
4388 | if (pid == 0) { | |
4389 | char *_argv[2]; | |
4390 | /* Child */ | |
4391 | ||
4392 | if (!argv) { | |
4393 | _argv[0] = path; | |
4394 | _argv[1] = NULL; | |
4395 | argv = _argv; | |
4396 | } else | |
4397 | if (!argv[0]) | |
4398 | argv[0] = path; | |
4399 | ||
4400 | execv(path, argv); | |
4401 | ||
4402 | log_error("Failed to execute %s: %m", path); | |
4403 | _exit(EXIT_FAILURE); | |
4404 | } | |
4405 | ||
4406 | log_debug("Spawned %s as %lu", path, (unsigned long) pid); | |
4407 | ||
4408 | if ((k = hashmap_put(pids, UINT_TO_PTR(pid), path)) < 0) { | |
4409 | log_error("Failed to add PID to set: %s", strerror(-k)); | |
4410 | free(path); | |
4411 | } | |
4412 | } | |
4413 | ||
4414 | while (!hashmap_isempty(pids)) { | |
4415 | siginfo_t si; | |
4416 | char *path; | |
4417 | ||
4418 | zero(si); | |
4419 | if (waitid(P_ALL, 0, &si, WEXITED) < 0) { | |
4420 | ||
4421 | if (errno == EINTR) | |
4422 | continue; | |
4423 | ||
4424 | log_error("waitid() failed: %m"); | |
4425 | goto finish; | |
4426 | } | |
4427 | ||
4428 | if ((path = hashmap_remove(pids, UINT_TO_PTR(si.si_pid)))) { | |
4429 | if (!is_clean_exit(si.si_code, si.si_status)) { | |
4430 | if (si.si_code == CLD_EXITED) | |
4431 | log_error("%s exited with exit status %i.", path, si.si_status); | |
4432 | else | |
4433 | log_error("%s terminated by signal %s.", path, signal_to_string(si.si_status)); | |
4434 | } else | |
4435 | log_debug("%s exited successfully.", path); | |
4436 | ||
4437 | free(path); | |
4438 | } | |
4439 | } | |
4440 | ||
4441 | finish: | |
4442 | if (_d) | |
4443 | closedir(_d); | |
4444 | ||
4445 | if (pids) | |
4446 | hashmap_free_free(pids); | |
4447 | } | |
4448 | ||
430c18ed LP |
4449 | int kill_and_sigcont(pid_t pid, int sig) { |
4450 | int r; | |
4451 | ||
4452 | r = kill(pid, sig) < 0 ? -errno : 0; | |
4453 | ||
4454 | if (r >= 0) | |
4455 | kill(pid, SIGCONT); | |
4456 | ||
4457 | return r; | |
4458 | } | |
4459 | ||
05feefe0 LP |
4460 | bool nulstr_contains(const char*nulstr, const char *needle) { |
4461 | const char *i; | |
4462 | ||
4463 | if (!nulstr) | |
4464 | return false; | |
4465 | ||
4466 | NULSTR_FOREACH(i, nulstr) | |
4467 | if (streq(i, needle)) | |
4468 | return true; | |
4469 | ||
4470 | return false; | |
4471 | } | |
4472 | ||
6faa1114 | 4473 | bool plymouth_running(void) { |
9408a2d2 | 4474 | return access("/run/plymouth/pid", F_OK) >= 0; |
6faa1114 LP |
4475 | } |
4476 | ||
7c3b203c LP |
4477 | void parse_syslog_priority(char **p, int *priority) { |
4478 | int a = 0, b = 0, c = 0; | |
4479 | int k; | |
4480 | ||
4481 | assert(p); | |
4482 | assert(*p); | |
4483 | assert(priority); | |
4484 | ||
4485 | if ((*p)[0] != '<') | |
4486 | return; | |
4487 | ||
4488 | if (!strchr(*p, '>')) | |
4489 | return; | |
4490 | ||
4491 | if ((*p)[2] == '>') { | |
4492 | c = undecchar((*p)[1]); | |
4493 | k = 3; | |
4494 | } else if ((*p)[3] == '>') { | |
4495 | b = undecchar((*p)[1]); | |
4496 | c = undecchar((*p)[2]); | |
4497 | k = 4; | |
4498 | } else if ((*p)[4] == '>') { | |
4499 | a = undecchar((*p)[1]); | |
4500 | b = undecchar((*p)[2]); | |
4501 | c = undecchar((*p)[3]); | |
4502 | k = 5; | |
4503 | } else | |
4504 | return; | |
4505 | ||
4506 | if (a < 0 || b < 0 || c < 0) | |
4507 | return; | |
4508 | ||
4509 | *priority = a*100+b*10+c; | |
4510 | *p += k; | |
4511 | } | |
4512 | ||
87d2c1ff LP |
4513 | void skip_syslog_pid(char **buf) { |
4514 | char *p; | |
4515 | ||
4516 | assert(buf); | |
4517 | assert(*buf); | |
4518 | ||
4519 | p = *buf; | |
4520 | ||
4521 | if (*p != '[') | |
4522 | return; | |
4523 | ||
4524 | p++; | |
4525 | p += strspn(p, "0123456789"); | |
4526 | ||
4527 | if (*p != ']') | |
4528 | return; | |
4529 | ||
4530 | p++; | |
4531 | ||
4532 | *buf = p; | |
4533 | } | |
4534 | ||
4535 | void skip_syslog_date(char **buf) { | |
4536 | enum { | |
4537 | LETTER, | |
4538 | SPACE, | |
4539 | NUMBER, | |
4540 | SPACE_OR_NUMBER, | |
4541 | COLON | |
4542 | } sequence[] = { | |
4543 | LETTER, LETTER, LETTER, | |
4544 | SPACE, | |
4545 | SPACE_OR_NUMBER, NUMBER, | |
4546 | SPACE, | |
4547 | SPACE_OR_NUMBER, NUMBER, | |
4548 | COLON, | |
4549 | SPACE_OR_NUMBER, NUMBER, | |
4550 | COLON, | |
4551 | SPACE_OR_NUMBER, NUMBER, | |
4552 | SPACE | |
4553 | }; | |
4554 | ||
4555 | char *p; | |
4556 | unsigned i; | |
4557 | ||
4558 | assert(buf); | |
4559 | assert(*buf); | |
4560 | ||
4561 | p = *buf; | |
4562 | ||
4563 | for (i = 0; i < ELEMENTSOF(sequence); i++, p++) { | |
4564 | ||
4565 | if (!*p) | |
4566 | return; | |
4567 | ||
4568 | switch (sequence[i]) { | |
4569 | ||
4570 | case SPACE: | |
4571 | if (*p != ' ') | |
4572 | return; | |
4573 | break; | |
4574 | ||
4575 | case SPACE_OR_NUMBER: | |
4576 | if (*p == ' ') | |
4577 | break; | |
4578 | ||
4579 | /* fall through */ | |
4580 | ||
4581 | case NUMBER: | |
4582 | if (*p < '0' || *p > '9') | |
4583 | return; | |
4584 | ||
4585 | break; | |
4586 | ||
4587 | case LETTER: | |
4588 | if (!(*p >= 'A' && *p <= 'Z') && | |
4589 | !(*p >= 'a' && *p <= 'z')) | |
4590 | return; | |
4591 | ||
4592 | break; | |
4593 | ||
4594 | case COLON: | |
4595 | if (*p != ':') | |
4596 | return; | |
4597 | break; | |
4598 | ||
4599 | } | |
4600 | } | |
4601 | ||
4602 | *buf = p; | |
4603 | } | |
4604 | ||
ac123445 LP |
4605 | int have_effective_cap(int value) { |
4606 | cap_t cap; | |
4607 | cap_flag_value_t fv; | |
4608 | int r; | |
4609 | ||
4610 | if (!(cap = cap_get_proc())) | |
4611 | return -errno; | |
4612 | ||
4613 | if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0) | |
4614 | r = -errno; | |
4615 | else | |
4616 | r = fv == CAP_SET; | |
4617 | ||
4618 | cap_free(cap); | |
4619 | return r; | |
4620 | } | |
4621 | ||
9beb3f4d LP |
4622 | char* strshorten(char *s, size_t l) { |
4623 | assert(s); | |
4624 | ||
4625 | if (l < strlen(s)) | |
4626 | s[l] = 0; | |
4627 | ||
4628 | return s; | |
4629 | } | |
4630 | ||
4631 | static bool hostname_valid_char(char c) { | |
4632 | return | |
4633 | (c >= 'a' && c <= 'z') || | |
4634 | (c >= 'A' && c <= 'Z') || | |
4635 | (c >= '0' && c <= '9') || | |
4636 | c == '-' || | |
4637 | c == '_' || | |
4638 | c == '.'; | |
4639 | } | |
4640 | ||
4641 | bool hostname_is_valid(const char *s) { | |
4642 | const char *p; | |
4643 | ||
4644 | if (isempty(s)) | |
4645 | return false; | |
4646 | ||
4647 | for (p = s; *p; p++) | |
4648 | if (!hostname_valid_char(*p)) | |
4649 | return false; | |
4650 | ||
4651 | if (p-s > HOST_NAME_MAX) | |
4652 | return false; | |
4653 | ||
4654 | return true; | |
4655 | } | |
4656 | ||
4657 | char* hostname_cleanup(char *s) { | |
4658 | char *p, *d; | |
4659 | ||
4660 | for (p = s, d = s; *p; p++) | |
4661 | if ((*p >= 'a' && *p <= 'z') || | |
4662 | (*p >= 'A' && *p <= 'Z') || | |
4663 | (*p >= '0' && *p <= '9') || | |
4664 | *p == '-' || | |
4665 | *p == '_' || | |
4666 | *p == '.') | |
4667 | *(d++) = *p; | |
4668 | ||
4669 | *d = 0; | |
4670 | ||
4671 | strshorten(s, HOST_NAME_MAX); | |
4672 | return s; | |
4673 | } | |
4674 | ||
1325aa42 LP |
4675 | int pipe_eof(int fd) { |
4676 | struct pollfd pollfd; | |
4677 | int r; | |
4678 | ||
4679 | zero(pollfd); | |
4680 | pollfd.fd = fd; | |
4681 | pollfd.events = POLLIN|POLLHUP; | |
4682 | ||
4683 | r = poll(&pollfd, 1, 0); | |
4684 | if (r < 0) | |
4685 | return -errno; | |
4686 | ||
4687 | if (r == 0) | |
4688 | return 0; | |
4689 | ||
4690 | return pollfd.revents & POLLHUP; | |
4691 | } | |
4692 | ||
5a3ab509 LP |
4693 | int fopen_temporary(const char *path, FILE **_f, char **_temp_path) { |
4694 | FILE *f; | |
4695 | char *t; | |
4696 | const char *fn; | |
4697 | size_t k; | |
4698 | int fd; | |
4699 | ||
4700 | assert(path); | |
4701 | assert(_f); | |
4702 | assert(_temp_path); | |
4703 | ||
4704 | t = new(char, strlen(path) + 1 + 6 + 1); | |
4705 | if (!t) | |
4706 | return -ENOMEM; | |
4707 | ||
4708 | fn = file_name_from_path(path); | |
4709 | k = fn-path; | |
4710 | memcpy(t, path, k); | |
4711 | t[k] = '.'; | |
4712 | stpcpy(stpcpy(t+k+1, fn), "XXXXXX"); | |
4713 | ||
4714 | fd = mkostemp(t, O_WRONLY|O_CLOEXEC); | |
4715 | if (fd < 0) { | |
4716 | free(t); | |
4717 | return -errno; | |
4718 | } | |
4719 | ||
4720 | f = fdopen(fd, "we"); | |
4721 | if (!f) { | |
4722 | unlink(t); | |
4723 | free(t); | |
4724 | return -errno; | |
4725 | } | |
4726 | ||
4727 | *_f = f; | |
4728 | *_temp_path = t; | |
4729 | ||
4730 | return 0; | |
4731 | } | |
4732 | ||
6ea832a2 | 4733 | int terminal_vhangup_fd(int fd) { |
5a3ab509 LP |
4734 | assert(fd >= 0); |
4735 | ||
6ea832a2 LP |
4736 | if (ioctl(fd, TIOCVHANGUP) < 0) |
4737 | return -errno; | |
4738 | ||
4739 | return 0; | |
4740 | } | |
4741 | ||
4742 | int terminal_vhangup(const char *name) { | |
4743 | int fd, r; | |
4744 | ||
4745 | fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC); | |
4746 | if (fd < 0) | |
4747 | return fd; | |
4748 | ||
4749 | r = terminal_vhangup_fd(fd); | |
4750 | close_nointr_nofail(fd); | |
4751 | ||
4752 | return r; | |
4753 | } | |
4754 | ||
4755 | int vt_disallocate(const char *name) { | |
4756 | int fd, r; | |
4757 | unsigned u; | |
6ea832a2 LP |
4758 | |
4759 | /* Deallocate the VT if possible. If not possible | |
4760 | * (i.e. because it is the active one), at least clear it | |
4761 | * entirely (including the scrollback buffer) */ | |
4762 | ||
b83bc4e9 LP |
4763 | if (!startswith(name, "/dev/")) |
4764 | return -EINVAL; | |
4765 | ||
4766 | if (!tty_is_vc(name)) { | |
4767 | /* So this is not a VT. I guess we cannot deallocate | |
4768 | * it then. But let's at least clear the screen */ | |
4769 | ||
4770 | fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC); | |
4771 | if (fd < 0) | |
4772 | return fd; | |
4773 | ||
8585357a LP |
4774 | loop_write(fd, |
4775 | "\033[r" /* clear scrolling region */ | |
4776 | "\033[H" /* move home */ | |
4777 | "\033[2J", /* clear screen */ | |
4778 | 10, false); | |
b83bc4e9 LP |
4779 | close_nointr_nofail(fd); |
4780 | ||
4781 | return 0; | |
4782 | } | |
6ea832a2 LP |
4783 | |
4784 | if (!startswith(name, "/dev/tty")) | |
4785 | return -EINVAL; | |
4786 | ||
4787 | r = safe_atou(name+8, &u); | |
4788 | if (r < 0) | |
4789 | return r; | |
4790 | ||
4791 | if (u <= 0) | |
b83bc4e9 | 4792 | return -EINVAL; |
6ea832a2 | 4793 | |
b83bc4e9 | 4794 | /* Try to deallocate */ |
6ea832a2 LP |
4795 | fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); |
4796 | if (fd < 0) | |
4797 | return fd; | |
4798 | ||
4799 | r = ioctl(fd, VT_DISALLOCATE, u); | |
b83bc4e9 | 4800 | close_nointr_nofail(fd); |
6ea832a2 | 4801 | |
b83bc4e9 LP |
4802 | if (r >= 0) |
4803 | return 0; | |
6ea832a2 | 4804 | |
b83bc4e9 | 4805 | if (errno != EBUSY) |
6ea832a2 | 4806 | return -errno; |
6ea832a2 | 4807 | |
b83bc4e9 LP |
4808 | /* Couldn't deallocate, so let's clear it fully with |
4809 | * scrollback */ | |
4810 | fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC); | |
6ea832a2 | 4811 | if (fd < 0) |
b83bc4e9 | 4812 | return fd; |
6ea832a2 | 4813 | |
8585357a LP |
4814 | loop_write(fd, |
4815 | "\033[r" /* clear scrolling region */ | |
4816 | "\033[H" /* move home */ | |
4817 | "\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */ | |
4818 | 10, false); | |
b83bc4e9 | 4819 | close_nointr_nofail(fd); |
6ea832a2 | 4820 | |
b83bc4e9 | 4821 | return 0; |
6ea832a2 LP |
4822 | } |
4823 | ||
db1413d7 KS |
4824 | static int files_add(Hashmap *h, const char *path, const char *suffix) { |
4825 | DIR *dir; | |
f782b8d0 | 4826 | struct dirent buffer, *de; |
db1413d7 KS |
4827 | int r = 0; |
4828 | ||
4829 | dir = opendir(path); | |
4830 | if (!dir) { | |
4831 | if (errno == ENOENT) | |
4832 | return 0; | |
4833 | return -errno; | |
4834 | } | |
4835 | ||
f782b8d0 LP |
4836 | for (;;) { |
4837 | int k; | |
223a3558 | 4838 | char *p, *f; |
f782b8d0 LP |
4839 | |
4840 | k = readdir_r(dir, &buffer, &de); | |
4841 | if (k != 0) { | |
4842 | r = -k; | |
4843 | goto finish; | |
4844 | } | |
4845 | ||
4846 | if (!de) | |
4847 | break; | |
db1413d7 | 4848 | |
87d2c1ff | 4849 | if (!dirent_is_file_with_suffix(de, suffix)) |
db1413d7 KS |
4850 | continue; |
4851 | ||
223a3558 | 4852 | if (asprintf(&p, "%s/%s", path, de->d_name) < 0) { |
db1413d7 KS |
4853 | r = -ENOMEM; |
4854 | goto finish; | |
4855 | } | |
4856 | ||
223a3558 KS |
4857 | f = canonicalize_file_name(p); |
4858 | if (!f) { | |
4859 | log_error("Failed to canonicalize file name '%s': %m", p); | |
4860 | free(p); | |
4861 | continue; | |
4862 | } | |
4863 | free(p); | |
4864 | ||
db1413d7 | 4865 | log_debug("found: %s\n", f); |
f782b8d0 | 4866 | if (hashmap_put(h, file_name_from_path(f), f) <= 0) |
db1413d7 KS |
4867 | free(f); |
4868 | } | |
4869 | ||
4870 | finish: | |
4871 | closedir(dir); | |
4872 | return r; | |
4873 | } | |
4874 | ||
4875 | static int base_cmp(const void *a, const void *b) { | |
4876 | const char *s1, *s2; | |
4877 | ||
4878 | s1 = *(char * const *)a; | |
4879 | s2 = *(char * const *)b; | |
4880 | return strcmp(file_name_from_path(s1), file_name_from_path(s2)); | |
4881 | } | |
4882 | ||
44143309 | 4883 | int conf_files_list(char ***strv, const char *suffix, const char *dir, ...) { |
223a3558 KS |
4884 | Hashmap *fh = NULL; |
4885 | char **dirs = NULL; | |
db1413d7 | 4886 | char **files = NULL; |
223a3558 | 4887 | char **p; |
db1413d7 | 4888 | va_list ap; |
44143309 | 4889 | int r = 0; |
db1413d7 | 4890 | |
223a3558 KS |
4891 | va_start(ap, dir); |
4892 | dirs = strv_new_ap(dir, ap); | |
4893 | va_end(ap); | |
4894 | if (!dirs) { | |
4895 | r = -ENOMEM; | |
4896 | goto finish; | |
4897 | } | |
4898 | if (!strv_path_canonicalize(dirs)) { | |
4899 | r = -ENOMEM; | |
4900 | goto finish; | |
4901 | } | |
4902 | if (!strv_uniq(dirs)) { | |
4903 | r = -ENOMEM; | |
4904 | goto finish; | |
4905 | } | |
4906 | ||
db1413d7 KS |
4907 | fh = hashmap_new(string_hash_func, string_compare_func); |
4908 | if (!fh) { | |
44143309 | 4909 | r = -ENOMEM; |
db1413d7 KS |
4910 | goto finish; |
4911 | } | |
4912 | ||
223a3558 KS |
4913 | STRV_FOREACH(p, dirs) { |
4914 | if (files_add(fh, *p, suffix) < 0) { | |
db1413d7 | 4915 | log_error("Failed to search for files."); |
44143309 | 4916 | r = -EINVAL; |
db1413d7 KS |
4917 | goto finish; |
4918 | } | |
db1413d7 | 4919 | } |
db1413d7 KS |
4920 | |
4921 | files = hashmap_get_strv(fh); | |
4922 | if (files == NULL) { | |
4923 | log_error("Failed to compose list of files."); | |
44143309 | 4924 | r = -ENOMEM; |
db1413d7 KS |
4925 | goto finish; |
4926 | } | |
4927 | ||
4928 | qsort(files, hashmap_size(fh), sizeof(char *), base_cmp); | |
8d0e38a2 | 4929 | |
db1413d7 | 4930 | finish: |
223a3558 | 4931 | strv_free(dirs); |
db1413d7 | 4932 | hashmap_free(fh); |
44143309 KS |
4933 | *strv = files; |
4934 | return r; | |
db1413d7 | 4935 | } |
7948c4df | 4936 | |
2076cf88 | 4937 | int hwclock_is_localtime(void) { |
7948c4df | 4938 | FILE *f; |
7948c4df KS |
4939 | bool local = false; |
4940 | ||
4941 | /* | |
4942 | * The third line of adjtime is "UTC" or "LOCAL" or nothing. | |
4943 | * # /etc/adjtime | |
2076cf88 | 4944 | * 0.0 0 0 |
7948c4df KS |
4945 | * 0 |
4946 | * UTC | |
4947 | */ | |
4948 | f = fopen("/etc/adjtime", "re"); | |
4949 | if (f) { | |
2076cf88 LP |
4950 | char line[LINE_MAX]; |
4951 | bool b; | |
4952 | ||
4953 | b = fgets(line, sizeof(line), f) && | |
4954 | fgets(line, sizeof(line), f) && | |
4955 | fgets(line, sizeof(line), f); | |
4956 | ||
7948c4df | 4957 | fclose(f); |
2076cf88 LP |
4958 | |
4959 | if (!b) | |
4960 | return -EIO; | |
4961 | ||
4962 | ||
4963 | truncate_nl(line); | |
4964 | local = streq(line, "LOCAL"); | |
4965 | ||
4966 | } else if (errno != -ENOENT) | |
4967 | return -errno; | |
4968 | ||
7948c4df KS |
4969 | return local; |
4970 | } | |
4971 | ||
ff4daf5a | 4972 | int hwclock_apply_localtime_delta(int *min) { |
7948c4df | 4973 | const struct timeval *tv_null = NULL; |
2076cf88 | 4974 | struct timespec ts; |
7948c4df KS |
4975 | struct tm *tm; |
4976 | int minuteswest; | |
4977 | struct timezone tz; | |
4978 | ||
2076cf88 LP |
4979 | assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0); |
4980 | assert_se(tm = localtime(&ts.tv_sec)); | |
7948c4df KS |
4981 | minuteswest = tm->tm_gmtoff / 60; |
4982 | ||
4983 | tz.tz_minuteswest = -minuteswest; | |
4984 | tz.tz_dsttime = 0; /* DST_NONE*/ | |
4985 | ||
4986 | /* | |
4987 | * If the hardware clock does not run in UTC, but in local time: | |
4988 | * The very first time we set the kernel's timezone, it will warp | |
4989 | * the clock so that it runs in UTC instead of local time. | |
4990 | */ | |
4991 | if (settimeofday(tv_null, &tz) < 0) | |
4992 | return -errno; | |
ff4daf5a KS |
4993 | if (min) |
4994 | *min = minuteswest; | |
4995 | return 0; | |
2076cf88 LP |
4996 | } |
4997 | ||
4998 | int hwclock_reset_localtime_delta(void) { | |
4999 | const struct timeval *tv_null = NULL; | |
5000 | struct timezone tz; | |
5001 | ||
5002 | tz.tz_minuteswest = 0; | |
5003 | tz.tz_dsttime = 0; /* DST_NONE*/ | |
5004 | ||
5005 | if (settimeofday(tv_null, &tz) < 0) | |
5006 | return -errno; | |
5007 | ||
5008 | return 0; | |
7948c4df KS |
5009 | } |
5010 | ||
5011 | int hwclock_get_time(struct tm *tm) { | |
5012 | int fd; | |
5013 | int err = 0; | |
5014 | ||
2076cf88 LP |
5015 | assert(tm); |
5016 | ||
7948c4df KS |
5017 | fd = open("/dev/rtc0", O_RDONLY|O_CLOEXEC); |
5018 | if (fd < 0) | |
5019 | return -errno; | |
2076cf88 LP |
5020 | |
5021 | /* This leaves the timezone fields of struct tm | |
5022 | * uninitialized! */ | |
7948c4df KS |
5023 | if (ioctl(fd, RTC_RD_TIME, tm) < 0) |
5024 | err = -errno; | |
2076cf88 LP |
5025 | |
5026 | /* We don't now daylight saving, so we reset this in order not | |
5027 | * to confused mktime(). */ | |
5028 | tm->tm_isdst = -1; | |
5029 | ||
5030 | close_nointr_nofail(fd); | |
7948c4df KS |
5031 | |
5032 | return err; | |
5033 | } | |
5034 | ||
5035 | int hwclock_set_time(const struct tm *tm) { | |
5036 | int fd; | |
5037 | int err = 0; | |
5038 | ||
2076cf88 LP |
5039 | assert(tm); |
5040 | ||
7948c4df KS |
5041 | fd = open("/dev/rtc0", O_RDONLY|O_CLOEXEC); |
5042 | if (fd < 0) | |
5043 | return -errno; | |
2076cf88 | 5044 | |
7948c4df KS |
5045 | if (ioctl(fd, RTC_SET_TIME, tm) < 0) |
5046 | err = -errno; | |
2076cf88 LP |
5047 | |
5048 | close_nointr_nofail(fd); | |
7948c4df KS |
5049 | |
5050 | return err; | |
5051 | } | |
f41607a6 | 5052 | |
34ca941c LP |
5053 | int copy_file(const char *from, const char *to) { |
5054 | int r, fdf, fdt; | |
5055 | ||
5056 | assert(from); | |
5057 | assert(to); | |
5058 | ||
5059 | fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
5060 | if (fdf < 0) | |
5061 | return -errno; | |
5062 | ||
5063 | fdt = open(to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY, 0644); | |
5064 | if (fdt < 0) { | |
5065 | close_nointr_nofail(fdf); | |
5066 | return -errno; | |
5067 | } | |
5068 | ||
5069 | for (;;) { | |
5070 | char buf[PIPE_BUF]; | |
5071 | ssize_t n, k; | |
5072 | ||
5073 | n = read(fdf, buf, sizeof(buf)); | |
5074 | if (n < 0) { | |
5075 | r = -errno; | |
5076 | ||
5077 | close_nointr_nofail(fdf); | |
5078 | close_nointr(fdt); | |
5079 | unlink(to); | |
5080 | ||
5081 | return r; | |
5082 | } | |
5083 | ||
5084 | if (n == 0) | |
5085 | break; | |
5086 | ||
5087 | errno = 0; | |
5088 | k = loop_write(fdt, buf, n, false); | |
5089 | if (n != k) { | |
5090 | r = k < 0 ? k : (errno ? -errno : -EIO); | |
5091 | ||
5092 | close_nointr_nofail(fdf); | |
5093 | close_nointr(fdt); | |
5094 | ||
5095 | unlink(to); | |
5096 | return r; | |
5097 | } | |
5098 | } | |
5099 | ||
5100 | close_nointr_nofail(fdf); | |
5101 | r = close_nointr(fdt); | |
5102 | ||
5103 | if (r < 0) { | |
5104 | unlink(to); | |
5105 | return r; | |
5106 | } | |
5107 | ||
5108 | return 0; | |
5109 | } | |
5110 | ||
5111 | int symlink_or_copy(const char *from, const char *to) { | |
5112 | char *pf = NULL, *pt = NULL; | |
5113 | struct stat a, b; | |
5114 | int r; | |
5115 | ||
5116 | assert(from); | |
5117 | assert(to); | |
5118 | ||
5119 | if (parent_of_path(from, &pf) < 0 || | |
5120 | parent_of_path(to, &pt) < 0) { | |
5121 | r = -ENOMEM; | |
5122 | goto finish; | |
5123 | } | |
5124 | ||
5125 | if (stat(pf, &a) < 0 || | |
5126 | stat(pt, &b) < 0) { | |
5127 | r = -errno; | |
5128 | goto finish; | |
5129 | } | |
5130 | ||
5131 | if (a.st_dev != b.st_dev) { | |
5132 | free(pf); | |
5133 | free(pt); | |
5134 | ||
5135 | return copy_file(from, to); | |
5136 | } | |
5137 | ||
5138 | if (symlink(from, to) < 0) { | |
5139 | r = -errno; | |
5140 | goto finish; | |
5141 | } | |
5142 | ||
5143 | r = 0; | |
5144 | ||
5145 | finish: | |
5146 | free(pf); | |
5147 | free(pt); | |
5148 | ||
5149 | return r; | |
5150 | } | |
5151 | ||
5152 | int symlink_or_copy_atomic(const char *from, const char *to) { | |
5153 | char *t, *x; | |
5154 | const char *fn; | |
5155 | size_t k; | |
5156 | unsigned long long ull; | |
5157 | unsigned i; | |
5158 | int r; | |
5159 | ||
5160 | assert(from); | |
5161 | assert(to); | |
5162 | ||
5163 | t = new(char, strlen(to) + 1 + 16 + 1); | |
5164 | if (!t) | |
5165 | return -ENOMEM; | |
5166 | ||
5167 | fn = file_name_from_path(to); | |
5168 | k = fn-to; | |
5169 | memcpy(t, to, k); | |
5170 | t[k] = '.'; | |
5171 | x = stpcpy(t+k+1, fn); | |
5172 | ||
5173 | ull = random_ull(); | |
5174 | for (i = 0; i < 16; i++) { | |
5175 | *(x++) = hexchar(ull & 0xF); | |
5176 | ull >>= 4; | |
5177 | } | |
5178 | ||
5179 | *x = 0; | |
5180 | ||
5181 | r = symlink_or_copy(from, t); | |
5182 | if (r < 0) { | |
5183 | unlink(t); | |
5184 | free(t); | |
5185 | return r; | |
5186 | } | |
5187 | ||
5188 | if (rename(t, to) < 0) { | |
5189 | r = -errno; | |
5190 | unlink(t); | |
5191 | free(t); | |
5192 | return r; | |
5193 | } | |
5194 | ||
5195 | free(t); | |
5196 | return r; | |
5197 | } | |
5198 | ||
98a28fef | 5199 | int audit_session_from_pid(pid_t pid, uint32_t *id) { |
87d2c1ff | 5200 | char *s; |
98a28fef LP |
5201 | uint32_t u; |
5202 | int r; | |
5203 | ||
98a28fef LP |
5204 | assert(id); |
5205 | ||
5206 | if (have_effective_cap(CAP_AUDIT_CONTROL) <= 0) | |
5207 | return -ENOENT; | |
5208 | ||
87d2c1ff LP |
5209 | if (pid == 0) |
5210 | r = read_one_line_file("/proc/self/sessionid", &s); | |
5211 | else { | |
5212 | char *p; | |
5213 | ||
5214 | if (asprintf(&p, "/proc/%lu/sessionid", (unsigned long) pid) < 0) | |
5215 | return -ENOMEM; | |
5216 | ||
5217 | r = read_one_line_file(p, &s); | |
5218 | free(p); | |
5219 | } | |
98a28fef | 5220 | |
98a28fef LP |
5221 | if (r < 0) |
5222 | return r; | |
5223 | ||
5224 | r = safe_atou32(s, &u); | |
5225 | free(s); | |
5226 | ||
5227 | if (r < 0) | |
5228 | return r; | |
5229 | ||
5230 | if (u == (uint32_t) -1 || u <= 0) | |
5231 | return -ENOENT; | |
5232 | ||
5233 | *id = u; | |
5234 | return 0; | |
5235 | } | |
5236 | ||
87d2c1ff LP |
5237 | int audit_loginuid_from_pid(pid_t pid, uid_t *uid) { |
5238 | char *s; | |
5239 | uid_t u; | |
5240 | int r; | |
5241 | ||
5242 | assert(uid); | |
5243 | ||
5244 | /* Only use audit login uid if we are executed with sufficient | |
5245 | * capabilities so that pam_loginuid could do its job. If we | |
5246 | * are lacking the CAP_AUDIT_CONTROL capabality we most likely | |
5247 | * are being run in a container and /proc/self/loginuid is | |
5248 | * useless since it probably contains a uid of the host | |
5249 | * system. */ | |
5250 | ||
5251 | if (have_effective_cap(CAP_AUDIT_CONTROL) <= 0) | |
5252 | return -ENOENT; | |
5253 | ||
5254 | if (pid == 0) | |
5255 | r = read_one_line_file("/proc/self/loginuid", &s); | |
5256 | else { | |
5257 | char *p; | |
5258 | ||
5259 | if (asprintf(&p, "/proc/%lu/loginuid", (unsigned long) pid) < 0) | |
5260 | return -ENOMEM; | |
5261 | ||
5262 | r = read_one_line_file(p, &s); | |
5263 | free(p); | |
5264 | } | |
5265 | ||
5266 | if (r < 0) | |
5267 | return r; | |
5268 | ||
5269 | r = parse_uid(s, &u); | |
5270 | free(s); | |
5271 | ||
5272 | if (r < 0) | |
5273 | return r; | |
5274 | ||
5275 | if (u == (uid_t) -1) | |
5276 | return -ENOENT; | |
5277 | ||
5278 | *uid = (uid_t) u; | |
5279 | return 0; | |
5280 | } | |
5281 | ||
4d6d6518 LP |
5282 | bool display_is_local(const char *display) { |
5283 | assert(display); | |
5284 | ||
5285 | return | |
5286 | display[0] == ':' && | |
5287 | display[1] >= '0' && | |
5288 | display[1] <= '9'; | |
5289 | } | |
5290 | ||
5291 | int socket_from_display(const char *display, char **path) { | |
5292 | size_t k; | |
5293 | char *f, *c; | |
5294 | ||
5295 | assert(display); | |
5296 | assert(path); | |
5297 | ||
5298 | if (!display_is_local(display)) | |
5299 | return -EINVAL; | |
5300 | ||
5301 | k = strspn(display+1, "0123456789"); | |
5302 | ||
5303 | f = new(char, sizeof("/tmp/.X11-unix/X") + k); | |
5304 | if (!f) | |
5305 | return -ENOMEM; | |
5306 | ||
5307 | c = stpcpy(f, "/tmp/.X11-unix/X"); | |
5308 | memcpy(c, display+1, k); | |
5309 | c[k] = 0; | |
5310 | ||
5311 | *path = f; | |
5312 | ||
5313 | return 0; | |
5314 | } | |
5315 | ||
1cccf435 MV |
5316 | int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) { |
5317 | struct passwd *p; | |
ddd88763 | 5318 | uid_t u; |
1cccf435 MV |
5319 | |
5320 | assert(username); | |
5321 | assert(*username); | |
1cccf435 MV |
5322 | |
5323 | /* We enforce some special rules for uid=0: in order to avoid | |
5324 | * NSS lookups for root we hardcode its data. */ | |
5325 | ||
5326 | if (streq(*username, "root") || streq(*username, "0")) { | |
5327 | *username = "root"; | |
4b67834e LP |
5328 | |
5329 | if (uid) | |
5330 | *uid = 0; | |
5331 | ||
5332 | if (gid) | |
5333 | *gid = 0; | |
5334 | ||
5335 | if (home) | |
5336 | *home = "/root"; | |
1cccf435 MV |
5337 | return 0; |
5338 | } | |
5339 | ||
ddd88763 | 5340 | if (parse_uid(*username, &u) >= 0) { |
1cccf435 | 5341 | errno = 0; |
ddd88763 | 5342 | p = getpwuid(u); |
1cccf435 MV |
5343 | |
5344 | /* If there are multiple users with the same id, make | |
5345 | * sure to leave $USER to the configured value instead | |
5346 | * of the first occurrence in the database. However if | |
5347 | * the uid was configured by a numeric uid, then let's | |
5348 | * pick the real username from /etc/passwd. */ | |
5349 | if (p) | |
5350 | *username = p->pw_name; | |
5351 | } else { | |
5352 | errno = 0; | |
5353 | p = getpwnam(*username); | |
5354 | } | |
5355 | ||
5356 | if (!p) | |
5357 | return errno != 0 ? -errno : -ESRCH; | |
5358 | ||
4b67834e LP |
5359 | if (uid) |
5360 | *uid = p->pw_uid; | |
5361 | ||
5362 | if (gid) | |
5363 | *gid = p->pw_gid; | |
5364 | ||
5365 | if (home) | |
5366 | *home = p->pw_dir; | |
5367 | ||
5368 | return 0; | |
5369 | } | |
5370 | ||
5371 | int get_group_creds(const char **groupname, gid_t *gid) { | |
5372 | struct group *g; | |
5373 | gid_t id; | |
5374 | ||
5375 | assert(groupname); | |
5376 | ||
5377 | /* We enforce some special rules for gid=0: in order to avoid | |
5378 | * NSS lookups for root we hardcode its data. */ | |
5379 | ||
5380 | if (streq(*groupname, "root") || streq(*groupname, "0")) { | |
5381 | *groupname = "root"; | |
5382 | ||
5383 | if (gid) | |
5384 | *gid = 0; | |
5385 | ||
5386 | return 0; | |
5387 | } | |
5388 | ||
5389 | if (parse_gid(*groupname, &id) >= 0) { | |
5390 | errno = 0; | |
5391 | g = getgrgid(id); | |
5392 | ||
5393 | if (g) | |
5394 | *groupname = g->gr_name; | |
5395 | } else { | |
5396 | errno = 0; | |
5397 | g = getgrnam(*groupname); | |
5398 | } | |
5399 | ||
5400 | if (!g) | |
5401 | return errno != 0 ? -errno : -ESRCH; | |
5402 | ||
5403 | if (gid) | |
5404 | *gid = g->gr_gid; | |
5405 | ||
1cccf435 MV |
5406 | return 0; |
5407 | } | |
5408 | ||
8092a428 LP |
5409 | int glob_exists(const char *path) { |
5410 | glob_t g; | |
5411 | int r, k; | |
5412 | ||
5413 | assert(path); | |
5414 | ||
5415 | zero(g); | |
5416 | errno = 0; | |
5417 | k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g); | |
5418 | ||
5419 | if (k == GLOB_NOMATCH) | |
5420 | r = 0; | |
5421 | else if (k == GLOB_NOSPACE) | |
5422 | r = -ENOMEM; | |
5423 | else if (k == 0) | |
5424 | r = !strv_isempty(g.gl_pathv); | |
5425 | else | |
5426 | r = errno ? -errno : -EIO; | |
5427 | ||
5428 | globfree(&g); | |
5429 | ||
5430 | return r; | |
5431 | } | |
5432 | ||
83096483 LP |
5433 | int dirent_ensure_type(DIR *d, struct dirent *de) { |
5434 | struct stat st; | |
5435 | ||
5436 | assert(d); | |
5437 | assert(de); | |
5438 | ||
5439 | if (de->d_type != DT_UNKNOWN) | |
5440 | return 0; | |
5441 | ||
5442 | if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) | |
5443 | return -errno; | |
5444 | ||
5445 | de->d_type = | |
5446 | S_ISREG(st.st_mode) ? DT_REG : | |
5447 | S_ISDIR(st.st_mode) ? DT_DIR : | |
5448 | S_ISLNK(st.st_mode) ? DT_LNK : | |
5449 | S_ISFIFO(st.st_mode) ? DT_FIFO : | |
5450 | S_ISSOCK(st.st_mode) ? DT_SOCK : | |
5451 | S_ISCHR(st.st_mode) ? DT_CHR : | |
5452 | S_ISBLK(st.st_mode) ? DT_BLK : | |
5453 | DT_UNKNOWN; | |
5454 | ||
5455 | return 0; | |
5456 | } | |
5457 | ||
5458 | int in_search_path(const char *path, char **search) { | |
5459 | char **i, *parent; | |
5460 | int r; | |
5461 | ||
5462 | r = parent_of_path(path, &parent); | |
5463 | if (r < 0) | |
5464 | return r; | |
5465 | ||
5466 | r = 0; | |
5467 | ||
5468 | STRV_FOREACH(i, search) { | |
5469 | if (path_equal(parent, *i)) { | |
5470 | r = 1; | |
5471 | break; | |
5472 | } | |
5473 | } | |
5474 | ||
5475 | free(parent); | |
5476 | ||
5477 | return r; | |
5478 | } | |
5479 | ||
034a2a52 LP |
5480 | int get_files_in_directory(const char *path, char ***list) { |
5481 | DIR *d; | |
5482 | int r = 0; | |
5483 | unsigned n = 0; | |
5484 | char **l = NULL; | |
5485 | ||
5486 | assert(path); | |
d60ef526 LP |
5487 | |
5488 | /* Returns all files in a directory in *list, and the number | |
5489 | * of files as return value. If list is NULL returns only the | |
5490 | * number */ | |
034a2a52 LP |
5491 | |
5492 | d = opendir(path); | |
8ea913b2 LP |
5493 | if (!d) |
5494 | return -errno; | |
5495 | ||
034a2a52 LP |
5496 | for (;;) { |
5497 | struct dirent buffer, *de; | |
5498 | int k; | |
5499 | ||
5500 | k = readdir_r(d, &buffer, &de); | |
5501 | if (k != 0) { | |
5502 | r = -k; | |
5503 | goto finish; | |
5504 | } | |
5505 | ||
5506 | if (!de) | |
5507 | break; | |
5508 | ||
5509 | dirent_ensure_type(d, de); | |
5510 | ||
5511 | if (!dirent_is_file(de)) | |
5512 | continue; | |
5513 | ||
d60ef526 LP |
5514 | if (list) { |
5515 | if ((unsigned) r >= n) { | |
5516 | char **t; | |
034a2a52 | 5517 | |
d60ef526 LP |
5518 | n = MAX(16, 2*r); |
5519 | t = realloc(l, sizeof(char*) * n); | |
5520 | if (!t) { | |
5521 | r = -ENOMEM; | |
5522 | goto finish; | |
5523 | } | |
034a2a52 | 5524 | |
d60ef526 LP |
5525 | l = t; |
5526 | } | |
034a2a52 | 5527 | |
d60ef526 | 5528 | assert((unsigned) r < n); |
034a2a52 | 5529 | |
d60ef526 LP |
5530 | l[r] = strdup(de->d_name); |
5531 | if (!l[r]) { | |
5532 | r = -ENOMEM; | |
5533 | goto finish; | |
5534 | } | |
034a2a52 | 5535 | |
d60ef526 LP |
5536 | l[++r] = NULL; |
5537 | } else | |
5538 | r++; | |
034a2a52 LP |
5539 | } |
5540 | ||
5541 | finish: | |
5542 | if (d) | |
5543 | closedir(d); | |
5544 | ||
d60ef526 LP |
5545 | if (r >= 0) { |
5546 | if (list) | |
5547 | *list = l; | |
5548 | } else | |
034a2a52 LP |
5549 | strv_free(l); |
5550 | ||
5551 | return r; | |
5552 | } | |
5553 | ||
911a4828 LP |
5554 | char *join(const char *x, ...) { |
5555 | va_list ap; | |
5556 | size_t l; | |
5557 | char *r, *p; | |
5558 | ||
5559 | va_start(ap, x); | |
5560 | ||
5561 | if (x) { | |
5562 | l = strlen(x); | |
5563 | ||
5564 | for (;;) { | |
5565 | const char *t; | |
5566 | ||
5567 | t = va_arg(ap, const char *); | |
5568 | if (!t) | |
5569 | break; | |
5570 | ||
5571 | l += strlen(t); | |
5572 | } | |
5573 | } else | |
5574 | l = 0; | |
5575 | ||
5576 | va_end(ap); | |
5577 | ||
5578 | r = new(char, l+1); | |
5579 | if (!r) | |
5580 | return NULL; | |
5581 | ||
5582 | if (x) { | |
5583 | p = stpcpy(r, x); | |
5584 | ||
5585 | va_start(ap, x); | |
5586 | ||
5587 | for (;;) { | |
5588 | const char *t; | |
5589 | ||
5590 | t = va_arg(ap, const char *); | |
5591 | if (!t) | |
5592 | break; | |
5593 | ||
5594 | p = stpcpy(p, t); | |
5595 | } | |
8ea913b2 LP |
5596 | |
5597 | va_end(ap); | |
911a4828 LP |
5598 | } else |
5599 | r[0] = 0; | |
5600 | ||
5601 | return r; | |
5602 | } | |
5603 | ||
b636465b LP |
5604 | bool is_main_thread(void) { |
5605 | static __thread int cached = 0; | |
5606 | ||
5607 | if (_unlikely_(cached == 0)) | |
5608 | cached = getpid() == gettid() ? 1 : -1; | |
5609 | ||
5610 | return cached > 0; | |
5611 | } | |
5612 | ||
94959f0f LP |
5613 | int block_get_whole_disk(dev_t d, dev_t *ret) { |
5614 | char *p, *s; | |
5615 | int r; | |
5616 | unsigned n, m; | |
5617 | ||
5618 | assert(ret); | |
5619 | ||
5620 | /* If it has a queue this is good enough for us */ | |
5621 | if (asprintf(&p, "/sys/dev/block/%u:%u/queue", major(d), minor(d)) < 0) | |
5622 | return -ENOMEM; | |
5623 | ||
5624 | r = access(p, F_OK); | |
5625 | free(p); | |
5626 | ||
5627 | if (r >= 0) { | |
5628 | *ret = d; | |
5629 | return 0; | |
5630 | } | |
5631 | ||
5632 | /* If it is a partition find the originating device */ | |
5633 | if (asprintf(&p, "/sys/dev/block/%u:%u/partition", major(d), minor(d)) < 0) | |
5634 | return -ENOMEM; | |
5635 | ||
5636 | r = access(p, F_OK); | |
5637 | free(p); | |
5638 | ||
5639 | if (r < 0) | |
5640 | return -ENOENT; | |
5641 | ||
5642 | /* Get parent dev_t */ | |
5643 | if (asprintf(&p, "/sys/dev/block/%u:%u/../dev", major(d), minor(d)) < 0) | |
5644 | return -ENOMEM; | |
5645 | ||
5646 | r = read_one_line_file(p, &s); | |
5647 | free(p); | |
5648 | ||
5649 | if (r < 0) | |
5650 | return r; | |
5651 | ||
5652 | r = sscanf(s, "%u:%u", &m, &n); | |
5653 | free(s); | |
5654 | ||
5655 | if (r != 2) | |
5656 | return -EINVAL; | |
5657 | ||
5658 | /* Only return this if it is really good enough for us. */ | |
5659 | if (asprintf(&p, "/sys/dev/block/%u:%u/queue", m, n) < 0) | |
5660 | return -ENOMEM; | |
5661 | ||
5662 | r = access(p, F_OK); | |
5663 | free(p); | |
5664 | ||
5665 | if (r >= 0) { | |
5666 | *ret = makedev(m, n); | |
5667 | return 0; | |
5668 | } | |
5669 | ||
5670 | return -ENOENT; | |
5671 | } | |
5672 | ||
ad293f5a LP |
5673 | int file_is_sticky(const char *p) { |
5674 | struct stat st; | |
5675 | ||
5676 | assert(p); | |
5677 | ||
5678 | if (lstat(p, &st) < 0) | |
5679 | return -errno; | |
5680 | ||
5681 | return | |
5682 | st.st_uid == 0 && | |
5683 | (st.st_mode & S_ISVTX); | |
5684 | } | |
94959f0f | 5685 | |
f41607a6 LP |
5686 | static const char *const ioprio_class_table[] = { |
5687 | [IOPRIO_CLASS_NONE] = "none", | |
5688 | [IOPRIO_CLASS_RT] = "realtime", | |
5689 | [IOPRIO_CLASS_BE] = "best-effort", | |
5690 | [IOPRIO_CLASS_IDLE] = "idle" | |
5691 | }; | |
5692 | ||
5693 | DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int); | |
5694 | ||
5695 | static const char *const sigchld_code_table[] = { | |
5696 | [CLD_EXITED] = "exited", | |
5697 | [CLD_KILLED] = "killed", | |
5698 | [CLD_DUMPED] = "dumped", | |
5699 | [CLD_TRAPPED] = "trapped", | |
5700 | [CLD_STOPPED] = "stopped", | |
5701 | [CLD_CONTINUED] = "continued", | |
5702 | }; | |
5703 | ||
5704 | DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int); | |
5705 | ||
5706 | static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = { | |
5707 | [LOG_FAC(LOG_KERN)] = "kern", | |
5708 | [LOG_FAC(LOG_USER)] = "user", | |
5709 | [LOG_FAC(LOG_MAIL)] = "mail", | |
5710 | [LOG_FAC(LOG_DAEMON)] = "daemon", | |
5711 | [LOG_FAC(LOG_AUTH)] = "auth", | |
5712 | [LOG_FAC(LOG_SYSLOG)] = "syslog", | |
5713 | [LOG_FAC(LOG_LPR)] = "lpr", | |
5714 | [LOG_FAC(LOG_NEWS)] = "news", | |
5715 | [LOG_FAC(LOG_UUCP)] = "uucp", | |
5716 | [LOG_FAC(LOG_CRON)] = "cron", | |
5717 | [LOG_FAC(LOG_AUTHPRIV)] = "authpriv", | |
5718 | [LOG_FAC(LOG_FTP)] = "ftp", | |
5719 | [LOG_FAC(LOG_LOCAL0)] = "local0", | |
5720 | [LOG_FAC(LOG_LOCAL1)] = "local1", | |
5721 | [LOG_FAC(LOG_LOCAL2)] = "local2", | |
5722 | [LOG_FAC(LOG_LOCAL3)] = "local3", | |
5723 | [LOG_FAC(LOG_LOCAL4)] = "local4", | |
5724 | [LOG_FAC(LOG_LOCAL5)] = "local5", | |
5725 | [LOG_FAC(LOG_LOCAL6)] = "local6", | |
5726 | [LOG_FAC(LOG_LOCAL7)] = "local7" | |
5727 | }; | |
5728 | ||
5729 | DEFINE_STRING_TABLE_LOOKUP(log_facility_unshifted, int); | |
5730 | ||
5731 | static const char *const log_level_table[] = { | |
5732 | [LOG_EMERG] = "emerg", | |
5733 | [LOG_ALERT] = "alert", | |
5734 | [LOG_CRIT] = "crit", | |
5735 | [LOG_ERR] = "err", | |
5736 | [LOG_WARNING] = "warning", | |
5737 | [LOG_NOTICE] = "notice", | |
5738 | [LOG_INFO] = "info", | |
5739 | [LOG_DEBUG] = "debug" | |
5740 | }; | |
5741 | ||
5742 | DEFINE_STRING_TABLE_LOOKUP(log_level, int); | |
5743 | ||
5744 | static const char* const sched_policy_table[] = { | |
5745 | [SCHED_OTHER] = "other", | |
5746 | [SCHED_BATCH] = "batch", | |
5747 | [SCHED_IDLE] = "idle", | |
5748 | [SCHED_FIFO] = "fifo", | |
5749 | [SCHED_RR] = "rr" | |
5750 | }; | |
5751 | ||
5752 | DEFINE_STRING_TABLE_LOOKUP(sched_policy, int); | |
5753 | ||
5754 | static const char* const rlimit_table[] = { | |
5755 | [RLIMIT_CPU] = "LimitCPU", | |
5756 | [RLIMIT_FSIZE] = "LimitFSIZE", | |
5757 | [RLIMIT_DATA] = "LimitDATA", | |
5758 | [RLIMIT_STACK] = "LimitSTACK", | |
5759 | [RLIMIT_CORE] = "LimitCORE", | |
5760 | [RLIMIT_RSS] = "LimitRSS", | |
5761 | [RLIMIT_NOFILE] = "LimitNOFILE", | |
5762 | [RLIMIT_AS] = "LimitAS", | |
5763 | [RLIMIT_NPROC] = "LimitNPROC", | |
5764 | [RLIMIT_MEMLOCK] = "LimitMEMLOCK", | |
5765 | [RLIMIT_LOCKS] = "LimitLOCKS", | |
5766 | [RLIMIT_SIGPENDING] = "LimitSIGPENDING", | |
5767 | [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE", | |
5768 | [RLIMIT_NICE] = "LimitNICE", | |
5769 | [RLIMIT_RTPRIO] = "LimitRTPRIO", | |
5770 | [RLIMIT_RTTIME] = "LimitRTTIME" | |
5771 | }; | |
5772 | ||
5773 | DEFINE_STRING_TABLE_LOOKUP(rlimit, int); | |
5774 | ||
5775 | static const char* const ip_tos_table[] = { | |
5776 | [IPTOS_LOWDELAY] = "low-delay", | |
5777 | [IPTOS_THROUGHPUT] = "throughput", | |
5778 | [IPTOS_RELIABILITY] = "reliability", | |
5779 | [IPTOS_LOWCOST] = "low-cost", | |
5780 | }; | |
5781 | ||
5782 | DEFINE_STRING_TABLE_LOOKUP(ip_tos, int); | |
5783 | ||
4e240ab0 | 5784 | static const char *const __signal_table[] = { |
f41607a6 LP |
5785 | [SIGHUP] = "HUP", |
5786 | [SIGINT] = "INT", | |
5787 | [SIGQUIT] = "QUIT", | |
5788 | [SIGILL] = "ILL", | |
5789 | [SIGTRAP] = "TRAP", | |
5790 | [SIGABRT] = "ABRT", | |
5791 | [SIGBUS] = "BUS", | |
5792 | [SIGFPE] = "FPE", | |
5793 | [SIGKILL] = "KILL", | |
5794 | [SIGUSR1] = "USR1", | |
5795 | [SIGSEGV] = "SEGV", | |
5796 | [SIGUSR2] = "USR2", | |
5797 | [SIGPIPE] = "PIPE", | |
5798 | [SIGALRM] = "ALRM", | |
5799 | [SIGTERM] = "TERM", | |
5800 | #ifdef SIGSTKFLT | |
5801 | [SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */ | |
5802 | #endif | |
5803 | [SIGCHLD] = "CHLD", | |
5804 | [SIGCONT] = "CONT", | |
5805 | [SIGSTOP] = "STOP", | |
5806 | [SIGTSTP] = "TSTP", | |
5807 | [SIGTTIN] = "TTIN", | |
5808 | [SIGTTOU] = "TTOU", | |
5809 | [SIGURG] = "URG", | |
5810 | [SIGXCPU] = "XCPU", | |
5811 | [SIGXFSZ] = "XFSZ", | |
5812 | [SIGVTALRM] = "VTALRM", | |
5813 | [SIGPROF] = "PROF", | |
5814 | [SIGWINCH] = "WINCH", | |
5815 | [SIGIO] = "IO", | |
5816 | [SIGPWR] = "PWR", | |
5817 | [SIGSYS] = "SYS" | |
5818 | }; | |
5819 | ||
4e240ab0 MS |
5820 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int); |
5821 | ||
5822 | const char *signal_to_string(int signo) { | |
5823 | static __thread char buf[12]; | |
5824 | const char *name; | |
5825 | ||
5826 | name = __signal_to_string(signo); | |
5827 | if (name) | |
5828 | return name; | |
5829 | ||
5830 | if (signo >= SIGRTMIN && signo <= SIGRTMAX) | |
5831 | snprintf(buf, sizeof(buf) - 1, "RTMIN+%d", signo - SIGRTMIN); | |
5832 | else | |
5833 | snprintf(buf, sizeof(buf) - 1, "%d", signo); | |
5834 | char_array_0(buf); | |
5835 | return buf; | |
5836 | } | |
5837 | ||
5838 | int signal_from_string(const char *s) { | |
5839 | int signo; | |
5840 | int offset = 0; | |
5841 | unsigned u; | |
5842 | ||
5843 | signo =__signal_from_string(s); | |
5844 | if (signo > 0) | |
5845 | return signo; | |
5846 | ||
5847 | if (startswith(s, "RTMIN+")) { | |
5848 | s += 6; | |
5849 | offset = SIGRTMIN; | |
5850 | } | |
5851 | if (safe_atou(s, &u) >= 0) { | |
5852 | signo = (int) u + offset; | |
5853 | if (signo > 0 && signo < _NSIG) | |
5854 | return signo; | |
5855 | } | |
5856 | return -1; | |
5857 | } | |
65457142 FC |
5858 | |
5859 | bool kexec_loaded(void) { | |
5860 | bool loaded = false; | |
5861 | char *s; | |
5862 | ||
5863 | if (read_one_line_file("/sys/kernel/kexec_loaded", &s) >= 0) { | |
5864 | if (s[0] == '1') | |
5865 | loaded = true; | |
5866 | free(s); | |
5867 | } | |
5868 | return loaded; | |
5869 | } | |
fb9de93d LP |
5870 | |
5871 | int strdup_or_null(const char *a, char **b) { | |
5872 | char *c; | |
5873 | ||
5874 | assert(b); | |
5875 | ||
5876 | if (!a) { | |
5877 | *b = NULL; | |
5878 | return 0; | |
5879 | } | |
5880 | ||
5881 | c = strdup(a); | |
5882 | if (!c) | |
5883 | return -ENOMEM; | |
5884 | ||
5885 | *b = c; | |
5886 | return 0; | |
5887 | } | |
87d2c1ff LP |
5888 | |
5889 | int prot_from_flags(int flags) { | |
5890 | ||
5891 | switch (flags & O_ACCMODE) { | |
5892 | ||
5893 | case O_RDONLY: | |
5894 | return PROT_READ; | |
5895 | ||
5896 | case O_WRONLY: | |
5897 | return PROT_WRITE; | |
5898 | ||
5899 | case O_RDWR: | |
5900 | return PROT_READ|PROT_WRITE; | |
5901 | ||
5902 | default: | |
5903 | return -EINVAL; | |
5904 | } | |
689b9a22 | 5905 | |
64685e0c LP |
5906 | unsigned long cap_last_cap(void) { |
5907 | static __thread unsigned long saved; | |
5908 | static __thread bool valid = false; | |
5909 | unsigned long p; | |
5910 | ||
5911 | if (valid) | |
5912 | return saved; | |
5913 | ||
5914 | p = (unsigned long) CAP_LAST_CAP; | |
5915 | ||
5916 | if (prctl(PR_CAPBSET_READ, p) < 0) { | |
5917 | ||
5918 | /* Hmm, look downwards, until we find one that | |
5919 | * works */ | |
5920 | for (p--; p > 0; p --) | |
5921 | if (prctl(PR_CAPBSET_READ, p) >= 0) | |
5922 | break; | |
5923 | ||
5924 | } else { | |
5925 | ||
5926 | /* Hmm, look upwards, until we find one that doesn't | |
5927 | * work */ | |
5928 | for (;; p++) | |
5929 | if (prctl(PR_CAPBSET_READ, p+1) < 0) | |
5930 | break; | |
5931 | } | |
5932 | ||
5933 | saved = p; | |
5934 | valid = true; | |
5935 | ||
5936 | return p; | |
87d2c1ff | 5937 | } |