Split run_command into two halves (start/finish)
[git/git.git] / run-command.c
CommitLineData
b1bf95bb
JW
1#include "cache.h"
2#include "run-command.h"
77cb17e9 3#include "exec_cmd.h"
b1bf95bb 4
ebcb5d16 5int start_command(struct child_process *cmd)
b1bf95bb 6{
ebcb5d16
SP
7 cmd->pid = fork();
8 if (cmd->pid < 0)
b1bf95bb 9 return -ERR_RUN_COMMAND_FORK;
ebcb5d16 10 if (!cmd->pid) {
f1000898 11 if (cmd->no_stdin) {
128aed68
DB
12 int fd = open("/dev/null", O_RDWR);
13 dup2(fd, 0);
77cb17e9 14 close(fd);
95d3c4f5 15 }
f1000898 16 if (cmd->stdout_to_stderr)
cd83c74c 17 dup2(2, 1);
f1000898
SP
18 if (cmd->git_cmd) {
19 execv_git_cmd(cmd->argv);
77cb17e9 20 } else {
f1000898 21 execvp(cmd->argv[0], (char *const*) cmd->argv);
128aed68 22 }
f1000898 23 die("exec %s failed.", cmd->argv[0]);
b1bf95bb 24 }
ebcb5d16
SP
25 return 0;
26}
27
28int finish_command(struct child_process *cmd)
29{
b1bf95bb
JW
30 for (;;) {
31 int status, code;
ebcb5d16 32 pid_t waiting = waitpid(cmd->pid, &status, 0);
b1bf95bb 33
6f002f98 34 if (waiting < 0) {
b1bf95bb
JW
35 if (errno == EINTR)
36 continue;
6f002f98 37 error("waitpid failed (%s)", strerror(errno));
b1bf95bb
JW
38 return -ERR_RUN_COMMAND_WAITPID;
39 }
ebcb5d16 40 if (waiting != cmd->pid)
b1bf95bb
JW
41 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
42 if (WIFSIGNALED(status))
43 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
44
45 if (!WIFEXITED(status))
46 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
47 code = WEXITSTATUS(status);
48 if (code)
49 return -code;
50 return 0;
51 }
52}
f1000898 53
ebcb5d16
SP
54int run_command(struct child_process *cmd)
55{
56 int code = start_command(cmd);
57 if (code)
58 return code;
59 return finish_command(cmd);
60}
61
f1000898
SP
62int run_command_v_opt(const char **argv, int opt)
63{
64 struct child_process cmd;
65 memset(&cmd, 0, sizeof(cmd));
66 cmd.argv = argv;
67 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
68 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
69 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
70 return run_command(&cmd);
71}