run-command.con commit run-command: add pre-exec callback (ccf08bc)
   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
  11static inline void dup_devnull(int to)
  12{
  13        int fd = open("/dev/null", O_RDWR);
  14        dup2(fd, to);
  15        close(fd);
  16}
  17
  18int start_command(struct child_process *cmd)
  19{
  20        int need_in, need_out, need_err;
  21        int fdin[2], fdout[2], fderr[2];
  22
  23        /*
  24         * In case of errors we must keep the promise to close FDs
  25         * that have been passed in via ->in and ->out.
  26         */
  27
  28        need_in = !cmd->no_stdin && cmd->in < 0;
  29        if (need_in) {
  30                if (pipe(fdin) < 0) {
  31                        if (cmd->out > 0)
  32                                close(cmd->out);
  33                        return -ERR_RUN_COMMAND_PIPE;
  34                }
  35                cmd->in = fdin[1];
  36        }
  37
  38        need_out = !cmd->no_stdout
  39                && !cmd->stdout_to_stderr
  40                && cmd->out < 0;
  41        if (need_out) {
  42                if (pipe(fdout) < 0) {
  43                        if (need_in)
  44                                close_pair(fdin);
  45                        else if (cmd->in)
  46                                close(cmd->in);
  47                        return -ERR_RUN_COMMAND_PIPE;
  48                }
  49                cmd->out = fdout[0];
  50        }
  51
  52        need_err = !cmd->no_stderr && cmd->err < 0;
  53        if (need_err) {
  54                if (pipe(fderr) < 0) {
  55                        if (need_in)
  56                                close_pair(fdin);
  57                        else if (cmd->in)
  58                                close(cmd->in);
  59                        if (need_out)
  60                                close_pair(fdout);
  61                        else if (cmd->out)
  62                                close(cmd->out);
  63                        return -ERR_RUN_COMMAND_PIPE;
  64                }
  65                cmd->err = fderr[0];
  66        }
  67
  68        trace_argv_printf(cmd->argv, "trace: run_command:");
  69
  70#ifndef __MINGW32__
  71        cmd->pid = fork();
  72        if (!cmd->pid) {
  73                if (cmd->no_stdin)
  74                        dup_devnull(0);
  75                else if (need_in) {
  76                        dup2(fdin[0], 0);
  77                        close_pair(fdin);
  78                } else if (cmd->in) {
  79                        dup2(cmd->in, 0);
  80                        close(cmd->in);
  81                }
  82
  83                if (cmd->no_stderr)
  84                        dup_devnull(2);
  85                else if (need_err) {
  86                        dup2(fderr[1], 2);
  87                        close_pair(fderr);
  88                }
  89
  90                if (cmd->no_stdout)
  91                        dup_devnull(1);
  92                else if (cmd->stdout_to_stderr)
  93                        dup2(2, 1);
  94                else if (need_out) {
  95                        dup2(fdout[1], 1);
  96                        close_pair(fdout);
  97                } else if (cmd->out > 1) {
  98                        dup2(cmd->out, 1);
  99                        close(cmd->out);
 100                }
 101
 102                if (cmd->dir && chdir(cmd->dir))
 103                        die("exec %s: cd to %s failed (%s)", cmd->argv[0],
 104                            cmd->dir, strerror(errno));
 105                if (cmd->env) {
 106                        for (; *cmd->env; cmd->env++) {
 107                                if (strchr(*cmd->env, '='))
 108                                        putenv((char*)*cmd->env);
 109                                else
 110                                        unsetenv(*cmd->env);
 111                        }
 112                }
 113                if (cmd->preexec_cb)
 114                        cmd->preexec_cb();
 115                if (cmd->git_cmd) {
 116                        execv_git_cmd(cmd->argv);
 117                } else {
 118                        execvp(cmd->argv[0], (char *const*) cmd->argv);
 119                }
 120                die("exec %s failed.", cmd->argv[0]);
 121        }
 122#else
 123        int s0 = -1, s1 = -1, s2 = -1;  /* backups of stdin, stdout, stderr */
 124        const char *sargv0 = cmd->argv[0];
 125        char **env = environ;
 126        struct strbuf git_cmd;
 127
 128        if (cmd->no_stdin) {
 129                s0 = dup(0);
 130                dup_devnull(0);
 131        } else if (need_in) {
 132                s0 = dup(0);
 133                dup2(fdin[0], 0);
 134        } else if (cmd->in) {
 135                s0 = dup(0);
 136                dup2(cmd->in, 0);
 137        }
 138
 139        if (cmd->no_stderr) {
 140                s2 = dup(2);
 141                dup_devnull(2);
 142        } else if (need_err) {
 143                s2 = dup(2);
 144                dup2(fderr[1], 2);
 145        }
 146
 147        if (cmd->no_stdout) {
 148                s1 = dup(1);
 149                dup_devnull(1);
 150        } else if (cmd->stdout_to_stderr) {
 151                s1 = dup(1);
 152                dup2(2, 1);
 153        } else if (need_out) {
 154                s1 = dup(1);
 155                dup2(fdout[1], 1);
 156        } else if (cmd->out > 1) {
 157                s1 = dup(1);
 158                dup2(cmd->out, 1);
 159        }
 160
 161        if (cmd->dir)
 162                die("chdir in start_command() not implemented");
 163        if (cmd->env) {
 164                env = copy_environ();
 165                for (; *cmd->env; cmd->env++)
 166                        env = env_setenv(env, *cmd->env);
 167        }
 168
 169        if (cmd->git_cmd) {
 170                strbuf_init(&git_cmd, 0);
 171                strbuf_addf(&git_cmd, "git-%s", cmd->argv[0]);
 172                cmd->argv[0] = git_cmd.buf;
 173        }
 174
 175        cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
 176
 177        if (cmd->env)
 178                free_environ(env);
 179        if (cmd->git_cmd)
 180                strbuf_release(&git_cmd);
 181
 182        cmd->argv[0] = sargv0;
 183        if (s0 >= 0)
 184                dup2(s0, 0), close(s0);
 185        if (s1 >= 0)
 186                dup2(s1, 1), close(s1);
 187        if (s2 >= 0)
 188                dup2(s2, 2), close(s2);
 189#endif
 190
 191        if (cmd->pid < 0) {
 192                if (need_in)
 193                        close_pair(fdin);
 194                else if (cmd->in)
 195                        close(cmd->in);
 196                if (need_out)
 197                        close_pair(fdout);
 198                else if (cmd->out)
 199                        close(cmd->out);
 200                if (need_err)
 201                        close_pair(fderr);
 202                return -ERR_RUN_COMMAND_FORK;
 203        }
 204
 205        if (need_in)
 206                close(fdin[0]);
 207        else if (cmd->in)
 208                close(cmd->in);
 209
 210        if (need_out)
 211                close(fdout[1]);
 212        else if (cmd->out)
 213                close(cmd->out);
 214
 215        if (need_err)
 216                close(fderr[1]);
 217
 218        return 0;
 219}
 220
 221static int wait_or_whine(pid_t pid)
 222{
 223        for (;;) {
 224                int status, code;
 225                pid_t waiting = waitpid(pid, &status, 0);
 226
 227                if (waiting < 0) {
 228                        if (errno == EINTR)
 229                                continue;
 230                        error("waitpid failed (%s)", strerror(errno));
 231                        return -ERR_RUN_COMMAND_WAITPID;
 232                }
 233                if (waiting != pid)
 234                        return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
 235                if (WIFSIGNALED(status))
 236                        return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
 237
 238                if (!WIFEXITED(status))
 239                        return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
 240                code = WEXITSTATUS(status);
 241                if (code)
 242                        return -code;
 243                return 0;
 244        }
 245}
 246
 247int finish_command(struct child_process *cmd)
 248{
 249        return wait_or_whine(cmd->pid);
 250}
 251
 252int run_command(struct child_process *cmd)
 253{
 254        int code = start_command(cmd);
 255        if (code)
 256                return code;
 257        return finish_command(cmd);
 258}
 259
 260static void prepare_run_command_v_opt(struct child_process *cmd,
 261                                      const char **argv,
 262                                      int opt)
 263{
 264        memset(cmd, 0, sizeof(*cmd));
 265        cmd->argv = argv;
 266        cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
 267        cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
 268        cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
 269}
 270
 271int run_command_v_opt(const char **argv, int opt)
 272{
 273        struct child_process cmd;
 274        prepare_run_command_v_opt(&cmd, argv, opt);
 275        return run_command(&cmd);
 276}
 277
 278int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
 279{
 280        struct child_process cmd;
 281        prepare_run_command_v_opt(&cmd, argv, opt);
 282        cmd.dir = dir;
 283        return run_command(&cmd);
 284}
 285
 286int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
 287{
 288        struct child_process cmd;
 289        prepare_run_command_v_opt(&cmd, argv, opt);
 290        cmd.dir = dir;
 291        cmd.env = env;
 292        return run_command(&cmd);
 293}
 294
 295#ifdef __MINGW32__
 296static __stdcall unsigned run_thread(void *data)
 297{
 298        struct async *async = data;
 299        return async->proc(async->fd_for_proc, async->data);
 300}
 301#endif
 302
 303int start_async(struct async *async)
 304{
 305        int pipe_out[2];
 306
 307        if (pipe(pipe_out) < 0)
 308                return error("cannot create pipe: %s", strerror(errno));
 309        async->out = pipe_out[0];
 310
 311#ifndef __MINGW32__
 312        async->pid = fork();
 313        if (async->pid < 0) {
 314                error("fork (async) failed: %s", strerror(errno));
 315                close_pair(pipe_out);
 316                return -1;
 317        }
 318        if (!async->pid) {
 319                close(pipe_out[0]);
 320                exit(!!async->proc(pipe_out[1], async->data));
 321        }
 322        close(pipe_out[1]);
 323#else
 324        async->fd_for_proc = pipe_out[1];
 325        async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
 326        if (!async->tid) {
 327                error("cannot create thread: %s", strerror(errno));
 328                close_pair(pipe_out);
 329                return -1;
 330        }
 331#endif
 332        return 0;
 333}
 334
 335int finish_async(struct async *async)
 336{
 337#ifndef __MINGW32__
 338        int ret = 0;
 339
 340        if (wait_or_whine(async->pid))
 341                ret = error("waitpid (async) failed");
 342#else
 343        DWORD ret = 0;
 344        if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
 345                ret = error("waiting for thread failed: %lu", GetLastError());
 346        else if (!GetExitCodeThread(async->tid, &ret))
 347                ret = error("cannot get thread exit code: %lu", GetLastError());
 348        CloseHandle(async->tid);
 349#endif
 350        return ret;
 351}