[BusyBox] Init does not always reap child processes

Jason Schoon floydpink at gmail.com
Fri Jul 29 15:02:20 UTC 2005


We had some issues on our embedded system where init did not always
reap child processes, resulting in zombies.

This happened because in a production mode we launch our main
application from a wait line in inittab, and run that forever.  This
caused the init code to never actually reach the loop at the bottom
that reaps child processes, because it is stopped processing the wait
command.

This is almost exactly the scenario desribed in this post from a year ago:
http://www.busybox.net/lists/busybox/2004-August/012420.html

My fix was to enable SIGCHLD handling while in a wait action, and then
disable it again upon leaving the wait action.  The child handler
simply does a waitpid() without hanging around.

diff -ruN --strip-trailing-cr init-trunk/init.c init/init.c
--- init-trunk/init.c	2005-07-28 16:07:06.000000000 -0500
+++ init/init.c	2005-07-28 16:13:52.000000000 -0500
@@ -185,6 +185,7 @@
 
 /* Function prototypes */
 static void delete_init_action(struct init_action *a);
+static void child_handler(int sig);
 static int waitfor(const struct init_action *a);
 static void halt_signal(int sig);
 
@@ -625,6 +626,12 @@
 	return pid;
 }
 
+static void child_handler(int sig)
+{
+    /* Reap any already available children */
+    (void)waitpid (-1, NULL, WNOHANG);
+}
+
 static int waitfor(const struct init_action *a)
 {
 	int pid;
@@ -654,7 +661,11 @@
 		tmp = a->next;
 		if (a->action == action) {
 			if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
+                if (a->action & WAIT)
+                    signal(SIGCHLD, child_handler);                
 				waitfor(a);
+                if (a->action & WAIT)
+                    signal(SIGCHLD, SIG_DFL);                
 				delete_init_action(a);
 			} else if (a->action & ONCE) {
 				run(a);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: init-reap-handling.patch
Type: text/x-patch
Size: 1078 bytes
Desc: not available
Url : http://lists.busybox.net/pipermail/busybox/attachments/20050729/93c5f004/attachment.bin 


More information about the busybox mailing list