chiark / gitweb /
bmfmt: allow passing more than one config file name
[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 #include "set.h"
38 #include "strv.h"
39
40 #ifndef TTY_GID
41 #define TTY_GID 5
42 #endif
43
44 typedef struct MountPoint {
45         const char *what;
46         const char *where;
47         const char *type;
48         const char *options;
49         unsigned long flags;
50         bool fatal;
51 } MountPoint;
52
53 /* The first three entries we might need before SELinux is up. The
54  * other ones we can delay until SELinux is loaded. */
55 #define N_EARLY_MOUNT 3
56
57 static const MountPoint mount_table[] = {
58         { "proc",     "/proc",                  "proc",     NULL,                MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
59         { "sysfs",    "/sys",                   "sysfs",    NULL,                MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
60         { "devtmpfs", "/dev",                   "devtmpfs", "mode=755",          MS_NOSUID,                    true },
61         { "tmpfs",    "/dev/shm",               "tmpfs",    "mode=1777",         MS_NOSUID|MS_NODEV,           true },
62         { "devpts",   "/dev/pts",               "devpts",   "mode=620,gid=" STRINGIFY(TTY_GID), MS_NOSUID|MS_NOEXEC, false },
63         { "tmpfs",    "/run",                   "tmpfs",    "mode=755",          MS_NOSUID|MS_NODEV, true },
64         { "tmpfs",    "/sys/fs/cgroup",         "tmpfs",    "mode=755",          MS_NOSUID|MS_NOEXEC|MS_NODEV, false },
65         { "cgroup",   "/sys/fs/cgroup/systemd", "cgroup",   "none,name=systemd", MS_NOSUID|MS_NOEXEC|MS_NODEV, false },
66 };
67
68 /* These are API file systems that might be mounted by other software,
69  * we just list them here so that we know that we should ignore them */
70
71 static const char * const ignore_paths[] = {
72         "/sys/fs/selinux",
73         "/selinux",
74         "/proc/bus/usb"
75 };
76
77 bool mount_point_is_api(const char *path) {
78         unsigned i;
79
80         /* Checks if this mount point is considered "API", and hence
81          * should be ignored */
82
83         for (i = 0; i < ELEMENTSOF(mount_table); i ++)
84                 if (path_equal(path, mount_table[i].where))
85                         return true;
86
87         return path_startswith(path, "/sys/fs/cgroup/");
88 }
89
90 bool mount_point_ignore(const char *path) {
91         unsigned i;
92
93         for (i = 0; i < ELEMENTSOF(ignore_paths); i++)
94                 if (path_equal(path, ignore_paths[i]))
95                         return true;
96
97         return false;
98 }
99
100 static int mount_one(const MountPoint *p, bool relabel) {
101         int r;
102
103         assert(p);
104
105         /* Relabel first, just in case */
106         if (relabel)
107                 label_fix(p->where, true);
108
109         if ((r = path_is_mount_point(p->where, true)) < 0)
110                 return r;
111
112         if (r > 0)
113                 return 0;
114
115         /* The access mode here doesn't really matter too much, since
116          * the mounted file system will take precedence anyway. */
117         mkdir_p(p->where, 0755);
118
119         log_debug("Mounting %s to %s of type %s with options %s.",
120                   p->what,
121                   p->where,
122                   p->type,
123                   strna(p->options));
124
125         if (mount(p->what,
126                   p->where,
127                   p->type,
128                   p->flags,
129                   p->options) < 0) {
130                 log_error("Failed to mount %s: %s", p->where, strerror(errno));
131                 return p->fatal ? -errno : 0;
132         }
133
134         /* Relabel again, since we now mounted something fresh here */
135         if (relabel)
136                 label_fix(p->where, false);
137
138         return 1;
139 }
140
141 int mount_setup_early(void) {
142         unsigned i;
143         int r = 0;
144
145         assert_cc(N_EARLY_MOUNT <= ELEMENTSOF(mount_table));
146
147         /* Do a minimal mount of /proc and friends to enable the most
148          * basic stuff, such as SELinux */
149         for (i = 0; i < N_EARLY_MOUNT; i ++)  {
150                 int j;
151
152                 j = mount_one(mount_table + i, false);
153                 if (r == 0)
154                         r = j;
155         }
156
157         return r;
158 }
159
160 int mount_cgroup_controllers(char ***join_controllers) {
161         int r;
162         FILE *f;
163         char buf[LINE_MAX];
164         Set *controllers;
165
166         /* Mount all available cgroup controllers that are built into the kernel. */
167
168         f = fopen("/proc/cgroups", "re");
169         if (!f) {
170                 log_error("Failed to enumerate cgroup controllers: %m");
171                 return 0;
172         }
173
174         controllers = set_new(string_hash_func, string_compare_func);
175         if (!controllers) {
176                 r = -ENOMEM;
177                 log_error("Failed to allocate controller set.");
178                 goto finish;
179         }
180
181         /* Ignore the header line */
182         (void) fgets(buf, sizeof(buf), f);
183
184         for (;;) {
185                 char *controller;
186                 int enabled = 0;
187
188                 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
189
190                         if (feof(f))
191                                 break;
192
193                         log_error("Failed to parse /proc/cgroups.");
194                         r = -EIO;
195                         goto finish;
196                 }
197
198                 if (!enabled) {
199                         free(controller);
200                         continue;
201                 }
202
203                 r = set_put(controllers, controller);
204                 if (r < 0) {
205                         log_error("Failed to add controller to set.");
206                         free(controller);
207                         goto finish;
208                 }
209         }
210
211         for (;;) {
212                 MountPoint p;
213                 char *controller, *where, *options;
214                 char ***k = NULL;
215
216                 controller = set_steal_first(controllers);
217                 if (!controller)
218                         break;
219
220                 if (join_controllers)
221                         for (k = join_controllers; *k; k++)
222                                 if (strv_find(*k, controller))
223                                         break;
224
225                 if (k && *k) {
226                         char **i, **j;
227
228                         for (i = *k, j = *k; *i; i++) {
229
230                                 if (!streq(*i, controller)) {
231                                         char *t;
232
233                                         t = set_remove(controllers, *i);
234                                         if (!t) {
235                                                 free(*i);
236                                                 continue;
237                                         }
238                                         free(t);
239                                 }
240
241                                 *(j++) = *i;
242                         }
243
244                         *j = NULL;
245
246                         options = strv_join(*k, ",");
247                         if (!options) {
248                                 log_error("Failed to join options");
249                                 free(controller);
250                                 r = -ENOMEM;
251                                 goto finish;
252                         }
253
254                 } else {
255                         options = controller;
256                         controller = NULL;
257                 }
258
259                 where = strappend("/sys/fs/cgroup/", options);
260                 if (!where) {
261                         log_error("Failed to build path");
262                         free(options);
263                         r = -ENOMEM;
264                         goto finish;
265                 }
266
267                 zero(p);
268                 p.what = "cgroup";
269                 p.where = where;
270                 p.type = "cgroup";
271                 p.options = options;
272                 p.flags = MS_NOSUID|MS_NOEXEC|MS_NODEV;
273                 p.fatal = false;
274
275                 r = mount_one(&p, true);
276                 free(controller);
277                 free(where);
278
279                 if (r < 0) {
280                         free(options);
281                         goto finish;
282                 }
283
284                 if (r > 0 && k && *k) {
285                         char **i;
286
287                         for (i = *k; *i; i++) {
288                                 char *t;
289
290                                 t = strappend("/sys/fs/cgroup/", *i);
291                                 if (!t) {
292                                         log_error("Failed to build path");
293                                         r = -ENOMEM;
294                                         free(options);
295                                         goto finish;
296                                 }
297
298                                 r = symlink(options, t);
299                                 free(t);
300
301                                 if (r < 0 && errno != EEXIST) {
302                                         log_error("Failed to create symlink: %m");
303                                         r = -errno;
304                                         free(options);
305                                         goto finish;
306                                 }
307                         }
308                 }
309
310                 free(options);
311         }
312
313         r = 0;
314
315 finish:
316         set_free_free(controllers);
317
318         fclose(f);
319
320         return r;
321 }
322
323 static int symlink_and_label(const char *old_path, const char *new_path) {
324         int r;
325
326         assert(old_path);
327         assert(new_path);
328
329         if ((r = label_symlinkfile_set(new_path)) < 0)
330                 return r;
331
332         if (symlink(old_path, new_path) < 0)
333                 r = -errno;
334
335         label_file_clear();
336
337         return r;
338 }
339
340 static int nftw_cb(
341                 const char *fpath,
342                 const struct stat *sb,
343                 int tflag,
344                 struct FTW *ftwbuf) {
345
346         /* No need to label /dev twice in a row... */
347         if (_unlikely_(ftwbuf->level == 0))
348                 return FTW_CONTINUE;
349
350         label_fix(fpath, true);
351
352         /* /run/initramfs is static data and big, no need to
353          * dynamically relabel its contents at boot... */
354         if (_unlikely_(ftwbuf->level == 1 &&
355                       tflag == FTW_D &&
356                       streq(fpath, "/run/initramfs")))
357                 return FTW_SKIP_SUBTREE;
358
359         return FTW_CONTINUE;
360 };
361
362 int mount_setup(bool loaded_policy) {
363
364         static const char symlinks[] =
365                 "/proc/kcore\0"      "/dev/core\0"
366                 "/proc/self/fd\0"    "/dev/fd\0"
367                 "/proc/self/fd/0\0"  "/dev/stdin\0"
368                 "/proc/self/fd/1\0"  "/dev/stdout\0"
369                 "/proc/self/fd/2\0"  "/dev/stderr\0";
370
371         static const char relabel[] =
372                 "/run/initramfs/root-fsck\0"
373                 "/run/initramfs/shutdown\0";
374
375         int r;
376         unsigned i;
377         const char *j, *k;
378
379         for (i = 0; i < ELEMENTSOF(mount_table); i ++) {
380                 r = mount_one(mount_table + i, true);
381
382                 if (r < 0)
383                         return r;
384         }
385
386         /* Nodes in devtmpfs and /run need to be manually updated for
387          * the appropriate labels, after mounting. The other virtual
388          * API file systems like /sys and /proc do not need that, they
389          * use the same label for all their files. */
390         if (loaded_policy) {
391                 usec_t before_relabel, after_relabel;
392                 char timespan[FORMAT_TIMESPAN_MAX];
393
394                 before_relabel = now(CLOCK_MONOTONIC);
395
396                 nftw("/dev", nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL);
397                 nftw("/run", nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL);
398
399                 /* Explicitly relabel these */
400                 NULSTR_FOREACH(j, relabel)
401                         label_fix(j, true);
402
403                 after_relabel = now(CLOCK_MONOTONIC);
404
405                 log_info("Relabelled /dev and /run in %s.",
406                          format_timespan(timespan, sizeof(timespan), after_relabel - before_relabel));
407         }
408
409         /* Create a few default symlinks, which are normally created
410          * by udevd, but some scripts might need them before we start
411          * udevd. */
412         NULSTR_FOREACH_PAIR(j, k, symlinks)
413                 symlink_and_label(j, k);
414
415         /* Create a few directories we always want around */
416         label_mkdir("/run/systemd", 0755);
417         label_mkdir("/run/systemd/system", 0755);
418
419         return 0;
420 }