[git commit] ash: dowait: remove incorrect looping

Denys Vlasenko vda.linux at googlemail.com
Mon Oct 5 15:40:47 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=3920b0a994a88d04be7053dfa60cde7138244937
branch: https://git.busybox.net/busybox/log/?h=master

Prev code:

         rpid = 1;
         do {
                 pid = waitone(block, jp);
                 rpid &= !!pid; <<<<<<<<<<<<<<<<<< HERE
                 if (!pid || (jp && jp->state != JOBRUNNING))
                         block = DOWAIT_NONBLOCK;
         } while (pid >= 0);
         return rpid;

If we got pid == 0, it happens in exactly one case:
we did waitpid(WNOHANG), got 0 ("children exist but none yet
changed its state"), then waited for a signal and got one,
which wasn't SIGCHLD.

The above behavior is requested by only one caller: waitcmd()
- the "wait" builtin.

"wait" builtin is required to be interruptible by signals and
to set exitcode to (128 + sig).

Therefore, if we got pid == 0, we don't need to wait
for any more processes. We need to return to waitcmd()
and let it set $? accordingly.

Instead, we do yet another loop.

I'm not saying that we know
that there are no more processes that finished.

I'm saying we don't care at this point: we know
that we are "wait" builtin and we got interrupted by a signal.

Fix this by not looping if "pid == 0" indicates we got a signal.
ONLY loop if we got a pid > 0 and we either do not wait for a specific job,
or this scpecific job is still RUNNING.

function                                             old     new   delta
dowait                                               546     530     -16

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 shell/ash.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index e652a69f7..138bace44 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -4351,6 +4351,9 @@ waitproc(int block, int *status)
 
 		if (err) /* waitpid() returned PID or error? */
 			break; /* return it */
+
+		/* We are here only if DOWAIT_NONBLOCK or DOWAIT_CHILD_OR_SIG */
+
 		err = -!block;
 		if (err) /* block == DOWAIT_NONBLOCK (0)? */
 			break; /* return -1 */
@@ -4493,7 +4496,6 @@ static int waitone(int block, struct job *job)
 static int dowait(int block, struct job *jp)
 {
 	smallint gotchld = *(volatile smallint *)&gotsigchld;
-	int rpid;
 	int pid;
 
 	if (jp && jp->state != JOBRUNNING)
@@ -4502,8 +4504,6 @@ static int dowait(int block, struct job *jp)
 	if (block == DOWAIT_NONBLOCK && !gotchld)
 		return 1;
 
-	rpid = 1;
-
 	/* In what cases we loop here:
 	 * = If we waited in blocking mode and got pid > 0,
 	 * but the job we waiting for is still running
@@ -4514,22 +4514,24 @@ static int dowait(int block, struct job *jp)
 	 * we should see whether more processes have terminated,
 	 * we have to wait in non-blocking mode again.
 	 * The users are
-	 *  showjobs() -> dowait(DOWAIT_NONBLOCK)
-	 *  waitforjob(NULL) -> dowait(DOWAIT_NONBLOCK)
+	 *  showjobs() -> dowait(DOWAIT_NONBLOCK, NULL)
+	 *  waitforjob(NULL) -> dowait(DOWAIT_NONBLOCK, NULL)
 	 * waitcmd() does its own looping while it sees at least one JOBRUNNING
 	 * job, it does not need us to loop here - only to do
 	 * one nonblocking wait and if it gives nothing, wait for signals.
 	 */
 	do {
 		pid = waitone(block, jp);
-		rpid &= !!pid;
-
-		if (!pid || (jp && jp->state != JOBRUNNING))
-			block = DOWAIT_NONBLOCK;
-	} while (pid >= 0); //WRONG? should be "pid > 0"
+		if (pid == 0)
+			return 0; /* got a signal */
+		if (jp && jp->state != JOBRUNNING) {
+			/* no more running procs in this job, stop waiting */
+			return 1; /* did not get a signal */
+		}
+		/* if we saw a process changing state, wait again (is there more?) */
+	} while (pid > 0);
 
-	/* Return: 0 if seen a signal, 1 otherwise */
-	return rpid;
+	return 1; /* did not get a signal */
 }
 
 /*


More information about the busybox-cvs mailing list