return k < 0 ? k : r;
}
+int terminal_reset_defensive(int fd, bool switch_to_text) {
+ int r = 0;
+
+ assert(fd >= 0);
+
+ /* Resets the terminal comprehensively, but defensively. i.e. both resets the tty via ioctl()s and
+ * via ANSI sequences, but avoids the latter in case we are talking to a pty. That's a safety measure
+ * because ptys might be connected to shell pipelines where we cannot expect such ansi sequences to
+ * work. Given that ptys are generally short-lived (and not recycled) this restriction shouldn't hurt
+ * much.
+ *
+ * The specified fd should be open for *writing*! */
+
+ RET_GATHER(r, reset_terminal_fd(fd, switch_to_text));
+
+ if (terminal_is_pty_fd(fd) == 0)
+ RET_GATHER(r, terminal_reset_ansi_seq(fd));
+
+ return r;
+}
+
void termios_disable_echo(struct termios *termios) {
assert(termios);
int reset_terminal_fd(int fd, bool switch_to_text);
int reset_terminal(const char *name);
int terminal_reset_ansi_seq(int fd);
+int terminal_reset_defensive(int fd, bool switch_to_text);
int terminal_set_cursor_position(int fd, unsigned row, unsigned column);
(void) ioctl(STDIN_FILENO, TIOCSCTTY, context->std_input == EXEC_INPUT_TTY_FORCE);
if (context->tty_reset)
- (void) reset_terminal_fd(STDIN_FILENO, /* switch_to_text= */ true);
+ (void) terminal_reset_defensive(STDIN_FILENO, /* switch_to_text= */ true);
(void) exec_context_apply_tty_size(context, STDIN_FILENO, /* tty_path= */ NULL);
}
if (r < 0)
return r;
- r = reset_terminal_fd(fd, /* switch_to_text= */ true);
+ r = terminal_reset_defensive(fd, /* switch_to_text= */ true);
if (r < 0)
return r;
(void) terminal_vhangup_fd(fd);
if (context->tty_reset)
- (void) reset_terminal_fd(fd, /* switch_to_text= */ true);
+ (void) terminal_reset_defensive(fd, /* switch_to_text= */ true);
(void) exec_context_apply_tty_size(context, fd, path);
pn = os_release_pretty_name(pretty_name, os_name);
ac = isempty(ansi_color) ? "0" : ansi_color;
- (void) reset_terminal_fd(STDIN_FILENO, /* switch_to_text= */ false);
+ (void) terminal_reset_defensive(STDIN_FILENO, /* switch_to_text= */ false);
if (colors_enabled())
printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac, pn);
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
- (void) reset_terminal_fd(STDIN_FILENO, /* switch_to_text= */ false);
+ (void) terminal_reset_defensive(STDIN_FILENO, /* switch_to_text= */ false);
for (;;) {
username = mfree(username);
reset_terminal_feature_caches();
}
+TEST(terminal_reset_defensive) {
+ int r;
+
+ r = terminal_reset_defensive(STDIN_FILENO, /* switch_to_text= */ false);
+ if (r < 0)
+ log_notice_errno(r, "Failed to reset terminal: %m");
+}
+
DEFINE_TEST_MAIN(LOG_INFO);
if (tty_fd < 0)
return log_error_errno(tty_fd, "Failed to acquire %s: %m", con);
- r = reset_terminal_fd(tty_fd, true);
- if (r < 0)
- log_warning_errno(r, "Failed to reset terminal, ignoring: %m");
+ (void) terminal_reset_defensive(tty_fd, /* switch_to_text= */ true);
log_info("Starting password query on %s.", con);
}