2 * linux/drivers/char/tty_io.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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.
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
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.
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
22 * makes for cleaner and more compact code. -TYT, 9/17/92
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).
28 * NOTE: pay no attention to the line discipline code (yet); its
29 * interface is still subject to change in this version...
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.
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
45 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
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
52 * Rewrote init_dev and release_dev to eliminate races.
53 * -- Bill Hawes <whawes@star.net>, June 97
55 * Added devfs support.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
58 * Added support for a Unix98-style ptmx device.
59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
61 * Reduced memory usage for older ARM systems
62 * -- Russell King <rmk@arm.linux.org.uk>
64 * Move do_SAK() into process context. Less stack use in devfs functions.
65 * alloc_tty_struct() always uses kmalloc()
66 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
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>
81 #include <linux/fdtable.h>
82 #include <linux/console.h>
83 #include <linux/timer.h>
84 #include <linux/ctype.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>
95 #include <linux/wait.h>
96 #include <linux/bitops.h>
97 #include <linux/delay.h>
98 #include <linux/seq_file.h>
100 #include <linux/uaccess.h>
101 #include <asm/system.h>
103 #include <linux/kbd_kern.h>
104 #include <linux/vt_kern.h>
105 #include <linux/selection.h>
107 #include <linux/kmod.h>
108 #include <linux/nsproxy.h>
110 #undef TTY_DEBUG_HANGUP
112 #define TTY_PARANOIA_CHECK 1
113 #define CHECK_TTY_COUNT 1
115 struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
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,
126 EXPORT_SYMBOL(tty_std_termios);
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
132 LIST_HEAD(tty_drivers); /* linked list of tty drivers */
134 /* Mutex to protect creating and releasing a tty. This is shared with
135 vt.c for deeply disgusting hack reasons */
136 DEFINE_MUTEX(tty_mutex);
137 EXPORT_SYMBOL(tty_mutex);
139 #ifdef CONFIG_UNIX98_PTYS
140 extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
141 static int ptmx_open(struct inode *, struct file *);
144 static void initialize_tty_struct(struct tty_struct *tty);
146 static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
147 static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
148 ssize_t redirected_tty_write(struct file *, const char __user *,
150 static unsigned int tty_poll(struct file *, poll_table *);
151 static int tty_open(struct inode *, struct file *);
152 static int tty_release(struct inode *, struct file *);
153 long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
155 static long tty_compat_ioctl(struct file *file, unsigned int cmd,
158 #define tty_compat_ioctl NULL
160 static int tty_fasync(int fd, struct file *filp, int on);
161 static void release_tty(struct tty_struct *tty, int idx);
162 static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
163 static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
166 * alloc_tty_struct - allocate a tty object
168 * Return a new empty tty structure. The data fields have not
169 * been initialized in any way but has been zeroed
174 static struct tty_struct *alloc_tty_struct(void)
176 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
180 * free_tty_struct - free a disused tty
181 * @tty: tty struct to free
183 * Free the write buffers, tty queue and tty memory itself.
185 * Locking: none. Must be called after tty is definitely unused
188 static inline void free_tty_struct(struct tty_struct *tty)
190 kfree(tty->write_buf);
191 tty_buffer_free_all(tty);
195 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
198 * tty_name - return tty naming
199 * @tty: tty structure
200 * @buf: buffer for output
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
208 char *tty_name(struct tty_struct *tty, char *buf)
210 if (!tty) /* Hmm. NULL pointer. That's fun. */
211 strcpy(buf, "NULL tty");
213 strcpy(buf, tty->name);
217 EXPORT_SYMBOL(tty_name);
219 int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
222 #ifdef TTY_PARANOIA_CHECK
225 "null TTY for (%d:%d) in %s\n",
226 imajor(inode), iminor(inode), routine);
229 if (tty->magic != TTY_MAGIC) {
231 "bad magic number for tty struct (%d:%d) in %s\n",
232 imajor(inode), iminor(inode), routine);
239 static int check_tty_count(struct tty_struct *tty, const char *routine)
241 #ifdef CHECK_TTY_COUNT
246 list_for_each(p, &tty->tty_files) {
250 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
251 tty->driver->subtype == PTY_TYPE_SLAVE &&
252 tty->link && tty->link->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);
265 * get_tty_driver - find device of a tty
266 * @dev_t: device identifier
267 * @index: returns the index of the tty
269 * This routine returns a tty driver structure, given a device number
270 * and also passes back the index number.
272 * Locking: caller must hold tty_mutex
275 static struct tty_driver *get_tty_driver(dev_t device, int *index)
277 struct tty_driver *p;
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)
283 *index = device - base;
289 #ifdef CONFIG_CONSOLE_POLL
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
296 * This routine returns a tty driver structure, given a name
297 * and the condition that the tty driver is capable of polled
300 struct tty_driver *tty_find_polling_driver(char *name, int *line)
302 struct tty_driver *p, *res = NULL;
307 for (str = name; *str; str++)
308 if ((*str >= '0' && *str <= '9') || *str == ',')
314 tty_line = simple_strtoul(str, &str, 10);
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) {
319 if (strncmp(name, p->name, len) != 0)
326 if (tty_line >= 0 && tty_line <= p->num && p->ops &&
327 p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) {
333 mutex_unlock(&tty_mutex);
337 EXPORT_SYMBOL_GPL(tty_find_polling_driver);
341 * tty_check_change - check for POSIX terminal changes
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)
351 int tty_check_change(struct tty_struct *tty)
356 if (current->signal->tty != tty)
359 spin_lock_irqsave(&tty->ctrl_lock, flags);
362 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
365 if (task_pgrp(current) == tty->pgrp)
367 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
368 if (is_ignored(SIGTTOU))
370 if (is_current_pgrp_orphaned()) {
374 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
375 set_thread_flag(TIF_SIGPENDING);
380 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
384 EXPORT_SYMBOL(tty_check_change);
386 static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
387 size_t count, loff_t *ppos)
392 static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
393 size_t count, loff_t *ppos)
398 /* No kernel lock held - none needed ;) */
399 static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
401 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
404 static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
407 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
410 static long hung_up_tty_compat_ioctl(struct file *file,
411 unsigned int cmd, unsigned long arg)
413 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
416 static const struct file_operations tty_fops = {
421 .unlocked_ioctl = tty_ioctl,
422 .compat_ioctl = tty_compat_ioctl,
424 .release = tty_release,
425 .fasync = tty_fasync,
428 #ifdef CONFIG_UNIX98_PTYS
429 static const struct file_operations ptmx_fops = {
434 .unlocked_ioctl = tty_ioctl,
435 .compat_ioctl = tty_compat_ioctl,
437 .release = tty_release,
438 .fasync = tty_fasync,
442 static const struct file_operations console_fops = {
445 .write = redirected_tty_write,
447 .unlocked_ioctl = tty_ioctl,
448 .compat_ioctl = tty_compat_ioctl,
450 .release = tty_release,
451 .fasync = tty_fasync,
454 static const struct file_operations hung_up_tty_fops = {
456 .read = hung_up_tty_read,
457 .write = hung_up_tty_write,
458 .poll = hung_up_tty_poll,
459 .unlocked_ioctl = hung_up_tty_ioctl,
460 .compat_ioctl = hung_up_tty_compat_ioctl,
461 .release = tty_release,
464 static DEFINE_SPINLOCK(redirect_lock);
465 static struct file *redirect;
468 * tty_wakeup - request more data
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.
476 void tty_wakeup(struct tty_struct *tty)
478 struct tty_ldisc *ld;
480 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
481 ld = tty_ldisc_ref(tty);
483 if (ld->ops->write_wakeup)
484 ld->ops->write_wakeup(tty);
488 wake_up_interruptible(&tty->write_wait);
491 EXPORT_SYMBOL_GPL(tty_wakeup);
494 * tty_ldisc_flush - flush line discipline queue
497 * Flush the line discipline queue (if any) for this tty. If there
498 * is no line discipline active this is a no-op.
501 void tty_ldisc_flush(struct tty_struct *tty)
503 struct tty_ldisc *ld = tty_ldisc_ref(tty);
505 if (ld->ops->flush_buffer)
506 ld->ops->flush_buffer(tty);
509 tty_buffer_flush(tty);
512 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
515 * tty_reset_termios - reset terminal state
518 * Restore a terminal to the driver default state
521 static void tty_reset_termios(struct tty_struct *tty)
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);
531 * do_tty_hangup - actual handler for hangup events
534 * This can be called by the "eventd" kernel thread. That is process
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.
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
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
552 static void do_tty_hangup(struct work_struct *work)
554 struct tty_struct *tty =
555 container_of(work, struct tty_struct, hangup_work);
556 struct file *cons_filp = NULL;
557 struct file *filp, *f = NULL;
558 struct task_struct *p;
559 struct tty_ldisc *ld;
560 int closecount = 0, n;
566 /* inuse_filps is protected by the single kernel lock */
569 spin_lock(&redirect_lock);
570 if (redirect && redirect->private_data == tty) {
574 spin_unlock(&redirect_lock);
576 check_tty_count(tty, "do_tty_hangup");
578 /* This breaks for file handles being sent over AF_UNIX sockets ? */
579 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
580 if (filp->f_op->write == redirected_tty_write)
582 if (filp->f_op->write != tty_write)
585 tty_fasync(-1, filp, 0); /* can't block */
586 filp->f_op = &hung_up_tty_fops;
590 * FIXME! What are the locking issues here? This may me overdoing
591 * things... This question is especially important now that we've
592 * removed the irqlock.
594 ld = tty_ldisc_ref(tty);
596 /* We may have no line discipline at this point */
597 if (ld->ops->flush_buffer)
598 ld->ops->flush_buffer(tty);
599 tty_driver_flush_buffer(tty);
600 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
601 ld->ops->write_wakeup)
602 ld->ops->write_wakeup(tty);
604 ld->ops->hangup(tty);
607 * FIXME: Once we trust the LDISC code better we can wait here for
608 * ldisc completion and fix the driver call race
610 wake_up_interruptible(&tty->write_wait);
611 wake_up_interruptible(&tty->read_wait);
613 * Shutdown the current line discipline, and reset it to
616 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
617 tty_reset_termios(tty);
618 /* Defer ldisc switch */
619 /* tty_deferred_ldisc_switch(N_TTY);
621 This should get done automatically when the port closes and
622 tty_release is called */
624 read_lock(&tasklist_lock);
626 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
627 spin_lock_irq(&p->sighand->siglock);
628 if (p->signal->tty == tty)
629 p->signal->tty = NULL;
630 if (!p->signal->leader) {
631 spin_unlock_irq(&p->sighand->siglock);
634 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
635 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
636 put_pid(p->signal->tty_old_pgrp); /* A noop */
637 spin_lock_irqsave(&tty->ctrl_lock, flags);
639 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
640 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
641 spin_unlock_irq(&p->sighand->siglock);
642 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
644 read_unlock(&tasklist_lock);
646 spin_lock_irqsave(&tty->ctrl_lock, flags);
648 put_pid(tty->session);
652 tty->ctrl_status = 0;
653 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
656 * If one of the devices matches a console pointer, we
657 * cannot just call hangup() because that will cause
658 * tty->count and state->count to go out of sync.
659 * So we just call close() the right number of times.
663 for (n = 0; n < closecount; n++)
664 tty->ops->close(tty, cons_filp);
665 } else if (tty->ops->hangup)
666 (tty->ops->hangup)(tty);
668 * We don't want to have driver/ldisc interactions beyond
669 * the ones we did here. The driver layer expects no
670 * calls after ->hangup() from the ldisc side. However we
671 * can't yet guarantee all that.
673 set_bit(TTY_HUPPED, &tty->flags);
675 tty_ldisc_enable(tty);
684 * tty_hangup - trigger a hangup event
685 * @tty: tty to hangup
687 * A carrier loss (virtual or otherwise) has occurred on this like
688 * schedule a hangup sequence to run after this event.
691 void tty_hangup(struct tty_struct *tty)
693 #ifdef TTY_DEBUG_HANGUP
695 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
697 schedule_work(&tty->hangup_work);
700 EXPORT_SYMBOL(tty_hangup);
703 * tty_vhangup - process vhangup
704 * @tty: tty to hangup
706 * The user has asked via system call for the terminal to be hung up.
707 * We do this synchronously so that when the syscall returns the process
708 * is complete. That guarantee is necessary for security reasons.
711 void tty_vhangup(struct tty_struct *tty)
713 #ifdef TTY_DEBUG_HANGUP
716 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
718 do_tty_hangup(&tty->hangup_work);
721 EXPORT_SYMBOL(tty_vhangup);
724 * tty_hung_up_p - was tty hung up
725 * @filp: file pointer of tty
727 * Return true if the tty has been subject to a vhangup or a carrier
731 int tty_hung_up_p(struct file *filp)
733 return (filp->f_op == &hung_up_tty_fops);
736 EXPORT_SYMBOL(tty_hung_up_p);
738 static void session_clear_tty(struct pid *session)
740 struct task_struct *p;
741 do_each_pid_task(session, PIDTYPE_SID, p) {
743 } while_each_pid_task(session, PIDTYPE_SID, p);
747 * disassociate_ctty - disconnect controlling tty
748 * @on_exit: true if exiting so need to "hang up" the session
750 * This function is typically called only by the session leader, when
751 * it wants to disassociate itself from its controlling tty.
753 * It performs the following functions:
754 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
755 * (2) Clears the tty from being controlling the session
756 * (3) Clears the controlling tty for all processes in the
759 * The argument on_exit is set to 1 if called when a process is
760 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
763 * BKL is taken for hysterical raisins
764 * tty_mutex is taken to protect tty
765 * ->siglock is taken to protect ->signal/->sighand
766 * tasklist_lock is taken to walk process list for sessions
767 * ->siglock is taken to protect ->signal/->sighand
770 void disassociate_ctty(int on_exit)
772 struct tty_struct *tty;
773 struct pid *tty_pgrp = NULL;
776 mutex_lock(&tty_mutex);
777 tty = get_current_tty();
779 tty_pgrp = get_pid(tty->pgrp);
781 mutex_unlock(&tty_mutex);
782 /* XXX: here we race, there is nothing protecting tty */
783 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
786 } else if (on_exit) {
787 struct pid *old_pgrp;
788 spin_lock_irq(¤t->sighand->siglock);
789 old_pgrp = current->signal->tty_old_pgrp;
790 current->signal->tty_old_pgrp = NULL;
791 spin_unlock_irq(¤t->sighand->siglock);
793 kill_pgrp(old_pgrp, SIGHUP, on_exit);
794 kill_pgrp(old_pgrp, SIGCONT, on_exit);
797 mutex_unlock(&tty_mutex);
801 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
803 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
807 spin_lock_irq(¤t->sighand->siglock);
808 put_pid(current->signal->tty_old_pgrp);
809 current->signal->tty_old_pgrp = NULL;
810 spin_unlock_irq(¤t->sighand->siglock);
812 mutex_lock(&tty_mutex);
813 /* It is possible that do_tty_hangup has free'd this tty */
814 tty = get_current_tty();
817 spin_lock_irqsave(&tty->ctrl_lock, flags);
818 put_pid(tty->session);
822 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
824 #ifdef TTY_DEBUG_HANGUP
825 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
829 mutex_unlock(&tty_mutex);
831 /* Now clear signal->tty under the lock */
832 read_lock(&tasklist_lock);
833 session_clear_tty(task_session(current));
834 read_unlock(&tasklist_lock);
839 * no_tty - Ensure the current process does not have a controlling tty
843 struct task_struct *tsk = current;
845 if (tsk->signal->leader)
846 disassociate_ctty(0);
853 * stop_tty - propagate flow control
856 * Perform flow control to the driver. For PTY/TTY pairs we
857 * must also propagate the TIOCKPKT status. May be called
858 * on an already stopped device and will not re-call the driver
861 * This functionality is used by both the line disciplines for
862 * halting incoming flow and by the driver. It may therefore be
863 * called from any context, may be under the tty atomic_write_lock
867 * Uses the tty control lock internally
870 void stop_tty(struct tty_struct *tty)
873 spin_lock_irqsave(&tty->ctrl_lock, flags);
875 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
879 if (tty->link && tty->link->packet) {
880 tty->ctrl_status &= ~TIOCPKT_START;
881 tty->ctrl_status |= TIOCPKT_STOP;
882 wake_up_interruptible(&tty->link->read_wait);
884 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
886 (tty->ops->stop)(tty);
889 EXPORT_SYMBOL(stop_tty);
892 * start_tty - propagate flow control
895 * Start a tty that has been stopped if at all possible. Perform
896 * any necessary wakeups and propagate the TIOCPKT status. If this
897 * is the tty was previous stopped and is being started then the
898 * driver start method is invoked and the line discipline woken.
904 void start_tty(struct tty_struct *tty)
907 spin_lock_irqsave(&tty->ctrl_lock, flags);
908 if (!tty->stopped || tty->flow_stopped) {
909 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
913 if (tty->link && tty->link->packet) {
914 tty->ctrl_status &= ~TIOCPKT_STOP;
915 tty->ctrl_status |= TIOCPKT_START;
916 wake_up_interruptible(&tty->link->read_wait);
918 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
920 (tty->ops->start)(tty);
921 /* If we have a running line discipline it may need kicking */
925 EXPORT_SYMBOL(start_tty);
928 * tty_read - read method for tty device files
929 * @file: pointer to tty file
931 * @count: size of user buffer
934 * Perform the read system call function on this terminal device. Checks
935 * for hung up devices before calling the line discipline method.
938 * Locks the line discipline internally while needed. Multiple
939 * read calls may be outstanding in parallel.
942 static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
946 struct tty_struct *tty;
948 struct tty_ldisc *ld;
950 tty = (struct tty_struct *)file->private_data;
951 inode = file->f_path.dentry->d_inode;
952 if (tty_paranoia_check(tty, inode, "tty_read"))
954 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
957 /* We want to wait for the line discipline to sort out in this
959 ld = tty_ldisc_ref_wait(tty);
961 i = (ld->ops->read)(tty, file, buf, count);
966 inode->i_atime = current_fs_time(inode->i_sb);
970 void tty_write_unlock(struct tty_struct *tty)
972 mutex_unlock(&tty->atomic_write_lock);
973 wake_up_interruptible(&tty->write_wait);
976 int tty_write_lock(struct tty_struct *tty, int ndelay)
978 if (!mutex_trylock(&tty->atomic_write_lock)) {
981 if (mutex_lock_interruptible(&tty->atomic_write_lock))
988 * Split writes up in sane blocksizes to avoid
989 * denial-of-service type attacks
991 static inline ssize_t do_tty_write(
992 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
993 struct tty_struct *tty,
995 const char __user *buf,
998 ssize_t ret, written = 0;
1001 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1006 * We chunk up writes into a temporary buffer. This
1007 * simplifies low-level drivers immensely, since they
1008 * don't have locking issues and user mode accesses.
1010 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1013 * The default chunk-size is 2kB, because the NTTY
1014 * layer has problems with bigger chunks. It will
1015 * claim to be able to handle more characters than
1018 * FIXME: This can probably go away now except that 64K chunks
1019 * are too likely to fail unless switched to vmalloc...
1022 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1027 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1028 if (tty->write_cnt < chunk) {
1034 buf = kmalloc(chunk, GFP_KERNEL);
1039 kfree(tty->write_buf);
1040 tty->write_cnt = chunk;
1041 tty->write_buf = buf;
1044 /* Do the write .. */
1046 size_t size = count;
1050 if (copy_from_user(tty->write_buf, buf, size))
1052 ret = write(tty, file, tty->write_buf, size);
1061 if (signal_pending(current))
1066 struct inode *inode = file->f_path.dentry->d_inode;
1067 inode->i_mtime = current_fs_time(inode->i_sb);
1071 tty_write_unlock(tty);
1077 * tty_write - write method for tty device file
1078 * @file: tty file pointer
1079 * @buf: user data to write
1080 * @count: bytes to write
1083 * Write data to a tty device via the line discipline.
1086 * Locks the line discipline as required
1087 * Writes to the tty driver are serialized by the atomic_write_lock
1088 * and are then processed in chunks to the device. The line discipline
1089 * write method will not be involked in parallel for each device
1090 * The line discipline write method is called under the big
1091 * kernel lock for historical reasons. New code should not rely on this.
1094 static ssize_t tty_write(struct file *file, const char __user *buf,
1095 size_t count, loff_t *ppos)
1097 struct tty_struct *tty;
1098 struct inode *inode = file->f_path.dentry->d_inode;
1100 struct tty_ldisc *ld;
1102 tty = (struct tty_struct *)file->private_data;
1103 if (tty_paranoia_check(tty, inode, "tty_write"))
1105 if (!tty || !tty->ops->write ||
1106 (test_bit(TTY_IO_ERROR, &tty->flags)))
1108 /* Short term debug to catch buggy drivers */
1109 if (tty->ops->write_room == NULL)
1110 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1112 ld = tty_ldisc_ref_wait(tty);
1113 if (!ld->ops->write)
1116 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
1117 tty_ldisc_deref(ld);
1121 ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1122 size_t count, loff_t *ppos)
1124 struct file *p = NULL;
1126 spin_lock(&redirect_lock);
1131 spin_unlock(&redirect_lock);
1135 res = vfs_write(p, buf, count, &p->f_pos);
1139 return tty_write(file, buf, count, ppos);
1142 void tty_port_init(struct tty_port *port)
1144 memset(port, 0, sizeof(*port));
1145 init_waitqueue_head(&port->open_wait);
1146 init_waitqueue_head(&port->close_wait);
1147 mutex_init(&port->mutex);
1148 port->close_delay = (50 * HZ) / 100;
1149 port->closing_wait = (3000 * HZ) / 100;
1151 EXPORT_SYMBOL(tty_port_init);
1153 int tty_port_alloc_xmit_buf(struct tty_port *port)
1155 /* We may sleep in get_zeroed_page() */
1156 mutex_lock(&port->mutex);
1157 if (port->xmit_buf == NULL)
1158 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1159 mutex_unlock(&port->mutex);
1160 if (port->xmit_buf == NULL)
1164 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
1166 void tty_port_free_xmit_buf(struct tty_port *port)
1168 mutex_lock(&port->mutex);
1169 if (port->xmit_buf != NULL) {
1170 free_page((unsigned long)port->xmit_buf);
1171 port->xmit_buf = NULL;
1173 mutex_unlock(&port->mutex);
1175 EXPORT_SYMBOL(tty_port_free_xmit_buf);
1178 static char ptychar[] = "pqrstuvwxyzabcde";
1181 * pty_line_name - generate name for a pty
1182 * @driver: the tty driver in use
1183 * @index: the minor number
1184 * @p: output buffer of at least 6 bytes
1186 * Generate a name from a driver reference and write it to the output
1191 static void pty_line_name(struct tty_driver *driver, int index, char *p)
1193 int i = index + driver->name_base;
1194 /* ->name is initialized to "ttyp", but "tty" is expected */
1195 sprintf(p, "%s%c%x",
1196 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1197 ptychar[i >> 4 & 0xf], i & 0xf);
1201 * pty_line_name - generate name for a tty
1202 * @driver: the tty driver in use
1203 * @index: the minor number
1204 * @p: output buffer of at least 7 bytes
1206 * Generate a name from a driver reference and write it to the output
1211 static void tty_line_name(struct tty_driver *driver, int index, char *p)
1213 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1217 * init_dev - initialise a tty device
1218 * @driver: tty driver we are opening a device on
1219 * @idx: device index
1220 * @tty: returned tty structure
1222 * Prepare a tty device. This may not be a "new" clean device but
1223 * could also be an active device. The pty drivers require special
1224 * handling because of this.
1227 * The function is called under the tty_mutex, which
1228 * protects us from the tty struct or driver itself going away.
1230 * On exit the tty device has the line discipline attached and
1231 * a reference count of 1. If a pair was created for pty/tty use
1232 * and the other was a pty master then it too has a reference count of 1.
1234 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1235 * failed open. The new code protects the open with a mutex, so it's
1236 * really quite straightforward. The mutex locking can probably be
1237 * relaxed for the (most common) case of reopening a tty.
1240 static int init_dev(struct tty_driver *driver, int idx,
1241 struct tty_struct **ret_tty)
1243 struct tty_struct *tty, *o_tty;
1244 struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
1245 struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
1248 /* check whether we're reopening an existing tty */
1249 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1250 tty = devpts_get_tty(idx);
1252 * If we don't have a tty here on a slave open, it's because
1253 * the master already started the close process and there's
1254 * no relation between devpts file and tty anymore.
1256 if (!tty && driver->subtype == PTY_TYPE_SLAVE) {
1261 * It's safe from now on because init_dev() is called with
1262 * tty_mutex held and release_dev() won't change tty->count
1263 * or tty->flags without having to grab tty_mutex
1265 if (tty && driver->subtype == PTY_TYPE_MASTER)
1268 tty = driver->ttys[idx];
1270 if (tty) goto fast_track;
1273 * First time open is complex, especially for PTY devices.
1274 * This code guarantees that either everything succeeds and the
1275 * TTY is ready for operation, or else the table slots are vacated
1276 * and the allocated memory released. (Except that the termios
1277 * and locked termios may be retained.)
1280 if (!try_module_get(driver->owner)) {
1289 tty = alloc_tty_struct();
1292 initialize_tty_struct(tty);
1293 tty->driver = driver;
1294 tty->ops = driver->ops;
1296 tty_line_name(driver, idx, tty->name);
1298 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1299 tp_loc = &tty->termios;
1300 ltp_loc = &tty->termios_locked;
1302 tp_loc = &driver->termios[idx];
1303 ltp_loc = &driver->termios_locked[idx];
1307 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1310 *tp = driver->init_termios;
1314 ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
1319 if (driver->type == TTY_DRIVER_TYPE_PTY) {
1320 o_tty = alloc_tty_struct();
1323 initialize_tty_struct(o_tty);
1324 o_tty->driver = driver->other;
1325 o_tty->ops = driver->ops;
1327 tty_line_name(driver->other, idx, o_tty->name);
1329 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1330 o_tp_loc = &o_tty->termios;
1331 o_ltp_loc = &o_tty->termios_locked;
1333 o_tp_loc = &driver->other->termios[idx];
1334 o_ltp_loc = &driver->other->termios_locked[idx];
1338 o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1341 *o_tp = driver->other->init_termios;
1345 o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
1351 * Everything allocated ... set up the o_tty structure.
1353 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM))
1354 driver->other->ttys[idx] = o_tty;
1359 o_tty->termios = *o_tp_loc;
1360 o_tty->termios_locked = *o_ltp_loc;
1361 driver->other->refcount++;
1362 if (driver->subtype == PTY_TYPE_MASTER)
1365 /* Establish the links in both directions */
1371 * All structures have been allocated, so now we install them.
1372 * Failures after this point use release_tty to clean up, so
1373 * there's no need to null out the local pointers.
1375 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM))
1376 driver->ttys[idx] = tty;
1382 tty->termios = *tp_loc;
1383 tty->termios_locked = *ltp_loc;
1384 /* Compatibility until drivers always set this */
1385 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1386 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1391 * Structures all installed ... call the ldisc open routines.
1392 * If we fail here just call release_tty to clean up. No need
1393 * to decrement the use counts, as release_tty doesn't care.
1396 retval = tty_ldisc_setup(tty, o_tty);
1399 goto release_mem_out;
1403 * This fast open can be used if the tty is already open.
1404 * No memory is allocated, and the only failures are from
1405 * attempting to open a closing tty or attempting multiple
1406 * opens on a pty master.
1409 if (test_bit(TTY_CLOSING, &tty->flags)) {
1413 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1414 driver->subtype == PTY_TYPE_MASTER) {
1416 * special case for PTY masters: only one open permitted,
1417 * and the slave side open count is incremented as well.
1426 tty->driver = driver; /* N.B. why do this every time?? */
1429 if (!test_bit(TTY_LDISC, &tty->flags))
1430 printk(KERN_ERR "init_dev but no ldisc\n");
1434 /* All paths come through here to release the mutex */
1438 /* Release locally allocated memory ... nothing placed in slots */
1442 free_tty_struct(o_tty);
1445 free_tty_struct(tty);
1448 module_put(driver->owner);
1452 /* call the tty release_tty routine to clean out this slot */
1454 if (printk_ratelimit())
1455 printk(KERN_INFO "init_dev: ldisc open failed, "
1456 "clearing slot %d\n", idx);
1457 release_tty(tty, idx);
1462 * release_one_tty - release tty structure memory
1464 * Releases memory associated with a tty structure, and clears out the
1465 * driver table slots. This function is called when a device is no longer
1466 * in use. It also gets called when setup of a device fails.
1469 * tty_mutex - sometimes only
1470 * takes the file list lock internally when working on the list
1471 * of ttys that the driver keeps.
1472 * FIXME: should we require tty_mutex is held here ??
1474 static void release_one_tty(struct tty_struct *tty, int idx)
1476 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
1477 struct ktermios *tp;
1480 tty->driver->ttys[idx] = NULL;
1482 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1485 tty->driver->termios[idx] = NULL;
1488 tp = tty->termios_locked;
1490 tty->driver->termios_locked[idx] = NULL;
1496 tty->driver->refcount--;
1499 list_del_init(&tty->tty_files);
1502 free_tty_struct(tty);
1506 * release_tty - release tty structure memory
1508 * Release both @tty and a possible linked partner (think pty pair),
1509 * and decrement the refcount of the backing module.
1512 * tty_mutex - sometimes only
1513 * takes the file list lock internally when working on the list
1514 * of ttys that the driver keeps.
1515 * FIXME: should we require tty_mutex is held here ??
1517 static void release_tty(struct tty_struct *tty, int idx)
1519 struct tty_driver *driver = tty->driver;
1522 release_one_tty(tty->link, idx);
1523 release_one_tty(tty, idx);
1524 module_put(driver->owner);
1528 * Even releasing the tty structures is a tricky business.. We have
1529 * to be very careful that the structures are all released at the
1530 * same time, as interrupts might otherwise get the wrong pointers.
1532 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1533 * lead to double frees or releasing memory still in use.
1535 static void release_dev(struct file *filp)
1537 struct tty_struct *tty, *o_tty;
1538 int pty_master, tty_closing, o_tty_closing, do_sleep;
1543 tty = (struct tty_struct *)filp->private_data;
1544 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode,
1548 check_tty_count(tty, "release_dev");
1550 tty_fasync(-1, filp, 0);
1553 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1554 tty->driver->subtype == PTY_TYPE_MASTER);
1555 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1558 #ifdef TTY_PARANOIA_CHECK
1559 if (idx < 0 || idx >= tty->driver->num) {
1560 printk(KERN_DEBUG "release_dev: bad idx when trying to "
1561 "free (%s)\n", tty->name);
1564 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1565 if (tty != tty->driver->ttys[idx]) {
1566 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1567 "for (%s)\n", idx, tty->name);
1570 if (tty->termios != tty->driver->termios[idx]) {
1571 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1576 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
1577 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1578 "termios_locked for (%s)\n",
1585 #ifdef TTY_DEBUG_HANGUP
1586 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1587 tty_name(tty, buf), tty->count);
1590 #ifdef TTY_PARANOIA_CHECK
1591 if (tty->driver->other &&
1592 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1593 if (o_tty != tty->driver->other->ttys[idx]) {
1594 printk(KERN_DEBUG "release_dev: other->table[%d] "
1595 "not o_tty for (%s)\n",
1599 if (o_tty->termios != tty->driver->other->termios[idx]) {
1600 printk(KERN_DEBUG "release_dev: other->termios[%d] "
1601 "not o_termios for (%s)\n",
1605 if (o_tty->termios_locked !=
1606 tty->driver->other->termios_locked[idx]) {
1607 printk(KERN_DEBUG "release_dev: other->termios_locked["
1608 "%d] not o_termios_locked for (%s)\n",
1612 if (o_tty->link != tty) {
1613 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
1618 if (tty->ops->close)
1619 tty->ops->close(tty, filp);
1622 * Sanity check: if tty->count is going to zero, there shouldn't be
1623 * any waiters on tty->read_wait or tty->write_wait. We test the
1624 * wait queues and kick everyone out _before_ actually starting to
1625 * close. This ensures that we won't block while releasing the tty
1628 * The test for the o_tty closing is necessary, since the master and
1629 * slave sides may close in any order. If the slave side closes out
1630 * first, its count will be one, since the master side holds an open.
1631 * Thus this test wouldn't be triggered at the time the slave closes,
1634 * Note that it's possible for the tty to be opened again while we're
1635 * flushing out waiters. By recalculating the closing flags before
1636 * each iteration we avoid any problems.
1639 /* Guard against races with tty->count changes elsewhere and
1640 opens on /dev/tty */
1642 mutex_lock(&tty_mutex);
1643 tty_closing = tty->count <= 1;
1644 o_tty_closing = o_tty &&
1645 (o_tty->count <= (pty_master ? 1 : 0));
1649 if (waitqueue_active(&tty->read_wait)) {
1650 wake_up(&tty->read_wait);
1653 if (waitqueue_active(&tty->write_wait)) {
1654 wake_up(&tty->write_wait);
1658 if (o_tty_closing) {
1659 if (waitqueue_active(&o_tty->read_wait)) {
1660 wake_up(&o_tty->read_wait);
1663 if (waitqueue_active(&o_tty->write_wait)) {
1664 wake_up(&o_tty->write_wait);
1671 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
1672 "active!\n", tty_name(tty, buf));
1673 mutex_unlock(&tty_mutex);
1678 * The closing flags are now consistent with the open counts on
1679 * both sides, and we've completed the last operation that could
1680 * block, so it's safe to proceed with closing.
1683 if (--o_tty->count < 0) {
1684 printk(KERN_WARNING "release_dev: bad pty slave count "
1686 o_tty->count, tty_name(o_tty, buf));
1690 if (--tty->count < 0) {
1691 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
1692 tty->count, tty_name(tty, buf));
1697 * We've decremented tty->count, so we need to remove this file
1698 * descriptor off the tty->tty_files list; this serves two
1700 * - check_tty_count sees the correct number of file descriptors
1701 * associated with this tty.
1702 * - do_tty_hangup no longer sees this file descriptor as
1703 * something that needs to be handled for hangups.
1706 filp->private_data = NULL;
1709 * Perform some housekeeping before deciding whether to return.
1711 * Set the TTY_CLOSING flag if this was the last open. In the
1712 * case of a pty we may have to wait around for the other side
1713 * to close, and TTY_CLOSING makes sure we can't be reopened.
1716 set_bit(TTY_CLOSING, &tty->flags);
1718 set_bit(TTY_CLOSING, &o_tty->flags);
1721 * If _either_ side is closing, make sure there aren't any
1722 * processes that still think tty or o_tty is their controlling
1725 if (tty_closing || o_tty_closing) {
1726 read_lock(&tasklist_lock);
1727 session_clear_tty(tty->session);
1729 session_clear_tty(o_tty->session);
1730 read_unlock(&tasklist_lock);
1733 mutex_unlock(&tty_mutex);
1735 /* check whether both sides are closing ... */
1736 if (!tty_closing || (o_tty && !o_tty_closing))
1739 #ifdef TTY_DEBUG_HANGUP
1740 printk(KERN_DEBUG "freeing tty structure...");
1743 * Ask the line discipline code to release its structures
1745 tty_ldisc_release(tty, o_tty);
1747 * The release_tty function takes care of the details of clearing
1748 * the slots and preserving the termios structure.
1750 release_tty(tty, idx);
1752 /* Make this pty number available for reallocation */
1754 devpts_kill_index(idx);
1758 * tty_open - open a tty device
1759 * @inode: inode of device file
1760 * @filp: file pointer to tty
1762 * tty_open and tty_release keep up the tty count that contains the
1763 * number of opens done on a tty. We cannot use the inode-count, as
1764 * different inodes might point to the same tty.
1766 * Open-counting is needed for pty masters, as well as for keeping
1767 * track of serial lines: DTR is dropped when the last close happens.
1768 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1770 * The termios state of a pty is reset on first open so that
1771 * settings don't persist across reuse.
1773 * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
1774 * tty->count should protect the rest.
1775 * ->siglock protects ->signal/->sighand
1778 static int __tty_open(struct inode *inode, struct file *filp)
1780 struct tty_struct *tty;
1782 struct tty_driver *driver;
1784 dev_t device = inode->i_rdev;
1785 unsigned short saved_flags = filp->f_flags;
1787 nonseekable_open(inode, filp);
1790 noctty = filp->f_flags & O_NOCTTY;
1794 mutex_lock(&tty_mutex);
1796 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
1797 tty = get_current_tty();
1799 mutex_unlock(&tty_mutex);
1802 driver = tty->driver;
1804 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1809 if (device == MKDEV(TTY_MAJOR, 0)) {
1810 extern struct tty_driver *console_driver;
1811 driver = console_driver;
1817 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
1818 driver = console_device(&index);
1820 /* Don't let /dev/console block */
1821 filp->f_flags |= O_NONBLOCK;
1825 mutex_unlock(&tty_mutex);
1829 driver = get_tty_driver(device, &index);
1831 mutex_unlock(&tty_mutex);
1835 retval = init_dev(driver, index, &tty);
1836 mutex_unlock(&tty_mutex);
1840 filp->private_data = tty;
1841 file_move(filp, &tty->tty_files);
1842 check_tty_count(tty, "tty_open");
1843 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1844 tty->driver->subtype == PTY_TYPE_MASTER)
1846 #ifdef TTY_DEBUG_HANGUP
1847 printk(KERN_DEBUG "opening %s...", tty->name);
1851 retval = tty->ops->open(tty, filp);
1855 filp->f_flags = saved_flags;
1857 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1858 !capable(CAP_SYS_ADMIN))
1862 #ifdef TTY_DEBUG_HANGUP
1863 printk(KERN_DEBUG "error %d in opening %s...", retval,
1867 if (retval != -ERESTARTSYS)
1869 if (signal_pending(current))
1873 * Need to reset f_op in case a hangup happened.
1875 if (filp->f_op == &hung_up_tty_fops)
1876 filp->f_op = &tty_fops;
1880 mutex_lock(&tty_mutex);
1881 spin_lock_irq(¤t->sighand->siglock);
1883 current->signal->leader &&
1884 !current->signal->tty &&
1885 tty->session == NULL)
1886 __proc_set_tty(current, tty);
1887 spin_unlock_irq(¤t->sighand->siglock);
1888 mutex_unlock(&tty_mutex);
1892 /* BKL pushdown: scary code avoidance wrapper */
1893 static int tty_open(struct inode *inode, struct file *filp)
1898 ret = __tty_open(inode, filp);
1905 #ifdef CONFIG_UNIX98_PTYS
1907 * ptmx_open - open a unix 98 pty master
1908 * @inode: inode of device file
1909 * @filp: file pointer to tty
1911 * Allocate a unix98 pty master device from the ptmx driver.
1913 * Locking: tty_mutex protects theinit_dev work. tty->count should
1915 * allocated_ptys_lock handles the list of free pty numbers
1918 static int __ptmx_open(struct inode *inode, struct file *filp)
1920 struct tty_struct *tty;
1924 nonseekable_open(inode, filp);
1926 /* find a device that is not in use. */
1927 index = devpts_new_index();
1931 mutex_lock(&tty_mutex);
1932 retval = init_dev(ptm_driver, index, &tty);
1933 mutex_unlock(&tty_mutex);
1938 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1939 filp->private_data = tty;
1940 file_move(filp, &tty->tty_files);
1942 retval = devpts_pty_new(tty->link);
1946 check_tty_count(tty, "ptmx_open");
1947 retval = ptm_driver->ops->open(tty, filp);
1954 devpts_kill_index(index);
1958 static int ptmx_open(struct inode *inode, struct file *filp)
1963 ret = __ptmx_open(inode, filp);
1970 * tty_release - vfs callback for close
1971 * @inode: inode of tty
1972 * @filp: file pointer for handle to tty
1974 * Called the last time each file handle is closed that references
1975 * this tty. There may however be several such references.
1978 * Takes bkl. See release_dev
1981 static int tty_release(struct inode *inode, struct file *filp)
1990 * tty_poll - check tty status
1991 * @filp: file being polled
1992 * @wait: poll wait structures to update
1994 * Call the line discipline polling method to obtain the poll
1995 * status of the device.
1997 * Locking: locks called line discipline but ldisc poll method
1998 * may be re-entered freely by other callers.
2001 static unsigned int tty_poll(struct file *filp, poll_table *wait)
2003 struct tty_struct *tty;
2004 struct tty_ldisc *ld;
2007 tty = (struct tty_struct *)filp->private_data;
2008 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
2011 ld = tty_ldisc_ref_wait(tty);
2013 ret = (ld->ops->poll)(tty, filp, wait);
2014 tty_ldisc_deref(ld);
2018 static int tty_fasync(int fd, struct file *filp, int on)
2020 struct tty_struct *tty;
2021 unsigned long flags;
2025 tty = (struct tty_struct *)filp->private_data;
2026 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
2029 retval = fasync_helper(fd, filp, on, &tty->fasync);
2036 if (!waitqueue_active(&tty->read_wait))
2037 tty->minimum_to_wake = 1;
2038 spin_lock_irqsave(&tty->ctrl_lock, flags);
2041 type = PIDTYPE_PGID;
2043 pid = task_pid(current);
2046 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2047 retval = __f_setown(filp, pid, type, 0);
2051 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2052 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2061 * tiocsti - fake input character
2062 * @tty: tty to fake input into
2063 * @p: pointer to character
2065 * Fake input to a tty device. Does the necessary locking and
2068 * FIXME: does not honour flow control ??
2071 * Called functions take tty_ldisc_lock
2072 * current->signal->tty check is safe without locks
2074 * FIXME: may race normal receive processing
2077 static int tiocsti(struct tty_struct *tty, char __user *p)
2080 struct tty_ldisc *ld;
2082 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2084 if (get_user(ch, p))
2086 ld = tty_ldisc_ref_wait(tty);
2087 ld->ops->receive_buf(tty, &ch, &mbz, 1);
2088 tty_ldisc_deref(ld);
2093 * tiocgwinsz - implement window query ioctl
2095 * @arg: user buffer for result
2097 * Copies the kernel idea of the window size into the user buffer.
2099 * Locking: tty->termios_mutex is taken to ensure the winsize data
2103 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
2107 mutex_lock(&tty->termios_mutex);
2108 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2109 mutex_unlock(&tty->termios_mutex);
2111 return err ? -EFAULT: 0;
2115 * tty_do_resize - resize event
2116 * @tty: tty being resized
2117 * @real_tty: real tty (not the same as tty if using a pty/tty pair)
2118 * @rows: rows (character)
2119 * @cols: cols (character)
2121 * Update the termios variables and send the neccessary signals to
2122 * peform a terminal resize correctly
2125 int tty_do_resize(struct tty_struct *tty, struct tty_struct *real_tty,
2128 struct pid *pgrp, *rpgrp;
2129 unsigned long flags;
2131 /* For a PTY we need to lock the tty side */
2132 mutex_lock(&real_tty->termios_mutex);
2133 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2135 /* Get the PID values and reference them so we can
2136 avoid holding the tty ctrl lock while sending signals */
2137 spin_lock_irqsave(&tty->ctrl_lock, flags);
2138 pgrp = get_pid(tty->pgrp);
2139 rpgrp = get_pid(real_tty->pgrp);
2140 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2143 kill_pgrp(pgrp, SIGWINCH, 1);
2144 if (rpgrp != pgrp && rpgrp)
2145 kill_pgrp(rpgrp, SIGWINCH, 1);
2151 real_tty->winsize = *ws;
2153 mutex_unlock(&real_tty->termios_mutex);
2158 * tiocswinsz - implement window size set ioctl
2160 * @arg: user buffer for result
2162 * Copies the user idea of the window size to the kernel. Traditionally
2163 * this is just advisory information but for the Linux console it
2164 * actually has driver level meaning and triggers a VC resize.
2167 * Driver dependant. The default do_resize method takes the
2168 * tty termios mutex and ctrl_lock. The console takes its own lock
2169 * then calls into the default method.
2172 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
2173 struct winsize __user *arg)
2175 struct winsize tmp_ws;
2176 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2179 if (tty->ops->resize)
2180 return tty->ops->resize(tty, real_tty, &tmp_ws);
2182 return tty_do_resize(tty, real_tty, &tmp_ws);
2186 * tioccons - allow admin to move logical console
2187 * @file: the file to become console
2189 * Allow the adminstrator to move the redirected console device
2191 * Locking: uses redirect_lock to guard the redirect information
2194 static int tioccons(struct file *file)
2196 if (!capable(CAP_SYS_ADMIN))
2198 if (file->f_op->write == redirected_tty_write) {
2200 spin_lock(&redirect_lock);
2203 spin_unlock(&redirect_lock);
2208 spin_lock(&redirect_lock);
2210 spin_unlock(&redirect_lock);
2215 spin_unlock(&redirect_lock);
2220 * fionbio - non blocking ioctl
2221 * @file: file to set blocking value
2222 * @p: user parameter
2224 * Historical tty interfaces had a blocking control ioctl before
2225 * the generic functionality existed. This piece of history is preserved
2226 * in the expected tty API of posix OS's.
2228 * Locking: none, the open fle handle ensures it won't go away.
2231 static int fionbio(struct file *file, int __user *p)
2235 if (get_user(nonblock, p))
2238 /* file->f_flags is still BKL protected in the fs layer - vomit */
2241 file->f_flags |= O_NONBLOCK;
2243 file->f_flags &= ~O_NONBLOCK;
2249 * tiocsctty - set controlling tty
2250 * @tty: tty structure
2251 * @arg: user argument
2253 * This ioctl is used to manage job control. It permits a session
2254 * leader to set this tty as the controlling tty for the session.
2257 * Takes tty_mutex() to protect tty instance
2258 * Takes tasklist_lock internally to walk sessions
2259 * Takes ->siglock() when updating signal->tty
2262 static int tiocsctty(struct tty_struct *tty, int arg)
2265 if (current->signal->leader && (task_session(current) == tty->session))
2268 mutex_lock(&tty_mutex);
2270 * The process must be a session leader and
2271 * not have a controlling tty already.
2273 if (!current->signal->leader || current->signal->tty) {
2280 * This tty is already the controlling
2281 * tty for another session group!
2283 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
2287 read_lock(&tasklist_lock);
2288 session_clear_tty(tty->session);
2289 read_unlock(&tasklist_lock);
2295 proc_set_tty(current, tty);
2297 mutex_unlock(&tty_mutex);
2302 * tty_get_pgrp - return a ref counted pgrp pid
2305 * Returns a refcounted instance of the pid struct for the process
2306 * group controlling the tty.
2309 struct pid *tty_get_pgrp(struct tty_struct *tty)
2311 unsigned long flags;
2314 spin_lock_irqsave(&tty->ctrl_lock, flags);
2315 pgrp = get_pid(tty->pgrp);
2316 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2320 EXPORT_SYMBOL_GPL(tty_get_pgrp);
2323 * tiocgpgrp - get process group
2324 * @tty: tty passed by user
2325 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2328 * Obtain the process group of the tty. If there is no process group
2331 * Locking: none. Reference to current->signal->tty is safe.
2334 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2339 * (tty == real_tty) is a cheap way of
2340 * testing if the tty is NOT a master pty.
2342 if (tty == real_tty && current->signal->tty != real_tty)
2344 pid = tty_get_pgrp(real_tty);
2345 ret = put_user(pid_vnr(pid), p);
2351 * tiocspgrp - attempt to set process group
2352 * @tty: tty passed by user
2353 * @real_tty: tty side device matching tty passed by user
2356 * Set the process group of the tty to the session passed. Only
2357 * permitted where the tty session is our session.
2359 * Locking: RCU, ctrl lock
2362 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2366 int retval = tty_check_change(real_tty);
2367 unsigned long flags;
2373 if (!current->signal->tty ||
2374 (current->signal->tty != real_tty) ||
2375 (real_tty->session != task_session(current)))
2377 if (get_user(pgrp_nr, p))
2382 pgrp = find_vpid(pgrp_nr);
2387 if (session_of_pgrp(pgrp) != task_session(current))
2390 spin_lock_irqsave(&tty->ctrl_lock, flags);
2391 put_pid(real_tty->pgrp);
2392 real_tty->pgrp = get_pid(pgrp);
2393 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2400 * tiocgsid - get session id
2401 * @tty: tty passed by user
2402 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2403 * @p: pointer to returned session id
2405 * Obtain the session id of the tty. If there is no session
2408 * Locking: none. Reference to current->signal->tty is safe.
2411 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2414 * (tty == real_tty) is a cheap way of
2415 * testing if the tty is NOT a master pty.
2417 if (tty == real_tty && current->signal->tty != real_tty)
2419 if (!real_tty->session)
2421 return put_user(pid_vnr(real_tty->session), p);
2425 * tiocsetd - set line discipline
2427 * @p: pointer to user data
2429 * Set the line discipline according to user request.
2431 * Locking: see tty_set_ldisc, this function is just a helper
2434 static int tiocsetd(struct tty_struct *tty, int __user *p)
2439 if (get_user(ldisc, p))
2443 ret = tty_set_ldisc(tty, ldisc);
2450 * send_break - performed time break
2451 * @tty: device to break on
2452 * @duration: timeout in mS
2454 * Perform a timed break on hardware that lacks its own driver level
2455 * timed break functionality.
2458 * atomic_write_lock serializes
2462 static int send_break(struct tty_struct *tty, unsigned int duration)
2466 if (tty->ops->break_ctl == NULL)
2469 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2470 retval = tty->ops->break_ctl(tty, duration);
2472 /* Do the work ourselves */
2473 if (tty_write_lock(tty, 0) < 0)
2475 retval = tty->ops->break_ctl(tty, -1);
2478 if (!signal_pending(current))
2479 msleep_interruptible(duration);
2480 retval = tty->ops->break_ctl(tty, 0);
2482 tty_write_unlock(tty);
2483 if (signal_pending(current))
2490 * tty_tiocmget - get modem status
2492 * @file: user file pointer
2493 * @p: pointer to result
2495 * Obtain the modem status bits from the tty driver if the feature
2496 * is supported. Return -EINVAL if it is not available.
2498 * Locking: none (up to the driver)
2501 static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
2503 int retval = -EINVAL;
2505 if (tty->ops->tiocmget) {
2506 retval = tty->ops->tiocmget(tty, file);
2509 retval = put_user(retval, p);
2515 * tty_tiocmset - set modem status
2517 * @file: user file pointer
2518 * @cmd: command - clear bits, set bits or set all
2519 * @p: pointer to desired bits
2521 * Set the modem status bits from the tty driver if the feature
2522 * is supported. Return -EINVAL if it is not available.
2524 * Locking: none (up to the driver)
2527 static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
2531 unsigned int set, clear, val;
2533 if (tty->ops->tiocmset == NULL)
2536 retval = get_user(val, p);
2552 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2553 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2554 return tty->ops->tiocmset(tty, file, set, clear);
2558 * Split this up, as gcc can choke on it otherwise..
2560 long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2562 struct tty_struct *tty, *real_tty;
2563 void __user *p = (void __user *)arg;
2565 struct tty_ldisc *ld;
2566 struct inode *inode = file->f_dentry->d_inode;
2568 tty = (struct tty_struct *)file->private_data;
2569 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2573 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2574 tty->driver->subtype == PTY_TYPE_MASTER)
2575 real_tty = tty->link;
2579 * Factor out some common prep work
2587 retval = tty_check_change(tty);
2590 if (cmd != TIOCCBRK) {
2591 tty_wait_until_sent(tty, 0);
2592 if (signal_pending(current))
2603 return tiocsti(tty, p);
2605 return tiocgwinsz(tty, p);
2607 return tiocswinsz(tty, real_tty, p);
2609 return real_tty != tty ? -EINVAL : tioccons(file);
2611 return fionbio(file, p);
2613 set_bit(TTY_EXCLUSIVE, &tty->flags);
2616 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2619 if (current->signal->tty != tty)
2624 return tiocsctty(tty, arg);
2626 return tiocgpgrp(tty, real_tty, p);
2628 return tiocspgrp(tty, real_tty, p);
2630 return tiocgsid(tty, real_tty, p);
2632 return put_user(tty->ldisc.ops->num, (int __user *)p);
2634 return tiocsetd(tty, p);
2638 case TIOCSBRK: /* Turn break on, unconditionally */
2639 if (tty->ops->break_ctl)
2640 return tty->ops->break_ctl(tty, -1);
2642 case TIOCCBRK: /* Turn break off, unconditionally */
2643 if (tty->ops->break_ctl)
2644 return tty->ops->break_ctl(tty, 0);
2646 case TCSBRK: /* SVID version: non-zero arg --> no break */
2647 /* non-zero arg means wait for all output data
2648 * to be sent (performed above) but don't send break.
2649 * This is used by the tcdrain() termios function.
2652 return send_break(tty, 250);
2654 case TCSBRKP: /* support for POSIX tcsendbreak() */
2655 return send_break(tty, arg ? arg*100 : 250);
2658 return tty_tiocmget(tty, file, p);
2662 return tty_tiocmset(tty, file, cmd, p);
2667 /* flush tty buffer and allow ldisc to process ioctl */
2668 tty_buffer_flush(tty);
2673 if (tty->ops->ioctl) {
2674 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
2675 if (retval != -ENOIOCTLCMD)
2678 ld = tty_ldisc_ref_wait(tty);
2680 if (ld->ops->ioctl) {
2681 retval = ld->ops->ioctl(tty, file, cmd, arg);
2682 if (retval == -ENOIOCTLCMD)
2685 tty_ldisc_deref(ld);
2689 #ifdef CONFIG_COMPAT
2690 static long tty_compat_ioctl(struct file *file, unsigned int cmd,
2693 struct inode *inode = file->f_dentry->d_inode;
2694 struct tty_struct *tty = file->private_data;
2695 struct tty_ldisc *ld;
2696 int retval = -ENOIOCTLCMD;
2698 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2701 if (tty->ops->compat_ioctl) {
2702 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
2703 if (retval != -ENOIOCTLCMD)
2707 ld = tty_ldisc_ref_wait(tty);
2708 if (ld->ops->compat_ioctl)
2709 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
2710 tty_ldisc_deref(ld);
2717 * This implements the "Secure Attention Key" --- the idea is to
2718 * prevent trojan horses by killing all processes associated with this
2719 * tty when the user hits the "Secure Attention Key". Required for
2720 * super-paranoid applications --- see the Orange Book for more details.
2722 * This code could be nicer; ideally it should send a HUP, wait a few
2723 * seconds, then send a INT, and then a KILL signal. But you then
2724 * have to coordinate with the init process, since all processes associated
2725 * with the current tty must be dead before the new getty is allowed
2728 * Now, if it would be correct ;-/ The current code has a nasty hole -
2729 * it doesn't catch files in flight. We may send the descriptor to ourselves
2730 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2732 * Nasty bug: do_SAK is being called in interrupt context. This can
2733 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2735 void __do_SAK(struct tty_struct *tty)
2740 struct task_struct *g, *p;
2741 struct pid *session;
2744 struct fdtable *fdt;
2748 session = tty->session;
2750 tty_ldisc_flush(tty);
2752 tty_driver_flush_buffer(tty);
2754 read_lock(&tasklist_lock);
2755 /* Kill the entire session */
2756 do_each_pid_task(session, PIDTYPE_SID, p) {
2757 printk(KERN_NOTICE "SAK: killed process %d"
2758 " (%s): task_session_nr(p)==tty->session\n",
2759 task_pid_nr(p), p->comm);
2760 send_sig(SIGKILL, p, 1);
2761 } while_each_pid_task(session, PIDTYPE_SID, p);
2762 /* Now kill any processes that happen to have the
2765 do_each_thread(g, p) {
2766 if (p->signal->tty == tty) {
2767 printk(KERN_NOTICE "SAK: killed process %d"
2768 " (%s): task_session_nr(p)==tty->session\n",
2769 task_pid_nr(p), p->comm);
2770 send_sig(SIGKILL, p, 1);
2776 * We don't take a ref to the file, so we must
2777 * hold ->file_lock instead.
2779 spin_lock(&p->files->file_lock);
2780 fdt = files_fdtable(p->files);
2781 for (i = 0; i < fdt->max_fds; i++) {
2782 filp = fcheck_files(p->files, i);
2785 if (filp->f_op->read == tty_read &&
2786 filp->private_data == tty) {
2787 printk(KERN_NOTICE "SAK: killed process %d"
2788 " (%s): fd#%d opened to the tty\n",
2789 task_pid_nr(p), p->comm, i);
2790 force_sig(SIGKILL, p);
2794 spin_unlock(&p->files->file_lock);
2797 } while_each_thread(g, p);
2798 read_unlock(&tasklist_lock);
2802 static void do_SAK_work(struct work_struct *work)
2804 struct tty_struct *tty =
2805 container_of(work, struct tty_struct, SAK_work);
2810 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2811 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2812 * the values which we write to it will be identical to the values which it
2813 * already has. --akpm
2815 void do_SAK(struct tty_struct *tty)
2819 schedule_work(&tty->SAK_work);
2822 EXPORT_SYMBOL(do_SAK);
2825 * initialize_tty_struct
2826 * @tty: tty to initialize
2828 * This subroutine initializes a tty structure that has been newly
2831 * Locking: none - tty in question must not be exposed at this point
2834 static void initialize_tty_struct(struct tty_struct *tty)
2836 memset(tty, 0, sizeof(struct tty_struct));
2837 tty->magic = TTY_MAGIC;
2838 tty_ldisc_init(tty);
2839 tty->session = NULL;
2841 tty->overrun_time = jiffies;
2842 tty->buf.head = tty->buf.tail = NULL;
2843 tty_buffer_init(tty);
2844 mutex_init(&tty->termios_mutex);
2845 init_waitqueue_head(&tty->write_wait);
2846 init_waitqueue_head(&tty->read_wait);
2847 INIT_WORK(&tty->hangup_work, do_tty_hangup);
2848 mutex_init(&tty->atomic_read_lock);
2849 mutex_init(&tty->atomic_write_lock);
2850 spin_lock_init(&tty->read_lock);
2851 spin_lock_init(&tty->ctrl_lock);
2852 INIT_LIST_HEAD(&tty->tty_files);
2853 INIT_WORK(&tty->SAK_work, do_SAK_work);
2857 * tty_put_char - write one character to a tty
2861 * Write one byte to the tty using the provided put_char method
2862 * if present. Returns the number of characters successfully output.
2864 * Note: the specific put_char operation in the driver layer may go
2865 * away soon. Don't call it directly, use this method
2868 int tty_put_char(struct tty_struct *tty, unsigned char ch)
2870 if (tty->ops->put_char)
2871 return tty->ops->put_char(tty, ch);
2872 return tty->ops->write(tty, &ch, 1);
2875 EXPORT_SYMBOL_GPL(tty_put_char);
2877 static struct class *tty_class;
2880 * tty_register_device - register a tty device
2881 * @driver: the tty driver that describes the tty device
2882 * @index: the index in the tty driver for this tty device
2883 * @device: a struct device that is associated with this tty device.
2884 * This field is optional, if there is no known struct device
2885 * for this tty device it can be set to NULL safely.
2887 * Returns a pointer to the struct device for this tty device
2888 * (or ERR_PTR(-EFOO) on error).
2890 * This call is required to be made to register an individual tty device
2891 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2892 * that bit is not set, this function should not be called by a tty
2898 struct device *tty_register_device(struct tty_driver *driver, unsigned index,
2899 struct device *device)
2902 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2904 if (index >= driver->num) {
2905 printk(KERN_ERR "Attempt to register invalid tty line number "
2907 return ERR_PTR(-EINVAL);
2910 if (driver->type == TTY_DRIVER_TYPE_PTY)
2911 pty_line_name(driver, index, name);
2913 tty_line_name(driver, index, name);
2915 return device_create_drvdata(tty_class, device, dev, NULL, name);
2919 * tty_unregister_device - unregister a tty device
2920 * @driver: the tty driver that describes the tty device
2921 * @index: the index in the tty driver for this tty device
2923 * If a tty device is registered with a call to tty_register_device() then
2924 * this function must be called when the tty device is gone.
2929 void tty_unregister_device(struct tty_driver *driver, unsigned index)
2931 device_destroy(tty_class,
2932 MKDEV(driver->major, driver->minor_start) + index);
2935 EXPORT_SYMBOL(tty_register_device);
2936 EXPORT_SYMBOL(tty_unregister_device);
2938 struct tty_driver *alloc_tty_driver(int lines)
2940 struct tty_driver *driver;
2942 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
2944 driver->magic = TTY_DRIVER_MAGIC;
2945 driver->num = lines;
2946 /* later we'll move allocation of tables here */
2951 void put_tty_driver(struct tty_driver *driver)
2956 void tty_set_operations(struct tty_driver *driver,
2957 const struct tty_operations *op)
2962 EXPORT_SYMBOL(alloc_tty_driver);
2963 EXPORT_SYMBOL(put_tty_driver);
2964 EXPORT_SYMBOL(tty_set_operations);
2967 * Called by a tty driver to register itself.
2969 int tty_register_driver(struct tty_driver *driver)
2976 if (driver->flags & TTY_DRIVER_INSTALLED)
2979 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
2980 p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
2985 if (!driver->major) {
2986 error = alloc_chrdev_region(&dev, driver->minor_start,
2987 driver->num, driver->name);
2989 driver->major = MAJOR(dev);
2990 driver->minor_start = MINOR(dev);
2993 dev = MKDEV(driver->major, driver->minor_start);
2994 error = register_chrdev_region(dev, driver->num, driver->name);
3002 driver->ttys = (struct tty_struct **)p;
3003 driver->termios = (struct ktermios **)(p + driver->num);
3004 driver->termios_locked = (struct ktermios **)
3005 (p + driver->num * 2);
3007 driver->ttys = NULL;
3008 driver->termios = NULL;
3009 driver->termios_locked = NULL;
3012 cdev_init(&driver->cdev, &tty_fops);
3013 driver->cdev.owner = driver->owner;
3014 error = cdev_add(&driver->cdev, dev, driver->num);
3016 unregister_chrdev_region(dev, driver->num);
3017 driver->ttys = NULL;
3018 driver->termios = driver->termios_locked = NULL;
3023 mutex_lock(&tty_mutex);
3024 list_add(&driver->tty_drivers, &tty_drivers);
3025 mutex_unlock(&tty_mutex);
3027 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
3028 for (i = 0; i < driver->num; i++)
3029 tty_register_device(driver, i, NULL);
3031 proc_tty_register_driver(driver);
3035 EXPORT_SYMBOL(tty_register_driver);
3038 * Called by a tty driver to unregister itself.
3040 int tty_unregister_driver(struct tty_driver *driver)
3043 struct ktermios *tp;
3046 if (driver->refcount)
3049 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3051 mutex_lock(&tty_mutex);
3052 list_del(&driver->tty_drivers);
3053 mutex_unlock(&tty_mutex);
3056 * Free the termios and termios_locked structures because
3057 * we don't want to get memory leaks when modular tty
3058 * drivers are removed from the kernel.
3060 for (i = 0; i < driver->num; i++) {
3061 tp = driver->termios[i];
3063 driver->termios[i] = NULL;
3066 tp = driver->termios_locked[i];
3068 driver->termios_locked[i] = NULL;
3071 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3072 tty_unregister_device(driver, i);
3075 proc_tty_unregister_driver(driver);
3076 driver->ttys = NULL;
3077 driver->termios = driver->termios_locked = NULL;
3079 cdev_del(&driver->cdev);
3082 EXPORT_SYMBOL(tty_unregister_driver);
3084 dev_t tty_devnum(struct tty_struct *tty)
3086 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3088 EXPORT_SYMBOL(tty_devnum);
3090 void proc_clear_tty(struct task_struct *p)
3092 spin_lock_irq(&p->sighand->siglock);
3093 p->signal->tty = NULL;
3094 spin_unlock_irq(&p->sighand->siglock);
3097 /* Called under the sighand lock */
3099 static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3102 unsigned long flags;
3103 /* We should not have a session or pgrp to put here but.... */
3104 spin_lock_irqsave(&tty->ctrl_lock, flags);
3105 put_pid(tty->session);
3107 tty->pgrp = get_pid(task_pgrp(tsk));
3108 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3109 tty->session = get_pid(task_session(tsk));
3111 put_pid(tsk->signal->tty_old_pgrp);
3112 tsk->signal->tty = tty;
3113 tsk->signal->tty_old_pgrp = NULL;
3116 static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3118 spin_lock_irq(&tsk->sighand->siglock);
3119 __proc_set_tty(tsk, tty);
3120 spin_unlock_irq(&tsk->sighand->siglock);
3123 struct tty_struct *get_current_tty(void)
3125 struct tty_struct *tty;
3126 WARN_ON_ONCE(!mutex_is_locked(&tty_mutex));
3127 tty = current->signal->tty;
3129 * session->tty can be changed/cleared from under us, make sure we
3130 * issue the load. The obtained pointer, when not NULL, is valid as
3131 * long as we hold tty_mutex.
3136 EXPORT_SYMBOL_GPL(get_current_tty);
3139 * Initialize the console device. This is called *early*, so
3140 * we can't necessarily depend on lots of kernel help here.
3141 * Just do some early initializations, and do the complex setup
3144 void __init console_init(void)
3148 /* Setup the default TTY line discipline. */
3152 * set up the console device so that later boot sequences can
3153 * inform about problems etc..
3155 call = __con_initcall_start;
3156 while (call < __con_initcall_end) {
3162 static int __init tty_class_init(void)
3164 tty_class = class_create(THIS_MODULE, "tty");
3165 if (IS_ERR(tty_class))
3166 return PTR_ERR(tty_class);
3170 postcore_initcall(tty_class_init);
3172 /* 3/2004 jmc: why do these devices exist? */
3174 static struct cdev tty_cdev, console_cdev;
3175 #ifdef CONFIG_UNIX98_PTYS
3176 static struct cdev ptmx_cdev;
3179 static struct cdev vc0_cdev;
3183 * Ok, now we can initialize the rest of the tty devices and can count
3184 * on memory allocations, interrupts etc..
3186 static int __init tty_init(void)
3188 cdev_init(&tty_cdev, &tty_fops);
3189 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3190 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3191 panic("Couldn't register /dev/tty driver\n");
3192 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
3195 cdev_init(&console_cdev, &console_fops);
3196 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3197 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3198 panic("Couldn't register /dev/console driver\n");
3199 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
3202 #ifdef CONFIG_UNIX98_PTYS
3203 cdev_init(&ptmx_cdev, &ptmx_fops);
3204 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
3205 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
3206 panic("Couldn't register /dev/ptmx driver\n");
3207 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
3211 cdev_init(&vc0_cdev, &console_fops);
3212 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3213 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3214 panic("Couldn't register /dev/tty0 driver\n");
3215 device_create_drvdata(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
3221 module_init(tty_init);