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 support for a Unix98-style ptmx device.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
59 #include <linux/config.h>
60 #include <linux/types.h>
61 #include <linux/major.h>
62 #include <linux/errno.h>
63 #include <linux/signal.h>
64 #include <linux/fcntl.h>
65 #include <linux/sched.h>
66 #include <linux/interrupt.h>
67 #include <linux/tty.h>
68 #include <linux/tty_driver.h>
69 #include <linux/tty_flip.h>
70 #include <linux/devpts_fs.h>
71 #include <linux/file.h>
72 #include <linux/console.h>
73 #include <linux/timer.h>
74 #include <linux/ctype.h>
77 #include <linux/string.h>
78 #include <linux/malloc.h>
79 #include <linux/poll.h>
81 #include <linux/proc_fs.h>
83 #include <linux/init.h>
84 #include <linux/smp_lock.h>
86 #include <asm/uaccess.h>
87 #include <asm/system.h>
88 #include <asm/bitops.h>
90 #include <linux/kbd_kern.h>
91 #include <linux/vt_kern.h>
92 #include <linux/selection.h>
95 #include <linux/kmod.h>
98 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
99 #define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
100 #define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
101 #define PTMX_DEV MKDEV(TTYAUX_MAJOR,2)
103 #undef TTY_DEBUG_HANGUP
105 #define TTY_PARANOIA_CHECK 1
106 #define CHECK_TTY_COUNT 1
108 struct termios tty_std_termios
; /* for the benefit of tty drivers */
109 struct tty_driver
*tty_drivers
= NULL
; /* linked list of tty drivers */
110 struct tty_ldisc ldiscs
[NR_LDISCS
]; /* line disc dispatch table */
112 #ifdef CONFIG_UNIX98_PTYS
113 extern struct tty_driver ptm_driver
[]; /* Unix98 pty masters; for /dev/ptmx */
117 * redirect is the pseudo-tty that console output
118 * is redirected to if asked by TIOCCONS.
120 struct tty_struct
* redirect
= NULL
;
122 static void initialize_tty_struct(struct tty_struct
*tty
);
124 static ssize_t
tty_read(struct file
*, char *, size_t, loff_t
*);
125 static ssize_t
tty_write(struct file
*, const char *, size_t, loff_t
*);
126 static unsigned int tty_poll(struct file
*, poll_table
*);
127 static int tty_open(struct inode
*, struct file
*);
128 static int tty_release(struct inode
*, struct file
*);
129 static int tty_ioctl(struct inode
* inode
, struct file
* file
,
130 unsigned int cmd
, unsigned long arg
);
131 static int tty_fasync(int fd
, struct file
* filp
, int on
);
133 extern long console_8xx_init(void);
134 extern int rs_8xx_init(void);
135 #endif /* CONFIG_8xx */
138 #define MIN(a,b) ((a) < (b) ? (a) : (b))
142 * This routine returns the name of tty.
144 #define TTY_NUMBER(tty) (MINOR((tty)->device) - (tty)->driver.minor_start + \
145 (tty)->driver.name_base)
147 char *tty_name(struct tty_struct
*tty
, char *buf
)
150 sprintf(buf
, "%s%d", tty
->driver
.name
, TTY_NUMBER(tty
));
152 strcpy(buf
, "NULL tty");
156 inline int tty_paranoia_check(struct tty_struct
*tty
, kdev_t device
,
159 #ifdef TTY_PARANOIA_CHECK
160 static const char *badmagic
=
161 "Warning: bad magic number for tty struct (%s) in %s\n";
162 static const char *badtty
=
163 "Warning: null TTY for (%s) in %s\n";
166 printk(badtty
, kdevname(device
), routine
);
169 if (tty
->magic
!= TTY_MAGIC
) {
170 printk(badmagic
, kdevname(device
), routine
);
177 static int check_tty_count(struct tty_struct
*tty
, const char *routine
)
179 #ifdef CHECK_TTY_COUNT
183 for(f
= inuse_filps
; f
; f
= f
->f_next
) {
184 if(f
->private_data
== tty
)
187 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
188 tty
->driver
.subtype
== PTY_TYPE_SLAVE
&&
189 tty
->link
&& tty
->link
->count
)
191 if (tty
->count
!= count
) {
192 printk("Warning: dev (%s) tty->count(%d) != #fd's(%d) in %s\n",
193 kdevname(tty
->device
), tty
->count
, count
, routine
);
200 int tty_register_ldisc(int disc
, struct tty_ldisc
*new_ldisc
)
202 if (disc
< N_TTY
|| disc
>= NR_LDISCS
)
206 ldiscs
[disc
] = *new_ldisc
;
207 ldiscs
[disc
].flags
|= LDISC_FLAG_DEFINED
;
208 ldiscs
[disc
].num
= disc
;
210 memset(&ldiscs
[disc
], 0, sizeof(struct tty_ldisc
));
215 /* Set the discipline of a tty line. */
216 static int tty_set_ldisc(struct tty_struct
*tty
, int ldisc
)
219 struct tty_ldisc o_ldisc
;
222 if ((ldisc
< N_TTY
) || (ldisc
>= NR_LDISCS
))
225 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
226 /* Cyrus Durgin <cider@speakeasy.org> */
227 if (!(ldiscs
[ldisc
].flags
& LDISC_FLAG_DEFINED
)) {
229 sprintf(modname
, "tty-ldisc-%d", ldisc
);
230 request_module (modname
);
233 if (!(ldiscs
[ldisc
].flags
& LDISC_FLAG_DEFINED
))
236 if (tty
->ldisc
.num
== ldisc
)
237 return 0; /* We are already in the desired discipline */
238 o_ldisc
= tty
->ldisc
;
240 tty_wait_until_sent(tty
, 0);
242 /* Shutdown the current discipline. */
243 if (tty
->ldisc
.close
)
244 (tty
->ldisc
.close
)(tty
);
246 /* Now set up the new line discipline. */
247 tty
->ldisc
= ldiscs
[ldisc
];
248 tty
->termios
->c_line
= ldisc
;
250 retval
= (tty
->ldisc
.open
)(tty
);
252 tty
->ldisc
= o_ldisc
;
253 tty
->termios
->c_line
= tty
->ldisc
.num
;
254 if (tty
->ldisc
.open
&& (tty
->ldisc
.open(tty
) < 0)) {
255 tty
->ldisc
= ldiscs
[N_TTY
];
256 tty
->termios
->c_line
= N_TTY
;
257 if (tty
->ldisc
.open
) {
258 int r
= tty
->ldisc
.open(tty
);
261 panic("Couldn't open N_TTY ldisc for "
263 tty_name(tty
, buf
), r
);
267 if (tty
->ldisc
.num
!= o_ldisc
.num
&& tty
->driver
.set_ldisc
)
268 tty
->driver
.set_ldisc(tty
);
273 * This routine returns a tty driver structure, given a device number
275 struct tty_driver
*get_tty_driver(kdev_t device
)
278 struct tty_driver
*p
;
280 minor
= MINOR(device
);
281 major
= MAJOR(device
);
283 for (p
= tty_drivers
; p
; p
= p
->next
) {
284 if (p
->major
!= major
)
286 if (minor
< p
->minor_start
)
288 if (minor
>= p
->minor_start
+ p
->num
)
296 * If we try to write to, or set the state of, a terminal and we're
297 * not in the foreground, send a SIGTTOU. If the signal is blocked or
298 * ignored, go ahead and perform the operation. (POSIX 7.2)
300 int tty_check_change(struct tty_struct
* tty
)
302 if (current
->tty
!= tty
)
304 if (tty
->pgrp
<= 0) {
305 printk("tty_check_change: tty->pgrp <= 0!\n");
308 if (current
->pgrp
== tty
->pgrp
)
310 if (is_ignored(SIGTTOU
))
312 if (is_orphaned_pgrp(current
->pgrp
))
314 (void) kill_pg(current
->pgrp
,SIGTTOU
,1);
318 static ssize_t
hung_up_tty_read(struct file
* file
, char * buf
,
319 size_t count
, loff_t
*ppos
)
321 /* Can't seek (pread) on ttys. */
322 if (ppos
!= &file
->f_pos
)
327 static ssize_t
hung_up_tty_write(struct file
* file
, const char * buf
,
328 size_t count
, loff_t
*ppos
)
330 /* Can't seek (pwrite) on ttys. */
331 if (ppos
!= &file
->f_pos
)
336 static unsigned int hung_up_tty_poll(struct file
* filp
, poll_table
* wait
)
338 return POLLIN
| POLLOUT
| POLLERR
| POLLHUP
| POLLRDNORM
| POLLWRNORM
;
341 static int hung_up_tty_ioctl(struct inode
* inode
, struct file
* file
,
342 unsigned int cmd
, unsigned long arg
)
344 return cmd
== TIOCSPGRP
? -ENOTTY
: -EIO
;
347 static long long tty_lseek(struct file
* file
, long long offset
, int orig
)
352 static struct file_operations tty_fops
= {
356 NULL
, /* tty_readdir */
363 NULL
, /* tty_fsync */
367 static struct file_operations hung_up_tty_fops
= {
371 NULL
, /* hung_up_tty_readdir */
374 NULL
, /* hung_up_tty_mmap */
375 NULL
, /* hung_up_tty_open */
377 tty_release
, /* hung_up_tty_release */
378 NULL
, /* hung_up_tty_fsync */
379 NULL
/* hung_up_tty_fasync */
383 * This can be called through the "tq_scheduler"
384 * task-list. That is process synchronous, but
385 * doesn't hold any locks, so we need to make
386 * sure we have the appropriate locks for what
389 void do_tty_hangup(void *data
)
391 struct tty_struct
*tty
= (struct tty_struct
*) data
;
393 struct task_struct
*p
;
398 /* inuse_filps is protected by the single kernel lock */
401 check_tty_count(tty
, "do_tty_hangup");
402 for (filp
= inuse_filps
; filp
; filp
= filp
->f_next
) {
403 if (filp
->private_data
!= tty
)
407 if (!filp
->f_dentry
->d_inode
)
409 if (filp
->f_dentry
->d_inode
->i_rdev
== CONSOLE_DEV
||
410 filp
->f_dentry
->d_inode
->i_rdev
== SYSCONS_DEV
)
412 if (filp
->f_op
!= &tty_fops
)
414 tty_fasync(-1, filp
, 0);
415 filp
->f_op
= &hung_up_tty_fops
;
418 /* FIXME! What are the locking issues here? This may me overdoing things.. */
422 save_flags(flags
); cli();
423 if (tty
->ldisc
.flush_buffer
)
424 tty
->ldisc
.flush_buffer(tty
);
425 if (tty
->driver
.flush_buffer
)
426 tty
->driver
.flush_buffer(tty
);
427 if ((test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) &&
428 tty
->ldisc
.write_wakeup
)
429 (tty
->ldisc
.write_wakeup
)(tty
);
430 restore_flags(flags
);
433 wake_up_interruptible(&tty
->write_wait
);
434 wake_up_interruptible(&tty
->read_wait
);
437 * Shutdown the current line discipline, and reset it to
440 if (tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
)
441 *tty
->termios
= tty
->driver
.init_termios
;
442 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
443 if (tty
->ldisc
.close
)
444 (tty
->ldisc
.close
)(tty
);
445 tty
->ldisc
= ldiscs
[N_TTY
];
446 tty
->termios
->c_line
= N_TTY
;
447 if (tty
->ldisc
.open
) {
448 int i
= (tty
->ldisc
.open
)(tty
);
450 printk("do_tty_hangup: N_TTY open: error %d\n",
455 read_lock(&tasklist_lock
);
457 if ((tty
->session
> 0) && (p
->session
== tty
->session
) &&
459 send_sig(SIGHUP
,p
,1);
460 send_sig(SIGCONT
,p
,1);
462 p
->tty_old_pgrp
= tty
->pgrp
;
467 read_unlock(&tasklist_lock
);
472 tty
->ctrl_status
= 0;
473 if (tty
->driver
.hangup
)
474 (tty
->driver
.hangup
)(tty
);
478 void tty_hangup(struct tty_struct
* tty
)
480 #ifdef TTY_DEBUG_HANGUP
483 printk("%s hangup...\n", tty_name(tty
, buf
));
485 queue_task(&tty
->tq_hangup
, &tq_scheduler
);
488 void tty_vhangup(struct tty_struct
* tty
)
490 #ifdef TTY_DEBUG_HANGUP
493 printk("%s vhangup...\n", tty_name(tty
, buf
));
495 do_tty_hangup((void *) tty
);
498 int tty_hung_up_p(struct file
* filp
)
500 return (filp
->f_op
== &hung_up_tty_fops
);
504 * This function is typically called only by the session leader, when
505 * it wants to disassociate itself from its controlling tty.
507 * It performs the following functions:
508 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
509 * (2) Clears the tty from being controlling the session
510 * (3) Clears the controlling tty for all processes in the
513 * The argument on_exit is set to 1 if called when a process is
514 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
516 void disassociate_ctty(int on_exit
)
518 struct tty_struct
*tty
= current
->tty
;
519 struct task_struct
*p
;
523 tty_pgrp
= tty
->pgrp
;
524 if (on_exit
&& tty
->driver
.type
!= TTY_DRIVER_TYPE_PTY
)
527 if (current
->tty_old_pgrp
) {
528 kill_pg(current
->tty_old_pgrp
, SIGHUP
, on_exit
);
529 kill_pg(current
->tty_old_pgrp
, SIGCONT
, on_exit
);
534 kill_pg(tty_pgrp
, SIGHUP
, on_exit
);
536 kill_pg(tty_pgrp
, SIGCONT
, on_exit
);
539 current
->tty_old_pgrp
= 0;
543 read_lock(&tasklist_lock
);
545 if (p
->session
== current
->session
)
547 read_unlock(&tasklist_lock
);
550 void wait_for_keypress(void)
552 struct console
*c
= console_drivers
;
553 if (c
) c
->wait_key(c
);
556 void stop_tty(struct tty_struct
*tty
)
561 if (tty
->link
&& tty
->link
->packet
) {
562 tty
->ctrl_status
&= ~TIOCPKT_START
;
563 tty
->ctrl_status
|= TIOCPKT_STOP
;
564 wake_up_interruptible(&tty
->link
->read_wait
);
566 if (tty
->driver
.stop
)
567 (tty
->driver
.stop
)(tty
);
570 void start_tty(struct tty_struct
*tty
)
572 if (!tty
->stopped
|| tty
->flow_stopped
)
575 if (tty
->link
&& tty
->link
->packet
) {
576 tty
->ctrl_status
&= ~TIOCPKT_STOP
;
577 tty
->ctrl_status
|= TIOCPKT_START
;
578 wake_up_interruptible(&tty
->link
->read_wait
);
580 if (tty
->driver
.start
)
581 (tty
->driver
.start
)(tty
);
582 if ((test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) &&
583 tty
->ldisc
.write_wakeup
)
584 (tty
->ldisc
.write_wakeup
)(tty
);
585 wake_up_interruptible(&tty
->write_wait
);
588 static ssize_t
tty_read(struct file
* file
, char * buf
, size_t count
,
592 struct tty_struct
* tty
;
595 /* Can't seek (pread) on ttys. */
596 if (ppos
!= &file
->f_pos
)
599 tty
= (struct tty_struct
*)file
->private_data
;
600 inode
= file
->f_dentry
->d_inode
;
601 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_read"))
603 if (!tty
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
606 /* This check not only needs to be done before reading, but also
607 whenever read_chan() gets woken up after sleeping, so I've
608 moved it to there. This should only be done for the N_TTY
609 line discipline, anyway. Same goes for write_chan(). -- jlc. */
611 if ((inode
->i_rdev
!= CONSOLE_DEV
) && /* don't stop on /dev/console */
613 (current
->tty
== tty
) &&
614 (tty
->pgrp
!= current
->pgrp
))
615 if (is_ignored(SIGTTIN
) || is_orphaned_pgrp(current
->pgrp
))
618 (void) kill_pg(current
->pgrp
, SIGTTIN
, 1);
623 i
= (tty
->ldisc
.read
)(tty
,file
,buf
,count
);
627 inode
->i_atime
= CURRENT_TIME
;
632 * Split writes up in sane blocksizes to avoid
633 * denial-of-service type attacks
635 static inline ssize_t
do_tty_write(
636 ssize_t (*write
)(struct tty_struct
*, struct file
*, const unsigned char *, size_t),
637 struct tty_struct
*tty
,
639 const unsigned char *buf
,
642 ssize_t ret
= 0, written
= 0;
645 unsigned long size
= PAGE_SIZE
*2;
648 ret
= write(tty
, file
, buf
, size
);
657 if (signal_pending(current
))
659 if (current
->need_resched
)
663 file
->f_dentry
->d_inode
->i_mtime
= CURRENT_TIME
;
670 static ssize_t
tty_write(struct file
* file
, const char * buf
, size_t count
,
674 struct tty_struct
* tty
;
677 /* Can't seek (pwrite) on ttys. */
678 if (ppos
!= &file
->f_pos
)
682 * For now, we redirect writes from /dev/console as
685 inode
= file
->f_dentry
->d_inode
;
686 is_console
= (inode
->i_rdev
== SYSCONS_DEV
||
687 inode
->i_rdev
== CONSOLE_DEV
);
689 if (is_console
&& redirect
)
692 tty
= (struct tty_struct
*)file
->private_data
;
693 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_write"))
695 if (!tty
|| !tty
->driver
.write
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
698 if (!is_console
&& L_TOSTOP(tty
) && (tty
->pgrp
> 0) &&
699 (current
->tty
== tty
) && (tty
->pgrp
!= current
->pgrp
)) {
700 if (is_orphaned_pgrp(current
->pgrp
))
702 if (!is_ignored(SIGTTOU
)) {
703 (void) kill_pg(current
->pgrp
, SIGTTOU
, 1);
708 if (!tty
->ldisc
.write
)
710 return do_tty_write(tty
->ldisc
.write
, tty
, file
,
711 (const unsigned char *)buf
, count
);
714 /* Semaphore to protect creating and releasing a tty */
715 static struct semaphore tty_sem
= MUTEX
;
717 static void down_tty_sem(int index
)
722 static void up_tty_sem(int index
)
727 static void release_mem(struct tty_struct
*tty
, int idx
);
730 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
731 * failed open. The new code protects the open with a semaphore, so it's
732 * really quite straightforward. The semaphore locking can probably be
733 * relaxed for the (most common) case of reopening a tty.
735 static int init_dev(kdev_t device
, struct tty_struct
**ret_tty
)
737 struct tty_struct
*tty
, *o_tty
;
738 struct termios
*tp
, **tp_loc
, *o_tp
, **o_tp_loc
;
739 struct termios
*ltp
, **ltp_loc
, *o_ltp
, **o_ltp_loc
;
740 struct tty_driver
*driver
;
744 driver
= get_tty_driver(device
);
748 idx
= MINOR(device
) - driver
->minor_start
;
751 * Check whether we need to acquire the tty semaphore to avoid
752 * race conditions. For now, play it safe.
756 /* check whether we're reopening an existing tty */
757 tty
= driver
->table
[idx
];
758 if (tty
) goto fast_track
;
761 * First time open is complex, especially for PTY devices.
762 * This code guarantees that either everything succeeds and the
763 * TTY is ready for operation, or else the table slots are vacated
764 * and the allocated memory released. (Except that the termios
765 * and locked termios may be retained.)
772 tty
= (struct tty_struct
*) get_free_page(GFP_KERNEL
);
775 initialize_tty_struct(tty
);
776 tty
->device
= device
;
777 tty
->driver
= *driver
;
779 tp_loc
= &driver
->termios
[idx
];
781 tp
= (struct termios
*) kmalloc(sizeof(struct termios
),
785 *tp
= driver
->init_termios
;
788 ltp_loc
= &driver
->termios_locked
[idx
];
790 ltp
= (struct termios
*) kmalloc(sizeof(struct termios
),
794 memset(ltp
, 0, sizeof(struct termios
));
797 if (driver
->type
== TTY_DRIVER_TYPE_PTY
) {
798 o_tty
= (struct tty_struct
*) get_free_page(GFP_KERNEL
);
801 initialize_tty_struct(o_tty
);
802 o_tty
->device
= (kdev_t
) MKDEV(driver
->other
->major
,
803 driver
->other
->minor_start
+ idx
);
804 o_tty
->driver
= *driver
->other
;
806 o_tp_loc
= &driver
->other
->termios
[idx
];
808 o_tp
= (struct termios
*)
809 kmalloc(sizeof(struct termios
), GFP_KERNEL
);
812 *o_tp
= driver
->other
->init_termios
;
815 o_ltp_loc
= &driver
->other
->termios_locked
[idx
];
817 o_ltp
= (struct termios
*)
818 kmalloc(sizeof(struct termios
), GFP_KERNEL
);
821 memset(o_ltp
, 0, sizeof(struct termios
));
825 * Everything allocated ... set up the o_tty structure.
827 driver
->other
->table
[idx
] = o_tty
;
832 o_tty
->termios
= *o_tp_loc
;
833 o_tty
->termios_locked
= *o_ltp_loc
;
834 (*driver
->other
->refcount
)++;
835 if (driver
->subtype
== PTY_TYPE_MASTER
)
838 /* Establish the links in both directions */
844 * All structures have been allocated, so now we install them.
845 * Failures after this point use release_mem to clean up, so
846 * there's no need to null out the local pointers.
848 driver
->table
[idx
] = tty
;
853 tty
->termios
= *tp_loc
;
854 tty
->termios_locked
= *ltp_loc
;
855 (*driver
->refcount
)++;
859 * Structures all installed ... call the ldisc open routines.
860 * If we fail here just call release_mem to clean up. No need
861 * to decrement the use counts, as release_mem doesn't care.
863 if (tty
->ldisc
.open
) {
864 retval
= (tty
->ldisc
.open
)(tty
);
866 goto release_mem_out
;
868 if (o_tty
&& o_tty
->ldisc
.open
) {
869 retval
= (o_tty
->ldisc
.open
)(o_tty
);
871 if (tty
->ldisc
.close
)
872 (tty
->ldisc
.close
)(tty
);
873 goto release_mem_out
;
879 * This fast open can be used if the tty is already open.
880 * No memory is allocated, and the only failures are from
881 * attempting to open a closing tty or attempting multiple
882 * opens on a pty master.
885 if (test_bit(TTY_CLOSING
, &tty
->flags
)) {
889 if (driver
->type
== TTY_DRIVER_TYPE_PTY
&&
890 driver
->subtype
== PTY_TYPE_MASTER
) {
892 * special case for PTY masters: only one open permitted,
893 * and the slave side open count is incremented as well.
902 tty
->driver
= *driver
; /* N.B. why do this every time?? */
907 /* All paths come through here to release the semaphore */
912 /* Release locally allocated memory ... nothing placed in slots */
915 kfree_s(o_tp
, sizeof(struct termios
));
917 free_page((unsigned long) o_tty
);
919 kfree_s(ltp
, sizeof(struct termios
));
921 kfree_s(tp
, sizeof(struct termios
));
922 free_page((unsigned long) tty
);
928 /* call the tty release_mem routine to clean out this slot */
930 printk("init_dev: ldisc open failed, clearing slot %d\n", idx
);
931 release_mem(tty
, idx
);
936 * Releases memory associated with a tty structure, and clears out the
937 * driver table slots.
939 static void release_mem(struct tty_struct
*tty
, int idx
)
941 struct tty_struct
*o_tty
;
944 if ((o_tty
= tty
->link
) != NULL
) {
945 o_tty
->driver
.table
[idx
] = NULL
;
946 if (o_tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
) {
947 tp
= o_tty
->driver
.termios
[idx
];
948 o_tty
->driver
.termios
[idx
] = NULL
;
949 kfree_s(tp
, sizeof(struct termios
));
952 (*o_tty
->driver
.refcount
)--;
953 free_page((unsigned long) o_tty
);
956 tty
->driver
.table
[idx
] = NULL
;
957 if (tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
) {
958 tp
= tty
->driver
.termios
[idx
];
959 tty
->driver
.termios
[idx
] = NULL
;
960 kfree_s(tp
, sizeof(struct termios
));
963 (*tty
->driver
.refcount
)--;
964 free_page((unsigned long) tty
);
968 * Even releasing the tty structures is a tricky business.. We have
969 * to be very careful that the structures are all released at the
970 * same time, as interrupts might otherwise get the wrong pointers.
972 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
973 * lead to double frees or releasing memory still in use.
975 static void release_dev(struct file
* filp
)
977 struct tty_struct
*tty
, *o_tty
;
978 int pty_master
, tty_closing
, o_tty_closing
, do_sleep
;
982 tty
= (struct tty_struct
*)filp
->private_data
;
983 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "release_dev"))
986 check_tty_count(tty
, "release_dev");
988 tty_fasync(-1, filp
, 0);
990 idx
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
991 pty_master
= (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
992 tty
->driver
.subtype
== PTY_TYPE_MASTER
);
995 #ifdef TTY_PARANOIA_CHECK
996 if (idx
< 0 || idx
>= tty
->driver
.num
) {
997 printk("release_dev: bad idx when trying to free (%s)\n",
998 kdevname(tty
->device
));
1001 if (tty
!= tty
->driver
.table
[idx
]) {
1002 printk("release_dev: driver.table[%d] not tty for (%s)\n",
1003 idx
, kdevname(tty
->device
));
1006 if (tty
->termios
!= tty
->driver
.termios
[idx
]) {
1007 printk("release_dev: driver.termios[%d] not termios "
1009 idx
, kdevname(tty
->device
));
1012 if (tty
->termios_locked
!= tty
->driver
.termios_locked
[idx
]) {
1013 printk("release_dev: driver.termios_locked[%d] not "
1014 "termios_locked for (%s)\n",
1015 idx
, kdevname(tty
->device
));
1020 #ifdef TTY_DEBUG_HANGUP
1021 printk("release_dev of %s (tty count=%d)...", tty_name(tty
, buf
),
1025 #ifdef TTY_PARANOIA_CHECK
1026 if (tty
->driver
.other
) {
1027 if (o_tty
!= tty
->driver
.other
->table
[idx
]) {
1028 printk("release_dev: other->table[%d] not o_tty for ("
1030 idx
, kdevname(tty
->device
));
1033 if (o_tty
->termios
!= tty
->driver
.other
->termios
[idx
]) {
1034 printk("release_dev: other->termios[%d] not o_termios "
1036 idx
, kdevname(tty
->device
));
1039 if (o_tty
->termios_locked
!=
1040 tty
->driver
.other
->termios_locked
[idx
]) {
1041 printk("release_dev: other->termios_locked[%d] not "
1042 "o_termios_locked for (%s)\n",
1043 idx
, kdevname(tty
->device
));
1046 if (o_tty
->link
!= tty
) {
1047 printk("release_dev: bad pty pointers\n");
1053 if (tty
->driver
.close
)
1054 tty
->driver
.close(tty
, filp
);
1057 * Sanity check: if tty->count is going to zero, there shouldn't be
1058 * any waiters on tty->read_wait or tty->write_wait. We test the
1059 * wait queues and kick everyone out _before_ actually starting to
1060 * close. This ensures that we won't block while releasing the tty
1063 * The test for the o_tty closing is necessary, since the master and
1064 * slave sides may close in any order. If the slave side closes out
1065 * first, its count will be one, since the master side holds an open.
1066 * Thus this test wouldn't be triggered at the time the slave closes,
1069 * Note that it's possible for the tty to be opened again while we're
1070 * flushing out waiters. By recalculating the closing flags before
1071 * each iteration we avoid any problems.
1074 tty_closing
= tty
->count
<= 1;
1075 o_tty_closing
= o_tty
&&
1076 (o_tty
->count
<= (pty_master
? 1 : 0));
1080 if (waitqueue_active(&tty
->read_wait
)) {
1081 wake_up(&tty
->read_wait
);
1084 if (waitqueue_active(&tty
->write_wait
)) {
1085 wake_up(&tty
->write_wait
);
1089 if (o_tty_closing
) {
1090 if (waitqueue_active(&o_tty
->read_wait
)) {
1091 wake_up(&o_tty
->read_wait
);
1094 if (waitqueue_active(&o_tty
->write_wait
)) {
1095 wake_up(&o_tty
->write_wait
);
1102 printk("release_dev: %s: read/write wait queue active!\n",
1103 tty_name(tty
, buf
));
1108 * The closing flags are now consistent with the open counts on
1109 * both sides, and we've completed the last operation that could
1110 * block, so it's safe to proceed with closing.
1114 if (--o_tty
->count
< 0) {
1115 printk("release_dev: bad pty slave count (%d) for %s\n",
1116 o_tty
->count
, tty_name(o_tty
, buf
));
1120 if (--tty
->count
< 0) {
1121 printk("release_dev: bad tty->count (%d) for %s\n",
1122 tty
->count
, tty_name(tty
, buf
));
1127 * Perform some housekeeping before deciding whether to return.
1129 * Set the TTY_CLOSING flag if this was the last open. In the
1130 * case of a pty we may have to wait around for the other side
1131 * to close, and TTY_CLOSING makes sure we can't be reopened.
1134 set_bit(TTY_CLOSING
, &tty
->flags
);
1136 set_bit(TTY_CLOSING
, &o_tty
->flags
);
1139 * If _either_ side is closing, make sure there aren't any
1140 * processes that still think tty or o_tty is their controlling
1141 * tty. Also, clear redirect if it points to either tty.
1143 if (tty_closing
|| o_tty_closing
) {
1144 struct task_struct
*p
;
1146 read_lock(&tasklist_lock
);
1148 if (p
->tty
== tty
|| (o_tty
&& p
->tty
== o_tty
))
1151 read_unlock(&tasklist_lock
);
1153 if (redirect
== tty
|| (o_tty
&& redirect
== o_tty
))
1157 /* check whether both sides are closing ... */
1158 if (!tty_closing
|| (o_tty
&& !o_tty_closing
))
1160 filp
->private_data
= 0;
1162 #ifdef TTY_DEBUG_HANGUP
1163 printk("freeing tty structure...");
1167 * Shutdown the current line discipline, and reset it to N_TTY.
1168 * N.B. why reset ldisc when we're releasing the memory??
1170 if (tty
->ldisc
.close
)
1171 (tty
->ldisc
.close
)(tty
);
1172 tty
->ldisc
= ldiscs
[N_TTY
];
1173 tty
->termios
->c_line
= N_TTY
;
1175 if (o_tty
->ldisc
.close
)
1176 (o_tty
->ldisc
.close
)(o_tty
);
1177 o_tty
->ldisc
= ldiscs
[N_TTY
];
1181 * Make sure that the tty's task queue isn't activated.
1183 run_task_queue(&tq_timer
);
1184 run_task_queue(&tq_scheduler
);
1187 * The release_mem function takes care of the details of clearing
1188 * the slots and preserving the termios structure.
1190 release_mem(tty
, idx
);
1194 * tty_open and tty_release keep up the tty count that contains the
1195 * number of opens done on a tty. We cannot use the inode-count, as
1196 * different inodes might point to the same tty.
1198 * Open-counting is needed for pty masters, as well as for keeping
1199 * track of serial lines: DTR is dropped when the last close happens.
1200 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1202 * The termios state of a pty is reset on first open so that
1203 * settings don't persist across reuse.
1205 static int tty_open(struct inode
* inode
, struct file
* filp
)
1207 struct tty_struct
*tty
;
1210 unsigned short saved_flags
;
1213 saved_flags
= filp
->f_flags
;
1215 noctty
= filp
->f_flags
& O_NOCTTY
;
1216 device
= inode
->i_rdev
;
1217 if (device
== TTY_DEV
) {
1220 device
= current
->tty
->device
;
1221 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/tty block */
1225 if (device
== CONSOLE_DEV
) {
1226 extern int fg_console
;
1227 device
= MKDEV(TTY_MAJOR
, fg_console
+ 1);
1231 if (device
== SYSCONS_DEV
) {
1232 struct console
*c
= console_drivers
;
1233 while(c
&& !c
->device
)
1237 device
= c
->device(c
);
1240 #ifdef CONFIG_UNIX98_PTYS
1241 if (device
== PTMX_DEV
) {
1242 /* find a free pty. */
1244 struct tty_driver
*driver
;
1246 /* find a device that is not in use. */
1248 for ( major
= 0 ; major
< UNIX98_NR_MAJORS
; major
++ ) {
1249 driver
= &ptm_driver
[major
];
1250 for (minor
= driver
->minor_start
;
1251 minor
< driver
->minor_start
+ driver
->num
;
1253 device
= MKDEV(driver
->major
, minor
);
1254 if (!init_dev(device
, &tty
)) goto ptmx_found
; /* ok! */
1257 return -EIO
; /* no free ptys */
1259 set_bit(TTY_PTY_LOCK
, &tty
->flags
); /* LOCK THE SLAVE */
1260 minor
-= driver
->minor_start
;
1261 devpts_pty_new(driver
->other
->name_base
+ minor
, MKDEV(driver
->other
->major
, minor
+ driver
->other
->minor_start
));
1267 retval
= init_dev(device
, &tty
);
1271 /* N.B. this error exit may leave filp->f_flags with O_NONBLOCK set */
1273 filp
->private_data
= tty
;
1274 check_tty_count(tty
, "tty_open");
1275 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
1276 tty
->driver
.subtype
== PTY_TYPE_MASTER
)
1278 #ifdef TTY_DEBUG_HANGUP
1279 printk("opening %s...", tty_name(tty
, buf
));
1281 if (tty
->driver
.open
)
1282 retval
= tty
->driver
.open(tty
, filp
);
1285 filp
->f_flags
= saved_flags
;
1287 if (!retval
&& test_bit(TTY_EXCLUSIVE
, &tty
->flags
) && !suser())
1291 #ifdef TTY_DEBUG_HANGUP
1292 printk("error %d in opening %s...", retval
,
1293 tty_name(tty
, buf
));
1297 if (retval
!= -ERESTARTSYS
)
1299 if (signal_pending(current
))
1303 * Need to reset f_op in case a hangup happened.
1305 filp
->f_op
= &tty_fops
;
1311 tty
->session
== 0) {
1313 current
->tty_old_pgrp
= 0;
1314 tty
->session
= current
->session
;
1315 tty
->pgrp
= current
->pgrp
;
1317 if ((tty
->driver
.type
== TTY_DRIVER_TYPE_SERIAL
) &&
1318 (tty
->driver
.subtype
== SERIAL_TYPE_CALLOUT
) &&
1319 (tty
->count
== 1)) {
1320 static int nr_warns
= 0;
1322 printk(KERN_WARNING
"tty_io.c: "
1323 "process %d (%s) used obsolete /dev/%s - "
1324 "update software to use /dev/ttyS%d\n",
1325 current
->pid
, current
->comm
,
1326 tty_name(tty
, buf
), TTY_NUMBER(tty
));
1333 static int tty_release(struct inode
* inode
, struct file
* filp
)
1339 static unsigned int tty_poll(struct file
* filp
, poll_table
* wait
)
1341 struct tty_struct
* tty
;
1343 tty
= (struct tty_struct
*)filp
->private_data
;
1344 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "tty_poll"))
1347 if (tty
->ldisc
.poll
)
1348 return (tty
->ldisc
.poll
)(tty
, filp
, wait
);
1353 * fasync_helper() is used by some character device drivers (mainly mice)
1354 * to set up the fasync queue. It returns negative on error, 0 if it did
1355 * no changes and positive if it added/deleted the entry.
1357 int fasync_helper(int fd
, struct file
* filp
, int on
, struct fasync_struct
**fapp
)
1359 struct fasync_struct
*fa
, **fp
;
1360 unsigned long flags
;
1362 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
1363 if (fa
->fa_file
== filp
)
1372 fa
= (struct fasync_struct
*)kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
);
1375 fa
->magic
= FASYNC_MAGIC
;
1380 fa
->fa_next
= *fapp
;
1382 restore_flags(flags
);
1390 restore_flags(flags
);
1395 static int tty_fasync(int fd
, struct file
* filp
, int on
)
1397 struct tty_struct
* tty
;
1400 tty
= (struct tty_struct
*)filp
->private_data
;
1401 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "tty_fasync"))
1404 retval
= fasync_helper(fd
, filp
, on
, &tty
->fasync
);
1409 if (!waitqueue_active(&tty
->read_wait
))
1410 tty
->minimum_to_wake
= 1;
1411 if (filp
->f_owner
.pid
== 0) {
1412 filp
->f_owner
.pid
= (-tty
->pgrp
) ? : current
->pid
;
1413 filp
->f_owner
.uid
= current
->uid
;
1414 filp
->f_owner
.euid
= current
->euid
;
1417 if (!tty
->fasync
&& !waitqueue_active(&tty
->read_wait
))
1418 tty
->minimum_to_wake
= N_TTY_BUF_SIZE
;
1423 static int tiocsti(struct tty_struct
*tty
, char * arg
)
1427 if ((current
->tty
!= tty
) && !suser())
1429 if (get_user(ch
, arg
))
1431 tty
->ldisc
.receive_buf(tty
, &ch
, &mbz
, 1);
1435 static int tiocgwinsz(struct tty_struct
*tty
, struct winsize
* arg
)
1437 if (copy_to_user(arg
, &tty
->winsize
, sizeof(*arg
)))
1442 static int tiocswinsz(struct tty_struct
*tty
, struct tty_struct
*real_tty
,
1443 struct winsize
* arg
)
1445 struct winsize tmp_ws
;
1447 if (copy_from_user(&tmp_ws
, arg
, sizeof(*arg
)))
1449 if (!memcmp(&tmp_ws
, &tty
->winsize
, sizeof(*arg
)))
1452 kill_pg(tty
->pgrp
, SIGWINCH
, 1);
1453 if ((real_tty
->pgrp
!= tty
->pgrp
) && (real_tty
->pgrp
> 0))
1454 kill_pg(real_tty
->pgrp
, SIGWINCH
, 1);
1455 tty
->winsize
= tmp_ws
;
1456 real_tty
->winsize
= tmp_ws
;
1460 static int tioccons(struct tty_struct
*tty
, struct tty_struct
*real_tty
)
1462 if (tty
->driver
.type
== TTY_DRIVER_TYPE_CONSOLE
||
1463 tty
->driver
.type
== TTY_DRIVER_TYPE_SYSCONS
) {
1471 redirect
= real_tty
;
1476 static int fionbio(struct file
*file
, int *arg
)
1480 if (get_user(nonblock
, arg
))
1484 file
->f_flags
|= O_NONBLOCK
;
1486 file
->f_flags
&= ~O_NONBLOCK
;
1490 static int tiocsctty(struct tty_struct
*tty
, int arg
)
1492 if (current
->leader
&&
1493 (current
->session
== tty
->session
))
1496 * The process must be a session leader and
1497 * not have a controlling tty already.
1499 if (!current
->leader
|| current
->tty
)
1501 if (tty
->session
> 0) {
1503 * This tty is already the controlling
1504 * tty for another session group!
1506 if ((arg
== 1) && suser()) {
1510 struct task_struct
*p
;
1512 read_lock(&tasklist_lock
);
1516 read_unlock(&tasklist_lock
);
1521 current
->tty_old_pgrp
= 0;
1522 tty
->session
= current
->session
;
1523 tty
->pgrp
= current
->pgrp
;
1527 static int tiocgpgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1530 * (tty == real_tty) is a cheap way of
1531 * testing if the tty is NOT a master pty.
1533 if (tty
== real_tty
&& current
->tty
!= real_tty
)
1535 return put_user(real_tty
->pgrp
, arg
);
1538 static int tiocspgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1541 int retval
= tty_check_change(real_tty
);
1547 if (!current
->tty
||
1548 (current
->tty
!= real_tty
) ||
1549 (real_tty
->session
!= current
->session
))
1551 get_user(pgrp
, (pid_t
*) arg
);
1554 if (session_of_pgrp(pgrp
) != current
->session
)
1556 real_tty
->pgrp
= pgrp
;
1560 static int tiocgsid(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1563 * (tty == real_tty) is a cheap way of
1564 * testing if the tty is NOT a master pty.
1566 if (tty
== real_tty
&& current
->tty
!= real_tty
)
1568 if (real_tty
->session
<= 0)
1570 return put_user(real_tty
->session
, arg
);
1573 static int tiocttygstruct(struct tty_struct
*tty
, struct tty_struct
*arg
)
1575 if (copy_to_user(arg
, tty
, sizeof(*arg
)))
1580 static int tiocsetd(struct tty_struct
*tty
, int *arg
)
1584 retval
= get_user(ldisc
, arg
);
1587 return tty_set_ldisc(tty
, ldisc
);
1590 static int send_break(struct tty_struct
*tty
, int duration
)
1592 current
->state
= TASK_INTERRUPTIBLE
;
1593 current
->timeout
= jiffies
+ duration
;
1595 tty
->driver
.break_ctl(tty
, -1);
1596 if (!signal_pending(current
))
1598 tty
->driver
.break_ctl(tty
, 0);
1599 if (signal_pending(current
))
1605 * Split this up, as gcc can choke on it otherwise..
1607 static int tty_ioctl(struct inode
* inode
, struct file
* file
,
1608 unsigned int cmd
, unsigned long arg
)
1610 struct tty_struct
*tty
, *real_tty
;
1613 tty
= (struct tty_struct
*)file
->private_data
;
1614 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_ioctl"))
1618 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
1619 tty
->driver
.subtype
== PTY_TYPE_MASTER
)
1620 real_tty
= tty
->link
;
1623 * Break handling by driver
1625 if (!tty
->driver
.break_ctl
) {
1629 if (tty
->driver
.ioctl
)
1630 return tty
->driver
.ioctl(tty
, file
, cmd
, arg
);
1633 /* These two ioctl's always return success; even if */
1634 /* the driver doesn't support them. */
1637 if (!tty
->driver
.ioctl
)
1639 retval
= tty
->driver
.ioctl(tty
, file
, cmd
, arg
);
1640 if (retval
== -ENOIOCTLCMD
)
1647 * Factor out some common prep work
1655 retval
= tty_check_change(tty
);
1658 if (cmd
!= TIOCCBRK
) {
1659 tty_wait_until_sent(tty
, 0);
1660 if (signal_pending(current
))
1668 return tiocsti(tty
, (char *)arg
);
1670 return tiocgwinsz(tty
, (struct winsize
*) arg
);
1672 return tiocswinsz(tty
, real_tty
, (struct winsize
*) arg
);
1674 return tioccons(tty
, real_tty
);
1676 return fionbio(file
, (int *) arg
);
1678 set_bit(TTY_EXCLUSIVE
, &tty
->flags
);
1681 clear_bit(TTY_EXCLUSIVE
, &tty
->flags
);
1684 if (current
->tty
!= tty
)
1686 if (current
->leader
)
1687 disassociate_ctty(0);
1688 current
->tty
= NULL
;
1691 return tiocsctty(tty
, arg
);
1693 return tiocgpgrp(tty
, real_tty
, (pid_t
*) arg
);
1695 return tiocspgrp(tty
, real_tty
, (pid_t
*) arg
);
1697 return tiocgsid(tty
, real_tty
, (pid_t
*) arg
);
1699 return put_user(tty
->ldisc
.num
, (int *) arg
);
1701 return tiocsetd(tty
, (int *) arg
);
1704 return tioclinux(tty
, arg
);
1706 case TIOCTTYGSTRUCT
:
1707 return tiocttygstruct(tty
, (struct tty_struct
*) arg
);
1712 case TIOCSBRK
: /* Turn break on, unconditionally */
1713 tty
->driver
.break_ctl(tty
, -1);
1716 case TIOCCBRK
: /* Turn break off, unconditionally */
1717 tty
->driver
.break_ctl(tty
, 0);
1719 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1721 * XXX is the above comment correct, or the
1722 * code below correct? Is this ioctl used at
1726 return send_break(tty
, HZ
/4);
1728 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1729 return send_break(tty
, arg
? arg
*(HZ
/10) : HZ
/4);
1731 if (tty
->driver
.ioctl
) {
1732 int retval
= (tty
->driver
.ioctl
)(tty
, file
, cmd
, arg
);
1733 if (retval
!= -ENOIOCTLCMD
)
1736 if (tty
->ldisc
.ioctl
) {
1737 int retval
= (tty
->ldisc
.ioctl
)(tty
, file
, cmd
, arg
);
1738 if (retval
!= -ENOIOCTLCMD
)
1746 * This implements the "Secure Attention Key" --- the idea is to
1747 * prevent trojan horses by killing all processes associated with this
1748 * tty when the user hits the "Secure Attention Key". Required for
1749 * super-paranoid applications --- see the Orange Book for more details.
1751 * This code could be nicer; ideally it should send a HUP, wait a few
1752 * seconds, then send a INT, and then a KILL signal. But you then
1753 * have to coordinate with the init process, since all processes associated
1754 * with the current tty must be dead before the new getty is allowed
1757 void do_SAK( struct tty_struct
*tty
)
1762 struct task_struct
*p
;
1769 session
= tty
->session
;
1770 if (tty
->ldisc
.flush_buffer
)
1771 tty
->ldisc
.flush_buffer(tty
);
1772 if (tty
->driver
.flush_buffer
)
1773 tty
->driver
.flush_buffer(tty
);
1774 read_lock(&tasklist_lock
);
1776 if ((p
->tty
== tty
) ||
1777 ((session
> 0) && (p
->session
== session
)))
1778 send_sig(SIGKILL
, p
, 1);
1779 else if (p
->files
) {
1780 for (i
=0; i
< p
->files
->max_fds
; i
++) {
1781 filp
= fcheck_task(p
, i
);
1782 if (filp
&& (filp
->f_op
== &tty_fops
) &&
1783 (filp
->private_data
== tty
)) {
1784 send_sig(SIGKILL
, p
, 1);
1790 read_unlock(&tasklist_lock
);
1795 * This routine is called out of the software interrupt to flush data
1796 * from the flip buffer to the line discipline.
1798 static void flush_to_ldisc(void *private_
)
1800 struct tty_struct
*tty
= (struct tty_struct
*) private_
;
1804 unsigned long flags
;
1806 if (tty
->flip
.buf_num
) {
1807 cp
= tty
->flip
.char_buf
+ TTY_FLIPBUF_SIZE
;
1808 fp
= tty
->flip
.flag_buf
+ TTY_FLIPBUF_SIZE
;
1809 tty
->flip
.buf_num
= 0;
1811 save_flags(flags
); cli();
1812 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
;
1813 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
;
1815 cp
= tty
->flip
.char_buf
;
1816 fp
= tty
->flip
.flag_buf
;
1817 tty
->flip
.buf_num
= 1;
1819 save_flags(flags
); cli();
1820 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
+ TTY_FLIPBUF_SIZE
;
1821 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
+ TTY_FLIPBUF_SIZE
;
1823 count
= tty
->flip
.count
;
1824 tty
->flip
.count
= 0;
1825 restore_flags(flags
);
1827 tty
->ldisc
.receive_buf(tty
, cp
, fp
, count
);
1831 * Routine which returns the baud rate of the tty
1835 * This is used to figure out the divisor speeds and the timeouts
1837 static int baud_table
[] = {
1838 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
1839 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };
1841 int tty_get_baud_rate(struct tty_struct
*tty
)
1843 unsigned int cflag
, i
;
1845 cflag
= tty
->termios
->c_cflag
;
1851 tty
->termios
->c_cflag
&= ~CBAUDEX
;
1855 if (i
==15 && tty
->alt_speed
) {
1857 printk("Use of setserial/setrocket to set SPD_* flags is deprecated\n");
1860 return(tty
->alt_speed
);
1863 return baud_table
[i
];
1866 void tty_flip_buffer_push(struct tty_struct
*tty
)
1868 if (tty
->low_latency
)
1869 flush_to_ldisc((void *) tty
);
1871 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
1875 * This subroutine initializes a tty structure.
1877 static void initialize_tty_struct(struct tty_struct
*tty
)
1879 memset(tty
, 0, sizeof(struct tty_struct
));
1880 tty
->magic
= TTY_MAGIC
;
1881 tty
->ldisc
= ldiscs
[N_TTY
];
1883 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
;
1884 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
;
1885 tty
->flip
.tqueue
.routine
= flush_to_ldisc
;
1886 tty
->flip
.tqueue
.data
= tty
;
1887 tty
->flip
.pty_sem
= MUTEX
;
1888 tty
->tq_hangup
.routine
= do_tty_hangup
;
1889 tty
->tq_hangup
.data
= tty
;
1893 * The default put_char routine if the driver did not define one.
1895 void tty_default_put_char(struct tty_struct
*tty
, unsigned char ch
)
1897 tty
->driver
.write(tty
, 0, &ch
, 1);
1901 * Called by a tty driver to register itself.
1903 int tty_register_driver(struct tty_driver
*driver
)
1907 if (driver
->flags
& TTY_DRIVER_INSTALLED
)
1910 error
= register_chrdev(driver
->major
, driver
->name
, &tty_fops
);
1913 else if(driver
->major
== 0)
1914 driver
->major
= error
;
1916 if (!driver
->put_char
)
1917 driver
->put_char
= tty_default_put_char
;
1920 driver
->next
= tty_drivers
;
1921 if (tty_drivers
) tty_drivers
->prev
= driver
;
1922 tty_drivers
= driver
;
1924 #ifdef CONFIG_PROC_FS
1925 proc_tty_register_driver(driver
);
1931 * Called by a tty driver to unregister itself.
1933 int tty_unregister_driver(struct tty_driver
*driver
)
1936 struct tty_driver
*p
;
1938 const char *othername
= NULL
;
1940 if (*driver
->refcount
)
1943 for (p
= tty_drivers
; p
; p
= p
->next
) {
1946 else if (p
->major
== driver
->major
)
1947 othername
= p
->name
;
1953 if (othername
== NULL
) {
1954 retval
= unregister_chrdev(driver
->major
, driver
->name
);
1958 register_chrdev(driver
->major
, othername
, &tty_fops
);
1961 driver
->prev
->next
= driver
->next
;
1963 tty_drivers
= driver
->next
;
1966 driver
->next
->prev
= driver
->prev
;
1968 #ifdef CONFIG_PROC_FS
1969 proc_tty_unregister_driver(driver
);
1976 * Initialize the console device. This is called *early*, so
1977 * we can't necessarily depend on lots of kernel help here.
1978 * Just do some early initializations, and do the complex setup
1981 long __init
console_init(long kmem_start
, long kmem_end
)
1983 /* Setup the default TTY line discipline. */
1984 memset(ldiscs
, 0, sizeof(ldiscs
));
1985 (void) tty_register_ldisc(N_TTY
, &tty_ldisc_N_TTY
);
1988 * Set up the standard termios. Individual tty drivers may
1989 * deviate from this; this is used as a template.
1991 memset(&tty_std_termios
, 0, sizeof(struct termios
));
1992 memcpy(tty_std_termios
.c_cc
, INIT_C_CC
, NCCS
);
1993 tty_std_termios
.c_iflag
= ICRNL
| IXON
;
1994 tty_std_termios
.c_oflag
= OPOST
| ONLCR
;
1995 tty_std_termios
.c_cflag
= B38400
| CS8
| CREAD
| HUPCL
;
1996 tty_std_termios
.c_lflag
= ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
|
1997 ECHOCTL
| ECHOKE
| IEXTEN
;
2000 * set up the console device so that later boot sequences can
2001 * inform about problems etc..
2004 kmem_start
= con_init(kmem_start
);
2006 #ifdef CONFIG_SERIAL_CONSOLE
2007 kmem_start
= serial_console_init(kmem_start
, kmem_end
);
2012 static struct tty_driver dev_tty_driver
, dev_syscons_driver
, dev_ptmx_driver
;
2014 static struct tty_driver dev_console_driver
;
2018 * Ok, now we can initialize the rest of the tty devices and can count
2019 * on memory allocations, interrupts etc..
2021 __initfunc(int tty_init(void))
2023 if (sizeof(struct tty_struct
) > PAGE_SIZE
)
2024 panic("size of tty structure > PAGE_SIZE!");
2027 * dev_tty_driver and dev_console_driver are actually magic
2028 * devices which get redirected at open time. Nevertheless,
2029 * we register them so that register_chrdev is called
2032 memset(&dev_tty_driver
, 0, sizeof(struct tty_driver
));
2033 dev_tty_driver
.magic
= TTY_DRIVER_MAGIC
;
2034 dev_tty_driver
.driver_name
= "/dev/tty";
2035 dev_tty_driver
.name
= dev_tty_driver
.driver_name
+ 5;
2036 dev_tty_driver
.name_base
= 0;
2037 dev_tty_driver
.major
= TTYAUX_MAJOR
;
2038 dev_tty_driver
.minor_start
= 0;
2039 dev_tty_driver
.num
= 1;
2040 dev_tty_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2041 dev_tty_driver
.subtype
= SYSTEM_TYPE_TTY
;
2043 if (tty_register_driver(&dev_tty_driver
))
2044 panic("Couldn't register /dev/tty driver\n");
2046 dev_syscons_driver
= dev_tty_driver
;
2047 dev_syscons_driver
.driver_name
= "/dev/console";
2048 dev_syscons_driver
.name
= dev_syscons_driver
.driver_name
+ 5;
2049 dev_syscons_driver
.major
= TTYAUX_MAJOR
;
2050 dev_syscons_driver
.minor_start
= 1;
2051 dev_syscons_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2052 dev_syscons_driver
.subtype
= SYSTEM_TYPE_SYSCONS
;
2054 if (tty_register_driver(&dev_syscons_driver
))
2055 panic("Couldn't register /dev/console driver\n");
2057 dev_ptmx_driver
= dev_tty_driver
;
2058 dev_ptmx_driver
.driver_name
= "/dev/ptmx";
2059 dev_ptmx_driver
.name
= dev_ptmx_driver
.driver_name
+ 5;
2060 dev_ptmx_driver
.major
= MAJOR(PTMX_DEV
);
2061 dev_ptmx_driver
.minor_start
= MINOR(PTMX_DEV
);
2062 dev_ptmx_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2063 dev_ptmx_driver
.subtype
= SYSTEM_TYPE_SYSPTMX
;
2065 if (tty_register_driver(&dev_ptmx_driver
))
2066 panic("Couldn't register /dev/ptmx driver\n");
2069 dev_console_driver
= dev_tty_driver
;
2070 dev_console_driver
.driver_name
= "/dev/tty0";
2071 dev_console_driver
.name
= dev_console_driver
.driver_name
+ 5;
2072 dev_console_driver
.major
= TTY_MAJOR
;
2073 dev_console_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2074 dev_console_driver
.subtype
= SYSTEM_TYPE_CONSOLE
;
2076 if (tty_register_driver(&dev_console_driver
))
2077 panic("Couldn't register /dev/tty0 driver\n");
2081 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2084 #ifdef CONFIG_SERIAL
2087 #ifdef CONFIG_MAC_SERIAL
2090 #ifdef CONFIG_ROCKETPORT
2093 #ifdef CONFIG_CYCLADES
2096 #ifdef CONFIG_STALLION
2099 #ifdef CONFIG_ISTALLION
2105 #ifdef CONFIG_DIGIEPCA
2108 #ifdef CONFIG_RISCOM8
2111 #ifdef CONFIG_SPECIALIX
2116 #endif /* CONFIG_8xx */