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>
65 #include <linux/config.h>
66 #include <linux/types.h>
67 #include <linux/major.h>
68 #include <linux/errno.h>
69 #include <linux/signal.h>
70 #include <linux/fcntl.h>
71 #include <linux/sched.h>
72 #include <linux/interrupt.h>
73 #include <linux/tty.h>
74 #include <linux/tty_driver.h>
75 #include <linux/tty_flip.h>
76 #include <linux/devpts_fs.h>
77 #include <linux/file.h>
78 #include <linux/console.h>
79 #include <linux/timer.h>
80 #include <linux/ctype.h>
83 #include <linux/string.h>
84 #include <linux/malloc.h>
85 #include <linux/poll.h>
86 #include <linux/proc_fs.h>
87 #include <linux/init.h>
88 #include <linux/module.h>
89 #include <linux/smp_lock.h>
91 #include <asm/uaccess.h>
92 #include <asm/system.h>
93 #include <asm/bitops.h>
95 #include <linux/kbd_kern.h>
96 #include <linux/vt_kern.h>
97 #include <linux/selection.h>
98 #include <linux/devfs_fs_kernel.h>
100 #include <linux/kmod.h>
103 extern void con_init_devfs (void);
105 extern int rio_init(void);
107 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
108 #define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
109 #define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
110 #define PTMX_DEV MKDEV(TTYAUX_MAJOR,2)
112 #undef TTY_DEBUG_HANGUP
114 #define TTY_PARANOIA_CHECK 1
115 #define CHECK_TTY_COUNT 1
117 struct termios tty_std_termios
; /* for the benefit of tty drivers */
118 struct tty_driver
*tty_drivers
; /* linked list of tty drivers */
119 struct tty_ldisc ldiscs
[NR_LDISCS
]; /* line disc dispatch table */
121 #ifdef CONFIG_UNIX98_PTYS
122 extern struct tty_driver ptm_driver
[]; /* Unix98 pty masters; for /dev/ptmx */
123 extern struct tty_driver pts_driver
[]; /* Unix98 pty slaves; for /dev/ptmx */
127 * redirect is the pseudo-tty that console output
128 * is redirected to if asked by TIOCCONS.
130 struct tty_struct
* redirect
;
132 static void initialize_tty_struct(struct tty_struct
*tty
);
134 static ssize_t
tty_read(struct file
*, char *, size_t, loff_t
*);
135 static ssize_t
tty_write(struct file
*, const char *, size_t, loff_t
*);
136 static unsigned int tty_poll(struct file
*, poll_table
*);
137 static int tty_open(struct inode
*, struct file
*);
138 static int tty_release(struct inode
*, struct file
*);
139 int tty_ioctl(struct inode
* inode
, struct file
* file
,
140 unsigned int cmd
, unsigned long arg
);
141 static int tty_fasync(int fd
, struct file
* filp
, int on
);
142 extern int sx_init (void);
143 extern int vme_scc_init (void);
144 extern long vme_scc_console_init(void);
145 extern int serial167_init(void);
146 extern long serial167_console_init(void);
147 extern void console_8xx_init(void);
148 extern int rs_8xx_init(void);
149 extern void hwc_console_init(void);
150 extern void con3215_init(void);
151 extern void rs285_console_init(void);
152 extern void sa1100_rs_console_init(void);
153 extern void sgi_serial_console_init(void);
154 extern void sci_console_init(void);
157 #define MIN(a,b) ((a) < (b) ? (a) : (b))
160 #define MAX(a,b) ((a) < (b) ? (b) : (a))
163 static inline struct tty_struct
*alloc_tty_struct(void)
165 struct tty_struct
*tty
;
167 if (PAGE_SIZE
> 8192) {
168 tty
= kmalloc(sizeof(struct tty_struct
), GFP_KERNEL
);
170 memset(tty
, 0, sizeof(struct tty_struct
));
172 tty
= (struct tty_struct
*)get_zeroed_page(GFP_KERNEL
);
177 static inline void free_tty_struct(struct tty_struct
*tty
)
179 if (PAGE_SIZE
> 8192)
182 free_page((unsigned long) tty
);
186 * This routine returns the name of tty.
189 _tty_make_name(struct tty_struct
*tty
, const char *name
, char *buf
)
191 int idx
= (tty
)?MINOR(tty
->device
) - tty
->driver
.minor_start
:0;
193 if (!tty
) /* Hmm. NULL pointer. That's fun. */
194 strcpy(buf
, "NULL tty");
197 idx
+ tty
->driver
.name_base
);
202 #define TTY_NUMBER(tty) (MINOR((tty)->device) - (tty)->driver.minor_start + \
203 (tty)->driver.name_base)
205 char *tty_name(struct tty_struct
*tty
, char *buf
)
207 return _tty_make_name(tty
, (tty
)?tty
->driver
.name
:NULL
, buf
);
210 inline int tty_paranoia_check(struct tty_struct
*tty
, kdev_t device
,
213 #ifdef TTY_PARANOIA_CHECK
214 static const char *badmagic
=
215 "Warning: bad magic number for tty struct (%s) in %s\n";
216 static const char *badtty
=
217 "Warning: null TTY for (%s) in %s\n";
220 printk(badtty
, kdevname(device
), routine
);
223 if (tty
->magic
!= TTY_MAGIC
) {
224 printk(badmagic
, kdevname(device
), routine
);
231 static int check_tty_count(struct tty_struct
*tty
, const char *routine
)
233 #ifdef CHECK_TTY_COUNT
238 for(p
= tty
->tty_files
.next
; p
!= &tty
->tty_files
; p
= p
->next
) {
239 if(list_entry(p
, struct file
, f_list
)->private_data
== tty
)
243 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
244 tty
->driver
.subtype
== PTY_TYPE_SLAVE
&&
245 tty
->link
&& tty
->link
->count
)
247 if (tty
->count
!= count
) {
248 printk("Warning: dev (%s) tty->count(%d) != #fd's(%d) in %s\n",
249 kdevname(tty
->device
), tty
->count
, count
, routine
);
256 int tty_register_ldisc(int disc
, struct tty_ldisc
*new_ldisc
)
258 if (disc
< N_TTY
|| disc
>= NR_LDISCS
)
262 ldiscs
[disc
] = *new_ldisc
;
263 ldiscs
[disc
].flags
|= LDISC_FLAG_DEFINED
;
264 ldiscs
[disc
].num
= disc
;
266 memset(&ldiscs
[disc
], 0, sizeof(struct tty_ldisc
));
271 /* Set the discipline of a tty line. */
272 static int tty_set_ldisc(struct tty_struct
*tty
, int ldisc
)
275 struct tty_ldisc o_ldisc
;
278 if ((ldisc
< N_TTY
) || (ldisc
>= NR_LDISCS
))
280 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
281 /* Cyrus Durgin <cider@speakeasy.org> */
282 if (!(ldiscs
[ldisc
].flags
& LDISC_FLAG_DEFINED
)) {
284 sprintf(modname
, "tty-ldisc-%d", ldisc
);
285 request_module (modname
);
287 if (!(ldiscs
[ldisc
].flags
& LDISC_FLAG_DEFINED
))
290 if (tty
->ldisc
.num
== ldisc
)
291 return 0; /* We are already in the desired discipline */
292 o_ldisc
= tty
->ldisc
;
294 tty_wait_until_sent(tty
, 0);
296 /* Shutdown the current discipline. */
297 if (tty
->ldisc
.close
)
298 (tty
->ldisc
.close
)(tty
);
300 /* Now set up the new line discipline. */
301 tty
->ldisc
= ldiscs
[ldisc
];
302 tty
->termios
->c_line
= ldisc
;
304 retval
= (tty
->ldisc
.open
)(tty
);
306 tty
->ldisc
= o_ldisc
;
307 tty
->termios
->c_line
= tty
->ldisc
.num
;
308 if (tty
->ldisc
.open
&& (tty
->ldisc
.open(tty
) < 0)) {
309 tty
->ldisc
= ldiscs
[N_TTY
];
310 tty
->termios
->c_line
= N_TTY
;
311 if (tty
->ldisc
.open
) {
312 int r
= tty
->ldisc
.open(tty
);
315 panic("Couldn't open N_TTY ldisc for "
317 tty_name(tty
, buf
), r
);
321 if (tty
->ldisc
.num
!= o_ldisc
.num
&& tty
->driver
.set_ldisc
)
322 tty
->driver
.set_ldisc(tty
);
327 * This routine returns a tty driver structure, given a device number
329 struct tty_driver
*get_tty_driver(kdev_t device
)
332 struct tty_driver
*p
;
334 minor
= MINOR(device
);
335 major
= MAJOR(device
);
337 for (p
= tty_drivers
; p
; p
= p
->next
) {
338 if (p
->major
!= major
)
340 if (minor
< p
->minor_start
)
342 if (minor
>= p
->minor_start
+ p
->num
)
350 * If we try to write to, or set the state of, a terminal and we're
351 * not in the foreground, send a SIGTTOU. If the signal is blocked or
352 * ignored, go ahead and perform the operation. (POSIX 7.2)
354 int tty_check_change(struct tty_struct
* tty
)
356 if (current
->tty
!= tty
)
358 if (tty
->pgrp
<= 0) {
359 printk("tty_check_change: tty->pgrp <= 0!\n");
362 if (current
->pgrp
== tty
->pgrp
)
364 if (is_ignored(SIGTTOU
))
366 if (is_orphaned_pgrp(current
->pgrp
))
368 (void) kill_pg(current
->pgrp
,SIGTTOU
,1);
372 static ssize_t
hung_up_tty_read(struct file
* file
, char * buf
,
373 size_t count
, loff_t
*ppos
)
375 /* Can't seek (pread) on ttys. */
376 if (ppos
!= &file
->f_pos
)
381 static ssize_t
hung_up_tty_write(struct file
* file
, const char * buf
,
382 size_t count
, loff_t
*ppos
)
384 /* Can't seek (pwrite) on ttys. */
385 if (ppos
!= &file
->f_pos
)
390 /* No kernel lock held - none needed ;) */
391 static unsigned int hung_up_tty_poll(struct file
* filp
, poll_table
* wait
)
393 return POLLIN
| POLLOUT
| POLLERR
| POLLHUP
| POLLRDNORM
| POLLWRNORM
;
396 static int hung_up_tty_ioctl(struct inode
* inode
, struct file
* file
,
397 unsigned int cmd
, unsigned long arg
)
399 return cmd
== TIOCSPGRP
? -ENOTTY
: -EIO
;
402 static loff_t
tty_lseek(struct file
* file
, loff_t offset
, int orig
)
407 static struct file_operations tty_fops
= {
414 release
: tty_release
,
418 static struct file_operations hung_up_tty_fops
= {
420 read
: hung_up_tty_read
,
421 write
: hung_up_tty_write
,
422 poll
: hung_up_tty_poll
,
423 ioctl
: hung_up_tty_ioctl
,
424 release
: tty_release
,
428 * This can be called by the "eventd" kernel thread. That is process synchronous,
429 * but doesn't hold any locks, so we need to make sure we have the appropriate
430 * locks for what we're doing..
432 void do_tty_hangup(void *data
)
434 struct tty_struct
*tty
= (struct tty_struct
*) data
;
435 struct file
* cons_filp
= NULL
;
436 struct task_struct
*p
;
438 int closecount
= 0, n
;
443 /* inuse_filps is protected by the single kernel lock */
446 check_tty_count(tty
, "do_tty_hangup");
448 for (l
= tty
->tty_files
.next
; l
!= &tty
->tty_files
; l
= l
->next
) {
449 struct file
* filp
= list_entry(l
, struct file
, f_list
);
452 if (filp
->f_dentry
->d_inode
->i_rdev
== CONSOLE_DEV
||
453 filp
->f_dentry
->d_inode
->i_rdev
== SYSCONS_DEV
) {
457 if (filp
->f_op
!= &tty_fops
)
460 tty_fasync(-1, filp
, 0); /* can't block */
461 filp
->f_op
= &hung_up_tty_fops
;
465 /* FIXME! What are the locking issues here? This may me overdoing things.. */
469 save_flags(flags
); cli();
470 if (tty
->ldisc
.flush_buffer
)
471 tty
->ldisc
.flush_buffer(tty
);
472 if (tty
->driver
.flush_buffer
)
473 tty
->driver
.flush_buffer(tty
);
474 if ((test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) &&
475 tty
->ldisc
.write_wakeup
)
476 (tty
->ldisc
.write_wakeup
)(tty
);
477 restore_flags(flags
);
480 wake_up_interruptible(&tty
->write_wait
);
481 wake_up_interruptible(&tty
->read_wait
);
484 * Shutdown the current line discipline, and reset it to
487 if (tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
)
488 *tty
->termios
= tty
->driver
.init_termios
;
489 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
490 if (tty
->ldisc
.close
)
491 (tty
->ldisc
.close
)(tty
);
492 tty
->ldisc
= ldiscs
[N_TTY
];
493 tty
->termios
->c_line
= N_TTY
;
494 if (tty
->ldisc
.open
) {
495 int i
= (tty
->ldisc
.open
)(tty
);
497 printk("do_tty_hangup: N_TTY open: error %d\n",
502 read_lock(&tasklist_lock
);
504 if ((tty
->session
> 0) && (p
->session
== tty
->session
) &&
506 send_sig(SIGHUP
,p
,1);
507 send_sig(SIGCONT
,p
,1);
509 p
->tty_old_pgrp
= tty
->pgrp
;
514 read_unlock(&tasklist_lock
);
519 tty
->ctrl_status
= 0;
521 * If one of the devices matches a console pointer, we
522 * cannot just call hangup() because that will cause
523 * tty->count and state->count to go out of sync.
524 * So we just call close() the right number of times.
527 if (tty
->driver
.close
)
528 for (n
= 0; n
< closecount
; n
++)
529 tty
->driver
.close(tty
, cons_filp
);
530 } else if (tty
->driver
.hangup
)
531 (tty
->driver
.hangup
)(tty
);
535 void tty_hangup(struct tty_struct
* tty
)
537 #ifdef TTY_DEBUG_HANGUP
540 printk("%s hangup...\n", tty_name(tty
, buf
));
542 schedule_task(&tty
->tq_hangup
);
545 void tty_vhangup(struct tty_struct
* tty
)
547 #ifdef TTY_DEBUG_HANGUP
550 printk("%s vhangup...\n", tty_name(tty
, buf
));
552 do_tty_hangup((void *) tty
);
555 int tty_hung_up_p(struct file
* filp
)
557 return (filp
->f_op
== &hung_up_tty_fops
);
561 * This function is typically called only by the session leader, when
562 * it wants to disassociate itself from its controlling tty.
564 * It performs the following functions:
565 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
566 * (2) Clears the tty from being controlling the session
567 * (3) Clears the controlling tty for all processes in the
570 * The argument on_exit is set to 1 if called when a process is
571 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
573 void disassociate_ctty(int on_exit
)
575 struct tty_struct
*tty
= current
->tty
;
576 struct task_struct
*p
;
580 tty_pgrp
= tty
->pgrp
;
581 if (on_exit
&& tty
->driver
.type
!= TTY_DRIVER_TYPE_PTY
)
584 if (current
->tty_old_pgrp
) {
585 kill_pg(current
->tty_old_pgrp
, SIGHUP
, on_exit
);
586 kill_pg(current
->tty_old_pgrp
, SIGCONT
, on_exit
);
591 kill_pg(tty_pgrp
, SIGHUP
, on_exit
);
593 kill_pg(tty_pgrp
, SIGCONT
, on_exit
);
596 current
->tty_old_pgrp
= 0;
600 read_lock(&tasklist_lock
);
602 if (p
->session
== current
->session
)
604 read_unlock(&tasklist_lock
);
607 void wait_for_keypress(void)
609 struct console
*c
= console_drivers
;
610 if (c
) c
->wait_key(c
);
613 void stop_tty(struct tty_struct
*tty
)
618 if (tty
->link
&& tty
->link
->packet
) {
619 tty
->ctrl_status
&= ~TIOCPKT_START
;
620 tty
->ctrl_status
|= TIOCPKT_STOP
;
621 wake_up_interruptible(&tty
->link
->read_wait
);
623 if (tty
->driver
.stop
)
624 (tty
->driver
.stop
)(tty
);
627 void start_tty(struct tty_struct
*tty
)
629 if (!tty
->stopped
|| tty
->flow_stopped
)
632 if (tty
->link
&& tty
->link
->packet
) {
633 tty
->ctrl_status
&= ~TIOCPKT_STOP
;
634 tty
->ctrl_status
|= TIOCPKT_START
;
635 wake_up_interruptible(&tty
->link
->read_wait
);
637 if (tty
->driver
.start
)
638 (tty
->driver
.start
)(tty
);
639 if ((test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) &&
640 tty
->ldisc
.write_wakeup
)
641 (tty
->ldisc
.write_wakeup
)(tty
);
642 wake_up_interruptible(&tty
->write_wait
);
645 static ssize_t
tty_read(struct file
* file
, char * buf
, size_t count
,
649 struct tty_struct
* tty
;
652 /* Can't seek (pread) on ttys. */
653 if (ppos
!= &file
->f_pos
)
656 tty
= (struct tty_struct
*)file
->private_data
;
657 inode
= file
->f_dentry
->d_inode
;
658 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_read"))
660 if (!tty
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
663 /* This check not only needs to be done before reading, but also
664 whenever read_chan() gets woken up after sleeping, so I've
665 moved it to there. This should only be done for the N_TTY
666 line discipline, anyway. Same goes for write_chan(). -- jlc. */
668 if ((inode
->i_rdev
!= CONSOLE_DEV
) && /* don't stop on /dev/console */
670 (current
->tty
== tty
) &&
671 (tty
->pgrp
!= current
->pgrp
))
672 if (is_ignored(SIGTTIN
) || is_orphaned_pgrp(current
->pgrp
))
675 (void) kill_pg(current
->pgrp
, SIGTTIN
, 1);
681 i
= (tty
->ldisc
.read
)(tty
,file
,buf
,count
);
686 inode
->i_atime
= CURRENT_TIME
;
691 * Split writes up in sane blocksizes to avoid
692 * denial-of-service type attacks
694 static inline ssize_t
do_tty_write(
695 ssize_t (*write
)(struct tty_struct
*, struct file
*, const unsigned char *, size_t),
696 struct tty_struct
*tty
,
698 const unsigned char *buf
,
701 ssize_t ret
= 0, written
= 0;
702 struct inode
*inode
= file
->f_dentry
->d_inode
;
704 if (down_interruptible(&inode
->i_sem
)) {
707 if ( test_bit(TTY_NO_WRITE_SPLIT
, &tty
->flags
) ) {
709 written
= write(tty
, file
, buf
, count
);
713 unsigned long size
= MAX(PAGE_SIZE
*2,16384);
717 ret
= write(tty
, file
, buf
, size
);
727 if (signal_pending(current
))
729 if (current
->need_resched
)
734 file
->f_dentry
->d_inode
->i_mtime
= CURRENT_TIME
;
742 static ssize_t
tty_write(struct file
* file
, const char * buf
, size_t count
,
746 struct tty_struct
* tty
;
749 /* Can't seek (pwrite) on ttys. */
750 if (ppos
!= &file
->f_pos
)
754 * For now, we redirect writes from /dev/console as
757 inode
= file
->f_dentry
->d_inode
;
758 is_console
= (inode
->i_rdev
== SYSCONS_DEV
||
759 inode
->i_rdev
== CONSOLE_DEV
);
761 if (is_console
&& redirect
)
764 tty
= (struct tty_struct
*)file
->private_data
;
765 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_write"))
767 if (!tty
|| !tty
->driver
.write
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
770 if (!is_console
&& L_TOSTOP(tty
) && (tty
->pgrp
> 0) &&
771 (current
->tty
== tty
) && (tty
->pgrp
!= current
->pgrp
)) {
772 if (is_orphaned_pgrp(current
->pgrp
))
774 if (!is_ignored(SIGTTOU
)) {
775 (void) kill_pg(current
->pgrp
, SIGTTOU
, 1);
780 if (!tty
->ldisc
.write
)
782 return do_tty_write(tty
->ldisc
.write
, tty
, file
,
783 (const unsigned char *)buf
, count
);
786 /* Semaphore to protect creating and releasing a tty */
787 static DECLARE_MUTEX(tty_sem
);
789 static void down_tty_sem(int index
)
794 static void up_tty_sem(int index
)
799 static void release_mem(struct tty_struct
*tty
, int idx
);
802 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
803 * failed open. The new code protects the open with a semaphore, so it's
804 * really quite straightforward. The semaphore locking can probably be
805 * relaxed for the (most common) case of reopening a tty.
807 static int init_dev(kdev_t device
, struct tty_struct
**ret_tty
)
809 struct tty_struct
*tty
, *o_tty
;
810 struct termios
*tp
, **tp_loc
, *o_tp
, **o_tp_loc
;
811 struct termios
*ltp
, **ltp_loc
, *o_ltp
, **o_ltp_loc
;
812 struct tty_driver
*driver
;
816 driver
= get_tty_driver(device
);
820 idx
= MINOR(device
) - driver
->minor_start
;
823 * Check whether we need to acquire the tty semaphore to avoid
824 * race conditions. For now, play it safe.
828 /* check whether we're reopening an existing tty */
829 tty
= driver
->table
[idx
];
830 if (tty
) goto fast_track
;
833 * First time open is complex, especially for PTY devices.
834 * This code guarantees that either everything succeeds and the
835 * TTY is ready for operation, or else the table slots are vacated
836 * and the allocated memory released. (Except that the termios
837 * and locked termios may be retained.)
844 tty
= alloc_tty_struct();
847 initialize_tty_struct(tty
);
848 tty
->device
= device
;
849 tty
->driver
= *driver
;
851 tp_loc
= &driver
->termios
[idx
];
853 tp
= (struct termios
*) kmalloc(sizeof(struct termios
),
857 *tp
= driver
->init_termios
;
860 ltp_loc
= &driver
->termios_locked
[idx
];
862 ltp
= (struct termios
*) kmalloc(sizeof(struct termios
),
866 memset(ltp
, 0, sizeof(struct termios
));
869 if (driver
->type
== TTY_DRIVER_TYPE_PTY
) {
870 o_tty
= alloc_tty_struct();
873 initialize_tty_struct(o_tty
);
874 o_tty
->device
= (kdev_t
) MKDEV(driver
->other
->major
,
875 driver
->other
->minor_start
+ idx
);
876 o_tty
->driver
= *driver
->other
;
878 o_tp_loc
= &driver
->other
->termios
[idx
];
880 o_tp
= (struct termios
*)
881 kmalloc(sizeof(struct termios
), GFP_KERNEL
);
884 *o_tp
= driver
->other
->init_termios
;
887 o_ltp_loc
= &driver
->other
->termios_locked
[idx
];
889 o_ltp
= (struct termios
*)
890 kmalloc(sizeof(struct termios
), GFP_KERNEL
);
893 memset(o_ltp
, 0, sizeof(struct termios
));
897 * Everything allocated ... set up the o_tty structure.
899 driver
->other
->table
[idx
] = o_tty
;
904 o_tty
->termios
= *o_tp_loc
;
905 o_tty
->termios_locked
= *o_ltp_loc
;
906 (*driver
->other
->refcount
)++;
907 if (driver
->subtype
== PTY_TYPE_MASTER
)
910 /* Establish the links in both directions */
916 * All structures have been allocated, so now we install them.
917 * Failures after this point use release_mem to clean up, so
918 * there's no need to null out the local pointers.
920 driver
->table
[idx
] = tty
;
926 tty
->termios
= *tp_loc
;
927 tty
->termios_locked
= *ltp_loc
;
928 (*driver
->refcount
)++;
932 * Structures all installed ... call the ldisc open routines.
933 * If we fail here just call release_mem to clean up. No need
934 * to decrement the use counts, as release_mem doesn't care.
936 if (tty
->ldisc
.open
) {
937 retval
= (tty
->ldisc
.open
)(tty
);
939 goto release_mem_out
;
941 if (o_tty
&& o_tty
->ldisc
.open
) {
942 retval
= (o_tty
->ldisc
.open
)(o_tty
);
944 if (tty
->ldisc
.close
)
945 (tty
->ldisc
.close
)(tty
);
946 goto release_mem_out
;
952 * This fast open can be used if the tty is already open.
953 * No memory is allocated, and the only failures are from
954 * attempting to open a closing tty or attempting multiple
955 * opens on a pty master.
958 if (test_bit(TTY_CLOSING
, &tty
->flags
)) {
962 if (driver
->type
== TTY_DRIVER_TYPE_PTY
&&
963 driver
->subtype
== PTY_TYPE_MASTER
) {
965 * special case for PTY masters: only one open permitted,
966 * and the slave side open count is incremented as well.
975 tty
->driver
= *driver
; /* N.B. why do this every time?? */
980 /* All paths come through here to release the semaphore */
985 /* Release locally allocated memory ... nothing placed in slots */
990 free_tty_struct(o_tty
);
995 free_tty_struct(tty
);
1001 /* call the tty release_mem routine to clean out this slot */
1003 printk("init_dev: ldisc open failed, clearing slot %d\n", idx
);
1004 release_mem(tty
, idx
);
1009 * Releases memory associated with a tty structure, and clears out the
1010 * driver table slots.
1012 static void release_mem(struct tty_struct
*tty
, int idx
)
1014 struct tty_struct
*o_tty
;
1017 if ((o_tty
= tty
->link
) != NULL
) {
1018 o_tty
->driver
.table
[idx
] = NULL
;
1019 if (o_tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
) {
1020 tp
= o_tty
->driver
.termios
[idx
];
1021 o_tty
->driver
.termios
[idx
] = NULL
;
1025 (*o_tty
->driver
.refcount
)--;
1026 free_tty_struct(o_tty
);
1029 tty
->driver
.table
[idx
] = NULL
;
1030 if (tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
) {
1031 tp
= tty
->driver
.termios
[idx
];
1032 tty
->driver
.termios
[idx
] = NULL
;
1036 (*tty
->driver
.refcount
)--;
1037 free_tty_struct(tty
);
1041 * Even releasing the tty structures is a tricky business.. We have
1042 * to be very careful that the structures are all released at the
1043 * same time, as interrupts might otherwise get the wrong pointers.
1045 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1046 * lead to double frees or releasing memory still in use.
1048 static void release_dev(struct file
* filp
)
1050 struct tty_struct
*tty
, *o_tty
;
1051 int pty_master
, tty_closing
, o_tty_closing
, do_sleep
;
1055 tty
= (struct tty_struct
*)filp
->private_data
;
1056 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "release_dev"))
1059 check_tty_count(tty
, "release_dev");
1061 tty_fasync(-1, filp
, 0);
1063 idx
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
1064 pty_master
= (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
1065 tty
->driver
.subtype
== PTY_TYPE_MASTER
);
1068 #ifdef TTY_PARANOIA_CHECK
1069 if (idx
< 0 || idx
>= tty
->driver
.num
) {
1070 printk("release_dev: bad idx when trying to free (%s)\n",
1071 kdevname(tty
->device
));
1074 if (tty
!= tty
->driver
.table
[idx
]) {
1075 printk("release_dev: driver.table[%d] not tty for (%s)\n",
1076 idx
, kdevname(tty
->device
));
1079 if (tty
->termios
!= tty
->driver
.termios
[idx
]) {
1080 printk("release_dev: driver.termios[%d] not termios "
1082 idx
, kdevname(tty
->device
));
1085 if (tty
->termios_locked
!= tty
->driver
.termios_locked
[idx
]) {
1086 printk("release_dev: driver.termios_locked[%d] not "
1087 "termios_locked for (%s)\n",
1088 idx
, kdevname(tty
->device
));
1093 #ifdef TTY_DEBUG_HANGUP
1094 printk("release_dev of %s (tty count=%d)...", tty_name(tty
, buf
),
1098 #ifdef TTY_PARANOIA_CHECK
1099 if (tty
->driver
.other
) {
1100 if (o_tty
!= tty
->driver
.other
->table
[idx
]) {
1101 printk("release_dev: other->table[%d] not o_tty for ("
1103 idx
, kdevname(tty
->device
));
1106 if (o_tty
->termios
!= tty
->driver
.other
->termios
[idx
]) {
1107 printk("release_dev: other->termios[%d] not o_termios "
1109 idx
, kdevname(tty
->device
));
1112 if (o_tty
->termios_locked
!=
1113 tty
->driver
.other
->termios_locked
[idx
]) {
1114 printk("release_dev: other->termios_locked[%d] not "
1115 "o_termios_locked for (%s)\n",
1116 idx
, kdevname(tty
->device
));
1119 if (o_tty
->link
!= tty
) {
1120 printk("release_dev: bad pty pointers\n");
1126 if (tty
->driver
.close
)
1127 tty
->driver
.close(tty
, filp
);
1130 * Sanity check: if tty->count is going to zero, there shouldn't be
1131 * any waiters on tty->read_wait or tty->write_wait. We test the
1132 * wait queues and kick everyone out _before_ actually starting to
1133 * close. This ensures that we won't block while releasing the tty
1136 * The test for the o_tty closing is necessary, since the master and
1137 * slave sides may close in any order. If the slave side closes out
1138 * first, its count will be one, since the master side holds an open.
1139 * Thus this test wouldn't be triggered at the time the slave closes,
1142 * Note that it's possible for the tty to be opened again while we're
1143 * flushing out waiters. By recalculating the closing flags before
1144 * each iteration we avoid any problems.
1147 tty_closing
= tty
->count
<= 1;
1148 o_tty_closing
= o_tty
&&
1149 (o_tty
->count
<= (pty_master
? 1 : 0));
1153 if (waitqueue_active(&tty
->read_wait
)) {
1154 wake_up(&tty
->read_wait
);
1157 if (waitqueue_active(&tty
->write_wait
)) {
1158 wake_up(&tty
->write_wait
);
1162 if (o_tty_closing
) {
1163 if (waitqueue_active(&o_tty
->read_wait
)) {
1164 wake_up(&o_tty
->read_wait
);
1167 if (waitqueue_active(&o_tty
->write_wait
)) {
1168 wake_up(&o_tty
->write_wait
);
1175 printk("release_dev: %s: read/write wait queue active!\n",
1176 tty_name(tty
, buf
));
1181 * The closing flags are now consistent with the open counts on
1182 * both sides, and we've completed the last operation that could
1183 * block, so it's safe to proceed with closing.
1186 if (--o_tty
->count
< 0) {
1187 printk("release_dev: bad pty slave count (%d) for %s\n",
1188 o_tty
->count
, tty_name(o_tty
, buf
));
1192 if (--tty
->count
< 0) {
1193 printk("release_dev: bad tty->count (%d) for %s\n",
1194 tty
->count
, tty_name(tty
, buf
));
1199 * We've decremented tty->count, so we should zero out
1200 * filp->private_data, to break the link between the tty and
1201 * the file descriptor. Otherwise if filp_close() blocks before
1202 * the the file descriptor is removed from the inuse_filp
1203 * list, check_tty_count() could observe a discrepancy and
1204 * printk a warning message to the user.
1206 filp
->private_data
= 0;
1209 * Perform some housekeeping before deciding whether to return.
1211 * Set the TTY_CLOSING flag if this was the last open. In the
1212 * case of a pty we may have to wait around for the other side
1213 * to close, and TTY_CLOSING makes sure we can't be reopened.
1216 set_bit(TTY_CLOSING
, &tty
->flags
);
1218 set_bit(TTY_CLOSING
, &o_tty
->flags
);
1221 * If _either_ side is closing, make sure there aren't any
1222 * processes that still think tty or o_tty is their controlling
1223 * tty. Also, clear redirect if it points to either tty.
1225 if (tty_closing
|| o_tty_closing
) {
1226 struct task_struct
*p
;
1228 read_lock(&tasklist_lock
);
1230 if (p
->tty
== tty
|| (o_tty
&& p
->tty
== o_tty
))
1233 read_unlock(&tasklist_lock
);
1235 if (redirect
== tty
|| (o_tty
&& redirect
== o_tty
))
1239 /* check whether both sides are closing ... */
1240 if (!tty_closing
|| (o_tty
&& !o_tty_closing
))
1243 #ifdef TTY_DEBUG_HANGUP
1244 printk("freeing tty structure...");
1248 * Shutdown the current line discipline, and reset it to N_TTY.
1249 * N.B. why reset ldisc when we're releasing the memory??
1251 if (tty
->ldisc
.close
)
1252 (tty
->ldisc
.close
)(tty
);
1253 tty
->ldisc
= ldiscs
[N_TTY
];
1254 tty
->termios
->c_line
= N_TTY
;
1256 if (o_tty
->ldisc
.close
)
1257 (o_tty
->ldisc
.close
)(o_tty
);
1258 o_tty
->ldisc
= ldiscs
[N_TTY
];
1262 * Make sure that the tty's task queue isn't activated.
1264 run_task_queue(&tq_timer
);
1265 run_schedule_tasks();
1268 * The release_mem function takes care of the details of clearing
1269 * the slots and preserving the termios structure.
1271 release_mem(tty
, idx
);
1275 * tty_open and tty_release keep up the tty count that contains the
1276 * number of opens done on a tty. We cannot use the inode-count, as
1277 * different inodes might point to the same tty.
1279 * Open-counting is needed for pty masters, as well as for keeping
1280 * track of serial lines: DTR is dropped when the last close happens.
1281 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1283 * The termios state of a pty is reset on first open so that
1284 * settings don't persist across reuse.
1286 static int tty_open(struct inode
* inode
, struct file
* filp
)
1288 struct tty_struct
*tty
;
1291 unsigned short saved_flags
;
1294 saved_flags
= filp
->f_flags
;
1296 noctty
= filp
->f_flags
& O_NOCTTY
;
1297 device
= inode
->i_rdev
;
1298 if (device
== TTY_DEV
) {
1301 device
= current
->tty
->device
;
1302 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/tty block */
1306 if (device
== CONSOLE_DEV
) {
1307 extern int fg_console
;
1308 device
= MKDEV(TTY_MAJOR
, fg_console
+ 1);
1312 if (device
== SYSCONS_DEV
) {
1313 struct console
*c
= console_drivers
;
1314 while(c
&& !c
->device
)
1318 device
= c
->device(c
);
1319 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/console block */
1323 if (device
== PTMX_DEV
) {
1324 #ifdef CONFIG_UNIX98_PTYS
1326 /* find a free pty. */
1328 struct tty_driver
*driver
;
1330 /* find a device that is not in use. */
1332 for ( major
= 0 ; major
< UNIX98_NR_MAJORS
; major
++ ) {
1333 driver
= &ptm_driver
[major
];
1334 for (minor
= driver
->minor_start
;
1335 minor
< driver
->minor_start
+ driver
->num
;
1337 device
= MKDEV(driver
->major
, minor
);
1338 if (!init_dev(device
, &tty
)) goto ptmx_found
; /* ok! */
1341 return -EIO
; /* no free ptys */
1343 set_bit(TTY_PTY_LOCK
, &tty
->flags
); /* LOCK THE SLAVE */
1344 minor
-= driver
->minor_start
;
1345 devpts_pty_new(driver
->other
->name_base
+ minor
, MKDEV(driver
->other
->major
, minor
+ driver
->other
->minor_start
));
1346 tty_register_devfs(&pts_driver
[major
], DEVFS_FL_NO_PERSISTENCE
,
1347 pts_driver
[major
].minor_start
+ minor
);
1351 #else /* CONFIG_UNIX_98_PTYS */
1355 #endif /* CONFIG_UNIX_98_PTYS */
1358 retval
= init_dev(device
, &tty
);
1362 #ifdef CONFIG_UNIX98_PTYS
1365 filp
->private_data
= tty
;
1366 file_move(filp
, &tty
->tty_files
);
1367 check_tty_count(tty
, "tty_open");
1368 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
1369 tty
->driver
.subtype
== PTY_TYPE_MASTER
)
1371 #ifdef TTY_DEBUG_HANGUP
1372 printk("opening %s...", tty_name(tty
, buf
));
1374 if (tty
->driver
.open
)
1375 retval
= tty
->driver
.open(tty
, filp
);
1378 filp
->f_flags
= saved_flags
;
1380 if (!retval
&& test_bit(TTY_EXCLUSIVE
, &tty
->flags
) && !suser())
1384 #ifdef TTY_DEBUG_HANGUP
1385 printk("error %d in opening %s...", retval
,
1386 tty_name(tty
, buf
));
1390 if (retval
!= -ERESTARTSYS
)
1392 if (signal_pending(current
))
1396 * Need to reset f_op in case a hangup happened.
1398 filp
->f_op
= &tty_fops
;
1404 tty
->session
== 0) {
1407 task_unlock(current
);
1408 current
->tty_old_pgrp
= 0;
1409 tty
->session
= current
->session
;
1410 tty
->pgrp
= current
->pgrp
;
1412 if ((tty
->driver
.type
== TTY_DRIVER_TYPE_SERIAL
) &&
1413 (tty
->driver
.subtype
== SERIAL_TYPE_CALLOUT
) &&
1414 (tty
->count
== 1)) {
1415 static int nr_warns
;
1417 printk(KERN_WARNING
"tty_io.c: "
1418 "process %d (%s) used obsolete /dev/%s - "
1419 "update software to use /dev/ttyS%d\n",
1420 current
->pid
, current
->comm
,
1421 tty_name(tty
, buf
), TTY_NUMBER(tty
));
1428 static int tty_release(struct inode
* inode
, struct file
* filp
)
1436 /* No kernel lock held - fine */
1437 static unsigned int tty_poll(struct file
* filp
, poll_table
* wait
)
1439 struct tty_struct
* tty
;
1441 tty
= (struct tty_struct
*)filp
->private_data
;
1442 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "tty_poll"))
1445 if (tty
->ldisc
.poll
)
1446 return (tty
->ldisc
.poll
)(tty
, filp
, wait
);
1450 static int tty_fasync(int fd
, struct file
* filp
, int on
)
1452 struct tty_struct
* tty
;
1455 tty
= (struct tty_struct
*)filp
->private_data
;
1456 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "tty_fasync"))
1459 retval
= fasync_helper(fd
, filp
, on
, &tty
->fasync
);
1464 if (!waitqueue_active(&tty
->read_wait
))
1465 tty
->minimum_to_wake
= 1;
1466 if (filp
->f_owner
.pid
== 0) {
1467 filp
->f_owner
.pid
= (-tty
->pgrp
) ? : current
->pid
;
1468 filp
->f_owner
.uid
= current
->uid
;
1469 filp
->f_owner
.euid
= current
->euid
;
1472 if (!tty
->fasync
&& !waitqueue_active(&tty
->read_wait
))
1473 tty
->minimum_to_wake
= N_TTY_BUF_SIZE
;
1478 static int tiocsti(struct tty_struct
*tty
, char * arg
)
1482 if ((current
->tty
!= tty
) && !suser())
1484 if (get_user(ch
, arg
))
1486 tty
->ldisc
.receive_buf(tty
, &ch
, &mbz
, 1);
1490 static int tiocgwinsz(struct tty_struct
*tty
, struct winsize
* arg
)
1492 if (copy_to_user(arg
, &tty
->winsize
, sizeof(*arg
)))
1497 static int tiocswinsz(struct tty_struct
*tty
, struct tty_struct
*real_tty
,
1498 struct winsize
* arg
)
1500 struct winsize tmp_ws
;
1502 if (copy_from_user(&tmp_ws
, arg
, sizeof(*arg
)))
1504 if (!memcmp(&tmp_ws
, &tty
->winsize
, sizeof(*arg
)))
1507 kill_pg(tty
->pgrp
, SIGWINCH
, 1);
1508 if ((real_tty
->pgrp
!= tty
->pgrp
) && (real_tty
->pgrp
> 0))
1509 kill_pg(real_tty
->pgrp
, SIGWINCH
, 1);
1510 tty
->winsize
= tmp_ws
;
1511 real_tty
->winsize
= tmp_ws
;
1515 static int tioccons(struct inode
*inode
,
1516 struct tty_struct
*tty
, struct tty_struct
*real_tty
)
1518 if (inode
->i_rdev
== SYSCONS_DEV
||
1519 inode
->i_rdev
== CONSOLE_DEV
) {
1527 redirect
= real_tty
;
1532 static int fionbio(struct file
*file
, int *arg
)
1536 if (get_user(nonblock
, arg
))
1540 file
->f_flags
|= O_NONBLOCK
;
1542 file
->f_flags
&= ~O_NONBLOCK
;
1546 static int tiocsctty(struct tty_struct
*tty
, int arg
)
1548 if (current
->leader
&&
1549 (current
->session
== tty
->session
))
1552 * The process must be a session leader and
1553 * not have a controlling tty already.
1555 if (!current
->leader
|| current
->tty
)
1557 if (tty
->session
> 0) {
1559 * This tty is already the controlling
1560 * tty for another session group!
1562 if ((arg
== 1) && suser()) {
1566 struct task_struct
*p
;
1568 read_lock(&tasklist_lock
);
1572 read_unlock(&tasklist_lock
);
1578 task_unlock(current
);
1579 current
->tty_old_pgrp
= 0;
1580 tty
->session
= current
->session
;
1581 tty
->pgrp
= current
->pgrp
;
1585 static int tiocgpgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1588 * (tty == real_tty) is a cheap way of
1589 * testing if the tty is NOT a master pty.
1591 if (tty
== real_tty
&& current
->tty
!= real_tty
)
1593 return put_user(real_tty
->pgrp
, arg
);
1596 static int tiocspgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1599 int retval
= tty_check_change(real_tty
);
1605 if (!current
->tty
||
1606 (current
->tty
!= real_tty
) ||
1607 (real_tty
->session
!= current
->session
))
1609 get_user(pgrp
, (pid_t
*) arg
);
1612 if (session_of_pgrp(pgrp
) != current
->session
)
1614 real_tty
->pgrp
= pgrp
;
1618 static int tiocgsid(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1621 * (tty == real_tty) is a cheap way of
1622 * testing if the tty is NOT a master pty.
1624 if (tty
== real_tty
&& current
->tty
!= real_tty
)
1626 if (real_tty
->session
<= 0)
1628 return put_user(real_tty
->session
, arg
);
1631 static int tiocttygstruct(struct tty_struct
*tty
, struct tty_struct
*arg
)
1633 if (copy_to_user(arg
, tty
, sizeof(*arg
)))
1638 static int tiocsetd(struct tty_struct
*tty
, int *arg
)
1642 retval
= get_user(ldisc
, arg
);
1645 return tty_set_ldisc(tty
, ldisc
);
1648 static int send_break(struct tty_struct
*tty
, int duration
)
1650 set_current_state(TASK_INTERRUPTIBLE
);
1652 tty
->driver
.break_ctl(tty
, -1);
1653 if (!signal_pending(current
))
1654 schedule_timeout(duration
);
1655 tty
->driver
.break_ctl(tty
, 0);
1656 if (signal_pending(current
))
1662 * Split this up, as gcc can choke on it otherwise..
1664 int tty_ioctl(struct inode
* inode
, struct file
* file
,
1665 unsigned int cmd
, unsigned long arg
)
1667 struct tty_struct
*tty
, *real_tty
;
1670 tty
= (struct tty_struct
*)file
->private_data
;
1671 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_ioctl"))
1675 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
1676 tty
->driver
.subtype
== PTY_TYPE_MASTER
)
1677 real_tty
= tty
->link
;
1680 * Break handling by driver
1682 if (!tty
->driver
.break_ctl
) {
1686 if (tty
->driver
.ioctl
)
1687 return tty
->driver
.ioctl(tty
, file
, cmd
, arg
);
1690 /* These two ioctl's always return success; even if */
1691 /* the driver doesn't support them. */
1694 if (!tty
->driver
.ioctl
)
1696 retval
= tty
->driver
.ioctl(tty
, file
, cmd
, arg
);
1697 if (retval
== -ENOIOCTLCMD
)
1704 * Factor out some common prep work
1712 retval
= tty_check_change(tty
);
1715 if (cmd
!= TIOCCBRK
) {
1716 tty_wait_until_sent(tty
, 0);
1717 if (signal_pending(current
))
1725 return tiocsti(tty
, (char *)arg
);
1727 return tiocgwinsz(tty
, (struct winsize
*) arg
);
1729 return tiocswinsz(tty
, real_tty
, (struct winsize
*) arg
);
1731 return tioccons(inode
, tty
, real_tty
);
1733 return fionbio(file
, (int *) arg
);
1735 set_bit(TTY_EXCLUSIVE
, &tty
->flags
);
1738 clear_bit(TTY_EXCLUSIVE
, &tty
->flags
);
1741 if (current
->tty
!= tty
)
1743 if (current
->leader
)
1744 disassociate_ctty(0);
1746 current
->tty
= NULL
;
1747 task_unlock(current
);
1750 return tiocsctty(tty
, arg
);
1752 return tiocgpgrp(tty
, real_tty
, (pid_t
*) arg
);
1754 return tiocspgrp(tty
, real_tty
, (pid_t
*) arg
);
1756 return tiocgsid(tty
, real_tty
, (pid_t
*) arg
);
1758 return put_user(tty
->ldisc
.num
, (int *) arg
);
1760 return tiocsetd(tty
, (int *) arg
);
1763 return tioclinux(tty
, arg
);
1765 case TIOCTTYGSTRUCT
:
1766 return tiocttygstruct(tty
, (struct tty_struct
*) arg
);
1771 case TIOCSBRK
: /* Turn break on, unconditionally */
1772 tty
->driver
.break_ctl(tty
, -1);
1775 case TIOCCBRK
: /* Turn break off, unconditionally */
1776 tty
->driver
.break_ctl(tty
, 0);
1778 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1780 * XXX is the above comment correct, or the
1781 * code below correct? Is this ioctl used at
1785 return send_break(tty
, HZ
/4);
1787 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1788 return send_break(tty
, arg
? arg
*(HZ
/10) : HZ
/4);
1790 if (tty
->driver
.ioctl
) {
1791 int retval
= (tty
->driver
.ioctl
)(tty
, file
, cmd
, arg
);
1792 if (retval
!= -ENOIOCTLCMD
)
1795 if (tty
->ldisc
.ioctl
) {
1796 int retval
= (tty
->ldisc
.ioctl
)(tty
, file
, cmd
, arg
);
1797 if (retval
!= -ENOIOCTLCMD
)
1805 * This implements the "Secure Attention Key" --- the idea is to
1806 * prevent trojan horses by killing all processes associated with this
1807 * tty when the user hits the "Secure Attention Key". Required for
1808 * super-paranoid applications --- see the Orange Book for more details.
1810 * This code could be nicer; ideally it should send a HUP, wait a few
1811 * seconds, then send a INT, and then a KILL signal. But you then
1812 * have to coordinate with the init process, since all processes associated
1813 * with the current tty must be dead before the new getty is allowed
1816 * Now, if it would be correct ;-/ The current code has a nasty hole -
1817 * it doesn't catch files in flight. We may send the descriptor to ourselves
1818 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
1820 void do_SAK( struct tty_struct
*tty
)
1825 struct task_struct
*p
;
1832 session
= tty
->session
;
1833 if (tty
->ldisc
.flush_buffer
)
1834 tty
->ldisc
.flush_buffer(tty
);
1835 if (tty
->driver
.flush_buffer
)
1836 tty
->driver
.flush_buffer(tty
);
1837 read_lock(&tasklist_lock
);
1839 if ((p
->tty
== tty
) ||
1840 ((session
> 0) && (p
->session
== session
))) {
1841 send_sig(SIGKILL
, p
, 1);
1846 read_lock(&p
->files
->file_lock
);
1847 /* FIXME: p->files could change */
1848 for (i
=0; i
< p
->files
->max_fds
; i
++) {
1849 filp
= fcheck_files(p
->files
, i
);
1850 if (filp
&& (filp
->f_op
== &tty_fops
) &&
1851 (filp
->private_data
== tty
)) {
1852 send_sig(SIGKILL
, p
, 1);
1856 read_unlock(&p
->files
->file_lock
);
1860 read_unlock(&tasklist_lock
);
1865 * This routine is called out of the software interrupt to flush data
1866 * from the flip buffer to the line discipline.
1868 static void flush_to_ldisc(void *private_
)
1870 struct tty_struct
*tty
= (struct tty_struct
*) private_
;
1874 unsigned long flags
;
1876 if (test_bit(TTY_DONT_FLIP
, &tty
->flags
)) {
1877 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
1880 if (tty
->flip
.buf_num
) {
1881 cp
= tty
->flip
.char_buf
+ TTY_FLIPBUF_SIZE
;
1882 fp
= tty
->flip
.flag_buf
+ TTY_FLIPBUF_SIZE
;
1883 tty
->flip
.buf_num
= 0;
1885 save_flags(flags
); cli();
1886 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
;
1887 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
;
1889 cp
= tty
->flip
.char_buf
;
1890 fp
= tty
->flip
.flag_buf
;
1891 tty
->flip
.buf_num
= 1;
1893 save_flags(flags
); cli();
1894 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
+ TTY_FLIPBUF_SIZE
;
1895 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
+ TTY_FLIPBUF_SIZE
;
1897 count
= tty
->flip
.count
;
1898 tty
->flip
.count
= 0;
1899 restore_flags(flags
);
1901 tty
->ldisc
.receive_buf(tty
, cp
, fp
, count
);
1905 * Routine which returns the baud rate of the tty
1907 * Note that the baud_table needs to be kept in sync with the
1908 * include/asm/termbits.h file.
1910 static int baud_table
[] = {
1911 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
1912 9600, 19200, 38400, 57600, 115200, 230400, 460800,
1914 76800, 153600, 307200, 614400, 921600
1916 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
1917 2500000, 3000000, 3500000, 4000000
1921 static int n_baud_table
= sizeof(baud_table
)/sizeof(int);
1923 int tty_get_baud_rate(struct tty_struct
*tty
)
1925 unsigned int cflag
, i
;
1927 cflag
= tty
->termios
->c_cflag
;
1932 if (i
< 1 || i
+15 >= n_baud_table
)
1933 tty
->termios
->c_cflag
&= ~CBAUDEX
;
1937 if (i
==15 && tty
->alt_speed
) {
1939 printk("Use of setserial/setrocket to set SPD_* flags is deprecated\n");
1942 return(tty
->alt_speed
);
1945 return baud_table
[i
];
1948 void tty_flip_buffer_push(struct tty_struct
*tty
)
1950 if (tty
->low_latency
)
1951 flush_to_ldisc((void *) tty
);
1953 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
1957 * This subroutine initializes a tty structure.
1959 static void initialize_tty_struct(struct tty_struct
*tty
)
1961 memset(tty
, 0, sizeof(struct tty_struct
));
1962 tty
->magic
= TTY_MAGIC
;
1963 tty
->ldisc
= ldiscs
[N_TTY
];
1965 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
;
1966 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
;
1967 tty
->flip
.tqueue
.routine
= flush_to_ldisc
;
1968 tty
->flip
.tqueue
.data
= tty
;
1969 init_MUTEX(&tty
->flip
.pty_sem
);
1970 init_waitqueue_head(&tty
->write_wait
);
1971 init_waitqueue_head(&tty
->read_wait
);
1972 tty
->tq_hangup
.routine
= do_tty_hangup
;
1973 tty
->tq_hangup
.data
= tty
;
1974 sema_init(&tty
->atomic_read
, 1);
1975 spin_lock_init(&tty
->read_lock
);
1976 INIT_LIST_HEAD(&tty
->tty_files
);
1980 * The default put_char routine if the driver did not define one.
1982 void tty_default_put_char(struct tty_struct
*tty
, unsigned char ch
)
1984 tty
->driver
.write(tty
, 0, &ch
, 1);
1988 * Register a tty device described by <driver>, with minor number <minor>.
1990 void tty_register_devfs (struct tty_driver
*driver
, unsigned int flags
,
1993 #ifdef CONFIG_DEVFS_FS
1994 umode_t mode
= S_IFCHR
| S_IRUSR
| S_IWUSR
;
1995 struct tty_struct tty
;
1998 tty
.driver
= *driver
;
1999 tty
.device
= MKDEV (driver
->major
, minor
);
2000 switch (tty
.device
) {
2003 mode
|= S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
2006 if (driver
->major
== PTY_MASTER_MAJOR
)
2007 flags
|= DEVFS_FL_AUTO_OWNER
;
2010 if ( (minor
< driver
->minor_start
) ||
2011 (minor
>= driver
->minor_start
+ driver
->num
) ) {
2012 printk(KERN_ERR
"Attempt to register invalid minor number "
2013 "with devfs (%d:%d).\n", (int)driver
->major
,(int)minor
);
2016 # ifdef CONFIG_UNIX98_PTYS
2017 if ( (driver
->major
>= UNIX98_PTY_SLAVE_MAJOR
) &&
2018 (driver
->major
< UNIX98_PTY_SLAVE_MAJOR
+ UNIX98_NR_MAJORS
) )
2019 flags
|= DEVFS_FL_CURRENT_OWNER
;
2021 devfs_register (NULL
, tty_name (&tty
, buf
), flags
| DEVFS_FL_DEFAULT
,
2022 driver
->major
, minor
, mode
, &tty_fops
, NULL
);
2023 #endif /* CONFIG_DEVFS_FS */
2026 void tty_unregister_devfs (struct tty_driver
*driver
, unsigned minor
)
2028 #ifdef CONFIG_DEVFS_FS
2030 struct tty_struct tty
;
2033 tty
.driver
= *driver
;
2034 tty
.device
= MKDEV(driver
->major
, minor
);
2036 handle
= devfs_find_handle (NULL
, tty_name (&tty
, buf
),
2037 driver
->major
, minor
,
2038 DEVFS_SPECIAL_CHR
, 0);
2039 devfs_unregister (handle
);
2040 #endif /* CONFIG_DEVFS_FS */
2043 EXPORT_SYMBOL(tty_register_devfs
);
2044 EXPORT_SYMBOL(tty_unregister_devfs
);
2047 * Called by a tty driver to register itself.
2049 int tty_register_driver(struct tty_driver
*driver
)
2054 if (driver
->flags
& TTY_DRIVER_INSTALLED
)
2057 error
= devfs_register_chrdev(driver
->major
, driver
->name
, &tty_fops
);
2060 else if(driver
->major
== 0)
2061 driver
->major
= error
;
2063 if (!driver
->put_char
)
2064 driver
->put_char
= tty_default_put_char
;
2067 driver
->next
= tty_drivers
;
2068 if (tty_drivers
) tty_drivers
->prev
= driver
;
2069 tty_drivers
= driver
;
2071 if ( !(driver
->flags
& TTY_DRIVER_NO_DEVFS
) ) {
2072 for(i
= 0; i
< driver
->num
; i
++)
2073 tty_register_devfs(driver
, 0, driver
->minor_start
+ i
);
2075 proc_tty_register_driver(driver
);
2080 * Called by a tty driver to unregister itself.
2082 int tty_unregister_driver(struct tty_driver
*driver
)
2085 struct tty_driver
*p
;
2088 const char *othername
= NULL
;
2090 if (*driver
->refcount
)
2093 for (p
= tty_drivers
; p
; p
= p
->next
) {
2096 else if (p
->major
== driver
->major
)
2097 othername
= p
->name
;
2103 if (othername
== NULL
) {
2104 retval
= devfs_unregister_chrdev(driver
->major
, driver
->name
);
2108 devfs_register_chrdev(driver
->major
, othername
, &tty_fops
);
2111 driver
->prev
->next
= driver
->next
;
2113 tty_drivers
= driver
->next
;
2116 driver
->next
->prev
= driver
->prev
;
2119 * Free the termios and termios_locked structures because
2120 * we don't want to get memory leaks when modular tty
2121 * drivers are removed from the kernel.
2123 for (i
= 0; i
< driver
->num
; i
++) {
2124 tp
= driver
->termios
[i
];
2126 driver
->termios
[i
] = NULL
;
2129 tp
= driver
->termios_locked
[i
];
2131 driver
->termios_locked
[i
] = NULL
;
2134 tty_unregister_devfs(driver
, driver
->minor_start
+ i
);
2136 proc_tty_unregister_driver(driver
);
2142 * Initialize the console device. This is called *early*, so
2143 * we can't necessarily depend on lots of kernel help here.
2144 * Just do some early initializations, and do the complex setup
2147 void __init
console_init(void)
2149 /* Setup the default TTY line discipline. */
2150 memset(ldiscs
, 0, sizeof(ldiscs
));
2151 (void) tty_register_ldisc(N_TTY
, &tty_ldisc_N_TTY
);
2154 * Set up the standard termios. Individual tty drivers may
2155 * deviate from this; this is used as a template.
2157 memset(&tty_std_termios
, 0, sizeof(struct termios
));
2158 memcpy(tty_std_termios
.c_cc
, INIT_C_CC
, NCCS
);
2159 tty_std_termios
.c_iflag
= ICRNL
| IXON
;
2160 tty_std_termios
.c_oflag
= OPOST
| ONLCR
;
2161 tty_std_termios
.c_cflag
= B38400
| CS8
| CREAD
| HUPCL
;
2162 tty_std_termios
.c_lflag
= ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
|
2163 ECHOCTL
| ECHOKE
| IEXTEN
;
2166 * set up the console device so that later boot sequences can
2167 * inform about problems etc..
2172 #ifdef CONFIG_SERIAL_CONSOLE
2173 #if (defined(CONFIG_8xx) || defined(CONFIG_8260))
2175 #elif defined(CONFIG_SERIAL)
2176 serial_console_init();
2177 #endif /* CONFIG_8xx */
2178 #ifdef CONFIG_SGI_SERIAL
2179 sgi_serial_console_init();
2181 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2182 vme_scc_console_init();
2184 #if defined(CONFIG_SERIAL167)
2185 serial167_console_init();
2187 #if defined(CONFIG_SH_SCI)
2197 #ifdef CONFIG_SERIAL_21285_CONSOLE
2198 rs285_console_init();
2200 #ifdef CONFIG_SERIAL_SA1100_CONSOLE
2201 sa1100_rs_console_init();
2203 #ifdef CONFIG_SERIAL_AMBA_CONSOLE
2204 ambauart_console_init();
2208 static struct tty_driver dev_tty_driver
, dev_syscons_driver
;
2209 #ifdef CONFIG_UNIX98_PTYS
2210 static struct tty_driver dev_ptmx_driver
;
2213 static struct tty_driver dev_console_driver
;
2217 * Ok, now we can initialize the rest of the tty devices and can count
2218 * on memory allocations, interrupts etc..
2220 void __init
tty_init(void)
2222 if (sizeof(struct tty_struct
) > PAGE_SIZE
)
2223 panic("size of tty structure > PAGE_SIZE!");
2226 * dev_tty_driver and dev_console_driver are actually magic
2227 * devices which get redirected at open time. Nevertheless,
2228 * we register them so that register_chrdev is called
2231 memset(&dev_tty_driver
, 0, sizeof(struct tty_driver
));
2232 dev_tty_driver
.magic
= TTY_DRIVER_MAGIC
;
2233 dev_tty_driver
.driver_name
= "/dev/tty";
2234 dev_tty_driver
.name
= dev_tty_driver
.driver_name
+ 5;
2235 dev_tty_driver
.name_base
= 0;
2236 dev_tty_driver
.major
= TTYAUX_MAJOR
;
2237 dev_tty_driver
.minor_start
= 0;
2238 dev_tty_driver
.num
= 1;
2239 dev_tty_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2240 dev_tty_driver
.subtype
= SYSTEM_TYPE_TTY
;
2242 if (tty_register_driver(&dev_tty_driver
))
2243 panic("Couldn't register /dev/tty driver\n");
2245 dev_syscons_driver
= dev_tty_driver
;
2246 dev_syscons_driver
.driver_name
= "/dev/console";
2247 dev_syscons_driver
.name
= dev_syscons_driver
.driver_name
+ 5;
2248 dev_syscons_driver
.major
= TTYAUX_MAJOR
;
2249 dev_syscons_driver
.minor_start
= 1;
2250 dev_syscons_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2251 dev_syscons_driver
.subtype
= SYSTEM_TYPE_SYSCONS
;
2253 if (tty_register_driver(&dev_syscons_driver
))
2254 panic("Couldn't register /dev/console driver\n");
2256 /* console calls tty_register_driver() before kmalloc() works.
2257 * Thus, we can't devfs_register() then. Do so now, instead.
2263 #ifdef CONFIG_UNIX98_PTYS
2264 dev_ptmx_driver
= dev_tty_driver
;
2265 dev_ptmx_driver
.driver_name
= "/dev/ptmx";
2266 dev_ptmx_driver
.name
= dev_ptmx_driver
.driver_name
+ 5;
2267 dev_ptmx_driver
.major
= MAJOR(PTMX_DEV
);
2268 dev_ptmx_driver
.minor_start
= MINOR(PTMX_DEV
);
2269 dev_ptmx_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2270 dev_ptmx_driver
.subtype
= SYSTEM_TYPE_SYSPTMX
;
2272 if (tty_register_driver(&dev_ptmx_driver
))
2273 panic("Couldn't register /dev/ptmx driver\n");
2277 dev_console_driver
= dev_tty_driver
;
2278 dev_console_driver
.driver_name
= "/dev/vc/0";
2279 dev_console_driver
.name
= dev_console_driver
.driver_name
+ 5;
2280 dev_console_driver
.major
= TTY_MAJOR
;
2281 dev_console_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2282 dev_console_driver
.subtype
= SYSTEM_TYPE_CONSOLE
;
2284 if (tty_register_driver(&dev_console_driver
))
2285 panic("Couldn't register /dev/tty0 driver\n");
2289 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2292 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2295 #ifdef CONFIG_COMPUTONE
2298 #ifdef CONFIG_MAC_SERIAL
2301 #ifdef CONFIG_ROCKETPORT
2304 #ifdef CONFIG_SERIAL167
2307 #ifdef CONFIG_CYCLADES
2310 #ifdef CONFIG_STALLION
2313 #ifdef CONFIG_ISTALLION
2319 #ifdef CONFIG_DIGIEPCA
2322 #ifdef CONFIG_RISCOM8
2325 #ifdef CONFIG_SPECIALIX
2334 #if (defined(CONFIG_8xx) || defined(CONFIG_8260))
2336 #endif /* CONFIG_8xx */
2338 #ifdef CONFIG_MOXA_SMARTIO
2341 #ifdef CONFIG_MOXA_INTELLIO