From 1a09eaea16b76ca953793e77a3a0f1b18f647720 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 15 Dec 2016 16:02:42 +0900 Subject: Redirect std fds before exec stdin -> /dev/null stdout -> journal fd or /dev/null stderr -> journal fd or /dev/null Change-Id: I7346df00a668c2cb08098a93ad15db97a1e8d2f7 Signed-off-by: Hwankyu Jhun --- CMakeLists.txt | 1 + inc/launchpad_common.h | 1 + src/launchpad.c | 3 +++ src/launchpad_common.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/launchpad_lib.c | 2 ++ 5 files changed, 60 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2989bb8..15735c7 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ PKG_CHECK_MODULES(${this_target_loader} REQUIRED aul vconf buxton2 + libsystemd-daemon ) FOREACH(flag ${${this_target_loader}_CFLAGS}) diff --git a/inc/launchpad_common.h b/inc/launchpad_common.h index 8016469..63a9de3 100644 --- a/inc/launchpad_common.h +++ b/inc/launchpad_common.h @@ -106,6 +106,7 @@ char *_appinfo_get_app_path(appinfo_t *menu_info); int _proc_get_attr_by_pid(int pid, char *buf, int size); int _close_all_fds(void); void _get_cpu_idle(long long *total, long long *idle); +int _setup_stdio(const char *ident); #endif /* __LAUNCHPAD_COMMON_H__ */ diff --git a/src/launchpad.c b/src/launchpad.c index 5305dc6..0ccd9f9 100755 --- a/src/launchpad.c +++ b/src/launchpad.c @@ -34,6 +34,7 @@ #include #include #include +#include #include "perf.h" #include "launchpad_common.h" @@ -387,6 +388,7 @@ static int __exec_loader_process(void *arg) _signal_fini(); _close_all_fds(); + _setup_stdio(basename(argv[LOADER_ARG_PATH])); if (execv(argv[LOADER_ARG_PATH], argv) < 0) _E("Failed to prepare candidate_process"); @@ -507,6 +509,7 @@ static int __normal_fork_exec(int argc, char **argv, const char *app_path) } _close_all_fds(); + _setup_stdio(basename(argv[LOADER_ARG_PATH])); if (execv(argv[LOADER_ARG_PATH], argv) < 0) { /* Flawfinder: ignore */ if (errno == EACCES) { _E("such a file is no executable - %s", diff --git a/src/launchpad_common.c b/src/launchpad_common.c index ddb4432..a0f6c3b 100644 --- a/src/launchpad_common.c +++ b/src/launchpad_common.c @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include "launchpad_common.h" #include "key.h" @@ -44,6 +46,7 @@ #define CONNECT_RETRY_COUNT 3 #define AUL_PKT_HEADER_SIZE (sizeof(int) + sizeof(int) + sizeof(int)) #define PATH_AMD_SOCK "/run/aul/daemons/.amd-sock" +#define PATH_DEV_NULL "/dev/null" static int __read_proc(const char *path, char *buf, int size) { @@ -809,3 +812,53 @@ int _close_all_fds(void) return 0; } +int _setup_stdio(const char *ident) +{ + int fd; + + /* stdin */ + fd = open(PATH_DEV_NULL, O_RDONLY | O_NOCTTY); + if (fd < 0) { + _W("Failed to open /dev/null - err(%d)", errno); + return -1; + } + if (dup2(fd, STDIN_FILENO) < 0) { + _W("Failed to duplicate fd - oldfd(%d), newfd(%d)", + fd, STDIN_FILENO); + } + close(fd); + + /* stdout */ + fd = sd_journal_stream_fd(ident, LOG_INFO, false); + if (fd < 0) { + _W("Failed to connect journal socket - err(%d)", errno); + fd = open(PATH_DEV_NULL, O_WRONLY | O_NOCTTY); + if (fd < 0) { + _W("Failed to open /dev/null - err(%d)", errno); + return -1; + } + } + if (dup2(fd, STDOUT_FILENO) < 0) { + _W("Failed to duplicate fd - oldfd(%d), newfd(%d)", + fd, STDOUT_FILENO); + } + close(fd); + + /* stderr */ + fd = sd_journal_stream_fd(ident, LOG_INFO, false); + if (fd < 0) { + _W("Failed to connect journal socket - err(%d)", errno); + fd = open(PATH_DEV_NULL, O_WRONLY | O_NOCTTY); + if (fd < 0) { + _W("Failed to open /dev/null - err(%d)", errno); + return -1; + } + } + if (dup2(fd, STDERR_FILENO) < 0) { + _W("Failed to duplicate fd - oldfd(%d), newfd(%d)", + fd, STDERR_FILENO); + } + close(fd); + + return 0; +} diff --git a/src/launchpad_lib.c b/src/launchpad_lib.c index 6336a19..17553ba 100644 --- a/src/launchpad_lib.c +++ b/src/launchpad_lib.c @@ -77,6 +77,8 @@ static int __prepare_exec(const char *appid, const char *app_path, return -1; } + _setup_stdio(basename(app_path)); + ret = buxton_open(&bxt_cli, NULL, NULL); if (ret != 0) { _E("buxton_open() failed"); -- cgit v1.2.3