From 57deaa8de55ceb8ddf87fcd16f382b915b95f15c Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Sat, 15 Jul 2023 21:50:57 +1100
Subject: [PATCH] do not start twice earlier service

---
 src/lib66/exec/ssexec_init.c              | 11 ++++++++++-
 src/lib66/exec/ssexec_tree_signal.c       |  2 +-
 src/lib66/sanitize/sanitize_graph.c       |  2 +-
 src/lib66/service/service_graph_collect.c | 22 ++++++++++++++++++----
 src/lib66/svc/svc_compute_ns.c            |  4 ++++
 5 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/src/lib66/exec/ssexec_init.c b/src/lib66/exec/ssexec_init.c
index 4e19f9c5..48ea38c1 100644
--- a/src/lib66/exec/ssexec_init.c
+++ b/src/lib66/exec/ssexec_init.c
@@ -45,6 +45,9 @@ static void doit(stralloc *sa, ssexec_t *info, uint8_t earlier)
 
     FLAGS_SET(flag, STATE_FLAGS_TOPROPAGATE|STATE_FLAGS_WANTUP) ;
 
+    if (earlier)
+        FLAGS_SET(flag, STATE_FLAGS_ISEARLIER) ;
+
     /** build the graph of the entire system */
     graph_build_service(&graph, ares, &areslen, info, flag) ;
 
@@ -54,8 +57,14 @@ static void doit(stralloc *sa, ssexec_t *info, uint8_t earlier)
     FOREACH_SASTR(sa, n) {
 
         int aresid = service_resolve_array_search(ares, areslen, sa->s + n) ;
-        if (aresid < 0)
+        if (aresid < 0) {
+
+            if (earlier) {
+                log_warn("ignoring none earlier service: ", sa->s + n) ;
+                continue ;
+            }
             log_die(LOG_EXIT_USER, "service: ", sa->s + n, " not available -- did you parse it?") ;
+        }
 
         unsigned int l[graph.mlen], c = 0, pos = 0, idx = 0 ;
 
diff --git a/src/lib66/exec/ssexec_tree_signal.c b/src/lib66/exec/ssexec_tree_signal.c
index da2e0854..e798344d 100644
--- a/src/lib66/exec/ssexec_tree_signal.c
+++ b/src/lib66/exec/ssexec_tree_signal.c
@@ -466,7 +466,7 @@ static int ssexec_callback(stralloc *sa, ssexec_t *info, unsigned int what)
         if (!state_read(&ste, &res))
             log_dieu(LOG_EXIT_SYS, "read state file of: ", name, " -- please make a bug report") ;
 
-        if (service_is(&ste, !what ? STATE_FLAGS_ISENABLED : STATE_FLAGS_ISSUPERVISED) == STATE_FLAGS_TRUE) {
+        if (!what ? ste.isenabled : ste.issupervised == STATE_FLAGS_TRUE && ste.isearlier == STATE_FLAGS_FALSE) {
 
             if (get_rstrlen_until(name, SS_LOG_SUFFIX) < 0 && !res.inmodule)
                 if (!sastr_add_string(sa, name))
diff --git a/src/lib66/sanitize/sanitize_graph.c b/src/lib66/sanitize/sanitize_graph.c
index 65507328..a6491cc2 100644
--- a/src/lib66/sanitize/sanitize_graph.c
+++ b/src/lib66/sanitize/sanitize_graph.c
@@ -43,7 +43,7 @@ void sanitize_graph(ssexec_t *info)
     resolve_service_t ares[SS_MAX_SERVICE + 1] ;
     graph_t graph = GRAPH_ZERO ;
 
-    FLAGS_SET(flag, STATE_FLAGS_TOPROPAGATE|STATE_FLAGS_TOPARSE|STATE_FLAGS_WANTUP|STATE_FLAGS_WANTDOWN) ;
+    FLAGS_SET(flag, STATE_FLAGS_TOPROPAGATE|STATE_FLAGS_TOPARSE|STATE_FLAGS_WANTUP|STATE_FLAGS_WANTDOWN|STATE_FLAGS_ISEARLIER) ;
 
     /** build the graph of the entire system */
     graph_build_service(&graph, ares, &areslen, info, flag) ;
diff --git a/src/lib66/service/service_graph_collect.c b/src/lib66/service/service_graph_collect.c
index 97e67c5a..f32e2da6 100644
--- a/src/lib66/service/service_graph_collect.c
+++ b/src/lib66/service/service_graph_collect.c
@@ -86,12 +86,26 @@ void service_graph_collect(graph_t *g, char const *alist, size_t alen, resolve_s
             if (resolve_read_g(wres, info->base.s, name) <= 0)
                 log_dieu(LOG_EXIT_SYS, "read resolve file of: ", name, " -- please make a bug report") ;
 
-            if (FLAGS_ISSET(flag, STATE_FLAGS_ISSUPERVISED)) {
+            if (!state_read(&ste, &res))
+                log_dieu(LOG_EXIT_SYS, "read state file of: ", name, " -- please make a bug report") ;
+
+            if (FLAGS_ISSET(flag, STATE_FLAGS_ISEARLIER)) {
 
-                if (!state_read(&ste, &res))
-                    log_dieu(LOG_EXIT_SYS, "read state file of: ", name, " -- please make a bug report") ;
+                if (ste.isearlier == STATE_FLAGS_TRUE) {
+
+                    if (!service_resolve_copy(&cp, &res))
+                        log_dieu(LOG_EXIT_SYS, "copy resolve file of: ", name, " -- please make a bug report") ;
+
+                    ares[(*areslen)++] = cp ;
+                    continue ;
+                }
+                resolve_free(wres) ;
+                continue ;
+            }
+
+            if (FLAGS_ISSET(flag, STATE_FLAGS_ISSUPERVISED)) {
 
-                if (service_is(&ste, STATE_FLAGS_ISSUPERVISED) == STATE_FLAGS_TRUE) {
+                if (ste.issupervised == STATE_FLAGS_TRUE) {
 
                     if (!service_resolve_copy(&cp, &res))
                         log_dieu(LOG_EXIT_SYS, "copy resolve file of: ", name, " -- please make a bug report") ;
diff --git a/src/lib66/svc/svc_compute_ns.c b/src/lib66/svc/svc_compute_ns.c
index 7a73df4d..d6503236 100644
--- a/src/lib66/svc/svc_compute_ns.c
+++ b/src/lib66/svc/svc_compute_ns.c
@@ -167,6 +167,10 @@ int svc_compute_ns(resolve_service_t *res, uint8_t what, ssexec_t *info, char co
         if (aresid < 0)
             log_die(LOG_EXIT_USER, "service: ", name, " not available -- did you parse it?") ;
 
+        if (ares[aresid].earlier) {
+            log_warn("ignoring ealier service: ", ares[aresid].sa.s + ares[aresid].name) ;
+            continue ;
+        }
         graph_compute_visit(name, visit, list, &graph, &napid, requiredby) ;
 
     }
-- 
GitLab