]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ptyfwd: reset tty when exiting
authorLennart Poettering <lennart@poettering.net>
Fri, 19 Sep 2025 12:50:53 +0000 (14:50 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 22 Sep 2025 05:39:05 +0000 (14:39 +0900)
Let's do a "soft" reset of the TTY when a ptyfwd session ends. This is a
good idea, in order to reset changes to the scrolling window that code
inside the session might have made. A "soft" reset will undo this.

While we are at it, make sure to output the ansi sequences for this
*after* terminating any half-written line, as that is still somewhat
contents of the session, even if it's augmented.

src/basic/terminal-util.c
src/basic/terminal-util.h
src/shared/ptyfwd.c

index 2106d0db8c7d878c59908d1f8dd50b90b973da75..2175523e201dcd6e88a671746040c61510d89ee9 100644 (file)
@@ -955,7 +955,7 @@ finish:
         return r;
 }
 
-static int terminal_reset_ansi_seq(int fd) {
+int terminal_reset_ansi_seq(int fd) {
         int r, k;
 
         assert(fd >= 0);
index 2869469353374c6237029dbc9b302a0bca1b83b1..6428d9a1472e33b7645407619fbfba0b9751e78e 100644 (file)
@@ -34,6 +34,8 @@
 
 bool isatty_safe(int fd);
 
+int terminal_reset_ansi_seq(int fd);
+
 typedef enum TerminalResetFlags {
         TERMINAL_RESET_SWITCH_TO_TEXT = 1 << 0,
         TERMINAL_RESET_AVOID_ANSI_SEQ = 1 << 1,
index 46151ee21b962897d91663687903865e5979d72b..6d2b07d2b4d9bb8fc85ec3329aaec45d7b767e9a 100644 (file)
@@ -133,13 +133,6 @@ static void pty_forward_disconnect(PTYForward *f) {
                 /* STDIN/STDOUT should not be non-blocking normally, so let's reset it */
                 (void) fd_nonblock(f->output_fd, false);
 
-                if (colors_enabled()) {
-                        (void) loop_write(f->output_fd, ANSI_NORMAL ANSI_ERASE_TO_END_OF_SCREEN, SIZE_MAX);
-
-                        if (f->title)
-                                (void) loop_write(f->output_fd, ANSI_WINDOW_TITLE_POP, SIZE_MAX);
-                }
-
                 if (f->last_char_set && f->last_char != '\n') {
                         const char *s;
 
@@ -153,6 +146,13 @@ static void pty_forward_disconnect(PTYForward *f) {
                         f->last_char = '\n';
                 }
 
+                if (colors_enabled()) {
+                        if (f->title)
+                                (void) loop_write(f->output_fd, ANSI_WINDOW_TITLE_POP, SIZE_MAX);
+
+                        terminal_reset_ansi_seq(f->output_fd);
+                }
+
                 if (f->close_output_fd)
                         f->output_fd = safe_close(f->output_fd);
         }