From: Denys Vlasenko <dvlasenk@redhat.com>
To: dash@vger.kernel.org, Herbert Xu <herbert.xu@redhat.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>,
Denys Vlasenko <vda.linux@googlemail.com>
Subject: [PATCH 2/2] Reorder wait-related functions. Pure block move, no code changes
Date: Mon, 5 Oct 2026 04:56:04 +0200 [thread overview]
Message-ID: <20261005025606.75329-2-dvlasenk@redhat.com> (raw)
In-Reply-To: <20261005025606.75329-1-dvlasenk@redhat.com>
Palcing them in the "callees first" order:
waitproc() {...}
waitone() {...} // calls waitproc
dowait() {...} // calls waitone
instead of current semi-random one:
waitone() {...}
dowait() {...}
waitproc() {...}
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
---
src/jobs.c | 121 ++++++++++++++++++++++++++---------------------------
1 file changed, 60 insertions(+), 61 deletions(-)
diff --git a/src/jobs.c b/src/jobs.c
index 51c94dc..3313599 100644
--- a/src/jobs.c
+++ b/src/jobs.c
@@ -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);
@@ -1013,6 +1012,66 @@ struct job *vforkexec(union node *n, char **argv, const char *path, int idx)
return 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
+ * 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 job to finish.
*
@@ -1174,66 +1233,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
*/
--
2.52.0
prev parent reply other threads:[~2026-10-05 2:56 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-05 2:56 [PATCH 1/2] waitproc: unobfuscate and explain what the code does Denys Vlasenko
2026-10-05 2:56 ` Denys Vlasenko [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261005025606.75329-2-dvlasenk@redhat.com \
--to=dvlasenk@redhat.com \
--cc=dash@vger.kernel.org \
--cc=herbert.xu@redhat.com \
--cc=vda.linux@googlemail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox