blob: 706c23b9cb959856b98913283750859afb47f4f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 */
4
5/*
6 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
7 * or rs-channels. It also implements echoing, cooked mode etc.
8 *
9 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
10 *
11 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
12 * tty_struct and tty_queue structures. Previously there was an array
13 * of 256 tty_struct's which was statically allocated, and the
14 * tty_queue structures were allocated at boot time. Both are now
15 * dynamically allocated only when the tty is open.
16 *
17 * Also restructured routines so that there is more of a separation
18 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
19 * the low-level tty routines (serial.c, pty.c, console.c). This
Alan Cox37bdfb02008-02-08 04:18:47 -080020 * makes for cleaner and more compact code. -TYT, 9/17/92
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
23 * which can be dynamically activated and de-activated by the line
24 * discipline handling modules (like SLIP).
25 *
26 * NOTE: pay no attention to the line discipline code (yet); its
27 * interface is still subject to change in this version...
28 * -- TYT, 1/31/92
29 *
30 * Added functionality to the OPOST tty handling. No delays, but all
31 * other bits should be there.
32 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
33 *
34 * Rewrote canonical mode and added more termios flags.
35 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
36 *
37 * Reorganized FASYNC support so mouse code can share it.
38 * -- ctm@ardi.com, 9Sep95
39 *
40 * New TIOCLINUX variants added.
41 * -- mj@k332.feld.cvut.cz, 19-Nov-95
Alan Cox37bdfb02008-02-08 04:18:47 -080042 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * Restrict vt switching via ioctl()
44 * -- grif@cs.ucr.edu, 5-Dec-95
45 *
46 * Move console and virtual terminal code to more appropriate files,
47 * implement CONFIG_VT and generalize console device interface.
48 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
49 *
Alan Coxd81ed102008-10-13 10:41:42 +010050 * Rewrote tty_init_dev and tty_release_dev to eliminate races.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * -- Bill Hawes <whawes@star.net>, June 97
52 *
53 * Added devfs support.
54 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
55 *
56 * Added support for a Unix98-style ptmx device.
57 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
58 *
59 * Reduced memory usage for older ARM systems
60 * -- Russell King <rmk@arm.linux.org.uk>
61 *
62 * Move do_SAK() into process context. Less stack use in devfs functions.
Alan Cox37bdfb02008-02-08 04:18:47 -080063 * alloc_tty_struct() always uses kmalloc()
64 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/types.h>
68#include <linux/major.h>
69#include <linux/errno.h>
70#include <linux/signal.h>
71#include <linux/fcntl.h>
72#include <linux/sched.h>
73#include <linux/interrupt.h>
74#include <linux/tty.h>
75#include <linux/tty_driver.h>
76#include <linux/tty_flip.h>
77#include <linux/devpts_fs.h>
78#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040079#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/console.h>
81#include <linux/timer.h>
82#include <linux/ctype.h>
83#include <linux/kd.h>
84#include <linux/mm.h>
85#include <linux/string.h>
86#include <linux/slab.h>
87#include <linux/poll.h>
88#include <linux/proc_fs.h>
89#include <linux/init.h>
90#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/wait.h>
93#include <linux/bitops.h>
Domen Puncerb20f3ae2005-06-25 14:58:42 -070094#include <linux/delay.h>
Alan Coxa352def2008-07-16 21:53:12 +010095#include <linux/seq_file.h>
Alan Coxd281da72010-09-16 18:21:24 +010096#include <linux/serial.h>
Manuel Zerpies5a3c6b22011-06-16 14:07:22 +020097#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Alan Coxa352def2008-07-16 21:53:12 +010099#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101#include <linux/kbd_kern.h>
102#include <linux/vt_kern.h>
103#include <linux/selection.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105#include <linux/kmod.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700106#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108#undef TTY_DEBUG_HANGUP
109
110#define TTY_PARANOIA_CHECK 1
111#define CHECK_TTY_COUNT 1
112
Alan Coxedc6afc2006-12-08 02:38:44 -0800113struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .c_iflag = ICRNL | IXON,
115 .c_oflag = OPOST | ONLCR,
116 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
117 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
118 ECHOCTL | ECHOKE | IEXTEN,
Alan Coxedc6afc2006-12-08 02:38:44 -0800119 .c_cc = INIT_C_CC,
120 .c_ispeed = 38400,
121 .c_ospeed = 38400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
124EXPORT_SYMBOL(tty_std_termios);
125
126/* This list gets poked at by procfs and various bits of boot up code. This
127 could do with some rationalisation such as pulling the tty proc function
128 into this file */
Alan Cox37bdfb02008-02-08 04:18:47 -0800129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130LIST_HEAD(tty_drivers); /* linked list of tty drivers */
131
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800132/* Mutex to protect creating and releasing a tty. This is shared with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 vt.c for deeply disgusting hack reasons */
Ingo Molnar70522e12006-03-23 03:00:31 -0800134DEFINE_MUTEX(tty_mutex);
Alan Coxde2a84f2006-09-29 02:00:57 -0700135EXPORT_SYMBOL(tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Nick Pigginee2ffa02010-08-18 04:37:35 +1000137/* Spinlock to protect the tty->tty_files list */
138DEFINE_SPINLOCK(tty_files_lock);
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
141static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
Alan Cox37bdfb02008-02-08 04:18:47 -0800142ssize_t redirected_tty_write(struct file *, const char __user *,
143 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static unsigned int tty_poll(struct file *, poll_table *);
145static int tty_open(struct inode *, struct file *);
Alan Cox04f378b2008-04-30 00:53:29 -0700146long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700147#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -0800148static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700149 unsigned long arg);
150#else
151#define tty_compat_ioctl NULL
152#endif
Arnd Bergmannec79d602010-06-01 22:53:01 +0200153static int __tty_fasync(int fd, struct file *filp, int on);
Alan Cox37bdfb02008-02-08 04:18:47 -0800154static int tty_fasync(int fd, struct file *filp, int on);
Christoph Hellwigd5698c22007-02-10 01:46:46 -0800155static void release_tty(struct tty_struct *tty, int idx);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700156static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700157static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Alan Coxaf9b8972006-08-27 01:24:01 -0700159/**
160 * alloc_tty_struct - allocate a tty object
161 *
162 * Return a new empty tty structure. The data fields have not
163 * been initialized in any way but has been zeroed
164 *
165 * Locking: none
Alan Coxaf9b8972006-08-27 01:24:01 -0700166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Alan Coxbf970ee2008-10-13 10:42:39 +0100168struct tty_struct *alloc_tty_struct(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Alan Cox1266b1e2006-09-29 02:00:40 -0700170 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Alan Coxaf9b8972006-08-27 01:24:01 -0700173/**
174 * free_tty_struct - free a disused tty
175 * @tty: tty struct to free
176 *
177 * Free the write buffers, tty queue and tty memory itself.
178 *
179 * Locking: none. Must be called after tty is definitely unused
180 */
181
Alan Coxbf970ee2008-10-13 10:42:39 +0100182void free_tty_struct(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Dan Carpenterdc6802a2012-07-24 12:52:04 +0100184 if (!tty)
185 return;
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +0400186 if (tty->dev)
187 put_device(tty->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kfree(tty->write_buf);
Alan Cox89c8d912012-08-08 16:30:13 +0100189 tty->magic = 0xDEADDEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 kfree(tty);
191}
192
Nick Piggind996b622010-08-18 04:37:36 +1000193static inline struct tty_struct *file_tty(struct file *file)
194{
195 return ((struct tty_file_private *)file->private_data)->tty;
196}
197
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200198int tty_alloc_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000199{
200 struct tty_file_private *priv;
201
Pekka Enbergf573bd12010-08-24 07:48:34 +0300202 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
203 if (!priv)
204 return -ENOMEM;
Nick Piggind996b622010-08-18 04:37:36 +1000205
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200206 file->private_data = priv;
207
208 return 0;
209}
210
211/* Associate a new file with the tty structure */
212void tty_add_file(struct tty_struct *tty, struct file *file)
213{
214 struct tty_file_private *priv = file->private_data;
215
Nick Piggind996b622010-08-18 04:37:36 +1000216 priv->tty = tty;
217 priv->file = file;
Nick Piggind996b622010-08-18 04:37:36 +1000218
219 spin_lock(&tty_files_lock);
220 list_add(&priv->list, &tty->tty_files);
221 spin_unlock(&tty_files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200222}
Pekka Enbergf573bd12010-08-24 07:48:34 +0300223
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200224/**
225 * tty_free_file - free file->private_data
226 *
227 * This shall be used only for fail path handling when tty_add_file was not
228 * called yet.
229 */
230void tty_free_file(struct file *file)
231{
232 struct tty_file_private *priv = file->private_data;
233
234 file->private_data = NULL;
235 kfree(priv);
Nick Piggind996b622010-08-18 04:37:36 +1000236}
237
238/* Delete file from its tty */
Josh Triplett2520e272012-11-18 21:27:47 -0800239static void tty_del_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000240{
241 struct tty_file_private *priv = file->private_data;
242
243 spin_lock(&tty_files_lock);
244 list_del(&priv->list);
245 spin_unlock(&tty_files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200246 tty_free_file(file);
Nick Piggind996b622010-08-18 04:37:36 +1000247}
248
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
251
Alan Coxaf9b8972006-08-27 01:24:01 -0700252/**
253 * tty_name - return tty naming
254 * @tty: tty structure
255 * @buf: buffer for output
256 *
257 * Convert a tty structure into a name. The name reflects the kernel
258 * naming policy and if udev is in use may not reflect user space
259 *
260 * Locking: none
261 */
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263char *tty_name(struct tty_struct *tty, char *buf)
264{
265 if (!tty) /* Hmm. NULL pointer. That's fun. */
266 strcpy(buf, "NULL tty");
267 else
268 strcpy(buf, tty->name);
269 return buf;
270}
271
272EXPORT_SYMBOL(tty_name);
273
Andrew Mortond769a662005-05-05 16:15:50 -0700274int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 const char *routine)
276{
277#ifdef TTY_PARANOIA_CHECK
278 if (!tty) {
279 printk(KERN_WARNING
280 "null TTY for (%d:%d) in %s\n",
281 imajor(inode), iminor(inode), routine);
282 return 1;
283 }
284 if (tty->magic != TTY_MAGIC) {
285 printk(KERN_WARNING
286 "bad magic number for tty struct (%d:%d) in %s\n",
287 imajor(inode), iminor(inode), routine);
288 return 1;
289 }
290#endif
291 return 0;
292}
293
294static int check_tty_count(struct tty_struct *tty, const char *routine)
295{
296#ifdef CHECK_TTY_COUNT
297 struct list_head *p;
298 int count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800299
Nick Pigginee2ffa02010-08-18 04:37:35 +1000300 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 list_for_each(p, &tty->tty_files) {
302 count++;
303 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000304 spin_unlock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
306 tty->driver->subtype == PTY_TYPE_SLAVE &&
307 tty->link && tty->link->count)
308 count++;
309 if (tty->count != count) {
310 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
311 "!= #fd's(%d) in %s\n",
312 tty->name, tty->count, count, routine);
313 return count;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#endif
316 return 0;
317}
318
Alan Coxaf9b8972006-08-27 01:24:01 -0700319/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700320 * get_tty_driver - find device of a tty
321 * @dev_t: device identifier
322 * @index: returns the index of the tty
323 *
324 * This routine returns a tty driver structure, given a device number
325 * and also passes back the index number.
326 *
327 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static struct tty_driver *get_tty_driver(dev_t device, int *index)
331{
332 struct tty_driver *p;
333
334 list_for_each_entry(p, &tty_drivers, tty_drivers) {
335 dev_t base = MKDEV(p->major, p->minor_start);
336 if (device < base || device >= base + p->num)
337 continue;
338 *index = device - base;
Alan Cox7d7b93c2008-10-13 10:42:09 +0100339 return tty_driver_kref_get(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 return NULL;
342}
343
Jason Wesself2d937f2008-04-17 20:05:37 +0200344#ifdef CONFIG_CONSOLE_POLL
345
346/**
347 * tty_find_polling_driver - find device of a polled tty
348 * @name: name string to match
349 * @line: pointer to resulting tty line nr
350 *
351 * This routine returns a tty driver structure, given a name
352 * and the condition that the tty driver is capable of polled
353 * operation.
354 */
355struct tty_driver *tty_find_polling_driver(char *name, int *line)
356{
357 struct tty_driver *p, *res = NULL;
358 int tty_line = 0;
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500359 int len;
Alan Cox5f0878a2009-06-11 12:46:41 +0100360 char *str, *stp;
Jason Wesself2d937f2008-04-17 20:05:37 +0200361
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500362 for (str = name; *str; str++)
363 if ((*str >= '0' && *str <= '9') || *str == ',')
364 break;
365 if (!*str)
366 return NULL;
367
368 len = str - name;
369 tty_line = simple_strtoul(str, &str, 10);
370
Jason Wesself2d937f2008-04-17 20:05:37 +0200371 mutex_lock(&tty_mutex);
372 /* Search through the tty devices to look for a match */
373 list_for_each_entry(p, &tty_drivers, tty_drivers) {
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500374 if (strncmp(name, p->name, len) != 0)
375 continue;
Alan Cox5f0878a2009-06-11 12:46:41 +0100376 stp = str;
377 if (*stp == ',')
378 stp++;
379 if (*stp == '\0')
380 stp = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +0200381
Nathael Pajani6eb68d62010-09-02 16:06:16 +0200382 if (tty_line >= 0 && tty_line < p->num && p->ops &&
Alan Cox5f0878a2009-06-11 12:46:41 +0100383 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
Alan Cox7d7b93c2008-10-13 10:42:09 +0100384 res = tty_driver_kref_get(p);
Jason Wesself2d937f2008-04-17 20:05:37 +0200385 *line = tty_line;
386 break;
387 }
388 }
389 mutex_unlock(&tty_mutex);
390
391 return res;
392}
393EXPORT_SYMBOL_GPL(tty_find_polling_driver);
394#endif
395
Alan Coxaf9b8972006-08-27 01:24:01 -0700396/**
397 * tty_check_change - check for POSIX terminal changes
398 * @tty: tty to check
399 *
400 * If we try to write to, or set the state of, a terminal and we're
401 * not in the foreground, send a SIGTTOU. If the signal is blocked or
402 * ignored, go ahead and perform the operation. (POSIX 7.2)
403 *
Alan Cox978e5952008-04-30 00:53:59 -0700404 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700406
Alan Cox37bdfb02008-02-08 04:18:47 -0800407int tty_check_change(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Alan Cox47f86832008-04-30 00:53:30 -0700409 unsigned long flags;
410 int ret = 0;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (current->signal->tty != tty)
413 return 0;
Alan Cox47f86832008-04-30 00:53:30 -0700414
415 spin_lock_irqsave(&tty->ctrl_lock, flags);
416
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800417 if (!tty->pgrp) {
418 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700419 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800421 if (task_pgrp(current) == tty->pgrp)
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700422 goto out_unlock;
423 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (is_ignored(SIGTTOU))
Alan Cox47f86832008-04-30 00:53:30 -0700425 goto out;
426 if (is_current_pgrp_orphaned()) {
427 ret = -EIO;
428 goto out;
429 }
Oleg Nesterov040b6362007-06-01 00:46:53 -0700430 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
431 set_thread_flag(TIF_SIGPENDING);
Alan Cox47f86832008-04-30 00:53:30 -0700432 ret = -ERESTARTSYS;
433out:
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700434 return ret;
435out_unlock:
Alan Cox47f86832008-04-30 00:53:30 -0700436 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
437 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440EXPORT_SYMBOL(tty_check_change);
441
Alan Cox37bdfb02008-02-08 04:18:47 -0800442static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 size_t count, loff_t *ppos)
444{
445 return 0;
446}
447
Alan Cox37bdfb02008-02-08 04:18:47 -0800448static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 size_t count, loff_t *ppos)
450{
451 return -EIO;
452}
453
454/* No kernel lock held - none needed ;) */
Alan Cox37bdfb02008-02-08 04:18:47 -0800455static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
458}
459
Alan Cox04f378b2008-04-30 00:53:29 -0700460static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
461 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700462{
463 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
464}
465
Alan Cox37bdfb02008-02-08 04:18:47 -0800466static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700467 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
470}
471
Arjan van de Ven62322d22006-07-03 00:24:21 -0700472static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 .llseek = no_llseek,
474 .read = tty_read,
475 .write = tty_write,
476 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700477 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700478 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 .open = tty_open,
480 .release = tty_release,
481 .fasync = tty_fasync,
482};
483
Arjan van de Ven62322d22006-07-03 00:24:21 -0700484static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .llseek = no_llseek,
486 .read = tty_read,
487 .write = redirected_tty_write,
488 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700489 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700490 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 .open = tty_open,
492 .release = tty_release,
493 .fasync = tty_fasync,
494};
495
Arjan van de Ven62322d22006-07-03 00:24:21 -0700496static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 .llseek = no_llseek,
498 .read = hung_up_tty_read,
499 .write = hung_up_tty_write,
500 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700501 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700502 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 .release = tty_release,
504};
505
506static DEFINE_SPINLOCK(redirect_lock);
507static struct file *redirect;
508
509/**
510 * tty_wakeup - request more data
511 * @tty: terminal
512 *
513 * Internal and external helper for wakeups of tty. This function
514 * informs the line discipline if present that the driver is ready
515 * to receive more output data.
516 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518void tty_wakeup(struct tty_struct *tty)
519{
520 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -0800521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
523 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800524 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100525 if (ld->ops->write_wakeup)
526 ld->ops->write_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 tty_ldisc_deref(ld);
528 }
529 }
Davide Libenzi4b194492009-03-31 15:24:24 -0700530 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533EXPORT_SYMBOL_GPL(tty_wakeup);
534
535/**
Peter Hurleyea648a42013-03-06 07:20:53 -0500536 * tty_signal_session_leader - sends SIGHUP to session leader
537 *
538 * Send SIGHUP and SIGCONT to the session leader and its
539 * process group.
540 *
541 * Returns the number of processes in the session with this tty
542 * as their controlling terminal. This value is used to drop
543 * tty references for those processes.
544 */
545static int tty_signal_session_leader(struct tty_struct *tty)
546{
547 struct task_struct *p;
548 unsigned long flags;
549 int refs = 0;
550
551 read_lock(&tasklist_lock);
552 if (tty->session) {
553 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
554 spin_lock_irq(&p->sighand->siglock);
555 if (p->signal->tty == tty) {
556 p->signal->tty = NULL;
557 /* We defer the dereferences outside fo
558 the tasklist lock */
559 refs++;
560 }
561 if (!p->signal->leader) {
562 spin_unlock_irq(&p->sighand->siglock);
563 continue;
564 }
565 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
566 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
567 put_pid(p->signal->tty_old_pgrp); /* A noop */
568 spin_lock_irqsave(&tty->ctrl_lock, flags);
569 if (tty->pgrp)
570 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
571 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
572 spin_unlock_irq(&p->sighand->siglock);
573 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
574 }
575 read_unlock(&tasklist_lock);
576
577 return refs;
578}
579
580/**
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200581 * __tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +0000582 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -0700583 *
Kevin Cernekeeef4f5272012-12-26 20:43:41 -0800584 * This can be called by a "kworker" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -0700585 * synchronous but doesn't hold any locks, so we need to make sure we
586 * have the appropriate locks for what we're doing.
587 *
588 * The hangup event clears any pending redirections onto the hung up
589 * device. It ensures future writes will error and it does the needed
590 * line discipline hangup and signal delivery. The tty object itself
591 * remains intact.
592 *
593 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200594 * BTM
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800595 * redirect lock for undoing redirection
596 * file list lock for manipulating list of ttys
597 * tty_ldisc_lock from called functions
598 * termios_mutex resetting termios data
599 * tasklist_lock to walk task list for hangup event
600 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Josh Triplett2520e272012-11-18 21:27:47 -0800602static void __tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Alan Cox37bdfb02008-02-08 04:18:47 -0800604 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct file *filp, *f = NULL;
Nick Piggind996b622010-08-18 04:37:36 +1000606 struct tty_file_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int closecount = 0, n;
Alan Cox47f86832008-04-30 00:53:30 -0700608 unsigned long flags;
Peter Hurleyea648a42013-03-06 07:20:53 -0500609 int refs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 if (!tty)
612 return;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 spin_lock(&redirect_lock);
Nick Piggind996b622010-08-18 04:37:36 +1000616 if (redirect && file_tty(redirect) == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 f = redirect;
618 redirect = NULL;
619 }
620 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800621
Alan Cox89c8d912012-08-08 16:30:13 +0100622 tty_lock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200623
Jiri Slabyacfa7472010-11-29 10:16:54 +0100624 /* some functions below drop BTM, so we need this bit */
625 set_bit(TTY_HUPPING, &tty->flags);
626
Arnd Bergmannec79d602010-06-01 22:53:01 +0200627 /* inuse_filps is protected by the single tty lock,
628 this really needs to change if we want to flush the
629 workqueue with the lock held */
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200630 check_tty_count(tty, "tty_hangup");
Alan Cox36ba7822009-11-30 13:18:51 +0000631
Nick Pigginee2ffa02010-08-18 04:37:35 +1000632 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Nick Piggind996b622010-08-18 04:37:36 +1000634 list_for_each_entry(priv, &tty->tty_files, list) {
635 filp = priv->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (filp->f_op->write == redirected_tty_write)
637 cons_filp = filp;
638 if (filp->f_op->write != tty_write)
639 continue;
640 closecount++;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200641 __tty_fasync(-1, filp, 0); /* can't block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 filp->f_op = &hung_up_tty_fops;
643 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000644 spin_unlock(&tty_files_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800645
Jiri Slabyacfa7472010-11-29 10:16:54 +0100646 /*
647 * it drops BTM and thus races with reopen
648 * we protect the race by TTY_HUPPING
649 */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100650 tty_ldisc_hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800651
Peter Hurleyea648a42013-03-06 07:20:53 -0500652 refs = tty_signal_session_leader(tty);
653 /* Account for the p->signal references we killed */
654 while (refs--)
655 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Alan Cox47f86832008-04-30 00:53:30 -0700657 spin_lock_irqsave(&tty->ctrl_lock, flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100658 clear_bit(TTY_THROTTLED, &tty->flags);
659 clear_bit(TTY_PUSH, &tty->flags);
660 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -0600661 put_pid(tty->session);
662 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800663 tty->session = NULL;
664 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 tty->ctrl_status = 0;
Alan Cox47f86832008-04-30 00:53:30 -0700666 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /*
Alan Cox37bdfb02008-02-08 04:18:47 -0800669 * If one of the devices matches a console pointer, we
670 * cannot just call hangup() because that will cause
671 * tty->count and state->count to go out of sync.
672 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
674 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -0700675 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -0700677 tty->ops->close(tty, cons_filp);
678 } else if (tty->ops->hangup)
679 (tty->ops->hangup)(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800680 /*
681 * We don't want to have driver/ldisc interactions beyond
682 * the ones we did here. The driver layer expects no
683 * calls after ->hangup() from the ldisc side. However we
684 * can't yet guarantee all that.
685 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 set_bit(TTY_HUPPED, &tty->flags);
Jiri Slabyacfa7472010-11-29 10:16:54 +0100687 clear_bit(TTY_HUPPING, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100688 tty_ldisc_enable(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200689
Alan Cox89c8d912012-08-08 16:30:13 +0100690 tty_unlock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (f)
693 fput(f);
694}
695
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200696static void do_tty_hangup(struct work_struct *work)
697{
698 struct tty_struct *tty =
699 container_of(work, struct tty_struct, hangup_work);
700
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200701 __tty_hangup(tty);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200702}
703
Alan Coxaf9b8972006-08-27 01:24:01 -0700704/**
705 * tty_hangup - trigger a hangup event
706 * @tty: tty to hangup
707 *
708 * A carrier loss (virtual or otherwise) has occurred on this like
709 * schedule a hangup sequence to run after this event.
710 */
711
Alan Cox37bdfb02008-02-08 04:18:47 -0800712void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714#ifdef TTY_DEBUG_HANGUP
715 char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
717#endif
718 schedule_work(&tty->hangup_work);
719}
720
721EXPORT_SYMBOL(tty_hangup);
722
Alan Coxaf9b8972006-08-27 01:24:01 -0700723/**
724 * tty_vhangup - process vhangup
725 * @tty: tty to hangup
726 *
727 * The user has asked via system call for the terminal to be hung up.
728 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200729 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -0700730 */
731
Alan Cox37bdfb02008-02-08 04:18:47 -0800732void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734#ifdef TTY_DEBUG_HANGUP
735 char buf[64];
736
737 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
738#endif
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200739 __tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
Alan Cox37bdfb02008-02-08 04:18:47 -0800741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742EXPORT_SYMBOL(tty_vhangup);
743
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200744
Alan Coxaf9b8972006-08-27 01:24:01 -0700745/**
Alan Cox2cb59982008-10-13 10:40:30 +0100746 * tty_vhangup_self - process vhangup for own ctty
747 *
748 * Perform a vhangup on the current controlling tty
749 */
750
751void tty_vhangup_self(void)
752{
753 struct tty_struct *tty;
754
Alan Cox2cb59982008-10-13 10:40:30 +0100755 tty = get_current_tty();
756 if (tty) {
757 tty_vhangup(tty);
758 tty_kref_put(tty);
759 }
Alan Cox2cb59982008-10-13 10:40:30 +0100760}
761
762/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700763 * tty_hung_up_p - was tty hung up
764 * @filp: file pointer of tty
765 *
766 * Return true if the tty has been subject to a vhangup or a carrier
767 * loss
768 */
769
Alan Cox37bdfb02008-02-08 04:18:47 -0800770int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 return (filp->f_op == &hung_up_tty_fops);
773}
774
775EXPORT_SYMBOL(tty_hung_up_p);
776
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800777static void session_clear_tty(struct pid *session)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800778{
779 struct task_struct *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800780 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800781 proc_clear_tty(p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800782 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800783}
784
Alan Coxaf9b8972006-08-27 01:24:01 -0700785/**
786 * disassociate_ctty - disconnect controlling tty
787 * @on_exit: true if exiting so need to "hang up" the session
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700789 * This function is typically called only by the session leader, when
790 * it wants to disassociate itself from its controlling tty.
791 *
792 * It performs the following functions:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
794 * (2) Clears the tty from being controlling the session
795 * (3) Clears the controlling tty for all processes in the
796 * session group.
797 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700798 * The argument on_exit is set to 1 if called when a process is
799 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
800 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800801 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200802 * BTM is taken for hysterical raisins, and held when
803 * called from no_tty().
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800804 * tty_mutex is taken to protect tty
805 * ->siglock is taken to protect ->signal/->sighand
806 * tasklist_lock is taken to walk process list for sessions
807 * ->siglock is taken to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810void disassociate_ctty(int on_exit)
811{
812 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Alan Cox5ec93d12009-11-30 13:18:45 +0000814 if (!current->signal->leader)
815 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800817 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (tty) {
Jiri Slaby1411dc42011-11-09 21:33:18 +0100819 struct pid *tty_pgrp = get_pid(tty->pgrp);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200820 if (on_exit) {
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200821 if (tty->driver->type != TTY_DRIVER_TYPE_PTY)
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200822 tty_vhangup(tty);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200823 }
Alan Cox452a00d2008-10-13 10:39:13 +0100824 tty_kref_put(tty);
Jiri Slaby1411dc42011-11-09 21:33:18 +0100825 if (tty_pgrp) {
826 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
827 if (!on_exit)
828 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
829 put_pid(tty_pgrp);
830 }
Eric W. Biederman680a9672007-02-12 00:52:52 -0800831 } else if (on_exit) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800832 struct pid *old_pgrp;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800833 spin_lock_irq(&current->sighand->siglock);
834 old_pgrp = current->signal->tty_old_pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800835 current->signal->tty_old_pgrp = NULL;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800836 spin_unlock_irq(&current->sighand->siglock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800837 if (old_pgrp) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800838 kill_pgrp(old_pgrp, SIGHUP, on_exit);
839 kill_pgrp(old_pgrp, SIGCONT, on_exit);
840 put_pid(old_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800845 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700846 put_pid(current->signal->tty_old_pgrp);
Randy Dunlap23cac8d2007-02-20 13:58:05 -0800847 current->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800848 spin_unlock_irq(&current->sighand->siglock);
849
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800850 tty = get_current_tty();
851 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -0700852 unsigned long flags;
853 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800854 put_pid(tty->session);
855 put_pid(tty->pgrp);
856 tty->session = NULL;
857 tty->pgrp = NULL;
Alan Cox47f86832008-04-30 00:53:30 -0700858 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +0100859 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800860 } else {
861#ifdef TTY_DEBUG_HANGUP
862 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
863 " = NULL", tty);
864#endif
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /* Now clear signal->tty under the lock */
868 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800869 session_clear_tty(task_session(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700873/**
874 *
875 * no_tty - Ensure the current process does not have a controlling tty
876 */
877void no_tty(void)
878{
Alan Cox3af502b2012-05-03 22:21:53 +0100879 /* FIXME: Review locking here. The tty_lock never covered any race
880 between a new association and proc_clear_tty but possible we need
881 to protect against this anyway */
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700882 struct task_struct *tsk = current;
Alan Cox5ec93d12009-11-30 13:18:45 +0000883 disassociate_ctty(0);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700884 proc_clear_tty(tsk);
885}
886
Alan Coxaf9b8972006-08-27 01:24:01 -0700887
888/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200889 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700890 * @tty: tty to stop
891 *
892 * Perform flow control to the driver. For PTY/TTY pairs we
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200893 * must also propagate the TIOCKPKT status. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -0700894 * on an already stopped device and will not re-call the driver
895 * method.
896 *
897 * This functionality is used by both the line disciplines for
898 * halting incoming flow and by the driver. It may therefore be
899 * called from any context, may be under the tty atomic_write_lock
900 * but not always.
901 *
902 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700903 * Uses the tty control lock internally
Alan Coxaf9b8972006-08-27 01:24:01 -0700904 */
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906void stop_tty(struct tty_struct *tty)
907{
Alan Cox04f378b2008-04-30 00:53:29 -0700908 unsigned long flags;
909 spin_lock_irqsave(&tty->ctrl_lock, flags);
910 if (tty->stopped) {
911 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 tty->stopped = 1;
915 if (tty->link && tty->link->packet) {
916 tty->ctrl_status &= ~TIOCPKT_START;
917 tty->ctrl_status |= TIOCPKT_STOP;
Davide Libenzi4b194492009-03-31 15:24:24 -0700918 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Alan Cox04f378b2008-04-30 00:53:29 -0700920 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700921 if (tty->ops->stop)
922 (tty->ops->stop)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
925EXPORT_SYMBOL(stop_tty);
926
Alan Coxaf9b8972006-08-27 01:24:01 -0700927/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200928 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700929 * @tty: tty to start
930 *
931 * Start a tty that has been stopped if at all possible. Perform
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200932 * any necessary wakeups and propagate the TIOCPKT status. If this
Alan Coxaf9b8972006-08-27 01:24:01 -0700933 * is the tty was previous stopped and is being started then the
934 * driver start method is invoked and the line discipline woken.
935 *
936 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700937 * ctrl_lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700938 */
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940void start_tty(struct tty_struct *tty)
941{
Alan Cox04f378b2008-04-30 00:53:29 -0700942 unsigned long flags;
943 spin_lock_irqsave(&tty->ctrl_lock, flags);
944 if (!tty->stopped || tty->flow_stopped) {
945 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 tty->stopped = 0;
949 if (tty->link && tty->link->packet) {
950 tty->ctrl_status &= ~TIOCPKT_STOP;
951 tty->ctrl_status |= TIOCPKT_START;
Davide Libenzi4b194492009-03-31 15:24:24 -0700952 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Alan Cox04f378b2008-04-30 00:53:29 -0700954 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700955 if (tty->ops->start)
956 (tty->ops->start)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /* If we have a running line discipline it may need kicking */
958 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961EXPORT_SYMBOL(start_tty);
962
Alan Coxaf9b8972006-08-27 01:24:01 -0700963/**
964 * tty_read - read method for tty device files
965 * @file: pointer to tty file
966 * @buf: user buffer
967 * @count: size of user buffer
968 * @ppos: unused
969 *
970 * Perform the read system call function on this terminal device. Checks
971 * for hung up devices before calling the line discipline method.
972 *
973 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -0700974 * Locks the line discipline internally while needed. Multiple
975 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -0700976 */
977
Alan Cox37bdfb02008-02-08 04:18:47 -0800978static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 loff_t *ppos)
980{
981 int i;
Nick Piggind996b622010-08-18 04:37:36 +1000982 struct tty_struct *tty = file_tty(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 struct tty_ldisc *ld;
984
Al Viro6131ffa2013-02-27 16:59:05 -0500985 if (tty_paranoia_check(tty, file_inode(file), "tty_read"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return -EIO;
987 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
988 return -EIO;
989
990 /* We want to wait for the line discipline to sort out in this
991 situation */
992 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +0100993 if (ld->ops->read)
994 i = (ld->ops->read)(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 else
996 i = -EIO;
997 tty_ldisc_deref(ld);
Jiri Slabyb0de59b2013-02-15 15:25:05 +0100998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return i;
1000}
1001
Alan Cox9c1729d2007-07-15 23:39:43 -07001002void tty_write_unlock(struct tty_struct *tty)
Jiri Slaby83c67572011-04-20 10:43:18 +02001003 __releases(&tty->atomic_write_lock)
Alan Cox9c1729d2007-07-15 23:39:43 -07001004{
1005 mutex_unlock(&tty->atomic_write_lock);
Davide Libenzi4b194492009-03-31 15:24:24 -07001006 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Alan Cox9c1729d2007-07-15 23:39:43 -07001007}
1008
1009int tty_write_lock(struct tty_struct *tty, int ndelay)
Jiri Slaby83c67572011-04-20 10:43:18 +02001010 __acquires(&tty->atomic_write_lock)
Alan Cox9c1729d2007-07-15 23:39:43 -07001011{
1012 if (!mutex_trylock(&tty->atomic_write_lock)) {
1013 if (ndelay)
1014 return -EAGAIN;
1015 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1016 return -ERESTARTSYS;
1017 }
1018 return 0;
1019}
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021/*
1022 * Split writes up in sane blocksizes to avoid
1023 * denial-of-service type attacks
1024 */
1025static inline ssize_t do_tty_write(
1026 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1027 struct tty_struct *tty,
1028 struct file *file,
1029 const char __user *buf,
1030 size_t count)
1031{
Alan Cox9c1729d2007-07-15 23:39:43 -07001032 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -08001034
Alan Cox9c1729d2007-07-15 23:39:43 -07001035 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1036 if (ret < 0)
1037 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 /*
1040 * We chunk up writes into a temporary buffer. This
1041 * simplifies low-level drivers immensely, since they
1042 * don't have locking issues and user mode accesses.
1043 *
1044 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1045 * big chunk-size..
1046 *
1047 * The default chunk-size is 2kB, because the NTTY
1048 * layer has problems with bigger chunks. It will
1049 * claim to be able to handle more characters than
1050 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -07001051 *
1052 * FIXME: This can probably go away now except that 64K chunks
1053 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
1055 chunk = 2048;
1056 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1057 chunk = 65536;
1058 if (count < chunk)
1059 chunk = count;
1060
Ingo Molnar70522e12006-03-23 03:00:31 -08001061 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (tty->write_cnt < chunk) {
Jason Wessel402fda92008-10-13 10:45:36 +01001063 unsigned char *buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (chunk < 1024)
1066 chunk = 1024;
1067
Jason Wessel402fda92008-10-13 10:45:36 +01001068 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1069 if (!buf_chunk) {
Alan Cox9c1729d2007-07-15 23:39:43 -07001070 ret = -ENOMEM;
1071 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073 kfree(tty->write_buf);
1074 tty->write_cnt = chunk;
Jason Wessel402fda92008-10-13 10:45:36 +01001075 tty->write_buf = buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078 /* Do the write .. */
1079 for (;;) {
1080 size_t size = count;
1081 if (size > chunk)
1082 size = chunk;
1083 ret = -EFAULT;
1084 if (copy_from_user(tty->write_buf, buf, size))
1085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (ret <= 0)
1088 break;
1089 written += ret;
1090 buf += ret;
1091 count -= ret;
1092 if (!count)
1093 break;
1094 ret = -ERESTARTSYS;
1095 if (signal_pending(current))
1096 break;
1097 cond_resched();
1098 }
Jiri Slabyb0de59b2013-02-15 15:25:05 +01001099 if (written)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 ret = written;
Alan Cox9c1729d2007-07-15 23:39:43 -07001101out:
1102 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return ret;
1104}
1105
Alan Cox95f9bfc2008-10-13 10:39:23 +01001106/**
1107 * tty_write_message - write a message to a certain tty, not just the console.
1108 * @tty: the destination tty_struct
1109 * @msg: the message to write
1110 *
1111 * This is used for messages that need to be redirected to a specific tty.
1112 * We don't put it into the syslog queue right now maybe in the future if
1113 * really needed.
1114 *
Arnd Bergmannec79d602010-06-01 22:53:01 +02001115 * We must still hold the BTM and test the CLOSING flag for the moment.
Alan Cox95f9bfc2008-10-13 10:39:23 +01001116 */
1117
1118void tty_write_message(struct tty_struct *tty, char *msg)
1119{
Alan Cox95f9bfc2008-10-13 10:39:23 +01001120 if (tty) {
1121 mutex_lock(&tty->atomic_write_lock);
Alan Cox89c8d912012-08-08 16:30:13 +01001122 tty_lock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001123 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001124 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +01001125 tty->ops->write(tty, msg, strlen(msg));
Alan Coxeeb89d92009-11-30 13:18:29 +00001126 } else
Alan Cox89c8d912012-08-08 16:30:13 +01001127 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +01001128 tty_write_unlock(tty);
1129 }
Alan Cox95f9bfc2008-10-13 10:39:23 +01001130 return;
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Alan Coxaf9b8972006-08-27 01:24:01 -07001134/**
1135 * tty_write - write method for tty device file
1136 * @file: tty file pointer
1137 * @buf: user data to write
1138 * @count: bytes to write
1139 * @ppos: unused
1140 *
1141 * Write data to a tty device via the line discipline.
1142 *
1143 * Locking:
1144 * Locks the line discipline as required
1145 * Writes to the tty driver are serialized by the atomic_write_lock
1146 * and are then processed in chunks to the device. The line discipline
Joe Petersona88a69c2009-01-02 13:40:53 +00001147 * write method will not be invoked in parallel for each device.
Alan Coxaf9b8972006-08-27 01:24:01 -07001148 */
1149
Alan Cox37bdfb02008-02-08 04:18:47 -08001150static ssize_t tty_write(struct file *file, const char __user *buf,
1151 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Nick Piggind996b622010-08-18 04:37:36 +10001153 struct tty_struct *tty = file_tty(file);
1154 struct tty_ldisc *ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 ssize_t ret;
Alan Cox37bdfb02008-02-08 04:18:47 -08001156
Al Viro6131ffa2013-02-27 16:59:05 -05001157 if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001159 if (!tty || !tty->ops->write ||
Alan Cox37bdfb02008-02-08 04:18:47 -08001160 (test_bit(TTY_IO_ERROR, &tty->flags)))
1161 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001162 /* Short term debug to catch buggy drivers */
1163 if (tty->ops->write_room == NULL)
1164 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1165 tty->driver->name);
Alan Cox37bdfb02008-02-08 04:18:47 -08001166 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001167 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ret = -EIO;
1169 else
Alan Coxa352def2008-07-16 21:53:12 +01001170 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 tty_ldisc_deref(ld);
1172 return ret;
1173}
1174
Alan Cox37bdfb02008-02-08 04:18:47 -08001175ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1176 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 struct file *p = NULL;
1179
1180 spin_lock(&redirect_lock);
Al Virocb0942b2012-08-27 14:48:26 -04001181 if (redirect)
1182 p = get_file(redirect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 spin_unlock(&redirect_lock);
1184
1185 if (p) {
1186 ssize_t res;
1187 res = vfs_write(p, buf, count, &p->f_pos);
1188 fput(p);
1189 return res;
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return tty_write(file, buf, count, ppos);
1192}
1193
1194static char ptychar[] = "pqrstuvwxyzabcde";
1195
Alan Coxaf9b8972006-08-27 01:24:01 -07001196/**
1197 * pty_line_name - generate name for a pty
1198 * @driver: the tty driver in use
1199 * @index: the minor number
1200 * @p: output buffer of at least 6 bytes
1201 *
1202 * Generate a name from a driver reference and write it to the output
1203 * buffer.
1204 *
1205 * Locking: None
1206 */
1207static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 int i = index + driver->name_base;
1210 /* ->name is initialized to "ttyp", but "tty" is expected */
1211 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08001212 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1213 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Alan Coxaf9b8972006-08-27 01:24:01 -07001216/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001217 * tty_line_name - generate name for a tty
Alan Coxaf9b8972006-08-27 01:24:01 -07001218 * @driver: the tty driver in use
1219 * @index: the minor number
1220 * @p: output buffer of at least 7 bytes
1221 *
1222 * Generate a name from a driver reference and write it to the output
1223 * buffer.
1224 *
1225 * Locking: None
1226 */
1227static void tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Jiri Slaby0019b402012-08-08 22:26:43 +02001229 if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
1230 strcpy(p, driver->name);
1231 else
1232 sprintf(p, "%s%d", driver->name, index + driver->name_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Alan Cox99f1fe12008-10-13 10:42:00 +01001235/**
1236 * tty_driver_lookup_tty() - find an existing tty, if any
1237 * @driver: the driver for the tty
1238 * @idx: the minor number
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001239 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001240 * Return the tty, if found or ERR_PTR() otherwise.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001241 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001242 * Locking: tty_mutex must be held. If tty is found, the mutex must
1243 * be held until the 'fast-open' is also done. Will change once we
1244 * have refcounting in the driver and per driver locking
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001245 */
Jason Wessela47d5452009-01-02 13:43:04 +00001246static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001247 struct inode *inode, int idx)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001248{
Alan Cox99f1fe12008-10-13 10:42:00 +01001249 if (driver->ops->lookup)
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001250 return driver->ops->lookup(driver, inode, idx);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001251
Jiri Slabyd4834262012-03-05 14:51:53 +01001252 return driver->ttys[idx];
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001253}
1254
Alan Cox99f1fe12008-10-13 10:42:00 +01001255/**
Alan Coxbf970ee2008-10-13 10:42:39 +01001256 * tty_init_termios - helper for termios setup
1257 * @tty: the tty to set up
1258 *
1259 * Initialise the termios structures for this tty. Thus runs under
1260 * the tty_mutex currently so we can be relaxed about ordering.
1261 */
1262
1263int tty_init_termios(struct tty_struct *tty)
1264{
Alan Coxfe6e29f2008-10-13 10:44:08 +01001265 struct ktermios *tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001266 int idx = tty->index;
1267
Alan Cox36b3c072012-07-17 17:06:57 +01001268 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1269 tty->termios = tty->driver->init_termios;
1270 else {
1271 /* Check for lazy saved data */
1272 tp = tty->driver->termios[idx];
1273 if (tp != NULL)
1274 tty->termios = *tp;
1275 else
1276 tty->termios = tty->driver->init_termios;
Alan Coxbf970ee2008-10-13 10:42:39 +01001277 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001278 /* Compatibility until drivers always set this */
Alan Coxadc8d742012-07-14 15:31:47 +01001279 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1280 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001281 return 0;
1282}
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001283EXPORT_SYMBOL_GPL(tty_init_termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001284
Jiri Slaby66d450e2012-01-30 21:14:28 +01001285int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1286{
1287 int ret = tty_init_termios(tty);
1288 if (ret)
1289 return ret;
1290
1291 tty_driver_kref_get(driver);
1292 tty->count++;
1293 driver->ttys[tty->index] = tty;
1294 return 0;
1295}
1296EXPORT_SYMBOL_GPL(tty_standard_install);
1297
Alan Coxbf970ee2008-10-13 10:42:39 +01001298/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001299 * tty_driver_install_tty() - install a tty entry in the driver
1300 * @driver: the driver for the tty
1301 * @tty: the tty
1302 *
1303 * Install a tty object into the driver tables. The tty->index field
Alan Coxbf970ee2008-10-13 10:42:39 +01001304 * will be set by the time this is called. This method is responsible
1305 * for ensuring any need additional structures are allocated and
1306 * configured.
Alan Cox8b0a88d2008-10-13 10:42:19 +01001307 *
1308 * Locking: tty_mutex for now
1309 */
1310static int tty_driver_install_tty(struct tty_driver *driver,
1311 struct tty_struct *tty)
1312{
Jiri Slaby66d450e2012-01-30 21:14:28 +01001313 return driver->ops->install ? driver->ops->install(driver, tty) :
1314 tty_standard_install(driver, tty);
Alan Cox8b0a88d2008-10-13 10:42:19 +01001315}
1316
1317/**
1318 * tty_driver_remove_tty() - remove a tty from the driver tables
1319 * @driver: the driver for the tty
1320 * @idx: the minor number
1321 *
1322 * Remvoe a tty object from the driver tables. The tty->index field
1323 * will be set by the time this is called.
1324 *
1325 * Locking: tty_mutex for now
1326 */
Jiri Slaby24d406a2011-08-10 14:59:28 +02001327void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +01001328{
1329 if (driver->ops->remove)
1330 driver->ops->remove(driver, tty);
1331 else
1332 driver->ttys[tty->index] = NULL;
1333}
1334
1335/*
1336 * tty_reopen() - fast re-open of an open tty
1337 * @tty - the tty to open
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001338 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001339 * Return 0 on success, -errno on error.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001340 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001341 * Locking: tty_mutex must be held from the time the tty was found
1342 * till this open completes.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001343 */
Alan Cox99f1fe12008-10-13 10:42:00 +01001344static int tty_reopen(struct tty_struct *tty)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001345{
1346 struct tty_driver *driver = tty->driver;
1347
Jiri Slabye2efafb2010-11-29 10:16:53 +01001348 if (test_bit(TTY_CLOSING, &tty->flags) ||
Jiri Slabyacfa7472010-11-29 10:16:54 +01001349 test_bit(TTY_HUPPING, &tty->flags) ||
Jiri Slabye2efafb2010-11-29 10:16:53 +01001350 test_bit(TTY_LDISC_CHANGING, &tty->flags))
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001351 return -EIO;
1352
1353 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1354 driver->subtype == PTY_TYPE_MASTER) {
1355 /*
1356 * special case for PTY masters: only one open permitted,
1357 * and the slave side open count is incremented as well.
1358 */
1359 if (tty->count)
1360 return -EIO;
1361
1362 tty->link->count++;
1363 }
1364 tty->count++;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001365
Alan Cox1aa4bed2009-06-16 17:01:33 +01001366 mutex_lock(&tty->ldisc_mutex);
Alan Cox99f1fe12008-10-13 10:42:00 +01001367 WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
Alan Cox1aa4bed2009-06-16 17:01:33 +01001368 mutex_unlock(&tty->ldisc_mutex);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001369
1370 return 0;
1371}
1372
Alan Coxaf9b8972006-08-27 01:24:01 -07001373/**
Alan Coxd81ed102008-10-13 10:41:42 +01001374 * tty_init_dev - initialise a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001375 * @driver: tty driver we are opening a device on
1376 * @idx: device index
Alan Cox15582d32008-10-13 10:41:03 +01001377 * @ret_tty: returned tty structure
Alan Coxaf9b8972006-08-27 01:24:01 -07001378 *
1379 * Prepare a tty device. This may not be a "new" clean device but
1380 * could also be an active device. The pty drivers require special
1381 * handling because of this.
1382 *
1383 * Locking:
1384 * The function is called under the tty_mutex, which
1385 * protects us from the tty struct or driver itself going away.
1386 *
1387 * On exit the tty device has the line discipline attached and
1388 * a reference count of 1. If a pair was created for pty/tty use
1389 * and the other was a pty master then it too has a reference count of 1.
1390 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001392 * failed open. The new code protects the open with a mutex, so it's
1393 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 * relaxed for the (most common) case of reopening a tty.
1395 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001396
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001397struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Alan Coxbf970ee2008-10-13 10:42:39 +01001399 struct tty_struct *tty;
Alan Cox73ec06f2008-10-13 10:42:29 +01001400 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
1403 * First time open is complex, especially for PTY devices.
1404 * This code guarantees that either everything succeeds and the
1405 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001406 * and the allocated memory released. (Except that the termios
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 * and locked termios may be retained.)
1408 */
1409
Alan Cox73ec06f2008-10-13 10:42:29 +01001410 if (!try_module_get(driver->owner))
1411 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 tty = alloc_tty_struct();
Jiri Slabyd5543502011-03-23 10:48:32 +01001414 if (!tty) {
1415 retval = -ENOMEM;
1416 goto err_module_put;
1417 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001418 initialize_tty_struct(tty, driver, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Alan Cox89c8d912012-08-08 16:30:13 +01001420 tty_lock(tty);
Alan Cox73ec06f2008-10-13 10:42:29 +01001421 retval = tty_driver_install_tty(driver, tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001422 if (retval < 0)
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001423 goto err_deinit_tty;
Alan Cox8b0a88d2008-10-13 10:42:19 +01001424
Jiri Slaby04831dc2012-06-04 13:35:36 +02001425 if (!tty->port)
1426 tty->port = driver->ports[idx];
1427
Jiri Slaby5d4121c2012-08-17 14:27:52 +02001428 WARN_RATELIMIT(!tty->port,
1429 "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
1430 __func__, tty->driver->name);
1431
Jiri Slaby967fab62012-10-18 22:26:46 +02001432 tty->port->itty = tty;
1433
Alan Cox37bdfb02008-02-08 04:18:47 -08001434 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001436 * If we fail here just call release_tty to clean up. No need
1437 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
Alan Coxbf970ee2008-10-13 10:42:39 +01001439 retval = tty_ldisc_setup(tty, tty->link);
Alan Cox01e1abb2008-07-22 11:16:55 +01001440 if (retval)
Jiri Slabyd5543502011-03-23 10:48:32 +01001441 goto err_release_tty;
Alan Cox89c8d912012-08-08 16:30:13 +01001442 /* Return the tty locked so that it cannot vanish under the caller */
Alan Cox73ec06f2008-10-13 10:42:29 +01001443 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001445err_deinit_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001446 tty_unlock(tty);
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001447 deinitialize_tty_struct(tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001448 free_tty_struct(tty);
1449err_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 module_put(driver->owner);
Jiri Slabyd5543502011-03-23 10:48:32 +01001451 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001453 /* call the tty release_tty routine to clean out this slot */
Jiri Slabyd5543502011-03-23 10:48:32 +01001454err_release_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001455 tty_unlock(tty);
Manuel Zerpies5a3c6b22011-06-16 14:07:22 +02001456 printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, "
Akinobu Mita40509142006-09-29 02:01:27 -07001457 "clearing slot %d\n", idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001458 release_tty(tty, idx);
Alan Cox73ec06f2008-10-13 10:42:29 +01001459 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Alan Coxfeebed62008-10-13 10:41:30 +01001462void tty_free_termios(struct tty_struct *tty)
1463{
1464 struct ktermios *tp;
1465 int idx = tty->index;
Alan Cox36b3c072012-07-17 17:06:57 +01001466
1467 /* If the port is going to reset then it has no termios to save */
1468 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1469 return;
1470
1471 /* Stash the termios data */
1472 tp = tty->driver->termios[idx];
1473 if (tp == NULL) {
1474 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1475 if (tp == NULL) {
1476 pr_warn("tty: no memory to save termios state.\n");
1477 return;
1478 }
Dan Carpenter4ac5d702012-07-24 12:51:52 +01001479 tty->driver->termios[idx] = tp;
Alan Coxfeebed62008-10-13 10:41:30 +01001480 }
Alan Cox36b3c072012-07-17 17:06:57 +01001481 *tp = tty->termios;
Alan Coxfeebed62008-10-13 10:41:30 +01001482}
1483EXPORT_SYMBOL(tty_free_termios);
1484
Alan Coxfeebed62008-10-13 10:41:30 +01001485
Alan Coxaf9b8972006-08-27 01:24:01 -07001486/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001487 * release_one_tty - release tty structure memory
Alan Cox9c9f4de2008-10-13 10:37:26 +01001488 * @kref: kref of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001489 *
1490 * Releases memory associated with a tty structure, and clears out the
1491 * driver table slots. This function is called when a device is no longer
1492 * in use. It also gets called when setup of a device fails.
1493 *
1494 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -07001495 * takes the file list lock internally when working on the list
1496 * of ttys that the driver keeps.
Alan Coxb50989d2009-09-19 13:13:22 -07001497 *
1498 * This method gets called from a work queue so that the driver private
Dave Youngf278a2f2009-09-27 16:00:42 +00001499 * cleanup ops can sleep (needed for USB at least)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 */
Alan Coxb50989d2009-09-19 13:13:22 -07001501static void release_one_tty(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Alan Coxb50989d2009-09-19 13:13:22 -07001503 struct tty_struct *tty =
1504 container_of(work, struct tty_struct, hangup_work);
Alan Cox6f967f72008-10-13 10:37:36 +01001505 struct tty_driver *driver = tty->driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Dave Youngf278a2f2009-09-27 16:00:42 +00001507 if (tty->ops->cleanup)
1508 tty->ops->cleanup(tty);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 tty->magic = 0;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001511 tty_driver_kref_put(driver);
Alan Cox6f967f72008-10-13 10:37:36 +01001512 module_put(driver->owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001513
Nick Pigginee2ffa02010-08-18 04:37:35 +10001514 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 list_del_init(&tty->tty_files);
Nick Pigginee2ffa02010-08-18 04:37:35 +10001516 spin_unlock(&tty_files_lock);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001517
Oleg Nesterov6da8d862010-04-02 18:05:12 +02001518 put_pid(tty->pgrp);
1519 put_pid(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 free_tty_struct(tty);
1521}
1522
Alan Coxb50989d2009-09-19 13:13:22 -07001523static void queue_release_one_tty(struct kref *kref)
1524{
1525 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Dave Youngf278a2f2009-09-27 16:00:42 +00001526
Alan Coxb50989d2009-09-19 13:13:22 -07001527 /* The hangup queue is now free so we can reuse it rather than
1528 waste a chunk of memory for each port */
1529 INIT_WORK(&tty->hangup_work, release_one_tty);
1530 schedule_work(&tty->hangup_work);
1531}
1532
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001533/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001534 * tty_kref_put - release a tty kref
1535 * @tty: tty device
1536 *
1537 * Release a reference to a tty device and if need be let the kref
1538 * layer destruct the object for us
1539 */
1540
1541void tty_kref_put(struct tty_struct *tty)
1542{
1543 if (tty)
Alan Coxb50989d2009-09-19 13:13:22 -07001544 kref_put(&tty->kref, queue_release_one_tty);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001545}
1546EXPORT_SYMBOL(tty_kref_put);
1547
1548/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001549 * release_tty - release tty structure memory
1550 *
1551 * Release both @tty and a possible linked partner (think pty pair),
1552 * and decrement the refcount of the backing module.
1553 *
1554 * Locking:
Alan Coxd1552552012-07-27 18:02:54 +01001555 * tty_mutex
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001556 * takes the file list lock internally when working on the list
1557 * of ttys that the driver keeps.
Alan Cox9c9f4de2008-10-13 10:37:26 +01001558 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001559 */
1560static void release_tty(struct tty_struct *tty, int idx)
1561{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001562 /* This should always be true but check for the moment */
1563 WARN_ON(tty->index != idx);
Alan Coxd1552552012-07-27 18:02:54 +01001564 WARN_ON(!mutex_is_locked(&tty_mutex));
Alan Cox36b3c072012-07-17 17:06:57 +01001565 if (tty->ops->shutdown)
1566 tty->ops->shutdown(tty);
1567 tty_free_termios(tty);
1568 tty_driver_remove_tty(tty->driver, tty);
Jiri Slaby967fab62012-10-18 22:26:46 +02001569 tty->port->itty = NULL;
Alan Cox36b3c072012-07-17 17:06:57 +01001570
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001571 if (tty->link)
Alan Cox9c9f4de2008-10-13 10:37:26 +01001572 tty_kref_put(tty->link);
1573 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001574}
1575
Alan Coxeeb89d92009-11-30 13:18:29 +00001576/**
Jiri Slaby955787ca2011-11-11 10:47:23 +01001577 * tty_release_checks - check a tty before real release
1578 * @tty: tty to check
1579 * @o_tty: link of @tty (if any)
1580 * @idx: index of the tty
1581 *
1582 * Performs some paranoid checking before true release of the @tty.
1583 * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1584 */
1585static int tty_release_checks(struct tty_struct *tty, struct tty_struct *o_tty,
1586 int idx)
1587{
1588#ifdef TTY_PARANOIA_CHECK
1589 if (idx < 0 || idx >= tty->driver->num) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001590 printk(KERN_DEBUG "%s: bad idx when trying to free (%s)\n",
1591 __func__, tty->name);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001592 return -1;
1593 }
1594
1595 /* not much to check for devpts */
1596 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1597 return 0;
1598
1599 if (tty != tty->driver->ttys[idx]) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001600 printk(KERN_DEBUG "%s: driver.table[%d] not tty for (%s)\n",
1601 __func__, idx, tty->name);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001602 return -1;
1603 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001604 if (tty->driver->other) {
1605 if (o_tty != tty->driver->other->ttys[idx]) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001606 printk(KERN_DEBUG "%s: other->table[%d] not o_tty for (%s)\n",
1607 __func__, idx, tty->name);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001608 return -1;
1609 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001610 if (o_tty->link != tty) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001611 printk(KERN_DEBUG "%s: bad pty pointers\n", __func__);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001612 return -1;
1613 }
1614 }
1615#endif
1616 return 0;
1617}
1618
1619/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001620 * tty_release - vfs callback for close
1621 * @inode: inode of tty
1622 * @filp: file pointer for handle to tty
1623 *
1624 * Called the last time each file handle is closed that references
1625 * this tty. There may however be several such references.
1626 *
1627 * Locking:
1628 * Takes bkl. See tty_release_dev
1629 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 * Even releasing the tty structures is a tricky business.. We have
1631 * to be very careful that the structures are all released at the
1632 * same time, as interrupts might otherwise get the wrong pointers.
1633 *
1634 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1635 * lead to double frees or releasing memory still in use.
1636 */
Alan Coxeeb89d92009-11-30 13:18:29 +00001637
1638int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Nick Piggind996b622010-08-18 04:37:36 +10001640 struct tty_struct *tty = file_tty(filp);
1641 struct tty_struct *o_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int pty_master, tty_closing, o_tty_closing, do_sleep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int idx;
1644 char buf[64];
Alan Cox37bdfb02008-02-08 04:18:47 -08001645
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001646 if (tty_paranoia_check(tty, inode, __func__))
Alan Coxeeb89d92009-11-30 13:18:29 +00001647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Alan Cox89c8d912012-08-08 16:30:13 +01001649 tty_lock(tty);
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001650 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Arnd Bergmannec79d602010-06-01 22:53:01 +02001652 __tty_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 idx = tty->index;
1655 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1656 tty->driver->subtype == PTY_TYPE_MASTER);
Alan Cox89c8d912012-08-08 16:30:13 +01001657 /* Review: parallel close */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 o_tty = tty->link;
1659
Jiri Slaby955787ca2011-11-11 10:47:23 +01001660 if (tty_release_checks(tty, o_tty, idx)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001661 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001666 printk(KERN_DEBUG "%s: %s (tty count=%d)...\n", __func__,
1667 tty_name(tty, buf), tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668#endif
1669
Alan Coxf34d7a52008-04-30 00:54:13 -07001670 if (tty->ops->close)
1671 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Alan Cox89c8d912012-08-08 16:30:13 +01001673 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
1675 * Sanity check: if tty->count is going to zero, there shouldn't be
1676 * any waiters on tty->read_wait or tty->write_wait. We test the
1677 * wait queues and kick everyone out _before_ actually starting to
1678 * close. This ensures that we won't block while releasing the tty
1679 * structure.
1680 *
1681 * The test for the o_tty closing is necessary, since the master and
1682 * slave sides may close in any order. If the slave side closes out
1683 * first, its count will be one, since the master side holds an open.
1684 * Thus this test wouldn't be triggered at the time the slave closes,
1685 * so we do it now.
1686 *
1687 * Note that it's possible for the tty to be opened again while we're
1688 * flushing out waiters. By recalculating the closing flags before
1689 * each iteration we avoid any problems.
1690 */
1691 while (1) {
1692 /* Guard against races with tty->count changes elsewhere and
1693 opens on /dev/tty */
Alan Cox37bdfb02008-02-08 04:18:47 -08001694
Ingo Molnar70522e12006-03-23 03:00:31 -08001695 mutex_lock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01001696 tty_lock_pair(tty, o_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 tty_closing = tty->count <= 1;
1698 o_tty_closing = o_tty &&
1699 (o_tty->count <= (pty_master ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 do_sleep = 0;
1701
1702 if (tty_closing) {
1703 if (waitqueue_active(&tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001704 wake_up_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 do_sleep++;
1706 }
1707 if (waitqueue_active(&tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001708 wake_up_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 do_sleep++;
1710 }
1711 }
1712 if (o_tty_closing) {
1713 if (waitqueue_active(&o_tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001714 wake_up_poll(&o_tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 do_sleep++;
1716 }
1717 if (waitqueue_active(&o_tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001718 wake_up_poll(&o_tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 do_sleep++;
1720 }
1721 }
1722 if (!do_sleep)
1723 break;
1724
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001725 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n",
1726 __func__, tty_name(tty, buf));
Alan Cox89c8d912012-08-08 16:30:13 +01001727 tty_unlock_pair(tty, o_tty);
Ingo Molnar70522e12006-03-23 03:00:31 -08001728 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 schedule();
Alan Cox37bdfb02008-02-08 04:18:47 -08001730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08001733 * The closing flags are now consistent with the open counts on
1734 * both sides, and we've completed the last operation that could
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 * block, so it's safe to proceed with closing.
Alan Coxd1552552012-07-27 18:02:54 +01001736 *
1737 * We must *not* drop the tty_mutex until we ensure that a further
1738 * entry into tty_open can not pick up this tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (pty_master) {
1741 if (--o_tty->count < 0) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001742 printk(KERN_WARNING "%s: bad pty slave count (%d) for %s\n",
1743 __func__, o_tty->count, tty_name(o_tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 o_tty->count = 0;
1745 }
1746 }
1747 if (--tty->count < 0) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001748 printk(KERN_WARNING "%s: bad tty->count (%d) for %s\n",
1749 __func__, tty->count, tty_name(tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 tty->count = 0;
1751 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /*
1754 * We've decremented tty->count, so we need to remove this file
1755 * descriptor off the tty->tty_files list; this serves two
1756 * purposes:
1757 * - check_tty_count sees the correct number of file descriptors
1758 * associated with this tty.
1759 * - do_tty_hangup no longer sees this file descriptor as
1760 * something that needs to be handled for hangups.
1761 */
Nick Piggind996b622010-08-18 04:37:36 +10001762 tty_del_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 /*
1765 * Perform some housekeeping before deciding whether to return.
1766 *
1767 * Set the TTY_CLOSING flag if this was the last open. In the
1768 * case of a pty we may have to wait around for the other side
1769 * to close, and TTY_CLOSING makes sure we can't be reopened.
1770 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001771 if (tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 set_bit(TTY_CLOSING, &tty->flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08001773 if (o_tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 set_bit(TTY_CLOSING, &o_tty->flags);
1775
1776 /*
1777 * If _either_ side is closing, make sure there aren't any
1778 * processes that still think tty or o_tty is their controlling
1779 * tty.
1780 */
1781 if (tty_closing || o_tty_closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001783 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001785 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 read_unlock(&tasklist_lock);
1787 }
1788
Ingo Molnar70522e12006-03-23 03:00:31 -08001789 mutex_unlock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01001790 tty_unlock_pair(tty, o_tty);
Alan Coxd1552552012-07-27 18:02:54 +01001791 /* At this point the TTY_CLOSING flag should ensure a dead tty
1792 cannot be re-opened by a racing opener */
Paul Fulghumda965822006-02-14 13:53:00 -08001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /* check whether both sides are closing ... */
Alan Coxd1552552012-07-27 18:02:54 +01001795 if (!tty_closing || (o_tty && !o_tty_closing))
Alan Coxeeb89d92009-11-30 13:18:29 +00001796 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001799 printk(KERN_DEBUG "%s: freeing tty structure...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800#endif
1801 /*
Alan Cox01e1abb2008-07-22 11:16:55 +01001802 * Ask the line discipline code to release its structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 */
Alan Cox01e1abb2008-07-22 11:16:55 +01001804 tty_ldisc_release(tty, o_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /*
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001806 * The release_tty function takes care of the details of clearing
Alan Cox89c8d912012-08-08 16:30:13 +01001807 * the slots and preserving the termios structure. The tty_unlock_pair
1808 * should be safe as we keep a kref while the tty is locked (so the
1809 * unlock never unlocks a freed tty).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Alan Coxd1552552012-07-27 18:02:54 +01001811 mutex_lock(&tty_mutex);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001812 release_tty(tty, idx);
Alan Coxd1552552012-07-27 18:02:54 +01001813 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Alan Coxeeb89d92009-11-30 13:18:29 +00001815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Alan Coxaf9b8972006-08-27 01:24:01 -07001818/**
Jiri Slabyb82154a2011-11-09 21:33:19 +01001819 * tty_open_current_tty - get tty of current task for open
1820 * @device: device number
1821 * @filp: file pointer to tty
1822 * @return: tty of the current task iff @device is /dev/tty
1823 *
1824 * We cannot return driver and index like for the other nodes because
1825 * devpts will not work then. It expects inodes to be from devpts FS.
Alan Cox3af502b2012-05-03 22:21:53 +01001826 *
1827 * We need to move to returning a refcounted object from all the lookup
1828 * paths including this one.
Jiri Slabyb82154a2011-11-09 21:33:19 +01001829 */
1830static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1831{
1832 struct tty_struct *tty;
1833
1834 if (device != MKDEV(TTYAUX_MAJOR, 0))
1835 return NULL;
1836
1837 tty = get_current_tty();
1838 if (!tty)
1839 return ERR_PTR(-ENXIO);
1840
1841 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1842 /* noctty = 1; */
1843 tty_kref_put(tty);
1844 /* FIXME: we put a reference and return a TTY! */
Alan Cox3af502b2012-05-03 22:21:53 +01001845 /* This is only safe because the caller holds tty_mutex */
Jiri Slabyb82154a2011-11-09 21:33:19 +01001846 return tty;
1847}
1848
1849/**
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001850 * tty_lookup_driver - lookup a tty driver for a given device file
1851 * @device: device number
1852 * @filp: file pointer to tty
1853 * @noctty: set if the device should not become a controlling tty
1854 * @index: index for the device in the @return driver
1855 * @return: driver for this inode (with increased refcount)
1856 *
1857 * If @return is not erroneous, the caller is responsible to decrement the
1858 * refcount by tty_driver_kref_put.
1859 *
1860 * Locking: tty_mutex protects get_tty_driver
1861 */
1862static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
1863 int *noctty, int *index)
1864{
1865 struct tty_driver *driver;
1866
Jiri Slaby2cd00502011-11-09 21:33:22 +01001867 switch (device) {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001868#ifdef CONFIG_VT
Jiri Slaby2cd00502011-11-09 21:33:22 +01001869 case MKDEV(TTY_MAJOR, 0): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001870 extern struct tty_driver *console_driver;
1871 driver = tty_driver_kref_get(console_driver);
1872 *index = fg_console;
1873 *noctty = 1;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001874 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001875 }
1876#endif
Jiri Slaby2cd00502011-11-09 21:33:22 +01001877 case MKDEV(TTYAUX_MAJOR, 1): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001878 struct tty_driver *console_driver = console_device(index);
1879 if (console_driver) {
1880 driver = tty_driver_kref_get(console_driver);
1881 if (driver) {
1882 /* Don't let /dev/console block */
1883 filp->f_flags |= O_NONBLOCK;
1884 *noctty = 1;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001885 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001886 }
1887 }
1888 return ERR_PTR(-ENODEV);
1889 }
Jiri Slaby2cd00502011-11-09 21:33:22 +01001890 default:
1891 driver = get_tty_driver(device, index);
1892 if (!driver)
1893 return ERR_PTR(-ENODEV);
1894 break;
1895 }
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001896 return driver;
1897}
1898
1899/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001900 * tty_open - open a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001901 * @inode: inode of device file
1902 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001904 * tty_open and tty_release keep up the tty count that contains the
1905 * number of opens done on a tty. We cannot use the inode-count, as
1906 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001908 * Open-counting is needed for pty masters, as well as for keeping
1909 * track of serial lines: DTR is dropped when the last close happens.
1910 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1911 *
1912 * The termios state of a pty is reset on first open so that
1913 * settings don't persist across reuse.
1914 *
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001915 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001916 * tty->count should protect the rest.
1917 * ->siglock protects ->signal/->sighand
Alan Cox89c8d912012-08-08 16:30:13 +01001918 *
1919 * Note: the tty_unlock/lock cases without a ref are only safe due to
1920 * tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001922
Alan Coxeeb89d92009-11-30 13:18:29 +00001923static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Jiri Slabyb82154a2011-11-09 21:33:19 +01001925 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 int noctty, retval;
Jiri Slabyb82154a2011-11-09 21:33:19 +01001927 struct tty_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 int index;
1929 dev_t device = inode->i_rdev;
Andrew Morton846c1512009-04-02 16:56:36 -07001930 unsigned saved_flags = filp->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934retry_open:
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02001935 retval = tty_alloc_file(filp);
1936 if (retval)
1937 return -ENOMEM;
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 noctty = filp->f_flags & O_NOCTTY;
1940 index = -1;
1941 retval = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001942
Ingo Molnar70522e12006-03-23 03:00:31 -08001943 mutex_lock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01001944 /* This is protected by the tty_mutex */
Jiri Slabyb82154a2011-11-09 21:33:19 +01001945 tty = tty_open_current_tty(device, filp);
1946 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001947 retval = PTR_ERR(tty);
1948 goto err_unlock;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001949 } else if (!tty) {
1950 driver = tty_lookup_driver(device, filp, &noctty, &index);
1951 if (IS_ERR(driver)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001952 retval = PTR_ERR(driver);
1953 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001956 /* check whether we're reopening an existing tty */
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001957 tty = tty_driver_lookup_tty(driver, inode, index);
Eric Paris808ffa32009-01-27 11:50:37 +00001958 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001959 retval = PTR_ERR(tty);
1960 goto err_unlock;
Eric Paris808ffa32009-01-27 11:50:37 +00001961 }
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001962 }
1963
1964 if (tty) {
Alan Cox89c8d912012-08-08 16:30:13 +01001965 tty_lock(tty);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001966 retval = tty_reopen(tty);
Alan Cox89c8d912012-08-08 16:30:13 +01001967 if (retval < 0) {
1968 tty_unlock(tty);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001969 tty = ERR_PTR(retval);
Alan Cox89c8d912012-08-08 16:30:13 +01001970 }
1971 } else /* Returns with the tty_lock held for now */
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001972 tty = tty_init_dev(driver, index);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001973
Ingo Molnar70522e12006-03-23 03:00:31 -08001974 mutex_unlock(&tty_mutex);
Jiri Slabyb82154a2011-11-09 21:33:19 +01001975 if (driver)
1976 tty_driver_kref_put(driver);
Alan Coxeeb89d92009-11-30 13:18:29 +00001977 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01001978 retval = PTR_ERR(tty);
1979 goto err_file;
Alan Coxeeb89d92009-11-30 13:18:29 +00001980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02001982 tty_add_file(tty, filp);
Nick Piggind996b622010-08-18 04:37:36 +10001983
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001984 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1986 tty->driver->subtype == PTY_TYPE_MASTER)
1987 noctty = 1;
1988#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001989 printk(KERN_DEBUG "%s: opening %s...\n", __func__, tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990#endif
Herton Ronaldo Krzesinski909bc772011-03-31 15:35:31 -03001991 if (tty->ops->open)
1992 retval = tty->ops->open(tty, filp);
1993 else
1994 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 filp->f_flags = saved_flags;
1996
Alan Cox37bdfb02008-02-08 04:18:47 -08001997 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1998 !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 retval = -EBUSY;
2000
2001 if (retval) {
2002#ifdef TTY_DEBUG_HANGUP
Jiri Slaby9de44bd2011-11-09 21:33:24 +01002003 printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__,
2004 retval, tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005#endif
Alan Cox89c8d912012-08-08 16:30:13 +01002006 tty_unlock(tty); /* need to call tty_release without BTM */
Alan Coxeeb89d92009-11-30 13:18:29 +00002007 tty_release(inode, filp);
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002008 if (retval != -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002010
2011 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 schedule();
2015 /*
2016 * Need to reset f_op in case a hangup happened.
2017 */
2018 if (filp->f_op == &hung_up_tty_fops)
2019 filp->f_op = &tty_fops;
2020 goto retry_open;
2021 }
Alan Cox89c8d912012-08-08 16:30:13 +01002022 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00002023
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002024
2025 mutex_lock(&tty_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +01002026 tty_lock(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002027 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (!noctty &&
2029 current->signal->leader &&
2030 !current->signal->tty &&
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002031 tty->session == NULL)
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07002032 __proc_set_tty(current, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002033 spin_unlock_irq(&current->sighand->siglock);
Alan Cox89c8d912012-08-08 16:30:13 +01002034 tty_unlock(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002035 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return 0;
Jiri Slabyba5db442011-11-09 21:33:21 +01002037err_unlock:
Jiri Slabyba5db442011-11-09 21:33:21 +01002038 mutex_unlock(&tty_mutex);
2039 /* after locks to avoid deadlock */
2040 if (!IS_ERR_OR_NULL(driver))
2041 tty_driver_kref_put(driver);
2042err_file:
2043 tty_free_file(filp);
2044 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046
Jonathan Corbet39d95b92008-05-16 09:10:50 -06002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Alan Coxaf9b8972006-08-27 01:24:01 -07002049/**
2050 * tty_poll - check tty status
2051 * @filp: file being polled
2052 * @wait: poll wait structures to update
2053 *
2054 * Call the line discipline polling method to obtain the poll
2055 * status of the device.
2056 *
2057 * Locking: locks called line discipline but ldisc poll method
2058 * may be re-entered freely by other callers.
2059 */
2060
Alan Cox37bdfb02008-02-08 04:18:47 -08002061static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Nick Piggind996b622010-08-18 04:37:36 +10002063 struct tty_struct *tty = file_tty(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct tty_ldisc *ld;
2065 int ret = 0;
2066
Al Viro6131ffa2013-02-27 16:59:05 -05002067 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002071 if (ld->ops->poll)
2072 ret = (ld->ops->poll)(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 tty_ldisc_deref(ld);
2074 return ret;
2075}
2076
Arnd Bergmannec79d602010-06-01 22:53:01 +02002077static int __tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Nick Piggind996b622010-08-18 04:37:36 +10002079 struct tty_struct *tty = file_tty(filp);
Alan Cox47f86832008-04-30 00:53:30 -07002080 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002081 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Al Viro6131ffa2013-02-27 16:59:05 -05002083 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002084 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 retval = fasync_helper(fd, filp, on, &tty->fasync);
2087 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002088 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002091 enum pid_type type;
2092 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (!waitqueue_active(&tty->read_wait))
2094 tty->minimum_to_wake = 1;
Alan Cox47f86832008-04-30 00:53:30 -07002095 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002096 if (tty->pgrp) {
2097 pid = tty->pgrp;
2098 type = PIDTYPE_PGID;
2099 } else {
2100 pid = task_pid(current);
2101 type = PIDTYPE_PID;
2102 }
Linus Torvalds80e1e822010-02-07 10:11:23 -08002103 get_pid(pid);
Greg Kroah-Hartman70362512009-12-17 07:07:19 -08002104 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds80e1e822010-02-07 10:11:23 -08002105 retval = __f_setown(filp, pid, type, 0);
2106 put_pid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (retval)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 } else {
2110 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2111 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2112 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002113 retval = 0;
2114out:
Arnd Bergmannec79d602010-06-01 22:53:01 +02002115 return retval;
2116}
2117
2118static int tty_fasync(int fd, struct file *filp, int on)
2119{
Alan Cox89c8d912012-08-08 16:30:13 +01002120 struct tty_struct *tty = file_tty(filp);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002121 int retval;
Alan Cox89c8d912012-08-08 16:30:13 +01002122
2123 tty_lock(tty);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002124 retval = __tty_fasync(fd, filp, on);
Alan Cox89c8d912012-08-08 16:30:13 +01002125 tty_unlock(tty);
2126
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002127 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
Alan Coxaf9b8972006-08-27 01:24:01 -07002130/**
2131 * tiocsti - fake input character
2132 * @tty: tty to fake input into
2133 * @p: pointer to character
2134 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02002135 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07002136 * input management.
2137 *
2138 * FIXME: does not honour flow control ??
2139 *
2140 * Locking:
2141 * Called functions take tty_ldisc_lock
2142 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07002143 *
2144 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07002145 */
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147static int tiocsti(struct tty_struct *tty, char __user *p)
2148{
2149 char ch, mbz = 0;
2150 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2153 return -EPERM;
2154 if (get_user(ch, p))
2155 return -EFAULT;
Al Viro1e641742008-12-09 09:23:33 +00002156 tty_audit_tiocsti(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002158 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 tty_ldisc_deref(ld);
2160 return 0;
2161}
2162
Alan Coxaf9b8972006-08-27 01:24:01 -07002163/**
2164 * tiocgwinsz - implement window query ioctl
2165 * @tty; tty
2166 * @arg: user buffer for result
2167 *
Alan Cox808a0d32006-09-29 02:00:40 -07002168 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002169 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002170 * Locking: tty->termios_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002171 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002172 */
2173
Alan Cox37bdfb02008-02-08 04:18:47 -08002174static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Alan Cox808a0d32006-09-29 02:00:40 -07002176 int err;
2177
Arjan van de Ven5785c952006-09-29 02:00:43 -07002178 mutex_lock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002179 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Arjan van de Ven5785c952006-09-29 02:00:43 -07002180 mutex_unlock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002181
2182 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Alan Coxaf9b8972006-08-27 01:24:01 -07002185/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002186 * tty_do_resize - resize event
2187 * @tty: tty being resized
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002188 * @rows: rows (character)
2189 * @cols: cols (character)
Alan Coxaf9b8972006-08-27 01:24:01 -07002190 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002191 * Update the termios variables and send the necessary signals to
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002192 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002193 */
2194
Alan Coxfc6f6232009-01-02 13:43:17 +00002195int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Alan Coxfc6f6232009-01-02 13:43:17 +00002197 struct pid *pgrp;
Alan Cox47f86832008-04-30 00:53:30 -07002198 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Alan Coxfc6f6232009-01-02 13:43:17 +00002200 /* Lock the tty */
2201 mutex_lock(&tty->termios_mutex);
2202 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002203 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002204 /* Get the PID values and reference them so we can
2205 avoid holding the tty ctrl lock while sending signals */
2206 spin_lock_irqsave(&tty->ctrl_lock, flags);
2207 pgrp = get_pid(tty->pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002208 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2209
2210 if (pgrp)
2211 kill_pgrp(pgrp, SIGWINCH, 1);
Alan Cox47f86832008-04-30 00:53:30 -07002212 put_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002213
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002214 tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002215done:
Alan Coxfc6f6232009-01-02 13:43:17 +00002216 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return 0;
2218}
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01002219EXPORT_SYMBOL(tty_do_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Alan Coxaf9b8972006-08-27 01:24:01 -07002221/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002222 * tiocswinsz - implement window size set ioctl
Alan Coxfc6f6232009-01-02 13:43:17 +00002223 * @tty; tty side of tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002224 * @arg: user buffer for result
2225 *
2226 * Copies the user idea of the window size to the kernel. Traditionally
2227 * this is just advisory information but for the Linux console it
2228 * actually has driver level meaning and triggers a VC resize.
2229 *
2230 * Locking:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002231 * Driver dependent. The default do_resize method takes the
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002232 * tty termios mutex and ctrl_lock. The console takes its own lock
2233 * then calls into the default method.
2234 */
2235
Alan Coxfc6f6232009-01-02 13:43:17 +00002236static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002237{
2238 struct winsize tmp_ws;
2239 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2240 return -EFAULT;
2241
2242 if (tty->ops->resize)
Alan Coxfc6f6232009-01-02 13:43:17 +00002243 return tty->ops->resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002244 else
Alan Coxfc6f6232009-01-02 13:43:17 +00002245 return tty_do_resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002246}
2247
2248/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002249 * tioccons - allow admin to move logical console
2250 * @file: the file to become console
2251 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002252 * Allow the administrator to move the redirected console device
Alan Coxaf9b8972006-08-27 01:24:01 -07002253 *
2254 * Locking: uses redirect_lock to guard the redirect information
2255 */
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257static int tioccons(struct file *file)
2258{
2259 if (!capable(CAP_SYS_ADMIN))
2260 return -EPERM;
2261 if (file->f_op->write == redirected_tty_write) {
2262 struct file *f;
2263 spin_lock(&redirect_lock);
2264 f = redirect;
2265 redirect = NULL;
2266 spin_unlock(&redirect_lock);
2267 if (f)
2268 fput(f);
2269 return 0;
2270 }
2271 spin_lock(&redirect_lock);
2272 if (redirect) {
2273 spin_unlock(&redirect_lock);
2274 return -EBUSY;
2275 }
Al Virocb0942b2012-08-27 14:48:26 -04002276 redirect = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 spin_unlock(&redirect_lock);
2278 return 0;
2279}
2280
Alan Coxaf9b8972006-08-27 01:24:01 -07002281/**
2282 * fionbio - non blocking ioctl
2283 * @file: file to set blocking value
2284 * @p: user parameter
2285 *
2286 * Historical tty interfaces had a blocking control ioctl before
2287 * the generic functionality existed. This piece of history is preserved
2288 * in the expected tty API of posix OS's.
2289 *
Alan Cox6146b9a2009-09-19 13:13:19 -07002290 * Locking: none, the open file handle ensures it won't go away.
Alan Coxaf9b8972006-08-27 01:24:01 -07002291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293static int fionbio(struct file *file, int __user *p)
2294{
2295 int nonblock;
2296
2297 if (get_user(nonblock, p))
2298 return -EFAULT;
2299
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002300 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (nonblock)
2302 file->f_flags |= O_NONBLOCK;
2303 else
2304 file->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002305 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return 0;
2307}
2308
Alan Coxaf9b8972006-08-27 01:24:01 -07002309/**
2310 * tiocsctty - set controlling tty
2311 * @tty: tty structure
2312 * @arg: user argument
2313 *
2314 * This ioctl is used to manage job control. It permits a session
2315 * leader to set this tty as the controlling tty for the session.
2316 *
2317 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002318 * Takes tty_mutex() to protect tty instance
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002319 * Takes tasklist_lock internally to walk sessions
2320 * Takes ->siglock() when updating signal->tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002321 */
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323static int tiocsctty(struct tty_struct *tty, int arg)
2324{
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002325 int ret = 0;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002326 if (current->signal->leader && (task_session(current) == tty->session))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002327 return ret;
2328
2329 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 /*
2331 * The process must be a session leader and
2332 * not have a controlling tty already.
2333 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002334 if (!current->signal->leader || current->signal->tty) {
2335 ret = -EPERM;
2336 goto unlock;
2337 }
2338
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002339 if (tty->session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 /*
2341 * This tty is already the controlling
2342 * tty for another session group!
2343 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002344 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 /*
2346 * Steal it away
2347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002349 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 read_unlock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002351 } else {
2352 ret = -EPERM;
2353 goto unlock;
2354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002356 proc_set_tty(current, tty);
2357unlock:
Alan Cox28298232006-09-29 02:00:58 -07002358 mutex_unlock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002359 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Alan Coxaf9b8972006-08-27 01:24:01 -07002362/**
Alan Cox5d0fdf12008-04-30 00:53:31 -07002363 * tty_get_pgrp - return a ref counted pgrp pid
2364 * @tty: tty to read
2365 *
2366 * Returns a refcounted instance of the pid struct for the process
2367 * group controlling the tty.
2368 */
2369
2370struct pid *tty_get_pgrp(struct tty_struct *tty)
2371{
2372 unsigned long flags;
2373 struct pid *pgrp;
2374
2375 spin_lock_irqsave(&tty->ctrl_lock, flags);
2376 pgrp = get_pid(tty->pgrp);
2377 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2378
2379 return pgrp;
2380}
2381EXPORT_SYMBOL_GPL(tty_get_pgrp);
2382
2383/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002384 * tiocgpgrp - get process group
2385 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002386 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002387 * @p: returned pid
2388 *
2389 * Obtain the process group of the tty. If there is no process group
2390 * return an error.
2391 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002392 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002393 */
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2396{
Alan Cox5d0fdf12008-04-30 00:53:31 -07002397 struct pid *pid;
2398 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 /*
2400 * (tty == real_tty) is a cheap way of
2401 * testing if the tty is NOT a master pty.
2402 */
2403 if (tty == real_tty && current->signal->tty != real_tty)
2404 return -ENOTTY;
Alan Cox5d0fdf12008-04-30 00:53:31 -07002405 pid = tty_get_pgrp(real_tty);
2406 ret = put_user(pid_vnr(pid), p);
2407 put_pid(pid);
2408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
Alan Coxaf9b8972006-08-27 01:24:01 -07002411/**
2412 * tiocspgrp - attempt to set process group
2413 * @tty: tty passed by user
2414 * @real_tty: tty side device matching tty passed by user
2415 * @p: pid pointer
2416 *
2417 * Set the process group of the tty to the session passed. Only
2418 * permitted where the tty session is our session.
2419 *
Alan Cox47f86832008-04-30 00:53:30 -07002420 * Locking: RCU, ctrl lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002421 */
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2424{
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002425 struct pid *pgrp;
2426 pid_t pgrp_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 int retval = tty_check_change(real_tty);
Alan Cox47f86832008-04-30 00:53:30 -07002428 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 if (retval == -EIO)
2431 return -ENOTTY;
2432 if (retval)
2433 return retval;
2434 if (!current->signal->tty ||
2435 (current->signal->tty != real_tty) ||
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002436 (real_tty->session != task_session(current)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return -ENOTTY;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002438 if (get_user(pgrp_nr, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return -EFAULT;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002440 if (pgrp_nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return -EINVAL;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002442 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002443 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002444 retval = -ESRCH;
2445 if (!pgrp)
2446 goto out_unlock;
2447 retval = -EPERM;
2448 if (session_of_pgrp(pgrp) != task_session(current))
2449 goto out_unlock;
2450 retval = 0;
Alan Cox47f86832008-04-30 00:53:30 -07002451 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002452 put_pid(real_tty->pgrp);
2453 real_tty->pgrp = get_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002454 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002455out_unlock:
2456 rcu_read_unlock();
2457 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
Alan Coxaf9b8972006-08-27 01:24:01 -07002460/**
2461 * tiocgsid - get session id
2462 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002463 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002464 * @p: pointer to returned session id
2465 *
2466 * Obtain the session id of the tty. If there is no session
2467 * return an error.
2468 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002469 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002470 */
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2473{
2474 /*
2475 * (tty == real_tty) is a cheap way of
2476 * testing if the tty is NOT a master pty.
2477 */
2478 if (tty == real_tty && current->signal->tty != real_tty)
2479 return -ENOTTY;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002480 if (!real_tty->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return -ENOTTY;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002482 return put_user(pid_vnr(real_tty->session), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
Alan Coxaf9b8972006-08-27 01:24:01 -07002485/**
2486 * tiocsetd - set line discipline
2487 * @tty: tty device
2488 * @p: pointer to user data
2489 *
2490 * Set the line discipline according to user request.
2491 *
2492 * Locking: see tty_set_ldisc, this function is just a helper
2493 */
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495static int tiocsetd(struct tty_struct *tty, int __user *p)
2496{
2497 int ldisc;
Alan Cox04f378b2008-04-30 00:53:29 -07002498 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 if (get_user(ldisc, p))
2501 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002502
Alan Cox04f378b2008-04-30 00:53:29 -07002503 ret = tty_set_ldisc(tty, ldisc);
Alan Cox04f378b2008-04-30 00:53:29 -07002504
2505 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Alan Coxaf9b8972006-08-27 01:24:01 -07002508/**
2509 * send_break - performed time break
2510 * @tty: device to break on
2511 * @duration: timeout in mS
2512 *
2513 * Perform a timed break on hardware that lacks its own driver level
2514 * timed break functionality.
2515 *
2516 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002517 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002518 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002519 */
2520
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002521static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Alan Cox9e989662008-07-22 11:18:03 +01002523 int retval;
2524
2525 if (tty->ops->break_ctl == NULL)
2526 return 0;
2527
2528 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2529 retval = tty->ops->break_ctl(tty, duration);
2530 else {
2531 /* Do the work ourselves */
2532 if (tty_write_lock(tty, 0) < 0)
2533 return -EINTR;
2534 retval = tty->ops->break_ctl(tty, -1);
2535 if (retval)
2536 goto out;
2537 if (!signal_pending(current))
2538 msleep_interruptible(duration);
2539 retval = tty->ops->break_ctl(tty, 0);
2540out:
2541 tty_write_unlock(tty);
2542 if (signal_pending(current))
2543 retval = -EINTR;
2544 }
2545 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
Alan Coxaf9b8972006-08-27 01:24:01 -07002548/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002549 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002550 * @tty: tty device
2551 * @file: user file pointer
2552 * @p: pointer to result
2553 *
2554 * Obtain the modem status bits from the tty driver if the feature
2555 * is supported. Return -EINVAL if it is not available.
2556 *
2557 * Locking: none (up to the driver)
2558 */
2559
Alan Cox60b33c12011-02-14 16:26:14 +00002560static int tty_tiocmget(struct tty_struct *tty, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
2562 int retval = -EINVAL;
2563
Alan Coxf34d7a52008-04-30 00:54:13 -07002564 if (tty->ops->tiocmget) {
Alan Cox60b33c12011-02-14 16:26:14 +00002565 retval = tty->ops->tiocmget(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
2567 if (retval >= 0)
2568 retval = put_user(retval, p);
2569 }
2570 return retval;
2571}
2572
Alan Coxaf9b8972006-08-27 01:24:01 -07002573/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002574 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002575 * @tty: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002576 * @cmd: command - clear bits, set bits or set all
2577 * @p: pointer to desired bits
2578 *
2579 * Set the modem status bits from the tty driver if the feature
2580 * is supported. Return -EINVAL if it is not available.
2581 *
2582 * Locking: none (up to the driver)
2583 */
2584
Alan Cox20b9d172011-02-14 16:26:50 +00002585static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 unsigned __user *p)
2587{
Alan Coxae677512008-07-16 21:56:54 +01002588 int retval;
2589 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Alan Coxae677512008-07-16 21:56:54 +01002591 if (tty->ops->tiocmset == NULL)
2592 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Alan Coxae677512008-07-16 21:56:54 +01002594 retval = get_user(val, p);
2595 if (retval)
2596 return retval;
2597 set = clear = 0;
2598 switch (cmd) {
2599 case TIOCMBIS:
2600 set = val;
2601 break;
2602 case TIOCMBIC:
2603 clear = val;
2604 break;
2605 case TIOCMSET:
2606 set = val;
2607 clear = ~val;
2608 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
Alan Coxae677512008-07-16 21:56:54 +01002610 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2611 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
Alan Cox20b9d172011-02-14 16:26:50 +00002612 return tty->ops->tiocmset(tty, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
2614
Alan Coxd281da72010-09-16 18:21:24 +01002615static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2616{
2617 int retval = -EINVAL;
2618 struct serial_icounter_struct icount;
2619 memset(&icount, 0, sizeof(icount));
2620 if (tty->ops->get_icount)
2621 retval = tty->ops->get_icount(tty, &icount);
2622 if (retval != 0)
2623 return retval;
2624 if (copy_to_user(arg, &icount, sizeof(icount)))
2625 return -EFAULT;
2626 return 0;
2627}
2628
Alan Coxe8b70e72009-06-11 12:48:02 +01002629struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2630{
2631 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2632 tty->driver->subtype == PTY_TYPE_MASTER)
2633 tty = tty->link;
2634 return tty;
2635}
2636EXPORT_SYMBOL(tty_pair_get_tty);
2637
2638struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
2639{
2640 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2641 tty->driver->subtype == PTY_TYPE_MASTER)
2642 return tty;
2643 return tty->link;
2644}
2645EXPORT_SYMBOL(tty_pair_get_pty);
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647/*
2648 * Split this up, as gcc can choke on it otherwise..
2649 */
Alan Cox04f378b2008-04-30 00:53:29 -07002650long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
Nick Piggind996b622010-08-18 04:37:36 +10002652 struct tty_struct *tty = file_tty(file);
2653 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 void __user *p = (void __user *)arg;
2655 int retval;
2656 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002657
Al Viro6131ffa2013-02-27 16:59:05 -05002658 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return -EINVAL;
2660
Alan Coxe8b70e72009-06-11 12:48:02 +01002661 real_tty = tty_pair_get_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663 /*
2664 * Factor out some common prep work
2665 */
2666 switch (cmd) {
2667 case TIOCSETD:
2668 case TIOCSBRK:
2669 case TIOCCBRK:
2670 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002671 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 retval = tty_check_change(tty);
2673 if (retval)
2674 return retval;
2675 if (cmd != TIOCCBRK) {
2676 tty_wait_until_sent(tty, 0);
2677 if (signal_pending(current))
2678 return -EINTR;
2679 }
2680 break;
2681 }
2682
Alan Cox9e989662008-07-22 11:18:03 +01002683 /*
2684 * Now do the stuff.
2685 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002687 case TIOCSTI:
2688 return tiocsti(tty, p);
2689 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002690 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002691 case TIOCSWINSZ:
Alan Coxfc6f6232009-01-02 13:43:17 +00002692 return tiocswinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002693 case TIOCCONS:
2694 return real_tty != tty ? -EINVAL : tioccons(file);
2695 case FIONBIO:
2696 return fionbio(file, p);
2697 case TIOCEXCL:
2698 set_bit(TTY_EXCLUSIVE, &tty->flags);
2699 return 0;
2700 case TIOCNXCL:
2701 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2702 return 0;
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +04002703 case TIOCGEXCL:
2704 {
2705 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2706 return put_user(excl, (int __user *)p);
2707 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002708 case TIOCNOTTY:
2709 if (current->signal->tty != tty)
2710 return -ENOTTY;
2711 no_tty();
2712 return 0;
2713 case TIOCSCTTY:
2714 return tiocsctty(tty, arg);
2715 case TIOCGPGRP:
2716 return tiocgpgrp(tty, real_tty, p);
2717 case TIOCSPGRP:
2718 return tiocspgrp(tty, real_tty, p);
2719 case TIOCGSID:
2720 return tiocgsid(tty, real_tty, p);
2721 case TIOCGETD:
Alan Coxc65c9bc2009-06-11 12:50:12 +01002722 return put_user(tty->ldisc->ops->num, (int __user *)p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002723 case TIOCSETD:
2724 return tiocsetd(tty, p);
Kay Sievers3c95c982011-02-17 18:39:28 +01002725 case TIOCVHANGUP:
2726 if (!capable(CAP_SYS_ADMIN))
2727 return -EPERM;
2728 tty_vhangup(tty);
2729 return 0;
Werner Finkb7b8de02010-12-03 12:48:23 +01002730 case TIOCGDEV:
2731 {
2732 unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2733 return put_user(ret, (unsigned int __user *)p);
2734 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002735 /*
2736 * Break handling
2737 */
2738 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002739 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002740 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002741 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002742 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002743 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002744 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002745 return 0;
2746 case TCSBRK: /* SVID version: non-zero arg --> no break */
2747 /* non-zero arg means wait for all output data
2748 * to be sent (performed above) but don't send break.
2749 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002751 if (!arg)
2752 return send_break(tty, 250);
2753 return 0;
2754 case TCSBRKP: /* support for POSIX tcsendbreak() */
2755 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Alan Cox37bdfb02008-02-08 04:18:47 -08002757 case TIOCMGET:
Alan Cox60b33c12011-02-14 16:26:14 +00002758 return tty_tiocmget(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002759 case TIOCMSET:
2760 case TIOCMBIC:
2761 case TIOCMBIS:
Alan Cox20b9d172011-02-14 16:26:50 +00002762 return tty_tiocmset(tty, cmd, p);
Alan Coxd281da72010-09-16 18:21:24 +01002763 case TIOCGICOUNT:
2764 retval = tty_tiocgicount(tty, p);
2765 /* For the moment allow fall through to the old method */
2766 if (retval != -EINVAL)
2767 return retval;
2768 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002769 case TCFLSH:
2770 switch (arg) {
2771 case TCIFLUSH:
2772 case TCIOFLUSH:
2773 /* flush tty buffer and allow ldisc to process ioctl */
2774 tty_buffer_flush(tty);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002775 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002776 }
2777 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002779 if (tty->ops->ioctl) {
Alan Cox6caa76b2011-02-14 16:27:22 +00002780 retval = (tty->ops->ioctl)(tty, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (retval != -ENOIOCTLCMD)
2782 return retval;
2783 }
2784 ld = tty_ldisc_ref_wait(tty);
2785 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002786 if (ld->ops->ioctl) {
2787 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (retval == -ENOIOCTLCMD)
Wanlong Gaobbb63c52012-08-27 15:23:12 +08002789 retval = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791 tty_ldisc_deref(ld);
2792 return retval;
2793}
2794
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002795#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08002796static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002797 unsigned long arg)
2798{
Nick Piggind996b622010-08-18 04:37:36 +10002799 struct tty_struct *tty = file_tty(file);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002800 struct tty_ldisc *ld;
2801 int retval = -ENOIOCTLCMD;
2802
Al Viro6131ffa2013-02-27 16:59:05 -05002803 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002804 return -EINVAL;
2805
Alan Coxf34d7a52008-04-30 00:54:13 -07002806 if (tty->ops->compat_ioctl) {
Alan Cox6caa76b2011-02-14 16:27:22 +00002807 retval = (tty->ops->compat_ioctl)(tty, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002808 if (retval != -ENOIOCTLCMD)
2809 return retval;
2810 }
2811
2812 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002813 if (ld->ops->compat_ioctl)
2814 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Thomas Meyer8193c422011-10-05 23:13:13 +02002815 else
2816 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002817 tty_ldisc_deref(ld);
2818
2819 return retval;
2820}
2821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Al Viroc3c073f2012-08-21 22:32:06 -04002823static int this_tty(const void *t, struct file *file, unsigned fd)
2824{
2825 if (likely(file->f_op->read != tty_read))
2826 return 0;
2827 return file_tty(file) != t ? 0 : fd + 1;
2828}
2829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830/*
2831 * This implements the "Secure Attention Key" --- the idea is to
2832 * prevent trojan horses by killing all processes associated with this
2833 * tty when the user hits the "Secure Attention Key". Required for
2834 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08002835 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 * This code could be nicer; ideally it should send a HUP, wait a few
2837 * seconds, then send a INT, and then a KILL signal. But you then
2838 * have to coordinate with the init process, since all processes associated
2839 * with the current tty must be dead before the new getty is allowed
2840 * to spawn.
2841 *
2842 * Now, if it would be correct ;-/ The current code has a nasty hole -
2843 * it doesn't catch files in flight. We may send the descriptor to ourselves
2844 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2845 *
2846 * Nasty bug: do_SAK is being called in interrupt context. This can
2847 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2848 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002849void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
2851#ifdef TTY_SOFT_SAK
2852 tty_hangup(tty);
2853#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08002854 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002855 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -08002857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (!tty)
2859 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002860 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08002861
Dan Carpenterb3f13de2006-12-13 00:35:09 -08002862 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Alan Coxf34d7a52008-04-30 00:54:13 -07002864 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08002865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002867 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002868 do_each_pid_task(session, PIDTYPE_SID, p) {
Eric W. Biederman652486f2006-03-28 16:11:02 -08002869 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07002870 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002871 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002872 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002873 } while_each_pid_task(session, PIDTYPE_SID, p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002874 /* Now kill any processes that happen to have the
2875 * tty open.
2876 */
2877 do_each_thread(g, p) {
2878 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07002880 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002881 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 send_sig(SIGKILL, p, 1);
2883 continue;
2884 }
2885 task_lock(p);
Al Viroc3c073f2012-08-21 22:32:06 -04002886 i = iterate_fd(p->files, 0, this_tty, tty);
2887 if (i != 0) {
2888 printk(KERN_NOTICE "SAK: killed process %d"
2889 " (%s): fd#%d opened to the tty\n",
2890 task_pid_nr(p), p->comm, i - 1);
2891 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
2893 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002894 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 read_unlock(&tasklist_lock);
2896#endif
2897}
2898
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002899static void do_SAK_work(struct work_struct *work)
2900{
2901 struct tty_struct *tty =
2902 container_of(work, struct tty_struct, SAK_work);
2903 __do_SAK(tty);
2904}
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906/*
2907 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2908 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2909 * the values which we write to it will be identical to the values which it
2910 * already has. --akpm
2911 */
2912void do_SAK(struct tty_struct *tty)
2913{
2914 if (!tty)
2915 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 schedule_work(&tty->SAK_work);
2917}
2918
2919EXPORT_SYMBOL(do_SAK);
2920
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08002921static int dev_match_devt(struct device *dev, const void *data)
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002922{
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08002923 const dev_t *devt = data;
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002924 return dev->devt == *devt;
2925}
2926
2927/* Must put_device() after it's unused! */
2928static struct device *tty_get_device(struct tty_struct *tty)
2929{
2930 dev_t devt = tty_devnum(tty);
2931 return class_find_device(tty_class, NULL, &devt, dev_match_devt);
2932}
2933
2934
Alan Coxaf9b8972006-08-27 01:24:01 -07002935/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002936 * initialize_tty_struct
2937 * @tty: tty to initialize
2938 *
2939 * This subroutine initializes a tty structure that has been newly
2940 * allocated.
2941 *
2942 * Locking: none - tty in question must not be exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002944
Alan Coxbf970ee2008-10-13 10:42:39 +01002945void initialize_tty_struct(struct tty_struct *tty,
2946 struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948 memset(tty, 0, sizeof(struct tty_struct));
Alan Cox9c9f4de2008-10-13 10:37:26 +01002949 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 tty->magic = TTY_MAGIC;
Alan Cox01e1abb2008-07-22 11:16:55 +01002951 tty_ldisc_init(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002952 tty->session = NULL;
2953 tty->pgrp = NULL;
Alan Cox89c8d912012-08-08 16:30:13 +01002954 mutex_init(&tty->legacy_mutex);
Arjan van de Ven5785c952006-09-29 02:00:43 -07002955 mutex_init(&tty->termios_mutex);
Alan Coxc65c9bc2009-06-11 12:50:12 +01002956 mutex_init(&tty->ldisc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 init_waitqueue_head(&tty->write_wait);
2958 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00002959 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08002960 mutex_init(&tty->atomic_write_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07002961 spin_lock_init(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002963 INIT_WORK(&tty->SAK_work, do_SAK_work);
Alan Coxbf970ee2008-10-13 10:42:39 +01002964
2965 tty->driver = driver;
2966 tty->ops = driver->ops;
2967 tty->index = idx;
2968 tty_line_name(driver, idx, tty->name);
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04002969 tty->dev = tty_get_device(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
Alan Coxf34d7a52008-04-30 00:54:13 -07002972/**
Jiri Slaby67166712011-03-23 10:48:35 +01002973 * deinitialize_tty_struct
2974 * @tty: tty to deinitialize
2975 *
2976 * This subroutine deinitializes a tty structure that has been newly
2977 * allocated but tty_release cannot be called on that yet.
2978 *
2979 * Locking: none - tty in question must not be exposed at this point
2980 */
2981void deinitialize_tty_struct(struct tty_struct *tty)
2982{
2983 tty_ldisc_deinit(tty);
2984}
2985
2986/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002987 * tty_put_char - write one character to a tty
2988 * @tty: tty
2989 * @ch: character
2990 *
2991 * Write one byte to the tty using the provided put_char method
2992 * if present. Returns the number of characters successfully output.
2993 *
2994 * Note: the specific put_char operation in the driver layer may go
2995 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002997
Alan Coxf34d7a52008-04-30 00:54:13 -07002998int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Alan Coxf34d7a52008-04-30 00:54:13 -07003000 if (tty->ops->put_char)
3001 return tty->ops->put_char(tty, ch);
3002 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
Alan Coxf34d7a52008-04-30 00:54:13 -07003004EXPORT_SYMBOL_GPL(tty_put_char);
3005
Alan Coxd81ed102008-10-13 10:41:42 +01003006struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003008static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
3009 unsigned int index, unsigned int count)
3010{
3011 /* init here, since reused cdevs cause crashes */
3012 cdev_init(&driver->cdevs[index], &tty_fops);
3013 driver->cdevs[index].owner = driver->owner;
3014 return cdev_add(&driver->cdevs[index], dev, count);
3015}
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003018 * tty_register_device - register a tty device
3019 * @driver: the tty driver that describes the tty device
3020 * @index: the index in the tty driver for this tty device
3021 * @device: a struct device that is associated with this tty device.
3022 * This field is optional, if there is no known struct device
3023 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003025 * Returns a pointer to the struct device for this tty device
3026 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003027 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003028 * This call is required to be made to register an individual tty device
3029 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3030 * that bit is not set, this function should not be called by a tty
3031 * driver.
3032 *
3033 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003035
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003036struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3037 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003039 return tty_register_device_attr(driver, index, device, NULL, NULL);
3040}
3041EXPORT_SYMBOL(tty_register_device);
3042
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003043static void tty_device_create_release(struct device *dev)
3044{
3045 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3046 kfree(dev);
3047}
3048
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003049/**
3050 * tty_register_device_attr - register a tty device
3051 * @driver: the tty driver that describes the tty device
3052 * @index: the index in the tty driver for this tty device
3053 * @device: a struct device that is associated with this tty device.
3054 * This field is optional, if there is no known struct device
3055 * for this tty device it can be set to NULL safely.
3056 * @drvdata: Driver data to be set to device.
3057 * @attr_grp: Attribute group to be set on device.
3058 *
3059 * Returns a pointer to the struct device for this tty device
3060 * (or ERR_PTR(-EFOO) on error).
3061 *
3062 * This call is required to be made to register an individual tty device
3063 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3064 * that bit is not set, this function should not be called by a tty
3065 * driver.
3066 *
3067 * Locking: ??
3068 */
3069struct device *tty_register_device_attr(struct tty_driver *driver,
3070 unsigned index, struct device *device,
3071 void *drvdata,
3072 const struct attribute_group **attr_grp)
3073{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 char name[64];
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003075 dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
3076 struct device *dev = NULL;
3077 int retval = -ENODEV;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003078 bool cdev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080 if (index >= driver->num) {
3081 printk(KERN_ERR "Attempt to register invalid tty line number "
3082 " (%d).\n", index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003083 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 if (driver->type == TTY_DRIVER_TYPE_PTY)
3087 pty_line_name(driver, index, name);
3088 else
3089 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003090
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003091 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003092 retval = tty_cdev_add(driver, devt, index, 1);
3093 if (retval)
3094 goto error;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003095 cdev = true;
3096 }
3097
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003098 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3099 if (!dev) {
3100 retval = -ENOMEM;
3101 goto error;
3102 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003103
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003104 dev->devt = devt;
3105 dev->class = tty_class;
3106 dev->parent = device;
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003107 dev->release = tty_device_create_release;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003108 dev_set_name(dev, "%s", name);
3109 dev->groups = attr_grp;
3110 dev_set_drvdata(dev, drvdata);
3111
3112 retval = device_register(dev);
3113 if (retval)
3114 goto error;
3115
3116 return dev;
3117
3118error:
3119 put_device(dev);
3120 if (cdev)
3121 cdev_del(&driver->cdevs[index]);
3122 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003124EXPORT_SYMBOL_GPL(tty_register_device_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003127 * tty_unregister_device - unregister a tty device
3128 * @driver: the tty driver that describes the tty device
3129 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003131 * If a tty device is registered with a call to tty_register_device() then
3132 * this function must be called when the tty device is gone.
3133 *
3134 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137void tty_unregister_device(struct tty_driver *driver, unsigned index)
3138{
Alan Cox37bdfb02008-02-08 04:18:47 -08003139 device_destroy(tty_class,
3140 MKDEV(driver->major, driver->minor_start) + index);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003141 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC))
3142 cdev_del(&driver->cdevs[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144EXPORT_SYMBOL(tty_unregister_device);
3145
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003146/**
3147 * __tty_alloc_driver -- allocate tty driver
3148 * @lines: count of lines this driver can handle at most
3149 * @owner: module which is repsonsible for this driver
3150 * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
3151 *
3152 * This should not be called directly, some of the provided macros should be
3153 * used instead. Use IS_ERR and friends on @retval.
3154 */
3155struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
3156 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
3158 struct tty_driver *driver;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003159 unsigned int cdevs = 1;
Jiri Slaby16a02082012-08-08 22:26:42 +02003160 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Jiri Slaby0019b402012-08-08 22:26:43 +02003162 if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003163 return ERR_PTR(-EINVAL);
3164
Jean Delvare506eb992007-07-15 23:40:14 -07003165 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003166 if (!driver)
3167 return ERR_PTR(-ENOMEM);
3168
3169 kref_init(&driver->kref);
3170 driver->magic = TTY_DRIVER_MAGIC;
3171 driver->num = lines;
3172 driver->owner = owner;
3173 driver->flags = flags;
Jiri Slaby16a02082012-08-08 22:26:42 +02003174
3175 if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
3176 driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
3177 GFP_KERNEL);
3178 driver->termios = kcalloc(lines, sizeof(*driver->termios),
3179 GFP_KERNEL);
3180 if (!driver->ttys || !driver->termios) {
3181 err = -ENOMEM;
3182 goto err_free_all;
3183 }
3184 }
3185
3186 if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3187 driver->ports = kcalloc(lines, sizeof(*driver->ports),
3188 GFP_KERNEL);
3189 if (!driver->ports) {
3190 err = -ENOMEM;
3191 goto err_free_all;
3192 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003193 cdevs = lines;
3194 }
3195
3196 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
3197 if (!driver->cdevs) {
3198 err = -ENOMEM;
3199 goto err_free_all;
Jiri Slaby16a02082012-08-08 22:26:42 +02003200 }
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 return driver;
Jiri Slaby16a02082012-08-08 22:26:42 +02003203err_free_all:
3204 kfree(driver->ports);
3205 kfree(driver->ttys);
3206 kfree(driver->termios);
3207 kfree(driver);
3208 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209}
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003210EXPORT_SYMBOL(__tty_alloc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Alan Cox7d7b93c2008-10-13 10:42:09 +01003212static void destruct_tty_driver(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003214 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
3215 int i;
3216 struct ktermios *tp;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003217
3218 if (driver->flags & TTY_DRIVER_INSTALLED) {
3219 /*
3220 * Free the termios and termios_locked structures because
3221 * we don't want to get memory leaks when modular tty
3222 * drivers are removed from the kernel.
3223 */
3224 for (i = 0; i < driver->num; i++) {
3225 tp = driver->termios[i];
3226 if (tp) {
3227 driver->termios[i] = NULL;
3228 kfree(tp);
3229 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003230 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3231 tty_unregister_device(driver, i);
3232 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003233 proc_tty_unregister_driver(driver);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003234 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
3235 cdev_del(&driver->cdevs[0]);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003236 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003237 kfree(driver->cdevs);
Jiri Slaby04831dc2012-06-04 13:35:36 +02003238 kfree(driver->ports);
Jiri Slaby16a02082012-08-08 22:26:42 +02003239 kfree(driver->termios);
3240 kfree(driver->ttys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 kfree(driver);
3242}
3243
Alan Cox7d7b93c2008-10-13 10:42:09 +01003244void tty_driver_kref_put(struct tty_driver *driver)
3245{
3246 kref_put(&driver->kref, destruct_tty_driver);
3247}
3248EXPORT_SYMBOL(tty_driver_kref_put);
3249
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003250void tty_set_operations(struct tty_driver *driver,
3251 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
Alan Coxf34d7a52008-04-30 00:54:13 -07003253 driver->ops = op;
3254};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255EXPORT_SYMBOL(tty_set_operations);
3256
Alan Cox7d7b93c2008-10-13 10:42:09 +01003257void put_tty_driver(struct tty_driver *d)
3258{
3259 tty_driver_kref_put(d);
3260}
3261EXPORT_SYMBOL(put_tty_driver);
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263/*
3264 * Called by a tty driver to register itself.
3265 */
3266int tty_register_driver(struct tty_driver *driver)
3267{
3268 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08003269 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 dev_t dev;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003271 struct device *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003274 error = alloc_chrdev_region(&dev, driver->minor_start,
3275 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 if (!error) {
3277 driver->major = MAJOR(dev);
3278 driver->minor_start = MINOR(dev);
3279 }
3280 } else {
3281 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01003282 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 }
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003284 if (error < 0)
Jiri Slaby16a02082012-08-08 22:26:42 +02003285 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003287 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
3288 error = tty_cdev_add(driver, dev, 0, driver->num);
3289 if (error)
3290 goto err_unreg_char;
3291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003293 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003295 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003296
3297 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003298 for (i = 0; i < driver->num; i++) {
3299 d = tty_register_device(driver, i, NULL);
3300 if (IS_ERR(d)) {
3301 error = PTR_ERR(d);
Jiri Slaby16a02082012-08-08 22:26:42 +02003302 goto err_unreg_devs;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003303 }
3304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
3306 proc_tty_register_driver(driver);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003307 driver->flags |= TTY_DRIVER_INSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return 0;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003309
Jiri Slaby16a02082012-08-08 22:26:42 +02003310err_unreg_devs:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003311 for (i--; i >= 0; i--)
3312 tty_unregister_device(driver, i);
3313
3314 mutex_lock(&tty_mutex);
3315 list_del(&driver->tty_drivers);
3316 mutex_unlock(&tty_mutex);
3317
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003318err_unreg_char:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003319 unregister_chrdev_region(dev, driver->num);
Jiri Slaby16a02082012-08-08 22:26:42 +02003320err:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003321 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323EXPORT_SYMBOL(tty_register_driver);
3324
3325/*
3326 * Called by a tty driver to unregister itself.
3327 */
3328int tty_unregister_driver(struct tty_driver *driver)
3329{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003330#if 0
3331 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 if (driver->refcount)
3333 return -EBUSY;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003334#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3336 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003337 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003339 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return 0;
3341}
Alan Cox7d7b93c2008-10-13 10:42:09 +01003342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343EXPORT_SYMBOL(tty_unregister_driver);
3344
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003345dev_t tty_devnum(struct tty_struct *tty)
3346{
3347 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3348}
3349EXPORT_SYMBOL(tty_devnum);
3350
3351void proc_clear_tty(struct task_struct *p)
3352{
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003353 unsigned long flags;
Alan Cox9c9f4de2008-10-13 10:37:26 +01003354 struct tty_struct *tty;
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003355 spin_lock_irqsave(&p->sighand->siglock, flags);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003356 tty = p->signal->tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003357 p->signal->tty = NULL;
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003358 spin_unlock_irqrestore(&p->sighand->siglock, flags);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003359 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003360}
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003361
Alan Cox47f86832008-04-30 00:53:30 -07003362/* Called under the sighand lock */
3363
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003364static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003365{
3366 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -07003367 unsigned long flags;
3368 /* We should not have a session or pgrp to put here but.... */
3369 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -06003370 put_pid(tty->session);
3371 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003372 tty->pgrp = get_pid(task_pgrp(tsk));
Alan Cox47f86832008-04-30 00:53:30 -07003373 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3374 tty->session = get_pid(task_session(tsk));
Alan Cox9c9f4de2008-10-13 10:37:26 +01003375 if (tsk->signal->tty) {
3376 printk(KERN_DEBUG "tty not NULL!!\n");
3377 tty_kref_put(tsk->signal->tty);
3378 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003379 }
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003380 put_pid(tsk->signal->tty_old_pgrp);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003381 tsk->signal->tty = tty_kref_get(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003382 tsk->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003383}
3384
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07003385static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003386{
3387 spin_lock_irq(&tsk->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003388 __proc_set_tty(tsk, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003389 spin_unlock_irq(&tsk->sighand->siglock);
3390}
3391
3392struct tty_struct *get_current_tty(void)
3393{
3394 struct tty_struct *tty;
Alan Cox934e6eb2008-10-13 10:40:43 +01003395 unsigned long flags;
3396
3397 spin_lock_irqsave(&current->sighand->siglock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +01003398 tty = tty_kref_get(current->signal->tty);
Alan Cox934e6eb2008-10-13 10:40:43 +01003399 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003400 return tty;
3401}
Heiko Carstensa311f742006-12-13 00:33:41 -08003402EXPORT_SYMBOL_GPL(get_current_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Alan Coxd81ed102008-10-13 10:41:42 +01003404void tty_default_fops(struct file_operations *fops)
3405{
3406 *fops = tty_fops;
3407}
3408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409/*
3410 * Initialize the console device. This is called *early*, so
3411 * we can't necessarily depend on lots of kernel help here.
3412 * Just do some early initializations, and do the complex setup
3413 * later.
3414 */
3415void __init console_init(void)
3416{
3417 initcall_t *call;
3418
3419 /* Setup the default TTY line discipline. */
Alan Cox01e1abb2008-07-22 11:16:55 +01003420 tty_ldisc_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
3422 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08003423 * set up the console device so that later boot sequences can
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 * inform about problems etc..
3425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 call = __con_initcall_start;
3427 while (call < __con_initcall_end) {
3428 (*call)();
3429 call++;
3430 }
3431}
3432
Al Viro2c9ede52011-07-23 20:24:48 -04003433static char *tty_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +02003434{
3435 if (!mode)
3436 return NULL;
3437 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3438 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3439 *mode = 0666;
3440 return NULL;
3441}
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443static int __init tty_class_init(void)
3444{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003445 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 if (IS_ERR(tty_class))
3447 return PTR_ERR(tty_class);
Kay Sieverse454cea2009-09-18 23:01:12 +02003448 tty_class->devnode = tty_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 return 0;
3450}
3451
3452postcore_initcall(tty_class_init);
3453
3454/* 3/2004 jmc: why do these devices exist? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455static struct cdev tty_cdev, console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Kay Sieversfbc92a32010-12-01 18:51:05 +01003457static ssize_t show_cons_active(struct device *dev,
3458 struct device_attribute *attr, char *buf)
3459{
3460 struct console *cs[16];
3461 int i = 0;
3462 struct console *c;
3463 ssize_t count = 0;
3464
Torben Hohnac751ef2011-01-25 15:07:35 -08003465 console_lock();
Kay Sieversa2a6a822011-01-09 16:39:14 +01003466 for_each_console(c) {
Kay Sieversfbc92a32010-12-01 18:51:05 +01003467 if (!c->device)
3468 continue;
3469 if (!c->write)
3470 continue;
3471 if ((c->flags & CON_ENABLED) == 0)
3472 continue;
3473 cs[i++] = c;
3474 if (i >= ARRAY_SIZE(cs))
3475 break;
3476 }
3477 while (i--)
3478 count += sprintf(buf + count, "%s%d%c",
3479 cs[i]->name, cs[i]->index, i ? ' ':'\n');
Torben Hohnac751ef2011-01-25 15:07:35 -08003480 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01003481
3482 return count;
3483}
3484static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3485
3486static struct device *consdev;
3487
3488void console_sysfs_notify(void)
3489{
3490 if (consdev)
3491 sysfs_notify(&consdev->kobj, NULL, "active");
3492}
3493
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494/*
3495 * Ok, now we can initialize the rest of the tty devices and can count
3496 * on memory allocations, interrupts etc..
3497 */
David Howells31d1d482010-08-06 16:34:43 +01003498int __init tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
3500 cdev_init(&tty_cdev, &tty_fops);
3501 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3502 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3503 panic("Couldn't register /dev/tty driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003504 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
3506 cdev_init(&console_cdev, &console_fops);
3507 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3508 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3509 panic("Couldn't register /dev/console driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003510 consdev = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003511 "console");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003512 if (IS_ERR(consdev))
3513 consdev = NULL;
3514 else
Kay Sieversa2a6a822011-01-09 16:39:14 +01003515 WARN_ON(device_create_file(consdev, &dev_attr_active) < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517#ifdef CONFIG_VT
Alan Coxd81ed102008-10-13 10:41:42 +01003518 vty_init(&console_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519#endif
3520 return 0;
3521}
David Howells31d1d482010-08-06 16:34:43 +01003522