#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);
}