blob: cd5b829634eafaa65d8c6149c7435c37617608e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/tty_io.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
9 * or rs-channels. It also implements echoing, cooked mode etc.
10 *
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
12 *
13 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
14 * tty_struct and tty_queue structures. Previously there was an array
15 * of 256 tty_struct's which was statically allocated, and the
16 * tty_queue structures were allocated at boot time. Both are now
17 * dynamically allocated only when the tty is open.
18 *
19 * Also restructured routines so that there is more of a separation
20 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
21 * the low-level tty routines (serial.c, pty.c, console.c). This
Alan Cox37bdfb02008-02-08 04:18:47 -080022 * makes for cleaner and more compact code. -TYT, 9/17/92
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
24 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
25 * which can be dynamically activated and de-activated by the line
26 * discipline handling modules (like SLIP).
27 *
28 * NOTE: pay no attention to the line discipline code (yet); its
29 * interface is still subject to change in this version...
30 * -- TYT, 1/31/92
31 *
32 * Added functionality to the OPOST tty handling. No delays, but all
33 * other bits should be there.
34 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
35 *
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
38 *
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
41 *
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
Alan Cox37bdfb02008-02-08 04:18:47 -080044 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
47 *
48 * Move console and virtual terminal code to more appropriate files,
49 * implement CONFIG_VT and generalize console device interface.
50 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
51 *
Alan Coxd81ed102008-10-13 10:41:42 +010052 * Rewrote tty_init_dev and tty_release_dev to eliminate races.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * -- Bill Hawes <whawes@star.net>, June 97
54 *
55 * Added devfs support.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
57 *
58 * Added support for a Unix98-style ptmx device.
59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
60 *
61 * Reduced memory usage for older ARM systems
62 * -- Russell King <rmk@arm.linux.org.uk>
63 *
64 * Move do_SAK() into process context. Less stack use in devfs functions.
Alan Cox37bdfb02008-02-08 04:18:47 -080065 * alloc_tty_struct() always uses kmalloc()
66 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/types.h>
70#include <linux/major.h>
71#include <linux/errno.h>
72#include <linux/signal.h>
73#include <linux/fcntl.h>
74#include <linux/sched.h>
75#include <linux/interrupt.h>
76#include <linux/tty.h>
77#include <linux/tty_driver.h>
78#include <linux/tty_flip.h>
79#include <linux/devpts_fs.h>
80#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040081#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/console.h>
83#include <linux/timer.h>
84#include <linux/ctype.h>
85#include <linux/kd.h>
86#include <linux/mm.h>
87#include <linux/string.h>
88#include <linux/slab.h>
89#include <linux/poll.h>
90#include <linux/proc_fs.h>
91#include <linux/init.h>
92#include <linux/module.h>
93#include <linux/smp_lock.h>
94#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/wait.h>
96#include <linux/bitops.h>
Domen Puncerb20f3ae2005-06-25 14:58:42 -070097#include <linux/delay.h>
Alan Coxa352def2008-07-16 21:53:12 +010098#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Alan Coxa352def2008-07-16 21:53:12 +0100100#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include <asm/system.h>
102
103#include <linux/kbd_kern.h>
104#include <linux/vt_kern.h>
105#include <linux/selection.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107#include <linux/kmod.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700108#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110#undef TTY_DEBUG_HANGUP
111
112#define TTY_PARANOIA_CHECK 1
113#define CHECK_TTY_COUNT 1
114
Alan Coxedc6afc2006-12-08 02:38:44 -0800115struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .c_iflag = ICRNL | IXON,
117 .c_oflag = OPOST | ONLCR,
118 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
119 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
120 ECHOCTL | ECHOKE | IEXTEN,
Alan Coxedc6afc2006-12-08 02:38:44 -0800121 .c_cc = INIT_C_CC,
122 .c_ispeed = 38400,
123 .c_ospeed = 38400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126EXPORT_SYMBOL(tty_std_termios);
127
128/* This list gets poked at by procfs and various bits of boot up code. This
129 could do with some rationalisation such as pulling the tty proc function
130 into this file */
Alan Cox37bdfb02008-02-08 04:18:47 -0800131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132LIST_HEAD(tty_drivers); /* linked list of tty drivers */
133
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800134/* Mutex to protect creating and releasing a tty. This is shared with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 vt.c for deeply disgusting hack reasons */
Ingo Molnar70522e12006-03-23 03:00:31 -0800136DEFINE_MUTEX(tty_mutex);
Alan Coxde2a84f2006-09-29 02:00:57 -0700137EXPORT_SYMBOL(tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Nick Pigginee2ffa02010-08-18 04:37:35 +1000139/* Spinlock to protect the tty->tty_files list */
140DEFINE_SPINLOCK(tty_files_lock);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
143static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
Alan Cox37bdfb02008-02-08 04:18:47 -0800144ssize_t redirected_tty_write(struct file *, const char __user *,
145 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static unsigned int tty_poll(struct file *, poll_table *);
147static int tty_open(struct inode *, struct file *);
Alan Cox04f378b2008-04-30 00:53:29 -0700148long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700149#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -0800150static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700151 unsigned long arg);
152#else
153#define tty_compat_ioctl NULL
154#endif
Arnd Bergmannec79d602010-06-01 22:53:01 +0200155static int __tty_fasync(int fd, struct file *filp, int on);
Alan Cox37bdfb02008-02-08 04:18:47 -0800156static int tty_fasync(int fd, struct file *filp, int on);
Christoph Hellwigd5698c22007-02-10 01:46:46 -0800157static void release_tty(struct tty_struct *tty, int idx);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700158static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700159static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Alan Coxaf9b8972006-08-27 01:24:01 -0700161/**
162 * alloc_tty_struct - allocate a tty object
163 *
164 * Return a new empty tty structure. The data fields have not
165 * been initialized in any way but has been zeroed
166 *
167 * Locking: none
Alan Coxaf9b8972006-08-27 01:24:01 -0700168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Alan Coxbf970ee2008-10-13 10:42:39 +0100170struct tty_struct *alloc_tty_struct(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Alan Cox1266b1e2006-09-29 02:00:40 -0700172 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Alan Coxaf9b8972006-08-27 01:24:01 -0700175/**
176 * free_tty_struct - free a disused tty
177 * @tty: tty struct to free
178 *
179 * Free the write buffers, tty queue and tty memory itself.
180 *
181 * Locking: none. Must be called after tty is definitely unused
182 */
183
Alan Coxbf970ee2008-10-13 10:42:39 +0100184void free_tty_struct(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 kfree(tty->write_buf);
Alan Cox33f0f882006-01-09 20:54:13 -0800187 tty_buffer_free_all(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kfree(tty);
189}
190
191#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
192
Alan Coxaf9b8972006-08-27 01:24:01 -0700193/**
194 * tty_name - return tty naming
195 * @tty: tty structure
196 * @buf: buffer for output
197 *
198 * Convert a tty structure into a name. The name reflects the kernel
199 * naming policy and if udev is in use may not reflect user space
200 *
201 * Locking: none
202 */
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204char *tty_name(struct tty_struct *tty, char *buf)
205{
206 if (!tty) /* Hmm. NULL pointer. That's fun. */
207 strcpy(buf, "NULL tty");
208 else
209 strcpy(buf, tty->name);
210 return buf;
211}
212
213EXPORT_SYMBOL(tty_name);
214
Andrew Mortond769a662005-05-05 16:15:50 -0700215int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 const char *routine)
217{
218#ifdef TTY_PARANOIA_CHECK
219 if (!tty) {
220 printk(KERN_WARNING
221 "null TTY for (%d:%d) in %s\n",
222 imajor(inode), iminor(inode), routine);
223 return 1;
224 }
225 if (tty->magic != TTY_MAGIC) {
226 printk(KERN_WARNING
227 "bad magic number for tty struct (%d:%d) in %s\n",
228 imajor(inode), iminor(inode), routine);
229 return 1;
230 }
231#endif
232 return 0;
233}
234
235static int check_tty_count(struct tty_struct *tty, const char *routine)
236{
237#ifdef CHECK_TTY_COUNT
238 struct list_head *p;
239 int count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800240
Nick Pigginee2ffa02010-08-18 04:37:35 +1000241 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 list_for_each(p, &tty->tty_files) {
243 count++;
244 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000245 spin_unlock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
247 tty->driver->subtype == PTY_TYPE_SLAVE &&
248 tty->link && tty->link->count)
249 count++;
250 if (tty->count != count) {
251 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
252 "!= #fd's(%d) in %s\n",
253 tty->name, tty->count, count, routine);
254 return count;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#endif
257 return 0;
258}
259
Alan Coxaf9b8972006-08-27 01:24:01 -0700260/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700261 * get_tty_driver - find device of a tty
262 * @dev_t: device identifier
263 * @index: returns the index of the tty
264 *
265 * This routine returns a tty driver structure, given a device number
266 * and also passes back the index number.
267 *
268 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static struct tty_driver *get_tty_driver(dev_t device, int *index)
272{
273 struct tty_driver *p;
274
275 list_for_each_entry(p, &tty_drivers, tty_drivers) {
276 dev_t base = MKDEV(p->major, p->minor_start);
277 if (device < base || device >= base + p->num)
278 continue;
279 *index = device - base;
Alan Cox7d7b93c2008-10-13 10:42:09 +0100280 return tty_driver_kref_get(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 return NULL;
283}
284
Jason Wesself2d937f2008-04-17 20:05:37 +0200285#ifdef CONFIG_CONSOLE_POLL
286
287/**
288 * tty_find_polling_driver - find device of a polled tty
289 * @name: name string to match
290 * @line: pointer to resulting tty line nr
291 *
292 * This routine returns a tty driver structure, given a name
293 * and the condition that the tty driver is capable of polled
294 * operation.
295 */
296struct tty_driver *tty_find_polling_driver(char *name, int *line)
297{
298 struct tty_driver *p, *res = NULL;
299 int tty_line = 0;
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500300 int len;
Alan Cox5f0878a2009-06-11 12:46:41 +0100301 char *str, *stp;
Jason Wesself2d937f2008-04-17 20:05:37 +0200302
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500303 for (str = name; *str; str++)
304 if ((*str >= '0' && *str <= '9') || *str == ',')
305 break;
306 if (!*str)
307 return NULL;
308
309 len = str - name;
310 tty_line = simple_strtoul(str, &str, 10);
311
Jason Wesself2d937f2008-04-17 20:05:37 +0200312 mutex_lock(&tty_mutex);
313 /* Search through the tty devices to look for a match */
314 list_for_each_entry(p, &tty_drivers, tty_drivers) {
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500315 if (strncmp(name, p->name, len) != 0)
316 continue;
Alan Cox5f0878a2009-06-11 12:46:41 +0100317 stp = str;
318 if (*stp == ',')
319 stp++;
320 if (*stp == '\0')
321 stp = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +0200322
Alan Coxf34d7a52008-04-30 00:54:13 -0700323 if (tty_line >= 0 && tty_line <= p->num && p->ops &&
Alan Cox5f0878a2009-06-11 12:46:41 +0100324 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
Alan Cox7d7b93c2008-10-13 10:42:09 +0100325 res = tty_driver_kref_get(p);
Jason Wesself2d937f2008-04-17 20:05:37 +0200326 *line = tty_line;
327 break;
328 }
329 }
330 mutex_unlock(&tty_mutex);
331
332 return res;
333}
334EXPORT_SYMBOL_GPL(tty_find_polling_driver);
335#endif
336
Alan Coxaf9b8972006-08-27 01:24:01 -0700337/**
338 * tty_check_change - check for POSIX terminal changes
339 * @tty: tty to check
340 *
341 * If we try to write to, or set the state of, a terminal and we're
342 * not in the foreground, send a SIGTTOU. If the signal is blocked or
343 * ignored, go ahead and perform the operation. (POSIX 7.2)
344 *
Alan Cox978e5952008-04-30 00:53:59 -0700345 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700347
Alan Cox37bdfb02008-02-08 04:18:47 -0800348int tty_check_change(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Alan Cox47f86832008-04-30 00:53:30 -0700350 unsigned long flags;
351 int ret = 0;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (current->signal->tty != tty)
354 return 0;
Alan Cox47f86832008-04-30 00:53:30 -0700355
356 spin_lock_irqsave(&tty->ctrl_lock, flags);
357
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800358 if (!tty->pgrp) {
359 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700360 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800362 if (task_pgrp(current) == tty->pgrp)
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700363 goto out_unlock;
364 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (is_ignored(SIGTTOU))
Alan Cox47f86832008-04-30 00:53:30 -0700366 goto out;
367 if (is_current_pgrp_orphaned()) {
368 ret = -EIO;
369 goto out;
370 }
Oleg Nesterov040b6362007-06-01 00:46:53 -0700371 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
372 set_thread_flag(TIF_SIGPENDING);
Alan Cox47f86832008-04-30 00:53:30 -0700373 ret = -ERESTARTSYS;
374out:
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700375 return ret;
376out_unlock:
Alan Cox47f86832008-04-30 00:53:30 -0700377 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
378 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381EXPORT_SYMBOL(tty_check_change);
382
Alan Cox37bdfb02008-02-08 04:18:47 -0800383static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 size_t count, loff_t *ppos)
385{
386 return 0;
387}
388
Alan Cox37bdfb02008-02-08 04:18:47 -0800389static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 size_t count, loff_t *ppos)
391{
392 return -EIO;
393}
394
395/* No kernel lock held - none needed ;) */
Alan Cox37bdfb02008-02-08 04:18:47 -0800396static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
399}
400
Alan Cox04f378b2008-04-30 00:53:29 -0700401static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
402 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700403{
404 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
405}
406
Alan Cox37bdfb02008-02-08 04:18:47 -0800407static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700408 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
411}
412
Arjan van de Ven62322d22006-07-03 00:24:21 -0700413static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 .llseek = no_llseek,
415 .read = tty_read,
416 .write = tty_write,
417 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700418 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700419 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 .open = tty_open,
421 .release = tty_release,
422 .fasync = tty_fasync,
423};
424
Arjan van de Ven62322d22006-07-03 00:24:21 -0700425static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 .llseek = no_llseek,
427 .read = tty_read,
428 .write = redirected_tty_write,
429 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700430 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700431 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 .open = tty_open,
433 .release = tty_release,
434 .fasync = tty_fasync,
435};
436
Arjan van de Ven62322d22006-07-03 00:24:21 -0700437static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 .llseek = no_llseek,
439 .read = hung_up_tty_read,
440 .write = hung_up_tty_write,
441 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700442 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700443 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 .release = tty_release,
445};
446
447static DEFINE_SPINLOCK(redirect_lock);
448static struct file *redirect;
449
450/**
451 * tty_wakeup - request more data
452 * @tty: terminal
453 *
454 * Internal and external helper for wakeups of tty. This function
455 * informs the line discipline if present that the driver is ready
456 * to receive more output data.
457 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459void tty_wakeup(struct tty_struct *tty)
460{
461 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -0800462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
464 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800465 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100466 if (ld->ops->write_wakeup)
467 ld->ops->write_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 tty_ldisc_deref(ld);
469 }
470 }
Davide Libenzi4b194492009-03-31 15:24:24 -0700471 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474EXPORT_SYMBOL_GPL(tty_wakeup);
475
476/**
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200477 * __tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +0000478 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -0700479 *
Alan Cox1bad8792008-07-22 23:38:04 +0100480 * This can be called by the "eventd" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -0700481 * synchronous but doesn't hold any locks, so we need to make sure we
482 * have the appropriate locks for what we're doing.
483 *
484 * The hangup event clears any pending redirections onto the hung up
485 * device. It ensures future writes will error and it does the needed
486 * line discipline hangup and signal delivery. The tty object itself
487 * remains intact.
488 *
489 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200490 * BTM
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800491 * redirect lock for undoing redirection
492 * file list lock for manipulating list of ttys
493 * tty_ldisc_lock from called functions
494 * termios_mutex resetting termios data
495 * tasklist_lock to walk task list for hangup event
496 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200498void __tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Alan Cox37bdfb02008-02-08 04:18:47 -0800500 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct file *filp, *f = NULL;
502 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 int closecount = 0, n;
Alan Cox47f86832008-04-30 00:53:30 -0700504 unsigned long flags;
Alan Cox9c9f4de2008-10-13 10:37:26 +0100505 int refs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (!tty)
508 return;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 spin_lock(&redirect_lock);
512 if (redirect && redirect->private_data == tty) {
513 f = redirect;
514 redirect = NULL;
515 }
516 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800517
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200518 tty_lock();
519
Arnd Bergmannec79d602010-06-01 22:53:01 +0200520 /* inuse_filps is protected by the single tty lock,
521 this really needs to change if we want to flush the
522 workqueue with the lock held */
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200523 check_tty_count(tty, "tty_hangup");
Alan Cox36ba7822009-11-30 13:18:51 +0000524
Nick Pigginee2ffa02010-08-18 04:37:35 +1000525 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Eric Dumazet2f512012005-10-30 15:02:16 -0800527 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (filp->f_op->write == redirected_tty_write)
529 cons_filp = filp;
530 if (filp->f_op->write != tty_write)
531 continue;
532 closecount++;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200533 __tty_fasync(-1, filp, 0); /* can't block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 filp->f_op = &hung_up_tty_fops;
535 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000536 spin_unlock(&tty_files_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800537
Alan Coxc65c9bc2009-06-11 12:50:12 +0100538 tty_ldisc_hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800541 if (tty->session) {
542 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800543 spin_lock_irq(&p->sighand->siglock);
Alan Cox9c9f4de2008-10-13 10:37:26 +0100544 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 p->signal->tty = NULL;
Alan Cox9c9f4de2008-10-13 10:37:26 +0100546 /* We defer the dereferences outside fo
547 the tasklist lock */
548 refs++;
549 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800550 if (!p->signal->leader) {
551 spin_unlock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 continue;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800553 }
554 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
555 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800556 put_pid(p->signal->tty_old_pgrp); /* A noop */
Alan Cox47f86832008-04-30 00:53:30 -0700557 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800558 if (tty->pgrp)
559 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
Alan Cox47f86832008-04-30 00:53:30 -0700560 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800561 spin_unlock_irq(&p->sighand->siglock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800562 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 read_unlock(&tasklist_lock);
565
Alan Cox47f86832008-04-30 00:53:30 -0700566 spin_lock_irqsave(&tty->ctrl_lock, flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100567 clear_bit(TTY_THROTTLED, &tty->flags);
568 clear_bit(TTY_PUSH, &tty->flags);
569 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -0600570 put_pid(tty->session);
571 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800572 tty->session = NULL;
573 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 tty->ctrl_status = 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100575 set_bit(TTY_HUPPED, &tty->flags);
Alan Cox47f86832008-04-30 00:53:30 -0700576 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
577
Alan Cox9c9f4de2008-10-13 10:37:26 +0100578 /* Account for the p->signal references we killed */
579 while (refs--)
580 tty_kref_put(tty);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /*
Alan Cox37bdfb02008-02-08 04:18:47 -0800583 * If one of the devices matches a console pointer, we
584 * cannot just call hangup() because that will cause
585 * tty->count and state->count to go out of sync.
586 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
588 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -0700589 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -0700591 tty->ops->close(tty, cons_filp);
592 } else if (tty->ops->hangup)
593 (tty->ops->hangup)(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800594 /*
595 * We don't want to have driver/ldisc interactions beyond
596 * the ones we did here. The driver layer expects no
597 * calls after ->hangup() from the ldisc side. However we
598 * can't yet guarantee all that.
599 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 set_bit(TTY_HUPPED, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100601 tty_ldisc_enable(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200602
603 tty_unlock();
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (f)
606 fput(f);
607}
608
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200609static void do_tty_hangup(struct work_struct *work)
610{
611 struct tty_struct *tty =
612 container_of(work, struct tty_struct, hangup_work);
613
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200614 __tty_hangup(tty);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200615}
616
Alan Coxaf9b8972006-08-27 01:24:01 -0700617/**
618 * tty_hangup - trigger a hangup event
619 * @tty: tty to hangup
620 *
621 * A carrier loss (virtual or otherwise) has occurred on this like
622 * schedule a hangup sequence to run after this event.
623 */
624
Alan Cox37bdfb02008-02-08 04:18:47 -0800625void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627#ifdef TTY_DEBUG_HANGUP
628 char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
630#endif
631 schedule_work(&tty->hangup_work);
632}
633
634EXPORT_SYMBOL(tty_hangup);
635
Alan Coxaf9b8972006-08-27 01:24:01 -0700636/**
637 * tty_vhangup - process vhangup
638 * @tty: tty to hangup
639 *
640 * The user has asked via system call for the terminal to be hung up.
641 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200642 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -0700643 */
644
Alan Cox37bdfb02008-02-08 04:18:47 -0800645void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647#ifdef TTY_DEBUG_HANGUP
648 char buf[64];
649
650 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
651#endif
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200652 __tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
Alan Cox37bdfb02008-02-08 04:18:47 -0800654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655EXPORT_SYMBOL(tty_vhangup);
656
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200657
Alan Coxaf9b8972006-08-27 01:24:01 -0700658/**
Alan Cox2cb59982008-10-13 10:40:30 +0100659 * tty_vhangup_self - process vhangup for own ctty
660 *
661 * Perform a vhangup on the current controlling tty
662 */
663
664void tty_vhangup_self(void)
665{
666 struct tty_struct *tty;
667
Alan Cox2cb59982008-10-13 10:40:30 +0100668 tty = get_current_tty();
669 if (tty) {
670 tty_vhangup(tty);
671 tty_kref_put(tty);
672 }
Alan Cox2cb59982008-10-13 10:40:30 +0100673}
674
675/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700676 * tty_hung_up_p - was tty hung up
677 * @filp: file pointer of tty
678 *
679 * Return true if the tty has been subject to a vhangup or a carrier
680 * loss
681 */
682
Alan Cox37bdfb02008-02-08 04:18:47 -0800683int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 return (filp->f_op == &hung_up_tty_fops);
686}
687
688EXPORT_SYMBOL(tty_hung_up_p);
689
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800690static void session_clear_tty(struct pid *session)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800691{
692 struct task_struct *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800693 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800694 proc_clear_tty(p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800695 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800696}
697
Alan Coxaf9b8972006-08-27 01:24:01 -0700698/**
699 * disassociate_ctty - disconnect controlling tty
700 * @on_exit: true if exiting so need to "hang up" the session
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700702 * This function is typically called only by the session leader, when
703 * it wants to disassociate itself from its controlling tty.
704 *
705 * It performs the following functions:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
707 * (2) Clears the tty from being controlling the session
708 * (3) Clears the controlling tty for all processes in the
709 * session group.
710 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700711 * The argument on_exit is set to 1 if called when a process is
712 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
713 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800714 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200715 * BTM is taken for hysterical raisins, and held when
716 * called from no_tty().
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800717 * tty_mutex is taken to protect tty
718 * ->siglock is taken to protect ->signal/->sighand
719 * tasklist_lock is taken to walk process list for sessions
720 * ->siglock is taken to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723void disassociate_ctty(int on_exit)
724{
725 struct tty_struct *tty;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800726 struct pid *tty_pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Alan Cox5ec93d12009-11-30 13:18:45 +0000728 if (!current->signal->leader)
729 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800731 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800733 tty_pgrp = get_pid(tty->pgrp);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200734 if (on_exit) {
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200735 if (tty->driver->type != TTY_DRIVER_TYPE_PTY)
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200736 tty_vhangup(tty);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200737 }
Alan Cox452a00d2008-10-13 10:39:13 +0100738 tty_kref_put(tty);
Eric W. Biederman680a9672007-02-12 00:52:52 -0800739 } else if (on_exit) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800740 struct pid *old_pgrp;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800741 spin_lock_irq(&current->sighand->siglock);
742 old_pgrp = current->signal->tty_old_pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800743 current->signal->tty_old_pgrp = NULL;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800744 spin_unlock_irq(&current->sighand->siglock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800745 if (old_pgrp) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800746 kill_pgrp(old_pgrp, SIGHUP, on_exit);
747 kill_pgrp(old_pgrp, SIGCONT, on_exit);
748 put_pid(old_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return;
751 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800752 if (tty_pgrp) {
753 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (!on_exit)
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800755 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
756 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800759 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700760 put_pid(current->signal->tty_old_pgrp);
Randy Dunlap23cac8d2007-02-20 13:58:05 -0800761 current->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800762 spin_unlock_irq(&current->sighand->siglock);
763
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800764 tty = get_current_tty();
765 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -0700766 unsigned long flags;
767 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800768 put_pid(tty->session);
769 put_pid(tty->pgrp);
770 tty->session = NULL;
771 tty->pgrp = NULL;
Alan Cox47f86832008-04-30 00:53:30 -0700772 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +0100773 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800774 } else {
775#ifdef TTY_DEBUG_HANGUP
776 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
777 " = NULL", tty);
778#endif
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /* Now clear signal->tty under the lock */
782 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800783 session_clear_tty(task_session(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700787/**
788 *
789 * no_tty - Ensure the current process does not have a controlling tty
790 */
791void no_tty(void)
792{
793 struct task_struct *tsk = current;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200794 tty_lock();
Alan Cox5ec93d12009-11-30 13:18:45 +0000795 disassociate_ctty(0);
Arnd Bergmannec79d602010-06-01 22:53:01 +0200796 tty_unlock();
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700797 proc_clear_tty(tsk);
798}
799
Alan Coxaf9b8972006-08-27 01:24:01 -0700800
801/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200802 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700803 * @tty: tty to stop
804 *
805 * Perform flow control to the driver. For PTY/TTY pairs we
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200806 * must also propagate the TIOCKPKT status. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -0700807 * on an already stopped device and will not re-call the driver
808 * method.
809 *
810 * This functionality is used by both the line disciplines for
811 * halting incoming flow and by the driver. It may therefore be
812 * called from any context, may be under the tty atomic_write_lock
813 * but not always.
814 *
815 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700816 * Uses the tty control lock internally
Alan Coxaf9b8972006-08-27 01:24:01 -0700817 */
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819void stop_tty(struct tty_struct *tty)
820{
Alan Cox04f378b2008-04-30 00:53:29 -0700821 unsigned long flags;
822 spin_lock_irqsave(&tty->ctrl_lock, flags);
823 if (tty->stopped) {
824 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 tty->stopped = 1;
828 if (tty->link && tty->link->packet) {
829 tty->ctrl_status &= ~TIOCPKT_START;
830 tty->ctrl_status |= TIOCPKT_STOP;
Davide Libenzi4b194492009-03-31 15:24:24 -0700831 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Alan Cox04f378b2008-04-30 00:53:29 -0700833 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700834 if (tty->ops->stop)
835 (tty->ops->stop)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838EXPORT_SYMBOL(stop_tty);
839
Alan Coxaf9b8972006-08-27 01:24:01 -0700840/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200841 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700842 * @tty: tty to start
843 *
844 * Start a tty that has been stopped if at all possible. Perform
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200845 * any necessary wakeups and propagate the TIOCPKT status. If this
Alan Coxaf9b8972006-08-27 01:24:01 -0700846 * is the tty was previous stopped and is being started then the
847 * driver start method is invoked and the line discipline woken.
848 *
849 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700850 * ctrl_lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700851 */
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853void start_tty(struct tty_struct *tty)
854{
Alan Cox04f378b2008-04-30 00:53:29 -0700855 unsigned long flags;
856 spin_lock_irqsave(&tty->ctrl_lock, flags);
857 if (!tty->stopped || tty->flow_stopped) {
858 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 tty->stopped = 0;
862 if (tty->link && tty->link->packet) {
863 tty->ctrl_status &= ~TIOCPKT_STOP;
864 tty->ctrl_status |= TIOCPKT_START;
Davide Libenzi4b194492009-03-31 15:24:24 -0700865 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Alan Cox04f378b2008-04-30 00:53:29 -0700867 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700868 if (tty->ops->start)
869 (tty->ops->start)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* If we have a running line discipline it may need kicking */
871 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874EXPORT_SYMBOL(start_tty);
875
Alan Coxaf9b8972006-08-27 01:24:01 -0700876/**
877 * tty_read - read method for tty device files
878 * @file: pointer to tty file
879 * @buf: user buffer
880 * @count: size of user buffer
881 * @ppos: unused
882 *
883 * Perform the read system call function on this terminal device. Checks
884 * for hung up devices before calling the line discipline method.
885 *
886 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -0700887 * Locks the line discipline internally while needed. Multiple
888 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -0700889 */
890
Alan Cox37bdfb02008-02-08 04:18:47 -0800891static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 loff_t *ppos)
893{
894 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -0800895 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 struct inode *inode;
897 struct tty_ldisc *ld;
898
Kulikov Vasiliy2b50d242010-06-29 14:15:09 +0400899 tty = file->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -0800900 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (tty_paranoia_check(tty, inode, "tty_read"))
902 return -EIO;
903 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
904 return -EIO;
905
906 /* We want to wait for the line discipline to sort out in this
907 situation */
908 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +0100909 if (ld->ops->read)
910 i = (ld->ops->read)(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 else
912 i = -EIO;
913 tty_ldisc_deref(ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (i > 0)
915 inode->i_atime = current_fs_time(inode->i_sb);
916 return i;
917}
918
Alan Cox9c1729d2007-07-15 23:39:43 -0700919void tty_write_unlock(struct tty_struct *tty)
920{
921 mutex_unlock(&tty->atomic_write_lock);
Davide Libenzi4b194492009-03-31 15:24:24 -0700922 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Alan Cox9c1729d2007-07-15 23:39:43 -0700923}
924
925int tty_write_lock(struct tty_struct *tty, int ndelay)
926{
927 if (!mutex_trylock(&tty->atomic_write_lock)) {
928 if (ndelay)
929 return -EAGAIN;
930 if (mutex_lock_interruptible(&tty->atomic_write_lock))
931 return -ERESTARTSYS;
932 }
933 return 0;
934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/*
937 * Split writes up in sane blocksizes to avoid
938 * denial-of-service type attacks
939 */
940static inline ssize_t do_tty_write(
941 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
942 struct tty_struct *tty,
943 struct file *file,
944 const char __user *buf,
945 size_t count)
946{
Alan Cox9c1729d2007-07-15 23:39:43 -0700947 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -0800949
Alan Cox9c1729d2007-07-15 23:39:43 -0700950 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
951 if (ret < 0)
952 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 /*
955 * We chunk up writes into a temporary buffer. This
956 * simplifies low-level drivers immensely, since they
957 * don't have locking issues and user mode accesses.
958 *
959 * But if TTY_NO_WRITE_SPLIT is set, we should use a
960 * big chunk-size..
961 *
962 * The default chunk-size is 2kB, because the NTTY
963 * layer has problems with bigger chunks. It will
964 * claim to be able to handle more characters than
965 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -0700966 *
967 * FIXME: This can probably go away now except that 64K chunks
968 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
970 chunk = 2048;
971 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
972 chunk = 65536;
973 if (count < chunk)
974 chunk = count;
975
Ingo Molnar70522e12006-03-23 03:00:31 -0800976 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (tty->write_cnt < chunk) {
Jason Wessel402fda92008-10-13 10:45:36 +0100978 unsigned char *buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (chunk < 1024)
981 chunk = 1024;
982
Jason Wessel402fda92008-10-13 10:45:36 +0100983 buf_chunk = kmalloc(chunk, GFP_KERNEL);
984 if (!buf_chunk) {
Alan Cox9c1729d2007-07-15 23:39:43 -0700985 ret = -ENOMEM;
986 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 kfree(tty->write_buf);
989 tty->write_cnt = chunk;
Jason Wessel402fda92008-10-13 10:45:36 +0100990 tty->write_buf = buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
993 /* Do the write .. */
994 for (;;) {
995 size_t size = count;
996 if (size > chunk)
997 size = chunk;
998 ret = -EFAULT;
999 if (copy_from_user(tty->write_buf, buf, size))
1000 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (ret <= 0)
1003 break;
1004 written += ret;
1005 buf += ret;
1006 count -= ret;
1007 if (!count)
1008 break;
1009 ret = -ERESTARTSYS;
1010 if (signal_pending(current))
1011 break;
1012 cond_resched();
1013 }
1014 if (written) {
Josef Sipeka7113a92006-12-08 02:36:55 -08001015 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 inode->i_mtime = current_fs_time(inode->i_sb);
1017 ret = written;
1018 }
Alan Cox9c1729d2007-07-15 23:39:43 -07001019out:
1020 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return ret;
1022}
1023
Alan Cox95f9bfc2008-10-13 10:39:23 +01001024/**
1025 * tty_write_message - write a message to a certain tty, not just the console.
1026 * @tty: the destination tty_struct
1027 * @msg: the message to write
1028 *
1029 * This is used for messages that need to be redirected to a specific tty.
1030 * We don't put it into the syslog queue right now maybe in the future if
1031 * really needed.
1032 *
Arnd Bergmannec79d602010-06-01 22:53:01 +02001033 * We must still hold the BTM and test the CLOSING flag for the moment.
Alan Cox95f9bfc2008-10-13 10:39:23 +01001034 */
1035
1036void tty_write_message(struct tty_struct *tty, char *msg)
1037{
Alan Cox95f9bfc2008-10-13 10:39:23 +01001038 if (tty) {
1039 mutex_lock(&tty->atomic_write_lock);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001040 tty_lock();
Alan Coxeeb89d92009-11-30 13:18:29 +00001041 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001042 tty_unlock();
Alan Cox95f9bfc2008-10-13 10:39:23 +01001043 tty->ops->write(tty, msg, strlen(msg));
Alan Coxeeb89d92009-11-30 13:18:29 +00001044 } else
Arnd Bergmannec79d602010-06-01 22:53:01 +02001045 tty_unlock();
Alan Cox95f9bfc2008-10-13 10:39:23 +01001046 tty_write_unlock(tty);
1047 }
Alan Cox95f9bfc2008-10-13 10:39:23 +01001048 return;
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Alan Coxaf9b8972006-08-27 01:24:01 -07001052/**
1053 * tty_write - write method for tty device file
1054 * @file: tty file pointer
1055 * @buf: user data to write
1056 * @count: bytes to write
1057 * @ppos: unused
1058 *
1059 * Write data to a tty device via the line discipline.
1060 *
1061 * Locking:
1062 * Locks the line discipline as required
1063 * Writes to the tty driver are serialized by the atomic_write_lock
1064 * and are then processed in chunks to the device. The line discipline
Joe Petersona88a69c2009-01-02 13:40:53 +00001065 * write method will not be invoked in parallel for each device.
Alan Coxaf9b8972006-08-27 01:24:01 -07001066 */
1067
Alan Cox37bdfb02008-02-08 04:18:47 -08001068static ssize_t tty_write(struct file *file, const char __user *buf,
1069 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Alan Cox37bdfb02008-02-08 04:18:47 -08001071 struct tty_struct *tty;
Josef Sipeka7113a92006-12-08 02:36:55 -08001072 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ssize_t ret;
1074 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08001075
Kulikov Vasiliy2b50d242010-06-29 14:15:09 +04001076 tty = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (tty_paranoia_check(tty, inode, "tty_write"))
1078 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001079 if (!tty || !tty->ops->write ||
Alan Cox37bdfb02008-02-08 04:18:47 -08001080 (test_bit(TTY_IO_ERROR, &tty->flags)))
1081 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001082 /* Short term debug to catch buggy drivers */
1083 if (tty->ops->write_room == NULL)
1084 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1085 tty->driver->name);
Alan Cox37bdfb02008-02-08 04:18:47 -08001086 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001087 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 ret = -EIO;
1089 else
Alan Coxa352def2008-07-16 21:53:12 +01001090 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 tty_ldisc_deref(ld);
1092 return ret;
1093}
1094
Alan Cox37bdfb02008-02-08 04:18:47 -08001095ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1096 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 struct file *p = NULL;
1099
1100 spin_lock(&redirect_lock);
1101 if (redirect) {
1102 get_file(redirect);
1103 p = redirect;
1104 }
1105 spin_unlock(&redirect_lock);
1106
1107 if (p) {
1108 ssize_t res;
1109 res = vfs_write(p, buf, count, &p->f_pos);
1110 fput(p);
1111 return res;
1112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return tty_write(file, buf, count, ppos);
1114}
1115
1116static char ptychar[] = "pqrstuvwxyzabcde";
1117
Alan Coxaf9b8972006-08-27 01:24:01 -07001118/**
1119 * pty_line_name - generate name for a pty
1120 * @driver: the tty driver in use
1121 * @index: the minor number
1122 * @p: output buffer of at least 6 bytes
1123 *
1124 * Generate a name from a driver reference and write it to the output
1125 * buffer.
1126 *
1127 * Locking: None
1128 */
1129static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 int i = index + driver->name_base;
1132 /* ->name is initialized to "ttyp", but "tty" is expected */
1133 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08001134 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1135 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Alan Coxaf9b8972006-08-27 01:24:01 -07001138/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001139 * tty_line_name - generate name for a tty
Alan Coxaf9b8972006-08-27 01:24:01 -07001140 * @driver: the tty driver in use
1141 * @index: the minor number
1142 * @p: output buffer of at least 7 bytes
1143 *
1144 * Generate a name from a driver reference and write it to the output
1145 * buffer.
1146 *
1147 * Locking: None
1148 */
1149static void tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1152}
1153
Alan Cox99f1fe12008-10-13 10:42:00 +01001154/**
1155 * tty_driver_lookup_tty() - find an existing tty, if any
1156 * @driver: the driver for the tty
1157 * @idx: the minor number
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001158 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001159 * Return the tty, if found or ERR_PTR() otherwise.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001160 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001161 * Locking: tty_mutex must be held. If tty is found, the mutex must
1162 * be held until the 'fast-open' is also done. Will change once we
1163 * have refcounting in the driver and per driver locking
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001164 */
Jason Wessela47d5452009-01-02 13:43:04 +00001165static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001166 struct inode *inode, int idx)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001167{
1168 struct tty_struct *tty;
1169
Alan Cox99f1fe12008-10-13 10:42:00 +01001170 if (driver->ops->lookup)
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001171 return driver->ops->lookup(driver, inode, idx);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001172
Alan Cox8b0a88d2008-10-13 10:42:19 +01001173 tty = driver->ttys[idx];
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001174 return tty;
1175}
1176
Alan Cox99f1fe12008-10-13 10:42:00 +01001177/**
Alan Coxbf970ee2008-10-13 10:42:39 +01001178 * tty_init_termios - helper for termios setup
1179 * @tty: the tty to set up
1180 *
1181 * Initialise the termios structures for this tty. Thus runs under
1182 * the tty_mutex currently so we can be relaxed about ordering.
1183 */
1184
1185int tty_init_termios(struct tty_struct *tty)
1186{
Alan Coxfe6e29f2008-10-13 10:44:08 +01001187 struct ktermios *tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001188 int idx = tty->index;
1189
1190 tp = tty->driver->termios[idx];
Alan Coxbf970ee2008-10-13 10:42:39 +01001191 if (tp == NULL) {
Alan Coxfe6e29f2008-10-13 10:44:08 +01001192 tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
1193 if (tp == NULL)
Alan Coxbf970ee2008-10-13 10:42:39 +01001194 return -ENOMEM;
Alan Coxbf970ee2008-10-13 10:42:39 +01001195 memcpy(tp, &tty->driver->init_termios,
1196 sizeof(struct ktermios));
1197 tty->driver->termios[idx] = tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001198 }
1199 tty->termios = tp;
Alan Coxfe6e29f2008-10-13 10:44:08 +01001200 tty->termios_locked = tp + 1;
Alan Coxbf970ee2008-10-13 10:42:39 +01001201
1202 /* Compatibility until drivers always set this */
1203 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1204 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1205 return 0;
1206}
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001207EXPORT_SYMBOL_GPL(tty_init_termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001208
1209/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001210 * tty_driver_install_tty() - install a tty entry in the driver
1211 * @driver: the driver for the tty
1212 * @tty: the tty
1213 *
1214 * Install a tty object into the driver tables. The tty->index field
Alan Coxbf970ee2008-10-13 10:42:39 +01001215 * will be set by the time this is called. This method is responsible
1216 * for ensuring any need additional structures are allocated and
1217 * configured.
Alan Cox8b0a88d2008-10-13 10:42:19 +01001218 *
1219 * Locking: tty_mutex for now
1220 */
1221static int tty_driver_install_tty(struct tty_driver *driver,
1222 struct tty_struct *tty)
1223{
Alan Coxbf970ee2008-10-13 10:42:39 +01001224 int idx = tty->index;
Alan Coxeeb89d92009-11-30 13:18:29 +00001225 int ret;
Alan Coxbf970ee2008-10-13 10:42:39 +01001226
Alan Coxeeb89d92009-11-30 13:18:29 +00001227 if (driver->ops->install) {
Alan Coxeeb89d92009-11-30 13:18:29 +00001228 ret = driver->ops->install(driver, tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001229 return ret;
1230 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001231
1232 if (tty_init_termios(tty) == 0) {
1233 tty_driver_kref_get(driver);
1234 tty->count++;
1235 driver->ttys[idx] = tty;
1236 return 0;
1237 }
1238 return -ENOMEM;
Alan Cox8b0a88d2008-10-13 10:42:19 +01001239}
1240
1241/**
1242 * tty_driver_remove_tty() - remove a tty from the driver tables
1243 * @driver: the driver for the tty
1244 * @idx: the minor number
1245 *
1246 * Remvoe a tty object from the driver tables. The tty->index field
1247 * will be set by the time this is called.
1248 *
1249 * Locking: tty_mutex for now
1250 */
1251static void tty_driver_remove_tty(struct tty_driver *driver,
1252 struct tty_struct *tty)
1253{
1254 if (driver->ops->remove)
1255 driver->ops->remove(driver, tty);
1256 else
1257 driver->ttys[tty->index] = NULL;
1258}
1259
1260/*
1261 * tty_reopen() - fast re-open of an open tty
1262 * @tty - the tty to open
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001263 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001264 * Return 0 on success, -errno on error.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001265 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001266 * Locking: tty_mutex must be held from the time the tty was found
1267 * till this open completes.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001268 */
Alan Cox99f1fe12008-10-13 10:42:00 +01001269static int tty_reopen(struct tty_struct *tty)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001270{
1271 struct tty_driver *driver = tty->driver;
1272
1273 if (test_bit(TTY_CLOSING, &tty->flags))
1274 return -EIO;
1275
1276 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1277 driver->subtype == PTY_TYPE_MASTER) {
1278 /*
1279 * special case for PTY masters: only one open permitted,
1280 * and the slave side open count is incremented as well.
1281 */
1282 if (tty->count)
1283 return -EIO;
1284
1285 tty->link->count++;
1286 }
1287 tty->count++;
1288 tty->driver = driver; /* N.B. why do this every time?? */
1289
Alan Cox1aa4bed2009-06-16 17:01:33 +01001290 mutex_lock(&tty->ldisc_mutex);
Alan Cox99f1fe12008-10-13 10:42:00 +01001291 WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
Alan Cox1aa4bed2009-06-16 17:01:33 +01001292 mutex_unlock(&tty->ldisc_mutex);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001293
1294 return 0;
1295}
1296
Alan Coxaf9b8972006-08-27 01:24:01 -07001297/**
Alan Coxd81ed102008-10-13 10:41:42 +01001298 * tty_init_dev - initialise a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001299 * @driver: tty driver we are opening a device on
1300 * @idx: device index
Alan Cox15582d32008-10-13 10:41:03 +01001301 * @ret_tty: returned tty structure
1302 * @first_ok: ok to open a new device (used by ptmx)
Alan Coxaf9b8972006-08-27 01:24:01 -07001303 *
1304 * Prepare a tty device. This may not be a "new" clean device but
1305 * could also be an active device. The pty drivers require special
1306 * handling because of this.
1307 *
1308 * Locking:
1309 * The function is called under the tty_mutex, which
1310 * protects us from the tty struct or driver itself going away.
1311 *
1312 * On exit the tty device has the line discipline attached and
1313 * a reference count of 1. If a pair was created for pty/tty use
1314 * and the other was a pty master then it too has a reference count of 1.
1315 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001317 * failed open. The new code protects the open with a mutex, so it's
1318 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 * relaxed for the (most common) case of reopening a tty.
1320 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001321
Alan Cox73ec06f2008-10-13 10:42:29 +01001322struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1323 int first_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Alan Coxbf970ee2008-10-13 10:42:39 +01001325 struct tty_struct *tty;
Alan Cox73ec06f2008-10-13 10:42:29 +01001326 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001328 /* Check if pty master is being opened multiple times */
Alan Cox15582d32008-10-13 10:41:03 +01001329 if (driver->subtype == PTY_TYPE_MASTER &&
Alan Coxeeb89d92009-11-30 13:18:29 +00001330 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
Alan Cox73ec06f2008-10-13 10:42:29 +01001331 return ERR_PTR(-EIO);
Alan Coxeeb89d92009-11-30 13:18:29 +00001332 }
Alan Cox73ec06f2008-10-13 10:42:29 +01001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /*
1335 * First time open is complex, especially for PTY devices.
1336 * This code guarantees that either everything succeeds and the
1337 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001338 * and the allocated memory released. (Except that the termios
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 * and locked termios may be retained.)
1340 */
1341
Alan Cox73ec06f2008-10-13 10:42:29 +01001342 if (!try_module_get(driver->owner))
1343 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 tty = alloc_tty_struct();
Alan Cox37bdfb02008-02-08 04:18:47 -08001346 if (!tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 goto fail_no_mem;
Alan Coxbf970ee2008-10-13 10:42:39 +01001348 initialize_tty_struct(tty, driver, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Alan Cox73ec06f2008-10-13 10:42:29 +01001350 retval = tty_driver_install_tty(driver, tty);
Alan Coxbf970ee2008-10-13 10:42:39 +01001351 if (retval < 0) {
1352 free_tty_struct(tty);
1353 module_put(driver->owner);
1354 return ERR_PTR(retval);
1355 }
Alan Cox8b0a88d2008-10-13 10:42:19 +01001356
Alan Cox37bdfb02008-02-08 04:18:47 -08001357 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001359 * If we fail here just call release_tty to clean up. No need
1360 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Alan Coxbf970ee2008-10-13 10:42:39 +01001362 retval = tty_ldisc_setup(tty, tty->link);
Alan Cox01e1abb2008-07-22 11:16:55 +01001363 if (retval)
1364 goto release_mem_out;
Alan Cox73ec06f2008-10-13 10:42:29 +01001365 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367fail_no_mem:
1368 module_put(driver->owner);
Alan Cox73ec06f2008-10-13 10:42:29 +01001369 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001371 /* call the tty release_tty routine to clean out this slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372release_mem_out:
Akinobu Mita40509142006-09-29 02:01:27 -07001373 if (printk_ratelimit())
Alan Coxd81ed102008-10-13 10:41:42 +01001374 printk(KERN_INFO "tty_init_dev: ldisc open failed, "
Akinobu Mita40509142006-09-29 02:01:27 -07001375 "clearing slot %d\n", idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001376 release_tty(tty, idx);
Alan Cox73ec06f2008-10-13 10:42:29 +01001377 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Alan Coxfeebed62008-10-13 10:41:30 +01001380void tty_free_termios(struct tty_struct *tty)
1381{
1382 struct ktermios *tp;
1383 int idx = tty->index;
1384 /* Kill this flag and push into drivers for locking etc */
1385 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1386 /* FIXME: Locking on ->termios array */
1387 tp = tty->termios;
1388 tty->driver->termios[idx] = NULL;
1389 kfree(tp);
Alan Coxfeebed62008-10-13 10:41:30 +01001390 }
1391}
1392EXPORT_SYMBOL(tty_free_termios);
1393
1394void tty_shutdown(struct tty_struct *tty)
1395{
Alan Cox8b0a88d2008-10-13 10:42:19 +01001396 tty_driver_remove_tty(tty->driver, tty);
Alan Coxfeebed62008-10-13 10:41:30 +01001397 tty_free_termios(tty);
1398}
1399EXPORT_SYMBOL(tty_shutdown);
1400
Alan Coxaf9b8972006-08-27 01:24:01 -07001401/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001402 * release_one_tty - release tty structure memory
Alan Cox9c9f4de2008-10-13 10:37:26 +01001403 * @kref: kref of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001404 *
1405 * Releases memory associated with a tty structure, and clears out the
1406 * driver table slots. This function is called when a device is no longer
1407 * in use. It also gets called when setup of a device fails.
1408 *
1409 * Locking:
1410 * tty_mutex - sometimes only
1411 * takes the file list lock internally when working on the list
1412 * of ttys that the driver keeps.
Alan Coxb50989d2009-09-19 13:13:22 -07001413 *
1414 * This method gets called from a work queue so that the driver private
Dave Youngf278a2f2009-09-27 16:00:42 +00001415 * cleanup ops can sleep (needed for USB at least)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 */
Alan Coxb50989d2009-09-19 13:13:22 -07001417static void release_one_tty(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Alan Coxb50989d2009-09-19 13:13:22 -07001419 struct tty_struct *tty =
1420 container_of(work, struct tty_struct, hangup_work);
Alan Cox6f967f72008-10-13 10:37:36 +01001421 struct tty_driver *driver = tty->driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Dave Youngf278a2f2009-09-27 16:00:42 +00001423 if (tty->ops->cleanup)
1424 tty->ops->cleanup(tty);
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 tty->magic = 0;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001427 tty_driver_kref_put(driver);
Alan Cox6f967f72008-10-13 10:37:36 +01001428 module_put(driver->owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001429
Nick Pigginee2ffa02010-08-18 04:37:35 +10001430 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 list_del_init(&tty->tty_files);
Nick Pigginee2ffa02010-08-18 04:37:35 +10001432 spin_unlock(&tty_files_lock);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001433
Oleg Nesterov6da8d862010-04-02 18:05:12 +02001434 put_pid(tty->pgrp);
1435 put_pid(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 free_tty_struct(tty);
1437}
1438
Alan Coxb50989d2009-09-19 13:13:22 -07001439static void queue_release_one_tty(struct kref *kref)
1440{
1441 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Dave Youngf278a2f2009-09-27 16:00:42 +00001442
1443 if (tty->ops->shutdown)
1444 tty->ops->shutdown(tty);
1445 else
1446 tty_shutdown(tty);
1447
Alan Coxb50989d2009-09-19 13:13:22 -07001448 /* The hangup queue is now free so we can reuse it rather than
1449 waste a chunk of memory for each port */
1450 INIT_WORK(&tty->hangup_work, release_one_tty);
1451 schedule_work(&tty->hangup_work);
1452}
1453
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001454/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001455 * tty_kref_put - release a tty kref
1456 * @tty: tty device
1457 *
1458 * Release a reference to a tty device and if need be let the kref
1459 * layer destruct the object for us
1460 */
1461
1462void tty_kref_put(struct tty_struct *tty)
1463{
1464 if (tty)
Alan Coxb50989d2009-09-19 13:13:22 -07001465 kref_put(&tty->kref, queue_release_one_tty);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001466}
1467EXPORT_SYMBOL(tty_kref_put);
1468
1469/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001470 * release_tty - release tty structure memory
1471 *
1472 * Release both @tty and a possible linked partner (think pty pair),
1473 * and decrement the refcount of the backing module.
1474 *
1475 * Locking:
1476 * tty_mutex - sometimes only
1477 * takes the file list lock internally when working on the list
1478 * of ttys that the driver keeps.
1479 * FIXME: should we require tty_mutex is held here ??
Alan Cox9c9f4de2008-10-13 10:37:26 +01001480 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001481 */
1482static void release_tty(struct tty_struct *tty, int idx)
1483{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001484 /* This should always be true but check for the moment */
1485 WARN_ON(tty->index != idx);
1486
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001487 if (tty->link)
Alan Cox9c9f4de2008-10-13 10:37:26 +01001488 tty_kref_put(tty->link);
1489 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001490}
1491
Alan Coxeeb89d92009-11-30 13:18:29 +00001492/**
1493 * tty_release - vfs callback for close
1494 * @inode: inode of tty
1495 * @filp: file pointer for handle to tty
1496 *
1497 * Called the last time each file handle is closed that references
1498 * this tty. There may however be several such references.
1499 *
1500 * Locking:
1501 * Takes bkl. See tty_release_dev
1502 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 * Even releasing the tty structures is a tricky business.. We have
1504 * to be very careful that the structures are all released at the
1505 * same time, as interrupts might otherwise get the wrong pointers.
1506 *
1507 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1508 * lead to double frees or releasing memory still in use.
1509 */
Alan Coxeeb89d92009-11-30 13:18:29 +00001510
1511int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 struct tty_struct *tty, *o_tty;
1514 int pty_master, tty_closing, o_tty_closing, do_sleep;
Paul Fulghum14a62832006-04-10 22:54:19 -07001515 int devpts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 int idx;
1517 char buf[64];
Alan Cox37bdfb02008-02-08 04:18:47 -08001518
Kulikov Vasiliy2b50d242010-06-29 14:15:09 +04001519 tty = filp->private_data;
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001520 if (tty_paranoia_check(tty, inode, "tty_release_dev"))
Alan Coxeeb89d92009-11-30 13:18:29 +00001521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Arnd Bergmannec79d602010-06-01 22:53:01 +02001523 tty_lock();
Alan Coxd81ed102008-10-13 10:41:42 +01001524 check_tty_count(tty, "tty_release_dev");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Arnd Bergmannec79d602010-06-01 22:53:01 +02001526 __tty_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 idx = tty->index;
1529 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1530 tty->driver->subtype == PTY_TYPE_MASTER);
1531 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 o_tty = tty->link;
1533
1534#ifdef TTY_PARANOIA_CHECK
1535 if (idx < 0 || idx >= tty->driver->num) {
Alan Coxd81ed102008-10-13 10:41:42 +01001536 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 "free (%s)\n", tty->name);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001538 tty_unlock();
Alan Coxeeb89d92009-11-30 13:18:29 +00001539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
Alan Cox8b0a88d2008-10-13 10:42:19 +01001541 if (!devpts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (tty != tty->driver->ttys[idx]) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001543 tty_unlock();
Alan Coxd81ed102008-10-13 10:41:42 +01001544 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 "for (%s)\n", idx, tty->name);
Alan Coxeeb89d92009-11-30 13:18:29 +00001546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548 if (tty->termios != tty->driver->termios[idx]) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001549 tty_unlock();
Alan Coxd81ed102008-10-13 10:41:42 +01001550 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 "for (%s)\n",
1552 idx, tty->name);
Alan Coxeeb89d92009-11-30 13:18:29 +00001553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556#endif
1557
1558#ifdef TTY_DEBUG_HANGUP
Alan Coxd81ed102008-10-13 10:41:42 +01001559 printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 tty_name(tty, buf), tty->count);
1561#endif
1562
1563#ifdef TTY_PARANOIA_CHECK
1564 if (tty->driver->other &&
1565 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1566 if (o_tty != tty->driver->other->ttys[idx]) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001567 tty_unlock();
Alan Coxd81ed102008-10-13 10:41:42 +01001568 printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 "not o_tty for (%s)\n",
1570 idx, tty->name);
Alan Coxeeb89d92009-11-30 13:18:29 +00001571 return 0 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573 if (o_tty->termios != tty->driver->other->termios[idx]) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001574 tty_unlock();
Alan Coxd81ed102008-10-13 10:41:42 +01001575 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 "not o_termios for (%s)\n",
1577 idx, tty->name);
Alan Coxeeb89d92009-11-30 13:18:29 +00001578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (o_tty->link != tty) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001581 tty_unlock();
Alan Coxd81ed102008-10-13 10:41:42 +01001582 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
Alan Coxeeb89d92009-11-30 13:18:29 +00001583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585 }
1586#endif
Alan Coxf34d7a52008-04-30 00:54:13 -07001587 if (tty->ops->close)
1588 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Arnd Bergmannec79d602010-06-01 22:53:01 +02001590 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
1592 * Sanity check: if tty->count is going to zero, there shouldn't be
1593 * any waiters on tty->read_wait or tty->write_wait. We test the
1594 * wait queues and kick everyone out _before_ actually starting to
1595 * close. This ensures that we won't block while releasing the tty
1596 * structure.
1597 *
1598 * The test for the o_tty closing is necessary, since the master and
1599 * slave sides may close in any order. If the slave side closes out
1600 * first, its count will be one, since the master side holds an open.
1601 * Thus this test wouldn't be triggered at the time the slave closes,
1602 * so we do it now.
1603 *
1604 * Note that it's possible for the tty to be opened again while we're
1605 * flushing out waiters. By recalculating the closing flags before
1606 * each iteration we avoid any problems.
1607 */
1608 while (1) {
1609 /* Guard against races with tty->count changes elsewhere and
1610 opens on /dev/tty */
Alan Cox37bdfb02008-02-08 04:18:47 -08001611
Ingo Molnar70522e12006-03-23 03:00:31 -08001612 mutex_lock(&tty_mutex);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001613 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 tty_closing = tty->count <= 1;
1615 o_tty_closing = o_tty &&
1616 (o_tty->count <= (pty_master ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 do_sleep = 0;
1618
1619 if (tty_closing) {
1620 if (waitqueue_active(&tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001621 wake_up_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 do_sleep++;
1623 }
1624 if (waitqueue_active(&tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001625 wake_up_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 do_sleep++;
1627 }
1628 }
1629 if (o_tty_closing) {
1630 if (waitqueue_active(&o_tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001631 wake_up_poll(&o_tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 do_sleep++;
1633 }
1634 if (waitqueue_active(&o_tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001635 wake_up_poll(&o_tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 do_sleep++;
1637 }
1638 }
1639 if (!do_sleep)
1640 break;
1641
Alan Coxd81ed102008-10-13 10:41:42 +01001642 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 "active!\n", tty_name(tty, buf));
Arnd Bergmannec79d602010-06-01 22:53:01 +02001644 tty_unlock();
Ingo Molnar70522e12006-03-23 03:00:31 -08001645 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 schedule();
Alan Cox37bdfb02008-02-08 04:18:47 -08001647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08001650 * The closing flags are now consistent with the open counts on
1651 * both sides, and we've completed the last operation that could
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 * block, so it's safe to proceed with closing.
1653 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (pty_master) {
1655 if (--o_tty->count < 0) {
Alan Coxd81ed102008-10-13 10:41:42 +01001656 printk(KERN_WARNING "tty_release_dev: bad pty slave count "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 "(%d) for %s\n",
1658 o_tty->count, tty_name(o_tty, buf));
1659 o_tty->count = 0;
1660 }
1661 }
1662 if (--tty->count < 0) {
Alan Coxd81ed102008-10-13 10:41:42 +01001663 printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 tty->count, tty_name(tty, buf));
1665 tty->count = 0;
1666 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /*
1669 * We've decremented tty->count, so we need to remove this file
1670 * descriptor off the tty->tty_files list; this serves two
1671 * purposes:
1672 * - check_tty_count sees the correct number of file descriptors
1673 * associated with this tty.
1674 * - do_tty_hangup no longer sees this file descriptor as
1675 * something that needs to be handled for hangups.
1676 */
Nick Pigginee2ffa02010-08-18 04:37:35 +10001677 spin_lock(&tty_files_lock);
1678 BUG_ON(list_empty(&filp->f_u.fu_list));
1679 list_del_init(&filp->f_u.fu_list);
1680 spin_unlock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 filp->private_data = NULL;
1682
1683 /*
1684 * Perform some housekeeping before deciding whether to return.
1685 *
1686 * Set the TTY_CLOSING flag if this was the last open. In the
1687 * case of a pty we may have to wait around for the other side
1688 * to close, and TTY_CLOSING makes sure we can't be reopened.
1689 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001690 if (tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 set_bit(TTY_CLOSING, &tty->flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08001692 if (o_tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 set_bit(TTY_CLOSING, &o_tty->flags);
1694
1695 /*
1696 * If _either_ side is closing, make sure there aren't any
1697 * processes that still think tty or o_tty is their controlling
1698 * tty.
1699 */
1700 if (tty_closing || o_tty_closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001702 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001704 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 read_unlock(&tasklist_lock);
1706 }
1707
Ingo Molnar70522e12006-03-23 03:00:31 -08001708 mutex_unlock(&tty_mutex);
Paul Fulghumda965822006-02-14 13:53:00 -08001709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 /* check whether both sides are closing ... */
Alan Coxeeb89d92009-11-30 13:18:29 +00001711 if (!tty_closing || (o_tty && !o_tty_closing)) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001712 tty_unlock();
Alan Coxeeb89d92009-11-30 13:18:29 +00001713 return 0;
1714 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716#ifdef TTY_DEBUG_HANGUP
1717 printk(KERN_DEBUG "freeing tty structure...");
1718#endif
1719 /*
Alan Cox01e1abb2008-07-22 11:16:55 +01001720 * Ask the line discipline code to release its structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 */
Alan Cox01e1abb2008-07-22 11:16:55 +01001722 tty_ldisc_release(tty, o_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /*
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001724 * The release_tty function takes care of the details of clearing
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 * the slots and preserving the termios structure.
1726 */
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001727 release_tty(tty, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 /* Make this pty number available for reallocation */
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -07001730 if (devpts)
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001731 devpts_kill_index(inode, idx);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001732 tty_unlock();
Alan Coxeeb89d92009-11-30 13:18:29 +00001733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
Alan Coxaf9b8972006-08-27 01:24:01 -07001736/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001737 * tty_open - open a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001738 * @inode: inode of device file
1739 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001741 * tty_open and tty_release keep up the tty count that contains the
1742 * number of opens done on a tty. We cannot use the inode-count, as
1743 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001745 * Open-counting is needed for pty masters, as well as for keeping
1746 * track of serial lines: DTR is dropped when the last close happens.
1747 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1748 *
1749 * The termios state of a pty is reset on first open so that
1750 * settings don't persist across reuse.
1751 *
Alan Coxd81ed102008-10-13 10:41:42 +01001752 * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001753 * tty->count should protect the rest.
1754 * ->siglock protects ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001756
Alan Coxeeb89d92009-11-30 13:18:29 +00001757static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001759 struct tty_struct *tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int noctty, retval;
1761 struct tty_driver *driver;
1762 int index;
1763 dev_t device = inode->i_rdev;
Andrew Morton846c1512009-04-02 16:56:36 -07001764 unsigned saved_flags = filp->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768retry_open:
1769 noctty = filp->f_flags & O_NOCTTY;
1770 index = -1;
1771 retval = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001772
Ingo Molnar70522e12006-03-23 03:00:31 -08001773 mutex_lock(&tty_mutex);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001774 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Alan Cox37bdfb02008-02-08 04:18:47 -08001776 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001777 tty = get_current_tty();
1778 if (!tty) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001779 tty_unlock();
Ingo Molnar70522e12006-03-23 03:00:31 -08001780 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return -ENXIO;
1782 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01001783 driver = tty_driver_kref_get(tty->driver);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001784 index = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1786 /* noctty = 1; */
Alan Cox452a00d2008-10-13 10:39:13 +01001787 /* FIXME: Should we take a driver reference ? */
1788 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 goto got_driver;
1790 }
1791#ifdef CONFIG_VT
Alan Cox37bdfb02008-02-08 04:18:47 -08001792 if (device == MKDEV(TTY_MAJOR, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 extern struct tty_driver *console_driver;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001794 driver = tty_driver_kref_get(console_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 index = fg_console;
1796 noctty = 1;
1797 goto got_driver;
1798 }
1799#endif
Alan Cox37bdfb02008-02-08 04:18:47 -08001800 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
Will Newton296fa7f2008-12-01 11:36:06 +00001801 struct tty_driver *console_driver = console_device(&index);
1802 if (console_driver) {
1803 driver = tty_driver_kref_get(console_driver);
1804 if (driver) {
1805 /* Don't let /dev/console block */
1806 filp->f_flags |= O_NONBLOCK;
1807 noctty = 1;
1808 goto got_driver;
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Arnd Bergmannec79d602010-06-01 22:53:01 +02001811 tty_unlock();
Ingo Molnar70522e12006-03-23 03:00:31 -08001812 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return -ENODEV;
1814 }
1815
1816 driver = get_tty_driver(device, &index);
1817 if (!driver) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001818 tty_unlock();
Ingo Molnar70522e12006-03-23 03:00:31 -08001819 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return -ENODEV;
1821 }
1822got_driver:
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001823 if (!tty) {
1824 /* check whether we're reopening an existing tty */
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001825 tty = tty_driver_lookup_tty(driver, inode, index);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001826
Eric Paris808ffa32009-01-27 11:50:37 +00001827 if (IS_ERR(tty)) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001828 tty_unlock();
Eric Paris808ffa32009-01-27 11:50:37 +00001829 mutex_unlock(&tty_mutex);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001830 return PTR_ERR(tty);
Eric Paris808ffa32009-01-27 11:50:37 +00001831 }
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001832 }
1833
1834 if (tty) {
1835 retval = tty_reopen(tty);
1836 if (retval)
1837 tty = ERR_PTR(retval);
1838 } else
1839 tty = tty_init_dev(driver, index, 0);
1840
Ingo Molnar70522e12006-03-23 03:00:31 -08001841 mutex_unlock(&tty_mutex);
Alan Cox7d7b93c2008-10-13 10:42:09 +01001842 tty_driver_kref_put(driver);
Alan Coxeeb89d92009-11-30 13:18:29 +00001843 if (IS_ERR(tty)) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001844 tty_unlock();
Alan Cox73ec06f2008-10-13 10:42:29 +01001845 return PTR_ERR(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 filp->private_data = tty;
Nick Pigginee2ffa02010-08-18 04:37:35 +10001849 BUG_ON(list_empty(&filp->f_u.fu_list));
1850 file_sb_list_del(filp); /* __dentry_open has put it on the sb list */
1851 spin_lock(&tty_files_lock);
1852 list_add(&filp->f_u.fu_list, &tty->tty_files);
1853 spin_unlock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 check_tty_count(tty, "tty_open");
1855 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1856 tty->driver->subtype == PTY_TYPE_MASTER)
1857 noctty = 1;
1858#ifdef TTY_DEBUG_HANGUP
1859 printk(KERN_DEBUG "opening %s...", tty->name);
1860#endif
1861 if (!retval) {
Alan Coxf34d7a52008-04-30 00:54:13 -07001862 if (tty->ops->open)
1863 retval = tty->ops->open(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 else
1865 retval = -ENODEV;
1866 }
1867 filp->f_flags = saved_flags;
1868
Alan Cox37bdfb02008-02-08 04:18:47 -08001869 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1870 !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 retval = -EBUSY;
1872
1873 if (retval) {
1874#ifdef TTY_DEBUG_HANGUP
1875 printk(KERN_DEBUG "error %d in opening %s...", retval,
1876 tty->name);
1877#endif
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02001878 tty_unlock(); /* need to call tty_release without BTM */
Alan Coxeeb89d92009-11-30 13:18:29 +00001879 tty_release(inode, filp);
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02001880 if (retval != -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02001882
1883 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 schedule();
1887 /*
1888 * Need to reset f_op in case a hangup happened.
1889 */
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02001890 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (filp->f_op == &hung_up_tty_fops)
1892 filp->f_op = &tty_fops;
Arnd Bergmannec79d602010-06-01 22:53:01 +02001893 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 goto retry_open;
1895 }
Arnd Bergmannec79d602010-06-01 22:53:01 +02001896 tty_unlock();
Alan Coxeeb89d92009-11-30 13:18:29 +00001897
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001898
1899 mutex_lock(&tty_mutex);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001900 tty_lock();
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001901 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (!noctty &&
1903 current->signal->leader &&
1904 !current->signal->tty &&
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001905 tty->session == NULL)
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07001906 __proc_set_tty(current, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001907 spin_unlock_irq(&current->sighand->siglock);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001908 tty_unlock();
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001909 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return 0;
1911}
1912
Jonathan Corbet39d95b92008-05-16 09:10:50 -06001913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Alan Coxaf9b8972006-08-27 01:24:01 -07001915/**
1916 * tty_poll - check tty status
1917 * @filp: file being polled
1918 * @wait: poll wait structures to update
1919 *
1920 * Call the line discipline polling method to obtain the poll
1921 * status of the device.
1922 *
1923 * Locking: locks called line discipline but ldisc poll method
1924 * may be re-entered freely by other callers.
1925 */
1926
Alan Cox37bdfb02008-02-08 04:18:47 -08001927static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Alan Cox37bdfb02008-02-08 04:18:47 -08001929 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 struct tty_ldisc *ld;
1931 int ret = 0;
1932
Kulikov Vasiliy2b50d242010-06-29 14:15:09 +04001933 tty = filp->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08001934 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001938 if (ld->ops->poll)
1939 ret = (ld->ops->poll)(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 tty_ldisc_deref(ld);
1941 return ret;
1942}
1943
Arnd Bergmannec79d602010-06-01 22:53:01 +02001944static int __tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Alan Cox37bdfb02008-02-08 04:18:47 -08001946 struct tty_struct *tty;
Alan Cox47f86832008-04-30 00:53:30 -07001947 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001948 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Kulikov Vasiliy2b50d242010-06-29 14:15:09 +04001950 tty = filp->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08001951 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001952 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08001953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 retval = fasync_helper(fd, filp, on, &tty->fasync);
1955 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001956 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001959 enum pid_type type;
1960 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (!waitqueue_active(&tty->read_wait))
1962 tty->minimum_to_wake = 1;
Alan Cox47f86832008-04-30 00:53:30 -07001963 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001964 if (tty->pgrp) {
1965 pid = tty->pgrp;
1966 type = PIDTYPE_PGID;
1967 } else {
1968 pid = task_pid(current);
1969 type = PIDTYPE_PID;
1970 }
Linus Torvalds80e1e822010-02-07 10:11:23 -08001971 get_pid(pid);
Greg Kroah-Hartman70362512009-12-17 07:07:19 -08001972 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds80e1e822010-02-07 10:11:23 -08001973 retval = __f_setown(filp, pid, type, 0);
1974 put_pid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (retval)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001976 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 } else {
1978 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1979 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1980 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001981 retval = 0;
1982out:
Arnd Bergmannec79d602010-06-01 22:53:01 +02001983 return retval;
1984}
1985
1986static int tty_fasync(int fd, struct file *filp, int on)
1987{
1988 int retval;
1989 tty_lock();
1990 retval = __tty_fasync(fd, filp, on);
1991 tty_unlock();
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001992 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Alan Coxaf9b8972006-08-27 01:24:01 -07001995/**
1996 * tiocsti - fake input character
1997 * @tty: tty to fake input into
1998 * @p: pointer to character
1999 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02002000 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07002001 * input management.
2002 *
2003 * FIXME: does not honour flow control ??
2004 *
2005 * Locking:
2006 * Called functions take tty_ldisc_lock
2007 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07002008 *
2009 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07002010 */
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012static int tiocsti(struct tty_struct *tty, char __user *p)
2013{
2014 char ch, mbz = 0;
2015 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2018 return -EPERM;
2019 if (get_user(ch, p))
2020 return -EFAULT;
Al Viro1e641742008-12-09 09:23:33 +00002021 tty_audit_tiocsti(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002023 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 tty_ldisc_deref(ld);
2025 return 0;
2026}
2027
Alan Coxaf9b8972006-08-27 01:24:01 -07002028/**
2029 * tiocgwinsz - implement window query ioctl
2030 * @tty; tty
2031 * @arg: user buffer for result
2032 *
Alan Cox808a0d32006-09-29 02:00:40 -07002033 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002034 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002035 * Locking: tty->termios_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002036 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002037 */
2038
Alan Cox37bdfb02008-02-08 04:18:47 -08002039static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Alan Cox808a0d32006-09-29 02:00:40 -07002041 int err;
2042
Arjan van de Ven5785c952006-09-29 02:00:43 -07002043 mutex_lock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002044 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Arjan van de Ven5785c952006-09-29 02:00:43 -07002045 mutex_unlock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002046
2047 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Alan Coxaf9b8972006-08-27 01:24:01 -07002050/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002051 * tty_do_resize - resize event
2052 * @tty: tty being resized
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002053 * @rows: rows (character)
2054 * @cols: cols (character)
Alan Coxaf9b8972006-08-27 01:24:01 -07002055 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002056 * Update the termios variables and send the necessary signals to
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002057 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002058 */
2059
Alan Coxfc6f6232009-01-02 13:43:17 +00002060int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Alan Coxfc6f6232009-01-02 13:43:17 +00002062 struct pid *pgrp;
Alan Cox47f86832008-04-30 00:53:30 -07002063 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Alan Coxfc6f6232009-01-02 13:43:17 +00002065 /* Lock the tty */
2066 mutex_lock(&tty->termios_mutex);
2067 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002068 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002069 /* Get the PID values and reference them so we can
2070 avoid holding the tty ctrl lock while sending signals */
2071 spin_lock_irqsave(&tty->ctrl_lock, flags);
2072 pgrp = get_pid(tty->pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002073 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2074
2075 if (pgrp)
2076 kill_pgrp(pgrp, SIGWINCH, 1);
Alan Cox47f86832008-04-30 00:53:30 -07002077 put_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002078
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002079 tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002080done:
Alan Coxfc6f6232009-01-02 13:43:17 +00002081 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return 0;
2083}
2084
Alan Coxaf9b8972006-08-27 01:24:01 -07002085/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002086 * tiocswinsz - implement window size set ioctl
Alan Coxfc6f6232009-01-02 13:43:17 +00002087 * @tty; tty side of tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002088 * @arg: user buffer for result
2089 *
2090 * Copies the user idea of the window size to the kernel. Traditionally
2091 * this is just advisory information but for the Linux console it
2092 * actually has driver level meaning and triggers a VC resize.
2093 *
2094 * Locking:
2095 * Driver dependant. The default do_resize method takes the
2096 * tty termios mutex and ctrl_lock. The console takes its own lock
2097 * then calls into the default method.
2098 */
2099
Alan Coxfc6f6232009-01-02 13:43:17 +00002100static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002101{
2102 struct winsize tmp_ws;
2103 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2104 return -EFAULT;
2105
2106 if (tty->ops->resize)
Alan Coxfc6f6232009-01-02 13:43:17 +00002107 return tty->ops->resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002108 else
Alan Coxfc6f6232009-01-02 13:43:17 +00002109 return tty_do_resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002110}
2111
2112/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002113 * tioccons - allow admin to move logical console
2114 * @file: the file to become console
2115 *
2116 * Allow the adminstrator to move the redirected console device
2117 *
2118 * Locking: uses redirect_lock to guard the redirect information
2119 */
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121static int tioccons(struct file *file)
2122{
2123 if (!capable(CAP_SYS_ADMIN))
2124 return -EPERM;
2125 if (file->f_op->write == redirected_tty_write) {
2126 struct file *f;
2127 spin_lock(&redirect_lock);
2128 f = redirect;
2129 redirect = NULL;
2130 spin_unlock(&redirect_lock);
2131 if (f)
2132 fput(f);
2133 return 0;
2134 }
2135 spin_lock(&redirect_lock);
2136 if (redirect) {
2137 spin_unlock(&redirect_lock);
2138 return -EBUSY;
2139 }
2140 get_file(file);
2141 redirect = file;
2142 spin_unlock(&redirect_lock);
2143 return 0;
2144}
2145
Alan Coxaf9b8972006-08-27 01:24:01 -07002146/**
2147 * fionbio - non blocking ioctl
2148 * @file: file to set blocking value
2149 * @p: user parameter
2150 *
2151 * Historical tty interfaces had a blocking control ioctl before
2152 * the generic functionality existed. This piece of history is preserved
2153 * in the expected tty API of posix OS's.
2154 *
Alan Cox6146b9a2009-09-19 13:13:19 -07002155 * Locking: none, the open file handle ensures it won't go away.
Alan Coxaf9b8972006-08-27 01:24:01 -07002156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158static int fionbio(struct file *file, int __user *p)
2159{
2160 int nonblock;
2161
2162 if (get_user(nonblock, p))
2163 return -EFAULT;
2164
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002165 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (nonblock)
2167 file->f_flags |= O_NONBLOCK;
2168 else
2169 file->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002170 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return 0;
2172}
2173
Alan Coxaf9b8972006-08-27 01:24:01 -07002174/**
2175 * tiocsctty - set controlling tty
2176 * @tty: tty structure
2177 * @arg: user argument
2178 *
2179 * This ioctl is used to manage job control. It permits a session
2180 * leader to set this tty as the controlling tty for the session.
2181 *
2182 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002183 * Takes tty_mutex() to protect tty instance
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002184 * Takes tasklist_lock internally to walk sessions
2185 * Takes ->siglock() when updating signal->tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002186 */
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188static int tiocsctty(struct tty_struct *tty, int arg)
2189{
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002190 int ret = 0;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002191 if (current->signal->leader && (task_session(current) == tty->session))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002192 return ret;
2193
2194 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /*
2196 * The process must be a session leader and
2197 * not have a controlling tty already.
2198 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002199 if (!current->signal->leader || current->signal->tty) {
2200 ret = -EPERM;
2201 goto unlock;
2202 }
2203
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002204 if (tty->session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 /*
2206 * This tty is already the controlling
2207 * tty for another session group!
2208 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002209 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /*
2211 * Steal it away
2212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002214 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 read_unlock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002216 } else {
2217 ret = -EPERM;
2218 goto unlock;
2219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002221 proc_set_tty(current, tty);
2222unlock:
Alan Cox28298232006-09-29 02:00:58 -07002223 mutex_unlock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Alan Coxaf9b8972006-08-27 01:24:01 -07002227/**
Alan Cox5d0fdf12008-04-30 00:53:31 -07002228 * tty_get_pgrp - return a ref counted pgrp pid
2229 * @tty: tty to read
2230 *
2231 * Returns a refcounted instance of the pid struct for the process
2232 * group controlling the tty.
2233 */
2234
2235struct pid *tty_get_pgrp(struct tty_struct *tty)
2236{
2237 unsigned long flags;
2238 struct pid *pgrp;
2239
2240 spin_lock_irqsave(&tty->ctrl_lock, flags);
2241 pgrp = get_pid(tty->pgrp);
2242 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2243
2244 return pgrp;
2245}
2246EXPORT_SYMBOL_GPL(tty_get_pgrp);
2247
2248/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002249 * tiocgpgrp - get process group
2250 * @tty: tty passed by user
2251 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2252 * @p: returned pid
2253 *
2254 * Obtain the process group of the tty. If there is no process group
2255 * return an error.
2256 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002257 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002258 */
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2261{
Alan Cox5d0fdf12008-04-30 00:53:31 -07002262 struct pid *pid;
2263 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 /*
2265 * (tty == real_tty) is a cheap way of
2266 * testing if the tty is NOT a master pty.
2267 */
2268 if (tty == real_tty && current->signal->tty != real_tty)
2269 return -ENOTTY;
Alan Cox5d0fdf12008-04-30 00:53:31 -07002270 pid = tty_get_pgrp(real_tty);
2271 ret = put_user(pid_vnr(pid), p);
2272 put_pid(pid);
2273 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
Alan Coxaf9b8972006-08-27 01:24:01 -07002276/**
2277 * tiocspgrp - attempt to set process group
2278 * @tty: tty passed by user
2279 * @real_tty: tty side device matching tty passed by user
2280 * @p: pid pointer
2281 *
2282 * Set the process group of the tty to the session passed. Only
2283 * permitted where the tty session is our session.
2284 *
Alan Cox47f86832008-04-30 00:53:30 -07002285 * Locking: RCU, ctrl lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002286 */
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2289{
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002290 struct pid *pgrp;
2291 pid_t pgrp_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 int retval = tty_check_change(real_tty);
Alan Cox47f86832008-04-30 00:53:30 -07002293 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 if (retval == -EIO)
2296 return -ENOTTY;
2297 if (retval)
2298 return retval;
2299 if (!current->signal->tty ||
2300 (current->signal->tty != real_tty) ||
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002301 (real_tty->session != task_session(current)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return -ENOTTY;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002303 if (get_user(pgrp_nr, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return -EFAULT;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002305 if (pgrp_nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return -EINVAL;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002307 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002308 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002309 retval = -ESRCH;
2310 if (!pgrp)
2311 goto out_unlock;
2312 retval = -EPERM;
2313 if (session_of_pgrp(pgrp) != task_session(current))
2314 goto out_unlock;
2315 retval = 0;
Alan Cox47f86832008-04-30 00:53:30 -07002316 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002317 put_pid(real_tty->pgrp);
2318 real_tty->pgrp = get_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002319 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002320out_unlock:
2321 rcu_read_unlock();
2322 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Alan Coxaf9b8972006-08-27 01:24:01 -07002325/**
2326 * tiocgsid - get session id
2327 * @tty: tty passed by user
2328 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2329 * @p: pointer to returned session id
2330 *
2331 * Obtain the session id of the tty. If there is no session
2332 * return an error.
2333 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002334 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002335 */
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2338{
2339 /*
2340 * (tty == real_tty) is a cheap way of
2341 * testing if the tty is NOT a master pty.
2342 */
2343 if (tty == real_tty && current->signal->tty != real_tty)
2344 return -ENOTTY;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002345 if (!real_tty->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return -ENOTTY;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002347 return put_user(pid_vnr(real_tty->session), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
Alan Coxaf9b8972006-08-27 01:24:01 -07002350/**
2351 * tiocsetd - set line discipline
2352 * @tty: tty device
2353 * @p: pointer to user data
2354 *
2355 * Set the line discipline according to user request.
2356 *
2357 * Locking: see tty_set_ldisc, this function is just a helper
2358 */
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360static int tiocsetd(struct tty_struct *tty, int __user *p)
2361{
2362 int ldisc;
Alan Cox04f378b2008-04-30 00:53:29 -07002363 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 if (get_user(ldisc, p))
2366 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002367
Alan Cox04f378b2008-04-30 00:53:29 -07002368 ret = tty_set_ldisc(tty, ldisc);
Alan Cox04f378b2008-04-30 00:53:29 -07002369
2370 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
Alan Coxaf9b8972006-08-27 01:24:01 -07002373/**
2374 * send_break - performed time break
2375 * @tty: device to break on
2376 * @duration: timeout in mS
2377 *
2378 * Perform a timed break on hardware that lacks its own driver level
2379 * timed break functionality.
2380 *
2381 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002382 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002383 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002384 */
2385
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002386static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Alan Cox9e989662008-07-22 11:18:03 +01002388 int retval;
2389
2390 if (tty->ops->break_ctl == NULL)
2391 return 0;
2392
2393 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2394 retval = tty->ops->break_ctl(tty, duration);
2395 else {
2396 /* Do the work ourselves */
2397 if (tty_write_lock(tty, 0) < 0)
2398 return -EINTR;
2399 retval = tty->ops->break_ctl(tty, -1);
2400 if (retval)
2401 goto out;
2402 if (!signal_pending(current))
2403 msleep_interruptible(duration);
2404 retval = tty->ops->break_ctl(tty, 0);
2405out:
2406 tty_write_unlock(tty);
2407 if (signal_pending(current))
2408 retval = -EINTR;
2409 }
2410 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
Alan Coxaf9b8972006-08-27 01:24:01 -07002413/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002414 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002415 * @tty: tty device
2416 * @file: user file pointer
2417 * @p: pointer to result
2418 *
2419 * Obtain the modem status bits from the tty driver if the feature
2420 * is supported. Return -EINVAL if it is not available.
2421 *
2422 * Locking: none (up to the driver)
2423 */
2424
2425static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
2427 int retval = -EINVAL;
2428
Alan Coxf34d7a52008-04-30 00:54:13 -07002429 if (tty->ops->tiocmget) {
2430 retval = tty->ops->tiocmget(tty, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 if (retval >= 0)
2433 retval = put_user(retval, p);
2434 }
2435 return retval;
2436}
2437
Alan Coxaf9b8972006-08-27 01:24:01 -07002438/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002439 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002440 * @tty: tty device
2441 * @file: user file pointer
2442 * @cmd: command - clear bits, set bits or set all
2443 * @p: pointer to desired bits
2444 *
2445 * Set the modem status bits from the tty driver if the feature
2446 * is supported. Return -EINVAL if it is not available.
2447 *
2448 * Locking: none (up to the driver)
2449 */
2450
2451static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 unsigned __user *p)
2453{
Alan Coxae677512008-07-16 21:56:54 +01002454 int retval;
2455 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Alan Coxae677512008-07-16 21:56:54 +01002457 if (tty->ops->tiocmset == NULL)
2458 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Alan Coxae677512008-07-16 21:56:54 +01002460 retval = get_user(val, p);
2461 if (retval)
2462 return retval;
2463 set = clear = 0;
2464 switch (cmd) {
2465 case TIOCMBIS:
2466 set = val;
2467 break;
2468 case TIOCMBIC:
2469 clear = val;
2470 break;
2471 case TIOCMSET:
2472 set = val;
2473 clear = ~val;
2474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
Alan Coxae677512008-07-16 21:56:54 +01002476 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2477 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2478 return tty->ops->tiocmset(tty, file, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
2480
Alan Coxe8b70e72009-06-11 12:48:02 +01002481struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2482{
2483 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2484 tty->driver->subtype == PTY_TYPE_MASTER)
2485 tty = tty->link;
2486 return tty;
2487}
2488EXPORT_SYMBOL(tty_pair_get_tty);
2489
2490struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
2491{
2492 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2493 tty->driver->subtype == PTY_TYPE_MASTER)
2494 return tty;
2495 return tty->link;
2496}
2497EXPORT_SYMBOL(tty_pair_get_pty);
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499/*
2500 * Split this up, as gcc can choke on it otherwise..
2501 */
Alan Cox04f378b2008-04-30 00:53:29 -07002502long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
2504 struct tty_struct *tty, *real_tty;
2505 void __user *p = (void __user *)arg;
2506 int retval;
2507 struct tty_ldisc *ld;
Alan Cox04f378b2008-04-30 00:53:29 -07002508 struct inode *inode = file->f_dentry->d_inode;
Alan Cox37bdfb02008-02-08 04:18:47 -08002509
Kulikov Vasiliy2b50d242010-06-29 14:15:09 +04002510 tty = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2512 return -EINVAL;
2513
Alan Coxe8b70e72009-06-11 12:48:02 +01002514 real_tty = tty_pair_get_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 /*
2517 * Factor out some common prep work
2518 */
2519 switch (cmd) {
2520 case TIOCSETD:
2521 case TIOCSBRK:
2522 case TIOCCBRK:
2523 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002524 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 retval = tty_check_change(tty);
2526 if (retval)
2527 return retval;
2528 if (cmd != TIOCCBRK) {
2529 tty_wait_until_sent(tty, 0);
2530 if (signal_pending(current))
2531 return -EINTR;
2532 }
2533 break;
2534 }
2535
Alan Cox9e989662008-07-22 11:18:03 +01002536 /*
2537 * Now do the stuff.
2538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002540 case TIOCSTI:
2541 return tiocsti(tty, p);
2542 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002543 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002544 case TIOCSWINSZ:
Alan Coxfc6f6232009-01-02 13:43:17 +00002545 return tiocswinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002546 case TIOCCONS:
2547 return real_tty != tty ? -EINVAL : tioccons(file);
2548 case FIONBIO:
2549 return fionbio(file, p);
2550 case TIOCEXCL:
2551 set_bit(TTY_EXCLUSIVE, &tty->flags);
2552 return 0;
2553 case TIOCNXCL:
2554 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2555 return 0;
2556 case TIOCNOTTY:
2557 if (current->signal->tty != tty)
2558 return -ENOTTY;
2559 no_tty();
2560 return 0;
2561 case TIOCSCTTY:
2562 return tiocsctty(tty, arg);
2563 case TIOCGPGRP:
2564 return tiocgpgrp(tty, real_tty, p);
2565 case TIOCSPGRP:
2566 return tiocspgrp(tty, real_tty, p);
2567 case TIOCGSID:
2568 return tiocgsid(tty, real_tty, p);
2569 case TIOCGETD:
Alan Coxc65c9bc2009-06-11 12:50:12 +01002570 return put_user(tty->ldisc->ops->num, (int __user *)p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002571 case TIOCSETD:
2572 return tiocsetd(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002573 /*
2574 * Break handling
2575 */
2576 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002577 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002578 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002579 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002580 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002581 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002582 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002583 return 0;
2584 case TCSBRK: /* SVID version: non-zero arg --> no break */
2585 /* non-zero arg means wait for all output data
2586 * to be sent (performed above) but don't send break.
2587 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002589 if (!arg)
2590 return send_break(tty, 250);
2591 return 0;
2592 case TCSBRKP: /* support for POSIX tcsendbreak() */
2593 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Alan Cox37bdfb02008-02-08 04:18:47 -08002595 case TIOCMGET:
2596 return tty_tiocmget(tty, file, p);
2597 case TIOCMSET:
2598 case TIOCMBIC:
2599 case TIOCMBIS:
2600 return tty_tiocmset(tty, file, cmd, p);
2601 case TCFLSH:
2602 switch (arg) {
2603 case TCIFLUSH:
2604 case TCIOFLUSH:
2605 /* flush tty buffer and allow ldisc to process ioctl */
2606 tty_buffer_flush(tty);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002607 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002608 }
2609 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002611 if (tty->ops->ioctl) {
2612 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (retval != -ENOIOCTLCMD)
2614 return retval;
2615 }
2616 ld = tty_ldisc_ref_wait(tty);
2617 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002618 if (ld->ops->ioctl) {
2619 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (retval == -ENOIOCTLCMD)
2621 retval = -EINVAL;
2622 }
2623 tty_ldisc_deref(ld);
2624 return retval;
2625}
2626
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002627#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08002628static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002629 unsigned long arg)
2630{
2631 struct inode *inode = file->f_dentry->d_inode;
2632 struct tty_struct *tty = file->private_data;
2633 struct tty_ldisc *ld;
2634 int retval = -ENOIOCTLCMD;
2635
2636 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2637 return -EINVAL;
2638
Alan Coxf34d7a52008-04-30 00:54:13 -07002639 if (tty->ops->compat_ioctl) {
2640 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002641 if (retval != -ENOIOCTLCMD)
2642 return retval;
2643 }
2644
2645 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002646 if (ld->ops->compat_ioctl)
2647 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002648 tty_ldisc_deref(ld);
2649
2650 return retval;
2651}
2652#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654/*
2655 * This implements the "Secure Attention Key" --- the idea is to
2656 * prevent trojan horses by killing all processes associated with this
2657 * tty when the user hits the "Secure Attention Key". Required for
2658 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08002659 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 * This code could be nicer; ideally it should send a HUP, wait a few
2661 * seconds, then send a INT, and then a KILL signal. But you then
2662 * have to coordinate with the init process, since all processes associated
2663 * with the current tty must be dead before the new getty is allowed
2664 * to spawn.
2665 *
2666 * Now, if it would be correct ;-/ The current code has a nasty hole -
2667 * it doesn't catch files in flight. We may send the descriptor to ourselves
2668 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2669 *
2670 * Nasty bug: do_SAK is being called in interrupt context. This can
2671 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2672 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002673void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
2675#ifdef TTY_SOFT_SAK
2676 tty_hangup(tty);
2677#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08002678 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002679 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 int i;
2681 struct file *filp;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002682 struct fdtable *fdt;
Alan Cox37bdfb02008-02-08 04:18:47 -08002683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (!tty)
2685 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002686 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08002687
Dan Carpenterb3f13de2006-12-13 00:35:09 -08002688 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Alan Coxf34d7a52008-04-30 00:54:13 -07002690 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08002691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002693 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002694 do_each_pid_task(session, PIDTYPE_SID, p) {
Eric W. Biederman652486f2006-03-28 16:11:02 -08002695 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07002696 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002697 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002698 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002699 } while_each_pid_task(session, PIDTYPE_SID, p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002700 /* Now kill any processes that happen to have the
2701 * tty open.
2702 */
2703 do_each_thread(g, p) {
2704 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07002706 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002707 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 send_sig(SIGKILL, p, 1);
2709 continue;
2710 }
2711 task_lock(p);
2712 if (p->files) {
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07002713 /*
2714 * We don't take a ref to the file, so we must
2715 * hold ->file_lock instead.
2716 */
2717 spin_lock(&p->files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002718 fdt = files_fdtable(p->files);
Alan Cox37bdfb02008-02-08 04:18:47 -08002719 for (i = 0; i < fdt->max_fds; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 filp = fcheck_files(p->files, i);
2721 if (!filp)
2722 continue;
2723 if (filp->f_op->read == tty_read &&
2724 filp->private_data == tty) {
2725 printk(KERN_NOTICE "SAK: killed process %d"
2726 " (%s): fd#%d opened to the tty\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002727 task_pid_nr(p), p->comm, i);
Eric W. Biederman20ac9432006-04-13 04:49:07 -06002728 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 break;
2730 }
2731 }
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07002732 spin_unlock(&p->files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
2734 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002735 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 read_unlock(&tasklist_lock);
2737#endif
2738}
2739
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002740static void do_SAK_work(struct work_struct *work)
2741{
2742 struct tty_struct *tty =
2743 container_of(work, struct tty_struct, SAK_work);
2744 __do_SAK(tty);
2745}
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747/*
2748 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2749 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2750 * the values which we write to it will be identical to the values which it
2751 * already has. --akpm
2752 */
2753void do_SAK(struct tty_struct *tty)
2754{
2755 if (!tty)
2756 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 schedule_work(&tty->SAK_work);
2758}
2759
2760EXPORT_SYMBOL(do_SAK);
2761
Alan Coxaf9b8972006-08-27 01:24:01 -07002762/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002763 * initialize_tty_struct
2764 * @tty: tty to initialize
2765 *
2766 * This subroutine initializes a tty structure that has been newly
2767 * allocated.
2768 *
2769 * Locking: none - tty in question must not be exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002771
Alan Coxbf970ee2008-10-13 10:42:39 +01002772void initialize_tty_struct(struct tty_struct *tty,
2773 struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
2775 memset(tty, 0, sizeof(struct tty_struct));
Alan Cox9c9f4de2008-10-13 10:37:26 +01002776 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 tty->magic = TTY_MAGIC;
Alan Cox01e1abb2008-07-22 11:16:55 +01002778 tty_ldisc_init(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002779 tty->session = NULL;
2780 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 tty->overrun_time = jiffies;
Alan Cox33f0f882006-01-09 20:54:13 -08002782 tty->buf.head = tty->buf.tail = NULL;
2783 tty_buffer_init(tty);
Arjan van de Ven5785c952006-09-29 02:00:43 -07002784 mutex_init(&tty->termios_mutex);
Alan Coxc65c9bc2009-06-11 12:50:12 +01002785 mutex_init(&tty->ldisc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 init_waitqueue_head(&tty->write_wait);
2787 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00002788 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08002789 mutex_init(&tty->atomic_read_lock);
2790 mutex_init(&tty->atomic_write_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +00002791 mutex_init(&tty->output_lock);
2792 mutex_init(&tty->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 spin_lock_init(&tty->read_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07002794 spin_lock_init(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002796 INIT_WORK(&tty->SAK_work, do_SAK_work);
Alan Coxbf970ee2008-10-13 10:42:39 +01002797
2798 tty->driver = driver;
2799 tty->ops = driver->ops;
2800 tty->index = idx;
2801 tty_line_name(driver, idx, tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
Alan Coxf34d7a52008-04-30 00:54:13 -07002804/**
2805 * tty_put_char - write one character to a tty
2806 * @tty: tty
2807 * @ch: character
2808 *
2809 * Write one byte to the tty using the provided put_char method
2810 * if present. Returns the number of characters successfully output.
2811 *
2812 * Note: the specific put_char operation in the driver layer may go
2813 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002815
Alan Coxf34d7a52008-04-30 00:54:13 -07002816int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Alan Coxf34d7a52008-04-30 00:54:13 -07002818 if (tty->ops->put_char)
2819 return tty->ops->put_char(tty, ch);
2820 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
Alan Coxf34d7a52008-04-30 00:54:13 -07002822EXPORT_SYMBOL_GPL(tty_put_char);
2823
Alan Coxd81ed102008-10-13 10:41:42 +01002824struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002827 * tty_register_device - register a tty device
2828 * @driver: the tty driver that describes the tty device
2829 * @index: the index in the tty driver for this tty device
2830 * @device: a struct device that is associated with this tty device.
2831 * This field is optional, if there is no known struct device
2832 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07002834 * Returns a pointer to the struct device for this tty device
2835 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002836 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002837 * This call is required to be made to register an individual tty device
2838 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2839 * that bit is not set, this function should not be called by a tty
2840 * driver.
2841 *
2842 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002844
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07002845struct device *tty_register_device(struct tty_driver *driver, unsigned index,
2846 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 char name[64];
2849 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2850
2851 if (index >= driver->num) {
2852 printk(KERN_ERR "Attempt to register invalid tty line number "
2853 " (%d).\n", index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002854 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (driver->type == TTY_DRIVER_TYPE_PTY)
2858 pty_line_name(driver, index, name);
2859 else
2860 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002861
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -07002862 return device_create(tty_class, device, dev, NULL, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
Alan Cox7d7b93c2008-10-13 10:42:09 +01002864EXPORT_SYMBOL(tty_register_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002867 * tty_unregister_device - unregister a tty device
2868 * @driver: the tty driver that describes the tty device
2869 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002871 * If a tty device is registered with a call to tty_register_device() then
2872 * this function must be called when the tty device is gone.
2873 *
2874 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877void tty_unregister_device(struct tty_driver *driver, unsigned index)
2878{
Alan Cox37bdfb02008-02-08 04:18:47 -08002879 device_destroy(tty_class,
2880 MKDEV(driver->major, driver->minor_start) + index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882EXPORT_SYMBOL(tty_unregister_device);
2883
2884struct tty_driver *alloc_tty_driver(int lines)
2885{
2886 struct tty_driver *driver;
2887
Jean Delvare506eb992007-07-15 23:40:14 -07002888 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (driver) {
Alan Cox7d7b93c2008-10-13 10:42:09 +01002890 kref_init(&driver->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 driver->magic = TTY_DRIVER_MAGIC;
2892 driver->num = lines;
2893 /* later we'll move allocation of tables here */
2894 }
2895 return driver;
2896}
Alan Cox7d7b93c2008-10-13 10:42:09 +01002897EXPORT_SYMBOL(alloc_tty_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Alan Cox7d7b93c2008-10-13 10:42:09 +01002899static void destruct_tty_driver(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
Alan Cox7d7b93c2008-10-13 10:42:09 +01002901 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
2902 int i;
2903 struct ktermios *tp;
2904 void *p;
2905
2906 if (driver->flags & TTY_DRIVER_INSTALLED) {
2907 /*
2908 * Free the termios and termios_locked structures because
2909 * we don't want to get memory leaks when modular tty
2910 * drivers are removed from the kernel.
2911 */
2912 for (i = 0; i < driver->num; i++) {
2913 tp = driver->termios[i];
2914 if (tp) {
2915 driver->termios[i] = NULL;
2916 kfree(tp);
2917 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01002918 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
2919 tty_unregister_device(driver, i);
2920 }
2921 p = driver->ttys;
2922 proc_tty_unregister_driver(driver);
2923 driver->ttys = NULL;
Alan Coxfe6e29f2008-10-13 10:44:08 +01002924 driver->termios = NULL;
Alan Cox7d7b93c2008-10-13 10:42:09 +01002925 kfree(p);
2926 cdev_del(&driver->cdev);
2927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 kfree(driver);
2929}
2930
Alan Cox7d7b93c2008-10-13 10:42:09 +01002931void tty_driver_kref_put(struct tty_driver *driver)
2932{
2933 kref_put(&driver->kref, destruct_tty_driver);
2934}
2935EXPORT_SYMBOL(tty_driver_kref_put);
2936
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002937void tty_set_operations(struct tty_driver *driver,
2938 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Alan Coxf34d7a52008-04-30 00:54:13 -07002940 driver->ops = op;
2941};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942EXPORT_SYMBOL(tty_set_operations);
2943
Alan Cox7d7b93c2008-10-13 10:42:09 +01002944void put_tty_driver(struct tty_driver *d)
2945{
2946 tty_driver_kref_put(d);
2947}
2948EXPORT_SYMBOL(put_tty_driver);
2949
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950/*
2951 * Called by a tty driver to register itself.
2952 */
2953int tty_register_driver(struct tty_driver *driver)
2954{
2955 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08002956 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 dev_t dev;
2958 void **p = NULL;
2959
Andy Whitcroft543691a2007-05-06 14:49:33 -07002960 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
Alan Coxfe6e29f2008-10-13 10:44:08 +01002961 p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (!p)
2963 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965
2966 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002967 error = alloc_chrdev_region(&dev, driver->minor_start,
2968 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (!error) {
2970 driver->major = MAJOR(dev);
2971 driver->minor_start = MINOR(dev);
2972 }
2973 } else {
2974 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01002975 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
2977 if (error < 0) {
2978 kfree(p);
2979 return error;
2980 }
2981
2982 if (p) {
2983 driver->ttys = (struct tty_struct **)p;
Alan Coxedc6afc2006-12-08 02:38:44 -08002984 driver->termios = (struct ktermios **)(p + driver->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 } else {
2986 driver->ttys = NULL;
2987 driver->termios = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
2989
2990 cdev_init(&driver->cdev, &tty_fops);
2991 driver->cdev.owner = driver->owner;
2992 error = cdev_add(&driver->cdev, dev, driver->num);
2993 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 unregister_chrdev_region(dev, driver->num);
2995 driver->ttys = NULL;
Alan Coxfe6e29f2008-10-13 10:44:08 +01002996 driver->termios = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 kfree(p);
2998 return error;
2999 }
3000
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003001 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003003 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003004
3005 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
3006 for (i = 0; i < driver->num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 tty_register_device(driver, i, NULL);
3008 }
3009 proc_tty_register_driver(driver);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003010 driver->flags |= TTY_DRIVER_INSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return 0;
3012}
3013
3014EXPORT_SYMBOL(tty_register_driver);
3015
3016/*
3017 * Called by a tty driver to unregister itself.
3018 */
3019int tty_unregister_driver(struct tty_driver *driver)
3020{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003021#if 0
3022 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 if (driver->refcount)
3024 return -EBUSY;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003025#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3027 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003028 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003030 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return 0;
3032}
Alan Cox7d7b93c2008-10-13 10:42:09 +01003033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034EXPORT_SYMBOL(tty_unregister_driver);
3035
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003036dev_t tty_devnum(struct tty_struct *tty)
3037{
3038 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3039}
3040EXPORT_SYMBOL(tty_devnum);
3041
3042void proc_clear_tty(struct task_struct *p)
3043{
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003044 unsigned long flags;
Alan Cox9c9f4de2008-10-13 10:37:26 +01003045 struct tty_struct *tty;
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003046 spin_lock_irqsave(&p->sighand->siglock, flags);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003047 tty = p->signal->tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003048 p->signal->tty = NULL;
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003049 spin_unlock_irqrestore(&p->sighand->siglock, flags);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003050 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003051}
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003052
Alan Cox47f86832008-04-30 00:53:30 -07003053/* Called under the sighand lock */
3054
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003055static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003056{
3057 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -07003058 unsigned long flags;
3059 /* We should not have a session or pgrp to put here but.... */
3060 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -06003061 put_pid(tty->session);
3062 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003063 tty->pgrp = get_pid(task_pgrp(tsk));
Alan Cox47f86832008-04-30 00:53:30 -07003064 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3065 tty->session = get_pid(task_session(tsk));
Alan Cox9c9f4de2008-10-13 10:37:26 +01003066 if (tsk->signal->tty) {
3067 printk(KERN_DEBUG "tty not NULL!!\n");
3068 tty_kref_put(tsk->signal->tty);
3069 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003070 }
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003071 put_pid(tsk->signal->tty_old_pgrp);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003072 tsk->signal->tty = tty_kref_get(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003073 tsk->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003074}
3075
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07003076static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003077{
3078 spin_lock_irq(&tsk->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003079 __proc_set_tty(tsk, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003080 spin_unlock_irq(&tsk->sighand->siglock);
3081}
3082
3083struct tty_struct *get_current_tty(void)
3084{
3085 struct tty_struct *tty;
Alan Cox934e6eb2008-10-13 10:40:43 +01003086 unsigned long flags;
3087
3088 spin_lock_irqsave(&current->sighand->siglock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +01003089 tty = tty_kref_get(current->signal->tty);
Alan Cox934e6eb2008-10-13 10:40:43 +01003090 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003091 return tty;
3092}
Heiko Carstensa311f742006-12-13 00:33:41 -08003093EXPORT_SYMBOL_GPL(get_current_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Alan Coxd81ed102008-10-13 10:41:42 +01003095void tty_default_fops(struct file_operations *fops)
3096{
3097 *fops = tty_fops;
3098}
3099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100/*
3101 * Initialize the console device. This is called *early*, so
3102 * we can't necessarily depend on lots of kernel help here.
3103 * Just do some early initializations, and do the complex setup
3104 * later.
3105 */
3106void __init console_init(void)
3107{
3108 initcall_t *call;
3109
3110 /* Setup the default TTY line discipline. */
Alan Cox01e1abb2008-07-22 11:16:55 +01003111 tty_ldisc_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08003114 * set up the console device so that later boot sequences can
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 * inform about problems etc..
3116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 call = __con_initcall_start;
3118 while (call < __con_initcall_end) {
3119 (*call)();
3120 call++;
3121 }
3122}
3123
Kay Sieverse454cea2009-09-18 23:01:12 +02003124static char *tty_devnode(struct device *dev, mode_t *mode)
3125{
3126 if (!mode)
3127 return NULL;
3128 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3129 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3130 *mode = 0666;
3131 return NULL;
3132}
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134static int __init tty_class_init(void)
3135{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003136 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 if (IS_ERR(tty_class))
3138 return PTR_ERR(tty_class);
Kay Sieverse454cea2009-09-18 23:01:12 +02003139 tty_class->devnode = tty_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return 0;
3141}
3142
3143postcore_initcall(tty_class_init);
3144
3145/* 3/2004 jmc: why do these devices exist? */
3146
3147static struct cdev tty_cdev, console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
3149/*
3150 * Ok, now we can initialize the rest of the tty devices and can count
3151 * on memory allocations, interrupts etc..
3152 */
David Howells31d1d482010-08-06 16:34:43 +01003153int __init tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154{
3155 cdev_init(&tty_cdev, &tty_fops);
3156 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3157 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3158 panic("Couldn't register /dev/tty driver\n");
Alan Coxd81ed102008-10-13 10:41:42 +01003159 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003160 "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
3162 cdev_init(&console_cdev, &console_fops);
3163 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3164 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3165 panic("Couldn't register /dev/console driver\n");
Alan Coxd81ed102008-10-13 10:41:42 +01003166 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003167 "console");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169#ifdef CONFIG_VT
Alan Coxd81ed102008-10-13 10:41:42 +01003170 vty_init(&console_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171#endif
3172 return 0;
3173}
David Howells31d1d482010-08-06 16:34:43 +01003174