Commit 541123f2 authored by Pavan Balaji's avatar Pavan Balaji
Browse files

[svn-r7003] Cleanup the functionality to append environment variables to a list

based on the suggestion by Yauheni Zelenko @ Cadence.
parent b7507b1f
......@@ -201,8 +201,8 @@ struct HYD_arg_match_table {
/* Environment information */
struct HYD_env {
char *env_name;
char *env_value;
const char *env_name;
const char *env_value;
struct HYD_env *next;
};
......
......@@ -143,14 +143,15 @@ void HYDU_dbg_finalize(void);
/* env */
HYD_status HYDU_env_to_str(struct HYD_env *env, char **str);
HYD_status HYDU_str_to_env(char *str, struct HYD_env **env);
HYD_status HYDU_list_inherited_env(struct HYD_env **env_list);
struct HYD_env *HYDU_env_list_dup(struct HYD_env *env);
HYD_status HYDU_env_create(struct HYD_env **env, const char *env_name, const char *env_value);
HYD_status HYDU_env_free(struct HYD_env *env);
HYD_status HYDU_env_free_list(struct HYD_env *env);
struct HYD_env *HYDU_env_lookup(char *env_name, struct HYD_env *env_list);
HYD_status HYDU_append_env_to_list(struct HYD_env env, struct HYD_env **env_list);
HYD_status HYDU_append_env_to_list(const char *env_name, const char *env_value,
struct HYD_env ** env_list);
HYD_status HYDU_append_env_str_to_list(char *str, struct HYD_env **env_list);
HYD_status HYDU_putenv(struct HYD_env *env, HYD_env_overwrite_t overwrite);
HYD_status HYDU_putenv_list(struct HYD_env *env_list, HYD_env_overwrite_t overwrite);
HYD_status HYDU_comma_list_to_env_list(char *str, struct HYD_env **env_list);
......
......@@ -542,14 +542,14 @@ static HYD_status launch_procs(void)
if (!exec->env_prop) {
/* user didn't specify anything; add inherited env to optional env */
for (env = HYD_pmcd_pmip.user_global.global_env.inherited; env; env = env->next) {
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list(env->env_name, env->env_value, &force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
}
}
else if (!strcmp(exec->env_prop, "all")) {
/* user explicitly asked us to pass all the environment */
for (env = HYD_pmcd_pmip.user_global.global_env.inherited; env; env = env->next) {
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list(env->env_name, env->env_value, &force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
}
}
......@@ -564,7 +564,8 @@ static HYD_status launch_procs(void)
while (envstr) {
env = HYDU_env_lookup(envstr, HYD_pmcd_pmip.user_global.global_env.inherited);
if (env) {
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list(env->env_name, env->env_value,
&force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
}
envstr = strtok(NULL, ",");
......@@ -573,19 +574,19 @@ static HYD_status launch_procs(void)
/* global user env */
for (env = HYD_pmcd_pmip.user_global.global_env.user; env; env = env->next) {
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list(env->env_name, env->env_value, &force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
}
/* local user env */
for (env = exec->user_env; env; env = env->next) {
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list(env->env_name, env->env_value, &force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
}
/* system env */
for (env = HYD_pmcd_pmip.user_global.global_env.system; env; env = env->next) {
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list(env->env_name, env->env_value, &force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
}
......@@ -593,11 +594,9 @@ static HYD_status launch_procs(void)
if (HYD_pmcd_pmip.local.interface_env_name) {
if (HYD_pmcd_pmip.user_global.iface) {
/* The user asked us to use a specific interface; let's find it */
status = HYDU_env_create(&env, HYD_pmcd_pmip.local.interface_env_name,
HYD_pmcd_pmip.user_global.iface);
HYDU_ERR_POP(status, "unable to create env\n");
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list(HYD_pmcd_pmip.local.interface_env_name,
HYD_pmcd_pmip.user_global.iface,
&force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
}
else if (HYD_pmcd_pmip.local.hostname) {
......@@ -606,7 +605,9 @@ static HYD_status launch_procs(void)
HYD_pmcd_pmip.local.hostname);
HYDU_ERR_POP(status, "unable to create env\n");
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list(HYD_pmcd_pmip.local.interface_env_name,
HYD_pmcd_pmip.local.hostname,
&force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
}
}
......@@ -634,33 +635,26 @@ static HYD_status launch_procs(void)
* checkpointing case, use PMI_PORT format */
/* PMI_PORT */
status = HYDU_env_create(&env, "PMI_PORT", pmi_port);
HYDU_ERR_POP(status, "unable to create env\n");
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list("PMI_PORT", pmi_port, &force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
/* PMI_ID */
str = HYDU_int_to_str(pmi_rank);
status = HYDU_env_create(&env, "PMI_ID", str);
HYDU_ERR_POP(status, "unable to create env\n");
HYDU_FREE(str);
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list("PMI_ID", str, &force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
HYDU_FREE(str);
}
else {
/* PMI_RANK */
str = HYDU_int_to_str(pmi_rank);
status = HYDU_env_create(&env, "PMI_RANK", str);
HYDU_ERR_POP(status, "unable to create env\n");
HYDU_FREE(str);
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list("PMI_RANK", str, &force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
HYDU_FREE(str);
/* PMI_FD */
if (HYD_pmcd_pmip.system_global.pmi_fd) {
/* If a global PMI port is provided, use it */
status = HYDU_env_create(&env, "PMI_FD", HYD_pmcd_pmip.system_global.pmi_fd);
HYDU_ERR_POP(status, "unable to create env\n");
str = HYDU_strdup(HYD_pmcd_pmip.system_global.pmi_fd);
}
else {
if (socketpair(AF_UNIX, SOCK_STREAM, 0, pmi_fds) < 0)
......@@ -670,23 +664,18 @@ static HYD_status launch_procs(void)
HYDU_ERR_POP(status, "unable to register fd\n");
str = HYDU_int_to_str(pmi_fds[1]);
status = HYDU_env_create(&env, "PMI_FD", str);
HYDU_ERR_POP(status, "unable to create env\n");
HYDU_FREE(str);
HYD_pmcd_pmip.downstream.pmi_fd[process_id] = pmi_fds[0];
}
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list("PMI_FD", str, &force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
HYDU_FREE(str);
/* PMI_SIZE */
str = HYDU_int_to_str(HYD_pmcd_pmip.system_global.global_process_count);
status = HYDU_env_create(&env, "PMI_SIZE", str);
HYDU_ERR_POP(status, "unable to create env\n");
HYDU_FREE(str);
status = HYDU_append_env_to_list(*env, &force_env);
status = HYDU_append_env_to_list("PMI_SIZE", str, &force_env);
HYDU_ERR_POP(status, "unable to add env to list\n");
HYDU_FREE(str);
}
for (j = 0, arg = 0; exec->exec[j]; j++)
......
......@@ -171,7 +171,6 @@ static HYD_status global_env_fn(char *arg, char ***argv)
{
int i, count;
char *str;
struct HYD_env *env;
HYD_status status = HYD_SUCCESS;
count = atoi(**argv);
......@@ -184,17 +183,13 @@ static HYD_status global_env_fn(char *arg, char ***argv)
str++;
str[strlen(str) - 1] = 0;
}
status = HYDU_str_to_env(str, &env);
HYDU_ERR_POP(status, "error converting string to env\n");
if (!strcmp(arg, "global-inherited-env"))
HYDU_append_env_to_list(*env, &HYD_pmcd_pmip.user_global.global_env.inherited);
HYDU_append_env_str_to_list(str, &HYD_pmcd_pmip.user_global.global_env.inherited);
else if (!strcmp(arg, "global-system-env"))
HYDU_append_env_to_list(*env, &HYD_pmcd_pmip.user_global.global_env.system);
HYDU_append_env_str_to_list(str, &HYD_pmcd_pmip.user_global.global_env.system);
else if (!strcmp(arg, "global-user-env"))
HYDU_append_env_to_list(*env, &HYD_pmcd_pmip.user_global.global_env.user);
HYDU_FREE(env);
HYDU_append_env_str_to_list(str, &HYD_pmcd_pmip.user_global.global_env.user);
}
(*argv)++;
......@@ -306,7 +301,6 @@ static HYD_status exec_proc_count_fn(char *arg, char ***argv)
static HYD_status exec_local_env_fn(char *arg, char ***argv)
{
struct HYD_exec *exec = NULL;
struct HYD_env *env;
int i, count;
char *str;
HYD_status status = HYD_SUCCESS;
......@@ -326,10 +320,7 @@ static HYD_status exec_local_env_fn(char *arg, char ***argv)
str++;
str[strlen(str) - 1] = 0;
}
status = HYDU_str_to_env(str, &env);
HYDU_ERR_POP(status, "error converting string to env\n");
HYDU_append_env_to_list(*env, &exec->user_env);
HYDU_FREE(env);
HYDU_append_env_str_to_list(str, &exec->user_env);
}
(*argv)++;
......
......@@ -58,7 +58,6 @@ static void genv_help_fn(void)
static HYD_status genv_fn(char *arg, char ***argv)
{
char *env_name, *env_value, *str[2] = { 0 };
struct HYD_env *env;
HYD_status status = HYD_SUCCESS;
status = HYDU_strsplit(**argv, &str[0], &str[1], '=');
......@@ -78,11 +77,7 @@ static HYD_status genv_fn(char *arg, char ***argv)
env_value = HYDU_strdup(str[1]);
}
status = HYDU_env_create(&env, env_name, env_value);
HYDU_ERR_POP(status, "unable to create env struct\n");
HYDU_append_env_to_list(*env, &HYD_handle.user_global.global_env.user);
HYDU_env_free(env);
HYDU_append_env_to_list(env_name, env_value, &HYD_handle.user_global.global_env.user);
if (str[0])
HYDU_FREE(str[0]);
......@@ -452,7 +447,6 @@ static void env_help_fn(void)
static HYD_status env_fn(char *arg, char ***argv)
{
char *env_name, *env_value, *str[2] = { 0 };
struct HYD_env *env;
struct HYD_exec *exec;
HYD_status status = HYD_SUCCESS;
......@@ -473,14 +467,10 @@ static HYD_status env_fn(char *arg, char ***argv)
env_value = HYDU_strdup(str[1]);
}
status = HYDU_env_create(&env, env_name, env_value);
HYDU_ERR_POP(status, "unable to create env struct\n");
status = get_current_exec(&exec);
HYDU_ERR_POP(status, "get_current_exec returned error\n");
HYDU_append_env_to_list(*env, &exec->user_env);
HYDU_env_free(env);
HYDU_append_env_to_list(env_name, env_value, &exec->user_env);
if (str[0])
HYDU_FREE(str[0]);
......
......@@ -38,60 +38,8 @@ HYD_status HYDU_env_to_str(struct HYD_env *env, char **str)
}
HYD_status HYDU_str_to_env(char *str, struct HYD_env **env)
{
char *env_name, *env_value;
HYD_status status = HYD_SUCCESS;
HYDU_FUNC_ENTER();
HYDU_MALLOC((*env), struct HYD_env *, sizeof(struct HYD_env), status);
env_name = strtok(str, "=");
env_value = strtok(NULL, "=");
(*env)->env_name = HYDU_strdup(env_name);
(*env)->env_value = env_value ? HYDU_strdup(env_value) : NULL;
(*env)->next = NULL;
fn_exit:
HYDU_FUNC_EXIT();
return status;
fn_fail:
if (*env)
HYDU_FREE(*env);
*env = NULL;
goto fn_exit;
}
static struct HYD_env *env_dup(struct HYD_env env)
{
struct HYD_env *tenv;
HYD_status status = HYD_SUCCESS;
HYDU_FUNC_ENTER();
HYDU_MALLOC(tenv, struct HYD_env *, sizeof(struct HYD_env), status);
memcpy(tenv, &env, sizeof(struct HYD_env));
tenv->next = NULL;
tenv->env_name = HYDU_strdup(env.env_name);
tenv->env_value = env.env_value ? HYDU_strdup(env.env_value) : NULL;
fn_exit:
HYDU_FUNC_EXIT();
return tenv;
fn_fail:
if (tenv)
HYDU_FREE(tenv);
tenv = NULL;
goto fn_exit;
}
HYD_status HYDU_list_inherited_env(struct HYD_env **env_list)
{
struct HYD_env *env;
char *env_str;
int i, ret;
HYD_status status = HYD_SUCCESS;
......@@ -111,13 +59,9 @@ HYD_status HYDU_list_inherited_env(struct HYD_env **env_list)
env_str = HYDU_strdup(environ[i]);
status = HYDU_str_to_env(env_str, &env);
HYDU_ERR_POP(status, "error converting string to env\n");
status = HYDU_append_env_to_list(*env, env_list);
status = HYDU_append_env_str_to_list(env_str, env_list);
HYDU_ERR_POP(status, "unable to add env to list\n");
HYDU_env_free(env);
HYDU_FREE(env_str);
i++;
......@@ -142,7 +86,7 @@ struct HYD_env *HYDU_env_list_dup(struct HYD_env *env)
run = env;
tenv = NULL;
while (run) {
status = HYDU_append_env_to_list(*run, &tenv);
status = HYDU_append_env_to_list(run->env_name, run->env_value, &tenv);
HYDU_ERR_POP(status, "unable to add env to list\n");
run = run->next;
}
......@@ -232,17 +176,16 @@ struct HYD_env *HYDU_env_lookup(char *env_name, struct HYD_env *env_list)
return run;
}
HYD_status HYDU_append_env_to_list(struct HYD_env env, struct HYD_env ** env_list)
HYD_status HYDU_append_env_to_list(const char *env_name, const char *env_value,
struct HYD_env ** env_list)
{
struct HYD_env *run, *tenv;
HYD_status status = HYD_SUCCESS;
HYDU_FUNC_ENTER();
tenv = env_dup(env);
if (tenv == NULL)
HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR, "unable to dup env\n");
status = HYDU_env_create(&tenv, env_name, env_value);
HYDU_ERR_POP(status, "unable to create env structure\n");
tenv->next = NULL;
......@@ -254,7 +197,7 @@ HYD_status HYDU_append_env_to_list(struct HYD_env env, struct HYD_env ** env_lis
run = *env_list;
while (1) {
if (!strcmp(run->env_name, env.env_name)) {
if (!strcmp(run->env_name, env_name)) {
/* If we found an entry for this environment variable, just update it */
if (run->env_value != NULL && tenv->env_value != NULL) {
HYDU_FREE(run->env_value);
......@@ -264,7 +207,7 @@ HYD_status HYDU_append_env_to_list(struct HYD_env env, struct HYD_env ** env_lis
HYDU_FREE(run->env_value);
run->env_value = NULL;
}
else if (env.env_value != NULL) {
else if (env_value != NULL) {
run->env_value = HYDU_strdup(tenv->env_value);
}
......@@ -291,6 +234,25 @@ HYD_status HYDU_append_env_to_list(struct HYD_env env, struct HYD_env ** env_lis
goto fn_exit;
}
HYD_status HYDU_append_env_str_to_list(char *str, struct HYD_env **env_list)
{
char *env_name, *env_value;
HYD_status status = HYD_SUCCESS;
HYDU_FUNC_ENTER();
env_name = strtok(str, "=");
env_value = strtok(NULL, "=");
status = HYDU_append_env_to_list(env_name, env_value, env_list);
HYDU_ERR_POP(status, "unable to append env to list\n");
fn_exit:
HYDU_FUNC_EXIT();
return status;
fn_fail:
goto fn_exit;
}
HYD_status HYDU_putenv(struct HYD_env *env, HYD_env_overwrite_t overwrite)
{
......@@ -351,17 +313,13 @@ HYD_status HYDU_putenv_list(struct HYD_env *env_list, HYD_env_overwrite_t overwr
HYD_status HYDU_comma_list_to_env_list(char *str, struct HYD_env **env_list)
{
char *env_name;
struct HYD_env *env;
HYD_status status = HYD_SUCCESS;
HYDU_FUNC_ENTER();
env_name = strtok(str, ",");
do {
status = HYDU_env_create(&env, env_name, NULL);
HYDU_ERR_POP(status, "unable to create env struct\n");
status = HYDU_append_env_to_list(*env, env_list);
status = HYDU_append_env_to_list(env_name, NULL, env_list);
HYDU_ERR_POP(status, "unable to add env to list\n");
} while ((env_name = strtok(NULL, ",")));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment