git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/4] start_command: report child process setup errors to the parent's stderr

From
Johannes Sixt <j6t@kdbg.org>
Date
Jan 10, 2010, 13:07 UTC
Message-ID
<201001101407.52281.j6t@kdbg.org>
In-Reply-To
<201001101404.22258.j6t@kdbg.org>

When the child process's environment is set up in start_command(), error messages were written to wherever the parent redirected the child's stderr channel. However, even if the parent redirected the child's stderr, errors during this setup process, including the exec itself, are usually an indication of a problem in the parent's environment. Therefore, the error messages should go to the parent's stderr.

Redirection of the child's error messages is usually only used to redirect hook error messages during client-server exchanges. In these cases, hook setup errors could be regarded as information leak.

This patch makes a copy of stderr if necessary and uses a special die routine that is used for all die() calls in the child that sends the errors messages to the parent's stderr.

The trace call that reported a failed execvp is removed (because it writes to stderr) and replaced by die_errno() with special treatment of ENOENT. The improvement in the error message can be seen with this sequence:

   mkdir .git/hooks/pre-commit
   git commit
Previously, the error message was
   error: cannot run .git/hooks/pre-commit: No such file or directory
and now it is
   fatal: cannot exec '.git/hooks/pre-commit': Permission denied
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
 This should address your concern that "errors go to who-knows-where",
 that I meanwhile share with you.
 run-command.c |   46 +++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/run-command.c b/run-command.c
index cf2d8f7..02c7bfb 100644
--- a/run-command.c
+++ b/run-command.c
@@ -15,6 +15,30 @@ static inline void dup_devnull(int to)
 	close(fd);
 }
 
+#ifndef WIN32
+static int child_err = 2;
+
+static NORETURN void die_child(const char *err, va_list params)
+{
+	char msg[4096];
+	int len = vsnprintf(msg, sizeof(msg), err, params);
+	if (len > sizeof(msg))
+		len = sizeof(msg);
+
+	write(child_err, "fatal: ", 7);
+	write(child_err, msg, len);
+	write(child_err, "\n", 1);
+	exit(128);
+}
+
+static inline void set_cloexec(int fd)
+{
+	int flags = fcntl(fd, F_GETFD);
+	if (flags >= 0)
+		fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
+}
+#endif
+
 int start_command(struct child_process *cmd)
 {
 	int need_in, need_out, need_err;
@@ -79,6 +103,17 @@ fail_pipe:
 	fflush(NULL);
 	cmd->pid = fork();
 	if (!cmd->pid) {
+		/*
+		 * Redirect the channel to write syscall error messages to
+		 * before redirecting the process's stderr so that all die()
+		 * in subsequent call paths use the parent's stderr.
+		 */
+		if (cmd->no_stderr || need_err) {
+			child_err = dup(2);
+			set_cloexec(child_err);
+		}
+		set_die_routine(die_child);
+
 		if (cmd->no_stdin)
 			dup_devnull(0);
 		else if (need_in) {
@@ -126,9 +161,14 @@ fail_pipe:
 		} else {
 			execvp(cmd->argv[0], (char *const*) cmd->argv);
 		}
-		trace_printf("trace: exec '%s' failed: %s\n", cmd->argv[0],
-				strerror(errno));
-		exit(127);
+		/*
+		 * Do not check for cmd->silent_exec_failure; the parent
+		 * process will check it when it sees this exit code.
+		 */
+		if (errno == ENOENT)
+			exit(127);
+		else
+			die_errno("cannot exec '%s'", cmd->argv[0]);
 	}
 	if (cmd->pid < 0)
 		error("cannot fork() for %s: %s", cmd->argv[0],
-- 
1.6.6.115.gd1ab3
Previous: Johannes SixtNext: Johannes Sixt
Message 4 of 11 in “Imporove remote helper exec failure reporting”
  1. 0/2 Imporove remote helper exec failure reportingIlari Liusvaara, Jan 9, 2010
  2. 1/2 Report exec errors from run-commandIlari Liusvaara, Jan 9, 2010
  3. 0/4 Detect exec errors in start_command earlyJohannes Sixt, Jan 10, 2010
  4. 1/4 start_command: report child process setup errors to the parent's stderrJohannes Sixt, Jan 10, 2010
  5. 2/4 run-command: move wait_or_whine earlierJohannes Sixt, Jan 10, 2010
  6. 3/4 start_command: detect execvp failures earlyJohannes Sixt, Jan 10, 2010
  7. Junio C HamanoJan 14, 2010
  8. Johannes SixtJan 14, 2010
  9. Junio C HamanoJan 14, 2010
  10. 4/4 Improve error message when a transport helper was not foundJohannes Sixt, Jan 10, 2010
  11. 2/2 Improve transport helper exec failure reportingIlari Liusvaara, Jan 9, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.