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