]>
Commit | Line | Data |
---|---|---|
5ba2dc25 KS |
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 <sys/stat.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <assert.h> | |
27 | #include <unistd.h> | |
28 | ||
29 | #include "dev-setup.h" | |
30 | #include "log.h" | |
31 | #include "macro.h" | |
32 | #include "util.h" | |
33 | #include "label.h" | |
34 | ||
35 | static int symlink_and_label(const char *old_path, const char *new_path) { | |
36 | int r; | |
37 | ||
38 | assert(old_path); | |
39 | assert(new_path); | |
40 | ||
41 | r = label_context_set(new_path, S_IFLNK); | |
42 | if (r < 0) | |
43 | return r; | |
44 | ||
45 | if (symlink(old_path, new_path) < 0) | |
46 | r = -errno; | |
47 | ||
48 | label_context_clear(); | |
49 | ||
50 | return r; | |
51 | } | |
52 | ||
7f112f50 | 53 | int dev_setup(const char *prefix) { |
5ba2dc25 KS |
54 | const char *j, *k; |
55 | ||
56 | static const char symlinks[] = | |
696fee7d | 57 | "-/proc/kcore\0" "/dev/core\0" |
5ba2dc25 KS |
58 | "/proc/self/fd\0" "/dev/fd\0" |
59 | "/proc/self/fd/0\0" "/dev/stdin\0" | |
60 | "/proc/self/fd/1\0" "/dev/stdout\0" | |
61 | "/proc/self/fd/2\0" "/dev/stderr\0"; | |
62 | ||
8f0e73f2 | 63 | NULSTR_FOREACH_PAIR(j, k, symlinks) { |
696fee7d ZJS |
64 | if (j[0] == '-') { |
65 | j++; | |
66 | ||
2b85f4e1 | 67 | if (access(j, F_OK) < 0) |
696fee7d ZJS |
68 | continue; |
69 | } | |
8f0e73f2 | 70 | |
01ed0e23 | 71 | if (prefix) { |
7f112f50 | 72 | _cleanup_free_ char *link_name = NULL; |
8f0e73f2 | 73 | |
7f112f50 LP |
74 | link_name = strjoin(prefix, "/", k, NULL); |
75 | if (!link_name) | |
76 | return -ENOMEM; | |
8f0e73f2 | 77 | |
7f112f50 | 78 | symlink_and_label(j, link_name); |
01ed0e23 LP |
79 | } else |
80 | symlink_and_label(j, k); | |
8f0e73f2 | 81 | } |
7f112f50 LP |
82 | |
83 | return 0; | |
5ba2dc25 | 84 | } |