summaryrefslogtreecommitdiff
path: root/src/core/switch-root.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/switch-root.c')
-rw-r--r--src/core/switch-root.c35
1 files changed, 10 insertions, 25 deletions
diff --git a/src/core/switch-root.c b/src/core/switch-root.c
index ce0e41d510..518ec1f0a7 100644
--- a/src/core/switch-root.c
+++ b/src/core/switch-root.c
@@ -41,11 +41,10 @@ int switch_root(const char *new_root) {
"/sys\0"
"/run\0";
- int r, old_root_fd = -1;
+ _cleanup_close_ int old_root_fd = -1;
struct stat new_root_stat;
bool old_root_remove;
- const char *i;
- _cleanup_free_ char *temporary_old_root = NULL;
+ const char *i, *temporary_old_root;
if (path_equal(new_root, "/"))
return 0;
@@ -56,16 +55,13 @@ int switch_root(const char *new_root) {
* directory we choose for this, but it should be more likely
* than not that /mnt exists and is suitable as mount point
* and is on the same fs as the old root dir */
- temporary_old_root = strappend(new_root, "/mnt");
- if (!temporary_old_root)
- return -ENOMEM;
+ temporary_old_root = strappenda(new_root, "/mnt");
old_root_remove = in_initrd();
if (stat(new_root, &new_root_stat) < 0) {
- r = -errno;
log_error("Failed to stat directory %s: %m", new_root);
- goto fail;
+ return -errno;
}
/* Work-around for a kernel bug: for some reason the kernel
@@ -104,9 +100,8 @@ int switch_root(const char *new_root) {
}
if (chdir(new_root) < 0) {
- r = -errno;
log_error("Failed to change directory to %s: %m", new_root);
- goto fail;
+ return -errno;
}
if (old_root_remove) {
@@ -123,27 +118,23 @@ int switch_root(const char *new_root) {
/* Immediately get rid of the old root. Since we are
* running off it we need to do this lazily. */
if (umount2(temporary_old_root, MNT_DETACH) < 0) {
- r = -errno;
log_error("Failed to umount old root dir %s: %m", temporary_old_root);
- goto fail;
+ return -errno;
}
} else if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0) {
- r = -errno;
log_error("Failed to mount moving %s to /: %m", new_root);
- goto fail;
+ return -errno;
}
if (chroot(".") < 0) {
- r = -errno;
log_error("Failed to change root: %m");
- goto fail;
+ return -errno;
}
if (chdir("/") < 0) {
- r = -errno;
log_error("Failed to change directory: %m");
- goto fail;
+ return -errno;
}
if (old_root_fd >= 0) {
@@ -157,11 +148,5 @@ int switch_root(const char *new_root) {
}
}
- r = 0;
-
-fail:
- if (old_root_fd >= 0)
- close_nointr_nofail(old_root_fd);
-
- return r;
+ return 0;
}