2005-07-31 19:17:43 +00:00
|
|
|
#include "cache.h"
|
|
|
|
#include "run-command.h"
|
2006-01-11 02:12:17 +00:00
|
|
|
#include "exec_cmd.h"
|
2005-07-31 19:17:43 +00:00
|
|
|
|
2006-12-31 02:55:15 +00:00
|
|
|
int run_command_v_opt(const char **argv, int flags)
|
2005-07-31 19:17:43 +00:00
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
|
|
|
|
if (pid < 0)
|
|
|
|
return -ERR_RUN_COMMAND_FORK;
|
|
|
|
if (!pid) {
|
2006-12-31 02:55:22 +00:00
|
|
|
if (flags & RUN_COMMAND_NO_STDIN) {
|
2005-12-08 02:04:38 +00:00
|
|
|
int fd = open("/dev/null", O_RDWR);
|
|
|
|
dup2(fd, 0);
|
2006-01-11 02:12:17 +00:00
|
|
|
close(fd);
|
2006-12-31 02:55:22 +00:00
|
|
|
}
|
|
|
|
if (flags & RUN_COMMAND_STDOUT_TO_STDERR)
|
2006-12-31 02:55:19 +00:00
|
|
|
dup2(2, 1);
|
2006-01-11 02:12:17 +00:00
|
|
|
if (flags & RUN_GIT_CMD) {
|
|
|
|
execv_git_cmd(argv);
|
|
|
|
} else {
|
|
|
|
execvp(argv[0], (char *const*) argv);
|
2005-12-08 02:04:38 +00:00
|
|
|
}
|
2005-08-02 21:24:22 +00:00
|
|
|
die("exec %s failed.", argv[0]);
|
2005-07-31 19:17:43 +00:00
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
int status, code;
|
2006-08-15 17:40:06 +00:00
|
|
|
pid_t waiting = waitpid(pid, &status, 0);
|
2005-07-31 19:17:43 +00:00
|
|
|
|
2006-08-15 17:40:06 +00:00
|
|
|
if (waiting < 0) {
|
2005-07-31 19:17:43 +00:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2006-08-15 17:40:06 +00:00
|
|
|
error("waitpid failed (%s)", strerror(errno));
|
2005-07-31 19:17:43 +00:00
|
|
|
return -ERR_RUN_COMMAND_WAITPID;
|
|
|
|
}
|
2006-08-15 17:40:06 +00:00
|
|
|
if (waiting != pid)
|
2005-07-31 19:17:43 +00:00
|
|
|
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;
|
|
|
|
code = WEXITSTATUS(status);
|
|
|
|
if (code)
|
|
|
|
return -code;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|