Redirect STDERR to /dev/null in process spawn

This commit is contained in:
Robbert van der Helm
2022-04-11 14:42:18 +02:00
parent 006cc6f52a
commit b80a30ba2a
2 changed files with 7 additions and 4 deletions
+2
View File
@@ -127,6 +127,8 @@ Process::StringResult Process::spawn_get_stdout_line() {
posix_spawn_file_actions_t actions; posix_spawn_file_actions_t actions;
posix_spawn_file_actions_init(&actions); posix_spawn_file_actions_init(&actions);
posix_spawn_file_actions_adddup2(&actions, output_pipe[1], STDOUT_FILENO); posix_spawn_file_actions_adddup2(&actions, output_pipe[1], STDOUT_FILENO);
posix_spawn_file_actions_addopen(&actions, STDERR_FILENO, "/dev/null",
O_WRONLY | O_APPEND, 0);
posix_spawn_file_actions_addclose(&actions, output_pipe[0]); posix_spawn_file_actions_addclose(&actions, output_pipe[0]);
posix_spawn_file_actions_addclose(&actions, output_pipe[1]); posix_spawn_file_actions_addclose(&actions, output_pipe[1]);
+5 -4
View File
@@ -158,10 +158,11 @@ class Process {
inline void environment(ProcessEnvironment env) { env_ = std::move(env); } inline void environment(ProcessEnvironment env) { env_ = std::move(env); }
/** /**
* Spawn the process, leave STDIN and STDERR alone, and return the first * Spawn the process, leave STDIN, redirect STDERR to `/dev/null`, and
* line (without the trailing linefeed) of STDOUT. The first output line * return the first line (without the trailing linefeed) of STDOUT. The
* will still be returned even if the process exits with a non-zero exit * first output line will still be returned even if the process exits with a
* code. Uses `posix_spawn()`, leaves file descriptors in tact. * non-zero exit code. Uses `posix_spawn()`, leaves file descriptors in
* tact.
*/ */
StringResult spawn_get_stdout_line(); StringResult spawn_get_stdout_line();