MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_popen_spawn_sh

Function cbm_popen_spawn_sh

src/foundation/compat_fs.c:909–941  ·  view source on GitHub ↗

`/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. */

Source from the content-addressed store, hash-verified

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. */
909static 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
943static int cbm_popen_git_wait(pid_t pid) {
944 int status = 0;

Callers 1

cbm_popen_gitFunction · 0.85

Calls 2

cbm_fd_set_cloexecFunction · 0.85
closeFunction · 0.50

Tested by

no test coverage detected