blob: 49abf04c90b2ac7db35756d5fb45634f0ee98e4e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 */
4
5/*
6 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
7 * or rs-channels. It also implements echoing, cooked mode etc.
8 *
9 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
10 *
11 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
12 * tty_struct and tty_queue structures. Previously there was an array
13 * of 256 tty_struct's which was statically allocated, and the
14 * tty_queue structures were allocated at boot time. Both are now
15 * dynamically allocated only when the tty is open.
16 *
17 * Also restructured routines so that there is more of a separation
18 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
19 * the low-level tty routines (serial.c, pty.c, console.c). This
Alan Cox37bdfb02008-02-08 04:18:47 -080020 * makes for cleaner and more compact code. -TYT, 9/17/92
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
23 * which can be dynamically activated and de-activated by the line
24 * discipline handling modules (like SLIP).
25 *
26 * NOTE: pay no attention to the line discipline code (yet); its
27 * interface is still subject to change in this version...
28 * -- TYT, 1/31/92
29 *
30 * Added functionality to the OPOST tty handling. No delays, but all
31 * other bits should be there.
32 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
33 *
34 * Rewrote canonical mode and added more termios flags.
35 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
36 *
37 * Reorganized FASYNC support so mouse code can share it.
38 * -- ctm@ardi.com, 9Sep95
39 *
40 * New TIOCLINUX variants added.
41 * -- mj@k332.feld.cvut.cz, 19-Nov-95
Alan Cox37bdfb02008-02-08 04:18:47 -080042 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * Restrict vt switching via ioctl()
44 * -- grif@cs.ucr.edu, 5-Dec-95
45 *
46 * Move console and virtual terminal code to more appropriate files,
47 * implement CONFIG_VT and generalize console device interface.
48 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
49 *
Alan Coxd81ed102008-10-13 10:41:42 +010050 * Rewrote tty_init_dev and tty_release_dev to eliminate races.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * -- Bill Hawes <whawes@star.net>, June 97
52 *
53 * Added devfs support.
54 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
55 *
56 * Added support for a Unix98-style ptmx device.
57 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
58 *
59 * Reduced memory usage for older ARM systems
60 * -- Russell King <rmk@arm.linux.org.uk>
61 *
62 * Move do_SAK() into process context. Less stack use in devfs functions.
Alan Cox37bdfb02008-02-08 04:18:47 -080063 * alloc_tty_struct() always uses kmalloc()
64 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/types.h>
68#include <linux/major.h>
69#include <linux/errno.h>
70#include <linux/signal.h>
71#include <linux/fcntl.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010072#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010073#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/interrupt.h>
75#include <linux/tty.h>
76#include <linux/tty_driver.h>
77#include <linux/tty_flip.h>
78#include <linux/devpts_fs.h>
79#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040080#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/console.h>
82#include <linux/timer.h>
83#include <linux/ctype.h>
84#include <linux/kd.h>
85#include <linux/mm.h>
86#include <linux/string.h>
87#include <linux/slab.h>
88#include <linux/poll.h>
89#include <linux/proc_fs.h>
90#include <linux/init.h>
91#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/wait.h>
94#include <linux/bitops.h>
Domen Puncerb20f3ae2005-06-25 14:58:42 -070095#include <linux/delay.h>
Alan Coxa352def2008-07-16 21:53:12 +010096#include <linux/seq_file.h>
Alan Coxd281da72010-09-16 18:21:24 +010097#include <linux/serial.h>
Manuel Zerpies5a3c6b2512011-06-16 14:07:22 +020098#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Alan Coxa352def2008-07-16 21:53:12 +0100100#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102#include <linux/kbd_kern.h>
103#include <linux/vt_kern.h>
104#include <linux/selection.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106#include <linux/kmod.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700107#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109#undef TTY_DEBUG_HANGUP
Peter Hurleyaccff792015-07-12 22:49:09 -0400110#ifdef TTY_DEBUG_HANGUP
111# define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args)
112#else
113# define tty_debug_hangup(tty, f, args...) do { } while (0)
114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define TTY_PARANOIA_CHECK 1
117#define CHECK_TTY_COUNT 1
118
Alan Coxedc6afc2006-12-08 02:38:44 -0800119struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .c_iflag = ICRNL | IXON,
121 .c_oflag = OPOST | ONLCR,
122 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
123 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
124 ECHOCTL | ECHOKE | IEXTEN,
Alan Coxedc6afc2006-12-08 02:38:44 -0800125 .c_cc = INIT_C_CC,
126 .c_ispeed = 38400,
Peter Hurley133b1302016-01-10 22:41:07 -0800127 .c_ospeed = 38400,
128 /* .c_line = N_TTY, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131EXPORT_SYMBOL(tty_std_termios);
132
133/* This list gets poked at by procfs and various bits of boot up code. This
134 could do with some rationalisation such as pulling the tty proc function
135 into this file */
Alan Cox37bdfb02008-02-08 04:18:47 -0800136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137LIST_HEAD(tty_drivers); /* linked list of tty drivers */
138
Peter Hurleyd1d027e2016-01-09 21:35:18 -0800139/* Mutex to protect creating and releasing a tty */
Ingo Molnar70522e12006-03-23 03:00:31 -0800140DEFINE_MUTEX(tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
143static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
Alan Cox37bdfb02008-02-08 04:18:47 -0800144ssize_t redirected_tty_write(struct file *, const char __user *,
145 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static unsigned int tty_poll(struct file *, poll_table *);
147static int tty_open(struct inode *, struct file *);
Alan Cox04f378b2008-04-30 00:53:29 -0700148long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700149#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -0800150static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700151 unsigned long arg);
152#else
153#define tty_compat_ioctl NULL
154#endif
Arnd Bergmannec79d602010-06-01 22:53:01 +0200155static int __tty_fasync(int fd, struct file *filp, int on);
Alan Cox37bdfb02008-02-08 04:18:47 -0800156static int tty_fasync(int fd, struct file *filp, int on);
Christoph Hellwigd5698c22007-02-10 01:46:46 -0800157static void release_tty(struct tty_struct *tty, int idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Alan Coxaf9b8972006-08-27 01:24:01 -0700159/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700160 * free_tty_struct - free a disused tty
161 * @tty: tty struct to free
162 *
163 * Free the write buffers, tty queue and tty memory itself.
164 *
165 * Locking: none. Must be called after tty is definitely unused
166 */
167
Peter Hurleya3123fd2016-01-09 21:13:48 -0800168static void free_tty_struct(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Peter Hurleyc8b710b2016-01-09 21:13:46 -0800170 tty_ldisc_deinit(tty);
Markus Elfringa211b1a2014-11-21 13:42:29 +0100171 put_device(tty->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 kfree(tty->write_buf);
Alan Cox89c8d912012-08-08 16:30:13 +0100173 tty->magic = 0xDEADDEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 kfree(tty);
175}
176
Nick Piggind996b622010-08-18 04:37:36 +1000177static inline struct tty_struct *file_tty(struct file *file)
178{
179 return ((struct tty_file_private *)file->private_data)->tty;
180}
181
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200182int tty_alloc_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000183{
184 struct tty_file_private *priv;
185
Pekka Enbergf573bd12010-08-24 07:48:34 +0300186 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
187 if (!priv)
188 return -ENOMEM;
Nick Piggind996b622010-08-18 04:37:36 +1000189
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200190 file->private_data = priv;
191
192 return 0;
193}
194
195/* Associate a new file with the tty structure */
196void tty_add_file(struct tty_struct *tty, struct file *file)
197{
198 struct tty_file_private *priv = file->private_data;
199
Nick Piggind996b622010-08-18 04:37:36 +1000200 priv->tty = tty;
201 priv->file = file;
Nick Piggind996b622010-08-18 04:37:36 +1000202
Peter Hurley4a510962016-01-09 21:35:23 -0800203 spin_lock(&tty->files_lock);
Nick Piggind996b622010-08-18 04:37:36 +1000204 list_add(&priv->list, &tty->tty_files);
Peter Hurley4a510962016-01-09 21:35:23 -0800205 spin_unlock(&tty->files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200206}
Pekka Enbergf573bd12010-08-24 07:48:34 +0300207
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200208/**
209 * tty_free_file - free file->private_data
210 *
211 * This shall be used only for fail path handling when tty_add_file was not
212 * called yet.
213 */
214void tty_free_file(struct file *file)
215{
216 struct tty_file_private *priv = file->private_data;
217
218 file->private_data = NULL;
219 kfree(priv);
Nick Piggind996b622010-08-18 04:37:36 +1000220}
221
222/* Delete file from its tty */
Josh Triplett2520e272012-11-18 21:27:47 -0800223static void tty_del_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000224{
225 struct tty_file_private *priv = file->private_data;
Peter Hurley4a510962016-01-09 21:35:23 -0800226 struct tty_struct *tty = priv->tty;
Nick Piggind996b622010-08-18 04:37:36 +1000227
Peter Hurley4a510962016-01-09 21:35:23 -0800228 spin_lock(&tty->files_lock);
Nick Piggind996b622010-08-18 04:37:36 +1000229 list_del(&priv->list);
Peter Hurley4a510962016-01-09 21:35:23 -0800230 spin_unlock(&tty->files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200231 tty_free_file(file);
Nick Piggind996b622010-08-18 04:37:36 +1000232}
233
Alan Coxaf9b8972006-08-27 01:24:01 -0700234/**
235 * tty_name - return tty naming
236 * @tty: tty structure
Alan Coxaf9b8972006-08-27 01:24:01 -0700237 *
238 * Convert a tty structure into a name. The name reflects the kernel
239 * naming policy and if udev is in use may not reflect user space
240 *
241 * Locking: none
242 */
243
Rasmus Villemoes429b4742015-03-31 15:55:59 +0200244const char *tty_name(const struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 if (!tty) /* Hmm. NULL pointer. That's fun. */
Rasmus Villemoes917162c2015-03-31 15:55:58 +0200247 return "NULL tty";
248 return tty->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251EXPORT_SYMBOL(tty_name);
252
Peter Hurley0a083ed2015-11-08 13:01:12 -0500253const char *tty_driver_name(const struct tty_struct *tty)
254{
255 if (!tty || !tty->driver)
256 return "";
257 return tty->driver->name;
258}
259
Peter Hurley82b8f882015-11-08 13:01:09 -0500260static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 const char *routine)
262{
263#ifdef TTY_PARANOIA_CHECK
264 if (!tty) {
Peter Hurley89222e62015-11-08 13:01:18 -0500265 pr_warn("(%d:%d): %s: NULL tty\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 imajor(inode), iminor(inode), routine);
267 return 1;
268 }
269 if (tty->magic != TTY_MAGIC) {
Peter Hurley89222e62015-11-08 13:01:18 -0500270 pr_warn("(%d:%d): %s: bad magic number\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 imajor(inode), iminor(inode), routine);
272 return 1;
273 }
274#endif
275 return 0;
276}
277
Peter Hurleydeb287e2014-11-05 12:12:55 -0500278/* Caller must hold tty_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static int check_tty_count(struct tty_struct *tty, const char *routine)
280{
281#ifdef CHECK_TTY_COUNT
282 struct list_head *p;
283 int count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800284
Peter Hurley4a510962016-01-09 21:35:23 -0800285 spin_lock(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 list_for_each(p, &tty->tty_files) {
287 count++;
288 }
Peter Hurley4a510962016-01-09 21:35:23 -0800289 spin_unlock(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
291 tty->driver->subtype == PTY_TYPE_SLAVE &&
292 tty->link && tty->link->count)
293 count++;
294 if (tty->count != count) {
Peter Hurley339f36b2015-11-08 13:01:13 -0500295 tty_warn(tty, "%s: tty->count(%d) != #fd's(%d)\n",
296 routine, tty->count, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return count;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#endif
300 return 0;
301}
302
Alan Coxaf9b8972006-08-27 01:24:01 -0700303/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700304 * get_tty_driver - find device of a tty
305 * @dev_t: device identifier
306 * @index: returns the index of the tty
307 *
308 * This routine returns a tty driver structure, given a device number
309 * and also passes back the index number.
310 *
311 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static struct tty_driver *get_tty_driver(dev_t device, int *index)
315{
316 struct tty_driver *p;
317
318 list_for_each_entry(p, &tty_drivers, tty_drivers) {
319 dev_t base = MKDEV(p->major, p->minor_start);
320 if (device < base || device >= base + p->num)
321 continue;
322 *index = device - base;
Alan Cox7d7b93c2008-10-13 10:42:09 +0100323 return tty_driver_kref_get(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325 return NULL;
326}
327
Jason Wesself2d937f2008-04-17 20:05:37 +0200328#ifdef CONFIG_CONSOLE_POLL
329
330/**
331 * tty_find_polling_driver - find device of a polled tty
332 * @name: name string to match
333 * @line: pointer to resulting tty line nr
334 *
335 * This routine returns a tty driver structure, given a name
336 * and the condition that the tty driver is capable of polled
337 * operation.
338 */
339struct tty_driver *tty_find_polling_driver(char *name, int *line)
340{
341 struct tty_driver *p, *res = NULL;
342 int tty_line = 0;
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500343 int len;
Alan Cox5f0878a2009-06-11 12:46:41 +0100344 char *str, *stp;
Jason Wesself2d937f2008-04-17 20:05:37 +0200345
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500346 for (str = name; *str; str++)
347 if ((*str >= '0' && *str <= '9') || *str == ',')
348 break;
349 if (!*str)
350 return NULL;
351
352 len = str - name;
353 tty_line = simple_strtoul(str, &str, 10);
354
Jason Wesself2d937f2008-04-17 20:05:37 +0200355 mutex_lock(&tty_mutex);
356 /* Search through the tty devices to look for a match */
357 list_for_each_entry(p, &tty_drivers, tty_drivers) {
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500358 if (strncmp(name, p->name, len) != 0)
359 continue;
Alan Cox5f0878a2009-06-11 12:46:41 +0100360 stp = str;
361 if (*stp == ',')
362 stp++;
363 if (*stp == '\0')
364 stp = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +0200365
Nathael Pajani6eb68d62010-09-02 16:06:16 +0200366 if (tty_line >= 0 && tty_line < p->num && p->ops &&
Alan Cox5f0878a2009-06-11 12:46:41 +0100367 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
Alan Cox7d7b93c2008-10-13 10:42:09 +0100368 res = tty_driver_kref_get(p);
Jason Wesself2d937f2008-04-17 20:05:37 +0200369 *line = tty_line;
370 break;
371 }
372 }
373 mutex_unlock(&tty_mutex);
374
375 return res;
376}
377EXPORT_SYMBOL_GPL(tty_find_polling_driver);
378#endif
379
Alan Cox37bdfb02008-02-08 04:18:47 -0800380static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 size_t count, loff_t *ppos)
382{
383 return 0;
384}
385
Alan Cox37bdfb02008-02-08 04:18:47 -0800386static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 size_t count, loff_t *ppos)
388{
389 return -EIO;
390}
391
392/* No kernel lock held - none needed ;) */
Alan Cox37bdfb02008-02-08 04:18:47 -0800393static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
396}
397
Alan Cox04f378b2008-04-30 00:53:29 -0700398static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
399 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700400{
401 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
402}
403
Alan Cox37bdfb02008-02-08 04:18:47 -0800404static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700405 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
408}
409
Peter Hurleyf5574742016-01-09 21:45:10 -0800410static int hung_up_tty_fasync(int fd, struct file *file, int on)
411{
412 return -ENOTTY;
413}
414
Arjan van de Ven62322d22006-07-03 00:24:21 -0700415static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 .llseek = no_llseek,
417 .read = tty_read,
418 .write = tty_write,
419 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700420 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700421 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 .open = tty_open,
423 .release = tty_release,
424 .fasync = tty_fasync,
425};
426
Arjan van de Ven62322d22006-07-03 00:24:21 -0700427static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 .llseek = no_llseek,
429 .read = tty_read,
430 .write = redirected_tty_write,
431 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700432 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700433 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 .open = tty_open,
435 .release = tty_release,
436 .fasync = tty_fasync,
437};
438
Arjan van de Ven62322d22006-07-03 00:24:21 -0700439static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 .llseek = no_llseek,
441 .read = hung_up_tty_read,
442 .write = hung_up_tty_write,
443 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700444 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700445 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 .release = tty_release,
Peter Hurleyf5574742016-01-09 21:45:10 -0800447 .fasync = hung_up_tty_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448};
449
450static DEFINE_SPINLOCK(redirect_lock);
451static struct file *redirect;
452
453/**
454 * tty_wakeup - request more data
455 * @tty: terminal
456 *
457 * Internal and external helper for wakeups of tty. This function
458 * informs the line discipline if present that the driver is ready
459 * to receive more output data.
460 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462void tty_wakeup(struct tty_struct *tty)
463{
464 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -0800465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
467 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800468 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100469 if (ld->ops->write_wakeup)
470 ld->ops->write_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 tty_ldisc_deref(ld);
472 }
473 }
Davide Libenzi4b194492009-03-31 15:24:24 -0700474 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477EXPORT_SYMBOL_GPL(tty_wakeup);
478
479/**
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200480 * __tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +0000481 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -0700482 *
Kevin Cernekeeef4f5272012-12-26 20:43:41 -0800483 * This can be called by a "kworker" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -0700484 * synchronous but doesn't hold any locks, so we need to make sure we
485 * have the appropriate locks for what we're doing.
486 *
487 * The hangup event clears any pending redirections onto the hung up
488 * device. It ensures future writes will error and it does the needed
489 * line discipline hangup and signal delivery. The tty object itself
490 * remains intact.
491 *
492 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200493 * BTM
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800494 * redirect lock for undoing redirection
495 * file list lock for manipulating list of ttys
Peter Hurley137084b2013-06-15 07:04:46 -0400496 * tty_ldiscs_lock from called functions
Peter Hurley6a1c0682013-06-15 09:14:23 -0400497 * termios_rwsem resetting termios data
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800498 * tasklist_lock to walk task list for hangup event
499 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
Peter Hurleyf91e2592013-03-06 07:20:56 -0500501static void __tty_hangup(struct tty_struct *tty, int exit_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Alan Cox37bdfb02008-02-08 04:18:47 -0800503 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct file *filp, *f = NULL;
Nick Piggind996b622010-08-18 04:37:36 +1000505 struct tty_file_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int closecount = 0, n;
Peter Hurleyea648a42013-03-06 07:20:53 -0500507 int refs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (!tty)
510 return;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 spin_lock(&redirect_lock);
Nick Piggind996b622010-08-18 04:37:36 +1000514 if (redirect && file_tty(redirect) == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 f = redirect;
516 redirect = NULL;
517 }
518 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800519
Alan Cox89c8d912012-08-08 16:30:13 +0100520 tty_lock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200521
Peter Hurleycb50e522013-07-31 14:05:45 -0400522 if (test_bit(TTY_HUPPED, &tty->flags)) {
523 tty_unlock(tty);
524 return;
525 }
526
Arnd Bergmannec79d602010-06-01 22:53:01 +0200527 /* inuse_filps is protected by the single tty lock,
528 this really needs to change if we want to flush the
529 workqueue with the lock held */
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200530 check_tty_count(tty, "tty_hangup");
Alan Cox36ba7822009-11-30 13:18:51 +0000531
Peter Hurley4a510962016-01-09 21:35:23 -0800532 spin_lock(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Nick Piggind996b622010-08-18 04:37:36 +1000534 list_for_each_entry(priv, &tty->tty_files, list) {
535 filp = priv->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (filp->f_op->write == redirected_tty_write)
537 cons_filp = filp;
538 if (filp->f_op->write != tty_write)
539 continue;
540 closecount++;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200541 __tty_fasync(-1, filp, 0); /* can't block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 filp->f_op = &hung_up_tty_fops;
543 }
Peter Hurley4a510962016-01-09 21:35:23 -0800544 spin_unlock(&tty->files_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800545
Peter Hurley25fdf242013-03-06 07:20:57 -0500546 refs = tty_signal_session_leader(tty, exit_session);
547 /* Account for the p->signal references we killed */
548 while (refs--)
549 tty_kref_put(tty);
550
Peter Hurley892d1fa2016-01-10 22:41:06 -0800551 tty_ldisc_hangup(tty, cons_filp != NULL);
Alan Cox37bdfb02008-02-08 04:18:47 -0800552
Peter Hurley20cc2252013-03-06 07:20:54 -0500553 spin_lock_irq(&tty->ctrl_lock);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100554 clear_bit(TTY_THROTTLED, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100555 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -0600556 put_pid(tty->session);
557 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800558 tty->session = NULL;
559 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 tty->ctrl_status = 0;
Peter Hurley20cc2252013-03-06 07:20:54 -0500561 spin_unlock_irq(&tty->ctrl_lock);
Alan Cox9c9f4de2008-10-13 10:37:26 +0100562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /*
Alan Cox37bdfb02008-02-08 04:18:47 -0800564 * If one of the devices matches a console pointer, we
565 * cannot just call hangup() because that will cause
566 * tty->count and state->count to go out of sync.
567 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
569 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -0700570 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -0700572 tty->ops->close(tty, cons_filp);
573 } else if (tty->ops->hangup)
Peter Hurley7c6d3402014-06-16 09:17:08 -0400574 tty->ops->hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800575 /*
Peter Hurley892d1fa2016-01-10 22:41:06 -0800576 * We don't want to have driver/ldisc interactions beyond the ones
577 * we did here. The driver layer expects no calls after ->hangup()
578 * from the ldisc side, which is now guaranteed.
Alan Cox37bdfb02008-02-08 04:18:47 -0800579 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 set_bit(TTY_HUPPED, &tty->flags);
Alan Cox89c8d912012-08-08 16:30:13 +0100581 tty_unlock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (f)
584 fput(f);
585}
586
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200587static void do_tty_hangup(struct work_struct *work)
588{
589 struct tty_struct *tty =
590 container_of(work, struct tty_struct, hangup_work);
591
Peter Hurleyf91e2592013-03-06 07:20:56 -0500592 __tty_hangup(tty, 0);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200593}
594
Alan Coxaf9b8972006-08-27 01:24:01 -0700595/**
596 * tty_hangup - trigger a hangup event
597 * @tty: tty to hangup
598 *
599 * A carrier loss (virtual or otherwise) has occurred on this like
600 * schedule a hangup sequence to run after this event.
601 */
602
Alan Cox37bdfb02008-02-08 04:18:47 -0800603void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Peter Hurleyd435cef2015-11-08 13:01:19 -0500605 tty_debug_hangup(tty, "hangup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 schedule_work(&tty->hangup_work);
607}
608
609EXPORT_SYMBOL(tty_hangup);
610
Alan Coxaf9b8972006-08-27 01:24:01 -0700611/**
612 * tty_vhangup - process vhangup
613 * @tty: tty to hangup
614 *
615 * The user has asked via system call for the terminal to be hung up.
616 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200617 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -0700618 */
619
Alan Cox37bdfb02008-02-08 04:18:47 -0800620void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Peter Hurleyd435cef2015-11-08 13:01:19 -0500622 tty_debug_hangup(tty, "vhangup\n");
Peter Hurleyf91e2592013-03-06 07:20:56 -0500623 __tty_hangup(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Alan Cox37bdfb02008-02-08 04:18:47 -0800625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626EXPORT_SYMBOL(tty_vhangup);
627
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200628
Alan Coxaf9b8972006-08-27 01:24:01 -0700629/**
Alan Cox2cb59982008-10-13 10:40:30 +0100630 * tty_vhangup_self - process vhangup for own ctty
631 *
632 * Perform a vhangup on the current controlling tty
633 */
634
635void tty_vhangup_self(void)
636{
637 struct tty_struct *tty;
638
Alan Cox2cb59982008-10-13 10:40:30 +0100639 tty = get_current_tty();
640 if (tty) {
641 tty_vhangup(tty);
642 tty_kref_put(tty);
643 }
Alan Cox2cb59982008-10-13 10:40:30 +0100644}
645
646/**
Peter Hurleyf91e2592013-03-06 07:20:56 -0500647 * tty_vhangup_session - hangup session leader exit
648 * @tty: tty to hangup
649 *
650 * The session leader is exiting and hanging up its controlling terminal.
651 * Every process in the foreground process group is signalled SIGHUP.
652 *
653 * We do this synchronously so that when the syscall returns the process
654 * is complete. That guarantee is necessary for security reasons.
655 */
656
Nicolas Pitrea1235b32017-04-12 18:37:16 -0400657void tty_vhangup_session(struct tty_struct *tty)
Peter Hurleyf91e2592013-03-06 07:20:56 -0500658{
Peter Hurleyd435cef2015-11-08 13:01:19 -0500659 tty_debug_hangup(tty, "session hangup\n");
Peter Hurleyf91e2592013-03-06 07:20:56 -0500660 __tty_hangup(tty, 1);
661}
662
663/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700664 * tty_hung_up_p - was tty hung up
665 * @filp: file pointer of tty
666 *
667 * Return true if the tty has been subject to a vhangup or a carrier
668 * loss
669 */
670
Alan Cox37bdfb02008-02-08 04:18:47 -0800671int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Alan Coxed3f0af2017-01-16 16:54:29 -0600673 return (filp && filp->f_op == &hung_up_tty_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676EXPORT_SYMBOL(tty_hung_up_p);
677
Alan Coxaf9b8972006-08-27 01:24:01 -0700678/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200679 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700680 * @tty: tty to stop
681 *
Peter Hurley01adc802014-09-10 15:06:32 -0400682 * Perform flow control to the driver. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -0700683 * on an already stopped device and will not re-call the driver
684 * method.
685 *
686 * This functionality is used by both the line disciplines for
687 * halting incoming flow and by the driver. It may therefore be
688 * called from any context, may be under the tty atomic_write_lock
689 * but not always.
690 *
691 * Locking:
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400692 * flow_lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700693 */
694
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400695void __stop_tty(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400697 if (tty->stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return;
699 tty->stopped = 1;
Alan Coxf34d7a52008-04-30 00:54:13 -0700700 if (tty->ops->stop)
Peter Hurleyc961bfb2014-11-05 12:26:25 -0500701 tty->ops->stop(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400704void stop_tty(struct tty_struct *tty)
705{
706 unsigned long flags;
707
708 spin_lock_irqsave(&tty->flow_lock, flags);
709 __stop_tty(tty);
710 spin_unlock_irqrestore(&tty->flow_lock, flags);
711}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712EXPORT_SYMBOL(stop_tty);
713
Alan Coxaf9b8972006-08-27 01:24:01 -0700714/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200715 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700716 * @tty: tty to start
717 *
Peter Hurley01adc802014-09-10 15:06:32 -0400718 * Start a tty that has been stopped if at all possible. If this
719 * tty was previous stopped and is now being started, the driver
720 * start method is invoked and the line discipline woken.
Alan Coxaf9b8972006-08-27 01:24:01 -0700721 *
722 * Locking:
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400723 * flow_lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700724 */
725
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400726void __start_tty(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400728 if (!tty->stopped || tty->flow_stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return;
730 tty->stopped = 0;
Alan Coxf34d7a52008-04-30 00:54:13 -0700731 if (tty->ops->start)
Peter Hurleyc961bfb2014-11-05 12:26:25 -0500732 tty->ops->start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400736void start_tty(struct tty_struct *tty)
737{
738 unsigned long flags;
739
740 spin_lock_irqsave(&tty->flow_lock, flags);
741 __start_tty(tty);
742 spin_unlock_irqrestore(&tty->flow_lock, flags);
743}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744EXPORT_SYMBOL(start_tty);
745
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200746static void tty_update_time(struct timespec *time)
747{
Jiri Slabyf0bf0bd2015-02-27 18:40:31 +0100748 unsigned long sec = get_seconds();
Greg Kroah-Hartmanfbf47632015-03-26 23:10:27 +0100749
750 /*
751 * We only care if the two values differ in anything other than the
752 * lower three bits (i.e every 8 seconds). If so, then we can update
753 * the time of the tty device, otherwise it could be construded as a
754 * security leak to let userspace know the exact timing of the tty.
755 */
756 if ((sec ^ time->tv_sec) & ~7)
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200757 time->tv_sec = sec;
758}
759
Alan Coxaf9b8972006-08-27 01:24:01 -0700760/**
761 * tty_read - read method for tty device files
762 * @file: pointer to tty file
763 * @buf: user buffer
764 * @count: size of user buffer
765 * @ppos: unused
766 *
767 * Perform the read system call function on this terminal device. Checks
768 * for hung up devices before calling the line discipline method.
769 *
770 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -0700771 * Locks the line discipline internally while needed. Multiple
772 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -0700773 */
774
Alan Cox37bdfb02008-02-08 04:18:47 -0800775static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 loff_t *ppos)
777{
778 int i;
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200779 struct inode *inode = file_inode(file);
Nick Piggind996b622010-08-18 04:37:36 +1000780 struct tty_struct *tty = file_tty(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct tty_ldisc *ld;
782
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200783 if (tty_paranoia_check(tty, inode, "tty_read"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return -EIO;
Peter Hurley18900ca2016-04-09 17:06:48 -0700785 if (!tty || tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -EIO;
787
788 /* We want to wait for the line discipline to sort out in this
789 situation */
790 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -0800791 if (!ld)
792 return hung_up_tty_read(file, buf, count, ppos);
Alan Coxa352def2008-07-16 21:53:12 +0100793 if (ld->ops->read)
Peter Hurleyc961bfb2014-11-05 12:26:25 -0500794 i = ld->ops->read(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 else
796 i = -EIO;
797 tty_ldisc_deref(ld);
Jiri Slabyb0de59b2013-02-15 15:25:05 +0100798
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200799 if (i > 0)
800 tty_update_time(&inode->i_atime);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return i;
803}
804
Peter Hurley136d5252014-09-10 15:06:34 -0400805static void tty_write_unlock(struct tty_struct *tty)
Alan Cox9c1729d2007-07-15 23:39:43 -0700806{
807 mutex_unlock(&tty->atomic_write_lock);
Davide Libenzi4b194492009-03-31 15:24:24 -0700808 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Alan Cox9c1729d2007-07-15 23:39:43 -0700809}
810
Peter Hurley136d5252014-09-10 15:06:34 -0400811static int tty_write_lock(struct tty_struct *tty, int ndelay)
Alan Cox9c1729d2007-07-15 23:39:43 -0700812{
813 if (!mutex_trylock(&tty->atomic_write_lock)) {
814 if (ndelay)
815 return -EAGAIN;
816 if (mutex_lock_interruptible(&tty->atomic_write_lock))
817 return -ERESTARTSYS;
818 }
819 return 0;
820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/*
823 * Split writes up in sane blocksizes to avoid
824 * denial-of-service type attacks
825 */
826static inline ssize_t do_tty_write(
827 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
828 struct tty_struct *tty,
829 struct file *file,
830 const char __user *buf,
831 size_t count)
832{
Alan Cox9c1729d2007-07-15 23:39:43 -0700833 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -0800835
Alan Cox9c1729d2007-07-15 23:39:43 -0700836 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
837 if (ret < 0)
838 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /*
841 * We chunk up writes into a temporary buffer. This
842 * simplifies low-level drivers immensely, since they
843 * don't have locking issues and user mode accesses.
844 *
845 * But if TTY_NO_WRITE_SPLIT is set, we should use a
846 * big chunk-size..
847 *
848 * The default chunk-size is 2kB, because the NTTY
849 * layer has problems with bigger chunks. It will
850 * claim to be able to handle more characters than
851 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -0700852 *
853 * FIXME: This can probably go away now except that 64K chunks
854 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
856 chunk = 2048;
857 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
858 chunk = 65536;
859 if (count < chunk)
860 chunk = count;
861
Ingo Molnar70522e12006-03-23 03:00:31 -0800862 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (tty->write_cnt < chunk) {
Jason Wessel402fda92008-10-13 10:45:36 +0100864 unsigned char *buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 if (chunk < 1024)
867 chunk = 1024;
868
Jason Wessel402fda92008-10-13 10:45:36 +0100869 buf_chunk = kmalloc(chunk, GFP_KERNEL);
870 if (!buf_chunk) {
Alan Cox9c1729d2007-07-15 23:39:43 -0700871 ret = -ENOMEM;
872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874 kfree(tty->write_buf);
875 tty->write_cnt = chunk;
Jason Wessel402fda92008-10-13 10:45:36 +0100876 tty->write_buf = buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878
879 /* Do the write .. */
880 for (;;) {
881 size_t size = count;
882 if (size > chunk)
883 size = chunk;
884 ret = -EFAULT;
885 if (copy_from_user(tty->write_buf, buf, size))
886 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (ret <= 0)
889 break;
890 written += ret;
891 buf += ret;
892 count -= ret;
893 if (!count)
894 break;
895 ret = -ERESTARTSYS;
896 if (signal_pending(current))
897 break;
898 cond_resched();
899 }
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200900 if (written) {
901 tty_update_time(&file_inode(file)->i_mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 ret = written;
Jiri Slaby37b7f3c2013-04-26 13:48:53 +0200903 }
Alan Cox9c1729d2007-07-15 23:39:43 -0700904out:
905 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return ret;
907}
908
Alan Cox95f9bfc2008-10-13 10:39:23 +0100909/**
910 * tty_write_message - write a message to a certain tty, not just the console.
911 * @tty: the destination tty_struct
912 * @msg: the message to write
913 *
914 * This is used for messages that need to be redirected to a specific tty.
915 * We don't put it into the syslog queue right now maybe in the future if
916 * really needed.
917 *
Arnd Bergmannec79d602010-06-01 22:53:01 +0200918 * We must still hold the BTM and test the CLOSING flag for the moment.
Alan Cox95f9bfc2008-10-13 10:39:23 +0100919 */
920
921void tty_write_message(struct tty_struct *tty, char *msg)
922{
Alan Cox95f9bfc2008-10-13 10:39:23 +0100923 if (tty) {
924 mutex_lock(&tty->atomic_write_lock);
Alan Cox89c8d912012-08-08 16:30:13 +0100925 tty_lock(tty);
Peter Hurley4b41b952015-10-10 20:28:44 -0400926 if (tty->ops->write && tty->count > 0)
Alan Cox95f9bfc2008-10-13 10:39:23 +0100927 tty->ops->write(tty, msg, strlen(msg));
Peter Hurley4b41b952015-10-10 20:28:44 -0400928 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +0100929 tty_write_unlock(tty);
930 }
Alan Cox95f9bfc2008-10-13 10:39:23 +0100931 return;
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Alan Coxaf9b8972006-08-27 01:24:01 -0700935/**
936 * tty_write - write method for tty device file
937 * @file: tty file pointer
938 * @buf: user data to write
939 * @count: bytes to write
940 * @ppos: unused
941 *
942 * Write data to a tty device via the line discipline.
943 *
944 * Locking:
945 * Locks the line discipline as required
946 * Writes to the tty driver are serialized by the atomic_write_lock
947 * and are then processed in chunks to the device. The line discipline
Joe Petersona88a69c2009-01-02 13:40:53 +0000948 * write method will not be invoked in parallel for each device.
Alan Coxaf9b8972006-08-27 01:24:01 -0700949 */
950
Alan Cox37bdfb02008-02-08 04:18:47 -0800951static ssize_t tty_write(struct file *file, const char __user *buf,
952 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Nick Piggind996b622010-08-18 04:37:36 +1000954 struct tty_struct *tty = file_tty(file);
955 struct tty_ldisc *ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 ssize_t ret;
Alan Cox37bdfb02008-02-08 04:18:47 -0800957
Al Viro6131ffa2013-02-27 16:59:05 -0500958 if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return -EIO;
Peter Hurley18900ca2016-04-09 17:06:48 -0700960 if (!tty || !tty->ops->write || tty_io_error(tty))
Alan Cox37bdfb02008-02-08 04:18:47 -0800961 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -0700962 /* Short term debug to catch buggy drivers */
963 if (tty->ops->write_room == NULL)
Peter Hurley339f36b2015-11-08 13:01:13 -0500964 tty_err(tty, "missing write_room method\n");
Alan Cox37bdfb02008-02-08 04:18:47 -0800965 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -0800966 if (!ld)
967 return hung_up_tty_write(file, buf, count, ppos);
Alan Coxa352def2008-07-16 21:53:12 +0100968 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 ret = -EIO;
970 else
Alan Coxa352def2008-07-16 21:53:12 +0100971 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 tty_ldisc_deref(ld);
973 return ret;
974}
975
Alan Cox37bdfb02008-02-08 04:18:47 -0800976ssize_t redirected_tty_write(struct file *file, const char __user *buf,
977 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct file *p = NULL;
980
981 spin_lock(&redirect_lock);
Al Virocb0942b2012-08-27 14:48:26 -0400982 if (redirect)
983 p = get_file(redirect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 spin_unlock(&redirect_lock);
985
986 if (p) {
987 ssize_t res;
988 res = vfs_write(p, buf, count, &p->f_pos);
989 fput(p);
990 return res;
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return tty_write(file, buf, count, ppos);
993}
994
Peter Hurley136d5252014-09-10 15:06:34 -0400995/**
996 * tty_send_xchar - send priority character
997 *
998 * Send a high priority character to the tty even if stopped
999 *
1000 * Locking: none for xchar method, write ordering for write method.
1001 */
1002
1003int tty_send_xchar(struct tty_struct *tty, char ch)
1004{
1005 int was_stopped = tty->stopped;
1006
1007 if (tty->ops->send_xchar) {
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001008 down_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001009 tty->ops->send_xchar(tty, ch);
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001010 up_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001011 return 0;
1012 }
1013
1014 if (tty_write_lock(tty, 0) < 0)
1015 return -ERESTARTSYS;
1016
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001017 down_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001018 if (was_stopped)
1019 start_tty(tty);
1020 tty->ops->write(tty, &ch, 1);
1021 if (was_stopped)
1022 stop_tty(tty);
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001023 up_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001024 tty_write_unlock(tty);
1025 return 0;
1026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028static char ptychar[] = "pqrstuvwxyzabcde";
1029
Alan Coxaf9b8972006-08-27 01:24:01 -07001030/**
1031 * pty_line_name - generate name for a pty
1032 * @driver: the tty driver in use
1033 * @index: the minor number
1034 * @p: output buffer of at least 6 bytes
1035 *
1036 * Generate a name from a driver reference and write it to the output
1037 * buffer.
1038 *
1039 * Locking: None
1040 */
1041static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 int i = index + driver->name_base;
1044 /* ->name is initialized to "ttyp", but "tty" is expected */
1045 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08001046 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1047 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Alan Coxaf9b8972006-08-27 01:24:01 -07001050/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001051 * tty_line_name - generate name for a tty
Alan Coxaf9b8972006-08-27 01:24:01 -07001052 * @driver: the tty driver in use
1053 * @index: the minor number
1054 * @p: output buffer of at least 7 bytes
1055 *
1056 * Generate a name from a driver reference and write it to the output
Greg Kroah-Hartman5c0a2452014-02-22 14:31:04 -08001057 * buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07001058 *
1059 * Locking: None
1060 */
Hannes Reinecke723abd82014-02-27 12:30:51 +01001061static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Jiri Slaby0019b402012-08-08 22:26:43 +02001063 if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
Hannes Reinecke723abd82014-02-27 12:30:51 +01001064 return sprintf(p, "%s", driver->name);
Jiri Slaby0019b402012-08-08 22:26:43 +02001065 else
Hannes Reinecke723abd82014-02-27 12:30:51 +01001066 return sprintf(p, "%s%d", driver->name,
1067 index + driver->name_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Alan Cox99f1fe12008-10-13 10:42:00 +01001070/**
1071 * tty_driver_lookup_tty() - find an existing tty, if any
1072 * @driver: the driver for the tty
1073 * @idx: the minor number
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001074 *
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001075 * Return the tty, if found. If not found, return NULL or ERR_PTR() if the
1076 * driver lookup() method returns an error.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001077 *
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001078 * Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001079 */
Jason Wessela47d5452009-01-02 13:43:04 +00001080static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
Linus Torvalds8ead9dd2016-04-25 20:04:08 -07001081 struct file *file, int idx)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001082{
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001083 struct tty_struct *tty;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001084
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001085 if (driver->ops->lookup)
Okash Khawaja12e84c72017-05-15 18:45:32 +01001086 if (!file)
1087 tty = ERR_PTR(-EIO);
1088 else
1089 tty = driver->ops->lookup(driver, file, idx);
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001090 else
1091 tty = driver->ttys[idx];
1092
1093 if (!IS_ERR(tty))
1094 tty_kref_get(tty);
1095 return tty;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001096}
1097
Alan Cox99f1fe12008-10-13 10:42:00 +01001098/**
Alan Coxbf970ee2008-10-13 10:42:39 +01001099 * tty_init_termios - helper for termios setup
1100 * @tty: the tty to set up
1101 *
1102 * Initialise the termios structures for this tty. Thus runs under
1103 * the tty_mutex currently so we can be relaxed about ordering.
1104 */
1105
Peter Hurleya3123fd2016-01-09 21:13:48 -08001106void tty_init_termios(struct tty_struct *tty)
Alan Coxbf970ee2008-10-13 10:42:39 +01001107{
Alan Coxfe6e29f2008-10-13 10:44:08 +01001108 struct ktermios *tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001109 int idx = tty->index;
1110
Alan Cox36b3c072012-07-17 17:06:57 +01001111 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1112 tty->termios = tty->driver->init_termios;
1113 else {
1114 /* Check for lazy saved data */
1115 tp = tty->driver->termios[idx];
Peter Hurleyece53402016-01-10 22:40:57 -08001116 if (tp != NULL) {
Alan Cox36b3c072012-07-17 17:06:57 +01001117 tty->termios = *tp;
Peter Hurleyece53402016-01-10 22:40:57 -08001118 tty->termios.c_line = tty->driver->init_termios.c_line;
1119 } else
Alan Cox36b3c072012-07-17 17:06:57 +01001120 tty->termios = tty->driver->init_termios;
Alan Coxbf970ee2008-10-13 10:42:39 +01001121 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001122 /* Compatibility until drivers always set this */
Alan Coxadc8d742012-07-14 15:31:47 +01001123 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1124 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001125}
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001126EXPORT_SYMBOL_GPL(tty_init_termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001127
Jiri Slaby66d450e2012-01-30 21:14:28 +01001128int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1129{
Peter Hurleya3123fd2016-01-09 21:13:48 -08001130 tty_init_termios(tty);
Jiri Slaby66d450e2012-01-30 21:14:28 +01001131 tty_driver_kref_get(driver);
1132 tty->count++;
1133 driver->ttys[tty->index] = tty;
1134 return 0;
1135}
1136EXPORT_SYMBOL_GPL(tty_standard_install);
1137
Alan Coxbf970ee2008-10-13 10:42:39 +01001138/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001139 * tty_driver_install_tty() - install a tty entry in the driver
1140 * @driver: the driver for the tty
1141 * @tty: the tty
1142 *
1143 * Install a tty object into the driver tables. The tty->index field
Alan Coxbf970ee2008-10-13 10:42:39 +01001144 * will be set by the time this is called. This method is responsible
1145 * for ensuring any need additional structures are allocated and
1146 * configured.
Alan Cox8b0a88d2008-10-13 10:42:19 +01001147 *
1148 * Locking: tty_mutex for now
1149 */
1150static int tty_driver_install_tty(struct tty_driver *driver,
1151 struct tty_struct *tty)
1152{
Jiri Slaby66d450e2012-01-30 21:14:28 +01001153 return driver->ops->install ? driver->ops->install(driver, tty) :
1154 tty_standard_install(driver, tty);
Alan Cox8b0a88d2008-10-13 10:42:19 +01001155}
1156
1157/**
1158 * tty_driver_remove_tty() - remove a tty from the driver tables
1159 * @driver: the driver for the tty
1160 * @idx: the minor number
1161 *
1162 * Remvoe a tty object from the driver tables. The tty->index field
1163 * will be set by the time this is called.
1164 *
1165 * Locking: tty_mutex for now
1166 */
Peter Hurley05de87ed2016-01-09 21:13:49 -08001167static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +01001168{
1169 if (driver->ops->remove)
1170 driver->ops->remove(driver, tty);
1171 else
1172 driver->ttys[tty->index] = NULL;
1173}
1174
1175/*
1176 * tty_reopen() - fast re-open of an open tty
1177 * @tty - the tty to open
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001178 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001179 * Return 0 on success, -errno on error.
Peter Hurley5d93e742014-11-05 12:12:47 -05001180 * Re-opens on master ptys are not allowed and return -EIO.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001181 *
Peter Hurley5d93e742014-11-05 12:12:47 -05001182 * Locking: Caller must hold tty_lock
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001183 */
Alan Cox99f1fe12008-10-13 10:42:00 +01001184static int tty_reopen(struct tty_struct *tty)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001185{
1186 struct tty_driver *driver = tty->driver;
1187
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001188 if (driver->type == TTY_DRIVER_TYPE_PTY &&
Peter Hurley5d93e742014-11-05 12:12:47 -05001189 driver->subtype == PTY_TYPE_MASTER)
1190 return -EIO;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001191
Peter Hurley7f22f6c2016-01-09 21:13:45 -08001192 if (!tty->count)
1193 return -EAGAIN;
1194
Peter Hurley86f2c002014-12-30 10:39:25 -05001195 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1196 return -EBUSY;
1197
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001198 tty->count++;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001199
Peter Hurley892d1fa2016-01-10 22:41:06 -08001200 if (!tty->ldisc)
1201 return tty_ldisc_reinit(tty, tty->termios.c_line);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001202
1203 return 0;
1204}
1205
Alan Coxaf9b8972006-08-27 01:24:01 -07001206/**
Alan Coxd81ed102008-10-13 10:41:42 +01001207 * tty_init_dev - initialise a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001208 * @driver: tty driver we are opening a device on
1209 * @idx: device index
Alan Cox15582d32008-10-13 10:41:03 +01001210 * @ret_tty: returned tty structure
Alan Coxaf9b8972006-08-27 01:24:01 -07001211 *
1212 * Prepare a tty device. This may not be a "new" clean device but
1213 * could also be an active device. The pty drivers require special
1214 * handling because of this.
1215 *
1216 * Locking:
1217 * The function is called under the tty_mutex, which
1218 * protects us from the tty struct or driver itself going away.
1219 *
1220 * On exit the tty device has the line discipline attached and
1221 * a reference count of 1. If a pair was created for pty/tty use
1222 * and the other was a pty master then it too has a reference count of 1.
1223 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001225 * failed open. The new code protects the open with a mutex, so it's
1226 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 * relaxed for the (most common) case of reopening a tty.
1228 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001229
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001230struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Alan Coxbf970ee2008-10-13 10:42:39 +01001232 struct tty_struct *tty;
Alan Cox73ec06f2008-10-13 10:42:29 +01001233 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /*
1236 * First time open is complex, especially for PTY devices.
1237 * This code guarantees that either everything succeeds and the
1238 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001239 * and the allocated memory released. (Except that the termios
Johan Hovold16b00ae2017-03-30 15:39:35 +02001240 * may be retained.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 */
1242
Alan Cox73ec06f2008-10-13 10:42:29 +01001243 if (!try_module_get(driver->owner))
1244 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02001246 tty = alloc_tty_struct(driver, idx);
Jiri Slabyd5543502011-03-23 10:48:32 +01001247 if (!tty) {
1248 retval = -ENOMEM;
1249 goto err_module_put;
1250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Alan Cox89c8d912012-08-08 16:30:13 +01001252 tty_lock(tty);
Alan Cox73ec06f2008-10-13 10:42:29 +01001253 retval = tty_driver_install_tty(driver, tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001254 if (retval < 0)
Peter Hurleyc8b710b2016-01-09 21:13:46 -08001255 goto err_free_tty;
Alan Cox8b0a88d2008-10-13 10:42:19 +01001256
Jiri Slaby04831dc2012-06-04 13:35:36 +02001257 if (!tty->port)
1258 tty->port = driver->ports[idx];
1259
Jiri Slaby5d4121c2012-08-17 14:27:52 +02001260 WARN_RATELIMIT(!tty->port,
1261 "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
1262 __func__, tty->driver->name);
1263
Jiri Slaby967fab62012-10-18 22:26:46 +02001264 tty->port->itty = tty;
1265
Alan Cox37bdfb02008-02-08 04:18:47 -08001266 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001268 * If we fail here just call release_tty to clean up. No need
1269 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 */
Alan Coxbf970ee2008-10-13 10:42:39 +01001271 retval = tty_ldisc_setup(tty, tty->link);
Alan Cox01e1abb2008-07-22 11:16:55 +01001272 if (retval)
Jiri Slabyd5543502011-03-23 10:48:32 +01001273 goto err_release_tty;
Alan Cox89c8d912012-08-08 16:30:13 +01001274 /* Return the tty locked so that it cannot vanish under the caller */
Alan Cox73ec06f2008-10-13 10:42:29 +01001275 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Peter Hurleyc8b710b2016-01-09 21:13:46 -08001277err_free_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001278 tty_unlock(tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001279 free_tty_struct(tty);
1280err_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 module_put(driver->owner);
Jiri Slabyd5543502011-03-23 10:48:32 +01001282 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001284 /* call the tty release_tty routine to clean out this slot */
Jiri Slabyd5543502011-03-23 10:48:32 +01001285err_release_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001286 tty_unlock(tty);
Peter Hurley339f36b2015-11-08 13:01:13 -05001287 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n",
1288 retval, idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001289 release_tty(tty, idx);
Alan Cox73ec06f2008-10-13 10:42:29 +01001290 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Peter Hurleya3123fd2016-01-09 21:13:48 -08001293static void tty_free_termios(struct tty_struct *tty)
Alan Coxfeebed62008-10-13 10:41:30 +01001294{
1295 struct ktermios *tp;
1296 int idx = tty->index;
Alan Cox36b3c072012-07-17 17:06:57 +01001297
1298 /* If the port is going to reset then it has no termios to save */
1299 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1300 return;
1301
1302 /* Stash the termios data */
1303 tp = tty->driver->termios[idx];
1304 if (tp == NULL) {
1305 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
Peter Hurley25080652015-11-08 13:01:11 -05001306 if (tp == NULL)
Alan Cox36b3c072012-07-17 17:06:57 +01001307 return;
Dan Carpenter4ac5d702012-07-24 12:51:52 +01001308 tty->driver->termios[idx] = tp;
Alan Coxfeebed62008-10-13 10:41:30 +01001309 }
Alan Cox36b3c072012-07-17 17:06:57 +01001310 *tp = tty->termios;
Alan Coxfeebed62008-10-13 10:41:30 +01001311}
Alan Coxfeebed62008-10-13 10:41:30 +01001312
Peter Hurleya2965b72013-03-11 16:44:35 -04001313/**
Peter Hurley949aa642014-11-05 12:12:57 -05001314 * tty_flush_works - flush all works of a tty/pty pair
1315 * @tty: tty device to flush works for (or either end of a pty pair)
Peter Hurleya2965b72013-03-11 16:44:35 -04001316 *
Peter Hurley949aa642014-11-05 12:12:57 -05001317 * Sync flush all works belonging to @tty (and the 'other' tty).
Peter Hurleya2965b72013-03-11 16:44:35 -04001318 */
1319static void tty_flush_works(struct tty_struct *tty)
1320{
1321 flush_work(&tty->SAK_work);
1322 flush_work(&tty->hangup_work);
Peter Hurley949aa642014-11-05 12:12:57 -05001323 if (tty->link) {
1324 flush_work(&tty->link->SAK_work);
1325 flush_work(&tty->link->hangup_work);
1326 }
Peter Hurleya2965b72013-03-11 16:44:35 -04001327}
Alan Coxfeebed62008-10-13 10:41:30 +01001328
Alan Coxaf9b8972006-08-27 01:24:01 -07001329/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001330 * release_one_tty - release tty structure memory
Alan Cox9c9f4de2008-10-13 10:37:26 +01001331 * @kref: kref of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001332 *
1333 * Releases memory associated with a tty structure, and clears out the
1334 * driver table slots. This function is called when a device is no longer
1335 * in use. It also gets called when setup of a device fails.
1336 *
1337 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -07001338 * takes the file list lock internally when working on the list
1339 * of ttys that the driver keeps.
Alan Coxb50989d2009-09-19 13:13:22 -07001340 *
1341 * This method gets called from a work queue so that the driver private
Dave Youngf278a2f2009-09-27 16:00:42 +00001342 * cleanup ops can sleep (needed for USB at least)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
Alan Coxb50989d2009-09-19 13:13:22 -07001344static void release_one_tty(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Alan Coxb50989d2009-09-19 13:13:22 -07001346 struct tty_struct *tty =
1347 container_of(work, struct tty_struct, hangup_work);
Alan Cox6f967f72008-10-13 10:37:36 +01001348 struct tty_driver *driver = tty->driver;
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001349 struct module *owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Dave Youngf278a2f2009-09-27 16:00:42 +00001351 if (tty->ops->cleanup)
1352 tty->ops->cleanup(tty);
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 tty->magic = 0;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001355 tty_driver_kref_put(driver);
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001356 module_put(owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001357
Peter Hurley4a510962016-01-09 21:35:23 -08001358 spin_lock(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 list_del_init(&tty->tty_files);
Peter Hurley4a510962016-01-09 21:35:23 -08001360 spin_unlock(&tty->files_lock);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001361
Oleg Nesterov6da8d862010-04-02 18:05:12 +02001362 put_pid(tty->pgrp);
1363 put_pid(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 free_tty_struct(tty);
1365}
1366
Alan Coxb50989d2009-09-19 13:13:22 -07001367static void queue_release_one_tty(struct kref *kref)
1368{
1369 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Dave Youngf278a2f2009-09-27 16:00:42 +00001370
Alan Coxb50989d2009-09-19 13:13:22 -07001371 /* The hangup queue is now free so we can reuse it rather than
1372 waste a chunk of memory for each port */
1373 INIT_WORK(&tty->hangup_work, release_one_tty);
1374 schedule_work(&tty->hangup_work);
1375}
1376
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001377/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001378 * tty_kref_put - release a tty kref
1379 * @tty: tty device
1380 *
1381 * Release a reference to a tty device and if need be let the kref
1382 * layer destruct the object for us
1383 */
1384
1385void tty_kref_put(struct tty_struct *tty)
1386{
1387 if (tty)
Alan Coxb50989d2009-09-19 13:13:22 -07001388 kref_put(&tty->kref, queue_release_one_tty);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001389}
1390EXPORT_SYMBOL(tty_kref_put);
1391
1392/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001393 * release_tty - release tty structure memory
1394 *
1395 * Release both @tty and a possible linked partner (think pty pair),
1396 * and decrement the refcount of the backing module.
1397 *
1398 * Locking:
Alan Coxd1552552012-07-27 18:02:54 +01001399 * tty_mutex
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001400 * takes the file list lock internally when working on the list
1401 * of ttys that the driver keeps.
Alan Cox9c9f4de2008-10-13 10:37:26 +01001402 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001403 */
1404static void release_tty(struct tty_struct *tty, int idx)
1405{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001406 /* This should always be true but check for the moment */
1407 WARN_ON(tty->index != idx);
Alan Coxd1552552012-07-27 18:02:54 +01001408 WARN_ON(!mutex_is_locked(&tty_mutex));
Alan Cox36b3c072012-07-17 17:06:57 +01001409 if (tty->ops->shutdown)
1410 tty->ops->shutdown(tty);
1411 tty_free_termios(tty);
1412 tty_driver_remove_tty(tty->driver, tty);
Jiri Slaby967fab62012-10-18 22:26:46 +02001413 tty->port->itty = NULL;
Peter Hurley64e377d2013-06-15 09:01:00 -04001414 if (tty->link)
1415 tty->link->port->itty = NULL;
Peter Hurleye1760582015-10-17 16:36:23 -04001416 tty_buffer_cancel_work(tty->port);
Alan Cox36b3c072012-07-17 17:06:57 +01001417
Markus Elfringa211b1a2014-11-21 13:42:29 +01001418 tty_kref_put(tty->link);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001419 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001420}
1421
Alan Coxeeb89d92009-11-30 13:18:29 +00001422/**
Jiri Slaby955787ca2011-11-11 10:47:23 +01001423 * tty_release_checks - check a tty before real release
1424 * @tty: tty to check
1425 * @o_tty: link of @tty (if any)
1426 * @idx: index of the tty
1427 *
1428 * Performs some paranoid checking before true release of the @tty.
1429 * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1430 */
Peter Hurley359b9fb2014-11-05 12:12:59 -05001431static int tty_release_checks(struct tty_struct *tty, int idx)
Jiri Slaby955787ca2011-11-11 10:47:23 +01001432{
1433#ifdef TTY_PARANOIA_CHECK
1434 if (idx < 0 || idx >= tty->driver->num) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001435 tty_debug(tty, "bad idx %d\n", idx);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001436 return -1;
1437 }
1438
1439 /* not much to check for devpts */
1440 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1441 return 0;
1442
1443 if (tty != tty->driver->ttys[idx]) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001444 tty_debug(tty, "bad driver table[%d] = %p\n",
1445 idx, tty->driver->ttys[idx]);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001446 return -1;
1447 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001448 if (tty->driver->other) {
Peter Hurley359b9fb2014-11-05 12:12:59 -05001449 struct tty_struct *o_tty = tty->link;
1450
Jiri Slaby955787ca2011-11-11 10:47:23 +01001451 if (o_tty != tty->driver->other->ttys[idx]) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001452 tty_debug(tty, "bad other table[%d] = %p\n",
1453 idx, tty->driver->other->ttys[idx]);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001454 return -1;
1455 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001456 if (o_tty->link != tty) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001457 tty_debug(tty, "bad link = %p\n", o_tty->link);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001458 return -1;
1459 }
1460 }
1461#endif
1462 return 0;
1463}
1464
1465/**
Rob Herring9ed90d22017-01-16 16:54:28 -06001466 * tty_release_struct - release a tty struct
1467 * @tty: tty device
1468 * @idx: index of the tty
1469 *
1470 * Performs the final steps to release and free a tty device. It is
1471 * roughly the reverse of tty_init_dev.
1472 */
1473void tty_release_struct(struct tty_struct *tty, int idx)
1474{
1475 /*
1476 * Ask the line discipline code to release its structures
1477 */
1478 tty_ldisc_release(tty);
1479
1480 /* Wait for pending work before tty destruction commmences */
1481 tty_flush_works(tty);
1482
1483 tty_debug_hangup(tty, "freeing structure\n");
1484 /*
1485 * The release_tty function takes care of the details of clearing
1486 * the slots and preserving the termios structure. The tty_unlock_pair
1487 * should be safe as we keep a kref while the tty is locked (so the
1488 * unlock never unlocks a freed tty).
1489 */
1490 mutex_lock(&tty_mutex);
1491 release_tty(tty, idx);
1492 mutex_unlock(&tty_mutex);
1493}
1494EXPORT_SYMBOL_GPL(tty_release_struct);
1495
1496/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001497 * tty_release - vfs callback for close
1498 * @inode: inode of tty
1499 * @filp: file pointer for handle to tty
1500 *
1501 * Called the last time each file handle is closed that references
1502 * this tty. There may however be several such references.
1503 *
1504 * Locking:
1505 * Takes bkl. See tty_release_dev
1506 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 * Even releasing the tty structures is a tricky business.. We have
1508 * to be very careful that the structures are all released at the
1509 * same time, as interrupts might otherwise get the wrong pointers.
1510 *
1511 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1512 * lead to double frees or releasing memory still in use.
1513 */
Alan Coxeeb89d92009-11-30 13:18:29 +00001514
1515int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Nick Piggind996b622010-08-18 04:37:36 +10001517 struct tty_struct *tty = file_tty(filp);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001518 struct tty_struct *o_tty = NULL;
1519 int do_sleep, final;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int idx;
Peter Hurley37b16452014-10-16 13:51:30 -04001521 long timeout = 0;
Peter Hurley494c1ea2014-10-16 13:54:36 -04001522 int once = 1;
Alan Cox37bdfb02008-02-08 04:18:47 -08001523
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001524 if (tty_paranoia_check(tty, inode, __func__))
Alan Coxeeb89d92009-11-30 13:18:29 +00001525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Alan Cox89c8d912012-08-08 16:30:13 +01001527 tty_lock(tty);
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001528 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Arnd Bergmannec79d602010-06-01 22:53:01 +02001530 __tty_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 idx = tty->index;
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001533 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1534 tty->driver->subtype == PTY_TYPE_MASTER)
1535 o_tty = tty->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Peter Hurley359b9fb2014-11-05 12:12:59 -05001537 if (tty_release_checks(tty, idx)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001538 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Peter Hurleyd435cef2015-11-08 13:01:19 -05001542 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Alan Coxf34d7a52008-04-30 00:54:13 -07001544 if (tty->ops->close)
1545 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Peter Hurley2aff5e22014-11-05 12:13:01 -05001547 /* If tty is pty master, lock the slave pty (stable lock order) */
1548 tty_lock_slave(o_tty);
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 /*
1551 * Sanity check: if tty->count is going to zero, there shouldn't be
1552 * any waiters on tty->read_wait or tty->write_wait. We test the
1553 * wait queues and kick everyone out _before_ actually starting to
1554 * close. This ensures that we won't block while releasing the tty
1555 * structure.
1556 *
1557 * The test for the o_tty closing is necessary, since the master and
1558 * slave sides may close in any order. If the slave side closes out
1559 * first, its count will be one, since the master side holds an open.
Peter Hurley324c1652014-11-05 12:12:56 -05001560 * Thus this test wouldn't be triggered at the time the slave closed,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 * so we do it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 */
1563 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 do_sleep = 0;
1565
Peter Hurley324c1652014-11-05 12:12:56 -05001566 if (tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (waitqueue_active(&tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001568 wake_up_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 do_sleep++;
1570 }
1571 if (waitqueue_active(&tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001572 wake_up_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 do_sleep++;
1574 }
1575 }
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001576 if (o_tty && o_tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (waitqueue_active(&o_tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001578 wake_up_poll(&o_tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 do_sleep++;
1580 }
1581 if (waitqueue_active(&o_tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001582 wake_up_poll(&o_tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 do_sleep++;
1584 }
1585 }
1586 if (!do_sleep)
1587 break;
1588
Peter Hurley494c1ea2014-10-16 13:54:36 -04001589 if (once) {
1590 once = 0;
Peter Hurley339f36b2015-11-08 13:01:13 -05001591 tty_warn(tty, "read/write wait queue active!\n");
Peter Hurley494c1ea2014-10-16 13:54:36 -04001592 }
Peter Hurley37b16452014-10-16 13:51:30 -04001593 schedule_timeout_killable(timeout);
1594 if (timeout < 120 * HZ)
1595 timeout = 2 * timeout + 1;
1596 else
1597 timeout = MAX_SCHEDULE_TIMEOUT;
Alan Cox37bdfb02008-02-08 04:18:47 -08001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001600 if (o_tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (--o_tty->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001602 tty_warn(tty, "bad slave count (%d)\n", o_tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 o_tty->count = 0;
1604 }
1605 }
1606 if (--tty->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001607 tty_warn(tty, "bad tty->count (%d)\n", tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 tty->count = 0;
1609 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 /*
1612 * We've decremented tty->count, so we need to remove this file
1613 * descriptor off the tty->tty_files list; this serves two
1614 * purposes:
1615 * - check_tty_count sees the correct number of file descriptors
1616 * associated with this tty.
1617 * - do_tty_hangup no longer sees this file descriptor as
1618 * something that needs to be handled for hangups.
1619 */
Nick Piggind996b622010-08-18 04:37:36 +10001620 tty_del_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 /*
1623 * Perform some housekeeping before deciding whether to return.
1624 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * If _either_ side is closing, make sure there aren't any
1626 * processes that still think tty or o_tty is their controlling
1627 * tty.
1628 */
Peter Hurley324c1652014-11-05 12:12:56 -05001629 if (!tty->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001631 session_clear_tty(tty->session);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001632 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001633 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 read_unlock(&tasklist_lock);
1635 }
1636
Peter Hurley324c1652014-11-05 12:12:56 -05001637 /* check whether both sides are closing ... */
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001638 final = !tty->count && !(o_tty && o_tty->count);
Peter Hurley324c1652014-11-05 12:12:56 -05001639
Peter Hurley2aff5e22014-11-05 12:13:01 -05001640 tty_unlock_slave(o_tty);
1641 tty_unlock(tty);
1642
Peter Hurley04980702014-11-05 12:12:52 -05001643 /* At this point, the tty->count == 0 should ensure a dead tty
Alan Coxd1552552012-07-27 18:02:54 +01001644 cannot be re-opened by a racing opener */
Paul Fulghumda965822006-02-14 13:53:00 -08001645
Peter Hurley324c1652014-11-05 12:12:56 -05001646 if (!final)
Alan Coxeeb89d92009-11-30 13:18:29 +00001647 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001648
Peter Hurleyaccff792015-07-12 22:49:09 -04001649 tty_debug_hangup(tty, "final close\n");
Peter Hurleya2965b72013-03-11 16:44:35 -04001650
Rob Herring9ed90d22017-01-16 16:54:28 -06001651 tty_release_struct(tty, idx);
Alan Coxeeb89d92009-11-30 13:18:29 +00001652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
Alan Coxaf9b8972006-08-27 01:24:01 -07001655/**
Peter Hurley52494ee2014-11-05 12:12:50 -05001656 * tty_open_current_tty - get locked tty of current task
Jiri Slabyb82154a2011-11-09 21:33:19 +01001657 * @device: device number
1658 * @filp: file pointer to tty
Peter Hurley52494ee2014-11-05 12:12:50 -05001659 * @return: locked tty of the current task iff @device is /dev/tty
1660 *
1661 * Performs a re-open of the current task's controlling tty.
Jiri Slabyb82154a2011-11-09 21:33:19 +01001662 *
1663 * We cannot return driver and index like for the other nodes because
1664 * devpts will not work then. It expects inodes to be from devpts FS.
1665 */
1666static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1667{
1668 struct tty_struct *tty;
Peter Hurley52494ee2014-11-05 12:12:50 -05001669 int retval;
Jiri Slabyb82154a2011-11-09 21:33:19 +01001670
1671 if (device != MKDEV(TTYAUX_MAJOR, 0))
1672 return NULL;
1673
1674 tty = get_current_tty();
1675 if (!tty)
1676 return ERR_PTR(-ENXIO);
1677
1678 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1679 /* noctty = 1; */
Peter Hurley52494ee2014-11-05 12:12:50 -05001680 tty_lock(tty);
1681 tty_kref_put(tty); /* safe to drop the kref now */
1682
1683 retval = tty_reopen(tty);
1684 if (retval < 0) {
1685 tty_unlock(tty);
1686 tty = ERR_PTR(retval);
1687 }
Jiri Slabyb82154a2011-11-09 21:33:19 +01001688 return tty;
1689}
1690
1691/**
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001692 * tty_lookup_driver - lookup a tty driver for a given device file
1693 * @device: device number
1694 * @filp: file pointer to tty
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001695 * @index: index for the device in the @return driver
1696 * @return: driver for this inode (with increased refcount)
1697 *
1698 * If @return is not erroneous, the caller is responsible to decrement the
1699 * refcount by tty_driver_kref_put.
1700 *
1701 * Locking: tty_mutex protects get_tty_driver
1702 */
1703static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
Peter Hurley11e1d4a2016-01-09 21:13:52 -08001704 int *index)
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001705{
1706 struct tty_driver *driver;
1707
Jiri Slaby2cd00502011-11-09 21:33:22 +01001708 switch (device) {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001709#ifdef CONFIG_VT
Jiri Slaby2cd00502011-11-09 21:33:22 +01001710 case MKDEV(TTY_MAJOR, 0): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001711 extern struct tty_driver *console_driver;
1712 driver = tty_driver_kref_get(console_driver);
1713 *index = fg_console;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001714 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001715 }
1716#endif
Jiri Slaby2cd00502011-11-09 21:33:22 +01001717 case MKDEV(TTYAUX_MAJOR, 1): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001718 struct tty_driver *console_driver = console_device(index);
1719 if (console_driver) {
1720 driver = tty_driver_kref_get(console_driver);
Okash Khawaja12e84c72017-05-15 18:45:32 +01001721 if (driver && filp) {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001722 /* Don't let /dev/console block */
1723 filp->f_flags |= O_NONBLOCK;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001724 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001725 }
1726 }
1727 return ERR_PTR(-ENODEV);
1728 }
Jiri Slaby2cd00502011-11-09 21:33:22 +01001729 default:
1730 driver = get_tty_driver(device, index);
1731 if (!driver)
1732 return ERR_PTR(-ENODEV);
1733 break;
1734 }
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001735 return driver;
1736}
1737
1738/**
Peter Hurleyd6203d02016-01-09 21:13:53 -08001739 * tty_open_by_driver - open a tty device
1740 * @device: dev_t of device to open
1741 * @inode: inode of device file
1742 * @filp: file pointer to tty
1743 *
1744 * Performs the driver lookup, checks for a reopen, or otherwise
1745 * performs the first-time tty initialization.
1746 *
1747 * Returns the locked initialized or re-opened &tty_struct
1748 *
1749 * Claims the global tty_mutex to serialize:
1750 * - concurrent first-time tty initialization
1751 * - concurrent tty driver removal w/ lookup
1752 * - concurrent tty removal from driver table
1753 */
Okash Khawaja12e84c72017-05-15 18:45:32 +01001754struct tty_struct *tty_open_by_driver(dev_t device, struct inode *inode,
Peter Hurleyd6203d02016-01-09 21:13:53 -08001755 struct file *filp)
1756{
1757 struct tty_struct *tty;
1758 struct tty_driver *driver = NULL;
1759 int index = -1;
1760 int retval;
1761
1762 mutex_lock(&tty_mutex);
1763 driver = tty_lookup_driver(device, filp, &index);
1764 if (IS_ERR(driver)) {
1765 mutex_unlock(&tty_mutex);
1766 return ERR_CAST(driver);
1767 }
1768
1769 /* check whether we're reopening an existing tty */
Linus Torvalds8ead9dd2016-04-25 20:04:08 -07001770 tty = tty_driver_lookup_tty(driver, filp, index);
Peter Hurleyd6203d02016-01-09 21:13:53 -08001771 if (IS_ERR(tty)) {
1772 mutex_unlock(&tty_mutex);
1773 goto out;
1774 }
1775
1776 if (tty) {
1777 mutex_unlock(&tty_mutex);
1778 retval = tty_lock_interruptible(tty);
Peter Hurley5e00bbf2016-03-31 17:47:07 -07001779 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */
Peter Hurleyd6203d02016-01-09 21:13:53 -08001780 if (retval) {
1781 if (retval == -EINTR)
1782 retval = -ERESTARTSYS;
1783 tty = ERR_PTR(retval);
1784 goto out;
1785 }
Peter Hurleyd6203d02016-01-09 21:13:53 -08001786 retval = tty_reopen(tty);
1787 if (retval < 0) {
1788 tty_unlock(tty);
1789 tty = ERR_PTR(retval);
1790 }
1791 } else { /* Returns with the tty_lock held for now */
1792 tty = tty_init_dev(driver, index);
1793 mutex_unlock(&tty_mutex);
1794 }
1795out:
1796 tty_driver_kref_put(driver);
1797 return tty;
1798}
Okash Khawaja12e84c72017-05-15 18:45:32 +01001799EXPORT_SYMBOL_GPL(tty_open_by_driver);
Peter Hurleyd6203d02016-01-09 21:13:53 -08001800
1801/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001802 * tty_open - open a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001803 * @inode: inode of device file
1804 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001806 * tty_open and tty_release keep up the tty count that contains the
1807 * number of opens done on a tty. We cannot use the inode-count, as
1808 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001810 * Open-counting is needed for pty masters, as well as for keeping
1811 * track of serial lines: DTR is dropped when the last close happens.
1812 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1813 *
1814 * The termios state of a pty is reset on first open so that
1815 * settings don't persist across reuse.
1816 *
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001817 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001818 * tty->count should protect the rest.
1819 * ->siglock protects ->signal/->sighand
Alan Cox89c8d912012-08-08 16:30:13 +01001820 *
1821 * Note: the tty_unlock/lock cases without a ref are only safe due to
1822 * tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001824
Alan Coxeeb89d92009-11-30 13:18:29 +00001825static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Jiri Slabyb82154a2011-11-09 21:33:19 +01001827 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 int noctty, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 dev_t device = inode->i_rdev;
Andrew Morton846c1512009-04-02 16:56:36 -07001830 unsigned saved_flags = filp->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834retry_open:
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02001835 retval = tty_alloc_file(filp);
1836 if (retval)
1837 return -ENOMEM;
1838
Jiri Slabyb82154a2011-11-09 21:33:19 +01001839 tty = tty_open_current_tty(device, filp);
Peter Hurleyd6203d02016-01-09 21:13:53 -08001840 if (!tty)
1841 tty = tty_open_by_driver(device, inode, filp);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001842
Alan Coxeeb89d92009-11-30 13:18:29 +00001843 if (IS_ERR(tty)) {
Peter Hurleyd6203d02016-01-09 21:13:53 -08001844 tty_free_file(filp);
Jiri Slabyba5db442011-11-09 21:33:21 +01001845 retval = PTR_ERR(tty);
Peter Hurley7f22f6c2016-01-09 21:13:45 -08001846 if (retval != -EAGAIN || signal_pending(current))
Peter Hurleyd6203d02016-01-09 21:13:53 -08001847 return retval;
Peter Hurley7f22f6c2016-01-09 21:13:45 -08001848 schedule();
1849 goto retry_open;
Alan Coxeeb89d92009-11-30 13:18:29 +00001850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02001852 tty_add_file(tty, filp);
Nick Piggind996b622010-08-18 04:37:36 +10001853
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001854 check_tty_count(tty, __func__);
Peter Hurleyd435cef2015-11-08 13:01:19 -05001855 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
Peter Hurleyaccff792015-07-12 22:49:09 -04001856
Herton Ronaldo Krzesinski909bc772011-03-31 15:35:31 -03001857 if (tty->ops->open)
1858 retval = tty->ops->open(tty, filp);
1859 else
1860 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 filp->f_flags = saved_flags;
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (retval) {
Peter Hurleyd435cef2015-11-08 13:01:19 -05001864 tty_debug_hangup(tty, "open error %d, releasing\n", retval);
Peter Hurleyaccff792015-07-12 22:49:09 -04001865
Alan Cox89c8d912012-08-08 16:30:13 +01001866 tty_unlock(tty); /* need to call tty_release without BTM */
Alan Coxeeb89d92009-11-30 13:18:29 +00001867 tty_release(inode, filp);
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +02001868 if (retval != -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return retval;
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +02001870
1871 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return retval;
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +02001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 schedule();
1875 /*
1876 * Need to reset f_op in case a hangup happened.
1877 */
Peter Hurley12569372014-11-05 12:26:24 -05001878 if (tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 filp->f_op = &tty_fops;
1880 goto retry_open;
1881 }
Peter Hurleyd4855e12013-11-19 08:46:27 -05001882 clear_bit(TTY_HUPPED, &tty->flags);
Alan Coxeeb89d92009-11-30 13:18:29 +00001883
Peter Hurley11e1d4a2016-01-09 21:13:52 -08001884 noctty = (filp->f_flags & O_NOCTTY) ||
Nicolas Pitrea1235b32017-04-12 18:37:16 -04001885 (IS_ENABLED(CONFIG_VT) && device == MKDEV(TTY_MAJOR, 0)) ||
1886 device == MKDEV(TTYAUX_MAJOR, 1) ||
1887 (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1888 tty->driver->subtype == PTY_TYPE_MASTER);
1889 if (!noctty)
1890 tty_open_proc_set_tty(filp, tty);
Alan Cox89c8d912012-08-08 16:30:13 +01001891 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893}
1894
Jonathan Corbet39d95b92008-05-16 09:10:50 -06001895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Alan Coxaf9b8972006-08-27 01:24:01 -07001897/**
1898 * tty_poll - check tty status
1899 * @filp: file being polled
1900 * @wait: poll wait structures to update
1901 *
1902 * Call the line discipline polling method to obtain the poll
1903 * status of the device.
1904 *
1905 * Locking: locks called line discipline but ldisc poll method
1906 * may be re-entered freely by other callers.
1907 */
1908
Alan Cox37bdfb02008-02-08 04:18:47 -08001909static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Nick Piggind996b622010-08-18 04:37:36 +10001911 struct tty_struct *tty = file_tty(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 struct tty_ldisc *ld;
1913 int ret = 0;
1914
Al Viro6131ffa2013-02-27 16:59:05 -05001915 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08001919 if (!ld)
1920 return hung_up_tty_poll(filp, wait);
Alan Coxa352def2008-07-16 21:53:12 +01001921 if (ld->ops->poll)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05001922 ret = ld->ops->poll(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 tty_ldisc_deref(ld);
1924 return ret;
1925}
1926
Arnd Bergmannec79d602010-06-01 22:53:01 +02001927static int __tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Nick Piggind996b622010-08-18 04:37:36 +10001929 struct tty_struct *tty = file_tty(filp);
Alan Cox47f86832008-04-30 00:53:30 -07001930 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001931 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Al Viro6131ffa2013-02-27 16:59:05 -05001933 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001934 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 retval = fasync_helper(fd, filp, on, &tty->fasync);
1937 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001938 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001941 enum pid_type type;
1942 struct pid *pid;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001943
Alan Cox47f86832008-04-30 00:53:30 -07001944 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001945 if (tty->pgrp) {
1946 pid = tty->pgrp;
1947 type = PIDTYPE_PGID;
1948 } else {
1949 pid = task_pid(current);
1950 type = PIDTYPE_PID;
1951 }
Linus Torvalds80e1e822010-02-07 10:11:23 -08001952 get_pid(pid);
Greg Kroah-Hartman70362512009-12-17 07:07:19 -08001953 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001954 __f_setown(filp, pid, type, 0);
Linus Torvalds80e1e822010-02-07 10:11:23 -08001955 put_pid(pid);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001956 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001958out:
Arnd Bergmannec79d602010-06-01 22:53:01 +02001959 return retval;
1960}
1961
1962static int tty_fasync(int fd, struct file *filp, int on)
1963{
Alan Cox89c8d912012-08-08 16:30:13 +01001964 struct tty_struct *tty = file_tty(filp);
Peter Hurleya8f3a292016-01-09 21:45:11 -08001965 int retval = -ENOTTY;
Alan Cox89c8d912012-08-08 16:30:13 +01001966
1967 tty_lock(tty);
Peter Hurleya8f3a292016-01-09 21:45:11 -08001968 if (!tty_hung_up_p(filp))
1969 retval = __tty_fasync(fd, filp, on);
Alan Cox89c8d912012-08-08 16:30:13 +01001970 tty_unlock(tty);
1971
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001972 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Alan Coxaf9b8972006-08-27 01:24:01 -07001975/**
1976 * tiocsti - fake input character
1977 * @tty: tty to fake input into
1978 * @p: pointer to character
1979 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001980 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07001981 * input management.
1982 *
1983 * FIXME: does not honour flow control ??
1984 *
1985 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -04001986 * Called functions take tty_ldiscs_lock
Alan Coxaf9b8972006-08-27 01:24:01 -07001987 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07001988 *
1989 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07001990 */
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992static int tiocsti(struct tty_struct *tty, char __user *p)
1993{
1994 char ch, mbz = 0;
1995 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08001996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
1998 return -EPERM;
1999 if (get_user(ch, p))
2000 return -EFAULT;
Al Viro1e641742008-12-09 09:23:33 +00002001 tty_audit_tiocsti(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002003 if (!ld)
2004 return -EIO;
Alan Coxa352def2008-07-16 21:53:12 +01002005 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 tty_ldisc_deref(ld);
2007 return 0;
2008}
2009
Alan Coxaf9b8972006-08-27 01:24:01 -07002010/**
2011 * tiocgwinsz - implement window query ioctl
2012 * @tty; tty
2013 * @arg: user buffer for result
2014 *
Alan Cox808a0d32006-09-29 02:00:40 -07002015 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002016 *
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002017 * Locking: tty->winsize_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002018 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002019 */
2020
Alan Cox37bdfb02008-02-08 04:18:47 -08002021static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Alan Cox808a0d32006-09-29 02:00:40 -07002023 int err;
2024
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002025 mutex_lock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002026 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002027 mutex_unlock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002028
2029 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Alan Coxaf9b8972006-08-27 01:24:01 -07002032/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002033 * tty_do_resize - resize event
2034 * @tty: tty being resized
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002035 * @rows: rows (character)
2036 * @cols: cols (character)
Alan Coxaf9b8972006-08-27 01:24:01 -07002037 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002038 * Update the termios variables and send the necessary signals to
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002039 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002040 */
2041
Alan Coxfc6f6232009-01-02 13:43:17 +00002042int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Alan Coxfc6f6232009-01-02 13:43:17 +00002044 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Alan Coxfc6f6232009-01-02 13:43:17 +00002046 /* Lock the tty */
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002047 mutex_lock(&tty->winsize_mutex);
Alan Coxfc6f6232009-01-02 13:43:17 +00002048 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002049 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002050
Peter Hurley5b239542014-10-16 14:59:45 -04002051 /* Signal the foreground process group */
2052 pgrp = tty_get_pgrp(tty);
Alan Cox47f86832008-04-30 00:53:30 -07002053 if (pgrp)
2054 kill_pgrp(pgrp, SIGWINCH, 1);
Alan Cox47f86832008-04-30 00:53:30 -07002055 put_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002056
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002057 tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002058done:
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002059 mutex_unlock(&tty->winsize_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return 0;
2061}
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01002062EXPORT_SYMBOL(tty_do_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Alan Coxaf9b8972006-08-27 01:24:01 -07002064/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002065 * tiocswinsz - implement window size set ioctl
Alan Coxfc6f6232009-01-02 13:43:17 +00002066 * @tty; tty side of tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002067 * @arg: user buffer for result
2068 *
2069 * Copies the user idea of the window size to the kernel. Traditionally
2070 * this is just advisory information but for the Linux console it
2071 * actually has driver level meaning and triggers a VC resize.
2072 *
2073 * Locking:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002074 * Driver dependent. The default do_resize method takes the
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002075 * tty termios mutex and ctrl_lock. The console takes its own lock
2076 * then calls into the default method.
2077 */
2078
Alan Coxfc6f6232009-01-02 13:43:17 +00002079static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002080{
2081 struct winsize tmp_ws;
2082 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2083 return -EFAULT;
2084
2085 if (tty->ops->resize)
Alan Coxfc6f6232009-01-02 13:43:17 +00002086 return tty->ops->resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002087 else
Alan Coxfc6f6232009-01-02 13:43:17 +00002088 return tty_do_resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002089}
2090
2091/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002092 * tioccons - allow admin to move logical console
2093 * @file: the file to become console
2094 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002095 * Allow the administrator to move the redirected console device
Alan Coxaf9b8972006-08-27 01:24:01 -07002096 *
2097 * Locking: uses redirect_lock to guard the redirect information
2098 */
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100static int tioccons(struct file *file)
2101{
2102 if (!capable(CAP_SYS_ADMIN))
2103 return -EPERM;
2104 if (file->f_op->write == redirected_tty_write) {
2105 struct file *f;
2106 spin_lock(&redirect_lock);
2107 f = redirect;
2108 redirect = NULL;
2109 spin_unlock(&redirect_lock);
2110 if (f)
2111 fput(f);
2112 return 0;
2113 }
2114 spin_lock(&redirect_lock);
2115 if (redirect) {
2116 spin_unlock(&redirect_lock);
2117 return -EBUSY;
2118 }
Al Virocb0942b2012-08-27 14:48:26 -04002119 redirect = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 spin_unlock(&redirect_lock);
2121 return 0;
2122}
2123
Alan Coxaf9b8972006-08-27 01:24:01 -07002124/**
2125 * fionbio - non blocking ioctl
2126 * @file: file to set blocking value
2127 * @p: user parameter
2128 *
2129 * Historical tty interfaces had a blocking control ioctl before
2130 * the generic functionality existed. This piece of history is preserved
2131 * in the expected tty API of posix OS's.
2132 *
Alan Cox6146b9a2009-09-19 13:13:19 -07002133 * Locking: none, the open file handle ensures it won't go away.
Alan Coxaf9b8972006-08-27 01:24:01 -07002134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136static int fionbio(struct file *file, int __user *p)
2137{
2138 int nonblock;
2139
2140 if (get_user(nonblock, p))
2141 return -EFAULT;
2142
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002143 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (nonblock)
2145 file->f_flags |= O_NONBLOCK;
2146 else
2147 file->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002148 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return 0;
2150}
2151
Alan Coxaf9b8972006-08-27 01:24:01 -07002152/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002153 * tiocsetd - set line discipline
2154 * @tty: tty device
2155 * @p: pointer to user data
2156 *
2157 * Set the line discipline according to user request.
2158 *
2159 * Locking: see tty_set_ldisc, this function is just a helper
2160 */
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162static int tiocsetd(struct tty_struct *tty, int __user *p)
2163{
Peter Hurleyc12da962016-01-10 22:41:04 -08002164 int disc;
Alan Cox04f378b2008-04-30 00:53:29 -07002165 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Peter Hurleyc12da962016-01-10 22:41:04 -08002167 if (get_user(disc, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002169
Peter Hurleyc12da962016-01-10 22:41:04 -08002170 ret = tty_set_ldisc(tty, disc);
Alan Cox04f378b2008-04-30 00:53:29 -07002171
2172 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
Alan Coxaf9b8972006-08-27 01:24:01 -07002175/**
Peter Hurley5c17c862016-01-10 22:40:55 -08002176 * tiocgetd - get line discipline
2177 * @tty: tty device
2178 * @p: pointer to user data
2179 *
2180 * Retrieves the line discipline id directly from the ldisc.
2181 *
2182 * Locking: waits for ldisc reference (in case the line discipline
2183 * is changing or the tty is being hungup)
2184 */
2185
2186static int tiocgetd(struct tty_struct *tty, int __user *p)
2187{
2188 struct tty_ldisc *ld;
2189 int ret;
2190
2191 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002192 if (!ld)
2193 return -EIO;
Peter Hurley5c17c862016-01-10 22:40:55 -08002194 ret = put_user(ld->ops->num, p);
2195 tty_ldisc_deref(ld);
2196 return ret;
2197}
2198
2199/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002200 * send_break - performed time break
2201 * @tty: device to break on
2202 * @duration: timeout in mS
2203 *
2204 * Perform a timed break on hardware that lacks its own driver level
2205 * timed break functionality.
2206 *
2207 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002208 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002209 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002210 */
2211
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002212static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Alan Cox9e98966c2008-07-22 11:18:03 +01002214 int retval;
2215
2216 if (tty->ops->break_ctl == NULL)
2217 return 0;
2218
2219 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2220 retval = tty->ops->break_ctl(tty, duration);
2221 else {
2222 /* Do the work ourselves */
2223 if (tty_write_lock(tty, 0) < 0)
2224 return -EINTR;
2225 retval = tty->ops->break_ctl(tty, -1);
2226 if (retval)
2227 goto out;
2228 if (!signal_pending(current))
2229 msleep_interruptible(duration);
2230 retval = tty->ops->break_ctl(tty, 0);
2231out:
2232 tty_write_unlock(tty);
2233 if (signal_pending(current))
2234 retval = -EINTR;
2235 }
2236 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
Alan Coxaf9b8972006-08-27 01:24:01 -07002239/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002240 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002241 * @tty: tty device
2242 * @file: user file pointer
2243 * @p: pointer to result
2244 *
2245 * Obtain the modem status bits from the tty driver if the feature
2246 * is supported. Return -EINVAL if it is not available.
2247 *
2248 * Locking: none (up to the driver)
2249 */
2250
Alan Cox60b33c12011-02-14 16:26:14 +00002251static int tty_tiocmget(struct tty_struct *tty, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 int retval = -EINVAL;
2254
Alan Coxf34d7a52008-04-30 00:54:13 -07002255 if (tty->ops->tiocmget) {
Alan Cox60b33c12011-02-14 16:26:14 +00002256 retval = tty->ops->tiocmget(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 if (retval >= 0)
2259 retval = put_user(retval, p);
2260 }
2261 return retval;
2262}
2263
Alan Coxaf9b8972006-08-27 01:24:01 -07002264/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002265 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002266 * @tty: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002267 * @cmd: command - clear bits, set bits or set all
2268 * @p: pointer to desired bits
2269 *
2270 * Set the modem status bits from the tty driver if the feature
2271 * is supported. Return -EINVAL if it is not available.
2272 *
2273 * Locking: none (up to the driver)
2274 */
2275
Alan Cox20b9d172011-02-14 16:26:50 +00002276static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 unsigned __user *p)
2278{
Alan Coxae677512008-07-16 21:56:54 +01002279 int retval;
2280 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Alan Coxae677512008-07-16 21:56:54 +01002282 if (tty->ops->tiocmset == NULL)
2283 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Alan Coxae677512008-07-16 21:56:54 +01002285 retval = get_user(val, p);
2286 if (retval)
2287 return retval;
2288 set = clear = 0;
2289 switch (cmd) {
2290 case TIOCMBIS:
2291 set = val;
2292 break;
2293 case TIOCMBIC:
2294 clear = val;
2295 break;
2296 case TIOCMSET:
2297 set = val;
2298 clear = ~val;
2299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
Alan Coxae677512008-07-16 21:56:54 +01002301 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2302 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
Alan Cox20b9d172011-02-14 16:26:50 +00002303 return tty->ops->tiocmset(tty, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Alan Coxd281da72010-09-16 18:21:24 +01002306static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2307{
2308 int retval = -EINVAL;
2309 struct serial_icounter_struct icount;
2310 memset(&icount, 0, sizeof(icount));
2311 if (tty->ops->get_icount)
2312 retval = tty->ops->get_icount(tty, &icount);
2313 if (retval != 0)
2314 return retval;
2315 if (copy_to_user(arg, &icount, sizeof(icount)))
2316 return -EFAULT;
2317 return 0;
2318}
2319
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002320static void tty_warn_deprecated_flags(struct serial_struct __user *ss)
2321{
2322 static DEFINE_RATELIMIT_STATE(depr_flags,
2323 DEFAULT_RATELIMIT_INTERVAL,
2324 DEFAULT_RATELIMIT_BURST);
2325 char comm[TASK_COMM_LEN];
2326 int flags;
2327
2328 if (get_user(flags, &ss->flags))
2329 return;
2330
2331 flags &= ASYNC_DEPRECATED;
2332
2333 if (flags && __ratelimit(&depr_flags))
Joe Perchesd437fa92017-02-16 23:11:44 -08002334 pr_warn("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
2335 __func__, get_task_comm(comm, current), flags);
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002336}
2337
Peter Hurley8f166e02014-10-16 14:59:41 -04002338/*
2339 * if pty, return the slave side (real_tty)
2340 * otherwise, return self
2341 */
2342static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
Alan Coxe8b70e72009-06-11 12:48:02 +01002343{
2344 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2345 tty->driver->subtype == PTY_TYPE_MASTER)
2346 tty = tty->link;
2347 return tty;
2348}
Alan Coxe8b70e72009-06-11 12:48:02 +01002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350/*
2351 * Split this up, as gcc can choke on it otherwise..
2352 */
Alan Cox04f378b2008-04-30 00:53:29 -07002353long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Nick Piggind996b622010-08-18 04:37:36 +10002355 struct tty_struct *tty = file_tty(file);
2356 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 void __user *p = (void __user *)arg;
2358 int retval;
2359 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002360
Al Viro6131ffa2013-02-27 16:59:05 -05002361 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return -EINVAL;
2363
Alan Coxe8b70e72009-06-11 12:48:02 +01002364 real_tty = tty_pair_get_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 /*
2367 * Factor out some common prep work
2368 */
2369 switch (cmd) {
2370 case TIOCSETD:
2371 case TIOCSBRK:
2372 case TIOCCBRK:
2373 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002374 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 retval = tty_check_change(tty);
2376 if (retval)
2377 return retval;
2378 if (cmd != TIOCCBRK) {
2379 tty_wait_until_sent(tty, 0);
2380 if (signal_pending(current))
2381 return -EINTR;
2382 }
2383 break;
2384 }
2385
Alan Cox9e98966c2008-07-22 11:18:03 +01002386 /*
2387 * Now do the stuff.
2388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002390 case TIOCSTI:
2391 return tiocsti(tty, p);
2392 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002393 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002394 case TIOCSWINSZ:
Alan Coxfc6f6232009-01-02 13:43:17 +00002395 return tiocswinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002396 case TIOCCONS:
2397 return real_tty != tty ? -EINVAL : tioccons(file);
2398 case FIONBIO:
2399 return fionbio(file, p);
2400 case TIOCEXCL:
2401 set_bit(TTY_EXCLUSIVE, &tty->flags);
2402 return 0;
2403 case TIOCNXCL:
2404 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2405 return 0;
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +04002406 case TIOCGEXCL:
2407 {
2408 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2409 return put_user(excl, (int __user *)p);
2410 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002411 case TIOCGETD:
Peter Hurley5c17c862016-01-10 22:40:55 -08002412 return tiocgetd(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002413 case TIOCSETD:
2414 return tiocsetd(tty, p);
Kay Sievers3c95c982011-02-17 18:39:28 +01002415 case TIOCVHANGUP:
2416 if (!capable(CAP_SYS_ADMIN))
2417 return -EPERM;
2418 tty_vhangup(tty);
2419 return 0;
Werner Finkb7b8de02010-12-03 12:48:23 +01002420 case TIOCGDEV:
2421 {
2422 unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2423 return put_user(ret, (unsigned int __user *)p);
2424 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002425 /*
2426 * Break handling
2427 */
2428 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002429 if (tty->ops->break_ctl)
Alan Cox9e98966c2008-07-22 11:18:03 +01002430 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002431 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002432 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002433 if (tty->ops->break_ctl)
Alan Cox9e98966c2008-07-22 11:18:03 +01002434 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002435 return 0;
2436 case TCSBRK: /* SVID version: non-zero arg --> no break */
2437 /* non-zero arg means wait for all output data
2438 * to be sent (performed above) but don't send break.
2439 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002441 if (!arg)
2442 return send_break(tty, 250);
2443 return 0;
2444 case TCSBRKP: /* support for POSIX tcsendbreak() */
2445 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Alan Cox37bdfb02008-02-08 04:18:47 -08002447 case TIOCMGET:
Alan Cox60b33c12011-02-14 16:26:14 +00002448 return tty_tiocmget(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002449 case TIOCMSET:
2450 case TIOCMBIC:
2451 case TIOCMBIS:
Alan Cox20b9d172011-02-14 16:26:50 +00002452 return tty_tiocmset(tty, cmd, p);
Alan Coxd281da72010-09-16 18:21:24 +01002453 case TIOCGICOUNT:
2454 retval = tty_tiocgicount(tty, p);
2455 /* For the moment allow fall through to the old method */
2456 if (retval != -EINVAL)
2457 return retval;
2458 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002459 case TCFLSH:
2460 switch (arg) {
2461 case TCIFLUSH:
2462 case TCIOFLUSH:
2463 /* flush tty buffer and allow ldisc to process ioctl */
Peter Hurley86c80a82014-11-05 12:13:09 -05002464 tty_buffer_flush(tty, NULL);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002465 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002466 }
2467 break;
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002468 case TIOCSSERIAL:
2469 tty_warn_deprecated_flags(p);
2470 break;
Nicolas Pitrea1235b32017-04-12 18:37:16 -04002471 default:
2472 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg);
2473 if (retval != -ENOIOCTLCMD)
2474 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002476 if (tty->ops->ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002477 retval = tty->ops->ioctl(tty, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (retval != -ENOIOCTLCMD)
2479 return retval;
2480 }
2481 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002482 if (!ld)
2483 return hung_up_tty_ioctl(file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002485 if (ld->ops->ioctl) {
2486 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (retval == -ENOIOCTLCMD)
Wanlong Gaobbb63c52012-08-27 15:23:12 +08002488 retval = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490 tty_ldisc_deref(ld);
2491 return retval;
2492}
2493
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002494#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08002495static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002496 unsigned long arg)
2497{
Nick Piggind996b622010-08-18 04:37:36 +10002498 struct tty_struct *tty = file_tty(file);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002499 struct tty_ldisc *ld;
2500 int retval = -ENOIOCTLCMD;
2501
Al Viro6131ffa2013-02-27 16:59:05 -05002502 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002503 return -EINVAL;
2504
Alan Coxf34d7a52008-04-30 00:54:13 -07002505 if (tty->ops->compat_ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002506 retval = tty->ops->compat_ioctl(tty, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002507 if (retval != -ENOIOCTLCMD)
2508 return retval;
2509 }
2510
2511 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002512 if (!ld)
2513 return hung_up_tty_compat_ioctl(file, cmd, arg);
Alan Coxa352def2008-07-16 21:53:12 +01002514 if (ld->ops->compat_ioctl)
2515 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Thomas Meyer8193c422011-10-05 23:13:13 +02002516 else
2517 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002518 tty_ldisc_deref(ld);
2519
2520 return retval;
2521}
2522#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Al Viroc3c073f2012-08-21 22:32:06 -04002524static int this_tty(const void *t, struct file *file, unsigned fd)
2525{
2526 if (likely(file->f_op->read != tty_read))
2527 return 0;
2528 return file_tty(file) != t ? 0 : fd + 1;
2529}
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531/*
2532 * This implements the "Secure Attention Key" --- the idea is to
2533 * prevent trojan horses by killing all processes associated with this
2534 * tty when the user hits the "Secure Attention Key". Required for
2535 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08002536 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 * This code could be nicer; ideally it should send a HUP, wait a few
2538 * seconds, then send a INT, and then a KILL signal. But you then
2539 * have to coordinate with the init process, since all processes associated
2540 * with the current tty must be dead before the new getty is allowed
2541 * to spawn.
2542 *
2543 * Now, if it would be correct ;-/ The current code has a nasty hole -
2544 * it doesn't catch files in flight. We may send the descriptor to ourselves
2545 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2546 *
2547 * Nasty bug: do_SAK is being called in interrupt context. This can
2548 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2549 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002550void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
2552#ifdef TTY_SOFT_SAK
2553 tty_hangup(tty);
2554#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08002555 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002556 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -08002558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (!tty)
2560 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002561 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08002562
Dan Carpenterb3f13de2006-12-13 00:35:09 -08002563 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Alan Coxf34d7a52008-04-30 00:54:13 -07002565 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08002566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002568 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002569 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05002570 tty_notice(tty, "SAK: killed process %d (%s): by session\n",
2571 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002572 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002573 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Hurley9b42bb72015-11-08 13:01:14 -05002574
2575 /* Now kill any processes that happen to have the tty open */
Eric W. Biederman652486f2006-03-28 16:11:02 -08002576 do_each_thread(g, p) {
2577 if (p->signal->tty == tty) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05002578 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
2579 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 send_sig(SIGKILL, p, 1);
2581 continue;
2582 }
2583 task_lock(p);
Al Viroc3c073f2012-08-21 22:32:06 -04002584 i = iterate_fd(p->files, 0, this_tty, tty);
2585 if (i != 0) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05002586 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n",
2587 task_pid_nr(p), p->comm, i - 1);
Al Viroc3c073f2012-08-21 22:32:06 -04002588 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002591 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 read_unlock(&tasklist_lock);
2593#endif
2594}
2595
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002596static void do_SAK_work(struct work_struct *work)
2597{
2598 struct tty_struct *tty =
2599 container_of(work, struct tty_struct, SAK_work);
2600 __do_SAK(tty);
2601}
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603/*
2604 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2605 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2606 * the values which we write to it will be identical to the values which it
2607 * already has. --akpm
2608 */
2609void do_SAK(struct tty_struct *tty)
2610{
2611 if (!tty)
2612 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 schedule_work(&tty->SAK_work);
2614}
2615
2616EXPORT_SYMBOL(do_SAK);
2617
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08002618static int dev_match_devt(struct device *dev, const void *data)
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002619{
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08002620 const dev_t *devt = data;
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002621 return dev->devt == *devt;
2622}
2623
2624/* Must put_device() after it's unused! */
2625static struct device *tty_get_device(struct tty_struct *tty)
2626{
2627 dev_t devt = tty_devnum(tty);
2628 return class_find_device(tty_class, NULL, &devt, dev_match_devt);
2629}
2630
2631
Alan Coxaf9b8972006-08-27 01:24:01 -07002632/**
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002633 * alloc_tty_struct
Alan Coxaf9b8972006-08-27 01:24:01 -07002634 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002635 * This subroutine allocates and initializes a tty structure.
Alan Coxaf9b8972006-08-27 01:24:01 -07002636 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002637 * Locking: none - tty in question is not exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002639
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002640struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002642 struct tty_struct *tty;
2643
2644 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
2645 if (!tty)
2646 return NULL;
2647
Alan Cox9c9f4de2008-10-13 10:37:26 +01002648 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 tty->magic = TTY_MAGIC;
Alan Cox01e1abb2008-07-22 11:16:55 +01002650 tty_ldisc_init(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002651 tty->session = NULL;
2652 tty->pgrp = NULL;
Alan Cox89c8d912012-08-08 16:30:13 +01002653 mutex_init(&tty->legacy_mutex);
Peter Hurleyd8c1f922013-06-15 09:14:31 -04002654 mutex_init(&tty->throttle_mutex);
Peter Hurley6a1c0682013-06-15 09:14:23 -04002655 init_rwsem(&tty->termios_rwsem);
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002656 mutex_init(&tty->winsize_mutex);
Peter Hurley36697522013-06-15 07:04:48 -04002657 init_ldsem(&tty->ldisc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 init_waitqueue_head(&tty->write_wait);
2659 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00002660 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08002661 mutex_init(&tty->atomic_write_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07002662 spin_lock_init(&tty->ctrl_lock);
Peter Hurleyf9e053d2014-09-10 15:06:31 -04002663 spin_lock_init(&tty->flow_lock);
Peter Hurley4a510962016-01-09 21:35:23 -08002664 spin_lock_init(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002666 INIT_WORK(&tty->SAK_work, do_SAK_work);
Alan Coxbf970ee2008-10-13 10:42:39 +01002667
2668 tty->driver = driver;
2669 tty->ops = driver->ops;
2670 tty->index = idx;
2671 tty_line_name(driver, idx, tty->name);
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002672 tty->dev = tty_get_device(tty);
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002673
2674 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
Alan Coxf34d7a52008-04-30 00:54:13 -07002677/**
2678 * tty_put_char - write one character to a tty
2679 * @tty: tty
2680 * @ch: character
2681 *
2682 * Write one byte to the tty using the provided put_char method
2683 * if present. Returns the number of characters successfully output.
2684 *
2685 * Note: the specific put_char operation in the driver layer may go
2686 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002688
Alan Coxf34d7a52008-04-30 00:54:13 -07002689int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
Alan Coxf34d7a52008-04-30 00:54:13 -07002691 if (tty->ops->put_char)
2692 return tty->ops->put_char(tty, ch);
2693 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
Alan Coxf34d7a52008-04-30 00:54:13 -07002695EXPORT_SYMBOL_GPL(tty_put_char);
2696
Alan Coxd81ed102008-10-13 10:41:42 +01002697struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002699static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
2700 unsigned int index, unsigned int count)
2701{
Leon Yuc1a752b2015-09-07 13:08:37 +00002702 int err;
2703
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002704 /* init here, since reused cdevs cause crashes */
Richard Wattsa3a10ce2015-05-19 16:06:53 +01002705 driver->cdevs[index] = cdev_alloc();
2706 if (!driver->cdevs[index])
2707 return -ENOMEM;
Leon Yuc1a752b2015-09-07 13:08:37 +00002708 driver->cdevs[index]->ops = &tty_fops;
Richard Wattsa3a10ce2015-05-19 16:06:53 +01002709 driver->cdevs[index]->owner = driver->owner;
Leon Yuc1a752b2015-09-07 13:08:37 +00002710 err = cdev_add(driver->cdevs[index], dev, count);
2711 if (err)
2712 kobject_put(&driver->cdevs[index]->kobj);
2713 return err;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002714}
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002717 * tty_register_device - register a tty device
2718 * @driver: the tty driver that describes the tty device
2719 * @index: the index in the tty driver for this tty device
2720 * @device: a struct device that is associated with this tty device.
2721 * This field is optional, if there is no known struct device
2722 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07002724 * Returns a pointer to the struct device for this tty device
2725 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002726 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002727 * This call is required to be made to register an individual tty device
2728 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2729 * that bit is not set, this function should not be called by a tty
2730 * driver.
2731 *
2732 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002734
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07002735struct device *tty_register_device(struct tty_driver *driver, unsigned index,
2736 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002738 return tty_register_device_attr(driver, index, device, NULL, NULL);
2739}
2740EXPORT_SYMBOL(tty_register_device);
2741
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002742static void tty_device_create_release(struct device *dev)
2743{
Peter Hurley83db1df2015-11-08 13:01:21 -05002744 dev_dbg(dev, "releasing...\n");
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002745 kfree(dev);
2746}
2747
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002748/**
2749 * tty_register_device_attr - register a tty device
2750 * @driver: the tty driver that describes the tty device
2751 * @index: the index in the tty driver for this tty device
2752 * @device: a struct device that is associated with this tty device.
2753 * This field is optional, if there is no known struct device
2754 * for this tty device it can be set to NULL safely.
2755 * @drvdata: Driver data to be set to device.
2756 * @attr_grp: Attribute group to be set on device.
2757 *
2758 * Returns a pointer to the struct device for this tty device
2759 * (or ERR_PTR(-EFOO) on error).
2760 *
2761 * This call is required to be made to register an individual tty device
2762 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2763 * that bit is not set, this function should not be called by a tty
2764 * driver.
2765 *
2766 * Locking: ??
2767 */
2768struct device *tty_register_device_attr(struct tty_driver *driver,
2769 unsigned index, struct device *device,
2770 void *drvdata,
2771 const struct attribute_group **attr_grp)
2772{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 char name[64];
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002774 dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
Johan Hovold93857ed2017-03-30 15:39:36 +02002775 struct ktermios *tp;
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002776 struct device *dev;
2777 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 if (index >= driver->num) {
Peter Hurley656fb862015-11-08 13:01:15 -05002780 pr_err("%s: Attempt to register invalid tty line number (%d)\n",
2781 driver->name, index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002782 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
2784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (driver->type == TTY_DRIVER_TYPE_PTY)
2786 pty_line_name(driver, index, name);
2787 else
2788 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002789
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002790 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002791 if (!dev)
2792 return ERR_PTR(-ENOMEM);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002793
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002794 dev->devt = devt;
2795 dev->class = tty_class;
2796 dev->parent = device;
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002797 dev->release = tty_device_create_release;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002798 dev_set_name(dev, "%s", name);
2799 dev->groups = attr_grp;
2800 dev_set_drvdata(dev, drvdata);
2801
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002802 dev_set_uevent_suppress(dev, 1);
2803
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002804 retval = device_register(dev);
2805 if (retval)
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002806 goto err_put;
2807
2808 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
Johan Hovold93857ed2017-03-30 15:39:36 +02002809 /*
2810 * Free any saved termios data so that the termios state is
2811 * reset when reusing a minor number.
2812 */
2813 tp = driver->termios[index];
2814 if (tp) {
2815 driver->termios[index] = NULL;
2816 kfree(tp);
2817 }
2818
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002819 retval = tty_cdev_add(driver, devt, index, 1);
2820 if (retval)
2821 goto err_del;
2822 }
2823
2824 dev_set_uevent_suppress(dev, 0);
2825 kobject_uevent(&dev->kobj, KOBJ_ADD);
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002826
2827 return dev;
2828
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002829err_del:
2830 device_del(dev);
2831err_put:
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002832 put_device(dev);
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002833
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002834 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002836EXPORT_SYMBOL_GPL(tty_register_device_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002839 * tty_unregister_device - unregister a tty device
2840 * @driver: the tty driver that describes the tty device
2841 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002843 * If a tty device is registered with a call to tty_register_device() then
2844 * this function must be called when the tty device is gone.
2845 *
2846 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849void tty_unregister_device(struct tty_driver *driver, unsigned index)
2850{
Alan Cox37bdfb02008-02-08 04:18:47 -08002851 device_destroy(tty_class,
2852 MKDEV(driver->major, driver->minor_start) + index);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01002853 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
2854 cdev_del(driver->cdevs[index]);
2855 driver->cdevs[index] = NULL;
2856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858EXPORT_SYMBOL(tty_unregister_device);
2859
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002860/**
2861 * __tty_alloc_driver -- allocate tty driver
2862 * @lines: count of lines this driver can handle at most
Thadeu Lima de Souza Cascardo87838ae2017-04-04 05:56:32 -03002863 * @owner: module which is responsible for this driver
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002864 * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
2865 *
2866 * This should not be called directly, some of the provided macros should be
2867 * used instead. Use IS_ERR and friends on @retval.
2868 */
2869struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
2870 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
2872 struct tty_driver *driver;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002873 unsigned int cdevs = 1;
Jiri Slaby16a02082012-08-08 22:26:42 +02002874 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Jiri Slaby0019b402012-08-08 22:26:43 +02002876 if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002877 return ERR_PTR(-EINVAL);
2878
Jean Delvare506eb992007-07-15 23:40:14 -07002879 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002880 if (!driver)
2881 return ERR_PTR(-ENOMEM);
2882
2883 kref_init(&driver->kref);
2884 driver->magic = TTY_DRIVER_MAGIC;
2885 driver->num = lines;
2886 driver->owner = owner;
2887 driver->flags = flags;
Jiri Slaby16a02082012-08-08 22:26:42 +02002888
2889 if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
2890 driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
2891 GFP_KERNEL);
2892 driver->termios = kcalloc(lines, sizeof(*driver->termios),
2893 GFP_KERNEL);
2894 if (!driver->ttys || !driver->termios) {
2895 err = -ENOMEM;
2896 goto err_free_all;
2897 }
2898 }
2899
2900 if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
2901 driver->ports = kcalloc(lines, sizeof(*driver->ports),
2902 GFP_KERNEL);
2903 if (!driver->ports) {
2904 err = -ENOMEM;
2905 goto err_free_all;
2906 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002907 cdevs = lines;
2908 }
2909
2910 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
2911 if (!driver->cdevs) {
2912 err = -ENOMEM;
2913 goto err_free_all;
Jiri Slaby16a02082012-08-08 22:26:42 +02002914 }
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return driver;
Jiri Slaby16a02082012-08-08 22:26:42 +02002917err_free_all:
2918 kfree(driver->ports);
2919 kfree(driver->ttys);
2920 kfree(driver->termios);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01002921 kfree(driver->cdevs);
Jiri Slaby16a02082012-08-08 22:26:42 +02002922 kfree(driver);
2923 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002925EXPORT_SYMBOL(__tty_alloc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Alan Cox7d7b93c2008-10-13 10:42:09 +01002927static void destruct_tty_driver(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
Alan Cox7d7b93c2008-10-13 10:42:09 +01002929 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
2930 int i;
2931 struct ktermios *tp;
Alan Cox7d7b93c2008-10-13 10:42:09 +01002932
2933 if (driver->flags & TTY_DRIVER_INSTALLED) {
Alan Cox7d7b93c2008-10-13 10:42:09 +01002934 for (i = 0; i < driver->num; i++) {
2935 tp = driver->termios[i];
2936 if (tp) {
2937 driver->termios[i] = NULL;
2938 kfree(tp);
2939 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01002940 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
2941 tty_unregister_device(driver, i);
2942 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01002943 proc_tty_unregister_driver(driver);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002944 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
Richard Wattsa3a10ce2015-05-19 16:06:53 +01002945 cdev_del(driver->cdevs[0]);
Alan Cox7d7b93c2008-10-13 10:42:09 +01002946 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002947 kfree(driver->cdevs);
Jiri Slaby04831dc2012-06-04 13:35:36 +02002948 kfree(driver->ports);
Jiri Slaby16a02082012-08-08 22:26:42 +02002949 kfree(driver->termios);
2950 kfree(driver->ttys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 kfree(driver);
2952}
2953
Alan Cox7d7b93c2008-10-13 10:42:09 +01002954void tty_driver_kref_put(struct tty_driver *driver)
2955{
2956 kref_put(&driver->kref, destruct_tty_driver);
2957}
2958EXPORT_SYMBOL(tty_driver_kref_put);
2959
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002960void tty_set_operations(struct tty_driver *driver,
2961 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Alan Coxf34d7a52008-04-30 00:54:13 -07002963 driver->ops = op;
2964};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965EXPORT_SYMBOL(tty_set_operations);
2966
Alan Cox7d7b93c2008-10-13 10:42:09 +01002967void put_tty_driver(struct tty_driver *d)
2968{
2969 tty_driver_kref_put(d);
2970}
2971EXPORT_SYMBOL(put_tty_driver);
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973/*
2974 * Called by a tty driver to register itself.
2975 */
2976int tty_register_driver(struct tty_driver *driver)
2977{
2978 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08002979 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 dev_t dev;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04002981 struct device *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002984 error = alloc_chrdev_region(&dev, driver->minor_start,
2985 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (!error) {
2987 driver->major = MAJOR(dev);
2988 driver->minor_start = MINOR(dev);
2989 }
2990 } else {
2991 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01002992 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02002994 if (error < 0)
Jiri Slaby16a02082012-08-08 22:26:42 +02002995 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002997 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
2998 error = tty_cdev_add(driver, dev, 0, driver->num);
2999 if (error)
3000 goto err_unreg_char;
3001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003003 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003005 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003006
3007 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003008 for (i = 0; i < driver->num; i++) {
3009 d = tty_register_device(driver, i, NULL);
3010 if (IS_ERR(d)) {
3011 error = PTR_ERR(d);
Jiri Slaby16a02082012-08-08 22:26:42 +02003012 goto err_unreg_devs;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003013 }
3014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
3016 proc_tty_register_driver(driver);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003017 driver->flags |= TTY_DRIVER_INSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return 0;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003019
Jiri Slaby16a02082012-08-08 22:26:42 +02003020err_unreg_devs:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003021 for (i--; i >= 0; i--)
3022 tty_unregister_device(driver, i);
3023
3024 mutex_lock(&tty_mutex);
3025 list_del(&driver->tty_drivers);
3026 mutex_unlock(&tty_mutex);
3027
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003028err_unreg_char:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003029 unregister_chrdev_region(dev, driver->num);
Jiri Slaby16a02082012-08-08 22:26:42 +02003030err:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003031 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033EXPORT_SYMBOL(tty_register_driver);
3034
3035/*
3036 * Called by a tty driver to unregister itself.
3037 */
3038int tty_unregister_driver(struct tty_driver *driver)
3039{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003040#if 0
3041 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if (driver->refcount)
3043 return -EBUSY;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003044#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3046 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003047 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003049 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 return 0;
3051}
Alan Cox7d7b93c2008-10-13 10:42:09 +01003052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053EXPORT_SYMBOL(tty_unregister_driver);
3054
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003055dev_t tty_devnum(struct tty_struct *tty)
3056{
3057 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3058}
3059EXPORT_SYMBOL(tty_devnum);
3060
Alan Coxd81ed102008-10-13 10:41:42 +01003061void tty_default_fops(struct file_operations *fops)
3062{
3063 *fops = tty_fops;
3064}
3065
Al Viro2c9ede52011-07-23 20:24:48 -04003066static char *tty_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +02003067{
3068 if (!mode)
3069 return NULL;
3070 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3071 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3072 *mode = 0666;
3073 return NULL;
3074}
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076static int __init tty_class_init(void)
3077{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003078 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 if (IS_ERR(tty_class))
3080 return PTR_ERR(tty_class);
Kay Sieverse454cea2009-09-18 23:01:12 +02003081 tty_class->devnode = tty_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 return 0;
3083}
3084
3085postcore_initcall(tty_class_init);
3086
3087/* 3/2004 jmc: why do these devices exist? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088static struct cdev tty_cdev, console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Kay Sieversfbc92a32010-12-01 18:51:05 +01003090static ssize_t show_cons_active(struct device *dev,
3091 struct device_attribute *attr, char *buf)
3092{
3093 struct console *cs[16];
3094 int i = 0;
3095 struct console *c;
3096 ssize_t count = 0;
3097
Torben Hohnac751ef2011-01-25 15:07:35 -08003098 console_lock();
Kay Sieversa2a6a822011-01-09 16:39:14 +01003099 for_each_console(c) {
Kay Sieversfbc92a32010-12-01 18:51:05 +01003100 if (!c->device)
3101 continue;
3102 if (!c->write)
3103 continue;
3104 if ((c->flags & CON_ENABLED) == 0)
3105 continue;
3106 cs[i++] = c;
3107 if (i >= ARRAY_SIZE(cs))
3108 break;
3109 }
Hannes Reinecke723abd82014-02-27 12:30:51 +01003110 while (i--) {
3111 int index = cs[i]->index;
3112 struct tty_driver *drv = cs[i]->device(cs[i], &index);
3113
3114 /* don't resolve tty0 as some programs depend on it */
3115 if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR))
3116 count += tty_line_name(drv, index, buf + count);
3117 else
3118 count += sprintf(buf + count, "%s%d",
3119 cs[i]->name, cs[i]->index);
3120
3121 count += sprintf(buf + count, "%c", i ? ' ':'\n');
3122 }
Torben Hohnac751ef2011-01-25 15:07:35 -08003123 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01003124
3125 return count;
3126}
3127static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3128
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003129static struct attribute *cons_dev_attrs[] = {
3130 &dev_attr_active.attr,
3131 NULL
3132};
3133
3134ATTRIBUTE_GROUPS(cons_dev);
3135
Kay Sieversfbc92a32010-12-01 18:51:05 +01003136static struct device *consdev;
3137
3138void console_sysfs_notify(void)
3139{
3140 if (consdev)
3141 sysfs_notify(&consdev->kobj, NULL, "active");
3142}
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144/*
3145 * Ok, now we can initialize the rest of the tty devices and can count
3146 * on memory allocations, interrupts etc..
3147 */
David Howells31d1d482010-08-06 16:34:43 +01003148int __init tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
3150 cdev_init(&tty_cdev, &tty_fops);
3151 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3152 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3153 panic("Couldn't register /dev/tty driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003154 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
3156 cdev_init(&console_cdev, &console_fops);
3157 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3158 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3159 panic("Couldn't register /dev/console driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003160 consdev = device_create_with_groups(tty_class, NULL,
3161 MKDEV(TTYAUX_MAJOR, 1), NULL,
3162 cons_dev_groups, "console");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003163 if (IS_ERR(consdev))
3164 consdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166#ifdef CONFIG_VT
Alan Coxd81ed102008-10-13 10:41:42 +01003167 vty_init(&console_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168#endif
3169 return 0;
3170}
David Howells31d1d482010-08-06 16:34:43 +01003171