From d010e16b1592b82a86b0716af166c97cceb37d36 Mon Sep 17 00:00:00 2001 From: Alexey Khromov Date: Wed, 15 May 2024 18:35:10 +0300 Subject: [PATCH] Environment patch --- source/bforce/bforce.c | 28 ++++++++++++++++++++++++++++ source/bforce/os_unix.c | 7 +++++++ source/include/bforce.h | 2 ++ 3 files changed, 37 insertions(+) diff --git a/source/bforce/bforce.c b/source/bforce/bforce.c index cc7a822..38c4d1c 100644 --- a/source/bforce/bforce.c +++ b/source/bforce/bforce.c @@ -54,6 +54,9 @@ const char *BFERR[] = { /* 27 */ "Unused entry" }; +/* Main environment on startup (copied) for external processes */ +char ** mainenv; + static void deinit_opts(s_bforce_opts *opts); /* @@ -253,9 +256,25 @@ int main(int argc, char *argv[], char *envp[]) int rc = 0; int ch = 0; opts.runmode = MODE_UNDEFINED; + int i = 0; + int argsz = 0; memset(&opts, '\0', sizeof(s_bforce_opts)); + // Copying environment + argsz = 4096; + //argsz = sysconf(_SC_ARG_MAX); + DEB((D_CONFIG, "Limits: %d\n",argsz)); + mainenv = malloc(sizeof(mainenv)*argsz); + mainenv[0] = NULL; + i=0; + while (envp[i]) { + mainenv[i] = malloc(strlen(envp[i])+2); + strcpy(mainenv[i], envp[i]); + mainenv[i++] = NULL; + DEB((D_FREE, "Program ENV: %s\n", mainenv[(i-1)])); + } + // parsing while( (ch=getopt(argc, argv, "hfI:p:n:l:a:u:oivC:S:dq")) != (int)-1 ) @@ -488,6 +507,15 @@ default: exit: + /* de-init sessenv */ + i=0; + while(mainenv[i]) { + DEB((D_FREE, "De-init session ENV: %s\n", mainenv[(i)])); + free(mainenv[i]); + } + free(mainenv); + + deinit_conf(); deinit_opts(&opts); diff --git a/source/bforce/os_unix.c b/source/bforce/os_unix.c index 1651756..b014d83 100644 --- a/source/bforce/os_unix.c +++ b/source/bforce/os_unix.c @@ -73,9 +73,16 @@ int exec_options_parse(char *str) void exec_options_init(s_exec_options *eopt) { + int i = 0; memset(eopt, '\0', sizeof(s_exec_options)); eopt->umask = EXEC_DEFAULT_UMASK; eopt->envp[0] = NULL; + while(mainenv[i]) { + log("EXEC: Added ENV variable: %s\n", mainenv[i]); + eopt->envp[i] = xmalloc(strlen(mainenv[i]) + 2); + sprintf(eopt->envp[i], mainenv[i++]); + eopt->envp[i] = NULL; + } } void exec_options_deinit(s_exec_options *eopt) diff --git a/source/include/bforce.h b/source/include/bforce.h index 283be5b..8fcfdcc 100644 --- a/source/include/bforce.h +++ b/source/include/bforce.h @@ -183,4 +183,6 @@ extern const char *BFERR[]; int daemon_run(const char *confname, const char *incname, bool quit); +extern char ** mainenv; + #endif