blob: 2661e86a2272b26425a7db759c277c610508ce35 [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;
Peter Hurleyea648a42013-03-06 07:20:53 -0500548 int refs = 0;
549
550 read_lock(&tasklist_lock);
551 if (tty->session) {
552 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
553 spin_lock_irq(&p->sighand->siglock);
554 if (p->signal->tty == tty) {
555 p->signal->tty = NULL;
556 /* We defer the dereferences outside fo
557 the tasklist lock */
558 refs++;
559 }
560 if (!p->signal->leader) {
561 spin_unlock_irq(&p->sighand->siglock);
562 continue;
563 }
564 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
565 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
566 put_pid(p->signal->tty_old_pgrp); /* A noop */
Peter Hurleybc30c3b2013-03-06 07:20:55 -0500567 spin_lock(&tty->ctrl_lock);
Peter Hurleyea648a42013-03-06 07:20:53 -0500568 if (tty->pgrp)
569 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
Peter Hurleybc30c3b2013-03-06 07:20:55 -0500570 spin_unlock(&tty->ctrl_lock);
Peter Hurleyea648a42013-03-06 07:20:53 -0500571 spin_unlock_irq(&p->sighand->siglock);
572 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
573 }
574 read_unlock(&tasklist_lock);
575
576 return refs;
577}
578
579/**
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200580 * __tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +0000581 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -0700582 *
Kevin Cernekeeef4f5272012-12-26 20:43:41 -0800583 * This can be called by a "kworker" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -0700584 * synchronous but doesn't hold any locks, so we need to make sure we
585 * have the appropriate locks for what we're doing.
586 *
587 * The hangup event clears any pending redirections onto the hung up
588 * device. It ensures future writes will error and it does the needed
589 * line discipline hangup and signal delivery. The tty object itself
590 * remains intact.
591 *
592 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200593 * BTM
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800594 * redirect lock for undoing redirection
595 * file list lock for manipulating list of ttys
596 * tty_ldisc_lock from called functions
597 * termios_mutex resetting termios data
598 * tasklist_lock to walk task list for hangup event
599 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Josh Triplett2520e272012-11-18 21:27:47 -0800601static void __tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Alan Cox37bdfb02008-02-08 04:18:47 -0800603 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct file *filp, *f = NULL;
Nick Piggind996b622010-08-18 04:37:36 +1000605 struct tty_file_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 int closecount = 0, n;
Peter Hurleyea648a42013-03-06 07:20:53 -0500607 int refs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if (!tty)
610 return;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 spin_lock(&redirect_lock);
Nick Piggind996b622010-08-18 04:37:36 +1000614 if (redirect && file_tty(redirect) == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 f = redirect;
616 redirect = NULL;
617 }
618 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800619
Alan Cox89c8d912012-08-08 16:30:13 +0100620 tty_lock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200621
Jiri Slabyacfa7472010-11-29 10:16:54 +0100622 /* some functions below drop BTM, so we need this bit */
623 set_bit(TTY_HUPPING, &tty->flags);
624
Arnd Bergmannec79d602010-06-01 22:53:01 +0200625 /* inuse_filps is protected by the single tty lock,
626 this really needs to change if we want to flush the
627 workqueue with the lock held */
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200628 check_tty_count(tty, "tty_hangup");
Alan Cox36ba7822009-11-30 13:18:51 +0000629
Nick Pigginee2ffa02010-08-18 04:37:35 +1000630 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Nick Piggind996b622010-08-18 04:37:36 +1000632 list_for_each_entry(priv, &tty->tty_files, list) {
633 filp = priv->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (filp->f_op->write == redirected_tty_write)
635 cons_filp = filp;
636 if (filp->f_op->write != tty_write)
637 continue;
638 closecount++;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200639 __tty_fasync(-1, filp, 0); /* can't block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 filp->f_op = &hung_up_tty_fops;
641 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000642 spin_unlock(&tty_files_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800643
Jiri Slabyacfa7472010-11-29 10:16:54 +0100644 /*
645 * it drops BTM and thus races with reopen
646 * we protect the race by TTY_HUPPING
647 */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100648 tty_ldisc_hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800649
Peter Hurleyea648a42013-03-06 07:20:53 -0500650 refs = tty_signal_session_leader(tty);
651 /* Account for the p->signal references we killed */
652 while (refs--)
653 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Peter Hurley20cc2252013-03-06 07:20:54 -0500655 spin_lock_irq(&tty->ctrl_lock);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100656 clear_bit(TTY_THROTTLED, &tty->flags);
657 clear_bit(TTY_PUSH, &tty->flags);
658 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -0600659 put_pid(tty->session);
660 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800661 tty->session = NULL;
662 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 tty->ctrl_status = 0;
Peter Hurley20cc2252013-03-06 07:20:54 -0500664 spin_unlock_irq(&tty->ctrl_lock);
Alan Cox47f86832008-04-30 00:53:30 -0700665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /*
Alan Cox37bdfb02008-02-08 04:18:47 -0800667 * If one of the devices matches a console pointer, we
668 * cannot just call hangup() because that will cause
669 * tty->count and state->count to go out of sync.
670 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
672 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -0700673 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -0700675 tty->ops->close(tty, cons_filp);
676 } else if (tty->ops->hangup)
677 (tty->ops->hangup)(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800678 /*
679 * We don't want to have driver/ldisc interactions beyond
680 * the ones we did here. The driver layer expects no
681 * calls after ->hangup() from the ldisc side. However we
682 * can't yet guarantee all that.
683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 set_bit(TTY_HUPPED, &tty->flags);
Jiri Slabyacfa7472010-11-29 10:16:54 +0100685 clear_bit(TTY_HUPPING, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100686 tty_ldisc_enable(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200687
Alan Cox89c8d912012-08-08 16:30:13 +0100688 tty_unlock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (f)
691 fput(f);
692}
693
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200694static void do_tty_hangup(struct work_struct *work)
695{
696 struct tty_struct *tty =
697 container_of(work, struct tty_struct, hangup_work);
698
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200699 __tty_hangup(tty);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200700}
701
Alan Coxaf9b8972006-08-27 01:24:01 -0700702/**
703 * tty_hangup - trigger a hangup event
704 * @tty: tty to hangup
705 *
706 * A carrier loss (virtual or otherwise) has occurred on this like
707 * schedule a hangup sequence to run after this event.
708 */
709
Alan Cox37bdfb02008-02-08 04:18:47 -0800710void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712#ifdef TTY_DEBUG_HANGUP
713 char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
715#endif
716 schedule_work(&tty->hangup_work);
717}
718
719EXPORT_SYMBOL(tty_hangup);
720
Alan Coxaf9b8972006-08-27 01:24:01 -0700721/**
722 * tty_vhangup - process vhangup
723 * @tty: tty to hangup
724 *
725 * The user has asked via system call for the terminal to be hung up.
726 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200727 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -0700728 */
729
Alan Cox37bdfb02008-02-08 04:18:47 -0800730void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732#ifdef TTY_DEBUG_HANGUP
733 char buf[64];
734
735 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
736#endif
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200737 __tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
Alan Cox37bdfb02008-02-08 04:18:47 -0800739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740EXPORT_SYMBOL(tty_vhangup);
741
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200742
Alan Coxaf9b8972006-08-27 01:24:01 -0700743/**
Alan Cox2cb59982008-10-13 10:40:30 +0100744 * tty_vhangup_self - process vhangup for own ctty
745 *
746 * Perform a vhangup on the current controlling tty
747 */
748
749void tty_vhangup_self(void)
750{
751 struct tty_struct *tty;
752
Alan Cox2cb59982008-10-13 10:40:30 +0100753 tty = get_current_tty();
754 if (tty) {
755 tty_vhangup(tty);
756 tty_kref_put(tty);
757 }
Alan Cox2cb59982008-10-13 10:40:30 +0100758}
759
760/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700761 * tty_hung_up_p - was tty hung up
762 * @filp: file pointer of tty
763 *
764 * Return true if the tty has been subject to a vhangup or a carrier
765 * loss
766 */
767
Alan Cox37bdfb02008-02-08 04:18:47 -0800768int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 return (filp->f_op == &hung_up_tty_fops);
771}
772
773EXPORT_SYMBOL(tty_hung_up_p);
774
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800775static void session_clear_tty(struct pid *session)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800776{
777 struct task_struct *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800778 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800779 proc_clear_tty(p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800780 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800781}
782
Alan Coxaf9b8972006-08-27 01:24:01 -0700783/**
784 * disassociate_ctty - disconnect controlling tty
785 * @on_exit: true if exiting so need to "hang up" the session
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700787 * This function is typically called only by the session leader, when
788 * it wants to disassociate itself from its controlling tty.
789 *
790 * It performs the following functions:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
792 * (2) Clears the tty from being controlling the session
793 * (3) Clears the controlling tty for all processes in the
794 * session group.
795 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700796 * The argument on_exit is set to 1 if called when a process is
797 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
798 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800799 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200800 * BTM is taken for hysterical raisins, and held when
801 * called from no_tty().
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800802 * tty_mutex is taken to protect tty
803 * ->siglock is taken to protect ->signal/->sighand
804 * tasklist_lock is taken to walk process list for sessions
805 * ->siglock is taken to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808void disassociate_ctty(int on_exit)
809{
810 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Alan Cox5ec93d12009-11-30 13:18:45 +0000812 if (!current->signal->leader)
813 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800815 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (tty) {
Jiri Slaby1411dc42011-11-09 21:33:18 +0100817 struct pid *tty_pgrp = get_pid(tty->pgrp);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200818 if (on_exit) {
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200819 if (tty->driver->type != TTY_DRIVER_TYPE_PTY)
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200820 tty_vhangup(tty);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200821 }
Alan Cox452a00d2008-10-13 10:39:13 +0100822 tty_kref_put(tty);
Jiri Slaby1411dc42011-11-09 21:33:18 +0100823 if (tty_pgrp) {
824 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
825 if (!on_exit)
826 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
827 put_pid(tty_pgrp);
828 }
Eric W. Biederman680a9672007-02-12 00:52:52 -0800829 } else if (on_exit) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800830 struct pid *old_pgrp;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800831 spin_lock_irq(&current->sighand->siglock);
832 old_pgrp = current->signal->tty_old_pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800833 current->signal->tty_old_pgrp = NULL;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800834 spin_unlock_irq(&current->sighand->siglock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800835 if (old_pgrp) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800836 kill_pgrp(old_pgrp, SIGHUP, on_exit);
837 kill_pgrp(old_pgrp, SIGCONT, on_exit);
838 put_pid(old_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return;
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800843 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700844 put_pid(current->signal->tty_old_pgrp);
Randy Dunlap23cac8d2007-02-20 13:58:05 -0800845 current->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800846 spin_unlock_irq(&current->sighand->siglock);
847
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800848 tty = get_current_tty();
849 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -0700850 unsigned long flags;
851 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800852 put_pid(tty->session);
853 put_pid(tty->pgrp);
854 tty->session = NULL;
855 tty->pgrp = NULL;
Alan Cox47f86832008-04-30 00:53:30 -0700856 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +0100857 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800858 } else {
859#ifdef TTY_DEBUG_HANGUP
860 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
861 " = NULL", tty);
862#endif
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* Now clear signal->tty under the lock */
866 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800867 session_clear_tty(task_session(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700871/**
872 *
873 * no_tty - Ensure the current process does not have a controlling tty
874 */
875void no_tty(void)
876{
Alan Cox3af502b2012-05-03 22:21:53 +0100877 /* FIXME: Review locking here. The tty_lock never covered any race
878 between a new association and proc_clear_tty but possible we need
879 to protect against this anyway */
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700880 struct task_struct *tsk = current;
Alan Cox5ec93d12009-11-30 13:18:45 +0000881 disassociate_ctty(0);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700882 proc_clear_tty(tsk);
883}
884
Alan Coxaf9b8972006-08-27 01:24:01 -0700885
886/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200887 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700888 * @tty: tty to stop
889 *
890 * Perform flow control to the driver. For PTY/TTY pairs we
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200891 * must also propagate the TIOCKPKT status. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -0700892 * on an already stopped device and will not re-call the driver
893 * method.
894 *
895 * This functionality is used by both the line disciplines for
896 * halting incoming flow and by the driver. It may therefore be
897 * called from any context, may be under the tty atomic_write_lock
898 * but not always.
899 *
900 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700901 * Uses the tty control lock internally
Alan Coxaf9b8972006-08-27 01:24:01 -0700902 */
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904void stop_tty(struct tty_struct *tty)
905{
Alan Cox04f378b2008-04-30 00:53:29 -0700906 unsigned long flags;
907 spin_lock_irqsave(&tty->ctrl_lock, flags);
908 if (tty->stopped) {
909 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 tty->stopped = 1;
913 if (tty->link && tty->link->packet) {
914 tty->ctrl_status &= ~TIOCPKT_START;
915 tty->ctrl_status |= TIOCPKT_STOP;
Davide Libenzi4b194492009-03-31 15:24:24 -0700916 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Alan Cox04f378b2008-04-30 00:53:29 -0700918 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700919 if (tty->ops->stop)
920 (tty->ops->stop)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923EXPORT_SYMBOL(stop_tty);
924
Alan Coxaf9b8972006-08-27 01:24:01 -0700925/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200926 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700927 * @tty: tty to start
928 *
929 * Start a tty that has been stopped if at all possible. Perform
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200930 * any necessary wakeups and propagate the TIOCPKT status. If this
Alan Coxaf9b8972006-08-27 01:24:01 -0700931 * is the tty was previous stopped and is being started then the
932 * driver start method is invoked and the line discipline woken.
933 *
934 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700935 * ctrl_lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700936 */
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938void start_tty(struct tty_struct *tty)
939{
Alan Cox04f378b2008-04-30 00:53:29 -0700940 unsigned long flags;
941 spin_lock_irqsave(&tty->ctrl_lock, flags);
942 if (!tty->stopped || tty->flow_stopped) {
943 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 tty->stopped = 0;
947 if (tty->link && tty->link->packet) {
948 tty->ctrl_status &= ~TIOCPKT_STOP;
949 tty->ctrl_status |= TIOCPKT_START;
Davide Libenzi4b194492009-03-31 15:24:24 -0700950 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Alan Cox04f378b2008-04-30 00:53:29 -0700952 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700953 if (tty->ops->start)
954 (tty->ops->start)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* If we have a running line discipline it may need kicking */
956 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959EXPORT_SYMBOL(start_tty);
960
Alan Coxaf9b8972006-08-27 01:24:01 -0700961/**
962 * tty_read - read method for tty device files
963 * @file: pointer to tty file
964 * @buf: user buffer
965 * @count: size of user buffer
966 * @ppos: unused
967 *
968 * Perform the read system call function on this terminal device. Checks
969 * for hung up devices before calling the line discipline method.
970 *
971 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -0700972 * Locks the line discipline internally while needed. Multiple
973 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -0700974 */
975
Alan Cox37bdfb02008-02-08 04:18:47 -0800976static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 loff_t *ppos)
978{
979 int i;
Nick Piggind996b622010-08-18 04:37:36 +1000980 struct tty_struct *tty = file_tty(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 struct tty_ldisc *ld;
982
Al Viro6131ffa2013-02-27 16:59:05 -0500983 if (tty_paranoia_check(tty, file_inode(file), "tty_read"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return -EIO;
985 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
986 return -EIO;
987
988 /* We want to wait for the line discipline to sort out in this
989 situation */
990 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +0100991 if (ld->ops->read)
992 i = (ld->ops->read)(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 else
994 i = -EIO;
995 tty_ldisc_deref(ld);
Jiri Slabyb0de59b2013-02-15 15:25:05 +0100996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return i;
998}
999
Alan Cox9c1729d2007-07-15 23:39:43 -07001000void tty_write_unlock(struct tty_struct *tty)
Jiri Slaby83c67572011-04-20 10:43:18 +02001001 __releases(&tty->atomic_write_lock)
Alan Cox9c1729d2007-07-15 23:39:43 -07001002{
1003 mutex_unlock(&tty->atomic_write_lock);
Davide Libenzi4b194492009-03-31 15:24:24 -07001004 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Alan Cox9c1729d2007-07-15 23:39:43 -07001005}
1006
1007int tty_write_lock(struct tty_struct *tty, int ndelay)
Jiri Slaby83c67572011-04-20 10:43:18 +02001008 __acquires(&tty->atomic_write_lock)
Alan Cox9c1729d2007-07-15 23:39:43 -07001009{
1010 if (!mutex_trylock(&tty->atomic_write_lock)) {
1011 if (ndelay)
1012 return -EAGAIN;
1013 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1014 return -ERESTARTSYS;
1015 }
1016 return 0;
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019/*
1020 * Split writes up in sane blocksizes to avoid
1021 * denial-of-service type attacks
1022 */
1023static inline ssize_t do_tty_write(
1024 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1025 struct tty_struct *tty,
1026 struct file *file,
1027 const char __user *buf,
1028 size_t count)
1029{
Alan Cox9c1729d2007-07-15 23:39:43 -07001030 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -08001032
Alan Cox9c1729d2007-07-15 23:39:43 -07001033 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1034 if (ret < 0)
1035 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /*
1038 * We chunk up writes into a temporary buffer. This
1039 * simplifies low-level drivers immensely, since they
1040 * don't have locking issues and user mode accesses.
1041 *
1042 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1043 * big chunk-size..
1044 *
1045 * The default chunk-size is 2kB, because the NTTY
1046 * layer has problems with bigger chunks. It will
1047 * claim to be able to handle more characters than
1048 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -07001049 *
1050 * FIXME: This can probably go away now except that 64K chunks
1051 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 */
1053 chunk = 2048;
1054 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1055 chunk = 65536;
1056 if (count < chunk)
1057 chunk = count;
1058
Ingo Molnar70522e12006-03-23 03:00:31 -08001059 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (tty->write_cnt < chunk) {
Jason Wessel402fda92008-10-13 10:45:36 +01001061 unsigned char *buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (chunk < 1024)
1064 chunk = 1024;
1065
Jason Wessel402fda92008-10-13 10:45:36 +01001066 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1067 if (!buf_chunk) {
Alan Cox9c1729d2007-07-15 23:39:43 -07001068 ret = -ENOMEM;
1069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 kfree(tty->write_buf);
1072 tty->write_cnt = chunk;
Jason Wessel402fda92008-10-13 10:45:36 +01001073 tty->write_buf = buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
1076 /* Do the write .. */
1077 for (;;) {
1078 size_t size = count;
1079 if (size > chunk)
1080 size = chunk;
1081 ret = -EFAULT;
1082 if (copy_from_user(tty->write_buf, buf, size))
1083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (ret <= 0)
1086 break;
1087 written += ret;
1088 buf += ret;
1089 count -= ret;
1090 if (!count)
1091 break;
1092 ret = -ERESTARTSYS;
1093 if (signal_pending(current))
1094 break;
1095 cond_resched();
1096 }
Jiri Slabyb0de59b2013-02-15 15:25:05 +01001097 if (written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 ret = written;
Alan Cox9c1729d2007-07-15 23:39:43 -07001099out:
1100 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return ret;
1102}
1103
Alan Cox95f9bfc2008-10-13 10:39:23 +01001104/**
1105 * tty_write_message - write a message to a certain tty, not just the console.
1106 * @tty: the destination tty_struct
1107 * @msg: the message to write
1108 *
1109 * This is used for messages that need to be redirected to a specific tty.
1110 * We don't put it into the syslog queue right now maybe in the future if
1111 * really needed.
1112 *
Arnd Bergmannec79d602010-06-01 22:53:01 +02001113 * We must still hold the BTM and test the CLOSING flag for the moment.
Alan Cox95f9bfc2008-10-13 10:39:23 +01001114 */
1115
1116void tty_write_message(struct tty_struct *tty, char *msg)
1117{
Alan Cox95f9bfc2008-10-13 10:39:23 +01001118 if (tty) {
1119 mutex_lock(&tty->atomic_write_lock);
Alan Cox89c8d912012-08-08 16:30:13 +01001120 tty_lock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001121 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001122 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +01001123 tty->ops->write(tty, msg, strlen(msg));
Alan Coxeeb89d92009-11-30 13:18:29 +00001124 } else
Alan Cox89c8d912012-08-08 16:30:13 +01001125 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +01001126 tty_write_unlock(tty);
1127 }
Alan Cox95f9bfc2008-10-13 10:39:23 +01001128 return;
1129}
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Alan Coxaf9b8972006-08-27 01:24:01 -07001132/**
1133 * tty_write - write method for tty device file
1134 * @file: tty file pointer
1135 * @buf: user data to write
1136 * @count: bytes to write
1137 * @ppos: unused
1138 *
1139 * Write data to a tty device via the line discipline.
1140 *
1141 * Locking:
1142 * Locks the line discipline as required
1143 * Writes to the tty driver are serialized by the atomic_write_lock
1144 * and are then processed in chunks to the device. The line discipline
Joe Petersona88a69c2009-01-02 13:40:53 +00001145 * write method will not be invoked in parallel for each device.
Alan Coxaf9b8972006-08-27 01:24:01 -07001146 */
1147
Alan Cox37bdfb02008-02-08 04:18:47 -08001148static ssize_t tty_write(struct file *file, const char __user *buf,
1149 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Nick Piggind996b622010-08-18 04:37:36 +10001151 struct tty_struct *tty = file_tty(file);
1152 struct tty_ldisc *ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 ssize_t ret;
Alan Cox37bdfb02008-02-08 04:18:47 -08001154
Al Viro6131ffa2013-02-27 16:59:05 -05001155 if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001157 if (!tty || !tty->ops->write ||
Alan Cox37bdfb02008-02-08 04:18:47 -08001158 (test_bit(TTY_IO_ERROR, &tty->flags)))
1159 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001160 /* Short term debug to catch buggy drivers */
1161 if (tty->ops->write_room == NULL)
1162 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1163 tty->driver->name);
Alan Cox37bdfb02008-02-08 04:18:47 -08001164 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001165 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ret = -EIO;
1167 else
Alan Coxa352def2008-07-16 21:53:12 +01001168 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 tty_ldisc_deref(ld);
1170 return ret;
1171}
1172
Alan Cox37bdfb02008-02-08 04:18:47 -08001173ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1174 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 struct file *p = NULL;
1177
1178 spin_lock(&redirect_lock);
Al Virocb0942b2012-08-27 14:48:26 -04001179 if (redirect)
1180 p = get_file(redirect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 spin_unlock(&redirect_lock);
1182
1183 if (p) {
1184 ssize_t res;
1185 res = vfs_write(p, buf, count, &p->f_pos);
1186 fput(p);
1187 return res;
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return tty_write(file, buf, count, ppos);
1190}
1191
1192static char ptychar[] = "pqrstuvwxyzabcde";
1193
Alan Coxaf9b8972006-08-27 01:24:01 -07001194/**
1195 * pty_line_name - generate name for a pty
1196 * @driver: the tty driver in use
1197 * @index: the minor number
1198 * @p: output buffer of at least 6 bytes
1199 *
1200 * Generate a name from a driver reference and write it to the output
1201 * buffer.
1202 *
1203 * Locking: None
1204 */
1205static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 int i = index + driver->name_base;
1208 /* ->name is initialized to "ttyp", but "tty" is expected */
1209 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08001210 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1211 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Alan Coxaf9b8972006-08-27 01:24:01 -07001214/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001215 * tty_line_name - generate name for a tty
Alan Coxaf9b8972006-08-27 01:24:01 -07001216 * @driver: the tty driver in use
1217 * @index: the minor number
1218 * @p: output buffer of at least 7 bytes
1219 *
1220 * Generate a name from a driver reference and write it to the output
1221 * buffer.
1222 *
1223 * Locking: None
1224 */
1225static void tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Jiri Slaby0019b402012-08-08 22:26:43 +02001227 if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
1228 strcpy(p, driver->name);
1229 else
1230 sprintf(p, "%s%d", driver->name, index + driver->name_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Alan Cox99f1fe12008-10-13 10:42:00 +01001233/**
1234 * tty_driver_lookup_tty() - find an existing tty, if any
1235 * @driver: the driver for the tty
1236 * @idx: the minor number
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001237 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001238 * Return the tty, if found or ERR_PTR() otherwise.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001239 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001240 * Locking: tty_mutex must be held. If tty is found, the mutex must
1241 * be held until the 'fast-open' is also done. Will change once we
1242 * have refcounting in the driver and per driver locking
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001243 */
Jason Wessela47d5452009-01-02 13:43:04 +00001244static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001245 struct inode *inode, int idx)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001246{
Alan Cox99f1fe12008-10-13 10:42:00 +01001247 if (driver->ops->lookup)
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001248 return driver->ops->lookup(driver, inode, idx);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001249
Jiri Slabyd4834262012-03-05 14:51:53 +01001250 return driver->ttys[idx];
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001251}
1252
Alan Cox99f1fe12008-10-13 10:42:00 +01001253/**
Alan Coxbf970ee2008-10-13 10:42:39 +01001254 * tty_init_termios - helper for termios setup
1255 * @tty: the tty to set up
1256 *
1257 * Initialise the termios structures for this tty. Thus runs under
1258 * the tty_mutex currently so we can be relaxed about ordering.
1259 */
1260
1261int tty_init_termios(struct tty_struct *tty)
1262{
Alan Coxfe6e29f2008-10-13 10:44:08 +01001263 struct ktermios *tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001264 int idx = tty->index;
1265
Alan Cox36b3c072012-07-17 17:06:57 +01001266 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1267 tty->termios = tty->driver->init_termios;
1268 else {
1269 /* Check for lazy saved data */
1270 tp = tty->driver->termios[idx];
1271 if (tp != NULL)
1272 tty->termios = *tp;
1273 else
1274 tty->termios = tty->driver->init_termios;
Alan Coxbf970ee2008-10-13 10:42:39 +01001275 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001276 /* Compatibility until drivers always set this */
Alan Coxadc8d742012-07-14 15:31:47 +01001277 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1278 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001279 return 0;
1280}
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001281EXPORT_SYMBOL_GPL(tty_init_termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001282
Jiri Slaby66d450e2012-01-30 21:14:28 +01001283int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1284{
1285 int ret = tty_init_termios(tty);
1286 if (ret)
1287 return ret;
1288
1289 tty_driver_kref_get(driver);
1290 tty->count++;
1291 driver->ttys[tty->index] = tty;
1292 return 0;
1293}
1294EXPORT_SYMBOL_GPL(tty_standard_install);
1295
Alan Coxbf970ee2008-10-13 10:42:39 +01001296/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001297 * tty_driver_install_tty() - install a tty entry in the driver
1298 * @driver: the driver for the tty
1299 * @tty: the tty
1300 *
1301 * Install a tty object into the driver tables. The tty->index field
Alan Coxbf970ee2008-10-13 10:42:39 +01001302 * will be set by the time this is called. This method is responsible
1303 * for ensuring any need additional structures are allocated and
1304 * configured.
Alan Cox8b0a88d2008-10-13 10:42:19 +01001305 *
1306 * Locking: tty_mutex for now
1307 */
1308static int tty_driver_install_tty(struct tty_driver *driver,
1309 struct tty_struct *tty)
1310{
Jiri Slaby66d450e2012-01-30 21:14:28 +01001311 return driver->ops->install ? driver->ops->install(driver, tty) :
1312 tty_standard_install(driver, tty);
Alan Cox8b0a88d2008-10-13 10:42:19 +01001313}
1314
1315/**
1316 * tty_driver_remove_tty() - remove a tty from the driver tables
1317 * @driver: the driver for the tty
1318 * @idx: the minor number
1319 *
1320 * Remvoe a tty object from the driver tables. The tty->index field
1321 * will be set by the time this is called.
1322 *
1323 * Locking: tty_mutex for now
1324 */
Jiri Slaby24d406a2011-08-10 14:59:28 +02001325void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +01001326{
1327 if (driver->ops->remove)
1328 driver->ops->remove(driver, tty);
1329 else
1330 driver->ttys[tty->index] = NULL;
1331}
1332
1333/*
1334 * tty_reopen() - fast re-open of an open tty
1335 * @tty - the tty to open
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001336 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001337 * Return 0 on success, -errno on error.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001338 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001339 * Locking: tty_mutex must be held from the time the tty was found
1340 * till this open completes.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001341 */
Alan Cox99f1fe12008-10-13 10:42:00 +01001342static int tty_reopen(struct tty_struct *tty)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001343{
1344 struct tty_driver *driver = tty->driver;
1345
Jiri Slabye2efafb2010-11-29 10:16:53 +01001346 if (test_bit(TTY_CLOSING, &tty->flags) ||
Jiri Slabyacfa7472010-11-29 10:16:54 +01001347 test_bit(TTY_HUPPING, &tty->flags) ||
Jiri Slabye2efafb2010-11-29 10:16:53 +01001348 test_bit(TTY_LDISC_CHANGING, &tty->flags))
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001349 return -EIO;
1350
1351 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1352 driver->subtype == PTY_TYPE_MASTER) {
1353 /*
1354 * special case for PTY masters: only one open permitted,
1355 * and the slave side open count is incremented as well.
1356 */
1357 if (tty->count)
1358 return -EIO;
1359
1360 tty->link->count++;
1361 }
1362 tty->count++;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001363
Alan Cox1aa4bed2009-06-16 17:01:33 +01001364 mutex_lock(&tty->ldisc_mutex);
Alan Cox99f1fe12008-10-13 10:42:00 +01001365 WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
Alan Cox1aa4bed2009-06-16 17:01:33 +01001366 mutex_unlock(&tty->ldisc_mutex);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001367
1368 return 0;
1369}
1370
Alan Coxaf9b8972006-08-27 01:24:01 -07001371/**
Alan Coxd81ed102008-10-13 10:41:42 +01001372 * tty_init_dev - initialise a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001373 * @driver: tty driver we are opening a device on
1374 * @idx: device index
Alan Cox15582d32008-10-13 10:41:03 +01001375 * @ret_tty: returned tty structure
Alan Coxaf9b8972006-08-27 01:24:01 -07001376 *
1377 * Prepare a tty device. This may not be a "new" clean device but
1378 * could also be an active device. The pty drivers require special
1379 * handling because of this.
1380 *
1381 * Locking:
1382 * The function is called under the tty_mutex, which
1383 * protects us from the tty struct or driver itself going away.
1384 *
1385 * On exit the tty device has the line discipline attached and
1386 * a reference count of 1. If a pair was created for pty/tty use
1387 * and the other was a pty master then it too has a reference count of 1.
1388 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001390 * failed open. The new code protects the open with a mutex, so it's
1391 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * relaxed for the (most common) case of reopening a tty.
1393 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001394
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001395struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Alan Coxbf970ee2008-10-13 10:42:39 +01001397 struct tty_struct *tty;
Alan Cox73ec06f2008-10-13 10:42:29 +01001398 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /*
1401 * First time open is complex, especially for PTY devices.
1402 * This code guarantees that either everything succeeds and the
1403 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001404 * and the allocated memory released. (Except that the termios
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 * and locked termios may be retained.)
1406 */
1407
Alan Cox73ec06f2008-10-13 10:42:29 +01001408 if (!try_module_get(driver->owner))
1409 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 tty = alloc_tty_struct();
Jiri Slabyd5543502011-03-23 10:48:32 +01001412 if (!tty) {
1413 retval = -ENOMEM;
1414 goto err_module_put;
1415 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001416 initialize_tty_struct(tty, driver, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Alan Cox89c8d912012-08-08 16:30:13 +01001418 tty_lock(tty);
Alan Cox73ec06f2008-10-13 10:42:29 +01001419 retval = tty_driver_install_tty(driver, tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001420 if (retval < 0)
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001421 goto err_deinit_tty;
Alan Cox8b0a88d2008-10-13 10:42:19 +01001422
Jiri Slaby04831dc2012-06-04 13:35:36 +02001423 if (!tty->port)
1424 tty->port = driver->ports[idx];
1425
Jiri Slaby5d4121c2012-08-17 14:27:52 +02001426 WARN_RATELIMIT(!tty->port,
1427 "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
1428 __func__, tty->driver->name);
1429
Jiri Slaby967fab62012-10-18 22:26:46 +02001430 tty->port->itty = tty;
1431
Alan Cox37bdfb02008-02-08 04:18:47 -08001432 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001434 * If we fail here just call release_tty to clean up. No need
1435 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 */
Alan Coxbf970ee2008-10-13 10:42:39 +01001437 retval = tty_ldisc_setup(tty, tty->link);
Alan Cox01e1abb2008-07-22 11:16:55 +01001438 if (retval)
Jiri Slabyd5543502011-03-23 10:48:32 +01001439 goto err_release_tty;
Alan Cox89c8d912012-08-08 16:30:13 +01001440 /* Return the tty locked so that it cannot vanish under the caller */
Alan Cox73ec06f2008-10-13 10:42:29 +01001441 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001443err_deinit_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001444 tty_unlock(tty);
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001445 deinitialize_tty_struct(tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001446 free_tty_struct(tty);
1447err_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 module_put(driver->owner);
Jiri Slabyd5543502011-03-23 10:48:32 +01001449 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001451 /* call the tty release_tty routine to clean out this slot */
Jiri Slabyd5543502011-03-23 10:48:32 +01001452err_release_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001453 tty_unlock(tty);
Manuel Zerpies5a3c6b22011-06-16 14:07:22 +02001454 printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, "
Akinobu Mita40509142006-09-29 02:01:27 -07001455 "clearing slot %d\n", idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001456 release_tty(tty, idx);
Alan Cox73ec06f2008-10-13 10:42:29 +01001457 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Alan Coxfeebed62008-10-13 10:41:30 +01001460void tty_free_termios(struct tty_struct *tty)
1461{
1462 struct ktermios *tp;
1463 int idx = tty->index;
Alan Cox36b3c072012-07-17 17:06:57 +01001464
1465 /* If the port is going to reset then it has no termios to save */
1466 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1467 return;
1468
1469 /* Stash the termios data */
1470 tp = tty->driver->termios[idx];
1471 if (tp == NULL) {
1472 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1473 if (tp == NULL) {
1474 pr_warn("tty: no memory to save termios state.\n");
1475 return;
1476 }
Dan Carpenter4ac5d702012-07-24 12:51:52 +01001477 tty->driver->termios[idx] = tp;
Alan Coxfeebed62008-10-13 10:41:30 +01001478 }
Alan Cox36b3c072012-07-17 17:06:57 +01001479 *tp = tty->termios;
Alan Coxfeebed62008-10-13 10:41:30 +01001480}
1481EXPORT_SYMBOL(tty_free_termios);
1482
Alan Coxfeebed62008-10-13 10:41:30 +01001483
Alan Coxaf9b8972006-08-27 01:24:01 -07001484/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001485 * release_one_tty - release tty structure memory
Alan Cox9c9f4de2008-10-13 10:37:26 +01001486 * @kref: kref of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001487 *
1488 * Releases memory associated with a tty structure, and clears out the
1489 * driver table slots. This function is called when a device is no longer
1490 * in use. It also gets called when setup of a device fails.
1491 *
1492 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -07001493 * takes the file list lock internally when working on the list
1494 * of ttys that the driver keeps.
Alan Coxb50989d2009-09-19 13:13:22 -07001495 *
1496 * This method gets called from a work queue so that the driver private
Dave Youngf278a2f2009-09-27 16:00:42 +00001497 * cleanup ops can sleep (needed for USB at least)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
Alan Coxb50989d2009-09-19 13:13:22 -07001499static void release_one_tty(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Alan Coxb50989d2009-09-19 13:13:22 -07001501 struct tty_struct *tty =
1502 container_of(work, struct tty_struct, hangup_work);
Alan Cox6f967f72008-10-13 10:37:36 +01001503 struct tty_driver *driver = tty->driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Dave Youngf278a2f2009-09-27 16:00:42 +00001505 if (tty->ops->cleanup)
1506 tty->ops->cleanup(tty);
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 tty->magic = 0;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001509 tty_driver_kref_put(driver);
Alan Cox6f967f72008-10-13 10:37:36 +01001510 module_put(driver->owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001511
Nick Pigginee2ffa02010-08-18 04:37:35 +10001512 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 list_del_init(&tty->tty_files);
Nick Pigginee2ffa02010-08-18 04:37:35 +10001514 spin_unlock(&tty_files_lock);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001515
Oleg Nesterov6da8d862010-04-02 18:05:12 +02001516 put_pid(tty->pgrp);
1517 put_pid(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 free_tty_struct(tty);
1519}
1520
Alan Coxb50989d2009-09-19 13:13:22 -07001521static void queue_release_one_tty(struct kref *kref)
1522{
1523 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Dave Youngf278a2f2009-09-27 16:00:42 +00001524
Alan Coxb50989d2009-09-19 13:13:22 -07001525 /* The hangup queue is now free so we can reuse it rather than
1526 waste a chunk of memory for each port */
1527 INIT_WORK(&tty->hangup_work, release_one_tty);
1528 schedule_work(&tty->hangup_work);
1529}
1530
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001531/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001532 * tty_kref_put - release a tty kref
1533 * @tty: tty device
1534 *
1535 * Release a reference to a tty device and if need be let the kref
1536 * layer destruct the object for us
1537 */
1538
1539void tty_kref_put(struct tty_struct *tty)
1540{
1541 if (tty)
Alan Coxb50989d2009-09-19 13:13:22 -07001542 kref_put(&tty->kref, queue_release_one_tty);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001543}
1544EXPORT_SYMBOL(tty_kref_put);
1545
1546/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001547 * release_tty - release tty structure memory
1548 *
1549 * Release both @tty and a possible linked partner (think pty pair),
1550 * and decrement the refcount of the backing module.
1551 *
1552 * Locking:
Alan Coxd1552552012-07-27 18:02:54 +01001553 * tty_mutex
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001554 * takes the file list lock internally when working on the list
1555 * of ttys that the driver keeps.
Alan Cox9c9f4de2008-10-13 10:37:26 +01001556 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001557 */
1558static void release_tty(struct tty_struct *tty, int idx)
1559{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001560 /* This should always be true but check for the moment */
1561 WARN_ON(tty->index != idx);
Alan Coxd1552552012-07-27 18:02:54 +01001562 WARN_ON(!mutex_is_locked(&tty_mutex));
Alan Cox36b3c072012-07-17 17:06:57 +01001563 if (tty->ops->shutdown)
1564 tty->ops->shutdown(tty);
1565 tty_free_termios(tty);
1566 tty_driver_remove_tty(tty->driver, tty);
Jiri Slaby967fab62012-10-18 22:26:46 +02001567 tty->port->itty = NULL;
Alan Cox36b3c072012-07-17 17:06:57 +01001568
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001569 if (tty->link)
Alan Cox9c9f4de2008-10-13 10:37:26 +01001570 tty_kref_put(tty->link);
1571 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001572}
1573
Alan Coxeeb89d92009-11-30 13:18:29 +00001574/**
Jiri Slaby955787c2011-11-11 10:47:23 +01001575 * tty_release_checks - check a tty before real release
1576 * @tty: tty to check
1577 * @o_tty: link of @tty (if any)
1578 * @idx: index of the tty
1579 *
1580 * Performs some paranoid checking before true release of the @tty.
1581 * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1582 */
1583static int tty_release_checks(struct tty_struct *tty, struct tty_struct *o_tty,
1584 int idx)
1585{
1586#ifdef TTY_PARANOIA_CHECK
1587 if (idx < 0 || idx >= tty->driver->num) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001588 printk(KERN_DEBUG "%s: bad idx when trying to free (%s)\n",
1589 __func__, tty->name);
Jiri Slaby955787c2011-11-11 10:47:23 +01001590 return -1;
1591 }
1592
1593 /* not much to check for devpts */
1594 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1595 return 0;
1596
1597 if (tty != tty->driver->ttys[idx]) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001598 printk(KERN_DEBUG "%s: driver.table[%d] not tty for (%s)\n",
1599 __func__, idx, tty->name);
Jiri Slaby955787c2011-11-11 10:47:23 +01001600 return -1;
1601 }
Jiri Slaby955787c2011-11-11 10:47:23 +01001602 if (tty->driver->other) {
1603 if (o_tty != tty->driver->other->ttys[idx]) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001604 printk(KERN_DEBUG "%s: other->table[%d] not o_tty for (%s)\n",
1605 __func__, idx, tty->name);
Jiri Slaby955787c2011-11-11 10:47:23 +01001606 return -1;
1607 }
Jiri Slaby955787c2011-11-11 10:47:23 +01001608 if (o_tty->link != tty) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001609 printk(KERN_DEBUG "%s: bad pty pointers\n", __func__);
Jiri Slaby955787c2011-11-11 10:47:23 +01001610 return -1;
1611 }
1612 }
1613#endif
1614 return 0;
1615}
1616
1617/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001618 * tty_release - vfs callback for close
1619 * @inode: inode of tty
1620 * @filp: file pointer for handle to tty
1621 *
1622 * Called the last time each file handle is closed that references
1623 * this tty. There may however be several such references.
1624 *
1625 * Locking:
1626 * Takes bkl. See tty_release_dev
1627 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * Even releasing the tty structures is a tricky business.. We have
1629 * to be very careful that the structures are all released at the
1630 * same time, as interrupts might otherwise get the wrong pointers.
1631 *
1632 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1633 * lead to double frees or releasing memory still in use.
1634 */
Alan Coxeeb89d92009-11-30 13:18:29 +00001635
1636int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Nick Piggind996b622010-08-18 04:37:36 +10001638 struct tty_struct *tty = file_tty(filp);
1639 struct tty_struct *o_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 int pty_master, tty_closing, o_tty_closing, do_sleep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int idx;
1642 char buf[64];
Alan Cox37bdfb02008-02-08 04:18:47 -08001643
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001644 if (tty_paranoia_check(tty, inode, __func__))
Alan Coxeeb89d92009-11-30 13:18:29 +00001645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Alan Cox89c8d912012-08-08 16:30:13 +01001647 tty_lock(tty);
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001648 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Arnd Bergmannec79d602010-06-01 22:53:01 +02001650 __tty_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 idx = tty->index;
1653 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1654 tty->driver->subtype == PTY_TYPE_MASTER);
Alan Cox89c8d912012-08-08 16:30:13 +01001655 /* Review: parallel close */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 o_tty = tty->link;
1657
Jiri Slaby955787c2011-11-11 10:47:23 +01001658 if (tty_release_checks(tty, o_tty, idx)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001659 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001664 printk(KERN_DEBUG "%s: %s (tty count=%d)...\n", __func__,
1665 tty_name(tty, buf), tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666#endif
1667
Alan Coxf34d7a52008-04-30 00:54:13 -07001668 if (tty->ops->close)
1669 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Alan Cox89c8d912012-08-08 16:30:13 +01001671 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 /*
1673 * Sanity check: if tty->count is going to zero, there shouldn't be
1674 * any waiters on tty->read_wait or tty->write_wait. We test the
1675 * wait queues and kick everyone out _before_ actually starting to
1676 * close. This ensures that we won't block while releasing the tty
1677 * structure.
1678 *
1679 * The test for the o_tty closing is necessary, since the master and
1680 * slave sides may close in any order. If the slave side closes out
1681 * first, its count will be one, since the master side holds an open.
1682 * Thus this test wouldn't be triggered at the time the slave closes,
1683 * so we do it now.
1684 *
1685 * Note that it's possible for the tty to be opened again while we're
1686 * flushing out waiters. By recalculating the closing flags before
1687 * each iteration we avoid any problems.
1688 */
1689 while (1) {
1690 /* Guard against races with tty->count changes elsewhere and
1691 opens on /dev/tty */
Alan Cox37bdfb02008-02-08 04:18:47 -08001692
Ingo Molnar70522e12006-03-23 03:00:31 -08001693 mutex_lock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01001694 tty_lock_pair(tty, o_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 tty_closing = tty->count <= 1;
1696 o_tty_closing = o_tty &&
1697 (o_tty->count <= (pty_master ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 do_sleep = 0;
1699
1700 if (tty_closing) {
1701 if (waitqueue_active(&tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001702 wake_up_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 do_sleep++;
1704 }
1705 if (waitqueue_active(&tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001706 wake_up_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 do_sleep++;
1708 }
1709 }
1710 if (o_tty_closing) {
1711 if (waitqueue_active(&o_tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001712 wake_up_poll(&o_tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 do_sleep++;
1714 }
1715 if (waitqueue_active(&o_tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001716 wake_up_poll(&o_tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 do_sleep++;
1718 }
1719 }
1720 if (!do_sleep)
1721 break;
1722
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001723 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n",
1724 __func__, tty_name(tty, buf));
Alan Cox89c8d912012-08-08 16:30:13 +01001725 tty_unlock_pair(tty, o_tty);
Ingo Molnar70522e12006-03-23 03:00:31 -08001726 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 schedule();
Alan Cox37bdfb02008-02-08 04:18:47 -08001728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08001731 * The closing flags are now consistent with the open counts on
1732 * both sides, and we've completed the last operation that could
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 * block, so it's safe to proceed with closing.
Alan Coxd1552552012-07-27 18:02:54 +01001734 *
1735 * We must *not* drop the tty_mutex until we ensure that a further
1736 * entry into tty_open can not pick up this tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (pty_master) {
1739 if (--o_tty->count < 0) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001740 printk(KERN_WARNING "%s: bad pty slave count (%d) for %s\n",
1741 __func__, o_tty->count, tty_name(o_tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 o_tty->count = 0;
1743 }
1744 }
1745 if (--tty->count < 0) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001746 printk(KERN_WARNING "%s: bad tty->count (%d) for %s\n",
1747 __func__, tty->count, tty_name(tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 tty->count = 0;
1749 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
1752 * We've decremented tty->count, so we need to remove this file
1753 * descriptor off the tty->tty_files list; this serves two
1754 * purposes:
1755 * - check_tty_count sees the correct number of file descriptors
1756 * associated with this tty.
1757 * - do_tty_hangup no longer sees this file descriptor as
1758 * something that needs to be handled for hangups.
1759 */
Nick Piggind996b622010-08-18 04:37:36 +10001760 tty_del_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 /*
1763 * Perform some housekeeping before deciding whether to return.
1764 *
1765 * Set the TTY_CLOSING flag if this was the last open. In the
1766 * case of a pty we may have to wait around for the other side
1767 * to close, and TTY_CLOSING makes sure we can't be reopened.
1768 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001769 if (tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 set_bit(TTY_CLOSING, &tty->flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08001771 if (o_tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 set_bit(TTY_CLOSING, &o_tty->flags);
1773
1774 /*
1775 * If _either_ side is closing, make sure there aren't any
1776 * processes that still think tty or o_tty is their controlling
1777 * tty.
1778 */
1779 if (tty_closing || o_tty_closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001781 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001783 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 read_unlock(&tasklist_lock);
1785 }
1786
Ingo Molnar70522e12006-03-23 03:00:31 -08001787 mutex_unlock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01001788 tty_unlock_pair(tty, o_tty);
Alan Coxd1552552012-07-27 18:02:54 +01001789 /* At this point the TTY_CLOSING flag should ensure a dead tty
1790 cannot be re-opened by a racing opener */
Paul Fulghumda965822006-02-14 13:53:00 -08001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* check whether both sides are closing ... */
Alan Coxd1552552012-07-27 18:02:54 +01001793 if (!tty_closing || (o_tty && !o_tty_closing))
Alan Coxeeb89d92009-11-30 13:18:29 +00001794 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001797 printk(KERN_DEBUG "%s: freeing tty structure...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798#endif
1799 /*
Alan Cox01e1abb2008-07-22 11:16:55 +01001800 * Ask the line discipline code to release its structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 */
Alan Cox01e1abb2008-07-22 11:16:55 +01001802 tty_ldisc_release(tty, o_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 /*
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001804 * The release_tty function takes care of the details of clearing
Alan Cox89c8d912012-08-08 16:30:13 +01001805 * the slots and preserving the termios structure. The tty_unlock_pair
1806 * should be safe as we keep a kref while the tty is locked (so the
1807 * unlock never unlocks a freed tty).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 */
Alan Coxd1552552012-07-27 18:02:54 +01001809 mutex_lock(&tty_mutex);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001810 release_tty(tty, idx);
Alan Coxd1552552012-07-27 18:02:54 +01001811 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Alan Coxeeb89d92009-11-30 13:18:29 +00001813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
Alan Coxaf9b8972006-08-27 01:24:01 -07001816/**
Jiri Slabyb82154a2011-11-09 21:33:19 +01001817 * tty_open_current_tty - get tty of current task for open
1818 * @device: device number
1819 * @filp: file pointer to tty
1820 * @return: tty of the current task iff @device is /dev/tty
1821 *
1822 * We cannot return driver and index like for the other nodes because
1823 * devpts will not work then. It expects inodes to be from devpts FS.
Alan Cox3af502b2012-05-03 22:21:53 +01001824 *
1825 * We need to move to returning a refcounted object from all the lookup
1826 * paths including this one.
Jiri Slabyb82154a2011-11-09 21:33:19 +01001827 */
1828static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1829{
1830 struct tty_struct *tty;
1831
1832 if (device != MKDEV(TTYAUX_MAJOR, 0))
1833 return NULL;
1834
1835 tty = get_current_tty();
1836 if (!tty)
1837 return ERR_PTR(-ENXIO);
1838
1839 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1840 /* noctty = 1; */
1841 tty_kref_put(tty);
1842 /* FIXME: we put a reference and return a TTY! */
Alan Cox3af502b2012-05-03 22:21:53 +01001843 /* This is only safe because the caller holds tty_mutex */
Jiri Slabyb82154a2011-11-09 21:33:19 +01001844 return tty;
1845}
1846
1847/**
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001848 * tty_lookup_driver - lookup a tty driver for a given device file
1849 * @device: device number
1850 * @filp: file pointer to tty
1851 * @noctty: set if the device should not become a controlling tty
1852 * @index: index for the device in the @return driver
1853 * @return: driver for this inode (with increased refcount)
1854 *
1855 * If @return is not erroneous, the caller is responsible to decrement the
1856 * refcount by tty_driver_kref_put.
1857 *
1858 * Locking: tty_mutex protects get_tty_driver
1859 */
1860static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
1861 int *noctty, int *index)
1862{
1863 struct tty_driver *driver;
1864
Jiri Slaby2cd00502011-11-09 21:33:22 +01001865 switch (device) {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001866#ifdef CONFIG_VT
Jiri Slaby2cd00502011-11-09 21:33:22 +01001867 case MKDEV(TTY_MAJOR, 0): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001868 extern struct tty_driver *console_driver;
1869 driver = tty_driver_kref_get(console_driver);
1870 *index = fg_console;
1871 *noctty = 1;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001872 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001873 }
1874#endif
Jiri Slaby2cd00502011-11-09 21:33:22 +01001875 case MKDEV(TTYAUX_MAJOR, 1): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001876 struct tty_driver *console_driver = console_device(index);
1877 if (console_driver) {
1878 driver = tty_driver_kref_get(console_driver);
1879 if (driver) {
1880 /* Don't let /dev/console block */
1881 filp->f_flags |= O_NONBLOCK;
1882 *noctty = 1;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001883 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001884 }
1885 }
1886 return ERR_PTR(-ENODEV);
1887 }
Jiri Slaby2cd00502011-11-09 21:33:22 +01001888 default:
1889 driver = get_tty_driver(device, index);
1890 if (!driver)
1891 return ERR_PTR(-ENODEV);
1892 break;
1893 }
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001894 return driver;
1895}
1896
1897/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001898 * tty_open - open a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001899 * @inode: inode of device file
1900 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001902 * tty_open and tty_release keep up the tty count that contains the
1903 * number of opens done on a tty. We cannot use the inode-count, as
1904 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001906 * Open-counting is needed for pty masters, as well as for keeping
1907 * track of serial lines: DTR is dropped when the last close happens.
1908 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1909 *
1910 * The termios state of a pty is reset on first open so that
1911 * settings don't persist across reuse.
1912 *
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001913 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001914 * tty->count should protect the rest.
1915 * ->siglock protects ->signal/->sighand
Alan Cox89c8d912012-08-08 16:30:13 +01001916 *
1917 * Note: the tty_unlock/lock cases without a ref are only safe due to
1918 * tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001920
Alan Coxeeb89d92009-11-30 13:18:29 +00001921static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Jiri Slabyb82154a2011-11-09 21:33:19 +01001923 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 int noctty, retval;
Jiri Slabyb82154a2011-11-09 21:33:19 +01001925 struct tty_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 int index;
1927 dev_t device = inode->i_rdev;
Andrew Morton846c1512009-04-02 16:56:36 -07001928 unsigned saved_flags = filp->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932retry_open:
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02001933 retval = tty_alloc_file(filp);
1934 if (retval)
1935 return -ENOMEM;
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 noctty = filp->f_flags & O_NOCTTY;
1938 index = -1;
1939 retval = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001940
Ingo Molnar70522e12006-03-23 03:00:31 -08001941 mutex_lock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01001942 /* This is protected by the tty_mutex */
Jiri Slabyb82154a2011-11-09 21:33:19 +01001943 tty = tty_open_current_tty(device, filp);
1944 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001945 retval = PTR_ERR(tty);
1946 goto err_unlock;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001947 } else if (!tty) {
1948 driver = tty_lookup_driver(device, filp, &noctty, &index);
1949 if (IS_ERR(driver)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001950 retval = PTR_ERR(driver);
1951 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001954 /* check whether we're reopening an existing tty */
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001955 tty = tty_driver_lookup_tty(driver, inode, index);
Eric Paris808ffa32009-01-27 11:50:37 +00001956 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001957 retval = PTR_ERR(tty);
1958 goto err_unlock;
Eric Paris808ffa32009-01-27 11:50:37 +00001959 }
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001960 }
1961
1962 if (tty) {
Alan Cox89c8d912012-08-08 16:30:13 +01001963 tty_lock(tty);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001964 retval = tty_reopen(tty);
Alan Cox89c8d912012-08-08 16:30:13 +01001965 if (retval < 0) {
1966 tty_unlock(tty);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001967 tty = ERR_PTR(retval);
Alan Cox89c8d912012-08-08 16:30:13 +01001968 }
1969 } else /* Returns with the tty_lock held for now */
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001970 tty = tty_init_dev(driver, index);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001971
Ingo Molnar70522e12006-03-23 03:00:31 -08001972 mutex_unlock(&tty_mutex);
Jiri Slabyb82154a2011-11-09 21:33:19 +01001973 if (driver)
1974 tty_driver_kref_put(driver);
Alan Coxeeb89d92009-11-30 13:18:29 +00001975 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001976 retval = PTR_ERR(tty);
1977 goto err_file;
Alan Coxeeb89d92009-11-30 13:18:29 +00001978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02001980 tty_add_file(tty, filp);
Nick Piggind996b622010-08-18 04:37:36 +10001981
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001982 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1984 tty->driver->subtype == PTY_TYPE_MASTER)
1985 noctty = 1;
1986#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001987 printk(KERN_DEBUG "%s: opening %s...\n", __func__, tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988#endif
Herton Ronaldo Krzesinski909bc772011-03-31 15:35:31 -03001989 if (tty->ops->open)
1990 retval = tty->ops->open(tty, filp);
1991 else
1992 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 filp->f_flags = saved_flags;
1994
Alan Cox37bdfb02008-02-08 04:18:47 -08001995 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1996 !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 retval = -EBUSY;
1998
1999 if (retval) {
2000#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01002001 printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__,
2002 retval, tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003#endif
Alan Cox89c8d912012-08-08 16:30:13 +01002004 tty_unlock(tty); /* need to call tty_release without BTM */
Alan Coxeeb89d92009-11-30 13:18:29 +00002005 tty_release(inode, filp);
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002006 if (retval != -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002008
2009 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 schedule();
2013 /*
2014 * Need to reset f_op in case a hangup happened.
2015 */
2016 if (filp->f_op == &hung_up_tty_fops)
2017 filp->f_op = &tty_fops;
2018 goto retry_open;
2019 }
Alan Cox89c8d912012-08-08 16:30:13 +01002020 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00002021
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002022
2023 mutex_lock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01002024 tty_lock(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002025 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (!noctty &&
2027 current->signal->leader &&
2028 !current->signal->tty &&
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002029 tty->session == NULL)
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07002030 __proc_set_tty(current, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002031 spin_unlock_irq(&current->sighand->siglock);
Alan Cox89c8d912012-08-08 16:30:13 +01002032 tty_unlock(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002033 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return 0;
Jiri Slabyba5db442011-11-09 21:33:21 +01002035err_unlock:
Jiri Slabyba5db442011-11-09 21:33:21 +01002036 mutex_unlock(&tty_mutex);
2037 /* after locks to avoid deadlock */
2038 if (!IS_ERR_OR_NULL(driver))
2039 tty_driver_kref_put(driver);
2040err_file:
2041 tty_free_file(filp);
2042 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Jonathan Corbet39d95b92008-05-16 09:10:50 -06002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Alan Coxaf9b8972006-08-27 01:24:01 -07002047/**
2048 * tty_poll - check tty status
2049 * @filp: file being polled
2050 * @wait: poll wait structures to update
2051 *
2052 * Call the line discipline polling method to obtain the poll
2053 * status of the device.
2054 *
2055 * Locking: locks called line discipline but ldisc poll method
2056 * may be re-entered freely by other callers.
2057 */
2058
Alan Cox37bdfb02008-02-08 04:18:47 -08002059static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Nick Piggind996b622010-08-18 04:37:36 +10002061 struct tty_struct *tty = file_tty(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 struct tty_ldisc *ld;
2063 int ret = 0;
2064
Al Viro6131ffa2013-02-27 16:59:05 -05002065 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002069 if (ld->ops->poll)
2070 ret = (ld->ops->poll)(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 tty_ldisc_deref(ld);
2072 return ret;
2073}
2074
Arnd Bergmannec79d602010-06-01 22:53:01 +02002075static int __tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Nick Piggind996b622010-08-18 04:37:36 +10002077 struct tty_struct *tty = file_tty(filp);
Alan Cox47f86832008-04-30 00:53:30 -07002078 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002079 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Al Viro6131ffa2013-02-27 16:59:05 -05002081 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002082 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 retval = fasync_helper(fd, filp, on, &tty->fasync);
2085 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002086 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002089 enum pid_type type;
2090 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (!waitqueue_active(&tty->read_wait))
2092 tty->minimum_to_wake = 1;
Alan Cox47f86832008-04-30 00:53:30 -07002093 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002094 if (tty->pgrp) {
2095 pid = tty->pgrp;
2096 type = PIDTYPE_PGID;
2097 } else {
2098 pid = task_pid(current);
2099 type = PIDTYPE_PID;
2100 }
Linus Torvalds80e1e822010-02-07 10:11:23 -08002101 get_pid(pid);
Greg Kroah-Hartman70362512009-12-17 07:07:19 -08002102 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds80e1e822010-02-07 10:11:23 -08002103 retval = __f_setown(filp, pid, type, 0);
2104 put_pid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (retval)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 } else {
2108 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2109 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2110 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002111 retval = 0;
2112out:
Arnd Bergmannec79d602010-06-01 22:53:01 +02002113 return retval;
2114}
2115
2116static int tty_fasync(int fd, struct file *filp, int on)
2117{
Alan Cox89c8d912012-08-08 16:30:13 +01002118 struct tty_struct *tty = file_tty(filp);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002119 int retval;
Alan Cox89c8d912012-08-08 16:30:13 +01002120
2121 tty_lock(tty);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002122 retval = __tty_fasync(fd, filp, on);
Alan Cox89c8d912012-08-08 16:30:13 +01002123 tty_unlock(tty);
2124
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002125 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Alan Coxaf9b8972006-08-27 01:24:01 -07002128/**
2129 * tiocsti - fake input character
2130 * @tty: tty to fake input into
2131 * @p: pointer to character
2132 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02002133 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07002134 * input management.
2135 *
2136 * FIXME: does not honour flow control ??
2137 *
2138 * Locking:
2139 * Called functions take tty_ldisc_lock
2140 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07002141 *
2142 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07002143 */
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145static int tiocsti(struct tty_struct *tty, char __user *p)
2146{
2147 char ch, mbz = 0;
2148 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2151 return -EPERM;
2152 if (get_user(ch, p))
2153 return -EFAULT;
Al Viro1e641742008-12-09 09:23:33 +00002154 tty_audit_tiocsti(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002156 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 tty_ldisc_deref(ld);
2158 return 0;
2159}
2160
Alan Coxaf9b8972006-08-27 01:24:01 -07002161/**
2162 * tiocgwinsz - implement window query ioctl
2163 * @tty; tty
2164 * @arg: user buffer for result
2165 *
Alan Cox808a0d32006-09-29 02:00:40 -07002166 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002167 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002168 * Locking: tty->termios_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002169 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002170 */
2171
Alan Cox37bdfb02008-02-08 04:18:47 -08002172static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Alan Cox808a0d32006-09-29 02:00:40 -07002174 int err;
2175
Arjan van de Ven5785c952006-09-29 02:00:43 -07002176 mutex_lock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002177 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Arjan van de Ven5785c952006-09-29 02:00:43 -07002178 mutex_unlock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002179
2180 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Alan Coxaf9b8972006-08-27 01:24:01 -07002183/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002184 * tty_do_resize - resize event
2185 * @tty: tty being resized
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002186 * @rows: rows (character)
2187 * @cols: cols (character)
Alan Coxaf9b8972006-08-27 01:24:01 -07002188 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002189 * Update the termios variables and send the necessary signals to
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002190 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002191 */
2192
Alan Coxfc6f62382009-01-02 13:43:17 +00002193int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Alan Coxfc6f62382009-01-02 13:43:17 +00002195 struct pid *pgrp;
Alan Cox47f86832008-04-30 00:53:30 -07002196 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Alan Coxfc6f62382009-01-02 13:43:17 +00002198 /* Lock the tty */
2199 mutex_lock(&tty->termios_mutex);
2200 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002201 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002202 /* Get the PID values and reference them so we can
2203 avoid holding the tty ctrl lock while sending signals */
2204 spin_lock_irqsave(&tty->ctrl_lock, flags);
2205 pgrp = get_pid(tty->pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002206 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2207
2208 if (pgrp)
2209 kill_pgrp(pgrp, SIGWINCH, 1);
Alan Cox47f86832008-04-30 00:53:30 -07002210 put_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002211
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002212 tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002213done:
Alan Coxfc6f62382009-01-02 13:43:17 +00002214 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return 0;
2216}
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01002217EXPORT_SYMBOL(tty_do_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Alan Coxaf9b8972006-08-27 01:24:01 -07002219/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002220 * tiocswinsz - implement window size set ioctl
Alan Coxfc6f62382009-01-02 13:43:17 +00002221 * @tty; tty side of tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002222 * @arg: user buffer for result
2223 *
2224 * Copies the user idea of the window size to the kernel. Traditionally
2225 * this is just advisory information but for the Linux console it
2226 * actually has driver level meaning and triggers a VC resize.
2227 *
2228 * Locking:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002229 * Driver dependent. The default do_resize method takes the
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002230 * tty termios mutex and ctrl_lock. The console takes its own lock
2231 * then calls into the default method.
2232 */
2233
Alan Coxfc6f62382009-01-02 13:43:17 +00002234static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002235{
2236 struct winsize tmp_ws;
2237 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2238 return -EFAULT;
2239
2240 if (tty->ops->resize)
Alan Coxfc6f62382009-01-02 13:43:17 +00002241 return tty->ops->resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002242 else
Alan Coxfc6f62382009-01-02 13:43:17 +00002243 return tty_do_resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002244}
2245
2246/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002247 * tioccons - allow admin to move logical console
2248 * @file: the file to become console
2249 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002250 * Allow the administrator to move the redirected console device
Alan Coxaf9b8972006-08-27 01:24:01 -07002251 *
2252 * Locking: uses redirect_lock to guard the redirect information
2253 */
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255static int tioccons(struct file *file)
2256{
2257 if (!capable(CAP_SYS_ADMIN))
2258 return -EPERM;
2259 if (file->f_op->write == redirected_tty_write) {
2260 struct file *f;
2261 spin_lock(&redirect_lock);
2262 f = redirect;
2263 redirect = NULL;
2264 spin_unlock(&redirect_lock);
2265 if (f)
2266 fput(f);
2267 return 0;
2268 }
2269 spin_lock(&redirect_lock);
2270 if (redirect) {
2271 spin_unlock(&redirect_lock);
2272 return -EBUSY;
2273 }
Al Virocb0942b2012-08-27 14:48:26 -04002274 redirect = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 spin_unlock(&redirect_lock);
2276 return 0;
2277}
2278
Alan Coxaf9b8972006-08-27 01:24:01 -07002279/**
2280 * fionbio - non blocking ioctl
2281 * @file: file to set blocking value
2282 * @p: user parameter
2283 *
2284 * Historical tty interfaces had a blocking control ioctl before
2285 * the generic functionality existed. This piece of history is preserved
2286 * in the expected tty API of posix OS's.
2287 *
Alan Cox6146b9a2009-09-19 13:13:19 -07002288 * Locking: none, the open file handle ensures it won't go away.
Alan Coxaf9b8972006-08-27 01:24:01 -07002289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291static int fionbio(struct file *file, int __user *p)
2292{
2293 int nonblock;
2294
2295 if (get_user(nonblock, p))
2296 return -EFAULT;
2297
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002298 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (nonblock)
2300 file->f_flags |= O_NONBLOCK;
2301 else
2302 file->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002303 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return 0;
2305}
2306
Alan Coxaf9b8972006-08-27 01:24:01 -07002307/**
2308 * tiocsctty - set controlling tty
2309 * @tty: tty structure
2310 * @arg: user argument
2311 *
2312 * This ioctl is used to manage job control. It permits a session
2313 * leader to set this tty as the controlling tty for the session.
2314 *
2315 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002316 * Takes tty_mutex() to protect tty instance
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002317 * Takes tasklist_lock internally to walk sessions
2318 * Takes ->siglock() when updating signal->tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002319 */
2320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321static int tiocsctty(struct tty_struct *tty, int arg)
2322{
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002323 int ret = 0;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002324 if (current->signal->leader && (task_session(current) == tty->session))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002325 return ret;
2326
2327 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 /*
2329 * The process must be a session leader and
2330 * not have a controlling tty already.
2331 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002332 if (!current->signal->leader || current->signal->tty) {
2333 ret = -EPERM;
2334 goto unlock;
2335 }
2336
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002337 if (tty->session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /*
2339 * This tty is already the controlling
2340 * tty for another session group!
2341 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002342 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 /*
2344 * Steal it away
2345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002347 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 read_unlock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002349 } else {
2350 ret = -EPERM;
2351 goto unlock;
2352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002354 proc_set_tty(current, tty);
2355unlock:
Alan Cox28298232006-09-29 02:00:58 -07002356 mutex_unlock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002357 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
Alan Coxaf9b8972006-08-27 01:24:01 -07002360/**
Alan Cox5d0fdf12008-04-30 00:53:31 -07002361 * tty_get_pgrp - return a ref counted pgrp pid
2362 * @tty: tty to read
2363 *
2364 * Returns a refcounted instance of the pid struct for the process
2365 * group controlling the tty.
2366 */
2367
2368struct pid *tty_get_pgrp(struct tty_struct *tty)
2369{
2370 unsigned long flags;
2371 struct pid *pgrp;
2372
2373 spin_lock_irqsave(&tty->ctrl_lock, flags);
2374 pgrp = get_pid(tty->pgrp);
2375 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2376
2377 return pgrp;
2378}
2379EXPORT_SYMBOL_GPL(tty_get_pgrp);
2380
2381/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002382 * tiocgpgrp - get process group
2383 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002384 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002385 * @p: returned pid
2386 *
2387 * Obtain the process group of the tty. If there is no process group
2388 * return an error.
2389 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002390 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002391 */
2392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2394{
Alan Cox5d0fdf12008-04-30 00:53:31 -07002395 struct pid *pid;
2396 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 /*
2398 * (tty == real_tty) is a cheap way of
2399 * testing if the tty is NOT a master pty.
2400 */
2401 if (tty == real_tty && current->signal->tty != real_tty)
2402 return -ENOTTY;
Alan Cox5d0fdf12008-04-30 00:53:31 -07002403 pid = tty_get_pgrp(real_tty);
2404 ret = put_user(pid_vnr(pid), p);
2405 put_pid(pid);
2406 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
Alan Coxaf9b8972006-08-27 01:24:01 -07002409/**
2410 * tiocspgrp - attempt to set process group
2411 * @tty: tty passed by user
2412 * @real_tty: tty side device matching tty passed by user
2413 * @p: pid pointer
2414 *
2415 * Set the process group of the tty to the session passed. Only
2416 * permitted where the tty session is our session.
2417 *
Alan Cox47f86832008-04-30 00:53:30 -07002418 * Locking: RCU, ctrl lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002419 */
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2422{
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002423 struct pid *pgrp;
2424 pid_t pgrp_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 int retval = tty_check_change(real_tty);
Alan Cox47f86832008-04-30 00:53:30 -07002426 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 if (retval == -EIO)
2429 return -ENOTTY;
2430 if (retval)
2431 return retval;
2432 if (!current->signal->tty ||
2433 (current->signal->tty != real_tty) ||
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002434 (real_tty->session != task_session(current)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return -ENOTTY;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002436 if (get_user(pgrp_nr, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return -EFAULT;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002438 if (pgrp_nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return -EINVAL;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002440 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002441 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002442 retval = -ESRCH;
2443 if (!pgrp)
2444 goto out_unlock;
2445 retval = -EPERM;
2446 if (session_of_pgrp(pgrp) != task_session(current))
2447 goto out_unlock;
2448 retval = 0;
Alan Cox47f86832008-04-30 00:53:30 -07002449 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002450 put_pid(real_tty->pgrp);
2451 real_tty->pgrp = get_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002452 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002453out_unlock:
2454 rcu_read_unlock();
2455 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Alan Coxaf9b8972006-08-27 01:24:01 -07002458/**
2459 * tiocgsid - get session id
2460 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002461 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002462 * @p: pointer to returned session id
2463 *
2464 * Obtain the session id of the tty. If there is no session
2465 * return an error.
2466 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002467 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002468 */
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2471{
2472 /*
2473 * (tty == real_tty) is a cheap way of
2474 * testing if the tty is NOT a master pty.
2475 */
2476 if (tty == real_tty && current->signal->tty != real_tty)
2477 return -ENOTTY;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002478 if (!real_tty->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return -ENOTTY;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002480 return put_user(pid_vnr(real_tty->session), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481}
2482
Alan Coxaf9b8972006-08-27 01:24:01 -07002483/**
2484 * tiocsetd - set line discipline
2485 * @tty: tty device
2486 * @p: pointer to user data
2487 *
2488 * Set the line discipline according to user request.
2489 *
2490 * Locking: see tty_set_ldisc, this function is just a helper
2491 */
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493static int tiocsetd(struct tty_struct *tty, int __user *p)
2494{
2495 int ldisc;
Alan Cox04f378b2008-04-30 00:53:29 -07002496 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 if (get_user(ldisc, p))
2499 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002500
Alan Cox04f378b2008-04-30 00:53:29 -07002501 ret = tty_set_ldisc(tty, ldisc);
Alan Cox04f378b2008-04-30 00:53:29 -07002502
2503 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
Alan Coxaf9b8972006-08-27 01:24:01 -07002506/**
2507 * send_break - performed time break
2508 * @tty: device to break on
2509 * @duration: timeout in mS
2510 *
2511 * Perform a timed break on hardware that lacks its own driver level
2512 * timed break functionality.
2513 *
2514 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002515 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002516 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002517 */
2518
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002519static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Alan Cox9e989662008-07-22 11:18:03 +01002521 int retval;
2522
2523 if (tty->ops->break_ctl == NULL)
2524 return 0;
2525
2526 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2527 retval = tty->ops->break_ctl(tty, duration);
2528 else {
2529 /* Do the work ourselves */
2530 if (tty_write_lock(tty, 0) < 0)
2531 return -EINTR;
2532 retval = tty->ops->break_ctl(tty, -1);
2533 if (retval)
2534 goto out;
2535 if (!signal_pending(current))
2536 msleep_interruptible(duration);
2537 retval = tty->ops->break_ctl(tty, 0);
2538out:
2539 tty_write_unlock(tty);
2540 if (signal_pending(current))
2541 retval = -EINTR;
2542 }
2543 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544}
2545
Alan Coxaf9b8972006-08-27 01:24:01 -07002546/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002547 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002548 * @tty: tty device
2549 * @file: user file pointer
2550 * @p: pointer to result
2551 *
2552 * Obtain the modem status bits from the tty driver if the feature
2553 * is supported. Return -EINVAL if it is not available.
2554 *
2555 * Locking: none (up to the driver)
2556 */
2557
Alan Cox60b33c12011-02-14 16:26:14 +00002558static int tty_tiocmget(struct tty_struct *tty, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
2560 int retval = -EINVAL;
2561
Alan Coxf34d7a52008-04-30 00:54:13 -07002562 if (tty->ops->tiocmget) {
Alan Cox60b33c12011-02-14 16:26:14 +00002563 retval = tty->ops->tiocmget(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 if (retval >= 0)
2566 retval = put_user(retval, p);
2567 }
2568 return retval;
2569}
2570
Alan Coxaf9b8972006-08-27 01:24:01 -07002571/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002572 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002573 * @tty: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002574 * @cmd: command - clear bits, set bits or set all
2575 * @p: pointer to desired bits
2576 *
2577 * Set the modem status bits from the tty driver if the feature
2578 * is supported. Return -EINVAL if it is not available.
2579 *
2580 * Locking: none (up to the driver)
2581 */
2582
Alan Cox20b9d172011-02-14 16:26:50 +00002583static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 unsigned __user *p)
2585{
Alan Coxae677512008-07-16 21:56:54 +01002586 int retval;
2587 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Alan Coxae677512008-07-16 21:56:54 +01002589 if (tty->ops->tiocmset == NULL)
2590 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Alan Coxae677512008-07-16 21:56:54 +01002592 retval = get_user(val, p);
2593 if (retval)
2594 return retval;
2595 set = clear = 0;
2596 switch (cmd) {
2597 case TIOCMBIS:
2598 set = val;
2599 break;
2600 case TIOCMBIC:
2601 clear = val;
2602 break;
2603 case TIOCMSET:
2604 set = val;
2605 clear = ~val;
2606 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
Alan Coxae677512008-07-16 21:56:54 +01002608 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2609 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
Alan Cox20b9d172011-02-14 16:26:50 +00002610 return tty->ops->tiocmset(tty, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
Alan Coxd281da72010-09-16 18:21:24 +01002613static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2614{
2615 int retval = -EINVAL;
2616 struct serial_icounter_struct icount;
2617 memset(&icount, 0, sizeof(icount));
2618 if (tty->ops->get_icount)
2619 retval = tty->ops->get_icount(tty, &icount);
2620 if (retval != 0)
2621 return retval;
2622 if (copy_to_user(arg, &icount, sizeof(icount)))
2623 return -EFAULT;
2624 return 0;
2625}
2626
Alan Coxe8b70e72009-06-11 12:48:02 +01002627struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2628{
2629 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2630 tty->driver->subtype == PTY_TYPE_MASTER)
2631 tty = tty->link;
2632 return tty;
2633}
2634EXPORT_SYMBOL(tty_pair_get_tty);
2635
2636struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
2637{
2638 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2639 tty->driver->subtype == PTY_TYPE_MASTER)
2640 return tty;
2641 return tty->link;
2642}
2643EXPORT_SYMBOL(tty_pair_get_pty);
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645/*
2646 * Split this up, as gcc can choke on it otherwise..
2647 */
Alan Cox04f378b2008-04-30 00:53:29 -07002648long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Nick Piggind996b622010-08-18 04:37:36 +10002650 struct tty_struct *tty = file_tty(file);
2651 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 void __user *p = (void __user *)arg;
2653 int retval;
2654 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002655
Al Viro6131ffa2013-02-27 16:59:05 -05002656 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 return -EINVAL;
2658
Alan Coxe8b70e72009-06-11 12:48:02 +01002659 real_tty = tty_pair_get_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
2661 /*
2662 * Factor out some common prep work
2663 */
2664 switch (cmd) {
2665 case TIOCSETD:
2666 case TIOCSBRK:
2667 case TIOCCBRK:
2668 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002669 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 retval = tty_check_change(tty);
2671 if (retval)
2672 return retval;
2673 if (cmd != TIOCCBRK) {
2674 tty_wait_until_sent(tty, 0);
2675 if (signal_pending(current))
2676 return -EINTR;
2677 }
2678 break;
2679 }
2680
Alan Cox9e989662008-07-22 11:18:03 +01002681 /*
2682 * Now do the stuff.
2683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002685 case TIOCSTI:
2686 return tiocsti(tty, p);
2687 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002688 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002689 case TIOCSWINSZ:
Alan Coxfc6f62382009-01-02 13:43:17 +00002690 return tiocswinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002691 case TIOCCONS:
2692 return real_tty != tty ? -EINVAL : tioccons(file);
2693 case FIONBIO:
2694 return fionbio(file, p);
2695 case TIOCEXCL:
2696 set_bit(TTY_EXCLUSIVE, &tty->flags);
2697 return 0;
2698 case TIOCNXCL:
2699 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2700 return 0;
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +04002701 case TIOCGEXCL:
2702 {
2703 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2704 return put_user(excl, (int __user *)p);
2705 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002706 case TIOCNOTTY:
2707 if (current->signal->tty != tty)
2708 return -ENOTTY;
2709 no_tty();
2710 return 0;
2711 case TIOCSCTTY:
2712 return tiocsctty(tty, arg);
2713 case TIOCGPGRP:
2714 return tiocgpgrp(tty, real_tty, p);
2715 case TIOCSPGRP:
2716 return tiocspgrp(tty, real_tty, p);
2717 case TIOCGSID:
2718 return tiocgsid(tty, real_tty, p);
2719 case TIOCGETD:
Alan Coxc65c9bc2009-06-11 12:50:12 +01002720 return put_user(tty->ldisc->ops->num, (int __user *)p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002721 case TIOCSETD:
2722 return tiocsetd(tty, p);
Kay Sievers3c95c982011-02-17 18:39:28 +01002723 case TIOCVHANGUP:
2724 if (!capable(CAP_SYS_ADMIN))
2725 return -EPERM;
2726 tty_vhangup(tty);
2727 return 0;
Werner Finkb7b8de02010-12-03 12:48:23 +01002728 case TIOCGDEV:
2729 {
2730 unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2731 return put_user(ret, (unsigned int __user *)p);
2732 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002733 /*
2734 * Break handling
2735 */
2736 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002737 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002738 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002739 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002740 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002741 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002742 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002743 return 0;
2744 case TCSBRK: /* SVID version: non-zero arg --> no break */
2745 /* non-zero arg means wait for all output data
2746 * to be sent (performed above) but don't send break.
2747 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002749 if (!arg)
2750 return send_break(tty, 250);
2751 return 0;
2752 case TCSBRKP: /* support for POSIX tcsendbreak() */
2753 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Alan Cox37bdfb02008-02-08 04:18:47 -08002755 case TIOCMGET:
Alan Cox60b33c12011-02-14 16:26:14 +00002756 return tty_tiocmget(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002757 case TIOCMSET:
2758 case TIOCMBIC:
2759 case TIOCMBIS:
Alan Cox20b9d172011-02-14 16:26:50 +00002760 return tty_tiocmset(tty, cmd, p);
Alan Coxd281da72010-09-16 18:21:24 +01002761 case TIOCGICOUNT:
2762 retval = tty_tiocgicount(tty, p);
2763 /* For the moment allow fall through to the old method */
2764 if (retval != -EINVAL)
2765 return retval;
2766 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002767 case TCFLSH:
2768 switch (arg) {
2769 case TCIFLUSH:
2770 case TCIOFLUSH:
2771 /* flush tty buffer and allow ldisc to process ioctl */
2772 tty_buffer_flush(tty);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002773 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002774 }
2775 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002777 if (tty->ops->ioctl) {
Alan Cox6caa76b2011-02-14 16:27:22 +00002778 retval = (tty->ops->ioctl)(tty, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 if (retval != -ENOIOCTLCMD)
2780 return retval;
2781 }
2782 ld = tty_ldisc_ref_wait(tty);
2783 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002784 if (ld->ops->ioctl) {
2785 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (retval == -ENOIOCTLCMD)
Wanlong Gaobbb63c52012-08-27 15:23:12 +08002787 retval = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
2789 tty_ldisc_deref(ld);
2790 return retval;
2791}
2792
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002793#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08002794static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002795 unsigned long arg)
2796{
Nick Piggind996b622010-08-18 04:37:36 +10002797 struct tty_struct *tty = file_tty(file);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002798 struct tty_ldisc *ld;
2799 int retval = -ENOIOCTLCMD;
2800
Al Viro6131ffa2013-02-27 16:59:05 -05002801 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002802 return -EINVAL;
2803
Alan Coxf34d7a52008-04-30 00:54:13 -07002804 if (tty->ops->compat_ioctl) {
Alan Cox6caa76b2011-02-14 16:27:22 +00002805 retval = (tty->ops->compat_ioctl)(tty, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002806 if (retval != -ENOIOCTLCMD)
2807 return retval;
2808 }
2809
2810 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002811 if (ld->ops->compat_ioctl)
2812 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Thomas Meyer8193c422011-10-05 23:13:13 +02002813 else
2814 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002815 tty_ldisc_deref(ld);
2816
2817 return retval;
2818}
2819#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Al Viroc3c073f2012-08-21 22:32:06 -04002821static int this_tty(const void *t, struct file *file, unsigned fd)
2822{
2823 if (likely(file->f_op->read != tty_read))
2824 return 0;
2825 return file_tty(file) != t ? 0 : fd + 1;
2826}
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828/*
2829 * This implements the "Secure Attention Key" --- the idea is to
2830 * prevent trojan horses by killing all processes associated with this
2831 * tty when the user hits the "Secure Attention Key". Required for
2832 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08002833 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 * This code could be nicer; ideally it should send a HUP, wait a few
2835 * seconds, then send a INT, and then a KILL signal. But you then
2836 * have to coordinate with the init process, since all processes associated
2837 * with the current tty must be dead before the new getty is allowed
2838 * to spawn.
2839 *
2840 * Now, if it would be correct ;-/ The current code has a nasty hole -
2841 * it doesn't catch files in flight. We may send the descriptor to ourselves
2842 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2843 *
2844 * Nasty bug: do_SAK is being called in interrupt context. This can
2845 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2846 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002847void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849#ifdef TTY_SOFT_SAK
2850 tty_hangup(tty);
2851#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08002852 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002853 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -08002855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (!tty)
2857 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002858 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08002859
Dan Carpenterb3f13de2006-12-13 00:35:09 -08002860 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Alan Coxf34d7a52008-04-30 00:54:13 -07002862 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08002863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002865 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002866 do_each_pid_task(session, PIDTYPE_SID, p) {
Eric W. Biederman652486f2006-03-28 16:11:02 -08002867 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07002868 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002869 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002870 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002871 } while_each_pid_task(session, PIDTYPE_SID, p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002872 /* Now kill any processes that happen to have the
2873 * tty open.
2874 */
2875 do_each_thread(g, p) {
2876 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07002878 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002879 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 send_sig(SIGKILL, p, 1);
2881 continue;
2882 }
2883 task_lock(p);
Al Viroc3c073f2012-08-21 22:32:06 -04002884 i = iterate_fd(p->files, 0, this_tty, tty);
2885 if (i != 0) {
2886 printk(KERN_NOTICE "SAK: killed process %d"
2887 " (%s): fd#%d opened to the tty\n",
2888 task_pid_nr(p), p->comm, i - 1);
2889 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
2891 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002892 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 read_unlock(&tasklist_lock);
2894#endif
2895}
2896
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002897static void do_SAK_work(struct work_struct *work)
2898{
2899 struct tty_struct *tty =
2900 container_of(work, struct tty_struct, SAK_work);
2901 __do_SAK(tty);
2902}
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904/*
2905 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2906 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2907 * the values which we write to it will be identical to the values which it
2908 * already has. --akpm
2909 */
2910void do_SAK(struct tty_struct *tty)
2911{
2912 if (!tty)
2913 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 schedule_work(&tty->SAK_work);
2915}
2916
2917EXPORT_SYMBOL(do_SAK);
2918
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08002919static int dev_match_devt(struct device *dev, const void *data)
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002920{
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08002921 const dev_t *devt = data;
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002922 return dev->devt == *devt;
2923}
2924
2925/* Must put_device() after it's unused! */
2926static struct device *tty_get_device(struct tty_struct *tty)
2927{
2928 dev_t devt = tty_devnum(tty);
2929 return class_find_device(tty_class, NULL, &devt, dev_match_devt);
2930}
2931
2932
Alan Coxaf9b8972006-08-27 01:24:01 -07002933/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002934 * initialize_tty_struct
2935 * @tty: tty to initialize
2936 *
2937 * This subroutine initializes a tty structure that has been newly
2938 * allocated.
2939 *
2940 * Locking: none - tty in question must not be exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002942
Alan Coxbf970ee2008-10-13 10:42:39 +01002943void initialize_tty_struct(struct tty_struct *tty,
2944 struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
2946 memset(tty, 0, sizeof(struct tty_struct));
Alan Cox9c9f4de2008-10-13 10:37:26 +01002947 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 tty->magic = TTY_MAGIC;
Alan Cox01e1abb2008-07-22 11:16:55 +01002949 tty_ldisc_init(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002950 tty->session = NULL;
2951 tty->pgrp = NULL;
Alan Cox89c8d912012-08-08 16:30:13 +01002952 mutex_init(&tty->legacy_mutex);
Arjan van de Ven5785c952006-09-29 02:00:43 -07002953 mutex_init(&tty->termios_mutex);
Alan Coxc65c9bc2009-06-11 12:50:12 +01002954 mutex_init(&tty->ldisc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 init_waitqueue_head(&tty->write_wait);
2956 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00002957 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08002958 mutex_init(&tty->atomic_write_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07002959 spin_lock_init(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002961 INIT_WORK(&tty->SAK_work, do_SAK_work);
Alan Coxbf970ee2008-10-13 10:42:39 +01002962
2963 tty->driver = driver;
2964 tty->ops = driver->ops;
2965 tty->index = idx;
2966 tty_line_name(driver, idx, tty->name);
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002967 tty->dev = tty_get_device(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968}
2969
Alan Coxf34d7a52008-04-30 00:54:13 -07002970/**
Jiri Slaby67166712011-03-23 10:48:35 +01002971 * deinitialize_tty_struct
2972 * @tty: tty to deinitialize
2973 *
2974 * This subroutine deinitializes a tty structure that has been newly
2975 * allocated but tty_release cannot be called on that yet.
2976 *
2977 * Locking: none - tty in question must not be exposed at this point
2978 */
2979void deinitialize_tty_struct(struct tty_struct *tty)
2980{
2981 tty_ldisc_deinit(tty);
2982}
2983
2984/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002985 * tty_put_char - write one character to a tty
2986 * @tty: tty
2987 * @ch: character
2988 *
2989 * Write one byte to the tty using the provided put_char method
2990 * if present. Returns the number of characters successfully output.
2991 *
2992 * Note: the specific put_char operation in the driver layer may go
2993 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002995
Alan Coxf34d7a52008-04-30 00:54:13 -07002996int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Alan Coxf34d7a52008-04-30 00:54:13 -07002998 if (tty->ops->put_char)
2999 return tty->ops->put_char(tty, ch);
3000 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
Alan Coxf34d7a52008-04-30 00:54:13 -07003002EXPORT_SYMBOL_GPL(tty_put_char);
3003
Alan Coxd81ed102008-10-13 10:41:42 +01003004struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003006static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
3007 unsigned int index, unsigned int count)
3008{
3009 /* init here, since reused cdevs cause crashes */
3010 cdev_init(&driver->cdevs[index], &tty_fops);
3011 driver->cdevs[index].owner = driver->owner;
3012 return cdev_add(&driver->cdevs[index], dev, count);
3013}
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003016 * tty_register_device - register a tty device
3017 * @driver: the tty driver that describes the tty device
3018 * @index: the index in the tty driver for this tty device
3019 * @device: a struct device that is associated with this tty device.
3020 * This field is optional, if there is no known struct device
3021 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003023 * Returns a pointer to the struct device for this tty device
3024 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003025 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003026 * This call is required to be made to register an individual tty device
3027 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3028 * that bit is not set, this function should not be called by a tty
3029 * driver.
3030 *
3031 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003033
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003034struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3035 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003037 return tty_register_device_attr(driver, index, device, NULL, NULL);
3038}
3039EXPORT_SYMBOL(tty_register_device);
3040
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003041static void tty_device_create_release(struct device *dev)
3042{
3043 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3044 kfree(dev);
3045}
3046
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003047/**
3048 * tty_register_device_attr - register a tty device
3049 * @driver: the tty driver that describes the tty device
3050 * @index: the index in the tty driver for this tty device
3051 * @device: a struct device that is associated with this tty device.
3052 * This field is optional, if there is no known struct device
3053 * for this tty device it can be set to NULL safely.
3054 * @drvdata: Driver data to be set to device.
3055 * @attr_grp: Attribute group to be set on device.
3056 *
3057 * Returns a pointer to the struct device for this tty device
3058 * (or ERR_PTR(-EFOO) on error).
3059 *
3060 * This call is required to be made to register an individual tty device
3061 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3062 * that bit is not set, this function should not be called by a tty
3063 * driver.
3064 *
3065 * Locking: ??
3066 */
3067struct device *tty_register_device_attr(struct tty_driver *driver,
3068 unsigned index, struct device *device,
3069 void *drvdata,
3070 const struct attribute_group **attr_grp)
3071{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 char name[64];
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003073 dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
3074 struct device *dev = NULL;
3075 int retval = -ENODEV;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003076 bool cdev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078 if (index >= driver->num) {
3079 printk(KERN_ERR "Attempt to register invalid tty line number "
3080 " (%d).\n", index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003081 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (driver->type == TTY_DRIVER_TYPE_PTY)
3085 pty_line_name(driver, index, name);
3086 else
3087 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003088
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003089 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003090 retval = tty_cdev_add(driver, devt, index, 1);
3091 if (retval)
3092 goto error;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003093 cdev = true;
3094 }
3095
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003096 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3097 if (!dev) {
3098 retval = -ENOMEM;
3099 goto error;
3100 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003101
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003102 dev->devt = devt;
3103 dev->class = tty_class;
3104 dev->parent = device;
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003105 dev->release = tty_device_create_release;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003106 dev_set_name(dev, "%s", name);
3107 dev->groups = attr_grp;
3108 dev_set_drvdata(dev, drvdata);
3109
3110 retval = device_register(dev);
3111 if (retval)
3112 goto error;
3113
3114 return dev;
3115
3116error:
3117 put_device(dev);
3118 if (cdev)
3119 cdev_del(&driver->cdevs[index]);
3120 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003122EXPORT_SYMBOL_GPL(tty_register_device_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
3124/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003125 * tty_unregister_device - unregister a tty device
3126 * @driver: the tty driver that describes the tty device
3127 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003129 * If a tty device is registered with a call to tty_register_device() then
3130 * this function must be called when the tty device is gone.
3131 *
3132 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003134
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135void tty_unregister_device(struct tty_driver *driver, unsigned index)
3136{
Alan Cox37bdfb02008-02-08 04:18:47 -08003137 device_destroy(tty_class,
3138 MKDEV(driver->major, driver->minor_start) + index);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003139 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC))
3140 cdev_del(&driver->cdevs[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142EXPORT_SYMBOL(tty_unregister_device);
3143
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003144/**
3145 * __tty_alloc_driver -- allocate tty driver
3146 * @lines: count of lines this driver can handle at most
3147 * @owner: module which is repsonsible for this driver
3148 * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
3149 *
3150 * This should not be called directly, some of the provided macros should be
3151 * used instead. Use IS_ERR and friends on @retval.
3152 */
3153struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
3154 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
3156 struct tty_driver *driver;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003157 unsigned int cdevs = 1;
Jiri Slaby16a02082012-08-08 22:26:42 +02003158 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Jiri Slaby0019b402012-08-08 22:26:43 +02003160 if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003161 return ERR_PTR(-EINVAL);
3162
Jean Delvare506eb992007-07-15 23:40:14 -07003163 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003164 if (!driver)
3165 return ERR_PTR(-ENOMEM);
3166
3167 kref_init(&driver->kref);
3168 driver->magic = TTY_DRIVER_MAGIC;
3169 driver->num = lines;
3170 driver->owner = owner;
3171 driver->flags = flags;
Jiri Slaby16a02082012-08-08 22:26:42 +02003172
3173 if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
3174 driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
3175 GFP_KERNEL);
3176 driver->termios = kcalloc(lines, sizeof(*driver->termios),
3177 GFP_KERNEL);
3178 if (!driver->ttys || !driver->termios) {
3179 err = -ENOMEM;
3180 goto err_free_all;
3181 }
3182 }
3183
3184 if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3185 driver->ports = kcalloc(lines, sizeof(*driver->ports),
3186 GFP_KERNEL);
3187 if (!driver->ports) {
3188 err = -ENOMEM;
3189 goto err_free_all;
3190 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003191 cdevs = lines;
3192 }
3193
3194 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
3195 if (!driver->cdevs) {
3196 err = -ENOMEM;
3197 goto err_free_all;
Jiri Slaby16a02082012-08-08 22:26:42 +02003198 }
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 return driver;
Jiri Slaby16a02082012-08-08 22:26:42 +02003201err_free_all:
3202 kfree(driver->ports);
3203 kfree(driver->ttys);
3204 kfree(driver->termios);
3205 kfree(driver);
3206 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003208EXPORT_SYMBOL(__tty_alloc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Alan Cox7d7b93c2008-10-13 10:42:09 +01003210static void destruct_tty_driver(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003212 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
3213 int i;
3214 struct ktermios *tp;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003215
3216 if (driver->flags & TTY_DRIVER_INSTALLED) {
3217 /*
3218 * Free the termios and termios_locked structures because
3219 * we don't want to get memory leaks when modular tty
3220 * drivers are removed from the kernel.
3221 */
3222 for (i = 0; i < driver->num; i++) {
3223 tp = driver->termios[i];
3224 if (tp) {
3225 driver->termios[i] = NULL;
3226 kfree(tp);
3227 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003228 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3229 tty_unregister_device(driver, i);
3230 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003231 proc_tty_unregister_driver(driver);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003232 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
3233 cdev_del(&driver->cdevs[0]);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003234 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003235 kfree(driver->cdevs);
Jiri Slaby04831dc2012-06-04 13:35:36 +02003236 kfree(driver->ports);
Jiri Slaby16a02082012-08-08 22:26:42 +02003237 kfree(driver->termios);
3238 kfree(driver->ttys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 kfree(driver);
3240}
3241
Alan Cox7d7b93c2008-10-13 10:42:09 +01003242void tty_driver_kref_put(struct tty_driver *driver)
3243{
3244 kref_put(&driver->kref, destruct_tty_driver);
3245}
3246EXPORT_SYMBOL(tty_driver_kref_put);
3247
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003248void tty_set_operations(struct tty_driver *driver,
3249 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Alan Coxf34d7a52008-04-30 00:54:13 -07003251 driver->ops = op;
3252};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253EXPORT_SYMBOL(tty_set_operations);
3254
Alan Cox7d7b93c2008-10-13 10:42:09 +01003255void put_tty_driver(struct tty_driver *d)
3256{
3257 tty_driver_kref_put(d);
3258}
3259EXPORT_SYMBOL(put_tty_driver);
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261/*
3262 * Called by a tty driver to register itself.
3263 */
3264int tty_register_driver(struct tty_driver *driver)
3265{
3266 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08003267 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 dev_t dev;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003269 struct device *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003272 error = alloc_chrdev_region(&dev, driver->minor_start,
3273 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if (!error) {
3275 driver->major = MAJOR(dev);
3276 driver->minor_start = MINOR(dev);
3277 }
3278 } else {
3279 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01003280 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 }
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003282 if (error < 0)
Jiri Slaby16a02082012-08-08 22:26:42 +02003283 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003285 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
3286 error = tty_cdev_add(driver, dev, 0, driver->num);
3287 if (error)
3288 goto err_unreg_char;
3289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003291 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003293 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003294
3295 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003296 for (i = 0; i < driver->num; i++) {
3297 d = tty_register_device(driver, i, NULL);
3298 if (IS_ERR(d)) {
3299 error = PTR_ERR(d);
Jiri Slaby16a02082012-08-08 22:26:42 +02003300 goto err_unreg_devs;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003301 }
3302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
3304 proc_tty_register_driver(driver);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003305 driver->flags |= TTY_DRIVER_INSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return 0;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003307
Jiri Slaby16a02082012-08-08 22:26:42 +02003308err_unreg_devs:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003309 for (i--; i >= 0; i--)
3310 tty_unregister_device(driver, i);
3311
3312 mutex_lock(&tty_mutex);
3313 list_del(&driver->tty_drivers);
3314 mutex_unlock(&tty_mutex);
3315
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003316err_unreg_char:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003317 unregister_chrdev_region(dev, driver->num);
Jiri Slaby16a02082012-08-08 22:26:42 +02003318err:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003319 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321EXPORT_SYMBOL(tty_register_driver);
3322
3323/*
3324 * Called by a tty driver to unregister itself.
3325 */
3326int tty_unregister_driver(struct tty_driver *driver)
3327{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003328#if 0
3329 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (driver->refcount)
3331 return -EBUSY;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003332#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3334 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003335 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003337 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 return 0;
3339}
Alan Cox7d7b93c2008-10-13 10:42:09 +01003340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341EXPORT_SYMBOL(tty_unregister_driver);
3342
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003343dev_t tty_devnum(struct tty_struct *tty)
3344{
3345 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3346}
3347EXPORT_SYMBOL(tty_devnum);
3348
3349void proc_clear_tty(struct task_struct *p)
3350{
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003351 unsigned long flags;
Alan Cox9c9f4de2008-10-13 10:37:26 +01003352 struct tty_struct *tty;
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003353 spin_lock_irqsave(&p->sighand->siglock, flags);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003354 tty = p->signal->tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003355 p->signal->tty = NULL;
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003356 spin_unlock_irqrestore(&p->sighand->siglock, flags);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003357 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003358}
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003359
Alan Cox47f86832008-04-30 00:53:30 -07003360/* Called under the sighand lock */
3361
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003362static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003363{
3364 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -07003365 unsigned long flags;
3366 /* We should not have a session or pgrp to put here but.... */
3367 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -06003368 put_pid(tty->session);
3369 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003370 tty->pgrp = get_pid(task_pgrp(tsk));
Alan Cox47f86832008-04-30 00:53:30 -07003371 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3372 tty->session = get_pid(task_session(tsk));
Alan Cox9c9f4de2008-10-13 10:37:26 +01003373 if (tsk->signal->tty) {
3374 printk(KERN_DEBUG "tty not NULL!!\n");
3375 tty_kref_put(tsk->signal->tty);
3376 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003377 }
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003378 put_pid(tsk->signal->tty_old_pgrp);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003379 tsk->signal->tty = tty_kref_get(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003380 tsk->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003381}
3382
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07003383static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003384{
3385 spin_lock_irq(&tsk->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003386 __proc_set_tty(tsk, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003387 spin_unlock_irq(&tsk->sighand->siglock);
3388}
3389
3390struct tty_struct *get_current_tty(void)
3391{
3392 struct tty_struct *tty;
Alan Cox934e6eb2008-10-13 10:40:43 +01003393 unsigned long flags;
3394
3395 spin_lock_irqsave(&current->sighand->siglock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +01003396 tty = tty_kref_get(current->signal->tty);
Alan Cox934e6eb2008-10-13 10:40:43 +01003397 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003398 return tty;
3399}
Heiko Carstensa311f742006-12-13 00:33:41 -08003400EXPORT_SYMBOL_GPL(get_current_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Alan Coxd81ed102008-10-13 10:41:42 +01003402void tty_default_fops(struct file_operations *fops)
3403{
3404 *fops = tty_fops;
3405}
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407/*
3408 * Initialize the console device. This is called *early*, so
3409 * we can't necessarily depend on lots of kernel help here.
3410 * Just do some early initializations, and do the complex setup
3411 * later.
3412 */
3413void __init console_init(void)
3414{
3415 initcall_t *call;
3416
3417 /* Setup the default TTY line discipline. */
Alan Cox01e1abb2008-07-22 11:16:55 +01003418 tty_ldisc_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
3420 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08003421 * set up the console device so that later boot sequences can
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 * inform about problems etc..
3423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 call = __con_initcall_start;
3425 while (call < __con_initcall_end) {
3426 (*call)();
3427 call++;
3428 }
3429}
3430
Al Viro2c9ede52011-07-23 20:24:48 -04003431static char *tty_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +02003432{
3433 if (!mode)
3434 return NULL;
3435 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3436 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3437 *mode = 0666;
3438 return NULL;
3439}
3440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441static int __init tty_class_init(void)
3442{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003443 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 if (IS_ERR(tty_class))
3445 return PTR_ERR(tty_class);
Kay Sieverse454cea2009-09-18 23:01:12 +02003446 tty_class->devnode = tty_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 return 0;
3448}
3449
3450postcore_initcall(tty_class_init);
3451
3452/* 3/2004 jmc: why do these devices exist? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453static struct cdev tty_cdev, console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Kay Sieversfbc92a32010-12-01 18:51:05 +01003455static ssize_t show_cons_active(struct device *dev,
3456 struct device_attribute *attr, char *buf)
3457{
3458 struct console *cs[16];
3459 int i = 0;
3460 struct console *c;
3461 ssize_t count = 0;
3462
Torben Hohnac751ef2011-01-25 15:07:35 -08003463 console_lock();
Kay Sieversa2a6a822011-01-09 16:39:14 +01003464 for_each_console(c) {
Kay Sieversfbc92a32010-12-01 18:51:05 +01003465 if (!c->device)
3466 continue;
3467 if (!c->write)
3468 continue;
3469 if ((c->flags & CON_ENABLED) == 0)
3470 continue;
3471 cs[i++] = c;
3472 if (i >= ARRAY_SIZE(cs))
3473 break;
3474 }
3475 while (i--)
3476 count += sprintf(buf + count, "%s%d%c",
3477 cs[i]->name, cs[i]->index, i ? ' ':'\n');
Torben Hohnac751ef2011-01-25 15:07:35 -08003478 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01003479
3480 return count;
3481}
3482static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3483
3484static struct device *consdev;
3485
3486void console_sysfs_notify(void)
3487{
3488 if (consdev)
3489 sysfs_notify(&consdev->kobj, NULL, "active");
3490}
3491
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492/*
3493 * Ok, now we can initialize the rest of the tty devices and can count
3494 * on memory allocations, interrupts etc..
3495 */
David Howells31d1d482010-08-06 16:34:43 +01003496int __init tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
3498 cdev_init(&tty_cdev, &tty_fops);
3499 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3500 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3501 panic("Couldn't register /dev/tty driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003502 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
3504 cdev_init(&console_cdev, &console_fops);
3505 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3506 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3507 panic("Couldn't register /dev/console driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003508 consdev = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003509 "console");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003510 if (IS_ERR(consdev))
3511 consdev = NULL;
3512 else
Kay Sieversa2a6a822011-01-09 16:39:14 +01003513 WARN_ON(device_create_file(consdev, &dev_attr_active) < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515#ifdef CONFIG_VT
Alan Coxd81ed102008-10-13 10:41:42 +01003516 vty_init(&console_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517#endif
3518 return 0;
3519}
David Howells31d1d482010-08-06 16:34:43 +01003520