Skip to content

Commit

Permalink
Fix process execution on Windows 7
Browse files Browse the repository at this point in the history
According to this bug report on Firefox:
<https://bugzilla.mozilla.org/show_bug.cgi?id=1460995>

> CreateProcess fails with ERROR_NO_SYSTEM_RESOURCES on Windows 7. It
> looks like the reason why is because PROC_THREAD_ATTRIBUTE_HANDLE_LIST
> doesn't like console handles.

To avoid the problem, do not pass console handles to
PROC_THREAD_ATTRIBUTE_HANDLE_LIST.

Refs #2783 <#2783>
Refs f801d8b
Fixes #2838 <#2838>
  • Loading branch information
rom1v committed Nov 30, 2021
1 parent 86c91e1 commit 5cbdd56
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions app/src/sys/win/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle, unsigned flags,
bool inherit_stderr = !perr && !(flags & SC_PROCESS_NO_STDERR);

// Add 1 per non-NULL pointer
unsigned handle_count = !!pin
+ (pout || inherit_stdout)
+ (perr || inherit_stderr);
unsigned handle_count = !!pin || !!pout || !!perr;

enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;

Expand Down Expand Up @@ -81,23 +79,29 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle, unsigned flags,
si.StartupInfo.cb = sizeof(si);
HANDLE handles[3];

si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
if (inherit_stdout) {
si.StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
}
if (inherit_stderr) {
si.StartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
}

LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL;
if (handle_count) {
si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;

unsigned i = 0;
if (pin) {
si.StartupInfo.hStdInput = stdin_read_handle;
handles[i++] = si.StartupInfo.hStdInput;
}
if (pout || inherit_stdout) {
si.StartupInfo.hStdOutput = pout ? stdout_write_handle
: GetStdHandle(STD_OUTPUT_HANDLE);
if (pout) {
assert(!inherit_stdout);
si.StartupInfo.hStdOutput = stdout_write_handle;
handles[i++] = si.StartupInfo.hStdOutput;
}
if (perr || inherit_stderr) {
si.StartupInfo.hStdError = perr ? stderr_write_handle
: GetStdHandle(STD_ERROR_HANDLE);
if (perr) {
assert(!inherit_stderr);
si.StartupInfo.hStdError = stderr_write_handle;
handles[i++] = si.StartupInfo.hStdError;
}

Expand Down Expand Up @@ -146,10 +150,15 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle, unsigned flags,
goto error_free_attribute_list;
}

BOOL bInheritHandles = handle_count > 0;
// DETACHED_PROCESS to disable stdin, stdout and stderr
DWORD dwCreationFlags = handle_count > 0 ? EXTENDED_STARTUPINFO_PRESENT
: DETACHED_PROCESS;
BOOL bInheritHandles = handle_count > 0 || inherit_stdout || inherit_stderr;
DWORD dwCreationFlags = 0;
if (handle_count > 0) {
dwCreationFlags |= EXTENDED_STARTUPINFO_PRESENT;
}
if (!inherit_stdout && !inherit_stderr) {
// DETACHED_PROCESS to disable stdin, stdout and stderr
dwCreationFlags |= DETACHED_PROCESS;
}
BOOL ok = CreateProcessW(NULL, wide, NULL, NULL, bInheritHandles,
dwCreationFlags, NULL, NULL, &si.StartupInfo, &pi);
free(wide);
Expand Down

0 comments on commit 5cbdd56

Please sign in to comment.