From 770e2583907fa38e2b78601a90799b6ae7ab15eb Mon Sep 17 00:00:00 2001 From: "Daniel, Dao Quang Minh" Date: Tue, 10 Feb 2015 04:50:22 -0500 Subject: [PATCH] handle SIGCHLD when running as child subreaper When running under child subreaper mode, it's useful for nsenter to be able to reap child processes. We have seen cases where spawned user processes wasnt reaped properly (https://github.com/creationix/nvm/issues/650) Signed-off-by: Daniel, Dao Quang Minh --- namespaces/nsenter/nsenter.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/namespaces/nsenter/nsenter.c b/namespaces/nsenter/nsenter.c index 4ab21774f..747fce3a4 100644 --- a/namespaces/nsenter/nsenter.c +++ b/namespaces/nsenter/nsenter.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -84,6 +85,12 @@ void print_usage() "nsenter --nspid --console -- cmd1 arg1 arg2...\n"); } +void child_handler(int sig) +{ + while (waitpid(-1, NULL, WNOHANG) > 0) { + } +} + void nsenter() { int argc, c; @@ -102,6 +109,15 @@ void nsenter() pr_perror("Failed to set child subreaper"); exit(1); } + // setup SIGCHLD handler to reap zombie processes + struct sigaction sa; + sigemptyset(&sa.sa_mask); + sa.sa_handler = &child_handler; + sa.sa_flags = SA_RESTART | SA_NOCLDSTOP; + if (sigaction(SIGCHLD, &sa, NULL) == -1) { + pr_perror("Failed to set SIGCHLD handler"); + exit(1); + } #endif static const struct option longopts[] = {