From e22eda3552702f1984a3d0c79ba62673bf597e15 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Mon, 27 Nov 2023 14:52:43 +0800
Subject: [PATCH 164/181] fix the problem of abnormal branches not waiting for
 child processes

Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
 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 c88de736..4a84cf40 100644
--- a/src/cmd/isulad-shim/common.c
+++ b/src/cmd/isulad-shim/common.c
@@ -193,12 +193,12 @@ int cmd_combined_output(const char *binary, const char *params[], void *output,
     }
     *output_len = 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 a91c3c16..df6b7599 100644
--- a/src/cmd/isulad-shim/process.c
+++ b/src/cmd/isulad-shim/process.c
@@ -1079,7 +1079,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);
 }
 
@@ -1117,11 +1117,6 @@ int create_process(process_t *p)
         close_fd(&p->stdio->resize);
     }
     nread = 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);
@@ -1131,6 +1126,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 f058ce84..c9e590b9 100644
--- a/src/daemon/modules/runtime/isula/isula_rt_ops.c
+++ b/src/daemon/modules/runtime/isula/isula_rt_ops.c
@@ -837,17 +837,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) {
@@ -856,6 +852,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 230c627a..459a870d 100644
--- a/src/daemon/modules/runtime/shim/shim_rt_ops.c
+++ b/src/daemon/modules/runtime/shim/shim_rt_ops.c
@@ -183,17 +183,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;
 
@@ -204,6 +200,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

