ash: propagate exitstatus to wait -n for correct $? value

function                                             old     new   delta
dowait                                               533     532      -1

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko committed 2026-10-05 17:40:47 +02:00
1 parent f4406b2d09
commit ef673ec65a
5 files changed
+32 -3

No files matched your search

+10 -3
View File
@@ -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;
}
/*
@@ -0,0 +1,4 @@
This should happen before wait -n #1 returns
1:1
3:3
Ok:0
@@ -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:$?
@@ -0,0 +1,4 @@
This should happen before wait -n #1 returns
1:1
3:3
Ok:0
@@ -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:$?