Bug 239604

Summary: FIONREAD ioctl returns the wrong result on a master pty after the slave has been closed
Product: Base System Reporter: martin
Component: kernAssignee: Kyle Evans <kevans>
Status: Closed FIXED    
Severity: Affects Only Me CC: bugs, kevans
Priority: --- Flags: kevans: mfc-stable14+
kevans: mfc-stable13+
kevans: mfc-stable12-
Version: 11.2-RELEASE   
Hardware: Any   
OS: Any   
URL: https://reviews.freebsd.org/D43457
Attachments:
Description Flags
Example to reproduce the bug none

Description martin 2019-08-02 15:51:17 UTC
Created attachment 206224 [details]
Example to reproduce the bug

Compile the attached example with:

cc -lutil pty-fionread.c

When it is run, I would expect it to print:

bytes1 = 0
bytes2 = 0

but in FreeBSD it prints:

bytes1 = 0
bytes2 = 1

It looks like the FIOREAD ioctl always returns 1 after the slave side of the pty has been closed, regardless of whether any data is available.  This contradicts the man page and also differs from Linux (and Solaris and AIX, which have very different code to open the pty).
Comment 1 commit-hook freebsd_committer freebsd_triage 2024-01-16 02:56:57 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=30189156d325fbcc9d1997d791daedc9fa3bed20

commit 30189156d325fbcc9d1997d791daedc9fa3bed20
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2024-01-16 02:55:59 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2024-01-16 02:55:59 +0000

    kern: pts: do not special case closed slave side

    This would previously return 1 if the slave side of the pts was closed
    to force an application to read() from it and observe the EOF, but it's
    not clear why and this is inconsistent both with how we handle devices
    with similar mechanics (like pipes) and also with other kernels, such as
    OpenBSD/NetBSD and Linux.

    PR:             239604
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D43457

 sys/kern/tty_pts.c             |  7 +----
 tests/sys/kern/Makefile        |  3 ++
 tests/sys/kern/tty_pts.c (new) | 64 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 6 deletions(-)
Comment 2 commit-hook freebsd_committer freebsd_triage 2024-01-30 17:12:27 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=984128ce2d5eb7d3374dd4b245d443ec2f5d96b2

commit 984128ce2d5eb7d3374dd4b245d443ec2f5d96b2
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2024-01-16 02:55:59 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2024-01-30 17:11:10 +0000

    kern: pts: do not special case closed slave side

    This would previously return 1 if the slave side of the pts was closed
    to force an application to read() from it and observe the EOF, but it's
    not clear why and this is inconsistent both with how we handle devices
    with similar mechanics (like pipes) and also with other kernels, such as
    OpenBSD/NetBSD and Linux.

    PR:             239604
    Reviewed by:    kib

    (cherry picked from commit 30189156d325fbcc9d1997d791daedc9fa3bed20)

 sys/kern/tty_pts.c             |  7 +----
 tests/sys/kern/Makefile        |  3 ++
 tests/sys/kern/tty_pts.c (new) | 64 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 6 deletions(-)
Comment 3 commit-hook freebsd_committer freebsd_triage 2024-01-30 17:12:30 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=aebaa32c3108e99cc28d9aef7229ed4fe7da18ca

commit aebaa32c3108e99cc28d9aef7229ed4fe7da18ca
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2024-01-16 02:55:59 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2024-01-30 17:11:24 +0000

    kern: pts: do not special case closed slave side

    This would previously return 1 if the slave side of the pts was closed
    to force an application to read() from it and observe the EOF, but it's
    not clear why and this is inconsistent both with how we handle devices
    with similar mechanics (like pipes) and also with other kernels, such as
    OpenBSD/NetBSD and Linux.

    PR:             239604
    Reviewed by:    kib

    (cherry picked from commit 30189156d325fbcc9d1997d791daedc9fa3bed20)

 sys/kern/tty_pts.c             |  7 +----
 tests/sys/kern/Makefile        |  3 ++
 tests/sys/kern/tty_pts.c (new) | 64 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 6 deletions(-)
Comment 4 Kyle Evans freebsd_committer freebsd_triage 2024-01-30 17:58:10 UTC
Merged to all stable branches, thanks!