diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2020-11-25 15:00:45 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2020-11-25 15:00:45 +0000 |
commit | 21e6ea800cc96ba76e94ad8de1dfa58ab1b7ceb6 (patch) | |
tree | 7954f73408bcfc10a8945661556f4dd41a8e5530 | |
parent | 481fdcb1faa76056c9c4d91cc7078d1c05b15f37 (diff) | |
download | skalibs-21e6ea800cc96ba76e94ad8de1dfa58ab1b7ceb6.tar.xz |
child_spawn cleanup, configure cleanup
-rwxr-xr-x | configure | 185 | ||||
-rw-r--r-- | package/deps.mak | 16 | ||||
-rw-r--r-- | src/libstddjb/child_spawn.c | 47 | ||||
-rw-r--r-- | src/libstddjb/child_spawn0.c | 15 | ||||
-rw-r--r-- | src/libstddjb/child_spawn1_internal.c | 101 | ||||
-rw-r--r-- | src/libstddjb/child_spawn2.c | 39 | ||||
-rw-r--r-- | src/libstddjb/child_spawn3.c | 36 |
7 files changed, 230 insertions, 209 deletions
@@ -3,6 +3,8 @@ cd `dirname "$0"` . package/info +# Helper functions + usage () { cat <<EOF Usage: $0 [OPTION]... [TARGET] @@ -51,18 +53,16 @@ EOF exit 0 } -# Helper functions - # If your system does not have printf, you can comment this, but it is # generally not a good idea to use echo. # See http://etalabs.net/sh_tricks.html echo () { - IFS=" " + IFS=' ' printf %s\\n "$*" } echon () { - IFS=" " + IFS=' ' printf %s "$*" } @@ -124,16 +124,24 @@ tryldflag () { fi } - # Sysdeps determination functions iscached () { - if test -r "$tmps" && grep -qF "${1}: " "$tmps" ; then - v=`grep -F "${1}: " "$tmps" | tail -n 1 | awk '{print $2;}'` - echo "${1}: $v" >> "$sysdeps/sysdeps" - echo " ... user-provided: $v" - return 0 ; + if test -r "$tmps" && grep -q "^${1}: " "$tmps" ; then + grep "^${1}: " "$tmps" | tail -n 1 | + { + IFS=' ' + read -r k v ldlibs + if test -n "$ldlibs" ; then + echo " ... user-provided: $v with linker args: $ldlibs" + echo "$ldlibs" >&3 + else + echo " ... user-provided: $v" + fi + echo "${1}: $v" >> "$sysdeps/sysdeps" + return 0 ; + } else return 1 ; fi @@ -284,39 +292,50 @@ trytypes () { echo " ... done" } -trylibs () { +detectlibs () { args= name=$1 - echo "Checking whether system has $2..." >&3 - shift 2 + shift if $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST -c -o try$name.o src/sysdeps/try$name.c 2>/dev/null ; then until $CC_AUTO $CFLAGS_AUTO $CFLAGS $CFLAGS_POST $LDFLAGS_AUTO $LDFLAGS $LDFLAGS_POST -o /dev/null try$name.o $args 2>/dev/null ; do if test -z "$*" ; then rm -f try$name.o - echo - echo " ... no" >&3 return 1 fi args="$args $1" shift done echo ${args# } - if test -z "$args" ; then - echo " ... yes" >&3 - else - echo " ... yes, with$args" >&3 - fi rm -f try$name.o return 0 else - echo - echo " ... no" >&3 return 1 fi } -# Actual script +trylibs () { + name="$1" + libfile="$2" + echo "Checking whether system has $3..." + shift 3 + if args=`iscached $name 3>&1 1>&4 4>&-` 4>&1 ; then : + elif args=`detectlibs "$name" "$@"` ; then + echo "${name}: yes" >> "${sysdeps}/sysdeps" + if test -z "$args" ; then + echo " ... yes" + else + echo " ... yes, with linker args: $args" + fi + else + echo "${name}: no" >> ${sysdeps}/sysdeps + echo " ... no" + fi + echo "$args" > "${sysdeps}/$libfile" +} +# Actual script! + +# Initialize variables CC_AUTO= CPPFLAGS_AUTO="-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -Isrc/include" CPPFLAGS_POST="$CPPFLAGS" @@ -356,6 +375,7 @@ vpaths='' vpathd='' build= +# Parse command line for arg ; do case "$arg" in --help) usage ;; @@ -388,7 +408,7 @@ for arg ; do --with-default-path=*) defaultpath=${arg#*=} ; dpathorig=false ;; --without-default-path) defaultpath=$ddefaultpath ; dpathorig=true ;; --with-sysdep-*=*) sysdeplist="$sysdeplist ${arg#--with-sysdep-}" ;; - --without-sysdep-*) sysdeplist="$sysdeplist ${arg#--with-sysdep-}=no" ;; + --without-sysdep-*) sysdeplist="$sysdeplist ${arg#--without-sysdep-}=no" ;; --enable-*|--disable-*|--with-*|--without-*|--*dir=*) ;; --host=*|--target=*) target=${arg#*=} ;; --build=*) build=${arg#*=} ;; @@ -419,22 +439,6 @@ for i in exec_prefix dynlibdir libdir includedir datadir sysdepdir sysdepspre sp stripdir $i done - -# Get usable temp filenames -i=0 -set -C -while : ; do - i=$(($i+1)) - tmpc="./tmp-configure-$$-$PPID-$i.c" - tmpe="./tmp-configure-$$-$PPID-$i.tmp" - tmps="./tmp-configure-$$-$PPID-$i.sysdeps" - 2>|/dev/null > "$tmpc" && break - 2>|/dev/null > "$tmpe" && break - test "$i" -gt 50 && fail "$0: cannot create temporary files" -done -set +C -trap 'rm -f "$tmpc" "$tmpe" "$tmps"' EXIT ABRT INT QUIT TERM HUP - # Set slashpackage values if $slashpackage ; then home=${sproot}/package/${category}/${package}-${version} @@ -452,6 +456,50 @@ if $slashpackage ; then fi fi +# Get usable temp filenames +i=0 +set -C +while test "$i" -lt 50 ; do + i=$(($i+1)) + tmpc="./tmp-configure-$$-$PPID-$i.c" + tmpe="./tmp-configure-$$-$PPID-$i.tmp" + tmps="./tmp-configure-$$-$PPID-$i.sysdeps" + 2>|/dev/null > "$tmpc" && break + 2>|/dev/null > "$tmpe" && break + 2>|/dev/null > "$tmps" && break +done +if test "$i" -gt 50 ; then + fail "$0: cannot create temporary files" +fi +set +C +trap 'rm -f "$tmpc" "$tmpe" "$tmps"' EXIT ABRT INT QUIT TERM HUP + +# Preprocess user-provided sysdeps +rm -f "$tmps" +if test -n "$sysdeplist" ; then + :> "$tmps" + for i in $sysdeplist ; do + k=${i%%=*} + echo ${i#*=} | sed 's/,/ /g' | + { + read v extras + if test -z "$k" || test -z "$v" ; then fail "$0: invalid user-provided sysdep: $i" ; fi + if test "$v" = "true" ; then v=yes + elif test "$v" = "false" ; then v=no + fi + if grep -q -e "^${k}: " "$tmps" ; then + grep -v -e "^${k}: " "$tmps" > "$tmpe" + mv -f "$tmpe" "$tmps" + fi + if test -n "$extras" ; then + echo "${k}: $v $extras" + else + echo "${k}: $v" + fi >> "$tmps" + } + done +fi + # Find a C compiler to use if test -n "$target" && test x${build} != x${target} ; then cross=${target}- @@ -500,6 +548,7 @@ if test -z "$target" ; then fi echo " ... $target" +# Produce automatic compilation flags if $allpic ; then tryflag CPPFLAGS_AUTO -fPIC fi @@ -518,60 +567,36 @@ tryflag CPPFLAGS_AUTO -Wno-unused-value tryflag CPPFLAGS_AUTO -Wno-parentheses tryflag CFLAGS_AUTO -ffunction-sections tryflag CFLAGS_AUTO -fdata-sections - tryldflag LDFLAGS_AUTO -Wl,--sort-section=alignment tryldflag LDFLAGS_AUTO -Wl,--sort-common if $shared ; then tryldflag LDFLAGS -Wl,--hash-style=both fi -rm -f "$tmps" -if test -n "$sysdeplist" ; then - :> "$tmps" - for i in $sysdeplist ; do - k=${i%%=*} - v=${i#*=} - if test "$v" = "true" ; then v=yes - elif test "$v" = "false" ; then v=no - fi - if grep -qF -e "${k}: " "$tmps" ; then - grep -vF -e "${k}: " "$tmps" > "$tmpe" - mv -f "$tmpe" "$tmps" - fi - echo "${k}: $v" >> "$tmps" - done -fi +# And now, determine a big fat batch of sysdeps. sysdeps=sysdeps.cfg mkdir -p $sysdeps echo "$target" > $sysdeps/target :> "$sysdeps/sysdeps" -exec 3>&1 util_lib= echo > $sysdeps/util.lib -socket_lib=`trylibs lsock 'accessible socket functions' -lsocket -lnsl` || fail "$0: unable to determine socket.lib sysdep" +echo 'Checking required linker flags for socket functions...' +socket_lib=`detectlibs lsock -lsocket -lnsl` || fail "$0: unable to determine required linker flags for socket functions" +if test -n "$socket_lib" ; then + echo " ... $socket_lib" +else + echo " ... none" +fi echo "$socket_lib" > $sysdeps/socket.lib -hasclock=yes -sysclock_lib=`trylibs clockrt 'clock_gettime()' -lrt` || hasclock=no -echo "$sysclock_lib" > $sysdeps/sysclock.lib -echo "clockrt: $hasclock" >> $sysdeps/sysdeps - -choose cl clockmon CLOCK_MONOTONIC $sysclock_lib -choose cl clockboot CLOCK_BOOTTIME $sysclock_lib - -hasspawn=yes -spawn_lib=`trylibs posixspawn 'posix_spawn()' -lrt` || hasspawn=no -echo "$spawn_lib" > $sysdeps/spawn.lib -echo "posixspawn: $hasspawn" >> $sysdeps/sysdeps - -hastimer=yes -timer_lib=`trylibs timer 'timer_create()' -lrt` || hastimer=no -echo "$timer_lib" > $sysdeps/timer.lib -echo "timer: $hastimer" >> $sysdeps/sysdeps -exec 3>&- +trylibs clockrt sysclock.lib 'clock_gettime()' -lrt +choose cl clockmon CLOCK_MONOTONIC `cat "${sysdeps}/sysclock.lib"` +choose cl clockboot CLOCK_BOOTTIME `cat "${sysdeps}/sysclock.lib"` +trylibs posixspawn spawn.lib 'posix_spawn()' -lrt +trylibs timer timer.lib 'timer_create()' -lrt tryendianness trytypes @@ -610,10 +635,12 @@ choose cl nsgetparent 'NS_GET_PARENT' choose cl explicit_bzero 'explicit_bzero()' choose cl getrandom 'getrandom()' -# And here are the evil irreducible sysdeps. - +# Here are the evil irreducible run-time sysdeps. choose clr devurandom '/dev/urandom' + +# Finally, produce config.mak and config.h + rm -f "$tmps" echo "Creating config.mak..." cmdline=$(quote "$0") diff --git a/package/deps.mak b/package/deps.mak index a0150fe..8e21d3a 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -33,7 +33,6 @@ src/include/skalibs/gensetdyn.h: src/include/skalibs/functypes.h src/include/ska src/include/skalibs/getpeereid.h: src/include/skalibs/posixplz.h src/include/skalibs/iobuffer.h: src/include/skalibs/buffer.h src/include/skalibs/djbunix.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/strerr2.h src/include/skalibs/netstring.h: src/include/skalibs/buffer.h src/include/skalibs/stralloc.h @@ -55,9 +54,6 @@ 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/stralloc.h src/include/skalibs/uint64.h src/include/skalibs/unixconnection.h: src/include/skalibs/unixmessage.h @@ -331,13 +327,13 @@ src/libstddjb/cdb_nextkey.o src/libstddjb/cdb_nextkey.lo: src/libstddjb/cdb_next src/libstddjb/cdb_read.o src/libstddjb/cdb_read.lo: src/libstddjb/cdb_read.c src/include/skalibs/allreadwrite.h src/include/skalibs/cdb.h src/include/skalibs/posixishard.h src/libstddjb/cdb_successor.o src/libstddjb/cdb_successor.lo: src/libstddjb/cdb_successor.c src/include/skalibs/cdb.h src/libstddjb/cdb_zero.o src/libstddjb/cdb_zero.lo: src/libstddjb/cdb_zero.c src/include/skalibs/cdb.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/sig.h src/include/skalibs/strerr2.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/sig.h src/include/skalibs/strerr2.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/sig.h src/include/skalibs/strerr2.h src/include/skalibs/sysdeps.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/strerr2.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/strerr2.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/strerr2.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/webipc.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/sig.h src/include/skalibs/strerr2.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/sig.h src/include/skalibs/strerr2.h src/include/skalibs/sysdeps.h src/include/skalibs/types.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/strerr2.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/strerr2.h src/include/skalibs/sysdeps.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/deepsleepuntil.o src/libstddjb/deepsleepuntil.lo: src/libstddjb/deepsleepuntil.c src/include/skalibs/iopause.h src/include/skalibs/tai.h src/libstddjb/dir_close.o src/libstddjb/dir_close.lo: src/libstddjb/dir_close.c src/include/skalibs/direntry.h @@ -752,8 +748,6 @@ src/libstddjb/waitn_reap.o src/libstddjb/waitn_reap.lo: src/libstddjb/waitn_reap src/libstddjb/waitpid_nointr.o src/libstddjb/waitpid_nointr.lo: src/libstddjb/waitpid_nointr.c src/include/skalibs/djbunix.h src/libstddjb/writenclose_unsafe.o src/libstddjb/writenclose_unsafe.lo: src/libstddjb/writenclose_unsafe.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/libstddjb/writevnclose_unsafe.o src/libstddjb/writevnclose_unsafe.lo: src/libstddjb/writevnclose_unsafe.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/siovec.h -src/libstddjb/xexecvep.o src/libstddjb/xexecvep.lo: src/libstddjb/xexecvep.c src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h src/include/skalibs/strerr2.h -src/libstddjb/xpathexec_run.o src/libstddjb/xpathexec_run.lo: src/libstddjb/xpathexec_run.c src/include/skalibs/djbunix.h src/include/skalibs/strerr2.h src/libunixonacid/access_at.o src/libunixonacid/access_at.lo: src/libunixonacid/access_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/ancil_recv_fd.o src/libunixonacid/ancil_recv_fd.lo: src/libunixonacid/ancil_recv_fd.c src/include/skalibs/allreadwrite.h src/include/skalibs/ancil.h src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/posixishard.h src/include/skalibs/sysdeps.h src/libunixonacid/ancil_send_fd.o src/libunixonacid/ancil_send_fd.lo: src/libunixonacid/ancil_send_fd.c src/include/skalibs/ancil.h src/include/skalibs/nonposix.h src/include/skalibs/posixishard.h diff --git a/src/libstddjb/child_spawn.c b/src/libstddjb/child_spawn.c index 58bbfc8..4ac1dde 100644 --- a/src/libstddjb/child_spawn.c +++ b/src/libstddjb/child_spawn.c @@ -21,6 +21,7 @@ #include <skalibs/sig.h> #include <skalibs/strerr2.h> +#include <skalibs/exec.h> #endif @@ -37,25 +38,27 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const *envp, int *fds, unsigned int n) { + pid_t pid ; #ifdef SKALIBS_HASPOSIXSPAWN posix_spawn_file_actions_t actions ; posix_spawnattr_t attr ; + int e ; #else int syncpipe[2] ; #endif int p[n ? n : 1][2] ; - pid_t pid ; - int e ; size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ; unsigned int i = 0 ; char modifs[m + 1 + n * UINT_FMT] ; memcpy(modifs, SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=", sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR)) ; - for (; i < n ; i++) if (pipe(p[i]) < 0) { e = errno ; goto errpi ; } - for (i = 0 ; i < n ; i++) + for (; i < n ; i++) + { + if (pipe(p[i]) < 0) goto errpi ; if ((ndelay_on(p[i][i & 1]) < 0) || (coe(p[i][i & 1]) < 0)) { - e = errno ; goto errp ; + i++ ; goto errpi ; } + } for (i = 2 ; i < n ; i++) { m += uint_fmt(modifs + m, p[i][!(i & 1)]) ; @@ -66,7 +69,7 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const #ifdef SKALIBS_HASPOSIXSPAWN e = posix_spawnattr_init(&attr) ; - if (e) goto errsp ; + if (e) goto erre ; { sigset_t set ; sigemptyset(&set) ; @@ -92,16 +95,16 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const if (e) goto erractions ; } { - int haspath = !!getenv("PATH") ; + int nopath = !getenv("PATH") ; size_t envlen = env_len(envp) ; char const *newenv[envlen + 2] ; - if (!env_merge(newenv, envlen+2, envp, envlen, modifs, m)) goto erractions ; - if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) + 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 (!haspath) unsetenv("PATH") ; + if (nopath) unsetenv("PATH") ; if (e) goto erractions ; } @@ -109,11 +112,10 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const posix_spawnattr_destroy(&attr) ; #else - if (pipe(syncpipe) < 0) { e = errno ; goto errp ; } - if (coe(syncpipe[1]) < 0) { e = errno ; goto errsp ; } + if (pipecoe(syncpipe) < 0) goto errp ; pid = fork() ; - if (pid < 0) { e = errno ; goto errsp ; } + if (pid < 0) goto errsp ; else if (!pid) { size_t len = strlen(PROG) ; @@ -121,7 +123,6 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const memcpy(name, PROG, len) ; memcpy(name + len, " (child)", 9) ; PROG = name ; - fd_close(syncpipe[0]) ; if (n >= 2) { if (fd_move2(0, p[1][0], 1, p[0][1]) < 0) goto syncdie ; @@ -131,7 +132,7 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const if (fd_move(1, p[0][1]) < 0) goto syncdie ; } sig_blocknone() ; - pathexec_r_name(prog, argv, envp, env_len(envp), modifs, m) ; + mexec_aen(prog, argv, envp, modifs, m, 1) ; syncdie: { @@ -147,13 +148,10 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; if (syncpipe[1]) { + int e = c ; if (syncpipe[1] < 0) e = errno ; - else - { - kill(pid, SIGKILL) ; - e = c ; - } - wait_pid(pid, &syncpipe[1]) ; + if (wait_pid(pid, &syncpipe[1]) < 0) e = errno ; + errno = e ; goto errsp0 ; } } @@ -172,14 +170,16 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const posix_spawn_file_actions_destroy(&actions) ; errattr: posix_spawnattr_destroy(&attr) ; + erre: + errno = e ; #endif - errsp: #ifndef SKALIBS_HASPOSIXSPAWN + errsp: fd_close(syncpipe[1]) ; errsp0: fd_close(syncpipe[0]) ; -#endif errp: +#endif i = n ; errpi: while (i--) @@ -187,6 +187,5 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const fd_close(p[i][1]) ; fd_close(p[i][0]) ; } - errno = e ; return 0 ; } diff --git a/src/libstddjb/child_spawn0.c b/src/libstddjb/child_spawn0.c index 31d44e2..7b95b9f 100644 --- a/src/libstddjb/child_spawn0.c +++ b/src/libstddjb/child_spawn0.c @@ -8,14 +8,15 @@ #include <signal.h> #include <spawn.h> #include <stdlib.h> + #include <skalibs/config.h> pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const *envp) { + pid_t pid ; posix_spawnattr_t attr ; int e ; - pid_t pid ; - int haspath = !!getenv("PATH") ; + int nopath = !getenv("PATH") ; e = posix_spawnattr_init(&attr) ; if (e) goto err ; { @@ -26,9 +27,9 @@ pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ; if (e) goto errattr ; } - if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; 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 (!haspath) unsetenv("PATH") ; + if (nopath) unsetenv("PATH") ; posix_spawnattr_destroy(&attr) ; if (e) goto err ; return pid ; @@ -44,16 +45,18 @@ pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const #include <unistd.h> #include <string.h> + #include <skalibs/allreadwrite.h> #include <skalibs/strerr2.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 e ; int p[2] ; - pid_t pid ; if (pipecoe(p) < 0) return 0 ; pid = fork() ; if (pid < 0) @@ -71,7 +74,7 @@ pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const PROG = name ; fd_close(p[0]) ; sig_blocknone() ; - pathexec_run(prog, argv, envp) ; + exec_ae(prog, argv, envp) ; e = errno ; fd_write(p[1], (char *)&e, sizeof(e)) ; _exit(127) ; diff --git a/src/libstddjb/child_spawn1_internal.c b/src/libstddjb/child_spawn1_internal.c index 116a65e..460bca0 100644 --- a/src/libstddjb/child_spawn1_internal.c +++ b/src/libstddjb/child_spawn1_internal.c @@ -9,19 +9,20 @@ #include <signal.h> #include <spawn.h> #include <stdlib.h> + #include <skalibs/config.h> #include <skalibs/djbunix.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 ; int e ; - pid_t pid ; - int haspath = !!getenv("PATH") ; - if (coe(p[!(to & 1)]) < 0) { e = errno ; goto err ; } + int nopath = !getenv("PATH") ; + if (coe(p[!(to & 1)]) < 0) goto err ; e = posix_spawnattr_init(&attr) ; - if (e) goto err ; + if (e) goto erre ; { sigset_t set ; sigemptyset(&set) ; @@ -44,55 +45,45 @@ pid_t child_spawn1_internal (char const *prog, char const *const *argv, char con e = posix_spawn_file_actions_adddup2(&actions, to & 1, !(to & 1)) ; if (e) goto erractions ; } - if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; 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 (!haspath) unsetenv("PATH") ; + if (nopath) unsetenv("PATH") ; + if (e) goto erractions ; posix_spawn_file_actions_destroy(&actions) ; posix_spawnattr_destroy(&attr) ; fd_close(p[to & 1]) ; - if (e) goto errp ; return pid ; erractions: posix_spawn_file_actions_destroy(&actions) ; errattr: posix_spawnattr_destroy(&attr) ; - err: - fd_close(p[to & 1]) ; - errp: - fd_close(p[!(to & 1)]) ; + erre: errno = e ; + err: + fd_close(p[1]) ; + fd_close(p[0]) ; return 0 ; } #else #include <string.h> + #include <skalibs/allreadwrite.h> #include <skalibs/strerr2.h> #include <skalibs/sig.h> #include <skalibs/djbunix.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) { - int e ; - int syncp[2] ; pid_t pid ; - if (coe(p[0]) < 0 || pipecoe(syncp) < 0) - { - fd_close(p[1]) ; - fd_close(p[0]) ; - return 0 ; - } + int syncpipe[2] ; + if (coe(p[0]) < 0 || pipecoe(syncpipe) < 0) goto err ; + pid = fork() ; - if (pid < 0) - { - fd_close(syncp[1]) ; - fd_close(syncp[0]) ; - fd_close(p[1]) ; - fd_close(p[0]) ; - return 0 ; - } + if (pid < 0) goto errsp ; if (!pid) { size_t len = strlen(PROG) ; @@ -100,35 +91,47 @@ pid_t child_spawn1_internal (char const *prog, char const *const *argv, char con memcpy(name, PROG, len) ; memcpy(name + len, " (child)", 9) ; PROG = name ; - fd_close(syncp[0]) ; fd_close(p[!(to & 1)]) ; - if (fd_move(to & 1, p[to & 1]) < 0) goto err ; - if ((to & 2) && (fd_copy(!(to & 1), to & 1) < 0)) goto err ; + 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() ; - pathexec_run(prog, argv, envp) ; - err: - e = errno ; - fd_write(syncp[1], (char *)&e, sizeof(e)) ; + exec_ae(prog, argv, envp) ; + + syncdie: + { + char c = errno ; + fd_write(syncpipe[1], &c, 1) ; + } _exit(127) ; } - fd_close(syncp[1]) ; - fd_close(p[to & 1]) ; - syncp[1] = fd_read(syncp[0], (char *)&e, sizeof(e)) ; - if (syncp[1] < 0) + + fd_close(syncpipe[1]) ; + { - fd_close(syncp[0]) ; - fd_close(p[!(to & 1)]) ; - return 0 ; - } - fd_close(syncp[0]) ; - if (syncp[1] == sizeof(e)) - { - fd_close(p[!(to & 1)]) ; - wait_pid(pid, &syncp[1]) ; - errno = e ; - return 0 ; + char c ; + syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; + if (syncpipe[1]) + { + int e = c ; + if (syncpipe[1] < 0) e = errno ; + if (wait_pid(pid, &syncpipe[1]) < 0) 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_spawn2.c b/src/libstddjb/child_spawn2.c index 4e7d0a5..03db0d5 100644 --- a/src/libstddjb/child_spawn2.c +++ b/src/libstddjb/child_spawn2.c @@ -22,6 +22,7 @@ #include <skalibs/allreadwrite.h> #include <skalibs/sig.h> #include <skalibs/strerr2.h> +#include <skalibs/exec.h> #endif @@ -29,23 +30,23 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const *envp, int *fds) { + pid_t pid ; #ifdef SKALIBS_HASPOSIXSPAWN posix_spawn_file_actions_t actions ; posix_spawnattr_t attr ; + int e ; #else int syncpipe[2] ; #endif int p[2][2] ; - pid_t pid ; - int e ; if (pipe(p[0]) < 0) return 0 ; - if (ndelay_on(p[0][0]) < 0 || coe(p[0][0]) < 0 || pipe(p[1]) < 0) { e = errno ; goto errp ; } - if (ndelay_on(p[1][1]) < 0 || coe(p[1][1]) < 0) { e = errno ; goto errp1 ; } + 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 ; #ifdef SKALIBS_HASPOSIXSPAWN e = posix_spawnattr_init(&attr) ; - if (e) goto errsp ; + if (e) goto erre ; { sigset_t set ; sigemptyset(&set) ; @@ -71,13 +72,13 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const if (e) goto erractions ; } { - int haspath = !!getenv("PATH") ; - if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) + int nopath = !getenv("PATH") ; + 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 (!haspath) unsetenv("PATH") ; + if (nopath) unsetenv("PATH") ; if (e) goto erractions ; } @@ -85,11 +86,10 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const posix_spawnattr_destroy(&attr) ; #else - if (pipe(syncpipe) < 0) { e = errno ; goto errp1 ; } - if (coe(syncpipe[1]) < 0) { e = errno ; goto errsp ; } + if (pipecoe(syncpipe) < 0) goto errp1 ; pid = fork() ; - if (pid < 0) { e = errno ; goto errsp ; } + if (pid < 0) goto errsp ; else if (!pid) { size_t len = strlen(PROG) ; @@ -97,10 +97,9 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const memcpy(name, PROG, len) ; memcpy(name + len, " (child)", 9) ; PROG = name ; - fd_close(syncpipe[0]) ; if (fd_move2(fds[0], p[1][0], fds[1], p[0][1]) < 0) goto syncdie ; sig_blocknone() ; - pathexec_run(prog, argv, envp) ; + exec_ae(prog, argv, envp) ; syncdie: { @@ -116,13 +115,10 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; if (syncpipe[1]) { + int e = c ; if (syncpipe[1] < 0) e = errno ; - else - { - kill(pid, SIGKILL) ; - e = c ; - } - wait_pid(pid, &syncpipe[1]) ; + if (wait_pid(pid, &syncpipe[1]) < 0) e = errno ; + errno = e ; goto errsp0 ; } } @@ -138,9 +134,11 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const posix_spawn_file_actions_destroy(&actions) ; errattr: posix_spawnattr_destroy(&attr) ; + erre: + errno = e ; #endif - errsp: #ifndef SKALIBS_HASPOSIXSPAWN + errsp: fd_close(syncpipe[1]) ; errsp0: fd_close(syncpipe[0]) ; @@ -151,6 +149,5 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const errp: fd_close(p[0][1]) ; fd_close(p[0][0]) ; - errno = e ; return 0 ; } diff --git a/src/libstddjb/child_spawn3.c b/src/libstddjb/child_spawn3.c index 9dbed41..e1e433a 100644 --- a/src/libstddjb/child_spawn3.c +++ b/src/libstddjb/child_spawn3.c @@ -21,6 +21,7 @@ #include <string.h> #include <skalibs/sig.h> #include <skalibs/strerr2.h> +#include <skalibs/exec.h> #endif @@ -31,15 +32,15 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const *envp, int *fds) { + pid_t pid ; #ifdef SKALIBS_HASPOSIXSPAWN posix_spawn_file_actions_t actions ; posix_spawnattr_t attr ; + int e ; #else int syncpipe[2] ; #endif int p[3][2] ; - pid_t pid ; - int e ; size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ; char modifs[sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) + UINT_FMT] = SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=" ; if (pipe(p[0]) < 0 || ndelay_on(p[0][0]) < 0 || coe(p[0][0]) < 0) return 0 ; @@ -51,7 +52,7 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const #ifdef SKALIBS_HASPOSIXSPAWN e = posix_spawnattr_init(&attr) ; - if (e) goto errp2 ; + if (e) goto erre ; { sigset_t set ; sigemptyset(&set) ; @@ -77,16 +78,16 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const if (e) goto erractions ; } { - int haspath = !!getenv("PATH") ; + int nopath = !getenv("PATH") ; size_t envlen = env_len(envp) ; char const *newenv[envlen + 2] ; - if (!env_merge(newenv, envlen+2, envp, envlen, modifs, m)) goto erractions ; - if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) + 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 (!haspath) unsetenv("PATH") ; + if (nopath) unsetenv("PATH") ; if (e) goto erractions ; } @@ -94,11 +95,10 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const posix_spawnattr_destroy(&attr) ; #else - if (pipe(syncpipe) < 0) { e = errno ; goto errp2 ; } - if (coe(syncpipe[1]) < 0) { e = errno ; goto errsp ; } + if (pipecoe(syncpipe) < 0) goto errp2 ; pid = fork() ; - if (pid < 0) { e = errno ; goto errsp ; } + if (pid < 0) goto errsp ; else if (!pid) { size_t len = strlen(PROG) ; @@ -106,10 +106,9 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const memcpy(name, PROG, len) ; memcpy(name + len, " (child)", 9) ; PROG = name ; - fd_close(syncpipe[0]) ; if (fd_move2(fds[0], p[1][0], fds[1], p[0][1]) < 0) goto syncdie ; sig_blocknone() ; - pathexec_r_name(prog, argv, envp, env_len(envp), modifs, m) ; + mexec_aen(prog, argv, envp, modifs, m, 1) ; syncdie: { @@ -125,13 +124,10 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; if (syncpipe[1]) { + int e = c ; if (syncpipe[1] < 0) e = errno ; - else - { - kill(pid, SIGKILL) ; - e = c ; - } - wait_pid(pid, &syncpipe[1]) ; + if (wait_pid(pid, &syncpipe[1]) < 0) e = errno ; + errno = e ; goto errsp0 ; } } @@ -151,14 +147,16 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const posix_spawn_file_actions_destroy(&actions) ; errattr: posix_spawnattr_destroy(&attr) ; + erre: + errno = e ; #endif #ifndef SKALIBS_HASPOSIXSPAWN errsp: fd_close(syncpipe[1]) ; errsp0: fd_close(syncpipe[0]) ; -#endif errp2: +#endif fd_close(p[2][1]) ; fd_close(p[2][0]) ; errp1: |