]> git.ipfire.org Git - thirdparty/git.git/blob - run-command.c
run-command.c: remove dead assignment in while-loop
[thirdparty/git.git] / run-command.c
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec-cmd.h"
4 #include "sigchain.h"
5 #include "strvec.h"
6 #include "thread-utils.h"
7 #include "strbuf.h"
8 #include "string-list.h"
9 #include "quote.h"
10 #include "config.h"
11 #include "packfile.h"
12 #include "hook.h"
13 #include "compat/nonblock.h"
14
15 void child_process_init(struct child_process *child)
16 {
17 struct child_process blank = CHILD_PROCESS_INIT;
18 memcpy(child, &blank, sizeof(*child));
19 }
20
21 void child_process_clear(struct child_process *child)
22 {
23 strvec_clear(&child->args);
24 strvec_clear(&child->env);
25 }
26
27 struct child_to_clean {
28 pid_t pid;
29 struct child_process *process;
30 struct child_to_clean *next;
31 };
32 static struct child_to_clean *children_to_clean;
33 static int installed_child_cleanup_handler;
34
35 static void cleanup_children(int sig, int in_signal)
36 {
37 struct child_to_clean *children_to_wait_for = NULL;
38
39 while (children_to_clean) {
40 struct child_to_clean *p = children_to_clean;
41 children_to_clean = p->next;
42
43 if (p->process && !in_signal) {
44 struct child_process *process = p->process;
45 if (process->clean_on_exit_handler) {
46 trace_printf(
47 "trace: run_command: running exit handler for pid %"
48 PRIuMAX, (uintmax_t)p->pid
49 );
50 process->clean_on_exit_handler(process);
51 }
52 }
53
54 kill(p->pid, sig);
55
56 if (p->process && p->process->wait_after_clean) {
57 p->next = children_to_wait_for;
58 children_to_wait_for = p;
59 } else {
60 if (!in_signal)
61 free(p);
62 }
63 }
64
65 while (children_to_wait_for) {
66 struct child_to_clean *p = children_to_wait_for;
67 children_to_wait_for = p->next;
68
69 while (waitpid(p->pid, NULL, 0) < 0 && errno == EINTR)
70 ; /* spin waiting for process exit or error */
71
72 if (!in_signal)
73 free(p);
74 }
75 }
76
77 static void cleanup_children_on_signal(int sig)
78 {
79 cleanup_children(sig, 1);
80 sigchain_pop(sig);
81 raise(sig);
82 }
83
84 static void cleanup_children_on_exit(void)
85 {
86 cleanup_children(SIGTERM, 0);
87 }
88
89 static void mark_child_for_cleanup(pid_t pid, struct child_process *process)
90 {
91 struct child_to_clean *p = xmalloc(sizeof(*p));
92 p->pid = pid;
93 p->process = process;
94 p->next = children_to_clean;
95 children_to_clean = p;
96
97 if (!installed_child_cleanup_handler) {
98 atexit(cleanup_children_on_exit);
99 sigchain_push_common(cleanup_children_on_signal);
100 installed_child_cleanup_handler = 1;
101 }
102 }
103
104 static void clear_child_for_cleanup(pid_t pid)
105 {
106 struct child_to_clean **pp;
107
108 for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
109 struct child_to_clean *clean_me = *pp;
110
111 if (clean_me->pid == pid) {
112 *pp = clean_me->next;
113 free(clean_me);
114 return;
115 }
116 }
117 }
118
119 static inline void close_pair(int fd[2])
120 {
121 close(fd[0]);
122 close(fd[1]);
123 }
124
125 int is_executable(const char *name)
126 {
127 struct stat st;
128
129 if (stat(name, &st) || /* stat, not lstat */
130 !S_ISREG(st.st_mode))
131 return 0;
132
133 #if defined(GIT_WINDOWS_NATIVE)
134 /*
135 * On Windows there is no executable bit. The file extension
136 * indicates whether it can be run as an executable, and Git
137 * has special-handling to detect scripts and launch them
138 * through the indicated script interpreter. We test for the
139 * file extension first because virus scanners may make
140 * it quite expensive to open many files.
141 */
142 if (ends_with(name, ".exe"))
143 return S_IXUSR;
144
145 {
146 /*
147 * Now that we know it does not have an executable extension,
148 * peek into the file instead.
149 */
150 char buf[3] = { 0 };
151 int n;
152 int fd = open(name, O_RDONLY);
153 st.st_mode &= ~S_IXUSR;
154 if (fd >= 0) {
155 n = read(fd, buf, 2);
156 if (n == 2)
157 /* look for a she-bang */
158 if (!strcmp(buf, "#!"))
159 st.st_mode |= S_IXUSR;
160 close(fd);
161 }
162 }
163 #endif
164 return st.st_mode & S_IXUSR;
165 }
166
167 /*
168 * Search $PATH for a command. This emulates the path search that
169 * execvp would perform, without actually executing the command so it
170 * can be used before fork() to prepare to run a command using
171 * execve() or after execvp() to diagnose why it failed.
172 *
173 * The caller should ensure that file contains no directory
174 * separators.
175 *
176 * Returns the path to the command, as found in $PATH or NULL if the
177 * command could not be found. The caller inherits ownership of the memory
178 * used to store the resultant path.
179 *
180 * This should not be used on Windows, where the $PATH search rules
181 * are more complicated (e.g., a search for "foo" should find
182 * "foo.exe").
183 */
184 static char *locate_in_PATH(const char *file)
185 {
186 const char *p = getenv("PATH");
187 struct strbuf buf = STRBUF_INIT;
188
189 if (!p || !*p)
190 return NULL;
191
192 while (1) {
193 const char *end = strchrnul(p, ':');
194
195 strbuf_reset(&buf);
196
197 /* POSIX specifies an empty entry as the current directory. */
198 if (end != p) {
199 strbuf_add(&buf, p, end - p);
200 strbuf_addch(&buf, '/');
201 }
202 strbuf_addstr(&buf, file);
203
204 if (is_executable(buf.buf))
205 return strbuf_detach(&buf, NULL);
206
207 if (!*end)
208 break;
209 p = end + 1;
210 }
211
212 strbuf_release(&buf);
213 return NULL;
214 }
215
216 int exists_in_PATH(const char *command)
217 {
218 char *r = locate_in_PATH(command);
219 int found = r != NULL;
220 free(r);
221 return found;
222 }
223
224 int sane_execvp(const char *file, char * const argv[])
225 {
226 #ifndef GIT_WINDOWS_NATIVE
227 /*
228 * execvp() doesn't return, so we all we can do is tell trace2
229 * what we are about to do and let it leave a hint in the log
230 * (unless of course the execvp() fails).
231 *
232 * we skip this for Windows because the compat layer already
233 * has to emulate the execvp() call anyway.
234 */
235 int exec_id = trace2_exec(file, (const char **)argv);
236 #endif
237
238 if (!execvp(file, argv))
239 return 0; /* cannot happen ;-) */
240
241 #ifndef GIT_WINDOWS_NATIVE
242 {
243 int ec = errno;
244 trace2_exec_result(exec_id, ec);
245 errno = ec;
246 }
247 #endif
248
249 /*
250 * When a command can't be found because one of the directories
251 * listed in $PATH is unsearchable, execvp reports EACCES, but
252 * careful usability testing (read: analysis of occasional bug
253 * reports) reveals that "No such file or directory" is more
254 * intuitive.
255 *
256 * We avoid commands with "/", because execvp will not do $PATH
257 * lookups in that case.
258 *
259 * The reassignment of EACCES to errno looks like a no-op below,
260 * but we need to protect against exists_in_PATH overwriting errno.
261 */
262 if (errno == EACCES && !strchr(file, '/'))
263 errno = exists_in_PATH(file) ? EACCES : ENOENT;
264 else if (errno == ENOTDIR && !strchr(file, '/'))
265 errno = ENOENT;
266 return -1;
267 }
268
269 static const char **prepare_shell_cmd(struct strvec *out, const char **argv)
270 {
271 if (!argv[0])
272 BUG("shell command is empty");
273
274 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
275 #ifndef GIT_WINDOWS_NATIVE
276 strvec_push(out, SHELL_PATH);
277 #else
278 strvec_push(out, "sh");
279 #endif
280 strvec_push(out, "-c");
281
282 /*
283 * If we have no extra arguments, we do not even need to
284 * bother with the "$@" magic.
285 */
286 if (!argv[1])
287 strvec_push(out, argv[0]);
288 else
289 strvec_pushf(out, "%s \"$@\"", argv[0]);
290 }
291
292 strvec_pushv(out, argv);
293 return out->v;
294 }
295
296 #ifndef GIT_WINDOWS_NATIVE
297 static int child_notifier = -1;
298
299 enum child_errcode {
300 CHILD_ERR_CHDIR,
301 CHILD_ERR_DUP2,
302 CHILD_ERR_CLOSE,
303 CHILD_ERR_SIGPROCMASK,
304 CHILD_ERR_ENOENT,
305 CHILD_ERR_SILENT,
306 CHILD_ERR_ERRNO
307 };
308
309 struct child_err {
310 enum child_errcode err;
311 int syserr; /* errno */
312 };
313
314 static void child_die(enum child_errcode err)
315 {
316 struct child_err buf;
317
318 buf.err = err;
319 buf.syserr = errno;
320
321 /* write(2) on buf smaller than PIPE_BUF (min 512) is atomic: */
322 xwrite(child_notifier, &buf, sizeof(buf));
323 _exit(1);
324 }
325
326 static void child_dup2(int fd, int to)
327 {
328 if (dup2(fd, to) < 0)
329 child_die(CHILD_ERR_DUP2);
330 }
331
332 static void child_close(int fd)
333 {
334 if (close(fd))
335 child_die(CHILD_ERR_CLOSE);
336 }
337
338 static void child_close_pair(int fd[2])
339 {
340 child_close(fd[0]);
341 child_close(fd[1]);
342 }
343
344 static void child_error_fn(const char *err, va_list params)
345 {
346 const char msg[] = "error() should not be called in child\n";
347 xwrite(2, msg, sizeof(msg) - 1);
348 }
349
350 static void child_warn_fn(const char *err, va_list params)
351 {
352 const char msg[] = "warn() should not be called in child\n";
353 xwrite(2, msg, sizeof(msg) - 1);
354 }
355
356 static void NORETURN child_die_fn(const char *err, va_list params)
357 {
358 const char msg[] = "die() should not be called in child\n";
359 xwrite(2, msg, sizeof(msg) - 1);
360 _exit(2);
361 }
362
363 /* this runs in the parent process */
364 static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
365 {
366 static void (*old_errfn)(const char *err, va_list params);
367 report_fn die_message_routine = get_die_message_routine();
368
369 old_errfn = get_error_routine();
370 set_error_routine(die_message_routine);
371 errno = cerr->syserr;
372
373 switch (cerr->err) {
374 case CHILD_ERR_CHDIR:
375 error_errno("exec '%s': cd to '%s' failed",
376 cmd->args.v[0], cmd->dir);
377 break;
378 case CHILD_ERR_DUP2:
379 error_errno("dup2() in child failed");
380 break;
381 case CHILD_ERR_CLOSE:
382 error_errno("close() in child failed");
383 break;
384 case CHILD_ERR_SIGPROCMASK:
385 error_errno("sigprocmask failed restoring signals");
386 break;
387 case CHILD_ERR_ENOENT:
388 error_errno("cannot run %s", cmd->args.v[0]);
389 break;
390 case CHILD_ERR_SILENT:
391 break;
392 case CHILD_ERR_ERRNO:
393 error_errno("cannot exec '%s'", cmd->args.v[0]);
394 break;
395 }
396 set_error_routine(old_errfn);
397 }
398
399 static int prepare_cmd(struct strvec *out, const struct child_process *cmd)
400 {
401 if (!cmd->args.v[0])
402 BUG("command is empty");
403
404 /*
405 * Add SHELL_PATH so in the event exec fails with ENOEXEC we can
406 * attempt to interpret the command with 'sh'.
407 */
408 strvec_push(out, SHELL_PATH);
409
410 if (cmd->git_cmd) {
411 prepare_git_cmd(out, cmd->args.v);
412 } else if (cmd->use_shell) {
413 prepare_shell_cmd(out, cmd->args.v);
414 } else {
415 strvec_pushv(out, cmd->args.v);
416 }
417
418 /*
419 * If there are no dir separator characters in the command then perform
420 * a path lookup and use the resolved path as the command to exec. If
421 * there are dir separator characters, we have exec attempt to invoke
422 * the command directly.
423 */
424 if (!has_dir_sep(out->v[1])) {
425 char *program = locate_in_PATH(out->v[1]);
426 if (program) {
427 free((char *)out->v[1]);
428 out->v[1] = program;
429 } else {
430 strvec_clear(out);
431 errno = ENOENT;
432 return -1;
433 }
434 }
435
436 return 0;
437 }
438
439 static char **prep_childenv(const char *const *deltaenv)
440 {
441 extern char **environ;
442 char **childenv;
443 struct string_list env = STRING_LIST_INIT_DUP;
444 struct strbuf key = STRBUF_INIT;
445 const char *const *p;
446 int i;
447
448 /* Construct a sorted string list consisting of the current environ */
449 for (p = (const char *const *) environ; p && *p; p++) {
450 const char *equals = strchr(*p, '=');
451
452 if (equals) {
453 strbuf_reset(&key);
454 strbuf_add(&key, *p, equals - *p);
455 string_list_append(&env, key.buf)->util = (void *) *p;
456 } else {
457 string_list_append(&env, *p)->util = (void *) *p;
458 }
459 }
460 string_list_sort(&env);
461
462 /* Merge in 'deltaenv' with the current environ */
463 for (p = deltaenv; p && *p; p++) {
464 const char *equals = strchr(*p, '=');
465
466 if (equals) {
467 /* ('key=value'), insert or replace entry */
468 strbuf_reset(&key);
469 strbuf_add(&key, *p, equals - *p);
470 string_list_insert(&env, key.buf)->util = (void *) *p;
471 } else {
472 /* otherwise ('key') remove existing entry */
473 string_list_remove(&env, *p, 0);
474 }
475 }
476
477 /* Create an array of 'char *' to be used as the childenv */
478 ALLOC_ARRAY(childenv, env.nr + 1);
479 for (i = 0; i < env.nr; i++)
480 childenv[i] = env.items[i].util;
481 childenv[env.nr] = NULL;
482
483 string_list_clear(&env, 0);
484 strbuf_release(&key);
485 return childenv;
486 }
487
488 struct atfork_state {
489 #ifndef NO_PTHREADS
490 int cs;
491 #endif
492 sigset_t old;
493 };
494
495 #define CHECK_BUG(err, msg) \
496 do { \
497 int e = (err); \
498 if (e) \
499 BUG("%s: %s", msg, strerror(e)); \
500 } while(0)
501
502 static void atfork_prepare(struct atfork_state *as)
503 {
504 sigset_t all;
505
506 if (sigfillset(&all))
507 die_errno("sigfillset");
508 #ifdef NO_PTHREADS
509 if (sigprocmask(SIG_SETMASK, &all, &as->old))
510 die_errno("sigprocmask");
511 #else
512 CHECK_BUG(pthread_sigmask(SIG_SETMASK, &all, &as->old),
513 "blocking all signals");
514 CHECK_BUG(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &as->cs),
515 "disabling cancellation");
516 #endif
517 }
518
519 static void atfork_parent(struct atfork_state *as)
520 {
521 #ifdef NO_PTHREADS
522 if (sigprocmask(SIG_SETMASK, &as->old, NULL))
523 die_errno("sigprocmask");
524 #else
525 CHECK_BUG(pthread_setcancelstate(as->cs, NULL),
526 "re-enabling cancellation");
527 CHECK_BUG(pthread_sigmask(SIG_SETMASK, &as->old, NULL),
528 "restoring signal mask");
529 #endif
530 }
531 #endif /* GIT_WINDOWS_NATIVE */
532
533 static inline void set_cloexec(int fd)
534 {
535 int flags = fcntl(fd, F_GETFD);
536 if (flags >= 0)
537 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
538 }
539
540 static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
541 {
542 int status, code = -1;
543 pid_t waiting;
544 int failed_errno = 0;
545
546 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
547 ; /* nothing */
548
549 if (waiting < 0) {
550 failed_errno = errno;
551 if (!in_signal)
552 error_errno("waitpid for %s failed", argv0);
553 } else if (waiting != pid) {
554 if (!in_signal)
555 error("waitpid is confused (%s)", argv0);
556 } else if (WIFSIGNALED(status)) {
557 code = WTERMSIG(status);
558 if (!in_signal && code != SIGINT && code != SIGQUIT && code != SIGPIPE)
559 error("%s died of signal %d", argv0, code);
560 /*
561 * This return value is chosen so that code & 0xff
562 * mimics the exit code that a POSIX shell would report for
563 * a program that died from this signal.
564 */
565 code += 128;
566 } else if (WIFEXITED(status)) {
567 code = WEXITSTATUS(status);
568 } else {
569 if (!in_signal)
570 error("waitpid is confused (%s)", argv0);
571 }
572
573 if (!in_signal)
574 clear_child_for_cleanup(pid);
575
576 errno = failed_errno;
577 return code;
578 }
579
580 static void trace_add_env(struct strbuf *dst, const char *const *deltaenv)
581 {
582 struct string_list envs = STRING_LIST_INIT_DUP;
583 const char *const *e;
584 int i;
585 int printed_unset = 0;
586
587 /* Last one wins, see run-command.c:prep_childenv() for context */
588 for (e = deltaenv; e && *e; e++) {
589 struct strbuf key = STRBUF_INIT;
590 char *equals = strchr(*e, '=');
591
592 if (equals) {
593 strbuf_add(&key, *e, equals - *e);
594 string_list_insert(&envs, key.buf)->util = equals + 1;
595 } else {
596 string_list_insert(&envs, *e)->util = NULL;
597 }
598 strbuf_release(&key);
599 }
600
601 /* "unset X Y...;" */
602 for (i = 0; i < envs.nr; i++) {
603 const char *var = envs.items[i].string;
604 const char *val = envs.items[i].util;
605
606 if (val || !getenv(var))
607 continue;
608
609 if (!printed_unset) {
610 strbuf_addstr(dst, " unset");
611 printed_unset = 1;
612 }
613 strbuf_addf(dst, " %s", var);
614 }
615 if (printed_unset)
616 strbuf_addch(dst, ';');
617
618 /* ... followed by "A=B C=D ..." */
619 for (i = 0; i < envs.nr; i++) {
620 const char *var = envs.items[i].string;
621 const char *val = envs.items[i].util;
622 const char *oldval;
623
624 if (!val)
625 continue;
626
627 oldval = getenv(var);
628 if (oldval && !strcmp(val, oldval))
629 continue;
630
631 strbuf_addf(dst, " %s=", var);
632 sq_quote_buf_pretty(dst, val);
633 }
634 string_list_clear(&envs, 0);
635 }
636
637 static void trace_run_command(const struct child_process *cp)
638 {
639 struct strbuf buf = STRBUF_INIT;
640
641 if (!trace_want(&trace_default_key))
642 return;
643
644 strbuf_addstr(&buf, "trace: run_command:");
645 if (cp->dir) {
646 strbuf_addstr(&buf, " cd ");
647 sq_quote_buf_pretty(&buf, cp->dir);
648 strbuf_addch(&buf, ';');
649 }
650 trace_add_env(&buf, cp->env.v);
651 if (cp->git_cmd)
652 strbuf_addstr(&buf, " git");
653 sq_quote_argv_pretty(&buf, cp->args.v);
654
655 trace_printf("%s", buf.buf);
656 strbuf_release(&buf);
657 }
658
659 int start_command(struct child_process *cmd)
660 {
661 int need_in, need_out, need_err;
662 int fdin[2], fdout[2], fderr[2];
663 int failed_errno;
664 char *str;
665
666 /*
667 * In case of errors we must keep the promise to close FDs
668 * that have been passed in via ->in and ->out.
669 */
670
671 need_in = !cmd->no_stdin && cmd->in < 0;
672 if (need_in) {
673 if (pipe(fdin) < 0) {
674 failed_errno = errno;
675 if (cmd->out > 0)
676 close(cmd->out);
677 str = "standard input";
678 goto fail_pipe;
679 }
680 cmd->in = fdin[1];
681 }
682
683 need_out = !cmd->no_stdout
684 && !cmd->stdout_to_stderr
685 && cmd->out < 0;
686 if (need_out) {
687 if (pipe(fdout) < 0) {
688 failed_errno = errno;
689 if (need_in)
690 close_pair(fdin);
691 else if (cmd->in)
692 close(cmd->in);
693 str = "standard output";
694 goto fail_pipe;
695 }
696 cmd->out = fdout[0];
697 }
698
699 need_err = !cmd->no_stderr && cmd->err < 0;
700 if (need_err) {
701 if (pipe(fderr) < 0) {
702 failed_errno = errno;
703 if (need_in)
704 close_pair(fdin);
705 else if (cmd->in)
706 close(cmd->in);
707 if (need_out)
708 close_pair(fdout);
709 else if (cmd->out)
710 close(cmd->out);
711 str = "standard error";
712 fail_pipe:
713 error("cannot create %s pipe for %s: %s",
714 str, cmd->args.v[0], strerror(failed_errno));
715 child_process_clear(cmd);
716 errno = failed_errno;
717 return -1;
718 }
719 cmd->err = fderr[0];
720 }
721
722 trace2_child_start(cmd);
723 trace_run_command(cmd);
724
725 fflush(NULL);
726
727 if (cmd->close_object_store)
728 close_object_store(the_repository->objects);
729
730 #ifndef GIT_WINDOWS_NATIVE
731 {
732 int notify_pipe[2];
733 int null_fd = -1;
734 char **childenv;
735 struct strvec argv = STRVEC_INIT;
736 struct child_err cerr;
737 struct atfork_state as;
738
739 if (prepare_cmd(&argv, cmd) < 0) {
740 failed_errno = errno;
741 cmd->pid = -1;
742 if (!cmd->silent_exec_failure)
743 error_errno("cannot run %s", cmd->args.v[0]);
744 goto end_of_spawn;
745 }
746
747 if (pipe(notify_pipe))
748 notify_pipe[0] = notify_pipe[1] = -1;
749
750 if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) {
751 null_fd = xopen("/dev/null", O_RDWR | O_CLOEXEC);
752 set_cloexec(null_fd);
753 }
754
755 childenv = prep_childenv(cmd->env.v);
756 atfork_prepare(&as);
757
758 /*
759 * NOTE: In order to prevent deadlocking when using threads special
760 * care should be taken with the function calls made in between the
761 * fork() and exec() calls. No calls should be made to functions which
762 * require acquiring a lock (e.g. malloc) as the lock could have been
763 * held by another thread at the time of forking, causing the lock to
764 * never be released in the child process. This means only
765 * Async-Signal-Safe functions are permitted in the child.
766 */
767 cmd->pid = fork();
768 failed_errno = errno;
769 if (!cmd->pid) {
770 int sig;
771 /*
772 * Ensure the default die/error/warn routines do not get
773 * called, they can take stdio locks and malloc.
774 */
775 set_die_routine(child_die_fn);
776 set_error_routine(child_error_fn);
777 set_warn_routine(child_warn_fn);
778
779 close(notify_pipe[0]);
780 set_cloexec(notify_pipe[1]);
781 child_notifier = notify_pipe[1];
782
783 if (cmd->no_stdin)
784 child_dup2(null_fd, 0);
785 else if (need_in) {
786 child_dup2(fdin[0], 0);
787 child_close_pair(fdin);
788 } else if (cmd->in) {
789 child_dup2(cmd->in, 0);
790 child_close(cmd->in);
791 }
792
793 if (cmd->no_stderr)
794 child_dup2(null_fd, 2);
795 else if (need_err) {
796 child_dup2(fderr[1], 2);
797 child_close_pair(fderr);
798 } else if (cmd->err > 1) {
799 child_dup2(cmd->err, 2);
800 child_close(cmd->err);
801 }
802
803 if (cmd->no_stdout)
804 child_dup2(null_fd, 1);
805 else if (cmd->stdout_to_stderr)
806 child_dup2(2, 1);
807 else if (need_out) {
808 child_dup2(fdout[1], 1);
809 child_close_pair(fdout);
810 } else if (cmd->out > 1) {
811 child_dup2(cmd->out, 1);
812 child_close(cmd->out);
813 }
814
815 if (cmd->dir && chdir(cmd->dir))
816 child_die(CHILD_ERR_CHDIR);
817
818 /*
819 * restore default signal handlers here, in case
820 * we catch a signal right before execve below
821 */
822 for (sig = 1; sig < NSIG; sig++) {
823 /* ignored signals get reset to SIG_DFL on execve */
824 if (signal(sig, SIG_DFL) == SIG_IGN)
825 signal(sig, SIG_IGN);
826 }
827
828 if (sigprocmask(SIG_SETMASK, &as.old, NULL) != 0)
829 child_die(CHILD_ERR_SIGPROCMASK);
830
831 /*
832 * Attempt to exec using the command and arguments starting at
833 * argv.argv[1]. argv.argv[0] contains SHELL_PATH which will
834 * be used in the event exec failed with ENOEXEC at which point
835 * we will try to interpret the command using 'sh'.
836 */
837 execve(argv.v[1], (char *const *) argv.v + 1,
838 (char *const *) childenv);
839 if (errno == ENOEXEC)
840 execve(argv.v[0], (char *const *) argv.v,
841 (char *const *) childenv);
842
843 if (errno == ENOENT) {
844 if (cmd->silent_exec_failure)
845 child_die(CHILD_ERR_SILENT);
846 child_die(CHILD_ERR_ENOENT);
847 } else {
848 child_die(CHILD_ERR_ERRNO);
849 }
850 }
851 atfork_parent(&as);
852 if (cmd->pid < 0)
853 error_errno("cannot fork() for %s", cmd->args.v[0]);
854 else if (cmd->clean_on_exit)
855 mark_child_for_cleanup(cmd->pid, cmd);
856
857 /*
858 * Wait for child's exec. If the exec succeeds (or if fork()
859 * failed), EOF is seen immediately by the parent. Otherwise, the
860 * child process sends a child_err struct.
861 * Note that use of this infrastructure is completely advisory,
862 * therefore, we keep error checks minimal.
863 */
864 close(notify_pipe[1]);
865 if (xread(notify_pipe[0], &cerr, sizeof(cerr)) == sizeof(cerr)) {
866 /*
867 * At this point we know that fork() succeeded, but exec()
868 * failed. Errors have been reported to our stderr.
869 */
870 wait_or_whine(cmd->pid, cmd->args.v[0], 0);
871 child_err_spew(cmd, &cerr);
872 failed_errno = errno;
873 cmd->pid = -1;
874 }
875 close(notify_pipe[0]);
876
877 if (null_fd >= 0)
878 close(null_fd);
879 strvec_clear(&argv);
880 free(childenv);
881 }
882 end_of_spawn:
883
884 #else
885 {
886 int fhin = 0, fhout = 1, fherr = 2;
887 const char **sargv = cmd->args.v;
888 struct strvec nargv = STRVEC_INIT;
889
890 if (cmd->no_stdin)
891 fhin = open("/dev/null", O_RDWR);
892 else if (need_in)
893 fhin = dup(fdin[0]);
894 else if (cmd->in)
895 fhin = dup(cmd->in);
896
897 if (cmd->no_stderr)
898 fherr = open("/dev/null", O_RDWR);
899 else if (need_err)
900 fherr = dup(fderr[1]);
901 else if (cmd->err > 2)
902 fherr = dup(cmd->err);
903
904 if (cmd->no_stdout)
905 fhout = open("/dev/null", O_RDWR);
906 else if (cmd->stdout_to_stderr)
907 fhout = dup(fherr);
908 else if (need_out)
909 fhout = dup(fdout[1]);
910 else if (cmd->out > 1)
911 fhout = dup(cmd->out);
912
913 if (cmd->git_cmd)
914 cmd->args.v = prepare_git_cmd(&nargv, sargv);
915 else if (cmd->use_shell)
916 cmd->args.v = prepare_shell_cmd(&nargv, sargv);
917
918 cmd->pid = mingw_spawnvpe(cmd->args.v[0], cmd->args.v,
919 (char**) cmd->env.v,
920 cmd->dir, fhin, fhout, fherr);
921 failed_errno = errno;
922 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
923 error_errno("cannot spawn %s", cmd->args.v[0]);
924 if (cmd->clean_on_exit && cmd->pid >= 0)
925 mark_child_for_cleanup(cmd->pid, cmd);
926
927 strvec_clear(&nargv);
928 cmd->args.v = sargv;
929 if (fhin != 0)
930 close(fhin);
931 if (fhout != 1)
932 close(fhout);
933 if (fherr != 2)
934 close(fherr);
935 }
936 #endif
937
938 if (cmd->pid < 0) {
939 trace2_child_exit(cmd, -1);
940
941 if (need_in)
942 close_pair(fdin);
943 else if (cmd->in)
944 close(cmd->in);
945 if (need_out)
946 close_pair(fdout);
947 else if (cmd->out)
948 close(cmd->out);
949 if (need_err)
950 close_pair(fderr);
951 else if (cmd->err)
952 close(cmd->err);
953 child_process_clear(cmd);
954 errno = failed_errno;
955 return -1;
956 }
957
958 if (need_in)
959 close(fdin[0]);
960 else if (cmd->in)
961 close(cmd->in);
962
963 if (need_out)
964 close(fdout[1]);
965 else if (cmd->out)
966 close(cmd->out);
967
968 if (need_err)
969 close(fderr[1]);
970 else if (cmd->err)
971 close(cmd->err);
972
973 return 0;
974 }
975
976 int finish_command(struct child_process *cmd)
977 {
978 int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 0);
979 trace2_child_exit(cmd, ret);
980 child_process_clear(cmd);
981 invalidate_lstat_cache();
982 return ret;
983 }
984
985 int finish_command_in_signal(struct child_process *cmd)
986 {
987 int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 1);
988 if (ret != -1)
989 trace2_child_exit(cmd, ret);
990 return ret;
991 }
992
993
994 int run_command(struct child_process *cmd)
995 {
996 int code;
997
998 if (cmd->out < 0 || cmd->err < 0)
999 BUG("run_command with a pipe can cause deadlock");
1000
1001 code = start_command(cmd);
1002 if (code)
1003 return code;
1004 return finish_command(cmd);
1005 }
1006
1007 #ifndef NO_PTHREADS
1008 static pthread_t main_thread;
1009 static int main_thread_set;
1010 static pthread_key_t async_key;
1011 static pthread_key_t async_die_counter;
1012
1013 static void *run_thread(void *data)
1014 {
1015 struct async *async = data;
1016 intptr_t ret;
1017
1018 if (async->isolate_sigpipe) {
1019 sigset_t mask;
1020 sigemptyset(&mask);
1021 sigaddset(&mask, SIGPIPE);
1022 if (pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
1023 ret = error("unable to block SIGPIPE in async thread");
1024 return (void *)ret;
1025 }
1026 }
1027
1028 pthread_setspecific(async_key, async);
1029 ret = async->proc(async->proc_in, async->proc_out, async->data);
1030 return (void *)ret;
1031 }
1032
1033 static NORETURN void die_async(const char *err, va_list params)
1034 {
1035 report_fn die_message_fn = get_die_message_routine();
1036
1037 die_message_fn(err, params);
1038
1039 if (in_async()) {
1040 struct async *async = pthread_getspecific(async_key);
1041 if (async->proc_in >= 0)
1042 close(async->proc_in);
1043 if (async->proc_out >= 0)
1044 close(async->proc_out);
1045 pthread_exit((void *)128);
1046 }
1047
1048 exit(128);
1049 }
1050
1051 static int async_die_is_recursing(void)
1052 {
1053 void *ret = pthread_getspecific(async_die_counter);
1054 pthread_setspecific(async_die_counter, &async_die_counter); /* set to any non-NULL valid pointer */
1055 return ret != NULL;
1056 }
1057
1058 int in_async(void)
1059 {
1060 if (!main_thread_set)
1061 return 0; /* no asyncs started yet */
1062 return !pthread_equal(main_thread, pthread_self());
1063 }
1064
1065 static void NORETURN async_exit(int code)
1066 {
1067 pthread_exit((void *)(intptr_t)code);
1068 }
1069
1070 #else
1071
1072 static struct {
1073 void (**handlers)(void);
1074 size_t nr;
1075 size_t alloc;
1076 } git_atexit_hdlrs;
1077
1078 static int git_atexit_installed;
1079
1080 static void git_atexit_dispatch(void)
1081 {
1082 size_t i;
1083
1084 for (i=git_atexit_hdlrs.nr ; i ; i--)
1085 git_atexit_hdlrs.handlers[i-1]();
1086 }
1087
1088 static void git_atexit_clear(void)
1089 {
1090 free(git_atexit_hdlrs.handlers);
1091 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
1092 git_atexit_installed = 0;
1093 }
1094
1095 #undef atexit
1096 int git_atexit(void (*handler)(void))
1097 {
1098 ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
1099 git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
1100 if (!git_atexit_installed) {
1101 if (atexit(&git_atexit_dispatch))
1102 return -1;
1103 git_atexit_installed = 1;
1104 }
1105 return 0;
1106 }
1107 #define atexit git_atexit
1108
1109 static int process_is_async;
1110 int in_async(void)
1111 {
1112 return process_is_async;
1113 }
1114
1115 static void NORETURN async_exit(int code)
1116 {
1117 exit(code);
1118 }
1119
1120 #endif
1121
1122 void check_pipe(int err)
1123 {
1124 if (err == EPIPE) {
1125 if (in_async())
1126 async_exit(141);
1127
1128 signal(SIGPIPE, SIG_DFL);
1129 raise(SIGPIPE);
1130 /* Should never happen, but just in case... */
1131 exit(141);
1132 }
1133 }
1134
1135 int start_async(struct async *async)
1136 {
1137 int need_in, need_out;
1138 int fdin[2], fdout[2];
1139 int proc_in, proc_out;
1140
1141 need_in = async->in < 0;
1142 if (need_in) {
1143 if (pipe(fdin) < 0) {
1144 if (async->out > 0)
1145 close(async->out);
1146 return error_errno("cannot create pipe");
1147 }
1148 async->in = fdin[1];
1149 }
1150
1151 need_out = async->out < 0;
1152 if (need_out) {
1153 if (pipe(fdout) < 0) {
1154 if (need_in)
1155 close_pair(fdin);
1156 else if (async->in)
1157 close(async->in);
1158 return error_errno("cannot create pipe");
1159 }
1160 async->out = fdout[0];
1161 }
1162
1163 if (need_in)
1164 proc_in = fdin[0];
1165 else if (async->in)
1166 proc_in = async->in;
1167 else
1168 proc_in = -1;
1169
1170 if (need_out)
1171 proc_out = fdout[1];
1172 else if (async->out)
1173 proc_out = async->out;
1174 else
1175 proc_out = -1;
1176
1177 #ifdef NO_PTHREADS
1178 /* Flush stdio before fork() to avoid cloning buffers */
1179 fflush(NULL);
1180
1181 async->pid = fork();
1182 if (async->pid < 0) {
1183 error_errno("fork (async) failed");
1184 goto error;
1185 }
1186 if (!async->pid) {
1187 if (need_in)
1188 close(fdin[1]);
1189 if (need_out)
1190 close(fdout[0]);
1191 git_atexit_clear();
1192 process_is_async = 1;
1193 exit(!!async->proc(proc_in, proc_out, async->data));
1194 }
1195
1196 mark_child_for_cleanup(async->pid, NULL);
1197
1198 if (need_in)
1199 close(fdin[0]);
1200 else if (async->in)
1201 close(async->in);
1202
1203 if (need_out)
1204 close(fdout[1]);
1205 else if (async->out)
1206 close(async->out);
1207 #else
1208 if (!main_thread_set) {
1209 /*
1210 * We assume that the first time that start_async is called
1211 * it is from the main thread.
1212 */
1213 main_thread_set = 1;
1214 main_thread = pthread_self();
1215 pthread_key_create(&async_key, NULL);
1216 pthread_key_create(&async_die_counter, NULL);
1217 set_die_routine(die_async);
1218 set_die_is_recursing_routine(async_die_is_recursing);
1219 }
1220
1221 if (proc_in >= 0)
1222 set_cloexec(proc_in);
1223 if (proc_out >= 0)
1224 set_cloexec(proc_out);
1225 async->proc_in = proc_in;
1226 async->proc_out = proc_out;
1227 {
1228 int err = pthread_create(&async->tid, NULL, run_thread, async);
1229 if (err) {
1230 error(_("cannot create async thread: %s"), strerror(err));
1231 goto error;
1232 }
1233 }
1234 #endif
1235 return 0;
1236
1237 error:
1238 if (need_in)
1239 close_pair(fdin);
1240 else if (async->in)
1241 close(async->in);
1242
1243 if (need_out)
1244 close_pair(fdout);
1245 else if (async->out)
1246 close(async->out);
1247 return -1;
1248 }
1249
1250 int finish_async(struct async *async)
1251 {
1252 #ifdef NO_PTHREADS
1253 int ret = wait_or_whine(async->pid, "child process", 0);
1254
1255 invalidate_lstat_cache();
1256
1257 return ret;
1258 #else
1259 void *ret = (void *)(intptr_t)(-1);
1260
1261 if (pthread_join(async->tid, &ret))
1262 error("pthread_join failed");
1263 invalidate_lstat_cache();
1264 return (int)(intptr_t)ret;
1265
1266 #endif
1267 }
1268
1269 int async_with_fork(void)
1270 {
1271 #ifdef NO_PTHREADS
1272 return 1;
1273 #else
1274 return 0;
1275 #endif
1276 }
1277
1278 struct io_pump {
1279 /* initialized by caller */
1280 int fd;
1281 int type; /* POLLOUT or POLLIN */
1282 union {
1283 struct {
1284 const char *buf;
1285 size_t len;
1286 } out;
1287 struct {
1288 struct strbuf *buf;
1289 size_t hint;
1290 } in;
1291 } u;
1292
1293 /* returned by pump_io */
1294 int error; /* 0 for success, otherwise errno */
1295
1296 /* internal use */
1297 struct pollfd *pfd;
1298 };
1299
1300 static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
1301 {
1302 int pollsize = 0;
1303 int i;
1304
1305 for (i = 0; i < nr; i++) {
1306 struct io_pump *io = &slots[i];
1307 if (io->fd < 0)
1308 continue;
1309 pfd[pollsize].fd = io->fd;
1310 pfd[pollsize].events = io->type;
1311 io->pfd = &pfd[pollsize++];
1312 }
1313
1314 if (!pollsize)
1315 return 0;
1316
1317 if (poll(pfd, pollsize, -1) < 0) {
1318 if (errno == EINTR)
1319 return 1;
1320 die_errno("poll failed");
1321 }
1322
1323 for (i = 0; i < nr; i++) {
1324 struct io_pump *io = &slots[i];
1325
1326 if (io->fd < 0)
1327 continue;
1328
1329 if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL)))
1330 continue;
1331
1332 if (io->type == POLLOUT) {
1333 ssize_t len;
1334
1335 /*
1336 * Don't use xwrite() here. It loops forever on EAGAIN,
1337 * and we're in our own poll() loop here.
1338 *
1339 * Note that we lose xwrite()'s handling of MAX_IO_SIZE
1340 * and EINTR, so we have to implement those ourselves.
1341 */
1342 len = write(io->fd, io->u.out.buf,
1343 io->u.out.len <= MAX_IO_SIZE ?
1344 io->u.out.len : MAX_IO_SIZE);
1345 if (len < 0) {
1346 if (errno != EINTR && errno != EAGAIN &&
1347 errno != ENOSPC) {
1348 io->error = errno;
1349 close(io->fd);
1350 io->fd = -1;
1351 }
1352 } else {
1353 io->u.out.buf += len;
1354 io->u.out.len -= len;
1355 if (!io->u.out.len) {
1356 close(io->fd);
1357 io->fd = -1;
1358 }
1359 }
1360 }
1361
1362 if (io->type == POLLIN) {
1363 ssize_t len = strbuf_read_once(io->u.in.buf,
1364 io->fd, io->u.in.hint);
1365 if (len < 0)
1366 io->error = errno;
1367 if (len <= 0) {
1368 close(io->fd);
1369 io->fd = -1;
1370 }
1371 }
1372 }
1373
1374 return 1;
1375 }
1376
1377 static int pump_io(struct io_pump *slots, int nr)
1378 {
1379 struct pollfd *pfd;
1380 int i;
1381
1382 for (i = 0; i < nr; i++)
1383 slots[i].error = 0;
1384
1385 ALLOC_ARRAY(pfd, nr);
1386 while (pump_io_round(slots, nr, pfd))
1387 ; /* nothing */
1388 free(pfd);
1389
1390 /* There may be multiple errno values, so just pick the first. */
1391 for (i = 0; i < nr; i++) {
1392 if (slots[i].error) {
1393 errno = slots[i].error;
1394 return -1;
1395 }
1396 }
1397 return 0;
1398 }
1399
1400
1401 int pipe_command(struct child_process *cmd,
1402 const char *in, size_t in_len,
1403 struct strbuf *out, size_t out_hint,
1404 struct strbuf *err, size_t err_hint)
1405 {
1406 struct io_pump io[3];
1407 int nr = 0;
1408
1409 if (in)
1410 cmd->in = -1;
1411 if (out)
1412 cmd->out = -1;
1413 if (err)
1414 cmd->err = -1;
1415
1416 if (start_command(cmd) < 0)
1417 return -1;
1418
1419 if (in) {
1420 if (enable_pipe_nonblock(cmd->in) < 0) {
1421 error_errno("unable to make pipe non-blocking");
1422 close(cmd->in);
1423 if (out)
1424 close(cmd->out);
1425 if (err)
1426 close(cmd->err);
1427 return -1;
1428 }
1429 io[nr].fd = cmd->in;
1430 io[nr].type = POLLOUT;
1431 io[nr].u.out.buf = in;
1432 io[nr].u.out.len = in_len;
1433 nr++;
1434 }
1435 if (out) {
1436 io[nr].fd = cmd->out;
1437 io[nr].type = POLLIN;
1438 io[nr].u.in.buf = out;
1439 io[nr].u.in.hint = out_hint;
1440 nr++;
1441 }
1442 if (err) {
1443 io[nr].fd = cmd->err;
1444 io[nr].type = POLLIN;
1445 io[nr].u.in.buf = err;
1446 io[nr].u.in.hint = err_hint;
1447 nr++;
1448 }
1449
1450 if (pump_io(io, nr) < 0) {
1451 finish_command(cmd); /* throw away exit code */
1452 return -1;
1453 }
1454
1455 return finish_command(cmd);
1456 }
1457
1458 enum child_state {
1459 GIT_CP_FREE,
1460 GIT_CP_WORKING,
1461 GIT_CP_WAIT_CLEANUP,
1462 };
1463
1464 struct parallel_processes {
1465 size_t nr_processes;
1466
1467 struct {
1468 enum child_state state;
1469 struct child_process process;
1470 struct strbuf err;
1471 void *data;
1472 } *children;
1473 /*
1474 * The struct pollfd is logically part of *children,
1475 * but the system call expects it as its own array.
1476 */
1477 struct pollfd *pfd;
1478
1479 unsigned shutdown : 1;
1480
1481 size_t output_owner;
1482 struct strbuf buffered_output; /* of finished children */
1483 };
1484
1485 struct parallel_processes_for_signal {
1486 const struct run_process_parallel_opts *opts;
1487 const struct parallel_processes *pp;
1488 };
1489
1490 static void kill_children(const struct parallel_processes *pp,
1491 const struct run_process_parallel_opts *opts,
1492 int signo)
1493 {
1494 for (size_t i = 0; i < opts->processes; i++)
1495 if (pp->children[i].state == GIT_CP_WORKING)
1496 kill(pp->children[i].process.pid, signo);
1497 }
1498
1499 static void kill_children_signal(const struct parallel_processes_for_signal *pp_sig,
1500 int signo)
1501 {
1502 kill_children(pp_sig->pp, pp_sig->opts, signo);
1503 }
1504
1505 static struct parallel_processes_for_signal *pp_for_signal;
1506
1507 static void handle_children_on_signal(int signo)
1508 {
1509 kill_children_signal(pp_for_signal, signo);
1510 sigchain_pop(signo);
1511 raise(signo);
1512 }
1513
1514 static void pp_init(struct parallel_processes *pp,
1515 const struct run_process_parallel_opts *opts,
1516 struct parallel_processes_for_signal *pp_sig)
1517 {
1518 const size_t n = opts->processes;
1519
1520 if (!n)
1521 BUG("you must provide a non-zero number of processes!");
1522
1523 trace_printf("run_processes_parallel: preparing to run up to %"PRIuMAX" tasks",
1524 (uintmax_t)n);
1525
1526 if (!opts->get_next_task)
1527 BUG("you need to specify a get_next_task function");
1528
1529 CALLOC_ARRAY(pp->children, n);
1530 if (!opts->ungroup)
1531 CALLOC_ARRAY(pp->pfd, n);
1532
1533 for (size_t i = 0; i < n; i++) {
1534 strbuf_init(&pp->children[i].err, 0);
1535 child_process_init(&pp->children[i].process);
1536 if (pp->pfd) {
1537 pp->pfd[i].events = POLLIN | POLLHUP;
1538 pp->pfd[i].fd = -1;
1539 }
1540 }
1541
1542 pp_sig->pp = pp;
1543 pp_sig->opts = opts;
1544 pp_for_signal = pp_sig;
1545 sigchain_push_common(handle_children_on_signal);
1546 }
1547
1548 static void pp_cleanup(struct parallel_processes *pp,
1549 const struct run_process_parallel_opts *opts)
1550 {
1551 trace_printf("run_processes_parallel: done");
1552 for (size_t i = 0; i < opts->processes; i++) {
1553 strbuf_release(&pp->children[i].err);
1554 child_process_clear(&pp->children[i].process);
1555 }
1556
1557 free(pp->children);
1558 free(pp->pfd);
1559
1560 /*
1561 * When get_next_task added messages to the buffer in its last
1562 * iteration, the buffered output is non empty.
1563 */
1564 strbuf_write(&pp->buffered_output, stderr);
1565 strbuf_release(&pp->buffered_output);
1566
1567 sigchain_pop_common();
1568 }
1569
1570 /* returns
1571 * 0 if a new task was started.
1572 * 1 if no new jobs was started (get_next_task ran out of work, non critical
1573 * problem with starting a new command)
1574 * <0 no new job was started, user wishes to shutdown early. Use negative code
1575 * to signal the children.
1576 */
1577 static int pp_start_one(struct parallel_processes *pp,
1578 const struct run_process_parallel_opts *opts)
1579 {
1580 size_t i;
1581 int code;
1582
1583 for (i = 0; i < opts->processes; i++)
1584 if (pp->children[i].state == GIT_CP_FREE)
1585 break;
1586 if (i == opts->processes)
1587 BUG("bookkeeping is hard");
1588
1589 code = opts->get_next_task(&pp->children[i].process,
1590 opts->ungroup ? NULL : &pp->children[i].err,
1591 opts->data,
1592 &pp->children[i].data);
1593 if (!code) {
1594 if (!opts->ungroup) {
1595 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1596 strbuf_reset(&pp->children[i].err);
1597 }
1598 return 1;
1599 }
1600 if (!opts->ungroup) {
1601 pp->children[i].process.err = -1;
1602 pp->children[i].process.stdout_to_stderr = 1;
1603 }
1604 pp->children[i].process.no_stdin = 1;
1605
1606 if (start_command(&pp->children[i].process)) {
1607 if (opts->start_failure)
1608 code = opts->start_failure(opts->ungroup ? NULL :
1609 &pp->children[i].err,
1610 opts->data,
1611 pp->children[i].data);
1612 else
1613 code = 0;
1614
1615 if (!opts->ungroup) {
1616 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1617 strbuf_reset(&pp->children[i].err);
1618 }
1619 if (code)
1620 pp->shutdown = 1;
1621 return code;
1622 }
1623
1624 pp->nr_processes++;
1625 pp->children[i].state = GIT_CP_WORKING;
1626 if (pp->pfd)
1627 pp->pfd[i].fd = pp->children[i].process.err;
1628 return 0;
1629 }
1630
1631 static void pp_buffer_stderr(struct parallel_processes *pp,
1632 const struct run_process_parallel_opts *opts,
1633 int output_timeout)
1634 {
1635 while (poll(pp->pfd, opts->processes, output_timeout) < 0) {
1636 if (errno == EINTR)
1637 continue;
1638 pp_cleanup(pp, opts);
1639 die_errno("poll");
1640 }
1641
1642 /* Buffer output from all pipes. */
1643 for (size_t i = 0; i < opts->processes; i++) {
1644 if (pp->children[i].state == GIT_CP_WORKING &&
1645 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1646 int n = strbuf_read_once(&pp->children[i].err,
1647 pp->children[i].process.err, 0);
1648 if (n == 0) {
1649 close(pp->children[i].process.err);
1650 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1651 } else if (n < 0)
1652 if (errno != EAGAIN)
1653 die_errno("read");
1654 }
1655 }
1656 }
1657
1658 static void pp_output(const struct parallel_processes *pp)
1659 {
1660 size_t i = pp->output_owner;
1661
1662 if (pp->children[i].state == GIT_CP_WORKING &&
1663 pp->children[i].err.len) {
1664 strbuf_write(&pp->children[i].err, stderr);
1665 strbuf_reset(&pp->children[i].err);
1666 }
1667 }
1668
1669 static int pp_collect_finished(struct parallel_processes *pp,
1670 const struct run_process_parallel_opts *opts)
1671 {
1672 int code;
1673 size_t i;
1674 int result = 0;
1675
1676 while (pp->nr_processes > 0) {
1677 for (i = 0; i < opts->processes; i++)
1678 if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1679 break;
1680 if (i == opts->processes)
1681 break;
1682
1683 code = finish_command(&pp->children[i].process);
1684
1685 if (opts->task_finished)
1686 code = opts->task_finished(code, opts->ungroup ? NULL :
1687 &pp->children[i].err, opts->data,
1688 pp->children[i].data);
1689 else
1690 code = 0;
1691
1692 if (code)
1693 result = code;
1694 if (code < 0)
1695 break;
1696
1697 pp->nr_processes--;
1698 pp->children[i].state = GIT_CP_FREE;
1699 if (pp->pfd)
1700 pp->pfd[i].fd = -1;
1701 child_process_init(&pp->children[i].process);
1702
1703 if (opts->ungroup) {
1704 ; /* no strbuf_*() work to do here */
1705 } else if (i != pp->output_owner) {
1706 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1707 strbuf_reset(&pp->children[i].err);
1708 } else {
1709 const size_t n = opts->processes;
1710
1711 strbuf_write(&pp->children[i].err, stderr);
1712 strbuf_reset(&pp->children[i].err);
1713
1714 /* Output all other finished child processes */
1715 strbuf_write(&pp->buffered_output, stderr);
1716 strbuf_reset(&pp->buffered_output);
1717
1718 /*
1719 * Pick next process to output live.
1720 * NEEDSWORK:
1721 * For now we pick it randomly by doing a round
1722 * robin. Later we may want to pick the one with
1723 * the most output or the longest or shortest
1724 * running process time.
1725 */
1726 for (i = 0; i < n; i++)
1727 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1728 break;
1729 pp->output_owner = (pp->output_owner + i) % n;
1730 }
1731 }
1732 return result;
1733 }
1734
1735 void run_processes_parallel(const struct run_process_parallel_opts *opts)
1736 {
1737 int i, code;
1738 int output_timeout = 100;
1739 int spawn_cap = 4;
1740 struct parallel_processes_for_signal pp_sig;
1741 struct parallel_processes pp = {
1742 .buffered_output = STRBUF_INIT,
1743 };
1744 /* options */
1745 const char *tr2_category = opts->tr2_category;
1746 const char *tr2_label = opts->tr2_label;
1747 const int do_trace2 = tr2_category && tr2_label;
1748
1749 if (do_trace2)
1750 trace2_region_enter_printf(tr2_category, tr2_label, NULL,
1751 "max:%d", opts->processes);
1752
1753 pp_init(&pp, opts, &pp_sig);
1754 while (1) {
1755 for (i = 0;
1756 i < spawn_cap && !pp.shutdown &&
1757 pp.nr_processes < opts->processes;
1758 i++) {
1759 code = pp_start_one(&pp, opts);
1760 if (!code)
1761 continue;
1762 if (code < 0) {
1763 pp.shutdown = 1;
1764 kill_children(&pp, opts, -code);
1765 }
1766 break;
1767 }
1768 if (!pp.nr_processes)
1769 break;
1770 if (opts->ungroup) {
1771 for (size_t i = 0; i < opts->processes; i++)
1772 pp.children[i].state = GIT_CP_WAIT_CLEANUP;
1773 } else {
1774 pp_buffer_stderr(&pp, opts, output_timeout);
1775 pp_output(&pp);
1776 }
1777 code = pp_collect_finished(&pp, opts);
1778 if (code) {
1779 pp.shutdown = 1;
1780 if (code < 0)
1781 kill_children(&pp, opts,-code);
1782 }
1783 }
1784
1785 pp_cleanup(&pp, opts);
1786
1787 if (do_trace2)
1788 trace2_region_leave(tr2_category, tr2_label, NULL);
1789 }
1790
1791 int run_auto_maintenance(int quiet)
1792 {
1793 int enabled;
1794 struct child_process maint = CHILD_PROCESS_INIT;
1795
1796 if (!git_config_get_bool("maintenance.auto", &enabled) &&
1797 !enabled)
1798 return 0;
1799
1800 maint.git_cmd = 1;
1801 maint.close_object_store = 1;
1802 strvec_pushl(&maint.args, "maintenance", "run", "--auto", NULL);
1803 strvec_push(&maint.args, quiet ? "--quiet" : "--no-quiet");
1804
1805 return run_command(&maint);
1806 }
1807
1808 void prepare_other_repo_env(struct strvec *env, const char *new_git_dir)
1809 {
1810 const char * const *var;
1811
1812 for (var = local_repo_env; *var; var++) {
1813 if (strcmp(*var, CONFIG_DATA_ENVIRONMENT) &&
1814 strcmp(*var, CONFIG_COUNT_ENVIRONMENT))
1815 strvec_push(env, *var);
1816 }
1817 strvec_pushf(env, "%s=%s", GIT_DIR_ENVIRONMENT, new_git_dir);
1818 }
1819
1820 enum start_bg_result start_bg_command(struct child_process *cmd,
1821 start_bg_wait_cb *wait_cb,
1822 void *cb_data,
1823 unsigned int timeout_sec)
1824 {
1825 enum start_bg_result sbgr = SBGR_ERROR;
1826 int ret;
1827 int wait_status;
1828 pid_t pid_seen;
1829 time_t time_limit;
1830
1831 /*
1832 * We do not allow clean-on-exit because the child process
1833 * should persist in the background and possibly/probably
1834 * after this process exits. So we don't want to kill the
1835 * child during our atexit routine.
1836 */
1837 if (cmd->clean_on_exit)
1838 BUG("start_bg_command() does not allow non-zero clean_on_exit");
1839
1840 if (!cmd->trace2_child_class)
1841 cmd->trace2_child_class = "background";
1842
1843 ret = start_command(cmd);
1844 if (ret) {
1845 /*
1846 * We assume that if `start_command()` fails, we
1847 * either get a complete `trace2_child_start() /
1848 * trace2_child_exit()` pair or it fails before the
1849 * `trace2_child_start()` is emitted, so we do not
1850 * need to worry about it here.
1851 *
1852 * We also assume that `start_command()` does not add
1853 * us to the cleanup list. And that it calls
1854 * `child_process_clear()`.
1855 */
1856 sbgr = SBGR_ERROR;
1857 goto done;
1858 }
1859
1860 time(&time_limit);
1861 time_limit += timeout_sec;
1862
1863 wait:
1864 pid_seen = waitpid(cmd->pid, &wait_status, WNOHANG);
1865
1866 if (!pid_seen) {
1867 /*
1868 * The child is currently running. Ask the callback
1869 * if the child is ready to do work or whether we
1870 * should keep waiting for it to boot up.
1871 */
1872 ret = (*wait_cb)(cmd, cb_data);
1873 if (!ret) {
1874 /*
1875 * The child is running and "ready".
1876 */
1877 trace2_child_ready(cmd, "ready");
1878 sbgr = SBGR_READY;
1879 goto done;
1880 } else if (ret > 0) {
1881 /*
1882 * The callback said to give it more time to boot up
1883 * (subject to our timeout limit).
1884 */
1885 time_t now;
1886
1887 time(&now);
1888 if (now < time_limit)
1889 goto wait;
1890
1891 /*
1892 * Our timeout has expired. We don't try to
1893 * kill the child, but rather let it continue
1894 * (hopefully) trying to startup.
1895 */
1896 trace2_child_ready(cmd, "timeout");
1897 sbgr = SBGR_TIMEOUT;
1898 goto done;
1899 } else {
1900 /*
1901 * The cb gave up on this child. It is still running,
1902 * but our cb got an error trying to probe it.
1903 */
1904 trace2_child_ready(cmd, "error");
1905 sbgr = SBGR_CB_ERROR;
1906 goto done;
1907 }
1908 }
1909
1910 else if (pid_seen == cmd->pid) {
1911 int child_code = -1;
1912
1913 /*
1914 * The child started, but exited or was terminated
1915 * before becoming "ready".
1916 *
1917 * We try to match the behavior of `wait_or_whine()`
1918 * WRT the handling of WIFSIGNALED() and WIFEXITED()
1919 * and convert the child's status to a return code for
1920 * tracing purposes and emit the `trace2_child_exit()`
1921 * event.
1922 *
1923 * We do not want the wait_or_whine() error message
1924 * because we will be called by client-side library
1925 * routines.
1926 */
1927 if (WIFEXITED(wait_status))
1928 child_code = WEXITSTATUS(wait_status);
1929 else if (WIFSIGNALED(wait_status))
1930 child_code = WTERMSIG(wait_status) + 128;
1931 trace2_child_exit(cmd, child_code);
1932
1933 sbgr = SBGR_DIED;
1934 goto done;
1935 }
1936
1937 else if (pid_seen < 0 && errno == EINTR)
1938 goto wait;
1939
1940 trace2_child_exit(cmd, -1);
1941 sbgr = SBGR_ERROR;
1942
1943 done:
1944 child_process_clear(cmd);
1945 invalidate_lstat_cache();
1946 return sbgr;
1947 }