fork+exec child setup. On Apple this runs ONLY for the exec-failure * fallback (see cbm_posix_spawn_apple), which preserves the documented * "bogus binary => child exits 127" contract across platforms. */
| 1467 | * fallback (see cbm_posix_spawn_apple), which preserves the documented |
| 1468 | * "bogus binary => child exits 127" contract across platforms. */ |
| 1469 | static void cbm_posix_child_exec(cbm_subprocess_t *process, int input, int output, int error, |
| 1470 | long max_fd) { |
| 1471 | if (setpgid(0, 0) < 0) { |
| 1472 | _exit(127); |
| 1473 | } |
| 1474 | cbm_posix_reset_child_signals(); |
| 1475 | |
| 1476 | /* Never let a worker consume the MCP transport inherited as stdin. Only |
| 1477 | * async-signal-safe calls are used between fork and exec. */ |
| 1478 | if (input < 0 || output < 0 || error < 0 || dup2(input, STDIN_FILENO) < 0 || |
| 1479 | dup2(output, STDOUT_FILENO) < 0 || dup2(error, STDERR_FILENO) < 0) { |
| 1480 | _exit(127); |
| 1481 | } |
| 1482 | if (input > STDERR_FILENO) { |
| 1483 | (void)close(input); |
| 1484 | } |
| 1485 | if (output > STDERR_FILENO) { |
| 1486 | (void)close(output); |
| 1487 | } |
| 1488 | (void)cbm_posix_close_fds_from(STDERR_FILENO + 1, max_fd); |
| 1489 | if (process->envp) { |
| 1490 | environ = process->envp; /* execvp passes environ to the new image */ |
| 1491 | } |
| 1492 | /* A fixed literal tool name (for example "git" or "curl") uses the |
| 1493 | * caller's normal PATH without introducing a shell. An explicit path |
| 1494 | * still has execvp's exact-path semantics because it contains '/'. */ |
| 1495 | if (process->environment) { |
| 1496 | #ifdef __APPLE__ |
| 1497 | /* Only this child changes environ. execvP keeps the caller's PATH for |
| 1498 | * executable lookup, including execvp's ENOEXEC shell fallback. */ |
| 1499 | environ = process->environment; |
| 1500 | execvP(process->bin, process->search_path, process->argv); |
| 1501 | #else |
| 1502 | /* execvpe searches the inherited caller PATH while passing the owned |
| 1503 | * environment to the executed image; the parent's environ is untouched. */ |
| 1504 | execvpe(process->bin, process->argv, process->environment); |
| 1505 | #endif |
| 1506 | } else { |
| 1507 | execvp(process->bin, process->argv); |
| 1508 | } |
| 1509 | _exit(127); |
| 1510 | } |
| 1511 | |
| 1512 | static int cbm_posix_fd_at_least_three(int fd) { |
| 1513 | if (fd < 0 || fd > STDERR_FILENO) { |
no test coverage detected