/***
This file is part of systemd.
Copyright 2015 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "alloc-util.h"
#include "cgroup-util.h"
#include "escape.h"
#include "fs-util.h"
#include "label.h"
#include "mkdir.h"
#include "mount-util.h"
#include "nspawn-mount.h"
#include "parse-util.h"
#include "path-util.h"
#include "rm-rf.h"
#include "set.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "user-util.h"
#include "util.h"
assert(l);
assert(n);
assert(t >= 0);
assert(t < _CUSTOM_MOUNT_TYPE_MAX);
if (!c)
return NULL;
*l = c;
ret = *l + *n;
(*n)++;
return ret;
}
unsigned i;
for (i = 0; i < n; i++) {
CustomMount *m = l + i;
free(m->destination);
if (m->work_dir) {
}
}
free(l);
}
int custom_mount_compare(const void *a, const void *b) {
const CustomMount *x = a, *y = b;
int r;
if (r != 0)
return r;
return -1;
return 1;
return 0;
}
const char *p = s;
CustomMount *m;
int r;
assert(l);
assert(n);
if (r < 0)
return r;
if (r == 0)
return -EINVAL;
if (r == 1) {
if (!destination)
return -ENOMEM;
}
if (r == 2 && !isempty(p)) {
if (!opts)
return -ENOMEM;
}
if (!path_is_absolute(source))
return -EINVAL;
if (!path_is_absolute(destination))
return -EINVAL;
m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
if (!m)
return log_oom();
m->destination = destination;
return 0;
}
const char *p = s;
CustomMount *m;
int r;
assert(l);
assert(n);
assert(s);
if (r < 0)
return r;
if (r == 0)
return -EINVAL;
if (isempty(p))
else
if (!opts)
return -ENOMEM;
if (!path_is_absolute(path))
return -EINVAL;
m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
if (!m)
return -ENOMEM;
m->destination = path;
return 0;
}
static int tmpfs_patch_options(
const char *options,
const char *selinux_apifs_context,
char **ret) {
if (options)
else
if (!buf)
return -ENOMEM;
}
#ifdef HAVE_SELINUX
if (selinux_apifs_context) {
char *t;
if (options)
else
if (!t) {
return -ENOMEM;
}
buf = t;
}
#endif
return !!buf;
}
int r;
if (r < 0)
/* /sys might already be mounted as sysfs by the outer child in the
* !netns case. In this case, it's all good. Don't touch it because we
* don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
*/
if (r > 0)
return 0;
if (!from)
return log_oom();
if (!to)
return log_oom();
}
(void) mkdir(x, 0755);
return 0;
}
bool use_userns, bool in_userns,
bool use_netns,
const char *selinux_apifs_context) {
typedef struct MountPoint {
const char *what;
const char *where;
const char *type;
const char *options;
unsigned long flags;
bool fatal;
bool in_userns;
bool use_netns;
} MountPoint;
{ NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, true, true, false }, /* Then, make it r/o */
{ "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, true, false, false },
#ifdef HAVE_SELINUX
{ "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND, false, false, false }, /* Bind mount first */
{ NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, false, false, false }, /* Then, make it r/o */
#endif
};
unsigned k;
int r;
for (k = 0; k < ELEMENTSOF(mount_table); k++) {
const char *o;
continue;
continue;
if (!where)
return log_oom();
if (r < 0 && r != -ENOENT)
/* Skip this entry if it is not a remount. */
if (mount_table[k].what && r > 0)
continue;
if (r < 0) {
if (mount_table[k].fatal)
continue;
}
o = mount_table[k].options;
if (r < 0)
return log_oom();
if (r > 0)
o = options;
}
mount_table[k].type,
mount_table[k].flags,
o) < 0) {
if (mount_table[k].fatal)
}
}
return 0;
}
static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts) {
const char *p = options;
for (;;) {
if (r < 0)
return log_error_errno(r, "Failed to extract mount option: %m");
if (r == 0)
break;
else {
return -EINVAL;
}
}
*mount_flags = flags;
/* in the future mount_opts will hold string options for mount(2) */
*mount_opts = opts;
return 0;
}
const char *where;
int r;
assert(m);
if (m->options) {
if (r < 0)
return r;
}
return -EINVAL;
}
return -EINVAL;
}
if (r < 0)
} else {
}
/* Create the mount point. Any non-directory file can be
* mounted on any non-directory file (regular, fifo, socket,
* char, block).
*/
else
if (r < 0 && r != -EEXIST)
if (m->read_only) {
r = bind_remount_recursive(where, true);
if (r < 0)
return log_error_errno(r, "Read-only bind mount failed: %m");
}
return 0;
}
static int mount_tmpfs(
const char *dest,
CustomMount *m,
const char *selinux_apifs_context) {
int r;
assert(m);
if (r < 0 && r != -EEXIST)
if (r < 0)
return log_oom();
return 0;
}
if (!sv)
return NULL;
return NULL;
}
int r;
assert(m);
if (r < 0 && r != -EEXIST)
if (!lower)
return log_oom();
if (m->read_only) {
if (!escaped_source)
return log_oom();
} else {
if (!escaped_source)
return log_oom();
if (!escaped_work_dir)
return log_oom();
options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
}
return 0;
}
int mount_custom(
const char *dest,
CustomMount *mounts, unsigned n,
const char *selinux_apifs_context) {
unsigned i;
int r;
for (i = 0; i < n; i++) {
CustomMount *m = mounts + i;
switch (m->type) {
case CUSTOM_MOUNT_BIND:
r = mount_bind(dest, m);
break;
case CUSTOM_MOUNT_TMPFS:
break;
case CUSTOM_MOUNT_OVERLAY:
r = mount_overlay(dest, m);
break;
default:
assert_not_reached("Unknown custom mount type");
}
if (r < 0)
return r;
}
return 0;
}
static int mount_legacy_cgroup_hierarchy(const char *dest, const char *controller, const char *hierarchy, bool read_only) {
char *to;
int r;
r = path_is_mount_point(to, 0);
if (r < 0 && r != -ENOENT)
if (r > 0)
return 0;
/* The superblock mount options of the mount point need to be
* identical to the hosts', and hence writable... */
/* ... hence let's only make the bind mount read-only, not the
* superblock. */
if (read_only) {
}
return 1;
}
static int mount_legacy_cgroups(
const char *dest,
const char *selinux_apifs_context) {
const char *cgroup_root;
int r;
if (r < 0)
return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
if (r == 0) {
if (r < 0)
return log_oom();
}
if (cg_unified() > 0)
goto skip_controllers;
if (!controllers)
return log_oom();
if (r < 0)
return log_error_errno(r, "Failed to determine cgroup controllers: %m");
for (;;) {
if (!controller)
break;
if (!origin)
return log_oom();
if (r == -EINVAL) {
/* Not a symbolic link, but directly a single cgroup hierarchy */
if (r < 0)
return r;
} else if (r < 0)
else {
if (!target)
return log_oom();
/* A symbolic link, a combination of controllers in one hierarchy */
if (!filename_is_valid(combined)) {
continue;
}
if (r < 0)
return r;
if (r == -EINVAL) {
log_error("Invalid existing symlink for combined hierarchy");
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m");
}
}
if (r < 0)
return r;
if (mount(NULL, cgroup_root, NULL, MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755") < 0)
return 0;
}
const char *p;
int r;
(void) mkdir_p(p, 0755);
r = path_is_mount_point(p, AT_SYMLINK_FOLLOW);
if (r < 0)
return log_error_errno(r, "Failed to determine if %s is mounted already: %m", p);
if (r > 0) {
return 0;
return log_error_errno(errno, "Failed to determine if mount point %s contains the unified cgroup hierarchy: %m", p);
log_error("%s is already mounted but not a unified cgroup hierarchy. Refusing.", p);
return -EINVAL;
}
return 0;
}
int mount_cgroups(
const char *dest,
bool unified_requested,
const char *selinux_apifs_context) {
if (unified_requested)
return mount_unified_cgroups(dest);
else
}
const char *dest,
bool unified_requested) {
int r;
if (r < 0)
return log_error_errno(r, "Failed to determine our own cgroup path: %m");
/* If we are living in the top-level, then there's nothing to do... */
return 0;
if (unified_requested) {
} else {
}
/* Make our own cgroup a (writable) bind mount */
/* And then remount the systemd cgroup root read-only */
if (mount(NULL, systemd_root, NULL, MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL) < 0)
return 0;
}
int setup_volatile_state(
const char *directory,
const char *selinux_apifs_context) {
const char *p, *options;
int r;
if (mode != VOLATILE_STATE)
return 0;
/* --volatile=state means we simply overmount /var
with a tmpfs, and the rest read-only. */
r = bind_remount_recursive(directory, true);
if (r < 0)
r = mkdir(p, 0755);
options = "mode=755";
if (r < 0)
return log_oom();
if (r > 0)
return 0;
}
int setup_volatile(
const char *directory,
const char *selinux_apifs_context) {
const char *f, *t, *options;
int r;
if (mode != VOLATILE_YES)
return 0;
/* --volatile=yes means we mount a tmpfs to the root dir, and
the original /usr to use inside it, and that read-only. */
options = "mode=755";
if (r < 0)
return log_oom();
if (r > 0)
goto fail;
}
tmpfs_mounted = true;
r = mkdir(t, 0755);
goto fail;
}
goto fail;
}
bind_mounted = true;
r = bind_remount_recursive(t, true);
if (r < 0) {
log_error_errno(r, "Failed to remount %s read-only: %m", t);
goto fail;
}
goto fail;
}
return 0;
fail:
if (bind_mounted)
(void) umount(t);
if (tmpfs_mounted)
return r;
}
int b;
if (isempty(s))
return _VOLATILE_MODE_INVALID;
b = parse_boolean(s);
if (b > 0)
return VOLATILE_YES;
if (b == 0)
return VOLATILE_NO;
if (streq(s, "state"))
return VOLATILE_STATE;
return _VOLATILE_MODE_INVALID;
}