chiark / gitweb /
unit: distuingish mandatory from triggering conditions
[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 #ifndef TTY_GID
39 #define TTY_GID 5
40 #endif
41
42 typedef struct MountPoint {
43         const char *what;
44         const char *where;
45         const char *type;
46         const char *options;
47         unsigned long flags;
48         bool fatal;
49 } MountPoint;
50
51 static const MountPoint mount_table[] = {
52         { "proc",     "/proc",                  "proc",     NULL,                MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
53         { "sysfs",    "/sys",                   "sysfs",    NULL,                MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
54         { "devtmpfs", "/dev",                   "devtmpfs", "mode=755",          MS_NOSUID,                    true },
55         { "tmpfs",    "/dev/shm",               "tmpfs",    "mode=1777",         MS_NOSUID|MS_NODEV,           true },
56         { "devpts",   "/dev/pts",               "devpts",   "mode=620,gid=" STRINGIFY(TTY_GID), MS_NOSUID|MS_NOEXEC, false },
57         { "tmpfs",    "/sys/fs/cgroup",         "tmpfs",    "mode=755",          MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
58         { "cgroup",   "/sys/fs/cgroup/systemd", "cgroup",   "none,name=systemd", MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
59 };
60
61 /* These are API file systems that might be mounted by other software,
62  * we just list them here so that we know that we should ignore them */
63
64 static const char * const ignore_paths[] = {
65         "/selinux",
66         "/proc/bus/usb",
67         "/var/lib/nfs/rpc_pipefs",
68         "/proc/fs/nfsd"
69 };
70
71 bool mount_point_is_api(const char *path) {
72         unsigned i;
73
74         /* Checks if this mount point is considered "API", and hence
75          * should be ignored */
76
77         for (i = 0; i < ELEMENTSOF(mount_table); i ++)
78                 if (path_equal(path, mount_table[i].where))
79                         return true;
80
81         return path_startswith(path, "/sys/fs/cgroup/");
82 }
83
84 bool mount_point_ignore(const char *path) {
85         unsigned i;
86
87         for (i = 0; i < ELEMENTSOF(ignore_paths); i++)
88                 if (path_equal(path, ignore_paths[i]))
89                         return true;
90
91         return false;
92 }
93
94 static int mount_one(const MountPoint *p) {
95         int r;
96
97         assert(p);
98
99         if ((r = path_is_mount_point(p->where)) < 0)
100                 return r;
101
102         if (r > 0)
103                 return 0;
104
105         /* The access mode here doesn't really matter too much, since
106          * the mounted file system will take precedence anyway. */
107         mkdir_p(p->where, 0755);
108
109         log_debug("Mounting %s to %s of type %s with options %s.",
110                   p->what,
111                   p->where,
112                   p->type,
113                   strna(p->options));
114
115         if (mount(p->what,
116                   p->where,
117                   p->type,
118                   p->flags,
119                   p->options) < 0) {
120                 log_error("Failed to mount %s: %s", p->where, strerror(errno));
121                 return p->fatal ? -errno : 0;
122         }
123
124         label_fix(p->where, false);
125
126         return 0;
127 }
128
129 static int mount_cgroup_controllers(void) {
130         int r;
131         FILE *f;
132         char buf [256];
133
134         /* Mount all available cgroup controllers that are built into the kernel. */
135
136         if (!(f = fopen("/proc/cgroups", "re")))
137                 return -ENOENT;
138
139         /* Ignore the header line */
140         (void) fgets(buf, sizeof(buf), f);
141
142         for (;;) {
143                 MountPoint p;
144                 char *controller, *where;
145                 int enabled = false;
146
147                 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
148
149                         if (feof(f))
150                                 break;
151
152                         log_error("Failed to parse /proc/cgroups.");
153                         r = -EIO;
154                         goto finish;
155                 }
156
157                 if (!enabled) {
158                         free(controller);
159                         continue;
160                 }
161
162                 if (asprintf(&where, "/sys/fs/cgroup/%s", controller) < 0) {
163                         free(controller);
164                         r = -ENOMEM;
165                         goto finish;
166                 }
167
168                 zero(p);
169                 p.what = "cgroup";
170                 p.where = where;
171                 p.type = "cgroup";
172                 p.options = controller;
173                 p.flags = MS_NOSUID|MS_NOEXEC|MS_NODEV;
174                 p.fatal = false;
175
176                 r = mount_one(&p);
177                 free(controller);
178                 free(where);
179
180                 if (r < 0)
181                         goto finish;
182         }
183
184         r = 0;
185
186 finish:
187         fclose(f);
188
189         return r;
190 }
191
192 static int symlink_and_label(const char *old_path, const char *new_path) {
193         int r;
194
195         assert(old_path);
196         assert(new_path);
197
198         if ((r = label_symlinkfile_set(new_path)) < 0)
199                 return r;
200
201         if (symlink(old_path, new_path) < 0)
202                 r = -errno;
203
204         label_file_clear();
205
206         return r;
207 }
208
209 static int nftw_cb(
210                 const char *fpath,
211                 const struct stat *sb,
212                 int tflag,
213                 struct FTW *ftwbuf) {
214
215         /* No need to label /dev twice in a row... */
216         if (ftwbuf->level == 0)
217                 return 0;
218
219         label_fix(fpath, true);
220         return 0;
221 };
222
223 int mount_setup(void) {
224
225         const char symlinks[] =
226                 "/proc/kcore\0"      "/dev/core\0"
227                 "/proc/self/fd\0"    "/dev/fd\0"
228                 "/proc/self/fd/0\0"  "/dev/stdin\0"
229                 "/proc/self/fd/1\0"  "/dev/stdout\0"
230                 "/proc/self/fd/2\0"  "/dev/stderr\0"
231                 "\0";
232
233         int r;
234         unsigned i;
235         const char *j, *k;
236
237         for (i = 0; i < ELEMENTSOF(mount_table); i ++)
238                 if ((r = mount_one(mount_table+i)) < 0)
239                         return r;
240
241         /* Nodes in devtmpfs need to be manually updated for the
242          * appropriate labels, after mounting. The other virtual API
243          * file systems do not need. */
244
245         if (unlink("/dev/.systemd/relabel-devtmpfs") >= 0)
246                 nftw("/dev", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
247
248         /* Create a few default symlinks, which are normally created
249          * bei udevd, but some scripts might need them before we start
250          * udevd. */
251
252         NULSTR_FOREACH_PAIR(j, k, symlinks)
253                 symlink_and_label(j, k);
254
255         return mount_cgroup_controllers();
256 }