summaryrefslogtreecommitdiff
path: root/src/libstddjb/child_spawn0.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
commit3534b428629be185e096be99e3bd5fdfe32d5544 (patch)
tree210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libstddjb/child_spawn0.c
downloadskalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libstddjb/child_spawn0.c')
-rw-r--r--src/libstddjb/child_spawn0.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/libstddjb/child_spawn0.c b/src/libstddjb/child_spawn0.c
new file mode 100644
index 0000000..b96b2ef
--- /dev/null
+++ b/src/libstddjb/child_spawn0.c
@@ -0,0 +1,73 @@
+/* ISC license. */
+
+#include <skalibs/sysdeps.h>
+#include <sys/types.h>
+#include <errno.h>
+
+#ifdef SKALIBS_HASPOSIXSPAWN
+
+#include <spawn.h>
+#include <stdlib.h>
+#include <skalibs/config.h>
+#include <skalibs/env.h>
+
+pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const *envp)
+{
+ pid_t pid ;
+ int e ;
+ int haspath = !!env_get("PATH") ;
+ if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) return 0 ;
+ e = posix_spawnp(&pid, prog, 0, 0, (char *const *)argv, (char *const *)envp) ;
+ if (!haspath) unsetenv("PATH") ;
+ return e ? (errno = e, 0) : pid ;
+}
+
+#else
+
+#include <unistd.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/djbunix.h>
+
+pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const *envp)
+{
+ int e ;
+ int p[2] ;
+ pid_t pid ;
+ if (pipecoe(p) < 0) return 0 ;
+ pid = fork() ;
+ if (pid < 0)
+ {
+ e = errno ;
+ fd_close(p[1]) ;
+ fd_close(p[0]) ;
+ errno = e ;
+ return 0 ;
+ }
+ if (pid)
+ {
+ fd_close(p[0]) ;
+ pathexec_run(prog, argv, envp) ;
+ e = errno ;
+ fd_write(p[1], (char *)&e, sizeof(e)) ;
+ _exit(127) ;
+ }
+ fd_close(p[1]) ;
+ p[1] = fd_read(p[0], (char *)&e, sizeof(e)) ;
+ if (p[1] < 0)
+ {
+ e = errno ;
+ fd_close(p[0]) ;
+ errno = e ;
+ return 0 ;
+ }
+ fd_close(p[0]) ;
+ if (p[1] == sizeof(e))
+ {
+ wait_pid(pid, &p[0]) ;
+ errno = e ;
+ return 0 ;
+ }
+ return pid ;
+}
+
+#endif