pager: don't use unsafe functions in signal handlers
[git] / run-command.c
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
4 #include "sigchain.h"
5 #include "argv-array.h"
6
7 void child_process_init(struct child_process *child)
8 {
9         memset(child, 0, sizeof(*child));
10         argv_array_init(&child->args);
11         argv_array_init(&child->env_array);
12 }
13
14 struct child_to_clean {
15         pid_t pid;
16         struct child_to_clean *next;
17 };
18 static struct child_to_clean *children_to_clean;
19 static int installed_child_cleanup_handler;
20
21 static void cleanup_children(int sig, int in_signal)
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                 if (!in_signal)
28                         free(p);
29         }
30 }
31
32 static void cleanup_children_on_signal(int sig)
33 {
34         cleanup_children(sig, 1);
35         sigchain_pop(sig);
36         raise(sig);
37 }
38
39 static void cleanup_children_on_exit(void)
40 {
41         cleanup_children(SIGTERM, 0);
42 }
43
44 static void mark_child_for_cleanup(pid_t pid)
45 {
46         struct child_to_clean *p = xmalloc(sizeof(*p));
47         p->pid = pid;
48         p->next = children_to_clean;
49         children_to_clean = p;
50
51         if (!installed_child_cleanup_handler) {
52                 atexit(cleanup_children_on_exit);
53                 sigchain_push_common(cleanup_children_on_signal);
54                 installed_child_cleanup_handler = 1;
55         }
56 }
57
58 static void clear_child_for_cleanup(pid_t pid)
59 {
60         struct child_to_clean **pp;
61
62         for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
63                 struct child_to_clean *clean_me = *pp;
64
65                 if (clean_me->pid == pid) {
66                         *pp = clean_me->next;
67                         free(clean_me);
68                         return;
69                 }
70         }
71 }
72
73 static inline void close_pair(int fd[2])
74 {
75         close(fd[0]);
76         close(fd[1]);
77 }
78
79 #ifndef GIT_WINDOWS_NATIVE
80 static inline void dup_devnull(int to)
81 {
82         int fd = open("/dev/null", O_RDWR);
83         if (fd < 0)
84                 die_errno(_("open /dev/null failed"));
85         if (dup2(fd, to) < 0)
86                 die_errno(_("dup2(%d,%d) failed"), fd, to);
87         close(fd);
88 }
89 #endif
90
91 static char *locate_in_PATH(const char *file)
92 {
93         const char *p = getenv("PATH");
94         struct strbuf buf = STRBUF_INIT;
95
96         if (!p || !*p)
97                 return NULL;
98
99         while (1) {
100                 const char *end = strchrnul(p, ':');
101
102                 strbuf_reset(&buf);
103
104                 /* POSIX specifies an empty entry as the current directory. */
105                 if (end != p) {
106                         strbuf_add(&buf, p, end - p);
107                         strbuf_addch(&buf, '/');
108                 }
109                 strbuf_addstr(&buf, file);
110
111                 if (!access(buf.buf, F_OK))
112                         return strbuf_detach(&buf, NULL);
113
114                 if (!*end)
115                         break;
116                 p = end + 1;
117         }
118
119         strbuf_release(&buf);
120         return NULL;
121 }
122
123 static int exists_in_PATH(const char *file)
124 {
125         char *r = locate_in_PATH(file);
126         free(r);
127         return r != NULL;
128 }
129
130 int sane_execvp(const char *file, char * const argv[])
131 {
132         if (!execvp(file, argv))
133                 return 0; /* cannot happen ;-) */
134
135         /*
136          * When a command can't be found because one of the directories
137          * listed in $PATH is unsearchable, execvp reports EACCES, but
138          * careful usability testing (read: analysis of occasional bug
139          * reports) reveals that "No such file or directory" is more
140          * intuitive.
141          *
142          * We avoid commands with "/", because execvp will not do $PATH
143          * lookups in that case.
144          *
145          * The reassignment of EACCES to errno looks like a no-op below,
146          * but we need to protect against exists_in_PATH overwriting errno.
147          */
148         if (errno == EACCES && !strchr(file, '/'))
149                 errno = exists_in_PATH(file) ? EACCES : ENOENT;
150         else if (errno == ENOTDIR && !strchr(file, '/'))
151                 errno = ENOENT;
152         return -1;
153 }
154
155 static const char **prepare_shell_cmd(const char **argv)
156 {
157         int argc, nargc = 0;
158         const char **nargv;
159
160         for (argc = 0; argv[argc]; argc++)
161                 ; /* just counting */
162         /* +1 for NULL, +3 for "sh -c" plus extra $0 */
163         nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
164
165         if (argc < 1)
166                 die("BUG: shell command is empty");
167
168         if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
169 #ifndef GIT_WINDOWS_NATIVE
170                 nargv[nargc++] = SHELL_PATH;
171 #else
172                 nargv[nargc++] = "sh";
173 #endif
174                 nargv[nargc++] = "-c";
175
176                 if (argc < 2)
177                         nargv[nargc++] = argv[0];
178                 else {
179                         struct strbuf arg0 = STRBUF_INIT;
180                         strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
181                         nargv[nargc++] = strbuf_detach(&arg0, NULL);
182                 }
183         }
184
185         for (argc = 0; argv[argc]; argc++)
186                 nargv[nargc++] = argv[argc];
187         nargv[nargc] = NULL;
188
189         return nargv;
190 }
191
192 #ifndef GIT_WINDOWS_NATIVE
193 static int execv_shell_cmd(const char **argv)
194 {
195         const char **nargv = prepare_shell_cmd(argv);
196         trace_argv_printf(nargv, "trace: exec:");
197         sane_execvp(nargv[0], (char **)nargv);
198         free(nargv);
199         return -1;
200 }
201 #endif
202
203 #ifndef GIT_WINDOWS_NATIVE
204 static int child_err = 2;
205 static int child_notifier = -1;
206
207 static void notify_parent(void)
208 {
209         /*
210          * execvp failed.  If possible, we'd like to let start_command
211          * know, so failures like ENOENT can be handled right away; but
212          * otherwise, finish_command will still report the error.
213          */
214         xwrite(child_notifier, "", 1);
215 }
216
217 static NORETURN void die_child(const char *err, va_list params)
218 {
219         vwritef(child_err, "fatal: ", err, params);
220         exit(128);
221 }
222
223 static void error_child(const char *err, va_list params)
224 {
225         vwritef(child_err, "error: ", err, params);
226 }
227 #endif
228
229 static inline void set_cloexec(int fd)
230 {
231         int flags = fcntl(fd, F_GETFD);
232         if (flags >= 0)
233                 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
234 }
235
236 static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
237 {
238         int status, code = -1;
239         pid_t waiting;
240         int failed_errno = 0;
241
242         while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
243                 ;       /* nothing */
244         if (in_signal)
245                 return 0;
246
247         if (waiting < 0) {
248                 failed_errno = errno;
249                 error("waitpid for %s failed: %s", argv0, strerror(errno));
250         } else if (waiting != pid) {
251                 error("waitpid is confused (%s)", argv0);
252         } else if (WIFSIGNALED(status)) {
253                 code = WTERMSIG(status);
254                 if (code != SIGINT && code != SIGQUIT)
255                         error("%s died of signal %d", argv0, code);
256                 /*
257                  * This return value is chosen so that code & 0xff
258                  * mimics the exit code that a POSIX shell would report for
259                  * a program that died from this signal.
260                  */
261                 code += 128;
262         } else if (WIFEXITED(status)) {
263                 code = WEXITSTATUS(status);
264                 /*
265                  * Convert special exit code when execvp failed.
266                  */
267                 if (code == 127) {
268                         code = -1;
269                         failed_errno = ENOENT;
270                 }
271         } else {
272                 error("waitpid is confused (%s)", argv0);
273         }
274
275         clear_child_for_cleanup(pid);
276
277         errno = failed_errno;
278         return code;
279 }
280
281 int start_command(struct child_process *cmd)
282 {
283         int need_in, need_out, need_err;
284         int fdin[2], fdout[2], fderr[2];
285         int failed_errno;
286         char *str;
287
288         if (!cmd->argv)
289                 cmd->argv = cmd->args.argv;
290         if (!cmd->env)
291                 cmd->env = cmd->env_array.argv;
292
293         /*
294          * In case of errors we must keep the promise to close FDs
295          * that have been passed in via ->in and ->out.
296          */
297
298         need_in = !cmd->no_stdin && cmd->in < 0;
299         if (need_in) {
300                 if (pipe(fdin) < 0) {
301                         failed_errno = errno;
302                         if (cmd->out > 0)
303                                 close(cmd->out);
304                         str = "standard input";
305                         goto fail_pipe;
306                 }
307                 cmd->in = fdin[1];
308         }
309
310         need_out = !cmd->no_stdout
311                 && !cmd->stdout_to_stderr
312                 && cmd->out < 0;
313         if (need_out) {
314                 if (pipe(fdout) < 0) {
315                         failed_errno = errno;
316                         if (need_in)
317                                 close_pair(fdin);
318                         else if (cmd->in)
319                                 close(cmd->in);
320                         str = "standard output";
321                         goto fail_pipe;
322                 }
323                 cmd->out = fdout[0];
324         }
325
326         need_err = !cmd->no_stderr && cmd->err < 0;
327         if (need_err) {
328                 if (pipe(fderr) < 0) {
329                         failed_errno = errno;
330                         if (need_in)
331                                 close_pair(fdin);
332                         else if (cmd->in)
333                                 close(cmd->in);
334                         if (need_out)
335                                 close_pair(fdout);
336                         else if (cmd->out)
337                                 close(cmd->out);
338                         str = "standard error";
339 fail_pipe:
340                         error("cannot create %s pipe for %s: %s",
341                                 str, cmd->argv[0], strerror(failed_errno));
342                         argv_array_clear(&cmd->args);
343                         argv_array_clear(&cmd->env_array);
344                         errno = failed_errno;
345                         return -1;
346                 }
347                 cmd->err = fderr[0];
348         }
349
350         trace_argv_printf(cmd->argv, "trace: run_command:");
351         fflush(NULL);
352
353 #ifndef GIT_WINDOWS_NATIVE
354 {
355         int notify_pipe[2];
356         if (pipe(notify_pipe))
357                 notify_pipe[0] = notify_pipe[1] = -1;
358
359         cmd->pid = fork();
360         failed_errno = errno;
361         if (!cmd->pid) {
362                 /*
363                  * Redirect the channel to write syscall error messages to
364                  * before redirecting the process's stderr so that all die()
365                  * in subsequent call paths use the parent's stderr.
366                  */
367                 if (cmd->no_stderr || need_err) {
368                         child_err = dup(2);
369                         set_cloexec(child_err);
370                 }
371                 set_die_routine(die_child);
372                 set_error_routine(error_child);
373
374                 close(notify_pipe[0]);
375                 set_cloexec(notify_pipe[1]);
376                 child_notifier = notify_pipe[1];
377                 atexit(notify_parent);
378
379                 if (cmd->no_stdin)
380                         dup_devnull(0);
381                 else if (need_in) {
382                         dup2(fdin[0], 0);
383                         close_pair(fdin);
384                 } else if (cmd->in) {
385                         dup2(cmd->in, 0);
386                         close(cmd->in);
387                 }
388
389                 if (cmd->no_stderr)
390                         dup_devnull(2);
391                 else if (need_err) {
392                         dup2(fderr[1], 2);
393                         close_pair(fderr);
394                 } else if (cmd->err > 1) {
395                         dup2(cmd->err, 2);
396                         close(cmd->err);
397                 }
398
399                 if (cmd->no_stdout)
400                         dup_devnull(1);
401                 else if (cmd->stdout_to_stderr)
402                         dup2(2, 1);
403                 else if (need_out) {
404                         dup2(fdout[1], 1);
405                         close_pair(fdout);
406                 } else if (cmd->out > 1) {
407                         dup2(cmd->out, 1);
408                         close(cmd->out);
409                 }
410
411                 if (cmd->dir && chdir(cmd->dir))
412                         die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
413                             cmd->dir);
414                 if (cmd->env) {
415                         for (; *cmd->env; cmd->env++) {
416                                 if (strchr(*cmd->env, '='))
417                                         putenv((char *)*cmd->env);
418                                 else
419                                         unsetenv(*cmd->env);
420                         }
421                 }
422                 if (cmd->git_cmd)
423                         execv_git_cmd(cmd->argv);
424                 else if (cmd->use_shell)
425                         execv_shell_cmd(cmd->argv);
426                 else
427                         sane_execvp(cmd->argv[0], (char *const*) cmd->argv);
428                 if (errno == ENOENT) {
429                         if (!cmd->silent_exec_failure)
430                                 error("cannot run %s: %s", cmd->argv[0],
431                                         strerror(ENOENT));
432                         exit(127);
433                 } else {
434                         die_errno("cannot exec '%s'", cmd->argv[0]);
435                 }
436         }
437         if (cmd->pid < 0)
438                 error("cannot fork() for %s: %s", cmd->argv[0],
439                         strerror(errno));
440         else if (cmd->clean_on_exit)
441                 mark_child_for_cleanup(cmd->pid);
442
443         /*
444          * Wait for child's execvp. If the execvp succeeds (or if fork()
445          * failed), EOF is seen immediately by the parent. Otherwise, the
446          * child process sends a single byte.
447          * Note that use of this infrastructure is completely advisory,
448          * therefore, we keep error checks minimal.
449          */
450         close(notify_pipe[1]);
451         if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
452                 /*
453                  * At this point we know that fork() succeeded, but execvp()
454                  * failed. Errors have been reported to our stderr.
455                  */
456                 wait_or_whine(cmd->pid, cmd->argv[0], 0);
457                 failed_errno = errno;
458                 cmd->pid = -1;
459         }
460         close(notify_pipe[0]);
461 }
462 #else
463 {
464         int fhin = 0, fhout = 1, fherr = 2;
465         const char **sargv = cmd->argv;
466
467         if (cmd->no_stdin)
468                 fhin = open("/dev/null", O_RDWR);
469         else if (need_in)
470                 fhin = dup(fdin[0]);
471         else if (cmd->in)
472                 fhin = dup(cmd->in);
473
474         if (cmd->no_stderr)
475                 fherr = open("/dev/null", O_RDWR);
476         else if (need_err)
477                 fherr = dup(fderr[1]);
478         else if (cmd->err > 2)
479                 fherr = dup(cmd->err);
480
481         if (cmd->no_stdout)
482                 fhout = open("/dev/null", O_RDWR);
483         else if (cmd->stdout_to_stderr)
484                 fhout = dup(fherr);
485         else if (need_out)
486                 fhout = dup(fdout[1]);
487         else if (cmd->out > 1)
488                 fhout = dup(cmd->out);
489
490         if (cmd->git_cmd)
491                 cmd->argv = prepare_git_cmd(cmd->argv);
492         else if (cmd->use_shell)
493                 cmd->argv = prepare_shell_cmd(cmd->argv);
494
495         cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
496                         cmd->dir, fhin, fhout, fherr);
497         failed_errno = errno;
498         if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
499                 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
500         if (cmd->clean_on_exit && cmd->pid >= 0)
501                 mark_child_for_cleanup(cmd->pid);
502
503         if (cmd->git_cmd)
504                 free(cmd->argv);
505
506         cmd->argv = sargv;
507         if (fhin != 0)
508                 close(fhin);
509         if (fhout != 1)
510                 close(fhout);
511         if (fherr != 2)
512                 close(fherr);
513 }
514 #endif
515
516         if (cmd->pid < 0) {
517                 if (need_in)
518                         close_pair(fdin);
519                 else if (cmd->in)
520                         close(cmd->in);
521                 if (need_out)
522                         close_pair(fdout);
523                 else if (cmd->out)
524                         close(cmd->out);
525                 if (need_err)
526                         close_pair(fderr);
527                 else if (cmd->err)
528                         close(cmd->err);
529                 argv_array_clear(&cmd->args);
530                 argv_array_clear(&cmd->env_array);
531                 errno = failed_errno;
532                 return -1;
533         }
534
535         if (need_in)
536                 close(fdin[0]);
537         else if (cmd->in)
538                 close(cmd->in);
539
540         if (need_out)
541                 close(fdout[1]);
542         else if (cmd->out)
543                 close(cmd->out);
544
545         if (need_err)
546                 close(fderr[1]);
547         else if (cmd->err)
548                 close(cmd->err);
549
550         return 0;
551 }
552
553 int finish_command(struct child_process *cmd)
554 {
555         int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
556         argv_array_clear(&cmd->args);
557         argv_array_clear(&cmd->env_array);
558         return ret;
559 }
560
561 int finish_command_in_signal(struct child_process *cmd)
562 {
563         return wait_or_whine(cmd->pid, cmd->argv[0], 1);
564 }
565
566
567 int run_command(struct child_process *cmd)
568 {
569         int code;
570
571         if (cmd->out < 0 || cmd->err < 0)
572                 die("BUG: run_command with a pipe can cause deadlock");
573
574         code = start_command(cmd);
575         if (code)
576                 return code;
577         return finish_command(cmd);
578 }
579
580 int run_command_v_opt(const char **argv, int opt)
581 {
582         return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
583 }
584
585 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
586 {
587         struct child_process cmd = CHILD_PROCESS_INIT;
588         cmd.argv = argv;
589         cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
590         cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
591         cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
592         cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
593         cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
594         cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
595         cmd.dir = dir;
596         cmd.env = env;
597         return run_command(&cmd);
598 }
599
600 #ifndef NO_PTHREADS
601 static pthread_t main_thread;
602 static int main_thread_set;
603 static pthread_key_t async_key;
604 static pthread_key_t async_die_counter;
605
606 static void *run_thread(void *data)
607 {
608         struct async *async = data;
609         intptr_t ret;
610
611         pthread_setspecific(async_key, async);
612         ret = async->proc(async->proc_in, async->proc_out, async->data);
613         return (void *)ret;
614 }
615
616 static NORETURN void die_async(const char *err, va_list params)
617 {
618         vreportf("fatal: ", err, params);
619
620         if (!pthread_equal(main_thread, pthread_self())) {
621                 struct async *async = pthread_getspecific(async_key);
622                 if (async->proc_in >= 0)
623                         close(async->proc_in);
624                 if (async->proc_out >= 0)
625                         close(async->proc_out);
626                 pthread_exit((void *)128);
627         }
628
629         exit(128);
630 }
631
632 static int async_die_is_recursing(void)
633 {
634         void *ret = pthread_getspecific(async_die_counter);
635         pthread_setspecific(async_die_counter, (void *)1);
636         return ret != NULL;
637 }
638
639 #else
640
641 static struct {
642         void (**handlers)(void);
643         size_t nr;
644         size_t alloc;
645 } git_atexit_hdlrs;
646
647 static int git_atexit_installed;
648
649 static void git_atexit_dispatch(void)
650 {
651         size_t i;
652
653         for (i=git_atexit_hdlrs.nr ; i ; i--)
654                 git_atexit_hdlrs.handlers[i-1]();
655 }
656
657 static void git_atexit_clear(void)
658 {
659         free(git_atexit_hdlrs.handlers);
660         memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
661         git_atexit_installed = 0;
662 }
663
664 #undef atexit
665 int git_atexit(void (*handler)(void))
666 {
667         ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
668         git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
669         if (!git_atexit_installed) {
670                 if (atexit(&git_atexit_dispatch))
671                         return -1;
672                 git_atexit_installed = 1;
673         }
674         return 0;
675 }
676 #define atexit git_atexit
677
678 #endif
679
680 int start_async(struct async *async)
681 {
682         int need_in, need_out;
683         int fdin[2], fdout[2];
684         int proc_in, proc_out;
685
686         need_in = async->in < 0;
687         if (need_in) {
688                 if (pipe(fdin) < 0) {
689                         if (async->out > 0)
690                                 close(async->out);
691                         return error("cannot create pipe: %s", strerror(errno));
692                 }
693                 async->in = fdin[1];
694         }
695
696         need_out = async->out < 0;
697         if (need_out) {
698                 if (pipe(fdout) < 0) {
699                         if (need_in)
700                                 close_pair(fdin);
701                         else if (async->in)
702                                 close(async->in);
703                         return error("cannot create pipe: %s", strerror(errno));
704                 }
705                 async->out = fdout[0];
706         }
707
708         if (need_in)
709                 proc_in = fdin[0];
710         else if (async->in)
711                 proc_in = async->in;
712         else
713                 proc_in = -1;
714
715         if (need_out)
716                 proc_out = fdout[1];
717         else if (async->out)
718                 proc_out = async->out;
719         else
720                 proc_out = -1;
721
722 #ifdef NO_PTHREADS
723         /* Flush stdio before fork() to avoid cloning buffers */
724         fflush(NULL);
725
726         async->pid = fork();
727         if (async->pid < 0) {
728                 error("fork (async) failed: %s", strerror(errno));
729                 goto error;
730         }
731         if (!async->pid) {
732                 if (need_in)
733                         close(fdin[1]);
734                 if (need_out)
735                         close(fdout[0]);
736                 git_atexit_clear();
737                 exit(!!async->proc(proc_in, proc_out, async->data));
738         }
739
740         mark_child_for_cleanup(async->pid);
741
742         if (need_in)
743                 close(fdin[0]);
744         else if (async->in)
745                 close(async->in);
746
747         if (need_out)
748                 close(fdout[1]);
749         else if (async->out)
750                 close(async->out);
751 #else
752         if (!main_thread_set) {
753                 /*
754                  * We assume that the first time that start_async is called
755                  * it is from the main thread.
756                  */
757                 main_thread_set = 1;
758                 main_thread = pthread_self();
759                 pthread_key_create(&async_key, NULL);
760                 pthread_key_create(&async_die_counter, NULL);
761                 set_die_routine(die_async);
762                 set_die_is_recursing_routine(async_die_is_recursing);
763         }
764
765         if (proc_in >= 0)
766                 set_cloexec(proc_in);
767         if (proc_out >= 0)
768                 set_cloexec(proc_out);
769         async->proc_in = proc_in;
770         async->proc_out = proc_out;
771         {
772                 int err = pthread_create(&async->tid, NULL, run_thread, async);
773                 if (err) {
774                         error("cannot create thread: %s", strerror(err));
775                         goto error;
776                 }
777         }
778 #endif
779         return 0;
780
781 error:
782         if (need_in)
783                 close_pair(fdin);
784         else if (async->in)
785                 close(async->in);
786
787         if (need_out)
788                 close_pair(fdout);
789         else if (async->out)
790                 close(async->out);
791         return -1;
792 }
793
794 int finish_async(struct async *async)
795 {
796 #ifdef NO_PTHREADS
797         return wait_or_whine(async->pid, "child process", 0);
798 #else
799         void *ret = (void *)(intptr_t)(-1);
800
801         if (pthread_join(async->tid, &ret))
802                 error("pthread_join failed");
803         return (int)(intptr_t)ret;
804 #endif
805 }
806
807 const char *find_hook(const char *name)
808 {
809         const char *path = git_path("hooks/%s", name);
810         if (access(path, X_OK) < 0)
811                 path = NULL;
812
813         return path;
814 }
815
816 int run_hook_ve(const char *const *env, const char *name, va_list args)
817 {
818         struct child_process hook = CHILD_PROCESS_INIT;
819         const char *p;
820
821         p = find_hook(name);
822         if (!p)
823                 return 0;
824
825         argv_array_push(&hook.args, p);
826         while ((p = va_arg(args, const char *)))
827                 argv_array_push(&hook.args, p);
828         hook.env = env;
829         hook.no_stdin = 1;
830         hook.stdout_to_stderr = 1;
831
832         return run_command(&hook);
833 }
834
835 int run_hook_le(const char *const *env, const char *name, ...)
836 {
837         va_list args;
838         int ret;
839
840         va_start(args, name);
841         ret = run_hook_ve(env, name, args);
842         va_end(args);
843
844         return ret;
845 }
846
847 int capture_command(struct child_process *cmd, struct strbuf *buf, size_t hint)
848 {
849         cmd->out = -1;
850         if (start_command(cmd) < 0)
851                 return -1;
852
853         if (strbuf_read(buf, cmd->out, hint) < 0) {
854                 close(cmd->out);
855                 finish_command(cmd); /* throw away exit code */
856                 return -1;
857         }
858
859         close(cmd->out);
860         return finish_command(cmd);
861 }