Use /dev/null instead of closing fds

Some adb commands do not like when stdin, stdout or stderr are closed
(they hang forever). Open /dev/null for each.
This commit is contained in:
Romain Vimont 2022-09-25 15:39:55 +02:00
parent 4a5cdcd390
commit 00e9e69c2a

View file

@ -92,8 +92,14 @@ sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags,
close(in[0]); close(in[0]);
} }
close(in[1]); close(in[1]);
} else {
int devnull = open("/dev/null", O_RDONLY | O_CREAT, 0666);
if (devnull != -1) {
dup2(devnull, STDIN_FILENO);
} else {
LOGE("Could not open /dev/null for stdin");
}
} }
// Do not close stdin in the child process, this makes adb fail on Linux
if (pout) { if (pout) {
if (out[1] != STDOUT_FILENO) { if (out[1] != STDOUT_FILENO) {
@ -102,8 +108,12 @@ sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags,
} }
close(out[0]); close(out[0]);
} else if (!inherit_stdout) { } else if (!inherit_stdout) {
// Close stdout in the child process int devnull = open("/dev/null", O_WRONLY | O_CREAT, 0666);
close(STDOUT_FILENO); if (devnull != -1) {
dup2(devnull, STDOUT_FILENO);
} else {
LOGE("Could not open /dev/null for stdout");
}
} }
if (perr) { if (perr) {
@ -113,8 +123,12 @@ sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags,
} }
close(err[0]); close(err[0]);
} else if (!inherit_stderr) { } else if (!inherit_stderr) {
// Close stderr in the child process int devnull = open("/dev/null", O_WRONLY | O_CREAT, 0666);
close(STDERR_FILENO); if (devnull != -1) {
dup2(devnull, STDERR_FILENO);
} else {
LOGE("Could not open /dev/null for stderr");
}
} }
close(internal[0]); close(internal[0]);