1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
5 int run_command_v_opt(const char **argv, int flags)
6 {
7 pid_t pid = fork();
9 if (pid < 0)
10 return -ERR_RUN_COMMAND_FORK;
11 if (!pid) {
12 if (flags & RUN_COMMAND_NO_STDIN) {
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, 0);
15 close(fd);
16 }
17 if (flags & RUN_COMMAND_STDOUT_TO_STDERR)
18 dup2(2, 1);
19 if (flags & RUN_GIT_CMD) {
20 execv_git_cmd(argv);
21 } else {
22 execvp(argv[0], (char *const*) argv);
23 }
24 die("exec %s failed.", argv[0]);
25 }
26 for (;;) {
27 int status, code;
28 pid_t waiting = waitpid(pid, &status, 0);
30 if (waiting < 0) {
31 if (errno == EINTR)
32 continue;
33 error("waitpid failed (%s)", strerror(errno));
34 return -ERR_RUN_COMMAND_WAITPID;
35 }
36 if (waiting != pid)
37 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
38 if (WIFSIGNALED(status))
39 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
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 }
50 int run_command_v(const char **argv)
51 {
52 return run_command_v_opt(argv, 0);
53 }
55 static int run_command_va_opt(int opt, const char *cmd, va_list param)
56 {
57 int argc;
58 const char *argv[MAX_RUN_COMMAND_ARGS];
59 const char *arg;
61 argv[0] = (char*) cmd;
62 argc = 1;
63 while (argc < MAX_RUN_COMMAND_ARGS) {
64 arg = argv[argc++] = va_arg(param, char *);
65 if (!arg)
66 break;
67 }
68 if (MAX_RUN_COMMAND_ARGS <= argc)
69 return error("too many args to run %s", cmd);
70 return run_command_v_opt(argv, opt);
71 }
73 int run_command_opt(int opt, const char *cmd, ...)
74 {
75 va_list params;
76 int r;
78 va_start(params, cmd);
79 r = run_command_va_opt(opt, cmd, params);
80 va_end(params);
81 return r;
82 }
84 int run_command(const char *cmd, ...)
85 {
86 va_list params;
87 int r;
89 va_start(params, cmd);
90 r = run_command_va_opt(0, cmd, params);
91 va_end(params);
92 return r;
93 }