1 /***
2 This file is part of eudev, forked from systemd.
3
4 Copyright 2010-2012 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <sys/stat.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <unistd.h>
26
27 #include "log.h"
28 #include "macro.h"
29 #include "util.h"
30 #include "label.h"
31 #include "path-util.h"
32 #include "dev-setup.h"
33
dev_setup(const char * prefix,uid_t uid,gid_t gid)34 int dev_setup(const char *prefix, uid_t uid, gid_t gid) {
35 static const char symlinks[] =
36 "-/proc/kcore\0" "/dev/core\0"
37 "/proc/self/fd\0" "/dev/fd\0"
38 "/proc/self/fd/0\0" "/dev/stdin\0"
39 "/proc/self/fd/1\0" "/dev/stdout\0"
40 "/proc/self/fd/2\0" "/dev/stderr\0";
41
42 const char *j, *k;
43 int r;
44
45 NULSTR_FOREACH_PAIR(j, k, symlinks) {
46 _cleanup_free_ char *link_name = NULL;
47 const char *n;
48
49 if (j[0] == '-') {
50 j++;
51
52 if (access(j, F_OK) < 0)
53 continue;
54 }
55
56 if (prefix) {
57 link_name = prefix_root(prefix, k);
58 if (!link_name)
59 return -ENOMEM;
60
61 n = link_name;
62 } else
63 n = k;
64
65 r = symlink_label(j, n);
66 if (r < 0)
67 log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n);
68
69 if (uid != UID_INVALID || gid != GID_INVALID)
70 if (lchown(n, uid, gid) < 0)
71 log_debug_errno(errno, "Failed to chown %s: %m", n);
72 }
73
74 return 0;
75 }
76