From 9e69acd0ccd3442995a53743f02f20bde7a70765 Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Sat, 15 Apr 2023 19:44:01 +1100
Subject: [PATCH] respect timeout given by the frontend file. remove svc_send.c
 file

---
 src/lib66/exec/ssexec_signal.c |  2 +-
 src/lib66/svc/deps-lib/deps    |  1 -
 src/lib66/svc/svc_compute_ns.c |  4 +--
 src/lib66/svc/svc_launch.c     | 56 +++++++++++++++++++---------------
 src/lib66/svc/svc_send.c       | 40 ------------------------
 src/lib66/svc/svc_send_wait.c  | 14 ++++++++-
 6 files changed, 48 insertions(+), 69 deletions(-)
 delete mode 100644 src/lib66/svc/svc_send.c

diff --git a/src/lib66/exec/ssexec_signal.c b/src/lib66/exec/ssexec_signal.c
index 36f654d8..81d240d6 100644
--- a/src/lib66/exec/ssexec_signal.c
+++ b/src/lib66/exec/ssexec_signal.c
@@ -251,7 +251,7 @@ int ssexec_signal(int argc, char const *const *argv, ssexec_t *info)
 
     pidservice_init_array(list, napid, apids, &graph, ares, areslen, info, requiredby, gflag) ;
 
-    r = svc_launch(apids, napid, what, &graph, ares, info->timeout ? info->timeout : 0, info, updown, opt_updown, reloadmsg, data, propagate) ;
+    r = svc_launch(apids, napid, what, &graph, ares, info, updown, opt_updown, reloadmsg, data, propagate) ;
 
     graph_free_all(&graph) ;
 
diff --git a/src/lib66/svc/deps-lib/deps b/src/lib66/svc/deps-lib/deps
index 193b082a..fdb5bd2a 100644
--- a/src/lib66/svc/deps-lib/deps
+++ b/src/lib66/svc/deps-lib/deps
@@ -2,7 +2,6 @@ svc_compute_ns.o
 svc_launch.o
 svc_scandir_ok.o
 svc_scandir_send.o
-svc_send.o
 svc_send_fdholder.o
 svc_send_wait.o
 svc_unsupervise.o
diff --git a/src/lib66/svc/svc_compute_ns.c b/src/lib66/svc/svc_compute_ns.c
index 97442648..c429ce2f 100644
--- a/src/lib66/svc/svc_compute_ns.c
+++ b/src/lib66/svc/svc_compute_ns.c
@@ -116,7 +116,7 @@ static void pidservice_init_array(unsigned int *list, unsigned int listlen, pids
 
 }
 
-int svc_compute_ns(resolve_service_t *res, uint8_t what, uint8_t timeout, ssexec_t *info, char const *updown, uint8_t opt_updown, uint8_t reloadmsg,char const *data, uint8_t propagate)
+int svc_compute_ns(resolve_service_t *res, uint8_t what, ssexec_t *info, char const *updown, uint8_t opt_updown, uint8_t reloadmsg,char const *data, uint8_t propagate)
 {
     log_flow() ;
 
@@ -178,7 +178,7 @@ int svc_compute_ns(resolve_service_t *res, uint8_t what, uint8_t timeout, ssexec
 
     pidservice_init_array(list, napid, apids, &graph, ares, areslen, info, requiredby, gflag) ;
 
-    r = svc_launch(apids, napid, what, &graph, ares, info->timeout ? info->timeout : 0, info, updown, opt_updown, reloadmsg, data, propagate) ;
+    r = svc_launch(apids, napid, what, &graph, ares, info, updown, opt_updown, reloadmsg, data, propagate) ;
 
     graph_free_all(&graph) ;
 
diff --git a/src/lib66/svc/svc_launch.c b/src/lib66/svc/svc_launch.c
index 5f945923..3a402a81 100644
--- a/src/lib66/svc/svc_launch.c
+++ b/src/lib66/svc/svc_launch.c
@@ -417,20 +417,30 @@ static int handle_signal(pidservice_t *apids, unsigned int what, graph_t *graph,
     return ok ;
 }
 
-/** this following function come from:
- * https://git.skarnet.org/cgi-bin/cgit.cgi/s6-rc/tree/src/s6-rc/s6-rc.c#n111
- * under license ISC where parameters was modified */
-static uint32_t compute_timeout (uint32_t timeout, tain *deadline)
+unsigned int compute_timeout(unsigned int idx, unsigned int what)
 {
-  uint32_t t = timeout ;
-  int globalt ;
-  tain globaltto ;
-  tain_sub(&globaltto, deadline, &STAMP) ;
-  globalt = tain_to_millisecs(&globaltto) ;
-  if (!globalt) globalt = 1 ;
-  if (globalt > 0 && (!t || (unsigned int)globalt < t))
-    t = (uint32_t)globalt ;
-  return t ;
+    unsigned int timeout = 0 ;
+
+    if (!what) {
+
+        if (pares[idx].type == TYPE_ONESHOT && pares[idx].execute.timeout.up)
+            timeout = pares[idx].execute.timeout.up ;
+        else if (pares[idx].type == TYPE_CLASSIC && pares[idx].execute.timeout.kill)
+            timeout = pares[idx].execute.timeout.kill ;
+
+    } else {
+
+        if (pares[idx].type == TYPE_ONESHOT && pares[idx].execute.timeout.down)
+            timeout = pares[idx].execute.timeout.down ;
+        else if (pares[idx].type == TYPE_CLASSIC && pares[idx].execute.timeout.finish)
+            timeout = pares[idx].execute.timeout.finish ;
+    }
+
+    if (!timeout && PINFO->opt_timeout)
+        timeout = PINFO->timeout ;
+
+    return timeout ;
+
 }
 
 static int doit(pidservice_t *sv, unsigned int what, tain *deadline)
@@ -445,10 +455,8 @@ static int doit(pidservice_t *sv, unsigned int what, tain *deadline)
     char tfmt[UINT32_FMT] ;
 
     unsigned int timeout = 0 ;
-    if (!what)
-        timeout = compute_timeout(type == TYPE_ONESHOT ? pares[sv->aresid].execute.timeout.up : pares[sv->aresid].execute.timeout.kill, deadline) ;
-    else
-        timeout = compute_timeout(type == TYPE_ONESHOT ? pares[sv->aresid].execute.timeout.down : pares[sv->aresid].execute.timeout.finish, deadline) ;
+
+    timeout = compute_timeout(sv->aresid, what) ;
 
     tfmt[uint_fmt(tfmt, timeout)] = 0 ;
 
@@ -534,7 +542,7 @@ static int doit(pidservice_t *sv, unsigned int what, tain *deadline)
 
     } else if (type == TYPE_BUNDLE || type == TYPE_MODULE) {
 
-        return svc_compute_ns(&pares[sv->aresid], what, timeout, PINFO, updown, opt_updown, reloadmsg, data, PROPAGATE) ; ;
+        return svc_compute_ns(&pares[sv->aresid], what, PINFO, updown, opt_updown, reloadmsg, data, PROPAGATE) ; ;
     }
     // never happen, let compiler to be happy
     return 0 ;
@@ -580,7 +588,7 @@ static int async(pidservice_t *apids, unsigned int i, unsigned int what, ssexec_
     return e ;
 }
 
-int svc_launch(pidservice_t *apids, unsigned int len, uint8_t what, graph_t *graph, resolve_service_t *ares, uint8_t timeout, ssexec_t *info, char const *rise, uint8_t rise_opt, uint8_t msg, char const *signal, uint8_t propagate)
+int svc_launch(pidservice_t *apids, unsigned int len, uint8_t what, graph_t *graph, resolve_service_t *ares, ssexec_t *info, char const *rise, uint8_t rise_opt, uint8_t msg, char const *signal, uint8_t propagate)
 {
     log_flow() ;
 
@@ -601,14 +609,11 @@ int svc_launch(pidservice_t *apids, unsigned int len, uint8_t what, graph_t *gra
     auto_strings(data, signal) ;
     pares = ares ;
 
-    if (timeout)
-        tain_from_millisecs(&deadline, timeout) ;
+    if (info->opt_timeout)
+        tain_from_millisecs(&deadline, info->timeout) ;
     else
         deadline = tain_infinite_relative ;
 
-    tain_now_set_stopwatch_g() ;
-    tain_add_g(&deadline, &deadline) ;
-
     int spfd = selfpipe_init() ;
 
     if (spfd < 0)
@@ -632,6 +637,9 @@ int svc_launch(pidservice_t *apids, unsigned int len, uint8_t what, graph_t *gra
 
     }
 
+    tain_now_set_stopwatch_g() ;
+    tain_add_g(&deadline, &deadline) ;
+
     for (pos = 0 ; pos < napid ; pos++) {
 
         pid = fork() ;
diff --git a/src/lib66/svc/svc_send.c b/src/lib66/svc/svc_send.c
deleted file mode 100644
index 042e4e0d..00000000
--- a/src/lib66/svc/svc_send.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * svc_send.c
- *
- * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org>
- *
- * All rights reserved.
- *
- * This file is part of Obarun. It is subject to the license terms in
- * the LICENSE file found in the top-level directory of this
- * distribution.
- * This file may not be copied, modified, propagated, or distributed
- * except according to the terms contained in the LICENSE file./
- */
-
-#include <oblibs/log.h>
-
-#include <66/svc.h>
-#include <66/resolve.h>
-#include <66/ssexec.h>
-
-int svc_send(char const *const *list, unsigned int nservice, char **sig, unsigned int siglen, ssexec_t *info)
-{
-    log_flow() ;
-
-    int nargc = 2 + nservice + siglen ;
-    char const *newargv[nargc] ;
-    unsigned int m = 0 ;
-
-    newargv[m++] = "signal" ;
-    for (; *sig ; sig++)
-        newargv[m++] = *sig ;
-
-    for (; *list ; list++)
-        newargv[m++] = *list ;
-
-    newargv[m++] = 0 ;
-
-    return ssexec_signal(nargc, newargv, info) ;
-
-}
diff --git a/src/lib66/svc/svc_send_wait.c b/src/lib66/svc/svc_send_wait.c
index b6f06efb..5d3ff45a 100644
--- a/src/lib66/svc/svc_send_wait.c
+++ b/src/lib66/svc/svc_send_wait.c
@@ -28,15 +28,27 @@ int svc_send_wait(char const *const *list, unsigned int nservice, char **sig, un
     pid_t pid ;
     int wstat ;
 
-    int nargc = 5 + nservice + siglen + info->opt_color ;
+    int nargc = 5 + nservice + siglen + info->opt_color + (info->opt_timeout ? 2 : 0) ;
     char const *newargv[nargc] ;
     unsigned int m = 0 ;
     char verbo[UINT_FMT] ;
+    char fmt[UINT32_FMT] ;
+
     verbo[uint_fmt(verbo, VERBOSITY)] = 0 ;
 
+    if (info->opt_timeout)
+        fmt[uint32_fmt(fmt,info->timeout)] = 0 ;
+
     newargv[m++] = "66" ;
+
     if (info->opt_color)
         newargv[m++] = "-z" ;
+
+    if (info->timeout) {
+        newargv[m++] = "-T" ;
+        newargv[m++] = fmt ;
+    }
+
     newargv[m++] = "-v" ;
     newargv[m++] = verbo ;
     newargv[m++] = "signal" ;
-- 
GitLab