From 144e919e73306f3f79aa3ffc2012f5cb3112d9be Mon Sep 17 00:00:00 2001 From: Woongsuk Cho Date: Mon, 16 Jul 2018 20:09:44 +0900 Subject: [PATCH] redirect stderr/stdout to dlog before app launching Change-Id: I97b08fbc844c23a61e7d3509c3679f9ad8239835 --- NativeLauncher/inc/utils.h | 1 + NativeLauncher/launcher/dotnet/dotnet_launcher.cc | 5 +++ NativeLauncher/util/utils.cc | 40 +++++++++++++++-------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/NativeLauncher/inc/utils.h b/NativeLauncher/inc/utils.h index ac3816e..d2293ac 100644 --- a/NativeLauncher/inc/utils.h +++ b/NativeLauncher/inc/utils.h @@ -44,5 +44,6 @@ std::string joinStrings(const std::vector& strings, const char* con typedef std::function FileReader; void scanFilesInDir(const char* directory, FileReader reader, unsigned int depth); +int redirectFD(); int runLoggingThread(); #endif /* __UTILS_H__ */ diff --git a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc index ba9de8a..a2b42ca 100644 --- a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc +++ b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc @@ -333,6 +333,11 @@ int CoreRuntime::launch(const char* appId, const char* root, const char* path, i return 1; } + if (redirectFD() < 0) { + _ERR("Fail to redirect FD"); + return 1; + } + if (pluginSetAppInfo) pluginSetAppInfo(appId, path); diff --git a/NativeLauncher/util/utils.cc b/NativeLauncher/util/utils.cc index dfb4cc8..b902783 100644 --- a/NativeLauncher/util/utils.cc +++ b/NativeLauncher/util/utils.cc @@ -322,36 +322,50 @@ static void *stdlog(void*) return 0; } +int redirectFD() +{ + if (__pfd[1] < 0) { + _DBG("fail to create pipe for logging"); + return -1; + } + // stdout + if (dup2(__pfd[1], 1) == -1) { + _DBG("fail to duplicate fd to stdout"); + return -1; + } + + // stderr + if (dup2(__pfd[1], 2) == -1) { + _DBG("fail to duplicate fd to stderr"); + return-1; + } + + return 0; +} + int runLoggingThread() { if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) { _DBG("fail to make stdout line-buffered"); - return 0; + return -1; } if (setvbuf(stderr, NULL, _IONBF, 0) < 0) { _DBG("make stderr unbuffered"); - return 0; + return -1; } /* create the pipe and redirect stdout and stderr */ if (pipe(__pfd) < 0) { _DBG("fail to create pipe for logging"); - return 0; - } - - if (dup2(__pfd[1], fileno(stdout)) == -1) { - _DBG("fail to duplicate fd to stdout"); - return 0; + return -1; } - if (dup2(__pfd[1], fileno(stderr)) == -1) { - _DBG("fail to duplicate fd to stderr"); - return 0; + if (redirectFD() < 0) { + _DBG("fail to redirect FD"); + return -1; } - close(__pfd[1]); - /* spawn the logging thread */ if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) { _DBG("fail to create pthread"); -- 2.7.4