[git commit] ash: propagate exitstatus to wait -n for correct $? value

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


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

function                                             old     new   delta
dowait                                               533     532      -1

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 shell/ash.c                                                 | 13 ++++++++++---
 .../ash-misc/wait_n_waits_for_the_job_not_process.right     |  4 ++++
 .../ash-misc/wait_n_waits_for_the_job_not_process.tests     |  7 +++++++
 .../hush-misc/wait_n_waits_for_the_job_not_process.right    |  4 ++++
 .../hush-misc/wait_n_waits_for_the_job_not_process.tests    |  7 +++++++
 5 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index 7d7f1d12c..cf7676c91 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -4473,6 +4473,9 @@ static int waitone(int block, struct job *job)
 		pid = -1;
 		if (thisjob && thisjob->state == JOBDONE)
 			pid = thisjob->ps[thisjob->nprocs - 1].ps_status;
+		/* else: the job is not found, or not fully completed:
+		 * "wait -n" should NOT yet finish. Giving it -1, it will retry.
+		 */
 	}
 #endif
 	if (thisjob && thisjob == job) {
@@ -4518,16 +4521,20 @@ static int dowait(int block, struct job *jp)
 	 */
 	do {
 		pid = waitone(block, jp);
+		if (block & DOWAIT_JOBSTATUS)
+			break; /* waitone() gave us the status, not pid (for "wait -n") */
+			/* on signals, unknown pids, or not-fully completed jobs we get -1 */
+			/* it is always DOWAIT_CHILD_OR_SIG in this case */
 		if (pid == 0)
-			return 0; /* got a signal */
+			break; /* 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 */
+			break; /* return pid != 0: did not get a signal */
 		}
 		/* if we saw a process changing state, wait again (is there more?) */
 	} while (pid > 0);
 
-	return 1; /* did not get a signal */
+	return pid;
 }
 
 /*
diff --git a/shell/ash_test/ash-misc/wait_n_waits_for_the_job_not_process.right b/shell/ash_test/ash-misc/wait_n_waits_for_the_job_not_process.right
new file mode 100644
index 000000000..806775364
--- /dev/null
+++ b/shell/ash_test/ash-misc/wait_n_waits_for_the_job_not_process.right
@@ -0,0 +1,4 @@
+This should happen before wait -n #1 returns
+1:1
+3:3
+Ok:0
diff --git a/shell/ash_test/ash-misc/wait_n_waits_for_the_job_not_process.tests b/shell/ash_test/ash-misc/wait_n_waits_for_the_job_not_process.tests
new file mode 100755
index 000000000..c17b15458
--- /dev/null
+++ b/shell/ash_test/ash-misc/wait_n_waits_for_the_job_not_process.tests
@@ -0,0 +1,7 @@
+(sleep 3;exit 3) &
+(sleep 2;echo 'This should happen before wait -n #1 returns' >&2;exit 2) | (sleep 1;exit 1) &
+# the exit code of pipe is the last member's exitcode: 1
+wait -n; echo 1:$?
+# next wait -n should wait for the remaining job
+wait -n; echo 3:$?
+echo Ok:$?
diff --git a/shell/hush_test/hush-misc/wait_n_waits_for_the_job_not_process.right b/shell/hush_test/hush-misc/wait_n_waits_for_the_job_not_process.right
new file mode 100644
index 000000000..806775364
--- /dev/null
+++ b/shell/hush_test/hush-misc/wait_n_waits_for_the_job_not_process.right
@@ -0,0 +1,4 @@
+This should happen before wait -n #1 returns
+1:1
+3:3
+Ok:0
diff --git a/shell/hush_test/hush-misc/wait_n_waits_for_the_job_not_process.tests b/shell/hush_test/hush-misc/wait_n_waits_for_the_job_not_process.tests
new file mode 100755
index 000000000..c17b15458
--- /dev/null
+++ b/shell/hush_test/hush-misc/wait_n_waits_for_the_job_not_process.tests
@@ -0,0 +1,7 @@
+(sleep 3;exit 3) &
+(sleep 2;echo 'This should happen before wait -n #1 returns' >&2;exit 2) | (sleep 1;exit 1) &
+# the exit code of pipe is the last member's exitcode: 1
+wait -n; echo 1:$?
+# next wait -n should wait for the remaining job
+wait -n; echo 3:$?
+echo Ok:$?


More information about the busybox-cvs mailing list