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
;
700 mutex_lock(&tty_mutex
);
701 /* Search through the tty devices to look for a match */
702 list_for_each_entry(p
, &tty_drivers
, tty_drivers
) {
703 str
= name
+ strlen(p
->name
);
704 tty_line
= simple_strtoul(str
, &str
, 10);
710 if (tty_line
>= 0 && tty_line
<= p
->num
&& p
->ops
&&
711 p
->ops
->poll_init
&& !p
->ops
->poll_init(p
, tty_line
, str
)) {
717 mutex_unlock(&tty_mutex
);
721 EXPORT_SYMBOL_GPL(tty_find_polling_driver
);
725 * tty_check_change - check for POSIX terminal changes
728 * If we try to write to, or set the state of, a terminal and we're
729 * not in the foreground, send a SIGTTOU. If the signal is blocked or
730 * ignored, go ahead and perform the operation. (POSIX 7.2)
735 int tty_check_change(struct tty_struct
*tty
)
740 if (current
->signal
->tty
!= tty
)
743 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
746 printk(KERN_WARNING
"tty_check_change: tty->pgrp == NULL!\n");
749 if (task_pgrp(current
) == tty
->pgrp
)
751 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
752 if (is_ignored(SIGTTOU
))
754 if (is_current_pgrp_orphaned()) {
758 kill_pgrp(task_pgrp(current
), SIGTTOU
, 1);
759 set_thread_flag(TIF_SIGPENDING
);
764 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
768 EXPORT_SYMBOL(tty_check_change
);
770 static ssize_t
hung_up_tty_read(struct file
*file
, char __user
*buf
,
771 size_t count
, loff_t
*ppos
)
776 static ssize_t
hung_up_tty_write(struct file
*file
, const char __user
*buf
,
777 size_t count
, loff_t
*ppos
)
782 /* No kernel lock held - none needed ;) */
783 static unsigned int hung_up_tty_poll(struct file
*filp
, poll_table
*wait
)
785 return POLLIN
| POLLOUT
| POLLERR
| POLLHUP
| POLLRDNORM
| POLLWRNORM
;
788 static long hung_up_tty_ioctl(struct file
*file
, unsigned int cmd
,
791 return cmd
== TIOCSPGRP
? -ENOTTY
: -EIO
;
794 static long hung_up_tty_compat_ioctl(struct file
*file
,
795 unsigned int cmd
, unsigned long arg
)
797 return cmd
== TIOCSPGRP
? -ENOTTY
: -EIO
;
800 static const struct file_operations tty_fops
= {
805 .unlocked_ioctl
= tty_ioctl
,
806 .compat_ioctl
= tty_compat_ioctl
,
808 .release
= tty_release
,
809 .fasync
= tty_fasync
,
812 #ifdef CONFIG_UNIX98_PTYS
813 static const struct file_operations ptmx_fops
= {
818 .unlocked_ioctl
= tty_ioctl
,
819 .compat_ioctl
= tty_compat_ioctl
,
821 .release
= tty_release
,
822 .fasync
= tty_fasync
,
826 static const struct file_operations console_fops
= {
829 .write
= redirected_tty_write
,
831 .unlocked_ioctl
= tty_ioctl
,
832 .compat_ioctl
= tty_compat_ioctl
,
834 .release
= tty_release
,
835 .fasync
= tty_fasync
,
838 static const struct file_operations hung_up_tty_fops
= {
840 .read
= hung_up_tty_read
,
841 .write
= hung_up_tty_write
,
842 .poll
= hung_up_tty_poll
,
843 .unlocked_ioctl
= hung_up_tty_ioctl
,
844 .compat_ioctl
= hung_up_tty_compat_ioctl
,
845 .release
= tty_release
,
848 static DEFINE_SPINLOCK(redirect_lock
);
849 static struct file
*redirect
;
852 * tty_wakeup - request more data
855 * Internal and external helper for wakeups of tty. This function
856 * informs the line discipline if present that the driver is ready
857 * to receive more output data.
860 void tty_wakeup(struct tty_struct
*tty
)
862 struct tty_ldisc
*ld
;
864 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) {
865 ld
= tty_ldisc_ref(tty
);
867 if (ld
->ops
->write_wakeup
)
868 ld
->ops
->write_wakeup(tty
);
872 wake_up_interruptible(&tty
->write_wait
);
875 EXPORT_SYMBOL_GPL(tty_wakeup
);
878 * tty_ldisc_flush - flush line discipline queue
881 * Flush the line discipline queue (if any) for this tty. If there
882 * is no line discipline active this is a no-op.
885 void tty_ldisc_flush(struct tty_struct
*tty
)
887 struct tty_ldisc
*ld
= tty_ldisc_ref(tty
);
889 if (ld
->ops
->flush_buffer
)
890 ld
->ops
->flush_buffer(tty
);
893 tty_buffer_flush(tty
);
896 EXPORT_SYMBOL_GPL(tty_ldisc_flush
);
899 * tty_reset_termios - reset terminal state
902 * Restore a terminal to the driver default state
905 static void tty_reset_termios(struct tty_struct
*tty
)
907 mutex_lock(&tty
->termios_mutex
);
908 *tty
->termios
= tty
->driver
->init_termios
;
909 tty
->termios
->c_ispeed
= tty_termios_input_baud_rate(tty
->termios
);
910 tty
->termios
->c_ospeed
= tty_termios_baud_rate(tty
->termios
);
911 mutex_unlock(&tty
->termios_mutex
);
915 * do_tty_hangup - actual handler for hangup events
918 * This can be called by the "eventd" kernel thread. That is process
919 * synchronous but doesn't hold any locks, so we need to make sure we
920 * have the appropriate locks for what we're doing.
922 * The hangup event clears any pending redirections onto the hung up
923 * device. It ensures future writes will error and it does the needed
924 * line discipline hangup and signal delivery. The tty object itself
929 * redirect lock for undoing redirection
930 * file list lock for manipulating list of ttys
931 * tty_ldisc_lock from called functions
932 * termios_mutex resetting termios data
933 * tasklist_lock to walk task list for hangup event
934 * ->siglock to protect ->signal/->sighand
936 static void do_tty_hangup(struct work_struct
*work
)
938 struct tty_struct
*tty
=
939 container_of(work
, struct tty_struct
, hangup_work
);
940 struct file
*cons_filp
= NULL
;
941 struct file
*filp
, *f
= NULL
;
942 struct task_struct
*p
;
943 struct tty_ldisc
*ld
;
944 int closecount
= 0, n
;
950 /* inuse_filps is protected by the single kernel lock */
953 spin_lock(&redirect_lock
);
954 if (redirect
&& redirect
->private_data
== tty
) {
958 spin_unlock(&redirect_lock
);
960 check_tty_count(tty
, "do_tty_hangup");
962 /* This breaks for file handles being sent over AF_UNIX sockets ? */
963 list_for_each_entry(filp
, &tty
->tty_files
, f_u
.fu_list
) {
964 if (filp
->f_op
->write
== redirected_tty_write
)
966 if (filp
->f_op
->write
!= tty_write
)
969 tty_fasync(-1, filp
, 0); /* can't block */
970 filp
->f_op
= &hung_up_tty_fops
;
974 * FIXME! What are the locking issues here? This may me overdoing
975 * things... This question is especially important now that we've
976 * removed the irqlock.
978 ld
= tty_ldisc_ref(tty
);
980 /* We may have no line discipline at this point */
981 if (ld
->ops
->flush_buffer
)
982 ld
->ops
->flush_buffer(tty
);
983 tty_driver_flush_buffer(tty
);
984 if ((test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) &&
985 ld
->ops
->write_wakeup
)
986 ld
->ops
->write_wakeup(tty
);
988 ld
->ops
->hangup(tty
);
991 * FIXME: Once we trust the LDISC code better we can wait here for
992 * ldisc completion and fix the driver call race
994 wake_up_interruptible(&tty
->write_wait
);
995 wake_up_interruptible(&tty
->read_wait
);
997 * Shutdown the current line discipline, and reset it to
1000 if (tty
->driver
->flags
& TTY_DRIVER_RESET_TERMIOS
)
1001 tty_reset_termios(tty
);
1002 /* Defer ldisc switch */
1003 /* tty_deferred_ldisc_switch(N_TTY);
1005 This should get done automatically when the port closes and
1006 tty_release is called */
1008 read_lock(&tasklist_lock
);
1010 do_each_pid_task(tty
->session
, PIDTYPE_SID
, p
) {
1011 spin_lock_irq(&p
->sighand
->siglock
);
1012 if (p
->signal
->tty
== tty
)
1013 p
->signal
->tty
= NULL
;
1014 if (!p
->signal
->leader
) {
1015 spin_unlock_irq(&p
->sighand
->siglock
);
1018 __group_send_sig_info(SIGHUP
, SEND_SIG_PRIV
, p
);
1019 __group_send_sig_info(SIGCONT
, SEND_SIG_PRIV
, p
);
1020 put_pid(p
->signal
->tty_old_pgrp
); /* A noop */
1021 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
1023 p
->signal
->tty_old_pgrp
= get_pid(tty
->pgrp
);
1024 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
1025 spin_unlock_irq(&p
->sighand
->siglock
);
1026 } while_each_pid_task(tty
->session
, PIDTYPE_SID
, p
);
1028 read_unlock(&tasklist_lock
);
1030 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
1032 put_pid(tty
->session
);
1034 tty
->session
= NULL
;
1036 tty
->ctrl_status
= 0;
1037 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
1040 * If one of the devices matches a console pointer, we
1041 * cannot just call hangup() because that will cause
1042 * tty->count and state->count to go out of sync.
1043 * So we just call close() the right number of times.
1046 if (tty
->ops
->close
)
1047 for (n
= 0; n
< closecount
; n
++)
1048 tty
->ops
->close(tty
, cons_filp
);
1049 } else if (tty
->ops
->hangup
)
1050 (tty
->ops
->hangup
)(tty
);
1052 * We don't want to have driver/ldisc interactions beyond
1053 * the ones we did here. The driver layer expects no
1054 * calls after ->hangup() from the ldisc side. However we
1055 * can't yet guarantee all that.
1057 set_bit(TTY_HUPPED
, &tty
->flags
);
1059 tty_ldisc_enable(tty
);
1060 tty_ldisc_deref(ld
);
1068 * tty_hangup - trigger a hangup event
1069 * @tty: tty to hangup
1071 * A carrier loss (virtual or otherwise) has occurred on this like
1072 * schedule a hangup sequence to run after this event.
1075 void tty_hangup(struct tty_struct
*tty
)
1077 #ifdef TTY_DEBUG_HANGUP
1079 printk(KERN_DEBUG
"%s hangup...\n", tty_name(tty
, buf
));
1081 schedule_work(&tty
->hangup_work
);
1084 EXPORT_SYMBOL(tty_hangup
);
1087 * tty_vhangup - process vhangup
1088 * @tty: tty to hangup
1090 * The user has asked via system call for the terminal to be hung up.
1091 * We do this synchronously so that when the syscall returns the process
1092 * is complete. That guarantee is necessary for security reasons.
1095 void tty_vhangup(struct tty_struct
*tty
)
1097 #ifdef TTY_DEBUG_HANGUP
1100 printk(KERN_DEBUG
"%s vhangup...\n", tty_name(tty
, buf
));
1102 do_tty_hangup(&tty
->hangup_work
);
1105 EXPORT_SYMBOL(tty_vhangup
);
1108 * tty_hung_up_p - was tty hung up
1109 * @filp: file pointer of tty
1111 * Return true if the tty has been subject to a vhangup or a carrier
1115 int tty_hung_up_p(struct file
*filp
)
1117 return (filp
->f_op
== &hung_up_tty_fops
);
1120 EXPORT_SYMBOL(tty_hung_up_p
);
1122 static void session_clear_tty(struct pid
*session
)
1124 struct task_struct
*p
;
1125 do_each_pid_task(session
, PIDTYPE_SID
, p
) {
1127 } while_each_pid_task(session
, PIDTYPE_SID
, p
);
1131 * disassociate_ctty - disconnect controlling tty
1132 * @on_exit: true if exiting so need to "hang up" the session
1134 * This function is typically called only by the session leader, when
1135 * it wants to disassociate itself from its controlling tty.
1137 * It performs the following functions:
1138 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
1139 * (2) Clears the tty from being controlling the session
1140 * (3) Clears the controlling tty for all processes in the
1143 * The argument on_exit is set to 1 if called when a process is
1144 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
1147 * BKL is taken for hysterical raisins
1148 * tty_mutex is taken to protect tty
1149 * ->siglock is taken to protect ->signal/->sighand
1150 * tasklist_lock is taken to walk process list for sessions
1151 * ->siglock is taken to protect ->signal/->sighand
1154 void disassociate_ctty(int on_exit
)
1156 struct tty_struct
*tty
;
1157 struct pid
*tty_pgrp
= NULL
;
1160 mutex_lock(&tty_mutex
);
1161 tty
= get_current_tty();
1163 tty_pgrp
= get_pid(tty
->pgrp
);
1164 mutex_unlock(&tty_mutex
);
1166 /* XXX: here we race, there is nothing protecting tty */
1167 if (on_exit
&& tty
->driver
->type
!= TTY_DRIVER_TYPE_PTY
)
1170 } else if (on_exit
) {
1171 struct pid
*old_pgrp
;
1172 spin_lock_irq(¤t
->sighand
->siglock
);
1173 old_pgrp
= current
->signal
->tty_old_pgrp
;
1174 current
->signal
->tty_old_pgrp
= NULL
;
1175 spin_unlock_irq(¤t
->sighand
->siglock
);
1177 kill_pgrp(old_pgrp
, SIGHUP
, on_exit
);
1178 kill_pgrp(old_pgrp
, SIGCONT
, on_exit
);
1181 mutex_unlock(&tty_mutex
);
1185 kill_pgrp(tty_pgrp
, SIGHUP
, on_exit
);
1187 kill_pgrp(tty_pgrp
, SIGCONT
, on_exit
);
1191 spin_lock_irq(¤t
->sighand
->siglock
);
1192 put_pid(current
->signal
->tty_old_pgrp
);
1193 current
->signal
->tty_old_pgrp
= NULL
;
1194 spin_unlock_irq(¤t
->sighand
->siglock
);
1196 mutex_lock(&tty_mutex
);
1197 /* It is possible that do_tty_hangup has free'd this tty */
1198 tty
= get_current_tty();
1200 unsigned long flags
;
1201 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
1202 put_pid(tty
->session
);
1204 tty
->session
= NULL
;
1206 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
1208 #ifdef TTY_DEBUG_HANGUP
1209 printk(KERN_DEBUG
"error attempted to write to tty [0x%p]"
1213 mutex_unlock(&tty_mutex
);
1215 /* Now clear signal->tty under the lock */
1216 read_lock(&tasklist_lock
);
1217 session_clear_tty(task_session(current
));
1218 read_unlock(&tasklist_lock
);
1223 * no_tty - Ensure the current process does not have a controlling tty
1227 struct task_struct
*tsk
= current
;
1229 if (tsk
->signal
->leader
)
1230 disassociate_ctty(0);
1232 proc_clear_tty(tsk
);
1237 * stop_tty - propagate flow control
1240 * Perform flow control to the driver. For PTY/TTY pairs we
1241 * must also propagate the TIOCKPKT status. May be called
1242 * on an already stopped device and will not re-call the driver
1245 * This functionality is used by both the line disciplines for
1246 * halting incoming flow and by the driver. It may therefore be
1247 * called from any context, may be under the tty atomic_write_lock
1251 * Uses the tty control lock internally
1254 void stop_tty(struct tty_struct
*tty
)
1256 unsigned long flags
;
1257 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
1259 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
1263 if (tty
->link
&& tty
->link
->packet
) {
1264 tty
->ctrl_status
&= ~TIOCPKT_START
;
1265 tty
->ctrl_status
|= TIOCPKT_STOP
;
1266 wake_up_interruptible(&tty
->link
->read_wait
);
1268 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
1270 (tty
->ops
->stop
)(tty
);
1273 EXPORT_SYMBOL(stop_tty
);
1276 * start_tty - propagate flow control
1277 * @tty: tty to start
1279 * Start a tty that has been stopped if at all possible. Perform
1280 * any necessary wakeups and propagate the TIOCPKT status. If this
1281 * is the tty was previous stopped and is being started then the
1282 * driver start method is invoked and the line discipline woken.
1288 void start_tty(struct tty_struct
*tty
)
1290 unsigned long flags
;
1291 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
1292 if (!tty
->stopped
|| tty
->flow_stopped
) {
1293 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
1297 if (tty
->link
&& tty
->link
->packet
) {
1298 tty
->ctrl_status
&= ~TIOCPKT_STOP
;
1299 tty
->ctrl_status
|= TIOCPKT_START
;
1300 wake_up_interruptible(&tty
->link
->read_wait
);
1302 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
1303 if (tty
->ops
->start
)
1304 (tty
->ops
->start
)(tty
);
1305 /* If we have a running line discipline it may need kicking */
1309 EXPORT_SYMBOL(start_tty
);
1312 * tty_read - read method for tty device files
1313 * @file: pointer to tty file
1315 * @count: size of user buffer
1318 * Perform the read system call function on this terminal device. Checks
1319 * for hung up devices before calling the line discipline method.
1322 * Locks the line discipline internally while needed. Multiple
1323 * read calls may be outstanding in parallel.
1326 static ssize_t
tty_read(struct file
*file
, char __user
*buf
, size_t count
,
1330 struct tty_struct
*tty
;
1331 struct inode
*inode
;
1332 struct tty_ldisc
*ld
;
1334 tty
= (struct tty_struct
*)file
->private_data
;
1335 inode
= file
->f_path
.dentry
->d_inode
;
1336 if (tty_paranoia_check(tty
, inode
, "tty_read"))
1338 if (!tty
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
1341 /* We want to wait for the line discipline to sort out in this
1343 ld
= tty_ldisc_ref_wait(tty
);
1345 i
= (ld
->ops
->read
)(tty
, file
, buf
, count
);
1348 tty_ldisc_deref(ld
);
1350 inode
->i_atime
= current_fs_time(inode
->i_sb
);
1354 void tty_write_unlock(struct tty_struct
*tty
)
1356 mutex_unlock(&tty
->atomic_write_lock
);
1357 wake_up_interruptible(&tty
->write_wait
);
1360 int tty_write_lock(struct tty_struct
*tty
, int ndelay
)
1362 if (!mutex_trylock(&tty
->atomic_write_lock
)) {
1365 if (mutex_lock_interruptible(&tty
->atomic_write_lock
))
1366 return -ERESTARTSYS
;
1372 * Split writes up in sane blocksizes to avoid
1373 * denial-of-service type attacks
1375 static inline ssize_t
do_tty_write(
1376 ssize_t (*write
)(struct tty_struct
*, struct file
*, const unsigned char *, size_t),
1377 struct tty_struct
*tty
,
1379 const char __user
*buf
,
1382 ssize_t ret
, written
= 0;
1385 ret
= tty_write_lock(tty
, file
->f_flags
& O_NDELAY
);
1390 * We chunk up writes into a temporary buffer. This
1391 * simplifies low-level drivers immensely, since they
1392 * don't have locking issues and user mode accesses.
1394 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1397 * The default chunk-size is 2kB, because the NTTY
1398 * layer has problems with bigger chunks. It will
1399 * claim to be able to handle more characters than
1402 * FIXME: This can probably go away now except that 64K chunks
1403 * are too likely to fail unless switched to vmalloc...
1406 if (test_bit(TTY_NO_WRITE_SPLIT
, &tty
->flags
))
1411 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1412 if (tty
->write_cnt
< chunk
) {
1418 buf
= kmalloc(chunk
, GFP_KERNEL
);
1423 kfree(tty
->write_buf
);
1424 tty
->write_cnt
= chunk
;
1425 tty
->write_buf
= buf
;
1428 /* Do the write .. */
1430 size_t size
= count
;
1434 if (copy_from_user(tty
->write_buf
, buf
, size
))
1436 ret
= write(tty
, file
, tty
->write_buf
, size
);
1445 if (signal_pending(current
))
1450 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1451 inode
->i_mtime
= current_fs_time(inode
->i_sb
);
1455 tty_write_unlock(tty
);
1461 * tty_write - write method for tty device file
1462 * @file: tty file pointer
1463 * @buf: user data to write
1464 * @count: bytes to write
1467 * Write data to a tty device via the line discipline.
1470 * Locks the line discipline as required
1471 * Writes to the tty driver are serialized by the atomic_write_lock
1472 * and are then processed in chunks to the device. The line discipline
1473 * write method will not be involked in parallel for each device
1474 * The line discipline write method is called under the big
1475 * kernel lock for historical reasons. New code should not rely on this.
1478 static ssize_t
tty_write(struct file
*file
, const char __user
*buf
,
1479 size_t count
, loff_t
*ppos
)
1481 struct tty_struct
*tty
;
1482 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1484 struct tty_ldisc
*ld
;
1486 tty
= (struct tty_struct
*)file
->private_data
;
1487 if (tty_paranoia_check(tty
, inode
, "tty_write"))
1489 if (!tty
|| !tty
->ops
->write
||
1490 (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
1492 /* Short term debug to catch buggy drivers */
1493 if (tty
->ops
->write_room
== NULL
)
1494 printk(KERN_ERR
"tty driver %s lacks a write_room method.\n",
1496 ld
= tty_ldisc_ref_wait(tty
);
1497 if (!ld
->ops
->write
)
1500 ret
= do_tty_write(ld
->ops
->write
, tty
, file
, buf
, count
);
1501 tty_ldisc_deref(ld
);
1505 ssize_t
redirected_tty_write(struct file
*file
, const char __user
*buf
,
1506 size_t count
, loff_t
*ppos
)
1508 struct file
*p
= NULL
;
1510 spin_lock(&redirect_lock
);
1515 spin_unlock(&redirect_lock
);
1519 res
= vfs_write(p
, buf
, count
, &p
->f_pos
);
1523 return tty_write(file
, buf
, count
, ppos
);
1526 void tty_port_init(struct tty_port
*port
)
1528 memset(port
, 0, sizeof(*port
));
1529 init_waitqueue_head(&port
->open_wait
);
1530 init_waitqueue_head(&port
->close_wait
);
1531 mutex_init(&port
->mutex
);
1532 port
->close_delay
= (50 * HZ
) / 100;
1533 port
->closing_wait
= (3000 * HZ
) / 100;
1535 EXPORT_SYMBOL(tty_port_init
);
1537 int tty_port_alloc_xmit_buf(struct tty_port
*port
)
1539 /* We may sleep in get_zeroed_page() */
1540 mutex_lock(&port
->mutex
);
1541 if (port
->xmit_buf
== NULL
)
1542 port
->xmit_buf
= (unsigned char *)get_zeroed_page(GFP_KERNEL
);
1543 mutex_unlock(&port
->mutex
);
1544 if (port
->xmit_buf
== NULL
)
1548 EXPORT_SYMBOL(tty_port_alloc_xmit_buf
);
1550 void tty_port_free_xmit_buf(struct tty_port
*port
)
1552 mutex_lock(&port
->mutex
);
1553 if (port
->xmit_buf
!= NULL
) {
1554 free_page((unsigned long)port
->xmit_buf
);
1555 port
->xmit_buf
= NULL
;
1557 mutex_unlock(&port
->mutex
);
1559 EXPORT_SYMBOL(tty_port_free_xmit_buf
);
1562 static char ptychar
[] = "pqrstuvwxyzabcde";
1565 * pty_line_name - generate name for a pty
1566 * @driver: the tty driver in use
1567 * @index: the minor number
1568 * @p: output buffer of at least 6 bytes
1570 * Generate a name from a driver reference and write it to the output
1575 static void pty_line_name(struct tty_driver
*driver
, int index
, char *p
)
1577 int i
= index
+ driver
->name_base
;
1578 /* ->name is initialized to "ttyp", but "tty" is expected */
1579 sprintf(p
, "%s%c%x",
1580 driver
->subtype
== PTY_TYPE_SLAVE
? "tty" : driver
->name
,
1581 ptychar
[i
>> 4 & 0xf], i
& 0xf);
1585 * pty_line_name - generate name for a tty
1586 * @driver: the tty driver in use
1587 * @index: the minor number
1588 * @p: output buffer of at least 7 bytes
1590 * Generate a name from a driver reference and write it to the output
1595 static void tty_line_name(struct tty_driver
*driver
, int index
, char *p
)
1597 sprintf(p
, "%s%d", driver
->name
, index
+ driver
->name_base
);
1601 * init_dev - initialise a tty device
1602 * @driver: tty driver we are opening a device on
1603 * @idx: device index
1604 * @tty: returned tty structure
1606 * Prepare a tty device. This may not be a "new" clean device but
1607 * could also be an active device. The pty drivers require special
1608 * handling because of this.
1611 * The function is called under the tty_mutex, which
1612 * protects us from the tty struct or driver itself going away.
1614 * On exit the tty device has the line discipline attached and
1615 * a reference count of 1. If a pair was created for pty/tty use
1616 * and the other was a pty master then it too has a reference count of 1.
1618 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1619 * failed open. The new code protects the open with a mutex, so it's
1620 * really quite straightforward. The mutex locking can probably be
1621 * relaxed for the (most common) case of reopening a tty.
1624 static int init_dev(struct tty_driver
*driver
, int idx
,
1625 struct tty_struct
**ret_tty
)
1627 struct tty_struct
*tty
, *o_tty
;
1628 struct ktermios
*tp
, **tp_loc
, *o_tp
, **o_tp_loc
;
1629 struct ktermios
*ltp
, **ltp_loc
, *o_ltp
, **o_ltp_loc
;
1632 /* check whether we're reopening an existing tty */
1633 if (driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) {
1634 tty
= devpts_get_tty(idx
);
1636 * If we don't have a tty here on a slave open, it's because
1637 * the master already started the close process and there's
1638 * no relation between devpts file and tty anymore.
1640 if (!tty
&& driver
->subtype
== PTY_TYPE_SLAVE
) {
1645 * It's safe from now on because init_dev() is called with
1646 * tty_mutex held and release_dev() won't change tty->count
1647 * or tty->flags without having to grab tty_mutex
1649 if (tty
&& driver
->subtype
== PTY_TYPE_MASTER
)
1652 tty
= driver
->ttys
[idx
];
1654 if (tty
) goto fast_track
;
1657 * First time open is complex, especially for PTY devices.
1658 * This code guarantees that either everything succeeds and the
1659 * TTY is ready for operation, or else the table slots are vacated
1660 * and the allocated memory released. (Except that the termios
1661 * and locked termios may be retained.)
1664 if (!try_module_get(driver
->owner
)) {
1673 tty
= alloc_tty_struct();
1676 initialize_tty_struct(tty
);
1677 tty
->driver
= driver
;
1678 tty
->ops
= driver
->ops
;
1680 tty_line_name(driver
, idx
, tty
->name
);
1682 if (driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) {
1683 tp_loc
= &tty
->termios
;
1684 ltp_loc
= &tty
->termios_locked
;
1686 tp_loc
= &driver
->termios
[idx
];
1687 ltp_loc
= &driver
->termios_locked
[idx
];
1691 tp
= kmalloc(sizeof(struct ktermios
), GFP_KERNEL
);
1694 *tp
= driver
->init_termios
;
1698 ltp
= kzalloc(sizeof(struct ktermios
), GFP_KERNEL
);
1703 if (driver
->type
== TTY_DRIVER_TYPE_PTY
) {
1704 o_tty
= alloc_tty_struct();
1707 initialize_tty_struct(o_tty
);
1708 o_tty
->driver
= driver
->other
;
1709 o_tty
->ops
= driver
->ops
;
1711 tty_line_name(driver
->other
, idx
, o_tty
->name
);
1713 if (driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) {
1714 o_tp_loc
= &o_tty
->termios
;
1715 o_ltp_loc
= &o_tty
->termios_locked
;
1717 o_tp_loc
= &driver
->other
->termios
[idx
];
1718 o_ltp_loc
= &driver
->other
->termios_locked
[idx
];
1722 o_tp
= kmalloc(sizeof(struct ktermios
), GFP_KERNEL
);
1725 *o_tp
= driver
->other
->init_termios
;
1729 o_ltp
= kzalloc(sizeof(struct ktermios
), GFP_KERNEL
);
1735 * Everything allocated ... set up the o_tty structure.
1737 if (!(driver
->other
->flags
& TTY_DRIVER_DEVPTS_MEM
))
1738 driver
->other
->ttys
[idx
] = o_tty
;
1743 o_tty
->termios
= *o_tp_loc
;
1744 o_tty
->termios_locked
= *o_ltp_loc
;
1745 driver
->other
->refcount
++;
1746 if (driver
->subtype
== PTY_TYPE_MASTER
)
1749 /* Establish the links in both directions */
1755 * All structures have been allocated, so now we install them.
1756 * Failures after this point use release_tty to clean up, so
1757 * there's no need to null out the local pointers.
1759 if (!(driver
->flags
& TTY_DRIVER_DEVPTS_MEM
))
1760 driver
->ttys
[idx
] = tty
;
1766 tty
->termios
= *tp_loc
;
1767 tty
->termios_locked
= *ltp_loc
;
1768 /* Compatibility until drivers always set this */
1769 tty
->termios
->c_ispeed
= tty_termios_input_baud_rate(tty
->termios
);
1770 tty
->termios
->c_ospeed
= tty_termios_baud_rate(tty
->termios
);
1775 * Structures all installed ... call the ldisc open routines.
1776 * If we fail here just call release_tty to clean up. No need
1777 * to decrement the use counts, as release_tty doesn't care.
1780 retval
= tty_ldisc_setup(tty
, o_tty
);
1783 goto release_mem_out
;
1787 * This fast open can be used if the tty is already open.
1788 * No memory is allocated, and the only failures are from
1789 * attempting to open a closing tty or attempting multiple
1790 * opens on a pty master.
1793 if (test_bit(TTY_CLOSING
, &tty
->flags
)) {
1797 if (driver
->type
== TTY_DRIVER_TYPE_PTY
&&
1798 driver
->subtype
== PTY_TYPE_MASTER
) {
1800 * special case for PTY masters: only one open permitted,
1801 * and the slave side open count is incremented as well.
1810 tty
->driver
= driver
; /* N.B. why do this every time?? */
1813 if (!test_bit(TTY_LDISC
, &tty
->flags
))
1814 printk(KERN_ERR
"init_dev but no ldisc\n");
1818 /* All paths come through here to release the mutex */
1822 /* Release locally allocated memory ... nothing placed in slots */
1826 free_tty_struct(o_tty
);
1829 free_tty_struct(tty
);
1832 module_put(driver
->owner
);
1836 /* call the tty release_tty routine to clean out this slot */
1838 if (printk_ratelimit())
1839 printk(KERN_INFO
"init_dev: ldisc open failed, "
1840 "clearing slot %d\n", idx
);
1841 release_tty(tty
, idx
);
1846 * release_one_tty - release tty structure memory
1848 * Releases memory associated with a tty structure, and clears out the
1849 * driver table slots. This function is called when a device is no longer
1850 * in use. It also gets called when setup of a device fails.
1853 * tty_mutex - sometimes only
1854 * takes the file list lock internally when working on the list
1855 * of ttys that the driver keeps.
1856 * FIXME: should we require tty_mutex is held here ??
1858 static void release_one_tty(struct tty_struct
*tty
, int idx
)
1860 int devpts
= tty
->driver
->flags
& TTY_DRIVER_DEVPTS_MEM
;
1861 struct ktermios
*tp
;
1864 tty
->driver
->ttys
[idx
] = NULL
;
1866 if (tty
->driver
->flags
& TTY_DRIVER_RESET_TERMIOS
) {
1869 tty
->driver
->termios
[idx
] = NULL
;
1872 tp
= tty
->termios_locked
;
1874 tty
->driver
->termios_locked
[idx
] = NULL
;
1880 tty
->driver
->refcount
--;
1883 list_del_init(&tty
->tty_files
);
1886 free_tty_struct(tty
);
1890 * release_tty - release tty structure memory
1892 * Release both @tty and a possible linked partner (think pty pair),
1893 * and decrement the refcount of the backing module.
1896 * tty_mutex - sometimes only
1897 * takes the file list lock internally when working on the list
1898 * of ttys that the driver keeps.
1899 * FIXME: should we require tty_mutex is held here ??
1901 static void release_tty(struct tty_struct
*tty
, int idx
)
1903 struct tty_driver
*driver
= tty
->driver
;
1906 release_one_tty(tty
->link
, idx
);
1907 release_one_tty(tty
, idx
);
1908 module_put(driver
->owner
);
1912 * Even releasing the tty structures is a tricky business.. We have
1913 * to be very careful that the structures are all released at the
1914 * same time, as interrupts might otherwise get the wrong pointers.
1916 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1917 * lead to double frees or releasing memory still in use.
1919 static void release_dev(struct file
*filp
)
1921 struct tty_struct
*tty
, *o_tty
;
1922 int pty_master
, tty_closing
, o_tty_closing
, do_sleep
;
1927 tty
= (struct tty_struct
*)filp
->private_data
;
1928 if (tty_paranoia_check(tty
, filp
->f_path
.dentry
->d_inode
,
1932 check_tty_count(tty
, "release_dev");
1934 tty_fasync(-1, filp
, 0);
1937 pty_master
= (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
1938 tty
->driver
->subtype
== PTY_TYPE_MASTER
);
1939 devpts
= (tty
->driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) != 0;
1942 #ifdef TTY_PARANOIA_CHECK
1943 if (idx
< 0 || idx
>= tty
->driver
->num
) {
1944 printk(KERN_DEBUG
"release_dev: bad idx when trying to "
1945 "free (%s)\n", tty
->name
);
1948 if (!(tty
->driver
->flags
& TTY_DRIVER_DEVPTS_MEM
)) {
1949 if (tty
!= tty
->driver
->ttys
[idx
]) {
1950 printk(KERN_DEBUG
"release_dev: driver.table[%d] not tty "
1951 "for (%s)\n", idx
, tty
->name
);
1954 if (tty
->termios
!= tty
->driver
->termios
[idx
]) {
1955 printk(KERN_DEBUG
"release_dev: driver.termios[%d] not termios "
1960 if (tty
->termios_locked
!= tty
->driver
->termios_locked
[idx
]) {
1961 printk(KERN_DEBUG
"release_dev: driver.termios_locked[%d] not "
1962 "termios_locked for (%s)\n",
1969 #ifdef TTY_DEBUG_HANGUP
1970 printk(KERN_DEBUG
"release_dev of %s (tty count=%d)...",
1971 tty_name(tty
, buf
), tty
->count
);
1974 #ifdef TTY_PARANOIA_CHECK
1975 if (tty
->driver
->other
&&
1976 !(tty
->driver
->flags
& TTY_DRIVER_DEVPTS_MEM
)) {
1977 if (o_tty
!= tty
->driver
->other
->ttys
[idx
]) {
1978 printk(KERN_DEBUG
"release_dev: other->table[%d] "
1979 "not o_tty for (%s)\n",
1983 if (o_tty
->termios
!= tty
->driver
->other
->termios
[idx
]) {
1984 printk(KERN_DEBUG
"release_dev: other->termios[%d] "
1985 "not o_termios for (%s)\n",
1989 if (o_tty
->termios_locked
!=
1990 tty
->driver
->other
->termios_locked
[idx
]) {
1991 printk(KERN_DEBUG
"release_dev: other->termios_locked["
1992 "%d] not o_termios_locked for (%s)\n",
1996 if (o_tty
->link
!= tty
) {
1997 printk(KERN_DEBUG
"release_dev: bad pty pointers\n");
2002 if (tty
->ops
->close
)
2003 tty
->ops
->close(tty
, filp
);
2006 * Sanity check: if tty->count is going to zero, there shouldn't be
2007 * any waiters on tty->read_wait or tty->write_wait. We test the
2008 * wait queues and kick everyone out _before_ actually starting to
2009 * close. This ensures that we won't block while releasing the tty
2012 * The test for the o_tty closing is necessary, since the master and
2013 * slave sides may close in any order. If the slave side closes out
2014 * first, its count will be one, since the master side holds an open.
2015 * Thus this test wouldn't be triggered at the time the slave closes,
2018 * Note that it's possible for the tty to be opened again while we're
2019 * flushing out waiters. By recalculating the closing flags before
2020 * each iteration we avoid any problems.
2023 /* Guard against races with tty->count changes elsewhere and
2024 opens on /dev/tty */
2026 mutex_lock(&tty_mutex
);
2027 tty_closing
= tty
->count
<= 1;
2028 o_tty_closing
= o_tty
&&
2029 (o_tty
->count
<= (pty_master
? 1 : 0));
2033 if (waitqueue_active(&tty
->read_wait
)) {
2034 wake_up(&tty
->read_wait
);
2037 if (waitqueue_active(&tty
->write_wait
)) {
2038 wake_up(&tty
->write_wait
);
2042 if (o_tty_closing
) {
2043 if (waitqueue_active(&o_tty
->read_wait
)) {
2044 wake_up(&o_tty
->read_wait
);
2047 if (waitqueue_active(&o_tty
->write_wait
)) {
2048 wake_up(&o_tty
->write_wait
);
2055 printk(KERN_WARNING
"release_dev: %s: read/write wait queue "
2056 "active!\n", tty_name(tty
, buf
));
2057 mutex_unlock(&tty_mutex
);
2062 * The closing flags are now consistent with the open counts on
2063 * both sides, and we've completed the last operation that could
2064 * block, so it's safe to proceed with closing.
2067 if (--o_tty
->count
< 0) {
2068 printk(KERN_WARNING
"release_dev: bad pty slave count "
2070 o_tty
->count
, tty_name(o_tty
, buf
));
2074 if (--tty
->count
< 0) {
2075 printk(KERN_WARNING
"release_dev: bad tty->count (%d) for %s\n",
2076 tty
->count
, tty_name(tty
, buf
));
2081 * We've decremented tty->count, so we need to remove this file
2082 * descriptor off the tty->tty_files list; this serves two
2084 * - check_tty_count sees the correct number of file descriptors
2085 * associated with this tty.
2086 * - do_tty_hangup no longer sees this file descriptor as
2087 * something that needs to be handled for hangups.
2090 filp
->private_data
= NULL
;
2093 * Perform some housekeeping before deciding whether to return.
2095 * Set the TTY_CLOSING flag if this was the last open. In the
2096 * case of a pty we may have to wait around for the other side
2097 * to close, and TTY_CLOSING makes sure we can't be reopened.
2100 set_bit(TTY_CLOSING
, &tty
->flags
);
2102 set_bit(TTY_CLOSING
, &o_tty
->flags
);
2105 * If _either_ side is closing, make sure there aren't any
2106 * processes that still think tty or o_tty is their controlling
2109 if (tty_closing
|| o_tty_closing
) {
2110 read_lock(&tasklist_lock
);
2111 session_clear_tty(tty
->session
);
2113 session_clear_tty(o_tty
->session
);
2114 read_unlock(&tasklist_lock
);
2117 mutex_unlock(&tty_mutex
);
2119 /* check whether both sides are closing ... */
2120 if (!tty_closing
|| (o_tty
&& !o_tty_closing
))
2123 #ifdef TTY_DEBUG_HANGUP
2124 printk(KERN_DEBUG
"freeing tty structure...");
2127 * Ask the line discipline code to release its structures
2129 tty_ldisc_release(tty
, o_tty
);
2131 * The release_tty function takes care of the details of clearing
2132 * the slots and preserving the termios structure.
2134 release_tty(tty
, idx
);
2136 /* Make this pty number available for reallocation */
2138 devpts_kill_index(idx
);
2142 * tty_open - open a tty device
2143 * @inode: inode of device file
2144 * @filp: file pointer to tty
2146 * tty_open and tty_release keep up the tty count that contains the
2147 * number of opens done on a tty. We cannot use the inode-count, as
2148 * different inodes might point to the same tty.
2150 * Open-counting is needed for pty masters, as well as for keeping
2151 * track of serial lines: DTR is dropped when the last close happens.
2152 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2154 * The termios state of a pty is reset on first open so that
2155 * settings don't persist across reuse.
2157 * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
2158 * tty->count should protect the rest.
2159 * ->siglock protects ->signal/->sighand
2162 static int __tty_open(struct inode
*inode
, struct file
*filp
)
2164 struct tty_struct
*tty
;
2166 struct tty_driver
*driver
;
2168 dev_t device
= inode
->i_rdev
;
2169 unsigned short saved_flags
= filp
->f_flags
;
2171 nonseekable_open(inode
, filp
);
2174 noctty
= filp
->f_flags
& O_NOCTTY
;
2178 mutex_lock(&tty_mutex
);
2180 if (device
== MKDEV(TTYAUX_MAJOR
, 0)) {
2181 tty
= get_current_tty();
2183 mutex_unlock(&tty_mutex
);
2186 driver
= tty
->driver
;
2188 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/tty block */
2193 if (device
== MKDEV(TTY_MAJOR
, 0)) {
2194 extern struct tty_driver
*console_driver
;
2195 driver
= console_driver
;
2201 if (device
== MKDEV(TTYAUX_MAJOR
, 1)) {
2202 driver
= console_device(&index
);
2204 /* Don't let /dev/console block */
2205 filp
->f_flags
|= O_NONBLOCK
;
2209 mutex_unlock(&tty_mutex
);
2213 driver
= get_tty_driver(device
, &index
);
2215 mutex_unlock(&tty_mutex
);
2219 retval
= init_dev(driver
, index
, &tty
);
2220 mutex_unlock(&tty_mutex
);
2224 filp
->private_data
= tty
;
2225 file_move(filp
, &tty
->tty_files
);
2226 check_tty_count(tty
, "tty_open");
2227 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
2228 tty
->driver
->subtype
== PTY_TYPE_MASTER
)
2230 #ifdef TTY_DEBUG_HANGUP
2231 printk(KERN_DEBUG
"opening %s...", tty
->name
);
2235 retval
= tty
->ops
->open(tty
, filp
);
2239 filp
->f_flags
= saved_flags
;
2241 if (!retval
&& test_bit(TTY_EXCLUSIVE
, &tty
->flags
) &&
2242 !capable(CAP_SYS_ADMIN
))
2246 #ifdef TTY_DEBUG_HANGUP
2247 printk(KERN_DEBUG
"error %d in opening %s...", retval
,
2251 if (retval
!= -ERESTARTSYS
)
2253 if (signal_pending(current
))
2257 * Need to reset f_op in case a hangup happened.
2259 if (filp
->f_op
== &hung_up_tty_fops
)
2260 filp
->f_op
= &tty_fops
;
2264 mutex_lock(&tty_mutex
);
2265 spin_lock_irq(¤t
->sighand
->siglock
);
2267 current
->signal
->leader
&&
2268 !current
->signal
->tty
&&
2269 tty
->session
== NULL
)
2270 __proc_set_tty(current
, tty
);
2271 spin_unlock_irq(¤t
->sighand
->siglock
);
2272 mutex_unlock(&tty_mutex
);
2276 /* BKL pushdown: scary code avoidance wrapper */
2277 static int tty_open(struct inode
*inode
, struct file
*filp
)
2282 ret
= __tty_open(inode
, filp
);
2289 #ifdef CONFIG_UNIX98_PTYS
2291 * ptmx_open - open a unix 98 pty master
2292 * @inode: inode of device file
2293 * @filp: file pointer to tty
2295 * Allocate a unix98 pty master device from the ptmx driver.
2297 * Locking: tty_mutex protects theinit_dev work. tty->count should
2299 * allocated_ptys_lock handles the list of free pty numbers
2302 static int __ptmx_open(struct inode
*inode
, struct file
*filp
)
2304 struct tty_struct
*tty
;
2308 nonseekable_open(inode
, filp
);
2310 /* find a device that is not in use. */
2311 index
= devpts_new_index();
2315 mutex_lock(&tty_mutex
);
2316 retval
= init_dev(ptm_driver
, index
, &tty
);
2317 mutex_unlock(&tty_mutex
);
2322 set_bit(TTY_PTY_LOCK
, &tty
->flags
); /* LOCK THE SLAVE */
2323 filp
->private_data
= tty
;
2324 file_move(filp
, &tty
->tty_files
);
2326 retval
= devpts_pty_new(tty
->link
);
2330 check_tty_count(tty
, "ptmx_open");
2331 retval
= ptm_driver
->ops
->open(tty
, filp
);
2338 devpts_kill_index(index
);
2342 static int ptmx_open(struct inode
*inode
, struct file
*filp
)
2347 ret
= __ptmx_open(inode
, filp
);
2354 * tty_release - vfs callback for close
2355 * @inode: inode of tty
2356 * @filp: file pointer for handle to tty
2358 * Called the last time each file handle is closed that references
2359 * this tty. There may however be several such references.
2362 * Takes bkl. See release_dev
2365 static int tty_release(struct inode
*inode
, struct file
*filp
)
2374 * tty_poll - check tty status
2375 * @filp: file being polled
2376 * @wait: poll wait structures to update
2378 * Call the line discipline polling method to obtain the poll
2379 * status of the device.
2381 * Locking: locks called line discipline but ldisc poll method
2382 * may be re-entered freely by other callers.
2385 static unsigned int tty_poll(struct file
*filp
, poll_table
*wait
)
2387 struct tty_struct
*tty
;
2388 struct tty_ldisc
*ld
;
2391 tty
= (struct tty_struct
*)filp
->private_data
;
2392 if (tty_paranoia_check(tty
, filp
->f_path
.dentry
->d_inode
, "tty_poll"))
2395 ld
= tty_ldisc_ref_wait(tty
);
2397 ret
= (ld
->ops
->poll
)(tty
, filp
, wait
);
2398 tty_ldisc_deref(ld
);
2402 static int tty_fasync(int fd
, struct file
*filp
, int on
)
2404 struct tty_struct
*tty
;
2405 unsigned long flags
;
2409 tty
= (struct tty_struct
*)filp
->private_data
;
2410 if (tty_paranoia_check(tty
, filp
->f_path
.dentry
->d_inode
, "tty_fasync"))
2413 retval
= fasync_helper(fd
, filp
, on
, &tty
->fasync
);
2420 if (!waitqueue_active(&tty
->read_wait
))
2421 tty
->minimum_to_wake
= 1;
2422 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
2425 type
= PIDTYPE_PGID
;
2427 pid
= task_pid(current
);
2430 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
2431 retval
= __f_setown(filp
, pid
, type
, 0);
2435 if (!tty
->fasync
&& !waitqueue_active(&tty
->read_wait
))
2436 tty
->minimum_to_wake
= N_TTY_BUF_SIZE
;
2445 * tiocsti - fake input character
2446 * @tty: tty to fake input into
2447 * @p: pointer to character
2449 * Fake input to a tty device. Does the necessary locking and
2452 * FIXME: does not honour flow control ??
2455 * Called functions take tty_ldisc_lock
2456 * current->signal->tty check is safe without locks
2458 * FIXME: may race normal receive processing
2461 static int tiocsti(struct tty_struct
*tty
, char __user
*p
)
2464 struct tty_ldisc
*ld
;
2466 if ((current
->signal
->tty
!= tty
) && !capable(CAP_SYS_ADMIN
))
2468 if (get_user(ch
, p
))
2470 ld
= tty_ldisc_ref_wait(tty
);
2471 ld
->ops
->receive_buf(tty
, &ch
, &mbz
, 1);
2472 tty_ldisc_deref(ld
);
2477 * tiocgwinsz - implement window query ioctl
2479 * @arg: user buffer for result
2481 * Copies the kernel idea of the window size into the user buffer.
2483 * Locking: tty->termios_mutex is taken to ensure the winsize data
2487 static int tiocgwinsz(struct tty_struct
*tty
, struct winsize __user
*arg
)
2491 mutex_lock(&tty
->termios_mutex
);
2492 err
= copy_to_user(arg
, &tty
->winsize
, sizeof(*arg
));
2493 mutex_unlock(&tty
->termios_mutex
);
2495 return err
? -EFAULT
: 0;
2499 * tiocswinsz - implement window size set ioctl
2501 * @arg: user buffer for result
2503 * Copies the user idea of the window size to the kernel. Traditionally
2504 * this is just advisory information but for the Linux console it
2505 * actually has driver level meaning and triggers a VC resize.
2508 * Called function use the console_sem is used to ensure we do
2509 * not try and resize the console twice at once.
2510 * The tty->termios_mutex is used to ensure we don't double
2511 * resize and get confused. Lock order - tty->termios_mutex before
2515 static int tiocswinsz(struct tty_struct
*tty
, struct tty_struct
*real_tty
,
2516 struct winsize __user
*arg
)
2518 struct winsize tmp_ws
;
2519 struct pid
*pgrp
, *rpgrp
;
2520 unsigned long flags
;
2522 if (copy_from_user(&tmp_ws
, arg
, sizeof(*arg
)))
2525 mutex_lock(&tty
->termios_mutex
);
2526 if (!memcmp(&tmp_ws
, &tty
->winsize
, sizeof(*arg
)))
2530 if (tty
->driver
->type
== TTY_DRIVER_TYPE_CONSOLE
) {
2531 if (vc_lock_resize(tty
->driver_data
, tmp_ws
.ws_col
,
2533 mutex_unlock(&tty
->termios_mutex
);
2538 /* Get the PID values and reference them so we can
2539 avoid holding the tty ctrl lock while sending signals */
2540 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
2541 pgrp
= get_pid(tty
->pgrp
);
2542 rpgrp
= get_pid(real_tty
->pgrp
);
2543 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
2546 kill_pgrp(pgrp
, SIGWINCH
, 1);
2547 if (rpgrp
!= pgrp
&& rpgrp
)
2548 kill_pgrp(rpgrp
, SIGWINCH
, 1);
2553 tty
->winsize
= tmp_ws
;
2554 real_tty
->winsize
= tmp_ws
;
2556 mutex_unlock(&tty
->termios_mutex
);
2561 * tioccons - allow admin to move logical console
2562 * @file: the file to become console
2564 * Allow the adminstrator to move the redirected console device
2566 * Locking: uses redirect_lock to guard the redirect information
2569 static int tioccons(struct file
*file
)
2571 if (!capable(CAP_SYS_ADMIN
))
2573 if (file
->f_op
->write
== redirected_tty_write
) {
2575 spin_lock(&redirect_lock
);
2578 spin_unlock(&redirect_lock
);
2583 spin_lock(&redirect_lock
);
2585 spin_unlock(&redirect_lock
);
2590 spin_unlock(&redirect_lock
);
2595 * fionbio - non blocking ioctl
2596 * @file: file to set blocking value
2597 * @p: user parameter
2599 * Historical tty interfaces had a blocking control ioctl before
2600 * the generic functionality existed. This piece of history is preserved
2601 * in the expected tty API of posix OS's.
2603 * Locking: none, the open fle handle ensures it won't go away.
2606 static int fionbio(struct file
*file
, int __user
*p
)
2610 if (get_user(nonblock
, p
))
2613 /* file->f_flags is still BKL protected in the fs layer - vomit */
2616 file
->f_flags
|= O_NONBLOCK
;
2618 file
->f_flags
&= ~O_NONBLOCK
;
2624 * tiocsctty - set controlling tty
2625 * @tty: tty structure
2626 * @arg: user argument
2628 * This ioctl is used to manage job control. It permits a session
2629 * leader to set this tty as the controlling tty for the session.
2632 * Takes tty_mutex() to protect tty instance
2633 * Takes tasklist_lock internally to walk sessions
2634 * Takes ->siglock() when updating signal->tty
2637 static int tiocsctty(struct tty_struct
*tty
, int arg
)
2640 if (current
->signal
->leader
&& (task_session(current
) == tty
->session
))
2643 mutex_lock(&tty_mutex
);
2645 * The process must be a session leader and
2646 * not have a controlling tty already.
2648 if (!current
->signal
->leader
|| current
->signal
->tty
) {
2655 * This tty is already the controlling
2656 * tty for another session group!
2658 if (arg
== 1 && capable(CAP_SYS_ADMIN
)) {
2662 read_lock(&tasklist_lock
);
2663 session_clear_tty(tty
->session
);
2664 read_unlock(&tasklist_lock
);
2670 proc_set_tty(current
, tty
);
2672 mutex_unlock(&tty_mutex
);
2677 * tty_get_pgrp - return a ref counted pgrp pid
2680 * Returns a refcounted instance of the pid struct for the process
2681 * group controlling the tty.
2684 struct pid
*tty_get_pgrp(struct tty_struct
*tty
)
2686 unsigned long flags
;
2689 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
2690 pgrp
= get_pid(tty
->pgrp
);
2691 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
2695 EXPORT_SYMBOL_GPL(tty_get_pgrp
);
2698 * tiocgpgrp - get process group
2699 * @tty: tty passed by user
2700 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2703 * Obtain the process group of the tty. If there is no process group
2706 * Locking: none. Reference to current->signal->tty is safe.
2709 static int tiocgpgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t __user
*p
)
2714 * (tty == real_tty) is a cheap way of
2715 * testing if the tty is NOT a master pty.
2717 if (tty
== real_tty
&& current
->signal
->tty
!= real_tty
)
2719 pid
= tty_get_pgrp(real_tty
);
2720 ret
= put_user(pid_vnr(pid
), p
);
2726 * tiocspgrp - attempt to set process group
2727 * @tty: tty passed by user
2728 * @real_tty: tty side device matching tty passed by user
2731 * Set the process group of the tty to the session passed. Only
2732 * permitted where the tty session is our session.
2734 * Locking: RCU, ctrl lock
2737 static int tiocspgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t __user
*p
)
2741 int retval
= tty_check_change(real_tty
);
2742 unsigned long flags
;
2748 if (!current
->signal
->tty
||
2749 (current
->signal
->tty
!= real_tty
) ||
2750 (real_tty
->session
!= task_session(current
)))
2752 if (get_user(pgrp_nr
, p
))
2757 pgrp
= find_vpid(pgrp_nr
);
2762 if (session_of_pgrp(pgrp
) != task_session(current
))
2765 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
2766 put_pid(real_tty
->pgrp
);
2767 real_tty
->pgrp
= get_pid(pgrp
);
2768 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
2775 * tiocgsid - get session id
2776 * @tty: tty passed by user
2777 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2778 * @p: pointer to returned session id
2780 * Obtain the session id of the tty. If there is no session
2783 * Locking: none. Reference to current->signal->tty is safe.
2786 static int tiocgsid(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t __user
*p
)
2789 * (tty == real_tty) is a cheap way of
2790 * testing if the tty is NOT a master pty.
2792 if (tty
== real_tty
&& current
->signal
->tty
!= real_tty
)
2794 if (!real_tty
->session
)
2796 return put_user(pid_vnr(real_tty
->session
), p
);
2800 * tiocsetd - set line discipline
2802 * @p: pointer to user data
2804 * Set the line discipline according to user request.
2806 * Locking: see tty_set_ldisc, this function is just a helper
2809 static int tiocsetd(struct tty_struct
*tty
, int __user
*p
)
2814 if (get_user(ldisc
, p
))
2818 ret
= tty_set_ldisc(tty
, ldisc
);
2825 * send_break - performed time break
2826 * @tty: device to break on
2827 * @duration: timeout in mS
2829 * Perform a timed break on hardware that lacks its own driver level
2830 * timed break functionality.
2833 * atomic_write_lock serializes
2837 static int send_break(struct tty_struct
*tty
, unsigned int duration
)
2841 if (tty
->ops
->break_ctl
== NULL
)
2844 if (tty
->driver
->flags
& TTY_DRIVER_HARDWARE_BREAK
)
2845 retval
= tty
->ops
->break_ctl(tty
, duration
);
2847 /* Do the work ourselves */
2848 if (tty_write_lock(tty
, 0) < 0)
2850 retval
= tty
->ops
->break_ctl(tty
, -1);
2853 if (!signal_pending(current
))
2854 msleep_interruptible(duration
);
2855 retval
= tty
->ops
->break_ctl(tty
, 0);
2857 tty_write_unlock(tty
);
2858 if (signal_pending(current
))
2865 * tty_tiocmget - get modem status
2867 * @file: user file pointer
2868 * @p: pointer to result
2870 * Obtain the modem status bits from the tty driver if the feature
2871 * is supported. Return -EINVAL if it is not available.
2873 * Locking: none (up to the driver)
2876 static int tty_tiocmget(struct tty_struct
*tty
, struct file
*file
, int __user
*p
)
2878 int retval
= -EINVAL
;
2880 if (tty
->ops
->tiocmget
) {
2881 retval
= tty
->ops
->tiocmget(tty
, file
);
2884 retval
= put_user(retval
, p
);
2890 * tty_tiocmset - set modem status
2892 * @file: user file pointer
2893 * @cmd: command - clear bits, set bits or set all
2894 * @p: pointer to desired bits
2896 * Set the modem status bits from the tty driver if the feature
2897 * is supported. Return -EINVAL if it is not available.
2899 * Locking: none (up to the driver)
2902 static int tty_tiocmset(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
,
2906 unsigned int set
, clear
, val
;
2908 if (tty
->ops
->tiocmset
== NULL
)
2911 retval
= get_user(val
, p
);
2927 set
&= TIOCM_DTR
|TIOCM_RTS
|TIOCM_OUT1
|TIOCM_OUT2
|TIOCM_LOOP
;
2928 clear
&= TIOCM_DTR
|TIOCM_RTS
|TIOCM_OUT1
|TIOCM_OUT2
|TIOCM_LOOP
;
2929 return tty
->ops
->tiocmset(tty
, file
, set
, clear
);
2933 * Split this up, as gcc can choke on it otherwise..
2935 long tty_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2937 struct tty_struct
*tty
, *real_tty
;
2938 void __user
*p
= (void __user
*)arg
;
2940 struct tty_ldisc
*ld
;
2941 struct inode
*inode
= file
->f_dentry
->d_inode
;
2943 tty
= (struct tty_struct
*)file
->private_data
;
2944 if (tty_paranoia_check(tty
, inode
, "tty_ioctl"))
2948 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
2949 tty
->driver
->subtype
== PTY_TYPE_MASTER
)
2950 real_tty
= tty
->link
;
2954 * Factor out some common prep work
2962 retval
= tty_check_change(tty
);
2965 if (cmd
!= TIOCCBRK
) {
2966 tty_wait_until_sent(tty
, 0);
2967 if (signal_pending(current
))
2978 return tiocsti(tty
, p
);
2980 return tiocgwinsz(tty
, p
);
2982 return tiocswinsz(tty
, real_tty
, p
);
2984 return real_tty
!= tty
? -EINVAL
: tioccons(file
);
2986 return fionbio(file
, p
);
2988 set_bit(TTY_EXCLUSIVE
, &tty
->flags
);
2991 clear_bit(TTY_EXCLUSIVE
, &tty
->flags
);
2994 if (current
->signal
->tty
!= tty
)
2999 return tiocsctty(tty
, arg
);
3001 return tiocgpgrp(tty
, real_tty
, p
);
3003 return tiocspgrp(tty
, real_tty
, p
);
3005 return tiocgsid(tty
, real_tty
, p
);
3007 return put_user(tty
->ldisc
.ops
->num
, (int __user
*)p
);
3009 return tiocsetd(tty
, p
);
3012 return tioclinux(tty
, arg
);
3017 case TIOCSBRK
: /* Turn break on, unconditionally */
3018 if (tty
->ops
->break_ctl
)
3019 return tty
->ops
->break_ctl(tty
, -1);
3021 case TIOCCBRK
: /* Turn break off, unconditionally */
3022 if (tty
->ops
->break_ctl
)
3023 return tty
->ops
->break_ctl(tty
, 0);
3025 case TCSBRK
: /* SVID version: non-zero arg --> no break */
3026 /* non-zero arg means wait for all output data
3027 * to be sent (performed above) but don't send break.
3028 * This is used by the tcdrain() termios function.
3031 return send_break(tty
, 250);
3033 case TCSBRKP
: /* support for POSIX tcsendbreak() */
3034 return send_break(tty
, arg
? arg
*100 : 250);
3037 return tty_tiocmget(tty
, file
, p
);
3041 return tty_tiocmset(tty
, file
, cmd
, p
);
3046 /* flush tty buffer and allow ldisc to process ioctl */
3047 tty_buffer_flush(tty
);
3052 if (tty
->ops
->ioctl
) {
3053 retval
= (tty
->ops
->ioctl
)(tty
, file
, cmd
, arg
);
3054 if (retval
!= -ENOIOCTLCMD
)
3057 ld
= tty_ldisc_ref_wait(tty
);
3059 if (ld
->ops
->ioctl
) {
3060 retval
= ld
->ops
->ioctl(tty
, file
, cmd
, arg
);
3061 if (retval
== -ENOIOCTLCMD
)
3064 tty_ldisc_deref(ld
);
3068 #ifdef CONFIG_COMPAT
3069 static long tty_compat_ioctl(struct file
*file
, unsigned int cmd
,
3072 struct inode
*inode
= file
->f_dentry
->d_inode
;
3073 struct tty_struct
*tty
= file
->private_data
;
3074 struct tty_ldisc
*ld
;
3075 int retval
= -ENOIOCTLCMD
;
3077 if (tty_paranoia_check(tty
, inode
, "tty_ioctl"))
3080 if (tty
->ops
->compat_ioctl
) {
3081 retval
= (tty
->ops
->compat_ioctl
)(tty
, file
, cmd
, arg
);
3082 if (retval
!= -ENOIOCTLCMD
)
3086 ld
= tty_ldisc_ref_wait(tty
);
3087 if (ld
->ops
->compat_ioctl
)
3088 retval
= ld
->ops
->compat_ioctl(tty
, file
, cmd
, arg
);
3089 tty_ldisc_deref(ld
);
3096 * This implements the "Secure Attention Key" --- the idea is to
3097 * prevent trojan horses by killing all processes associated with this
3098 * tty when the user hits the "Secure Attention Key". Required for
3099 * super-paranoid applications --- see the Orange Book for more details.
3101 * This code could be nicer; ideally it should send a HUP, wait a few
3102 * seconds, then send a INT, and then a KILL signal. But you then
3103 * have to coordinate with the init process, since all processes associated
3104 * with the current tty must be dead before the new getty is allowed
3107 * Now, if it would be correct ;-/ The current code has a nasty hole -
3108 * it doesn't catch files in flight. We may send the descriptor to ourselves
3109 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
3111 * Nasty bug: do_SAK is being called in interrupt context. This can
3112 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3114 void __do_SAK(struct tty_struct
*tty
)
3119 struct task_struct
*g
, *p
;
3120 struct pid
*session
;
3123 struct fdtable
*fdt
;
3127 session
= tty
->session
;
3129 tty_ldisc_flush(tty
);
3131 tty_driver_flush_buffer(tty
);
3133 read_lock(&tasklist_lock
);
3134 /* Kill the entire session */
3135 do_each_pid_task(session
, PIDTYPE_SID
, p
) {
3136 printk(KERN_NOTICE
"SAK: killed process %d"
3137 " (%s): task_session_nr(p)==tty->session\n",
3138 task_pid_nr(p
), p
->comm
);
3139 send_sig(SIGKILL
, p
, 1);
3140 } while_each_pid_task(session
, PIDTYPE_SID
, p
);
3141 /* Now kill any processes that happen to have the
3144 do_each_thread(g
, p
) {
3145 if (p
->signal
->tty
== tty
) {
3146 printk(KERN_NOTICE
"SAK: killed process %d"
3147 " (%s): task_session_nr(p)==tty->session\n",
3148 task_pid_nr(p
), p
->comm
);
3149 send_sig(SIGKILL
, p
, 1);
3155 * We don't take a ref to the file, so we must
3156 * hold ->file_lock instead.
3158 spin_lock(&p
->files
->file_lock
);
3159 fdt
= files_fdtable(p
->files
);
3160 for (i
= 0; i
< fdt
->max_fds
; i
++) {
3161 filp
= fcheck_files(p
->files
, i
);
3164 if (filp
->f_op
->read
== tty_read
&&
3165 filp
->private_data
== tty
) {
3166 printk(KERN_NOTICE
"SAK: killed process %d"
3167 " (%s): fd#%d opened to the tty\n",
3168 task_pid_nr(p
), p
->comm
, i
);
3169 force_sig(SIGKILL
, p
);
3173 spin_unlock(&p
->files
->file_lock
);
3176 } while_each_thread(g
, p
);
3177 read_unlock(&tasklist_lock
);
3181 static void do_SAK_work(struct work_struct
*work
)
3183 struct tty_struct
*tty
=
3184 container_of(work
, struct tty_struct
, SAK_work
);
3189 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3190 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3191 * the values which we write to it will be identical to the values which it
3192 * already has. --akpm
3194 void do_SAK(struct tty_struct
*tty
)
3198 schedule_work(&tty
->SAK_work
);
3201 EXPORT_SYMBOL(do_SAK
);
3205 * @work: tty structure passed from work queue.
3207 * This routine is called out of the software interrupt to flush data
3208 * from the buffer chain to the line discipline.
3210 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
3211 * while invoking the line discipline receive_buf method. The
3212 * receive_buf method is single threaded for each tty instance.
3215 static void flush_to_ldisc(struct work_struct
*work
)
3217 struct tty_struct
*tty
=
3218 container_of(work
, struct tty_struct
, buf
.work
.work
);
3219 unsigned long flags
;
3220 struct tty_ldisc
*disc
;
3221 struct tty_buffer
*tbuf
, *head
;
3223 unsigned char *flag_buf
;
3225 disc
= tty_ldisc_ref(tty
);
3226 if (disc
== NULL
) /* !TTY_LDISC */
3229 spin_lock_irqsave(&tty
->buf
.lock
, flags
);
3230 /* So we know a flush is running */
3231 set_bit(TTY_FLUSHING
, &tty
->flags
);
3232 head
= tty
->buf
.head
;
3234 tty
->buf
.head
= NULL
;
3236 int count
= head
->commit
- head
->read
;
3238 if (head
->next
== NULL
)
3242 tty_buffer_free(tty
, tbuf
);
3245 /* Ldisc or user is trying to flush the buffers
3246 we are feeding to the ldisc, stop feeding the
3247 line discipline as we want to empty the queue */
3248 if (test_bit(TTY_FLUSHPENDING
, &tty
->flags
))
3250 if (!tty
->receive_room
) {
3251 schedule_delayed_work(&tty
->buf
.work
, 1);
3254 if (count
> tty
->receive_room
)
3255 count
= tty
->receive_room
;
3256 char_buf
= head
->char_buf_ptr
+ head
->read
;
3257 flag_buf
= head
->flag_buf_ptr
+ head
->read
;
3258 head
->read
+= count
;
3259 spin_unlock_irqrestore(&tty
->buf
.lock
, flags
);
3260 disc
->ops
->receive_buf(tty
, char_buf
,
3262 spin_lock_irqsave(&tty
->buf
.lock
, flags
);
3264 /* Restore the queue head */
3265 tty
->buf
.head
= head
;
3267 /* We may have a deferred request to flush the input buffer,
3268 if so pull the chain under the lock and empty the queue */
3269 if (test_bit(TTY_FLUSHPENDING
, &tty
->flags
)) {
3270 __tty_buffer_flush(tty
);
3271 clear_bit(TTY_FLUSHPENDING
, &tty
->flags
);
3272 wake_up(&tty
->read_wait
);
3274 clear_bit(TTY_FLUSHING
, &tty
->flags
);
3275 spin_unlock_irqrestore(&tty
->buf
.lock
, flags
);
3277 tty_ldisc_deref(disc
);
3281 * tty_flip_buffer_push - terminal
3284 * Queue a push of the terminal flip buffers to the line discipline. This
3285 * function must not be called from IRQ context if tty->low_latency is set.
3287 * In the event of the queue being busy for flipping the work will be
3288 * held off and retried later.
3290 * Locking: tty buffer lock. Driver locks in low latency mode.
3293 void tty_flip_buffer_push(struct tty_struct
*tty
)
3295 unsigned long flags
;
3296 spin_lock_irqsave(&tty
->buf
.lock
, flags
);
3297 if (tty
->buf
.tail
!= NULL
)
3298 tty
->buf
.tail
->commit
= tty
->buf
.tail
->used
;
3299 spin_unlock_irqrestore(&tty
->buf
.lock
, flags
);
3301 if (tty
->low_latency
)
3302 flush_to_ldisc(&tty
->buf
.work
.work
);
3304 schedule_delayed_work(&tty
->buf
.work
, 1);
3307 EXPORT_SYMBOL(tty_flip_buffer_push
);
3311 * initialize_tty_struct
3312 * @tty: tty to initialize
3314 * This subroutine initializes a tty structure that has been newly
3317 * Locking: none - tty in question must not be exposed at this point
3320 static void initialize_tty_struct(struct tty_struct
*tty
)
3322 memset(tty
, 0, sizeof(struct tty_struct
));
3323 tty
->magic
= TTY_MAGIC
;
3324 tty_ldisc_init(tty
);
3325 tty
->session
= NULL
;
3327 tty
->overrun_time
= jiffies
;
3328 tty
->buf
.head
= tty
->buf
.tail
= NULL
;
3329 tty_buffer_init(tty
);
3330 INIT_DELAYED_WORK(&tty
->buf
.work
, flush_to_ldisc
);
3331 mutex_init(&tty
->termios_mutex
);
3332 init_waitqueue_head(&tty
->write_wait
);
3333 init_waitqueue_head(&tty
->read_wait
);
3334 INIT_WORK(&tty
->hangup_work
, do_tty_hangup
);
3335 mutex_init(&tty
->atomic_read_lock
);
3336 mutex_init(&tty
->atomic_write_lock
);
3337 spin_lock_init(&tty
->read_lock
);
3338 spin_lock_init(&tty
->ctrl_lock
);
3339 INIT_LIST_HEAD(&tty
->tty_files
);
3340 INIT_WORK(&tty
->SAK_work
, do_SAK_work
);
3344 * tty_put_char - write one character to a tty
3348 * Write one byte to the tty using the provided put_char method
3349 * if present. Returns the number of characters successfully output.
3351 * Note: the specific put_char operation in the driver layer may go
3352 * away soon. Don't call it directly, use this method
3355 int tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
3357 if (tty
->ops
->put_char
)
3358 return tty
->ops
->put_char(tty
, ch
);
3359 return tty
->ops
->write(tty
, &ch
, 1);
3362 EXPORT_SYMBOL_GPL(tty_put_char
);
3364 static struct class *tty_class
;
3367 * tty_register_device - register a tty device
3368 * @driver: the tty driver that describes the tty device
3369 * @index: the index in the tty driver for this tty device
3370 * @device: a struct device that is associated with this tty device.
3371 * This field is optional, if there is no known struct device
3372 * for this tty device it can be set to NULL safely.
3374 * Returns a pointer to the struct device for this tty device
3375 * (or ERR_PTR(-EFOO) on error).
3377 * This call is required to be made to register an individual tty device
3378 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3379 * that bit is not set, this function should not be called by a tty
3385 struct device
*tty_register_device(struct tty_driver
*driver
, unsigned index
,
3386 struct device
*device
)
3389 dev_t dev
= MKDEV(driver
->major
, driver
->minor_start
) + index
;
3391 if (index
>= driver
->num
) {
3392 printk(KERN_ERR
"Attempt to register invalid tty line number "
3394 return ERR_PTR(-EINVAL
);
3397 if (driver
->type
== TTY_DRIVER_TYPE_PTY
)
3398 pty_line_name(driver
, index
, name
);
3400 tty_line_name(driver
, index
, name
);
3402 return device_create_drvdata(tty_class
, device
, dev
, NULL
, name
);
3406 * tty_unregister_device - unregister a tty device
3407 * @driver: the tty driver that describes the tty device
3408 * @index: the index in the tty driver for this tty device
3410 * If a tty device is registered with a call to tty_register_device() then
3411 * this function must be called when the tty device is gone.
3416 void tty_unregister_device(struct tty_driver
*driver
, unsigned index
)
3418 device_destroy(tty_class
,
3419 MKDEV(driver
->major
, driver
->minor_start
) + index
);
3422 EXPORT_SYMBOL(tty_register_device
);
3423 EXPORT_SYMBOL(tty_unregister_device
);
3425 struct tty_driver
*alloc_tty_driver(int lines
)
3427 struct tty_driver
*driver
;
3429 driver
= kzalloc(sizeof(struct tty_driver
), GFP_KERNEL
);
3431 driver
->magic
= TTY_DRIVER_MAGIC
;
3432 driver
->num
= lines
;
3433 /* later we'll move allocation of tables here */
3438 void put_tty_driver(struct tty_driver
*driver
)
3443 void tty_set_operations(struct tty_driver
*driver
,
3444 const struct tty_operations
*op
)
3449 EXPORT_SYMBOL(alloc_tty_driver
);
3450 EXPORT_SYMBOL(put_tty_driver
);
3451 EXPORT_SYMBOL(tty_set_operations
);
3454 * Called by a tty driver to register itself.
3456 int tty_register_driver(struct tty_driver
*driver
)
3463 if (driver
->flags
& TTY_DRIVER_INSTALLED
)
3466 if (!(driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) && driver
->num
) {
3467 p
= kzalloc(driver
->num
* 3 * sizeof(void *), GFP_KERNEL
);
3472 if (!driver
->major
) {
3473 error
= alloc_chrdev_region(&dev
, driver
->minor_start
,
3474 driver
->num
, driver
->name
);
3476 driver
->major
= MAJOR(dev
);
3477 driver
->minor_start
= MINOR(dev
);
3480 dev
= MKDEV(driver
->major
, driver
->minor_start
);
3481 error
= register_chrdev_region(dev
, driver
->num
, driver
->name
);
3489 driver
->ttys
= (struct tty_struct
**)p
;
3490 driver
->termios
= (struct ktermios
**)(p
+ driver
->num
);
3491 driver
->termios_locked
= (struct ktermios
**)
3492 (p
+ driver
->num
* 2);
3494 driver
->ttys
= NULL
;
3495 driver
->termios
= NULL
;
3496 driver
->termios_locked
= NULL
;
3499 cdev_init(&driver
->cdev
, &tty_fops
);
3500 driver
->cdev
.owner
= driver
->owner
;
3501 error
= cdev_add(&driver
->cdev
, dev
, driver
->num
);
3503 unregister_chrdev_region(dev
, driver
->num
);
3504 driver
->ttys
= NULL
;
3505 driver
->termios
= driver
->termios_locked
= NULL
;
3510 mutex_lock(&tty_mutex
);
3511 list_add(&driver
->tty_drivers
, &tty_drivers
);
3512 mutex_unlock(&tty_mutex
);
3514 if (!(driver
->flags
& TTY_DRIVER_DYNAMIC_DEV
)) {
3515 for (i
= 0; i
< driver
->num
; i
++)
3516 tty_register_device(driver
, i
, NULL
);
3518 proc_tty_register_driver(driver
);
3522 EXPORT_SYMBOL(tty_register_driver
);
3525 * Called by a tty driver to unregister itself.
3527 int tty_unregister_driver(struct tty_driver
*driver
)
3530 struct ktermios
*tp
;
3533 if (driver
->refcount
)
3536 unregister_chrdev_region(MKDEV(driver
->major
, driver
->minor_start
),
3538 mutex_lock(&tty_mutex
);
3539 list_del(&driver
->tty_drivers
);
3540 mutex_unlock(&tty_mutex
);
3543 * Free the termios and termios_locked structures because
3544 * we don't want to get memory leaks when modular tty
3545 * drivers are removed from the kernel.
3547 for (i
= 0; i
< driver
->num
; i
++) {
3548 tp
= driver
->termios
[i
];
3550 driver
->termios
[i
] = NULL
;
3553 tp
= driver
->termios_locked
[i
];
3555 driver
->termios_locked
[i
] = NULL
;
3558 if (!(driver
->flags
& TTY_DRIVER_DYNAMIC_DEV
))
3559 tty_unregister_device(driver
, i
);
3562 proc_tty_unregister_driver(driver
);
3563 driver
->ttys
= NULL
;
3564 driver
->termios
= driver
->termios_locked
= NULL
;
3566 cdev_del(&driver
->cdev
);
3569 EXPORT_SYMBOL(tty_unregister_driver
);
3571 dev_t
tty_devnum(struct tty_struct
*tty
)
3573 return MKDEV(tty
->driver
->major
, tty
->driver
->minor_start
) + tty
->index
;
3575 EXPORT_SYMBOL(tty_devnum
);
3577 void proc_clear_tty(struct task_struct
*p
)
3579 spin_lock_irq(&p
->sighand
->siglock
);
3580 p
->signal
->tty
= NULL
;
3581 spin_unlock_irq(&p
->sighand
->siglock
);
3584 /* Called under the sighand lock */
3586 static void __proc_set_tty(struct task_struct
*tsk
, struct tty_struct
*tty
)
3589 unsigned long flags
;
3590 /* We should not have a session or pgrp to put here but.... */
3591 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
3592 put_pid(tty
->session
);
3594 tty
->pgrp
= get_pid(task_pgrp(tsk
));
3595 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
3596 tty
->session
= get_pid(task_session(tsk
));
3598 put_pid(tsk
->signal
->tty_old_pgrp
);
3599 tsk
->signal
->tty
= tty
;
3600 tsk
->signal
->tty_old_pgrp
= NULL
;
3603 static void proc_set_tty(struct task_struct
*tsk
, struct tty_struct
*tty
)
3605 spin_lock_irq(&tsk
->sighand
->siglock
);
3606 __proc_set_tty(tsk
, tty
);
3607 spin_unlock_irq(&tsk
->sighand
->siglock
);
3610 struct tty_struct
*get_current_tty(void)
3612 struct tty_struct
*tty
;
3613 WARN_ON_ONCE(!mutex_is_locked(&tty_mutex
));
3614 tty
= current
->signal
->tty
;
3616 * session->tty can be changed/cleared from under us, make sure we
3617 * issue the load. The obtained pointer, when not NULL, is valid as
3618 * long as we hold tty_mutex.
3623 EXPORT_SYMBOL_GPL(get_current_tty
);
3626 * Initialize the console device. This is called *early*, so
3627 * we can't necessarily depend on lots of kernel help here.
3628 * Just do some early initializations, and do the complex setup
3631 void __init
console_init(void)
3635 /* Setup the default TTY line discipline. */
3639 * set up the console device so that later boot sequences can
3640 * inform about problems etc..
3642 call
= __con_initcall_start
;
3643 while (call
< __con_initcall_end
) {
3649 static int __init
tty_class_init(void)
3651 tty_class
= class_create(THIS_MODULE
, "tty");
3652 if (IS_ERR(tty_class
))
3653 return PTR_ERR(tty_class
);
3657 postcore_initcall(tty_class_init
);
3659 /* 3/2004 jmc: why do these devices exist? */
3661 static struct cdev tty_cdev
, console_cdev
;
3662 #ifdef CONFIG_UNIX98_PTYS
3663 static struct cdev ptmx_cdev
;
3666 static struct cdev vc0_cdev
;
3670 * Ok, now we can initialize the rest of the tty devices and can count
3671 * on memory allocations, interrupts etc..
3673 static int __init
tty_init(void)
3675 cdev_init(&tty_cdev
, &tty_fops
);
3676 if (cdev_add(&tty_cdev
, MKDEV(TTYAUX_MAJOR
, 0), 1) ||
3677 register_chrdev_region(MKDEV(TTYAUX_MAJOR
, 0), 1, "/dev/tty") < 0)
3678 panic("Couldn't register /dev/tty driver\n");
3679 device_create_drvdata(tty_class
, NULL
, MKDEV(TTYAUX_MAJOR
, 0), NULL
,
3682 cdev_init(&console_cdev
, &console_fops
);
3683 if (cdev_add(&console_cdev
, MKDEV(TTYAUX_MAJOR
, 1), 1) ||
3684 register_chrdev_region(MKDEV(TTYAUX_MAJOR
, 1), 1, "/dev/console") < 0)
3685 panic("Couldn't register /dev/console driver\n");
3686 device_create_drvdata(tty_class
, NULL
, MKDEV(TTYAUX_MAJOR
, 1), NULL
,
3689 #ifdef CONFIG_UNIX98_PTYS
3690 cdev_init(&ptmx_cdev
, &ptmx_fops
);
3691 if (cdev_add(&ptmx_cdev
, MKDEV(TTYAUX_MAJOR
, 2), 1) ||
3692 register_chrdev_region(MKDEV(TTYAUX_MAJOR
, 2), 1, "/dev/ptmx") < 0)
3693 panic("Couldn't register /dev/ptmx driver\n");
3694 device_create_drvdata(tty_class
, NULL
, MKDEV(TTYAUX_MAJOR
, 2), NULL
, "ptmx");
3698 cdev_init(&vc0_cdev
, &console_fops
);
3699 if (cdev_add(&vc0_cdev
, MKDEV(TTY_MAJOR
, 0), 1) ||
3700 register_chrdev_region(MKDEV(TTY_MAJOR
, 0), 1, "/dev/vc/0") < 0)
3701 panic("Couldn't register /dev/tty0 driver\n");
3702 device_create_drvdata(tty_class
, NULL
, MKDEV(TTY_MAJOR
, 0), NULL
, "tty0");
3708 module_init(tty_init
);