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 externvoidcon_init_devfs(void); 105 externintrio_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 externstruct tty_driver ptm_driver
[];/* Unix98 pty masters; for /dev/ptmx */ 123 externstruct 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 voidinitialize_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 inttty_poll(struct file
*, poll_table
*); 137 static inttty_open(struct inode
*,struct file
*); 138 static inttty_release(struct inode
*,struct file
*); 139 inttty_ioctl(struct inode
* inode
,struct file
* file
, 140 unsigned int cmd
,unsigned long arg
); 141 static inttty_fasync(int fd
,struct file
* filp
,int on
); 142 externintsx_init(void); 143 externintvme_scc_init(void); 144 externlongvme_scc_console_init(void); 145 externintserial167_init(void); 146 externlongserial167_console_init(void); 147 externvoidconsole_8xx_init(void); 148 externintrs_8xx_init(void); 149 externvoidhwc_console_init(void); 150 externvoidcon3215_init(void); 151 externvoidrs285_console_init(void); 152 externvoidsa1100_rs_console_init(void); 153 externvoidsgi_serial_console_init(void); 154 externvoidsci_console_init(void); 157 #define MIN(a,b) ((a) < (b) ? (a) : (b)) 160 #define MAX(a,b) ((a) < (b) ? (b) : (a)) 163 staticinlinestruct 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 staticinlinevoidfree_tty_struct(struct tty_struct
*tty
) 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 inlineinttty_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 intcheck_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 inttty_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 inttty_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 return0;/* We are already in the desired discipline */ 292 o_ldisc
= tty
->ldisc
; 294 tty_wait_until_sent(tty
,0); 296 /* Shutdown the current discipline. */ 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 inttty_check_change(struct tty_struct
* tty
) 356 if(current
->tty
!= tty
) 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 inthung_up_tty_poll(struct file
* filp
, poll_table
* wait
) 393 return POLLIN
| POLLOUT
| POLLERR
| POLLHUP
| POLLRDNORM
| POLLWRNORM
; 396 static inthung_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 voiddo_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
) { 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
); 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 voidtty_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 voidtty_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 inttty_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 voiddisassociate_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 voidwait_for_keypress(void) 609 struct console
*c
= console_drivers
; 610 if(c
) c
->wait_key(c
); 613 voidstop_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
); 624 (tty
->driver
.stop
)(tty
); 627 voidstart_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 staticinline 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; 703 if(down_interruptible(&tty
->atomic_write
)) { 706 if(test_bit(TTY_NO_WRITE_SPLIT
, &tty
->flags
) ) { 708 written
=write(tty
, file
, buf
, count
); 712 unsigned long size
=MAX(PAGE_SIZE
*2,16384); 716 ret
=write(tty
, file
, buf
, size
); 726 if(signal_pending(current
)) 728 if(current
->need_resched
) 733 file
->f_dentry
->d_inode
->i_mtime
= CURRENT_TIME
; 736 up(&tty
->atomic_write
); 741 static ssize_t
tty_write(struct file
* file
,const char* buf
,size_t count
, 745 struct tty_struct
* tty
; 748 /* Can't seek (pwrite) on ttys. */ 749 if(ppos
!= &file
->f_pos
) 753 * For now, we redirect writes from /dev/console as 756 inode
= file
->f_dentry
->d_inode
; 757 is_console
= (inode
->i_rdev
== SYSCONS_DEV
|| 758 inode
->i_rdev
== CONSOLE_DEV
); 760 if(is_console
&& redirect
) 763 tty
= (struct tty_struct
*)file
->private_data
; 764 if(tty_paranoia_check(tty
, inode
->i_rdev
,"tty_write")) 766 if(!tty
|| !tty
->driver
.write
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
))) 769 if(!is_console
&&L_TOSTOP(tty
) && (tty
->pgrp
>0) && 770 (current
->tty
== tty
) && (tty
->pgrp
!= current
->pgrp
)) { 771 if(is_orphaned_pgrp(current
->pgrp
)) 773 if(!is_ignored(SIGTTOU
)) { 774 (void)kill_pg(current
->pgrp
, SIGTTOU
,1); 779 if(!tty
->ldisc
.write
) 781 returndo_tty_write(tty
->ldisc
.write
, tty
, file
, 782 (const unsigned char*)buf
, count
); 785 /* Semaphore to protect creating and releasing a tty */ 786 staticDECLARE_MUTEX(tty_sem
); 788 static voiddown_tty_sem(int index
) 793 static voidup_tty_sem(int index
) 798 static voidrelease_mem(struct tty_struct
*tty
,int idx
); 801 * WSH 06/09/97: Rewritten to remove races and properly clean up after a 802 * failed open. The new code protects the open with a semaphore, so it's 803 * really quite straightforward. The semaphore locking can probably be 804 * relaxed for the (most common) case of reopening a tty. 806 static intinit_dev(kdev_t device
,struct tty_struct
**ret_tty
) 808 struct tty_struct
*tty
, *o_tty
; 809 struct termios
*tp
, **tp_loc
, *o_tp
, **o_tp_loc
; 810 struct termios
*ltp
, **ltp_loc
, *o_ltp
, **o_ltp_loc
; 811 struct tty_driver
*driver
; 815 driver
=get_tty_driver(device
); 819 idx
=MINOR(device
) - driver
->minor_start
; 822 * Check whether we need to acquire the tty semaphore to avoid 823 * race conditions. For now, play it safe. 827 /* check whether we're reopening an existing tty */ 828 tty
= driver
->table
[idx
]; 829 if(tty
)goto fast_track
; 832 * First time open is complex, especially for PTY devices. 833 * This code guarantees that either everything succeeds and the 834 * TTY is ready for operation, or else the table slots are vacated 835 * and the allocated memory released. (Except that the termios 836 * and locked termios may be retained.) 843 tty
=alloc_tty_struct(); 846 initialize_tty_struct(tty
); 847 tty
->device
= device
; 848 tty
->driver
= *driver
; 850 tp_loc
= &driver
->termios
[idx
]; 852 tp
= (struct termios
*)kmalloc(sizeof(struct termios
), 856 *tp
= driver
->init_termios
; 859 ltp_loc
= &driver
->termios_locked
[idx
]; 861 ltp
= (struct termios
*)kmalloc(sizeof(struct termios
), 865 memset(ltp
,0,sizeof(struct termios
)); 868 if(driver
->type
== TTY_DRIVER_TYPE_PTY
) { 869 o_tty
=alloc_tty_struct(); 872 initialize_tty_struct(o_tty
); 873 o_tty
->device
= (kdev_t
)MKDEV(driver
->other
->major
, 874 driver
->other
->minor_start
+ idx
); 875 o_tty
->driver
= *driver
->other
; 877 o_tp_loc
= &driver
->other
->termios
[idx
]; 879 o_tp
= (struct termios
*) 880 kmalloc(sizeof(struct termios
), GFP_KERNEL
); 883 *o_tp
= driver
->other
->init_termios
; 886 o_ltp_loc
= &driver
->other
->termios_locked
[idx
]; 888 o_ltp
= (struct termios
*) 889 kmalloc(sizeof(struct termios
), GFP_KERNEL
); 892 memset(o_ltp
,0,sizeof(struct termios
)); 896 * Everything allocated ... set up the o_tty structure. 898 driver
->other
->table
[idx
] = o_tty
; 903 o_tty
->termios
= *o_tp_loc
; 904 o_tty
->termios_locked
= *o_ltp_loc
; 905 (*driver
->other
->refcount
)++; 906 if(driver
->subtype
== PTY_TYPE_MASTER
) 909 /* Establish the links in both directions */ 915 * All structures have been allocated, so now we install them. 916 * Failures after this point use release_mem to clean up, so 917 * there's no need to null out the local pointers. 919 driver
->table
[idx
] = tty
; 925 tty
->termios
= *tp_loc
; 926 tty
->termios_locked
= *ltp_loc
; 927 (*driver
->refcount
)++; 931 * Structures all installed ... call the ldisc open routines. 932 * If we fail here just call release_mem to clean up. No need 933 * to decrement the use counts, as release_mem doesn't care. 935 if(tty
->ldisc
.open
) { 936 retval
= (tty
->ldisc
.open
)(tty
); 938 goto release_mem_out
; 940 if(o_tty
&& o_tty
->ldisc
.open
) { 941 retval
= (o_tty
->ldisc
.open
)(o_tty
); 944 (tty
->ldisc
.close
)(tty
); 945 goto release_mem_out
; 951 * This fast open can be used if the tty is already open. 952 * No memory is allocated, and the only failures are from 953 * attempting to open a closing tty or attempting multiple 954 * opens on a pty master. 957 if(test_bit(TTY_CLOSING
, &tty
->flags
)) { 961 if(driver
->type
== TTY_DRIVER_TYPE_PTY
&& 962 driver
->subtype
== PTY_TYPE_MASTER
) { 964 * special case for PTY masters: only one open permitted, 965 * and the slave side open count is incremented as well. 974 tty
->driver
= *driver
;/* N.B. why do this every time?? */ 979 /* All paths come through here to release the semaphore */ 984 /* Release locally allocated memory ... nothing placed in slots */ 989 free_tty_struct(o_tty
); 994 free_tty_struct(tty
); 1000 /* call the tty release_mem routine to clean out this slot */ 1002 printk("init_dev: ldisc open failed, clearing slot %d\n", idx
); 1003 release_mem(tty
, idx
); 1008 * Releases memory associated with a tty structure, and clears out the 1009 * driver table slots. 1011 static voidrelease_mem(struct tty_struct
*tty
,int idx
) 1013 struct tty_struct
*o_tty
; 1016 if((o_tty
= tty
->link
) != NULL
) { 1017 o_tty
->driver
.table
[idx
] = NULL
; 1018 if(o_tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
) { 1019 tp
= o_tty
->driver
.termios
[idx
]; 1020 o_tty
->driver
.termios
[idx
] = NULL
; 1024 (*o_tty
->driver
.refcount
)--; 1025 free_tty_struct(o_tty
); 1028 tty
->driver
.table
[idx
] = NULL
; 1029 if(tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
) { 1030 tp
= tty
->driver
.termios
[idx
]; 1031 tty
->driver
.termios
[idx
] = NULL
; 1035 (*tty
->driver
.refcount
)--; 1036 free_tty_struct(tty
); 1040 * Even releasing the tty structures is a tricky business.. We have 1041 * to be very careful that the structures are all released at the 1042 * same time, as interrupts might otherwise get the wrong pointers. 1044 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could 1045 * lead to double frees or releasing memory still in use. 1047 static voidrelease_dev(struct file
* filp
) 1049 struct tty_struct
*tty
, *o_tty
; 1050 int pty_master
, tty_closing
, o_tty_closing
, do_sleep
; 1054 tty
= (struct tty_struct
*)filp
->private_data
; 1055 if(tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
,"release_dev")) 1058 check_tty_count(tty
,"release_dev"); 1060 tty_fasync(-1, filp
,0); 1062 idx
=MINOR(tty
->device
) - tty
->driver
.minor_start
; 1063 pty_master
= (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&& 1064 tty
->driver
.subtype
== PTY_TYPE_MASTER
); 1067 #ifdef TTY_PARANOIA_CHECK 1068 if(idx
<0|| idx
>= tty
->driver
.num
) { 1069 printk("release_dev: bad idx when trying to free (%s)\n", 1070 kdevname(tty
->device
)); 1073 if(tty
!= tty
->driver
.table
[idx
]) { 1074 printk("release_dev: driver.table[%d] not tty for (%s)\n", 1075 idx
,kdevname(tty
->device
)); 1078 if(tty
->termios
!= tty
->driver
.termios
[idx
]) { 1079 printk("release_dev: driver.termios[%d] not termios " 1081 idx
,kdevname(tty
->device
)); 1084 if(tty
->termios_locked
!= tty
->driver
.termios_locked
[idx
]) { 1085 printk("release_dev: driver.termios_locked[%d] not " 1086 "termios_locked for (%s)\n", 1087 idx
,kdevname(tty
->device
)); 1092 #ifdef TTY_DEBUG_HANGUP 1093 printk("release_dev of %s (tty count=%d)...",tty_name(tty
, buf
), 1097 #ifdef TTY_PARANOIA_CHECK 1098 if(tty
->driver
.other
) { 1099 if(o_tty
!= tty
->driver
.other
->table
[idx
]) { 1100 printk("release_dev: other->table[%d] not o_tty for (" 1102 idx
,kdevname(tty
->device
)); 1105 if(o_tty
->termios
!= tty
->driver
.other
->termios
[idx
]) { 1106 printk("release_dev: other->termios[%d] not o_termios " 1108 idx
,kdevname(tty
->device
)); 1111 if(o_tty
->termios_locked
!= 1112 tty
->driver
.other
->termios_locked
[idx
]) { 1113 printk("release_dev: other->termios_locked[%d] not " 1114 "o_termios_locked for (%s)\n", 1115 idx
,kdevname(tty
->device
)); 1118 if(o_tty
->link
!= tty
) { 1119 printk("release_dev: bad pty pointers\n"); 1125 if(tty
->driver
.close
) 1126 tty
->driver
.close(tty
, filp
); 1129 * Sanity check: if tty->count is going to zero, there shouldn't be 1130 * any waiters on tty->read_wait or tty->write_wait. We test the 1131 * wait queues and kick everyone out _before_ actually starting to 1132 * close. This ensures that we won't block while releasing the tty 1135 * The test for the o_tty closing is necessary, since the master and 1136 * slave sides may close in any order. If the slave side closes out 1137 * first, its count will be one, since the master side holds an open. 1138 * Thus this test wouldn't be triggered at the time the slave closes, 1141 * Note that it's possible for the tty to be opened again while we're 1142 * flushing out waiters. By recalculating the closing flags before 1143 * each iteration we avoid any problems. 1146 tty_closing
= tty
->count
<=1; 1147 o_tty_closing
= o_tty
&& 1148 (o_tty
->count
<= (pty_master
?1:0)); 1152 if(waitqueue_active(&tty
->read_wait
)) { 1153 wake_up(&tty
->read_wait
); 1156 if(waitqueue_active(&tty
->write_wait
)) { 1157 wake_up(&tty
->write_wait
); 1162 if(waitqueue_active(&o_tty
->read_wait
)) { 1163 wake_up(&o_tty
->read_wait
); 1166 if(waitqueue_active(&o_tty
->write_wait
)) { 1167 wake_up(&o_tty
->write_wait
); 1174 printk("release_dev: %s: read/write wait queue active!\n", 1175 tty_name(tty
, buf
)); 1180 * The closing flags are now consistent with the open counts on 1181 * both sides, and we've completed the last operation that could 1182 * block, so it's safe to proceed with closing. 1185 if(--o_tty
->count
<0) { 1186 printk("release_dev: bad pty slave count (%d) for %s\n", 1187 o_tty
->count
,tty_name(o_tty
, buf
)); 1191 if(--tty
->count
<0) { 1192 printk("release_dev: bad tty->count (%d) for %s\n", 1193 tty
->count
,tty_name(tty
, buf
)); 1198 * We've decremented tty->count, so we should zero out 1199 * filp->private_data, to break the link between the tty and 1200 * the file descriptor. Otherwise if filp_close() blocks before 1201 * the the file descriptor is removed from the inuse_filp 1202 * list, check_tty_count() could observe a discrepancy and 1203 * printk a warning message to the user. 1205 filp
->private_data
=0; 1208 * Perform some housekeeping before deciding whether to return. 1210 * Set the TTY_CLOSING flag if this was the last open. In the 1211 * case of a pty we may have to wait around for the other side 1212 * to close, and TTY_CLOSING makes sure we can't be reopened. 1215 set_bit(TTY_CLOSING
, &tty
->flags
); 1217 set_bit(TTY_CLOSING
, &o_tty
->flags
); 1220 * If _either_ side is closing, make sure there aren't any 1221 * processes that still think tty or o_tty is their controlling 1222 * tty. Also, clear redirect if it points to either tty. 1224 if(tty_closing
|| o_tty_closing
) { 1225 struct task_struct
*p
; 1227 read_lock(&tasklist_lock
); 1229 if(p
->tty
== tty
|| (o_tty
&& p
->tty
== o_tty
)) 1232 read_unlock(&tasklist_lock
); 1234 if(redirect
== tty
|| (o_tty
&& redirect
== o_tty
)) 1238 /* check whether both sides are closing ... */ 1239 if(!tty_closing
|| (o_tty
&& !o_tty_closing
)) 1242 #ifdef TTY_DEBUG_HANGUP 1243 printk("freeing tty structure..."); 1247 * Shutdown the current line discipline, and reset it to N_TTY. 1248 * N.B. why reset ldisc when we're releasing the memory?? 1250 if(tty
->ldisc
.close
) 1251 (tty
->ldisc
.close
)(tty
); 1252 tty
->ldisc
= ldiscs
[N_TTY
]; 1253 tty
->termios
->c_line
= N_TTY
; 1255 if(o_tty
->ldisc
.close
) 1256 (o_tty
->ldisc
.close
)(o_tty
); 1257 o_tty
->ldisc
= ldiscs
[N_TTY
]; 1261 * Make sure that the tty's task queue isn't activated. 1263 run_task_queue(&tq_timer
); 1264 flush_scheduled_tasks(); 1267 * The release_mem function takes care of the details of clearing 1268 * the slots and preserving the termios structure. 1270 release_mem(tty
, idx
); 1274 * tty_open and tty_release keep up the tty count that contains the 1275 * number of opens done on a tty. We cannot use the inode-count, as 1276 * different inodes might point to the same tty. 1278 * Open-counting is needed for pty masters, as well as for keeping 1279 * track of serial lines: DTR is dropped when the last close happens. 1280 * (This is not done solely through tty->count, now. - Ted 1/27/92) 1282 * The termios state of a pty is reset on first open so that 1283 * settings don't persist across reuse. 1285 static inttty_open(struct inode
* inode
,struct file
* filp
) 1287 struct tty_struct
*tty
; 1290 unsigned short saved_flags
; 1293 saved_flags
= filp
->f_flags
; 1295 noctty
= filp
->f_flags
& O_NOCTTY
; 1296 device
= inode
->i_rdev
; 1297 if(device
== TTY_DEV
) { 1300 device
= current
->tty
->device
; 1301 filp
->f_flags
|= O_NONBLOCK
;/* Don't let /dev/tty block */ 1305 if(device
== CONSOLE_DEV
) { 1306 externint fg_console
; 1307 device
=MKDEV(TTY_MAJOR
, fg_console
+1); 1311 if(device
== SYSCONS_DEV
) { 1312 struct console
*c
= console_drivers
; 1313 while(c
&& !c
->device
) 1317 device
= c
->device(c
); 1318 filp
->f_flags
|= O_NONBLOCK
;/* Don't let /dev/console block */ 1322 if(device
== PTMX_DEV
) { 1323 #ifdef CONFIG_UNIX98_PTYS 1325 /* find a free pty. */ 1327 struct tty_driver
*driver
; 1329 /* find a device that is not in use. */ 1331 for( major
=0; major
< UNIX98_NR_MAJORS
; major
++ ) { 1332 driver
= &ptm_driver
[major
]; 1333 for(minor
= driver
->minor_start
; 1334 minor
< driver
->minor_start
+ driver
->num
; 1336 device
=MKDEV(driver
->major
, minor
); 1337 if(!init_dev(device
, &tty
))goto ptmx_found
;/* ok! */ 1340 return-EIO
;/* no free ptys */ 1342 set_bit(TTY_PTY_LOCK
, &tty
->flags
);/* LOCK THE SLAVE */ 1343 minor
-= driver
->minor_start
; 1344 devpts_pty_new(driver
->other
->name_base
+ minor
,MKDEV(driver
->other
->major
, minor
+ driver
->other
->minor_start
)); 1345 tty_register_devfs(&pts_driver
[major
], DEVFS_FL_NO_PERSISTENCE
, 1346 pts_driver
[major
].minor_start
+ minor
); 1350 #else/* CONFIG_UNIX_98_PTYS */ 1354 #endif/* CONFIG_UNIX_98_PTYS */ 1357 retval
=init_dev(device
, &tty
); 1361 #ifdef CONFIG_UNIX98_PTYS 1364 filp
->private_data
= tty
; 1365 file_move(filp
, &tty
->tty_files
); 1366 check_tty_count(tty
,"tty_open"); 1367 if(tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&& 1368 tty
->driver
.subtype
== PTY_TYPE_MASTER
) 1370 #ifdef TTY_DEBUG_HANGUP 1371 printk("opening %s...",tty_name(tty
, buf
)); 1373 if(tty
->driver
.open
) 1374 retval
= tty
->driver
.open(tty
, filp
); 1377 filp
->f_flags
= saved_flags
; 1379 if(!retval
&&test_bit(TTY_EXCLUSIVE
, &tty
->flags
) && !suser()) 1383 #ifdef TTY_DEBUG_HANGUP 1384 printk("error %d in opening %s...", retval
, 1385 tty_name(tty
, buf
)); 1389 if(retval
!= -ERESTARTSYS
) 1391 if(signal_pending(current
)) 1395 * Need to reset f_op in case a hangup happened. 1397 filp
->f_op
= &tty_fops
; 1406 task_unlock(current
); 1407 current
->tty_old_pgrp
=0; 1408 tty
->session
= current
->session
; 1409 tty
->pgrp
= current
->pgrp
; 1411 if((tty
->driver
.type
== TTY_DRIVER_TYPE_SERIAL
) && 1412 (tty
->driver
.subtype
== SERIAL_TYPE_CALLOUT
) && 1414 static int nr_warns
; 1416 printk(KERN_WARNING
"tty_io.c: " 1417 "process %d (%s) used obsolete /dev/%s - " 1418 "update software to use /dev/ttyS%d\n", 1419 current
->pid
, current
->comm
, 1420 tty_name(tty
, buf
),TTY_NUMBER(tty
)); 1427 static inttty_release(struct inode
* inode
,struct file
* filp
) 1435 /* No kernel lock held - fine */ 1436 static unsigned inttty_poll(struct file
* filp
, poll_table
* wait
) 1438 struct tty_struct
* tty
; 1440 tty
= (struct tty_struct
*)filp
->private_data
; 1441 if(tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
,"tty_poll")) 1445 return(tty
->ldisc
.poll
)(tty
, filp
, wait
); 1449 static inttty_fasync(int fd
,struct file
* filp
,int on
) 1451 struct tty_struct
* tty
; 1454 tty
= (struct tty_struct
*)filp
->private_data
; 1455 if(tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
,"tty_fasync")) 1458 retval
=fasync_helper(fd
, filp
, on
, &tty
->fasync
); 1463 if(!waitqueue_active(&tty
->read_wait
)) 1464 tty
->minimum_to_wake
=1; 1465 if(filp
->f_owner
.pid
==0) { 1466 filp
->f_owner
.pid
= (-tty
->pgrp
) ? : current
->pid
; 1467 filp
->f_owner
.uid
= current
->uid
; 1468 filp
->f_owner
.euid
= current
->euid
; 1471 if(!tty
->fasync
&& !waitqueue_active(&tty
->read_wait
)) 1472 tty
->minimum_to_wake
= N_TTY_BUF_SIZE
; 1477 static inttiocsti(struct tty_struct
*tty
,char* arg
) 1481 if((current
->tty
!= tty
) && !suser()) 1483 if(get_user(ch
, arg
)) 1485 tty
->ldisc
.receive_buf(tty
, &ch
, &mbz
,1); 1489 static inttiocgwinsz(struct tty_struct
*tty
,struct winsize
* arg
) 1491 if(copy_to_user(arg
, &tty
->winsize
,sizeof(*arg
))) 1496 static inttiocswinsz(struct tty_struct
*tty
,struct tty_struct
*real_tty
, 1497 struct winsize
* arg
) 1499 struct winsize tmp_ws
; 1501 if(copy_from_user(&tmp_ws
, arg
,sizeof(*arg
))) 1503 if(!memcmp(&tmp_ws
, &tty
->winsize
,sizeof(*arg
))) 1506 kill_pg(tty
->pgrp
, SIGWINCH
,1); 1507 if((real_tty
->pgrp
!= tty
->pgrp
) && (real_tty
->pgrp
>0)) 1508 kill_pg(real_tty
->pgrp
, SIGWINCH
,1); 1509 tty
->winsize
= tmp_ws
; 1510 real_tty
->winsize
= tmp_ws
; 1514 static inttioccons(struct inode
*inode
, 1515 struct tty_struct
*tty
,struct tty_struct
*real_tty
) 1517 if(inode
->i_rdev
== SYSCONS_DEV
|| 1518 inode
->i_rdev
== CONSOLE_DEV
) { 1526 redirect
= real_tty
; 1531 static intfionbio(struct file
*file
,int*arg
) 1535 if(get_user(nonblock
, arg
)) 1539 file
->f_flags
|= O_NONBLOCK
; 1541 file
->f_flags
&= ~O_NONBLOCK
; 1545 static inttiocsctty(struct tty_struct
*tty
,int arg
) 1547 if(current
->leader
&& 1548 (current
->session
== tty
->session
)) 1551 * The process must be a session leader and 1552 * not have a controlling tty already. 1554 if(!current
->leader
|| current
->tty
) 1556 if(tty
->session
>0) { 1558 * This tty is already the controlling 1559 * tty for another session group! 1561 if((arg
==1) &&suser()) { 1565 struct task_struct
*p
; 1567 read_lock(&tasklist_lock
); 1571 read_unlock(&tasklist_lock
); 1577 task_unlock(current
); 1578 current
->tty_old_pgrp
=0; 1579 tty
->session
= current
->session
; 1580 tty
->pgrp
= current
->pgrp
; 1584 static inttiocgpgrp(struct tty_struct
*tty
,struct tty_struct
*real_tty
, pid_t
*arg
) 1587 * (tty == real_tty) is a cheap way of 1588 * testing if the tty is NOT a master pty. 1590 if(tty
== real_tty
&& current
->tty
!= real_tty
) 1592 returnput_user(real_tty
->pgrp
, arg
); 1595 static inttiocspgrp(struct tty_struct
*tty
,struct tty_struct
*real_tty
, pid_t
*arg
) 1598 int retval
=tty_check_change(real_tty
); 1605 (current
->tty
!= real_tty
) || 1606 (real_tty
->session
!= current
->session
)) 1608 get_user(pgrp
, (pid_t
*) arg
); 1611 if(session_of_pgrp(pgrp
) != current
->session
) 1613 real_tty
->pgrp
= pgrp
; 1617 static inttiocgsid(struct tty_struct
*tty
,struct tty_struct
*real_tty
, pid_t
*arg
) 1620 * (tty == real_tty) is a cheap way of 1621 * testing if the tty is NOT a master pty. 1623 if(tty
== real_tty
&& current
->tty
!= real_tty
) 1625 if(real_tty
->session
<=0) 1627 returnput_user(real_tty
->session
, arg
); 1630 static inttiocttygstruct(struct tty_struct
*tty
,struct tty_struct
*arg
) 1632 if(copy_to_user(arg
, tty
,sizeof(*arg
))) 1637 static inttiocsetd(struct tty_struct
*tty
,int*arg
) 1641 retval
=get_user(ldisc
, arg
); 1644 returntty_set_ldisc(tty
, ldisc
); 1647 static intsend_break(struct tty_struct
*tty
,int duration
) 1649 set_current_state(TASK_INTERRUPTIBLE
); 1651 tty
->driver
.break_ctl(tty
, -1); 1652 if(!signal_pending(current
)) 1653 schedule_timeout(duration
); 1654 tty
->driver
.break_ctl(tty
,0); 1655 if(signal_pending(current
)) 1661 * Split this up, as gcc can choke on it otherwise.. 1663 inttty_ioctl(struct inode
* inode
,struct file
* file
, 1664 unsigned int cmd
,unsigned long arg
) 1666 struct tty_struct
*tty
, *real_tty
; 1669 tty
= (struct tty_struct
*)file
->private_data
; 1670 if(tty_paranoia_check(tty
, inode
->i_rdev
,"tty_ioctl")) 1674 if(tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&& 1675 tty
->driver
.subtype
== PTY_TYPE_MASTER
) 1676 real_tty
= tty
->link
; 1679 * Break handling by driver 1681 if(!tty
->driver
.break_ctl
) { 1685 if(tty
->driver
.ioctl
) 1686 return tty
->driver
.ioctl(tty
, file
, cmd
, arg
); 1689 /* These two ioctl's always return success; even if */ 1690 /* the driver doesn't support them. */ 1693 if(!tty
->driver
.ioctl
) 1695 retval
= tty
->driver
.ioctl(tty
, file
, cmd
, arg
); 1696 if(retval
== -ENOIOCTLCMD
) 1703 * Factor out some common prep work 1711 retval
=tty_check_change(tty
); 1714 if(cmd
!= TIOCCBRK
) { 1715 tty_wait_until_sent(tty
,0); 1716 if(signal_pending(current
)) 1724 returntiocsti(tty
, (char*)arg
); 1726 returntiocgwinsz(tty
, (struct winsize
*) arg
); 1728 returntiocswinsz(tty
, real_tty
, (struct winsize
*) arg
); 1730 returntioccons(inode
, tty
, real_tty
); 1732 returnfionbio(file
, (int*) arg
); 1734 set_bit(TTY_EXCLUSIVE
, &tty
->flags
); 1737 clear_bit(TTY_EXCLUSIVE
, &tty
->flags
); 1740 if(current
->tty
!= tty
) 1743 disassociate_ctty(0); 1745 current
->tty
= NULL
; 1746 task_unlock(current
); 1749 returntiocsctty(tty
, arg
); 1751 returntiocgpgrp(tty
, real_tty
, (pid_t
*) arg
); 1753 returntiocspgrp(tty
, real_tty
, (pid_t
*) arg
); 1755 returntiocgsid(tty
, real_tty
, (pid_t
*) arg
); 1757 returnput_user(tty
->ldisc
.num
, (int*) arg
); 1759 returntiocsetd(tty
, (int*) arg
); 1762 returntioclinux(tty
, arg
); 1764 case TIOCTTYGSTRUCT
: 1765 returntiocttygstruct(tty
, (struct tty_struct
*) arg
); 1770 case TIOCSBRK
:/* Turn break on, unconditionally */ 1771 tty
->driver
.break_ctl(tty
, -1); 1774 case TIOCCBRK
:/* Turn break off, unconditionally */ 1775 tty
->driver
.break_ctl(tty
,0); 1777 case TCSBRK
:/* SVID version: non-zero arg --> no break */ 1779 * XXX is the above comment correct, or the 1780 * code below correct? Is this ioctl used at 1784 returnsend_break(tty
, HZ
/4); 1786 case TCSBRKP
:/* support for POSIX tcsendbreak() */ 1787 returnsend_break(tty
, arg
? arg
*(HZ
/10) : HZ
/4); 1789 if(tty
->driver
.ioctl
) { 1790 int retval
= (tty
->driver
.ioctl
)(tty
, file
, cmd
, arg
); 1791 if(retval
!= -ENOIOCTLCMD
) 1794 if(tty
->ldisc
.ioctl
) { 1795 int retval
= (tty
->ldisc
.ioctl
)(tty
, file
, cmd
, arg
); 1796 if(retval
!= -ENOIOCTLCMD
) 1804 * This implements the "Secure Attention Key" --- the idea is to 1805 * prevent trojan horses by killing all processes associated with this 1806 * tty when the user hits the "Secure Attention Key". Required for 1807 * super-paranoid applications --- see the Orange Book for more details. 1809 * This code could be nicer; ideally it should send a HUP, wait a few 1810 * seconds, then send a INT, and then a KILL signal. But you then 1811 * have to coordinate with the init process, since all processes associated 1812 * with the current tty must be dead before the new getty is allowed 1815 * Now, if it would be correct ;-/ The current code has a nasty hole - 1816 * it doesn't catch files in flight. We may send the descriptor to ourselves 1817 * via AF_UNIX socket, close it and later fetch from socket. FIXME. 1819 voiddo_SAK(struct tty_struct
*tty
) 1824 struct task_struct
*p
; 1831 session
= tty
->session
; 1832 if(tty
->ldisc
.flush_buffer
) 1833 tty
->ldisc
.flush_buffer(tty
); 1834 if(tty
->driver
.flush_buffer
) 1835 tty
->driver
.flush_buffer(tty
); 1836 read_lock(&tasklist_lock
); 1838 if((p
->tty
== tty
) || 1839 ((session
>0) && (p
->session
== session
))) { 1840 send_sig(SIGKILL
, p
,1); 1845 read_lock(&p
->files
->file_lock
); 1846 /* FIXME: p->files could change */ 1847 for(i
=0; i
< p
->files
->max_fds
; i
++) { 1848 filp
=fcheck_files(p
->files
, i
); 1849 if(filp
&& (filp
->f_op
== &tty_fops
) && 1850 (filp
->private_data
== tty
)) { 1851 send_sig(SIGKILL
, p
,1); 1855 read_unlock(&p
->files
->file_lock
); 1859 read_unlock(&tasklist_lock
); 1864 * This routine is called out of the software interrupt to flush data 1865 * from the flip buffer to the line discipline. 1867 static voidflush_to_ldisc(void*private_
) 1869 struct tty_struct
*tty
= (struct tty_struct
*) private_
; 1873 unsigned long flags
; 1875 if(test_bit(TTY_DONT_FLIP
, &tty
->flags
)) { 1876 queue_task(&tty
->flip
.tqueue
, &tq_timer
); 1879 if(tty
->flip
.buf_num
) { 1880 cp
= tty
->flip
.char_buf
+ TTY_FLIPBUF_SIZE
; 1881 fp
= tty
->flip
.flag_buf
+ TTY_FLIPBUF_SIZE
; 1882 tty
->flip
.buf_num
=0; 1884 save_flags(flags
);cli(); 1885 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
; 1886 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
; 1888 cp
= tty
->flip
.char_buf
; 1889 fp
= tty
->flip
.flag_buf
; 1890 tty
->flip
.buf_num
=1; 1892 save_flags(flags
);cli(); 1893 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
+ TTY_FLIPBUF_SIZE
; 1894 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
+ TTY_FLIPBUF_SIZE
; 1896 count
= tty
->flip
.count
; 1898 restore_flags(flags
); 1900 tty
->ldisc
.receive_buf(tty
, cp
, fp
, count
); 1904 * Routine which returns the baud rate of the tty 1906 * Note that the baud_table needs to be kept in sync with the 1907 * include/asm/termbits.h file. 1909 static int baud_table
[] = { 1910 0,50,75,110,134,150,200,300,600,1200,1800,2400,4800, 1911 9600,19200,38400,57600,115200,230400,460800, 1913 76800,153600,307200,614400,921600 1915 500000,576000,921600,1000000,1152000,1500000,2000000, 1916 2500000,3000000,3500000,4000000 1920 static int n_baud_table
=sizeof(baud_table
)/sizeof(int); 1922 inttty_get_baud_rate(struct tty_struct
*tty
) 1924 unsigned int cflag
, i
; 1926 cflag
= tty
->termios
->c_cflag
; 1931 if(i
<1|| i
+15>= n_baud_table
) 1932 tty
->termios
->c_cflag
&= ~CBAUDEX
; 1936 if(i
==15&& tty
->alt_speed
) { 1938 printk("Use of setserial/setrocket to set SPD_* flags is deprecated\n"); 1941 return(tty
->alt_speed
); 1944 return baud_table
[i
]; 1947 voidtty_flip_buffer_push(struct tty_struct
*tty
) 1949 if(tty
->low_latency
) 1950 flush_to_ldisc((void*) tty
); 1952 queue_task(&tty
->flip
.tqueue
, &tq_timer
); 1956 * This subroutine initializes a tty structure. 1958 static voidinitialize_tty_struct(struct tty_struct
*tty
) 1960 memset(tty
,0,sizeof(struct tty_struct
)); 1961 tty
->magic
= TTY_MAGIC
; 1962 tty
->ldisc
= ldiscs
[N_TTY
]; 1964 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
; 1965 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
; 1966 tty
->flip
.tqueue
.routine
= flush_to_ldisc
; 1967 tty
->flip
.tqueue
.data
= tty
; 1968 init_MUTEX(&tty
->flip
.pty_sem
); 1969 init_waitqueue_head(&tty
->write_wait
); 1970 init_waitqueue_head(&tty
->read_wait
); 1971 tty
->tq_hangup
.routine
= do_tty_hangup
; 1972 tty
->tq_hangup
.data
= tty
; 1973 sema_init(&tty
->atomic_read
,1); 1974 sema_init(&tty
->atomic_write
,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 voidtty_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 voidtty_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 voidtty_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 inttty_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 inttty_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_SPECIALIX 2328 #if (defined(CONFIG_8xx) || defined(CONFIG_8260)) 2330 #endif/* CONFIG_8xx */ 2332 #ifdef CONFIG_MOXA_SMARTIO 2335 #ifdef CONFIG_MOXA_INTELLIO