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 static char ptychar[] = "pqrstuvwxyzabcde";
1145 * pty_line_name - generate name for a pty
1146 * @driver: the tty driver in use
1147 * @index: the minor number
1148 * @p: output buffer of at least 6 bytes
1150 * Generate a name from a driver reference and write it to the output
1155 static void pty_line_name(struct tty_driver *driver, int index, char *p)
1157 int i = index + driver->name_base;
1158 /* ->name is initialized to "ttyp", but "tty" is expected */
1159 sprintf(p, "%s%c%x",
1160 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1161 ptychar[i >> 4 & 0xf], i & 0xf);
1165 * pty_line_name - generate name for a tty
1166 * @driver: the tty driver in use
1167 * @index: the minor number
1168 * @p: output buffer of at least 7 bytes
1170 * Generate a name from a driver reference and write it to the output
1175 static void tty_line_name(struct tty_driver *driver, int index, char *p)
1177 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1181 * init_dev - initialise a tty device
1182 * @driver: tty driver we are opening a device on
1183 * @idx: device index
1184 * @tty: returned tty structure
1186 * Prepare a tty device. This may not be a "new" clean device but
1187 * could also be an active device. The pty drivers require special
1188 * handling because of this.
1191 * The function is called under the tty_mutex, which
1192 * protects us from the tty struct or driver itself going away.
1194 * On exit the tty device has the line discipline attached and
1195 * a reference count of 1. If a pair was created for pty/tty use
1196 * and the other was a pty master then it too has a reference count of 1.
1198 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1199 * failed open. The new code protects the open with a mutex, so it's
1200 * really quite straightforward. The mutex locking can probably be
1201 * relaxed for the (most common) case of reopening a tty.
1204 static int init_dev(struct tty_driver *driver, int idx,
1205 struct tty_struct **ret_tty)
1207 struct tty_struct *tty, *o_tty;
1208 struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
1209 struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
1212 /* check whether we're reopening an existing tty */
1213 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1214 tty = devpts_get_tty(idx);
1216 * If we don't have a tty here on a slave open, it's because
1217 * the master already started the close process and there's
1218 * no relation between devpts file and tty anymore.
1220 if (!tty && driver->subtype == PTY_TYPE_SLAVE) {
1225 * It's safe from now on because init_dev() is called with
1226 * tty_mutex held and release_dev() won't change tty->count
1227 * or tty->flags without having to grab tty_mutex
1229 if (tty && driver->subtype == PTY_TYPE_MASTER)
1232 tty = driver->ttys[idx];
1234 if (tty) goto fast_track;
1237 * First time open is complex, especially for PTY devices.
1238 * This code guarantees that either everything succeeds and the
1239 * TTY is ready for operation, or else the table slots are vacated
1240 * and the allocated memory released. (Except that the termios
1241 * and locked termios may be retained.)
1244 if (!try_module_get(driver->owner)) {
1253 tty = alloc_tty_struct();
1256 initialize_tty_struct(tty);
1257 tty->driver = driver;
1258 tty->ops = driver->ops;
1260 tty_line_name(driver, idx, tty->name);
1262 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1263 tp_loc = &tty->termios;
1264 ltp_loc = &tty->termios_locked;
1266 tp_loc = &driver->termios[idx];
1267 ltp_loc = &driver->termios_locked[idx];
1271 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1274 *tp = driver->init_termios;
1278 ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
1283 if (driver->type == TTY_DRIVER_TYPE_PTY) {
1284 o_tty = alloc_tty_struct();
1287 initialize_tty_struct(o_tty);
1288 o_tty->driver = driver->other;
1289 o_tty->ops = driver->ops;
1291 tty_line_name(driver->other, idx, o_tty->name);
1293 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1294 o_tp_loc = &o_tty->termios;
1295 o_ltp_loc = &o_tty->termios_locked;
1297 o_tp_loc = &driver->other->termios[idx];
1298 o_ltp_loc = &driver->other->termios_locked[idx];
1302 o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1305 *o_tp = driver->other->init_termios;
1309 o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
1315 * Everything allocated ... set up the o_tty structure.
1317 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM))
1318 driver->other->ttys[idx] = o_tty;
1323 o_tty->termios = *o_tp_loc;
1324 o_tty->termios_locked = *o_ltp_loc;
1325 driver->other->refcount++;
1326 if (driver->subtype == PTY_TYPE_MASTER)
1329 /* Establish the links in both directions */
1335 * All structures have been allocated, so now we install them.
1336 * Failures after this point use release_tty to clean up, so
1337 * there's no need to null out the local pointers.
1339 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM))
1340 driver->ttys[idx] = tty;
1346 tty->termios = *tp_loc;
1347 tty->termios_locked = *ltp_loc;
1348 /* Compatibility until drivers always set this */
1349 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1350 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1355 * Structures all installed ... call the ldisc open routines.
1356 * If we fail here just call release_tty to clean up. No need
1357 * to decrement the use counts, as release_tty doesn't care.
1360 retval = tty_ldisc_setup(tty, o_tty);
1363 goto release_mem_out;
1367 * This fast open can be used if the tty is already open.
1368 * No memory is allocated, and the only failures are from
1369 * attempting to open a closing tty or attempting multiple
1370 * opens on a pty master.
1373 if (test_bit(TTY_CLOSING, &tty->flags)) {
1377 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1378 driver->subtype == PTY_TYPE_MASTER) {
1380 * special case for PTY masters: only one open permitted,
1381 * and the slave side open count is incremented as well.
1390 tty->driver = driver; /* N.B. why do this every time?? */
1393 if (!test_bit(TTY_LDISC, &tty->flags))
1394 printk(KERN_ERR "init_dev but no ldisc\n");
1398 /* All paths come through here to release the mutex */
1402 /* Release locally allocated memory ... nothing placed in slots */
1406 free_tty_struct(o_tty);
1409 free_tty_struct(tty);
1412 module_put(driver->owner);
1416 /* call the tty release_tty routine to clean out this slot */
1418 if (printk_ratelimit())
1419 printk(KERN_INFO "init_dev: ldisc open failed, "
1420 "clearing slot %d\n", idx);
1421 release_tty(tty, idx);
1426 * release_one_tty - release tty structure memory
1428 * Releases memory associated with a tty structure, and clears out the
1429 * driver table slots. This function is called when a device is no longer
1430 * in use. It also gets called when setup of a device fails.
1433 * tty_mutex - sometimes only
1434 * takes the file list lock internally when working on the list
1435 * of ttys that the driver keeps.
1436 * FIXME: should we require tty_mutex is held here ??
1438 static void release_one_tty(struct tty_struct *tty, int idx)
1440 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
1441 struct ktermios *tp;
1444 tty->driver->ttys[idx] = NULL;
1446 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1449 tty->driver->termios[idx] = NULL;
1452 tp = tty->termios_locked;
1454 tty->driver->termios_locked[idx] = NULL;
1460 tty->driver->refcount--;
1463 list_del_init(&tty->tty_files);
1466 free_tty_struct(tty);
1470 * release_tty - release tty structure memory
1472 * Release both @tty and a possible linked partner (think pty pair),
1473 * and decrement the refcount of the backing module.
1476 * tty_mutex - sometimes only
1477 * takes the file list lock internally when working on the list
1478 * of ttys that the driver keeps.
1479 * FIXME: should we require tty_mutex is held here ??
1481 static void release_tty(struct tty_struct *tty, int idx)
1483 struct tty_driver *driver = tty->driver;
1486 release_one_tty(tty->link, idx);
1487 release_one_tty(tty, idx);
1488 module_put(driver->owner);
1492 * Even releasing the tty structures is a tricky business.. We have
1493 * to be very careful that the structures are all released at the
1494 * same time, as interrupts might otherwise get the wrong pointers.
1496 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1497 * lead to double frees or releasing memory still in use.
1499 static void release_dev(struct file *filp)
1501 struct tty_struct *tty, *o_tty;
1502 int pty_master, tty_closing, o_tty_closing, do_sleep;
1507 tty = (struct tty_struct *)filp->private_data;
1508 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode,
1512 check_tty_count(tty, "release_dev");
1514 tty_fasync(-1, filp, 0);
1517 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1518 tty->driver->subtype == PTY_TYPE_MASTER);
1519 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1522 #ifdef TTY_PARANOIA_CHECK
1523 if (idx < 0 || idx >= tty->driver->num) {
1524 printk(KERN_DEBUG "release_dev: bad idx when trying to "
1525 "free (%s)\n", tty->name);
1528 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1529 if (tty != tty->driver->ttys[idx]) {
1530 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1531 "for (%s)\n", idx, tty->name);
1534 if (tty->termios != tty->driver->termios[idx]) {
1535 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1540 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
1541 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1542 "termios_locked for (%s)\n",
1549 #ifdef TTY_DEBUG_HANGUP
1550 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1551 tty_name(tty, buf), tty->count);
1554 #ifdef TTY_PARANOIA_CHECK
1555 if (tty->driver->other &&
1556 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1557 if (o_tty != tty->driver->other->ttys[idx]) {
1558 printk(KERN_DEBUG "release_dev: other->table[%d] "
1559 "not o_tty for (%s)\n",
1563 if (o_tty->termios != tty->driver->other->termios[idx]) {
1564 printk(KERN_DEBUG "release_dev: other->termios[%d] "
1565 "not o_termios for (%s)\n",
1569 if (o_tty->termios_locked !=
1570 tty->driver->other->termios_locked[idx]) {
1571 printk(KERN_DEBUG "release_dev: other->termios_locked["
1572 "%d] not o_termios_locked for (%s)\n",
1576 if (o_tty->link != tty) {
1577 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
1582 if (tty->ops->close)
1583 tty->ops->close(tty, filp);
1586 * Sanity check: if tty->count is going to zero, there shouldn't be
1587 * any waiters on tty->read_wait or tty->write_wait. We test the
1588 * wait queues and kick everyone out _before_ actually starting to
1589 * close. This ensures that we won't block while releasing the tty
1592 * The test for the o_tty closing is necessary, since the master and
1593 * slave sides may close in any order. If the slave side closes out
1594 * first, its count will be one, since the master side holds an open.
1595 * Thus this test wouldn't be triggered at the time the slave closes,
1598 * Note that it's possible for the tty to be opened again while we're
1599 * flushing out waiters. By recalculating the closing flags before
1600 * each iteration we avoid any problems.
1603 /* Guard against races with tty->count changes elsewhere and
1604 opens on /dev/tty */
1606 mutex_lock(&tty_mutex);
1607 tty_closing = tty->count <= 1;
1608 o_tty_closing = o_tty &&
1609 (o_tty->count <= (pty_master ? 1 : 0));
1613 if (waitqueue_active(&tty->read_wait)) {
1614 wake_up(&tty->read_wait);
1617 if (waitqueue_active(&tty->write_wait)) {
1618 wake_up(&tty->write_wait);
1622 if (o_tty_closing) {
1623 if (waitqueue_active(&o_tty->read_wait)) {
1624 wake_up(&o_tty->read_wait);
1627 if (waitqueue_active(&o_tty->write_wait)) {
1628 wake_up(&o_tty->write_wait);
1635 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
1636 "active!\n", tty_name(tty, buf));
1637 mutex_unlock(&tty_mutex);
1642 * The closing flags are now consistent with the open counts on
1643 * both sides, and we've completed the last operation that could
1644 * block, so it's safe to proceed with closing.
1647 if (--o_tty->count < 0) {
1648 printk(KERN_WARNING "release_dev: bad pty slave count "
1650 o_tty->count, tty_name(o_tty, buf));
1654 if (--tty->count < 0) {
1655 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
1656 tty->count, tty_name(tty, buf));
1661 * We've decremented tty->count, so we need to remove this file
1662 * descriptor off the tty->tty_files list; this serves two
1664 * - check_tty_count sees the correct number of file descriptors
1665 * associated with this tty.
1666 * - do_tty_hangup no longer sees this file descriptor as
1667 * something that needs to be handled for hangups.
1670 filp->private_data = NULL;
1673 * Perform some housekeeping before deciding whether to return.
1675 * Set the TTY_CLOSING flag if this was the last open. In the
1676 * case of a pty we may have to wait around for the other side
1677 * to close, and TTY_CLOSING makes sure we can't be reopened.
1680 set_bit(TTY_CLOSING, &tty->flags);
1682 set_bit(TTY_CLOSING, &o_tty->flags);
1685 * If _either_ side is closing, make sure there aren't any
1686 * processes that still think tty or o_tty is their controlling
1689 if (tty_closing || o_tty_closing) {
1690 read_lock(&tasklist_lock);
1691 session_clear_tty(tty->session);
1693 session_clear_tty(o_tty->session);
1694 read_unlock(&tasklist_lock);
1697 mutex_unlock(&tty_mutex);
1699 /* check whether both sides are closing ... */
1700 if (!tty_closing || (o_tty && !o_tty_closing))
1703 #ifdef TTY_DEBUG_HANGUP
1704 printk(KERN_DEBUG "freeing tty structure...");
1707 * Ask the line discipline code to release its structures
1709 tty_ldisc_release(tty, o_tty);
1711 * The release_tty function takes care of the details of clearing
1712 * the slots and preserving the termios structure.
1714 release_tty(tty, idx);
1716 /* Make this pty number available for reallocation */
1718 devpts_kill_index(idx);
1722 * tty_open - open a tty device
1723 * @inode: inode of device file
1724 * @filp: file pointer to tty
1726 * tty_open and tty_release keep up the tty count that contains the
1727 * number of opens done on a tty. We cannot use the inode-count, as
1728 * different inodes might point to the same tty.
1730 * Open-counting is needed for pty masters, as well as for keeping
1731 * track of serial lines: DTR is dropped when the last close happens.
1732 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1734 * The termios state of a pty is reset on first open so that
1735 * settings don't persist across reuse.
1737 * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
1738 * tty->count should protect the rest.
1739 * ->siglock protects ->signal/->sighand
1742 static int __tty_open(struct inode *inode, struct file *filp)
1744 struct tty_struct *tty;
1746 struct tty_driver *driver;
1748 dev_t device = inode->i_rdev;
1749 unsigned short saved_flags = filp->f_flags;
1751 nonseekable_open(inode, filp);
1754 noctty = filp->f_flags & O_NOCTTY;
1758 mutex_lock(&tty_mutex);
1760 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
1761 tty = get_current_tty();
1763 mutex_unlock(&tty_mutex);
1766 driver = tty->driver;
1768 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1773 if (device == MKDEV(TTY_MAJOR, 0)) {
1774 extern struct tty_driver *console_driver;
1775 driver = console_driver;
1781 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
1782 driver = console_device(&index);
1784 /* Don't let /dev/console block */
1785 filp->f_flags |= O_NONBLOCK;
1789 mutex_unlock(&tty_mutex);
1793 driver = get_tty_driver(device, &index);
1795 mutex_unlock(&tty_mutex);
1799 retval = init_dev(driver, index, &tty);
1800 mutex_unlock(&tty_mutex);
1804 filp->private_data = tty;
1805 file_move(filp, &tty->tty_files);
1806 check_tty_count(tty, "tty_open");
1807 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1808 tty->driver->subtype == PTY_TYPE_MASTER)
1810 #ifdef TTY_DEBUG_HANGUP
1811 printk(KERN_DEBUG "opening %s...", tty->name);
1815 retval = tty->ops->open(tty, filp);
1819 filp->f_flags = saved_flags;
1821 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1822 !capable(CAP_SYS_ADMIN))
1826 #ifdef TTY_DEBUG_HANGUP
1827 printk(KERN_DEBUG "error %d in opening %s...", retval,
1831 if (retval != -ERESTARTSYS)
1833 if (signal_pending(current))
1837 * Need to reset f_op in case a hangup happened.
1839 if (filp->f_op == &hung_up_tty_fops)
1840 filp->f_op = &tty_fops;
1844 mutex_lock(&tty_mutex);
1845 spin_lock_irq(¤t->sighand->siglock);
1847 current->signal->leader &&
1848 !current->signal->tty &&
1849 tty->session == NULL)
1850 __proc_set_tty(current, tty);
1851 spin_unlock_irq(¤t->sighand->siglock);
1852 mutex_unlock(&tty_mutex);
1856 /* BKL pushdown: scary code avoidance wrapper */
1857 static int tty_open(struct inode *inode, struct file *filp)
1862 ret = __tty_open(inode, filp);
1869 #ifdef CONFIG_UNIX98_PTYS
1871 * ptmx_open - open a unix 98 pty master
1872 * @inode: inode of device file
1873 * @filp: file pointer to tty
1875 * Allocate a unix98 pty master device from the ptmx driver.
1877 * Locking: tty_mutex protects theinit_dev work. tty->count should
1879 * allocated_ptys_lock handles the list of free pty numbers
1882 static int __ptmx_open(struct inode *inode, struct file *filp)
1884 struct tty_struct *tty;
1888 nonseekable_open(inode, filp);
1890 /* find a device that is not in use. */
1891 index = devpts_new_index();
1895 mutex_lock(&tty_mutex);
1896 retval = init_dev(ptm_driver, index, &tty);
1897 mutex_unlock(&tty_mutex);
1902 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1903 filp->private_data = tty;
1904 file_move(filp, &tty->tty_files);
1906 retval = devpts_pty_new(tty->link);
1910 check_tty_count(tty, "ptmx_open");
1911 retval = ptm_driver->ops->open(tty, filp);
1918 devpts_kill_index(index);
1922 static int ptmx_open(struct inode *inode, struct file *filp)
1927 ret = __ptmx_open(inode, filp);
1934 * tty_release - vfs callback for close
1935 * @inode: inode of tty
1936 * @filp: file pointer for handle to tty
1938 * Called the last time each file handle is closed that references
1939 * this tty. There may however be several such references.
1942 * Takes bkl. See release_dev
1945 static int tty_release(struct inode *inode, struct file *filp)
1954 * tty_poll - check tty status
1955 * @filp: file being polled
1956 * @wait: poll wait structures to update
1958 * Call the line discipline polling method to obtain the poll
1959 * status of the device.
1961 * Locking: locks called line discipline but ldisc poll method
1962 * may be re-entered freely by other callers.
1965 static unsigned int tty_poll(struct file *filp, poll_table *wait)
1967 struct tty_struct *tty;
1968 struct tty_ldisc *ld;
1971 tty = (struct tty_struct *)filp->private_data;
1972 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
1975 ld = tty_ldisc_ref_wait(tty);
1977 ret = (ld->ops->poll)(tty, filp, wait);
1978 tty_ldisc_deref(ld);
1982 static int tty_fasync(int fd, struct file *filp, int on)
1984 struct tty_struct *tty;
1985 unsigned long flags;
1989 tty = (struct tty_struct *)filp->private_data;
1990 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
1993 retval = fasync_helper(fd, filp, on, &tty->fasync);
2000 if (!waitqueue_active(&tty->read_wait))
2001 tty->minimum_to_wake = 1;
2002 spin_lock_irqsave(&tty->ctrl_lock, flags);
2005 type = PIDTYPE_PGID;
2007 pid = task_pid(current);
2010 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2011 retval = __f_setown(filp, pid, type, 0);
2015 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2016 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2025 * tiocsti - fake input character
2026 * @tty: tty to fake input into
2027 * @p: pointer to character
2029 * Fake input to a tty device. Does the necessary locking and
2032 * FIXME: does not honour flow control ??
2035 * Called functions take tty_ldisc_lock
2036 * current->signal->tty check is safe without locks
2038 * FIXME: may race normal receive processing
2041 static int tiocsti(struct tty_struct *tty, char __user *p)
2044 struct tty_ldisc *ld;
2046 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2048 if (get_user(ch, p))
2050 ld = tty_ldisc_ref_wait(tty);
2051 ld->ops->receive_buf(tty, &ch, &mbz, 1);
2052 tty_ldisc_deref(ld);
2057 * tiocgwinsz - implement window query ioctl
2059 * @arg: user buffer for result
2061 * Copies the kernel idea of the window size into the user buffer.
2063 * Locking: tty->termios_mutex is taken to ensure the winsize data
2067 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
2071 mutex_lock(&tty->termios_mutex);
2072 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2073 mutex_unlock(&tty->termios_mutex);
2075 return err ? -EFAULT: 0;
2079 * tty_do_resize - resize event
2080 * @tty: tty being resized
2081 * @real_tty: real tty (not the same as tty if using a pty/tty pair)
2082 * @rows: rows (character)
2083 * @cols: cols (character)
2085 * Update the termios variables and send the neccessary signals to
2086 * peform a terminal resize correctly
2089 int tty_do_resize(struct tty_struct *tty, struct tty_struct *real_tty,
2092 struct pid *pgrp, *rpgrp;
2093 unsigned long flags;
2095 /* For a PTY we need to lock the tty side */
2096 mutex_lock(&real_tty->termios_mutex);
2097 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2099 /* Get the PID values and reference them so we can
2100 avoid holding the tty ctrl lock while sending signals */
2101 spin_lock_irqsave(&tty->ctrl_lock, flags);
2102 pgrp = get_pid(tty->pgrp);
2103 rpgrp = get_pid(real_tty->pgrp);
2104 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2107 kill_pgrp(pgrp, SIGWINCH, 1);
2108 if (rpgrp != pgrp && rpgrp)
2109 kill_pgrp(rpgrp, SIGWINCH, 1);
2115 real_tty->winsize = *ws;
2117 mutex_unlock(&real_tty->termios_mutex);
2122 * tiocswinsz - implement window size set ioctl
2124 * @arg: user buffer for result
2126 * Copies the user idea of the window size to the kernel. Traditionally
2127 * this is just advisory information but for the Linux console it
2128 * actually has driver level meaning and triggers a VC resize.
2131 * Driver dependant. The default do_resize method takes the
2132 * tty termios mutex and ctrl_lock. The console takes its own lock
2133 * then calls into the default method.
2136 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
2137 struct winsize __user *arg)
2139 struct winsize tmp_ws;
2140 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2143 if (tty->ops->resize)
2144 return tty->ops->resize(tty, real_tty, &tmp_ws);
2146 return tty_do_resize(tty, real_tty, &tmp_ws);
2150 * tioccons - allow admin to move logical console
2151 * @file: the file to become console
2153 * Allow the adminstrator to move the redirected console device
2155 * Locking: uses redirect_lock to guard the redirect information
2158 static int tioccons(struct file *file)
2160 if (!capable(CAP_SYS_ADMIN))
2162 if (file->f_op->write == redirected_tty_write) {
2164 spin_lock(&redirect_lock);
2167 spin_unlock(&redirect_lock);
2172 spin_lock(&redirect_lock);
2174 spin_unlock(&redirect_lock);
2179 spin_unlock(&redirect_lock);
2184 * fionbio - non blocking ioctl
2185 * @file: file to set blocking value
2186 * @p: user parameter
2188 * Historical tty interfaces had a blocking control ioctl before
2189 * the generic functionality existed. This piece of history is preserved
2190 * in the expected tty API of posix OS's.
2192 * Locking: none, the open fle handle ensures it won't go away.
2195 static int fionbio(struct file *file, int __user *p)
2199 if (get_user(nonblock, p))
2202 /* file->f_flags is still BKL protected in the fs layer - vomit */
2205 file->f_flags |= O_NONBLOCK;
2207 file->f_flags &= ~O_NONBLOCK;
2213 * tiocsctty - set controlling tty
2214 * @tty: tty structure
2215 * @arg: user argument
2217 * This ioctl is used to manage job control. It permits a session
2218 * leader to set this tty as the controlling tty for the session.
2221 * Takes tty_mutex() to protect tty instance
2222 * Takes tasklist_lock internally to walk sessions
2223 * Takes ->siglock() when updating signal->tty
2226 static int tiocsctty(struct tty_struct *tty, int arg)
2229 if (current->signal->leader && (task_session(current) == tty->session))
2232 mutex_lock(&tty_mutex);
2234 * The process must be a session leader and
2235 * not have a controlling tty already.
2237 if (!current->signal->leader || current->signal->tty) {
2244 * This tty is already the controlling
2245 * tty for another session group!
2247 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
2251 read_lock(&tasklist_lock);
2252 session_clear_tty(tty->session);
2253 read_unlock(&tasklist_lock);
2259 proc_set_tty(current, tty);
2261 mutex_unlock(&tty_mutex);
2266 * tty_get_pgrp - return a ref counted pgrp pid
2269 * Returns a refcounted instance of the pid struct for the process
2270 * group controlling the tty.
2273 struct pid *tty_get_pgrp(struct tty_struct *tty)
2275 unsigned long flags;
2278 spin_lock_irqsave(&tty->ctrl_lock, flags);
2279 pgrp = get_pid(tty->pgrp);
2280 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2284 EXPORT_SYMBOL_GPL(tty_get_pgrp);
2287 * tiocgpgrp - get process group
2288 * @tty: tty passed by user
2289 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2292 * Obtain the process group of the tty. If there is no process group
2295 * Locking: none. Reference to current->signal->tty is safe.
2298 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2303 * (tty == real_tty) is a cheap way of
2304 * testing if the tty is NOT a master pty.
2306 if (tty == real_tty && current->signal->tty != real_tty)
2308 pid = tty_get_pgrp(real_tty);
2309 ret = put_user(pid_vnr(pid), p);
2315 * tiocspgrp - attempt to set process group
2316 * @tty: tty passed by user
2317 * @real_tty: tty side device matching tty passed by user
2320 * Set the process group of the tty to the session passed. Only
2321 * permitted where the tty session is our session.
2323 * Locking: RCU, ctrl lock
2326 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2330 int retval = tty_check_change(real_tty);
2331 unsigned long flags;
2337 if (!current->signal->tty ||
2338 (current->signal->tty != real_tty) ||
2339 (real_tty->session != task_session(current)))
2341 if (get_user(pgrp_nr, p))
2346 pgrp = find_vpid(pgrp_nr);
2351 if (session_of_pgrp(pgrp) != task_session(current))
2354 spin_lock_irqsave(&tty->ctrl_lock, flags);
2355 put_pid(real_tty->pgrp);
2356 real_tty->pgrp = get_pid(pgrp);
2357 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2364 * tiocgsid - get session id
2365 * @tty: tty passed by user
2366 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2367 * @p: pointer to returned session id
2369 * Obtain the session id of the tty. If there is no session
2372 * Locking: none. Reference to current->signal->tty is safe.
2375 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2378 * (tty == real_tty) is a cheap way of
2379 * testing if the tty is NOT a master pty.
2381 if (tty == real_tty && current->signal->tty != real_tty)
2383 if (!real_tty->session)
2385 return put_user(pid_vnr(real_tty->session), p);
2389 * tiocsetd - set line discipline
2391 * @p: pointer to user data
2393 * Set the line discipline according to user request.
2395 * Locking: see tty_set_ldisc, this function is just a helper
2398 static int tiocsetd(struct tty_struct *tty, int __user *p)
2403 if (get_user(ldisc, p))
2407 ret = tty_set_ldisc(tty, ldisc);
2414 * send_break - performed time break
2415 * @tty: device to break on
2416 * @duration: timeout in mS
2418 * Perform a timed break on hardware that lacks its own driver level
2419 * timed break functionality.
2422 * atomic_write_lock serializes
2426 static int send_break(struct tty_struct *tty, unsigned int duration)
2430 if (tty->ops->break_ctl == NULL)
2433 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2434 retval = tty->ops->break_ctl(tty, duration);
2436 /* Do the work ourselves */
2437 if (tty_write_lock(tty, 0) < 0)
2439 retval = tty->ops->break_ctl(tty, -1);
2442 if (!signal_pending(current))
2443 msleep_interruptible(duration);
2444 retval = tty->ops->break_ctl(tty, 0);
2446 tty_write_unlock(tty);
2447 if (signal_pending(current))
2454 * tty_tiocmget - get modem status
2456 * @file: user file pointer
2457 * @p: pointer to result
2459 * Obtain the modem status bits from the tty driver if the feature
2460 * is supported. Return -EINVAL if it is not available.
2462 * Locking: none (up to the driver)
2465 static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
2467 int retval = -EINVAL;
2469 if (tty->ops->tiocmget) {
2470 retval = tty->ops->tiocmget(tty, file);
2473 retval = put_user(retval, p);
2479 * tty_tiocmset - set modem status
2481 * @file: user file pointer
2482 * @cmd: command - clear bits, set bits or set all
2483 * @p: pointer to desired bits
2485 * Set the modem status bits from the tty driver if the feature
2486 * is supported. Return -EINVAL if it is not available.
2488 * Locking: none (up to the driver)
2491 static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
2495 unsigned int set, clear, val;
2497 if (tty->ops->tiocmset == NULL)
2500 retval = get_user(val, p);
2516 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2517 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2518 return tty->ops->tiocmset(tty, file, set, clear);
2522 * Split this up, as gcc can choke on it otherwise..
2524 long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2526 struct tty_struct *tty, *real_tty;
2527 void __user *p = (void __user *)arg;
2529 struct tty_ldisc *ld;
2530 struct inode *inode = file->f_dentry->d_inode;
2532 tty = (struct tty_struct *)file->private_data;
2533 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2537 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2538 tty->driver->subtype == PTY_TYPE_MASTER)
2539 real_tty = tty->link;
2543 * Factor out some common prep work
2551 retval = tty_check_change(tty);
2554 if (cmd != TIOCCBRK) {
2555 tty_wait_until_sent(tty, 0);
2556 if (signal_pending(current))
2567 return tiocsti(tty, p);
2569 return tiocgwinsz(tty, p);
2571 return tiocswinsz(tty, real_tty, p);
2573 return real_tty != tty ? -EINVAL : tioccons(file);
2575 return fionbio(file, p);
2577 set_bit(TTY_EXCLUSIVE, &tty->flags);
2580 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2583 if (current->signal->tty != tty)
2588 return tiocsctty(tty, arg);
2590 return tiocgpgrp(tty, real_tty, p);
2592 return tiocspgrp(tty, real_tty, p);
2594 return tiocgsid(tty, real_tty, p);
2596 return put_user(tty->ldisc.ops->num, (int __user *)p);
2598 return tiocsetd(tty, p);
2602 case TIOCSBRK: /* Turn break on, unconditionally */
2603 if (tty->ops->break_ctl)
2604 return tty->ops->break_ctl(tty, -1);
2606 case TIOCCBRK: /* Turn break off, unconditionally */
2607 if (tty->ops->break_ctl)
2608 return tty->ops->break_ctl(tty, 0);
2610 case TCSBRK: /* SVID version: non-zero arg --> no break */
2611 /* non-zero arg means wait for all output data
2612 * to be sent (performed above) but don't send break.
2613 * This is used by the tcdrain() termios function.
2616 return send_break(tty, 250);
2618 case TCSBRKP: /* support for POSIX tcsendbreak() */
2619 return send_break(tty, arg ? arg*100 : 250);
2622 return tty_tiocmget(tty, file, p);
2626 return tty_tiocmset(tty, file, cmd, p);
2631 /* flush tty buffer and allow ldisc to process ioctl */
2632 tty_buffer_flush(tty);
2637 if (tty->ops->ioctl) {
2638 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
2639 if (retval != -ENOIOCTLCMD)
2642 ld = tty_ldisc_ref_wait(tty);
2644 if (ld->ops->ioctl) {
2645 retval = ld->ops->ioctl(tty, file, cmd, arg);
2646 if (retval == -ENOIOCTLCMD)
2649 tty_ldisc_deref(ld);
2653 #ifdef CONFIG_COMPAT
2654 static long tty_compat_ioctl(struct file *file, unsigned int cmd,
2657 struct inode *inode = file->f_dentry->d_inode;
2658 struct tty_struct *tty = file->private_data;
2659 struct tty_ldisc *ld;
2660 int retval = -ENOIOCTLCMD;
2662 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2665 if (tty->ops->compat_ioctl) {
2666 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
2667 if (retval != -ENOIOCTLCMD)
2671 ld = tty_ldisc_ref_wait(tty);
2672 if (ld->ops->compat_ioctl)
2673 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
2674 tty_ldisc_deref(ld);
2681 * This implements the "Secure Attention Key" --- the idea is to
2682 * prevent trojan horses by killing all processes associated with this
2683 * tty when the user hits the "Secure Attention Key". Required for
2684 * super-paranoid applications --- see the Orange Book for more details.
2686 * This code could be nicer; ideally it should send a HUP, wait a few
2687 * seconds, then send a INT, and then a KILL signal. But you then
2688 * have to coordinate with the init process, since all processes associated
2689 * with the current tty must be dead before the new getty is allowed
2692 * Now, if it would be correct ;-/ The current code has a nasty hole -
2693 * it doesn't catch files in flight. We may send the descriptor to ourselves
2694 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2696 * Nasty bug: do_SAK is being called in interrupt context. This can
2697 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2699 void __do_SAK(struct tty_struct *tty)
2704 struct task_struct *g, *p;
2705 struct pid *session;
2708 struct fdtable *fdt;
2712 session = tty->session;
2714 tty_ldisc_flush(tty);
2716 tty_driver_flush_buffer(tty);
2718 read_lock(&tasklist_lock);
2719 /* Kill the entire session */
2720 do_each_pid_task(session, PIDTYPE_SID, p) {
2721 printk(KERN_NOTICE "SAK: killed process %d"
2722 " (%s): task_session_nr(p)==tty->session\n",
2723 task_pid_nr(p), p->comm);
2724 send_sig(SIGKILL, p, 1);
2725 } while_each_pid_task(session, PIDTYPE_SID, p);
2726 /* Now kill any processes that happen to have the
2729 do_each_thread(g, p) {
2730 if (p->signal->tty == tty) {
2731 printk(KERN_NOTICE "SAK: killed process %d"
2732 " (%s): task_session_nr(p)==tty->session\n",
2733 task_pid_nr(p), p->comm);
2734 send_sig(SIGKILL, p, 1);
2740 * We don't take a ref to the file, so we must
2741 * hold ->file_lock instead.
2743 spin_lock(&p->files->file_lock);
2744 fdt = files_fdtable(p->files);
2745 for (i = 0; i < fdt->max_fds; i++) {
2746 filp = fcheck_files(p->files, i);
2749 if (filp->f_op->read == tty_read &&
2750 filp->private_data == tty) {
2751 printk(KERN_NOTICE "SAK: killed process %d"
2752 " (%s): fd#%d opened to the tty\n",
2753 task_pid_nr(p), p->comm, i);
2754 force_sig(SIGKILL, p);
2758 spin_unlock(&p->files->file_lock);
2761 } while_each_thread(g, p);
2762 read_unlock(&tasklist_lock);
2766 static void do_SAK_work(struct work_struct *work)
2768 struct tty_struct *tty =
2769 container_of(work, struct tty_struct, SAK_work);
2774 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2775 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2776 * the values which we write to it will be identical to the values which it
2777 * already has. --akpm
2779 void do_SAK(struct tty_struct *tty)
2783 schedule_work(&tty->SAK_work);
2786 EXPORT_SYMBOL(do_SAK);
2789 * initialize_tty_struct
2790 * @tty: tty to initialize
2792 * This subroutine initializes a tty structure that has been newly
2795 * Locking: none - tty in question must not be exposed at this point
2798 static void initialize_tty_struct(struct tty_struct *tty)
2800 memset(tty, 0, sizeof(struct tty_struct));
2801 tty->magic = TTY_MAGIC;
2802 tty_ldisc_init(tty);
2803 tty->session = NULL;
2805 tty->overrun_time = jiffies;
2806 tty->buf.head = tty->buf.tail = NULL;
2807 tty_buffer_init(tty);
2808 mutex_init(&tty->termios_mutex);
2809 init_waitqueue_head(&tty->write_wait);
2810 init_waitqueue_head(&tty->read_wait);
2811 INIT_WORK(&tty->hangup_work, do_tty_hangup);
2812 mutex_init(&tty->atomic_read_lock);
2813 mutex_init(&tty->atomic_write_lock);
2814 spin_lock_init(&tty->read_lock);
2815 spin_lock_init(&tty->ctrl_lock);
2816 INIT_LIST_HEAD(&tty->tty_files);
2817 INIT_WORK(&tty->SAK_work, do_SAK_work);
2821 * tty_put_char - write one character to a tty
2825 * Write one byte to the tty using the provided put_char method
2826 * if present. Returns the number of characters successfully output.
2828 * Note: the specific put_char operation in the driver layer may go
2829 * away soon. Don't call it directly, use this method
2832 int tty_put_char(struct tty_struct *tty, unsigned char ch)
2834 if (tty->ops->put_char)
2835 return tty->ops->put_char(tty, ch);
2836 return tty->ops->write(tty, &ch, 1);
2839 EXPORT_SYMBOL_GPL(tty_put_char);
2841 static struct class *tty_class;
2844 * tty_register_device - register a tty device
2845 * @driver: the tty driver that describes the tty device
2846 * @index: the index in the tty driver for this tty device
2847 * @device: a struct device that is associated with this tty device.
2848 * This field is optional, if there is no known struct device
2849 * for this tty device it can be set to NULL safely.
2851 * Returns a pointer to the struct device for this tty device
2852 * (or ERR_PTR(-EFOO) on error).
2854 * This call is required to be made to register an individual tty device
2855 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2856 * that bit is not set, this function should not be called by a tty
2862 struct device *tty_register_device(struct tty_driver *driver, unsigned index,
2863 struct device *device)
2866 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2868 if (index >= driver->num) {
2869 printk(KERN_ERR "Attempt to register invalid tty line number "
2871 return ERR_PTR(-EINVAL);
2874 if (driver->type == TTY_DRIVER_TYPE_PTY)
2875 pty_line_name(driver, index, name);
2877 tty_line_name(driver, index, name);
2879 return device_create_drvdata(tty_class, device, dev, NULL, name);
2883 * tty_unregister_device - unregister a tty device
2884 * @driver: the tty driver that describes the tty device
2885 * @index: the index in the tty driver for this tty device
2887 * If a tty device is registered with a call to tty_register_device() then
2888 * this function must be called when the tty device is gone.
2893 void tty_unregister_device(struct tty_driver *driver, unsigned index)
2895 device_destroy(tty_class,
2896 MKDEV(driver->major, driver->minor_start) + index);
2899 EXPORT_SYMBOL(tty_register_device);
2900 EXPORT_SYMBOL(tty_unregister_device);
2902 struct tty_driver *alloc_tty_driver(int lines)
2904 struct tty_driver *driver;
2906 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
2908 driver->magic = TTY_DRIVER_MAGIC;
2909 driver->num = lines;
2910 /* later we'll move allocation of tables here */
2915 void put_tty_driver(struct tty_driver *driver)
2920 void tty_set_operations(struct tty_driver *driver,
2921 const struct tty_operations *op)
2926 EXPORT_SYMBOL(alloc_tty_driver);
2927 EXPORT_SYMBOL(put_tty_driver);
2928 EXPORT_SYMBOL(tty_set_operations);
2931 * Called by a tty driver to register itself.
2933 int tty_register_driver(struct tty_driver *driver)
2940 if (driver->flags & TTY_DRIVER_INSTALLED)
2943 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
2944 p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
2949 if (!driver->major) {
2950 error = alloc_chrdev_region(&dev, driver->minor_start,
2951 driver->num, driver->name);
2953 driver->major = MAJOR(dev);
2954 driver->minor_start = MINOR(dev);
2957 dev = MKDEV(driver->major, driver->minor_start);
2958 error = register_chrdev_region(dev, driver->num, driver->name);
2966 driver->ttys = (struct tty_struct **)p;
2967 driver->termios = (struct ktermios **)(p + driver->num);
2968 driver->termios_locked = (struct ktermios **)
2969 (p + driver->num * 2);
2971 driver->ttys = NULL;
2972 driver->termios = NULL;
2973 driver->termios_locked = NULL;
2976 cdev_init(&driver->cdev, &tty_fops);
2977 driver->cdev.owner = driver->owner;
2978 error = cdev_add(&driver->cdev, dev, driver->num);
2980 unregister_chrdev_region(dev, driver->num);
2981 driver->ttys = NULL;
2982 driver->termios = driver->termios_locked = NULL;
2987 mutex_lock(&tty_mutex);
2988 list_add(&driver->tty_drivers, &tty_drivers);
2989 mutex_unlock(&tty_mutex);
2991 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
2992 for (i = 0; i < driver->num; i++)
2993 tty_register_device(driver, i, NULL);
2995 proc_tty_register_driver(driver);
2999 EXPORT_SYMBOL(tty_register_driver);
3002 * Called by a tty driver to unregister itself.
3004 int tty_unregister_driver(struct tty_driver *driver)
3007 struct ktermios *tp;
3010 if (driver->refcount)
3013 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3015 mutex_lock(&tty_mutex);
3016 list_del(&driver->tty_drivers);
3017 mutex_unlock(&tty_mutex);
3020 * Free the termios and termios_locked structures because
3021 * we don't want to get memory leaks when modular tty
3022 * drivers are removed from the kernel.
3024 for (i = 0; i < driver->num; i++) {
3025 tp = driver->termios[i];
3027 driver->termios[i] = NULL;
3030 tp = driver->termios_locked[i];
3032 driver->termios_locked[i] = NULL;
3035 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3036 tty_unregister_device(driver, i);
3039 proc_tty_unregister_driver(driver);
3040 driver->ttys = NULL;
3041 driver->termios = driver->termios_locked = NULL;
3043 cdev_del(&driver->cdev);
3046 EXPORT_SYMBOL(tty_unregister_driver);
3048 dev_t tty_devnum(struct tty_struct *tty)
3050 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3052 EXPORT_SYMBOL(tty_devnum);
3054 void proc_clear_tty(struct task_struct *p)
3056 spin_lock_irq(&p->sighand->siglock);
3057 p->signal->tty = NULL;
3058 spin_unlock_irq(&p->sighand->siglock);
3061 /* Called under the sighand lock */
3063 static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3066 unsigned long flags;
3067 /* We should not have a session or pgrp to put here but.... */
3068 spin_lock_irqsave(&tty->ctrl_lock, flags);
3069 put_pid(tty->session);
3071 tty->pgrp = get_pid(task_pgrp(tsk));
3072 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3073 tty->session = get_pid(task_session(tsk));
3075 put_pid(tsk->signal->tty_old_pgrp);
3076 tsk->signal->tty = tty;
3077 tsk->signal->tty_old_pgrp = NULL;
3080 static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3082 spin_lock_irq(&tsk->sighand->siglock);
3083 __proc_set_tty(tsk, tty);
3084 spin_unlock_irq(&tsk->sighand->siglock);
3087 struct tty_struct *get_current_tty(void)
3089 struct tty_struct *tty;
3090 WARN_ON_ONCE(!mutex_is_locked(&tty_mutex));
3091 tty = current->signal->tty;
3093 * session->tty can be changed/cleared from under us, make sure we
3094 * issue the load. The obtained pointer, when not NULL, is valid as
3095 * long as we hold tty_mutex.
3100 EXPORT_SYMBOL_GPL(get_current_tty);
3103 * Initialize the console device. This is called *early*, so
3104 * we can't necessarily depend on lots of kernel help here.
3105 * Just do some early initializations, and do the complex setup
3108 void __init console_init(void)
3112 /* Setup the default TTY line discipline. */
3116 * set up the console device so that later boot sequences can
3117 * inform about problems etc..
3119 call = __con_initcall_start;
3120 while (call < __con_initcall_end) {
3126 static int __init tty_class_init(void)
3128 tty_class = class_create(THIS_MODULE, "tty");
3129 if (IS_ERR(tty_class))
3130 return PTR_ERR(tty_class);
3134 postcore_initcall(tty_class_init);
3136 /* 3/2004 jmc: why do these devices exist? */
3138 static struct cdev tty_cdev, console_cdev;
3139 #ifdef CONFIG_UNIX98_PTYS
3140 static struct cdev ptmx_cdev;
3143 static struct cdev vc0_cdev;
3147 * Ok, now we can initialize the rest of the tty devices and can count
3148 * on memory allocations, interrupts etc..
3150 static int __init tty_init(void)
3152 cdev_init(&tty_cdev, &tty_fops);
3153 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3154 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3155 panic("Couldn't register /dev/tty driver\n");
3156 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
3159 cdev_init(&console_cdev, &console_fops);
3160 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3161 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3162 panic("Couldn't register /dev/console driver\n");
3163 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
3166 #ifdef CONFIG_UNIX98_PTYS
3167 cdev_init(&ptmx_cdev, &ptmx_fops);
3168 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
3169 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
3170 panic("Couldn't register /dev/ptmx driver\n");
3171 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
3175 cdev_init(&vc0_cdev, &console_fops);
3176 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3177 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3178 panic("Couldn't register /dev/tty0 driver\n");
3179 device_create_drvdata(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
3185 module_init(tty_init);