commit 7d0199963a2ec019270ee187f8c9cb8adb25dac1
parent 7c6ca159eb3cb6e36cbdfd613547d3b0b99e7684
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Fri, 15 Apr 2022 06:30:46 +0000
Add waitn_posix() and waitn_reap_posix()
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat:
9 files changed, 70 insertions(+), 21 deletions(-)
diff --git a/NEWS b/NEWS
@@ -8,6 +8,7 @@ In 2.12.0.0
* random_char() removed.
* random_string() renamed to random_buf().
* early versions added.
+ - New djbunix functions: waitn_posix(), waitn_reap_posix()
In 2.11.2.0
diff --git a/doc/libstddjb/djbunix.html b/doc/libstddjb/djbunix.html
@@ -422,6 +422,12 @@ Returns 1 if it succeeds, and 0 (and sets errno) if it fails. The
</p>
<p>
+<code> int waitn_posix (pid_t *pids, unsigned int n, int *wstat) </code> <br />
+Like <tt>waitn</tt>, but stores into <em>*wstat</em> the status
+of the last process in the <em>pids</em> array (i.e. <tt>pids[n-1]</tt>).
+</p>
+
+<p>
<code> int waitn_reap (pid_t *pids, unsigned int n) </code> <br />
Instantly reaps all zombies whose PIDs are stored in the
<em>pids</em> array, of size <em>n</em>.
@@ -431,6 +437,13 @@ be unchanged.
</p>
<p>
+<code> int waitn_reap_posix (pid_t *pids, unsigned int n, int *wstat) </code> <br />
+Like <tt>waitn_reap</tt>, but stores into <em>*wstat</em> the status
+of the last process in the <em>pids</em> array (i.e. <tt>pids[n-1]</tt>),
+if applicable; otherwise <em>*wstat</em> is unchanged.
+</p>
+
+<p>
<code> pid_t wait_nohang (int *wstat) </code> <br />
Instantly reaps one zombie, and stores the status information into
*<em>wstat</em>.
diff --git a/doc/upgrade.html b/doc/upgrade.html
@@ -25,6 +25,8 @@ as well as <tt>random_char()</tt>. </li>
<li> random functions now have an <tt>early</tt> version, to use when
randomness is required before the generator can be seeded (i.e. in
programs running in the very early boot). </li>
+ <li> New <a href="libstddjb/djbunix.html">djbunix</a> functions:
+<tt>waitn_posix()</tt>, <tt>waitn_reap_posix()</tt> </li>
</ul>
<h2> in 2.11.2.0 </h2>
diff --git a/package/deps.mak b/package/deps.mak
@@ -682,7 +682,9 @@ src/libstddjb/wait_pid_nohang.o src/libstddjb/wait_pid_nohang.lo: src/libstddjb/
src/libstddjb/wait_pids_nohang.o src/libstddjb/wait_pids_nohang.lo: src/libstddjb/wait_pids_nohang.c src/include/skalibs/djbunix.h
src/libstddjb/wait_reap.o src/libstddjb/wait_reap.lo: src/libstddjb/wait_reap.c src/include/skalibs/djbunix.h
src/libstddjb/waitn.o src/libstddjb/waitn.lo: src/libstddjb/waitn.c src/include/skalibs/djbunix.h
+src/libstddjb/waitn_posix.o src/libstddjb/waitn_posix.lo: src/libstddjb/waitn_posix.c src/include/skalibs/djbunix.h
src/libstddjb/waitn_reap.o src/libstddjb/waitn_reap.lo: src/libstddjb/waitn_reap.c src/include/skalibs/djbunix.h
+src/libstddjb/waitn_reap_posix.o src/libstddjb/waitn_reap_posix.lo: src/libstddjb/waitn_reap_posix.c src/include/skalibs/djbunix.h
src/libstddjb/waitpid_nointr.o src/libstddjb/waitpid_nointr.lo: src/libstddjb/waitpid_nointr.c src/include/skalibs/djbunix.h
src/libstddjb/writenclose_unsafe.o src/libstddjb/writenclose_unsafe.lo: src/libstddjb/writenclose_unsafe.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h
src/libstddjb/writevnclose_unsafe.o src/libstddjb/writevnclose_unsafe.lo: src/libstddjb/writevnclose_unsafe.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/siovec.h
diff --git a/src/include/skalibs/djbunix.h b/src/include/skalibs/djbunix.h
@@ -66,9 +66,12 @@ extern pid_t wait_pid_nohang (pid_t, int *) ;
extern int wait_pids_nohang (pid_t const *, unsigned int, int *) ;
#define wait_status(w) (WIFSIGNALED(w) ? 256 + WTERMSIG(w) : WEXITSTATUS(w))
#define wait_estatus(w) (WIFSIGNALED(w) ? 128 + WTERMSIG(w) : WEXITSTATUS(w) >= 128 ? 128 : WEXITSTATUS(w))
+
extern unsigned int wait_reap (void) ;
extern int waitn (pid_t *, unsigned int) ;
+extern int waitn_posix (pid_t *, unsigned int, int *) ;
extern int waitn_reap (pid_t *, unsigned int) ;
+extern int waitn_reap_posix (pid_t *, unsigned int, int *) ;
extern int fd_chdir (int) ;
diff --git a/src/libstddjb/waitn.c b/src/libstddjb/waitn.c
@@ -1,18 +1,11 @@
/* ISC license. */
#include <sys/wait.h>
+
#include <skalibs/djbunix.h>
int waitn (pid_t *pids, unsigned int n)
{
- while (n)
- {
- int wstat ;
- unsigned int i = 0 ;
- pid_t pid = wait_nointr(&wstat) ;
- if (pid < 0) return 0 ;
- for (; i < n ; i++) if (pid == pids[i]) break ;
- if (i < n) pids[i] = pids[--n] ;
- }
- return 1 ;
+ int dummy ;
+ return waitn_posix(pids, n, &dummy) ;
}
diff --git a/src/libstddjb/waitn_posix.c b/src/libstddjb/waitn_posix.c
@@ -0,0 +1,21 @@
+/* ISC license. */
+
+#include <sys/wait.h>
+
+#include <skalibs/djbunix.h>
+
+int waitn_posix (pid_t *pids, unsigned int n, int *w)
+{
+ pid_t wanted = n ? pids[n-1] : 0 ;
+ while (n)
+ {
+ int wstat ;
+ unsigned int i = 0 ;
+ pid_t pid = wait_nointr(&wstat) ;
+ if (pid < 0) return 0 ;
+ for (; i < n ; i++) if (pid == pids[i]) break ;
+ if (i < n) pids[i] = pids[--n] ;
+ if (pid == wanted) *w = wstat ;
+ }
+ return 1 ;
+}
diff --git a/src/libstddjb/waitn_reap.c b/src/libstddjb/waitn_reap.c
@@ -1,19 +1,11 @@
/* ISC license. */
#include <sys/wait.h>
+
#include <skalibs/djbunix.h>
int waitn_reap (pid_t *pids, unsigned int len)
{
- unsigned int n = 0 ;
- while (len)
- {
- int w ;
- int r = wait_pids_nohang(pids, len, &w) ;
- if (r < 0) return r ;
- else if (!r) break ;
- pids[r-1] = pids[--len] ;
- n++ ;
- }
- return n ;
+ int dummy ;
+ return waitn_reap_posix(pids, len, &dummy) ;
}
diff --git a/src/libstddjb/waitn_reap_posix.c b/src/libstddjb/waitn_reap_posix.c
@@ -0,0 +1,22 @@
+/* ISC license. */
+
+#include <sys/wait.h>
+
+#include <skalibs/djbunix.h>
+
+int waitn_reap_posix (pid_t *pids, unsigned int len, int *w)
+{
+ pid_t wanted = len ? pids[len-1] : 0 ;
+ unsigned int n = 0 ;
+ while (len)
+ {
+ int wstat ;
+ int r = wait_pids_nohang(pids, len, &wstat) ;
+ if (r < 0) return r ;
+ else if (!r) break ;
+ if (pids[r-1] == wanted) *w = wstat ;
+ pids[r-1] = pids[--len] ;
+ n++ ;
+ }
+ return n ;
+}