mount-setup.c revision 53f7d807bff9c39e9e565ad2fb20f29b4306af40
0N/A/*-*- Mode: C; c-basic-offset: 8 -*-*/
0N/A
0N/A/***
0N/A This file is part of systemd.
0N/A
0N/A Copyright 2010 Lennart Poettering
0N/A
0N/A systemd is free software; you can redistribute it and/or modify it
0N/A under the terms of the GNU General Public License as published by
0N/A the Free Software Foundation; either version 2 of the License, or
0N/A (at your option) any later version.
0N/A
0N/A systemd is distributed in the hope that it will be useful, but
0N/A WITHOUT ANY WARRANTY; without even the implied warranty of
0N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0N/A General Public License for more details.
0N/A
0N/A You should have received a copy of the GNU General Public License
0N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
0N/A***/
0N/A
0N/A#include <sys/mount.h>
0N/A#include <errno.h>
0N/A#include <sys/stat.h>
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A#include <libgen.h>
0N/A#include <assert.h>
0N/A
0N/A#include "mount-setup.h"
0N/A#include "log.h"
0N/A#include "macro.h"
0N/A#include "util.h"
0N/A
0N/Atypedef struct MountPoint {
0N/A const char *what;
0N/A const char *where;
0N/A const char *type;
0N/A const char *options;
0N/A unsigned long flags;
0N/A bool fatal;
0N/A} MountPoint;
0N/A
0N/Astatic const MountPoint mount_table[] = {
0N/A { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
0N/A { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
0N/A { "devtmps", "/dev", "devtmpfs", "mode=755", MS_NOSUID, true },
0N/A { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
0N/A { "devpts", "/dev/pts", "devpts", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, false },
0N/A { "cgroup", "/cgroup/systemd", "cgroup", "none,name=systemd", MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
0N/A { "debugfs", "/sys/kernel/debug", "debugfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, false },
0N/A { "binfmt_misc", "/proc/sys/fs/binfmt_misc", "binfmt_misc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, false },
0N/A { "mqueue", "/dev/mqueue", "mqueue", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, false },
0N/A};
0N/A
0N/Abool mount_point_is_api(const char *path) {
0N/A unsigned i;
0N/A
0N/A /* Checks if this mount point is considered "API", and hence
0N/A * should be ignored */
0N/A
0N/A for (i = 0; i < ELEMENTSOF(mount_table); i ++)
0N/A if (path_startswith(path, mount_table[i].where))
0N/A return true;
0N/A
0N/A return path_startswith(path, "/cgroup/");
0N/A}
0N/A
0N/Astatic int mount_one(const MountPoint *p) {
0N/A int r;
0N/A
0N/A assert(p);
0N/A
0N/A if ((r = path_is_mount_point(p->where)) < 0)
0N/A return r;
0N/A
0N/A if (r > 0)
0N/A return 0;
0N/A
0N/A /* The access mode here doesn't really matter too much, since
0N/A * the mounted file system will take precedence anyway. */
0N/A mkdir_p(p->where, 0755);
0N/A
0N/A log_debug("Mounting %s to %s of type %s with options %s.",
0N/A p->what,
0N/A p->where,
0N/A p->type,
0N/A strna(p->options));
0N/A
0N/A if (mount(p->what,
0N/A p->where,
0N/A p->type,
0N/A p->flags,
0N/A p->options) < 0) {
0N/A log_error("Failed to mount %s: %s", p->where, strerror(errno));
0N/A return p->fatal ? -errno : 0;
0N/A }
0N/A
0N/A return 0;
0N/A}
0N/A
0N/Astatic int mount_cgroup_controllers(void) {
0N/A int r;
0N/A FILE *f;
0N/A char buf [256];
0N/A
0N/A /* Mount all available cgroup controllers. */
0N/A
0N/A if (!(f = fopen("/proc/cgroups", "re")))
0N/A return -ENOENT;
0N/A
0N/A /* Ignore the header line */
0N/A (void) fgets(buf, sizeof(buf), f);
0N/A
0N/A for (;;) {
0N/A MountPoint p;
0N/A char *controller, *where;
0N/A
0N/A if (fscanf(f, "%ms %*i %*i %*i", &controller) != 1) {
0N/A
0N/A if (feof(f))
0N/A break;
0N/A
0N/A log_error("Failed to parse /proc/cgroups.");
0N/A r = -EIO;
goto finish;
}
if (asprintf(&where, "/cgroup/%s", controller) < 0) {
free(controller);
r = -ENOMEM;
goto finish;
}
zero(p);
p.what = "cgroup";
p.where = where;
p.type = "cgroup";
p.options = controller;
p.flags = MS_NOSUID|MS_NOEXEC|MS_NODEV;
p.fatal = false;
r = mount_one(&p);
free(controller);
free(where);
if (r < 0)
goto finish;
}
r = 0;
finish:
fclose(f);
return r;
}
int mount_setup(void) {
int r;
unsigned i;
for (i = 0; i < ELEMENTSOF(mount_table); i ++)
if ((r = mount_one(mount_table+i)) < 0)
return r;
return mount_cgroup_controllers();
}