run_hook: use argv_array API
[git/gitweb.git] / run-command.c
blob73e013ea3a003afa22ef769f0a5459c390c1d106
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])
8 close(fd[0]);
9 close(fd[1]);
12 #ifndef WIN32
13 static inline void dup_devnull(int to)
15 int fd = open("/dev/null", O_RDWR);
16 dup2(fd, to);
17 close(fd);
19 #endif
21 static const char **prepare_shell_cmd(const char **argv)
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);
47 for (argc = 0; argv[argc]; argc++)
48 nargv[nargc++] = argv[argc];
49 nargv[nargc] = NULL;
51 return nargv;
54 #ifndef WIN32
55 static int execv_shell_cmd(const char **argv)
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;
63 #endif
65 #ifndef WIN32
66 static int child_err = 2;
67 static int child_notifier = -1;
69 static void notify_parent(void)
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.
76 xwrite(child_notifier, "", 1);
79 static NORETURN void die_child(const char *err, va_list params)
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);
91 #endif
93 static inline void set_cloexec(int fd)
95 int flags = fcntl(fd, F_GETFD);
96 if (flags >= 0)
97 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
100 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
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);
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.
122 code -= 128;
123 } else if (WIFEXITED(status)) {
124 code = WEXITSTATUS(status);
126 * Convert special exit code when execvp failed.
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));
135 } else {
136 error("waitpid is confused (%s)", argv0);
138 errno = failed_errno;
139 return code;
142 int start_command(struct child_process *cmd)
144 int need_in, need_out, need_err;
145 int fdin[2], fdout[2], fderr[2];
146 int failed_errno = failed_errno;
149 * In case of errors we must keep the promise to close FDs
150 * that have been passed in via ->in and ->out.
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;
161 cmd->in = fdin[1];
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;
176 cmd->out = fdout[0];
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;
197 cmd->err = fderr[0];
200 trace_argv_printf(cmd->argv, "trace: run_command:");
201 fflush(NULL);
203 #ifndef WIN32
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) {
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.
216 if (cmd->no_stderr || need_err) {
217 child_err = dup(2);
218 set_cloexec(child_err);
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);
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);
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);
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);
270 if (cmd->preexec_cb) {
272 * We cannot predict what the pre-exec callback does.
273 * Forgo parent notification.
275 close(child_notifier);
276 child_notifier = -1;
278 cmd->preexec_cb();
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);
288 * Do not check for cmd->silent_exec_failure; the parent
289 * process will check it when it sees this exit code.
291 if (errno == ENOENT)
292 exit(127);
293 else
294 die_errno("cannot exec '%s'", cmd->argv[0]);
296 if (cmd->pid < 0)
297 error("cannot fork() for %s: %s", cmd->argv[0],
298 strerror(failed_errno = errno));
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.
307 close(notify_pipe[1]);
308 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
310 * At this point we know that fork() succeeded, but execvp()
311 * failed. Errors have been reported to our stderr.
313 wait_or_whine(cmd->pid, cmd->argv[0],
314 cmd->silent_exec_failure);
315 failed_errno = errno;
316 cmd->pid = -1;
318 close(notify_pipe[0]);
320 #else
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);
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);
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;
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;
414 int finish_command(struct child_process *cmd)
416 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
419 int run_command(struct child_process *cmd)
421 int code = start_command(cmd);
422 if (code)
423 return code;
424 return finish_command(cmd);
427 static void prepare_run_command_v_opt(struct child_process *cmd,
428 const char **argv,
429 int opt)
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;
440 int run_command_v_opt(const char **argv, int opt)
442 struct child_process cmd;
443 prepare_run_command_v_opt(&cmd, argv, opt);
444 return run_command(&cmd);
447 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
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);
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)
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;
471 static NORETURN void die_async(const char *err, va_list params)
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);
484 exit(128);
486 #endif
488 int start_async(struct async *async)
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));
501 async->in = fdin[1];
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));
513 async->out = fdout[0];
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;
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));
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) {
559 * We assume that the first time that start_async is called
560 * it is from the main thread.
562 main_thread_set = 1;
563 main_thread = pthread_self();
564 pthread_key_create(&async_key, NULL);
565 set_die_routine(die_async);
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;
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;
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;
597 int finish_async(struct async *async)
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
610 int run_hook(const char *index_file, const char *name, ...)
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;
639 ret = run_command(&hook);
640 argv_array_clear(&argv);
641 return ret;