ash: fix "wait -n" behavior wrt already-exited jobs

function                                             old     new   delta
waitcmd                                              318     346     +28
kill_main                                            907     919     +12
dowait                                               553     557      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 44/0)               Total: 44 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko committed 2026-10-07 11:15:37 +02:00
1 parent 730f515626
commit 30f2f883bd
8 files changed
+70 -19

No files matched your search

+25 -18
View File
@@ -4480,9 +4480,10 @@ static int waitone(int block, int *status, struct job *job)
#if BASH_WAIT_N
if (want_jobexitstatus) {
if (thisjob && thisjob->state == JOBDONE)
if (thisjob && thisjob->state == JOBDONE) {
*status = thisjob->ps[thisjob->nprocs - 1].ps_status;
else {
thisjob->waited = 1;
} else {
/* The job is not found, or not fully completed:
* "wait -n" should NOT yet finish. Giving it -1, it will retry. */
*status = -1;
@@ -4864,7 +4865,7 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
int status;
char wait_n = nextopt("n");
#else
const int wait_n = 0;
int wait_n = 0; /* never changes if !BASH_WAIT_N */
nextopt(nullstr);
#endif
retval = 0;
@@ -4875,14 +4876,28 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
for (;;) {
jp = curjob;
while (1) {
if (!jp) { /* no running procs */
if (wait_n) /* exitcode of "wait -n" with nothing to wait for is 127, not 0 */
if (!jp) { /* no more jobs */
if (wait_n == 1)
break; /* there is a live job */
if (wait_n == 'n') /* exitcode of "wait -n" with nothing to wait for is 127, not 0 */
retval = 127;
goto ret;
}
if (jp->state == JOBRUNNING)
break;
jp->waited = 1;
if (!wait_n) {
if (jp->state != JOBDONE)
break;
jp->waited = 1;
} else {
if (jp->state != JOBDONE) {
wait_n = 1; /* replace 'n': mark that we found a live job */
} else if (!jp->waited) {
jp->waited = 1;
/* "wait -n" returns status of unwaited-for dead jobs immediately */
/* (bash-5.3.9 does this in -c 'CMDs' and scripts only) */
retval = getstatus(jp);
goto ret;
}
}
jp = jp->prev_job;
}
/* man bash:
@@ -4911,7 +4926,7 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
*/
if (pid > 0) { /* some process did exit */
if (status != -1) {
# if JOBS /* only possible if JOBS */
# if JOBS /* processes can be stopped even if !JOBS, but we never wait for them with WUNTRACED - never see stops */
if (WIFSTOPPED(status))
continue; /* not interested in stops, find another process */
# endif
@@ -4921,14 +4936,6 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
goto ret;
}
/* else: not entire job has exited (only a process), wait more */
} else {
/* There are no alive jobs. Are there dead ones? */
/* (bash-5.3.9 does this) */
if (curjob) {
retval = getstatus(curjob);
freejob(curjob);
goto ret;
}
}
}
#endif
@@ -4950,7 +4957,7 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
} else {
job = getjob(*argv, 0);
}
/* loop until process terminated or stopped */
/* loop until process terminated (but stops do not count) */
dowait(DOWAIT_CHILD_OR_SIG, /*status:*/ NULL, job);
if (pending_sig)
goto sigout;
@@ -0,0 +1,7 @@
wait1
INT
w1:130
wait2
INT
w2:130
Ok:0
@@ -0,0 +1,9 @@
trap 'echo INT' INT
(sleep 2;sh -c 'kill -sstop $$') &
bg=$!
(sleep 1;kill -INT $$;sleep 2;kill -INT $$) &
echo wait1; wait; echo w1:$?
echo wait2; wait; echo w2:$?
kill -cont $bg
kill $bg
echo Ok:$?
@@ -6,10 +6,10 @@ echo "Sent SIGTERM:$?"
# The internal state depends on whether shell manages to see the death of $bg before entering "wait -n":
# will the job be already "dead and waited-for"? In the latter case, the correctness is harder to achieve.
sleep 0.1
echo "Test: wait -n exit code for sigkilled (and possibly already waited-for) pipe"
wait -n
echo "wait -n #1: $? (should be 143 = 128+SIGTERM)"
echo "Test: wait -n exits 127 if nothing to wait for"
wait -n
echo "wait -n #1: $? (should be 127)"
@@ -0,0 +1,3 @@
4:4
8:8
127:127
@@ -0,0 +1,9 @@
#(sleep 0.2;exit 2) &
#^^^ bash will report jobs in order of them exiting, we do the reverse order. fix?
(sleep 0.4;exit 4) &
(sleep 0.8;exit 8) &
sleep 0.6
#wait -n; echo 2:$?
wait -n; echo 4:$?
wait -n; echo 8:$?
wait -n; echo 127:$?
@@ -0,0 +1,7 @@
wait1
INT
w1:130
wait2
INT
w2:130
Ok:0
@@ -0,0 +1,9 @@
trap 'echo INT' INT
(sleep 2;sh -c 'kill -sstop $$') &
bg=$!
(sleep 1;kill -INT $$;sleep 2;kill -INT $$) &
echo wait1; wait; echo w1:$?
echo wait2; wait; echo w2:$?
kill -cont $bg
kill $bg
echo Ok:$?