From 68e4c637f8cda2cc5750e1e04a422e6750c6f2e5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 22 Nov 2023 13:08:17 +0100 Subject: [PATCH 1/3] terminal-util: trivial modernizations for reset_terminal_fd() --- src/basic/terminal-util.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index b505a3ad0e..c266fea810 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -233,7 +233,7 @@ int ask_string(char **ret, const char *text, ...) { int reset_terminal_fd(int fd, bool switch_to_text) { struct termios termios; - int r = 0; + int r; /* Set terminal to some sane defaults */ @@ -256,7 +256,9 @@ int reset_terminal_fd(int fd, bool switch_to_text) { /* Set default keyboard mode */ - (void) vt_reset_keyboard(fd); + r = vt_reset_keyboard(fd); + if (r < 0) + log_debug_errno(r, "Failed to reset VT keyboard, ignoring: %m"); if (tcgetattr(fd, &termios) < 0) { r = log_debug_errno(errno, "Failed to get terminal parameters: %m"); @@ -290,8 +292,7 @@ int reset_terminal_fd(int fd, bool switch_to_text) { termios.c_cc[VTIME] = 0; termios.c_cc[VMIN] = 1; - if (tcsetattr(fd, TCSANOW, &termios) < 0) - r = -errno; + r = RET_NERRNO(tcsetattr(fd, TCSANOW, &termios)); finish: /* Just in case, flush all crap out */ From d5b6c6e3781b0c48f819bdfe2548d44a6583ec41 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 22 Nov 2023 13:10:16 +0100 Subject: [PATCH 2/3] terminal-util: drop ECHOPRT flag from TTY settings when resetting Let's disable ECHOPRT for terminals we reset. The feature only really makes sense for hardcopy terminals and we sure as shit don't talk to one of those. It has the effect that when line editing is on and you hit backspace it outputs "\" followed by the removed character. This never makes sense on a TTY that can just erase the character. Hence turn of this flag. We have carried this flag along for about forever, but it doesn't really make sense. I guess we mostly tested the terminal reset stuff for output only, not for input. This change is in particular useful for tools such as "systemd-firstboot" which interactively ask questions on the console, and where line editing should really work. --- src/basic/terminal-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index c266fea810..3355b749cc 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -273,7 +273,7 @@ int reset_terminal_fd(int fd, bool switch_to_text) { termios.c_iflag |= ICRNL | IMAXBEL | IUTF8; termios.c_oflag |= ONLCR | OPOST; termios.c_cflag |= CREAD; - termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE; + termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE; termios.c_cc[VINTR] = 03; /* ^C */ termios.c_cc[VQUIT] = 034; /* ^\ */ From 5fee4ac0f4b2d339300cc692d09e3b904c25b887 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 22 Nov 2023 13:13:03 +0100 Subject: [PATCH 3/3] firstboot: reset terminal settings right before asking a question We are about to do interactivity on the terminal, hence let's ensure we are in the TTY mode we expect to be in. --- src/firstboot/firstboot.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index 6cf7415a18..f77a5f6266 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -131,6 +131,8 @@ static void print_welcome(int rfd) { 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); + if (colors_enabled()) printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac, pn); else