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 --- 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 ++++++++++++++++++++++ 5 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 src/libstddjb/waitn_posix.c create mode 100644 src/libstddjb/waitn_reap_posix.c (limited to 'src') 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