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