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);
179 static void tty_buffer_free_all(struct tty_struct *);
182 * free_tty_struct - free a disused tty
183 * @tty: tty struct to free
185 * Free the write buffers, tty queue and tty memory itself.
187 * Locking: none. Must be called after tty is definitely unused
190 static inline void free_tty_struct(struct tty_struct *tty)
192 kfree(tty->write_buf);
193 tty_buffer_free_all(tty);
197 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
200 * tty_name - return tty naming
201 * @tty: tty structure
202 * @buf: buffer for output
204 * Convert a tty structure into a name. The name reflects the kernel
205 * naming policy and if udev is in use may not reflect user space
210 char *tty_name(struct tty_struct *tty, char *buf)
212 if (!tty) /* Hmm. NULL pointer. That's fun. */
213 strcpy(buf, "NULL tty");
215 strcpy(buf, tty->name);
219 EXPORT_SYMBOL(tty_name);
221 int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
224 #ifdef TTY_PARANOIA_CHECK
227 "null TTY for (%d:%d) in %s\n",
228 imajor(inode), iminor(inode), routine);
231 if (tty->magic != TTY_MAGIC) {
233 "bad magic number for tty struct (%d:%d) in %s\n",
234 imajor(inode), iminor(inode), routine);
241 static int check_tty_count(struct tty_struct *tty, const char *routine)
243 #ifdef CHECK_TTY_COUNT
248 list_for_each(p, &tty->tty_files) {
252 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
253 tty->driver->subtype == PTY_TYPE_SLAVE &&
254 tty->link && tty->link->count)
256 if (tty->count != count) {
257 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
258 "!= #fd's(%d) in %s\n",
259 tty->name, tty->count, count, routine);
267 * Tty buffer allocation management
271 * tty_buffer_free_all - free buffers used by a tty
272 * @tty: tty to free from
274 * Remove all the buffers pending on a tty whether queued with data
275 * or in the free ring. Must be called when the tty is no longer in use
280 static void tty_buffer_free_all(struct tty_struct *tty)
282 struct tty_buffer *thead;
283 while ((thead = tty->buf.head) != NULL) {
284 tty->buf.head = thead->next;
287 while ((thead = tty->buf.free) != NULL) {
288 tty->buf.free = thead->next;
291 tty->buf.tail = NULL;
292 tty->buf.memory_used = 0;
296 * tty_buffer_init - prepare a tty buffer structure
297 * @tty: tty to initialise
299 * Set up the initial state of the buffer management for a tty device.
300 * Must be called before the other tty buffer functions are used.
305 static void tty_buffer_init(struct tty_struct *tty)
307 spin_lock_init(&tty->buf.lock);
308 tty->buf.head = NULL;
309 tty->buf.tail = NULL;
310 tty->buf.free = NULL;
311 tty->buf.memory_used = 0;
315 * tty_buffer_alloc - allocate a tty buffer
317 * @size: desired size (characters)
319 * Allocate a new tty buffer to hold the desired number of characters.
320 * Return NULL if out of memory or the allocation would exceed the
323 * Locking: Caller must hold tty->buf.lock
326 static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
328 struct tty_buffer *p;
330 if (tty->buf.memory_used + size > 65536)
332 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
340 p->char_buf_ptr = (char *)(p->data);
341 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
342 tty->buf.memory_used += size;
347 * tty_buffer_free - free a tty buffer
348 * @tty: tty owning the buffer
349 * @b: the buffer to free
351 * Free a tty buffer, or add it to the free list according to our
354 * Locking: Caller must hold tty->buf.lock
357 static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
359 /* Dumb strategy for now - should keep some stats */
360 tty->buf.memory_used -= b->size;
361 WARN_ON(tty->buf.memory_used < 0);
366 b->next = tty->buf.free;
372 * __tty_buffer_flush - flush full tty buffers
375 * flush all the buffers containing receive data. Caller must
376 * hold the buffer lock and must have ensured no parallel flush to
379 * Locking: Caller must hold tty->buf.lock
382 static void __tty_buffer_flush(struct tty_struct *tty)
384 struct tty_buffer *thead;
386 while ((thead = tty->buf.head) != NULL) {
387 tty->buf.head = thead->next;
388 tty_buffer_free(tty, thead);
390 tty->buf.tail = NULL;
394 * tty_buffer_flush - flush full tty buffers
397 * flush all the buffers containing receive data. If the buffer is
398 * being processed by flush_to_ldisc then we defer the processing
404 static void tty_buffer_flush(struct tty_struct *tty)
407 spin_lock_irqsave(&tty->buf.lock, flags);
409 /* If the data is being pushed to the tty layer then we can't
410 process it here. Instead set a flag and the flush_to_ldisc
411 path will process the flush request before it exits */
412 if (test_bit(TTY_FLUSHING, &tty->flags)) {
413 set_bit(TTY_FLUSHPENDING, &tty->flags);
414 spin_unlock_irqrestore(&tty->buf.lock, flags);
415 wait_event(tty->read_wait,
416 test_bit(TTY_FLUSHPENDING, &tty->flags) == 0);
419 __tty_buffer_flush(tty);
420 spin_unlock_irqrestore(&tty->buf.lock, flags);
424 * tty_buffer_find - find a free tty buffer
425 * @tty: tty owning the buffer
426 * @size: characters wanted
428 * Locate an existing suitable tty buffer or if we are lacking one then
429 * allocate a new one. We round our buffers off in 256 character chunks
430 * to get better allocation behaviour.
432 * Locking: Caller must hold tty->buf.lock
435 static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
437 struct tty_buffer **tbh = &tty->buf.free;
438 while ((*tbh) != NULL) {
439 struct tty_buffer *t = *tbh;
440 if (t->size >= size) {
446 tty->buf.memory_used += t->size;
449 tbh = &((*tbh)->next);
451 /* Round the buffer size out */
452 size = (size + 0xFF) & ~0xFF;
453 return tty_buffer_alloc(tty, size);
454 /* Should possibly check if this fails for the largest buffer we
455 have queued and recycle that ? */
459 * tty_buffer_request_room - grow tty buffer if needed
460 * @tty: tty structure
461 * @size: size desired
463 * Make at least size bytes of linear space available for the tty
464 * buffer. If we fail return the size we managed to find.
466 * Locking: Takes tty->buf.lock
468 int tty_buffer_request_room(struct tty_struct *tty, size_t size)
470 struct tty_buffer *b, *n;
474 spin_lock_irqsave(&tty->buf.lock, flags);
476 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
477 remove this conditional if its worth it. This would be invisible
479 if ((b = tty->buf.tail) != NULL)
480 left = b->size - b->used;
485 /* This is the slow path - looking for new buffers to use */
486 if ((n = tty_buffer_find(tty, size)) != NULL) {
497 spin_unlock_irqrestore(&tty->buf.lock, flags);
500 EXPORT_SYMBOL_GPL(tty_buffer_request_room);
503 * tty_insert_flip_string - Add characters to the tty buffer
504 * @tty: tty structure
508 * Queue a series of bytes to the tty buffering. All the characters
509 * passed are marked as without error. Returns the number added.
511 * Locking: Called functions may take tty->buf.lock
514 int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars,
519 int space = tty_buffer_request_room(tty, size - copied);
520 struct tty_buffer *tb = tty->buf.tail;
521 /* If there is no space then tb may be NULL */
522 if (unlikely(space == 0))
524 memcpy(tb->char_buf_ptr + tb->used, chars, space);
525 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
529 /* There is a small chance that we need to split the data over
530 several buffers. If this is the case we must loop */
531 } while (unlikely(size > copied));
534 EXPORT_SYMBOL(tty_insert_flip_string);
537 * tty_insert_flip_string_flags - Add characters to the tty buffer
538 * @tty: tty structure
543 * Queue a series of bytes to the tty buffering. For each character
544 * the flags array indicates the status of the character. Returns the
547 * Locking: Called functions may take tty->buf.lock
550 int tty_insert_flip_string_flags(struct tty_struct *tty,
551 const unsigned char *chars, const char *flags, size_t size)
555 int space = tty_buffer_request_room(tty, size - copied);
556 struct tty_buffer *tb = tty->buf.tail;
557 /* If there is no space then tb may be NULL */
558 if (unlikely(space == 0))
560 memcpy(tb->char_buf_ptr + tb->used, chars, space);
561 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
566 /* There is a small chance that we need to split the data over
567 several buffers. If this is the case we must loop */
568 } while (unlikely(size > copied));
571 EXPORT_SYMBOL(tty_insert_flip_string_flags);
574 * tty_schedule_flip - push characters to ldisc
575 * @tty: tty to push from
577 * Takes any pending buffers and transfers their ownership to the
578 * ldisc side of the queue. It then schedules those characters for
579 * processing by the line discipline.
581 * Locking: Takes tty->buf.lock
584 void tty_schedule_flip(struct tty_struct *tty)
587 spin_lock_irqsave(&tty->buf.lock, flags);
588 if (tty->buf.tail != NULL)
589 tty->buf.tail->commit = tty->buf.tail->used;
590 spin_unlock_irqrestore(&tty->buf.lock, flags);
591 schedule_delayed_work(&tty->buf.work, 1);
593 EXPORT_SYMBOL(tty_schedule_flip);
596 * tty_prepare_flip_string - make room for characters
598 * @chars: return pointer for character write area
599 * @size: desired size
601 * Prepare a block of space in the buffer for data. Returns the length
602 * available and buffer pointer to the space which is now allocated and
603 * accounted for as ready for normal characters. This is used for drivers
604 * that need their own block copy routines into the buffer. There is no
605 * guarantee the buffer is a DMA target!
607 * Locking: May call functions taking tty->buf.lock
610 int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
613 int space = tty_buffer_request_room(tty, size);
615 struct tty_buffer *tb = tty->buf.tail;
616 *chars = tb->char_buf_ptr + tb->used;
617 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
623 EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
626 * tty_prepare_flip_string_flags - make room for characters
628 * @chars: return pointer for character write area
629 * @flags: return pointer for status flag write area
630 * @size: desired size
632 * Prepare a block of space in the buffer for data. Returns the length
633 * available and buffer pointer to the space which is now allocated and
634 * accounted for as ready for characters. This is used for drivers
635 * that need their own block copy routines into the buffer. There is no
636 * guarantee the buffer is a DMA target!
638 * Locking: May call functions taking tty->buf.lock
641 int tty_prepare_flip_string_flags(struct tty_struct *tty,
642 unsigned char **chars, char **flags, size_t size)
644 int space = tty_buffer_request_room(tty, size);
646 struct tty_buffer *tb = tty->buf.tail;
647 *chars = tb->char_buf_ptr + tb->used;
648 *flags = tb->flag_buf_ptr + tb->used;
654 EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
659 * get_tty_driver - find device of a tty
660 * @dev_t: device identifier
661 * @index: returns the index of the tty
663 * This routine returns a tty driver structure, given a device number
664 * and also passes back the index number.
666 * Locking: caller must hold tty_mutex
669 static struct tty_driver *get_tty_driver(dev_t device, int *index)
671 struct tty_driver *p;
673 list_for_each_entry(p, &tty_drivers, tty_drivers) {
674 dev_t base = MKDEV(p->major, p->minor_start);
675 if (device < base || device >= base + p->num)
677 *index = device - base;
683 #ifdef CONFIG_CONSOLE_POLL
686 * tty_find_polling_driver - find device of a polled tty
687 * @name: name string to match
688 * @line: pointer to resulting tty line nr
690 * This routine returns a tty driver structure, given a name
691 * and the condition that the tty driver is capable of polled
694 struct tty_driver *tty_find_polling_driver(char *name, int *line)
696 struct tty_driver *p, *res = NULL;
701 for (str = name; *str; str++)
702 if ((*str >= '0' && *str <= '9') || *str == ',')
708 tty_line = simple_strtoul(str, &str, 10);
710 mutex_lock(&tty_mutex);
711 /* Search through the tty devices to look for a match */
712 list_for_each_entry(p, &tty_drivers, tty_drivers) {
713 if (strncmp(name, p->name, len) != 0)
720 if (tty_line >= 0 && tty_line <= p->num && p->ops &&
721 p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) {
727 mutex_unlock(&tty_mutex);
731 EXPORT_SYMBOL_GPL(tty_find_polling_driver);
735 * tty_check_change - check for POSIX terminal changes
738 * If we try to write to, or set the state of, a terminal and we're
739 * not in the foreground, send a SIGTTOU. If the signal is blocked or
740 * ignored, go ahead and perform the operation. (POSIX 7.2)
745 int tty_check_change(struct tty_struct *tty)
750 if (current->signal->tty != tty)
753 spin_lock_irqsave(&tty->ctrl_lock, flags);
756 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
759 if (task_pgrp(current) == tty->pgrp)
761 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
762 if (is_ignored(SIGTTOU))
764 if (is_current_pgrp_orphaned()) {
768 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
769 set_thread_flag(TIF_SIGPENDING);
774 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
778 EXPORT_SYMBOL(tty_check_change);
780 static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
781 size_t count, loff_t *ppos)
786 static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
787 size_t count, loff_t *ppos)
792 /* No kernel lock held - none needed ;) */
793 static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
795 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
798 static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
801 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
804 static long hung_up_tty_compat_ioctl(struct file *file,
805 unsigned int cmd, unsigned long arg)
807 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
810 static const struct file_operations tty_fops = {
815 .unlocked_ioctl = tty_ioctl,
816 .compat_ioctl = tty_compat_ioctl,
818 .release = tty_release,
819 .fasync = tty_fasync,
822 #ifdef CONFIG_UNIX98_PTYS
823 static const struct file_operations ptmx_fops = {
828 .unlocked_ioctl = tty_ioctl,
829 .compat_ioctl = tty_compat_ioctl,
831 .release = tty_release,
832 .fasync = tty_fasync,
836 static const struct file_operations console_fops = {
839 .write = redirected_tty_write,
841 .unlocked_ioctl = tty_ioctl,
842 .compat_ioctl = tty_compat_ioctl,
844 .release = tty_release,
845 .fasync = tty_fasync,
848 static const struct file_operations hung_up_tty_fops = {
850 .read = hung_up_tty_read,
851 .write = hung_up_tty_write,
852 .poll = hung_up_tty_poll,
853 .unlocked_ioctl = hung_up_tty_ioctl,
854 .compat_ioctl = hung_up_tty_compat_ioctl,
855 .release = tty_release,
858 static DEFINE_SPINLOCK(redirect_lock);
859 static struct file *redirect;
862 * tty_wakeup - request more data
865 * Internal and external helper for wakeups of tty. This function
866 * informs the line discipline if present that the driver is ready
867 * to receive more output data.
870 void tty_wakeup(struct tty_struct *tty)
872 struct tty_ldisc *ld;
874 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
875 ld = tty_ldisc_ref(tty);
877 if (ld->ops->write_wakeup)
878 ld->ops->write_wakeup(tty);
882 wake_up_interruptible(&tty->write_wait);
885 EXPORT_SYMBOL_GPL(tty_wakeup);
888 * tty_ldisc_flush - flush line discipline queue
891 * Flush the line discipline queue (if any) for this tty. If there
892 * is no line discipline active this is a no-op.
895 void tty_ldisc_flush(struct tty_struct *tty)
897 struct tty_ldisc *ld = tty_ldisc_ref(tty);
899 if (ld->ops->flush_buffer)
900 ld->ops->flush_buffer(tty);
903 tty_buffer_flush(tty);
906 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
909 * tty_reset_termios - reset terminal state
912 * Restore a terminal to the driver default state
915 static void tty_reset_termios(struct tty_struct *tty)
917 mutex_lock(&tty->termios_mutex);
918 *tty->termios = tty->driver->init_termios;
919 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
920 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
921 mutex_unlock(&tty->termios_mutex);
925 * do_tty_hangup - actual handler for hangup events
928 * This can be called by the "eventd" kernel thread. That is process
929 * synchronous but doesn't hold any locks, so we need to make sure we
930 * have the appropriate locks for what we're doing.
932 * The hangup event clears any pending redirections onto the hung up
933 * device. It ensures future writes will error and it does the needed
934 * line discipline hangup and signal delivery. The tty object itself
939 * redirect lock for undoing redirection
940 * file list lock for manipulating list of ttys
941 * tty_ldisc_lock from called functions
942 * termios_mutex resetting termios data
943 * tasklist_lock to walk task list for hangup event
944 * ->siglock to protect ->signal/->sighand
946 static void do_tty_hangup(struct work_struct *work)
948 struct tty_struct *tty =
949 container_of(work, struct tty_struct, hangup_work);
950 struct file *cons_filp = NULL;
951 struct file *filp, *f = NULL;
952 struct task_struct *p;
953 struct tty_ldisc *ld;
954 int closecount = 0, n;
960 /* inuse_filps is protected by the single kernel lock */
963 spin_lock(&redirect_lock);
964 if (redirect && redirect->private_data == tty) {
968 spin_unlock(&redirect_lock);
970 check_tty_count(tty, "do_tty_hangup");
972 /* This breaks for file handles being sent over AF_UNIX sockets ? */
973 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
974 if (filp->f_op->write == redirected_tty_write)
976 if (filp->f_op->write != tty_write)
979 tty_fasync(-1, filp, 0); /* can't block */
980 filp->f_op = &hung_up_tty_fops;
984 * FIXME! What are the locking issues here? This may me overdoing
985 * things... This question is especially important now that we've
986 * removed the irqlock.
988 ld = tty_ldisc_ref(tty);
990 /* We may have no line discipline at this point */
991 if (ld->ops->flush_buffer)
992 ld->ops->flush_buffer(tty);
993 tty_driver_flush_buffer(tty);
994 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
995 ld->ops->write_wakeup)
996 ld->ops->write_wakeup(tty);
998 ld->ops->hangup(tty);
1001 * FIXME: Once we trust the LDISC code better we can wait here for
1002 * ldisc completion and fix the driver call race
1004 wake_up_interruptible(&tty->write_wait);
1005 wake_up_interruptible(&tty->read_wait);
1007 * Shutdown the current line discipline, and reset it to
1010 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1011 tty_reset_termios(tty);
1012 /* Defer ldisc switch */
1013 /* tty_deferred_ldisc_switch(N_TTY);
1015 This should get done automatically when the port closes and
1016 tty_release is called */
1018 read_lock(&tasklist_lock);
1020 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
1021 spin_lock_irq(&p->sighand->siglock);
1022 if (p->signal->tty == tty)
1023 p->signal->tty = NULL;
1024 if (!p->signal->leader) {
1025 spin_unlock_irq(&p->sighand->siglock);
1028 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
1029 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
1030 put_pid(p->signal->tty_old_pgrp); /* A noop */
1031 spin_lock_irqsave(&tty->ctrl_lock, flags);
1033 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
1034 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1035 spin_unlock_irq(&p->sighand->siglock);
1036 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
1038 read_unlock(&tasklist_lock);
1040 spin_lock_irqsave(&tty->ctrl_lock, flags);
1042 put_pid(tty->session);
1044 tty->session = NULL;
1046 tty->ctrl_status = 0;
1047 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1050 * If one of the devices matches a console pointer, we
1051 * cannot just call hangup() because that will cause
1052 * tty->count and state->count to go out of sync.
1053 * So we just call close() the right number of times.
1056 if (tty->ops->close)
1057 for (n = 0; n < closecount; n++)
1058 tty->ops->close(tty, cons_filp);
1059 } else if (tty->ops->hangup)
1060 (tty->ops->hangup)(tty);
1062 * We don't want to have driver/ldisc interactions beyond
1063 * the ones we did here. The driver layer expects no
1064 * calls after ->hangup() from the ldisc side. However we
1065 * can't yet guarantee all that.
1067 set_bit(TTY_HUPPED, &tty->flags);
1069 tty_ldisc_enable(tty);
1070 tty_ldisc_deref(ld);
1078 * tty_hangup - trigger a hangup event
1079 * @tty: tty to hangup
1081 * A carrier loss (virtual or otherwise) has occurred on this like
1082 * schedule a hangup sequence to run after this event.
1085 void tty_hangup(struct tty_struct *tty)
1087 #ifdef TTY_DEBUG_HANGUP
1089 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
1091 schedule_work(&tty->hangup_work);
1094 EXPORT_SYMBOL(tty_hangup);
1097 * tty_vhangup - process vhangup
1098 * @tty: tty to hangup
1100 * The user has asked via system call for the terminal to be hung up.
1101 * We do this synchronously so that when the syscall returns the process
1102 * is complete. That guarantee is necessary for security reasons.
1105 void tty_vhangup(struct tty_struct *tty)
1107 #ifdef TTY_DEBUG_HANGUP
1110 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
1112 do_tty_hangup(&tty->hangup_work);
1115 EXPORT_SYMBOL(tty_vhangup);
1118 * tty_hung_up_p - was tty hung up
1119 * @filp: file pointer of tty
1121 * Return true if the tty has been subject to a vhangup or a carrier
1125 int tty_hung_up_p(struct file *filp)
1127 return (filp->f_op == &hung_up_tty_fops);
1130 EXPORT_SYMBOL(tty_hung_up_p);
1132 static void session_clear_tty(struct pid *session)
1134 struct task_struct *p;
1135 do_each_pid_task(session, PIDTYPE_SID, p) {
1137 } while_each_pid_task(session, PIDTYPE_SID, p);
1141 * disassociate_ctty - disconnect controlling tty
1142 * @on_exit: true if exiting so need to "hang up" the session
1144 * This function is typically called only by the session leader, when
1145 * it wants to disassociate itself from its controlling tty.
1147 * It performs the following functions:
1148 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
1149 * (2) Clears the tty from being controlling the session
1150 * (3) Clears the controlling tty for all processes in the
1153 * The argument on_exit is set to 1 if called when a process is
1154 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
1157 * BKL is taken for hysterical raisins
1158 * tty_mutex is taken to protect tty
1159 * ->siglock is taken to protect ->signal/->sighand
1160 * tasklist_lock is taken to walk process list for sessions
1161 * ->siglock is taken to protect ->signal/->sighand
1164 void disassociate_ctty(int on_exit)
1166 struct tty_struct *tty;
1167 struct pid *tty_pgrp = NULL;
1170 mutex_lock(&tty_mutex);
1171 tty = get_current_tty();
1173 tty_pgrp = get_pid(tty->pgrp);
1175 mutex_unlock(&tty_mutex);
1176 /* XXX: here we race, there is nothing protecting tty */
1177 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
1180 } else if (on_exit) {
1181 struct pid *old_pgrp;
1182 spin_lock_irq(¤t->sighand->siglock);
1183 old_pgrp = current->signal->tty_old_pgrp;
1184 current->signal->tty_old_pgrp = NULL;
1185 spin_unlock_irq(¤t->sighand->siglock);
1187 kill_pgrp(old_pgrp, SIGHUP, on_exit);
1188 kill_pgrp(old_pgrp, SIGCONT, on_exit);
1191 mutex_unlock(&tty_mutex);
1195 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
1197 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
1201 spin_lock_irq(¤t->sighand->siglock);
1202 put_pid(current->signal->tty_old_pgrp);
1203 current->signal->tty_old_pgrp = NULL;
1204 spin_unlock_irq(¤t->sighand->siglock);
1206 mutex_lock(&tty_mutex);
1207 /* It is possible that do_tty_hangup has free'd this tty */
1208 tty = get_current_tty();
1210 unsigned long flags;
1211 spin_lock_irqsave(&tty->ctrl_lock, flags);
1212 put_pid(tty->session);
1214 tty->session = NULL;
1216 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1218 #ifdef TTY_DEBUG_HANGUP
1219 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
1223 mutex_unlock(&tty_mutex);
1225 /* Now clear signal->tty under the lock */
1226 read_lock(&tasklist_lock);
1227 session_clear_tty(task_session(current));
1228 read_unlock(&tasklist_lock);
1233 * no_tty - Ensure the current process does not have a controlling tty
1237 struct task_struct *tsk = current;
1239 if (tsk->signal->leader)
1240 disassociate_ctty(0);
1242 proc_clear_tty(tsk);
1247 * stop_tty - propagate flow control
1250 * Perform flow control to the driver. For PTY/TTY pairs we
1251 * must also propagate the TIOCKPKT status. May be called
1252 * on an already stopped device and will not re-call the driver
1255 * This functionality is used by both the line disciplines for
1256 * halting incoming flow and by the driver. It may therefore be
1257 * called from any context, may be under the tty atomic_write_lock
1261 * Uses the tty control lock internally
1264 void stop_tty(struct tty_struct *tty)
1266 unsigned long flags;
1267 spin_lock_irqsave(&tty->ctrl_lock, flags);
1269 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1273 if (tty->link && tty->link->packet) {
1274 tty->ctrl_status &= ~TIOCPKT_START;
1275 tty->ctrl_status |= TIOCPKT_STOP;
1276 wake_up_interruptible(&tty->link->read_wait);
1278 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1280 (tty->ops->stop)(tty);
1283 EXPORT_SYMBOL(stop_tty);
1286 * start_tty - propagate flow control
1287 * @tty: tty to start
1289 * Start a tty that has been stopped if at all possible. Perform
1290 * any necessary wakeups and propagate the TIOCPKT status. If this
1291 * is the tty was previous stopped and is being started then the
1292 * driver start method is invoked and the line discipline woken.
1298 void start_tty(struct tty_struct *tty)
1300 unsigned long flags;
1301 spin_lock_irqsave(&tty->ctrl_lock, flags);
1302 if (!tty->stopped || tty->flow_stopped) {
1303 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1307 if (tty->link && tty->link->packet) {
1308 tty->ctrl_status &= ~TIOCPKT_STOP;
1309 tty->ctrl_status |= TIOCPKT_START;
1310 wake_up_interruptible(&tty->link->read_wait);
1312 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1313 if (tty->ops->start)
1314 (tty->ops->start)(tty);
1315 /* If we have a running line discipline it may need kicking */
1319 EXPORT_SYMBOL(start_tty);
1322 * tty_read - read method for tty device files
1323 * @file: pointer to tty file
1325 * @count: size of user buffer
1328 * Perform the read system call function on this terminal device. Checks
1329 * for hung up devices before calling the line discipline method.
1332 * Locks the line discipline internally while needed. Multiple
1333 * read calls may be outstanding in parallel.
1336 static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
1340 struct tty_struct *tty;
1341 struct inode *inode;
1342 struct tty_ldisc *ld;
1344 tty = (struct tty_struct *)file->private_data;
1345 inode = file->f_path.dentry->d_inode;
1346 if (tty_paranoia_check(tty, inode, "tty_read"))
1348 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1351 /* We want to wait for the line discipline to sort out in this
1353 ld = tty_ldisc_ref_wait(tty);
1355 i = (ld->ops->read)(tty, file, buf, count);
1358 tty_ldisc_deref(ld);
1360 inode->i_atime = current_fs_time(inode->i_sb);
1364 void tty_write_unlock(struct tty_struct *tty)
1366 mutex_unlock(&tty->atomic_write_lock);
1367 wake_up_interruptible(&tty->write_wait);
1370 int tty_write_lock(struct tty_struct *tty, int ndelay)
1372 if (!mutex_trylock(&tty->atomic_write_lock)) {
1375 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1376 return -ERESTARTSYS;
1382 * Split writes up in sane blocksizes to avoid
1383 * denial-of-service type attacks
1385 static inline ssize_t do_tty_write(
1386 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1387 struct tty_struct *tty,
1389 const char __user *buf,
1392 ssize_t ret, written = 0;
1395 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1400 * We chunk up writes into a temporary buffer. This
1401 * simplifies low-level drivers immensely, since they
1402 * don't have locking issues and user mode accesses.
1404 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1407 * The default chunk-size is 2kB, because the NTTY
1408 * layer has problems with bigger chunks. It will
1409 * claim to be able to handle more characters than
1412 * FIXME: This can probably go away now except that 64K chunks
1413 * are too likely to fail unless switched to vmalloc...
1416 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1421 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1422 if (tty->write_cnt < chunk) {
1428 buf = kmalloc(chunk, GFP_KERNEL);
1433 kfree(tty->write_buf);
1434 tty->write_cnt = chunk;
1435 tty->write_buf = buf;
1438 /* Do the write .. */
1440 size_t size = count;
1444 if (copy_from_user(tty->write_buf, buf, size))
1446 ret = write(tty, file, tty->write_buf, size);
1455 if (signal_pending(current))
1460 struct inode *inode = file->f_path.dentry->d_inode;
1461 inode->i_mtime = current_fs_time(inode->i_sb);
1465 tty_write_unlock(tty);
1471 * tty_write - write method for tty device file
1472 * @file: tty file pointer
1473 * @buf: user data to write
1474 * @count: bytes to write
1477 * Write data to a tty device via the line discipline.
1480 * Locks the line discipline as required
1481 * Writes to the tty driver are serialized by the atomic_write_lock
1482 * and are then processed in chunks to the device. The line discipline
1483 * write method will not be involked in parallel for each device
1484 * The line discipline write method is called under the big
1485 * kernel lock for historical reasons. New code should not rely on this.
1488 static ssize_t tty_write(struct file *file, const char __user *buf,
1489 size_t count, loff_t *ppos)
1491 struct tty_struct *tty;
1492 struct inode *inode = file->f_path.dentry->d_inode;
1494 struct tty_ldisc *ld;
1496 tty = (struct tty_struct *)file->private_data;
1497 if (tty_paranoia_check(tty, inode, "tty_write"))
1499 if (!tty || !tty->ops->write ||
1500 (test_bit(TTY_IO_ERROR, &tty->flags)))
1502 /* Short term debug to catch buggy drivers */
1503 if (tty->ops->write_room == NULL)
1504 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1506 ld = tty_ldisc_ref_wait(tty);
1507 if (!ld->ops->write)
1510 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
1511 tty_ldisc_deref(ld);
1515 ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1516 size_t count, loff_t *ppos)
1518 struct file *p = NULL;
1520 spin_lock(&redirect_lock);
1525 spin_unlock(&redirect_lock);
1529 res = vfs_write(p, buf, count, &p->f_pos);
1533 return tty_write(file, buf, count, ppos);
1536 void tty_port_init(struct tty_port *port)
1538 memset(port, 0, sizeof(*port));
1539 init_waitqueue_head(&port->open_wait);
1540 init_waitqueue_head(&port->close_wait);
1541 mutex_init(&port->mutex);
1542 port->close_delay = (50 * HZ) / 100;
1543 port->closing_wait = (3000 * HZ) / 100;
1545 EXPORT_SYMBOL(tty_port_init);
1547 int tty_port_alloc_xmit_buf(struct tty_port *port)
1549 /* We may sleep in get_zeroed_page() */
1550 mutex_lock(&port->mutex);
1551 if (port->xmit_buf == NULL)
1552 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1553 mutex_unlock(&port->mutex);
1554 if (port->xmit_buf == NULL)
1558 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
1560 void tty_port_free_xmit_buf(struct tty_port *port)
1562 mutex_lock(&port->mutex);
1563 if (port->xmit_buf != NULL) {
1564 free_page((unsigned long)port->xmit_buf);
1565 port->xmit_buf = NULL;
1567 mutex_unlock(&port->mutex);
1569 EXPORT_SYMBOL(tty_port_free_xmit_buf);
1572 static char ptychar[] = "pqrstuvwxyzabcde";
1575 * pty_line_name - generate name for a pty
1576 * @driver: the tty driver in use
1577 * @index: the minor number
1578 * @p: output buffer of at least 6 bytes
1580 * Generate a name from a driver reference and write it to the output
1585 static void pty_line_name(struct tty_driver *driver, int index, char *p)
1587 int i = index + driver->name_base;
1588 /* ->name is initialized to "ttyp", but "tty" is expected */
1589 sprintf(p, "%s%c%x",
1590 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1591 ptychar[i >> 4 & 0xf], i & 0xf);
1595 * pty_line_name - generate name for a tty
1596 * @driver: the tty driver in use
1597 * @index: the minor number
1598 * @p: output buffer of at least 7 bytes
1600 * Generate a name from a driver reference and write it to the output
1605 static void tty_line_name(struct tty_driver *driver, int index, char *p)
1607 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1611 * init_dev - initialise a tty device
1612 * @driver: tty driver we are opening a device on
1613 * @idx: device index
1614 * @tty: returned tty structure
1616 * Prepare a tty device. This may not be a "new" clean device but
1617 * could also be an active device. The pty drivers require special
1618 * handling because of this.
1621 * The function is called under the tty_mutex, which
1622 * protects us from the tty struct or driver itself going away.
1624 * On exit the tty device has the line discipline attached and
1625 * a reference count of 1. If a pair was created for pty/tty use
1626 * and the other was a pty master then it too has a reference count of 1.
1628 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1629 * failed open. The new code protects the open with a mutex, so it's
1630 * really quite straightforward. The mutex locking can probably be
1631 * relaxed for the (most common) case of reopening a tty.
1634 static int init_dev(struct tty_driver *driver, int idx,
1635 struct tty_struct **ret_tty)
1637 struct tty_struct *tty, *o_tty;
1638 struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
1639 struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
1642 /* check whether we're reopening an existing tty */
1643 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1644 tty = devpts_get_tty(idx);
1646 * If we don't have a tty here on a slave open, it's because
1647 * the master already started the close process and there's
1648 * no relation between devpts file and tty anymore.
1650 if (!tty && driver->subtype == PTY_TYPE_SLAVE) {
1655 * It's safe from now on because init_dev() is called with
1656 * tty_mutex held and release_dev() won't change tty->count
1657 * or tty->flags without having to grab tty_mutex
1659 if (tty && driver->subtype == PTY_TYPE_MASTER)
1662 tty = driver->ttys[idx];
1664 if (tty) goto fast_track;
1667 * First time open is complex, especially for PTY devices.
1668 * This code guarantees that either everything succeeds and the
1669 * TTY is ready for operation, or else the table slots are vacated
1670 * and the allocated memory released. (Except that the termios
1671 * and locked termios may be retained.)
1674 if (!try_module_get(driver->owner)) {
1683 tty = alloc_tty_struct();
1686 initialize_tty_struct(tty);
1687 tty->driver = driver;
1688 tty->ops = driver->ops;
1690 tty_line_name(driver, idx, tty->name);
1692 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1693 tp_loc = &tty->termios;
1694 ltp_loc = &tty->termios_locked;
1696 tp_loc = &driver->termios[idx];
1697 ltp_loc = &driver->termios_locked[idx];
1701 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1704 *tp = driver->init_termios;
1708 ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
1713 if (driver->type == TTY_DRIVER_TYPE_PTY) {
1714 o_tty = alloc_tty_struct();
1717 initialize_tty_struct(o_tty);
1718 o_tty->driver = driver->other;
1719 o_tty->ops = driver->ops;
1721 tty_line_name(driver->other, idx, o_tty->name);
1723 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1724 o_tp_loc = &o_tty->termios;
1725 o_ltp_loc = &o_tty->termios_locked;
1727 o_tp_loc = &driver->other->termios[idx];
1728 o_ltp_loc = &driver->other->termios_locked[idx];
1732 o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1735 *o_tp = driver->other->init_termios;
1739 o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
1745 * Everything allocated ... set up the o_tty structure.
1747 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM))
1748 driver->other->ttys[idx] = o_tty;
1753 o_tty->termios = *o_tp_loc;
1754 o_tty->termios_locked = *o_ltp_loc;
1755 driver->other->refcount++;
1756 if (driver->subtype == PTY_TYPE_MASTER)
1759 /* Establish the links in both directions */
1765 * All structures have been allocated, so now we install them.
1766 * Failures after this point use release_tty to clean up, so
1767 * there's no need to null out the local pointers.
1769 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM))
1770 driver->ttys[idx] = tty;
1776 tty->termios = *tp_loc;
1777 tty->termios_locked = *ltp_loc;
1778 /* Compatibility until drivers always set this */
1779 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1780 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1785 * Structures all installed ... call the ldisc open routines.
1786 * If we fail here just call release_tty to clean up. No need
1787 * to decrement the use counts, as release_tty doesn't care.
1790 retval = tty_ldisc_setup(tty, o_tty);
1793 goto release_mem_out;
1797 * This fast open can be used if the tty is already open.
1798 * No memory is allocated, and the only failures are from
1799 * attempting to open a closing tty or attempting multiple
1800 * opens on a pty master.
1803 if (test_bit(TTY_CLOSING, &tty->flags)) {
1807 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1808 driver->subtype == PTY_TYPE_MASTER) {
1810 * special case for PTY masters: only one open permitted,
1811 * and the slave side open count is incremented as well.
1820 tty->driver = driver; /* N.B. why do this every time?? */
1823 if (!test_bit(TTY_LDISC, &tty->flags))
1824 printk(KERN_ERR "init_dev but no ldisc\n");
1828 /* All paths come through here to release the mutex */
1832 /* Release locally allocated memory ... nothing placed in slots */
1836 free_tty_struct(o_tty);
1839 free_tty_struct(tty);
1842 module_put(driver->owner);
1846 /* call the tty release_tty routine to clean out this slot */
1848 if (printk_ratelimit())
1849 printk(KERN_INFO "init_dev: ldisc open failed, "
1850 "clearing slot %d\n", idx);
1851 release_tty(tty, idx);
1856 * release_one_tty - release tty structure memory
1858 * Releases memory associated with a tty structure, and clears out the
1859 * driver table slots. This function is called when a device is no longer
1860 * in use. It also gets called when setup of a device fails.
1863 * tty_mutex - sometimes only
1864 * takes the file list lock internally when working on the list
1865 * of ttys that the driver keeps.
1866 * FIXME: should we require tty_mutex is held here ??
1868 static void release_one_tty(struct tty_struct *tty, int idx)
1870 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
1871 struct ktermios *tp;
1874 tty->driver->ttys[idx] = NULL;
1876 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1879 tty->driver->termios[idx] = NULL;
1882 tp = tty->termios_locked;
1884 tty->driver->termios_locked[idx] = NULL;
1890 tty->driver->refcount--;
1893 list_del_init(&tty->tty_files);
1896 free_tty_struct(tty);
1900 * release_tty - release tty structure memory
1902 * Release both @tty and a possible linked partner (think pty pair),
1903 * and decrement the refcount of the backing module.
1906 * tty_mutex - sometimes only
1907 * takes the file list lock internally when working on the list
1908 * of ttys that the driver keeps.
1909 * FIXME: should we require tty_mutex is held here ??
1911 static void release_tty(struct tty_struct *tty, int idx)
1913 struct tty_driver *driver = tty->driver;
1916 release_one_tty(tty->link, idx);
1917 release_one_tty(tty, idx);
1918 module_put(driver->owner);
1922 * Even releasing the tty structures is a tricky business.. We have
1923 * to be very careful that the structures are all released at the
1924 * same time, as interrupts might otherwise get the wrong pointers.
1926 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1927 * lead to double frees or releasing memory still in use.
1929 static void release_dev(struct file *filp)
1931 struct tty_struct *tty, *o_tty;
1932 int pty_master, tty_closing, o_tty_closing, do_sleep;
1937 tty = (struct tty_struct *)filp->private_data;
1938 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode,
1942 check_tty_count(tty, "release_dev");
1944 tty_fasync(-1, filp, 0);
1947 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1948 tty->driver->subtype == PTY_TYPE_MASTER);
1949 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1952 #ifdef TTY_PARANOIA_CHECK
1953 if (idx < 0 || idx >= tty->driver->num) {
1954 printk(KERN_DEBUG "release_dev: bad idx when trying to "
1955 "free (%s)\n", tty->name);
1958 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1959 if (tty != tty->driver->ttys[idx]) {
1960 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1961 "for (%s)\n", idx, tty->name);
1964 if (tty->termios != tty->driver->termios[idx]) {
1965 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1970 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
1971 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1972 "termios_locked for (%s)\n",
1979 #ifdef TTY_DEBUG_HANGUP
1980 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1981 tty_name(tty, buf), tty->count);
1984 #ifdef TTY_PARANOIA_CHECK
1985 if (tty->driver->other &&
1986 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1987 if (o_tty != tty->driver->other->ttys[idx]) {
1988 printk(KERN_DEBUG "release_dev: other->table[%d] "
1989 "not o_tty for (%s)\n",
1993 if (o_tty->termios != tty->driver->other->termios[idx]) {
1994 printk(KERN_DEBUG "release_dev: other->termios[%d] "
1995 "not o_termios for (%s)\n",
1999 if (o_tty->termios_locked !=
2000 tty->driver->other->termios_locked[idx]) {
2001 printk(KERN_DEBUG "release_dev: other->termios_locked["
2002 "%d] not o_termios_locked for (%s)\n",
2006 if (o_tty->link != tty) {
2007 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
2012 if (tty->ops->close)
2013 tty->ops->close(tty, filp);
2016 * Sanity check: if tty->count is going to zero, there shouldn't be
2017 * any waiters on tty->read_wait or tty->write_wait. We test the
2018 * wait queues and kick everyone out _before_ actually starting to
2019 * close. This ensures that we won't block while releasing the tty
2022 * The test for the o_tty closing is necessary, since the master and
2023 * slave sides may close in any order. If the slave side closes out
2024 * first, its count will be one, since the master side holds an open.
2025 * Thus this test wouldn't be triggered at the time the slave closes,
2028 * Note that it's possible for the tty to be opened again while we're
2029 * flushing out waiters. By recalculating the closing flags before
2030 * each iteration we avoid any problems.
2033 /* Guard against races with tty->count changes elsewhere and
2034 opens on /dev/tty */
2036 mutex_lock(&tty_mutex);
2037 tty_closing = tty->count <= 1;
2038 o_tty_closing = o_tty &&
2039 (o_tty->count <= (pty_master ? 1 : 0));
2043 if (waitqueue_active(&tty->read_wait)) {
2044 wake_up(&tty->read_wait);
2047 if (waitqueue_active(&tty->write_wait)) {
2048 wake_up(&tty->write_wait);
2052 if (o_tty_closing) {
2053 if (waitqueue_active(&o_tty->read_wait)) {
2054 wake_up(&o_tty->read_wait);
2057 if (waitqueue_active(&o_tty->write_wait)) {
2058 wake_up(&o_tty->write_wait);
2065 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
2066 "active!\n", tty_name(tty, buf));
2067 mutex_unlock(&tty_mutex);
2072 * The closing flags are now consistent with the open counts on
2073 * both sides, and we've completed the last operation that could
2074 * block, so it's safe to proceed with closing.
2077 if (--o_tty->count < 0) {
2078 printk(KERN_WARNING "release_dev: bad pty slave count "
2080 o_tty->count, tty_name(o_tty, buf));
2084 if (--tty->count < 0) {
2085 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
2086 tty->count, tty_name(tty, buf));
2091 * We've decremented tty->count, so we need to remove this file
2092 * descriptor off the tty->tty_files list; this serves two
2094 * - check_tty_count sees the correct number of file descriptors
2095 * associated with this tty.
2096 * - do_tty_hangup no longer sees this file descriptor as
2097 * something that needs to be handled for hangups.
2100 filp->private_data = NULL;
2103 * Perform some housekeeping before deciding whether to return.
2105 * Set the TTY_CLOSING flag if this was the last open. In the
2106 * case of a pty we may have to wait around for the other side
2107 * to close, and TTY_CLOSING makes sure we can't be reopened.
2110 set_bit(TTY_CLOSING, &tty->flags);
2112 set_bit(TTY_CLOSING, &o_tty->flags);
2115 * If _either_ side is closing, make sure there aren't any
2116 * processes that still think tty or o_tty is their controlling
2119 if (tty_closing || o_tty_closing) {
2120 read_lock(&tasklist_lock);
2121 session_clear_tty(tty->session);
2123 session_clear_tty(o_tty->session);
2124 read_unlock(&tasklist_lock);
2127 mutex_unlock(&tty_mutex);
2129 /* check whether both sides are closing ... */
2130 if (!tty_closing || (o_tty && !o_tty_closing))
2133 #ifdef TTY_DEBUG_HANGUP
2134 printk(KERN_DEBUG "freeing tty structure...");
2137 * Ask the line discipline code to release its structures
2139 tty_ldisc_release(tty, o_tty);
2141 * The release_tty function takes care of the details of clearing
2142 * the slots and preserving the termios structure.
2144 release_tty(tty, idx);
2146 /* Make this pty number available for reallocation */
2148 devpts_kill_index(idx);
2152 * tty_open - open a tty device
2153 * @inode: inode of device file
2154 * @filp: file pointer to tty
2156 * tty_open and tty_release keep up the tty count that contains the
2157 * number of opens done on a tty. We cannot use the inode-count, as
2158 * different inodes might point to the same tty.
2160 * Open-counting is needed for pty masters, as well as for keeping
2161 * track of serial lines: DTR is dropped when the last close happens.
2162 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2164 * The termios state of a pty is reset on first open so that
2165 * settings don't persist across reuse.
2167 * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
2168 * tty->count should protect the rest.
2169 * ->siglock protects ->signal/->sighand
2172 static int __tty_open(struct inode *inode, struct file *filp)
2174 struct tty_struct *tty;
2176 struct tty_driver *driver;
2178 dev_t device = inode->i_rdev;
2179 unsigned short saved_flags = filp->f_flags;
2181 nonseekable_open(inode, filp);
2184 noctty = filp->f_flags & O_NOCTTY;
2188 mutex_lock(&tty_mutex);
2190 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
2191 tty = get_current_tty();
2193 mutex_unlock(&tty_mutex);
2196 driver = tty->driver;
2198 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
2203 if (device == MKDEV(TTY_MAJOR, 0)) {
2204 extern struct tty_driver *console_driver;
2205 driver = console_driver;
2211 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
2212 driver = console_device(&index);
2214 /* Don't let /dev/console block */
2215 filp->f_flags |= O_NONBLOCK;
2219 mutex_unlock(&tty_mutex);
2223 driver = get_tty_driver(device, &index);
2225 mutex_unlock(&tty_mutex);
2229 retval = init_dev(driver, index, &tty);
2230 mutex_unlock(&tty_mutex);
2234 filp->private_data = tty;
2235 file_move(filp, &tty->tty_files);
2236 check_tty_count(tty, "tty_open");
2237 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2238 tty->driver->subtype == PTY_TYPE_MASTER)
2240 #ifdef TTY_DEBUG_HANGUP
2241 printk(KERN_DEBUG "opening %s...", tty->name);
2245 retval = tty->ops->open(tty, filp);
2249 filp->f_flags = saved_flags;
2251 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
2252 !capable(CAP_SYS_ADMIN))
2256 #ifdef TTY_DEBUG_HANGUP
2257 printk(KERN_DEBUG "error %d in opening %s...", retval,
2261 if (retval != -ERESTARTSYS)
2263 if (signal_pending(current))
2267 * Need to reset f_op in case a hangup happened.
2269 if (filp->f_op == &hung_up_tty_fops)
2270 filp->f_op = &tty_fops;
2274 mutex_lock(&tty_mutex);
2275 spin_lock_irq(¤t->sighand->siglock);
2277 current->signal->leader &&
2278 !current->signal->tty &&
2279 tty->session == NULL)
2280 __proc_set_tty(current, tty);
2281 spin_unlock_irq(¤t->sighand->siglock);
2282 mutex_unlock(&tty_mutex);
2286 /* BKL pushdown: scary code avoidance wrapper */
2287 static int tty_open(struct inode *inode, struct file *filp)
2292 ret = __tty_open(inode, filp);
2299 #ifdef CONFIG_UNIX98_PTYS
2301 * ptmx_open - open a unix 98 pty master
2302 * @inode: inode of device file
2303 * @filp: file pointer to tty
2305 * Allocate a unix98 pty master device from the ptmx driver.
2307 * Locking: tty_mutex protects theinit_dev work. tty->count should
2309 * allocated_ptys_lock handles the list of free pty numbers
2312 static int __ptmx_open(struct inode *inode, struct file *filp)
2314 struct tty_struct *tty;
2318 nonseekable_open(inode, filp);
2320 /* find a device that is not in use. */
2321 index = devpts_new_index();
2325 mutex_lock(&tty_mutex);
2326 retval = init_dev(ptm_driver, index, &tty);
2327 mutex_unlock(&tty_mutex);
2332 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
2333 filp->private_data = tty;
2334 file_move(filp, &tty->tty_files);
2336 retval = devpts_pty_new(tty->link);
2340 check_tty_count(tty, "ptmx_open");
2341 retval = ptm_driver->ops->open(tty, filp);
2348 devpts_kill_index(index);
2352 static int ptmx_open(struct inode *inode, struct file *filp)
2357 ret = __ptmx_open(inode, filp);
2364 * tty_release - vfs callback for close
2365 * @inode: inode of tty
2366 * @filp: file pointer for handle to tty
2368 * Called the last time each file handle is closed that references
2369 * this tty. There may however be several such references.
2372 * Takes bkl. See release_dev
2375 static int tty_release(struct inode *inode, struct file *filp)
2384 * tty_poll - check tty status
2385 * @filp: file being polled
2386 * @wait: poll wait structures to update
2388 * Call the line discipline polling method to obtain the poll
2389 * status of the device.
2391 * Locking: locks called line discipline but ldisc poll method
2392 * may be re-entered freely by other callers.
2395 static unsigned int tty_poll(struct file *filp, poll_table *wait)
2397 struct tty_struct *tty;
2398 struct tty_ldisc *ld;
2401 tty = (struct tty_struct *)filp->private_data;
2402 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
2405 ld = tty_ldisc_ref_wait(tty);
2407 ret = (ld->ops->poll)(tty, filp, wait);
2408 tty_ldisc_deref(ld);
2412 static int tty_fasync(int fd, struct file *filp, int on)
2414 struct tty_struct *tty;
2415 unsigned long flags;
2419 tty = (struct tty_struct *)filp->private_data;
2420 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
2423 retval = fasync_helper(fd, filp, on, &tty->fasync);
2430 if (!waitqueue_active(&tty->read_wait))
2431 tty->minimum_to_wake = 1;
2432 spin_lock_irqsave(&tty->ctrl_lock, flags);
2435 type = PIDTYPE_PGID;
2437 pid = task_pid(current);
2440 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2441 retval = __f_setown(filp, pid, type, 0);
2445 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2446 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2455 * tiocsti - fake input character
2456 * @tty: tty to fake input into
2457 * @p: pointer to character
2459 * Fake input to a tty device. Does the necessary locking and
2462 * FIXME: does not honour flow control ??
2465 * Called functions take tty_ldisc_lock
2466 * current->signal->tty check is safe without locks
2468 * FIXME: may race normal receive processing
2471 static int tiocsti(struct tty_struct *tty, char __user *p)
2474 struct tty_ldisc *ld;
2476 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2478 if (get_user(ch, p))
2480 ld = tty_ldisc_ref_wait(tty);
2481 ld->ops->receive_buf(tty, &ch, &mbz, 1);
2482 tty_ldisc_deref(ld);
2487 * tiocgwinsz - implement window query ioctl
2489 * @arg: user buffer for result
2491 * Copies the kernel idea of the window size into the user buffer.
2493 * Locking: tty->termios_mutex is taken to ensure the winsize data
2497 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
2501 mutex_lock(&tty->termios_mutex);
2502 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2503 mutex_unlock(&tty->termios_mutex);
2505 return err ? -EFAULT: 0;
2509 * tty_do_resize - resize event
2510 * @tty: tty being resized
2511 * @real_tty: real tty (not the same as tty if using a pty/tty pair)
2512 * @rows: rows (character)
2513 * @cols: cols (character)
2515 * Update the termios variables and send the neccessary signals to
2516 * peform a terminal resize correctly
2519 int tty_do_resize(struct tty_struct *tty, struct tty_struct *real_tty,
2522 struct pid *pgrp, *rpgrp;
2523 unsigned long flags;
2525 /* For a PTY we need to lock the tty side */
2526 mutex_lock(&real_tty->termios_mutex);
2527 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2529 /* Get the PID values and reference them so we can
2530 avoid holding the tty ctrl lock while sending signals */
2531 spin_lock_irqsave(&tty->ctrl_lock, flags);
2532 pgrp = get_pid(tty->pgrp);
2533 rpgrp = get_pid(real_tty->pgrp);
2534 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2537 kill_pgrp(pgrp, SIGWINCH, 1);
2538 if (rpgrp != pgrp && rpgrp)
2539 kill_pgrp(rpgrp, SIGWINCH, 1);
2545 real_tty->winsize = *ws;
2547 mutex_unlock(&real_tty->termios_mutex);
2552 * tiocswinsz - implement window size set ioctl
2554 * @arg: user buffer for result
2556 * Copies the user idea of the window size to the kernel. Traditionally
2557 * this is just advisory information but for the Linux console it
2558 * actually has driver level meaning and triggers a VC resize.
2561 * Driver dependant. The default do_resize method takes the
2562 * tty termios mutex and ctrl_lock. The console takes its own lock
2563 * then calls into the default method.
2566 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
2567 struct winsize __user *arg)
2569 struct winsize tmp_ws;
2570 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2573 if (tty->ops->resize)
2574 return tty->ops->resize(tty, real_tty, &tmp_ws);
2576 return tty_do_resize(tty, real_tty, &tmp_ws);
2580 * tioccons - allow admin to move logical console
2581 * @file: the file to become console
2583 * Allow the adminstrator to move the redirected console device
2585 * Locking: uses redirect_lock to guard the redirect information
2588 static int tioccons(struct file *file)
2590 if (!capable(CAP_SYS_ADMIN))
2592 if (file->f_op->write == redirected_tty_write) {
2594 spin_lock(&redirect_lock);
2597 spin_unlock(&redirect_lock);
2602 spin_lock(&redirect_lock);
2604 spin_unlock(&redirect_lock);
2609 spin_unlock(&redirect_lock);
2614 * fionbio - non blocking ioctl
2615 * @file: file to set blocking value
2616 * @p: user parameter
2618 * Historical tty interfaces had a blocking control ioctl before
2619 * the generic functionality existed. This piece of history is preserved
2620 * in the expected tty API of posix OS's.
2622 * Locking: none, the open fle handle ensures it won't go away.
2625 static int fionbio(struct file *file, int __user *p)
2629 if (get_user(nonblock, p))
2632 /* file->f_flags is still BKL protected in the fs layer - vomit */
2635 file->f_flags |= O_NONBLOCK;
2637 file->f_flags &= ~O_NONBLOCK;
2643 * tiocsctty - set controlling tty
2644 * @tty: tty structure
2645 * @arg: user argument
2647 * This ioctl is used to manage job control. It permits a session
2648 * leader to set this tty as the controlling tty for the session.
2651 * Takes tty_mutex() to protect tty instance
2652 * Takes tasklist_lock internally to walk sessions
2653 * Takes ->siglock() when updating signal->tty
2656 static int tiocsctty(struct tty_struct *tty, int arg)
2659 if (current->signal->leader && (task_session(current) == tty->session))
2662 mutex_lock(&tty_mutex);
2664 * The process must be a session leader and
2665 * not have a controlling tty already.
2667 if (!current->signal->leader || current->signal->tty) {
2674 * This tty is already the controlling
2675 * tty for another session group!
2677 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
2681 read_lock(&tasklist_lock);
2682 session_clear_tty(tty->session);
2683 read_unlock(&tasklist_lock);
2689 proc_set_tty(current, tty);
2691 mutex_unlock(&tty_mutex);
2696 * tty_get_pgrp - return a ref counted pgrp pid
2699 * Returns a refcounted instance of the pid struct for the process
2700 * group controlling the tty.
2703 struct pid *tty_get_pgrp(struct tty_struct *tty)
2705 unsigned long flags;
2708 spin_lock_irqsave(&tty->ctrl_lock, flags);
2709 pgrp = get_pid(tty->pgrp);
2710 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2714 EXPORT_SYMBOL_GPL(tty_get_pgrp);
2717 * tiocgpgrp - get process group
2718 * @tty: tty passed by user
2719 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2722 * Obtain the process group of the tty. If there is no process group
2725 * Locking: none. Reference to current->signal->tty is safe.
2728 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2733 * (tty == real_tty) is a cheap way of
2734 * testing if the tty is NOT a master pty.
2736 if (tty == real_tty && current->signal->tty != real_tty)
2738 pid = tty_get_pgrp(real_tty);
2739 ret = put_user(pid_vnr(pid), p);
2745 * tiocspgrp - attempt to set process group
2746 * @tty: tty passed by user
2747 * @real_tty: tty side device matching tty passed by user
2750 * Set the process group of the tty to the session passed. Only
2751 * permitted where the tty session is our session.
2753 * Locking: RCU, ctrl lock
2756 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2760 int retval = tty_check_change(real_tty);
2761 unsigned long flags;
2767 if (!current->signal->tty ||
2768 (current->signal->tty != real_tty) ||
2769 (real_tty->session != task_session(current)))
2771 if (get_user(pgrp_nr, p))
2776 pgrp = find_vpid(pgrp_nr);
2781 if (session_of_pgrp(pgrp) != task_session(current))
2784 spin_lock_irqsave(&tty->ctrl_lock, flags);
2785 put_pid(real_tty->pgrp);
2786 real_tty->pgrp = get_pid(pgrp);
2787 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2794 * tiocgsid - get session id
2795 * @tty: tty passed by user
2796 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2797 * @p: pointer to returned session id
2799 * Obtain the session id of the tty. If there is no session
2802 * Locking: none. Reference to current->signal->tty is safe.
2805 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2808 * (tty == real_tty) is a cheap way of
2809 * testing if the tty is NOT a master pty.
2811 if (tty == real_tty && current->signal->tty != real_tty)
2813 if (!real_tty->session)
2815 return put_user(pid_vnr(real_tty->session), p);
2819 * tiocsetd - set line discipline
2821 * @p: pointer to user data
2823 * Set the line discipline according to user request.
2825 * Locking: see tty_set_ldisc, this function is just a helper
2828 static int tiocsetd(struct tty_struct *tty, int __user *p)
2833 if (get_user(ldisc, p))
2837 ret = tty_set_ldisc(tty, ldisc);
2844 * send_break - performed time break
2845 * @tty: device to break on
2846 * @duration: timeout in mS
2848 * Perform a timed break on hardware that lacks its own driver level
2849 * timed break functionality.
2852 * atomic_write_lock serializes
2856 static int send_break(struct tty_struct *tty, unsigned int duration)
2860 if (tty->ops->break_ctl == NULL)
2863 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2864 retval = tty->ops->break_ctl(tty, duration);
2866 /* Do the work ourselves */
2867 if (tty_write_lock(tty, 0) < 0)
2869 retval = tty->ops->break_ctl(tty, -1);
2872 if (!signal_pending(current))
2873 msleep_interruptible(duration);
2874 retval = tty->ops->break_ctl(tty, 0);
2876 tty_write_unlock(tty);
2877 if (signal_pending(current))
2884 * tty_tiocmget - get modem status
2886 * @file: user file pointer
2887 * @p: pointer to result
2889 * Obtain the modem status bits from the tty driver if the feature
2890 * is supported. Return -EINVAL if it is not available.
2892 * Locking: none (up to the driver)
2895 static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
2897 int retval = -EINVAL;
2899 if (tty->ops->tiocmget) {
2900 retval = tty->ops->tiocmget(tty, file);
2903 retval = put_user(retval, p);
2909 * tty_tiocmset - set modem status
2911 * @file: user file pointer
2912 * @cmd: command - clear bits, set bits or set all
2913 * @p: pointer to desired bits
2915 * Set the modem status bits from the tty driver if the feature
2916 * is supported. Return -EINVAL if it is not available.
2918 * Locking: none (up to the driver)
2921 static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
2925 unsigned int set, clear, val;
2927 if (tty->ops->tiocmset == NULL)
2930 retval = get_user(val, p);
2946 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2947 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2948 return tty->ops->tiocmset(tty, file, set, clear);
2952 * Split this up, as gcc can choke on it otherwise..
2954 long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2956 struct tty_struct *tty, *real_tty;
2957 void __user *p = (void __user *)arg;
2959 struct tty_ldisc *ld;
2960 struct inode *inode = file->f_dentry->d_inode;
2962 tty = (struct tty_struct *)file->private_data;
2963 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2967 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2968 tty->driver->subtype == PTY_TYPE_MASTER)
2969 real_tty = tty->link;
2973 * Factor out some common prep work
2981 retval = tty_check_change(tty);
2984 if (cmd != TIOCCBRK) {
2985 tty_wait_until_sent(tty, 0);
2986 if (signal_pending(current))
2997 return tiocsti(tty, p);
2999 return tiocgwinsz(tty, p);
3001 return tiocswinsz(tty, real_tty, p);
3003 return real_tty != tty ? -EINVAL : tioccons(file);
3005 return fionbio(file, p);
3007 set_bit(TTY_EXCLUSIVE, &tty->flags);
3010 clear_bit(TTY_EXCLUSIVE, &tty->flags);
3013 if (current->signal->tty != tty)
3018 return tiocsctty(tty, arg);
3020 return tiocgpgrp(tty, real_tty, p);
3022 return tiocspgrp(tty, real_tty, p);
3024 return tiocgsid(tty, real_tty, p);
3026 return put_user(tty->ldisc.ops->num, (int __user *)p);
3028 return tiocsetd(tty, p);
3031 return tioclinux(tty, arg);
3036 case TIOCSBRK: /* Turn break on, unconditionally */
3037 if (tty->ops->break_ctl)
3038 return tty->ops->break_ctl(tty, -1);
3040 case TIOCCBRK: /* Turn break off, unconditionally */
3041 if (tty->ops->break_ctl)
3042 return tty->ops->break_ctl(tty, 0);
3044 case TCSBRK: /* SVID version: non-zero arg --> no break */
3045 /* non-zero arg means wait for all output data
3046 * to be sent (performed above) but don't send break.
3047 * This is used by the tcdrain() termios function.
3050 return send_break(tty, 250);
3052 case TCSBRKP: /* support for POSIX tcsendbreak() */
3053 return send_break(tty, arg ? arg*100 : 250);
3056 return tty_tiocmget(tty, file, p);
3060 return tty_tiocmset(tty, file, cmd, p);
3065 /* flush tty buffer and allow ldisc to process ioctl */
3066 tty_buffer_flush(tty);
3071 if (tty->ops->ioctl) {
3072 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
3073 if (retval != -ENOIOCTLCMD)
3076 ld = tty_ldisc_ref_wait(tty);
3078 if (ld->ops->ioctl) {
3079 retval = ld->ops->ioctl(tty, file, cmd, arg);
3080 if (retval == -ENOIOCTLCMD)
3083 tty_ldisc_deref(ld);
3087 #ifdef CONFIG_COMPAT
3088 static long tty_compat_ioctl(struct file *file, unsigned int cmd,
3091 struct inode *inode = file->f_dentry->d_inode;
3092 struct tty_struct *tty = file->private_data;
3093 struct tty_ldisc *ld;
3094 int retval = -ENOIOCTLCMD;
3096 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
3099 if (tty->ops->compat_ioctl) {
3100 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
3101 if (retval != -ENOIOCTLCMD)
3105 ld = tty_ldisc_ref_wait(tty);
3106 if (ld->ops->compat_ioctl)
3107 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
3108 tty_ldisc_deref(ld);
3115 * This implements the "Secure Attention Key" --- the idea is to
3116 * prevent trojan horses by killing all processes associated with this
3117 * tty when the user hits the "Secure Attention Key". Required for
3118 * super-paranoid applications --- see the Orange Book for more details.
3120 * This code could be nicer; ideally it should send a HUP, wait a few
3121 * seconds, then send a INT, and then a KILL signal. But you then
3122 * have to coordinate with the init process, since all processes associated
3123 * with the current tty must be dead before the new getty is allowed
3126 * Now, if it would be correct ;-/ The current code has a nasty hole -
3127 * it doesn't catch files in flight. We may send the descriptor to ourselves
3128 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
3130 * Nasty bug: do_SAK is being called in interrupt context. This can
3131 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3133 void __do_SAK(struct tty_struct *tty)
3138 struct task_struct *g, *p;
3139 struct pid *session;
3142 struct fdtable *fdt;
3146 session = tty->session;
3148 tty_ldisc_flush(tty);
3150 tty_driver_flush_buffer(tty);
3152 read_lock(&tasklist_lock);
3153 /* Kill the entire session */
3154 do_each_pid_task(session, PIDTYPE_SID, p) {
3155 printk(KERN_NOTICE "SAK: killed process %d"
3156 " (%s): task_session_nr(p)==tty->session\n",
3157 task_pid_nr(p), p->comm);
3158 send_sig(SIGKILL, p, 1);
3159 } while_each_pid_task(session, PIDTYPE_SID, p);
3160 /* Now kill any processes that happen to have the
3163 do_each_thread(g, p) {
3164 if (p->signal->tty == tty) {
3165 printk(KERN_NOTICE "SAK: killed process %d"
3166 " (%s): task_session_nr(p)==tty->session\n",
3167 task_pid_nr(p), p->comm);
3168 send_sig(SIGKILL, p, 1);
3174 * We don't take a ref to the file, so we must
3175 * hold ->file_lock instead.
3177 spin_lock(&p->files->file_lock);
3178 fdt = files_fdtable(p->files);
3179 for (i = 0; i < fdt->max_fds; i++) {
3180 filp = fcheck_files(p->files, i);
3183 if (filp->f_op->read == tty_read &&
3184 filp->private_data == tty) {
3185 printk(KERN_NOTICE "SAK: killed process %d"
3186 " (%s): fd#%d opened to the tty\n",
3187 task_pid_nr(p), p->comm, i);
3188 force_sig(SIGKILL, p);
3192 spin_unlock(&p->files->file_lock);
3195 } while_each_thread(g, p);
3196 read_unlock(&tasklist_lock);
3200 static void do_SAK_work(struct work_struct *work)
3202 struct tty_struct *tty =
3203 container_of(work, struct tty_struct, SAK_work);
3208 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3209 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3210 * the values which we write to it will be identical to the values which it
3211 * already has. --akpm
3213 void do_SAK(struct tty_struct *tty)
3217 schedule_work(&tty->SAK_work);
3220 EXPORT_SYMBOL(do_SAK);
3224 * @work: tty structure passed from work queue.
3226 * This routine is called out of the software interrupt to flush data
3227 * from the buffer chain to the line discipline.
3229 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
3230 * while invoking the line discipline receive_buf method. The
3231 * receive_buf method is single threaded for each tty instance.
3234 static void flush_to_ldisc(struct work_struct *work)
3236 struct tty_struct *tty =
3237 container_of(work, struct tty_struct, buf.work.work);
3238 unsigned long flags;
3239 struct tty_ldisc *disc;
3240 struct tty_buffer *tbuf, *head;
3242 unsigned char *flag_buf;
3244 disc = tty_ldisc_ref(tty);
3245 if (disc == NULL) /* !TTY_LDISC */
3248 spin_lock_irqsave(&tty->buf.lock, flags);
3249 /* So we know a flush is running */
3250 set_bit(TTY_FLUSHING, &tty->flags);
3251 head = tty->buf.head;
3253 tty->buf.head = NULL;
3255 int count = head->commit - head->read;
3257 if (head->next == NULL)
3261 tty_buffer_free(tty, tbuf);
3264 /* Ldisc or user is trying to flush the buffers
3265 we are feeding to the ldisc, stop feeding the
3266 line discipline as we want to empty the queue */
3267 if (test_bit(TTY_FLUSHPENDING, &tty->flags))
3269 if (!tty->receive_room) {
3270 schedule_delayed_work(&tty->buf.work, 1);
3273 if (count > tty->receive_room)
3274 count = tty->receive_room;
3275 char_buf = head->char_buf_ptr + head->read;
3276 flag_buf = head->flag_buf_ptr + head->read;
3277 head->read += count;
3278 spin_unlock_irqrestore(&tty->buf.lock, flags);
3279 disc->ops->receive_buf(tty, char_buf,
3281 spin_lock_irqsave(&tty->buf.lock, flags);
3283 /* Restore the queue head */
3284 tty->buf.head = head;
3286 /* We may have a deferred request to flush the input buffer,
3287 if so pull the chain under the lock and empty the queue */
3288 if (test_bit(TTY_FLUSHPENDING, &tty->flags)) {
3289 __tty_buffer_flush(tty);
3290 clear_bit(TTY_FLUSHPENDING, &tty->flags);
3291 wake_up(&tty->read_wait);
3293 clear_bit(TTY_FLUSHING, &tty->flags);
3294 spin_unlock_irqrestore(&tty->buf.lock, flags);
3296 tty_ldisc_deref(disc);
3300 * tty_flip_buffer_push - terminal
3303 * Queue a push of the terminal flip buffers to the line discipline. This
3304 * function must not be called from IRQ context if tty->low_latency is set.
3306 * In the event of the queue being busy for flipping the work will be
3307 * held off and retried later.
3309 * Locking: tty buffer lock. Driver locks in low latency mode.
3312 void tty_flip_buffer_push(struct tty_struct *tty)
3314 unsigned long flags;
3315 spin_lock_irqsave(&tty->buf.lock, flags);
3316 if (tty->buf.tail != NULL)
3317 tty->buf.tail->commit = tty->buf.tail->used;
3318 spin_unlock_irqrestore(&tty->buf.lock, flags);
3320 if (tty->low_latency)
3321 flush_to_ldisc(&tty->buf.work.work);
3323 schedule_delayed_work(&tty->buf.work, 1);
3326 EXPORT_SYMBOL(tty_flip_buffer_push);
3330 * initialize_tty_struct
3331 * @tty: tty to initialize
3333 * This subroutine initializes a tty structure that has been newly
3336 * Locking: none - tty in question must not be exposed at this point
3339 static void initialize_tty_struct(struct tty_struct *tty)
3341 memset(tty, 0, sizeof(struct tty_struct));
3342 tty->magic = TTY_MAGIC;
3343 tty_ldisc_init(tty);
3344 tty->session = NULL;
3346 tty->overrun_time = jiffies;
3347 tty->buf.head = tty->buf.tail = NULL;
3348 tty_buffer_init(tty);
3349 INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc);
3350 mutex_init(&tty->termios_mutex);
3351 init_waitqueue_head(&tty->write_wait);
3352 init_waitqueue_head(&tty->read_wait);
3353 INIT_WORK(&tty->hangup_work, do_tty_hangup);
3354 mutex_init(&tty->atomic_read_lock);
3355 mutex_init(&tty->atomic_write_lock);
3356 spin_lock_init(&tty->read_lock);
3357 spin_lock_init(&tty->ctrl_lock);
3358 INIT_LIST_HEAD(&tty->tty_files);
3359 INIT_WORK(&tty->SAK_work, do_SAK_work);
3363 * tty_put_char - write one character to a tty
3367 * Write one byte to the tty using the provided put_char method
3368 * if present. Returns the number of characters successfully output.
3370 * Note: the specific put_char operation in the driver layer may go
3371 * away soon. Don't call it directly, use this method
3374 int tty_put_char(struct tty_struct *tty, unsigned char ch)
3376 if (tty->ops->put_char)
3377 return tty->ops->put_char(tty, ch);
3378 return tty->ops->write(tty, &ch, 1);
3381 EXPORT_SYMBOL_GPL(tty_put_char);
3383 static struct class *tty_class;
3386 * tty_register_device - register a tty device
3387 * @driver: the tty driver that describes the tty device
3388 * @index: the index in the tty driver for this tty device
3389 * @device: a struct device that is associated with this tty device.
3390 * This field is optional, if there is no known struct device
3391 * for this tty device it can be set to NULL safely.
3393 * Returns a pointer to the struct device for this tty device
3394 * (or ERR_PTR(-EFOO) on error).
3396 * This call is required to be made to register an individual tty device
3397 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3398 * that bit is not set, this function should not be called by a tty
3404 struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3405 struct device *device)
3408 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
3410 if (index >= driver->num) {
3411 printk(KERN_ERR "Attempt to register invalid tty line number "
3413 return ERR_PTR(-EINVAL);
3416 if (driver->type == TTY_DRIVER_TYPE_PTY)
3417 pty_line_name(driver, index, name);
3419 tty_line_name(driver, index, name);
3421 return device_create_drvdata(tty_class, device, dev, NULL, name);
3425 * tty_unregister_device - unregister a tty device
3426 * @driver: the tty driver that describes the tty device
3427 * @index: the index in the tty driver for this tty device
3429 * If a tty device is registered with a call to tty_register_device() then
3430 * this function must be called when the tty device is gone.
3435 void tty_unregister_device(struct tty_driver *driver, unsigned index)
3437 device_destroy(tty_class,
3438 MKDEV(driver->major, driver->minor_start) + index);
3441 EXPORT_SYMBOL(tty_register_device);
3442 EXPORT_SYMBOL(tty_unregister_device);
3444 struct tty_driver *alloc_tty_driver(int lines)
3446 struct tty_driver *driver;
3448 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
3450 driver->magic = TTY_DRIVER_MAGIC;
3451 driver->num = lines;
3452 /* later we'll move allocation of tables here */
3457 void put_tty_driver(struct tty_driver *driver)
3462 void tty_set_operations(struct tty_driver *driver,
3463 const struct tty_operations *op)
3468 EXPORT_SYMBOL(alloc_tty_driver);
3469 EXPORT_SYMBOL(put_tty_driver);
3470 EXPORT_SYMBOL(tty_set_operations);
3473 * Called by a tty driver to register itself.
3475 int tty_register_driver(struct tty_driver *driver)
3482 if (driver->flags & TTY_DRIVER_INSTALLED)
3485 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
3486 p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
3491 if (!driver->major) {
3492 error = alloc_chrdev_region(&dev, driver->minor_start,
3493 driver->num, driver->name);
3495 driver->major = MAJOR(dev);
3496 driver->minor_start = MINOR(dev);
3499 dev = MKDEV(driver->major, driver->minor_start);
3500 error = register_chrdev_region(dev, driver->num, driver->name);
3508 driver->ttys = (struct tty_struct **)p;
3509 driver->termios = (struct ktermios **)(p + driver->num);
3510 driver->termios_locked = (struct ktermios **)
3511 (p + driver->num * 2);
3513 driver->ttys = NULL;
3514 driver->termios = NULL;
3515 driver->termios_locked = NULL;
3518 cdev_init(&driver->cdev, &tty_fops);
3519 driver->cdev.owner = driver->owner;
3520 error = cdev_add(&driver->cdev, dev, driver->num);
3522 unregister_chrdev_region(dev, driver->num);
3523 driver->ttys = NULL;
3524 driver->termios = driver->termios_locked = NULL;
3529 mutex_lock(&tty_mutex);
3530 list_add(&driver->tty_drivers, &tty_drivers);
3531 mutex_unlock(&tty_mutex);
3533 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
3534 for (i = 0; i < driver->num; i++)
3535 tty_register_device(driver, i, NULL);
3537 proc_tty_register_driver(driver);
3541 EXPORT_SYMBOL(tty_register_driver);
3544 * Called by a tty driver to unregister itself.
3546 int tty_unregister_driver(struct tty_driver *driver)
3549 struct ktermios *tp;
3552 if (driver->refcount)
3555 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3557 mutex_lock(&tty_mutex);
3558 list_del(&driver->tty_drivers);
3559 mutex_unlock(&tty_mutex);
3562 * Free the termios and termios_locked structures because
3563 * we don't want to get memory leaks when modular tty
3564 * drivers are removed from the kernel.
3566 for (i = 0; i < driver->num; i++) {
3567 tp = driver->termios[i];
3569 driver->termios[i] = NULL;
3572 tp = driver->termios_locked[i];
3574 driver->termios_locked[i] = NULL;
3577 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3578 tty_unregister_device(driver, i);
3581 proc_tty_unregister_driver(driver);
3582 driver->ttys = NULL;
3583 driver->termios = driver->termios_locked = NULL;
3585 cdev_del(&driver->cdev);
3588 EXPORT_SYMBOL(tty_unregister_driver);
3590 dev_t tty_devnum(struct tty_struct *tty)
3592 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3594 EXPORT_SYMBOL(tty_devnum);
3596 void proc_clear_tty(struct task_struct *p)
3598 spin_lock_irq(&p->sighand->siglock);
3599 p->signal->tty = NULL;
3600 spin_unlock_irq(&p->sighand->siglock);
3603 /* Called under the sighand lock */
3605 static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3608 unsigned long flags;
3609 /* We should not have a session or pgrp to put here but.... */
3610 spin_lock_irqsave(&tty->ctrl_lock, flags);
3611 put_pid(tty->session);
3613 tty->pgrp = get_pid(task_pgrp(tsk));
3614 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3615 tty->session = get_pid(task_session(tsk));
3617 put_pid(tsk->signal->tty_old_pgrp);
3618 tsk->signal->tty = tty;
3619 tsk->signal->tty_old_pgrp = NULL;
3622 static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3624 spin_lock_irq(&tsk->sighand->siglock);
3625 __proc_set_tty(tsk, tty);
3626 spin_unlock_irq(&tsk->sighand->siglock);
3629 struct tty_struct *get_current_tty(void)
3631 struct tty_struct *tty;
3632 WARN_ON_ONCE(!mutex_is_locked(&tty_mutex));
3633 tty = current->signal->tty;
3635 * session->tty can be changed/cleared from under us, make sure we
3636 * issue the load. The obtained pointer, when not NULL, is valid as
3637 * long as we hold tty_mutex.
3642 EXPORT_SYMBOL_GPL(get_current_tty);
3645 * Initialize the console device. This is called *early*, so
3646 * we can't necessarily depend on lots of kernel help here.
3647 * Just do some early initializations, and do the complex setup
3650 void __init console_init(void)
3654 /* Setup the default TTY line discipline. */
3658 * set up the console device so that later boot sequences can
3659 * inform about problems etc..
3661 call = __con_initcall_start;
3662 while (call < __con_initcall_end) {
3668 static int __init tty_class_init(void)
3670 tty_class = class_create(THIS_MODULE, "tty");
3671 if (IS_ERR(tty_class))
3672 return PTR_ERR(tty_class);
3676 postcore_initcall(tty_class_init);
3678 /* 3/2004 jmc: why do these devices exist? */
3680 static struct cdev tty_cdev, console_cdev;
3681 #ifdef CONFIG_UNIX98_PTYS
3682 static struct cdev ptmx_cdev;
3685 static struct cdev vc0_cdev;
3689 * Ok, now we can initialize the rest of the tty devices and can count
3690 * on memory allocations, interrupts etc..
3692 static int __init tty_init(void)
3694 cdev_init(&tty_cdev, &tty_fops);
3695 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3696 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3697 panic("Couldn't register /dev/tty driver\n");
3698 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
3701 cdev_init(&console_cdev, &console_fops);
3702 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3703 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3704 panic("Couldn't register /dev/console driver\n");
3705 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
3708 #ifdef CONFIG_UNIX98_PTYS
3709 cdev_init(&ptmx_cdev, &ptmx_fops);
3710 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
3711 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
3712 panic("Couldn't register /dev/ptmx driver\n");
3713 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
3717 cdev_init(&vc0_cdev, &console_fops);
3718 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3719 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3720 panic("Couldn't register /dev/tty0 driver\n");
3721 device_create_drvdata(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
3727 module_init(tty_init);