Fix standard I/O redirection 84/236584/8
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 18 Jun 2020 08:18:39 +0000 (17:18 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 18 Jun 2020 23:21:06 +0000 (08:21 +0900)
If calling sd_journal_stream_fd() is failed, the child process uses
the interitance of file descriptors.

Change-Id: Ia37c1ca586d735280cc07298088c00c93815b2aa
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/common/src/launchpad_common.c

index e41a39101513937230cb93f92c133dcc7eae128e..d2bf9652396dc943e98d3364a4e665b4221dc783 100644 (file)
@@ -923,53 +923,80 @@ int _close_all_fds(void)
        return 0;
 }
 
-int _setup_stdio(const char *ident)
+static int __redirect_stdin(void)
 {
+       int ret;
        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);
+               ret = -errno;
+               _E("open(%s) is failed. errno(%d)", PATH_DEV_NULL, errno);
+               return ret;
        }
+
+       ret = dup2(fd, STDIN_FILENO);
+       if (ret < 0)
+               _W("dup2(%d, 0) is failed. errno(%d)", fd, errno);
+
        close(fd);
+       return ret;
+}
+
+static int __redirect_stdout(const char *ident)
+{
+       int ret;
+       int fd;
 
-       /* stdout */
-       fd = sd_journal_stream_fd(ident, LOG_INFO, false);
+       fd = sd_journal_stream_fd(ident, LOG_INFO, 0);
        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);
+               _W("sd_journal_stream_fd() is failed. error(%d)", fd);
+               return fd;
        }
+
+       ret = dup2(fd, STDOUT_FILENO);
+       if (ret < 0)
+               _W("dup(%d, 1) is failed. errno(%d)", fd, errno);
+
        close(fd);
+       return ret;
+}
 
-       /* stderr */
-       fd = sd_journal_stream_fd(ident, LOG_INFO, false);
+static int __redirect_stderr(const char *ident)
+{
+       int ret;
+       int fd;
+
+       fd = sd_journal_stream_fd(ident, LOG_WARNING, 0);
        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);
+               _W("sd_journal_stream_fd() is failed. error(%d)", fd);
+               return fd;
        }
+
+       ret = dup2(fd, STDERR_FILENO);
+       if (ret < 0)
+               _W("dup(%d, 2) is failed. errno(%d)", fd, errno);
+
        close(fd);
+       return ret;
+
+}
+
+int _setup_stdio(const char *ident)
+{
+       int ret;
+
+       ret = __redirect_stdin();
+       if (ret < 0)
+               return ret;
+
+       ret = __redirect_stdout(ident);
+       if (ret < 0)
+               return ret;
+
+       ret = __redirect_stderr(ident);
+       if (ret < 0)
+               return ret;
 
        return 0;
 }