[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[Nbd] [PATCH] nbd-client: do not leave child processes in the zombie state

Change SIGCHLD handler to SIG_DFL with SA_NOCLDWAIT flag set, to avoid
transforming child processes into zombies when they terminate.  Since
the main process is inevitably blocked in NBD_DO_IT ioctl at the time
when child processes terminate, this is the simplest way to get rid of
zombie processes.

Signed-off-by: Dmitry V. Levin <ldv@...1147...>
 nbd-client.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/nbd-client.c b/nbd-client.c
index 7a526b9..2321e7b 100644
--- a/nbd-client.c
+++ b/nbd-client.c
@@ -35,6 +35,7 @@
 #include <stdlib.h>
 #include <sys/mount.h>
 #include <sys/mman.h>
+#include <signal.h>
 #include <errno.h>
 #include <getopt.h>
 #include <stdarg.h>
@@ -571,6 +572,16 @@ int main(int argc, char *argv[]) {
 	do {
 #ifndef NOFORK
+		struct sigaction sa;
+		sa.sa_handler = SIG_DFL;
+		sigemptyset(&sa.sa_mask);
+		sa.sa_flags = SA_NOCLDWAIT;
+		if (sigaction(SIGCHLD, &sa, NULL) < 0)
+			err("sigaction: %m");
 		if (!fork()) {
 			/* Due to a race, the kernel NBD driver cannot
 			 * call for a reread of the partition table

Reply to: