From d2e11024c7e5ee356d009e3bb269814db42dc306 Mon Sep 17 00:00:00 2001 From: Wang Xu Date: Thu, 6 Apr 2017 00:09:37 +0800 Subject: [PATCH] double confirm the process has already been started Having sent pid, the container might have not been started well, a signal comes at this time may not take into effect. see #285 Signed-off-by: Wang Xu --- src/exec.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/exec.c b/src/exec.c index afde3fb8..09bd6139 100644 --- a/src/exec.c +++ b/src/exec.c @@ -29,7 +29,7 @@ struct stdio_config { }; static int hyper_release_exec(struct hyper_exec *); -static void hyper_exec_process(struct hyper_exec *exec, struct stdio_config *io); +static void hyper_exec_process(struct hyper_exec *exec, int pipe, struct stdio_config *io); static int send_exec_finishing(uint64_t seq, int len, int code) { @@ -500,7 +500,6 @@ static int hyper_do_exec_cmd(struct hyper_exec *exec, int pipe, struct stdio_con if (hyper_enter_sandbox(exec->pod, pipe) < 0) { perror("enter pidns of pod init failed"); - hyper_send_type(pipe, -1); goto out; } @@ -530,13 +529,14 @@ static int hyper_do_exec_cmd(struct hyper_exec *exec, int pipe, struct stdio_con else unsetenv("TERM"); - hyper_exec_process(exec, io); + hyper_exec_process(exec, pipe, io); out: + hyper_send_type(pipe, -1); _exit(125); } // do the exec, no return -static void hyper_exec_process(struct hyper_exec *exec, struct stdio_config *io) +static void hyper_exec_process(struct hyper_exec *exec, int pipe, struct stdio_config *io) { bool defaultPath = false; if (sigprocmask(SIG_SETMASK, &orig_mask, NULL) < 0) { @@ -563,9 +563,12 @@ static void hyper_exec_process(struct hyper_exec *exec, struct stdio_config *io) setsid(); + //send success notification before stdio was installed, otherwise the debug log in hyper_send_type() will be sent to user tty. + hyper_send_type(pipe, 0); + if (hyper_install_process_stdio(exec, io) < 0) { fprintf(stderr, "dup pts to exec stdio failed\n"); - goto exit; + goto exit_nosend; } if (execvp(exec->argv[0], exec->argv) < 0) { @@ -581,6 +584,8 @@ static void hyper_exec_process(struct hyper_exec *exec, struct stdio_config *io) } exit: + hyper_send_type(pipe, -1); +exit_nosend: fflush(NULL); _exit(125); } @@ -626,7 +631,7 @@ int hyper_run_process(struct hyper_exec *exec) { int pipe[2] = {-1, -1}; int pid, ret = -1; - uint32_t type; + uint32_t cpid, type; struct stdio_config io = {-1, -1,-1, -1,-1, -1}; if (exec->argv == NULL || exec->seq == 0 || exec->container_id == NULL || strlen(exec->container_id) == 0) { @@ -652,14 +657,14 @@ int hyper_run_process(struct hyper_exec *exec) } else if (pid == 0) { if (strcmp(exec->container_id, HYPERSTART_EXEC_CONTAINER) == 0) { hyper_send_type(pipe[1], getpid()); - hyper_exec_process(exec, &io); + hyper_exec_process(exec, pipe[1], &io); _exit(125); } hyper_do_exec_cmd(exec, pipe[1], &io); } fprintf(stdout, "prerequisite process pid %d\n", pid); - if (hyper_get_type(pipe[0], &type) < 0 || (int)type < 0) { + if (hyper_get_type(pipe[0], &cpid) < 0 || (int)cpid < 0) { fprintf(stderr, "run process failed\n"); goto close_tty; } @@ -669,7 +674,13 @@ int hyper_run_process(struct hyper_exec *exec) goto close_tty; } - exec->pid = type; + if (hyper_get_type(pipe[0], &type) < 0 || (int)type < 0) { + fprintf(stderr, "container process %u exec failed\n", cpid); + goto close_tty; + } + + //two message may come back in reversed sequence + exec->pid = (type == 0) ? cpid : type; list_add_tail(&exec->list, &exec->pod->exec_head); exec->ref++; fprintf(stdout, "%s process pid %d\n", __func__, exec->pid);