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