diff --git a/src/container.c b/src/container.c index 2849188..6ef2ad1 100644 --- a/src/container.c +++ b/src/container.c @@ -391,22 +391,12 @@ 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; } -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; @@ -605,34 +595,8 @@ static int hyper_container_init(void *data) goto fail; } - 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; - } - hyper_send_type(arg->pipe[1], READY); - fflush(stdout); - - if (container_setup_tty(container) < 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 4237d01..8cab953 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; @@ -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); @@ -586,11 +585,34 @@ 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; + } + if (hyper_setup_exec_user(exec) < 0) { fprintf(stderr, "setup exec user failed\n"); 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; @@ -608,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);