From e37666f948c74d3dd1aa1c3e3ea2a4d04abaede1 Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Sat, 15 Oct 2022 20:17:23 +1100
Subject: [PATCH] fix write process

---
 src/lib66/write/deps-lib/deps                |   1 -
 src/lib66/write/write_classic.c              |  10 +-
 src/lib66/write/write_common.c               |   4 -
 src/lib66/write/write_execute_scripts.c      |  69 +--------
 src/lib66/write/write_execute_scripts_user.c |   8 +-
 src/lib66/write/write_logger.c               | 140 +++----------------
 src/lib66/write/write_oneshot.c              |   8 +-
 src/lib66/write/write_service.c              |  31 +++-
 8 files changed, 61 insertions(+), 210 deletions(-)

diff --git a/src/lib66/write/deps-lib/deps b/src/lib66/write/deps-lib/deps
index bd387c82..d07d1f64 100644
--- a/src/lib66/write/deps-lib/deps
+++ b/src/lib66/write/deps-lib/deps
@@ -2,7 +2,6 @@ write_classic.o
 write_common.o
 write_environ.o
 write_execute_scripts.o
-write_execute_scripts_user.o
 write_logger.o
 write_oneshot.o
 write_service.o
diff --git a/src/lib66/write/write_classic.c b/src/lib66/write/write_classic.c
index e582c0a7..6b38afcd 100644
--- a/src/lib66/write/write_classic.c
+++ b/src/lib66/write/write_classic.c
@@ -30,23 +30,23 @@ void write_classic(resolve_service_t *res, char const *dst, uint8_t force)
     write_common(res, dst) ;
 
     /** run file */
-    write_execute_scripts(res, &res->execute.run, "run", dst) ;
+    write_execute_scripts("run", res->sa.s + res->execute.run.run, dst) ;
 
     /** finish file */
     if (res->execute.finish.run_user)
-        write_execute_scripts(res, &res->execute.finish, "finish", dst) ;
+        write_execute_scripts("finish", res->sa.s + res->execute.finish.run, dst) ;
 
     /** run.user file */
-    write_execute_scripts_user(res, &res->execute.run, "run.user", dst) ;
+    write_execute_scripts( "run.user", res->sa.s + res->execute.run.run_user, dst) ;
 
     /** finish.user file */
     if (res->execute.finish.run_user)
-        write_execute_scripts_user(res, &res->execute.finish, "finish.user", dst) ;
+        write_execute_scripts("finish.user", res->sa.s + res->execute.finish.run_user, dst) ;
 
     /** logger */
     if (res->logger.name) {
 
-        char destination[strlen(dst)] ;
+        char destination[strlen(dst) + 1] ;
 
         if (!ob_dirname(destination, dst))
             log_dieu(LOG_EXIT_SYS, "get dirname of: ", dst) ;
diff --git a/src/lib66/write/write_common.c b/src/lib66/write/write_common.c
index e0d35104..3c8c48b8 100644
--- a/src/lib66/write/write_common.c
+++ b/src/lib66/write/write_common.c
@@ -36,9 +36,6 @@ void write_common(resolve_service_t *res, char const *dst)
 {
     log_flow() ;
 
-    char *time = 0 ;
-    size_t dstlen = strlen(dst) ;
-
     /** down file */
     if (res->execute.down)
         if (!file_create_empty(dst, "down", 0644))
@@ -100,7 +97,6 @@ void write_common(resolve_service_t *res, char const *dst)
         FOREACH_SASTR(&sa, pos) {
 
             char *what = sa.s + pos ;
-            size_t whatlen = strlen(what) ;
             char tmp[SS_MAX_PATH_LEN + 1] ;
             char basedir[srclen + 1] ;
 
diff --git a/src/lib66/write/write_execute_scripts.c b/src/lib66/write/write_execute_scripts.c
index 4bab4205..d4bb5e8a 100644
--- a/src/lib66/write/write_execute_scripts.c
+++ b/src/lib66/write/write_execute_scripts.c
@@ -19,86 +19,25 @@
 #include <oblibs/string.h>
 #include <oblibs/files.h>
 
-#include <66/service.h>
-#include <66/config.h>
-#include <66/constants.h>
-#include <66/enum.h>
-
-#include <s6/config.h>
-
 #ifndef FAKELEN
 #define FAKELEN strlen(run)
 #endif
 
-void write_execute_scripts(resolve_service_t *res, resolve_service_addon_scripts_t *scripts, char const *file, char const *dst)
+void write_execute_scripts(char const *file, char const *contents, char const *dst)
 {
+
     log_flow() ;
 
     char write[strlen(dst) + 1 + strlen(file) + 1] ;
-    char *shebang = "#!" SS_EXECLINE_SHEBANGPREFIX "execlineb -P\n" ;
-    size_t shebanglen = strlen(shebang) ;
-    char run[shebanglen + strlen(res->sa.s + res->live.fdholderdir) + SS_FDHOLDER_PIPENAME_LEN + strlen(res->sa.s + res->name) + SS_LOG_SUFFIX_LEN + strlen(S6_BINPREFIX) + strlen(res->sa.s + scripts->runas) + strlen(res->sa.s + res->environ.envdir) + SS_SYM_VERSION_LEN + (SS_MAX_PATH*2) + SS_MAX_PATH + strlen(file) + 117 + 1] ;
 
     auto_strings(write, dst, "/", file) ;
 
-    /** shebang */
-    auto_strings(run, shebang) ;
-
-    if (res->logger.name && res->type != TYPE_ONESHOT)
-        auto_strings(run + FAKELEN, \
-                "fdmove 1 0\n", \
-                "s6-fdholder-retrieve ", \
-                res->sa.s + res->live.fdholderdir, "/s ", \
-                "\"" SS_FDHOLDER_PIPENAME "w-", \
-                res->sa.s + res->name, SS_LOG_SUFFIX "\"\n", \
-                "fdswap 0 1\n") ;
-
-
-    /** environ */
-    if (res->environ.env)
-        auto_strings(run + FAKELEN, res->sa.s + res->environ.envdir, SS_SYM_VERSION "\n") ;
-
-    /** log redirection for oneshot service */
-    if (res->logger.name && res->type == TYPE_ONESHOT) {
-
-        char verbo[UINT_FMT] ;
-        verbo[uint_fmt(verbo, VERBOSITY)] = 0 ;
-
-        size_t namelen = strlen(res->sa.s + res->name) ;
-        size_t syslen = res->owner ? strlen(res->sa.s + res->path.home) + 1 + strlen(SS_LOGGER_USERDIR) : strlen(SS_LOGGER_SYSDIR) ;
-        size_t dstlen = res->logger.destination ? strlen(res->sa.s + res->logger.destination) : strlen(SS_LOGGER_SYSDIR) ;
-
-        char dstlog[syslen + dstlen + namelen + 1] ;
-
-        if (!res->logger.destination) {
-
-            if (res->owner)
-
-                auto_strings(dstlog, res->sa.s + res->path.home, "/", SS_LOGGER_USERDIR, res->sa.s + res->name) ;
-
-            else
-
-                auto_strings(dstlog, SS_LOGGER_SYSDIR, res->sa.s + res->name) ;
-
-        } else {
-
-            auto_strings(dstlog, res->sa.s + res->logger.destination) ;
-        }
-
-        auto_strings(run + FAKELEN, "execl-toc -v", verbo, " -d ", dstlog, " -m 0755\n") ;
-        auto_strings(run + FAKELEN, "redirfd -a 1 ", dstlog, "/current\n") ;
-    }
-
-    /** runas */
-    if (!res->owner && scripts->runas)
-        auto_strings(run + FAKELEN, S6_BINPREFIX "s6-setuidgid ", res->sa.s + scripts->runas, "\n") ;
-
-    auto_strings(run + FAKELEN, "./", file, ".user") ;
+    char run[strlen(contents) + 1] ;
+    auto_strings(run, contents) ;
 
     if (!file_write_unsafe(dst, file, run, FAKELEN))
         log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/", file) ;
 
     if (chmod(write, 0755) < 0)
         log_dieusys(LOG_EXIT_SYS, "chmod", write) ;
-
 }
diff --git a/src/lib66/write/write_execute_scripts_user.c b/src/lib66/write/write_execute_scripts_user.c
index 65ee0fff..7ddd99a8 100644
--- a/src/lib66/write/write_execute_scripts_user.c
+++ b/src/lib66/write/write_execute_scripts_user.c
@@ -34,7 +34,7 @@ void write_execute_scripts_user(resolve_service_t *res, resolve_service_addon_sc
     char *shebang = scripts->shebang ? res->sa.s + scripts->shebang : SS_EXECLINE_SHEBANGPREFIX "execlineb -P\n" ;
     size_t shebanglen = strlen(shebang) ;
     size_t scriptlen = strlen(res->sa.s + scripts->run_user) ;
-    char run[2 + shebanglen + 15 + scriptlen + 1] ;
+    char run[shebanglen + scriptlen + 4 + 1] ;
     int build = !strcmp(res->sa.s + scripts->build, "custom") ? 1 : 0 ;
     char write[strlen(dst) + 1 + strlen(file) + 1] ;
 
@@ -45,11 +45,9 @@ void write_execute_scripts_user(resolve_service_t *res, resolve_service_addon_sc
     if (build && scripts->shebang)
         auto_strings(run + FAKELEN, res->sa.s + scripts->shebang, "\n") ;
     else
-        auto_strings(run + FAKELEN, \
-                    SS_EXECLINE_SHEBANGPREFIX "execlineb -P\n",
-                    "fdmove -c 2 1\n") ;
+        auto_strings(run + FAKELEN, shebang, "\n") ;
 
-    auto_strings(run + FAKELEN, res->sa.s + scripts->run_user) ;
+    auto_strings(run + FAKELEN, res->sa.s + scripts->run_user, "\n") ;
 
     if (!file_write_unsafe(dst, file, run, FAKELEN))
         log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/", file) ;
diff --git a/src/lib66/write/write_logger.c b/src/lib66/write/write_logger.c
index 46bcd69b..f30150e4 100644
--- a/src/lib66/write/write_logger.c
+++ b/src/lib66/write/write_logger.c
@@ -49,18 +49,10 @@ void write_logger(resolve_service_t *res, char const *destination, uint8_t force
     gid_t log_gid ;
     uint8_t owner = res->owner ;
 
-    int build = !strcmp(res->sa.s + res->logger.execute.run.build, "custom") ? 1 : 0 ;
-
-    char *pmax = 0 ;
-    char *pback = 0 ;
-    char max[UINT32_FMT] ;
-    char back[UINT32_FMT] ;
-    char *timestamp = 0 ;
-    int itimestamp = SS_LOGGER_TIMESTAMP ;
     char *logrunner = res->logger.execute.run.runas ? res->sa.s + res->logger.execute.run.runas : SS_LOGGER_RUNNER ;
 
-    char dst[strlen(destination) + 1 + strlen(res->sa.s + res->logger.name) + 1] ;
-    auto_strings(dst, destination, "/", res->sa.s + res->logger.name) ;
+    char dst[strlen(destination) + strlen(res->sa.s + res->logger.name) + 1] ;
+    auto_strings(dst, destination, res->sa.s + res->logger.name) ;
 
     r = scan_mode(dst, S_IFDIR) ;
     if (r && force) {
@@ -83,133 +75,37 @@ void write_logger(resolve_service_t *res, char const *destination, uint8_t force
     if (res->logger.execute.timeout.finish)
         write_uint(dst, "timeout-finish", res->logger.execute.timeout.finish) ;
 
-    /** timestamp */
-    if (res->logger.timestamp != 3)
-        timestamp = res->logger.timestamp == TIME_NONE ? "" : res->logger.timestamp == TIME_ISO ? "T" : "t" ;
-    else
-        timestamp = itimestamp == TIME_NONE ? "" : itimestamp == TIME_ISO ? "T" : "t" ;
-
-    /** backup */
-    if (res->logger.backup) {
-
-        back[uint32_fmt(back,res->logger.backup)] = 0 ;
-        pback = back ;
-
-    } else
-        pback = "3" ;
-
-    /** file size */
-    if (res->logger.maxsize) {
-
-        max[uint32_fmt(max,res->logger.maxsize)] = 0 ;
-        pmax = max ;
-
-    } else
-        pmax = "1000000" ;
+    /** notification */
+    write_uint(dst, "notification-fd", 3) ;
 
     /** log destination */
-    size_t namelen = strlen(res->sa.s + res->name) ;
-    size_t syslen = res->owner ? strlen(res->sa.s + res->path.home) + 1 + strlen(SS_LOGGER_USERDIR) : strlen(SS_LOGGER_SYSDIR) ;
-    size_t dstlen = res->logger.destination ? strlen(res->sa.s + res->logger.destination) : strlen(SS_LOGGER_SYSDIR) ;
-
-    char dstlog[syslen + dstlen + namelen + 1] ;
-
-    if (!res->logger.destination) {
-
-        if (res->owner)
-
-            auto_strings(dstlog, res->sa.s + res->path.home, "/", SS_LOGGER_USERDIR, res->sa.s + res->name) ;
-
-        else
-
-            auto_strings(dstlog, SS_LOGGER_SYSDIR, res->sa.s + res->name) ;
-
-    } else {
-
-        auto_strings(dstlog, res->sa.s + res->logger.destination) ;
-    }
-
-    if (!dir_create_parent(dstlog, 0755))
-        log_dieusys(LOG_EXIT_SYS, "create directory: ", dstlog) ;
+    if (!dir_create_parent(res->sa.s + res->logger.destination, 0755))
+        log_dieusys(LOG_EXIT_SYS, "create directory: ", res->sa.s + res->logger.destination) ;
 
     if (!owner && ((res->logger.execute.run.build == BUILD_AUTO) || (!res->logger.execute.run.build))) {
 
         if (!youruid(&log_uid, logrunner) || !yourgid(&log_gid, log_uid))
             log_dieusys(LOG_EXIT_SYS, "get uid and gid of: ", logrunner) ;
 
-        if (chown(dstlog, log_uid, log_gid) == -1)
-            log_dieusys(LOG_EXIT_SYS, "chown: ", dstlog) ;
+        if (chown(res->sa.s + res->logger.destination, log_uid, log_gid) == -1)
+            log_dieusys(LOG_EXIT_SYS, "chown: ", res->sa.s + res->logger.destination) ;
     }
 
-    {
-        /** dst/run file */
-        char *shebang = res->logger.execute.run.shebang ? res->sa.s + res->logger.execute.run.shebang : "#!" SS_EXECLINE_SHEBANGPREFIX "execlineb -P\n" ;
-
-        char run[strlen(shebang) + strlen(res->sa.s + res->live.fdholderdir) + SS_FDHOLDER_PIPENAME_LEN +  strlen(res->sa.s + res->logger.name) + strlen(S6_BINPREFIX) + strlen(res->sa.s + res->logger.execute.run.runas) + 53 + 1] ;
-
-        auto_strings(run, \
-                    shebang, \
-                    "s6-fdholder-retrieve ", \
-                    res->sa.s + res->live.fdholderdir, "/s ", \
-                    "\"" SS_FDHOLDER_PIPENAME "r-", \
-                    res->sa.s + res->logger.name, "\"\n") ;
-
-        /** runas */
-        if (!res->owner && res->logger.execute.run.runas)
-            auto_strings(run + FAKELEN, S6_BINPREFIX "s6-setuidgid ", res->sa.s + res->logger.execute.run.runas, "\n") ;
-
-        auto_strings(run + FAKELEN, "./run.user\n") ;
-
-        if (!file_write_unsafe(dst, "run", run, FAKELEN))
-            log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ;
-
-        char write[strlen(dst) + 5] ;
-        auto_strings(write, dst, "/run") ;
-
-        if (chmod(write, 0755) < 0)
-            log_dieusys(LOG_EXIT_SYS, "chmod", write) ;
-    }
-
-    if (!build) {
-
-        char *shebang = "#!" SS_EXECLINE_SHEBANGPREFIX "execlineb -P\n" ;
-        size_t shebanglen = strlen(shebang) ;
-
-        char run[shebanglen + strlen(S6_BINPREFIX) + strlen(logrunner) + strlen(pback) + strlen(timestamp) + strlen(pmax) + strlen(dstlog) + 45 + 1] ;
-
-        auto_strings(run, \
-                    shebang, \
-                    "fdmove -c 2 1\n") ;
-
-        if (!owner)
-            auto_strings(run + FAKELEN, S6_BINPREFIX "s6-setuidgid ", logrunner, "\n") ;
-
-        auto_strings(run + FAKELEN, "s6-log ") ;
-
-        if (SS_LOGGER_NOTIFY)
-            auto_strings(run + FAKELEN, "-d3 ") ;
-
-        auto_strings(run + FAKELEN, "n", pback, " ") ;
-
-        if (res->logger.timestamp < TIME_NONE)
-            auto_strings(run + FAKELEN, timestamp, " ") ;
-
-        auto_strings(run + FAKELEN, "s", pmax, " ", dstlog, "\n") ;
+    char write[strlen(dst) + 10] ;
 
-        if (!file_write_unsafe(dst, "run.user", run, FAKELEN))
-            log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ;
+    /** run script */
+    if (!file_write_unsafe(dst, "run", res->sa.s + res->logger.execute.run.run, strlen(res->sa.s + res->logger.execute.run.run)))
+        log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ;
 
-        /** notification fd */
-        if (SS_LOGGER_NOTIFY)
-            write_uint(dst, SS_NOTIFICATION, 3) ;
+    auto_strings(write, dst, "/run") ;
 
-    } else {
+    if (chmod(write, 0755) < 0)
+        log_dieusys(LOG_EXIT_SYS, "chmod", write) ;
 
-        if (!file_write_unsafe(dst, "run.user", res->sa.s + res->logger.execute.run.run_user, strlen(res->sa.s + res->logger.execute.run.run_user)))
-            log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ;
-    }
+    /** run.user script */
+    if (!file_write_unsafe(dst, "run.user", res->sa.s + res->logger.execute.run.run_user, strlen(res->sa.s + res->logger.execute.run.run_user)))
+        log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ;
 
-    char write[strlen(dst) + 10] ;
     auto_strings(write, dst, "/run.user") ;
 
     if (chmod(write, 0755) < 0)
diff --git a/src/lib66/write/write_oneshot.c b/src/lib66/write/write_oneshot.c
index 673f975e..071f50ad 100644
--- a/src/lib66/write/write_oneshot.c
+++ b/src/lib66/write/write_oneshot.c
@@ -25,17 +25,17 @@ void write_oneshot(resolve_service_t *res, char const *dst)
     write_common(res, dst) ;
 
     /** run file */
-    write_execute_scripts(res, &res->execute.run, "up", dst) ;
+    write_execute_scripts("up", res->sa.s + res->execute.run.run, dst) ;
 
     /** finish file */
     if (res->execute.finish.run_user)
-        write_execute_scripts(res, &res->execute.finish, "down", dst) ;
+        write_execute_scripts("down", res->sa.s + res->execute.finish.run, dst) ;
 
     /** run.user file */
-    write_execute_scripts_user(res, &res->execute.run, "up.user", dst) ;
+    write_execute_scripts("up.user", res->sa.s + res->execute.run.run_user, dst) ;
 
     /** finish.user file */
     if (res->execute.finish.run_user)
-        write_execute_scripts_user(res, &res->execute.finish, "down.user", dst) ;
+        write_execute_scripts("down.user", res->sa.s + res->execute.finish.run_user, dst) ;
 
 }
diff --git a/src/lib66/write/write_service.c b/src/lib66/write/write_service.c
index d9d4c8da..1de04001 100644
--- a/src/lib66/write/write_service.c
+++ b/src/lib66/write/write_service.c
@@ -47,14 +47,33 @@ int write_services(resolve_service_t *res, char const *workdir, uint8_t force)
     {
         resolve_service_t fres = RESOLVE_SERVICE_ZERO ;
         resolve_wrapper_t_ref wres = resolve_set_struct(DATA_SERVICE, &fres) ;
+        ss_state_t ste = STATE_ZERO ;
+        if (resolve_check(workdir, name)) {
+            if (!resolve_read(wres, workdir, name))
+                log_dieu(LOG_EXIT_SYS, "read resolve file of: ", name) ;
 
-        if (resolve_read_g(wres, workdir, name))
-            if (fres.type != type && fres.live.disen)
-                log_die(LOG_EXIT_SYS, "Detection of incompatible type format for: ", name, " -- current: ", get_key_by_enum(ENUM_TYPE, type), " previous: ", get_key_by_enum(ENUM_TYPE, fres.type)) ;
+            if (state_check(fres.sa.s + fres.path.home, name)) {
 
+                if (!state_read(&ste, fres.sa.s + fres.path.home, name))
+                    log_dieu(LOG_EXIT_SYS, "read state file of: ", name) ;
+
+                if (fres.type != type && FLAGS_ISSET(ste.isenabled, STATE_FLAGS_TRUE))
+                    log_die(LOG_EXIT_SYS, "Detection of incompatible type format for: ", name, " -- current: ", get_key_by_enum(ENUM_TYPE, type), " previous: ", get_key_by_enum(ENUM_TYPE, fres.type)) ;
+            }
+        }
         resolve_free(wres) ;
     }
-
+    /**
+     *
+     *
+     * please pass through a temporary or backup
+     *
+     * just need to switch the /run/66/state/0/<service> symlink
+     * with atomic_symlink
+     *
+     *
+     *
+     * */
     char wname[workdirlen + SS_SVC_LEN + 1 + namelen + 1] ;
     auto_strings(wname, workdir, SS_SVC, "/", name) ;
 
@@ -80,6 +99,10 @@ int write_services(resolve_service_t *res, char const *workdir, uint8_t force)
 
     switch(type) {
 
+        case TYPE_MODULE:
+        case TYPE_BUNDLE:
+            break ;
+
         case TYPE_CLASSIC:
 
             write_classic(res, wname, force) ;
-- 
GitLab