Use start_comand() in builtin-fetch-pack.c instead of explicit fork/exec.
[git/git.git] / run-command.c
CommitLineData
b1bf95bb
JW
1#include "cache.h"
2#include "run-command.h"
77cb17e9 3#include "exec_cmd.h"
b1bf95bb 4
9dc09c76
SP
5static inline void close_pair(int fd[2])
6{
7 close(fd[0]);
8 close(fd[1]);
9}
10
e4507ae8
SP
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
ebcb5d16 18int start_command(struct child_process *cmd)
b1bf95bb 19{
e4507ae8 20 int need_in, need_out;
f4bba25b 21 int fdin[2], fdout[2];
4919bf03 22
e4507ae8 23 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03
SP
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
e4507ae8
SP
31 need_out = !cmd->no_stdout
32 && !cmd->stdout_to_stderr
33 && cmd->out < 0;
f4bba25b
SP
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
ebcb5d16 44 cmd->pid = fork();
4919bf03 45 if (cmd->pid < 0) {
9dc09c76
SP
46 if (need_in)
47 close_pair(fdin);
f4bba25b
SP
48 if (need_out)
49 close_pair(fdout);
b1bf95bb 50 return -ERR_RUN_COMMAND_FORK;
4919bf03
SP
51 }
52
ebcb5d16 53 if (!cmd->pid) {
e4507ae8
SP
54 if (cmd->no_stdin)
55 dup_devnull(0);
56 else if (need_in) {
4919bf03 57 dup2(fdin[0], 0);
9dc09c76 58 close_pair(fdin);
4919bf03
SP
59 } else if (cmd->in) {
60 dup2(cmd->in, 0);
61 close(cmd->in);
95d3c4f5 62 }
4919bf03 63
e4507ae8
SP
64 if (cmd->no_stdout)
65 dup_devnull(1);
66 else if (cmd->stdout_to_stderr)
cd83c74c 67 dup2(2, 1);
f4bba25b
SP
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
1568fea0
AR
76 if (cmd->dir && chdir(cmd->dir))
77 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
78 cmd->dir, strerror(errno));
ee493148 79 if (cmd->env) {
3427b375
AR
80 for (; *cmd->env; cmd->env++) {
81 if (strchr(*cmd->env, '='))
82 putenv((char*)*cmd->env);
83 else
84 unsetenv(*cmd->env);
85 }
ee493148 86 }
f1000898
SP
87 if (cmd->git_cmd) {
88 execv_git_cmd(cmd->argv);
77cb17e9 89 } else {
f1000898 90 execvp(cmd->argv[0], (char *const*) cmd->argv);
128aed68 91 }
f1000898 92 die("exec %s failed.", cmd->argv[0]);
b1bf95bb 93 }
4919bf03
SP
94
95 if (need_in)
96 close(fdin[0]);
97 else if (cmd->in)
98 close(cmd->in);
99
f4bba25b
SP
100 if (need_out)
101 close(fdout[1]);
102 else if (cmd->out > 1)
103 close(cmd->out);
104
ebcb5d16
SP
105 return 0;
106}
107
108int finish_command(struct child_process *cmd)
109{
4919bf03
SP
110 if (cmd->close_in)
111 close(cmd->in);
f4bba25b
SP
112 if (cmd->close_out)
113 close(cmd->out);
4919bf03 114
b1bf95bb
JW
115 for (;;) {
116 int status, code;
ebcb5d16 117 pid_t waiting = waitpid(cmd->pid, &status, 0);
b1bf95bb 118
6f002f98 119 if (waiting < 0) {
b1bf95bb
JW
120 if (errno == EINTR)
121 continue;
6f002f98 122 error("waitpid failed (%s)", strerror(errno));
b1bf95bb
JW
123 return -ERR_RUN_COMMAND_WAITPID;
124 }
ebcb5d16 125 if (waiting != cmd->pid)
b1bf95bb
JW
126 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
127 if (WIFSIGNALED(status))
128 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
129
130 if (!WIFEXITED(status))
131 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
132 code = WEXITSTATUS(status);
133 if (code)
134 return -code;
135 return 0;
136 }
137}
f1000898 138
ebcb5d16
SP
139int run_command(struct child_process *cmd)
140{
141 int code = start_command(cmd);
142 if (code)
143 return code;
144 return finish_command(cmd);
145}
146
1568fea0 147static void prepare_run_command_v_opt(struct child_process *cmd,
ee493148
AR
148 const char **argv,
149 int opt)
1568fea0
AR
150{
151 memset(cmd, 0, sizeof(*cmd));
152 cmd->argv = argv;
153 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
154 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
155 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
156}
157
f1000898
SP
158int run_command_v_opt(const char **argv, int opt)
159{
160 struct child_process cmd;
1568fea0
AR
161 prepare_run_command_v_opt(&cmd, argv, opt);
162 return run_command(&cmd);
163}
164
165int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
166{
167 struct child_process cmd;
168 prepare_run_command_v_opt(&cmd, argv, opt);
169 cmd.dir = dir;
f1000898
SP
170 return run_command(&cmd);
171}
ee493148
AR
172
173int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
174{
175 struct child_process cmd;
176 prepare_run_command_v_opt(&cmd, argv, opt);
177 cmd.dir = dir;
178 cmd.env = env;
179 return run_command(&cmd);
180}