From b26654a73694c20fcd895b3b93ad5d42a1d5b3fb Mon Sep 17 00:00:00 2001 From: zhongtao Date: Mon, 27 Nov 2023 14:52:43 +0800 Subject: [PATCH 39/64] fix the problem of abnormal branches not waiting for child processes Signed-off-by: zhongtao --- src/cmd/isulad-shim/common.c | 6 +++--- src/cmd/isulad-shim/process.c | 14 ++++++++------ src/daemon/modules/runtime/isula/isula_rt_ops.c | 16 ++++++++++------ src/daemon/modules/runtime/shim/shim_rt_ops.c | 15 +++++++++------ 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/cmd/isulad-shim/common.c b/src/cmd/isulad-shim/common.c index 48d266dc..3cc7d2a7 100644 --- a/src/cmd/isulad-shim/common.c +++ b/src/cmd/isulad-shim/common.c @@ -126,12 +126,12 @@ int cmd_combined_output(const char *binary, const char *params[], void *output, } *output_len = isula_file_read_nointr(stdio[0], output, BUFSIZ - 1); - close(stdio[0]); - close(exec_fd[0]); - wait(&status); ret = SHIM_OK; out: + close(stdio[0]); + close(exec_fd[0]); + wait(&status); if (ret != SHIM_OK) { kill(pid, 9); } diff --git a/src/cmd/isulad-shim/process.c b/src/cmd/isulad-shim/process.c index 187067d2..e8cb9b32 100644 --- a/src/cmd/isulad-shim/process.c +++ b/src/cmd/isulad-shim/process.c @@ -1472,7 +1472,7 @@ static void exec_runtime_process(process_t *p, int exec_fd) const char *params[MAX_RUNTIME_ARGS] = { 0 }; get_runtime_cmd(p, log_path, pid_path, process_desc, params); execvp(p->runtime, (char * const *)params); - (void)dprintf(exec_fd, "fork/exec error: %s", strerror(errno)); + (void)dprintf(exec_fd, "run process: %s error: %s", p->runtime, strerror(errno)); _exit(EXIT_FAILURE); } @@ -1510,11 +1510,6 @@ int create_process(process_t *p) close_fd(&p->stdio->resize); } nread = isula_file_read_nointr(exec_fd[0], exec_buff, sizeof(exec_buff) - 1); - if (nread > 0) { - write_message(ERR_MSG, "runtime error"); - ret = SHIM_ERR; - goto out; - } /* block to wait runtime pid exit */ ret = waitpid(pid, NULL, 0); @@ -1524,6 +1519,13 @@ int create_process(process_t *p) goto out; } + // if an error occurs in exec_runtime_process, jump directly to the out branch after waitpid. + if (nread > 0) { + write_message(ERR_MSG, "%s", exec_buff); + ret = SHIM_ERR; + goto out; + } + /* save runtime pid */ data = read_text_file("pid"); if (data == NULL) { diff --git a/src/daemon/modules/runtime/isula/isula_rt_ops.c b/src/daemon/modules/runtime/isula/isula_rt_ops.c index 859356e5..5d7ae500 100644 --- a/src/daemon/modules/runtime/isula/isula_rt_ops.c +++ b/src/daemon/modules/runtime/isula/isula_rt_ops.c @@ -906,17 +906,13 @@ realexec: } execvp(SHIM_BINARY, (char * const *)params); - (void)dprintf(shim_stderr_pipe[1], "exec failed: %s", strerror(errno)); + (void)dprintf(shim_stderr_pipe[1], "run process: %s failed: %s", SHIM_BINARY, strerror(errno)); + exit(EXIT_FAILURE); } close(shim_stderr_pipe[1]); close(shim_stdout_pipe[1]); num = util_read_nointr(shim_stderr_pipe[0], exec_buff, sizeof(exec_buff) - 1); - if (num > 0) { - ERROR("Exec failed: %s", exec_buff); - ret = -1; - goto out; - } status = util_wait_for_pid_status(pid); if (status < 0) { @@ -925,6 +921,14 @@ realexec: goto out; } + // if failed to exec, jump directly to the out branch after waitpid. + if (num > 0) { + ERROR("%s", exec_buff); + isulad_set_error_message("%s", exec_buff); + ret = -1; + goto out; + } + *shim_exit_code = status_to_exit_code(status); if (*shim_exit_code != 0) { ERROR("Isulad-shim exit error"); diff --git a/src/daemon/modules/runtime/shim/shim_rt_ops.c b/src/daemon/modules/runtime/shim/shim_rt_ops.c index 5066f804..81daf224 100644 --- a/src/daemon/modules/runtime/shim/shim_rt_ops.c +++ b/src/daemon/modules/runtime/shim/shim_rt_ops.c @@ -251,17 +251,13 @@ static int shim_bin_v2_create(const char *runtime, const char *id, const char *w } execvp(binary, (char * const *)params); - (void)dprintf(exec_fd[1], "exec failed: %s", strerror(errno)); + (void)dprintf(exec_fd[1], "run process: %s failed: %s", binary, strerror(errno)); exit(EXIT_FAILURE); } close(exec_fd[1]); exec_fd[1] = -1; - if (util_read_nointr(exec_fd[0], exec_buff, sizeof(exec_buff) - 1) > 0) { - ERROR("exec failed: %s", exec_buff); - ret = -1; - goto out; - } + nret = util_read_nointr(exec_fd[0], exec_buff, sizeof(exec_buff) - 1); close(exec_fd[0]); exec_fd[0] = -1; @@ -272,6 +268,13 @@ static int shim_bin_v2_create(const char *runtime, const char *id, const char *w goto out; } + // if failed to exec, jump directly to the out branch after waitpid. + if (nret > 0) { + ERROR("%s", exec_buff); + ret = -1; + goto out; + } + status = status_to_exit_code(status); close(out_fd[1]); -- 2.42.0