blob: 0c150b5a9dd68d7bba0e91a50c45ecd5df831cac [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)
Linus Torvalds8ead9dd2016-04-25 20:04:08 -07001086 tty = driver->ops->lookup(driver, file, idx);
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001087 else
1088 tty = driver->ttys[idx];
1089
1090 if (!IS_ERR(tty))
1091 tty_kref_get(tty);
1092 return tty;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001093}
1094
Alan Cox99f1fe12008-10-13 10:42:00 +01001095/**
Alan Coxbf970ee2008-10-13 10:42:39 +01001096 * tty_init_termios - helper for termios setup
1097 * @tty: the tty to set up
1098 *
1099 * Initialise the termios structures for this tty. Thus runs under
1100 * the tty_mutex currently so we can be relaxed about ordering.
1101 */
1102
Peter Hurleya3123fd2016-01-09 21:13:48 -08001103void tty_init_termios(struct tty_struct *tty)
Alan Coxbf970ee2008-10-13 10:42:39 +01001104{
Alan Coxfe6e29f2008-10-13 10:44:08 +01001105 struct ktermios *tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001106 int idx = tty->index;
1107
Alan Cox36b3c072012-07-17 17:06:57 +01001108 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1109 tty->termios = tty->driver->init_termios;
1110 else {
1111 /* Check for lazy saved data */
1112 tp = tty->driver->termios[idx];
Peter Hurleyece53402016-01-10 22:40:57 -08001113 if (tp != NULL) {
Alan Cox36b3c072012-07-17 17:06:57 +01001114 tty->termios = *tp;
Peter Hurleyece53402016-01-10 22:40:57 -08001115 tty->termios.c_line = tty->driver->init_termios.c_line;
1116 } else
Alan Cox36b3c072012-07-17 17:06:57 +01001117 tty->termios = tty->driver->init_termios;
Alan Coxbf970ee2008-10-13 10:42:39 +01001118 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001119 /* Compatibility until drivers always set this */
Alan Coxadc8d742012-07-14 15:31:47 +01001120 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1121 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001122}
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001123EXPORT_SYMBOL_GPL(tty_init_termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001124
Jiri Slaby66d450e2012-01-30 21:14:28 +01001125int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1126{
Peter Hurleya3123fd2016-01-09 21:13:48 -08001127 tty_init_termios(tty);
Jiri Slaby66d450e2012-01-30 21:14:28 +01001128 tty_driver_kref_get(driver);
1129 tty->count++;
1130 driver->ttys[tty->index] = tty;
1131 return 0;
1132}
1133EXPORT_SYMBOL_GPL(tty_standard_install);
1134
Alan Coxbf970ee2008-10-13 10:42:39 +01001135/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001136 * tty_driver_install_tty() - install a tty entry in the driver
1137 * @driver: the driver for the tty
1138 * @tty: the tty
1139 *
1140 * Install a tty object into the driver tables. The tty->index field
Alan Coxbf970ee2008-10-13 10:42:39 +01001141 * will be set by the time this is called. This method is responsible
1142 * for ensuring any need additional structures are allocated and
1143 * configured.
Alan Cox8b0a88d2008-10-13 10:42:19 +01001144 *
1145 * Locking: tty_mutex for now
1146 */
1147static int tty_driver_install_tty(struct tty_driver *driver,
1148 struct tty_struct *tty)
1149{
Jiri Slaby66d450e2012-01-30 21:14:28 +01001150 return driver->ops->install ? driver->ops->install(driver, tty) :
1151 tty_standard_install(driver, tty);
Alan Cox8b0a88d2008-10-13 10:42:19 +01001152}
1153
1154/**
1155 * tty_driver_remove_tty() - remove a tty from the driver tables
1156 * @driver: the driver for the tty
1157 * @idx: the minor number
1158 *
1159 * Remvoe a tty object from the driver tables. The tty->index field
1160 * will be set by the time this is called.
1161 *
1162 * Locking: tty_mutex for now
1163 */
Peter Hurley05de87ed2016-01-09 21:13:49 -08001164static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +01001165{
1166 if (driver->ops->remove)
1167 driver->ops->remove(driver, tty);
1168 else
1169 driver->ttys[tty->index] = NULL;
1170}
1171
1172/*
1173 * tty_reopen() - fast re-open of an open tty
1174 * @tty - the tty to open
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001175 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001176 * Return 0 on success, -errno on error.
Peter Hurley5d93e742014-11-05 12:12:47 -05001177 * Re-opens on master ptys are not allowed and return -EIO.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001178 *
Peter Hurley5d93e742014-11-05 12:12:47 -05001179 * Locking: Caller must hold tty_lock
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001180 */
Alan Cox99f1fe12008-10-13 10:42:00 +01001181static int tty_reopen(struct tty_struct *tty)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001182{
1183 struct tty_driver *driver = tty->driver;
1184
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001185 if (driver->type == TTY_DRIVER_TYPE_PTY &&
Peter Hurley5d93e742014-11-05 12:12:47 -05001186 driver->subtype == PTY_TYPE_MASTER)
1187 return -EIO;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001188
Peter Hurley7f22f6c2016-01-09 21:13:45 -08001189 if (!tty->count)
1190 return -EAGAIN;
1191
Peter Hurley86f2c002014-12-30 10:39:25 -05001192 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1193 return -EBUSY;
1194
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001195 tty->count++;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001196
Peter Hurley892d1fa2016-01-10 22:41:06 -08001197 if (!tty->ldisc)
1198 return tty_ldisc_reinit(tty, tty->termios.c_line);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001199
1200 return 0;
1201}
1202
Alan Coxaf9b8972006-08-27 01:24:01 -07001203/**
Alan Coxd81ed102008-10-13 10:41:42 +01001204 * tty_init_dev - initialise a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001205 * @driver: tty driver we are opening a device on
1206 * @idx: device index
Alan Cox15582d32008-10-13 10:41:03 +01001207 * @ret_tty: returned tty structure
Alan Coxaf9b8972006-08-27 01:24:01 -07001208 *
1209 * Prepare a tty device. This may not be a "new" clean device but
1210 * could also be an active device. The pty drivers require special
1211 * handling because of this.
1212 *
1213 * Locking:
1214 * The function is called under the tty_mutex, which
1215 * protects us from the tty struct or driver itself going away.
1216 *
1217 * On exit the tty device has the line discipline attached and
1218 * a reference count of 1. If a pair was created for pty/tty use
1219 * and the other was a pty master then it too has a reference count of 1.
1220 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001222 * failed open. The new code protects the open with a mutex, so it's
1223 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 * relaxed for the (most common) case of reopening a tty.
1225 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001226
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001227struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Alan Coxbf970ee2008-10-13 10:42:39 +01001229 struct tty_struct *tty;
Alan Cox73ec06f2008-10-13 10:42:29 +01001230 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /*
1233 * First time open is complex, especially for PTY devices.
1234 * This code guarantees that either everything succeeds and the
1235 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001236 * and the allocated memory released. (Except that the termios
Johan Hovold16b00ae2017-03-30 15:39:35 +02001237 * may be retained.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 */
1239
Alan Cox73ec06f2008-10-13 10:42:29 +01001240 if (!try_module_get(driver->owner))
1241 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02001243 tty = alloc_tty_struct(driver, idx);
Jiri Slabyd5543502011-03-23 10:48:32 +01001244 if (!tty) {
1245 retval = -ENOMEM;
1246 goto err_module_put;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Alan Cox89c8d912012-08-08 16:30:13 +01001249 tty_lock(tty);
Alan Cox73ec06f2008-10-13 10:42:29 +01001250 retval = tty_driver_install_tty(driver, tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001251 if (retval < 0)
Peter Hurleyc8b710b2016-01-09 21:13:46 -08001252 goto err_free_tty;
Alan Cox8b0a88d2008-10-13 10:42:19 +01001253
Jiri Slaby04831dc2012-06-04 13:35:36 +02001254 if (!tty->port)
1255 tty->port = driver->ports[idx];
1256
Jiri Slaby5d4121c2012-08-17 14:27:52 +02001257 WARN_RATELIMIT(!tty->port,
1258 "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
1259 __func__, tty->driver->name);
1260
Jiri Slaby967fab62012-10-18 22:26:46 +02001261 tty->port->itty = tty;
1262
Alan Cox37bdfb02008-02-08 04:18:47 -08001263 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001265 * If we fail here just call release_tty to clean up. No need
1266 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 */
Alan Coxbf970ee2008-10-13 10:42:39 +01001268 retval = tty_ldisc_setup(tty, tty->link);
Alan Cox01e1abb2008-07-22 11:16:55 +01001269 if (retval)
Jiri Slabyd5543502011-03-23 10:48:32 +01001270 goto err_release_tty;
Alan Cox89c8d912012-08-08 16:30:13 +01001271 /* Return the tty locked so that it cannot vanish under the caller */
Alan Cox73ec06f2008-10-13 10:42:29 +01001272 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Peter Hurleyc8b710b2016-01-09 21:13:46 -08001274err_free_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001275 tty_unlock(tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001276 free_tty_struct(tty);
1277err_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 module_put(driver->owner);
Jiri Slabyd5543502011-03-23 10:48:32 +01001279 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001281 /* call the tty release_tty routine to clean out this slot */
Jiri Slabyd5543502011-03-23 10:48:32 +01001282err_release_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001283 tty_unlock(tty);
Peter Hurley339f36b2015-11-08 13:01:13 -05001284 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n",
1285 retval, idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001286 release_tty(tty, idx);
Alan Cox73ec06f2008-10-13 10:42:29 +01001287 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Peter Hurleya3123fd2016-01-09 21:13:48 -08001290static void tty_free_termios(struct tty_struct *tty)
Alan Coxfeebed62008-10-13 10:41:30 +01001291{
1292 struct ktermios *tp;
1293 int idx = tty->index;
Alan Cox36b3c072012-07-17 17:06:57 +01001294
1295 /* If the port is going to reset then it has no termios to save */
1296 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1297 return;
1298
1299 /* Stash the termios data */
1300 tp = tty->driver->termios[idx];
1301 if (tp == NULL) {
1302 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
Peter Hurley25080652015-11-08 13:01:11 -05001303 if (tp == NULL)
Alan Cox36b3c072012-07-17 17:06:57 +01001304 return;
Dan Carpenter4ac5d702012-07-24 12:51:52 +01001305 tty->driver->termios[idx] = tp;
Alan Coxfeebed62008-10-13 10:41:30 +01001306 }
Alan Cox36b3c072012-07-17 17:06:57 +01001307 *tp = tty->termios;
Alan Coxfeebed62008-10-13 10:41:30 +01001308}
Alan Coxfeebed62008-10-13 10:41:30 +01001309
Peter Hurleya2965b72013-03-11 16:44:35 -04001310/**
Peter Hurley949aa642014-11-05 12:12:57 -05001311 * tty_flush_works - flush all works of a tty/pty pair
1312 * @tty: tty device to flush works for (or either end of a pty pair)
Peter Hurleya2965b72013-03-11 16:44:35 -04001313 *
Peter Hurley949aa642014-11-05 12:12:57 -05001314 * Sync flush all works belonging to @tty (and the 'other' tty).
Peter Hurleya2965b72013-03-11 16:44:35 -04001315 */
1316static void tty_flush_works(struct tty_struct *tty)
1317{
1318 flush_work(&tty->SAK_work);
1319 flush_work(&tty->hangup_work);
Peter Hurley949aa642014-11-05 12:12:57 -05001320 if (tty->link) {
1321 flush_work(&tty->link->SAK_work);
1322 flush_work(&tty->link->hangup_work);
1323 }
Peter Hurleya2965b72013-03-11 16:44:35 -04001324}
Alan Coxfeebed62008-10-13 10:41:30 +01001325
Alan Coxaf9b8972006-08-27 01:24:01 -07001326/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001327 * release_one_tty - release tty structure memory
Alan Cox9c9f4de2008-10-13 10:37:26 +01001328 * @kref: kref of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001329 *
1330 * Releases memory associated with a tty structure, and clears out the
1331 * driver table slots. This function is called when a device is no longer
1332 * in use. It also gets called when setup of a device fails.
1333 *
1334 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -07001335 * takes the file list lock internally when working on the list
1336 * of ttys that the driver keeps.
Alan Coxb50989d2009-09-19 13:13:22 -07001337 *
1338 * This method gets called from a work queue so that the driver private
Dave Youngf278a2f2009-09-27 16:00:42 +00001339 * cleanup ops can sleep (needed for USB at least)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 */
Alan Coxb50989d2009-09-19 13:13:22 -07001341static void release_one_tty(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Alan Coxb50989d2009-09-19 13:13:22 -07001343 struct tty_struct *tty =
1344 container_of(work, struct tty_struct, hangup_work);
Alan Cox6f967f72008-10-13 10:37:36 +01001345 struct tty_driver *driver = tty->driver;
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001346 struct module *owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Dave Youngf278a2f2009-09-27 16:00:42 +00001348 if (tty->ops->cleanup)
1349 tty->ops->cleanup(tty);
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 tty->magic = 0;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001352 tty_driver_kref_put(driver);
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001353 module_put(owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001354
Peter Hurley4a510962016-01-09 21:35:23 -08001355 spin_lock(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 list_del_init(&tty->tty_files);
Peter Hurley4a510962016-01-09 21:35:23 -08001357 spin_unlock(&tty->files_lock);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001358
Oleg Nesterov6da8d862010-04-02 18:05:12 +02001359 put_pid(tty->pgrp);
1360 put_pid(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 free_tty_struct(tty);
1362}
1363
Alan Coxb50989d2009-09-19 13:13:22 -07001364static void queue_release_one_tty(struct kref *kref)
1365{
1366 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Dave Youngf278a2f2009-09-27 16:00:42 +00001367
Alan Coxb50989d2009-09-19 13:13:22 -07001368 /* The hangup queue is now free so we can reuse it rather than
1369 waste a chunk of memory for each port */
1370 INIT_WORK(&tty->hangup_work, release_one_tty);
1371 schedule_work(&tty->hangup_work);
1372}
1373
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001374/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001375 * tty_kref_put - release a tty kref
1376 * @tty: tty device
1377 *
1378 * Release a reference to a tty device and if need be let the kref
1379 * layer destruct the object for us
1380 */
1381
1382void tty_kref_put(struct tty_struct *tty)
1383{
1384 if (tty)
Alan Coxb50989d2009-09-19 13:13:22 -07001385 kref_put(&tty->kref, queue_release_one_tty);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001386}
1387EXPORT_SYMBOL(tty_kref_put);
1388
1389/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001390 * release_tty - release tty structure memory
1391 *
1392 * Release both @tty and a possible linked partner (think pty pair),
1393 * and decrement the refcount of the backing module.
1394 *
1395 * Locking:
Alan Coxd1552552012-07-27 18:02:54 +01001396 * tty_mutex
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001397 * takes the file list lock internally when working on the list
1398 * of ttys that the driver keeps.
Alan Cox9c9f4de2008-10-13 10:37:26 +01001399 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001400 */
1401static void release_tty(struct tty_struct *tty, int idx)
1402{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001403 /* This should always be true but check for the moment */
1404 WARN_ON(tty->index != idx);
Alan Coxd1552552012-07-27 18:02:54 +01001405 WARN_ON(!mutex_is_locked(&tty_mutex));
Alan Cox36b3c072012-07-17 17:06:57 +01001406 if (tty->ops->shutdown)
1407 tty->ops->shutdown(tty);
1408 tty_free_termios(tty);
1409 tty_driver_remove_tty(tty->driver, tty);
Jiri Slaby967fab62012-10-18 22:26:46 +02001410 tty->port->itty = NULL;
Peter Hurley64e377d2013-06-15 09:01:00 -04001411 if (tty->link)
1412 tty->link->port->itty = NULL;
Peter Hurleye1760582015-10-17 16:36:23 -04001413 tty_buffer_cancel_work(tty->port);
Alan Cox36b3c072012-07-17 17:06:57 +01001414
Markus Elfringa211b1a2014-11-21 13:42:29 +01001415 tty_kref_put(tty->link);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001416 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001417}
1418
Alan Coxeeb89d92009-11-30 13:18:29 +00001419/**
Jiri Slaby955787ca2011-11-11 10:47:23 +01001420 * tty_release_checks - check a tty before real release
1421 * @tty: tty to check
1422 * @o_tty: link of @tty (if any)
1423 * @idx: index of the tty
1424 *
1425 * Performs some paranoid checking before true release of the @tty.
1426 * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1427 */
Peter Hurley359b9fb2014-11-05 12:12:59 -05001428static int tty_release_checks(struct tty_struct *tty, int idx)
Jiri Slaby955787ca2011-11-11 10:47:23 +01001429{
1430#ifdef TTY_PARANOIA_CHECK
1431 if (idx < 0 || idx >= tty->driver->num) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001432 tty_debug(tty, "bad idx %d\n", idx);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001433 return -1;
1434 }
1435
1436 /* not much to check for devpts */
1437 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1438 return 0;
1439
1440 if (tty != tty->driver->ttys[idx]) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001441 tty_debug(tty, "bad driver table[%d] = %p\n",
1442 idx, tty->driver->ttys[idx]);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001443 return -1;
1444 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001445 if (tty->driver->other) {
Peter Hurley359b9fb2014-11-05 12:12:59 -05001446 struct tty_struct *o_tty = tty->link;
1447
Jiri Slaby955787ca2011-11-11 10:47:23 +01001448 if (o_tty != tty->driver->other->ttys[idx]) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001449 tty_debug(tty, "bad other table[%d] = %p\n",
1450 idx, tty->driver->other->ttys[idx]);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001451 return -1;
1452 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001453 if (o_tty->link != tty) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001454 tty_debug(tty, "bad link = %p\n", o_tty->link);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001455 return -1;
1456 }
1457 }
1458#endif
1459 return 0;
1460}
1461
1462/**
Rob Herring9ed90d22017-01-16 16:54:28 -06001463 * tty_release_struct - release a tty struct
1464 * @tty: tty device
1465 * @idx: index of the tty
1466 *
1467 * Performs the final steps to release and free a tty device. It is
1468 * roughly the reverse of tty_init_dev.
1469 */
1470void tty_release_struct(struct tty_struct *tty, int idx)
1471{
1472 /*
1473 * Ask the line discipline code to release its structures
1474 */
1475 tty_ldisc_release(tty);
1476
1477 /* Wait for pending work before tty destruction commmences */
1478 tty_flush_works(tty);
1479
1480 tty_debug_hangup(tty, "freeing structure\n");
1481 /*
1482 * The release_tty function takes care of the details of clearing
1483 * the slots and preserving the termios structure. The tty_unlock_pair
1484 * should be safe as we keep a kref while the tty is locked (so the
1485 * unlock never unlocks a freed tty).
1486 */
1487 mutex_lock(&tty_mutex);
1488 release_tty(tty, idx);
1489 mutex_unlock(&tty_mutex);
1490}
1491EXPORT_SYMBOL_GPL(tty_release_struct);
1492
1493/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001494 * tty_release - vfs callback for close
1495 * @inode: inode of tty
1496 * @filp: file pointer for handle to tty
1497 *
1498 * Called the last time each file handle is closed that references
1499 * this tty. There may however be several such references.
1500 *
1501 * Locking:
1502 * Takes bkl. See tty_release_dev
1503 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 * Even releasing the tty structures is a tricky business.. We have
1505 * to be very careful that the structures are all released at the
1506 * same time, as interrupts might otherwise get the wrong pointers.
1507 *
1508 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1509 * lead to double frees or releasing memory still in use.
1510 */
Alan Coxeeb89d92009-11-30 13:18:29 +00001511
1512int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Nick Piggind996b622010-08-18 04:37:36 +10001514 struct tty_struct *tty = file_tty(filp);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001515 struct tty_struct *o_tty = NULL;
1516 int do_sleep, final;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 int idx;
Peter Hurley37b16452014-10-16 13:51:30 -04001518 long timeout = 0;
Peter Hurley494c1ea2014-10-16 13:54:36 -04001519 int once = 1;
Alan Cox37bdfb02008-02-08 04:18:47 -08001520
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001521 if (tty_paranoia_check(tty, inode, __func__))
Alan Coxeeb89d92009-11-30 13:18:29 +00001522 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Alan Cox89c8d912012-08-08 16:30:13 +01001524 tty_lock(tty);
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001525 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Arnd Bergmannec79d602010-06-01 22:53:01 +02001527 __tty_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 idx = tty->index;
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001530 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1531 tty->driver->subtype == PTY_TYPE_MASTER)
1532 o_tty = tty->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Peter Hurley359b9fb2014-11-05 12:12:59 -05001534 if (tty_release_checks(tty, idx)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001535 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Peter Hurleyd435cef2015-11-08 13:01:19 -05001539 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Alan Coxf34d7a52008-04-30 00:54:13 -07001541 if (tty->ops->close)
1542 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Peter Hurley2aff5e22014-11-05 12:13:01 -05001544 /* If tty is pty master, lock the slave pty (stable lock order) */
1545 tty_lock_slave(o_tty);
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /*
1548 * Sanity check: if tty->count is going to zero, there shouldn't be
1549 * any waiters on tty->read_wait or tty->write_wait. We test the
1550 * wait queues and kick everyone out _before_ actually starting to
1551 * close. This ensures that we won't block while releasing the tty
1552 * structure.
1553 *
1554 * The test for the o_tty closing is necessary, since the master and
1555 * slave sides may close in any order. If the slave side closes out
1556 * first, its count will be one, since the master side holds an open.
Peter Hurley324c1652014-11-05 12:12:56 -05001557 * Thus this test wouldn't be triggered at the time the slave closed,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 * so we do it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 */
1560 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 do_sleep = 0;
1562
Peter Hurley324c1652014-11-05 12:12:56 -05001563 if (tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (waitqueue_active(&tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001565 wake_up_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 do_sleep++;
1567 }
1568 if (waitqueue_active(&tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001569 wake_up_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 do_sleep++;
1571 }
1572 }
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001573 if (o_tty && o_tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (waitqueue_active(&o_tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001575 wake_up_poll(&o_tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 do_sleep++;
1577 }
1578 if (waitqueue_active(&o_tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001579 wake_up_poll(&o_tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 do_sleep++;
1581 }
1582 }
1583 if (!do_sleep)
1584 break;
1585
Peter Hurley494c1ea2014-10-16 13:54:36 -04001586 if (once) {
1587 once = 0;
Peter Hurley339f36b2015-11-08 13:01:13 -05001588 tty_warn(tty, "read/write wait queue active!\n");
Peter Hurley494c1ea2014-10-16 13:54:36 -04001589 }
Peter Hurley37b16452014-10-16 13:51:30 -04001590 schedule_timeout_killable(timeout);
1591 if (timeout < 120 * HZ)
1592 timeout = 2 * timeout + 1;
1593 else
1594 timeout = MAX_SCHEDULE_TIMEOUT;
Alan Cox37bdfb02008-02-08 04:18:47 -08001595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001597 if (o_tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (--o_tty->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001599 tty_warn(tty, "bad slave count (%d)\n", o_tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 o_tty->count = 0;
1601 }
1602 }
1603 if (--tty->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001604 tty_warn(tty, "bad tty->count (%d)\n", tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 tty->count = 0;
1606 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 /*
1609 * We've decremented tty->count, so we need to remove this file
1610 * descriptor off the tty->tty_files list; this serves two
1611 * purposes:
1612 * - check_tty_count sees the correct number of file descriptors
1613 * associated with this tty.
1614 * - do_tty_hangup no longer sees this file descriptor as
1615 * something that needs to be handled for hangups.
1616 */
Nick Piggind996b622010-08-18 04:37:36 +10001617 tty_del_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 /*
1620 * Perform some housekeeping before deciding whether to return.
1621 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 * If _either_ side is closing, make sure there aren't any
1623 * processes that still think tty or o_tty is their controlling
1624 * tty.
1625 */
Peter Hurley324c1652014-11-05 12:12:56 -05001626 if (!tty->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001628 session_clear_tty(tty->session);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001629 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001630 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 read_unlock(&tasklist_lock);
1632 }
1633
Peter Hurley324c1652014-11-05 12:12:56 -05001634 /* check whether both sides are closing ... */
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001635 final = !tty->count && !(o_tty && o_tty->count);
Peter Hurley324c1652014-11-05 12:12:56 -05001636
Peter Hurley2aff5e22014-11-05 12:13:01 -05001637 tty_unlock_slave(o_tty);
1638 tty_unlock(tty);
1639
Peter Hurley04980702014-11-05 12:12:52 -05001640 /* At this point, the tty->count == 0 should ensure a dead tty
Alan Coxd1552552012-07-27 18:02:54 +01001641 cannot be re-opened by a racing opener */
Paul Fulghumda965822006-02-14 13:53:00 -08001642
Peter Hurley324c1652014-11-05 12:12:56 -05001643 if (!final)
Alan Coxeeb89d92009-11-30 13:18:29 +00001644 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001645
Peter Hurleyaccff792015-07-12 22:49:09 -04001646 tty_debug_hangup(tty, "final close\n");
Peter Hurleya2965b72013-03-11 16:44:35 -04001647
Rob Herring9ed90d22017-01-16 16:54:28 -06001648 tty_release_struct(tty, idx);
Alan Coxeeb89d92009-11-30 13:18:29 +00001649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Alan Coxaf9b8972006-08-27 01:24:01 -07001652/**
Peter Hurley52494ee2014-11-05 12:12:50 -05001653 * tty_open_current_tty - get locked tty of current task
Jiri Slabyb82154a2011-11-09 21:33:19 +01001654 * @device: device number
1655 * @filp: file pointer to tty
Peter Hurley52494ee2014-11-05 12:12:50 -05001656 * @return: locked tty of the current task iff @device is /dev/tty
1657 *
1658 * Performs a re-open of the current task's controlling tty.
Jiri Slabyb82154a2011-11-09 21:33:19 +01001659 *
1660 * We cannot return driver and index like for the other nodes because
1661 * devpts will not work then. It expects inodes to be from devpts FS.
1662 */
1663static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1664{
1665 struct tty_struct *tty;
Peter Hurley52494ee2014-11-05 12:12:50 -05001666 int retval;
Jiri Slabyb82154a2011-11-09 21:33:19 +01001667
1668 if (device != MKDEV(TTYAUX_MAJOR, 0))
1669 return NULL;
1670
1671 tty = get_current_tty();
1672 if (!tty)
1673 return ERR_PTR(-ENXIO);
1674
1675 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1676 /* noctty = 1; */
Peter Hurley52494ee2014-11-05 12:12:50 -05001677 tty_lock(tty);
1678 tty_kref_put(tty); /* safe to drop the kref now */
1679
1680 retval = tty_reopen(tty);
1681 if (retval < 0) {
1682 tty_unlock(tty);
1683 tty = ERR_PTR(retval);
1684 }
Jiri Slabyb82154a2011-11-09 21:33:19 +01001685 return tty;
1686}
1687
1688/**
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001689 * tty_lookup_driver - lookup a tty driver for a given device file
1690 * @device: device number
1691 * @filp: file pointer to tty
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001692 * @index: index for the device in the @return driver
1693 * @return: driver for this inode (with increased refcount)
1694 *
1695 * If @return is not erroneous, the caller is responsible to decrement the
1696 * refcount by tty_driver_kref_put.
1697 *
1698 * Locking: tty_mutex protects get_tty_driver
1699 */
1700static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
Peter Hurley11e1d4a2016-01-09 21:13:52 -08001701 int *index)
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001702{
1703 struct tty_driver *driver;
1704
Jiri Slaby2cd00502011-11-09 21:33:22 +01001705 switch (device) {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001706#ifdef CONFIG_VT
Jiri Slaby2cd00502011-11-09 21:33:22 +01001707 case MKDEV(TTY_MAJOR, 0): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001708 extern struct tty_driver *console_driver;
1709 driver = tty_driver_kref_get(console_driver);
1710 *index = fg_console;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001711 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001712 }
1713#endif
Jiri Slaby2cd00502011-11-09 21:33:22 +01001714 case MKDEV(TTYAUX_MAJOR, 1): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001715 struct tty_driver *console_driver = console_device(index);
1716 if (console_driver) {
1717 driver = tty_driver_kref_get(console_driver);
1718 if (driver) {
1719 /* Don't let /dev/console block */
1720 filp->f_flags |= O_NONBLOCK;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001721 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001722 }
1723 }
1724 return ERR_PTR(-ENODEV);
1725 }
Jiri Slaby2cd00502011-11-09 21:33:22 +01001726 default:
1727 driver = get_tty_driver(device, index);
1728 if (!driver)
1729 return ERR_PTR(-ENODEV);
1730 break;
1731 }
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001732 return driver;
1733}
1734
1735/**
Peter Hurleyd6203d02016-01-09 21:13:53 -08001736 * tty_open_by_driver - open a tty device
1737 * @device: dev_t of device to open
1738 * @inode: inode of device file
1739 * @filp: file pointer to tty
1740 *
1741 * Performs the driver lookup, checks for a reopen, or otherwise
1742 * performs the first-time tty initialization.
1743 *
1744 * Returns the locked initialized or re-opened &tty_struct
1745 *
1746 * Claims the global tty_mutex to serialize:
1747 * - concurrent first-time tty initialization
1748 * - concurrent tty driver removal w/ lookup
1749 * - concurrent tty removal from driver table
1750 */
1751static struct tty_struct *tty_open_by_driver(dev_t device, struct inode *inode,
1752 struct file *filp)
1753{
1754 struct tty_struct *tty;
1755 struct tty_driver *driver = NULL;
1756 int index = -1;
1757 int retval;
1758
1759 mutex_lock(&tty_mutex);
1760 driver = tty_lookup_driver(device, filp, &index);
1761 if (IS_ERR(driver)) {
1762 mutex_unlock(&tty_mutex);
1763 return ERR_CAST(driver);
1764 }
1765
1766 /* check whether we're reopening an existing tty */
Linus Torvalds8ead9dd2016-04-25 20:04:08 -07001767 tty = tty_driver_lookup_tty(driver, filp, index);
Peter Hurleyd6203d02016-01-09 21:13:53 -08001768 if (IS_ERR(tty)) {
1769 mutex_unlock(&tty_mutex);
1770 goto out;
1771 }
1772
1773 if (tty) {
1774 mutex_unlock(&tty_mutex);
1775 retval = tty_lock_interruptible(tty);
Peter Hurley5e00bbf2016-03-31 17:47:07 -07001776 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */
Peter Hurleyd6203d02016-01-09 21:13:53 -08001777 if (retval) {
1778 if (retval == -EINTR)
1779 retval = -ERESTARTSYS;
1780 tty = ERR_PTR(retval);
1781 goto out;
1782 }
Peter Hurleyd6203d02016-01-09 21:13:53 -08001783 retval = tty_reopen(tty);
1784 if (retval < 0) {
1785 tty_unlock(tty);
1786 tty = ERR_PTR(retval);
1787 }
1788 } else { /* Returns with the tty_lock held for now */
1789 tty = tty_init_dev(driver, index);
1790 mutex_unlock(&tty_mutex);
1791 }
1792out:
1793 tty_driver_kref_put(driver);
1794 return tty;
1795}
1796
1797/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001798 * tty_open - open a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001799 * @inode: inode of device file
1800 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001802 * tty_open and tty_release keep up the tty count that contains the
1803 * number of opens done on a tty. We cannot use the inode-count, as
1804 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001806 * Open-counting is needed for pty masters, as well as for keeping
1807 * track of serial lines: DTR is dropped when the last close happens.
1808 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1809 *
1810 * The termios state of a pty is reset on first open so that
1811 * settings don't persist across reuse.
1812 *
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001813 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001814 * tty->count should protect the rest.
1815 * ->siglock protects ->signal/->sighand
Alan Cox89c8d912012-08-08 16:30:13 +01001816 *
1817 * Note: the tty_unlock/lock cases without a ref are only safe due to
1818 * tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001820
Alan Coxeeb89d92009-11-30 13:18:29 +00001821static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Jiri Slabyb82154a2011-11-09 21:33:19 +01001823 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 int noctty, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 dev_t device = inode->i_rdev;
Andrew Morton846c1512009-04-02 16:56:36 -07001826 unsigned saved_flags = filp->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830retry_open:
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02001831 retval = tty_alloc_file(filp);
1832 if (retval)
1833 return -ENOMEM;
1834
Jiri Slabyb82154a2011-11-09 21:33:19 +01001835 tty = tty_open_current_tty(device, filp);
Peter Hurleyd6203d02016-01-09 21:13:53 -08001836 if (!tty)
1837 tty = tty_open_by_driver(device, inode, filp);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001838
Alan Coxeeb89d92009-11-30 13:18:29 +00001839 if (IS_ERR(tty)) {
Peter Hurleyd6203d02016-01-09 21:13:53 -08001840 tty_free_file(filp);
Jiri Slabyba5db442011-11-09 21:33:21 +01001841 retval = PTR_ERR(tty);
Peter Hurley7f22f6c2016-01-09 21:13:45 -08001842 if (retval != -EAGAIN || signal_pending(current))
Peter Hurleyd6203d02016-01-09 21:13:53 -08001843 return retval;
Peter Hurley7f22f6c2016-01-09 21:13:45 -08001844 schedule();
1845 goto retry_open;
Alan Coxeeb89d92009-11-30 13:18:29 +00001846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02001848 tty_add_file(tty, filp);
Nick Piggind996b622010-08-18 04:37:36 +10001849
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001850 check_tty_count(tty, __func__);
Peter Hurleyd435cef2015-11-08 13:01:19 -05001851 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
Peter Hurleyaccff792015-07-12 22:49:09 -04001852
Herton Ronaldo Krzesinski909bc772011-03-31 15:35:31 -03001853 if (tty->ops->open)
1854 retval = tty->ops->open(tty, filp);
1855 else
1856 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 filp->f_flags = saved_flags;
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (retval) {
Peter Hurleyd435cef2015-11-08 13:01:19 -05001860 tty_debug_hangup(tty, "open error %d, releasing\n", retval);
Peter Hurleyaccff792015-07-12 22:49:09 -04001861
Alan Cox89c8d912012-08-08 16:30:13 +01001862 tty_unlock(tty); /* need to call tty_release without BTM */
Alan Coxeeb89d92009-11-30 13:18:29 +00001863 tty_release(inode, filp);
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +02001864 if (retval != -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return retval;
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +02001866
1867 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return retval;
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +02001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 schedule();
1871 /*
1872 * Need to reset f_op in case a hangup happened.
1873 */
Peter Hurley12569372014-11-05 12:26:24 -05001874 if (tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 filp->f_op = &tty_fops;
1876 goto retry_open;
1877 }
Peter Hurleyd4855e12013-11-19 08:46:27 -05001878 clear_bit(TTY_HUPPED, &tty->flags);
Alan Coxeeb89d92009-11-30 13:18:29 +00001879
Peter Hurley11e1d4a2016-01-09 21:13:52 -08001880 noctty = (filp->f_flags & O_NOCTTY) ||
Nicolas Pitrea1235b32017-04-12 18:37:16 -04001881 (IS_ENABLED(CONFIG_VT) && device == MKDEV(TTY_MAJOR, 0)) ||
1882 device == MKDEV(TTYAUX_MAJOR, 1) ||
1883 (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1884 tty->driver->subtype == PTY_TYPE_MASTER);
1885 if (!noctty)
1886 tty_open_proc_set_tty(filp, tty);
Alan Cox89c8d912012-08-08 16:30:13 +01001887 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return 0;
1889}
1890
Jonathan Corbet39d95b92008-05-16 09:10:50 -06001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Alan Coxaf9b8972006-08-27 01:24:01 -07001893/**
1894 * tty_poll - check tty status
1895 * @filp: file being polled
1896 * @wait: poll wait structures to update
1897 *
1898 * Call the line discipline polling method to obtain the poll
1899 * status of the device.
1900 *
1901 * Locking: locks called line discipline but ldisc poll method
1902 * may be re-entered freely by other callers.
1903 */
1904
Alan Cox37bdfb02008-02-08 04:18:47 -08001905static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Nick Piggind996b622010-08-18 04:37:36 +10001907 struct tty_struct *tty = file_tty(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 struct tty_ldisc *ld;
1909 int ret = 0;
1910
Al Viro6131ffa2013-02-27 16:59:05 -05001911 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08001915 if (!ld)
1916 return hung_up_tty_poll(filp, wait);
Alan Coxa352def2008-07-16 21:53:12 +01001917 if (ld->ops->poll)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05001918 ret = ld->ops->poll(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 tty_ldisc_deref(ld);
1920 return ret;
1921}
1922
Arnd Bergmannec79d602010-06-01 22:53:01 +02001923static int __tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Nick Piggind996b622010-08-18 04:37:36 +10001925 struct tty_struct *tty = file_tty(filp);
Alan Cox47f86832008-04-30 00:53:30 -07001926 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001927 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Al Viro6131ffa2013-02-27 16:59:05 -05001929 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001930 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 retval = fasync_helper(fd, filp, on, &tty->fasync);
1933 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001937 enum pid_type type;
1938 struct pid *pid;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001939
Alan Cox47f86832008-04-30 00:53:30 -07001940 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001941 if (tty->pgrp) {
1942 pid = tty->pgrp;
1943 type = PIDTYPE_PGID;
1944 } else {
1945 pid = task_pid(current);
1946 type = PIDTYPE_PID;
1947 }
Linus Torvalds80e1e822010-02-07 10:11:23 -08001948 get_pid(pid);
Greg Kroah-Hartman70362512009-12-17 07:07:19 -08001949 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001950 __f_setown(filp, pid, type, 0);
Linus Torvalds80e1e822010-02-07 10:11:23 -08001951 put_pid(pid);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001952 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001954out:
Arnd Bergmannec79d602010-06-01 22:53:01 +02001955 return retval;
1956}
1957
1958static int tty_fasync(int fd, struct file *filp, int on)
1959{
Alan Cox89c8d912012-08-08 16:30:13 +01001960 struct tty_struct *tty = file_tty(filp);
Peter Hurleya8f3a292016-01-09 21:45:11 -08001961 int retval = -ENOTTY;
Alan Cox89c8d912012-08-08 16:30:13 +01001962
1963 tty_lock(tty);
Peter Hurleya8f3a292016-01-09 21:45:11 -08001964 if (!tty_hung_up_p(filp))
1965 retval = __tty_fasync(fd, filp, on);
Alan Cox89c8d912012-08-08 16:30:13 +01001966 tty_unlock(tty);
1967
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001968 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
Alan Coxaf9b8972006-08-27 01:24:01 -07001971/**
1972 * tiocsti - fake input character
1973 * @tty: tty to fake input into
1974 * @p: pointer to character
1975 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001976 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07001977 * input management.
1978 *
1979 * FIXME: does not honour flow control ??
1980 *
1981 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -04001982 * Called functions take tty_ldiscs_lock
Alan Coxaf9b8972006-08-27 01:24:01 -07001983 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07001984 *
1985 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07001986 */
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988static int tiocsti(struct tty_struct *tty, char __user *p)
1989{
1990 char ch, mbz = 0;
1991 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
1994 return -EPERM;
1995 if (get_user(ch, p))
1996 return -EFAULT;
Al Viro1e641742008-12-09 09:23:33 +00001997 tty_audit_tiocsti(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08001999 if (!ld)
2000 return -EIO;
Alan Coxa352def2008-07-16 21:53:12 +01002001 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 tty_ldisc_deref(ld);
2003 return 0;
2004}
2005
Alan Coxaf9b8972006-08-27 01:24:01 -07002006/**
2007 * tiocgwinsz - implement window query ioctl
2008 * @tty; tty
2009 * @arg: user buffer for result
2010 *
Alan Cox808a0d32006-09-29 02:00:40 -07002011 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002012 *
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002013 * Locking: tty->winsize_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002014 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002015 */
2016
Alan Cox37bdfb02008-02-08 04:18:47 -08002017static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Alan Cox808a0d32006-09-29 02:00:40 -07002019 int err;
2020
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002021 mutex_lock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002022 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002023 mutex_unlock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002024
2025 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Alan Coxaf9b8972006-08-27 01:24:01 -07002028/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002029 * tty_do_resize - resize event
2030 * @tty: tty being resized
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002031 * @rows: rows (character)
2032 * @cols: cols (character)
Alan Coxaf9b8972006-08-27 01:24:01 -07002033 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002034 * Update the termios variables and send the necessary signals to
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002035 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002036 */
2037
Alan Coxfc6f6232009-01-02 13:43:17 +00002038int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Alan Coxfc6f6232009-01-02 13:43:17 +00002040 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Alan Coxfc6f6232009-01-02 13:43:17 +00002042 /* Lock the tty */
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002043 mutex_lock(&tty->winsize_mutex);
Alan Coxfc6f6232009-01-02 13:43:17 +00002044 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002045 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002046
Peter Hurley5b239542014-10-16 14:59:45 -04002047 /* Signal the foreground process group */
2048 pgrp = tty_get_pgrp(tty);
Alan Cox47f86832008-04-30 00:53:30 -07002049 if (pgrp)
2050 kill_pgrp(pgrp, SIGWINCH, 1);
Alan Cox47f86832008-04-30 00:53:30 -07002051 put_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002052
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002053 tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002054done:
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002055 mutex_unlock(&tty->winsize_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return 0;
2057}
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01002058EXPORT_SYMBOL(tty_do_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Alan Coxaf9b8972006-08-27 01:24:01 -07002060/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002061 * tiocswinsz - implement window size set ioctl
Alan Coxfc6f6232009-01-02 13:43:17 +00002062 * @tty; tty side of tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002063 * @arg: user buffer for result
2064 *
2065 * Copies the user idea of the window size to the kernel. Traditionally
2066 * this is just advisory information but for the Linux console it
2067 * actually has driver level meaning and triggers a VC resize.
2068 *
2069 * Locking:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002070 * Driver dependent. The default do_resize method takes the
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002071 * tty termios mutex and ctrl_lock. The console takes its own lock
2072 * then calls into the default method.
2073 */
2074
Alan Coxfc6f6232009-01-02 13:43:17 +00002075static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002076{
2077 struct winsize tmp_ws;
2078 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2079 return -EFAULT;
2080
2081 if (tty->ops->resize)
Alan Coxfc6f6232009-01-02 13:43:17 +00002082 return tty->ops->resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002083 else
Alan Coxfc6f6232009-01-02 13:43:17 +00002084 return tty_do_resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002085}
2086
2087/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002088 * tioccons - allow admin to move logical console
2089 * @file: the file to become console
2090 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002091 * Allow the administrator to move the redirected console device
Alan Coxaf9b8972006-08-27 01:24:01 -07002092 *
2093 * Locking: uses redirect_lock to guard the redirect information
2094 */
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096static int tioccons(struct file *file)
2097{
2098 if (!capable(CAP_SYS_ADMIN))
2099 return -EPERM;
2100 if (file->f_op->write == redirected_tty_write) {
2101 struct file *f;
2102 spin_lock(&redirect_lock);
2103 f = redirect;
2104 redirect = NULL;
2105 spin_unlock(&redirect_lock);
2106 if (f)
2107 fput(f);
2108 return 0;
2109 }
2110 spin_lock(&redirect_lock);
2111 if (redirect) {
2112 spin_unlock(&redirect_lock);
2113 return -EBUSY;
2114 }
Al Virocb0942b2012-08-27 14:48:26 -04002115 redirect = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 spin_unlock(&redirect_lock);
2117 return 0;
2118}
2119
Alan Coxaf9b8972006-08-27 01:24:01 -07002120/**
2121 * fionbio - non blocking ioctl
2122 * @file: file to set blocking value
2123 * @p: user parameter
2124 *
2125 * Historical tty interfaces had a blocking control ioctl before
2126 * the generic functionality existed. This piece of history is preserved
2127 * in the expected tty API of posix OS's.
2128 *
Alan Cox6146b9a2009-09-19 13:13:19 -07002129 * Locking: none, the open file handle ensures it won't go away.
Alan Coxaf9b8972006-08-27 01:24:01 -07002130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132static int fionbio(struct file *file, int __user *p)
2133{
2134 int nonblock;
2135
2136 if (get_user(nonblock, p))
2137 return -EFAULT;
2138
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002139 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (nonblock)
2141 file->f_flags |= O_NONBLOCK;
2142 else
2143 file->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002144 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return 0;
2146}
2147
Alan Coxaf9b8972006-08-27 01:24:01 -07002148/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002149 * tiocsetd - set line discipline
2150 * @tty: tty device
2151 * @p: pointer to user data
2152 *
2153 * Set the line discipline according to user request.
2154 *
2155 * Locking: see tty_set_ldisc, this function is just a helper
2156 */
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158static int tiocsetd(struct tty_struct *tty, int __user *p)
2159{
Peter Hurleyc12da962016-01-10 22:41:04 -08002160 int disc;
Alan Cox04f378b2008-04-30 00:53:29 -07002161 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Peter Hurleyc12da962016-01-10 22:41:04 -08002163 if (get_user(disc, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002165
Peter Hurleyc12da962016-01-10 22:41:04 -08002166 ret = tty_set_ldisc(tty, disc);
Alan Cox04f378b2008-04-30 00:53:29 -07002167
2168 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
Alan Coxaf9b8972006-08-27 01:24:01 -07002171/**
Peter Hurley5c17c862016-01-10 22:40:55 -08002172 * tiocgetd - get line discipline
2173 * @tty: tty device
2174 * @p: pointer to user data
2175 *
2176 * Retrieves the line discipline id directly from the ldisc.
2177 *
2178 * Locking: waits for ldisc reference (in case the line discipline
2179 * is changing or the tty is being hungup)
2180 */
2181
2182static int tiocgetd(struct tty_struct *tty, int __user *p)
2183{
2184 struct tty_ldisc *ld;
2185 int ret;
2186
2187 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002188 if (!ld)
2189 return -EIO;
Peter Hurley5c17c862016-01-10 22:40:55 -08002190 ret = put_user(ld->ops->num, p);
2191 tty_ldisc_deref(ld);
2192 return ret;
2193}
2194
2195/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002196 * send_break - performed time break
2197 * @tty: device to break on
2198 * @duration: timeout in mS
2199 *
2200 * Perform a timed break on hardware that lacks its own driver level
2201 * timed break functionality.
2202 *
2203 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002204 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002205 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002206 */
2207
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002208static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Alan Cox9e989662008-07-22 11:18:03 +01002210 int retval;
2211
2212 if (tty->ops->break_ctl == NULL)
2213 return 0;
2214
2215 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2216 retval = tty->ops->break_ctl(tty, duration);
2217 else {
2218 /* Do the work ourselves */
2219 if (tty_write_lock(tty, 0) < 0)
2220 return -EINTR;
2221 retval = tty->ops->break_ctl(tty, -1);
2222 if (retval)
2223 goto out;
2224 if (!signal_pending(current))
2225 msleep_interruptible(duration);
2226 retval = tty->ops->break_ctl(tty, 0);
2227out:
2228 tty_write_unlock(tty);
2229 if (signal_pending(current))
2230 retval = -EINTR;
2231 }
2232 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Alan Coxaf9b8972006-08-27 01:24:01 -07002235/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002236 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002237 * @tty: tty device
2238 * @file: user file pointer
2239 * @p: pointer to result
2240 *
2241 * Obtain the modem status bits from the tty driver if the feature
2242 * is supported. Return -EINVAL if it is not available.
2243 *
2244 * Locking: none (up to the driver)
2245 */
2246
Alan Cox60b33c12011-02-14 16:26:14 +00002247static int tty_tiocmget(struct tty_struct *tty, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
2249 int retval = -EINVAL;
2250
Alan Coxf34d7a52008-04-30 00:54:13 -07002251 if (tty->ops->tiocmget) {
Alan Cox60b33c12011-02-14 16:26:14 +00002252 retval = tty->ops->tiocmget(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 if (retval >= 0)
2255 retval = put_user(retval, p);
2256 }
2257 return retval;
2258}
2259
Alan Coxaf9b8972006-08-27 01:24:01 -07002260/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002261 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002262 * @tty: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002263 * @cmd: command - clear bits, set bits or set all
2264 * @p: pointer to desired bits
2265 *
2266 * Set the modem status bits from the tty driver if the feature
2267 * is supported. Return -EINVAL if it is not available.
2268 *
2269 * Locking: none (up to the driver)
2270 */
2271
Alan Cox20b9d172011-02-14 16:26:50 +00002272static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 unsigned __user *p)
2274{
Alan Coxae677512008-07-16 21:56:54 +01002275 int retval;
2276 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Alan Coxae677512008-07-16 21:56:54 +01002278 if (tty->ops->tiocmset == NULL)
2279 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Alan Coxae677512008-07-16 21:56:54 +01002281 retval = get_user(val, p);
2282 if (retval)
2283 return retval;
2284 set = clear = 0;
2285 switch (cmd) {
2286 case TIOCMBIS:
2287 set = val;
2288 break;
2289 case TIOCMBIC:
2290 clear = val;
2291 break;
2292 case TIOCMSET:
2293 set = val;
2294 clear = ~val;
2295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
Alan Coxae677512008-07-16 21:56:54 +01002297 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2298 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
Alan Cox20b9d172011-02-14 16:26:50 +00002299 return tty->ops->tiocmset(tty, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
Alan Coxd281da72010-09-16 18:21:24 +01002302static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2303{
2304 int retval = -EINVAL;
2305 struct serial_icounter_struct icount;
2306 memset(&icount, 0, sizeof(icount));
2307 if (tty->ops->get_icount)
2308 retval = tty->ops->get_icount(tty, &icount);
2309 if (retval != 0)
2310 return retval;
2311 if (copy_to_user(arg, &icount, sizeof(icount)))
2312 return -EFAULT;
2313 return 0;
2314}
2315
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002316static void tty_warn_deprecated_flags(struct serial_struct __user *ss)
2317{
2318 static DEFINE_RATELIMIT_STATE(depr_flags,
2319 DEFAULT_RATELIMIT_INTERVAL,
2320 DEFAULT_RATELIMIT_BURST);
2321 char comm[TASK_COMM_LEN];
2322 int flags;
2323
2324 if (get_user(flags, &ss->flags))
2325 return;
2326
2327 flags &= ASYNC_DEPRECATED;
2328
2329 if (flags && __ratelimit(&depr_flags))
Joe Perchesd437fa92017-02-16 23:11:44 -08002330 pr_warn("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
2331 __func__, get_task_comm(comm, current), flags);
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002332}
2333
Peter Hurley8f166e02014-10-16 14:59:41 -04002334/*
2335 * if pty, return the slave side (real_tty)
2336 * otherwise, return self
2337 */
2338static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
Alan Coxe8b70e72009-06-11 12:48:02 +01002339{
2340 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2341 tty->driver->subtype == PTY_TYPE_MASTER)
2342 tty = tty->link;
2343 return tty;
2344}
Alan Coxe8b70e72009-06-11 12:48:02 +01002345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346/*
2347 * Split this up, as gcc can choke on it otherwise..
2348 */
Alan Cox04f378b2008-04-30 00:53:29 -07002349long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Nick Piggind996b622010-08-18 04:37:36 +10002351 struct tty_struct *tty = file_tty(file);
2352 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 void __user *p = (void __user *)arg;
2354 int retval;
2355 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002356
Al Viro6131ffa2013-02-27 16:59:05 -05002357 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return -EINVAL;
2359
Alan Coxe8b70e72009-06-11 12:48:02 +01002360 real_tty = tty_pair_get_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 /*
2363 * Factor out some common prep work
2364 */
2365 switch (cmd) {
2366 case TIOCSETD:
2367 case TIOCSBRK:
2368 case TIOCCBRK:
2369 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002370 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 retval = tty_check_change(tty);
2372 if (retval)
2373 return retval;
2374 if (cmd != TIOCCBRK) {
2375 tty_wait_until_sent(tty, 0);
2376 if (signal_pending(current))
2377 return -EINTR;
2378 }
2379 break;
2380 }
2381
Alan Cox9e989662008-07-22 11:18:03 +01002382 /*
2383 * Now do the stuff.
2384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002386 case TIOCSTI:
2387 return tiocsti(tty, p);
2388 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002389 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002390 case TIOCSWINSZ:
Alan Coxfc6f6232009-01-02 13:43:17 +00002391 return tiocswinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002392 case TIOCCONS:
2393 return real_tty != tty ? -EINVAL : tioccons(file);
2394 case FIONBIO:
2395 return fionbio(file, p);
2396 case TIOCEXCL:
2397 set_bit(TTY_EXCLUSIVE, &tty->flags);
2398 return 0;
2399 case TIOCNXCL:
2400 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2401 return 0;
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +04002402 case TIOCGEXCL:
2403 {
2404 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2405 return put_user(excl, (int __user *)p);
2406 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002407 case TIOCGETD:
Peter Hurley5c17c862016-01-10 22:40:55 -08002408 return tiocgetd(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002409 case TIOCSETD:
2410 return tiocsetd(tty, p);
Kay Sievers3c95c982011-02-17 18:39:28 +01002411 case TIOCVHANGUP:
2412 if (!capable(CAP_SYS_ADMIN))
2413 return -EPERM;
2414 tty_vhangup(tty);
2415 return 0;
Werner Finkb7b8de02010-12-03 12:48:23 +01002416 case TIOCGDEV:
2417 {
2418 unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2419 return put_user(ret, (unsigned int __user *)p);
2420 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002421 /*
2422 * Break handling
2423 */
2424 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002425 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002426 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002427 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002428 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002429 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002430 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002431 return 0;
2432 case TCSBRK: /* SVID version: non-zero arg --> no break */
2433 /* non-zero arg means wait for all output data
2434 * to be sent (performed above) but don't send break.
2435 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002437 if (!arg)
2438 return send_break(tty, 250);
2439 return 0;
2440 case TCSBRKP: /* support for POSIX tcsendbreak() */
2441 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Alan Cox37bdfb02008-02-08 04:18:47 -08002443 case TIOCMGET:
Alan Cox60b33c12011-02-14 16:26:14 +00002444 return tty_tiocmget(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002445 case TIOCMSET:
2446 case TIOCMBIC:
2447 case TIOCMBIS:
Alan Cox20b9d172011-02-14 16:26:50 +00002448 return tty_tiocmset(tty, cmd, p);
Alan Coxd281da72010-09-16 18:21:24 +01002449 case TIOCGICOUNT:
2450 retval = tty_tiocgicount(tty, p);
2451 /* For the moment allow fall through to the old method */
2452 if (retval != -EINVAL)
2453 return retval;
2454 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002455 case TCFLSH:
2456 switch (arg) {
2457 case TCIFLUSH:
2458 case TCIOFLUSH:
2459 /* flush tty buffer and allow ldisc to process ioctl */
Peter Hurley86c80a82014-11-05 12:13:09 -05002460 tty_buffer_flush(tty, NULL);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002461 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002462 }
2463 break;
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002464 case TIOCSSERIAL:
2465 tty_warn_deprecated_flags(p);
2466 break;
Nicolas Pitrea1235b32017-04-12 18:37:16 -04002467 default:
2468 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg);
2469 if (retval != -ENOIOCTLCMD)
2470 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002472 if (tty->ops->ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002473 retval = tty->ops->ioctl(tty, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (retval != -ENOIOCTLCMD)
2475 return retval;
2476 }
2477 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002478 if (!ld)
2479 return hung_up_tty_ioctl(file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002481 if (ld->ops->ioctl) {
2482 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (retval == -ENOIOCTLCMD)
Wanlong Gaobbb63c52012-08-27 15:23:12 +08002484 retval = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
2486 tty_ldisc_deref(ld);
2487 return retval;
2488}
2489
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002490#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08002491static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002492 unsigned long arg)
2493{
Nick Piggind996b622010-08-18 04:37:36 +10002494 struct tty_struct *tty = file_tty(file);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002495 struct tty_ldisc *ld;
2496 int retval = -ENOIOCTLCMD;
2497
Al Viro6131ffa2013-02-27 16:59:05 -05002498 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002499 return -EINVAL;
2500
Alan Coxf34d7a52008-04-30 00:54:13 -07002501 if (tty->ops->compat_ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002502 retval = tty->ops->compat_ioctl(tty, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002503 if (retval != -ENOIOCTLCMD)
2504 return retval;
2505 }
2506
2507 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002508 if (!ld)
2509 return hung_up_tty_compat_ioctl(file, cmd, arg);
Alan Coxa352def2008-07-16 21:53:12 +01002510 if (ld->ops->compat_ioctl)
2511 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Thomas Meyer8193c422011-10-05 23:13:13 +02002512 else
2513 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002514 tty_ldisc_deref(ld);
2515
2516 return retval;
2517}
2518#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Al Viroc3c073f2012-08-21 22:32:06 -04002520static int this_tty(const void *t, struct file *file, unsigned fd)
2521{
2522 if (likely(file->f_op->read != tty_read))
2523 return 0;
2524 return file_tty(file) != t ? 0 : fd + 1;
2525}
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527/*
2528 * This implements the "Secure Attention Key" --- the idea is to
2529 * prevent trojan horses by killing all processes associated with this
2530 * tty when the user hits the "Secure Attention Key". Required for
2531 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08002532 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 * This code could be nicer; ideally it should send a HUP, wait a few
2534 * seconds, then send a INT, and then a KILL signal. But you then
2535 * have to coordinate with the init process, since all processes associated
2536 * with the current tty must be dead before the new getty is allowed
2537 * to spawn.
2538 *
2539 * Now, if it would be correct ;-/ The current code has a nasty hole -
2540 * it doesn't catch files in flight. We may send the descriptor to ourselves
2541 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2542 *
2543 * Nasty bug: do_SAK is being called in interrupt context. This can
2544 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2545 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002546void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
2548#ifdef TTY_SOFT_SAK
2549 tty_hangup(tty);
2550#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08002551 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002552 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -08002554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (!tty)
2556 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002557 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08002558
Dan Carpenterb3f13de2006-12-13 00:35:09 -08002559 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Alan Coxf34d7a52008-04-30 00:54:13 -07002561 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08002562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002564 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002565 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05002566 tty_notice(tty, "SAK: killed process %d (%s): by session\n",
2567 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002568 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002569 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Hurley9b42bb72015-11-08 13:01:14 -05002570
2571 /* Now kill any processes that happen to have the tty open */
Eric W. Biederman652486f2006-03-28 16:11:02 -08002572 do_each_thread(g, p) {
2573 if (p->signal->tty == tty) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05002574 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
2575 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 send_sig(SIGKILL, p, 1);
2577 continue;
2578 }
2579 task_lock(p);
Al Viroc3c073f2012-08-21 22:32:06 -04002580 i = iterate_fd(p->files, 0, this_tty, tty);
2581 if (i != 0) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05002582 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n",
2583 task_pid_nr(p), p->comm, i - 1);
Al Viroc3c073f2012-08-21 22:32:06 -04002584 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
2586 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002587 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 read_unlock(&tasklist_lock);
2589#endif
2590}
2591
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002592static void do_SAK_work(struct work_struct *work)
2593{
2594 struct tty_struct *tty =
2595 container_of(work, struct tty_struct, SAK_work);
2596 __do_SAK(tty);
2597}
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599/*
2600 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2601 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2602 * the values which we write to it will be identical to the values which it
2603 * already has. --akpm
2604 */
2605void do_SAK(struct tty_struct *tty)
2606{
2607 if (!tty)
2608 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 schedule_work(&tty->SAK_work);
2610}
2611
2612EXPORT_SYMBOL(do_SAK);
2613
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08002614static int dev_match_devt(struct device *dev, const void *data)
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002615{
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08002616 const dev_t *devt = data;
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002617 return dev->devt == *devt;
2618}
2619
2620/* Must put_device() after it's unused! */
2621static struct device *tty_get_device(struct tty_struct *tty)
2622{
2623 dev_t devt = tty_devnum(tty);
2624 return class_find_device(tty_class, NULL, &devt, dev_match_devt);
2625}
2626
2627
Alan Coxaf9b8972006-08-27 01:24:01 -07002628/**
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002629 * alloc_tty_struct
Alan Coxaf9b8972006-08-27 01:24:01 -07002630 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002631 * This subroutine allocates and initializes a tty structure.
Alan Coxaf9b8972006-08-27 01:24:01 -07002632 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002633 * Locking: none - tty in question is not exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002635
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002636struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002638 struct tty_struct *tty;
2639
2640 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
2641 if (!tty)
2642 return NULL;
2643
Alan Cox9c9f4de2008-10-13 10:37:26 +01002644 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 tty->magic = TTY_MAGIC;
Alan Cox01e1abb2008-07-22 11:16:55 +01002646 tty_ldisc_init(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002647 tty->session = NULL;
2648 tty->pgrp = NULL;
Alan Cox89c8d912012-08-08 16:30:13 +01002649 mutex_init(&tty->legacy_mutex);
Peter Hurleyd8c1f922013-06-15 09:14:31 -04002650 mutex_init(&tty->throttle_mutex);
Peter Hurley6a1c0682013-06-15 09:14:23 -04002651 init_rwsem(&tty->termios_rwsem);
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002652 mutex_init(&tty->winsize_mutex);
Peter Hurley36697522013-06-15 07:04:48 -04002653 init_ldsem(&tty->ldisc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 init_waitqueue_head(&tty->write_wait);
2655 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00002656 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08002657 mutex_init(&tty->atomic_write_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07002658 spin_lock_init(&tty->ctrl_lock);
Peter Hurleyf9e053d2014-09-10 15:06:31 -04002659 spin_lock_init(&tty->flow_lock);
Peter Hurley4a510962016-01-09 21:35:23 -08002660 spin_lock_init(&tty->files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002662 INIT_WORK(&tty->SAK_work, do_SAK_work);
Alan Coxbf970ee2008-10-13 10:42:39 +01002663
2664 tty->driver = driver;
2665 tty->ops = driver->ops;
2666 tty->index = idx;
2667 tty_line_name(driver, idx, tty->name);
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002668 tty->dev = tty_get_device(tty);
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02002669
2670 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
Alan Coxf34d7a52008-04-30 00:54:13 -07002673/**
2674 * tty_put_char - write one character to a tty
2675 * @tty: tty
2676 * @ch: character
2677 *
2678 * Write one byte to the tty using the provided put_char method
2679 * if present. Returns the number of characters successfully output.
2680 *
2681 * Note: the specific put_char operation in the driver layer may go
2682 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002684
Alan Coxf34d7a52008-04-30 00:54:13 -07002685int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
Alan Coxf34d7a52008-04-30 00:54:13 -07002687 if (tty->ops->put_char)
2688 return tty->ops->put_char(tty, ch);
2689 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690}
Alan Coxf34d7a52008-04-30 00:54:13 -07002691EXPORT_SYMBOL_GPL(tty_put_char);
2692
Alan Coxd81ed102008-10-13 10:41:42 +01002693struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002695static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
2696 unsigned int index, unsigned int count)
2697{
Leon Yuc1a752b2015-09-07 13:08:37 +00002698 int err;
2699
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002700 /* init here, since reused cdevs cause crashes */
Richard Wattsa3a10ce2015-05-19 16:06:53 +01002701 driver->cdevs[index] = cdev_alloc();
2702 if (!driver->cdevs[index])
2703 return -ENOMEM;
Leon Yuc1a752b2015-09-07 13:08:37 +00002704 driver->cdevs[index]->ops = &tty_fops;
Richard Wattsa3a10ce2015-05-19 16:06:53 +01002705 driver->cdevs[index]->owner = driver->owner;
Leon Yuc1a752b2015-09-07 13:08:37 +00002706 err = cdev_add(driver->cdevs[index], dev, count);
2707 if (err)
2708 kobject_put(&driver->cdevs[index]->kobj);
2709 return err;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002710}
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002713 * tty_register_device - register a tty device
2714 * @driver: the tty driver that describes the tty device
2715 * @index: the index in the tty driver for this tty device
2716 * @device: a struct device that is associated with this tty device.
2717 * This field is optional, if there is no known struct device
2718 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07002720 * Returns a pointer to the struct device for this tty device
2721 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002722 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002723 * This call is required to be made to register an individual tty device
2724 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2725 * that bit is not set, this function should not be called by a tty
2726 * driver.
2727 *
2728 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002730
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07002731struct device *tty_register_device(struct tty_driver *driver, unsigned index,
2732 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002734 return tty_register_device_attr(driver, index, device, NULL, NULL);
2735}
2736EXPORT_SYMBOL(tty_register_device);
2737
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002738static void tty_device_create_release(struct device *dev)
2739{
Peter Hurley83db1df2015-11-08 13:01:21 -05002740 dev_dbg(dev, "releasing...\n");
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002741 kfree(dev);
2742}
2743
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002744/**
2745 * tty_register_device_attr - register a tty device
2746 * @driver: the tty driver that describes the tty device
2747 * @index: the index in the tty driver for this tty device
2748 * @device: a struct device that is associated with this tty device.
2749 * This field is optional, if there is no known struct device
2750 * for this tty device it can be set to NULL safely.
2751 * @drvdata: Driver data to be set to device.
2752 * @attr_grp: Attribute group to be set on device.
2753 *
2754 * Returns a pointer to the struct device for this tty device
2755 * (or ERR_PTR(-EFOO) on error).
2756 *
2757 * This call is required to be made to register an individual tty device
2758 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2759 * that bit is not set, this function should not be called by a tty
2760 * driver.
2761 *
2762 * Locking: ??
2763 */
2764struct device *tty_register_device_attr(struct tty_driver *driver,
2765 unsigned index, struct device *device,
2766 void *drvdata,
2767 const struct attribute_group **attr_grp)
2768{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 char name[64];
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002770 dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
Johan Hovold93857ed2017-03-30 15:39:36 +02002771 struct ktermios *tp;
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002772 struct device *dev;
2773 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 if (index >= driver->num) {
Peter Hurley656fb862015-11-08 13:01:15 -05002776 pr_err("%s: Attempt to register invalid tty line number (%d)\n",
2777 driver->name, index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002778 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (driver->type == TTY_DRIVER_TYPE_PTY)
2782 pty_line_name(driver, index, name);
2783 else
2784 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002785
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002786 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002787 if (!dev)
2788 return ERR_PTR(-ENOMEM);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002789
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002790 dev->devt = devt;
2791 dev->class = tty_class;
2792 dev->parent = device;
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002793 dev->release = tty_device_create_release;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002794 dev_set_name(dev, "%s", name);
2795 dev->groups = attr_grp;
2796 dev_set_drvdata(dev, drvdata);
2797
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002798 dev_set_uevent_suppress(dev, 1);
2799
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002800 retval = device_register(dev);
2801 if (retval)
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002802 goto err_put;
2803
2804 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
Johan Hovold93857ed2017-03-30 15:39:36 +02002805 /*
2806 * Free any saved termios data so that the termios state is
2807 * reset when reusing a minor number.
2808 */
2809 tp = driver->termios[index];
2810 if (tp) {
2811 driver->termios[index] = NULL;
2812 kfree(tp);
2813 }
2814
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002815 retval = tty_cdev_add(driver, devt, index, 1);
2816 if (retval)
2817 goto err_del;
2818 }
2819
2820 dev_set_uevent_suppress(dev, 0);
2821 kobject_uevent(&dev->kobj, KOBJ_ADD);
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002822
2823 return dev;
2824
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002825err_del:
2826 device_del(dev);
2827err_put:
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002828 put_device(dev);
Johan Hovold6a7e6f72017-03-30 15:39:34 +02002829
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002830 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002832EXPORT_SYMBOL_GPL(tty_register_device_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002835 * tty_unregister_device - unregister a tty device
2836 * @driver: the tty driver that describes the tty device
2837 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002839 * If a tty device is registered with a call to tty_register_device() then
2840 * this function must be called when the tty device is gone.
2841 *
2842 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845void tty_unregister_device(struct tty_driver *driver, unsigned index)
2846{
Alan Cox37bdfb02008-02-08 04:18:47 -08002847 device_destroy(tty_class,
2848 MKDEV(driver->major, driver->minor_start) + index);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01002849 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
2850 cdev_del(driver->cdevs[index]);
2851 driver->cdevs[index] = NULL;
2852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854EXPORT_SYMBOL(tty_unregister_device);
2855
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002856/**
2857 * __tty_alloc_driver -- allocate tty driver
2858 * @lines: count of lines this driver can handle at most
Thadeu Lima de Souza Cascardo87838ae2017-04-04 05:56:32 -03002859 * @owner: module which is responsible for this driver
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002860 * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
2861 *
2862 * This should not be called directly, some of the provided macros should be
2863 * used instead. Use IS_ERR and friends on @retval.
2864 */
2865struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
2866 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
2868 struct tty_driver *driver;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002869 unsigned int cdevs = 1;
Jiri Slaby16a02082012-08-08 22:26:42 +02002870 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Jiri Slaby0019b402012-08-08 22:26:43 +02002872 if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002873 return ERR_PTR(-EINVAL);
2874
Jean Delvare506eb992007-07-15 23:40:14 -07002875 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002876 if (!driver)
2877 return ERR_PTR(-ENOMEM);
2878
2879 kref_init(&driver->kref);
2880 driver->magic = TTY_DRIVER_MAGIC;
2881 driver->num = lines;
2882 driver->owner = owner;
2883 driver->flags = flags;
Jiri Slaby16a02082012-08-08 22:26:42 +02002884
2885 if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
2886 driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
2887 GFP_KERNEL);
2888 driver->termios = kcalloc(lines, sizeof(*driver->termios),
2889 GFP_KERNEL);
2890 if (!driver->ttys || !driver->termios) {
2891 err = -ENOMEM;
2892 goto err_free_all;
2893 }
2894 }
2895
2896 if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
2897 driver->ports = kcalloc(lines, sizeof(*driver->ports),
2898 GFP_KERNEL);
2899 if (!driver->ports) {
2900 err = -ENOMEM;
2901 goto err_free_all;
2902 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002903 cdevs = lines;
2904 }
2905
2906 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
2907 if (!driver->cdevs) {
2908 err = -ENOMEM;
2909 goto err_free_all;
Jiri Slaby16a02082012-08-08 22:26:42 +02002910 }
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return driver;
Jiri Slaby16a02082012-08-08 22:26:42 +02002913err_free_all:
2914 kfree(driver->ports);
2915 kfree(driver->ttys);
2916 kfree(driver->termios);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01002917 kfree(driver->cdevs);
Jiri Slaby16a02082012-08-08 22:26:42 +02002918 kfree(driver);
2919 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02002921EXPORT_SYMBOL(__tty_alloc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Alan Cox7d7b93c2008-10-13 10:42:09 +01002923static void destruct_tty_driver(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
Alan Cox7d7b93c2008-10-13 10:42:09 +01002925 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
2926 int i;
2927 struct ktermios *tp;
Alan Cox7d7b93c2008-10-13 10:42:09 +01002928
2929 if (driver->flags & TTY_DRIVER_INSTALLED) {
Alan Cox7d7b93c2008-10-13 10:42:09 +01002930 for (i = 0; i < driver->num; i++) {
2931 tp = driver->termios[i];
2932 if (tp) {
2933 driver->termios[i] = NULL;
2934 kfree(tp);
2935 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01002936 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
2937 tty_unregister_device(driver, i);
2938 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01002939 proc_tty_unregister_driver(driver);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002940 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
Richard Wattsa3a10ce2015-05-19 16:06:53 +01002941 cdev_del(driver->cdevs[0]);
Alan Cox7d7b93c2008-10-13 10:42:09 +01002942 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002943 kfree(driver->cdevs);
Jiri Slaby04831dc2012-06-04 13:35:36 +02002944 kfree(driver->ports);
Jiri Slaby16a02082012-08-08 22:26:42 +02002945 kfree(driver->termios);
2946 kfree(driver->ttys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 kfree(driver);
2948}
2949
Alan Cox7d7b93c2008-10-13 10:42:09 +01002950void tty_driver_kref_put(struct tty_driver *driver)
2951{
2952 kref_put(&driver->kref, destruct_tty_driver);
2953}
2954EXPORT_SYMBOL(tty_driver_kref_put);
2955
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002956void tty_set_operations(struct tty_driver *driver,
2957 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
Alan Coxf34d7a52008-04-30 00:54:13 -07002959 driver->ops = op;
2960};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961EXPORT_SYMBOL(tty_set_operations);
2962
Alan Cox7d7b93c2008-10-13 10:42:09 +01002963void put_tty_driver(struct tty_driver *d)
2964{
2965 tty_driver_kref_put(d);
2966}
2967EXPORT_SYMBOL(put_tty_driver);
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969/*
2970 * Called by a tty driver to register itself.
2971 */
2972int tty_register_driver(struct tty_driver *driver)
2973{
2974 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08002975 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 dev_t dev;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04002977 struct device *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002980 error = alloc_chrdev_region(&dev, driver->minor_start,
2981 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 if (!error) {
2983 driver->major = MAJOR(dev);
2984 driver->minor_start = MINOR(dev);
2985 }
2986 } else {
2987 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01002988 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02002990 if (error < 0)
Jiri Slaby16a02082012-08-08 22:26:42 +02002991 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Jiri Slaby7e73eca2012-08-08 22:26:44 +02002993 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
2994 error = tty_cdev_add(driver, dev, 0, driver->num);
2995 if (error)
2996 goto err_unreg_char;
2997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Alexey Dobriyanca509f62007-05-08 00:27:12 -07002999 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003001 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003002
3003 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003004 for (i = 0; i < driver->num; i++) {
3005 d = tty_register_device(driver, i, NULL);
3006 if (IS_ERR(d)) {
3007 error = PTR_ERR(d);
Jiri Slaby16a02082012-08-08 22:26:42 +02003008 goto err_unreg_devs;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003009 }
3010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
3012 proc_tty_register_driver(driver);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003013 driver->flags |= TTY_DRIVER_INSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return 0;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003015
Jiri Slaby16a02082012-08-08 22:26:42 +02003016err_unreg_devs:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003017 for (i--; i >= 0; i--)
3018 tty_unregister_device(driver, i);
3019
3020 mutex_lock(&tty_mutex);
3021 list_del(&driver->tty_drivers);
3022 mutex_unlock(&tty_mutex);
3023
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003024err_unreg_char:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003025 unregister_chrdev_region(dev, driver->num);
Jiri Slaby16a02082012-08-08 22:26:42 +02003026err:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003027 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029EXPORT_SYMBOL(tty_register_driver);
3030
3031/*
3032 * Called by a tty driver to unregister itself.
3033 */
3034int tty_unregister_driver(struct tty_driver *driver)
3035{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003036#if 0
3037 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (driver->refcount)
3039 return -EBUSY;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003040#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3042 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003043 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003045 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return 0;
3047}
Alan Cox7d7b93c2008-10-13 10:42:09 +01003048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049EXPORT_SYMBOL(tty_unregister_driver);
3050
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003051dev_t tty_devnum(struct tty_struct *tty)
3052{
3053 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3054}
3055EXPORT_SYMBOL(tty_devnum);
3056
Alan Coxd81ed102008-10-13 10:41:42 +01003057void tty_default_fops(struct file_operations *fops)
3058{
3059 *fops = tty_fops;
3060}
3061
Al Viro2c9ede52011-07-23 20:24:48 -04003062static char *tty_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +02003063{
3064 if (!mode)
3065 return NULL;
3066 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3067 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3068 *mode = 0666;
3069 return NULL;
3070}
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072static int __init tty_class_init(void)
3073{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003074 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 if (IS_ERR(tty_class))
3076 return PTR_ERR(tty_class);
Kay Sieverse454cea2009-09-18 23:01:12 +02003077 tty_class->devnode = tty_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return 0;
3079}
3080
3081postcore_initcall(tty_class_init);
3082
3083/* 3/2004 jmc: why do these devices exist? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084static struct cdev tty_cdev, console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Kay Sieversfbc92a32010-12-01 18:51:05 +01003086static ssize_t show_cons_active(struct device *dev,
3087 struct device_attribute *attr, char *buf)
3088{
3089 struct console *cs[16];
3090 int i = 0;
3091 struct console *c;
3092 ssize_t count = 0;
3093
Torben Hohnac751ef2011-01-25 15:07:35 -08003094 console_lock();
Kay Sieversa2a6a822011-01-09 16:39:14 +01003095 for_each_console(c) {
Kay Sieversfbc92a32010-12-01 18:51:05 +01003096 if (!c->device)
3097 continue;
3098 if (!c->write)
3099 continue;
3100 if ((c->flags & CON_ENABLED) == 0)
3101 continue;
3102 cs[i++] = c;
3103 if (i >= ARRAY_SIZE(cs))
3104 break;
3105 }
Hannes Reinecke723abd82014-02-27 12:30:51 +01003106 while (i--) {
3107 int index = cs[i]->index;
3108 struct tty_driver *drv = cs[i]->device(cs[i], &index);
3109
3110 /* don't resolve tty0 as some programs depend on it */
3111 if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR))
3112 count += tty_line_name(drv, index, buf + count);
3113 else
3114 count += sprintf(buf + count, "%s%d",
3115 cs[i]->name, cs[i]->index);
3116
3117 count += sprintf(buf + count, "%c", i ? ' ':'\n');
3118 }
Torben Hohnac751ef2011-01-25 15:07:35 -08003119 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01003120
3121 return count;
3122}
3123static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3124
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003125static struct attribute *cons_dev_attrs[] = {
3126 &dev_attr_active.attr,
3127 NULL
3128};
3129
3130ATTRIBUTE_GROUPS(cons_dev);
3131
Kay Sieversfbc92a32010-12-01 18:51:05 +01003132static struct device *consdev;
3133
3134void console_sysfs_notify(void)
3135{
3136 if (consdev)
3137 sysfs_notify(&consdev->kobj, NULL, "active");
3138}
3139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140/*
3141 * Ok, now we can initialize the rest of the tty devices and can count
3142 * on memory allocations, interrupts etc..
3143 */
David Howells31d1d482010-08-06 16:34:43 +01003144int __init tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
3146 cdev_init(&tty_cdev, &tty_fops);
3147 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3148 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3149 panic("Couldn't register /dev/tty driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003150 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 cdev_init(&console_cdev, &console_fops);
3153 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3154 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3155 panic("Couldn't register /dev/console driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003156 consdev = device_create_with_groups(tty_class, NULL,
3157 MKDEV(TTYAUX_MAJOR, 1), NULL,
3158 cons_dev_groups, "console");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003159 if (IS_ERR(consdev))
3160 consdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162#ifdef CONFIG_VT
Alan Coxd81ed102008-10-13 10:41:42 +01003163 vty_init(&console_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164#endif
3165 return 0;
3166}
David Howells31d1d482010-08-06 16:34:43 +01003167