Elijah Newren | 64c8559 | 2023-05-16 06:33:49 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 2 | #include "run-command.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 3 | #include "environment.h" |
Stefan Beller | d807c4a | 2018-04-10 14:26:18 -0700 | [diff] [blame] | 4 | #include "exec-cmd.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 5 | #include "gettext.h" |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 6 | #include "sigchain.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 7 | #include "strvec.h" |
Elijah Newren | cb2a513 | 2023-04-22 20:17:09 +0000 | [diff] [blame] | 8 | #include "symlinks.h" |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 9 | #include "thread-utils.h" |
| 10 | #include "strbuf.h" |
Damien Marié | f805a00 | 2017-10-06 08:07:55 +0000 | [diff] [blame] | 11 | #include "string-list.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 12 | #include "trace.h" |
| 13 | #include "trace2.h" |
Nguyễn Thái Ngọc Duy | e73dd78 | 2018-01-18 16:45:09 +0700 | [diff] [blame] | 14 | #include "quote.h" |
Derrick Stolee | 1942d48 | 2020-08-28 15:45:12 +0000 | [diff] [blame] | 15 | #include "config.h" |
Johannes Schindelin | 28d04e1 | 2021-09-09 09:47:06 +0000 | [diff] [blame] | 16 | #include "packfile.h" |
Ævar Arnfjörð Bjarmason | 5e3aba3 | 2021-09-26 21:03:26 +0200 | [diff] [blame] | 17 | #include "hook.h" |
Jeff King | 716c1f6 | 2022-08-17 02:10:22 -0400 | [diff] [blame] | 18 | #include "compat/nonblock.h" |
Taylor Blau | 52c0f33 | 2023-05-16 17:33:43 -0400 | [diff] [blame] | 19 | #include "alloc.h" |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 20 | |
René Scharfe | 483bbd4 | 2014-08-19 21:10:48 +0200 | [diff] [blame] | 21 | void child_process_init(struct child_process *child) |
| 22 | { |
Ævar Arnfjörð Bjarmason | 5726a6b | 2021-07-01 12:51:26 +0200 | [diff] [blame] | 23 | struct child_process blank = CHILD_PROCESS_INIT; |
| 24 | memcpy(child, &blank, sizeof(*child)); |
René Scharfe | 483bbd4 | 2014-08-19 21:10:48 +0200 | [diff] [blame] | 25 | } |
| 26 | |
René Scharfe | 2d71608 | 2015-10-24 14:11:27 +0200 | [diff] [blame] | 27 | void child_process_clear(struct child_process *child) |
| 28 | { |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 29 | strvec_clear(&child->args); |
Ævar Arnfjörð Bjarmason | 29fda24 | 2022-06-02 11:09:50 +0200 | [diff] [blame] | 30 | strvec_clear(&child->env); |
René Scharfe | 2d71608 | 2015-10-24 14:11:27 +0200 | [diff] [blame] | 31 | } |
| 32 | |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 33 | struct child_to_clean { |
| 34 | pid_t pid; |
Lars Schneider | ac2fbaa | 2016-10-16 16:20:28 -0700 | [diff] [blame] | 35 | struct child_process *process; |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 36 | struct child_to_clean *next; |
| 37 | }; |
| 38 | static struct child_to_clean *children_to_clean; |
| 39 | static int installed_child_cleanup_handler; |
| 40 | |
Takashi Iwai | 507d780 | 2015-09-04 11:35:57 +0200 | [diff] [blame] | 41 | static void cleanup_children(int sig, int in_signal) |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 42 | { |
Jeff King | 46df690 | 2017-01-06 20:22:23 -0500 | [diff] [blame] | 43 | struct child_to_clean *children_to_wait_for = NULL; |
| 44 | |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 45 | while (children_to_clean) { |
| 46 | struct child_to_clean *p = children_to_clean; |
| 47 | children_to_clean = p->next; |
Lars Schneider | ac2fbaa | 2016-10-16 16:20:28 -0700 | [diff] [blame] | 48 | |
| 49 | if (p->process && !in_signal) { |
| 50 | struct child_process *process = p->process; |
| 51 | if (process->clean_on_exit_handler) { |
| 52 | trace_printf( |
| 53 | "trace: run_command: running exit handler for pid %" |
| 54 | PRIuMAX, (uintmax_t)p->pid |
| 55 | ); |
| 56 | process->clean_on_exit_handler(process); |
| 57 | } |
| 58 | } |
| 59 | |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 60 | kill(p->pid, sig); |
Jeff King | 46df690 | 2017-01-06 20:22:23 -0500 | [diff] [blame] | 61 | |
Jeff King | 7b91929 | 2017-03-17 19:20:04 -0400 | [diff] [blame] | 62 | if (p->process && p->process->wait_after_clean) { |
Jeff King | 46df690 | 2017-01-06 20:22:23 -0500 | [diff] [blame] | 63 | p->next = children_to_wait_for; |
| 64 | children_to_wait_for = p; |
| 65 | } else { |
| 66 | if (!in_signal) |
| 67 | free(p); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | while (children_to_wait_for) { |
| 72 | struct child_to_clean *p = children_to_wait_for; |
| 73 | children_to_wait_for = p->next; |
| 74 | |
| 75 | while (waitpid(p->pid, NULL, 0) < 0 && errno == EINTR) |
| 76 | ; /* spin waiting for process exit or error */ |
| 77 | |
Takashi Iwai | 507d780 | 2015-09-04 11:35:57 +0200 | [diff] [blame] | 78 | if (!in_signal) |
| 79 | free(p); |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | static void cleanup_children_on_signal(int sig) |
| 84 | { |
Takashi Iwai | 507d780 | 2015-09-04 11:35:57 +0200 | [diff] [blame] | 85 | cleanup_children(sig, 1); |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 86 | sigchain_pop(sig); |
| 87 | raise(sig); |
| 88 | } |
| 89 | |
| 90 | static void cleanup_children_on_exit(void) |
| 91 | { |
Takashi Iwai | 507d780 | 2015-09-04 11:35:57 +0200 | [diff] [blame] | 92 | cleanup_children(SIGTERM, 0); |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Lars Schneider | ac2fbaa | 2016-10-16 16:20:28 -0700 | [diff] [blame] | 95 | static void mark_child_for_cleanup(pid_t pid, struct child_process *process) |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 96 | { |
| 97 | struct child_to_clean *p = xmalloc(sizeof(*p)); |
| 98 | p->pid = pid; |
Lars Schneider | ac2fbaa | 2016-10-16 16:20:28 -0700 | [diff] [blame] | 99 | p->process = process; |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 100 | p->next = children_to_clean; |
| 101 | children_to_clean = p; |
| 102 | |
| 103 | if (!installed_child_cleanup_handler) { |
| 104 | atexit(cleanup_children_on_exit); |
| 105 | sigchain_push_common(cleanup_children_on_signal); |
| 106 | installed_child_cleanup_handler = 1; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static void clear_child_for_cleanup(pid_t pid) |
| 111 | { |
David Gould | bdee397 | 2012-09-11 15:32:47 +0100 | [diff] [blame] | 112 | struct child_to_clean **pp; |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 113 | |
David Gould | bdee397 | 2012-09-11 15:32:47 +0100 | [diff] [blame] | 114 | for (pp = &children_to_clean; *pp; pp = &(*pp)->next) { |
| 115 | struct child_to_clean *clean_me = *pp; |
| 116 | |
| 117 | if (clean_me->pid == pid) { |
| 118 | *pp = clean_me->next; |
| 119 | free(clean_me); |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Shawn O. Pearce | 9dc09c7 | 2007-03-12 14:37:28 -0400 | [diff] [blame] | 125 | static inline void close_pair(int fd[2]) |
| 126 | { |
| 127 | close(fd[0]); |
| 128 | close(fd[1]); |
| 129 | } |
| 130 | |
Brandon Williams | 38124a4 | 2017-04-25 16:46:59 -0700 | [diff] [blame] | 131 | int is_executable(const char *name) |
| 132 | { |
| 133 | struct stat st; |
| 134 | |
| 135 | if (stat(name, &st) || /* stat, not lstat */ |
| 136 | !S_ISREG(st.st_mode)) |
| 137 | return 0; |
| 138 | |
| 139 | #if defined(GIT_WINDOWS_NATIVE) |
| 140 | /* |
| 141 | * On Windows there is no executable bit. The file extension |
| 142 | * indicates whether it can be run as an executable, and Git |
| 143 | * has special-handling to detect scripts and launch them |
| 144 | * through the indicated script interpreter. We test for the |
| 145 | * file extension first because virus scanners may make |
| 146 | * it quite expensive to open many files. |
| 147 | */ |
| 148 | if (ends_with(name, ".exe")) |
| 149 | return S_IXUSR; |
| 150 | |
| 151 | { |
| 152 | /* |
| 153 | * Now that we know it does not have an executable extension, |
| 154 | * peek into the file instead. |
| 155 | */ |
| 156 | char buf[3] = { 0 }; |
| 157 | int n; |
| 158 | int fd = open(name, O_RDONLY); |
| 159 | st.st_mode &= ~S_IXUSR; |
| 160 | if (fd >= 0) { |
| 161 | n = read(fd, buf, 2); |
| 162 | if (n == 2) |
| 163 | /* look for a she-bang */ |
| 164 | if (!strcmp(buf, "#!")) |
| 165 | st.st_mode |= S_IXUSR; |
| 166 | close(fd); |
| 167 | } |
| 168 | } |
| 169 | #endif |
| 170 | return st.st_mode & S_IXUSR; |
| 171 | } |
| 172 | |
Matthias Aßhauer | bb532b5 | 2023-08-04 04:08:42 +0000 | [diff] [blame] | 173 | #ifndef locate_in_PATH |
Brandon Williams | 9402831 | 2017-04-25 16:47:00 -0700 | [diff] [blame] | 174 | /* |
| 175 | * Search $PATH for a command. This emulates the path search that |
| 176 | * execvp would perform, without actually executing the command so it |
| 177 | * can be used before fork() to prepare to run a command using |
| 178 | * execve() or after execvp() to diagnose why it failed. |
| 179 | * |
| 180 | * The caller should ensure that file contains no directory |
| 181 | * separators. |
| 182 | * |
| 183 | * Returns the path to the command, as found in $PATH or NULL if the |
| 184 | * command could not be found. The caller inherits ownership of the memory |
| 185 | * used to store the resultant path. |
| 186 | * |
| 187 | * This should not be used on Windows, where the $PATH search rules |
| 188 | * are more complicated (e.g., a search for "foo" should find |
| 189 | * "foo.exe"). |
| 190 | */ |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 191 | static char *locate_in_PATH(const char *file) |
| 192 | { |
| 193 | const char *p = getenv("PATH"); |
| 194 | struct strbuf buf = STRBUF_INIT; |
| 195 | |
| 196 | if (!p || !*p) |
| 197 | return NULL; |
| 198 | |
| 199 | while (1) { |
| 200 | const char *end = strchrnul(p, ':'); |
| 201 | |
| 202 | strbuf_reset(&buf); |
| 203 | |
| 204 | /* POSIX specifies an empty entry as the current directory. */ |
| 205 | if (end != p) { |
| 206 | strbuf_add(&buf, p, end - p); |
| 207 | strbuf_addch(&buf, '/'); |
| 208 | } |
| 209 | strbuf_addstr(&buf, file); |
| 210 | |
Brandon Williams | 9402831 | 2017-04-25 16:47:00 -0700 | [diff] [blame] | 211 | if (is_executable(buf.buf)) |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 212 | return strbuf_detach(&buf, NULL); |
| 213 | |
| 214 | if (!*end) |
| 215 | break; |
| 216 | p = end + 1; |
| 217 | } |
| 218 | |
| 219 | strbuf_release(&buf); |
| 220 | return NULL; |
| 221 | } |
Matthias Aßhauer | bb532b5 | 2023-08-04 04:08:42 +0000 | [diff] [blame] | 222 | #endif |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 223 | |
Pranit Bauva | 3f36e6f | 2021-09-13 19:39:01 +0200 | [diff] [blame] | 224 | int exists_in_PATH(const char *command) |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 225 | { |
Pranit Bauva | 3f36e6f | 2021-09-13 19:39:01 +0200 | [diff] [blame] | 226 | char *r = locate_in_PATH(command); |
brian m. carlson | 63ab08f | 2020-01-07 01:36:40 +0000 | [diff] [blame] | 227 | int found = r != NULL; |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 228 | free(r); |
brian m. carlson | 63ab08f | 2020-01-07 01:36:40 +0000 | [diff] [blame] | 229 | return found; |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | int sane_execvp(const char *file, char * const argv[]) |
| 233 | { |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 234 | #ifndef GIT_WINDOWS_NATIVE |
| 235 | /* |
| 236 | * execvp() doesn't return, so we all we can do is tell trace2 |
| 237 | * what we are about to do and let it leave a hint in the log |
| 238 | * (unless of course the execvp() fails). |
| 239 | * |
| 240 | * we skip this for Windows because the compat layer already |
| 241 | * has to emulate the execvp() call anyway. |
| 242 | */ |
| 243 | int exec_id = trace2_exec(file, (const char **)argv); |
| 244 | #endif |
| 245 | |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 246 | if (!execvp(file, argv)) |
| 247 | return 0; /* cannot happen ;-) */ |
| 248 | |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 249 | #ifndef GIT_WINDOWS_NATIVE |
| 250 | { |
| 251 | int ec = errno; |
| 252 | trace2_exec_result(exec_id, ec); |
| 253 | errno = ec; |
| 254 | } |
| 255 | #endif |
| 256 | |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 257 | /* |
| 258 | * When a command can't be found because one of the directories |
| 259 | * listed in $PATH is unsearchable, execvp reports EACCES, but |
| 260 | * careful usability testing (read: analysis of occasional bug |
| 261 | * reports) reveals that "No such file or directory" is more |
| 262 | * intuitive. |
| 263 | * |
| 264 | * We avoid commands with "/", because execvp will not do $PATH |
| 265 | * lookups in that case. |
| 266 | * |
| 267 | * The reassignment of EACCES to errno looks like a no-op below, |
| 268 | * but we need to protect against exists_in_PATH overwriting errno. |
| 269 | */ |
| 270 | if (errno == EACCES && !strchr(file, '/')) |
| 271 | errno = exists_in_PATH(file) ? EACCES : ENOENT; |
Junio C Hamano | a785508 | 2012-07-31 12:51:30 -0700 | [diff] [blame] | 272 | else if (errno == ENOTDIR && !strchr(file, '/')) |
| 273 | errno = ENOENT; |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 277 | static const char **prepare_shell_cmd(struct strvec *out, const char **argv) |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 278 | { |
Jeff King | 20574f5 | 2016-02-22 17:44:39 -0500 | [diff] [blame] | 279 | if (!argv[0]) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 280 | BUG("shell command is empty"); |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 281 | |
Jeff King | f445644 | 2009-12-30 05:55:36 -0500 | [diff] [blame] | 282 | if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) { |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 283 | #ifndef GIT_WINDOWS_NATIVE |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 284 | strvec_push(out, SHELL_PATH); |
Johannes Sixt | 7762975 | 2012-04-17 09:03:21 +0200 | [diff] [blame] | 285 | #else |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 286 | strvec_push(out, "sh"); |
Johannes Sixt | 7762975 | 2012-04-17 09:03:21 +0200 | [diff] [blame] | 287 | #endif |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 288 | strvec_push(out, "-c"); |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 289 | |
Jeff King | 20574f5 | 2016-02-22 17:44:39 -0500 | [diff] [blame] | 290 | /* |
| 291 | * If we have no extra arguments, we do not even need to |
| 292 | * bother with the "$@" magic. |
| 293 | */ |
| 294 | if (!argv[1]) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 295 | strvec_push(out, argv[0]); |
Jeff King | 20574f5 | 2016-02-22 17:44:39 -0500 | [diff] [blame] | 296 | else |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 297 | strvec_pushf(out, "%s \"$@\"", argv[0]); |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 298 | } |
| 299 | |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 300 | strvec_pushv(out, argv); |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 301 | return out->v; |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 302 | } |
| 303 | |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 304 | #ifndef GIT_WINDOWS_NATIVE |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 305 | static int child_notifier = -1; |
| 306 | |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 307 | enum child_errcode { |
| 308 | CHILD_ERR_CHDIR, |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 309 | CHILD_ERR_DUP2, |
| 310 | CHILD_ERR_CLOSE, |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 311 | CHILD_ERR_SIGPROCMASK, |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 312 | CHILD_ERR_SILENT, |
| 313 | CHILD_ERR_ERRNO |
| 314 | }; |
| 315 | |
| 316 | struct child_err { |
| 317 | enum child_errcode err; |
| 318 | int syserr; /* errno */ |
| 319 | }; |
| 320 | |
| 321 | static void child_die(enum child_errcode err) |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 322 | { |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 323 | struct child_err buf; |
| 324 | |
| 325 | buf.err = err; |
| 326 | buf.syserr = errno; |
| 327 | |
| 328 | /* write(2) on buf smaller than PIPE_BUF (min 512) is atomic: */ |
| 329 | xwrite(child_notifier, &buf, sizeof(buf)); |
| 330 | _exit(1); |
| 331 | } |
| 332 | |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 333 | static void child_dup2(int fd, int to) |
| 334 | { |
| 335 | if (dup2(fd, to) < 0) |
| 336 | child_die(CHILD_ERR_DUP2); |
| 337 | } |
| 338 | |
| 339 | static void child_close(int fd) |
| 340 | { |
| 341 | if (close(fd)) |
| 342 | child_die(CHILD_ERR_CLOSE); |
| 343 | } |
| 344 | |
| 345 | static void child_close_pair(int fd[2]) |
| 346 | { |
| 347 | child_close(fd[0]); |
| 348 | child_close(fd[1]); |
| 349 | } |
| 350 | |
Jeff King | ce41759 | 2023-02-24 01:39:18 -0500 | [diff] [blame] | 351 | static void child_error_fn(const char *err UNUSED, va_list params UNUSED) |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 352 | { |
| 353 | const char msg[] = "error() should not be called in child\n"; |
| 354 | xwrite(2, msg, sizeof(msg) - 1); |
| 355 | } |
| 356 | |
Jeff King | ce41759 | 2023-02-24 01:39:18 -0500 | [diff] [blame] | 357 | static void child_warn_fn(const char *err UNUSED, va_list params UNUSED) |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 358 | { |
| 359 | const char msg[] = "warn() should not be called in child\n"; |
| 360 | xwrite(2, msg, sizeof(msg) - 1); |
| 361 | } |
| 362 | |
Jeff King | ce41759 | 2023-02-24 01:39:18 -0500 | [diff] [blame] | 363 | static void NORETURN child_die_fn(const char *err UNUSED, va_list params UNUSED) |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 364 | { |
| 365 | const char msg[] = "die() should not be called in child\n"; |
| 366 | xwrite(2, msg, sizeof(msg) - 1); |
| 367 | _exit(2); |
| 368 | } |
| 369 | |
| 370 | /* this runs in the parent process */ |
| 371 | static void child_err_spew(struct child_process *cmd, struct child_err *cerr) |
| 372 | { |
| 373 | static void (*old_errfn)(const char *err, va_list params); |
Ævar Arnfjörð Bjarmason | e081a7c | 2021-12-07 19:26:30 +0100 | [diff] [blame] | 374 | report_fn die_message_routine = get_die_message_routine(); |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 375 | |
| 376 | old_errfn = get_error_routine(); |
Ævar Arnfjörð Bjarmason | e081a7c | 2021-12-07 19:26:30 +0100 | [diff] [blame] | 377 | set_error_routine(die_message_routine); |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 378 | errno = cerr->syserr; |
| 379 | |
| 380 | switch (cerr->err) { |
| 381 | case CHILD_ERR_CHDIR: |
| 382 | error_errno("exec '%s': cd to '%s' failed", |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 383 | cmd->args.v[0], cmd->dir); |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 384 | break; |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 385 | case CHILD_ERR_DUP2: |
| 386 | error_errno("dup2() in child failed"); |
| 387 | break; |
| 388 | case CHILD_ERR_CLOSE: |
| 389 | error_errno("close() in child failed"); |
| 390 | break; |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 391 | case CHILD_ERR_SIGPROCMASK: |
| 392 | error_errno("sigprocmask failed restoring signals"); |
| 393 | break; |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 394 | case CHILD_ERR_SILENT: |
| 395 | break; |
| 396 | case CHILD_ERR_ERRNO: |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 397 | error_errno("cannot exec '%s'", cmd->args.v[0]); |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 398 | break; |
| 399 | } |
| 400 | set_error_routine(old_errfn); |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 401 | } |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 402 | |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 403 | static int prepare_cmd(struct strvec *out, const struct child_process *cmd) |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 404 | { |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 405 | if (!cmd->args.v[0]) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 406 | BUG("command is empty"); |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 407 | |
Brandon Williams | e3a4344 | 2017-04-19 16:13:20 -0700 | [diff] [blame] | 408 | /* |
| 409 | * Add SHELL_PATH so in the event exec fails with ENOEXEC we can |
| 410 | * attempt to interpret the command with 'sh'. |
| 411 | */ |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 412 | strvec_push(out, SHELL_PATH); |
Brandon Williams | e3a4344 | 2017-04-19 16:13:20 -0700 | [diff] [blame] | 413 | |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 414 | if (cmd->git_cmd) { |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 415 | prepare_git_cmd(out, cmd->args.v); |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 416 | } else if (cmd->use_shell) { |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 417 | prepare_shell_cmd(out, cmd->args.v); |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 418 | } else { |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 419 | strvec_pushv(out, cmd->args.v); |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 420 | } |
Brandon Williams | e3a4344 | 2017-04-19 16:13:20 -0700 | [diff] [blame] | 421 | |
| 422 | /* |
Andras Kucsma | 05ac858 | 2020-03-27 00:36:43 +0000 | [diff] [blame] | 423 | * If there are no dir separator characters in the command then perform |
| 424 | * a path lookup and use the resolved path as the command to exec. If |
| 425 | * there are dir separator characters, we have exec attempt to invoke |
| 426 | * the command directly. |
Brandon Williams | e3a4344 | 2017-04-19 16:13:20 -0700 | [diff] [blame] | 427 | */ |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 428 | if (!has_dir_sep(out->v[1])) { |
| 429 | char *program = locate_in_PATH(out->v[1]); |
Brandon Williams | e3a4344 | 2017-04-19 16:13:20 -0700 | [diff] [blame] | 430 | if (program) { |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 431 | free((char *)out->v[1]); |
| 432 | out->v[1] = program; |
Jeff King | 321fd82 | 2018-10-24 03:38:00 -0400 | [diff] [blame] | 433 | } else { |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 434 | strvec_clear(out); |
Jeff King | 321fd82 | 2018-10-24 03:38:00 -0400 | [diff] [blame] | 435 | errno = ENOENT; |
| 436 | return -1; |
Brandon Williams | e3a4344 | 2017-04-19 16:13:20 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
Jeff King | 321fd82 | 2018-10-24 03:38:00 -0400 | [diff] [blame] | 439 | |
| 440 | return 0; |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 441 | } |
Brandon Williams | ae25394 | 2017-04-19 16:13:22 -0700 | [diff] [blame] | 442 | |
| 443 | static char **prep_childenv(const char *const *deltaenv) |
| 444 | { |
| 445 | extern char **environ; |
| 446 | char **childenv; |
| 447 | struct string_list env = STRING_LIST_INIT_DUP; |
| 448 | struct strbuf key = STRBUF_INIT; |
| 449 | const char *const *p; |
| 450 | int i; |
| 451 | |
| 452 | /* Construct a sorted string list consisting of the current environ */ |
| 453 | for (p = (const char *const *) environ; p && *p; p++) { |
| 454 | const char *equals = strchr(*p, '='); |
| 455 | |
| 456 | if (equals) { |
| 457 | strbuf_reset(&key); |
| 458 | strbuf_add(&key, *p, equals - *p); |
| 459 | string_list_append(&env, key.buf)->util = (void *) *p; |
| 460 | } else { |
| 461 | string_list_append(&env, *p)->util = (void *) *p; |
| 462 | } |
| 463 | } |
| 464 | string_list_sort(&env); |
| 465 | |
| 466 | /* Merge in 'deltaenv' with the current environ */ |
| 467 | for (p = deltaenv; p && *p; p++) { |
| 468 | const char *equals = strchr(*p, '='); |
| 469 | |
| 470 | if (equals) { |
| 471 | /* ('key=value'), insert or replace entry */ |
| 472 | strbuf_reset(&key); |
| 473 | strbuf_add(&key, *p, equals - *p); |
| 474 | string_list_insert(&env, key.buf)->util = (void *) *p; |
| 475 | } else { |
| 476 | /* otherwise ('key') remove existing entry */ |
| 477 | string_list_remove(&env, *p, 0); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | /* Create an array of 'char *' to be used as the childenv */ |
René Scharfe | 0e187d7 | 2017-10-01 17:14:31 +0200 | [diff] [blame] | 482 | ALLOC_ARRAY(childenv, env.nr + 1); |
Brandon Williams | ae25394 | 2017-04-19 16:13:22 -0700 | [diff] [blame] | 483 | for (i = 0; i < env.nr; i++) |
| 484 | childenv[i] = env.items[i].util; |
| 485 | childenv[env.nr] = NULL; |
| 486 | |
| 487 | string_list_clear(&env, 0); |
| 488 | strbuf_release(&key); |
| 489 | return childenv; |
| 490 | } |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 491 | |
| 492 | struct atfork_state { |
| 493 | #ifndef NO_PTHREADS |
| 494 | int cs; |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 495 | #endif |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 496 | sigset_t old; |
| 497 | }; |
| 498 | |
Johannes Schindelin | dde74d7 | 2018-05-02 11:38:31 +0200 | [diff] [blame] | 499 | #define CHECK_BUG(err, msg) \ |
| 500 | do { \ |
| 501 | int e = (err); \ |
| 502 | if (e) \ |
| 503 | BUG("%s: %s", msg, strerror(e)); \ |
| 504 | } while(0) |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 505 | |
| 506 | static void atfork_prepare(struct atfork_state *as) |
| 507 | { |
| 508 | sigset_t all; |
| 509 | |
| 510 | if (sigfillset(&all)) |
| 511 | die_errno("sigfillset"); |
| 512 | #ifdef NO_PTHREADS |
| 513 | if (sigprocmask(SIG_SETMASK, &all, &as->old)) |
| 514 | die_errno("sigprocmask"); |
| 515 | #else |
Johannes Schindelin | dde74d7 | 2018-05-02 11:38:31 +0200 | [diff] [blame] | 516 | CHECK_BUG(pthread_sigmask(SIG_SETMASK, &all, &as->old), |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 517 | "blocking all signals"); |
Johannes Schindelin | dde74d7 | 2018-05-02 11:38:31 +0200 | [diff] [blame] | 518 | CHECK_BUG(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &as->cs), |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 519 | "disabling cancellation"); |
| 520 | #endif |
| 521 | } |
| 522 | |
| 523 | static void atfork_parent(struct atfork_state *as) |
| 524 | { |
| 525 | #ifdef NO_PTHREADS |
| 526 | if (sigprocmask(SIG_SETMASK, &as->old, NULL)) |
| 527 | die_errno("sigprocmask"); |
| 528 | #else |
Johannes Schindelin | dde74d7 | 2018-05-02 11:38:31 +0200 | [diff] [blame] | 529 | CHECK_BUG(pthread_setcancelstate(as->cs, NULL), |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 530 | "re-enabling cancellation"); |
Johannes Schindelin | dde74d7 | 2018-05-02 11:38:31 +0200 | [diff] [blame] | 531 | CHECK_BUG(pthread_sigmask(SIG_SETMASK, &as->old, NULL), |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 532 | "restoring signal mask"); |
| 533 | #endif |
| 534 | } |
| 535 | #endif /* GIT_WINDOWS_NATIVE */ |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 536 | |
| 537 | static inline void set_cloexec(int fd) |
| 538 | { |
| 539 | int flags = fcntl(fd, F_GETFD); |
| 540 | if (flags >= 0) |
| 541 | fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
| 542 | } |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 543 | |
Takashi Iwai | 507d780 | 2015-09-04 11:35:57 +0200 | [diff] [blame] | 544 | static int wait_or_whine(pid_t pid, const char *argv0, int in_signal) |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 545 | { |
| 546 | int status, code = -1; |
| 547 | pid_t waiting; |
| 548 | int failed_errno = 0; |
| 549 | |
| 550 | while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR) |
| 551 | ; /* nothing */ |
| 552 | |
| 553 | if (waiting < 0) { |
| 554 | failed_errno = errno; |
Jeff King | 96bfb2d | 2021-11-22 16:28:13 -0500 | [diff] [blame] | 555 | if (!in_signal) |
| 556 | error_errno("waitpid for %s failed", argv0); |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 557 | } else if (waiting != pid) { |
Jeff King | 96bfb2d | 2021-11-22 16:28:13 -0500 | [diff] [blame] | 558 | if (!in_signal) |
| 559 | error("waitpid is confused (%s)", argv0); |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 560 | } else if (WIFSIGNALED(status)) { |
| 561 | code = WTERMSIG(status); |
Jeff King | 96bfb2d | 2021-11-22 16:28:13 -0500 | [diff] [blame] | 562 | if (!in_signal && code != SIGINT && code != SIGQUIT && code != SIGPIPE) |
Jeff King | a2767c5 | 2012-11-30 17:41:38 -0500 | [diff] [blame] | 563 | error("%s died of signal %d", argv0, code); |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 564 | /* |
| 565 | * This return value is chosen so that code & 0xff |
| 566 | * mimics the exit code that a POSIX shell would report for |
| 567 | * a program that died from this signal. |
| 568 | */ |
Jeff King | 709ca73 | 2013-01-05 09:49:49 -0500 | [diff] [blame] | 569 | code += 128; |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 570 | } else if (WIFEXITED(status)) { |
| 571 | code = WEXITSTATUS(status); |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 572 | } else { |
Jeff King | 96bfb2d | 2021-11-22 16:28:13 -0500 | [diff] [blame] | 573 | if (!in_signal) |
| 574 | error("waitpid is confused (%s)", argv0); |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 575 | } |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 576 | |
Jeff King | 96bfb2d | 2021-11-22 16:28:13 -0500 | [diff] [blame] | 577 | if (!in_signal) |
| 578 | clear_child_for_cleanup(pid); |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 579 | |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 580 | errno = failed_errno; |
| 581 | return code; |
| 582 | } |
| 583 | |
Nguyễn Thái Ngọc Duy | c61a975 | 2018-01-18 16:45:11 +0700 | [diff] [blame] | 584 | static void trace_add_env(struct strbuf *dst, const char *const *deltaenv) |
| 585 | { |
| 586 | struct string_list envs = STRING_LIST_INIT_DUP; |
| 587 | const char *const *e; |
| 588 | int i; |
| 589 | int printed_unset = 0; |
| 590 | |
| 591 | /* Last one wins, see run-command.c:prep_childenv() for context */ |
| 592 | for (e = deltaenv; e && *e; e++) { |
| 593 | struct strbuf key = STRBUF_INIT; |
| 594 | char *equals = strchr(*e, '='); |
| 595 | |
| 596 | if (equals) { |
| 597 | strbuf_add(&key, *e, equals - *e); |
| 598 | string_list_insert(&envs, key.buf)->util = equals + 1; |
| 599 | } else { |
| 600 | string_list_insert(&envs, *e)->util = NULL; |
| 601 | } |
| 602 | strbuf_release(&key); |
| 603 | } |
| 604 | |
| 605 | /* "unset X Y...;" */ |
| 606 | for (i = 0; i < envs.nr; i++) { |
| 607 | const char *var = envs.items[i].string; |
| 608 | const char *val = envs.items[i].util; |
| 609 | |
| 610 | if (val || !getenv(var)) |
| 611 | continue; |
| 612 | |
| 613 | if (!printed_unset) { |
| 614 | strbuf_addstr(dst, " unset"); |
| 615 | printed_unset = 1; |
| 616 | } |
| 617 | strbuf_addf(dst, " %s", var); |
| 618 | } |
| 619 | if (printed_unset) |
| 620 | strbuf_addch(dst, ';'); |
| 621 | |
| 622 | /* ... followed by "A=B C=D ..." */ |
| 623 | for (i = 0; i < envs.nr; i++) { |
| 624 | const char *var = envs.items[i].string; |
| 625 | const char *val = envs.items[i].util; |
| 626 | const char *oldval; |
| 627 | |
| 628 | if (!val) |
| 629 | continue; |
| 630 | |
| 631 | oldval = getenv(var); |
| 632 | if (oldval && !strcmp(val, oldval)) |
| 633 | continue; |
| 634 | |
| 635 | strbuf_addf(dst, " %s=", var); |
| 636 | sq_quote_buf_pretty(dst, val); |
| 637 | } |
| 638 | string_list_clear(&envs, 0); |
| 639 | } |
| 640 | |
Nguyễn Thái Ngọc Duy | e73dd78 | 2018-01-18 16:45:09 +0700 | [diff] [blame] | 641 | static void trace_run_command(const struct child_process *cp) |
| 642 | { |
| 643 | struct strbuf buf = STRBUF_INIT; |
| 644 | |
| 645 | if (!trace_want(&trace_default_key)) |
| 646 | return; |
| 647 | |
René Scharfe | 248f66e | 2018-03-25 12:57:50 +0200 | [diff] [blame] | 648 | strbuf_addstr(&buf, "trace: run_command:"); |
Nguyễn Thái Ngọc Duy | 090a092 | 2018-01-18 16:45:12 +0700 | [diff] [blame] | 649 | if (cp->dir) { |
| 650 | strbuf_addstr(&buf, " cd "); |
| 651 | sq_quote_buf_pretty(&buf, cp->dir); |
| 652 | strbuf_addch(&buf, ';'); |
| 653 | } |
Ævar Arnfjörð Bjarmason | 29fda24 | 2022-06-02 11:09:50 +0200 | [diff] [blame] | 654 | trace_add_env(&buf, cp->env.v); |
Nguyễn Thái Ngọc Duy | 21dfc5e | 2018-01-18 16:45:10 +0700 | [diff] [blame] | 655 | if (cp->git_cmd) |
| 656 | strbuf_addstr(&buf, " git"); |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 657 | sq_quote_argv_pretty(&buf, cp->args.v); |
Nguyễn Thái Ngọc Duy | e73dd78 | 2018-01-18 16:45:09 +0700 | [diff] [blame] | 658 | |
| 659 | trace_printf("%s", buf.buf); |
| 660 | strbuf_release(&buf); |
| 661 | } |
| 662 | |
Shawn O. Pearce | ebcb5d1 | 2007-03-10 03:28:05 -0500 | [diff] [blame] | 663 | int start_command(struct child_process *cmd) |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 664 | { |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 665 | int need_in, need_out, need_err; |
| 666 | int fdin[2], fdout[2], fderr[2]; |
Jeff King | 25043d8 | 2013-03-21 11:45:00 -0400 | [diff] [blame] | 667 | int failed_errno; |
Stephen Boyd | 939296c | 2013-01-30 18:01:05 -0800 | [diff] [blame] | 668 | char *str; |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 669 | |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 670 | /* |
| 671 | * In case of errors we must keep the promise to close FDs |
| 672 | * that have been passed in via ->in and ->out. |
| 673 | */ |
| 674 | |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 675 | need_in = !cmd->no_stdin && cmd->in < 0; |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 676 | if (need_in) { |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 677 | if (pipe(fdin) < 0) { |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 678 | failed_errno = errno; |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 679 | if (cmd->out > 0) |
| 680 | close(cmd->out); |
Stephen Boyd | 939296c | 2013-01-30 18:01:05 -0800 | [diff] [blame] | 681 | str = "standard input"; |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 682 | goto fail_pipe; |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 683 | } |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 684 | cmd->in = fdin[1]; |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 685 | } |
| 686 | |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 687 | need_out = !cmd->no_stdout |
| 688 | && !cmd->stdout_to_stderr |
| 689 | && cmd->out < 0; |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 690 | if (need_out) { |
| 691 | if (pipe(fdout) < 0) { |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 692 | failed_errno = errno; |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 693 | if (need_in) |
| 694 | close_pair(fdin); |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 695 | else if (cmd->in) |
| 696 | close(cmd->in); |
Stephen Boyd | 939296c | 2013-01-30 18:01:05 -0800 | [diff] [blame] | 697 | str = "standard output"; |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 698 | goto fail_pipe; |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 699 | } |
| 700 | cmd->out = fdout[0]; |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 701 | } |
| 702 | |
Shawn O. Pearce | b73a439 | 2007-11-11 02:29:37 -0500 | [diff] [blame] | 703 | need_err = !cmd->no_stderr && cmd->err < 0; |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 704 | if (need_err) { |
| 705 | if (pipe(fderr) < 0) { |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 706 | failed_errno = errno; |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 707 | if (need_in) |
| 708 | close_pair(fdin); |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 709 | else if (cmd->in) |
| 710 | close(cmd->in); |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 711 | if (need_out) |
| 712 | close_pair(fdout); |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 713 | else if (cmd->out) |
| 714 | close(cmd->out); |
Stephen Boyd | 939296c | 2013-01-30 18:01:05 -0800 | [diff] [blame] | 715 | str = "standard error"; |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 716 | fail_pipe: |
Stephen Boyd | 939296c | 2013-01-30 18:01:05 -0800 | [diff] [blame] | 717 | error("cannot create %s pipe for %s: %s", |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 718 | str, cmd->args.v[0], strerror(failed_errno)); |
René Scharfe | 2d71608 | 2015-10-24 14:11:27 +0200 | [diff] [blame] | 719 | child_process_clear(cmd); |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 720 | errno = failed_errno; |
| 721 | return -1; |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 722 | } |
| 723 | cmd->err = fderr[0]; |
| 724 | } |
| 725 | |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 726 | trace2_child_start(cmd); |
Nguyễn Thái Ngọc Duy | e73dd78 | 2018-01-18 16:45:09 +0700 | [diff] [blame] | 727 | trace_run_command(cmd); |
| 728 | |
Johannes Sixt | 13af8cb | 2011-02-04 09:41:58 +0100 | [diff] [blame] | 729 | fflush(NULL); |
Johannes Schindelin | 8852f5d | 2008-07-07 14:41:34 +0100 | [diff] [blame] | 730 | |
Johannes Schindelin | 28d04e1 | 2021-09-09 09:47:06 +0000 | [diff] [blame] | 731 | if (cmd->close_object_store) |
| 732 | close_object_store(the_repository->objects); |
| 733 | |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 734 | #ifndef GIT_WINDOWS_NATIVE |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 735 | { |
| 736 | int notify_pipe[2]; |
Brandon Williams | db015a2 | 2017-04-19 16:13:23 -0700 | [diff] [blame] | 737 | int null_fd = -1; |
Brandon Williams | ae25394 | 2017-04-19 16:13:22 -0700 | [diff] [blame] | 738 | char **childenv; |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 739 | struct strvec argv = STRVEC_INIT; |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 740 | struct child_err cerr; |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 741 | struct atfork_state as; |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 742 | |
Jeff King | 321fd82 | 2018-10-24 03:38:00 -0400 | [diff] [blame] | 743 | if (prepare_cmd(&argv, cmd) < 0) { |
| 744 | failed_errno = errno; |
| 745 | cmd->pid = -1; |
Junio C Hamano | e5a329a | 2018-12-11 14:46:07 +0900 | [diff] [blame] | 746 | if (!cmd->silent_exec_failure) |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 747 | error_errno("cannot run %s", cmd->args.v[0]); |
Jeff King | 321fd82 | 2018-10-24 03:38:00 -0400 | [diff] [blame] | 748 | goto end_of_spawn; |
| 749 | } |
| 750 | |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 751 | if (pipe(notify_pipe)) |
| 752 | notify_pipe[0] = notify_pipe[1] = -1; |
| 753 | |
Brandon Williams | db015a2 | 2017-04-19 16:13:23 -0700 | [diff] [blame] | 754 | if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) { |
René Scharfe | 66e905b | 2021-08-25 22:16:46 +0200 | [diff] [blame] | 755 | null_fd = xopen("/dev/null", O_RDWR | O_CLOEXEC); |
Brandon Williams | db015a2 | 2017-04-19 16:13:23 -0700 | [diff] [blame] | 756 | set_cloexec(null_fd); |
| 757 | } |
| 758 | |
Ævar Arnfjörð Bjarmason | 29fda24 | 2022-06-02 11:09:50 +0200 | [diff] [blame] | 759 | childenv = prep_childenv(cmd->env.v); |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 760 | atfork_prepare(&as); |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 761 | |
Brandon Williams | e503cd6 | 2017-04-19 16:13:26 -0700 | [diff] [blame] | 762 | /* |
| 763 | * NOTE: In order to prevent deadlocking when using threads special |
| 764 | * care should be taken with the function calls made in between the |
| 765 | * fork() and exec() calls. No calls should be made to functions which |
| 766 | * require acquiring a lock (e.g. malloc) as the lock could have been |
| 767 | * held by another thread at the time of forking, causing the lock to |
| 768 | * never be released in the child process. This means only |
| 769 | * Async-Signal-Safe functions are permitted in the child. |
| 770 | */ |
Shawn O. Pearce | ebcb5d1 | 2007-03-10 03:28:05 -0500 | [diff] [blame] | 771 | cmd->pid = fork(); |
Jeff King | 25043d8 | 2013-03-21 11:45:00 -0400 | [diff] [blame] | 772 | failed_errno = errno; |
Shawn O. Pearce | ebcb5d1 | 2007-03-10 03:28:05 -0500 | [diff] [blame] | 773 | if (!cmd->pid) { |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 774 | int sig; |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 775 | /* |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 776 | * Ensure the default die/error/warn routines do not get |
| 777 | * called, they can take stdio locks and malloc. |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 778 | */ |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 779 | set_die_routine(child_die_fn); |
| 780 | set_error_routine(child_error_fn); |
| 781 | set_warn_routine(child_warn_fn); |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 782 | |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 783 | close(notify_pipe[0]); |
| 784 | set_cloexec(notify_pipe[1]); |
| 785 | child_notifier = notify_pipe[1]; |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 786 | |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 787 | if (cmd->no_stdin) |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 788 | child_dup2(null_fd, 0); |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 789 | else if (need_in) { |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 790 | child_dup2(fdin[0], 0); |
| 791 | child_close_pair(fdin); |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 792 | } else if (cmd->in) { |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 793 | child_dup2(cmd->in, 0); |
| 794 | child_close(cmd->in); |
Shawn O. Pearce | 95d3c4f | 2006-12-30 21:55:22 -0500 | [diff] [blame] | 795 | } |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 796 | |
Christian Couder | ce2cf27 | 2008-03-05 08:35:16 +0100 | [diff] [blame] | 797 | if (cmd->no_stderr) |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 798 | child_dup2(null_fd, 2); |
Christian Couder | ce2cf27 | 2008-03-05 08:35:16 +0100 | [diff] [blame] | 799 | else if (need_err) { |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 800 | child_dup2(fderr[1], 2); |
| 801 | child_close_pair(fderr); |
Shawn O. Pearce | 4f41b61 | 2010-02-05 12:57:37 -0800 | [diff] [blame] | 802 | } else if (cmd->err > 1) { |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 803 | child_dup2(cmd->err, 2); |
| 804 | child_close(cmd->err); |
Christian Couder | ce2cf27 | 2008-03-05 08:35:16 +0100 | [diff] [blame] | 805 | } |
| 806 | |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 807 | if (cmd->no_stdout) |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 808 | child_dup2(null_fd, 1); |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 809 | else if (cmd->stdout_to_stderr) |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 810 | child_dup2(2, 1); |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 811 | else if (need_out) { |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 812 | child_dup2(fdout[1], 1); |
| 813 | child_close_pair(fdout); |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 814 | } else if (cmd->out > 1) { |
Brandon Williams | 53fa675 | 2017-04-19 16:13:25 -0700 | [diff] [blame] | 815 | child_dup2(cmd->out, 1); |
| 816 | child_close(cmd->out); |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 817 | } |
| 818 | |
Alex Riesen | 1568fea | 2007-05-22 23:48:23 +0200 | [diff] [blame] | 819 | if (cmd->dir && chdir(cmd->dir)) |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 820 | child_die(CHILD_ERR_CHDIR); |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 821 | |
Brandon Williams | e3a4344 | 2017-04-19 16:13:20 -0700 | [diff] [blame] | 822 | /* |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 823 | * restore default signal handlers here, in case |
| 824 | * we catch a signal right before execve below |
| 825 | */ |
| 826 | for (sig = 1; sig < NSIG; sig++) { |
| 827 | /* ignored signals get reset to SIG_DFL on execve */ |
| 828 | if (signal(sig, SIG_DFL) == SIG_IGN) |
| 829 | signal(sig, SIG_IGN); |
| 830 | } |
| 831 | |
| 832 | if (sigprocmask(SIG_SETMASK, &as.old, NULL) != 0) |
| 833 | child_die(CHILD_ERR_SIGPROCMASK); |
| 834 | |
| 835 | /* |
Brandon Williams | e3a4344 | 2017-04-19 16:13:20 -0700 | [diff] [blame] | 836 | * Attempt to exec using the command and arguments starting at |
| 837 | * argv.argv[1]. argv.argv[0] contains SHELL_PATH which will |
| 838 | * be used in the event exec failed with ENOEXEC at which point |
| 839 | * we will try to interpret the command using 'sh'. |
| 840 | */ |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 841 | execve(argv.v[1], (char *const *) argv.v + 1, |
Brandon Williams | ae25394 | 2017-04-19 16:13:22 -0700 | [diff] [blame] | 842 | (char *const *) childenv); |
Brandon Williams | e3a4344 | 2017-04-19 16:13:20 -0700 | [diff] [blame] | 843 | if (errno == ENOEXEC) |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 844 | execve(argv.v[0], (char *const *) argv.v, |
Brandon Williams | ae25394 | 2017-04-19 16:13:22 -0700 | [diff] [blame] | 845 | (char *const *) childenv); |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 846 | |
René Scharfe | 6d224ac | 2023-06-10 16:51:21 +0200 | [diff] [blame] | 847 | if (cmd->silent_exec_failure && errno == ENOENT) |
| 848 | child_die(CHILD_ERR_SILENT); |
| 849 | child_die(CHILD_ERR_ERRNO); |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 850 | } |
Eric Wong | 45afb1c | 2017-04-19 16:13:27 -0700 | [diff] [blame] | 851 | atfork_parent(&as); |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 852 | if (cmd->pid < 0) |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 853 | error_errno("cannot fork() for %s", cmd->args.v[0]); |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 854 | else if (cmd->clean_on_exit) |
Lars Schneider | ac2fbaa | 2016-10-16 16:20:28 -0700 | [diff] [blame] | 855 | mark_child_for_cleanup(cmd->pid, cmd); |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 856 | |
| 857 | /* |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 858 | * Wait for child's exec. If the exec succeeds (or if fork() |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 859 | * failed), EOF is seen immediately by the parent. Otherwise, the |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 860 | * child process sends a child_err struct. |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 861 | * Note that use of this infrastructure is completely advisory, |
| 862 | * therefore, we keep error checks minimal. |
| 863 | */ |
| 864 | close(notify_pipe[1]); |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 865 | if (xread(notify_pipe[0], &cerr, sizeof(cerr)) == sizeof(cerr)) { |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 866 | /* |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 867 | * At this point we know that fork() succeeded, but exec() |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 868 | * failed. Errors have been reported to our stderr. |
| 869 | */ |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 870 | wait_or_whine(cmd->pid, cmd->args.v[0], 0); |
Brandon Williams | 79319b1 | 2017-04-19 16:13:24 -0700 | [diff] [blame] | 871 | child_err_spew(cmd, &cerr); |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 872 | failed_errno = errno; |
| 873 | cmd->pid = -1; |
| 874 | } |
| 875 | close(notify_pipe[0]); |
Brandon Williams | 3967e25 | 2017-04-19 16:13:19 -0700 | [diff] [blame] | 876 | |
Brandon Williams | db015a2 | 2017-04-19 16:13:23 -0700 | [diff] [blame] | 877 | if (null_fd >= 0) |
| 878 | close(null_fd); |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 879 | strvec_clear(&argv); |
Brandon Williams | ae25394 | 2017-04-19 16:13:22 -0700 | [diff] [blame] | 880 | free(childenv); |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 881 | } |
Jeff King | 321fd82 | 2018-10-24 03:38:00 -0400 | [diff] [blame] | 882 | end_of_spawn: |
| 883 | |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 884 | #else |
Frank Li | 0d30ad7 | 2009-09-16 10:20:17 +0200 | [diff] [blame] | 885 | { |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 886 | int fhin = 0, fhout = 1, fherr = 2; |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 887 | const char **sargv = cmd->args.v; |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 888 | struct strvec nargv = STRVEC_INIT; |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 889 | |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 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); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 896 | |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 897 | if (cmd->no_stderr) |
| 898 | fherr = open("/dev/null", O_RDWR); |
| 899 | else if (need_err) |
| 900 | fherr = dup(fderr[1]); |
Junio C Hamano | 76d44c8 | 2010-02-05 21:08:53 -0800 | [diff] [blame] | 901 | else if (cmd->err > 2) |
| 902 | fherr = dup(cmd->err); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 903 | |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 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); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 912 | |
Felipe Contreras | 5a50085 | 2013-10-31 03:25:45 -0600 | [diff] [blame] | 913 | if (cmd->git_cmd) |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 914 | cmd->args.v = prepare_git_cmd(&nargv, sargv); |
Felipe Contreras | 5a50085 | 2013-10-31 03:25:45 -0600 | [diff] [blame] | 915 | else if (cmd->use_shell) |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 916 | cmd->args.v = prepare_shell_cmd(&nargv, sargv); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 917 | |
Ævar Arnfjörð Bjarmason | 29fda24 | 2022-06-02 11:09:50 +0200 | [diff] [blame] | 918 | cmd->pid = mingw_spawnvpe(cmd->args.v[0], cmd->args.v, |
| 919 | (char**) cmd->env.v, |
| 920 | cmd->dir, fhin, fhout, fherr); |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 921 | failed_errno = errno; |
Johannes Sixt | c024beb | 2009-07-04 21:26:42 +0200 | [diff] [blame] | 922 | if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT)) |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 923 | error_errno("cannot spawn %s", cmd->args.v[0]); |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 924 | if (cmd->clean_on_exit && cmd->pid >= 0) |
Lars Schneider | ac2fbaa | 2016-10-16 16:20:28 -0700 | [diff] [blame] | 925 | mark_child_for_cleanup(cmd->pid, cmd); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 926 | |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 927 | strvec_clear(&nargv); |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 928 | cmd->args.v = sargv; |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 929 | if (fhin != 0) |
| 930 | close(fhin); |
| 931 | if (fhout != 1) |
| 932 | close(fhout); |
| 933 | if (fherr != 2) |
| 934 | close(fherr); |
Frank Li | 0d30ad7 | 2009-09-16 10:20:17 +0200 | [diff] [blame] | 935 | } |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 936 | #endif |
| 937 | |
| 938 | if (cmd->pid < 0) { |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 939 | trace2_child_exit(cmd, -1); |
| 940 | |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 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); |
bert Dvornik | fc012c2 | 2010-05-20 20:57:52 +0200 | [diff] [blame] | 951 | else if (cmd->err) |
| 952 | close(cmd->err); |
René Scharfe | 2d71608 | 2015-10-24 14:11:27 +0200 | [diff] [blame] | 953 | child_process_clear(cmd); |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 954 | errno = failed_errno; |
| 955 | return -1; |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 956 | } |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 957 | |
| 958 | if (need_in) |
| 959 | close(fdin[0]); |
| 960 | else if (cmd->in) |
| 961 | close(cmd->in); |
| 962 | |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 963 | if (need_out) |
| 964 | close(fdout[1]); |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 965 | else if (cmd->out) |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 966 | close(cmd->out); |
| 967 | |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 968 | if (need_err) |
| 969 | close(fderr[1]); |
Shawn O. Pearce | 4f41b61 | 2010-02-05 12:57:37 -0800 | [diff] [blame] | 970 | else if (cmd->err) |
| 971 | close(cmd->err); |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 972 | |
Shawn O. Pearce | ebcb5d1 | 2007-03-10 03:28:05 -0500 | [diff] [blame] | 973 | return 0; |
| 974 | } |
| 975 | |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 976 | int finish_command(struct child_process *cmd) |
| 977 | { |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 978 | int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 0); |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 979 | trace2_child_exit(cmd, ret); |
René Scharfe | 2d71608 | 2015-10-24 14:11:27 +0200 | [diff] [blame] | 980 | child_process_clear(cmd); |
Johannes Schindelin | 0d58fef | 2021-02-02 22:09:52 +0100 | [diff] [blame] | 981 | invalidate_lstat_cache(); |
Jeff King | c460c0e | 2014-05-15 04:33:26 -0400 | [diff] [blame] | 982 | return ret; |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 983 | } |
| 984 | |
Takashi Iwai | 507d780 | 2015-09-04 11:35:57 +0200 | [diff] [blame] | 985 | int finish_command_in_signal(struct child_process *cmd) |
| 986 | { |
Ævar Arnfjörð Bjarmason | d3b2159 | 2021-11-25 23:52:22 +0100 | [diff] [blame] | 987 | int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 1); |
Josh Steadmon | ce3986b | 2022-06-07 11:21:57 -0700 | [diff] [blame] | 988 | if (ret != -1) |
| 989 | trace2_child_exit(cmd, ret); |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 990 | return ret; |
Takashi Iwai | 507d780 | 2015-09-04 11:35:57 +0200 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | |
Shawn O. Pearce | ebcb5d1 | 2007-03-10 03:28:05 -0500 | [diff] [blame] | 994 | int run_command(struct child_process *cmd) |
| 995 | { |
Jeff King | c29b396 | 2015-03-22 23:54:05 -0400 | [diff] [blame] | 996 | int code; |
| 997 | |
| 998 | if (cmd->out < 0 || cmd->err < 0) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 999 | BUG("run_command with a pipe can cause deadlock"); |
Jeff King | c29b396 | 2015-03-22 23:54:05 -0400 | [diff] [blame] | 1000 | |
| 1001 | code = start_command(cmd); |
Shawn O. Pearce | ebcb5d1 | 2007-03-10 03:28:05 -0500 | [diff] [blame] | 1002 | if (code) |
| 1003 | return code; |
| 1004 | return finish_command(cmd); |
| 1005 | } |
| 1006 | |
Johannes Sixt | f6b6098 | 2010-03-09 21:00:36 +0100 | [diff] [blame] | 1007 | #ifndef NO_PTHREADS |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 1008 | static pthread_t main_thread; |
| 1009 | static int main_thread_set; |
| 1010 | static pthread_key_t async_key; |
Jeff King | 1ece66b | 2013-04-16 15:50:07 -0400 | [diff] [blame] | 1011 | static pthread_key_t async_die_counter; |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 1012 | |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 1013 | static void *run_thread(void *data) |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 1014 | { |
| 1015 | struct async *async = data; |
Johannes Sixt | f6b6098 | 2010-03-09 21:00:36 +0100 | [diff] [blame] | 1016 | intptr_t ret; |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 1017 | |
Jeff King | c792d7b | 2016-04-19 18:49:41 -0400 | [diff] [blame] | 1018 | if (async->isolate_sigpipe) { |
| 1019 | sigset_t mask; |
| 1020 | sigemptyset(&mask); |
| 1021 | sigaddset(&mask, SIGPIPE); |
Seija | 786e676 | 2022-12-02 17:02:58 +0000 | [diff] [blame] | 1022 | if (pthread_sigmask(SIG_BLOCK, &mask, NULL)) { |
Jeff King | c792d7b | 2016-04-19 18:49:41 -0400 | [diff] [blame] | 1023 | ret = error("unable to block SIGPIPE in async thread"); |
| 1024 | return (void *)ret; |
| 1025 | } |
| 1026 | } |
| 1027 | |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 1028 | pthread_setspecific(async_key, async); |
Johannes Sixt | f6b6098 | 2010-03-09 21:00:36 +0100 | [diff] [blame] | 1029 | ret = async->proc(async->proc_in, async->proc_out, async->data); |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 1030 | return (void *)ret; |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 1031 | } |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 1032 | |
| 1033 | static NORETURN void die_async(const char *err, va_list params) |
| 1034 | { |
Ævar Arnfjörð Bjarmason | e081a7c | 2021-12-07 19:26:30 +0100 | [diff] [blame] | 1035 | report_fn die_message_fn = get_die_message_routine(); |
| 1036 | |
| 1037 | die_message_fn(err, params); |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 1038 | |
Jeff King | 661a8cf | 2015-09-01 16:22:43 -0400 | [diff] [blame] | 1039 | if (in_async()) { |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 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); |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 1049 | } |
Jeff King | 1ece66b | 2013-04-16 15:50:07 -0400 | [diff] [blame] | 1050 | |
| 1051 | static int async_die_is_recursing(void) |
| 1052 | { |
| 1053 | void *ret = pthread_getspecific(async_die_counter); |
Victoria Dye | 4b540cf | 2021-11-04 04:01:03 +0000 | [diff] [blame] | 1054 | pthread_setspecific(async_die_counter, &async_die_counter); /* set to any non-NULL valid pointer */ |
Jeff King | 1ece66b | 2013-04-16 15:50:07 -0400 | [diff] [blame] | 1055 | return ret != NULL; |
| 1056 | } |
| 1057 | |
Jeff King | 661a8cf | 2015-09-01 16:22:43 -0400 | [diff] [blame] | 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 | |
Lars Schneider | b992fe1 | 2016-10-16 16:20:27 -0700 | [diff] [blame] | 1065 | static void NORETURN async_exit(int code) |
Jeff King | 9658846 | 2016-02-24 02:40:16 -0500 | [diff] [blame] | 1066 | { |
| 1067 | pthread_exit((void *)(intptr_t)code); |
| 1068 | } |
| 1069 | |
Etienne Buira | 0f4b6db | 2014-10-18 14:31:15 +0200 | [diff] [blame] | 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 | |
René Scharfe | 6066a7e | 2014-11-10 22:17:00 +0100 | [diff] [blame] | 1080 | static void git_atexit_dispatch(void) |
Etienne Buira | 0f4b6db | 2014-10-18 14:31:15 +0200 | [diff] [blame] | 1081 | { |
| 1082 | size_t i; |
| 1083 | |
| 1084 | for (i=git_atexit_hdlrs.nr ; i ; i--) |
| 1085 | git_atexit_hdlrs.handlers[i-1](); |
| 1086 | } |
| 1087 | |
René Scharfe | 6066a7e | 2014-11-10 22:17:00 +0100 | [diff] [blame] | 1088 | static void git_atexit_clear(void) |
Etienne Buira | 0f4b6db | 2014-10-18 14:31:15 +0200 | [diff] [blame] | 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 | |
Jeff King | 661a8cf | 2015-09-01 16:22:43 -0400 | [diff] [blame] | 1109 | static int process_is_async; |
| 1110 | int in_async(void) |
| 1111 | { |
| 1112 | return process_is_async; |
| 1113 | } |
| 1114 | |
Lars Schneider | b992fe1 | 2016-10-16 16:20:27 -0700 | [diff] [blame] | 1115 | static void NORETURN async_exit(int code) |
Jeff King | 9658846 | 2016-02-24 02:40:16 -0500 | [diff] [blame] | 1116 | { |
| 1117 | exit(code); |
| 1118 | } |
| 1119 | |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 1120 | #endif |
| 1121 | |
Lars Schneider | b992fe1 | 2016-10-16 16:20:27 -0700 | [diff] [blame] | 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 | |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 1135 | int start_async(struct async *async) |
| 1136 | { |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 1137 | int need_in, need_out; |
| 1138 | int fdin[2], fdout[2]; |
| 1139 | int proc_in, proc_out; |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 1140 | |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 1141 | 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 Duy | fbcb0e0 | 2016-05-08 16:47:53 +0700 | [diff] [blame] | 1146 | return error_errno("cannot create pipe"); |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 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); |
Nguyễn Thái Ngọc Duy | fbcb0e0 | 2016-05-08 16:47:53 +0700 | [diff] [blame] | 1158 | return error_errno("cannot create pipe"); |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 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; |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 1176 | |
Johannes Sixt | f6b6098 | 2010-03-09 21:00:36 +0100 | [diff] [blame] | 1177 | #ifdef NO_PTHREADS |
Anders Melchiorsen | 2c3766f | 2008-08-04 02:30:03 +0200 | [diff] [blame] | 1178 | /* Flush stdio before fork() to avoid cloning buffers */ |
| 1179 | fflush(NULL); |
| 1180 | |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 1181 | async->pid = fork(); |
| 1182 | if (async->pid < 0) { |
Nguyễn Thái Ngọc Duy | fbcb0e0 | 2016-05-08 16:47:53 +0700 | [diff] [blame] | 1183 | error_errno("fork (async) failed"); |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 1184 | goto error; |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 1185 | } |
| 1186 | if (!async->pid) { |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 1187 | if (need_in) |
| 1188 | close(fdin[1]); |
| 1189 | if (need_out) |
| 1190 | close(fdout[0]); |
Etienne Buira | 0f4b6db | 2014-10-18 14:31:15 +0200 | [diff] [blame] | 1191 | git_atexit_clear(); |
Jeff King | 661a8cf | 2015-09-01 16:22:43 -0400 | [diff] [blame] | 1192 | process_is_async = 1; |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 1193 | exit(!!async->proc(proc_in, proc_out, async->data)); |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 1194 | } |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 1195 | |
Lars Schneider | ac2fbaa | 2016-10-16 16:20:28 -0700 | [diff] [blame] | 1196 | mark_child_for_cleanup(async->pid, NULL); |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 1197 | |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 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); |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 1207 | #else |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 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); |
Jeff King | 1ece66b | 2013-04-16 15:50:07 -0400 | [diff] [blame] | 1216 | pthread_key_create(&async_die_counter, NULL); |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 1217 | set_die_routine(die_async); |
Jeff King | 1ece66b | 2013-04-16 15:50:07 -0400 | [diff] [blame] | 1218 | set_die_is_recursing_routine(async_die_is_recursing); |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 1221 | if (proc_in >= 0) |
| 1222 | set_cloexec(proc_in); |
| 1223 | if (proc_out >= 0) |
| 1224 | set_cloexec(proc_out); |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 1225 | async->proc_in = proc_in; |
| 1226 | async->proc_out = proc_out; |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 1227 | { |
| 1228 | int err = pthread_create(&async->tid, NULL, run_thread, async); |
| 1229 | if (err) { |
Nguyễn Thái Ngọc Duy | 2179045 | 2018-11-03 09:48:50 +0100 | [diff] [blame] | 1230 | error(_("cannot create async thread: %s"), strerror(err)); |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 1231 | goto error; |
| 1232 | } |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 1233 | } |
| 1234 | #endif |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 1235 | return 0; |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 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; |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | int finish_async(struct async *async) |
| 1251 | { |
Johannes Sixt | f6b6098 | 2010-03-09 21:00:36 +0100 | [diff] [blame] | 1252 | #ifdef NO_PTHREADS |
Johannes Schindelin | 0d58fef | 2021-02-02 22:09:52 +0100 | [diff] [blame] | 1253 | int ret = wait_or_whine(async->pid, "child process", 0); |
| 1254 | |
| 1255 | invalidate_lstat_cache(); |
| 1256 | |
| 1257 | return ret; |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 1258 | #else |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 1259 | void *ret = (void *)(intptr_t)(-1); |
| 1260 | |
| 1261 | if (pthread_join(async->tid, &ret)) |
| 1262 | error("pthread_join failed"); |
Johannes Schindelin | 0d58fef | 2021-02-02 22:09:52 +0100 | [diff] [blame] | 1263 | invalidate_lstat_cache(); |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 1264 | return (int)(intptr_t)ret; |
Johannes Schindelin | 0d58fef | 2021-02-02 22:09:52 +0100 | [diff] [blame] | 1265 | |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 1266 | #endif |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 1267 | } |
Stephan Beyer | ae98a00 | 2009-01-16 20:09:59 +0100 | [diff] [blame] | 1268 | |
Nguyễn Thái Ngọc Duy | c0e40a2 | 2018-11-03 09:48:39 +0100 | [diff] [blame] | 1269 | int async_with_fork(void) |
| 1270 | { |
| 1271 | #ifdef NO_PTHREADS |
| 1272 | return 1; |
| 1273 | #else |
| 1274 | return 0; |
| 1275 | #endif |
| 1276 | } |
| 1277 | |
Jeff King | 96335bc | 2016-06-17 19:38:47 -0400 | [diff] [blame] | 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) |
Jeff King | 911ec99 | 2015-03-22 23:53:43 -0400 | [diff] [blame] | 1301 | { |
Jeff King | 96335bc | 2016-06-17 19:38:47 -0400 | [diff] [blame] | 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) { |
Jeff King | 14eab81 | 2022-08-17 02:08:06 -0400 | [diff] [blame] | 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); |
Jeff King | 96335bc | 2016-06-17 19:38:47 -0400 | [diff] [blame] | 1345 | if (len < 0) { |
Jeff King | c6d3cce | 2022-08-17 02:09:42 -0400 | [diff] [blame] | 1346 | if (errno != EINTR && errno != EAGAIN && |
| 1347 | errno != ENOSPC) { |
Jeff King | 14eab81 | 2022-08-17 02:08:06 -0400 | [diff] [blame] | 1348 | io->error = errno; |
| 1349 | close(io->fd); |
| 1350 | io->fd = -1; |
| 1351 | } |
Jeff King | 96335bc | 2016-06-17 19:38:47 -0400 | [diff] [blame] | 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 | |
Jeff King | 911ec99 | 2015-03-22 23:53:43 -0400 | [diff] [blame] | 1416 | if (start_command(cmd) < 0) |
| 1417 | return -1; |
| 1418 | |
Jeff King | 96335bc | 2016-06-17 19:38:47 -0400 | [diff] [blame] | 1419 | if (in) { |
Jeff King | 716c1f6 | 2022-08-17 02:10:22 -0400 | [diff] [blame] | 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 | } |
Jeff King | 96335bc | 2016-06-17 19:38:47 -0400 | [diff] [blame] | 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) { |
Jeff King | 911ec99 | 2015-03-22 23:53:43 -0400 | [diff] [blame] | 1451 | finish_command(cmd); /* throw away exit code */ |
| 1452 | return -1; |
| 1453 | } |
| 1454 | |
Jeff King | 911ec99 | 2015-03-22 23:53:43 -0400 | [diff] [blame] | 1455 | return finish_command(cmd); |
| 1456 | } |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1457 | |
| 1458 | enum child_state { |
| 1459 | GIT_CP_FREE, |
| 1460 | GIT_CP_WORKING, |
| 1461 | GIT_CP_WAIT_CLEANUP, |
| 1462 | }; |
| 1463 | |
| 1464 | struct parallel_processes { |
Ævar Arnfjörð Bjarmason | 6a48b42 | 2022-10-12 23:02:23 +0200 | [diff] [blame] | 1465 | size_t nr_processes; |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1466 | |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 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 | |
Ævar Arnfjörð Bjarmason | 6a48b42 | 2022-10-12 23:02:23 +0200 | [diff] [blame] | 1481 | size_t output_owner; |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1482 | struct strbuf buffered_output; /* of finished children */ |
| 1483 | }; |
| 1484 | |
Ævar Arnfjörð Bjarmason | 0b0ab95 | 2022-10-12 23:02:34 +0200 | [diff] [blame] | 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) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1493 | { |
Ævar Arnfjörð Bjarmason | 0b0ab95 | 2022-10-12 23:02:34 +0200 | [diff] [blame] | 1494 | for (size_t i = 0; i < opts->processes; i++) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1495 | if (pp->children[i].state == GIT_CP_WORKING) |
| 1496 | kill(pp->children[i].process.pid, signo); |
| 1497 | } |
| 1498 | |
Ævar Arnfjörð Bjarmason | 0b0ab95 | 2022-10-12 23:02:34 +0200 | [diff] [blame] | 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; |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1506 | |
| 1507 | static void handle_children_on_signal(int signo) |
| 1508 | { |
Ævar Arnfjörð Bjarmason | 0b0ab95 | 2022-10-12 23:02:34 +0200 | [diff] [blame] | 1509 | kill_children_signal(pp_for_signal, signo); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1510 | sigchain_pop(signo); |
| 1511 | raise(signo); |
| 1512 | } |
| 1513 | |
| 1514 | static void pp_init(struct parallel_processes *pp, |
Ævar Arnfjörð Bjarmason | 0b0ab95 | 2022-10-12 23:02:34 +0200 | [diff] [blame] | 1515 | const struct run_process_parallel_opts *opts, |
| 1516 | struct parallel_processes_for_signal *pp_sig) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1517 | { |
Ævar Arnfjörð Bjarmason | 6e5ba0b | 2022-10-12 23:02:26 +0200 | [diff] [blame] | 1518 | const size_t n = opts->processes; |
Ævar Arnfjörð Bjarmason | c333e6f | 2022-10-12 23:02:25 +0200 | [diff] [blame] | 1519 | |
Ævar Arnfjörð Bjarmason | 51243f9 | 2022-10-12 23:02:24 +0200 | [diff] [blame] | 1520 | if (!n) |
| 1521 | BUG("you must provide a non-zero number of processes!"); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1522 | |
Ævar Arnfjörð Bjarmason | 6a48b42 | 2022-10-12 23:02:23 +0200 | [diff] [blame] | 1523 | trace_printf("run_processes_parallel: preparing to run up to %"PRIuMAX" tasks", |
| 1524 | (uintmax_t)n); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1525 | |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1526 | if (!opts->get_next_task) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1527 | BUG("you need to specify a get_next_task function"); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1528 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 1529 | CALLOC_ARRAY(pp->children, n); |
Ævar Arnfjörð Bjarmason | 357f8e6 | 2022-10-12 23:02:30 +0200 | [diff] [blame] | 1530 | if (!opts->ungroup) |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1531 | CALLOC_ARRAY(pp->pfd, n); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1532 | |
Ævar Arnfjörð Bjarmason | 6a48b42 | 2022-10-12 23:02:23 +0200 | [diff] [blame] | 1533 | for (size_t i = 0; i < n; i++) { |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1534 | strbuf_init(&pp->children[i].err, 0); |
| 1535 | child_process_init(&pp->children[i].process); |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1536 | if (pp->pfd) { |
| 1537 | pp->pfd[i].events = POLLIN | POLLHUP; |
| 1538 | pp->pfd[i].fd = -1; |
| 1539 | } |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1540 | } |
| 1541 | |
Ævar Arnfjörð Bjarmason | 0b0ab95 | 2022-10-12 23:02:34 +0200 | [diff] [blame] | 1542 | pp_sig->pp = pp; |
| 1543 | pp_sig->opts = opts; |
| 1544 | pp_for_signal = pp_sig; |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1545 | sigchain_push_common(handle_children_on_signal); |
| 1546 | } |
| 1547 | |
Ævar Arnfjörð Bjarmason | d1610ee | 2022-10-12 23:02:33 +0200 | [diff] [blame] | 1548 | static void pp_cleanup(struct parallel_processes *pp, |
| 1549 | const struct run_process_parallel_opts *opts) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1550 | { |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1551 | trace_printf("run_processes_parallel: done"); |
Ævar Arnfjörð Bjarmason | d1610ee | 2022-10-12 23:02:33 +0200 | [diff] [blame] | 1552 | for (size_t i = 0; i < opts->processes; i++) { |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 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 | */ |
Stefan Beller | 2dac9b5 | 2016-02-29 18:07:15 -0800 | [diff] [blame] | 1564 | strbuf_write(&pp->buffered_output, stderr); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 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 | */ |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1577 | static int pp_start_one(struct parallel_processes *pp, |
| 1578 | const struct run_process_parallel_opts *opts) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1579 | { |
Ævar Arnfjörð Bjarmason | 6a48b42 | 2022-10-12 23:02:23 +0200 | [diff] [blame] | 1580 | size_t i; |
| 1581 | int code; |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1582 | |
Ævar Arnfjörð Bjarmason | 9f3df6c | 2022-10-12 23:02:32 +0200 | [diff] [blame] | 1583 | for (i = 0; i < opts->processes; i++) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1584 | if (pp->children[i].state == GIT_CP_FREE) |
| 1585 | break; |
Ævar Arnfjörð Bjarmason | 9f3df6c | 2022-10-12 23:02:32 +0200 | [diff] [blame] | 1586 | if (i == opts->processes) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1587 | BUG("bookkeeping is hard"); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1588 | |
Emily Shaffer | 5402673 | 2023-02-08 20:21:12 +0100 | [diff] [blame] | 1589 | /* |
| 1590 | * By default, do not inherit stdin from the parent process - otherwise, |
| 1591 | * all children would share stdin! Users may overwrite this to provide |
| 1592 | * something to the child's stdin by having their 'get_next_task' |
| 1593 | * callback assign 0 to .no_stdin and an appropriate integer to .in. |
| 1594 | */ |
| 1595 | pp->children[i].process.no_stdin = 1; |
| 1596 | |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1597 | code = opts->get_next_task(&pp->children[i].process, |
Ævar Arnfjörð Bjarmason | 357f8e6 | 2022-10-12 23:02:30 +0200 | [diff] [blame] | 1598 | opts->ungroup ? NULL : &pp->children[i].err, |
Ævar Arnfjörð Bjarmason | 2aa8d22 | 2022-10-12 23:02:31 +0200 | [diff] [blame] | 1599 | opts->data, |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1600 | &pp->children[i].data); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1601 | if (!code) { |
Ævar Arnfjörð Bjarmason | 357f8e6 | 2022-10-12 23:02:30 +0200 | [diff] [blame] | 1602 | if (!opts->ungroup) { |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1603 | strbuf_addbuf(&pp->buffered_output, &pp->children[i].err); |
| 1604 | strbuf_reset(&pp->children[i].err); |
| 1605 | } |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1606 | return 1; |
| 1607 | } |
Ævar Arnfjörð Bjarmason | 357f8e6 | 2022-10-12 23:02:30 +0200 | [diff] [blame] | 1608 | if (!opts->ungroup) { |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1609 | pp->children[i].process.err = -1; |
| 1610 | pp->children[i].process.stdout_to_stderr = 1; |
| 1611 | } |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1612 | |
| 1613 | if (start_command(&pp->children[i].process)) { |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1614 | if (opts->start_failure) |
Ævar Arnfjörð Bjarmason | 357f8e6 | 2022-10-12 23:02:30 +0200 | [diff] [blame] | 1615 | code = opts->start_failure(opts->ungroup ? NULL : |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1616 | &pp->children[i].err, |
Ævar Arnfjörð Bjarmason | 2aa8d22 | 2022-10-12 23:02:31 +0200 | [diff] [blame] | 1617 | opts->data, |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1618 | pp->children[i].data); |
| 1619 | else |
| 1620 | code = 0; |
| 1621 | |
Ævar Arnfjörð Bjarmason | 357f8e6 | 2022-10-12 23:02:30 +0200 | [diff] [blame] | 1622 | if (!opts->ungroup) { |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1623 | strbuf_addbuf(&pp->buffered_output, &pp->children[i].err); |
| 1624 | strbuf_reset(&pp->children[i].err); |
| 1625 | } |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1626 | if (code) |
| 1627 | pp->shutdown = 1; |
| 1628 | return code; |
| 1629 | } |
| 1630 | |
| 1631 | pp->nr_processes++; |
| 1632 | pp->children[i].state = GIT_CP_WORKING; |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1633 | if (pp->pfd) |
| 1634 | pp->pfd[i].fd = pp->children[i].process.err; |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1635 | return 0; |
| 1636 | } |
| 1637 | |
Ævar Arnfjörð Bjarmason | d1610ee | 2022-10-12 23:02:33 +0200 | [diff] [blame] | 1638 | static void pp_buffer_stderr(struct parallel_processes *pp, |
| 1639 | const struct run_process_parallel_opts *opts, |
| 1640 | int output_timeout) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1641 | { |
Ævar Arnfjörð Bjarmason | 5123e6e | 2023-02-08 20:21:11 +0100 | [diff] [blame] | 1642 | while (poll(pp->pfd, opts->processes, output_timeout) < 0) { |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1643 | if (errno == EINTR) |
| 1644 | continue; |
Ævar Arnfjörð Bjarmason | d1610ee | 2022-10-12 23:02:33 +0200 | [diff] [blame] | 1645 | pp_cleanup(pp, opts); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1646 | die_errno("poll"); |
| 1647 | } |
| 1648 | |
| 1649 | /* Buffer output from all pipes. */ |
Ævar Arnfjörð Bjarmason | d1610ee | 2022-10-12 23:02:33 +0200 | [diff] [blame] | 1650 | for (size_t i = 0; i < opts->processes; i++) { |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1651 | if (pp->children[i].state == GIT_CP_WORKING && |
| 1652 | pp->pfd[i].revents & (POLLIN | POLLHUP)) { |
| 1653 | int n = strbuf_read_once(&pp->children[i].err, |
| 1654 | pp->children[i].process.err, 0); |
| 1655 | if (n == 0) { |
| 1656 | close(pp->children[i].process.err); |
| 1657 | pp->children[i].state = GIT_CP_WAIT_CLEANUP; |
| 1658 | } else if (n < 0) |
| 1659 | if (errno != EAGAIN) |
| 1660 | die_errno("read"); |
| 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | |
Ævar Arnfjörð Bjarmason | e39c9de | 2022-10-12 23:02:28 +0200 | [diff] [blame] | 1665 | static void pp_output(const struct parallel_processes *pp) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1666 | { |
Ævar Arnfjörð Bjarmason | 6a48b42 | 2022-10-12 23:02:23 +0200 | [diff] [blame] | 1667 | size_t i = pp->output_owner; |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1668 | |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1669 | if (pp->children[i].state == GIT_CP_WORKING && |
| 1670 | pp->children[i].err.len) { |
Stefan Beller | 2dac9b5 | 2016-02-29 18:07:15 -0800 | [diff] [blame] | 1671 | strbuf_write(&pp->children[i].err, stderr); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1672 | strbuf_reset(&pp->children[i].err); |
| 1673 | } |
| 1674 | } |
| 1675 | |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1676 | static int pp_collect_finished(struct parallel_processes *pp, |
| 1677 | const struct run_process_parallel_opts *opts) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1678 | { |
Ævar Arnfjörð Bjarmason | 6a48b42 | 2022-10-12 23:02:23 +0200 | [diff] [blame] | 1679 | int code; |
Ævar Arnfjörð Bjarmason | 9f3df6c | 2022-10-12 23:02:32 +0200 | [diff] [blame] | 1680 | size_t i; |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1681 | int result = 0; |
| 1682 | |
| 1683 | while (pp->nr_processes > 0) { |
Ævar Arnfjörð Bjarmason | 9f3df6c | 2022-10-12 23:02:32 +0200 | [diff] [blame] | 1684 | for (i = 0; i < opts->processes; i++) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1685 | if (pp->children[i].state == GIT_CP_WAIT_CLEANUP) |
| 1686 | break; |
Ævar Arnfjörð Bjarmason | 9f3df6c | 2022-10-12 23:02:32 +0200 | [diff] [blame] | 1687 | if (i == opts->processes) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1688 | break; |
| 1689 | |
| 1690 | code = finish_command(&pp->children[i].process); |
| 1691 | |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1692 | if (opts->task_finished) |
Ævar Arnfjörð Bjarmason | 357f8e6 | 2022-10-12 23:02:30 +0200 | [diff] [blame] | 1693 | code = opts->task_finished(code, opts->ungroup ? NULL : |
Ævar Arnfjörð Bjarmason | 2aa8d22 | 2022-10-12 23:02:31 +0200 | [diff] [blame] | 1694 | &pp->children[i].err, opts->data, |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1695 | pp->children[i].data); |
| 1696 | else |
| 1697 | code = 0; |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1698 | |
| 1699 | if (code) |
| 1700 | result = code; |
| 1701 | if (code < 0) |
| 1702 | break; |
| 1703 | |
| 1704 | pp->nr_processes--; |
| 1705 | pp->children[i].state = GIT_CP_FREE; |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1706 | if (pp->pfd) |
| 1707 | pp->pfd[i].fd = -1; |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1708 | child_process_init(&pp->children[i].process); |
| 1709 | |
Ævar Arnfjörð Bjarmason | 357f8e6 | 2022-10-12 23:02:30 +0200 | [diff] [blame] | 1710 | if (opts->ungroup) { |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1711 | ; /* no strbuf_*() work to do here */ |
| 1712 | } else if (i != pp->output_owner) { |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1713 | strbuf_addbuf(&pp->buffered_output, &pp->children[i].err); |
| 1714 | strbuf_reset(&pp->children[i].err); |
| 1715 | } else { |
Ævar Arnfjörð Bjarmason | 9f3df6c | 2022-10-12 23:02:32 +0200 | [diff] [blame] | 1716 | const size_t n = opts->processes; |
| 1717 | |
Stefan Beller | 2dac9b5 | 2016-02-29 18:07:15 -0800 | [diff] [blame] | 1718 | strbuf_write(&pp->children[i].err, stderr); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1719 | strbuf_reset(&pp->children[i].err); |
| 1720 | |
| 1721 | /* Output all other finished child processes */ |
Stefan Beller | 2dac9b5 | 2016-02-29 18:07:15 -0800 | [diff] [blame] | 1722 | strbuf_write(&pp->buffered_output, stderr); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1723 | strbuf_reset(&pp->buffered_output); |
| 1724 | |
| 1725 | /* |
| 1726 | * Pick next process to output live. |
| 1727 | * NEEDSWORK: |
| 1728 | * For now we pick it randomly by doing a round |
| 1729 | * robin. Later we may want to pick the one with |
| 1730 | * the most output or the longest or shortest |
| 1731 | * running process time. |
| 1732 | */ |
| 1733 | for (i = 0; i < n; i++) |
| 1734 | if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING) |
| 1735 | break; |
| 1736 | pp->output_owner = (pp->output_owner + i) % n; |
| 1737 | } |
| 1738 | } |
| 1739 | return result; |
| 1740 | } |
| 1741 | |
Ævar Arnfjörð Bjarmason | 6e5ba0b | 2022-10-12 23:02:26 +0200 | [diff] [blame] | 1742 | void run_processes_parallel(const struct run_process_parallel_opts *opts) |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1743 | { |
| 1744 | int i, code; |
| 1745 | int output_timeout = 100; |
| 1746 | int spawn_cap = 4; |
Ævar Arnfjörð Bjarmason | 0b0ab95 | 2022-10-12 23:02:34 +0200 | [diff] [blame] | 1747 | struct parallel_processes_for_signal pp_sig; |
Ævar Arnfjörð Bjarmason | c333e6f | 2022-10-12 23:02:25 +0200 | [diff] [blame] | 1748 | struct parallel_processes pp = { |
Ævar Arnfjörð Bjarmason | c333e6f | 2022-10-12 23:02:25 +0200 | [diff] [blame] | 1749 | .buffered_output = STRBUF_INIT, |
Ævar Arnfjörð Bjarmason | c333e6f | 2022-10-12 23:02:25 +0200 | [diff] [blame] | 1750 | }; |
Ævar Arnfjörð Bjarmason | 6e5ba0b | 2022-10-12 23:02:26 +0200 | [diff] [blame] | 1751 | /* options */ |
| 1752 | const char *tr2_category = opts->tr2_category; |
| 1753 | const char *tr2_label = opts->tr2_label; |
| 1754 | const int do_trace2 = tr2_category && tr2_label; |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1755 | |
Ævar Arnfjörð Bjarmason | 6e5ba0b | 2022-10-12 23:02:26 +0200 | [diff] [blame] | 1756 | if (do_trace2) |
| 1757 | trace2_region_enter_printf(tr2_category, tr2_label, NULL, |
| 1758 | "max:%d", opts->processes); |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1759 | |
Ævar Arnfjörð Bjarmason | 0b0ab95 | 2022-10-12 23:02:34 +0200 | [diff] [blame] | 1760 | pp_init(&pp, opts, &pp_sig); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1761 | while (1) { |
| 1762 | for (i = 0; |
| 1763 | i < spawn_cap && !pp.shutdown && |
Ævar Arnfjörð Bjarmason | 9f3df6c | 2022-10-12 23:02:32 +0200 | [diff] [blame] | 1764 | pp.nr_processes < opts->processes; |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1765 | i++) { |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1766 | code = pp_start_one(&pp, opts); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1767 | if (!code) |
| 1768 | continue; |
| 1769 | if (code < 0) { |
| 1770 | pp.shutdown = 1; |
Ævar Arnfjörð Bjarmason | 0b0ab95 | 2022-10-12 23:02:34 +0200 | [diff] [blame] | 1771 | kill_children(&pp, opts, -code); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1772 | } |
| 1773 | break; |
| 1774 | } |
| 1775 | if (!pp.nr_processes) |
| 1776 | break; |
Ævar Arnfjörð Bjarmason | 6e5ba0b | 2022-10-12 23:02:26 +0200 | [diff] [blame] | 1777 | if (opts->ungroup) { |
Ævar Arnfjörð Bjarmason | 9f3df6c | 2022-10-12 23:02:32 +0200 | [diff] [blame] | 1778 | for (size_t i = 0; i < opts->processes; i++) |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1779 | pp.children[i].state = GIT_CP_WAIT_CLEANUP; |
| 1780 | } else { |
Ævar Arnfjörð Bjarmason | d1610ee | 2022-10-12 23:02:33 +0200 | [diff] [blame] | 1781 | pp_buffer_stderr(&pp, opts, output_timeout); |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 1782 | pp_output(&pp); |
| 1783 | } |
Ævar Arnfjörð Bjarmason | fa93951 | 2022-10-12 23:02:29 +0200 | [diff] [blame] | 1784 | code = pp_collect_finished(&pp, opts); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1785 | if (code) { |
| 1786 | pp.shutdown = 1; |
| 1787 | if (code < 0) |
Ævar Arnfjörð Bjarmason | 0b0ab95 | 2022-10-12 23:02:34 +0200 | [diff] [blame] | 1788 | kill_children(&pp, opts,-code); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1789 | } |
| 1790 | } |
| 1791 | |
Ævar Arnfjörð Bjarmason | d1610ee | 2022-10-12 23:02:33 +0200 | [diff] [blame] | 1792 | pp_cleanup(&pp, opts); |
Ævar Arnfjörð Bjarmason | 6e5ba0b | 2022-10-12 23:02:26 +0200 | [diff] [blame] | 1793 | |
| 1794 | if (do_trace2) |
| 1795 | trace2_region_leave(tr2_category, tr2_label, NULL); |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 1796 | } |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 1797 | |
Derrick Stolee | a95ce12 | 2020-09-17 18:11:44 +0000 | [diff] [blame] | 1798 | int run_auto_maintenance(int quiet) |
Junio C Hamano | 850b6ed | 2020-05-06 13:18:29 -0700 | [diff] [blame] | 1799 | { |
Derrick Stolee | 1942d48 | 2020-08-28 15:45:12 +0000 | [diff] [blame] | 1800 | int enabled; |
Derrick Stolee | a95ce12 | 2020-09-17 18:11:44 +0000 | [diff] [blame] | 1801 | struct child_process maint = CHILD_PROCESS_INIT; |
Junio C Hamano | 850b6ed | 2020-05-06 13:18:29 -0700 | [diff] [blame] | 1802 | |
Derrick Stolee | 1942d48 | 2020-08-28 15:45:12 +0000 | [diff] [blame] | 1803 | if (!git_config_get_bool("maintenance.auto", &enabled) && |
| 1804 | !enabled) |
| 1805 | return 0; |
| 1806 | |
Derrick Stolee | a95ce12 | 2020-09-17 18:11:44 +0000 | [diff] [blame] | 1807 | maint.git_cmd = 1; |
Johannes Schindelin | 5a22a33 | 2021-09-09 09:47:07 +0000 | [diff] [blame] | 1808 | maint.close_object_store = 1; |
Derrick Stolee | a95ce12 | 2020-09-17 18:11:44 +0000 | [diff] [blame] | 1809 | strvec_pushl(&maint.args, "maintenance", "run", "--auto", NULL); |
| 1810 | strvec_push(&maint.args, quiet ? "--quiet" : "--no-quiet"); |
| 1811 | |
| 1812 | return run_command(&maint); |
Junio C Hamano | 850b6ed | 2020-05-06 13:18:29 -0700 | [diff] [blame] | 1813 | } |
Jonathan Tan | d1fa943 | 2021-06-17 10:13:25 -0700 | [diff] [blame] | 1814 | |
Ævar Arnfjörð Bjarmason | b319325 | 2022-06-02 11:09:51 +0200 | [diff] [blame] | 1815 | void prepare_other_repo_env(struct strvec *env, const char *new_git_dir) |
Jonathan Tan | d1fa943 | 2021-06-17 10:13:25 -0700 | [diff] [blame] | 1816 | { |
| 1817 | const char * const *var; |
| 1818 | |
| 1819 | for (var = local_repo_env; *var; var++) { |
| 1820 | if (strcmp(*var, CONFIG_DATA_ENVIRONMENT) && |
| 1821 | strcmp(*var, CONFIG_COUNT_ENVIRONMENT)) |
Ævar Arnfjörð Bjarmason | b319325 | 2022-06-02 11:09:51 +0200 | [diff] [blame] | 1822 | strvec_push(env, *var); |
Jonathan Tan | d1fa943 | 2021-06-17 10:13:25 -0700 | [diff] [blame] | 1823 | } |
Ævar Arnfjörð Bjarmason | b319325 | 2022-06-02 11:09:51 +0200 | [diff] [blame] | 1824 | strvec_pushf(env, "%s=%s", GIT_DIR_ENVIRONMENT, new_git_dir); |
Jonathan Tan | d1fa943 | 2021-06-17 10:13:25 -0700 | [diff] [blame] | 1825 | } |
Jeff Hostetler | fdb1322 | 2021-09-20 15:36:17 +0000 | [diff] [blame] | 1826 | |
| 1827 | enum start_bg_result start_bg_command(struct child_process *cmd, |
| 1828 | start_bg_wait_cb *wait_cb, |
| 1829 | void *cb_data, |
| 1830 | unsigned int timeout_sec) |
| 1831 | { |
| 1832 | enum start_bg_result sbgr = SBGR_ERROR; |
| 1833 | int ret; |
| 1834 | int wait_status; |
| 1835 | pid_t pid_seen; |
| 1836 | time_t time_limit; |
| 1837 | |
| 1838 | /* |
| 1839 | * We do not allow clean-on-exit because the child process |
| 1840 | * should persist in the background and possibly/probably |
| 1841 | * after this process exits. So we don't want to kill the |
| 1842 | * child during our atexit routine. |
| 1843 | */ |
| 1844 | if (cmd->clean_on_exit) |
| 1845 | BUG("start_bg_command() does not allow non-zero clean_on_exit"); |
| 1846 | |
| 1847 | if (!cmd->trace2_child_class) |
| 1848 | cmd->trace2_child_class = "background"; |
| 1849 | |
| 1850 | ret = start_command(cmd); |
| 1851 | if (ret) { |
| 1852 | /* |
| 1853 | * We assume that if `start_command()` fails, we |
| 1854 | * either get a complete `trace2_child_start() / |
| 1855 | * trace2_child_exit()` pair or it fails before the |
| 1856 | * `trace2_child_start()` is emitted, so we do not |
| 1857 | * need to worry about it here. |
| 1858 | * |
| 1859 | * We also assume that `start_command()` does not add |
| 1860 | * us to the cleanup list. And that it calls |
Andrei Rybak | b39a841 | 2023-01-07 14:56:55 +0100 | [diff] [blame] | 1861 | * `child_process_clear()`. |
Jeff Hostetler | fdb1322 | 2021-09-20 15:36:17 +0000 | [diff] [blame] | 1862 | */ |
| 1863 | sbgr = SBGR_ERROR; |
| 1864 | goto done; |
| 1865 | } |
| 1866 | |
| 1867 | time(&time_limit); |
| 1868 | time_limit += timeout_sec; |
| 1869 | |
| 1870 | wait: |
| 1871 | pid_seen = waitpid(cmd->pid, &wait_status, WNOHANG); |
| 1872 | |
| 1873 | if (!pid_seen) { |
| 1874 | /* |
| 1875 | * The child is currently running. Ask the callback |
| 1876 | * if the child is ready to do work or whether we |
| 1877 | * should keep waiting for it to boot up. |
| 1878 | */ |
| 1879 | ret = (*wait_cb)(cmd, cb_data); |
| 1880 | if (!ret) { |
| 1881 | /* |
| 1882 | * The child is running and "ready". |
| 1883 | */ |
| 1884 | trace2_child_ready(cmd, "ready"); |
| 1885 | sbgr = SBGR_READY; |
| 1886 | goto done; |
| 1887 | } else if (ret > 0) { |
| 1888 | /* |
| 1889 | * The callback said to give it more time to boot up |
| 1890 | * (subject to our timeout limit). |
| 1891 | */ |
| 1892 | time_t now; |
| 1893 | |
| 1894 | time(&now); |
| 1895 | if (now < time_limit) |
| 1896 | goto wait; |
| 1897 | |
| 1898 | /* |
| 1899 | * Our timeout has expired. We don't try to |
| 1900 | * kill the child, but rather let it continue |
| 1901 | * (hopefully) trying to startup. |
| 1902 | */ |
| 1903 | trace2_child_ready(cmd, "timeout"); |
| 1904 | sbgr = SBGR_TIMEOUT; |
| 1905 | goto done; |
| 1906 | } else { |
| 1907 | /* |
| 1908 | * The cb gave up on this child. It is still running, |
| 1909 | * but our cb got an error trying to probe it. |
| 1910 | */ |
| 1911 | trace2_child_ready(cmd, "error"); |
| 1912 | sbgr = SBGR_CB_ERROR; |
| 1913 | goto done; |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | else if (pid_seen == cmd->pid) { |
| 1918 | int child_code = -1; |
| 1919 | |
| 1920 | /* |
| 1921 | * The child started, but exited or was terminated |
| 1922 | * before becoming "ready". |
| 1923 | * |
| 1924 | * We try to match the behavior of `wait_or_whine()` |
| 1925 | * WRT the handling of WIFSIGNALED() and WIFEXITED() |
| 1926 | * and convert the child's status to a return code for |
| 1927 | * tracing purposes and emit the `trace2_child_exit()` |
| 1928 | * event. |
| 1929 | * |
| 1930 | * We do not want the wait_or_whine() error message |
| 1931 | * because we will be called by client-side library |
| 1932 | * routines. |
| 1933 | */ |
| 1934 | if (WIFEXITED(wait_status)) |
| 1935 | child_code = WEXITSTATUS(wait_status); |
| 1936 | else if (WIFSIGNALED(wait_status)) |
| 1937 | child_code = WTERMSIG(wait_status) + 128; |
| 1938 | trace2_child_exit(cmd, child_code); |
| 1939 | |
| 1940 | sbgr = SBGR_DIED; |
| 1941 | goto done; |
| 1942 | } |
| 1943 | |
| 1944 | else if (pid_seen < 0 && errno == EINTR) |
| 1945 | goto wait; |
| 1946 | |
| 1947 | trace2_child_exit(cmd, -1); |
| 1948 | sbgr = SBGR_ERROR; |
| 1949 | |
| 1950 | done: |
| 1951 | child_process_clear(cmd); |
| 1952 | invalidate_lstat_cache(); |
| 1953 | return sbgr; |
| 1954 | } |