about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-09-09 03:55:38 +0000
committerLaurent Bercot <ska@appnovation.com>2023-09-09 03:55:38 +0000
commit4cce87557feb6933c284d234c448bd8bde4facae (patch)
tree7417e8558b0770da2ae24df8abd755dbc913855f /src
parenta646c96d5f7ec4b4e0bb538a41fc202c2cdb3309 (diff)
downloadskalibs-4cce87557feb6933c284d234c448bd8bde4facae.tar.gz
skalibs-4cce87557feb6933c284d234c448bd8bde4facae.tar.xz
skalibs-4cce87557feb6933c284d234c448bd8bde4facae.zip
cspawn revamp, part 1. Prepare for 2.14.0.0.
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r--src/include/skalibs/cspawn.h81
-rw-r--r--src/include/skalibs/djbunix.h50
-rw-r--r--src/include/skalibs/stddjb.h1
-rw-r--r--src/libstddjb/child_spawn.c198
-rw-r--r--src/libstddjb/child_spawn0.c101
-rw-r--r--src/libstddjb/child_spawn1_internal.c131
-rw-r--r--src/libstddjb/child_spawn1_pipe.c5
-rw-r--r--src/libstddjb/child_spawn1_socket.c4
-rw-r--r--src/libstddjb/child_spawn2.c149
-rw-r--r--src/libstddjb/child_spawn3.c175
-rw-r--r--src/libstddjb/cspawn-internal.h (renamed from src/libstddjb/djbunix-internal.h)4
-rw-r--r--src/libstddjb/cspawn.c220
-rw-r--r--src/libstddjb/cspawn_workaround.c (renamed from src/libstddjb/child_spawn_workaround.c)4
-rw-r--r--src/libstddjb/openslurpclose.c8
-rw-r--r--src/libstddjb/slurp.c8
-rw-r--r--src/libunixonacid/skaclient_startf_async.c1
-rw-r--r--src/libunixonacid/textclient_server_init.c3
-rw-r--r--src/libunixonacid/textclient_server_init_frompipe.c2
-rw-r--r--src/libunixonacid/textclient_startf.c2
19 files changed, 377 insertions, 770 deletions
diff --git a/src/include/skalibs/cspawn.h b/src/include/skalibs/cspawn.h
new file mode 100644
index 0000000..f76fa62
--- /dev/null
+++ b/src/include/skalibs/cspawn.h
@@ -0,0 +1,81 @@
+/* ISC license. */
+
+#ifndef SKALIBS_CSPAWN_H
+#define SKALIBS_CSPAWN_H
+
+#include <sys/types.h>
+#include <stdint.h>
+
+#define CSPAWN_FLAGS_SELFPIPE_FINISH 0x0001U
+#define CSPAWN_FLAGS_SIGBLOCKNONE 0x0002U
+
+#define CSPAWN_FA_CLOSE 0x0000U
+#define CSPAWN_FA_COPY 0x0001U
+#define CSPAWN_FA_MOVE 0x0002U
+#define CSPAWN_FA_OPEN 0x0003U
+
+struct cspawn_fa_openinfo_s
+{
+  int fd ;
+  char const *file ;
+  int oflag ;
+  mode_t mode ;
+} ;
+
+union cspawn_fileaction_u
+{
+  int fd ;
+  int fd2[2] ;
+  struct cspawn_fa_openinfo_s openinfo ;
+} ;
+
+typedef struct cspawn_fileaction_s cspawn_fileaction, *cspawn_fileaction_ref ;
+struct cspawn_fileaction_s
+{
+  uint32_t type ;
+  union cspawn_fileaction_u x ;
+} ;
+
+
+ /* Generic interface for posix_spawn() with a fork()+execve() fallback */
+
+extern pid_t cspawn (char const *, char const *const *, char const *const *, uint32_t, cspawn_fileaction const *, size_t) ;
+
+
+ /* Simple spawn functions with 0 or 1 communicating fds. */
+
+extern pid_t child_spawn0 (char const *, char const *const *, char const *const *) ;
+extern pid_t child_spawn1_pipe (char const *, char const *const *, char const *const *, int *, int) ;
+extern pid_t child_spawn1_socket (char const *, char const *const *, char const *const *, int *) ;
+
+
+ /*
+    Spawn function with 2 communicating pipes. The int * points to 2 fds.
+    Input: fds[0] and fds[1] are the fds to move the pipes to in the child.
+    Output: fds[0] and fds[1] contain the pipes to the child.
+ */
+
+extern pid_t child_spawn2 (char const *, char const *const *, char const *const *, int *) ;
+
+
+ /*
+    Same, with an additional pipe from the child to the parent.
+    The int * points to 3 fds.
+    The additional fd# is available to the child in the defined env variable.
+ */
+
+#define SKALIBS_CHILD_SPAWN_FDS_ENVVAR "SKALIBS_CHILD_SPAWN_FDS"
+
+extern pid_t child_spawn3 (char const *, char const *const *, char const *const *, int *) ;
+
+
+ /*
+    Generalization of the previous functions.
+    * requests n (the last arg) communication fds between parent and child. Uses pipes.
+    * if n=1, equivalent to child_spawn1_pipe; child writes, parent reads.
+    * if n>=2, parent reads on even and writes on odd.
+ */
+
+extern pid_t child_spawn (char const *, char const *const *, char const *const *, int *, size_t) ;
+
+#endif
diff --git a/src/include/skalibs/djbunix.h b/src/include/skalibs/djbunix.h
index 1ac2d03..b95a5ab 100644
--- a/src/include/skalibs/djbunix.h
+++ b/src/include/skalibs/djbunix.h
@@ -83,13 +83,8 @@ extern int sagetcwd (stralloc *) ;
 extern int sareadlink (stralloc *, char const *) ;
 extern int sagethostname (stralloc *) ;
 
-extern int slurp (stralloc *, int) ;
-extern int openslurpclose (stralloc *, char const *) ;
-/*
-  TODO: next ABI break: change to
 #define slurp(sa, fd) slurpn((fd), (sa), 0)
 #define openslurpclose(sa, fn) openslurpnclose((fn), (sa), 0)
-*/
 
 extern int slurpn (int, stralloc *, size_t) ;
 extern int openslurpnclose (char const *, stralloc *, size_t) ;
@@ -169,49 +164,4 @@ extern int hiercopy_loose (char const *, char const *) ;
 extern int hiercopy_loose_tmp (char const *, char const *, stralloc *) ;
 extern int hiercopy_internal (char const *, char const *, stralloc *, unsigned int) ;
 
-
-
- /* Simple spawn functions with 0 or 1 communicating fds. */
-
-extern pid_t child_spawn0 (char const *, char const *const *, char const *const *) ;
-extern pid_t child_spawn1_pipe (char const *, char const *const *, char const *const *, int *, int) ;
-extern pid_t child_spawn1_socket (char const *, char const *const *, char const *const *, int *) ;
-
-
- /*
-    Spawn function with 2 communicating pipes. The int * points to 2 fds.
-    Input: fds[0] and fds[1] are the fds to move the pipes to in the child.
-    Output: fds[0] and fds[1] contain the pipes to the child.
- */
-
-extern pid_t child_spawn2 (char const *, char const *const *, char const *const *, int *) ;
-
-
- /*
-    Same, with an additional pipe from the child to the parent.
-    The int * points to 3 fds.
-    The additional fd# is available to the child in the defined env variable.
- */
-
-#define SKALIBS_CHILD_SPAWN_FDS_ENVVAR "SKALIBS_CHILD_SPAWN_FDS"
-
-extern pid_t child_spawn3 (char const *, char const *const *, char const *const *, int *) ;
-
-
- /*
-    Generalization of the previous functions.
-    * uses posix_spawn() if available, else uses fork+exec
-    * requests n (the last arg) communication fds between parent and child. Uses pipes.
-    * if n=1, equivalent to child_spawn1_pipe; child writes, parent reads.
-    * if n>=2, parent reads on even and writes on odd.
- */
-
-extern pid_t child_spawn (char const *, char const *const *, char const *const *, int *, unsigned int) ;
-
-
- /* Work around buggy posix_spawn */
-
-extern pid_t child_spawn_workaround (pid_t, int const *) ;  /* closes the pipe if defined */
-
-
 #endif
diff --git a/src/include/skalibs/stddjb.h b/src/include/skalibs/stddjb.h
index 3ba1a51..d241de8 100644
--- a/src/include/skalibs/stddjb.h
+++ b/src/include/skalibs/stddjb.h
@@ -20,6 +20,7 @@
 #include <skalibs/cbuffer.h>
 #include <skalibs/cdb.h>
 #include <skalibs/cdbmake.h>
+#include <skalibs/cspawn.h>
 #include <skalibs/devino.h>
 #include <skalibs/direntry.h>
 #include <skalibs/diuint32.h>
diff --git a/src/libstddjb/child_spawn.c b/src/libstddjb/child_spawn.c
index 7c59fba..fb5be27 100644
--- a/src/libstddjb/child_spawn.c
+++ b/src/libstddjb/child_spawn.c
@@ -1,208 +1,56 @@
 /* ISC license. */
 
-/* MT-unsafe */
-
-#include <skalibs/sysdeps.h>
-
 #include <string.h>
-#include <errno.h>
 #include <unistd.h>
 
 #include <skalibs/types.h>
 #include <skalibs/djbunix.h>
-
-#ifdef SKALIBS_HASPOSIXSPAWN
-
-#include <signal.h>
-#include <stdlib.h>
-#include <spawn.h>
-
-#include <skalibs/config.h>
 #include <skalibs/env.h>
+#include <skalibs/cspawn.h>
 
-pid_t child_spawn (char const *prog, char const *const *argv, char const *const *envp, int *fds, unsigned int n)
+pid_t child_spawn (char const *prog, char const *const *argv, char const *const *envp, int *fds, size_t n)
 {
   pid_t pid ;
-  posix_spawn_file_actions_t actions ;
-  posix_spawnattr_t attr ;
-  sigset_t set ;
-  int e ;
-  int p[n ? n : 1][2] ;
+  cspawn_fileaction fa[2] =
+  {
+    [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = 1 } } },
+    [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = 0 } } }
+  } ;
   size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ;
-  unsigned int i = 0 ;
+  size_t envlen = env_len(envp) ;
+  size_t i = 0 ;
+  int p[n ? n : 1][2] ;
+  char const *newenv[envlen + 2] ;
   char modifs[m + 1 + n * UINT_FMT] ;
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  int syncpipe[2] ;
-  if (pipecoe(syncpipe) == -1) return 0 ;
-#endif
 
-  memcpy(modifs, SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=", sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR)) ;
   for (; i < n ; i++)
   {
     if (pipe(p[i]) == -1) goto errpi ;
-    if ((ndelay_on(p[i][i & 1]) == -1) || (coe(p[i][i & 1]) == -1))
-    {
-      i++ ; goto errpi ;
-    }
-  }
-  for (i = 2 ; i < n ; i++)
-  {
-    m += uint_fmt(modifs + m, p[i][!(i & 1)]) ;
-    if (i+1 < n) modifs[m++] = ',' ;
-  }
-  modifs[m++] = 0 ;
-
-  e = posix_spawnattr_init(&attr) ;
-  if (e) goto erre ;
-  sigemptyset(&set) ;
-  e = posix_spawnattr_setsigmask(&attr, &set) ;
-  if (e) goto errattr ;
-  e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ;
-  if (e) goto errattr ;
-  e = posix_spawn_file_actions_init(&actions) ;
-  if (e) goto errattr ;
-  if (n >= 2 && p[1][0])
-  {
-    e = posix_spawn_file_actions_adddup2(&actions, p[1][0], 0) ;
-    if (e) goto erractions ;
-    e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ;
-    if (e) goto erractions ;
-  }
-  if (n && p[0][1] != 1)
-  {
-    e = posix_spawn_file_actions_adddup2(&actions, p[0][1], 1) ;
-    if (e) goto erractions ;
-    e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ;
-    if (e) goto erractions ;
-  }
-  {
-    int nopath = !getenv("PATH") ;
-    size_t envlen = env_len(envp) ;
-    char const *newenv[envlen + 2] ;
-    if (!env_mergen(newenv, envlen+2, envp, envlen, modifs, m, 1)) goto erractions ;
-    if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0))
-    {
-      e = errno ; goto erractions ;
-    }
-    e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)newenv) ;
-    if (nopath) unsetenv("PATH") ;
-    if (e) goto erractions ;
-  }
-
-  posix_spawn_file_actions_destroy(&actions) ;
-  posix_spawnattr_destroy(&attr) ;
-  for (i = 0 ; i < n ; i++) fd_close(p[i][!(i & 1)]) ;
-
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  pid = child_spawn_workaround(pid, syncpipe) ;
-  if (!pid)
-  {
-    for (i = 0 ; i < n ; i++) fd_close(p[i][i & 1]) ;
-    return 0 ;
-  }
-#endif
-  for (i = 0 ; i < n ; i++) fds[i] = p[i][i & 1] ;
-  return pid ;
-
- erractions:
-  posix_spawn_file_actions_destroy(&actions) ;
- errattr:
-  posix_spawnattr_destroy(&attr) ;
- erre:
-  errno = e ;
-  i = n ;
- errpi:
-  while (i--)
-  {
-    fd_close(p[i][1]) ;
-    fd_close(p[i][0]) ;
+    if ((ndelay_on(p[i][i & 1]) == -1) || (coe(p[i][i & 1]) == -1)) goto errpip ;
   }
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  fd_close(syncpipe[1]) ;
-  fd_close(syncpipe[0]) ;
-#endif
-  return 0 ;
-}
-
-#else
-
-#include <skalibs/allreadwrite.h>
-#include <skalibs/sig.h>
-#include <skalibs/exec.h>
-
-pid_t child_spawn (char const *prog, char const *const *argv, char const *const *envp, int *fds, unsigned int n)
-{
-  pid_t pid ;
-  int syncpipe[2] ;
-  int p[n ? n : 1][2] ;
-  size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ;
-  unsigned int i = 0 ;
-  char modifs[m + 1 + n * UINT_FMT] ;
-  char c ;
 
   memcpy(modifs, SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=", sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR)) ;
-  for (; i < n ; i++)
-  {
-    if (pipe(p[i]) == -1) goto errpi ;
-    if ((ndelay_on(p[i][i & 1]) == -1) || (coe(p[i][i & 1]) == -1))
-    {
-      i++ ; goto errpi ;
-    }
-  }
   for (i = 2 ; i < n ; i++)
   {
     m += uint_fmt(modifs + m, p[i][!(i & 1)]) ;
     if (i+1 < n) modifs[m++] = ',' ;
   }
   modifs[m++] = 0 ;
+  if (!env_mergen(newenv, envlen + 2, envp, envlen, modifs, m, 1)) goto errpi ;
+  if (n) fa[0].x.fd2[1] = p[0][1] ;
+  if (n >= 2) fa[1].x.fd2[1] = p[1][0] ;
+  pid = cspawn(prog, argv, newenv, CSPAWN_FLAGS_SIGBLOCKNONE, fa, n < 2 ? n : 2) ;
+  if (!pid) goto errpi ;
 
-  if (pipecoe(syncpipe) < 0) goto errp ;
-
-  pid = fork() ;
-  if (pid < 0) goto errsp ;
-  else if (!pid)
+  for (i = 0 ; i < n ; i++)
   {
-    if (n >= 2)
-    {
-      if (fd_move2(0, p[1][0], 1, p[0][1]) == -1) goto syncdie ;
-    }
-    else if (n)
-    {
-      if (fd_move(1, p[0][1]) == -1) goto syncdie ;
-    }
-    sig_blocknone() ;
-    mexec_aen(prog, argv, envp, modifs, m, 1) ;
-  syncdie:
-    c = errno ;
-    fd_write(syncpipe[1], &c, 1) ;
-    _exit(127) ;
-  }
-
-  fd_close(syncpipe[1]) ;
-  syncpipe[1] = fd_read(syncpipe[0], &c, 1) ;
-  if (syncpipe[1])
-  {
-    int e = (unsigned char)c ;
-    if (syncpipe[1] == -1) e = errno ;
-    if (wait_pid(pid, 0) == -1) e = errno ;
-    errno = e ;
-    goto errsp0 ;
-  }
-  fd_close(syncpipe[0]) ;
-
-  for (i = n ; i ; i--)
-  {
-    fd_close(p[i-1][i & 1]) ;
-    fds[i-1] = p[i-1][!(i & 1)] ;
+    fd_close(p[i][!(i & 1)]) ;
+    fds[i] = p[i][i & 1] ;
   }
   return pid ;
 
- errsp:
-  fd_close(syncpipe[1]) ;
- errsp0:
-  fd_close(syncpipe[0]) ;
- errp:
-  i = n ;
+ errpip:
+  i++ ;
  errpi:
   while (i--)
   {
@@ -211,5 +59,3 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const
   }
   return 0 ;
 }
-
-#endif
diff --git a/src/libstddjb/child_spawn0.c b/src/libstddjb/child_spawn0.c
index ea7a5b1..b1353b6 100644
--- a/src/libstddjb/child_spawn0.c
+++ b/src/libstddjb/child_spawn0.c
@@ -1,105 +1,8 @@
 /* ISC license. */
 
-#include <skalibs/sysdeps.h>
-
-#include <errno.h>
-
-#ifdef SKALIBS_HASPOSIXSPAWN
-
-#include <signal.h>
-#include <stdlib.h>
-#include <spawn.h>
-
-#include <skalibs/config.h>
-#include <skalibs/djbunix.h>
+#include <skalibs/cspawn.h>
 
 pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const *envp)
 {
-  pid_t pid ;
-  posix_spawnattr_t attr ;
-  sigset_t set ;
-  int e ;
-  int nopath = !getenv("PATH") ;
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  int p[2] ;
-  if (pipecoe(p) == -1) return 0 ;
-#endif
-  e = posix_spawnattr_init(&attr) ;
-  if (e) goto err ;
-  sigemptyset(&set) ;
-  e = posix_spawnattr_setsigmask(&attr, &set) ;
-  if (e) goto errattr ;
-  e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ;
-  if (e) goto errattr ;
-  if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; goto errattr ; }
-  e = posix_spawnp(&pid, prog, 0, &attr, (char *const *)argv, (char *const *)envp) ;
-  if (nopath) unsetenv("PATH") ;
-  if (e) goto errattr ;
-
-  posix_spawnattr_destroy(&attr) ;
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  return child_spawn_workaround(pid, p) ;
-#else
-  return pid ;
-#endif
-
- errattr:
-  posix_spawnattr_destroy(&attr) ;
- err:
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  fd_close(p[1]) ;
-  fd_close(p[0]) ;
-#endif
-  errno = e ;
-  return 0 ;
+  return cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, 0, 0) ;
 }
-
-#else
-
-#include <unistd.h>
-
-#include <skalibs/allreadwrite.h>
-#include <skalibs/sig.h>
-#include <skalibs/djbunix.h>
-#include <skalibs/exec.h>
-
-pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const *envp)
-{
-  pid_t pid ;
-  int p[2] ;
-  char c ;
-
-  if (pipecoe(p) == -1) return 0 ;
-  pid = fork() ;
-  if (pid < 0)
-  {
-    fd_close(p[1]) ;
-    fd_close(p[0]) ;
-    return 0 ;
-  }
-  if (!pid)
-  {
-    sig_blocknone() ;
-    exec_ae(prog, argv, envp) ;
-    c = errno ;
-    fd_write(p[1], &c, 1) ;
-    _exit(127) ;
-  }
-  fd_close(p[1]) ;
-  p[1] = fd_read(p[0], &c, 1) ;
-  if (p[1] < 0)
-  {
-    fd_close(p[0]) ;
-    return 0 ;
-  }
-  fd_close(p[0]) ;
-  if (p[1])
-  {
-    wait_pid(pid, &p[0]) ;
-    errno = (unsigned char)c ;
-    return 0 ;
-  }
-  return pid ;
-}
-
-#endif
diff --git a/src/libstddjb/child_spawn1_internal.c b/src/libstddjb/child_spawn1_internal.c
index 9e6eeff..e65b588 100644
--- a/src/libstddjb/child_spawn1_internal.c
+++ b/src/libstddjb/child_spawn1_internal.c
@@ -1,140 +1,27 @@
 /* ISC license. */
 
-#include <skalibs/sysdeps.h>
-
-#include <errno.h>
-
 #include <skalibs/djbunix.h>
-
-#ifdef SKALIBS_HASPOSIXSPAWN
-
-#include <signal.h>
-#include <spawn.h>
-#include <stdlib.h>
-
-#include <skalibs/config.h>
+#include <skalibs/cspawn.h>
+#include "cspawn-internal.h"
 
 pid_t child_spawn1_internal (char const *prog, char const *const *argv, char const *const *envp, int *p, int to)
 {
   pid_t pid ;
-  posix_spawn_file_actions_t actions ;
-  posix_spawnattr_t attr ;
-  sigset_t set ;
-  int e ;
-  int nopath = !getenv("PATH") ;
-
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  int syncpipe[2] ;
-  if (pipecoe(syncpipe) == -1) goto err ;
-#endif
-
-  if (coe(p[!(to & 1)]) == -1) goto errp ;
-  e = posix_spawnattr_init(&attr) ;
-  if (e) goto erre ;
-  sigemptyset(&set) ;
-  e = posix_spawnattr_setsigmask(&attr, &set) ;
-  if (e) goto errattr ;
-  e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ;
-  if (e) goto errattr ;
-  e = posix_spawn_file_actions_init(&actions) ;
-  if (e) goto errattr ;
-  if (p[to & 1] != (to & 1))
-  {
-    e = posix_spawn_file_actions_adddup2(&actions, p[to & 1], to & 1) ;
-    if (e) goto erractions ;
-    e = posix_spawn_file_actions_addclose(&actions, p[to & 1]) ;
-    if (e) goto erractions ;
-  }
-  if (to & 2)
+  cspawn_fileaction fa[3] =
   {
-    e = posix_spawn_file_actions_adddup2(&actions, to & 1, !(to & 1)) ;
-    if (e) goto erractions ;
-  }
-  if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; goto erractions ; }
-  e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)envp) ;
-  if (nopath) unsetenv("PATH") ;
-  if (e) goto erractions ;
+    [0] = { .type = CSPAWN_FA_CLOSE, .x = { .fd = p[!(to & 1)]} },
+    [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = to & 1, [1] = p[to & 1] } } },
+    [2] = { .type = CSPAWN_FA_COPY, .x = { .fd2 = { [0] = !(to & 1), [1] = to & 1 } } }
+  } ;
 
-  posix_spawn_file_actions_destroy(&actions) ;
-  posix_spawnattr_destroy(&attr) ;
-  fd_close(p[to & 1]) ;
-
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  return child_spawn_workaround(pid, syncpipe) ;
-#else
-  return pid ;
-#endif
+  pid = cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2 + !!(to & 2)) ;
+  if (!pid) goto err ;
 
- erractions:
-  posix_spawn_file_actions_destroy(&actions) ;
- errattr:
-  posix_spawnattr_destroy(&attr) ;
- erre:
-  errno = e ;
- errp:
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  fd_close(syncpipe[1]) ;
-  fd_close(syncpipe[0]) ;
- err:
-#endif
-  fd_close(p[1]) ;
-  fd_close(p[0]) ;
-  return 0 ;
-}
-
-#else
-
-#include <unistd.h>
-
-#include <skalibs/allreadwrite.h>
-#include <skalibs/sig.h>
-#include <skalibs/exec.h>
-
-pid_t child_spawn1_internal (char const *prog, char const *const *argv, char const *const *envp, int *p, int to)
-{
-  pid_t pid ;
-  int syncpipe[2] ;
-  char c ;
-
-  if (coe(p[0]) < 0 || pipecoe(syncpipe) < 0) goto err ;
-
-  pid = fork() ;
-  if (pid < 0) goto errsp ;
-  if (!pid)
-  {
-    fd_close(p[!(to & 1)]) ;
-    if (fd_move(to & 1, p[to & 1]) < 0) goto syncdie ;
-    if ((to & 2) && (fd_copy(!(to & 1), to & 1) < 0)) goto syncdie ;
-    sig_blocknone() ;
-    exec_ae(prog, argv, envp) ;
-   syncdie:
-    c = errno ;
-    fd_write(syncpipe[1], &c, 1) ;
-    _exit(127) ;
-  }
-
-  fd_close(syncpipe[1]) ;
-  syncpipe[1] = fd_read(syncpipe[0], &c, 1) ;
-  if (syncpipe[1])
-  {
-    int e = (unsigned char)c ;
-    if (syncpipe[1] == -1) e = errno ;
-    if (wait_pid(pid, 0) == -1) e = errno ;
-    errno = e ;
-    goto errsp0 ;
-  }
-  fd_close(syncpipe[0]) ;
   fd_close(p[to & 1]) ;
   return pid ;
 
- errsp:
-  fd_close(syncpipe[1]) ;
- errsp0:
-  fd_close(syncpipe[0]) ;
  err:
   fd_close(p[1]) ;
   fd_close(p[0]) ;
   return 0 ;
 }
-
-#endif
diff --git a/src/libstddjb/child_spawn1_pipe.c b/src/libstddjb/child_spawn1_pipe.c
index a436f6e..a31e091 100644
--- a/src/libstddjb/child_spawn1_pipe.c
+++ b/src/libstddjb/child_spawn1_pipe.c
@@ -1,8 +1,9 @@
 /* ISC license. */
 
 #include <unistd.h>
-#include <skalibs/djbunix.h>
-#include "djbunix-internal.h"
+
+#include <skalibs/cspawn.h>
+#include "cspawn-internal.h"
 
 pid_t child_spawn1_pipe (char const *prog, char const *const *argv, char const *const *envp, int *fd, int to)
 {
diff --git a/src/libstddjb/child_spawn1_socket.c b/src/libstddjb/child_spawn1_socket.c
index 0bb2da9..684842e 100644
--- a/src/libstddjb/child_spawn1_socket.c
+++ b/src/libstddjb/child_spawn1_socket.c
@@ -1,8 +1,8 @@
 /* ISC license. */
 
 #include <skalibs/socket.h>
-#include <skalibs/djbunix.h>
-#include "djbunix-internal.h"
+#include <skalibs/cspawn.h>
+#include "cspawn-internal.h"
 
 pid_t child_spawn1_socket (char const *prog, char const *const *argv, char const *const *envp, int *fd)
 {
diff --git a/src/libstddjb/child_spawn2.c b/src/libstddjb/child_spawn2.c
index 986fd15..f287c0b 100644
--- a/src/libstddjb/child_spawn2.c
+++ b/src/libstddjb/child_spawn2.c
@@ -1,162 +1,35 @@
 /* ISC license. */
 
-/* MT-unsafe */
-
-#include <skalibs/sysdeps.h>
-
 #include <unistd.h>
-#include <errno.h>
 
 #include <skalibs/djbunix.h>
-
-#ifdef SKALIBS_HASPOSIXSPAWN
-
-#include <signal.h>
-#include <stdlib.h>
-#include <spawn.h>
-
-#include <skalibs/config.h>
+#include <skalibs/cspawn.h>
 
 pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const *envp, int *fds)
 {
   pid_t pid ;
-  posix_spawn_file_actions_t actions ;
-  posix_spawnattr_t attr ;
   int p[2][2] ;
-  int e ;
-  int nopath = !getenv("PATH") ;
-  sigset_t set ;
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  int syncpipe[2] ;
-  if (pipecoe(syncpipe) == -1) return 0 ;
-#endif
-  if (pipe(p[0]) == -1) goto err ;
+  if (pipe(p[0]) == -1) return 0 ;
   if (ndelay_on(p[0][0]) == -1 || coe(p[0][0]) == -1 || pipe(p[1]) == -1) goto errp ;
-  if (ndelay_on(p[1][1]) == -1 || coe(p[1][1]) == -1) goto errp1 ;
+  if (ndelay_on(p[1][1]) == -1 || coe(p[1][1]) == -1) goto err ;
 
-  e = posix_spawnattr_init(&attr) ;
-  if (e) goto erre ;
-  sigemptyset(&set) ;
-  e = posix_spawnattr_setsigmask(&attr, &set) ;
-  if (e) goto errattr ;
-  e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ;
-  if (e) goto errattr ;
-  e = posix_spawn_file_actions_init(&actions) ;
-  if (e) goto errattr ;
-  if (p[1][0] != fds[0])
   {
-    e = posix_spawn_file_actions_adddup2(&actions, p[1][0], fds[0]) ;
-    if (e) goto erractions ;
-    e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ;
-    if (e) goto erractions ;
+    cspawn_fileaction fa[2] =
+    {
+      [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[0], [1] = p[1][0] } } },
+      [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[1], [1] = p[0][1] } } }
+    } ;
+    pid = cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2) ;
+    if (!pid) goto err ;
   }
-  if (p[0][1] != fds[1])
-  {
-    e = posix_spawn_file_actions_adddup2(&actions, p[0][1], fds[1]) ;
-    if (e) goto erractions ;
-    e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ;
-    if (e) goto erractions ;
-  }
-  if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) == -1))
-  {
-    e = errno ; goto erractions ;
-  }
-  e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)envp) ;
-  if (nopath) unsetenv("PATH") ;
-  if (e) goto erractions ;
 
-  posix_spawn_file_actions_destroy(&actions) ;
-  posix_spawnattr_destroy(&attr) ;
   fd_close(p[0][1]) ;
   fd_close(p[1][0]) ;
-
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  pid = child_spawn_workaround(pid, syncpipe) ;
-  if (!pid)
-  {
-    fd_close(p[1][1]) ;
-    fd_close(p[0][0]) ;
-    return 0 ;
-  }
-#endif
   fds[0] = p[0][0] ;
   fds[1] = p[1][1] ;
   return pid ;
 
- erractions:
-  posix_spawn_file_actions_destroy(&actions) ;
- errattr:
-  posix_spawnattr_destroy(&attr) ;
- erre:
-  errno = e ;
- errp1:
-  fd_close(p[1][1]) ;
-  fd_close(p[1][0]) ;
- errp:
-  fd_close(p[0][1]) ;
-  fd_close(p[0][0]) ;
  err:
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  fd_close(syncpipe[1]) ;
-  fd_close(syncpipe[0]) ;
-#endif
-  return 0 ;
-}
-
-#else
-
-#include <skalibs/allreadwrite.h>
-#include <skalibs/sig.h>
-#include <skalibs/exec.h>
-
-pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const *envp, int *fds)
-{
-  pid_t pid ;
-  int syncpipe[2] ;
-  int p[2][2] ;
-  char c ;
-
-  if (pipe(p[0]) < 0) return 0 ;
-  if (ndelay_on(p[0][0]) < 0 || coe(p[0][0]) < 0 || pipe(p[1]) < 0) goto errp ;
-  if (ndelay_on(p[1][1]) < 0 || coe(p[1][1]) < 0) goto errp1 ;
-  if (pipecoe(syncpipe) < 0) goto errp1 ;
-
-  pid = fork() ;
-  if (pid == -1) goto errsp ;
-  else if (!pid)
-  {
-    if (fd_move2(fds[0], p[1][0], fds[1], p[0][1]) < 0) goto syncdie ;
-    sig_blocknone() ;
-    exec_ae(prog, argv, envp) ;
-   syncdie:
-    c = errno ;
-    fd_write(syncpipe[1], &c, 1) ;
-    _exit(127) ;
-  }
-
-  fd_close(syncpipe[1]) ;
-  syncpipe[1] = fd_read(syncpipe[0], &c, 1) ;
-  if (syncpipe[1])
-  {
-    int e = (unsigned char)c ;
-    if (syncpipe[1] == -1) e = errno ;
-    if (wait_pid(pid, &syncpipe[1]) == -1) e = errno ;
-    errno = e ;
-    goto errsp0 ;
-  }
-  fd_close(syncpipe[0]) ;
-  fd_close(p[0][1]) ;
-  fd_close(p[1][0]) ;
-
-  fds[0] = p[0][0] ;
-  fds[1] = p[1][1] ;
-  return pid ;
-
- errsp:
-  fd_close(syncpipe[1]) ;
- errsp0:
-  fd_close(syncpipe[0]) ;
- errp1:
   fd_close(p[1][1]) ;
   fd_close(p[1][0]) ;
  errp:
@@ -164,5 +37,3 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const
   fd_close(p[0][0]) ;
   return 0 ;
 }
-
-#endif
diff --git a/src/libstddjb/child_spawn3.c b/src/libstddjb/child_spawn3.c
index 6eb7519..932e5ab 100644
--- a/src/libstddjb/child_spawn3.c
+++ b/src/libstddjb/child_spawn3.c
@@ -1,185 +1,48 @@
 /* ISC license. */
 
-/* MT-unsafe */
-
-#include <skalibs/sysdeps.h>
-
 #include <unistd.h>
-#include <errno.h>
 
 #include <skalibs/types.h>
-#include <skalibs/djbunix.h>
-
-#ifdef SKALIBS_HASPOSIXSPAWN
-
-#include <signal.h>
-#include <stdlib.h>
-#include <spawn.h>
-
-#include <skalibs/config.h>
 #include <skalibs/env.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/cspawn.h>
 
 pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const *envp, int *fds)
 {
   pid_t pid ;
-  posix_spawn_file_actions_t actions ;
-  posix_spawnattr_t attr ;
-  sigset_t set ;
-  int e ;
   int p[3][2] ;
-  size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ;
-  char modifs[sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) + UINT_FMT] = SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=" ;
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  int syncpipe[2] ;
-  if (pipecoe(syncpipe) == -1) return 0 ;
-#endif
 
-  if (pipe(p[0]) < 0 || ndelay_on(p[0][0]) < 0 || coe(p[0][0]) < 0) goto err ;
-  if (pipe(p[1]) < 0 || ndelay_on(p[1][1]) < 0 || coe(p[1][1]) < 0) goto errp0 ;
-  if (pipe(p[2]) < 0 || ndelay_on(p[2][0]) < 0 || coe(p[2][0]) < 0) goto errp1 ;
-  m += uint_fmt(modifs + sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR), p[2][1]) ;
-  modifs[m++] = 0 ;
+  if (pipe(p[0]) == -1) return 0 ;
+  if (ndelay_on(p[0][0]) == -1 || coe(p[0][0]) == -1 || pipe(p[1]) == -1) goto errp0 ;
+  if (ndelay_on(p[1][1]) == -1 || coe(p[1][1]) == -1 || pipe(p[2]) == -1) goto errp1 ;
+  if (ndelay_on(p[2][0]) == -1 || coe(p[2][0]) == -1) goto err ;
 
-  e = posix_spawnattr_init(&attr) ;
-  if (e) goto erre ;
-  sigemptyset(&set) ;
-  e = posix_spawnattr_setsigmask(&attr, &set) ;
-  if (e) goto errattr ;
-  e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ;
-  if (e) goto errattr ;
-  e = posix_spawn_file_actions_init(&actions) ;
-  if (e) goto errattr ;
-  if (p[1][0] != fds[0])
   {
-    e = posix_spawn_file_actions_adddup2(&actions, p[1][0], fds[0]) ;
-    if (e) goto erractions ;
-    e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ;
-    if (e) goto erractions ;
-  }
-  if (p[0][1] != fds[1])
-  {
-    e = posix_spawn_file_actions_adddup2(&actions, p[0][1], fds[1]) ;
-    if (e) goto erractions ;
-    e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ;
-    if (e) goto erractions ;
-  }
-  { 
-    int nopath = !getenv("PATH") ;
+    cspawn_fileaction fa[2] =
+    {
+      [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[0], [1] = p[1][0] } } },
+      [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[1], [1] = p[0][1] } } }
+    } ;
     size_t envlen = env_len(envp) ;
+    size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ;
+    char modifs[sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) + UINT_FMT] = SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=" ;
     char const *newenv[envlen + 2] ;
-    if (!env_mergen(newenv, envlen+2, envp, envlen, modifs, m, 1)) goto erractions ;
-    if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0))
-    {
-      e = errno ; goto erractions ;
-    }
-    e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)newenv) ;
-    if (nopath) unsetenv("PATH") ;
-    if (e) goto erractions ;
+    m += uint_fmt(modifs + sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR), p[2][1]) ;
+    modifs[m++] = 0 ;
+    if (!env_mergen(newenv, envlen + 2, envp, envlen, modifs, m, 1)) goto err ;
+    pid = cspawn(prog, argv, newenv, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2) ;
+    if (!pid) goto err ;
   }
 
-  posix_spawn_file_actions_destroy(&actions) ;
-  posix_spawnattr_destroy(&attr) ;
   fd_close(p[2][1]) ;
   fd_close(p[1][0]) ;
   fd_close(p[0][1]) ;
-
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  pid = child_spawn_workaround(pid, syncpipe) ;
-  if (!pid)
-  {
-    fd_close(p[2][0]) ;
-    fd_close(p[1][1]) ;
-    fd_close(p[0][0]) ;
-    return 0 ;
-  }
-#endif
   fds[0] = p[0][0] ;
   fds[1] = p[1][1] ;
   fds[2] = p[2][0] ;
   return pid ;
 
- erractions:
-  posix_spawn_file_actions_destroy(&actions) ;
- errattr:
-  posix_spawnattr_destroy(&attr) ;
- erre:
-  errno = e ;
-  fd_close(p[2][1]) ;
-  fd_close(p[2][0]) ;
- errp1:
-  fd_close(p[1][1]) ;
-  fd_close(p[1][0]) ;
- errp0:
-  fd_close(p[0][1]) ;
-  fd_close(p[0][0]) ;
  err:
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  fd_close(syncpipe[1]) ;
-  fd_close(syncpipe[0]) ;
-#endif
-  return 0 ;
-}
-
-#else
-
-#include <skalibs/allreadwrite.h>
-#include <skalibs/sig.h>
-#include <skalibs/exec.h>
-
-pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const *envp, int *fds)
-{
-  pid_t pid ;
-  int syncpipe[2] ;
-  int p[3][2] ;
-  size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ;
-  char modifs[sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) + UINT_FMT] = SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=" ;
-  char c ;
-
-  if (pipe(p[0]) == -1 || ndelay_on(p[0][0]) == -1 || coe(p[0][0]) == -1) return 0 ;
-  if (pipe(p[1]) == -1 || ndelay_on(p[1][1]) == -1 || coe(p[1][1]) == -1) goto errp0 ;
-  if (pipe(p[2]) == -1 || ndelay_on(p[2][0]) == -1 || coe(p[2][0]) == -1) goto errp1 ;
-  m += uint_fmt(modifs + sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR), p[2][1]) ;
-  modifs[m++] = 0 ;
-  if (pipecoe(syncpipe) < 0) goto errp2 ;
-
-  pid = fork() ;
-  if (pid < 0) goto errsp ;
-  else if (!pid)
-  {
-    if (fd_move2(fds[0], p[1][0], fds[1], p[0][1]) == -1) goto syncdie ;
-    sig_blocknone() ;
-    mexec_aen(prog, argv, envp, modifs, m, 1) ;
-   syncdie:
-    c = errno ;
-    fd_write(syncpipe[1], &c, 1) ;
-    _exit(127) ;
-  }
-
-  fd_close(syncpipe[1]) ;
-  syncpipe[1] = fd_read(syncpipe[0], &c, 1) ;
-  if (syncpipe[1])
-  {
-    int e = (unsigned char)c ;
-    if (syncpipe[1] == -1) e = errno ;
-    if (wait_pid(pid, &syncpipe[1]) == -1) e = errno ;
-    errno = e ;
-    goto errsp0 ;
-  }
-  fd_close(syncpipe[0]) ;
-
-  fd_close(p[2][1]) ;
-  fd_close(p[1][0]) ;
-  fd_close(p[0][1]) ;
-  fds[0] = p[0][0] ;
-  fds[1] = p[1][1] ;
-  fds[2] = p[2][0] ;
-  return pid ;
-
- errsp:
-  fd_close(syncpipe[1]) ;
- errsp0:
-  fd_close(syncpipe[0]) ;
- errp2:
   fd_close(p[2][1]) ;
   fd_close(p[2][0]) ;
  errp1:
@@ -190,5 +53,3 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const
   fd_close(p[0][0]) ;
   return 0 ;
 }
-
-#endif
diff --git a/src/libstddjb/djbunix-internal.h b/src/libstddjb/cspawn-internal.h
index 775cb80..0e5efc7 100644
--- a/src/libstddjb/djbunix-internal.h
+++ b/src/libstddjb/cspawn-internal.h
@@ -1,7 +1,7 @@
 /* ISC license. */
 
-#ifndef DJBUNIX_INTERNAL_H
-#define DJBUNIX_INTERNAL_H
+#ifndef CSPAWN_INTERNAL_H
+#define CSPAWN_INTERNAL_H
 
 #include <sys/types.h>
 
diff --git a/src/libstddjb/cspawn.c b/src/libstddjb/cspawn.c
new file mode 100644
index 0000000..4db86e0
--- /dev/null
+++ b/src/libstddjb/cspawn.c
@@ -0,0 +1,220 @@
+/* ISC license. */
+
+#include <skalibs/sysdeps.h>
+
+#include <errno.h>
+
+#include <skalibs/cspawn.h>
+
+#ifdef SKALIBS_HASPOSIXSPAWN
+
+#include <signal.h>
+#include <stdlib.h>
+#include <spawn.h>
+
+#include <skalibs/config.h>
+#include <skalibs/djbunix.h>
+
+#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
+
+#include <sys/wait.h>
+
+#include <skalibs/allreadwrite.h>
+
+static inline pid_t cspawn_workaround (pid_t pid, int const *p)
+{
+  siginfo_t si ;
+  int e ;
+  ssize_t r ;
+  char c ;
+
+  fd_close(p[1]) ;
+  r = fd_read(p[0], &c, 1) ;
+  fd_close(p[0]) ;
+  if (r == -1) return 0 ;
+  if (r) return (errno = EILSEQ, 0) ;  /* child wrote, wtf */
+
+  do e = waitid(P_PID, pid, &si, WEXITED | WNOHANG | WNOWAIT) ;
+  while (e == -1 && errno == EINTR) ;
+  if (e == -1) return pid ;  /* we're in trouble, but don't leak a child */
+  if (!si.si_pid) return pid ;  /* child is running */
+  if (si.si_code != CLD_EXITED || si.si_status != 127) return pid ; /* child died after execve(), let caller handle it */
+  /*
+    child exited 127, so either execve() failed, which is what we want to catch,
+    or it raced like a mofo, execve()d and then exited 127 on its own, in which
+    case, tough luck, it never existed.
+  */
+  wait_pid(pid, 0) ;
+  return (errno = 0, 0) ;
+}
+
+#endif
+
+pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n)
+{
+  pid_t pid ;
+  posix_spawnattr_t attr ;
+  posix_spawn_file_actions_t actions ;
+  int e ;
+  int nopath = !getenv("PATH") ;
+#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
+  int p[2] ;
+  if (pipecoe(p) == -1) return 0 ;
+#endif
+
+  if (flags)
+  {
+    e = posix_spawnattr_init(&attr) ;
+    if (e) goto err ;
+    if (flags & 3)
+    {
+      sigset_t set ;
+      sigemptyset(&set) ;
+      e = posix_spawnattr_setsigmask(&attr, &set) ;
+      if (e) goto errattr ;
+      e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ;
+      if (e) goto errattr ;
+    }
+  }
+
+  if (n)
+  {
+    e = posix_spawn_file_actions_init(&actions) ;
+    if (e) goto errattr ;
+    for (size_t i = 0 ; i < n ; i++)
+    {
+      switch (fa[i].type)
+      {
+        case CSPAWN_FA_CLOSE :
+          e = posix_spawn_file_actions_addclose(&actions, fa[i].x.fd) ;
+          if (e) goto erractions ;
+          break ;
+        case CSPAWN_FA_COPY :
+          e = posix_spawn_file_actions_adddup2(&actions, fa[i].x.fd2[1], fa[i].x.fd2[0]) ;
+          if (e) goto erractions ;
+          break ;
+        case CSPAWN_FA_MOVE :
+          e = posix_spawn_file_actions_adddup2(&actions, fa[i].x.fd2[1], fa[i].x.fd2[0]) ;
+          if (e) goto erractions ;
+          if (fa[i].x.fd2[0] != fa[i].x.fd2[1])
+          {
+            e = posix_spawn_file_actions_addclose(&actions, fa[i].x.fd2[1]) ;
+            if (e) goto erractions ;
+          }
+          break ;
+        case CSPAWN_FA_OPEN :
+          e = posix_spawn_file_actions_addopen(&actions, fa[i].x.openinfo.fd, fa[i].x.openinfo.file, fa[i].x.openinfo.oflag, fa[i].x.openinfo.mode) ;
+          if (e) goto erractions ;
+          break ;
+        default :
+          e = EINVAL ;
+          goto erractions ;
+      }
+    }
+  }
+
+  if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) == -1)) { e = errno ; goto erractions ; }
+  e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)envp) ;
+  if (nopath) unsetenv("PATH") ;
+  if (e) goto errattr ;
+
+  if (n) posix_spawn_file_actions_destroy(&actions) ;
+  if (flags) posix_spawnattr_destroy(&attr) ;
+#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
+  return cspawn_workaround(pid, p) ;
+#else
+  return pid ;
+#endif
+
+ erractions:
+  if (n) posix_spawn_file_actions_destroy(&actions) ;
+ errattr:
+  if (flags) posix_spawnattr_destroy(&attr) ;
+ err:
+#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
+  fd_close(p[1]) ;
+  fd_close(p[0]) ;
+#endif
+  errno = e ;
+  return 0 ;
+}
+
+#else
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <skalibs/allreadwrite.h>
+#include <skalibs/sig.h>
+#include <skalibs/selfpipe.h>
+#include <skalibs/exec.h>
+
+static inline void cspawn_child_exec (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n)
+{
+  for (size_t i = 0 ; i < n ; i++)
+  {
+    switch (fa[i].type)
+    {
+      case CSPAWN_FA_CLOSE : fd_close(fa[i].x.fd) ; break ;
+      case CSPAWN_FA_COPY :
+        if (fd_copy(fa[i].x.fd2[0], fa[i].x.fd2[1]) == -1) return ;
+        break ;
+      case CSPAWN_FA_MOVE :
+        if (fd_move(fa[i].x.fd2[0], fa[i].x.fd2[1]) == -1) return ;
+        break ;
+      case CSPAWN_FA_OPEN :
+      {
+        int fd = open(fa[i].x.openinfo.file, fa[i].x.openinfo.oflag, fa[i].x.openinfo.mode) ;
+        if (fd == -1) return ;
+        if (fd_move(fa[i].x.openinfo.fd, fd) == -1) return ;
+        break ;
+      }
+      default :
+        errno = EINVAL ; return ;
+    }
+  }
+
+  if (flags & CSPAWN_FLAGS_SELFPIPE_FINISH) selfpipe_finish() ;
+  if (flags & CSPAWN_FLAGS_SIGBLOCKNONE) sig_blocknone() ;
+}
+
+pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n)
+{
+  pid_t pid ;
+  int p[2] ;
+  char c ;
+
+  if (pipecoe(p) == -1) return 0 ;
+  pid = fork() ;
+  if (pid == -1)
+  {
+    fd_close(p[1]) ;
+    fd_close(p[0]) ;
+    return 0 ;
+  }
+  if (!pid)
+  {
+    cspawn_child_exec(prog, argv, envp, flags, fa, n) ;
+    c = errno ;
+    fd_write(p[1], &c, 1) ;
+    _exit(127) ;
+  }
+
+  fd_close(p[1]) ;
+  p[1] = fd_read(p[0], &c, 1) ;
+  if (p[1] < 0)
+  {
+    fd_close(p[0]) ;
+    return 0 ;
+  }
+  fd_close(p[0]) ;
+  if (p[1])
+  {
+    wait_pid(pid, &p[0]) ;
+    errno = (unsigned char)c ;
+    return 0 ;
+  }
+  return pid ;
+}
+
+#endif
diff --git a/src/libstddjb/child_spawn_workaround.c b/src/libstddjb/cspawn_workaround.c
index b5fb4f0..da9bd89 100644
--- a/src/libstddjb/child_spawn_workaround.c
+++ b/src/libstddjb/cspawn_workaround.c
@@ -13,7 +13,7 @@
 
  /* when posix_spawn returns too early, you need this */
 
-pid_t child_spawn_workaround (pid_t pid, int const *p)
+pid_t cspawn_workaround (pid_t pid, int const *p)
 {
   siginfo_t si ;
   int e ;
@@ -42,7 +42,7 @@ pid_t child_spawn_workaround (pid_t pid, int const *p)
 
 #else
 
-pid_t child_spawn_workaround (pid_t pid, int const *p)
+pid_t cspawn_workaround (pid_t pid, int const *p)
 {
   if (p)
   {
diff --git a/src/libstddjb/openslurpclose.c b/src/libstddjb/openslurpclose.c
deleted file mode 100644
index e6b0570..0000000
--- a/src/libstddjb/openslurpclose.c
+++ /dev/null
@@ -1,8 +0,0 @@
-/* ISC license. */
-
-#include <skalibs/djbunix.h>
-
-int openslurpclose (stralloc *sa, char const *fn)
-{
-  return openslurpnclose(fn, sa, 0) ;
-}
diff --git a/src/libstddjb/slurp.c b/src/libstddjb/slurp.c
deleted file mode 100644
index eccd833..0000000
--- a/src/libstddjb/slurp.c
+++ /dev/null
@@ -1,8 +0,0 @@
-/* ISC license. */
-
-#include <skalibs/djbunix.h>
-
-int slurp (stralloc *sa, int fd)
-{
-  return slurpn(fd, sa, 0) ;
-}
diff --git a/src/libunixonacid/skaclient_startf_async.c b/src/libunixonacid/skaclient_startf_async.c
index 7b2e1da..6190913 100644
--- a/src/libunixonacid/skaclient_startf_async.c
+++ b/src/libunixonacid/skaclient_startf_async.c
@@ -3,6 +3,7 @@
 #include <errno.h>
 #include <signal.h>
 
+#include <skalibs/cspawn.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/kolbak.h>
 #include <skalibs/skaclient.h>
diff --git a/src/libunixonacid/textclient_server_init.c b/src/libunixonacid/textclient_server_init.c
index 09f17ba..d2d886b 100644
--- a/src/libunixonacid/textclient_server_init.c
+++ b/src/libunixonacid/textclient_server_init.c
@@ -1,7 +1,8 @@
 /* ISC license. */
 
 #include <stdlib.h>
-#include <skalibs/djbunix.h>
+
+#include <skalibs/cspawn.h>
 #include <skalibs/textclient.h>
 
 int textclient_server_init (textmessage_receiver *in, textmessage_sender *syncout, textmessage_sender *asyncout, char const *before, size_t beforelen, char const *after, size_t afterlen, tain const *deadline, tain *stamp)
diff --git a/src/libunixonacid/textclient_server_init_frompipe.c b/src/libunixonacid/textclient_server_init_frompipe.c
index 78e07b9..d5d9d97 100644
--- a/src/libunixonacid/textclient_server_init_frompipe.c
+++ b/src/libunixonacid/textclient_server_init_frompipe.c
@@ -7,7 +7,7 @@
 
 #include <skalibs/types.h>
 #include <skalibs/allreadwrite.h>
-#include <skalibs/djbunix.h>
+#include <skalibs/cspawn.h>
 #include <skalibs/textmessage.h>
 #include <skalibs/textclient.h>
 #include <skalibs/posixishard.h>
diff --git a/src/libunixonacid/textclient_startf.c b/src/libunixonacid/textclient_startf.c
index 0162d6b..f486792 100644
--- a/src/libunixonacid/textclient_startf.c
+++ b/src/libunixonacid/textclient_startf.c
@@ -5,7 +5,7 @@
 #include <errno.h>
 
 #include <skalibs/allreadwrite.h>
-#include <skalibs/djbunix.h>
+#include <skalibs/cspawn.h>
 #include <skalibs/textmessage.h>
 #include <skalibs/textclient.h>
 #include <skalibs/posixishard.h>