diff options
author | Hwankyu Jhun <h.jhun@samsung.com> | 2020-06-18 17:18:39 +0900 |
---|---|---|
committer | Hwankyu Jhun <h.jhun@samsung.com> | 2020-06-19 08:21:06 +0900 |
commit | 6d9d7ba1328b71a35fb57850efba49bb47f256d2 (patch) | |
tree | aabeac1b59877c355589ad3828328b12044122c9 | |
parent | 8f36fe1f81baf48af89550021dfb267208174f9a (diff) | |
download | launchpad-6d9d7ba1328b71a35fb57850efba49bb47f256d2.tar.gz launchpad-6d9d7ba1328b71a35fb57850efba49bb47f256d2.tar.bz2 launchpad-6d9d7ba1328b71a35fb57850efba49bb47f256d2.zip |
Fix standard I/O redirection
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>
-rw-r--r-- | src/common/src/launchpad_common.c | 91 |
1 files changed, 59 insertions, 32 deletions
diff --git a/src/common/src/launchpad_common.c b/src/common/src/launchpad_common.c index e41a391..d2bf965 100644 --- a/src/common/src/launchpad_common.c +++ b/src/common/src/launchpad_common.c @@ -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; } |