]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
dual-fork for daemonized fork in lxcapi-start
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Fri, 7 Sep 2012 16:14:04 +0000 (11:14 -0500)
committerStéphane Graber <stgraber@ubuntu.com>
Mon, 12 Nov 2012 18:17:54 +0000 (13:17 -0500)
So the container will be reparented by init.  Otherwise children of the
lxc-start might be reaped by python3 rather than lxc-start.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
src/lxc/lxccontainer.c

index c8dc8c483515c54283effd570e95faaaa518780b..ea39710b37756d6d50923c4e8dfc1e5f3cedd1ab 100644 (file)
@@ -265,8 +265,15 @@ static bool lxcapi_wait(struct lxc_container *c, char *state, int timeout)
 static bool wait_on_daemonized_start(struct lxc_container *c)
 {
        /* we'll probably want to make this timeout configurable? */
-       int timeout = 5;
+       int timeout = 5, ret, status;
 
+       /*
+        * our child is going to fork again, then exit.  reap the
+        * child
+        */
+       ret = wait(&status);
+       if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
+               DEBUG("failed waiting for first dual-fork child");
        return lxcapi_wait(c, "RUNNING", timeout);
 }
 
@@ -325,6 +332,14 @@ static bool lxcapi_start(struct lxc_container *c, int useinit, char ** argv)
                }
                if (pid != 0)
                        return wait_on_daemonized_start(c);
+               /* second fork to be reparented by init */
+               pid = fork();
+               if (pid < 0) {
+                       SYSERROR("Error doing dual-fork");
+                       return false;
+               }
+               if (pid != 0)
+                       exit(0);
                /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
                if (chdir("/")) {
                        SYSERROR("Error chdir()ing to /.");