Expose flags for process execution
Let the caller decide if stdout and stderr must be inherited on process creation, i.e. if stdout and stderr of the child process should be printed in the scrcpy console. This allows to get output and errors for specific adb commands depending on the context. PR #2827 <https://github.com/Genymobile/scrcpy/pull/2827>
This commit is contained in:
@@ -11,8 +11,11 @@
|
||||
#include "util/log.h"
|
||||
|
||||
enum sc_process_result
|
||||
sc_process_execute_p(const char *const argv[], sc_pid *pid,
|
||||
sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags,
|
||||
int *pin, int *pout, int *perr) {
|
||||
bool inherit_stdout = !pout && !(flags & SC_PROCESS_NO_STDOUT);
|
||||
bool inherit_stderr = !perr && !(flags & SC_PROCESS_NO_STDERR);
|
||||
|
||||
int in[2];
|
||||
int out[2];
|
||||
int err[2];
|
||||
@@ -90,20 +93,30 @@ sc_process_execute_p(const char *const argv[], sc_pid *pid,
|
||||
}
|
||||
close(in[1]);
|
||||
}
|
||||
// Do not close stdin in the child process, this makes adb fail on Linux
|
||||
|
||||
if (pout) {
|
||||
if (out[1] != STDOUT_FILENO) {
|
||||
dup2(out[1], STDOUT_FILENO);
|
||||
close(out[1]);
|
||||
}
|
||||
close(out[0]);
|
||||
} else if (!inherit_stdout) {
|
||||
// Close stdout in the child process
|
||||
close(STDOUT_FILENO);
|
||||
}
|
||||
|
||||
if (perr) {
|
||||
if (err[1] != STDERR_FILENO) {
|
||||
dup2(err[1], STDERR_FILENO);
|
||||
close(err[1]);
|
||||
}
|
||||
close(err[0]);
|
||||
} else if (!inherit_stderr) {
|
||||
// Close stderr in the child process
|
||||
close(STDERR_FILENO);
|
||||
}
|
||||
|
||||
close(internal[0]);
|
||||
enum sc_process_result err;
|
||||
if (fcntl(internal[1], F_SETFD, FD_CLOEXEC) == 0) {
|
||||
|
||||
@@ -24,12 +24,17 @@ build_cmd(char *cmd, size_t len, const char *const argv[]) {
|
||||
}
|
||||
|
||||
enum sc_process_result
|
||||
sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
||||
sc_process_execute_p(const char *const argv[], HANDLE *handle, unsigned flags,
|
||||
HANDLE *pin, HANDLE *pout, HANDLE *perr) {
|
||||
enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;
|
||||
bool inherit_stdout = !pout && !(flags & SC_PROCESS_NO_STDOUT);
|
||||
bool inherit_stderr = !perr && !(flags & SC_PROCESS_NO_STDERR);
|
||||
|
||||
// Add 1 per non-NULL pointer
|
||||
unsigned handle_count = !!pin + !!pout + !!perr;
|
||||
unsigned handle_count = !!pin
|
||||
+ (pout || inherit_stdout)
|
||||
+ (perr || inherit_stderr);
|
||||
|
||||
enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;
|
||||
|
||||
SECURITY_ATTRIBUTES sa;
|
||||
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||
@@ -85,12 +90,14 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
||||
si.StartupInfo.hStdInput = stdin_read_handle;
|
||||
handles[i++] = si.StartupInfo.hStdInput;
|
||||
}
|
||||
if (pout) {
|
||||
si.StartupInfo.hStdOutput = stdout_write_handle;
|
||||
if (pout || inherit_stdout) {
|
||||
si.StartupInfo.hStdOutput = pout ? stdout_write_handle
|
||||
: GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
handles[i++] = si.StartupInfo.hStdOutput;
|
||||
}
|
||||
if (perr) {
|
||||
si.StartupInfo.hStdError = stderr_write_handle;
|
||||
if (perr || inherit_stderr) {
|
||||
si.StartupInfo.hStdError = perr ? stderr_write_handle
|
||||
: GetStdHandle(STD_ERROR_HANDLE);
|
||||
handles[i++] = si.StartupInfo.hStdError;
|
||||
}
|
||||
|
||||
@@ -140,7 +147,9 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
||||
}
|
||||
|
||||
BOOL bInheritHandles = handle_count > 0;
|
||||
DWORD dwCreationFlags = handle_count > 0 ? EXTENDED_STARTUPINFO_PRESENT : 0;
|
||||
// DETACHED_PROCESS to disable stdin, stdout and stderr
|
||||
DWORD dwCreationFlags = handle_count > 0 ? EXTENDED_STARTUPINFO_PRESENT
|
||||
: DETACHED_PROCESS;
|
||||
BOOL ok = CreateProcessW(NULL, wide, NULL, NULL, bInheritHandles,
|
||||
dwCreationFlags, NULL, NULL, &si.StartupInfo, &pi);
|
||||
free(wide);
|
||||
|
||||
Reference in New Issue
Block a user