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