diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2022-04-15 06:30:46 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2022-04-15 06:30:46 +0000 |
commit | 7d0199963a2ec019270ee187f8c9cb8adb25dac1 (patch) | |
tree | dfee736b516e1e996364a23059d590542196c85c /src | |
parent | 7c6ca159eb3cb6e36cbdfd613547d3b0b99e7684 (diff) | |
download | skalibs-7d0199963a2ec019270ee187f8c9cb8adb25dac1.tar.xz |
Add waitn_posix() and waitn_reap_posix()
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/include/skalibs/djbunix.h | 3 | ||||
-rw-r--r-- | src/libstddjb/waitn.c | 13 | ||||
-rw-r--r-- | src/libstddjb/waitn_posix.c | 21 | ||||
-rw-r--r-- | src/libstddjb/waitn_reap.c | 14 | ||||
-rw-r--r-- | src/libstddjb/waitn_reap_posix.c | 22 |
5 files changed, 52 insertions, 21 deletions
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 <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 new file mode 100644 index 0000000..ea5935d --- /dev/null +++ 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 index 9f26b5d..2c4ebe8 100644 --- 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 new file mode 100644 index 0000000..4a6994f --- /dev/null +++ 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 ; +} |