blob: 50cc011654e1734cf79c4b073f130d909ecbf07f [file] [log] [blame]
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +02001#include "cache.h"
2#include "run-command.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07003#include "exec-cmd.h"
Jeff Kingafe19ff2012-01-07 12:42:43 +01004#include "sigchain.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -04005#include "strvec.h"
Stefan Bellerc553c722015-12-15 16:04:10 -08006#include "thread-utils.h"
7#include "strbuf.h"
Damien Mariéf805a002017-10-06 08:07:55 +00008#include "string-list.h"
Nguyễn Thái Ngọc Duye73dd782018-01-18 16:45:09 +07009#include "quote.h"
Derrick Stolee1942d482020-08-28 15:45:12 +000010#include "config.h"
Johannes Schindelin28d04e12021-09-09 09:47:06 +000011#include "packfile.h"
Ævar Arnfjörð Bjarmason5e3aba32021-09-26 21:03:26 +020012#include "hook.h"
Jeff King716c1f62022-08-17 02:10:22 -040013#include "compat/nonblock.h"
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +020014
René Scharfe483bbd42014-08-19 21:10:48 +020015void child_process_init(struct child_process *child)
16{
Ævar Arnfjörð Bjarmason5726a6b2021-07-01 12:51:26 +020017 struct child_process blank = CHILD_PROCESS_INIT;
18 memcpy(child, &blank, sizeof(*child));
René Scharfe483bbd42014-08-19 21:10:48 +020019}
20
René Scharfe2d716082015-10-24 14:11:27 +020021void child_process_clear(struct child_process *child)
22{
Jeff Kingc972bf42020-07-28 16:25:12 -040023 strvec_clear(&child->args);
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +020024 strvec_clear(&child->env);
René Scharfe2d716082015-10-24 14:11:27 +020025}
26
Jeff Kingafe19ff2012-01-07 12:42:43 +010027struct child_to_clean {
28 pid_t pid;
Lars Schneiderac2fbaa2016-10-16 16:20:28 -070029 struct child_process *process;
Jeff Kingafe19ff2012-01-07 12:42:43 +010030 struct child_to_clean *next;
31};
32static struct child_to_clean *children_to_clean;
33static int installed_child_cleanup_handler;
34
Takashi Iwai507d7802015-09-04 11:35:57 +020035static void cleanup_children(int sig, int in_signal)
Jeff Kingafe19ff2012-01-07 12:42:43 +010036{
Jeff King46df6902017-01-06 20:22:23 -050037 struct child_to_clean *children_to_wait_for = NULL;
38
Jeff Kingafe19ff2012-01-07 12:42:43 +010039 while (children_to_clean) {
40 struct child_to_clean *p = children_to_clean;
41 children_to_clean = p->next;
Lars Schneiderac2fbaa2016-10-16 16:20:28 -070042
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
Jeff Kingafe19ff2012-01-07 12:42:43 +010054 kill(p->pid, sig);
Jeff King46df6902017-01-06 20:22:23 -050055
Jeff King7b919292017-03-17 19:20:04 -040056 if (p->process && p->process->wait_after_clean) {
Jeff King46df6902017-01-06 20:22:23 -050057 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
Takashi Iwai507d7802015-09-04 11:35:57 +020072 if (!in_signal)
73 free(p);
Jeff Kingafe19ff2012-01-07 12:42:43 +010074 }
75}
76
77static void cleanup_children_on_signal(int sig)
78{
Takashi Iwai507d7802015-09-04 11:35:57 +020079 cleanup_children(sig, 1);
Jeff Kingafe19ff2012-01-07 12:42:43 +010080 sigchain_pop(sig);
81 raise(sig);
82}
83
84static void cleanup_children_on_exit(void)
85{
Takashi Iwai507d7802015-09-04 11:35:57 +020086 cleanup_children(SIGTERM, 0);
Jeff Kingafe19ff2012-01-07 12:42:43 +010087}
88
Lars Schneiderac2fbaa2016-10-16 16:20:28 -070089static void mark_child_for_cleanup(pid_t pid, struct child_process *process)
Jeff Kingafe19ff2012-01-07 12:42:43 +010090{
91 struct child_to_clean *p = xmalloc(sizeof(*p));
92 p->pid = pid;
Lars Schneiderac2fbaa2016-10-16 16:20:28 -070093 p->process = process;
Jeff Kingafe19ff2012-01-07 12:42:43 +010094 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
104static void clear_child_for_cleanup(pid_t pid)
105{
David Gouldbdee3972012-09-11 15:32:47 +0100106 struct child_to_clean **pp;
Jeff Kingafe19ff2012-01-07 12:42:43 +0100107
David Gouldbdee3972012-09-11 15:32:47 +0100108 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);
Jeff Kingafe19ff2012-01-07 12:42:43 +0100114 return;
115 }
116 }
117}
118
Shawn O. Pearce9dc09c72007-03-12 14:37:28 -0400119static inline void close_pair(int fd[2])
120{
121 close(fd[0]);
122 close(fd[1]);
123}
124
Brandon Williams38124a42017-04-25 16:46:59 -0700125int 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
Brandon Williams94028312017-04-25 16:47:00 -0700167/*
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 */
Jeff King38f865c2012-03-30 03:52:18 -0400184static 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
Brandon Williams94028312017-04-25 16:47:00 -0700204 if (is_executable(buf.buf))
Jeff King38f865c2012-03-30 03:52:18 -0400205 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
Pranit Bauva3f36e6f2021-09-13 19:39:01 +0200216int exists_in_PATH(const char *command)
Jeff King38f865c2012-03-30 03:52:18 -0400217{
Pranit Bauva3f36e6f2021-09-13 19:39:01 +0200218 char *r = locate_in_PATH(command);
brian m. carlson63ab08f2020-01-07 01:36:40 +0000219 int found = r != NULL;
Jeff King38f865c2012-03-30 03:52:18 -0400220 free(r);
brian m. carlson63ab08f2020-01-07 01:36:40 +0000221 return found;
Jeff King38f865c2012-03-30 03:52:18 -0400222}
223
224int sane_execvp(const char *file, char * const argv[])
225{
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800226#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
Jeff King38f865c2012-03-30 03:52:18 -0400238 if (!execvp(file, argv))
239 return 0; /* cannot happen ;-) */
240
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800241#ifndef GIT_WINDOWS_NATIVE
242 {
243 int ec = errno;
244 trace2_exec_result(exec_id, ec);
245 errno = ec;
246 }
247#endif
248
Jeff King38f865c2012-03-30 03:52:18 -0400249 /*
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;
Junio C Hamanoa7855082012-07-31 12:51:30 -0700264 else if (errno == ENOTDIR && !strchr(file, '/'))
265 errno = ENOENT;
Jeff King38f865c2012-03-30 03:52:18 -0400266 return -1;
267}
268
Jeff Kingc972bf42020-07-28 16:25:12 -0400269static const char **prepare_shell_cmd(struct strvec *out, const char **argv)
Jeff King8dba1e62009-12-30 05:53:16 -0500270{
Jeff King20574f52016-02-22 17:44:39 -0500271 if (!argv[0])
Johannes Schindelin033abf92018-05-02 11:38:39 +0200272 BUG("shell command is empty");
Jeff King8dba1e62009-12-30 05:53:16 -0500273
Jeff Kingf4456442009-12-30 05:55:36 -0500274 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
Jonathan Nieder380395d2013-05-02 20:26:08 +0100275#ifndef GIT_WINDOWS_NATIVE
Jeff Kingc972bf42020-07-28 16:25:12 -0400276 strvec_push(out, SHELL_PATH);
Johannes Sixt77629752012-04-17 09:03:21 +0200277#else
Jeff Kingc972bf42020-07-28 16:25:12 -0400278 strvec_push(out, "sh");
Johannes Sixt77629752012-04-17 09:03:21 +0200279#endif
Jeff Kingc972bf42020-07-28 16:25:12 -0400280 strvec_push(out, "-c");
Jeff King8dba1e62009-12-30 05:53:16 -0500281
Jeff King20574f52016-02-22 17:44:39 -0500282 /*
283 * If we have no extra arguments, we do not even need to
284 * bother with the "$@" magic.
285 */
286 if (!argv[1])
Jeff Kingc972bf42020-07-28 16:25:12 -0400287 strvec_push(out, argv[0]);
Jeff King20574f52016-02-22 17:44:39 -0500288 else
Jeff Kingc972bf42020-07-28 16:25:12 -0400289 strvec_pushf(out, "%s \"$@\"", argv[0]);
Jeff King8dba1e62009-12-30 05:53:16 -0500290 }
291
Jeff Kingc972bf42020-07-28 16:25:12 -0400292 strvec_pushv(out, argv);
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400293 return out->v;
Jeff King8dba1e62009-12-30 05:53:16 -0500294}
295
Jonathan Nieder380395d2013-05-02 20:26:08 +0100296#ifndef GIT_WINDOWS_NATIVE
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100297static int child_notifier = -1;
298
Brandon Williams79319b12017-04-19 16:13:24 -0700299enum child_errcode {
300 CHILD_ERR_CHDIR,
Brandon Williams53fa6752017-04-19 16:13:25 -0700301 CHILD_ERR_DUP2,
302 CHILD_ERR_CLOSE,
Eric Wong45afb1c2017-04-19 16:13:27 -0700303 CHILD_ERR_SIGPROCMASK,
Brandon Williams79319b12017-04-19 16:13:24 -0700304 CHILD_ERR_ENOENT,
305 CHILD_ERR_SILENT,
306 CHILD_ERR_ERRNO
307};
308
309struct child_err {
310 enum child_errcode err;
311 int syserr; /* errno */
312};
313
314static void child_die(enum child_errcode err)
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100315{
Brandon Williams79319b12017-04-19 16:13:24 -0700316 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
Brandon Williams53fa6752017-04-19 16:13:25 -0700326static void child_dup2(int fd, int to)
327{
328 if (dup2(fd, to) < 0)
329 child_die(CHILD_ERR_DUP2);
330}
331
332static void child_close(int fd)
333{
334 if (close(fd))
335 child_die(CHILD_ERR_CLOSE);
336}
337
338static void child_close_pair(int fd[2])
339{
340 child_close(fd[0]);
341 child_close(fd[1]);
342}
343
Brandon Williams79319b12017-04-19 16:13:24 -0700344static 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
350static 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
356static 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 */
364static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
365{
366 static void (*old_errfn)(const char *err, va_list params);
Ævar Arnfjörð Bjarmasone081a7c2021-12-07 19:26:30 +0100367 report_fn die_message_routine = get_die_message_routine();
Brandon Williams79319b12017-04-19 16:13:24 -0700368
369 old_errfn = get_error_routine();
Ævar Arnfjörð Bjarmasone081a7c2021-12-07 19:26:30 +0100370 set_error_routine(die_message_routine);
Brandon Williams79319b12017-04-19 16:13:24 -0700371 errno = cerr->syserr;
372
373 switch (cerr->err) {
374 case CHILD_ERR_CHDIR:
375 error_errno("exec '%s': cd to '%s' failed",
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100376 cmd->args.v[0], cmd->dir);
Brandon Williams79319b12017-04-19 16:13:24 -0700377 break;
Brandon Williams53fa6752017-04-19 16:13:25 -0700378 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;
Eric Wong45afb1c2017-04-19 16:13:27 -0700384 case CHILD_ERR_SIGPROCMASK:
385 error_errno("sigprocmask failed restoring signals");
386 break;
Brandon Williams79319b12017-04-19 16:13:24 -0700387 case CHILD_ERR_ENOENT:
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100388 error_errno("cannot run %s", cmd->args.v[0]);
Brandon Williams79319b12017-04-19 16:13:24 -0700389 break;
390 case CHILD_ERR_SILENT:
391 break;
392 case CHILD_ERR_ERRNO:
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100393 error_errno("cannot exec '%s'", cmd->args.v[0]);
Brandon Williams79319b12017-04-19 16:13:24 -0700394 break;
395 }
396 set_error_routine(old_errfn);
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100397}
Brandon Williams3967e252017-04-19 16:13:19 -0700398
Jeff Kingc972bf42020-07-28 16:25:12 -0400399static int prepare_cmd(struct strvec *out, const struct child_process *cmd)
Brandon Williams3967e252017-04-19 16:13:19 -0700400{
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100401 if (!cmd->args.v[0])
Johannes Schindelin033abf92018-05-02 11:38:39 +0200402 BUG("command is empty");
Brandon Williams3967e252017-04-19 16:13:19 -0700403
Brandon Williamse3a43442017-04-19 16:13:20 -0700404 /*
405 * Add SHELL_PATH so in the event exec fails with ENOEXEC we can
406 * attempt to interpret the command with 'sh'.
407 */
Jeff Kingc972bf42020-07-28 16:25:12 -0400408 strvec_push(out, SHELL_PATH);
Brandon Williamse3a43442017-04-19 16:13:20 -0700409
Brandon Williams3967e252017-04-19 16:13:19 -0700410 if (cmd->git_cmd) {
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100411 prepare_git_cmd(out, cmd->args.v);
Brandon Williams3967e252017-04-19 16:13:19 -0700412 } else if (cmd->use_shell) {
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100413 prepare_shell_cmd(out, cmd->args.v);
Brandon Williams3967e252017-04-19 16:13:19 -0700414 } else {
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100415 strvec_pushv(out, cmd->args.v);
Brandon Williams3967e252017-04-19 16:13:19 -0700416 }
Brandon Williamse3a43442017-04-19 16:13:20 -0700417
418 /*
Andras Kucsma05ac8582020-03-27 00:36:43 +0000419 * 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.
Brandon Williamse3a43442017-04-19 16:13:20 -0700423 */
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400424 if (!has_dir_sep(out->v[1])) {
425 char *program = locate_in_PATH(out->v[1]);
Brandon Williamse3a43442017-04-19 16:13:20 -0700426 if (program) {
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400427 free((char *)out->v[1]);
428 out->v[1] = program;
Jeff King321fd822018-10-24 03:38:00 -0400429 } else {
Jeff Kingc972bf42020-07-28 16:25:12 -0400430 strvec_clear(out);
Jeff King321fd822018-10-24 03:38:00 -0400431 errno = ENOENT;
432 return -1;
Brandon Williamse3a43442017-04-19 16:13:20 -0700433 }
434 }
Jeff King321fd822018-10-24 03:38:00 -0400435
436 return 0;
Brandon Williams3967e252017-04-19 16:13:19 -0700437}
Brandon Williamsae253942017-04-19 16:13:22 -0700438
439static 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 */
René Scharfe0e187d72017-10-01 17:14:31 +0200478 ALLOC_ARRAY(childenv, env.nr + 1);
Brandon Williamsae253942017-04-19 16:13:22 -0700479 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}
Eric Wong45afb1c2017-04-19 16:13:27 -0700487
488struct atfork_state {
489#ifndef NO_PTHREADS
490 int cs;
Johannes Sixt200a76b2010-03-06 16:40:42 +0100491#endif
Eric Wong45afb1c2017-04-19 16:13:27 -0700492 sigset_t old;
493};
494
Johannes Schindelindde74d72018-05-02 11:38:31 +0200495#define CHECK_BUG(err, msg) \
496 do { \
497 int e = (err); \
498 if (e) \
499 BUG("%s: %s", msg, strerror(e)); \
500 } while(0)
Eric Wong45afb1c2017-04-19 16:13:27 -0700501
502static 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
Johannes Schindelindde74d72018-05-02 11:38:31 +0200512 CHECK_BUG(pthread_sigmask(SIG_SETMASK, &all, &as->old),
Eric Wong45afb1c2017-04-19 16:13:27 -0700513 "blocking all signals");
Johannes Schindelindde74d72018-05-02 11:38:31 +0200514 CHECK_BUG(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &as->cs),
Eric Wong45afb1c2017-04-19 16:13:27 -0700515 "disabling cancellation");
516#endif
517}
518
519static 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
Johannes Schindelindde74d72018-05-02 11:38:31 +0200525 CHECK_BUG(pthread_setcancelstate(as->cs, NULL),
Eric Wong45afb1c2017-04-19 16:13:27 -0700526 "re-enabling cancellation");
Johannes Schindelindde74d72018-05-02 11:38:31 +0200527 CHECK_BUG(pthread_sigmask(SIG_SETMASK, &as->old, NULL),
Eric Wong45afb1c2017-04-19 16:13:27 -0700528 "restoring signal mask");
529#endif
530}
531#endif /* GIT_WINDOWS_NATIVE */
Johannes Sixta5487dd2010-01-10 14:07:52 +0100532
533static 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}
Johannes Sixta5487dd2010-01-10 14:07:52 +0100539
Takashi Iwai507d7802015-09-04 11:35:57 +0200540static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100541{
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;
Jeff King96bfb2d2021-11-22 16:28:13 -0500551 if (!in_signal)
552 error_errno("waitpid for %s failed", argv0);
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100553 } else if (waiting != pid) {
Jeff King96bfb2d2021-11-22 16:28:13 -0500554 if (!in_signal)
555 error("waitpid is confused (%s)", argv0);
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100556 } else if (WIFSIGNALED(status)) {
557 code = WTERMSIG(status);
Jeff King96bfb2d2021-11-22 16:28:13 -0500558 if (!in_signal && code != SIGINT && code != SIGQUIT && code != SIGPIPE)
Jeff Kinga2767c52012-11-30 17:41:38 -0500559 error("%s died of signal %d", argv0, code);
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100560 /*
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 */
Jeff King709ca732013-01-05 09:49:49 -0500565 code += 128;
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100566 } else if (WIFEXITED(status)) {
567 code = WEXITSTATUS(status);
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100568 } else {
Jeff King96bfb2d2021-11-22 16:28:13 -0500569 if (!in_signal)
570 error("waitpid is confused (%s)", argv0);
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100571 }
Jeff Kingafe19ff2012-01-07 12:42:43 +0100572
Jeff King96bfb2d2021-11-22 16:28:13 -0500573 if (!in_signal)
574 clear_child_for_cleanup(pid);
Jeff Kingafe19ff2012-01-07 12:42:43 +0100575
Johannes Sixtab0b41d2010-01-10 14:08:45 +0100576 errno = failed_errno;
577 return code;
578}
579
Nguyễn Thái Ngọc Duyc61a9752018-01-18 16:45:11 +0700580static 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
Nguyễn Thái Ngọc Duye73dd782018-01-18 16:45:09 +0700637static 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
René Scharfe248f66e2018-03-25 12:57:50 +0200644 strbuf_addstr(&buf, "trace: run_command:");
Nguyễn Thái Ngọc Duy090a0922018-01-18 16:45:12 +0700645 if (cp->dir) {
646 strbuf_addstr(&buf, " cd ");
647 sq_quote_buf_pretty(&buf, cp->dir);
648 strbuf_addch(&buf, ';');
649 }
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +0200650 trace_add_env(&buf, cp->env.v);
Nguyễn Thái Ngọc Duy21dfc5e2018-01-18 16:45:10 +0700651 if (cp->git_cmd)
652 strbuf_addstr(&buf, " git");
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100653 sq_quote_argv_pretty(&buf, cp->args.v);
Nguyễn Thái Ngọc Duye73dd782018-01-18 16:45:09 +0700654
655 trace_printf("%s", buf.buf);
656 strbuf_release(&buf);
657}
658
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500659int start_command(struct child_process *cmd)
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200660{
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200661 int need_in, need_out, need_err;
662 int fdin[2], fdout[2], fderr[2];
Jeff King25043d82013-03-21 11:45:00 -0400663 int failed_errno;
Stephen Boyd939296c2013-01-30 18:01:05 -0800664 char *str;
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500665
Johannes Sixtc20181e2008-02-21 23:42:56 +0100666 /*
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
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -0400671 need_in = !cmd->no_stdin && cmd->in < 0;
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500672 if (need_in) {
Johannes Sixtc20181e2008-02-21 23:42:56 +0100673 if (pipe(fdin) < 0) {
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200674 failed_errno = errno;
Johannes Sixtc20181e2008-02-21 23:42:56 +0100675 if (cmd->out > 0)
676 close(cmd->out);
Stephen Boyd939296c2013-01-30 18:01:05 -0800677 str = "standard input";
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200678 goto fail_pipe;
Johannes Sixtc20181e2008-02-21 23:42:56 +0100679 }
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500680 cmd->in = fdin[1];
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500681 }
682
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -0400683 need_out = !cmd->no_stdout
684 && !cmd->stdout_to_stderr
685 && cmd->out < 0;
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400686 if (need_out) {
687 if (pipe(fdout) < 0) {
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200688 failed_errno = errno;
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400689 if (need_in)
690 close_pair(fdin);
Johannes Sixtc20181e2008-02-21 23:42:56 +0100691 else if (cmd->in)
692 close(cmd->in);
Stephen Boyd939296c2013-01-30 18:01:05 -0800693 str = "standard output";
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200694 goto fail_pipe;
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400695 }
696 cmd->out = fdout[0];
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400697 }
698
Shawn O. Pearceb73a4392007-11-11 02:29:37 -0500699 need_err = !cmd->no_stderr && cmd->err < 0;
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200700 if (need_err) {
701 if (pipe(fderr) < 0) {
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200702 failed_errno = errno;
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200703 if (need_in)
704 close_pair(fdin);
Johannes Sixtc20181e2008-02-21 23:42:56 +0100705 else if (cmd->in)
706 close(cmd->in);
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200707 if (need_out)
708 close_pair(fdout);
Johannes Sixtc20181e2008-02-21 23:42:56 +0100709 else if (cmd->out)
710 close(cmd->out);
Stephen Boyd939296c2013-01-30 18:01:05 -0800711 str = "standard error";
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200712fail_pipe:
Stephen Boyd939296c2013-01-30 18:01:05 -0800713 error("cannot create %s pipe for %s: %s",
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100714 str, cmd->args.v[0], strerror(failed_errno));
René Scharfe2d716082015-10-24 14:11:27 +0200715 child_process_clear(cmd);
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200716 errno = failed_errno;
717 return -1;
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200718 }
719 cmd->err = fderr[0];
720 }
721
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800722 trace2_child_start(cmd);
Nguyễn Thái Ngọc Duye73dd782018-01-18 16:45:09 +0700723 trace_run_command(cmd);
724
Johannes Sixt13af8cb2011-02-04 09:41:58 +0100725 fflush(NULL);
Johannes Schindelin8852f5d2008-07-07 14:41:34 +0100726
Johannes Schindelin28d04e12021-09-09 09:47:06 +0000727 if (cmd->close_object_store)
728 close_object_store(the_repository->objects);
729
Jonathan Nieder380395d2013-05-02 20:26:08 +0100730#ifndef GIT_WINDOWS_NATIVE
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100731{
732 int notify_pipe[2];
Brandon Williamsdb015a22017-04-19 16:13:23 -0700733 int null_fd = -1;
Brandon Williamsae253942017-04-19 16:13:22 -0700734 char **childenv;
Jeff Kingc972bf42020-07-28 16:25:12 -0400735 struct strvec argv = STRVEC_INIT;
Brandon Williams79319b12017-04-19 16:13:24 -0700736 struct child_err cerr;
Eric Wong45afb1c2017-04-19 16:13:27 -0700737 struct atfork_state as;
Brandon Williams3967e252017-04-19 16:13:19 -0700738
Jeff King321fd822018-10-24 03:38:00 -0400739 if (prepare_cmd(&argv, cmd) < 0) {
740 failed_errno = errno;
741 cmd->pid = -1;
Junio C Hamanoe5a329a2018-12-11 14:46:07 +0900742 if (!cmd->silent_exec_failure)
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100743 error_errno("cannot run %s", cmd->args.v[0]);
Jeff King321fd822018-10-24 03:38:00 -0400744 goto end_of_spawn;
745 }
746
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100747 if (pipe(notify_pipe))
748 notify_pipe[0] = notify_pipe[1] = -1;
749
Brandon Williamsdb015a22017-04-19 16:13:23 -0700750 if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) {
René Scharfe66e905b2021-08-25 22:16:46 +0200751 null_fd = xopen("/dev/null", O_RDWR | O_CLOEXEC);
Brandon Williamsdb015a22017-04-19 16:13:23 -0700752 set_cloexec(null_fd);
753 }
754
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +0200755 childenv = prep_childenv(cmd->env.v);
Eric Wong45afb1c2017-04-19 16:13:27 -0700756 atfork_prepare(&as);
Brandon Williams3967e252017-04-19 16:13:19 -0700757
Brandon Williamse503cd62017-04-19 16:13:26 -0700758 /*
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 */
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500767 cmd->pid = fork();
Jeff King25043d82013-03-21 11:45:00 -0400768 failed_errno = errno;
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500769 if (!cmd->pid) {
Eric Wong45afb1c2017-04-19 16:13:27 -0700770 int sig;
Johannes Sixta5487dd2010-01-10 14:07:52 +0100771 /*
Brandon Williams79319b12017-04-19 16:13:24 -0700772 * Ensure the default die/error/warn routines do not get
773 * called, they can take stdio locks and malloc.
Johannes Sixta5487dd2010-01-10 14:07:52 +0100774 */
Brandon Williams79319b12017-04-19 16:13:24 -0700775 set_die_routine(child_die_fn);
776 set_error_routine(child_error_fn);
777 set_warn_routine(child_warn_fn);
Johannes Sixta5487dd2010-01-10 14:07:52 +0100778
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100779 close(notify_pipe[0]);
780 set_cloexec(notify_pipe[1]);
781 child_notifier = notify_pipe[1];
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100782
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -0400783 if (cmd->no_stdin)
Brandon Williams53fa6752017-04-19 16:13:25 -0700784 child_dup2(null_fd, 0);
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -0400785 else if (need_in) {
Brandon Williams53fa6752017-04-19 16:13:25 -0700786 child_dup2(fdin[0], 0);
787 child_close_pair(fdin);
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500788 } else if (cmd->in) {
Brandon Williams53fa6752017-04-19 16:13:25 -0700789 child_dup2(cmd->in, 0);
790 child_close(cmd->in);
Shawn O. Pearce95d3c4f2006-12-30 21:55:22 -0500791 }
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500792
Christian Couderce2cf272008-03-05 08:35:16 +0100793 if (cmd->no_stderr)
Brandon Williams53fa6752017-04-19 16:13:25 -0700794 child_dup2(null_fd, 2);
Christian Couderce2cf272008-03-05 08:35:16 +0100795 else if (need_err) {
Brandon Williams53fa6752017-04-19 16:13:25 -0700796 child_dup2(fderr[1], 2);
797 child_close_pair(fderr);
Shawn O. Pearce4f41b612010-02-05 12:57:37 -0800798 } else if (cmd->err > 1) {
Brandon Williams53fa6752017-04-19 16:13:25 -0700799 child_dup2(cmd->err, 2);
800 child_close(cmd->err);
Christian Couderce2cf272008-03-05 08:35:16 +0100801 }
802
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -0400803 if (cmd->no_stdout)
Brandon Williams53fa6752017-04-19 16:13:25 -0700804 child_dup2(null_fd, 1);
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -0400805 else if (cmd->stdout_to_stderr)
Brandon Williams53fa6752017-04-19 16:13:25 -0700806 child_dup2(2, 1);
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400807 else if (need_out) {
Brandon Williams53fa6752017-04-19 16:13:25 -0700808 child_dup2(fdout[1], 1);
809 child_close_pair(fdout);
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400810 } else if (cmd->out > 1) {
Brandon Williams53fa6752017-04-19 16:13:25 -0700811 child_dup2(cmd->out, 1);
812 child_close(cmd->out);
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400813 }
814
Alex Riesen1568fea2007-05-22 23:48:23 +0200815 if (cmd->dir && chdir(cmd->dir))
Brandon Williams79319b12017-04-19 16:13:24 -0700816 child_die(CHILD_ERR_CHDIR);
Brandon Williams3967e252017-04-19 16:13:19 -0700817
Brandon Williamse3a43442017-04-19 16:13:20 -0700818 /*
Eric Wong45afb1c2017-04-19 16:13:27 -0700819 * 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 /*
Brandon Williamse3a43442017-04-19 16:13:20 -0700832 * 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 */
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400837 execve(argv.v[1], (char *const *) argv.v + 1,
Brandon Williamsae253942017-04-19 16:13:22 -0700838 (char *const *) childenv);
Brandon Williamse3a43442017-04-19 16:13:20 -0700839 if (errno == ENOEXEC)
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400840 execve(argv.v[0], (char *const *) argv.v,
Brandon Williamsae253942017-04-19 16:13:22 -0700841 (char *const *) childenv);
Brandon Williams3967e252017-04-19 16:13:19 -0700842
Clemens Buchacherfc1b56f2011-08-01 19:59:21 +0200843 if (errno == ENOENT) {
Brandon Williams79319b12017-04-19 16:13:24 -0700844 if (cmd->silent_exec_failure)
845 child_die(CHILD_ERR_SILENT);
846 child_die(CHILD_ERR_ENOENT);
Clemens Buchacherfc1b56f2011-08-01 19:59:21 +0200847 } else {
Brandon Williams79319b12017-04-19 16:13:24 -0700848 child_die(CHILD_ERR_ERRNO);
Clemens Buchacherfc1b56f2011-08-01 19:59:21 +0200849 }
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200850 }
Eric Wong45afb1c2017-04-19 16:13:27 -0700851 atfork_parent(&as);
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200852 if (cmd->pid < 0)
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100853 error_errno("cannot fork() for %s", cmd->args.v[0]);
Jeff Kingafe19ff2012-01-07 12:42:43 +0100854 else if (cmd->clean_on_exit)
Lars Schneiderac2fbaa2016-10-16 16:20:28 -0700855 mark_child_for_cleanup(cmd->pid, cmd);
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100856
857 /*
Brandon Williams3967e252017-04-19 16:13:19 -0700858 * Wait for child's exec. If the exec succeeds (or if fork()
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100859 * failed), EOF is seen immediately by the parent. Otherwise, the
Brandon Williams79319b12017-04-19 16:13:24 -0700860 * child process sends a child_err struct.
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100861 * Note that use of this infrastructure is completely advisory,
862 * therefore, we keep error checks minimal.
863 */
864 close(notify_pipe[1]);
Brandon Williams79319b12017-04-19 16:13:24 -0700865 if (xread(notify_pipe[0], &cerr, sizeof(cerr)) == sizeof(cerr)) {
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100866 /*
Brandon Williams3967e252017-04-19 16:13:19 -0700867 * At this point we know that fork() succeeded, but exec()
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100868 * failed. Errors have been reported to our stderr.
869 */
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100870 wait_or_whine(cmd->pid, cmd->args.v[0], 0);
Brandon Williams79319b12017-04-19 16:13:24 -0700871 child_err_spew(cmd, &cerr);
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100872 failed_errno = errno;
873 cmd->pid = -1;
874 }
875 close(notify_pipe[0]);
Brandon Williams3967e252017-04-19 16:13:19 -0700876
Brandon Williamsdb015a22017-04-19 16:13:23 -0700877 if (null_fd >= 0)
878 close(null_fd);
Jeff Kingc972bf42020-07-28 16:25:12 -0400879 strvec_clear(&argv);
Brandon Williamsae253942017-04-19 16:13:22 -0700880 free(childenv);
Johannes Sixt2b541bf2010-01-10 14:11:22 +0100881}
Jeff King321fd822018-10-24 03:38:00 -0400882end_of_spawn:
883
Johannes Sixtba26f292007-12-07 22:08:59 +0100884#else
Frank Li0d30ad72009-09-16 10:20:17 +0200885{
Johannes Sixt75301f92010-01-15 21:12:18 +0100886 int fhin = 0, fhout = 1, fherr = 2;
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100887 const char **sargv = cmd->args.v;
Jeff Kingc972bf42020-07-28 16:25:12 -0400888 struct strvec nargv = STRVEC_INIT;
Johannes Sixtba26f292007-12-07 22:08:59 +0100889
Johannes Sixt75301f92010-01-15 21:12:18 +0100890 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);
Johannes Sixtba26f292007-12-07 22:08:59 +0100896
Johannes Sixt75301f92010-01-15 21:12:18 +0100897 if (cmd->no_stderr)
898 fherr = open("/dev/null", O_RDWR);
899 else if (need_err)
900 fherr = dup(fderr[1]);
Junio C Hamano76d44c82010-02-05 21:08:53 -0800901 else if (cmd->err > 2)
902 fherr = dup(cmd->err);
Johannes Sixtba26f292007-12-07 22:08:59 +0100903
Johannes Sixt75301f92010-01-15 21:12:18 +0100904 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);
Johannes Sixtba26f292007-12-07 22:08:59 +0100912
Felipe Contreras5a500852013-10-31 03:25:45 -0600913 if (cmd->git_cmd)
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100914 cmd->args.v = prepare_git_cmd(&nargv, sargv);
Felipe Contreras5a500852013-10-31 03:25:45 -0600915 else if (cmd->use_shell)
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100916 cmd->args.v = prepare_shell_cmd(&nargv, sargv);
Johannes Sixtba26f292007-12-07 22:08:59 +0100917
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +0200918 cmd->pid = mingw_spawnvpe(cmd->args.v[0], cmd->args.v,
919 (char**) cmd->env.v,
920 cmd->dir, fhin, fhout, fherr);
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200921 failed_errno = errno;
Johannes Sixtc024beb2009-07-04 21:26:42 +0200922 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100923 error_errno("cannot spawn %s", cmd->args.v[0]);
Jeff Kingafe19ff2012-01-07 12:42:43 +0100924 if (cmd->clean_on_exit && cmd->pid >= 0)
Lars Schneiderac2fbaa2016-10-16 16:20:28 -0700925 mark_child_for_cleanup(cmd->pid, cmd);
Johannes Sixtba26f292007-12-07 22:08:59 +0100926
Jeff Kingc972bf42020-07-28 16:25:12 -0400927 strvec_clear(&nargv);
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100928 cmd->args.v = sargv;
Johannes Sixt75301f92010-01-15 21:12:18 +0100929 if (fhin != 0)
930 close(fhin);
931 if (fhout != 1)
932 close(fhout);
933 if (fherr != 2)
934 close(fherr);
Frank Li0d30ad72009-09-16 10:20:17 +0200935}
Johannes Sixtba26f292007-12-07 22:08:59 +0100936#endif
937
938 if (cmd->pid < 0) {
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800939 trace2_child_exit(cmd, -1);
940
Johannes Sixtba26f292007-12-07 22:08:59 +0100941 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);
bert Dvornikfc012c22010-05-20 20:57:52 +0200951 else if (cmd->err)
952 close(cmd->err);
René Scharfe2d716082015-10-24 14:11:27 +0200953 child_process_clear(cmd);
Johannes Sixt0ac77ec2009-07-04 21:26:40 +0200954 errno = failed_errno;
955 return -1;
Johannes Sixtba26f292007-12-07 22:08:59 +0100956 }
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500957
958 if (need_in)
959 close(fdin[0]);
960 else if (cmd->in)
961 close(cmd->in);
962
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400963 if (need_out)
964 close(fdout[1]);
Johannes Sixtc20181e2008-02-21 23:42:56 +0100965 else if (cmd->out)
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400966 close(cmd->out);
967
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200968 if (need_err)
969 close(fderr[1]);
Shawn O. Pearce4f41b612010-02-05 12:57:37 -0800970 else if (cmd->err)
971 close(cmd->err);
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200972
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500973 return 0;
974}
975
Johannes Sixt2d22c202007-10-19 21:48:00 +0200976int finish_command(struct child_process *cmd)
977{
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100978 int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 0);
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800979 trace2_child_exit(cmd, ret);
René Scharfe2d716082015-10-24 14:11:27 +0200980 child_process_clear(cmd);
Johannes Schindelin0d58fef2021-02-02 22:09:52 +0100981 invalidate_lstat_cache();
Jeff Kingc460c0e2014-05-15 04:33:26 -0400982 return ret;
Johannes Sixt2d22c202007-10-19 21:48:00 +0200983}
984
Takashi Iwai507d7802015-09-04 11:35:57 +0200985int finish_command_in_signal(struct child_process *cmd)
986{
Ævar Arnfjörð Bjarmasond3b21592021-11-25 23:52:22 +0100987 int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 1);
Josh Steadmonce3986b2022-06-07 11:21:57 -0700988 if (ret != -1)
989 trace2_child_exit(cmd, ret);
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800990 return ret;
Takashi Iwai507d7802015-09-04 11:35:57 +0200991}
992
993
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500994int run_command(struct child_process *cmd)
995{
Jeff Kingc29b3962015-03-22 23:54:05 -0400996 int code;
997
998 if (cmd->out < 0 || cmd->err < 0)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200999 BUG("run_command with a pipe can cause deadlock");
Jeff Kingc29b3962015-03-22 23:54:05 -04001000
1001 code = start_command(cmd);
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -05001002 if (code)
1003 return code;
1004 return finish_command(cmd);
1005}
1006
Johannes Sixtf6b60982010-03-09 21:00:36 +01001007#ifndef NO_PTHREADS
Johannes Sixt0ea1c892010-03-06 16:40:43 +01001008static pthread_t main_thread;
1009static int main_thread_set;
1010static pthread_key_t async_key;
Jeff King1ece66b2013-04-16 15:50:07 -04001011static pthread_key_t async_die_counter;
Johannes Sixt0ea1c892010-03-06 16:40:43 +01001012
Johannes Sixt200a76b2010-03-06 16:40:42 +01001013static void *run_thread(void *data)
Johannes Sixt618ebe92007-12-08 22:19:14 +01001014{
1015 struct async *async = data;
Johannes Sixtf6b60982010-03-09 21:00:36 +01001016 intptr_t ret;
Johannes Sixt0ea1c892010-03-06 16:40:43 +01001017
Jeff Kingc792d7b2016-04-19 18:49:41 -04001018 if (async->isolate_sigpipe) {
1019 sigset_t mask;
1020 sigemptyset(&mask);
1021 sigaddset(&mask, SIGPIPE);
Seija786e6762022-12-02 17:02:58 +00001022 if (pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
Jeff Kingc792d7b2016-04-19 18:49:41 -04001023 ret = error("unable to block SIGPIPE in async thread");
1024 return (void *)ret;
1025 }
1026 }
1027
Johannes Sixt0ea1c892010-03-06 16:40:43 +01001028 pthread_setspecific(async_key, async);
Johannes Sixtf6b60982010-03-09 21:00:36 +01001029 ret = async->proc(async->proc_in, async->proc_out, async->data);
Johannes Sixt200a76b2010-03-06 16:40:42 +01001030 return (void *)ret;
Johannes Sixt618ebe92007-12-08 22:19:14 +01001031}
Johannes Sixt0ea1c892010-03-06 16:40:43 +01001032
1033static NORETURN void die_async(const char *err, va_list params)
1034{
Ævar Arnfjörð Bjarmasone081a7c2021-12-07 19:26:30 +01001035 report_fn die_message_fn = get_die_message_routine();
1036
1037 die_message_fn(err, params);
Johannes Sixt0ea1c892010-03-06 16:40:43 +01001038
Jeff King661a8cf2015-09-01 16:22:43 -04001039 if (in_async()) {
Johannes Sixt0ea1c892010-03-06 16:40:43 +01001040 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);
Johannes Sixt618ebe92007-12-08 22:19:14 +01001049}
Jeff King1ece66b2013-04-16 15:50:07 -04001050
1051static int async_die_is_recursing(void)
1052{
1053 void *ret = pthread_getspecific(async_die_counter);
Victoria Dye4b540cf2021-11-04 04:01:03 +00001054 pthread_setspecific(async_die_counter, &async_die_counter); /* set to any non-NULL valid pointer */
Jeff King1ece66b2013-04-16 15:50:07 -04001055 return ret != NULL;
1056}
1057
Jeff King661a8cf2015-09-01 16:22:43 -04001058int 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
Lars Schneiderb992fe12016-10-16 16:20:27 -07001065static void NORETURN async_exit(int code)
Jeff King96588462016-02-24 02:40:16 -05001066{
1067 pthread_exit((void *)(intptr_t)code);
1068}
1069
Etienne Buira0f4b6db2014-10-18 14:31:15 +02001070#else
1071
1072static struct {
1073 void (**handlers)(void);
1074 size_t nr;
1075 size_t alloc;
1076} git_atexit_hdlrs;
1077
1078static int git_atexit_installed;
1079
René Scharfe6066a7e2014-11-10 22:17:00 +01001080static void git_atexit_dispatch(void)
Etienne Buira0f4b6db2014-10-18 14:31:15 +02001081{
1082 size_t i;
1083
1084 for (i=git_atexit_hdlrs.nr ; i ; i--)
1085 git_atexit_hdlrs.handlers[i-1]();
1086}
1087
René Scharfe6066a7e2014-11-10 22:17:00 +01001088static void git_atexit_clear(void)
Etienne Buira0f4b6db2014-10-18 14:31:15 +02001089{
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
1096int 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
Jeff King661a8cf2015-09-01 16:22:43 -04001109static int process_is_async;
1110int in_async(void)
1111{
1112 return process_is_async;
1113}
1114
Lars Schneiderb992fe12016-10-16 16:20:27 -07001115static void NORETURN async_exit(int code)
Jeff King96588462016-02-24 02:40:16 -05001116{
1117 exit(code);
1118}
1119
Johannes Sixt618ebe92007-12-08 22:19:14 +01001120#endif
1121
Lars Schneiderb992fe12016-10-16 16:20:27 -07001122void 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
Johannes Sixt2d22c202007-10-19 21:48:00 +02001135int start_async(struct async *async)
1136{
Erik Faye-Lundae6a5602010-02-05 12:57:38 -08001137 int need_in, need_out;
1138 int fdin[2], fdout[2];
1139 int proc_in, proc_out;
Johannes Sixt2d22c202007-10-19 21:48:00 +02001140
Erik Faye-Lundae6a5602010-02-05 12:57:38 -08001141 need_in = async->in < 0;
1142 if (need_in) {
1143 if (pipe(fdin) < 0) {
1144 if (async->out > 0)
1145 close(async->out);
Nguyễn Thái Ngọc Duyfbcb0e02016-05-08 16:47:53 +07001146 return error_errno("cannot create pipe");
Erik Faye-Lundae6a5602010-02-05 12:57:38 -08001147 }
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);
Nguyễn Thái Ngọc Duyfbcb0e02016-05-08 16:47:53 +07001158 return error_errno("cannot create pipe");
Erik Faye-Lundae6a5602010-02-05 12:57:38 -08001159 }
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;
Johannes Sixt2d22c202007-10-19 21:48:00 +02001176
Johannes Sixtf6b60982010-03-09 21:00:36 +01001177#ifdef NO_PTHREADS
Anders Melchiorsen2c3766f2008-08-04 02:30:03 +02001178 /* Flush stdio before fork() to avoid cloning buffers */
1179 fflush(NULL);
1180
Johannes Sixt2d22c202007-10-19 21:48:00 +02001181 async->pid = fork();
1182 if (async->pid < 0) {
Nguyễn Thái Ngọc Duyfbcb0e02016-05-08 16:47:53 +07001183 error_errno("fork (async) failed");
Erik Faye-Lundae6a5602010-02-05 12:57:38 -08001184 goto error;
Johannes Sixt2d22c202007-10-19 21:48:00 +02001185 }
1186 if (!async->pid) {
Erik Faye-Lundae6a5602010-02-05 12:57:38 -08001187 if (need_in)
1188 close(fdin[1]);
1189 if (need_out)
1190 close(fdout[0]);
Etienne Buira0f4b6db2014-10-18 14:31:15 +02001191 git_atexit_clear();
Jeff King661a8cf2015-09-01 16:22:43 -04001192 process_is_async = 1;
Erik Faye-Lundae6a5602010-02-05 12:57:38 -08001193 exit(!!async->proc(proc_in, proc_out, async->data));
Johannes Sixt2d22c202007-10-19 21:48:00 +02001194 }
Erik Faye-Lundae6a5602010-02-05 12:57:38 -08001195
Lars Schneiderac2fbaa2016-10-16 16:20:28 -07001196 mark_child_for_cleanup(async->pid, NULL);
Jeff Kingafe19ff2012-01-07 12:42:43 +01001197
Erik Faye-Lundae6a5602010-02-05 12:57:38 -08001198 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);
Johannes Sixt618ebe92007-12-08 22:19:14 +01001207#else
Johannes Sixt0ea1c892010-03-06 16:40:43 +01001208 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);
Jeff King1ece66b2013-04-16 15:50:07 -04001216 pthread_key_create(&async_die_counter, NULL);
Johannes Sixt0ea1c892010-03-06 16:40:43 +01001217 set_die_routine(die_async);
Jeff King1ece66b2013-04-16 15:50:07 -04001218 set_die_is_recursing_routine(async_die_is_recursing);
Johannes Sixt0ea1c892010-03-06 16:40:43 +01001219 }
1220
Johannes Sixt200a76b2010-03-06 16:40:42 +01001221 if (proc_in >= 0)
1222 set_cloexec(proc_in);
1223 if (proc_out >= 0)
1224 set_cloexec(proc_out);
Erik Faye-Lundae6a5602010-02-05 12:57:38 -08001225 async->proc_in = proc_in;
1226 async->proc_out = proc_out;
Johannes Sixt200a76b2010-03-06 16:40:42 +01001227 {
1228 int err = pthread_create(&async->tid, NULL, run_thread, async);
1229 if (err) {
Nguyễn Thái Ngọc Duy21790452018-11-03 09:48:50 +01001230 error(_("cannot create async thread: %s"), strerror(err));
Johannes Sixt200a76b2010-03-06 16:40:42 +01001231 goto error;
1232 }
Johannes Sixt618ebe92007-12-08 22:19:14 +01001233 }
1234#endif
Johannes Sixt2d22c202007-10-19 21:48:00 +02001235 return 0;
Erik Faye-Lundae6a5602010-02-05 12:57:38 -08001236
1237error:
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;
Johannes Sixt2d22c202007-10-19 21:48:00 +02001248}
1249
1250int finish_async(struct async *async)
1251{
Johannes Sixtf6b60982010-03-09 21:00:36 +01001252#ifdef NO_PTHREADS
Johannes Schindelin0d58fef2021-02-02 22:09:52 +01001253 int ret = wait_or_whine(async->pid, "child process", 0);
1254
1255 invalidate_lstat_cache();
1256
1257 return ret;
Johannes Sixt618ebe92007-12-08 22:19:14 +01001258#else
Johannes Sixt200a76b2010-03-06 16:40:42 +01001259 void *ret = (void *)(intptr_t)(-1);
1260
1261 if (pthread_join(async->tid, &ret))
1262 error("pthread_join failed");
Johannes Schindelin0d58fef2021-02-02 22:09:52 +01001263 invalidate_lstat_cache();
Johannes Sixt200a76b2010-03-06 16:40:42 +01001264 return (int)(intptr_t)ret;
Johannes Schindelin0d58fef2021-02-02 22:09:52 +01001265
Johannes Sixt618ebe92007-12-08 22:19:14 +01001266#endif
Johannes Sixt2d22c202007-10-19 21:48:00 +02001267}
Stephan Beyerae98a002009-01-16 20:09:59 +01001268
Nguyễn Thái Ngọc Duyc0e40a22018-11-03 09:48:39 +01001269int async_with_fork(void)
1270{
1271#ifdef NO_PTHREADS
1272 return 1;
1273#else
1274 return 0;
1275#endif
1276}
1277
Jeff King96335bc2016-06-17 19:38:47 -04001278struct 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
1300static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
Jeff King911ec992015-03-22 23:53:43 -04001301{
Jeff King96335bc2016-06-17 19:38:47 -04001302 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) {
Jeff King14eab812022-08-17 02:08:06 -04001333 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);
Jeff King96335bc2016-06-17 19:38:47 -04001345 if (len < 0) {
Jeff Kingc6d3cce2022-08-17 02:09:42 -04001346 if (errno != EINTR && errno != EAGAIN &&
1347 errno != ENOSPC) {
Jeff King14eab812022-08-17 02:08:06 -04001348 io->error = errno;
1349 close(io->fd);
1350 io->fd = -1;
1351 }
Jeff King96335bc2016-06-17 19:38:47 -04001352 } 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
1377static 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
1401int 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
Jeff King911ec992015-03-22 23:53:43 -04001416 if (start_command(cmd) < 0)
1417 return -1;
1418
Jeff King96335bc2016-06-17 19:38:47 -04001419 if (in) {
Jeff King716c1f62022-08-17 02:10:22 -04001420 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 }
Jeff King96335bc2016-06-17 19:38:47 -04001429 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) {
Jeff King911ec992015-03-22 23:53:43 -04001451 finish_command(cmd); /* throw away exit code */
1452 return -1;
1453 }
1454
Jeff King911ec992015-03-22 23:53:43 -04001455 return finish_command(cmd);
1456}
Stefan Bellerc553c722015-12-15 16:04:10 -08001457
1458enum child_state {
1459 GIT_CP_FREE,
1460 GIT_CP_WORKING,
1461 GIT_CP_WAIT_CLEANUP,
1462};
1463
1464struct parallel_processes {
Ævar Arnfjörð Bjarmason6a48b422022-10-12 23:02:23 +02001465 size_t nr_processes;
Stefan Bellerc553c722015-12-15 16:04:10 -08001466
Stefan Bellerc553c722015-12-15 16:04:10 -08001467 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
Ævar Arnfjörð Bjarmason6a48b422022-10-12 23:02:23 +02001481 size_t output_owner;
Stefan Bellerc553c722015-12-15 16:04:10 -08001482 struct strbuf buffered_output; /* of finished children */
1483};
1484
Ævar Arnfjörð Bjarmason0b0ab952022-10-12 23:02:34 +02001485struct parallel_processes_for_signal {
1486 const struct run_process_parallel_opts *opts;
1487 const struct parallel_processes *pp;
1488};
1489
1490static void kill_children(const struct parallel_processes *pp,
1491 const struct run_process_parallel_opts *opts,
1492 int signo)
Stefan Bellerc553c722015-12-15 16:04:10 -08001493{
Ævar Arnfjörð Bjarmason0b0ab952022-10-12 23:02:34 +02001494 for (size_t i = 0; i < opts->processes; i++)
Stefan Bellerc553c722015-12-15 16:04:10 -08001495 if (pp->children[i].state == GIT_CP_WORKING)
1496 kill(pp->children[i].process.pid, signo);
1497}
1498
Ævar Arnfjörð Bjarmason0b0ab952022-10-12 23:02:34 +02001499static 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
1505static struct parallel_processes_for_signal *pp_for_signal;
Stefan Bellerc553c722015-12-15 16:04:10 -08001506
1507static void handle_children_on_signal(int signo)
1508{
Ævar Arnfjörð Bjarmason0b0ab952022-10-12 23:02:34 +02001509 kill_children_signal(pp_for_signal, signo);
Stefan Bellerc553c722015-12-15 16:04:10 -08001510 sigchain_pop(signo);
1511 raise(signo);
1512}
1513
1514static void pp_init(struct parallel_processes *pp,
Ævar Arnfjörð Bjarmason0b0ab952022-10-12 23:02:34 +02001515 const struct run_process_parallel_opts *opts,
1516 struct parallel_processes_for_signal *pp_sig)
Stefan Bellerc553c722015-12-15 16:04:10 -08001517{
Ævar Arnfjörð Bjarmason6e5ba0b2022-10-12 23:02:26 +02001518 const size_t n = opts->processes;
Ævar Arnfjörð Bjarmasonc333e6f2022-10-12 23:02:25 +02001519
Ævar Arnfjörð Bjarmason51243f92022-10-12 23:02:24 +02001520 if (!n)
1521 BUG("you must provide a non-zero number of processes!");
Stefan Bellerc553c722015-12-15 16:04:10 -08001522
Ævar Arnfjörð Bjarmason6a48b422022-10-12 23:02:23 +02001523 trace_printf("run_processes_parallel: preparing to run up to %"PRIuMAX" tasks",
1524 (uintmax_t)n);
Stefan Bellerc553c722015-12-15 16:04:10 -08001525
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001526 if (!opts->get_next_task)
Johannes Schindelin033abf92018-05-02 11:38:39 +02001527 BUG("you need to specify a get_next_task function");
Stefan Bellerc553c722015-12-15 16:04:10 -08001528
René Scharfeca56dad2021-03-13 17:17:22 +01001529 CALLOC_ARRAY(pp->children, n);
Ævar Arnfjörð Bjarmason357f8e62022-10-12 23:02:30 +02001530 if (!opts->ungroup)
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001531 CALLOC_ARRAY(pp->pfd, n);
Stefan Bellerc553c722015-12-15 16:04:10 -08001532
Ævar Arnfjörð Bjarmason6a48b422022-10-12 23:02:23 +02001533 for (size_t i = 0; i < n; i++) {
Stefan Bellerc553c722015-12-15 16:04:10 -08001534 strbuf_init(&pp->children[i].err, 0);
1535 child_process_init(&pp->children[i].process);
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001536 if (pp->pfd) {
1537 pp->pfd[i].events = POLLIN | POLLHUP;
1538 pp->pfd[i].fd = -1;
1539 }
Stefan Bellerc553c722015-12-15 16:04:10 -08001540 }
1541
Ævar Arnfjörð Bjarmason0b0ab952022-10-12 23:02:34 +02001542 pp_sig->pp = pp;
1543 pp_sig->opts = opts;
1544 pp_for_signal = pp_sig;
Stefan Bellerc553c722015-12-15 16:04:10 -08001545 sigchain_push_common(handle_children_on_signal);
1546}
1547
Ævar Arnfjörð Bjarmasond1610ee2022-10-12 23:02:33 +02001548static void pp_cleanup(struct parallel_processes *pp,
1549 const struct run_process_parallel_opts *opts)
Stefan Bellerc553c722015-12-15 16:04:10 -08001550{
Stefan Bellerc553c722015-12-15 16:04:10 -08001551 trace_printf("run_processes_parallel: done");
Ævar Arnfjörð Bjarmasond1610ee2022-10-12 23:02:33 +02001552 for (size_t i = 0; i < opts->processes; i++) {
Stefan Bellerc553c722015-12-15 16:04:10 -08001553 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 */
Stefan Beller2dac9b52016-02-29 18:07:15 -08001564 strbuf_write(&pp->buffered_output, stderr);
Stefan Bellerc553c722015-12-15 16:04:10 -08001565 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 */
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001577static int pp_start_one(struct parallel_processes *pp,
1578 const struct run_process_parallel_opts *opts)
Stefan Bellerc553c722015-12-15 16:04:10 -08001579{
Ævar Arnfjörð Bjarmason6a48b422022-10-12 23:02:23 +02001580 size_t i;
1581 int code;
Stefan Bellerc553c722015-12-15 16:04:10 -08001582
Ævar Arnfjörð Bjarmason9f3df6c2022-10-12 23:02:32 +02001583 for (i = 0; i < opts->processes; i++)
Stefan Bellerc553c722015-12-15 16:04:10 -08001584 if (pp->children[i].state == GIT_CP_FREE)
1585 break;
Ævar Arnfjörð Bjarmason9f3df6c2022-10-12 23:02:32 +02001586 if (i == opts->processes)
Johannes Schindelin033abf92018-05-02 11:38:39 +02001587 BUG("bookkeeping is hard");
Stefan Bellerc553c722015-12-15 16:04:10 -08001588
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001589 code = opts->get_next_task(&pp->children[i].process,
Ævar Arnfjörð Bjarmason357f8e62022-10-12 23:02:30 +02001590 opts->ungroup ? NULL : &pp->children[i].err,
Ævar Arnfjörð Bjarmason2aa8d222022-10-12 23:02:31 +02001591 opts->data,
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001592 &pp->children[i].data);
Stefan Bellerc553c722015-12-15 16:04:10 -08001593 if (!code) {
Ævar Arnfjörð Bjarmason357f8e62022-10-12 23:02:30 +02001594 if (!opts->ungroup) {
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001595 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1596 strbuf_reset(&pp->children[i].err);
1597 }
Stefan Bellerc553c722015-12-15 16:04:10 -08001598 return 1;
1599 }
Ævar Arnfjörð Bjarmason357f8e62022-10-12 23:02:30 +02001600 if (!opts->ungroup) {
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001601 pp->children[i].process.err = -1;
1602 pp->children[i].process.stdout_to_stderr = 1;
1603 }
Stefan Bellerc553c722015-12-15 16:04:10 -08001604 pp->children[i].process.no_stdin = 1;
1605
1606 if (start_command(&pp->children[i].process)) {
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001607 if (opts->start_failure)
Ævar Arnfjörð Bjarmason357f8e62022-10-12 23:02:30 +02001608 code = opts->start_failure(opts->ungroup ? NULL :
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001609 &pp->children[i].err,
Ævar Arnfjörð Bjarmason2aa8d222022-10-12 23:02:31 +02001610 opts->data,
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001611 pp->children[i].data);
1612 else
1613 code = 0;
1614
Ævar Arnfjörð Bjarmason357f8e62022-10-12 23:02:30 +02001615 if (!opts->ungroup) {
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001616 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1617 strbuf_reset(&pp->children[i].err);
1618 }
Stefan Bellerc553c722015-12-15 16:04:10 -08001619 if (code)
1620 pp->shutdown = 1;
1621 return code;
1622 }
1623
1624 pp->nr_processes++;
1625 pp->children[i].state = GIT_CP_WORKING;
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001626 if (pp->pfd)
1627 pp->pfd[i].fd = pp->children[i].process.err;
Stefan Bellerc553c722015-12-15 16:04:10 -08001628 return 0;
1629}
1630
Ævar Arnfjörð Bjarmasond1610ee2022-10-12 23:02:33 +02001631static void pp_buffer_stderr(struct parallel_processes *pp,
1632 const struct run_process_parallel_opts *opts,
1633 int output_timeout)
Stefan Bellerc553c722015-12-15 16:04:10 -08001634{
1635 int i;
1636
Ævar Arnfjörð Bjarmasond1610ee2022-10-12 23:02:33 +02001637 while ((i = poll(pp->pfd, opts->processes, output_timeout) < 0)) {
Stefan Bellerc553c722015-12-15 16:04:10 -08001638 if (errno == EINTR)
1639 continue;
Ævar Arnfjörð Bjarmasond1610ee2022-10-12 23:02:33 +02001640 pp_cleanup(pp, opts);
Stefan Bellerc553c722015-12-15 16:04:10 -08001641 die_errno("poll");
1642 }
1643
1644 /* Buffer output from all pipes. */
Ævar Arnfjörð Bjarmasond1610ee2022-10-12 23:02:33 +02001645 for (size_t i = 0; i < opts->processes; i++) {
Stefan Bellerc553c722015-12-15 16:04:10 -08001646 if (pp->children[i].state == GIT_CP_WORKING &&
1647 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1648 int n = strbuf_read_once(&pp->children[i].err,
1649 pp->children[i].process.err, 0);
1650 if (n == 0) {
1651 close(pp->children[i].process.err);
1652 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1653 } else if (n < 0)
1654 if (errno != EAGAIN)
1655 die_errno("read");
1656 }
1657 }
1658}
1659
Ævar Arnfjörð Bjarmasone39c9de2022-10-12 23:02:28 +02001660static void pp_output(const struct parallel_processes *pp)
Stefan Bellerc553c722015-12-15 16:04:10 -08001661{
Ævar Arnfjörð Bjarmason6a48b422022-10-12 23:02:23 +02001662 size_t i = pp->output_owner;
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001663
Stefan Bellerc553c722015-12-15 16:04:10 -08001664 if (pp->children[i].state == GIT_CP_WORKING &&
1665 pp->children[i].err.len) {
Stefan Beller2dac9b52016-02-29 18:07:15 -08001666 strbuf_write(&pp->children[i].err, stderr);
Stefan Bellerc553c722015-12-15 16:04:10 -08001667 strbuf_reset(&pp->children[i].err);
1668 }
1669}
1670
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001671static int pp_collect_finished(struct parallel_processes *pp,
1672 const struct run_process_parallel_opts *opts)
Stefan Bellerc553c722015-12-15 16:04:10 -08001673{
Ævar Arnfjörð Bjarmason6a48b422022-10-12 23:02:23 +02001674 int code;
Ævar Arnfjörð Bjarmason9f3df6c2022-10-12 23:02:32 +02001675 size_t i;
Stefan Bellerc553c722015-12-15 16:04:10 -08001676 int result = 0;
1677
1678 while (pp->nr_processes > 0) {
Ævar Arnfjörð Bjarmason9f3df6c2022-10-12 23:02:32 +02001679 for (i = 0; i < opts->processes; i++)
Stefan Bellerc553c722015-12-15 16:04:10 -08001680 if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1681 break;
Ævar Arnfjörð Bjarmason9f3df6c2022-10-12 23:02:32 +02001682 if (i == opts->processes)
Stefan Bellerc553c722015-12-15 16:04:10 -08001683 break;
1684
1685 code = finish_command(&pp->children[i].process);
1686
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001687 if (opts->task_finished)
Ævar Arnfjörð Bjarmason357f8e62022-10-12 23:02:30 +02001688 code = opts->task_finished(code, opts->ungroup ? NULL :
Ævar Arnfjörð Bjarmason2aa8d222022-10-12 23:02:31 +02001689 &pp->children[i].err, opts->data,
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001690 pp->children[i].data);
1691 else
1692 code = 0;
Stefan Bellerc553c722015-12-15 16:04:10 -08001693
1694 if (code)
1695 result = code;
1696 if (code < 0)
1697 break;
1698
1699 pp->nr_processes--;
1700 pp->children[i].state = GIT_CP_FREE;
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001701 if (pp->pfd)
1702 pp->pfd[i].fd = -1;
Stefan Bellerc553c722015-12-15 16:04:10 -08001703 child_process_init(&pp->children[i].process);
1704
Ævar Arnfjörð Bjarmason357f8e62022-10-12 23:02:30 +02001705 if (opts->ungroup) {
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001706 ; /* no strbuf_*() work to do here */
1707 } else if (i != pp->output_owner) {
Stefan Bellerc553c722015-12-15 16:04:10 -08001708 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1709 strbuf_reset(&pp->children[i].err);
1710 } else {
Ævar Arnfjörð Bjarmason9f3df6c2022-10-12 23:02:32 +02001711 const size_t n = opts->processes;
1712
Stefan Beller2dac9b52016-02-29 18:07:15 -08001713 strbuf_write(&pp->children[i].err, stderr);
Stefan Bellerc553c722015-12-15 16:04:10 -08001714 strbuf_reset(&pp->children[i].err);
1715
1716 /* Output all other finished child processes */
Stefan Beller2dac9b52016-02-29 18:07:15 -08001717 strbuf_write(&pp->buffered_output, stderr);
Stefan Bellerc553c722015-12-15 16:04:10 -08001718 strbuf_reset(&pp->buffered_output);
1719
1720 /*
1721 * Pick next process to output live.
1722 * NEEDSWORK:
1723 * For now we pick it randomly by doing a round
1724 * robin. Later we may want to pick the one with
1725 * the most output or the longest or shortest
1726 * running process time.
1727 */
1728 for (i = 0; i < n; i++)
1729 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1730 break;
1731 pp->output_owner = (pp->output_owner + i) % n;
1732 }
1733 }
1734 return result;
1735}
1736
Ævar Arnfjörð Bjarmason6e5ba0b2022-10-12 23:02:26 +02001737void run_processes_parallel(const struct run_process_parallel_opts *opts)
Stefan Bellerc553c722015-12-15 16:04:10 -08001738{
1739 int i, code;
1740 int output_timeout = 100;
1741 int spawn_cap = 4;
Ævar Arnfjörð Bjarmason0b0ab952022-10-12 23:02:34 +02001742 struct parallel_processes_for_signal pp_sig;
Ævar Arnfjörð Bjarmasonc333e6f2022-10-12 23:02:25 +02001743 struct parallel_processes pp = {
Ævar Arnfjörð Bjarmasonc333e6f2022-10-12 23:02:25 +02001744 .buffered_output = STRBUF_INIT,
Ævar Arnfjörð Bjarmasonc333e6f2022-10-12 23:02:25 +02001745 };
Ævar Arnfjörð Bjarmason6e5ba0b2022-10-12 23:02:26 +02001746 /* options */
1747 const char *tr2_category = opts->tr2_category;
1748 const char *tr2_label = opts->tr2_label;
1749 const int do_trace2 = tr2_category && tr2_label;
Stefan Bellerc553c722015-12-15 16:04:10 -08001750
Ævar Arnfjörð Bjarmason6e5ba0b2022-10-12 23:02:26 +02001751 if (do_trace2)
1752 trace2_region_enter_printf(tr2_category, tr2_label, NULL,
1753 "max:%d", opts->processes);
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001754
Ævar Arnfjörð Bjarmason0b0ab952022-10-12 23:02:34 +02001755 pp_init(&pp, opts, &pp_sig);
Stefan Bellerc553c722015-12-15 16:04:10 -08001756 while (1) {
1757 for (i = 0;
1758 i < spawn_cap && !pp.shutdown &&
Ævar Arnfjörð Bjarmason9f3df6c2022-10-12 23:02:32 +02001759 pp.nr_processes < opts->processes;
Stefan Bellerc553c722015-12-15 16:04:10 -08001760 i++) {
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001761 code = pp_start_one(&pp, opts);
Stefan Bellerc553c722015-12-15 16:04:10 -08001762 if (!code)
1763 continue;
1764 if (code < 0) {
1765 pp.shutdown = 1;
Ævar Arnfjörð Bjarmason0b0ab952022-10-12 23:02:34 +02001766 kill_children(&pp, opts, -code);
Stefan Bellerc553c722015-12-15 16:04:10 -08001767 }
1768 break;
1769 }
1770 if (!pp.nr_processes)
1771 break;
Ævar Arnfjörð Bjarmason6e5ba0b2022-10-12 23:02:26 +02001772 if (opts->ungroup) {
Ævar Arnfjörð Bjarmason9f3df6c2022-10-12 23:02:32 +02001773 for (size_t i = 0; i < opts->processes; i++)
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001774 pp.children[i].state = GIT_CP_WAIT_CLEANUP;
1775 } else {
Ævar Arnfjörð Bjarmasond1610ee2022-10-12 23:02:33 +02001776 pp_buffer_stderr(&pp, opts, output_timeout);
Ævar Arnfjörð Bjarmasonfd3aaf52022-06-07 10:48:19 +02001777 pp_output(&pp);
1778 }
Ævar Arnfjörð Bjarmasonfa939512022-10-12 23:02:29 +02001779 code = pp_collect_finished(&pp, opts);
Stefan Bellerc553c722015-12-15 16:04:10 -08001780 if (code) {
1781 pp.shutdown = 1;
1782 if (code < 0)
Ævar Arnfjörð Bjarmason0b0ab952022-10-12 23:02:34 +02001783 kill_children(&pp, opts,-code);
Stefan Bellerc553c722015-12-15 16:04:10 -08001784 }
1785 }
1786
Ævar Arnfjörð Bjarmasond1610ee2022-10-12 23:02:33 +02001787 pp_cleanup(&pp, opts);
Ævar Arnfjörð Bjarmason6e5ba0b2022-10-12 23:02:26 +02001788
1789 if (do_trace2)
1790 trace2_region_leave(tr2_category, tr2_label, NULL);
Stefan Bellerc553c722015-12-15 16:04:10 -08001791}
Jeff Hostetleree4512e2019-02-22 14:25:01 -08001792
Derrick Stoleea95ce122020-09-17 18:11:44 +00001793int run_auto_maintenance(int quiet)
Junio C Hamano850b6ed2020-05-06 13:18:29 -07001794{
Derrick Stolee1942d482020-08-28 15:45:12 +00001795 int enabled;
Derrick Stoleea95ce122020-09-17 18:11:44 +00001796 struct child_process maint = CHILD_PROCESS_INIT;
Junio C Hamano850b6ed2020-05-06 13:18:29 -07001797
Derrick Stolee1942d482020-08-28 15:45:12 +00001798 if (!git_config_get_bool("maintenance.auto", &enabled) &&
1799 !enabled)
1800 return 0;
1801
Derrick Stoleea95ce122020-09-17 18:11:44 +00001802 maint.git_cmd = 1;
Johannes Schindelin5a22a332021-09-09 09:47:07 +00001803 maint.close_object_store = 1;
Derrick Stoleea95ce122020-09-17 18:11:44 +00001804 strvec_pushl(&maint.args, "maintenance", "run", "--auto", NULL);
1805 strvec_push(&maint.args, quiet ? "--quiet" : "--no-quiet");
1806
1807 return run_command(&maint);
Junio C Hamano850b6ed2020-05-06 13:18:29 -07001808}
Jonathan Tand1fa9432021-06-17 10:13:25 -07001809
Ævar Arnfjörð Bjarmasonb3193252022-06-02 11:09:51 +02001810void prepare_other_repo_env(struct strvec *env, const char *new_git_dir)
Jonathan Tand1fa9432021-06-17 10:13:25 -07001811{
1812 const char * const *var;
1813
1814 for (var = local_repo_env; *var; var++) {
1815 if (strcmp(*var, CONFIG_DATA_ENVIRONMENT) &&
1816 strcmp(*var, CONFIG_COUNT_ENVIRONMENT))
Ævar Arnfjörð Bjarmasonb3193252022-06-02 11:09:51 +02001817 strvec_push(env, *var);
Jonathan Tand1fa9432021-06-17 10:13:25 -07001818 }
Ævar Arnfjörð Bjarmasonb3193252022-06-02 11:09:51 +02001819 strvec_pushf(env, "%s=%s", GIT_DIR_ENVIRONMENT, new_git_dir);
Jonathan Tand1fa9432021-06-17 10:13:25 -07001820}
Jeff Hostetlerfdb13222021-09-20 15:36:17 +00001821
1822enum start_bg_result start_bg_command(struct child_process *cmd,
1823 start_bg_wait_cb *wait_cb,
1824 void *cb_data,
1825 unsigned int timeout_sec)
1826{
1827 enum start_bg_result sbgr = SBGR_ERROR;
1828 int ret;
1829 int wait_status;
1830 pid_t pid_seen;
1831 time_t time_limit;
1832
1833 /*
1834 * We do not allow clean-on-exit because the child process
1835 * should persist in the background and possibly/probably
1836 * after this process exits. So we don't want to kill the
1837 * child during our atexit routine.
1838 */
1839 if (cmd->clean_on_exit)
1840 BUG("start_bg_command() does not allow non-zero clean_on_exit");
1841
1842 if (!cmd->trace2_child_class)
1843 cmd->trace2_child_class = "background";
1844
1845 ret = start_command(cmd);
1846 if (ret) {
1847 /*
1848 * We assume that if `start_command()` fails, we
1849 * either get a complete `trace2_child_start() /
1850 * trace2_child_exit()` pair or it fails before the
1851 * `trace2_child_start()` is emitted, so we do not
1852 * need to worry about it here.
1853 *
1854 * We also assume that `start_command()` does not add
1855 * us to the cleanup list. And that it calls
Andrei Rybakb39a8412023-01-07 14:56:55 +01001856 * `child_process_clear()`.
Jeff Hostetlerfdb13222021-09-20 15:36:17 +00001857 */
1858 sbgr = SBGR_ERROR;
1859 goto done;
1860 }
1861
1862 time(&time_limit);
1863 time_limit += timeout_sec;
1864
1865wait:
1866 pid_seen = waitpid(cmd->pid, &wait_status, WNOHANG);
1867
1868 if (!pid_seen) {
1869 /*
1870 * The child is currently running. Ask the callback
1871 * if the child is ready to do work or whether we
1872 * should keep waiting for it to boot up.
1873 */
1874 ret = (*wait_cb)(cmd, cb_data);
1875 if (!ret) {
1876 /*
1877 * The child is running and "ready".
1878 */
1879 trace2_child_ready(cmd, "ready");
1880 sbgr = SBGR_READY;
1881 goto done;
1882 } else if (ret > 0) {
1883 /*
1884 * The callback said to give it more time to boot up
1885 * (subject to our timeout limit).
1886 */
1887 time_t now;
1888
1889 time(&now);
1890 if (now < time_limit)
1891 goto wait;
1892
1893 /*
1894 * Our timeout has expired. We don't try to
1895 * kill the child, but rather let it continue
1896 * (hopefully) trying to startup.
1897 */
1898 trace2_child_ready(cmd, "timeout");
1899 sbgr = SBGR_TIMEOUT;
1900 goto done;
1901 } else {
1902 /*
1903 * The cb gave up on this child. It is still running,
1904 * but our cb got an error trying to probe it.
1905 */
1906 trace2_child_ready(cmd, "error");
1907 sbgr = SBGR_CB_ERROR;
1908 goto done;
1909 }
1910 }
1911
1912 else if (pid_seen == cmd->pid) {
1913 int child_code = -1;
1914
1915 /*
1916 * The child started, but exited or was terminated
1917 * before becoming "ready".
1918 *
1919 * We try to match the behavior of `wait_or_whine()`
1920 * WRT the handling of WIFSIGNALED() and WIFEXITED()
1921 * and convert the child's status to a return code for
1922 * tracing purposes and emit the `trace2_child_exit()`
1923 * event.
1924 *
1925 * We do not want the wait_or_whine() error message
1926 * because we will be called by client-side library
1927 * routines.
1928 */
1929 if (WIFEXITED(wait_status))
1930 child_code = WEXITSTATUS(wait_status);
1931 else if (WIFSIGNALED(wait_status))
1932 child_code = WTERMSIG(wait_status) + 128;
1933 trace2_child_exit(cmd, child_code);
1934
1935 sbgr = SBGR_DIED;
1936 goto done;
1937 }
1938
1939 else if (pid_seen < 0 && errno == EINTR)
1940 goto wait;
1941
1942 trace2_child_exit(cmd, -1);
1943 sbgr = SBGR_ERROR;
1944
1945done:
1946 child_process_clear(cmd);
1947 invalidate_lstat_cache();
1948 return sbgr;
1949}