Skip to content

Commit c4dc304

Browse files
Francesco Ruggerigregkh
authored andcommitted
tty: Fix pty master poll() after slave closes v2
Commit f95499c ("n_tty: Don't wait for buffer work in read() loop") introduces a race window where a pty master can be signalled that the pty slave was closed before all the data that the slave wrote is delivered. Commit f8747d4 ("tty: Fix pty master read() after slave closes") fixed the problem in case of n_tty_read, but the problem still exists for n_tty_poll. This can be seen by running 'for ((i=0; i<100;i++));do ./test.py ;done' where test.py is: import os, select, pty (pid, pty_fd) = pty.fork() if pid == 0: os.write(1, 'This string should be received by parent') else: poller = select.epoll() poller.register( pty_fd, select.EPOLLIN ) ready = poller.poll( 1 * 1000 ) for fd, events in ready: if not events & select.EPOLLIN: print 'missed POLLIN event' else: print os.read(fd, 100) poller.close() The string from the slave is missed several times. This patch takes the same approach as the fix for read and special cases this condition for poll. Tested on 3.16. Signed-off-by: Francesco Ruggeri <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 7e12e67 commit c4dc304

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

drivers/tty/n_tty.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,12 +2413,17 @@ static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
24132413

24142414
poll_wait(file, &tty->read_wait, wait);
24152415
poll_wait(file, &tty->write_wait, wait);
2416+
if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2417+
mask |= POLLHUP;
24162418
if (input_available_p(tty, 1))
24172419
mask |= POLLIN | POLLRDNORM;
2420+
else if (mask & POLLHUP) {
2421+
tty_flush_to_ldisc(tty);
2422+
if (input_available_p(tty, 1))
2423+
mask |= POLLIN | POLLRDNORM;
2424+
}
24182425
if (tty->packet && tty->link->ctrl_status)
24192426
mask |= POLLPRI | POLLIN | POLLRDNORM;
2420-
if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2421-
mask |= POLLHUP;
24222427
if (tty_hung_up_p(file))
24232428
mask |= POLLHUP;
24242429
if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {

0 commit comments

Comments
 (0)