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