terminal-util: Enable line wrapping in reset_terminal_fd()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 19 Apr 2024 19:58:18 +0000 (21:58 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 22 Apr 2024 13:28:26 +0000 (15:28 +0200)
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
test/TEST-08-INITRD/test.sh

index 9470c9fb1a5439535566cedcc41a8d7276993021..11c0da870c72b24af237e199c87b6f2060f7ec5f 100644 (file)
@@ -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);
index e8dbb2c36c61d2502e7447445b29325359cecf6c..3ad60f1a949d4d24a88c8fe8f548a2c39f7b6656 100755 (executable)
@@ -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