run-command.con commit Add ability to specify environment extension to run_command (ee49314)
   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;
  21        int fdin[2], fdout[2];
  22
  23        need_in = !cmd->no_stdin && cmd->in < 0;
  24        if (need_in) {
  25                if (pipe(fdin) < 0)
  26                        return -ERR_RUN_COMMAND_PIPE;
  27                cmd->in = fdin[1];
  28                cmd->close_in = 1;
  29        }
  30
  31        need_out = !cmd->no_stdout
  32                && !cmd->stdout_to_stderr
  33                && cmd->out < 0;
  34        if (need_out) {
  35                if (pipe(fdout) < 0) {
  36                        if (need_in)
  37                                close_pair(fdin);
  38                        return -ERR_RUN_COMMAND_PIPE;
  39                }
  40                cmd->out = fdout[0];
  41                cmd->close_out = 1;
  42        }
  43
  44        cmd->pid = fork();
  45        if (cmd->pid < 0) {
  46                if (need_in)
  47                        close_pair(fdin);
  48                if (need_out)
  49                        close_pair(fdout);
  50                return -ERR_RUN_COMMAND_FORK;
  51        }
  52
  53        if (!cmd->pid) {
  54                if (cmd->no_stdin)
  55                        dup_devnull(0);
  56                else if (need_in) {
  57                        dup2(fdin[0], 0);
  58                        close_pair(fdin);
  59                } else if (cmd->in) {
  60                        dup2(cmd->in, 0);
  61                        close(cmd->in);
  62                }
  63
  64                if (cmd->no_stdout)
  65                        dup_devnull(1);
  66                else if (cmd->stdout_to_stderr)
  67                        dup2(2, 1);
  68                else if (need_out) {
  69                        dup2(fdout[1], 1);
  70                        close_pair(fdout);
  71                } else if (cmd->out > 1) {
  72                        dup2(cmd->out, 1);
  73                        close(cmd->out);
  74                }
  75
  76                if (cmd->dir && chdir(cmd->dir))
  77                        die("exec %s: cd to %s failed (%s)", cmd->argv[0],
  78                            cmd->dir, strerror(errno));
  79                if (cmd->env) {
  80                        for (; *cmd->env; cmd->env++)
  81                                putenv((char*)*cmd->env);
  82                }
  83                if (cmd->git_cmd) {
  84                        execv_git_cmd(cmd->argv);
  85                } else {
  86                        execvp(cmd->argv[0], (char *const*) cmd->argv);
  87                }
  88                die("exec %s failed.", cmd->argv[0]);
  89        }
  90
  91        if (need_in)
  92                close(fdin[0]);
  93        else if (cmd->in)
  94                close(cmd->in);
  95
  96        if (need_out)
  97                close(fdout[1]);
  98        else if (cmd->out > 1)
  99                close(cmd->out);
 100
 101        return 0;
 102}
 103
 104int finish_command(struct child_process *cmd)
 105{
 106        if (cmd->close_in)
 107                close(cmd->in);
 108        if (cmd->close_out)
 109                close(cmd->out);
 110
 111        for (;;) {
 112                int status, code;
 113                pid_t waiting = waitpid(cmd->pid, &status, 0);
 114
 115                if (waiting < 0) {
 116                        if (errno == EINTR)
 117                                continue;
 118                        error("waitpid failed (%s)", strerror(errno));
 119                        return -ERR_RUN_COMMAND_WAITPID;
 120                }
 121                if (waiting != cmd->pid)
 122                        return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
 123                if (WIFSIGNALED(status))
 124                        return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
 125
 126                if (!WIFEXITED(status))
 127                        return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
 128                code = WEXITSTATUS(status);
 129                if (code)
 130                        return -code;
 131                return 0;
 132        }
 133}
 134
 135int run_command(struct child_process *cmd)
 136{
 137        int code = start_command(cmd);
 138        if (code)
 139                return code;
 140        return finish_command(cmd);
 141}
 142
 143static void prepare_run_command_v_opt(struct child_process *cmd,
 144                                      const char **argv,
 145                                      int opt)
 146{
 147        memset(cmd, 0, sizeof(*cmd));
 148        cmd->argv = argv;
 149        cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
 150        cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
 151        cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
 152}
 153
 154int run_command_v_opt(const char **argv, int opt)
 155{
 156        struct child_process cmd;
 157        prepare_run_command_v_opt(&cmd, argv, opt);
 158        return run_command(&cmd);
 159}
 160
 161int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
 162{
 163        struct child_process cmd;
 164        prepare_run_command_v_opt(&cmd, argv, opt);
 165        cmd.dir = dir;
 166        return run_command(&cmd);
 167}
 168
 169int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
 170{
 171        struct child_process cmd;
 172        prepare_run_command_v_opt(&cmd, argv, opt);
 173        cmd.dir = dir;
 174        cmd.env = env;
 175        return run_command(&cmd);
 176}