- update defconfig
[oweals/busybox.git] / init / init.c
index 51125f3480da71c74a946803a9cbc58f2e2e6dfd..f7eb8f34ba3510d1b463e8acc1b1d18a97795e07 100644 (file)
@@ -95,9 +95,20 @@ static const char *const environment[] = {
 
 /* Function prototypes */
 static void delete_init_action(struct init_action *a);
-static int waitfor(pid_t pid);
 static void halt_reboot_pwoff(int sig) ATTRIBUTE_NORETURN;
 
+static void waitfor(pid_t pid)
+{
+       /* waitfor(run(x)): protect against failed fork inside run() */
+       if (pid <= 0)
+               return;
+
+       /* Wait for any child (prevent zombies from exiting orphaned processes)
+        * but exit the loop only when specified one has exited. */
+       while (wait(NULL) != pid)
+               continue;
+}
+
 static void loop_forever(void) ATTRIBUTE_NORETURN;
 static void loop_forever(void)
 {
@@ -465,19 +476,6 @@ static pid_t run(const struct init_action *a)
        _exit(-1);
 }
 
-static int waitfor(pid_t runpid)
-{
-       int status, wpid;
-
-       while (1) {
-               wpid = waitpid(runpid, &status, 0);
-               if (wpid == -1 && errno == EINTR)
-                       continue;
-               break;
-       }
-       return wpid;
-}
-
 /* Run all commands of a particular type */
 static void run_actions(int action)
 {
@@ -520,7 +518,7 @@ static void init_reboot(unsigned long magic)
                reboot(magic);
                _exit(0);
        }
-       waitpid(pid, NULL, 0);
+       waitfor(pid);
 }
 
 static void kill_all_processes(void)
@@ -980,7 +978,7 @@ int init_main(int argc, char **argv)
                /* Don't consume all CPU time -- sleep a bit */
                sleep(1);
 
-               /* Wait for a child process to exit */
+               /* Wait for any child process to exit */
                wpid = wait(NULL);
                while (wpid > 0) {
                        /* Find out who died and clean up their corpse */
@@ -995,7 +993,7 @@ int init_main(int argc, char **argv)
                                }
                        }
                        /* see if anyone else is waiting to be reaped */
-                       wpid = waitpid(-1, NULL, WNOHANG);
+                       wpid = wait_any_nohang(NULL);
                }
        }
 }