]> Pileus Git - ~andy/git/blob - run-command.c
start_command: detect execvp failures early
[~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 static inline void dup_devnull(int to)
12 {
13         int fd = open("/dev/null", O_RDWR);
14         dup2(fd, to);
15         close(fd);
16 }
17
18 #ifndef WIN32
19 static int child_err = 2;
20 static int child_notifier = -1;
21
22 static void notify_parent(void)
23 {
24         write(child_notifier, "", 1);
25 }
26
27 static NORETURN void die_child(const char *err, va_list params)
28 {
29         char msg[4096];
30         int len = vsnprintf(msg, sizeof(msg), err, params);
31         if (len > sizeof(msg))
32                 len = sizeof(msg);
33
34         write(child_err, "fatal: ", 7);
35         write(child_err, msg, len);
36         write(child_err, "\n", 1);
37         exit(128);
38 }
39
40 static inline void set_cloexec(int fd)
41 {
42         int flags = fcntl(fd, F_GETFD);
43         if (flags >= 0)
44                 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
45 }
46 #endif
47
48 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
49 {
50         int status, code = -1;
51         pid_t waiting;
52         int failed_errno = 0;
53
54         while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
55                 ;       /* nothing */
56
57         if (waiting < 0) {
58                 failed_errno = errno;
59                 error("waitpid for %s failed: %s", argv0, strerror(errno));
60         } else if (waiting != pid) {
61                 error("waitpid is confused (%s)", argv0);
62         } else if (WIFSIGNALED(status)) {
63                 code = WTERMSIG(status);
64                 error("%s died of signal %d", argv0, code);
65                 /*
66                  * This return value is chosen so that code & 0xff
67                  * mimics the exit code that a POSIX shell would report for
68                  * a program that died from this signal.
69                  */
70                 code -= 128;
71         } else if (WIFEXITED(status)) {
72                 code = WEXITSTATUS(status);
73                 /*
74                  * Convert special exit code when execvp failed.
75                  */
76                 if (code == 127) {
77                         code = -1;
78                         failed_errno = ENOENT;
79                         if (!silent_exec_failure)
80                                 error("cannot run %s: %s", argv0,
81                                         strerror(ENOENT));
82                 }
83         } else {
84                 error("waitpid is confused (%s)", argv0);
85         }
86         errno = failed_errno;
87         return code;
88 }
89
90 int start_command(struct child_process *cmd)
91 {
92         int need_in, need_out, need_err;
93         int fdin[2], fdout[2], fderr[2];
94         int failed_errno = failed_errno;
95
96         /*
97          * In case of errors we must keep the promise to close FDs
98          * that have been passed in via ->in and ->out.
99          */
100
101         need_in = !cmd->no_stdin && cmd->in < 0;
102         if (need_in) {
103                 if (pipe(fdin) < 0) {
104                         failed_errno = errno;
105                         if (cmd->out > 0)
106                                 close(cmd->out);
107                         goto fail_pipe;
108                 }
109                 cmd->in = fdin[1];
110         }
111
112         need_out = !cmd->no_stdout
113                 && !cmd->stdout_to_stderr
114                 && cmd->out < 0;
115         if (need_out) {
116                 if (pipe(fdout) < 0) {
117                         failed_errno = errno;
118                         if (need_in)
119                                 close_pair(fdin);
120                         else if (cmd->in)
121                                 close(cmd->in);
122                         goto fail_pipe;
123                 }
124                 cmd->out = fdout[0];
125         }
126
127         need_err = !cmd->no_stderr && cmd->err < 0;
128         if (need_err) {
129                 if (pipe(fderr) < 0) {
130                         failed_errno = errno;
131                         if (need_in)
132                                 close_pair(fdin);
133                         else if (cmd->in)
134                                 close(cmd->in);
135                         if (need_out)
136                                 close_pair(fdout);
137                         else if (cmd->out)
138                                 close(cmd->out);
139 fail_pipe:
140                         error("cannot create pipe for %s: %s",
141                                 cmd->argv[0], strerror(failed_errno));
142                         errno = failed_errno;
143                         return -1;
144                 }
145                 cmd->err = fderr[0];
146         }
147
148         trace_argv_printf(cmd->argv, "trace: run_command:");
149
150 #ifndef WIN32
151 {
152         int notify_pipe[2];
153         if (pipe(notify_pipe))
154                 notify_pipe[0] = notify_pipe[1] = -1;
155
156         fflush(NULL);
157         cmd->pid = fork();
158         if (!cmd->pid) {
159                 /*
160                  * Redirect the channel to write syscall error messages to
161                  * before redirecting the process's stderr so that all die()
162                  * in subsequent call paths use the parent's stderr.
163                  */
164                 if (cmd->no_stderr || need_err) {
165                         child_err = dup(2);
166                         set_cloexec(child_err);
167                 }
168                 set_die_routine(die_child);
169
170                 close(notify_pipe[0]);
171                 set_cloexec(notify_pipe[1]);
172                 child_notifier = notify_pipe[1];
173                 atexit(notify_parent);
174
175                 if (cmd->no_stdin)
176                         dup_devnull(0);
177                 else if (need_in) {
178                         dup2(fdin[0], 0);
179                         close_pair(fdin);
180                 } else if (cmd->in) {
181                         dup2(cmd->in, 0);
182                         close(cmd->in);
183                 }
184
185                 if (cmd->no_stderr)
186                         dup_devnull(2);
187                 else if (need_err) {
188                         dup2(fderr[1], 2);
189                         close_pair(fderr);
190                 }
191
192                 if (cmd->no_stdout)
193                         dup_devnull(1);
194                 else if (cmd->stdout_to_stderr)
195                         dup2(2, 1);
196                 else if (need_out) {
197                         dup2(fdout[1], 1);
198                         close_pair(fdout);
199                 } else if (cmd->out > 1) {
200                         dup2(cmd->out, 1);
201                         close(cmd->out);
202                 }
203
204                 if (cmd->dir && chdir(cmd->dir))
205                         die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
206                             cmd->dir);
207                 if (cmd->env) {
208                         for (; *cmd->env; cmd->env++) {
209                                 if (strchr(*cmd->env, '='))
210                                         putenv((char *)*cmd->env);
211                                 else
212                                         unsetenv(*cmd->env);
213                         }
214                 }
215                 if (cmd->preexec_cb) {
216                         /*
217                          * We cannot predict what the pre-exec callback does.
218                          * Forgo parent notification.
219                          */
220                         close(child_notifier);
221                         child_notifier = -1;
222
223                         cmd->preexec_cb();
224                 }
225                 if (cmd->git_cmd) {
226                         execv_git_cmd(cmd->argv);
227                 } else {
228                         execvp(cmd->argv[0], (char *const*) cmd->argv);
229                 }
230                 /*
231                  * Do not check for cmd->silent_exec_failure; the parent
232                  * process will check it when it sees this exit code.
233                  */
234                 if (errno == ENOENT)
235                         exit(127);
236                 else
237                         die_errno("cannot exec '%s'", cmd->argv[0]);
238         }
239         if (cmd->pid < 0)
240                 error("cannot fork() for %s: %s", cmd->argv[0],
241                         strerror(failed_errno = errno));
242
243         /*
244          * Wait for child's execvp. If the execvp succeeds (or if fork()
245          * failed), EOF is seen immediately by the parent. Otherwise, the
246          * child process sends a single byte.
247          * Note that use of this infrastructure is completely advisory,
248          * therefore, we keep error checks minimal.
249          */
250         close(notify_pipe[1]);
251         if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
252                 /*
253                  * At this point we know that fork() succeeded, but execvp()
254                  * failed. Errors have been reported to our stderr.
255                  */
256                 wait_or_whine(cmd->pid, cmd->argv[0],
257                               cmd->silent_exec_failure);
258                 failed_errno = errno;
259                 cmd->pid = -1;
260         }
261         close(notify_pipe[0]);
262 }
263 #else
264 {
265         int s0 = -1, s1 = -1, s2 = -1;  /* backups of stdin, stdout, stderr */
266         const char **sargv = cmd->argv;
267         char **env = environ;
268
269         if (cmd->no_stdin) {
270                 s0 = dup(0);
271                 dup_devnull(0);
272         } else if (need_in) {
273                 s0 = dup(0);
274                 dup2(fdin[0], 0);
275         } else if (cmd->in) {
276                 s0 = dup(0);
277                 dup2(cmd->in, 0);
278         }
279
280         if (cmd->no_stderr) {
281                 s2 = dup(2);
282                 dup_devnull(2);
283         } else if (need_err) {
284                 s2 = dup(2);
285                 dup2(fderr[1], 2);
286         }
287
288         if (cmd->no_stdout) {
289                 s1 = dup(1);
290                 dup_devnull(1);
291         } else if (cmd->stdout_to_stderr) {
292                 s1 = dup(1);
293                 dup2(2, 1);
294         } else if (need_out) {
295                 s1 = dup(1);
296                 dup2(fdout[1], 1);
297         } else if (cmd->out > 1) {
298                 s1 = dup(1);
299                 dup2(cmd->out, 1);
300         }
301
302         if (cmd->dir)
303                 die("chdir in start_command() not implemented");
304         if (cmd->env)
305                 env = make_augmented_environ(cmd->env);
306
307         if (cmd->git_cmd) {
308                 cmd->argv = prepare_git_cmd(cmd->argv);
309         }
310
311         cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
312         failed_errno = errno;
313         if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
314                 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
315
316         if (cmd->env)
317                 free_environ(env);
318         if (cmd->git_cmd)
319                 free(cmd->argv);
320
321         cmd->argv = sargv;
322         if (s0 >= 0)
323                 dup2(s0, 0), close(s0);
324         if (s1 >= 0)
325                 dup2(s1, 1), close(s1);
326         if (s2 >= 0)
327                 dup2(s2, 2), close(s2);
328 }
329 #endif
330
331         if (cmd->pid < 0) {
332                 if (need_in)
333                         close_pair(fdin);
334                 else if (cmd->in)
335                         close(cmd->in);
336                 if (need_out)
337                         close_pair(fdout);
338                 else if (cmd->out)
339                         close(cmd->out);
340                 if (need_err)
341                         close_pair(fderr);
342                 errno = failed_errno;
343                 return -1;
344         }
345
346         if (need_in)
347                 close(fdin[0]);
348         else if (cmd->in)
349                 close(cmd->in);
350
351         if (need_out)
352                 close(fdout[1]);
353         else if (cmd->out)
354                 close(cmd->out);
355
356         if (need_err)
357                 close(fderr[1]);
358
359         return 0;
360 }
361
362 int finish_command(struct child_process *cmd)
363 {
364         return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
365 }
366
367 int run_command(struct child_process *cmd)
368 {
369         int code = start_command(cmd);
370         if (code)
371                 return code;
372         return finish_command(cmd);
373 }
374
375 static void prepare_run_command_v_opt(struct child_process *cmd,
376                                       const char **argv,
377                                       int opt)
378 {
379         memset(cmd, 0, sizeof(*cmd));
380         cmd->argv = argv;
381         cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
382         cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
383         cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
384         cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
385 }
386
387 int run_command_v_opt(const char **argv, int opt)
388 {
389         struct child_process cmd;
390         prepare_run_command_v_opt(&cmd, argv, opt);
391         return run_command(&cmd);
392 }
393
394 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
395 {
396         struct child_process cmd;
397         prepare_run_command_v_opt(&cmd, argv, opt);
398         cmd.dir = dir;
399         cmd.env = env;
400         return run_command(&cmd);
401 }
402
403 #ifdef WIN32
404 static unsigned __stdcall run_thread(void *data)
405 {
406         struct async *async = data;
407         return async->proc(async->fd_for_proc, async->data);
408 }
409 #endif
410
411 int start_async(struct async *async)
412 {
413         int pipe_out[2];
414
415         if (pipe(pipe_out) < 0)
416                 return error("cannot create pipe: %s", strerror(errno));
417         async->out = pipe_out[0];
418
419 #ifndef WIN32
420         /* Flush stdio before fork() to avoid cloning buffers */
421         fflush(NULL);
422
423         async->pid = fork();
424         if (async->pid < 0) {
425                 error("fork (async) failed: %s", strerror(errno));
426                 close_pair(pipe_out);
427                 return -1;
428         }
429         if (!async->pid) {
430                 close(pipe_out[0]);
431                 exit(!!async->proc(pipe_out[1], async->data));
432         }
433         close(pipe_out[1]);
434 #else
435         async->fd_for_proc = pipe_out[1];
436         async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
437         if (!async->tid) {
438                 error("cannot create thread: %s", strerror(errno));
439                 close_pair(pipe_out);
440                 return -1;
441         }
442 #endif
443         return 0;
444 }
445
446 int finish_async(struct async *async)
447 {
448 #ifndef WIN32
449         int ret = wait_or_whine(async->pid, "child process", 0);
450 #else
451         DWORD ret = 0;
452         if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
453                 ret = error("waiting for thread failed: %lu", GetLastError());
454         else if (!GetExitCodeThread(async->tid, &ret))
455                 ret = error("cannot get thread exit code: %lu", GetLastError());
456         CloseHandle(async->tid);
457 #endif
458         return ret;
459 }
460
461 int run_hook(const char *index_file, const char *name, ...)
462 {
463         struct child_process hook;
464         const char **argv = NULL, *env[2];
465         char index[PATH_MAX];
466         va_list args;
467         int ret;
468         size_t i = 0, alloc = 0;
469
470         if (access(git_path("hooks/%s", name), X_OK) < 0)
471                 return 0;
472
473         va_start(args, name);
474         ALLOC_GROW(argv, i + 1, alloc);
475         argv[i++] = git_path("hooks/%s", name);
476         while (argv[i-1]) {
477                 ALLOC_GROW(argv, i + 1, alloc);
478                 argv[i++] = va_arg(args, const char *);
479         }
480         va_end(args);
481
482         memset(&hook, 0, sizeof(hook));
483         hook.argv = argv;
484         hook.no_stdin = 1;
485         hook.stdout_to_stderr = 1;
486         if (index_file) {
487                 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
488                 env[0] = index;
489                 env[1] = NULL;
490                 hook.env = env;
491         }
492
493         ret = run_command(&hook);
494         free(argv);
495         return ret;
496 }