From f016b99e0897074e59c88ac0d473e6c4692b4b20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20BOULMIER?= Date: Wed, 6 May 2020 22:28:05 +0200 Subject: [PATCH] Redirect process output to dev null --- include/utils/process.hpp | 2 ++ src/utils/command.cpp | 1 + src/utils/process.cpp | 19 +++++++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/utils/process.hpp b/include/utils/process.hpp index 8210ac49..c5d91c01 100644 --- a/include/utils/process.hpp +++ b/include/utils/process.hpp @@ -8,6 +8,8 @@ namespace process_util { bool in_parent_process(pid_t pid); bool in_forked_process(pid_t pid); + void redirect_process_output_to_dev_null(); + void exec(char* cmd, char** args); void exec_sh(const char* cmd); diff --git a/src/utils/command.cpp b/src/utils/command.cpp index 1d08ae4b..ef44c0c5 100644 --- a/src/utils/command.cpp +++ b/src/utils/command.cpp @@ -37,6 +37,7 @@ int command::exec(bool wait_for_completion) { if (process_util::in_forked_process(m_forkpid)) { setpgid(m_forkpid, 0); + process_util::redirect_process_output_to_dev_null(); process_util::exec_sh(m_cmd.c_str()); } else { if (wait_for_completion) { diff --git a/src/utils/process.cpp b/src/utils/process.cpp index b11541de..79b1bce0 100644 --- a/src/utils/process.cpp +++ b/src/utils/process.cpp @@ -1,9 +1,11 @@ +#include "utils/process.hpp" + +#include #include #include #include "errors.hpp" #include "utils/env.hpp" -#include "utils/process.hpp" #include "utils/string.hpp" POLYBAR_NS @@ -23,6 +25,19 @@ namespace process_util { return pid == 0; } + void redirect_process_output_to_dev_null() { + auto redirect = [](int fd_to_redirect) { + int fd = open("/dev/null", O_WRONLY); + if (fd < 0 || dup2(fd, fd_to_redirect) < 0) { + throw system_error("Failed to redirect process output"); + } + close(fd); + }; + + redirect(STDOUT_FILENO); + redirect(STDERR_FILENO); + } + /** * Execute command */ @@ -105,6 +120,6 @@ namespace process_util { bool notify_childprocess() { return wait_for_completion_nohang() > 0; } -} +} // namespace process_util POLYBAR_NS_END