blob: 1eaf0fbd99e45e0e1df8a198284347ec818ce5df [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>
72#include <linux/sched.h>
73#include <linux/interrupt.h>
74#include <linux/tty.h>
75#include <linux/tty_driver.h>
76#include <linux/tty_flip.h>
77#include <linux/devpts_fs.h>
78#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040079#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/console.h>
81#include <linux/timer.h>
82#include <linux/ctype.h>
83#include <linux/kd.h>
84#include <linux/mm.h>
85#include <linux/string.h>
86#include <linux/slab.h>
87#include <linux/poll.h>
88#include <linux/proc_fs.h>
89#include <linux/init.h>
90#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/wait.h>
93#include <linux/bitops.h>
Domen Puncerb20f3ae2005-06-25 14:58:42 -070094#include <linux/delay.h>
Alan Coxa352def2008-07-16 21:53:12 +010095#include <linux/seq_file.h>
Alan Coxd281da72010-09-16 18:21:24 +010096#include <linux/serial.h>
Manuel Zerpies5a3c6b22011-06-16 14:07:22 +020097#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Alan Coxa352def2008-07-16 21:53:12 +010099#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101#include <linux/kbd_kern.h>
102#include <linux/vt_kern.h>
103#include <linux/selection.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105#include <linux/kmod.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700106#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108#undef TTY_DEBUG_HANGUP
109
110#define TTY_PARANOIA_CHECK 1
111#define CHECK_TTY_COUNT 1
112
Alan Coxedc6afc2006-12-08 02:38:44 -0800113struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .c_iflag = ICRNL | IXON,
115 .c_oflag = OPOST | ONLCR,
116 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
117 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
118 ECHOCTL | ECHOKE | IEXTEN,
Alan Coxedc6afc2006-12-08 02:38:44 -0800119 .c_cc = INIT_C_CC,
120 .c_ispeed = 38400,
121 .c_ospeed = 38400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
124EXPORT_SYMBOL(tty_std_termios);
125
126/* This list gets poked at by procfs and various bits of boot up code. This
127 could do with some rationalisation such as pulling the tty proc function
128 into this file */
Alan Cox37bdfb02008-02-08 04:18:47 -0800129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130LIST_HEAD(tty_drivers); /* linked list of tty drivers */
131
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800132/* Mutex to protect creating and releasing a tty. This is shared with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 vt.c for deeply disgusting hack reasons */
Ingo Molnar70522e12006-03-23 03:00:31 -0800134DEFINE_MUTEX(tty_mutex);
Alan Coxde2a84f2006-09-29 02:00:57 -0700135EXPORT_SYMBOL(tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Nick Pigginee2ffa02010-08-18 04:37:35 +1000137/* Spinlock to protect the tty->tty_files list */
138DEFINE_SPINLOCK(tty_files_lock);
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
141static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
Alan Cox37bdfb02008-02-08 04:18:47 -0800142ssize_t redirected_tty_write(struct file *, const char __user *,
143 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static unsigned int tty_poll(struct file *, poll_table *);
145static int tty_open(struct inode *, struct file *);
Alan Cox04f378b2008-04-30 00:53:29 -0700146long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700147#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -0800148static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700149 unsigned long arg);
150#else
151#define tty_compat_ioctl NULL
152#endif
Arnd Bergmannec79d602010-06-01 22:53:01 +0200153static int __tty_fasync(int fd, struct file *filp, int on);
Alan Cox37bdfb02008-02-08 04:18:47 -0800154static int tty_fasync(int fd, struct file *filp, int on);
Christoph Hellwigd5698c22007-02-10 01:46:46 -0800155static void release_tty(struct tty_struct *tty, int idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Alan Coxaf9b8972006-08-27 01:24:01 -0700157/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700158 * free_tty_struct - free a disused tty
159 * @tty: tty struct to free
160 *
161 * Free the write buffers, tty queue and tty memory itself.
162 *
163 * Locking: none. Must be called after tty is definitely unused
164 */
165
Alan Coxbf970ee2008-10-13 10:42:39 +0100166void free_tty_struct(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Dan Carpenterdc6802a2012-07-24 12:52:04 +0100168 if (!tty)
169 return;
Markus Elfringa211b1a2014-11-21 13:42:29 +0100170 put_device(tty->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 kfree(tty->write_buf);
Alan Cox89c8d912012-08-08 16:30:13 +0100172 tty->magic = 0xDEADDEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 kfree(tty);
174}
175
Nick Piggind996b622010-08-18 04:37:36 +1000176static inline struct tty_struct *file_tty(struct file *file)
177{
178 return ((struct tty_file_private *)file->private_data)->tty;
179}
180
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200181int tty_alloc_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000182{
183 struct tty_file_private *priv;
184
Pekka Enbergf573bd12010-08-24 07:48:34 +0300185 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
186 if (!priv)
187 return -ENOMEM;
Nick Piggind996b622010-08-18 04:37:36 +1000188
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200189 file->private_data = priv;
190
191 return 0;
192}
193
194/* Associate a new file with the tty structure */
195void tty_add_file(struct tty_struct *tty, struct file *file)
196{
197 struct tty_file_private *priv = file->private_data;
198
Nick Piggind996b622010-08-18 04:37:36 +1000199 priv->tty = tty;
200 priv->file = file;
Nick Piggind996b622010-08-18 04:37:36 +1000201
202 spin_lock(&tty_files_lock);
203 list_add(&priv->list, &tty->tty_files);
204 spin_unlock(&tty_files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200205}
Pekka Enbergf573bd12010-08-24 07:48:34 +0300206
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200207/**
208 * tty_free_file - free file->private_data
209 *
210 * This shall be used only for fail path handling when tty_add_file was not
211 * called yet.
212 */
213void tty_free_file(struct file *file)
214{
215 struct tty_file_private *priv = file->private_data;
216
217 file->private_data = NULL;
218 kfree(priv);
Nick Piggind996b622010-08-18 04:37:36 +1000219}
220
221/* Delete file from its tty */
Josh Triplett2520e272012-11-18 21:27:47 -0800222static void tty_del_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000223{
224 struct tty_file_private *priv = file->private_data;
225
226 spin_lock(&tty_files_lock);
227 list_del(&priv->list);
228 spin_unlock(&tty_files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200229 tty_free_file(file);
Nick Piggind996b622010-08-18 04:37:36 +1000230}
231
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
234
Alan Coxaf9b8972006-08-27 01:24:01 -0700235/**
236 * tty_name - return tty naming
237 * @tty: tty structure
Rasmus Villemoes917162c2015-03-31 15:55:58 +0200238 * @buf: unused
Alan Coxaf9b8972006-08-27 01:24:01 -0700239 *
240 * Convert a tty structure into a name. The name reflects the kernel
241 * naming policy and if udev is in use may not reflect user space
242 *
243 * Locking: none
244 */
245
Rasmus Villemoes1d6b9872015-03-31 15:55:57 +0200246const char *tty_name(const struct tty_struct *tty, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 if (!tty) /* Hmm. NULL pointer. That's fun. */
Rasmus Villemoes917162c2015-03-31 15:55:58 +0200249 return "NULL tty";
250 return tty->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253EXPORT_SYMBOL(tty_name);
254
Andrew Mortond769a662005-05-05 16:15:50 -0700255int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 const char *routine)
257{
258#ifdef TTY_PARANOIA_CHECK
259 if (!tty) {
260 printk(KERN_WARNING
261 "null TTY for (%d:%d) in %s\n",
262 imajor(inode), iminor(inode), routine);
263 return 1;
264 }
265 if (tty->magic != TTY_MAGIC) {
266 printk(KERN_WARNING
267 "bad magic number for tty struct (%d:%d) in %s\n",
268 imajor(inode), iminor(inode), routine);
269 return 1;
270 }
271#endif
272 return 0;
273}
274
Peter Hurleydeb287e2014-11-05 12:12:55 -0500275/* Caller must hold tty_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static int check_tty_count(struct tty_struct *tty, const char *routine)
277{
278#ifdef CHECK_TTY_COUNT
279 struct list_head *p;
280 int count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800281
Nick Pigginee2ffa02010-08-18 04:37:35 +1000282 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 list_for_each(p, &tty->tty_files) {
284 count++;
285 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000286 spin_unlock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
288 tty->driver->subtype == PTY_TYPE_SLAVE &&
289 tty->link && tty->link->count)
290 count++;
291 if (tty->count != count) {
292 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
293 "!= #fd's(%d) in %s\n",
294 tty->name, tty->count, count, routine);
295 return count;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#endif
298 return 0;
299}
300
Alan Coxaf9b8972006-08-27 01:24:01 -0700301/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700302 * get_tty_driver - find device of a tty
303 * @dev_t: device identifier
304 * @index: returns the index of the tty
305 *
306 * This routine returns a tty driver structure, given a device number
307 * and also passes back the index number.
308 *
309 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static struct tty_driver *get_tty_driver(dev_t device, int *index)
313{
314 struct tty_driver *p;
315
316 list_for_each_entry(p, &tty_drivers, tty_drivers) {
317 dev_t base = MKDEV(p->major, p->minor_start);
318 if (device < base || device >= base + p->num)
319 continue;
320 *index = device - base;
Alan Cox7d7b93c2008-10-13 10:42:09 +0100321 return tty_driver_kref_get(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323 return NULL;
324}
325
Jason Wesself2d937f2008-04-17 20:05:37 +0200326#ifdef CONFIG_CONSOLE_POLL
327
328/**
329 * tty_find_polling_driver - find device of a polled tty
330 * @name: name string to match
331 * @line: pointer to resulting tty line nr
332 *
333 * This routine returns a tty driver structure, given a name
334 * and the condition that the tty driver is capable of polled
335 * operation.
336 */
337struct tty_driver *tty_find_polling_driver(char *name, int *line)
338{
339 struct tty_driver *p, *res = NULL;
340 int tty_line = 0;
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500341 int len;
Alan Cox5f0878a2009-06-11 12:46:41 +0100342 char *str, *stp;
Jason Wesself2d937f2008-04-17 20:05:37 +0200343
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500344 for (str = name; *str; str++)
345 if ((*str >= '0' && *str <= '9') || *str == ',')
346 break;
347 if (!*str)
348 return NULL;
349
350 len = str - name;
351 tty_line = simple_strtoul(str, &str, 10);
352
Jason Wesself2d937f2008-04-17 20:05:37 +0200353 mutex_lock(&tty_mutex);
354 /* Search through the tty devices to look for a match */
355 list_for_each_entry(p, &tty_drivers, tty_drivers) {
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500356 if (strncmp(name, p->name, len) != 0)
357 continue;
Alan Cox5f0878a2009-06-11 12:46:41 +0100358 stp = str;
359 if (*stp == ',')
360 stp++;
361 if (*stp == '\0')
362 stp = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +0200363
Nathael Pajani6eb68d62010-09-02 16:06:16 +0200364 if (tty_line >= 0 && tty_line < p->num && p->ops &&
Alan Cox5f0878a2009-06-11 12:46:41 +0100365 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
Alan Cox7d7b93c2008-10-13 10:42:09 +0100366 res = tty_driver_kref_get(p);
Jason Wesself2d937f2008-04-17 20:05:37 +0200367 *line = tty_line;
368 break;
369 }
370 }
371 mutex_unlock(&tty_mutex);
372
373 return res;
374}
375EXPORT_SYMBOL_GPL(tty_find_polling_driver);
376#endif
377
Alan Coxaf9b8972006-08-27 01:24:01 -0700378/**
379 * tty_check_change - check for POSIX terminal changes
380 * @tty: tty to check
381 *
382 * If we try to write to, or set the state of, a terminal and we're
383 * not in the foreground, send a SIGTTOU. If the signal is blocked or
384 * ignored, go ahead and perform the operation. (POSIX 7.2)
385 *
Alan Cox978e5952008-04-30 00:53:59 -0700386 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700388
Alan Cox37bdfb02008-02-08 04:18:47 -0800389int tty_check_change(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Alan Cox47f86832008-04-30 00:53:30 -0700391 unsigned long flags;
392 int ret = 0;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (current->signal->tty != tty)
395 return 0;
Alan Cox47f86832008-04-30 00:53:30 -0700396
397 spin_lock_irqsave(&tty->ctrl_lock, flags);
398
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800399 if (!tty->pgrp) {
400 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700401 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800403 if (task_pgrp(current) == tty->pgrp)
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700404 goto out_unlock;
405 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (is_ignored(SIGTTOU))
Alan Cox47f86832008-04-30 00:53:30 -0700407 goto out;
408 if (is_current_pgrp_orphaned()) {
409 ret = -EIO;
410 goto out;
411 }
Oleg Nesterov040b6362007-06-01 00:46:53 -0700412 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
413 set_thread_flag(TIF_SIGPENDING);
Alan Cox47f86832008-04-30 00:53:30 -0700414 ret = -ERESTARTSYS;
415out:
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700416 return ret;
417out_unlock:
Alan Cox47f86832008-04-30 00:53:30 -0700418 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
419 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422EXPORT_SYMBOL(tty_check_change);
423
Alan Cox37bdfb02008-02-08 04:18:47 -0800424static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 size_t count, loff_t *ppos)
426{
427 return 0;
428}
429
Alan Cox37bdfb02008-02-08 04:18:47 -0800430static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 size_t count, loff_t *ppos)
432{
433 return -EIO;
434}
435
436/* No kernel lock held - none needed ;) */
Alan Cox37bdfb02008-02-08 04:18:47 -0800437static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
440}
441
Alan Cox04f378b2008-04-30 00:53:29 -0700442static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
443 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700444{
445 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
446}
447
Alan Cox37bdfb02008-02-08 04:18:47 -0800448static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700449 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
452}
453
Arjan van de Ven62322d22006-07-03 00:24:21 -0700454static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 .llseek = no_llseek,
456 .read = tty_read,
457 .write = tty_write,
458 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700459 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700460 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 .open = tty_open,
462 .release = tty_release,
463 .fasync = tty_fasync,
464};
465
Arjan van de Ven62322d22006-07-03 00:24:21 -0700466static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 .llseek = no_llseek,
468 .read = tty_read,
469 .write = redirected_tty_write,
470 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700471 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700472 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 .open = tty_open,
474 .release = tty_release,
475 .fasync = tty_fasync,
476};
477
Arjan van de Ven62322d22006-07-03 00:24:21 -0700478static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 .llseek = no_llseek,
480 .read = hung_up_tty_read,
481 .write = hung_up_tty_write,
482 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700483 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700484 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .release = tty_release,
486};
487
488static DEFINE_SPINLOCK(redirect_lock);
489static struct file *redirect;
490
Peter Hurley11d9bef2014-10-16 14:59:42 -0400491
492void proc_clear_tty(struct task_struct *p)
493{
494 unsigned long flags;
495 struct tty_struct *tty;
496 spin_lock_irqsave(&p->sighand->siglock, flags);
497 tty = p->signal->tty;
498 p->signal->tty = NULL;
499 spin_unlock_irqrestore(&p->sighand->siglock, flags);
500 tty_kref_put(tty);
501}
502
Peter Hurley2c411c12014-10-16 14:59:47 -0400503/**
504 * proc_set_tty - set the controlling terminal
505 *
506 * Only callable by the session leader and only if it does not already have
507 * a controlling terminal.
508 *
Peter Hurleye218eb32014-10-16 14:59:49 -0400509 * Caller must hold: tty_lock()
510 * a readlock on tasklist_lock
Peter Hurley2c411c12014-10-16 14:59:47 -0400511 * sighand lock
512 */
Peter Hurleybce65f12014-10-16 14:59:43 -0400513static void __proc_set_tty(struct tty_struct *tty)
Peter Hurley11d9bef2014-10-16 14:59:42 -0400514{
Peter Hurleyae28fa72014-10-16 14:59:46 -0400515 unsigned long flags;
516
Peter Hurleyae28fa72014-10-16 14:59:46 -0400517 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurleya3618582014-10-16 14:59:50 -0400518 /*
519 * The session and fg pgrp references will be non-NULL if
520 * tiocsctty() is stealing the controlling tty
521 */
Peter Hurleyae28fa72014-10-16 14:59:46 -0400522 put_pid(tty->session);
523 put_pid(tty->pgrp);
524 tty->pgrp = get_pid(task_pgrp(current));
525 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
526 tty->session = get_pid(task_session(current));
527 if (current->signal->tty) {
528 printk(KERN_DEBUG "tty not NULL!!\n");
529 tty_kref_put(current->signal->tty);
Peter Hurley11d9bef2014-10-16 14:59:42 -0400530 }
Peter Hurleybce65f12014-10-16 14:59:43 -0400531 put_pid(current->signal->tty_old_pgrp);
532 current->signal->tty = tty_kref_get(tty);
533 current->signal->tty_old_pgrp = NULL;
Peter Hurley11d9bef2014-10-16 14:59:42 -0400534}
535
Peter Hurleybce65f12014-10-16 14:59:43 -0400536static void proc_set_tty(struct tty_struct *tty)
Peter Hurley11d9bef2014-10-16 14:59:42 -0400537{
Peter Hurleybce65f12014-10-16 14:59:43 -0400538 spin_lock_irq(&current->sighand->siglock);
539 __proc_set_tty(tty);
540 spin_unlock_irq(&current->sighand->siglock);
Peter Hurley11d9bef2014-10-16 14:59:42 -0400541}
542
543struct tty_struct *get_current_tty(void)
544{
545 struct tty_struct *tty;
546 unsigned long flags;
547
548 spin_lock_irqsave(&current->sighand->siglock, flags);
549 tty = tty_kref_get(current->signal->tty);
550 spin_unlock_irqrestore(&current->sighand->siglock, flags);
551 return tty;
552}
553EXPORT_SYMBOL_GPL(get_current_tty);
554
555static void session_clear_tty(struct pid *session)
556{
557 struct task_struct *p;
558 do_each_pid_task(session, PIDTYPE_SID, p) {
559 proc_clear_tty(p);
560 } while_each_pid_task(session, PIDTYPE_SID, p);
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/**
564 * tty_wakeup - request more data
565 * @tty: terminal
566 *
567 * Internal and external helper for wakeups of tty. This function
568 * informs the line discipline if present that the driver is ready
569 * to receive more output data.
570 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572void tty_wakeup(struct tty_struct *tty)
573{
574 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -0800575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
577 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800578 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100579 if (ld->ops->write_wakeup)
580 ld->ops->write_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 tty_ldisc_deref(ld);
582 }
583 }
Davide Libenzi4b194492009-03-31 15:24:24 -0700584 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587EXPORT_SYMBOL_GPL(tty_wakeup);
588
589/**
Peter Hurleyea648a42013-03-06 07:20:53 -0500590 * tty_signal_session_leader - sends SIGHUP to session leader
Peter Hurleyf91e2592013-03-06 07:20:56 -0500591 * @tty controlling tty
592 * @exit_session if non-zero, signal all foreground group processes
Peter Hurleyea648a42013-03-06 07:20:53 -0500593 *
Peter Hurleyf91e2592013-03-06 07:20:56 -0500594 * Send SIGHUP and SIGCONT to the session leader and its process group.
595 * Optionally, signal all processes in the foreground process group.
Peter Hurleyea648a42013-03-06 07:20:53 -0500596 *
597 * Returns the number of processes in the session with this tty
598 * as their controlling terminal. This value is used to drop
599 * tty references for those processes.
600 */
Peter Hurleyf91e2592013-03-06 07:20:56 -0500601static int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
Peter Hurleyea648a42013-03-06 07:20:53 -0500602{
603 struct task_struct *p;
Peter Hurleyea648a42013-03-06 07:20:53 -0500604 int refs = 0;
Peter Hurleyf91e2592013-03-06 07:20:56 -0500605 struct pid *tty_pgrp = NULL;
Peter Hurleyea648a42013-03-06 07:20:53 -0500606
607 read_lock(&tasklist_lock);
608 if (tty->session) {
609 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
610 spin_lock_irq(&p->sighand->siglock);
611 if (p->signal->tty == tty) {
612 p->signal->tty = NULL;
613 /* We defer the dereferences outside fo
614 the tasklist lock */
615 refs++;
616 }
617 if (!p->signal->leader) {
618 spin_unlock_irq(&p->sighand->siglock);
619 continue;
620 }
621 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
622 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
623 put_pid(p->signal->tty_old_pgrp); /* A noop */
Peter Hurleybc30c3b2013-03-06 07:20:55 -0500624 spin_lock(&tty->ctrl_lock);
Peter Hurleyf91e2592013-03-06 07:20:56 -0500625 tty_pgrp = get_pid(tty->pgrp);
Peter Hurleyea648a42013-03-06 07:20:53 -0500626 if (tty->pgrp)
627 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
Peter Hurleybc30c3b2013-03-06 07:20:55 -0500628 spin_unlock(&tty->ctrl_lock);
Peter Hurleyea648a42013-03-06 07:20:53 -0500629 spin_unlock_irq(&p->sighand->siglock);
630 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
631 }
632 read_unlock(&tasklist_lock);
633
Peter Hurleyf91e2592013-03-06 07:20:56 -0500634 if (tty_pgrp) {
635 if (exit_session)
636 kill_pgrp(tty_pgrp, SIGHUP, exit_session);
637 put_pid(tty_pgrp);
638 }
639
Peter Hurleyea648a42013-03-06 07:20:53 -0500640 return refs;
641}
642
643/**
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200644 * __tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +0000645 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -0700646 *
Kevin Cernekeeef4f5272012-12-26 20:43:41 -0800647 * This can be called by a "kworker" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -0700648 * synchronous but doesn't hold any locks, so we need to make sure we
649 * have the appropriate locks for what we're doing.
650 *
651 * The hangup event clears any pending redirections onto the hung up
652 * device. It ensures future writes will error and it does the needed
653 * line discipline hangup and signal delivery. The tty object itself
654 * remains intact.
655 *
656 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200657 * BTM
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800658 * redirect lock for undoing redirection
659 * file list lock for manipulating list of ttys
Peter Hurley137084b2013-06-15 07:04:46 -0400660 * tty_ldiscs_lock from called functions
Peter Hurley6a1c0682013-06-15 09:14:23 -0400661 * termios_rwsem resetting termios data
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800662 * tasklist_lock to walk task list for hangup event
663 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
Peter Hurleyf91e2592013-03-06 07:20:56 -0500665static void __tty_hangup(struct tty_struct *tty, int exit_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Alan Cox37bdfb02008-02-08 04:18:47 -0800667 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct file *filp, *f = NULL;
Nick Piggind996b622010-08-18 04:37:36 +1000669 struct tty_file_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 int closecount = 0, n;
Peter Hurleyea648a42013-03-06 07:20:53 -0500671 int refs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (!tty)
674 return;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 spin_lock(&redirect_lock);
Nick Piggind996b622010-08-18 04:37:36 +1000678 if (redirect && file_tty(redirect) == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 f = redirect;
680 redirect = NULL;
681 }
682 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800683
Alan Cox89c8d912012-08-08 16:30:13 +0100684 tty_lock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200685
Peter Hurleycb50e522013-07-31 14:05:45 -0400686 if (test_bit(TTY_HUPPED, &tty->flags)) {
687 tty_unlock(tty);
688 return;
689 }
690
Arnd Bergmannec79d602010-06-01 22:53:01 +0200691 /* inuse_filps is protected by the single tty lock,
692 this really needs to change if we want to flush the
693 workqueue with the lock held */
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200694 check_tty_count(tty, "tty_hangup");
Alan Cox36ba7822009-11-30 13:18:51 +0000695
Nick Pigginee2ffa02010-08-18 04:37:35 +1000696 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Nick Piggind996b622010-08-18 04:37:36 +1000698 list_for_each_entry(priv, &tty->tty_files, list) {
699 filp = priv->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (filp->f_op->write == redirected_tty_write)
701 cons_filp = filp;
702 if (filp->f_op->write != tty_write)
703 continue;
704 closecount++;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200705 __tty_fasync(-1, filp, 0); /* can't block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 filp->f_op = &hung_up_tty_fops;
707 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000708 spin_unlock(&tty_files_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800709
Peter Hurley25fdf242013-03-06 07:20:57 -0500710 refs = tty_signal_session_leader(tty, exit_session);
711 /* Account for the p->signal references we killed */
712 while (refs--)
713 tty_kref_put(tty);
714
Alan Coxc65c9bc2009-06-11 12:50:12 +0100715 tty_ldisc_hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800716
Peter Hurley20cc2252013-03-06 07:20:54 -0500717 spin_lock_irq(&tty->ctrl_lock);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100718 clear_bit(TTY_THROTTLED, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100719 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -0600720 put_pid(tty->session);
721 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800722 tty->session = NULL;
723 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 tty->ctrl_status = 0;
Peter Hurley20cc2252013-03-06 07:20:54 -0500725 spin_unlock_irq(&tty->ctrl_lock);
Alan Cox9c9f4de2008-10-13 10:37:26 +0100726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /*
Alan Cox37bdfb02008-02-08 04:18:47 -0800728 * If one of the devices matches a console pointer, we
729 * cannot just call hangup() because that will cause
730 * tty->count and state->count to go out of sync.
731 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
733 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -0700734 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -0700736 tty->ops->close(tty, cons_filp);
737 } else if (tty->ops->hangup)
Peter Hurley7c6d3402014-06-16 09:17:08 -0400738 tty->ops->hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800739 /*
740 * We don't want to have driver/ldisc interactions beyond
741 * the ones we did here. The driver layer expects no
742 * calls after ->hangup() from the ldisc side. However we
743 * can't yet guarantee all that.
744 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 set_bit(TTY_HUPPED, &tty->flags);
Alan Cox89c8d912012-08-08 16:30:13 +0100746 tty_unlock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (f)
749 fput(f);
750}
751
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200752static void do_tty_hangup(struct work_struct *work)
753{
754 struct tty_struct *tty =
755 container_of(work, struct tty_struct, hangup_work);
756
Peter Hurleyf91e2592013-03-06 07:20:56 -0500757 __tty_hangup(tty, 0);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200758}
759
Alan Coxaf9b8972006-08-27 01:24:01 -0700760/**
761 * tty_hangup - trigger a hangup event
762 * @tty: tty to hangup
763 *
764 * A carrier loss (virtual or otherwise) has occurred on this like
765 * schedule a hangup sequence to run after this event.
766 */
767
Alan Cox37bdfb02008-02-08 04:18:47 -0800768void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770#ifdef TTY_DEBUG_HANGUP
771 char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
773#endif
774 schedule_work(&tty->hangup_work);
775}
776
777EXPORT_SYMBOL(tty_hangup);
778
Alan Coxaf9b8972006-08-27 01:24:01 -0700779/**
780 * tty_vhangup - process vhangup
781 * @tty: tty to hangup
782 *
783 * The user has asked via system call for the terminal to be hung up.
784 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200785 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -0700786 */
787
Alan Cox37bdfb02008-02-08 04:18:47 -0800788void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790#ifdef TTY_DEBUG_HANGUP
791 char buf[64];
792
793 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
794#endif
Peter Hurleyf91e2592013-03-06 07:20:56 -0500795 __tty_hangup(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
Alan Cox37bdfb02008-02-08 04:18:47 -0800797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798EXPORT_SYMBOL(tty_vhangup);
799
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200800
Alan Coxaf9b8972006-08-27 01:24:01 -0700801/**
Alan Cox2cb59982008-10-13 10:40:30 +0100802 * tty_vhangup_self - process vhangup for own ctty
803 *
804 * Perform a vhangup on the current controlling tty
805 */
806
807void tty_vhangup_self(void)
808{
809 struct tty_struct *tty;
810
Alan Cox2cb59982008-10-13 10:40:30 +0100811 tty = get_current_tty();
812 if (tty) {
813 tty_vhangup(tty);
814 tty_kref_put(tty);
815 }
Alan Cox2cb59982008-10-13 10:40:30 +0100816}
817
818/**
Peter Hurleyf91e2592013-03-06 07:20:56 -0500819 * tty_vhangup_session - hangup session leader exit
820 * @tty: tty to hangup
821 *
822 * The session leader is exiting and hanging up its controlling terminal.
823 * Every process in the foreground process group is signalled SIGHUP.
824 *
825 * We do this synchronously so that when the syscall returns the process
826 * is complete. That guarantee is necessary for security reasons.
827 */
828
Changlong Xie44a459f2013-03-20 08:42:04 +0800829static void tty_vhangup_session(struct tty_struct *tty)
Peter Hurleyf91e2592013-03-06 07:20:56 -0500830{
831#ifdef TTY_DEBUG_HANGUP
832 char buf[64];
833
834 printk(KERN_DEBUG "%s vhangup session...\n", tty_name(tty, buf));
835#endif
836 __tty_hangup(tty, 1);
837}
838
839/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700840 * tty_hung_up_p - was tty hung up
841 * @filp: file pointer of tty
842 *
843 * Return true if the tty has been subject to a vhangup or a carrier
844 * loss
845 */
846
Alan Cox37bdfb02008-02-08 04:18:47 -0800847int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 return (filp->f_op == &hung_up_tty_fops);
850}
851
852EXPORT_SYMBOL(tty_hung_up_p);
853
Alan Coxaf9b8972006-08-27 01:24:01 -0700854/**
855 * disassociate_ctty - disconnect controlling tty
856 * @on_exit: true if exiting so need to "hang up" the session
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700858 * This function is typically called only by the session leader, when
859 * it wants to disassociate itself from its controlling tty.
860 *
861 * It performs the following functions:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
863 * (2) Clears the tty from being controlling the session
864 * (3) Clears the controlling tty for all processes in the
865 * session group.
866 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700867 * The argument on_exit is set to 1 if called when a process is
868 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
869 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800870 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200871 * BTM is taken for hysterical raisins, and held when
872 * called from no_tty().
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800873 * tty_mutex is taken to protect tty
874 * ->siglock is taken to protect ->signal/->sighand
875 * tasklist_lock is taken to walk process list for sessions
876 * ->siglock is taken to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879void disassociate_ctty(int on_exit)
880{
881 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Alan Cox5ec93d12009-11-30 13:18:45 +0000883 if (!current->signal->leader)
884 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800886 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (tty) {
Peter Hurleyf91e2592013-03-06 07:20:56 -0500888 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) {
889 tty_vhangup_session(tty);
890 } else {
891 struct pid *tty_pgrp = tty_get_pgrp(tty);
892 if (tty_pgrp) {
893 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
Oleg Nesterov03e12612013-09-15 17:50:26 +0200894 if (!on_exit)
895 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
Peter Hurleyf91e2592013-03-06 07:20:56 -0500896 put_pid(tty_pgrp);
897 }
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200898 }
Alan Cox452a00d2008-10-13 10:39:13 +0100899 tty_kref_put(tty);
Peter Hurleyf91e2592013-03-06 07:20:56 -0500900
Eric W. Biederman680a9672007-02-12 00:52:52 -0800901 } else if (on_exit) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800902 struct pid *old_pgrp;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800903 spin_lock_irq(&current->sighand->siglock);
904 old_pgrp = current->signal->tty_old_pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800905 current->signal->tty_old_pgrp = NULL;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800906 spin_unlock_irq(&current->sighand->siglock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800907 if (old_pgrp) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800908 kill_pgrp(old_pgrp, SIGHUP, on_exit);
909 kill_pgrp(old_pgrp, SIGCONT, on_exit);
910 put_pid(old_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800915 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700916 put_pid(current->signal->tty_old_pgrp);
Randy Dunlap23cac8d2007-02-20 13:58:05 -0800917 current->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800918
Chen Tingjiec70dbb12014-04-15 11:52:51 +0800919 tty = tty_kref_get(current->signal->tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800920 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -0700921 unsigned long flags;
922 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800923 put_pid(tty->session);
924 put_pid(tty->pgrp);
925 tty->session = NULL;
926 tty->pgrp = NULL;
Alan Cox47f86832008-04-30 00:53:30 -0700927 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +0100928 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800929 } else {
930#ifdef TTY_DEBUG_HANGUP
931 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
932 " = NULL", tty);
933#endif
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Chen Tingjiec70dbb12014-04-15 11:52:51 +0800936 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* Now clear signal->tty under the lock */
938 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800939 session_clear_tty(task_session(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700943/**
944 *
945 * no_tty - Ensure the current process does not have a controlling tty
946 */
947void no_tty(void)
948{
Alan Cox3af502b2012-05-03 22:21:53 +0100949 /* FIXME: Review locking here. The tty_lock never covered any race
950 between a new association and proc_clear_tty but possible we need
951 to protect against this anyway */
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700952 struct task_struct *tsk = current;
Alan Cox5ec93d12009-11-30 13:18:45 +0000953 disassociate_ctty(0);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700954 proc_clear_tty(tsk);
955}
956
Alan Coxaf9b8972006-08-27 01:24:01 -0700957
958/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200959 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700960 * @tty: tty to stop
961 *
Peter Hurley01adc802014-09-10 15:06:32 -0400962 * Perform flow control to the driver. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -0700963 * on an already stopped device and will not re-call the driver
964 * method.
965 *
966 * This functionality is used by both the line disciplines for
967 * halting incoming flow and by the driver. It may therefore be
968 * called from any context, may be under the tty atomic_write_lock
969 * but not always.
970 *
971 * Locking:
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400972 * flow_lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700973 */
974
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400975void __stop_tty(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400977 if (tty->stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return;
979 tty->stopped = 1;
Alan Coxf34d7a52008-04-30 00:54:13 -0700980 if (tty->ops->stop)
Peter Hurleyc961bfb2014-11-05 12:26:25 -0500981 tty->ops->stop(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400984void stop_tty(struct tty_struct *tty)
985{
986 unsigned long flags;
987
988 spin_lock_irqsave(&tty->flow_lock, flags);
989 __stop_tty(tty);
990 spin_unlock_irqrestore(&tty->flow_lock, flags);
991}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992EXPORT_SYMBOL(stop_tty);
993
Alan Coxaf9b8972006-08-27 01:24:01 -0700994/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200995 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700996 * @tty: tty to start
997 *
Peter Hurley01adc802014-09-10 15:06:32 -0400998 * Start a tty that has been stopped if at all possible. If this
999 * tty was previous stopped and is now being started, the driver
1000 * start method is invoked and the line discipline woken.
Alan Coxaf9b8972006-08-27 01:24:01 -07001001 *
1002 * Locking:
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001003 * flow_lock
Alan Coxaf9b8972006-08-27 01:24:01 -07001004 */
1005
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001006void __start_tty(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001008 if (!tty->stopped || tty->flow_stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return;
1010 tty->stopped = 0;
Alan Coxf34d7a52008-04-30 00:54:13 -07001011 if (tty->ops->start)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05001012 tty->ops->start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001016void start_tty(struct tty_struct *tty)
1017{
1018 unsigned long flags;
1019
1020 spin_lock_irqsave(&tty->flow_lock, flags);
1021 __start_tty(tty);
1022 spin_unlock_irqrestore(&tty->flow_lock, flags);
1023}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024EXPORT_SYMBOL(start_tty);
1025
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001026static void tty_update_time(struct timespec *time)
1027{
Jiri Slabyf0bf0bd2015-02-27 18:40:31 +01001028 unsigned long sec = get_seconds();
Greg Kroah-Hartmanfbf47632015-03-26 23:10:27 +01001029
1030 /*
1031 * We only care if the two values differ in anything other than the
1032 * lower three bits (i.e every 8 seconds). If so, then we can update
1033 * the time of the tty device, otherwise it could be construded as a
1034 * security leak to let userspace know the exact timing of the tty.
1035 */
1036 if ((sec ^ time->tv_sec) & ~7)
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001037 time->tv_sec = sec;
1038}
1039
Alan Coxaf9b8972006-08-27 01:24:01 -07001040/**
1041 * tty_read - read method for tty device files
1042 * @file: pointer to tty file
1043 * @buf: user buffer
1044 * @count: size of user buffer
1045 * @ppos: unused
1046 *
1047 * Perform the read system call function on this terminal device. Checks
1048 * for hung up devices before calling the line discipline method.
1049 *
1050 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -07001051 * Locks the line discipline internally while needed. Multiple
1052 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -07001053 */
1054
Alan Cox37bdfb02008-02-08 04:18:47 -08001055static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 loff_t *ppos)
1057{
1058 int i;
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001059 struct inode *inode = file_inode(file);
Nick Piggind996b622010-08-18 04:37:36 +10001060 struct tty_struct *tty = file_tty(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct tty_ldisc *ld;
1062
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001063 if (tty_paranoia_check(tty, inode, "tty_read"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return -EIO;
1065 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1066 return -EIO;
1067
1068 /* We want to wait for the line discipline to sort out in this
1069 situation */
1070 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001071 if (ld->ops->read)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05001072 i = ld->ops->read(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 else
1074 i = -EIO;
1075 tty_ldisc_deref(ld);
Jiri Slabyb0de59b2013-02-15 15:25:05 +01001076
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001077 if (i > 0)
1078 tty_update_time(&inode->i_atime);
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return i;
1081}
1082
Peter Hurley136d5252014-09-10 15:06:34 -04001083static void tty_write_unlock(struct tty_struct *tty)
Alan Cox9c1729d2007-07-15 23:39:43 -07001084{
1085 mutex_unlock(&tty->atomic_write_lock);
Davide Libenzi4b194492009-03-31 15:24:24 -07001086 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Alan Cox9c1729d2007-07-15 23:39:43 -07001087}
1088
Peter Hurley136d5252014-09-10 15:06:34 -04001089static int tty_write_lock(struct tty_struct *tty, int ndelay)
Alan Cox9c1729d2007-07-15 23:39:43 -07001090{
1091 if (!mutex_trylock(&tty->atomic_write_lock)) {
1092 if (ndelay)
1093 return -EAGAIN;
1094 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1095 return -ERESTARTSYS;
1096 }
1097 return 0;
1098}
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100/*
1101 * Split writes up in sane blocksizes to avoid
1102 * denial-of-service type attacks
1103 */
1104static inline ssize_t do_tty_write(
1105 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1106 struct tty_struct *tty,
1107 struct file *file,
1108 const char __user *buf,
1109 size_t count)
1110{
Alan Cox9c1729d2007-07-15 23:39:43 -07001111 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -08001113
Alan Cox9c1729d2007-07-15 23:39:43 -07001114 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1115 if (ret < 0)
1116 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /*
1119 * We chunk up writes into a temporary buffer. This
1120 * simplifies low-level drivers immensely, since they
1121 * don't have locking issues and user mode accesses.
1122 *
1123 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1124 * big chunk-size..
1125 *
1126 * The default chunk-size is 2kB, because the NTTY
1127 * layer has problems with bigger chunks. It will
1128 * claim to be able to handle more characters than
1129 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -07001130 *
1131 * FIXME: This can probably go away now except that 64K chunks
1132 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
1134 chunk = 2048;
1135 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1136 chunk = 65536;
1137 if (count < chunk)
1138 chunk = count;
1139
Ingo Molnar70522e12006-03-23 03:00:31 -08001140 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (tty->write_cnt < chunk) {
Jason Wessel402fda92008-10-13 10:45:36 +01001142 unsigned char *buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 if (chunk < 1024)
1145 chunk = 1024;
1146
Jason Wessel402fda92008-10-13 10:45:36 +01001147 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1148 if (!buf_chunk) {
Alan Cox9c1729d2007-07-15 23:39:43 -07001149 ret = -ENOMEM;
1150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 kfree(tty->write_buf);
1153 tty->write_cnt = chunk;
Jason Wessel402fda92008-10-13 10:45:36 +01001154 tty->write_buf = buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
1157 /* Do the write .. */
1158 for (;;) {
1159 size_t size = count;
1160 if (size > chunk)
1161 size = chunk;
1162 ret = -EFAULT;
1163 if (copy_from_user(tty->write_buf, buf, size))
1164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (ret <= 0)
1167 break;
1168 written += ret;
1169 buf += ret;
1170 count -= ret;
1171 if (!count)
1172 break;
1173 ret = -ERESTARTSYS;
1174 if (signal_pending(current))
1175 break;
1176 cond_resched();
1177 }
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001178 if (written) {
1179 tty_update_time(&file_inode(file)->i_mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 ret = written;
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001181 }
Alan Cox9c1729d2007-07-15 23:39:43 -07001182out:
1183 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return ret;
1185}
1186
Alan Cox95f9bfc2008-10-13 10:39:23 +01001187/**
1188 * tty_write_message - write a message to a certain tty, not just the console.
1189 * @tty: the destination tty_struct
1190 * @msg: the message to write
1191 *
1192 * This is used for messages that need to be redirected to a specific tty.
1193 * We don't put it into the syslog queue right now maybe in the future if
1194 * really needed.
1195 *
Arnd Bergmannec79d602010-06-01 22:53:01 +02001196 * We must still hold the BTM and test the CLOSING flag for the moment.
Alan Cox95f9bfc2008-10-13 10:39:23 +01001197 */
1198
1199void tty_write_message(struct tty_struct *tty, char *msg)
1200{
Alan Cox95f9bfc2008-10-13 10:39:23 +01001201 if (tty) {
1202 mutex_lock(&tty->atomic_write_lock);
Alan Cox89c8d912012-08-08 16:30:13 +01001203 tty_lock(tty);
Peter Hurley04980702014-11-05 12:12:52 -05001204 if (tty->ops->write && tty->count > 0) {
Alan Cox89c8d912012-08-08 16:30:13 +01001205 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +01001206 tty->ops->write(tty, msg, strlen(msg));
Alan Coxeeb89d92009-11-30 13:18:29 +00001207 } else
Alan Cox89c8d912012-08-08 16:30:13 +01001208 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +01001209 tty_write_unlock(tty);
1210 }
Alan Cox95f9bfc2008-10-13 10:39:23 +01001211 return;
1212}
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Alan Coxaf9b8972006-08-27 01:24:01 -07001215/**
1216 * tty_write - write method for tty device file
1217 * @file: tty file pointer
1218 * @buf: user data to write
1219 * @count: bytes to write
1220 * @ppos: unused
1221 *
1222 * Write data to a tty device via the line discipline.
1223 *
1224 * Locking:
1225 * Locks the line discipline as required
1226 * Writes to the tty driver are serialized by the atomic_write_lock
1227 * and are then processed in chunks to the device. The line discipline
Joe Petersona88a69c2009-01-02 13:40:53 +00001228 * write method will not be invoked in parallel for each device.
Alan Coxaf9b8972006-08-27 01:24:01 -07001229 */
1230
Alan Cox37bdfb02008-02-08 04:18:47 -08001231static ssize_t tty_write(struct file *file, const char __user *buf,
1232 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Nick Piggind996b622010-08-18 04:37:36 +10001234 struct tty_struct *tty = file_tty(file);
1235 struct tty_ldisc *ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ssize_t ret;
Alan Cox37bdfb02008-02-08 04:18:47 -08001237
Al Viro6131ffa2013-02-27 16:59:05 -05001238 if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001240 if (!tty || !tty->ops->write ||
Alan Cox37bdfb02008-02-08 04:18:47 -08001241 (test_bit(TTY_IO_ERROR, &tty->flags)))
1242 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001243 /* Short term debug to catch buggy drivers */
1244 if (tty->ops->write_room == NULL)
1245 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1246 tty->driver->name);
Alan Cox37bdfb02008-02-08 04:18:47 -08001247 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001248 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 ret = -EIO;
1250 else
Alan Coxa352def2008-07-16 21:53:12 +01001251 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 tty_ldisc_deref(ld);
1253 return ret;
1254}
1255
Alan Cox37bdfb02008-02-08 04:18:47 -08001256ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1257 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 struct file *p = NULL;
1260
1261 spin_lock(&redirect_lock);
Al Virocb0942b2012-08-27 14:48:26 -04001262 if (redirect)
1263 p = get_file(redirect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 spin_unlock(&redirect_lock);
1265
1266 if (p) {
1267 ssize_t res;
1268 res = vfs_write(p, buf, count, &p->f_pos);
1269 fput(p);
1270 return res;
1271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return tty_write(file, buf, count, ppos);
1273}
1274
Peter Hurley136d5252014-09-10 15:06:34 -04001275/**
1276 * tty_send_xchar - send priority character
1277 *
1278 * Send a high priority character to the tty even if stopped
1279 *
1280 * Locking: none for xchar method, write ordering for write method.
1281 */
1282
1283int tty_send_xchar(struct tty_struct *tty, char ch)
1284{
1285 int was_stopped = tty->stopped;
1286
1287 if (tty->ops->send_xchar) {
1288 tty->ops->send_xchar(tty, ch);
1289 return 0;
1290 }
1291
1292 if (tty_write_lock(tty, 0) < 0)
1293 return -ERESTARTSYS;
1294
1295 if (was_stopped)
1296 start_tty(tty);
1297 tty->ops->write(tty, &ch, 1);
1298 if (was_stopped)
1299 stop_tty(tty);
1300 tty_write_unlock(tty);
1301 return 0;
1302}
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304static char ptychar[] = "pqrstuvwxyzabcde";
1305
Alan Coxaf9b8972006-08-27 01:24:01 -07001306/**
1307 * pty_line_name - generate name for a pty
1308 * @driver: the tty driver in use
1309 * @index: the minor number
1310 * @p: output buffer of at least 6 bytes
1311 *
1312 * Generate a name from a driver reference and write it to the output
1313 * buffer.
1314 *
1315 * Locking: None
1316 */
1317static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 int i = index + driver->name_base;
1320 /* ->name is initialized to "ttyp", but "tty" is expected */
1321 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08001322 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1323 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
Alan Coxaf9b8972006-08-27 01:24:01 -07001326/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001327 * tty_line_name - generate name for a tty
Alan Coxaf9b8972006-08-27 01:24:01 -07001328 * @driver: the tty driver in use
1329 * @index: the minor number
1330 * @p: output buffer of at least 7 bytes
1331 *
1332 * Generate a name from a driver reference and write it to the output
Greg Kroah-Hartman5c0a2452014-02-22 14:31:04 -08001333 * buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07001334 *
1335 * Locking: None
1336 */
Hannes Reinecke723abd82014-02-27 12:30:51 +01001337static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Jiri Slaby0019b402012-08-08 22:26:43 +02001339 if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
Hannes Reinecke723abd82014-02-27 12:30:51 +01001340 return sprintf(p, "%s", driver->name);
Jiri Slaby0019b402012-08-08 22:26:43 +02001341 else
Hannes Reinecke723abd82014-02-27 12:30:51 +01001342 return sprintf(p, "%s%d", driver->name,
1343 index + driver->name_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Alan Cox99f1fe12008-10-13 10:42:00 +01001346/**
1347 * tty_driver_lookup_tty() - find an existing tty, if any
1348 * @driver: the driver for the tty
1349 * @idx: the minor number
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001350 *
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001351 * Return the tty, if found. If not found, return NULL or ERR_PTR() if the
1352 * driver lookup() method returns an error.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001353 *
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001354 * Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001355 */
Jason Wessela47d5452009-01-02 13:43:04 +00001356static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001357 struct inode *inode, int idx)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001358{
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001359 struct tty_struct *tty;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001360
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001361 if (driver->ops->lookup)
1362 tty = driver->ops->lookup(driver, inode, idx);
1363 else
1364 tty = driver->ttys[idx];
1365
1366 if (!IS_ERR(tty))
1367 tty_kref_get(tty);
1368 return tty;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001369}
1370
Alan Cox99f1fe12008-10-13 10:42:00 +01001371/**
Alan Coxbf970ee2008-10-13 10:42:39 +01001372 * tty_init_termios - helper for termios setup
1373 * @tty: the tty to set up
1374 *
1375 * Initialise the termios structures for this tty. Thus runs under
1376 * the tty_mutex currently so we can be relaxed about ordering.
1377 */
1378
1379int tty_init_termios(struct tty_struct *tty)
1380{
Alan Coxfe6e29f2008-10-13 10:44:08 +01001381 struct ktermios *tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001382 int idx = tty->index;
1383
Alan Cox36b3c072012-07-17 17:06:57 +01001384 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1385 tty->termios = tty->driver->init_termios;
1386 else {
1387 /* Check for lazy saved data */
1388 tp = tty->driver->termios[idx];
1389 if (tp != NULL)
1390 tty->termios = *tp;
1391 else
1392 tty->termios = tty->driver->init_termios;
Alan Coxbf970ee2008-10-13 10:42:39 +01001393 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001394 /* Compatibility until drivers always set this */
Alan Coxadc8d742012-07-14 15:31:47 +01001395 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1396 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001397 return 0;
1398}
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001399EXPORT_SYMBOL_GPL(tty_init_termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001400
Jiri Slaby66d450e2012-01-30 21:14:28 +01001401int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1402{
1403 int ret = tty_init_termios(tty);
1404 if (ret)
1405 return ret;
1406
1407 tty_driver_kref_get(driver);
1408 tty->count++;
1409 driver->ttys[tty->index] = tty;
1410 return 0;
1411}
1412EXPORT_SYMBOL_GPL(tty_standard_install);
1413
Alan Coxbf970ee2008-10-13 10:42:39 +01001414/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001415 * tty_driver_install_tty() - install a tty entry in the driver
1416 * @driver: the driver for the tty
1417 * @tty: the tty
1418 *
1419 * Install a tty object into the driver tables. The tty->index field
Alan Coxbf970ee2008-10-13 10:42:39 +01001420 * will be set by the time this is called. This method is responsible
1421 * for ensuring any need additional structures are allocated and
1422 * configured.
Alan Cox8b0a88d2008-10-13 10:42:19 +01001423 *
1424 * Locking: tty_mutex for now
1425 */
1426static int tty_driver_install_tty(struct tty_driver *driver,
1427 struct tty_struct *tty)
1428{
Jiri Slaby66d450e2012-01-30 21:14:28 +01001429 return driver->ops->install ? driver->ops->install(driver, tty) :
1430 tty_standard_install(driver, tty);
Alan Cox8b0a88d2008-10-13 10:42:19 +01001431}
1432
1433/**
1434 * tty_driver_remove_tty() - remove a tty from the driver tables
1435 * @driver: the driver for the tty
1436 * @idx: the minor number
1437 *
1438 * Remvoe a tty object from the driver tables. The tty->index field
1439 * will be set by the time this is called.
1440 *
1441 * Locking: tty_mutex for now
1442 */
Jiri Slaby24d406a2011-08-10 14:59:28 +02001443void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +01001444{
1445 if (driver->ops->remove)
1446 driver->ops->remove(driver, tty);
1447 else
1448 driver->ttys[tty->index] = NULL;
1449}
1450
1451/*
1452 * tty_reopen() - fast re-open of an open tty
1453 * @tty - the tty to open
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001454 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001455 * Return 0 on success, -errno on error.
Peter Hurley5d93e742014-11-05 12:12:47 -05001456 * Re-opens on master ptys are not allowed and return -EIO.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001457 *
Peter Hurley5d93e742014-11-05 12:12:47 -05001458 * Locking: Caller must hold tty_lock
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001459 */
Alan Cox99f1fe12008-10-13 10:42:00 +01001460static int tty_reopen(struct tty_struct *tty)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001461{
1462 struct tty_driver *driver = tty->driver;
1463
Peter Hurley216030e2014-11-05 12:12:48 -05001464 if (!tty->count)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001465 return -EIO;
1466
1467 if (driver->type == TTY_DRIVER_TYPE_PTY &&
Peter Hurley5d93e742014-11-05 12:12:47 -05001468 driver->subtype == PTY_TYPE_MASTER)
1469 return -EIO;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001470
Peter Hurley86f2c002014-12-30 10:39:25 -05001471 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1472 return -EBUSY;
1473
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001474 tty->count++;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001475
Peter Hurley36697522013-06-15 07:04:48 -04001476 WARN_ON(!tty->ldisc);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001477
1478 return 0;
1479}
1480
Alan Coxaf9b8972006-08-27 01:24:01 -07001481/**
Alan Coxd81ed102008-10-13 10:41:42 +01001482 * tty_init_dev - initialise a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001483 * @driver: tty driver we are opening a device on
1484 * @idx: device index
Alan Cox15582d32008-10-13 10:41:03 +01001485 * @ret_tty: returned tty structure
Alan Coxaf9b8972006-08-27 01:24:01 -07001486 *
1487 * Prepare a tty device. This may not be a "new" clean device but
1488 * could also be an active device. The pty drivers require special
1489 * handling because of this.
1490 *
1491 * Locking:
1492 * The function is called under the tty_mutex, which
1493 * protects us from the tty struct or driver itself going away.
1494 *
1495 * On exit the tty device has the line discipline attached and
1496 * a reference count of 1. If a pair was created for pty/tty use
1497 * and the other was a pty master then it too has a reference count of 1.
1498 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001500 * failed open. The new code protects the open with a mutex, so it's
1501 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 * relaxed for the (most common) case of reopening a tty.
1503 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001504
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001505struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Alan Coxbf970ee2008-10-13 10:42:39 +01001507 struct tty_struct *tty;
Alan Cox73ec06f2008-10-13 10:42:29 +01001508 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /*
1511 * First time open is complex, especially for PTY devices.
1512 * This code guarantees that either everything succeeds and the
1513 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001514 * and the allocated memory released. (Except that the termios
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 * and locked termios may be retained.)
1516 */
1517
Alan Cox73ec06f2008-10-13 10:42:29 +01001518 if (!try_module_get(driver->owner))
1519 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02001521 tty = alloc_tty_struct(driver, idx);
Jiri Slabyd5543502011-03-23 10:48:32 +01001522 if (!tty) {
1523 retval = -ENOMEM;
1524 goto err_module_put;
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Alan Cox89c8d912012-08-08 16:30:13 +01001527 tty_lock(tty);
Alan Cox73ec06f2008-10-13 10:42:29 +01001528 retval = tty_driver_install_tty(driver, tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001529 if (retval < 0)
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001530 goto err_deinit_tty;
Alan Cox8b0a88d2008-10-13 10:42:19 +01001531
Jiri Slaby04831dc2012-06-04 13:35:36 +02001532 if (!tty->port)
1533 tty->port = driver->ports[idx];
1534
Jiri Slaby5d4121c2012-08-17 14:27:52 +02001535 WARN_RATELIMIT(!tty->port,
1536 "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
1537 __func__, tty->driver->name);
1538
Jiri Slaby967fab62012-10-18 22:26:46 +02001539 tty->port->itty = tty;
1540
Alan Cox37bdfb02008-02-08 04:18:47 -08001541 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001543 * If we fail here just call release_tty to clean up. No need
1544 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 */
Alan Coxbf970ee2008-10-13 10:42:39 +01001546 retval = tty_ldisc_setup(tty, tty->link);
Alan Cox01e1abb2008-07-22 11:16:55 +01001547 if (retval)
Jiri Slabyd5543502011-03-23 10:48:32 +01001548 goto err_release_tty;
Alan Cox89c8d912012-08-08 16:30:13 +01001549 /* Return the tty locked so that it cannot vanish under the caller */
Alan Cox73ec06f2008-10-13 10:42:29 +01001550 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001552err_deinit_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001553 tty_unlock(tty);
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001554 deinitialize_tty_struct(tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001555 free_tty_struct(tty);
1556err_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 module_put(driver->owner);
Jiri Slabyd5543502011-03-23 10:48:32 +01001558 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001560 /* call the tty release_tty routine to clean out this slot */
Jiri Slabyd5543502011-03-23 10:48:32 +01001561err_release_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001562 tty_unlock(tty);
Manuel Zerpies5a3c6b22011-06-16 14:07:22 +02001563 printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, "
Akinobu Mita40509142006-09-29 02:01:27 -07001564 "clearing slot %d\n", idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001565 release_tty(tty, idx);
Alan Cox73ec06f2008-10-13 10:42:29 +01001566 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Alan Coxfeebed62008-10-13 10:41:30 +01001569void tty_free_termios(struct tty_struct *tty)
1570{
1571 struct ktermios *tp;
1572 int idx = tty->index;
Alan Cox36b3c072012-07-17 17:06:57 +01001573
1574 /* If the port is going to reset then it has no termios to save */
1575 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1576 return;
1577
1578 /* Stash the termios data */
1579 tp = tty->driver->termios[idx];
1580 if (tp == NULL) {
1581 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1582 if (tp == NULL) {
1583 pr_warn("tty: no memory to save termios state.\n");
1584 return;
1585 }
Dan Carpenter4ac5d702012-07-24 12:51:52 +01001586 tty->driver->termios[idx] = tp;
Alan Coxfeebed62008-10-13 10:41:30 +01001587 }
Alan Cox36b3c072012-07-17 17:06:57 +01001588 *tp = tty->termios;
Alan Coxfeebed62008-10-13 10:41:30 +01001589}
1590EXPORT_SYMBOL(tty_free_termios);
1591
Peter Hurleya2965b72013-03-11 16:44:35 -04001592/**
Peter Hurley949aa642014-11-05 12:12:57 -05001593 * tty_flush_works - flush all works of a tty/pty pair
1594 * @tty: tty device to flush works for (or either end of a pty pair)
Peter Hurleya2965b72013-03-11 16:44:35 -04001595 *
Peter Hurley949aa642014-11-05 12:12:57 -05001596 * Sync flush all works belonging to @tty (and the 'other' tty).
Peter Hurleya2965b72013-03-11 16:44:35 -04001597 */
1598static void tty_flush_works(struct tty_struct *tty)
1599{
1600 flush_work(&tty->SAK_work);
1601 flush_work(&tty->hangup_work);
Peter Hurley949aa642014-11-05 12:12:57 -05001602 if (tty->link) {
1603 flush_work(&tty->link->SAK_work);
1604 flush_work(&tty->link->hangup_work);
1605 }
Peter Hurleya2965b72013-03-11 16:44:35 -04001606}
Alan Coxfeebed62008-10-13 10:41:30 +01001607
Alan Coxaf9b8972006-08-27 01:24:01 -07001608/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001609 * release_one_tty - release tty structure memory
Alan Cox9c9f4de2008-10-13 10:37:26 +01001610 * @kref: kref of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001611 *
1612 * Releases memory associated with a tty structure, and clears out the
1613 * driver table slots. This function is called when a device is no longer
1614 * in use. It also gets called when setup of a device fails.
1615 *
1616 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -07001617 * takes the file list lock internally when working on the list
1618 * of ttys that the driver keeps.
Alan Coxb50989d2009-09-19 13:13:22 -07001619 *
1620 * This method gets called from a work queue so that the driver private
Dave Youngf278a2f2009-09-27 16:00:42 +00001621 * cleanup ops can sleep (needed for USB at least)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 */
Alan Coxb50989d2009-09-19 13:13:22 -07001623static void release_one_tty(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Alan Coxb50989d2009-09-19 13:13:22 -07001625 struct tty_struct *tty =
1626 container_of(work, struct tty_struct, hangup_work);
Alan Cox6f967f72008-10-13 10:37:36 +01001627 struct tty_driver *driver = tty->driver;
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001628 struct module *owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Dave Youngf278a2f2009-09-27 16:00:42 +00001630 if (tty->ops->cleanup)
1631 tty->ops->cleanup(tty);
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 tty->magic = 0;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001634 tty_driver_kref_put(driver);
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001635 module_put(owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001636
Nick Pigginee2ffa02010-08-18 04:37:35 +10001637 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 list_del_init(&tty->tty_files);
Nick Pigginee2ffa02010-08-18 04:37:35 +10001639 spin_unlock(&tty_files_lock);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001640
Oleg Nesterov6da8d862010-04-02 18:05:12 +02001641 put_pid(tty->pgrp);
1642 put_pid(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 free_tty_struct(tty);
1644}
1645
Alan Coxb50989d2009-09-19 13:13:22 -07001646static void queue_release_one_tty(struct kref *kref)
1647{
1648 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Dave Youngf278a2f2009-09-27 16:00:42 +00001649
Alan Coxb50989d2009-09-19 13:13:22 -07001650 /* The hangup queue is now free so we can reuse it rather than
1651 waste a chunk of memory for each port */
1652 INIT_WORK(&tty->hangup_work, release_one_tty);
1653 schedule_work(&tty->hangup_work);
1654}
1655
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001656/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001657 * tty_kref_put - release a tty kref
1658 * @tty: tty device
1659 *
1660 * Release a reference to a tty device and if need be let the kref
1661 * layer destruct the object for us
1662 */
1663
1664void tty_kref_put(struct tty_struct *tty)
1665{
1666 if (tty)
Alan Coxb50989d2009-09-19 13:13:22 -07001667 kref_put(&tty->kref, queue_release_one_tty);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001668}
1669EXPORT_SYMBOL(tty_kref_put);
1670
1671/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001672 * release_tty - release tty structure memory
1673 *
1674 * Release both @tty and a possible linked partner (think pty pair),
1675 * and decrement the refcount of the backing module.
1676 *
1677 * Locking:
Alan Coxd1552552012-07-27 18:02:54 +01001678 * tty_mutex
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001679 * takes the file list lock internally when working on the list
1680 * of ttys that the driver keeps.
Alan Cox9c9f4de2008-10-13 10:37:26 +01001681 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001682 */
1683static void release_tty(struct tty_struct *tty, int idx)
1684{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001685 /* This should always be true but check for the moment */
1686 WARN_ON(tty->index != idx);
Alan Coxd1552552012-07-27 18:02:54 +01001687 WARN_ON(!mutex_is_locked(&tty_mutex));
Alan Cox36b3c072012-07-17 17:06:57 +01001688 if (tty->ops->shutdown)
1689 tty->ops->shutdown(tty);
1690 tty_free_termios(tty);
1691 tty_driver_remove_tty(tty->driver, tty);
Jiri Slaby967fab62012-10-18 22:26:46 +02001692 tty->port->itty = NULL;
Peter Hurley64e377d2013-06-15 09:01:00 -04001693 if (tty->link)
1694 tty->link->port->itty = NULL;
Peter Hurley4f98d462013-03-11 16:44:34 -04001695 cancel_work_sync(&tty->port->buf.work);
Alan Cox36b3c072012-07-17 17:06:57 +01001696
Markus Elfringa211b1a2014-11-21 13:42:29 +01001697 tty_kref_put(tty->link);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001698 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001699}
1700
Alan Coxeeb89d92009-11-30 13:18:29 +00001701/**
Jiri Slaby955787ca2011-11-11 10:47:23 +01001702 * tty_release_checks - check a tty before real release
1703 * @tty: tty to check
1704 * @o_tty: link of @tty (if any)
1705 * @idx: index of the tty
1706 *
1707 * Performs some paranoid checking before true release of the @tty.
1708 * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1709 */
Peter Hurley359b9fb2014-11-05 12:12:59 -05001710static int tty_release_checks(struct tty_struct *tty, int idx)
Jiri Slaby955787ca2011-11-11 10:47:23 +01001711{
1712#ifdef TTY_PARANOIA_CHECK
1713 if (idx < 0 || idx >= tty->driver->num) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001714 printk(KERN_DEBUG "%s: bad idx when trying to free (%s)\n",
1715 __func__, tty->name);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001716 return -1;
1717 }
1718
1719 /* not much to check for devpts */
1720 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1721 return 0;
1722
1723 if (tty != tty->driver->ttys[idx]) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001724 printk(KERN_DEBUG "%s: driver.table[%d] not tty for (%s)\n",
1725 __func__, idx, tty->name);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001726 return -1;
1727 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001728 if (tty->driver->other) {
Peter Hurley359b9fb2014-11-05 12:12:59 -05001729 struct tty_struct *o_tty = tty->link;
1730
Jiri Slaby955787ca2011-11-11 10:47:23 +01001731 if (o_tty != tty->driver->other->ttys[idx]) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001732 printk(KERN_DEBUG "%s: other->table[%d] not o_tty for (%s)\n",
1733 __func__, idx, tty->name);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001734 return -1;
1735 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001736 if (o_tty->link != tty) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001737 printk(KERN_DEBUG "%s: bad pty pointers\n", __func__);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001738 return -1;
1739 }
1740 }
1741#endif
1742 return 0;
1743}
1744
1745/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001746 * tty_release - vfs callback for close
1747 * @inode: inode of tty
1748 * @filp: file pointer for handle to tty
1749 *
1750 * Called the last time each file handle is closed that references
1751 * this tty. There may however be several such references.
1752 *
1753 * Locking:
1754 * Takes bkl. See tty_release_dev
1755 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 * Even releasing the tty structures is a tricky business.. We have
1757 * to be very careful that the structures are all released at the
1758 * same time, as interrupts might otherwise get the wrong pointers.
1759 *
1760 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1761 * lead to double frees or releasing memory still in use.
1762 */
Alan Coxeeb89d92009-11-30 13:18:29 +00001763
1764int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Nick Piggind996b622010-08-18 04:37:36 +10001766 struct tty_struct *tty = file_tty(filp);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001767 struct tty_struct *o_tty = NULL;
1768 int do_sleep, final;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 int idx;
1770 char buf[64];
Peter Hurley37b16452014-10-16 13:51:30 -04001771 long timeout = 0;
Peter Hurley494c1ea2014-10-16 13:54:36 -04001772 int once = 1;
Alan Cox37bdfb02008-02-08 04:18:47 -08001773
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001774 if (tty_paranoia_check(tty, inode, __func__))
Alan Coxeeb89d92009-11-30 13:18:29 +00001775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Alan Cox89c8d912012-08-08 16:30:13 +01001777 tty_lock(tty);
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001778 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Arnd Bergmannec79d602010-06-01 22:53:01 +02001780 __tty_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 idx = tty->index;
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001783 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1784 tty->driver->subtype == PTY_TYPE_MASTER)
1785 o_tty = tty->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Peter Hurley359b9fb2014-11-05 12:12:59 -05001787 if (tty_release_checks(tty, idx)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001788 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001793 printk(KERN_DEBUG "%s: %s (tty count=%d)...\n", __func__,
1794 tty_name(tty, buf), tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795#endif
1796
Alan Coxf34d7a52008-04-30 00:54:13 -07001797 if (tty->ops->close)
1798 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Peter Hurley2aff5e22014-11-05 12:13:01 -05001800 /* If tty is pty master, lock the slave pty (stable lock order) */
1801 tty_lock_slave(o_tty);
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 /*
1804 * Sanity check: if tty->count is going to zero, there shouldn't be
1805 * any waiters on tty->read_wait or tty->write_wait. We test the
1806 * wait queues and kick everyone out _before_ actually starting to
1807 * close. This ensures that we won't block while releasing the tty
1808 * structure.
1809 *
1810 * The test for the o_tty closing is necessary, since the master and
1811 * slave sides may close in any order. If the slave side closes out
1812 * first, its count will be one, since the master side holds an open.
Peter Hurley324c1652014-11-05 12:12:56 -05001813 * Thus this test wouldn't be triggered at the time the slave closed,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 * so we do it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 */
1816 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 do_sleep = 0;
1818
Peter Hurley324c1652014-11-05 12:12:56 -05001819 if (tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (waitqueue_active(&tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001821 wake_up_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 do_sleep++;
1823 }
1824 if (waitqueue_active(&tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001825 wake_up_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 do_sleep++;
1827 }
1828 }
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001829 if (o_tty && o_tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (waitqueue_active(&o_tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001831 wake_up_poll(&o_tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 do_sleep++;
1833 }
1834 if (waitqueue_active(&o_tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001835 wake_up_poll(&o_tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 do_sleep++;
1837 }
1838 }
1839 if (!do_sleep)
1840 break;
1841
Peter Hurley494c1ea2014-10-16 13:54:36 -04001842 if (once) {
1843 once = 0;
1844 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n",
1845 __func__, tty_name(tty, buf));
1846 }
Peter Hurley37b16452014-10-16 13:51:30 -04001847 schedule_timeout_killable(timeout);
1848 if (timeout < 120 * HZ)
1849 timeout = 2 * timeout + 1;
1850 else
1851 timeout = MAX_SCHEDULE_TIMEOUT;
Alan Cox37bdfb02008-02-08 04:18:47 -08001852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001854 if (o_tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (--o_tty->count < 0) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001856 printk(KERN_WARNING "%s: bad pty slave count (%d) for %s\n",
1857 __func__, o_tty->count, tty_name(o_tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 o_tty->count = 0;
1859 }
1860 }
1861 if (--tty->count < 0) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001862 printk(KERN_WARNING "%s: bad tty->count (%d) for %s\n",
1863 __func__, tty->count, tty_name(tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 tty->count = 0;
1865 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 /*
1868 * We've decremented tty->count, so we need to remove this file
1869 * descriptor off the tty->tty_files list; this serves two
1870 * purposes:
1871 * - check_tty_count sees the correct number of file descriptors
1872 * associated with this tty.
1873 * - do_tty_hangup no longer sees this file descriptor as
1874 * something that needs to be handled for hangups.
1875 */
Nick Piggind996b622010-08-18 04:37:36 +10001876 tty_del_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 /*
1879 * Perform some housekeeping before deciding whether to return.
1880 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 * If _either_ side is closing, make sure there aren't any
1882 * processes that still think tty or o_tty is their controlling
1883 * tty.
1884 */
Peter Hurley324c1652014-11-05 12:12:56 -05001885 if (!tty->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001887 session_clear_tty(tty->session);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001888 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001889 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 read_unlock(&tasklist_lock);
1891 }
1892
Peter Hurley324c1652014-11-05 12:12:56 -05001893 /* check whether both sides are closing ... */
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001894 final = !tty->count && !(o_tty && o_tty->count);
Peter Hurley324c1652014-11-05 12:12:56 -05001895
Peter Hurley2aff5e22014-11-05 12:13:01 -05001896 tty_unlock_slave(o_tty);
1897 tty_unlock(tty);
1898
Peter Hurley04980702014-11-05 12:12:52 -05001899 /* At this point, the tty->count == 0 should ensure a dead tty
Alan Coxd1552552012-07-27 18:02:54 +01001900 cannot be re-opened by a racing opener */
Paul Fulghumda965822006-02-14 13:53:00 -08001901
Peter Hurley324c1652014-11-05 12:12:56 -05001902 if (!final)
Alan Coxeeb89d92009-11-30 13:18:29 +00001903 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905#ifdef TTY_DEBUG_HANGUP
Peter Hurley96433d12013-03-11 16:44:37 -04001906 printk(KERN_DEBUG "%s: %s: final close\n", __func__, tty_name(tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907#endif
1908 /*
Alan Cox01e1abb2008-07-22 11:16:55 +01001909 * Ask the line discipline code to release its structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 */
Peter Hurley62462ae2014-11-05 12:12:58 -05001911 tty_ldisc_release(tty);
Peter Hurleya2965b72013-03-11 16:44:35 -04001912
1913 /* Wait for pending work before tty destruction commmences */
1914 tty_flush_works(tty);
Peter Hurleya2965b72013-03-11 16:44:35 -04001915
Peter Hurley96433d12013-03-11 16:44:37 -04001916#ifdef TTY_DEBUG_HANGUP
1917 printk(KERN_DEBUG "%s: %s: freeing structure...\n", __func__, tty_name(tty, buf));
1918#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 /*
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001920 * The release_tty function takes care of the details of clearing
Alan Cox89c8d912012-08-08 16:30:13 +01001921 * the slots and preserving the termios structure. The tty_unlock_pair
1922 * should be safe as we keep a kref while the tty is locked (so the
1923 * unlock never unlocks a freed tty).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 */
Alan Coxd1552552012-07-27 18:02:54 +01001925 mutex_lock(&tty_mutex);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001926 release_tty(tty, idx);
Alan Coxd1552552012-07-27 18:02:54 +01001927 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Alan Coxeeb89d92009-11-30 13:18:29 +00001929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Alan Coxaf9b8972006-08-27 01:24:01 -07001932/**
Peter Hurley52494ee2014-11-05 12:12:50 -05001933 * tty_open_current_tty - get locked tty of current task
Jiri Slabyb82154a2011-11-09 21:33:19 +01001934 * @device: device number
1935 * @filp: file pointer to tty
Peter Hurley52494ee2014-11-05 12:12:50 -05001936 * @return: locked tty of the current task iff @device is /dev/tty
1937 *
1938 * Performs a re-open of the current task's controlling tty.
Jiri Slabyb82154a2011-11-09 21:33:19 +01001939 *
1940 * We cannot return driver and index like for the other nodes because
1941 * devpts will not work then. It expects inodes to be from devpts FS.
1942 */
1943static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1944{
1945 struct tty_struct *tty;
Peter Hurley52494ee2014-11-05 12:12:50 -05001946 int retval;
Jiri Slabyb82154a2011-11-09 21:33:19 +01001947
1948 if (device != MKDEV(TTYAUX_MAJOR, 0))
1949 return NULL;
1950
1951 tty = get_current_tty();
1952 if (!tty)
1953 return ERR_PTR(-ENXIO);
1954
1955 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1956 /* noctty = 1; */
Peter Hurley52494ee2014-11-05 12:12:50 -05001957 tty_lock(tty);
1958 tty_kref_put(tty); /* safe to drop the kref now */
1959
1960 retval = tty_reopen(tty);
1961 if (retval < 0) {
1962 tty_unlock(tty);
1963 tty = ERR_PTR(retval);
1964 }
Jiri Slabyb82154a2011-11-09 21:33:19 +01001965 return tty;
1966}
1967
1968/**
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001969 * tty_lookup_driver - lookup a tty driver for a given device file
1970 * @device: device number
1971 * @filp: file pointer to tty
1972 * @noctty: set if the device should not become a controlling tty
1973 * @index: index for the device in the @return driver
1974 * @return: driver for this inode (with increased refcount)
1975 *
1976 * If @return is not erroneous, the caller is responsible to decrement the
1977 * refcount by tty_driver_kref_put.
1978 *
1979 * Locking: tty_mutex protects get_tty_driver
1980 */
1981static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
1982 int *noctty, int *index)
1983{
1984 struct tty_driver *driver;
1985
Jiri Slaby2cd00502011-11-09 21:33:22 +01001986 switch (device) {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001987#ifdef CONFIG_VT
Jiri Slaby2cd00502011-11-09 21:33:22 +01001988 case MKDEV(TTY_MAJOR, 0): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001989 extern struct tty_driver *console_driver;
1990 driver = tty_driver_kref_get(console_driver);
1991 *index = fg_console;
1992 *noctty = 1;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001993 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001994 }
1995#endif
Jiri Slaby2cd00502011-11-09 21:33:22 +01001996 case MKDEV(TTYAUX_MAJOR, 1): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001997 struct tty_driver *console_driver = console_device(index);
1998 if (console_driver) {
1999 driver = tty_driver_kref_get(console_driver);
2000 if (driver) {
2001 /* Don't let /dev/console block */
2002 filp->f_flags |= O_NONBLOCK;
2003 *noctty = 1;
Jiri Slaby2cd00502011-11-09 21:33:22 +01002004 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01002005 }
2006 }
2007 return ERR_PTR(-ENODEV);
2008 }
Jiri Slaby2cd00502011-11-09 21:33:22 +01002009 default:
2010 driver = get_tty_driver(device, index);
2011 if (!driver)
2012 return ERR_PTR(-ENODEV);
2013 break;
2014 }
Jiri Slaby5b5e7042011-11-09 21:33:20 +01002015 return driver;
2016}
2017
2018/**
Alan Coxeeb89d92009-11-30 13:18:29 +00002019 * tty_open - open a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002020 * @inode: inode of device file
2021 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002023 * tty_open and tty_release keep up the tty count that contains the
2024 * number of opens done on a tty. We cannot use the inode-count, as
2025 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002027 * Open-counting is needed for pty masters, as well as for keeping
2028 * track of serial lines: DTR is dropped when the last close happens.
2029 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2030 *
2031 * The termios state of a pty is reset on first open so that
2032 * settings don't persist across reuse.
2033 *
Jiri Slaby5b5e7042011-11-09 21:33:20 +01002034 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002035 * tty->count should protect the rest.
2036 * ->siglock protects ->signal/->sighand
Alan Cox89c8d912012-08-08 16:30:13 +01002037 *
2038 * Note: the tty_unlock/lock cases without a ref are only safe due to
2039 * tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002041
Alan Coxeeb89d92009-11-30 13:18:29 +00002042static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Jiri Slabyb82154a2011-11-09 21:33:19 +01002044 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int noctty, retval;
Jiri Slabyb82154a2011-11-09 21:33:19 +01002046 struct tty_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 int index;
2048 dev_t device = inode->i_rdev;
Andrew Morton846c1512009-04-02 16:56:36 -07002049 unsigned saved_flags = filp->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08002052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053retry_open:
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02002054 retval = tty_alloc_file(filp);
2055 if (retval)
2056 return -ENOMEM;
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 noctty = filp->f_flags & O_NOCTTY;
2059 index = -1;
2060 retval = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002061
Jiri Slabyb82154a2011-11-09 21:33:19 +01002062 tty = tty_open_current_tty(device, filp);
Peter Hurley52494ee2014-11-05 12:12:50 -05002063 if (!tty) {
2064 mutex_lock(&tty_mutex);
Jiri Slaby5b5e7042011-11-09 21:33:20 +01002065 driver = tty_lookup_driver(device, filp, &noctty, &index);
2066 if (IS_ERR(driver)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01002067 retval = PTR_ERR(driver);
2068 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01002071 /* check whether we're reopening an existing tty */
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01002072 tty = tty_driver_lookup_tty(driver, inode, index);
Eric Paris808ffa32009-01-27 11:50:37 +00002073 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01002074 retval = PTR_ERR(tty);
2075 goto err_unlock;
Eric Paris808ffa32009-01-27 11:50:37 +00002076 }
Peter Hurley52494ee2014-11-05 12:12:50 -05002077
2078 if (tty) {
Peter Hurleyaa3cb812014-11-05 12:12:51 -05002079 mutex_unlock(&tty_mutex);
Peter Hurley52494ee2014-11-05 12:12:50 -05002080 tty_lock(tty);
Peter Hurleyaa3cb812014-11-05 12:12:51 -05002081 /* safe to drop the kref from tty_driver_lookup_tty() */
2082 tty_kref_put(tty);
Peter Hurley52494ee2014-11-05 12:12:50 -05002083 retval = tty_reopen(tty);
2084 if (retval < 0) {
2085 tty_unlock(tty);
2086 tty = ERR_PTR(retval);
2087 }
Peter Hurleyaa3cb812014-11-05 12:12:51 -05002088 } else { /* Returns with the tty_lock held for now */
Peter Hurley52494ee2014-11-05 12:12:50 -05002089 tty = tty_init_dev(driver, index);
Peter Hurleyaa3cb812014-11-05 12:12:51 -05002090 mutex_unlock(&tty_mutex);
2091 }
Peter Hurley52494ee2014-11-05 12:12:50 -05002092
Peter Hurley52494ee2014-11-05 12:12:50 -05002093 tty_driver_kref_put(driver);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01002094 }
2095
Alan Coxeeb89d92009-11-30 13:18:29 +00002096 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01002097 retval = PTR_ERR(tty);
2098 goto err_file;
Alan Coxeeb89d92009-11-30 13:18:29 +00002099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02002101 tty_add_file(tty, filp);
Nick Piggind996b622010-08-18 04:37:36 +10002102
Jiri Slaby9de44bd2011-11-09 21:33:24 +01002103 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2105 tty->driver->subtype == PTY_TYPE_MASTER)
2106 noctty = 1;
2107#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01002108 printk(KERN_DEBUG "%s: opening %s...\n", __func__, tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109#endif
Herton Ronaldo Krzesinski909bc772011-03-31 15:35:31 -03002110 if (tty->ops->open)
2111 retval = tty->ops->open(tty, filp);
2112 else
2113 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 filp->f_flags = saved_flags;
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (retval) {
2117#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01002118 printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__,
2119 retval, tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120#endif
Alan Cox89c8d912012-08-08 16:30:13 +01002121 tty_unlock(tty); /* need to call tty_release without BTM */
Alan Coxeeb89d92009-11-30 13:18:29 +00002122 tty_release(inode, filp);
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002123 if (retval != -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002125
2126 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 schedule();
2130 /*
2131 * Need to reset f_op in case a hangup happened.
2132 */
Peter Hurley12569372014-11-05 12:26:24 -05002133 if (tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 filp->f_op = &tty_fops;
2135 goto retry_open;
2136 }
Peter Hurleyd4855e12013-11-19 08:46:27 -05002137 clear_bit(TTY_HUPPED, &tty->flags);
Alan Coxeeb89d92009-11-30 13:18:29 +00002138
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002139
Peter Hurley2c411c12014-10-16 14:59:47 -04002140 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002141 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (!noctty &&
2143 current->signal->leader &&
2144 !current->signal->tty &&
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002145 tty->session == NULL)
Peter Hurleybce65f12014-10-16 14:59:43 -04002146 __proc_set_tty(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002147 spin_unlock_irq(&current->sighand->siglock);
Peter Hurley2c411c12014-10-16 14:59:47 -04002148 read_unlock(&tasklist_lock);
Alan Cox89c8d912012-08-08 16:30:13 +01002149 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return 0;
Jiri Slabyba5db442011-11-09 21:33:21 +01002151err_unlock:
Jiri Slabyba5db442011-11-09 21:33:21 +01002152 mutex_unlock(&tty_mutex);
2153 /* after locks to avoid deadlock */
2154 if (!IS_ERR_OR_NULL(driver))
2155 tty_driver_kref_put(driver);
2156err_file:
2157 tty_free_file(filp);
2158 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Jonathan Corbet39d95b92008-05-16 09:10:50 -06002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Alan Coxaf9b8972006-08-27 01:24:01 -07002163/**
2164 * tty_poll - check tty status
2165 * @filp: file being polled
2166 * @wait: poll wait structures to update
2167 *
2168 * Call the line discipline polling method to obtain the poll
2169 * status of the device.
2170 *
2171 * Locking: locks called line discipline but ldisc poll method
2172 * may be re-entered freely by other callers.
2173 */
2174
Alan Cox37bdfb02008-02-08 04:18:47 -08002175static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Nick Piggind996b622010-08-18 04:37:36 +10002177 struct tty_struct *tty = file_tty(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 struct tty_ldisc *ld;
2179 int ret = 0;
2180
Al Viro6131ffa2013-02-27 16:59:05 -05002181 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002185 if (ld->ops->poll)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002186 ret = ld->ops->poll(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 tty_ldisc_deref(ld);
2188 return ret;
2189}
2190
Arnd Bergmannec79d602010-06-01 22:53:01 +02002191static int __tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Nick Piggind996b622010-08-18 04:37:36 +10002193 struct tty_struct *tty = file_tty(filp);
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002194 struct tty_ldisc *ldisc;
Alan Cox47f86832008-04-30 00:53:30 -07002195 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002196 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Al Viro6131ffa2013-02-27 16:59:05 -05002198 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002199 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08002200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 retval = fasync_helper(fd, filp, on, &tty->fasync);
2202 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002205 ldisc = tty_ldisc_ref(tty);
2206 if (ldisc) {
2207 if (ldisc->ops->fasync)
2208 ldisc->ops->fasync(tty, on);
2209 tty_ldisc_deref(ldisc);
2210 }
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002213 enum pid_type type;
2214 struct pid *pid;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002215
Alan Cox47f86832008-04-30 00:53:30 -07002216 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002217 if (tty->pgrp) {
2218 pid = tty->pgrp;
2219 type = PIDTYPE_PGID;
2220 } else {
2221 pid = task_pid(current);
2222 type = PIDTYPE_PID;
2223 }
Linus Torvalds80e1e822010-02-07 10:11:23 -08002224 get_pid(pid);
Greg Kroah-Hartman70362512009-12-17 07:07:19 -08002225 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002226 __f_setown(filp, pid, type, 0);
Linus Torvalds80e1e822010-02-07 10:11:23 -08002227 put_pid(pid);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002228 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002230out:
Arnd Bergmannec79d602010-06-01 22:53:01 +02002231 return retval;
2232}
2233
2234static int tty_fasync(int fd, struct file *filp, int on)
2235{
Alan Cox89c8d912012-08-08 16:30:13 +01002236 struct tty_struct *tty = file_tty(filp);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002237 int retval;
Alan Cox89c8d912012-08-08 16:30:13 +01002238
2239 tty_lock(tty);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002240 retval = __tty_fasync(fd, filp, on);
Alan Cox89c8d912012-08-08 16:30:13 +01002241 tty_unlock(tty);
2242
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002243 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Alan Coxaf9b8972006-08-27 01:24:01 -07002246/**
2247 * tiocsti - fake input character
2248 * @tty: tty to fake input into
2249 * @p: pointer to character
2250 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02002251 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07002252 * input management.
2253 *
2254 * FIXME: does not honour flow control ??
2255 *
2256 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -04002257 * Called functions take tty_ldiscs_lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002258 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07002259 *
2260 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07002261 */
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263static int tiocsti(struct tty_struct *tty, char __user *p)
2264{
2265 char ch, mbz = 0;
2266 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2269 return -EPERM;
2270 if (get_user(ch, p))
2271 return -EFAULT;
Al Viro1e641742008-12-09 09:23:33 +00002272 tty_audit_tiocsti(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002274 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 tty_ldisc_deref(ld);
2276 return 0;
2277}
2278
Alan Coxaf9b8972006-08-27 01:24:01 -07002279/**
2280 * tiocgwinsz - implement window query ioctl
2281 * @tty; tty
2282 * @arg: user buffer for result
2283 *
Alan Cox808a0d32006-09-29 02:00:40 -07002284 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002285 *
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002286 * Locking: tty->winsize_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002287 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002288 */
2289
Alan Cox37bdfb02008-02-08 04:18:47 -08002290static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
Alan Cox808a0d32006-09-29 02:00:40 -07002292 int err;
2293
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002294 mutex_lock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002295 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002296 mutex_unlock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002297
2298 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
Alan Coxaf9b8972006-08-27 01:24:01 -07002301/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002302 * tty_do_resize - resize event
2303 * @tty: tty being resized
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002304 * @rows: rows (character)
2305 * @cols: cols (character)
Alan Coxaf9b8972006-08-27 01:24:01 -07002306 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002307 * Update the termios variables and send the necessary signals to
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002308 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002309 */
2310
Alan Coxfc6f6232009-01-02 13:43:17 +00002311int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Alan Coxfc6f6232009-01-02 13:43:17 +00002313 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Alan Coxfc6f6232009-01-02 13:43:17 +00002315 /* Lock the tty */
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002316 mutex_lock(&tty->winsize_mutex);
Alan Coxfc6f6232009-01-02 13:43:17 +00002317 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002318 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002319
Peter Hurley5b239542014-10-16 14:59:45 -04002320 /* Signal the foreground process group */
2321 pgrp = tty_get_pgrp(tty);
Alan Cox47f86832008-04-30 00:53:30 -07002322 if (pgrp)
2323 kill_pgrp(pgrp, SIGWINCH, 1);
Alan Cox47f86832008-04-30 00:53:30 -07002324 put_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002325
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002326 tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002327done:
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002328 mutex_unlock(&tty->winsize_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return 0;
2330}
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01002331EXPORT_SYMBOL(tty_do_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Alan Coxaf9b8972006-08-27 01:24:01 -07002333/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002334 * tiocswinsz - implement window size set ioctl
Alan Coxfc6f6232009-01-02 13:43:17 +00002335 * @tty; tty side of tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002336 * @arg: user buffer for result
2337 *
2338 * Copies the user idea of the window size to the kernel. Traditionally
2339 * this is just advisory information but for the Linux console it
2340 * actually has driver level meaning and triggers a VC resize.
2341 *
2342 * Locking:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002343 * Driver dependent. The default do_resize method takes the
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002344 * tty termios mutex and ctrl_lock. The console takes its own lock
2345 * then calls into the default method.
2346 */
2347
Alan Coxfc6f6232009-01-02 13:43:17 +00002348static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002349{
2350 struct winsize tmp_ws;
2351 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2352 return -EFAULT;
2353
2354 if (tty->ops->resize)
Alan Coxfc6f6232009-01-02 13:43:17 +00002355 return tty->ops->resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002356 else
Alan Coxfc6f6232009-01-02 13:43:17 +00002357 return tty_do_resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002358}
2359
2360/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002361 * tioccons - allow admin to move logical console
2362 * @file: the file to become console
2363 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002364 * Allow the administrator to move the redirected console device
Alan Coxaf9b8972006-08-27 01:24:01 -07002365 *
2366 * Locking: uses redirect_lock to guard the redirect information
2367 */
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369static int tioccons(struct file *file)
2370{
2371 if (!capable(CAP_SYS_ADMIN))
2372 return -EPERM;
2373 if (file->f_op->write == redirected_tty_write) {
2374 struct file *f;
2375 spin_lock(&redirect_lock);
2376 f = redirect;
2377 redirect = NULL;
2378 spin_unlock(&redirect_lock);
2379 if (f)
2380 fput(f);
2381 return 0;
2382 }
2383 spin_lock(&redirect_lock);
2384 if (redirect) {
2385 spin_unlock(&redirect_lock);
2386 return -EBUSY;
2387 }
Al Virocb0942b2012-08-27 14:48:26 -04002388 redirect = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 spin_unlock(&redirect_lock);
2390 return 0;
2391}
2392
Alan Coxaf9b8972006-08-27 01:24:01 -07002393/**
2394 * fionbio - non blocking ioctl
2395 * @file: file to set blocking value
2396 * @p: user parameter
2397 *
2398 * Historical tty interfaces had a blocking control ioctl before
2399 * the generic functionality existed. This piece of history is preserved
2400 * in the expected tty API of posix OS's.
2401 *
Alan Cox6146b9a2009-09-19 13:13:19 -07002402 * Locking: none, the open file handle ensures it won't go away.
Alan Coxaf9b8972006-08-27 01:24:01 -07002403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405static int fionbio(struct file *file, int __user *p)
2406{
2407 int nonblock;
2408
2409 if (get_user(nonblock, p))
2410 return -EFAULT;
2411
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002412 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (nonblock)
2414 file->f_flags |= O_NONBLOCK;
2415 else
2416 file->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002417 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return 0;
2419}
2420
Alan Coxaf9b8972006-08-27 01:24:01 -07002421/**
2422 * tiocsctty - set controlling tty
2423 * @tty: tty structure
2424 * @arg: user argument
2425 *
2426 * This ioctl is used to manage job control. It permits a session
2427 * leader to set this tty as the controlling tty for the session.
2428 *
2429 * Locking:
Peter Hurleye218eb32014-10-16 14:59:49 -04002430 * Takes tty_lock() to serialize proc_set_tty() for this tty
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002431 * Takes tasklist_lock internally to walk sessions
2432 * Takes ->siglock() when updating signal->tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002433 */
2434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435static int tiocsctty(struct tty_struct *tty, int arg)
2436{
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002437 int ret = 0;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002438
Peter Hurleye218eb32014-10-16 14:59:49 -04002439 tty_lock(tty);
Peter Hurley2c411c12014-10-16 14:59:47 -04002440 read_lock(&tasklist_lock);
2441
2442 if (current->signal->leader && (task_session(current) == tty->session))
2443 goto unlock;
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 /*
2446 * The process must be a session leader and
2447 * not have a controlling tty already.
2448 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002449 if (!current->signal->leader || current->signal->tty) {
2450 ret = -EPERM;
2451 goto unlock;
2452 }
2453
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002454 if (tty->session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 /*
2456 * This tty is already the controlling
2457 * tty for another session group!
2458 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002459 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 /*
2461 * Steal it away
2462 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002463 session_clear_tty(tty->session);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002464 } else {
2465 ret = -EPERM;
2466 goto unlock;
2467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
Peter Hurleybce65f12014-10-16 14:59:43 -04002469 proc_set_tty(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002470unlock:
Peter Hurley2c411c12014-10-16 14:59:47 -04002471 read_unlock(&tasklist_lock);
Peter Hurleye218eb32014-10-16 14:59:49 -04002472 tty_unlock(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002473 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Alan Coxaf9b8972006-08-27 01:24:01 -07002476/**
Alan Cox5d0fdf12008-04-30 00:53:31 -07002477 * tty_get_pgrp - return a ref counted pgrp pid
2478 * @tty: tty to read
2479 *
2480 * Returns a refcounted instance of the pid struct for the process
2481 * group controlling the tty.
2482 */
2483
2484struct pid *tty_get_pgrp(struct tty_struct *tty)
2485{
2486 unsigned long flags;
2487 struct pid *pgrp;
2488
2489 spin_lock_irqsave(&tty->ctrl_lock, flags);
2490 pgrp = get_pid(tty->pgrp);
2491 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2492
2493 return pgrp;
2494}
2495EXPORT_SYMBOL_GPL(tty_get_pgrp);
2496
Peter Hurleye1c22962014-10-16 14:59:48 -04002497/*
2498 * This checks not only the pgrp, but falls back on the pid if no
2499 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
2500 * without this...
2501 *
2502 * The caller must hold rcu lock or the tasklist lock.
2503 */
2504static struct pid *session_of_pgrp(struct pid *pgrp)
2505{
2506 struct task_struct *p;
2507 struct pid *sid = NULL;
2508
2509 p = pid_task(pgrp, PIDTYPE_PGID);
2510 if (p == NULL)
2511 p = pid_task(pgrp, PIDTYPE_PID);
2512 if (p != NULL)
2513 sid = task_session(p);
2514
2515 return sid;
2516}
2517
Alan Cox5d0fdf12008-04-30 00:53:31 -07002518/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002519 * tiocgpgrp - get process group
2520 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002521 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002522 * @p: returned pid
2523 *
2524 * Obtain the process group of the tty. If there is no process group
2525 * return an error.
2526 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002527 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002528 */
2529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2531{
Alan Cox5d0fdf12008-04-30 00:53:31 -07002532 struct pid *pid;
2533 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /*
2535 * (tty == real_tty) is a cheap way of
2536 * testing if the tty is NOT a master pty.
2537 */
2538 if (tty == real_tty && current->signal->tty != real_tty)
2539 return -ENOTTY;
Alan Cox5d0fdf12008-04-30 00:53:31 -07002540 pid = tty_get_pgrp(real_tty);
2541 ret = put_user(pid_vnr(pid), p);
2542 put_pid(pid);
2543 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544}
2545
Alan Coxaf9b8972006-08-27 01:24:01 -07002546/**
2547 * tiocspgrp - attempt to set process group
2548 * @tty: tty passed by user
2549 * @real_tty: tty side device matching tty passed by user
2550 * @p: pid pointer
2551 *
2552 * Set the process group of the tty to the session passed. Only
2553 * permitted where the tty session is our session.
2554 *
Alan Cox47f86832008-04-30 00:53:30 -07002555 * Locking: RCU, ctrl lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002556 */
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2559{
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002560 struct pid *pgrp;
2561 pid_t pgrp_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 int retval = tty_check_change(real_tty);
Alan Cox47f86832008-04-30 00:53:30 -07002563 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 if (retval == -EIO)
2566 return -ENOTTY;
2567 if (retval)
2568 return retval;
2569 if (!current->signal->tty ||
2570 (current->signal->tty != real_tty) ||
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002571 (real_tty->session != task_session(current)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 return -ENOTTY;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002573 if (get_user(pgrp_nr, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return -EFAULT;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002575 if (pgrp_nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return -EINVAL;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002577 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002578 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002579 retval = -ESRCH;
2580 if (!pgrp)
2581 goto out_unlock;
2582 retval = -EPERM;
2583 if (session_of_pgrp(pgrp) != task_session(current))
2584 goto out_unlock;
2585 retval = 0;
Alan Cox47f86832008-04-30 00:53:30 -07002586 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002587 put_pid(real_tty->pgrp);
2588 real_tty->pgrp = get_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002589 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002590out_unlock:
2591 rcu_read_unlock();
2592 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
Alan Coxaf9b8972006-08-27 01:24:01 -07002595/**
2596 * tiocgsid - get session id
2597 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002598 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002599 * @p: pointer to returned session id
2600 *
2601 * Obtain the session id of the tty. If there is no session
2602 * return an error.
2603 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002604 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002605 */
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2608{
2609 /*
2610 * (tty == real_tty) is a cheap way of
2611 * testing if the tty is NOT a master pty.
2612 */
2613 if (tty == real_tty && current->signal->tty != real_tty)
2614 return -ENOTTY;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002615 if (!real_tty->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return -ENOTTY;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002617 return put_user(pid_vnr(real_tty->session), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
Alan Coxaf9b8972006-08-27 01:24:01 -07002620/**
2621 * tiocsetd - set line discipline
2622 * @tty: tty device
2623 * @p: pointer to user data
2624 *
2625 * Set the line discipline according to user request.
2626 *
2627 * Locking: see tty_set_ldisc, this function is just a helper
2628 */
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630static int tiocsetd(struct tty_struct *tty, int __user *p)
2631{
2632 int ldisc;
Alan Cox04f378b2008-04-30 00:53:29 -07002633 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 if (get_user(ldisc, p))
2636 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002637
Alan Cox04f378b2008-04-30 00:53:29 -07002638 ret = tty_set_ldisc(tty, ldisc);
Alan Cox04f378b2008-04-30 00:53:29 -07002639
2640 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Alan Coxaf9b8972006-08-27 01:24:01 -07002643/**
2644 * send_break - performed time break
2645 * @tty: device to break on
2646 * @duration: timeout in mS
2647 *
2648 * Perform a timed break on hardware that lacks its own driver level
2649 * timed break functionality.
2650 *
2651 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002652 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002653 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002654 */
2655
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002656static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Alan Cox9e989662008-07-22 11:18:03 +01002658 int retval;
2659
2660 if (tty->ops->break_ctl == NULL)
2661 return 0;
2662
2663 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2664 retval = tty->ops->break_ctl(tty, duration);
2665 else {
2666 /* Do the work ourselves */
2667 if (tty_write_lock(tty, 0) < 0)
2668 return -EINTR;
2669 retval = tty->ops->break_ctl(tty, -1);
2670 if (retval)
2671 goto out;
2672 if (!signal_pending(current))
2673 msleep_interruptible(duration);
2674 retval = tty->ops->break_ctl(tty, 0);
2675out:
2676 tty_write_unlock(tty);
2677 if (signal_pending(current))
2678 retval = -EINTR;
2679 }
2680 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Alan Coxaf9b8972006-08-27 01:24:01 -07002683/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002684 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002685 * @tty: tty device
2686 * @file: user file pointer
2687 * @p: pointer to result
2688 *
2689 * Obtain the modem status bits from the tty driver if the feature
2690 * is supported. Return -EINVAL if it is not available.
2691 *
2692 * Locking: none (up to the driver)
2693 */
2694
Alan Cox60b33c12011-02-14 16:26:14 +00002695static int tty_tiocmget(struct tty_struct *tty, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 int retval = -EINVAL;
2698
Alan Coxf34d7a52008-04-30 00:54:13 -07002699 if (tty->ops->tiocmget) {
Alan Cox60b33c12011-02-14 16:26:14 +00002700 retval = tty->ops->tiocmget(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 if (retval >= 0)
2703 retval = put_user(retval, p);
2704 }
2705 return retval;
2706}
2707
Alan Coxaf9b8972006-08-27 01:24:01 -07002708/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002709 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002710 * @tty: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002711 * @cmd: command - clear bits, set bits or set all
2712 * @p: pointer to desired bits
2713 *
2714 * Set the modem status bits from the tty driver if the feature
2715 * is supported. Return -EINVAL if it is not available.
2716 *
2717 * Locking: none (up to the driver)
2718 */
2719
Alan Cox20b9d172011-02-14 16:26:50 +00002720static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 unsigned __user *p)
2722{
Alan Coxae677512008-07-16 21:56:54 +01002723 int retval;
2724 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Alan Coxae677512008-07-16 21:56:54 +01002726 if (tty->ops->tiocmset == NULL)
2727 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Alan Coxae677512008-07-16 21:56:54 +01002729 retval = get_user(val, p);
2730 if (retval)
2731 return retval;
2732 set = clear = 0;
2733 switch (cmd) {
2734 case TIOCMBIS:
2735 set = val;
2736 break;
2737 case TIOCMBIC:
2738 clear = val;
2739 break;
2740 case TIOCMSET:
2741 set = val;
2742 clear = ~val;
2743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
Alan Coxae677512008-07-16 21:56:54 +01002745 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2746 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
Alan Cox20b9d172011-02-14 16:26:50 +00002747 return tty->ops->tiocmset(tty, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}
2749
Alan Coxd281da72010-09-16 18:21:24 +01002750static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2751{
2752 int retval = -EINVAL;
2753 struct serial_icounter_struct icount;
2754 memset(&icount, 0, sizeof(icount));
2755 if (tty->ops->get_icount)
2756 retval = tty->ops->get_icount(tty, &icount);
2757 if (retval != 0)
2758 return retval;
2759 if (copy_to_user(arg, &icount, sizeof(icount)))
2760 return -EFAULT;
2761 return 0;
2762}
2763
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002764static void tty_warn_deprecated_flags(struct serial_struct __user *ss)
2765{
2766 static DEFINE_RATELIMIT_STATE(depr_flags,
2767 DEFAULT_RATELIMIT_INTERVAL,
2768 DEFAULT_RATELIMIT_BURST);
2769 char comm[TASK_COMM_LEN];
2770 int flags;
2771
2772 if (get_user(flags, &ss->flags))
2773 return;
2774
2775 flags &= ASYNC_DEPRECATED;
2776
2777 if (flags && __ratelimit(&depr_flags))
2778 pr_warning("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
2779 __func__, get_task_comm(comm, current), flags);
2780}
2781
Peter Hurley8f166e02014-10-16 14:59:41 -04002782/*
2783 * if pty, return the slave side (real_tty)
2784 * otherwise, return self
2785 */
2786static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
Alan Coxe8b70e72009-06-11 12:48:02 +01002787{
2788 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2789 tty->driver->subtype == PTY_TYPE_MASTER)
2790 tty = tty->link;
2791 return tty;
2792}
Alan Coxe8b70e72009-06-11 12:48:02 +01002793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794/*
2795 * Split this up, as gcc can choke on it otherwise..
2796 */
Alan Cox04f378b2008-04-30 00:53:29 -07002797long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Nick Piggind996b622010-08-18 04:37:36 +10002799 struct tty_struct *tty = file_tty(file);
2800 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 void __user *p = (void __user *)arg;
2802 int retval;
2803 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002804
Al Viro6131ffa2013-02-27 16:59:05 -05002805 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return -EINVAL;
2807
Alan Coxe8b70e72009-06-11 12:48:02 +01002808 real_tty = tty_pair_get_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 /*
2811 * Factor out some common prep work
2812 */
2813 switch (cmd) {
2814 case TIOCSETD:
2815 case TIOCSBRK:
2816 case TIOCCBRK:
2817 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002818 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 retval = tty_check_change(tty);
2820 if (retval)
2821 return retval;
2822 if (cmd != TIOCCBRK) {
2823 tty_wait_until_sent(tty, 0);
2824 if (signal_pending(current))
2825 return -EINTR;
2826 }
2827 break;
2828 }
2829
Alan Cox9e989662008-07-22 11:18:03 +01002830 /*
2831 * Now do the stuff.
2832 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002834 case TIOCSTI:
2835 return tiocsti(tty, p);
2836 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002837 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002838 case TIOCSWINSZ:
Alan Coxfc6f6232009-01-02 13:43:17 +00002839 return tiocswinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002840 case TIOCCONS:
2841 return real_tty != tty ? -EINVAL : tioccons(file);
2842 case FIONBIO:
2843 return fionbio(file, p);
2844 case TIOCEXCL:
2845 set_bit(TTY_EXCLUSIVE, &tty->flags);
2846 return 0;
2847 case TIOCNXCL:
2848 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2849 return 0;
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +04002850 case TIOCGEXCL:
2851 {
2852 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2853 return put_user(excl, (int __user *)p);
2854 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002855 case TIOCNOTTY:
2856 if (current->signal->tty != tty)
2857 return -ENOTTY;
2858 no_tty();
2859 return 0;
2860 case TIOCSCTTY:
2861 return tiocsctty(tty, arg);
2862 case TIOCGPGRP:
2863 return tiocgpgrp(tty, real_tty, p);
2864 case TIOCSPGRP:
2865 return tiocspgrp(tty, real_tty, p);
2866 case TIOCGSID:
2867 return tiocgsid(tty, real_tty, p);
2868 case TIOCGETD:
Alan Coxc65c9bc2009-06-11 12:50:12 +01002869 return put_user(tty->ldisc->ops->num, (int __user *)p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002870 case TIOCSETD:
2871 return tiocsetd(tty, p);
Kay Sievers3c95c982011-02-17 18:39:28 +01002872 case TIOCVHANGUP:
2873 if (!capable(CAP_SYS_ADMIN))
2874 return -EPERM;
2875 tty_vhangup(tty);
2876 return 0;
Werner Finkb7b8de02010-12-03 12:48:23 +01002877 case TIOCGDEV:
2878 {
2879 unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2880 return put_user(ret, (unsigned int __user *)p);
2881 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002882 /*
2883 * Break handling
2884 */
2885 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002886 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002887 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002888 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002889 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002890 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002891 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002892 return 0;
2893 case TCSBRK: /* SVID version: non-zero arg --> no break */
2894 /* non-zero arg means wait for all output data
2895 * to be sent (performed above) but don't send break.
2896 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002898 if (!arg)
2899 return send_break(tty, 250);
2900 return 0;
2901 case TCSBRKP: /* support for POSIX tcsendbreak() */
2902 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Alan Cox37bdfb02008-02-08 04:18:47 -08002904 case TIOCMGET:
Alan Cox60b33c12011-02-14 16:26:14 +00002905 return tty_tiocmget(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002906 case TIOCMSET:
2907 case TIOCMBIC:
2908 case TIOCMBIS:
Alan Cox20b9d172011-02-14 16:26:50 +00002909 return tty_tiocmset(tty, cmd, p);
Alan Coxd281da72010-09-16 18:21:24 +01002910 case TIOCGICOUNT:
2911 retval = tty_tiocgicount(tty, p);
2912 /* For the moment allow fall through to the old method */
2913 if (retval != -EINVAL)
2914 return retval;
2915 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002916 case TCFLSH:
2917 switch (arg) {
2918 case TCIFLUSH:
2919 case TCIOFLUSH:
2920 /* flush tty buffer and allow ldisc to process ioctl */
Peter Hurley86c80a82014-11-05 12:13:09 -05002921 tty_buffer_flush(tty, NULL);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002922 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002923 }
2924 break;
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002925 case TIOCSSERIAL:
2926 tty_warn_deprecated_flags(p);
2927 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002929 if (tty->ops->ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002930 retval = tty->ops->ioctl(tty, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (retval != -ENOIOCTLCMD)
2932 return retval;
2933 }
2934 ld = tty_ldisc_ref_wait(tty);
2935 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002936 if (ld->ops->ioctl) {
2937 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (retval == -ENOIOCTLCMD)
Wanlong Gaobbb63c52012-08-27 15:23:12 +08002939 retval = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
2941 tty_ldisc_deref(ld);
2942 return retval;
2943}
2944
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002945#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08002946static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002947 unsigned long arg)
2948{
Nick Piggind996b622010-08-18 04:37:36 +10002949 struct tty_struct *tty = file_tty(file);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002950 struct tty_ldisc *ld;
2951 int retval = -ENOIOCTLCMD;
2952
Al Viro6131ffa2013-02-27 16:59:05 -05002953 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002954 return -EINVAL;
2955
Alan Coxf34d7a52008-04-30 00:54:13 -07002956 if (tty->ops->compat_ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002957 retval = tty->ops->compat_ioctl(tty, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002958 if (retval != -ENOIOCTLCMD)
2959 return retval;
2960 }
2961
2962 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002963 if (ld->ops->compat_ioctl)
2964 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Thomas Meyer8193c422011-10-05 23:13:13 +02002965 else
2966 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002967 tty_ldisc_deref(ld);
2968
2969 return retval;
2970}
2971#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Al Viroc3c073f2012-08-21 22:32:06 -04002973static int this_tty(const void *t, struct file *file, unsigned fd)
2974{
2975 if (likely(file->f_op->read != tty_read))
2976 return 0;
2977 return file_tty(file) != t ? 0 : fd + 1;
2978}
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980/*
2981 * This implements the "Secure Attention Key" --- the idea is to
2982 * prevent trojan horses by killing all processes associated with this
2983 * tty when the user hits the "Secure Attention Key". Required for
2984 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08002985 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 * This code could be nicer; ideally it should send a HUP, wait a few
2987 * seconds, then send a INT, and then a KILL signal. But you then
2988 * have to coordinate with the init process, since all processes associated
2989 * with the current tty must be dead before the new getty is allowed
2990 * to spawn.
2991 *
2992 * Now, if it would be correct ;-/ The current code has a nasty hole -
2993 * it doesn't catch files in flight. We may send the descriptor to ourselves
2994 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2995 *
2996 * Nasty bug: do_SAK is being called in interrupt context. This can
2997 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2998 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002999void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
3001#ifdef TTY_SOFT_SAK
3002 tty_hangup(tty);
3003#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08003004 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003005 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -08003007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (!tty)
3009 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003010 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08003011
Dan Carpenterb3f13de2006-12-13 00:35:09 -08003012 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Alan Coxf34d7a52008-04-30 00:54:13 -07003014 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08003015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003017 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003018 do_each_pid_task(session, PIDTYPE_SID, p) {
Eric W. Biederman652486f2006-03-28 16:11:02 -08003019 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07003020 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003021 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003022 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003023 } while_each_pid_task(session, PIDTYPE_SID, p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003024 /* Now kill any processes that happen to have the
3025 * tty open.
3026 */
3027 do_each_thread(g, p) {
3028 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07003030 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003031 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 send_sig(SIGKILL, p, 1);
3033 continue;
3034 }
3035 task_lock(p);
Al Viroc3c073f2012-08-21 22:32:06 -04003036 i = iterate_fd(p->files, 0, this_tty, tty);
3037 if (i != 0) {
3038 printk(KERN_NOTICE "SAK: killed process %d"
3039 " (%s): fd#%d opened to the tty\n",
3040 task_pid_nr(p), p->comm, i - 1);
3041 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003044 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 read_unlock(&tasklist_lock);
3046#endif
3047}
3048
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08003049static void do_SAK_work(struct work_struct *work)
3050{
3051 struct tty_struct *tty =
3052 container_of(work, struct tty_struct, SAK_work);
3053 __do_SAK(tty);
3054}
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056/*
3057 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3058 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3059 * the values which we write to it will be identical to the values which it
3060 * already has. --akpm
3061 */
3062void do_SAK(struct tty_struct *tty)
3063{
3064 if (!tty)
3065 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 schedule_work(&tty->SAK_work);
3067}
3068
3069EXPORT_SYMBOL(do_SAK);
3070
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08003071static int dev_match_devt(struct device *dev, const void *data)
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003072{
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08003073 const dev_t *devt = data;
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003074 return dev->devt == *devt;
3075}
3076
3077/* Must put_device() after it's unused! */
3078static struct device *tty_get_device(struct tty_struct *tty)
3079{
3080 dev_t devt = tty_devnum(tty);
3081 return class_find_device(tty_class, NULL, &devt, dev_match_devt);
3082}
3083
3084
Alan Coxaf9b8972006-08-27 01:24:01 -07003085/**
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003086 * alloc_tty_struct
Alan Coxaf9b8972006-08-27 01:24:01 -07003087 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003088 * This subroutine allocates and initializes a tty structure.
Alan Coxaf9b8972006-08-27 01:24:01 -07003089 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003090 * Locking: none - tty in question is not exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003092
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003093struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003095 struct tty_struct *tty;
3096
3097 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
3098 if (!tty)
3099 return NULL;
3100
Alan Cox9c9f4de2008-10-13 10:37:26 +01003101 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 tty->magic = TTY_MAGIC;
Alan Cox01e1abb2008-07-22 11:16:55 +01003103 tty_ldisc_init(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003104 tty->session = NULL;
3105 tty->pgrp = NULL;
Alan Cox89c8d912012-08-08 16:30:13 +01003106 mutex_init(&tty->legacy_mutex);
Peter Hurleyd8c1f922013-06-15 09:14:31 -04003107 mutex_init(&tty->throttle_mutex);
Peter Hurley6a1c0682013-06-15 09:14:23 -04003108 init_rwsem(&tty->termios_rwsem);
Peter Hurleydee4a0b2013-07-24 16:43:51 -04003109 mutex_init(&tty->winsize_mutex);
Peter Hurley36697522013-06-15 07:04:48 -04003110 init_ldsem(&tty->ldisc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 init_waitqueue_head(&tty->write_wait);
3112 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00003113 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08003114 mutex_init(&tty->atomic_write_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07003115 spin_lock_init(&tty->ctrl_lock);
Peter Hurleyf9e053d2014-09-10 15:06:31 -04003116 spin_lock_init(&tty->flow_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003118 INIT_WORK(&tty->SAK_work, do_SAK_work);
Alan Coxbf970ee2008-10-13 10:42:39 +01003119
3120 tty->driver = driver;
3121 tty->ops = driver->ops;
3122 tty->index = idx;
3123 tty_line_name(driver, idx, tty->name);
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003124 tty->dev = tty_get_device(tty);
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003125
3126 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
Alan Coxf34d7a52008-04-30 00:54:13 -07003129/**
Jiri Slaby67166712011-03-23 10:48:35 +01003130 * deinitialize_tty_struct
3131 * @tty: tty to deinitialize
3132 *
3133 * This subroutine deinitializes a tty structure that has been newly
3134 * allocated but tty_release cannot be called on that yet.
3135 *
3136 * Locking: none - tty in question must not be exposed at this point
3137 */
3138void deinitialize_tty_struct(struct tty_struct *tty)
3139{
3140 tty_ldisc_deinit(tty);
3141}
3142
3143/**
Alan Coxf34d7a52008-04-30 00:54:13 -07003144 * tty_put_char - write one character to a tty
3145 * @tty: tty
3146 * @ch: character
3147 *
3148 * Write one byte to the tty using the provided put_char method
3149 * if present. Returns the number of characters successfully output.
3150 *
3151 * Note: the specific put_char operation in the driver layer may go
3152 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003154
Alan Coxf34d7a52008-04-30 00:54:13 -07003155int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Alan Coxf34d7a52008-04-30 00:54:13 -07003157 if (tty->ops->put_char)
3158 return tty->ops->put_char(tty, ch);
3159 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160}
Alan Coxf34d7a52008-04-30 00:54:13 -07003161EXPORT_SYMBOL_GPL(tty_put_char);
3162
Alan Coxd81ed102008-10-13 10:41:42 +01003163struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003165static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
3166 unsigned int index, unsigned int count)
3167{
3168 /* init here, since reused cdevs cause crashes */
3169 cdev_init(&driver->cdevs[index], &tty_fops);
3170 driver->cdevs[index].owner = driver->owner;
3171 return cdev_add(&driver->cdevs[index], dev, count);
3172}
3173
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003175 * tty_register_device - register a tty device
3176 * @driver: the tty driver that describes the tty device
3177 * @index: the index in the tty driver for this tty device
3178 * @device: a struct device that is associated with this tty device.
3179 * This field is optional, if there is no known struct device
3180 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003182 * Returns a pointer to the struct device for this tty device
3183 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003184 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003185 * This call is required to be made to register an individual tty device
3186 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3187 * that bit is not set, this function should not be called by a tty
3188 * driver.
3189 *
3190 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003192
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003193struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3194 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003196 return tty_register_device_attr(driver, index, device, NULL, NULL);
3197}
3198EXPORT_SYMBOL(tty_register_device);
3199
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003200static void tty_device_create_release(struct device *dev)
3201{
3202 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3203 kfree(dev);
3204}
3205
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003206/**
3207 * tty_register_device_attr - register a tty device
3208 * @driver: the tty driver that describes the tty device
3209 * @index: the index in the tty driver for this tty device
3210 * @device: a struct device that is associated with this tty device.
3211 * This field is optional, if there is no known struct device
3212 * for this tty device it can be set to NULL safely.
3213 * @drvdata: Driver data to be set to device.
3214 * @attr_grp: Attribute group to be set on device.
3215 *
3216 * Returns a pointer to the struct device for this tty device
3217 * (or ERR_PTR(-EFOO) on error).
3218 *
3219 * This call is required to be made to register an individual tty device
3220 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3221 * that bit is not set, this function should not be called by a tty
3222 * driver.
3223 *
3224 * Locking: ??
3225 */
3226struct device *tty_register_device_attr(struct tty_driver *driver,
3227 unsigned index, struct device *device,
3228 void *drvdata,
3229 const struct attribute_group **attr_grp)
3230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 char name[64];
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003232 dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
3233 struct device *dev = NULL;
3234 int retval = -ENODEV;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003235 bool cdev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
3237 if (index >= driver->num) {
3238 printk(KERN_ERR "Attempt to register invalid tty line number "
3239 " (%d).\n", index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003240 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
3242
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 if (driver->type == TTY_DRIVER_TYPE_PTY)
3244 pty_line_name(driver, index, name);
3245 else
3246 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003247
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003248 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003249 retval = tty_cdev_add(driver, devt, index, 1);
3250 if (retval)
3251 goto error;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003252 cdev = true;
3253 }
3254
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003255 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3256 if (!dev) {
3257 retval = -ENOMEM;
3258 goto error;
3259 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003260
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003261 dev->devt = devt;
3262 dev->class = tty_class;
3263 dev->parent = device;
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003264 dev->release = tty_device_create_release;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003265 dev_set_name(dev, "%s", name);
3266 dev->groups = attr_grp;
3267 dev_set_drvdata(dev, drvdata);
3268
3269 retval = device_register(dev);
3270 if (retval)
3271 goto error;
3272
3273 return dev;
3274
3275error:
3276 put_device(dev);
3277 if (cdev)
3278 cdev_del(&driver->cdevs[index]);
3279 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003281EXPORT_SYMBOL_GPL(tty_register_device_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003284 * tty_unregister_device - unregister a tty device
3285 * @driver: the tty driver that describes the tty device
3286 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003288 * If a tty device is registered with a call to tty_register_device() then
3289 * this function must be called when the tty device is gone.
3290 *
3291 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294void tty_unregister_device(struct tty_driver *driver, unsigned index)
3295{
Alan Cox37bdfb02008-02-08 04:18:47 -08003296 device_destroy(tty_class,
3297 MKDEV(driver->major, driver->minor_start) + index);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003298 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC))
3299 cdev_del(&driver->cdevs[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301EXPORT_SYMBOL(tty_unregister_device);
3302
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003303/**
3304 * __tty_alloc_driver -- allocate tty driver
3305 * @lines: count of lines this driver can handle at most
3306 * @owner: module which is repsonsible for this driver
3307 * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
3308 *
3309 * This should not be called directly, some of the provided macros should be
3310 * used instead. Use IS_ERR and friends on @retval.
3311 */
3312struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
3313 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
3315 struct tty_driver *driver;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003316 unsigned int cdevs = 1;
Jiri Slaby16a02082012-08-08 22:26:42 +02003317 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Jiri Slaby0019b402012-08-08 22:26:43 +02003319 if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003320 return ERR_PTR(-EINVAL);
3321
Jean Delvare506eb992007-07-15 23:40:14 -07003322 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003323 if (!driver)
3324 return ERR_PTR(-ENOMEM);
3325
3326 kref_init(&driver->kref);
3327 driver->magic = TTY_DRIVER_MAGIC;
3328 driver->num = lines;
3329 driver->owner = owner;
3330 driver->flags = flags;
Jiri Slaby16a02082012-08-08 22:26:42 +02003331
3332 if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
3333 driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
3334 GFP_KERNEL);
3335 driver->termios = kcalloc(lines, sizeof(*driver->termios),
3336 GFP_KERNEL);
3337 if (!driver->ttys || !driver->termios) {
3338 err = -ENOMEM;
3339 goto err_free_all;
3340 }
3341 }
3342
3343 if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3344 driver->ports = kcalloc(lines, sizeof(*driver->ports),
3345 GFP_KERNEL);
3346 if (!driver->ports) {
3347 err = -ENOMEM;
3348 goto err_free_all;
3349 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003350 cdevs = lines;
3351 }
3352
3353 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
3354 if (!driver->cdevs) {
3355 err = -ENOMEM;
3356 goto err_free_all;
Jiri Slaby16a02082012-08-08 22:26:42 +02003357 }
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 return driver;
Jiri Slaby16a02082012-08-08 22:26:42 +02003360err_free_all:
3361 kfree(driver->ports);
3362 kfree(driver->ttys);
3363 kfree(driver->termios);
3364 kfree(driver);
3365 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003367EXPORT_SYMBOL(__tty_alloc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Alan Cox7d7b93c2008-10-13 10:42:09 +01003369static void destruct_tty_driver(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003371 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
3372 int i;
3373 struct ktermios *tp;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003374
3375 if (driver->flags & TTY_DRIVER_INSTALLED) {
3376 /*
3377 * Free the termios and termios_locked structures because
3378 * we don't want to get memory leaks when modular tty
3379 * drivers are removed from the kernel.
3380 */
3381 for (i = 0; i < driver->num; i++) {
3382 tp = driver->termios[i];
3383 if (tp) {
3384 driver->termios[i] = NULL;
3385 kfree(tp);
3386 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003387 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3388 tty_unregister_device(driver, i);
3389 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003390 proc_tty_unregister_driver(driver);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003391 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
3392 cdev_del(&driver->cdevs[0]);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003393 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003394 kfree(driver->cdevs);
Jiri Slaby04831dc2012-06-04 13:35:36 +02003395 kfree(driver->ports);
Jiri Slaby16a02082012-08-08 22:26:42 +02003396 kfree(driver->termios);
3397 kfree(driver->ttys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 kfree(driver);
3399}
3400
Alan Cox7d7b93c2008-10-13 10:42:09 +01003401void tty_driver_kref_put(struct tty_driver *driver)
3402{
3403 kref_put(&driver->kref, destruct_tty_driver);
3404}
3405EXPORT_SYMBOL(tty_driver_kref_put);
3406
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003407void tty_set_operations(struct tty_driver *driver,
3408 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
Alan Coxf34d7a52008-04-30 00:54:13 -07003410 driver->ops = op;
3411};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412EXPORT_SYMBOL(tty_set_operations);
3413
Alan Cox7d7b93c2008-10-13 10:42:09 +01003414void put_tty_driver(struct tty_driver *d)
3415{
3416 tty_driver_kref_put(d);
3417}
3418EXPORT_SYMBOL(put_tty_driver);
3419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420/*
3421 * Called by a tty driver to register itself.
3422 */
3423int tty_register_driver(struct tty_driver *driver)
3424{
3425 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08003426 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 dev_t dev;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003428 struct device *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003431 error = alloc_chrdev_region(&dev, driver->minor_start,
3432 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 if (!error) {
3434 driver->major = MAJOR(dev);
3435 driver->minor_start = MINOR(dev);
3436 }
3437 } else {
3438 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01003439 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 }
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003441 if (error < 0)
Jiri Slaby16a02082012-08-08 22:26:42 +02003442 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003444 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
3445 error = tty_cdev_add(driver, dev, 0, driver->num);
3446 if (error)
3447 goto err_unreg_char;
3448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003450 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003452 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003453
3454 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003455 for (i = 0; i < driver->num; i++) {
3456 d = tty_register_device(driver, i, NULL);
3457 if (IS_ERR(d)) {
3458 error = PTR_ERR(d);
Jiri Slaby16a02082012-08-08 22:26:42 +02003459 goto err_unreg_devs;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003460 }
3461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 }
3463 proc_tty_register_driver(driver);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003464 driver->flags |= TTY_DRIVER_INSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return 0;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003466
Jiri Slaby16a02082012-08-08 22:26:42 +02003467err_unreg_devs:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003468 for (i--; i >= 0; i--)
3469 tty_unregister_device(driver, i);
3470
3471 mutex_lock(&tty_mutex);
3472 list_del(&driver->tty_drivers);
3473 mutex_unlock(&tty_mutex);
3474
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003475err_unreg_char:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003476 unregister_chrdev_region(dev, driver->num);
Jiri Slaby16a02082012-08-08 22:26:42 +02003477err:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003478 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480EXPORT_SYMBOL(tty_register_driver);
3481
3482/*
3483 * Called by a tty driver to unregister itself.
3484 */
3485int tty_unregister_driver(struct tty_driver *driver)
3486{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003487#if 0
3488 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 if (driver->refcount)
3490 return -EBUSY;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003491#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3493 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003494 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003496 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return 0;
3498}
Alan Cox7d7b93c2008-10-13 10:42:09 +01003499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500EXPORT_SYMBOL(tty_unregister_driver);
3501
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003502dev_t tty_devnum(struct tty_struct *tty)
3503{
3504 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3505}
3506EXPORT_SYMBOL(tty_devnum);
3507
Alan Coxd81ed102008-10-13 10:41:42 +01003508void tty_default_fops(struct file_operations *fops)
3509{
3510 *fops = tty_fops;
3511}
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513/*
3514 * Initialize the console device. This is called *early*, so
3515 * we can't necessarily depend on lots of kernel help here.
3516 * Just do some early initializations, and do the complex setup
3517 * later.
3518 */
3519void __init console_init(void)
3520{
3521 initcall_t *call;
3522
3523 /* Setup the default TTY line discipline. */
Alan Cox01e1abb2008-07-22 11:16:55 +01003524 tty_ldisc_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
3526 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08003527 * set up the console device so that later boot sequences can
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 * inform about problems etc..
3529 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 call = __con_initcall_start;
3531 while (call < __con_initcall_end) {
3532 (*call)();
3533 call++;
3534 }
3535}
3536
Al Viro2c9ede52011-07-23 20:24:48 -04003537static char *tty_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +02003538{
3539 if (!mode)
3540 return NULL;
3541 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3542 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3543 *mode = 0666;
3544 return NULL;
3545}
3546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547static int __init tty_class_init(void)
3548{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003549 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 if (IS_ERR(tty_class))
3551 return PTR_ERR(tty_class);
Kay Sieverse454cea2009-09-18 23:01:12 +02003552 tty_class->devnode = tty_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return 0;
3554}
3555
3556postcore_initcall(tty_class_init);
3557
3558/* 3/2004 jmc: why do these devices exist? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559static struct cdev tty_cdev, console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Kay Sieversfbc92a32010-12-01 18:51:05 +01003561static ssize_t show_cons_active(struct device *dev,
3562 struct device_attribute *attr, char *buf)
3563{
3564 struct console *cs[16];
3565 int i = 0;
3566 struct console *c;
3567 ssize_t count = 0;
3568
Torben Hohnac751ef2011-01-25 15:07:35 -08003569 console_lock();
Kay Sieversa2a6a822011-01-09 16:39:14 +01003570 for_each_console(c) {
Kay Sieversfbc92a32010-12-01 18:51:05 +01003571 if (!c->device)
3572 continue;
3573 if (!c->write)
3574 continue;
3575 if ((c->flags & CON_ENABLED) == 0)
3576 continue;
3577 cs[i++] = c;
3578 if (i >= ARRAY_SIZE(cs))
3579 break;
3580 }
Hannes Reinecke723abd82014-02-27 12:30:51 +01003581 while (i--) {
3582 int index = cs[i]->index;
3583 struct tty_driver *drv = cs[i]->device(cs[i], &index);
3584
3585 /* don't resolve tty0 as some programs depend on it */
3586 if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR))
3587 count += tty_line_name(drv, index, buf + count);
3588 else
3589 count += sprintf(buf + count, "%s%d",
3590 cs[i]->name, cs[i]->index);
3591
3592 count += sprintf(buf + count, "%c", i ? ' ':'\n');
3593 }
Torben Hohnac751ef2011-01-25 15:07:35 -08003594 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01003595
3596 return count;
3597}
3598static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3599
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003600static struct attribute *cons_dev_attrs[] = {
3601 &dev_attr_active.attr,
3602 NULL
3603};
3604
3605ATTRIBUTE_GROUPS(cons_dev);
3606
Kay Sieversfbc92a32010-12-01 18:51:05 +01003607static struct device *consdev;
3608
3609void console_sysfs_notify(void)
3610{
3611 if (consdev)
3612 sysfs_notify(&consdev->kobj, NULL, "active");
3613}
3614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615/*
3616 * Ok, now we can initialize the rest of the tty devices and can count
3617 * on memory allocations, interrupts etc..
3618 */
David Howells31d1d482010-08-06 16:34:43 +01003619int __init tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 cdev_init(&tty_cdev, &tty_fops);
3622 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3623 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3624 panic("Couldn't register /dev/tty driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003625 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
3627 cdev_init(&console_cdev, &console_fops);
3628 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3629 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3630 panic("Couldn't register /dev/console driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003631 consdev = device_create_with_groups(tty_class, NULL,
3632 MKDEV(TTYAUX_MAJOR, 1), NULL,
3633 cons_dev_groups, "console");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003634 if (IS_ERR(consdev))
3635 consdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637#ifdef CONFIG_VT
Alan Coxd81ed102008-10-13 10:41:42 +01003638 vty_init(&console_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639#endif
3640 return 0;
3641}
David Howells31d1d482010-08-06 16:34:43 +01003642