2
* linux/drivers/char/tty_io.c
4
* Copyright (C) 1991, 1992 Linus Torvalds
8
* 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
9
* or rs-channels. It also implements echoing, cooked mode etc.
11
* Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
13
* Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
14
* tty_struct and tty_queue structures. Previously there was an array
15
* of 256 tty_struct's which was statically allocated, and the
16
* tty_queue structures were allocated at boot time. Both are now
17
* dynamically allocated only when the tty is open.
19
* Also restructured routines so that there is more of a separation
20
* between the high-level tty routines (tty_io.c and tty_ioctl.c) and
21
* the low-level tty routines (serial.c, pty.c, console.c). This
22
* makes for cleaner and more compact code. -TYT, 9/17/92
24
* Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
25
* which can be dynamically activated and de-activated by the line
26
* discipline handling modules (like SLIP).
28
* NOTE: pay no attention to the line discipline code (yet); its
29
* interface is still subject to change in this version...
32
* Added functionality to the OPOST tty handling. No delays, but all
33
* other bits should be there.
34
* -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
36
* Rewrote canonical mode and added more termios flags.
37
* -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
39
* Reorganized FASYNC support so mouse code can share it.
40
* -- ctm@ardi.com, 9Sep95
42
* New TIOCLINUX variants added.
43
* -- mj@k332.feld.cvut.cz, 19-Nov-95
45
* Restrict vt switching via ioctl()
46
* -- grif@cs.ucr.edu, 5-Dec-95
48
* Move console and virtual terminal code to more appropriate files,
49
* implement CONFIG_VT and generalize console device interface.
50
* -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
52
* Rewrote init_dev and release_dev to eliminate races.
53
* -- Bill Hawes <whawes@star.net>, June 97
55
* Added devfs support.
56
* -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
58
* Added support for a Unix98-style ptmx device.
59
* -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
61
* Reduced memory usage for older ARM systems
62
* -- Russell King <rmk@arm.linux.org.uk>
64
* Move do_SAK() into process context. Less stack use in devfs functions.
65
* alloc_tty_struct() always uses kmalloc() -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
68
#include <linux/config.h>
69
#include <linux/types.h>
70
#include <linux/major.h>
71
#include <linux/errno.h>
72
#include <linux/signal.h>
73
#include <linux/fcntl.h>
74
#include <linux/sched.h>
75
#include <linux/interrupt.h>
76
#include <linux/tty.h>
77
#include <linux/tty_driver.h>
78
#include <linux/tty_flip.h>
79
#include <linux/devpts_fs.h>
80
#include <linux/file.h>
81
#include <linux/console.h>
82
#include <linux/timer.h>
83
#include <linux/ctype.h>
86
#include <linux/string.h>
87
#include <linux/slab.h>
88
#include <linux/poll.h>
89
#include <linux/proc_fs.h>
90
#include <linux/init.h>
91
#include <linux/module.h>
92
#include <linux/smp_lock.h>
94
#include <asm/uaccess.h>
95
#include <asm/system.h>
96
#include <asm/bitops.h>
98
#include <linux/kbd_kern.h>
99
#include <linux/vt_kern.h>
100
#include <linux/selection.h>
101
#include <linux/devfs_fs_kernel.h>
103
#include <linux/kmod.h>
105
#ifdef CONFIG_XEN_CONSOLE
106
extern void xen_console_init(void);
110
extern void con_init_devfs (void);
113
extern void disable_early_printk(void);
115
#define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
116
#define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
117
#define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
118
#define PTMX_DEV MKDEV(TTYAUX_MAJOR,2)
120
#undef TTY_DEBUG_HANGUP
122
#define TTY_PARANOIA_CHECK 1
123
#define CHECK_TTY_COUNT 1
125
struct termios tty_std_termios; /* for the benefit of tty drivers */
126
struct tty_driver *tty_drivers; /* linked list of tty drivers */
127
struct tty_ldisc ldiscs[NR_LDISCS]; /* line disc dispatch table */
129
#ifdef CONFIG_UNIX98_PTYS
130
extern struct tty_driver ptm_driver[]; /* Unix98 pty masters; for /dev/ptmx */
131
extern struct tty_driver pts_driver[]; /* Unix98 pty slaves; for /dev/ptmx */
134
static void initialize_tty_struct(struct tty_struct *tty);
136
static ssize_t tty_read(struct file *, char *, size_t, loff_t *);
137
static ssize_t tty_write(struct file *, const char *, size_t, loff_t *);
138
static unsigned int tty_poll(struct file *, poll_table *);
139
static int tty_open(struct inode *, struct file *);
140
static int tty_release(struct inode *, struct file *);
141
int tty_ioctl(struct inode * inode, struct file * file,
142
unsigned int cmd, unsigned long arg);
143
static int tty_fasync(int fd, struct file * filp, int on);
144
extern int vme_scc_init (void);
145
extern long vme_scc_console_init(void);
146
extern int serial167_init(void);
147
extern long serial167_console_init(void);
148
extern void console_8xx_init(void);
149
extern void au1x00_serial_console_init(void);
150
extern int rs_8xx_init(void);
151
extern void mac_scc_console_init(void);
152
extern void hwc_console_init(void);
153
extern void hwc_tty_init(void);
154
extern void con3215_init(void);
155
extern void tty3215_init(void);
156
extern void tub3270_con_init(void);
157
extern void tub3270_init(void);
158
extern void rs285_console_init(void);
159
extern void sa1100_rs_console_init(void);
160
extern void sgi_serial_console_init(void);
161
extern void sn_sal_serial_console_init(void);
162
extern void sci_console_init(void);
163
extern void dec_serial_console_init(void);
164
extern void tx3912_console_init(void);
165
extern void tx3912_rs_init(void);
166
extern void txx927_console_init(void);
167
extern void txx9_rs_init(void);
168
extern void txx9_serial_console_init(void);
169
extern void sb1250_serial_console_init(void);
170
extern void arc_console_init(void);
171
extern int hvc_console_init(void);
174
#define MIN(a,b) ((a) < (b) ? (a) : (b))
177
#define MAX(a,b) ((a) < (b) ? (b) : (a))
180
static struct tty_struct *alloc_tty_struct(void)
182
struct tty_struct *tty;
184
tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
186
memset(tty, 0, sizeof(struct tty_struct));
190
static inline void free_tty_struct(struct tty_struct *tty)
196
* This routine returns the name of tty.
199
_tty_make_name(struct tty_struct *tty, const char *name, char *buf)
201
int idx = (tty)?MINOR(tty->device) - tty->driver.minor_start:0;
203
if (!tty) /* Hmm. NULL pointer. That's fun. */
204
strcpy(buf, "NULL tty");
207
idx + tty->driver.name_base);
212
#define TTY_NUMBER(tty) (MINOR((tty)->device) - (tty)->driver.minor_start + \
213
(tty)->driver.name_base)
215
char *tty_name(struct tty_struct *tty, char *buf)
217
return _tty_make_name(tty, (tty)?tty->driver.name:NULL, buf);
220
inline int tty_paranoia_check(struct tty_struct *tty, kdev_t device,
223
#ifdef TTY_PARANOIA_CHECK
224
static const char badmagic[] = KERN_WARNING
225
"Warning: bad magic number for tty struct (%s) in %s\n";
226
static const char badtty[] = KERN_WARNING
227
"Warning: null TTY for (%s) in %s\n";
230
printk(badtty, kdevname(device), routine);
233
if (tty->magic != TTY_MAGIC) {
234
printk(badmagic, kdevname(device), routine);
241
static int check_tty_count(struct tty_struct *tty, const char *routine)
243
#ifdef CHECK_TTY_COUNT
248
for(p = tty->tty_files.next; p != &tty->tty_files; p = p->next) {
249
if(list_entry(p, struct file, f_list)->private_data == tty)
253
if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
254
tty->driver.subtype == PTY_TYPE_SLAVE &&
255
tty->link && tty->link->count)
257
if (tty->count != count) {
258
printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
259
"!= #fd's(%d) in %s\n",
260
kdevname(tty->device), tty->count, count, routine);
267
int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
269
if (disc < N_TTY || disc >= NR_LDISCS)
273
ldiscs[disc] = *new_ldisc;
274
ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
275
ldiscs[disc].num = disc;
277
memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
282
EXPORT_SYMBOL(tty_register_ldisc);
284
/* Set the discipline of a tty line. */
285
static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
288
struct tty_ldisc o_ldisc;
291
if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
293
/* Eduardo Blanco <ejbs@cs.cs.com.uy> */
294
/* Cyrus Durgin <cider@speakeasy.org> */
295
if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED)) {
297
sprintf(modname, "tty-ldisc-%d", ldisc);
298
request_module (modname);
300
if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
303
if (tty->ldisc.num == ldisc)
304
return 0; /* We are already in the desired discipline */
305
o_ldisc = tty->ldisc;
307
tty_wait_until_sent(tty, 0);
309
/* Shutdown the current discipline. */
310
if (tty->ldisc.close)
311
(tty->ldisc.close)(tty);
313
/* Now set up the new line discipline. */
314
tty->ldisc = ldiscs[ldisc];
315
tty->termios->c_line = ldisc;
317
retval = (tty->ldisc.open)(tty);
319
tty->ldisc = o_ldisc;
320
tty->termios->c_line = tty->ldisc.num;
321
if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
322
tty->ldisc = ldiscs[N_TTY];
323
tty->termios->c_line = N_TTY;
324
if (tty->ldisc.open) {
325
int r = tty->ldisc.open(tty);
328
panic("Couldn't open N_TTY ldisc for "
330
tty_name(tty, buf), r);
334
if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
335
tty->driver.set_ldisc(tty);
340
* This routine returns a tty driver structure, given a device number
342
struct tty_driver *get_tty_driver(kdev_t device)
345
struct tty_driver *p;
347
minor = MINOR(device);
348
major = MAJOR(device);
350
for (p = tty_drivers; p; p = p->next) {
351
if (p->major != major)
353
if (minor < p->minor_start)
355
if (minor >= p->minor_start + p->num)
363
* If we try to write to, or set the state of, a terminal and we're
364
* not in the foreground, send a SIGTTOU. If the signal is blocked or
365
* ignored, go ahead and perform the operation. (POSIX 7.2)
367
int tty_check_change(struct tty_struct * tty)
369
if (current->tty != tty)
371
if (tty->pgrp <= 0) {
372
printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
375
if (current->pgrp == tty->pgrp)
377
if (is_ignored(SIGTTOU))
379
if (is_orphaned_pgrp(current->pgrp))
381
(void) kill_pg(current->pgrp,SIGTTOU,1);
385
static ssize_t hung_up_tty_read(struct file * file, char * buf,
386
size_t count, loff_t *ppos)
388
/* Can't seek (pread) on ttys. */
389
if (ppos != &file->f_pos)
394
static ssize_t hung_up_tty_write(struct file * file, const char * buf,
395
size_t count, loff_t *ppos)
397
/* Can't seek (pwrite) on ttys. */
398
if (ppos != &file->f_pos)
403
/* No kernel lock held - none needed ;) */
404
static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
406
return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
409
static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
410
unsigned int cmd, unsigned long arg)
412
return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
415
static struct file_operations tty_fops = {
422
release: tty_release,
426
static struct file_operations hung_up_tty_fops = {
428
read: hung_up_tty_read,
429
write: hung_up_tty_write,
430
poll: hung_up_tty_poll,
431
ioctl: hung_up_tty_ioctl,
432
release: tty_release,
435
static spinlock_t redirect_lock = SPIN_LOCK_UNLOCKED;
436
static struct file *redirect;
438
* This can be called by the "eventd" kernel thread. That is process synchronous,
439
* but doesn't hold any locks, so we need to make sure we have the appropriate
440
* locks for what we're doing..
442
void do_tty_hangup(void *data)
444
struct tty_struct *tty = (struct tty_struct *) data;
445
struct file * cons_filp = NULL;
446
struct file *f = NULL;
447
struct task_struct *p;
449
int closecount = 0, n;
454
/* inuse_filps is protected by the single kernel lock */
457
spin_lock(&redirect_lock);
458
if (redirect && redirect->private_data == tty) {
462
spin_unlock(&redirect_lock);
464
check_tty_count(tty, "do_tty_hangup");
466
for (l = tty->tty_files.next; l != &tty->tty_files; l = l->next) {
467
struct file * filp = list_entry(l, struct file, f_list);
468
if (filp->f_dentry->d_inode->i_rdev == CONSOLE_DEV ||
469
filp->f_dentry->d_inode->i_rdev == SYSCONS_DEV) {
473
if (filp->f_op != &tty_fops)
476
tty_fasync(-1, filp, 0); /* can't block */
477
filp->f_op = &hung_up_tty_fops;
481
/* FIXME! What are the locking issues here? This may me overdoing things.. */
485
save_flags(flags); cli();
486
if (tty->ldisc.flush_buffer)
487
tty->ldisc.flush_buffer(tty);
488
if (tty->driver.flush_buffer)
489
tty->driver.flush_buffer(tty);
490
if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
491
tty->ldisc.write_wakeup)
492
(tty->ldisc.write_wakeup)(tty);
493
restore_flags(flags);
496
wake_up_interruptible(&tty->write_wait);
497
wake_up_interruptible(&tty->read_wait);
500
* Shutdown the current line discipline, and reset it to
503
if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
504
*tty->termios = tty->driver.init_termios;
505
if (tty->ldisc.num != ldiscs[N_TTY].num) {
506
if (tty->ldisc.close)
507
(tty->ldisc.close)(tty);
508
tty->ldisc = ldiscs[N_TTY];
509
tty->termios->c_line = N_TTY;
510
if (tty->ldisc.open) {
511
int i = (tty->ldisc.open)(tty);
513
printk(KERN_ERR "do_tty_hangup: N_TTY open: "
518
read_lock(&tasklist_lock);
520
if ((tty->session > 0) && (p->session == tty->session) &&
522
send_sig(SIGHUP,p,1);
523
send_sig(SIGCONT,p,1);
525
p->tty_old_pgrp = tty->pgrp;
530
read_unlock(&tasklist_lock);
535
tty->ctrl_status = 0;
537
* If one of the devices matches a console pointer, we
538
* cannot just call hangup() because that will cause
539
* tty->count and state->count to go out of sync.
540
* So we just call close() the right number of times.
543
if (tty->driver.close)
544
for (n = 0; n < closecount; n++)
545
tty->driver.close(tty, cons_filp);
546
} else if (tty->driver.hangup)
547
(tty->driver.hangup)(tty);
553
void tty_hangup(struct tty_struct * tty)
555
#ifdef TTY_DEBUG_HANGUP
558
printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
560
schedule_task(&tty->tq_hangup);
563
void tty_vhangup(struct tty_struct * tty)
565
#ifdef TTY_DEBUG_HANGUP
568
printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
570
do_tty_hangup((void *) tty);
573
int tty_hung_up_p(struct file * filp)
575
return (filp->f_op == &hung_up_tty_fops);
579
* This function is typically called only by the session leader, when
580
* it wants to disassociate itself from its controlling tty.
582
* It performs the following functions:
583
* (1) Sends a SIGHUP and SIGCONT to the foreground process group
584
* (2) Clears the tty from being controlling the session
585
* (3) Clears the controlling tty for all processes in the
588
* The argument on_exit is set to 1 if called when a process is
589
* exiting; it is 0 if called by the ioctl TIOCNOTTY.
591
void disassociate_ctty(int on_exit)
593
struct tty_struct *tty = current->tty;
594
struct task_struct *p;
598
tty_pgrp = tty->pgrp;
599
if (on_exit && tty->driver.type != TTY_DRIVER_TYPE_PTY)
602
if (current->tty_old_pgrp) {
603
kill_pg(current->tty_old_pgrp, SIGHUP, on_exit);
604
kill_pg(current->tty_old_pgrp, SIGCONT, on_exit);
609
kill_pg(tty_pgrp, SIGHUP, on_exit);
611
kill_pg(tty_pgrp, SIGCONT, on_exit);
614
current->tty_old_pgrp = 0;
618
read_lock(&tasklist_lock);
620
if (p->session == current->session)
622
read_unlock(&tasklist_lock);
625
void stop_tty(struct tty_struct *tty)
630
if (tty->link && tty->link->packet) {
631
tty->ctrl_status &= ~TIOCPKT_START;
632
tty->ctrl_status |= TIOCPKT_STOP;
633
wake_up_interruptible(&tty->link->read_wait);
635
if (tty->driver.stop)
636
(tty->driver.stop)(tty);
639
void start_tty(struct tty_struct *tty)
641
if (!tty->stopped || tty->flow_stopped)
644
if (tty->link && tty->link->packet) {
645
tty->ctrl_status &= ~TIOCPKT_STOP;
646
tty->ctrl_status |= TIOCPKT_START;
647
wake_up_interruptible(&tty->link->read_wait);
649
if (tty->driver.start)
650
(tty->driver.start)(tty);
651
if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
652
tty->ldisc.write_wakeup)
653
(tty->ldisc.write_wakeup)(tty);
654
wake_up_interruptible(&tty->write_wait);
657
static ssize_t tty_read(struct file * file, char * buf, size_t count,
661
struct tty_struct * tty;
664
/* Can't seek (pread) on ttys. */
665
if (ppos != &file->f_pos)
668
tty = (struct tty_struct *)file->private_data;
669
inode = file->f_dentry->d_inode;
670
if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
672
if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
675
/* This check not only needs to be done before reading, but also
676
whenever read_chan() gets woken up after sleeping, so I've
677
moved it to there. This should only be done for the N_TTY
678
line discipline, anyway. Same goes for write_chan(). -- jlc. */
680
if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
682
(current->tty == tty) &&
683
(tty->pgrp != current->pgrp))
684
if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
687
(void) kill_pg(current->pgrp, SIGTTIN, 1);
693
i = (tty->ldisc.read)(tty,file,buf,count);
698
inode->i_atime = CURRENT_TIME;
703
* Split writes up in sane blocksizes to avoid
704
* denial-of-service type attacks
706
static inline ssize_t do_tty_write(
707
ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
708
struct tty_struct *tty,
710
const unsigned char *buf,
713
ssize_t ret = 0, written = 0;
715
if (file->f_flags & O_NONBLOCK) {
716
if (down_trylock(&tty->atomic_write))
720
if (down_interruptible(&tty->atomic_write))
723
if ( test_bit(TTY_NO_WRITE_SPLIT, &tty->flags) ) {
725
written = write(tty, file, buf, count);
729
unsigned long size = MAX(PAGE_SIZE*2,16384);
733
ret = write(tty, file, buf, size);
743
if (signal_pending(current))
745
if (current->need_resched)
750
file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
753
up(&tty->atomic_write);
758
static ssize_t tty_write(struct file * file, const char * buf, size_t count,
762
struct tty_struct * tty;
763
struct inode *inode = file->f_dentry->d_inode;
765
/* Can't seek (pwrite) on ttys. */
766
if (ppos != &file->f_pos)
770
* For now, we redirect writes from /dev/console as
773
inode = file->f_dentry->d_inode;
774
is_console = (inode->i_rdev == SYSCONS_DEV ||
775
inode->i_rdev == CONSOLE_DEV);
778
struct file *p = NULL;
780
spin_lock(&redirect_lock);
785
spin_unlock(&redirect_lock);
788
ssize_t res = p->f_op->write(p, buf, count, &p->f_pos);
794
tty = (struct tty_struct *)file->private_data;
795
if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
797
if (!tty || !tty->driver.write || (test_bit(TTY_IO_ERROR, &tty->flags)))
800
if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
801
(current->tty == tty) && (tty->pgrp != current->pgrp)) {
802
if (is_orphaned_pgrp(current->pgrp))
804
if (!is_ignored(SIGTTOU)) {
805
(void) kill_pg(current->pgrp, SIGTTOU, 1);
810
if (!tty->ldisc.write)
812
return do_tty_write(tty->ldisc.write, tty, file,
813
(const unsigned char *)buf, count);
816
/* Semaphore to protect creating and releasing a tty */
817
static DECLARE_MUTEX(tty_sem);
819
static void down_tty_sem(int index)
824
static void up_tty_sem(int index)
829
static void release_mem(struct tty_struct *tty, int idx);
832
* WSH 06/09/97: Rewritten to remove races and properly clean up after a
833
* failed open. The new code protects the open with a semaphore, so it's
834
* really quite straightforward. The semaphore locking can probably be
835
* relaxed for the (most common) case of reopening a tty.
837
static int init_dev(kdev_t device, struct tty_struct **ret_tty)
839
struct tty_struct *tty, *o_tty;
840
struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
841
struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
842
struct tty_driver *driver;
846
driver = get_tty_driver(device);
850
idx = MINOR(device) - driver->minor_start;
853
* Check whether we need to acquire the tty semaphore to avoid
854
* race conditions. For now, play it safe.
858
/* check whether we're reopening an existing tty */
859
tty = driver->table[idx];
860
if (tty) goto fast_track;
863
* First time open is complex, especially for PTY devices.
864
* This code guarantees that either everything succeeds and the
865
* TTY is ready for operation, or else the table slots are vacated
866
* and the allocated memory released. (Except that the termios
867
* and locked termios may be retained.)
874
tty = alloc_tty_struct();
877
initialize_tty_struct(tty);
878
tty->device = device;
879
tty->driver = *driver;
881
tp_loc = &driver->termios[idx];
883
tp = (struct termios *) kmalloc(sizeof(struct termios),
887
*tp = driver->init_termios;
890
ltp_loc = &driver->termios_locked[idx];
892
ltp = (struct termios *) kmalloc(sizeof(struct termios),
896
memset(ltp, 0, sizeof(struct termios));
899
if (driver->type == TTY_DRIVER_TYPE_PTY) {
900
o_tty = alloc_tty_struct();
903
initialize_tty_struct(o_tty);
904
o_tty->device = (kdev_t) MKDEV(driver->other->major,
905
driver->other->minor_start + idx);
906
o_tty->driver = *driver->other;
908
o_tp_loc = &driver->other->termios[idx];
910
o_tp = (struct termios *)
911
kmalloc(sizeof(struct termios), GFP_KERNEL);
914
*o_tp = driver->other->init_termios;
917
o_ltp_loc = &driver->other->termios_locked[idx];
919
o_ltp = (struct termios *)
920
kmalloc(sizeof(struct termios), GFP_KERNEL);
923
memset(o_ltp, 0, sizeof(struct termios));
927
* Everything allocated ... set up the o_tty structure.
929
driver->other->table[idx] = o_tty;
934
o_tty->termios = *o_tp_loc;
935
o_tty->termios_locked = *o_ltp_loc;
936
(*driver->other->refcount)++;
937
if (driver->subtype == PTY_TYPE_MASTER)
940
/* Establish the links in both directions */
946
* All structures have been allocated, so now we install them.
947
* Failures after this point use release_mem to clean up, so
948
* there's no need to null out the local pointers.
950
driver->table[idx] = tty;
956
tty->termios = *tp_loc;
957
tty->termios_locked = *ltp_loc;
958
(*driver->refcount)++;
962
* Structures all installed ... call the ldisc open routines.
963
* If we fail here just call release_mem to clean up. No need
964
* to decrement the use counts, as release_mem doesn't care.
966
if (tty->ldisc.open) {
967
retval = (tty->ldisc.open)(tty);
969
goto release_mem_out;
971
if (o_tty && o_tty->ldisc.open) {
972
retval = (o_tty->ldisc.open)(o_tty);
974
if (tty->ldisc.close)
975
(tty->ldisc.close)(tty);
976
goto release_mem_out;
982
* This fast open can be used if the tty is already open.
983
* No memory is allocated, and the only failures are from
984
* attempting to open a closing tty or attempting multiple
985
* opens on a pty master.
988
if (test_bit(TTY_CLOSING, &tty->flags)) {
992
if (driver->type == TTY_DRIVER_TYPE_PTY &&
993
driver->subtype == PTY_TYPE_MASTER) {
995
* special case for PTY masters: only one open permitted,
996
* and the slave side open count is incremented as well.
1005
tty->driver = *driver; /* N.B. why do this every time?? */
1010
/* All paths come through here to release the semaphore */
1015
/* Release locally allocated memory ... nothing placed in slots */
1020
free_tty_struct(o_tty);
1025
free_tty_struct(tty);
1031
/* call the tty release_mem routine to clean out this slot */
1033
printk(KERN_INFO "init_dev: ldisc open failed, "
1034
"clearing slot %d\n", idx);
1035
release_mem(tty, idx);
1040
* Releases memory associated with a tty structure, and clears out the
1041
* driver table slots.
1043
static void release_mem(struct tty_struct *tty, int idx)
1045
struct tty_struct *o_tty;
1048
if ((o_tty = tty->link) != NULL) {
1049
o_tty->driver.table[idx] = NULL;
1050
if (o_tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1051
tp = o_tty->driver.termios[idx];
1052
o_tty->driver.termios[idx] = NULL;
1056
(*o_tty->driver.refcount)--;
1057
list_del_init(&o_tty->tty_files);
1058
free_tty_struct(o_tty);
1061
tty->driver.table[idx] = NULL;
1062
if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1063
tp = tty->driver.termios[idx];
1064
tty->driver.termios[idx] = NULL;
1068
(*tty->driver.refcount)--;
1069
list_del_init(&tty->tty_files);
1070
free_tty_struct(tty);
1074
* Even releasing the tty structures is a tricky business.. We have
1075
* to be very careful that the structures are all released at the
1076
* same time, as interrupts might otherwise get the wrong pointers.
1078
* WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1079
* lead to double frees or releasing memory still in use.
1081
static void release_dev(struct file * filp)
1083
struct tty_struct *tty, *o_tty;
1084
int pty_master, tty_closing, o_tty_closing, do_sleep;
1088
tty = (struct tty_struct *)filp->private_data;
1089
if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "release_dev"))
1092
check_tty_count(tty, "release_dev");
1094
tty_fasync(-1, filp, 0);
1096
idx = MINOR(tty->device) - tty->driver.minor_start;
1097
pty_master = (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1098
tty->driver.subtype == PTY_TYPE_MASTER);
1101
#ifdef TTY_PARANOIA_CHECK
1102
if (idx < 0 || idx >= tty->driver.num) {
1103
printk(KERN_DEBUG "release_dev: bad idx when trying to "
1104
"free (%s)\n", kdevname(tty->device));
1107
if (tty != tty->driver.table[idx]) {
1108
printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1109
"for (%s)\n", idx, kdevname(tty->device));
1112
if (tty->termios != tty->driver.termios[idx]) {
1113
printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1115
idx, kdevname(tty->device));
1118
if (tty->termios_locked != tty->driver.termios_locked[idx]) {
1119
printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1120
"termios_locked for (%s)\n",
1121
idx, kdevname(tty->device));
1126
#ifdef TTY_DEBUG_HANGUP
1127
printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1128
tty_name(tty, buf), tty->count);
1131
#ifdef TTY_PARANOIA_CHECK
1132
if (tty->driver.other) {
1133
if (o_tty != tty->driver.other->table[idx]) {
1134
printk(KERN_DEBUG "release_dev: other->table[%d] "
1135
"not o_tty for (%s)\n",
1136
idx, kdevname(tty->device));
1139
if (o_tty->termios != tty->driver.other->termios[idx]) {
1140
printk(KERN_DEBUG "release_dev: other->termios[%d] "
1141
"not o_termios for (%s)\n",
1142
idx, kdevname(tty->device));
1145
if (o_tty->termios_locked !=
1146
tty->driver.other->termios_locked[idx]) {
1147
printk(KERN_DEBUG "release_dev: other->termios_locked["
1148
"%d] not o_termios_locked for (%s)\n",
1149
idx, kdevname(tty->device));
1152
if (o_tty->link != tty) {
1153
printk(KERN_DEBUG "release_dev: bad pty pointers\n");
1159
if (tty->driver.close)
1160
tty->driver.close(tty, filp);
1163
* Sanity check: if tty->count is going to zero, there shouldn't be
1164
* any waiters on tty->read_wait or tty->write_wait. We test the
1165
* wait queues and kick everyone out _before_ actually starting to
1166
* close. This ensures that we won't block while releasing the tty
1169
* The test for the o_tty closing is necessary, since the master and
1170
* slave sides may close in any order. If the slave side closes out
1171
* first, its count will be one, since the master side holds an open.
1172
* Thus this test wouldn't be triggered at the time the slave closes,
1175
* Note that it's possible for the tty to be opened again while we're
1176
* flushing out waiters. By recalculating the closing flags before
1177
* each iteration we avoid any problems.
1180
tty_closing = tty->count <= 1;
1181
o_tty_closing = o_tty &&
1182
(o_tty->count <= (pty_master ? 1 : 0));
1186
if (waitqueue_active(&tty->read_wait)) {
1187
wake_up(&tty->read_wait);
1190
if (waitqueue_active(&tty->write_wait)) {
1191
wake_up(&tty->write_wait);
1195
if (o_tty_closing) {
1196
if (waitqueue_active(&o_tty->read_wait)) {
1197
wake_up(&o_tty->read_wait);
1200
if (waitqueue_active(&o_tty->write_wait)) {
1201
wake_up(&o_tty->write_wait);
1208
printk(KERN_WARNING "release_dev: %s: read/write wait queue "
1209
"active!\n", tty_name(tty, buf));
1214
* The closing flags are now consistent with the open counts on
1215
* both sides, and we've completed the last operation that could
1216
* block, so it's safe to proceed with closing.
1219
if (--o_tty->count < 0) {
1220
printk(KERN_WARNING "release_dev: bad pty slave count "
1222
o_tty->count, tty_name(o_tty, buf));
1226
if (--tty->count < 0) {
1227
printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
1228
tty->count, tty_name(tty, buf));
1233
* We've decremented tty->count, so we should zero out
1234
* filp->private_data, to break the link between the tty and
1235
* the file descriptor. Otherwise if filp_close() blocks before
1236
* the file descriptor is removed from the inuse_filp
1237
* list, check_tty_count() could observe a discrepancy and
1238
* printk a warning message to the user.
1240
filp->private_data = 0;
1243
* Perform some housekeeping before deciding whether to return.
1245
* Set the TTY_CLOSING flag if this was the last open. In the
1246
* case of a pty we may have to wait around for the other side
1247
* to close, and TTY_CLOSING makes sure we can't be reopened.
1250
set_bit(TTY_CLOSING, &tty->flags);
1252
set_bit(TTY_CLOSING, &o_tty->flags);
1255
* If _either_ side is closing, make sure there aren't any
1256
* processes that still think tty or o_tty is their controlling
1259
if (tty_closing || o_tty_closing) {
1260
struct task_struct *p;
1262
read_lock(&tasklist_lock);
1264
if (p->tty == tty || (o_tty && p->tty == o_tty))
1267
read_unlock(&tasklist_lock);
1270
/* check whether both sides are closing ... */
1271
if (!tty_closing || (o_tty && !o_tty_closing))
1274
#ifdef TTY_DEBUG_HANGUP
1275
printk(KERN_DEBUG "freeing tty structure...");
1279
* Shutdown the current line discipline, and reset it to N_TTY.
1280
* N.B. why reset ldisc when we're releasing the memory??
1282
if (tty->ldisc.close)
1283
(tty->ldisc.close)(tty);
1284
tty->ldisc = ldiscs[N_TTY];
1285
tty->termios->c_line = N_TTY;
1287
if (o_tty->ldisc.close)
1288
(o_tty->ldisc.close)(o_tty);
1289
o_tty->ldisc = ldiscs[N_TTY];
1293
* Make sure that the tty's task queue isn't activated.
1295
run_task_queue(&tq_timer);
1296
flush_scheduled_tasks();
1299
* The release_mem function takes care of the details of clearing
1300
* the slots and preserving the termios structure.
1302
release_mem(tty, idx);
1306
* tty_open and tty_release keep up the tty count that contains the
1307
* number of opens done on a tty. We cannot use the inode-count, as
1308
* different inodes might point to the same tty.
1310
* Open-counting is needed for pty masters, as well as for keeping
1311
* track of serial lines: DTR is dropped when the last close happens.
1312
* (This is not done solely through tty->count, now. - Ted 1/27/92)
1314
* The termios state of a pty is reset on first open so that
1315
* settings don't persist across reuse.
1317
static int tty_open(struct inode * inode, struct file * filp)
1319
struct tty_struct *tty;
1322
unsigned short saved_flags;
1325
saved_flags = filp->f_flags;
1327
noctty = filp->f_flags & O_NOCTTY;
1328
device = inode->i_rdev;
1329
if (device == TTY_DEV) {
1332
device = current->tty->device;
1333
filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1337
if (device == CONSOLE_DEV) {
1338
extern int fg_console;
1339
device = MKDEV(TTY_MAJOR, fg_console + 1);
1343
if (device == SYSCONS_DEV) {
1344
struct console *c = console_drivers;
1345
while(c && !c->device)
1349
device = c->device(c);
1350
filp->f_flags |= O_NONBLOCK; /* Don't let /dev/console block */
1354
if (device == PTMX_DEV) {
1355
#ifdef CONFIG_UNIX98_PTYS
1357
/* find a free pty. */
1359
struct tty_driver *driver;
1361
/* find a device that is not in use. */
1363
for ( major = 0 ; major < UNIX98_NR_MAJORS ; major++ ) {
1364
driver = &ptm_driver[major];
1365
for (minor = driver->minor_start ;
1366
minor < driver->minor_start + driver->num ;
1368
device = MKDEV(driver->major, minor);
1369
if (!init_dev(device, &tty)) goto ptmx_found; /* ok! */
1372
return -EIO; /* no free ptys */
1374
set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1375
minor -= driver->minor_start;
1376
devpts_pty_new(driver->other->name_base + minor, MKDEV(driver->other->major, minor + driver->other->minor_start));
1377
tty_register_devfs(&pts_driver[major], DEVFS_FL_DEFAULT,
1378
pts_driver[major].minor_start + minor);
1382
#else /* CONFIG_UNIX_98_PTYS */
1386
#endif /* CONFIG_UNIX_98_PTYS */
1389
retval = init_dev(device, &tty);
1393
#ifdef CONFIG_UNIX98_PTYS
1396
filp->private_data = tty;
1397
file_move(filp, &tty->tty_files);
1398
check_tty_count(tty, "tty_open");
1399
if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1400
tty->driver.subtype == PTY_TYPE_MASTER)
1402
#ifdef TTY_DEBUG_HANGUP
1403
printk(KERN_DEBUG "opening %s...", tty_name(tty, buf));
1405
if (tty->driver.open)
1406
retval = tty->driver.open(tty, filp);
1409
filp->f_flags = saved_flags;
1411
if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1415
#ifdef TTY_DEBUG_HANGUP
1416
printk(KERN_DEBUG "error %d in opening %s...", retval,
1417
tty_name(tty, buf));
1421
if (retval != -ERESTARTSYS)
1423
if (signal_pending(current))
1427
* Need to reset f_op in case a hangup happened.
1429
filp->f_op = &tty_fops;
1435
tty->session == 0) {
1438
task_unlock(current);
1439
current->tty_old_pgrp = 0;
1440
tty->session = current->session;
1441
tty->pgrp = current->pgrp;
1443
if ((tty->driver.type == TTY_DRIVER_TYPE_SERIAL) &&
1444
(tty->driver.subtype == SERIAL_TYPE_CALLOUT) &&
1445
(tty->count == 1)) {
1446
static int nr_warns;
1448
printk(KERN_WARNING "tty_io.c: "
1449
"process %d (%s) used obsolete /dev/%s - "
1450
"update software to use /dev/ttyS%d\n",
1451
current->pid, current->comm,
1452
tty_name(tty, buf), TTY_NUMBER(tty));
1459
static int tty_release(struct inode * inode, struct file * filp)
1467
/* No kernel lock held - fine */
1468
static unsigned int tty_poll(struct file * filp, poll_table * wait)
1470
struct tty_struct * tty;
1472
tty = (struct tty_struct *)filp->private_data;
1473
if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_poll"))
1476
if (tty->ldisc.poll)
1477
return (tty->ldisc.poll)(tty, filp, wait);
1481
static int tty_fasync(int fd, struct file * filp, int on)
1483
struct tty_struct * tty;
1486
tty = (struct tty_struct *)filp->private_data;
1487
if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_fasync"))
1490
retval = fasync_helper(fd, filp, on, &tty->fasync);
1495
if (!waitqueue_active(&tty->read_wait))
1496
tty->minimum_to_wake = 1;
1497
if (filp->f_owner.pid == 0) {
1498
filp->f_owner.pid = (-tty->pgrp) ? : current->pid;
1499
filp->f_owner.uid = current->uid;
1500
filp->f_owner.euid = current->euid;
1503
if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1504
tty->minimum_to_wake = N_TTY_BUF_SIZE;
1509
static int tiocsti(struct tty_struct *tty, char * arg)
1513
if ((current->tty != tty) && !suser())
1515
if (get_user(ch, arg))
1517
tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1521
static int tiocgwinsz(struct tty_struct *tty, struct winsize * arg)
1523
if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
1528
static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
1529
struct winsize * arg)
1531
struct winsize tmp_ws;
1533
if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
1535
if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
1538
kill_pg(tty->pgrp, SIGWINCH, 1);
1539
if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
1540
kill_pg(real_tty->pgrp, SIGWINCH, 1);
1541
tty->winsize = tmp_ws;
1542
real_tty->winsize = tmp_ws;
1546
static int tioccons(struct inode *inode, struct file *file)
1548
if (inode->i_rdev == SYSCONS_DEV ||
1549
inode->i_rdev == CONSOLE_DEV) {
1553
spin_lock(&redirect_lock);
1556
spin_unlock(&redirect_lock);
1561
spin_lock(&redirect_lock);
1563
spin_unlock(&redirect_lock);
1568
spin_unlock(&redirect_lock);
1573
static int fionbio(struct file *file, int *arg)
1577
if (get_user(nonblock, arg))
1581
file->f_flags |= O_NONBLOCK;
1583
file->f_flags &= ~O_NONBLOCK;
1587
static int tiocsctty(struct tty_struct *tty, int arg)
1589
if (current->leader &&
1590
(current->session == tty->session))
1593
* The process must be a session leader and
1594
* not have a controlling tty already.
1596
if (!current->leader || current->tty)
1598
if (tty->session > 0) {
1600
* This tty is already the controlling
1601
* tty for another session group!
1603
if ((arg == 1) && suser()) {
1607
struct task_struct *p;
1609
read_lock(&tasklist_lock);
1613
read_unlock(&tasklist_lock);
1619
task_unlock(current);
1620
current->tty_old_pgrp = 0;
1621
tty->session = current->session;
1622
tty->pgrp = current->pgrp;
1626
static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1629
* (tty == real_tty) is a cheap way of
1630
* testing if the tty is NOT a master pty.
1632
if (tty == real_tty && current->tty != real_tty)
1634
return put_user(real_tty->pgrp, arg);
1637
static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1640
int retval = tty_check_change(real_tty);
1646
if (!current->tty ||
1647
(current->tty != real_tty) ||
1648
(real_tty->session != current->session))
1650
if (get_user(pgrp, (pid_t *) arg))
1654
if (session_of_pgrp(pgrp) != current->session)
1656
real_tty->pgrp = pgrp;
1660
static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1663
* (tty == real_tty) is a cheap way of
1664
* testing if the tty is NOT a master pty.
1666
if (tty == real_tty && current->tty != real_tty)
1668
if (real_tty->session <= 0)
1670
return put_user(real_tty->session, arg);
1673
static int tiocttygstruct(struct tty_struct *tty, struct tty_struct *arg)
1675
if (copy_to_user(arg, tty, sizeof(*arg)))
1680
static int tiocsetd(struct tty_struct *tty, int *arg)
1684
if (get_user(ldisc, arg))
1686
return tty_set_ldisc(tty, ldisc);
1689
static int send_break(struct tty_struct *tty, int duration)
1691
set_current_state(TASK_INTERRUPTIBLE);
1693
tty->driver.break_ctl(tty, -1);
1694
if (!signal_pending(current))
1695
schedule_timeout(duration);
1696
tty->driver.break_ctl(tty, 0);
1697
if (signal_pending(current))
1702
static int tty_generic_brk(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1704
if (cmd == TCSBRK && arg)
1707
int retval = tty_check_change(tty);
1710
tty_wait_until_sent(tty, 0);
1711
if (signal_pending(current))
1718
* Split this up, as gcc can choke on it otherwise..
1720
int tty_ioctl(struct inode * inode, struct file * file,
1721
unsigned int cmd, unsigned long arg)
1723
struct tty_struct *tty, *real_tty;
1726
tty = (struct tty_struct *)file->private_data;
1727
if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1731
if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1732
tty->driver.subtype == PTY_TYPE_MASTER)
1733
real_tty = tty->link;
1736
* Break handling by driver
1738
if (!tty->driver.break_ctl) {
1742
if (tty->driver.ioctl)
1743
return tty->driver.ioctl(tty, file, cmd, arg);
1746
/* These two ioctl's always return success; even if */
1747
/* the driver doesn't support them. */
1750
retval = -ENOIOCTLCMD;
1751
if (tty->driver.ioctl)
1752
retval = tty->driver.ioctl(tty, file, cmd, arg);
1753
/* Not driver handled */
1754
if (retval == -ENOIOCTLCMD)
1755
retval = tty_generic_brk(tty, file, cmd, arg);
1761
* Factor out some common prep work
1769
retval = tty_check_change(tty);
1772
if (cmd != TIOCCBRK) {
1773
tty_wait_until_sent(tty, 0);
1774
if (signal_pending(current))
1782
return tiocsti(tty, (char *)arg);
1784
return tiocgwinsz(tty, (struct winsize *) arg);
1786
return tiocswinsz(tty, real_tty, (struct winsize *) arg);
1788
return real_tty!=tty ? -EINVAL : tioccons(inode, file);
1790
return fionbio(file, (int *) arg);
1792
set_bit(TTY_EXCLUSIVE, &tty->flags);
1795
clear_bit(TTY_EXCLUSIVE, &tty->flags);
1798
if (current->tty != tty)
1800
if (current->leader)
1801
disassociate_ctty(0);
1803
current->tty = NULL;
1804
task_unlock(current);
1807
return tiocsctty(tty, arg);
1809
return tiocgpgrp(tty, real_tty, (pid_t *) arg);
1811
return tiocspgrp(tty, real_tty, (pid_t *) arg);
1813
return tiocgsid(tty, real_tty, (pid_t *) arg);
1815
return put_user(tty->ldisc.num, (int *) arg);
1817
return tiocsetd(tty, (int *) arg);
1820
return tioclinux(tty, arg);
1822
case TIOCTTYGSTRUCT:
1823
return tiocttygstruct(tty, (struct tty_struct *) arg);
1828
case TIOCSBRK: /* Turn break on, unconditionally */
1829
tty->driver.break_ctl(tty, -1);
1832
case TIOCCBRK: /* Turn break off, unconditionally */
1833
tty->driver.break_ctl(tty, 0);
1835
case TCSBRK: /* SVID version: non-zero arg --> no break */
1837
* XXX is the above comment correct, or the
1838
* code below correct? Is this ioctl used at
1842
return send_break(tty, HZ/4);
1844
case TCSBRKP: /* support for POSIX tcsendbreak() */
1845
return send_break(tty, arg ? arg*(HZ/10) : HZ/4);
1847
if (tty->driver.ioctl) {
1848
int retval = (tty->driver.ioctl)(tty, file, cmd, arg);
1849
if (retval != -ENOIOCTLCMD)
1852
if (tty->ldisc.ioctl) {
1853
int retval = (tty->ldisc.ioctl)(tty, file, cmd, arg);
1854
if (retval != -ENOIOCTLCMD)
1862
* This implements the "Secure Attention Key" --- the idea is to
1863
* prevent trojan horses by killing all processes associated with this
1864
* tty when the user hits the "Secure Attention Key". Required for
1865
* super-paranoid applications --- see the Orange Book for more details.
1867
* This code could be nicer; ideally it should send a HUP, wait a few
1868
* seconds, then send a INT, and then a KILL signal. But you then
1869
* have to coordinate with the init process, since all processes associated
1870
* with the current tty must be dead before the new getty is allowed
1873
* Now, if it would be correct ;-/ The current code has a nasty hole -
1874
* it doesn't catch files in flight. We may send the descriptor to ourselves
1875
* via AF_UNIX socket, close it and later fetch from socket. FIXME.
1877
* Nasty bug: do_SAK is being called in interrupt context. This can
1878
* deadlock. We punt it up to process context. AKPM - 16Mar2001
1880
static void __do_SAK(void *arg)
1885
struct tty_struct *tty = arg;
1886
struct task_struct *p;
1893
session = tty->session;
1894
if (tty->ldisc.flush_buffer)
1895
tty->ldisc.flush_buffer(tty);
1896
if (tty->driver.flush_buffer)
1897
tty->driver.flush_buffer(tty);
1898
read_lock(&tasklist_lock);
1900
if ((p->tty == tty) ||
1901
((session > 0) && (p->session == session))) {
1902
send_sig(SIGKILL, p, 1);
1907
read_lock(&p->files->file_lock);
1908
for (i=0; i < p->files->max_fds; i++) {
1909
filp = fcheck_files(p->files, i);
1910
if (filp && (filp->f_op == &tty_fops) &&
1911
(filp->private_data == tty)) {
1912
send_sig(SIGKILL, p, 1);
1916
read_unlock(&p->files->file_lock);
1920
read_unlock(&tasklist_lock);
1925
* The tq handling here is a little racy - tty->SAK_tq may already be queued.
1926
* But there's no mechanism to fix that without futzing with tqueue_lock.
1927
* Fortunately we don't need to worry, because if ->SAK_tq is already queued,
1928
* the values which we write to it will be identical to the values which it
1929
* already has. --akpm
1931
void do_SAK(struct tty_struct *tty)
1935
PREPARE_TQUEUE(&tty->SAK_tq, __do_SAK, tty);
1936
schedule_task(&tty->SAK_tq);
1940
* This routine is called out of the software interrupt to flush data
1941
* from the flip buffer to the line discipline.
1943
static void flush_to_ldisc(void *private_)
1945
struct tty_struct *tty = (struct tty_struct *) private_;
1949
unsigned long flags;
1951
if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
1952
queue_task(&tty->flip.tqueue, &tq_timer);
1955
if (tty->flip.buf_num) {
1956
cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1957
fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1958
tty->flip.buf_num = 0;
1960
save_flags(flags); cli();
1961
tty->flip.char_buf_ptr = tty->flip.char_buf;
1962
tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1964
cp = tty->flip.char_buf;
1965
fp = tty->flip.flag_buf;
1966
tty->flip.buf_num = 1;
1968
save_flags(flags); cli();
1969
tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1970
tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1972
count = tty->flip.count;
1973
tty->flip.count = 0;
1974
restore_flags(flags);
1976
tty->ldisc.receive_buf(tty, cp, fp, count);
1980
* Routine which returns the baud rate of the tty
1982
* Note that the baud_table needs to be kept in sync with the
1983
* include/asm/termbits.h file.
1985
static int baud_table[] = {
1986
0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
1987
9600, 19200, 38400, 57600, 115200, 230400, 460800,
1989
76800, 153600, 307200, 614400, 921600
1991
500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
1992
2500000, 3000000, 3500000, 4000000
1996
static int n_baud_table = sizeof(baud_table)/sizeof(int);
1998
int tty_get_baud_rate(struct tty_struct *tty)
2000
unsigned int cflag, i;
2002
cflag = tty->termios->c_cflag;
2007
if (i < 1 || i+15 >= n_baud_table)
2008
tty->termios->c_cflag &= ~CBAUDEX;
2012
if (i==15 && tty->alt_speed) {
2014
printk(KERN_WARNING "Use of setserial/setrocket to "
2015
"set SPD_* flags is deprecated\n");
2018
return(tty->alt_speed);
2021
return baud_table[i];
2024
void tty_flip_buffer_push(struct tty_struct *tty)
2026
if (tty->low_latency)
2027
flush_to_ldisc((void *) tty);
2029
queue_task(&tty->flip.tqueue, &tq_timer);
2033
* This subroutine initializes a tty structure.
2035
static void initialize_tty_struct(struct tty_struct *tty)
2037
memset(tty, 0, sizeof(struct tty_struct));
2038
tty->magic = TTY_MAGIC;
2039
tty->ldisc = ldiscs[N_TTY];
2041
tty->flip.char_buf_ptr = tty->flip.char_buf;
2042
tty->flip.flag_buf_ptr = tty->flip.flag_buf;
2043
tty->flip.tqueue.routine = flush_to_ldisc;
2044
tty->flip.tqueue.data = tty;
2045
init_MUTEX(&tty->flip.pty_sem);
2046
init_waitqueue_head(&tty->write_wait);
2047
init_waitqueue_head(&tty->read_wait);
2048
tty->tq_hangup.routine = do_tty_hangup;
2049
tty->tq_hangup.data = tty;
2050
sema_init(&tty->atomic_read, 1);
2051
sema_init(&tty->atomic_write, 1);
2052
spin_lock_init(&tty->read_lock);
2053
INIT_LIST_HEAD(&tty->tty_files);
2054
INIT_TQUEUE(&tty->SAK_tq, 0, 0);
2058
* The default put_char routine if the driver did not define one.
2060
void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
2062
tty->driver.write(tty, 0, &ch, 1);
2066
* Register a tty device described by <driver>, with minor number <minor>.
2068
void tty_register_devfs (struct tty_driver *driver, unsigned int flags, unsigned minor)
2070
#ifdef CONFIG_DEVFS_FS
2071
umode_t mode = S_IFCHR | S_IRUSR | S_IWUSR;
2072
kdev_t device = MKDEV (driver->major, minor);
2073
int idx = minor - driver->minor_start;
2079
mode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2082
if (driver->major == PTY_MASTER_MAJOR)
2083
mode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2086
if ( (minor < driver->minor_start) ||
2087
(minor >= driver->minor_start + driver->num) ) {
2088
printk(KERN_ERR "Attempt to register invalid minor number "
2089
"with devfs (%d:%d).\n", (int)driver->major,(int)minor);
2092
# ifdef CONFIG_UNIX98_PTYS
2093
if ( (driver->major >= UNIX98_PTY_SLAVE_MAJOR) &&
2094
(driver->major < UNIX98_PTY_SLAVE_MAJOR + UNIX98_NR_MAJORS) )
2095
flags |= DEVFS_FL_CURRENT_OWNER;
2097
sprintf(buf, driver->name, idx + driver->name_base);
2098
devfs_register (NULL, buf, flags | DEVFS_FL_DEFAULT,
2099
driver->major, minor, mode, &tty_fops, NULL);
2100
#endif /* CONFIG_DEVFS_FS */
2103
void tty_unregister_devfs (struct tty_driver *driver, unsigned minor)
2105
#ifdef CONFIG_DEVFS_FS
2107
int idx = minor - driver->minor_start;
2110
sprintf(buf, driver->name, idx + driver->name_base);
2111
handle = devfs_find_handle (NULL, buf, driver->major, minor,
2112
DEVFS_SPECIAL_CHR, 0);
2113
devfs_unregister (handle);
2114
#endif /* CONFIG_DEVFS_FS */
2117
EXPORT_SYMBOL(tty_register_devfs);
2118
EXPORT_SYMBOL(tty_unregister_devfs);
2121
* Called by a tty driver to register itself.
2123
int tty_register_driver(struct tty_driver *driver)
2128
if (driver->flags & TTY_DRIVER_INSTALLED)
2131
error = devfs_register_chrdev(driver->major, driver->name, &tty_fops);
2134
else if(driver->major == 0)
2135
driver->major = error;
2137
if (!driver->put_char)
2138
driver->put_char = tty_default_put_char;
2141
driver->next = tty_drivers;
2142
if (tty_drivers) tty_drivers->prev = driver;
2143
tty_drivers = driver;
2145
if ( !(driver->flags & TTY_DRIVER_NO_DEVFS) ) {
2146
for(i = 0; i < driver->num; i++)
2147
tty_register_devfs(driver, 0, driver->minor_start + i);
2149
proc_tty_register_driver(driver);
2154
* Called by a tty driver to unregister itself.
2156
int tty_unregister_driver(struct tty_driver *driver)
2159
struct tty_driver *p;
2162
const char *othername = NULL;
2164
if (*driver->refcount)
2167
for (p = tty_drivers; p; p = p->next) {
2170
else if (p->major == driver->major)
2171
othername = p->name;
2177
if (othername == NULL) {
2178
retval = devfs_unregister_chrdev(driver->major, driver->name);
2182
devfs_register_chrdev(driver->major, othername, &tty_fops);
2185
driver->prev->next = driver->next;
2187
tty_drivers = driver->next;
2190
driver->next->prev = driver->prev;
2193
* Free the termios and termios_locked structures because
2194
* we don't want to get memory leaks when modular tty
2195
* drivers are removed from the kernel.
2197
for (i = 0; i < driver->num; i++) {
2198
tp = driver->termios[i];
2200
driver->termios[i] = NULL;
2203
tp = driver->termios_locked[i];
2205
driver->termios_locked[i] = NULL;
2208
tty_unregister_devfs(driver, driver->minor_start + i);
2210
proc_tty_unregister_driver(driver);
2216
* Initialize the console device. This is called *early*, so
2217
* we can't necessarily depend on lots of kernel help here.
2218
* Just do some early initializations, and do the complex setup
2221
void __init console_init(void)
2223
/* Setup the default TTY line discipline. */
2224
memset(ldiscs, 0, sizeof(ldiscs));
2225
(void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
2228
* Set up the standard termios. Individual tty drivers may
2229
* deviate from this; this is used as a template.
2231
memset(&tty_std_termios, 0, sizeof(struct termios));
2232
memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
2233
tty_std_termios.c_iflag = ICRNL | IXON;
2234
tty_std_termios.c_oflag = OPOST | ONLCR;
2235
tty_std_termios.c_cflag = B38400 | CS8 | CREAD | HUPCL;
2236
tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
2237
ECHOCTL | ECHOKE | IEXTEN;
2240
* set up the console device so that later boot sequences can
2241
* inform about problems etc..
2243
#ifdef CONFIG_EARLY_PRINTK
2244
disable_early_printk();
2247
#ifdef CONFIG_XEN_CONSOLE
2254
#ifdef CONFIG_AU1X00_SERIAL_CONSOLE
2255
au1x00_serial_console_init();
2257
#ifdef CONFIG_SERIAL_CONSOLE
2258
#if (defined(CONFIG_8xx) || defined(CONFIG_8260))
2260
#elif defined(CONFIG_MAC_SERIAL) && defined(CONFIG_SERIAL)
2261
if (_machine == _MACH_Pmac)
2262
mac_scc_console_init();
2264
serial_console_init();
2265
#elif defined(CONFIG_MAC_SERIAL)
2266
mac_scc_console_init();
2267
#elif defined(CONFIG_PARISC)
2269
#elif defined(CONFIG_SERIAL)
2270
serial_console_init();
2271
#endif /* CONFIG_8xx */
2272
#if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2273
vme_scc_console_init();
2275
#if defined(CONFIG_SERIAL167)
2276
serial167_console_init();
2278
#if defined(CONFIG_SH_SCI)
2282
#ifdef CONFIG_SERIAL_DEC_CONSOLE
2283
dec_serial_console_init();
2285
#ifdef CONFIG_TN3270_CONSOLE
2288
#ifdef CONFIG_TN3215
2294
#ifdef CONFIG_STDIO_CONSOLE
2295
stdio_console_init();
2297
#ifdef CONFIG_SERIAL_21285_CONSOLE
2298
rs285_console_init();
2300
#ifdef CONFIG_SERIAL_SA1100_CONSOLE
2301
sa1100_rs_console_init();
2303
#ifdef CONFIG_ARC_CONSOLE
2306
#ifdef CONFIG_SERIAL_AMBA_CONSOLE
2307
ambauart_console_init();
2309
#ifdef CONFIG_SERIAL_TX3912_CONSOLE
2310
tx3912_console_init();
2312
#ifdef CONFIG_TXX927_SERIAL_CONSOLE
2313
txx927_console_init();
2315
#ifdef CONFIG_SERIAL_TXX9_CONSOLE
2316
txx9_serial_console_init();
2318
#ifdef CONFIG_SIBYTE_SB1250_DUART_CONSOLE
2319
sb1250_serial_console_init();
2321
#ifdef CONFIG_IP22_SERIAL
2322
sgi_serial_console_init();
2326
static struct tty_driver dev_tty_driver, dev_syscons_driver;
2327
#ifdef CONFIG_UNIX98_PTYS
2328
static struct tty_driver dev_ptmx_driver;
2330
#ifdef CONFIG_HVC_CONSOLE
2334
static struct tty_driver dev_console_driver;
2338
* Ok, now we can initialize the rest of the tty devices and can count
2339
* on memory allocations, interrupts etc..
2341
void __init tty_init(void)
2344
* dev_tty_driver and dev_console_driver are actually magic
2345
* devices which get redirected at open time. Nevertheless,
2346
* we register them so that register_chrdev is called
2349
memset(&dev_tty_driver, 0, sizeof(struct tty_driver));
2350
dev_tty_driver.magic = TTY_DRIVER_MAGIC;
2351
dev_tty_driver.driver_name = "/dev/tty";
2352
dev_tty_driver.name = dev_tty_driver.driver_name + 5;
2353
dev_tty_driver.name_base = 0;
2354
dev_tty_driver.major = TTYAUX_MAJOR;
2355
dev_tty_driver.minor_start = 0;
2356
dev_tty_driver.num = 1;
2357
dev_tty_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2358
dev_tty_driver.subtype = SYSTEM_TYPE_TTY;
2360
if (tty_register_driver(&dev_tty_driver))
2361
panic("Couldn't register /dev/tty driver\n");
2363
dev_syscons_driver = dev_tty_driver;
2364
dev_syscons_driver.driver_name = "/dev/console";
2365
dev_syscons_driver.name = dev_syscons_driver.driver_name + 5;
2366
dev_syscons_driver.major = TTYAUX_MAJOR;
2367
dev_syscons_driver.minor_start = 1;
2368
dev_syscons_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2369
dev_syscons_driver.subtype = SYSTEM_TYPE_SYSCONS;
2371
if (tty_register_driver(&dev_syscons_driver))
2372
panic("Couldn't register /dev/console driver\n");
2374
/* console calls tty_register_driver() before kmalloc() works.
2375
* Thus, we can't devfs_register() then. Do so now, instead.
2381
#ifdef CONFIG_UNIX98_PTYS
2382
dev_ptmx_driver = dev_tty_driver;
2383
dev_ptmx_driver.driver_name = "/dev/ptmx";
2384
dev_ptmx_driver.name = dev_ptmx_driver.driver_name + 5;
2385
dev_ptmx_driver.major= MAJOR(PTMX_DEV);
2386
dev_ptmx_driver.minor_start = MINOR(PTMX_DEV);
2387
dev_ptmx_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2388
dev_ptmx_driver.subtype = SYSTEM_TYPE_SYSPTMX;
2390
if (tty_register_driver(&dev_ptmx_driver))
2391
panic("Couldn't register /dev/ptmx driver\n");
2395
dev_console_driver = dev_tty_driver;
2396
dev_console_driver.driver_name = "/dev/vc/0";
2397
dev_console_driver.name = dev_console_driver.driver_name + 5;
2398
dev_console_driver.major = TTY_MAJOR;
2399
dev_console_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2400
dev_console_driver.subtype = SYSTEM_TYPE_CONSOLE;
2402
if (tty_register_driver(&dev_console_driver))
2403
panic("Couldn't register /dev/tty0 driver\n");
2408
#ifdef CONFIG_SGI_L1_SERIAL_CONSOLE
2409
if (ia64_platform_is("sn2")) {
2410
sn_sal_serial_console_init();
2411
return; /* only one console right now for SN2 */
2414
#ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2417
#if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2420
#ifdef CONFIG_SERIAL_TX3912
2423
#ifdef CONFIG_ROCKETPORT
2426
#ifdef CONFIG_SERIAL167
2429
#ifdef CONFIG_CYCLADES
2432
#ifdef CONFIG_STALLION
2435
#ifdef CONFIG_ISTALLION
2441
#ifdef CONFIG_DIGIEPCA
2444
#ifdef CONFIG_SPECIALIX
2447
#if (defined(CONFIG_8xx) || defined(CONFIG_8260))
2449
#endif /* CONFIG_8xx */
2451
#ifdef CONFIG_MOXA_SMARTIO
2454
#ifdef CONFIG_MOXA_INTELLIO
2460
#ifdef CONFIG_TN3270
2463
#ifdef CONFIG_TN3215