[git commit] ash: waiting machinery: add comments explaining the code

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


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

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

diff --git a/shell/ash.c b/shell/ash.c
index 8b9ac5c32..e652a69f7 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -4392,7 +4392,7 @@ static int waitone(int block, struct job *job)
 	block = (block & ~DOWAIT_JOBSTATUS);
 #endif
 
-	TRACE(("dowait(0x%x) called\n", block));
+	TRACE(("waitone(0x%x) called\n", block));
 
 	/* It's wrong to call waitpid() outside of INTOFF region:
 	 * signal can arrive just after syscall return and handler can
@@ -4414,7 +4414,7 @@ static int waitone(int block, struct job *job)
 	 */
 	INTOFF;
 	pid = waitproc(block, &status);
-	TRACE(("wait returns pid %d, status=%d\n", pid, status));
+	TRACE(("waitproc returns pid %d, status=%d\n", pid, status));
 	if (pid <= 0)
 		goto out;
 
@@ -4504,14 +4504,31 @@ static int dowait(int block, struct job *jp)
 
 	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
+	 * (jp->state == JOBRUNNING), we have to wait in blocking mode again,
+	 * until it's not JOBRUNNING.
+	 * The user is waitforjob(jp) -> dowait(DOWAIT_BLOCK, jp).
+	 * = If we waited in non-blocking mode and got pid > 0,
+	 * 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)
+	 * 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);
+	} while (pid >= 0); //WRONG? should be "pid > 0"
 
+	/* Return: 0 if seen a signal, 1 otherwise */
 	return rpid;
 }
 


More information about the busybox-cvs mailing list