diff --git a/meson.build b/meson.build index b595cec8..77437a51 100644 --- a/meson.build +++ b/meson.build @@ -57,7 +57,7 @@ lxcmandir = join_paths(datadir, 'man') conf.set_quoted('BINDIR', bindir) conf.set_quoted('LIBDIR', libdir) conf.set_quoted('LOCALSTATEDIR', localstatedir) -conf.set_quoted('RUNTIME_PATH', runtimepath) +conf.set_quoted('DEFAULT_RUNTIME_PATH', runtimepath) conf.set_quoted('SYSCONFDIR', sysconfdir) conf.set_quoted('LXCCONFDIR', lxcconfdir) diff --git a/src/bindings.c b/src/bindings.c index 27c08c38..310be257 100644 --- a/src/bindings.c +++ b/src/bindings.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -40,15 +41,30 @@ #include "syscall_numbers.h" #include "utils.h" +/* directory under which we mount the controllers - /run/lxcfs/controllers */ +#define BASEDIR "/lxcfs/controllers" +#define ROOTDIR "/lxcfs/root" + static bool can_use_pidfd; static bool can_use_swap; static bool can_use_sys_cpu; static bool has_versioned_opts; static bool memory_is_cgroupv2; static __u32 host_personality; +static char runtime_path[PATH_MAX] = DEFAULT_RUNTIME_PATH; + static volatile sig_atomic_t reload_successful; + +static char* get_base_dir(void) { + return must_make_path(runtime_path, BASEDIR, NULL); +} + +static char *get_root_dir(void) { + return must_make_path(runtime_path, ROOTDIR, NULL); +} + bool liblxcfs_functional(void) { return reload_successful != 0; @@ -580,8 +596,9 @@ pid_t lookup_initpid_in_store(pid_t pid) static bool umount_if_mounted(void) { - if (umount2(BASEDIR, MNT_DETACH) < 0 && errno != EINVAL) { - lxcfs_error("Failed to unmount %s: %s.\n", BASEDIR, strerror(errno)); + __do_free char *base_dir = get_base_dir(); + if (umount2(base_dir, MNT_DETACH) < 0 && errno != EINVAL) { + lxcfs_error("Failed to unmount %s: %s.\n", base_dir, strerror(errno)); return false; } return true; @@ -639,13 +656,14 @@ static bool is_on_ramfs(void) static int pivot_enter(void) { __do_close int oldroot = -EBADF, newroot = -EBADF; + __do_free char *root_dir = get_root_dir(); oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); if (oldroot < 0) return log_error_errno(-1, errno, "Failed to open old root for fchdir"); - newroot = open(ROOTDIR, O_DIRECTORY | O_RDONLY | O_CLOEXEC); + newroot = open(root_dir, O_DIRECTORY | O_RDONLY | O_CLOEXEC); if (newroot < 0) return log_error_errno(-1, errno, "Failed to open new root for fchdir"); @@ -654,7 +672,7 @@ static int pivot_enter(void) if (fchdir(newroot) < 0) return log_error_errno(-1, errno, "Failed to change directory to new rootfs: %s", - ROOTDIR); + root_dir); /* pivot_root into our new root fs */ if (pivot_root(".", ".") < 0) @@ -681,8 +699,10 @@ static int pivot_enter(void) static int chroot_enter(void) { - if (mount(ROOTDIR, "/", NULL, MS_REC | MS_BIND, NULL)) { - lxcfs_error("Failed to recursively bind-mount %s into /.", ROOTDIR); + __do_free char *root_dir = get_root_dir(); + + if (mount(root_dir, "/", NULL, MS_REC | MS_BIND, NULL)) { + lxcfs_error("Failed to recursively bind-mount %s into /.", root_dir); return -1; } @@ -725,23 +745,28 @@ static int permute_and_enter(void) /* Prepare our new clean root. */ static int permute_prepare(void) { - if (mkdir(ROOTDIR, 0700) < 0 && errno != EEXIST) { + __do_free char *base_dir = get_base_dir(); + __do_free char *root_dir = get_root_dir(); + __do_free char *new_runtime = must_make_path(root_dir, runtime_path, NULL); + __do_free char *new_base_dir = must_make_path(root_dir, base_dir, NULL); + + if (mkdir(root_dir, 0700) < 0 && errno != EEXIST) { lxcfs_error("%s\n", "Failed to create directory for new root."); return -1; } - if (mount("/", ROOTDIR, NULL, MS_BIND, 0) < 0) { + if (mount("/", root_dir, NULL, MS_BIND, 0) < 0) { lxcfs_error("Failed to bind-mount / for new root: %s.\n", strerror(errno)); return -1; } - if (mount(RUNTIME_PATH, ROOTDIR RUNTIME_PATH, NULL, MS_BIND, 0) < 0) { + if (mount(runtime_path, new_runtime, NULL, MS_BIND, 0) < 0) { lxcfs_error("Failed to bind-mount /run into new root: %s.\n", strerror(errno)); return -1; } - if (mount(BASEDIR, ROOTDIR BASEDIR, NULL, MS_REC | MS_MOVE, 0) < 0) { - printf("Failed to move " BASEDIR " into new root: %s.\n", strerror(errno)); + if (mount(base_dir, new_base_dir, NULL, MS_REC | MS_MOVE, 0) < 0) { + printf("Failed to move %s into new root: %s.\n", base_dir, strerror(errno)); return -1; } @@ -764,7 +789,9 @@ static bool permute_root(void) static bool cgfs_prepare_mounts(void) { - if (!mkdir_p(BASEDIR, 0700)) { + __do_free char *base_dir = get_base_dir(); + + if (!mkdir_p(base_dir, 0700)) { lxcfs_error("%s\n", "Failed to create lxcfs cgroup mountpoint."); return false; } @@ -790,7 +817,7 @@ static bool cgfs_prepare_mounts(void) return false; } - if (mount("tmpfs", BASEDIR, "tmpfs", 0, "size=100000,mode=700") < 0) { + if (mount("tmpfs", base_dir, "tmpfs", 0, "size=100000,mode=700") < 0) { lxcfs_error("%s\n", "Failed to mount tmpfs over lxcfs cgroup mountpoint."); return false; } @@ -800,14 +827,17 @@ static bool cgfs_prepare_mounts(void) static bool cgfs_mount_hierarchies(void) { - if (!mkdir_p(BASEDIR DEFAULT_CGROUP_MOUNTPOINT, 0755)) + __do_free char *base_dir = get_base_dir(); + __do_free char *base_dir_cgroup_mount = must_make_path(base_dir, DEFAULT_CGROUP_MOUNTPOINT, NULL); + + if (!mkdir_p(base_dir_cgroup_mount, 0755)) return false; - if (!cgroup_ops->mount(cgroup_ops, BASEDIR)) + if (!cgroup_ops->mount(cgroup_ops, base_dir)) return false; for (struct hierarchy **h = cgroup_ops->hierarchies; h && *h; h++) { - __do_free char *path = must_make_path(BASEDIR, (*h)->mountpoint, NULL); + __do_free char *path = must_make_path(base_dir, (*h)->mountpoint, NULL); (*h)->fd = open(path, O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW); if ((*h)->fd < 0) return false; diff --git a/src/bindings.h b/src/bindings.h index 617179df..ba55a8d4 100644 --- a/src/bindings.h +++ b/src/bindings.h @@ -23,10 +23,6 @@ #include "proc_loadavg.h" #include "sysfs_fuse.h" -/* directory under which we mount the controllers - /run/lxcfs/controllers */ -#define BASEDIR RUNTIME_PATH "/lxcfs/controllers" -#define ROOTDIR RUNTIME_PATH "/lxcfs/root" - /* Maximum number for 64 bit integer is a string with 21 digits: 2^64 - 1 = 21 */ #define LXCFS_NUMSTRLEN64 21 diff --git a/src/lxcfs.c b/src/lxcfs.c index cb0739cf..c9db6221 100644 --- a/src/lxcfs.c +++ b/src/lxcfs.c @@ -1185,7 +1185,7 @@ static void usage(void) lxcfs_info(" -l, --enable-loadavg Enable loadavg virtualization"); lxcfs_info(" -o Options to pass directly through fuse"); lxcfs_info(" -p, --pidfile=FILE Path to use for storing lxcfs pid"); - lxcfs_info(" Default pidfile is %s/lxcfs.pid", RUNTIME_PATH); + lxcfs_info(" Default pidfile is %s/lxcfs.pid", DEFAULT_RUNTIME_PATH); lxcfs_info(" -u, --disable-swap Disable swap virtualization"); lxcfs_info(" -v, --version Print lxcfs version"); lxcfs_info(" --enable-cfs Enable CPU virtualization via CPU shares"); @@ -1282,7 +1282,7 @@ int main(int argc, char *argv[]) int pidfile_fd = -EBADF; int ret = EXIT_FAILURE; char *pidfile = NULL, *token = NULL; - char pidfile_buf[STRLITERALLEN(RUNTIME_PATH) + STRLITERALLEN("/lxcfs.pid") + 1] = {}; + char pidfile_buf[STRLITERALLEN(DEFAULT_RUNTIME_PATH) + STRLITERALLEN("/lxcfs.pid") + 1] = {}; bool debug = false, foreground = false; #if !HAVE_FUSE3 bool nonempty = false; @@ -1474,7 +1474,7 @@ int main(int argc, char *argv[]) #endif if (!pidfile) { - snprintf(pidfile_buf, sizeof(pidfile_buf), "%s/lxcfs.pid", RUNTIME_PATH); + snprintf(pidfile_buf, sizeof(pidfile_buf), "%s/lxcfs.pid", DEFAULT_RUNTIME_PATH); pidfile = pidfile_buf; } diff --git a/tests/main.sh.in b/tests/main.sh.in index 656bb6a0..c6223b68 100755 --- a/tests/main.sh.in +++ b/tests/main.sh.in @@ -49,7 +49,7 @@ if [ -x ${lxcfs} ]; then LXCFSPID=$! else UNSHARE=0 - LXCFSPID=$(cat "{{RUNTIME_PATH}}/lxcfs.pid") + LXCFSPID=$(cat "{{DEFAULT_RUNTIME_PATH}}/lxcfs.pid") echo "=> Re-using host lxcfs" rmdir $LXCFSDIR export LXCFSDIR=/var/lib/lxcfs