From 1f4198454a3fd35933c230e08d7f0f4f5fc45a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Hern=C3=A1n=20Carle?= Date: Tue, 21 Mar 2023 16:08:05 +0100 Subject: [PATCH 1/6] fixes and comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add comment for get_sharedenv function - get-launch-components now gets the environment variables - start-component trims values of environment variables. Signed-off-by: Pablo Hernán Carle --- src/main.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main.c b/src/main.c index 51f544d..11cab20 100644 --- a/src/main.c +++ b/src/main.c @@ -383,7 +383,7 @@ static void set_shared_uss_env(ConfigManager *configmgr) { } char *entry = malloc(strlen(key) + strlen(value) + 2); - sprintf(entry, "%s=%s", key, value); + sprintf(entry, "%s=%s", key, trimRight(value)); shared_uss_env[idx++] = entry; } } @@ -1227,10 +1227,23 @@ static void handle_get_component_line(void *data, const char *line) { } } +static char* get_get_launch_components_cmd(char* sharedenv) { + const char basecmd[] = "%s %s/bin/zwe internal get-launch-components --config \"%s\" --ha-instance %s"; + int size = strlen(zl_context.root_dir) + strlen(zl_context.config_path) + strlen(zl_context.ha_instance_id) + strlen(sharedenv) + sizeof(basecmd) + 1; + char *command = malloc(size); + + snprintf(command, size, basecmd, + sharedenv, zl_context.root_dir, zl_context.config_path, zl_context.ha_instance_id); + + return command; +} + static int get_component_list(char *buf, size_t buf_size) { - char command[4*PATH_MAX]; - snprintf (command, sizeof(command), "%s/bin/zwe internal get-launch-components --config \"%s\" --ha-instance %s", - zl_context.root_dir, zl_context.config_path, zl_context.ha_instance_id); + char *sharedenv = get_sharedenv(); + char *command = get_get_launch_components_cmd(sharedenv); + + free(sharedenv); + DEBUG("about to get component list\n"); char comp_list[COMP_LIST_SIZE] = {0}; if (run_command(command, handle_get_component_line, (void*)comp_list)) { @@ -1331,6 +1344,11 @@ static void print_line(void *data, const char *line) { printf("%s", line); } +/** + * @brief Get the sharedenv. The function contemplates enclosing in quotes the values of the variables. + * + * @return char* string representation of the shared_uss_env variable, e.g. VAR1="sample" VAR2=12345 + */ static char* get_sharedenv(void) { char *output = NULL; char *aux = NULL; @@ -1381,7 +1399,6 @@ static char* get_start_prepare_cmd(char *sharedenv) { return command; } - static int prepare_instance() { char *sharedenv = get_sharedenv(); char *command = get_start_prepare_cmd(sharedenv); From c8152665da2ad424052e0fa371b583a9d7e0d80a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Hern=C3=A1n=20Carle?= Date: Wed, 22 Mar 2023 10:44:06 +0100 Subject: [PATCH 2/6] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pablo Hernán Carle --- src/main.c | 114 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 46 deletions(-) diff --git a/src/main.c b/src/main.c index 11cab20..1de53cc 100644 --- a/src/main.c +++ b/src/main.c @@ -343,7 +343,7 @@ static void set_shared_uss_env(ConfigManager *configmgr) { object = jsonAsObject(env); } - int maxRecords = 2; + int maxRecords = 3; for (char **env = environ; *env != 0; env++) { maxRecords++; @@ -383,7 +383,20 @@ static void set_shared_uss_env(ConfigManager *configmgr) { } char *entry = malloc(strlen(key) + strlen(value) + 2); - sprintf(entry, "%s=%s", key, trimRight(value)); + + // REMOVE? + if (value[0] == '"') { + value[strlen(value) - 1] = 0; + value = value + 1; + } + // REMOVE? + + // REMOVE? + trimRight(value, strlen(value)); + printf("setting \"%s\" (%lu) with value \"%s\" (%lu)\n", key, strlen(key), value, strlen(value)); + // REMOVE? + + sprintf(entry, "%s=%s", key, value); shared_uss_env[idx++] = entry; } } @@ -406,6 +419,7 @@ static void set_shared_uss_env(ConfigManager *configmgr) { shared_uss_env[idx++] = thisEnv; } } + shared_uss_env[idx] = NULL; arrayListFree(list); } @@ -763,6 +777,14 @@ static int start_component(zl_comp_t *comp) { }; shared_uss_env[0] = (char *)get_shareas_env(comp); + + // REMOVE + for (char **env = shared_uss_env; *env != 0; env++) { + char *thisEnv = *env; + printf("Entry in shared_uss_env is '%s' with length %lu\n", thisEnv, strlen(thisEnv)); + } + // REMOVE + comp->pid = spawn(bin, fd_count, fd_map, &inherit, c_args, (const char **)shared_uss_env); if (comp->pid == -1) { DEBUG("spawn() failed for %s - %s\n", comp->name, strerror(errno)); @@ -1238,6 +1260,50 @@ static char* get_get_launch_components_cmd(char* sharedenv) { return command; } +/** + * @brief Get the sharedenv. The function contemplates enclosing in quotes the values of the variables. + * + * @return char* string representation of the shared_uss_env variable, e.g. VAR1="sample" VAR2=12345 + */ +static char* get_sharedenv(void) { + char *output = NULL; + char *aux = NULL; + + int required = 0; + for (char **env = shared_uss_env + 1; *env != 0; env++) { // First element is NULL, reserved to _BPX_SHAREAS + char *thisEnv = *env; + required += (strlen(thisEnv) + 3); // space + quotes + } + + required++; + output = malloc(required); + aux = malloc(required); + for (char **env = shared_uss_env + 1; *env != 0; env++) { // First element is NULL, reserved to _BPX_SHAREAS + char *thisEnv = *env; + strcat(aux, thisEnv); + char *envName = strtok(aux, "="); + if (envName) { + strcat(output, envName); + char *envValue = &thisEnv[strlen(envName) + 1]; + if (*envValue == '"') { // Env value is already enclosed in quotes + strcat(output, "="); + strcat(output, envValue); + trimRight(output, strlen(output)); + strcat(output, " "); + } else { + strcat(output, "=\""); + strcat(output, envValue); + trimRight(output, strlen(output)); + strcat(output, "\" "); + } + } + aux[0] = 0; + } + trimRight(output, strlen(output)); + free(aux); + return output; +} + static int get_component_list(char *buf, size_t buf_size) { char *sharedenv = get_sharedenv(); char *command = get_get_launch_components_cmd(sharedenv); @@ -1344,50 +1410,6 @@ static void print_line(void *data, const char *line) { printf("%s", line); } -/** - * @brief Get the sharedenv. The function contemplates enclosing in quotes the values of the variables. - * - * @return char* string representation of the shared_uss_env variable, e.g. VAR1="sample" VAR2=12345 - */ -static char* get_sharedenv(void) { - char *output = NULL; - char *aux = NULL; - - int required = 0; - for (char **env = shared_uss_env + 1; *env != 0; env++) { // First element is NULL, reserved to _BPX_SHAREAS - char *thisEnv = *env; - required += (strlen(thisEnv) + 3); // space + quotes - } - - required++; - output = malloc(required); - aux = malloc(required); - for (char **env = shared_uss_env + 1; *env != 0; env++) { // First element is NULL, reserved to _BPX_SHAREAS - char *thisEnv = *env; - strcat(aux, thisEnv); - char *envName = strtok(aux, "="); - if (envName) { - strcat(output, envName); - char *envValue = &thisEnv[strlen(envName) + 1]; - if (*envValue == '"') { // Env value is already enclosed in quotes - strcat(output, "="); - strcat(output, envValue); - trimRight(output, strlen(output)); - strcat(output, " "); - } else { - strcat(output, "=\""); - strcat(output, envValue); - trimRight(output, strlen(output)); - strcat(output, "\" "); - } - } - aux[0] = 0; - } - trimRight(output, strlen(output)); - free(aux); - return output; -} - static char* get_start_prepare_cmd(char *sharedenv) { const char basecmd[] = "%s %s/bin/zwe internal start prepare --config \"%s\" --ha-instance %s 2>&1"; int size = strlen(zl_context.root_dir) + strlen(zl_context.config_path) + strlen(zl_context.ha_instance_id) + strlen(sharedenv) + sizeof(basecmd) + 1; From ad98b27f4790549324060e33b5b69a06a77c8dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Hern=C3=A1n=20Carle?= Date: Thu, 23 Mar 2023 17:10:25 +0100 Subject: [PATCH 3/6] wip working (with _CEE_) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pablo Hernán Carle --- src/main.c | 68 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/src/main.c b/src/main.c index 1de53cc..4bfa236 100644 --- a/src/main.c +++ b/src/main.c @@ -54,6 +54,8 @@ extern char ** environ; #define COMP_ID "ZWELNCH" +#define CEE_ENVFILE_PREFIX "_CEE_ENVFILE" + #define MIN_UPTIME_SECS 90 #define SHUTDOWN_GRACEFUL_PERIOD (20 * 1000) @@ -372,6 +374,11 @@ static void set_shared_uss_env(ConfigManager *configmgr) { continue; } + if (strncmp(key, CEE_ENVFILE_PREFIX, strlen(CEE_ENVFILE_PREFIX)) == 0) { + DEBUG("Ignoring environment variable: %s, conflict\n", key); + continue; + } + if (!arrayListContains(list, key)) { arrayListAdd(list, key); @@ -384,18 +391,6 @@ static void set_shared_uss_env(ConfigManager *configmgr) { char *entry = malloc(strlen(key) + strlen(value) + 2); - // REMOVE? - if (value[0] == '"') { - value[strlen(value) - 1] = 0; - value = value + 1; - } - // REMOVE? - - // REMOVE? - trimRight(value, strlen(value)); - printf("setting \"%s\" (%lu) with value \"%s\" (%lu)\n", key, strlen(key), value, strlen(value)); - // REMOVE? - sprintf(entry, "%s=%s", key, value); shared_uss_env[idx++] = entry; } @@ -409,6 +404,10 @@ static void set_shared_uss_env(ConfigManager *configmgr) { if (!index) { continue; } + if (strncmp(thisEnv, CEE_ENVFILE_PREFIX, strlen(CEE_ENVFILE_PREFIX)) == 0) { + DEBUG("Ignoring environment variable: %s, conflict\n", thisEnv); + continue; + } int length = index - thisEnv; char *key = malloc(length + 1); @@ -778,14 +777,49 @@ static int start_component(zl_comp_t *comp) { shared_uss_env[0] = (char *)get_shareas_env(comp); - // REMOVE - for (char **env = shared_uss_env; *env != 0; env++) { + int env_count = 8; + + const char *c_env[10]; + + int i = 0; + char *aux = NULL; + char *output = NULL; + for (char **env = shared_uss_env; *env != 0 && i < env_count; env++) { char *thisEnv = *env; - printf("Entry in shared_uss_env is '%s' with length %lu\n", thisEnv, strlen(thisEnv)); + aux = malloc(strlen(thisEnv) + 1); + output = malloc(strlen(thisEnv) + 1); + strncpy(aux, thisEnv, strlen(thisEnv)); + //printf("aux: %s\n", aux); + char *envName = strtok(aux, "="); + //printf("envName: %s\n", envName); + if (envName) { + if (strncmp(envName, "_CEE", 4) == 0) { + continue; + } + strcat(output, envName); + char *envValue = &aux[strlen(envName) + 1]; + + setenv(envName, envValue, 0); + + strcat(output, "="); + strcat(output, envValue); + output[strlen(thisEnv)] = 0; + trimRight(output, strlen(output)); + printf("element %d is '%s'\n", i, output); + } + aux[0] = 0; + c_env[i] = output; + i++; + } + c_env[i] = NULL; + + for (int j = 0; j < i; j++) { + const char *str = c_env[j]; + int str_length = strlen(str); + printf("last characters of '%s': 0x%08X, 0x%08X, 0x%08X, 0x%08X\n", str, str[str_length - 3], str[str_length - 2], str[str_length - 1], str[str_length]); } - // REMOVE - comp->pid = spawn(bin, fd_count, fd_map, &inherit, c_args, (const char **)shared_uss_env); + comp->pid = spawn(bin, fd_count, fd_map, &inherit, c_args, c_env); if (comp->pid == -1) { DEBUG("spawn() failed for %s - %s\n", comp->name, strerror(errno)); return -1; From 679c5944df9fc6c58296a48963057f69c5a8f52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Hern=C3=A1n=20Carle?= Date: Fri, 24 Mar 2023 10:21:19 +0100 Subject: [PATCH 4/6] fix environment for components MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pablo Hernán Carle --- src/main.c | 77 +++++++++++++++++++++++------------------------------- 1 file changed, 33 insertions(+), 44 deletions(-) diff --git a/src/main.c b/src/main.c index 4bfa236..0b6a5a8 100644 --- a/src/main.c +++ b/src/main.c @@ -718,6 +718,37 @@ static void *handle_comp_comm(void *args) { return NULL; } +/** + * @brief Copy environment variables + _BPX_SHAREAS for the specified component + * + * @param comp The component + * @return const char** environment strings list + */ +static const char **env_comp(zl_comp_t *comp) { + shared_uss_env[0] = (char *)get_shareas_env(comp); + + int env_records = 0; + for (char **env = shared_uss_env; *env != 0; env++) { + env_records++; + } + + const char **env_comp = malloc(env_records + 1); + + int i = 0; + char *aux = NULL; + for (char **env = shared_uss_env; *env != 0 && i < env_records; env++) { + char *thisEnv = *env; + aux = malloc(strlen(thisEnv) + 1); + strncpy(aux, thisEnv, strlen(thisEnv)); + aux[strlen(thisEnv)] = 0; + trimRight(aux, strlen(aux)); + env_comp[i] = aux; + i++; + } + env_comp[i] = NULL; + return env_comp; +} + static int start_component(zl_comp_t *comp) { if (comp->pid != -1) { @@ -775,51 +806,9 @@ static int start_component(zl_comp_t *comp) { NULL }; - shared_uss_env[0] = (char *)get_shareas_env(comp); - - int env_count = 8; - - const char *c_env[10]; - - int i = 0; - char *aux = NULL; - char *output = NULL; - for (char **env = shared_uss_env; *env != 0 && i < env_count; env++) { - char *thisEnv = *env; - aux = malloc(strlen(thisEnv) + 1); - output = malloc(strlen(thisEnv) + 1); - strncpy(aux, thisEnv, strlen(thisEnv)); - //printf("aux: %s\n", aux); - char *envName = strtok(aux, "="); - //printf("envName: %s\n", envName); - if (envName) { - if (strncmp(envName, "_CEE", 4) == 0) { - continue; - } - strcat(output, envName); - char *envValue = &aux[strlen(envName) + 1]; - - setenv(envName, envValue, 0); - - strcat(output, "="); - strcat(output, envValue); - output[strlen(thisEnv)] = 0; - trimRight(output, strlen(output)); - printf("element %d is '%s'\n", i, output); - } - aux[0] = 0; - c_env[i] = output; - i++; - } - c_env[i] = NULL; - - for (int j = 0; j < i; j++) { - const char *str = c_env[j]; - int str_length = strlen(str); - printf("last characters of '%s': 0x%08X, 0x%08X, 0x%08X, 0x%08X\n", str, str[str_length - 3], str[str_length - 2], str[str_length - 1], str[str_length]); - } + const char **c_envp = env_comp(comp); - comp->pid = spawn(bin, fd_count, fd_map, &inherit, c_args, c_env); + comp->pid = spawn(bin, fd_count, fd_map, &inherit, c_args, c_envp); if (comp->pid == -1) { DEBUG("spawn() failed for %s - %s\n", comp->name, strerror(errno)); return -1; From d9bbcc2d4f78cf805e215b078a01766be95f1c34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Jare=C5=A1?= Date: Wed, 15 Mar 2023 11:24:17 +0100 Subject: [PATCH 5/6] check if log context was created MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pablo Hernán Carle --- src/main.c | 4 ++++ src/msg.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/main.c b/src/main.c index 0b6a5a8..cf21ce6 100644 --- a/src/main.c +++ b/src/main.c @@ -1535,6 +1535,10 @@ int main(int argc, char **argv) { zl_context.config = config; LoggingContext *logContext = makeLoggingContext(); + if (!logContext) { + ERROR(MSG_NO_LOG_CONTEXT); + exit(EXIT_FAILURE); + } logConfigureStandardDestinations(logContext); ConfigManager *configmgr = makeConfigManager(); /* configs,schemas,1,stderr); */ diff --git a/src/msg.h b/src/msg.h index aabd9cf..db2f15d 100644 --- a/src/msg.h +++ b/src/msg.h @@ -87,6 +87,7 @@ #define MSG_CFG_INTERNAL_FAIL MSG_PREFIX "0071E" " Internal failure during validation, please contact support\n" #define MSG_CFG_LOAD_FAIL MSG_PREFIX "0072E" " Launcher Could not load configurations\n" #define MSG_CFG_SCHEMA_FAIL MSG_PREFIX "0073E" " Launcher Could not load schemas, status=%d\n" +#define MSG_NO_LOG_CONTEXT MSG_PREFIX "0074E" " Log context was not created\n" #endif // MSG_H From 58ba74fdb9e26538edfe09ba5ae2e7a4111596d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Hern=C3=A1n=20Carle?= Date: Fri, 24 Mar 2023 12:22:51 +0100 Subject: [PATCH 6/6] pr review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pablo Hernán Carle --- src/main.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main.c b/src/main.c index cf21ce6..652144c 100644 --- a/src/main.c +++ b/src/main.c @@ -735,12 +735,10 @@ static const char **env_comp(zl_comp_t *comp) { const char **env_comp = malloc(env_records + 1); int i = 0; - char *aux = NULL; for (char **env = shared_uss_env; *env != 0 && i < env_records; env++) { char *thisEnv = *env; - aux = malloc(strlen(thisEnv) + 1); + char *aux = malloc(strlen(thisEnv) + 1); strncpy(aux, thisEnv, strlen(thisEnv)); - aux[strlen(thisEnv)] = 0; trimRight(aux, strlen(aux)); env_comp[i] = aux; i++; @@ -1272,7 +1270,7 @@ static void handle_get_component_line(void *data, const char *line) { } } -static char* get_get_launch_components_cmd(char* sharedenv) { +static char* get_launch_components_cmd(char* sharedenv) { const char basecmd[] = "%s %s/bin/zwe internal get-launch-components --config \"%s\" --ha-instance %s"; int size = strlen(zl_context.root_dir) + strlen(zl_context.config_path) + strlen(zl_context.ha_instance_id) + strlen(sharedenv) + sizeof(basecmd) + 1; char *command = malloc(size); @@ -1329,7 +1327,7 @@ static char* get_sharedenv(void) { static int get_component_list(char *buf, size_t buf_size) { char *sharedenv = get_sharedenv(); - char *command = get_get_launch_components_cmd(sharedenv); + char *command = get_launch_components_cmd(sharedenv); free(sharedenv);