diff --git a/src/container.c b/src/container.c index a56f969..99b671c 100644 --- a/src/container.c +++ b/src/container.c @@ -402,9 +402,9 @@ static int container_setup_workdir(struct hyper_container *container) return 0; } -static int container_setup_tty(int fd, struct hyper_container *container) +static int container_setup_tty(struct hyper_container *container) { - return hyper_dup_exec_tty(fd, &container->exec); + return hyper_dup_exec_tty(&container->exec); } static int hyper_rescan_scsi(void) @@ -457,7 +457,6 @@ struct hyper_container_arg { struct hyper_pod *pod; int ipcns; int utsns; - int pipe[2]; }; static int hyper_container_init(void *data) @@ -617,7 +616,7 @@ static int hyper_container_init(void *data) fflush(stdout); - if (container_setup_tty(arg->pipe[1], container) < 0) { + if (container_setup_tty(container) < 0) { fprintf(stdout, "setup tty failed\n"); goto fail; } @@ -625,11 +624,15 @@ static int hyper_container_init(void *data) execvp(container->exec.argv[0], container->exec.argv); perror("exec container command failed"); - _exit(-1); + /* 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); fail: - hyper_send_type(arg->pipe[1], ERROR); - _exit(-1); + _exit(125); } static int hyper_setup_pty(struct hyper_container *c) @@ -666,11 +669,9 @@ int hyper_start_container(struct hyper_container *container, .pod = pod, .utsns = utsns, .ipcns = ipcns, - .pipe = {-1, -1}, }; int flags = CLONE_NEWNS | SIGCHLD; char path[128]; - uint32_t type; void *stack; int pid; @@ -685,11 +686,6 @@ int hyper_start_container(struct hyper_container *container, goto fail; } - if (pipe2(arg.pipe, O_CLOEXEC) < 0) { - perror("create pipe between pod init execcmd failed"); - goto fail; - } - if (hyper_watch_exec_pty(&container->exec, pod) < 0) { fprintf(stderr, "faile to watch container pty\n"); goto fail; @@ -715,24 +711,13 @@ int hyper_start_container(struct hyper_container *container, goto fail; } - /* wait for ready message */ - if (hyper_get_type(arg.pipe[0], &type) < 0 || type != READY) { - fprintf(stderr, "wait for container started failed\n"); - goto fail; - } - container->exec.pid = pid; list_add_tail(&container->exec.list, &pod->exec_head); container->exec.ref++; - close(arg.pipe[0]); - close(arg.pipe[1]); - fprintf(stdout, "container %s,init pid %d,ref %d\n", container->id, pid, container->exec.ref); return 0; fail: - close(arg.pipe[0]); - close(arg.pipe[1]); close(container->ns); hyper_reset_event(&container->exec.stdinev); hyper_reset_event(&container->exec.stdoutev); diff --git a/src/exec.c b/src/exec.c index 97cfda4..2c0f121 100644 --- a/src/exec.c +++ b/src/exec.c @@ -390,7 +390,7 @@ int hyper_setup_exec_tty(struct hyper_exec *e) return 0; } -int hyper_dup_exec_tty(int to, struct hyper_exec *e) +int hyper_dup_exec_tty(struct hyper_exec *e) { int ret = -1; @@ -416,7 +416,6 @@ int hyper_dup_exec_tty(int to, struct hyper_exec *e) } fflush(stdout); - hyper_send_type(to, READY); if (dup2(e->stdinfd, STDIN_FILENO) < 0) { perror("dup tty device to stdin failed"); @@ -535,8 +534,7 @@ static int hyper_do_exec_cmd(void *data) struct hyper_exec_arg *arg = data; struct hyper_exec *exec = arg->exec; struct hyper_pod *pod = arg->pod; - int pipe[2] = {-1, -1}, pid; - int ret = -1; + int pid, ret = -1; if (exec->id) { char path[512]; @@ -558,11 +556,6 @@ static int hyper_do_exec_cmd(void *data) close(pidns); } - if (pipe2(pipe, O_CLOEXEC) < 0) { - perror("create pipe in exec command failed"); - goto out; - } - if (hyper_watch_exec_pty(exec, pod) < 0) { fprintf(stderr, "add pts master event failed\n"); goto out; @@ -573,14 +566,6 @@ static int hyper_do_exec_cmd(void *data) perror("fail to fork"); goto out; } else if (pid > 0) { - uint32_t type; - - if (hyper_get_type(pipe[0], &type) < 0 || type != READY) { - fprintf(stderr, "hyper init doesn't get execcmd ready message\n"); - goto out; - } - - fprintf(stdout, "hyper init get ready message\n"); exec->pid = pid; //TODO combin ref++ and add to list. list_add_tail(&exec->list, &pod->exec_head); @@ -600,25 +585,26 @@ static int hyper_do_exec_cmd(void *data) goto exit; } - if (hyper_dup_exec_tty(pipe[1], exec) < 0) { + if (hyper_dup_exec_tty(exec) < 0) { fprintf(stderr, "dup pts to exec stdio failed\n"); goto exit; } if (execvp(exec->argv[0], exec->argv) < 0) { perror("exec failed"); - goto exit; + + /* 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); + } - ret = 0; exit: - close(pipe[0]); - close(pipe[1]); - hyper_send_type(pipe[1], ERROR); - _exit(ret); + _exit(125); out: - close(pipe[0]); - close(pipe[1]); hyper_send_type(arg->pipe[1], ret ? ERROR : READY); _exit(ret); } diff --git a/src/exec.h b/src/exec.h index 3bb7b08..589668a 100644 --- a/src/exec.h +++ b/src/exec.h @@ -48,7 +48,7 @@ int hyper_exec_cmd(char *json, int length); 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(int fd, struct hyper_exec *e); +int hyper_dup_exec_tty(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);