blob: fb50442fd2a42c5663bb410dbd76c8db0b98fbdd [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);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700156static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700157static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Alan Coxaf9b8972006-08-27 01:24:01 -0700159/**
160 * alloc_tty_struct - allocate a tty object
161 *
162 * Return a new empty tty structure. The data fields have not
163 * been initialized in any way but has been zeroed
164 *
165 * Locking: none
Alan Coxaf9b8972006-08-27 01:24:01 -0700166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Alan Coxbf970ee2008-10-13 10:42:39 +0100168struct tty_struct *alloc_tty_struct(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Alan Cox1266b1e2006-09-29 02:00:40 -0700170 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Alan Coxaf9b8972006-08-27 01:24:01 -0700173/**
174 * free_tty_struct - free a disused tty
175 * @tty: tty struct to free
176 *
177 * Free the write buffers, tty queue and tty memory itself.
178 *
179 * Locking: none. Must be called after tty is definitely unused
180 */
181
Alan Coxbf970ee2008-10-13 10:42:39 +0100182void free_tty_struct(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Dan Carpenterdc6802a2012-07-24 12:52:04 +0100184 if (!tty)
185 return;
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +0400186 if (tty->dev)
187 put_device(tty->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kfree(tty->write_buf);
Alan Cox89c8d912012-08-08 16:30:13 +0100189 tty->magic = 0xDEADDEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 kfree(tty);
191}
192
Nick Piggind996b622010-08-18 04:37:36 +1000193static inline struct tty_struct *file_tty(struct file *file)
194{
195 return ((struct tty_file_private *)file->private_data)->tty;
196}
197
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200198int tty_alloc_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000199{
200 struct tty_file_private *priv;
201
Pekka Enbergf573bd12010-08-24 07:48:34 +0300202 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
203 if (!priv)
204 return -ENOMEM;
Nick Piggind996b622010-08-18 04:37:36 +1000205
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200206 file->private_data = priv;
207
208 return 0;
209}
210
211/* Associate a new file with the tty structure */
212void tty_add_file(struct tty_struct *tty, struct file *file)
213{
214 struct tty_file_private *priv = file->private_data;
215
Nick Piggind996b622010-08-18 04:37:36 +1000216 priv->tty = tty;
217 priv->file = file;
Nick Piggind996b622010-08-18 04:37:36 +1000218
219 spin_lock(&tty_files_lock);
220 list_add(&priv->list, &tty->tty_files);
221 spin_unlock(&tty_files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200222}
Pekka Enbergf573bd12010-08-24 07:48:34 +0300223
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200224/**
225 * tty_free_file - free file->private_data
226 *
227 * This shall be used only for fail path handling when tty_add_file was not
228 * called yet.
229 */
230void tty_free_file(struct file *file)
231{
232 struct tty_file_private *priv = file->private_data;
233
234 file->private_data = NULL;
235 kfree(priv);
Nick Piggind996b622010-08-18 04:37:36 +1000236}
237
238/* Delete file from its tty */
Josh Triplett2520e272012-11-18 21:27:47 -0800239static void tty_del_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000240{
241 struct tty_file_private *priv = file->private_data;
242
243 spin_lock(&tty_files_lock);
244 list_del(&priv->list);
245 spin_unlock(&tty_files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200246 tty_free_file(file);
Nick Piggind996b622010-08-18 04:37:36 +1000247}
248
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
251
Alan Coxaf9b8972006-08-27 01:24:01 -0700252/**
253 * tty_name - return tty naming
254 * @tty: tty structure
255 * @buf: buffer for output
256 *
257 * Convert a tty structure into a name. The name reflects the kernel
258 * naming policy and if udev is in use may not reflect user space
259 *
260 * Locking: none
261 */
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263char *tty_name(struct tty_struct *tty, char *buf)
264{
265 if (!tty) /* Hmm. NULL pointer. That's fun. */
266 strcpy(buf, "NULL tty");
267 else
268 strcpy(buf, tty->name);
269 return buf;
270}
271
272EXPORT_SYMBOL(tty_name);
273
Andrew Mortond769a662005-05-05 16:15:50 -0700274int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 const char *routine)
276{
277#ifdef TTY_PARANOIA_CHECK
278 if (!tty) {
279 printk(KERN_WARNING
280 "null TTY for (%d:%d) in %s\n",
281 imajor(inode), iminor(inode), routine);
282 return 1;
283 }
284 if (tty->magic != TTY_MAGIC) {
285 printk(KERN_WARNING
286 "bad magic number for tty struct (%d:%d) in %s\n",
287 imajor(inode), iminor(inode), routine);
288 return 1;
289 }
290#endif
291 return 0;
292}
293
294static int check_tty_count(struct tty_struct *tty, const char *routine)
295{
296#ifdef CHECK_TTY_COUNT
297 struct list_head *p;
298 int count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800299
Nick Pigginee2ffa02010-08-18 04:37:35 +1000300 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 list_for_each(p, &tty->tty_files) {
302 count++;
303 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000304 spin_unlock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
306 tty->driver->subtype == PTY_TYPE_SLAVE &&
307 tty->link && tty->link->count)
308 count++;
309 if (tty->count != count) {
310 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
311 "!= #fd's(%d) in %s\n",
312 tty->name, tty->count, count, routine);
313 return count;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#endif
316 return 0;
317}
318
Alan Coxaf9b8972006-08-27 01:24:01 -0700319/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700320 * get_tty_driver - find device of a tty
321 * @dev_t: device identifier
322 * @index: returns the index of the tty
323 *
324 * This routine returns a tty driver structure, given a device number
325 * and also passes back the index number.
326 *
327 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static struct tty_driver *get_tty_driver(dev_t device, int *index)
331{
332 struct tty_driver *p;
333
334 list_for_each_entry(p, &tty_drivers, tty_drivers) {
335 dev_t base = MKDEV(p->major, p->minor_start);
336 if (device < base || device >= base + p->num)
337 continue;
338 *index = device - base;
Alan Cox7d7b93c2008-10-13 10:42:09 +0100339 return tty_driver_kref_get(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 return NULL;
342}
343
Jason Wesself2d937f2008-04-17 20:05:37 +0200344#ifdef CONFIG_CONSOLE_POLL
345
346/**
347 * tty_find_polling_driver - find device of a polled tty
348 * @name: name string to match
349 * @line: pointer to resulting tty line nr
350 *
351 * This routine returns a tty driver structure, given a name
352 * and the condition that the tty driver is capable of polled
353 * operation.
354 */
355struct tty_driver *tty_find_polling_driver(char *name, int *line)
356{
357 struct tty_driver *p, *res = NULL;
358 int tty_line = 0;
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500359 int len;
Alan Cox5f0878a2009-06-11 12:46:41 +0100360 char *str, *stp;
Jason Wesself2d937f2008-04-17 20:05:37 +0200361
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500362 for (str = name; *str; str++)
363 if ((*str >= '0' && *str <= '9') || *str == ',')
364 break;
365 if (!*str)
366 return NULL;
367
368 len = str - name;
369 tty_line = simple_strtoul(str, &str, 10);
370
Jason Wesself2d937f2008-04-17 20:05:37 +0200371 mutex_lock(&tty_mutex);
372 /* Search through the tty devices to look for a match */
373 list_for_each_entry(p, &tty_drivers, tty_drivers) {
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500374 if (strncmp(name, p->name, len) != 0)
375 continue;
Alan Cox5f0878a2009-06-11 12:46:41 +0100376 stp = str;
377 if (*stp == ',')
378 stp++;
379 if (*stp == '\0')
380 stp = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +0200381
Nathael Pajani6eb68d62010-09-02 16:06:16 +0200382 if (tty_line >= 0 && tty_line < p->num && p->ops &&
Alan Cox5f0878a2009-06-11 12:46:41 +0100383 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
Alan Cox7d7b93c2008-10-13 10:42:09 +0100384 res = tty_driver_kref_get(p);
Jason Wesself2d937f2008-04-17 20:05:37 +0200385 *line = tty_line;
386 break;
387 }
388 }
389 mutex_unlock(&tty_mutex);
390
391 return res;
392}
393EXPORT_SYMBOL_GPL(tty_find_polling_driver);
394#endif
395
Alan Coxaf9b8972006-08-27 01:24:01 -0700396/**
397 * tty_check_change - check for POSIX terminal changes
398 * @tty: tty to check
399 *
400 * If we try to write to, or set the state of, a terminal and we're
401 * not in the foreground, send a SIGTTOU. If the signal is blocked or
402 * ignored, go ahead and perform the operation. (POSIX 7.2)
403 *
Alan Cox978e5952008-04-30 00:53:59 -0700404 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700406
Alan Cox37bdfb02008-02-08 04:18:47 -0800407int tty_check_change(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Alan Cox47f86832008-04-30 00:53:30 -0700409 unsigned long flags;
410 int ret = 0;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (current->signal->tty != tty)
413 return 0;
Alan Cox47f86832008-04-30 00:53:30 -0700414
415 spin_lock_irqsave(&tty->ctrl_lock, flags);
416
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800417 if (!tty->pgrp) {
418 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700419 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800421 if (task_pgrp(current) == tty->pgrp)
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700422 goto out_unlock;
423 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (is_ignored(SIGTTOU))
Alan Cox47f86832008-04-30 00:53:30 -0700425 goto out;
426 if (is_current_pgrp_orphaned()) {
427 ret = -EIO;
428 goto out;
429 }
Oleg Nesterov040b6362007-06-01 00:46:53 -0700430 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
431 set_thread_flag(TIF_SIGPENDING);
Alan Cox47f86832008-04-30 00:53:30 -0700432 ret = -ERESTARTSYS;
433out:
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700434 return ret;
435out_unlock:
Alan Cox47f86832008-04-30 00:53:30 -0700436 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
437 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440EXPORT_SYMBOL(tty_check_change);
441
Alan Cox37bdfb02008-02-08 04:18:47 -0800442static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 size_t count, loff_t *ppos)
444{
445 return 0;
446}
447
Alan Cox37bdfb02008-02-08 04:18:47 -0800448static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 size_t count, loff_t *ppos)
450{
451 return -EIO;
452}
453
454/* No kernel lock held - none needed ;) */
Alan Cox37bdfb02008-02-08 04:18:47 -0800455static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
458}
459
Alan Cox04f378b2008-04-30 00:53:29 -0700460static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
461 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700462{
463 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
464}
465
Alan Cox37bdfb02008-02-08 04:18:47 -0800466static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700467 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
470}
471
Arjan van de Ven62322d22006-07-03 00:24:21 -0700472static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 .llseek = no_llseek,
474 .read = tty_read,
475 .write = tty_write,
476 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700477 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700478 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 .open = tty_open,
480 .release = tty_release,
481 .fasync = tty_fasync,
482};
483
Arjan van de Ven62322d22006-07-03 00:24:21 -0700484static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .llseek = no_llseek,
486 .read = tty_read,
487 .write = redirected_tty_write,
488 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700489 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700490 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 .open = tty_open,
492 .release = tty_release,
493 .fasync = tty_fasync,
494};
495
Arjan van de Ven62322d22006-07-03 00:24:21 -0700496static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 .llseek = no_llseek,
498 .read = hung_up_tty_read,
499 .write = hung_up_tty_write,
500 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700501 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700502 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 .release = tty_release,
504};
505
506static DEFINE_SPINLOCK(redirect_lock);
507static struct file *redirect;
508
509/**
510 * tty_wakeup - request more data
511 * @tty: terminal
512 *
513 * Internal and external helper for wakeups of tty. This function
514 * informs the line discipline if present that the driver is ready
515 * to receive more output data.
516 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518void tty_wakeup(struct tty_struct *tty)
519{
520 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -0800521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
523 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800524 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100525 if (ld->ops->write_wakeup)
526 ld->ops->write_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 tty_ldisc_deref(ld);
528 }
529 }
Davide Libenzi4b194492009-03-31 15:24:24 -0700530 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533EXPORT_SYMBOL_GPL(tty_wakeup);
534
535/**
Peter Hurleyea648a42013-03-06 07:20:53 -0500536 * tty_signal_session_leader - sends SIGHUP to session leader
537 *
538 * Send SIGHUP and SIGCONT to the session leader and its
539 * process group.
540 *
541 * Returns the number of processes in the session with this tty
542 * as their controlling terminal. This value is used to drop
543 * tty references for those processes.
544 */
545static int tty_signal_session_leader(struct tty_struct *tty)
546{
547 struct task_struct *p;
548 unsigned long flags;
549 int refs = 0;
550
551 read_lock(&tasklist_lock);
552 if (tty->session) {
553 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
554 spin_lock_irq(&p->sighand->siglock);
555 if (p->signal->tty == tty) {
556 p->signal->tty = NULL;
557 /* We defer the dereferences outside fo
558 the tasklist lock */
559 refs++;
560 }
561 if (!p->signal->leader) {
562 spin_unlock_irq(&p->sighand->siglock);
563 continue;
564 }
565 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
566 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
567 put_pid(p->signal->tty_old_pgrp); /* A noop */
568 spin_lock_irqsave(&tty->ctrl_lock, flags);
569 if (tty->pgrp)
570 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
571 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
572 spin_unlock_irq(&p->sighand->siglock);
573 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
574 }
575 read_unlock(&tasklist_lock);
576
577 return refs;
578}
579
580/**
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200581 * __tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +0000582 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -0700583 *
Kevin Cernekeeef4f5272012-12-26 20:43:41 -0800584 * This can be called by a "kworker" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -0700585 * synchronous but doesn't hold any locks, so we need to make sure we
586 * have the appropriate locks for what we're doing.
587 *
588 * The hangup event clears any pending redirections onto the hung up
589 * device. It ensures future writes will error and it does the needed
590 * line discipline hangup and signal delivery. The tty object itself
591 * remains intact.
592 *
593 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200594 * BTM
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800595 * redirect lock for undoing redirection
596 * file list lock for manipulating list of ttys
597 * tty_ldisc_lock from called functions
598 * termios_mutex resetting termios data
599 * tasklist_lock to walk task list for hangup event
600 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Josh Triplett2520e272012-11-18 21:27:47 -0800602static void __tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Alan Cox37bdfb02008-02-08 04:18:47 -0800604 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct file *filp, *f = NULL;
Nick Piggind996b622010-08-18 04:37:36 +1000606 struct tty_file_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int closecount = 0, n;
Peter Hurleyea648a42013-03-06 07:20:53 -0500608 int refs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (!tty)
611 return;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 spin_lock(&redirect_lock);
Nick Piggind996b622010-08-18 04:37:36 +1000615 if (redirect && file_tty(redirect) == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 f = redirect;
617 redirect = NULL;
618 }
619 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800620
Alan Cox89c8d912012-08-08 16:30:13 +0100621 tty_lock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200622
Jiri Slabyacfa7472010-11-29 10:16:54 +0100623 /* some functions below drop BTM, so we need this bit */
624 set_bit(TTY_HUPPING, &tty->flags);
625
Arnd Bergmannec79d602010-06-01 22:53:01 +0200626 /* inuse_filps is protected by the single tty lock,
627 this really needs to change if we want to flush the
628 workqueue with the lock held */
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200629 check_tty_count(tty, "tty_hangup");
Alan Cox36ba7822009-11-30 13:18:51 +0000630
Nick Pigginee2ffa02010-08-18 04:37:35 +1000631 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Nick Piggind996b622010-08-18 04:37:36 +1000633 list_for_each_entry(priv, &tty->tty_files, list) {
634 filp = priv->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (filp->f_op->write == redirected_tty_write)
636 cons_filp = filp;
637 if (filp->f_op->write != tty_write)
638 continue;
639 closecount++;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200640 __tty_fasync(-1, filp, 0); /* can't block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 filp->f_op = &hung_up_tty_fops;
642 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000643 spin_unlock(&tty_files_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800644
Jiri Slabyacfa7472010-11-29 10:16:54 +0100645 /*
646 * it drops BTM and thus races with reopen
647 * we protect the race by TTY_HUPPING
648 */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100649 tty_ldisc_hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800650
Peter Hurleyea648a42013-03-06 07:20:53 -0500651 refs = tty_signal_session_leader(tty);
652 /* Account for the p->signal references we killed */
653 while (refs--)
654 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Peter Hurley20cc2252013-03-06 07:20:54 -0500656 spin_lock_irq(&tty->ctrl_lock);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100657 clear_bit(TTY_THROTTLED, &tty->flags);
658 clear_bit(TTY_PUSH, &tty->flags);
659 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -0600660 put_pid(tty->session);
661 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800662 tty->session = NULL;
663 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 tty->ctrl_status = 0;
Peter Hurley20cc2252013-03-06 07:20:54 -0500665 spin_unlock_irq(&tty->ctrl_lock);
Alan Cox47f86832008-04-30 00:53:30 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /*
Alan Cox37bdfb02008-02-08 04:18:47 -0800668 * If one of the devices matches a console pointer, we
669 * cannot just call hangup() because that will cause
670 * tty->count and state->count to go out of sync.
671 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
673 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -0700674 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -0700676 tty->ops->close(tty, cons_filp);
677 } else if (tty->ops->hangup)
678 (tty->ops->hangup)(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800679 /*
680 * We don't want to have driver/ldisc interactions beyond
681 * the ones we did here. The driver layer expects no
682 * calls after ->hangup() from the ldisc side. However we
683 * can't yet guarantee all that.
684 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 set_bit(TTY_HUPPED, &tty->flags);
Jiri Slabyacfa7472010-11-29 10:16:54 +0100686 clear_bit(TTY_HUPPING, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100687 tty_ldisc_enable(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200688
Alan Cox89c8d912012-08-08 16:30:13 +0100689 tty_unlock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (f)
692 fput(f);
693}
694
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200695static void do_tty_hangup(struct work_struct *work)
696{
697 struct tty_struct *tty =
698 container_of(work, struct tty_struct, hangup_work);
699
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200700 __tty_hangup(tty);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200701}
702
Alan Coxaf9b8972006-08-27 01:24:01 -0700703/**
704 * tty_hangup - trigger a hangup event
705 * @tty: tty to hangup
706 *
707 * A carrier loss (virtual or otherwise) has occurred on this like
708 * schedule a hangup sequence to run after this event.
709 */
710
Alan Cox37bdfb02008-02-08 04:18:47 -0800711void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713#ifdef TTY_DEBUG_HANGUP
714 char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
716#endif
717 schedule_work(&tty->hangup_work);
718}
719
720EXPORT_SYMBOL(tty_hangup);
721
Alan Coxaf9b8972006-08-27 01:24:01 -0700722/**
723 * tty_vhangup - process vhangup
724 * @tty: tty to hangup
725 *
726 * The user has asked via system call for the terminal to be hung up.
727 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200728 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -0700729 */
730
Alan Cox37bdfb02008-02-08 04:18:47 -0800731void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733#ifdef TTY_DEBUG_HANGUP
734 char buf[64];
735
736 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
737#endif
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200738 __tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
Alan Cox37bdfb02008-02-08 04:18:47 -0800740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741EXPORT_SYMBOL(tty_vhangup);
742
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200743
Alan Coxaf9b8972006-08-27 01:24:01 -0700744/**
Alan Cox2cb59982008-10-13 10:40:30 +0100745 * tty_vhangup_self - process vhangup for own ctty
746 *
747 * Perform a vhangup on the current controlling tty
748 */
749
750void tty_vhangup_self(void)
751{
752 struct tty_struct *tty;
753
Alan Cox2cb59982008-10-13 10:40:30 +0100754 tty = get_current_tty();
755 if (tty) {
756 tty_vhangup(tty);
757 tty_kref_put(tty);
758 }
Alan Cox2cb59982008-10-13 10:40:30 +0100759}
760
761/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700762 * tty_hung_up_p - was tty hung up
763 * @filp: file pointer of tty
764 *
765 * Return true if the tty has been subject to a vhangup or a carrier
766 * loss
767 */
768
Alan Cox37bdfb02008-02-08 04:18:47 -0800769int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 return (filp->f_op == &hung_up_tty_fops);
772}
773
774EXPORT_SYMBOL(tty_hung_up_p);
775
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800776static void session_clear_tty(struct pid *session)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800777{
778 struct task_struct *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800779 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800780 proc_clear_tty(p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800781 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800782}
783
Alan Coxaf9b8972006-08-27 01:24:01 -0700784/**
785 * disassociate_ctty - disconnect controlling tty
786 * @on_exit: true if exiting so need to "hang up" the session
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700788 * This function is typically called only by the session leader, when
789 * it wants to disassociate itself from its controlling tty.
790 *
791 * It performs the following functions:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
793 * (2) Clears the tty from being controlling the session
794 * (3) Clears the controlling tty for all processes in the
795 * session group.
796 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700797 * The argument on_exit is set to 1 if called when a process is
798 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
799 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800800 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200801 * BTM is taken for hysterical raisins, and held when
802 * called from no_tty().
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800803 * tty_mutex is taken to protect tty
804 * ->siglock is taken to protect ->signal/->sighand
805 * tasklist_lock is taken to walk process list for sessions
806 * ->siglock is taken to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809void disassociate_ctty(int on_exit)
810{
811 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Alan Cox5ec93d12009-11-30 13:18:45 +0000813 if (!current->signal->leader)
814 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800816 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (tty) {
Jiri Slaby1411dc42011-11-09 21:33:18 +0100818 struct pid *tty_pgrp = get_pid(tty->pgrp);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200819 if (on_exit) {
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200820 if (tty->driver->type != TTY_DRIVER_TYPE_PTY)
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200821 tty_vhangup(tty);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200822 }
Alan Cox452a00d2008-10-13 10:39:13 +0100823 tty_kref_put(tty);
Jiri Slaby1411dc42011-11-09 21:33:18 +0100824 if (tty_pgrp) {
825 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
826 if (!on_exit)
827 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
828 put_pid(tty_pgrp);
829 }
Eric W. Biederman680a9672007-02-12 00:52:52 -0800830 } else if (on_exit) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800831 struct pid *old_pgrp;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800832 spin_lock_irq(&current->sighand->siglock);
833 old_pgrp = current->signal->tty_old_pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800834 current->signal->tty_old_pgrp = NULL;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800835 spin_unlock_irq(&current->sighand->siglock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800836 if (old_pgrp) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800837 kill_pgrp(old_pgrp, SIGHUP, on_exit);
838 kill_pgrp(old_pgrp, SIGCONT, on_exit);
839 put_pid(old_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800844 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700845 put_pid(current->signal->tty_old_pgrp);
Randy Dunlap23cac8d2007-02-20 13:58:05 -0800846 current->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800847 spin_unlock_irq(&current->sighand->siglock);
848
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800849 tty = get_current_tty();
850 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -0700851 unsigned long flags;
852 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800853 put_pid(tty->session);
854 put_pid(tty->pgrp);
855 tty->session = NULL;
856 tty->pgrp = NULL;
Alan Cox47f86832008-04-30 00:53:30 -0700857 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +0100858 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800859 } else {
860#ifdef TTY_DEBUG_HANGUP
861 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
862 " = NULL", tty);
863#endif
864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 /* Now clear signal->tty under the lock */
867 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800868 session_clear_tty(task_session(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700872/**
873 *
874 * no_tty - Ensure the current process does not have a controlling tty
875 */
876void no_tty(void)
877{
Alan Cox3af502b2012-05-03 22:21:53 +0100878 /* FIXME: Review locking here. The tty_lock never covered any race
879 between a new association and proc_clear_tty but possible we need
880 to protect against this anyway */
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700881 struct task_struct *tsk = current;
Alan Cox5ec93d12009-11-30 13:18:45 +0000882 disassociate_ctty(0);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700883 proc_clear_tty(tsk);
884}
885
Alan Coxaf9b8972006-08-27 01:24:01 -0700886
887/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200888 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700889 * @tty: tty to stop
890 *
891 * Perform flow control to the driver. For PTY/TTY pairs we
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200892 * must also propagate the TIOCKPKT status. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -0700893 * on an already stopped device and will not re-call the driver
894 * method.
895 *
896 * This functionality is used by both the line disciplines for
897 * halting incoming flow and by the driver. It may therefore be
898 * called from any context, may be under the tty atomic_write_lock
899 * but not always.
900 *
901 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700902 * Uses the tty control lock internally
Alan Coxaf9b8972006-08-27 01:24:01 -0700903 */
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905void stop_tty(struct tty_struct *tty)
906{
Alan Cox04f378b2008-04-30 00:53:29 -0700907 unsigned long flags;
908 spin_lock_irqsave(&tty->ctrl_lock, flags);
909 if (tty->stopped) {
910 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 tty->stopped = 1;
914 if (tty->link && tty->link->packet) {
915 tty->ctrl_status &= ~TIOCPKT_START;
916 tty->ctrl_status |= TIOCPKT_STOP;
Davide Libenzi4b194492009-03-31 15:24:24 -0700917 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Alan Cox04f378b2008-04-30 00:53:29 -0700919 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700920 if (tty->ops->stop)
921 (tty->ops->stop)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924EXPORT_SYMBOL(stop_tty);
925
Alan Coxaf9b8972006-08-27 01:24:01 -0700926/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200927 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700928 * @tty: tty to start
929 *
930 * Start a tty that has been stopped if at all possible. Perform
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200931 * any necessary wakeups and propagate the TIOCPKT status. If this
Alan Coxaf9b8972006-08-27 01:24:01 -0700932 * is the tty was previous stopped and is being started then the
933 * driver start method is invoked and the line discipline woken.
934 *
935 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700936 * ctrl_lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700937 */
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939void start_tty(struct tty_struct *tty)
940{
Alan Cox04f378b2008-04-30 00:53:29 -0700941 unsigned long flags;
942 spin_lock_irqsave(&tty->ctrl_lock, flags);
943 if (!tty->stopped || tty->flow_stopped) {
944 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 tty->stopped = 0;
948 if (tty->link && tty->link->packet) {
949 tty->ctrl_status &= ~TIOCPKT_STOP;
950 tty->ctrl_status |= TIOCPKT_START;
Davide Libenzi4b194492009-03-31 15:24:24 -0700951 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Alan Cox04f378b2008-04-30 00:53:29 -0700953 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700954 if (tty->ops->start)
955 (tty->ops->start)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* If we have a running line discipline it may need kicking */
957 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960EXPORT_SYMBOL(start_tty);
961
Alan Coxaf9b8972006-08-27 01:24:01 -0700962/**
963 * tty_read - read method for tty device files
964 * @file: pointer to tty file
965 * @buf: user buffer
966 * @count: size of user buffer
967 * @ppos: unused
968 *
969 * Perform the read system call function on this terminal device. Checks
970 * for hung up devices before calling the line discipline method.
971 *
972 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -0700973 * Locks the line discipline internally while needed. Multiple
974 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -0700975 */
976
Alan Cox37bdfb02008-02-08 04:18:47 -0800977static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 loff_t *ppos)
979{
980 int i;
Nick Piggind996b622010-08-18 04:37:36 +1000981 struct tty_struct *tty = file_tty(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 struct tty_ldisc *ld;
983
Al Viro6131ffa2013-02-27 16:59:05 -0500984 if (tty_paranoia_check(tty, file_inode(file), "tty_read"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return -EIO;
986 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
987 return -EIO;
988
989 /* We want to wait for the line discipline to sort out in this
990 situation */
991 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +0100992 if (ld->ops->read)
993 i = (ld->ops->read)(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 else
995 i = -EIO;
996 tty_ldisc_deref(ld);
Jiri Slabyb0de59b2013-02-15 15:25:05 +0100997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return i;
999}
1000
Alan Cox9c1729d2007-07-15 23:39:43 -07001001void tty_write_unlock(struct tty_struct *tty)
Jiri Slaby83c67572011-04-20 10:43:18 +02001002 __releases(&tty->atomic_write_lock)
Alan Cox9c1729d2007-07-15 23:39:43 -07001003{
1004 mutex_unlock(&tty->atomic_write_lock);
Davide Libenzi4b194492009-03-31 15:24:24 -07001005 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Alan Cox9c1729d2007-07-15 23:39:43 -07001006}
1007
1008int tty_write_lock(struct tty_struct *tty, int ndelay)
Jiri Slaby83c67572011-04-20 10:43:18 +02001009 __acquires(&tty->atomic_write_lock)
Alan Cox9c1729d2007-07-15 23:39:43 -07001010{
1011 if (!mutex_trylock(&tty->atomic_write_lock)) {
1012 if (ndelay)
1013 return -EAGAIN;
1014 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1015 return -ERESTARTSYS;
1016 }
1017 return 0;
1018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020/*
1021 * Split writes up in sane blocksizes to avoid
1022 * denial-of-service type attacks
1023 */
1024static inline ssize_t do_tty_write(
1025 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1026 struct tty_struct *tty,
1027 struct file *file,
1028 const char __user *buf,
1029 size_t count)
1030{
Alan Cox9c1729d2007-07-15 23:39:43 -07001031 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -08001033
Alan Cox9c1729d2007-07-15 23:39:43 -07001034 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1035 if (ret < 0)
1036 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /*
1039 * We chunk up writes into a temporary buffer. This
1040 * simplifies low-level drivers immensely, since they
1041 * don't have locking issues and user mode accesses.
1042 *
1043 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1044 * big chunk-size..
1045 *
1046 * The default chunk-size is 2kB, because the NTTY
1047 * layer has problems with bigger chunks. It will
1048 * claim to be able to handle more characters than
1049 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -07001050 *
1051 * FIXME: This can probably go away now except that 64K chunks
1052 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
1054 chunk = 2048;
1055 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1056 chunk = 65536;
1057 if (count < chunk)
1058 chunk = count;
1059
Ingo Molnar70522e12006-03-23 03:00:31 -08001060 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (tty->write_cnt < chunk) {
Jason Wessel402fda92008-10-13 10:45:36 +01001062 unsigned char *buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 if (chunk < 1024)
1065 chunk = 1024;
1066
Jason Wessel402fda92008-10-13 10:45:36 +01001067 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1068 if (!buf_chunk) {
Alan Cox9c1729d2007-07-15 23:39:43 -07001069 ret = -ENOMEM;
1070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072 kfree(tty->write_buf);
1073 tty->write_cnt = chunk;
Jason Wessel402fda92008-10-13 10:45:36 +01001074 tty->write_buf = buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
1077 /* Do the write .. */
1078 for (;;) {
1079 size_t size = count;
1080 if (size > chunk)
1081 size = chunk;
1082 ret = -EFAULT;
1083 if (copy_from_user(tty->write_buf, buf, size))
1084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (ret <= 0)
1087 break;
1088 written += ret;
1089 buf += ret;
1090 count -= ret;
1091 if (!count)
1092 break;
1093 ret = -ERESTARTSYS;
1094 if (signal_pending(current))
1095 break;
1096 cond_resched();
1097 }
Jiri Slabyb0de59b2013-02-15 15:25:05 +01001098 if (written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 ret = written;
Alan Cox9c1729d2007-07-15 23:39:43 -07001100out:
1101 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return ret;
1103}
1104
Alan Cox95f9bfc2008-10-13 10:39:23 +01001105/**
1106 * tty_write_message - write a message to a certain tty, not just the console.
1107 * @tty: the destination tty_struct
1108 * @msg: the message to write
1109 *
1110 * This is used for messages that need to be redirected to a specific tty.
1111 * We don't put it into the syslog queue right now maybe in the future if
1112 * really needed.
1113 *
Arnd Bergmannec79d602010-06-01 22:53:01 +02001114 * We must still hold the BTM and test the CLOSING flag for the moment.
Alan Cox95f9bfc2008-10-13 10:39:23 +01001115 */
1116
1117void tty_write_message(struct tty_struct *tty, char *msg)
1118{
Alan Cox95f9bfc2008-10-13 10:39:23 +01001119 if (tty) {
1120 mutex_lock(&tty->atomic_write_lock);
Alan Cox89c8d912012-08-08 16:30:13 +01001121 tty_lock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001122 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001123 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +01001124 tty->ops->write(tty, msg, strlen(msg));
Alan Coxeeb89d92009-11-30 13:18:29 +00001125 } else
Alan Cox89c8d912012-08-08 16:30:13 +01001126 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +01001127 tty_write_unlock(tty);
1128 }
Alan Cox95f9bfc2008-10-13 10:39:23 +01001129 return;
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Alan Coxaf9b8972006-08-27 01:24:01 -07001133/**
1134 * tty_write - write method for tty device file
1135 * @file: tty file pointer
1136 * @buf: user data to write
1137 * @count: bytes to write
1138 * @ppos: unused
1139 *
1140 * Write data to a tty device via the line discipline.
1141 *
1142 * Locking:
1143 * Locks the line discipline as required
1144 * Writes to the tty driver are serialized by the atomic_write_lock
1145 * and are then processed in chunks to the device. The line discipline
Joe Petersona88a69c2009-01-02 13:40:53 +00001146 * write method will not be invoked in parallel for each device.
Alan Coxaf9b8972006-08-27 01:24:01 -07001147 */
1148
Alan Cox37bdfb02008-02-08 04:18:47 -08001149static ssize_t tty_write(struct file *file, const char __user *buf,
1150 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Nick Piggind996b622010-08-18 04:37:36 +10001152 struct tty_struct *tty = file_tty(file);
1153 struct tty_ldisc *ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ssize_t ret;
Alan Cox37bdfb02008-02-08 04:18:47 -08001155
Al Viro6131ffa2013-02-27 16:59:05 -05001156 if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001158 if (!tty || !tty->ops->write ||
Alan Cox37bdfb02008-02-08 04:18:47 -08001159 (test_bit(TTY_IO_ERROR, &tty->flags)))
1160 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001161 /* Short term debug to catch buggy drivers */
1162 if (tty->ops->write_room == NULL)
1163 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1164 tty->driver->name);
Alan Cox37bdfb02008-02-08 04:18:47 -08001165 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001166 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 ret = -EIO;
1168 else
Alan Coxa352def2008-07-16 21:53:12 +01001169 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 tty_ldisc_deref(ld);
1171 return ret;
1172}
1173
Alan Cox37bdfb02008-02-08 04:18:47 -08001174ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1175 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 struct file *p = NULL;
1178
1179 spin_lock(&redirect_lock);
Al Virocb0942b2012-08-27 14:48:26 -04001180 if (redirect)
1181 p = get_file(redirect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 spin_unlock(&redirect_lock);
1183
1184 if (p) {
1185 ssize_t res;
1186 res = vfs_write(p, buf, count, &p->f_pos);
1187 fput(p);
1188 return res;
1189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return tty_write(file, buf, count, ppos);
1191}
1192
1193static char ptychar[] = "pqrstuvwxyzabcde";
1194
Alan Coxaf9b8972006-08-27 01:24:01 -07001195/**
1196 * pty_line_name - generate name for a pty
1197 * @driver: the tty driver in use
1198 * @index: the minor number
1199 * @p: output buffer of at least 6 bytes
1200 *
1201 * Generate a name from a driver reference and write it to the output
1202 * buffer.
1203 *
1204 * Locking: None
1205 */
1206static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 int i = index + driver->name_base;
1209 /* ->name is initialized to "ttyp", but "tty" is expected */
1210 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08001211 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1212 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Alan Coxaf9b8972006-08-27 01:24:01 -07001215/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001216 * tty_line_name - generate name for a tty
Alan Coxaf9b8972006-08-27 01:24:01 -07001217 * @driver: the tty driver in use
1218 * @index: the minor number
1219 * @p: output buffer of at least 7 bytes
1220 *
1221 * Generate a name from a driver reference and write it to the output
1222 * buffer.
1223 *
1224 * Locking: None
1225 */
1226static void tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Jiri Slaby0019b402012-08-08 22:26:43 +02001228 if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
1229 strcpy(p, driver->name);
1230 else
1231 sprintf(p, "%s%d", driver->name, index + driver->name_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Alan Cox99f1fe12008-10-13 10:42:00 +01001234/**
1235 * tty_driver_lookup_tty() - find an existing tty, if any
1236 * @driver: the driver for the tty
1237 * @idx: the minor number
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001238 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001239 * Return the tty, if found or ERR_PTR() otherwise.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001240 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001241 * Locking: tty_mutex must be held. If tty is found, the mutex must
1242 * be held until the 'fast-open' is also done. Will change once we
1243 * have refcounting in the driver and per driver locking
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001244 */
Jason Wessela47d5452009-01-02 13:43:04 +00001245static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001246 struct inode *inode, int idx)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001247{
Alan Cox99f1fe12008-10-13 10:42:00 +01001248 if (driver->ops->lookup)
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001249 return driver->ops->lookup(driver, inode, idx);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001250
Jiri Slabyd4834262012-03-05 14:51:53 +01001251 return driver->ttys[idx];
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001252}
1253
Alan Cox99f1fe12008-10-13 10:42:00 +01001254/**
Alan Coxbf970ee2008-10-13 10:42:39 +01001255 * tty_init_termios - helper for termios setup
1256 * @tty: the tty to set up
1257 *
1258 * Initialise the termios structures for this tty. Thus runs under
1259 * the tty_mutex currently so we can be relaxed about ordering.
1260 */
1261
1262int tty_init_termios(struct tty_struct *tty)
1263{
Alan Coxfe6e29f2008-10-13 10:44:08 +01001264 struct ktermios *tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001265 int idx = tty->index;
1266
Alan Cox36b3c072012-07-17 17:06:57 +01001267 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1268 tty->termios = tty->driver->init_termios;
1269 else {
1270 /* Check for lazy saved data */
1271 tp = tty->driver->termios[idx];
1272 if (tp != NULL)
1273 tty->termios = *tp;
1274 else
1275 tty->termios = tty->driver->init_termios;
Alan Coxbf970ee2008-10-13 10:42:39 +01001276 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001277 /* Compatibility until drivers always set this */
Alan Coxadc8d742012-07-14 15:31:47 +01001278 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1279 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001280 return 0;
1281}
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001282EXPORT_SYMBOL_GPL(tty_init_termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001283
Jiri Slaby66d450e2012-01-30 21:14:28 +01001284int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1285{
1286 int ret = tty_init_termios(tty);
1287 if (ret)
1288 return ret;
1289
1290 tty_driver_kref_get(driver);
1291 tty->count++;
1292 driver->ttys[tty->index] = tty;
1293 return 0;
1294}
1295EXPORT_SYMBOL_GPL(tty_standard_install);
1296
Alan Coxbf970ee2008-10-13 10:42:39 +01001297/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001298 * tty_driver_install_tty() - install a tty entry in the driver
1299 * @driver: the driver for the tty
1300 * @tty: the tty
1301 *
1302 * Install a tty object into the driver tables. The tty->index field
Alan Coxbf970ee2008-10-13 10:42:39 +01001303 * will be set by the time this is called. This method is responsible
1304 * for ensuring any need additional structures are allocated and
1305 * configured.
Alan Cox8b0a88d2008-10-13 10:42:19 +01001306 *
1307 * Locking: tty_mutex for now
1308 */
1309static int tty_driver_install_tty(struct tty_driver *driver,
1310 struct tty_struct *tty)
1311{
Jiri Slaby66d450e2012-01-30 21:14:28 +01001312 return driver->ops->install ? driver->ops->install(driver, tty) :
1313 tty_standard_install(driver, tty);
Alan Cox8b0a88d2008-10-13 10:42:19 +01001314}
1315
1316/**
1317 * tty_driver_remove_tty() - remove a tty from the driver tables
1318 * @driver: the driver for the tty
1319 * @idx: the minor number
1320 *
1321 * Remvoe a tty object from the driver tables. The tty->index field
1322 * will be set by the time this is called.
1323 *
1324 * Locking: tty_mutex for now
1325 */
Jiri Slaby24d406a2011-08-10 14:59:28 +02001326void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +01001327{
1328 if (driver->ops->remove)
1329 driver->ops->remove(driver, tty);
1330 else
1331 driver->ttys[tty->index] = NULL;
1332}
1333
1334/*
1335 * tty_reopen() - fast re-open of an open tty
1336 * @tty - the tty to open
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001337 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001338 * Return 0 on success, -errno on error.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001339 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001340 * Locking: tty_mutex must be held from the time the tty was found
1341 * till this open completes.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001342 */
Alan Cox99f1fe12008-10-13 10:42:00 +01001343static int tty_reopen(struct tty_struct *tty)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001344{
1345 struct tty_driver *driver = tty->driver;
1346
Jiri Slabye2efafb2010-11-29 10:16:53 +01001347 if (test_bit(TTY_CLOSING, &tty->flags) ||
Jiri Slabyacfa7472010-11-29 10:16:54 +01001348 test_bit(TTY_HUPPING, &tty->flags) ||
Jiri Slabye2efafb2010-11-29 10:16:53 +01001349 test_bit(TTY_LDISC_CHANGING, &tty->flags))
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001350 return -EIO;
1351
1352 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1353 driver->subtype == PTY_TYPE_MASTER) {
1354 /*
1355 * special case for PTY masters: only one open permitted,
1356 * and the slave side open count is incremented as well.
1357 */
1358 if (tty->count)
1359 return -EIO;
1360
1361 tty->link->count++;
1362 }
1363 tty->count++;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001364
Alan Cox1aa4bed2009-06-16 17:01:33 +01001365 mutex_lock(&tty->ldisc_mutex);
Alan Cox99f1fe12008-10-13 10:42:00 +01001366 WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
Alan Cox1aa4bed2009-06-16 17:01:33 +01001367 mutex_unlock(&tty->ldisc_mutex);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001368
1369 return 0;
1370}
1371
Alan Coxaf9b8972006-08-27 01:24:01 -07001372/**
Alan Coxd81ed102008-10-13 10:41:42 +01001373 * tty_init_dev - initialise a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001374 * @driver: tty driver we are opening a device on
1375 * @idx: device index
Alan Cox15582d32008-10-13 10:41:03 +01001376 * @ret_tty: returned tty structure
Alan Coxaf9b8972006-08-27 01:24:01 -07001377 *
1378 * Prepare a tty device. This may not be a "new" clean device but
1379 * could also be an active device. The pty drivers require special
1380 * handling because of this.
1381 *
1382 * Locking:
1383 * The function is called under the tty_mutex, which
1384 * protects us from the tty struct or driver itself going away.
1385 *
1386 * On exit the tty device has the line discipline attached and
1387 * a reference count of 1. If a pair was created for pty/tty use
1388 * and the other was a pty master then it too has a reference count of 1.
1389 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001391 * failed open. The new code protects the open with a mutex, so it's
1392 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * relaxed for the (most common) case of reopening a tty.
1394 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001395
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001396struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Alan Coxbf970ee2008-10-13 10:42:39 +01001398 struct tty_struct *tty;
Alan Cox73ec06f2008-10-13 10:42:29 +01001399 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /*
1402 * First time open is complex, especially for PTY devices.
1403 * This code guarantees that either everything succeeds and the
1404 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001405 * and the allocated memory released. (Except that the termios
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 * and locked termios may be retained.)
1407 */
1408
Alan Cox73ec06f2008-10-13 10:42:29 +01001409 if (!try_module_get(driver->owner))
1410 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 tty = alloc_tty_struct();
Jiri Slabyd5543502011-03-23 10:48:32 +01001413 if (!tty) {
1414 retval = -ENOMEM;
1415 goto err_module_put;
1416 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001417 initialize_tty_struct(tty, driver, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Alan Cox89c8d912012-08-08 16:30:13 +01001419 tty_lock(tty);
Alan Cox73ec06f2008-10-13 10:42:29 +01001420 retval = tty_driver_install_tty(driver, tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001421 if (retval < 0)
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001422 goto err_deinit_tty;
Alan Cox8b0a88d2008-10-13 10:42:19 +01001423
Jiri Slaby04831dc2012-06-04 13:35:36 +02001424 if (!tty->port)
1425 tty->port = driver->ports[idx];
1426
Jiri Slaby5d4121c2012-08-17 14:27:52 +02001427 WARN_RATELIMIT(!tty->port,
1428 "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
1429 __func__, tty->driver->name);
1430
Jiri Slaby967fab62012-10-18 22:26:46 +02001431 tty->port->itty = tty;
1432
Alan Cox37bdfb02008-02-08 04:18:47 -08001433 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001435 * If we fail here just call release_tty to clean up. No need
1436 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 */
Alan Coxbf970ee2008-10-13 10:42:39 +01001438 retval = tty_ldisc_setup(tty, tty->link);
Alan Cox01e1abb2008-07-22 11:16:55 +01001439 if (retval)
Jiri Slabyd5543502011-03-23 10:48:32 +01001440 goto err_release_tty;
Alan Cox89c8d912012-08-08 16:30:13 +01001441 /* Return the tty locked so that it cannot vanish under the caller */
Alan Cox73ec06f2008-10-13 10:42:29 +01001442 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001444err_deinit_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001445 tty_unlock(tty);
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001446 deinitialize_tty_struct(tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001447 free_tty_struct(tty);
1448err_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 module_put(driver->owner);
Jiri Slabyd5543502011-03-23 10:48:32 +01001450 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001452 /* call the tty release_tty routine to clean out this slot */
Jiri Slabyd5543502011-03-23 10:48:32 +01001453err_release_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001454 tty_unlock(tty);
Manuel Zerpies5a3c6b22011-06-16 14:07:22 +02001455 printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, "
Akinobu Mita40509142006-09-29 02:01:27 -07001456 "clearing slot %d\n", idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001457 release_tty(tty, idx);
Alan Cox73ec06f2008-10-13 10:42:29 +01001458 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
Alan Coxfeebed62008-10-13 10:41:30 +01001461void tty_free_termios(struct tty_struct *tty)
1462{
1463 struct ktermios *tp;
1464 int idx = tty->index;
Alan Cox36b3c072012-07-17 17:06:57 +01001465
1466 /* If the port is going to reset then it has no termios to save */
1467 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1468 return;
1469
1470 /* Stash the termios data */
1471 tp = tty->driver->termios[idx];
1472 if (tp == NULL) {
1473 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1474 if (tp == NULL) {
1475 pr_warn("tty: no memory to save termios state.\n");
1476 return;
1477 }
Dan Carpenter4ac5d702012-07-24 12:51:52 +01001478 tty->driver->termios[idx] = tp;
Alan Coxfeebed62008-10-13 10:41:30 +01001479 }
Alan Cox36b3c072012-07-17 17:06:57 +01001480 *tp = tty->termios;
Alan Coxfeebed62008-10-13 10:41:30 +01001481}
1482EXPORT_SYMBOL(tty_free_termios);
1483
Alan Coxfeebed62008-10-13 10:41:30 +01001484
Alan Coxaf9b8972006-08-27 01:24:01 -07001485/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001486 * release_one_tty - release tty structure memory
Alan Cox9c9f4de2008-10-13 10:37:26 +01001487 * @kref: kref of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001488 *
1489 * Releases memory associated with a tty structure, and clears out the
1490 * driver table slots. This function is called when a device is no longer
1491 * in use. It also gets called when setup of a device fails.
1492 *
1493 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -07001494 * takes the file list lock internally when working on the list
1495 * of ttys that the driver keeps.
Alan Coxb50989d2009-09-19 13:13:22 -07001496 *
1497 * This method gets called from a work queue so that the driver private
Dave Youngf278a2f2009-09-27 16:00:42 +00001498 * cleanup ops can sleep (needed for USB at least)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
Alan Coxb50989d2009-09-19 13:13:22 -07001500static void release_one_tty(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Alan Coxb50989d2009-09-19 13:13:22 -07001502 struct tty_struct *tty =
1503 container_of(work, struct tty_struct, hangup_work);
Alan Cox6f967f72008-10-13 10:37:36 +01001504 struct tty_driver *driver = tty->driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Dave Youngf278a2f2009-09-27 16:00:42 +00001506 if (tty->ops->cleanup)
1507 tty->ops->cleanup(tty);
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 tty->magic = 0;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001510 tty_driver_kref_put(driver);
Alan Cox6f967f72008-10-13 10:37:36 +01001511 module_put(driver->owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001512
Nick Pigginee2ffa02010-08-18 04:37:35 +10001513 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 list_del_init(&tty->tty_files);
Nick Pigginee2ffa02010-08-18 04:37:35 +10001515 spin_unlock(&tty_files_lock);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001516
Oleg Nesterov6da8d862010-04-02 18:05:12 +02001517 put_pid(tty->pgrp);
1518 put_pid(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 free_tty_struct(tty);
1520}
1521
Alan Coxb50989d2009-09-19 13:13:22 -07001522static void queue_release_one_tty(struct kref *kref)
1523{
1524 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Dave Youngf278a2f2009-09-27 16:00:42 +00001525
Alan Coxb50989d2009-09-19 13:13:22 -07001526 /* The hangup queue is now free so we can reuse it rather than
1527 waste a chunk of memory for each port */
1528 INIT_WORK(&tty->hangup_work, release_one_tty);
1529 schedule_work(&tty->hangup_work);
1530}
1531
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001532/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001533 * tty_kref_put - release a tty kref
1534 * @tty: tty device
1535 *
1536 * Release a reference to a tty device and if need be let the kref
1537 * layer destruct the object for us
1538 */
1539
1540void tty_kref_put(struct tty_struct *tty)
1541{
1542 if (tty)
Alan Coxb50989d2009-09-19 13:13:22 -07001543 kref_put(&tty->kref, queue_release_one_tty);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001544}
1545EXPORT_SYMBOL(tty_kref_put);
1546
1547/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001548 * release_tty - release tty structure memory
1549 *
1550 * Release both @tty and a possible linked partner (think pty pair),
1551 * and decrement the refcount of the backing module.
1552 *
1553 * Locking:
Alan Coxd1552552012-07-27 18:02:54 +01001554 * tty_mutex
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001555 * takes the file list lock internally when working on the list
1556 * of ttys that the driver keeps.
Alan Cox9c9f4de2008-10-13 10:37:26 +01001557 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001558 */
1559static void release_tty(struct tty_struct *tty, int idx)
1560{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001561 /* This should always be true but check for the moment */
1562 WARN_ON(tty->index != idx);
Alan Coxd1552552012-07-27 18:02:54 +01001563 WARN_ON(!mutex_is_locked(&tty_mutex));
Alan Cox36b3c072012-07-17 17:06:57 +01001564 if (tty->ops->shutdown)
1565 tty->ops->shutdown(tty);
1566 tty_free_termios(tty);
1567 tty_driver_remove_tty(tty->driver, tty);
Jiri Slaby967fab62012-10-18 22:26:46 +02001568 tty->port->itty = NULL;
Alan Cox36b3c072012-07-17 17:06:57 +01001569
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001570 if (tty->link)
Alan Cox9c9f4de2008-10-13 10:37:26 +01001571 tty_kref_put(tty->link);
1572 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001573}
1574
Alan Coxeeb89d92009-11-30 13:18:29 +00001575/**
Jiri Slaby955787ca2011-11-11 10:47:23 +01001576 * tty_release_checks - check a tty before real release
1577 * @tty: tty to check
1578 * @o_tty: link of @tty (if any)
1579 * @idx: index of the tty
1580 *
1581 * Performs some paranoid checking before true release of the @tty.
1582 * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1583 */
1584static int tty_release_checks(struct tty_struct *tty, struct tty_struct *o_tty,
1585 int idx)
1586{
1587#ifdef TTY_PARANOIA_CHECK
1588 if (idx < 0 || idx >= tty->driver->num) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001589 printk(KERN_DEBUG "%s: bad idx when trying to free (%s)\n",
1590 __func__, tty->name);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001591 return -1;
1592 }
1593
1594 /* not much to check for devpts */
1595 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1596 return 0;
1597
1598 if (tty != tty->driver->ttys[idx]) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001599 printk(KERN_DEBUG "%s: driver.table[%d] not tty for (%s)\n",
1600 __func__, idx, tty->name);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001601 return -1;
1602 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001603 if (tty->driver->other) {
1604 if (o_tty != tty->driver->other->ttys[idx]) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001605 printk(KERN_DEBUG "%s: other->table[%d] not o_tty for (%s)\n",
1606 __func__, idx, tty->name);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001607 return -1;
1608 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001609 if (o_tty->link != tty) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001610 printk(KERN_DEBUG "%s: bad pty pointers\n", __func__);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001611 return -1;
1612 }
1613 }
1614#endif
1615 return 0;
1616}
1617
1618/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001619 * tty_release - vfs callback for close
1620 * @inode: inode of tty
1621 * @filp: file pointer for handle to tty
1622 *
1623 * Called the last time each file handle is closed that references
1624 * this tty. There may however be several such references.
1625 *
1626 * Locking:
1627 * Takes bkl. See tty_release_dev
1628 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 * Even releasing the tty structures is a tricky business.. We have
1630 * to be very careful that the structures are all released at the
1631 * same time, as interrupts might otherwise get the wrong pointers.
1632 *
1633 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1634 * lead to double frees or releasing memory still in use.
1635 */
Alan Coxeeb89d92009-11-30 13:18:29 +00001636
1637int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Nick Piggind996b622010-08-18 04:37:36 +10001639 struct tty_struct *tty = file_tty(filp);
1640 struct tty_struct *o_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int pty_master, tty_closing, o_tty_closing, do_sleep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int idx;
1643 char buf[64];
Alan Cox37bdfb02008-02-08 04:18:47 -08001644
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001645 if (tty_paranoia_check(tty, inode, __func__))
Alan Coxeeb89d92009-11-30 13:18:29 +00001646 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Alan Cox89c8d912012-08-08 16:30:13 +01001648 tty_lock(tty);
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001649 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Arnd Bergmannec79d602010-06-01 22:53:01 +02001651 __tty_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 idx = tty->index;
1654 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1655 tty->driver->subtype == PTY_TYPE_MASTER);
Alan Cox89c8d912012-08-08 16:30:13 +01001656 /* Review: parallel close */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 o_tty = tty->link;
1658
Jiri Slaby955787ca2011-11-11 10:47:23 +01001659 if (tty_release_checks(tty, o_tty, idx)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001660 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001665 printk(KERN_DEBUG "%s: %s (tty count=%d)...\n", __func__,
1666 tty_name(tty, buf), tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667#endif
1668
Alan Coxf34d7a52008-04-30 00:54:13 -07001669 if (tty->ops->close)
1670 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Alan Cox89c8d912012-08-08 16:30:13 +01001672 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /*
1674 * Sanity check: if tty->count is going to zero, there shouldn't be
1675 * any waiters on tty->read_wait or tty->write_wait. We test the
1676 * wait queues and kick everyone out _before_ actually starting to
1677 * close. This ensures that we won't block while releasing the tty
1678 * structure.
1679 *
1680 * The test for the o_tty closing is necessary, since the master and
1681 * slave sides may close in any order. If the slave side closes out
1682 * first, its count will be one, since the master side holds an open.
1683 * Thus this test wouldn't be triggered at the time the slave closes,
1684 * so we do it now.
1685 *
1686 * Note that it's possible for the tty to be opened again while we're
1687 * flushing out waiters. By recalculating the closing flags before
1688 * each iteration we avoid any problems.
1689 */
1690 while (1) {
1691 /* Guard against races with tty->count changes elsewhere and
1692 opens on /dev/tty */
Alan Cox37bdfb02008-02-08 04:18:47 -08001693
Ingo Molnar70522e12006-03-23 03:00:31 -08001694 mutex_lock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01001695 tty_lock_pair(tty, o_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 tty_closing = tty->count <= 1;
1697 o_tty_closing = o_tty &&
1698 (o_tty->count <= (pty_master ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 do_sleep = 0;
1700
1701 if (tty_closing) {
1702 if (waitqueue_active(&tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001703 wake_up_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 do_sleep++;
1705 }
1706 if (waitqueue_active(&tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001707 wake_up_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 do_sleep++;
1709 }
1710 }
1711 if (o_tty_closing) {
1712 if (waitqueue_active(&o_tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001713 wake_up_poll(&o_tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 do_sleep++;
1715 }
1716 if (waitqueue_active(&o_tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001717 wake_up_poll(&o_tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 do_sleep++;
1719 }
1720 }
1721 if (!do_sleep)
1722 break;
1723
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001724 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n",
1725 __func__, tty_name(tty, buf));
Alan Cox89c8d912012-08-08 16:30:13 +01001726 tty_unlock_pair(tty, o_tty);
Ingo Molnar70522e12006-03-23 03:00:31 -08001727 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 schedule();
Alan Cox37bdfb02008-02-08 04:18:47 -08001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08001732 * The closing flags are now consistent with the open counts on
1733 * both sides, and we've completed the last operation that could
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * block, so it's safe to proceed with closing.
Alan Coxd1552552012-07-27 18:02:54 +01001735 *
1736 * We must *not* drop the tty_mutex until we ensure that a further
1737 * entry into tty_open can not pick up this tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (pty_master) {
1740 if (--o_tty->count < 0) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001741 printk(KERN_WARNING "%s: bad pty slave count (%d) for %s\n",
1742 __func__, o_tty->count, tty_name(o_tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 o_tty->count = 0;
1744 }
1745 }
1746 if (--tty->count < 0) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001747 printk(KERN_WARNING "%s: bad tty->count (%d) for %s\n",
1748 __func__, tty->count, tty_name(tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 tty->count = 0;
1750 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 /*
1753 * We've decremented tty->count, so we need to remove this file
1754 * descriptor off the tty->tty_files list; this serves two
1755 * purposes:
1756 * - check_tty_count sees the correct number of file descriptors
1757 * associated with this tty.
1758 * - do_tty_hangup no longer sees this file descriptor as
1759 * something that needs to be handled for hangups.
1760 */
Nick Piggind996b622010-08-18 04:37:36 +10001761 tty_del_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 /*
1764 * Perform some housekeeping before deciding whether to return.
1765 *
1766 * Set the TTY_CLOSING flag if this was the last open. In the
1767 * case of a pty we may have to wait around for the other side
1768 * to close, and TTY_CLOSING makes sure we can't be reopened.
1769 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001770 if (tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 set_bit(TTY_CLOSING, &tty->flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08001772 if (o_tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 set_bit(TTY_CLOSING, &o_tty->flags);
1774
1775 /*
1776 * If _either_ side is closing, make sure there aren't any
1777 * processes that still think tty or o_tty is their controlling
1778 * tty.
1779 */
1780 if (tty_closing || o_tty_closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001782 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001784 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 read_unlock(&tasklist_lock);
1786 }
1787
Ingo Molnar70522e12006-03-23 03:00:31 -08001788 mutex_unlock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01001789 tty_unlock_pair(tty, o_tty);
Alan Coxd1552552012-07-27 18:02:54 +01001790 /* At this point the TTY_CLOSING flag should ensure a dead tty
1791 cannot be re-opened by a racing opener */
Paul Fulghumda965822006-02-14 13:53:00 -08001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 /* check whether both sides are closing ... */
Alan Coxd1552552012-07-27 18:02:54 +01001794 if (!tty_closing || (o_tty && !o_tty_closing))
Alan Coxeeb89d92009-11-30 13:18:29 +00001795 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001798 printk(KERN_DEBUG "%s: freeing tty structure...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799#endif
1800 /*
Alan Cox01e1abb2008-07-22 11:16:55 +01001801 * Ask the line discipline code to release its structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 */
Alan Cox01e1abb2008-07-22 11:16:55 +01001803 tty_ldisc_release(tty, o_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 /*
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001805 * The release_tty function takes care of the details of clearing
Alan Cox89c8d912012-08-08 16:30:13 +01001806 * the slots and preserving the termios structure. The tty_unlock_pair
1807 * should be safe as we keep a kref while the tty is locked (so the
1808 * unlock never unlocks a freed tty).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
Alan Coxd1552552012-07-27 18:02:54 +01001810 mutex_lock(&tty_mutex);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001811 release_tty(tty, idx);
Alan Coxd1552552012-07-27 18:02:54 +01001812 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Alan Coxeeb89d92009-11-30 13:18:29 +00001814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Alan Coxaf9b8972006-08-27 01:24:01 -07001817/**
Jiri Slabyb82154a2011-11-09 21:33:19 +01001818 * tty_open_current_tty - get tty of current task for open
1819 * @device: device number
1820 * @filp: file pointer to tty
1821 * @return: tty of the current task iff @device is /dev/tty
1822 *
1823 * We cannot return driver and index like for the other nodes because
1824 * devpts will not work then. It expects inodes to be from devpts FS.
Alan Cox3af502b2012-05-03 22:21:53 +01001825 *
1826 * We need to move to returning a refcounted object from all the lookup
1827 * paths including this one.
Jiri Slabyb82154a2011-11-09 21:33:19 +01001828 */
1829static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1830{
1831 struct tty_struct *tty;
1832
1833 if (device != MKDEV(TTYAUX_MAJOR, 0))
1834 return NULL;
1835
1836 tty = get_current_tty();
1837 if (!tty)
1838 return ERR_PTR(-ENXIO);
1839
1840 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1841 /* noctty = 1; */
1842 tty_kref_put(tty);
1843 /* FIXME: we put a reference and return a TTY! */
Alan Cox3af502b2012-05-03 22:21:53 +01001844 /* This is only safe because the caller holds tty_mutex */
Jiri Slabyb82154a2011-11-09 21:33:19 +01001845 return tty;
1846}
1847
1848/**
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001849 * tty_lookup_driver - lookup a tty driver for a given device file
1850 * @device: device number
1851 * @filp: file pointer to tty
1852 * @noctty: set if the device should not become a controlling tty
1853 * @index: index for the device in the @return driver
1854 * @return: driver for this inode (with increased refcount)
1855 *
1856 * If @return is not erroneous, the caller is responsible to decrement the
1857 * refcount by tty_driver_kref_put.
1858 *
1859 * Locking: tty_mutex protects get_tty_driver
1860 */
1861static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
1862 int *noctty, int *index)
1863{
1864 struct tty_driver *driver;
1865
Jiri Slaby2cd00502011-11-09 21:33:22 +01001866 switch (device) {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001867#ifdef CONFIG_VT
Jiri Slaby2cd00502011-11-09 21:33:22 +01001868 case MKDEV(TTY_MAJOR, 0): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001869 extern struct tty_driver *console_driver;
1870 driver = tty_driver_kref_get(console_driver);
1871 *index = fg_console;
1872 *noctty = 1;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001873 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001874 }
1875#endif
Jiri Slaby2cd00502011-11-09 21:33:22 +01001876 case MKDEV(TTYAUX_MAJOR, 1): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001877 struct tty_driver *console_driver = console_device(index);
1878 if (console_driver) {
1879 driver = tty_driver_kref_get(console_driver);
1880 if (driver) {
1881 /* Don't let /dev/console block */
1882 filp->f_flags |= O_NONBLOCK;
1883 *noctty = 1;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001884 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001885 }
1886 }
1887 return ERR_PTR(-ENODEV);
1888 }
Jiri Slaby2cd00502011-11-09 21:33:22 +01001889 default:
1890 driver = get_tty_driver(device, index);
1891 if (!driver)
1892 return ERR_PTR(-ENODEV);
1893 break;
1894 }
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001895 return driver;
1896}
1897
1898/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001899 * tty_open - open a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001900 * @inode: inode of device file
1901 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001903 * tty_open and tty_release keep up the tty count that contains the
1904 * number of opens done on a tty. We cannot use the inode-count, as
1905 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001907 * Open-counting is needed for pty masters, as well as for keeping
1908 * track of serial lines: DTR is dropped when the last close happens.
1909 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1910 *
1911 * The termios state of a pty is reset on first open so that
1912 * settings don't persist across reuse.
1913 *
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001914 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001915 * tty->count should protect the rest.
1916 * ->siglock protects ->signal/->sighand
Alan Cox89c8d912012-08-08 16:30:13 +01001917 *
1918 * Note: the tty_unlock/lock cases without a ref are only safe due to
1919 * tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001921
Alan Coxeeb89d92009-11-30 13:18:29 +00001922static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Jiri Slabyb82154a2011-11-09 21:33:19 +01001924 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 int noctty, retval;
Jiri Slabyb82154a2011-11-09 21:33:19 +01001926 struct tty_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int index;
1928 dev_t device = inode->i_rdev;
Andrew Morton846c1512009-04-02 16:56:36 -07001929 unsigned saved_flags = filp->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933retry_open:
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02001934 retval = tty_alloc_file(filp);
1935 if (retval)
1936 return -ENOMEM;
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 noctty = filp->f_flags & O_NOCTTY;
1939 index = -1;
1940 retval = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001941
Ingo Molnar70522e12006-03-23 03:00:31 -08001942 mutex_lock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01001943 /* This is protected by the tty_mutex */
Jiri Slabyb82154a2011-11-09 21:33:19 +01001944 tty = tty_open_current_tty(device, filp);
1945 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001946 retval = PTR_ERR(tty);
1947 goto err_unlock;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001948 } else if (!tty) {
1949 driver = tty_lookup_driver(device, filp, &noctty, &index);
1950 if (IS_ERR(driver)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001951 retval = PTR_ERR(driver);
1952 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001955 /* check whether we're reopening an existing tty */
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001956 tty = tty_driver_lookup_tty(driver, inode, index);
Eric Paris808ffa32009-01-27 11:50:37 +00001957 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001958 retval = PTR_ERR(tty);
1959 goto err_unlock;
Eric Paris808ffa32009-01-27 11:50:37 +00001960 }
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001961 }
1962
1963 if (tty) {
Alan Cox89c8d912012-08-08 16:30:13 +01001964 tty_lock(tty);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001965 retval = tty_reopen(tty);
Alan Cox89c8d912012-08-08 16:30:13 +01001966 if (retval < 0) {
1967 tty_unlock(tty);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001968 tty = ERR_PTR(retval);
Alan Cox89c8d912012-08-08 16:30:13 +01001969 }
1970 } else /* Returns with the tty_lock held for now */
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001971 tty = tty_init_dev(driver, index);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001972
Ingo Molnar70522e12006-03-23 03:00:31 -08001973 mutex_unlock(&tty_mutex);
Jiri Slabyb82154a2011-11-09 21:33:19 +01001974 if (driver)
1975 tty_driver_kref_put(driver);
Alan Coxeeb89d92009-11-30 13:18:29 +00001976 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001977 retval = PTR_ERR(tty);
1978 goto err_file;
Alan Coxeeb89d92009-11-30 13:18:29 +00001979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02001981 tty_add_file(tty, filp);
Nick Piggind996b622010-08-18 04:37:36 +10001982
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001983 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1985 tty->driver->subtype == PTY_TYPE_MASTER)
1986 noctty = 1;
1987#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001988 printk(KERN_DEBUG "%s: opening %s...\n", __func__, tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989#endif
Herton Ronaldo Krzesinski909bc772011-03-31 15:35:31 -03001990 if (tty->ops->open)
1991 retval = tty->ops->open(tty, filp);
1992 else
1993 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 filp->f_flags = saved_flags;
1995
Alan Cox37bdfb02008-02-08 04:18:47 -08001996 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1997 !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 retval = -EBUSY;
1999
2000 if (retval) {
2001#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01002002 printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__,
2003 retval, tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004#endif
Alan Cox89c8d912012-08-08 16:30:13 +01002005 tty_unlock(tty); /* need to call tty_release without BTM */
Alan Coxeeb89d92009-11-30 13:18:29 +00002006 tty_release(inode, filp);
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002007 if (retval != -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002009
2010 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 schedule();
2014 /*
2015 * Need to reset f_op in case a hangup happened.
2016 */
2017 if (filp->f_op == &hung_up_tty_fops)
2018 filp->f_op = &tty_fops;
2019 goto retry_open;
2020 }
Alan Cox89c8d912012-08-08 16:30:13 +01002021 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00002022
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002023
2024 mutex_lock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01002025 tty_lock(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002026 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (!noctty &&
2028 current->signal->leader &&
2029 !current->signal->tty &&
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002030 tty->session == NULL)
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07002031 __proc_set_tty(current, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002032 spin_unlock_irq(&current->sighand->siglock);
Alan Cox89c8d912012-08-08 16:30:13 +01002033 tty_unlock(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002034 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return 0;
Jiri Slabyba5db442011-11-09 21:33:21 +01002036err_unlock:
Jiri Slabyba5db442011-11-09 21:33:21 +01002037 mutex_unlock(&tty_mutex);
2038 /* after locks to avoid deadlock */
2039 if (!IS_ERR_OR_NULL(driver))
2040 tty_driver_kref_put(driver);
2041err_file:
2042 tty_free_file(filp);
2043 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Jonathan Corbet39d95b92008-05-16 09:10:50 -06002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Alan Coxaf9b8972006-08-27 01:24:01 -07002048/**
2049 * tty_poll - check tty status
2050 * @filp: file being polled
2051 * @wait: poll wait structures to update
2052 *
2053 * Call the line discipline polling method to obtain the poll
2054 * status of the device.
2055 *
2056 * Locking: locks called line discipline but ldisc poll method
2057 * may be re-entered freely by other callers.
2058 */
2059
Alan Cox37bdfb02008-02-08 04:18:47 -08002060static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Nick Piggind996b622010-08-18 04:37:36 +10002062 struct tty_struct *tty = file_tty(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct tty_ldisc *ld;
2064 int ret = 0;
2065
Al Viro6131ffa2013-02-27 16:59:05 -05002066 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002070 if (ld->ops->poll)
2071 ret = (ld->ops->poll)(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 tty_ldisc_deref(ld);
2073 return ret;
2074}
2075
Arnd Bergmannec79d602010-06-01 22:53:01 +02002076static int __tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Nick Piggind996b622010-08-18 04:37:36 +10002078 struct tty_struct *tty = file_tty(filp);
Alan Cox47f86832008-04-30 00:53:30 -07002079 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002080 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Al Viro6131ffa2013-02-27 16:59:05 -05002082 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002083 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 retval = fasync_helper(fd, filp, on, &tty->fasync);
2086 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002087 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002090 enum pid_type type;
2091 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (!waitqueue_active(&tty->read_wait))
2093 tty->minimum_to_wake = 1;
Alan Cox47f86832008-04-30 00:53:30 -07002094 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002095 if (tty->pgrp) {
2096 pid = tty->pgrp;
2097 type = PIDTYPE_PGID;
2098 } else {
2099 pid = task_pid(current);
2100 type = PIDTYPE_PID;
2101 }
Linus Torvalds80e1e822010-02-07 10:11:23 -08002102 get_pid(pid);
Greg Kroah-Hartman70362512009-12-17 07:07:19 -08002103 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds80e1e822010-02-07 10:11:23 -08002104 retval = __f_setown(filp, pid, type, 0);
2105 put_pid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (retval)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 } else {
2109 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2110 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2111 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002112 retval = 0;
2113out:
Arnd Bergmannec79d602010-06-01 22:53:01 +02002114 return retval;
2115}
2116
2117static int tty_fasync(int fd, struct file *filp, int on)
2118{
Alan Cox89c8d912012-08-08 16:30:13 +01002119 struct tty_struct *tty = file_tty(filp);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002120 int retval;
Alan Cox89c8d912012-08-08 16:30:13 +01002121
2122 tty_lock(tty);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002123 retval = __tty_fasync(fd, filp, on);
Alan Cox89c8d912012-08-08 16:30:13 +01002124 tty_unlock(tty);
2125
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002126 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Alan Coxaf9b8972006-08-27 01:24:01 -07002129/**
2130 * tiocsti - fake input character
2131 * @tty: tty to fake input into
2132 * @p: pointer to character
2133 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02002134 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07002135 * input management.
2136 *
2137 * FIXME: does not honour flow control ??
2138 *
2139 * Locking:
2140 * Called functions take tty_ldisc_lock
2141 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07002142 *
2143 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07002144 */
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146static int tiocsti(struct tty_struct *tty, char __user *p)
2147{
2148 char ch, mbz = 0;
2149 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2152 return -EPERM;
2153 if (get_user(ch, p))
2154 return -EFAULT;
Al Viro1e641742008-12-09 09:23:33 +00002155 tty_audit_tiocsti(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002157 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 tty_ldisc_deref(ld);
2159 return 0;
2160}
2161
Alan Coxaf9b8972006-08-27 01:24:01 -07002162/**
2163 * tiocgwinsz - implement window query ioctl
2164 * @tty; tty
2165 * @arg: user buffer for result
2166 *
Alan Cox808a0d32006-09-29 02:00:40 -07002167 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002168 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002169 * Locking: tty->termios_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002170 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002171 */
2172
Alan Cox37bdfb02008-02-08 04:18:47 -08002173static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Alan Cox808a0d32006-09-29 02:00:40 -07002175 int err;
2176
Arjan van de Ven5785c952006-09-29 02:00:43 -07002177 mutex_lock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002178 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Arjan van de Ven5785c952006-09-29 02:00:43 -07002179 mutex_unlock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002180
2181 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Alan Coxaf9b8972006-08-27 01:24:01 -07002184/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002185 * tty_do_resize - resize event
2186 * @tty: tty being resized
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002187 * @rows: rows (character)
2188 * @cols: cols (character)
Alan Coxaf9b8972006-08-27 01:24:01 -07002189 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002190 * Update the termios variables and send the necessary signals to
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002191 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002192 */
2193
Alan Coxfc6f6232009-01-02 13:43:17 +00002194int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Alan Coxfc6f6232009-01-02 13:43:17 +00002196 struct pid *pgrp;
Alan Cox47f86832008-04-30 00:53:30 -07002197 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Alan Coxfc6f6232009-01-02 13:43:17 +00002199 /* Lock the tty */
2200 mutex_lock(&tty->termios_mutex);
2201 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002202 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002203 /* Get the PID values and reference them so we can
2204 avoid holding the tty ctrl lock while sending signals */
2205 spin_lock_irqsave(&tty->ctrl_lock, flags);
2206 pgrp = get_pid(tty->pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002207 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2208
2209 if (pgrp)
2210 kill_pgrp(pgrp, SIGWINCH, 1);
Alan Cox47f86832008-04-30 00:53:30 -07002211 put_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002212
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002213 tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002214done:
Alan Coxfc6f6232009-01-02 13:43:17 +00002215 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return 0;
2217}
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01002218EXPORT_SYMBOL(tty_do_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Alan Coxaf9b8972006-08-27 01:24:01 -07002220/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002221 * tiocswinsz - implement window size set ioctl
Alan Coxfc6f6232009-01-02 13:43:17 +00002222 * @tty; tty side of tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002223 * @arg: user buffer for result
2224 *
2225 * Copies the user idea of the window size to the kernel. Traditionally
2226 * this is just advisory information but for the Linux console it
2227 * actually has driver level meaning and triggers a VC resize.
2228 *
2229 * Locking:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002230 * Driver dependent. The default do_resize method takes the
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002231 * tty termios mutex and ctrl_lock. The console takes its own lock
2232 * then calls into the default method.
2233 */
2234
Alan Coxfc6f6232009-01-02 13:43:17 +00002235static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002236{
2237 struct winsize tmp_ws;
2238 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2239 return -EFAULT;
2240
2241 if (tty->ops->resize)
Alan Coxfc6f6232009-01-02 13:43:17 +00002242 return tty->ops->resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002243 else
Alan Coxfc6f6232009-01-02 13:43:17 +00002244 return tty_do_resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002245}
2246
2247/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002248 * tioccons - allow admin to move logical console
2249 * @file: the file to become console
2250 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002251 * Allow the administrator to move the redirected console device
Alan Coxaf9b8972006-08-27 01:24:01 -07002252 *
2253 * Locking: uses redirect_lock to guard the redirect information
2254 */
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256static int tioccons(struct file *file)
2257{
2258 if (!capable(CAP_SYS_ADMIN))
2259 return -EPERM;
2260 if (file->f_op->write == redirected_tty_write) {
2261 struct file *f;
2262 spin_lock(&redirect_lock);
2263 f = redirect;
2264 redirect = NULL;
2265 spin_unlock(&redirect_lock);
2266 if (f)
2267 fput(f);
2268 return 0;
2269 }
2270 spin_lock(&redirect_lock);
2271 if (redirect) {
2272 spin_unlock(&redirect_lock);
2273 return -EBUSY;
2274 }
Al Virocb0942b2012-08-27 14:48:26 -04002275 redirect = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 spin_unlock(&redirect_lock);
2277 return 0;
2278}
2279
Alan Coxaf9b8972006-08-27 01:24:01 -07002280/**
2281 * fionbio - non blocking ioctl
2282 * @file: file to set blocking value
2283 * @p: user parameter
2284 *
2285 * Historical tty interfaces had a blocking control ioctl before
2286 * the generic functionality existed. This piece of history is preserved
2287 * in the expected tty API of posix OS's.
2288 *
Alan Cox6146b9a2009-09-19 13:13:19 -07002289 * Locking: none, the open file handle ensures it won't go away.
Alan Coxaf9b8972006-08-27 01:24:01 -07002290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292static int fionbio(struct file *file, int __user *p)
2293{
2294 int nonblock;
2295
2296 if (get_user(nonblock, p))
2297 return -EFAULT;
2298
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002299 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (nonblock)
2301 file->f_flags |= O_NONBLOCK;
2302 else
2303 file->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002304 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return 0;
2306}
2307
Alan Coxaf9b8972006-08-27 01:24:01 -07002308/**
2309 * tiocsctty - set controlling tty
2310 * @tty: tty structure
2311 * @arg: user argument
2312 *
2313 * This ioctl is used to manage job control. It permits a session
2314 * leader to set this tty as the controlling tty for the session.
2315 *
2316 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002317 * Takes tty_mutex() to protect tty instance
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002318 * Takes tasklist_lock internally to walk sessions
2319 * Takes ->siglock() when updating signal->tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002320 */
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322static int tiocsctty(struct tty_struct *tty, int arg)
2323{
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002324 int ret = 0;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002325 if (current->signal->leader && (task_session(current) == tty->session))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002326 return ret;
2327
2328 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 /*
2330 * The process must be a session leader and
2331 * not have a controlling tty already.
2332 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002333 if (!current->signal->leader || current->signal->tty) {
2334 ret = -EPERM;
2335 goto unlock;
2336 }
2337
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002338 if (tty->session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /*
2340 * This tty is already the controlling
2341 * tty for another session group!
2342 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002343 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 /*
2345 * Steal it away
2346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002348 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 read_unlock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002350 } else {
2351 ret = -EPERM;
2352 goto unlock;
2353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002355 proc_set_tty(current, tty);
2356unlock:
Alan Cox28298232006-09-29 02:00:58 -07002357 mutex_unlock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
Alan Coxaf9b8972006-08-27 01:24:01 -07002361/**
Alan Cox5d0fdf12008-04-30 00:53:31 -07002362 * tty_get_pgrp - return a ref counted pgrp pid
2363 * @tty: tty to read
2364 *
2365 * Returns a refcounted instance of the pid struct for the process
2366 * group controlling the tty.
2367 */
2368
2369struct pid *tty_get_pgrp(struct tty_struct *tty)
2370{
2371 unsigned long flags;
2372 struct pid *pgrp;
2373
2374 spin_lock_irqsave(&tty->ctrl_lock, flags);
2375 pgrp = get_pid(tty->pgrp);
2376 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2377
2378 return pgrp;
2379}
2380EXPORT_SYMBOL_GPL(tty_get_pgrp);
2381
2382/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002383 * tiocgpgrp - get process group
2384 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002385 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002386 * @p: returned pid
2387 *
2388 * Obtain the process group of the tty. If there is no process group
2389 * return an error.
2390 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002391 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002392 */
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2395{
Alan Cox5d0fdf12008-04-30 00:53:31 -07002396 struct pid *pid;
2397 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 /*
2399 * (tty == real_tty) is a cheap way of
2400 * testing if the tty is NOT a master pty.
2401 */
2402 if (tty == real_tty && current->signal->tty != real_tty)
2403 return -ENOTTY;
Alan Cox5d0fdf12008-04-30 00:53:31 -07002404 pid = tty_get_pgrp(real_tty);
2405 ret = put_user(pid_vnr(pid), p);
2406 put_pid(pid);
2407 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Alan Coxaf9b8972006-08-27 01:24:01 -07002410/**
2411 * tiocspgrp - attempt to set process group
2412 * @tty: tty passed by user
2413 * @real_tty: tty side device matching tty passed by user
2414 * @p: pid pointer
2415 *
2416 * Set the process group of the tty to the session passed. Only
2417 * permitted where the tty session is our session.
2418 *
Alan Cox47f86832008-04-30 00:53:30 -07002419 * Locking: RCU, ctrl lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002420 */
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2423{
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002424 struct pid *pgrp;
2425 pid_t pgrp_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 int retval = tty_check_change(real_tty);
Alan Cox47f86832008-04-30 00:53:30 -07002427 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429 if (retval == -EIO)
2430 return -ENOTTY;
2431 if (retval)
2432 return retval;
2433 if (!current->signal->tty ||
2434 (current->signal->tty != real_tty) ||
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002435 (real_tty->session != task_session(current)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return -ENOTTY;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002437 if (get_user(pgrp_nr, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return -EFAULT;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002439 if (pgrp_nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return -EINVAL;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002441 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002442 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002443 retval = -ESRCH;
2444 if (!pgrp)
2445 goto out_unlock;
2446 retval = -EPERM;
2447 if (session_of_pgrp(pgrp) != task_session(current))
2448 goto out_unlock;
2449 retval = 0;
Alan Cox47f86832008-04-30 00:53:30 -07002450 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002451 put_pid(real_tty->pgrp);
2452 real_tty->pgrp = get_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002453 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002454out_unlock:
2455 rcu_read_unlock();
2456 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
Alan Coxaf9b8972006-08-27 01:24:01 -07002459/**
2460 * tiocgsid - get session id
2461 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002462 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002463 * @p: pointer to returned session id
2464 *
2465 * Obtain the session id of the tty. If there is no session
2466 * return an error.
2467 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002468 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002469 */
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2472{
2473 /*
2474 * (tty == real_tty) is a cheap way of
2475 * testing if the tty is NOT a master pty.
2476 */
2477 if (tty == real_tty && current->signal->tty != real_tty)
2478 return -ENOTTY;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002479 if (!real_tty->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return -ENOTTY;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002481 return put_user(pid_vnr(real_tty->session), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Alan Coxaf9b8972006-08-27 01:24:01 -07002484/**
2485 * tiocsetd - set line discipline
2486 * @tty: tty device
2487 * @p: pointer to user data
2488 *
2489 * Set the line discipline according to user request.
2490 *
2491 * Locking: see tty_set_ldisc, this function is just a helper
2492 */
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494static int tiocsetd(struct tty_struct *tty, int __user *p)
2495{
2496 int ldisc;
Alan Cox04f378b2008-04-30 00:53:29 -07002497 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 if (get_user(ldisc, p))
2500 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002501
Alan Cox04f378b2008-04-30 00:53:29 -07002502 ret = tty_set_ldisc(tty, ldisc);
Alan Cox04f378b2008-04-30 00:53:29 -07002503
2504 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Alan Coxaf9b8972006-08-27 01:24:01 -07002507/**
2508 * send_break - performed time break
2509 * @tty: device to break on
2510 * @duration: timeout in mS
2511 *
2512 * Perform a timed break on hardware that lacks its own driver level
2513 * timed break functionality.
2514 *
2515 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002516 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002517 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002518 */
2519
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002520static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Alan Cox9e989662008-07-22 11:18:03 +01002522 int retval;
2523
2524 if (tty->ops->break_ctl == NULL)
2525 return 0;
2526
2527 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2528 retval = tty->ops->break_ctl(tty, duration);
2529 else {
2530 /* Do the work ourselves */
2531 if (tty_write_lock(tty, 0) < 0)
2532 return -EINTR;
2533 retval = tty->ops->break_ctl(tty, -1);
2534 if (retval)
2535 goto out;
2536 if (!signal_pending(current))
2537 msleep_interruptible(duration);
2538 retval = tty->ops->break_ctl(tty, 0);
2539out:
2540 tty_write_unlock(tty);
2541 if (signal_pending(current))
2542 retval = -EINTR;
2543 }
2544 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Alan Coxaf9b8972006-08-27 01:24:01 -07002547/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002548 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002549 * @tty: tty device
2550 * @file: user file pointer
2551 * @p: pointer to result
2552 *
2553 * Obtain the modem status bits from the tty driver if the feature
2554 * is supported. Return -EINVAL if it is not available.
2555 *
2556 * Locking: none (up to the driver)
2557 */
2558
Alan Cox60b33c12011-02-14 16:26:14 +00002559static int tty_tiocmget(struct tty_struct *tty, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
2561 int retval = -EINVAL;
2562
Alan Coxf34d7a52008-04-30 00:54:13 -07002563 if (tty->ops->tiocmget) {
Alan Cox60b33c12011-02-14 16:26:14 +00002564 retval = tty->ops->tiocmget(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 if (retval >= 0)
2567 retval = put_user(retval, p);
2568 }
2569 return retval;
2570}
2571
Alan Coxaf9b8972006-08-27 01:24:01 -07002572/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002573 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002574 * @tty: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002575 * @cmd: command - clear bits, set bits or set all
2576 * @p: pointer to desired bits
2577 *
2578 * Set the modem status bits from the tty driver if the feature
2579 * is supported. Return -EINVAL if it is not available.
2580 *
2581 * Locking: none (up to the driver)
2582 */
2583
Alan Cox20b9d172011-02-14 16:26:50 +00002584static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 unsigned __user *p)
2586{
Alan Coxae677512008-07-16 21:56:54 +01002587 int retval;
2588 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Alan Coxae677512008-07-16 21:56:54 +01002590 if (tty->ops->tiocmset == NULL)
2591 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Alan Coxae677512008-07-16 21:56:54 +01002593 retval = get_user(val, p);
2594 if (retval)
2595 return retval;
2596 set = clear = 0;
2597 switch (cmd) {
2598 case TIOCMBIS:
2599 set = val;
2600 break;
2601 case TIOCMBIC:
2602 clear = val;
2603 break;
2604 case TIOCMSET:
2605 set = val;
2606 clear = ~val;
2607 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
Alan Coxae677512008-07-16 21:56:54 +01002609 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2610 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
Alan Cox20b9d172011-02-14 16:26:50 +00002611 return tty->ops->tiocmset(tty, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Alan Coxd281da72010-09-16 18:21:24 +01002614static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2615{
2616 int retval = -EINVAL;
2617 struct serial_icounter_struct icount;
2618 memset(&icount, 0, sizeof(icount));
2619 if (tty->ops->get_icount)
2620 retval = tty->ops->get_icount(tty, &icount);
2621 if (retval != 0)
2622 return retval;
2623 if (copy_to_user(arg, &icount, sizeof(icount)))
2624 return -EFAULT;
2625 return 0;
2626}
2627
Alan Coxe8b70e72009-06-11 12:48:02 +01002628struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2629{
2630 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2631 tty->driver->subtype == PTY_TYPE_MASTER)
2632 tty = tty->link;
2633 return tty;
2634}
2635EXPORT_SYMBOL(tty_pair_get_tty);
2636
2637struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
2638{
2639 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2640 tty->driver->subtype == PTY_TYPE_MASTER)
2641 return tty;
2642 return tty->link;
2643}
2644EXPORT_SYMBOL(tty_pair_get_pty);
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646/*
2647 * Split this up, as gcc can choke on it otherwise..
2648 */
Alan Cox04f378b2008-04-30 00:53:29 -07002649long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
Nick Piggind996b622010-08-18 04:37:36 +10002651 struct tty_struct *tty = file_tty(file);
2652 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 void __user *p = (void __user *)arg;
2654 int retval;
2655 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002656
Al Viro6131ffa2013-02-27 16:59:05 -05002657 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return -EINVAL;
2659
Alan Coxe8b70e72009-06-11 12:48:02 +01002660 real_tty = tty_pair_get_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
2662 /*
2663 * Factor out some common prep work
2664 */
2665 switch (cmd) {
2666 case TIOCSETD:
2667 case TIOCSBRK:
2668 case TIOCCBRK:
2669 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002670 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 retval = tty_check_change(tty);
2672 if (retval)
2673 return retval;
2674 if (cmd != TIOCCBRK) {
2675 tty_wait_until_sent(tty, 0);
2676 if (signal_pending(current))
2677 return -EINTR;
2678 }
2679 break;
2680 }
2681
Alan Cox9e989662008-07-22 11:18:03 +01002682 /*
2683 * Now do the stuff.
2684 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002686 case TIOCSTI:
2687 return tiocsti(tty, p);
2688 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002689 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002690 case TIOCSWINSZ:
Alan Coxfc6f6232009-01-02 13:43:17 +00002691 return tiocswinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002692 case TIOCCONS:
2693 return real_tty != tty ? -EINVAL : tioccons(file);
2694 case FIONBIO:
2695 return fionbio(file, p);
2696 case TIOCEXCL:
2697 set_bit(TTY_EXCLUSIVE, &tty->flags);
2698 return 0;
2699 case TIOCNXCL:
2700 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2701 return 0;
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +04002702 case TIOCGEXCL:
2703 {
2704 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2705 return put_user(excl, (int __user *)p);
2706 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002707 case TIOCNOTTY:
2708 if (current->signal->tty != tty)
2709 return -ENOTTY;
2710 no_tty();
2711 return 0;
2712 case TIOCSCTTY:
2713 return tiocsctty(tty, arg);
2714 case TIOCGPGRP:
2715 return tiocgpgrp(tty, real_tty, p);
2716 case TIOCSPGRP:
2717 return tiocspgrp(tty, real_tty, p);
2718 case TIOCGSID:
2719 return tiocgsid(tty, real_tty, p);
2720 case TIOCGETD:
Alan Coxc65c9bc2009-06-11 12:50:12 +01002721 return put_user(tty->ldisc->ops->num, (int __user *)p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002722 case TIOCSETD:
2723 return tiocsetd(tty, p);
Kay Sievers3c95c982011-02-17 18:39:28 +01002724 case TIOCVHANGUP:
2725 if (!capable(CAP_SYS_ADMIN))
2726 return -EPERM;
2727 tty_vhangup(tty);
2728 return 0;
Werner Finkb7b8de02010-12-03 12:48:23 +01002729 case TIOCGDEV:
2730 {
2731 unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2732 return put_user(ret, (unsigned int __user *)p);
2733 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002734 /*
2735 * Break handling
2736 */
2737 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002738 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002739 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002740 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002741 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002742 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002743 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002744 return 0;
2745 case TCSBRK: /* SVID version: non-zero arg --> no break */
2746 /* non-zero arg means wait for all output data
2747 * to be sent (performed above) but don't send break.
2748 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002750 if (!arg)
2751 return send_break(tty, 250);
2752 return 0;
2753 case TCSBRKP: /* support for POSIX tcsendbreak() */
2754 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Alan Cox37bdfb02008-02-08 04:18:47 -08002756 case TIOCMGET:
Alan Cox60b33c12011-02-14 16:26:14 +00002757 return tty_tiocmget(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002758 case TIOCMSET:
2759 case TIOCMBIC:
2760 case TIOCMBIS:
Alan Cox20b9d172011-02-14 16:26:50 +00002761 return tty_tiocmset(tty, cmd, p);
Alan Coxd281da72010-09-16 18:21:24 +01002762 case TIOCGICOUNT:
2763 retval = tty_tiocgicount(tty, p);
2764 /* For the moment allow fall through to the old method */
2765 if (retval != -EINVAL)
2766 return retval;
2767 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002768 case TCFLSH:
2769 switch (arg) {
2770 case TCIFLUSH:
2771 case TCIOFLUSH:
2772 /* flush tty buffer and allow ldisc to process ioctl */
2773 tty_buffer_flush(tty);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002774 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002775 }
2776 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002778 if (tty->ops->ioctl) {
Alan Cox6caa76b2011-02-14 16:27:22 +00002779 retval = (tty->ops->ioctl)(tty, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (retval != -ENOIOCTLCMD)
2781 return retval;
2782 }
2783 ld = tty_ldisc_ref_wait(tty);
2784 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002785 if (ld->ops->ioctl) {
2786 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (retval == -ENOIOCTLCMD)
Wanlong Gaobbb63c52012-08-27 15:23:12 +08002788 retval = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
2790 tty_ldisc_deref(ld);
2791 return retval;
2792}
2793
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002794#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08002795static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002796 unsigned long arg)
2797{
Nick Piggind996b622010-08-18 04:37:36 +10002798 struct tty_struct *tty = file_tty(file);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002799 struct tty_ldisc *ld;
2800 int retval = -ENOIOCTLCMD;
2801
Al Viro6131ffa2013-02-27 16:59:05 -05002802 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002803 return -EINVAL;
2804
Alan Coxf34d7a52008-04-30 00:54:13 -07002805 if (tty->ops->compat_ioctl) {
Alan Cox6caa76b2011-02-14 16:27:22 +00002806 retval = (tty->ops->compat_ioctl)(tty, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002807 if (retval != -ENOIOCTLCMD)
2808 return retval;
2809 }
2810
2811 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002812 if (ld->ops->compat_ioctl)
2813 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Thomas Meyer8193c422011-10-05 23:13:13 +02002814 else
2815 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002816 tty_ldisc_deref(ld);
2817
2818 return retval;
2819}
2820#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Al Viroc3c073f2012-08-21 22:32:06 -04002822static int this_tty(const void *t, struct file *file, unsigned fd)
2823{
2824 if (likely(file->f_op->read != tty_read))
2825 return 0;
2826 return file_tty(file) != t ? 0 : fd + 1;
2827}
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829/*
2830 * This implements the "Secure Attention Key" --- the idea is to
2831 * prevent trojan horses by killing all processes associated with this
2832 * tty when the user hits the "Secure Attention Key". Required for
2833 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08002834 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 * This code could be nicer; ideally it should send a HUP, wait a few
2836 * seconds, then send a INT, and then a KILL signal. But you then
2837 * have to coordinate with the init process, since all processes associated
2838 * with the current tty must be dead before the new getty is allowed
2839 * to spawn.
2840 *
2841 * Now, if it would be correct ;-/ The current code has a nasty hole -
2842 * it doesn't catch files in flight. We may send the descriptor to ourselves
2843 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2844 *
2845 * Nasty bug: do_SAK is being called in interrupt context. This can
2846 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2847 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002848void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
2850#ifdef TTY_SOFT_SAK
2851 tty_hangup(tty);
2852#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08002853 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002854 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -08002856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (!tty)
2858 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002859 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08002860
Dan Carpenterb3f13de2006-12-13 00:35:09 -08002861 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Alan Coxf34d7a52008-04-30 00:54:13 -07002863 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08002864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002866 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002867 do_each_pid_task(session, PIDTYPE_SID, p) {
Eric W. Biederman652486f2006-03-28 16:11:02 -08002868 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07002869 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002870 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002871 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002872 } while_each_pid_task(session, PIDTYPE_SID, p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002873 /* Now kill any processes that happen to have the
2874 * tty open.
2875 */
2876 do_each_thread(g, p) {
2877 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07002879 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002880 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 send_sig(SIGKILL, p, 1);
2882 continue;
2883 }
2884 task_lock(p);
Al Viroc3c073f2012-08-21 22:32:06 -04002885 i = iterate_fd(p->files, 0, this_tty, tty);
2886 if (i != 0) {
2887 printk(KERN_NOTICE "SAK: killed process %d"
2888 " (%s): fd#%d opened to the tty\n",
2889 task_pid_nr(p), p->comm, i - 1);
2890 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
2892 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002893 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 read_unlock(&tasklist_lock);
2895#endif
2896}
2897
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002898static void do_SAK_work(struct work_struct *work)
2899{
2900 struct tty_struct *tty =
2901 container_of(work, struct tty_struct, SAK_work);
2902 __do_SAK(tty);
2903}
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905/*
2906 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2907 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2908 * the values which we write to it will be identical to the values which it
2909 * already has. --akpm
2910 */
2911void do_SAK(struct tty_struct *tty)
2912{
2913 if (!tty)
2914 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 schedule_work(&tty->SAK_work);
2916}
2917
2918EXPORT_SYMBOL(do_SAK);
2919
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08002920static int dev_match_devt(struct device *dev, const void *data)
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002921{
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08002922 const dev_t *devt = data;
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002923 return dev->devt == *devt;
2924}
2925
2926/* Must put_device() after it's unused! */
2927static struct device *tty_get_device(struct tty_struct *tty)
2928{
2929 dev_t devt = tty_devnum(tty);
2930 return class_find_device(tty_class, NULL, &devt, dev_match_devt);
2931}
2932
2933
Alan Coxaf9b8972006-08-27 01:24:01 -07002934/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002935 * initialize_tty_struct
2936 * @tty: tty to initialize
2937 *
2938 * This subroutine initializes a tty structure that has been newly
2939 * allocated.
2940 *
2941 * Locking: none - tty in question must not be exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002943
Alan Coxbf970ee2008-10-13 10:42:39 +01002944void initialize_tty_struct(struct tty_struct *tty,
2945 struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
2947 memset(tty, 0, sizeof(struct tty_struct));
Alan Cox9c9f4de2008-10-13 10:37:26 +01002948 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 tty->magic = TTY_MAGIC;
Alan Cox01e1abb2008-07-22 11:16:55 +01002950 tty_ldisc_init(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002951 tty->session = NULL;
2952 tty->pgrp = NULL;
Alan Cox89c8d912012-08-08 16:30:13 +01002953 mutex_init(&tty->legacy_mutex);
Arjan van de Ven5785c952006-09-29 02:00:43 -07002954 mutex_init(&tty->termios_mutex);
Alan Coxc65c9bc2009-06-11 12:50:12 +01002955 mutex_init(&tty->ldisc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 init_waitqueue_head(&tty->write_wait);
2957 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00002958 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08002959 mutex_init(&tty->atomic_write_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07002960 spin_lock_init(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002962 INIT_WORK(&tty->SAK_work, do_SAK_work);
Alan Coxbf970ee2008-10-13 10:42:39 +01002963
2964 tty->driver = driver;
2965 tty->ops = driver->ops;
2966 tty->index = idx;
2967 tty_line_name(driver, idx, tty->name);
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002968 tty->dev = tty_get_device(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
Alan Coxf34d7a52008-04-30 00:54:13 -07002971/**
Jiri Slaby67166712011-03-23 10:48:35 +01002972 * deinitialize_tty_struct
2973 * @tty: tty to deinitialize
2974 *
2975 * This subroutine deinitializes a tty structure that has been newly
2976 * allocated but tty_release cannot be called on that yet.
2977 *
2978 * Locking: none - tty in question must not be exposed at this point
2979 */
2980void deinitialize_tty_struct(struct tty_struct *tty)
2981{
2982 tty_ldisc_deinit(tty);
2983}
2984
2985/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002986 * tty_put_char - write one character to a tty
2987 * @tty: tty
2988 * @ch: character
2989 *
2990 * Write one byte to the tty using the provided put_char method
2991 * if present. Returns the number of characters successfully output.
2992 *
2993 * Note: the specific put_char operation in the driver layer may go
2994 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002996
Alan Coxf34d7a52008-04-30 00:54:13 -07002997int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
Alan Coxf34d7a52008-04-30 00:54:13 -07002999 if (tty->ops->put_char)
3000 return tty->ops->put_char(tty, ch);
3001 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
Alan Coxf34d7a52008-04-30 00:54:13 -07003003EXPORT_SYMBOL_GPL(tty_put_char);
3004
Alan Coxd81ed102008-10-13 10:41:42 +01003005struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003007static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
3008 unsigned int index, unsigned int count)
3009{
3010 /* init here, since reused cdevs cause crashes */
3011 cdev_init(&driver->cdevs[index], &tty_fops);
3012 driver->cdevs[index].owner = driver->owner;
3013 return cdev_add(&driver->cdevs[index], dev, count);
3014}
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003017 * tty_register_device - register a tty device
3018 * @driver: the tty driver that describes the tty device
3019 * @index: the index in the tty driver for this tty device
3020 * @device: a struct device that is associated with this tty device.
3021 * This field is optional, if there is no known struct device
3022 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003024 * Returns a pointer to the struct device for this tty device
3025 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003026 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003027 * This call is required to be made to register an individual tty device
3028 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3029 * that bit is not set, this function should not be called by a tty
3030 * driver.
3031 *
3032 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003034
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003035struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3036 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003038 return tty_register_device_attr(driver, index, device, NULL, NULL);
3039}
3040EXPORT_SYMBOL(tty_register_device);
3041
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003042static void tty_device_create_release(struct device *dev)
3043{
3044 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3045 kfree(dev);
3046}
3047
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003048/**
3049 * tty_register_device_attr - register a tty device
3050 * @driver: the tty driver that describes the tty device
3051 * @index: the index in the tty driver for this tty device
3052 * @device: a struct device that is associated with this tty device.
3053 * This field is optional, if there is no known struct device
3054 * for this tty device it can be set to NULL safely.
3055 * @drvdata: Driver data to be set to device.
3056 * @attr_grp: Attribute group to be set on device.
3057 *
3058 * Returns a pointer to the struct device for this tty device
3059 * (or ERR_PTR(-EFOO) on error).
3060 *
3061 * This call is required to be made to register an individual tty device
3062 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3063 * that bit is not set, this function should not be called by a tty
3064 * driver.
3065 *
3066 * Locking: ??
3067 */
3068struct device *tty_register_device_attr(struct tty_driver *driver,
3069 unsigned index, struct device *device,
3070 void *drvdata,
3071 const struct attribute_group **attr_grp)
3072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 char name[64];
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003074 dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
3075 struct device *dev = NULL;
3076 int retval = -ENODEV;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003077 bool cdev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 if (index >= driver->num) {
3080 printk(KERN_ERR "Attempt to register invalid tty line number "
3081 " (%d).\n", index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003082 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
3084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (driver->type == TTY_DRIVER_TYPE_PTY)
3086 pty_line_name(driver, index, name);
3087 else
3088 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003089
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003090 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003091 retval = tty_cdev_add(driver, devt, index, 1);
3092 if (retval)
3093 goto error;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003094 cdev = true;
3095 }
3096
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003097 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3098 if (!dev) {
3099 retval = -ENOMEM;
3100 goto error;
3101 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003102
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003103 dev->devt = devt;
3104 dev->class = tty_class;
3105 dev->parent = device;
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003106 dev->release = tty_device_create_release;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003107 dev_set_name(dev, "%s", name);
3108 dev->groups = attr_grp;
3109 dev_set_drvdata(dev, drvdata);
3110
3111 retval = device_register(dev);
3112 if (retval)
3113 goto error;
3114
3115 return dev;
3116
3117error:
3118 put_device(dev);
3119 if (cdev)
3120 cdev_del(&driver->cdevs[index]);
3121 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122}
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003123EXPORT_SYMBOL_GPL(tty_register_device_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003126 * tty_unregister_device - unregister a tty device
3127 * @driver: the tty driver that describes the tty device
3128 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003130 * If a tty device is registered with a call to tty_register_device() then
3131 * this function must be called when the tty device is gone.
3132 *
3133 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136void tty_unregister_device(struct tty_driver *driver, unsigned index)
3137{
Alan Cox37bdfb02008-02-08 04:18:47 -08003138 device_destroy(tty_class,
3139 MKDEV(driver->major, driver->minor_start) + index);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003140 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC))
3141 cdev_del(&driver->cdevs[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143EXPORT_SYMBOL(tty_unregister_device);
3144
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003145/**
3146 * __tty_alloc_driver -- allocate tty driver
3147 * @lines: count of lines this driver can handle at most
3148 * @owner: module which is repsonsible for this driver
3149 * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
3150 *
3151 * This should not be called directly, some of the provided macros should be
3152 * used instead. Use IS_ERR and friends on @retval.
3153 */
3154struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
3155 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
3157 struct tty_driver *driver;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003158 unsigned int cdevs = 1;
Jiri Slaby16a02082012-08-08 22:26:42 +02003159 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Jiri Slaby0019b402012-08-08 22:26:43 +02003161 if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003162 return ERR_PTR(-EINVAL);
3163
Jean Delvare506eb992007-07-15 23:40:14 -07003164 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003165 if (!driver)
3166 return ERR_PTR(-ENOMEM);
3167
3168 kref_init(&driver->kref);
3169 driver->magic = TTY_DRIVER_MAGIC;
3170 driver->num = lines;
3171 driver->owner = owner;
3172 driver->flags = flags;
Jiri Slaby16a02082012-08-08 22:26:42 +02003173
3174 if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
3175 driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
3176 GFP_KERNEL);
3177 driver->termios = kcalloc(lines, sizeof(*driver->termios),
3178 GFP_KERNEL);
3179 if (!driver->ttys || !driver->termios) {
3180 err = -ENOMEM;
3181 goto err_free_all;
3182 }
3183 }
3184
3185 if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3186 driver->ports = kcalloc(lines, sizeof(*driver->ports),
3187 GFP_KERNEL);
3188 if (!driver->ports) {
3189 err = -ENOMEM;
3190 goto err_free_all;
3191 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003192 cdevs = lines;
3193 }
3194
3195 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
3196 if (!driver->cdevs) {
3197 err = -ENOMEM;
3198 goto err_free_all;
Jiri Slaby16a02082012-08-08 22:26:42 +02003199 }
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 return driver;
Jiri Slaby16a02082012-08-08 22:26:42 +02003202err_free_all:
3203 kfree(driver->ports);
3204 kfree(driver->ttys);
3205 kfree(driver->termios);
3206 kfree(driver);
3207 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208}
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003209EXPORT_SYMBOL(__tty_alloc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Alan Cox7d7b93c2008-10-13 10:42:09 +01003211static void destruct_tty_driver(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003213 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
3214 int i;
3215 struct ktermios *tp;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003216
3217 if (driver->flags & TTY_DRIVER_INSTALLED) {
3218 /*
3219 * Free the termios and termios_locked structures because
3220 * we don't want to get memory leaks when modular tty
3221 * drivers are removed from the kernel.
3222 */
3223 for (i = 0; i < driver->num; i++) {
3224 tp = driver->termios[i];
3225 if (tp) {
3226 driver->termios[i] = NULL;
3227 kfree(tp);
3228 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003229 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3230 tty_unregister_device(driver, i);
3231 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003232 proc_tty_unregister_driver(driver);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003233 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
3234 cdev_del(&driver->cdevs[0]);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003235 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003236 kfree(driver->cdevs);
Jiri Slaby04831dc2012-06-04 13:35:36 +02003237 kfree(driver->ports);
Jiri Slaby16a02082012-08-08 22:26:42 +02003238 kfree(driver->termios);
3239 kfree(driver->ttys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 kfree(driver);
3241}
3242
Alan Cox7d7b93c2008-10-13 10:42:09 +01003243void tty_driver_kref_put(struct tty_driver *driver)
3244{
3245 kref_put(&driver->kref, destruct_tty_driver);
3246}
3247EXPORT_SYMBOL(tty_driver_kref_put);
3248
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003249void tty_set_operations(struct tty_driver *driver,
3250 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Alan Coxf34d7a52008-04-30 00:54:13 -07003252 driver->ops = op;
3253};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254EXPORT_SYMBOL(tty_set_operations);
3255
Alan Cox7d7b93c2008-10-13 10:42:09 +01003256void put_tty_driver(struct tty_driver *d)
3257{
3258 tty_driver_kref_put(d);
3259}
3260EXPORT_SYMBOL(put_tty_driver);
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262/*
3263 * Called by a tty driver to register itself.
3264 */
3265int tty_register_driver(struct tty_driver *driver)
3266{
3267 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08003268 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 dev_t dev;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003270 struct device *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003273 error = alloc_chrdev_region(&dev, driver->minor_start,
3274 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 if (!error) {
3276 driver->major = MAJOR(dev);
3277 driver->minor_start = MINOR(dev);
3278 }
3279 } else {
3280 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01003281 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003283 if (error < 0)
Jiri Slaby16a02082012-08-08 22:26:42 +02003284 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003286 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
3287 error = tty_cdev_add(driver, dev, 0, driver->num);
3288 if (error)
3289 goto err_unreg_char;
3290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003292 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003294 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003295
3296 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003297 for (i = 0; i < driver->num; i++) {
3298 d = tty_register_device(driver, i, NULL);
3299 if (IS_ERR(d)) {
3300 error = PTR_ERR(d);
Jiri Slaby16a02082012-08-08 22:26:42 +02003301 goto err_unreg_devs;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003302 }
3303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 }
3305 proc_tty_register_driver(driver);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003306 driver->flags |= TTY_DRIVER_INSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 return 0;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003308
Jiri Slaby16a02082012-08-08 22:26:42 +02003309err_unreg_devs:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003310 for (i--; i >= 0; i--)
3311 tty_unregister_device(driver, i);
3312
3313 mutex_lock(&tty_mutex);
3314 list_del(&driver->tty_drivers);
3315 mutex_unlock(&tty_mutex);
3316
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003317err_unreg_char:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003318 unregister_chrdev_region(dev, driver->num);
Jiri Slaby16a02082012-08-08 22:26:42 +02003319err:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003320 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322EXPORT_SYMBOL(tty_register_driver);
3323
3324/*
3325 * Called by a tty driver to unregister itself.
3326 */
3327int tty_unregister_driver(struct tty_driver *driver)
3328{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003329#if 0
3330 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 if (driver->refcount)
3332 return -EBUSY;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003333#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3335 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003336 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003338 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 return 0;
3340}
Alan Cox7d7b93c2008-10-13 10:42:09 +01003341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342EXPORT_SYMBOL(tty_unregister_driver);
3343
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003344dev_t tty_devnum(struct tty_struct *tty)
3345{
3346 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3347}
3348EXPORT_SYMBOL(tty_devnum);
3349
3350void proc_clear_tty(struct task_struct *p)
3351{
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003352 unsigned long flags;
Alan Cox9c9f4de2008-10-13 10:37:26 +01003353 struct tty_struct *tty;
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003354 spin_lock_irqsave(&p->sighand->siglock, flags);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003355 tty = p->signal->tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003356 p->signal->tty = NULL;
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003357 spin_unlock_irqrestore(&p->sighand->siglock, flags);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003358 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003359}
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003360
Alan Cox47f86832008-04-30 00:53:30 -07003361/* Called under the sighand lock */
3362
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003363static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003364{
3365 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -07003366 unsigned long flags;
3367 /* We should not have a session or pgrp to put here but.... */
3368 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -06003369 put_pid(tty->session);
3370 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003371 tty->pgrp = get_pid(task_pgrp(tsk));
Alan Cox47f86832008-04-30 00:53:30 -07003372 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3373 tty->session = get_pid(task_session(tsk));
Alan Cox9c9f4de2008-10-13 10:37:26 +01003374 if (tsk->signal->tty) {
3375 printk(KERN_DEBUG "tty not NULL!!\n");
3376 tty_kref_put(tsk->signal->tty);
3377 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003378 }
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003379 put_pid(tsk->signal->tty_old_pgrp);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003380 tsk->signal->tty = tty_kref_get(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003381 tsk->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003382}
3383
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07003384static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003385{
3386 spin_lock_irq(&tsk->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003387 __proc_set_tty(tsk, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003388 spin_unlock_irq(&tsk->sighand->siglock);
3389}
3390
3391struct tty_struct *get_current_tty(void)
3392{
3393 struct tty_struct *tty;
Alan Cox934e6eb2008-10-13 10:40:43 +01003394 unsigned long flags;
3395
3396 spin_lock_irqsave(&current->sighand->siglock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +01003397 tty = tty_kref_get(current->signal->tty);
Alan Cox934e6eb2008-10-13 10:40:43 +01003398 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003399 return tty;
3400}
Heiko Carstensa311f742006-12-13 00:33:41 -08003401EXPORT_SYMBOL_GPL(get_current_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Alan Coxd81ed102008-10-13 10:41:42 +01003403void tty_default_fops(struct file_operations *fops)
3404{
3405 *fops = tty_fops;
3406}
3407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408/*
3409 * Initialize the console device. This is called *early*, so
3410 * we can't necessarily depend on lots of kernel help here.
3411 * Just do some early initializations, and do the complex setup
3412 * later.
3413 */
3414void __init console_init(void)
3415{
3416 initcall_t *call;
3417
3418 /* Setup the default TTY line discipline. */
Alan Cox01e1abb2008-07-22 11:16:55 +01003419 tty_ldisc_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
3421 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08003422 * set up the console device so that later boot sequences can
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 * inform about problems etc..
3424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 call = __con_initcall_start;
3426 while (call < __con_initcall_end) {
3427 (*call)();
3428 call++;
3429 }
3430}
3431
Al Viro2c9ede52011-07-23 20:24:48 -04003432static char *tty_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +02003433{
3434 if (!mode)
3435 return NULL;
3436 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3437 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3438 *mode = 0666;
3439 return NULL;
3440}
3441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442static int __init tty_class_init(void)
3443{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003444 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 if (IS_ERR(tty_class))
3446 return PTR_ERR(tty_class);
Kay Sieverse454cea2009-09-18 23:01:12 +02003447 tty_class->devnode = tty_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 return 0;
3449}
3450
3451postcore_initcall(tty_class_init);
3452
3453/* 3/2004 jmc: why do these devices exist? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454static struct cdev tty_cdev, console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Kay Sieversfbc92a32010-12-01 18:51:05 +01003456static ssize_t show_cons_active(struct device *dev,
3457 struct device_attribute *attr, char *buf)
3458{
3459 struct console *cs[16];
3460 int i = 0;
3461 struct console *c;
3462 ssize_t count = 0;
3463
Torben Hohnac751ef2011-01-25 15:07:35 -08003464 console_lock();
Kay Sieversa2a6a822011-01-09 16:39:14 +01003465 for_each_console(c) {
Kay Sieversfbc92a32010-12-01 18:51:05 +01003466 if (!c->device)
3467 continue;
3468 if (!c->write)
3469 continue;
3470 if ((c->flags & CON_ENABLED) == 0)
3471 continue;
3472 cs[i++] = c;
3473 if (i >= ARRAY_SIZE(cs))
3474 break;
3475 }
3476 while (i--)
3477 count += sprintf(buf + count, "%s%d%c",
3478 cs[i]->name, cs[i]->index, i ? ' ':'\n');
Torben Hohnac751ef2011-01-25 15:07:35 -08003479 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01003480
3481 return count;
3482}
3483static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3484
3485static struct device *consdev;
3486
3487void console_sysfs_notify(void)
3488{
3489 if (consdev)
3490 sysfs_notify(&consdev->kobj, NULL, "active");
3491}
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493/*
3494 * Ok, now we can initialize the rest of the tty devices and can count
3495 * on memory allocations, interrupts etc..
3496 */
David Howells31d1d482010-08-06 16:34:43 +01003497int __init tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
3499 cdev_init(&tty_cdev, &tty_fops);
3500 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3501 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3502 panic("Couldn't register /dev/tty driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003503 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 cdev_init(&console_cdev, &console_fops);
3506 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3507 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3508 panic("Couldn't register /dev/console driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003509 consdev = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003510 "console");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003511 if (IS_ERR(consdev))
3512 consdev = NULL;
3513 else
Kay Sieversa2a6a822011-01-09 16:39:14 +01003514 WARN_ON(device_create_file(consdev, &dev_attr_active) < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516#ifdef CONFIG_VT
Alan Coxd81ed102008-10-13 10:41:42 +01003517 vty_init(&console_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518#endif
3519 return 0;
3520}
David Howells31d1d482010-08-06 16:34:43 +01003521