2 #include "run-command.h"
5 int start_command(struct child_process *cmd)
9 return -ERR_RUN_COMMAND_FORK;
12 int fd = open("/dev/null", O_RDWR);
16 if (cmd->stdout_to_stderr)
19 execv_git_cmd(cmd->argv);
21 execvp(cmd->argv[0], (char *const*) cmd->argv);
23 die("exec %s failed.", cmd->argv[0]);
28 int finish_command(struct child_process *cmd)
32 pid_t waiting = waitpid(cmd->pid, &status, 0);
37 error("waitpid failed (%s)", strerror(errno));
38 return -ERR_RUN_COMMAND_WAITPID;
40 if (waiting != cmd->pid)
41 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
42 if (WIFSIGNALED(status))
43 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
45 if (!WIFEXITED(status))
46 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
47 code = WEXITSTATUS(status);
54 int run_command(struct child_process *cmd)
56 int code = start_command(cmd);
59 return finish_command(cmd);
62 int run_command_v_opt(const char **argv, int opt)
64 struct child_process cmd;
65 memset(&cmd, 0, sizeof(cmd));
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);