aboutsummaryrefslogtreecommitdiffstats
path: root/src/s6/service_startstop.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2025-06-30 14:41:53 +0000
committerLaurent Bercot <ska@appnovation.com>2025-06-30 14:41:53 +0000
commit6d953b22ae50261812b27ba84ecd6dc33b2007de (patch)
tree82d61de117f1e8ec02522c3d2eb40bd78877c543 /src/s6/service_startstop.c
parent8a6eaed305dbe4ec6a87fe37000f7461bf563937 (diff)
downloads6-frontend-6d953b22ae50261812b27ba84ecd6dc33b2007de.tar.gz
Adapt to new gol
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/s6/service_startstop.c')
-rw-r--r--src/s6/service_startstop.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/s6/service_startstop.c b/src/s6/service_startstop.c
new file mode 100644
index 0000000..c4946fc
--- /dev/null
+++ b/src/s6/service_startstop.c
@@ -0,0 +1,129 @@
+/* ISC license. */
+
+#include <string.h>
+#include <sys/wait.h>
+
+#include <skalibs/posixplz.h>
+#include <skalibs/uint64.h>
+#include <skalibs/types.h>
+#include <skalibs/strerr.h>
+#include <skalibs/gol.h>
+#include <skalibs/env.h>
+#include <skalibs/cspawn.h>
+#include <skalibs/djbunix.h>
+
+#include <s6-rc/config.h>
+#include <s6-rc/s6rc-utils.h>
+
+#include "s6-internal.h"
+
+enum golb_e
+{
+ GOLB_DRYRUN,
+ GOLB_WAIT,
+ GOLB_N
+} ;
+
+enum gola_e
+{
+ GOLA_TIMEOUT,
+ GOLA_N
+} ;
+
+static gol_bool const rgolb[3] =
+{
+ { .so = 'n', .lo = "dryrun", .clear = 0, .set = 1 << GOLB_DRYRUN },
+ { .so = 'W', .lo = "nowait", .clear = 1 << GOLB_WAIT, .set = 0 },
+ { .so = 'w', .lo = "wait", .clear = 0, .set = 1 << GOLB_WAIT }
+} ;
+
+static gol_arg const rgola[GOLA_N] =
+{
+ { .so = 't', .lo = "timeout", .i = GOLA_TIMEOUT }
+} ;
+
+static int service_startstop (char const *const *argv, int h)
+{
+ char const *wgola[GOLA_N] = { 0 } ;
+ uint64_t wgolb = 0 ;
+ unsigned int t = 0 ;
+ size_t argc = env_len(argv) ;
+ pid_t pid ;
+ int wstat ;
+ uint32_t nlong, nshort, n ;
+ int livelock, compiledlock ;
+ size_t livelen = strlen(g->dirs.live) ;
+ PROG = h ? "s6 service start" : "s6 service stop" ;
+
+ argv += gol_argv(argv, rgolb, 3, rgola, GOLA_N, &wgolb, wgola) ;
+ if (!*argv) strerr_dieusage(100, h ? "s6 service start services..." : "s6 service stop services...") ;
+
+ if (wgola[GOLA_TIMEOUT])
+ {
+ if (!uint0_scan(wgola[GOLA_TIMEOUT], &t))
+ strerr_dief1x(100, "timeout must be an integer (milliseconds)") ;
+ }
+ s6f_lock(g->dirs.stmp, 0) ; /* leaks, it's fine */
+ char dbfn[livelen + 10] ;
+ memcpy(dbfn, g->dirs.live, livelen) ;
+ memcpy(dbfn + livelen, "/compiled", 10) ;
+ if (!s6rc_lock(g->dirs.live, 2, &livelock, dbfn, 1, &compiledlock, 1))
+ strerr_diefu2sys(111, "take lock in ", g->dirs.live) ;
+ if (!s6rc_live_state_size(g->dirs.live, &nlong, &nshort))
+ strerr_diefu2sys(111, "read state size in ", g->dirs.live) ;
+ n = nlong + nshort ;
+ unsigned char oldstate[n] ;
+ if (!s6rc_live_state_read(g->dirs.live, oldstate, n))
+ strerr_diefu2sys(111, "read state in ", g->dirs.live) ;
+
+ {
+ size_t m = 0 ;
+ char const *newargv[13 + argc] ;
+ char fmtv[UINT_FMT] ;
+ char fmtt[UINT_FMT] ;
+
+ newargv[m++] = S6RC_EXTBINPREFIX "s6-rc" ;
+ newargv[m++] = "-X" ; /* we already took the locks */
+ newargv[m++] = "-v" ;
+ newargv[m++] = fmtv ;
+ fmtv[uint_fmt(fmtv, g->verbosity)] = 0 ;
+ if (t)
+ {
+ newargv[m++] = "-t" ;
+ newargv[m++] = fmtt ;
+ fmtt[uint_fmt(fmtt, t)] = 0 ;
+ }
+ if (wgolb & 1 << GOLB_DRYRUN)
+ {
+ newargv[m++] = "-n" ;
+ newargv[m++] = "1" ;
+ }
+ newargv[m++] = "-l" ;
+ newargv[m++] = g->dirs.live ;
+ newargv[m++] = "--" ;
+ newargv[m++] = h ? "start" : "stop" ;
+ while (*argv) newargv[m++] = *argv++ ;
+ newargv[m++] = 0 ;
+ pid = cspawn(newargv[0], newargv, (char const *const *)environ, CSPAWN_FLAGS_SIGBLOCKNONE, 0, 0) ;
+ if (!pid) strerr_diefu2sys(111, "spawn ", newargv[0]) ;
+ }
+ if (wait_pid(pid, &wstat) <= 0) strerr_diefu1sys(111, "wait for s6-rc") ;
+ if ((WIFEXITED(wstat) && !WEXITSTATUS(wstat)) || !g->verbosity)
+ return wait_estatus(wstat) ;
+
+ unsigned char newstate[n] ;
+ if (!s6rc_live_state_read(g->dirs.live, newstate, n))
+ strerr_diefu2sys(111, "read state in ", g->dirs.live) ;
+ s6f_report_state_change(n, oldstate, newstate, dbfn, h) ;
+ return wait_estatus(wstat) ;
+}
+
+int service_start (char const *const *argv)
+{
+ return service_startstop(argv, 1) ;
+}
+
+int service_stop (char const *const *argv)
+{
+ return service_startstop(argv, 0) ;
+}