From 4b2cbddf2fdd394d4a91735237b266f9b026123c Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 3 Sep 2026 22:42:12 +0000 Subject: Add waitpids, waitpids_nohang, refactor wait stuff Also fix fd_cat doc --- src/include/skalibs/djbunix.h | 15 ++++++++++----- src/libstddjb/waitpids.c | 20 ++++++++++++++++++++ src/libstddjb/waitpids_nohang.c | 13 +++++++++---- 3 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 src/libstddjb/waitpids.c (limited to 'src') diff --git a/src/include/skalibs/djbunix.h b/src/include/skalibs/djbunix.h index d6efa09..0bba29a 100644 --- a/src/include/skalibs/djbunix.h +++ b/src/include/skalibs/djbunix.h @@ -59,18 +59,23 @@ extern int openc_write (char const *) ; extern size_t path_canonicalize (char *, char const *, int) ; -extern pid_t wait_nointr (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)) + +/* These do not reap unknown children */ extern pid_t waitpid_nointr (pid_t, int *, int) ; #define wait_pid(pid, wstat) waitpid_nointr(pid, (wstat), 0) #define waitpid_nohang(pid, wstat) waitpid_nointr(pid, (wstat), WNOHANG) +extern int waitpids_nohang (pid_t const *, size_t, int *, size_t *) ; +extern size_t waitpids (pid_t const *, size_t, int *) ; + +/* These reap unknown children */ +extern pid_t wait_nointr (int *) ; #define wait_nohang(wstat) waitpid_nohang(-1, (wstat)) extern pid_t wait_pid_nohang (pid_t, int *) ; extern int wait_pids_nohang (pid_t const *, unsigned int, int *) ; -extern int waitpids_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)) +/* These will disappear or be refactored */ extern unsigned int wait_reap (void) ; extern int waitn (pid_t *, unsigned int) ; extern int waitn_posix (pid_t *, unsigned int, int *) ; diff --git a/src/libstddjb/waitpids.c b/src/libstddjb/waitpids.c new file mode 100644 index 0000000..47c9c34 --- /dev/null +++ b/src/libstddjb/waitpids.c @@ -0,0 +1,20 @@ +/* ISC license. */ + +#include +#include + +#include + + /* + waitpids: blocking wait, don't reap anything unlisted + */ + +size_t waitpids (pid_t const *pids, size_t len, int *wstat) +{ + for (size_t i = 0 ; i < len ; i++) + { + pid_t r = wait_pid(pids[i], wstat) ; + if (r == (pid_t)-1) return i ; + } + return len ; +} diff --git a/src/libstddjb/waitpids_nohang.c b/src/libstddjb/waitpids_nohang.c index 23f03de..e731d4d 100644 --- a/src/libstddjb/waitpids_nohang.c +++ b/src/libstddjb/waitpids_nohang.c @@ -1,19 +1,24 @@ /* ISC license. */ +#include #include + #include /* waitpids_nohang (defined here): only reap pids, don't touch the rest */ -int waitpids_nohang (pid_t const *pids, unsigned int len, int *wstat) +int waitpids_nohang (pid_t const *pids, size_t len, int *wstat, size_t *n) { - for (unsigned int i = 0 ; i < len ; i++) + for (size_t i = 0 ; i < len ; i++) { pid_t r = waitpid_nohang(pids[i], wstat) ; - if (r == -1) return -1-i ; - else if (r == pids[i]) return 1+i ; + if (r == (pid_t)-1 || r == pids[i]) + { + *n = i ; + return r == (pid_t)-1 ? -1 : 1 ; + } } return 0 ; } -- cgit v1.3.1