Reimplement async procedures using pthreads
[git/jnareb-git.git] / run-command.c
blob77aefff2e27e369feffccbad817845af17597b02
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 write(child_notifier, "", 1);
73 static NORETURN void die_child(const char *err, va_list params)
75 char msg[4096];
76 int len = vsnprintf(msg, sizeof(msg), err, params);
77 if (len > sizeof(msg))
78 len = sizeof(msg);
80 write(child_err, "fatal: ", 7);
81 write(child_err, msg, len);
82 write(child_err, "\n", 1);
83 exit(128);
85 #endif
87 static inline void set_cloexec(int fd)
89 int flags = fcntl(fd, F_GETFD);
90 if (flags >= 0)
91 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
94 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
96 int status, code = -1;
97 pid_t waiting;
98 int failed_errno = 0;
100 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
101 ; /* nothing */
103 if (waiting < 0) {
104 failed_errno = errno;
105 error("waitpid for %s failed: %s", argv0, strerror(errno));
106 } else if (waiting != pid) {
107 error("waitpid is confused (%s)", argv0);
108 } else if (WIFSIGNALED(status)) {
109 code = WTERMSIG(status);
110 error("%s died of signal %d", argv0, code);
112 * This return value is chosen so that code & 0xff
113 * mimics the exit code that a POSIX shell would report for
114 * a program that died from this signal.
116 code -= 128;
117 } else if (WIFEXITED(status)) {
118 code = WEXITSTATUS(status);
120 * Convert special exit code when execvp failed.
122 if (code == 127) {
123 code = -1;
124 failed_errno = ENOENT;
125 if (!silent_exec_failure)
126 error("cannot run %s: %s", argv0,
127 strerror(ENOENT));
129 } else {
130 error("waitpid is confused (%s)", argv0);
132 errno = failed_errno;
133 return code;
136 int start_command(struct child_process *cmd)
138 int need_in, need_out, need_err;
139 int fdin[2], fdout[2], fderr[2];
140 int failed_errno = failed_errno;
143 * In case of errors we must keep the promise to close FDs
144 * that have been passed in via ->in and ->out.
147 need_in = !cmd->no_stdin && cmd->in < 0;
148 if (need_in) {
149 if (pipe(fdin) < 0) {
150 failed_errno = errno;
151 if (cmd->out > 0)
152 close(cmd->out);
153 goto fail_pipe;
155 cmd->in = fdin[1];
158 need_out = !cmd->no_stdout
159 && !cmd->stdout_to_stderr
160 && cmd->out < 0;
161 if (need_out) {
162 if (pipe(fdout) < 0) {
163 failed_errno = errno;
164 if (need_in)
165 close_pair(fdin);
166 else if (cmd->in)
167 close(cmd->in);
168 goto fail_pipe;
170 cmd->out = fdout[0];
173 need_err = !cmd->no_stderr && cmd->err < 0;
174 if (need_err) {
175 if (pipe(fderr) < 0) {
176 failed_errno = errno;
177 if (need_in)
178 close_pair(fdin);
179 else if (cmd->in)
180 close(cmd->in);
181 if (need_out)
182 close_pair(fdout);
183 else if (cmd->out)
184 close(cmd->out);
185 fail_pipe:
186 error("cannot create pipe for %s: %s",
187 cmd->argv[0], strerror(failed_errno));
188 errno = failed_errno;
189 return -1;
191 cmd->err = fderr[0];
194 trace_argv_printf(cmd->argv, "trace: run_command:");
196 #ifndef WIN32
198 int notify_pipe[2];
199 if (pipe(notify_pipe))
200 notify_pipe[0] = notify_pipe[1] = -1;
202 fflush(NULL);
203 cmd->pid = fork();
204 if (!cmd->pid) {
206 * Redirect the channel to write syscall error messages to
207 * before redirecting the process's stderr so that all die()
208 * in subsequent call paths use the parent's stderr.
210 if (cmd->no_stderr || need_err) {
211 child_err = dup(2);
212 set_cloexec(child_err);
214 set_die_routine(die_child);
216 close(notify_pipe[0]);
217 set_cloexec(notify_pipe[1]);
218 child_notifier = notify_pipe[1];
219 atexit(notify_parent);
221 if (cmd->no_stdin)
222 dup_devnull(0);
223 else if (need_in) {
224 dup2(fdin[0], 0);
225 close_pair(fdin);
226 } else if (cmd->in) {
227 dup2(cmd->in, 0);
228 close(cmd->in);
231 if (cmd->no_stderr)
232 dup_devnull(2);
233 else if (need_err) {
234 dup2(fderr[1], 2);
235 close_pair(fderr);
236 } else if (cmd->err > 1) {
237 dup2(cmd->err, 2);
238 close(cmd->err);
241 if (cmd->no_stdout)
242 dup_devnull(1);
243 else if (cmd->stdout_to_stderr)
244 dup2(2, 1);
245 else if (need_out) {
246 dup2(fdout[1], 1);
247 close_pair(fdout);
248 } else if (cmd->out > 1) {
249 dup2(cmd->out, 1);
250 close(cmd->out);
253 if (cmd->dir && chdir(cmd->dir))
254 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
255 cmd->dir);
256 if (cmd->env) {
257 for (; *cmd->env; cmd->env++) {
258 if (strchr(*cmd->env, '='))
259 putenv((char *)*cmd->env);
260 else
261 unsetenv(*cmd->env);
264 if (cmd->preexec_cb) {
266 * We cannot predict what the pre-exec callback does.
267 * Forgo parent notification.
269 close(child_notifier);
270 child_notifier = -1;
272 cmd->preexec_cb();
274 if (cmd->git_cmd) {
275 execv_git_cmd(cmd->argv);
276 } else if (cmd->use_shell) {
277 execv_shell_cmd(cmd->argv);
278 } else {
279 execvp(cmd->argv[0], (char *const*) cmd->argv);
282 * Do not check for cmd->silent_exec_failure; the parent
283 * process will check it when it sees this exit code.
285 if (errno == ENOENT)
286 exit(127);
287 else
288 die_errno("cannot exec '%s'", cmd->argv[0]);
290 if (cmd->pid < 0)
291 error("cannot fork() for %s: %s", cmd->argv[0],
292 strerror(failed_errno = errno));
295 * Wait for child's execvp. If the execvp succeeds (or if fork()
296 * failed), EOF is seen immediately by the parent. Otherwise, the
297 * child process sends a single byte.
298 * Note that use of this infrastructure is completely advisory,
299 * therefore, we keep error checks minimal.
301 close(notify_pipe[1]);
302 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
304 * At this point we know that fork() succeeded, but execvp()
305 * failed. Errors have been reported to our stderr.
307 wait_or_whine(cmd->pid, cmd->argv[0],
308 cmd->silent_exec_failure);
309 failed_errno = errno;
310 cmd->pid = -1;
312 close(notify_pipe[0]);
314 #else
316 int fhin = 0, fhout = 1, fherr = 2;
317 const char **sargv = cmd->argv;
318 char **env = environ;
320 if (cmd->no_stdin)
321 fhin = open("/dev/null", O_RDWR);
322 else if (need_in)
323 fhin = dup(fdin[0]);
324 else if (cmd->in)
325 fhin = dup(cmd->in);
327 if (cmd->no_stderr)
328 fherr = open("/dev/null", O_RDWR);
329 else if (need_err)
330 fherr = dup(fderr[1]);
331 else if (cmd->err > 2)
332 fherr = dup(cmd->err);
334 if (cmd->no_stdout)
335 fhout = open("/dev/null", O_RDWR);
336 else if (cmd->stdout_to_stderr)
337 fhout = dup(fherr);
338 else if (need_out)
339 fhout = dup(fdout[1]);
340 else if (cmd->out > 1)
341 fhout = dup(cmd->out);
343 if (cmd->dir)
344 die("chdir in start_command() not implemented");
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,
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 ASYNC_AS_THREAD
451 static void *run_thread(void *data)
453 struct async *async = data;
454 intptr_t ret = async->proc(async->proc_in, async->proc_out, async->data);
455 return (void *)ret;
457 #endif
459 int start_async(struct async *async)
461 int need_in, need_out;
462 int fdin[2], fdout[2];
463 int proc_in, proc_out;
465 need_in = async->in < 0;
466 if (need_in) {
467 if (pipe(fdin) < 0) {
468 if (async->out > 0)
469 close(async->out);
470 return error("cannot create pipe: %s", strerror(errno));
472 async->in = fdin[1];
475 need_out = async->out < 0;
476 if (need_out) {
477 if (pipe(fdout) < 0) {
478 if (need_in)
479 close_pair(fdin);
480 else if (async->in)
481 close(async->in);
482 return error("cannot create pipe: %s", strerror(errno));
484 async->out = fdout[0];
487 if (need_in)
488 proc_in = fdin[0];
489 else if (async->in)
490 proc_in = async->in;
491 else
492 proc_in = -1;
494 if (need_out)
495 proc_out = fdout[1];
496 else if (async->out)
497 proc_out = async->out;
498 else
499 proc_out = -1;
501 #ifndef ASYNC_AS_THREAD
502 /* Flush stdio before fork() to avoid cloning buffers */
503 fflush(NULL);
505 async->pid = fork();
506 if (async->pid < 0) {
507 error("fork (async) failed: %s", strerror(errno));
508 goto error;
510 if (!async->pid) {
511 if (need_in)
512 close(fdin[1]);
513 if (need_out)
514 close(fdout[0]);
515 exit(!!async->proc(proc_in, proc_out, async->data));
518 if (need_in)
519 close(fdin[0]);
520 else if (async->in)
521 close(async->in);
523 if (need_out)
524 close(fdout[1]);
525 else if (async->out)
526 close(async->out);
527 #else
528 if (proc_in >= 0)
529 set_cloexec(proc_in);
530 if (proc_out >= 0)
531 set_cloexec(proc_out);
532 async->proc_in = proc_in;
533 async->proc_out = proc_out;
535 int err = pthread_create(&async->tid, NULL, run_thread, async);
536 if (err) {
537 error("cannot create thread: %s", strerror(err));
538 goto error;
541 #endif
542 return 0;
544 error:
545 if (need_in)
546 close_pair(fdin);
547 else if (async->in)
548 close(async->in);
550 if (need_out)
551 close_pair(fdout);
552 else if (async->out)
553 close(async->out);
554 return -1;
557 int finish_async(struct async *async)
559 #ifndef ASYNC_AS_THREAD
560 return wait_or_whine(async->pid, "child process", 0);
561 #else
562 void *ret = (void *)(intptr_t)(-1);
564 if (pthread_join(async->tid, &ret))
565 error("pthread_join failed");
566 return (int)(intptr_t)ret;
567 #endif
570 int run_hook(const char *index_file, const char *name, ...)
572 struct child_process hook;
573 const char **argv = NULL, *env[2];
574 char index[PATH_MAX];
575 va_list args;
576 int ret;
577 size_t i = 0, alloc = 0;
579 if (access(git_path("hooks/%s", name), X_OK) < 0)
580 return 0;
582 va_start(args, name);
583 ALLOC_GROW(argv, i + 1, alloc);
584 argv[i++] = git_path("hooks/%s", name);
585 while (argv[i-1]) {
586 ALLOC_GROW(argv, i + 1, alloc);
587 argv[i++] = va_arg(args, const char *);
589 va_end(args);
591 memset(&hook, 0, sizeof(hook));
592 hook.argv = argv;
593 hook.no_stdin = 1;
594 hook.stdout_to_stderr = 1;
595 if (index_file) {
596 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
597 env[0] = index;
598 env[1] = NULL;
599 hook.env = env;
602 ret = run_command(&hook);
603 free(argv);
604 return ret;