Line data Source code
1 : /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2 :
3 : /***
4 : This file is part of systemd.
5 :
6 : Copyright 2010-2012 Lennart Poettering
7 :
8 : systemd is free software; you can redistribute it and/or modify it
9 : under the terms of the GNU Lesser General Public License as published by
10 : the Free Software Foundation; either version 2.1 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 : Lesser General Public License for more details.
17 :
18 : You should have received a copy of the GNU Lesser General Public License
19 : along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 : ***/
21 :
22 : #include <errno.h>
23 : #include <stdlib.h>
24 : #include <unistd.h>
25 :
26 : #include "util.h"
27 : #include "label.h"
28 : #include "path-util.h"
29 : #include "dev-setup.h"
30 :
31 0 : int dev_setup(const char *prefix, uid_t uid, gid_t gid) {
32 : static const char symlinks[] =
33 : "-/proc/kcore\0" "/dev/core\0"
34 : "/proc/self/fd\0" "/dev/fd\0"
35 : "/proc/self/fd/0\0" "/dev/stdin\0"
36 : "/proc/self/fd/1\0" "/dev/stdout\0"
37 : "/proc/self/fd/2\0" "/dev/stderr\0";
38 :
39 : const char *j, *k;
40 : int r;
41 :
42 0 : NULSTR_FOREACH_PAIR(j, k, symlinks) {
43 0 : _cleanup_free_ char *link_name = NULL;
44 : const char *n;
45 :
46 0 : if (j[0] == '-') {
47 0 : j++;
48 :
49 0 : if (access(j, F_OK) < 0)
50 0 : continue;
51 : }
52 :
53 0 : if (prefix) {
54 0 : link_name = prefix_root(prefix, k);
55 0 : if (!link_name)
56 0 : return -ENOMEM;
57 :
58 0 : n = link_name;
59 : } else
60 0 : n = k;
61 :
62 0 : r = symlink_label(j, n);
63 0 : if (r < 0)
64 0 : log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n);
65 :
66 0 : if (uid != UID_INVALID || gid != GID_INVALID)
67 0 : if (lchown(n, uid, gid) < 0)
68 0 : log_debug_errno(errno, "Failed to chown %s: %m", n);
69 : }
70 :
71 0 : return 0;
72 : }
|