From 0ccf1492d572482958bd127d3fb9ae67c466f1e7 Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Sun, 21 May 2023 20:57:49 +1100
Subject: [PATCH] add TORELOAD flag definition

---
 src/lib66/sanitize/sanitize_init.c | 39 +++++++++++++++++-------------
 1 file changed, 22 insertions(+), 17 deletions(-)

diff --git a/src/lib66/sanitize/sanitize_init.c b/src/lib66/sanitize/sanitize_init.c
index eaf24ee6..4f29449e 100644
--- a/src/lib66/sanitize/sanitize_init.c
+++ b/src/lib66/sanitize/sanitize_init.c
@@ -12,8 +12,6 @@
  * except according to the terms contained in the LICENSE file./
  */
 
-#include <66/svc.h>
-
 #include <string.h>
 #include <stdlib.h>
 
@@ -24,6 +22,7 @@
 #include <oblibs/environ.h>
 
 #include <skalibs/types.h>
+#include <skalibs/genalloc.h>
 #include <skalibs/tai.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/unix-transactional.h>//atomic_symlink
@@ -40,17 +39,18 @@
 #include <66/enum.h>
 #include <66/sanitize.h>
 
-void sanitize_init(unsigned int *alist, unsigned int alen, graph_t *g, resolve_service_t *ares, unsigned int areslen, uint32_t flags)
+void sanitize_init(unsigned int *alist, unsigned int alen, graph_t *g, resolve_service_t *ares, unsigned int areslen)
 {
     log_flow() ;
 
     ftrigr_t fifo = FTRIGR_ZERO ;
-    uint32_t earlier = FLAGS_ISSET(flags, STATE_FLAGS_ISEARLIER) ;
+    uint32_t earlier ;
     gid_t gid = getgid() ;
     int is_supervised = 0, is_init ;
     unsigned int pos = 0, nsv = 0 ;
     unsigned int real[alen] ;
     unsigned int msg[areslen] ;
+    ss_state_t sta = STATE_ZERO ;
 
     memset(msg, 0, areslen) ;
 
@@ -61,24 +61,26 @@ void sanitize_init(unsigned int *alist, unsigned int alen, graph_t *g, resolve_s
     for (; pos < alen ; pos++) {
 
         char *name = g->data.s + genalloc_s(graph_hash_t,&g->hash)[alist[pos]].vertex ;
+
         int aresid = service_resolve_array_search(ares, areslen, name) ;
         if (aresid < 0)
             log_dieu(LOG_EXIT_SYS,"find ares id -- please make a bug reports") ;
 
+        if (!state_read(&sta, &ares[aresid]))
+            log_dieu(LOG_EXIT_SYS, "read state file of: ", name) ;
+
+        earlier = service_is(&sta, STATE_FLAGS_ISEARLIER) == STATE_FLAGS_TRUE ? 1 : 0 ;
         char *sa = ares[aresid].sa.s ;
         char *scandir = sa + ares[aresid].live.scandir ;
         size_t scandirlen = strlen(scandir) ;
 
         is_init = access(sa + ares[aresid].live.statedir, F_OK) ;
-        if (is_init < 0 || FLAGS_ISSET(flags, STATE_FLAGS_TOINIT))
-            sanitize_livestate(&ares[aresid], STATE_FLAGS_UNKNOWN) ;
+        if (is_init < 0 || service_is(&sta, STATE_FLAGS_TOINIT) == STATE_FLAGS_TRUE)
+            sanitize_livestate(&ares[aresid]) ;
 
         /**
-         * Bundle, module type are not a daemons. We don't need
-         * to supervise it.
-         * Special case for Oneshot, we only deal with
-         * the scandir symlink.
-         * */
+         * Bundle and module type are not a daemons. We don't need to supervise it.
+         * Special case for Oneshot, we only deal with the scandir symlink. */
         if (ares[aresid].type == TYPE_BUNDLE || ares[aresid].type == TYPE_MODULE)
             continue ;
 
@@ -92,7 +94,7 @@ void sanitize_init(unsigned int *alist, unsigned int alen, graph_t *g, resolve_s
 
         if (is_supervised == -1) {
 
-            sanitize_scandir(&ares[aresid], STATE_FLAGS_TOINIT) ;
+            sanitize_scandir(&ares[aresid]) ;
 
             if (ares[aresid].type == TYPE_ONESHOT)
                 continue ;
@@ -108,8 +110,7 @@ void sanitize_init(unsigned int *alist, unsigned int alen, graph_t *g, resolve_s
 
         if (!earlier && is_supervised) {
 
-            if (!FLAGS_ISSET(flags, STATE_FLAGS_TORELOAD) && !FLAGS_ISSET(flags, STATE_FLAGS_TORESTART))
-                sanitize_fdholder(&ares[aresid], STATE_FLAGS_TRUE) ;
+            sanitize_fdholder(&ares[aresid], STATE_FLAGS_TRUE) ;
 
             log_trace("create fifo: ", sa + ares[aresid].live.eventdir) ;
             if (!ftrigw_fifodir_make(sa + ares[aresid].live.eventdir, gid, 0))
@@ -147,12 +148,16 @@ void sanitize_init(unsigned int *alist, unsigned int alen, graph_t *g, resolve_s
 
                 if (!ids[nids++])
                     log_dieusys(LOG_EXIT_SYS, "subcribe to fifo: ", eventdir) ;
+
+                if (!state_messenger(&ares[real[pos]], STATE_FLAGS_TORELOAD, STATE_FLAGS_TRUE))
+                    log_dieusys(LOG_EXIT_SYS, "send message to state of: ", sa + ares[real[pos]].name) ;
+
             }
         }
 
         if (nids) {
 
-            sanitize_scandir(&ares[real[0]], STATE_FLAGS_TORELOAD) ;
+            sanitize_scandir(&ares[real[0]]) ;
 
             log_trace("waiting for events on fifo...") ;
             if (ftrigr_wait_and_g(&fifo, ids, nids, &deadline) < 0)
@@ -162,7 +167,7 @@ void sanitize_init(unsigned int *alist, unsigned int alen, graph_t *g, resolve_s
     }
 
     if (earlier)
-        sanitize_scandir(&ares[0], STATE_FLAGS_ISEARLIER) ;
+        sanitize_scandir(&ares[0]) ;
 
     /**
      * We pass through here even for Bundle, Module and Oneshot.
@@ -171,7 +176,6 @@ void sanitize_init(unsigned int *alist, unsigned int alen, graph_t *g, resolve_s
      * */
     for (pos = 0 ; pos < alen ; pos++) {
 
-
         char *name = g->data.s + genalloc_s(graph_hash_t,&g->hash)[alist[pos]].vertex ;
         int aresid = service_resolve_array_search(ares, areslen, name) ;
         if (aresid < 0)
@@ -181,6 +185,7 @@ void sanitize_init(unsigned int *alist, unsigned int alen, graph_t *g, resolve_s
 
         if (ares[aresid].type == TYPE_CLASSIC || ares[aresid].type == TYPE_ONESHOT) {
 
+            log_trace("clean event directory: ", sa + ares[aresid].live.eventdir) ;
             if (!ftrigw_clean(sa + ares[aresid].live.eventdir))
                 log_warnu("clean event directory: ", sa + ares[aresid].live.eventdir) ;
 
-- 
GitLab