From 2fc674dbf20e987bba08868e45c6165730f8aacf Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Sat, 3 Jun 2023 01:05:51 +1100
Subject: [PATCH] fix write of module environment file

---
 src/lib66/environ/env_resolve_conf.c  | 32 ++++++++++++++++++---------
 src/lib66/parse/parse_store_environ.c |  2 +-
 src/lib66/write/write_common.c        |  7 +++---
 3 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/src/lib66/environ/env_resolve_conf.c b/src/lib66/environ/env_resolve_conf.c
index 26317898..5e078288 100644
--- a/src/lib66/environ/env_resolve_conf.c
+++ b/src/lib66/environ/env_resolve_conf.c
@@ -22,20 +22,32 @@
 #include <66/environ.h>
 #include <66/constants.h>
 #include <66/utils.h>
+#include <66/service.h>
 
-int env_resolve_conf(stralloc *env,char const *svname, uid_t owner)
+int env_resolve_conf(stralloc *env, resolve_service_t *res)
 {
     log_flow() ;
 
-    if (!owner)
-    {
-        if (!stralloc_cats(env,SS_SERVICE_ADMCONFDIR)) return 0 ;
-    }
-    else
-    {
-        if (!set_ownerhome(env,owner)) return 0 ;
-        if (!stralloc_cats(env,SS_SERVICE_USERCONFDIR)) return 0 ;
+    if (!res->owner) {
+
+        if (!stralloc_cats(env, SS_SERVICE_ADMCONFDIR))
+            return 0 ;
+
+    } else {
+
+        if (!set_ownerhome(env, res->owner))
+            return 0 ;
+
+        if (!stralloc_cats(env, SS_SERVICE_USERCONFDIR))
+            return 0 ;
     }
-    if (!auto_stra(env,svname)) return 0 ;
+
+    if (res->inmodule)
+        if (!auto_stra(env, res->sa.s + res->inmodule, SS_SYM_VERSION, "/"))
+            return 0 ;
+
+    if (!auto_stra(env, res->sa.s + res->name))
+        return 0 ;
+
     return 1 ;
 }
diff --git a/src/lib66/parse/parse_store_environ.c b/src/lib66/parse/parse_store_environ.c
index 765e5aef..7804285c 100644
--- a/src/lib66/parse/parse_store_environ.c
+++ b/src/lib66/parse/parse_store_environ.c
@@ -49,7 +49,7 @@ int parse_store_environ(resolve_service_t *res, char *store, int idsec, int idke
             res->environ.env = resolve_add_string(wres, sa.s) ;
 
             sa.len = 0 ;
-            if (!env_resolve_conf(&sa, res->sa.s + res->name, MYUID))
+            if (!env_resolve_conf(&sa, res))
                 goto err ;
 
             res->environ.envdir = resolve_add_string(wres, sa.s) ;
diff --git a/src/lib66/write/write_common.c b/src/lib66/write/write_common.c
index ee2c8c3d..2672c1cb 100644
--- a/src/lib66/write/write_common.c
+++ b/src/lib66/write/write_common.c
@@ -50,7 +50,6 @@ int write_common(resolve_service_t *res, char const *dst)
             log_warnusys_return(LOG_EXIT_ZERO, "write uint file", SS_NOTIFICATION) ;
 
     /** timeout family
-     *
      * Only write timeout file for classic service.
      * S6-supervise need it otherwise it's read directly
      * from the resolve file at start process. */
@@ -72,8 +71,8 @@ int write_common(resolve_service_t *res, char const *dst)
         if (!write_uint(dst, "down-signal", res->execute.downsignal))
             log_warnusys_return(LOG_EXIT_ZERO, "write uint file down-signal") ;
 
-    /** environment for module is already written by the parse_module() function */
-    if (res->environ.env && res->type != TYPE_MODULE) {
+    /** environment */
+    if (res->environ.env) {
 
         stralloc dst = STRALLOC_ZERO ;
         stralloc contents = STRALLOC_ZERO ;
@@ -103,7 +102,6 @@ int write_common(resolve_service_t *res, char const *dst)
         if (!ob_dirname(basedir, src))
             log_warnusys_return(LOG_EXIT_ZERO, "get dirname of: ", src) ;
 
-
         if (!sastr_clean_string(&sa, res->sa.s + res->hiercopy))
             log_warnusys_return(LOG_EXIT_ZERO, "clean string") ;
 
@@ -182,6 +180,7 @@ int write_common(resolve_service_t *res, char const *dst)
             if (!hiercopy(tmp, dest))
                 log_warnusys_return(LOG_EXIT_ZERO, "copy: ", tmp, " to: ", dest) ;
         }
+
         stralloc_free(&sa) ;
     }
 
-- 
GitLab