commit 4cce87557feb6933c284d234c448bd8bde4facae
parent a646c96d5f7ec4b4e0bb538a41fc202c2cdb3309
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Sat, 9 Sep 2023 03:55:38 +0000
cspawn revamp, part 1. Prepare for 2.14.0.0.
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat:
26 files changed, 463 insertions(+), 856 deletions(-)
diff --git a/NEWS b/NEWS
@@ -6,8 +6,10 @@ In 2.13.2.0
- Bugfixes.
- New accessor function: selfpipe_fd().
- New functions: slurpn(), openslurpnclose().
- - New strerr macros to warn with a "fatal" message
- - New cdb functions: cdb_hashv(), cdbmake_addv()
+ - slurp() and openslurpclose() are now macros.
+ - New strerr macros to warn with a "fatal" message.
+ - New cdb functions: cdb_hashv(), cdbmake_addv().
+ - child_spawn() revamp. Prototype change (last arg is a size_t).
In 2.13.1.1
diff --git a/doc/index.html b/doc/index.html
@@ -60,7 +60,7 @@ with a standard C development environment </li>
<h3> Download </h3>
<ul>
- <li> The current released version of skalibs is <a href="skalibs-2.13.2.0.tar.gz">2.13.2.0</a>. </li>
+ <li> The current released version of skalibs is <a href="skalibs-2.14.0.0.tar.gz">2.14.0.0</a>. </li>
<li> Alternatively, you can checkout a copy of the
<a href="//git.skarnet.org/cgi-bin/cgit.cgi/skalibs/">skalibs
git repository</a>:
diff --git a/doc/upgrade.html b/doc/upgrade.html
@@ -17,12 +17,15 @@
</p>
-<h2> in 2.13.2.0 </h2>
+<h2> in 2.14.0.0 </h2>
<ul>
<li> New selfpipe accessor function: <tt>selfpipe_fd()</tt> </li>
<li> New djbunix functions: <tt>slurpn()</tt>, <tt>openslurpnclose()</tt> </li>
- <li> New cdb functions: <tt>cdb_hashv()</tt>, <tt>cdmake_addv()</tt> </li>
+ <li> <tt>slurp</tt> and <tt>openslurpclose</tt> are now macros </tt> </li>
+ <li> New cdb functions: <tt>cdb_hashv()</tt>, <tt>cdbmake_addv()</tt> </li>
+ <li> New <tt>cspawn</tt> function, for posix_spawn/fork unification </li>
+ <li> <tt>child_spawn</tt> functions are now declared in <tt>cspawn.h</tt> </li>
</ul>
<h2> in 2.13.1.1 </h2>
diff --git a/package/deps.mak b/package/deps.mak
@@ -29,7 +29,6 @@ src/include/skalibs/genqdyn.h: src/include/skalibs/stralloc.h
src/include/skalibs/genset.h: src/include/skalibs/functypes.h
src/include/skalibs/gensetdyn.h: src/include/skalibs/functypes.h src/include/skalibs/genalloc.h src/include/skalibs/stralloc.h
src/include/skalibs/iopause.h: src/include/skalibs/tai.h
-src/include/skalibs/ip46.h: src/include/skalibs/fmtscan.h src/include/skalibs/socket.h
src/include/skalibs/kolbak.h: src/include/skalibs/unixmessage.h
src/include/skalibs/lolstdio.h: src/include/skalibs/bufalloc.h src/include/skalibs/buffer.h src/include/skalibs/strerr.h
src/include/skalibs/netstring.h: src/include/skalibs/buffer.h src/include/skalibs/stralloc.h
@@ -45,15 +44,12 @@ src/include/skalibs/skalibs.h: src/include/skalibs/datastruct.h src/include/skal
src/include/skalibs/skamisc.h: src/include/skalibs/buffer.h src/include/skalibs/stralloc.h
src/include/skalibs/socket.h: src/include/skalibs/gccattributes.h src/include/skalibs/posixplz.h src/include/skalibs/tai.h
src/include/skalibs/stdcrypto.h: src/include/skalibs/blake2s.h src/include/skalibs/sha1.h src/include/skalibs/sha256.h src/include/skalibs/sha512.h
-src/include/skalibs/stddjb.h: src/include/skalibs/alarm.h src/include/skalibs/alloc.h src/include/skalibs/allreadwrite.h src/include/skalibs/bitarray.h src/include/skalibs/bufalloc.h src/include/skalibs/buffer.h src/include/skalibs/bytestr.h src/include/skalibs/cbuffer.h src/include/skalibs/cdb.h src/include/skalibs/cdbmake.h src/include/skalibs/devino.h src/include/skalibs/direntry.h src/include/skalibs/disize.h src/include/skalibs/diuint.h src/include/skalibs/diuint32.h src/include/skalibs/djbtime.h src/include/skalibs/djbunix.h src/include/skalibs/env.h src/include/skalibs/envalloc.h src/include/skalibs/error.h src/include/skalibs/exec.h src/include/skalibs/fmtscan.h src/include/skalibs/functypes.h src/include/skalibs/gccattributes.h src/include/skalibs/genalloc.h src/include/skalibs/genwrite.h src/include/skalibs/iopause.h src/include/skalibs/ip46.h src/include/skalibs/lolstdio.h src/include/skalibs/netstring.h src/include/skalibs/segfault.h src/include/skalibs/selfpipe.h src/include/skalibs/setgroups.h src/include/skalibs/sgetopt.h src/include/skalibs/sig.h src/include/skalibs/siovec.h src/include/skalibs/skamisc.h src/include/skalibs/socket.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h src/include/skalibs/types.h src/include/skalibs/uint16.h src/include/skalibs/uint32.h src/include/skalibs/uint64.h
+src/include/skalibs/stddjb.h: src/include/skalibs/alarm.h src/include/skalibs/alloc.h src/include/skalibs/allreadwrite.h src/include/skalibs/bitarray.h src/include/skalibs/bufalloc.h src/include/skalibs/buffer.h src/include/skalibs/bytestr.h src/include/skalibs/cbuffer.h src/include/skalibs/cdb.h src/include/skalibs/cdbmake.h src/include/skalibs/cspawn.h src/include/skalibs/devino.h src/include/skalibs/direntry.h src/include/skalibs/disize.h src/include/skalibs/diuint.h src/include/skalibs/diuint32.h src/include/skalibs/djbtime.h src/include/skalibs/djbunix.h src/include/skalibs/env.h src/include/skalibs/envalloc.h src/include/skalibs/error.h src/include/skalibs/exec.h src/include/skalibs/fmtscan.h src/include/skalibs/functypes.h src/include/skalibs/gccattributes.h src/include/skalibs/genalloc.h src/include/skalibs/genwrite.h src/include/skalibs/iopause.h src/include/skalibs/ip46.h src/include/skalibs/lolstdio.h src/include/skalibs/netstring.h src/include/skalibs/segfault.h src/include/skalibs/selfpipe.h src/include/skalibs/setgroups.h src/include/skalibs/sgetopt.h src/include/skalibs/sig.h src/include/skalibs/siovec.h src/include/skalibs/skamisc.h src/include/skalibs/socket.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h src/include/skalibs/types.h src/include/skalibs/uint16.h src/include/skalibs/uint32.h src/include/skalibs/uint64.h
src/include/skalibs/strerr.h: src/include/skalibs/gccattributes.h
src/include/skalibs/strerr2.h: src/include/skalibs/strerr.h
src/include/skalibs/tai.h: src/include/skalibs/gccattributes.h src/include/skalibs/uint64.h
src/include/skalibs/textclient.h: src/include/skalibs/allreadwrite.h src/include/skalibs/tai.h src/include/skalibs/textmessage.h
src/include/skalibs/textmessage.h: src/include/skalibs/allreadwrite.h src/include/skalibs/bufalloc.h src/include/skalibs/buffer.h src/include/skalibs/gccattributes.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h
-src/include/skalibs/types.h: src/include/skalibs/uint16.h src/include/skalibs/uint32.h src/include/skalibs/uint64.h
-src/include/skalibs/uint16.h: src/include/skalibs/uint64.h
-src/include/skalibs/uint32.h: src/include/skalibs/uint64.h
src/include/skalibs/unix-timed.h: src/include/skalibs/bufalloc.h src/include/skalibs/buffer.h src/include/skalibs/functypes.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h
src/include/skalibs/unix-transactional.h: src/include/skalibs/direntry.h src/include/skalibs/stralloc.h
src/include/skalibs/unixconnection.h: src/include/skalibs/unixmessage.h
@@ -305,15 +301,16 @@ src/libstddjb/cdbmake_addv.o src/libstddjb/cdbmake_addv.lo: src/libstddjb/cdbmak
src/libstddjb/cdbmake_finish.o src/libstddjb/cdbmake_finish.lo: src/libstddjb/cdbmake_finish.c src/include/skalibs/buffer.h src/libstddjb/cdbmake-internal.h src/include/skalibs/cdbmake.h src/include/skalibs/diuint32.h src/include/skalibs/genalloc.h src/include/skalibs/uint32.h
src/libstddjb/cdbmake_posplus.o src/libstddjb/cdbmake_posplus.lo: src/libstddjb/cdbmake_posplus.c src/libstddjb/cdbmake-internal.h
src/libstddjb/cdbmake_start.o src/libstddjb/cdbmake_start.lo: src/libstddjb/cdbmake_start.c src/include/skalibs/buffer.h src/include/skalibs/cdbmake.h src/include/skalibs/genalloc.h
-src/libstddjb/child_spawn.o src/libstddjb/child_spawn.lo: src/libstddjb/child_spawn.c src/include/skalibs/allreadwrite.h src/include/skalibs/config.h src/include/skalibs/djbunix.h src/include/skalibs/env.h src/include/skalibs/exec.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h src/include/skalibs/types.h
-src/libstddjb/child_spawn0.o src/libstddjb/child_spawn0.lo: src/libstddjb/child_spawn0.c src/include/skalibs/allreadwrite.h src/include/skalibs/config.h src/include/skalibs/djbunix.h src/include/skalibs/exec.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h
-src/libstddjb/child_spawn1_internal.o src/libstddjb/child_spawn1_internal.lo: src/libstddjb/child_spawn1_internal.c src/include/skalibs/allreadwrite.h src/include/skalibs/config.h src/include/skalibs/djbunix.h src/include/skalibs/exec.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h
-src/libstddjb/child_spawn1_pipe.o src/libstddjb/child_spawn1_pipe.lo: src/libstddjb/child_spawn1_pipe.c src/libstddjb/djbunix-internal.h src/include/skalibs/djbunix.h
-src/libstddjb/child_spawn1_socket.o src/libstddjb/child_spawn1_socket.lo: src/libstddjb/child_spawn1_socket.c src/libstddjb/djbunix-internal.h src/include/skalibs/djbunix.h src/include/skalibs/socket.h
-src/libstddjb/child_spawn2.o src/libstddjb/child_spawn2.lo: src/libstddjb/child_spawn2.c src/include/skalibs/allreadwrite.h src/include/skalibs/config.h src/include/skalibs/djbunix.h src/include/skalibs/exec.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h
-src/libstddjb/child_spawn3.o src/libstddjb/child_spawn3.lo: src/libstddjb/child_spawn3.c src/include/skalibs/allreadwrite.h src/include/skalibs/config.h src/include/skalibs/djbunix.h src/include/skalibs/env.h src/include/skalibs/exec.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h src/include/skalibs/types.h
-src/libstddjb/child_spawn_workaround.o src/libstddjb/child_spawn_workaround.lo: src/libstddjb/child_spawn_workaround.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/sysdeps.h
+src/libstddjb/child_spawn.o src/libstddjb/child_spawn.lo: src/libstddjb/child_spawn.c src/include/skalibs/cspawn.h src/include/skalibs/djbunix.h src/include/skalibs/env.h src/include/skalibs/types.h
+src/libstddjb/child_spawn0.o src/libstddjb/child_spawn0.lo: src/libstddjb/child_spawn0.c src/include/skalibs/cspawn.h
+src/libstddjb/child_spawn1_internal.o src/libstddjb/child_spawn1_internal.lo: src/libstddjb/child_spawn1_internal.c src/libstddjb/cspawn-internal.h src/include/skalibs/cspawn.h src/include/skalibs/djbunix.h
+src/libstddjb/child_spawn1_pipe.o src/libstddjb/child_spawn1_pipe.lo: src/libstddjb/child_spawn1_pipe.c src/libstddjb/cspawn-internal.h src/include/skalibs/cspawn.h
+src/libstddjb/child_spawn1_socket.o src/libstddjb/child_spawn1_socket.lo: src/libstddjb/child_spawn1_socket.c src/libstddjb/cspawn-internal.h src/include/skalibs/cspawn.h src/include/skalibs/socket.h
+src/libstddjb/child_spawn2.o src/libstddjb/child_spawn2.lo: src/libstddjb/child_spawn2.c src/include/skalibs/cspawn.h src/include/skalibs/djbunix.h
+src/libstddjb/child_spawn3.o src/libstddjb/child_spawn3.lo: src/libstddjb/child_spawn3.c src/include/skalibs/cspawn.h src/include/skalibs/djbunix.h src/include/skalibs/env.h src/include/skalibs/types.h
src/libstddjb/coe.o src/libstddjb/coe.lo: src/libstddjb/coe.c src/include/skalibs/djbunix.h
+src/libstddjb/cspawn.o src/libstddjb/cspawn.lo: src/libstddjb/cspawn.c src/include/skalibs/allreadwrite.h src/include/skalibs/config.h src/include/skalibs/cspawn.h src/include/skalibs/djbunix.h src/include/skalibs/exec.h src/include/skalibs/selfpipe.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h
+src/libstddjb/cspawn_workaround.o src/libstddjb/cspawn_workaround.lo: src/libstddjb/cspawn_workaround.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/sysdeps.h
src/libstddjb/deepsleepuntil.o src/libstddjb/deepsleepuntil.lo: src/libstddjb/deepsleepuntil.c src/include/skalibs/iopause.h src/include/skalibs/tai.h
src/libstddjb/devino_cmp.o src/libstddjb/devino_cmp.lo: src/libstddjb/devino_cmp.c src/include/skalibs/devino.h
src/libstddjb/dir_close.o src/libstddjb/dir_close.lo: src/libstddjb/dir_close.c src/include/skalibs/direntry.h
@@ -455,7 +452,6 @@ src/libstddjb/openc_write.o src/libstddjb/openc_write.lo: src/libstddjb/openc_wr
src/libstddjb/openreadfileclose.o src/libstddjb/openreadfileclose.lo: src/libstddjb/openreadfileclose.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
src/libstddjb/openreadnclose.o src/libstddjb/openreadnclose.lo: src/libstddjb/openreadnclose.c src/include/skalibs/djbunix.h
src/libstddjb/openreadnclose_nb.o src/libstddjb/openreadnclose_nb.lo: src/libstddjb/openreadnclose_nb.c src/include/skalibs/djbunix.h
-src/libstddjb/openslurpclose.o src/libstddjb/openslurpclose.lo: src/libstddjb/openslurpclose.c src/include/skalibs/djbunix.h
src/libstddjb/openslurpnclose.o src/libstddjb/openslurpnclose.lo: src/libstddjb/openslurpnclose.c src/include/skalibs/djbunix.h
src/libstddjb/openwritenclose5.o src/libstddjb/openwritenclose5.lo: src/libstddjb/openwritenclose5.c src/include/skalibs/devino.h src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h
src/libstddjb/openwritenclose_internal_deprecated.o src/libstddjb/openwritenclose_internal_deprecated.lo: src/libstddjb/openwritenclose_internal_deprecated.c src/include/skalibs/devino.h src/include/skalibs/djbunix.h
@@ -517,7 +513,6 @@ src/libstddjb/skagetlnmaxsep.o src/libstddjb/skagetlnmaxsep.lo: src/libstddjb/sk
src/libstddjb/skagetlnsep.o src/libstddjb/skagetlnsep.lo: src/libstddjb/skagetlnsep.c src/include/skalibs/buffer.h src/include/skalibs/siovec.h src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h
src/libstddjb/skagetlnsep_loose.o src/libstddjb/skagetlnsep_loose.lo: src/libstddjb/skagetlnsep_loose.c src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h
src/libstddjb/skalibs_tzisright.o src/libstddjb/skalibs_tzisright.lo: src/libstddjb/skalibs_tzisright.c src/libstddjb/djbtime-internal.h
-src/libstddjb/slurp.o src/libstddjb/slurp.lo: src/libstddjb/slurp.c src/include/skalibs/djbunix.h
src/libstddjb/slurpn.o src/libstddjb/slurpn.lo: src/libstddjb/slurpn.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
src/libstddjb/socket_accept4.o src/libstddjb/socket_accept4.lo: src/libstddjb/socket_accept4.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/socket.h src/include/skalibs/sysdeps.h src/include/skalibs/uint16.h
src/libstddjb/socket_accept4_u32.o src/libstddjb/socket_accept4_u32.lo: src/libstddjb/socket_accept4_u32.c src/include/skalibs/socket.h src/include/skalibs/uint32.h
@@ -787,18 +782,18 @@ src/libunixonacid/skaclient_start.o src/libunixonacid/skaclient_start.lo: src/li
src/libunixonacid/skaclient_start_async.o src/libunixonacid/skaclient_start_async.lo: src/libunixonacid/skaclient_start_async.c src/include/skalibs/djbunix.h src/include/skalibs/error.h src/include/skalibs/kolbak.h src/libunixonacid/skaclient-internal.h src/include/skalibs/skaclient.h src/include/skalibs/socket.h src/include/skalibs/unixmessage.h
src/libunixonacid/skaclient_start_cb.o src/libunixonacid/skaclient_start_cb.lo: src/libunixonacid/skaclient_start_cb.c src/include/skalibs/posixishard.h src/libunixonacid/skaclient-internal.h src/include/skalibs/skaclient.h src/include/skalibs/unixmessage.h
src/libunixonacid/skaclient_startf.o src/libunixonacid/skaclient_startf.lo: src/libunixonacid/skaclient_startf.c src/include/skalibs/kolbak.h src/libunixonacid/skaclient-internal.h src/include/skalibs/skaclient.h
-src/libunixonacid/skaclient_startf_async.o src/libunixonacid/skaclient_startf_async.lo: src/libunixonacid/skaclient_startf_async.c src/include/skalibs/djbunix.h src/include/skalibs/kolbak.h src/libunixonacid/skaclient-internal.h src/include/skalibs/skaclient.h
+src/libunixonacid/skaclient_startf_async.o src/libunixonacid/skaclient_startf_async.lo: src/libunixonacid/skaclient_startf_async.c src/include/skalibs/cspawn.h src/include/skalibs/djbunix.h src/include/skalibs/kolbak.h src/libunixonacid/skaclient-internal.h src/include/skalibs/skaclient.h
src/libunixonacid/skaclient_syncify.o src/libunixonacid/skaclient_syncify.lo: src/libunixonacid/skaclient_syncify.c src/include/skalibs/skaclient.h
src/libunixonacid/skaclient_zero.o src/libunixonacid/skaclient_zero.lo: src/libunixonacid/skaclient_zero.c src/include/skalibs/skaclient.h
src/libunixonacid/stat_at.o src/libunixonacid/stat_at.lo: src/libunixonacid/stat_at.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/sysdeps.h src/include/skalibs/unix-transactional.h
src/libunixonacid/textclient_command.o src/libunixonacid/textclient_command.lo: src/libunixonacid/textclient_command.c src/include/skalibs/posixishard.h src/include/skalibs/textclient.h
src/libunixonacid/textclient_commandv.o src/libunixonacid/textclient_commandv.lo: src/libunixonacid/textclient_commandv.c src/include/skalibs/posixishard.h src/include/skalibs/textclient.h
src/libunixonacid/textclient_end.o src/libunixonacid/textclient_end.lo: src/libunixonacid/textclient_end.c src/include/skalibs/djbunix.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h
-src/libunixonacid/textclient_server_init.o src/libunixonacid/textclient_server_init.lo: src/libunixonacid/textclient_server_init.c src/include/skalibs/djbunix.h src/include/skalibs/textclient.h
-src/libunixonacid/textclient_server_init_frompipe.o src/libunixonacid/textclient_server_init_frompipe.lo: src/libunixonacid/textclient_server_init_frompipe.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/types.h
+src/libunixonacid/textclient_server_init.o src/libunixonacid/textclient_server_init.lo: src/libunixonacid/textclient_server_init.c src/include/skalibs/cspawn.h src/include/skalibs/textclient.h
+src/libunixonacid/textclient_server_init_frompipe.o src/libunixonacid/textclient_server_init_frompipe.lo: src/libunixonacid/textclient_server_init_frompipe.c src/include/skalibs/allreadwrite.h src/include/skalibs/cspawn.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/types.h
src/libunixonacid/textclient_server_init_fromsocket.o src/libunixonacid/textclient_server_init_fromsocket.lo: src/libunixonacid/textclient_server_init_fromsocket.c src/include/skalibs/allreadwrite.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h
src/libunixonacid/textclient_start.o src/libunixonacid/textclient_start.lo: src/libunixonacid/textclient_start.c src/include/skalibs/djbunix.h src/include/skalibs/posixishard.h src/include/skalibs/socket.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h
-src/libunixonacid/textclient_startf.o src/libunixonacid/textclient_startf.lo: src/libunixonacid/textclient_startf.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h
+src/libunixonacid/textclient_startf.o src/libunixonacid/textclient_startf.lo: src/libunixonacid/textclient_startf.c src/include/skalibs/allreadwrite.h src/include/skalibs/cspawn.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h
src/libunixonacid/textclient_zero.o src/libunixonacid/textclient_zero.lo: src/libunixonacid/textclient_zero.c src/include/skalibs/textclient.h
src/libunixonacid/textmessage_create_send_channel.o src/libunixonacid/textmessage_create_send_channel.lo: src/libunixonacid/textmessage_create_send_channel.c src/include/skalibs/ancil.h src/include/skalibs/djbunix.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/unix-timed.h
src/libunixonacid/textmessage_handle.o src/libunixonacid/textmessage_handle.lo: src/libunixonacid/textmessage_handle.c src/include/skalibs/textmessage.h
diff --git a/package/info b/package/info
@@ -1,4 +1,4 @@
package=skalibs
-version=2.13.2.0
+version=2.14.0.0
category=prog
package_macro_name=SKALIBS
diff --git a/src/include/skalibs/cspawn.h 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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/child_spawn_workaround.c b/src/libstddjb/child_spawn_workaround.c
@@ -1,55 +0,0 @@
-/* ISC license. */
-
-#include <skalibs/sysdeps.h>
-#include <skalibs/djbunix.h>
-
-#if defined(SKALIBS_HASPOSIXSPAWN) && defined(SKALIBS_HASPOSIXSPAWNEARLYRETURN)
-
-#include <errno.h>
-#include <signal.h>
-#include <sys/wait.h>
-
-#include <skalibs/allreadwrite.h>
-
- /* when posix_spawn returns too early, you need this */
-
-pid_t child_spawn_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) ;
-}
-
-#else
-
-pid_t child_spawn_workaround (pid_t pid, int const *p)
-{
- if (p)
- {
- fd_close(p[1]) ;
- fd_close(p[0]) ;
- }
- return pid ;
-}
-
-#endif
diff --git a/src/libstddjb/cspawn-internal.h b/src/libstddjb/cspawn-internal.h
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#ifndef CSPAWN_INTERNAL_H
+#define CSPAWN_INTERNAL_H
+
+#include <sys/types.h>
+
+extern pid_t child_spawn1_internal (char const *, char const *const *, char const *const *, int *, int) ;
+
+#endif
diff --git a/src/libstddjb/cspawn.c 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/cspawn_workaround.c b/src/libstddjb/cspawn_workaround.c
@@ -0,0 +1,55 @@
+/* ISC license. */
+
+#include <skalibs/sysdeps.h>
+#include <skalibs/djbunix.h>
+
+#if defined(SKALIBS_HASPOSIXSPAWN) && defined(SKALIBS_HASPOSIXSPAWNEARLYRETURN)
+
+#include <errno.h>
+#include <signal.h>
+#include <sys/wait.h>
+
+#include <skalibs/allreadwrite.h>
+
+ /* when posix_spawn returns too early, you need this */
+
+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) ;
+}
+
+#else
+
+pid_t cspawn_workaround (pid_t pid, int const *p)
+{
+ if (p)
+ {
+ fd_close(p[1]) ;
+ fd_close(p[0]) ;
+ }
+ return pid ;
+}
+
+#endif
diff --git a/src/libstddjb/djbunix-internal.h b/src/libstddjb/djbunix-internal.h
@@ -1,10 +0,0 @@
-/* ISC license. */
-
-#ifndef DJBUNIX_INTERNAL_H
-#define DJBUNIX_INTERNAL_H
-
-#include <sys/types.h>
-
-extern pid_t child_spawn1_internal (char const *, char const *const *, char const *const *, int *, int) ;
-
-#endif
diff --git a/src/libstddjb/openslurpclose.c b/src/libstddjb/openslurpclose.c
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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>