Start defining a more sophisticated run_command
[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
f1000898 5int run_command(struct child_process *cmd)
b1bf95bb
JW
6{
7 pid_t pid = fork();
8
9 if (pid < 0)
10 return -ERR_RUN_COMMAND_FORK;
11 if (!pid) {
f1000898 12 if (cmd->no_stdin) {
128aed68
DB
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, 0);
77cb17e9 15 close(fd);
95d3c4f5 16 }
f1000898 17 if (cmd->stdout_to_stderr)
cd83c74c 18 dup2(2, 1);
f1000898
SP
19 if (cmd->git_cmd) {
20 execv_git_cmd(cmd->argv);
77cb17e9 21 } else {
f1000898 22 execvp(cmd->argv[0], (char *const*) cmd->argv);
128aed68 23 }
f1000898 24 die("exec %s failed.", cmd->argv[0]);
b1bf95bb
JW
25 }
26 for (;;) {
27 int status, code;
6f002f98 28 pid_t waiting = waitpid(pid, &status, 0);
b1bf95bb 29
6f002f98 30 if (waiting < 0) {
b1bf95bb
JW
31 if (errno == EINTR)
32 continue;
6f002f98 33 error("waitpid failed (%s)", strerror(errno));
b1bf95bb
JW
34 return -ERR_RUN_COMMAND_WAITPID;
35 }
6f002f98 36 if (waiting != pid)
b1bf95bb
JW
37 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
38 if (WIFSIGNALED(status))
39 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
40
41 if (!WIFEXITED(status))
42 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
43 code = WEXITSTATUS(status);
44 if (code)
45 return -code;
46 return 0;
47 }
48}
f1000898
SP
49
50int run_command_v_opt(const char **argv, int opt)
51{
52 struct child_process cmd;
53 memset(&cmd, 0, sizeof(cmd));
54 cmd.argv = argv;
55 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
56 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
57 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
58 return run_command(&cmd);
59}