]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
5ba2dc25 KS |
2 | |
3 | #include <errno.h> | |
5ba2dc25 | 4 | #include <stdlib.h> |
5ba2dc25 KS |
5 | #include <unistd.h> |
6 | ||
b5efdb8a | 7 | #include "alloc-util.h" |
ee104e11 | 8 | #include "dev-setup.h" |
5ba2dc25 | 9 | #include "label.h" |
a8fbdf54 | 10 | #include "log.h" |
03cfe0d5 | 11 | #include "path-util.h" |
ee104e11 LP |
12 | #include "user-util.h" |
13 | #include "util.h" | |
5ba2dc25 | 14 | |
03cfe0d5 | 15 | int dev_setup(const char *prefix, uid_t uid, gid_t gid) { |
5ba2dc25 | 16 | static const char symlinks[] = |
696fee7d | 17 | "-/proc/kcore\0" "/dev/core\0" |
5ba2dc25 KS |
18 | "/proc/self/fd\0" "/dev/fd\0" |
19 | "/proc/self/fd/0\0" "/dev/stdin\0" | |
20 | "/proc/self/fd/1\0" "/dev/stdout\0" | |
21 | "/proc/self/fd/2\0" "/dev/stderr\0"; | |
22 | ||
03cfe0d5 LP |
23 | const char *j, *k; |
24 | int r; | |
25 | ||
8f0e73f2 | 26 | NULSTR_FOREACH_PAIR(j, k, symlinks) { |
03cfe0d5 LP |
27 | _cleanup_free_ char *link_name = NULL; |
28 | const char *n; | |
29 | ||
696fee7d ZJS |
30 | if (j[0] == '-') { |
31 | j++; | |
32 | ||
2b85f4e1 | 33 | if (access(j, F_OK) < 0) |
696fee7d ZJS |
34 | continue; |
35 | } | |
8f0e73f2 | 36 | |
01ed0e23 | 37 | if (prefix) { |
03cfe0d5 | 38 | link_name = prefix_root(prefix, k); |
7f112f50 LP |
39 | if (!link_name) |
40 | return -ENOMEM; | |
8f0e73f2 | 41 | |
03cfe0d5 | 42 | n = link_name; |
01ed0e23 | 43 | } else |
03cfe0d5 LP |
44 | n = k; |
45 | ||
46 | r = symlink_label(j, n); | |
47 | if (r < 0) | |
48 | log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n); | |
49 | ||
50 | if (uid != UID_INVALID || gid != GID_INVALID) | |
51 | if (lchown(n, uid, gid) < 0) | |
52 | log_debug_errno(errno, "Failed to chown %s: %m", n); | |
8f0e73f2 | 53 | } |
7f112f50 LP |
54 | |
55 | return 0; | |
5ba2dc25 | 56 | } |