Redirect std fds before exec 38/105038/10
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 15 Dec 2016 07:02:42 +0000 (16:02 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 21 Dec 2016 01:14:23 +0000 (10:14 +0900)
stdin -> /dev/null
stdout -> journal fd or /dev/null
stderr -> journal fd or /dev/null

Change-Id: I7346df00a668c2cb08098a93ad15db97a1e8d2f7
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
CMakeLists.txt
inc/launchpad_common.h
src/launchpad.c
src/launchpad_common.c
src/launchpad_lib.c

index 2989bb8..15735c7 100755 (executable)
@@ -29,6 +29,7 @@ PKG_CHECK_MODULES(${this_target_loader} REQUIRED
        aul
        vconf
        buxton2
+       libsystemd-daemon
        )
 
 FOREACH(flag ${${this_target_loader}_CFLAGS})
index 8016469..63a9de3 100644 (file)
@@ -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__ */
 
index 5305dc6..0ccd9f9 100755 (executable)
@@ -34,6 +34,7 @@
 #include <linux/limits.h>
 #include <ttrace.h>
 #include <vconf.h>
+#include <libgen.h>
 
 #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",
index ddb4432..a0f6c3b 100644 (file)
@@ -32,6 +32,8 @@
 #include <unistd.h>
 #include <tzplatform_config.h>
 #include <stdio.h>
+#include <stdbool.h>
+#include <systemd/sd-journal.h>
 
 #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;
+}
index 6336a19..17553ba 100644 (file)
@@ -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");