]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dev-setup.c
Merge pull request #1821 from darkcircle/ko-catalog-translation
[thirdparty/systemd.git] / src / shared / dev-setup.c
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 "alloc-util.h"
27 #include "dev-setup.h"
28 #include "label.h"
29 #include "path-util.h"
30 #include "user-util.h"
31 #include "util.h"
32
33 int dev_setup(const char *prefix, uid_t uid, gid_t gid) {
34 static const char symlinks[] =
35 "-/proc/kcore\0" "/dev/core\0"
36 "/proc/self/fd\0" "/dev/fd\0"
37 "/proc/self/fd/0\0" "/dev/stdin\0"
38 "/proc/self/fd/1\0" "/dev/stdout\0"
39 "/proc/self/fd/2\0" "/dev/stderr\0";
40
41 const char *j, *k;
42 int r;
43
44 NULSTR_FOREACH_PAIR(j, k, symlinks) {
45 _cleanup_free_ char *link_name = NULL;
46 const char *n;
47
48 if (j[0] == '-') {
49 j++;
50
51 if (access(j, F_OK) < 0)
52 continue;
53 }
54
55 if (prefix) {
56 link_name = prefix_root(prefix, k);
57 if (!link_name)
58 return -ENOMEM;
59
60 n = link_name;
61 } else
62 n = k;
63
64 r = symlink_label(j, n);
65 if (r < 0)
66 log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n);
67
68 if (uid != UID_INVALID || gid != GID_INVALID)
69 if (lchown(n, uid, gid) < 0)
70 log_debug_errno(errno, "Failed to chown %s: %m", n);
71 }
72
73 return 0;
74 }