serial: Add CONSOLE_POLL support for uartlite
[linux-2.6-block.git] / drivers / char / tty_io.c
CommitLineData
1da177e4
LT
1/*
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
37bdfb07 22 * makes for cleaner and more compact code. -TYT, 9/17/92
1da177e4
LT
23 *
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
37bdfb07 44 *
1da177e4
LT
45 * 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 *
d81ed103 52 * Rewrote tty_init_dev and tty_release_dev to eliminate races.
1da177e4
LT
53 * -- 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.
37bdfb07
AC
65 * alloc_tty_struct() always uses kmalloc()
66 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
1da177e4
LT
67 */
68
1da177e4
LT
69#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>
9f3acc31 81#include <linux/fdtable.h>
1da177e4
LT
82#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>
1da177e4
LT
95#include <linux/wait.h>
96#include <linux/bitops.h>
b20f3ae5 97#include <linux/delay.h>
a352def2 98#include <linux/seq_file.h>
1da177e4 99
a352def2 100#include <linux/uaccess.h>
1da177e4
LT
101#include <asm/system.h>
102
103#include <linux/kbd_kern.h>
104#include <linux/vt_kern.h>
105#include <linux/selection.h>
1da177e4
LT
106
107#include <linux/kmod.h>
b488893a 108#include <linux/nsproxy.h>
1da177e4
LT
109
110#undef TTY_DEBUG_HANGUP
111
112#define TTY_PARANOIA_CHECK 1
113#define CHECK_TTY_COUNT 1
114
edc6afc5 115struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
1da177e4
LT
116 .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,
edc6afc5
AC
121 .c_cc = INIT_C_CC,
122 .c_ispeed = 38400,
123 .c_ospeed = 38400
1da177e4
LT
124};
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 */
37bdfb07 131
1da177e4
LT
132LIST_HEAD(tty_drivers); /* linked list of tty drivers */
133
24ec839c 134/* Mutex to protect creating and releasing a tty. This is shared with
1da177e4 135 vt.c for deeply disgusting hack reasons */
70522e12 136DEFINE_MUTEX(tty_mutex);
de2a84f2 137EXPORT_SYMBOL(tty_mutex);
1da177e4 138
ee2ffa0d
NP
139/* Spinlock to protect the tty->tty_files list */
140DEFINE_SPINLOCK(tty_files_lock);
141
1da177e4
LT
142static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
143static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
37bdfb07
AC
144ssize_t redirected_tty_write(struct file *, const char __user *,
145 size_t, loff_t *);
1da177e4
LT
146static unsigned int tty_poll(struct file *, poll_table *);
147static int tty_open(struct inode *, struct file *);
04f378b1 148long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
e10cc1df 149#ifdef CONFIG_COMPAT
37bdfb07 150static long tty_compat_ioctl(struct file *file, unsigned int cmd,
e10cc1df
PF
151 unsigned long arg);
152#else
153#define tty_compat_ioctl NULL
154#endif
ec79d605 155static int __tty_fasync(int fd, struct file *filp, int on);
37bdfb07 156static int tty_fasync(int fd, struct file *filp, int on);
d5698c28 157static void release_tty(struct tty_struct *tty, int idx);
2a65f1d9 158static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
98a27ba4 159static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
1da177e4 160
af9b897e
AC
161/**
162 * alloc_tty_struct - allocate a tty object
163 *
164 * Return a new empty tty structure. The data fields have not
165 * been initialized in any way but has been zeroed
166 *
167 * Locking: none
af9b897e 168 */
1da177e4 169
bf970ee4 170struct tty_struct *alloc_tty_struct(void)
1da177e4 171{
1266b1e1 172 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
1da177e4
LT
173}
174
af9b897e
AC
175/**
176 * free_tty_struct - free a disused tty
177 * @tty: tty struct to free
178 *
179 * Free the write buffers, tty queue and tty memory itself.
180 *
181 * Locking: none. Must be called after tty is definitely unused
182 */
183
bf970ee4 184void free_tty_struct(struct tty_struct *tty)
1da177e4 185{
30004ac9
DES
186 if (tty->dev)
187 put_device(tty->dev);
1da177e4 188 kfree(tty->write_buf);
33f0f88f 189 tty_buffer_free_all(tty);
1da177e4
LT
190 kfree(tty);
191}
192
d996b62a
NP
193static inline struct tty_struct *file_tty(struct file *file)
194{
195 return ((struct tty_file_private *)file->private_data)->tty;
196}
197
198/* Associate a new file with the tty structure */
199void tty_add_file(struct tty_struct *tty, struct file *file)
200{
201 struct tty_file_private *priv;
202
203 /* XXX: must implement proper error handling in callers */
204 priv = kmalloc(sizeof(*priv), GFP_KERNEL|__GFP_NOFAIL);
205
206 priv->tty = tty;
207 priv->file = file;
208 file->private_data = priv;
209
210 spin_lock(&tty_files_lock);
211 list_add(&priv->list, &tty->tty_files);
212 spin_unlock(&tty_files_lock);
213}
214
215/* Delete file from its tty */
216void tty_del_file(struct file *file)
217{
218 struct tty_file_private *priv = file->private_data;
219
220 spin_lock(&tty_files_lock);
221 list_del(&priv->list);
222 spin_unlock(&tty_files_lock);
223 file->private_data = NULL;
224 kfree(priv);
225}
226
227
1da177e4
LT
228#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
229
af9b897e
AC
230/**
231 * tty_name - return tty naming
232 * @tty: tty structure
233 * @buf: buffer for output
234 *
235 * Convert a tty structure into a name. The name reflects the kernel
236 * naming policy and if udev is in use may not reflect user space
237 *
238 * Locking: none
239 */
240
1da177e4
LT
241char *tty_name(struct tty_struct *tty, char *buf)
242{
243 if (!tty) /* Hmm. NULL pointer. That's fun. */
244 strcpy(buf, "NULL tty");
245 else
246 strcpy(buf, tty->name);
247 return buf;
248}
249
250EXPORT_SYMBOL(tty_name);
251
d769a669 252int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
1da177e4
LT
253 const char *routine)
254{
255#ifdef TTY_PARANOIA_CHECK
256 if (!tty) {
257 printk(KERN_WARNING
258 "null TTY for (%d:%d) in %s\n",
259 imajor(inode), iminor(inode), routine);
260 return 1;
261 }
262 if (tty->magic != TTY_MAGIC) {
263 printk(KERN_WARNING
264 "bad magic number for tty struct (%d:%d) in %s\n",
265 imajor(inode), iminor(inode), routine);
266 return 1;
267 }
268#endif
269 return 0;
270}
271
272static int check_tty_count(struct tty_struct *tty, const char *routine)
273{
274#ifdef CHECK_TTY_COUNT
275 struct list_head *p;
276 int count = 0;
37bdfb07 277
ee2ffa0d 278 spin_lock(&tty_files_lock);
1da177e4
LT
279 list_for_each(p, &tty->tty_files) {
280 count++;
281 }
ee2ffa0d 282 spin_unlock(&tty_files_lock);
1da177e4
LT
283 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
284 tty->driver->subtype == PTY_TYPE_SLAVE &&
285 tty->link && tty->link->count)
286 count++;
287 if (tty->count != count) {
288 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
289 "!= #fd's(%d) in %s\n",
290 tty->name, tty->count, count, routine);
291 return count;
24ec839c 292 }
1da177e4
LT
293#endif
294 return 0;
295}
296
af9b897e
AC
297/**
298 * get_tty_driver - find device of a tty
299 * @dev_t: device identifier
300 * @index: returns the index of the tty
301 *
302 * This routine returns a tty driver structure, given a device number
303 * and also passes back the index number.
304 *
305 * Locking: caller must hold tty_mutex
1da177e4 306 */
af9b897e 307
1da177e4
LT
308static struct tty_driver *get_tty_driver(dev_t device, int *index)
309{
310 struct tty_driver *p;
311
312 list_for_each_entry(p, &tty_drivers, tty_drivers) {
313 dev_t base = MKDEV(p->major, p->minor_start);
314 if (device < base || device >= base + p->num)
315 continue;
316 *index = device - base;
7d7b93c1 317 return tty_driver_kref_get(p);
1da177e4
LT
318 }
319 return NULL;
320}
321
f2d937f3
JW
322#ifdef CONFIG_CONSOLE_POLL
323
324/**
325 * tty_find_polling_driver - find device of a polled tty
326 * @name: name string to match
327 * @line: pointer to resulting tty line nr
328 *
329 * This routine returns a tty driver structure, given a name
330 * and the condition that the tty driver is capable of polled
331 * operation.
332 */
333struct tty_driver *tty_find_polling_driver(char *name, int *line)
334{
335 struct tty_driver *p, *res = NULL;
336 int tty_line = 0;
0dca0fd2 337 int len;
5f0878ac 338 char *str, *stp;
f2d937f3 339
0dca0fd2
JW
340 for (str = name; *str; str++)
341 if ((*str >= '0' && *str <= '9') || *str == ',')
342 break;
343 if (!*str)
344 return NULL;
345
346 len = str - name;
347 tty_line = simple_strtoul(str, &str, 10);
348
f2d937f3
JW
349 mutex_lock(&tty_mutex);
350 /* Search through the tty devices to look for a match */
351 list_for_each_entry(p, &tty_drivers, tty_drivers) {
0dca0fd2
JW
352 if (strncmp(name, p->name, len) != 0)
353 continue;
5f0878ac
AC
354 stp = str;
355 if (*stp == ',')
356 stp++;
357 if (*stp == '\0')
358 stp = NULL;
f2d937f3 359
6eb68d6f 360 if (tty_line >= 0 && tty_line < p->num && p->ops &&
5f0878ac 361 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
7d7b93c1 362 res = tty_driver_kref_get(p);
f2d937f3
JW
363 *line = tty_line;
364 break;
365 }
366 }
367 mutex_unlock(&tty_mutex);
368
369 return res;
370}
371EXPORT_SYMBOL_GPL(tty_find_polling_driver);
372#endif
373
af9b897e
AC
374/**
375 * tty_check_change - check for POSIX terminal changes
376 * @tty: tty to check
377 *
378 * If we try to write to, or set the state of, a terminal and we're
379 * not in the foreground, send a SIGTTOU. If the signal is blocked or
380 * ignored, go ahead and perform the operation. (POSIX 7.2)
381 *
978e595f 382 * Locking: ctrl_lock
1da177e4 383 */
af9b897e 384
37bdfb07 385int tty_check_change(struct tty_struct *tty)
1da177e4 386{
47f86834
AC
387 unsigned long flags;
388 int ret = 0;
389
1da177e4
LT
390 if (current->signal->tty != tty)
391 return 0;
47f86834
AC
392
393 spin_lock_irqsave(&tty->ctrl_lock, flags);
394
ab521dc0
EB
395 if (!tty->pgrp) {
396 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
9ffee4cb 397 goto out_unlock;
1da177e4 398 }
ab521dc0 399 if (task_pgrp(current) == tty->pgrp)
9ffee4cb
AM
400 goto out_unlock;
401 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1da177e4 402 if (is_ignored(SIGTTOU))
47f86834
AC
403 goto out;
404 if (is_current_pgrp_orphaned()) {
405 ret = -EIO;
406 goto out;
407 }
040b6362
ON
408 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
409 set_thread_flag(TIF_SIGPENDING);
47f86834
AC
410 ret = -ERESTARTSYS;
411out:
9ffee4cb
AM
412 return ret;
413out_unlock:
47f86834
AC
414 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
415 return ret;
1da177e4
LT
416}
417
418EXPORT_SYMBOL(tty_check_change);
419
37bdfb07 420static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
1da177e4
LT
421 size_t count, loff_t *ppos)
422{
423 return 0;
424}
425
37bdfb07 426static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
1da177e4
LT
427 size_t count, loff_t *ppos)
428{
429 return -EIO;
430}
431
432/* No kernel lock held - none needed ;) */
37bdfb07 433static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
1da177e4
LT
434{
435 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
436}
437
04f378b1
AC
438static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
439 unsigned long arg)
38ad2ed0
PF
440{
441 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
442}
443
37bdfb07 444static long hung_up_tty_compat_ioctl(struct file *file,
38ad2ed0 445 unsigned int cmd, unsigned long arg)
1da177e4
LT
446{
447 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
448}
449
62322d25 450static const struct file_operations tty_fops = {
1da177e4
LT
451 .llseek = no_llseek,
452 .read = tty_read,
453 .write = tty_write,
454 .poll = tty_poll,
04f378b1 455 .unlocked_ioctl = tty_ioctl,
e10cc1df 456 .compat_ioctl = tty_compat_ioctl,
1da177e4
LT
457 .open = tty_open,
458 .release = tty_release,
459 .fasync = tty_fasync,
460};
461
62322d25 462static const struct file_operations console_fops = {
1da177e4
LT
463 .llseek = no_llseek,
464 .read = tty_read,
465 .write = redirected_tty_write,
466 .poll = tty_poll,
04f378b1 467 .unlocked_ioctl = tty_ioctl,
e10cc1df 468 .compat_ioctl = tty_compat_ioctl,
1da177e4
LT
469 .open = tty_open,
470 .release = tty_release,
471 .fasync = tty_fasync,
472};
473
62322d25 474static const struct file_operations hung_up_tty_fops = {
1da177e4
LT
475 .llseek = no_llseek,
476 .read = hung_up_tty_read,
477 .write = hung_up_tty_write,
478 .poll = hung_up_tty_poll,
04f378b1 479 .unlocked_ioctl = hung_up_tty_ioctl,
38ad2ed0 480 .compat_ioctl = hung_up_tty_compat_ioctl,
1da177e4
LT
481 .release = tty_release,
482};
483
484static DEFINE_SPINLOCK(redirect_lock);
485static struct file *redirect;
486
487/**
488 * tty_wakeup - request more data
489 * @tty: terminal
490 *
491 * Internal and external helper for wakeups of tty. This function
492 * informs the line discipline if present that the driver is ready
493 * to receive more output data.
494 */
37bdfb07 495
1da177e4
LT
496void tty_wakeup(struct tty_struct *tty)
497{
498 struct tty_ldisc *ld;
37bdfb07 499
1da177e4
LT
500 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
501 ld = tty_ldisc_ref(tty);
37bdfb07 502 if (ld) {
a352def2
AC
503 if (ld->ops->write_wakeup)
504 ld->ops->write_wakeup(tty);
1da177e4
LT
505 tty_ldisc_deref(ld);
506 }
507 }
4b19449d 508 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
1da177e4
LT
509}
510
511EXPORT_SYMBOL_GPL(tty_wakeup);
512
af9b897e 513/**
11dbf203 514 * __tty_hangup - actual handler for hangup events
65f27f38 515 * @work: tty device
af9b897e 516 *
1bad879a 517 * This can be called by the "eventd" kernel thread. That is process
af9b897e
AC
518 * synchronous but doesn't hold any locks, so we need to make sure we
519 * have the appropriate locks for what we're doing.
520 *
521 * The hangup event clears any pending redirections onto the hung up
522 * device. It ensures future writes will error and it does the needed
523 * line discipline hangup and signal delivery. The tty object itself
524 * remains intact.
525 *
526 * Locking:
ec79d605 527 * BTM
24ec839c
PZ
528 * redirect lock for undoing redirection
529 * file list lock for manipulating list of ttys
530 * tty_ldisc_lock from called functions
531 * termios_mutex resetting termios data
532 * tasklist_lock to walk task list for hangup event
533 * ->siglock to protect ->signal/->sighand
1da177e4 534 */
11dbf203 535void __tty_hangup(struct tty_struct *tty)
1da177e4 536{
37bdfb07 537 struct file *cons_filp = NULL;
1da177e4
LT
538 struct file *filp, *f = NULL;
539 struct task_struct *p;
d996b62a 540 struct tty_file_private *priv;
1da177e4 541 int closecount = 0, n;
47f86834 542 unsigned long flags;
9c9f4ded 543 int refs = 0;
1da177e4
LT
544
545 if (!tty)
546 return;
547
1da177e4
LT
548
549 spin_lock(&redirect_lock);
d996b62a 550 if (redirect && file_tty(redirect) == tty) {
1da177e4
LT
551 f = redirect;
552 redirect = NULL;
553 }
554 spin_unlock(&redirect_lock);
37bdfb07 555
11dbf203
AB
556 tty_lock();
557
ec79d605
AB
558 /* inuse_filps is protected by the single tty lock,
559 this really needs to change if we want to flush the
560 workqueue with the lock held */
11dbf203 561 check_tty_count(tty, "tty_hangup");
36ba782e 562
ee2ffa0d 563 spin_lock(&tty_files_lock);
1da177e4 564 /* This breaks for file handles being sent over AF_UNIX sockets ? */
d996b62a
NP
565 list_for_each_entry(priv, &tty->tty_files, list) {
566 filp = priv->file;
1da177e4
LT
567 if (filp->f_op->write == redirected_tty_write)
568 cons_filp = filp;
569 if (filp->f_op->write != tty_write)
570 continue;
571 closecount++;
ec79d605 572 __tty_fasync(-1, filp, 0); /* can't block */
1da177e4
LT
573 filp->f_op = &hung_up_tty_fops;
574 }
ee2ffa0d 575 spin_unlock(&tty_files_lock);
37bdfb07 576
c65c9bc3 577 tty_ldisc_hangup(tty);
37bdfb07 578
1da177e4 579 read_lock(&tasklist_lock);
ab521dc0
EB
580 if (tty->session) {
581 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
24ec839c 582 spin_lock_irq(&p->sighand->siglock);
9c9f4ded 583 if (p->signal->tty == tty) {
1da177e4 584 p->signal->tty = NULL;
9c9f4ded
AC
585 /* We defer the dereferences outside fo
586 the tasklist lock */
587 refs++;
588 }
24ec839c
PZ
589 if (!p->signal->leader) {
590 spin_unlock_irq(&p->sighand->siglock);
1da177e4 591 continue;
24ec839c
PZ
592 }
593 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
594 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
ab521dc0 595 put_pid(p->signal->tty_old_pgrp); /* A noop */
47f86834 596 spin_lock_irqsave(&tty->ctrl_lock, flags);
ab521dc0
EB
597 if (tty->pgrp)
598 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
47f86834 599 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
24ec839c 600 spin_unlock_irq(&p->sighand->siglock);
ab521dc0 601 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
1da177e4
LT
602 }
603 read_unlock(&tasklist_lock);
604
47f86834 605 spin_lock_irqsave(&tty->ctrl_lock, flags);
c65c9bc3
AC
606 clear_bit(TTY_THROTTLED, &tty->flags);
607 clear_bit(TTY_PUSH, &tty->flags);
608 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
d9c1e9a8
EB
609 put_pid(tty->session);
610 put_pid(tty->pgrp);
ab521dc0
EB
611 tty->session = NULL;
612 tty->pgrp = NULL;
1da177e4 613 tty->ctrl_status = 0;
c65c9bc3 614 set_bit(TTY_HUPPED, &tty->flags);
47f86834
AC
615 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
616
9c9f4ded
AC
617 /* Account for the p->signal references we killed */
618 while (refs--)
619 tty_kref_put(tty);
620
1da177e4 621 /*
37bdfb07
AC
622 * If one of the devices matches a console pointer, we
623 * cannot just call hangup() because that will cause
624 * tty->count and state->count to go out of sync.
625 * So we just call close() the right number of times.
1da177e4
LT
626 */
627 if (cons_filp) {
f34d7a5b 628 if (tty->ops->close)
1da177e4 629 for (n = 0; n < closecount; n++)
f34d7a5b
AC
630 tty->ops->close(tty, cons_filp);
631 } else if (tty->ops->hangup)
632 (tty->ops->hangup)(tty);
37bdfb07
AC
633 /*
634 * We don't want to have driver/ldisc interactions beyond
635 * the ones we did here. The driver layer expects no
636 * calls after ->hangup() from the ldisc side. However we
637 * can't yet guarantee all that.
638 */
1da177e4 639 set_bit(TTY_HUPPED, &tty->flags);
c65c9bc3 640 tty_ldisc_enable(tty);
11dbf203
AB
641
642 tty_unlock();
643
1da177e4
LT
644 if (f)
645 fput(f);
646}
647
ddcd9fb6
AB
648static void do_tty_hangup(struct work_struct *work)
649{
650 struct tty_struct *tty =
651 container_of(work, struct tty_struct, hangup_work);
652
11dbf203 653 __tty_hangup(tty);
ddcd9fb6
AB
654}
655
af9b897e
AC
656/**
657 * tty_hangup - trigger a hangup event
658 * @tty: tty to hangup
659 *
660 * A carrier loss (virtual or otherwise) has occurred on this like
661 * schedule a hangup sequence to run after this event.
662 */
663
37bdfb07 664void tty_hangup(struct tty_struct *tty)
1da177e4
LT
665{
666#ifdef TTY_DEBUG_HANGUP
667 char buf[64];
1da177e4
LT
668 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
669#endif
670 schedule_work(&tty->hangup_work);
671}
672
673EXPORT_SYMBOL(tty_hangup);
674
af9b897e
AC
675/**
676 * tty_vhangup - process vhangup
677 * @tty: tty to hangup
678 *
679 * The user has asked via system call for the terminal to be hung up.
680 * We do this synchronously so that when the syscall returns the process
3a4fa0a2 681 * is complete. That guarantee is necessary for security reasons.
af9b897e
AC
682 */
683
37bdfb07 684void tty_vhangup(struct tty_struct *tty)
1da177e4
LT
685{
686#ifdef TTY_DEBUG_HANGUP
687 char buf[64];
688
689 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
690#endif
11dbf203 691 __tty_hangup(tty);
1da177e4 692}
37bdfb07 693
1da177e4
LT
694EXPORT_SYMBOL(tty_vhangup);
695
11dbf203 696
2cb5998b
AC
697/**
698 * tty_vhangup_self - process vhangup for own ctty
699 *
700 * Perform a vhangup on the current controlling tty
701 */
702
703void tty_vhangup_self(void)
704{
705 struct tty_struct *tty;
706
2cb5998b
AC
707 tty = get_current_tty();
708 if (tty) {
709 tty_vhangup(tty);
710 tty_kref_put(tty);
711 }
2cb5998b
AC
712}
713
af9b897e
AC
714/**
715 * tty_hung_up_p - was tty hung up
716 * @filp: file pointer of tty
717 *
718 * Return true if the tty has been subject to a vhangup or a carrier
719 * loss
720 */
721
37bdfb07 722int tty_hung_up_p(struct file *filp)
1da177e4
LT
723{
724 return (filp->f_op == &hung_up_tty_fops);
725}
726
727EXPORT_SYMBOL(tty_hung_up_p);
728
ab521dc0 729static void session_clear_tty(struct pid *session)
24ec839c
PZ
730{
731 struct task_struct *p;
ab521dc0 732 do_each_pid_task(session, PIDTYPE_SID, p) {
24ec839c 733 proc_clear_tty(p);
ab521dc0 734 } while_each_pid_task(session, PIDTYPE_SID, p);
24ec839c
PZ
735}
736
af9b897e
AC
737/**
738 * disassociate_ctty - disconnect controlling tty
739 * @on_exit: true if exiting so need to "hang up" the session
1da177e4 740 *
af9b897e
AC
741 * This function is typically called only by the session leader, when
742 * it wants to disassociate itself from its controlling tty.
743 *
744 * It performs the following functions:
1da177e4
LT
745 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
746 * (2) Clears the tty from being controlling the session
747 * (3) Clears the controlling tty for all processes in the
748 * session group.
749 *
af9b897e
AC
750 * The argument on_exit is set to 1 if called when a process is
751 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
752 *
24ec839c 753 * Locking:
ec79d605
AB
754 * BTM is taken for hysterical raisins, and held when
755 * called from no_tty().
24ec839c
PZ
756 * tty_mutex is taken to protect tty
757 * ->siglock is taken to protect ->signal/->sighand
758 * tasklist_lock is taken to walk process list for sessions
759 * ->siglock is taken to protect ->signal/->sighand
1da177e4 760 */
af9b897e 761
1da177e4
LT
762void disassociate_ctty(int on_exit)
763{
764 struct tty_struct *tty;
ab521dc0 765 struct pid *tty_pgrp = NULL;
1da177e4 766
5ec93d11
AC
767 if (!current->signal->leader)
768 return;
1da177e4 769
24ec839c 770 tty = get_current_tty();
1da177e4 771 if (tty) {
ab521dc0 772 tty_pgrp = get_pid(tty->pgrp);
ddcd9fb6 773 if (on_exit) {
ddcd9fb6 774 if (tty->driver->type != TTY_DRIVER_TYPE_PTY)
11dbf203 775 tty_vhangup(tty);
ddcd9fb6 776 }
452a00d2 777 tty_kref_put(tty);
680a9671 778 } else if (on_exit) {
ab521dc0 779 struct pid *old_pgrp;
680a9671
EB
780 spin_lock_irq(&current->sighand->siglock);
781 old_pgrp = current->signal->tty_old_pgrp;
ab521dc0 782 current->signal->tty_old_pgrp = NULL;
680a9671 783 spin_unlock_irq(&current->sighand->siglock);
24ec839c 784 if (old_pgrp) {
ab521dc0
EB
785 kill_pgrp(old_pgrp, SIGHUP, on_exit);
786 kill_pgrp(old_pgrp, SIGCONT, on_exit);
787 put_pid(old_pgrp);
1da177e4 788 }
1da177e4
LT
789 return;
790 }
ab521dc0
EB
791 if (tty_pgrp) {
792 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
1da177e4 793 if (!on_exit)
ab521dc0
EB
794 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
795 put_pid(tty_pgrp);
1da177e4
LT
796 }
797
24ec839c 798 spin_lock_irq(&current->sighand->siglock);
2a65f1d9 799 put_pid(current->signal->tty_old_pgrp);
23cac8de 800 current->signal->tty_old_pgrp = NULL;
24ec839c
PZ
801 spin_unlock_irq(&current->sighand->siglock);
802
24ec839c
PZ
803 tty = get_current_tty();
804 if (tty) {
47f86834
AC
805 unsigned long flags;
806 spin_lock_irqsave(&tty->ctrl_lock, flags);
ab521dc0
EB
807 put_pid(tty->session);
808 put_pid(tty->pgrp);
809 tty->session = NULL;
810 tty->pgrp = NULL;
47f86834 811 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
452a00d2 812 tty_kref_put(tty);
24ec839c
PZ
813 } else {
814#ifdef TTY_DEBUG_HANGUP
815 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
816 " = NULL", tty);
817#endif
818 }
1da177e4
LT
819
820 /* Now clear signal->tty under the lock */
821 read_lock(&tasklist_lock);
ab521dc0 822 session_clear_tty(task_session(current));
1da177e4 823 read_unlock(&tasklist_lock);
1da177e4
LT
824}
825
98a27ba4
EB
826/**
827 *
828 * no_tty - Ensure the current process does not have a controlling tty
829 */
830void no_tty(void)
831{
832 struct task_struct *tsk = current;
ec79d605 833 tty_lock();
5ec93d11 834 disassociate_ctty(0);
ec79d605 835 tty_unlock();
98a27ba4
EB
836 proc_clear_tty(tsk);
837}
838
af9b897e
AC
839
840/**
beb7dd86 841 * stop_tty - propagate flow control
af9b897e
AC
842 * @tty: tty to stop
843 *
844 * Perform flow control to the driver. For PTY/TTY pairs we
beb7dd86 845 * must also propagate the TIOCKPKT status. May be called
af9b897e
AC
846 * on an already stopped device and will not re-call the driver
847 * method.
848 *
849 * This functionality is used by both the line disciplines for
850 * halting incoming flow and by the driver. It may therefore be
851 * called from any context, may be under the tty atomic_write_lock
852 * but not always.
853 *
854 * Locking:
04f378b1 855 * Uses the tty control lock internally
af9b897e
AC
856 */
857
1da177e4
LT
858void stop_tty(struct tty_struct *tty)
859{
04f378b1
AC
860 unsigned long flags;
861 spin_lock_irqsave(&tty->ctrl_lock, flags);
862 if (tty->stopped) {
863 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1da177e4 864 return;
04f378b1 865 }
1da177e4
LT
866 tty->stopped = 1;
867 if (tty->link && tty->link->packet) {
868 tty->ctrl_status &= ~TIOCPKT_START;
869 tty->ctrl_status |= TIOCPKT_STOP;
4b19449d 870 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
1da177e4 871 }
04f378b1 872 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
f34d7a5b
AC
873 if (tty->ops->stop)
874 (tty->ops->stop)(tty);
1da177e4
LT
875}
876
877EXPORT_SYMBOL(stop_tty);
878
af9b897e 879/**
beb7dd86 880 * start_tty - propagate flow control
af9b897e
AC
881 * @tty: tty to start
882 *
883 * Start a tty that has been stopped if at all possible. Perform
3a4fa0a2 884 * any necessary wakeups and propagate the TIOCPKT status. If this
af9b897e
AC
885 * is the tty was previous stopped and is being started then the
886 * driver start method is invoked and the line discipline woken.
887 *
888 * Locking:
04f378b1 889 * ctrl_lock
af9b897e
AC
890 */
891
1da177e4
LT
892void start_tty(struct tty_struct *tty)
893{
04f378b1
AC
894 unsigned long flags;
895 spin_lock_irqsave(&tty->ctrl_lock, flags);
896 if (!tty->stopped || tty->flow_stopped) {
897 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1da177e4 898 return;
04f378b1 899 }
1da177e4
LT
900 tty->stopped = 0;
901 if (tty->link && tty->link->packet) {
902 tty->ctrl_status &= ~TIOCPKT_STOP;
903 tty->ctrl_status |= TIOCPKT_START;
4b19449d 904 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
1da177e4 905 }
04f378b1 906 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
f34d7a5b
AC
907 if (tty->ops->start)
908 (tty->ops->start)(tty);
1da177e4
LT
909 /* If we have a running line discipline it may need kicking */
910 tty_wakeup(tty);
1da177e4
LT
911}
912
913EXPORT_SYMBOL(start_tty);
914
af9b897e
AC
915/**
916 * tty_read - read method for tty device files
917 * @file: pointer to tty file
918 * @buf: user buffer
919 * @count: size of user buffer
920 * @ppos: unused
921 *
922 * Perform the read system call function on this terminal device. Checks
923 * for hung up devices before calling the line discipline method.
924 *
925 * Locking:
47f86834
AC
926 * Locks the line discipline internally while needed. Multiple
927 * read calls may be outstanding in parallel.
af9b897e
AC
928 */
929
37bdfb07 930static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
1da177e4
LT
931 loff_t *ppos)
932{
933 int i;
d996b62a
NP
934 struct inode *inode = file->f_path.dentry->d_inode;
935 struct tty_struct *tty = file_tty(file);
1da177e4
LT
936 struct tty_ldisc *ld;
937
1da177e4
LT
938 if (tty_paranoia_check(tty, inode, "tty_read"))
939 return -EIO;
940 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
941 return -EIO;
942
943 /* We want to wait for the line discipline to sort out in this
944 situation */
945 ld = tty_ldisc_ref_wait(tty);
a352def2
AC
946 if (ld->ops->read)
947 i = (ld->ops->read)(tty, file, buf, count);
1da177e4
LT
948 else
949 i = -EIO;
950 tty_ldisc_deref(ld);
1da177e4
LT
951 if (i > 0)
952 inode->i_atime = current_fs_time(inode->i_sb);
953 return i;
954}
955
9c1729db
AC
956void tty_write_unlock(struct tty_struct *tty)
957{
958 mutex_unlock(&tty->atomic_write_lock);
4b19449d 959 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
9c1729db
AC
960}
961
962int tty_write_lock(struct tty_struct *tty, int ndelay)
963{
964 if (!mutex_trylock(&tty->atomic_write_lock)) {
965 if (ndelay)
966 return -EAGAIN;
967 if (mutex_lock_interruptible(&tty->atomic_write_lock))
968 return -ERESTARTSYS;
969 }
970 return 0;
971}
972
1da177e4
LT
973/*
974 * Split writes up in sane blocksizes to avoid
975 * denial-of-service type attacks
976 */
977static inline ssize_t do_tty_write(
978 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
979 struct tty_struct *tty,
980 struct file *file,
981 const char __user *buf,
982 size_t count)
983{
9c1729db 984 ssize_t ret, written = 0;
1da177e4 985 unsigned int chunk;
37bdfb07 986
9c1729db
AC
987 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
988 if (ret < 0)
989 return ret;
1da177e4
LT
990
991 /*
992 * We chunk up writes into a temporary buffer. This
993 * simplifies low-level drivers immensely, since they
994 * don't have locking issues and user mode accesses.
995 *
996 * But if TTY_NO_WRITE_SPLIT is set, we should use a
997 * big chunk-size..
998 *
999 * The default chunk-size is 2kB, because the NTTY
1000 * layer has problems with bigger chunks. It will
1001 * claim to be able to handle more characters than
1002 * it actually does.
af9b897e
AC
1003 *
1004 * FIXME: This can probably go away now except that 64K chunks
1005 * are too likely to fail unless switched to vmalloc...
1da177e4
LT
1006 */
1007 chunk = 2048;
1008 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1009 chunk = 65536;
1010 if (count < chunk)
1011 chunk = count;
1012
70522e12 1013 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1da177e4 1014 if (tty->write_cnt < chunk) {
402fda92 1015 unsigned char *buf_chunk;
1da177e4
LT
1016
1017 if (chunk < 1024)
1018 chunk = 1024;
1019
402fda92
JW
1020 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1021 if (!buf_chunk) {
9c1729db
AC
1022 ret = -ENOMEM;
1023 goto out;
1da177e4
LT
1024 }
1025 kfree(tty->write_buf);
1026 tty->write_cnt = chunk;
402fda92 1027 tty->write_buf = buf_chunk;
1da177e4
LT
1028 }
1029
1030 /* Do the write .. */
1031 for (;;) {
1032 size_t size = count;
1033 if (size > chunk)
1034 size = chunk;
1035 ret = -EFAULT;
1036 if (copy_from_user(tty->write_buf, buf, size))
1037 break;
1da177e4 1038 ret = write(tty, file, tty->write_buf, size);
1da177e4
LT
1039 if (ret <= 0)
1040 break;
1041 written += ret;
1042 buf += ret;
1043 count -= ret;
1044 if (!count)
1045 break;
1046 ret = -ERESTARTSYS;
1047 if (signal_pending(current))
1048 break;
1049 cond_resched();
1050 }
1051 if (written) {
a7113a96 1052 struct inode *inode = file->f_path.dentry->d_inode;
1da177e4
LT
1053 inode->i_mtime = current_fs_time(inode->i_sb);
1054 ret = written;
1055 }
9c1729db
AC
1056out:
1057 tty_write_unlock(tty);
1da177e4
LT
1058 return ret;
1059}
1060
95f9bfc6
AC
1061/**
1062 * tty_write_message - write a message to a certain tty, not just the console.
1063 * @tty: the destination tty_struct
1064 * @msg: the message to write
1065 *
1066 * This is used for messages that need to be redirected to a specific tty.
1067 * We don't put it into the syslog queue right now maybe in the future if
1068 * really needed.
1069 *
ec79d605 1070 * We must still hold the BTM and test the CLOSING flag for the moment.
95f9bfc6
AC
1071 */
1072
1073void tty_write_message(struct tty_struct *tty, char *msg)
1074{
95f9bfc6
AC
1075 if (tty) {
1076 mutex_lock(&tty->atomic_write_lock);
ec79d605 1077 tty_lock();
eeb89d91 1078 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
ec79d605 1079 tty_unlock();
95f9bfc6 1080 tty->ops->write(tty, msg, strlen(msg));
eeb89d91 1081 } else
ec79d605 1082 tty_unlock();
95f9bfc6
AC
1083 tty_write_unlock(tty);
1084 }
95f9bfc6
AC
1085 return;
1086}
1087
1da177e4 1088
af9b897e
AC
1089/**
1090 * tty_write - write method for tty device file
1091 * @file: tty file pointer
1092 * @buf: user data to write
1093 * @count: bytes to write
1094 * @ppos: unused
1095 *
1096 * Write data to a tty device via the line discipline.
1097 *
1098 * Locking:
1099 * Locks the line discipline as required
1100 * Writes to the tty driver are serialized by the atomic_write_lock
1101 * and are then processed in chunks to the device. The line discipline
a88a69c9 1102 * write method will not be invoked in parallel for each device.
af9b897e
AC
1103 */
1104
37bdfb07
AC
1105static ssize_t tty_write(struct file *file, const char __user *buf,
1106 size_t count, loff_t *ppos)
1da177e4 1107{
a7113a96 1108 struct inode *inode = file->f_path.dentry->d_inode;
d996b62a
NP
1109 struct tty_struct *tty = file_tty(file);
1110 struct tty_ldisc *ld;
1da177e4 1111 ssize_t ret;
37bdfb07 1112
1da177e4
LT
1113 if (tty_paranoia_check(tty, inode, "tty_write"))
1114 return -EIO;
f34d7a5b 1115 if (!tty || !tty->ops->write ||
37bdfb07
AC
1116 (test_bit(TTY_IO_ERROR, &tty->flags)))
1117 return -EIO;
f34d7a5b
AC
1118 /* Short term debug to catch buggy drivers */
1119 if (tty->ops->write_room == NULL)
1120 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1121 tty->driver->name);
37bdfb07 1122 ld = tty_ldisc_ref_wait(tty);
a352def2 1123 if (!ld->ops->write)
1da177e4
LT
1124 ret = -EIO;
1125 else
a352def2 1126 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
1da177e4
LT
1127 tty_ldisc_deref(ld);
1128 return ret;
1129}
1130
37bdfb07
AC
1131ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1132 size_t count, loff_t *ppos)
1da177e4
LT
1133{
1134 struct file *p = NULL;
1135
1136 spin_lock(&redirect_lock);
1137 if (redirect) {
1138 get_file(redirect);
1139 p = redirect;
1140 }
1141 spin_unlock(&redirect_lock);
1142
1143 if (p) {
1144 ssize_t res;
1145 res = vfs_write(p, buf, count, &p->f_pos);
1146 fput(p);
1147 return res;
1148 }
1da177e4
LT
1149 return tty_write(file, buf, count, ppos);
1150}
1151
1152static char ptychar[] = "pqrstuvwxyzabcde";
1153
af9b897e
AC
1154/**
1155 * pty_line_name - generate name for a pty
1156 * @driver: the tty driver in use
1157 * @index: the minor number
1158 * @p: output buffer of at least 6 bytes
1159 *
1160 * Generate a name from a driver reference and write it to the output
1161 * buffer.
1162 *
1163 * Locking: None
1164 */
1165static void pty_line_name(struct tty_driver *driver, int index, char *p)
1da177e4
LT
1166{
1167 int i = index + driver->name_base;
1168 /* ->name is initialized to "ttyp", but "tty" is expected */
1169 sprintf(p, "%s%c%x",
37bdfb07
AC
1170 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1171 ptychar[i >> 4 & 0xf], i & 0xf);
1da177e4
LT
1172}
1173
af9b897e 1174/**
8b0a88d5 1175 * tty_line_name - generate name for a tty
af9b897e
AC
1176 * @driver: the tty driver in use
1177 * @index: the minor number
1178 * @p: output buffer of at least 7 bytes
1179 *
1180 * Generate a name from a driver reference and write it to the output
1181 * buffer.
1182 *
1183 * Locking: None
1184 */
1185static void tty_line_name(struct tty_driver *driver, int index, char *p)
1da177e4
LT
1186{
1187 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1188}
1189
99f1fe18
AC
1190/**
1191 * tty_driver_lookup_tty() - find an existing tty, if any
1192 * @driver: the driver for the tty
1193 * @idx: the minor number
23499705 1194 *
99f1fe18 1195 * Return the tty, if found or ERR_PTR() otherwise.
23499705 1196 *
99f1fe18
AC
1197 * Locking: tty_mutex must be held. If tty is found, the mutex must
1198 * be held until the 'fast-open' is also done. Will change once we
1199 * have refcounting in the driver and per driver locking
23499705 1200 */
a47d545f 1201static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
15f1a633 1202 struct inode *inode, int idx)
23499705
SB
1203{
1204 struct tty_struct *tty;
1205
99f1fe18 1206 if (driver->ops->lookup)
15f1a633 1207 return driver->ops->lookup(driver, inode, idx);
23499705 1208
8b0a88d5 1209 tty = driver->ttys[idx];
23499705
SB
1210 return tty;
1211}
1212
bf970ee4
AC
1213/**
1214 * tty_init_termios - helper for termios setup
1215 * @tty: the tty to set up
1216 *
1217 * Initialise the termios structures for this tty. Thus runs under
1218 * the tty_mutex currently so we can be relaxed about ordering.
1219 */
1220
1221int tty_init_termios(struct tty_struct *tty)
1222{
fe6e29fd 1223 struct ktermios *tp;
bf970ee4
AC
1224 int idx = tty->index;
1225
1226 tp = tty->driver->termios[idx];
bf970ee4 1227 if (tp == NULL) {
fe6e29fd
AC
1228 tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
1229 if (tp == NULL)
bf970ee4 1230 return -ENOMEM;
bf970ee4
AC
1231 memcpy(tp, &tty->driver->init_termios,
1232 sizeof(struct ktermios));
1233 tty->driver->termios[idx] = tp;
bf970ee4
AC
1234 }
1235 tty->termios = tp;
fe6e29fd 1236 tty->termios_locked = tp + 1;
bf970ee4
AC
1237
1238 /* Compatibility until drivers always set this */
1239 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1240 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1241 return 0;
1242}
fe1ae7fd 1243EXPORT_SYMBOL_GPL(tty_init_termios);
bf970ee4 1244
99f1fe18 1245/**
8b0a88d5
AC
1246 * tty_driver_install_tty() - install a tty entry in the driver
1247 * @driver: the driver for the tty
1248 * @tty: the tty
1249 *
1250 * Install a tty object into the driver tables. The tty->index field
bf970ee4
AC
1251 * will be set by the time this is called. This method is responsible
1252 * for ensuring any need additional structures are allocated and
1253 * configured.
8b0a88d5
AC
1254 *
1255 * Locking: tty_mutex for now
1256 */
1257static int tty_driver_install_tty(struct tty_driver *driver,
1258 struct tty_struct *tty)
1259{
bf970ee4 1260 int idx = tty->index;
eeb89d91 1261 int ret;
bf970ee4 1262
eeb89d91 1263 if (driver->ops->install) {
eeb89d91 1264 ret = driver->ops->install(driver, tty);
eeb89d91
AC
1265 return ret;
1266 }
bf970ee4
AC
1267
1268 if (tty_init_termios(tty) == 0) {
1269 tty_driver_kref_get(driver);
1270 tty->count++;
1271 driver->ttys[idx] = tty;
1272 return 0;
1273 }
1274 return -ENOMEM;
8b0a88d5
AC
1275}
1276
1277/**
1278 * tty_driver_remove_tty() - remove a tty from the driver tables
1279 * @driver: the driver for the tty
1280 * @idx: the minor number
1281 *
1282 * Remvoe a tty object from the driver tables. The tty->index field
1283 * will be set by the time this is called.
1284 *
1285 * Locking: tty_mutex for now
1286 */
1287static void tty_driver_remove_tty(struct tty_driver *driver,
1288 struct tty_struct *tty)
1289{
1290 if (driver->ops->remove)
1291 driver->ops->remove(driver, tty);
1292 else
1293 driver->ttys[tty->index] = NULL;
1294}
1295
1296/*
1297 * tty_reopen() - fast re-open of an open tty
1298 * @tty - the tty to open
23499705 1299 *
99f1fe18 1300 * Return 0 on success, -errno on error.
23499705 1301 *
99f1fe18
AC
1302 * Locking: tty_mutex must be held from the time the tty was found
1303 * till this open completes.
23499705 1304 */
99f1fe18 1305static int tty_reopen(struct tty_struct *tty)
23499705
SB
1306{
1307 struct tty_driver *driver = tty->driver;
1308
1309 if (test_bit(TTY_CLOSING, &tty->flags))
1310 return -EIO;
1311
1312 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1313 driver->subtype == PTY_TYPE_MASTER) {
1314 /*
1315 * special case for PTY masters: only one open permitted,
1316 * and the slave side open count is incremented as well.
1317 */
1318 if (tty->count)
1319 return -EIO;
1320
1321 tty->link->count++;
1322 }
1323 tty->count++;
1324 tty->driver = driver; /* N.B. why do this every time?? */
1325
1aa4bed8 1326 mutex_lock(&tty->ldisc_mutex);
99f1fe18 1327 WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
1aa4bed8 1328 mutex_unlock(&tty->ldisc_mutex);
23499705
SB
1329
1330 return 0;
1331}
1332
af9b897e 1333/**
d81ed103 1334 * tty_init_dev - initialise a tty device
af9b897e
AC
1335 * @driver: tty driver we are opening a device on
1336 * @idx: device index
15582d36
AC
1337 * @ret_tty: returned tty structure
1338 * @first_ok: ok to open a new device (used by ptmx)
af9b897e
AC
1339 *
1340 * Prepare a tty device. This may not be a "new" clean device but
1341 * could also be an active device. The pty drivers require special
1342 * handling because of this.
1343 *
1344 * Locking:
1345 * The function is called under the tty_mutex, which
1346 * protects us from the tty struct or driver itself going away.
1347 *
1348 * On exit the tty device has the line discipline attached and
1349 * a reference count of 1. If a pair was created for pty/tty use
1350 * and the other was a pty master then it too has a reference count of 1.
1351 *
1da177e4 1352 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
70522e12
IM
1353 * failed open. The new code protects the open with a mutex, so it's
1354 * really quite straightforward. The mutex locking can probably be
1da177e4
LT
1355 * relaxed for the (most common) case of reopening a tty.
1356 */
af9b897e 1357
73ec06fc
AC
1358struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1359 int first_ok)
1da177e4 1360{
bf970ee4 1361 struct tty_struct *tty;
73ec06fc 1362 int retval;
1da177e4 1363
23499705 1364 /* Check if pty master is being opened multiple times */
15582d36 1365 if (driver->subtype == PTY_TYPE_MASTER &&
eeb89d91 1366 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
73ec06fc 1367 return ERR_PTR(-EIO);
eeb89d91 1368 }
73ec06fc 1369
1da177e4
LT
1370 /*
1371 * First time open is complex, especially for PTY devices.
1372 * This code guarantees that either everything succeeds and the
1373 * TTY is ready for operation, or else the table slots are vacated
37bdfb07 1374 * and the allocated memory released. (Except that the termios
1da177e4
LT
1375 * and locked termios may be retained.)
1376 */
1377
73ec06fc
AC
1378 if (!try_module_get(driver->owner))
1379 return ERR_PTR(-ENODEV);
1da177e4 1380
1da177e4 1381 tty = alloc_tty_struct();
37bdfb07 1382 if (!tty)
1da177e4 1383 goto fail_no_mem;
bf970ee4 1384 initialize_tty_struct(tty, driver, idx);
1da177e4 1385
73ec06fc 1386 retval = tty_driver_install_tty(driver, tty);
bf970ee4
AC
1387 if (retval < 0) {
1388 free_tty_struct(tty);
1389 module_put(driver->owner);
1390 return ERR_PTR(retval);
1391 }
8b0a88d5 1392
37bdfb07 1393 /*
1da177e4 1394 * Structures all installed ... call the ldisc open routines.
d5698c28
CH
1395 * If we fail here just call release_tty to clean up. No need
1396 * to decrement the use counts, as release_tty doesn't care.
1da177e4 1397 */
bf970ee4 1398 retval = tty_ldisc_setup(tty, tty->link);
01e1abb2
AC
1399 if (retval)
1400 goto release_mem_out;
73ec06fc 1401 return tty;
1da177e4 1402
1da177e4
LT
1403fail_no_mem:
1404 module_put(driver->owner);
73ec06fc 1405 return ERR_PTR(-ENOMEM);
1da177e4 1406
d5698c28 1407 /* call the tty release_tty routine to clean out this slot */
1da177e4 1408release_mem_out:
4050914f 1409 if (printk_ratelimit())
d81ed103 1410 printk(KERN_INFO "tty_init_dev: ldisc open failed, "
4050914f 1411 "clearing slot %d\n", idx);
d5698c28 1412 release_tty(tty, idx);
73ec06fc 1413 return ERR_PTR(retval);
1da177e4
LT
1414}
1415
feebed65
AC
1416void tty_free_termios(struct tty_struct *tty)
1417{
1418 struct ktermios *tp;
1419 int idx = tty->index;
1420 /* Kill this flag and push into drivers for locking etc */
1421 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1422 /* FIXME: Locking on ->termios array */
1423 tp = tty->termios;
1424 tty->driver->termios[idx] = NULL;
1425 kfree(tp);
feebed65
AC
1426 }
1427}
1428EXPORT_SYMBOL(tty_free_termios);
1429
1430void tty_shutdown(struct tty_struct *tty)
1431{
8b0a88d5 1432 tty_driver_remove_tty(tty->driver, tty);
feebed65
AC
1433 tty_free_termios(tty);
1434}
1435EXPORT_SYMBOL(tty_shutdown);
1436
af9b897e 1437/**
d5698c28 1438 * release_one_tty - release tty structure memory
9c9f4ded 1439 * @kref: kref of tty we are obliterating
af9b897e
AC
1440 *
1441 * Releases memory associated with a tty structure, and clears out the
1442 * driver table slots. This function is called when a device is no longer
1443 * in use. It also gets called when setup of a device fails.
1444 *
1445 * Locking:
1446 * tty_mutex - sometimes only
1447 * takes the file list lock internally when working on the list
1448 * of ttys that the driver keeps.
b50989dc
AC
1449 *
1450 * This method gets called from a work queue so that the driver private
f278a2f7 1451 * cleanup ops can sleep (needed for USB at least)
1da177e4 1452 */
b50989dc 1453static void release_one_tty(struct work_struct *work)
1da177e4 1454{
b50989dc
AC
1455 struct tty_struct *tty =
1456 container_of(work, struct tty_struct, hangup_work);
6f967f78 1457 struct tty_driver *driver = tty->driver;
d5698c28 1458
f278a2f7
DY
1459 if (tty->ops->cleanup)
1460 tty->ops->cleanup(tty);
1461
1da177e4 1462 tty->magic = 0;
7d7b93c1 1463 tty_driver_kref_put(driver);
6f967f78 1464 module_put(driver->owner);
d5698c28 1465
ee2ffa0d 1466 spin_lock(&tty_files_lock);
1da177e4 1467 list_del_init(&tty->tty_files);
ee2ffa0d 1468 spin_unlock(&tty_files_lock);
d5698c28 1469
6da8d866
ON
1470 put_pid(tty->pgrp);
1471 put_pid(tty->session);
1da177e4
LT
1472 free_tty_struct(tty);
1473}
1474
b50989dc
AC
1475static void queue_release_one_tty(struct kref *kref)
1476{
1477 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
f278a2f7
DY
1478
1479 if (tty->ops->shutdown)
1480 tty->ops->shutdown(tty);
1481 else
1482 tty_shutdown(tty);
1483
b50989dc
AC
1484 /* The hangup queue is now free so we can reuse it rather than
1485 waste a chunk of memory for each port */
1486 INIT_WORK(&tty->hangup_work, release_one_tty);
1487 schedule_work(&tty->hangup_work);
1488}
1489
9c9f4ded
AC
1490/**
1491 * tty_kref_put - release a tty kref
1492 * @tty: tty device
1493 *
1494 * Release a reference to a tty device and if need be let the kref
1495 * layer destruct the object for us
1496 */
1497
1498void tty_kref_put(struct tty_struct *tty)
1499{
1500 if (tty)
b50989dc 1501 kref_put(&tty->kref, queue_release_one_tty);
9c9f4ded
AC
1502}
1503EXPORT_SYMBOL(tty_kref_put);
1504
d5698c28
CH
1505/**
1506 * release_tty - release tty structure memory
1507 *
1508 * Release both @tty and a possible linked partner (think pty pair),
1509 * and decrement the refcount of the backing module.
1510 *
1511 * Locking:
1512 * tty_mutex - sometimes only
1513 * takes the file list lock internally when working on the list
1514 * of ttys that the driver keeps.
1515 * FIXME: should we require tty_mutex is held here ??
9c9f4ded 1516 *
d5698c28
CH
1517 */
1518static void release_tty(struct tty_struct *tty, int idx)
1519{
9c9f4ded
AC
1520 /* This should always be true but check for the moment */
1521 WARN_ON(tty->index != idx);
1522
d5698c28 1523 if (tty->link)
9c9f4ded
AC
1524 tty_kref_put(tty->link);
1525 tty_kref_put(tty);
d5698c28
CH
1526}
1527
eeb89d91
AC
1528/**
1529 * tty_release - vfs callback for close
1530 * @inode: inode of tty
1531 * @filp: file pointer for handle to tty
1532 *
1533 * Called the last time each file handle is closed that references
1534 * this tty. There may however be several such references.
1535 *
1536 * Locking:
1537 * Takes bkl. See tty_release_dev
1538 *
1da177e4
LT
1539 * Even releasing the tty structures is a tricky business.. We have
1540 * to be very careful that the structures are all released at the
1541 * same time, as interrupts might otherwise get the wrong pointers.
1542 *
1543 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1544 * lead to double frees or releasing memory still in use.
1545 */
eeb89d91
AC
1546
1547int tty_release(struct inode *inode, struct file *filp)
1da177e4 1548{
d996b62a
NP
1549 struct tty_struct *tty = file_tty(filp);
1550 struct tty_struct *o_tty;
1da177e4 1551 int pty_master, tty_closing, o_tty_closing, do_sleep;
14a6283e 1552 int devpts;
1da177e4
LT
1553 int idx;
1554 char buf[64];
37bdfb07 1555
15f1a633 1556 if (tty_paranoia_check(tty, inode, "tty_release_dev"))
eeb89d91 1557 return 0;
1da177e4 1558
ec79d605 1559 tty_lock();
d81ed103 1560 check_tty_count(tty, "tty_release_dev");
1da177e4 1561
ec79d605 1562 __tty_fasync(-1, filp, 0);
1da177e4
LT
1563
1564 idx = tty->index;
1565 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1566 tty->driver->subtype == PTY_TYPE_MASTER);
1567 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1da177e4
LT
1568 o_tty = tty->link;
1569
1570#ifdef TTY_PARANOIA_CHECK
1571 if (idx < 0 || idx >= tty->driver->num) {
d81ed103 1572 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
1da177e4 1573 "free (%s)\n", tty->name);
ec79d605 1574 tty_unlock();
eeb89d91 1575 return 0;
1da177e4 1576 }
8b0a88d5 1577 if (!devpts) {
1da177e4 1578 if (tty != tty->driver->ttys[idx]) {
ec79d605 1579 tty_unlock();
d81ed103 1580 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
1da177e4 1581 "for (%s)\n", idx, tty->name);
eeb89d91 1582 return 0;
1da177e4
LT
1583 }
1584 if (tty->termios != tty->driver->termios[idx]) {
ec79d605 1585 tty_unlock();
d81ed103 1586 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
1da177e4
LT
1587 "for (%s)\n",
1588 idx, tty->name);
eeb89d91 1589 return 0;
1da177e4 1590 }
1da177e4
LT
1591 }
1592#endif
1593
1594#ifdef TTY_DEBUG_HANGUP
d81ed103 1595 printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...",
1da177e4
LT
1596 tty_name(tty, buf), tty->count);
1597#endif
1598
1599#ifdef TTY_PARANOIA_CHECK
1600 if (tty->driver->other &&
1601 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1602 if (o_tty != tty->driver->other->ttys[idx]) {
ec79d605 1603 tty_unlock();
d81ed103 1604 printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
1da177e4
LT
1605 "not o_tty for (%s)\n",
1606 idx, tty->name);
eeb89d91 1607 return 0 ;
1da177e4
LT
1608 }
1609 if (o_tty->termios != tty->driver->other->termios[idx]) {
ec79d605 1610 tty_unlock();
d81ed103 1611 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
1da177e4
LT
1612 "not o_termios for (%s)\n",
1613 idx, tty->name);
eeb89d91 1614 return 0;
1da177e4 1615 }
1da177e4 1616 if (o_tty->link != tty) {
ec79d605 1617 tty_unlock();
d81ed103 1618 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
eeb89d91 1619 return 0;
1da177e4
LT
1620 }
1621 }
1622#endif
f34d7a5b
AC
1623 if (tty->ops->close)
1624 tty->ops->close(tty, filp);
1da177e4 1625
ec79d605 1626 tty_unlock();
1da177e4
LT
1627 /*
1628 * Sanity check: if tty->count is going to zero, there shouldn't be
1629 * any waiters on tty->read_wait or tty->write_wait. We test the
1630 * wait queues and kick everyone out _before_ actually starting to
1631 * close. This ensures that we won't block while releasing the tty
1632 * structure.
1633 *
1634 * The test for the o_tty closing is necessary, since the master and
1635 * slave sides may close in any order. If the slave side closes out
1636 * first, its count will be one, since the master side holds an open.
1637 * Thus this test wouldn't be triggered at the time the slave closes,
1638 * so we do it now.
1639 *
1640 * Note that it's possible for the tty to be opened again while we're
1641 * flushing out waiters. By recalculating the closing flags before
1642 * each iteration we avoid any problems.
1643 */
1644 while (1) {
1645 /* Guard against races with tty->count changes elsewhere and
1646 opens on /dev/tty */
37bdfb07 1647
70522e12 1648 mutex_lock(&tty_mutex);
ec79d605 1649 tty_lock();
1da177e4
LT
1650 tty_closing = tty->count <= 1;
1651 o_tty_closing = o_tty &&
1652 (o_tty->count <= (pty_master ? 1 : 0));
1da177e4
LT
1653 do_sleep = 0;
1654
1655 if (tty_closing) {
1656 if (waitqueue_active(&tty->read_wait)) {
4b19449d 1657 wake_up_poll(&tty->read_wait, POLLIN);
1da177e4
LT
1658 do_sleep++;
1659 }
1660 if (waitqueue_active(&tty->write_wait)) {
4b19449d 1661 wake_up_poll(&tty->write_wait, POLLOUT);
1da177e4
LT
1662 do_sleep++;
1663 }
1664 }
1665 if (o_tty_closing) {
1666 if (waitqueue_active(&o_tty->read_wait)) {
4b19449d 1667 wake_up_poll(&o_tty->read_wait, POLLIN);
1da177e4
LT
1668 do_sleep++;
1669 }
1670 if (waitqueue_active(&o_tty->write_wait)) {
4b19449d 1671 wake_up_poll(&o_tty->write_wait, POLLOUT);
1da177e4
LT
1672 do_sleep++;
1673 }
1674 }
1675 if (!do_sleep)
1676 break;
1677
d81ed103 1678 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
1da177e4 1679 "active!\n", tty_name(tty, buf));
ec79d605 1680 tty_unlock();
70522e12 1681 mutex_unlock(&tty_mutex);
1da177e4 1682 schedule();
37bdfb07 1683 }
1da177e4
LT
1684
1685 /*
37bdfb07
AC
1686 * The closing flags are now consistent with the open counts on
1687 * both sides, and we've completed the last operation that could
1da177e4
LT
1688 * block, so it's safe to proceed with closing.
1689 */
1da177e4
LT
1690 if (pty_master) {
1691 if (--o_tty->count < 0) {
d81ed103 1692 printk(KERN_WARNING "tty_release_dev: bad pty slave count "
1da177e4
LT
1693 "(%d) for %s\n",
1694 o_tty->count, tty_name(o_tty, buf));
1695 o_tty->count = 0;
1696 }
1697 }
1698 if (--tty->count < 0) {
d81ed103 1699 printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n",
1da177e4
LT
1700 tty->count, tty_name(tty, buf));
1701 tty->count = 0;
1702 }
37bdfb07 1703
1da177e4
LT
1704 /*
1705 * We've decremented tty->count, so we need to remove this file
1706 * descriptor off the tty->tty_files list; this serves two
1707 * purposes:
1708 * - check_tty_count sees the correct number of file descriptors
1709 * associated with this tty.
1710 * - do_tty_hangup no longer sees this file descriptor as
1711 * something that needs to be handled for hangups.
1712 */
d996b62a 1713 tty_del_file(filp);
1da177e4
LT
1714
1715 /*
1716 * Perform some housekeeping before deciding whether to return.
1717 *
1718 * Set the TTY_CLOSING flag if this was the last open. In the
1719 * case of a pty we may have to wait around for the other side
1720 * to close, and TTY_CLOSING makes sure we can't be reopened.
1721 */
37bdfb07 1722 if (tty_closing)
1da177e4 1723 set_bit(TTY_CLOSING, &tty->flags);
37bdfb07 1724 if (o_tty_closing)
1da177e4
LT
1725 set_bit(TTY_CLOSING, &o_tty->flags);
1726
1727 /*
1728 * If _either_ side is closing, make sure there aren't any
1729 * processes that still think tty or o_tty is their controlling
1730 * tty.
1731 */
1732 if (tty_closing || o_tty_closing) {
1da177e4 1733 read_lock(&tasklist_lock);
24ec839c 1734 session_clear_tty(tty->session);
1da177e4 1735 if (o_tty)
24ec839c 1736 session_clear_tty(o_tty->session);
1da177e4
LT
1737 read_unlock(&tasklist_lock);
1738 }
1739
70522e12 1740 mutex_unlock(&tty_mutex);
da965822 1741
1da177e4 1742 /* check whether both sides are closing ... */
eeb89d91 1743 if (!tty_closing || (o_tty && !o_tty_closing)) {
ec79d605 1744 tty_unlock();
eeb89d91
AC
1745 return 0;
1746 }
37bdfb07 1747
1da177e4
LT
1748#ifdef TTY_DEBUG_HANGUP
1749 printk(KERN_DEBUG "freeing tty structure...");
1750#endif
1751 /*
01e1abb2 1752 * Ask the line discipline code to release its structures
1da177e4 1753 */
01e1abb2 1754 tty_ldisc_release(tty, o_tty);
1da177e4 1755 /*
d5698c28 1756 * The release_tty function takes care of the details of clearing
1da177e4
LT
1757 * the slots and preserving the termios structure.
1758 */
d5698c28 1759 release_tty(tty, idx);
1da177e4 1760
1da177e4 1761 /* Make this pty number available for reallocation */
718a9163 1762 if (devpts)
15f1a633 1763 devpts_kill_index(inode, idx);
ec79d605 1764 tty_unlock();
eeb89d91 1765 return 0;
1da177e4
LT
1766}
1767
af9b897e 1768/**
eeb89d91 1769 * tty_open - open a tty device
af9b897e
AC
1770 * @inode: inode of device file
1771 * @filp: file pointer to tty
1da177e4 1772 *
af9b897e
AC
1773 * tty_open and tty_release keep up the tty count that contains the
1774 * number of opens done on a tty. We cannot use the inode-count, as
1775 * different inodes might point to the same tty.
1da177e4 1776 *
af9b897e
AC
1777 * Open-counting is needed for pty masters, as well as for keeping
1778 * track of serial lines: DTR is dropped when the last close happens.
1779 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1780 *
1781 * The termios state of a pty is reset on first open so that
1782 * settings don't persist across reuse.
1783 *
d81ed103 1784 * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
24ec839c
PZ
1785 * tty->count should protect the rest.
1786 * ->siglock protects ->signal/->sighand
1da177e4 1787 */
af9b897e 1788
eeb89d91 1789static int tty_open(struct inode *inode, struct file *filp)
1da177e4 1790{
4a2b5fdd 1791 struct tty_struct *tty = NULL;
1da177e4
LT
1792 int noctty, retval;
1793 struct tty_driver *driver;
1794 int index;
1795 dev_t device = inode->i_rdev;
846c151a 1796 unsigned saved_flags = filp->f_flags;
1da177e4
LT
1797
1798 nonseekable_open(inode, filp);
37bdfb07 1799
1da177e4
LT
1800retry_open:
1801 noctty = filp->f_flags & O_NOCTTY;
1802 index = -1;
1803 retval = 0;
37bdfb07 1804
70522e12 1805 mutex_lock(&tty_mutex);
ec79d605 1806 tty_lock();
1da177e4 1807
37bdfb07 1808 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
24ec839c
PZ
1809 tty = get_current_tty();
1810 if (!tty) {
ec79d605 1811 tty_unlock();
70522e12 1812 mutex_unlock(&tty_mutex);
1da177e4
LT
1813 return -ENXIO;
1814 }
7d7b93c1 1815 driver = tty_driver_kref_get(tty->driver);
24ec839c 1816 index = tty->index;
1da177e4
LT
1817 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1818 /* noctty = 1; */
452a00d2
AC
1819 /* FIXME: Should we take a driver reference ? */
1820 tty_kref_put(tty);
1da177e4
LT
1821 goto got_driver;
1822 }
1823#ifdef CONFIG_VT
37bdfb07 1824 if (device == MKDEV(TTY_MAJOR, 0)) {
1da177e4 1825 extern struct tty_driver *console_driver;
7d7b93c1 1826 driver = tty_driver_kref_get(console_driver);
1da177e4
LT
1827 index = fg_console;
1828 noctty = 1;
1829 goto got_driver;
1830 }
1831#endif
37bdfb07 1832 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
296fa7f6
WN
1833 struct tty_driver *console_driver = console_device(&index);
1834 if (console_driver) {
1835 driver = tty_driver_kref_get(console_driver);
1836 if (driver) {
1837 /* Don't let /dev/console block */
1838 filp->f_flags |= O_NONBLOCK;
1839 noctty = 1;
1840 goto got_driver;
1841 }
1da177e4 1842 }
ec79d605 1843 tty_unlock();
70522e12 1844 mutex_unlock(&tty_mutex);
1da177e4
LT
1845 return -ENODEV;
1846 }
1847
1848 driver = get_tty_driver(device, &index);
1849 if (!driver) {
ec79d605 1850 tty_unlock();
70522e12 1851 mutex_unlock(&tty_mutex);
1da177e4
LT
1852 return -ENODEV;
1853 }
1854got_driver:
4a2b5fdd
SB
1855 if (!tty) {
1856 /* check whether we're reopening an existing tty */
15f1a633 1857 tty = tty_driver_lookup_tty(driver, inode, index);
4a2b5fdd 1858
808ffa3d 1859 if (IS_ERR(tty)) {
ec79d605 1860 tty_unlock();
808ffa3d 1861 mutex_unlock(&tty_mutex);
4a2b5fdd 1862 return PTR_ERR(tty);
808ffa3d 1863 }
4a2b5fdd
SB
1864 }
1865
1866 if (tty) {
1867 retval = tty_reopen(tty);
1868 if (retval)
1869 tty = ERR_PTR(retval);
1870 } else
1871 tty = tty_init_dev(driver, index, 0);
1872
70522e12 1873 mutex_unlock(&tty_mutex);
7d7b93c1 1874 tty_driver_kref_put(driver);
eeb89d91 1875 if (IS_ERR(tty)) {
ec79d605 1876 tty_unlock();
73ec06fc 1877 return PTR_ERR(tty);
eeb89d91 1878 }
1da177e4 1879
d996b62a
NP
1880 tty_add_file(tty, filp);
1881
1da177e4
LT
1882 check_tty_count(tty, "tty_open");
1883 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1884 tty->driver->subtype == PTY_TYPE_MASTER)
1885 noctty = 1;
1886#ifdef TTY_DEBUG_HANGUP
1887 printk(KERN_DEBUG "opening %s...", tty->name);
1888#endif
1889 if (!retval) {
f34d7a5b
AC
1890 if (tty->ops->open)
1891 retval = tty->ops->open(tty, filp);
1da177e4
LT
1892 else
1893 retval = -ENODEV;
1894 }
1895 filp->f_flags = saved_flags;
1896
37bdfb07
AC
1897 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1898 !capable(CAP_SYS_ADMIN))
1da177e4
LT
1899 retval = -EBUSY;
1900
1901 if (retval) {
1902#ifdef TTY_DEBUG_HANGUP
1903 printk(KERN_DEBUG "error %d in opening %s...", retval,
1904 tty->name);
1905#endif
64ba3dc3 1906 tty_unlock(); /* need to call tty_release without BTM */
eeb89d91 1907 tty_release(inode, filp);
64ba3dc3 1908 if (retval != -ERESTARTSYS)
1da177e4 1909 return retval;
64ba3dc3
AB
1910
1911 if (signal_pending(current))
1da177e4 1912 return retval;
64ba3dc3 1913
1da177e4
LT
1914 schedule();
1915 /*
1916 * Need to reset f_op in case a hangup happened.
1917 */
64ba3dc3 1918 tty_lock();
1da177e4
LT
1919 if (filp->f_op == &hung_up_tty_fops)
1920 filp->f_op = &tty_fops;
ec79d605 1921 tty_unlock();
1da177e4
LT
1922 goto retry_open;
1923 }
ec79d605 1924 tty_unlock();
eeb89d91 1925
24ec839c
PZ
1926
1927 mutex_lock(&tty_mutex);
ec79d605 1928 tty_lock();
24ec839c 1929 spin_lock_irq(&current->sighand->siglock);
1da177e4
LT
1930 if (!noctty &&
1931 current->signal->leader &&
1932 !current->signal->tty &&
ab521dc0 1933 tty->session == NULL)
2a65f1d9 1934 __proc_set_tty(current, tty);
24ec839c 1935 spin_unlock_irq(&current->sighand->siglock);
ec79d605 1936 tty_unlock();
24ec839c 1937 mutex_unlock(&tty_mutex);
1da177e4
LT
1938 return 0;
1939}
1940
39d95b9d
JC
1941
1942
af9b897e
AC
1943/**
1944 * tty_poll - check tty status
1945 * @filp: file being polled
1946 * @wait: poll wait structures to update
1947 *
1948 * Call the line discipline polling method to obtain the poll
1949 * status of the device.
1950 *
1951 * Locking: locks called line discipline but ldisc poll method
1952 * may be re-entered freely by other callers.
1953 */
1954
37bdfb07 1955static unsigned int tty_poll(struct file *filp, poll_table *wait)
1da177e4 1956{
d996b62a 1957 struct tty_struct *tty = file_tty(filp);
1da177e4
LT
1958 struct tty_ldisc *ld;
1959 int ret = 0;
1960
a7113a96 1961 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
1da177e4 1962 return 0;
37bdfb07 1963
1da177e4 1964 ld = tty_ldisc_ref_wait(tty);
a352def2
AC
1965 if (ld->ops->poll)
1966 ret = (ld->ops->poll)(tty, filp, wait);
1da177e4
LT
1967 tty_ldisc_deref(ld);
1968 return ret;
1969}
1970
ec79d605 1971static int __tty_fasync(int fd, struct file *filp, int on)
1da177e4 1972{
d996b62a 1973 struct tty_struct *tty = file_tty(filp);
47f86834 1974 unsigned long flags;
5d1e3230 1975 int retval = 0;
1da177e4 1976
a7113a96 1977 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
5d1e3230 1978 goto out;
37bdfb07 1979
1da177e4
LT
1980 retval = fasync_helper(fd, filp, on, &tty->fasync);
1981 if (retval <= 0)
5d1e3230 1982 goto out;
1da177e4
LT
1983
1984 if (on) {
ab521dc0
EB
1985 enum pid_type type;
1986 struct pid *pid;
1da177e4
LT
1987 if (!waitqueue_active(&tty->read_wait))
1988 tty->minimum_to_wake = 1;
47f86834 1989 spin_lock_irqsave(&tty->ctrl_lock, flags);
ab521dc0
EB
1990 if (tty->pgrp) {
1991 pid = tty->pgrp;
1992 type = PIDTYPE_PGID;
1993 } else {
1994 pid = task_pid(current);
1995 type = PIDTYPE_PID;
1996 }
80e1e823 1997 get_pid(pid);
70362511 1998 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
80e1e823
LT
1999 retval = __f_setown(filp, pid, type, 0);
2000 put_pid(pid);
1da177e4 2001 if (retval)
5d1e3230 2002 goto out;
1da177e4
LT
2003 } else {
2004 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2005 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2006 }
5d1e3230
JC
2007 retval = 0;
2008out:
ec79d605
AB
2009 return retval;
2010}
2011
2012static int tty_fasync(int fd, struct file *filp, int on)
2013{
2014 int retval;
2015 tty_lock();
2016 retval = __tty_fasync(fd, filp, on);
2017 tty_unlock();
5d1e3230 2018 return retval;
1da177e4
LT
2019}
2020
af9b897e
AC
2021/**
2022 * tiocsti - fake input character
2023 * @tty: tty to fake input into
2024 * @p: pointer to character
2025 *
3a4fa0a2 2026 * Fake input to a tty device. Does the necessary locking and
af9b897e
AC
2027 * input management.
2028 *
2029 * FIXME: does not honour flow control ??
2030 *
2031 * Locking:
2032 * Called functions take tty_ldisc_lock
2033 * current->signal->tty check is safe without locks
28298232
AC
2034 *
2035 * FIXME: may race normal receive processing
af9b897e
AC
2036 */
2037
1da177e4
LT
2038static int tiocsti(struct tty_struct *tty, char __user *p)
2039{
2040 char ch, mbz = 0;
2041 struct tty_ldisc *ld;
37bdfb07 2042
1da177e4
LT
2043 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2044 return -EPERM;
2045 if (get_user(ch, p))
2046 return -EFAULT;
1e641743 2047 tty_audit_tiocsti(tty, ch);
1da177e4 2048 ld = tty_ldisc_ref_wait(tty);
a352def2 2049 ld->ops->receive_buf(tty, &ch, &mbz, 1);
1da177e4
LT
2050 tty_ldisc_deref(ld);
2051 return 0;
2052}
2053
af9b897e
AC
2054/**
2055 * tiocgwinsz - implement window query ioctl
2056 * @tty; tty
2057 * @arg: user buffer for result
2058 *
808a0d38 2059 * Copies the kernel idea of the window size into the user buffer.
af9b897e 2060 *
24ec839c 2061 * Locking: tty->termios_mutex is taken to ensure the winsize data
808a0d38 2062 * is consistent.
af9b897e
AC
2063 */
2064
37bdfb07 2065static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
1da177e4 2066{
808a0d38
AC
2067 int err;
2068
5785c95b 2069 mutex_lock(&tty->termios_mutex);
808a0d38 2070 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
5785c95b 2071 mutex_unlock(&tty->termios_mutex);
808a0d38
AC
2072
2073 return err ? -EFAULT: 0;
1da177e4
LT
2074}
2075
af9b897e 2076/**
8c9a9dd0
AC
2077 * tty_do_resize - resize event
2078 * @tty: tty being resized
8c9a9dd0
AC
2079 * @rows: rows (character)
2080 * @cols: cols (character)
2081 *
3ad2f3fb 2082 * Update the termios variables and send the necessary signals to
8c9a9dd0 2083 * peform a terminal resize correctly
af9b897e
AC
2084 */
2085
fc6f6238 2086int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
1da177e4 2087{
fc6f6238 2088 struct pid *pgrp;
47f86834 2089 unsigned long flags;
1da177e4 2090
fc6f6238
AC
2091 /* Lock the tty */
2092 mutex_lock(&tty->termios_mutex);
2093 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
ca9bda00 2094 goto done;
47f86834
AC
2095 /* Get the PID values and reference them so we can
2096 avoid holding the tty ctrl lock while sending signals */
2097 spin_lock_irqsave(&tty->ctrl_lock, flags);
2098 pgrp = get_pid(tty->pgrp);
47f86834
AC
2099 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2100
2101 if (pgrp)
2102 kill_pgrp(pgrp, SIGWINCH, 1);
47f86834 2103 put_pid(pgrp);
47f86834 2104
8c9a9dd0 2105 tty->winsize = *ws;
ca9bda00 2106done:
fc6f6238 2107 mutex_unlock(&tty->termios_mutex);
1da177e4
LT
2108 return 0;
2109}
2110
8c9a9dd0
AC
2111/**
2112 * tiocswinsz - implement window size set ioctl
fc6f6238 2113 * @tty; tty side of tty
8c9a9dd0
AC
2114 * @arg: user buffer for result
2115 *
2116 * Copies the user idea of the window size to the kernel. Traditionally
2117 * this is just advisory information but for the Linux console it
2118 * actually has driver level meaning and triggers a VC resize.
2119 *
2120 * Locking:
2121 * Driver dependant. The default do_resize method takes the
2122 * tty termios mutex and ctrl_lock. The console takes its own lock
2123 * then calls into the default method.
2124 */
2125
fc6f6238 2126static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
8c9a9dd0
AC
2127{
2128 struct winsize tmp_ws;
2129 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2130 return -EFAULT;
2131
2132 if (tty->ops->resize)
fc6f6238 2133 return tty->ops->resize(tty, &tmp_ws);
8c9a9dd0 2134 else
fc6f6238 2135 return tty_do_resize(tty, &tmp_ws);
8c9a9dd0
AC
2136}
2137
af9b897e
AC
2138/**
2139 * tioccons - allow admin to move logical console
2140 * @file: the file to become console
2141 *
2142 * Allow the adminstrator to move the redirected console device
2143 *
2144 * Locking: uses redirect_lock to guard the redirect information
2145 */
2146
1da177e4
LT
2147static int tioccons(struct file *file)
2148{
2149 if (!capable(CAP_SYS_ADMIN))
2150 return -EPERM;
2151 if (file->f_op->write == redirected_tty_write) {
2152 struct file *f;
2153 spin_lock(&redirect_lock);
2154 f = redirect;
2155 redirect = NULL;
2156 spin_unlock(&redirect_lock);
2157 if (f)
2158 fput(f);
2159 return 0;
2160 }
2161 spin_lock(&redirect_lock);
2162 if (redirect) {
2163 spin_unlock(&redirect_lock);
2164 return -EBUSY;
2165 }
2166 get_file(file);
2167 redirect = file;
2168 spin_unlock(&redirect_lock);
2169 return 0;
2170}
2171
af9b897e
AC
2172/**
2173 * fionbio - non blocking ioctl
2174 * @file: file to set blocking value
2175 * @p: user parameter
2176 *
2177 * Historical tty interfaces had a blocking control ioctl before
2178 * the generic functionality existed. This piece of history is preserved
2179 * in the expected tty API of posix OS's.
2180 *
6146b9af 2181 * Locking: none, the open file handle ensures it won't go away.
af9b897e 2182 */
1da177e4
LT
2183
2184static int fionbio(struct file *file, int __user *p)
2185{
2186 int nonblock;
2187
2188 if (get_user(nonblock, p))
2189 return -EFAULT;
2190
db1dd4d3 2191 spin_lock(&file->f_lock);
1da177e4
LT
2192 if (nonblock)
2193 file->f_flags |= O_NONBLOCK;
2194 else
2195 file->f_flags &= ~O_NONBLOCK;
db1dd4d3 2196 spin_unlock(&file->f_lock);
1da177e4
LT
2197 return 0;
2198}
2199
af9b897e
AC
2200/**
2201 * tiocsctty - set controlling tty
2202 * @tty: tty structure
2203 * @arg: user argument
2204 *
2205 * This ioctl is used to manage job control. It permits a session
2206 * leader to set this tty as the controlling tty for the session.
2207 *
2208 * Locking:
28298232 2209 * Takes tty_mutex() to protect tty instance
24ec839c
PZ
2210 * Takes tasklist_lock internally to walk sessions
2211 * Takes ->siglock() when updating signal->tty
af9b897e
AC
2212 */
2213
1da177e4
LT
2214static int tiocsctty(struct tty_struct *tty, int arg)
2215{
24ec839c 2216 int ret = 0;
ab521dc0 2217 if (current->signal->leader && (task_session(current) == tty->session))
24ec839c
PZ
2218 return ret;
2219
2220 mutex_lock(&tty_mutex);
1da177e4
LT
2221 /*
2222 * The process must be a session leader and
2223 * not have a controlling tty already.
2224 */
24ec839c
PZ
2225 if (!current->signal->leader || current->signal->tty) {
2226 ret = -EPERM;
2227 goto unlock;
2228 }
2229
ab521dc0 2230 if (tty->session) {
1da177e4
LT
2231 /*
2232 * This tty is already the controlling
2233 * tty for another session group!
2234 */
37bdfb07 2235 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
1da177e4
LT
2236 /*
2237 * Steal it away
2238 */
1da177e4 2239 read_lock(&tasklist_lock);
24ec839c 2240 session_clear_tty(tty->session);
1da177e4 2241 read_unlock(&tasklist_lock);
24ec839c
PZ
2242 } else {
2243 ret = -EPERM;
2244 goto unlock;
2245 }
1da177e4 2246 }
24ec839c
PZ
2247 proc_set_tty(current, tty);
2248unlock:
28298232 2249 mutex_unlock(&tty_mutex);
24ec839c 2250 return ret;
1da177e4
LT
2251}
2252
5d0fdf1e
AC
2253/**
2254 * tty_get_pgrp - return a ref counted pgrp pid
2255 * @tty: tty to read
2256 *
2257 * Returns a refcounted instance of the pid struct for the process
2258 * group controlling the tty.
2259 */
2260
2261struct pid *tty_get_pgrp(struct tty_struct *tty)
2262{
2263 unsigned long flags;
2264 struct pid *pgrp;
2265
2266 spin_lock_irqsave(&tty->ctrl_lock, flags);
2267 pgrp = get_pid(tty->pgrp);
2268 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2269
2270 return pgrp;
2271}
2272EXPORT_SYMBOL_GPL(tty_get_pgrp);
2273
af9b897e
AC
2274/**
2275 * tiocgpgrp - get process group
2276 * @tty: tty passed by user
2277 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2278 * @p: returned pid
2279 *
2280 * Obtain the process group of the tty. If there is no process group
2281 * return an error.
2282 *
24ec839c 2283 * Locking: none. Reference to current->signal->tty is safe.
af9b897e
AC
2284 */
2285
1da177e4
LT
2286static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2287{
5d0fdf1e
AC
2288 struct pid *pid;
2289 int ret;
1da177e4
LT
2290 /*
2291 * (tty == real_tty) is a cheap way of
2292 * testing if the tty is NOT a master pty.
2293 */
2294 if (tty == real_tty && current->signal->tty != real_tty)
2295 return -ENOTTY;
5d0fdf1e
AC
2296 pid = tty_get_pgrp(real_tty);
2297 ret = put_user(pid_vnr(pid), p);
2298 put_pid(pid);
2299 return ret;
1da177e4
LT
2300}
2301
af9b897e
AC
2302/**
2303 * tiocspgrp - attempt to set process group
2304 * @tty: tty passed by user
2305 * @real_tty: tty side device matching tty passed by user
2306 * @p: pid pointer
2307 *
2308 * Set the process group of the tty to the session passed. Only
2309 * permitted where the tty session is our session.
2310 *
47f86834 2311 * Locking: RCU, ctrl lock
af9b897e
AC
2312 */
2313
1da177e4
LT
2314static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2315{
04a2e6a5
EB
2316 struct pid *pgrp;
2317 pid_t pgrp_nr;
1da177e4 2318 int retval = tty_check_change(real_tty);
47f86834 2319 unsigned long flags;
1da177e4
LT
2320
2321 if (retval == -EIO)
2322 return -ENOTTY;
2323 if (retval)
2324 return retval;
2325 if (!current->signal->tty ||
2326 (current->signal->tty != real_tty) ||
ab521dc0 2327 (real_tty->session != task_session(current)))
1da177e4 2328 return -ENOTTY;
04a2e6a5 2329 if (get_user(pgrp_nr, p))
1da177e4 2330 return -EFAULT;
04a2e6a5 2331 if (pgrp_nr < 0)
1da177e4 2332 return -EINVAL;
04a2e6a5 2333 rcu_read_lock();
b488893a 2334 pgrp = find_vpid(pgrp_nr);
04a2e6a5
EB
2335 retval = -ESRCH;
2336 if (!pgrp)
2337 goto out_unlock;
2338 retval = -EPERM;
2339 if (session_of_pgrp(pgrp) != task_session(current))
2340 goto out_unlock;
2341 retval = 0;
47f86834 2342 spin_lock_irqsave(&tty->ctrl_lock, flags);
ab521dc0
EB
2343 put_pid(real_tty->pgrp);
2344 real_tty->pgrp = get_pid(pgrp);
47f86834 2345 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
04a2e6a5
EB
2346out_unlock:
2347 rcu_read_unlock();
2348 return retval;
1da177e4
LT
2349}
2350
af9b897e
AC
2351/**
2352 * tiocgsid - get session id
2353 * @tty: tty passed by user
2354 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2355 * @p: pointer to returned session id
2356 *
2357 * Obtain the session id of the tty. If there is no session
2358 * return an error.
2359 *
24ec839c 2360 * Locking: none. Reference to current->signal->tty is safe.
af9b897e
AC
2361 */
2362
1da177e4
LT
2363static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2364{
2365 /*
2366 * (tty == real_tty) is a cheap way of
2367 * testing if the tty is NOT a master pty.
2368 */
2369 if (tty == real_tty && current->signal->tty != real_tty)
2370 return -ENOTTY;
ab521dc0 2371 if (!real_tty->session)
1da177e4 2372 return -ENOTTY;
b488893a 2373 return put_user(pid_vnr(real_tty->session), p);
1da177e4
LT
2374}
2375
af9b897e
AC
2376/**
2377 * tiocsetd - set line discipline
2378 * @tty: tty device
2379 * @p: pointer to user data
2380 *
2381 * Set the line discipline according to user request.
2382 *
2383 * Locking: see tty_set_ldisc, this function is just a helper
2384 */
2385
1da177e4
LT
2386static int tiocsetd(struct tty_struct *tty, int __user *p)
2387{
2388 int ldisc;
04f378b1 2389 int ret;
1da177e4
LT
2390
2391 if (get_user(ldisc, p))
2392 return -EFAULT;
04f378b1 2393
04f378b1 2394 ret = tty_set_ldisc(tty, ldisc);
04f378b1
AC
2395
2396 return ret;
1da177e4
LT
2397}
2398
af9b897e
AC
2399/**
2400 * send_break - performed time break
2401 * @tty: device to break on
2402 * @duration: timeout in mS
2403 *
2404 * Perform a timed break on hardware that lacks its own driver level
2405 * timed break functionality.
2406 *
2407 * Locking:
28298232 2408 * atomic_write_lock serializes
af9b897e 2409 *
af9b897e
AC
2410 */
2411
b20f3ae5 2412static int send_break(struct tty_struct *tty, unsigned int duration)
1da177e4 2413{
9e98966c
AC
2414 int retval;
2415
2416 if (tty->ops->break_ctl == NULL)
2417 return 0;
2418
2419 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2420 retval = tty->ops->break_ctl(tty, duration);
2421 else {
2422 /* Do the work ourselves */
2423 if (tty_write_lock(tty, 0) < 0)
2424 return -EINTR;
2425 retval = tty->ops->break_ctl(tty, -1);
2426 if (retval)
2427 goto out;
2428 if (!signal_pending(current))
2429 msleep_interruptible(duration);
2430 retval = tty->ops->break_ctl(tty, 0);
2431out:
2432 tty_write_unlock(tty);
2433 if (signal_pending(current))
2434 retval = -EINTR;
2435 }
2436 return retval;
1da177e4
LT
2437}
2438
af9b897e 2439/**
f34d7a5b 2440 * tty_tiocmget - get modem status
af9b897e
AC
2441 * @tty: tty device
2442 * @file: user file pointer
2443 * @p: pointer to result
2444 *
2445 * Obtain the modem status bits from the tty driver if the feature
2446 * is supported. Return -EINVAL if it is not available.
2447 *
2448 * Locking: none (up to the driver)
2449 */
2450
2451static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
1da177e4
LT
2452{
2453 int retval = -EINVAL;
2454
f34d7a5b
AC
2455 if (tty->ops->tiocmget) {
2456 retval = tty->ops->tiocmget(tty, file);
1da177e4
LT
2457
2458 if (retval >= 0)
2459 retval = put_user(retval, p);
2460 }
2461 return retval;
2462}
2463
af9b897e 2464/**
f34d7a5b 2465 * tty_tiocmset - set modem status
af9b897e
AC
2466 * @tty: tty device
2467 * @file: user file pointer
2468 * @cmd: command - clear bits, set bits or set all
2469 * @p: pointer to desired bits
2470 *
2471 * Set the modem status bits from the tty driver if the feature
2472 * is supported. Return -EINVAL if it is not available.
2473 *
2474 * Locking: none (up to the driver)
2475 */
2476
2477static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
1da177e4
LT
2478 unsigned __user *p)
2479{
ae677517
AC
2480 int retval;
2481 unsigned int set, clear, val;
1da177e4 2482
ae677517
AC
2483 if (tty->ops->tiocmset == NULL)
2484 return -EINVAL;
1da177e4 2485
ae677517
AC
2486 retval = get_user(val, p);
2487 if (retval)
2488 return retval;
2489 set = clear = 0;
2490 switch (cmd) {
2491 case TIOCMBIS:
2492 set = val;
2493 break;
2494 case TIOCMBIC:
2495 clear = val;
2496 break;
2497 case TIOCMSET:
2498 set = val;
2499 clear = ~val;
2500 break;
2501 }
2502 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2503 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2504 return tty->ops->tiocmset(tty, file, set, clear);
1da177e4
LT
2505}
2506
e8b70e7d
AC
2507struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2508{
2509 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2510 tty->driver->subtype == PTY_TYPE_MASTER)
2511 tty = tty->link;
2512 return tty;
2513}
2514EXPORT_SYMBOL(tty_pair_get_tty);
2515
2516struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
2517{
2518 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2519 tty->driver->subtype == PTY_TYPE_MASTER)
2520 return tty;
2521 return tty->link;
2522}
2523EXPORT_SYMBOL(tty_pair_get_pty);
2524
1da177e4
LT
2525/*
2526 * Split this up, as gcc can choke on it otherwise..
2527 */
04f378b1 2528long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4 2529{
d996b62a
NP
2530 struct tty_struct *tty = file_tty(file);
2531 struct tty_struct *real_tty;
1da177e4
LT
2532 void __user *p = (void __user *)arg;
2533 int retval;
2534 struct tty_ldisc *ld;
04f378b1 2535 struct inode *inode = file->f_dentry->d_inode;
37bdfb07 2536
1da177e4
LT
2537 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2538 return -EINVAL;
2539
e8b70e7d 2540 real_tty = tty_pair_get_tty(tty);
1da177e4
LT
2541
2542 /*
2543 * Factor out some common prep work
2544 */
2545 switch (cmd) {
2546 case TIOCSETD:
2547 case TIOCSBRK:
2548 case TIOCCBRK:
2549 case TCSBRK:
37bdfb07 2550 case TCSBRKP:
1da177e4
LT
2551 retval = tty_check_change(tty);
2552 if (retval)
2553 return retval;
2554 if (cmd != TIOCCBRK) {
2555 tty_wait_until_sent(tty, 0);
2556 if (signal_pending(current))
2557 return -EINTR;
2558 }
2559 break;
2560 }
2561
9e98966c
AC
2562 /*
2563 * Now do the stuff.
2564 */
1da177e4 2565 switch (cmd) {
37bdfb07
AC
2566 case TIOCSTI:
2567 return tiocsti(tty, p);
2568 case TIOCGWINSZ:
8f520021 2569 return tiocgwinsz(real_tty, p);
37bdfb07 2570 case TIOCSWINSZ:
fc6f6238 2571 return tiocswinsz(real_tty, p);
37bdfb07
AC
2572 case TIOCCONS:
2573 return real_tty != tty ? -EINVAL : tioccons(file);
2574 case FIONBIO:
2575 return fionbio(file, p);
2576 case TIOCEXCL:
2577 set_bit(TTY_EXCLUSIVE, &tty->flags);
2578 return 0;
2579 case TIOCNXCL:
2580 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2581 return 0;
2582 case TIOCNOTTY:
2583 if (current->signal->tty != tty)
2584 return -ENOTTY;
2585 no_tty();
2586 return 0;
2587 case TIOCSCTTY:
2588 return tiocsctty(tty, arg);
2589 case TIOCGPGRP:
2590 return tiocgpgrp(tty, real_tty, p);
2591 case TIOCSPGRP:
2592 return tiocspgrp(tty, real_tty, p);
2593 case TIOCGSID:
2594 return tiocgsid(tty, real_tty, p);
2595 case TIOCGETD:
c65c9bc3 2596 return put_user(tty->ldisc->ops->num, (int __user *)p);
37bdfb07
AC
2597 case TIOCSETD:
2598 return tiocsetd(tty, p);
37bdfb07
AC
2599 /*
2600 * Break handling
2601 */
2602 case TIOCSBRK: /* Turn break on, unconditionally */
f34d7a5b 2603 if (tty->ops->break_ctl)
9e98966c 2604 return tty->ops->break_ctl(tty, -1);
37bdfb07 2605 return 0;
37bdfb07 2606 case TIOCCBRK: /* Turn break off, unconditionally */
f34d7a5b 2607 if (tty->ops->break_ctl)
9e98966c 2608 return tty->ops->break_ctl(tty, 0);
37bdfb07
AC
2609 return 0;
2610 case TCSBRK: /* SVID version: non-zero arg --> no break */
2611 /* non-zero arg means wait for all output data
2612 * to be sent (performed above) but don't send break.
2613 * This is used by the tcdrain() termios function.
2614 */
2615 if (!arg)
2616 return send_break(tty, 250);
2617 return 0;
2618 case TCSBRKP: /* support for POSIX tcsendbreak() */
2619 return send_break(tty, arg ? arg*100 : 250);
2620
2621 case TIOCMGET:
2622 return tty_tiocmget(tty, file, p);
2623 case TIOCMSET:
2624 case TIOCMBIC:
2625 case TIOCMBIS:
2626 return tty_tiocmset(tty, file, cmd, p);
2627 case TCFLSH:
2628 switch (arg) {
2629 case TCIFLUSH:
2630 case TCIOFLUSH:
2631 /* flush tty buffer and allow ldisc to process ioctl */
2632 tty_buffer_flush(tty);
c5c34d48 2633 break;
37bdfb07
AC
2634 }
2635 break;
1da177e4 2636 }
f34d7a5b
AC
2637 if (tty->ops->ioctl) {
2638 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
1da177e4
LT
2639 if (retval != -ENOIOCTLCMD)
2640 return retval;
2641 }
2642 ld = tty_ldisc_ref_wait(tty);
2643 retval = -EINVAL;
a352def2
AC
2644 if (ld->ops->ioctl) {
2645 retval = ld->ops->ioctl(tty, file, cmd, arg);
1da177e4
LT
2646 if (retval == -ENOIOCTLCMD)
2647 retval = -EINVAL;
2648 }
2649 tty_ldisc_deref(ld);
2650 return retval;
2651}
2652
e10cc1df 2653#ifdef CONFIG_COMPAT
37bdfb07 2654static long tty_compat_ioctl(struct file *file, unsigned int cmd,
e10cc1df
PF
2655 unsigned long arg)
2656{
2657 struct inode *inode = file->f_dentry->d_inode;
d996b62a 2658 struct tty_struct *tty = file_tty(file);
e10cc1df
PF
2659 struct tty_ldisc *ld;
2660 int retval = -ENOIOCTLCMD;
2661
2662 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2663 return -EINVAL;
2664
f34d7a5b
AC
2665 if (tty->ops->compat_ioctl) {
2666 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
e10cc1df
PF
2667 if (retval != -ENOIOCTLCMD)
2668 return retval;
2669 }
2670
2671 ld = tty_ldisc_ref_wait(tty);
a352def2
AC
2672 if (ld->ops->compat_ioctl)
2673 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
e10cc1df
PF
2674 tty_ldisc_deref(ld);
2675
2676 return retval;
2677}
2678#endif
1da177e4
LT
2679
2680/*
2681 * This implements the "Secure Attention Key" --- the idea is to
2682 * prevent trojan horses by killing all processes associated with this
2683 * tty when the user hits the "Secure Attention Key". Required for
2684 * super-paranoid applications --- see the Orange Book for more details.
37bdfb07 2685 *
1da177e4
LT
2686 * This code could be nicer; ideally it should send a HUP, wait a few
2687 * seconds, then send a INT, and then a KILL signal. But you then
2688 * have to coordinate with the init process, since all processes associated
2689 * with the current tty must be dead before the new getty is allowed
2690 * to spawn.
2691 *
2692 * Now, if it would be correct ;-/ The current code has a nasty hole -
2693 * it doesn't catch files in flight. We may send the descriptor to ourselves
2694 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2695 *
2696 * Nasty bug: do_SAK is being called in interrupt context. This can
2697 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2698 */
8b6312f4 2699void __do_SAK(struct tty_struct *tty)
1da177e4
LT
2700{
2701#ifdef TTY_SOFT_SAK
2702 tty_hangup(tty);
2703#else
652486fb 2704 struct task_struct *g, *p;
ab521dc0 2705 struct pid *session;
1da177e4
LT
2706 int i;
2707 struct file *filp;
badf1662 2708 struct fdtable *fdt;
37bdfb07 2709
1da177e4
LT
2710 if (!tty)
2711 return;
24ec839c 2712 session = tty->session;
37bdfb07 2713
b3f13deb 2714 tty_ldisc_flush(tty);
1da177e4 2715
f34d7a5b 2716 tty_driver_flush_buffer(tty);
37bdfb07 2717
1da177e4 2718 read_lock(&tasklist_lock);
652486fb 2719 /* Kill the entire session */
ab521dc0 2720 do_each_pid_task(session, PIDTYPE_SID, p) {
652486fb 2721 printk(KERN_NOTICE "SAK: killed process %d"
1b0f7ffd 2722 " (%s): task_session(p)==tty->session\n",
ba25f9dc 2723 task_pid_nr(p), p->comm);
652486fb 2724 send_sig(SIGKILL, p, 1);
ab521dc0 2725 } while_each_pid_task(session, PIDTYPE_SID, p);
652486fb
EB
2726 /* Now kill any processes that happen to have the
2727 * tty open.
2728 */
2729 do_each_thread(g, p) {
2730 if (p->signal->tty == tty) {
1da177e4 2731 printk(KERN_NOTICE "SAK: killed process %d"
1b0f7ffd 2732 " (%s): task_session(p)==tty->session\n",
ba25f9dc 2733 task_pid_nr(p), p->comm);
1da177e4
LT
2734 send_sig(SIGKILL, p, 1);
2735 continue;
2736 }
2737 task_lock(p);
2738 if (p->files) {
ca99c1da
DS
2739 /*
2740 * We don't take a ref to the file, so we must
2741 * hold ->file_lock instead.
2742 */
2743 spin_lock(&p->files->file_lock);
badf1662 2744 fdt = files_fdtable(p->files);
37bdfb07 2745 for (i = 0; i < fdt->max_fds; i++) {
1da177e4
LT
2746 filp = fcheck_files(p->files, i);
2747 if (!filp)
2748 continue;
2749 if (filp->f_op->read == tty_read &&
d996b62a 2750 file_tty(filp) == tty) {
1da177e4
LT
2751 printk(KERN_NOTICE "SAK: killed process %d"
2752 " (%s): fd#%d opened to the tty\n",
ba25f9dc 2753 task_pid_nr(p), p->comm, i);
20ac9437 2754 force_sig(SIGKILL, p);
1da177e4
LT
2755 break;
2756 }
2757 }
ca99c1da 2758 spin_unlock(&p->files->file_lock);
1da177e4
LT
2759 }
2760 task_unlock(p);
652486fb 2761 } while_each_thread(g, p);
1da177e4
LT
2762 read_unlock(&tasklist_lock);
2763#endif
2764}
2765
8b6312f4
EB
2766static void do_SAK_work(struct work_struct *work)
2767{
2768 struct tty_struct *tty =
2769 container_of(work, struct tty_struct, SAK_work);
2770 __do_SAK(tty);
2771}
2772
1da177e4
LT
2773/*
2774 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2775 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2776 * the values which we write to it will be identical to the values which it
2777 * already has. --akpm
2778 */
2779void do_SAK(struct tty_struct *tty)
2780{
2781 if (!tty)
2782 return;
1da177e4
LT
2783 schedule_work(&tty->SAK_work);
2784}
2785
2786EXPORT_SYMBOL(do_SAK);
2787
30004ac9
DES
2788static int dev_match_devt(struct device *dev, void *data)
2789{
2790 dev_t *devt = data;
2791 return dev->devt == *devt;
2792}
2793
2794/* Must put_device() after it's unused! */
2795static struct device *tty_get_device(struct tty_struct *tty)
2796{
2797 dev_t devt = tty_devnum(tty);
2798 return class_find_device(tty_class, NULL, &devt, dev_match_devt);
2799}
2800
2801
af9b897e
AC
2802/**
2803 * initialize_tty_struct
2804 * @tty: tty to initialize
2805 *
2806 * This subroutine initializes a tty structure that has been newly
2807 * allocated.
2808 *
2809 * Locking: none - tty in question must not be exposed at this point
1da177e4 2810 */
af9b897e 2811
bf970ee4
AC
2812void initialize_tty_struct(struct tty_struct *tty,
2813 struct tty_driver *driver, int idx)
1da177e4
LT
2814{
2815 memset(tty, 0, sizeof(struct tty_struct));
9c9f4ded 2816 kref_init(&tty->kref);
1da177e4 2817 tty->magic = TTY_MAGIC;
01e1abb2 2818 tty_ldisc_init(tty);
ab521dc0
EB
2819 tty->session = NULL;
2820 tty->pgrp = NULL;
1da177e4 2821 tty->overrun_time = jiffies;
33f0f88f
AC
2822 tty->buf.head = tty->buf.tail = NULL;
2823 tty_buffer_init(tty);
5785c95b 2824 mutex_init(&tty->termios_mutex);
c65c9bc3 2825 mutex_init(&tty->ldisc_mutex);
1da177e4
LT
2826 init_waitqueue_head(&tty->write_wait);
2827 init_waitqueue_head(&tty->read_wait);
65f27f38 2828 INIT_WORK(&tty->hangup_work, do_tty_hangup);
70522e12
IM
2829 mutex_init(&tty->atomic_read_lock);
2830 mutex_init(&tty->atomic_write_lock);
a88a69c9
JP
2831 mutex_init(&tty->output_lock);
2832 mutex_init(&tty->echo_lock);
1da177e4 2833 spin_lock_init(&tty->read_lock);
04f378b1 2834 spin_lock_init(&tty->ctrl_lock);
1da177e4 2835 INIT_LIST_HEAD(&tty->tty_files);
7f1f86a0 2836 INIT_WORK(&tty->SAK_work, do_SAK_work);
bf970ee4
AC
2837
2838 tty->driver = driver;
2839 tty->ops = driver->ops;
2840 tty->index = idx;
2841 tty_line_name(driver, idx, tty->name);
30004ac9 2842 tty->dev = tty_get_device(tty);
1da177e4
LT
2843}
2844
f34d7a5b
AC
2845/**
2846 * tty_put_char - write one character to a tty
2847 * @tty: tty
2848 * @ch: character
2849 *
2850 * Write one byte to the tty using the provided put_char method
2851 * if present. Returns the number of characters successfully output.
2852 *
2853 * Note: the specific put_char operation in the driver layer may go
2854 * away soon. Don't call it directly, use this method
1da177e4 2855 */
af9b897e 2856
f34d7a5b 2857int tty_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4 2858{
f34d7a5b
AC
2859 if (tty->ops->put_char)
2860 return tty->ops->put_char(tty, ch);
2861 return tty->ops->write(tty, &ch, 1);
1da177e4 2862}
f34d7a5b
AC
2863EXPORT_SYMBOL_GPL(tty_put_char);
2864
d81ed103 2865struct class *tty_class;
1da177e4
LT
2866
2867/**
af9b897e
AC
2868 * tty_register_device - register a tty device
2869 * @driver: the tty driver that describes the tty device
2870 * @index: the index in the tty driver for this tty device
2871 * @device: a struct device that is associated with this tty device.
2872 * This field is optional, if there is no known struct device
2873 * for this tty device it can be set to NULL safely.
1da177e4 2874 *
01107d34
GKH
2875 * Returns a pointer to the struct device for this tty device
2876 * (or ERR_PTR(-EFOO) on error).
1cdcb6b4 2877 *
af9b897e
AC
2878 * This call is required to be made to register an individual tty device
2879 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2880 * that bit is not set, this function should not be called by a tty
2881 * driver.
2882 *
2883 * Locking: ??
1da177e4 2884 */
af9b897e 2885
01107d34
GKH
2886struct device *tty_register_device(struct tty_driver *driver, unsigned index,
2887 struct device *device)
1da177e4
LT
2888{
2889 char name[64];
2890 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2891
2892 if (index >= driver->num) {
2893 printk(KERN_ERR "Attempt to register invalid tty line number "
2894 " (%d).\n", index);
1cdcb6b4 2895 return ERR_PTR(-EINVAL);
1da177e4
LT
2896 }
2897
1da177e4
LT
2898 if (driver->type == TTY_DRIVER_TYPE_PTY)
2899 pty_line_name(driver, index, name);
2900 else
2901 tty_line_name(driver, index, name);
1cdcb6b4 2902
03457cd4 2903 return device_create(tty_class, device, dev, NULL, name);
1da177e4 2904}
7d7b93c1 2905EXPORT_SYMBOL(tty_register_device);
1da177e4
LT
2906
2907/**
af9b897e
AC
2908 * tty_unregister_device - unregister a tty device
2909 * @driver: the tty driver that describes the tty device
2910 * @index: the index in the tty driver for this tty device
1da177e4 2911 *
af9b897e
AC
2912 * If a tty device is registered with a call to tty_register_device() then
2913 * this function must be called when the tty device is gone.
2914 *
2915 * Locking: ??
1da177e4 2916 */
af9b897e 2917
1da177e4
LT
2918void tty_unregister_device(struct tty_driver *driver, unsigned index)
2919{
37bdfb07
AC
2920 device_destroy(tty_class,
2921 MKDEV(driver->major, driver->minor_start) + index);
1da177e4 2922}
1da177e4
LT
2923EXPORT_SYMBOL(tty_unregister_device);
2924
2925struct tty_driver *alloc_tty_driver(int lines)
2926{
2927 struct tty_driver *driver;
2928
506eb99a 2929 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
1da177e4 2930 if (driver) {
7d7b93c1 2931 kref_init(&driver->kref);
1da177e4
LT
2932 driver->magic = TTY_DRIVER_MAGIC;
2933 driver->num = lines;
2934 /* later we'll move allocation of tables here */
2935 }
2936 return driver;
2937}
7d7b93c1 2938EXPORT_SYMBOL(alloc_tty_driver);
1da177e4 2939
7d7b93c1 2940static void destruct_tty_driver(struct kref *kref)
1da177e4 2941{
7d7b93c1
AC
2942 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
2943 int i;
2944 struct ktermios *tp;
2945 void *p;
2946
2947 if (driver->flags & TTY_DRIVER_INSTALLED) {
2948 /*
2949 * Free the termios and termios_locked structures because
2950 * we don't want to get memory leaks when modular tty
2951 * drivers are removed from the kernel.
2952 */
2953 for (i = 0; i < driver->num; i++) {
2954 tp = driver->termios[i];
2955 if (tp) {
2956 driver->termios[i] = NULL;
2957 kfree(tp);
2958 }
7d7b93c1
AC
2959 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
2960 tty_unregister_device(driver, i);
2961 }
2962 p = driver->ttys;
2963 proc_tty_unregister_driver(driver);
2964 driver->ttys = NULL;
fe6e29fd 2965 driver->termios = NULL;
7d7b93c1
AC
2966 kfree(p);
2967 cdev_del(&driver->cdev);
2968 }
1da177e4
LT
2969 kfree(driver);
2970}
2971
7d7b93c1
AC
2972void tty_driver_kref_put(struct tty_driver *driver)
2973{
2974 kref_put(&driver->kref, destruct_tty_driver);
2975}
2976EXPORT_SYMBOL(tty_driver_kref_put);
2977
b68e31d0
JD
2978void tty_set_operations(struct tty_driver *driver,
2979 const struct tty_operations *op)
1da177e4 2980{
f34d7a5b
AC
2981 driver->ops = op;
2982};
7d7b93c1 2983EXPORT_SYMBOL(tty_set_operations);
1da177e4 2984
7d7b93c1
AC
2985void put_tty_driver(struct tty_driver *d)
2986{
2987 tty_driver_kref_put(d);
2988}
1da177e4 2989EXPORT_SYMBOL(put_tty_driver);
1da177e4
LT
2990
2991/*
2992 * Called by a tty driver to register itself.
2993 */
2994int tty_register_driver(struct tty_driver *driver)
2995{
2996 int error;
37bdfb07 2997 int i;
1da177e4
LT
2998 dev_t dev;
2999 void **p = NULL;
3000
543691a6 3001 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
fe6e29fd 3002 p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
1da177e4
LT
3003 if (!p)
3004 return -ENOMEM;
1da177e4
LT
3005 }
3006
3007 if (!driver->major) {
37bdfb07
AC
3008 error = alloc_chrdev_region(&dev, driver->minor_start,
3009 driver->num, driver->name);
1da177e4
LT
3010 if (!error) {
3011 driver->major = MAJOR(dev);
3012 driver->minor_start = MINOR(dev);
3013 }
3014 } else {
3015 dev = MKDEV(driver->major, driver->minor_start);
e5717c48 3016 error = register_chrdev_region(dev, driver->num, driver->name);
1da177e4
LT
3017 }
3018 if (error < 0) {
3019 kfree(p);
3020 return error;
3021 }
3022
3023 if (p) {
3024 driver->ttys = (struct tty_struct **)p;
edc6afc5 3025 driver->termios = (struct ktermios **)(p + driver->num);
1da177e4
LT
3026 } else {
3027 driver->ttys = NULL;
3028 driver->termios = NULL;
1da177e4
LT
3029 }
3030
3031 cdev_init(&driver->cdev, &tty_fops);
3032 driver->cdev.owner = driver->owner;
3033 error = cdev_add(&driver->cdev, dev, driver->num);
3034 if (error) {
1da177e4
LT
3035 unregister_chrdev_region(dev, driver->num);
3036 driver->ttys = NULL;
fe6e29fd 3037 driver->termios = NULL;
1da177e4
LT
3038 kfree(p);
3039 return error;
3040 }
3041
ca509f69 3042 mutex_lock(&tty_mutex);
1da177e4 3043 list_add(&driver->tty_drivers, &tty_drivers);
ca509f69 3044 mutex_unlock(&tty_mutex);
37bdfb07
AC
3045
3046 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
3047 for (i = 0; i < driver->num; i++)
1da177e4
LT
3048 tty_register_device(driver, i, NULL);
3049 }
3050 proc_tty_register_driver(driver);
7d7b93c1 3051 driver->flags |= TTY_DRIVER_INSTALLED;
1da177e4
LT
3052 return 0;
3053}
3054
3055EXPORT_SYMBOL(tty_register_driver);
3056
3057/*
3058 * Called by a tty driver to unregister itself.
3059 */
3060int tty_unregister_driver(struct tty_driver *driver)
3061{
7d7b93c1
AC
3062#if 0
3063 /* FIXME */
1da177e4
LT
3064 if (driver->refcount)
3065 return -EBUSY;
7d7b93c1 3066#endif
1da177e4
LT
3067 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3068 driver->num);
ca509f69 3069 mutex_lock(&tty_mutex);
1da177e4 3070 list_del(&driver->tty_drivers);
ca509f69 3071 mutex_unlock(&tty_mutex);
1da177e4
LT
3072 return 0;
3073}
7d7b93c1 3074
1da177e4
LT
3075EXPORT_SYMBOL(tty_unregister_driver);
3076
24ec839c
PZ
3077dev_t tty_devnum(struct tty_struct *tty)
3078{
3079 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3080}
3081EXPORT_SYMBOL(tty_devnum);
3082
3083void proc_clear_tty(struct task_struct *p)
3084{
7c3b1dcf 3085 unsigned long flags;
9c9f4ded 3086 struct tty_struct *tty;
7c3b1dcf 3087 spin_lock_irqsave(&p->sighand->siglock, flags);
9c9f4ded 3088 tty = p->signal->tty;
24ec839c 3089 p->signal->tty = NULL;
7c3b1dcf 3090 spin_unlock_irqrestore(&p->sighand->siglock, flags);
9c9f4ded 3091 tty_kref_put(tty);
24ec839c 3092}
24ec839c 3093
47f86834
AC
3094/* Called under the sighand lock */
3095
2a65f1d9 3096static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
24ec839c
PZ
3097{
3098 if (tty) {
47f86834
AC
3099 unsigned long flags;
3100 /* We should not have a session or pgrp to put here but.... */
3101 spin_lock_irqsave(&tty->ctrl_lock, flags);
d9c1e9a8
EB
3102 put_pid(tty->session);
3103 put_pid(tty->pgrp);
ab521dc0 3104 tty->pgrp = get_pid(task_pgrp(tsk));
47f86834
AC
3105 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3106 tty->session = get_pid(task_session(tsk));
9c9f4ded
AC
3107 if (tsk->signal->tty) {
3108 printk(KERN_DEBUG "tty not NULL!!\n");
3109 tty_kref_put(tsk->signal->tty);
3110 }
24ec839c 3111 }
2a65f1d9 3112 put_pid(tsk->signal->tty_old_pgrp);
9c9f4ded 3113 tsk->signal->tty = tty_kref_get(tty);
ab521dc0 3114 tsk->signal->tty_old_pgrp = NULL;
24ec839c
PZ
3115}
3116
98a27ba4 3117static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
24ec839c
PZ
3118{
3119 spin_lock_irq(&tsk->sighand->siglock);
2a65f1d9 3120 __proc_set_tty(tsk, tty);
24ec839c
PZ
3121 spin_unlock_irq(&tsk->sighand->siglock);
3122}
3123
3124struct tty_struct *get_current_tty(void)
3125{
3126 struct tty_struct *tty;
934e6ebf
AC
3127 unsigned long flags;
3128
3129 spin_lock_irqsave(&current->sighand->siglock, flags);
452a00d2 3130 tty = tty_kref_get(current->signal->tty);
934e6ebf 3131 spin_unlock_irqrestore(&current->sighand->siglock, flags);
24ec839c
PZ
3132 return tty;
3133}
a311f743 3134EXPORT_SYMBOL_GPL(get_current_tty);
1da177e4 3135
d81ed103
AC
3136void tty_default_fops(struct file_operations *fops)
3137{
3138 *fops = tty_fops;
3139}
3140
1da177e4
LT
3141/*
3142 * Initialize the console device. This is called *early*, so
3143 * we can't necessarily depend on lots of kernel help here.
3144 * Just do some early initializations, and do the complex setup
3145 * later.
3146 */
3147void __init console_init(void)
3148{
3149 initcall_t *call;
3150
3151 /* Setup the default TTY line discipline. */
01e1abb2 3152 tty_ldisc_begin();
1da177e4
LT
3153
3154 /*
37bdfb07 3155 * set up the console device so that later boot sequences can
1da177e4
LT
3156 * inform about problems etc..
3157 */
1da177e4
LT
3158 call = __con_initcall_start;
3159 while (call < __con_initcall_end) {
3160 (*call)();
3161 call++;
3162 }
3163}
3164
e454cea2
KS
3165static char *tty_devnode(struct device *dev, mode_t *mode)
3166{
3167 if (!mode)
3168 return NULL;
3169 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3170 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3171 *mode = 0666;
3172 return NULL;
3173}
3174
1da177e4
LT
3175static int __init tty_class_init(void)
3176{
7fe845d1 3177 tty_class = class_create(THIS_MODULE, "tty");
1da177e4
LT
3178 if (IS_ERR(tty_class))
3179 return PTR_ERR(tty_class);
e454cea2 3180 tty_class->devnode = tty_devnode;
1da177e4
LT
3181 return 0;
3182}
3183
3184postcore_initcall(tty_class_init);
3185
3186/* 3/2004 jmc: why do these devices exist? */
3187
3188static struct cdev tty_cdev, console_cdev;
1da177e4
LT
3189
3190/*
3191 * Ok, now we can initialize the rest of the tty devices and can count
3192 * on memory allocations, interrupts etc..
3193 */
31d1d48e 3194int __init tty_init(void)
1da177e4
LT
3195{
3196 cdev_init(&tty_cdev, &tty_fops);
3197 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3198 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3199 panic("Couldn't register /dev/tty driver\n");
d81ed103 3200 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
47aa5793 3201 "tty");
1da177e4
LT
3202
3203 cdev_init(&console_cdev, &console_fops);
3204 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3205 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3206 panic("Couldn't register /dev/console driver\n");
d81ed103 3207 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
47aa5793 3208 "console");
1da177e4 3209
1da177e4 3210#ifdef CONFIG_VT
d81ed103 3211 vty_init(&console_fops);
1da177e4
LT
3212#endif
3213 return 0;
3214}
31d1d48e 3215