]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
switch_root: do recursiveRemove after our root is moved to avoid races.
authorPeter Jones <pjones@redhat.com>
Fri, 19 Jun 2009 19:44:32 +0000 (15:44 -0400)
committerKarel Zak <kzak@redhat.com>
Mon, 22 Jun 2009 19:45:25 +0000 (21:45 +0200)
This way there's no race between unlinking the /newroot directory and
the MS_MOVE/chroot() to get away from it.

Signed-off-by: Peter Jones <pjones@redhat.com>
sys-utils/switch_root.c

index 1de3a2e58cf3fb53c91407190eae493dcf803c55..b947f54468979c38a77ecc0aadcc457e71615cb9 100644 (file)
@@ -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;
 }