Skip to content

Commit

Permalink
run_command: report system call errors instead of returning error codes
Browse files Browse the repository at this point in the history
The motivation for this change is that system call failures are serious
errors that should be reported to the user, but only few callers took the
burden to decode the error codes that the functions returned into error
messages.

If at all, then only an unspecific error message was given. A prominent
example is this:

   $ git upload-pack . | :
   fatal: unable to run 'git-upload-pack'

In this example, git-upload-pack, the external command invoked through the
git wrapper, dies due to SIGPIPE, but the git wrapper does not bother to
report the real cause. In fact, this very error message is copied to the
syslog if git-daemon's client aborts the connection early.

With this change, system call failures are reported immediately after the
failure and only a generic failure code is returned to the caller. In the
above example the error is now to the point:

   $ git upload-pack . | :
   error: git-upload-pack died of signal

Note that there is no error report if the invoked program terminated with
a non-zero exit code, because it is reasonable to expect that the invoked
program has already reported an error. (But many run_command call sites
nevertheless write a generic error message.)

There was one special return code that was used to identify the case where
run_command failed because the requested program could not be exec'd. This
special case is now treated like a system call failure with errno set to
ENOENT. No error is reported in this case, because the call site in git.c
expects this as a normal result. Therefore, the callers that carefully
decoded the return value still check for this condition.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Johannes Sixt authored and Junio C Hamano committed Jul 6, 2009
1 parent 5709e03 commit 0ac77ec
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 86 deletions.
22 changes: 3 additions & 19 deletions builtin-receive-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,11 @@ static const char post_receive_hook[] = "hooks/post-receive";

static int run_status(int code, const char *cmd_name)
{
switch (code) {
case 0:
return 0;
case -ERR_RUN_COMMAND_FORK:
return error("fork of %s failed", cmd_name);
case -ERR_RUN_COMMAND_EXEC:
if (code < 0 && errno == ENOENT)
return error("execute of %s failed", cmd_name);
case -ERR_RUN_COMMAND_PIPE:
return error("pipe failed");
case -ERR_RUN_COMMAND_WAITPID:
return error("waitpid failed");
case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
return error("waitpid is confused");
case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
return error("%s died of signal", cmd_name);
case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
return error("%s died strangely", cmd_name);
default:
else if (code > 0)
error("%s exited with error code %d", cmd_name, code);
return code;
}
return code;
}

static int run_receive_hook(const char *hook_name)
Expand Down
6 changes: 1 addition & 5 deletions git.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,8 @@ static void execv_dashed_external(const char **argv)
* OK to return. Otherwise, we just pass along the status code.
*/
status = run_command_v_opt(argv, 0);
if (status != -ERR_RUN_COMMAND_EXEC) {
if (status < 0)
die("unable to run '%s'", argv[0]);
if (status >= 0 || errno != ENOENT)
exit(status);
}
errno = ENOENT; /* as if we called execvp */

argv[0] = tmp;

Expand Down
89 changes: 49 additions & 40 deletions run-command.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ int start_command(struct child_process *cmd)
{
int need_in, need_out, need_err;
int fdin[2], fdout[2], fderr[2];
int failed_errno;

/*
* In case of errors we must keep the promise to close FDs
Expand All @@ -28,9 +29,10 @@ int start_command(struct child_process *cmd)
need_in = !cmd->no_stdin && cmd->in < 0;
if (need_in) {
if (pipe(fdin) < 0) {
failed_errno = errno;
if (cmd->out > 0)
close(cmd->out);
return -ERR_RUN_COMMAND_PIPE;
goto fail_pipe;
}
cmd->in = fdin[1];
}
Expand All @@ -40,18 +42,20 @@ int start_command(struct child_process *cmd)
&& cmd->out < 0;
if (need_out) {
if (pipe(fdout) < 0) {
failed_errno = errno;
if (need_in)
close_pair(fdin);
else if (cmd->in)
close(cmd->in);
return -ERR_RUN_COMMAND_PIPE;
goto fail_pipe;
}
cmd->out = fdout[0];
}

need_err = !cmd->no_stderr && cmd->err < 0;
if (need_err) {
if (pipe(fderr) < 0) {
failed_errno = errno;
if (need_in)
close_pair(fdin);
else if (cmd->in)
Expand All @@ -60,7 +64,11 @@ int start_command(struct child_process *cmd)
close_pair(fdout);
else if (cmd->out)
close(cmd->out);
return -ERR_RUN_COMMAND_PIPE;
fail_pipe:
error("cannot create pipe for %s: %s",
cmd->argv[0], strerror(failed_errno));
errno = failed_errno;
return -1;
}
cmd->err = fderr[0];
}
Expand Down Expand Up @@ -122,6 +130,9 @@ int start_command(struct child_process *cmd)
strerror(errno));
exit(127);
}
if (cmd->pid < 0)
error("cannot fork() for %s: %s", cmd->argv[0],
strerror(failed_errno = errno));
#else
int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
const char **sargv = cmd->argv;
Expand Down Expand Up @@ -173,6 +184,9 @@ int start_command(struct child_process *cmd)
}

cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
failed_errno = errno;
if (cmd->pid < 0 && errno != ENOENT)
error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));

if (cmd->env)
free_environ(env);
Expand All @@ -189,7 +203,6 @@ int start_command(struct child_process *cmd)
#endif

if (cmd->pid < 0) {
int err = errno;
if (need_in)
close_pair(fdin);
else if (cmd->in)
Expand All @@ -200,9 +213,8 @@ int start_command(struct child_process *cmd)
close(cmd->out);
if (need_err)
close_pair(fderr);
return err == ENOENT ?
-ERR_RUN_COMMAND_EXEC :
-ERR_RUN_COMMAND_FORK;
errno = failed_errno;
return -1;
}

if (need_in)
Expand All @@ -221,33 +233,41 @@ int start_command(struct child_process *cmd)
return 0;
}

static int wait_or_whine(pid_t pid)
static int wait_or_whine(pid_t pid, const char *argv0)
{
for (;;) {
int status, code;
pid_t waiting = waitpid(pid, &status, 0);

if (waiting < 0) {
if (errno == EINTR)
continue;
error("waitpid failed (%s)", strerror(errno));
return -ERR_RUN_COMMAND_WAITPID;
}
if (waiting != pid)
return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
if (WIFSIGNALED(status))
return -ERR_RUN_COMMAND_WAITPID_SIGNAL;

if (!WIFEXITED(status))
return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
int status, code = -1;
pid_t waiting;
int failed_errno = 0;

while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
; /* nothing */

if (waiting < 0) {
failed_errno = errno;
error("waitpid for %s failed: %s", argv0, strerror(errno));
} else if (waiting != pid) {
error("waitpid is confused (%s)", argv0);
} else if (WIFSIGNALED(status)) {
error("%s died of signal", argv0);
} else if (WIFEXITED(status)) {
code = WEXITSTATUS(status);
return code == 127 ? -ERR_RUN_COMMAND_EXEC : code;
/*
* Convert special exit code when execvp failed.
*/
if (code == 127) {
code = -1;
failed_errno = ENOENT;
}
} else {
error("waitpid is confused (%s)", argv0);
}
errno = failed_errno;
return code;
}

int finish_command(struct child_process *cmd)
{
return wait_or_whine(cmd->pid);
return wait_or_whine(cmd->pid, cmd->argv[0]);
}

int run_command(struct child_process *cmd)
Expand Down Expand Up @@ -331,10 +351,7 @@ int start_async(struct async *async)
int finish_async(struct async *async)
{
#ifndef __MINGW32__
int ret = 0;

if (wait_or_whine(async->pid))
ret = error("waitpid (async) failed");
int ret = wait_or_whine(async->pid, "child process");
#else
DWORD ret = 0;
if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
Expand Down Expand Up @@ -378,15 +395,7 @@ int run_hook(const char *index_file, const char *name, ...)
hook.env = env;
}

ret = start_command(&hook);
ret = run_command(&hook);
free(argv);
if (ret) {
warning("Could not spawn %s", argv[0]);
return ret;
}
ret = finish_command(&hook);
if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
warning("%s exited due to uncaught signal", argv[0]);

return ret;
}
10 changes: 0 additions & 10 deletions run-command.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
#ifndef RUN_COMMAND_H
#define RUN_COMMAND_H

enum {
ERR_RUN_COMMAND_FORK = 10000,
ERR_RUN_COMMAND_EXEC,
ERR_RUN_COMMAND_PIPE,
ERR_RUN_COMMAND_WAITPID,
ERR_RUN_COMMAND_WAITPID_WRONG_PID,
ERR_RUN_COMMAND_WAITPID_SIGNAL,
ERR_RUN_COMMAND_WAITPID_NOEXIT,
};

struct child_process {
const char **argv;
pid_t pid;
Expand Down
5 changes: 4 additions & 1 deletion t/t5530-upload-pack-error.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ test_expect_success 'upload-pack fails due to error in rev-list' '
! echo "0032want $(git rev-parse HEAD)
00000009done
0000" | git upload-pack . > /dev/null 2> output.err &&
grep "waitpid (async) failed" output.err
# pack-objects survived
grep "Total.*, reused" output.err &&
# but there was an error, which must have been in rev-list
grep "bad tree object" output.err
'

test_expect_success 'create empty repository' '
Expand Down
12 changes: 1 addition & 11 deletions transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,8 @@ static int curl_transport_push(struct transport *transport, int refspec_nr, cons
argv[argc++] = *refspec++;
argv[argc] = NULL;
err = run_command_v_opt(argv, RUN_GIT_CMD);
switch (err) {
case -ERR_RUN_COMMAND_FORK:
error("unable to fork for %s", argv[0]);
case -ERR_RUN_COMMAND_EXEC:
if (err < 0 && errno == ENOENT)
error("unable to exec %s", argv[0]);
break;
case -ERR_RUN_COMMAND_WAITPID:
case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
error("%s died with strange error", argv[0]);
}
return !!err;
}

Expand Down

0 comments on commit 0ac77ec

Please sign in to comment.