🌐 AI搜索 & 代理 主页
blob: a2796c4caecc0fae6ea1a95b2c965b92d98f8a31 [file] [log] [blame]
Josef Weidendorferb1bf95b2005-07-31 19:17:431#include "cache.h"
2#include "run-command.h"
Michal Ostrowski77cb17e2006-01-11 02:12:173#include "exec_cmd.h"
Josef Weidendorferb1bf95b2005-07-31 19:17:434
Shawn O. Pearce9dc09c72007-03-12 18:37:285static inline void close_pair(int fd[2])
6{
7 close(fd[0]);
8 close(fd[1]);
9}
10
Johannes Sixt75301f92010-01-15 20:12:1811#ifndef WIN32
Shawn O. Pearcee4507ae2007-03-12 18:37:5512static inline void dup_devnull(int to)
13{
14 int fd = open("/dev/null", O_RDWR);
15 dup2(fd, to);
16 close(fd);
17}
Johannes Sixt75301f92010-01-15 20:12:1818#endif
Shawn O. Pearcee4507ae2007-03-12 18:37:5519
Jeff King8dba1e62009-12-30 10:53:1620static const char **prepare_shell_cmd(const char **argv)
21{
22 int argc, nargc = 0;
23 const char **nargv;
24
25 for (argc = 0; argv[argc]; argc++)
26 ; /* just counting */
27 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
28 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
29
30 if (argc < 1)
31 die("BUG: shell command is empty");
32
Jeff Kingf4456442009-12-30 10:55:3633 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
34 nargv[nargc++] = "sh";
35 nargv[nargc++] = "-c";
Jeff King8dba1e62009-12-30 10:53:1636
Jeff Kingf4456442009-12-30 10:55:3637 if (argc < 2)
38 nargv[nargc++] = argv[0];
39 else {
40 struct strbuf arg0 = STRBUF_INIT;
41 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
42 nargv[nargc++] = strbuf_detach(&arg0, NULL);
43 }
Jeff King8dba1e62009-12-30 10:53:1644 }
45
46 for (argc = 0; argv[argc]; argc++)
47 nargv[nargc++] = argv[argc];
48 nargv[nargc] = NULL;
49
50 return nargv;
51}
52
53#ifndef WIN32
54static int execv_shell_cmd(const char **argv)
55{
56 const char **nargv = prepare_shell_cmd(argv);
57 trace_argv_printf(nargv, "trace: exec:");
58 execvp(nargv[0], (char **)nargv);
59 free(nargv);
60 return -1;
61}
62#endif
63
Johannes Sixta5487dd2010-01-10 13:07:5264#ifndef WIN32
65static int child_err = 2;
Johannes Sixt2b541bf2010-01-10 13:11:2266static int child_notifier = -1;
67
68static void notify_parent(void)
69{
Jonathan Niedera111eb72011-04-20 10:40:0570 /*
71 * execvp failed. If possible, we'd like to let start_command
72 * know, so failures like ENOENT can be handled right away; but
73 * otherwise, finish_command will still report the error.
74 */
75 xwrite(child_notifier, "", 1);
Johannes Sixt2b541bf2010-01-10 13:11:2276}
Johannes Sixta5487dd2010-01-10 13:07:5277
78static NORETURN void die_child(const char *err, va_list params)
79{
Clemens Buchacher3bc41812011-07-27 21:32:3480 vwritef(child_err, "fatal: ", err, params);
Johannes Sixta5487dd2010-01-10 13:07:5281 exit(128);
82}
Clemens Buchacher3bc41812011-07-27 21:32:3483
84static void error_child(const char *err, va_list params)
85{
86 vwritef(child_err, "error: ", err, params);
87}
Johannes Sixt200a76b2010-03-06 15:40:4288#endif
Johannes Sixta5487dd2010-01-10 13:07:5289
90static inline void set_cloexec(int fd)
91{
92 int flags = fcntl(fd, F_GETFD);
93 if (flags >= 0)
94 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
95}
Johannes Sixta5487dd2010-01-10 13:07:5296
Johannes Sixtab0b41d2010-01-10 13:08:4597static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
98{
99 int status, code = -1;
100 pid_t waiting;
101 int failed_errno = 0;
102
103 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
104 ; /* nothing */
105
106 if (waiting < 0) {
107 failed_errno = errno;
108 error("waitpid for %s failed: %s", argv0, strerror(errno));
109 } else if (waiting != pid) {
110 error("waitpid is confused (%s)", argv0);
111 } else if (WIFSIGNALED(status)) {
112 code = WTERMSIG(status);
113 error("%s died of signal %d", argv0, code);
114 /*
115 * This return value is chosen so that code & 0xff
116 * mimics the exit code that a POSIX shell would report for
117 * a program that died from this signal.
118 */
119 code -= 128;
120 } else if (WIFEXITED(status)) {
121 code = WEXITSTATUS(status);
122 /*
123 * Convert special exit code when execvp failed.
124 */
125 if (code == 127) {
126 code = -1;
127 failed_errno = ENOENT;
Johannes Sixtab0b41d2010-01-10 13:08:45128 }
129 } else {
130 error("waitpid is confused (%s)", argv0);
131 }
132 errno = failed_errno;
133 return code;
134}
135
Shawn O. Pearceebcb5d12007-03-10 08:28:05136int start_command(struct child_process *cmd)
Josef Weidendorferb1bf95b2005-07-31 19:17:43137{
Johannes Sixtf3b33f12007-10-19 19:47:58138 int need_in, need_out, need_err;
139 int fdin[2], fdout[2], fderr[2];
David Soria Parra5a7a3672009-08-04 09:28:40140 int failed_errno = failed_errno;
Shawn O. Pearce4919bf02007-03-10 08:28:08141
Johannes Sixtc20181e2008-02-21 22:42:56142 /*
143 * In case of errors we must keep the promise to close FDs
144 * that have been passed in via ->in and ->out.
145 */
146
Shawn O. Pearcee4507ae2007-03-12 18:37:55147 need_in = !cmd->no_stdin && cmd->in < 0;
Shawn O. Pearce4919bf02007-03-10 08:28:08148 if (need_in) {
Johannes Sixtc20181e2008-02-21 22:42:56149 if (pipe(fdin) < 0) {
Johannes Sixt0ac77ec2009-07-04 19:26:40150 failed_errno = errno;
Johannes Sixtc20181e2008-02-21 22:42:56151 if (cmd->out > 0)
152 close(cmd->out);
Johannes Sixt0ac77ec2009-07-04 19:26:40153 goto fail_pipe;
Johannes Sixtc20181e2008-02-21 22:42:56154 }
Shawn O. Pearce4919bf02007-03-10 08:28:08155 cmd->in = fdin[1];
Shawn O. Pearce4919bf02007-03-10 08:28:08156 }
157
Shawn O. Pearcee4507ae2007-03-12 18:37:55158 need_out = !cmd->no_stdout
159 && !cmd->stdout_to_stderr
160 && cmd->out < 0;
Shawn O. Pearcef4bba252007-03-12 18:37:45161 if (need_out) {
162 if (pipe(fdout) < 0) {
Johannes Sixt0ac77ec2009-07-04 19:26:40163 failed_errno = errno;
Shawn O. Pearcef4bba252007-03-12 18:37:45164 if (need_in)
165 close_pair(fdin);
Johannes Sixtc20181e2008-02-21 22:42:56166 else if (cmd->in)
167 close(cmd->in);
Johannes Sixt0ac77ec2009-07-04 19:26:40168 goto fail_pipe;
Shawn O. Pearcef4bba252007-03-12 18:37:45169 }
170 cmd->out = fdout[0];
Shawn O. Pearcef4bba252007-03-12 18:37:45171 }
172
Shawn O. Pearceb73a4392007-11-11 07:29:37173 need_err = !cmd->no_stderr && cmd->err < 0;
Johannes Sixtf3b33f12007-10-19 19:47:58174 if (need_err) {
175 if (pipe(fderr) < 0) {
Johannes Sixt0ac77ec2009-07-04 19:26:40176 failed_errno = errno;
Johannes Sixtf3b33f12007-10-19 19:47:58177 if (need_in)
178 close_pair(fdin);
Johannes Sixtc20181e2008-02-21 22:42:56179 else if (cmd->in)
180 close(cmd->in);
Johannes Sixtf3b33f12007-10-19 19:47:58181 if (need_out)
182 close_pair(fdout);
Johannes Sixtc20181e2008-02-21 22:42:56183 else if (cmd->out)
184 close(cmd->out);
Johannes Sixt0ac77ec2009-07-04 19:26:40185fail_pipe:
186 error("cannot create pipe for %s: %s",
187 cmd->argv[0], strerror(failed_errno));
188 errno = failed_errno;
189 return -1;
Johannes Sixtf3b33f12007-10-19 19:47:58190 }
191 cmd->err = fderr[0];
192 }
193
Johannes Schindelin8852f5d2008-07-07 13:41:34194 trace_argv_printf(cmd->argv, "trace: run_command:");
Johannes Sixt13af8cb2011-02-04 08:41:58195 fflush(NULL);
Johannes Schindelin8852f5d2008-07-07 13:41:34196
Frank Li71064e32009-09-16 08:20:22197#ifndef WIN32
Johannes Sixt2b541bf2010-01-10 13:11:22198{
199 int notify_pipe[2];
200 if (pipe(notify_pipe))
201 notify_pipe[0] = notify_pipe[1] = -1;
202
Shawn O. Pearceebcb5d12007-03-10 08:28:05203 cmd->pid = fork();
Shawn O. Pearceebcb5d12007-03-10 08:28:05204 if (!cmd->pid) {
Johannes Sixta5487dd2010-01-10 13:07:52205 /*
206 * Redirect the channel to write syscall error messages to
207 * before redirecting the process's stderr so that all die()
208 * in subsequent call paths use the parent's stderr.
209 */
210 if (cmd->no_stderr || need_err) {
211 child_err = dup(2);
212 set_cloexec(child_err);
213 }
214 set_die_routine(die_child);
Clemens Buchacher3bc41812011-07-27 21:32:34215 set_error_routine(error_child);
Johannes Sixta5487dd2010-01-10 13:07:52216
Johannes Sixt2b541bf2010-01-10 13:11:22217 close(notify_pipe[0]);
218 set_cloexec(notify_pipe[1]);
219 child_notifier = notify_pipe[1];
220 atexit(notify_parent);
221
Shawn O. Pearcee4507ae2007-03-12 18:37:55222 if (cmd->no_stdin)
223 dup_devnull(0);
224 else if (need_in) {
Shawn O. Pearce4919bf02007-03-10 08:28:08225 dup2(fdin[0], 0);
Shawn O. Pearce9dc09c72007-03-12 18:37:28226 close_pair(fdin);
Shawn O. Pearce4919bf02007-03-10 08:28:08227 } else if (cmd->in) {
228 dup2(cmd->in, 0);
229 close(cmd->in);
Shawn O. Pearce95d3c4f2006-12-31 02:55:22230 }
Shawn O. Pearce4919bf02007-03-10 08:28:08231
Christian Couderce2cf272008-03-05 07:35:16232 if (cmd->no_stderr)
233 dup_devnull(2);
234 else if (need_err) {
235 dup2(fderr[1], 2);
236 close_pair(fderr);
Shawn O. Pearce4f41b612010-02-05 20:57:37237 } else if (cmd->err > 1) {
238 dup2(cmd->err, 2);
239 close(cmd->err);
Christian Couderce2cf272008-03-05 07:35:16240 }
241
Shawn O. Pearcee4507ae2007-03-12 18:37:55242 if (cmd->no_stdout)
243 dup_devnull(1);
244 else if (cmd->stdout_to_stderr)
Shawn O. Pearcecd83c742006-12-31 02:55:19245 dup2(2, 1);
Shawn O. Pearcef4bba252007-03-12 18:37:45246 else if (need_out) {
247 dup2(fdout[1], 1);
248 close_pair(fdout);
249 } else if (cmd->out > 1) {
250 dup2(cmd->out, 1);
251 close(cmd->out);
252 }
253
Alex Riesen1568fea2007-05-22 21:48:23254 if (cmd->dir && chdir(cmd->dir))
Thomas Rastd824cbb2009-06-27 15:58:46255 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
256 cmd->dir);
Alex Riesenee493142007-05-22 21:48:47257 if (cmd->env) {
Alex Riesen3427b372007-05-23 20:21:39258 for (; *cmd->env; cmd->env++) {
259 if (strchr(*cmd->env, '='))
Felipe Contreras4b25d092009-05-01 09:06:36260 putenv((char *)*cmd->env);
Alex Riesen3427b372007-05-23 20:21:39261 else
262 unsetenv(*cmd->env);
263 }
Alex Riesenee493142007-05-22 21:48:47264 }
Johannes Sixt2b541bf2010-01-10 13:11:22265 if (cmd->preexec_cb) {
266 /*
267 * We cannot predict what the pre-exec callback does.
268 * Forgo parent notification.
269 */
270 close(child_notifier);
271 child_notifier = -1;
272
Jeff Kingccf08bc2008-07-22 07:12:46273 cmd->preexec_cb();
Johannes Sixt2b541bf2010-01-10 13:11:22274 }
Shawn O. Pearcef1000892007-03-10 08:28:00275 if (cmd->git_cmd) {
276 execv_git_cmd(cmd->argv);
Jeff King8dba1e62009-12-30 10:53:16277 } else if (cmd->use_shell) {
278 execv_shell_cmd(cmd->argv);
Michal Ostrowski77cb17e2006-01-11 02:12:17279 } else {
Shawn O. Pearcef1000892007-03-10 08:28:00280 execvp(cmd->argv[0], (char *const*) cmd->argv);
Michal Ostrowski77cb17e2006-01-11 02:12:17281 }
Clemens Buchacherfc1b56f2011-08-01 17:59:21282 if (errno == ENOENT) {
283 if (!cmd->silent_exec_failure)
284 error("cannot run %s: %s", cmd->argv[0],
285 strerror(ENOENT));
Johannes Sixta5487dd2010-01-10 13:07:52286 exit(127);
Clemens Buchacherfc1b56f2011-08-01 17:59:21287 } else {
Johannes Sixta5487dd2010-01-10 13:07:52288 die_errno("cannot exec '%s'", cmd->argv[0]);
Clemens Buchacherfc1b56f2011-08-01 17:59:21289 }
Josef Weidendorferb1bf95b2005-07-31 19:17:43290 }
Johannes Sixt0ac77ec2009-07-04 19:26:40291 if (cmd->pid < 0)
292 error("cannot fork() for %s: %s", cmd->argv[0],
293 strerror(failed_errno = errno));
Johannes Sixt2b541bf2010-01-10 13:11:22294
295 /*
296 * Wait for child's execvp. If the execvp succeeds (or if fork()
297 * failed), EOF is seen immediately by the parent. Otherwise, the
298 * child process sends a single byte.
299 * Note that use of this infrastructure is completely advisory,
300 * therefore, we keep error checks minimal.
301 */
302 close(notify_pipe[1]);
303 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
304 /*
305 * At this point we know that fork() succeeded, but execvp()
306 * failed. Errors have been reported to our stderr.
307 */
308 wait_or_whine(cmd->pid, cmd->argv[0],
309 cmd->silent_exec_failure);
310 failed_errno = errno;
311 cmd->pid = -1;
312 }
313 close(notify_pipe[0]);
314}
Johannes Sixtba26f292007-12-07 21:08:59315#else
Frank Li0d30ad72009-09-16 08:20:17316{
Johannes Sixt75301f92010-01-15 20:12:18317 int fhin = 0, fhout = 1, fherr = 2;
Steffen Prohaska108ac312008-07-28 05:50:28318 const char **sargv = cmd->argv;
Johannes Sixtba26f292007-12-07 21:08:59319 char **env = environ;
Johannes Sixtba26f292007-12-07 21:08:59320
Johannes Sixt75301f92010-01-15 20:12:18321 if (cmd->no_stdin)
322 fhin = open("/dev/null", O_RDWR);
323 else if (need_in)
324 fhin = dup(fdin[0]);
325 else if (cmd->in)
326 fhin = dup(cmd->in);
Johannes Sixtba26f292007-12-07 21:08:59327
Johannes Sixt75301f92010-01-15 20:12:18328 if (cmd->no_stderr)
329 fherr = open("/dev/null", O_RDWR);
330 else if (need_err)
331 fherr = dup(fderr[1]);
Junio C Hamano76d44c82010-02-06 05:08:53332 else if (cmd->err > 2)
333 fherr = dup(cmd->err);
Johannes Sixtba26f292007-12-07 21:08:59334
Johannes Sixt75301f92010-01-15 20:12:18335 if (cmd->no_stdout)
336 fhout = open("/dev/null", O_RDWR);
337 else if (cmd->stdout_to_stderr)
338 fhout = dup(fherr);
339 else if (need_out)
340 fhout = dup(fdout[1]);
341 else if (cmd->out > 1)
342 fhout = dup(cmd->out);
Johannes Sixtba26f292007-12-07 21:08:59343
Johannes Sixt2affea42009-09-11 17:40:08344 if (cmd->env)
345 env = make_augmented_environ(cmd->env);
Johannes Sixtba26f292007-12-07 21:08:59346
347 if (cmd->git_cmd) {
Steffen Prohaska108ac312008-07-28 05:50:28348 cmd->argv = prepare_git_cmd(cmd->argv);
Jeff King8dba1e62009-12-30 10:53:16349 } else if (cmd->use_shell) {
350 cmd->argv = prepare_shell_cmd(cmd->argv);
Johannes Sixtba26f292007-12-07 21:08:59351 }
352
Johannes Sixtf9a27432010-04-11 20:40:12353 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir,
Johannes Sixt75301f92010-01-15 20:12:18354 fhin, fhout, fherr);
Johannes Sixt0ac77ec2009-07-04 19:26:40355 failed_errno = errno;
Johannes Sixtc024beb2009-07-04 19:26:42356 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
Johannes Sixt0ac77ec2009-07-04 19:26:40357 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
Johannes Sixtba26f292007-12-07 21:08:59358
359 if (cmd->env)
360 free_environ(env);
361 if (cmd->git_cmd)
Steffen Prohaska108ac312008-07-28 05:50:28362 free(cmd->argv);
Johannes Sixtba26f292007-12-07 21:08:59363
Steffen Prohaska108ac312008-07-28 05:50:28364 cmd->argv = sargv;
Johannes Sixt75301f92010-01-15 20:12:18365 if (fhin != 0)
366 close(fhin);
367 if (fhout != 1)
368 close(fhout);
369 if (fherr != 2)
370 close(fherr);
Frank Li0d30ad72009-09-16 08:20:17371}
Johannes Sixtba26f292007-12-07 21:08:59372#endif
373
374 if (cmd->pid < 0) {
Johannes Sixtba26f292007-12-07 21:08:59375 if (need_in)
376 close_pair(fdin);
377 else if (cmd->in)
378 close(cmd->in);
379 if (need_out)
380 close_pair(fdout);
381 else if (cmd->out)
382 close(cmd->out);
383 if (need_err)
384 close_pair(fderr);
bert Dvornikfc012c22010-05-20 18:57:52385 else if (cmd->err)
386 close(cmd->err);
Johannes Sixt0ac77ec2009-07-04 19:26:40387 errno = failed_errno;
388 return -1;
Johannes Sixtba26f292007-12-07 21:08:59389 }
Shawn O. Pearce4919bf02007-03-10 08:28:08390
391 if (need_in)
392 close(fdin[0]);
393 else if (cmd->in)
394 close(cmd->in);
395
Shawn O. Pearcef4bba252007-03-12 18:37:45396 if (need_out)
397 close(fdout[1]);
Johannes Sixtc20181e2008-02-21 22:42:56398 else if (cmd->out)
Shawn O. Pearcef4bba252007-03-12 18:37:45399 close(cmd->out);
400
Johannes Sixtf3b33f12007-10-19 19:47:58401 if (need_err)
402 close(fderr[1]);
Shawn O. Pearce4f41b612010-02-05 20:57:37403 else if (cmd->err)
404 close(cmd->err);
Johannes Sixtf3b33f12007-10-19 19:47:58405
Shawn O. Pearceebcb5d12007-03-10 08:28:05406 return 0;
407}
408
Johannes Sixt2d22c202007-10-19 19:48:00409int finish_command(struct child_process *cmd)
410{
Johannes Sixtc024beb2009-07-04 19:26:42411 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
Johannes Sixt2d22c202007-10-19 19:48:00412}
413
Shawn O. Pearceebcb5d12007-03-10 08:28:05414int run_command(struct child_process *cmd)
415{
416 int code = start_command(cmd);
417 if (code)
418 return code;
419 return finish_command(cmd);
420}
421
Alex Riesen1568fea2007-05-22 21:48:23422static void prepare_run_command_v_opt(struct child_process *cmd,
Alex Riesenee493142007-05-22 21:48:47423 const char **argv,
424 int opt)
Alex Riesen1568fea2007-05-22 21:48:23425{
426 memset(cmd, 0, sizeof(*cmd));
427 cmd->argv = argv;
428 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
429 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
430 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
Johannes Sixtc024beb2009-07-04 19:26:42431 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
Jeff King8dba1e62009-12-30 10:53:16432 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
Alex Riesen1568fea2007-05-22 21:48:23433}
434
Shawn O. Pearcef1000892007-03-10 08:28:00435int run_command_v_opt(const char **argv, int opt)
436{
437 struct child_process cmd;
Alex Riesen1568fea2007-05-22 21:48:23438 prepare_run_command_v_opt(&cmd, argv, opt);
439 return run_command(&cmd);
440}
441
Alex Riesenee493142007-05-22 21:48:47442int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
443{
444 struct child_process cmd;
445 prepare_run_command_v_opt(&cmd, argv, opt);
446 cmd.dir = dir;
447 cmd.env = env;
448 return run_command(&cmd);
449}
Johannes Sixt2d22c202007-10-19 19:48:00450
Johannes Sixtf6b60982010-03-09 20:00:36451#ifndef NO_PTHREADS
Johannes Sixt0ea1c892010-03-06 15:40:43452static pthread_t main_thread;
453static int main_thread_set;
454static pthread_key_t async_key;
455
Johannes Sixt200a76b2010-03-06 15:40:42456static void *run_thread(void *data)
Johannes Sixt618ebe92007-12-08 21:19:14457{
458 struct async *async = data;
Johannes Sixtf6b60982010-03-09 20:00:36459 intptr_t ret;
Johannes Sixt0ea1c892010-03-06 15:40:43460
461 pthread_setspecific(async_key, async);
Johannes Sixtf6b60982010-03-09 20:00:36462 ret = async->proc(async->proc_in, async->proc_out, async->data);
Johannes Sixt200a76b2010-03-06 15:40:42463 return (void *)ret;
Johannes Sixt618ebe92007-12-08 21:19:14464}
Johannes Sixt0ea1c892010-03-06 15:40:43465
466static NORETURN void die_async(const char *err, va_list params)
467{
468 vreportf("fatal: ", err, params);
469
470 if (!pthread_equal(main_thread, pthread_self())) {
471 struct async *async = pthread_getspecific(async_key);
472 if (async->proc_in >= 0)
473 close(async->proc_in);
474 if (async->proc_out >= 0)
475 close(async->proc_out);
476 pthread_exit((void *)128);
477 }
478
479 exit(128);
Johannes Sixt618ebe92007-12-08 21:19:14480}
481#endif
482
Johannes Sixt2d22c202007-10-19 19:48:00483int start_async(struct async *async)
484{
Erik Faye-Lundae6a5602010-02-05 20:57:38485 int need_in, need_out;
486 int fdin[2], fdout[2];
487 int proc_in, proc_out;
Johannes Sixt2d22c202007-10-19 19:48:00488
Erik Faye-Lundae6a5602010-02-05 20:57:38489 need_in = async->in < 0;
490 if (need_in) {
491 if (pipe(fdin) < 0) {
492 if (async->out > 0)
493 close(async->out);
494 return error("cannot create pipe: %s", strerror(errno));
495 }
496 async->in = fdin[1];
497 }
498
499 need_out = async->out < 0;
500 if (need_out) {
501 if (pipe(fdout) < 0) {
502 if (need_in)
503 close_pair(fdin);
504 else if (async->in)
505 close(async->in);
506 return error("cannot create pipe: %s", strerror(errno));
507 }
508 async->out = fdout[0];
509 }
510
511 if (need_in)
512 proc_in = fdin[0];
513 else if (async->in)
514 proc_in = async->in;
515 else
516 proc_in = -1;
517
518 if (need_out)
519 proc_out = fdout[1];
520 else if (async->out)
521 proc_out = async->out;
522 else
523 proc_out = -1;
Johannes Sixt2d22c202007-10-19 19:48:00524
Johannes Sixtf6b60982010-03-09 20:00:36525#ifdef NO_PTHREADS
Anders Melchiorsen2c3766f2008-08-04 00:30:03526 /* Flush stdio before fork() to avoid cloning buffers */
527 fflush(NULL);
528
Johannes Sixt2d22c202007-10-19 19:48:00529 async->pid = fork();
530 if (async->pid < 0) {
531 error("fork (async) failed: %s", strerror(errno));
Erik Faye-Lundae6a5602010-02-05 20:57:38532 goto error;
Johannes Sixt2d22c202007-10-19 19:48:00533 }
534 if (!async->pid) {
Erik Faye-Lundae6a5602010-02-05 20:57:38535 if (need_in)
536 close(fdin[1]);
537 if (need_out)
538 close(fdout[0]);
539 exit(!!async->proc(proc_in, proc_out, async->data));
Johannes Sixt2d22c202007-10-19 19:48:00540 }
Erik Faye-Lundae6a5602010-02-05 20:57:38541
542 if (need_in)
543 close(fdin[0]);
544 else if (async->in)
545 close(async->in);
546
547 if (need_out)
548 close(fdout[1]);
549 else if (async->out)
550 close(async->out);
Johannes Sixt618ebe92007-12-08 21:19:14551#else
Johannes Sixt0ea1c892010-03-06 15:40:43552 if (!main_thread_set) {
553 /*
554 * We assume that the first time that start_async is called
555 * it is from the main thread.
556 */
557 main_thread_set = 1;
558 main_thread = pthread_self();
559 pthread_key_create(&async_key, NULL);
560 set_die_routine(die_async);
561 }
562
Johannes Sixt200a76b2010-03-06 15:40:42563 if (proc_in >= 0)
564 set_cloexec(proc_in);
565 if (proc_out >= 0)
566 set_cloexec(proc_out);
Erik Faye-Lundae6a5602010-02-05 20:57:38567 async->proc_in = proc_in;
568 async->proc_out = proc_out;
Johannes Sixt200a76b2010-03-06 15:40:42569 {
570 int err = pthread_create(&async->tid, NULL, run_thread, async);
571 if (err) {
572 error("cannot create thread: %s", strerror(err));
573 goto error;
574 }
Johannes Sixt618ebe92007-12-08 21:19:14575 }
576#endif
Johannes Sixt2d22c202007-10-19 19:48:00577 return 0;
Erik Faye-Lundae6a5602010-02-05 20:57:38578
579error:
580 if (need_in)
581 close_pair(fdin);
582 else if (async->in)
583 close(async->in);
584
585 if (need_out)
586 close_pair(fdout);
587 else if (async->out)
588 close(async->out);
589 return -1;
Johannes Sixt2d22c202007-10-19 19:48:00590}
591
592int finish_async(struct async *async)
593{
Johannes Sixtf6b60982010-03-09 20:00:36594#ifdef NO_PTHREADS
Johannes Sixt200a76b2010-03-06 15:40:42595 return wait_or_whine(async->pid, "child process", 0);
Johannes Sixt618ebe92007-12-08 21:19:14596#else
Johannes Sixt200a76b2010-03-06 15:40:42597 void *ret = (void *)(intptr_t)(-1);
598
599 if (pthread_join(async->tid, &ret))
600 error("pthread_join failed");
601 return (int)(intptr_t)ret;
Johannes Sixt618ebe92007-12-08 21:19:14602#endif
Johannes Sixt2d22c202007-10-19 19:48:00603}
Stephan Beyerae98a002009-01-16 19:09:59604
605int run_hook(const char *index_file, const char *name, ...)
606{
607 struct child_process hook;
Stephan Beyer14e62982009-01-17 03:02:55608 const char **argv = NULL, *env[2];
Stephan Beyerae98a002009-01-16 19:09:59609 char index[PATH_MAX];
610 va_list args;
611 int ret;
Stephan Beyer14e62982009-01-17 03:02:55612 size_t i = 0, alloc = 0;
Stephan Beyerae98a002009-01-16 19:09:59613
Stephan Beyercf94ca82009-01-16 19:10:01614 if (access(git_path("hooks/%s", name), X_OK) < 0)
615 return 0;
616
Stephan Beyerae98a002009-01-16 19:09:59617 va_start(args, name);
Stephan Beyer14e62982009-01-17 03:02:55618 ALLOC_GROW(argv, i + 1, alloc);
619 argv[i++] = git_path("hooks/%s", name);
620 while (argv[i-1]) {
621 ALLOC_GROW(argv, i + 1, alloc);
622 argv[i++] = va_arg(args, const char *);
623 }
Stephan Beyerae98a002009-01-16 19:09:59624 va_end(args);
625
Stephan Beyerae98a002009-01-16 19:09:59626 memset(&hook, 0, sizeof(hook));
627 hook.argv = argv;
628 hook.no_stdin = 1;
629 hook.stdout_to_stderr = 1;
630 if (index_file) {
631 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
632 env[0] = index;
633 env[1] = NULL;
634 hook.env = env;
635 }
636
Johannes Sixt0ac77ec2009-07-04 19:26:40637 ret = run_command(&hook);
Stephan Beyer14e62982009-01-17 03:02:55638 free(argv);
Stephan Beyerae98a002009-01-16 19:09:59639 return ret;
640}