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