From aa50e5812b160503f60cf550a7822aa9add41e7a Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Wed, 6 Apr 2016 14:59:08 +0800 Subject: [PATCH] move envs to struct hyper_exec Signed-off-by: Lai Jiangshan --- src/container.c | 2 +- src/container.h | 7 ------- src/exec.c | 4 +++- src/exec.h | 7 +++++++ src/parse.c | 46 ++++++++++++++++++++-------------------------- 5 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/container.c b/src/container.c index d5efdb2..21c3c2a 100644 --- a/src/container.c +++ b/src/container.c @@ -440,7 +440,7 @@ static int hyper_container_init(void *data) else unsetenv("TERM"); - if (hyper_setup_env(container->envs, container->envs_num) < 0) { + if (hyper_setup_env(container->exec.envs, container->exec.envs_num) < 0) { fprintf(stdout, "setup env failed\n"); goto fail; } diff --git a/src/container.h b/src/container.h index 5a43ac8..0b7bddb 100644 --- a/src/container.h +++ b/src/container.h @@ -3,11 +3,6 @@ #include "exec.h" -struct env { - char *env; - char *value; -}; - struct volume { char *device; char *scsiaddr; @@ -36,11 +31,9 @@ struct hyper_container { char *scsiaddr; char *fstype; struct volume *vols; - struct env *envs; struct fsmap *maps; struct sysctl *sys; int vols_num; - int envs_num; int maps_num; int sys_num; int ns; diff --git a/src/exec.c b/src/exec.c index e2644ab..3820a7f 100644 --- a/src/exec.c +++ b/src/exec.c @@ -421,7 +421,9 @@ int hyper_enter_container(struct hyper_pod *pod, /* TODO: wait for container finishing setup root */ chdir("/"); - ret = hyper_setup_env(c->envs, c->envs_num); + if (hyper_setup_env(c->exec.envs, c->exec.envs_num) < 0) + goto out; + ret = hyper_setup_env(exec->envs, exec->envs_num); out: close(ipcns); close(utsns); diff --git a/src/exec.h b/src/exec.h index 7ab2068..c84df97 100644 --- a/src/exec.h +++ b/src/exec.h @@ -4,6 +4,11 @@ #include "list.h" #include "event.h" +struct env { + char *env; + char *value; +}; + struct hyper_exec { struct list_head list; struct hyper_event stdinev; @@ -23,6 +28,8 @@ struct hyper_exec { // configs char *id; + struct env *envs; + int envs_num; char **argv; int argc; int tty; // use tty or not diff --git a/src/parse.c b/src/parse.c index d693ce1..15e56c2 100644 --- a/src/parse.c +++ b/src/parse.c @@ -167,6 +167,15 @@ static void container_cleanup_exec(struct hyper_exec *exec) free(exec->workdir); exec->workdir = NULL; + for (i = 0; i < exec->envs_num; i++) { + free(exec->envs[i].env); + free(exec->envs[i].value); + } + + free(exec->envs); + exec->envs = NULL; + exec->envs_num = 0; + for (i = 0; i < exec->argc; i++) { free(exec->argv[i]); } @@ -323,21 +332,7 @@ static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_ return i; } -static void container_free_envs(struct hyper_container *c) -{ - int i; - - for (i = 0; i < c->envs_num; i++) { - free(c->envs[i].env); - free(c->envs[i].value); - } - - free(c->envs); - c->envs = NULL; - c->envs_num = 0; -} - -static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t *toks) +static int container_parse_envs(struct hyper_exec *exec, char *json, jsmntok_t *toks) { int i = 0, j; @@ -346,17 +341,17 @@ static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t return -1; } - c->envs = calloc(toks[i].size, sizeof(*c->envs)); - if (c->envs == NULL) { + exec->envs = calloc(toks[i].size, sizeof(*exec->envs)); + if (exec->envs == NULL) { fprintf(stderr, "allocate memory for env failed\n"); return -1; } - c->envs_num = toks[i].size; - fprintf(stdout, "envs num %d\n", c->envs_num); + exec->envs_num = toks[i].size; + fprintf(stdout, "envs num %d\n", exec->envs_num); i++; - for (j = 0; j < c->envs_num; j++) { + for (j = 0; j < exec->envs_num; j++) { int i_env, next_env; if (toks[i].type != JSMN_OBJECT) { @@ -367,13 +362,13 @@ static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t i++; for (i_env = 0; i_env < next_env; i_env++, i++) { if (json_token_streq(json, &toks[i], "env")) { - c->envs[j].env = + exec->envs[j].env = (json_token_str(json, &toks[++i])); - fprintf(stdout, "envs %d env %s\n", j, c->envs[j].env); + fprintf(stdout, "envs %d env %s\n", j, exec->envs[j].env); } else if (json_token_streq(json, &toks[i], "value")) { - c->envs[j].value = + exec->envs[j].value = (json_token_str(json, &toks[++i])); - fprintf(stdout, "envs %d value %s\n", j, c->envs[j].value); + fprintf(stdout, "envs %d value %s\n", j, exec->envs[j].value); } else { fprintf(stdout, "get unknown section %s in envs\n", json_token_str(json, &toks[i])); @@ -448,7 +443,6 @@ void hyper_free_container(struct hyper_container *c) c->fstype = NULL; container_free_volumes(c); - container_free_envs(c); container_free_sysctl(c); container_free_fsmap(c); container_cleanup_exec(&c->exec); @@ -550,7 +544,7 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container * goto fail; i += next; } else if (json_token_streq(json, t, "envs") && t->size == 1) { - next = container_parse_envs(c, json, &toks[++i]); + next = container_parse_envs(&c->exec, json, &toks[++i]); if (next < 0) goto fail; i += next;