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