From 00e9e69c2a198974d992d8bcbb97866f10da4e9f Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 25 Sep 2022 15:39:55 +0200 Subject: [PATCH] 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. --- app/src/sys/unix/process.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/app/src/sys/unix/process.c b/app/src/sys/unix/process.c index cef227ed..8c4a53c3 100644 --- a/app/src/sys/unix/process.c +++ b/app/src/sys/unix/process.c @@ -92,8 +92,14 @@ sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags, close(in[0]); } 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 (out[1] != STDOUT_FILENO) { @@ -102,8 +108,12 @@ sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags, } close(out[0]); } else if (!inherit_stdout) { - // Close stdout in the child process - close(STDOUT_FILENO); + int devnull = open("/dev/null", O_WRONLY | O_CREAT, 0666); + if (devnull != -1) { + dup2(devnull, STDOUT_FILENO); + } else { + LOGE("Could not open /dev/null for stdout"); + } } if (perr) { @@ -113,8 +123,12 @@ sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags, } close(err[0]); } else if (!inherit_stderr) { - // Close stderr in the child process - close(STDERR_FILENO); + int devnull = open("/dev/null", O_WRONLY | O_CREAT, 0666); + if (devnull != -1) { + dup2(devnull, STDERR_FILENO); + } else { + LOGE("Could not open /dev/null for stderr"); + } } close(internal[0]);