From 8bb2db738e13f67cdfe17afefac5fb03ca68a7e6 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 28 Feb 2018 21:21:33 +0100 Subject: [PATCH] terminal-util: port some generic code over to rearrange_stdio() --- src/basic/fd-util.h | 4 ++++ src/basic/terminal-util.c | 38 ++------------------------------------ src/basic/terminal-util.h | 2 -- 3 files changed, 6 insertions(+), 38 deletions(-) diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index b687f1a5559..e8d915bfa6e 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -102,3 +102,7 @@ int acquire_data_fd(const void *data, size_t size, unsigned flags); int fd_move_above_stdio(int fd); int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd); + +static inline int make_null_stdio(void) { + return rearrange_stdio(-1, -1, -1); +} diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index cdad4cb6213..eacfd14677a 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -628,9 +628,9 @@ int make_console_stdio(void) { if (r < 0) log_warning_errno(r, "Failed to reset terminal, ignoring: %m"); - r = make_stdio(fd); + r = rearrange_stdio(fd, fd, fd); /* This invalidates 'fd' both on success and on failure. */ if (r < 0) - return log_error_errno(r, "Failed to duplicate terminal fd: %m"); + return log_error_errno(r, "Failed to make terminal stdin/stdout/stderr: %m"); reset_terminal_feature_caches(); @@ -905,40 +905,6 @@ bool on_tty(void) { return cached_on_tty; } -int make_stdio(int fd) { - int r = 0; - - assert(fd >= 0); - - if (dup2(fd, STDIN_FILENO) < 0) - r = -errno; - if (dup2(fd, STDOUT_FILENO) < 0 && r >= 0) - r = -errno; - if (dup2(fd, STDERR_FILENO) < 0 && r >= 0) - r = -errno; - - safe_close_above_stdio(fd); - - /* Explicitly unset O_CLOEXEC, since if fd was < 3, then dup2() was a NOP and the bit hence possibly set. */ - stdio_unset_cloexec(); - - return r; -} - -int make_null_stdio(void) { - int null_fd, r; - - null_fd = open("/dev/null", O_RDWR|O_NOCTTY|O_CLOEXEC); - if (null_fd < 0) - return -errno; - - r = make_stdio(null_fd); - - reset_terminal_feature_caches(); - - return r; -} - int getttyname_malloc(int fd, char **ret) { size_t l = 100; int r; diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index f6e6020b66e..643e8e55bdc 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -90,8 +90,6 @@ bool tty_is_console(const char *tty) _pure_; int vtnr_from_tty(const char *tty); const char *default_term_for_tty(const char *tty); -int make_stdio(int fd); -int make_null_stdio(void); int make_console_stdio(void); int fd_columns(int fd);