summaryrefslogtreecommitdiff
path: root/src/core/switch-root.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-03-18 19:22:43 +0100
committerLennart Poettering <lennart@poettering.net>2014-03-18 19:31:34 +0100
commit03e334a1c7dc8c20c38902aa039440763acc9b17 (patch)
treebc30b522de8ef9c251bf3ff2fe2d52c92dd8b1ea /src/core/switch-root.c
parent9459781ee66eb57709c8b8701701365ba60a9f1c (diff)
util: replace close_nointr_nofail() by a more useful safe_close()
safe_close() automatically becomes a NOP when a negative fd is passed, and returns -1 unconditionally. This makes it easy to write lines like this: fd = safe_close(fd); Which will close an fd if it is open, and reset the fd variable correctly. By making use of this new scheme we can drop a > 200 lines of code that was required to test for non-negative fds or to reset the closed fd variable afterwards.
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;
}