• Okash Khawaja's avatar
    tty: resolve tty contention between kernel and user space · a09ac397
    Okash Khawaja authored
    The commit 12e84c71 ("tty: export tty_open_by_driver") exports
    tty_open_by_device to allow tty to be opened from inside kernel which
    works fine except that it doesn't handle contention with user space or
    another kernel-space open of the same tty. For example, opening a tty
    from user space while it is kernel opened results in failure and a
    kernel log message about mismatch between tty->count and tty's file
    open count.
    
    This patch makes kernel access to tty exclusive, so that if a user
    process or kernel opens a kernel opened tty, it gets -EBUSY. It does
    this by adding TTY_KOPENED flag to tty->flags. When this flag is set,
    tty_open_by_driver returns -EBUSY. Instead of overloading
    tty_open_by_driver for both kernel and user space, this
    patch creates a separate function tty_kopen which closely follows
    tty_open_by_driver. tty_kclose closes the tty opened by tty_kopen.
    
    To address the mismatch between tty->count and #fd's, this patch adds
    #kopen's to the count before comparing it with tty->count. That way
    check_tty_count reflects correct usage count.
    
    Returning -EBUSY on tty open is a change in the interface. I have
    tested this with minicom, picocom and commands like "echo foo >
    /dev/ttyS0". They all correctly report "Device or resource busy" when
    the tty is already kernel opened.
    Signed-off-by: default avatarOkash Khawaja <okash.khawaja@gmail.com>
    Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
    a09ac397
tty_io.c 80.6 KB