From 40f04580ec7884c08307ea4401acd8b05f713e79 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Sun, 5 Jun 2016 22:56:47 +0800 Subject: [PATCH 1/7] chdir to the specific dir Signed-off-by: Lai Jiangshan --- src/exec.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/exec.c b/src/exec.c index 4237d01..e02675c 100644 --- a/src/exec.c +++ b/src/exec.c @@ -586,6 +586,11 @@ static int hyper_do_exec_cmd(void *data) goto exit; } + if (exec->workdir && chdir(exec->workdir) < 0) { + perror("change work directory failed"); + goto exit; + } + if (hyper_setup_exec_user(exec) < 0) { fprintf(stderr, "setup exec user failed\n"); goto exit; From ab6ff1d2bd04781d4c6b4adcd8e8458121ae3cf1 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Sun, 5 Jun 2016 22:50:59 +0800 Subject: [PATCH 2/7] setup the exec env in hyper_do_exec_cmd() Signed-off-by: Lai Jiangshan --- src/exec.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/exec.c b/src/exec.c index e02675c..7a6bf5c 100644 --- a/src/exec.c +++ b/src/exec.c @@ -519,9 +519,8 @@ int hyper_enter_container(struct hyper_pod *pod, /* TODO: wait for container finishing setup root */ chdir("/"); - if (hyper_setup_env(c->exec.envs, c->exec.envs_num) < 0) - goto out; - ret = hyper_setup_env(exec->envs, exec->envs_num); + // TODO: let the hyperd do it (merging the env) when needed. + ret = hyper_setup_env(c->exec.envs, c->exec.envs_num); out: close(ipcns); close(utsns); @@ -596,6 +595,12 @@ static int hyper_do_exec_cmd(void *data) goto exit; } + // set the container env + if (hyper_setup_env(exec->envs, exec->envs_num) < 0) { + fprintf(stderr, "setup env failed\n"); + goto exit; + } + if (hyper_dup_exec_tty(exec) < 0) { fprintf(stderr, "dup pts to exec stdio failed\n"); goto exit; From 72996cd5e88ebc066f4cd389dc67f60b181e8e4f Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Sun, 5 Jun 2016 22:05:44 +0800 Subject: [PATCH 3/7] make hyper_enter_container() static Signed-off-by: Lai Jiangshan --- src/exec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/exec.c b/src/exec.c index 7a6bf5c..b7c70ad 100644 --- a/src/exec.c +++ b/src/exec.c @@ -474,7 +474,7 @@ int hyper_watch_exec_pty(struct hyper_exec *exec, struct hyper_pod *pod) return 0; } -int hyper_enter_container(struct hyper_pod *pod, +static int hyper_enter_container(struct hyper_pod *pod, struct hyper_exec *exec) { int ipcns, utsns, mntns, ret; From 9ca23869fc47bc3f50fac4b453488716297c7b8e Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Sun, 5 Jun 2016 22:24:36 +0800 Subject: [PATCH 4/7] split container_setup_workdir() Signed-off-by: Lai Jiangshan --- src/container.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/container.c b/src/container.c index 2849188..5a62fd4 100644 --- a/src/container.c +++ b/src/container.c @@ -391,12 +391,7 @@ static int container_setup_workdir(struct hyper_container *container) { if (container->initialize) { // create workdir - hyper_mkdir(container->exec.workdir); - } - - if (container->exec.workdir && chdir(container->exec.workdir) < 0) { - perror("change work directory failed"); - return -1; + return hyper_mkdir(container->exec.workdir); } return 0; @@ -605,6 +600,11 @@ static int hyper_container_init(void *data) goto fail; } + if (container->exec.workdir && chdir(container->exec.workdir) < 0) { + perror("change work directory failed"); + return -1; + } + if (hyper_setup_exec_user(&container->exec) < 0) { fprintf(stderr, "setup exec user failed\n"); goto fail; From 3d57690daaaf6b923163a9936c8c459162c3323c Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Sun, 5 Jun 2016 22:27:14 +0800 Subject: [PATCH 5/7] send READY a little earlier when creating container the flush(stdout) is removed, because it is also called in the following code hyper_dup_exec_tty(). Signed-off-by: Lai Jiangshan --- src/container.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/container.c b/src/container.c index 5a62fd4..ec979d9 100644 --- a/src/container.c +++ b/src/container.c @@ -600,6 +600,8 @@ static int hyper_container_init(void *data) goto fail; } + hyper_send_type(arg->pipe[1], READY); + if (container->exec.workdir && chdir(container->exec.workdir) < 0) { perror("change work directory failed"); return -1; @@ -616,9 +618,6 @@ static int hyper_container_init(void *data) goto fail; } - hyper_send_type(arg->pipe[1], READY); - fflush(stdout); - if (container_setup_tty(container) < 0) { fprintf(stdout, "setup tty failed\n"); goto fail; From c3eee6616f3b1fccb2246bb98d41e4100e2d379b Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Sun, 5 Jun 2016 22:31:50 +0800 Subject: [PATCH 6/7] remove container_setup_tty() Signed-off-by: Lai Jiangshan --- src/container.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/container.c b/src/container.c index ec979d9..e9939e1 100644 --- a/src/container.c +++ b/src/container.c @@ -397,11 +397,6 @@ static int container_setup_workdir(struct hyper_container *container) return 0; } -static int container_setup_tty(struct hyper_container *container) -{ - return hyper_dup_exec_tty(&container->exec); -} - static int hyper_rescan_scsi(void) { struct dirent **list; @@ -618,7 +613,7 @@ static int hyper_container_init(void *data) goto fail; } - if (container_setup_tty(container) < 0) { + if (hyper_dup_exec_tty(&container->exec) < 0) { fprintf(stdout, "setup tty failed\n"); goto fail; } From 193c1cc97ed94ad89f5445a580781547e02e2313 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Sun, 5 Jun 2016 22:46:10 +0800 Subject: [PATCH 7/7] introduce hyper_exec_process() Signed-off-by: Lai Jiangshan --- src/container.c | 32 +------------------------------- src/exec.c | 16 +++++++++++++--- src/exec.h | 1 + 3 files changed, 15 insertions(+), 34 deletions(-) diff --git a/src/container.c b/src/container.c index e9939e1..6ef2ad1 100644 --- a/src/container.c +++ b/src/container.c @@ -596,37 +596,7 @@ static int hyper_container_init(void *data) } hyper_send_type(arg->pipe[1], READY); - - if (container->exec.workdir && chdir(container->exec.workdir) < 0) { - perror("change work directory failed"); - return -1; - } - - if (hyper_setup_exec_user(&container->exec) < 0) { - fprintf(stderr, "setup exec user failed\n"); - goto fail; - } - - // set the container env - if (hyper_setup_env(container->exec.envs, container->exec.envs_num) < 0) { - fprintf(stdout, "setup env failed\n"); - goto fail; - } - - if (hyper_dup_exec_tty(&container->exec) < 0) { - fprintf(stdout, "setup tty failed\n"); - goto fail; - } - - execvp(container->exec.argv[0], container->exec.argv); - perror("exec container command failed"); - - /* the exit codes follow the `chroot` standard, - see docker/docs/reference/run.md#exit-status */ - if (errno == ENOENT) - _exit(127); - else if (errno == EACCES) - _exit(126); + hyper_exec_process(&container->exec); fail: hyper_send_type(arg->pipe[1], ERROR); diff --git a/src/exec.c b/src/exec.c index b7c70ad..8cab953 100644 --- a/src/exec.c +++ b/src/exec.c @@ -585,6 +585,18 @@ static int hyper_do_exec_cmd(void *data) goto exit; } + hyper_exec_process(exec); + +exit: + _exit(125); +out: + hyper_send_type(arg->pipe[1], ret ? ERROR : READY); + _exit(ret); +} + +// do the exec, no return +void hyper_exec_process(struct hyper_exec *exec) +{ if (exec->workdir && chdir(exec->workdir) < 0) { perror("change work directory failed"); goto exit; @@ -618,10 +630,8 @@ static int hyper_do_exec_cmd(void *data) } exit: + fflush(stdout); _exit(125); -out: - hyper_send_type(arg->pipe[1], ret ? ERROR : READY); - _exit(ret); } static void hyper_free_exec(struct hyper_exec *exec) diff --git a/src/exec.h b/src/exec.h index 589668a..f176262 100644 --- a/src/exec.h +++ b/src/exec.h @@ -49,6 +49,7 @@ int hyper_release_exec(struct hyper_exec *, struct hyper_pod *); int hyper_container_execcmd(struct hyper_pod *pod); int hyper_setup_exec_tty(struct hyper_exec *e); int hyper_dup_exec_tty(struct hyper_exec *e); +void hyper_exec_process(struct hyper_exec *e); struct hyper_exec *hyper_find_exec_by_pid(struct list_head *head, int pid); struct hyper_exec *hyper_find_exec_by_seq(struct hyper_pod *pod, uint64_t seq); int hyper_setup_exec_user(struct hyper_exec *e);