chiark / gitweb /
mount-setup: mount all cgroup controllers by default but make it non-fatal
[elogind.git] / mount-setup.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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
30 #include "mount-setup.h"
31 #include "log.h"
32 #include "macro.h"
33 #include "util.h"
34
35 typedef struct MountPoint {
36         const char *what;
37         const char *where;
38         const char *type;
39         const char *options;
40         unsigned long flags;
41         bool fatal;
42 } MountPoint;
43
44 static const MountPoint mount_table[] = {
45         { "proc",    "/proc",             "proc",     NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
46         { "sysfs",   "/sys",              "sysfs",    NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
47         { "devtmps", "/dev",              "devtmpfs", "mode=755",  MS_NOSUID,                    true },
48         { "tmpfs",   "/dev/shm",          "tmpfs",    "mode=1777", MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
49         { "devpts",  "/dev/pts",          "devpts",   NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV, false },
50         { "cgroup",  "/cgroup/debug",     "cgroup",   "debug",     MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
51         { "debugfs", "/sys/kernel/debug", "debugfs",  NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV, false }
52 };
53
54 bool mount_point_is_api(const char *path) {
55         unsigned i;
56
57         /* Checks if this mount point is considered "API", and hence
58          * should be ignored */
59
60         for (i = 0; i < ELEMENTSOF(mount_table); i ++)
61                 if (path_startswith(path, mount_table[i].where))
62                         return true;
63
64         return false;
65 }
66
67 static int is_mount_point(const char *t) {
68         struct stat a, b;
69         char *copy;
70
71         if (lstat(t, &a) < 0) {
72
73                 if (errno == ENOENT)
74                         return 0;
75
76                 return -errno;
77         }
78
79         if (!(copy = strdup(t)))
80                 return -ENOMEM;
81
82         if (lstat(dirname(copy), &b) < 0) {
83                 free(copy);
84                 return -errno;
85         }
86
87         free(copy);
88
89         return a.st_dev != b.st_dev;
90 }
91
92 static int mount_one(const MountPoint *p) {
93         int r;
94
95         assert(p);
96
97         if ((r = is_mount_point(p->where)) < 0)
98                 return r;
99
100         if (r > 0)
101                 return 0;
102
103         /* The access mode here doesn't really matter too much, since
104          * the mounted file system will take precedence anyway. */
105         mkdir_p(p->where, 0755);
106
107         log_debug("Mounting %s to %s of type %s with options %s.",
108                   p->what,
109                   p->where,
110                   p->type,
111                   strna(p->options));
112
113         if (mount(p->what,
114                   p->where,
115                   p->type,
116                   p->flags,
117                   p->options) < 0) {
118                 log_error("Failed to mount %s: %s", p->where, strerror(errno));
119                 return p->fatal ? -errno : 0;
120         }
121
122         return 0;
123 }
124
125 static int mount_cgroup_controllers(void) {
126         int r;
127         FILE *f;
128         char buf [256];
129
130         /* Mount all available cgroup controllers. */
131
132         if (!(f = fopen("/proc/cgroups", "re")))
133                 return -ENOENT;
134
135         /* Ignore the header line */
136         fgets(buf, sizeof(buf), f);
137
138         for (;;) {
139                 MountPoint p;
140                 char *controller, *where;
141
142                 if (fscanf(f, "%ms %*i %*i %*i", &controller) != 1) {
143
144                         if (feof(f))
145                                 break;
146
147                         log_error("Failed to parse /proc/cgroups.");
148                         r = -EIO;
149                         goto finish;
150                 }
151
152                 if (asprintf(&where, "/cgroup/%s", controller) < 0) {
153                         free(controller);
154                         r = -ENOMEM;
155                         goto finish;
156                 }
157
158                 zero(p);
159                 p.what = "cgroup";
160                 p.where = where;
161                 p.type = "cgroup";
162                 p.options = controller;
163                 p.flags = MS_NOSUID|MS_NOEXEC|MS_NODEV;
164                 p.fatal = false;
165
166                 r = mount_one(&p);
167                 free(controller);
168                 free(where);
169
170                 if (r < 0)
171                         goto finish;
172         }
173
174         r = 0;
175
176 finish:
177         fclose(f);
178
179         return r;
180 }
181
182 int mount_setup(void) {
183         int r;
184         unsigned i;
185
186         for (i = 0; i < ELEMENTSOF(mount_table); i ++)
187                 if ((r = mount_one(mount_table+i)) < 0)
188                         return r;
189
190         return mount_cgroup_controllers();
191 }