|
7 | 7 | #include "foundation/constants.h" |
8 | 8 | #include "foundation/compat_fs.h" |
9 | 9 | #include "foundation/compat_fs_internal.h" |
| 10 | +#include "foundation/git_env.h" |
10 | 11 |
|
11 | 12 | #include <stdio.h> |
12 | 13 | #include <stdlib.h> |
@@ -236,7 +237,9 @@ static wchar_t *cbm_resolve_comspec(void) { |
236 | 237 |
|
237 | 238 | /* On failure returns NULL with *stage naming the failing step and *gle the |
238 | 239 | * GetLastError value captured at that step (0 when errno is the signal). */ |
239 | | -static FILE *cbm_popen_isolated(const char *cmd, const char **stage, DWORD *gle) { |
| 240 | +/* env: a CREATE_UNICODE_ENVIRONMENT block for the child, or NULL to inherit. */ |
| 241 | +static FILE *cbm_popen_isolated(const char *cmd, const wchar_t *env, const char **stage, |
| 242 | + DWORD *gle) { |
240 | 243 | *stage = ""; |
241 | 244 | *gle = 0; |
242 | 245 | InitOnceExecuteOnce(&g_popen_once, cbm_popen_init, NULL, NULL); |
@@ -315,8 +318,11 @@ static FILE *cbm_popen_isolated(const char *cmd, const char **stage, DWORD *gle) |
315 | 318 | *stage = "cmdline"; |
316 | 319 | *gle = ERROR_NOT_ENOUGH_MEMORY; |
317 | 320 | } else { |
318 | | - created = CreateProcessW(app, wcmdline, NULL, NULL, TRUE, |
319 | | - EXTENDED_STARTUPINFO_PRESENT | CREATE_NO_WINDOW, NULL, NULL, |
| 321 | + DWORD flags = EXTENDED_STARTUPINFO_PRESENT | CREATE_NO_WINDOW; |
| 322 | + if (env) { |
| 323 | + flags |= CREATE_UNICODE_ENVIRONMENT; |
| 324 | + } |
| 325 | + created = CreateProcessW(app, wcmdline, NULL, NULL, TRUE, flags, (LPVOID)env, NULL, |
320 | 326 | &si.StartupInfo, &pi); |
321 | 327 | if (!created) { |
322 | 328 | *stage = "spawn"; |
@@ -372,29 +378,46 @@ static FILE *cbm_popen_isolated(const char *cmd, const char **stage, DWORD *gle) |
372 | 378 | return NULL; |
373 | 379 | } |
374 | 380 |
|
| 381 | +static FILE *cbm_popen_read_isolated(const char *cmd, const wchar_t *env) { |
| 382 | + const char *stage = ""; |
| 383 | + DWORD gle = 0; |
| 384 | + FILE *fp = cbm_popen_isolated(cmd, env, &stage, &gle); |
| 385 | + g_popen_last_isolated = (fp != NULL); |
| 386 | + if (!fp) { |
| 387 | + char glebuf[CBM_SZ_16]; |
| 388 | + char errnobuf[CBM_SZ_16]; |
| 389 | + snprintf(glebuf, sizeof(glebuf), "%lu", (unsigned long)gle); |
| 390 | + snprintf(errnobuf, sizeof(errnobuf), "%d", errno); |
| 391 | + cbm_log_warn("compat.popen_isolated_failed", "stage", stage, "gle", glebuf, "errno", |
| 392 | + errnobuf); |
| 393 | + } |
| 394 | + return fp; |
| 395 | +} |
| 396 | + |
375 | 397 | FILE *cbm_popen(const char *cmd, const char *mode) { |
376 | 398 | /* Our git shell-outs are all read-mode; they MUST use the isolated |
377 | 399 | * spawn. On failure, log and fail the call — never fall back to |
378 | 400 | * _popen, whose full handle inheritance re-arms the UI hang (#798). */ |
379 | 401 | if (mode && mode[0] == 'r' && mode[1] == '\0') { |
380 | | - const char *stage = ""; |
381 | | - DWORD gle = 0; |
382 | | - FILE *fp = cbm_popen_isolated(cmd, &stage, &gle); |
383 | | - g_popen_last_isolated = (fp != NULL); |
384 | | - if (!fp) { |
385 | | - char glebuf[CBM_SZ_16]; |
386 | | - char errnobuf[CBM_SZ_16]; |
387 | | - snprintf(glebuf, sizeof(glebuf), "%lu", (unsigned long)gle); |
388 | | - snprintf(errnobuf, sizeof(errnobuf), "%d", errno); |
389 | | - cbm_log_warn("compat.popen_isolated_failed", "stage", stage, "gle", glebuf, "errno", |
390 | | - errnobuf); |
391 | | - } |
392 | | - return fp; |
| 402 | + return cbm_popen_read_isolated(cmd, NULL); |
393 | 403 | } |
394 | 404 | g_popen_last_isolated = 0; |
395 | 405 | return _popen(cmd, mode); |
396 | 406 | } |
397 | 407 |
|
| 408 | +FILE *cbm_popen_git(const char *cmd) { |
| 409 | + /* Fail closed: without the scrubbed block the child would inherit a |
| 410 | + * caller's GIT_DIR and read the wrong repository (#2003). */ |
| 411 | + wchar_t *env = cbm_git_child_env_block(); |
| 412 | + if (!env) { |
| 413 | + cbm_log_warn("compat.popen_git_env_failed", "stage", "env_block"); |
| 414 | + return NULL; |
| 415 | + } |
| 416 | + FILE *fp = cbm_popen_read_isolated(cmd, env); |
| 417 | + cbm_git_child_env_free(env); /* CreateProcessW copies the block into the child */ |
| 418 | + return fp; |
| 419 | +} |
| 420 | + |
398 | 421 | int cbm_pclose(FILE *f) { |
399 | 422 | InitOnceExecuteOnce(&g_popen_once, cbm_popen_init, NULL, NULL); |
400 | 423 |
|
@@ -759,6 +782,8 @@ int cbm_exec_no_shell(const char *const *argv) { |
759 | 782 | #include <dirent.h> |
760 | 783 | #include <errno.h> |
761 | 784 | #include <fcntl.h> |
| 785 | +#include <pthread.h> |
| 786 | +#include <spawn.h> |
762 | 787 | #include <sys/file.h> |
763 | 788 | #include <sys/stat.h> |
764 | 789 | #include <sys/wait.h> |
@@ -863,8 +888,119 @@ FILE *cbm_popen(const char *cmd, const char *mode) { |
863 | 888 | return popen(cmd, mode); |
864 | 889 | } |
865 | 890 |
|
| 891 | +/* Streams opened by cbm_popen_git: popen() cannot take an environment, so |
| 892 | + * those children are spawned here and reaped by cbm_pclose via this list. */ |
| 893 | +typedef struct cbm_popen_git_entry { |
| 894 | + FILE *fp; |
| 895 | + pid_t pid; |
| 896 | + struct cbm_popen_git_entry *next; |
| 897 | +} cbm_popen_git_entry_t; |
| 898 | + |
| 899 | +static cbm_popen_git_entry_t *g_popen_git_list = NULL; |
| 900 | +static pthread_mutex_t g_popen_git_lock = PTHREAD_MUTEX_INITIALIZER; |
| 901 | + |
| 902 | +static bool cbm_fd_set_cloexec(int fd) { |
| 903 | + int flags = fcntl(fd, F_GETFD); |
| 904 | + return flags >= 0 && fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == 0; |
| 905 | +} |
| 906 | + |
| 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; |
| 945 | + while (waitpid(pid, &status, 0) < 0) { |
| 946 | + if (errno != EINTR) { |
| 947 | + return -1; |
| 948 | + } |
| 949 | + } |
| 950 | + return status; |
| 951 | +} |
| 952 | + |
| 953 | +FILE *cbm_popen_git(const char *cmd) { |
| 954 | + if (!cmd) { |
| 955 | + return NULL; |
| 956 | + } |
| 957 | + /* Fail closed: without the scrubbed environment the child would inherit |
| 958 | + * a caller's GIT_DIR and read the wrong repository (#2003). */ |
| 959 | + char **envp = cbm_git_child_envp(); |
| 960 | + if (!envp) { |
| 961 | + return NULL; |
| 962 | + } |
| 963 | + int read_fd = -1; |
| 964 | + pid_t pid = cbm_popen_spawn_sh(cmd, envp, &read_fd); |
| 965 | + cbm_git_child_env_free(envp); /* posix_spawn copied it into the child */ |
| 966 | + if (pid < 0) { |
| 967 | + return NULL; |
| 968 | + } |
| 969 | + cbm_popen_git_entry_t *entry = (cbm_popen_git_entry_t *)malloc(sizeof(*entry)); |
| 970 | + FILE *fp = entry ? fdopen(read_fd, "r") : NULL; |
| 971 | + if (!fp) { |
| 972 | + free(entry); |
| 973 | + (void)close(read_fd); |
| 974 | + (void)cbm_popen_git_wait(pid); |
| 975 | + return NULL; |
| 976 | + } |
| 977 | + entry->fp = fp; |
| 978 | + entry->pid = pid; |
| 979 | + pthread_mutex_lock(&g_popen_git_lock); |
| 980 | + entry->next = g_popen_git_list; |
| 981 | + g_popen_git_list = entry; |
| 982 | + pthread_mutex_unlock(&g_popen_git_lock); |
| 983 | + return fp; |
| 984 | +} |
| 985 | + |
866 | 986 | int cbm_pclose(FILE *f) { |
867 | | - return pclose(f); |
| 987 | + cbm_popen_git_entry_t *found = NULL; |
| 988 | + pthread_mutex_lock(&g_popen_git_lock); |
| 989 | + for (cbm_popen_git_entry_t **link = &g_popen_git_list; *link; link = &(*link)->next) { |
| 990 | + if ((*link)->fp == f) { |
| 991 | + found = *link; |
| 992 | + *link = found->next; |
| 993 | + break; |
| 994 | + } |
| 995 | + } |
| 996 | + pthread_mutex_unlock(&g_popen_git_lock); |
| 997 | + if (!found) { |
| 998 | + return pclose(f); /* opened by popen() */ |
| 999 | + } |
| 1000 | + pid_t pid = found->pid; |
| 1001 | + free(found); |
| 1002 | + (void)fclose(f); |
| 1003 | + return cbm_popen_git_wait(pid); /* the raw wait status, as pclose() */ |
868 | 1004 | } |
869 | 1005 |
|
870 | 1006 | FILE *cbm_fopen(const char *path, const char *mode) { |
|
0 commit comments