Skip to contentSkip to conversation

jobs: waitproc: unobfuscate and explain what the code does

PATCH New Denys Vlasenko 1 participant 5 messages Oct 6

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 jobs: waitproc: unobfuscate and explain what the code does

Also, the #ifdef'ed out waitpid() has extra argument, fix that. No logic changes.

---
 src/jobs.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
src/jobs.c +14 -6
index 914b383..51c94dc 100644
@@ -1175,8 +1175,8 @@ static int dowait(int block, struct job *jp)
 }
 
 /*
- * Do a wait system call.  If block is zero, we return -1 rather than
- * blocking.  If block is DOWAIT_WAITCMD, we return 0 when a signal
+ * Do a wait system call.  If block is DOWAIT_NONBLOCK, we return -1 rather
+ * than blocking.  If block is DOWAIT_WAITCMD, we return 0 when a signal
  * other than SIGCHLD interrupted the wait.
  *
  * We use sigsuspend in conjunction with a non-blocking wait3 in
@@ -1189,7 +1189,6 @@ static int dowait(int block, struct job *jp)
  * children have been reaped.  Otherwise zombies may linger.
  */
 
-
 STATIC int
 waitproc(int block, int *status)
 {
@@ -1208,12 +1207,15 @@ waitproc(int block, int *status)
 #ifdef HAVE_WAIT3
 			err = wait3(status, flags, NULL);
 #else
-			err = waitpid((pid_t)-1, status, flags, NULL);
+			err = waitpid((pid_t)-1, status, flags);
 #endif
 		while (err < 0 && errno == EINTR);
 
-		if (err || (err = -!block))
-			break;
+		if (err) /* wait3() returned PID or error? */
+			break; /* return it */
+		err = -!block;
+		if (err) /* block == DOWAIT_NONBLOCK (0)? */
+			break; /* return -1 */
 
 		sigblockall(&oldmask);
 
@@ -1223,6 +1225,12 @@ waitproc(int block, int *status)
 		sigclearmask();
 	} while (gotsigchld);
 
+	/* Possible returns:
+	 * PID of successfully waited-for child.
+	 * -1 on wait3() error (can get ECHILD: no one to wait for)
+	 * -1 if DOWAIT_NONBLOCK
+	 * 0 if there were no waitable children and got a signal (not SIGCHLD)
+	 */
 	return err;
 }
 
Signature · 1 line
2.52.0
#1 lore raw

PATCH 2/5 jobs: reorder wait-related functions. Pure block move, no code changes

Placing them in the "callees first" order:

waitproc() {...}
waitone() {...} // calls waitproc
dowait() {...} // calls waitone

instead of current semi-random one:

waitone() {...}
dowait() {...}
waitproc() {...}

---
 src/jobs.c | 119 ++++++++++++++++++++++++++---------------------------
 1 file changed, 58 insertions(+), 61 deletions(-)
src/jobs.c +58 -61
index 51c94dc..568ffd4 100644
@@ -116,7 +116,6 @@ STATIC int dowait(int, struct job *);
 #ifdef SYSV
 STATIC int onsigchild(void);
 #endif
-STATIC int waitproc(int, int *);
 STATIC char *commandtext(union node *);
 STATIC void cmdtxt(union node *);
 STATIC void cmdlist(union node *, int);
@@ -1065,7 +1064,65 @@ waitforjob(struct job *jp)
 	return st;
 }
 
+/*
+ * Do a wait system call.  If block is DOWAIT_NONBLOCK, we return -1 rather
+ * than blocking.  If block is DOWAIT_WAITCMD, we return 0 when a signal
+ * other than SIGCHLD interrupted the wait.
+ *
+ * We use sigsuspend in conjunction with a non-blocking wait3 in
+ * order to ensure that waitcmd exits promptly upon the reception
+ * of a signal.
+ *
+ * For code paths other than waitcmd we either use a blocking wait3
+ * or a non-blocking wait3.  For the latter case the caller of dowait
+ * must ensure that it is called over and over again until all dead
+ * children have been reaped.  Otherwise zombies may linger.
+ */
+
+STATIC int
+waitproc(int block, int *status)
+{
+	sigset_t oldmask;
+	int flags = block == DOWAIT_BLOCK ? 0 : WNOHANG;
+	int err;
 
+#if JOBS
+	if (jobctl)
+		flags |= WUNTRACED;
+#endif
+
+	do {
+		gotsigchld = 0;
+		do
+#ifdef HAVE_WAIT3
+			err = wait3(status, flags, NULL);
+#else
+			err = waitpid((pid_t)-1, status, flags);
+#endif
+		while (err < 0 && errno == EINTR);
+
+		if (err) /* wait3() returned PID or error? */
+			break; /* return it */
+		err = -!block;
+		if (err) /* block == DOWAIT_NONBLOCK (0)? */
+			break; /* return -1 */
+
+		sigblockall(&oldmask);
+
+		while (!gotsigchld && !pending_sig)
+			sigsuspend(&oldmask);
+
+		sigclearmask();
+	} while (gotsigchld);
+
+	/* Possible returns:
+	 * PID of successfully waited-for child.
+	 * -1 on wait3() error (can get ECHILD: no one to wait for)
+	 * -1 if DOWAIT_NONBLOCK
+	 * 0 if there were no waitable children and got a signal (not SIGCHLD)
+	 */
+	return err;
+}
 
 /*
  * Wait for a process to terminate.
@@ -1174,66 +1231,6 @@ static int dowait(int block, struct job *jp)
 	return rpid;
 }
 
-/*
- * Do a wait system call.  If block is DOWAIT_NONBLOCK, we return -1 rather
- * than blocking.  If block is DOWAIT_WAITCMD, we return 0 when a signal
- * other than SIGCHLD interrupted the wait.
- *
- * We use sigsuspend in conjunction with a non-blocking wait3 in
- * order to ensure that waitcmd exits promptly upon the reception
- * of a signal.
- *
- * For code paths other than waitcmd we either use a blocking wait3
- * or a non-blocking wait3.  For the latter case the caller of dowait
- * must ensure that it is called over and over again until all dead
- * children have been reaped.  Otherwise zombies may linger.
- */
-
-STATIC int
-waitproc(int block, int *status)
-{
-	sigset_t oldmask;
-	int flags = block == DOWAIT_BLOCK ? 0 : WNOHANG;
-	int err;
-
-#if JOBS
-	if (jobctl)
-		flags |= WUNTRACED;
-#endif
-
-	do {
-		gotsigchld = 0;
-		do
-#ifdef HAVE_WAIT3
-			err = wait3(status, flags, NULL);
-#else
-			err = waitpid((pid_t)-1, status, flags);
-#endif
-		while (err < 0 && errno == EINTR);
-
-		if (err) /* wait3() returned PID or error? */
-			break; /* return it */
-		err = -!block;
-		if (err) /* block == DOWAIT_NONBLOCK (0)? */
-			break; /* return -1 */
-
-		sigblockall(&oldmask);
-
-		while (!gotsigchld && !pending_sig)
-			sigsuspend(&oldmask);
-
-		sigclearmask();
-	} while (gotsigchld);
-
-	/* Possible returns:
-	 * PID of successfully waited-for child.
-	 * -1 on wait3() error (can get ECHILD: no one to wait for)
-	 * -1 if DOWAIT_NONBLOCK
-	 * 0 if there were no waitable children and got a signal (not SIGCHLD)
-	 */
-	return err;
-}
-
 /*
  * return 1 if there are stopped jobs, otherwise 0
  */
Signature · 1 line
2.52.0
#2 lore raw

PATCH 3/5 jobs: add comments explaining waiting machinery

In reply to Denys Vlasenko
---
 src/jobs.c | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)
src/jobs.c +23 -6
index 568ffd4..008978f 100644
@@ -79,9 +79,9 @@
 #define CUR_STOPPED 0
 
 /* mode flags for dowait */
-#define DOWAIT_NONBLOCK 0
-#define DOWAIT_BLOCK 1
-#define DOWAIT_WAITCMD 2
+#define DOWAIT_NONBLOCK    0 /* waitpid() will use WNOHANG and won't wait for signals */
+#define DOWAIT_BLOCK       1 /* waitpid() will NOT use WNOHANG */
+#define DOWAIT_WAITCMD     2 /* waitpid() will use WNOHANG and if got 0, will wait for signals, then loop back */
 #define DOWAIT_WAITCMD_ALL 4
 
 /* array of jobs */
@@ -1137,9 +1137,9 @@ static int waitone(int block, struct job *job)
 	int state;
 
 	INTOFF;
-	TRACE(("dowait(%d) called\n", block));
+	TRACE(("waitone(%d) called\n", block));
 	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;
 
@@ -1219,6 +1219,22 @@ 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;
@@ -1226,8 +1242,9 @@ static int dowait(int block, struct job *jp)
 		block &= ~DOWAIT_WAITCMD_ALL;
 		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;
 }
 
Signature · 1 line
2.52.0
#3 lore raw

PATCH 4/5 jobs: dowait: remove incorrect looping, DOWAIT_WAITCMD_ALL not needed after this

In reply to Denys Vlasenko

Prev code:

         rpid = 1;
         do {
                 pid = waitone(block, jp);
                 rpid &= !!pid; <<<<<<<<<<<<<<<<<< HERE
                 block &= ~DOWAIT_WAITCMD_ALL;
                 if (!pid || (jp && jp->state != JOBRUNNING))
                         block = DOWAIT_NONBLOCK;
         } while (pid >= 0);
         return rpid;

If we got pid == 0, it happens in exactly one case:
we did waitpid(WNOHANG), got 0 ("children exist but none yet changed its state"), then waited for a signal and got one, which wasn't SIGCHLD.

The above behavior is requested by only one caller: waitcmd()
- the "wait" builtin.

"wait" builtin is required to be interruptible by signals and to set exitcode to (128 + sig).

Therefore, if we got pid == 0, we don't need to wait for any more processes. We need to return to waitcmd() and let it set $? accordingly.

Instead, we do yet another loop.

I'm not saying that we know
that there are no more processes that finished.

I'm saying we don't care at this point: we know
that we are "wait" builtin and we got interrupted by a signal.

Fix this by not looping if "pid == 0" indicates we got a signal. ONLY loop if we got a pid > 0 and we either do not wait for a specific job, or this scpecific job is still RUNNING.

---
 src/jobs.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
src/jobs.c +15 -15
index 008978f..0a4ba75 100644
@@ -82,7 +82,6 @@
 #define DOWAIT_NONBLOCK    0 /* waitpid() will use WNOHANG and won't wait for signals */
 #define DOWAIT_BLOCK       1 /* waitpid() will NOT use WNOHANG */
 #define DOWAIT_WAITCMD     2 /* waitpid() will use WNOHANG and if got 0, will wait for signals, then loop back */
-#define DOWAIT_WAITCMD_ALL 4
 
 /* array of jobs */
 static struct job *jobtab;
@@ -630,7 +629,7 @@ waitcmd(int argc, char **argv)
 				jp->waited = 1;
 				jp = jp->prev_job;
 			}
-			if (!dowait(DOWAIT_WAITCMD_ALL, 0))
+			if (!dowait(DOWAIT_WAITCMD, NULL))
 				goto sigout;
 		}
 	}
@@ -1103,6 +1102,9 @@ waitproc(int block, int *status)
 
 		if (err) /* wait3() returned PID or error? */
 			break; /* return it */
+
+		/* We are here only if DOWAIT_NONBLOCK or DOWAIT_WAITCMD */
+
 		err = -!block;
 		if (err) /* block == DOWAIT_NONBLOCK (0)? */
 			break; /* return -1 */
@@ -1208,7 +1210,6 @@ out:
 static int dowait(int block, struct job *jp)
 {
 	int gotchld = *(volatile int *)&gotsigchld;
-	int rpid;
 	int pid;
 
 	if (jp && jp->state != JOBRUNNING)
@@ -1217,8 +1218,6 @@ static int dowait(int block, struct job *jp)
 	if (block == DOWAIT_NONBLOCK && !gotchld)
 		return 1;
 
-	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
@@ -1229,23 +1228,24 @@ static int dowait(int block, struct job *jp)
 	 * 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)
+	 *  showjobs() -> dowait(DOWAIT_NONBLOCK, NULL)
+	 *  waitforjob(NULL) -> dowait(DOWAIT_NONBLOCK, NULL)
 	 * 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;
-
-		block &= ~DOWAIT_WAITCMD_ALL;
-		if (!pid || (jp && jp->state != JOBRUNNING))
-			block = DOWAIT_NONBLOCK;
-	} while (pid >= 0); //WRONG? should be "pid > 0"
+		if (pid == 0)
+			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 */
+		}
+		/* if we saw a process changing state, wait again (is there more?) */
+	} while (pid > 0);
 
-	/* Return: 0 if seen a signal, 1 otherwise */
-	return rpid;
+	return 1;  /* did not get a signal */
 }
 
 /*
Signature · 1 line
2.52.0
#4 lore raw

PATCH 5/5 jobs: rename DOWAIT_WAITCMD to DOWAIT_CHILD_OR_SIG

In reply to Denys Vlasenko

First two modes tell dowait() what to do:
do a nonblocking, or blocking wait3().

But DOWAIT_WAITCMD name uses a different convention. Instead of telling dowait() what to do, it tells dowait() *where the call came from*! So, what would dowait() do? Code reader has no idea...

Rename it to DOWAIT_CHILD_OR_SIG instead,
so that it describes what dowait() will wait for.

While at it, rename "int err" in waitproc() to "pid". It's a much more logically correct name.

---
 src/jobs.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
src/jobs.c +16 -17
index 0a4ba75..9bdb8af 100644
@@ -79,9 +79,9 @@
 #define CUR_STOPPED 0
 
 /* mode flags for dowait */
-#define DOWAIT_NONBLOCK    0 /* waitpid() will use WNOHANG and won't wait for signals */
-#define DOWAIT_BLOCK       1 /* waitpid() will NOT use WNOHANG */
-#define DOWAIT_WAITCMD     2 /* waitpid() will use WNOHANG and if got 0, will wait for signals, then loop back */
+#define DOWAIT_NONBLOCK     0 /* waitpid() will use WNOHANG and won't wait for signals */
+#define DOWAIT_BLOCK        1 /* waitpid() will NOT use WNOHANG */
+#define DOWAIT_CHILD_OR_SIG 2 /* waitpid() will use WNOHANG and if got 0, will wait for signals, then loop back if SIGCLD */
 
 /* array of jobs */
 static struct job *jobtab;
@@ -629,7 +629,7 @@ waitcmd(int argc, char **argv)
 				jp->waited = 1;
 				jp = jp->prev_job;
 			}
-			if (!dowait(DOWAIT_WAITCMD, NULL))
+			if (!dowait(DOWAIT_CHILD_OR_SIG, NULL))
 				goto sigout;
 		}
 	}
@@ -651,7 +651,7 @@ start:
 		} else
 			job = getjob(*argv, 0);
 		/* loop until process terminated or stopped */
-		if (!dowait(DOWAIT_WAITCMD, job))
+		if (!dowait(DOWAIT_CHILD_OR_SIG, job))
 			goto sigout;
 		job->waited = 1;
 		retval = getstatus(job);
@@ -1065,7 +1065,7 @@ waitforjob(struct job *jp)
 
 /*
  * Do a wait system call.  If block is DOWAIT_NONBLOCK, we return -1 rather
- * than blocking.  If block is DOWAIT_WAITCMD, we return 0 when a signal
+ * than blocking.  If block is DOWAIT_CHILD_OR_SIG, we return 0 when a signal
  * other than SIGCHLD interrupted the wait.
  *
  * We use sigsuspend in conjunction with a non-blocking wait3 in
@@ -1083,7 +1083,7 @@ waitproc(int block, int *status)
 {
 	sigset_t oldmask;
 	int flags = block == DOWAIT_BLOCK ? 0 : WNOHANG;
-	int err;
+	int pid;
 
 #if JOBS
 	if (jobctl)
@@ -1094,21 +1094,20 @@ waitproc(int block, int *status)
 		gotsigchld = 0;
 		do
 #ifdef HAVE_WAIT3
-			err = wait3(status, flags, NULL);
+			pid = wait3(status, flags, NULL);
 #else
-			err = waitpid((pid_t)-1, status, flags);
+			pid = waitpid((pid_t)-1, status, flags);
 #endif
-		while (err < 0 && errno == EINTR);
+		while (pid < 0 && errno == EINTR);
 
-		if (err) /* wait3() returned PID or error? */
+		if (pid != 0) /* wait3() returned PID or error? */
 			break; /* return it */
 
-		/* We are here only if DOWAIT_NONBLOCK or DOWAIT_WAITCMD */
-
-		err = -!block;
-		if (err) /* block == DOWAIT_NONBLOCK (0)? */
-			break; /* return -1 */
+		/* We are here only if DOWAIT_NONBLOCK or DOWAIT_CHILD_OR_SIG */
+		if (block == DOWAIT_NONBLOCK)
+			return -1;
 
+		/* DOWAIT_CHILD_OR_SIG: children exist, wait for a signal */
 		sigblockall(&oldmask);
 
 		while (!gotsigchld && !pending_sig)
@@ -1123,7 +1122,7 @@ waitproc(int block, int *status)
 	 * -1 if DOWAIT_NONBLOCK
 	 * 0 if there were no waitable children and got a signal (not SIGCHLD)
 	 */
-	return err;
+	return pid;
 }
 
 /*
Signature · 1 line
2.52.0