chiark / gitweb /
84a729ebf21eaf638e7d7f167ea97a0ac0724f91
[elogind.git] / src / mount-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 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/mount.h>
23 #include <errno.h>
24 #include <sys/stat.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <libgen.h>
28 #include <assert.h>
29 #include <unistd.h>
30 #include <ftw.h>
31
32 #include "mount-setup.h"
33 #include "log.h"
34 #include "macro.h"
35 #include "util.h"
36 #include "label.h"
37
38 typedef struct MountPoint {
39         const char *what;
40         const char *where;
41         const char *type;
42         const char *options;
43         unsigned long flags;
44         bool fatal;
45 } MountPoint;
46
47 static const MountPoint mount_table[] = {
48         { "proc",     "/proc",                  "proc",     NULL,                MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
49         { "sysfs",    "/sys",                   "sysfs",    NULL,                MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
50         { "devtmpfs", "/dev",                   "devtmpfs", "mode=755",          MS_NOSUID,                    true },
51         { "tmpfs",    "/dev/shm",               "tmpfs",    "mode=1777",         MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
52         { "devpts",   "/dev/pts",               "devpts",   NULL,                MS_NOSUID|MS_NOEXEC,          false },
53         { "tmpfs",    "/sys/fs/cgroup",         "tmpfs",    "mode=755",          MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
54         { "cgroup",   "/sys/fs/cgroup/systemd", "cgroup",   "none,name=systemd", MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
55 };
56
57 /* These are API file systems that might be mounted by other software,
58  * we just list them here so that we know that we should igore them */
59
60 static const char * const ignore_paths[] = {
61         "/selinux",
62         "/proc/bus/usb",
63         "/var/lib/nfs/rpc_pipefs",
64         "/proc/fs/nfsd"
65 };
66
67 bool mount_point_is_api(const char *path) {
68         unsigned i;
69
70         /* Checks if this mount point is considered "API", and hence
71          * should be ignored */
72
73         for (i = 0; i < ELEMENTSOF(mount_table); i ++)
74                 if (path_equal(path, mount_table[i].where))
75                         return true;
76
77         for (i = 0; i < ELEMENTSOF(ignore_paths); i++)
78                 if (path_equal(path, ignore_paths[i]))
79                         return true;
80
81         return path_startswith(path, "/sys/fs/cgroup/");
82 }
83
84 static int mount_one(const MountPoint *p) {
85         int r;
86
87         assert(p);
88
89         if ((r = path_is_mount_point(p->where)) < 0)
90                 return r;
91
92         if (r > 0)
93                 return 0;
94
95         /* The access mode here doesn't really matter too much, since
96          * the mounted file system will take precedence anyway. */
97         mkdir_p(p->where, 0755);
98
99         log_debug("Mounting %s to %s of type %s with options %s.",
100                   p->what,
101                   p->where,
102                   p->type,
103                   strna(p->options));
104
105         if (mount(p->what,
106                   p->where,
107                   p->type,
108                   p->flags,
109                   p->options) < 0) {
110                 log_error("Failed to mount %s: %s", p->where, strerror(errno));
111                 return p->fatal ? -errno : 0;
112         }
113
114         label_fix(p->where);
115
116         return 0;
117 }
118
119 static int mount_cgroup_controllers(void) {
120         int r;
121         FILE *f;
122         char buf [256];
123
124         /* Mount all available cgroup controllers that are built into the kernel. */
125
126         if (!(f = fopen("/proc/cgroups", "re")))
127                 return -ENOENT;
128
129         /* Ignore the header line */
130         (void) fgets(buf, sizeof(buf), f);
131
132         for (;;) {
133                 MountPoint p;
134                 char *controller, *where;
135
136                 if (fscanf(f, "%ms %*i %*i %*i", &controller) != 1) {
137
138                         if (feof(f))
139                                 break;
140
141                         log_error("Failed to parse /proc/cgroups.");
142                         r = -EIO;
143                         goto finish;
144                 }
145
146                 if (asprintf(&where, "/sys/fs/cgroup/%s", controller) < 0) {
147                         free(controller);
148                         r = -ENOMEM;
149                         goto finish;
150                 }
151
152                 zero(p);
153                 p.what = "cgroup";
154                 p.where = where;
155                 p.type = "cgroup";
156                 p.options = controller;
157                 p.flags = MS_NOSUID|MS_NOEXEC|MS_NODEV;
158                 p.fatal = false;
159
160                 r = mount_one(&p);
161                 free(controller);
162                 free(where);
163
164                 if (r < 0)
165                         goto finish;
166         }
167
168         r = 0;
169
170 finish:
171         fclose(f);
172
173         return r;
174 }
175
176 static int symlink_and_label(const char *old_path, const char *new_path) {
177         int r;
178
179         assert(old_path);
180         assert(new_path);
181
182         if ((r = label_symlinkfile_set(new_path)) < 0)
183                 return r;
184
185         if (symlink(old_path, new_path) < 0)
186                 r = -errno;
187
188         label_file_clear();
189
190         return r;
191 }
192
193 static int nftw_cb(
194                 const char *fpath,
195                 const struct stat *sb,
196                 int tflag,
197                 struct FTW *ftwbuf) {
198
199         label_fix(fpath);
200         return 0;
201 };
202
203 int mount_setup(void) {
204
205         const char *symlinks =
206                 "/proc/kcore\0"      "/dev/core\0"
207                 "/proc/self/fd\0"    "/dev/fd\0"
208                 "/proc/self/fd/0\0"  "/dev/stdin\0"
209                 "/proc/self/fd/1\0"  "/dev/stdout\0"
210                 "/proc/self/fd/2\0"  "/dev/stderr\0"
211                 "\0";
212
213         int r;
214         unsigned i;
215         const char *j, *k;
216
217         for (i = 0; i < ELEMENTSOF(mount_table); i ++)
218                 if ((r = mount_one(mount_table+i)) < 0)
219                         return r;
220
221         /* Nodes in devtmpfs need to be manually updated for the
222          * appropriate labels, after mounting. The other virtual API
223          * file systems do not need. */
224
225         if (unlink("/dev/.systemd/relabel-devtmpfs") >= 0)
226                 nftw("/dev", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
227
228         /* Create a few default symlinks, which are normally created
229          * bei udevd, but some scripts might need them before we start
230          * udevd. */
231
232         NULSTR_FOREACH_PAIR(j, k, symlinks)
233                 symlink_and_label(j, k);
234
235         return mount_cgroup_controllers();
236 }