Skip to content

Commit 40315ce

Browse files
Jiri Slaby (SUSE)gregkh
authored andcommitted
tty: n_tty: extract n_tty_wait_for_input()
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) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent aa1ebc9 commit 40315ce

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

drivers/tty/n_tty.c

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,33 @@ static ssize_t n_tty_continue_cookie(struct tty_struct *tty, u8 *kbuf,
21452145
return kb - kbuf;
21462146
}
21472147

2148+
static int n_tty_wait_for_input(struct tty_struct *tty, struct file *file,
2149+
struct wait_queue_entry *wait, long *timeout)
2150+
{
2151+
if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2152+
return -EIO;
2153+
if (tty_hung_up_p(file))
2154+
return 0;
2155+
/*
2156+
* Abort readers for ttys which never actually get hung up.
2157+
* See __tty_hangup().
2158+
*/
2159+
if (test_bit(TTY_HUPPING, &tty->flags))
2160+
return 0;
2161+
if (!*timeout)
2162+
return 0;
2163+
if (tty_io_nonblock(tty, file))
2164+
return -EAGAIN;
2165+
if (signal_pending(current))
2166+
return -ERESTARTSYS;
2167+
2168+
up_read(&tty->termios_rwsem);
2169+
*timeout = wait_woken(wait, TASK_INTERRUPTIBLE, *timeout);
2170+
down_read(&tty->termios_rwsem);
2171+
2172+
return 1;
2173+
}
2174+
21482175
/**
21492176
* n_tty_read - read function for tty
21502177
* @tty: tty device
@@ -2234,34 +2261,12 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
22342261
tty_buffer_flush_work(tty->port);
22352262
down_read(&tty->termios_rwsem);
22362263
if (!input_available_p(tty, 0)) {
2237-
if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2238-
retval = -EIO;
2264+
int ret = n_tty_wait_for_input(tty, file, &wait,
2265+
&timeout);
2266+
if (ret <= 0) {
2267+
retval = ret;
22392268
break;
22402269
}
2241-
if (tty_hung_up_p(file))
2242-
break;
2243-
/*
2244-
* Abort readers for ttys which never actually
2245-
* get hung up. See __tty_hangup().
2246-
*/
2247-
if (test_bit(TTY_HUPPING, &tty->flags))
2248-
break;
2249-
if (!timeout)
2250-
break;
2251-
if (tty_io_nonblock(tty, file)) {
2252-
retval = -EAGAIN;
2253-
break;
2254-
}
2255-
if (signal_pending(current)) {
2256-
retval = -ERESTARTSYS;
2257-
break;
2258-
}
2259-
up_read(&tty->termios_rwsem);
2260-
2261-
timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2262-
timeout);
2263-
2264-
down_read(&tty->termios_rwsem);
22652270
continue;
22662271
}
22672272
}

0 commit comments

Comments
 (0)