2 * linux/drivers/char/tty_io.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
9 * or rs-channels. It also implements echoing, cooked mode etc.
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
13 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
14 * tty_struct and tty_queue structures. Previously there was an array
15 * of 256 tty_struct's which was statically allocated, and the
16 * tty_queue structures were allocated at boot time. Both are now
17 * dynamically allocated only when the tty is open.
19 * Also restructured routines so that there is more of a separation
20 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
21 * the low-level tty routines (serial.c, pty.c, console.c). This
22 * makes for cleaner and more compact code. -TYT, 9/17/92
24 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
25 * which can be dynamically activated and de-activated by the line
26 * discipline handling modules (like SLIP).
28 * NOTE: pay no attention to the line discipline code (yet); its
29 * interface is still subject to change in this version...
32 * Added functionality to the OPOST tty handling. No delays, but all
33 * other bits should be there.
34 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
45 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
48 * Move console and virtual terminal code to more appropriate files,
49 * implement CONFIG_VT and generalize console device interface.
50 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
52 * Rewrote init_dev and release_dev to eliminate races.
53 * -- Bill Hawes <whawes@star.net>, June 97
55 * Added devfs support.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
58 * Added support for a Unix98-style ptmx device.
59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
61 * Reduced memory usage for older ARM systems
62 * -- Russell King <rmk@arm.linux.org.uk>
64 * Move do_SAK() into process context. Less stack use in devfs functions.
65 * alloc_tty_struct() always uses kmalloc()
66 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
69 #include <linux/types.h>
70 #include <linux/major.h>
71 #include <linux/errno.h>
72 #include <linux/signal.h>
73 #include <linux/fcntl.h>
74 #include <linux/sched.h>
75 #include <linux/interrupt.h>
76 #include <linux/tty.h>
77 #include <linux/tty_driver.h>
78 #include <linux/tty_flip.h>
79 #include <linux/devpts_fs.h>
80 #include <linux/file.h>
81 #include <linux/fdtable.h>
82 #include <linux/console.h>
83 #include <linux/timer.h>
84 #include <linux/ctype.h>
87 #include <linux/string.h>
88 #include <linux/slab.h>
89 #include <linux/poll.h>
90 #include <linux/proc_fs.h>
91 #include <linux/init.h>
92 #include <linux/module.h>
93 #include <linux/smp_lock.h>
94 #include <linux/device.h>
95 #include <linux/wait.h>
96 #include <linux/bitops.h>
97 #include <linux/delay.h>
98 #include <linux/seq_file.h>
100 #include <linux/uaccess.h>
101 #include <asm/system.h>
103 #include <linux/kbd_kern.h>
104 #include <linux/vt_kern.h>
105 #include <linux/selection.h>
107 #include <linux/kmod.h>
108 #include <linux/nsproxy.h>
110 #undef TTY_DEBUG_HANGUP
112 #define TTY_PARANOIA_CHECK 1
113 #define CHECK_TTY_COUNT 1
115 struct ktermios tty_std_termios
= { /* for the benefit of tty drivers */
116 .c_iflag
= ICRNL
| IXON
,
117 .c_oflag
= OPOST
| ONLCR
,
118 .c_cflag
= B38400
| CS8
| CREAD
| HUPCL
,
119 .c_lflag
= ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
|
120 ECHOCTL
| ECHOKE
| IEXTEN
,
126 EXPORT_SYMBOL(tty_std_termios
);
128 /* This list gets poked at by procfs and various bits of boot up code. This
129 could do with some rationalisation such as pulling the tty proc function
132 LIST_HEAD(tty_drivers
); /* linked list of tty drivers */
134 /* Mutex to protect creating and releasing a tty. This is shared with
135 vt.c for deeply disgusting hack reasons */
136 DEFINE_MUTEX(tty_mutex
);
137 EXPORT_SYMBOL(tty_mutex
);
139 #ifdef CONFIG_UNIX98_PTYS
140 extern struct tty_driver
*ptm_driver
; /* Unix98 pty masters; for /dev/ptmx */
141 static int ptmx_open(struct inode
*, struct file
*);
144 static void initialize_tty_struct(struct tty_struct
*tty
);
146 static ssize_t
tty_read(struct file
*, char __user
*, size_t, loff_t
*);
147 static ssize_t
tty_write(struct file
*, const char __user
*, size_t, loff_t
*);
148 ssize_t
redirected_tty_write(struct file
*, const char __user
*,
150 static unsigned int tty_poll(struct file
*, poll_table
*);
151 static int tty_open(struct inode
*, struct file
*);
152 static int tty_release(struct inode
*, struct file
*);
153 long tty_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
155 static long tty_compat_ioctl(struct file
*file
, unsigned int cmd
,
158 #define tty_compat_ioctl NULL
160 static int tty_fasync(int fd
, struct file
*filp
, int on
);
161 static void release_tty(struct tty_struct
*tty
, int idx
);
162 static void __proc_set_tty(struct task_struct
*tsk
, struct tty_struct
*tty
);
163 static void proc_set_tty(struct task_struct
*tsk
, struct tty_struct
*tty
);
166 * alloc_tty_struct - allocate a tty object
168 * Return a new empty tty structure. The data fields have not
169 * been initialized in any way but has been zeroed
174 static struct tty_struct
*alloc_tty_struct(void)
176 return kzalloc(sizeof(struct tty_struct
), GFP_KERNEL
);
180 * free_tty_struct - free a disused tty
181 * @tty: tty struct to free
183 * Free the write buffers, tty queue and tty memory itself.
185 * Locking: none. Must be called after tty is definitely unused
188 static inline void free_tty_struct(struct tty_struct
*tty
)
190 kfree(tty
->write_buf
);
191 tty_buffer_free_all(tty
);
195 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
198 * tty_name - return tty naming
199 * @tty: tty structure
200 * @buf: buffer for output
202 * Convert a tty structure into a name. The name reflects the kernel
203 * naming policy and if udev is in use may not reflect user space
208 char *tty_name(struct tty_struct
*tty
, char *buf
)
210 if (!tty
) /* Hmm. NULL pointer. That's fun. */
211 strcpy(buf
, "NULL tty");
213 strcpy(buf
, tty
->name
);
217 EXPORT_SYMBOL(tty_name
);
219 int tty_paranoia_check(struct tty_struct
*tty
, struct inode
*inode
,
222 #ifdef TTY_PARANOIA_CHECK
225 "null TTY for (%d:%d) in %s\n",
226 imajor(inode
), iminor(inode
), routine
);
229 if (tty
->magic
!= TTY_MAGIC
) {
231 "bad magic number for tty struct (%d:%d) in %s\n",
232 imajor(inode
), iminor(inode
), routine
);
239 static int check_tty_count(struct tty_struct
*tty
, const char *routine
)
241 #ifdef CHECK_TTY_COUNT
246 list_for_each(p
, &tty
->tty_files
) {
250 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
251 tty
->driver
->subtype
== PTY_TYPE_SLAVE
&&
252 tty
->link
&& tty
->link
->count
)
254 if (tty
->count
!= count
) {
255 printk(KERN_WARNING
"Warning: dev (%s) tty->count(%d) "
256 "!= #fd's(%d) in %s\n",
257 tty
->name
, tty
->count
, count
, routine
);
265 * get_tty_driver - find device of a tty
266 * @dev_t: device identifier
267 * @index: returns the index of the tty
269 * This routine returns a tty driver structure, given a device number
270 * and also passes back the index number.
272 * Locking: caller must hold tty_mutex
275 static struct tty_driver
*get_tty_driver(dev_t device
, int *index
)
277 struct tty_driver
*p
;
279 list_for_each_entry(p
, &tty_drivers
, tty_drivers
) {
280 dev_t base
= MKDEV(p
->major
, p
->minor_start
);
281 if (device
< base
|| device
>= base
+ p
->num
)
283 *index
= device
- base
;
289 #ifdef CONFIG_CONSOLE_POLL
292 * tty_find_polling_driver - find device of a polled tty
293 * @name: name string to match
294 * @line: pointer to resulting tty line nr
296 * This routine returns a tty driver structure, given a name
297 * and the condition that the tty driver is capable of polled
300 struct tty_driver
*tty_find_polling_driver(char *name
, int *line
)
302 struct tty_driver
*p
, *res
= NULL
;
307 for (str
= name
; *str
; str
++)
308 if ((*str
>= '0' && *str
<= '9') || *str
== ',')
314 tty_line
= simple_strtoul(str
, &str
, 10);
316 mutex_lock(&tty_mutex
);
317 /* Search through the tty devices to look for a match */
318 list_for_each_entry(p
, &tty_drivers
, tty_drivers
) {
319 if (strncmp(name
, p
->name
, len
) != 0)
326 if (tty_line
>= 0 && tty_line
<= p
->num
&& p
->ops
&&
327 p
->ops
->poll_init
&& !p
->ops
->poll_init(p
, tty_line
, str
)) {
333 mutex_unlock(&tty_mutex
);
337 EXPORT_SYMBOL_GPL(tty_find_polling_driver
);
341 * tty_check_change - check for POSIX terminal changes
344 * If we try to write to, or set the state of, a terminal and we're
345 * not in the foreground, send a SIGTTOU. If the signal is blocked or
346 * ignored, go ahead and perform the operation. (POSIX 7.2)
351 int tty_check_change(struct tty_struct
*tty
)
356 if (current
->signal
->tty
!= tty
)
359 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
362 printk(KERN_WARNING
"tty_check_change: tty->pgrp == NULL!\n");
365 if (task_pgrp(current
) == tty
->pgrp
)
367 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
368 if (is_ignored(SIGTTOU
))
370 if (is_current_pgrp_orphaned()) {
374 kill_pgrp(task_pgrp(current
), SIGTTOU
, 1);
375 set_thread_flag(TIF_SIGPENDING
);
380 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
384 EXPORT_SYMBOL(tty_check_change
);
386 static ssize_t
hung_up_tty_read(struct file
*file
, char __user
*buf
,
387 size_t count
, loff_t
*ppos
)
392 static ssize_t
hung_up_tty_write(struct file
*file
, const char __user
*buf
,
393 size_t count
, loff_t
*ppos
)
398 /* No kernel lock held - none needed ;) */
399 static unsigned int hung_up_tty_poll(struct file
*filp
, poll_table
*wait
)
401 return POLLIN
| POLLOUT
| POLLERR
| POLLHUP
| POLLRDNORM
| POLLWRNORM
;
404 static long hung_up_tty_ioctl(struct file
*file
, unsigned int cmd
,
407 return cmd
== TIOCSPGRP
? -ENOTTY
: -EIO
;
410 static long hung_up_tty_compat_ioctl(struct file
*file
,
411 unsigned int cmd
, unsigned long arg
)
413 return cmd
== TIOCSPGRP
? -ENOTTY
: -EIO
;
416 static const struct file_operations tty_fops
= {
421 .unlocked_ioctl
= tty_ioctl
,
422 .compat_ioctl
= tty_compat_ioctl
,
424 .release
= tty_release
,
425 .fasync
= tty_fasync
,
428 #ifdef CONFIG_UNIX98_PTYS
429 static const struct file_operations ptmx_fops
= {
434 .unlocked_ioctl
= tty_ioctl
,
435 .compat_ioctl
= tty_compat_ioctl
,
437 .release
= tty_release
,
438 .fasync
= tty_fasync
,
442 static const struct file_operations console_fops
= {
445 .write
= redirected_tty_write
,
447 .unlocked_ioctl
= tty_ioctl
,
448 .compat_ioctl
= tty_compat_ioctl
,
450 .release
= tty_release
,
451 .fasync
= tty_fasync
,
454 static const struct file_operations hung_up_tty_fops
= {
456 .read
= hung_up_tty_read
,
457 .write
= hung_up_tty_write
,
458 .poll
= hung_up_tty_poll
,
459 .unlocked_ioctl
= hung_up_tty_ioctl
,
460 .compat_ioctl
= hung_up_tty_compat_ioctl
,
461 .release
= tty_release
,
464 static DEFINE_SPINLOCK(redirect_lock
);
465 static struct file
*redirect
;
468 * tty_wakeup - request more data
471 * Internal and external helper for wakeups of tty. This function
472 * informs the line discipline if present that the driver is ready
473 * to receive more output data.
476 void tty_wakeup(struct tty_struct
*tty
)
478 struct tty_ldisc
*ld
;
480 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) {
481 ld
= tty_ldisc_ref(tty
);
483 if (ld
->ops
->write_wakeup
)
484 ld
->ops
->write_wakeup(tty
);
488 wake_up_interruptible(&tty
->write_wait
);
491 EXPORT_SYMBOL_GPL(tty_wakeup
);
494 * tty_ldisc_flush - flush line discipline queue
497 * Flush the line discipline queue (if any) for this tty. If there
498 * is no line discipline active this is a no-op.
501 void tty_ldisc_flush(struct tty_struct
*tty
)
503 struct tty_ldisc
*ld
= tty_ldisc_ref(tty
);
505 if (ld
->ops
->flush_buffer
)
506 ld
->ops
->flush_buffer(tty
);
509 tty_buffer_flush(tty
);
512 EXPORT_SYMBOL_GPL(tty_ldisc_flush
);
515 * tty_reset_termios - reset terminal state
518 * Restore a terminal to the driver default state
521 static void tty_reset_termios(struct tty_struct
*tty
)
523 mutex_lock(&tty
->termios_mutex
);
524 *tty
->termios
= tty
->driver
->init_termios
;
525 tty
->termios
->c_ispeed
= tty_termios_input_baud_rate(tty
->termios
);
526 tty
->termios
->c_ospeed
= tty_termios_baud_rate(tty
->termios
);
527 mutex_unlock(&tty
->termios_mutex
);
531 * do_tty_hangup - actual handler for hangup events
534 * This can be called by the "eventd" kernel thread. That is process
535 * synchronous but doesn't hold any locks, so we need to make sure we
536 * have the appropriate locks for what we're doing.
538 * The hangup event clears any pending redirections onto the hung up
539 * device. It ensures future writes will error and it does the needed
540 * line discipline hangup and signal delivery. The tty object itself
545 * redirect lock for undoing redirection
546 * file list lock for manipulating list of ttys
547 * tty_ldisc_lock from called functions
548 * termios_mutex resetting termios data
549 * tasklist_lock to walk task list for hangup event
550 * ->siglock to protect ->signal/->sighand
552 static void do_tty_hangup(struct work_struct
*work
)
554 struct tty_struct
*tty
=
555 container_of(work
, struct tty_struct
, hangup_work
);
556 struct file
*cons_filp
= NULL
;
557 struct file
*filp
, *f
= NULL
;
558 struct task_struct
*p
;
559 struct tty_ldisc
*ld
;
560 int closecount
= 0, n
;
567 /* inuse_filps is protected by the single kernel lock */
570 spin_lock(&redirect_lock
);
571 if (redirect
&& redirect
->private_data
== tty
) {
575 spin_unlock(&redirect_lock
);
577 check_tty_count(tty
, "do_tty_hangup");
579 /* This breaks for file handles being sent over AF_UNIX sockets ? */
580 list_for_each_entry(filp
, &tty
->tty_files
, f_u
.fu_list
) {
581 if (filp
->f_op
->write
== redirected_tty_write
)
583 if (filp
->f_op
->write
!= tty_write
)
586 tty_fasync(-1, filp
, 0); /* can't block */
587 filp
->f_op
= &hung_up_tty_fops
;
591 * FIXME! What are the locking issues here? This may me overdoing
592 * things... This question is especially important now that we've
593 * removed the irqlock.
595 ld
= tty_ldisc_ref(tty
);
597 /* We may have no line discipline at this point */
598 if (ld
->ops
->flush_buffer
)
599 ld
->ops
->flush_buffer(tty
);
600 tty_driver_flush_buffer(tty
);
601 if ((test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) &&
602 ld
->ops
->write_wakeup
)
603 ld
->ops
->write_wakeup(tty
);
605 ld
->ops
->hangup(tty
);
608 * FIXME: Once we trust the LDISC code better we can wait here for
609 * ldisc completion and fix the driver call race
611 wake_up_interruptible(&tty
->write_wait
);
612 wake_up_interruptible(&tty
->read_wait
);
614 * Shutdown the current line discipline, and reset it to
617 if (tty
->driver
->flags
& TTY_DRIVER_RESET_TERMIOS
)
618 tty_reset_termios(tty
);
619 /* Defer ldisc switch */
620 /* tty_deferred_ldisc_switch(N_TTY);
622 This should get done automatically when the port closes and
623 tty_release is called */
625 read_lock(&tasklist_lock
);
627 do_each_pid_task(tty
->session
, PIDTYPE_SID
, p
) {
628 spin_lock_irq(&p
->sighand
->siglock
);
629 if (p
->signal
->tty
== tty
) {
630 p
->signal
->tty
= NULL
;
631 /* We defer the dereferences outside fo
635 if (!p
->signal
->leader
) {
636 spin_unlock_irq(&p
->sighand
->siglock
);
639 __group_send_sig_info(SIGHUP
, SEND_SIG_PRIV
, p
);
640 __group_send_sig_info(SIGCONT
, SEND_SIG_PRIV
, p
);
641 put_pid(p
->signal
->tty_old_pgrp
); /* A noop */
642 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
644 p
->signal
->tty_old_pgrp
= get_pid(tty
->pgrp
);
645 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
646 spin_unlock_irq(&p
->sighand
->siglock
);
647 } while_each_pid_task(tty
->session
, PIDTYPE_SID
, p
);
649 read_unlock(&tasklist_lock
);
651 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
653 put_pid(tty
->session
);
657 tty
->ctrl_status
= 0;
658 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
660 /* Account for the p->signal references we killed */
665 * If one of the devices matches a console pointer, we
666 * cannot just call hangup() because that will cause
667 * tty->count and state->count to go out of sync.
668 * So we just call close() the right number of times.
672 for (n
= 0; n
< closecount
; n
++)
673 tty
->ops
->close(tty
, cons_filp
);
674 } else if (tty
->ops
->hangup
)
675 (tty
->ops
->hangup
)(tty
);
677 * We don't want to have driver/ldisc interactions beyond
678 * the ones we did here. The driver layer expects no
679 * calls after ->hangup() from the ldisc side. However we
680 * can't yet guarantee all that.
682 set_bit(TTY_HUPPED
, &tty
->flags
);
684 tty_ldisc_enable(tty
);
693 * tty_hangup - trigger a hangup event
694 * @tty: tty to hangup
696 * A carrier loss (virtual or otherwise) has occurred on this like
697 * schedule a hangup sequence to run after this event.
700 void tty_hangup(struct tty_struct
*tty
)
702 #ifdef TTY_DEBUG_HANGUP
704 printk(KERN_DEBUG
"%s hangup...\n", tty_name(tty
, buf
));
706 schedule_work(&tty
->hangup_work
);
709 EXPORT_SYMBOL(tty_hangup
);
712 * tty_vhangup - process vhangup
713 * @tty: tty to hangup
715 * The user has asked via system call for the terminal to be hung up.
716 * We do this synchronously so that when the syscall returns the process
717 * is complete. That guarantee is necessary for security reasons.
720 void tty_vhangup(struct tty_struct
*tty
)
722 #ifdef TTY_DEBUG_HANGUP
725 printk(KERN_DEBUG
"%s vhangup...\n", tty_name(tty
, buf
));
727 do_tty_hangup(&tty
->hangup_work
);
730 EXPORT_SYMBOL(tty_vhangup
);
733 * tty_vhangup_self - process vhangup for own ctty
735 * Perform a vhangup on the current controlling tty
738 void tty_vhangup_self(void)
740 struct tty_struct
*tty
;
742 tty
= get_current_tty();
750 * tty_hung_up_p - was tty hung up
751 * @filp: file pointer of tty
753 * Return true if the tty has been subject to a vhangup or a carrier
757 int tty_hung_up_p(struct file
*filp
)
759 return (filp
->f_op
== &hung_up_tty_fops
);
762 EXPORT_SYMBOL(tty_hung_up_p
);
764 static void session_clear_tty(struct pid
*session
)
766 struct task_struct
*p
;
767 do_each_pid_task(session
, PIDTYPE_SID
, p
) {
769 } while_each_pid_task(session
, PIDTYPE_SID
, p
);
773 * disassociate_ctty - disconnect controlling tty
774 * @on_exit: true if exiting so need to "hang up" the session
776 * This function is typically called only by the session leader, when
777 * it wants to disassociate itself from its controlling tty.
779 * It performs the following functions:
780 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
781 * (2) Clears the tty from being controlling the session
782 * (3) Clears the controlling tty for all processes in the
785 * The argument on_exit is set to 1 if called when a process is
786 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
789 * BKL is taken for hysterical raisins
790 * tty_mutex is taken to protect tty
791 * ->siglock is taken to protect ->signal/->sighand
792 * tasklist_lock is taken to walk process list for sessions
793 * ->siglock is taken to protect ->signal/->sighand
796 void disassociate_ctty(int on_exit
)
798 struct tty_struct
*tty
;
799 struct pid
*tty_pgrp
= NULL
;
802 tty
= get_current_tty();
804 tty_pgrp
= get_pid(tty
->pgrp
);
806 if (on_exit
&& tty
->driver
->type
!= TTY_DRIVER_TYPE_PTY
)
810 } else if (on_exit
) {
811 struct pid
*old_pgrp
;
812 spin_lock_irq(¤t
->sighand
->siglock
);
813 old_pgrp
= current
->signal
->tty_old_pgrp
;
814 current
->signal
->tty_old_pgrp
= NULL
;
815 spin_unlock_irq(¤t
->sighand
->siglock
);
817 kill_pgrp(old_pgrp
, SIGHUP
, on_exit
);
818 kill_pgrp(old_pgrp
, SIGCONT
, on_exit
);
824 kill_pgrp(tty_pgrp
, SIGHUP
, on_exit
);
826 kill_pgrp(tty_pgrp
, SIGCONT
, on_exit
);
830 spin_lock_irq(¤t
->sighand
->siglock
);
831 put_pid(current
->signal
->tty_old_pgrp
);
832 current
->signal
->tty_old_pgrp
= NULL
;
833 spin_unlock_irq(¤t
->sighand
->siglock
);
835 tty
= get_current_tty();
838 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
839 put_pid(tty
->session
);
843 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
846 #ifdef TTY_DEBUG_HANGUP
847 printk(KERN_DEBUG
"error attempted to write to tty [0x%p]"
852 /* Now clear signal->tty under the lock */
853 read_lock(&tasklist_lock
);
854 session_clear_tty(task_session(current
));
855 read_unlock(&tasklist_lock
);
860 * no_tty - Ensure the current process does not have a controlling tty
864 struct task_struct
*tsk
= current
;
866 if (tsk
->signal
->leader
)
867 disassociate_ctty(0);
874 * stop_tty - propagate flow control
877 * Perform flow control to the driver. For PTY/TTY pairs we
878 * must also propagate the TIOCKPKT status. May be called
879 * on an already stopped device and will not re-call the driver
882 * This functionality is used by both the line disciplines for
883 * halting incoming flow and by the driver. It may therefore be
884 * called from any context, may be under the tty atomic_write_lock
888 * Uses the tty control lock internally
891 void stop_tty(struct tty_struct
*tty
)
894 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
896 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
900 if (tty
->link
&& tty
->link
->packet
) {
901 tty
->ctrl_status
&= ~TIOCPKT_START
;
902 tty
->ctrl_status
|= TIOCPKT_STOP
;
903 wake_up_interruptible(&tty
->link
->read_wait
);
905 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
907 (tty
->ops
->stop
)(tty
);
910 EXPORT_SYMBOL(stop_tty
);
913 * start_tty - propagate flow control
916 * Start a tty that has been stopped if at all possible. Perform
917 * any necessary wakeups and propagate the TIOCPKT status. If this
918 * is the tty was previous stopped and is being started then the
919 * driver start method is invoked and the line discipline woken.
925 void start_tty(struct tty_struct
*tty
)
928 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
929 if (!tty
->stopped
|| tty
->flow_stopped
) {
930 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
934 if (tty
->link
&& tty
->link
->packet
) {
935 tty
->ctrl_status
&= ~TIOCPKT_STOP
;
936 tty
->ctrl_status
|= TIOCPKT_START
;
937 wake_up_interruptible(&tty
->link
->read_wait
);
939 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
941 (tty
->ops
->start
)(tty
);
942 /* If we have a running line discipline it may need kicking */
946 EXPORT_SYMBOL(start_tty
);
949 * tty_read - read method for tty device files
950 * @file: pointer to tty file
952 * @count: size of user buffer
955 * Perform the read system call function on this terminal device. Checks
956 * for hung up devices before calling the line discipline method.
959 * Locks the line discipline internally while needed. Multiple
960 * read calls may be outstanding in parallel.
963 static ssize_t
tty_read(struct file
*file
, char __user
*buf
, size_t count
,
967 struct tty_struct
*tty
;
969 struct tty_ldisc
*ld
;
971 tty
= (struct tty_struct
*)file
->private_data
;
972 inode
= file
->f_path
.dentry
->d_inode
;
973 if (tty_paranoia_check(tty
, inode
, "tty_read"))
975 if (!tty
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
978 /* We want to wait for the line discipline to sort out in this
980 ld
= tty_ldisc_ref_wait(tty
);
982 i
= (ld
->ops
->read
)(tty
, file
, buf
, count
);
987 inode
->i_atime
= current_fs_time(inode
->i_sb
);
991 void tty_write_unlock(struct tty_struct
*tty
)
993 mutex_unlock(&tty
->atomic_write_lock
);
994 wake_up_interruptible(&tty
->write_wait
);
997 int tty_write_lock(struct tty_struct
*tty
, int ndelay
)
999 if (!mutex_trylock(&tty
->atomic_write_lock
)) {
1002 if (mutex_lock_interruptible(&tty
->atomic_write_lock
))
1003 return -ERESTARTSYS
;
1009 * Split writes up in sane blocksizes to avoid
1010 * denial-of-service type attacks
1012 static inline ssize_t
do_tty_write(
1013 ssize_t (*write
)(struct tty_struct
*, struct file
*, const unsigned char *, size_t),
1014 struct tty_struct
*tty
,
1016 const char __user
*buf
,
1019 ssize_t ret
, written
= 0;
1022 ret
= tty_write_lock(tty
, file
->f_flags
& O_NDELAY
);
1027 * We chunk up writes into a temporary buffer. This
1028 * simplifies low-level drivers immensely, since they
1029 * don't have locking issues and user mode accesses.
1031 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1034 * The default chunk-size is 2kB, because the NTTY
1035 * layer has problems with bigger chunks. It will
1036 * claim to be able to handle more characters than
1039 * FIXME: This can probably go away now except that 64K chunks
1040 * are too likely to fail unless switched to vmalloc...
1043 if (test_bit(TTY_NO_WRITE_SPLIT
, &tty
->flags
))
1048 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1049 if (tty
->write_cnt
< chunk
) {
1055 buf
= kmalloc(chunk
, GFP_KERNEL
);
1060 kfree(tty
->write_buf
);
1061 tty
->write_cnt
= chunk
;
1062 tty
->write_buf
= buf
;
1065 /* Do the write .. */
1067 size_t size
= count
;
1071 if (copy_from_user(tty
->write_buf
, buf
, size
))
1073 ret
= write(tty
, file
, tty
->write_buf
, size
);
1082 if (signal_pending(current
))
1087 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1088 inode
->i_mtime
= current_fs_time(inode
->i_sb
);
1092 tty_write_unlock(tty
);
1097 * tty_write_message - write a message to a certain tty, not just the console.
1098 * @tty: the destination tty_struct
1099 * @msg: the message to write
1101 * This is used for messages that need to be redirected to a specific tty.
1102 * We don't put it into the syslog queue right now maybe in the future if
1105 * We must still hold the BKL and test the CLOSING flag for the moment.
1108 void tty_write_message(struct tty_struct
*tty
, char *msg
)
1112 mutex_lock(&tty
->atomic_write_lock
);
1113 if (tty
->ops
->write
&& !test_bit(TTY_CLOSING
, &tty
->flags
))
1114 tty
->ops
->write(tty
, msg
, strlen(msg
));
1115 tty_write_unlock(tty
);
1123 * tty_write - write method for tty device file
1124 * @file: tty file pointer
1125 * @buf: user data to write
1126 * @count: bytes to write
1129 * Write data to a tty device via the line discipline.
1132 * Locks the line discipline as required
1133 * Writes to the tty driver are serialized by the atomic_write_lock
1134 * and are then processed in chunks to the device. The line discipline
1135 * write method will not be involked in parallel for each device
1136 * The line discipline write method is called under the big
1137 * kernel lock for historical reasons. New code should not rely on this.
1140 static ssize_t
tty_write(struct file
*file
, const char __user
*buf
,
1141 size_t count
, loff_t
*ppos
)
1143 struct tty_struct
*tty
;
1144 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1146 struct tty_ldisc
*ld
;
1148 tty
= (struct tty_struct
*)file
->private_data
;
1149 if (tty_paranoia_check(tty
, inode
, "tty_write"))
1151 if (!tty
|| !tty
->ops
->write
||
1152 (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
1154 /* Short term debug to catch buggy drivers */
1155 if (tty
->ops
->write_room
== NULL
)
1156 printk(KERN_ERR
"tty driver %s lacks a write_room method.\n",
1158 ld
= tty_ldisc_ref_wait(tty
);
1159 if (!ld
->ops
->write
)
1162 ret
= do_tty_write(ld
->ops
->write
, tty
, file
, buf
, count
);
1163 tty_ldisc_deref(ld
);
1167 ssize_t
redirected_tty_write(struct file
*file
, const char __user
*buf
,
1168 size_t count
, loff_t
*ppos
)
1170 struct file
*p
= NULL
;
1172 spin_lock(&redirect_lock
);
1177 spin_unlock(&redirect_lock
);
1181 res
= vfs_write(p
, buf
, count
, &p
->f_pos
);
1185 return tty_write(file
, buf
, count
, ppos
);
1188 static char ptychar
[] = "pqrstuvwxyzabcde";
1191 * pty_line_name - generate name for a pty
1192 * @driver: the tty driver in use
1193 * @index: the minor number
1194 * @p: output buffer of at least 6 bytes
1196 * Generate a name from a driver reference and write it to the output
1201 static void pty_line_name(struct tty_driver
*driver
, int index
, char *p
)
1203 int i
= index
+ driver
->name_base
;
1204 /* ->name is initialized to "ttyp", but "tty" is expected */
1205 sprintf(p
, "%s%c%x",
1206 driver
->subtype
== PTY_TYPE_SLAVE
? "tty" : driver
->name
,
1207 ptychar
[i
>> 4 & 0xf], i
& 0xf);
1211 * pty_line_name - generate name for a tty
1212 * @driver: the tty driver in use
1213 * @index: the minor number
1214 * @p: output buffer of at least 7 bytes
1216 * Generate a name from a driver reference and write it to the output
1221 static void tty_line_name(struct tty_driver
*driver
, int index
, char *p
)
1223 sprintf(p
, "%s%d", driver
->name
, index
+ driver
->name_base
);
1227 * init_dev - initialise a tty device
1228 * @driver: tty driver we are opening a device on
1229 * @idx: device index
1230 * @tty: returned tty structure
1232 * Prepare a tty device. This may not be a "new" clean device but
1233 * could also be an active device. The pty drivers require special
1234 * handling because of this.
1237 * The function is called under the tty_mutex, which
1238 * protects us from the tty struct or driver itself going away.
1240 * On exit the tty device has the line discipline attached and
1241 * a reference count of 1. If a pair was created for pty/tty use
1242 * and the other was a pty master then it too has a reference count of 1.
1244 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1245 * failed open. The new code protects the open with a mutex, so it's
1246 * really quite straightforward. The mutex locking can probably be
1247 * relaxed for the (most common) case of reopening a tty.
1250 static int init_dev(struct tty_driver
*driver
, int idx
,
1251 struct tty_struct
**ret_tty
)
1253 struct tty_struct
*tty
, *o_tty
;
1254 struct ktermios
*tp
, **tp_loc
, *o_tp
, **o_tp_loc
;
1255 struct ktermios
*ltp
, **ltp_loc
, *o_ltp
, **o_ltp_loc
;
1258 /* check whether we're reopening an existing tty */
1259 if (driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) {
1260 tty
= devpts_get_tty(idx
);
1262 * If we don't have a tty here on a slave open, it's because
1263 * the master already started the close process and there's
1264 * no relation between devpts file and tty anymore.
1266 if (!tty
&& driver
->subtype
== PTY_TYPE_SLAVE
) {
1271 * It's safe from now on because init_dev() is called with
1272 * tty_mutex held and release_dev() won't change tty->count
1273 * or tty->flags without having to grab tty_mutex
1275 if (tty
&& driver
->subtype
== PTY_TYPE_MASTER
)
1278 tty
= driver
->ttys
[idx
];
1280 if (tty
) goto fast_track
;
1283 * First time open is complex, especially for PTY devices.
1284 * This code guarantees that either everything succeeds and the
1285 * TTY is ready for operation, or else the table slots are vacated
1286 * and the allocated memory released. (Except that the termios
1287 * and locked termios may be retained.)
1290 if (!try_module_get(driver
->owner
)) {
1299 tty
= alloc_tty_struct();
1302 initialize_tty_struct(tty
);
1303 tty
->driver
= driver
;
1304 tty
->ops
= driver
->ops
;
1306 tty_line_name(driver
, idx
, tty
->name
);
1308 if (driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) {
1309 tp_loc
= &tty
->termios
;
1310 ltp_loc
= &tty
->termios_locked
;
1312 tp_loc
= &driver
->termios
[idx
];
1313 ltp_loc
= &driver
->termios_locked
[idx
];
1317 tp
= kmalloc(sizeof(struct ktermios
), GFP_KERNEL
);
1320 *tp
= driver
->init_termios
;
1324 ltp
= kzalloc(sizeof(struct ktermios
), GFP_KERNEL
);
1329 if (driver
->type
== TTY_DRIVER_TYPE_PTY
) {
1330 o_tty
= alloc_tty_struct();
1333 if (!try_module_get(driver
->other
->owner
)) {
1334 /* This cannot in fact currently happen */
1335 free_tty_struct(o_tty
);
1339 initialize_tty_struct(o_tty
);
1340 o_tty
->driver
= driver
->other
;
1341 o_tty
->ops
= driver
->ops
;
1343 tty_line_name(driver
->other
, idx
, o_tty
->name
);
1345 if (driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) {
1346 o_tp_loc
= &o_tty
->termios
;
1347 o_ltp_loc
= &o_tty
->termios_locked
;
1349 o_tp_loc
= &driver
->other
->termios
[idx
];
1350 o_ltp_loc
= &driver
->other
->termios_locked
[idx
];
1354 o_tp
= kmalloc(sizeof(struct ktermios
), GFP_KERNEL
);
1357 *o_tp
= driver
->other
->init_termios
;
1361 o_ltp
= kzalloc(sizeof(struct ktermios
), GFP_KERNEL
);
1367 * Everything allocated ... set up the o_tty structure.
1369 if (!(driver
->other
->flags
& TTY_DRIVER_DEVPTS_MEM
))
1370 driver
->other
->ttys
[idx
] = o_tty
;
1375 o_tty
->termios
= *o_tp_loc
;
1376 o_tty
->termios_locked
= *o_ltp_loc
;
1377 driver
->other
->refcount
++;
1378 if (driver
->subtype
== PTY_TYPE_MASTER
)
1381 /* Establish the links in both directions */
1387 * All structures have been allocated, so now we install them.
1388 * Failures after this point use release_tty to clean up, so
1389 * there's no need to null out the local pointers.
1391 if (!(driver
->flags
& TTY_DRIVER_DEVPTS_MEM
))
1392 driver
->ttys
[idx
] = tty
;
1398 tty
->termios
= *tp_loc
;
1399 tty
->termios_locked
= *ltp_loc
;
1400 /* Compatibility until drivers always set this */
1401 tty
->termios
->c_ispeed
= tty_termios_input_baud_rate(tty
->termios
);
1402 tty
->termios
->c_ospeed
= tty_termios_baud_rate(tty
->termios
);
1407 * Structures all installed ... call the ldisc open routines.
1408 * If we fail here just call release_tty to clean up. No need
1409 * to decrement the use counts, as release_tty doesn't care.
1412 retval
= tty_ldisc_setup(tty
, o_tty
);
1415 goto release_mem_out
;
1419 * This fast open can be used if the tty is already open.
1420 * No memory is allocated, and the only failures are from
1421 * attempting to open a closing tty or attempting multiple
1422 * opens on a pty master.
1425 if (test_bit(TTY_CLOSING
, &tty
->flags
)) {
1429 if (driver
->type
== TTY_DRIVER_TYPE_PTY
&&
1430 driver
->subtype
== PTY_TYPE_MASTER
) {
1432 * special case for PTY masters: only one open permitted,
1433 * and the slave side open count is incremented as well.
1442 tty
->driver
= driver
; /* N.B. why do this every time?? */
1445 if (!test_bit(TTY_LDISC
, &tty
->flags
))
1446 printk(KERN_ERR
"init_dev but no ldisc\n");
1450 /* All paths come through here to release the mutex */
1454 /* Release locally allocated memory ... nothing placed in slots */
1458 module_put(o_tty
->driver
->owner
);
1459 free_tty_struct(o_tty
);
1463 free_tty_struct(tty
);
1466 module_put(driver
->owner
);
1470 /* call the tty release_tty routine to clean out this slot */
1472 if (printk_ratelimit())
1473 printk(KERN_INFO
"init_dev: ldisc open failed, "
1474 "clearing slot %d\n", idx
);
1475 release_tty(tty
, idx
);
1480 * release_one_tty - release tty structure memory
1481 * @kref: kref of tty we are obliterating
1483 * Releases memory associated with a tty structure, and clears out the
1484 * driver table slots. This function is called when a device is no longer
1485 * in use. It also gets called when setup of a device fails.
1488 * tty_mutex - sometimes only
1489 * takes the file list lock internally when working on the list
1490 * of ttys that the driver keeps.
1492 static void release_one_tty(struct kref
*kref
)
1494 struct tty_struct
*tty
= container_of(kref
, struct tty_struct
, kref
);
1495 struct tty_driver
*driver
= tty
->driver
;
1496 int devpts
= tty
->driver
->flags
& TTY_DRIVER_DEVPTS_MEM
;
1497 struct ktermios
*tp
;
1498 int idx
= tty
->index
;
1501 tty
->driver
->ttys
[idx
] = NULL
;
1503 if (tty
->driver
->flags
& TTY_DRIVER_RESET_TERMIOS
) {
1504 /* FIXME: Locking on ->termios array */
1507 tty
->driver
->termios
[idx
] = NULL
;
1510 tp
= tty
->termios_locked
;
1512 tty
->driver
->termios_locked
[idx
] = NULL
;
1518 /* FIXME: locking on tty->driver->refcount */
1519 tty
->driver
->refcount
--;
1520 module_put(driver
->owner
);
1523 list_del_init(&tty
->tty_files
);
1526 free_tty_struct(tty
);
1530 * tty_kref_put - release a tty kref
1533 * Release a reference to a tty device and if need be let the kref
1534 * layer destruct the object for us
1537 void tty_kref_put(struct tty_struct
*tty
)
1540 kref_put(&tty
->kref
, release_one_tty
);
1542 EXPORT_SYMBOL(tty_kref_put
);
1545 * release_tty - release tty structure memory
1547 * Release both @tty and a possible linked partner (think pty pair),
1548 * and decrement the refcount of the backing module.
1551 * tty_mutex - sometimes only
1552 * takes the file list lock internally when working on the list
1553 * of ttys that the driver keeps.
1554 * FIXME: should we require tty_mutex is held here ??
1557 static void release_tty(struct tty_struct
*tty
, int idx
)
1559 /* This should always be true but check for the moment */
1560 WARN_ON(tty
->index
!= idx
);
1563 tty_kref_put(tty
->link
);
1568 * Even releasing the tty structures is a tricky business.. We have
1569 * to be very careful that the structures are all released at the
1570 * same time, as interrupts might otherwise get the wrong pointers.
1572 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1573 * lead to double frees or releasing memory still in use.
1575 static void release_dev(struct file
*filp
)
1577 struct tty_struct
*tty
, *o_tty
;
1578 int pty_master
, tty_closing
, o_tty_closing
, do_sleep
;
1583 tty
= (struct tty_struct
*)filp
->private_data
;
1584 if (tty_paranoia_check(tty
, filp
->f_path
.dentry
->d_inode
,
1588 check_tty_count(tty
, "release_dev");
1590 tty_fasync(-1, filp
, 0);
1593 pty_master
= (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
1594 tty
->driver
->subtype
== PTY_TYPE_MASTER
);
1595 devpts
= (tty
->driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) != 0;
1598 #ifdef TTY_PARANOIA_CHECK
1599 if (idx
< 0 || idx
>= tty
->driver
->num
) {
1600 printk(KERN_DEBUG
"release_dev: bad idx when trying to "
1601 "free (%s)\n", tty
->name
);
1604 if (!(tty
->driver
->flags
& TTY_DRIVER_DEVPTS_MEM
)) {
1605 if (tty
!= tty
->driver
->ttys
[idx
]) {
1606 printk(KERN_DEBUG
"release_dev: driver.table[%d] not tty "
1607 "for (%s)\n", idx
, tty
->name
);
1610 if (tty
->termios
!= tty
->driver
->termios
[idx
]) {
1611 printk(KERN_DEBUG
"release_dev: driver.termios[%d] not termios "
1616 if (tty
->termios_locked
!= tty
->driver
->termios_locked
[idx
]) {
1617 printk(KERN_DEBUG
"release_dev: driver.termios_locked[%d] not "
1618 "termios_locked for (%s)\n",
1625 #ifdef TTY_DEBUG_HANGUP
1626 printk(KERN_DEBUG
"release_dev of %s (tty count=%d)...",
1627 tty_name(tty
, buf
), tty
->count
);
1630 #ifdef TTY_PARANOIA_CHECK
1631 if (tty
->driver
->other
&&
1632 !(tty
->driver
->flags
& TTY_DRIVER_DEVPTS_MEM
)) {
1633 if (o_tty
!= tty
->driver
->other
->ttys
[idx
]) {
1634 printk(KERN_DEBUG
"release_dev: other->table[%d] "
1635 "not o_tty for (%s)\n",
1639 if (o_tty
->termios
!= tty
->driver
->other
->termios
[idx
]) {
1640 printk(KERN_DEBUG
"release_dev: other->termios[%d] "
1641 "not o_termios for (%s)\n",
1645 if (o_tty
->termios_locked
!=
1646 tty
->driver
->other
->termios_locked
[idx
]) {
1647 printk(KERN_DEBUG
"release_dev: other->termios_locked["
1648 "%d] not o_termios_locked for (%s)\n",
1652 if (o_tty
->link
!= tty
) {
1653 printk(KERN_DEBUG
"release_dev: bad pty pointers\n");
1658 if (tty
->ops
->close
)
1659 tty
->ops
->close(tty
, filp
);
1662 * Sanity check: if tty->count is going to zero, there shouldn't be
1663 * any waiters on tty->read_wait or tty->write_wait. We test the
1664 * wait queues and kick everyone out _before_ actually starting to
1665 * close. This ensures that we won't block while releasing the tty
1668 * The test for the o_tty closing is necessary, since the master and
1669 * slave sides may close in any order. If the slave side closes out
1670 * first, its count will be one, since the master side holds an open.
1671 * Thus this test wouldn't be triggered at the time the slave closes,
1674 * Note that it's possible for the tty to be opened again while we're
1675 * flushing out waiters. By recalculating the closing flags before
1676 * each iteration we avoid any problems.
1679 /* Guard against races with tty->count changes elsewhere and
1680 opens on /dev/tty */
1682 mutex_lock(&tty_mutex
);
1683 tty_closing
= tty
->count
<= 1;
1684 o_tty_closing
= o_tty
&&
1685 (o_tty
->count
<= (pty_master
? 1 : 0));
1689 if (waitqueue_active(&tty
->read_wait
)) {
1690 wake_up(&tty
->read_wait
);
1693 if (waitqueue_active(&tty
->write_wait
)) {
1694 wake_up(&tty
->write_wait
);
1698 if (o_tty_closing
) {
1699 if (waitqueue_active(&o_tty
->read_wait
)) {
1700 wake_up(&o_tty
->read_wait
);
1703 if (waitqueue_active(&o_tty
->write_wait
)) {
1704 wake_up(&o_tty
->write_wait
);
1711 printk(KERN_WARNING
"release_dev: %s: read/write wait queue "
1712 "active!\n", tty_name(tty
, buf
));
1713 mutex_unlock(&tty_mutex
);
1718 * The closing flags are now consistent with the open counts on
1719 * both sides, and we've completed the last operation that could
1720 * block, so it's safe to proceed with closing.
1723 if (--o_tty
->count
< 0) {
1724 printk(KERN_WARNING
"release_dev: bad pty slave count "
1726 o_tty
->count
, tty_name(o_tty
, buf
));
1730 if (--tty
->count
< 0) {
1731 printk(KERN_WARNING
"release_dev: bad tty->count (%d) for %s\n",
1732 tty
->count
, tty_name(tty
, buf
));
1737 * We've decremented tty->count, so we need to remove this file
1738 * descriptor off the tty->tty_files list; this serves two
1740 * - check_tty_count sees the correct number of file descriptors
1741 * associated with this tty.
1742 * - do_tty_hangup no longer sees this file descriptor as
1743 * something that needs to be handled for hangups.
1746 filp
->private_data
= NULL
;
1749 * Perform some housekeeping before deciding whether to return.
1751 * Set the TTY_CLOSING flag if this was the last open. In the
1752 * case of a pty we may have to wait around for the other side
1753 * to close, and TTY_CLOSING makes sure we can't be reopened.
1756 set_bit(TTY_CLOSING
, &tty
->flags
);
1758 set_bit(TTY_CLOSING
, &o_tty
->flags
);
1761 * If _either_ side is closing, make sure there aren't any
1762 * processes that still think tty or o_tty is their controlling
1765 if (tty_closing
|| o_tty_closing
) {
1766 read_lock(&tasklist_lock
);
1767 session_clear_tty(tty
->session
);
1769 session_clear_tty(o_tty
->session
);
1770 read_unlock(&tasklist_lock
);
1773 mutex_unlock(&tty_mutex
);
1775 /* check whether both sides are closing ... */
1776 if (!tty_closing
|| (o_tty
&& !o_tty_closing
))
1779 #ifdef TTY_DEBUG_HANGUP
1780 printk(KERN_DEBUG
"freeing tty structure...");
1783 * Ask the line discipline code to release its structures
1785 tty_ldisc_release(tty
, o_tty
);
1787 * The release_tty function takes care of the details of clearing
1788 * the slots and preserving the termios structure.
1790 release_tty(tty
, idx
);
1792 /* Make this pty number available for reallocation */
1794 devpts_kill_index(idx
);
1798 * tty_open - open a tty device
1799 * @inode: inode of device file
1800 * @filp: file pointer to tty
1802 * tty_open and tty_release keep up the tty count that contains the
1803 * number of opens done on a tty. We cannot use the inode-count, as
1804 * different inodes might point to the same tty.
1806 * Open-counting is needed for pty masters, as well as for keeping
1807 * track of serial lines: DTR is dropped when the last close happens.
1808 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1810 * The termios state of a pty is reset on first open so that
1811 * settings don't persist across reuse.
1813 * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
1814 * tty->count should protect the rest.
1815 * ->siglock protects ->signal/->sighand
1818 static int __tty_open(struct inode
*inode
, struct file
*filp
)
1820 struct tty_struct
*tty
;
1822 struct tty_driver
*driver
;
1824 dev_t device
= inode
->i_rdev
;
1825 unsigned short saved_flags
= filp
->f_flags
;
1827 nonseekable_open(inode
, filp
);
1830 noctty
= filp
->f_flags
& O_NOCTTY
;
1834 mutex_lock(&tty_mutex
);
1836 if (device
== MKDEV(TTYAUX_MAJOR
, 0)) {
1837 tty
= get_current_tty();
1839 mutex_unlock(&tty_mutex
);
1842 driver
= tty
->driver
;
1844 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/tty block */
1846 /* FIXME: Should we take a driver reference ? */
1851 if (device
== MKDEV(TTY_MAJOR
, 0)) {
1852 extern struct tty_driver
*console_driver
;
1853 driver
= console_driver
;
1859 if (device
== MKDEV(TTYAUX_MAJOR
, 1)) {
1860 driver
= console_device(&index
);
1862 /* Don't let /dev/console block */
1863 filp
->f_flags
|= O_NONBLOCK
;
1867 mutex_unlock(&tty_mutex
);
1871 driver
= get_tty_driver(device
, &index
);
1873 mutex_unlock(&tty_mutex
);
1877 retval
= init_dev(driver
, index
, &tty
);
1878 mutex_unlock(&tty_mutex
);
1882 filp
->private_data
= tty
;
1883 file_move(filp
, &tty
->tty_files
);
1884 check_tty_count(tty
, "tty_open");
1885 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
1886 tty
->driver
->subtype
== PTY_TYPE_MASTER
)
1888 #ifdef TTY_DEBUG_HANGUP
1889 printk(KERN_DEBUG
"opening %s...", tty
->name
);
1893 retval
= tty
->ops
->open(tty
, filp
);
1897 filp
->f_flags
= saved_flags
;
1899 if (!retval
&& test_bit(TTY_EXCLUSIVE
, &tty
->flags
) &&
1900 !capable(CAP_SYS_ADMIN
))
1904 #ifdef TTY_DEBUG_HANGUP
1905 printk(KERN_DEBUG
"error %d in opening %s...", retval
,
1909 if (retval
!= -ERESTARTSYS
)
1911 if (signal_pending(current
))
1915 * Need to reset f_op in case a hangup happened.
1917 if (filp
->f_op
== &hung_up_tty_fops
)
1918 filp
->f_op
= &tty_fops
;
1922 mutex_lock(&tty_mutex
);
1923 spin_lock_irq(¤t
->sighand
->siglock
);
1925 current
->signal
->leader
&&
1926 !current
->signal
->tty
&&
1927 tty
->session
== NULL
)
1928 __proc_set_tty(current
, tty
);
1929 spin_unlock_irq(¤t
->sighand
->siglock
);
1930 mutex_unlock(&tty_mutex
);
1934 /* BKL pushdown: scary code avoidance wrapper */
1935 static int tty_open(struct inode
*inode
, struct file
*filp
)
1940 ret
= __tty_open(inode
, filp
);
1947 #ifdef CONFIG_UNIX98_PTYS
1949 * ptmx_open - open a unix 98 pty master
1950 * @inode: inode of device file
1951 * @filp: file pointer to tty
1953 * Allocate a unix98 pty master device from the ptmx driver.
1955 * Locking: tty_mutex protects theinit_dev work. tty->count should
1957 * allocated_ptys_lock handles the list of free pty numbers
1960 static int __ptmx_open(struct inode
*inode
, struct file
*filp
)
1962 struct tty_struct
*tty
;
1966 nonseekable_open(inode
, filp
);
1968 /* find a device that is not in use. */
1969 index
= devpts_new_index();
1973 mutex_lock(&tty_mutex
);
1974 retval
= init_dev(ptm_driver
, index
, &tty
);
1975 mutex_unlock(&tty_mutex
);
1980 set_bit(TTY_PTY_LOCK
, &tty
->flags
); /* LOCK THE SLAVE */
1981 filp
->private_data
= tty
;
1982 file_move(filp
, &tty
->tty_files
);
1984 retval
= devpts_pty_new(tty
->link
);
1988 check_tty_count(tty
, "ptmx_open");
1989 retval
= ptm_driver
->ops
->open(tty
, filp
);
1996 devpts_kill_index(index
);
2000 static int ptmx_open(struct inode
*inode
, struct file
*filp
)
2005 ret
= __ptmx_open(inode
, filp
);
2012 * tty_release - vfs callback for close
2013 * @inode: inode of tty
2014 * @filp: file pointer for handle to tty
2016 * Called the last time each file handle is closed that references
2017 * this tty. There may however be several such references.
2020 * Takes bkl. See release_dev
2023 static int tty_release(struct inode
*inode
, struct file
*filp
)
2032 * tty_poll - check tty status
2033 * @filp: file being polled
2034 * @wait: poll wait structures to update
2036 * Call the line discipline polling method to obtain the poll
2037 * status of the device.
2039 * Locking: locks called line discipline but ldisc poll method
2040 * may be re-entered freely by other callers.
2043 static unsigned int tty_poll(struct file
*filp
, poll_table
*wait
)
2045 struct tty_struct
*tty
;
2046 struct tty_ldisc
*ld
;
2049 tty
= (struct tty_struct
*)filp
->private_data
;
2050 if (tty_paranoia_check(tty
, filp
->f_path
.dentry
->d_inode
, "tty_poll"))
2053 ld
= tty_ldisc_ref_wait(tty
);
2055 ret
= (ld
->ops
->poll
)(tty
, filp
, wait
);
2056 tty_ldisc_deref(ld
);
2060 static int tty_fasync(int fd
, struct file
*filp
, int on
)
2062 struct tty_struct
*tty
;
2063 unsigned long flags
;
2067 tty
= (struct tty_struct
*)filp
->private_data
;
2068 if (tty_paranoia_check(tty
, filp
->f_path
.dentry
->d_inode
, "tty_fasync"))
2071 retval
= fasync_helper(fd
, filp
, on
, &tty
->fasync
);
2078 if (!waitqueue_active(&tty
->read_wait
))
2079 tty
->minimum_to_wake
= 1;
2080 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
2083 type
= PIDTYPE_PGID
;
2085 pid
= task_pid(current
);
2088 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
2089 retval
= __f_setown(filp
, pid
, type
, 0);
2093 if (!tty
->fasync
&& !waitqueue_active(&tty
->read_wait
))
2094 tty
->minimum_to_wake
= N_TTY_BUF_SIZE
;
2103 * tiocsti - fake input character
2104 * @tty: tty to fake input into
2105 * @p: pointer to character
2107 * Fake input to a tty device. Does the necessary locking and
2110 * FIXME: does not honour flow control ??
2113 * Called functions take tty_ldisc_lock
2114 * current->signal->tty check is safe without locks
2116 * FIXME: may race normal receive processing
2119 static int tiocsti(struct tty_struct
*tty
, char __user
*p
)
2122 struct tty_ldisc
*ld
;
2124 if ((current
->signal
->tty
!= tty
) && !capable(CAP_SYS_ADMIN
))
2126 if (get_user(ch
, p
))
2128 ld
= tty_ldisc_ref_wait(tty
);
2129 ld
->ops
->receive_buf(tty
, &ch
, &mbz
, 1);
2130 tty_ldisc_deref(ld
);
2135 * tiocgwinsz - implement window query ioctl
2137 * @arg: user buffer for result
2139 * Copies the kernel idea of the window size into the user buffer.
2141 * Locking: tty->termios_mutex is taken to ensure the winsize data
2145 static int tiocgwinsz(struct tty_struct
*tty
, struct winsize __user
*arg
)
2149 mutex_lock(&tty
->termios_mutex
);
2150 err
= copy_to_user(arg
, &tty
->winsize
, sizeof(*arg
));
2151 mutex_unlock(&tty
->termios_mutex
);
2153 return err
? -EFAULT
: 0;
2157 * tty_do_resize - resize event
2158 * @tty: tty being resized
2159 * @real_tty: real tty (not the same as tty if using a pty/tty pair)
2160 * @rows: rows (character)
2161 * @cols: cols (character)
2163 * Update the termios variables and send the neccessary signals to
2164 * peform a terminal resize correctly
2167 int tty_do_resize(struct tty_struct
*tty
, struct tty_struct
*real_tty
,
2170 struct pid
*pgrp
, *rpgrp
;
2171 unsigned long flags
;
2173 /* For a PTY we need to lock the tty side */
2174 mutex_lock(&real_tty
->termios_mutex
);
2175 if (!memcmp(ws
, &real_tty
->winsize
, sizeof(*ws
)))
2177 /* Get the PID values and reference them so we can
2178 avoid holding the tty ctrl lock while sending signals */
2179 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
2180 pgrp
= get_pid(tty
->pgrp
);
2181 rpgrp
= get_pid(real_tty
->pgrp
);
2182 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
2185 kill_pgrp(pgrp
, SIGWINCH
, 1);
2186 if (rpgrp
!= pgrp
&& rpgrp
)
2187 kill_pgrp(rpgrp
, SIGWINCH
, 1);
2193 real_tty
->winsize
= *ws
;
2195 mutex_unlock(&real_tty
->termios_mutex
);
2200 * tiocswinsz - implement window size set ioctl
2202 * @arg: user buffer for result
2204 * Copies the user idea of the window size to the kernel. Traditionally
2205 * this is just advisory information but for the Linux console it
2206 * actually has driver level meaning and triggers a VC resize.
2209 * Driver dependant. The default do_resize method takes the
2210 * tty termios mutex and ctrl_lock. The console takes its own lock
2211 * then calls into the default method.
2214 static int tiocswinsz(struct tty_struct
*tty
, struct tty_struct
*real_tty
,
2215 struct winsize __user
*arg
)
2217 struct winsize tmp_ws
;
2218 if (copy_from_user(&tmp_ws
, arg
, sizeof(*arg
)))
2221 if (tty
->ops
->resize
)
2222 return tty
->ops
->resize(tty
, real_tty
, &tmp_ws
);
2224 return tty_do_resize(tty
, real_tty
, &tmp_ws
);
2228 * tioccons - allow admin to move logical console
2229 * @file: the file to become console
2231 * Allow the adminstrator to move the redirected console device
2233 * Locking: uses redirect_lock to guard the redirect information
2236 static int tioccons(struct file
*file
)
2238 if (!capable(CAP_SYS_ADMIN
))
2240 if (file
->f_op
->write
== redirected_tty_write
) {
2242 spin_lock(&redirect_lock
);
2245 spin_unlock(&redirect_lock
);
2250 spin_lock(&redirect_lock
);
2252 spin_unlock(&redirect_lock
);
2257 spin_unlock(&redirect_lock
);
2262 * fionbio - non blocking ioctl
2263 * @file: file to set blocking value
2264 * @p: user parameter
2266 * Historical tty interfaces had a blocking control ioctl before
2267 * the generic functionality existed. This piece of history is preserved
2268 * in the expected tty API of posix OS's.
2270 * Locking: none, the open fle handle ensures it won't go away.
2273 static int fionbio(struct file
*file
, int __user
*p
)
2277 if (get_user(nonblock
, p
))
2280 /* file->f_flags is still BKL protected in the fs layer - vomit */
2283 file
->f_flags
|= O_NONBLOCK
;
2285 file
->f_flags
&= ~O_NONBLOCK
;
2291 * tiocsctty - set controlling tty
2292 * @tty: tty structure
2293 * @arg: user argument
2295 * This ioctl is used to manage job control. It permits a session
2296 * leader to set this tty as the controlling tty for the session.
2299 * Takes tty_mutex() to protect tty instance
2300 * Takes tasklist_lock internally to walk sessions
2301 * Takes ->siglock() when updating signal->tty
2304 static int tiocsctty(struct tty_struct
*tty
, int arg
)
2307 if (current
->signal
->leader
&& (task_session(current
) == tty
->session
))
2310 mutex_lock(&tty_mutex
);
2312 * The process must be a session leader and
2313 * not have a controlling tty already.
2315 if (!current
->signal
->leader
|| current
->signal
->tty
) {
2322 * This tty is already the controlling
2323 * tty for another session group!
2325 if (arg
== 1 && capable(CAP_SYS_ADMIN
)) {
2329 read_lock(&tasklist_lock
);
2330 session_clear_tty(tty
->session
);
2331 read_unlock(&tasklist_lock
);
2337 proc_set_tty(current
, tty
);
2339 mutex_unlock(&tty_mutex
);
2344 * tty_get_pgrp - return a ref counted pgrp pid
2347 * Returns a refcounted instance of the pid struct for the process
2348 * group controlling the tty.
2351 struct pid
*tty_get_pgrp(struct tty_struct
*tty
)
2353 unsigned long flags
;
2356 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
2357 pgrp
= get_pid(tty
->pgrp
);
2358 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
2362 EXPORT_SYMBOL_GPL(tty_get_pgrp
);
2365 * tiocgpgrp - get process group
2366 * @tty: tty passed by user
2367 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2370 * Obtain the process group of the tty. If there is no process group
2373 * Locking: none. Reference to current->signal->tty is safe.
2376 static int tiocgpgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t __user
*p
)
2381 * (tty == real_tty) is a cheap way of
2382 * testing if the tty is NOT a master pty.
2384 if (tty
== real_tty
&& current
->signal
->tty
!= real_tty
)
2386 pid
= tty_get_pgrp(real_tty
);
2387 ret
= put_user(pid_vnr(pid
), p
);
2393 * tiocspgrp - attempt to set process group
2394 * @tty: tty passed by user
2395 * @real_tty: tty side device matching tty passed by user
2398 * Set the process group of the tty to the session passed. Only
2399 * permitted where the tty session is our session.
2401 * Locking: RCU, ctrl lock
2404 static int tiocspgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t __user
*p
)
2408 int retval
= tty_check_change(real_tty
);
2409 unsigned long flags
;
2415 if (!current
->signal
->tty
||
2416 (current
->signal
->tty
!= real_tty
) ||
2417 (real_tty
->session
!= task_session(current
)))
2419 if (get_user(pgrp_nr
, p
))
2424 pgrp
= find_vpid(pgrp_nr
);
2429 if (session_of_pgrp(pgrp
) != task_session(current
))
2432 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
2433 put_pid(real_tty
->pgrp
);
2434 real_tty
->pgrp
= get_pid(pgrp
);
2435 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
2442 * tiocgsid - get session id
2443 * @tty: tty passed by user
2444 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2445 * @p: pointer to returned session id
2447 * Obtain the session id of the tty. If there is no session
2450 * Locking: none. Reference to current->signal->tty is safe.
2453 static int tiocgsid(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t __user
*p
)
2456 * (tty == real_tty) is a cheap way of
2457 * testing if the tty is NOT a master pty.
2459 if (tty
== real_tty
&& current
->signal
->tty
!= real_tty
)
2461 if (!real_tty
->session
)
2463 return put_user(pid_vnr(real_tty
->session
), p
);
2467 * tiocsetd - set line discipline
2469 * @p: pointer to user data
2471 * Set the line discipline according to user request.
2473 * Locking: see tty_set_ldisc, this function is just a helper
2476 static int tiocsetd(struct tty_struct
*tty
, int __user
*p
)
2481 if (get_user(ldisc
, p
))
2485 ret
= tty_set_ldisc(tty
, ldisc
);
2492 * send_break - performed time break
2493 * @tty: device to break on
2494 * @duration: timeout in mS
2496 * Perform a timed break on hardware that lacks its own driver level
2497 * timed break functionality.
2500 * atomic_write_lock serializes
2504 static int send_break(struct tty_struct
*tty
, unsigned int duration
)
2508 if (tty
->ops
->break_ctl
== NULL
)
2511 if (tty
->driver
->flags
& TTY_DRIVER_HARDWARE_BREAK
)
2512 retval
= tty
->ops
->break_ctl(tty
, duration
);
2514 /* Do the work ourselves */
2515 if (tty_write_lock(tty
, 0) < 0)
2517 retval
= tty
->ops
->break_ctl(tty
, -1);
2520 if (!signal_pending(current
))
2521 msleep_interruptible(duration
);
2522 retval
= tty
->ops
->break_ctl(tty
, 0);
2524 tty_write_unlock(tty
);
2525 if (signal_pending(current
))
2532 * tty_tiocmget - get modem status
2534 * @file: user file pointer
2535 * @p: pointer to result
2537 * Obtain the modem status bits from the tty driver if the feature
2538 * is supported. Return -EINVAL if it is not available.
2540 * Locking: none (up to the driver)
2543 static int tty_tiocmget(struct tty_struct
*tty
, struct file
*file
, int __user
*p
)
2545 int retval
= -EINVAL
;
2547 if (tty
->ops
->tiocmget
) {
2548 retval
= tty
->ops
->tiocmget(tty
, file
);
2551 retval
= put_user(retval
, p
);
2557 * tty_tiocmset - set modem status
2559 * @file: user file pointer
2560 * @cmd: command - clear bits, set bits or set all
2561 * @p: pointer to desired bits
2563 * Set the modem status bits from the tty driver if the feature
2564 * is supported. Return -EINVAL if it is not available.
2566 * Locking: none (up to the driver)
2569 static int tty_tiocmset(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
,
2573 unsigned int set
, clear
, val
;
2575 if (tty
->ops
->tiocmset
== NULL
)
2578 retval
= get_user(val
, p
);
2594 set
&= TIOCM_DTR
|TIOCM_RTS
|TIOCM_OUT1
|TIOCM_OUT2
|TIOCM_LOOP
;
2595 clear
&= TIOCM_DTR
|TIOCM_RTS
|TIOCM_OUT1
|TIOCM_OUT2
|TIOCM_LOOP
;
2596 return tty
->ops
->tiocmset(tty
, file
, set
, clear
);
2600 * Split this up, as gcc can choke on it otherwise..
2602 long tty_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2604 struct tty_struct
*tty
, *real_tty
;
2605 void __user
*p
= (void __user
*)arg
;
2607 struct tty_ldisc
*ld
;
2608 struct inode
*inode
= file
->f_dentry
->d_inode
;
2610 tty
= (struct tty_struct
*)file
->private_data
;
2611 if (tty_paranoia_check(tty
, inode
, "tty_ioctl"))
2615 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
2616 tty
->driver
->subtype
== PTY_TYPE_MASTER
)
2617 real_tty
= tty
->link
;
2621 * Factor out some common prep work
2629 retval
= tty_check_change(tty
);
2632 if (cmd
!= TIOCCBRK
) {
2633 tty_wait_until_sent(tty
, 0);
2634 if (signal_pending(current
))
2645 return tiocsti(tty
, p
);
2647 return tiocgwinsz(real_tty
, p
);
2649 return tiocswinsz(tty
, real_tty
, p
);
2651 return real_tty
!= tty
? -EINVAL
: tioccons(file
);
2653 return fionbio(file
, p
);
2655 set_bit(TTY_EXCLUSIVE
, &tty
->flags
);
2658 clear_bit(TTY_EXCLUSIVE
, &tty
->flags
);
2661 if (current
->signal
->tty
!= tty
)
2666 return tiocsctty(tty
, arg
);
2668 return tiocgpgrp(tty
, real_tty
, p
);
2670 return tiocspgrp(tty
, real_tty
, p
);
2672 return tiocgsid(tty
, real_tty
, p
);
2674 return put_user(tty
->ldisc
.ops
->num
, (int __user
*)p
);
2676 return tiocsetd(tty
, p
);
2680 case TIOCSBRK
: /* Turn break on, unconditionally */
2681 if (tty
->ops
->break_ctl
)
2682 return tty
->ops
->break_ctl(tty
, -1);
2684 case TIOCCBRK
: /* Turn break off, unconditionally */
2685 if (tty
->ops
->break_ctl
)
2686 return tty
->ops
->break_ctl(tty
, 0);
2688 case TCSBRK
: /* SVID version: non-zero arg --> no break */
2689 /* non-zero arg means wait for all output data
2690 * to be sent (performed above) but don't send break.
2691 * This is used by the tcdrain() termios function.
2694 return send_break(tty
, 250);
2696 case TCSBRKP
: /* support for POSIX tcsendbreak() */
2697 return send_break(tty
, arg
? arg
*100 : 250);
2700 return tty_tiocmget(tty
, file
, p
);
2704 return tty_tiocmset(tty
, file
, cmd
, p
);
2709 /* flush tty buffer and allow ldisc to process ioctl */
2710 tty_buffer_flush(tty
);
2715 if (tty
->ops
->ioctl
) {
2716 retval
= (tty
->ops
->ioctl
)(tty
, file
, cmd
, arg
);
2717 if (retval
!= -ENOIOCTLCMD
)
2720 ld
= tty_ldisc_ref_wait(tty
);
2722 if (ld
->ops
->ioctl
) {
2723 retval
= ld
->ops
->ioctl(tty
, file
, cmd
, arg
);
2724 if (retval
== -ENOIOCTLCMD
)
2727 tty_ldisc_deref(ld
);
2731 #ifdef CONFIG_COMPAT
2732 static long tty_compat_ioctl(struct file
*file
, unsigned int cmd
,
2735 struct inode
*inode
= file
->f_dentry
->d_inode
;
2736 struct tty_struct
*tty
= file
->private_data
;
2737 struct tty_ldisc
*ld
;
2738 int retval
= -ENOIOCTLCMD
;
2740 if (tty_paranoia_check(tty
, inode
, "tty_ioctl"))
2743 if (tty
->ops
->compat_ioctl
) {
2744 retval
= (tty
->ops
->compat_ioctl
)(tty
, file
, cmd
, arg
);
2745 if (retval
!= -ENOIOCTLCMD
)
2749 ld
= tty_ldisc_ref_wait(tty
);
2750 if (ld
->ops
->compat_ioctl
)
2751 retval
= ld
->ops
->compat_ioctl(tty
, file
, cmd
, arg
);
2752 tty_ldisc_deref(ld
);
2759 * This implements the "Secure Attention Key" --- the idea is to
2760 * prevent trojan horses by killing all processes associated with this
2761 * tty when the user hits the "Secure Attention Key". Required for
2762 * super-paranoid applications --- see the Orange Book for more details.
2764 * This code could be nicer; ideally it should send a HUP, wait a few
2765 * seconds, then send a INT, and then a KILL signal. But you then
2766 * have to coordinate with the init process, since all processes associated
2767 * with the current tty must be dead before the new getty is allowed
2770 * Now, if it would be correct ;-/ The current code has a nasty hole -
2771 * it doesn't catch files in flight. We may send the descriptor to ourselves
2772 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2774 * Nasty bug: do_SAK is being called in interrupt context. This can
2775 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2777 void __do_SAK(struct tty_struct
*tty
)
2782 struct task_struct
*g
, *p
;
2783 struct pid
*session
;
2786 struct fdtable
*fdt
;
2790 session
= tty
->session
;
2792 tty_ldisc_flush(tty
);
2794 tty_driver_flush_buffer(tty
);
2796 read_lock(&tasklist_lock
);
2797 /* Kill the entire session */
2798 do_each_pid_task(session
, PIDTYPE_SID
, p
) {
2799 printk(KERN_NOTICE
"SAK: killed process %d"
2800 " (%s): task_session_nr(p)==tty->session\n",
2801 task_pid_nr(p
), p
->comm
);
2802 send_sig(SIGKILL
, p
, 1);
2803 } while_each_pid_task(session
, PIDTYPE_SID
, p
);
2804 /* Now kill any processes that happen to have the
2807 do_each_thread(g
, p
) {
2808 if (p
->signal
->tty
== tty
) {
2809 printk(KERN_NOTICE
"SAK: killed process %d"
2810 " (%s): task_session_nr(p)==tty->session\n",
2811 task_pid_nr(p
), p
->comm
);
2812 send_sig(SIGKILL
, p
, 1);
2818 * We don't take a ref to the file, so we must
2819 * hold ->file_lock instead.
2821 spin_lock(&p
->files
->file_lock
);
2822 fdt
= files_fdtable(p
->files
);
2823 for (i
= 0; i
< fdt
->max_fds
; i
++) {
2824 filp
= fcheck_files(p
->files
, i
);
2827 if (filp
->f_op
->read
== tty_read
&&
2828 filp
->private_data
== tty
) {
2829 printk(KERN_NOTICE
"SAK: killed process %d"
2830 " (%s): fd#%d opened to the tty\n",
2831 task_pid_nr(p
), p
->comm
, i
);
2832 force_sig(SIGKILL
, p
);
2836 spin_unlock(&p
->files
->file_lock
);
2839 } while_each_thread(g
, p
);
2840 read_unlock(&tasklist_lock
);
2844 static void do_SAK_work(struct work_struct
*work
)
2846 struct tty_struct
*tty
=
2847 container_of(work
, struct tty_struct
, SAK_work
);
2852 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2853 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2854 * the values which we write to it will be identical to the values which it
2855 * already has. --akpm
2857 void do_SAK(struct tty_struct
*tty
)
2861 schedule_work(&tty
->SAK_work
);
2864 EXPORT_SYMBOL(do_SAK
);
2867 * initialize_tty_struct
2868 * @tty: tty to initialize
2870 * This subroutine initializes a tty structure that has been newly
2873 * Locking: none - tty in question must not be exposed at this point
2876 static void initialize_tty_struct(struct tty_struct
*tty
)
2878 memset(tty
, 0, sizeof(struct tty_struct
));
2879 kref_init(&tty
->kref
);
2880 tty
->magic
= TTY_MAGIC
;
2881 tty_ldisc_init(tty
);
2882 tty
->session
= NULL
;
2884 tty
->overrun_time
= jiffies
;
2885 tty
->buf
.head
= tty
->buf
.tail
= NULL
;
2886 tty_buffer_init(tty
);
2887 mutex_init(&tty
->termios_mutex
);
2888 init_waitqueue_head(&tty
->write_wait
);
2889 init_waitqueue_head(&tty
->read_wait
);
2890 INIT_WORK(&tty
->hangup_work
, do_tty_hangup
);
2891 mutex_init(&tty
->atomic_read_lock
);
2892 mutex_init(&tty
->atomic_write_lock
);
2893 spin_lock_init(&tty
->read_lock
);
2894 spin_lock_init(&tty
->ctrl_lock
);
2895 INIT_LIST_HEAD(&tty
->tty_files
);
2896 INIT_WORK(&tty
->SAK_work
, do_SAK_work
);
2900 * tty_put_char - write one character to a tty
2904 * Write one byte to the tty using the provided put_char method
2905 * if present. Returns the number of characters successfully output.
2907 * Note: the specific put_char operation in the driver layer may go
2908 * away soon. Don't call it directly, use this method
2911 int tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
2913 if (tty
->ops
->put_char
)
2914 return tty
->ops
->put_char(tty
, ch
);
2915 return tty
->ops
->write(tty
, &ch
, 1);
2918 EXPORT_SYMBOL_GPL(tty_put_char
);
2920 static struct class *tty_class
;
2923 * tty_register_device - register a tty device
2924 * @driver: the tty driver that describes the tty device
2925 * @index: the index in the tty driver for this tty device
2926 * @device: a struct device that is associated with this tty device.
2927 * This field is optional, if there is no known struct device
2928 * for this tty device it can be set to NULL safely.
2930 * Returns a pointer to the struct device for this tty device
2931 * (or ERR_PTR(-EFOO) on error).
2933 * This call is required to be made to register an individual tty device
2934 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2935 * that bit is not set, this function should not be called by a tty
2941 struct device
*tty_register_device(struct tty_driver
*driver
, unsigned index
,
2942 struct device
*device
)
2945 dev_t dev
= MKDEV(driver
->major
, driver
->minor_start
) + index
;
2947 if (index
>= driver
->num
) {
2948 printk(KERN_ERR
"Attempt to register invalid tty line number "
2950 return ERR_PTR(-EINVAL
);
2953 if (driver
->type
== TTY_DRIVER_TYPE_PTY
)
2954 pty_line_name(driver
, index
, name
);
2956 tty_line_name(driver
, index
, name
);
2958 return device_create_drvdata(tty_class
, device
, dev
, NULL
, name
);
2962 * tty_unregister_device - unregister a tty device
2963 * @driver: the tty driver that describes the tty device
2964 * @index: the index in the tty driver for this tty device
2966 * If a tty device is registered with a call to tty_register_device() then
2967 * this function must be called when the tty device is gone.
2972 void tty_unregister_device(struct tty_driver
*driver
, unsigned index
)
2974 device_destroy(tty_class
,
2975 MKDEV(driver
->major
, driver
->minor_start
) + index
);
2978 EXPORT_SYMBOL(tty_register_device
);
2979 EXPORT_SYMBOL(tty_unregister_device
);
2981 struct tty_driver
*alloc_tty_driver(int lines
)
2983 struct tty_driver
*driver
;
2985 driver
= kzalloc(sizeof(struct tty_driver
), GFP_KERNEL
);
2987 driver
->magic
= TTY_DRIVER_MAGIC
;
2988 driver
->num
= lines
;
2989 /* later we'll move allocation of tables here */
2994 void put_tty_driver(struct tty_driver
*driver
)
2999 void tty_set_operations(struct tty_driver
*driver
,
3000 const struct tty_operations
*op
)
3005 EXPORT_SYMBOL(alloc_tty_driver
);
3006 EXPORT_SYMBOL(put_tty_driver
);
3007 EXPORT_SYMBOL(tty_set_operations
);
3010 * Called by a tty driver to register itself.
3012 int tty_register_driver(struct tty_driver
*driver
)
3019 if (driver
->flags
& TTY_DRIVER_INSTALLED
)
3022 if (!(driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) && driver
->num
) {
3023 p
= kzalloc(driver
->num
* 3 * sizeof(void *), GFP_KERNEL
);
3028 if (!driver
->major
) {
3029 error
= alloc_chrdev_region(&dev
, driver
->minor_start
,
3030 driver
->num
, driver
->name
);
3032 driver
->major
= MAJOR(dev
);
3033 driver
->minor_start
= MINOR(dev
);
3036 dev
= MKDEV(driver
->major
, driver
->minor_start
);
3037 error
= register_chrdev_region(dev
, driver
->num
, driver
->name
);
3045 driver
->ttys
= (struct tty_struct
**)p
;
3046 driver
->termios
= (struct ktermios
**)(p
+ driver
->num
);
3047 driver
->termios_locked
= (struct ktermios
**)
3048 (p
+ driver
->num
* 2);
3050 driver
->ttys
= NULL
;
3051 driver
->termios
= NULL
;
3052 driver
->termios_locked
= NULL
;
3055 cdev_init(&driver
->cdev
, &tty_fops
);
3056 driver
->cdev
.owner
= driver
->owner
;
3057 error
= cdev_add(&driver
->cdev
, dev
, driver
->num
);
3059 unregister_chrdev_region(dev
, driver
->num
);
3060 driver
->ttys
= NULL
;
3061 driver
->termios
= driver
->termios_locked
= NULL
;
3066 mutex_lock(&tty_mutex
);
3067 list_add(&driver
->tty_drivers
, &tty_drivers
);
3068 mutex_unlock(&tty_mutex
);
3070 if (!(driver
->flags
& TTY_DRIVER_DYNAMIC_DEV
)) {
3071 for (i
= 0; i
< driver
->num
; i
++)
3072 tty_register_device(driver
, i
, NULL
);
3074 proc_tty_register_driver(driver
);
3078 EXPORT_SYMBOL(tty_register_driver
);
3081 * Called by a tty driver to unregister itself.
3083 int tty_unregister_driver(struct tty_driver
*driver
)
3086 struct ktermios
*tp
;
3089 if (driver
->refcount
)
3092 unregister_chrdev_region(MKDEV(driver
->major
, driver
->minor_start
),
3094 mutex_lock(&tty_mutex
);
3095 list_del(&driver
->tty_drivers
);
3096 mutex_unlock(&tty_mutex
);
3099 * Free the termios and termios_locked structures because
3100 * we don't want to get memory leaks when modular tty
3101 * drivers are removed from the kernel.
3103 for (i
= 0; i
< driver
->num
; i
++) {
3104 tp
= driver
->termios
[i
];
3106 driver
->termios
[i
] = NULL
;
3109 tp
= driver
->termios_locked
[i
];
3111 driver
->termios_locked
[i
] = NULL
;
3114 if (!(driver
->flags
& TTY_DRIVER_DYNAMIC_DEV
))
3115 tty_unregister_device(driver
, i
);
3118 proc_tty_unregister_driver(driver
);
3119 driver
->ttys
= NULL
;
3120 driver
->termios
= driver
->termios_locked
= NULL
;
3122 cdev_del(&driver
->cdev
);
3125 EXPORT_SYMBOL(tty_unregister_driver
);
3127 dev_t
tty_devnum(struct tty_struct
*tty
)
3129 return MKDEV(tty
->driver
->major
, tty
->driver
->minor_start
) + tty
->index
;
3131 EXPORT_SYMBOL(tty_devnum
);
3133 void proc_clear_tty(struct task_struct
*p
)
3135 struct tty_struct
*tty
;
3136 spin_lock_irq(&p
->sighand
->siglock
);
3137 tty
= p
->signal
->tty
;
3138 p
->signal
->tty
= NULL
;
3139 spin_unlock_irq(&p
->sighand
->siglock
);
3143 /* Called under the sighand lock */
3145 static void __proc_set_tty(struct task_struct
*tsk
, struct tty_struct
*tty
)
3148 unsigned long flags
;
3149 /* We should not have a session or pgrp to put here but.... */
3150 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
3151 put_pid(tty
->session
);
3153 tty
->pgrp
= get_pid(task_pgrp(tsk
));
3154 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
3155 tty
->session
= get_pid(task_session(tsk
));
3156 if (tsk
->signal
->tty
) {
3157 printk(KERN_DEBUG
"tty not NULL!!\n");
3158 tty_kref_put(tsk
->signal
->tty
);
3161 put_pid(tsk
->signal
->tty_old_pgrp
);
3162 tsk
->signal
->tty
= tty_kref_get(tty
);
3163 tsk
->signal
->tty_old_pgrp
= NULL
;
3166 static void proc_set_tty(struct task_struct
*tsk
, struct tty_struct
*tty
)
3168 spin_lock_irq(&tsk
->sighand
->siglock
);
3169 __proc_set_tty(tsk
, tty
);
3170 spin_unlock_irq(&tsk
->sighand
->siglock
);
3173 struct tty_struct
*get_current_tty(void)
3175 struct tty_struct
*tty
;
3176 unsigned long flags
;
3178 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
3179 tty
= tty_kref_get(current
->signal
->tty
);
3180 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
3183 EXPORT_SYMBOL_GPL(get_current_tty
);
3186 * Initialize the console device. This is called *early*, so
3187 * we can't necessarily depend on lots of kernel help here.
3188 * Just do some early initializations, and do the complex setup
3191 void __init
console_init(void)
3195 /* Setup the default TTY line discipline. */
3199 * set up the console device so that later boot sequences can
3200 * inform about problems etc..
3202 call
= __con_initcall_start
;
3203 while (call
< __con_initcall_end
) {
3209 static int __init
tty_class_init(void)
3211 tty_class
= class_create(THIS_MODULE
, "tty");
3212 if (IS_ERR(tty_class
))
3213 return PTR_ERR(tty_class
);
3217 postcore_initcall(tty_class_init
);
3219 /* 3/2004 jmc: why do these devices exist? */
3221 static struct cdev tty_cdev
, console_cdev
;
3222 #ifdef CONFIG_UNIX98_PTYS
3223 static struct cdev ptmx_cdev
;
3226 static struct cdev vc0_cdev
;
3230 * Ok, now we can initialize the rest of the tty devices and can count
3231 * on memory allocations, interrupts etc..
3233 static int __init
tty_init(void)
3235 cdev_init(&tty_cdev
, &tty_fops
);
3236 if (cdev_add(&tty_cdev
, MKDEV(TTYAUX_MAJOR
, 0), 1) ||
3237 register_chrdev_region(MKDEV(TTYAUX_MAJOR
, 0), 1, "/dev/tty") < 0)
3238 panic("Couldn't register /dev/tty driver\n");
3239 device_create_drvdata(tty_class
, NULL
, MKDEV(TTYAUX_MAJOR
, 0), NULL
,
3242 cdev_init(&console_cdev
, &console_fops
);
3243 if (cdev_add(&console_cdev
, MKDEV(TTYAUX_MAJOR
, 1), 1) ||
3244 register_chrdev_region(MKDEV(TTYAUX_MAJOR
, 1), 1, "/dev/console") < 0)
3245 panic("Couldn't register /dev/console driver\n");
3246 device_create_drvdata(tty_class
, NULL
, MKDEV(TTYAUX_MAJOR
, 1), NULL
,
3249 #ifdef CONFIG_UNIX98_PTYS
3250 cdev_init(&ptmx_cdev
, &ptmx_fops
);
3251 if (cdev_add(&ptmx_cdev
, MKDEV(TTYAUX_MAJOR
, 2), 1) ||
3252 register_chrdev_region(MKDEV(TTYAUX_MAJOR
, 2), 1, "/dev/ptmx") < 0)
3253 panic("Couldn't register /dev/ptmx driver\n");
3254 device_create_drvdata(tty_class
, NULL
, MKDEV(TTYAUX_MAJOR
, 2), NULL
, "ptmx");
3258 cdev_init(&vc0_cdev
, &console_fops
);
3259 if (cdev_add(&vc0_cdev
, MKDEV(TTY_MAJOR
, 0), 1) ||
3260 register_chrdev_region(MKDEV(TTY_MAJOR
, 0), 1, "/dev/vc/0") < 0)
3261 panic("Couldn't register /dev/tty0 driver\n");
3262 device_create_drvdata(tty_class
, NULL
, MKDEV(TTY_MAJOR
, 0), NULL
, "tty0");
3268 module_init(tty_init
);