]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/mount-setup.c
journal: PAGE_SIZE is not known on ppc and other archs
[thirdparty/systemd.git] / src / mount-setup.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
8e274523
LP
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>
5c0532d1 29#include <unistd.h>
1829dc9d 30#include <ftw.h>
8e274523
LP
31
32#include "mount-setup.h"
33#include "log.h"
c9af1080
LP
34#include "macro.h"
35#include "util.h"
5275d3c1 36#include "label.h"
0c85a4f3
LP
37#include "set.h"
38#include "strv.h"
8e274523 39
bef2733f
LP
40#ifndef TTY_GID
41#define TTY_GID 5
42#endif
43
ca714c0e
LP
44typedef struct MountPoint {
45 const char *what;
46 const char *where;
47 const char *type;
48 const char *options;
49 unsigned long flags;
2076ca54 50 bool fatal;
ca714c0e
LP
51} MountPoint;
52
4ef31082
LP
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
ca714c0e 57static const MountPoint mount_table[] = {
77d5f105
LP
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 },
501c875b 61 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV, true },
bef2733f 62 { "devpts", "/dev/pts", "devpts", "mode=620,gid=" STRINGIFY(TTY_GID), MS_NOSUID|MS_NOEXEC, false },
fbe092cc 63 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV, true },
e5a53dc7
LP
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 },
8e274523
LP
66};
67
949c6510 68/* These are API file systems that might be mounted by other software,
46ff0ed7 69 * we just list them here so that we know that we should ignore them */
949c6510
LP
70
71static const char * const ignore_paths[] = {
ef9d7dca 72 "/sys/fs/selinux",
949c6510 73 "/selinux",
af49ec2c 74 "/proc/bus/usb"
949c6510
LP
75};
76
dad08730
LP
77bool 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
ca714c0e 83 for (i = 0; i < ELEMENTSOF(mount_table); i ++)
449ddb2d 84 if (path_equal(path, mount_table[i].where))
dad08730
LP
85 return true;
86
57f2a956
KS
87 return path_startswith(path, "/sys/fs/cgroup/");
88}
89
90bool mount_point_ignore(const char *path) {
46ff0ed7 91 unsigned i;
57f2a956 92
949c6510 93 for (i = 0; i < ELEMENTSOF(ignore_paths); i++)
449ddb2d 94 if (path_equal(path, ignore_paths[i]))
949c6510
LP
95 return true;
96
57f2a956 97 return false;
dad08730
LP
98}
99
4ef31082 100static int mount_one(const MountPoint *p, bool relabel) {
8e274523
LP
101 int r;
102
ca714c0e 103 assert(p);
8e274523 104
51b4af2c 105 /* Relabel first, just in case */
4ef31082
LP
106 if (relabel)
107 label_fix(p->where, true);
51b4af2c 108
0c85a4f3 109 if ((r = path_is_mount_point(p->where, true)) < 0)
8e274523
LP
110 return r;
111
112 if (r > 0)
51b4af2c 113 return 0;
8e274523 114
a04f58d6
LP
115 /* The access mode here doesn't really matter too much, since
116 * the mounted file system will take precedence anyway. */
ca714c0e 117 mkdir_p(p->where, 0755);
a04f58d6 118
8e274523 119 log_debug("Mounting %s to %s of type %s with options %s.",
ca714c0e
LP
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));
2076ca54 131 return p->fatal ? -errno : 0;
8e274523
LP
132 }
133
51b4af2c 134 /* Relabel again, since we now mounted something fresh here */
4ef31082
LP
135 if (relabel)
136 label_fix(p->where, false);
5275d3c1 137
0c85a4f3 138 return 1;
8e274523
LP
139}
140
4ef31082
LP
141int 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
0c85a4f3 160int mount_cgroup_controllers(char ***join_controllers) {
2076ca54
LP
161 int r;
162 FILE *f;
20c03b7b 163 char buf[LINE_MAX];
0c85a4f3 164 Set *controllers;
2076ca54 165
670802d4 166 /* Mount all available cgroup controllers that are built into the kernel. */
2076ca54 167
0c85a4f3
LP
168 f = fopen("/proc/cgroups", "re");
169 if (!f) {
016e9849
LP
170 log_error("Failed to enumerate cgroup controllers: %m");
171 return 0;
172 }
2076ca54 173
0c85a4f3
LP
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
2076ca54 181 /* Ignore the header line */
bab45044 182 (void) fgets(buf, sizeof(buf), f);
2076ca54
LP
183
184 for (;;) {
0c85a4f3
LP
185 char *controller;
186 int enabled = 0;
2076ca54 187
16f6682d 188 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
2076ca54
LP
189
190 if (feof(f))
191 break;
192
193 log_error("Failed to parse /proc/cgroups.");
194 r = -EIO;
195 goto finish;
196 }
197
600a328f
LP
198 if (!enabled) {
199 free(controller);
200 continue;
201 }
202
0c85a4f3
LP
203 r = set_put(controllers, controller);
204 if (r < 0) {
205 log_error("Failed to add controller to set.");
2076ca54 206 free(controller);
0c85a4f3
LP
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);
2076ca54
LP
263 r = -ENOMEM;
264 goto finish;
265 }
266
267 zero(p);
268 p.what = "cgroup";
269 p.where = where;
270 p.type = "cgroup";
0c85a4f3 271 p.options = options;
2076ca54
LP
272 p.flags = MS_NOSUID|MS_NOEXEC|MS_NODEV;
273 p.fatal = false;
274
4ef31082 275 r = mount_one(&p, true);
2076ca54
LP
276 free(controller);
277 free(where);
278
0c85a4f3
LP
279 if (r < 0) {
280 free(options);
2076ca54 281 goto finish;
0c85a4f3
LP
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);
2076ca54
LP
311 }
312
313 r = 0;
314
315finish:
0c85a4f3
LP
316 set_free_free(controllers);
317
2076ca54
LP
318 fclose(f);
319
320 return r;
321}
322
5c0532d1
LP
323static 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
1829dc9d
LP
340static int nftw_cb(
341 const char *fpath,
342 const struct stat *sb,
343 int tflag,
344 struct FTW *ftwbuf) {
345
9fe117ea 346 /* No need to label /dev twice in a row... */
edb49778
LP
347 if (_unlikely_(ftwbuf->level == 0))
348 return FTW_CONTINUE;
349
af65c248
LP
350 label_fix(fpath, true);
351
edb49778 352 /* /run/initramfs is static data and big, no need to
af65c248 353 * dynamically relabel its contents at boot... */
edb49778
LP
354 if (_unlikely_(ftwbuf->level == 1 &&
355 tflag == FTW_D &&
356 streq(fpath, "/run/initramfs")))
357 return FTW_SKIP_SUBTREE;
9fe117ea 358
edb49778 359 return FTW_CONTINUE;
1829dc9d
LP
360};
361
0b3325e7 362int mount_setup(bool loaded_policy) {
5c0532d1 363
af65c248 364 static const char symlinks[] =
5c0532d1
LP
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"
34df5a34 369 "/proc/self/fd/2\0" "/dev/stderr\0";
5c0532d1 370
af65c248
LP
371 static const char relabel[] =
372 "/run/initramfs/root-fsck\0"
373 "/run/initramfs/shutdown\0";
374
8e274523 375 int r;
dad08730 376 unsigned i;
5c0532d1 377 const char *j, *k;
8e274523 378
4ef31082
LP
379 for (i = 0; i < ELEMENTSOF(mount_table); i ++) {
380 r = mount_one(mount_table + i, true);
381
382 if (r < 0)
8e274523 383 return r;
4ef31082 384 }
8e274523 385
f1d19aa4
LP
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. */
0b3325e7
LP
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
edb49778
LP
396 nftw("/dev", nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL);
397 nftw("/run", nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL);
0b3325e7 398
af65c248
LP
399 /* Explicitly relabel these */
400 NULSTR_FOREACH(j, relabel)
401 label_fix(j, true);
402
0b3325e7
LP
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));
3bbecb2f 407 }
1829dc9d 408
5c0532d1 409 /* Create a few default symlinks, which are normally created
f1d19aa4 410 * by udevd, but some scripts might need them before we start
5c0532d1 411 * udevd. */
5c0532d1
LP
412 NULSTR_FOREACH_PAIR(j, k, symlinks)
413 symlink_and_label(j, k);
414
b925e726 415 /* Create a few directories we always want around */
af65c248
LP
416 label_mkdir("/run/systemd", 0755);
417 label_mkdir("/run/systemd/system", 0755);
b925e726 418
0c85a4f3 419 return 0;
8e274523 420}