summaryrefslogtreecommitdiff
path: root/src/libenvexec/gcspawn.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2024-04-30 19:09:43 +0000
committerLaurent Bercot <ska@appnovation.com>2024-04-30 19:09:43 +0000
commit02926ee3447b1ea0d04b53b8fcb08d8b1a4deec5 (patch)
treef0cffb716e9ba61382795b460203df506e13ed4b /src/libenvexec/gcspawn.c
parent944a9d260a41b30f32730ccb12b3f5dafb507b7d (diff)
downloadskalibs-02926ee3447b1ea0d04b53b8fcb08d8b1a4deec5.tar.xz
Add mspawn functions to cspawn.h; move everything to libenvexec
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libenvexec/gcspawn.c')
-rw-r--r--src/libenvexec/gcspawn.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/libenvexec/gcspawn.c b/src/libenvexec/gcspawn.c
new file mode 100644
index 0000000..7e9e602
--- /dev/null
+++ b/src/libenvexec/gcspawn.c
@@ -0,0 +1,48 @@
+/* ISC license. */
+
+#include <sys/wait.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <skalibs/types.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/cspawn.h>
+
+pid_t gcspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n)
+{
+ pid_t pid = 0 ;
+ int wstat ;
+ int p[2] ;
+ char pack[PID_PACK] ;
+ if (pipecoe(p) == -1) return 0 ;
+ pid = fork() ;
+ switch (pid)
+ {
+ case -1:
+ {
+ fd_close(p[1]) ;
+ fd_close(p[0]) ;
+ return 0 ;
+ }
+ case 0:
+ {
+ fd_close(p[0]) ;
+ pid = cspawn(prog, argv, envp, flags, fa, n) ;
+ if (!pid) _exit(errno) ;
+ pid_pack_big(pack, pid) ;
+ _exit(fd_write(p[1], pack, PID_PACK) < PID_PACK ? errno : 0) ;
+ }
+ }
+ fd_close(p[1]) ;
+ if (fd_read(p[0], pack, PID_PACK) < PID_PACK) goto err ;
+ fd_close(p[0]) ;
+ wait_pid(pid, &wstat) ;
+ pid_unpack_big(pack, &pid) ;
+ return pid ;
+
+ err:
+ fd_close(p[0]) ;
+ wait_pid(pid, &wstat) ;
+ return (errno = WIFSIGNALED(wstat) ? EINTR : WEXITSTATUS(wstat), 0) ;
+}