From 8803fa8ccbebdd7844f85de729975bc0ba267657 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Wed, 6 Apr 2016 10:35:50 +0800 Subject: [PATCH 1/7] move symlink of devices to container_setup_mount() Signed-off-by: Lai Jiangshan --- src/container.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/container.c b/src/container.c index c21390a..a7029c4 100644 --- a/src/container.c +++ b/src/container.c @@ -197,6 +197,11 @@ static int container_setup_mount(struct hyper_container *container) if (symlink("/dev/pts/ptmx", "./dev/ptmx") < 0) perror("link /dev/pts/ptmx to /dev/ptmx failed"); + symlink("/proc/self/fd", "./dev/fd"); + symlink("/proc/self/fd/0", "./dev/stdin"); + symlink("/proc/self/fd/1", "./dev/stdout"); + symlink("/proc/self/fd/2", "./dev/stderr"); + return 0; } @@ -541,11 +546,6 @@ static int hyper_container_init(void *data) goto fail; } - symlink("/proc/self/fd", "/dev/fd"); - symlink("/proc/self/fd/0", "/dev/stdin"); - symlink("/proc/self/fd/1", "/dev/stdout"); - symlink("/proc/self/fd/2", "/dev/stderr"); - execvp(container->exec.argv[0], container->exec.argv); perror("exec container command failed"); From 6a5656212089ed08cc2cfa17ab3fffe9fcc1ae83 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Wed, 6 Apr 2016 12:06:04 +0800 Subject: [PATCH 2/7] require exec->seq be set, we can't handle zero seq well Signed-off-by: Lai Jiangshan --- src/exec.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/exec.c b/src/exec.c index 873aa6e..e2644ab 100644 --- a/src/exec.c +++ b/src/exec.c @@ -227,8 +227,8 @@ int hyper_setup_exec_tty(struct hyper_exec *e) char ptmx[512], path[512]; if (e->seq == 0) { - e->ptyfd = open("/dev/null", O_RDWR | O_NOCTTY | O_CLOEXEC); - goto done; + fprintf(stderr, "e->seq should be set\n"); + return -1; } if (!e->tty) { // don't use tty for stdio @@ -289,7 +289,6 @@ int hyper_setup_exec_tty(struct hyper_exec *e) e->stdinev.fd = ptymaster; e->stdoutev.fd = dup(ptymaster); -done: if (e->errseq == 0) { e->stderrev.fd = dup(e->stdoutev.fd); } From 5a91148a37bd02007d12835131694205aa7dd29e Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Wed, 6 Apr 2016 12:52:25 +0800 Subject: [PATCH 3/7] remove unused var path Signed-off-by: Lai Jiangshan --- src/init.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/init.c b/src/init.c index ed9d04a..6f037e8 100644 --- a/src/init.c +++ b/src/init.c @@ -692,7 +692,6 @@ static int hyper_cmd_write_file(char *json, int length) struct hyper_pod *pod = &global_pod; int pipe[2] = {-1, -1}; int pid, mntns = -1, fd; - char path[512]; int len = 0, size, ret = -1; fprintf(stdout, "%s\n", __func__); From e4873032057713a9c52a4962a9b64a527d009ae5 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Wed, 6 Apr 2016 13:56:11 +0800 Subject: [PATCH 4/7] rename container_free_cmd() to container_cleanup_exec() Signed-off-by: Lai Jiangshan --- src/parse.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/parse.c b/src/parse.c index 99773df..9fc7866 100644 --- a/src/parse.c +++ b/src/parse.c @@ -157,17 +157,20 @@ static int container_parse_cmd(struct hyper_container *c, char *json, jsmntok_t return i; } -static void container_free_cmd(struct hyper_container *c) +static void container_cleanup_exec(struct hyper_exec *exec) { int i; - for (i = 0; i < c->exec.argc; i++) { - free(c->exec.argv[i]); + free(exec->id); + exec->id = NULL; + + for (i = 0; i < exec->argc; i++) { + free(exec->argv[i]); } - free(c->exec.argv); - c->exec.argv = NULL; - c->exec.argc = 0; + free(exec->argv); + exec->argv = NULL; + exec->argc = 0; } static void container_free_volumes(struct hyper_container *c) @@ -444,14 +447,11 @@ void hyper_free_container(struct hyper_container *c) free(c->fstype); c->fstype = NULL; - free(c->exec.id); - c->exec.id = NULL; - container_free_volumes(c); container_free_envs(c); container_free_sysctl(c); container_free_fsmap(c); - container_free_cmd(c); + container_cleanup_exec(&c->exec); list_del_init(&c->list); free(c); @@ -1098,13 +1098,8 @@ out: free(toks); return exec; fail: - free(exec->id); - for (i = 0; i < exec->argc; i++) - free(exec->argv[i]); - - free(exec->argv); + container_cleanup_exec(exec); free(exec); - exec = NULL; goto out; } From 64cbfd2a0bbf4abe45106ecff0628cb5db561fba Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Wed, 6 Apr 2016 14:19:15 +0800 Subject: [PATCH 5/7] move workdir to struct hyper_exec Signed-off-by: Lai Jiangshan --- src/container.c | 4 ++-- src/container.h | 1 - src/exec.h | 15 +++++++++------ src/parse.c | 10 +++++----- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/container.c b/src/container.c index a7029c4..d5efdb2 100644 --- a/src/container.c +++ b/src/container.c @@ -336,10 +336,10 @@ static int container_setup_workdir(struct hyper_container *container) { if (container->initialize) { // create workdir - hyper_mkdir(container->workdir); + hyper_mkdir(container->exec.workdir); } - if (container->workdir && chdir(container->workdir) < 0) { + if (container->exec.workdir && chdir(container->exec.workdir) < 0) { perror("change work directory failed"); return -1; } diff --git a/src/container.h b/src/container.h index f8f0144..5a43ac8 100644 --- a/src/container.h +++ b/src/container.h @@ -34,7 +34,6 @@ struct hyper_container { char *rootfs; char *image; char *scsiaddr; - char *workdir; char *fstype; struct volume *vols; struct env *envs; diff --git a/src/exec.h b/src/exec.h index d664976..7ab2068 100644 --- a/src/exec.h +++ b/src/exec.h @@ -9,12 +9,6 @@ struct hyper_exec { struct hyper_event stdinev; struct hyper_event stdoutev; struct hyper_event stderrev; - char *id; - char **argv; - int argc; - int tty; // use tty or not - uint64_t seq; - uint64_t errseq; int pid; int ptyno; int init; @@ -26,6 +20,15 @@ struct hyper_exec { uint8_t code; uint8_t exit; uint8_t ref; + + // configs + char *id; + char **argv; + int argc; + int tty; // use tty or not + uint64_t seq; + uint64_t errseq; + char *workdir; }; struct hyper_pod; diff --git a/src/parse.c b/src/parse.c index 9fc7866..d693ce1 100644 --- a/src/parse.c +++ b/src/parse.c @@ -164,6 +164,9 @@ static void container_cleanup_exec(struct hyper_exec *exec) free(exec->id); exec->id = NULL; + free(exec->workdir); + exec->workdir = NULL; + for (i = 0; i < exec->argc; i++) { free(exec->argv[i]); } @@ -441,9 +444,6 @@ void hyper_free_container(struct hyper_container *c) free(c->scsiaddr); c->scsiaddr = NULL; - free(c->workdir); - c->workdir = NULL; - free(c->fstype); c->fstype = NULL; @@ -524,8 +524,8 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container * fprintf(stdout, "container stderr seq %" PRIu64 "\n", c->exec.errseq); i++; } else if (json_token_streq(json, t, "workdir") && t->size == 1) { - c->workdir = (json_token_str(json, &toks[++i])); - fprintf(stdout, "container workdir %s\n", c->workdir); + c->exec.workdir = (json_token_str(json, &toks[++i])); + fprintf(stdout, "container workdir %s\n", c->exec.workdir); i++; } else if (json_token_streq(json, t, "image") && t->size == 1) { c->image = (json_token_str(json, &toks[++i])); From aa50e5812b160503f60cf550a7822aa9add41e7a Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Wed, 6 Apr 2016 14:59:08 +0800 Subject: [PATCH 6/7] 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; From 1cff207e6731ed90c5f6ee5ad26b569baf60a309 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Wed, 6 Apr 2016 15:03:52 +0800 Subject: [PATCH 7/7] kill all exec rather than only container in hyper_term_all() Signed-off-by: Lai Jiangshan --- src/init.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/init.c b/src/init.c index 6f037e8..4b8a0f3 100644 --- a/src/init.c +++ b/src/init.c @@ -143,7 +143,7 @@ static void hyper_term_all(struct hyper_pod *pod) DIR *dp; struct dirent *de; pid_t *pids = NULL; - struct hyper_container *c; + struct hyper_exec *e; dp = opendir("/proc"); if (dp == NULL) @@ -175,8 +175,8 @@ static void hyper_term_all(struct hyper_pod *pod) free(pids); closedir(dp); - list_for_each_entry(c, &pod->containers, list) - hyper_kill_process(c->exec.pid); + list_for_each_entry(e, &pod->exec_head, list) + hyper_kill_process(e->pid); } static int hyper_handle_exit(struct hyper_pod *pod)