blob: ff0809d462eb0b469729b732dc8008326c389c06 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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 Cox37bdfb02008-02-08 04:18:47 -080021 * makes for cleaner and more compact code. -TYT, 9/17/92
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
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 Tan395e7832021-05-12 17:26:14 +080036 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
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 Cox37bdfb02008-02-08 04:18:47 -080043 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * 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 Coxd81ed102008-10-13 10:41:42 +010051 * Rewrote tty_init_dev and tty_release_dev to eliminate races.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * -- 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 Cox37bdfb02008-02-08 04:18:47 -080064 * alloc_tty_struct() always uses kmalloc()
65 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#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 Molnar3f07c012017-02-08 18:51:30 +010073#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010074#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#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 Viro9f3acc32008-04-24 07:44:08 -040081#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#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 Bergmannb7aff092019-06-06 10:07:36 +020090#include <linux/ppp-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include <linux/proc_fs.h>
92#include <linux/init.h>
93#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/wait.h>
96#include <linux/bitops.h>
Domen Puncerb20f3ae2005-06-25 14:58:42 -070097#include <linux/delay.h>
Alan Coxa352def2008-07-16 21:53:12 +010098#include <linux/seq_file.h>
Alan Coxd281da72010-09-16 18:21:24 +010099#include <linux/serial.h>
Manuel Zerpies5a3c6b2512011-06-16 14:07:22 +0200100#include <linux/ratelimit.h>
Al Viroe2112032018-09-11 19:47:09 -0400101#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Alan Coxa352def2008-07-16 21:53:12 +0100103#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105#include <linux/kbd_kern.h>
106#include <linux/vt_kern.h>
107#include <linux/selection.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109#include <linux/kmod.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700110#include <linux/nsproxy.h>
Greg Kroah-Hartman98602c02021-04-08 14:51:22 +0200111#include "tty.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113#undef TTY_DEBUG_HANGUP
Peter Hurleyaccff792015-07-12 22:49:09 -0400114#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 Torvalds1da177e2005-04-16 15:20:36 -0700119
120#define TTY_PARANOIA_CHECK 1
121#define CHECK_TTY_COUNT 1
122
Alan Coxedc6afc2006-12-08 02:38:44 -0800123struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .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 Coxedc6afc2006-12-08 02:38:44 -0800129 .c_cc = INIT_C_CC,
130 .c_ispeed = 38400,
Peter Hurley133b1302016-01-10 22:41:07 -0800131 .c_ospeed = 38400,
132 /* .c_line = N_TTY, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135EXPORT_SYMBOL(tty_std_termios);
136
137/* This list gets poked at by procfs and various bits of boot up code. This
138 could do with some rationalisation such as pulling the tty proc function
139 into this file */
Alan Cox37bdfb02008-02-08 04:18:47 -0800140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141LIST_HEAD(tty_drivers); /* linked list of tty drivers */
142
Peter Hurleyd1d027e2016-01-09 21:35:18 -0800143/* Mutex to protect creating and releasing a tty */
Ingo Molnar70522e12006-03-23 03:00:31 -0800144DEFINE_MUTEX(tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800146static ssize_t tty_read(struct kiocb *, struct iov_iter *);
Linus Torvalds9bb48c82021-01-19 11:41:16 -0800147static ssize_t tty_write(struct kiocb *, struct iov_iter *);
Al Viroafc9a422017-07-03 06:39:46 -0400148static __poll_t tty_poll(struct file *, poll_table *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static int tty_open(struct inode *, struct file *);
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700150#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -0800151static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700152 unsigned long arg);
153#else
154#define tty_compat_ioctl NULL
155#endif
Arnd Bergmannec79d602010-06-01 22:53:01 +0200156static int __tty_fasync(int fd, struct file *filp, int on);
Alan Cox37bdfb02008-02-08 04:18:47 -0800157static int tty_fasync(int fd, struct file *filp, int on);
Christoph Hellwigd5698c22007-02-10 01:46:46 -0800158static void release_tty(struct tty_struct *tty, int idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Alan Coxaf9b8972006-08-27 01:24:01 -0700160/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700161 * free_tty_struct - free a disused tty
162 * @tty: tty struct to free
163 *
164 * Free the write buffers, tty queue and tty memory itself.
165 *
166 * Locking: none. Must be called after tty is definitely unused
167 */
168
Peter Hurleya3123fd2016-01-09 21:13:48 -0800169static void free_tty_struct(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Peter Hurleyc8b710b2016-01-09 21:13:46 -0800171 tty_ldisc_deinit(tty);
Markus Elfringa211b1a2014-11-21 13:42:29 +0100172 put_device(tty->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 kfree(tty->write_buf);
Alan Cox89c8d912012-08-08 16:30:13 +0100174 tty->magic = 0xDEADDEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 kfree(tty);
176}
177
Nick Piggind996b622010-08-18 04:37:36 +1000178static inline struct tty_struct *file_tty(struct file *file)
179{
180 return ((struct tty_file_private *)file->private_data)->tty;
181}
182
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200183int tty_alloc_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000184{
185 struct tty_file_private *priv;
186
Pekka Enbergf573bd12010-08-24 07:48:34 +0300187 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
188 if (!priv)
189 return -ENOMEM;
Nick Piggind996b622010-08-18 04:37:36 +1000190
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200191 file->private_data = priv;
192
193 return 0;
194}
195
196/* Associate a new file with the tty structure */
197void tty_add_file(struct tty_struct *tty, struct file *file)
198{
199 struct tty_file_private *priv = file->private_data;
200
Nick Piggind996b622010-08-18 04:37:36 +1000201 priv->tty = tty;
202 priv->file = file;
Nick Piggind996b622010-08-18 04:37:36 +1000203
Peter Hurley4a510962016-01-09 21:35:23 -0800204 spin_lock(&tty->files_lock);
Nick Piggind996b622010-08-18 04:37:36 +1000205 list_add(&priv->list, &tty->tty_files);
Peter Hurley4a510962016-01-09 21:35:23 -0800206 spin_unlock(&tty->files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200207}
Pekka Enbergf573bd12010-08-24 07:48:34 +0300208
Lee Jones08aa5042020-11-04 19:35:25 +0000209/*
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200210 * tty_free_file - free file->private_data
211 *
212 * This shall be used only for fail path handling when tty_add_file was not
213 * called yet.
214 */
215void tty_free_file(struct file *file)
216{
217 struct tty_file_private *priv = file->private_data;
218
219 file->private_data = NULL;
220 kfree(priv);
Nick Piggind996b622010-08-18 04:37:36 +1000221}
222
223/* Delete file from its tty */
Josh Triplett2520e272012-11-18 21:27:47 -0800224static void tty_del_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000225{
226 struct tty_file_private *priv = file->private_data;
Peter Hurley4a510962016-01-09 21:35:23 -0800227 struct tty_struct *tty = priv->tty;
Nick Piggind996b622010-08-18 04:37:36 +1000228
Peter Hurley4a510962016-01-09 21:35:23 -0800229 spin_lock(&tty->files_lock);
Nick Piggind996b622010-08-18 04:37:36 +1000230 list_del(&priv->list);
Peter Hurley4a510962016-01-09 21:35:23 -0800231 spin_unlock(&tty->files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200232 tty_free_file(file);
Nick Piggind996b622010-08-18 04:37:36 +1000233}
234
Alan Coxaf9b8972006-08-27 01:24:01 -0700235/**
236 * tty_name - return tty naming
237 * @tty: tty structure
Alan Coxaf9b8972006-08-27 01:24:01 -0700238 *
239 * Convert a tty structure into a name. The name reflects the kernel
240 * naming policy and if udev is in use may not reflect user space
241 *
242 * Locking: none
243 */
244
Rasmus Villemoes429b4742015-03-31 15:55:59 +0200245const char *tty_name(const struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 if (!tty) /* Hmm. NULL pointer. That's fun. */
Rasmus Villemoes917162c2015-03-31 15:55:58 +0200248 return "NULL tty";
249 return tty->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252EXPORT_SYMBOL(tty_name);
253
Peter Hurley0a083ed2015-11-08 13:01:12 -0500254const char *tty_driver_name(const struct tty_struct *tty)
255{
256 if (!tty || !tty->driver)
257 return "";
258 return tty->driver->name;
259}
260
Peter Hurley82b8f882015-11-08 13:01:09 -0500261static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 const char *routine)
263{
264#ifdef TTY_PARANOIA_CHECK
265 if (!tty) {
Peter Hurley89222e62015-11-08 13:01:18 -0500266 pr_warn("(%d:%d): %s: NULL tty\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 imajor(inode), iminor(inode), routine);
268 return 1;
269 }
270 if (tty->magic != TTY_MAGIC) {
Peter Hurley89222e62015-11-08 13:01:18 -0500271 pr_warn("(%d:%d): %s: bad magic number\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 imajor(inode), iminor(inode), routine);
273 return 1;
274 }
275#endif
276 return 0;
277}
278
Peter Hurleydeb287e2014-11-05 12:12:55 -0500279/* Caller must hold tty_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static int check_tty_count(struct tty_struct *tty, const char *routine)
281{
282#ifdef CHECK_TTY_COUNT
283 struct list_head *p;
Okash Khawajaa09ac392017-07-20 08:22:36 +0100284 int count = 0, kopen_count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800285
Peter Hurley4a510962016-01-09 21:35:23 -0800286 spin_lock(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 list_for_each(p, &tty->tty_files) {
288 count++;
289 }
Peter Hurley4a510962016-01-09 21:35:23 -0800290 spin_unlock(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
292 tty->driver->subtype == PTY_TYPE_SLAVE &&
293 tty->link && tty->link->count)
294 count++;
Okash Khawajaa09ac392017-07-20 08:22:36 +0100295 if (tty_port_kopened(tty->port))
296 kopen_count++;
297 if (tty->count != (count + kopen_count)) {
298 tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n",
299 routine, tty->count, count, kopen_count);
300 return (count + kopen_count);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#endif
303 return 0;
304}
305
Alan Coxaf9b8972006-08-27 01:24:01 -0700306/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700307 * get_tty_driver - find device of a tty
Jiri Slabyfa441952020-08-18 10:56:51 +0200308 * @device: device identifier
Alan Coxaf9b8972006-08-27 01:24:01 -0700309 * @index: returns the index of the tty
310 *
311 * This routine returns a tty driver structure, given a device number
312 * and also passes back the index number.
313 *
314 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static struct tty_driver *get_tty_driver(dev_t device, int *index)
318{
319 struct tty_driver *p;
320
321 list_for_each_entry(p, &tty_drivers, tty_drivers) {
322 dev_t base = MKDEV(p->major, p->minor_start);
Xiaofei Tane73b2402021-05-12 17:26:15 +0800323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (device < base || device >= base + p->num)
325 continue;
326 *index = device - base;
Alan Cox7d7b93c2008-10-13 10:42:09 +0100327 return tty_driver_kref_get(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 return NULL;
330}
331
Okash Khawajafc61ed52017-06-25 19:40:00 +0100332/**
333 * tty_dev_name_to_number - return dev_t for device name
334 * @name: user space name of device under /dev
335 * @number: pointer to dev_t that this function will populate
336 *
337 * This function converts device names like ttyS0 or ttyUSB1 into dev_t
338 * like (4, 64) or (188, 1). If no corresponding driver is registered then
339 * the function returns -ENODEV.
340 *
341 * Locking: this acquires tty_mutex to protect the tty_drivers list from
342 * being modified while we are traversing it, and makes sure to
343 * release it before exiting.
344 */
345int tty_dev_name_to_number(const char *name, dev_t *number)
346{
347 struct tty_driver *p;
348 int ret;
349 int index, prefix_length = 0;
350 const char *str;
351
352 for (str = name; *str && !isdigit(*str); str++)
353 ;
354
355 if (!*str)
356 return -EINVAL;
357
358 ret = kstrtoint(str, 10, &index);
359 if (ret)
360 return ret;
361
362 prefix_length = str - name;
363 mutex_lock(&tty_mutex);
364
365 list_for_each_entry(p, &tty_drivers, tty_drivers)
366 if (prefix_length == strlen(p->name) && strncmp(name,
367 p->name, prefix_length) == 0) {
368 if (index < p->num) {
369 *number = MKDEV(p->major, p->minor_start + index);
370 goto out;
371 }
372 }
373
374 /* if here then driver wasn't found */
375 ret = -ENODEV;
376out:
377 mutex_unlock(&tty_mutex);
378 return ret;
379}
380EXPORT_SYMBOL_GPL(tty_dev_name_to_number);
381
Jason Wesself2d937f2008-04-17 20:05:37 +0200382#ifdef CONFIG_CONSOLE_POLL
383
384/**
385 * tty_find_polling_driver - find device of a polled tty
386 * @name: name string to match
387 * @line: pointer to resulting tty line nr
388 *
389 * This routine returns a tty driver structure, given a name
390 * and the condition that the tty driver is capable of polled
391 * operation.
392 */
393struct tty_driver *tty_find_polling_driver(char *name, int *line)
394{
395 struct tty_driver *p, *res = NULL;
396 int tty_line = 0;
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500397 int len;
Alan Cox5f0878a2009-06-11 12:46:41 +0100398 char *str, *stp;
Jason Wesself2d937f2008-04-17 20:05:37 +0200399
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500400 for (str = name; *str; str++)
401 if ((*str >= '0' && *str <= '9') || *str == ',')
402 break;
403 if (!*str)
404 return NULL;
405
406 len = str - name;
407 tty_line = simple_strtoul(str, &str, 10);
408
Jason Wesself2d937f2008-04-17 20:05:37 +0200409 mutex_lock(&tty_mutex);
410 /* Search through the tty devices to look for a match */
411 list_for_each_entry(p, &tty_drivers, tty_drivers) {
Miles Chen33a1a7b2018-10-08 10:39:17 +0800412 if (!len || strncmp(name, p->name, len) != 0)
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500413 continue;
Alan Cox5f0878a2009-06-11 12:46:41 +0100414 stp = str;
415 if (*stp == ',')
416 stp++;
417 if (*stp == '\0')
418 stp = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +0200419
Nathael Pajani6eb68d62010-09-02 16:06:16 +0200420 if (tty_line >= 0 && tty_line < p->num && p->ops &&
Alan Cox5f0878a2009-06-11 12:46:41 +0100421 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
Alan Cox7d7b93c2008-10-13 10:42:09 +0100422 res = tty_driver_kref_get(p);
Jason Wesself2d937f2008-04-17 20:05:37 +0200423 *line = tty_line;
424 break;
425 }
426 }
427 mutex_unlock(&tty_mutex);
428
429 return res;
430}
431EXPORT_SYMBOL_GPL(tty_find_polling_driver);
432#endif
433
Linus Torvaldsddc5fda2021-01-21 10:08:15 -0800434static ssize_t hung_up_tty_read(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 return 0;
437}
438
Linus Torvalds17749852021-01-21 10:04:27 -0800439static ssize_t hung_up_tty_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 return -EIO;
442}
443
444/* No kernel lock held - none needed ;) */
Al Viroafc9a422017-07-03 06:39:46 -0400445static __poll_t hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800447 return EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP | EPOLLRDNORM | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Alan Cox04f378b2008-04-30 00:53:29 -0700450static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
451 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700452{
453 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
454}
455
Alan Cox37bdfb02008-02-08 04:18:47 -0800456static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700457 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
460}
461
Peter Hurleyf5574742016-01-09 21:45:10 -0800462static int hung_up_tty_fasync(int fd, struct file *file, int on)
463{
464 return -ENOTTY;
465}
466
Masatake YAMATOd01c3282017-07-18 06:27:59 +0900467static void tty_show_fdinfo(struct seq_file *m, struct file *file)
468{
469 struct tty_struct *tty = file_tty(file);
470
471 if (tty && tty->ops && tty->ops->show_fdinfo)
472 tty->ops->show_fdinfo(tty, m);
473}
474
Arjan van de Ven62322d22006-07-03 00:24:21 -0700475static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 .llseek = no_llseek,
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800477 .read_iter = tty_read,
Linus Torvalds9bb48c82021-01-19 11:41:16 -0800478 .write_iter = tty_write,
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800479 .splice_read = generic_file_splice_read,
Linus Torvalds9bb48c82021-01-19 11:41:16 -0800480 .splice_write = iter_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700482 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700483 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 .open = tty_open,
485 .release = tty_release,
486 .fasync = tty_fasync,
Masatake YAMATOd01c3282017-07-18 06:27:59 +0900487 .show_fdinfo = tty_show_fdinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488};
489
Arjan van de Ven62322d22006-07-03 00:24:21 -0700490static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 .llseek = no_llseek,
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800492 .read_iter = tty_read,
Linus Torvalds9bb48c82021-01-19 11:41:16 -0800493 .write_iter = redirected_tty_write,
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800494 .splice_read = generic_file_splice_read,
Linus Torvalds9bb48c82021-01-19 11:41:16 -0800495 .splice_write = iter_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700497 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700498 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 .open = tty_open,
500 .release = tty_release,
501 .fasync = tty_fasync,
502};
503
Arjan van de Ven62322d22006-07-03 00:24:21 -0700504static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .llseek = no_llseek,
Linus Torvaldsddc5fda2021-01-21 10:08:15 -0800506 .read_iter = hung_up_tty_read,
Linus Torvalds17749852021-01-21 10:04:27 -0800507 .write_iter = hung_up_tty_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700509 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700510 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 .release = tty_release,
Peter Hurleyf5574742016-01-09 21:45:10 -0800512 .fasync = hung_up_tty_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513};
514
515static DEFINE_SPINLOCK(redirect_lock);
516static struct file *redirect;
517
518/**
519 * tty_wakeup - request more data
520 * @tty: terminal
521 *
522 * Internal and external helper for wakeups of tty. This function
523 * informs the line discipline if present that the driver is ready
524 * to receive more output data.
525 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527void tty_wakeup(struct tty_struct *tty)
528{
529 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -0800530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
532 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800533 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100534 if (ld->ops->write_wakeup)
535 ld->ops->write_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 tty_ldisc_deref(ld);
537 }
538 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800539 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542EXPORT_SYMBOL_GPL(tty_wakeup);
543
544/**
Corey Minyardf4467762020-11-23 18:49:01 -0600545 * tty_release_redirect - Release a redirect on a pty if present
546 * @tty: tty device
547 *
548 * This is available to the pty code so if the master closes, if the
Greg Kroah-Hartmanc776b772021-03-04 17:18:02 +0100549 * slave is a redirect it can release the redirect.
Corey Minyardf4467762020-11-23 18:49:01 -0600550 */
Greg Kroah-Hartmandd9f6112021-04-08 14:51:31 +0200551static struct file *tty_release_redirect(struct tty_struct *tty)
Corey Minyardf4467762020-11-23 18:49:01 -0600552{
553 struct file *f = NULL;
554
555 spin_lock(&redirect_lock);
556 if (redirect && file_tty(redirect) == tty) {
557 f = redirect;
558 redirect = NULL;
559 }
560 spin_unlock(&redirect_lock);
561
562 return f;
563}
Corey Minyardf4467762020-11-23 18:49:01 -0600564
565/**
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200566 * __tty_hangup - actual handler for hangup events
Jiri Slabyfa441952020-08-18 10:56:51 +0200567 * @tty: tty device
Lee Jones08aa5042020-11-04 19:35:25 +0000568 * @exit_session: if non-zero, signal all foreground group processes
Alan Coxaf9b8972006-08-27 01:24:01 -0700569 *
Kevin Cernekeeef4f5272012-12-26 20:43:41 -0800570 * This can be called by a "kworker" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -0700571 * synchronous but doesn't hold any locks, so we need to make sure we
572 * have the appropriate locks for what we're doing.
573 *
574 * The hangup event clears any pending redirections onto the hung up
575 * device. It ensures future writes will error and it does the needed
576 * line discipline hangup and signal delivery. The tty object itself
577 * remains intact.
578 *
579 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200580 * BTM
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800581 * redirect lock for undoing redirection
582 * file list lock for manipulating list of ttys
Peter Hurley137084b2013-06-15 07:04:46 -0400583 * tty_ldiscs_lock from called functions
Peter Hurley6a1c0682013-06-15 09:14:23 -0400584 * termios_rwsem resetting termios data
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800585 * tasklist_lock to walk task list for hangup event
586 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
Peter Hurleyf91e2592013-03-06 07:20:56 -0500588static void __tty_hangup(struct tty_struct *tty, int exit_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Alan Cox37bdfb02008-02-08 04:18:47 -0800590 struct file *cons_filp = NULL;
Corey Minyardf4467762020-11-23 18:49:01 -0600591 struct file *filp, *f;
Nick Piggind996b622010-08-18 04:37:36 +1000592 struct tty_file_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int closecount = 0, n;
Peter Hurleyea648a42013-03-06 07:20:53 -0500594 int refs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 if (!tty)
597 return;
598
Corey Minyardf4467762020-11-23 18:49:01 -0600599 f = tty_release_redirect(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800600
Alan Cox89c8d912012-08-08 16:30:13 +0100601 tty_lock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200602
Peter Hurleycb50e522013-07-31 14:05:45 -0400603 if (test_bit(TTY_HUPPED, &tty->flags)) {
604 tty_unlock(tty);
605 return;
606 }
607
Tejun Heo28b0f8a2018-02-13 07:38:08 -0800608 /*
609 * Some console devices aren't actually hung up for technical and
610 * historical reasons, which can lead to indefinite interruptible
611 * sleep in n_tty_read(). The following explicitly tells
612 * n_tty_read() to abort readers.
613 */
614 set_bit(TTY_HUPPING, &tty->flags);
615
Arnd Bergmannec79d602010-06-01 22:53:01 +0200616 /* inuse_filps is protected by the single tty lock,
617 this really needs to change if we want to flush the
618 workqueue with the lock held */
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200619 check_tty_count(tty, "tty_hangup");
Alan Cox36ba7822009-11-30 13:18:51 +0000620
Peter Hurley4a510962016-01-09 21:35:23 -0800621 spin_lock(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Nick Piggind996b622010-08-18 04:37:36 +1000623 list_for_each_entry(priv, &tty->tty_files, list) {
624 filp = priv->file;
Linus Torvalds9bb48c82021-01-19 11:41:16 -0800625 if (filp->f_op->write_iter == redirected_tty_write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 cons_filp = filp;
Linus Torvalds9bb48c82021-01-19 11:41:16 -0800627 if (filp->f_op->write_iter != tty_write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 continue;
629 closecount++;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200630 __tty_fasync(-1, filp, 0); /* can't block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 filp->f_op = &hung_up_tty_fops;
632 }
Peter Hurley4a510962016-01-09 21:35:23 -0800633 spin_unlock(&tty->files_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800634
Peter Hurley25fdf242013-03-06 07:20:57 -0500635 refs = tty_signal_session_leader(tty, exit_session);
636 /* Account for the p->signal references we killed */
637 while (refs--)
638 tty_kref_put(tty);
639
Peter Hurley892d1fa2016-01-10 22:41:06 -0800640 tty_ldisc_hangup(tty, cons_filp != NULL);
Alan Cox37bdfb02008-02-08 04:18:47 -0800641
Jiri Slaby64d608d2021-05-05 11:19:06 +0200642 spin_lock_irq(&tty->ctrl.lock);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100643 clear_bit(TTY_THROTTLED, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100644 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Jiri Slaby64d608d2021-05-05 11:19:06 +0200645 put_pid(tty->ctrl.session);
646 put_pid(tty->ctrl.pgrp);
647 tty->ctrl.session = NULL;
648 tty->ctrl.pgrp = NULL;
649 tty->ctrl.pktstatus = 0;
650 spin_unlock_irq(&tty->ctrl.lock);
Alan Cox9c9f4de2008-10-13 10:37:26 +0100651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
Alan Cox37bdfb02008-02-08 04:18:47 -0800653 * If one of the devices matches a console pointer, we
654 * cannot just call hangup() because that will cause
655 * tty->count and state->count to go out of sync.
656 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -0700659 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -0700661 tty->ops->close(tty, cons_filp);
662 } else if (tty->ops->hangup)
Peter Hurley7c6d3402014-06-16 09:17:08 -0400663 tty->ops->hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800664 /*
Peter Hurley892d1fa2016-01-10 22:41:06 -0800665 * We don't want to have driver/ldisc interactions beyond the ones
666 * we did here. The driver layer expects no calls after ->hangup()
667 * from the ldisc side, which is now guaranteed.
Alan Cox37bdfb02008-02-08 04:18:47 -0800668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 set_bit(TTY_HUPPED, &tty->flags);
Tejun Heo28b0f8a2018-02-13 07:38:08 -0800670 clear_bit(TTY_HUPPING, &tty->flags);
Alan Cox89c8d912012-08-08 16:30:13 +0100671 tty_unlock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (f)
674 fput(f);
675}
676
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200677static void do_tty_hangup(struct work_struct *work)
678{
679 struct tty_struct *tty =
680 container_of(work, struct tty_struct, hangup_work);
681
Peter Hurleyf91e2592013-03-06 07:20:56 -0500682 __tty_hangup(tty, 0);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200683}
684
Alan Coxaf9b8972006-08-27 01:24:01 -0700685/**
686 * tty_hangup - trigger a hangup event
687 * @tty: tty to hangup
688 *
689 * A carrier loss (virtual or otherwise) has occurred on this like
690 * schedule a hangup sequence to run after this event.
691 */
692
Alan Cox37bdfb02008-02-08 04:18:47 -0800693void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Peter Hurleyd435cef2015-11-08 13:01:19 -0500695 tty_debug_hangup(tty, "hangup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 schedule_work(&tty->hangup_work);
697}
698
699EXPORT_SYMBOL(tty_hangup);
700
Alan Coxaf9b8972006-08-27 01:24:01 -0700701/**
702 * tty_vhangup - process vhangup
703 * @tty: tty to hangup
704 *
705 * The user has asked via system call for the terminal to be hung up.
706 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200707 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -0700708 */
709
Alan Cox37bdfb02008-02-08 04:18:47 -0800710void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Peter Hurleyd435cef2015-11-08 13:01:19 -0500712 tty_debug_hangup(tty, "vhangup\n");
Peter Hurleyf91e2592013-03-06 07:20:56 -0500713 __tty_hangup(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
Alan Cox37bdfb02008-02-08 04:18:47 -0800715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716EXPORT_SYMBOL(tty_vhangup);
717
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200718
Alan Coxaf9b8972006-08-27 01:24:01 -0700719/**
Alan Cox2cb59982008-10-13 10:40:30 +0100720 * tty_vhangup_self - process vhangup for own ctty
721 *
722 * Perform a vhangup on the current controlling tty
723 */
724
725void tty_vhangup_self(void)
726{
727 struct tty_struct *tty;
728
Alan Cox2cb59982008-10-13 10:40:30 +0100729 tty = get_current_tty();
730 if (tty) {
731 tty_vhangup(tty);
732 tty_kref_put(tty);
733 }
Alan Cox2cb59982008-10-13 10:40:30 +0100734}
735
736/**
Peter Hurleyf91e2592013-03-06 07:20:56 -0500737 * tty_vhangup_session - hangup session leader exit
738 * @tty: tty to hangup
739 *
740 * The session leader is exiting and hanging up its controlling terminal.
741 * Every process in the foreground process group is signalled SIGHUP.
742 *
743 * We do this synchronously so that when the syscall returns the process
744 * is complete. That guarantee is necessary for security reasons.
745 */
746
Nicolas Pitrea1235b32017-04-12 18:37:16 -0400747void tty_vhangup_session(struct tty_struct *tty)
Peter Hurleyf91e2592013-03-06 07:20:56 -0500748{
Peter Hurleyd435cef2015-11-08 13:01:19 -0500749 tty_debug_hangup(tty, "session hangup\n");
Peter Hurleyf91e2592013-03-06 07:20:56 -0500750 __tty_hangup(tty, 1);
751}
752
753/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700754 * tty_hung_up_p - was tty hung up
755 * @filp: file pointer of tty
756 *
757 * Return true if the tty has been subject to a vhangup or a carrier
758 * loss
759 */
760
Alan Cox37bdfb02008-02-08 04:18:47 -0800761int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Alan Coxed3f0af2017-01-16 16:54:29 -0600763 return (filp && filp->f_op == &hung_up_tty_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766EXPORT_SYMBOL(tty_hung_up_p);
767
Alan Coxaf9b8972006-08-27 01:24:01 -0700768/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200769 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700770 * @tty: tty to stop
771 *
Peter Hurley01adc802014-09-10 15:06:32 -0400772 * Perform flow control to the driver. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -0700773 * on an already stopped device and will not re-call the driver
774 * method.
775 *
776 * This functionality is used by both the line disciplines for
777 * halting incoming flow and by the driver. It may therefore be
778 * called from any context, may be under the tty atomic_write_lock
779 * but not always.
780 *
781 * Locking:
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200782 * flow.lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700783 */
784
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400785void __stop_tty(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200787 if (tty->flow.stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return;
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200789 tty->flow.stopped = true;
Alan Coxf34d7a52008-04-30 00:54:13 -0700790 if (tty->ops->stop)
Peter Hurleyc961bfb2014-11-05 12:26:25 -0500791 tty->ops->stop(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400794void stop_tty(struct tty_struct *tty)
795{
796 unsigned long flags;
797
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200798 spin_lock_irqsave(&tty->flow.lock, flags);
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400799 __stop_tty(tty);
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200800 spin_unlock_irqrestore(&tty->flow.lock, flags);
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400801}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802EXPORT_SYMBOL(stop_tty);
803
Alan Coxaf9b8972006-08-27 01:24:01 -0700804/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200805 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700806 * @tty: tty to start
807 *
Peter Hurley01adc802014-09-10 15:06:32 -0400808 * Start a tty that has been stopped if at all possible. If this
809 * tty was previous stopped and is now being started, the driver
810 * start method is invoked and the line discipline woken.
Alan Coxaf9b8972006-08-27 01:24:01 -0700811 *
812 * Locking:
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200813 * flow.lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700814 */
815
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400816void __start_tty(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200818 if (!tty->flow.stopped || tty->flow.tco_stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return;
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200820 tty->flow.stopped = false;
Alan Coxf34d7a52008-04-30 00:54:13 -0700821 if (tty->ops->start)
Peter Hurleyc961bfb2014-11-05 12:26:25 -0500822 tty->ops->start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400826void start_tty(struct tty_struct *tty)
827{
828 unsigned long flags;
829
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200830 spin_lock_irqsave(&tty->flow.lock, flags);
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400831 __start_tty(tty);
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200832 spin_unlock_irqrestore(&tty->flow.lock, flags);
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400833}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834EXPORT_SYMBOL(start_tty);
835
Arnd Bergmannc884f872018-06-18 16:16:26 +0200836static void tty_update_time(struct timespec64 *time)
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200837{
Arnd Bergmannc884f872018-06-18 16:16:26 +0200838 time64_t sec = ktime_get_real_seconds();
Greg Kroah-Hartmanfbf47632015-03-26 23:10:27 +0100839
840 /*
841 * We only care if the two values differ in anything other than the
842 * lower three bits (i.e every 8 seconds). If so, then we can update
843 * the time of the tty device, otherwise it could be construded as a
844 * security leak to let userspace know the exact timing of the tty.
845 */
846 if ((sec ^ time->tv_sec) & ~7)
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200847 time->tv_sec = sec;
848}
849
Linus Torvalds3b830a92021-01-18 13:31:30 -0800850/*
851 * Iterate on the ldisc ->read() function until we've gotten all
852 * the data the ldisc has for us.
853 *
854 * The "cookie" is something that the ldisc read function can fill
855 * in to let us know that there is more data to be had.
856 *
857 * We promise to continue to call the ldisc until it stops returning
858 * data or clears the cookie. The cookie may be something that the
859 * ldisc maintains state for and needs to free.
860 */
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800861static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty,
862 struct file *file, struct iov_iter *to)
Linus Torvalds3b830a92021-01-18 13:31:30 -0800863{
864 int retval = 0;
865 void *cookie = NULL;
866 unsigned long offset = 0;
867 char kernel_buf[64];
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800868 size_t count = iov_iter_count(to);
Linus Torvalds3b830a92021-01-18 13:31:30 -0800869
870 do {
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800871 int size, copied;
Linus Torvalds3b830a92021-01-18 13:31:30 -0800872
873 size = count > sizeof(kernel_buf) ? sizeof(kernel_buf) : count;
874 size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset);
875 if (!size)
876 break;
877
Linus Torvalds3b830a92021-01-18 13:31:30 -0800878 if (size < 0) {
Linus Torvaldse71a8d52021-01-21 10:17:25 -0800879 /* Did we have an earlier error (ie -EFAULT)? */
880 if (retval)
881 break;
882 retval = size;
883
884 /*
885 * -EOVERFLOW means we didn't have enough space
886 * for a whole packet, and we shouldn't return
887 * a partial result.
888 */
889 if (retval == -EOVERFLOW)
890 offset = 0;
891 break;
Linus Torvalds3b830a92021-01-18 13:31:30 -0800892 }
893
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800894 copied = copy_to_iter(kernel_buf, size, to);
895 offset += copied;
896 count -= copied;
Linus Torvalds3b830a92021-01-18 13:31:30 -0800897
898 /*
899 * If the user copy failed, we still need to do another ->read()
900 * call if we had a cookie to let the ldisc clear up.
901 *
902 * But make sure size is zeroed.
903 */
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800904 if (unlikely(copied != size)) {
Linus Torvalds3b830a92021-01-18 13:31:30 -0800905 count = 0;
906 retval = -EFAULT;
907 }
908 } while (cookie);
909
910 /* We always clear tty buffer in case they contained passwords */
911 memzero_explicit(kernel_buf, sizeof(kernel_buf));
912 return offset ? offset : retval;
913}
914
915
Alan Coxaf9b8972006-08-27 01:24:01 -0700916/**
917 * tty_read - read method for tty device files
918 * @file: pointer to tty file
919 * @buf: user buffer
920 * @count: size of user buffer
921 * @ppos: unused
922 *
923 * Perform the read system call function on this terminal device. Checks
924 * for hung up devices before calling the line discipline method.
925 *
926 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -0700927 * Locks the line discipline internally while needed. Multiple
928 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -0700929 */
930
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800931static ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 int i;
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800934 struct file *file = iocb->ki_filp;
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200935 struct inode *inode = file_inode(file);
Nick Piggind996b622010-08-18 04:37:36 +1000936 struct tty_struct *tty = file_tty(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct tty_ldisc *ld;
938
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200939 if (tty_paranoia_check(tty, inode, "tty_read"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EIO;
Peter Hurley18900ca2016-04-09 17:06:48 -0700941 if (!tty || tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return -EIO;
943
944 /* We want to wait for the line discipline to sort out in this
945 situation */
946 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -0800947 if (!ld)
Linus Torvaldsddc5fda2021-01-21 10:08:15 -0800948 return hung_up_tty_read(iocb, to);
Linus Torvalds3b830a92021-01-18 13:31:30 -0800949 i = -EIO;
Alan Coxa352def2008-07-16 21:53:12 +0100950 if (ld->ops->read)
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -0800951 i = iterate_tty_read(ld, tty, file, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 tty_ldisc_deref(ld);
Jiri Slabyb0de59b2013-02-15 15:25:05 +0100953
Arnd Bergmannc884f872018-06-18 16:16:26 +0200954 if (i > 0)
955 tty_update_time(&inode->i_atime);
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return i;
958}
959
Peter Hurley136d5252014-09-10 15:06:34 -0400960static void tty_write_unlock(struct tty_struct *tty)
Alan Cox9c1729d2007-07-15 23:39:43 -0700961{
962 mutex_unlock(&tty->atomic_write_lock);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800963 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
Alan Cox9c1729d2007-07-15 23:39:43 -0700964}
965
Peter Hurley136d5252014-09-10 15:06:34 -0400966static int tty_write_lock(struct tty_struct *tty, int ndelay)
Alan Cox9c1729d2007-07-15 23:39:43 -0700967{
968 if (!mutex_trylock(&tty->atomic_write_lock)) {
969 if (ndelay)
970 return -EAGAIN;
971 if (mutex_lock_interruptible(&tty->atomic_write_lock))
972 return -ERESTARTSYS;
973 }
974 return 0;
975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/*
978 * Split writes up in sane blocksizes to avoid
979 * denial-of-service type attacks
980 */
981static inline ssize_t do_tty_write(
982 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
983 struct tty_struct *tty,
984 struct file *file,
Linus Torvalds9bb48c82021-01-19 11:41:16 -0800985 struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Linus Torvalds9bb48c82021-01-19 11:41:16 -0800987 size_t count = iov_iter_count(from);
Alan Cox9c1729d2007-07-15 23:39:43 -0700988 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -0800990
Alan Cox9c1729d2007-07-15 23:39:43 -0700991 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
992 if (ret < 0)
993 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /*
996 * We chunk up writes into a temporary buffer. This
997 * simplifies low-level drivers immensely, since they
998 * don't have locking issues and user mode accesses.
999 *
1000 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1001 * big chunk-size..
1002 *
1003 * The default chunk-size is 2kB, because the NTTY
1004 * layer has problems with bigger chunks. It will
1005 * claim to be able to handle more characters than
1006 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -07001007 *
1008 * FIXME: This can probably go away now except that 64K chunks
1009 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
1011 chunk = 2048;
1012 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1013 chunk = 65536;
1014 if (count < chunk)
1015 chunk = count;
1016
Ingo Molnar70522e12006-03-23 03:00:31 -08001017 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (tty->write_cnt < chunk) {
Jason Wessel402fda92008-10-13 10:45:36 +01001019 unsigned char *buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (chunk < 1024)
1022 chunk = 1024;
1023
Jason Wessel402fda92008-10-13 10:45:36 +01001024 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1025 if (!buf_chunk) {
Alan Cox9c1729d2007-07-15 23:39:43 -07001026 ret = -ENOMEM;
1027 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 kfree(tty->write_buf);
1030 tty->write_cnt = chunk;
Jason Wessel402fda92008-10-13 10:45:36 +01001031 tty->write_buf = buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
1034 /* Do the write .. */
1035 for (;;) {
1036 size_t size = count;
Xiaofei Tane73b2402021-05-12 17:26:15 +08001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (size > chunk)
1039 size = chunk;
Linus Torvalds9bb48c82021-01-19 11:41:16 -08001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 ret = -EFAULT;
Linus Torvalds9bb48c82021-01-19 11:41:16 -08001042 if (copy_from_iter(tty->write_buf, size, from) != size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
Linus Torvalds9bb48c82021-01-19 11:41:16 -08001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (ret <= 0)
1047 break;
Linus Torvalds9bb48c82021-01-19 11:41:16 -08001048
Linus Torvalds3342ff22021-02-20 21:15:00 -08001049 written += ret;
1050 if (ret > size)
1051 break;
1052
Linus Torvalds9bb48c82021-01-19 11:41:16 -08001053 /* FIXME! Have Al check this! */
1054 if (ret != size)
1055 iov_iter_revert(from, size-ret);
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 count -= ret;
1058 if (!count)
1059 break;
1060 ret = -ERESTARTSYS;
1061 if (signal_pending(current))
1062 break;
1063 cond_resched();
1064 }
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001065 if (written) {
Arnd Bergmannc884f872018-06-18 16:16:26 +02001066 tty_update_time(&file_inode(file)->i_mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 ret = written;
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001068 }
Alan Cox9c1729d2007-07-15 23:39:43 -07001069out:
1070 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return ret;
1072}
1073
Alan Cox95f9bfc2008-10-13 10:39:23 +01001074/**
1075 * tty_write_message - write a message to a certain tty, not just the console.
1076 * @tty: the destination tty_struct
1077 * @msg: the message to write
1078 *
1079 * This is used for messages that need to be redirected to a specific tty.
1080 * We don't put it into the syslog queue right now maybe in the future if
1081 * really needed.
1082 *
Arnd Bergmannec79d602010-06-01 22:53:01 +02001083 * We must still hold the BTM and test the CLOSING flag for the moment.
Alan Cox95f9bfc2008-10-13 10:39:23 +01001084 */
1085
1086void tty_write_message(struct tty_struct *tty, char *msg)
1087{
Alan Cox95f9bfc2008-10-13 10:39:23 +01001088 if (tty) {
1089 mutex_lock(&tty->atomic_write_lock);
Alan Cox89c8d912012-08-08 16:30:13 +01001090 tty_lock(tty);
Peter Hurley4b41b952015-10-10 20:28:44 -04001091 if (tty->ops->write && tty->count > 0)
Alan Cox95f9bfc2008-10-13 10:39:23 +01001092 tty->ops->write(tty, msg, strlen(msg));
Peter Hurley4b41b952015-10-10 20:28:44 -04001093 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +01001094 tty_write_unlock(tty);
1095 }
Alan Cox95f9bfc2008-10-13 10:39:23 +01001096 return;
1097}
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Alan Coxaf9b8972006-08-27 01:24:01 -07001100/**
1101 * tty_write - write method for tty device file
1102 * @file: tty file pointer
1103 * @buf: user data to write
1104 * @count: bytes to write
1105 * @ppos: unused
1106 *
1107 * Write data to a tty device via the line discipline.
1108 *
1109 * Locking:
1110 * Locks the line discipline as required
1111 * Writes to the tty driver are serialized by the atomic_write_lock
1112 * and are then processed in chunks to the device. The line discipline
Joe Petersona88a69c2009-01-02 13:40:53 +00001113 * write method will not be invoked in parallel for each device.
Alan Coxaf9b8972006-08-27 01:24:01 -07001114 */
1115
Linus Torvaldsa9cbbb82021-01-29 12:28:20 -08001116static ssize_t file_tty_write(struct file *file, struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Nick Piggind996b622010-08-18 04:37:36 +10001118 struct tty_struct *tty = file_tty(file);
Xiaofei Tan395e7832021-05-12 17:26:14 +08001119 struct tty_ldisc *ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 ssize_t ret;
Alan Cox37bdfb02008-02-08 04:18:47 -08001121
Al Viro6131ffa2013-02-27 16:59:05 -05001122 if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return -EIO;
Peter Hurley18900ca2016-04-09 17:06:48 -07001124 if (!tty || !tty->ops->write || tty_io_error(tty))
Alan Cox37bdfb02008-02-08 04:18:47 -08001125 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001126 /* Short term debug to catch buggy drivers */
1127 if (tty->ops->write_room == NULL)
Peter Hurley339f36b2015-11-08 13:01:13 -05001128 tty_err(tty, "missing write_room method\n");
Alan Cox37bdfb02008-02-08 04:18:47 -08001129 ld = tty_ldisc_ref_wait(tty);
Linus Torvalds17749852021-01-21 10:04:27 -08001130 if (!ld)
1131 return hung_up_tty_write(iocb, from);
1132 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 ret = -EIO;
1134 else
Linus Torvalds9bb48c82021-01-19 11:41:16 -08001135 ret = do_tty_write(ld->ops->write, tty, file, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 tty_ldisc_deref(ld);
1137 return ret;
1138}
1139
Linus Torvaldsa9cbbb82021-01-29 12:28:20 -08001140static ssize_t tty_write(struct kiocb *iocb, struct iov_iter *from)
1141{
1142 return file_tty_write(iocb->ki_filp, iocb, from);
1143}
1144
Linus Torvalds9bb48c82021-01-19 11:41:16 -08001145ssize_t redirected_tty_write(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 struct file *p = NULL;
1148
1149 spin_lock(&redirect_lock);
Al Virocb0942b2012-08-27 14:48:26 -04001150 if (redirect)
1151 p = get_file(redirect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 spin_unlock(&redirect_lock);
1153
Linus Torvaldsa9cbbb82021-01-29 12:28:20 -08001154 /*
1155 * We know the redirected tty is just another tty, we can can
1156 * call file_tty_write() directly with that file pointer.
1157 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (p) {
1159 ssize_t res;
Xiaofei Tane73b2402021-05-12 17:26:15 +08001160
Linus Torvaldsa9cbbb82021-01-29 12:28:20 -08001161 res = file_tty_write(p, iocb, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 fput(p);
1163 return res;
1164 }
Linus Torvalds9bb48c82021-01-19 11:41:16 -08001165 return tty_write(iocb, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Lee Jones08aa5042020-11-04 19:35:25 +00001168/*
Peter Hurley136d5252014-09-10 15:06:34 -04001169 * tty_send_xchar - send priority character
1170 *
1171 * Send a high priority character to the tty even if stopped
1172 *
1173 * Locking: none for xchar method, write ordering for write method.
1174 */
1175
1176int tty_send_xchar(struct tty_struct *tty, char ch)
1177{
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001178 bool was_stopped = tty->flow.stopped;
Peter Hurley136d5252014-09-10 15:06:34 -04001179
1180 if (tty->ops->send_xchar) {
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001181 down_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001182 tty->ops->send_xchar(tty, ch);
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001183 up_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001184 return 0;
1185 }
1186
1187 if (tty_write_lock(tty, 0) < 0)
1188 return -ERESTARTSYS;
1189
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001190 down_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001191 if (was_stopped)
1192 start_tty(tty);
1193 tty->ops->write(tty, &ch, 1);
1194 if (was_stopped)
1195 stop_tty(tty);
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001196 up_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001197 tty_write_unlock(tty);
1198 return 0;
1199}
1200
Alan Coxaf9b8972006-08-27 01:24:01 -07001201/**
1202 * pty_line_name - generate name for a pty
1203 * @driver: the tty driver in use
1204 * @index: the minor number
1205 * @p: output buffer of at least 6 bytes
1206 *
1207 * Generate a name from a driver reference and write it to the output
1208 * buffer.
1209 *
1210 * Locking: None
1211 */
1212static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Jiri Slabya846dcf2021-03-02 07:22:06 +01001214 static const char ptychar[] = "pqrstuvwxyzabcde";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int i = index + driver->name_base;
1216 /* ->name is initialized to "ttyp", but "tty" is expected */
1217 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08001218 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1219 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Alan Coxaf9b8972006-08-27 01:24:01 -07001222/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001223 * tty_line_name - generate name for a tty
Alan Coxaf9b8972006-08-27 01:24:01 -07001224 * @driver: the tty driver in use
1225 * @index: the minor number
1226 * @p: output buffer of at least 7 bytes
1227 *
1228 * Generate a name from a driver reference and write it to the output
Greg Kroah-Hartman5c0a2452014-02-22 14:31:04 -08001229 * buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07001230 *
1231 * Locking: None
1232 */
Hannes Reinecke723abd82014-02-27 12:30:51 +01001233static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Jiri Slaby0019b402012-08-08 22:26:43 +02001235 if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
Hannes Reinecke723abd82014-02-27 12:30:51 +01001236 return sprintf(p, "%s", driver->name);
Jiri Slaby0019b402012-08-08 22:26:43 +02001237 else
Hannes Reinecke723abd82014-02-27 12:30:51 +01001238 return sprintf(p, "%s%d", driver->name,
1239 index + driver->name_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Alan Cox99f1fe12008-10-13 10:42:00 +01001242/**
1243 * tty_driver_lookup_tty() - find an existing tty, if any
1244 * @driver: the driver for the tty
Lee Jones08aa5042020-11-04 19:35:25 +00001245 * @file: file object
Alan Cox99f1fe12008-10-13 10:42:00 +01001246 * @idx: the minor number
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001247 *
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001248 * Return the tty, if found. If not found, return NULL or ERR_PTR() if the
1249 * driver lookup() method returns an error.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001250 *
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001251 * Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001252 */
Jason Wessela47d5452009-01-02 13:43:04 +00001253static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
Linus Torvalds8ead9dd2016-04-25 20:04:08 -07001254 struct file *file, int idx)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001255{
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001256 struct tty_struct *tty;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001257
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001258 if (driver->ops->lookup)
Okash Khawaja12e84c72017-05-15 18:45:32 +01001259 if (!file)
1260 tty = ERR_PTR(-EIO);
1261 else
1262 tty = driver->ops->lookup(driver, file, idx);
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001263 else
1264 tty = driver->ttys[idx];
1265
1266 if (!IS_ERR(tty))
1267 tty_kref_get(tty);
1268 return tty;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001269}
1270
Alan Cox99f1fe12008-10-13 10:42:00 +01001271/**
Alan Coxbf970ee2008-10-13 10:42:39 +01001272 * tty_init_termios - helper for termios setup
1273 * @tty: the tty to set up
1274 *
Johan Hovold8daa89e2019-04-18 18:05:54 +02001275 * Initialise the termios structure for this tty. This runs under
Alan Coxbf970ee2008-10-13 10:42:39 +01001276 * the tty_mutex currently so we can be relaxed about ordering.
1277 */
1278
Peter Hurleya3123fd2016-01-09 21:13:48 -08001279void tty_init_termios(struct tty_struct *tty)
Alan Coxbf970ee2008-10-13 10:42:39 +01001280{
Alan Coxfe6e29f2008-10-13 10:44:08 +01001281 struct ktermios *tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001282 int idx = tty->index;
1283
Alan Cox36b3c072012-07-17 17:06:57 +01001284 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1285 tty->termios = tty->driver->init_termios;
1286 else {
1287 /* Check for lazy saved data */
1288 tp = tty->driver->termios[idx];
Peter Hurleyece53402016-01-10 22:40:57 -08001289 if (tp != NULL) {
Alan Cox36b3c072012-07-17 17:06:57 +01001290 tty->termios = *tp;
Peter Hurleyece53402016-01-10 22:40:57 -08001291 tty->termios.c_line = tty->driver->init_termios.c_line;
1292 } else
Alan Cox36b3c072012-07-17 17:06:57 +01001293 tty->termios = tty->driver->init_termios;
Alan Coxbf970ee2008-10-13 10:42:39 +01001294 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001295 /* Compatibility until drivers always set this */
Alan Coxadc8d742012-07-14 15:31:47 +01001296 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1297 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001298}
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001299EXPORT_SYMBOL_GPL(tty_init_termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001300
Jiri Slaby66d450e2012-01-30 21:14:28 +01001301int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1302{
Peter Hurleya3123fd2016-01-09 21:13:48 -08001303 tty_init_termios(tty);
Jiri Slaby66d450e2012-01-30 21:14:28 +01001304 tty_driver_kref_get(driver);
1305 tty->count++;
1306 driver->ttys[tty->index] = tty;
1307 return 0;
1308}
1309EXPORT_SYMBOL_GPL(tty_standard_install);
1310
Alan Coxbf970ee2008-10-13 10:42:39 +01001311/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001312 * tty_driver_install_tty() - install a tty entry in the driver
1313 * @driver: the driver for the tty
1314 * @tty: the tty
1315 *
1316 * Install a tty object into the driver tables. The tty->index field
Alan Coxbf970ee2008-10-13 10:42:39 +01001317 * will be set by the time this is called. This method is responsible
1318 * for ensuring any need additional structures are allocated and
1319 * configured.
Alan Cox8b0a88d2008-10-13 10:42:19 +01001320 *
1321 * Locking: tty_mutex for now
1322 */
1323static int tty_driver_install_tty(struct tty_driver *driver,
1324 struct tty_struct *tty)
1325{
Jiri Slaby66d450e2012-01-30 21:14:28 +01001326 return driver->ops->install ? driver->ops->install(driver, tty) :
1327 tty_standard_install(driver, tty);
Alan Cox8b0a88d2008-10-13 10:42:19 +01001328}
1329
1330/**
1331 * tty_driver_remove_tty() - remove a tty from the driver tables
1332 * @driver: the driver for the tty
Jiri Slabyfa441952020-08-18 10:56:51 +02001333 * @tty: tty to remove
Alan Cox8b0a88d2008-10-13 10:42:19 +01001334 *
1335 * Remvoe a tty object from the driver tables. The tty->index field
1336 * will be set by the time this is called.
1337 *
1338 * Locking: tty_mutex for now
1339 */
Peter Hurley05de87ed2016-01-09 21:13:49 -08001340static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +01001341{
1342 if (driver->ops->remove)
1343 driver->ops->remove(driver, tty);
1344 else
1345 driver->ttys[tty->index] = NULL;
1346}
1347
Jiri Slabyfa441952020-08-18 10:56:51 +02001348/**
1349 * tty_reopen() - fast re-open of an open tty
1350 * @tty: the tty to open
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001351 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001352 * Return 0 on success, -errno on error.
Peter Hurley5d93e742014-11-05 12:12:47 -05001353 * Re-opens on master ptys are not allowed and return -EIO.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001354 *
Peter Hurley5d93e742014-11-05 12:12:47 -05001355 * Locking: Caller must hold tty_lock
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001356 */
Alan Cox99f1fe12008-10-13 10:42:00 +01001357static int tty_reopen(struct tty_struct *tty)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001358{
1359 struct tty_driver *driver = tty->driver;
Dmitry Safonovd3736d82019-01-09 01:17:40 +00001360 struct tty_ldisc *ld;
1361 int retval = 0;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001362
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001363 if (driver->type == TTY_DRIVER_TYPE_PTY &&
Peter Hurley5d93e742014-11-05 12:12:47 -05001364 driver->subtype == PTY_TYPE_MASTER)
1365 return -EIO;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001366
Peter Hurley7f22f6c2016-01-09 21:13:45 -08001367 if (!tty->count)
1368 return -EAGAIN;
1369
Peter Hurley86f2c002014-12-30 10:39:25 -05001370 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1371 return -EBUSY;
1372
Dmitry Safonovd3736d82019-01-09 01:17:40 +00001373 ld = tty_ldisc_ref_wait(tty);
1374 if (ld) {
1375 tty_ldisc_deref(ld);
1376 } else {
1377 retval = tty_ldisc_lock(tty, 5 * HZ);
1378 if (retval)
1379 return retval;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001380
Dmitry Safonovd3736d82019-01-09 01:17:40 +00001381 if (!tty->ldisc)
1382 retval = tty_ldisc_reinit(tty, tty->termios.c_line);
1383 tty_ldisc_unlock(tty);
1384 }
Dmitry Safonovcf62a1a2018-11-01 00:24:49 +00001385
1386 if (retval == 0)
1387 tty->count++;
1388
Dmitry Safonovfe324162018-09-18 00:52:52 +01001389 return retval;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001390}
1391
Alan Coxaf9b8972006-08-27 01:24:01 -07001392/**
Alan Coxd81ed102008-10-13 10:41:42 +01001393 * tty_init_dev - initialise a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001394 * @driver: tty driver we are opening a device on
1395 * @idx: device index
Alan Coxaf9b8972006-08-27 01:24:01 -07001396 *
1397 * Prepare a tty device. This may not be a "new" clean device but
1398 * could also be an active device. The pty drivers require special
1399 * handling because of this.
1400 *
1401 * Locking:
1402 * The function is called under the tty_mutex, which
1403 * protects us from the tty struct or driver itself going away.
1404 *
1405 * On exit the tty device has the line discipline attached and
1406 * a reference count of 1. If a pair was created for pty/tty use
1407 * and the other was a pty master then it too has a reference count of 1.
1408 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001410 * failed open. The new code protects the open with a mutex, so it's
1411 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 * relaxed for the (most common) case of reopening a tty.
Jiri Slabyfa441952020-08-18 10:56:51 +02001413 *
1414 * Return: returned tty structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001416
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001417struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Alan Coxbf970ee2008-10-13 10:42:39 +01001419 struct tty_struct *tty;
Alan Cox73ec06f2008-10-13 10:42:29 +01001420 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /*
1423 * First time open is complex, especially for PTY devices.
1424 * This code guarantees that either everything succeeds and the
1425 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001426 * and the allocated memory released. (Except that the termios
Johan Hovold16b00ae2017-03-30 15:39:35 +02001427 * may be retained.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
1429
Alan Cox73ec06f2008-10-13 10:42:29 +01001430 if (!try_module_get(driver->owner))
1431 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02001433 tty = alloc_tty_struct(driver, idx);
Jiri Slabyd5543502011-03-23 10:48:32 +01001434 if (!tty) {
1435 retval = -ENOMEM;
1436 goto err_module_put;
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Alan Cox89c8d912012-08-08 16:30:13 +01001439 tty_lock(tty);
Alan Cox73ec06f2008-10-13 10:42:29 +01001440 retval = tty_driver_install_tty(driver, tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001441 if (retval < 0)
Peter Hurleyc8b710b2016-01-09 21:13:46 -08001442 goto err_free_tty;
Alan Cox8b0a88d2008-10-13 10:42:19 +01001443
Jiri Slaby04831dc2012-06-04 13:35:36 +02001444 if (!tty->port)
1445 tty->port = driver->ports[idx];
1446
Jiri Slaby2ae0b312019-11-22 11:17:21 +01001447 if (WARN_RATELIMIT(!tty->port,
1448 "%s: %s driver does not set tty->port. This would crash the kernel. Fix the driver!\n",
1449 __func__, tty->driver->name)) {
1450 retval = -EINVAL;
1451 goto err_release_lock;
1452 }
Jiri Slaby5d4121c2012-08-17 14:27:52 +02001453
Gaurav Kohlib027e222018-01-23 13:16:34 +05301454 retval = tty_ldisc_lock(tty, 5 * HZ);
1455 if (retval)
1456 goto err_release_lock;
Jiri Slaby967fab62012-10-18 22:26:46 +02001457 tty->port->itty = tty;
1458
Alan Cox37bdfb02008-02-08 04:18:47 -08001459 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001461 * If we fail here just call release_tty to clean up. No need
1462 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 */
Alan Coxbf970ee2008-10-13 10:42:39 +01001464 retval = tty_ldisc_setup(tty, tty->link);
Alan Cox01e1abb2008-07-22 11:16:55 +01001465 if (retval)
Jiri Slabyd5543502011-03-23 10:48:32 +01001466 goto err_release_tty;
Gaurav Kohlib027e222018-01-23 13:16:34 +05301467 tty_ldisc_unlock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +01001468 /* Return the tty locked so that it cannot vanish under the caller */
Alan Cox73ec06f2008-10-13 10:42:29 +01001469 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Peter Hurleyc8b710b2016-01-09 21:13:46 -08001471err_free_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001472 tty_unlock(tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001473 free_tty_struct(tty);
1474err_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 module_put(driver->owner);
Jiri Slabyd5543502011-03-23 10:48:32 +01001476 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001478 /* call the tty release_tty routine to clean out this slot */
Jiri Slabyd5543502011-03-23 10:48:32 +01001479err_release_tty:
Gaurav Kohlib027e222018-01-23 13:16:34 +05301480 tty_ldisc_unlock(tty);
Peter Hurley339f36b2015-11-08 13:01:13 -05001481 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n",
1482 retval, idx);
Gaurav Kohlib027e222018-01-23 13:16:34 +05301483err_release_lock:
1484 tty_unlock(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001485 release_tty(tty, idx);
Alan Cox73ec06f2008-10-13 10:42:29 +01001486 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Johan Hovoldf51ccf42018-12-04 17:00:36 +01001489/**
1490 * tty_save_termios() - save tty termios data in driver table
1491 * @tty: tty whose termios data to save
1492 *
1493 * Locking: Caller guarantees serialisation with tty_init_termios().
1494 */
1495void tty_save_termios(struct tty_struct *tty)
Alan Coxfeebed62008-10-13 10:41:30 +01001496{
1497 struct ktermios *tp;
1498 int idx = tty->index;
Alan Cox36b3c072012-07-17 17:06:57 +01001499
1500 /* If the port is going to reset then it has no termios to save */
1501 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1502 return;
1503
1504 /* Stash the termios data */
1505 tp = tty->driver->termios[idx];
1506 if (tp == NULL) {
Gustavo A. R. Silvaa3241892020-07-23 17:34:22 -05001507 tp = kmalloc(sizeof(*tp), GFP_KERNEL);
Peter Hurley25080652015-11-08 13:01:11 -05001508 if (tp == NULL)
Alan Cox36b3c072012-07-17 17:06:57 +01001509 return;
Dan Carpenter4ac5d702012-07-24 12:51:52 +01001510 tty->driver->termios[idx] = tp;
Alan Coxfeebed62008-10-13 10:41:30 +01001511 }
Alan Cox36b3c072012-07-17 17:06:57 +01001512 *tp = tty->termios;
Alan Coxfeebed62008-10-13 10:41:30 +01001513}
Johan Hovoldf51ccf42018-12-04 17:00:36 +01001514EXPORT_SYMBOL_GPL(tty_save_termios);
Alan Coxfeebed62008-10-13 10:41:30 +01001515
Peter Hurleya2965b72013-03-11 16:44:35 -04001516/**
Peter Hurley949aa642014-11-05 12:12:57 -05001517 * tty_flush_works - flush all works of a tty/pty pair
1518 * @tty: tty device to flush works for (or either end of a pty pair)
Peter Hurleya2965b72013-03-11 16:44:35 -04001519 *
Peter Hurley949aa642014-11-05 12:12:57 -05001520 * Sync flush all works belonging to @tty (and the 'other' tty).
Peter Hurleya2965b72013-03-11 16:44:35 -04001521 */
1522static void tty_flush_works(struct tty_struct *tty)
1523{
1524 flush_work(&tty->SAK_work);
1525 flush_work(&tty->hangup_work);
Peter Hurley949aa642014-11-05 12:12:57 -05001526 if (tty->link) {
1527 flush_work(&tty->link->SAK_work);
1528 flush_work(&tty->link->hangup_work);
1529 }
Peter Hurleya2965b72013-03-11 16:44:35 -04001530}
Alan Coxfeebed62008-10-13 10:41:30 +01001531
Alan Coxaf9b8972006-08-27 01:24:01 -07001532/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001533 * release_one_tty - release tty structure memory
Jiri Slabyfa441952020-08-18 10:56:51 +02001534 * @work: work of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001535 *
1536 * Releases memory associated with a tty structure, and clears out the
1537 * driver table slots. This function is called when a device is no longer
1538 * in use. It also gets called when setup of a device fails.
1539 *
1540 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -07001541 * takes the file list lock internally when working on the list
1542 * of ttys that the driver keeps.
Alan Coxb50989d2009-09-19 13:13:22 -07001543 *
1544 * This method gets called from a work queue so that the driver private
Dave Youngf278a2f2009-09-27 16:00:42 +00001545 * cleanup ops can sleep (needed for USB at least)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 */
Alan Coxb50989d2009-09-19 13:13:22 -07001547static void release_one_tty(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Alan Coxb50989d2009-09-19 13:13:22 -07001549 struct tty_struct *tty =
1550 container_of(work, struct tty_struct, hangup_work);
Alan Cox6f967f72008-10-13 10:37:36 +01001551 struct tty_driver *driver = tty->driver;
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001552 struct module *owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Dave Youngf278a2f2009-09-27 16:00:42 +00001554 if (tty->ops->cleanup)
1555 tty->ops->cleanup(tty);
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 tty->magic = 0;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001558 tty_driver_kref_put(driver);
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001559 module_put(owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001560
Peter Hurley4a510962016-01-09 21:35:23 -08001561 spin_lock(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 list_del_init(&tty->tty_files);
Peter Hurley4a510962016-01-09 21:35:23 -08001563 spin_unlock(&tty->files_lock);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001564
Jiri Slaby64d608d2021-05-05 11:19:06 +02001565 put_pid(tty->ctrl.pgrp);
1566 put_pid(tty->ctrl.session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 free_tty_struct(tty);
1568}
1569
Alan Coxb50989d2009-09-19 13:13:22 -07001570static void queue_release_one_tty(struct kref *kref)
1571{
1572 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Dave Youngf278a2f2009-09-27 16:00:42 +00001573
Alan Coxb50989d2009-09-19 13:13:22 -07001574 /* The hangup queue is now free so we can reuse it rather than
1575 waste a chunk of memory for each port */
1576 INIT_WORK(&tty->hangup_work, release_one_tty);
1577 schedule_work(&tty->hangup_work);
1578}
1579
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001580/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001581 * tty_kref_put - release a tty kref
1582 * @tty: tty device
1583 *
1584 * Release a reference to a tty device and if need be let the kref
1585 * layer destruct the object for us
1586 */
1587
1588void tty_kref_put(struct tty_struct *tty)
1589{
1590 if (tty)
Alan Coxb50989d2009-09-19 13:13:22 -07001591 kref_put(&tty->kref, queue_release_one_tty);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001592}
1593EXPORT_SYMBOL(tty_kref_put);
1594
1595/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001596 * release_tty - release tty structure memory
Lee Jones08aa5042020-11-04 19:35:25 +00001597 * @tty: tty device release
1598 * @idx: index of the tty device release
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001599 *
1600 * Release both @tty and a possible linked partner (think pty pair),
1601 * and decrement the refcount of the backing module.
1602 *
1603 * Locking:
Alan Coxd1552552012-07-27 18:02:54 +01001604 * tty_mutex
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001605 * takes the file list lock internally when working on the list
1606 * of ttys that the driver keeps.
Alan Cox9c9f4de2008-10-13 10:37:26 +01001607 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001608 */
1609static void release_tty(struct tty_struct *tty, int idx)
1610{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001611 /* This should always be true but check for the moment */
1612 WARN_ON(tty->index != idx);
Alan Coxd1552552012-07-27 18:02:54 +01001613 WARN_ON(!mutex_is_locked(&tty_mutex));
Alan Cox36b3c072012-07-17 17:06:57 +01001614 if (tty->ops->shutdown)
1615 tty->ops->shutdown(tty);
Johan Hovoldf51ccf42018-12-04 17:00:36 +01001616 tty_save_termios(tty);
Alan Cox36b3c072012-07-17 17:06:57 +01001617 tty_driver_remove_tty(tty->driver, tty);
Matthias Reichl4466d6d2020-11-05 13:34:32 +01001618 if (tty->port)
1619 tty->port->itty = NULL;
Peter Hurley64e377d2013-06-15 09:01:00 -04001620 if (tty->link)
1621 tty->link->port->itty = NULL;
Matthias Reichl4466d6d2020-11-05 13:34:32 +01001622 if (tty->port)
1623 tty_buffer_cancel_work(tty->port);
Sahara2b022ab2017-12-13 09:10:48 +04001624 if (tty->link)
1625 tty_buffer_cancel_work(tty->link->port);
Alan Cox36b3c072012-07-17 17:06:57 +01001626
Markus Elfringa211b1a2014-11-21 13:42:29 +01001627 tty_kref_put(tty->link);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001628 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001629}
1630
Alan Coxeeb89d92009-11-30 13:18:29 +00001631/**
Jiri Slaby955787ca2011-11-11 10:47:23 +01001632 * tty_release_checks - check a tty before real release
1633 * @tty: tty to check
Jiri Slaby955787ca2011-11-11 10:47:23 +01001634 * @idx: index of the tty
1635 *
1636 * Performs some paranoid checking before true release of the @tty.
1637 * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1638 */
Peter Hurley359b9fb2014-11-05 12:12:59 -05001639static int tty_release_checks(struct tty_struct *tty, int idx)
Jiri Slaby955787ca2011-11-11 10:47:23 +01001640{
1641#ifdef TTY_PARANOIA_CHECK
1642 if (idx < 0 || idx >= tty->driver->num) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001643 tty_debug(tty, "bad idx %d\n", idx);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001644 return -1;
1645 }
1646
1647 /* not much to check for devpts */
1648 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1649 return 0;
1650
1651 if (tty != tty->driver->ttys[idx]) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001652 tty_debug(tty, "bad driver table[%d] = %p\n",
1653 idx, tty->driver->ttys[idx]);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001654 return -1;
1655 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001656 if (tty->driver->other) {
Peter Hurley359b9fb2014-11-05 12:12:59 -05001657 struct tty_struct *o_tty = tty->link;
1658
Jiri Slaby955787ca2011-11-11 10:47:23 +01001659 if (o_tty != tty->driver->other->ttys[idx]) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001660 tty_debug(tty, "bad other table[%d] = %p\n",
1661 idx, tty->driver->other->ttys[idx]);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001662 return -1;
1663 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001664 if (o_tty->link != tty) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001665 tty_debug(tty, "bad link = %p\n", o_tty->link);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001666 return -1;
1667 }
1668 }
1669#endif
1670 return 0;
1671}
1672
1673/**
Okash Khawajaa09ac392017-07-20 08:22:36 +01001674 * tty_kclose - closes tty opened by tty_kopen
1675 * @tty: tty device
1676 *
1677 * Performs the final steps to release and free a tty device. It is the
1678 * same as tty_release_struct except that it also resets TTY_PORT_KOPENED
1679 * flag on tty->port.
1680 */
1681void tty_kclose(struct tty_struct *tty)
1682{
1683 /*
1684 * Ask the line discipline code to release its structures
1685 */
1686 tty_ldisc_release(tty);
1687
1688 /* Wait for pending work before tty destruction commmences */
1689 tty_flush_works(tty);
1690
1691 tty_debug_hangup(tty, "freeing structure\n");
1692 /*
1693 * The release_tty function takes care of the details of clearing
Eric Biggersed069822020-02-23 23:33:59 -08001694 * the slots and preserving the termios structure.
Okash Khawajaa09ac392017-07-20 08:22:36 +01001695 */
1696 mutex_lock(&tty_mutex);
1697 tty_port_set_kopened(tty->port, 0);
1698 release_tty(tty, tty->index);
1699 mutex_unlock(&tty_mutex);
1700}
1701EXPORT_SYMBOL_GPL(tty_kclose);
1702
1703/**
Rob Herring9ed90d22017-01-16 16:54:28 -06001704 * tty_release_struct - release a tty struct
1705 * @tty: tty device
1706 * @idx: index of the tty
1707 *
1708 * Performs the final steps to release and free a tty device. It is
1709 * roughly the reverse of tty_init_dev.
1710 */
1711void tty_release_struct(struct tty_struct *tty, int idx)
1712{
1713 /*
1714 * Ask the line discipline code to release its structures
1715 */
1716 tty_ldisc_release(tty);
1717
1718 /* Wait for pending work before tty destruction commmences */
1719 tty_flush_works(tty);
1720
1721 tty_debug_hangup(tty, "freeing structure\n");
1722 /*
1723 * The release_tty function takes care of the details of clearing
Eric Biggersed069822020-02-23 23:33:59 -08001724 * the slots and preserving the termios structure.
Rob Herring9ed90d22017-01-16 16:54:28 -06001725 */
1726 mutex_lock(&tty_mutex);
1727 release_tty(tty, idx);
1728 mutex_unlock(&tty_mutex);
1729}
1730EXPORT_SYMBOL_GPL(tty_release_struct);
1731
1732/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001733 * tty_release - vfs callback for close
1734 * @inode: inode of tty
1735 * @filp: file pointer for handle to tty
1736 *
1737 * Called the last time each file handle is closed that references
1738 * this tty. There may however be several such references.
1739 *
1740 * Locking:
1741 * Takes bkl. See tty_release_dev
1742 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 * Even releasing the tty structures is a tricky business.. We have
1744 * to be very careful that the structures are all released at the
1745 * same time, as interrupts might otherwise get the wrong pointers.
1746 *
1747 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1748 * lead to double frees or releasing memory still in use.
1749 */
Alan Coxeeb89d92009-11-30 13:18:29 +00001750
1751int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Nick Piggind996b622010-08-18 04:37:36 +10001753 struct tty_struct *tty = file_tty(filp);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001754 struct tty_struct *o_tty = NULL;
1755 int do_sleep, final;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int idx;
Peter Hurley37b16452014-10-16 13:51:30 -04001757 long timeout = 0;
Peter Hurley494c1ea2014-10-16 13:54:36 -04001758 int once = 1;
Alan Cox37bdfb02008-02-08 04:18:47 -08001759
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001760 if (tty_paranoia_check(tty, inode, __func__))
Alan Coxeeb89d92009-11-30 13:18:29 +00001761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Alan Cox89c8d912012-08-08 16:30:13 +01001763 tty_lock(tty);
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001764 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Arnd Bergmannec79d602010-06-01 22:53:01 +02001766 __tty_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 idx = tty->index;
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001769 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1770 tty->driver->subtype == PTY_TYPE_MASTER)
1771 o_tty = tty->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Peter Hurley359b9fb2014-11-05 12:12:59 -05001773 if (tty_release_checks(tty, idx)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001774 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Peter Hurleyd435cef2015-11-08 13:01:19 -05001778 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Alan Coxf34d7a52008-04-30 00:54:13 -07001780 if (tty->ops->close)
1781 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Peter Hurley2aff5e22014-11-05 12:13:01 -05001783 /* If tty is pty master, lock the slave pty (stable lock order) */
1784 tty_lock_slave(o_tty);
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 /*
1787 * Sanity check: if tty->count is going to zero, there shouldn't be
1788 * any waiters on tty->read_wait or tty->write_wait. We test the
1789 * wait queues and kick everyone out _before_ actually starting to
1790 * close. This ensures that we won't block while releasing the tty
1791 * structure.
1792 *
1793 * The test for the o_tty closing is necessary, since the master and
1794 * slave sides may close in any order. If the slave side closes out
1795 * first, its count will be one, since the master side holds an open.
Peter Hurley324c1652014-11-05 12:12:56 -05001796 * Thus this test wouldn't be triggered at the time the slave closed,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 * so we do it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 */
1799 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 do_sleep = 0;
1801
Peter Hurley324c1652014-11-05 12:12:56 -05001802 if (tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (waitqueue_active(&tty->read_wait)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001804 wake_up_poll(&tty->read_wait, EPOLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 do_sleep++;
1806 }
1807 if (waitqueue_active(&tty->write_wait)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001808 wake_up_poll(&tty->write_wait, EPOLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 do_sleep++;
1810 }
1811 }
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001812 if (o_tty && o_tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (waitqueue_active(&o_tty->read_wait)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001814 wake_up_poll(&o_tty->read_wait, EPOLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 do_sleep++;
1816 }
1817 if (waitqueue_active(&o_tty->write_wait)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001818 wake_up_poll(&o_tty->write_wait, EPOLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 do_sleep++;
1820 }
1821 }
1822 if (!do_sleep)
1823 break;
1824
Peter Hurley494c1ea2014-10-16 13:54:36 -04001825 if (once) {
1826 once = 0;
Peter Hurley339f36b2015-11-08 13:01:13 -05001827 tty_warn(tty, "read/write wait queue active!\n");
Peter Hurley494c1ea2014-10-16 13:54:36 -04001828 }
Peter Hurley37b16452014-10-16 13:51:30 -04001829 schedule_timeout_killable(timeout);
1830 if (timeout < 120 * HZ)
1831 timeout = 2 * timeout + 1;
1832 else
1833 timeout = MAX_SCHEDULE_TIMEOUT;
Alan Cox37bdfb02008-02-08 04:18:47 -08001834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001836 if (o_tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (--o_tty->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001838 tty_warn(tty, "bad slave count (%d)\n", o_tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 o_tty->count = 0;
1840 }
1841 }
1842 if (--tty->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001843 tty_warn(tty, "bad tty->count (%d)\n", tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 tty->count = 0;
1845 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /*
1848 * We've decremented tty->count, so we need to remove this file
1849 * descriptor off the tty->tty_files list; this serves two
1850 * purposes:
1851 * - check_tty_count sees the correct number of file descriptors
1852 * associated with this tty.
1853 * - do_tty_hangup no longer sees this file descriptor as
1854 * something that needs to be handled for hangups.
1855 */
Nick Piggind996b622010-08-18 04:37:36 +10001856 tty_del_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 /*
1859 * Perform some housekeeping before deciding whether to return.
1860 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 * If _either_ side is closing, make sure there aren't any
1862 * processes that still think tty or o_tty is their controlling
1863 * tty.
1864 */
Peter Hurley324c1652014-11-05 12:12:56 -05001865 if (!tty->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 read_lock(&tasklist_lock);
Jiri Slaby64d608d2021-05-05 11:19:06 +02001867 session_clear_tty(tty->ctrl.session);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001868 if (o_tty)
Jiri Slaby64d608d2021-05-05 11:19:06 +02001869 session_clear_tty(o_tty->ctrl.session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 read_unlock(&tasklist_lock);
1871 }
1872
Peter Hurley324c1652014-11-05 12:12:56 -05001873 /* check whether both sides are closing ... */
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001874 final = !tty->count && !(o_tty && o_tty->count);
Peter Hurley324c1652014-11-05 12:12:56 -05001875
Peter Hurley2aff5e22014-11-05 12:13:01 -05001876 tty_unlock_slave(o_tty);
1877 tty_unlock(tty);
1878
Peter Hurley04980702014-11-05 12:12:52 -05001879 /* At this point, the tty->count == 0 should ensure a dead tty
Alan Coxd1552552012-07-27 18:02:54 +01001880 cannot be re-opened by a racing opener */
Paul Fulghumda965822006-02-14 13:53:00 -08001881
Peter Hurley324c1652014-11-05 12:12:56 -05001882 if (!final)
Alan Coxeeb89d92009-11-30 13:18:29 +00001883 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001884
Peter Hurleyaccff792015-07-12 22:49:09 -04001885 tty_debug_hangup(tty, "final close\n");
Peter Hurleya2965b72013-03-11 16:44:35 -04001886
Rob Herring9ed90d22017-01-16 16:54:28 -06001887 tty_release_struct(tty, idx);
Alan Coxeeb89d92009-11-30 13:18:29 +00001888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Alan Coxaf9b8972006-08-27 01:24:01 -07001891/**
Peter Hurley52494ee2014-11-05 12:12:50 -05001892 * tty_open_current_tty - get locked tty of current task
Jiri Slabyb82154a2011-11-09 21:33:19 +01001893 * @device: device number
1894 * @filp: file pointer to tty
Peter Hurley52494ee2014-11-05 12:12:50 -05001895 * @return: locked tty of the current task iff @device is /dev/tty
1896 *
1897 * Performs a re-open of the current task's controlling tty.
Jiri Slabyb82154a2011-11-09 21:33:19 +01001898 *
1899 * We cannot return driver and index like for the other nodes because
1900 * devpts will not work then. It expects inodes to be from devpts FS.
1901 */
1902static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1903{
1904 struct tty_struct *tty;
Peter Hurley52494ee2014-11-05 12:12:50 -05001905 int retval;
Jiri Slabyb82154a2011-11-09 21:33:19 +01001906
1907 if (device != MKDEV(TTYAUX_MAJOR, 0))
1908 return NULL;
1909
1910 tty = get_current_tty();
1911 if (!tty)
1912 return ERR_PTR(-ENXIO);
1913
1914 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1915 /* noctty = 1; */
Peter Hurley52494ee2014-11-05 12:12:50 -05001916 tty_lock(tty);
1917 tty_kref_put(tty); /* safe to drop the kref now */
1918
1919 retval = tty_reopen(tty);
1920 if (retval < 0) {
1921 tty_unlock(tty);
1922 tty = ERR_PTR(retval);
1923 }
Jiri Slabyb82154a2011-11-09 21:33:19 +01001924 return tty;
1925}
1926
1927/**
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001928 * tty_lookup_driver - lookup a tty driver for a given device file
1929 * @device: device number
1930 * @filp: file pointer to tty
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001931 * @index: index for the device in the @return driver
1932 * @return: driver for this inode (with increased refcount)
1933 *
Xiaofei Tan395e7832021-05-12 17:26:14 +08001934 * If @return is not erroneous, the caller is responsible to decrement the
1935 * refcount by tty_driver_kref_put.
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001936 *
1937 * Locking: tty_mutex protects get_tty_driver
1938 */
1939static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
Peter Hurley11e1d4a2016-01-09 21:13:52 -08001940 int *index)
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001941{
Lin Yi8c8af412019-05-10 12:22:57 +08001942 struct tty_driver *driver = NULL;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001943
Jiri Slaby2cd00502011-11-09 21:33:22 +01001944 switch (device) {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001945#ifdef CONFIG_VT
Jiri Slaby2cd00502011-11-09 21:33:22 +01001946 case MKDEV(TTY_MAJOR, 0): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001947 extern struct tty_driver *console_driver;
Xiaofei Tane73b2402021-05-12 17:26:15 +08001948
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001949 driver = tty_driver_kref_get(console_driver);
1950 *index = fg_console;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001951 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001952 }
1953#endif
Jiri Slaby2cd00502011-11-09 21:33:22 +01001954 case MKDEV(TTYAUX_MAJOR, 1): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001955 struct tty_driver *console_driver = console_device(index);
Xiaofei Tane73b2402021-05-12 17:26:15 +08001956
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001957 if (console_driver) {
1958 driver = tty_driver_kref_get(console_driver);
Okash Khawaja12e84c72017-05-15 18:45:32 +01001959 if (driver && filp) {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001960 /* Don't let /dev/console block */
1961 filp->f_flags |= O_NONBLOCK;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001962 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001963 }
1964 }
Lin Yi8c8af412019-05-10 12:22:57 +08001965 if (driver)
1966 tty_driver_kref_put(driver);
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001967 return ERR_PTR(-ENODEV);
1968 }
Jiri Slaby2cd00502011-11-09 21:33:22 +01001969 default:
1970 driver = get_tty_driver(device, index);
1971 if (!driver)
1972 return ERR_PTR(-ENODEV);
1973 break;
1974 }
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001975 return driver;
1976}
1977
Uwe Kleine-König4ea3cd62020-12-18 11:42:44 +01001978static struct tty_struct *tty_kopen(dev_t device, int shared)
Okash Khawajaa09ac392017-07-20 08:22:36 +01001979{
1980 struct tty_struct *tty;
Uwe Kleine-Königf1d31742019-12-17 08:50:40 +01001981 struct tty_driver *driver;
Okash Khawajaa09ac392017-07-20 08:22:36 +01001982 int index = -1;
1983
1984 mutex_lock(&tty_mutex);
1985 driver = tty_lookup_driver(device, NULL, &index);
1986 if (IS_ERR(driver)) {
1987 mutex_unlock(&tty_mutex);
1988 return ERR_CAST(driver);
1989 }
1990
1991 /* check whether we're reopening an existing tty */
1992 tty = tty_driver_lookup_tty(driver, NULL, index);
Uwe Kleine-König4ea3cd62020-12-18 11:42:44 +01001993 if (IS_ERR(tty) || shared)
Okash Khawajaa09ac392017-07-20 08:22:36 +01001994 goto out;
1995
1996 if (tty) {
1997 /* drop kref from tty_driver_lookup_tty() */
1998 tty_kref_put(tty);
1999 tty = ERR_PTR(-EBUSY);
2000 } else { /* tty_init_dev returns tty with the tty_lock held */
2001 tty = tty_init_dev(driver, index);
2002 if (IS_ERR(tty))
2003 goto out;
2004 tty_port_set_kopened(tty->port, 1);
2005 }
2006out:
2007 mutex_unlock(&tty_mutex);
2008 tty_driver_kref_put(driver);
2009 return tty;
2010}
Uwe Kleine-König4ea3cd62020-12-18 11:42:44 +01002011
2012/**
2013 * tty_kopen_exclusive - open a tty device for kernel
2014 * @device: dev_t of device to open
2015 *
2016 * Opens tty exclusively for kernel. Performs the driver lookup,
2017 * makes sure it's not already opened and performs the first-time
2018 * tty initialization.
2019 *
2020 * Returns the locked initialized &tty_struct
2021 *
2022 * Claims the global tty_mutex to serialize:
2023 * - concurrent first-time tty initialization
2024 * - concurrent tty driver removal w/ lookup
2025 * - concurrent tty removal from driver table
2026 */
2027struct tty_struct *tty_kopen_exclusive(dev_t device)
2028{
2029 return tty_kopen(device, 0);
2030}
2031EXPORT_SYMBOL_GPL(tty_kopen_exclusive);
2032
2033/**
2034 * tty_kopen_shared - open a tty device for shared in-kernel use
2035 * @device: dev_t of device to open
2036 *
2037 * Opens an already existing tty for in-kernel use. Compared to
2038 * tty_kopen_exclusive() above it doesn't ensure to be the only user.
2039 *
2040 * Locking is identical to tty_kopen() above.
2041 */
2042struct tty_struct *tty_kopen_shared(dev_t device)
2043{
2044 return tty_kopen(device, 1);
2045}
2046EXPORT_SYMBOL_GPL(tty_kopen_shared);
Okash Khawajaa09ac392017-07-20 08:22:36 +01002047
2048/**
Peter Hurleyd6203d02016-01-09 21:13:53 -08002049 * tty_open_by_driver - open a tty device
2050 * @device: dev_t of device to open
Peter Hurleyd6203d02016-01-09 21:13:53 -08002051 * @filp: file pointer to tty
2052 *
2053 * Performs the driver lookup, checks for a reopen, or otherwise
2054 * performs the first-time tty initialization.
2055 *
2056 * Returns the locked initialized or re-opened &tty_struct
2057 *
2058 * Claims the global tty_mutex to serialize:
2059 * - concurrent first-time tty initialization
2060 * - concurrent tty driver removal w/ lookup
2061 * - concurrent tty removal from driver table
2062 */
Sudip Mukherjee14ce3842019-11-20 15:17:08 +00002063static struct tty_struct *tty_open_by_driver(dev_t device,
Peter Hurleyd6203d02016-01-09 21:13:53 -08002064 struct file *filp)
2065{
2066 struct tty_struct *tty;
2067 struct tty_driver *driver = NULL;
2068 int index = -1;
2069 int retval;
2070
2071 mutex_lock(&tty_mutex);
2072 driver = tty_lookup_driver(device, filp, &index);
2073 if (IS_ERR(driver)) {
2074 mutex_unlock(&tty_mutex);
2075 return ERR_CAST(driver);
2076 }
2077
2078 /* check whether we're reopening an existing tty */
Linus Torvalds8ead9dd2016-04-25 20:04:08 -07002079 tty = tty_driver_lookup_tty(driver, filp, index);
Peter Hurleyd6203d02016-01-09 21:13:53 -08002080 if (IS_ERR(tty)) {
2081 mutex_unlock(&tty_mutex);
2082 goto out;
2083 }
2084
2085 if (tty) {
Okash Khawajaa09ac392017-07-20 08:22:36 +01002086 if (tty_port_kopened(tty->port)) {
2087 tty_kref_put(tty);
2088 mutex_unlock(&tty_mutex);
2089 tty = ERR_PTR(-EBUSY);
2090 goto out;
2091 }
Peter Hurleyd6203d02016-01-09 21:13:53 -08002092 mutex_unlock(&tty_mutex);
2093 retval = tty_lock_interruptible(tty);
Peter Hurley5e00bbf2016-03-31 17:47:07 -07002094 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */
Peter Hurleyd6203d02016-01-09 21:13:53 -08002095 if (retval) {
2096 if (retval == -EINTR)
2097 retval = -ERESTARTSYS;
2098 tty = ERR_PTR(retval);
2099 goto out;
2100 }
Peter Hurleyd6203d02016-01-09 21:13:53 -08002101 retval = tty_reopen(tty);
2102 if (retval < 0) {
2103 tty_unlock(tty);
2104 tty = ERR_PTR(retval);
2105 }
2106 } else { /* Returns with the tty_lock held for now */
2107 tty = tty_init_dev(driver, index);
2108 mutex_unlock(&tty_mutex);
2109 }
2110out:
2111 tty_driver_kref_put(driver);
2112 return tty;
2113}
2114
2115/**
Alan Coxeeb89d92009-11-30 13:18:29 +00002116 * tty_open - open a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002117 * @inode: inode of device file
2118 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002120 * tty_open and tty_release keep up the tty count that contains the
2121 * number of opens done on a tty. We cannot use the inode-count, as
2122 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002124 * Open-counting is needed for pty masters, as well as for keeping
2125 * track of serial lines: DTR is dropped when the last close happens.
2126 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2127 *
2128 * The termios state of a pty is reset on first open so that
2129 * settings don't persist across reuse.
2130 *
Jiri Slaby5b5e7042011-11-09 21:33:20 +01002131 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002132 * tty->count should protect the rest.
2133 * ->siglock protects ->signal/->sighand
Alan Cox89c8d912012-08-08 16:30:13 +01002134 *
2135 * Note: the tty_unlock/lock cases without a ref are only safe due to
2136 * tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002138
Alan Coxeeb89d92009-11-30 13:18:29 +00002139static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Jiri Slabyb82154a2011-11-09 21:33:19 +01002141 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 int noctty, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 dev_t device = inode->i_rdev;
Andrew Morton846c1512009-04-02 16:56:36 -07002144 unsigned saved_flags = filp->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08002147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148retry_open:
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02002149 retval = tty_alloc_file(filp);
2150 if (retval)
2151 return -ENOMEM;
2152
Jiri Slabyb82154a2011-11-09 21:33:19 +01002153 tty = tty_open_current_tty(device, filp);
Peter Hurleyd6203d02016-01-09 21:13:53 -08002154 if (!tty)
Sudip Mukherjee14ce3842019-11-20 15:17:08 +00002155 tty = tty_open_by_driver(device, filp);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01002156
Alan Coxeeb89d92009-11-30 13:18:29 +00002157 if (IS_ERR(tty)) {
Peter Hurleyd6203d02016-01-09 21:13:53 -08002158 tty_free_file(filp);
Jiri Slabyba5db442011-11-09 21:33:21 +01002159 retval = PTR_ERR(tty);
Peter Hurley7f22f6c2016-01-09 21:13:45 -08002160 if (retval != -EAGAIN || signal_pending(current))
Peter Hurleyd6203d02016-01-09 21:13:53 -08002161 return retval;
Peter Hurley7f22f6c2016-01-09 21:13:45 -08002162 schedule();
2163 goto retry_open;
Alan Coxeeb89d92009-11-30 13:18:29 +00002164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02002166 tty_add_file(tty, filp);
Nick Piggind996b622010-08-18 04:37:36 +10002167
Jiri Slaby9de44bd2011-11-09 21:33:24 +01002168 check_tty_count(tty, __func__);
Peter Hurleyd435cef2015-11-08 13:01:19 -05002169 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
Peter Hurleyaccff792015-07-12 22:49:09 -04002170
Herton Ronaldo Krzesinski909bc772011-03-31 15:35:31 -03002171 if (tty->ops->open)
2172 retval = tty->ops->open(tty, filp);
2173 else
2174 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 filp->f_flags = saved_flags;
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if (retval) {
Peter Hurleyd435cef2015-11-08 13:01:19 -05002178 tty_debug_hangup(tty, "open error %d, releasing\n", retval);
Peter Hurleyaccff792015-07-12 22:49:09 -04002179
Alan Cox89c8d912012-08-08 16:30:13 +01002180 tty_unlock(tty); /* need to call tty_release without BTM */
Alan Coxeeb89d92009-11-30 13:18:29 +00002181 tty_release(inode, filp);
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +02002182 if (retval != -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return retval;
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +02002184
2185 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return retval;
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +02002187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 schedule();
2189 /*
2190 * Need to reset f_op in case a hangup happened.
2191 */
Peter Hurley12569372014-11-05 12:26:24 -05002192 if (tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 filp->f_op = &tty_fops;
2194 goto retry_open;
2195 }
Peter Hurleyd4855e12013-11-19 08:46:27 -05002196 clear_bit(TTY_HUPPED, &tty->flags);
Alan Coxeeb89d92009-11-30 13:18:29 +00002197
Peter Hurley11e1d4a2016-01-09 21:13:52 -08002198 noctty = (filp->f_flags & O_NOCTTY) ||
Nicolas Pitrea1235b32017-04-12 18:37:16 -04002199 (IS_ENABLED(CONFIG_VT) && device == MKDEV(TTY_MAJOR, 0)) ||
2200 device == MKDEV(TTYAUX_MAJOR, 1) ||
2201 (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2202 tty->driver->subtype == PTY_TYPE_MASTER);
2203 if (!noctty)
2204 tty_open_proc_set_tty(filp, tty);
Alan Cox89c8d912012-08-08 16:30:13 +01002205 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return 0;
2207}
2208
Jonathan Corbet39d95b92008-05-16 09:10:50 -06002209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Alan Coxaf9b8972006-08-27 01:24:01 -07002211/**
2212 * tty_poll - check tty status
2213 * @filp: file being polled
2214 * @wait: poll wait structures to update
2215 *
2216 * Call the line discipline polling method to obtain the poll
2217 * status of the device.
2218 *
2219 * Locking: locks called line discipline but ldisc poll method
2220 * may be re-entered freely by other callers.
2221 */
2222
Al Viroafc9a422017-07-03 06:39:46 -04002223static __poll_t tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Nick Piggind996b622010-08-18 04:37:36 +10002225 struct tty_struct *tty = file_tty(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 struct tty_ldisc *ld;
Al Viroe6c8adc2017-07-03 22:25:56 -04002227 __poll_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Al Viro6131ffa2013-02-27 16:59:05 -05002229 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002233 if (!ld)
2234 return hung_up_tty_poll(filp, wait);
Alan Coxa352def2008-07-16 21:53:12 +01002235 if (ld->ops->poll)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002236 ret = ld->ops->poll(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 tty_ldisc_deref(ld);
2238 return ret;
2239}
2240
Arnd Bergmannec79d602010-06-01 22:53:01 +02002241static int __tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
Nick Piggind996b622010-08-18 04:37:36 +10002243 struct tty_struct *tty = file_tty(filp);
Alan Cox47f86832008-04-30 00:53:30 -07002244 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002245 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Al Viro6131ffa2013-02-27 16:59:05 -05002247 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002248 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 retval = fasync_helper(fd, filp, on, &tty->fasync);
2251 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002252 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002255 enum pid_type type;
2256 struct pid *pid;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002257
Jiri Slaby64d608d2021-05-05 11:19:06 +02002258 spin_lock_irqsave(&tty->ctrl.lock, flags);
2259 if (tty->ctrl.pgrp) {
2260 pid = tty->ctrl.pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002261 type = PIDTYPE_PGID;
2262 } else {
2263 pid = task_pid(current);
Eric W. Biederman01919132017-07-16 22:05:57 -05002264 type = PIDTYPE_TGID;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002265 }
Linus Torvalds80e1e822010-02-07 10:11:23 -08002266 get_pid(pid);
Jiri Slaby64d608d2021-05-05 11:19:06 +02002267 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002268 __f_setown(filp, pid, type, 0);
Linus Torvalds80e1e822010-02-07 10:11:23 -08002269 put_pid(pid);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002270 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002272out:
Arnd Bergmannec79d602010-06-01 22:53:01 +02002273 return retval;
2274}
2275
2276static int tty_fasync(int fd, struct file *filp, int on)
2277{
Alan Cox89c8d912012-08-08 16:30:13 +01002278 struct tty_struct *tty = file_tty(filp);
Peter Hurleya8f3a292016-01-09 21:45:11 -08002279 int retval = -ENOTTY;
Alan Cox89c8d912012-08-08 16:30:13 +01002280
2281 tty_lock(tty);
Peter Hurleya8f3a292016-01-09 21:45:11 -08002282 if (!tty_hung_up_p(filp))
2283 retval = __tty_fasync(fd, filp, on);
Alan Cox89c8d912012-08-08 16:30:13 +01002284 tty_unlock(tty);
2285
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002286 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Alan Coxaf9b8972006-08-27 01:24:01 -07002289/**
2290 * tiocsti - fake input character
2291 * @tty: tty to fake input into
2292 * @p: pointer to character
2293 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02002294 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07002295 * input management.
2296 *
2297 * FIXME: does not honour flow control ??
2298 *
2299 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -04002300 * Called functions take tty_ldiscs_lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002301 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07002302 *
2303 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07002304 */
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306static int tiocsti(struct tty_struct *tty, char __user *p)
2307{
2308 char ch, mbz = 0;
2309 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2312 return -EPERM;
2313 if (get_user(ch, p))
2314 return -EFAULT;
Al Viro1e641742008-12-09 09:23:33 +00002315 tty_audit_tiocsti(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002317 if (!ld)
2318 return -EIO;
Greg Kroah-Hartman27cfb3a2019-01-20 10:46:58 +01002319 if (ld->ops->receive_buf)
2320 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 tty_ldisc_deref(ld);
2322 return 0;
2323}
2324
Alan Coxaf9b8972006-08-27 01:24:01 -07002325/**
2326 * tiocgwinsz - implement window query ioctl
Jiri Slabyfa441952020-08-18 10:56:51 +02002327 * @tty: tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002328 * @arg: user buffer for result
2329 *
Alan Cox808a0d32006-09-29 02:00:40 -07002330 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002331 *
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002332 * Locking: tty->winsize_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002333 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002334 */
2335
Alan Cox37bdfb02008-02-08 04:18:47 -08002336static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Alan Cox808a0d32006-09-29 02:00:40 -07002338 int err;
2339
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002340 mutex_lock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002341 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002342 mutex_unlock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002343
Xiaofei Tan94bc2eb2021-05-12 17:26:16 +08002344 return err ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346
Alan Coxaf9b8972006-08-27 01:24:01 -07002347/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002348 * tty_do_resize - resize event
2349 * @tty: tty being resized
Jiri Slabyfa441952020-08-18 10:56:51 +02002350 * @ws: new dimensions
Alan Coxaf9b8972006-08-27 01:24:01 -07002351 *
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08002352 * Update the termios variables and send the necessary signals to
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002353 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002354 */
2355
Alan Coxfc6f6232009-01-02 13:43:17 +00002356int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Alan Coxfc6f6232009-01-02 13:43:17 +00002358 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Alan Coxfc6f6232009-01-02 13:43:17 +00002360 /* Lock the tty */
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002361 mutex_lock(&tty->winsize_mutex);
Alan Coxfc6f6232009-01-02 13:43:17 +00002362 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002363 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002364
Peter Hurley5b239542014-10-16 14:59:45 -04002365 /* Signal the foreground process group */
2366 pgrp = tty_get_pgrp(tty);
Alan Cox47f86832008-04-30 00:53:30 -07002367 if (pgrp)
2368 kill_pgrp(pgrp, SIGWINCH, 1);
Alan Cox47f86832008-04-30 00:53:30 -07002369 put_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002370
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002371 tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002372done:
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002373 mutex_unlock(&tty->winsize_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return 0;
2375}
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01002376EXPORT_SYMBOL(tty_do_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Alan Coxaf9b8972006-08-27 01:24:01 -07002378/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002379 * tiocswinsz - implement window size set ioctl
Jiri Slabyfa441952020-08-18 10:56:51 +02002380 * @tty: tty side of tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002381 * @arg: user buffer for result
2382 *
2383 * Copies the user idea of the window size to the kernel. Traditionally
2384 * this is just advisory information but for the Linux console it
2385 * actually has driver level meaning and triggers a VC resize.
2386 *
2387 * Locking:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002388 * Driver dependent. The default do_resize method takes the
Jiri Slaby64d608d2021-05-05 11:19:06 +02002389 * tty termios mutex and ctrl.lock. The console takes its own lock
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002390 * then calls into the default method.
2391 */
2392
Alan Coxfc6f6232009-01-02 13:43:17 +00002393static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002394{
2395 struct winsize tmp_ws;
Xiaofei Tane73b2402021-05-12 17:26:15 +08002396
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002397 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2398 return -EFAULT;
2399
2400 if (tty->ops->resize)
Alan Coxfc6f6232009-01-02 13:43:17 +00002401 return tty->ops->resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002402 else
Alan Coxfc6f6232009-01-02 13:43:17 +00002403 return tty_do_resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002404}
2405
2406/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002407 * tioccons - allow admin to move logical console
2408 * @file: the file to become console
2409 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002410 * Allow the administrator to move the redirected console device
Alan Coxaf9b8972006-08-27 01:24:01 -07002411 *
2412 * Locking: uses redirect_lock to guard the redirect information
2413 */
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415static int tioccons(struct file *file)
2416{
2417 if (!capable(CAP_SYS_ADMIN))
2418 return -EPERM;
Linus Torvalds9bb48c82021-01-19 11:41:16 -08002419 if (file->f_op->write_iter == redirected_tty_write) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 struct file *f;
Xiaofei Tane73b2402021-05-12 17:26:15 +08002421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 spin_lock(&redirect_lock);
2423 f = redirect;
2424 redirect = NULL;
2425 spin_unlock(&redirect_lock);
2426 if (f)
2427 fput(f);
2428 return 0;
2429 }
Linus Torvaldsa9cbbb82021-01-29 12:28:20 -08002430 if (file->f_op->write_iter != tty_write)
2431 return -ENOTTY;
2432 if (!(file->f_mode & FMODE_WRITE))
2433 return -EBADF;
2434 if (!(file->f_mode & FMODE_CAN_WRITE))
2435 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 spin_lock(&redirect_lock);
2437 if (redirect) {
2438 spin_unlock(&redirect_lock);
2439 return -EBUSY;
2440 }
Al Virocb0942b2012-08-27 14:48:26 -04002441 redirect = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 spin_unlock(&redirect_lock);
2443 return 0;
2444}
2445
Alan Coxaf9b8972006-08-27 01:24:01 -07002446/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002447 * tiocsetd - set line discipline
2448 * @tty: tty device
2449 * @p: pointer to user data
2450 *
2451 * Set the line discipline according to user request.
2452 *
2453 * Locking: see tty_set_ldisc, this function is just a helper
2454 */
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456static int tiocsetd(struct tty_struct *tty, int __user *p)
2457{
Peter Hurleyc12da962016-01-10 22:41:04 -08002458 int disc;
Alan Cox04f378b2008-04-30 00:53:29 -07002459 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Peter Hurleyc12da962016-01-10 22:41:04 -08002461 if (get_user(disc, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002463
Peter Hurleyc12da962016-01-10 22:41:04 -08002464 ret = tty_set_ldisc(tty, disc);
Alan Cox04f378b2008-04-30 00:53:29 -07002465
2466 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Alan Coxaf9b8972006-08-27 01:24:01 -07002469/**
Peter Hurley5c17c862016-01-10 22:40:55 -08002470 * tiocgetd - get line discipline
2471 * @tty: tty device
2472 * @p: pointer to user data
2473 *
2474 * Retrieves the line discipline id directly from the ldisc.
2475 *
2476 * Locking: waits for ldisc reference (in case the line discipline
2477 * is changing or the tty is being hungup)
2478 */
2479
2480static int tiocgetd(struct tty_struct *tty, int __user *p)
2481{
2482 struct tty_ldisc *ld;
2483 int ret;
2484
2485 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002486 if (!ld)
2487 return -EIO;
Peter Hurley5c17c862016-01-10 22:40:55 -08002488 ret = put_user(ld->ops->num, p);
2489 tty_ldisc_deref(ld);
2490 return ret;
2491}
2492
2493/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002494 * send_break - performed time break
2495 * @tty: device to break on
2496 * @duration: timeout in mS
2497 *
2498 * Perform a timed break on hardware that lacks its own driver level
2499 * timed break functionality.
2500 *
2501 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002502 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002503 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002504 */
2505
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002506static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Alan Cox9e98966c2008-07-22 11:18:03 +01002508 int retval;
2509
2510 if (tty->ops->break_ctl == NULL)
2511 return 0;
2512
2513 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2514 retval = tty->ops->break_ctl(tty, duration);
2515 else {
2516 /* Do the work ourselves */
2517 if (tty_write_lock(tty, 0) < 0)
2518 return -EINTR;
2519 retval = tty->ops->break_ctl(tty, -1);
2520 if (retval)
2521 goto out;
2522 if (!signal_pending(current))
2523 msleep_interruptible(duration);
2524 retval = tty->ops->break_ctl(tty, 0);
2525out:
2526 tty_write_unlock(tty);
2527 if (signal_pending(current))
2528 retval = -EINTR;
2529 }
2530 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Alan Coxaf9b8972006-08-27 01:24:01 -07002533/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002534 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002535 * @tty: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002536 * @p: pointer to result
2537 *
2538 * Obtain the modem status bits from the tty driver if the feature
Johan Hovold1b8b2082021-04-07 11:52:02 +02002539 * is supported. Return -ENOTTY if it is not available.
Alan Coxaf9b8972006-08-27 01:24:01 -07002540 *
2541 * Locking: none (up to the driver)
2542 */
2543
Alan Cox60b33c12011-02-14 16:26:14 +00002544static int tty_tiocmget(struct tty_struct *tty, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
Johan Hovold1b8b2082021-04-07 11:52:02 +02002546 int retval = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Alan Coxf34d7a52008-04-30 00:54:13 -07002548 if (tty->ops->tiocmget) {
Alan Cox60b33c12011-02-14 16:26:14 +00002549 retval = tty->ops->tiocmget(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 if (retval >= 0)
2552 retval = put_user(retval, p);
2553 }
2554 return retval;
2555}
2556
Alan Coxaf9b8972006-08-27 01:24:01 -07002557/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002558 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002559 * @tty: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002560 * @cmd: command - clear bits, set bits or set all
2561 * @p: pointer to desired bits
2562 *
2563 * Set the modem status bits from the tty driver if the feature
Johan Hovold1b8b2082021-04-07 11:52:02 +02002564 * is supported. Return -ENOTTY if it is not available.
Alan Coxaf9b8972006-08-27 01:24:01 -07002565 *
2566 * Locking: none (up to the driver)
2567 */
2568
Alan Cox20b9d172011-02-14 16:26:50 +00002569static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 unsigned __user *p)
2571{
Alan Coxae677512008-07-16 21:56:54 +01002572 int retval;
2573 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Alan Coxae677512008-07-16 21:56:54 +01002575 if (tty->ops->tiocmset == NULL)
Johan Hovold1b8b2082021-04-07 11:52:02 +02002576 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Alan Coxae677512008-07-16 21:56:54 +01002578 retval = get_user(val, p);
2579 if (retval)
2580 return retval;
2581 set = clear = 0;
2582 switch (cmd) {
2583 case TIOCMBIS:
2584 set = val;
2585 break;
2586 case TIOCMBIC:
2587 clear = val;
2588 break;
2589 case TIOCMSET:
2590 set = val;
2591 clear = ~val;
2592 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
Alan Coxae677512008-07-16 21:56:54 +01002594 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2595 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
Alan Cox20b9d172011-02-14 16:26:50 +00002596 return tty->ops->tiocmset(tty, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
Uwe Kleine-Königd20c2192020-12-18 11:42:45 +01002599/**
2600 * tty_get_icount - get tty statistics
2601 * @tty: tty device
2602 * @icount: output parameter
2603 *
2604 * Gets a copy of the tty's icount statistics.
2605 *
2606 * Locking: none (up to the driver)
2607 */
2608int tty_get_icount(struct tty_struct *tty,
2609 struct serial_icounter_struct *icount)
2610{
2611 memset(icount, 0, sizeof(*icount));
2612
2613 if (tty->ops->get_icount)
2614 return tty->ops->get_icount(tty, icount);
2615 else
Johan Hovold1b8b2082021-04-07 11:52:02 +02002616 return -ENOTTY;
Uwe Kleine-Königd20c2192020-12-18 11:42:45 +01002617}
2618EXPORT_SYMBOL_GPL(tty_get_icount);
2619
Alan Coxd281da72010-09-16 18:21:24 +01002620static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2621{
Alan Coxd281da72010-09-16 18:21:24 +01002622 struct serial_icounter_struct icount;
Uwe Kleine-Königd20c2192020-12-18 11:42:45 +01002623 int retval;
2624
2625 retval = tty_get_icount(tty, &icount);
Alan Coxd281da72010-09-16 18:21:24 +01002626 if (retval != 0)
2627 return retval;
Uwe Kleine-Königd20c2192020-12-18 11:42:45 +01002628
Alan Coxd281da72010-09-16 18:21:24 +01002629 if (copy_to_user(arg, &icount, sizeof(icount)))
2630 return -EFAULT;
2631 return 0;
2632}
2633
Johan Hovold885c77d2021-04-07 11:52:05 +02002634static int tty_set_serial(struct tty_struct *tty, struct serial_struct *ss)
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002635{
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002636 char comm[TASK_COMM_LEN];
2637 int flags;
2638
Johan Hovold885c77d2021-04-07 11:52:05 +02002639 flags = ss->flags & ASYNC_DEPRECATED;
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002640
Johan Hovold1b7bc6b2021-04-07 11:52:04 +02002641 if (flags)
2642 pr_warn_ratelimited("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
2643 __func__, get_task_comm(comm, current), flags);
Johan Hovold885c77d2021-04-07 11:52:05 +02002644
Al Viro2f46a2c2018-09-11 21:53:32 -04002645 if (!tty->ops->set_serial)
Al Viro930236a2018-09-12 07:46:51 -04002646 return -ENOTTY;
Johan Hovold885c77d2021-04-07 11:52:05 +02002647
2648 return tty->ops->set_serial(tty, ss);
2649}
2650
2651static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss)
2652{
2653 struct serial_struct v;
2654
2655 if (copy_from_user(&v, ss, sizeof(*ss)))
2656 return -EFAULT;
2657
2658 return tty_set_serial(tty, &v);
Al Viro2f46a2c2018-09-11 21:53:32 -04002659}
2660
2661static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss)
2662{
2663 struct serial_struct v;
2664 int err;
2665
Gustavo A. R. Silvaa3241892020-07-23 17:34:22 -05002666 memset(&v, 0, sizeof(v));
Al Viro2f46a2c2018-09-11 21:53:32 -04002667 if (!tty->ops->get_serial)
Al Viro930236a2018-09-12 07:46:51 -04002668 return -ENOTTY;
Al Viro2f46a2c2018-09-11 21:53:32 -04002669 err = tty->ops->get_serial(tty, &v);
Gustavo A. R. Silvaa3241892020-07-23 17:34:22 -05002670 if (!err && copy_to_user(ss, &v, sizeof(v)))
Al Viro2f46a2c2018-09-11 21:53:32 -04002671 err = -EFAULT;
2672 return err;
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002673}
2674
Peter Hurley8f166e02014-10-16 14:59:41 -04002675/*
2676 * if pty, return the slave side (real_tty)
2677 * otherwise, return self
2678 */
2679static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
Alan Coxe8b70e72009-06-11 12:48:02 +01002680{
2681 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2682 tty->driver->subtype == PTY_TYPE_MASTER)
2683 tty = tty->link;
2684 return tty;
2685}
Alan Coxe8b70e72009-06-11 12:48:02 +01002686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687/*
2688 * Split this up, as gcc can choke on it otherwise..
2689 */
Alan Cox04f378b2008-04-30 00:53:29 -07002690long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
Nick Piggind996b622010-08-18 04:37:36 +10002692 struct tty_struct *tty = file_tty(file);
2693 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 void __user *p = (void __user *)arg;
2695 int retval;
2696 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002697
Al Viro6131ffa2013-02-27 16:59:05 -05002698 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 return -EINVAL;
2700
Alan Coxe8b70e72009-06-11 12:48:02 +01002701 real_tty = tty_pair_get_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 /*
2704 * Factor out some common prep work
2705 */
2706 switch (cmd) {
2707 case TIOCSETD:
2708 case TIOCSBRK:
2709 case TIOCCBRK:
2710 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002711 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 retval = tty_check_change(tty);
2713 if (retval)
2714 return retval;
2715 if (cmd != TIOCCBRK) {
2716 tty_wait_until_sent(tty, 0);
2717 if (signal_pending(current))
2718 return -EINTR;
2719 }
2720 break;
2721 }
2722
Alan Cox9e98966c2008-07-22 11:18:03 +01002723 /*
2724 * Now do the stuff.
2725 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002727 case TIOCSTI:
2728 return tiocsti(tty, p);
2729 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002730 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002731 case TIOCSWINSZ:
Alan Coxfc6f6232009-01-02 13:43:17 +00002732 return tiocswinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002733 case TIOCCONS:
2734 return real_tty != tty ? -EINVAL : tioccons(file);
Alan Cox37bdfb02008-02-08 04:18:47 -08002735 case TIOCEXCL:
2736 set_bit(TTY_EXCLUSIVE, &tty->flags);
2737 return 0;
2738 case TIOCNXCL:
2739 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2740 return 0;
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +04002741 case TIOCGEXCL:
2742 {
2743 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
Xiaofei Tane73b2402021-05-12 17:26:15 +08002744
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +04002745 return put_user(excl, (int __user *)p);
2746 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002747 case TIOCGETD:
Peter Hurley5c17c862016-01-10 22:40:55 -08002748 return tiocgetd(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002749 case TIOCSETD:
2750 return tiocsetd(tty, p);
Kay Sievers3c95c982011-02-17 18:39:28 +01002751 case TIOCVHANGUP:
2752 if (!capable(CAP_SYS_ADMIN))
2753 return -EPERM;
2754 tty_vhangup(tty);
2755 return 0;
Werner Finkb7b8de02010-12-03 12:48:23 +01002756 case TIOCGDEV:
2757 {
2758 unsigned int ret = new_encode_dev(tty_devnum(real_tty));
Xiaofei Tane73b2402021-05-12 17:26:15 +08002759
Werner Finkb7b8de02010-12-03 12:48:23 +01002760 return put_user(ret, (unsigned int __user *)p);
2761 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002762 /*
2763 * Break handling
2764 */
2765 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002766 if (tty->ops->break_ctl)
Alan Cox9e98966c2008-07-22 11:18:03 +01002767 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002768 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002769 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002770 if (tty->ops->break_ctl)
Alan Cox9e98966c2008-07-22 11:18:03 +01002771 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002772 return 0;
2773 case TCSBRK: /* SVID version: non-zero arg --> no break */
2774 /* non-zero arg means wait for all output data
2775 * to be sent (performed above) but don't send break.
2776 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002778 if (!arg)
2779 return send_break(tty, 250);
2780 return 0;
2781 case TCSBRKP: /* support for POSIX tcsendbreak() */
2782 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Alan Cox37bdfb02008-02-08 04:18:47 -08002784 case TIOCMGET:
Alan Cox60b33c12011-02-14 16:26:14 +00002785 return tty_tiocmget(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002786 case TIOCMSET:
2787 case TIOCMBIC:
2788 case TIOCMBIS:
Alan Cox20b9d172011-02-14 16:26:50 +00002789 return tty_tiocmset(tty, cmd, p);
Alan Coxd281da72010-09-16 18:21:24 +01002790 case TIOCGICOUNT:
Al Viroa3096192018-09-12 18:37:18 -04002791 return tty_tiocgicount(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002792 case TCFLSH:
2793 switch (arg) {
2794 case TCIFLUSH:
2795 case TCIOFLUSH:
2796 /* flush tty buffer and allow ldisc to process ioctl */
Peter Hurley86c80a82014-11-05 12:13:09 -05002797 tty_buffer_flush(tty, NULL);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002798 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002799 }
2800 break;
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002801 case TIOCSSERIAL:
Al Viro930236a2018-09-12 07:46:51 -04002802 return tty_tiocsserial(tty, p);
Al Viro2f46a2c2018-09-11 21:53:32 -04002803 case TIOCGSERIAL:
Al Viro930236a2018-09-12 07:46:51 -04002804 return tty_tiocgserial(tty, p);
Eric W. Biederman311fc652017-08-24 15:13:29 -05002805 case TIOCGPTPEER:
2806 /* Special because the struct file is needed */
2807 return ptm_open_peer(file, tty, (int)arg);
Nicolas Pitrea1235b32017-04-12 18:37:16 -04002808 default:
2809 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg);
2810 if (retval != -ENOIOCTLCMD)
2811 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002813 if (tty->ops->ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002814 retval = tty->ops->ioctl(tty, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (retval != -ENOIOCTLCMD)
2816 return retval;
2817 }
2818 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002819 if (!ld)
2820 return hung_up_tty_ioctl(file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002822 if (ld->ops->ioctl) {
2823 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 if (retval == -ENOIOCTLCMD)
Wanlong Gaobbb63c52012-08-27 15:23:12 +08002825 retval = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
2827 tty_ldisc_deref(ld);
2828 return retval;
2829}
2830
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002831#ifdef CONFIG_COMPAT
Al Viro77654352018-09-12 11:28:34 -04002832
2833struct serial_struct32 {
Gustavo A. R. Silva52b52e92020-07-23 17:32:25 -05002834 compat_int_t type;
2835 compat_int_t line;
2836 compat_uint_t port;
2837 compat_int_t irq;
2838 compat_int_t flags;
2839 compat_int_t xmit_fifo_size;
2840 compat_int_t custom_divisor;
2841 compat_int_t baud_base;
2842 unsigned short close_delay;
2843 char io_type;
2844 char reserved_char;
2845 compat_int_t hub6;
2846 unsigned short closing_wait; /* time to wait before closing */
2847 unsigned short closing_wait2; /* no longer used... */
2848 compat_uint_t iomem_base;
2849 unsigned short iomem_reg_shift;
2850 unsigned int port_high;
2851 /* compat_ulong_t iomap_base FIXME */
2852 compat_int_t reserved;
Al Viro77654352018-09-12 11:28:34 -04002853};
2854
2855static int compat_tty_tiocsserial(struct tty_struct *tty,
2856 struct serial_struct32 __user *ss)
2857{
Al Viro77654352018-09-12 11:28:34 -04002858 struct serial_struct32 v32;
2859 struct serial_struct v;
Al Viro77654352018-09-12 11:28:34 -04002860
Gustavo A. R. Silvaa3241892020-07-23 17:34:22 -05002861 if (copy_from_user(&v32, ss, sizeof(*ss)))
Al Viro77654352018-09-12 11:28:34 -04002862 return -EFAULT;
2863
2864 memcpy(&v, &v32, offsetof(struct serial_struct32, iomem_base));
2865 v.iomem_base = compat_ptr(v32.iomem_base);
2866 v.iomem_reg_shift = v32.iomem_reg_shift;
2867 v.port_high = v32.port_high;
2868 v.iomap_base = 0;
2869
Johan Hovold885c77d2021-04-07 11:52:05 +02002870 return tty_set_serial(tty, &v);
Al Viro77654352018-09-12 11:28:34 -04002871}
2872
2873static int compat_tty_tiocgserial(struct tty_struct *tty,
2874 struct serial_struct32 __user *ss)
2875{
2876 struct serial_struct32 v32;
2877 struct serial_struct v;
2878 int err;
Eric Biggers17329562020-02-24 10:20:43 -08002879
2880 memset(&v, 0, sizeof(v));
2881 memset(&v32, 0, sizeof(v32));
Al Viro77654352018-09-12 11:28:34 -04002882
Eric Biggers6e622cd2020-02-24 10:20:44 -08002883 if (!tty->ops->get_serial)
Al Viro77654352018-09-12 11:28:34 -04002884 return -ENOTTY;
2885 err = tty->ops->get_serial(tty, &v);
2886 if (!err) {
2887 memcpy(&v32, &v, offsetof(struct serial_struct32, iomem_base));
2888 v32.iomem_base = (unsigned long)v.iomem_base >> 32 ?
2889 0xfffffff : ptr_to_compat(v.iomem_base);
2890 v32.iomem_reg_shift = v.iomem_reg_shift;
2891 v32.port_high = v.port_high;
Gustavo A. R. Silvaa3241892020-07-23 17:34:22 -05002892 if (copy_to_user(ss, &v32, sizeof(v32)))
Al Viro77654352018-09-12 11:28:34 -04002893 err = -EFAULT;
2894 }
2895 return err;
2896}
Alan Cox37bdfb02008-02-08 04:18:47 -08002897static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002898 unsigned long arg)
2899{
Nick Piggind996b622010-08-18 04:37:36 +10002900 struct tty_struct *tty = file_tty(file);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002901 struct tty_ldisc *ld;
2902 int retval = -ENOIOCTLCMD;
2903
Al Viroe2112032018-09-11 19:47:09 -04002904 switch (cmd) {
Arnd Bergmannc7dc5042019-06-03 23:07:12 +02002905 case TIOCOUTQ:
Al Viroe2112032018-09-11 19:47:09 -04002906 case TIOCSTI:
2907 case TIOCGWINSZ:
2908 case TIOCSWINSZ:
2909 case TIOCGEXCL:
2910 case TIOCGETD:
2911 case TIOCSETD:
2912 case TIOCGDEV:
2913 case TIOCMGET:
2914 case TIOCMSET:
2915 case TIOCMBIC:
2916 case TIOCMBIS:
2917 case TIOCGICOUNT:
2918 case TIOCGPGRP:
2919 case TIOCSPGRP:
2920 case TIOCGSID:
2921 case TIOCSERGETLSR:
2922 case TIOCGRS485:
2923 case TIOCSRS485:
2924#ifdef TIOCGETP
2925 case TIOCGETP:
2926 case TIOCSETP:
2927 case TIOCSETN:
2928#endif
2929#ifdef TIOCGETC
2930 case TIOCGETC:
2931 case TIOCSETC:
2932#endif
2933#ifdef TIOCGLTC
2934 case TIOCGLTC:
2935 case TIOCSLTC:
2936#endif
2937 case TCSETSF:
2938 case TCSETSW:
2939 case TCSETS:
2940 case TCGETS:
2941#ifdef TCGETS2
2942 case TCGETS2:
2943 case TCSETSF2:
2944 case TCSETSW2:
2945 case TCSETS2:
2946#endif
2947 case TCGETA:
2948 case TCSETAF:
2949 case TCSETAW:
2950 case TCSETA:
2951 case TIOCGLCKTRMIOS:
2952 case TIOCSLCKTRMIOS:
2953#ifdef TCGETX
2954 case TCGETX:
2955 case TCSETX:
2956 case TCSETXW:
2957 case TCSETXF:
2958#endif
2959 case TIOCGSOFTCAR:
2960 case TIOCSSOFTCAR:
Arnd Bergmannb7aff092019-06-06 10:07:36 +02002961
2962 case PPPIOCGCHAN:
2963 case PPPIOCGUNIT:
Al Viroe2112032018-09-11 19:47:09 -04002964 return tty_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2965 case TIOCCONS:
2966 case TIOCEXCL:
2967 case TIOCNXCL:
2968 case TIOCVHANGUP:
2969 case TIOCSBRK:
2970 case TIOCCBRK:
2971 case TCSBRK:
2972 case TCSBRKP:
2973 case TCFLSH:
2974 case TIOCGPTPEER:
2975 case TIOCNOTTY:
2976 case TIOCSCTTY:
2977 case TCXONC:
2978 case TIOCMIWAIT:
2979 case TIOCSERCONFIG:
2980 return tty_ioctl(file, cmd, arg);
2981 }
2982
Al Viro6131ffa2013-02-27 16:59:05 -05002983 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002984 return -EINVAL;
2985
Al Viro77654352018-09-12 11:28:34 -04002986 switch (cmd) {
2987 case TIOCSSERIAL:
2988 return compat_tty_tiocsserial(tty, compat_ptr(arg));
2989 case TIOCGSERIAL:
2990 return compat_tty_tiocgserial(tty, compat_ptr(arg));
2991 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002992 if (tty->ops->compat_ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002993 retval = tty->ops->compat_ioctl(tty, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002994 if (retval != -ENOIOCTLCMD)
2995 return retval;
2996 }
2997
2998 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002999 if (!ld)
3000 return hung_up_tty_compat_ioctl(file, cmd, arg);
Alan Coxa352def2008-07-16 21:53:12 +01003001 if (ld->ops->compat_ioctl)
3002 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Al Virof0193d32018-09-13 22:12:15 -04003003 if (retval == -ENOIOCTLCMD && ld->ops->ioctl)
3004 retval = ld->ops->ioctl(tty, file,
3005 (unsigned long)compat_ptr(cmd), arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003006 tty_ldisc_deref(ld);
3007
3008 return retval;
3009}
3010#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Al Viroc3c073f2012-08-21 22:32:06 -04003012static int this_tty(const void *t, struct file *file, unsigned fd)
3013{
Linus Torvaldsdd78b0c2021-01-19 10:49:19 -08003014 if (likely(file->f_op->read_iter != tty_read))
Al Viroc3c073f2012-08-21 22:32:06 -04003015 return 0;
3016 return file_tty(file) != t ? 0 : fd + 1;
3017}
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019/*
3020 * This implements the "Secure Attention Key" --- the idea is to
3021 * prevent trojan horses by killing all processes associated with this
3022 * tty when the user hits the "Secure Attention Key". Required for
3023 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08003024 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 * This code could be nicer; ideally it should send a HUP, wait a few
3026 * seconds, then send a INT, and then a KILL signal. But you then
3027 * have to coordinate with the init process, since all processes associated
3028 * with the current tty must be dead before the new getty is allowed
3029 * to spawn.
3030 *
3031 * Now, if it would be correct ;-/ The current code has a nasty hole -
3032 * it doesn't catch files in flight. We may send the descriptor to ourselves
3033 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
3034 *
3035 * Nasty bug: do_SAK is being called in interrupt context. This can
3036 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3037 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08003038void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
3040#ifdef TTY_SOFT_SAK
3041 tty_hangup(tty);
3042#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08003043 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003044 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 int i;
Jann Hornc8bcd9c2020-12-03 02:25:05 +01003046 unsigned long flags;
Alan Cox37bdfb02008-02-08 04:18:47 -08003047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 if (!tty)
3049 return;
Jann Hornc8bcd9c2020-12-03 02:25:05 +01003050
Jiri Slaby64d608d2021-05-05 11:19:06 +02003051 spin_lock_irqsave(&tty->ctrl.lock, flags);
3052 session = get_pid(tty->ctrl.session);
3053 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08003054
Dan Carpenterb3f13de2006-12-13 00:35:09 -08003055 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Alan Coxf34d7a52008-04-30 00:54:13 -07003057 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08003058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003060 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003061 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05003062 tty_notice(tty, "SAK: killed process %d (%s): by session\n",
3063 task_pid_nr(p), p->comm);
Eric W. Biedermana8ebd172018-07-20 15:59:17 -05003064 group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003065 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Hurley9b42bb72015-11-08 13:01:14 -05003066
3067 /* Now kill any processes that happen to have the tty open */
Eric W. Biederman652486f2006-03-28 16:11:02 -08003068 do_each_thread(g, p) {
3069 if (p->signal->tty == tty) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05003070 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
3071 task_pid_nr(p), p->comm);
Eric W. Biedermana8ebd172018-07-20 15:59:17 -05003072 group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 continue;
3074 }
3075 task_lock(p);
Al Viroc3c073f2012-08-21 22:32:06 -04003076 i = iterate_fd(p->files, 0, this_tty, tty);
3077 if (i != 0) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05003078 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n",
3079 task_pid_nr(p), p->comm, i - 1);
Eric W. Biedermana8ebd172018-07-20 15:59:17 -05003080 group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
3082 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003083 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 read_unlock(&tasklist_lock);
Jann Hornc8bcd9c2020-12-03 02:25:05 +01003085 put_pid(session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086#endif
3087}
3088
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08003089static void do_SAK_work(struct work_struct *work)
3090{
3091 struct tty_struct *tty =
3092 container_of(work, struct tty_struct, SAK_work);
3093 __do_SAK(tty);
3094}
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096/*
3097 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3098 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3099 * the values which we write to it will be identical to the values which it
3100 * already has. --akpm
3101 */
3102void do_SAK(struct tty_struct *tty)
3103{
3104 if (!tty)
3105 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 schedule_work(&tty->SAK_work);
3107}
3108
3109EXPORT_SYMBOL(do_SAK);
3110
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003111/* Must put_device() after it's unused! */
3112static struct device *tty_get_device(struct tty_struct *tty)
3113{
3114 dev_t devt = tty_devnum(tty);
Xiaofei Tane73b2402021-05-12 17:26:15 +08003115
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01003116 return class_find_device_by_devt(tty_class, devt);
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003117}
3118
3119
Lee Jones08aa5042020-11-04 19:35:25 +00003120/*
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003121 * alloc_tty_struct
Alan Coxaf9b8972006-08-27 01:24:01 -07003122 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003123 * This subroutine allocates and initializes a tty structure.
Alan Coxaf9b8972006-08-27 01:24:01 -07003124 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003125 * Locking: none - tty in question is not exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003127
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003128struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003130 struct tty_struct *tty;
3131
3132 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
3133 if (!tty)
3134 return NULL;
3135
Alan Cox9c9f4de2008-10-13 10:37:26 +01003136 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 tty->magic = TTY_MAGIC;
Tetsuo Handa903f9db2018-04-05 19:40:16 +09003138 if (tty_ldisc_init(tty)) {
3139 kfree(tty);
3140 return NULL;
3141 }
Jiri Slaby64d608d2021-05-05 11:19:06 +02003142 tty->ctrl.session = NULL;
3143 tty->ctrl.pgrp = NULL;
Alan Cox89c8d912012-08-08 16:30:13 +01003144 mutex_init(&tty->legacy_mutex);
Peter Hurleyd8c1f922013-06-15 09:14:31 -04003145 mutex_init(&tty->throttle_mutex);
Peter Hurley6a1c0682013-06-15 09:14:23 -04003146 init_rwsem(&tty->termios_rwsem);
Peter Hurleydee4a0b2013-07-24 16:43:51 -04003147 mutex_init(&tty->winsize_mutex);
Peter Hurley36697522013-06-15 07:04:48 -04003148 init_ldsem(&tty->ldisc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 init_waitqueue_head(&tty->write_wait);
3150 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00003151 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08003152 mutex_init(&tty->atomic_write_lock);
Jiri Slaby64d608d2021-05-05 11:19:06 +02003153 spin_lock_init(&tty->ctrl.lock);
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02003154 spin_lock_init(&tty->flow.lock);
Peter Hurley4a510962016-01-09 21:35:23 -08003155 spin_lock_init(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003157 INIT_WORK(&tty->SAK_work, do_SAK_work);
Alan Coxbf970ee2008-10-13 10:42:39 +01003158
3159 tty->driver = driver;
3160 tty->ops = driver->ops;
3161 tty->index = idx;
3162 tty_line_name(driver, idx, tty->name);
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003163 tty->dev = tty_get_device(tty);
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003164
3165 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
Alan Coxf34d7a52008-04-30 00:54:13 -07003168/**
3169 * tty_put_char - write one character to a tty
3170 * @tty: tty
3171 * @ch: character
3172 *
3173 * Write one byte to the tty using the provided put_char method
3174 * if present. Returns the number of characters successfully output.
3175 *
3176 * Note: the specific put_char operation in the driver layer may go
3177 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003179
Alan Coxf34d7a52008-04-30 00:54:13 -07003180int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Alan Coxf34d7a52008-04-30 00:54:13 -07003182 if (tty->ops->put_char)
3183 return tty->ops->put_char(tty, ch);
3184 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
Alan Coxf34d7a52008-04-30 00:54:13 -07003186EXPORT_SYMBOL_GPL(tty_put_char);
3187
Alan Coxd81ed102008-10-13 10:41:42 +01003188struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003190static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
3191 unsigned int index, unsigned int count)
3192{
Leon Yuc1a752b2015-09-07 13:08:37 +00003193 int err;
3194
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003195 /* init here, since reused cdevs cause crashes */
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003196 driver->cdevs[index] = cdev_alloc();
3197 if (!driver->cdevs[index])
3198 return -ENOMEM;
Leon Yuc1a752b2015-09-07 13:08:37 +00003199 driver->cdevs[index]->ops = &tty_fops;
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003200 driver->cdevs[index]->owner = driver->owner;
Leon Yuc1a752b2015-09-07 13:08:37 +00003201 err = cdev_add(driver->cdevs[index], dev, count);
3202 if (err)
3203 kobject_put(&driver->cdevs[index]->kobj);
3204 return err;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003205}
3206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003208 * tty_register_device - register a tty device
3209 * @driver: the tty driver that describes the tty device
3210 * @index: the index in the tty driver for this tty device
3211 * @device: a struct device that is associated with this tty device.
3212 * This field is optional, if there is no known struct device
3213 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003215 * Returns a pointer to the struct device for this tty device
3216 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003217 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003218 * This call is required to be made to register an individual tty device
3219 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3220 * that bit is not set, this function should not be called by a tty
3221 * driver.
3222 *
3223 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003225
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003226struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3227 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003229 return tty_register_device_attr(driver, index, device, NULL, NULL);
3230}
3231EXPORT_SYMBOL(tty_register_device);
3232
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003233static void tty_device_create_release(struct device *dev)
3234{
Peter Hurley83db1df2015-11-08 13:01:21 -05003235 dev_dbg(dev, "releasing...\n");
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003236 kfree(dev);
3237}
3238
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003239/**
3240 * tty_register_device_attr - register a tty device
3241 * @driver: the tty driver that describes the tty device
3242 * @index: the index in the tty driver for this tty device
3243 * @device: a struct device that is associated with this tty device.
3244 * This field is optional, if there is no known struct device
3245 * for this tty device it can be set to NULL safely.
3246 * @drvdata: Driver data to be set to device.
3247 * @attr_grp: Attribute group to be set on device.
3248 *
3249 * Returns a pointer to the struct device for this tty device
3250 * (or ERR_PTR(-EFOO) on error).
3251 *
3252 * This call is required to be made to register an individual tty device
3253 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3254 * that bit is not set, this function should not be called by a tty
3255 * driver.
3256 *
3257 * Locking: ??
3258 */
3259struct device *tty_register_device_attr(struct tty_driver *driver,
3260 unsigned index, struct device *device,
3261 void *drvdata,
3262 const struct attribute_group **attr_grp)
3263{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 char name[64];
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003265 dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
Johan Hovold93857ed2017-03-30 15:39:36 +02003266 struct ktermios *tp;
Johan Hovold6a7e6f72017-03-30 15:39:34 +02003267 struct device *dev;
3268 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
3270 if (index >= driver->num) {
Peter Hurley656fb862015-11-08 13:01:15 -05003271 pr_err("%s: Attempt to register invalid tty line number (%d)\n",
3272 driver->name, index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003273 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 }
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 if (driver->type == TTY_DRIVER_TYPE_PTY)
3277 pty_line_name(driver, index, name);
3278 else
3279 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003280
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003281 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Johan Hovold6a7e6f72017-03-30 15:39:34 +02003282 if (!dev)
3283 return ERR_PTR(-ENOMEM);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003284
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003285 dev->devt = devt;
3286 dev->class = tty_class;
3287 dev->parent = device;
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003288 dev->release = tty_device_create_release;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003289 dev_set_name(dev, "%s", name);
3290 dev->groups = attr_grp;
3291 dev_set_drvdata(dev, drvdata);
3292
Johan Hovold6a7e6f72017-03-30 15:39:34 +02003293 dev_set_uevent_suppress(dev, 1);
3294
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003295 retval = device_register(dev);
3296 if (retval)
Johan Hovold6a7e6f72017-03-30 15:39:34 +02003297 goto err_put;
3298
3299 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
Johan Hovold93857ed2017-03-30 15:39:36 +02003300 /*
3301 * Free any saved termios data so that the termios state is
3302 * reset when reusing a minor number.
3303 */
3304 tp = driver->termios[index];
3305 if (tp) {
3306 driver->termios[index] = NULL;
3307 kfree(tp);
3308 }
3309
Johan Hovold6a7e6f72017-03-30 15:39:34 +02003310 retval = tty_cdev_add(driver, devt, index, 1);
3311 if (retval)
3312 goto err_del;
3313 }
3314
3315 dev_set_uevent_suppress(dev, 0);
3316 kobject_uevent(&dev->kobj, KOBJ_ADD);
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003317
3318 return dev;
3319
Johan Hovold6a7e6f72017-03-30 15:39:34 +02003320err_del:
3321 device_del(dev);
3322err_put:
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003323 put_device(dev);
Johan Hovold6a7e6f72017-03-30 15:39:34 +02003324
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003325 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003327EXPORT_SYMBOL_GPL(tty_register_device_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
3329/**
Xiaofei Tan395e7832021-05-12 17:26:14 +08003330 * tty_unregister_device - unregister a tty device
3331 * @driver: the tty driver that describes the tty device
3332 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 *
Xiaofei Tan395e7832021-05-12 17:26:14 +08003334 * If a tty device is registered with a call to tty_register_device() then
Alan Coxaf9b8972006-08-27 01:24:01 -07003335 * this function must be called when the tty device is gone.
3336 *
3337 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340void tty_unregister_device(struct tty_driver *driver, unsigned index)
3341{
Alan Cox37bdfb02008-02-08 04:18:47 -08003342 device_destroy(tty_class,
3343 MKDEV(driver->major, driver->minor_start) + index);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003344 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3345 cdev_del(driver->cdevs[index]);
3346 driver->cdevs[index] = NULL;
3347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349EXPORT_SYMBOL(tty_unregister_device);
3350
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003351/**
3352 * __tty_alloc_driver -- allocate tty driver
3353 * @lines: count of lines this driver can handle at most
Thadeu Lima de Souza Cascardo87838ae2017-04-04 05:56:32 -03003354 * @owner: module which is responsible for this driver
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003355 * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
3356 *
3357 * This should not be called directly, some of the provided macros should be
3358 * used instead. Use IS_ERR and friends on @retval.
3359 */
3360struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
3361 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
3363 struct tty_driver *driver;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003364 unsigned int cdevs = 1;
Jiri Slaby16a02082012-08-08 22:26:42 +02003365 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Jiri Slaby0019b402012-08-08 22:26:43 +02003367 if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003368 return ERR_PTR(-EINVAL);
3369
Gustavo A. R. Silvaa3241892020-07-23 17:34:22 -05003370 driver = kzalloc(sizeof(*driver), GFP_KERNEL);
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003371 if (!driver)
3372 return ERR_PTR(-ENOMEM);
3373
3374 kref_init(&driver->kref);
3375 driver->magic = TTY_DRIVER_MAGIC;
3376 driver->num = lines;
3377 driver->owner = owner;
3378 driver->flags = flags;
Jiri Slaby16a02082012-08-08 22:26:42 +02003379
3380 if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
3381 driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
3382 GFP_KERNEL);
3383 driver->termios = kcalloc(lines, sizeof(*driver->termios),
3384 GFP_KERNEL);
3385 if (!driver->ttys || !driver->termios) {
3386 err = -ENOMEM;
3387 goto err_free_all;
3388 }
3389 }
3390
3391 if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3392 driver->ports = kcalloc(lines, sizeof(*driver->ports),
3393 GFP_KERNEL);
3394 if (!driver->ports) {
3395 err = -ENOMEM;
3396 goto err_free_all;
3397 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003398 cdevs = lines;
3399 }
3400
3401 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
3402 if (!driver->cdevs) {
3403 err = -ENOMEM;
3404 goto err_free_all;
Jiri Slaby16a02082012-08-08 22:26:42 +02003405 }
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return driver;
Jiri Slaby16a02082012-08-08 22:26:42 +02003408err_free_all:
3409 kfree(driver->ports);
3410 kfree(driver->ttys);
3411 kfree(driver->termios);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003412 kfree(driver->cdevs);
Jiri Slaby16a02082012-08-08 22:26:42 +02003413 kfree(driver);
3414 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003416EXPORT_SYMBOL(__tty_alloc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Alan Cox7d7b93c2008-10-13 10:42:09 +01003418static void destruct_tty_driver(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003420 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
3421 int i;
3422 struct ktermios *tp;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003423
3424 if (driver->flags & TTY_DRIVER_INSTALLED) {
Alan Cox7d7b93c2008-10-13 10:42:09 +01003425 for (i = 0; i < driver->num; i++) {
3426 tp = driver->termios[i];
3427 if (tp) {
3428 driver->termios[i] = NULL;
3429 kfree(tp);
3430 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003431 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3432 tty_unregister_device(driver, i);
3433 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003434 proc_tty_unregister_driver(driver);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003435 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003436 cdev_del(driver->cdevs[0]);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003437 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003438 kfree(driver->cdevs);
Jiri Slaby04831dc2012-06-04 13:35:36 +02003439 kfree(driver->ports);
Jiri Slaby16a02082012-08-08 22:26:42 +02003440 kfree(driver->termios);
3441 kfree(driver->ttys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 kfree(driver);
3443}
3444
Alan Cox7d7b93c2008-10-13 10:42:09 +01003445void tty_driver_kref_put(struct tty_driver *driver)
3446{
3447 kref_put(&driver->kref, destruct_tty_driver);
3448}
3449EXPORT_SYMBOL(tty_driver_kref_put);
3450
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003451void tty_set_operations(struct tty_driver *driver,
3452 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453{
Alan Coxf34d7a52008-04-30 00:54:13 -07003454 driver->ops = op;
3455};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456EXPORT_SYMBOL(tty_set_operations);
3457
Alan Cox7d7b93c2008-10-13 10:42:09 +01003458void put_tty_driver(struct tty_driver *d)
3459{
3460 tty_driver_kref_put(d);
3461}
3462EXPORT_SYMBOL(put_tty_driver);
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464/*
3465 * Called by a tty driver to register itself.
3466 */
3467int tty_register_driver(struct tty_driver *driver)
3468{
3469 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08003470 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 dev_t dev;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003472 struct device *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003475 error = alloc_chrdev_region(&dev, driver->minor_start,
3476 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 if (!error) {
3478 driver->major = MAJOR(dev);
3479 driver->minor_start = MINOR(dev);
3480 }
3481 } else {
3482 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01003483 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 }
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003485 if (error < 0)
Jiri Slaby16a02082012-08-08 22:26:42 +02003486 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003488 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
3489 error = tty_cdev_add(driver, dev, 0, driver->num);
3490 if (error)
3491 goto err_unreg_char;
3492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003494 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003496 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003497
3498 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003499 for (i = 0; i < driver->num; i++) {
3500 d = tty_register_device(driver, i, NULL);
3501 if (IS_ERR(d)) {
3502 error = PTR_ERR(d);
Jiri Slaby16a02082012-08-08 22:26:42 +02003503 goto err_unreg_devs;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003504 }
3505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
3507 proc_tty_register_driver(driver);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003508 driver->flags |= TTY_DRIVER_INSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 return 0;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003510
Jiri Slaby16a02082012-08-08 22:26:42 +02003511err_unreg_devs:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003512 for (i--; i >= 0; i--)
3513 tty_unregister_device(driver, i);
3514
3515 mutex_lock(&tty_mutex);
3516 list_del(&driver->tty_drivers);
3517 mutex_unlock(&tty_mutex);
3518
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003519err_unreg_char:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003520 unregister_chrdev_region(dev, driver->num);
Jiri Slaby16a02082012-08-08 22:26:42 +02003521err:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003522 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524EXPORT_SYMBOL(tty_register_driver);
3525
3526/*
3527 * Called by a tty driver to unregister itself.
3528 */
Jiri Slabya872ab42021-03-02 07:22:05 +01003529void tty_unregister_driver(struct tty_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3532 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003533 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003535 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537EXPORT_SYMBOL(tty_unregister_driver);
3538
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003539dev_t tty_devnum(struct tty_struct *tty)
3540{
3541 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3542}
3543EXPORT_SYMBOL(tty_devnum);
3544
Alan Coxd81ed102008-10-13 10:41:42 +01003545void tty_default_fops(struct file_operations *fops)
3546{
3547 *fops = tty_fops;
3548}
3549
Al Viro2c9ede52011-07-23 20:24:48 -04003550static char *tty_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +02003551{
3552 if (!mode)
3553 return NULL;
3554 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3555 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3556 *mode = 0666;
3557 return NULL;
3558}
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560static int __init tty_class_init(void)
3561{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003562 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 if (IS_ERR(tty_class))
3564 return PTR_ERR(tty_class);
Kay Sieverse454cea2009-09-18 23:01:12 +02003565 tty_class->devnode = tty_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 return 0;
3567}
3568
3569postcore_initcall(tty_class_init);
3570
3571/* 3/2004 jmc: why do these devices exist? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572static struct cdev tty_cdev, console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Kay Sieversfbc92a32010-12-01 18:51:05 +01003574static ssize_t show_cons_active(struct device *dev,
3575 struct device_attribute *attr, char *buf)
3576{
3577 struct console *cs[16];
3578 int i = 0;
3579 struct console *c;
3580 ssize_t count = 0;
3581
Torben Hohnac751ef2011-01-25 15:07:35 -08003582 console_lock();
Kay Sieversa2a6a822011-01-09 16:39:14 +01003583 for_each_console(c) {
Kay Sieversfbc92a32010-12-01 18:51:05 +01003584 if (!c->device)
3585 continue;
3586 if (!c->write)
3587 continue;
3588 if ((c->flags & CON_ENABLED) == 0)
3589 continue;
3590 cs[i++] = c;
3591 if (i >= ARRAY_SIZE(cs))
3592 break;
3593 }
Hannes Reinecke723abd82014-02-27 12:30:51 +01003594 while (i--) {
3595 int index = cs[i]->index;
3596 struct tty_driver *drv = cs[i]->device(cs[i], &index);
3597
3598 /* don't resolve tty0 as some programs depend on it */
3599 if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR))
3600 count += tty_line_name(drv, index, buf + count);
3601 else
3602 count += sprintf(buf + count, "%s%d",
3603 cs[i]->name, cs[i]->index);
3604
3605 count += sprintf(buf + count, "%c", i ? ' ':'\n');
3606 }
Torben Hohnac751ef2011-01-25 15:07:35 -08003607 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01003608
3609 return count;
3610}
3611static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3612
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003613static struct attribute *cons_dev_attrs[] = {
3614 &dev_attr_active.attr,
3615 NULL
3616};
3617
3618ATTRIBUTE_GROUPS(cons_dev);
3619
Kay Sieversfbc92a32010-12-01 18:51:05 +01003620static struct device *consdev;
3621
3622void console_sysfs_notify(void)
3623{
3624 if (consdev)
3625 sysfs_notify(&consdev->kobj, NULL, "active");
3626}
3627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628/*
3629 * Ok, now we can initialize the rest of the tty devices and can count
3630 * on memory allocations, interrupts etc..
3631 */
David Howells31d1d482010-08-06 16:34:43 +01003632int __init tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +01003634 tty_sysctl_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 cdev_init(&tty_cdev, &tty_fops);
3636 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3637 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3638 panic("Couldn't register /dev/tty driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003639 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641 cdev_init(&console_cdev, &console_fops);
3642 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3643 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3644 panic("Couldn't register /dev/console driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003645 consdev = device_create_with_groups(tty_class, NULL,
3646 MKDEV(TTYAUX_MAJOR, 1), NULL,
3647 cons_dev_groups, "console");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003648 if (IS_ERR(consdev))
3649 consdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651#ifdef CONFIG_VT
Alan Coxd81ed102008-10-13 10:41:42 +01003652 vty_init(&console_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653#endif
3654 return 0;
3655}
David Howells31d1d482010-08-06 16:34:43 +01003656