]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tty: n_tty: extract n_tty_wait_for_input()
authorJiri Slaby (SUSE) <jirislaby@kernel.org>
Mon, 17 Mar 2025 07:00:25 +0000 (08:00 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 20 Mar 2025 15:00:51 +0000 (08:00 -0700)
n_tty_read() is a very long function doing too much of different stuff.
Extract the "wait for input" to a separate function:
n_tty_wait_for_input(). It returns an error (< 0), no input (0), or has
potential input (1).

Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Link: https://lore.kernel.org/r/20250317070046.24386-11-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/n_tty.c

index 88aa5f9cbe5e007b80b4cd4cc2c64a34d08a55c5..0e3eb18490f098422d6a94d6fbb437adc15d0646 100644 (file)
@@ -2145,6 +2145,33 @@ static ssize_t n_tty_continue_cookie(struct tty_struct *tty, u8 *kbuf,
        return kb - kbuf;
 }
 
+static int n_tty_wait_for_input(struct tty_struct *tty, struct file *file,
+                               struct wait_queue_entry *wait, long *timeout)
+{
+       if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
+               return -EIO;
+       if (tty_hung_up_p(file))
+               return 0;
+       /*
+        * Abort readers for ttys which never actually get hung up.
+        * See __tty_hangup().
+        */
+       if (test_bit(TTY_HUPPING, &tty->flags))
+               return 0;
+       if (!*timeout)
+               return 0;
+       if (tty_io_nonblock(tty, file))
+               return -EAGAIN;
+       if (signal_pending(current))
+               return -ERESTARTSYS;
+
+       up_read(&tty->termios_rwsem);
+       *timeout = wait_woken(wait, TASK_INTERRUPTIBLE, *timeout);
+       down_read(&tty->termios_rwsem);
+
+       return 1;
+}
+
 /**
  * n_tty_read          -       read function for tty
  * @tty: tty device
@@ -2234,34 +2261,12 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
                        tty_buffer_flush_work(tty->port);
                        down_read(&tty->termios_rwsem);
                        if (!input_available_p(tty, 0)) {
-                               if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
-                                       retval = -EIO;
+                               int ret = n_tty_wait_for_input(tty, file, &wait,
+                                                              &timeout);
+                               if (ret <= 0) {
+                                       retval = ret;
                                        break;
                                }
-                               if (tty_hung_up_p(file))
-                                       break;
-                               /*
-                                * Abort readers for ttys which never actually
-                                * get hung up.  See __tty_hangup().
-                                */
-                               if (test_bit(TTY_HUPPING, &tty->flags))
-                                       break;
-                               if (!timeout)
-                                       break;
-                               if (tty_io_nonblock(tty, file)) {
-                                       retval = -EAGAIN;
-                                       break;
-                               }
-                               if (signal_pending(current)) {
-                                       retval = -ERESTARTSYS;
-                                       break;
-                               }
-                               up_read(&tty->termios_rwsem);
-
-                               timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
-                                               timeout);
-
-                               down_read(&tty->termios_rwsem);
                                continue;
                        }
                }