run-command.con commit error_routine: use parent's stderr if exec fails (3bc4181)
   1#include "cache.h"
   2#include "run-command.h"
   3#include "exec_cmd.h"
   4
   5static inline void close_pair(int fd[2])
   6{
   7        close(fd[0]);
   8        close(fd[1]);
   9}
  10
  11#ifndef WIN32
  12static inline void dup_devnull(int to)
  13{
  14        int fd = open("/dev/null", O_RDWR);
  15        dup2(fd, to);
  16        close(fd);
  17}
  18#endif
  19
  20static 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
  33        if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
  34                nargv[nargc++] = "sh";
  35                nargv[nargc++] = "-c";
  36
  37                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                }
  44        }
  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
  64#ifndef WIN32
  65static int child_err = 2;
  66static int child_notifier = -1;
  67
  68static void notify_parent(void)
  69{
  70        /*
  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);
  76}
  77
  78static NORETURN void die_child(const char *err, va_list params)
  79{
  80        vwritef(child_err, "fatal: ", err, params);
  81        exit(128);
  82}
  83
  84static void error_child(const char *err, va_list params)
  85{
  86        vwritef(child_err, "error: ", err, params);
  87}
  88#endif
  89
  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}
  96
  97static 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;
 128                        if (!silent_exec_failure)
 129                                error("cannot run %s: %s", argv0,
 130                                        strerror(ENOENT));
 131                }
 132        } else {
 133                error("waitpid is confused (%s)", argv0);
 134        }
 135        errno = failed_errno;
 136        return code;
 137}
 138
 139int start_command(struct child_process *cmd)
 140{
 141        int need_in, need_out, need_err;
 142        int fdin[2], fdout[2], fderr[2];
 143        int failed_errno = failed_errno;
 144
 145        /*
 146         * In case of errors we must keep the promise to close FDs
 147         * that have been passed in via ->in and ->out.
 148         */
 149
 150        need_in = !cmd->no_stdin && cmd->in < 0;
 151        if (need_in) {
 152                if (pipe(fdin) < 0) {
 153                        failed_errno = errno;
 154                        if (cmd->out > 0)
 155                                close(cmd->out);
 156                        goto fail_pipe;
 157                }
 158                cmd->in = fdin[1];
 159        }
 160
 161        need_out = !cmd->no_stdout
 162                && !cmd->stdout_to_stderr
 163                && cmd->out < 0;
 164        if (need_out) {
 165                if (pipe(fdout) < 0) {
 166                        failed_errno = errno;
 167                        if (need_in)
 168                                close_pair(fdin);
 169                        else if (cmd->in)
 170                                close(cmd->in);
 171                        goto fail_pipe;
 172                }
 173                cmd->out = fdout[0];
 174        }
 175
 176        need_err = !cmd->no_stderr && cmd->err < 0;
 177        if (need_err) {
 178                if (pipe(fderr) < 0) {
 179                        failed_errno = errno;
 180                        if (need_in)
 181                                close_pair(fdin);
 182                        else if (cmd->in)
 183                                close(cmd->in);
 184                        if (need_out)
 185                                close_pair(fdout);
 186                        else if (cmd->out)
 187                                close(cmd->out);
 188fail_pipe:
 189                        error("cannot create pipe for %s: %s",
 190                                cmd->argv[0], strerror(failed_errno));
 191                        errno = failed_errno;
 192                        return -1;
 193                }
 194                cmd->err = fderr[0];
 195        }
 196
 197        trace_argv_printf(cmd->argv, "trace: run_command:");
 198        fflush(NULL);
 199
 200#ifndef WIN32
 201{
 202        int notify_pipe[2];
 203        if (pipe(notify_pipe))
 204                notify_pipe[0] = notify_pipe[1] = -1;
 205
 206        cmd->pid = fork();
 207        if (!cmd->pid) {
 208                /*
 209                 * Redirect the channel to write syscall error messages to
 210                 * before redirecting the process's stderr so that all die()
 211                 * in subsequent call paths use the parent's stderr.
 212                 */
 213                if (cmd->no_stderr || need_err) {
 214                        child_err = dup(2);
 215                        set_cloexec(child_err);
 216                }
 217                set_die_routine(die_child);
 218                set_error_routine(error_child);
 219
 220                close(notify_pipe[0]);
 221                set_cloexec(notify_pipe[1]);
 222                child_notifier = notify_pipe[1];
 223                atexit(notify_parent);
 224
 225                if (cmd->no_stdin)
 226                        dup_devnull(0);
 227                else if (need_in) {
 228                        dup2(fdin[0], 0);
 229                        close_pair(fdin);
 230                } else if (cmd->in) {
 231                        dup2(cmd->in, 0);
 232                        close(cmd->in);
 233                }
 234
 235                if (cmd->no_stderr)
 236                        dup_devnull(2);
 237                else if (need_err) {
 238                        dup2(fderr[1], 2);
 239                        close_pair(fderr);
 240                } else if (cmd->err > 1) {
 241                        dup2(cmd->err, 2);
 242                        close(cmd->err);
 243                }
 244
 245                if (cmd->no_stdout)
 246                        dup_devnull(1);
 247                else if (cmd->stdout_to_stderr)
 248                        dup2(2, 1);
 249                else if (need_out) {
 250                        dup2(fdout[1], 1);
 251                        close_pair(fdout);
 252                } else if (cmd->out > 1) {
 253                        dup2(cmd->out, 1);
 254                        close(cmd->out);
 255                }
 256
 257                if (cmd->dir && chdir(cmd->dir))
 258                        die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
 259                            cmd->dir);
 260                if (cmd->env) {
 261                        for (; *cmd->env; cmd->env++) {
 262                                if (strchr(*cmd->env, '='))
 263                                        putenv((char *)*cmd->env);
 264                                else
 265                                        unsetenv(*cmd->env);
 266                        }
 267                }
 268                if (cmd->preexec_cb) {
 269                        /*
 270                         * We cannot predict what the pre-exec callback does.
 271                         * Forgo parent notification.
 272                         */
 273                        close(child_notifier);
 274                        child_notifier = -1;
 275
 276                        cmd->preexec_cb();
 277                }
 278                if (cmd->git_cmd) {
 279                        execv_git_cmd(cmd->argv);
 280                } else if (cmd->use_shell) {
 281                        execv_shell_cmd(cmd->argv);
 282                } else {
 283                        execvp(cmd->argv[0], (char *const*) cmd->argv);
 284                }
 285                /*
 286                 * Do not check for cmd->silent_exec_failure; the parent
 287                 * process will check it when it sees this exit code.
 288                 */
 289                if (errno == ENOENT)
 290                        exit(127);
 291                else
 292                        die_errno("cannot exec '%s'", cmd->argv[0]);
 293        }
 294        if (cmd->pid < 0)
 295                error("cannot fork() for %s: %s", cmd->argv[0],
 296                        strerror(failed_errno = errno));
 297
 298        /*
 299         * Wait for child's execvp. If the execvp succeeds (or if fork()
 300         * failed), EOF is seen immediately by the parent. Otherwise, the
 301         * child process sends a single byte.
 302         * Note that use of this infrastructure is completely advisory,
 303         * therefore, we keep error checks minimal.
 304         */
 305        close(notify_pipe[1]);
 306        if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
 307                /*
 308                 * At this point we know that fork() succeeded, but execvp()
 309                 * failed. Errors have been reported to our stderr.
 310                 */
 311                wait_or_whine(cmd->pid, cmd->argv[0],
 312                              cmd->silent_exec_failure);
 313                failed_errno = errno;
 314                cmd->pid = -1;
 315        }
 316        close(notify_pipe[0]);
 317}
 318#else
 319{
 320        int fhin = 0, fhout = 1, fherr = 2;
 321        const char **sargv = cmd->argv;
 322        char **env = environ;
 323
 324        if (cmd->no_stdin)
 325                fhin = open("/dev/null", O_RDWR);
 326        else if (need_in)
 327                fhin = dup(fdin[0]);
 328        else if (cmd->in)
 329                fhin = dup(cmd->in);
 330
 331        if (cmd->no_stderr)
 332                fherr = open("/dev/null", O_RDWR);
 333        else if (need_err)
 334                fherr = dup(fderr[1]);
 335        else if (cmd->err > 2)
 336                fherr = dup(cmd->err);
 337
 338        if (cmd->no_stdout)
 339                fhout = open("/dev/null", O_RDWR);
 340        else if (cmd->stdout_to_stderr)
 341                fhout = dup(fherr);
 342        else if (need_out)
 343                fhout = dup(fdout[1]);
 344        else if (cmd->out > 1)
 345                fhout = dup(cmd->out);
 346
 347        if (cmd->env)
 348                env = make_augmented_environ(cmd->env);
 349
 350        if (cmd->git_cmd) {
 351                cmd->argv = prepare_git_cmd(cmd->argv);
 352        } else if (cmd->use_shell) {
 353                cmd->argv = prepare_shell_cmd(cmd->argv);
 354        }
 355
 356        cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir,
 357                                  fhin, fhout, fherr);
 358        failed_errno = errno;
 359        if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
 360                error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
 361
 362        if (cmd->env)
 363                free_environ(env);
 364        if (cmd->git_cmd)
 365                free(cmd->argv);
 366
 367        cmd->argv = sargv;
 368        if (fhin != 0)
 369                close(fhin);
 370        if (fhout != 1)
 371                close(fhout);
 372        if (fherr != 2)
 373                close(fherr);
 374}
 375#endif
 376
 377        if (cmd->pid < 0) {
 378                if (need_in)
 379                        close_pair(fdin);
 380                else if (cmd->in)
 381                        close(cmd->in);
 382                if (need_out)
 383                        close_pair(fdout);
 384                else if (cmd->out)
 385                        close(cmd->out);
 386                if (need_err)
 387                        close_pair(fderr);
 388                else if (cmd->err)
 389                        close(cmd->err);
 390                errno = failed_errno;
 391                return -1;
 392        }
 393
 394        if (need_in)
 395                close(fdin[0]);
 396        else if (cmd->in)
 397                close(cmd->in);
 398
 399        if (need_out)
 400                close(fdout[1]);
 401        else if (cmd->out)
 402                close(cmd->out);
 403
 404        if (need_err)
 405                close(fderr[1]);
 406        else if (cmd->err)
 407                close(cmd->err);
 408
 409        return 0;
 410}
 411
 412int finish_command(struct child_process *cmd)
 413{
 414        return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
 415}
 416
 417int run_command(struct child_process *cmd)
 418{
 419        int code = start_command(cmd);
 420        if (code)
 421                return code;
 422        return finish_command(cmd);
 423}
 424
 425static void prepare_run_command_v_opt(struct child_process *cmd,
 426                                      const char **argv,
 427                                      int opt)
 428{
 429        memset(cmd, 0, sizeof(*cmd));
 430        cmd->argv = argv;
 431        cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
 432        cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
 433        cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
 434        cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
 435        cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
 436}
 437
 438int run_command_v_opt(const char **argv, int opt)
 439{
 440        struct child_process cmd;
 441        prepare_run_command_v_opt(&cmd, argv, opt);
 442        return run_command(&cmd);
 443}
 444
 445int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
 446{
 447        struct child_process cmd;
 448        prepare_run_command_v_opt(&cmd, argv, opt);
 449        cmd.dir = dir;
 450        cmd.env = env;
 451        return run_command(&cmd);
 452}
 453
 454#ifndef NO_PTHREADS
 455static pthread_t main_thread;
 456static int main_thread_set;
 457static pthread_key_t async_key;
 458
 459static void *run_thread(void *data)
 460{
 461        struct async *async = data;
 462        intptr_t ret;
 463
 464        pthread_setspecific(async_key, async);
 465        ret = async->proc(async->proc_in, async->proc_out, async->data);
 466        return (void *)ret;
 467}
 468
 469static NORETURN void die_async(const char *err, va_list params)
 470{
 471        vreportf("fatal: ", err, params);
 472
 473        if (!pthread_equal(main_thread, pthread_self())) {
 474                struct async *async = pthread_getspecific(async_key);
 475                if (async->proc_in >= 0)
 476                        close(async->proc_in);
 477                if (async->proc_out >= 0)
 478                        close(async->proc_out);
 479                pthread_exit((void *)128);
 480        }
 481
 482        exit(128);
 483}
 484#endif
 485
 486int start_async(struct async *async)
 487{
 488        int need_in, need_out;
 489        int fdin[2], fdout[2];
 490        int proc_in, proc_out;
 491
 492        need_in = async->in < 0;
 493        if (need_in) {
 494                if (pipe(fdin) < 0) {
 495                        if (async->out > 0)
 496                                close(async->out);
 497                        return error("cannot create pipe: %s", strerror(errno));
 498                }
 499                async->in = fdin[1];
 500        }
 501
 502        need_out = async->out < 0;
 503        if (need_out) {
 504                if (pipe(fdout) < 0) {
 505                        if (need_in)
 506                                close_pair(fdin);
 507                        else if (async->in)
 508                                close(async->in);
 509                        return error("cannot create pipe: %s", strerror(errno));
 510                }
 511                async->out = fdout[0];
 512        }
 513
 514        if (need_in)
 515                proc_in = fdin[0];
 516        else if (async->in)
 517                proc_in = async->in;
 518        else
 519                proc_in = -1;
 520
 521        if (need_out)
 522                proc_out = fdout[1];
 523        else if (async->out)
 524                proc_out = async->out;
 525        else
 526                proc_out = -1;
 527
 528#ifdef NO_PTHREADS
 529        /* Flush stdio before fork() to avoid cloning buffers */
 530        fflush(NULL);
 531
 532        async->pid = fork();
 533        if (async->pid < 0) {
 534                error("fork (async) failed: %s", strerror(errno));
 535                goto error;
 536        }
 537        if (!async->pid) {
 538                if (need_in)
 539                        close(fdin[1]);
 540                if (need_out)
 541                        close(fdout[0]);
 542                exit(!!async->proc(proc_in, proc_out, async->data));
 543        }
 544
 545        if (need_in)
 546                close(fdin[0]);
 547        else if (async->in)
 548                close(async->in);
 549
 550        if (need_out)
 551                close(fdout[1]);
 552        else if (async->out)
 553                close(async->out);
 554#else
 555        if (!main_thread_set) {
 556                /*
 557                 * We assume that the first time that start_async is called
 558                 * it is from the main thread.
 559                 */
 560                main_thread_set = 1;
 561                main_thread = pthread_self();
 562                pthread_key_create(&async_key, NULL);
 563                set_die_routine(die_async);
 564        }
 565
 566        if (proc_in >= 0)
 567                set_cloexec(proc_in);
 568        if (proc_out >= 0)
 569                set_cloexec(proc_out);
 570        async->proc_in = proc_in;
 571        async->proc_out = proc_out;
 572        {
 573                int err = pthread_create(&async->tid, NULL, run_thread, async);
 574                if (err) {
 575                        error("cannot create thread: %s", strerror(err));
 576                        goto error;
 577                }
 578        }
 579#endif
 580        return 0;
 581
 582error:
 583        if (need_in)
 584                close_pair(fdin);
 585        else if (async->in)
 586                close(async->in);
 587
 588        if (need_out)
 589                close_pair(fdout);
 590        else if (async->out)
 591                close(async->out);
 592        return -1;
 593}
 594
 595int finish_async(struct async *async)
 596{
 597#ifdef NO_PTHREADS
 598        return wait_or_whine(async->pid, "child process", 0);
 599#else
 600        void *ret = (void *)(intptr_t)(-1);
 601
 602        if (pthread_join(async->tid, &ret))
 603                error("pthread_join failed");
 604        return (int)(intptr_t)ret;
 605#endif
 606}
 607
 608int run_hook(const char *index_file, const char *name, ...)
 609{
 610        struct child_process hook;
 611        const char **argv = NULL, *env[2];
 612        char index[PATH_MAX];
 613        va_list args;
 614        int ret;
 615        size_t i = 0, alloc = 0;
 616
 617        if (access(git_path("hooks/%s", name), X_OK) < 0)
 618                return 0;
 619
 620        va_start(args, name);
 621        ALLOC_GROW(argv, i + 1, alloc);
 622        argv[i++] = git_path("hooks/%s", name);
 623        while (argv[i-1]) {
 624                ALLOC_GROW(argv, i + 1, alloc);
 625                argv[i++] = va_arg(args, const char *);
 626        }
 627        va_end(args);
 628
 629        memset(&hook, 0, sizeof(hook));
 630        hook.argv = argv;
 631        hook.no_stdin = 1;
 632        hook.stdout_to_stderr = 1;
 633        if (index_file) {
 634                snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
 635                env[0] = index;
 636                env[1] = NULL;
 637                hook.env = env;
 638        }
 639
 640        ret = run_command(&hook);
 641        free(argv);
 642        return ret;
 643}