| 948 | } |
| 949 | |
| 950 | static int cbm_subprocess_spawn_win(cbm_subprocess_t *process) { |
| 951 | char cmdline[8192]; |
| 952 | bool built = |
| 953 | process->windows_cmd_payload |
| 954 | ? cbm_build_win_cmd_payload(cmdline, sizeof(cmdline), process->bin, |
| 955 | process->windows_cmd_payload) |
| 956 | : cbm_build_win_cmdline(cmdline, sizeof(cmdline), (const char *const *)process->argv); |
| 957 | if (!built) { |
| 958 | return -1; |
| 959 | } |
| 960 | /* Everything a failure must release, so every failure leaves through |
| 961 | * the one exit at the end. */ |
| 962 | HANDLE job = NULL; |
| 963 | HANDLE nul = INVALID_HANDLE_VALUE; |
| 964 | HANDLE input = INVALID_HANDLE_VALUE; |
| 965 | HANDLE log = INVALID_HANDLE_VALUE; |
| 966 | HANDLE stdout_capture = INVALID_HANDLE_VALUE; |
| 967 | LPPROC_THREAD_ATTRIBUTE_LIST attrs = NULL; |
| 968 | bool attrs_init = false; |
| 969 | wchar_t *wbin = cbm_utf8_to_wide(process->bin); |
| 970 | wchar_t *wcmdline = cbm_utf8_to_wide(cmdline); |
| 971 | if (!wbin || !wcmdline) { |
| 972 | goto fail; |
| 973 | } |
| 974 | |
| 975 | JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits; |
| 976 | ZeroMemory(&limits, sizeof(limits)); |
| 977 | limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; |
| 978 | if (process->memory_limit_bytes > 0) { |
| 979 | limits.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_JOB_MEMORY; |
| 980 | limits.JobMemoryLimit = (SIZE_T)process->memory_limit_bytes; |
| 981 | } |
| 982 | job = CreateJobObjectW(NULL, NULL); |
| 983 | if (!job || |
| 984 | !SetInformationJobObject(job, JobObjectExtendedLimitInformation, &limits, sizeof(limits))) { |
| 985 | goto fail; |
| 986 | } |
| 987 | |
| 988 | SECURITY_ATTRIBUTES security; |
| 989 | ZeroMemory(&security, sizeof(security)); |
| 990 | security.nLength = sizeof(security); |
| 991 | security.bInheritHandle = TRUE; |
| 992 | nul = CreateFileW(L"NUL", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 993 | &security, OPEN_EXISTING, 0, NULL); |
| 994 | if (nul == INVALID_HANDLE_VALUE) { |
| 995 | goto fail; |
| 996 | } |
| 997 | if (process->stdin_file) { |
| 998 | wchar_t *winput = cbm_win_stdin_path(process); |
| 999 | /* Validate before any output can be truncated; recheck the opened |
| 1000 | * handle rather than relying only on path attributes. */ |
| 1001 | DWORD attributes = winput ? GetFileAttributesW(winput) : INVALID_FILE_ATTRIBUTES; |
| 1002 | if (attributes != INVALID_FILE_ATTRIBUTES && |
| 1003 | !(attributes & |
| 1004 | (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DEVICE))) { |
| 1005 | input = CreateFileW(winput, GENERIC_READ, FILE_SHARE_READ, &security, OPEN_EXISTING, |
| 1006 | FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 1007 | } |
no test coverage detected