1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
4 #include "argv-array.h"
6 static inline void close_pair(int fd[2])
7 {
8 close(fd[0]);
9 close(fd[1]);
10 }
12 #ifndef WIN32
13 static inline void dup_devnull(int to)
14 {
15 int fd = open("/dev/null", O_RDWR);
16 dup2(fd, to);
17 close(fd);
18 }
19 #endif
21 static const char **prepare_shell_cmd(const char **argv)
22 {
23 int argc, nargc = 0;
24 const char **nargv;
26 for (argc = 0; argv[argc]; argc++)
27 ; /* just counting */
28 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
29 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
31 if (argc < 1)
32 die("BUG: shell command is empty");
34 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
35 nargv[nargc++] = "sh";
36 nargv[nargc++] = "-c";
38 if (argc < 2)
39 nargv[nargc++] = argv[0];
40 else {
41 struct strbuf arg0 = STRBUF_INIT;
42 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
43 nargv[nargc++] = strbuf_detach(&arg0, NULL);
44 }
45 }
47 for (argc = 0; argv[argc]; argc++)
48 nargv[nargc++] = argv[argc];
49 nargv[nargc] = NULL;
51 return nargv;
52 }
54 #ifndef WIN32
55 static int execv_shell_cmd(const char **argv)
56 {
57 const char **nargv = prepare_shell_cmd(argv);
58 trace_argv_printf(nargv, "trace: exec:");
59 execvp(nargv[0], (char **)nargv);
60 free(nargv);
61 return -1;
62 }
63 #endif
65 #ifndef WIN32
66 static int child_err = 2;
67 static int child_notifier = -1;
69 static void notify_parent(void)
70 {
71 /*
72 * execvp failed. If possible, we'd like to let start_command
73 * know, so failures like ENOENT can be handled right away; but
74 * otherwise, finish_command will still report the error.
75 */
76 xwrite(child_notifier, "", 1);
77 }
79 static NORETURN void die_child(const char *err, va_list params)
80 {
81 char msg[4096];
82 int len = vsnprintf(msg, sizeof(msg), err, params);
83 if (len > sizeof(msg))
84 len = sizeof(msg);
86 write_in_full(child_err, "fatal: ", 7);
87 write_in_full(child_err, msg, len);
88 write_in_full(child_err, "\n", 1);
89 exit(128);
90 }
91 #endif
93 static inline void set_cloexec(int fd)
94 {
95 int flags = fcntl(fd, F_GETFD);
96 if (flags >= 0)
97 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
98 }
100 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
101 {
102 int status, code = -1;
103 pid_t waiting;
104 int failed_errno = 0;
106 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
107 ; /* nothing */
109 if (waiting < 0) {
110 failed_errno = errno;
111 error("waitpid for %s failed: %s", argv0, strerror(errno));
112 } else if (waiting != pid) {
113 error("waitpid is confused (%s)", argv0);
114 } else if (WIFSIGNALED(status)) {
115 code = WTERMSIG(status);
116 error("%s died of signal %d", argv0, code);
117 /*
118 * This return value is chosen so that code & 0xff
119 * mimics the exit code that a POSIX shell would report for
120 * a program that died from this signal.
121 */
122 code -= 128;
123 } else if (WIFEXITED(status)) {
124 code = WEXITSTATUS(status);
125 /*
126 * Convert special exit code when execvp failed.
127 */
128 if (code == 127) {
129 code = -1;
130 failed_errno = ENOENT;
131 if (!silent_exec_failure)
132 error("cannot run %s: %s", argv0,
133 strerror(ENOENT));
134 }
135 } else {
136 error("waitpid is confused (%s)", argv0);
137 }
138 errno = failed_errno;
139 return code;
140 }
142 int start_command(struct child_process *cmd)
143 {
144 int need_in, need_out, need_err;
145 int fdin[2], fdout[2], fderr[2];
146 int failed_errno = failed_errno;
148 /*
149 * In case of errors we must keep the promise to close FDs
150 * that have been passed in via ->in and ->out.
151 */
153 need_in = !cmd->no_stdin && cmd->in < 0;
154 if (need_in) {
155 if (pipe(fdin) < 0) {
156 failed_errno = errno;
157 if (cmd->out > 0)
158 close(cmd->out);
159 goto fail_pipe;
160 }
161 cmd->in = fdin[1];
162 }
164 need_out = !cmd->no_stdout
165 && !cmd->stdout_to_stderr
166 && cmd->out < 0;
167 if (need_out) {
168 if (pipe(fdout) < 0) {
169 failed_errno = errno;
170 if (need_in)
171 close_pair(fdin);
172 else if (cmd->in)
173 close(cmd->in);
174 goto fail_pipe;
175 }
176 cmd->out = fdout[0];
177 }
179 need_err = !cmd->no_stderr && cmd->err < 0;
180 if (need_err) {
181 if (pipe(fderr) < 0) {
182 failed_errno = errno;
183 if (need_in)
184 close_pair(fdin);
185 else if (cmd->in)
186 close(cmd->in);
187 if (need_out)
188 close_pair(fdout);
189 else if (cmd->out)
190 close(cmd->out);
191 fail_pipe:
192 error("cannot create pipe for %s: %s",
193 cmd->argv[0], strerror(failed_errno));
194 errno = failed_errno;
195 return -1;
196 }
197 cmd->err = fderr[0];
198 }
200 trace_argv_printf(cmd->argv, "trace: run_command:");
201 fflush(NULL);
203 #ifndef WIN32
204 {
205 int notify_pipe[2];
206 if (pipe(notify_pipe))
207 notify_pipe[0] = notify_pipe[1] = -1;
209 cmd->pid = fork();
210 if (!cmd->pid) {
211 /*
212 * Redirect the channel to write syscall error messages to
213 * before redirecting the process's stderr so that all die()
214 * in subsequent call paths use the parent's stderr.
215 */
216 if (cmd->no_stderr || need_err) {
217 child_err = dup(2);
218 set_cloexec(child_err);
219 }
220 set_die_routine(die_child);
222 close(notify_pipe[0]);
223 set_cloexec(notify_pipe[1]);
224 child_notifier = notify_pipe[1];
225 atexit(notify_parent);
227 if (cmd->no_stdin)
228 dup_devnull(0);
229 else if (need_in) {
230 dup2(fdin[0], 0);
231 close_pair(fdin);
232 } else if (cmd->in) {
233 dup2(cmd->in, 0);
234 close(cmd->in);
235 }
237 if (cmd->no_stderr)
238 dup_devnull(2);
239 else if (need_err) {
240 dup2(fderr[1], 2);
241 close_pair(fderr);
242 } else if (cmd->err > 1) {
243 dup2(cmd->err, 2);
244 close(cmd->err);
245 }
247 if (cmd->no_stdout)
248 dup_devnull(1);
249 else if (cmd->stdout_to_stderr)
250 dup2(2, 1);
251 else if (need_out) {
252 dup2(fdout[1], 1);
253 close_pair(fdout);
254 } else if (cmd->out > 1) {
255 dup2(cmd->out, 1);
256 close(cmd->out);
257 }
259 if (cmd->dir && chdir(cmd->dir))
260 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
261 cmd->dir);
262 if (cmd->env) {
263 for (; *cmd->env; cmd->env++) {
264 if (strchr(*cmd->env, '='))
265 putenv((char *)*cmd->env);
266 else
267 unsetenv(*cmd->env);
268 }
269 }
270 if (cmd->preexec_cb) {
271 /*
272 * We cannot predict what the pre-exec callback does.
273 * Forgo parent notification.
274 */
275 close(child_notifier);
276 child_notifier = -1;
278 cmd->preexec_cb();
279 }
280 if (cmd->git_cmd) {
281 execv_git_cmd(cmd->argv);
282 } else if (cmd->use_shell) {
283 execv_shell_cmd(cmd->argv);
284 } else {
285 execvp(cmd->argv[0], (char *const*) cmd->argv);
286 }
287 /*
288 * Do not check for cmd->silent_exec_failure; the parent
289 * process will check it when it sees this exit code.
290 */
291 if (errno == ENOENT)
292 exit(127);
293 else
294 die_errno("cannot exec '%s'", cmd->argv[0]);
295 }
296 if (cmd->pid < 0)
297 error("cannot fork() for %s: %s", cmd->argv[0],
298 strerror(failed_errno = errno));
300 /*
301 * Wait for child's execvp. If the execvp succeeds (or if fork()
302 * failed), EOF is seen immediately by the parent. Otherwise, the
303 * child process sends a single byte.
304 * Note that use of this infrastructure is completely advisory,
305 * therefore, we keep error checks minimal.
306 */
307 close(notify_pipe[1]);
308 if (read(notify_pipe[0], ¬ify_pipe[1], 1) == 1) {
309 /*
310 * At this point we know that fork() succeeded, but execvp()
311 * failed. Errors have been reported to our stderr.
312 */
313 wait_or_whine(cmd->pid, cmd->argv[0],
314 cmd->silent_exec_failure);
315 failed_errno = errno;
316 cmd->pid = -1;
317 }
318 close(notify_pipe[0]);
319 }
320 #else
321 {
322 int fhin = 0, fhout = 1, fherr = 2;
323 const char **sargv = cmd->argv;
324 char **env = environ;
326 if (cmd->no_stdin)
327 fhin = open("/dev/null", O_RDWR);
328 else if (need_in)
329 fhin = dup(fdin[0]);
330 else if (cmd->in)
331 fhin = dup(cmd->in);
333 if (cmd->no_stderr)
334 fherr = open("/dev/null", O_RDWR);
335 else if (need_err)
336 fherr = dup(fderr[1]);
337 else if (cmd->err > 2)
338 fherr = dup(cmd->err);
340 if (cmd->no_stdout)
341 fhout = open("/dev/null", O_RDWR);
342 else if (cmd->stdout_to_stderr)
343 fhout = dup(fherr);
344 else if (need_out)
345 fhout = dup(fdout[1]);
346 else if (cmd->out > 1)
347 fhout = dup(cmd->out);
349 if (cmd->env)
350 env = make_augmented_environ(cmd->env);
352 if (cmd->git_cmd) {
353 cmd->argv = prepare_git_cmd(cmd->argv);
354 } else if (cmd->use_shell) {
355 cmd->argv = prepare_shell_cmd(cmd->argv);
356 }
358 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir,
359 fhin, fhout, fherr);
360 failed_errno = errno;
361 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
362 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
364 if (cmd->env)
365 free_environ(env);
366 if (cmd->git_cmd)
367 free(cmd->argv);
369 cmd->argv = sargv;
370 if (fhin != 0)
371 close(fhin);
372 if (fhout != 1)
373 close(fhout);
374 if (fherr != 2)
375 close(fherr);
376 }
377 #endif
379 if (cmd->pid < 0) {
380 if (need_in)
381 close_pair(fdin);
382 else if (cmd->in)
383 close(cmd->in);
384 if (need_out)
385 close_pair(fdout);
386 else if (cmd->out)
387 close(cmd->out);
388 if (need_err)
389 close_pair(fderr);
390 else if (cmd->err)
391 close(cmd->err);
392 errno = failed_errno;
393 return -1;
394 }
396 if (need_in)
397 close(fdin[0]);
398 else if (cmd->in)
399 close(cmd->in);
401 if (need_out)
402 close(fdout[1]);
403 else if (cmd->out)
404 close(cmd->out);
406 if (need_err)
407 close(fderr[1]);
408 else if (cmd->err)
409 close(cmd->err);
411 return 0;
412 }
414 int finish_command(struct child_process *cmd)
415 {
416 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
417 }
419 int run_command(struct child_process *cmd)
420 {
421 int code = start_command(cmd);
422 if (code)
423 return code;
424 return finish_command(cmd);
425 }
427 static void prepare_run_command_v_opt(struct child_process *cmd,
428 const char **argv,
429 int opt)
430 {
431 memset(cmd, 0, sizeof(*cmd));
432 cmd->argv = argv;
433 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
434 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
435 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
436 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
437 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
438 }
440 int run_command_v_opt(const char **argv, int opt)
441 {
442 struct child_process cmd;
443 prepare_run_command_v_opt(&cmd, argv, opt);
444 return run_command(&cmd);
445 }
447 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
448 {
449 struct child_process cmd;
450 prepare_run_command_v_opt(&cmd, argv, opt);
451 cmd.dir = dir;
452 cmd.env = env;
453 return run_command(&cmd);
454 }
456 #ifndef NO_PTHREADS
457 static pthread_t main_thread;
458 static int main_thread_set;
459 static pthread_key_t async_key;
461 static void *run_thread(void *data)
462 {
463 struct async *async = data;
464 intptr_t ret;
466 pthread_setspecific(async_key, async);
467 ret = async->proc(async->proc_in, async->proc_out, async->data);
468 return (void *)ret;
469 }
471 static NORETURN void die_async(const char *err, va_list params)
472 {
473 vreportf("fatal: ", err, params);
475 if (!pthread_equal(main_thread, pthread_self())) {
476 struct async *async = pthread_getspecific(async_key);
477 if (async->proc_in >= 0)
478 close(async->proc_in);
479 if (async->proc_out >= 0)
480 close(async->proc_out);
481 pthread_exit((void *)128);
482 }
484 exit(128);
485 }
486 #endif
488 int start_async(struct async *async)
489 {
490 int need_in, need_out;
491 int fdin[2], fdout[2];
492 int proc_in, proc_out;
494 need_in = async->in < 0;
495 if (need_in) {
496 if (pipe(fdin) < 0) {
497 if (async->out > 0)
498 close(async->out);
499 return error("cannot create pipe: %s", strerror(errno));
500 }
501 async->in = fdin[1];
502 }
504 need_out = async->out < 0;
505 if (need_out) {
506 if (pipe(fdout) < 0) {
507 if (need_in)
508 close_pair(fdin);
509 else if (async->in)
510 close(async->in);
511 return error("cannot create pipe: %s", strerror(errno));
512 }
513 async->out = fdout[0];
514 }
516 if (need_in)
517 proc_in = fdin[0];
518 else if (async->in)
519 proc_in = async->in;
520 else
521 proc_in = -1;
523 if (need_out)
524 proc_out = fdout[1];
525 else if (async->out)
526 proc_out = async->out;
527 else
528 proc_out = -1;
530 #ifdef NO_PTHREADS
531 /* Flush stdio before fork() to avoid cloning buffers */
532 fflush(NULL);
534 async->pid = fork();
535 if (async->pid < 0) {
536 error("fork (async) failed: %s", strerror(errno));
537 goto error;
538 }
539 if (!async->pid) {
540 if (need_in)
541 close(fdin[1]);
542 if (need_out)
543 close(fdout[0]);
544 exit(!!async->proc(proc_in, proc_out, async->data));
545 }
547 if (need_in)
548 close(fdin[0]);
549 else if (async->in)
550 close(async->in);
552 if (need_out)
553 close(fdout[1]);
554 else if (async->out)
555 close(async->out);
556 #else
557 if (!main_thread_set) {
558 /*
559 * We assume that the first time that start_async is called
560 * it is from the main thread.
561 */
562 main_thread_set = 1;
563 main_thread = pthread_self();
564 pthread_key_create(&async_key, NULL);
565 set_die_routine(die_async);
566 }
568 if (proc_in >= 0)
569 set_cloexec(proc_in);
570 if (proc_out >= 0)
571 set_cloexec(proc_out);
572 async->proc_in = proc_in;
573 async->proc_out = proc_out;
574 {
575 int err = pthread_create(&async->tid, NULL, run_thread, async);
576 if (err) {
577 error("cannot create thread: %s", strerror(err));
578 goto error;
579 }
580 }
581 #endif
582 return 0;
584 error:
585 if (need_in)
586 close_pair(fdin);
587 else if (async->in)
588 close(async->in);
590 if (need_out)
591 close_pair(fdout);
592 else if (async->out)
593 close(async->out);
594 return -1;
595 }
597 int finish_async(struct async *async)
598 {
599 #ifdef NO_PTHREADS
600 return wait_or_whine(async->pid, "child process", 0);
601 #else
602 void *ret = (void *)(intptr_t)(-1);
604 if (pthread_join(async->tid, &ret))
605 error("pthread_join failed");
606 return (int)(intptr_t)ret;
607 #endif
608 }
610 int run_hook(const char *index_file, const char *name, ...)
611 {
612 struct child_process hook;
613 struct argv_array argv = ARGV_ARRAY_INIT;
614 const char *p, *env[2];
615 char index[PATH_MAX];
616 va_list args;
617 int ret;
619 if (access(git_path("hooks/%s", name), X_OK) < 0)
620 return 0;
622 va_start(args, name);
623 argv_array_push(&argv, git_path("hooks/%s", name));
624 while ((p = va_arg(args, const char *)))
625 argv_array_push(&argv, p);
626 va_end(args);
628 memset(&hook, 0, sizeof(hook));
629 hook.argv = argv.argv;
630 hook.no_stdin = 1;
631 hook.stdout_to_stderr = 1;
632 if (index_file) {
633 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
634 env[0] = index;
635 env[1] = NULL;
636 hook.env = env;
637 }
639 ret = run_command(&hook);
640 argv_array_clear(&argv);
641 return ret;
642 }