From e6724664c3d8fa8669746192f647c1845f9857ff Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 22 Apr 2024 10:16:40 +0200 Subject: [PATCH 1/3] fd-util: Return 1 from fd_nonblock() if we actually change the mode --- src/basic/fd-util.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 5d00530296..121e6cd219 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -167,7 +167,10 @@ int fd_nonblock(int fd, bool nonblock) { if (nflags == flags) return 0; - return RET_NERRNO(fcntl(fd, F_SETFL, nflags)); + if (fcntl(fd, F_SETFL, nflags) < 0) + return -errno; + + return 1; } int stdio_disable_nonblock(void) { From f57705d67d13593a05c95ef082ce85caeb18b062 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Fri, 19 Apr 2024 21:58:18 +0200 Subject: [PATCH 2/3] terminal-util: Enable line wrapping in reset_terminal_fd() The qemu seabios firmware disables serial console line wrapping. Let's make sure we re-enable it again when we reset a terminal to some sane defaults. To avoid potentially blocking on writing to the terminal, we put it in nonblocking mode and add a timeout of 50ms. --- src/basic/terminal-util.c | 22 ++++++++++++++++++++++ test/TEST-08-INITRD/test.sh | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index 9470c9fb1a..11c0da870c 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -306,7 +306,29 @@ int reset_terminal_fd(int fd, bool switch_to_text) { termios.c_cc[VMIN] = 1; r = RET_NERRNO(tcsetattr(fd, TCSANOW, &termios)); + if (r < 0) { + log_debug_errno(r, "Failed to set terminal parameters: %m"); + goto finish; + } + if (!terminal_is_dumb()) { + r = fd_nonblock(fd, true); + if (r < 0) { + log_debug_errno(r, "Failed to set terminal to non-blocking mode: %m"); + goto finish; + } + + /* Enable line wrapping. */ + (void) loop_write_full(fd, "\033[?7h", SIZE_MAX, 50 * USEC_PER_MSEC); + + if (r > 0) { + r = fd_nonblock(fd, false); + if (r < 0) { + log_debug_errno(r, "Failed to set terminal back to blocking mode: %m"); + goto finish; + } + } + } finish: /* Just in case, flush all crap out */ (void) tcflush(fd, TCIOFLUSH); diff --git a/test/TEST-08-INITRD/test.sh b/test/TEST-08-INITRD/test.sh index e8dbb2c36c..3ad60f1a94 100755 --- a/test/TEST-08-INITRD/test.sh +++ b/test/TEST-08-INITRD/test.sh @@ -70,7 +70,7 @@ check_result_qemu_hook() { fi # Check if the shutdown initrd was executed at all - if ! grep -qE "^Hello from shutdown initrd\s*$" "$console_log"; then + if ! grep -q "Hello from shutdown initrd" "$console_log"; then derror "Missing 'hello' message from shutdown initrd" return 1 fi From e888d1cae84bb572cd602b845c932b855835f99a Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 22 Apr 2024 10:21:13 +0200 Subject: [PATCH 3/3] mkosi: Drop workaround to re-enable serial console line wrapping Since we do it in reset_terminal_fd() now, there's no need to carry this workaround anymore. --- mkosi.images/system/mkosi.postinst.chroot | 4 ---- 1 file changed, 4 deletions(-) diff --git a/mkosi.images/system/mkosi.postinst.chroot b/mkosi.images/system/mkosi.postinst.chroot index 8a00ca9f68..d1052694aa 100755 --- a/mkosi.images/system/mkosi.postinst.chroot +++ b/mkosi.images/system/mkosi.postinst.chroot @@ -2,10 +2,6 @@ # SPDX-License-Identifier: LGPL-2.1-or-later set -e -# Make sure serial console line wrapping is re-enabled as qemu's seabios firmware disables serial console -# line-wrapping on boot. -echo "tput smam || :" >>/etc/profile - if [ -n "$SANITIZERS" ]; then LD_PRELOAD=$(ldd /usr/lib/systemd/systemd | grep libasan.so | awk '{print $3}')