From 7d0199963a2ec019270ee187f8c9cb8adb25dac1 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 15 Apr 2022 06:30:46 +0000 Subject: Add waitn_posix() and waitn_reap_posix() Signed-off-by: Laurent Bercot --- NEWS | 1 + doc/libstddjb/djbunix.html | 13 +++++++++++++ doc/upgrade.html | 2 ++ package/deps.mak | 2 ++ src/include/skalibs/djbunix.h | 3 +++ src/libstddjb/waitn.c | 13 +++---------- src/libstddjb/waitn_posix.c | 21 +++++++++++++++++++++ src/libstddjb/waitn_reap.c | 14 +++----------- src/libstddjb/waitn_reap_posix.c | 22 ++++++++++++++++++++++ 9 files changed, 70 insertions(+), 21 deletions(-) create mode 100644 src/libstddjb/waitn_posix.c create mode 100644 src/libstddjb/waitn_reap_posix.c diff --git a/NEWS b/NEWS index 273c0bd..203c08b 100644 --- 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 index 36de54b..d1470b4 100644 --- a/doc/libstddjb/djbunix.html +++ b/doc/libstddjb/djbunix.html @@ -421,6 +421,12 @@ Returns 1 if it succeeds, and 0 (and sets errno) if it fails. The pid array is not guaranteed to be unchanged.

+

+ int waitn_posix (pid_t *pids, unsigned int n, int *wstat)
+Like waitn, but stores into *wstat the status +of the last process in the pids array (i.e. pids[n-1]). +

+

int waitn_reap (pid_t *pids, unsigned int n)
Instantly reaps all zombies whose PIDs are stored in the @@ -430,6 +436,13 @@ zombies if it succeeds. The pid array is not guaranteed to be unchanged.

+

+ int waitn_reap_posix (pid_t *pids, unsigned int n, int *wstat)
+Like waitn_reap, but stores into *wstat the status +of the last process in the pids array (i.e. pids[n-1]), +if applicable; otherwise *wstat is unchanged. +

+

pid_t wait_nohang (int *wstat)
Instantly reaps one zombie, and stores the status information into diff --git a/doc/upgrade.html b/doc/upgrade.html index 9e6c272..4dff35b 100644 --- a/doc/upgrade.html +++ b/doc/upgrade.html @@ -25,6 +25,8 @@ as well as random_char().

  • random functions now have an early version, to use when randomness is required before the generator can be seeded (i.e. in programs running in the very early boot).
  • +
  • New djbunix functions: +waitn_posix(), waitn_reap_posix()
  • in 2.11.2.0

    diff --git a/package/deps.mak b/package/deps.mak index 6cc5846..c9db09f 100644 --- 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 index 2d98074..dec232c 100644 --- 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 index 05d7f37..4d980cc 100644 --- a/src/libstddjb/waitn.c +++ b/src/libstddjb/waitn.c @@ -1,18 +1,11 @@ /* ISC license. */ #include + #include 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 new file mode 100644 index 0000000..ea5935d --- /dev/null +++ b/src/libstddjb/waitn_posix.c @@ -0,0 +1,21 @@ +/* ISC license. */ + +#include + +#include + +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 index 9f26b5d..2c4ebe8 100644 --- a/src/libstddjb/waitn_reap.c +++ b/src/libstddjb/waitn_reap.c @@ -1,19 +1,11 @@ /* ISC license. */ #include + #include 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 new file mode 100644 index 0000000..4a6994f --- /dev/null +++ b/src/libstddjb/waitn_reap_posix.c @@ -0,0 +1,22 @@ +/* ISC license. */ + +#include + +#include + +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 ; +} -- cgit v1.2.3