`/bin/sh -c cmd` with stdout on a pipe, stdin on /dev/null (never the MCP * transport), stderr inherited like popen(), and `envp` as the environment. */
| 907 | /* `/bin/sh -c cmd` with stdout on a pipe, stdin on /dev/null (never the MCP |
| 908 | * transport), stderr inherited like popen(), and `envp` as the environment. */ |
| 909 | static pid_t cbm_popen_spawn_sh(const char *cmd, char **envp, int *read_fd) { |
| 910 | int fds[2]; |
| 911 | if (pipe(fds) != 0) { |
| 912 | return -1; |
| 913 | } |
| 914 | if (!cbm_fd_set_cloexec(fds[0]) || !cbm_fd_set_cloexec(fds[1])) { |
| 915 | (void)close(fds[0]); |
| 916 | (void)close(fds[1]); |
| 917 | return -1; |
| 918 | } |
| 919 | posix_spawn_file_actions_t actions; |
| 920 | if (posix_spawn_file_actions_init(&actions) != 0) { |
| 921 | (void)close(fds[0]); |
| 922 | (void)close(fds[1]); |
| 923 | return -1; |
| 924 | } |
| 925 | /* dup2 clears close-on-exec on the child's stdout; both pipe ends |
| 926 | * themselves stay close-on-exec, so no other child ever holds them. */ |
| 927 | bool configured = |
| 928 | posix_spawn_file_actions_addopen(&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0) == 0 && |
| 929 | posix_spawn_file_actions_adddup2(&actions, fds[1], STDOUT_FILENO) == 0; |
| 930 | char *const argv[] = {(char *)"sh", (char *)"-c", (char *)cmd, NULL}; |
| 931 | pid_t pid = -1; |
| 932 | int rc = configured ? posix_spawn(&pid, "/bin/sh", &actions, NULL, argv, envp) : -1; |
| 933 | (void)posix_spawn_file_actions_destroy(&actions); |
| 934 | (void)close(fds[1]); |
| 935 | if (rc != 0 || pid <= 0) { |
| 936 | (void)close(fds[0]); |
| 937 | return -1; |
| 938 | } |
| 939 | *read_fd = fds[0]; |
| 940 | return pid; |
| 941 | } |
| 942 | |
| 943 | static int cbm_popen_git_wait(pid_t pid) { |
| 944 | int status = 0; |
no test coverage detected