From 9ba1b9946963d7470ceb25e0d051efda1f996302 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 12 Jun 2023 05:20:55 +0000 Subject: [PATCH] Add missing redirection If calling dlog_connect_fd() is failed, the launchpad should try to redirect fd to null node. Change-Id: I8bb2a2d2a585b7c0e1eff9e3ba958801a5bf49e6 Signed-off-by: Hwankyu Jhun --- src/lib/launchpad-common/stdio.cc | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/lib/launchpad-common/stdio.cc b/src/lib/launchpad-common/stdio.cc index 6d3d805..c701f18 100644 --- a/src/lib/launchpad-common/stdio.cc +++ b/src/lib/launchpad-common/stdio.cc @@ -16,10 +16,13 @@ #include "launchpad-common/stdio.hh" -#include #include -#include #include +#include +#include +#include +#include +#include #include #include "launchpad-common/log_private.hh" @@ -27,15 +30,31 @@ namespace launchpad { namespace { +void RedirectToNullNode(int new_fd, int flags) { + int fd = open("/dev/null", flags); + if (fd < 0) { + _E("open() is failed. errno(%d)", errno); + return; + } + + if (dup2(fd, new_fd) < 0) + _E("dup2(%d, %d) is failed. errno(%d)", fd, new_fd, errno); + + close(fd); +} + void Redirect(int fd, const char* ident, int priority) { int ret = dlog_connect_fd(LOG_ID_APPS, fd, ident, priority); - if (ret != 0) + if (ret != 0) { _E("dlog_connect_fd() is failed. error(%d)", ret); + RedirectToNullNode(fd, O_WRONLY | O_NOCTTY); + } } } // namespace void Stdio::Setup() { + RedirectToNullNode(STDIN_FILENO, O_RDONLY | O_NOCTTY); Redirect(STDOUT_FILENO, "STDOUT", DLOG_WARN); Redirect(STDERR_FILENO, "STDERR", DLOG_ERROR); } -- 2.7.4