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 tty_init_dev and tty_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 /* Spinlock to protect the tty->tty_files list */
140 DEFINE_SPINLOCK(tty_files_lock
);
142 static ssize_t
tty_read(struct file
*, char __user
*, size_t, loff_t
*);
143 static ssize_t
tty_write(struct file
*, const char __user
*, size_t, loff_t
*);
144 ssize_t
redirected_tty_write(struct file
*, const char __user
*,
146 static unsigned int tty_poll(struct file
*, poll_table
*);
147 static int tty_open(struct inode
*, struct file
*);
148 long tty_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
150 static long tty_compat_ioctl(struct file
*file
, unsigned int cmd
,
153 #define tty_compat_ioctl NULL
155 static int __tty_fasync(int fd
, struct file
*filp
, int on
);
156 static int tty_fasync(int fd
, struct file
*filp
, int on
);
157 static void release_tty(struct tty_struct
*tty
, int idx
);
158 static void __proc_set_tty(struct task_struct
*tsk
, struct tty_struct
*tty
);
159 static void proc_set_tty(struct task_struct
*tsk
, struct tty_struct
*tty
);
162 * alloc_tty_struct - allocate a tty object
164 * Return a new empty tty structure. The data fields have not
165 * been initialized in any way but has been zeroed
170 struct tty_struct
*alloc_tty_struct(void)
172 return kzalloc(sizeof(struct tty_struct
), GFP_KERNEL
);
176 * free_tty_struct - free a disused tty
177 * @tty: tty struct to free
179 * Free the write buffers, tty queue and tty memory itself.
181 * Locking: none. Must be called after tty is definitely unused
184 void free_tty_struct(struct tty_struct
*tty
)
186 kfree(tty
->write_buf
);
187 tty_buffer_free_all(tty
);
191 static inline struct tty_struct
*file_tty(struct file
*file
)
193 return ((struct tty_file_private
*)file
->private_data
)->tty
;
196 /* Associate a new file with the tty structure */
197 void tty_add_file(struct tty_struct
*tty
, struct file
*file
)
199 struct tty_file_private
*priv
;
201 /* XXX: must implement proper error handling in callers */
202 priv
= kmalloc(sizeof(*priv
), GFP_KERNEL
|__GFP_NOFAIL
);
206 file
->private_data
= priv
;
208 spin_lock(&tty_files_lock
);
209 list_add(&priv
->list
, &tty
->tty_files
);
210 spin_unlock(&tty_files_lock
);
213 /* Delete file from its tty */
214 void tty_del_file(struct file
*file
)
216 struct tty_file_private
*priv
= file
->private_data
;
218 spin_lock(&tty_files_lock
);
219 list_del(&priv
->list
);
220 spin_unlock(&tty_files_lock
);
221 file
->private_data
= NULL
;
226 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
229 * tty_name - return tty naming
230 * @tty: tty structure
231 * @buf: buffer for output
233 * Convert a tty structure into a name. The name reflects the kernel
234 * naming policy and if udev is in use may not reflect user space
239 char *tty_name(struct tty_struct
*tty
, char *buf
)
241 if (!tty
) /* Hmm. NULL pointer. That's fun. */
242 strcpy(buf
, "NULL tty");
244 strcpy(buf
, tty
->name
);
248 EXPORT_SYMBOL(tty_name
);
250 int tty_paranoia_check(struct tty_struct
*tty
, struct inode
*inode
,
253 #ifdef TTY_PARANOIA_CHECK
256 "null TTY for (%d:%d) in %s\n",
257 imajor(inode
), iminor(inode
), routine
);
260 if (tty
->magic
!= TTY_MAGIC
) {
262 "bad magic number for tty struct (%d:%d) in %s\n",
263 imajor(inode
), iminor(inode
), routine
);
270 static int check_tty_count(struct tty_struct
*tty
, const char *routine
)
272 #ifdef CHECK_TTY_COUNT
276 spin_lock(&tty_files_lock
);
277 list_for_each(p
, &tty
->tty_files
) {
280 spin_unlock(&tty_files_lock
);
281 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
282 tty
->driver
->subtype
== PTY_TYPE_SLAVE
&&
283 tty
->link
&& tty
->link
->count
)
285 if (tty
->count
!= count
) {
286 printk(KERN_WARNING
"Warning: dev (%s) tty->count(%d) "
287 "!= #fd's(%d) in %s\n",
288 tty
->name
, tty
->count
, count
, routine
);
296 * get_tty_driver - find device of a tty
297 * @dev_t: device identifier
298 * @index: returns the index of the tty
300 * This routine returns a tty driver structure, given a device number
301 * and also passes back the index number.
303 * Locking: caller must hold tty_mutex
306 static struct tty_driver
*get_tty_driver(dev_t device
, int *index
)
308 struct tty_driver
*p
;
310 list_for_each_entry(p
, &tty_drivers
, tty_drivers
) {
311 dev_t base
= MKDEV(p
->major
, p
->minor_start
);
312 if (device
< base
|| device
>= base
+ p
->num
)
314 *index
= device
- base
;
315 return tty_driver_kref_get(p
);
320 #ifdef CONFIG_CONSOLE_POLL
323 * tty_find_polling_driver - find device of a polled tty
324 * @name: name string to match
325 * @line: pointer to resulting tty line nr
327 * This routine returns a tty driver structure, given a name
328 * and the condition that the tty driver is capable of polled
331 struct tty_driver
*tty_find_polling_driver(char *name
, int *line
)
333 struct tty_driver
*p
, *res
= NULL
;
338 for (str
= name
; *str
; str
++)
339 if ((*str
>= '0' && *str
<= '9') || *str
== ',')
345 tty_line
= simple_strtoul(str
, &str
, 10);
347 mutex_lock(&tty_mutex
);
348 /* Search through the tty devices to look for a match */
349 list_for_each_entry(p
, &tty_drivers
, tty_drivers
) {
350 if (strncmp(name
, p
->name
, len
) != 0)
358 if (tty_line
>= 0 && tty_line
< p
->num
&& p
->ops
&&
359 p
->ops
->poll_init
&& !p
->ops
->poll_init(p
, tty_line
, stp
)) {
360 res
= tty_driver_kref_get(p
);
365 mutex_unlock(&tty_mutex
);
369 EXPORT_SYMBOL_GPL(tty_find_polling_driver
);
373 * tty_check_change - check for POSIX terminal changes
376 * If we try to write to, or set the state of, a terminal and we're
377 * not in the foreground, send a SIGTTOU. If the signal is blocked or
378 * ignored, go ahead and perform the operation. (POSIX 7.2)
383 int tty_check_change(struct tty_struct
*tty
)
388 if (current
->signal
->tty
!= tty
)
391 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
394 printk(KERN_WARNING
"tty_check_change: tty->pgrp == NULL!\n");
397 if (task_pgrp(current
) == tty
->pgrp
)
399 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
400 if (is_ignored(SIGTTOU
))
402 if (is_current_pgrp_orphaned()) {
406 kill_pgrp(task_pgrp(current
), SIGTTOU
, 1);
407 set_thread_flag(TIF_SIGPENDING
);
412 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
416 EXPORT_SYMBOL(tty_check_change
);
418 static ssize_t
hung_up_tty_read(struct file
*file
, char __user
*buf
,
419 size_t count
, loff_t
*ppos
)
424 static ssize_t
hung_up_tty_write(struct file
*file
, const char __user
*buf
,
425 size_t count
, loff_t
*ppos
)
430 /* No kernel lock held - none needed ;) */
431 static unsigned int hung_up_tty_poll(struct file
*filp
, poll_table
*wait
)
433 return POLLIN
| POLLOUT
| POLLERR
| POLLHUP
| POLLRDNORM
| POLLWRNORM
;
436 static long hung_up_tty_ioctl(struct file
*file
, unsigned int cmd
,
439 return cmd
== TIOCSPGRP
? -ENOTTY
: -EIO
;
442 static long hung_up_tty_compat_ioctl(struct file
*file
,
443 unsigned int cmd
, unsigned long arg
)
445 return cmd
== TIOCSPGRP
? -ENOTTY
: -EIO
;
448 static const struct file_operations tty_fops
= {
453 .unlocked_ioctl
= tty_ioctl
,
454 .compat_ioctl
= tty_compat_ioctl
,
456 .release
= tty_release
,
457 .fasync
= tty_fasync
,
460 static const struct file_operations console_fops
= {
463 .write
= redirected_tty_write
,
465 .unlocked_ioctl
= tty_ioctl
,
466 .compat_ioctl
= tty_compat_ioctl
,
468 .release
= tty_release
,
469 .fasync
= tty_fasync
,
472 static const struct file_operations hung_up_tty_fops
= {
474 .read
= hung_up_tty_read
,
475 .write
= hung_up_tty_write
,
476 .poll
= hung_up_tty_poll
,
477 .unlocked_ioctl
= hung_up_tty_ioctl
,
478 .compat_ioctl
= hung_up_tty_compat_ioctl
,
479 .release
= tty_release
,
482 static DEFINE_SPINLOCK(redirect_lock
);
483 static struct file
*redirect
;
486 * tty_wakeup - request more data
489 * Internal and external helper for wakeups of tty. This function
490 * informs the line discipline if present that the driver is ready
491 * to receive more output data.
494 void tty_wakeup(struct tty_struct
*tty
)
496 struct tty_ldisc
*ld
;
498 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) {
499 ld
= tty_ldisc_ref(tty
);
501 if (ld
->ops
->write_wakeup
)
502 ld
->ops
->write_wakeup(tty
);
506 wake_up_interruptible_poll(&tty
->write_wait
, POLLOUT
);
509 EXPORT_SYMBOL_GPL(tty_wakeup
);
512 * __tty_hangup - actual handler for hangup events
515 * This can be called by the "eventd" kernel thread. That is process
516 * synchronous but doesn't hold any locks, so we need to make sure we
517 * have the appropriate locks for what we're doing.
519 * The hangup event clears any pending redirections onto the hung up
520 * device. It ensures future writes will error and it does the needed
521 * line discipline hangup and signal delivery. The tty object itself
526 * redirect lock for undoing redirection
527 * file list lock for manipulating list of ttys
528 * tty_ldisc_lock from called functions
529 * termios_mutex resetting termios data
530 * tasklist_lock to walk task list for hangup event
531 * ->siglock to protect ->signal/->sighand
533 void __tty_hangup(struct tty_struct
*tty
)
535 struct file
*cons_filp
= NULL
;
536 struct file
*filp
, *f
= NULL
;
537 struct task_struct
*p
;
538 struct tty_file_private
*priv
;
539 int closecount
= 0, n
;
547 spin_lock(&redirect_lock
);
548 if (redirect
&& file_tty(redirect
) == tty
) {
552 spin_unlock(&redirect_lock
);
556 /* inuse_filps is protected by the single tty lock,
557 this really needs to change if we want to flush the
558 workqueue with the lock held */
559 check_tty_count(tty
, "tty_hangup");
561 spin_lock(&tty_files_lock
);
562 /* This breaks for file handles being sent over AF_UNIX sockets ? */
563 list_for_each_entry(priv
, &tty
->tty_files
, list
) {
565 if (filp
->f_op
->write
== redirected_tty_write
)
567 if (filp
->f_op
->write
!= tty_write
)
570 __tty_fasync(-1, filp
, 0); /* can't block */
571 filp
->f_op
= &hung_up_tty_fops
;
573 spin_unlock(&tty_files_lock
);
575 tty_ldisc_hangup(tty
);
577 read_lock(&tasklist_lock
);
579 do_each_pid_task(tty
->session
, PIDTYPE_SID
, p
) {
580 spin_lock_irq(&p
->sighand
->siglock
);
581 if (p
->signal
->tty
== tty
) {
582 p
->signal
->tty
= NULL
;
583 /* We defer the dereferences outside fo
587 if (!p
->signal
->leader
) {
588 spin_unlock_irq(&p
->sighand
->siglock
);
591 __group_send_sig_info(SIGHUP
, SEND_SIG_PRIV
, p
);
592 __group_send_sig_info(SIGCONT
, SEND_SIG_PRIV
, p
);
593 put_pid(p
->signal
->tty_old_pgrp
); /* A noop */
594 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
596 p
->signal
->tty_old_pgrp
= get_pid(tty
->pgrp
);
597 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
598 spin_unlock_irq(&p
->sighand
->siglock
);
599 } while_each_pid_task(tty
->session
, PIDTYPE_SID
, p
);
601 read_unlock(&tasklist_lock
);
603 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
604 clear_bit(TTY_THROTTLED
, &tty
->flags
);
605 clear_bit(TTY_PUSH
, &tty
->flags
);
606 clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
607 put_pid(tty
->session
);
611 tty
->ctrl_status
= 0;
612 set_bit(TTY_HUPPED
, &tty
->flags
);
613 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
615 /* Account for the p->signal references we killed */
620 * If one of the devices matches a console pointer, we
621 * cannot just call hangup() because that will cause
622 * tty->count and state->count to go out of sync.
623 * So we just call close() the right number of times.
627 for (n
= 0; n
< closecount
; n
++)
628 tty
->ops
->close(tty
, cons_filp
);
629 } else if (tty
->ops
->hangup
)
630 (tty
->ops
->hangup
)(tty
);
632 * We don't want to have driver/ldisc interactions beyond
633 * the ones we did here. The driver layer expects no
634 * calls after ->hangup() from the ldisc side. However we
635 * can't yet guarantee all that.
637 set_bit(TTY_HUPPED
, &tty
->flags
);
638 tty_ldisc_enable(tty
);
646 static void do_tty_hangup(struct work_struct
*work
)
648 struct tty_struct
*tty
=
649 container_of(work
, struct tty_struct
, hangup_work
);
655 * tty_hangup - trigger a hangup event
656 * @tty: tty to hangup
658 * A carrier loss (virtual or otherwise) has occurred on this like
659 * schedule a hangup sequence to run after this event.
662 void tty_hangup(struct tty_struct
*tty
)
664 #ifdef TTY_DEBUG_HANGUP
666 printk(KERN_DEBUG
"%s hangup...\n", tty_name(tty
, buf
));
668 schedule_work(&tty
->hangup_work
);
671 EXPORT_SYMBOL(tty_hangup
);
674 * tty_vhangup - process vhangup
675 * @tty: tty to hangup
677 * The user has asked via system call for the terminal to be hung up.
678 * We do this synchronously so that when the syscall returns the process
679 * is complete. That guarantee is necessary for security reasons.
682 void tty_vhangup(struct tty_struct
*tty
)
684 #ifdef TTY_DEBUG_HANGUP
687 printk(KERN_DEBUG
"%s vhangup...\n", tty_name(tty
, buf
));
692 EXPORT_SYMBOL(tty_vhangup
);
696 * tty_vhangup_self - process vhangup for own ctty
698 * Perform a vhangup on the current controlling tty
701 void tty_vhangup_self(void)
703 struct tty_struct
*tty
;
705 tty
= get_current_tty();
713 * tty_hung_up_p - was tty hung up
714 * @filp: file pointer of tty
716 * Return true if the tty has been subject to a vhangup or a carrier
720 int tty_hung_up_p(struct file
*filp
)
722 return (filp
->f_op
== &hung_up_tty_fops
);
725 EXPORT_SYMBOL(tty_hung_up_p
);
727 static void session_clear_tty(struct pid
*session
)
729 struct task_struct
*p
;
730 do_each_pid_task(session
, PIDTYPE_SID
, p
) {
732 } while_each_pid_task(session
, PIDTYPE_SID
, p
);
736 * disassociate_ctty - disconnect controlling tty
737 * @on_exit: true if exiting so need to "hang up" the session
739 * This function is typically called only by the session leader, when
740 * it wants to disassociate itself from its controlling tty.
742 * It performs the following functions:
743 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
744 * (2) Clears the tty from being controlling the session
745 * (3) Clears the controlling tty for all processes in the
748 * The argument on_exit is set to 1 if called when a process is
749 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
752 * BTM is taken for hysterical raisins, and held when
753 * called from no_tty().
754 * tty_mutex is taken to protect tty
755 * ->siglock is taken to protect ->signal/->sighand
756 * tasklist_lock is taken to walk process list for sessions
757 * ->siglock is taken to protect ->signal/->sighand
760 void disassociate_ctty(int on_exit
)
762 struct tty_struct
*tty
;
763 struct pid
*tty_pgrp
= NULL
;
765 if (!current
->signal
->leader
)
768 tty
= get_current_tty();
770 tty_pgrp
= get_pid(tty
->pgrp
);
772 if (tty
->driver
->type
!= TTY_DRIVER_TYPE_PTY
)
776 } else if (on_exit
) {
777 struct pid
*old_pgrp
;
778 spin_lock_irq(¤t
->sighand
->siglock
);
779 old_pgrp
= current
->signal
->tty_old_pgrp
;
780 current
->signal
->tty_old_pgrp
= NULL
;
781 spin_unlock_irq(¤t
->sighand
->siglock
);
783 kill_pgrp(old_pgrp
, SIGHUP
, on_exit
);
784 kill_pgrp(old_pgrp
, SIGCONT
, on_exit
);
790 kill_pgrp(tty_pgrp
, SIGHUP
, on_exit
);
792 kill_pgrp(tty_pgrp
, SIGCONT
, on_exit
);
796 spin_lock_irq(¤t
->sighand
->siglock
);
797 put_pid(current
->signal
->tty_old_pgrp
);
798 current
->signal
->tty_old_pgrp
= NULL
;
799 spin_unlock_irq(¤t
->sighand
->siglock
);
801 tty
= get_current_tty();
804 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
805 put_pid(tty
->session
);
809 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
812 #ifdef TTY_DEBUG_HANGUP
813 printk(KERN_DEBUG
"error attempted to write to tty [0x%p]"
818 /* Now clear signal->tty under the lock */
819 read_lock(&tasklist_lock
);
820 session_clear_tty(task_session(current
));
821 read_unlock(&tasklist_lock
);
826 * no_tty - Ensure the current process does not have a controlling tty
830 struct task_struct
*tsk
= current
;
832 disassociate_ctty(0);
839 * stop_tty - propagate flow control
842 * Perform flow control to the driver. For PTY/TTY pairs we
843 * must also propagate the TIOCKPKT status. May be called
844 * on an already stopped device and will not re-call the driver
847 * This functionality is used by both the line disciplines for
848 * halting incoming flow and by the driver. It may therefore be
849 * called from any context, may be under the tty atomic_write_lock
853 * Uses the tty control lock internally
856 void stop_tty(struct tty_struct
*tty
)
859 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
861 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
865 if (tty
->link
&& tty
->link
->packet
) {
866 tty
->ctrl_status
&= ~TIOCPKT_START
;
867 tty
->ctrl_status
|= TIOCPKT_STOP
;
868 wake_up_interruptible_poll(&tty
->link
->read_wait
, POLLIN
);
870 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
872 (tty
->ops
->stop
)(tty
);
875 EXPORT_SYMBOL(stop_tty
);
878 * start_tty - propagate flow control
881 * Start a tty that has been stopped if at all possible. Perform
882 * any necessary wakeups and propagate the TIOCPKT status. If this
883 * is the tty was previous stopped and is being started then the
884 * driver start method is invoked and the line discipline woken.
890 void start_tty(struct tty_struct
*tty
)
893 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
894 if (!tty
->stopped
|| tty
->flow_stopped
) {
895 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
899 if (tty
->link
&& tty
->link
->packet
) {
900 tty
->ctrl_status
&= ~TIOCPKT_STOP
;
901 tty
->ctrl_status
|= TIOCPKT_START
;
902 wake_up_interruptible_poll(&tty
->link
->read_wait
, POLLIN
);
904 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
906 (tty
->ops
->start
)(tty
);
907 /* If we have a running line discipline it may need kicking */
911 EXPORT_SYMBOL(start_tty
);
914 * tty_read - read method for tty device files
915 * @file: pointer to tty file
917 * @count: size of user buffer
920 * Perform the read system call function on this terminal device. Checks
921 * for hung up devices before calling the line discipline method.
924 * Locks the line discipline internally while needed. Multiple
925 * read calls may be outstanding in parallel.
928 static ssize_t
tty_read(struct file
*file
, char __user
*buf
, size_t count
,
932 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
933 struct tty_struct
*tty
= file_tty(file
);
934 struct tty_ldisc
*ld
;
936 if (tty_paranoia_check(tty
, inode
, "tty_read"))
938 if (!tty
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
941 /* We want to wait for the line discipline to sort out in this
943 ld
= tty_ldisc_ref_wait(tty
);
945 i
= (ld
->ops
->read
)(tty
, file
, buf
, count
);
950 inode
->i_atime
= current_fs_time(inode
->i_sb
);
954 void tty_write_unlock(struct tty_struct
*tty
)
956 mutex_unlock(&tty
->atomic_write_lock
);
957 wake_up_interruptible_poll(&tty
->write_wait
, POLLOUT
);
960 int tty_write_lock(struct tty_struct
*tty
, int ndelay
)
962 if (!mutex_trylock(&tty
->atomic_write_lock
)) {
965 if (mutex_lock_interruptible(&tty
->atomic_write_lock
))
972 * Split writes up in sane blocksizes to avoid
973 * denial-of-service type attacks
975 static inline ssize_t
do_tty_write(
976 ssize_t (*write
)(struct tty_struct
*, struct file
*, const unsigned char *, size_t),
977 struct tty_struct
*tty
,
979 const char __user
*buf
,
982 ssize_t ret
, written
= 0;
985 ret
= tty_write_lock(tty
, file
->f_flags
& O_NDELAY
);
990 * We chunk up writes into a temporary buffer. This
991 * simplifies low-level drivers immensely, since they
992 * don't have locking issues and user mode accesses.
994 * But if TTY_NO_WRITE_SPLIT is set, we should use a
997 * The default chunk-size is 2kB, because the NTTY
998 * layer has problems with bigger chunks. It will
999 * claim to be able to handle more characters than
1002 * FIXME: This can probably go away now except that 64K chunks
1003 * are too likely to fail unless switched to vmalloc...
1006 if (test_bit(TTY_NO_WRITE_SPLIT
, &tty
->flags
))
1011 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1012 if (tty
->write_cnt
< chunk
) {
1013 unsigned char *buf_chunk
;
1018 buf_chunk
= kmalloc(chunk
, GFP_KERNEL
);
1023 kfree(tty
->write_buf
);
1024 tty
->write_cnt
= chunk
;
1025 tty
->write_buf
= buf_chunk
;
1028 /* Do the write .. */
1030 size_t size
= count
;
1034 if (copy_from_user(tty
->write_buf
, buf
, size
))
1036 ret
= write(tty
, file
, tty
->write_buf
, size
);
1045 if (signal_pending(current
))
1050 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1051 inode
->i_mtime
= current_fs_time(inode
->i_sb
);
1055 tty_write_unlock(tty
);
1060 * tty_write_message - write a message to a certain tty, not just the console.
1061 * @tty: the destination tty_struct
1062 * @msg: the message to write
1064 * This is used for messages that need to be redirected to a specific tty.
1065 * We don't put it into the syslog queue right now maybe in the future if
1068 * We must still hold the BTM and test the CLOSING flag for the moment.
1071 void tty_write_message(struct tty_struct
*tty
, char *msg
)
1074 mutex_lock(&tty
->atomic_write_lock
);
1076 if (tty
->ops
->write
&& !test_bit(TTY_CLOSING
, &tty
->flags
)) {
1078 tty
->ops
->write(tty
, msg
, strlen(msg
));
1081 tty_write_unlock(tty
);
1088 * tty_write - write method for tty device file
1089 * @file: tty file pointer
1090 * @buf: user data to write
1091 * @count: bytes to write
1094 * Write data to a tty device via the line discipline.
1097 * Locks the line discipline as required
1098 * Writes to the tty driver are serialized by the atomic_write_lock
1099 * and are then processed in chunks to the device. The line discipline
1100 * write method will not be invoked in parallel for each device.
1103 static ssize_t
tty_write(struct file
*file
, const char __user
*buf
,
1104 size_t count
, loff_t
*ppos
)
1106 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1107 struct tty_struct
*tty
= file_tty(file
);
1108 struct tty_ldisc
*ld
;
1111 if (tty_paranoia_check(tty
, inode
, "tty_write"))
1113 if (!tty
|| !tty
->ops
->write
||
1114 (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
1116 /* Short term debug to catch buggy drivers */
1117 if (tty
->ops
->write_room
== NULL
)
1118 printk(KERN_ERR
"tty driver %s lacks a write_room method.\n",
1120 ld
= tty_ldisc_ref_wait(tty
);
1121 if (!ld
->ops
->write
)
1124 ret
= do_tty_write(ld
->ops
->write
, tty
, file
, buf
, count
);
1125 tty_ldisc_deref(ld
);
1129 ssize_t
redirected_tty_write(struct file
*file
, const char __user
*buf
,
1130 size_t count
, loff_t
*ppos
)
1132 struct file
*p
= NULL
;
1134 spin_lock(&redirect_lock
);
1139 spin_unlock(&redirect_lock
);
1143 res
= vfs_write(p
, buf
, count
, &p
->f_pos
);
1147 return tty_write(file
, buf
, count
, ppos
);
1150 static char ptychar
[] = "pqrstuvwxyzabcde";
1153 * pty_line_name - generate name for a pty
1154 * @driver: the tty driver in use
1155 * @index: the minor number
1156 * @p: output buffer of at least 6 bytes
1158 * Generate a name from a driver reference and write it to the output
1163 static void pty_line_name(struct tty_driver
*driver
, int index
, char *p
)
1165 int i
= index
+ driver
->name_base
;
1166 /* ->name is initialized to "ttyp", but "tty" is expected */
1167 sprintf(p
, "%s%c%x",
1168 driver
->subtype
== PTY_TYPE_SLAVE
? "tty" : driver
->name
,
1169 ptychar
[i
>> 4 & 0xf], i
& 0xf);
1173 * tty_line_name - generate name for a tty
1174 * @driver: the tty driver in use
1175 * @index: the minor number
1176 * @p: output buffer of at least 7 bytes
1178 * Generate a name from a driver reference and write it to the output
1183 static void tty_line_name(struct tty_driver
*driver
, int index
, char *p
)
1185 sprintf(p
, "%s%d", driver
->name
, index
+ driver
->name_base
);
1189 * tty_driver_lookup_tty() - find an existing tty, if any
1190 * @driver: the driver for the tty
1191 * @idx: the minor number
1193 * Return the tty, if found or ERR_PTR() otherwise.
1195 * Locking: tty_mutex must be held. If tty is found, the mutex must
1196 * be held until the 'fast-open' is also done. Will change once we
1197 * have refcounting in the driver and per driver locking
1199 static struct tty_struct
*tty_driver_lookup_tty(struct tty_driver
*driver
,
1200 struct inode
*inode
, int idx
)
1202 struct tty_struct
*tty
;
1204 if (driver
->ops
->lookup
)
1205 return driver
->ops
->lookup(driver
, inode
, idx
);
1207 tty
= driver
->ttys
[idx
];
1212 * tty_init_termios - helper for termios setup
1213 * @tty: the tty to set up
1215 * Initialise the termios structures for this tty. Thus runs under
1216 * the tty_mutex currently so we can be relaxed about ordering.
1219 int tty_init_termios(struct tty_struct
*tty
)
1221 struct ktermios
*tp
;
1222 int idx
= tty
->index
;
1224 tp
= tty
->driver
->termios
[idx
];
1226 tp
= kzalloc(sizeof(struct ktermios
[2]), GFP_KERNEL
);
1229 memcpy(tp
, &tty
->driver
->init_termios
,
1230 sizeof(struct ktermios
));
1231 tty
->driver
->termios
[idx
] = tp
;
1234 tty
->termios_locked
= tp
+ 1;
1236 /* Compatibility until drivers always set this */
1237 tty
->termios
->c_ispeed
= tty_termios_input_baud_rate(tty
->termios
);
1238 tty
->termios
->c_ospeed
= tty_termios_baud_rate(tty
->termios
);
1241 EXPORT_SYMBOL_GPL(tty_init_termios
);
1244 * tty_driver_install_tty() - install a tty entry in the driver
1245 * @driver: the driver for the tty
1248 * Install a tty object into the driver tables. The tty->index field
1249 * will be set by the time this is called. This method is responsible
1250 * for ensuring any need additional structures are allocated and
1253 * Locking: tty_mutex for now
1255 static int tty_driver_install_tty(struct tty_driver
*driver
,
1256 struct tty_struct
*tty
)
1258 int idx
= tty
->index
;
1261 if (driver
->ops
->install
) {
1262 ret
= driver
->ops
->install(driver
, tty
);
1266 if (tty_init_termios(tty
) == 0) {
1267 tty_driver_kref_get(driver
);
1269 driver
->ttys
[idx
] = tty
;
1276 * tty_driver_remove_tty() - remove a tty from the driver tables
1277 * @driver: the driver for the tty
1278 * @idx: the minor number
1280 * Remvoe a tty object from the driver tables. The tty->index field
1281 * will be set by the time this is called.
1283 * Locking: tty_mutex for now
1285 static void tty_driver_remove_tty(struct tty_driver
*driver
,
1286 struct tty_struct
*tty
)
1288 if (driver
->ops
->remove
)
1289 driver
->ops
->remove(driver
, tty
);
1291 driver
->ttys
[tty
->index
] = NULL
;
1295 * tty_reopen() - fast re-open of an open tty
1296 * @tty - the tty to open
1298 * Return 0 on success, -errno on error.
1300 * Locking: tty_mutex must be held from the time the tty was found
1301 * till this open completes.
1303 static int tty_reopen(struct tty_struct
*tty
)
1305 struct tty_driver
*driver
= tty
->driver
;
1307 if (test_bit(TTY_CLOSING
, &tty
->flags
))
1310 if (driver
->type
== TTY_DRIVER_TYPE_PTY
&&
1311 driver
->subtype
== PTY_TYPE_MASTER
) {
1313 * special case for PTY masters: only one open permitted,
1314 * and the slave side open count is incremented as well.
1322 tty
->driver
= driver
; /* N.B. why do this every time?? */
1324 mutex_lock(&tty
->ldisc_mutex
);
1325 WARN_ON(!test_bit(TTY_LDISC
, &tty
->flags
));
1326 mutex_unlock(&tty
->ldisc_mutex
);
1332 * tty_init_dev - initialise a tty device
1333 * @driver: tty driver we are opening a device on
1334 * @idx: device index
1335 * @ret_tty: returned tty structure
1336 * @first_ok: ok to open a new device (used by ptmx)
1338 * Prepare a tty device. This may not be a "new" clean device but
1339 * could also be an active device. The pty drivers require special
1340 * handling because of this.
1343 * The function is called under the tty_mutex, which
1344 * protects us from the tty struct or driver itself going away.
1346 * On exit the tty device has the line discipline attached and
1347 * a reference count of 1. If a pair was created for pty/tty use
1348 * and the other was a pty master then it too has a reference count of 1.
1350 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1351 * failed open. The new code protects the open with a mutex, so it's
1352 * really quite straightforward. The mutex locking can probably be
1353 * relaxed for the (most common) case of reopening a tty.
1356 struct tty_struct
*tty_init_dev(struct tty_driver
*driver
, int idx
,
1359 struct tty_struct
*tty
;
1362 /* Check if pty master is being opened multiple times */
1363 if (driver
->subtype
== PTY_TYPE_MASTER
&&
1364 (driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) && !first_ok
) {
1365 return ERR_PTR(-EIO
);
1369 * First time open is complex, especially for PTY devices.
1370 * This code guarantees that either everything succeeds and the
1371 * TTY is ready for operation, or else the table slots are vacated
1372 * and the allocated memory released. (Except that the termios
1373 * and locked termios may be retained.)
1376 if (!try_module_get(driver
->owner
))
1377 return ERR_PTR(-ENODEV
);
1379 tty
= alloc_tty_struct();
1382 initialize_tty_struct(tty
, driver
, idx
);
1384 retval
= tty_driver_install_tty(driver
, tty
);
1386 free_tty_struct(tty
);
1387 module_put(driver
->owner
);
1388 return ERR_PTR(retval
);
1392 * Structures all installed ... call the ldisc open routines.
1393 * If we fail here just call release_tty to clean up. No need
1394 * to decrement the use counts, as release_tty doesn't care.
1396 retval
= tty_ldisc_setup(tty
, tty
->link
);
1398 goto release_mem_out
;
1402 module_put(driver
->owner
);
1403 return ERR_PTR(-ENOMEM
);
1405 /* call the tty release_tty routine to clean out this slot */
1407 if (printk_ratelimit())
1408 printk(KERN_INFO
"tty_init_dev: ldisc open failed, "
1409 "clearing slot %d\n", idx
);
1410 release_tty(tty
, idx
);
1411 return ERR_PTR(retval
);
1414 void tty_free_termios(struct tty_struct
*tty
)
1416 struct ktermios
*tp
;
1417 int idx
= tty
->index
;
1418 /* Kill this flag and push into drivers for locking etc */
1419 if (tty
->driver
->flags
& TTY_DRIVER_RESET_TERMIOS
) {
1420 /* FIXME: Locking on ->termios array */
1422 tty
->driver
->termios
[idx
] = NULL
;
1426 EXPORT_SYMBOL(tty_free_termios
);
1428 void tty_shutdown(struct tty_struct
*tty
)
1430 tty_driver_remove_tty(tty
->driver
, tty
);
1431 tty_free_termios(tty
);
1433 EXPORT_SYMBOL(tty_shutdown
);
1436 * release_one_tty - release tty structure memory
1437 * @kref: kref of tty we are obliterating
1439 * Releases memory associated with a tty structure, and clears out the
1440 * driver table slots. This function is called when a device is no longer
1441 * in use. It also gets called when setup of a device fails.
1444 * tty_mutex - sometimes only
1445 * takes the file list lock internally when working on the list
1446 * of ttys that the driver keeps.
1448 * This method gets called from a work queue so that the driver private
1449 * cleanup ops can sleep (needed for USB at least)
1451 static void release_one_tty(struct work_struct
*work
)
1453 struct tty_struct
*tty
=
1454 container_of(work
, struct tty_struct
, hangup_work
);
1455 struct tty_driver
*driver
= tty
->driver
;
1457 if (tty
->ops
->cleanup
)
1458 tty
->ops
->cleanup(tty
);
1461 tty_driver_kref_put(driver
);
1462 module_put(driver
->owner
);
1464 spin_lock(&tty_files_lock
);
1465 list_del_init(&tty
->tty_files
);
1466 spin_unlock(&tty_files_lock
);
1469 put_pid(tty
->session
);
1470 free_tty_struct(tty
);
1473 static void queue_release_one_tty(struct kref
*kref
)
1475 struct tty_struct
*tty
= container_of(kref
, struct tty_struct
, kref
);
1477 if (tty
->ops
->shutdown
)
1478 tty
->ops
->shutdown(tty
);
1482 /* The hangup queue is now free so we can reuse it rather than
1483 waste a chunk of memory for each port */
1484 INIT_WORK(&tty
->hangup_work
, release_one_tty
);
1485 schedule_work(&tty
->hangup_work
);
1489 * tty_kref_put - release a tty kref
1492 * Release a reference to a tty device and if need be let the kref
1493 * layer destruct the object for us
1496 void tty_kref_put(struct tty_struct
*tty
)
1499 kref_put(&tty
->kref
, queue_release_one_tty
);
1501 EXPORT_SYMBOL(tty_kref_put
);
1504 * release_tty - release tty structure memory
1506 * Release both @tty and a possible linked partner (think pty pair),
1507 * and decrement the refcount of the backing module.
1510 * tty_mutex - sometimes only
1511 * takes the file list lock internally when working on the list
1512 * of ttys that the driver keeps.
1513 * FIXME: should we require tty_mutex is held here ??
1516 static void release_tty(struct tty_struct
*tty
, int idx
)
1518 /* This should always be true but check for the moment */
1519 WARN_ON(tty
->index
!= idx
);
1522 tty_kref_put(tty
->link
);
1527 * tty_release - vfs callback for close
1528 * @inode: inode of tty
1529 * @filp: file pointer for handle to tty
1531 * Called the last time each file handle is closed that references
1532 * this tty. There may however be several such references.
1535 * Takes bkl. See tty_release_dev
1537 * Even releasing the tty structures is a tricky business.. We have
1538 * to be very careful that the structures are all released at the
1539 * same time, as interrupts might otherwise get the wrong pointers.
1541 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1542 * lead to double frees or releasing memory still in use.
1545 int tty_release(struct inode
*inode
, struct file
*filp
)
1547 struct tty_struct
*tty
= file_tty(filp
);
1548 struct tty_struct
*o_tty
;
1549 int pty_master
, tty_closing
, o_tty_closing
, do_sleep
;
1554 if (tty_paranoia_check(tty
, inode
, "tty_release_dev"))
1558 check_tty_count(tty
, "tty_release_dev");
1560 __tty_fasync(-1, filp
, 0);
1563 pty_master
= (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
1564 tty
->driver
->subtype
== PTY_TYPE_MASTER
);
1565 devpts
= (tty
->driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) != 0;
1568 #ifdef TTY_PARANOIA_CHECK
1569 if (idx
< 0 || idx
>= tty
->driver
->num
) {
1570 printk(KERN_DEBUG
"tty_release_dev: bad idx when trying to "
1571 "free (%s)\n", tty
->name
);
1576 if (tty
!= tty
->driver
->ttys
[idx
]) {
1578 printk(KERN_DEBUG
"tty_release_dev: driver.table[%d] not tty "
1579 "for (%s)\n", idx
, tty
->name
);
1582 if (tty
->termios
!= tty
->driver
->termios
[idx
]) {
1584 printk(KERN_DEBUG
"tty_release_dev: driver.termios[%d] not termios "
1592 #ifdef TTY_DEBUG_HANGUP
1593 printk(KERN_DEBUG
"tty_release_dev of %s (tty count=%d)...",
1594 tty_name(tty
, buf
), tty
->count
);
1597 #ifdef TTY_PARANOIA_CHECK
1598 if (tty
->driver
->other
&&
1599 !(tty
->driver
->flags
& TTY_DRIVER_DEVPTS_MEM
)) {
1600 if (o_tty
!= tty
->driver
->other
->ttys
[idx
]) {
1602 printk(KERN_DEBUG
"tty_release_dev: other->table[%d] "
1603 "not o_tty for (%s)\n",
1607 if (o_tty
->termios
!= tty
->driver
->other
->termios
[idx
]) {
1609 printk(KERN_DEBUG
"tty_release_dev: other->termios[%d] "
1610 "not o_termios for (%s)\n",
1614 if (o_tty
->link
!= tty
) {
1616 printk(KERN_DEBUG
"tty_release_dev: bad pty pointers\n");
1621 if (tty
->ops
->close
)
1622 tty
->ops
->close(tty
, filp
);
1626 * Sanity check: if tty->count is going to zero, there shouldn't be
1627 * any waiters on tty->read_wait or tty->write_wait. We test the
1628 * wait queues and kick everyone out _before_ actually starting to
1629 * close. This ensures that we won't block while releasing the tty
1632 * The test for the o_tty closing is necessary, since the master and
1633 * slave sides may close in any order. If the slave side closes out
1634 * first, its count will be one, since the master side holds an open.
1635 * Thus this test wouldn't be triggered at the time the slave closes,
1638 * Note that it's possible for the tty to be opened again while we're
1639 * flushing out waiters. By recalculating the closing flags before
1640 * each iteration we avoid any problems.
1643 /* Guard against races with tty->count changes elsewhere and
1644 opens on /dev/tty */
1646 mutex_lock(&tty_mutex
);
1648 tty_closing
= tty
->count
<= 1;
1649 o_tty_closing
= o_tty
&&
1650 (o_tty
->count
<= (pty_master
? 1 : 0));
1654 if (waitqueue_active(&tty
->read_wait
)) {
1655 wake_up_poll(&tty
->read_wait
, POLLIN
);
1658 if (waitqueue_active(&tty
->write_wait
)) {
1659 wake_up_poll(&tty
->write_wait
, POLLOUT
);
1663 if (o_tty_closing
) {
1664 if (waitqueue_active(&o_tty
->read_wait
)) {
1665 wake_up_poll(&o_tty
->read_wait
, POLLIN
);
1668 if (waitqueue_active(&o_tty
->write_wait
)) {
1669 wake_up_poll(&o_tty
->write_wait
, POLLOUT
);
1676 printk(KERN_WARNING
"tty_release_dev: %s: read/write wait queue "
1677 "active!\n", tty_name(tty
, buf
));
1679 mutex_unlock(&tty_mutex
);
1684 * The closing flags are now consistent with the open counts on
1685 * both sides, and we've completed the last operation that could
1686 * block, so it's safe to proceed with closing.
1689 if (--o_tty
->count
< 0) {
1690 printk(KERN_WARNING
"tty_release_dev: bad pty slave count "
1692 o_tty
->count
, tty_name(o_tty
, buf
));
1696 if (--tty
->count
< 0) {
1697 printk(KERN_WARNING
"tty_release_dev: bad tty->count (%d) for %s\n",
1698 tty
->count
, tty_name(tty
, buf
));
1703 * We've decremented tty->count, so we need to remove this file
1704 * descriptor off the tty->tty_files list; this serves two
1706 * - check_tty_count sees the correct number of file descriptors
1707 * associated with this tty.
1708 * - do_tty_hangup no longer sees this file descriptor as
1709 * something that needs to be handled for hangups.
1714 * Perform some housekeeping before deciding whether to return.
1716 * Set the TTY_CLOSING flag if this was the last open. In the
1717 * case of a pty we may have to wait around for the other side
1718 * to close, and TTY_CLOSING makes sure we can't be reopened.
1721 set_bit(TTY_CLOSING
, &tty
->flags
);
1723 set_bit(TTY_CLOSING
, &o_tty
->flags
);
1726 * If _either_ side is closing, make sure there aren't any
1727 * processes that still think tty or o_tty is their controlling
1730 if (tty_closing
|| o_tty_closing
) {
1731 read_lock(&tasklist_lock
);
1732 session_clear_tty(tty
->session
);
1734 session_clear_tty(o_tty
->session
);
1735 read_unlock(&tasklist_lock
);
1738 mutex_unlock(&tty_mutex
);
1740 /* check whether both sides are closing ... */
1741 if (!tty_closing
|| (o_tty
&& !o_tty_closing
)) {
1746 #ifdef TTY_DEBUG_HANGUP
1747 printk(KERN_DEBUG
"freeing tty structure...");
1750 * Ask the line discipline code to release its structures
1752 tty_ldisc_release(tty
, o_tty
);
1754 * The release_tty function takes care of the details of clearing
1755 * the slots and preserving the termios structure.
1757 release_tty(tty
, idx
);
1759 /* Make this pty number available for reallocation */
1761 devpts_kill_index(inode
, idx
);
1767 * tty_open - open a tty device
1768 * @inode: inode of device file
1769 * @filp: file pointer to tty
1771 * tty_open and tty_release keep up the tty count that contains the
1772 * number of opens done on a tty. We cannot use the inode-count, as
1773 * different inodes might point to the same tty.
1775 * Open-counting is needed for pty masters, as well as for keeping
1776 * track of serial lines: DTR is dropped when the last close happens.
1777 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1779 * The termios state of a pty is reset on first open so that
1780 * settings don't persist across reuse.
1782 * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
1783 * tty->count should protect the rest.
1784 * ->siglock protects ->signal/->sighand
1787 static int tty_open(struct inode
*inode
, struct file
*filp
)
1789 struct tty_struct
*tty
= NULL
;
1791 struct tty_driver
*driver
;
1793 dev_t device
= inode
->i_rdev
;
1794 unsigned saved_flags
= filp
->f_flags
;
1796 nonseekable_open(inode
, filp
);
1799 noctty
= filp
->f_flags
& O_NOCTTY
;
1803 mutex_lock(&tty_mutex
);
1806 if (device
== MKDEV(TTYAUX_MAJOR
, 0)) {
1807 tty
= get_current_tty();
1810 mutex_unlock(&tty_mutex
);
1813 driver
= tty_driver_kref_get(tty
->driver
);
1815 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/tty block */
1817 /* FIXME: Should we take a driver reference ? */
1822 if (device
== MKDEV(TTY_MAJOR
, 0)) {
1823 extern struct tty_driver
*console_driver
;
1824 driver
= tty_driver_kref_get(console_driver
);
1830 if (device
== MKDEV(TTYAUX_MAJOR
, 1)) {
1831 struct tty_driver
*console_driver
= console_device(&index
);
1832 if (console_driver
) {
1833 driver
= tty_driver_kref_get(console_driver
);
1835 /* Don't let /dev/console block */
1836 filp
->f_flags
|= O_NONBLOCK
;
1842 mutex_unlock(&tty_mutex
);
1846 driver
= get_tty_driver(device
, &index
);
1849 mutex_unlock(&tty_mutex
);
1854 /* check whether we're reopening an existing tty */
1855 tty
= tty_driver_lookup_tty(driver
, inode
, index
);
1859 mutex_unlock(&tty_mutex
);
1860 return PTR_ERR(tty
);
1865 retval
= tty_reopen(tty
);
1867 tty
= ERR_PTR(retval
);
1869 tty
= tty_init_dev(driver
, index
, 0);
1871 mutex_unlock(&tty_mutex
);
1872 tty_driver_kref_put(driver
);
1875 return PTR_ERR(tty
);
1878 tty_add_file(tty
, filp
);
1880 check_tty_count(tty
, "tty_open");
1881 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
1882 tty
->driver
->subtype
== PTY_TYPE_MASTER
)
1884 #ifdef TTY_DEBUG_HANGUP
1885 printk(KERN_DEBUG
"opening %s...", tty
->name
);
1889 retval
= tty
->ops
->open(tty
, filp
);
1893 filp
->f_flags
= saved_flags
;
1895 if (!retval
&& test_bit(TTY_EXCLUSIVE
, &tty
->flags
) &&
1896 !capable(CAP_SYS_ADMIN
))
1900 #ifdef TTY_DEBUG_HANGUP
1901 printk(KERN_DEBUG
"error %d in opening %s...", retval
,
1904 tty_unlock(); /* need to call tty_release without BTM */
1905 tty_release(inode
, filp
);
1906 if (retval
!= -ERESTARTSYS
)
1909 if (signal_pending(current
))
1914 * 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
;
1925 mutex_lock(&tty_mutex
);
1927 spin_lock_irq(¤t
->sighand
->siglock
);
1929 current
->signal
->leader
&&
1930 !current
->signal
->tty
&&
1931 tty
->session
== NULL
)
1932 __proc_set_tty(current
, tty
);
1933 spin_unlock_irq(¤t
->sighand
->siglock
);
1935 mutex_unlock(&tty_mutex
);
1942 * tty_poll - check tty status
1943 * @filp: file being polled
1944 * @wait: poll wait structures to update
1946 * Call the line discipline polling method to obtain the poll
1947 * status of the device.
1949 * Locking: locks called line discipline but ldisc poll method
1950 * may be re-entered freely by other callers.
1953 static unsigned int tty_poll(struct file
*filp
, poll_table
*wait
)
1955 struct tty_struct
*tty
= file_tty(filp
);
1956 struct tty_ldisc
*ld
;
1959 if (tty_paranoia_check(tty
, filp
->f_path
.dentry
->d_inode
, "tty_poll"))
1962 ld
= tty_ldisc_ref_wait(tty
);
1964 ret
= (ld
->ops
->poll
)(tty
, filp
, wait
);
1965 tty_ldisc_deref(ld
);
1969 static int __tty_fasync(int fd
, struct file
*filp
, int on
)
1971 struct tty_struct
*tty
= file_tty(filp
);
1972 unsigned long flags
;
1975 if (tty_paranoia_check(tty
, filp
->f_path
.dentry
->d_inode
, "tty_fasync"))
1978 retval
= fasync_helper(fd
, filp
, on
, &tty
->fasync
);
1985 if (!waitqueue_active(&tty
->read_wait
))
1986 tty
->minimum_to_wake
= 1;
1987 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
1990 type
= PIDTYPE_PGID
;
1992 pid
= task_pid(current
);
1996 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
1997 retval
= __f_setown(filp
, pid
, type
, 0);
2002 if (!tty
->fasync
&& !waitqueue_active(&tty
->read_wait
))
2003 tty
->minimum_to_wake
= N_TTY_BUF_SIZE
;
2010 static int tty_fasync(int fd
, struct file
*filp
, int on
)
2014 retval
= __tty_fasync(fd
, filp
, on
);
2020 * tiocsti - fake input character
2021 * @tty: tty to fake input into
2022 * @p: pointer to character
2024 * Fake input to a tty device. Does the necessary locking and
2027 * FIXME: does not honour flow control ??
2030 * Called functions take tty_ldisc_lock
2031 * current->signal->tty check is safe without locks
2033 * FIXME: may race normal receive processing
2036 static int tiocsti(struct tty_struct
*tty
, char __user
*p
)
2039 struct tty_ldisc
*ld
;
2041 if ((current
->signal
->tty
!= tty
) && !capable(CAP_SYS_ADMIN
))
2043 if (get_user(ch
, p
))
2045 tty_audit_tiocsti(tty
, ch
);
2046 ld
= tty_ldisc_ref_wait(tty
);
2047 ld
->ops
->receive_buf(tty
, &ch
, &mbz
, 1);
2048 tty_ldisc_deref(ld
);
2053 * tiocgwinsz - implement window query ioctl
2055 * @arg: user buffer for result
2057 * Copies the kernel idea of the window size into the user buffer.
2059 * Locking: tty->termios_mutex is taken to ensure the winsize data
2063 static int tiocgwinsz(struct tty_struct
*tty
, struct winsize __user
*arg
)
2067 mutex_lock(&tty
->termios_mutex
);
2068 err
= copy_to_user(arg
, &tty
->winsize
, sizeof(*arg
));
2069 mutex_unlock(&tty
->termios_mutex
);
2071 return err
? -EFAULT
: 0;
2075 * tty_do_resize - resize event
2076 * @tty: tty being resized
2077 * @rows: rows (character)
2078 * @cols: cols (character)
2080 * Update the termios variables and send the necessary signals to
2081 * peform a terminal resize correctly
2084 int tty_do_resize(struct tty_struct
*tty
, struct winsize
*ws
)
2087 unsigned long flags
;
2090 mutex_lock(&tty
->termios_mutex
);
2091 if (!memcmp(ws
, &tty
->winsize
, sizeof(*ws
)))
2093 /* Get the PID values and reference them so we can
2094 avoid holding the tty ctrl lock while sending signals */
2095 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
2096 pgrp
= get_pid(tty
->pgrp
);
2097 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
2100 kill_pgrp(pgrp
, SIGWINCH
, 1);
2105 mutex_unlock(&tty
->termios_mutex
);
2110 * tiocswinsz - implement window size set ioctl
2111 * @tty; tty side of tty
2112 * @arg: user buffer for result
2114 * Copies the user idea of the window size to the kernel. Traditionally
2115 * this is just advisory information but for the Linux console it
2116 * actually has driver level meaning and triggers a VC resize.
2119 * Driver dependant. The default do_resize method takes the
2120 * tty termios mutex and ctrl_lock. The console takes its own lock
2121 * then calls into the default method.
2124 static int tiocswinsz(struct tty_struct
*tty
, struct winsize __user
*arg
)
2126 struct winsize tmp_ws
;
2127 if (copy_from_user(&tmp_ws
, arg
, sizeof(*arg
)))
2130 if (tty
->ops
->resize
)
2131 return tty
->ops
->resize(tty
, &tmp_ws
);
2133 return tty_do_resize(tty
, &tmp_ws
);
2137 * tioccons - allow admin to move logical console
2138 * @file: the file to become console
2140 * Allow the adminstrator to move the redirected console device
2142 * Locking: uses redirect_lock to guard the redirect information
2145 static int tioccons(struct file
*file
)
2147 if (!capable(CAP_SYS_ADMIN
))
2149 if (file
->f_op
->write
== redirected_tty_write
) {
2151 spin_lock(&redirect_lock
);
2154 spin_unlock(&redirect_lock
);
2159 spin_lock(&redirect_lock
);
2161 spin_unlock(&redirect_lock
);
2166 spin_unlock(&redirect_lock
);
2171 * fionbio - non blocking ioctl
2172 * @file: file to set blocking value
2173 * @p: user parameter
2175 * Historical tty interfaces had a blocking control ioctl before
2176 * the generic functionality existed. This piece of history is preserved
2177 * in the expected tty API of posix OS's.
2179 * Locking: none, the open file handle ensures it won't go away.
2182 static int fionbio(struct file
*file
, int __user
*p
)
2186 if (get_user(nonblock
, p
))
2189 spin_lock(&file
->f_lock
);
2191 file
->f_flags
|= O_NONBLOCK
;
2193 file
->f_flags
&= ~O_NONBLOCK
;
2194 spin_unlock(&file
->f_lock
);
2199 * tiocsctty - set controlling tty
2200 * @tty: tty structure
2201 * @arg: user argument
2203 * This ioctl is used to manage job control. It permits a session
2204 * leader to set this tty as the controlling tty for the session.
2207 * Takes tty_mutex() to protect tty instance
2208 * Takes tasklist_lock internally to walk sessions
2209 * Takes ->siglock() when updating signal->tty
2212 static int tiocsctty(struct tty_struct
*tty
, int arg
)
2215 if (current
->signal
->leader
&& (task_session(current
) == tty
->session
))
2218 mutex_lock(&tty_mutex
);
2220 * The process must be a session leader and
2221 * not have a controlling tty already.
2223 if (!current
->signal
->leader
|| current
->signal
->tty
) {
2230 * This tty is already the controlling
2231 * tty for another session group!
2233 if (arg
== 1 && capable(CAP_SYS_ADMIN
)) {
2237 read_lock(&tasklist_lock
);
2238 session_clear_tty(tty
->session
);
2239 read_unlock(&tasklist_lock
);
2245 proc_set_tty(current
, tty
);
2247 mutex_unlock(&tty_mutex
);
2252 * tty_get_pgrp - return a ref counted pgrp pid
2255 * Returns a refcounted instance of the pid struct for the process
2256 * group controlling the tty.
2259 struct pid
*tty_get_pgrp(struct tty_struct
*tty
)
2261 unsigned long flags
;
2264 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
2265 pgrp
= get_pid(tty
->pgrp
);
2266 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
2270 EXPORT_SYMBOL_GPL(tty_get_pgrp
);
2273 * tiocgpgrp - get process group
2274 * @tty: tty passed by user
2275 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2278 * Obtain the process group of the tty. If there is no process group
2281 * Locking: none. Reference to current->signal->tty is safe.
2284 static int tiocgpgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t __user
*p
)
2289 * (tty == real_tty) is a cheap way of
2290 * testing if the tty is NOT a master pty.
2292 if (tty
== real_tty
&& current
->signal
->tty
!= real_tty
)
2294 pid
= tty_get_pgrp(real_tty
);
2295 ret
= put_user(pid_vnr(pid
), p
);
2301 * tiocspgrp - attempt to set process group
2302 * @tty: tty passed by user
2303 * @real_tty: tty side device matching tty passed by user
2306 * Set the process group of the tty to the session passed. Only
2307 * permitted where the tty session is our session.
2309 * Locking: RCU, ctrl lock
2312 static int tiocspgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t __user
*p
)
2316 int retval
= tty_check_change(real_tty
);
2317 unsigned long flags
;
2323 if (!current
->signal
->tty
||
2324 (current
->signal
->tty
!= real_tty
) ||
2325 (real_tty
->session
!= task_session(current
)))
2327 if (get_user(pgrp_nr
, p
))
2332 pgrp
= find_vpid(pgrp_nr
);
2337 if (session_of_pgrp(pgrp
) != task_session(current
))
2340 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
2341 put_pid(real_tty
->pgrp
);
2342 real_tty
->pgrp
= get_pid(pgrp
);
2343 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
2350 * tiocgsid - get session id
2351 * @tty: tty passed by user
2352 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2353 * @p: pointer to returned session id
2355 * Obtain the session id of the tty. If there is no session
2358 * Locking: none. Reference to current->signal->tty is safe.
2361 static int tiocgsid(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t __user
*p
)
2364 * (tty == real_tty) is a cheap way of
2365 * testing if the tty is NOT a master pty.
2367 if (tty
== real_tty
&& current
->signal
->tty
!= real_tty
)
2369 if (!real_tty
->session
)
2371 return put_user(pid_vnr(real_tty
->session
), p
);
2375 * tiocsetd - set line discipline
2377 * @p: pointer to user data
2379 * Set the line discipline according to user request.
2381 * Locking: see tty_set_ldisc, this function is just a helper
2384 static int tiocsetd(struct tty_struct
*tty
, int __user
*p
)
2389 if (get_user(ldisc
, p
))
2392 ret
= tty_set_ldisc(tty
, ldisc
);
2398 * send_break - performed time break
2399 * @tty: device to break on
2400 * @duration: timeout in mS
2402 * Perform a timed break on hardware that lacks its own driver level
2403 * timed break functionality.
2406 * atomic_write_lock serializes
2410 static int send_break(struct tty_struct
*tty
, unsigned int duration
)
2414 if (tty
->ops
->break_ctl
== NULL
)
2417 if (tty
->driver
->flags
& TTY_DRIVER_HARDWARE_BREAK
)
2418 retval
= tty
->ops
->break_ctl(tty
, duration
);
2420 /* Do the work ourselves */
2421 if (tty_write_lock(tty
, 0) < 0)
2423 retval
= tty
->ops
->break_ctl(tty
, -1);
2426 if (!signal_pending(current
))
2427 msleep_interruptible(duration
);
2428 retval
= tty
->ops
->break_ctl(tty
, 0);
2430 tty_write_unlock(tty
);
2431 if (signal_pending(current
))
2438 * tty_tiocmget - get modem status
2440 * @file: user file pointer
2441 * @p: pointer to result
2443 * Obtain the modem status bits from the tty driver if the feature
2444 * is supported. Return -EINVAL if it is not available.
2446 * Locking: none (up to the driver)
2449 static int tty_tiocmget(struct tty_struct
*tty
, struct file
*file
, int __user
*p
)
2451 int retval
= -EINVAL
;
2453 if (tty
->ops
->tiocmget
) {
2454 retval
= tty
->ops
->tiocmget(tty
, file
);
2457 retval
= put_user(retval
, p
);
2463 * tty_tiocmset - set modem status
2465 * @file: user file pointer
2466 * @cmd: command - clear bits, set bits or set all
2467 * @p: pointer to desired bits
2469 * Set the modem status bits from the tty driver if the feature
2470 * is supported. Return -EINVAL if it is not available.
2472 * Locking: none (up to the driver)
2475 static int tty_tiocmset(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
,
2479 unsigned int set
, clear
, val
;
2481 if (tty
->ops
->tiocmset
== NULL
)
2484 retval
= get_user(val
, p
);
2500 set
&= TIOCM_DTR
|TIOCM_RTS
|TIOCM_OUT1
|TIOCM_OUT2
|TIOCM_LOOP
;
2501 clear
&= TIOCM_DTR
|TIOCM_RTS
|TIOCM_OUT1
|TIOCM_OUT2
|TIOCM_LOOP
;
2502 return tty
->ops
->tiocmset(tty
, file
, set
, clear
);
2505 struct tty_struct
*tty_pair_get_tty(struct tty_struct
*tty
)
2507 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
2508 tty
->driver
->subtype
== PTY_TYPE_MASTER
)
2512 EXPORT_SYMBOL(tty_pair_get_tty
);
2514 struct tty_struct
*tty_pair_get_pty(struct tty_struct
*tty
)
2516 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
&&
2517 tty
->driver
->subtype
== PTY_TYPE_MASTER
)
2521 EXPORT_SYMBOL(tty_pair_get_pty
);
2524 * Split this up, as gcc can choke on it otherwise..
2526 long tty_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2528 struct tty_struct
*tty
= file_tty(file
);
2529 struct tty_struct
*real_tty
;
2530 void __user
*p
= (void __user
*)arg
;
2532 struct tty_ldisc
*ld
;
2533 struct inode
*inode
= file
->f_dentry
->d_inode
;
2535 if (tty_paranoia_check(tty
, inode
, "tty_ioctl"))
2538 real_tty
= tty_pair_get_tty(tty
);
2541 * Factor out some common prep work
2549 retval
= tty_check_change(tty
);
2552 if (cmd
!= TIOCCBRK
) {
2553 tty_wait_until_sent(tty
, 0);
2554 if (signal_pending(current
))
2565 return tiocsti(tty
, p
);
2567 return tiocgwinsz(real_tty
, p
);
2569 return tiocswinsz(real_tty
, p
);
2571 return real_tty
!= tty
? -EINVAL
: tioccons(file
);
2573 return fionbio(file
, p
);
2575 set_bit(TTY_EXCLUSIVE
, &tty
->flags
);
2578 clear_bit(TTY_EXCLUSIVE
, &tty
->flags
);
2581 if (current
->signal
->tty
!= tty
)
2586 return tiocsctty(tty
, arg
);
2588 return tiocgpgrp(tty
, real_tty
, p
);
2590 return tiocspgrp(tty
, real_tty
, p
);
2592 return tiocgsid(tty
, real_tty
, p
);
2594 return put_user(tty
->ldisc
->ops
->num
, (int __user
*)p
);
2596 return tiocsetd(tty
, p
);
2600 case TIOCSBRK
: /* Turn break on, unconditionally */
2601 if (tty
->ops
->break_ctl
)
2602 return tty
->ops
->break_ctl(tty
, -1);
2604 case TIOCCBRK
: /* Turn break off, unconditionally */
2605 if (tty
->ops
->break_ctl
)
2606 return tty
->ops
->break_ctl(tty
, 0);
2608 case TCSBRK
: /* SVID version: non-zero arg --> no break */
2609 /* non-zero arg means wait for all output data
2610 * to be sent (performed above) but don't send break.
2611 * This is used by the tcdrain() termios function.
2614 return send_break(tty
, 250);
2616 case TCSBRKP
: /* support for POSIX tcsendbreak() */
2617 return send_break(tty
, arg
? arg
*100 : 250);
2620 return tty_tiocmget(tty
, file
, p
);
2624 return tty_tiocmset(tty
, file
, cmd
, p
);
2629 /* flush tty buffer and allow ldisc to process ioctl */
2630 tty_buffer_flush(tty
);
2635 if (tty
->ops
->ioctl
) {
2636 retval
= (tty
->ops
->ioctl
)(tty
, file
, cmd
, arg
);
2637 if (retval
!= -ENOIOCTLCMD
)
2640 ld
= tty_ldisc_ref_wait(tty
);
2642 if (ld
->ops
->ioctl
) {
2643 retval
= ld
->ops
->ioctl(tty
, file
, cmd
, arg
);
2644 if (retval
== -ENOIOCTLCMD
)
2647 tty_ldisc_deref(ld
);
2651 #ifdef CONFIG_COMPAT
2652 static long tty_compat_ioctl(struct file
*file
, unsigned int cmd
,
2655 struct inode
*inode
= file
->f_dentry
->d_inode
;
2656 struct tty_struct
*tty
= file_tty(file
);
2657 struct tty_ldisc
*ld
;
2658 int retval
= -ENOIOCTLCMD
;
2660 if (tty_paranoia_check(tty
, inode
, "tty_ioctl"))
2663 if (tty
->ops
->compat_ioctl
) {
2664 retval
= (tty
->ops
->compat_ioctl
)(tty
, file
, cmd
, arg
);
2665 if (retval
!= -ENOIOCTLCMD
)
2669 ld
= tty_ldisc_ref_wait(tty
);
2670 if (ld
->ops
->compat_ioctl
)
2671 retval
= ld
->ops
->compat_ioctl(tty
, file
, cmd
, arg
);
2672 tty_ldisc_deref(ld
);
2679 * This implements the "Secure Attention Key" --- the idea is to
2680 * prevent trojan horses by killing all processes associated with this
2681 * tty when the user hits the "Secure Attention Key". Required for
2682 * super-paranoid applications --- see the Orange Book for more details.
2684 * This code could be nicer; ideally it should send a HUP, wait a few
2685 * seconds, then send a INT, and then a KILL signal. But you then
2686 * have to coordinate with the init process, since all processes associated
2687 * with the current tty must be dead before the new getty is allowed
2690 * Now, if it would be correct ;-/ The current code has a nasty hole -
2691 * it doesn't catch files in flight. We may send the descriptor to ourselves
2692 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2694 * Nasty bug: do_SAK is being called in interrupt context. This can
2695 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2697 void __do_SAK(struct tty_struct
*tty
)
2702 struct task_struct
*g
, *p
;
2703 struct pid
*session
;
2706 struct fdtable
*fdt
;
2710 session
= tty
->session
;
2712 tty_ldisc_flush(tty
);
2714 tty_driver_flush_buffer(tty
);
2716 read_lock(&tasklist_lock
);
2717 /* Kill the entire session */
2718 do_each_pid_task(session
, PIDTYPE_SID
, p
) {
2719 printk(KERN_NOTICE
"SAK: killed process %d"
2720 " (%s): task_session(p)==tty->session\n",
2721 task_pid_nr(p
), p
->comm
);
2722 send_sig(SIGKILL
, p
, 1);
2723 } while_each_pid_task(session
, PIDTYPE_SID
, p
);
2724 /* Now kill any processes that happen to have the
2727 do_each_thread(g
, p
) {
2728 if (p
->signal
->tty
== tty
) {
2729 printk(KERN_NOTICE
"SAK: killed process %d"
2730 " (%s): task_session(p)==tty->session\n",
2731 task_pid_nr(p
), p
->comm
);
2732 send_sig(SIGKILL
, p
, 1);
2738 * We don't take a ref to the file, so we must
2739 * hold ->file_lock instead.
2741 spin_lock(&p
->files
->file_lock
);
2742 fdt
= files_fdtable(p
->files
);
2743 for (i
= 0; i
< fdt
->max_fds
; i
++) {
2744 filp
= fcheck_files(p
->files
, i
);
2747 if (filp
->f_op
->read
== tty_read
&&
2748 file_tty(filp
) == tty
) {
2749 printk(KERN_NOTICE
"SAK: killed process %d"
2750 " (%s): fd#%d opened to the tty\n",
2751 task_pid_nr(p
), p
->comm
, i
);
2752 force_sig(SIGKILL
, p
);
2756 spin_unlock(&p
->files
->file_lock
);
2759 } while_each_thread(g
, p
);
2760 read_unlock(&tasklist_lock
);
2764 static void do_SAK_work(struct work_struct
*work
)
2766 struct tty_struct
*tty
=
2767 container_of(work
, struct tty_struct
, SAK_work
);
2772 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2773 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2774 * the values which we write to it will be identical to the values which it
2775 * already has. --akpm
2777 void do_SAK(struct tty_struct
*tty
)
2781 schedule_work(&tty
->SAK_work
);
2784 EXPORT_SYMBOL(do_SAK
);
2787 * initialize_tty_struct
2788 * @tty: tty to initialize
2790 * This subroutine initializes a tty structure that has been newly
2793 * Locking: none - tty in question must not be exposed at this point
2796 void initialize_tty_struct(struct tty_struct
*tty
,
2797 struct tty_driver
*driver
, int idx
)
2799 memset(tty
, 0, sizeof(struct tty_struct
));
2800 kref_init(&tty
->kref
);
2801 tty
->magic
= TTY_MAGIC
;
2802 tty_ldisc_init(tty
);
2803 tty
->session
= NULL
;
2805 tty
->overrun_time
= jiffies
;
2806 tty
->buf
.head
= tty
->buf
.tail
= NULL
;
2807 tty_buffer_init(tty
);
2808 mutex_init(&tty
->termios_mutex
);
2809 mutex_init(&tty
->ldisc_mutex
);
2810 init_waitqueue_head(&tty
->write_wait
);
2811 init_waitqueue_head(&tty
->read_wait
);
2812 INIT_WORK(&tty
->hangup_work
, do_tty_hangup
);
2813 mutex_init(&tty
->atomic_read_lock
);
2814 mutex_init(&tty
->atomic_write_lock
);
2815 mutex_init(&tty
->output_lock
);
2816 mutex_init(&tty
->echo_lock
);
2817 spin_lock_init(&tty
->read_lock
);
2818 spin_lock_init(&tty
->ctrl_lock
);
2819 INIT_LIST_HEAD(&tty
->tty_files
);
2820 INIT_WORK(&tty
->SAK_work
, do_SAK_work
);
2822 tty
->driver
= driver
;
2823 tty
->ops
= driver
->ops
;
2825 tty_line_name(driver
, idx
, tty
->name
);
2829 * tty_put_char - write one character to a tty
2833 * Write one byte to the tty using the provided put_char method
2834 * if present. Returns the number of characters successfully output.
2836 * Note: the specific put_char operation in the driver layer may go
2837 * away soon. Don't call it directly, use this method
2840 int tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
2842 if (tty
->ops
->put_char
)
2843 return tty
->ops
->put_char(tty
, ch
);
2844 return tty
->ops
->write(tty
, &ch
, 1);
2846 EXPORT_SYMBOL_GPL(tty_put_char
);
2848 struct class *tty_class
;
2851 * tty_register_device - register a tty device
2852 * @driver: the tty driver that describes the tty device
2853 * @index: the index in the tty driver for this tty device
2854 * @device: a struct device that is associated with this tty device.
2855 * This field is optional, if there is no known struct device
2856 * for this tty device it can be set to NULL safely.
2858 * Returns a pointer to the struct device for this tty device
2859 * (or ERR_PTR(-EFOO) on error).
2861 * This call is required to be made to register an individual tty device
2862 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2863 * that bit is not set, this function should not be called by a tty
2869 struct device
*tty_register_device(struct tty_driver
*driver
, unsigned index
,
2870 struct device
*device
)
2873 dev_t dev
= MKDEV(driver
->major
, driver
->minor_start
) + index
;
2875 if (index
>= driver
->num
) {
2876 printk(KERN_ERR
"Attempt to register invalid tty line number "
2878 return ERR_PTR(-EINVAL
);
2881 if (driver
->type
== TTY_DRIVER_TYPE_PTY
)
2882 pty_line_name(driver
, index
, name
);
2884 tty_line_name(driver
, index
, name
);
2886 return device_create(tty_class
, device
, dev
, NULL
, name
);
2888 EXPORT_SYMBOL(tty_register_device
);
2891 * tty_unregister_device - unregister a tty device
2892 * @driver: the tty driver that describes the tty device
2893 * @index: the index in the tty driver for this tty device
2895 * If a tty device is registered with a call to tty_register_device() then
2896 * this function must be called when the tty device is gone.
2901 void tty_unregister_device(struct tty_driver
*driver
, unsigned index
)
2903 device_destroy(tty_class
,
2904 MKDEV(driver
->major
, driver
->minor_start
) + index
);
2906 EXPORT_SYMBOL(tty_unregister_device
);
2908 struct tty_driver
*alloc_tty_driver(int lines
)
2910 struct tty_driver
*driver
;
2912 driver
= kzalloc(sizeof(struct tty_driver
), GFP_KERNEL
);
2914 kref_init(&driver
->kref
);
2915 driver
->magic
= TTY_DRIVER_MAGIC
;
2916 driver
->num
= lines
;
2917 /* later we'll move allocation of tables here */
2921 EXPORT_SYMBOL(alloc_tty_driver
);
2923 static void destruct_tty_driver(struct kref
*kref
)
2925 struct tty_driver
*driver
= container_of(kref
, struct tty_driver
, kref
);
2927 struct ktermios
*tp
;
2930 if (driver
->flags
& TTY_DRIVER_INSTALLED
) {
2932 * Free the termios and termios_locked structures because
2933 * we don't want to get memory leaks when modular tty
2934 * drivers are removed from the kernel.
2936 for (i
= 0; i
< driver
->num
; i
++) {
2937 tp
= driver
->termios
[i
];
2939 driver
->termios
[i
] = NULL
;
2942 if (!(driver
->flags
& TTY_DRIVER_DYNAMIC_DEV
))
2943 tty_unregister_device(driver
, i
);
2946 proc_tty_unregister_driver(driver
);
2947 driver
->ttys
= NULL
;
2948 driver
->termios
= NULL
;
2950 cdev_del(&driver
->cdev
);
2955 void tty_driver_kref_put(struct tty_driver
*driver
)
2957 kref_put(&driver
->kref
, destruct_tty_driver
);
2959 EXPORT_SYMBOL(tty_driver_kref_put
);
2961 void tty_set_operations(struct tty_driver
*driver
,
2962 const struct tty_operations
*op
)
2966 EXPORT_SYMBOL(tty_set_operations
);
2968 void put_tty_driver(struct tty_driver
*d
)
2970 tty_driver_kref_put(d
);
2972 EXPORT_SYMBOL(put_tty_driver
);
2975 * Called by a tty driver to register itself.
2977 int tty_register_driver(struct tty_driver
*driver
)
2984 if (!(driver
->flags
& TTY_DRIVER_DEVPTS_MEM
) && driver
->num
) {
2985 p
= kzalloc(driver
->num
* 2 * sizeof(void *), GFP_KERNEL
);
2990 if (!driver
->major
) {
2991 error
= alloc_chrdev_region(&dev
, driver
->minor_start
,
2992 driver
->num
, driver
->name
);
2994 driver
->major
= MAJOR(dev
);
2995 driver
->minor_start
= MINOR(dev
);
2998 dev
= MKDEV(driver
->major
, driver
->minor_start
);
2999 error
= register_chrdev_region(dev
, driver
->num
, driver
->name
);
3007 driver
->ttys
= (struct tty_struct
**)p
;
3008 driver
->termios
= (struct ktermios
**)(p
+ driver
->num
);
3010 driver
->ttys
= NULL
;
3011 driver
->termios
= NULL
;
3014 cdev_init(&driver
->cdev
, &tty_fops
);
3015 driver
->cdev
.owner
= driver
->owner
;
3016 error
= cdev_add(&driver
->cdev
, dev
, driver
->num
);
3018 unregister_chrdev_region(dev
, driver
->num
);
3019 driver
->ttys
= NULL
;
3020 driver
->termios
= NULL
;
3025 mutex_lock(&tty_mutex
);
3026 list_add(&driver
->tty_drivers
, &tty_drivers
);
3027 mutex_unlock(&tty_mutex
);
3029 if (!(driver
->flags
& TTY_DRIVER_DYNAMIC_DEV
)) {
3030 for (i
= 0; i
< driver
->num
; i
++)
3031 tty_register_device(driver
, i
, NULL
);
3033 proc_tty_register_driver(driver
);
3034 driver
->flags
|= TTY_DRIVER_INSTALLED
;
3038 EXPORT_SYMBOL(tty_register_driver
);
3041 * Called by a tty driver to unregister itself.
3043 int tty_unregister_driver(struct tty_driver
*driver
)
3047 if (driver
->refcount
)
3050 unregister_chrdev_region(MKDEV(driver
->major
, driver
->minor_start
),
3052 mutex_lock(&tty_mutex
);
3053 list_del(&driver
->tty_drivers
);
3054 mutex_unlock(&tty_mutex
);
3058 EXPORT_SYMBOL(tty_unregister_driver
);
3060 dev_t
tty_devnum(struct tty_struct
*tty
)
3062 return MKDEV(tty
->driver
->major
, tty
->driver
->minor_start
) + tty
->index
;
3064 EXPORT_SYMBOL(tty_devnum
);
3066 void proc_clear_tty(struct task_struct
*p
)
3068 unsigned long flags
;
3069 struct tty_struct
*tty
;
3070 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
3071 tty
= p
->signal
->tty
;
3072 p
->signal
->tty
= NULL
;
3073 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
3077 /* Called under the sighand lock */
3079 static void __proc_set_tty(struct task_struct
*tsk
, struct tty_struct
*tty
)
3082 unsigned long flags
;
3083 /* We should not have a session or pgrp to put here but.... */
3084 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
3085 put_pid(tty
->session
);
3087 tty
->pgrp
= get_pid(task_pgrp(tsk
));
3088 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
3089 tty
->session
= get_pid(task_session(tsk
));
3090 if (tsk
->signal
->tty
) {
3091 printk(KERN_DEBUG
"tty not NULL!!\n");
3092 tty_kref_put(tsk
->signal
->tty
);
3095 put_pid(tsk
->signal
->tty_old_pgrp
);
3096 tsk
->signal
->tty
= tty_kref_get(tty
);
3097 tsk
->signal
->tty_old_pgrp
= NULL
;
3100 static void proc_set_tty(struct task_struct
*tsk
, struct tty_struct
*tty
)
3102 spin_lock_irq(&tsk
->sighand
->siglock
);
3103 __proc_set_tty(tsk
, tty
);
3104 spin_unlock_irq(&tsk
->sighand
->siglock
);
3107 struct tty_struct
*get_current_tty(void)
3109 struct tty_struct
*tty
;
3110 unsigned long flags
;
3112 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
3113 tty
= tty_kref_get(current
->signal
->tty
);
3114 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
3117 EXPORT_SYMBOL_GPL(get_current_tty
);
3119 void tty_default_fops(struct file_operations
*fops
)
3125 * Initialize the console device. This is called *early*, so
3126 * we can't necessarily depend on lots of kernel help here.
3127 * Just do some early initializations, and do the complex setup
3130 void __init
console_init(void)
3134 /* Setup the default TTY line discipline. */
3138 * set up the console device so that later boot sequences can
3139 * inform about problems etc..
3141 call
= __con_initcall_start
;
3142 while (call
< __con_initcall_end
) {
3148 static char *tty_devnode(struct device
*dev
, mode_t
*mode
)
3152 if (dev
->devt
== MKDEV(TTYAUX_MAJOR
, 0) ||
3153 dev
->devt
== MKDEV(TTYAUX_MAJOR
, 2))
3158 static int __init
tty_class_init(void)
3160 tty_class
= class_create(THIS_MODULE
, "tty");
3161 if (IS_ERR(tty_class
))
3162 return PTR_ERR(tty_class
);
3163 tty_class
->devnode
= tty_devnode
;
3167 postcore_initcall(tty_class_init
);
3169 /* 3/2004 jmc: why do these devices exist? */
3171 static struct cdev tty_cdev
, console_cdev
;
3174 * Ok, now we can initialize the rest of the tty devices and can count
3175 * on memory allocations, interrupts etc..
3177 int __init
tty_init(void)
3179 cdev_init(&tty_cdev
, &tty_fops
);
3180 if (cdev_add(&tty_cdev
, MKDEV(TTYAUX_MAJOR
, 0), 1) ||
3181 register_chrdev_region(MKDEV(TTYAUX_MAJOR
, 0), 1, "/dev/tty") < 0)
3182 panic("Couldn't register /dev/tty driver\n");
3183 device_create(tty_class
, NULL
, MKDEV(TTYAUX_MAJOR
, 0), NULL
,
3186 cdev_init(&console_cdev
, &console_fops
);
3187 if (cdev_add(&console_cdev
, MKDEV(TTYAUX_MAJOR
, 1), 1) ||
3188 register_chrdev_region(MKDEV(TTYAUX_MAJOR
, 1), 1, "/dev/console") < 0)
3189 panic("Couldn't register /dev/console driver\n");
3190 device_create(tty_class
, NULL
, MKDEV(TTYAUX_MAJOR
, 1), NULL
,
3194 vty_init(&console_fops
);