Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles |
| 8 | * or rs-channels. It also implements echoing, cooked mode etc. |
| 9 | * |
| 10 | * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0. |
| 11 | * |
| 12 | * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the |
| 13 | * tty_struct and tty_queue structures. Previously there was an array |
| 14 | * of 256 tty_struct's which was statically allocated, and the |
| 15 | * tty_queue structures were allocated at boot time. Both are now |
| 16 | * dynamically allocated only when the tty is open. |
| 17 | * |
| 18 | * Also restructured routines so that there is more of a separation |
| 19 | * between the high-level tty routines (tty_io.c and tty_ioctl.c) and |
| 20 | * the low-level tty routines (serial.c, pty.c, console.c). This |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 21 | * makes for cleaner and more compact code. -TYT, 9/17/92 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * |
| 23 | * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines |
| 24 | * which can be dynamically activated and de-activated by the line |
| 25 | * discipline handling modules (like SLIP). |
| 26 | * |
| 27 | * NOTE: pay no attention to the line discipline code (yet); its |
| 28 | * interface is still subject to change in this version... |
| 29 | * -- TYT, 1/31/92 |
| 30 | * |
| 31 | * Added functionality to the OPOST tty handling. No delays, but all |
| 32 | * other bits should be there. |
| 33 | * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993. |
| 34 | * |
| 35 | * Rewrote canonical mode and added more termios flags. |
Xiaofei Tan | 395e783 | 2021-05-12 17:26:14 +0800 | [diff] [blame] | 36 | * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * |
| 38 | * Reorganized FASYNC support so mouse code can share it. |
| 39 | * -- ctm@ardi.com, 9Sep95 |
| 40 | * |
| 41 | * New TIOCLINUX variants added. |
| 42 | * -- mj@k332.feld.cvut.cz, 19-Nov-95 |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 43 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * Restrict vt switching via ioctl() |
| 45 | * -- grif@cs.ucr.edu, 5-Dec-95 |
| 46 | * |
| 47 | * Move console and virtual terminal code to more appropriate files, |
| 48 | * implement CONFIG_VT and generalize console device interface. |
| 49 | * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97 |
| 50 | * |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 51 | * Rewrote tty_init_dev and tty_release_dev to eliminate races. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * -- Bill Hawes <whawes@star.net>, June 97 |
| 53 | * |
| 54 | * Added devfs support. |
| 55 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998 |
| 56 | * |
| 57 | * Added support for a Unix98-style ptmx device. |
| 58 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 |
| 59 | * |
| 60 | * Reduced memory usage for older ARM systems |
| 61 | * -- Russell King <rmk@arm.linux.org.uk> |
| 62 | * |
| 63 | * Move do_SAK() into process context. Less stack use in devfs functions. |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 64 | * alloc_tty_struct() always uses kmalloc() |
| 65 | * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | */ |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <linux/types.h> |
| 69 | #include <linux/major.h> |
| 70 | #include <linux/errno.h> |
| 71 | #include <linux/signal.h> |
| 72 | #include <linux/fcntl.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 73 | #include <linux/sched/signal.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 74 | #include <linux/sched/task.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #include <linux/interrupt.h> |
| 76 | #include <linux/tty.h> |
| 77 | #include <linux/tty_driver.h> |
| 78 | #include <linux/tty_flip.h> |
| 79 | #include <linux/devpts_fs.h> |
| 80 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 81 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #include <linux/console.h> |
| 83 | #include <linux/timer.h> |
| 84 | #include <linux/ctype.h> |
| 85 | #include <linux/kd.h> |
| 86 | #include <linux/mm.h> |
| 87 | #include <linux/string.h> |
| 88 | #include <linux/slab.h> |
| 89 | #include <linux/poll.h> |
Arnd Bergmann | b7aff09 | 2019-06-06 10:07:36 +0200 | [diff] [blame] | 90 | #include <linux/ppp-ioctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | #include <linux/proc_fs.h> |
| 92 | #include <linux/init.h> |
| 93 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | #include <linux/wait.h> |
| 96 | #include <linux/bitops.h> |
Domen Puncer | b20f3ae | 2005-06-25 14:58:42 -0700 | [diff] [blame] | 97 | #include <linux/delay.h> |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 98 | #include <linux/seq_file.h> |
Alan Cox | d281da7 | 2010-09-16 18:21:24 +0100 | [diff] [blame] | 99 | #include <linux/serial.h> |
Manuel Zerpies | 5a3c6b251 | 2011-06-16 14:07:22 +0200 | [diff] [blame] | 100 | #include <linux/ratelimit.h> |
Al Viro | e211203 | 2018-09-11 19:47:09 -0400 | [diff] [blame] | 101 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 103 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | #include <linux/kbd_kern.h> |
| 106 | #include <linux/vt_kern.h> |
| 107 | #include <linux/selection.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | #include <linux/kmod.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 110 | #include <linux/nsproxy.h> |
Greg Kroah-Hartman | 98602c0 | 2021-04-08 14:51:22 +0200 | [diff] [blame] | 111 | #include "tty.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | #undef TTY_DEBUG_HANGUP |
Peter Hurley | accff79 | 2015-07-12 22:49:09 -0400 | [diff] [blame] | 114 | #ifdef TTY_DEBUG_HANGUP |
| 115 | # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args) |
| 116 | #else |
| 117 | # define tty_debug_hangup(tty, f, args...) do { } while (0) |
| 118 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | #define TTY_PARANOIA_CHECK 1 |
| 121 | #define CHECK_TTY_COUNT 1 |
| 122 | |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 123 | struct ktermios tty_std_termios = { /* for the benefit of tty drivers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | .c_iflag = ICRNL | IXON, |
| 125 | .c_oflag = OPOST | ONLCR, |
| 126 | .c_cflag = B38400 | CS8 | CREAD | HUPCL, |
| 127 | .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | |
| 128 | ECHOCTL | ECHOKE | IEXTEN, |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 129 | .c_cc = INIT_C_CC, |
| 130 | .c_ispeed = 38400, |
Peter Hurley | 133b130 | 2016-01-10 22:41:07 -0800 | [diff] [blame] | 131 | .c_ospeed = 38400, |
| 132 | /* .c_line = N_TTY, */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | EXPORT_SYMBOL(tty_std_termios); |
| 136 | |
| 137 | /* This list gets poked at by procfs and various bits of boot up code. This |
| 138 | could do with some rationalisation such as pulling the tty proc function |
| 139 | into this file */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | LIST_HEAD(tty_drivers); /* linked list of tty drivers */ |
| 142 | |
Peter Hurley | d1d027e | 2016-01-09 21:35:18 -0800 | [diff] [blame] | 143 | /* Mutex to protect creating and releasing a tty */ |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 144 | DEFINE_MUTEX(tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 146 | static ssize_t tty_read(struct kiocb *, struct iov_iter *); |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 147 | static ssize_t tty_write(struct kiocb *, struct iov_iter *); |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 148 | static __poll_t tty_poll(struct file *, poll_table *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | static int tty_open(struct inode *, struct file *); |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 150 | #ifdef CONFIG_COMPAT |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 151 | static long tty_compat_ioctl(struct file *file, unsigned int cmd, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 152 | unsigned long arg); |
| 153 | #else |
| 154 | #define tty_compat_ioctl NULL |
| 155 | #endif |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 156 | static int __tty_fasync(int fd, struct file *filp, int on); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 157 | static int tty_fasync(int fd, struct file *filp, int on); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 158 | static void release_tty(struct tty_struct *tty, int idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 160 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 161 | * free_tty_struct - free a disused tty |
| 162 | * @tty: tty struct to free |
| 163 | * |
| 164 | * Free the write buffers, tty queue and tty memory itself. |
| 165 | * |
| 166 | * Locking: none. Must be called after tty is definitely unused |
| 167 | */ |
| 168 | |
Peter Hurley | a3123fd | 2016-01-09 21:13:48 -0800 | [diff] [blame] | 169 | static void free_tty_struct(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Peter Hurley | c8b710b | 2016-01-09 21:13:46 -0800 | [diff] [blame] | 171 | tty_ldisc_deinit(tty); |
Markus Elfring | a211b1a | 2014-11-21 13:42:29 +0100 | [diff] [blame] | 172 | put_device(tty->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | kfree(tty->write_buf); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 174 | tty->magic = 0xDEADDEAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | kfree(tty); |
| 176 | } |
| 177 | |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 178 | static inline struct tty_struct *file_tty(struct file *file) |
| 179 | { |
| 180 | return ((struct tty_file_private *)file->private_data)->tty; |
| 181 | } |
| 182 | |
Jiri Slaby | fa90e1c | 2011-10-12 11:32:43 +0200 | [diff] [blame] | 183 | int tty_alloc_file(struct file *file) |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 184 | { |
| 185 | struct tty_file_private *priv; |
| 186 | |
Pekka Enberg | f573bd1 | 2010-08-24 07:48:34 +0300 | [diff] [blame] | 187 | priv = kmalloc(sizeof(*priv), GFP_KERNEL); |
| 188 | if (!priv) |
| 189 | return -ENOMEM; |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 190 | |
Jiri Slaby | fa90e1c | 2011-10-12 11:32:43 +0200 | [diff] [blame] | 191 | file->private_data = priv; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* Associate a new file with the tty structure */ |
| 197 | void tty_add_file(struct tty_struct *tty, struct file *file) |
| 198 | { |
| 199 | struct tty_file_private *priv = file->private_data; |
| 200 | |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 201 | priv->tty = tty; |
| 202 | priv->file = file; |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 203 | |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 204 | spin_lock(&tty->files_lock); |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 205 | list_add(&priv->list, &tty->tty_files); |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 206 | spin_unlock(&tty->files_lock); |
Jiri Slaby | fa90e1c | 2011-10-12 11:32:43 +0200 | [diff] [blame] | 207 | } |
Pekka Enberg | f573bd1 | 2010-08-24 07:48:34 +0300 | [diff] [blame] | 208 | |
Lee Jones | 08aa504 | 2020-11-04 19:35:25 +0000 | [diff] [blame] | 209 | /* |
Jiri Slaby | fa90e1c | 2011-10-12 11:32:43 +0200 | [diff] [blame] | 210 | * tty_free_file - free file->private_data |
| 211 | * |
| 212 | * This shall be used only for fail path handling when tty_add_file was not |
| 213 | * called yet. |
| 214 | */ |
| 215 | void tty_free_file(struct file *file) |
| 216 | { |
| 217 | struct tty_file_private *priv = file->private_data; |
| 218 | |
| 219 | file->private_data = NULL; |
| 220 | kfree(priv); |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* Delete file from its tty */ |
Josh Triplett | 2520e27 | 2012-11-18 21:27:47 -0800 | [diff] [blame] | 224 | static void tty_del_file(struct file *file) |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 225 | { |
| 226 | struct tty_file_private *priv = file->private_data; |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 227 | struct tty_struct *tty = priv->tty; |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 228 | |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 229 | spin_lock(&tty->files_lock); |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 230 | list_del(&priv->list); |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 231 | spin_unlock(&tty->files_lock); |
Jiri Slaby | fa90e1c | 2011-10-12 11:32:43 +0200 | [diff] [blame] | 232 | tty_free_file(file); |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 233 | } |
| 234 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 235 | /** |
| 236 | * tty_name - return tty naming |
| 237 | * @tty: tty structure |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 238 | * |
| 239 | * Convert a tty structure into a name. The name reflects the kernel |
| 240 | * naming policy and if udev is in use may not reflect user space |
| 241 | * |
| 242 | * Locking: none |
| 243 | */ |
| 244 | |
Rasmus Villemoes | 429b474 | 2015-03-31 15:55:59 +0200 | [diff] [blame] | 245 | const char *tty_name(const struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
| 247 | if (!tty) /* Hmm. NULL pointer. That's fun. */ |
Rasmus Villemoes | 917162c | 2015-03-31 15:55:58 +0200 | [diff] [blame] | 248 | return "NULL tty"; |
| 249 | return tty->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | EXPORT_SYMBOL(tty_name); |
| 253 | |
Peter Hurley | 0a083ed | 2015-11-08 13:01:12 -0500 | [diff] [blame] | 254 | const char *tty_driver_name(const struct tty_struct *tty) |
| 255 | { |
| 256 | if (!tty || !tty->driver) |
| 257 | return ""; |
| 258 | return tty->driver->name; |
| 259 | } |
| 260 | |
Peter Hurley | 82b8f88 | 2015-11-08 13:01:09 -0500 | [diff] [blame] | 261 | static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | const char *routine) |
| 263 | { |
| 264 | #ifdef TTY_PARANOIA_CHECK |
| 265 | if (!tty) { |
Peter Hurley | 89222e6 | 2015-11-08 13:01:18 -0500 | [diff] [blame] | 266 | pr_warn("(%d:%d): %s: NULL tty\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | imajor(inode), iminor(inode), routine); |
| 268 | return 1; |
| 269 | } |
| 270 | if (tty->magic != TTY_MAGIC) { |
Peter Hurley | 89222e6 | 2015-11-08 13:01:18 -0500 | [diff] [blame] | 271 | pr_warn("(%d:%d): %s: bad magic number\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | imajor(inode), iminor(inode), routine); |
| 273 | return 1; |
| 274 | } |
| 275 | #endif |
| 276 | return 0; |
| 277 | } |
| 278 | |
Peter Hurley | deb287e | 2014-11-05 12:12:55 -0500 | [diff] [blame] | 279 | /* Caller must hold tty_lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | static int check_tty_count(struct tty_struct *tty, const char *routine) |
| 281 | { |
| 282 | #ifdef CHECK_TTY_COUNT |
| 283 | struct list_head *p; |
Okash Khawaja | a09ac39 | 2017-07-20 08:22:36 +0100 | [diff] [blame] | 284 | int count = 0, kopen_count = 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 285 | |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 286 | spin_lock(&tty->files_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | list_for_each(p, &tty->tty_files) { |
| 288 | count++; |
| 289 | } |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 290 | spin_unlock(&tty->files_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 292 | tty->driver->subtype == PTY_TYPE_SLAVE && |
| 293 | tty->link && tty->link->count) |
| 294 | count++; |
Okash Khawaja | a09ac39 | 2017-07-20 08:22:36 +0100 | [diff] [blame] | 295 | if (tty_port_kopened(tty->port)) |
| 296 | kopen_count++; |
| 297 | if (tty->count != (count + kopen_count)) { |
| 298 | tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n", |
| 299 | routine, tty->count, count, kopen_count); |
| 300 | return (count + kopen_count); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | #endif |
| 303 | return 0; |
| 304 | } |
| 305 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 306 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 307 | * get_tty_driver - find device of a tty |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 308 | * @device: device identifier |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 309 | * @index: returns the index of the tty |
| 310 | * |
| 311 | * This routine returns a tty driver structure, given a device number |
| 312 | * and also passes back the index number. |
| 313 | * |
| 314 | * Locking: caller must hold tty_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | static struct tty_driver *get_tty_driver(dev_t device, int *index) |
| 318 | { |
| 319 | struct tty_driver *p; |
| 320 | |
| 321 | list_for_each_entry(p, &tty_drivers, tty_drivers) { |
| 322 | dev_t base = MKDEV(p->major, p->minor_start); |
Xiaofei Tan | e73b240 | 2021-05-12 17:26:15 +0800 | [diff] [blame] | 323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | if (device < base || device >= base + p->num) |
| 325 | continue; |
| 326 | *index = device - base; |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 327 | return tty_driver_kref_get(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | return NULL; |
| 330 | } |
| 331 | |
Okash Khawaja | fc61ed5 | 2017-06-25 19:40:00 +0100 | [diff] [blame] | 332 | /** |
| 333 | * tty_dev_name_to_number - return dev_t for device name |
| 334 | * @name: user space name of device under /dev |
| 335 | * @number: pointer to dev_t that this function will populate |
| 336 | * |
| 337 | * This function converts device names like ttyS0 or ttyUSB1 into dev_t |
| 338 | * like (4, 64) or (188, 1). If no corresponding driver is registered then |
| 339 | * the function returns -ENODEV. |
| 340 | * |
| 341 | * Locking: this acquires tty_mutex to protect the tty_drivers list from |
| 342 | * being modified while we are traversing it, and makes sure to |
| 343 | * release it before exiting. |
| 344 | */ |
| 345 | int tty_dev_name_to_number(const char *name, dev_t *number) |
| 346 | { |
| 347 | struct tty_driver *p; |
| 348 | int ret; |
| 349 | int index, prefix_length = 0; |
| 350 | const char *str; |
| 351 | |
| 352 | for (str = name; *str && !isdigit(*str); str++) |
| 353 | ; |
| 354 | |
| 355 | if (!*str) |
| 356 | return -EINVAL; |
| 357 | |
| 358 | ret = kstrtoint(str, 10, &index); |
| 359 | if (ret) |
| 360 | return ret; |
| 361 | |
| 362 | prefix_length = str - name; |
| 363 | mutex_lock(&tty_mutex); |
| 364 | |
| 365 | list_for_each_entry(p, &tty_drivers, tty_drivers) |
| 366 | if (prefix_length == strlen(p->name) && strncmp(name, |
| 367 | p->name, prefix_length) == 0) { |
| 368 | if (index < p->num) { |
| 369 | *number = MKDEV(p->major, p->minor_start + index); |
| 370 | goto out; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* if here then driver wasn't found */ |
| 375 | ret = -ENODEV; |
| 376 | out: |
| 377 | mutex_unlock(&tty_mutex); |
| 378 | return ret; |
| 379 | } |
| 380 | EXPORT_SYMBOL_GPL(tty_dev_name_to_number); |
| 381 | |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 382 | #ifdef CONFIG_CONSOLE_POLL |
| 383 | |
| 384 | /** |
| 385 | * tty_find_polling_driver - find device of a polled tty |
| 386 | * @name: name string to match |
| 387 | * @line: pointer to resulting tty line nr |
| 388 | * |
| 389 | * This routine returns a tty driver structure, given a name |
| 390 | * and the condition that the tty driver is capable of polled |
| 391 | * operation. |
| 392 | */ |
| 393 | struct tty_driver *tty_find_polling_driver(char *name, int *line) |
| 394 | { |
| 395 | struct tty_driver *p, *res = NULL; |
| 396 | int tty_line = 0; |
Jason Wessel | 0dca0fd | 2008-09-26 10:36:42 -0500 | [diff] [blame] | 397 | int len; |
Alan Cox | 5f0878a | 2009-06-11 12:46:41 +0100 | [diff] [blame] | 398 | char *str, *stp; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 399 | |
Jason Wessel | 0dca0fd | 2008-09-26 10:36:42 -0500 | [diff] [blame] | 400 | for (str = name; *str; str++) |
| 401 | if ((*str >= '0' && *str <= '9') || *str == ',') |
| 402 | break; |
| 403 | if (!*str) |
| 404 | return NULL; |
| 405 | |
| 406 | len = str - name; |
| 407 | tty_line = simple_strtoul(str, &str, 10); |
| 408 | |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 409 | mutex_lock(&tty_mutex); |
| 410 | /* Search through the tty devices to look for a match */ |
| 411 | list_for_each_entry(p, &tty_drivers, tty_drivers) { |
Miles Chen | 33a1a7b | 2018-10-08 10:39:17 +0800 | [diff] [blame] | 412 | if (!len || strncmp(name, p->name, len) != 0) |
Jason Wessel | 0dca0fd | 2008-09-26 10:36:42 -0500 | [diff] [blame] | 413 | continue; |
Alan Cox | 5f0878a | 2009-06-11 12:46:41 +0100 | [diff] [blame] | 414 | stp = str; |
| 415 | if (*stp == ',') |
| 416 | stp++; |
| 417 | if (*stp == '\0') |
| 418 | stp = NULL; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 419 | |
Nathael Pajani | 6eb68d6 | 2010-09-02 16:06:16 +0200 | [diff] [blame] | 420 | if (tty_line >= 0 && tty_line < p->num && p->ops && |
Alan Cox | 5f0878a | 2009-06-11 12:46:41 +0100 | [diff] [blame] | 421 | p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) { |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 422 | res = tty_driver_kref_get(p); |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 423 | *line = tty_line; |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | mutex_unlock(&tty_mutex); |
| 428 | |
| 429 | return res; |
| 430 | } |
| 431 | EXPORT_SYMBOL_GPL(tty_find_polling_driver); |
| 432 | #endif |
| 433 | |
Linus Torvalds | ddc5fda | 2021-01-21 10:08:15 -0800 | [diff] [blame] | 434 | static ssize_t hung_up_tty_read(struct kiocb *iocb, struct iov_iter *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
| 436 | return 0; |
| 437 | } |
| 438 | |
Linus Torvalds | 1774985 | 2021-01-21 10:04:27 -0800 | [diff] [blame] | 439 | static ssize_t hung_up_tty_write(struct kiocb *iocb, struct iov_iter *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | return -EIO; |
| 442 | } |
| 443 | |
| 444 | /* No kernel lock held - none needed ;) */ |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 445 | static __poll_t hung_up_tty_poll(struct file *filp, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 447 | return EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP | EPOLLRDNORM | EPOLLWRNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 450 | static long hung_up_tty_ioctl(struct file *file, unsigned int cmd, |
| 451 | unsigned long arg) |
Paul Fulghum | 38ad2ed | 2007-06-16 10:15:55 -0700 | [diff] [blame] | 452 | { |
| 453 | return cmd == TIOCSPGRP ? -ENOTTY : -EIO; |
| 454 | } |
| 455 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 456 | static long hung_up_tty_compat_ioctl(struct file *file, |
Paul Fulghum | 38ad2ed | 2007-06-16 10:15:55 -0700 | [diff] [blame] | 457 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
| 459 | return cmd == TIOCSPGRP ? -ENOTTY : -EIO; |
| 460 | } |
| 461 | |
Peter Hurley | f557474 | 2016-01-09 21:45:10 -0800 | [diff] [blame] | 462 | static int hung_up_tty_fasync(int fd, struct file *file, int on) |
| 463 | { |
| 464 | return -ENOTTY; |
| 465 | } |
| 466 | |
Masatake YAMATO | d01c328 | 2017-07-18 06:27:59 +0900 | [diff] [blame] | 467 | static void tty_show_fdinfo(struct seq_file *m, struct file *file) |
| 468 | { |
| 469 | struct tty_struct *tty = file_tty(file); |
| 470 | |
| 471 | if (tty && tty->ops && tty->ops->show_fdinfo) |
| 472 | tty->ops->show_fdinfo(tty, m); |
| 473 | } |
| 474 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 475 | static const struct file_operations tty_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | .llseek = no_llseek, |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 477 | .read_iter = tty_read, |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 478 | .write_iter = tty_write, |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 479 | .splice_read = generic_file_splice_read, |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 480 | .splice_write = iter_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | .poll = tty_poll, |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 482 | .unlocked_ioctl = tty_ioctl, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 483 | .compat_ioctl = tty_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | .open = tty_open, |
| 485 | .release = tty_release, |
| 486 | .fasync = tty_fasync, |
Masatake YAMATO | d01c328 | 2017-07-18 06:27:59 +0900 | [diff] [blame] | 487 | .show_fdinfo = tty_show_fdinfo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | }; |
| 489 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 490 | static const struct file_operations console_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | .llseek = no_llseek, |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 492 | .read_iter = tty_read, |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 493 | .write_iter = redirected_tty_write, |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 494 | .splice_read = generic_file_splice_read, |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 495 | .splice_write = iter_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | .poll = tty_poll, |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 497 | .unlocked_ioctl = tty_ioctl, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 498 | .compat_ioctl = tty_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | .open = tty_open, |
| 500 | .release = tty_release, |
| 501 | .fasync = tty_fasync, |
| 502 | }; |
| 503 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 504 | static const struct file_operations hung_up_tty_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | .llseek = no_llseek, |
Linus Torvalds | ddc5fda | 2021-01-21 10:08:15 -0800 | [diff] [blame] | 506 | .read_iter = hung_up_tty_read, |
Linus Torvalds | 1774985 | 2021-01-21 10:04:27 -0800 | [diff] [blame] | 507 | .write_iter = hung_up_tty_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | .poll = hung_up_tty_poll, |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 509 | .unlocked_ioctl = hung_up_tty_ioctl, |
Paul Fulghum | 38ad2ed | 2007-06-16 10:15:55 -0700 | [diff] [blame] | 510 | .compat_ioctl = hung_up_tty_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | .release = tty_release, |
Peter Hurley | f557474 | 2016-01-09 21:45:10 -0800 | [diff] [blame] | 512 | .fasync = hung_up_tty_fasync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | }; |
| 514 | |
| 515 | static DEFINE_SPINLOCK(redirect_lock); |
| 516 | static struct file *redirect; |
| 517 | |
| 518 | /** |
| 519 | * tty_wakeup - request more data |
| 520 | * @tty: terminal |
| 521 | * |
| 522 | * Internal and external helper for wakeups of tty. This function |
| 523 | * informs the line discipline if present that the driver is ready |
| 524 | * to receive more output data. |
| 525 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | void tty_wakeup(struct tty_struct *tty) |
| 528 | { |
| 529 | struct tty_ldisc *ld; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { |
| 532 | ld = tty_ldisc_ref(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 533 | if (ld) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 534 | if (ld->ops->write_wakeup) |
| 535 | ld->ops->write_wakeup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | tty_ldisc_deref(ld); |
| 537 | } |
| 538 | } |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 539 | wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | EXPORT_SYMBOL_GPL(tty_wakeup); |
| 543 | |
| 544 | /** |
Corey Minyard | f446776 | 2020-11-23 18:49:01 -0600 | [diff] [blame] | 545 | * tty_release_redirect - Release a redirect on a pty if present |
| 546 | * @tty: tty device |
| 547 | * |
| 548 | * This is available to the pty code so if the master closes, if the |
Greg Kroah-Hartman | c776b77 | 2021-03-04 17:18:02 +0100 | [diff] [blame] | 549 | * slave is a redirect it can release the redirect. |
Corey Minyard | f446776 | 2020-11-23 18:49:01 -0600 | [diff] [blame] | 550 | */ |
Greg Kroah-Hartman | dd9f611 | 2021-04-08 14:51:31 +0200 | [diff] [blame] | 551 | static struct file *tty_release_redirect(struct tty_struct *tty) |
Corey Minyard | f446776 | 2020-11-23 18:49:01 -0600 | [diff] [blame] | 552 | { |
| 553 | struct file *f = NULL; |
| 554 | |
| 555 | spin_lock(&redirect_lock); |
| 556 | if (redirect && file_tty(redirect) == tty) { |
| 557 | f = redirect; |
| 558 | redirect = NULL; |
| 559 | } |
| 560 | spin_unlock(&redirect_lock); |
| 561 | |
| 562 | return f; |
| 563 | } |
Corey Minyard | f446776 | 2020-11-23 18:49:01 -0600 | [diff] [blame] | 564 | |
| 565 | /** |
Arnd Bergmann | 11dbf20 | 2010-06-18 14:58:07 +0200 | [diff] [blame] | 566 | * __tty_hangup - actual handler for hangup events |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 567 | * @tty: tty device |
Lee Jones | 08aa504 | 2020-11-04 19:35:25 +0000 | [diff] [blame] | 568 | * @exit_session: if non-zero, signal all foreground group processes |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 569 | * |
Kevin Cernekee | ef4f527 | 2012-12-26 20:43:41 -0800 | [diff] [blame] | 570 | * This can be called by a "kworker" kernel thread. That is process |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 571 | * synchronous but doesn't hold any locks, so we need to make sure we |
| 572 | * have the appropriate locks for what we're doing. |
| 573 | * |
| 574 | * The hangup event clears any pending redirections onto the hung up |
| 575 | * device. It ensures future writes will error and it does the needed |
| 576 | * line discipline hangup and signal delivery. The tty object itself |
| 577 | * remains intact. |
| 578 | * |
| 579 | * Locking: |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 580 | * BTM |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 581 | * redirect lock for undoing redirection |
| 582 | * file list lock for manipulating list of ttys |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 583 | * tty_ldiscs_lock from called functions |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 584 | * termios_rwsem resetting termios data |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 585 | * tasklist_lock to walk task list for hangup event |
| 586 | * ->siglock to protect ->signal/->sighand |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | */ |
Peter Hurley | f91e259 | 2013-03-06 07:20:56 -0500 | [diff] [blame] | 588 | static void __tty_hangup(struct tty_struct *tty, int exit_session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 590 | struct file *cons_filp = NULL; |
Corey Minyard | f446776 | 2020-11-23 18:49:01 -0600 | [diff] [blame] | 591 | struct file *filp, *f; |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 592 | struct tty_file_private *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | int closecount = 0, n; |
Peter Hurley | ea648a4 | 2013-03-06 07:20:53 -0500 | [diff] [blame] | 594 | int refs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | if (!tty) |
| 597 | return; |
| 598 | |
Corey Minyard | f446776 | 2020-11-23 18:49:01 -0600 | [diff] [blame] | 599 | f = tty_release_redirect(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 600 | |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 601 | tty_lock(tty); |
Arnd Bergmann | 11dbf20 | 2010-06-18 14:58:07 +0200 | [diff] [blame] | 602 | |
Peter Hurley | cb50e52 | 2013-07-31 14:05:45 -0400 | [diff] [blame] | 603 | if (test_bit(TTY_HUPPED, &tty->flags)) { |
| 604 | tty_unlock(tty); |
| 605 | return; |
| 606 | } |
| 607 | |
Tejun Heo | 28b0f8a | 2018-02-13 07:38:08 -0800 | [diff] [blame] | 608 | /* |
| 609 | * Some console devices aren't actually hung up for technical and |
| 610 | * historical reasons, which can lead to indefinite interruptible |
| 611 | * sleep in n_tty_read(). The following explicitly tells |
| 612 | * n_tty_read() to abort readers. |
| 613 | */ |
| 614 | set_bit(TTY_HUPPING, &tty->flags); |
| 615 | |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 616 | /* inuse_filps is protected by the single tty lock, |
| 617 | this really needs to change if we want to flush the |
| 618 | workqueue with the lock held */ |
Arnd Bergmann | 11dbf20 | 2010-06-18 14:58:07 +0200 | [diff] [blame] | 619 | check_tty_count(tty, "tty_hangup"); |
Alan Cox | 36ba782 | 2009-11-30 13:18:51 +0000 | [diff] [blame] | 620 | |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 621 | spin_lock(&tty->files_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | /* This breaks for file handles being sent over AF_UNIX sockets ? */ |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 623 | list_for_each_entry(priv, &tty->tty_files, list) { |
| 624 | filp = priv->file; |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 625 | if (filp->f_op->write_iter == redirected_tty_write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | cons_filp = filp; |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 627 | if (filp->f_op->write_iter != tty_write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | continue; |
| 629 | closecount++; |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 630 | __tty_fasync(-1, filp, 0); /* can't block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | filp->f_op = &hung_up_tty_fops; |
| 632 | } |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 633 | spin_unlock(&tty->files_lock); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 634 | |
Peter Hurley | 25fdf24 | 2013-03-06 07:20:57 -0500 | [diff] [blame] | 635 | refs = tty_signal_session_leader(tty, exit_session); |
| 636 | /* Account for the p->signal references we killed */ |
| 637 | while (refs--) |
| 638 | tty_kref_put(tty); |
| 639 | |
Peter Hurley | 892d1fa | 2016-01-10 22:41:06 -0800 | [diff] [blame] | 640 | tty_ldisc_hangup(tty, cons_filp != NULL); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 641 | |
Jiri Slaby | 64d608d | 2021-05-05 11:19:06 +0200 | [diff] [blame] | 642 | spin_lock_irq(&tty->ctrl.lock); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 643 | clear_bit(TTY_THROTTLED, &tty->flags); |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 644 | clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
Jiri Slaby | 64d608d | 2021-05-05 11:19:06 +0200 | [diff] [blame] | 645 | put_pid(tty->ctrl.session); |
| 646 | put_pid(tty->ctrl.pgrp); |
| 647 | tty->ctrl.session = NULL; |
| 648 | tty->ctrl.pgrp = NULL; |
| 649 | tty->ctrl.pktstatus = 0; |
| 650 | spin_unlock_irq(&tty->ctrl.lock); |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | /* |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 653 | * If one of the devices matches a console pointer, we |
| 654 | * cannot just call hangup() because that will cause |
| 655 | * tty->count and state->count to go out of sync. |
| 656 | * So we just call close() the right number of times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | */ |
| 658 | if (cons_filp) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 659 | if (tty->ops->close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | for (n = 0; n < closecount; n++) |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 661 | tty->ops->close(tty, cons_filp); |
| 662 | } else if (tty->ops->hangup) |
Peter Hurley | 7c6d340 | 2014-06-16 09:17:08 -0400 | [diff] [blame] | 663 | tty->ops->hangup(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 664 | /* |
Peter Hurley | 892d1fa | 2016-01-10 22:41:06 -0800 | [diff] [blame] | 665 | * We don't want to have driver/ldisc interactions beyond the ones |
| 666 | * we did here. The driver layer expects no calls after ->hangup() |
| 667 | * from the ldisc side, which is now guaranteed. |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 668 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | set_bit(TTY_HUPPED, &tty->flags); |
Tejun Heo | 28b0f8a | 2018-02-13 07:38:08 -0800 | [diff] [blame] | 670 | clear_bit(TTY_HUPPING, &tty->flags); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 671 | tty_unlock(tty); |
Arnd Bergmann | 11dbf20 | 2010-06-18 14:58:07 +0200 | [diff] [blame] | 672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | if (f) |
| 674 | fput(f); |
| 675 | } |
| 676 | |
Arnd Bergmann | ddcd9fb | 2010-06-01 22:53:08 +0200 | [diff] [blame] | 677 | static void do_tty_hangup(struct work_struct *work) |
| 678 | { |
| 679 | struct tty_struct *tty = |
| 680 | container_of(work, struct tty_struct, hangup_work); |
| 681 | |
Peter Hurley | f91e259 | 2013-03-06 07:20:56 -0500 | [diff] [blame] | 682 | __tty_hangup(tty, 0); |
Arnd Bergmann | ddcd9fb | 2010-06-01 22:53:08 +0200 | [diff] [blame] | 683 | } |
| 684 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 685 | /** |
| 686 | * tty_hangup - trigger a hangup event |
| 687 | * @tty: tty to hangup |
| 688 | * |
| 689 | * A carrier loss (virtual or otherwise) has occurred on this like |
| 690 | * schedule a hangup sequence to run after this event. |
| 691 | */ |
| 692 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 693 | void tty_hangup(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
Peter Hurley | d435cef | 2015-11-08 13:01:19 -0500 | [diff] [blame] | 695 | tty_debug_hangup(tty, "hangup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | schedule_work(&tty->hangup_work); |
| 697 | } |
| 698 | |
| 699 | EXPORT_SYMBOL(tty_hangup); |
| 700 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 701 | /** |
| 702 | * tty_vhangup - process vhangup |
| 703 | * @tty: tty to hangup |
| 704 | * |
| 705 | * The user has asked via system call for the terminal to be hung up. |
| 706 | * We do this synchronously so that when the syscall returns the process |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 707 | * is complete. That guarantee is necessary for security reasons. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 708 | */ |
| 709 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 710 | void tty_vhangup(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | { |
Peter Hurley | d435cef | 2015-11-08 13:01:19 -0500 | [diff] [blame] | 712 | tty_debug_hangup(tty, "vhangup\n"); |
Peter Hurley | f91e259 | 2013-03-06 07:20:56 -0500 | [diff] [blame] | 713 | __tty_hangup(tty, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | EXPORT_SYMBOL(tty_vhangup); |
| 717 | |
Arnd Bergmann | 11dbf20 | 2010-06-18 14:58:07 +0200 | [diff] [blame] | 718 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 719 | /** |
Alan Cox | 2cb5998 | 2008-10-13 10:40:30 +0100 | [diff] [blame] | 720 | * tty_vhangup_self - process vhangup for own ctty |
| 721 | * |
| 722 | * Perform a vhangup on the current controlling tty |
| 723 | */ |
| 724 | |
| 725 | void tty_vhangup_self(void) |
| 726 | { |
| 727 | struct tty_struct *tty; |
| 728 | |
Alan Cox | 2cb5998 | 2008-10-13 10:40:30 +0100 | [diff] [blame] | 729 | tty = get_current_tty(); |
| 730 | if (tty) { |
| 731 | tty_vhangup(tty); |
| 732 | tty_kref_put(tty); |
| 733 | } |
Alan Cox | 2cb5998 | 2008-10-13 10:40:30 +0100 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /** |
Peter Hurley | f91e259 | 2013-03-06 07:20:56 -0500 | [diff] [blame] | 737 | * tty_vhangup_session - hangup session leader exit |
| 738 | * @tty: tty to hangup |
| 739 | * |
| 740 | * The session leader is exiting and hanging up its controlling terminal. |
| 741 | * Every process in the foreground process group is signalled SIGHUP. |
| 742 | * |
| 743 | * We do this synchronously so that when the syscall returns the process |
| 744 | * is complete. That guarantee is necessary for security reasons. |
| 745 | */ |
| 746 | |
Nicolas Pitre | a1235b3 | 2017-04-12 18:37:16 -0400 | [diff] [blame] | 747 | void tty_vhangup_session(struct tty_struct *tty) |
Peter Hurley | f91e259 | 2013-03-06 07:20:56 -0500 | [diff] [blame] | 748 | { |
Peter Hurley | d435cef | 2015-11-08 13:01:19 -0500 | [diff] [blame] | 749 | tty_debug_hangup(tty, "session hangup\n"); |
Peter Hurley | f91e259 | 2013-03-06 07:20:56 -0500 | [diff] [blame] | 750 | __tty_hangup(tty, 1); |
| 751 | } |
| 752 | |
| 753 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 754 | * tty_hung_up_p - was tty hung up |
| 755 | * @filp: file pointer of tty |
| 756 | * |
| 757 | * Return true if the tty has been subject to a vhangup or a carrier |
| 758 | * loss |
| 759 | */ |
| 760 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 761 | int tty_hung_up_p(struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { |
Alan Cox | ed3f0af | 2017-01-16 16:54:29 -0600 | [diff] [blame] | 763 | return (filp && filp->f_op == &hung_up_tty_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | EXPORT_SYMBOL(tty_hung_up_p); |
| 767 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 768 | /** |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 769 | * stop_tty - propagate flow control |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 770 | * @tty: tty to stop |
| 771 | * |
Peter Hurley | 01adc80 | 2014-09-10 15:06:32 -0400 | [diff] [blame] | 772 | * Perform flow control to the driver. May be called |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 773 | * on an already stopped device and will not re-call the driver |
| 774 | * method. |
| 775 | * |
| 776 | * This functionality is used by both the line disciplines for |
| 777 | * halting incoming flow and by the driver. It may therefore be |
| 778 | * called from any context, may be under the tty atomic_write_lock |
| 779 | * but not always. |
| 780 | * |
| 781 | * Locking: |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 782 | * flow.lock |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 783 | */ |
| 784 | |
Peter Hurley | f9e053d | 2014-09-10 15:06:31 -0400 | [diff] [blame] | 785 | void __stop_tty(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | { |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 787 | if (tty->flow.stopped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | return; |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 789 | tty->flow.stopped = true; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 790 | if (tty->ops->stop) |
Peter Hurley | c961bfb | 2014-11-05 12:26:25 -0500 | [diff] [blame] | 791 | tty->ops->stop(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Peter Hurley | f9e053d | 2014-09-10 15:06:31 -0400 | [diff] [blame] | 794 | void stop_tty(struct tty_struct *tty) |
| 795 | { |
| 796 | unsigned long flags; |
| 797 | |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 798 | spin_lock_irqsave(&tty->flow.lock, flags); |
Peter Hurley | f9e053d | 2014-09-10 15:06:31 -0400 | [diff] [blame] | 799 | __stop_tty(tty); |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 800 | spin_unlock_irqrestore(&tty->flow.lock, flags); |
Peter Hurley | f9e053d | 2014-09-10 15:06:31 -0400 | [diff] [blame] | 801 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | EXPORT_SYMBOL(stop_tty); |
| 803 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 804 | /** |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 805 | * start_tty - propagate flow control |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 806 | * @tty: tty to start |
| 807 | * |
Peter Hurley | 01adc80 | 2014-09-10 15:06:32 -0400 | [diff] [blame] | 808 | * Start a tty that has been stopped if at all possible. If this |
| 809 | * tty was previous stopped and is now being started, the driver |
| 810 | * start method is invoked and the line discipline woken. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 811 | * |
| 812 | * Locking: |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 813 | * flow.lock |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 814 | */ |
| 815 | |
Peter Hurley | f9e053d | 2014-09-10 15:06:31 -0400 | [diff] [blame] | 816 | void __start_tty(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | { |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 818 | if (!tty->flow.stopped || tty->flow.tco_stopped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | return; |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 820 | tty->flow.stopped = false; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 821 | if (tty->ops->start) |
Peter Hurley | c961bfb | 2014-11-05 12:26:25 -0500 | [diff] [blame] | 822 | tty->ops->start(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | tty_wakeup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Peter Hurley | f9e053d | 2014-09-10 15:06:31 -0400 | [diff] [blame] | 826 | void start_tty(struct tty_struct *tty) |
| 827 | { |
| 828 | unsigned long flags; |
| 829 | |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 830 | spin_lock_irqsave(&tty->flow.lock, flags); |
Peter Hurley | f9e053d | 2014-09-10 15:06:31 -0400 | [diff] [blame] | 831 | __start_tty(tty); |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 832 | spin_unlock_irqrestore(&tty->flow.lock, flags); |
Peter Hurley | f9e053d | 2014-09-10 15:06:31 -0400 | [diff] [blame] | 833 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | EXPORT_SYMBOL(start_tty); |
| 835 | |
Arnd Bergmann | c884f87 | 2018-06-18 16:16:26 +0200 | [diff] [blame] | 836 | static void tty_update_time(struct timespec64 *time) |
Jiri Slaby | 37b7f3c | 2013-04-26 13:48:53 +0200 | [diff] [blame] | 837 | { |
Arnd Bergmann | c884f87 | 2018-06-18 16:16:26 +0200 | [diff] [blame] | 838 | time64_t sec = ktime_get_real_seconds(); |
Greg Kroah-Hartman | fbf4763 | 2015-03-26 23:10:27 +0100 | [diff] [blame] | 839 | |
| 840 | /* |
| 841 | * We only care if the two values differ in anything other than the |
| 842 | * lower three bits (i.e every 8 seconds). If so, then we can update |
| 843 | * the time of the tty device, otherwise it could be construded as a |
| 844 | * security leak to let userspace know the exact timing of the tty. |
| 845 | */ |
| 846 | if ((sec ^ time->tv_sec) & ~7) |
Jiri Slaby | 37b7f3c | 2013-04-26 13:48:53 +0200 | [diff] [blame] | 847 | time->tv_sec = sec; |
| 848 | } |
| 849 | |
Linus Torvalds | 3b830a9 | 2021-01-18 13:31:30 -0800 | [diff] [blame] | 850 | /* |
| 851 | * Iterate on the ldisc ->read() function until we've gotten all |
| 852 | * the data the ldisc has for us. |
| 853 | * |
| 854 | * The "cookie" is something that the ldisc read function can fill |
| 855 | * in to let us know that there is more data to be had. |
| 856 | * |
| 857 | * We promise to continue to call the ldisc until it stops returning |
| 858 | * data or clears the cookie. The cookie may be something that the |
| 859 | * ldisc maintains state for and needs to free. |
| 860 | */ |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 861 | static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, |
| 862 | struct file *file, struct iov_iter *to) |
Linus Torvalds | 3b830a9 | 2021-01-18 13:31:30 -0800 | [diff] [blame] | 863 | { |
| 864 | int retval = 0; |
| 865 | void *cookie = NULL; |
| 866 | unsigned long offset = 0; |
| 867 | char kernel_buf[64]; |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 868 | size_t count = iov_iter_count(to); |
Linus Torvalds | 3b830a9 | 2021-01-18 13:31:30 -0800 | [diff] [blame] | 869 | |
| 870 | do { |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 871 | int size, copied; |
Linus Torvalds | 3b830a9 | 2021-01-18 13:31:30 -0800 | [diff] [blame] | 872 | |
| 873 | size = count > sizeof(kernel_buf) ? sizeof(kernel_buf) : count; |
| 874 | size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset); |
| 875 | if (!size) |
| 876 | break; |
| 877 | |
Linus Torvalds | 3b830a9 | 2021-01-18 13:31:30 -0800 | [diff] [blame] | 878 | if (size < 0) { |
Linus Torvalds | e71a8d5 | 2021-01-21 10:17:25 -0800 | [diff] [blame] | 879 | /* Did we have an earlier error (ie -EFAULT)? */ |
| 880 | if (retval) |
| 881 | break; |
| 882 | retval = size; |
| 883 | |
| 884 | /* |
| 885 | * -EOVERFLOW means we didn't have enough space |
| 886 | * for a whole packet, and we shouldn't return |
| 887 | * a partial result. |
| 888 | */ |
| 889 | if (retval == -EOVERFLOW) |
| 890 | offset = 0; |
| 891 | break; |
Linus Torvalds | 3b830a9 | 2021-01-18 13:31:30 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 894 | copied = copy_to_iter(kernel_buf, size, to); |
| 895 | offset += copied; |
| 896 | count -= copied; |
Linus Torvalds | 3b830a9 | 2021-01-18 13:31:30 -0800 | [diff] [blame] | 897 | |
| 898 | /* |
| 899 | * If the user copy failed, we still need to do another ->read() |
| 900 | * call if we had a cookie to let the ldisc clear up. |
| 901 | * |
| 902 | * But make sure size is zeroed. |
| 903 | */ |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 904 | if (unlikely(copied != size)) { |
Linus Torvalds | 3b830a9 | 2021-01-18 13:31:30 -0800 | [diff] [blame] | 905 | count = 0; |
| 906 | retval = -EFAULT; |
| 907 | } |
| 908 | } while (cookie); |
| 909 | |
| 910 | /* We always clear tty buffer in case they contained passwords */ |
| 911 | memzero_explicit(kernel_buf, sizeof(kernel_buf)); |
| 912 | return offset ? offset : retval; |
| 913 | } |
| 914 | |
| 915 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 916 | /** |
| 917 | * tty_read - read method for tty device files |
| 918 | * @file: pointer to tty file |
| 919 | * @buf: user buffer |
| 920 | * @count: size of user buffer |
| 921 | * @ppos: unused |
| 922 | * |
| 923 | * Perform the read system call function on this terminal device. Checks |
| 924 | * for hung up devices before calling the line discipline method. |
| 925 | * |
| 926 | * Locking: |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 927 | * Locks the line discipline internally while needed. Multiple |
| 928 | * read calls may be outstanding in parallel. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 929 | */ |
| 930 | |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 931 | static ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | { |
| 933 | int i; |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 934 | struct file *file = iocb->ki_filp; |
Jiri Slaby | 37b7f3c | 2013-04-26 13:48:53 +0200 | [diff] [blame] | 935 | struct inode *inode = file_inode(file); |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 936 | struct tty_struct *tty = file_tty(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | struct tty_ldisc *ld; |
| 938 | |
Jiri Slaby | 37b7f3c | 2013-04-26 13:48:53 +0200 | [diff] [blame] | 939 | if (tty_paranoia_check(tty, inode, "tty_read")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | return -EIO; |
Peter Hurley | 18900ca | 2016-04-09 17:06:48 -0700 | [diff] [blame] | 941 | if (!tty || tty_io_error(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | return -EIO; |
| 943 | |
| 944 | /* We want to wait for the line discipline to sort out in this |
| 945 | situation */ |
| 946 | ld = tty_ldisc_ref_wait(tty); |
Peter Hurley | e55afd1 | 2016-01-10 22:41:01 -0800 | [diff] [blame] | 947 | if (!ld) |
Linus Torvalds | ddc5fda | 2021-01-21 10:08:15 -0800 | [diff] [blame] | 948 | return hung_up_tty_read(iocb, to); |
Linus Torvalds | 3b830a9 | 2021-01-18 13:31:30 -0800 | [diff] [blame] | 949 | i = -EIO; |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 950 | if (ld->ops->read) |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 951 | i = iterate_tty_read(ld, tty, file, to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | tty_ldisc_deref(ld); |
Jiri Slaby | b0de59b | 2013-02-15 15:25:05 +0100 | [diff] [blame] | 953 | |
Arnd Bergmann | c884f87 | 2018-06-18 16:16:26 +0200 | [diff] [blame] | 954 | if (i > 0) |
| 955 | tty_update_time(&inode->i_atime); |
Jiri Slaby | 37b7f3c | 2013-04-26 13:48:53 +0200 | [diff] [blame] | 956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | return i; |
| 958 | } |
| 959 | |
Peter Hurley | 136d525 | 2014-09-10 15:06:34 -0400 | [diff] [blame] | 960 | static void tty_write_unlock(struct tty_struct *tty) |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 961 | { |
| 962 | mutex_unlock(&tty->atomic_write_lock); |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 963 | wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Peter Hurley | 136d525 | 2014-09-10 15:06:34 -0400 | [diff] [blame] | 966 | static int tty_write_lock(struct tty_struct *tty, int ndelay) |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 967 | { |
| 968 | if (!mutex_trylock(&tty->atomic_write_lock)) { |
| 969 | if (ndelay) |
| 970 | return -EAGAIN; |
| 971 | if (mutex_lock_interruptible(&tty->atomic_write_lock)) |
| 972 | return -ERESTARTSYS; |
| 973 | } |
| 974 | return 0; |
| 975 | } |
| 976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | /* |
| 978 | * Split writes up in sane blocksizes to avoid |
| 979 | * denial-of-service type attacks |
| 980 | */ |
| 981 | static inline ssize_t do_tty_write( |
| 982 | ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), |
| 983 | struct tty_struct *tty, |
| 984 | struct file *file, |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 985 | struct iov_iter *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | { |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 987 | size_t count = iov_iter_count(from); |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 988 | ssize_t ret, written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | unsigned int chunk; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 990 | |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 991 | ret = tty_write_lock(tty, file->f_flags & O_NDELAY); |
| 992 | if (ret < 0) |
| 993 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | * We chunk up writes into a temporary buffer. This |
| 997 | * simplifies low-level drivers immensely, since they |
| 998 | * don't have locking issues and user mode accesses. |
| 999 | * |
| 1000 | * But if TTY_NO_WRITE_SPLIT is set, we should use a |
| 1001 | * big chunk-size.. |
| 1002 | * |
| 1003 | * The default chunk-size is 2kB, because the NTTY |
| 1004 | * layer has problems with bigger chunks. It will |
| 1005 | * claim to be able to handle more characters than |
| 1006 | * it actually does. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1007 | * |
| 1008 | * FIXME: This can probably go away now except that 64K chunks |
| 1009 | * are too likely to fail unless switched to vmalloc... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | */ |
| 1011 | chunk = 2048; |
| 1012 | if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) |
| 1013 | chunk = 65536; |
| 1014 | if (count < chunk) |
| 1015 | chunk = count; |
| 1016 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1017 | /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | if (tty->write_cnt < chunk) { |
Jason Wessel | 402fda9 | 2008-10-13 10:45:36 +0100 | [diff] [blame] | 1019 | unsigned char *buf_chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
| 1021 | if (chunk < 1024) |
| 1022 | chunk = 1024; |
| 1023 | |
Jason Wessel | 402fda9 | 2008-10-13 10:45:36 +0100 | [diff] [blame] | 1024 | buf_chunk = kmalloc(chunk, GFP_KERNEL); |
| 1025 | if (!buf_chunk) { |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 1026 | ret = -ENOMEM; |
| 1027 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | kfree(tty->write_buf); |
| 1030 | tty->write_cnt = chunk; |
Jason Wessel | 402fda9 | 2008-10-13 10:45:36 +0100 | [diff] [blame] | 1031 | tty->write_buf = buf_chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | /* Do the write .. */ |
| 1035 | for (;;) { |
| 1036 | size_t size = count; |
Xiaofei Tan | e73b240 | 2021-05-12 17:26:15 +0800 | [diff] [blame] | 1037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | if (size > chunk) |
| 1039 | size = chunk; |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 1040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | ret = -EFAULT; |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 1042 | if (copy_from_iter(tty->write_buf, size, from) != size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | break; |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 1044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | ret = write(tty, file, tty->write_buf, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | if (ret <= 0) |
| 1047 | break; |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 1048 | |
Linus Torvalds | 3342ff2 | 2021-02-20 21:15:00 -0800 | [diff] [blame] | 1049 | written += ret; |
| 1050 | if (ret > size) |
| 1051 | break; |
| 1052 | |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 1053 | /* FIXME! Have Al check this! */ |
| 1054 | if (ret != size) |
| 1055 | iov_iter_revert(from, size-ret); |
| 1056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | count -= ret; |
| 1058 | if (!count) |
| 1059 | break; |
| 1060 | ret = -ERESTARTSYS; |
| 1061 | if (signal_pending(current)) |
| 1062 | break; |
| 1063 | cond_resched(); |
| 1064 | } |
Jiri Slaby | 37b7f3c | 2013-04-26 13:48:53 +0200 | [diff] [blame] | 1065 | if (written) { |
Arnd Bergmann | c884f87 | 2018-06-18 16:16:26 +0200 | [diff] [blame] | 1066 | tty_update_time(&file_inode(file)->i_mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | ret = written; |
Jiri Slaby | 37b7f3c | 2013-04-26 13:48:53 +0200 | [diff] [blame] | 1068 | } |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 1069 | out: |
| 1070 | tty_write_unlock(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | return ret; |
| 1072 | } |
| 1073 | |
Alan Cox | 95f9bfc | 2008-10-13 10:39:23 +0100 | [diff] [blame] | 1074 | /** |
| 1075 | * tty_write_message - write a message to a certain tty, not just the console. |
| 1076 | * @tty: the destination tty_struct |
| 1077 | * @msg: the message to write |
| 1078 | * |
| 1079 | * This is used for messages that need to be redirected to a specific tty. |
| 1080 | * We don't put it into the syslog queue right now maybe in the future if |
| 1081 | * really needed. |
| 1082 | * |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 1083 | * We must still hold the BTM and test the CLOSING flag for the moment. |
Alan Cox | 95f9bfc | 2008-10-13 10:39:23 +0100 | [diff] [blame] | 1084 | */ |
| 1085 | |
| 1086 | void tty_write_message(struct tty_struct *tty, char *msg) |
| 1087 | { |
Alan Cox | 95f9bfc | 2008-10-13 10:39:23 +0100 | [diff] [blame] | 1088 | if (tty) { |
| 1089 | mutex_lock(&tty->atomic_write_lock); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 1090 | tty_lock(tty); |
Peter Hurley | 4b41b95 | 2015-10-10 20:28:44 -0400 | [diff] [blame] | 1091 | if (tty->ops->write && tty->count > 0) |
Alan Cox | 95f9bfc | 2008-10-13 10:39:23 +0100 | [diff] [blame] | 1092 | tty->ops->write(tty, msg, strlen(msg)); |
Peter Hurley | 4b41b95 | 2015-10-10 20:28:44 -0400 | [diff] [blame] | 1093 | tty_unlock(tty); |
Alan Cox | 95f9bfc | 2008-10-13 10:39:23 +0100 | [diff] [blame] | 1094 | tty_write_unlock(tty); |
| 1095 | } |
Alan Cox | 95f9bfc | 2008-10-13 10:39:23 +0100 | [diff] [blame] | 1096 | return; |
| 1097 | } |
| 1098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1100 | /** |
| 1101 | * tty_write - write method for tty device file |
| 1102 | * @file: tty file pointer |
| 1103 | * @buf: user data to write |
| 1104 | * @count: bytes to write |
| 1105 | * @ppos: unused |
| 1106 | * |
| 1107 | * Write data to a tty device via the line discipline. |
| 1108 | * |
| 1109 | * Locking: |
| 1110 | * Locks the line discipline as required |
| 1111 | * Writes to the tty driver are serialized by the atomic_write_lock |
| 1112 | * and are then processed in chunks to the device. The line discipline |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1113 | * write method will not be invoked in parallel for each device. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1114 | */ |
| 1115 | |
Linus Torvalds | a9cbbb8 | 2021-01-29 12:28:20 -0800 | [diff] [blame] | 1116 | static ssize_t file_tty_write(struct file *file, struct kiocb *iocb, struct iov_iter *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 1118 | struct tty_struct *tty = file_tty(file); |
Xiaofei Tan | 395e783 | 2021-05-12 17:26:14 +0800 | [diff] [blame] | 1119 | struct tty_ldisc *ld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | ssize_t ret; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1121 | |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 1122 | if (tty_paranoia_check(tty, file_inode(file), "tty_write")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | return -EIO; |
Peter Hurley | 18900ca | 2016-04-09 17:06:48 -0700 | [diff] [blame] | 1124 | if (!tty || !tty->ops->write || tty_io_error(tty)) |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1125 | return -EIO; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1126 | /* Short term debug to catch buggy drivers */ |
| 1127 | if (tty->ops->write_room == NULL) |
Peter Hurley | 339f36b | 2015-11-08 13:01:13 -0500 | [diff] [blame] | 1128 | tty_err(tty, "missing write_room method\n"); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1129 | ld = tty_ldisc_ref_wait(tty); |
Linus Torvalds | 1774985 | 2021-01-21 10:04:27 -0800 | [diff] [blame] | 1130 | if (!ld) |
| 1131 | return hung_up_tty_write(iocb, from); |
| 1132 | if (!ld->ops->write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | ret = -EIO; |
| 1134 | else |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 1135 | ret = do_tty_write(ld->ops->write, tty, file, from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | tty_ldisc_deref(ld); |
| 1137 | return ret; |
| 1138 | } |
| 1139 | |
Linus Torvalds | a9cbbb8 | 2021-01-29 12:28:20 -0800 | [diff] [blame] | 1140 | static ssize_t tty_write(struct kiocb *iocb, struct iov_iter *from) |
| 1141 | { |
| 1142 | return file_tty_write(iocb->ki_filp, iocb, from); |
| 1143 | } |
| 1144 | |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 1145 | ssize_t redirected_tty_write(struct kiocb *iocb, struct iov_iter *iter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | { |
| 1147 | struct file *p = NULL; |
| 1148 | |
| 1149 | spin_lock(&redirect_lock); |
Al Viro | cb0942b | 2012-08-27 14:48:26 -0400 | [diff] [blame] | 1150 | if (redirect) |
| 1151 | p = get_file(redirect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | spin_unlock(&redirect_lock); |
| 1153 | |
Linus Torvalds | a9cbbb8 | 2021-01-29 12:28:20 -0800 | [diff] [blame] | 1154 | /* |
| 1155 | * We know the redirected tty is just another tty, we can can |
| 1156 | * call file_tty_write() directly with that file pointer. |
| 1157 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | if (p) { |
| 1159 | ssize_t res; |
Xiaofei Tan | e73b240 | 2021-05-12 17:26:15 +0800 | [diff] [blame] | 1160 | |
Linus Torvalds | a9cbbb8 | 2021-01-29 12:28:20 -0800 | [diff] [blame] | 1161 | res = file_tty_write(p, iocb, iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | fput(p); |
| 1163 | return res; |
| 1164 | } |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 1165 | return tty_write(iocb, iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Lee Jones | 08aa504 | 2020-11-04 19:35:25 +0000 | [diff] [blame] | 1168 | /* |
Peter Hurley | 136d525 | 2014-09-10 15:06:34 -0400 | [diff] [blame] | 1169 | * tty_send_xchar - send priority character |
| 1170 | * |
| 1171 | * Send a high priority character to the tty even if stopped |
| 1172 | * |
| 1173 | * Locking: none for xchar method, write ordering for write method. |
| 1174 | */ |
| 1175 | |
| 1176 | int tty_send_xchar(struct tty_struct *tty, char ch) |
| 1177 | { |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 1178 | bool was_stopped = tty->flow.stopped; |
Peter Hurley | 136d525 | 2014-09-10 15:06:34 -0400 | [diff] [blame] | 1179 | |
| 1180 | if (tty->ops->send_xchar) { |
Peter Hurley | ee0c1a6 | 2015-11-11 08:03:54 -0500 | [diff] [blame] | 1181 | down_read(&tty->termios_rwsem); |
Peter Hurley | 136d525 | 2014-09-10 15:06:34 -0400 | [diff] [blame] | 1182 | tty->ops->send_xchar(tty, ch); |
Peter Hurley | ee0c1a6 | 2015-11-11 08:03:54 -0500 | [diff] [blame] | 1183 | up_read(&tty->termios_rwsem); |
Peter Hurley | 136d525 | 2014-09-10 15:06:34 -0400 | [diff] [blame] | 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | if (tty_write_lock(tty, 0) < 0) |
| 1188 | return -ERESTARTSYS; |
| 1189 | |
Peter Hurley | ee0c1a6 | 2015-11-11 08:03:54 -0500 | [diff] [blame] | 1190 | down_read(&tty->termios_rwsem); |
Peter Hurley | 136d525 | 2014-09-10 15:06:34 -0400 | [diff] [blame] | 1191 | if (was_stopped) |
| 1192 | start_tty(tty); |
| 1193 | tty->ops->write(tty, &ch, 1); |
| 1194 | if (was_stopped) |
| 1195 | stop_tty(tty); |
Peter Hurley | ee0c1a6 | 2015-11-11 08:03:54 -0500 | [diff] [blame] | 1196 | up_read(&tty->termios_rwsem); |
Peter Hurley | 136d525 | 2014-09-10 15:06:34 -0400 | [diff] [blame] | 1197 | tty_write_unlock(tty); |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1201 | /** |
| 1202 | * pty_line_name - generate name for a pty |
| 1203 | * @driver: the tty driver in use |
| 1204 | * @index: the minor number |
| 1205 | * @p: output buffer of at least 6 bytes |
| 1206 | * |
| 1207 | * Generate a name from a driver reference and write it to the output |
| 1208 | * buffer. |
| 1209 | * |
| 1210 | * Locking: None |
| 1211 | */ |
| 1212 | static void pty_line_name(struct tty_driver *driver, int index, char *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | { |
Jiri Slaby | a846dcf | 2021-03-02 07:22:06 +0100 | [diff] [blame] | 1214 | static const char ptychar[] = "pqrstuvwxyzabcde"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | int i = index + driver->name_base; |
| 1216 | /* ->name is initialized to "ttyp", but "tty" is expected */ |
| 1217 | sprintf(p, "%s%c%x", |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1218 | driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, |
| 1219 | ptychar[i >> 4 & 0xf], i & 0xf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1222 | /** |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 1223 | * tty_line_name - generate name for a tty |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1224 | * @driver: the tty driver in use |
| 1225 | * @index: the minor number |
| 1226 | * @p: output buffer of at least 7 bytes |
| 1227 | * |
| 1228 | * Generate a name from a driver reference and write it to the output |
Greg Kroah-Hartman | 5c0a245 | 2014-02-22 14:31:04 -0800 | [diff] [blame] | 1229 | * buffer. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1230 | * |
| 1231 | * Locking: None |
| 1232 | */ |
Hannes Reinecke | 723abd8 | 2014-02-27 12:30:51 +0100 | [diff] [blame] | 1233 | static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | { |
Jiri Slaby | 0019b40 | 2012-08-08 22:26:43 +0200 | [diff] [blame] | 1235 | if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE) |
Hannes Reinecke | 723abd8 | 2014-02-27 12:30:51 +0100 | [diff] [blame] | 1236 | return sprintf(p, "%s", driver->name); |
Jiri Slaby | 0019b40 | 2012-08-08 22:26:43 +0200 | [diff] [blame] | 1237 | else |
Hannes Reinecke | 723abd8 | 2014-02-27 12:30:51 +0100 | [diff] [blame] | 1238 | return sprintf(p, "%s%d", driver->name, |
| 1239 | index + driver->name_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 1242 | /** |
| 1243 | * tty_driver_lookup_tty() - find an existing tty, if any |
| 1244 | * @driver: the driver for the tty |
Lee Jones | 08aa504 | 2020-11-04 19:35:25 +0000 | [diff] [blame] | 1245 | * @file: file object |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 1246 | * @idx: the minor number |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1247 | * |
Peter Hurley | aa3cb81 | 2014-11-05 12:12:51 -0500 | [diff] [blame] | 1248 | * Return the tty, if found. If not found, return NULL or ERR_PTR() if the |
| 1249 | * driver lookup() method returns an error. |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1250 | * |
Peter Hurley | aa3cb81 | 2014-11-05 12:12:51 -0500 | [diff] [blame] | 1251 | * Locking: tty_mutex must be held. If the tty is found, bump the tty kref. |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1252 | */ |
Jason Wessel | a47d545 | 2009-01-02 13:43:04 +0000 | [diff] [blame] | 1253 | static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, |
Linus Torvalds | 8ead9dd | 2016-04-25 20:04:08 -0700 | [diff] [blame] | 1254 | struct file *file, int idx) |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1255 | { |
Peter Hurley | aa3cb81 | 2014-11-05 12:12:51 -0500 | [diff] [blame] | 1256 | struct tty_struct *tty; |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1257 | |
Peter Hurley | aa3cb81 | 2014-11-05 12:12:51 -0500 | [diff] [blame] | 1258 | if (driver->ops->lookup) |
Okash Khawaja | 12e84c7 | 2017-05-15 18:45:32 +0100 | [diff] [blame] | 1259 | if (!file) |
| 1260 | tty = ERR_PTR(-EIO); |
| 1261 | else |
| 1262 | tty = driver->ops->lookup(driver, file, idx); |
Peter Hurley | aa3cb81 | 2014-11-05 12:12:51 -0500 | [diff] [blame] | 1263 | else |
| 1264 | tty = driver->ttys[idx]; |
| 1265 | |
| 1266 | if (!IS_ERR(tty)) |
| 1267 | tty_kref_get(tty); |
| 1268 | return tty; |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1269 | } |
| 1270 | |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 1271 | /** |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1272 | * tty_init_termios - helper for termios setup |
| 1273 | * @tty: the tty to set up |
| 1274 | * |
Johan Hovold | 8daa89e | 2019-04-18 18:05:54 +0200 | [diff] [blame] | 1275 | * Initialise the termios structure for this tty. This runs under |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1276 | * the tty_mutex currently so we can be relaxed about ordering. |
| 1277 | */ |
| 1278 | |
Peter Hurley | a3123fd | 2016-01-09 21:13:48 -0800 | [diff] [blame] | 1279 | void tty_init_termios(struct tty_struct *tty) |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1280 | { |
Alan Cox | fe6e29f | 2008-10-13 10:44:08 +0100 | [diff] [blame] | 1281 | struct ktermios *tp; |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1282 | int idx = tty->index; |
| 1283 | |
Alan Cox | 36b3c07 | 2012-07-17 17:06:57 +0100 | [diff] [blame] | 1284 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) |
| 1285 | tty->termios = tty->driver->init_termios; |
| 1286 | else { |
| 1287 | /* Check for lazy saved data */ |
| 1288 | tp = tty->driver->termios[idx]; |
Peter Hurley | ece5340 | 2016-01-10 22:40:57 -0800 | [diff] [blame] | 1289 | if (tp != NULL) { |
Alan Cox | 36b3c07 | 2012-07-17 17:06:57 +0100 | [diff] [blame] | 1290 | tty->termios = *tp; |
Peter Hurley | ece5340 | 2016-01-10 22:40:57 -0800 | [diff] [blame] | 1291 | tty->termios.c_line = tty->driver->init_termios.c_line; |
| 1292 | } else |
Alan Cox | 36b3c07 | 2012-07-17 17:06:57 +0100 | [diff] [blame] | 1293 | tty->termios = tty->driver->init_termios; |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1294 | } |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1295 | /* Compatibility until drivers always set this */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1296 | tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); |
| 1297 | tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1298 | } |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 1299 | EXPORT_SYMBOL_GPL(tty_init_termios); |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1300 | |
Jiri Slaby | 66d450e | 2012-01-30 21:14:28 +0100 | [diff] [blame] | 1301 | int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) |
| 1302 | { |
Peter Hurley | a3123fd | 2016-01-09 21:13:48 -0800 | [diff] [blame] | 1303 | tty_init_termios(tty); |
Jiri Slaby | 66d450e | 2012-01-30 21:14:28 +0100 | [diff] [blame] | 1304 | tty_driver_kref_get(driver); |
| 1305 | tty->count++; |
| 1306 | driver->ttys[tty->index] = tty; |
| 1307 | return 0; |
| 1308 | } |
| 1309 | EXPORT_SYMBOL_GPL(tty_standard_install); |
| 1310 | |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1311 | /** |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 1312 | * tty_driver_install_tty() - install a tty entry in the driver |
| 1313 | * @driver: the driver for the tty |
| 1314 | * @tty: the tty |
| 1315 | * |
| 1316 | * Install a tty object into the driver tables. The tty->index field |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1317 | * will be set by the time this is called. This method is responsible |
| 1318 | * for ensuring any need additional structures are allocated and |
| 1319 | * configured. |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 1320 | * |
| 1321 | * Locking: tty_mutex for now |
| 1322 | */ |
| 1323 | static int tty_driver_install_tty(struct tty_driver *driver, |
| 1324 | struct tty_struct *tty) |
| 1325 | { |
Jiri Slaby | 66d450e | 2012-01-30 21:14:28 +0100 | [diff] [blame] | 1326 | return driver->ops->install ? driver->ops->install(driver, tty) : |
| 1327 | tty_standard_install(driver, tty); |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * tty_driver_remove_tty() - remove a tty from the driver tables |
| 1332 | * @driver: the driver for the tty |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 1333 | * @tty: tty to remove |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 1334 | * |
| 1335 | * Remvoe a tty object from the driver tables. The tty->index field |
| 1336 | * will be set by the time this is called. |
| 1337 | * |
| 1338 | * Locking: tty_mutex for now |
| 1339 | */ |
Peter Hurley | 05de87ed | 2016-01-09 21:13:49 -0800 | [diff] [blame] | 1340 | static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 1341 | { |
| 1342 | if (driver->ops->remove) |
| 1343 | driver->ops->remove(driver, tty); |
| 1344 | else |
| 1345 | driver->ttys[tty->index] = NULL; |
| 1346 | } |
| 1347 | |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 1348 | /** |
| 1349 | * tty_reopen() - fast re-open of an open tty |
| 1350 | * @tty: the tty to open |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1351 | * |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 1352 | * Return 0 on success, -errno on error. |
Peter Hurley | 5d93e74 | 2014-11-05 12:12:47 -0500 | [diff] [blame] | 1353 | * Re-opens on master ptys are not allowed and return -EIO. |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1354 | * |
Peter Hurley | 5d93e74 | 2014-11-05 12:12:47 -0500 | [diff] [blame] | 1355 | * Locking: Caller must hold tty_lock |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1356 | */ |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 1357 | static int tty_reopen(struct tty_struct *tty) |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1358 | { |
| 1359 | struct tty_driver *driver = tty->driver; |
Dmitry Safonov | d3736d8 | 2019-01-09 01:17:40 +0000 | [diff] [blame] | 1360 | struct tty_ldisc *ld; |
| 1361 | int retval = 0; |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1362 | |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1363 | if (driver->type == TTY_DRIVER_TYPE_PTY && |
Peter Hurley | 5d93e74 | 2014-11-05 12:12:47 -0500 | [diff] [blame] | 1364 | driver->subtype == PTY_TYPE_MASTER) |
| 1365 | return -EIO; |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1366 | |
Peter Hurley | 7f22f6c | 2016-01-09 21:13:45 -0800 | [diff] [blame] | 1367 | if (!tty->count) |
| 1368 | return -EAGAIN; |
| 1369 | |
Peter Hurley | 86f2c00 | 2014-12-30 10:39:25 -0500 | [diff] [blame] | 1370 | if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) |
| 1371 | return -EBUSY; |
| 1372 | |
Dmitry Safonov | d3736d8 | 2019-01-09 01:17:40 +0000 | [diff] [blame] | 1373 | ld = tty_ldisc_ref_wait(tty); |
| 1374 | if (ld) { |
| 1375 | tty_ldisc_deref(ld); |
| 1376 | } else { |
| 1377 | retval = tty_ldisc_lock(tty, 5 * HZ); |
| 1378 | if (retval) |
| 1379 | return retval; |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1380 | |
Dmitry Safonov | d3736d8 | 2019-01-09 01:17:40 +0000 | [diff] [blame] | 1381 | if (!tty->ldisc) |
| 1382 | retval = tty_ldisc_reinit(tty, tty->termios.c_line); |
| 1383 | tty_ldisc_unlock(tty); |
| 1384 | } |
Dmitry Safonov | cf62a1a | 2018-11-01 00:24:49 +0000 | [diff] [blame] | 1385 | |
| 1386 | if (retval == 0) |
| 1387 | tty->count++; |
| 1388 | |
Dmitry Safonov | fe32416 | 2018-09-18 00:52:52 +0100 | [diff] [blame] | 1389 | return retval; |
Sukadev Bhattiprolu | 2349970 | 2008-10-13 10:41:51 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1392 | /** |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 1393 | * tty_init_dev - initialise a tty device |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1394 | * @driver: tty driver we are opening a device on |
| 1395 | * @idx: device index |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1396 | * |
| 1397 | * Prepare a tty device. This may not be a "new" clean device but |
| 1398 | * could also be an active device. The pty drivers require special |
| 1399 | * handling because of this. |
| 1400 | * |
| 1401 | * Locking: |
| 1402 | * The function is called under the tty_mutex, which |
| 1403 | * protects us from the tty struct or driver itself going away. |
| 1404 | * |
| 1405 | * On exit the tty device has the line discipline attached and |
| 1406 | * a reference count of 1. If a pair was created for pty/tty use |
| 1407 | * and the other was a pty master then it too has a reference count of 1. |
| 1408 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | * WSH 06/09/97: Rewritten to remove races and properly clean up after a |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1410 | * failed open. The new code protects the open with a mutex, so it's |
| 1411 | * really quite straightforward. The mutex locking can probably be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | * relaxed for the (most common) case of reopening a tty. |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 1413 | * |
| 1414 | * Return: returned tty structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1416 | |
Konstantin Khlebnikov | 593a27c | 2012-01-05 13:04:21 +0400 | [diff] [blame] | 1417 | struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | { |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1419 | struct tty_struct *tty; |
Alan Cox | 73ec06f | 2008-10-13 10:42:29 +0100 | [diff] [blame] | 1420 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | /* |
| 1423 | * First time open is complex, especially for PTY devices. |
| 1424 | * This code guarantees that either everything succeeds and the |
| 1425 | * TTY is ready for operation, or else the table slots are vacated |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1426 | * and the allocated memory released. (Except that the termios |
Johan Hovold | 16b00ae | 2017-03-30 15:39:35 +0200 | [diff] [blame] | 1427 | * may be retained.) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | */ |
| 1429 | |
Alan Cox | 73ec06f | 2008-10-13 10:42:29 +0100 | [diff] [blame] | 1430 | if (!try_module_get(driver->owner)) |
| 1431 | return ERR_PTR(-ENODEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | |
Rasmus Villemoes | 2c964a2 | 2014-07-10 21:01:22 +0200 | [diff] [blame] | 1433 | tty = alloc_tty_struct(driver, idx); |
Jiri Slaby | d554350 | 2011-03-23 10:48:32 +0100 | [diff] [blame] | 1434 | if (!tty) { |
| 1435 | retval = -ENOMEM; |
| 1436 | goto err_module_put; |
| 1437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 1439 | tty_lock(tty); |
Alan Cox | 73ec06f | 2008-10-13 10:42:29 +0100 | [diff] [blame] | 1440 | retval = tty_driver_install_tty(driver, tty); |
Jiri Slaby | d554350 | 2011-03-23 10:48:32 +0100 | [diff] [blame] | 1441 | if (retval < 0) |
Peter Hurley | c8b710b | 2016-01-09 21:13:46 -0800 | [diff] [blame] | 1442 | goto err_free_tty; |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 1443 | |
Jiri Slaby | 04831dc | 2012-06-04 13:35:36 +0200 | [diff] [blame] | 1444 | if (!tty->port) |
| 1445 | tty->port = driver->ports[idx]; |
| 1446 | |
Jiri Slaby | 2ae0b31 | 2019-11-22 11:17:21 +0100 | [diff] [blame] | 1447 | if (WARN_RATELIMIT(!tty->port, |
| 1448 | "%s: %s driver does not set tty->port. This would crash the kernel. Fix the driver!\n", |
| 1449 | __func__, tty->driver->name)) { |
| 1450 | retval = -EINVAL; |
| 1451 | goto err_release_lock; |
| 1452 | } |
Jiri Slaby | 5d4121c | 2012-08-17 14:27:52 +0200 | [diff] [blame] | 1453 | |
Gaurav Kohli | b027e22 | 2018-01-23 13:16:34 +0530 | [diff] [blame] | 1454 | retval = tty_ldisc_lock(tty, 5 * HZ); |
| 1455 | if (retval) |
| 1456 | goto err_release_lock; |
Jiri Slaby | 967fab6 | 2012-10-18 22:26:46 +0200 | [diff] [blame] | 1457 | tty->port->itty = tty; |
| 1458 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1459 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | * Structures all installed ... call the ldisc open routines. |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1461 | * If we fail here just call release_tty to clean up. No need |
| 1462 | * to decrement the use counts, as release_tty doesn't care. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | */ |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 1464 | retval = tty_ldisc_setup(tty, tty->link); |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 1465 | if (retval) |
Jiri Slaby | d554350 | 2011-03-23 10:48:32 +0100 | [diff] [blame] | 1466 | goto err_release_tty; |
Gaurav Kohli | b027e22 | 2018-01-23 13:16:34 +0530 | [diff] [blame] | 1467 | tty_ldisc_unlock(tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 1468 | /* Return the tty locked so that it cannot vanish under the caller */ |
Alan Cox | 73ec06f | 2008-10-13 10:42:29 +0100 | [diff] [blame] | 1469 | return tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | |
Peter Hurley | c8b710b | 2016-01-09 21:13:46 -0800 | [diff] [blame] | 1471 | err_free_tty: |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 1472 | tty_unlock(tty); |
Jiri Slaby | d554350 | 2011-03-23 10:48:32 +0100 | [diff] [blame] | 1473 | free_tty_struct(tty); |
| 1474 | err_module_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | module_put(driver->owner); |
Jiri Slaby | d554350 | 2011-03-23 10:48:32 +0100 | [diff] [blame] | 1476 | return ERR_PTR(retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1478 | /* call the tty release_tty routine to clean out this slot */ |
Jiri Slaby | d554350 | 2011-03-23 10:48:32 +0100 | [diff] [blame] | 1479 | err_release_tty: |
Gaurav Kohli | b027e22 | 2018-01-23 13:16:34 +0530 | [diff] [blame] | 1480 | tty_ldisc_unlock(tty); |
Peter Hurley | 339f36b | 2015-11-08 13:01:13 -0500 | [diff] [blame] | 1481 | tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n", |
| 1482 | retval, idx); |
Gaurav Kohli | b027e22 | 2018-01-23 13:16:34 +0530 | [diff] [blame] | 1483 | err_release_lock: |
| 1484 | tty_unlock(tty); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1485 | release_tty(tty, idx); |
Alan Cox | 73ec06f | 2008-10-13 10:42:29 +0100 | [diff] [blame] | 1486 | return ERR_PTR(retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
Johan Hovold | f51ccf4 | 2018-12-04 17:00:36 +0100 | [diff] [blame] | 1489 | /** |
| 1490 | * tty_save_termios() - save tty termios data in driver table |
| 1491 | * @tty: tty whose termios data to save |
| 1492 | * |
| 1493 | * Locking: Caller guarantees serialisation with tty_init_termios(). |
| 1494 | */ |
| 1495 | void tty_save_termios(struct tty_struct *tty) |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 1496 | { |
| 1497 | struct ktermios *tp; |
| 1498 | int idx = tty->index; |
Alan Cox | 36b3c07 | 2012-07-17 17:06:57 +0100 | [diff] [blame] | 1499 | |
| 1500 | /* If the port is going to reset then it has no termios to save */ |
| 1501 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) |
| 1502 | return; |
| 1503 | |
| 1504 | /* Stash the termios data */ |
| 1505 | tp = tty->driver->termios[idx]; |
| 1506 | if (tp == NULL) { |
Gustavo A. R. Silva | a324189 | 2020-07-23 17:34:22 -0500 | [diff] [blame] | 1507 | tp = kmalloc(sizeof(*tp), GFP_KERNEL); |
Peter Hurley | 2508065 | 2015-11-08 13:01:11 -0500 | [diff] [blame] | 1508 | if (tp == NULL) |
Alan Cox | 36b3c07 | 2012-07-17 17:06:57 +0100 | [diff] [blame] | 1509 | return; |
Dan Carpenter | 4ac5d70 | 2012-07-24 12:51:52 +0100 | [diff] [blame] | 1510 | tty->driver->termios[idx] = tp; |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 1511 | } |
Alan Cox | 36b3c07 | 2012-07-17 17:06:57 +0100 | [diff] [blame] | 1512 | *tp = tty->termios; |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 1513 | } |
Johan Hovold | f51ccf4 | 2018-12-04 17:00:36 +0100 | [diff] [blame] | 1514 | EXPORT_SYMBOL_GPL(tty_save_termios); |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 1515 | |
Peter Hurley | a2965b7 | 2013-03-11 16:44:35 -0400 | [diff] [blame] | 1516 | /** |
Peter Hurley | 949aa64 | 2014-11-05 12:12:57 -0500 | [diff] [blame] | 1517 | * tty_flush_works - flush all works of a tty/pty pair |
| 1518 | * @tty: tty device to flush works for (or either end of a pty pair) |
Peter Hurley | a2965b7 | 2013-03-11 16:44:35 -0400 | [diff] [blame] | 1519 | * |
Peter Hurley | 949aa64 | 2014-11-05 12:12:57 -0500 | [diff] [blame] | 1520 | * Sync flush all works belonging to @tty (and the 'other' tty). |
Peter Hurley | a2965b7 | 2013-03-11 16:44:35 -0400 | [diff] [blame] | 1521 | */ |
| 1522 | static void tty_flush_works(struct tty_struct *tty) |
| 1523 | { |
| 1524 | flush_work(&tty->SAK_work); |
| 1525 | flush_work(&tty->hangup_work); |
Peter Hurley | 949aa64 | 2014-11-05 12:12:57 -0500 | [diff] [blame] | 1526 | if (tty->link) { |
| 1527 | flush_work(&tty->link->SAK_work); |
| 1528 | flush_work(&tty->link->hangup_work); |
| 1529 | } |
Peter Hurley | a2965b7 | 2013-03-11 16:44:35 -0400 | [diff] [blame] | 1530 | } |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 1531 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1532 | /** |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1533 | * release_one_tty - release tty structure memory |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 1534 | * @work: work of tty we are obliterating |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1535 | * |
| 1536 | * Releases memory associated with a tty structure, and clears out the |
| 1537 | * driver table slots. This function is called when a device is no longer |
| 1538 | * in use. It also gets called when setup of a device fails. |
| 1539 | * |
| 1540 | * Locking: |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1541 | * takes the file list lock internally when working on the list |
| 1542 | * of ttys that the driver keeps. |
Alan Cox | b50989d | 2009-09-19 13:13:22 -0700 | [diff] [blame] | 1543 | * |
| 1544 | * This method gets called from a work queue so that the driver private |
Dave Young | f278a2f | 2009-09-27 16:00:42 +0000 | [diff] [blame] | 1545 | * cleanup ops can sleep (needed for USB at least) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | */ |
Alan Cox | b50989d | 2009-09-19 13:13:22 -0700 | [diff] [blame] | 1547 | static void release_one_tty(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | { |
Alan Cox | b50989d | 2009-09-19 13:13:22 -0700 | [diff] [blame] | 1549 | struct tty_struct *tty = |
| 1550 | container_of(work, struct tty_struct, hangup_work); |
Alan Cox | 6f967f7 | 2008-10-13 10:37:36 +0100 | [diff] [blame] | 1551 | struct tty_driver *driver = tty->driver; |
Cyrill Gorcunov | b216df5 | 2014-08-08 00:26:15 +0400 | [diff] [blame] | 1552 | struct module *owner = driver->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | |
Dave Young | f278a2f | 2009-09-27 16:00:42 +0000 | [diff] [blame] | 1554 | if (tty->ops->cleanup) |
| 1555 | tty->ops->cleanup(tty); |
| 1556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | tty->magic = 0; |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 1558 | tty_driver_kref_put(driver); |
Cyrill Gorcunov | b216df5 | 2014-08-08 00:26:15 +0400 | [diff] [blame] | 1559 | module_put(owner); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1560 | |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 1561 | spin_lock(&tty->files_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | list_del_init(&tty->tty_files); |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 1563 | spin_unlock(&tty->files_lock); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1564 | |
Jiri Slaby | 64d608d | 2021-05-05 11:19:06 +0200 | [diff] [blame] | 1565 | put_pid(tty->ctrl.pgrp); |
| 1566 | put_pid(tty->ctrl.session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | free_tty_struct(tty); |
| 1568 | } |
| 1569 | |
Alan Cox | b50989d | 2009-09-19 13:13:22 -0700 | [diff] [blame] | 1570 | static void queue_release_one_tty(struct kref *kref) |
| 1571 | { |
| 1572 | struct tty_struct *tty = container_of(kref, struct tty_struct, kref); |
Dave Young | f278a2f | 2009-09-27 16:00:42 +0000 | [diff] [blame] | 1573 | |
Alan Cox | b50989d | 2009-09-19 13:13:22 -0700 | [diff] [blame] | 1574 | /* The hangup queue is now free so we can reuse it rather than |
| 1575 | waste a chunk of memory for each port */ |
| 1576 | INIT_WORK(&tty->hangup_work, release_one_tty); |
| 1577 | schedule_work(&tty->hangup_work); |
| 1578 | } |
| 1579 | |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1580 | /** |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1581 | * tty_kref_put - release a tty kref |
| 1582 | * @tty: tty device |
| 1583 | * |
| 1584 | * Release a reference to a tty device and if need be let the kref |
| 1585 | * layer destruct the object for us |
| 1586 | */ |
| 1587 | |
| 1588 | void tty_kref_put(struct tty_struct *tty) |
| 1589 | { |
| 1590 | if (tty) |
Alan Cox | b50989d | 2009-09-19 13:13:22 -0700 | [diff] [blame] | 1591 | kref_put(&tty->kref, queue_release_one_tty); |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1592 | } |
| 1593 | EXPORT_SYMBOL(tty_kref_put); |
| 1594 | |
| 1595 | /** |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1596 | * release_tty - release tty structure memory |
Lee Jones | 08aa504 | 2020-11-04 19:35:25 +0000 | [diff] [blame] | 1597 | * @tty: tty device release |
| 1598 | * @idx: index of the tty device release |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1599 | * |
| 1600 | * Release both @tty and a possible linked partner (think pty pair), |
| 1601 | * and decrement the refcount of the backing module. |
| 1602 | * |
| 1603 | * Locking: |
Alan Cox | d155255 | 2012-07-27 18:02:54 +0100 | [diff] [blame] | 1604 | * tty_mutex |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1605 | * takes the file list lock internally when working on the list |
| 1606 | * of ttys that the driver keeps. |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1607 | * |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1608 | */ |
| 1609 | static void release_tty(struct tty_struct *tty, int idx) |
| 1610 | { |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1611 | /* This should always be true but check for the moment */ |
| 1612 | WARN_ON(tty->index != idx); |
Alan Cox | d155255 | 2012-07-27 18:02:54 +0100 | [diff] [blame] | 1613 | WARN_ON(!mutex_is_locked(&tty_mutex)); |
Alan Cox | 36b3c07 | 2012-07-17 17:06:57 +0100 | [diff] [blame] | 1614 | if (tty->ops->shutdown) |
| 1615 | tty->ops->shutdown(tty); |
Johan Hovold | f51ccf4 | 2018-12-04 17:00:36 +0100 | [diff] [blame] | 1616 | tty_save_termios(tty); |
Alan Cox | 36b3c07 | 2012-07-17 17:06:57 +0100 | [diff] [blame] | 1617 | tty_driver_remove_tty(tty->driver, tty); |
Matthias Reichl | 4466d6d | 2020-11-05 13:34:32 +0100 | [diff] [blame] | 1618 | if (tty->port) |
| 1619 | tty->port->itty = NULL; |
Peter Hurley | 64e377d | 2013-06-15 09:01:00 -0400 | [diff] [blame] | 1620 | if (tty->link) |
| 1621 | tty->link->port->itty = NULL; |
Matthias Reichl | 4466d6d | 2020-11-05 13:34:32 +0100 | [diff] [blame] | 1622 | if (tty->port) |
| 1623 | tty_buffer_cancel_work(tty->port); |
Sahara | 2b022ab | 2017-12-13 09:10:48 +0400 | [diff] [blame] | 1624 | if (tty->link) |
| 1625 | tty_buffer_cancel_work(tty->link->port); |
Alan Cox | 36b3c07 | 2012-07-17 17:06:57 +0100 | [diff] [blame] | 1626 | |
Markus Elfring | a211b1a | 2014-11-21 13:42:29 +0100 | [diff] [blame] | 1627 | tty_kref_put(tty->link); |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1628 | tty_kref_put(tty); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1629 | } |
| 1630 | |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 1631 | /** |
Jiri Slaby | 955787ca | 2011-11-11 10:47:23 +0100 | [diff] [blame] | 1632 | * tty_release_checks - check a tty before real release |
| 1633 | * @tty: tty to check |
Jiri Slaby | 955787ca | 2011-11-11 10:47:23 +0100 | [diff] [blame] | 1634 | * @idx: index of the tty |
| 1635 | * |
| 1636 | * Performs some paranoid checking before true release of the @tty. |
| 1637 | * This is a no-op unless TTY_PARANOIA_CHECK is defined. |
| 1638 | */ |
Peter Hurley | 359b9fb | 2014-11-05 12:12:59 -0500 | [diff] [blame] | 1639 | static int tty_release_checks(struct tty_struct *tty, int idx) |
Jiri Slaby | 955787ca | 2011-11-11 10:47:23 +0100 | [diff] [blame] | 1640 | { |
| 1641 | #ifdef TTY_PARANOIA_CHECK |
| 1642 | if (idx < 0 || idx >= tty->driver->num) { |
Peter Hurley | e2dfa3d | 2015-07-12 22:49:08 -0400 | [diff] [blame] | 1643 | tty_debug(tty, "bad idx %d\n", idx); |
Jiri Slaby | 955787ca | 2011-11-11 10:47:23 +0100 | [diff] [blame] | 1644 | return -1; |
| 1645 | } |
| 1646 | |
| 1647 | /* not much to check for devpts */ |
| 1648 | if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) |
| 1649 | return 0; |
| 1650 | |
| 1651 | if (tty != tty->driver->ttys[idx]) { |
Peter Hurley | e2dfa3d | 2015-07-12 22:49:08 -0400 | [diff] [blame] | 1652 | tty_debug(tty, "bad driver table[%d] = %p\n", |
| 1653 | idx, tty->driver->ttys[idx]); |
Jiri Slaby | 955787ca | 2011-11-11 10:47:23 +0100 | [diff] [blame] | 1654 | return -1; |
| 1655 | } |
Jiri Slaby | 955787ca | 2011-11-11 10:47:23 +0100 | [diff] [blame] | 1656 | if (tty->driver->other) { |
Peter Hurley | 359b9fb | 2014-11-05 12:12:59 -0500 | [diff] [blame] | 1657 | struct tty_struct *o_tty = tty->link; |
| 1658 | |
Jiri Slaby | 955787ca | 2011-11-11 10:47:23 +0100 | [diff] [blame] | 1659 | if (o_tty != tty->driver->other->ttys[idx]) { |
Peter Hurley | e2dfa3d | 2015-07-12 22:49:08 -0400 | [diff] [blame] | 1660 | tty_debug(tty, "bad other table[%d] = %p\n", |
| 1661 | idx, tty->driver->other->ttys[idx]); |
Jiri Slaby | 955787ca | 2011-11-11 10:47:23 +0100 | [diff] [blame] | 1662 | return -1; |
| 1663 | } |
Jiri Slaby | 955787ca | 2011-11-11 10:47:23 +0100 | [diff] [blame] | 1664 | if (o_tty->link != tty) { |
Peter Hurley | e2dfa3d | 2015-07-12 22:49:08 -0400 | [diff] [blame] | 1665 | tty_debug(tty, "bad link = %p\n", o_tty->link); |
Jiri Slaby | 955787ca | 2011-11-11 10:47:23 +0100 | [diff] [blame] | 1666 | return -1; |
| 1667 | } |
| 1668 | } |
| 1669 | #endif |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | /** |
Okash Khawaja | a09ac39 | 2017-07-20 08:22:36 +0100 | [diff] [blame] | 1674 | * tty_kclose - closes tty opened by tty_kopen |
| 1675 | * @tty: tty device |
| 1676 | * |
| 1677 | * Performs the final steps to release and free a tty device. It is the |
| 1678 | * same as tty_release_struct except that it also resets TTY_PORT_KOPENED |
| 1679 | * flag on tty->port. |
| 1680 | */ |
| 1681 | void tty_kclose(struct tty_struct *tty) |
| 1682 | { |
| 1683 | /* |
| 1684 | * Ask the line discipline code to release its structures |
| 1685 | */ |
| 1686 | tty_ldisc_release(tty); |
| 1687 | |
| 1688 | /* Wait for pending work before tty destruction commmences */ |
| 1689 | tty_flush_works(tty); |
| 1690 | |
| 1691 | tty_debug_hangup(tty, "freeing structure\n"); |
| 1692 | /* |
| 1693 | * The release_tty function takes care of the details of clearing |
Eric Biggers | ed06982 | 2020-02-23 23:33:59 -0800 | [diff] [blame] | 1694 | * the slots and preserving the termios structure. |
Okash Khawaja | a09ac39 | 2017-07-20 08:22:36 +0100 | [diff] [blame] | 1695 | */ |
| 1696 | mutex_lock(&tty_mutex); |
| 1697 | tty_port_set_kopened(tty->port, 0); |
| 1698 | release_tty(tty, tty->index); |
| 1699 | mutex_unlock(&tty_mutex); |
| 1700 | } |
| 1701 | EXPORT_SYMBOL_GPL(tty_kclose); |
| 1702 | |
| 1703 | /** |
Rob Herring | 9ed90d2 | 2017-01-16 16:54:28 -0600 | [diff] [blame] | 1704 | * tty_release_struct - release a tty struct |
| 1705 | * @tty: tty device |
| 1706 | * @idx: index of the tty |
| 1707 | * |
| 1708 | * Performs the final steps to release and free a tty device. It is |
| 1709 | * roughly the reverse of tty_init_dev. |
| 1710 | */ |
| 1711 | void tty_release_struct(struct tty_struct *tty, int idx) |
| 1712 | { |
| 1713 | /* |
| 1714 | * Ask the line discipline code to release its structures |
| 1715 | */ |
| 1716 | tty_ldisc_release(tty); |
| 1717 | |
| 1718 | /* Wait for pending work before tty destruction commmences */ |
| 1719 | tty_flush_works(tty); |
| 1720 | |
| 1721 | tty_debug_hangup(tty, "freeing structure\n"); |
| 1722 | /* |
| 1723 | * The release_tty function takes care of the details of clearing |
Eric Biggers | ed06982 | 2020-02-23 23:33:59 -0800 | [diff] [blame] | 1724 | * the slots and preserving the termios structure. |
Rob Herring | 9ed90d2 | 2017-01-16 16:54:28 -0600 | [diff] [blame] | 1725 | */ |
| 1726 | mutex_lock(&tty_mutex); |
| 1727 | release_tty(tty, idx); |
| 1728 | mutex_unlock(&tty_mutex); |
| 1729 | } |
| 1730 | EXPORT_SYMBOL_GPL(tty_release_struct); |
| 1731 | |
| 1732 | /** |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 1733 | * tty_release - vfs callback for close |
| 1734 | * @inode: inode of tty |
| 1735 | * @filp: file pointer for handle to tty |
| 1736 | * |
| 1737 | * Called the last time each file handle is closed that references |
| 1738 | * this tty. There may however be several such references. |
| 1739 | * |
| 1740 | * Locking: |
| 1741 | * Takes bkl. See tty_release_dev |
| 1742 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | * Even releasing the tty structures is a tricky business.. We have |
| 1744 | * to be very careful that the structures are all released at the |
| 1745 | * same time, as interrupts might otherwise get the wrong pointers. |
| 1746 | * |
| 1747 | * WSH 09/09/97: rewritten to avoid some nasty race conditions that could |
| 1748 | * lead to double frees or releasing memory still in use. |
| 1749 | */ |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 1750 | |
| 1751 | int tty_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 1753 | struct tty_struct *tty = file_tty(filp); |
Peter Hurley | 7ffb6da | 2014-11-05 12:13:00 -0500 | [diff] [blame] | 1754 | struct tty_struct *o_tty = NULL; |
| 1755 | int do_sleep, final; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | int idx; |
Peter Hurley | 37b1645 | 2014-10-16 13:51:30 -0400 | [diff] [blame] | 1757 | long timeout = 0; |
Peter Hurley | 494c1ea | 2014-10-16 13:54:36 -0400 | [diff] [blame] | 1758 | int once = 1; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1759 | |
Jiri Slaby | 9de44bd | 2011-11-09 21:33:24 +0100 | [diff] [blame] | 1760 | if (tty_paranoia_check(tty, inode, __func__)) |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 1761 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 1763 | tty_lock(tty); |
Jiri Slaby | 9de44bd | 2011-11-09 21:33:24 +0100 | [diff] [blame] | 1764 | check_tty_count(tty, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 1766 | __tty_fasync(-1, filp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | |
| 1768 | idx = tty->index; |
Peter Hurley | 7ffb6da | 2014-11-05 12:13:00 -0500 | [diff] [blame] | 1769 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 1770 | tty->driver->subtype == PTY_TYPE_MASTER) |
| 1771 | o_tty = tty->link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | |
Peter Hurley | 359b9fb | 2014-11-05 12:12:59 -0500 | [diff] [blame] | 1773 | if (tty_release_checks(tty, idx)) { |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 1774 | tty_unlock(tty); |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 1775 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | |
Peter Hurley | d435cef | 2015-11-08 13:01:19 -0500 | [diff] [blame] | 1778 | tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1780 | if (tty->ops->close) |
| 1781 | tty->ops->close(tty, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | |
Peter Hurley | 2aff5e2 | 2014-11-05 12:13:01 -0500 | [diff] [blame] | 1783 | /* If tty is pty master, lock the slave pty (stable lock order) */ |
| 1784 | tty_lock_slave(o_tty); |
| 1785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | /* |
| 1787 | * Sanity check: if tty->count is going to zero, there shouldn't be |
| 1788 | * any waiters on tty->read_wait or tty->write_wait. We test the |
| 1789 | * wait queues and kick everyone out _before_ actually starting to |
| 1790 | * close. This ensures that we won't block while releasing the tty |
| 1791 | * structure. |
| 1792 | * |
| 1793 | * The test for the o_tty closing is necessary, since the master and |
| 1794 | * slave sides may close in any order. If the slave side closes out |
| 1795 | * first, its count will be one, since the master side holds an open. |
Peter Hurley | 324c165 | 2014-11-05 12:12:56 -0500 | [diff] [blame] | 1796 | * Thus this test wouldn't be triggered at the time the slave closed, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | * so we do it now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | */ |
| 1799 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | do_sleep = 0; |
| 1801 | |
Peter Hurley | 324c165 | 2014-11-05 12:12:56 -0500 | [diff] [blame] | 1802 | if (tty->count <= 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | if (waitqueue_active(&tty->read_wait)) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1804 | wake_up_poll(&tty->read_wait, EPOLLIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | do_sleep++; |
| 1806 | } |
| 1807 | if (waitqueue_active(&tty->write_wait)) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1808 | wake_up_poll(&tty->write_wait, EPOLLOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | do_sleep++; |
| 1810 | } |
| 1811 | } |
Peter Hurley | 7ffb6da | 2014-11-05 12:13:00 -0500 | [diff] [blame] | 1812 | if (o_tty && o_tty->count <= 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | if (waitqueue_active(&o_tty->read_wait)) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1814 | wake_up_poll(&o_tty->read_wait, EPOLLIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | do_sleep++; |
| 1816 | } |
| 1817 | if (waitqueue_active(&o_tty->write_wait)) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1818 | wake_up_poll(&o_tty->write_wait, EPOLLOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | do_sleep++; |
| 1820 | } |
| 1821 | } |
| 1822 | if (!do_sleep) |
| 1823 | break; |
| 1824 | |
Peter Hurley | 494c1ea | 2014-10-16 13:54:36 -0400 | [diff] [blame] | 1825 | if (once) { |
| 1826 | once = 0; |
Peter Hurley | 339f36b | 2015-11-08 13:01:13 -0500 | [diff] [blame] | 1827 | tty_warn(tty, "read/write wait queue active!\n"); |
Peter Hurley | 494c1ea | 2014-10-16 13:54:36 -0400 | [diff] [blame] | 1828 | } |
Peter Hurley | 37b1645 | 2014-10-16 13:51:30 -0400 | [diff] [blame] | 1829 | schedule_timeout_killable(timeout); |
| 1830 | if (timeout < 120 * HZ) |
| 1831 | timeout = 2 * timeout + 1; |
| 1832 | else |
| 1833 | timeout = MAX_SCHEDULE_TIMEOUT; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1834 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | |
Peter Hurley | 7ffb6da | 2014-11-05 12:13:00 -0500 | [diff] [blame] | 1836 | if (o_tty) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | if (--o_tty->count < 0) { |
Peter Hurley | 339f36b | 2015-11-08 13:01:13 -0500 | [diff] [blame] | 1838 | tty_warn(tty, "bad slave count (%d)\n", o_tty->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | o_tty->count = 0; |
| 1840 | } |
| 1841 | } |
| 1842 | if (--tty->count < 0) { |
Peter Hurley | 339f36b | 2015-11-08 13:01:13 -0500 | [diff] [blame] | 1843 | tty_warn(tty, "bad tty->count (%d)\n", tty->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | tty->count = 0; |
| 1845 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | /* |
| 1848 | * We've decremented tty->count, so we need to remove this file |
| 1849 | * descriptor off the tty->tty_files list; this serves two |
| 1850 | * purposes: |
| 1851 | * - check_tty_count sees the correct number of file descriptors |
| 1852 | * associated with this tty. |
| 1853 | * - do_tty_hangup no longer sees this file descriptor as |
| 1854 | * something that needs to be handled for hangups. |
| 1855 | */ |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 1856 | tty_del_file(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | |
| 1858 | /* |
| 1859 | * Perform some housekeeping before deciding whether to return. |
| 1860 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | * If _either_ side is closing, make sure there aren't any |
| 1862 | * processes that still think tty or o_tty is their controlling |
| 1863 | * tty. |
| 1864 | */ |
Peter Hurley | 324c165 | 2014-11-05 12:12:56 -0500 | [diff] [blame] | 1865 | if (!tty->count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | read_lock(&tasklist_lock); |
Jiri Slaby | 64d608d | 2021-05-05 11:19:06 +0200 | [diff] [blame] | 1867 | session_clear_tty(tty->ctrl.session); |
Peter Hurley | 7ffb6da | 2014-11-05 12:13:00 -0500 | [diff] [blame] | 1868 | if (o_tty) |
Jiri Slaby | 64d608d | 2021-05-05 11:19:06 +0200 | [diff] [blame] | 1869 | session_clear_tty(o_tty->ctrl.session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | read_unlock(&tasklist_lock); |
| 1871 | } |
| 1872 | |
Peter Hurley | 324c165 | 2014-11-05 12:12:56 -0500 | [diff] [blame] | 1873 | /* check whether both sides are closing ... */ |
Peter Hurley | 7ffb6da | 2014-11-05 12:13:00 -0500 | [diff] [blame] | 1874 | final = !tty->count && !(o_tty && o_tty->count); |
Peter Hurley | 324c165 | 2014-11-05 12:12:56 -0500 | [diff] [blame] | 1875 | |
Peter Hurley | 2aff5e2 | 2014-11-05 12:13:01 -0500 | [diff] [blame] | 1876 | tty_unlock_slave(o_tty); |
| 1877 | tty_unlock(tty); |
| 1878 | |
Peter Hurley | 0498070 | 2014-11-05 12:12:52 -0500 | [diff] [blame] | 1879 | /* At this point, the tty->count == 0 should ensure a dead tty |
Alan Cox | d155255 | 2012-07-27 18:02:54 +0100 | [diff] [blame] | 1880 | cannot be re-opened by a racing opener */ |
Paul Fulghum | da96582 | 2006-02-14 13:53:00 -0800 | [diff] [blame] | 1881 | |
Peter Hurley | 324c165 | 2014-11-05 12:12:56 -0500 | [diff] [blame] | 1882 | if (!final) |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 1883 | return 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1884 | |
Peter Hurley | accff79 | 2015-07-12 22:49:09 -0400 | [diff] [blame] | 1885 | tty_debug_hangup(tty, "final close\n"); |
Peter Hurley | a2965b7 | 2013-03-11 16:44:35 -0400 | [diff] [blame] | 1886 | |
Rob Herring | 9ed90d2 | 2017-01-16 16:54:28 -0600 | [diff] [blame] | 1887 | tty_release_struct(tty, idx); |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 1888 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1891 | /** |
Peter Hurley | 52494ee | 2014-11-05 12:12:50 -0500 | [diff] [blame] | 1892 | * tty_open_current_tty - get locked tty of current task |
Jiri Slaby | b82154a | 2011-11-09 21:33:19 +0100 | [diff] [blame] | 1893 | * @device: device number |
| 1894 | * @filp: file pointer to tty |
Peter Hurley | 52494ee | 2014-11-05 12:12:50 -0500 | [diff] [blame] | 1895 | * @return: locked tty of the current task iff @device is /dev/tty |
| 1896 | * |
| 1897 | * Performs a re-open of the current task's controlling tty. |
Jiri Slaby | b82154a | 2011-11-09 21:33:19 +0100 | [diff] [blame] | 1898 | * |
| 1899 | * We cannot return driver and index like for the other nodes because |
| 1900 | * devpts will not work then. It expects inodes to be from devpts FS. |
| 1901 | */ |
| 1902 | static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp) |
| 1903 | { |
| 1904 | struct tty_struct *tty; |
Peter Hurley | 52494ee | 2014-11-05 12:12:50 -0500 | [diff] [blame] | 1905 | int retval; |
Jiri Slaby | b82154a | 2011-11-09 21:33:19 +0100 | [diff] [blame] | 1906 | |
| 1907 | if (device != MKDEV(TTYAUX_MAJOR, 0)) |
| 1908 | return NULL; |
| 1909 | |
| 1910 | tty = get_current_tty(); |
| 1911 | if (!tty) |
| 1912 | return ERR_PTR(-ENXIO); |
| 1913 | |
| 1914 | filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ |
| 1915 | /* noctty = 1; */ |
Peter Hurley | 52494ee | 2014-11-05 12:12:50 -0500 | [diff] [blame] | 1916 | tty_lock(tty); |
| 1917 | tty_kref_put(tty); /* safe to drop the kref now */ |
| 1918 | |
| 1919 | retval = tty_reopen(tty); |
| 1920 | if (retval < 0) { |
| 1921 | tty_unlock(tty); |
| 1922 | tty = ERR_PTR(retval); |
| 1923 | } |
Jiri Slaby | b82154a | 2011-11-09 21:33:19 +0100 | [diff] [blame] | 1924 | return tty; |
| 1925 | } |
| 1926 | |
| 1927 | /** |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1928 | * tty_lookup_driver - lookup a tty driver for a given device file |
| 1929 | * @device: device number |
| 1930 | * @filp: file pointer to tty |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1931 | * @index: index for the device in the @return driver |
| 1932 | * @return: driver for this inode (with increased refcount) |
| 1933 | * |
Xiaofei Tan | 395e783 | 2021-05-12 17:26:14 +0800 | [diff] [blame] | 1934 | * If @return is not erroneous, the caller is responsible to decrement the |
| 1935 | * refcount by tty_driver_kref_put. |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1936 | * |
| 1937 | * Locking: tty_mutex protects get_tty_driver |
| 1938 | */ |
| 1939 | static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp, |
Peter Hurley | 11e1d4a | 2016-01-09 21:13:52 -0800 | [diff] [blame] | 1940 | int *index) |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1941 | { |
Lin Yi | 8c8af41 | 2019-05-10 12:22:57 +0800 | [diff] [blame] | 1942 | struct tty_driver *driver = NULL; |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1943 | |
Jiri Slaby | 2cd0050 | 2011-11-09 21:33:22 +0100 | [diff] [blame] | 1944 | switch (device) { |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1945 | #ifdef CONFIG_VT |
Jiri Slaby | 2cd0050 | 2011-11-09 21:33:22 +0100 | [diff] [blame] | 1946 | case MKDEV(TTY_MAJOR, 0): { |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1947 | extern struct tty_driver *console_driver; |
Xiaofei Tan | e73b240 | 2021-05-12 17:26:15 +0800 | [diff] [blame] | 1948 | |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1949 | driver = tty_driver_kref_get(console_driver); |
| 1950 | *index = fg_console; |
Jiri Slaby | 2cd0050 | 2011-11-09 21:33:22 +0100 | [diff] [blame] | 1951 | break; |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1952 | } |
| 1953 | #endif |
Jiri Slaby | 2cd0050 | 2011-11-09 21:33:22 +0100 | [diff] [blame] | 1954 | case MKDEV(TTYAUX_MAJOR, 1): { |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1955 | struct tty_driver *console_driver = console_device(index); |
Xiaofei Tan | e73b240 | 2021-05-12 17:26:15 +0800 | [diff] [blame] | 1956 | |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1957 | if (console_driver) { |
| 1958 | driver = tty_driver_kref_get(console_driver); |
Okash Khawaja | 12e84c7 | 2017-05-15 18:45:32 +0100 | [diff] [blame] | 1959 | if (driver && filp) { |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1960 | /* Don't let /dev/console block */ |
| 1961 | filp->f_flags |= O_NONBLOCK; |
Jiri Slaby | 2cd0050 | 2011-11-09 21:33:22 +0100 | [diff] [blame] | 1962 | break; |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1963 | } |
| 1964 | } |
Lin Yi | 8c8af41 | 2019-05-10 12:22:57 +0800 | [diff] [blame] | 1965 | if (driver) |
| 1966 | tty_driver_kref_put(driver); |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1967 | return ERR_PTR(-ENODEV); |
| 1968 | } |
Jiri Slaby | 2cd0050 | 2011-11-09 21:33:22 +0100 | [diff] [blame] | 1969 | default: |
| 1970 | driver = get_tty_driver(device, index); |
| 1971 | if (!driver) |
| 1972 | return ERR_PTR(-ENODEV); |
| 1973 | break; |
| 1974 | } |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 1975 | return driver; |
| 1976 | } |
| 1977 | |
Uwe Kleine-König | 4ea3cd6 | 2020-12-18 11:42:44 +0100 | [diff] [blame] | 1978 | static struct tty_struct *tty_kopen(dev_t device, int shared) |
Okash Khawaja | a09ac39 | 2017-07-20 08:22:36 +0100 | [diff] [blame] | 1979 | { |
| 1980 | struct tty_struct *tty; |
Uwe Kleine-König | f1d3174 | 2019-12-17 08:50:40 +0100 | [diff] [blame] | 1981 | struct tty_driver *driver; |
Okash Khawaja | a09ac39 | 2017-07-20 08:22:36 +0100 | [diff] [blame] | 1982 | int index = -1; |
| 1983 | |
| 1984 | mutex_lock(&tty_mutex); |
| 1985 | driver = tty_lookup_driver(device, NULL, &index); |
| 1986 | if (IS_ERR(driver)) { |
| 1987 | mutex_unlock(&tty_mutex); |
| 1988 | return ERR_CAST(driver); |
| 1989 | } |
| 1990 | |
| 1991 | /* check whether we're reopening an existing tty */ |
| 1992 | tty = tty_driver_lookup_tty(driver, NULL, index); |
Uwe Kleine-König | 4ea3cd6 | 2020-12-18 11:42:44 +0100 | [diff] [blame] | 1993 | if (IS_ERR(tty) || shared) |
Okash Khawaja | a09ac39 | 2017-07-20 08:22:36 +0100 | [diff] [blame] | 1994 | goto out; |
| 1995 | |
| 1996 | if (tty) { |
| 1997 | /* drop kref from tty_driver_lookup_tty() */ |
| 1998 | tty_kref_put(tty); |
| 1999 | tty = ERR_PTR(-EBUSY); |
| 2000 | } else { /* tty_init_dev returns tty with the tty_lock held */ |
| 2001 | tty = tty_init_dev(driver, index); |
| 2002 | if (IS_ERR(tty)) |
| 2003 | goto out; |
| 2004 | tty_port_set_kopened(tty->port, 1); |
| 2005 | } |
| 2006 | out: |
| 2007 | mutex_unlock(&tty_mutex); |
| 2008 | tty_driver_kref_put(driver); |
| 2009 | return tty; |
| 2010 | } |
Uwe Kleine-König | 4ea3cd6 | 2020-12-18 11:42:44 +0100 | [diff] [blame] | 2011 | |
| 2012 | /** |
| 2013 | * tty_kopen_exclusive - open a tty device for kernel |
| 2014 | * @device: dev_t of device to open |
| 2015 | * |
| 2016 | * Opens tty exclusively for kernel. Performs the driver lookup, |
| 2017 | * makes sure it's not already opened and performs the first-time |
| 2018 | * tty initialization. |
| 2019 | * |
| 2020 | * Returns the locked initialized &tty_struct |
| 2021 | * |
| 2022 | * Claims the global tty_mutex to serialize: |
| 2023 | * - concurrent first-time tty initialization |
| 2024 | * - concurrent tty driver removal w/ lookup |
| 2025 | * - concurrent tty removal from driver table |
| 2026 | */ |
| 2027 | struct tty_struct *tty_kopen_exclusive(dev_t device) |
| 2028 | { |
| 2029 | return tty_kopen(device, 0); |
| 2030 | } |
| 2031 | EXPORT_SYMBOL_GPL(tty_kopen_exclusive); |
| 2032 | |
| 2033 | /** |
| 2034 | * tty_kopen_shared - open a tty device for shared in-kernel use |
| 2035 | * @device: dev_t of device to open |
| 2036 | * |
| 2037 | * Opens an already existing tty for in-kernel use. Compared to |
| 2038 | * tty_kopen_exclusive() above it doesn't ensure to be the only user. |
| 2039 | * |
| 2040 | * Locking is identical to tty_kopen() above. |
| 2041 | */ |
| 2042 | struct tty_struct *tty_kopen_shared(dev_t device) |
| 2043 | { |
| 2044 | return tty_kopen(device, 1); |
| 2045 | } |
| 2046 | EXPORT_SYMBOL_GPL(tty_kopen_shared); |
Okash Khawaja | a09ac39 | 2017-07-20 08:22:36 +0100 | [diff] [blame] | 2047 | |
| 2048 | /** |
Peter Hurley | d6203d0 | 2016-01-09 21:13:53 -0800 | [diff] [blame] | 2049 | * tty_open_by_driver - open a tty device |
| 2050 | * @device: dev_t of device to open |
Peter Hurley | d6203d0 | 2016-01-09 21:13:53 -0800 | [diff] [blame] | 2051 | * @filp: file pointer to tty |
| 2052 | * |
| 2053 | * Performs the driver lookup, checks for a reopen, or otherwise |
| 2054 | * performs the first-time tty initialization. |
| 2055 | * |
| 2056 | * Returns the locked initialized or re-opened &tty_struct |
| 2057 | * |
| 2058 | * Claims the global tty_mutex to serialize: |
| 2059 | * - concurrent first-time tty initialization |
| 2060 | * - concurrent tty driver removal w/ lookup |
| 2061 | * - concurrent tty removal from driver table |
| 2062 | */ |
Sudip Mukherjee | 14ce384 | 2019-11-20 15:17:08 +0000 | [diff] [blame] | 2063 | static struct tty_struct *tty_open_by_driver(dev_t device, |
Peter Hurley | d6203d0 | 2016-01-09 21:13:53 -0800 | [diff] [blame] | 2064 | struct file *filp) |
| 2065 | { |
| 2066 | struct tty_struct *tty; |
| 2067 | struct tty_driver *driver = NULL; |
| 2068 | int index = -1; |
| 2069 | int retval; |
| 2070 | |
| 2071 | mutex_lock(&tty_mutex); |
| 2072 | driver = tty_lookup_driver(device, filp, &index); |
| 2073 | if (IS_ERR(driver)) { |
| 2074 | mutex_unlock(&tty_mutex); |
| 2075 | return ERR_CAST(driver); |
| 2076 | } |
| 2077 | |
| 2078 | /* check whether we're reopening an existing tty */ |
Linus Torvalds | 8ead9dd | 2016-04-25 20:04:08 -0700 | [diff] [blame] | 2079 | tty = tty_driver_lookup_tty(driver, filp, index); |
Peter Hurley | d6203d0 | 2016-01-09 21:13:53 -0800 | [diff] [blame] | 2080 | if (IS_ERR(tty)) { |
| 2081 | mutex_unlock(&tty_mutex); |
| 2082 | goto out; |
| 2083 | } |
| 2084 | |
| 2085 | if (tty) { |
Okash Khawaja | a09ac39 | 2017-07-20 08:22:36 +0100 | [diff] [blame] | 2086 | if (tty_port_kopened(tty->port)) { |
| 2087 | tty_kref_put(tty); |
| 2088 | mutex_unlock(&tty_mutex); |
| 2089 | tty = ERR_PTR(-EBUSY); |
| 2090 | goto out; |
| 2091 | } |
Peter Hurley | d6203d0 | 2016-01-09 21:13:53 -0800 | [diff] [blame] | 2092 | mutex_unlock(&tty_mutex); |
| 2093 | retval = tty_lock_interruptible(tty); |
Peter Hurley | 5e00bbf | 2016-03-31 17:47:07 -0700 | [diff] [blame] | 2094 | tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */ |
Peter Hurley | d6203d0 | 2016-01-09 21:13:53 -0800 | [diff] [blame] | 2095 | if (retval) { |
| 2096 | if (retval == -EINTR) |
| 2097 | retval = -ERESTARTSYS; |
| 2098 | tty = ERR_PTR(retval); |
| 2099 | goto out; |
| 2100 | } |
Peter Hurley | d6203d0 | 2016-01-09 21:13:53 -0800 | [diff] [blame] | 2101 | retval = tty_reopen(tty); |
| 2102 | if (retval < 0) { |
| 2103 | tty_unlock(tty); |
| 2104 | tty = ERR_PTR(retval); |
| 2105 | } |
| 2106 | } else { /* Returns with the tty_lock held for now */ |
| 2107 | tty = tty_init_dev(driver, index); |
| 2108 | mutex_unlock(&tty_mutex); |
| 2109 | } |
| 2110 | out: |
| 2111 | tty_driver_kref_put(driver); |
| 2112 | return tty; |
| 2113 | } |
| 2114 | |
| 2115 | /** |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 2116 | * tty_open - open a tty device |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2117 | * @inode: inode of device file |
| 2118 | * @filp: file pointer to tty |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2120 | * tty_open and tty_release keep up the tty count that contains the |
| 2121 | * number of opens done on a tty. We cannot use the inode-count, as |
| 2122 | * different inodes might point to the same tty. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2124 | * Open-counting is needed for pty masters, as well as for keeping |
| 2125 | * track of serial lines: DTR is dropped when the last close happens. |
| 2126 | * (This is not done solely through tty->count, now. - Ted 1/27/92) |
| 2127 | * |
| 2128 | * The termios state of a pty is reset on first open so that |
| 2129 | * settings don't persist across reuse. |
| 2130 | * |
Jiri Slaby | 5b5e704 | 2011-11-09 21:33:20 +0100 | [diff] [blame] | 2131 | * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev. |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2132 | * tty->count should protect the rest. |
| 2133 | * ->siglock protects ->signal/->sighand |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 2134 | * |
| 2135 | * Note: the tty_unlock/lock cases without a ref are only safe due to |
| 2136 | * tty_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2138 | |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 2139 | static int tty_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | { |
Jiri Slaby | b82154a | 2011-11-09 21:33:19 +0100 | [diff] [blame] | 2141 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | int noctty, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | dev_t device = inode->i_rdev; |
Andrew Morton | 846c151 | 2009-04-02 16:56:36 -0700 | [diff] [blame] | 2144 | unsigned saved_flags = filp->f_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | |
| 2146 | nonseekable_open(inode, filp); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | retry_open: |
Jiri Slaby | fa90e1c | 2011-10-12 11:32:43 +0200 | [diff] [blame] | 2149 | retval = tty_alloc_file(filp); |
| 2150 | if (retval) |
| 2151 | return -ENOMEM; |
| 2152 | |
Jiri Slaby | b82154a | 2011-11-09 21:33:19 +0100 | [diff] [blame] | 2153 | tty = tty_open_current_tty(device, filp); |
Peter Hurley | d6203d0 | 2016-01-09 21:13:53 -0800 | [diff] [blame] | 2154 | if (!tty) |
Sudip Mukherjee | 14ce384 | 2019-11-20 15:17:08 +0000 | [diff] [blame] | 2155 | tty = tty_open_by_driver(device, filp); |
Sukadev Bhattiprolu | 4a2b5fd | 2008-10-13 10:42:49 +0100 | [diff] [blame] | 2156 | |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 2157 | if (IS_ERR(tty)) { |
Peter Hurley | d6203d0 | 2016-01-09 21:13:53 -0800 | [diff] [blame] | 2158 | tty_free_file(filp); |
Jiri Slaby | ba5db44 | 2011-11-09 21:33:21 +0100 | [diff] [blame] | 2159 | retval = PTR_ERR(tty); |
Peter Hurley | 7f22f6c | 2016-01-09 21:13:45 -0800 | [diff] [blame] | 2160 | if (retval != -EAGAIN || signal_pending(current)) |
Peter Hurley | d6203d0 | 2016-01-09 21:13:53 -0800 | [diff] [blame] | 2161 | return retval; |
Peter Hurley | 7f22f6c | 2016-01-09 21:13:45 -0800 | [diff] [blame] | 2162 | schedule(); |
| 2163 | goto retry_open; |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 2164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | |
Jiri Slaby | fa90e1c | 2011-10-12 11:32:43 +0200 | [diff] [blame] | 2166 | tty_add_file(tty, filp); |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2167 | |
Jiri Slaby | 9de44bd | 2011-11-09 21:33:24 +0100 | [diff] [blame] | 2168 | check_tty_count(tty, __func__); |
Peter Hurley | d435cef | 2015-11-08 13:01:19 -0500 | [diff] [blame] | 2169 | tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); |
Peter Hurley | accff79 | 2015-07-12 22:49:09 -0400 | [diff] [blame] | 2170 | |
Herton Ronaldo Krzesinski | 909bc77 | 2011-03-31 15:35:31 -0300 | [diff] [blame] | 2171 | if (tty->ops->open) |
| 2172 | retval = tty->ops->open(tty, filp); |
| 2173 | else |
| 2174 | retval = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | filp->f_flags = saved_flags; |
| 2176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | if (retval) { |
Peter Hurley | d435cef | 2015-11-08 13:01:19 -0500 | [diff] [blame] | 2178 | tty_debug_hangup(tty, "open error %d, releasing\n", retval); |
Peter Hurley | accff79 | 2015-07-12 22:49:09 -0400 | [diff] [blame] | 2179 | |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 2180 | tty_unlock(tty); /* need to call tty_release without BTM */ |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 2181 | tty_release(inode, filp); |
Arnd Bergmann | 64ba3dc3 | 2010-06-01 22:53:02 +0200 | [diff] [blame] | 2182 | if (retval != -ERESTARTSYS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | return retval; |
Arnd Bergmann | 64ba3dc3 | 2010-06-01 22:53:02 +0200 | [diff] [blame] | 2184 | |
| 2185 | if (signal_pending(current)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | return retval; |
Arnd Bergmann | 64ba3dc3 | 2010-06-01 22:53:02 +0200 | [diff] [blame] | 2187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | schedule(); |
| 2189 | /* |
| 2190 | * Need to reset f_op in case a hangup happened. |
| 2191 | */ |
Peter Hurley | 1256937 | 2014-11-05 12:26:24 -0500 | [diff] [blame] | 2192 | if (tty_hung_up_p(filp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | filp->f_op = &tty_fops; |
| 2194 | goto retry_open; |
| 2195 | } |
Peter Hurley | d4855e1 | 2013-11-19 08:46:27 -0500 | [diff] [blame] | 2196 | clear_bit(TTY_HUPPED, &tty->flags); |
Alan Cox | eeb89d9 | 2009-11-30 13:18:29 +0000 | [diff] [blame] | 2197 | |
Peter Hurley | 11e1d4a | 2016-01-09 21:13:52 -0800 | [diff] [blame] | 2198 | noctty = (filp->f_flags & O_NOCTTY) || |
Nicolas Pitre | a1235b3 | 2017-04-12 18:37:16 -0400 | [diff] [blame] | 2199 | (IS_ENABLED(CONFIG_VT) && device == MKDEV(TTY_MAJOR, 0)) || |
| 2200 | device == MKDEV(TTYAUX_MAJOR, 1) || |
| 2201 | (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 2202 | tty->driver->subtype == PTY_TYPE_MASTER); |
| 2203 | if (!noctty) |
| 2204 | tty_open_proc_set_tty(filp, tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 2205 | tty_unlock(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | return 0; |
| 2207 | } |
| 2208 | |
Jonathan Corbet | 39d95b9 | 2008-05-16 09:10:50 -0600 | [diff] [blame] | 2209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2211 | /** |
| 2212 | * tty_poll - check tty status |
| 2213 | * @filp: file being polled |
| 2214 | * @wait: poll wait structures to update |
| 2215 | * |
| 2216 | * Call the line discipline polling method to obtain the poll |
| 2217 | * status of the device. |
| 2218 | * |
| 2219 | * Locking: locks called line discipline but ldisc poll method |
| 2220 | * may be re-entered freely by other callers. |
| 2221 | */ |
| 2222 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 2223 | static __poll_t tty_poll(struct file *filp, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2225 | struct tty_struct *tty = file_tty(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | struct tty_ldisc *ld; |
Al Viro | e6c8adc | 2017-07-03 22:25:56 -0400 | [diff] [blame] | 2227 | __poll_t ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 2229 | if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | return 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | ld = tty_ldisc_ref_wait(tty); |
Peter Hurley | e55afd1 | 2016-01-10 22:41:01 -0800 | [diff] [blame] | 2233 | if (!ld) |
| 2234 | return hung_up_tty_poll(filp, wait); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2235 | if (ld->ops->poll) |
Peter Hurley | c961bfb | 2014-11-05 12:26:25 -0500 | [diff] [blame] | 2236 | ret = ld->ops->poll(tty, filp, wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | tty_ldisc_deref(ld); |
| 2238 | return ret; |
| 2239 | } |
| 2240 | |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 2241 | static int __tty_fasync(int fd, struct file *filp, int on) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2243 | struct tty_struct *tty = file_tty(filp); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2244 | unsigned long flags; |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 2245 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 2247 | if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 2248 | goto out; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | retval = fasync_helper(fd, filp, on, &tty->fasync); |
| 2251 | if (retval <= 0) |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 2252 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | |
| 2254 | if (on) { |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2255 | enum pid_type type; |
| 2256 | struct pid *pid; |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 2257 | |
Jiri Slaby | 64d608d | 2021-05-05 11:19:06 +0200 | [diff] [blame] | 2258 | spin_lock_irqsave(&tty->ctrl.lock, flags); |
| 2259 | if (tty->ctrl.pgrp) { |
| 2260 | pid = tty->ctrl.pgrp; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2261 | type = PIDTYPE_PGID; |
| 2262 | } else { |
| 2263 | pid = task_pid(current); |
Eric W. Biederman | 0191913 | 2017-07-16 22:05:57 -0500 | [diff] [blame] | 2264 | type = PIDTYPE_TGID; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2265 | } |
Linus Torvalds | 80e1e82 | 2010-02-07 10:11:23 -0800 | [diff] [blame] | 2266 | get_pid(pid); |
Jiri Slaby | 64d608d | 2021-05-05 11:19:06 +0200 | [diff] [blame] | 2267 | spin_unlock_irqrestore(&tty->ctrl.lock, flags); |
Jeff Layton | e0b93ed | 2014-08-22 11:27:32 -0400 | [diff] [blame] | 2268 | __f_setown(filp, pid, type, 0); |
Linus Torvalds | 80e1e82 | 2010-02-07 10:11:23 -0800 | [diff] [blame] | 2269 | put_pid(pid); |
Jeff Layton | e0b93ed | 2014-08-22 11:27:32 -0400 | [diff] [blame] | 2270 | retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | } |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 2272 | out: |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 2273 | return retval; |
| 2274 | } |
| 2275 | |
| 2276 | static int tty_fasync(int fd, struct file *filp, int on) |
| 2277 | { |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 2278 | struct tty_struct *tty = file_tty(filp); |
Peter Hurley | a8f3a29 | 2016-01-09 21:45:11 -0800 | [diff] [blame] | 2279 | int retval = -ENOTTY; |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 2280 | |
| 2281 | tty_lock(tty); |
Peter Hurley | a8f3a29 | 2016-01-09 21:45:11 -0800 | [diff] [blame] | 2282 | if (!tty_hung_up_p(filp)) |
| 2283 | retval = __tty_fasync(fd, filp, on); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 2284 | tty_unlock(tty); |
| 2285 | |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 2286 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | } |
| 2288 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2289 | /** |
| 2290 | * tiocsti - fake input character |
| 2291 | * @tty: tty to fake input into |
| 2292 | * @p: pointer to character |
| 2293 | * |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 2294 | * Fake input to a tty device. Does the necessary locking and |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2295 | * input management. |
| 2296 | * |
| 2297 | * FIXME: does not honour flow control ?? |
| 2298 | * |
| 2299 | * Locking: |
Peter Hurley | 137084b | 2013-06-15 07:04:46 -0400 | [diff] [blame] | 2300 | * Called functions take tty_ldiscs_lock |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2301 | * current->signal->tty check is safe without locks |
Alan Cox | 2829823 | 2006-09-29 02:00:58 -0700 | [diff] [blame] | 2302 | * |
| 2303 | * FIXME: may race normal receive processing |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2304 | */ |
| 2305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | static int tiocsti(struct tty_struct *tty, char __user *p) |
| 2307 | { |
| 2308 | char ch, mbz = 0; |
| 2309 | struct tty_ldisc *ld; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) |
| 2312 | return -EPERM; |
| 2313 | if (get_user(ch, p)) |
| 2314 | return -EFAULT; |
Al Viro | 1e64174 | 2008-12-09 09:23:33 +0000 | [diff] [blame] | 2315 | tty_audit_tiocsti(tty, ch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | ld = tty_ldisc_ref_wait(tty); |
Peter Hurley | e55afd1 | 2016-01-10 22:41:01 -0800 | [diff] [blame] | 2317 | if (!ld) |
| 2318 | return -EIO; |
Greg Kroah-Hartman | 27cfb3a | 2019-01-20 10:46:58 +0100 | [diff] [blame] | 2319 | if (ld->ops->receive_buf) |
| 2320 | ld->ops->receive_buf(tty, &ch, &mbz, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | tty_ldisc_deref(ld); |
| 2322 | return 0; |
| 2323 | } |
| 2324 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2325 | /** |
| 2326 | * tiocgwinsz - implement window query ioctl |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 2327 | * @tty: tty |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2328 | * @arg: user buffer for result |
| 2329 | * |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 2330 | * Copies the kernel idea of the window size into the user buffer. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2331 | * |
Peter Hurley | dee4a0b | 2013-07-24 16:43:51 -0400 | [diff] [blame] | 2332 | * Locking: tty->winsize_mutex is taken to ensure the winsize data |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 2333 | * is consistent. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2334 | */ |
| 2335 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2336 | static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | { |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 2338 | int err; |
| 2339 | |
Peter Hurley | dee4a0b | 2013-07-24 16:43:51 -0400 | [diff] [blame] | 2340 | mutex_lock(&tty->winsize_mutex); |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 2341 | err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); |
Peter Hurley | dee4a0b | 2013-07-24 16:43:51 -0400 | [diff] [blame] | 2342 | mutex_unlock(&tty->winsize_mutex); |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 2343 | |
Xiaofei Tan | 94bc2eb | 2021-05-12 17:26:16 +0800 | [diff] [blame^] | 2344 | return err ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | } |
| 2346 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2347 | /** |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2348 | * tty_do_resize - resize event |
| 2349 | * @tty: tty being resized |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 2350 | * @ws: new dimensions |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2351 | * |
Daniel Mack | 3ad2f3fb | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 2352 | * Update the termios variables and send the necessary signals to |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2353 | * peform a terminal resize correctly |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2354 | */ |
| 2355 | |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 2356 | int tty_do_resize(struct tty_struct *tty, struct winsize *ws) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | { |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 2358 | struct pid *pgrp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 2360 | /* Lock the tty */ |
Peter Hurley | dee4a0b | 2013-07-24 16:43:51 -0400 | [diff] [blame] | 2361 | mutex_lock(&tty->winsize_mutex); |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 2362 | if (!memcmp(ws, &tty->winsize, sizeof(*ws))) |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 2363 | goto done; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2364 | |
Peter Hurley | 5b23954 | 2014-10-16 14:59:45 -0400 | [diff] [blame] | 2365 | /* Signal the foreground process group */ |
| 2366 | pgrp = tty_get_pgrp(tty); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2367 | if (pgrp) |
| 2368 | kill_pgrp(pgrp, SIGWINCH, 1); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2369 | put_pid(pgrp); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2370 | |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2371 | tty->winsize = *ws; |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 2372 | done: |
Peter Hurley | dee4a0b | 2013-07-24 16:43:51 -0400 | [diff] [blame] | 2373 | mutex_unlock(&tty->winsize_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | return 0; |
| 2375 | } |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 2376 | EXPORT_SYMBOL(tty_do_resize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2378 | /** |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2379 | * tiocswinsz - implement window size set ioctl |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 2380 | * @tty: tty side of tty |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2381 | * @arg: user buffer for result |
| 2382 | * |
| 2383 | * Copies the user idea of the window size to the kernel. Traditionally |
| 2384 | * this is just advisory information but for the Linux console it |
| 2385 | * actually has driver level meaning and triggers a VC resize. |
| 2386 | * |
| 2387 | * Locking: |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2388 | * Driver dependent. The default do_resize method takes the |
Jiri Slaby | 64d608d | 2021-05-05 11:19:06 +0200 | [diff] [blame] | 2389 | * tty termios mutex and ctrl.lock. The console takes its own lock |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2390 | * then calls into the default method. |
| 2391 | */ |
| 2392 | |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 2393 | static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2394 | { |
| 2395 | struct winsize tmp_ws; |
Xiaofei Tan | e73b240 | 2021-05-12 17:26:15 +0800 | [diff] [blame] | 2396 | |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2397 | if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) |
| 2398 | return -EFAULT; |
| 2399 | |
| 2400 | if (tty->ops->resize) |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 2401 | return tty->ops->resize(tty, &tmp_ws); |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2402 | else |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 2403 | return tty_do_resize(tty, &tmp_ws); |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2407 | * tioccons - allow admin to move logical console |
| 2408 | * @file: the file to become console |
| 2409 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2410 | * Allow the administrator to move the redirected console device |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2411 | * |
| 2412 | * Locking: uses redirect_lock to guard the redirect information |
| 2413 | */ |
| 2414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | static int tioccons(struct file *file) |
| 2416 | { |
| 2417 | if (!capable(CAP_SYS_ADMIN)) |
| 2418 | return -EPERM; |
Linus Torvalds | 9bb48c8 | 2021-01-19 11:41:16 -0800 | [diff] [blame] | 2419 | if (file->f_op->write_iter == redirected_tty_write) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | struct file *f; |
Xiaofei Tan | e73b240 | 2021-05-12 17:26:15 +0800 | [diff] [blame] | 2421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | spin_lock(&redirect_lock); |
| 2423 | f = redirect; |
| 2424 | redirect = NULL; |
| 2425 | spin_unlock(&redirect_lock); |
| 2426 | if (f) |
| 2427 | fput(f); |
| 2428 | return 0; |
| 2429 | } |
Linus Torvalds | a9cbbb8 | 2021-01-29 12:28:20 -0800 | [diff] [blame] | 2430 | if (file->f_op->write_iter != tty_write) |
| 2431 | return -ENOTTY; |
| 2432 | if (!(file->f_mode & FMODE_WRITE)) |
| 2433 | return -EBADF; |
| 2434 | if (!(file->f_mode & FMODE_CAN_WRITE)) |
| 2435 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | spin_lock(&redirect_lock); |
| 2437 | if (redirect) { |
| 2438 | spin_unlock(&redirect_lock); |
| 2439 | return -EBUSY; |
| 2440 | } |
Al Viro | cb0942b | 2012-08-27 14:48:26 -0400 | [diff] [blame] | 2441 | redirect = get_file(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | spin_unlock(&redirect_lock); |
| 2443 | return 0; |
| 2444 | } |
| 2445 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2446 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2447 | * tiocsetd - set line discipline |
| 2448 | * @tty: tty device |
| 2449 | * @p: pointer to user data |
| 2450 | * |
| 2451 | * Set the line discipline according to user request. |
| 2452 | * |
| 2453 | * Locking: see tty_set_ldisc, this function is just a helper |
| 2454 | */ |
| 2455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | static int tiocsetd(struct tty_struct *tty, int __user *p) |
| 2457 | { |
Peter Hurley | c12da96 | 2016-01-10 22:41:04 -0800 | [diff] [blame] | 2458 | int disc; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 2459 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | |
Peter Hurley | c12da96 | 2016-01-10 22:41:04 -0800 | [diff] [blame] | 2461 | if (get_user(disc, p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | return -EFAULT; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 2463 | |
Peter Hurley | c12da96 | 2016-01-10 22:41:04 -0800 | [diff] [blame] | 2464 | ret = tty_set_ldisc(tty, disc); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 2465 | |
| 2466 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | } |
| 2468 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2469 | /** |
Peter Hurley | 5c17c86 | 2016-01-10 22:40:55 -0800 | [diff] [blame] | 2470 | * tiocgetd - get line discipline |
| 2471 | * @tty: tty device |
| 2472 | * @p: pointer to user data |
| 2473 | * |
| 2474 | * Retrieves the line discipline id directly from the ldisc. |
| 2475 | * |
| 2476 | * Locking: waits for ldisc reference (in case the line discipline |
| 2477 | * is changing or the tty is being hungup) |
| 2478 | */ |
| 2479 | |
| 2480 | static int tiocgetd(struct tty_struct *tty, int __user *p) |
| 2481 | { |
| 2482 | struct tty_ldisc *ld; |
| 2483 | int ret; |
| 2484 | |
| 2485 | ld = tty_ldisc_ref_wait(tty); |
Peter Hurley | e55afd1 | 2016-01-10 22:41:01 -0800 | [diff] [blame] | 2486 | if (!ld) |
| 2487 | return -EIO; |
Peter Hurley | 5c17c86 | 2016-01-10 22:40:55 -0800 | [diff] [blame] | 2488 | ret = put_user(ld->ops->num, p); |
| 2489 | tty_ldisc_deref(ld); |
| 2490 | return ret; |
| 2491 | } |
| 2492 | |
| 2493 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2494 | * send_break - performed time break |
| 2495 | * @tty: device to break on |
| 2496 | * @duration: timeout in mS |
| 2497 | * |
| 2498 | * Perform a timed break on hardware that lacks its own driver level |
| 2499 | * timed break functionality. |
| 2500 | * |
| 2501 | * Locking: |
Alan Cox | 2829823 | 2006-09-29 02:00:58 -0700 | [diff] [blame] | 2502 | * atomic_write_lock serializes |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2503 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2504 | */ |
| 2505 | |
Domen Puncer | b20f3ae | 2005-06-25 14:58:42 -0700 | [diff] [blame] | 2506 | static int send_break(struct tty_struct *tty, unsigned int duration) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | { |
Alan Cox | 9e98966c | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 2508 | int retval; |
| 2509 | |
| 2510 | if (tty->ops->break_ctl == NULL) |
| 2511 | return 0; |
| 2512 | |
| 2513 | if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) |
| 2514 | retval = tty->ops->break_ctl(tty, duration); |
| 2515 | else { |
| 2516 | /* Do the work ourselves */ |
| 2517 | if (tty_write_lock(tty, 0) < 0) |
| 2518 | return -EINTR; |
| 2519 | retval = tty->ops->break_ctl(tty, -1); |
| 2520 | if (retval) |
| 2521 | goto out; |
| 2522 | if (!signal_pending(current)) |
| 2523 | msleep_interruptible(duration); |
| 2524 | retval = tty->ops->break_ctl(tty, 0); |
| 2525 | out: |
| 2526 | tty_write_unlock(tty); |
| 2527 | if (signal_pending(current)) |
| 2528 | retval = -EINTR; |
| 2529 | } |
| 2530 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | } |
| 2532 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2533 | /** |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2534 | * tty_tiocmget - get modem status |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2535 | * @tty: tty device |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2536 | * @p: pointer to result |
| 2537 | * |
| 2538 | * Obtain the modem status bits from the tty driver if the feature |
Johan Hovold | 1b8b208 | 2021-04-07 11:52:02 +0200 | [diff] [blame] | 2539 | * is supported. Return -ENOTTY if it is not available. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2540 | * |
| 2541 | * Locking: none (up to the driver) |
| 2542 | */ |
| 2543 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 2544 | static int tty_tiocmget(struct tty_struct *tty, int __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | { |
Johan Hovold | 1b8b208 | 2021-04-07 11:52:02 +0200 | [diff] [blame] | 2546 | int retval = -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2548 | if (tty->ops->tiocmget) { |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 2549 | retval = tty->ops->tiocmget(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | |
| 2551 | if (retval >= 0) |
| 2552 | retval = put_user(retval, p); |
| 2553 | } |
| 2554 | return retval; |
| 2555 | } |
| 2556 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2557 | /** |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2558 | * tty_tiocmset - set modem status |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2559 | * @tty: tty device |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2560 | * @cmd: command - clear bits, set bits or set all |
| 2561 | * @p: pointer to desired bits |
| 2562 | * |
| 2563 | * Set the modem status bits from the tty driver if the feature |
Johan Hovold | 1b8b208 | 2021-04-07 11:52:02 +0200 | [diff] [blame] | 2564 | * is supported. Return -ENOTTY if it is not available. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2565 | * |
| 2566 | * Locking: none (up to the driver) |
| 2567 | */ |
| 2568 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 2569 | static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | unsigned __user *p) |
| 2571 | { |
Alan Cox | ae67751 | 2008-07-16 21:56:54 +0100 | [diff] [blame] | 2572 | int retval; |
| 2573 | unsigned int set, clear, val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | |
Alan Cox | ae67751 | 2008-07-16 21:56:54 +0100 | [diff] [blame] | 2575 | if (tty->ops->tiocmset == NULL) |
Johan Hovold | 1b8b208 | 2021-04-07 11:52:02 +0200 | [diff] [blame] | 2576 | return -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | |
Alan Cox | ae67751 | 2008-07-16 21:56:54 +0100 | [diff] [blame] | 2578 | retval = get_user(val, p); |
| 2579 | if (retval) |
| 2580 | return retval; |
| 2581 | set = clear = 0; |
| 2582 | switch (cmd) { |
| 2583 | case TIOCMBIS: |
| 2584 | set = val; |
| 2585 | break; |
| 2586 | case TIOCMBIC: |
| 2587 | clear = val; |
| 2588 | break; |
| 2589 | case TIOCMSET: |
| 2590 | set = val; |
| 2591 | clear = ~val; |
| 2592 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | } |
Alan Cox | ae67751 | 2008-07-16 21:56:54 +0100 | [diff] [blame] | 2594 | set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; |
| 2595 | clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 2596 | return tty->ops->tiocmset(tty, set, clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | } |
| 2598 | |
Uwe Kleine-König | d20c219 | 2020-12-18 11:42:45 +0100 | [diff] [blame] | 2599 | /** |
| 2600 | * tty_get_icount - get tty statistics |
| 2601 | * @tty: tty device |
| 2602 | * @icount: output parameter |
| 2603 | * |
| 2604 | * Gets a copy of the tty's icount statistics. |
| 2605 | * |
| 2606 | * Locking: none (up to the driver) |
| 2607 | */ |
| 2608 | int tty_get_icount(struct tty_struct *tty, |
| 2609 | struct serial_icounter_struct *icount) |
| 2610 | { |
| 2611 | memset(icount, 0, sizeof(*icount)); |
| 2612 | |
| 2613 | if (tty->ops->get_icount) |
| 2614 | return tty->ops->get_icount(tty, icount); |
| 2615 | else |
Johan Hovold | 1b8b208 | 2021-04-07 11:52:02 +0200 | [diff] [blame] | 2616 | return -ENOTTY; |
Uwe Kleine-König | d20c219 | 2020-12-18 11:42:45 +0100 | [diff] [blame] | 2617 | } |
| 2618 | EXPORT_SYMBOL_GPL(tty_get_icount); |
| 2619 | |
Alan Cox | d281da7 | 2010-09-16 18:21:24 +0100 | [diff] [blame] | 2620 | static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) |
| 2621 | { |
Alan Cox | d281da7 | 2010-09-16 18:21:24 +0100 | [diff] [blame] | 2622 | struct serial_icounter_struct icount; |
Uwe Kleine-König | d20c219 | 2020-12-18 11:42:45 +0100 | [diff] [blame] | 2623 | int retval; |
| 2624 | |
| 2625 | retval = tty_get_icount(tty, &icount); |
Alan Cox | d281da7 | 2010-09-16 18:21:24 +0100 | [diff] [blame] | 2626 | if (retval != 0) |
| 2627 | return retval; |
Uwe Kleine-König | d20c219 | 2020-12-18 11:42:45 +0100 | [diff] [blame] | 2628 | |
Alan Cox | d281da7 | 2010-09-16 18:21:24 +0100 | [diff] [blame] | 2629 | if (copy_to_user(arg, &icount, sizeof(icount))) |
| 2630 | return -EFAULT; |
| 2631 | return 0; |
| 2632 | } |
| 2633 | |
Johan Hovold | 885c77d | 2021-04-07 11:52:05 +0200 | [diff] [blame] | 2634 | static int tty_set_serial(struct tty_struct *tty, struct serial_struct *ss) |
Jiri Slaby | 8a8ae62 | 2014-11-06 16:56:33 +0100 | [diff] [blame] | 2635 | { |
Jiri Slaby | 8a8ae62 | 2014-11-06 16:56:33 +0100 | [diff] [blame] | 2636 | char comm[TASK_COMM_LEN]; |
| 2637 | int flags; |
| 2638 | |
Johan Hovold | 885c77d | 2021-04-07 11:52:05 +0200 | [diff] [blame] | 2639 | flags = ss->flags & ASYNC_DEPRECATED; |
Jiri Slaby | 8a8ae62 | 2014-11-06 16:56:33 +0100 | [diff] [blame] | 2640 | |
Johan Hovold | 1b7bc6b | 2021-04-07 11:52:04 +0200 | [diff] [blame] | 2641 | if (flags) |
| 2642 | pr_warn_ratelimited("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n", |
| 2643 | __func__, get_task_comm(comm, current), flags); |
Johan Hovold | 885c77d | 2021-04-07 11:52:05 +0200 | [diff] [blame] | 2644 | |
Al Viro | 2f46a2c | 2018-09-11 21:53:32 -0400 | [diff] [blame] | 2645 | if (!tty->ops->set_serial) |
Al Viro | 930236a | 2018-09-12 07:46:51 -0400 | [diff] [blame] | 2646 | return -ENOTTY; |
Johan Hovold | 885c77d | 2021-04-07 11:52:05 +0200 | [diff] [blame] | 2647 | |
| 2648 | return tty->ops->set_serial(tty, ss); |
| 2649 | } |
| 2650 | |
| 2651 | static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss) |
| 2652 | { |
| 2653 | struct serial_struct v; |
| 2654 | |
| 2655 | if (copy_from_user(&v, ss, sizeof(*ss))) |
| 2656 | return -EFAULT; |
| 2657 | |
| 2658 | return tty_set_serial(tty, &v); |
Al Viro | 2f46a2c | 2018-09-11 21:53:32 -0400 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss) |
| 2662 | { |
| 2663 | struct serial_struct v; |
| 2664 | int err; |
| 2665 | |
Gustavo A. R. Silva | a324189 | 2020-07-23 17:34:22 -0500 | [diff] [blame] | 2666 | memset(&v, 0, sizeof(v)); |
Al Viro | 2f46a2c | 2018-09-11 21:53:32 -0400 | [diff] [blame] | 2667 | if (!tty->ops->get_serial) |
Al Viro | 930236a | 2018-09-12 07:46:51 -0400 | [diff] [blame] | 2668 | return -ENOTTY; |
Al Viro | 2f46a2c | 2018-09-11 21:53:32 -0400 | [diff] [blame] | 2669 | err = tty->ops->get_serial(tty, &v); |
Gustavo A. R. Silva | a324189 | 2020-07-23 17:34:22 -0500 | [diff] [blame] | 2670 | if (!err && copy_to_user(ss, &v, sizeof(v))) |
Al Viro | 2f46a2c | 2018-09-11 21:53:32 -0400 | [diff] [blame] | 2671 | err = -EFAULT; |
| 2672 | return err; |
Jiri Slaby | 8a8ae62 | 2014-11-06 16:56:33 +0100 | [diff] [blame] | 2673 | } |
| 2674 | |
Peter Hurley | 8f166e0 | 2014-10-16 14:59:41 -0400 | [diff] [blame] | 2675 | /* |
| 2676 | * if pty, return the slave side (real_tty) |
| 2677 | * otherwise, return self |
| 2678 | */ |
| 2679 | static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) |
Alan Cox | e8b70e7 | 2009-06-11 12:48:02 +0100 | [diff] [blame] | 2680 | { |
| 2681 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 2682 | tty->driver->subtype == PTY_TYPE_MASTER) |
| 2683 | tty = tty->link; |
| 2684 | return tty; |
| 2685 | } |
Alan Cox | e8b70e7 | 2009-06-11 12:48:02 +0100 | [diff] [blame] | 2686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | /* |
| 2688 | * Split this up, as gcc can choke on it otherwise.. |
| 2689 | */ |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 2690 | long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2692 | struct tty_struct *tty = file_tty(file); |
| 2693 | struct tty_struct *real_tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | void __user *p = (void __user *)arg; |
| 2695 | int retval; |
| 2696 | struct tty_ldisc *ld; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2697 | |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 2698 | if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | return -EINVAL; |
| 2700 | |
Alan Cox | e8b70e7 | 2009-06-11 12:48:02 +0100 | [diff] [blame] | 2701 | real_tty = tty_pair_get_tty(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | |
| 2703 | /* |
| 2704 | * Factor out some common prep work |
| 2705 | */ |
| 2706 | switch (cmd) { |
| 2707 | case TIOCSETD: |
| 2708 | case TIOCSBRK: |
| 2709 | case TIOCCBRK: |
| 2710 | case TCSBRK: |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2711 | case TCSBRKP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | retval = tty_check_change(tty); |
| 2713 | if (retval) |
| 2714 | return retval; |
| 2715 | if (cmd != TIOCCBRK) { |
| 2716 | tty_wait_until_sent(tty, 0); |
| 2717 | if (signal_pending(current)) |
| 2718 | return -EINTR; |
| 2719 | } |
| 2720 | break; |
| 2721 | } |
| 2722 | |
Alan Cox | 9e98966c | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 2723 | /* |
| 2724 | * Now do the stuff. |
| 2725 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | switch (cmd) { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2727 | case TIOCSTI: |
| 2728 | return tiocsti(tty, p); |
| 2729 | case TIOCGWINSZ: |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 2730 | return tiocgwinsz(real_tty, p); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2731 | case TIOCSWINSZ: |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 2732 | return tiocswinsz(real_tty, p); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2733 | case TIOCCONS: |
| 2734 | return real_tty != tty ? -EINVAL : tioccons(file); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2735 | case TIOCEXCL: |
| 2736 | set_bit(TTY_EXCLUSIVE, &tty->flags); |
| 2737 | return 0; |
| 2738 | case TIOCNXCL: |
| 2739 | clear_bit(TTY_EXCLUSIVE, &tty->flags); |
| 2740 | return 0; |
Cyrill Gorcunov | 84fd7bd | 2012-10-24 23:43:22 +0400 | [diff] [blame] | 2741 | case TIOCGEXCL: |
| 2742 | { |
| 2743 | int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); |
Xiaofei Tan | e73b240 | 2021-05-12 17:26:15 +0800 | [diff] [blame] | 2744 | |
Cyrill Gorcunov | 84fd7bd | 2012-10-24 23:43:22 +0400 | [diff] [blame] | 2745 | return put_user(excl, (int __user *)p); |
| 2746 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2747 | case TIOCGETD: |
Peter Hurley | 5c17c86 | 2016-01-10 22:40:55 -0800 | [diff] [blame] | 2748 | return tiocgetd(tty, p); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2749 | case TIOCSETD: |
| 2750 | return tiocsetd(tty, p); |
Kay Sievers | 3c95c98 | 2011-02-17 18:39:28 +0100 | [diff] [blame] | 2751 | case TIOCVHANGUP: |
| 2752 | if (!capable(CAP_SYS_ADMIN)) |
| 2753 | return -EPERM; |
| 2754 | tty_vhangup(tty); |
| 2755 | return 0; |
Werner Fink | b7b8de0 | 2010-12-03 12:48:23 +0100 | [diff] [blame] | 2756 | case TIOCGDEV: |
| 2757 | { |
| 2758 | unsigned int ret = new_encode_dev(tty_devnum(real_tty)); |
Xiaofei Tan | e73b240 | 2021-05-12 17:26:15 +0800 | [diff] [blame] | 2759 | |
Werner Fink | b7b8de0 | 2010-12-03 12:48:23 +0100 | [diff] [blame] | 2760 | return put_user(ret, (unsigned int __user *)p); |
| 2761 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2762 | /* |
| 2763 | * Break handling |
| 2764 | */ |
| 2765 | case TIOCSBRK: /* Turn break on, unconditionally */ |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2766 | if (tty->ops->break_ctl) |
Alan Cox | 9e98966c | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 2767 | return tty->ops->break_ctl(tty, -1); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2768 | return 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2769 | case TIOCCBRK: /* Turn break off, unconditionally */ |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2770 | if (tty->ops->break_ctl) |
Alan Cox | 9e98966c | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 2771 | return tty->ops->break_ctl(tty, 0); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2772 | return 0; |
| 2773 | case TCSBRK: /* SVID version: non-zero arg --> no break */ |
| 2774 | /* non-zero arg means wait for all output data |
| 2775 | * to be sent (performed above) but don't send break. |
| 2776 | * This is used by the tcdrain() termios function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2778 | if (!arg) |
| 2779 | return send_break(tty, 250); |
| 2780 | return 0; |
| 2781 | case TCSBRKP: /* support for POSIX tcsendbreak() */ |
| 2782 | return send_break(tty, arg ? arg*100 : 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2784 | case TIOCMGET: |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 2785 | return tty_tiocmget(tty, p); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2786 | case TIOCMSET: |
| 2787 | case TIOCMBIC: |
| 2788 | case TIOCMBIS: |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 2789 | return tty_tiocmset(tty, cmd, p); |
Alan Cox | d281da7 | 2010-09-16 18:21:24 +0100 | [diff] [blame] | 2790 | case TIOCGICOUNT: |
Al Viro | a309619 | 2018-09-12 18:37:18 -0400 | [diff] [blame] | 2791 | return tty_tiocgicount(tty, p); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2792 | case TCFLSH: |
| 2793 | switch (arg) { |
| 2794 | case TCIFLUSH: |
| 2795 | case TCIOFLUSH: |
| 2796 | /* flush tty buffer and allow ldisc to process ioctl */ |
Peter Hurley | 86c80a8 | 2014-11-05 12:13:09 -0500 | [diff] [blame] | 2797 | tty_buffer_flush(tty, NULL); |
Paul Fulghum | c5c34d4 | 2007-05-12 10:36:55 -0700 | [diff] [blame] | 2798 | break; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2799 | } |
| 2800 | break; |
Jiri Slaby | 8a8ae62 | 2014-11-06 16:56:33 +0100 | [diff] [blame] | 2801 | case TIOCSSERIAL: |
Al Viro | 930236a | 2018-09-12 07:46:51 -0400 | [diff] [blame] | 2802 | return tty_tiocsserial(tty, p); |
Al Viro | 2f46a2c | 2018-09-11 21:53:32 -0400 | [diff] [blame] | 2803 | case TIOCGSERIAL: |
Al Viro | 930236a | 2018-09-12 07:46:51 -0400 | [diff] [blame] | 2804 | return tty_tiocgserial(tty, p); |
Eric W. Biederman | 311fc65 | 2017-08-24 15:13:29 -0500 | [diff] [blame] | 2805 | case TIOCGPTPEER: |
| 2806 | /* Special because the struct file is needed */ |
| 2807 | return ptm_open_peer(file, tty, (int)arg); |
Nicolas Pitre | a1235b3 | 2017-04-12 18:37:16 -0400 | [diff] [blame] | 2808 | default: |
| 2809 | retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg); |
| 2810 | if (retval != -ENOIOCTLCMD) |
| 2811 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2813 | if (tty->ops->ioctl) { |
Peter Hurley | c961bfb | 2014-11-05 12:26:25 -0500 | [diff] [blame] | 2814 | retval = tty->ops->ioctl(tty, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | if (retval != -ENOIOCTLCMD) |
| 2816 | return retval; |
| 2817 | } |
| 2818 | ld = tty_ldisc_ref_wait(tty); |
Peter Hurley | e55afd1 | 2016-01-10 22:41:01 -0800 | [diff] [blame] | 2819 | if (!ld) |
| 2820 | return hung_up_tty_ioctl(file, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | retval = -EINVAL; |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2822 | if (ld->ops->ioctl) { |
| 2823 | retval = ld->ops->ioctl(tty, file, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | if (retval == -ENOIOCTLCMD) |
Wanlong Gao | bbb63c5 | 2012-08-27 15:23:12 +0800 | [diff] [blame] | 2825 | retval = -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | } |
| 2827 | tty_ldisc_deref(ld); |
| 2828 | return retval; |
| 2829 | } |
| 2830 | |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2831 | #ifdef CONFIG_COMPAT |
Al Viro | 7765435 | 2018-09-12 11:28:34 -0400 | [diff] [blame] | 2832 | |
| 2833 | struct serial_struct32 { |
Gustavo A. R. Silva | 52b52e9 | 2020-07-23 17:32:25 -0500 | [diff] [blame] | 2834 | compat_int_t type; |
| 2835 | compat_int_t line; |
| 2836 | compat_uint_t port; |
| 2837 | compat_int_t irq; |
| 2838 | compat_int_t flags; |
| 2839 | compat_int_t xmit_fifo_size; |
| 2840 | compat_int_t custom_divisor; |
| 2841 | compat_int_t baud_base; |
| 2842 | unsigned short close_delay; |
| 2843 | char io_type; |
| 2844 | char reserved_char; |
| 2845 | compat_int_t hub6; |
| 2846 | unsigned short closing_wait; /* time to wait before closing */ |
| 2847 | unsigned short closing_wait2; /* no longer used... */ |
| 2848 | compat_uint_t iomem_base; |
| 2849 | unsigned short iomem_reg_shift; |
| 2850 | unsigned int port_high; |
| 2851 | /* compat_ulong_t iomap_base FIXME */ |
| 2852 | compat_int_t reserved; |
Al Viro | 7765435 | 2018-09-12 11:28:34 -0400 | [diff] [blame] | 2853 | }; |
| 2854 | |
| 2855 | static int compat_tty_tiocsserial(struct tty_struct *tty, |
| 2856 | struct serial_struct32 __user *ss) |
| 2857 | { |
Al Viro | 7765435 | 2018-09-12 11:28:34 -0400 | [diff] [blame] | 2858 | struct serial_struct32 v32; |
| 2859 | struct serial_struct v; |
Al Viro | 7765435 | 2018-09-12 11:28:34 -0400 | [diff] [blame] | 2860 | |
Gustavo A. R. Silva | a324189 | 2020-07-23 17:34:22 -0500 | [diff] [blame] | 2861 | if (copy_from_user(&v32, ss, sizeof(*ss))) |
Al Viro | 7765435 | 2018-09-12 11:28:34 -0400 | [diff] [blame] | 2862 | return -EFAULT; |
| 2863 | |
| 2864 | memcpy(&v, &v32, offsetof(struct serial_struct32, iomem_base)); |
| 2865 | v.iomem_base = compat_ptr(v32.iomem_base); |
| 2866 | v.iomem_reg_shift = v32.iomem_reg_shift; |
| 2867 | v.port_high = v32.port_high; |
| 2868 | v.iomap_base = 0; |
| 2869 | |
Johan Hovold | 885c77d | 2021-04-07 11:52:05 +0200 | [diff] [blame] | 2870 | return tty_set_serial(tty, &v); |
Al Viro | 7765435 | 2018-09-12 11:28:34 -0400 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | static int compat_tty_tiocgserial(struct tty_struct *tty, |
| 2874 | struct serial_struct32 __user *ss) |
| 2875 | { |
| 2876 | struct serial_struct32 v32; |
| 2877 | struct serial_struct v; |
| 2878 | int err; |
Eric Biggers | 1732956 | 2020-02-24 10:20:43 -0800 | [diff] [blame] | 2879 | |
| 2880 | memset(&v, 0, sizeof(v)); |
| 2881 | memset(&v32, 0, sizeof(v32)); |
Al Viro | 7765435 | 2018-09-12 11:28:34 -0400 | [diff] [blame] | 2882 | |
Eric Biggers | 6e622cd | 2020-02-24 10:20:44 -0800 | [diff] [blame] | 2883 | if (!tty->ops->get_serial) |
Al Viro | 7765435 | 2018-09-12 11:28:34 -0400 | [diff] [blame] | 2884 | return -ENOTTY; |
| 2885 | err = tty->ops->get_serial(tty, &v); |
| 2886 | if (!err) { |
| 2887 | memcpy(&v32, &v, offsetof(struct serial_struct32, iomem_base)); |
| 2888 | v32.iomem_base = (unsigned long)v.iomem_base >> 32 ? |
| 2889 | 0xfffffff : ptr_to_compat(v.iomem_base); |
| 2890 | v32.iomem_reg_shift = v.iomem_reg_shift; |
| 2891 | v32.port_high = v.port_high; |
Gustavo A. R. Silva | a324189 | 2020-07-23 17:34:22 -0500 | [diff] [blame] | 2892 | if (copy_to_user(ss, &v32, sizeof(v32))) |
Al Viro | 7765435 | 2018-09-12 11:28:34 -0400 | [diff] [blame] | 2893 | err = -EFAULT; |
| 2894 | } |
| 2895 | return err; |
| 2896 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2897 | static long tty_compat_ioctl(struct file *file, unsigned int cmd, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2898 | unsigned long arg) |
| 2899 | { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2900 | struct tty_struct *tty = file_tty(file); |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2901 | struct tty_ldisc *ld; |
| 2902 | int retval = -ENOIOCTLCMD; |
| 2903 | |
Al Viro | e211203 | 2018-09-11 19:47:09 -0400 | [diff] [blame] | 2904 | switch (cmd) { |
Arnd Bergmann | c7dc504 | 2019-06-03 23:07:12 +0200 | [diff] [blame] | 2905 | case TIOCOUTQ: |
Al Viro | e211203 | 2018-09-11 19:47:09 -0400 | [diff] [blame] | 2906 | case TIOCSTI: |
| 2907 | case TIOCGWINSZ: |
| 2908 | case TIOCSWINSZ: |
| 2909 | case TIOCGEXCL: |
| 2910 | case TIOCGETD: |
| 2911 | case TIOCSETD: |
| 2912 | case TIOCGDEV: |
| 2913 | case TIOCMGET: |
| 2914 | case TIOCMSET: |
| 2915 | case TIOCMBIC: |
| 2916 | case TIOCMBIS: |
| 2917 | case TIOCGICOUNT: |
| 2918 | case TIOCGPGRP: |
| 2919 | case TIOCSPGRP: |
| 2920 | case TIOCGSID: |
| 2921 | case TIOCSERGETLSR: |
| 2922 | case TIOCGRS485: |
| 2923 | case TIOCSRS485: |
| 2924 | #ifdef TIOCGETP |
| 2925 | case TIOCGETP: |
| 2926 | case TIOCSETP: |
| 2927 | case TIOCSETN: |
| 2928 | #endif |
| 2929 | #ifdef TIOCGETC |
| 2930 | case TIOCGETC: |
| 2931 | case TIOCSETC: |
| 2932 | #endif |
| 2933 | #ifdef TIOCGLTC |
| 2934 | case TIOCGLTC: |
| 2935 | case TIOCSLTC: |
| 2936 | #endif |
| 2937 | case TCSETSF: |
| 2938 | case TCSETSW: |
| 2939 | case TCSETS: |
| 2940 | case TCGETS: |
| 2941 | #ifdef TCGETS2 |
| 2942 | case TCGETS2: |
| 2943 | case TCSETSF2: |
| 2944 | case TCSETSW2: |
| 2945 | case TCSETS2: |
| 2946 | #endif |
| 2947 | case TCGETA: |
| 2948 | case TCSETAF: |
| 2949 | case TCSETAW: |
| 2950 | case TCSETA: |
| 2951 | case TIOCGLCKTRMIOS: |
| 2952 | case TIOCSLCKTRMIOS: |
| 2953 | #ifdef TCGETX |
| 2954 | case TCGETX: |
| 2955 | case TCSETX: |
| 2956 | case TCSETXW: |
| 2957 | case TCSETXF: |
| 2958 | #endif |
| 2959 | case TIOCGSOFTCAR: |
| 2960 | case TIOCSSOFTCAR: |
Arnd Bergmann | b7aff09 | 2019-06-06 10:07:36 +0200 | [diff] [blame] | 2961 | |
| 2962 | case PPPIOCGCHAN: |
| 2963 | case PPPIOCGUNIT: |
Al Viro | e211203 | 2018-09-11 19:47:09 -0400 | [diff] [blame] | 2964 | return tty_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
| 2965 | case TIOCCONS: |
| 2966 | case TIOCEXCL: |
| 2967 | case TIOCNXCL: |
| 2968 | case TIOCVHANGUP: |
| 2969 | case TIOCSBRK: |
| 2970 | case TIOCCBRK: |
| 2971 | case TCSBRK: |
| 2972 | case TCSBRKP: |
| 2973 | case TCFLSH: |
| 2974 | case TIOCGPTPEER: |
| 2975 | case TIOCNOTTY: |
| 2976 | case TIOCSCTTY: |
| 2977 | case TCXONC: |
| 2978 | case TIOCMIWAIT: |
| 2979 | case TIOCSERCONFIG: |
| 2980 | return tty_ioctl(file, cmd, arg); |
| 2981 | } |
| 2982 | |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 2983 | if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2984 | return -EINVAL; |
| 2985 | |
Al Viro | 7765435 | 2018-09-12 11:28:34 -0400 | [diff] [blame] | 2986 | switch (cmd) { |
| 2987 | case TIOCSSERIAL: |
| 2988 | return compat_tty_tiocsserial(tty, compat_ptr(arg)); |
| 2989 | case TIOCGSERIAL: |
| 2990 | return compat_tty_tiocgserial(tty, compat_ptr(arg)); |
| 2991 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2992 | if (tty->ops->compat_ioctl) { |
Peter Hurley | c961bfb | 2014-11-05 12:26:25 -0500 | [diff] [blame] | 2993 | retval = tty->ops->compat_ioctl(tty, cmd, arg); |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2994 | if (retval != -ENOIOCTLCMD) |
| 2995 | return retval; |
| 2996 | } |
| 2997 | |
| 2998 | ld = tty_ldisc_ref_wait(tty); |
Peter Hurley | e55afd1 | 2016-01-10 22:41:01 -0800 | [diff] [blame] | 2999 | if (!ld) |
| 3000 | return hung_up_tty_compat_ioctl(file, cmd, arg); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 3001 | if (ld->ops->compat_ioctl) |
| 3002 | retval = ld->ops->compat_ioctl(tty, file, cmd, arg); |
Al Viro | f0193d3 | 2018-09-13 22:12:15 -0400 | [diff] [blame] | 3003 | if (retval == -ENOIOCTLCMD && ld->ops->ioctl) |
| 3004 | retval = ld->ops->ioctl(tty, file, |
| 3005 | (unsigned long)compat_ptr(cmd), arg); |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 3006 | tty_ldisc_deref(ld); |
| 3007 | |
| 3008 | return retval; |
| 3009 | } |
| 3010 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 3012 | static int this_tty(const void *t, struct file *file, unsigned fd) |
| 3013 | { |
Linus Torvalds | dd78b0c | 2021-01-19 10:49:19 -0800 | [diff] [blame] | 3014 | if (likely(file->f_op->read_iter != tty_read)) |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 3015 | return 0; |
| 3016 | return file_tty(file) != t ? 0 : fd + 1; |
| 3017 | } |
| 3018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | /* |
| 3020 | * This implements the "Secure Attention Key" --- the idea is to |
| 3021 | * prevent trojan horses by killing all processes associated with this |
| 3022 | * tty when the user hits the "Secure Attention Key". Required for |
| 3023 | * super-paranoid applications --- see the Orange Book for more details. |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3024 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | * This code could be nicer; ideally it should send a HUP, wait a few |
| 3026 | * seconds, then send a INT, and then a KILL signal. But you then |
| 3027 | * have to coordinate with the init process, since all processes associated |
| 3028 | * with the current tty must be dead before the new getty is allowed |
| 3029 | * to spawn. |
| 3030 | * |
| 3031 | * Now, if it would be correct ;-/ The current code has a nasty hole - |
| 3032 | * it doesn't catch files in flight. We may send the descriptor to ourselves |
| 3033 | * via AF_UNIX socket, close it and later fetch from socket. FIXME. |
| 3034 | * |
| 3035 | * Nasty bug: do_SAK is being called in interrupt context. This can |
| 3036 | * deadlock. We punt it up to process context. AKPM - 16Mar2001 |
| 3037 | */ |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 3038 | void __do_SAK(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | { |
| 3040 | #ifdef TTY_SOFT_SAK |
| 3041 | tty_hangup(tty); |
| 3042 | #else |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 3043 | struct task_struct *g, *p; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3044 | struct pid *session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | int i; |
Jann Horn | c8bcd9c | 2020-12-03 02:25:05 +0100 | [diff] [blame] | 3046 | unsigned long flags; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | if (!tty) |
| 3049 | return; |
Jann Horn | c8bcd9c | 2020-12-03 02:25:05 +0100 | [diff] [blame] | 3050 | |
Jiri Slaby | 64d608d | 2021-05-05 11:19:06 +0200 | [diff] [blame] | 3051 | spin_lock_irqsave(&tty->ctrl.lock, flags); |
| 3052 | session = get_pid(tty->ctrl.session); |
| 3053 | spin_unlock_irqrestore(&tty->ctrl.lock, flags); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3054 | |
Dan Carpenter | b3f13de | 2006-12-13 00:35:09 -0800 | [diff] [blame] | 3055 | tty_ldisc_flush(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3057 | tty_driver_flush_buffer(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | read_lock(&tasklist_lock); |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 3060 | /* Kill the entire session */ |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3061 | do_each_pid_task(session, PIDTYPE_SID, p) { |
Peter Hurley | 9b42bb7 | 2015-11-08 13:01:14 -0500 | [diff] [blame] | 3062 | tty_notice(tty, "SAK: killed process %d (%s): by session\n", |
| 3063 | task_pid_nr(p), p->comm); |
Eric W. Biederman | a8ebd17 | 2018-07-20 15:59:17 -0500 | [diff] [blame] | 3064 | group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3065 | } while_each_pid_task(session, PIDTYPE_SID, p); |
Peter Hurley | 9b42bb7 | 2015-11-08 13:01:14 -0500 | [diff] [blame] | 3066 | |
| 3067 | /* Now kill any processes that happen to have the tty open */ |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 3068 | do_each_thread(g, p) { |
| 3069 | if (p->signal->tty == tty) { |
Peter Hurley | 9b42bb7 | 2015-11-08 13:01:14 -0500 | [diff] [blame] | 3070 | tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n", |
| 3071 | task_pid_nr(p), p->comm); |
Eric W. Biederman | a8ebd17 | 2018-07-20 15:59:17 -0500 | [diff] [blame] | 3072 | group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | continue; |
| 3074 | } |
| 3075 | task_lock(p); |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 3076 | i = iterate_fd(p->files, 0, this_tty, tty); |
| 3077 | if (i != 0) { |
Peter Hurley | 9b42bb7 | 2015-11-08 13:01:14 -0500 | [diff] [blame] | 3078 | tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", |
| 3079 | task_pid_nr(p), p->comm, i - 1); |
Eric W. Biederman | a8ebd17 | 2018-07-20 15:59:17 -0500 | [diff] [blame] | 3080 | group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | } |
| 3082 | task_unlock(p); |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 3083 | } while_each_thread(g, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | read_unlock(&tasklist_lock); |
Jann Horn | c8bcd9c | 2020-12-03 02:25:05 +0100 | [diff] [blame] | 3085 | put_pid(session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | #endif |
| 3087 | } |
| 3088 | |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 3089 | static void do_SAK_work(struct work_struct *work) |
| 3090 | { |
| 3091 | struct tty_struct *tty = |
| 3092 | container_of(work, struct tty_struct, SAK_work); |
| 3093 | __do_SAK(tty); |
| 3094 | } |
| 3095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | /* |
| 3097 | * The tq handling here is a little racy - tty->SAK_work may already be queued. |
| 3098 | * Fortunately we don't need to worry, because if ->SAK_work is already queued, |
| 3099 | * the values which we write to it will be identical to the values which it |
| 3100 | * already has. --akpm |
| 3101 | */ |
| 3102 | void do_SAK(struct tty_struct *tty) |
| 3103 | { |
| 3104 | if (!tty) |
| 3105 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | schedule_work(&tty->SAK_work); |
| 3107 | } |
| 3108 | |
| 3109 | EXPORT_SYMBOL(do_SAK); |
| 3110 | |
Dmitry Eremin-Solenikov | 30004ac | 2010-08-09 18:22:49 +0400 | [diff] [blame] | 3111 | /* Must put_device() after it's unused! */ |
| 3112 | static struct device *tty_get_device(struct tty_struct *tty) |
| 3113 | { |
| 3114 | dev_t devt = tty_devnum(tty); |
Xiaofei Tan | e73b240 | 2021-05-12 17:26:15 +0800 | [diff] [blame] | 3115 | |
Suzuki K Poulose | 4495dfd | 2019-07-23 23:18:35 +0100 | [diff] [blame] | 3116 | return class_find_device_by_devt(tty_class, devt); |
Dmitry Eremin-Solenikov | 30004ac | 2010-08-09 18:22:49 +0400 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | |
Lee Jones | 08aa504 | 2020-11-04 19:35:25 +0000 | [diff] [blame] | 3120 | /* |
Rasmus Villemoes | 2c964a2 | 2014-07-10 21:01:22 +0200 | [diff] [blame] | 3121 | * alloc_tty_struct |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3122 | * |
Rasmus Villemoes | 2c964a2 | 2014-07-10 21:01:22 +0200 | [diff] [blame] | 3123 | * This subroutine allocates and initializes a tty structure. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3124 | * |
Rasmus Villemoes | 2c964a2 | 2014-07-10 21:01:22 +0200 | [diff] [blame] | 3125 | * Locking: none - tty in question is not exposed at this point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3127 | |
Rasmus Villemoes | 2c964a2 | 2014-07-10 21:01:22 +0200 | [diff] [blame] | 3128 | struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | { |
Rasmus Villemoes | 2c964a2 | 2014-07-10 21:01:22 +0200 | [diff] [blame] | 3130 | struct tty_struct *tty; |
| 3131 | |
| 3132 | tty = kzalloc(sizeof(*tty), GFP_KERNEL); |
| 3133 | if (!tty) |
| 3134 | return NULL; |
| 3135 | |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 3136 | kref_init(&tty->kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | tty->magic = TTY_MAGIC; |
Tetsuo Handa | 903f9db | 2018-04-05 19:40:16 +0900 | [diff] [blame] | 3138 | if (tty_ldisc_init(tty)) { |
| 3139 | kfree(tty); |
| 3140 | return NULL; |
| 3141 | } |
Jiri Slaby | 64d608d | 2021-05-05 11:19:06 +0200 | [diff] [blame] | 3142 | tty->ctrl.session = NULL; |
| 3143 | tty->ctrl.pgrp = NULL; |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 3144 | mutex_init(&tty->legacy_mutex); |
Peter Hurley | d8c1f92 | 2013-06-15 09:14:31 -0400 | [diff] [blame] | 3145 | mutex_init(&tty->throttle_mutex); |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 3146 | init_rwsem(&tty->termios_rwsem); |
Peter Hurley | dee4a0b | 2013-07-24 16:43:51 -0400 | [diff] [blame] | 3147 | mutex_init(&tty->winsize_mutex); |
Peter Hurley | 3669752 | 2013-06-15 07:04:48 -0400 | [diff] [blame] | 3148 | init_ldsem(&tty->ldisc_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | init_waitqueue_head(&tty->write_wait); |
| 3150 | init_waitqueue_head(&tty->read_wait); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3151 | INIT_WORK(&tty->hangup_work, do_tty_hangup); |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 3152 | mutex_init(&tty->atomic_write_lock); |
Jiri Slaby | 64d608d | 2021-05-05 11:19:06 +0200 | [diff] [blame] | 3153 | spin_lock_init(&tty->ctrl.lock); |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 3154 | spin_lock_init(&tty->flow.lock); |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 3155 | spin_lock_init(&tty->files_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | INIT_LIST_HEAD(&tty->tty_files); |
Eric W. Biederman | 7f1f86a | 2007-02-13 14:38:58 -0700 | [diff] [blame] | 3157 | INIT_WORK(&tty->SAK_work, do_SAK_work); |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 3158 | |
| 3159 | tty->driver = driver; |
| 3160 | tty->ops = driver->ops; |
| 3161 | tty->index = idx; |
| 3162 | tty_line_name(driver, idx, tty->name); |
Dmitry Eremin-Solenikov | 30004ac | 2010-08-09 18:22:49 +0400 | [diff] [blame] | 3163 | tty->dev = tty_get_device(tty); |
Rasmus Villemoes | 2c964a2 | 2014-07-10 21:01:22 +0200 | [diff] [blame] | 3164 | |
| 3165 | return tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | } |
| 3167 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3168 | /** |
| 3169 | * tty_put_char - write one character to a tty |
| 3170 | * @tty: tty |
| 3171 | * @ch: character |
| 3172 | * |
| 3173 | * Write one byte to the tty using the provided put_char method |
| 3174 | * if present. Returns the number of characters successfully output. |
| 3175 | * |
| 3176 | * Note: the specific put_char operation in the driver layer may go |
| 3177 | * away soon. Don't call it directly, use this method |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3179 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3180 | int tty_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3182 | if (tty->ops->put_char) |
| 3183 | return tty->ops->put_char(tty, ch); |
| 3184 | return tty->ops->write(tty, &ch, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3186 | EXPORT_SYMBOL_GPL(tty_put_char); |
| 3187 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 3188 | struct class *tty_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | |
Jiri Slaby | 7e73eca | 2012-08-08 22:26:44 +0200 | [diff] [blame] | 3190 | static int tty_cdev_add(struct tty_driver *driver, dev_t dev, |
| 3191 | unsigned int index, unsigned int count) |
| 3192 | { |
Leon Yu | c1a752b | 2015-09-07 13:08:37 +0000 | [diff] [blame] | 3193 | int err; |
| 3194 | |
Jiri Slaby | 7e73eca | 2012-08-08 22:26:44 +0200 | [diff] [blame] | 3195 | /* init here, since reused cdevs cause crashes */ |
Richard Watts | a3a10ce | 2015-05-19 16:06:53 +0100 | [diff] [blame] | 3196 | driver->cdevs[index] = cdev_alloc(); |
| 3197 | if (!driver->cdevs[index]) |
| 3198 | return -ENOMEM; |
Leon Yu | c1a752b | 2015-09-07 13:08:37 +0000 | [diff] [blame] | 3199 | driver->cdevs[index]->ops = &tty_fops; |
Richard Watts | a3a10ce | 2015-05-19 16:06:53 +0100 | [diff] [blame] | 3200 | driver->cdevs[index]->owner = driver->owner; |
Leon Yu | c1a752b | 2015-09-07 13:08:37 +0000 | [diff] [blame] | 3201 | err = cdev_add(driver->cdevs[index], dev, count); |
| 3202 | if (err) |
| 3203 | kobject_put(&driver->cdevs[index]->kobj); |
| 3204 | return err; |
Jiri Slaby | 7e73eca | 2012-08-08 22:26:44 +0200 | [diff] [blame] | 3205 | } |
| 3206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3208 | * tty_register_device - register a tty device |
| 3209 | * @driver: the tty driver that describes the tty device |
| 3210 | * @index: the index in the tty driver for this tty device |
| 3211 | * @device: a struct device that is associated with this tty device. |
| 3212 | * This field is optional, if there is no known struct device |
| 3213 | * for this tty device it can be set to NULL safely. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | * |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 3215 | * Returns a pointer to the struct device for this tty device |
| 3216 | * (or ERR_PTR(-EFOO) on error). |
Hansjoerg Lipp | 1cdcb6b | 2006-04-22 18:36:53 +0200 | [diff] [blame] | 3217 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3218 | * This call is required to be made to register an individual tty device |
| 3219 | * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If |
| 3220 | * that bit is not set, this function should not be called by a tty |
| 3221 | * driver. |
| 3222 | * |
| 3223 | * Locking: ?? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3224 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3225 | |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 3226 | struct device *tty_register_device(struct tty_driver *driver, unsigned index, |
| 3227 | struct device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | { |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 3229 | return tty_register_device_attr(driver, index, device, NULL, NULL); |
| 3230 | } |
| 3231 | EXPORT_SYMBOL(tty_register_device); |
| 3232 | |
Tomas Hlavacek | b1b7991 | 2012-09-06 23:17:47 +0200 | [diff] [blame] | 3233 | static void tty_device_create_release(struct device *dev) |
| 3234 | { |
Peter Hurley | 83db1df | 2015-11-08 13:01:21 -0500 | [diff] [blame] | 3235 | dev_dbg(dev, "releasing...\n"); |
Tomas Hlavacek | b1b7991 | 2012-09-06 23:17:47 +0200 | [diff] [blame] | 3236 | kfree(dev); |
| 3237 | } |
| 3238 | |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 3239 | /** |
| 3240 | * tty_register_device_attr - register a tty device |
| 3241 | * @driver: the tty driver that describes the tty device |
| 3242 | * @index: the index in the tty driver for this tty device |
| 3243 | * @device: a struct device that is associated with this tty device. |
| 3244 | * This field is optional, if there is no known struct device |
| 3245 | * for this tty device it can be set to NULL safely. |
| 3246 | * @drvdata: Driver data to be set to device. |
| 3247 | * @attr_grp: Attribute group to be set on device. |
| 3248 | * |
| 3249 | * Returns a pointer to the struct device for this tty device |
| 3250 | * (or ERR_PTR(-EFOO) on error). |
| 3251 | * |
| 3252 | * This call is required to be made to register an individual tty device |
| 3253 | * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If |
| 3254 | * that bit is not set, this function should not be called by a tty |
| 3255 | * driver. |
| 3256 | * |
| 3257 | * Locking: ?? |
| 3258 | */ |
| 3259 | struct device *tty_register_device_attr(struct tty_driver *driver, |
| 3260 | unsigned index, struct device *device, |
| 3261 | void *drvdata, |
| 3262 | const struct attribute_group **attr_grp) |
| 3263 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | char name[64]; |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 3265 | dev_t devt = MKDEV(driver->major, driver->minor_start) + index; |
Johan Hovold | 93857ed | 2017-03-30 15:39:36 +0200 | [diff] [blame] | 3266 | struct ktermios *tp; |
Johan Hovold | 6a7e6f7 | 2017-03-30 15:39:34 +0200 | [diff] [blame] | 3267 | struct device *dev; |
| 3268 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | |
| 3270 | if (index >= driver->num) { |
Peter Hurley | 656fb86 | 2015-11-08 13:01:15 -0500 | [diff] [blame] | 3271 | pr_err("%s: Attempt to register invalid tty line number (%d)\n", |
| 3272 | driver->name, index); |
Hansjoerg Lipp | 1cdcb6b | 2006-04-22 18:36:53 +0200 | [diff] [blame] | 3273 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | } |
| 3275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | if (driver->type == TTY_DRIVER_TYPE_PTY) |
| 3277 | pty_line_name(driver, index, name); |
| 3278 | else |
| 3279 | tty_line_name(driver, index, name); |
Hansjoerg Lipp | 1cdcb6b | 2006-04-22 18:36:53 +0200 | [diff] [blame] | 3280 | |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 3281 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Johan Hovold | 6a7e6f7 | 2017-03-30 15:39:34 +0200 | [diff] [blame] | 3282 | if (!dev) |
| 3283 | return ERR_PTR(-ENOMEM); |
Jiri Slaby | 7e73eca | 2012-08-08 22:26:44 +0200 | [diff] [blame] | 3284 | |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 3285 | dev->devt = devt; |
| 3286 | dev->class = tty_class; |
| 3287 | dev->parent = device; |
Tomas Hlavacek | b1b7991 | 2012-09-06 23:17:47 +0200 | [diff] [blame] | 3288 | dev->release = tty_device_create_release; |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 3289 | dev_set_name(dev, "%s", name); |
| 3290 | dev->groups = attr_grp; |
| 3291 | dev_set_drvdata(dev, drvdata); |
| 3292 | |
Johan Hovold | 6a7e6f7 | 2017-03-30 15:39:34 +0200 | [diff] [blame] | 3293 | dev_set_uevent_suppress(dev, 1); |
| 3294 | |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 3295 | retval = device_register(dev); |
| 3296 | if (retval) |
Johan Hovold | 6a7e6f7 | 2017-03-30 15:39:34 +0200 | [diff] [blame] | 3297 | goto err_put; |
| 3298 | |
| 3299 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { |
Johan Hovold | 93857ed | 2017-03-30 15:39:36 +0200 | [diff] [blame] | 3300 | /* |
| 3301 | * Free any saved termios data so that the termios state is |
| 3302 | * reset when reusing a minor number. |
| 3303 | */ |
| 3304 | tp = driver->termios[index]; |
| 3305 | if (tp) { |
| 3306 | driver->termios[index] = NULL; |
| 3307 | kfree(tp); |
| 3308 | } |
| 3309 | |
Johan Hovold | 6a7e6f7 | 2017-03-30 15:39:34 +0200 | [diff] [blame] | 3310 | retval = tty_cdev_add(driver, devt, index, 1); |
| 3311 | if (retval) |
| 3312 | goto err_del; |
| 3313 | } |
| 3314 | |
| 3315 | dev_set_uevent_suppress(dev, 0); |
| 3316 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 3317 | |
| 3318 | return dev; |
| 3319 | |
Johan Hovold | 6a7e6f7 | 2017-03-30 15:39:34 +0200 | [diff] [blame] | 3320 | err_del: |
| 3321 | device_del(dev); |
| 3322 | err_put: |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 3323 | put_device(dev); |
Johan Hovold | 6a7e6f7 | 2017-03-30 15:39:34 +0200 | [diff] [blame] | 3324 | |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 3325 | return ERR_PTR(retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | } |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 3327 | EXPORT_SYMBOL_GPL(tty_register_device_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3328 | |
| 3329 | /** |
Xiaofei Tan | 395e783 | 2021-05-12 17:26:14 +0800 | [diff] [blame] | 3330 | * tty_unregister_device - unregister a tty device |
| 3331 | * @driver: the tty driver that describes the tty device |
| 3332 | * @index: the index in the tty driver for this tty device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | * |
Xiaofei Tan | 395e783 | 2021-05-12 17:26:14 +0800 | [diff] [blame] | 3334 | * If a tty device is registered with a call to tty_register_device() then |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3335 | * this function must be called when the tty device is gone. |
| 3336 | * |
| 3337 | * Locking: ?? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | void tty_unregister_device(struct tty_driver *driver, unsigned index) |
| 3341 | { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3342 | device_destroy(tty_class, |
| 3343 | MKDEV(driver->major, driver->minor_start) + index); |
Richard Watts | a3a10ce | 2015-05-19 16:06:53 +0100 | [diff] [blame] | 3344 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { |
| 3345 | cdev_del(driver->cdevs[index]); |
| 3346 | driver->cdevs[index] = NULL; |
| 3347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | EXPORT_SYMBOL(tty_unregister_device); |
| 3350 | |
Jiri Slaby | 7f0bc6a | 2012-08-07 21:47:42 +0200 | [diff] [blame] | 3351 | /** |
| 3352 | * __tty_alloc_driver -- allocate tty driver |
| 3353 | * @lines: count of lines this driver can handle at most |
Thadeu Lima de Souza Cascardo | 87838ae | 2017-04-04 05:56:32 -0300 | [diff] [blame] | 3354 | * @owner: module which is responsible for this driver |
Jiri Slaby | 7f0bc6a | 2012-08-07 21:47:42 +0200 | [diff] [blame] | 3355 | * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags |
| 3356 | * |
| 3357 | * This should not be called directly, some of the provided macros should be |
| 3358 | * used instead. Use IS_ERR and friends on @retval. |
| 3359 | */ |
| 3360 | struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner, |
| 3361 | unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | { |
| 3363 | struct tty_driver *driver; |
Jiri Slaby | 7e73eca | 2012-08-08 22:26:44 +0200 | [diff] [blame] | 3364 | unsigned int cdevs = 1; |
Jiri Slaby | 16a0208 | 2012-08-08 22:26:42 +0200 | [diff] [blame] | 3365 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3366 | |
Jiri Slaby | 0019b40 | 2012-08-08 22:26:43 +0200 | [diff] [blame] | 3367 | if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1)) |
Jiri Slaby | 7f0bc6a | 2012-08-07 21:47:42 +0200 | [diff] [blame] | 3368 | return ERR_PTR(-EINVAL); |
| 3369 | |
Gustavo A. R. Silva | a324189 | 2020-07-23 17:34:22 -0500 | [diff] [blame] | 3370 | driver = kzalloc(sizeof(*driver), GFP_KERNEL); |
Jiri Slaby | 7f0bc6a | 2012-08-07 21:47:42 +0200 | [diff] [blame] | 3371 | if (!driver) |
| 3372 | return ERR_PTR(-ENOMEM); |
| 3373 | |
| 3374 | kref_init(&driver->kref); |
| 3375 | driver->magic = TTY_DRIVER_MAGIC; |
| 3376 | driver->num = lines; |
| 3377 | driver->owner = owner; |
| 3378 | driver->flags = flags; |
Jiri Slaby | 16a0208 | 2012-08-08 22:26:42 +0200 | [diff] [blame] | 3379 | |
| 3380 | if (!(flags & TTY_DRIVER_DEVPTS_MEM)) { |
| 3381 | driver->ttys = kcalloc(lines, sizeof(*driver->ttys), |
| 3382 | GFP_KERNEL); |
| 3383 | driver->termios = kcalloc(lines, sizeof(*driver->termios), |
| 3384 | GFP_KERNEL); |
| 3385 | if (!driver->ttys || !driver->termios) { |
| 3386 | err = -ENOMEM; |
| 3387 | goto err_free_all; |
| 3388 | } |
| 3389 | } |
| 3390 | |
| 3391 | if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) { |
| 3392 | driver->ports = kcalloc(lines, sizeof(*driver->ports), |
| 3393 | GFP_KERNEL); |
| 3394 | if (!driver->ports) { |
| 3395 | err = -ENOMEM; |
| 3396 | goto err_free_all; |
| 3397 | } |
Jiri Slaby | 7e73eca | 2012-08-08 22:26:44 +0200 | [diff] [blame] | 3398 | cdevs = lines; |
| 3399 | } |
| 3400 | |
| 3401 | driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL); |
| 3402 | if (!driver->cdevs) { |
| 3403 | err = -ENOMEM; |
| 3404 | goto err_free_all; |
Jiri Slaby | 16a0208 | 2012-08-08 22:26:42 +0200 | [diff] [blame] | 3405 | } |
Jiri Slaby | 7f0bc6a | 2012-08-07 21:47:42 +0200 | [diff] [blame] | 3406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | return driver; |
Jiri Slaby | 16a0208 | 2012-08-08 22:26:42 +0200 | [diff] [blame] | 3408 | err_free_all: |
| 3409 | kfree(driver->ports); |
| 3410 | kfree(driver->ttys); |
| 3411 | kfree(driver->termios); |
Richard Watts | a3a10ce | 2015-05-19 16:06:53 +0100 | [diff] [blame] | 3412 | kfree(driver->cdevs); |
Jiri Slaby | 16a0208 | 2012-08-08 22:26:42 +0200 | [diff] [blame] | 3413 | kfree(driver); |
| 3414 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | } |
Jiri Slaby | 7f0bc6a | 2012-08-07 21:47:42 +0200 | [diff] [blame] | 3416 | EXPORT_SYMBOL(__tty_alloc_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 3418 | static void destruct_tty_driver(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3419 | { |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 3420 | struct tty_driver *driver = container_of(kref, struct tty_driver, kref); |
| 3421 | int i; |
| 3422 | struct ktermios *tp; |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 3423 | |
| 3424 | if (driver->flags & TTY_DRIVER_INSTALLED) { |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 3425 | for (i = 0; i < driver->num; i++) { |
| 3426 | tp = driver->termios[i]; |
| 3427 | if (tp) { |
| 3428 | driver->termios[i] = NULL; |
| 3429 | kfree(tp); |
| 3430 | } |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 3431 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) |
| 3432 | tty_unregister_device(driver, i); |
| 3433 | } |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 3434 | proc_tty_unregister_driver(driver); |
Jiri Slaby | 7e73eca | 2012-08-08 22:26:44 +0200 | [diff] [blame] | 3435 | if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) |
Richard Watts | a3a10ce | 2015-05-19 16:06:53 +0100 | [diff] [blame] | 3436 | cdev_del(driver->cdevs[0]); |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 3437 | } |
Jiri Slaby | 7e73eca | 2012-08-08 22:26:44 +0200 | [diff] [blame] | 3438 | kfree(driver->cdevs); |
Jiri Slaby | 04831dc | 2012-06-04 13:35:36 +0200 | [diff] [blame] | 3439 | kfree(driver->ports); |
Jiri Slaby | 16a0208 | 2012-08-08 22:26:42 +0200 | [diff] [blame] | 3440 | kfree(driver->termios); |
| 3441 | kfree(driver->ttys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | kfree(driver); |
| 3443 | } |
| 3444 | |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 3445 | void tty_driver_kref_put(struct tty_driver *driver) |
| 3446 | { |
| 3447 | kref_put(&driver->kref, destruct_tty_driver); |
| 3448 | } |
| 3449 | EXPORT_SYMBOL(tty_driver_kref_put); |
| 3450 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 3451 | void tty_set_operations(struct tty_driver *driver, |
| 3452 | const struct tty_operations *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3454 | driver->ops = op; |
| 3455 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | EXPORT_SYMBOL(tty_set_operations); |
| 3457 | |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 3458 | void put_tty_driver(struct tty_driver *d) |
| 3459 | { |
| 3460 | tty_driver_kref_put(d); |
| 3461 | } |
| 3462 | EXPORT_SYMBOL(put_tty_driver); |
| 3463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | /* |
| 3465 | * Called by a tty driver to register itself. |
| 3466 | */ |
| 3467 | int tty_register_driver(struct tty_driver *driver) |
| 3468 | { |
| 3469 | int error; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3470 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | dev_t dev; |
Vasiliy Kulikov | b670bde | 2010-09-05 22:32:22 +0400 | [diff] [blame] | 3472 | struct device *d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | if (!driver->major) { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3475 | error = alloc_chrdev_region(&dev, driver->minor_start, |
| 3476 | driver->num, driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3477 | if (!error) { |
| 3478 | driver->major = MAJOR(dev); |
| 3479 | driver->minor_start = MINOR(dev); |
| 3480 | } |
| 3481 | } else { |
| 3482 | dev = MKDEV(driver->major, driver->minor_start); |
Geert Uytterhoeven | e5717c4 | 2007-02-20 15:45:21 +0100 | [diff] [blame] | 3483 | error = register_chrdev_region(dev, driver->num, driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | } |
Jiri Slaby | 9bb8a3d | 2012-06-04 13:35:35 +0200 | [diff] [blame] | 3485 | if (error < 0) |
Jiri Slaby | 16a0208 | 2012-08-08 22:26:42 +0200 | [diff] [blame] | 3486 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | |
Jiri Slaby | 7e73eca | 2012-08-08 22:26:44 +0200 | [diff] [blame] | 3488 | if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) { |
| 3489 | error = tty_cdev_add(driver, dev, 0, driver->num); |
| 3490 | if (error) |
| 3491 | goto err_unreg_char; |
| 3492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 3494 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | list_add(&driver->tty_drivers, &tty_drivers); |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 3496 | mutex_unlock(&tty_mutex); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3497 | |
| 3498 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { |
Vasiliy Kulikov | b670bde | 2010-09-05 22:32:22 +0400 | [diff] [blame] | 3499 | for (i = 0; i < driver->num; i++) { |
| 3500 | d = tty_register_device(driver, i, NULL); |
| 3501 | if (IS_ERR(d)) { |
| 3502 | error = PTR_ERR(d); |
Jiri Slaby | 16a0208 | 2012-08-08 22:26:42 +0200 | [diff] [blame] | 3503 | goto err_unreg_devs; |
Vasiliy Kulikov | b670bde | 2010-09-05 22:32:22 +0400 | [diff] [blame] | 3504 | } |
| 3505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | } |
| 3507 | proc_tty_register_driver(driver); |
Alan Cox | 7d7b93c | 2008-10-13 10:42:09 +0100 | [diff] [blame] | 3508 | driver->flags |= TTY_DRIVER_INSTALLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3509 | return 0; |
Vasiliy Kulikov | b670bde | 2010-09-05 22:32:22 +0400 | [diff] [blame] | 3510 | |
Jiri Slaby | 16a0208 | 2012-08-08 22:26:42 +0200 | [diff] [blame] | 3511 | err_unreg_devs: |
Vasiliy Kulikov | b670bde | 2010-09-05 22:32:22 +0400 | [diff] [blame] | 3512 | for (i--; i >= 0; i--) |
| 3513 | tty_unregister_device(driver, i); |
| 3514 | |
| 3515 | mutex_lock(&tty_mutex); |
| 3516 | list_del(&driver->tty_drivers); |
| 3517 | mutex_unlock(&tty_mutex); |
| 3518 | |
Jiri Slaby | 9bb8a3d | 2012-06-04 13:35:35 +0200 | [diff] [blame] | 3519 | err_unreg_char: |
Vasiliy Kulikov | b670bde | 2010-09-05 22:32:22 +0400 | [diff] [blame] | 3520 | unregister_chrdev_region(dev, driver->num); |
Jiri Slaby | 16a0208 | 2012-08-08 22:26:42 +0200 | [diff] [blame] | 3521 | err: |
Vasiliy Kulikov | b670bde | 2010-09-05 22:32:22 +0400 | [diff] [blame] | 3522 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | EXPORT_SYMBOL(tty_register_driver); |
| 3525 | |
| 3526 | /* |
| 3527 | * Called by a tty driver to unregister itself. |
| 3528 | */ |
Jiri Slaby | a872ab4 | 2021-03-02 07:22:05 +0100 | [diff] [blame] | 3529 | void tty_unregister_driver(struct tty_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), |
| 3532 | driver->num); |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 3533 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | list_del(&driver->tty_drivers); |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 3535 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | EXPORT_SYMBOL(tty_unregister_driver); |
| 3538 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3539 | dev_t tty_devnum(struct tty_struct *tty) |
| 3540 | { |
| 3541 | return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; |
| 3542 | } |
| 3543 | EXPORT_SYMBOL(tty_devnum); |
| 3544 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 3545 | void tty_default_fops(struct file_operations *fops) |
| 3546 | { |
| 3547 | *fops = tty_fops; |
| 3548 | } |
| 3549 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 3550 | static char *tty_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 3551 | { |
| 3552 | if (!mode) |
| 3553 | return NULL; |
| 3554 | if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) || |
| 3555 | dev->devt == MKDEV(TTYAUX_MAJOR, 2)) |
| 3556 | *mode = 0666; |
| 3557 | return NULL; |
| 3558 | } |
| 3559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | static int __init tty_class_init(void) |
| 3561 | { |
gregkh@suse.de | 7fe845d | 2005-03-15 14:23:15 -0800 | [diff] [blame] | 3562 | tty_class = class_create(THIS_MODULE, "tty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | if (IS_ERR(tty_class)) |
| 3564 | return PTR_ERR(tty_class); |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 3565 | tty_class->devnode = tty_devnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | return 0; |
| 3567 | } |
| 3568 | |
| 3569 | postcore_initcall(tty_class_init); |
| 3570 | |
| 3571 | /* 3/2004 jmc: why do these devices exist? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | static struct cdev tty_cdev, console_cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3573 | |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 3574 | static ssize_t show_cons_active(struct device *dev, |
| 3575 | struct device_attribute *attr, char *buf) |
| 3576 | { |
| 3577 | struct console *cs[16]; |
| 3578 | int i = 0; |
| 3579 | struct console *c; |
| 3580 | ssize_t count = 0; |
| 3581 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3582 | console_lock(); |
Kay Sievers | a2a6a82 | 2011-01-09 16:39:14 +0100 | [diff] [blame] | 3583 | for_each_console(c) { |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 3584 | if (!c->device) |
| 3585 | continue; |
| 3586 | if (!c->write) |
| 3587 | continue; |
| 3588 | if ((c->flags & CON_ENABLED) == 0) |
| 3589 | continue; |
| 3590 | cs[i++] = c; |
| 3591 | if (i >= ARRAY_SIZE(cs)) |
| 3592 | break; |
| 3593 | } |
Hannes Reinecke | 723abd8 | 2014-02-27 12:30:51 +0100 | [diff] [blame] | 3594 | while (i--) { |
| 3595 | int index = cs[i]->index; |
| 3596 | struct tty_driver *drv = cs[i]->device(cs[i], &index); |
| 3597 | |
| 3598 | /* don't resolve tty0 as some programs depend on it */ |
| 3599 | if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR)) |
| 3600 | count += tty_line_name(drv, index, buf + count); |
| 3601 | else |
| 3602 | count += sprintf(buf + count, "%s%d", |
| 3603 | cs[i]->name, cs[i]->index); |
| 3604 | |
| 3605 | count += sprintf(buf + count, "%c", i ? ' ':'\n'); |
| 3606 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3607 | console_unlock(); |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 3608 | |
| 3609 | return count; |
| 3610 | } |
| 3611 | static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL); |
| 3612 | |
Takashi Iwai | 1083a7b | 2015-02-05 11:07:42 +0100 | [diff] [blame] | 3613 | static struct attribute *cons_dev_attrs[] = { |
| 3614 | &dev_attr_active.attr, |
| 3615 | NULL |
| 3616 | }; |
| 3617 | |
| 3618 | ATTRIBUTE_GROUPS(cons_dev); |
| 3619 | |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 3620 | static struct device *consdev; |
| 3621 | |
| 3622 | void console_sysfs_notify(void) |
| 3623 | { |
| 3624 | if (consdev) |
| 3625 | sysfs_notify(&consdev->kobj, NULL, "active"); |
| 3626 | } |
| 3627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3628 | /* |
| 3629 | * Ok, now we can initialize the rest of the tty devices and can count |
| 3630 | * on memory allocations, interrupts etc.. |
| 3631 | */ |
David Howells | 31d1d48 | 2010-08-06 16:34:43 +0100 | [diff] [blame] | 3632 | int __init tty_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | { |
Greg Kroah-Hartman | 7c0cca7 | 2019-01-21 17:26:42 +0100 | [diff] [blame] | 3634 | tty_sysctl_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | cdev_init(&tty_cdev, &tty_fops); |
| 3636 | if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || |
| 3637 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) |
| 3638 | panic("Couldn't register /dev/tty driver\n"); |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 3639 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | |
| 3641 | cdev_init(&console_cdev, &console_fops); |
| 3642 | if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || |
| 3643 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) |
| 3644 | panic("Couldn't register /dev/console driver\n"); |
Takashi Iwai | 1083a7b | 2015-02-05 11:07:42 +0100 | [diff] [blame] | 3645 | consdev = device_create_with_groups(tty_class, NULL, |
| 3646 | MKDEV(TTYAUX_MAJOR, 1), NULL, |
| 3647 | cons_dev_groups, "console"); |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 3648 | if (IS_ERR(consdev)) |
| 3649 | consdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3651 | #ifdef CONFIG_VT |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 3652 | vty_init(&console_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | #endif |
| 3654 | return 0; |
| 3655 | } |
David Howells | 31d1d48 | 2010-08-06 16:34:43 +0100 | [diff] [blame] | 3656 | |