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] 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;