]> Pileus Git - ~andy/git/blob - run-command.c
Windows: avoid the "dup dance" when spawning a child process
[~andy/git] / run-command.c
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
4
5 static inline void close_pair(int fd[2])
6 {
7         close(fd[0]);
8         close(fd[1]);
9 }
10
11 #ifndef WIN32
12 static 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
20 int start_command(struct child_process *cmd)
21 {
22         int need_in, need_out, need_err;
23         int fdin[2], fdout[2], fderr[2];
24         int failed_errno = failed_errno;
25
26         /*
27          * In case of errors we must keep the promise to close FDs
28          * that have been passed in via ->in and ->out.
29          */
30
31         need_in = !cmd->no_stdin && cmd->in < 0;
32         if (need_in) {
33                 if (pipe(fdin) < 0) {
34                         failed_errno = errno;
35                         if (cmd->out > 0)
36                                 close(cmd->out);
37                         goto fail_pipe;
38                 }
39                 cmd->in = fdin[1];
40         }
41
42         need_out = !cmd->no_stdout
43                 && !cmd->stdout_to_stderr
44                 && cmd->out < 0;
45         if (need_out) {
46                 if (pipe(fdout) < 0) {
47                         failed_errno = errno;
48                         if (need_in)
49                                 close_pair(fdin);
50                         else if (cmd->in)
51                                 close(cmd->in);
52                         goto fail_pipe;
53                 }
54                 cmd->out = fdout[0];
55         }
56
57         need_err = !cmd->no_stderr && cmd->err < 0;
58         if (need_err) {
59                 if (pipe(fderr) < 0) {
60                         failed_errno = errno;
61                         if (need_in)
62                                 close_pair(fdin);
63                         else if (cmd->in)
64                                 close(cmd->in);
65                         if (need_out)
66                                 close_pair(fdout);
67                         else if (cmd->out)
68                                 close(cmd->out);
69 fail_pipe:
70                         error("cannot create pipe for %s: %s",
71                                 cmd->argv[0], strerror(failed_errno));
72                         errno = failed_errno;
73                         return -1;
74                 }
75                 cmd->err = fderr[0];
76         }
77
78         trace_argv_printf(cmd->argv, "trace: run_command:");
79
80 #ifndef WIN32
81         fflush(NULL);
82         cmd->pid = fork();
83         if (!cmd->pid) {
84                 if (cmd->no_stdin)
85                         dup_devnull(0);
86                 else if (need_in) {
87                         dup2(fdin[0], 0);
88                         close_pair(fdin);
89                 } else if (cmd->in) {
90                         dup2(cmd->in, 0);
91                         close(cmd->in);
92                 }
93
94                 if (cmd->no_stderr)
95                         dup_devnull(2);
96                 else if (need_err) {
97                         dup2(fderr[1], 2);
98                         close_pair(fderr);
99                 }
100
101                 if (cmd->no_stdout)
102                         dup_devnull(1);
103                 else if (cmd->stdout_to_stderr)
104                         dup2(2, 1);
105                 else if (need_out) {
106                         dup2(fdout[1], 1);
107                         close_pair(fdout);
108                 } else if (cmd->out > 1) {
109                         dup2(cmd->out, 1);
110                         close(cmd->out);
111                 }
112
113                 if (cmd->dir && chdir(cmd->dir))
114                         die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
115                             cmd->dir);
116                 if (cmd->env) {
117                         for (; *cmd->env; cmd->env++) {
118                                 if (strchr(*cmd->env, '='))
119                                         putenv((char *)*cmd->env);
120                                 else
121                                         unsetenv(*cmd->env);
122                         }
123                 }
124                 if (cmd->preexec_cb)
125                         cmd->preexec_cb();
126                 if (cmd->git_cmd) {
127                         execv_git_cmd(cmd->argv);
128                 } else {
129                         execvp(cmd->argv[0], (char *const*) cmd->argv);
130                 }
131                 trace_printf("trace: exec '%s' failed: %s\n", cmd->argv[0],
132                                 strerror(errno));
133                 exit(127);
134         }
135         if (cmd->pid < 0)
136                 error("cannot fork() for %s: %s", cmd->argv[0],
137                         strerror(failed_errno = errno));
138 #else
139 {
140         int fhin = 0, fhout = 1, fherr = 2;
141         const char **sargv = cmd->argv;
142         char **env = environ;
143
144         if (cmd->no_stdin)
145                 fhin = open("/dev/null", O_RDWR);
146         else if (need_in)
147                 fhin = dup(fdin[0]);
148         else if (cmd->in)
149                 fhin = dup(cmd->in);
150
151         if (cmd->no_stderr)
152                 fherr = open("/dev/null", O_RDWR);
153         else if (need_err)
154                 fherr = dup(fderr[1]);
155
156         if (cmd->no_stdout)
157                 fhout = open("/dev/null", O_RDWR);
158         else if (cmd->stdout_to_stderr)
159                 fhout = dup(fherr);
160         else if (need_out)
161                 fhout = dup(fdout[1]);
162         else if (cmd->out > 1)
163                 fhout = dup(cmd->out);
164
165         if (cmd->dir)
166                 die("chdir in start_command() not implemented");
167         if (cmd->env)
168                 env = make_augmented_environ(cmd->env);
169
170         if (cmd->git_cmd) {
171                 cmd->argv = prepare_git_cmd(cmd->argv);
172         }
173
174         cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env,
175                                   fhin, fhout, fherr);
176         failed_errno = errno;
177         if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
178                 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
179
180         if (cmd->env)
181                 free_environ(env);
182         if (cmd->git_cmd)
183                 free(cmd->argv);
184
185         cmd->argv = sargv;
186         if (fhin != 0)
187                 close(fhin);
188         if (fhout != 1)
189                 close(fhout);
190         if (fherr != 2)
191                 close(fherr);
192 }
193 #endif
194
195         if (cmd->pid < 0) {
196                 if (need_in)
197                         close_pair(fdin);
198                 else if (cmd->in)
199                         close(cmd->in);
200                 if (need_out)
201                         close_pair(fdout);
202                 else if (cmd->out)
203                         close(cmd->out);
204                 if (need_err)
205                         close_pair(fderr);
206                 errno = failed_errno;
207                 return -1;
208         }
209
210         if (need_in)
211                 close(fdin[0]);
212         else if (cmd->in)
213                 close(cmd->in);
214
215         if (need_out)
216                 close(fdout[1]);
217         else if (cmd->out)
218                 close(cmd->out);
219
220         if (need_err)
221                 close(fderr[1]);
222
223         return 0;
224 }
225
226 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
227 {
228         int status, code = -1;
229         pid_t waiting;
230         int failed_errno = 0;
231
232         while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
233                 ;       /* nothing */
234
235         if (waiting < 0) {
236                 failed_errno = errno;
237                 error("waitpid for %s failed: %s", argv0, strerror(errno));
238         } else if (waiting != pid) {
239                 error("waitpid is confused (%s)", argv0);
240         } else if (WIFSIGNALED(status)) {
241                 code = WTERMSIG(status);
242                 error("%s died of signal %d", argv0, code);
243                 /*
244                  * This return value is chosen so that code & 0xff
245                  * mimics the exit code that a POSIX shell would report for
246                  * a program that died from this signal.
247                  */
248                 code -= 128;
249         } else if (WIFEXITED(status)) {
250                 code = WEXITSTATUS(status);
251                 /*
252                  * Convert special exit code when execvp failed.
253                  */
254                 if (code == 127) {
255                         code = -1;
256                         failed_errno = ENOENT;
257                         if (!silent_exec_failure)
258                                 error("cannot run %s: %s", argv0,
259                                         strerror(ENOENT));
260                 }
261         } else {
262                 error("waitpid is confused (%s)", argv0);
263         }
264         errno = failed_errno;
265         return code;
266 }
267
268 int finish_command(struct child_process *cmd)
269 {
270         return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
271 }
272
273 int run_command(struct child_process *cmd)
274 {
275         int code = start_command(cmd);
276         if (code)
277                 return code;
278         return finish_command(cmd);
279 }
280
281 static void prepare_run_command_v_opt(struct child_process *cmd,
282                                       const char **argv,
283                                       int opt)
284 {
285         memset(cmd, 0, sizeof(*cmd));
286         cmd->argv = argv;
287         cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
288         cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
289         cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
290         cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
291 }
292
293 int run_command_v_opt(const char **argv, int opt)
294 {
295         struct child_process cmd;
296         prepare_run_command_v_opt(&cmd, argv, opt);
297         return run_command(&cmd);
298 }
299
300 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
301 {
302         struct child_process cmd;
303         prepare_run_command_v_opt(&cmd, argv, opt);
304         cmd.dir = dir;
305         cmd.env = env;
306         return run_command(&cmd);
307 }
308
309 #ifdef WIN32
310 static unsigned __stdcall run_thread(void *data)
311 {
312         struct async *async = data;
313         return async->proc(async->fd_for_proc, async->data);
314 }
315 #endif
316
317 int start_async(struct async *async)
318 {
319         int pipe_out[2];
320
321         if (pipe(pipe_out) < 0)
322                 return error("cannot create pipe: %s", strerror(errno));
323         async->out = pipe_out[0];
324
325 #ifndef WIN32
326         /* Flush stdio before fork() to avoid cloning buffers */
327         fflush(NULL);
328
329         async->pid = fork();
330         if (async->pid < 0) {
331                 error("fork (async) failed: %s", strerror(errno));
332                 close_pair(pipe_out);
333                 return -1;
334         }
335         if (!async->pid) {
336                 close(pipe_out[0]);
337                 exit(!!async->proc(pipe_out[1], async->data));
338         }
339         close(pipe_out[1]);
340 #else
341         async->fd_for_proc = pipe_out[1];
342         async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
343         if (!async->tid) {
344                 error("cannot create thread: %s", strerror(errno));
345                 close_pair(pipe_out);
346                 return -1;
347         }
348 #endif
349         return 0;
350 }
351
352 int finish_async(struct async *async)
353 {
354 #ifndef WIN32
355         int ret = wait_or_whine(async->pid, "child process", 0);
356 #else
357         DWORD ret = 0;
358         if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
359                 ret = error("waiting for thread failed: %lu", GetLastError());
360         else if (!GetExitCodeThread(async->tid, &ret))
361                 ret = error("cannot get thread exit code: %lu", GetLastError());
362         CloseHandle(async->tid);
363 #endif
364         return ret;
365 }
366
367 int run_hook(const char *index_file, const char *name, ...)
368 {
369         struct child_process hook;
370         const char **argv = NULL, *env[2];
371         char index[PATH_MAX];
372         va_list args;
373         int ret;
374         size_t i = 0, alloc = 0;
375
376         if (access(git_path("hooks/%s", name), X_OK) < 0)
377                 return 0;
378
379         va_start(args, name);
380         ALLOC_GROW(argv, i + 1, alloc);
381         argv[i++] = git_path("hooks/%s", name);
382         while (argv[i-1]) {
383                 ALLOC_GROW(argv, i + 1, alloc);
384                 argv[i++] = va_arg(args, const char *);
385         }
386         va_end(args);
387
388         memset(&hook, 0, sizeof(hook));
389         hook.argv = argv;
390         hook.no_stdin = 1;
391         hook.stdout_to_stderr = 1;
392         if (index_file) {
393                 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
394                 env[0] = index;
395                 env[1] = NULL;
396                 hook.env = env;
397         }
398
399         ret = run_command(&hook);
400         free(argv);
401         return ret;
402 }