X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fswitch-root.c;h=b12189cd10c7a195f98de5e0eaea9d4599f119fe;hb=d896ac2d2fbce41a0b11a0618a685adeaf18b8fe;hp=c5b635d17d8efe537f5fba319163380bde84ae18;hpb=56f64d95763a799ba4475daf44d8e9f72a1bd474;p=elogind.git diff --git a/src/shared/switch-root.c b/src/shared/switch-root.c index c5b635d17..b12189cd1 100644 --- a/src/shared/switch-root.c +++ b/src/shared/switch-root.c @@ -29,10 +29,11 @@ #include "util.h" #include "path-util.h" -#include "switch-root.h" #include "mkdir.h" +#include "rm-rf.h" #include "base-filesystem.h" #include "missing.h" +#include "switch-root.h" int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, unsigned long mountflags) { @@ -51,15 +52,13 @@ int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, if (path_equal(new_root, "/")) return 0; - temporary_old_root = strappenda(new_root, oldroot); + temporary_old_root = strjoina(new_root, oldroot); mkdir_p_label(temporary_old_root, 0755); old_root_remove = in_initrd(); - if (stat(new_root, &new_root_stat) < 0) { - log_error_errno(errno, "Failed to stat directory %s: %m", new_root); - return -errno; - } + if (stat(new_root, &new_root_stat) < 0) + return log_error_errno(errno, "Failed to stat directory %s: %m", new_root); /* Work-around for kernel design: the kernel refuses switching * root if any file systems are mounted MS_SHARED. Hence @@ -74,7 +73,6 @@ int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, struct stat sb; snprintf(new_mount, sizeof(new_mount), "%s%s", new_root, i); - char_array_0(new_mount); mkdir_p_label(new_mount, 0755); @@ -107,12 +105,10 @@ int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, * to look like. They might even boot, if they are RO and * don't have the FS layout. Just ignore the error and * switch_root() nevertheless. */ - (void) base_filesystem_create(new_root); + (void) base_filesystem_create(new_root, UID_INVALID, GID_INVALID); - if (chdir(new_root) < 0) { - log_error_errno(errno, "Failed to change directory to %s: %m", new_root); - return -errno; - } + if (chdir(new_root) < 0) + return log_error_errno(errno, "Failed to change directory to %s: %m", new_root); if (old_root_remove) { old_root_fd = open("/", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY|O_DIRECTORY); @@ -132,20 +128,14 @@ int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, oldroot, errno == ENOENT ? "ignoring" : "leaving it around"); - } else if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0) { - log_error_errno(errno, "Failed to mount moving %s to /: %m", new_root); - return -errno; - } + } else if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0) + return log_error_errno(errno, "Failed to mount moving %s to /: %m", new_root); - if (chroot(".") < 0) { - log_error_errno(errno, "Failed to change root: %m"); - return -errno; - } + if (chroot(".") < 0) + return log_error_errno(errno, "Failed to change root: %m"); - if (chdir("/") < 0) { - log_error_errno(errno, "Failed to change directory: %m"); - return -errno; - } + if (chdir("/") < 0) + return log_error_errno(errno, "Failed to change directory: %m"); if (old_root_fd >= 0) { struct stat rb; @@ -153,7 +143,7 @@ int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, if (fstat(old_root_fd, &rb) < 0) log_warning_errno(errno, "Failed to stat old root directory, leaving: %m"); else { - rm_rf_children(old_root_fd, false, false, &rb); + (void) rm_rf_children(old_root_fd, 0, &rb); old_root_fd = -1; } }