← Commit list

ef673ec65

Denys Vlasenko
Denys Vlasenko
2026-10-05
SUCCESS Duration: 196s

Subject

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

Binary

No binary (build failed)

Architecture & Compiler Flags

Architecture: x86_64-linux-gnu Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0

—

Build Log

(log yok)

Commit Diff GitHub →

shell/ash.c+10 -3
@@ -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;
}
/*
shell/ash_test/ash-misc/wait_n_waits_for_the_job_not_process.right+4 -0
@@ -0,0 +1,4 @@
+This should happen before wait -n #1 returns
+1:1
+3:3
+Ok:0
shell/ash_test/ash-misc/wait_n_waits_for_the_job_not_process.tests+7 -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:$?
shell/hush_test/hush-misc/wait_n_waits_for_the_job_not_process.right+4 -0
@@ -0,0 +1,4 @@
+This should happen before wait -n #1 returns
+1:1
+3:3
+Ok:0
shell/hush_test/hush-misc/wait_n_waits_for_the_job_not_process.tests+7 -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:$?