Add infrastructure to run a function asynchronously.
[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
9dc09c76
SP
5static inline void close_pair(int fd[2])
6{
7 close(fd[0]);
8 close(fd[1]);
9}
10
e4507ae8
SP
11static inline void dup_devnull(int to)
12{
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, to);
15 close(fd);
16}
17
ebcb5d16 18int start_command(struct child_process *cmd)
b1bf95bb 19{
f3b33f1d
JS
20 int need_in, need_out, need_err;
21 int fdin[2], fdout[2], fderr[2];
4919bf03 22
e4507ae8 23 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03
SP
24 if (need_in) {
25 if (pipe(fdin) < 0)
26 return -ERR_RUN_COMMAND_PIPE;
27 cmd->in = fdin[1];
28 cmd->close_in = 1;
29 }
30
e4507ae8
SP
31 need_out = !cmd->no_stdout
32 && !cmd->stdout_to_stderr
33 && cmd->out < 0;
f4bba25b
SP
34 if (need_out) {
35 if (pipe(fdout) < 0) {
36 if (need_in)
37 close_pair(fdin);
38 return -ERR_RUN_COMMAND_PIPE;
39 }
40 cmd->out = fdout[0];
41 cmd->close_out = 1;
42 }
43
f3b33f1d
JS
44 need_err = cmd->err < 0;
45 if (need_err) {
46 if (pipe(fderr) < 0) {
47 if (need_in)
48 close_pair(fdin);
49 if (need_out)
50 close_pair(fdout);
51 return -ERR_RUN_COMMAND_PIPE;
52 }
53 cmd->err = fderr[0];
54 }
55
ebcb5d16 56 cmd->pid = fork();
4919bf03 57 if (cmd->pid < 0) {
9dc09c76
SP
58 if (need_in)
59 close_pair(fdin);
f4bba25b
SP
60 if (need_out)
61 close_pair(fdout);
f3b33f1d
JS
62 if (need_err)
63 close_pair(fderr);
b1bf95bb 64 return -ERR_RUN_COMMAND_FORK;
4919bf03
SP
65 }
66
ebcb5d16 67 if (!cmd->pid) {
e4507ae8
SP
68 if (cmd->no_stdin)
69 dup_devnull(0);
70 else if (need_in) {
4919bf03 71 dup2(fdin[0], 0);
9dc09c76 72 close_pair(fdin);
4919bf03
SP
73 } else if (cmd->in) {
74 dup2(cmd->in, 0);
75 close(cmd->in);
95d3c4f5 76 }
4919bf03 77
e4507ae8
SP
78 if (cmd->no_stdout)
79 dup_devnull(1);
80 else if (cmd->stdout_to_stderr)
cd83c74c 81 dup2(2, 1);
f4bba25b
SP
82 else if (need_out) {
83 dup2(fdout[1], 1);
84 close_pair(fdout);
85 } else if (cmd->out > 1) {
86 dup2(cmd->out, 1);
87 close(cmd->out);
88 }
89
f3b33f1d
JS
90 if (need_err) {
91 dup2(fderr[1], 2);
92 close_pair(fderr);
93 }
94
1568fea0
AR
95 if (cmd->dir && chdir(cmd->dir))
96 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
97 cmd->dir, strerror(errno));
ee493148 98 if (cmd->env) {
3427b375
AR
99 for (; *cmd->env; cmd->env++) {
100 if (strchr(*cmd->env, '='))
101 putenv((char*)*cmd->env);
102 else
103 unsetenv(*cmd->env);
104 }
ee493148 105 }
f1000898
SP
106 if (cmd->git_cmd) {
107 execv_git_cmd(cmd->argv);
77cb17e9 108 } else {
f1000898 109 execvp(cmd->argv[0], (char *const*) cmd->argv);
128aed68 110 }
f1000898 111 die("exec %s failed.", cmd->argv[0]);
b1bf95bb 112 }
4919bf03
SP
113
114 if (need_in)
115 close(fdin[0]);
116 else if (cmd->in)
117 close(cmd->in);
118
f4bba25b
SP
119 if (need_out)
120 close(fdout[1]);
121 else if (cmd->out > 1)
122 close(cmd->out);
123
f3b33f1d
JS
124 if (need_err)
125 close(fderr[1]);
126
ebcb5d16
SP
127 return 0;
128}
129
2d22c208 130static int wait_or_whine(pid_t pid)
ebcb5d16 131{
b1bf95bb
JW
132 for (;;) {
133 int status, code;
2d22c208 134 pid_t waiting = waitpid(pid, &status, 0);
b1bf95bb 135
6f002f98 136 if (waiting < 0) {
b1bf95bb
JW
137 if (errno == EINTR)
138 continue;
6f002f98 139 error("waitpid failed (%s)", strerror(errno));
b1bf95bb
JW
140 return -ERR_RUN_COMMAND_WAITPID;
141 }
2d22c208 142 if (waiting != pid)
b1bf95bb
JW
143 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
144 if (WIFSIGNALED(status))
145 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
146
147 if (!WIFEXITED(status))
148 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
149 code = WEXITSTATUS(status);
150 if (code)
151 return -code;
152 return 0;
153 }
154}
f1000898 155
2d22c208
JS
156int finish_command(struct child_process *cmd)
157{
158 if (cmd->close_in)
159 close(cmd->in);
160 if (cmd->close_out)
161 close(cmd->out);
162 return wait_or_whine(cmd->pid);
163}
164
ebcb5d16
SP
165int run_command(struct child_process *cmd)
166{
167 int code = start_command(cmd);
168 if (code)
169 return code;
170 return finish_command(cmd);
171}
172
1568fea0 173static void prepare_run_command_v_opt(struct child_process *cmd,
ee493148
AR
174 const char **argv,
175 int opt)
1568fea0
AR
176{
177 memset(cmd, 0, sizeof(*cmd));
178 cmd->argv = argv;
179 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
180 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
181 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
182}
183
f1000898
SP
184int run_command_v_opt(const char **argv, int opt)
185{
186 struct child_process cmd;
1568fea0
AR
187 prepare_run_command_v_opt(&cmd, argv, opt);
188 return run_command(&cmd);
189}
190
191int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
192{
193 struct child_process cmd;
194 prepare_run_command_v_opt(&cmd, argv, opt);
195 cmd.dir = dir;
f1000898
SP
196 return run_command(&cmd);
197}
ee493148
AR
198
199int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
200{
201 struct child_process cmd;
202 prepare_run_command_v_opt(&cmd, argv, opt);
203 cmd.dir = dir;
204 cmd.env = env;
205 return run_command(&cmd);
206}
2d22c208
JS
207
208int start_async(struct async *async)
209{
210 int pipe_out[2];
211
212 if (pipe(pipe_out) < 0)
213 return error("cannot create pipe: %s", strerror(errno));
214
215 async->pid = fork();
216 if (async->pid < 0) {
217 error("fork (async) failed: %s", strerror(errno));
218 close_pair(pipe_out);
219 return -1;
220 }
221 if (!async->pid) {
222 close(pipe_out[0]);
223 exit(!!async->proc(pipe_out[1], async->data));
224 }
225 async->out = pipe_out[0];
226 close(pipe_out[1]);
227 return 0;
228}
229
230int finish_async(struct async *async)
231{
232 int ret = 0;
233
234 if (wait_or_whine(async->pid))
235 ret = error("waitpid (async) failed");
236 return ret;
237}