]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dev-setup.c
4f9c0f42851bafe05770344e3a0c6ff1d917f9e6
[thirdparty/systemd.git] / src / shared / dev-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2010-2012 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9
10 #include "alloc-util.h"
11 #include "dev-setup.h"
12 #include "label.h"
13 #include "log.h"
14 #include "path-util.h"
15 #include "user-util.h"
16 #include "util.h"
17
18 int dev_setup(const char *prefix, uid_t uid, gid_t gid) {
19 static const char symlinks[] =
20 "-/proc/kcore\0" "/dev/core\0"
21 "/proc/self/fd\0" "/dev/fd\0"
22 "/proc/self/fd/0\0" "/dev/stdin\0"
23 "/proc/self/fd/1\0" "/dev/stdout\0"
24 "/proc/self/fd/2\0" "/dev/stderr\0";
25
26 const char *j, *k;
27 int r;
28
29 NULSTR_FOREACH_PAIR(j, k, symlinks) {
30 _cleanup_free_ char *link_name = NULL;
31 const char *n;
32
33 if (j[0] == '-') {
34 j++;
35
36 if (access(j, F_OK) < 0)
37 continue;
38 }
39
40 if (prefix) {
41 link_name = prefix_root(prefix, k);
42 if (!link_name)
43 return -ENOMEM;
44
45 n = link_name;
46 } else
47 n = k;
48
49 r = symlink_label(j, n);
50 if (r < 0)
51 log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n);
52
53 if (uid != UID_INVALID || gid != GID_INVALID)
54 if (lchown(n, uid, gid) < 0)
55 log_debug_errno(errno, "Failed to chown %s: %m", n);
56 }
57
58 return 0;
59 }