gitweb: Improve installation instructions in gitweb/INSTALL
[git/mjg.git] / run-command.c
blobeb5c57562909c1e505c2a7688bad6dbeea4b96a0
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
5 static inline void close_pair(int fd[2])
7 close(fd[0]);
8 close(fd[1]);
11 #ifndef WIN32
12 static inline void dup_devnull(int to)
14 int fd = open("/dev/null", O_RDWR);
15 dup2(fd, to);
16 close(fd);
18 #endif
20 static const char **prepare_shell_cmd(const char **argv)
22 int argc, nargc = 0;
23 const char **nargv;
25 for (argc = 0; argv[argc]; argc++)
26 ; /* just counting */
27 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
28 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
30 if (argc < 1)
31 die("BUG: shell command is empty");
33 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
34 nargv[nargc++] = "sh";
35 nargv[nargc++] = "-c";
37 if (argc < 2)
38 nargv[nargc++] = argv[0];
39 else {
40 struct strbuf arg0 = STRBUF_INIT;
41 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
42 nargv[nargc++] = strbuf_detach(&arg0, NULL);
46 for (argc = 0; argv[argc]; argc++)
47 nargv[nargc++] = argv[argc];
48 nargv[nargc] = NULL;
50 return nargv;
53 #ifndef WIN32
54 static int execv_shell_cmd(const char **argv)
56 const char **nargv = prepare_shell_cmd(argv);
57 trace_argv_printf(nargv, "trace: exec:");
58 execvp(nargv[0], (char **)nargv);
59 free(nargv);
60 return -1;
62 #endif
64 #ifndef WIN32
65 static int child_err = 2;
66 static int child_notifier = -1;
68 static void notify_parent(void)
70 ssize_t unused;
71 unused = write(child_notifier, "", 1);
74 static NORETURN void die_child(const char *err, va_list params)
76 char msg[4096];
77 ssize_t unused;
78 int len = vsnprintf(msg, sizeof(msg), err, params);
79 if (len > sizeof(msg))
80 len = sizeof(msg);
82 unused = write(child_err, "fatal: ", 7);
83 unused = write(child_err, msg, len);
84 unused = write(child_err, "\n", 1);
85 exit(128);
88 static inline void set_cloexec(int fd)
90 int flags = fcntl(fd, F_GETFD);
91 if (flags >= 0)
92 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
94 #endif
96 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
98 int status, code = -1;
99 pid_t waiting;
100 int failed_errno = 0;
102 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
103 ; /* nothing */
105 if (waiting < 0) {
106 failed_errno = errno;
107 error("waitpid for %s failed: %s", argv0, strerror(errno));
108 } else if (waiting != pid) {
109 error("waitpid is confused (%s)", argv0);
110 } else if (WIFSIGNALED(status)) {
111 code = WTERMSIG(status);
112 error("%s died of signal %d", argv0, code);
114 * This return value is chosen so that code & 0xff
115 * mimics the exit code that a POSIX shell would report for
116 * a program that died from this signal.
118 code -= 128;
119 } else if (WIFEXITED(status)) {
120 code = WEXITSTATUS(status);
122 * Convert special exit code when execvp failed.
124 if (code == 127) {
125 code = -1;
126 failed_errno = ENOENT;
127 if (!silent_exec_failure)
128 error("cannot run %s: %s", argv0,
129 strerror(ENOENT));
131 } else {
132 error("waitpid is confused (%s)", argv0);
134 errno = failed_errno;
135 return code;
138 int start_command(struct child_process *cmd)
140 int need_in, need_out, need_err;
141 int fdin[2], fdout[2], fderr[2];
142 int failed_errno = failed_errno;
145 * In case of errors we must keep the promise to close FDs
146 * that have been passed in via ->in and ->out.
149 need_in = !cmd->no_stdin && cmd->in < 0;
150 if (need_in) {
151 if (pipe(fdin) < 0) {
152 failed_errno = errno;
153 if (cmd->out > 0)
154 close(cmd->out);
155 goto fail_pipe;
157 cmd->in = fdin[1];
160 need_out = !cmd->no_stdout
161 && !cmd->stdout_to_stderr
162 && cmd->out < 0;
163 if (need_out) {
164 if (pipe(fdout) < 0) {
165 failed_errno = errno;
166 if (need_in)
167 close_pair(fdin);
168 else if (cmd->in)
169 close(cmd->in);
170 goto fail_pipe;
172 cmd->out = fdout[0];
175 need_err = !cmd->no_stderr && cmd->err < 0;
176 if (need_err) {
177 if (pipe(fderr) < 0) {
178 failed_errno = errno;
179 if (need_in)
180 close_pair(fdin);
181 else if (cmd->in)
182 close(cmd->in);
183 if (need_out)
184 close_pair(fdout);
185 else if (cmd->out)
186 close(cmd->out);
187 fail_pipe:
188 error("cannot create pipe for %s: %s",
189 cmd->argv[0], strerror(failed_errno));
190 errno = failed_errno;
191 return -1;
193 cmd->err = fderr[0];
196 trace_argv_printf(cmd->argv, "trace: run_command:");
198 #ifndef WIN32
200 int notify_pipe[2];
201 if (pipe(notify_pipe))
202 notify_pipe[0] = notify_pipe[1] = -1;
204 fflush(NULL);
205 cmd->pid = fork();
206 if (!cmd->pid) {
208 * Redirect the channel to write syscall error messages to
209 * before redirecting the process's stderr so that all die()
210 * in subsequent call paths use the parent's stderr.
212 if (cmd->no_stderr || need_err) {
213 child_err = dup(2);
214 set_cloexec(child_err);
216 set_die_routine(die_child);
218 close(notify_pipe[0]);
219 set_cloexec(notify_pipe[1]);
220 child_notifier = notify_pipe[1];
221 atexit(notify_parent);
223 if (cmd->no_stdin)
224 dup_devnull(0);
225 else if (need_in) {
226 dup2(fdin[0], 0);
227 close_pair(fdin);
228 } else if (cmd->in) {
229 dup2(cmd->in, 0);
230 close(cmd->in);
233 if (cmd->no_stderr)
234 dup_devnull(2);
235 else if (need_err) {
236 dup2(fderr[1], 2);
237 close_pair(fderr);
238 } else if (cmd->err > 1) {
239 dup2(cmd->err, 2);
240 close(cmd->err);
243 if (cmd->no_stdout)
244 dup_devnull(1);
245 else if (cmd->stdout_to_stderr)
246 dup2(2, 1);
247 else if (need_out) {
248 dup2(fdout[1], 1);
249 close_pair(fdout);
250 } else if (cmd->out > 1) {
251 dup2(cmd->out, 1);
252 close(cmd->out);
255 if (cmd->dir && chdir(cmd->dir))
256 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
257 cmd->dir);
258 if (cmd->env) {
259 for (; *cmd->env; cmd->env++) {
260 if (strchr(*cmd->env, '='))
261 putenv((char *)*cmd->env);
262 else
263 unsetenv(*cmd->env);
266 if (cmd->preexec_cb) {
268 * We cannot predict what the pre-exec callback does.
269 * Forgo parent notification.
271 close(child_notifier);
272 child_notifier = -1;
274 cmd->preexec_cb();
276 if (cmd->git_cmd) {
277 execv_git_cmd(cmd->argv);
278 } else if (cmd->use_shell) {
279 execv_shell_cmd(cmd->argv);
280 } else {
281 execvp(cmd->argv[0], (char *const*) cmd->argv);
284 * Do not check for cmd->silent_exec_failure; the parent
285 * process will check it when it sees this exit code.
287 if (errno == ENOENT)
288 exit(127);
289 else
290 die_errno("cannot exec '%s'", cmd->argv[0]);
292 if (cmd->pid < 0)
293 error("cannot fork() for %s: %s", cmd->argv[0],
294 strerror(failed_errno = errno));
297 * Wait for child's execvp. If the execvp succeeds (or if fork()
298 * failed), EOF is seen immediately by the parent. Otherwise, the
299 * child process sends a single byte.
300 * Note that use of this infrastructure is completely advisory,
301 * therefore, we keep error checks minimal.
303 close(notify_pipe[1]);
304 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
306 * At this point we know that fork() succeeded, but execvp()
307 * failed. Errors have been reported to our stderr.
309 wait_or_whine(cmd->pid, cmd->argv[0],
310 cmd->silent_exec_failure);
311 failed_errno = errno;
312 cmd->pid = -1;
314 close(notify_pipe[0]);
316 #else
318 int fhin = 0, fhout = 1, fherr = 2;
319 const char **sargv = cmd->argv;
320 char **env = environ;
322 if (cmd->no_stdin)
323 fhin = open("/dev/null", O_RDWR);
324 else if (need_in)
325 fhin = dup(fdin[0]);
326 else if (cmd->in)
327 fhin = dup(cmd->in);
329 if (cmd->no_stderr)
330 fherr = open("/dev/null", O_RDWR);
331 else if (need_err)
332 fherr = dup(fderr[1]);
333 else if (cmd->err > 2)
334 fherr = dup(cmd->err);
336 if (cmd->no_stdout)
337 fhout = open("/dev/null", O_RDWR);
338 else if (cmd->stdout_to_stderr)
339 fhout = dup(fherr);
340 else if (need_out)
341 fhout = dup(fdout[1]);
342 else if (cmd->out > 1)
343 fhout = dup(cmd->out);
345 if (cmd->env)
346 env = make_augmented_environ(cmd->env);
348 if (cmd->git_cmd) {
349 cmd->argv = prepare_git_cmd(cmd->argv);
350 } else if (cmd->use_shell) {
351 cmd->argv = prepare_shell_cmd(cmd->argv);
354 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir,
355 fhin, fhout, fherr);
356 failed_errno = errno;
357 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
358 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
360 if (cmd->env)
361 free_environ(env);
362 if (cmd->git_cmd)
363 free(cmd->argv);
365 cmd->argv = sargv;
366 if (fhin != 0)
367 close(fhin);
368 if (fhout != 1)
369 close(fhout);
370 if (fherr != 2)
371 close(fherr);
373 #endif
375 if (cmd->pid < 0) {
376 if (need_in)
377 close_pair(fdin);
378 else if (cmd->in)
379 close(cmd->in);
380 if (need_out)
381 close_pair(fdout);
382 else if (cmd->out)
383 close(cmd->out);
384 if (need_err)
385 close_pair(fderr);
386 errno = failed_errno;
387 return -1;
390 if (need_in)
391 close(fdin[0]);
392 else if (cmd->in)
393 close(cmd->in);
395 if (need_out)
396 close(fdout[1]);
397 else if (cmd->out)
398 close(cmd->out);
400 if (need_err)
401 close(fderr[1]);
402 else if (cmd->err)
403 close(cmd->err);
405 return 0;
408 int finish_command(struct child_process *cmd)
410 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
413 int run_command(struct child_process *cmd)
415 int code = start_command(cmd);
416 if (code)
417 return code;
418 return finish_command(cmd);
421 static void prepare_run_command_v_opt(struct child_process *cmd,
422 const char **argv,
423 int opt)
425 memset(cmd, 0, sizeof(*cmd));
426 cmd->argv = argv;
427 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
428 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
429 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
430 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
431 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
434 int run_command_v_opt(const char **argv, int opt)
436 struct child_process cmd;
437 prepare_run_command_v_opt(&cmd, argv, opt);
438 return run_command(&cmd);
441 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
443 struct child_process cmd;
444 prepare_run_command_v_opt(&cmd, argv, opt);
445 cmd.dir = dir;
446 cmd.env = env;
447 return run_command(&cmd);
450 #ifdef WIN32
451 static unsigned __stdcall run_thread(void *data)
453 struct async *async = data;
454 return async->proc(async->proc_in, async->proc_out, async->data);
456 #endif
458 int start_async(struct async *async)
460 int need_in, need_out;
461 int fdin[2], fdout[2];
462 int proc_in, proc_out;
464 need_in = async->in < 0;
465 if (need_in) {
466 if (pipe(fdin) < 0) {
467 if (async->out > 0)
468 close(async->out);
469 return error("cannot create pipe: %s", strerror(errno));
471 async->in = fdin[1];
474 need_out = async->out < 0;
475 if (need_out) {
476 if (pipe(fdout) < 0) {
477 if (need_in)
478 close_pair(fdin);
479 else if (async->in)
480 close(async->in);
481 return error("cannot create pipe: %s", strerror(errno));
483 async->out = fdout[0];
486 if (need_in)
487 proc_in = fdin[0];
488 else if (async->in)
489 proc_in = async->in;
490 else
491 proc_in = -1;
493 if (need_out)
494 proc_out = fdout[1];
495 else if (async->out)
496 proc_out = async->out;
497 else
498 proc_out = -1;
500 #ifndef WIN32
501 /* Flush stdio before fork() to avoid cloning buffers */
502 fflush(NULL);
504 async->pid = fork();
505 if (async->pid < 0) {
506 error("fork (async) failed: %s", strerror(errno));
507 goto error;
509 if (!async->pid) {
510 if (need_in)
511 close(fdin[1]);
512 if (need_out)
513 close(fdout[0]);
514 exit(!!async->proc(proc_in, proc_out, async->data));
517 if (need_in)
518 close(fdin[0]);
519 else if (async->in)
520 close(async->in);
522 if (need_out)
523 close(fdout[1]);
524 else if (async->out)
525 close(async->out);
526 #else
527 async->proc_in = proc_in;
528 async->proc_out = proc_out;
529 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
530 if (!async->tid) {
531 error("cannot create thread: %s", strerror(errno));
532 goto error;
534 #endif
535 return 0;
537 error:
538 if (need_in)
539 close_pair(fdin);
540 else if (async->in)
541 close(async->in);
543 if (need_out)
544 close_pair(fdout);
545 else if (async->out)
546 close(async->out);
547 return -1;
550 int finish_async(struct async *async)
552 #ifndef WIN32
553 int ret = wait_or_whine(async->pid, "child process", 0);
554 #else
555 DWORD ret = 0;
556 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
557 ret = error("waiting for thread failed: %lu", GetLastError());
558 else if (!GetExitCodeThread(async->tid, &ret))
559 ret = error("cannot get thread exit code: %lu", GetLastError());
560 CloseHandle(async->tid);
561 #endif
562 return ret;
565 int run_hook(const char *index_file, const char *name, ...)
567 struct child_process hook;
568 const char **argv = NULL, *env[2];
569 char index[PATH_MAX];
570 va_list args;
571 int ret;
572 size_t i = 0, alloc = 0;
574 if (access(git_path("hooks/%s", name), X_OK) < 0)
575 return 0;
577 va_start(args, name);
578 ALLOC_GROW(argv, i + 1, alloc);
579 argv[i++] = git_path("hooks/%s", name);
580 while (argv[i-1]) {
581 ALLOC_GROW(argv, i + 1, alloc);
582 argv[i++] = va_arg(args, const char *);
584 va_end(args);
586 memset(&hook, 0, sizeof(hook));
587 hook.argv = argv;
588 hook.no_stdin = 1;
589 hook.stdout_to_stderr = 1;
590 if (index_file) {
591 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
592 env[0] = index;
593 env[1] = NULL;
594 hook.env = env;
597 ret = run_command(&hook);
598 free(argv);
599 return ret;