2005-07-31 21:17:43 +02:00
|
|
|
#include "cache.h"
|
|
|
|
#include "run-command.h"
|
2006-01-11 03:12:17 +01:00
|
|
|
#include "exec_cmd.h"
|
2005-07-31 21:17:43 +02:00
|
|
|
|
2006-12-31 03:55:15 +01:00
|
|
|
int run_command_v_opt(const char **argv, int flags)
|
2005-07-31 21:17:43 +02:00
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
|
|
|
|
if (pid < 0)
|
|
|
|
return -ERR_RUN_COMMAND_FORK;
|
|
|
|
if (!pid) {
|
2006-12-31 03:55:22 +01:00
|
|
|
if (flags & RUN_COMMAND_NO_STDIN) {
|
2005-12-08 03:04:38 +01:00
|
|
|
int fd = open("/dev/null", O_RDWR);
|
|
|
|
dup2(fd, 0);
|
2006-01-11 03:12:17 +01:00
|
|
|
close(fd);
|
2006-12-31 03:55:22 +01:00
|
|
|
}
|
|
|
|
if (flags & RUN_COMMAND_STDOUT_TO_STDERR)
|
2006-12-31 03:55:19 +01:00
|
|
|
dup2(2, 1);
|
2006-01-11 03:12:17 +01:00
|
|
|
if (flags & RUN_GIT_CMD) {
|
|
|
|
execv_git_cmd(argv);
|
|
|
|
} else {
|
|
|
|
execvp(argv[0], (char *const*) argv);
|
2005-12-08 03:04:38 +01:00
|
|
|
}
|
2005-08-02 23:24:22 +02:00
|
|
|
die("exec %s failed.", argv[0]);
|
2005-07-31 21:17:43 +02:00
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
int status, code;
|
2006-08-15 19:40:06 +02:00
|
|
|
pid_t waiting = waitpid(pid, &status, 0);
|
2005-07-31 21:17:43 +02:00
|
|
|
|
2006-08-15 19:40:06 +02:00
|
|
|
if (waiting < 0) {
|
2005-07-31 21:17:43 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2006-08-15 19:40:06 +02:00
|
|
|
error("waitpid failed (%s)", strerror(errno));
|
2005-07-31 21:17:43 +02:00
|
|
|
return -ERR_RUN_COMMAND_WAITPID;
|
|
|
|
}
|
2006-08-15 19:40:06 +02:00
|
|
|
if (waiting != pid)
|
2005-07-31 21:17:43 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-31 03:55:15 +01:00
|
|
|
int run_command_v(const char **argv)
|
2005-12-08 03:04:38 +01:00
|
|
|
{
|
2006-12-31 03:55:15 +01:00
|
|
|
return run_command_v_opt(argv, 0);
|
2005-12-08 03:04:38 +01:00
|
|
|
}
|
|
|
|
|
2006-12-31 03:55:19 +01:00
|
|
|
static int run_command_va_opt(int opt, const char *cmd, va_list param)
|
2005-07-31 21:17:43 +02:00
|
|
|
{
|
|
|
|
int argc;
|
2006-03-05 11:47:29 +01:00
|
|
|
const char *argv[MAX_RUN_COMMAND_ARGS];
|
2005-07-31 21:17:43 +02:00
|
|
|
const char *arg;
|
|
|
|
|
2005-08-02 23:24:22 +02:00
|
|
|
argv[0] = (char*) cmd;
|
2005-07-31 21:17:43 +02:00
|
|
|
argc = 1;
|
|
|
|
while (argc < MAX_RUN_COMMAND_ARGS) {
|
|
|
|
arg = argv[argc++] = va_arg(param, char *);
|
|
|
|
if (!arg)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (MAX_RUN_COMMAND_ARGS <= argc)
|
|
|
|
return error("too many args to run %s", cmd);
|
2006-12-31 03:55:19 +01:00
|
|
|
return run_command_v_opt(argv, opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
int run_command_opt(int opt, const char *cmd, ...)
|
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
va_start(params, cmd);
|
|
|
|
r = run_command_va_opt(opt, cmd, params);
|
|
|
|
va_end(params);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int run_command(const char *cmd, ...)
|
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
va_start(params, cmd);
|
|
|
|
r = run_command_va_opt(0, cmd, params);
|
|
|
|
va_end(params);
|
|
|
|
return r;
|
2005-07-31 21:17:43 +02:00
|
|
|
}
|