run-command.con commit run-command (Windows): Run dashless "git <cmd>" (108ac31)
   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->git_cmd) {
 114                        execv_git_cmd(cmd->argv);
 115                } else {
 116                        execvp(cmd->argv[0], (char *const*) cmd->argv);
 117                }
 118                die("exec %s failed.", cmd->argv[0]);
 119        }
 120#else
 121        int s0 = -1, s1 = -1, s2 = -1;  /* backups of stdin, stdout, stderr */
 122        const char **sargv = cmd->argv;
 123        char **env = environ;
 124
 125        if (cmd->no_stdin) {
 126                s0 = dup(0);
 127                dup_devnull(0);
 128        } else if (need_in) {
 129                s0 = dup(0);
 130                dup2(fdin[0], 0);
 131        } else if (cmd->in) {
 132                s0 = dup(0);
 133                dup2(cmd->in, 0);
 134        }
 135
 136        if (cmd->no_stderr) {
 137                s2 = dup(2);
 138                dup_devnull(2);
 139        } else if (need_err) {
 140                s2 = dup(2);
 141                dup2(fderr[1], 2);
 142        }
 143
 144        if (cmd->no_stdout) {
 145                s1 = dup(1);
 146                dup_devnull(1);
 147        } else if (cmd->stdout_to_stderr) {
 148                s1 = dup(1);
 149                dup2(2, 1);
 150        } else if (need_out) {
 151                s1 = dup(1);
 152                dup2(fdout[1], 1);
 153        } else if (cmd->out > 1) {
 154                s1 = dup(1);
 155                dup2(cmd->out, 1);
 156        }
 157
 158        if (cmd->dir)
 159                die("chdir in start_command() not implemented");
 160        if (cmd->env) {
 161                env = copy_environ();
 162                for (; *cmd->env; cmd->env++)
 163                        env = env_setenv(env, *cmd->env);
 164        }
 165
 166        if (cmd->git_cmd) {
 167                cmd->argv = prepare_git_cmd(cmd->argv);
 168        }
 169
 170        cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
 171
 172        if (cmd->env)
 173                free_environ(env);
 174        if (cmd->git_cmd)
 175                free(cmd->argv);
 176
 177        cmd->argv = sargv;
 178        if (s0 >= 0)
 179                dup2(s0, 0), close(s0);
 180        if (s1 >= 0)
 181                dup2(s1, 1), close(s1);
 182        if (s2 >= 0)
 183                dup2(s2, 2), close(s2);
 184#endif
 185
 186        if (cmd->pid < 0) {
 187                if (need_in)
 188                        close_pair(fdin);
 189                else if (cmd->in)
 190                        close(cmd->in);
 191                if (need_out)
 192                        close_pair(fdout);
 193                else if (cmd->out)
 194                        close(cmd->out);
 195                if (need_err)
 196                        close_pair(fderr);
 197                return -ERR_RUN_COMMAND_FORK;
 198        }
 199
 200        if (need_in)
 201                close(fdin[0]);
 202        else if (cmd->in)
 203                close(cmd->in);
 204
 205        if (need_out)
 206                close(fdout[1]);
 207        else if (cmd->out)
 208                close(cmd->out);
 209
 210        if (need_err)
 211                close(fderr[1]);
 212
 213        return 0;
 214}
 215
 216static int wait_or_whine(pid_t pid)
 217{
 218        for (;;) {
 219                int status, code;
 220                pid_t waiting = waitpid(pid, &status, 0);
 221
 222                if (waiting < 0) {
 223                        if (errno == EINTR)
 224                                continue;
 225                        error("waitpid failed (%s)", strerror(errno));
 226                        return -ERR_RUN_COMMAND_WAITPID;
 227                }
 228                if (waiting != pid)
 229                        return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
 230                if (WIFSIGNALED(status))
 231                        return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
 232
 233                if (!WIFEXITED(status))
 234                        return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
 235                code = WEXITSTATUS(status);
 236                if (code)
 237                        return -code;
 238                return 0;
 239        }
 240}
 241
 242int finish_command(struct child_process *cmd)
 243{
 244        return wait_or_whine(cmd->pid);
 245}
 246
 247int run_command(struct child_process *cmd)
 248{
 249        int code = start_command(cmd);
 250        if (code)
 251                return code;
 252        return finish_command(cmd);
 253}
 254
 255static void prepare_run_command_v_opt(struct child_process *cmd,
 256                                      const char **argv,
 257                                      int opt)
 258{
 259        memset(cmd, 0, sizeof(*cmd));
 260        cmd->argv = argv;
 261        cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
 262        cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
 263        cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
 264}
 265
 266int run_command_v_opt(const char **argv, int opt)
 267{
 268        struct child_process cmd;
 269        prepare_run_command_v_opt(&cmd, argv, opt);
 270        return run_command(&cmd);
 271}
 272
 273int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
 274{
 275        struct child_process cmd;
 276        prepare_run_command_v_opt(&cmd, argv, opt);
 277        cmd.dir = dir;
 278        return run_command(&cmd);
 279}
 280
 281int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
 282{
 283        struct child_process cmd;
 284        prepare_run_command_v_opt(&cmd, argv, opt);
 285        cmd.dir = dir;
 286        cmd.env = env;
 287        return run_command(&cmd);
 288}
 289
 290#ifdef __MINGW32__
 291static __stdcall unsigned run_thread(void *data)
 292{
 293        struct async *async = data;
 294        return async->proc(async->fd_for_proc, async->data);
 295}
 296#endif
 297
 298int start_async(struct async *async)
 299{
 300        int pipe_out[2];
 301
 302        if (pipe(pipe_out) < 0)
 303                return error("cannot create pipe: %s", strerror(errno));
 304        async->out = pipe_out[0];
 305
 306#ifndef __MINGW32__
 307        async->pid = fork();
 308        if (async->pid < 0) {
 309                error("fork (async) failed: %s", strerror(errno));
 310                close_pair(pipe_out);
 311                return -1;
 312        }
 313        if (!async->pid) {
 314                close(pipe_out[0]);
 315                exit(!!async->proc(pipe_out[1], async->data));
 316        }
 317        close(pipe_out[1]);
 318#else
 319        async->fd_for_proc = pipe_out[1];
 320        async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
 321        if (!async->tid) {
 322                error("cannot create thread: %s", strerror(errno));
 323                close_pair(pipe_out);
 324                return -1;
 325        }
 326#endif
 327        return 0;
 328}
 329
 330int finish_async(struct async *async)
 331{
 332#ifndef __MINGW32__
 333        int ret = 0;
 334
 335        if (wait_or_whine(async->pid))
 336                ret = error("waitpid (async) failed");
 337#else
 338        DWORD ret = 0;
 339        if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
 340                ret = error("waiting for thread failed: %lu", GetLastError());
 341        else if (!GetExitCodeThread(async->tid, &ret))
 342                ret = error("cannot get thread exit code: %lu", GetLastError());
 343        CloseHandle(async->tid);
 344#endif
 345        return ret;
 346}