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