chiark / gitweb /
core: introduce exit_status_set_is_empty() to make things a bit easier to read
[elogind.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 <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
53 int dev_setup(const char *prefix) {
54         const char *j, *k;
55
56         static const char symlinks[] =
57                 "-/proc/kcore\0"     "/dev/core\0"
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
63         NULSTR_FOREACH_PAIR(j, k, symlinks) {
64                 if (j[0] == '-') {
65                         j++;
66
67                         if (access(j, F_OK) < 0)
68                                 continue;
69                 }
70
71                 if (prefix) {
72                         _cleanup_free_ char *link_name = NULL;
73
74                         link_name = strjoin(prefix, "/", k, NULL);
75                         if (!link_name)
76                                 return -ENOMEM;
77
78                         symlink_and_label(j, link_name);
79                 } else
80                         symlink_and_label(j, k);
81         }
82
83         return 0;
84 }