summaryrefslogtreecommitdiff
path: root/src/tls/s6tls_io_spawn.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-09-08 19:38:35 +0000
committerLaurent Bercot <ska@appnovation.com>2023-09-08 19:38:35 +0000
commitfdd1f0c0ec33575a78b589b46d095c3a4a0d8510 (patch)
tree231894189c8baf3833418afe1e9fc6587c19d091 /src/tls/s6tls_io_spawn.c
parent7113c4cf792ec44ec04a54e9723fa31a97bee5a7 (diff)
downloads6-networking-fdd1f0c0ec33575a78b589b46d095c3a4a0d8510.tar.xz
Fully defork s6-tlsc and s6-tlsd
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/tls/s6tls_io_spawn.c')
-rw-r--r--src/tls/s6tls_io_spawn.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/src/tls/s6tls_io_spawn.c b/src/tls/s6tls_io_spawn.c
new file mode 100644
index 0000000..e2d589a
--- /dev/null
+++ b/src/tls/s6tls_io_spawn.c
@@ -0,0 +1,104 @@
+/* ISC license. */
+
+#include <skalibs/sysdeps.h>
+
+#include <skalibs/djbunix.h>
+
+#include "s6tls-internal.h"
+
+#ifdef SKALIBS_HASPOSIXSPAWN
+
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <spawn.h>
+
+#include <skalibs/config.h>
+#include <skalibs/posixplz.h>
+
+pid_t s6tls_io_spawn (char const *const *argv, int const p[4][2])
+{
+ pid_t pid ;
+ posix_spawn_file_actions_t actions ;
+ int e ;
+ int nopath = !getenv("PATH") ;
+#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
+ int eep[2] ;
+ if (pipecoe(eep) == -1) return 0 ;
+#endif
+ e = posix_spawn_file_actions_init(&actions) ;
+ if (e) goto err ;
+ e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ;
+ if (e) goto erractions ;
+ e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ;
+ if (e) goto erractions ;
+ e = posix_spawn_file_actions_addclose(&actions, p[2][0]) ;
+ if (e) goto erractions ;
+ if (p[3][0] >= 0)
+ {
+ e = posix_spawn_file_actions_adddup2(&actions, p[3][0], 0) ;
+ if (e) goto erractions ;
+ e = posix_spawn_file_actions_addclose(&actions, p[3][0]) ;
+ if (e) goto erractions ;
+ }
+ if (p[3][1] >= 0)
+ {
+ e = posix_spawn_file_actions_adddup2(&actions, p[3][1], 1) ;
+ if (e) goto erractions ;
+ e = posix_spawn_file_actions_addclose(&actions, p[3][1]) ;
+ if (e) goto erractions ;
+ }
+
+ if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; goto erractions ; }
+ e = posix_spawnp(&pid, argv[0], &actions, 0, (char *const *)argv, environ) ;
+ if (nopath) unsetenv("PATH") ;
+ if (e) goto erractions ;
+
+ posix_spawn_file_actions_destroy(&actions) ;
+#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
+ return child_spawn_workaround(pid, eep) ;
+#else
+ return pid ;
+#endif
+
+ erractions:
+ posix_spawn_file_actions_destroy(&actions) ;
+ err:
+#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
+ fd_close(eep[1]) ;
+ fd_close(eep[0]) ;
+#endif
+ errno = e ;
+ return 0 ;
+}
+
+#else
+
+#include <string.h>
+
+#include <skalibs/strerr.h>
+#include <skalibs/exec.h>
+
+pid_t s6tls_io_spawn (char const *const *argv, int const p[4][2])
+{
+ pid_t pid = fork() ;
+ if (pid == -1) return 0 ;
+ if (!pid)
+ {
+ size_t proglen = strlen(PROG) ;
+ char newprog[proglen + 9] ;
+ memcpy(newprog, PROG, proglen) ;
+ memcpy(newprog, " (child)", 9) ;
+ PROG = newprog ;
+ fd_close(p[0][1]) ;
+ fd_close(p[1][0]) ;
+ fd_close(p[2][0]) ;
+ if ((p[3][0] >= 0 && fd_move(0, p[3][0]) == -1)
+ || (p[3][1] >= 0 && fd_move(1, p[3][1]) == -1))
+ strerr_diefu1sys(111, "move network fds to stdin/stdout") ;
+ xexec(argv) ;
+ }
+ return pid ;
+}
+
+#endif