Add missing redirection 45/294045/1
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 12 Jun 2023 05:20:55 +0000 (05:20 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 12 Jun 2023 05:20:55 +0000 (05:20 +0000)
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 <h.jhun@samsung.com>
src/lib/launchpad-common/stdio.cc

index 6d3d805..c701f18 100644 (file)
 
 #include "launchpad-common/stdio.hh"
 
-#include <stdio.h>
 #include <stdbool.h>
-#include <dlog.h>
 #include <dlog-redirect-stdout.h>
+#include <dlog.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
 
 #include "launchpad-common/log_private.hh"
 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);
 }