Fix process execution on Windows 7

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 <https://github.com/Genymobile/scrcpy/pull/2783>
Refs f801d8b312
Fixes #2838 <https://github.com/Genymobile/scrcpy/issues/2838>
PR #2840 <https://github.com/Genymobile/scrcpy/pull/2840>
This commit is contained in:
Romain Vimont 2021-11-30 12:22:12 +01:00
parent 86c91e183d
commit 64a04b8d4a

View file

@ -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); bool inherit_stderr = !perr && !(flags & SC_PROCESS_NO_STDERR);
// Add 1 per non-NULL pointer // Add 1 per non-NULL pointer
unsigned handle_count = !!pin unsigned handle_count = !!pin || !!pout || !!perr;
+ (pout || inherit_stdout)
+ (perr || inherit_stderr);
enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC; enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;
@ -81,23 +79,29 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle, unsigned flags,
si.StartupInfo.cb = sizeof(si); si.StartupInfo.cb = sizeof(si);
HANDLE handles[3]; 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; LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL;
if (handle_count) { if (handle_count) {
si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
unsigned i = 0; unsigned i = 0;
if (pin) { if (pin) {
si.StartupInfo.hStdInput = stdin_read_handle; si.StartupInfo.hStdInput = stdin_read_handle;
handles[i++] = si.StartupInfo.hStdInput; handles[i++] = si.StartupInfo.hStdInput;
} }
if (pout || inherit_stdout) { if (pout) {
si.StartupInfo.hStdOutput = pout ? stdout_write_handle assert(!inherit_stdout);
: GetStdHandle(STD_OUTPUT_HANDLE); si.StartupInfo.hStdOutput = stdout_write_handle;
handles[i++] = si.StartupInfo.hStdOutput; handles[i++] = si.StartupInfo.hStdOutput;
} }
if (perr || inherit_stderr) { if (perr) {
si.StartupInfo.hStdError = perr ? stderr_write_handle assert(!inherit_stderr);
: GetStdHandle(STD_ERROR_HANDLE); si.StartupInfo.hStdError = stderr_write_handle;
handles[i++] = si.StartupInfo.hStdError; handles[i++] = si.StartupInfo.hStdError;
} }
@ -146,10 +150,15 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle, unsigned flags,
goto error_free_attribute_list; goto error_free_attribute_list;
} }
BOOL bInheritHandles = handle_count > 0; 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 // DETACHED_PROCESS to disable stdin, stdout and stderr
DWORD dwCreationFlags = handle_count > 0 ? EXTENDED_STARTUPINFO_PRESENT dwCreationFlags |= DETACHED_PROCESS;
: DETACHED_PROCESS; }
BOOL ok = CreateProcessW(NULL, wide, NULL, NULL, bInheritHandles, BOOL ok = CreateProcessW(NULL, wide, NULL, NULL, bInheritHandles,
dwCreationFlags, NULL, NULL, &si.StartupInfo, &pi); dwCreationFlags, NULL, NULL, &si.StartupInfo, &pi);
free(wide); free(wide);