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