From 0d21c2183e3908caff242fe298592b6cc4bf361b Mon Sep 17 00:00:00 2001 From: Alex Fang Date: Sat, 6 May 2017 21:34:32 +1000 Subject: [PATCH] Pass back the pid of runc:[1:CHILD] so we can wait on it This allows the libcontainer to automatically clean up runc:[1:CHILD] processes created as part of nsenter. Signed-off-by: Alex Fang --- libcontainer/init_linux.go | 3 ++- libcontainer/nsenter/nsexec.c | 17 +++++++++++------ libcontainer/process_linux.go | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/libcontainer/init_linux.go b/libcontainer/init_linux.go index 65bee0bed23..8bbe662f56c 100644 --- a/libcontainer/init_linux.go +++ b/libcontainer/init_linux.go @@ -31,7 +31,8 @@ const ( ) type pid struct { - Pid int `json:"pid"` + Pid int `json:"pid"` + PidFirstChild int `json:"pid_first"` } // network is an internal struct used to setup container networks. diff --git a/libcontainer/nsenter/nsexec.c b/libcontainer/nsenter/nsexec.c index 0ad68834388..b181ba658b9 100644 --- a/libcontainer/nsenter/nsexec.c +++ b/libcontainer/nsenter/nsexec.c @@ -543,7 +543,7 @@ void nsexec(void) */ case JUMP_PARENT: { int len; - pid_t child; + pid_t child, first_child = -1; char buf[JSON_MAX]; bool ready = false; @@ -607,18 +607,18 @@ void nsexec(void) } break; case SYNC_RECVPID_PLS: { - pid_t old = child; + first_child = child; /* Get the init_func pid. */ if (read(syncfd, &child, sizeof(child)) != sizeof(child)) { - kill(old, SIGKILL); + kill(first_child, SIGKILL); bail("failed to sync with child: read(childpid)"); } /* Send ACK. */ s = SYNC_RECVPID_ACK; if (write(syncfd, &s, sizeof(s)) != sizeof(s)) { - kill(old, SIGKILL); + kill(first_child, SIGKILL); kill(child, SIGKILL); bail("failed to sync with child: write(SYNC_RECVPID_ACK)"); } @@ -666,8 +666,13 @@ void nsexec(void) } } - /* Send the init_func pid back to our parent. */ - len = snprintf(buf, JSON_MAX, "{\"pid\": %d}\n", child); + /* + * Send the init_func pid and the pid of the first child back to our parent. + * + * We need to send both back because we can't reap the first child we created (CLONE_PARENT). + * It becomes the responsibility of our parent to reap the first child. + */ + len = snprintf(buf, JSON_MAX, "{\"pid\": %d, \"pid_first\": %d}\n", child, first_child); if (len < 0) { kill(child, SIGKILL); bail("unable to generate JSON for child pid"); diff --git a/libcontainer/process_linux.go b/libcontainer/process_linux.go index 171685ccd2d..8a7ceeacd1e 100644 --- a/libcontainer/process_linux.go +++ b/libcontainer/process_linux.go @@ -141,6 +141,16 @@ func (p *setnsProcess) execSetns() error { p.cmd.Wait() return newSystemErrorWithCause(err, "reading pid from init pipe") } + + // Clean up the zombie parent process + firstChildProcess, err := os.FindProcess(pid.PidFirstChild) + if err != nil { + return err + } + if _, err := firstChildProcess.Wait(); err != nil { + return err + } + process, err := os.FindProcess(pid.Pid) if err != nil { return err @@ -224,6 +234,16 @@ func (p *initProcess) execSetns() error { p.cmd.Wait() return err } + + // Clean up the zombie parent process + firstChildProcess, err := os.FindProcess(pid.PidFirstChild) + if err != nil { + return err + } + if _, err := firstChildProcess.Wait(); err != nil { + return err + } + process, err := os.FindProcess(pid.Pid) if err != nil { return err