summaryrefslogtreecommitdiff
path: root/src/libstddjb/cspawn.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-09-10 04:59:41 +0000
committerLaurent Bercot <ska@appnovation.com>2023-09-10 04:59:41 +0000
commit252e2ad869637ca3ef8690854a8fac784b869ec9 (patch)
tree4c5b015828fdfc70fcc920b858892688d5c6ce8b /src/libstddjb/cspawn.c
parent8ba811fb48d666a23d60a5ce3a947dcac7baf6ea (diff)
downloadskalibs-252e2ad869637ca3ef8690854a8fac784b869ec9.tar.xz
Add POSIX_SPAWN_SETSID support to cspawn
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libstddjb/cspawn.c')
-rw-r--r--src/libstddjb/cspawn.c168
1 files changed, 97 insertions, 71 deletions
diff --git a/src/libstddjb/cspawn.c b/src/libstddjb/cspawn.c
index 4db86e0..a4c0497 100644
--- a/src/libstddjb/cspawn.c
+++ b/src/libstddjb/cspawn.c
@@ -2,10 +2,93 @@
#include <skalibs/sysdeps.h>
+#ifdef SKALIBS_HASPOSIXSPAWN
+#include <skalibs/nonposix.h>
+#endif
+
#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <skalibs/allreadwrite.h>
+#include <skalibs/sig.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/selfpipe.h>
+#include <skalibs/exec.h>
#include <skalibs/cspawn.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() ;
+ if (flags & CSPAWN_FLAGS_SETSID) setsid() ;
+
+ exec_ae(prog, argv, envp) ;
+}
+
+static inline pid_t cspawn_fork (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 ;
+}
+
#ifdef SKALIBS_HASPOSIXSPAWN
#include <signal.h>
@@ -19,8 +102,6 @@
#include <sys/wait.h>
-#include <skalibs/allreadwrite.h>
-
static inline pid_t cspawn_workaround (pid_t pid, int const *p)
{
siginfo_t si ;
@@ -50,7 +131,7 @@ static inline pid_t cspawn_workaround (pid_t pid, int const *p)
#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)
+static inline pid_t cspawn_pspawn (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 ;
@@ -139,82 +220,27 @@ pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp
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>
+#ifdef SKALIBS_HASPOSIXSPAWNSETSID
-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)
+pid_t cspawn (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() ;
+ return cspawn_pspawn(prog, argv, envp, flags, fa, n) ;
}
+#else
+
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 ;
+ return flags & CSPAWN_FLAGS_SETSID ? cspawn_fork(prog, argv, envp, flags, fa, n) : cspawn_pspawn(prog, argv, envp, flags, fa, n) ;
+}
- 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) ;
- }
+#endif
- 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 ;
+#else
+
+pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n)
+{
+ return cspawn_fork(prog, argv, envp, flags, fa, n) ;
}
#endif