aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2026-09-03 22:42:12 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2026-09-03 22:42:12 +0000
commit4b2cbddf2fdd394d4a91735237b266f9b026123c (patch)
tree50f38c2ff67a761df787b518c104efa0069ddb71 /src
parent233e3ccb96525e7a6af31a60b891badd8d73c5a4 (diff)
downloadskalibs-4b2cbddf2fdd394d4a91735237b266f9b026123c.tar.gz
Add waitpids, waitpids_nohang, refactor wait stuffHEADmain
Also fix fd_cat doc
Diffstat (limited to 'src')
-rw-r--r--src/include/skalibs/djbunix.h15
-rw-r--r--src/libstddjb/waitpids.c20
-rw-r--r--src/libstddjb/waitpids_nohang.c13
3 files changed, 39 insertions, 9 deletions
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 <unistd.h>
+#include <sys/wait.h>
+
+#include <skalibs/djbunix.h>
+
+ /*
+ 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 <unistd.h>
#include <sys/wait.h>
+
#include <skalibs/djbunix.h>
/*
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 ;
}