blob: cc941a3504d755c7aa02ed1d53d89d2db5d6a1a3 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
140static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
Alan Cox37bdfb02008-02-08 04:18:47 -0800141ssize_t redirected_tty_write(struct file *, const char __user *,
142 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static unsigned int tty_poll(struct file *, poll_table *);
144static int tty_open(struct inode *, struct file *);
Alan Cox04f378b2008-04-30 00:53:29 -0700145long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700146#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -0800147static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700148 unsigned long arg);
149#else
150#define tty_compat_ioctl NULL
151#endif
Alan Cox37bdfb02008-02-08 04:18:47 -0800152static int tty_fasync(int fd, struct file *filp, int on);
Christoph Hellwigd5698c22007-02-10 01:46:46 -0800153static void release_tty(struct tty_struct *tty, int idx);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700154static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700155static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Alan Coxaf9b8972006-08-27 01:24:01 -0700157/**
158 * alloc_tty_struct - allocate a tty object
159 *
160 * Return a new empty tty structure. The data fields have not
161 * been initialized in any way but has been zeroed
162 *
163 * Locking: none
Alan Coxaf9b8972006-08-27 01:24:01 -0700164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Alan Coxbf970ee2008-10-13 10:42:39 +0100166struct tty_struct *alloc_tty_struct(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Alan Cox1266b1e2006-09-29 02:00:40 -0700168 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Alan Coxaf9b8972006-08-27 01:24:01 -0700171/**
172 * free_tty_struct - free a disused tty
173 * @tty: tty struct to free
174 *
175 * Free the write buffers, tty queue and tty memory itself.
176 *
177 * Locking: none. Must be called after tty is definitely unused
178 */
179
Alan Coxbf970ee2008-10-13 10:42:39 +0100180void free_tty_struct(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 kfree(tty->write_buf);
Alan Cox33f0f882006-01-09 20:54:13 -0800183 tty_buffer_free_all(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 kfree(tty);
185}
186
187#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
188
Alan Coxaf9b8972006-08-27 01:24:01 -0700189/**
190 * tty_name - return tty naming
191 * @tty: tty structure
192 * @buf: buffer for output
193 *
194 * Convert a tty structure into a name. The name reflects the kernel
195 * naming policy and if udev is in use may not reflect user space
196 *
197 * Locking: none
198 */
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200char *tty_name(struct tty_struct *tty, char *buf)
201{
202 if (!tty) /* Hmm. NULL pointer. That's fun. */
203 strcpy(buf, "NULL tty");
204 else
205 strcpy(buf, tty->name);
206 return buf;
207}
208
209EXPORT_SYMBOL(tty_name);
210
Andrew Mortond769a662005-05-05 16:15:50 -0700211int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 const char *routine)
213{
214#ifdef TTY_PARANOIA_CHECK
215 if (!tty) {
216 printk(KERN_WARNING
217 "null TTY for (%d:%d) in %s\n",
218 imajor(inode), iminor(inode), routine);
219 return 1;
220 }
221 if (tty->magic != TTY_MAGIC) {
222 printk(KERN_WARNING
223 "bad magic number for tty struct (%d:%d) in %s\n",
224 imajor(inode), iminor(inode), routine);
225 return 1;
226 }
227#endif
228 return 0;
229}
230
231static int check_tty_count(struct tty_struct *tty, const char *routine)
232{
233#ifdef CHECK_TTY_COUNT
234 struct list_head *p;
235 int count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 file_list_lock();
238 list_for_each(p, &tty->tty_files) {
239 count++;
240 }
241 file_list_unlock();
242 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
243 tty->driver->subtype == PTY_TYPE_SLAVE &&
244 tty->link && tty->link->count)
245 count++;
246 if (tty->count != count) {
247 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
248 "!= #fd's(%d) in %s\n",
249 tty->name, tty->count, count, routine);
250 return count;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#endif
253 return 0;
254}
255
Alan Coxaf9b8972006-08-27 01:24:01 -0700256/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700257 * get_tty_driver - find device of a tty
258 * @dev_t: device identifier
259 * @index: returns the index of the tty
260 *
261 * This routine returns a tty driver structure, given a device number
262 * and also passes back the index number.
263 *
264 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267static struct tty_driver *get_tty_driver(dev_t device, int *index)
268{
269 struct tty_driver *p;
270
271 list_for_each_entry(p, &tty_drivers, tty_drivers) {
272 dev_t base = MKDEV(p->major, p->minor_start);
273 if (device < base || device >= base + p->num)
274 continue;
275 *index = device - base;
Alan Cox7d7b93c2008-10-13 10:42:09 +0100276 return tty_driver_kref_get(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 return NULL;
279}
280
Jason Wesself2d937f2008-04-17 20:05:37 +0200281#ifdef CONFIG_CONSOLE_POLL
282
283/**
284 * tty_find_polling_driver - find device of a polled tty
285 * @name: name string to match
286 * @line: pointer to resulting tty line nr
287 *
288 * This routine returns a tty driver structure, given a name
289 * and the condition that the tty driver is capable of polled
290 * operation.
291 */
292struct tty_driver *tty_find_polling_driver(char *name, int *line)
293{
294 struct tty_driver *p, *res = NULL;
295 int tty_line = 0;
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500296 int len;
Alan Cox5f0878a2009-06-11 12:46:41 +0100297 char *str, *stp;
Jason Wesself2d937f2008-04-17 20:05:37 +0200298
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500299 for (str = name; *str; str++)
300 if ((*str >= '0' && *str <= '9') || *str == ',')
301 break;
302 if (!*str)
303 return NULL;
304
305 len = str - name;
306 tty_line = simple_strtoul(str, &str, 10);
307
Jason Wesself2d937f2008-04-17 20:05:37 +0200308 mutex_lock(&tty_mutex);
309 /* Search through the tty devices to look for a match */
310 list_for_each_entry(p, &tty_drivers, tty_drivers) {
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500311 if (strncmp(name, p->name, len) != 0)
312 continue;
Alan Cox5f0878a2009-06-11 12:46:41 +0100313 stp = str;
314 if (*stp == ',')
315 stp++;
316 if (*stp == '\0')
317 stp = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +0200318
Alan Coxf34d7a52008-04-30 00:54:13 -0700319 if (tty_line >= 0 && tty_line <= p->num && p->ops &&
Alan Cox5f0878a2009-06-11 12:46:41 +0100320 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
Alan Cox7d7b93c2008-10-13 10:42:09 +0100321 res = tty_driver_kref_get(p);
Jason Wesself2d937f2008-04-17 20:05:37 +0200322 *line = tty_line;
323 break;
324 }
325 }
326 mutex_unlock(&tty_mutex);
327
328 return res;
329}
330EXPORT_SYMBOL_GPL(tty_find_polling_driver);
331#endif
332
Alan Coxaf9b8972006-08-27 01:24:01 -0700333/**
334 * tty_check_change - check for POSIX terminal changes
335 * @tty: tty to check
336 *
337 * If we try to write to, or set the state of, a terminal and we're
338 * not in the foreground, send a SIGTTOU. If the signal is blocked or
339 * ignored, go ahead and perform the operation. (POSIX 7.2)
340 *
Alan Cox978e5952008-04-30 00:53:59 -0700341 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700343
Alan Cox37bdfb02008-02-08 04:18:47 -0800344int tty_check_change(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Alan Cox47f86832008-04-30 00:53:30 -0700346 unsigned long flags;
347 int ret = 0;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (current->signal->tty != tty)
350 return 0;
Alan Cox47f86832008-04-30 00:53:30 -0700351
352 spin_lock_irqsave(&tty->ctrl_lock, flags);
353
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800354 if (!tty->pgrp) {
355 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700356 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800358 if (task_pgrp(current) == tty->pgrp)
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700359 goto out_unlock;
360 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (is_ignored(SIGTTOU))
Alan Cox47f86832008-04-30 00:53:30 -0700362 goto out;
363 if (is_current_pgrp_orphaned()) {
364 ret = -EIO;
365 goto out;
366 }
Oleg Nesterov040b6362007-06-01 00:46:53 -0700367 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
368 set_thread_flag(TIF_SIGPENDING);
Alan Cox47f86832008-04-30 00:53:30 -0700369 ret = -ERESTARTSYS;
370out:
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700371 return ret;
372out_unlock:
Alan Cox47f86832008-04-30 00:53:30 -0700373 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
374 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377EXPORT_SYMBOL(tty_check_change);
378
Alan Cox37bdfb02008-02-08 04:18:47 -0800379static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 size_t count, loff_t *ppos)
381{
382 return 0;
383}
384
Alan Cox37bdfb02008-02-08 04:18:47 -0800385static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 size_t count, loff_t *ppos)
387{
388 return -EIO;
389}
390
391/* No kernel lock held - none needed ;) */
Alan Cox37bdfb02008-02-08 04:18:47 -0800392static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
395}
396
Alan Cox04f378b2008-04-30 00:53:29 -0700397static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
398 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700399{
400 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
401}
402
Alan Cox37bdfb02008-02-08 04:18:47 -0800403static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700404 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
407}
408
Arjan van de Ven62322d22006-07-03 00:24:21 -0700409static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 .llseek = no_llseek,
411 .read = tty_read,
412 .write = tty_write,
413 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700414 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700415 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 .open = tty_open,
417 .release = tty_release,
418 .fasync = tty_fasync,
419};
420
Arjan van de Ven62322d22006-07-03 00:24:21 -0700421static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 .llseek = no_llseek,
423 .read = tty_read,
424 .write = redirected_tty_write,
425 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700426 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700427 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 .open = tty_open,
429 .release = tty_release,
430 .fasync = tty_fasync,
431};
432
Arjan van de Ven62322d22006-07-03 00:24:21 -0700433static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 .llseek = no_llseek,
435 .read = hung_up_tty_read,
436 .write = hung_up_tty_write,
437 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700438 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700439 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 .release = tty_release,
441};
442
443static DEFINE_SPINLOCK(redirect_lock);
444static struct file *redirect;
445
446/**
447 * tty_wakeup - request more data
448 * @tty: terminal
449 *
450 * Internal and external helper for wakeups of tty. This function
451 * informs the line discipline if present that the driver is ready
452 * to receive more output data.
453 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455void tty_wakeup(struct tty_struct *tty)
456{
457 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -0800458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
460 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800461 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100462 if (ld->ops->write_wakeup)
463 ld->ops->write_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 tty_ldisc_deref(ld);
465 }
466 }
Davide Libenzi4b194492009-03-31 15:24:24 -0700467 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470EXPORT_SYMBOL_GPL(tty_wakeup);
471
472/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700473 * do_tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +0000474 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -0700475 *
Alan Cox1bad8792008-07-22 23:38:04 +0100476 * This can be called by the "eventd" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -0700477 * synchronous but doesn't hold any locks, so we need to make sure we
478 * have the appropriate locks for what we're doing.
479 *
480 * The hangup event clears any pending redirections onto the hung up
481 * device. It ensures future writes will error and it does the needed
482 * line discipline hangup and signal delivery. The tty object itself
483 * remains intact.
484 *
485 * Locking:
486 * BKL
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800487 * redirect lock for undoing redirection
488 * file list lock for manipulating list of ttys
489 * tty_ldisc_lock from called functions
490 * termios_mutex resetting termios data
491 * tasklist_lock to walk task list for hangup event
492 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 */
David Howells65f27f32006-11-22 14:55:48 +0000494static void do_tty_hangup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
David Howells65f27f32006-11-22 14:55:48 +0000496 struct tty_struct *tty =
497 container_of(work, struct tty_struct, hangup_work);
Alan Cox37bdfb02008-02-08 04:18:47 -0800498 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct file *filp, *f = NULL;
500 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 int closecount = 0, n;
Alan Cox47f86832008-04-30 00:53:30 -0700502 unsigned long flags;
Alan Cox9c9f4de2008-10-13 10:37:26 +0100503 int refs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 if (!tty)
506 return;
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 spin_lock(&redirect_lock);
510 if (redirect && redirect->private_data == tty) {
511 f = redirect;
512 redirect = NULL;
513 }
514 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800515
Alan Cox38c70b22009-11-30 13:18:40 +0000516 /* inuse_filps is protected by the single kernel lock */
517 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 check_tty_count(tty, "do_tty_hangup");
519 file_list_lock();
520 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Eric Dumazet2f512012005-10-30 15:02:16 -0800521 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (filp->f_op->write == redirected_tty_write)
523 cons_filp = filp;
524 if (filp->f_op->write != tty_write)
525 continue;
526 closecount++;
527 tty_fasync(-1, filp, 0); /* can't block */
528 filp->f_op = &hung_up_tty_fops;
529 }
530 file_list_unlock();
Alan Cox37bdfb02008-02-08 04:18:47 -0800531
Alan Coxc65c9bc2009-06-11 12:50:12 +0100532 tty_ldisc_hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800535 if (tty->session) {
536 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800537 spin_lock_irq(&p->sighand->siglock);
Alan Cox9c9f4de2008-10-13 10:37:26 +0100538 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 p->signal->tty = NULL;
Alan Cox9c9f4de2008-10-13 10:37:26 +0100540 /* We defer the dereferences outside fo
541 the tasklist lock */
542 refs++;
543 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800544 if (!p->signal->leader) {
545 spin_unlock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 continue;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800547 }
548 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
549 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800550 put_pid(p->signal->tty_old_pgrp); /* A noop */
Alan Cox47f86832008-04-30 00:53:30 -0700551 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800552 if (tty->pgrp)
553 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
Alan Cox47f86832008-04-30 00:53:30 -0700554 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800555 spin_unlock_irq(&p->sighand->siglock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800556 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 read_unlock(&tasklist_lock);
559
Alan Cox47f86832008-04-30 00:53:30 -0700560 spin_lock_irqsave(&tty->ctrl_lock, flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100561 clear_bit(TTY_THROTTLED, &tty->flags);
562 clear_bit(TTY_PUSH, &tty->flags);
563 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -0600564 put_pid(tty->session);
565 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800566 tty->session = NULL;
567 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 tty->ctrl_status = 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100569 set_bit(TTY_HUPPED, &tty->flags);
Alan Cox47f86832008-04-30 00:53:30 -0700570 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
571
Alan Cox9c9f4de2008-10-13 10:37:26 +0100572 /* Account for the p->signal references we killed */
573 while (refs--)
574 tty_kref_put(tty);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /*
Alan Cox37bdfb02008-02-08 04:18:47 -0800577 * If one of the devices matches a console pointer, we
578 * cannot just call hangup() because that will cause
579 * tty->count and state->count to go out of sync.
580 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
582 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -0700583 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -0700585 tty->ops->close(tty, cons_filp);
586 } else if (tty->ops->hangup)
587 (tty->ops->hangup)(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800588 /*
589 * We don't want to have driver/ldisc interactions beyond
590 * the ones we did here. The driver layer expects no
591 * calls after ->hangup() from the ldisc side. However we
592 * can't yet guarantee all that.
593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 set_bit(TTY_HUPPED, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100595 tty_ldisc_enable(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 unlock_kernel();
597 if (f)
598 fput(f);
599}
600
Alan Coxaf9b8972006-08-27 01:24:01 -0700601/**
602 * tty_hangup - trigger a hangup event
603 * @tty: tty to hangup
604 *
605 * A carrier loss (virtual or otherwise) has occurred on this like
606 * schedule a hangup sequence to run after this event.
607 */
608
Alan Cox37bdfb02008-02-08 04:18:47 -0800609void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611#ifdef TTY_DEBUG_HANGUP
612 char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
614#endif
615 schedule_work(&tty->hangup_work);
616}
617
618EXPORT_SYMBOL(tty_hangup);
619
Alan Coxaf9b8972006-08-27 01:24:01 -0700620/**
621 * tty_vhangup - process vhangup
622 * @tty: tty to hangup
623 *
624 * The user has asked via system call for the terminal to be hung up.
625 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200626 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -0700627 */
628
Alan Cox37bdfb02008-02-08 04:18:47 -0800629void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631#ifdef TTY_DEBUG_HANGUP
632 char buf[64];
633
634 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
635#endif
David Howells65f27f32006-11-22 14:55:48 +0000636 do_tty_hangup(&tty->hangup_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
Alan Cox37bdfb02008-02-08 04:18:47 -0800638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639EXPORT_SYMBOL(tty_vhangup);
640
Alan Coxaf9b8972006-08-27 01:24:01 -0700641/**
Alan Cox2cb59982008-10-13 10:40:30 +0100642 * tty_vhangup_self - process vhangup for own ctty
643 *
644 * Perform a vhangup on the current controlling tty
645 */
646
647void tty_vhangup_self(void)
648{
649 struct tty_struct *tty;
650
Alan Cox2cb59982008-10-13 10:40:30 +0100651 tty = get_current_tty();
652 if (tty) {
653 tty_vhangup(tty);
654 tty_kref_put(tty);
655 }
Alan Cox2cb59982008-10-13 10:40:30 +0100656}
657
658/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700659 * tty_hung_up_p - was tty hung up
660 * @filp: file pointer of tty
661 *
662 * Return true if the tty has been subject to a vhangup or a carrier
663 * loss
664 */
665
Alan Cox37bdfb02008-02-08 04:18:47 -0800666int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 return (filp->f_op == &hung_up_tty_fops);
669}
670
671EXPORT_SYMBOL(tty_hung_up_p);
672
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800673static void session_clear_tty(struct pid *session)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800674{
675 struct task_struct *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800676 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800677 proc_clear_tty(p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800678 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800679}
680
Alan Coxaf9b8972006-08-27 01:24:01 -0700681/**
682 * disassociate_ctty - disconnect controlling tty
683 * @on_exit: true if exiting so need to "hang up" the session
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700685 * This function is typically called only by the session leader, when
686 * it wants to disassociate itself from its controlling tty.
687 *
688 * It performs the following functions:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
690 * (2) Clears the tty from being controlling the session
691 * (3) Clears the controlling tty for all processes in the
692 * session group.
693 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700694 * The argument on_exit is set to 1 if called when a process is
695 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
696 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800697 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -0700698 * BKL is taken for hysterical raisins
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800699 * tty_mutex is taken to protect tty
700 * ->siglock is taken to protect ->signal/->sighand
701 * tasklist_lock is taken to walk process list for sessions
702 * ->siglock is taken to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705void disassociate_ctty(int on_exit)
706{
707 struct tty_struct *tty;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800708 struct pid *tty_pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800711 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800713 tty_pgrp = get_pid(tty->pgrp);
Alan Cox452a00d2008-10-13 10:39:13 +0100714 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
716 tty_vhangup(tty);
Alan Cox04f378b2008-04-30 00:53:29 -0700717 unlock_kernel();
Alan Cox452a00d2008-10-13 10:39:13 +0100718 tty_kref_put(tty);
Eric W. Biederman680a9672007-02-12 00:52:52 -0800719 } else if (on_exit) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800720 struct pid *old_pgrp;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800721 spin_lock_irq(&current->sighand->siglock);
722 old_pgrp = current->signal->tty_old_pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800723 current->signal->tty_old_pgrp = NULL;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800724 spin_unlock_irq(&current->sighand->siglock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800725 if (old_pgrp) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800726 kill_pgrp(old_pgrp, SIGHUP, on_exit);
727 kill_pgrp(old_pgrp, SIGCONT, on_exit);
728 put_pid(old_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return;
731 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800732 if (tty_pgrp) {
733 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (!on_exit)
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800735 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
736 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800739 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700740 put_pid(current->signal->tty_old_pgrp);
Randy Dunlap23cac8d2007-02-20 13:58:05 -0800741 current->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800742 spin_unlock_irq(&current->sighand->siglock);
743
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800744 tty = get_current_tty();
745 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -0700746 unsigned long flags;
747 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800748 put_pid(tty->session);
749 put_pid(tty->pgrp);
750 tty->session = NULL;
751 tty->pgrp = NULL;
Alan Cox47f86832008-04-30 00:53:30 -0700752 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +0100753 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800754 } else {
755#ifdef TTY_DEBUG_HANGUP
756 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
757 " = NULL", tty);
758#endif
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /* Now clear signal->tty under the lock */
762 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800763 session_clear_tty(task_session(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700767/**
768 *
769 * no_tty - Ensure the current process does not have a controlling tty
770 */
771void no_tty(void)
772{
773 struct task_struct *tsk = current;
Alan Cox04f378b2008-04-30 00:53:29 -0700774 lock_kernel();
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700775 if (tsk->signal->leader)
776 disassociate_ctty(0);
Alan Cox04f378b2008-04-30 00:53:29 -0700777 unlock_kernel();
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700778 proc_clear_tty(tsk);
779}
780
Alan Coxaf9b8972006-08-27 01:24:01 -0700781
782/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200783 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700784 * @tty: tty to stop
785 *
786 * Perform flow control to the driver. For PTY/TTY pairs we
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200787 * must also propagate the TIOCKPKT status. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -0700788 * on an already stopped device and will not re-call the driver
789 * method.
790 *
791 * This functionality is used by both the line disciplines for
792 * halting incoming flow and by the driver. It may therefore be
793 * called from any context, may be under the tty atomic_write_lock
794 * but not always.
795 *
796 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700797 * Uses the tty control lock internally
Alan Coxaf9b8972006-08-27 01:24:01 -0700798 */
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800void stop_tty(struct tty_struct *tty)
801{
Alan Cox04f378b2008-04-30 00:53:29 -0700802 unsigned long flags;
803 spin_lock_irqsave(&tty->ctrl_lock, flags);
804 if (tty->stopped) {
805 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 tty->stopped = 1;
809 if (tty->link && tty->link->packet) {
810 tty->ctrl_status &= ~TIOCPKT_START;
811 tty->ctrl_status |= TIOCPKT_STOP;
Davide Libenzi4b194492009-03-31 15:24:24 -0700812 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Alan Cox04f378b2008-04-30 00:53:29 -0700814 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700815 if (tty->ops->stop)
816 (tty->ops->stop)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819EXPORT_SYMBOL(stop_tty);
820
Alan Coxaf9b8972006-08-27 01:24:01 -0700821/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200822 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700823 * @tty: tty to start
824 *
825 * Start a tty that has been stopped if at all possible. Perform
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200826 * any necessary wakeups and propagate the TIOCPKT status. If this
Alan Coxaf9b8972006-08-27 01:24:01 -0700827 * is the tty was previous stopped and is being started then the
828 * driver start method is invoked and the line discipline woken.
829 *
830 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700831 * ctrl_lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700832 */
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834void start_tty(struct tty_struct *tty)
835{
Alan Cox04f378b2008-04-30 00:53:29 -0700836 unsigned long flags;
837 spin_lock_irqsave(&tty->ctrl_lock, flags);
838 if (!tty->stopped || tty->flow_stopped) {
839 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 tty->stopped = 0;
843 if (tty->link && tty->link->packet) {
844 tty->ctrl_status &= ~TIOCPKT_STOP;
845 tty->ctrl_status |= TIOCPKT_START;
Davide Libenzi4b194492009-03-31 15:24:24 -0700846 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Alan Cox04f378b2008-04-30 00:53:29 -0700848 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700849 if (tty->ops->start)
850 (tty->ops->start)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* If we have a running line discipline it may need kicking */
852 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855EXPORT_SYMBOL(start_tty);
856
Alan Coxaf9b8972006-08-27 01:24:01 -0700857/**
858 * tty_read - read method for tty device files
859 * @file: pointer to tty file
860 * @buf: user buffer
861 * @count: size of user buffer
862 * @ppos: unused
863 *
864 * Perform the read system call function on this terminal device. Checks
865 * for hung up devices before calling the line discipline method.
866 *
867 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -0700868 * Locks the line discipline internally while needed. Multiple
869 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -0700870 */
871
Alan Cox37bdfb02008-02-08 04:18:47 -0800872static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 loff_t *ppos)
874{
875 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -0800876 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 struct inode *inode;
878 struct tty_ldisc *ld;
879
880 tty = (struct tty_struct *)file->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -0800881 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (tty_paranoia_check(tty, inode, "tty_read"))
883 return -EIO;
884 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
885 return -EIO;
886
887 /* We want to wait for the line discipline to sort out in this
888 situation */
889 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +0100890 if (ld->ops->read)
891 i = (ld->ops->read)(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 else
893 i = -EIO;
894 tty_ldisc_deref(ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (i > 0)
896 inode->i_atime = current_fs_time(inode->i_sb);
897 return i;
898}
899
Alan Cox9c1729d2007-07-15 23:39:43 -0700900void tty_write_unlock(struct tty_struct *tty)
901{
902 mutex_unlock(&tty->atomic_write_lock);
Davide Libenzi4b194492009-03-31 15:24:24 -0700903 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Alan Cox9c1729d2007-07-15 23:39:43 -0700904}
905
906int tty_write_lock(struct tty_struct *tty, int ndelay)
907{
908 if (!mutex_trylock(&tty->atomic_write_lock)) {
909 if (ndelay)
910 return -EAGAIN;
911 if (mutex_lock_interruptible(&tty->atomic_write_lock))
912 return -ERESTARTSYS;
913 }
914 return 0;
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/*
918 * Split writes up in sane blocksizes to avoid
919 * denial-of-service type attacks
920 */
921static inline ssize_t do_tty_write(
922 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
923 struct tty_struct *tty,
924 struct file *file,
925 const char __user *buf,
926 size_t count)
927{
Alan Cox9c1729d2007-07-15 23:39:43 -0700928 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -0800930
Alan Cox9c1729d2007-07-15 23:39:43 -0700931 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
932 if (ret < 0)
933 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /*
936 * We chunk up writes into a temporary buffer. This
937 * simplifies low-level drivers immensely, since they
938 * don't have locking issues and user mode accesses.
939 *
940 * But if TTY_NO_WRITE_SPLIT is set, we should use a
941 * big chunk-size..
942 *
943 * The default chunk-size is 2kB, because the NTTY
944 * layer has problems with bigger chunks. It will
945 * claim to be able to handle more characters than
946 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -0700947 *
948 * FIXME: This can probably go away now except that 64K chunks
949 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
951 chunk = 2048;
952 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
953 chunk = 65536;
954 if (count < chunk)
955 chunk = count;
956
Ingo Molnar70522e12006-03-23 03:00:31 -0800957 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (tty->write_cnt < chunk) {
Jason Wessel402fda92008-10-13 10:45:36 +0100959 unsigned char *buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if (chunk < 1024)
962 chunk = 1024;
963
Jason Wessel402fda92008-10-13 10:45:36 +0100964 buf_chunk = kmalloc(chunk, GFP_KERNEL);
965 if (!buf_chunk) {
Alan Cox9c1729d2007-07-15 23:39:43 -0700966 ret = -ENOMEM;
967 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969 kfree(tty->write_buf);
970 tty->write_cnt = chunk;
Jason Wessel402fda92008-10-13 10:45:36 +0100971 tty->write_buf = buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
974 /* Do the write .. */
975 for (;;) {
976 size_t size = count;
977 if (size > chunk)
978 size = chunk;
979 ret = -EFAULT;
980 if (copy_from_user(tty->write_buf, buf, size))
981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (ret <= 0)
984 break;
985 written += ret;
986 buf += ret;
987 count -= ret;
988 if (!count)
989 break;
990 ret = -ERESTARTSYS;
991 if (signal_pending(current))
992 break;
993 cond_resched();
994 }
995 if (written) {
Josef Sipeka7113a92006-12-08 02:36:55 -0800996 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 inode->i_mtime = current_fs_time(inode->i_sb);
998 ret = written;
999 }
Alan Cox9c1729d2007-07-15 23:39:43 -07001000out:
1001 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return ret;
1003}
1004
Alan Cox95f9bfc2008-10-13 10:39:23 +01001005/**
1006 * tty_write_message - write a message to a certain tty, not just the console.
1007 * @tty: the destination tty_struct
1008 * @msg: the message to write
1009 *
1010 * This is used for messages that need to be redirected to a specific tty.
1011 * We don't put it into the syslog queue right now maybe in the future if
1012 * really needed.
1013 *
1014 * We must still hold the BKL and test the CLOSING flag for the moment.
1015 */
1016
1017void tty_write_message(struct tty_struct *tty, char *msg)
1018{
Alan Cox95f9bfc2008-10-13 10:39:23 +01001019 if (tty) {
1020 mutex_lock(&tty->atomic_write_lock);
Alan Coxeeb89d92009-11-30 13:18:29 +00001021 lock_kernel();
1022 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
1023 unlock_kernel();
Alan Cox95f9bfc2008-10-13 10:39:23 +01001024 tty->ops->write(tty, msg, strlen(msg));
Alan Coxeeb89d92009-11-30 13:18:29 +00001025 } else
1026 unlock_kernel();
Alan Cox95f9bfc2008-10-13 10:39:23 +01001027 tty_write_unlock(tty);
1028 }
Alan Cox95f9bfc2008-10-13 10:39:23 +01001029 return;
1030}
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Alan Coxaf9b8972006-08-27 01:24:01 -07001033/**
1034 * tty_write - write method for tty device file
1035 * @file: tty file pointer
1036 * @buf: user data to write
1037 * @count: bytes to write
1038 * @ppos: unused
1039 *
1040 * Write data to a tty device via the line discipline.
1041 *
1042 * Locking:
1043 * Locks the line discipline as required
1044 * Writes to the tty driver are serialized by the atomic_write_lock
1045 * and are then processed in chunks to the device. The line discipline
Joe Petersona88a69c2009-01-02 13:40:53 +00001046 * write method will not be invoked in parallel for each device.
Alan Coxaf9b8972006-08-27 01:24:01 -07001047 */
1048
Alan Cox37bdfb02008-02-08 04:18:47 -08001049static ssize_t tty_write(struct file *file, const char __user *buf,
1050 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Alan Cox37bdfb02008-02-08 04:18:47 -08001052 struct tty_struct *tty;
Josef Sipeka7113a92006-12-08 02:36:55 -08001053 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 ssize_t ret;
1055 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 tty = (struct tty_struct *)file->private_data;
1058 if (tty_paranoia_check(tty, inode, "tty_write"))
1059 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001060 if (!tty || !tty->ops->write ||
Alan Cox37bdfb02008-02-08 04:18:47 -08001061 (test_bit(TTY_IO_ERROR, &tty->flags)))
1062 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001063 /* Short term debug to catch buggy drivers */
1064 if (tty->ops->write_room == NULL)
1065 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1066 tty->driver->name);
Alan Cox37bdfb02008-02-08 04:18:47 -08001067 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001068 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ret = -EIO;
1070 else
Alan Coxa352def2008-07-16 21:53:12 +01001071 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 tty_ldisc_deref(ld);
1073 return ret;
1074}
1075
Alan Cox37bdfb02008-02-08 04:18:47 -08001076ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1077 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 struct file *p = NULL;
1080
1081 spin_lock(&redirect_lock);
1082 if (redirect) {
1083 get_file(redirect);
1084 p = redirect;
1085 }
1086 spin_unlock(&redirect_lock);
1087
1088 if (p) {
1089 ssize_t res;
1090 res = vfs_write(p, buf, count, &p->f_pos);
1091 fput(p);
1092 return res;
1093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return tty_write(file, buf, count, ppos);
1095}
1096
1097static char ptychar[] = "pqrstuvwxyzabcde";
1098
Alan Coxaf9b8972006-08-27 01:24:01 -07001099/**
1100 * pty_line_name - generate name for a pty
1101 * @driver: the tty driver in use
1102 * @index: the minor number
1103 * @p: output buffer of at least 6 bytes
1104 *
1105 * Generate a name from a driver reference and write it to the output
1106 * buffer.
1107 *
1108 * Locking: None
1109 */
1110static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 int i = index + driver->name_base;
1113 /* ->name is initialized to "ttyp", but "tty" is expected */
1114 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08001115 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1116 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Alan Coxaf9b8972006-08-27 01:24:01 -07001119/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001120 * tty_line_name - generate name for a tty
Alan Coxaf9b8972006-08-27 01:24:01 -07001121 * @driver: the tty driver in use
1122 * @index: the minor number
1123 * @p: output buffer of at least 7 bytes
1124 *
1125 * Generate a name from a driver reference and write it to the output
1126 * buffer.
1127 *
1128 * Locking: None
1129 */
1130static void tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1133}
1134
Alan Cox99f1fe12008-10-13 10:42:00 +01001135/**
1136 * tty_driver_lookup_tty() - find an existing tty, if any
1137 * @driver: the driver for the tty
1138 * @idx: the minor number
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001139 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001140 * Return the tty, if found or ERR_PTR() otherwise.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001141 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001142 * Locking: tty_mutex must be held. If tty is found, the mutex must
1143 * be held until the 'fast-open' is also done. Will change once we
1144 * have refcounting in the driver and per driver locking
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001145 */
Jason Wessela47d5452009-01-02 13:43:04 +00001146static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001147 struct inode *inode, int idx)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001148{
1149 struct tty_struct *tty;
1150
Alan Cox99f1fe12008-10-13 10:42:00 +01001151 if (driver->ops->lookup)
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001152 return driver->ops->lookup(driver, inode, idx);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001153
Alan Cox8b0a88d2008-10-13 10:42:19 +01001154 tty = driver->ttys[idx];
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001155 return tty;
1156}
1157
Alan Cox99f1fe12008-10-13 10:42:00 +01001158/**
Alan Coxbf970ee2008-10-13 10:42:39 +01001159 * tty_init_termios - helper for termios setup
1160 * @tty: the tty to set up
1161 *
1162 * Initialise the termios structures for this tty. Thus runs under
1163 * the tty_mutex currently so we can be relaxed about ordering.
1164 */
1165
1166int tty_init_termios(struct tty_struct *tty)
1167{
Alan Coxfe6e29f2008-10-13 10:44:08 +01001168 struct ktermios *tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001169 int idx = tty->index;
1170
1171 tp = tty->driver->termios[idx];
Alan Coxbf970ee2008-10-13 10:42:39 +01001172 if (tp == NULL) {
Alan Coxfe6e29f2008-10-13 10:44:08 +01001173 tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
1174 if (tp == NULL)
Alan Coxbf970ee2008-10-13 10:42:39 +01001175 return -ENOMEM;
Alan Coxbf970ee2008-10-13 10:42:39 +01001176 memcpy(tp, &tty->driver->init_termios,
1177 sizeof(struct ktermios));
1178 tty->driver->termios[idx] = tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001179 }
1180 tty->termios = tp;
Alan Coxfe6e29f2008-10-13 10:44:08 +01001181 tty->termios_locked = tp + 1;
Alan Coxbf970ee2008-10-13 10:42:39 +01001182
1183 /* Compatibility until drivers always set this */
1184 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1185 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1186 return 0;
1187}
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001188EXPORT_SYMBOL_GPL(tty_init_termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001189
1190/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001191 * tty_driver_install_tty() - install a tty entry in the driver
1192 * @driver: the driver for the tty
1193 * @tty: the tty
1194 *
1195 * Install a tty object into the driver tables. The tty->index field
Alan Coxbf970ee2008-10-13 10:42:39 +01001196 * will be set by the time this is called. This method is responsible
1197 * for ensuring any need additional structures are allocated and
1198 * configured.
Alan Cox8b0a88d2008-10-13 10:42:19 +01001199 *
1200 * Locking: tty_mutex for now
1201 */
1202static int tty_driver_install_tty(struct tty_driver *driver,
1203 struct tty_struct *tty)
1204{
Alan Coxbf970ee2008-10-13 10:42:39 +01001205 int idx = tty->index;
Alan Coxeeb89d92009-11-30 13:18:29 +00001206 int ret;
Alan Coxbf970ee2008-10-13 10:42:39 +01001207
Alan Coxeeb89d92009-11-30 13:18:29 +00001208 if (driver->ops->install) {
1209 lock_kernel();
1210 ret = driver->ops->install(driver, tty);
1211 unlock_kernel();
1212 return ret;
1213 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001214
1215 if (tty_init_termios(tty) == 0) {
Alan Coxeeb89d92009-11-30 13:18:29 +00001216 lock_kernel();
Alan Coxbf970ee2008-10-13 10:42:39 +01001217 tty_driver_kref_get(driver);
1218 tty->count++;
1219 driver->ttys[idx] = tty;
Alan Coxeeb89d92009-11-30 13:18:29 +00001220 unlock_kernel();
Alan Coxbf970ee2008-10-13 10:42:39 +01001221 return 0;
1222 }
1223 return -ENOMEM;
Alan Cox8b0a88d2008-10-13 10:42:19 +01001224}
1225
1226/**
1227 * tty_driver_remove_tty() - remove a tty from the driver tables
1228 * @driver: the driver for the tty
1229 * @idx: the minor number
1230 *
1231 * Remvoe a tty object from the driver tables. The tty->index field
1232 * will be set by the time this is called.
1233 *
1234 * Locking: tty_mutex for now
1235 */
1236static void tty_driver_remove_tty(struct tty_driver *driver,
1237 struct tty_struct *tty)
1238{
1239 if (driver->ops->remove)
1240 driver->ops->remove(driver, tty);
1241 else
1242 driver->ttys[tty->index] = NULL;
1243}
1244
1245/*
1246 * tty_reopen() - fast re-open of an open tty
1247 * @tty - the tty to open
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001248 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001249 * Return 0 on success, -errno on error.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001250 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001251 * Locking: tty_mutex must be held from the time the tty was found
1252 * till this open completes.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001253 */
Alan Cox99f1fe12008-10-13 10:42:00 +01001254static int tty_reopen(struct tty_struct *tty)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001255{
1256 struct tty_driver *driver = tty->driver;
1257
1258 if (test_bit(TTY_CLOSING, &tty->flags))
1259 return -EIO;
1260
1261 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1262 driver->subtype == PTY_TYPE_MASTER) {
1263 /*
1264 * special case for PTY masters: only one open permitted,
1265 * and the slave side open count is incremented as well.
1266 */
1267 if (tty->count)
1268 return -EIO;
1269
1270 tty->link->count++;
1271 }
1272 tty->count++;
1273 tty->driver = driver; /* N.B. why do this every time?? */
1274
Alan Cox1aa4bed2009-06-16 17:01:33 +01001275 mutex_lock(&tty->ldisc_mutex);
Alan Cox99f1fe12008-10-13 10:42:00 +01001276 WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
Alan Cox1aa4bed2009-06-16 17:01:33 +01001277 mutex_unlock(&tty->ldisc_mutex);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001278
1279 return 0;
1280}
1281
Alan Coxaf9b8972006-08-27 01:24:01 -07001282/**
Alan Coxd81ed102008-10-13 10:41:42 +01001283 * tty_init_dev - initialise a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001284 * @driver: tty driver we are opening a device on
1285 * @idx: device index
Alan Cox15582d32008-10-13 10:41:03 +01001286 * @ret_tty: returned tty structure
1287 * @first_ok: ok to open a new device (used by ptmx)
Alan Coxaf9b8972006-08-27 01:24:01 -07001288 *
1289 * Prepare a tty device. This may not be a "new" clean device but
1290 * could also be an active device. The pty drivers require special
1291 * handling because of this.
1292 *
1293 * Locking:
1294 * The function is called under the tty_mutex, which
1295 * protects us from the tty struct or driver itself going away.
1296 *
1297 * On exit the tty device has the line discipline attached and
1298 * a reference count of 1. If a pair was created for pty/tty use
1299 * and the other was a pty master then it too has a reference count of 1.
1300 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001302 * failed open. The new code protects the open with a mutex, so it's
1303 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 * relaxed for the (most common) case of reopening a tty.
1305 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001306
Alan Cox73ec06f2008-10-13 10:42:29 +01001307struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1308 int first_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Alan Coxbf970ee2008-10-13 10:42:39 +01001310 struct tty_struct *tty;
Alan Cox73ec06f2008-10-13 10:42:29 +01001311 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Alan Coxeeb89d92009-11-30 13:18:29 +00001313 lock_kernel();
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001314 /* Check if pty master is being opened multiple times */
Alan Cox15582d32008-10-13 10:41:03 +01001315 if (driver->subtype == PTY_TYPE_MASTER &&
Alan Coxeeb89d92009-11-30 13:18:29 +00001316 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
1317 unlock_kernel();
Alan Cox73ec06f2008-10-13 10:42:29 +01001318 return ERR_PTR(-EIO);
Alan Coxeeb89d92009-11-30 13:18:29 +00001319 }
1320 unlock_kernel();
Alan Cox73ec06f2008-10-13 10:42:29 +01001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 /*
1323 * First time open is complex, especially for PTY devices.
1324 * This code guarantees that either everything succeeds and the
1325 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001326 * and the allocated memory released. (Except that the termios
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * and locked termios may be retained.)
1328 */
1329
Alan Cox73ec06f2008-10-13 10:42:29 +01001330 if (!try_module_get(driver->owner))
1331 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 tty = alloc_tty_struct();
Alan Cox37bdfb02008-02-08 04:18:47 -08001334 if (!tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 goto fail_no_mem;
Alan Coxbf970ee2008-10-13 10:42:39 +01001336 initialize_tty_struct(tty, driver, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Alan Cox73ec06f2008-10-13 10:42:29 +01001338 retval = tty_driver_install_tty(driver, tty);
Alan Coxbf970ee2008-10-13 10:42:39 +01001339 if (retval < 0) {
1340 free_tty_struct(tty);
1341 module_put(driver->owner);
1342 return ERR_PTR(retval);
1343 }
Alan Cox8b0a88d2008-10-13 10:42:19 +01001344
Alan Cox37bdfb02008-02-08 04:18:47 -08001345 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001347 * If we fail here just call release_tty to clean up. No need
1348 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
Alan Coxbf970ee2008-10-13 10:42:39 +01001350 retval = tty_ldisc_setup(tty, tty->link);
Alan Cox01e1abb2008-07-22 11:16:55 +01001351 if (retval)
1352 goto release_mem_out;
Alan Cox73ec06f2008-10-13 10:42:29 +01001353 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355fail_no_mem:
1356 module_put(driver->owner);
Alan Cox73ec06f2008-10-13 10:42:29 +01001357 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001359 /* call the tty release_tty routine to clean out this slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360release_mem_out:
Akinobu Mita40509142006-09-29 02:01:27 -07001361 if (printk_ratelimit())
Alan Coxd81ed102008-10-13 10:41:42 +01001362 printk(KERN_INFO "tty_init_dev: ldisc open failed, "
Akinobu Mita40509142006-09-29 02:01:27 -07001363 "clearing slot %d\n", idx);
Alan Coxeeb89d92009-11-30 13:18:29 +00001364 lock_kernel();
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001365 release_tty(tty, idx);
Alan Coxeeb89d92009-11-30 13:18:29 +00001366 unlock_kernel();
Alan Cox73ec06f2008-10-13 10:42:29 +01001367 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Alan Coxfeebed62008-10-13 10:41:30 +01001370void tty_free_termios(struct tty_struct *tty)
1371{
1372 struct ktermios *tp;
1373 int idx = tty->index;
1374 /* Kill this flag and push into drivers for locking etc */
1375 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1376 /* FIXME: Locking on ->termios array */
1377 tp = tty->termios;
1378 tty->driver->termios[idx] = NULL;
1379 kfree(tp);
Alan Coxfeebed62008-10-13 10:41:30 +01001380 }
1381}
1382EXPORT_SYMBOL(tty_free_termios);
1383
1384void tty_shutdown(struct tty_struct *tty)
1385{
Alan Cox8b0a88d2008-10-13 10:42:19 +01001386 tty_driver_remove_tty(tty->driver, tty);
Alan Coxfeebed62008-10-13 10:41:30 +01001387 tty_free_termios(tty);
1388}
1389EXPORT_SYMBOL(tty_shutdown);
1390
Alan Coxaf9b8972006-08-27 01:24:01 -07001391/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001392 * release_one_tty - release tty structure memory
Alan Cox9c9f4de2008-10-13 10:37:26 +01001393 * @kref: kref of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001394 *
1395 * Releases memory associated with a tty structure, and clears out the
1396 * driver table slots. This function is called when a device is no longer
1397 * in use. It also gets called when setup of a device fails.
1398 *
1399 * Locking:
1400 * tty_mutex - sometimes only
1401 * takes the file list lock internally when working on the list
1402 * of ttys that the driver keeps.
Alan Coxb50989d2009-09-19 13:13:22 -07001403 *
1404 * This method gets called from a work queue so that the driver private
Dave Youngf278a2f2009-09-27 16:00:42 +00001405 * cleanup ops can sleep (needed for USB at least)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
Alan Coxb50989d2009-09-19 13:13:22 -07001407static void release_one_tty(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Alan Coxb50989d2009-09-19 13:13:22 -07001409 struct tty_struct *tty =
1410 container_of(work, struct tty_struct, hangup_work);
Alan Cox6f967f72008-10-13 10:37:36 +01001411 struct tty_driver *driver = tty->driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Dave Youngf278a2f2009-09-27 16:00:42 +00001413 if (tty->ops->cleanup)
1414 tty->ops->cleanup(tty);
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 tty->magic = 0;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001417 tty_driver_kref_put(driver);
Alan Cox6f967f72008-10-13 10:37:36 +01001418 module_put(driver->owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 file_list_lock();
1421 list_del_init(&tty->tty_files);
1422 file_list_unlock();
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 free_tty_struct(tty);
1425}
1426
Alan Coxb50989d2009-09-19 13:13:22 -07001427static void queue_release_one_tty(struct kref *kref)
1428{
1429 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Dave Youngf278a2f2009-09-27 16:00:42 +00001430
1431 if (tty->ops->shutdown)
1432 tty->ops->shutdown(tty);
1433 else
1434 tty_shutdown(tty);
1435
Alan Coxb50989d2009-09-19 13:13:22 -07001436 /* The hangup queue is now free so we can reuse it rather than
1437 waste a chunk of memory for each port */
1438 INIT_WORK(&tty->hangup_work, release_one_tty);
1439 schedule_work(&tty->hangup_work);
1440}
1441
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001442/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001443 * tty_kref_put - release a tty kref
1444 * @tty: tty device
1445 *
1446 * Release a reference to a tty device and if need be let the kref
1447 * layer destruct the object for us
1448 */
1449
1450void tty_kref_put(struct tty_struct *tty)
1451{
1452 if (tty)
Alan Coxb50989d2009-09-19 13:13:22 -07001453 kref_put(&tty->kref, queue_release_one_tty);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001454}
1455EXPORT_SYMBOL(tty_kref_put);
1456
1457/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001458 * release_tty - release tty structure memory
1459 *
1460 * Release both @tty and a possible linked partner (think pty pair),
1461 * and decrement the refcount of the backing module.
1462 *
1463 * Locking:
1464 * tty_mutex - sometimes only
1465 * takes the file list lock internally when working on the list
1466 * of ttys that the driver keeps.
1467 * FIXME: should we require tty_mutex is held here ??
Alan Cox9c9f4de2008-10-13 10:37:26 +01001468 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001469 */
1470static void release_tty(struct tty_struct *tty, int idx)
1471{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001472 /* This should always be true but check for the moment */
1473 WARN_ON(tty->index != idx);
1474
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001475 if (tty->link)
Alan Cox9c9f4de2008-10-13 10:37:26 +01001476 tty_kref_put(tty->link);
1477 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001478}
1479
Alan Coxeeb89d92009-11-30 13:18:29 +00001480/**
1481 * tty_release - vfs callback for close
1482 * @inode: inode of tty
1483 * @filp: file pointer for handle to tty
1484 *
1485 * Called the last time each file handle is closed that references
1486 * this tty. There may however be several such references.
1487 *
1488 * Locking:
1489 * Takes bkl. See tty_release_dev
1490 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 * Even releasing the tty structures is a tricky business.. We have
1492 * to be very careful that the structures are all released at the
1493 * same time, as interrupts might otherwise get the wrong pointers.
1494 *
1495 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1496 * lead to double frees or releasing memory still in use.
1497 */
Alan Coxeeb89d92009-11-30 13:18:29 +00001498
1499int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 struct tty_struct *tty, *o_tty;
1502 int pty_master, tty_closing, o_tty_closing, do_sleep;
Paul Fulghum14a62832006-04-10 22:54:19 -07001503 int devpts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 int idx;
1505 char buf[64];
Alan Cox37bdfb02008-02-08 04:18:47 -08001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 tty = (struct tty_struct *)filp->private_data;
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001508 if (tty_paranoia_check(tty, inode, "tty_release_dev"))
Alan Coxeeb89d92009-11-30 13:18:29 +00001509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Alan Coxeeb89d92009-11-30 13:18:29 +00001511 lock_kernel();
Alan Coxd81ed102008-10-13 10:41:42 +01001512 check_tty_count(tty, "tty_release_dev");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 tty_fasync(-1, filp, 0);
1515
1516 idx = tty->index;
1517 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1518 tty->driver->subtype == PTY_TYPE_MASTER);
1519 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 o_tty = tty->link;
1521
1522#ifdef TTY_PARANOIA_CHECK
1523 if (idx < 0 || idx >= tty->driver->num) {
Alan Coxd81ed102008-10-13 10:41:42 +01001524 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 "free (%s)\n", tty->name);
Alan Coxeeb89d92009-11-30 13:18:29 +00001526 unlock_kernel();
1527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
Alan Cox8b0a88d2008-10-13 10:42:19 +01001529 if (!devpts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (tty != tty->driver->ttys[idx]) {
Alan Coxeeb89d92009-11-30 13:18:29 +00001531 unlock_kernel();
Alan Coxd81ed102008-10-13 10:41:42 +01001532 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 "for (%s)\n", idx, tty->name);
Alan Coxeeb89d92009-11-30 13:18:29 +00001534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536 if (tty->termios != tty->driver->termios[idx]) {
Alan Coxeeb89d92009-11-30 13:18:29 +00001537 unlock_kernel();
Alan Coxd81ed102008-10-13 10:41:42 +01001538 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 "for (%s)\n",
1540 idx, tty->name);
Alan Coxeeb89d92009-11-30 13:18:29 +00001541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544#endif
1545
1546#ifdef TTY_DEBUG_HANGUP
Alan Coxd81ed102008-10-13 10:41:42 +01001547 printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 tty_name(tty, buf), tty->count);
1549#endif
1550
1551#ifdef TTY_PARANOIA_CHECK
1552 if (tty->driver->other &&
1553 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1554 if (o_tty != tty->driver->other->ttys[idx]) {
Alan Coxeeb89d92009-11-30 13:18:29 +00001555 unlock_kernel();
Alan Coxd81ed102008-10-13 10:41:42 +01001556 printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 "not o_tty for (%s)\n",
1558 idx, tty->name);
Alan Coxeeb89d92009-11-30 13:18:29 +00001559 return 0 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561 if (o_tty->termios != tty->driver->other->termios[idx]) {
Alan Coxeeb89d92009-11-30 13:18:29 +00001562 unlock_kernel();
Alan Coxd81ed102008-10-13 10:41:42 +01001563 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 "not o_termios for (%s)\n",
1565 idx, tty->name);
Alan Coxeeb89d92009-11-30 13:18:29 +00001566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (o_tty->link != tty) {
Alan Coxeeb89d92009-11-30 13:18:29 +00001569 unlock_kernel();
Alan Coxd81ed102008-10-13 10:41:42 +01001570 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
Alan Coxeeb89d92009-11-30 13:18:29 +00001571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573 }
1574#endif
Alan Coxf34d7a52008-04-30 00:54:13 -07001575 if (tty->ops->close)
1576 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Alan Coxeeb89d92009-11-30 13:18:29 +00001578 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /*
1580 * Sanity check: if tty->count is going to zero, there shouldn't be
1581 * any waiters on tty->read_wait or tty->write_wait. We test the
1582 * wait queues and kick everyone out _before_ actually starting to
1583 * close. This ensures that we won't block while releasing the tty
1584 * structure.
1585 *
1586 * The test for the o_tty closing is necessary, since the master and
1587 * slave sides may close in any order. If the slave side closes out
1588 * first, its count will be one, since the master side holds an open.
1589 * Thus this test wouldn't be triggered at the time the slave closes,
1590 * so we do it now.
1591 *
1592 * Note that it's possible for the tty to be opened again while we're
1593 * flushing out waiters. By recalculating the closing flags before
1594 * each iteration we avoid any problems.
1595 */
1596 while (1) {
1597 /* Guard against races with tty->count changes elsewhere and
1598 opens on /dev/tty */
Alan Cox37bdfb02008-02-08 04:18:47 -08001599
Ingo Molnar70522e12006-03-23 03:00:31 -08001600 mutex_lock(&tty_mutex);
Alan Coxeeb89d92009-11-30 13:18:29 +00001601 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 tty_closing = tty->count <= 1;
1603 o_tty_closing = o_tty &&
1604 (o_tty->count <= (pty_master ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 do_sleep = 0;
1606
1607 if (tty_closing) {
1608 if (waitqueue_active(&tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001609 wake_up_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 do_sleep++;
1611 }
1612 if (waitqueue_active(&tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001613 wake_up_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 do_sleep++;
1615 }
1616 }
1617 if (o_tty_closing) {
1618 if (waitqueue_active(&o_tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001619 wake_up_poll(&o_tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 do_sleep++;
1621 }
1622 if (waitqueue_active(&o_tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001623 wake_up_poll(&o_tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 do_sleep++;
1625 }
1626 }
1627 if (!do_sleep)
1628 break;
1629
Alan Coxd81ed102008-10-13 10:41:42 +01001630 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 "active!\n", tty_name(tty, buf));
Alan Coxeeb89d92009-11-30 13:18:29 +00001632 unlock_kernel();
Ingo Molnar70522e12006-03-23 03:00:31 -08001633 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 schedule();
Alan Cox37bdfb02008-02-08 04:18:47 -08001635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08001638 * The closing flags are now consistent with the open counts on
1639 * both sides, and we've completed the last operation that could
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 * block, so it's safe to proceed with closing.
1641 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (pty_master) {
1643 if (--o_tty->count < 0) {
Alan Coxd81ed102008-10-13 10:41:42 +01001644 printk(KERN_WARNING "tty_release_dev: bad pty slave count "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 "(%d) for %s\n",
1646 o_tty->count, tty_name(o_tty, buf));
1647 o_tty->count = 0;
1648 }
1649 }
1650 if (--tty->count < 0) {
Alan Coxd81ed102008-10-13 10:41:42 +01001651 printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 tty->count, tty_name(tty, buf));
1653 tty->count = 0;
1654 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /*
1657 * We've decremented tty->count, so we need to remove this file
1658 * descriptor off the tty->tty_files list; this serves two
1659 * purposes:
1660 * - check_tty_count sees the correct number of file descriptors
1661 * associated with this tty.
1662 * - do_tty_hangup no longer sees this file descriptor as
1663 * something that needs to be handled for hangups.
1664 */
1665 file_kill(filp);
1666 filp->private_data = NULL;
1667
1668 /*
1669 * Perform some housekeeping before deciding whether to return.
1670 *
1671 * Set the TTY_CLOSING flag if this was the last open. In the
1672 * case of a pty we may have to wait around for the other side
1673 * to close, and TTY_CLOSING makes sure we can't be reopened.
1674 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001675 if (tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 set_bit(TTY_CLOSING, &tty->flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08001677 if (o_tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 set_bit(TTY_CLOSING, &o_tty->flags);
1679
1680 /*
1681 * If _either_ side is closing, make sure there aren't any
1682 * processes that still think tty or o_tty is their controlling
1683 * tty.
1684 */
1685 if (tty_closing || o_tty_closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001687 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001689 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 read_unlock(&tasklist_lock);
1691 }
1692
Ingo Molnar70522e12006-03-23 03:00:31 -08001693 mutex_unlock(&tty_mutex);
Paul Fulghumda965822006-02-14 13:53:00 -08001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /* check whether both sides are closing ... */
Alan Coxeeb89d92009-11-30 13:18:29 +00001696 if (!tty_closing || (o_tty && !o_tty_closing)) {
1697 unlock_kernel();
1698 return 0;
1699 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701#ifdef TTY_DEBUG_HANGUP
1702 printk(KERN_DEBUG "freeing tty structure...");
1703#endif
1704 /*
Alan Cox01e1abb2008-07-22 11:16:55 +01001705 * Ask the line discipline code to release its structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
Alan Cox01e1abb2008-07-22 11:16:55 +01001707 tty_ldisc_release(tty, o_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 /*
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001709 * The release_tty function takes care of the details of clearing
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 * the slots and preserving the termios structure.
1711 */
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001712 release_tty(tty, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /* Make this pty number available for reallocation */
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -07001715 if (devpts)
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001716 devpts_kill_index(inode, idx);
Alan Coxeeb89d92009-11-30 13:18:29 +00001717 unlock_kernel();
1718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Alan Coxaf9b8972006-08-27 01:24:01 -07001721/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001722 * tty_open - open a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001723 * @inode: inode of device file
1724 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001726 * tty_open and tty_release keep up the tty count that contains the
1727 * number of opens done on a tty. We cannot use the inode-count, as
1728 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001730 * Open-counting is needed for pty masters, as well as for keeping
1731 * track of serial lines: DTR is dropped when the last close happens.
1732 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1733 *
1734 * The termios state of a pty is reset on first open so that
1735 * settings don't persist across reuse.
1736 *
Alan Coxd81ed102008-10-13 10:41:42 +01001737 * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001738 * tty->count should protect the rest.
1739 * ->siglock protects ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001741
Alan Coxeeb89d92009-11-30 13:18:29 +00001742static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001744 struct tty_struct *tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 int noctty, retval;
1746 struct tty_driver *driver;
1747 int index;
1748 dev_t device = inode->i_rdev;
Andrew Morton846c1512009-04-02 16:56:36 -07001749 unsigned saved_flags = filp->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753retry_open:
1754 noctty = filp->f_flags & O_NOCTTY;
1755 index = -1;
1756 retval = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001757
Ingo Molnar70522e12006-03-23 03:00:31 -08001758 mutex_lock(&tty_mutex);
Alan Coxeeb89d92009-11-30 13:18:29 +00001759 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Alan Cox37bdfb02008-02-08 04:18:47 -08001761 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001762 tty = get_current_tty();
1763 if (!tty) {
Alan Coxeeb89d92009-11-30 13:18:29 +00001764 unlock_kernel();
Ingo Molnar70522e12006-03-23 03:00:31 -08001765 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return -ENXIO;
1767 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01001768 driver = tty_driver_kref_get(tty->driver);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001769 index = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1771 /* noctty = 1; */
Alan Cox452a00d2008-10-13 10:39:13 +01001772 /* FIXME: Should we take a driver reference ? */
1773 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 goto got_driver;
1775 }
1776#ifdef CONFIG_VT
Alan Cox37bdfb02008-02-08 04:18:47 -08001777 if (device == MKDEV(TTY_MAJOR, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 extern struct tty_driver *console_driver;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001779 driver = tty_driver_kref_get(console_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 index = fg_console;
1781 noctty = 1;
1782 goto got_driver;
1783 }
1784#endif
Alan Cox37bdfb02008-02-08 04:18:47 -08001785 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
Will Newton296fa7f2008-12-01 11:36:06 +00001786 struct tty_driver *console_driver = console_device(&index);
1787 if (console_driver) {
1788 driver = tty_driver_kref_get(console_driver);
1789 if (driver) {
1790 /* Don't let /dev/console block */
1791 filp->f_flags |= O_NONBLOCK;
1792 noctty = 1;
1793 goto got_driver;
1794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
Alan Coxeeb89d92009-11-30 13:18:29 +00001796 unlock_kernel();
Ingo Molnar70522e12006-03-23 03:00:31 -08001797 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return -ENODEV;
1799 }
1800
1801 driver = get_tty_driver(device, &index);
1802 if (!driver) {
Alan Coxeeb89d92009-11-30 13:18:29 +00001803 unlock_kernel();
Ingo Molnar70522e12006-03-23 03:00:31 -08001804 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return -ENODEV;
1806 }
1807got_driver:
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001808 if (!tty) {
1809 /* check whether we're reopening an existing tty */
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001810 tty = tty_driver_lookup_tty(driver, inode, index);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001811
Eric Paris808ffa32009-01-27 11:50:37 +00001812 if (IS_ERR(tty)) {
Alan Coxeeb89d92009-11-30 13:18:29 +00001813 unlock_kernel();
Eric Paris808ffa32009-01-27 11:50:37 +00001814 mutex_unlock(&tty_mutex);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001815 return PTR_ERR(tty);
Eric Paris808ffa32009-01-27 11:50:37 +00001816 }
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01001817 }
1818
1819 if (tty) {
1820 retval = tty_reopen(tty);
1821 if (retval)
1822 tty = ERR_PTR(retval);
1823 } else
1824 tty = tty_init_dev(driver, index, 0);
1825
Ingo Molnar70522e12006-03-23 03:00:31 -08001826 mutex_unlock(&tty_mutex);
Alan Cox7d7b93c2008-10-13 10:42:09 +01001827 tty_driver_kref_put(driver);
Alan Coxeeb89d92009-11-30 13:18:29 +00001828 if (IS_ERR(tty)) {
1829 unlock_kernel();
Alan Cox73ec06f2008-10-13 10:42:29 +01001830 return PTR_ERR(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 filp->private_data = tty;
1834 file_move(filp, &tty->tty_files);
1835 check_tty_count(tty, "tty_open");
1836 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1837 tty->driver->subtype == PTY_TYPE_MASTER)
1838 noctty = 1;
1839#ifdef TTY_DEBUG_HANGUP
1840 printk(KERN_DEBUG "opening %s...", tty->name);
1841#endif
1842 if (!retval) {
Alan Coxf34d7a52008-04-30 00:54:13 -07001843 if (tty->ops->open)
1844 retval = tty->ops->open(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 else
1846 retval = -ENODEV;
1847 }
1848 filp->f_flags = saved_flags;
1849
Alan Cox37bdfb02008-02-08 04:18:47 -08001850 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1851 !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 retval = -EBUSY;
1853
1854 if (retval) {
1855#ifdef TTY_DEBUG_HANGUP
1856 printk(KERN_DEBUG "error %d in opening %s...", retval,
1857 tty->name);
1858#endif
Alan Coxeeb89d92009-11-30 13:18:29 +00001859 tty_release(inode, filp);
1860 if (retval != -ERESTARTSYS) {
1861 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return retval;
Alan Coxeeb89d92009-11-30 13:18:29 +00001863 }
1864 if (signal_pending(current)) {
1865 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return retval;
Alan Coxeeb89d92009-11-30 13:18:29 +00001867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 schedule();
1869 /*
1870 * Need to reset f_op in case a hangup happened.
1871 */
1872 if (filp->f_op == &hung_up_tty_fops)
1873 filp->f_op = &tty_fops;
1874 goto retry_open;
1875 }
Alan Coxeeb89d92009-11-30 13:18:29 +00001876 unlock_kernel();
1877
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001878
1879 mutex_lock(&tty_mutex);
Alan Coxeeb89d92009-11-30 13:18:29 +00001880 lock_kernel();
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001881 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (!noctty &&
1883 current->signal->leader &&
1884 !current->signal->tty &&
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001885 tty->session == NULL)
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07001886 __proc_set_tty(current, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001887 spin_unlock_irq(&current->sighand->siglock);
Alan Coxeeb89d92009-11-30 13:18:29 +00001888 unlock_kernel();
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001889 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return 0;
1891}
1892
Jonathan Corbet39d95b92008-05-16 09:10:50 -06001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Alan Coxaf9b8972006-08-27 01:24:01 -07001895/**
1896 * tty_poll - check tty status
1897 * @filp: file being polled
1898 * @wait: poll wait structures to update
1899 *
1900 * Call the line discipline polling method to obtain the poll
1901 * status of the device.
1902 *
1903 * Locking: locks called line discipline but ldisc poll method
1904 * may be re-entered freely by other callers.
1905 */
1906
Alan Cox37bdfb02008-02-08 04:18:47 -08001907static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Alan Cox37bdfb02008-02-08 04:18:47 -08001909 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 struct tty_ldisc *ld;
1911 int ret = 0;
1912
1913 tty = (struct tty_struct *)filp->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08001914 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001918 if (ld->ops->poll)
1919 ret = (ld->ops->poll)(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 tty_ldisc_deref(ld);
1921 return ret;
1922}
1923
Alan Cox37bdfb02008-02-08 04:18:47 -08001924static int tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Alan Cox37bdfb02008-02-08 04:18:47 -08001926 struct tty_struct *tty;
Alan Cox47f86832008-04-30 00:53:30 -07001927 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001928 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001930 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 tty = (struct tty_struct *)filp->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08001932 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001933 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 retval = fasync_helper(fd, filp, on, &tty->fasync);
1936 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001937 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001940 enum pid_type type;
1941 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (!waitqueue_active(&tty->read_wait))
1943 tty->minimum_to_wake = 1;
Alan Cox47f86832008-04-30 00:53:30 -07001944 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001945 if (tty->pgrp) {
1946 pid = tty->pgrp;
1947 type = PIDTYPE_PGID;
1948 } else {
1949 pid = task_pid(current);
1950 type = PIDTYPE_PID;
1951 }
Alan Cox47f86832008-04-30 00:53:30 -07001952 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001953 retval = __f_setown(filp, pid, type, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (retval)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001955 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 } else {
1957 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1958 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1959 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06001960 retval = 0;
1961out:
1962 unlock_kernel();
1963 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Alan Coxaf9b8972006-08-27 01:24:01 -07001966/**
1967 * tiocsti - fake input character
1968 * @tty: tty to fake input into
1969 * @p: pointer to character
1970 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001971 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07001972 * input management.
1973 *
1974 * FIXME: does not honour flow control ??
1975 *
1976 * Locking:
1977 * Called functions take tty_ldisc_lock
1978 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07001979 *
1980 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07001981 */
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983static int tiocsti(struct tty_struct *tty, char __user *p)
1984{
1985 char ch, mbz = 0;
1986 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
1989 return -EPERM;
1990 if (get_user(ch, p))
1991 return -EFAULT;
Al Viro1e641742008-12-09 09:23:33 +00001992 tty_audit_tiocsti(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001994 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 tty_ldisc_deref(ld);
1996 return 0;
1997}
1998
Alan Coxaf9b8972006-08-27 01:24:01 -07001999/**
2000 * tiocgwinsz - implement window query ioctl
2001 * @tty; tty
2002 * @arg: user buffer for result
2003 *
Alan Cox808a0d32006-09-29 02:00:40 -07002004 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002005 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002006 * Locking: tty->termios_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002007 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002008 */
2009
Alan Cox37bdfb02008-02-08 04:18:47 -08002010static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Alan Cox808a0d32006-09-29 02:00:40 -07002012 int err;
2013
Arjan van de Ven5785c952006-09-29 02:00:43 -07002014 mutex_lock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002015 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Arjan van de Ven5785c952006-09-29 02:00:43 -07002016 mutex_unlock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002017
2018 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Alan Coxaf9b8972006-08-27 01:24:01 -07002021/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002022 * tty_do_resize - resize event
2023 * @tty: tty being resized
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002024 * @rows: rows (character)
2025 * @cols: cols (character)
Alan Coxaf9b8972006-08-27 01:24:01 -07002026 *
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002027 * Update the termios variables and send the neccessary signals to
2028 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002029 */
2030
Alan Coxfc6f6232009-01-02 13:43:17 +00002031int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Alan Coxfc6f6232009-01-02 13:43:17 +00002033 struct pid *pgrp;
Alan Cox47f86832008-04-30 00:53:30 -07002034 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Alan Coxfc6f6232009-01-02 13:43:17 +00002036 /* Lock the tty */
2037 mutex_lock(&tty->termios_mutex);
2038 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002039 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002040 /* Get the PID values and reference them so we can
2041 avoid holding the tty ctrl lock while sending signals */
2042 spin_lock_irqsave(&tty->ctrl_lock, flags);
2043 pgrp = get_pid(tty->pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002044 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2045
2046 if (pgrp)
2047 kill_pgrp(pgrp, SIGWINCH, 1);
Alan Cox47f86832008-04-30 00:53:30 -07002048 put_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002049
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002050 tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002051done:
Alan Coxfc6f6232009-01-02 13:43:17 +00002052 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return 0;
2054}
2055
Alan Coxaf9b8972006-08-27 01:24:01 -07002056/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002057 * tiocswinsz - implement window size set ioctl
Alan Coxfc6f6232009-01-02 13:43:17 +00002058 * @tty; tty side of tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002059 * @arg: user buffer for result
2060 *
2061 * Copies the user idea of the window size to the kernel. Traditionally
2062 * this is just advisory information but for the Linux console it
2063 * actually has driver level meaning and triggers a VC resize.
2064 *
2065 * Locking:
2066 * Driver dependant. The default do_resize method takes the
2067 * tty termios mutex and ctrl_lock. The console takes its own lock
2068 * then calls into the default method.
2069 */
2070
Alan Coxfc6f6232009-01-02 13:43:17 +00002071static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002072{
2073 struct winsize tmp_ws;
2074 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2075 return -EFAULT;
2076
2077 if (tty->ops->resize)
Alan Coxfc6f6232009-01-02 13:43:17 +00002078 return tty->ops->resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002079 else
Alan Coxfc6f6232009-01-02 13:43:17 +00002080 return tty_do_resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002081}
2082
2083/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002084 * tioccons - allow admin to move logical console
2085 * @file: the file to become console
2086 *
2087 * Allow the adminstrator to move the redirected console device
2088 *
2089 * Locking: uses redirect_lock to guard the redirect information
2090 */
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092static int tioccons(struct file *file)
2093{
2094 if (!capable(CAP_SYS_ADMIN))
2095 return -EPERM;
2096 if (file->f_op->write == redirected_tty_write) {
2097 struct file *f;
2098 spin_lock(&redirect_lock);
2099 f = redirect;
2100 redirect = NULL;
2101 spin_unlock(&redirect_lock);
2102 if (f)
2103 fput(f);
2104 return 0;
2105 }
2106 spin_lock(&redirect_lock);
2107 if (redirect) {
2108 spin_unlock(&redirect_lock);
2109 return -EBUSY;
2110 }
2111 get_file(file);
2112 redirect = file;
2113 spin_unlock(&redirect_lock);
2114 return 0;
2115}
2116
Alan Coxaf9b8972006-08-27 01:24:01 -07002117/**
2118 * fionbio - non blocking ioctl
2119 * @file: file to set blocking value
2120 * @p: user parameter
2121 *
2122 * Historical tty interfaces had a blocking control ioctl before
2123 * the generic functionality existed. This piece of history is preserved
2124 * in the expected tty API of posix OS's.
2125 *
Alan Cox6146b9a2009-09-19 13:13:19 -07002126 * Locking: none, the open file handle ensures it won't go away.
Alan Coxaf9b8972006-08-27 01:24:01 -07002127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129static int fionbio(struct file *file, int __user *p)
2130{
2131 int nonblock;
2132
2133 if (get_user(nonblock, p))
2134 return -EFAULT;
2135
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002136 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (nonblock)
2138 file->f_flags |= O_NONBLOCK;
2139 else
2140 file->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002141 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return 0;
2143}
2144
Alan Coxaf9b8972006-08-27 01:24:01 -07002145/**
2146 * tiocsctty - set controlling tty
2147 * @tty: tty structure
2148 * @arg: user argument
2149 *
2150 * This ioctl is used to manage job control. It permits a session
2151 * leader to set this tty as the controlling tty for the session.
2152 *
2153 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002154 * Takes tty_mutex() to protect tty instance
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002155 * Takes tasklist_lock internally to walk sessions
2156 * Takes ->siglock() when updating signal->tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002157 */
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159static int tiocsctty(struct tty_struct *tty, int arg)
2160{
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002161 int ret = 0;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002162 if (current->signal->leader && (task_session(current) == tty->session))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002163 return ret;
2164
2165 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /*
2167 * The process must be a session leader and
2168 * not have a controlling tty already.
2169 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002170 if (!current->signal->leader || current->signal->tty) {
2171 ret = -EPERM;
2172 goto unlock;
2173 }
2174
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002175 if (tty->session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /*
2177 * This tty is already the controlling
2178 * tty for another session group!
2179 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002180 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /*
2182 * Steal it away
2183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002185 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 read_unlock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002187 } else {
2188 ret = -EPERM;
2189 goto unlock;
2190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002192 proc_set_tty(current, tty);
2193unlock:
Alan Cox28298232006-09-29 02:00:58 -07002194 mutex_unlock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002195 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Alan Coxaf9b8972006-08-27 01:24:01 -07002198/**
Alan Cox5d0fdf12008-04-30 00:53:31 -07002199 * tty_get_pgrp - return a ref counted pgrp pid
2200 * @tty: tty to read
2201 *
2202 * Returns a refcounted instance of the pid struct for the process
2203 * group controlling the tty.
2204 */
2205
2206struct pid *tty_get_pgrp(struct tty_struct *tty)
2207{
2208 unsigned long flags;
2209 struct pid *pgrp;
2210
2211 spin_lock_irqsave(&tty->ctrl_lock, flags);
2212 pgrp = get_pid(tty->pgrp);
2213 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2214
2215 return pgrp;
2216}
2217EXPORT_SYMBOL_GPL(tty_get_pgrp);
2218
2219/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002220 * tiocgpgrp - get process group
2221 * @tty: tty passed by user
2222 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2223 * @p: returned pid
2224 *
2225 * Obtain the process group of the tty. If there is no process group
2226 * return an error.
2227 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002228 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002229 */
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2232{
Alan Cox5d0fdf12008-04-30 00:53:31 -07002233 struct pid *pid;
2234 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /*
2236 * (tty == real_tty) is a cheap way of
2237 * testing if the tty is NOT a master pty.
2238 */
2239 if (tty == real_tty && current->signal->tty != real_tty)
2240 return -ENOTTY;
Alan Cox5d0fdf12008-04-30 00:53:31 -07002241 pid = tty_get_pgrp(real_tty);
2242 ret = put_user(pid_vnr(pid), p);
2243 put_pid(pid);
2244 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Alan Coxaf9b8972006-08-27 01:24:01 -07002247/**
2248 * tiocspgrp - attempt to set process group
2249 * @tty: tty passed by user
2250 * @real_tty: tty side device matching tty passed by user
2251 * @p: pid pointer
2252 *
2253 * Set the process group of the tty to the session passed. Only
2254 * permitted where the tty session is our session.
2255 *
Alan Cox47f86832008-04-30 00:53:30 -07002256 * Locking: RCU, ctrl lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002257 */
2258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2260{
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002261 struct pid *pgrp;
2262 pid_t pgrp_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 int retval = tty_check_change(real_tty);
Alan Cox47f86832008-04-30 00:53:30 -07002264 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 if (retval == -EIO)
2267 return -ENOTTY;
2268 if (retval)
2269 return retval;
2270 if (!current->signal->tty ||
2271 (current->signal->tty != real_tty) ||
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002272 (real_tty->session != task_session(current)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return -ENOTTY;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002274 if (get_user(pgrp_nr, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return -EFAULT;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002276 if (pgrp_nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return -EINVAL;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002278 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002279 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002280 retval = -ESRCH;
2281 if (!pgrp)
2282 goto out_unlock;
2283 retval = -EPERM;
2284 if (session_of_pgrp(pgrp) != task_session(current))
2285 goto out_unlock;
2286 retval = 0;
Alan Cox47f86832008-04-30 00:53:30 -07002287 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002288 put_pid(real_tty->pgrp);
2289 real_tty->pgrp = get_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002290 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002291out_unlock:
2292 rcu_read_unlock();
2293 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Alan Coxaf9b8972006-08-27 01:24:01 -07002296/**
2297 * tiocgsid - get session id
2298 * @tty: tty passed by user
2299 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2300 * @p: pointer to returned session id
2301 *
2302 * Obtain the session id of the tty. If there is no session
2303 * return an error.
2304 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002305 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002306 */
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2309{
2310 /*
2311 * (tty == real_tty) is a cheap way of
2312 * testing if the tty is NOT a master pty.
2313 */
2314 if (tty == real_tty && current->signal->tty != real_tty)
2315 return -ENOTTY;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002316 if (!real_tty->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return -ENOTTY;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002318 return put_user(pid_vnr(real_tty->session), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
Alan Coxaf9b8972006-08-27 01:24:01 -07002321/**
2322 * tiocsetd - set line discipline
2323 * @tty: tty device
2324 * @p: pointer to user data
2325 *
2326 * Set the line discipline according to user request.
2327 *
2328 * Locking: see tty_set_ldisc, this function is just a helper
2329 */
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331static int tiocsetd(struct tty_struct *tty, int __user *p)
2332{
2333 int ldisc;
Alan Cox04f378b2008-04-30 00:53:29 -07002334 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 if (get_user(ldisc, p))
2337 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002338
Alan Cox04f378b2008-04-30 00:53:29 -07002339 ret = tty_set_ldisc(tty, ldisc);
Alan Cox04f378b2008-04-30 00:53:29 -07002340
2341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Alan Coxaf9b8972006-08-27 01:24:01 -07002344/**
2345 * send_break - performed time break
2346 * @tty: device to break on
2347 * @duration: timeout in mS
2348 *
2349 * Perform a timed break on hardware that lacks its own driver level
2350 * timed break functionality.
2351 *
2352 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002353 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002354 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002355 */
2356
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002357static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
Alan Cox9e989662008-07-22 11:18:03 +01002359 int retval;
2360
2361 if (tty->ops->break_ctl == NULL)
2362 return 0;
2363
2364 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2365 retval = tty->ops->break_ctl(tty, duration);
2366 else {
2367 /* Do the work ourselves */
2368 if (tty_write_lock(tty, 0) < 0)
2369 return -EINTR;
2370 retval = tty->ops->break_ctl(tty, -1);
2371 if (retval)
2372 goto out;
2373 if (!signal_pending(current))
2374 msleep_interruptible(duration);
2375 retval = tty->ops->break_ctl(tty, 0);
2376out:
2377 tty_write_unlock(tty);
2378 if (signal_pending(current))
2379 retval = -EINTR;
2380 }
2381 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Alan Coxaf9b8972006-08-27 01:24:01 -07002384/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002385 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002386 * @tty: tty device
2387 * @file: user file pointer
2388 * @p: pointer to result
2389 *
2390 * Obtain the modem status bits from the tty driver if the feature
2391 * is supported. Return -EINVAL if it is not available.
2392 *
2393 * Locking: none (up to the driver)
2394 */
2395
2396static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
2398 int retval = -EINVAL;
2399
Alan Coxf34d7a52008-04-30 00:54:13 -07002400 if (tty->ops->tiocmget) {
2401 retval = tty->ops->tiocmget(tty, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 if (retval >= 0)
2404 retval = put_user(retval, p);
2405 }
2406 return retval;
2407}
2408
Alan Coxaf9b8972006-08-27 01:24:01 -07002409/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002410 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002411 * @tty: tty device
2412 * @file: user file pointer
2413 * @cmd: command - clear bits, set bits or set all
2414 * @p: pointer to desired bits
2415 *
2416 * Set the modem status bits from the tty driver if the feature
2417 * is supported. Return -EINVAL if it is not available.
2418 *
2419 * Locking: none (up to the driver)
2420 */
2421
2422static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 unsigned __user *p)
2424{
Alan Coxae677512008-07-16 21:56:54 +01002425 int retval;
2426 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Alan Coxae677512008-07-16 21:56:54 +01002428 if (tty->ops->tiocmset == NULL)
2429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Alan Coxae677512008-07-16 21:56:54 +01002431 retval = get_user(val, p);
2432 if (retval)
2433 return retval;
2434 set = clear = 0;
2435 switch (cmd) {
2436 case TIOCMBIS:
2437 set = val;
2438 break;
2439 case TIOCMBIC:
2440 clear = val;
2441 break;
2442 case TIOCMSET:
2443 set = val;
2444 clear = ~val;
2445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
Alan Coxae677512008-07-16 21:56:54 +01002447 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2448 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2449 return tty->ops->tiocmset(tty, file, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
Alan Coxe8b70e72009-06-11 12:48:02 +01002452struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2453{
2454 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2455 tty->driver->subtype == PTY_TYPE_MASTER)
2456 tty = tty->link;
2457 return tty;
2458}
2459EXPORT_SYMBOL(tty_pair_get_tty);
2460
2461struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
2462{
2463 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2464 tty->driver->subtype == PTY_TYPE_MASTER)
2465 return tty;
2466 return tty->link;
2467}
2468EXPORT_SYMBOL(tty_pair_get_pty);
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470/*
2471 * Split this up, as gcc can choke on it otherwise..
2472 */
Alan Cox04f378b2008-04-30 00:53:29 -07002473long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
2475 struct tty_struct *tty, *real_tty;
2476 void __user *p = (void __user *)arg;
2477 int retval;
2478 struct tty_ldisc *ld;
Alan Cox04f378b2008-04-30 00:53:29 -07002479 struct inode *inode = file->f_dentry->d_inode;
Alan Cox37bdfb02008-02-08 04:18:47 -08002480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 tty = (struct tty_struct *)file->private_data;
2482 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2483 return -EINVAL;
2484
Alan Coxe8b70e72009-06-11 12:48:02 +01002485 real_tty = tty_pair_get_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 /*
2488 * Factor out some common prep work
2489 */
2490 switch (cmd) {
2491 case TIOCSETD:
2492 case TIOCSBRK:
2493 case TIOCCBRK:
2494 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002495 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 retval = tty_check_change(tty);
2497 if (retval)
2498 return retval;
2499 if (cmd != TIOCCBRK) {
2500 tty_wait_until_sent(tty, 0);
2501 if (signal_pending(current))
2502 return -EINTR;
2503 }
2504 break;
2505 }
2506
Alan Cox9e989662008-07-22 11:18:03 +01002507 /*
2508 * Now do the stuff.
2509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002511 case TIOCSTI:
2512 return tiocsti(tty, p);
2513 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002514 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002515 case TIOCSWINSZ:
Alan Coxfc6f6232009-01-02 13:43:17 +00002516 return tiocswinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002517 case TIOCCONS:
2518 return real_tty != tty ? -EINVAL : tioccons(file);
2519 case FIONBIO:
2520 return fionbio(file, p);
2521 case TIOCEXCL:
2522 set_bit(TTY_EXCLUSIVE, &tty->flags);
2523 return 0;
2524 case TIOCNXCL:
2525 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2526 return 0;
2527 case TIOCNOTTY:
2528 if (current->signal->tty != tty)
2529 return -ENOTTY;
2530 no_tty();
2531 return 0;
2532 case TIOCSCTTY:
2533 return tiocsctty(tty, arg);
2534 case TIOCGPGRP:
2535 return tiocgpgrp(tty, real_tty, p);
2536 case TIOCSPGRP:
2537 return tiocspgrp(tty, real_tty, p);
2538 case TIOCGSID:
2539 return tiocgsid(tty, real_tty, p);
2540 case TIOCGETD:
Alan Coxc65c9bc2009-06-11 12:50:12 +01002541 return put_user(tty->ldisc->ops->num, (int __user *)p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002542 case TIOCSETD:
2543 return tiocsetd(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002544 /*
2545 * Break handling
2546 */
2547 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002548 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002549 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002550 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002551 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002552 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002553 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002554 return 0;
2555 case TCSBRK: /* SVID version: non-zero arg --> no break */
2556 /* non-zero arg means wait for all output data
2557 * to be sent (performed above) but don't send break.
2558 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002560 if (!arg)
2561 return send_break(tty, 250);
2562 return 0;
2563 case TCSBRKP: /* support for POSIX tcsendbreak() */
2564 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Alan Cox37bdfb02008-02-08 04:18:47 -08002566 case TIOCMGET:
2567 return tty_tiocmget(tty, file, p);
2568 case TIOCMSET:
2569 case TIOCMBIC:
2570 case TIOCMBIS:
2571 return tty_tiocmset(tty, file, cmd, p);
2572 case TCFLSH:
2573 switch (arg) {
2574 case TCIFLUSH:
2575 case TCIOFLUSH:
2576 /* flush tty buffer and allow ldisc to process ioctl */
2577 tty_buffer_flush(tty);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002578 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002579 }
2580 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002582 if (tty->ops->ioctl) {
2583 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (retval != -ENOIOCTLCMD)
2585 return retval;
2586 }
2587 ld = tty_ldisc_ref_wait(tty);
2588 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002589 if (ld->ops->ioctl) {
2590 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (retval == -ENOIOCTLCMD)
2592 retval = -EINVAL;
2593 }
2594 tty_ldisc_deref(ld);
2595 return retval;
2596}
2597
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002598#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08002599static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002600 unsigned long arg)
2601{
2602 struct inode *inode = file->f_dentry->d_inode;
2603 struct tty_struct *tty = file->private_data;
2604 struct tty_ldisc *ld;
2605 int retval = -ENOIOCTLCMD;
2606
2607 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2608 return -EINVAL;
2609
Alan Coxf34d7a52008-04-30 00:54:13 -07002610 if (tty->ops->compat_ioctl) {
2611 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002612 if (retval != -ENOIOCTLCMD)
2613 return retval;
2614 }
2615
2616 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002617 if (ld->ops->compat_ioctl)
2618 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002619 tty_ldisc_deref(ld);
2620
2621 return retval;
2622}
2623#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625/*
2626 * This implements the "Secure Attention Key" --- the idea is to
2627 * prevent trojan horses by killing all processes associated with this
2628 * tty when the user hits the "Secure Attention Key". Required for
2629 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08002630 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 * This code could be nicer; ideally it should send a HUP, wait a few
2632 * seconds, then send a INT, and then a KILL signal. But you then
2633 * have to coordinate with the init process, since all processes associated
2634 * with the current tty must be dead before the new getty is allowed
2635 * to spawn.
2636 *
2637 * Now, if it would be correct ;-/ The current code has a nasty hole -
2638 * it doesn't catch files in flight. We may send the descriptor to ourselves
2639 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2640 *
2641 * Nasty bug: do_SAK is being called in interrupt context. This can
2642 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2643 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002644void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
2646#ifdef TTY_SOFT_SAK
2647 tty_hangup(tty);
2648#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08002649 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002650 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 int i;
2652 struct file *filp;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002653 struct fdtable *fdt;
Alan Cox37bdfb02008-02-08 04:18:47 -08002654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 if (!tty)
2656 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002657 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08002658
Dan Carpenterb3f13de2006-12-13 00:35:09 -08002659 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Alan Coxf34d7a52008-04-30 00:54:13 -07002661 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08002662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002664 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002665 do_each_pid_task(session, PIDTYPE_SID, p) {
Eric W. Biederman652486f2006-03-28 16:11:02 -08002666 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07002667 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002668 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002669 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002670 } while_each_pid_task(session, PIDTYPE_SID, p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002671 /* Now kill any processes that happen to have the
2672 * tty open.
2673 */
2674 do_each_thread(g, p) {
2675 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07002677 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002678 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 send_sig(SIGKILL, p, 1);
2680 continue;
2681 }
2682 task_lock(p);
2683 if (p->files) {
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07002684 /*
2685 * We don't take a ref to the file, so we must
2686 * hold ->file_lock instead.
2687 */
2688 spin_lock(&p->files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002689 fdt = files_fdtable(p->files);
Alan Cox37bdfb02008-02-08 04:18:47 -08002690 for (i = 0; i < fdt->max_fds; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 filp = fcheck_files(p->files, i);
2692 if (!filp)
2693 continue;
2694 if (filp->f_op->read == tty_read &&
2695 filp->private_data == tty) {
2696 printk(KERN_NOTICE "SAK: killed process %d"
2697 " (%s): fd#%d opened to the tty\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002698 task_pid_nr(p), p->comm, i);
Eric W. Biederman20ac9432006-04-13 04:49:07 -06002699 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 break;
2701 }
2702 }
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07002703 spin_unlock(&p->files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
2705 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002706 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 read_unlock(&tasklist_lock);
2708#endif
2709}
2710
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002711static void do_SAK_work(struct work_struct *work)
2712{
2713 struct tty_struct *tty =
2714 container_of(work, struct tty_struct, SAK_work);
2715 __do_SAK(tty);
2716}
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718/*
2719 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2720 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2721 * the values which we write to it will be identical to the values which it
2722 * already has. --akpm
2723 */
2724void do_SAK(struct tty_struct *tty)
2725{
2726 if (!tty)
2727 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 schedule_work(&tty->SAK_work);
2729}
2730
2731EXPORT_SYMBOL(do_SAK);
2732
Alan Coxaf9b8972006-08-27 01:24:01 -07002733/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002734 * initialize_tty_struct
2735 * @tty: tty to initialize
2736 *
2737 * This subroutine initializes a tty structure that has been newly
2738 * allocated.
2739 *
2740 * Locking: none - tty in question must not be exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002742
Alan Coxbf970ee2008-10-13 10:42:39 +01002743void initialize_tty_struct(struct tty_struct *tty,
2744 struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
2746 memset(tty, 0, sizeof(struct tty_struct));
Alan Cox9c9f4de2008-10-13 10:37:26 +01002747 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 tty->magic = TTY_MAGIC;
Alan Cox01e1abb2008-07-22 11:16:55 +01002749 tty_ldisc_init(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002750 tty->session = NULL;
2751 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 tty->overrun_time = jiffies;
Alan Cox33f0f882006-01-09 20:54:13 -08002753 tty->buf.head = tty->buf.tail = NULL;
2754 tty_buffer_init(tty);
Arjan van de Ven5785c952006-09-29 02:00:43 -07002755 mutex_init(&tty->termios_mutex);
Alan Coxc65c9bc2009-06-11 12:50:12 +01002756 mutex_init(&tty->ldisc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 init_waitqueue_head(&tty->write_wait);
2758 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00002759 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08002760 mutex_init(&tty->atomic_read_lock);
2761 mutex_init(&tty->atomic_write_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +00002762 mutex_init(&tty->output_lock);
2763 mutex_init(&tty->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 spin_lock_init(&tty->read_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07002765 spin_lock_init(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002767 INIT_WORK(&tty->SAK_work, do_SAK_work);
Alan Coxbf970ee2008-10-13 10:42:39 +01002768
2769 tty->driver = driver;
2770 tty->ops = driver->ops;
2771 tty->index = idx;
2772 tty_line_name(driver, idx, tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773}
2774
Alan Coxf34d7a52008-04-30 00:54:13 -07002775/**
2776 * tty_put_char - write one character to a tty
2777 * @tty: tty
2778 * @ch: character
2779 *
2780 * Write one byte to the tty using the provided put_char method
2781 * if present. Returns the number of characters successfully output.
2782 *
2783 * Note: the specific put_char operation in the driver layer may go
2784 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002786
Alan Coxf34d7a52008-04-30 00:54:13 -07002787int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Alan Coxf34d7a52008-04-30 00:54:13 -07002789 if (tty->ops->put_char)
2790 return tty->ops->put_char(tty, ch);
2791 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
Alan Coxf34d7a52008-04-30 00:54:13 -07002793EXPORT_SYMBOL_GPL(tty_put_char);
2794
Alan Coxd81ed102008-10-13 10:41:42 +01002795struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002798 * tty_register_device - register a tty device
2799 * @driver: the tty driver that describes the tty device
2800 * @index: the index in the tty driver for this tty device
2801 * @device: a struct device that is associated with this tty device.
2802 * This field is optional, if there is no known struct device
2803 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07002805 * Returns a pointer to the struct device for this tty device
2806 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002807 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002808 * This call is required to be made to register an individual tty device
2809 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2810 * that bit is not set, this function should not be called by a tty
2811 * driver.
2812 *
2813 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002815
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07002816struct device *tty_register_device(struct tty_driver *driver, unsigned index,
2817 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
2819 char name[64];
2820 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2821
2822 if (index >= driver->num) {
2823 printk(KERN_ERR "Attempt to register invalid tty line number "
2824 " (%d).\n", index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002825 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (driver->type == TTY_DRIVER_TYPE_PTY)
2829 pty_line_name(driver, index, name);
2830 else
2831 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002832
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -07002833 return device_create(tty_class, device, dev, NULL, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
Alan Cox7d7b93c2008-10-13 10:42:09 +01002835EXPORT_SYMBOL(tty_register_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002838 * tty_unregister_device - unregister a tty device
2839 * @driver: the tty driver that describes the tty device
2840 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002842 * If a tty device is registered with a call to tty_register_device() then
2843 * this function must be called when the tty device is gone.
2844 *
2845 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002847
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848void tty_unregister_device(struct tty_driver *driver, unsigned index)
2849{
Alan Cox37bdfb02008-02-08 04:18:47 -08002850 device_destroy(tty_class,
2851 MKDEV(driver->major, driver->minor_start) + index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853EXPORT_SYMBOL(tty_unregister_device);
2854
2855struct tty_driver *alloc_tty_driver(int lines)
2856{
2857 struct tty_driver *driver;
2858
Jean Delvare506eb992007-07-15 23:40:14 -07002859 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (driver) {
Alan Cox7d7b93c2008-10-13 10:42:09 +01002861 kref_init(&driver->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 driver->magic = TTY_DRIVER_MAGIC;
2863 driver->num = lines;
2864 /* later we'll move allocation of tables here */
2865 }
2866 return driver;
2867}
Alan Cox7d7b93c2008-10-13 10:42:09 +01002868EXPORT_SYMBOL(alloc_tty_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Alan Cox7d7b93c2008-10-13 10:42:09 +01002870static void destruct_tty_driver(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Alan Cox7d7b93c2008-10-13 10:42:09 +01002872 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
2873 int i;
2874 struct ktermios *tp;
2875 void *p;
2876
2877 if (driver->flags & TTY_DRIVER_INSTALLED) {
2878 /*
2879 * Free the termios and termios_locked structures because
2880 * we don't want to get memory leaks when modular tty
2881 * drivers are removed from the kernel.
2882 */
2883 for (i = 0; i < driver->num; i++) {
2884 tp = driver->termios[i];
2885 if (tp) {
2886 driver->termios[i] = NULL;
2887 kfree(tp);
2888 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01002889 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
2890 tty_unregister_device(driver, i);
2891 }
2892 p = driver->ttys;
2893 proc_tty_unregister_driver(driver);
2894 driver->ttys = NULL;
Alan Coxfe6e29f2008-10-13 10:44:08 +01002895 driver->termios = NULL;
Alan Cox7d7b93c2008-10-13 10:42:09 +01002896 kfree(p);
2897 cdev_del(&driver->cdev);
2898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 kfree(driver);
2900}
2901
Alan Cox7d7b93c2008-10-13 10:42:09 +01002902void tty_driver_kref_put(struct tty_driver *driver)
2903{
2904 kref_put(&driver->kref, destruct_tty_driver);
2905}
2906EXPORT_SYMBOL(tty_driver_kref_put);
2907
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002908void tty_set_operations(struct tty_driver *driver,
2909 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
Alan Coxf34d7a52008-04-30 00:54:13 -07002911 driver->ops = op;
2912};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913EXPORT_SYMBOL(tty_set_operations);
2914
Alan Cox7d7b93c2008-10-13 10:42:09 +01002915void put_tty_driver(struct tty_driver *d)
2916{
2917 tty_driver_kref_put(d);
2918}
2919EXPORT_SYMBOL(put_tty_driver);
2920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921/*
2922 * Called by a tty driver to register itself.
2923 */
2924int tty_register_driver(struct tty_driver *driver)
2925{
2926 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08002927 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 dev_t dev;
2929 void **p = NULL;
2930
Andy Whitcroft543691a62007-05-06 14:49:33 -07002931 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
Alan Coxfe6e29f2008-10-13 10:44:08 +01002932 p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (!p)
2934 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
2936
2937 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002938 error = alloc_chrdev_region(&dev, driver->minor_start,
2939 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 if (!error) {
2941 driver->major = MAJOR(dev);
2942 driver->minor_start = MINOR(dev);
2943 }
2944 } else {
2945 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01002946 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
2948 if (error < 0) {
2949 kfree(p);
2950 return error;
2951 }
2952
2953 if (p) {
2954 driver->ttys = (struct tty_struct **)p;
Alan Coxedc6afc2006-12-08 02:38:44 -08002955 driver->termios = (struct ktermios **)(p + driver->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 } else {
2957 driver->ttys = NULL;
2958 driver->termios = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
2960
2961 cdev_init(&driver->cdev, &tty_fops);
2962 driver->cdev.owner = driver->owner;
2963 error = cdev_add(&driver->cdev, dev, driver->num);
2964 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 unregister_chrdev_region(dev, driver->num);
2966 driver->ttys = NULL;
Alan Coxfe6e29f2008-10-13 10:44:08 +01002967 driver->termios = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 kfree(p);
2969 return error;
2970 }
2971
Alexey Dobriyanca509f62007-05-08 00:27:12 -07002972 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07002974 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08002975
2976 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
2977 for (i = 0; i < driver->num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 tty_register_device(driver, i, NULL);
2979 }
2980 proc_tty_register_driver(driver);
Alan Cox7d7b93c2008-10-13 10:42:09 +01002981 driver->flags |= TTY_DRIVER_INSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return 0;
2983}
2984
2985EXPORT_SYMBOL(tty_register_driver);
2986
2987/*
2988 * Called by a tty driver to unregister itself.
2989 */
2990int tty_unregister_driver(struct tty_driver *driver)
2991{
Alan Cox7d7b93c2008-10-13 10:42:09 +01002992#if 0
2993 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 if (driver->refcount)
2995 return -EBUSY;
Alan Cox7d7b93c2008-10-13 10:42:09 +01002996#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
2998 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07002999 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003001 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return 0;
3003}
Alan Cox7d7b93c2008-10-13 10:42:09 +01003004
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005EXPORT_SYMBOL(tty_unregister_driver);
3006
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003007dev_t tty_devnum(struct tty_struct *tty)
3008{
3009 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3010}
3011EXPORT_SYMBOL(tty_devnum);
3012
3013void proc_clear_tty(struct task_struct *p)
3014{
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003015 unsigned long flags;
Alan Cox9c9f4de2008-10-13 10:37:26 +01003016 struct tty_struct *tty;
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003017 spin_lock_irqsave(&p->sighand->siglock, flags);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003018 tty = p->signal->tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003019 p->signal->tty = NULL;
Arjan van de Ven7c3b1dc2008-10-15 10:52:34 +01003020 spin_unlock_irqrestore(&p->sighand->siglock, flags);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003021 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003022}
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003023
Alan Cox47f86832008-04-30 00:53:30 -07003024/* Called under the sighand lock */
3025
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003026static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003027{
3028 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -07003029 unsigned long flags;
3030 /* We should not have a session or pgrp to put here but.... */
3031 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -06003032 put_pid(tty->session);
3033 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003034 tty->pgrp = get_pid(task_pgrp(tsk));
Alan Cox47f86832008-04-30 00:53:30 -07003035 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3036 tty->session = get_pid(task_session(tsk));
Alan Cox9c9f4de2008-10-13 10:37:26 +01003037 if (tsk->signal->tty) {
3038 printk(KERN_DEBUG "tty not NULL!!\n");
3039 tty_kref_put(tsk->signal->tty);
3040 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003041 }
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003042 put_pid(tsk->signal->tty_old_pgrp);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003043 tsk->signal->tty = tty_kref_get(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003044 tsk->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003045}
3046
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07003047static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003048{
3049 spin_lock_irq(&tsk->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003050 __proc_set_tty(tsk, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003051 spin_unlock_irq(&tsk->sighand->siglock);
3052}
3053
3054struct tty_struct *get_current_tty(void)
3055{
3056 struct tty_struct *tty;
Alan Cox934e6eb2008-10-13 10:40:43 +01003057 unsigned long flags;
3058
3059 spin_lock_irqsave(&current->sighand->siglock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +01003060 tty = tty_kref_get(current->signal->tty);
Alan Cox934e6eb2008-10-13 10:40:43 +01003061 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003062 return tty;
3063}
Heiko Carstensa311f742006-12-13 00:33:41 -08003064EXPORT_SYMBOL_GPL(get_current_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Alan Coxd81ed102008-10-13 10:41:42 +01003066void tty_default_fops(struct file_operations *fops)
3067{
3068 *fops = tty_fops;
3069}
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071/*
3072 * Initialize the console device. This is called *early*, so
3073 * we can't necessarily depend on lots of kernel help here.
3074 * Just do some early initializations, and do the complex setup
3075 * later.
3076 */
3077void __init console_init(void)
3078{
3079 initcall_t *call;
3080
3081 /* Setup the default TTY line discipline. */
Alan Cox01e1abb2008-07-22 11:16:55 +01003082 tty_ldisc_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
3084 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08003085 * set up the console device so that later boot sequences can
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 * inform about problems etc..
3087 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 call = __con_initcall_start;
3089 while (call < __con_initcall_end) {
3090 (*call)();
3091 call++;
3092 }
3093}
3094
Kay Sieverse454cea2009-09-18 23:01:12 +02003095static char *tty_devnode(struct device *dev, mode_t *mode)
3096{
3097 if (!mode)
3098 return NULL;
3099 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3100 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3101 *mode = 0666;
3102 return NULL;
3103}
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105static int __init tty_class_init(void)
3106{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003107 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 if (IS_ERR(tty_class))
3109 return PTR_ERR(tty_class);
Kay Sieverse454cea2009-09-18 23:01:12 +02003110 tty_class->devnode = tty_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return 0;
3112}
3113
3114postcore_initcall(tty_class_init);
3115
3116/* 3/2004 jmc: why do these devices exist? */
3117
3118static struct cdev tty_cdev, console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120/*
3121 * Ok, now we can initialize the rest of the tty devices and can count
3122 * on memory allocations, interrupts etc..
3123 */
3124static int __init tty_init(void)
3125{
3126 cdev_init(&tty_cdev, &tty_fops);
3127 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3128 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3129 panic("Couldn't register /dev/tty driver\n");
Alan Coxd81ed102008-10-13 10:41:42 +01003130 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003131 "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133 cdev_init(&console_cdev, &console_fops);
3134 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3135 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3136 panic("Couldn't register /dev/console driver\n");
Alan Coxd81ed102008-10-13 10:41:42 +01003137 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003138 "console");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140#ifdef CONFIG_VT
Alan Coxd81ed102008-10-13 10:41:42 +01003141 vty_init(&console_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142#endif
3143 return 0;
3144}
3145module_init(tty_init);