blob: 4d73e90fad159184bfdd204b82dd8637ad28a955 [file] [log] [blame]
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +02001#include "cache.h"
2#include "run-command.h"
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05003#include "exec_cmd.h"
Jeff Kingafe19ff2012-01-07 12:42:43 +01004#include "sigchain.h"
Jeff King5d40a172011-09-13 17:58:25 -04005#include "argv-array.h"
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +02006
René Scharfe483bbd42014-08-19 21:10:48 +02007void child_process_init(struct child_process *child)
8{
9 memset(child, 0, sizeof(*child));
10 argv_array_init(&child->args);
René Scharfe19a583d2014-10-19 13:13:55 +020011 argv_array_init(&child->env_array);
René Scharfe483bbd42014-08-19 21:10:48 +020012}
13
Jeff Kingafe19ff2012-01-07 12:42:43 +010014struct child_to_clean {
15 pid_t pid;
16 struct child_to_clean *next;
17};
18static struct child_to_clean *children_to_clean;
19static int installed_child_cleanup_handler;
20
21static void cleanup_children(int sig)
22{
23 while (children_to_clean) {
24 struct child_to_clean *p = children_to_clean;
25 children_to_clean = p->next;
26 kill(p->pid, sig);
27 free(p);
28 }
29}
30
31static void cleanup_children_on_signal(int sig)
32{
33 cleanup_children(sig);
34 sigchain_pop(sig);
35 raise(sig);
36}
37
38static void cleanup_children_on_exit(void)
39{
40 cleanup_children(SIGTERM);
41}
42
43static void mark_child_for_cleanup(pid_t pid)
44{
45 struct child_to_clean *p = xmalloc(sizeof(*p));
46 p->pid = pid;
47 p->next = children_to_clean;
48 children_to_clean = p;
49
50 if (!installed_child_cleanup_handler) {
51 atexit(cleanup_children_on_exit);
52 sigchain_push_common(cleanup_children_on_signal);
53 installed_child_cleanup_handler = 1;
54 }
55}
56
57static void clear_child_for_cleanup(pid_t pid)
58{
David Gouldbdee3972012-09-11 15:32:47 +010059 struct child_to_clean **pp;
Jeff Kingafe19ff2012-01-07 12:42:43 +010060
David Gouldbdee3972012-09-11 15:32:47 +010061 for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
62 struct child_to_clean *clean_me = *pp;
63
64 if (clean_me->pid == pid) {
65 *pp = clean_me->next;
66 free(clean_me);
Jeff Kingafe19ff2012-01-07 12:42:43 +010067 return;
68 }
69 }
70}
71
Shawn O. Pearce9dc09c72007-03-12 14:37:28 -040072static inline void close_pair(int fd[2])
73{
74 close(fd[0]);
75 close(fd[1]);
76}
77
Jonathan Nieder380395d2013-05-02 20:26:08 +010078#ifndef GIT_WINDOWS_NATIVE
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -040079static inline void dup_devnull(int to)
80{
81 int fd = open("/dev/null", O_RDWR);
Thomas Rasta77f1062013-07-12 10:58:36 +020082 if (fd < 0)
83 die_errno(_("open /dev/null failed"));
84 if (dup2(fd, to) < 0)
85 die_errno(_("dup2(%d,%d) failed"), fd, to);
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -040086 close(fd);
87}
Johannes Sixt75301f92010-01-15 21:12:18 +010088#endif
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -040089
Jeff King38f865c2012-03-30 03:52:18 -040090static char *locate_in_PATH(const char *file)
91{
92 const char *p = getenv("PATH");
93 struct strbuf buf = STRBUF_INIT;
94
95 if (!p || !*p)
96 return NULL;
97
98 while (1) {
99 const char *end = strchrnul(p, ':');
100
101 strbuf_reset(&buf);
102
103 /* POSIX specifies an empty entry as the current directory. */
104 if (end != p) {
105 strbuf_add(&buf, p, end - p);
106 strbuf_addch(&buf, '/');
107 }
108 strbuf_addstr(&buf, file);
109
110 if (!access(buf.buf, F_OK))
111 return strbuf_detach(&buf, NULL);
112
113 if (!*end)
114 break;
115 p = end + 1;
116 }
117
118 strbuf_release(&buf);
119 return NULL;
120}
121
122static int exists_in_PATH(const char *file)
123{
124 char *r = locate_in_PATH(file);
125 free(r);
126 return r != NULL;
127}
128
129int sane_execvp(const char *file, char * const argv[])
130{
131 if (!execvp(file, argv))
132 return 0; /* cannot happen ;-) */
133
134 /*
135 * When a command can't be found because one of the directories
136 * listed in $PATH is unsearchable, execvp reports EACCES, but
137 * careful usability testing (read: analysis of occasional bug
138 * reports) reveals that "No such file or directory" is more
139 * intuitive.
140 *
141 * We avoid commands with "/", because execvp will not do $PATH
142 * lookups in that case.
143 *
144 * The reassignment of EACCES to errno looks like a no-op below,
145 * but we need to protect against exists_in_PATH overwriting errno.
146 */
147 if (errno == EACCES && !strchr(file, '/'))
148 errno = exists_in_PATH(file) ? EACCES : ENOENT;
Junio C Hamanoa7855082012-07-31 12:51:30 -0700149 else if (errno == ENOTDIR && !strchr(file, '/'))
150 errno = ENOENT;
Jeff King38f865c2012-03-30 03:52:18 -0400151 return -1;
152}
153
Jeff King8dba1e62009-12-30 05:53:16 -0500154static const char **prepare_shell_cmd(const char **argv)
155{
156 int argc, nargc = 0;
157 const char **nargv;
158
159 for (argc = 0; argv[argc]; argc++)
160 ; /* just counting */
161 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
162 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
163
164 if (argc < 1)
165 die("BUG: shell command is empty");
166
Jeff Kingf4456442009-12-30 05:55:36 -0500167 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
Jonathan Nieder380395d2013-05-02 20:26:08 +0100168#ifndef GIT_WINDOWS_NATIVE
Ben Waltonb3e34dd2012-03-30 21:33:21 -0400169 nargv[nargc++] = SHELL_PATH;
Johannes Sixt77629752012-04-17 09:03:21 +0200170#else
171 nargv[nargc++] = "sh";
172#endif
Jeff Kingf4456442009-12-30 05:55:36 -0500173 nargv[nargc++] = "-c";
Jeff King8dba1e62009-12-30 05:53:16 -0500174
Jeff Kingf4456442009-12-30 05:55:36 -0500175 if (argc < 2)
176 nargv[nargc++] = argv[0];
177 else {
178 struct strbuf arg0 = STRBUF_INIT;
179 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
180 nargv[nargc++] = strbuf_detach(&arg0, NULL);
181 }
Jeff King8dba1e62009-12-30 05:53:16 -0500182 }
183
184 for (argc = 0; argv[argc]; argc++)
185 nargv[nargc++] = argv[argc];
186 nargv[nargc] = NULL;
187
188 return nargv;
189}
190
Jonathan Nieder380395d2013-05-02 20:26:08 +0100191#ifndef GIT_WINDOWS_NATIVE
Jeff King8dba1e62009-12-30 05:53:16 -0500192static int execv_shell_cmd(const char **argv)
193{
194 const char **nargv = prepare_shell_cmd(argv);
195 trace_argv_printf(nargv, "trace: exec:");
Jeff King38f865c2012-03-30 03:52:18 -0400196 sane_execvp(nargv[0], (char **)nargv);
Jeff King8dba1e62009-12-30 05:53:16 -0500197 free(nargv);
198 return -1;
199}
200#endif
201
Jonathan Nieder380395d2013-05-02 20:26:08 +0100202#ifndef GIT_WINDOWS_NATIVE
Johannes Sixta5487dd2010-01-10 14:07:52 +0100203static int child_err = 2;
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100204static int child_notifier = -1;
205
206static void notify_parent(void)
207{
Jonathan Niedera111eb72011-04-20 05:40:05 -0500208 /*
209 * execvp failed. If possible, we'd like to let start_command
210 * know, so failures like ENOENT can be handled right away; but
211 * otherwise, finish_command will still report the error.
212 */
213 xwrite(child_notifier, "", 1);
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100214}
Johannes Sixta5487dd2010-01-10 14:07:52 +0100215
216static NORETURN void die_child(const char *err, va_list params)
217{
Clemens Buchacher3bc41812011-07-27 23:32:34 +0200218 vwritef(child_err, "fatal: ", err, params);
Johannes Sixta5487dd2010-01-10 14:07:52 +0100219 exit(128);
220}
Clemens Buchacher3bc41812011-07-27 23:32:34 +0200221
222static void error_child(const char *err, va_list params)
223{
224 vwritef(child_err, "error: ", err, params);
225}
Johannes Sixt200a76b2010-03-06 16:40:42 +0100226#endif
Johannes Sixta5487dd2010-01-10 14:07:52 +0100227
228static inline void set_cloexec(int fd)
229{
230 int flags = fcntl(fd, F_GETFD);
231 if (flags >= 0)
232 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
233}
Johannes Sixta5487dd2010-01-10 14:07:52 +0100234
Jeff King13274522012-11-30 17:40:50 -0500235static int wait_or_whine(pid_t pid, const char *argv0)
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100236{
237 int status, code = -1;
238 pid_t waiting;
239 int failed_errno = 0;
240
241 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
242 ; /* nothing */
243
244 if (waiting < 0) {
245 failed_errno = errno;
246 error("waitpid for %s failed: %s", argv0, strerror(errno));
247 } else if (waiting != pid) {
248 error("waitpid is confused (%s)", argv0);
249 } else if (WIFSIGNALED(status)) {
250 code = WTERMSIG(status);
Jeff Kinga2767c52012-11-30 17:41:38 -0500251 if (code != SIGINT && code != SIGQUIT)
252 error("%s died of signal %d", argv0, code);
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100253 /*
254 * This return value is chosen so that code & 0xff
255 * mimics the exit code that a POSIX shell would report for
256 * a program that died from this signal.
257 */
Jeff King709ca732013-01-05 09:49:49 -0500258 code += 128;
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100259 } else if (WIFEXITED(status)) {
260 code = WEXITSTATUS(status);
261 /*
262 * Convert special exit code when execvp failed.
263 */
264 if (code == 127) {
265 code = -1;
266 failed_errno = ENOENT;
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100267 }
268 } else {
269 error("waitpid is confused (%s)", argv0);
270 }
Jeff Kingafe19ff2012-01-07 12:42:43 +0100271
272 clear_child_for_cleanup(pid);
273
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100274 errno = failed_errno;
275 return code;
276}
277
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500278int start_command(struct child_process *cmd)
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200279{
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200280 int need_in, need_out, need_err;
281 int fdin[2], fdout[2], fderr[2];
Jeff King25043d82013-03-21 11:45:00 -0400282 int failed_errno;
Stephen Boyd939296c2013-01-30 18:01:05 -0800283 char *str;
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500284
Jeff Kingc460c0e2014-05-15 04:33:26 -0400285 if (!cmd->argv)
286 cmd->argv = cmd->args.argv;
René Scharfe19a583d2014-10-19 13:13:55 +0200287 if (!cmd->env)
288 cmd->env = cmd->env_array.argv;
Jeff Kingc460c0e2014-05-15 04:33:26 -0400289
Johannes Sixtc20181e2008-02-21 23:42:56 +0100290 /*
291 * In case of errors we must keep the promise to close FDs
292 * that have been passed in via ->in and ->out.
293 */
294
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -0400295 need_in = !cmd->no_stdin && cmd->in < 0;
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500296 if (need_in) {
Johannes Sixtc20181e2008-02-21 23:42:56 +0100297 if (pipe(fdin) < 0) {
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200298 failed_errno = errno;
Johannes Sixtc20181e2008-02-21 23:42:56 +0100299 if (cmd->out > 0)
300 close(cmd->out);
Stephen Boyd939296c2013-01-30 18:01:05 -0800301 str = "standard input";
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200302 goto fail_pipe;
Johannes Sixtc20181e2008-02-21 23:42:56 +0100303 }
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500304 cmd->in = fdin[1];
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500305 }
306
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -0400307 need_out = !cmd->no_stdout
308 && !cmd->stdout_to_stderr
309 && cmd->out < 0;
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400310 if (need_out) {
311 if (pipe(fdout) < 0) {
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200312 failed_errno = errno;
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400313 if (need_in)
314 close_pair(fdin);
Johannes Sixtc20181e2008-02-21 23:42:56 +0100315 else if (cmd->in)
316 close(cmd->in);
Stephen Boyd939296c2013-01-30 18:01:05 -0800317 str = "standard output";
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200318 goto fail_pipe;
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400319 }
320 cmd->out = fdout[0];
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400321 }
322
Shawn O. Pearceb73a4392007-11-11 02:29:37 -0500323 need_err = !cmd->no_stderr && cmd->err < 0;
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200324 if (need_err) {
325 if (pipe(fderr) < 0) {
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200326 failed_errno = errno;
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200327 if (need_in)
328 close_pair(fdin);
Johannes Sixtc20181e2008-02-21 23:42:56 +0100329 else if (cmd->in)
330 close(cmd->in);
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200331 if (need_out)
332 close_pair(fdout);
Johannes Sixtc20181e2008-02-21 23:42:56 +0100333 else if (cmd->out)
334 close(cmd->out);
Stephen Boyd939296c2013-01-30 18:01:05 -0800335 str = "standard error";
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200336fail_pipe:
Stephen Boyd939296c2013-01-30 18:01:05 -0800337 error("cannot create %s pipe for %s: %s",
338 str, cmd->argv[0], strerror(failed_errno));
Jeff Kingc460c0e2014-05-15 04:33:26 -0400339 argv_array_clear(&cmd->args);
René Scharfe19a583d2014-10-19 13:13:55 +0200340 argv_array_clear(&cmd->env_array);
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200341 errno = failed_errno;
342 return -1;
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200343 }
344 cmd->err = fderr[0];
345 }
346
Johannes Schindelin8852f5d2008-07-07 14:41:34 +0100347 trace_argv_printf(cmd->argv, "trace: run_command:");
Johannes Sixt13af8cb2011-02-04 09:41:58 +0100348 fflush(NULL);
Johannes Schindelin8852f5d2008-07-07 14:41:34 +0100349
Jonathan Nieder380395d2013-05-02 20:26:08 +0100350#ifndef GIT_WINDOWS_NATIVE
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100351{
352 int notify_pipe[2];
353 if (pipe(notify_pipe))
354 notify_pipe[0] = notify_pipe[1] = -1;
355
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500356 cmd->pid = fork();
Jeff King25043d82013-03-21 11:45:00 -0400357 failed_errno = errno;
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500358 if (!cmd->pid) {
Johannes Sixta5487dd2010-01-10 14:07:52 +0100359 /*
360 * Redirect the channel to write syscall error messages to
361 * before redirecting the process's stderr so that all die()
362 * in subsequent call paths use the parent's stderr.
363 */
364 if (cmd->no_stderr || need_err) {
365 child_err = dup(2);
366 set_cloexec(child_err);
367 }
368 set_die_routine(die_child);
Clemens Buchacher3bc41812011-07-27 23:32:34 +0200369 set_error_routine(error_child);
Johannes Sixta5487dd2010-01-10 14:07:52 +0100370
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100371 close(notify_pipe[0]);
372 set_cloexec(notify_pipe[1]);
373 child_notifier = notify_pipe[1];
374 atexit(notify_parent);
375
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -0400376 if (cmd->no_stdin)
377 dup_devnull(0);
378 else if (need_in) {
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500379 dup2(fdin[0], 0);
Shawn O. Pearce9dc09c72007-03-12 14:37:28 -0400380 close_pair(fdin);
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500381 } else if (cmd->in) {
382 dup2(cmd->in, 0);
383 close(cmd->in);
Shawn O. Pearce95d3c4f2006-12-30 21:55:22 -0500384 }
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500385
Christian Couderce2cf272008-03-05 08:35:16 +0100386 if (cmd->no_stderr)
387 dup_devnull(2);
388 else if (need_err) {
389 dup2(fderr[1], 2);
390 close_pair(fderr);
Shawn O. Pearce4f41b612010-02-05 12:57:37 -0800391 } else if (cmd->err > 1) {
392 dup2(cmd->err, 2);
393 close(cmd->err);
Christian Couderce2cf272008-03-05 08:35:16 +0100394 }
395
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -0400396 if (cmd->no_stdout)
397 dup_devnull(1);
398 else if (cmd->stdout_to_stderr)
Shawn O. Pearcecd83c742006-12-30 21:55:19 -0500399 dup2(2, 1);
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400400 else if (need_out) {
401 dup2(fdout[1], 1);
402 close_pair(fdout);
403 } else if (cmd->out > 1) {
404 dup2(cmd->out, 1);
405 close(cmd->out);
406 }
407
Alex Riesen1568fea2007-05-22 23:48:23 +0200408 if (cmd->dir && chdir(cmd->dir))
Thomas Rastd824cbb2009-06-27 17:58:46 +0200409 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
410 cmd->dir);
Alex Riesenee493142007-05-22 23:48:47 +0200411 if (cmd->env) {
Alex Riesen3427b372007-05-23 22:21:39 +0200412 for (; *cmd->env; cmd->env++) {
413 if (strchr(*cmd->env, '='))
Felipe Contreras4b25d092009-05-01 12:06:36 +0300414 putenv((char *)*cmd->env);
Alex Riesen3427b372007-05-23 22:21:39 +0200415 else
416 unsetenv(*cmd->env);
417 }
Alex Riesenee493142007-05-22 23:48:47 +0200418 }
Felipe Contreras5a500852013-10-31 03:25:45 -0600419 if (cmd->git_cmd)
Shawn O. Pearcef1000892007-03-10 03:28:00 -0500420 execv_git_cmd(cmd->argv);
Felipe Contreras5a500852013-10-31 03:25:45 -0600421 else if (cmd->use_shell)
Jeff King8dba1e62009-12-30 05:53:16 -0500422 execv_shell_cmd(cmd->argv);
Felipe Contreras5a500852013-10-31 03:25:45 -0600423 else
Jeff King38f865c2012-03-30 03:52:18 -0400424 sane_execvp(cmd->argv[0], (char *const*) cmd->argv);
Clemens Buchacherfc1b56f2011-08-01 19:59:21 +0200425 if (errno == ENOENT) {
426 if (!cmd->silent_exec_failure)
427 error("cannot run %s: %s", cmd->argv[0],
428 strerror(ENOENT));
Johannes Sixta5487dd2010-01-10 14:07:52 +0100429 exit(127);
Clemens Buchacherfc1b56f2011-08-01 19:59:21 +0200430 } else {
Johannes Sixta5487dd2010-01-10 14:07:52 +0100431 die_errno("cannot exec '%s'", cmd->argv[0]);
Clemens Buchacherfc1b56f2011-08-01 19:59:21 +0200432 }
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200433 }
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200434 if (cmd->pid < 0)
435 error("cannot fork() for %s: %s", cmd->argv[0],
Jeff King25043d82013-03-21 11:45:00 -0400436 strerror(errno));
Jeff Kingafe19ff2012-01-07 12:42:43 +0100437 else if (cmd->clean_on_exit)
438 mark_child_for_cleanup(cmd->pid);
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100439
440 /*
441 * Wait for child's execvp. If the execvp succeeds (or if fork()
442 * failed), EOF is seen immediately by the parent. Otherwise, the
443 * child process sends a single byte.
444 * Note that use of this infrastructure is completely advisory,
445 * therefore, we keep error checks minimal.
446 */
447 close(notify_pipe[1]);
448 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
449 /*
450 * At this point we know that fork() succeeded, but execvp()
451 * failed. Errors have been reported to our stderr.
452 */
Jeff King13274522012-11-30 17:40:50 -0500453 wait_or_whine(cmd->pid, cmd->argv[0]);
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100454 failed_errno = errno;
455 cmd->pid = -1;
456 }
457 close(notify_pipe[0]);
458}
Johannes Sixtba26f292007-12-07 22:08:59 +0100459#else
Frank Li0d30ad72009-09-16 10:20:17 +0200460{
Johannes Sixt75301f92010-01-15 21:12:18 +0100461 int fhin = 0, fhout = 1, fherr = 2;
Steffen Prohaska108ac312008-07-28 07:50:28 +0200462 const char **sargv = cmd->argv;
Johannes Sixtba26f292007-12-07 22:08:59 +0100463
Johannes Sixt75301f92010-01-15 21:12:18 +0100464 if (cmd->no_stdin)
465 fhin = open("/dev/null", O_RDWR);
466 else if (need_in)
467 fhin = dup(fdin[0]);
468 else if (cmd->in)
469 fhin = dup(cmd->in);
Johannes Sixtba26f292007-12-07 22:08:59 +0100470
Johannes Sixt75301f92010-01-15 21:12:18 +0100471 if (cmd->no_stderr)
472 fherr = open("/dev/null", O_RDWR);
473 else if (need_err)
474 fherr = dup(fderr[1]);
Junio C Hamano76d44c82010-02-05 21:08:53 -0800475 else if (cmd->err > 2)
476 fherr = dup(cmd->err);
Johannes Sixtba26f292007-12-07 22:08:59 +0100477
Johannes Sixt75301f92010-01-15 21:12:18 +0100478 if (cmd->no_stdout)
479 fhout = open("/dev/null", O_RDWR);
480 else if (cmd->stdout_to_stderr)
481 fhout = dup(fherr);
482 else if (need_out)
483 fhout = dup(fdout[1]);
484 else if (cmd->out > 1)
485 fhout = dup(cmd->out);
Johannes Sixtba26f292007-12-07 22:08:59 +0100486
Felipe Contreras5a500852013-10-31 03:25:45 -0600487 if (cmd->git_cmd)
Steffen Prohaska108ac312008-07-28 07:50:28 +0200488 cmd->argv = prepare_git_cmd(cmd->argv);
Felipe Contreras5a500852013-10-31 03:25:45 -0600489 else if (cmd->use_shell)
Jeff King8dba1e62009-12-30 05:53:16 -0500490 cmd->argv = prepare_shell_cmd(cmd->argv);
Johannes Sixtba26f292007-12-07 22:08:59 +0100491
Karsten Blees77734da2014-07-17 17:38:01 +0200492 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
493 cmd->dir, fhin, fhout, fherr);
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200494 failed_errno = errno;
Johannes Sixtc024beb2009-07-04 21:26:42 +0200495 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200496 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
Jeff Kingafe19ff2012-01-07 12:42:43 +0100497 if (cmd->clean_on_exit && cmd->pid >= 0)
498 mark_child_for_cleanup(cmd->pid);
Johannes Sixtba26f292007-12-07 22:08:59 +0100499
Johannes Sixtba26f292007-12-07 22:08:59 +0100500 if (cmd->git_cmd)
Steffen Prohaska108ac312008-07-28 07:50:28 +0200501 free(cmd->argv);
Johannes Sixtba26f292007-12-07 22:08:59 +0100502
Steffen Prohaska108ac312008-07-28 07:50:28 +0200503 cmd->argv = sargv;
Johannes Sixt75301f92010-01-15 21:12:18 +0100504 if (fhin != 0)
505 close(fhin);
506 if (fhout != 1)
507 close(fhout);
508 if (fherr != 2)
509 close(fherr);
Frank Li0d30ad72009-09-16 10:20:17 +0200510}
Johannes Sixtba26f292007-12-07 22:08:59 +0100511#endif
512
513 if (cmd->pid < 0) {
514 if (need_in)
515 close_pair(fdin);
516 else if (cmd->in)
517 close(cmd->in);
518 if (need_out)
519 close_pair(fdout);
520 else if (cmd->out)
521 close(cmd->out);
522 if (need_err)
523 close_pair(fderr);
bert Dvornikfc012c22010-05-20 20:57:52 +0200524 else if (cmd->err)
525 close(cmd->err);
Jeff Kingc460c0e2014-05-15 04:33:26 -0400526 argv_array_clear(&cmd->args);
René Scharfe19a583d2014-10-19 13:13:55 +0200527 argv_array_clear(&cmd->env_array);
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200528 errno = failed_errno;
529 return -1;
Johannes Sixtba26f292007-12-07 22:08:59 +0100530 }
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500531
532 if (need_in)
533 close(fdin[0]);
534 else if (cmd->in)
535 close(cmd->in);
536
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400537 if (need_out)
538 close(fdout[1]);
Johannes Sixtc20181e2008-02-21 23:42:56 +0100539 else if (cmd->out)
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400540 close(cmd->out);
541
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200542 if (need_err)
543 close(fderr[1]);
Shawn O. Pearce4f41b612010-02-05 12:57:37 -0800544 else if (cmd->err)
545 close(cmd->err);
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200546
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500547 return 0;
548}
549
Johannes Sixt2d22c202007-10-19 21:48:00 +0200550int finish_command(struct child_process *cmd)
551{
Jeff Kingc460c0e2014-05-15 04:33:26 -0400552 int ret = wait_or_whine(cmd->pid, cmd->argv[0]);
553 argv_array_clear(&cmd->args);
René Scharfe19a583d2014-10-19 13:13:55 +0200554 argv_array_clear(&cmd->env_array);
Jeff Kingc460c0e2014-05-15 04:33:26 -0400555 return ret;
Johannes Sixt2d22c202007-10-19 21:48:00 +0200556}
557
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500558int run_command(struct child_process *cmd)
559{
Jeff Kingc29b3962015-03-22 23:54:05 -0400560 int code;
561
562 if (cmd->out < 0 || cmd->err < 0)
563 die("BUG: run_command with a pipe can cause deadlock");
564
565 code = start_command(cmd);
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500566 if (code)
567 return code;
568 return finish_command(cmd);
569}
570
Shawn O. Pearcef1000892007-03-10 03:28:00 -0500571int run_command_v_opt(const char **argv, int opt)
572{
René Scharfe41e9bad2014-08-19 21:11:00 +0200573 return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
Alex Riesen1568fea2007-05-22 23:48:23 +0200574}
575
Alex Riesenee493142007-05-22 23:48:47 +0200576int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
577{
René Scharfe1f872932014-08-19 21:11:43 +0200578 struct child_process cmd = CHILD_PROCESS_INIT;
579 cmd.argv = argv;
580 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
581 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
582 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
583 cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
584 cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
585 cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
Alex Riesenee493142007-05-22 23:48:47 +0200586 cmd.dir = dir;
587 cmd.env = env;
588 return run_command(&cmd);
589}
Johannes Sixt2d22c202007-10-19 21:48:00 +0200590
Johannes Sixtf6b60982010-03-09 21:00:36 +0100591#ifndef NO_PTHREADS
Johannes Sixt0ea1c892010-03-06 16:40:43 +0100592static pthread_t main_thread;
593static int main_thread_set;
594static pthread_key_t async_key;
Jeff King1ece66b2013-04-16 15:50:07 -0400595static pthread_key_t async_die_counter;
Johannes Sixt0ea1c892010-03-06 16:40:43 +0100596
Johannes Sixt200a76b2010-03-06 16:40:42 +0100597static void *run_thread(void *data)
Johannes Sixt618ebe92007-12-08 22:19:14 +0100598{
599 struct async *async = data;
Johannes Sixtf6b60982010-03-09 21:00:36 +0100600 intptr_t ret;
Johannes Sixt0ea1c892010-03-06 16:40:43 +0100601
602 pthread_setspecific(async_key, async);
Johannes Sixtf6b60982010-03-09 21:00:36 +0100603 ret = async->proc(async->proc_in, async->proc_out, async->data);
Johannes Sixt200a76b2010-03-06 16:40:42 +0100604 return (void *)ret;
Johannes Sixt618ebe92007-12-08 22:19:14 +0100605}
Johannes Sixt0ea1c892010-03-06 16:40:43 +0100606
607static NORETURN void die_async(const char *err, va_list params)
608{
609 vreportf("fatal: ", err, params);
610
611 if (!pthread_equal(main_thread, pthread_self())) {
612 struct async *async = pthread_getspecific(async_key);
613 if (async->proc_in >= 0)
614 close(async->proc_in);
615 if (async->proc_out >= 0)
616 close(async->proc_out);
617 pthread_exit((void *)128);
618 }
619
620 exit(128);
Johannes Sixt618ebe92007-12-08 22:19:14 +0100621}
Jeff King1ece66b2013-04-16 15:50:07 -0400622
623static int async_die_is_recursing(void)
624{
625 void *ret = pthread_getspecific(async_die_counter);
626 pthread_setspecific(async_die_counter, (void *)1);
627 return ret != NULL;
628}
629
Etienne Buira0f4b6db2014-10-18 14:31:15 +0200630#else
631
632static struct {
633 void (**handlers)(void);
634 size_t nr;
635 size_t alloc;
636} git_atexit_hdlrs;
637
638static int git_atexit_installed;
639
René Scharfe6066a7e2014-11-10 22:17:00 +0100640static void git_atexit_dispatch(void)
Etienne Buira0f4b6db2014-10-18 14:31:15 +0200641{
642 size_t i;
643
644 for (i=git_atexit_hdlrs.nr ; i ; i--)
645 git_atexit_hdlrs.handlers[i-1]();
646}
647
René Scharfe6066a7e2014-11-10 22:17:00 +0100648static void git_atexit_clear(void)
Etienne Buira0f4b6db2014-10-18 14:31:15 +0200649{
650 free(git_atexit_hdlrs.handlers);
651 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
652 git_atexit_installed = 0;
653}
654
655#undef atexit
656int git_atexit(void (*handler)(void))
657{
658 ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
659 git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
660 if (!git_atexit_installed) {
661 if (atexit(&git_atexit_dispatch))
662 return -1;
663 git_atexit_installed = 1;
664 }
665 return 0;
666}
667#define atexit git_atexit
668
Johannes Sixt618ebe92007-12-08 22:19:14 +0100669#endif
670
Johannes Sixt2d22c202007-10-19 21:48:00 +0200671int start_async(struct async *async)
672{
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800673 int need_in, need_out;
674 int fdin[2], fdout[2];
675 int proc_in, proc_out;
Johannes Sixt2d22c202007-10-19 21:48:00 +0200676
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800677 need_in = async->in < 0;
678 if (need_in) {
679 if (pipe(fdin) < 0) {
680 if (async->out > 0)
681 close(async->out);
682 return error("cannot create pipe: %s", strerror(errno));
683 }
684 async->in = fdin[1];
685 }
686
687 need_out = async->out < 0;
688 if (need_out) {
689 if (pipe(fdout) < 0) {
690 if (need_in)
691 close_pair(fdin);
692 else if (async->in)
693 close(async->in);
694 return error("cannot create pipe: %s", strerror(errno));
695 }
696 async->out = fdout[0];
697 }
698
699 if (need_in)
700 proc_in = fdin[0];
701 else if (async->in)
702 proc_in = async->in;
703 else
704 proc_in = -1;
705
706 if (need_out)
707 proc_out = fdout[1];
708 else if (async->out)
709 proc_out = async->out;
710 else
711 proc_out = -1;
Johannes Sixt2d22c202007-10-19 21:48:00 +0200712
Johannes Sixtf6b60982010-03-09 21:00:36 +0100713#ifdef NO_PTHREADS
Anders Melchiorsen2c3766f2008-08-04 02:30:03 +0200714 /* Flush stdio before fork() to avoid cloning buffers */
715 fflush(NULL);
716
Johannes Sixt2d22c202007-10-19 21:48:00 +0200717 async->pid = fork();
718 if (async->pid < 0) {
719 error("fork (async) failed: %s", strerror(errno));
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800720 goto error;
Johannes Sixt2d22c202007-10-19 21:48:00 +0200721 }
722 if (!async->pid) {
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800723 if (need_in)
724 close(fdin[1]);
725 if (need_out)
726 close(fdout[0]);
Etienne Buira0f4b6db2014-10-18 14:31:15 +0200727 git_atexit_clear();
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800728 exit(!!async->proc(proc_in, proc_out, async->data));
Johannes Sixt2d22c202007-10-19 21:48:00 +0200729 }
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800730
Jeff Kingafe19ff2012-01-07 12:42:43 +0100731 mark_child_for_cleanup(async->pid);
732
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800733 if (need_in)
734 close(fdin[0]);
735 else if (async->in)
736 close(async->in);
737
738 if (need_out)
739 close(fdout[1]);
740 else if (async->out)
741 close(async->out);
Johannes Sixt618ebe92007-12-08 22:19:14 +0100742#else
Johannes Sixt0ea1c892010-03-06 16:40:43 +0100743 if (!main_thread_set) {
744 /*
745 * We assume that the first time that start_async is called
746 * it is from the main thread.
747 */
748 main_thread_set = 1;
749 main_thread = pthread_self();
750 pthread_key_create(&async_key, NULL);
Jeff King1ece66b2013-04-16 15:50:07 -0400751 pthread_key_create(&async_die_counter, NULL);
Johannes Sixt0ea1c892010-03-06 16:40:43 +0100752 set_die_routine(die_async);
Jeff King1ece66b2013-04-16 15:50:07 -0400753 set_die_is_recursing_routine(async_die_is_recursing);
Johannes Sixt0ea1c892010-03-06 16:40:43 +0100754 }
755
Johannes Sixt200a76b2010-03-06 16:40:42 +0100756 if (proc_in >= 0)
757 set_cloexec(proc_in);
758 if (proc_out >= 0)
759 set_cloexec(proc_out);
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800760 async->proc_in = proc_in;
761 async->proc_out = proc_out;
Johannes Sixt200a76b2010-03-06 16:40:42 +0100762 {
763 int err = pthread_create(&async->tid, NULL, run_thread, async);
764 if (err) {
765 error("cannot create thread: %s", strerror(err));
766 goto error;
767 }
Johannes Sixt618ebe92007-12-08 22:19:14 +0100768 }
769#endif
Johannes Sixt2d22c202007-10-19 21:48:00 +0200770 return 0;
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800771
772error:
773 if (need_in)
774 close_pair(fdin);
775 else if (async->in)
776 close(async->in);
777
778 if (need_out)
779 close_pair(fdout);
780 else if (async->out)
781 close(async->out);
782 return -1;
Johannes Sixt2d22c202007-10-19 21:48:00 +0200783}
784
785int finish_async(struct async *async)
786{
Johannes Sixtf6b60982010-03-09 21:00:36 +0100787#ifdef NO_PTHREADS
Jeff King0398fc32013-01-05 09:52:29 -0500788 return wait_or_whine(async->pid, "child process");
Johannes Sixt618ebe92007-12-08 22:19:14 +0100789#else
Johannes Sixt200a76b2010-03-06 16:40:42 +0100790 void *ret = (void *)(intptr_t)(-1);
791
792 if (pthread_join(async->tid, &ret))
793 error("pthread_join failed");
794 return (int)(intptr_t)ret;
Johannes Sixt618ebe92007-12-08 22:19:14 +0100795#endif
Johannes Sixt2d22c202007-10-19 21:48:00 +0200796}
Stephan Beyerae98a002009-01-16 20:09:59 +0100797
Nguyễn Thái Ngọc Duydcf69262014-11-30 15:24:27 +0700798const char *find_hook(const char *name)
Aaron Schrab5a7da2d2013-01-13 00:17:02 -0500799{
Nguyễn Thái Ngọc Duydcf69262014-11-30 15:24:27 +0700800 const char *path = git_path("hooks/%s", name);
Aaron Schrab5a7da2d2013-01-13 00:17:02 -0500801 if (access(path, X_OK) < 0)
802 path = NULL;
803
804 return path;
805}
806
Benoit Pierre15048f82014-03-18 11:00:53 +0100807int run_hook_ve(const char *const *env, const char *name, va_list args)
Stephan Beyerae98a002009-01-16 20:09:59 +0100808{
René Scharfed3180272014-08-19 21:09:35 +0200809 struct child_process hook = CHILD_PROCESS_INIT;
Benoit Pierre15048f82014-03-18 11:00:53 +0100810 const char *p;
Stephan Beyerae98a002009-01-16 20:09:59 +0100811
Aaron Schrab5a7da2d2013-01-13 00:17:02 -0500812 p = find_hook(name);
813 if (!p)
Stephan Beyercf94ca82009-01-16 20:10:01 +0100814 return 0;
815
René Scharfed1d09452014-07-16 23:57:47 +0200816 argv_array_push(&hook.args, p);
817 while ((p = va_arg(args, const char *)))
818 argv_array_push(&hook.args, p);
Benoit Pierre15048f82014-03-18 11:00:53 +0100819 hook.env = env;
Stephan Beyerae98a002009-01-16 20:09:59 +0100820 hook.no_stdin = 1;
821 hook.stdout_to_stderr = 1;
Stephan Beyerae98a002009-01-16 20:09:59 +0100822
René Scharfed1d09452014-07-16 23:57:47 +0200823 return run_command(&hook);
Stephan Beyerae98a002009-01-16 20:09:59 +0100824}
Benoit Pierre15048f82014-03-18 11:00:53 +0100825
826int run_hook_le(const char *const *env, const char *name, ...)
827{
828 va_list args;
829 int ret;
830
831 va_start(args, name);
832 ret = run_hook_ve(env, name, args);
833 va_end(args);
834
835 return ret;
836}
Jeff King911ec992015-03-22 23:53:43 -0400837
838int capture_command(struct child_process *cmd, struct strbuf *buf, size_t hint)
839{
840 cmd->out = -1;
841 if (start_command(cmd) < 0)
842 return -1;
843
844 if (strbuf_read(buf, cmd->out, hint) < 0) {
845 close(cmd->out);
846 finish_command(cmd); /* throw away exit code */
847 return -1;
848 }
849
850 close(cmd->out);
851 return finish_command(cmd);
852}