Don't flush the tty device input queue when setting device attribute.

Rationale:
  * microcom: The tty device might already have a _good enough_ termios
    to read data from. Flushing the input queue just to set the terminal
    attribute would result in data loss in this case.
  * stty: This command doesn't read nor write data to the tty device, so
    flushing the input queue doesn't make sense here. The program
    actually talking to the tty should decide if it wants the tty
    flushed or not.
    Note: other implementations of stty also uses TCSANOW (bsd) or
    TCSADRAIN (coreutils), so I think this assumption is fine.
From 861774f11c49db0e9c59d54f84fbc71fc378544a Mon Sep 17 00:00:00 2001
From: Yi-Yo Chiang <yochiang@google.com>
Date: Tue, 21 May 2024 12:13:31 +0800
Subject: [PATCH] microcom,stty: Use TCSADRAIN to set tty device attribute

Don't flush the tty device input queue when setting device attribute.

Rationale:
  * microcom: The tty device might already have a _good enough_ termios
    to read data from. Flushing the input queue just to set the terminal
    attribute would result in data loss in this case.
  * stty: This command doesn't read nor write data to the tty device, so
    flushing the input queue doesn't make sense here. The program
    actually talking to the tty should decide if it wants the tty
    flushed or not.
    Note: other implementations of stty also uses TCSANOW (bsd) or
    TCSADRAIN (coreutils), so I think this assumption is fine.

Change-Id: Ia42e65eaae9d70fdef6c2016469070e52071e9c3
---
 toys/net/microcom.c | 4 ++--
 toys/pending/stty.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/toys/net/microcom.c b/toys/net/microcom.c
index 49ccde0f..d06afb14 100644
--- a/toys/net/microcom.c
+++ b/toys/net/microcom.c
@@ -30,7 +30,7 @@ GLOBALS(
 static void restore_states(int i)
 {
   if (TT.stok) tcsetattr(0, TCSAFLUSH, &TT.old_stdin);
-  tcsetattr(TT.fd, TCSAFLUSH, &TT.old_fd);
+  tcsetattr(TT.fd, TCSADRAIN, &TT.old_fd);
 }
 
 static void handle_esc(void)
@@ -107,7 +107,7 @@ void microcom_main(void)
   memcpy(&tio, &TT.old_fd, sizeof(struct termios));
   cfmakeraw(&tio);
   xsetspeed(&tio, TT.s);
-  if (tcsetattr(TT.fd, TCSAFLUSH, &tio)) perror_exit("set speed");
+  if (tcsetattr(TT.fd, TCSADRAIN, &tio)) perror_exit("set speed");
   if (!set_terminal(0, 1, 0, &TT.old_stdin)) TT.stok++;
   // ...and arrange to restore things, however we may exit.
   sigatexit(restore_states);
diff --git a/toys/pending/stty.c b/toys/pending/stty.c
index 217c0c2b..25d2d9b4 100644
--- a/toys/pending/stty.c
+++ b/toys/pending/stty.c
@@ -406,7 +406,7 @@ void stty_main(void)
       }
     }
 
-    tcsetattr(TT.fd, TCSAFLUSH, &new);
+    tcsetattr(TT.fd, TCSADRAIN, &new);
     xtcgetattr(&old);
     if (memcmp(&old, &new, sizeof(old)))
       error_exit("unable to perform all requested operations on %s", TT.F);
-- 
2.45.0.rc1.225.g2a3ae87e7f-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to