Skip to content

Commit 8bb2db7

Browse files
committed
terminal-util: port some generic code over to rearrange_stdio()
1 parent aa11e28 commit 8bb2db7

File tree

3 files changed

+6
-38
lines changed

3 files changed

+6
-38
lines changed

src/basic/fd-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,7 @@ int acquire_data_fd(const void *data, size_t size, unsigned flags);
102102
int fd_move_above_stdio(int fd);
103103

104104
int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
105+
106+
static inline int make_null_stdio(void) {
107+
return rearrange_stdio(-1, -1, -1);
108+
}

src/basic/terminal-util.c

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,9 @@ int make_console_stdio(void) {
628628
if (r < 0)
629629
log_warning_errno(r, "Failed to reset terminal, ignoring: %m");
630630

631-
r = make_stdio(fd);
631+
r = rearrange_stdio(fd, fd, fd); /* This invalidates 'fd' both on success and on failure. */
632632
if (r < 0)
633-
return log_error_errno(r, "Failed to duplicate terminal fd: %m");
633+
return log_error_errno(r, "Failed to make terminal stdin/stdout/stderr: %m");
634634

635635
reset_terminal_feature_caches();
636636

@@ -905,40 +905,6 @@ bool on_tty(void) {
905905
return cached_on_tty;
906906
}
907907

908-
int make_stdio(int fd) {
909-
int r = 0;
910-
911-
assert(fd >= 0);
912-
913-
if (dup2(fd, STDIN_FILENO) < 0)
914-
r = -errno;
915-
if (dup2(fd, STDOUT_FILENO) < 0 && r >= 0)
916-
r = -errno;
917-
if (dup2(fd, STDERR_FILENO) < 0 && r >= 0)
918-
r = -errno;
919-
920-
safe_close_above_stdio(fd);
921-
922-
/* Explicitly unset O_CLOEXEC, since if fd was < 3, then dup2() was a NOP and the bit hence possibly set. */
923-
stdio_unset_cloexec();
924-
925-
return r;
926-
}
927-
928-
int make_null_stdio(void) {
929-
int null_fd, r;
930-
931-
null_fd = open("/dev/null", O_RDWR|O_NOCTTY|O_CLOEXEC);
932-
if (null_fd < 0)
933-
return -errno;
934-
935-
r = make_stdio(null_fd);
936-
937-
reset_terminal_feature_caches();
938-
939-
return r;
940-
}
941-
942908
int getttyname_malloc(int fd, char **ret) {
943909
size_t l = 100;
944910
int r;

src/basic/terminal-util.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ bool tty_is_console(const char *tty) _pure_;
9090
int vtnr_from_tty(const char *tty);
9191
const char *default_term_for_tty(const char *tty);
9292

93-
int make_stdio(int fd);
94-
int make_null_stdio(void);
9593
int make_console_stdio(void);
9694

9795
int fd_columns(int fd);

0 commit comments

Comments
 (0)