From 05cf3e2c1b015175be82d4040e2d2f421494ac57 Mon Sep 17 00:00:00 2001
From: tanyifeng <tanyifeng1@huawei.com>
Date: Wed, 15 May 2019 00:53:49 -0400
Subject: [PATCH 097/131] lxc: report error when fork/exec error for hooks

Signed-off-by: tanyifeng <tanyifeng1@huawei.com>
Signed-off-by: LiFeng <lifeng68@huawei.com>
---
 src/lxc/conf.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 6cf86a4e..341fdab5 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -4559,21 +4559,12 @@ static struct lxc_popen_FILE *lxc_popen_ocihook(char *commandpath, char **args,
 		goto on_error;
 
 	if (child_pid == 0) {
-		/* child */
-		int inherit_fds[2] = {
-			pipe_msg[0], pipe_fds[1]
-		};
-		if (lxc_check_inherited(NULL, true, inherit_fds, 2) != 0) {
-			SYSERROR("check inherited fd failed");
-			exit(127);
-		}
-
 		close(pipe_msg[1]);
 		if (pipe_msg[0] != STDIN_FILENO)
 			dup2(pipe_msg[0], STDIN_FILENO);
 		else {
 			if (fcntl(pipe_msg[0], F_SETFD, 0) != 0) {
-				SYSERROR("Failed to remove FD_CLOEXEC from fd.");
+				fprintf(stderr, "Failed to remove FD_CLOEXEC from fd.");
 				exit(127);
 			}
 		}
@@ -4600,6 +4591,11 @@ static struct lxc_popen_FILE *lxc_popen_ocihook(char *commandpath, char **args,
 		if (ret < 0)
 			_exit(EXIT_FAILURE);
 
+		if (lxc_check_inherited(NULL, true, NULL, 0) != 0) {
+			fprintf(stderr, "check inherited fd failed");
+			exit(127);
+		}
+
 		/*
 		 * Unblock signals.
 		 * This is the main/only reason
@@ -4615,6 +4611,7 @@ static struct lxc_popen_FILE *lxc_popen_ocihook(char *commandpath, char **args,
 			execvpe(commandpath, args, envs);
 		else
 			execvp(commandpath, args);
+		fprintf(stderr, "fork/exec %s: %s", commandpath, strerror(errno));
 		exit(127);
 	}
 
@@ -4764,8 +4761,8 @@ static int run_ocihook_buffer(struct oci_hook_conf *oconf, char *inmsg)
 		SYSERROR("Script exited with error.");
 		goto print_hook;
 	} else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0) {
-		ERROR("Script exited with status %d. output:%s", WEXITSTATUS(ret), output);
-		lxc_write_error_message(conf->errfd, "%s:%d: running %s hook caused \"error running hook: exit status %d, output:%s\".",
+		ERROR("Script exited with status %d. output: %s", WEXITSTATUS(ret), output);
+		lxc_write_error_message(conf->errfd, "%s:%d: running %s hook caused \"error running hook: exit status %d, output: %s\".",
 			__FILE__, __LINE__,
 			(conf->which >= NUM_LXC_HOOKS) ? "invalid type" : lxchook_names[conf->which],
 			WEXITSTATUS(ret), output);
-- 
2.23.0

