This way there's no race between unlinking the /newroot directory and the MS_MOVE/chroot() to get away from it. --- sys-utils/switch_root.c | 19 ++++++++++--------- 1 files changed, 10 insertions(+), 9 deletions(-) diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c index 4674290..72818d9 100644 --- a/sys-utils/switch_root.c +++ b/sys-utils/switch_root.c @@ -135,15 +135,6 @@ static int switchroot(const char *newroot) } cfd = open("/", O_RDONLY); - if (cfd >= 0) { - pid = fork(); - if (pid <= 0) { - recursiveRemove(cfd); - if (pid == 0) - exit(EXIT_SUCCESS); - } - close(cfd); - } if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) { warn("failed to mount moving %s to /", newroot); @@ -154,6 +145,16 @@ static int switchroot(const char *newroot) warn("failed to change root"); return -1; } + + if (cfd >= 0) { + pid = fork(); + if (pid <= 0) { + recursiveRemove(cfd); + if (pid == 0) + exit(EXIT_SUCCESS); + } + close(cfd); + } return 0; } -- 1.6.2.2 -- To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html