about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-09-09 04:35:20 +0000
committerLaurent Bercot <ska@appnovation.com>2023-09-09 04:35:20 +0000
commit1f592173a28907dadfb75132e1eb25dfe4fb3a94 (patch)
tree1759ad59cf444b3c151aa2af185a96b1891ae477 /src
parent8f0980665c368c06adb46a1fddb6790e945c7c51 (diff)
downloads6-1f592173a28907dadfb75132e1eb25dfe4fb3a94.tar.gz
s6-1f592173a28907dadfb75132e1eb25dfe4fb3a94.tar.xz
s6-1f592173a28907dadfb75132e1eb25dfe4fb3a94.zip
Adapt to skalibs-2.14
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r--src/daemontools-extras/s6-setlock.c1
-rw-r--r--src/libs6/s6_ucspiserver_spawn.c110
-rw-r--r--src/libs6/s6lockd.c1
-rw-r--r--src/pipe-tools/s6-ftrig-listen.c3
-rw-r--r--src/pipe-tools/s6-ftrig-listen1.c3
-rw-r--r--src/supervision/s6-notifyoncheck.c3
-rw-r--r--src/supervision/s6-svlisten.c4
-rw-r--r--src/supervision/s6-svlisten1.c4
-rw-r--r--src/supervision/s6-svscan.c54
9 files changed, 49 insertions, 134 deletions
diff --git a/src/daemontools-extras/s6-setlock.c b/src/daemontools-extras/s6-setlock.c
index 8eeb95b..fea6a11 100644
--- a/src/daemontools-extras/s6-setlock.c
+++ b/src/daemontools-extras/s6-setlock.c
@@ -10,6 +10,7 @@
 #include <skalibs/types.h>
 #include <skalibs/tai.h>
 #include <skalibs/iopause.h>
+#include <skalibs/cspawn.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/exec.h>
 
diff --git a/src/libs6/s6_ucspiserver_spawn.c b/src/libs6/s6_ucspiserver_spawn.c
index 7fd4116..23f102d 100644
--- a/src/libs6/s6_ucspiserver_spawn.c
+++ b/src/libs6/s6_ucspiserver_spawn.c
@@ -1,112 +1,18 @@
 /* ISC license. */
 
-#include <skalibs/sysdeps.h>
-
-#include <stdint.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <skalibs/uint16.h>
-#include <skalibs/fmtscan.h>
-#include <skalibs/djbunix.h>
+#include <skalibs/env.h>
+#include <skalibs/cspawn.h>
 
 #include <s6/ucspiserver.h>
 
-#ifdef SKALIBS_HASPOSIXSPAWN
-
-#include <errno.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <spawn.h>
-
-#include <skalibs/config.h>
-#include <skalibs/env.h>
-
 pid_t s6_ucspiserver_spawn (int fd, char const *const *argv, char const *const *envp, size_t envlen, char const *modifs, size_t modiflen, size_t modifn)
 {
-  pid_t pid ;
-  posix_spawn_file_actions_t actions ;
-  posix_spawnattr_t attr ;
-  sigset_t set ;
-  int e ;
-  int nopath = !getenv("PATH") ;
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  int p[2] ;
-#endif
-  char const *newenvp[envlen + modifn + 1] ;
-
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  if (pipecoe(p) == -1) return 0 ;
-#endif
-  e = posix_spawnattr_init(&attr) ;
-  if (e) goto err ;
-  sigemptyset(&set) ;
-  e = posix_spawnattr_setsigmask(&attr, &set) ;
-  if (e) goto errattr ;
-  e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ;
-  if (e) goto errattr ;
-  e = posix_spawn_file_actions_init(&actions) ;
-  if (e) goto errattr ;
-  e = posix_spawn_file_actions_adddup2(&actions, fd, 0) ;
-  if (e) goto erractions ;
-  e = posix_spawn_file_actions_adddup2(&actions, fd, 1) ;
-  if (e) goto erractions ;
-  if (fd > 1)
+  cspawn_fileaction fa[2] =
   {
-    e = posix_spawn_file_actions_addclose(&actions, fd) ;
-    if (e) goto erractions ;
-  }
-
+    [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = 0, [1] = fd } } },
+    [1] = { .type = CSPAWN_FA_COPY, .x = { .fd2 = { [0] = 1, [1] = 0 } } }
+  } ;
+  char const *newenvp[envlen + modifn + 1] ;
   env_mergen(newenvp, envlen + modifn + 1, envp, envlen, modifs, modiflen, modifn) ;
-  if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; goto erractions ; }
-  e = posix_spawnp(&pid, argv[0], &actions, &attr, (char *const *)argv, (char *const *)newenvp) ;
-  if (nopath) unsetenv("PATH") ;
-  if (e) goto erractions ;
-
-  posix_spawn_file_actions_destroy(&actions) ;
-  posix_spawnattr_destroy(&attr) ;
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  return child_spawn_workaround(pid, p) ;
-#else
-  return pid ;
-#endif
-
- erractions:
-  posix_spawn_file_actions_destroy(&actions) ;
- errattr:
-  posix_spawnattr_destroy(&attr) ;
- err:
-#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN
-  fd_close(p[1]) ;
-  fd_close(p[0]) ;
-#endif
-  errno = e ;
-  return 0 ;
-}
-
-#else
-
-#include <skalibs/strerr.h>
-#include <skalibs/selfpipe.h>
-#include <skalibs/exec.h>
-
-pid_t s6_ucspiserver_spawn (int fd, char const *const *argv, char const *const *envp, size_t envlen, char const *modifs, size_t modiflen, size_t modifn)
-{
-  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 ;
-    if ((fd_move(1, fd) == -1) || (fd_copy(0, 1) == -1))
-      strerr_diefu1sys(111, "move fds") ;
-    selfpipe_finish() ;
-    xmexec_fn(argv, envp, envlen, modifs, modiflen, modifn) ;
-  }
-  return pid ;
+  return cspawn(argv[0], argv, newenvp, CSPAWN_FLAGS_SELFPIPE_FINISH, fa, 2) ;
 }
-
-#endif
diff --git a/src/libs6/s6lockd.c b/src/libs6/s6lockd.c
index e7303af..8cc767a 100644
--- a/src/libs6/s6lockd.c
+++ b/src/libs6/s6lockd.c
@@ -15,6 +15,7 @@
 #include <skalibs/sig.h>
 #include <skalibs/selfpipe.h>
 #include <skalibs/tai.h>
+#include <skalibs/cspawn.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/iopause.h>
 #include <skalibs/textmessage.h>
diff --git a/src/pipe-tools/s6-ftrig-listen.c b/src/pipe-tools/s6-ftrig-listen.c
index 92e257e..041226e 100644
--- a/src/pipe-tools/s6-ftrig-listen.c
+++ b/src/pipe-tools/s6-ftrig-listen.c
@@ -13,6 +13,7 @@
 #include <skalibs/djbunix.h>
 #include <skalibs/sig.h>
 #include <skalibs/selfpipe.h>
+#include <skalibs/cspawn.h>
 
 #include <s6/compat.h>
 #include <s6/ftrigr.h>
@@ -81,7 +82,7 @@ int main (int argc, char const **argv, char const *const *envp)
       if (!ids[i]) strerr_diefu4sys(111, "subscribe to ", argv[i<<1], " with regexp ", argv[(i<<1)+1]) ;
     }
 
-    pid = child_spawn0(argv[argc1 + 1], argv + argc1 + 1, envp) ;
+    pid = cspawn(argv[argc1 + 1], argv + argc1 + 1, envp, CSPAWN_FLAGS_SELFPIPE_FINISH, 0, 0) ;
     if (!pid) strerr_diefu2sys(111, "spawn ", argv[argc1 + 1]) ;
 
     for (;;)
diff --git a/src/pipe-tools/s6-ftrig-listen1.c b/src/pipe-tools/s6-ftrig-listen1.c
index b81b484..4c42820 100644
--- a/src/pipe-tools/s6-ftrig-listen1.c
+++ b/src/pipe-tools/s6-ftrig-listen1.c
@@ -14,6 +14,7 @@
 #include <skalibs/djbunix.h>
 #include <skalibs/sig.h>
 #include <skalibs/selfpipe.h>
+#include <skalibs/cspawn.h>
 
 #include <s6/ftrigr.h>
 
@@ -69,7 +70,7 @@ int main (int argc, char const *const *argv, char const *const *envp)
   if (!selfpipe_trap(SIGCHLD)) strerr_diefu1sys(111, "selfpipe_trap") ;
   x[1].fd = ftrigr_fd(&a) ;
 
-  pid = child_spawn0(argv[2], argv+2, envp) ;
+  pid = cspawn(argv[2], argv+2, envp, CSPAWN_FLAGS_SELFPIPE_FINISH, 0, 0) ;
   if (!pid) strerr_diefu2sys(111, "spawn ", argv[2]) ;
 
   for (;;)
diff --git a/src/supervision/s6-notifyoncheck.c b/src/supervision/s6-notifyoncheck.c
index 592502c..8c139af 100644
--- a/src/supervision/s6-notifyoncheck.c
+++ b/src/supervision/s6-notifyoncheck.c
@@ -13,6 +13,7 @@
 #include <skalibs/sgetopt.h>
 #include <skalibs/strerr.h>
 #include <skalibs/tai.h>
+#include <skalibs/cspawn.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/selfpipe.h>
 #include <skalibs/iopause.h>
@@ -230,7 +231,7 @@ int main (int argc, char const *const *argv, char const *const *envp)
       if (handle_event(&a, id, 0)) return 2 ;
     }
 
-    pid = child_spawn0(childargv[0], childargv, envp) ;
+    pid = cspawn(childargv[0], childargv, envp, CSPAWN_FLAGS_SELFPIPE_FINISH, 0, 0) ;
     if (!pid)
     {
       strerr_warnwu2sys("spawn ", childargv[0]) ;
diff --git a/src/supervision/s6-svlisten.c b/src/supervision/s6-svlisten.c
index 2ab1d9b..0774ef2 100644
--- a/src/supervision/s6-svlisten.c
+++ b/src/supervision/s6-svlisten.c
@@ -7,7 +7,7 @@
 #include <skalibs/bitarray.h>
 #include <skalibs/tai.h>
 #include <skalibs/strerr.h>
-#include <skalibs/djbunix.h>
+#include <skalibs/cspawn.h>
 #include <skalibs/selfpipe.h>
 #include <skalibs/exec.h>
 
@@ -71,7 +71,7 @@ int main (int argc, char const **argv, char const *const *envp)
     unsigned char upstate[bitarray_div8(argc1)] ;
     unsigned char readystate[bitarray_div8(argc1)] ;
     s6_svlisten_init(argc1, argv, &foo, ids, upstate, readystate, &deadline) ;
-    pid = child_spawn0(argv[argc1 + 1], argv + argc1 + 1, envp) ;
+    pid = cspawn(argv[argc1 + 1], argv + argc1 + 1, envp, CSPAWN_FLAGS_SELFPIPE_FINISH, 0, 0) ;
     if (!pid) strerr_diefu2sys(111, "spawn ", argv[argc1 + 1]) ;
     if (wantup == 2)
     {
diff --git a/src/supervision/s6-svlisten1.c b/src/supervision/s6-svlisten1.c
index ab4f44c..4289e8c 100644
--- a/src/supervision/s6-svlisten1.c
+++ b/src/supervision/s6-svlisten1.c
@@ -6,7 +6,7 @@
 #include <skalibs/types.h>
 #include <skalibs/tai.h>
 #include <skalibs/strerr.h>
-#include <skalibs/djbunix.h>
+#include <skalibs/cspawn.h>
 #include <skalibs/selfpipe.h>
 
 #include "s6-svlisten.h"
@@ -50,7 +50,7 @@ int main (int argc, char const *const *argv, char const *const *envp)
   tain_add_g(&deadline, &tto) ;
   s6_svlisten_selfpipe_init() ;
   s6_svlisten_init(1, argv, &foo, &id, &upstate, &readystate, &deadline) ;
-  pid = child_spawn0(argv[1], argv + 1, envp) ;
+  pid = cspawn(argv[1], argv + 1, envp, CSPAWN_FLAGS_SELFPIPE_FINISH, 0, 0) ;
   if (!pid) strerr_diefu2sys(111, "spawn ", argv[1]) ;
   if (wantrestart)
     if (s6_svlisten_loop(&foo, 0, 1, 1, &deadline, selfpipe_fd(), &s6_svlisten_signal_handler))
diff --git a/src/supervision/s6-svscan.c b/src/supervision/s6-svscan.c
index b2edc93..92f4d7a 100644
--- a/src/supervision/s6-svscan.c
+++ b/src/supervision/s6-svscan.c
@@ -17,6 +17,7 @@
 #include <skalibs/tai.h>
 #include <skalibs/iopause.h>
 #include <skalibs/devino.h>
+#include <skalibs/cspawn.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/direntry.h>
 #include <skalibs/sig.h>
@@ -242,7 +243,8 @@ static void handle_signals (unsigned int *what)
         memcpy(fn + SIGNAL_PROG_LEN, name, len + 1) ;
         if (access(newargv[0], X_OK) == 0)  /* avoids a fork, don't care about the toctou */
         {
-          if (child_spawn0(newargv[0], newargv, (char const **)environ)) usebuiltin = 0 ;
+          if (cspawn(newargv[0], newargv, (char const **)environ, CSPAWN_FLAGS_SELFPIPE_FINISH, 0, 0))
+            usebuiltin = 0 ;
           else if (errno != ENOENT) strerr_warnwu2sys("spawn ", newargv[0]) ;
         }
         if (usebuiltin) switch (sig)
@@ -530,34 +532,36 @@ static int start_iter (void *data, void *aux)
 {
   service *sv = data ;
   uint32_t i = sv - SERVICE(0) ;
+  char const *const cargv[3] = { "s6-supervise", NAME(i), 0 } ;
+  cspawn_fileaction fa[2] =
+  {
+    [0] = { .type = CSPAWN_FA_MOVE },
+    [1] = { .type = CSPAWN_FA_MOVE }
+  } ;
+  size_t j = 0 ;
+
   if (!bitarray_peek(active, i)
    || sv->pid
    || tain_future(&sv->start)) return 1 ;
-  sv->pid = fork() ;
-  switch (sv->pid)
+  if (sv->peer < max)
   {
-    case -1 :
-      sv->pid = 0 ;
-      strerr_warnwu2sys("fork", NAME(i)) ;
-      tain_addsec_g(&start_deadline, 10) ;
-      return 0 ;
-    case 0 :
-    {
-      char const *cargv[3] = { "s6-supervise", NAME(i), 0 } ;
-      PROG = "s6-svscan (child)" ;
-      if (sv->peer < max)
-      {
-        if (fd_move(!is_logger(i), sv->p) == -1)
-          strerr_diefu2sys(111, "dup2 pipe for ", NAME(i)) ;
-      }
-      if (consoleholder && i == special)
-      {
-        if (fd_move(2, consoleholder) == -1)
-         strerr_diefu2sys(111, "restore console fd for service ", NAME(i)) ;
-      }
-      selfpipe_finish() ;
-      xexec_a(S6_BINPREFIX "s6-supervise", cargv) ;
-    }
+    fa[j].x.fd2[0] = !is_logger(i) ;
+    fa[j].x.fd2[1] = sv->p ;
+    j++ ;
+  }
+  if (consoleholder && i == special)
+  {
+    fa[j].x.fd2[0] = 2 ;
+    fa[j].x.fd2[1] = consoleholder ;
+    j++ ;
+  }
+  
+  sv->pid = cspawn(S6_BINPREFIX "s6-supervise", cargv, (char const *const *)environ, CSPAWN_FLAGS_SELFPIPE_FINISH, fa, j) ;
+  if (!sv->pid)
+  {
+    strerr_warnwu2sys("spawn s6-supervise for ", NAME(i)) ;
+    tain_addsec_g(&start_deadline, 10) ;
+    return 0 ;
   }
   avltreen_insert(by_pid, i) ;
   tain_addsec_g(&sv->start, 1) ;