Import 2.1.118
[davej-history.git] / drivers / char / tty_io.c
blob091cd26dc0c99127097655bdb7ff6dcac080f13f
1 /*
2 * linux/drivers/char/tty_io.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
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...
30 * -- TYT, 1/31/92
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>
75 #include <linux/kd.h>
76 #include <linux/mm.h>
77 #include <linux/string.h>
78 #include <linux/malloc.h>
79 #include <linux/poll.h>
80 #ifdef CONFIG_PROC_FS
81 #include <linux/proc_fs.h>
82 #endif
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>
94 #ifdef CONFIG_KMOD
95 #include <linux/kmod.h>
96 #endif
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 */
114 #endif
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);
132 #ifdef CONFIG_8xx
133 extern long console_8xx_init(void);
134 extern int rs_8xx_init(void);
135 #endif /* CONFIG_8xx */
137 #ifndef MIN
138 #define MIN(a,b) ((a) < (b) ? (a) : (b))
139 #endif
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)
149 if (tty)
150 sprintf(buf, "%s%d", tty->driver.name, TTY_NUMBER(tty));
151 else
152 strcpy(buf, "NULL tty");
153 return buf;
156 inline int tty_paranoia_check(struct tty_struct *tty, kdev_t device,
157 const char *routine)
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";
165 if (!tty) {
166 printk(badtty, kdevname(device), routine);
167 return 1;
169 if (tty->magic != TTY_MAGIC) {
170 printk(badmagic, kdevname(device), routine);
171 return 1;
173 #endif
174 return 0;
177 static int check_tty_count(struct tty_struct *tty, const char *routine)
179 #ifdef CHECK_TTY_COUNT
180 struct file *f;
181 int count = 0;
183 for(f = inuse_filps; f; f = f->f_next) {
184 if(f->private_data == tty)
185 count++;
187 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
188 tty->driver.subtype == PTY_TYPE_SLAVE &&
189 tty->link && tty->link->count)
190 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);
194 return count;
196 #endif
197 return 0;
200 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
202 if (disc < N_TTY || disc >= NR_LDISCS)
203 return -EINVAL;
205 if (new_ldisc) {
206 ldiscs[disc] = *new_ldisc;
207 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
208 ldiscs[disc].num = disc;
209 } else
210 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
212 return 0;
215 /* Set the discipline of a tty line. */
216 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
218 int retval = 0;
219 struct tty_ldisc o_ldisc;
220 char buf[64];
222 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
223 return -EINVAL;
224 #ifdef CONFIG_KMOD
225 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
226 /* Cyrus Durgin <cider@speakeasy.org> */
227 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED)) {
228 char modname [20];
229 sprintf(modname, "tty-ldisc-%d", ldisc);
230 request_module (modname);
232 #endif
233 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
234 return -EINVAL;
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;
249 if (tty->ldisc.open)
250 retval = (tty->ldisc.open)(tty);
251 if (retval < 0) {
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);
260 if (r < 0)
261 panic("Couldn't open N_TTY ldisc for "
262 "%s --- error %d.",
263 tty_name(tty, buf), r);
267 if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
268 tty->driver.set_ldisc(tty);
269 return retval;
273 * This routine returns a tty driver structure, given a device number
275 struct tty_driver *get_tty_driver(kdev_t device)
277 int major, minor;
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)
285 continue;
286 if (minor < p->minor_start)
287 continue;
288 if (minor >= p->minor_start + p->num)
289 continue;
290 return p;
292 return NULL;
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)
303 return 0;
304 if (tty->pgrp <= 0) {
305 printk("tty_check_change: tty->pgrp <= 0!\n");
306 return 0;
308 if (current->pgrp == tty->pgrp)
309 return 0;
310 if (is_ignored(SIGTTOU))
311 return 0;
312 if (is_orphaned_pgrp(current->pgrp))
313 return -EIO;
314 (void) kill_pg(current->pgrp,SIGTTOU,1);
315 return -ERESTARTSYS;
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)
323 return -ESPIPE;
324 return 0;
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)
332 return -ESPIPE;
333 return -EIO;
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)
349 return -ESPIPE;
352 static struct file_operations tty_fops = {
353 tty_lseek,
354 tty_read,
355 tty_write,
356 NULL, /* tty_readdir */
357 tty_poll,
358 tty_ioctl,
359 NULL, /* tty_mmap */
360 tty_open,
361 NULL, /* flush */
362 tty_release,
363 NULL, /* tty_fsync */
364 tty_fasync
367 static struct file_operations hung_up_tty_fops = {
368 tty_lseek,
369 hung_up_tty_read,
370 hung_up_tty_write,
371 NULL, /* hung_up_tty_readdir */
372 hung_up_tty_poll,
373 hung_up_tty_ioctl,
374 NULL, /* hung_up_tty_mmap */
375 NULL, /* hung_up_tty_open */
376 NULL, /* flush */
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
387 * we're doing..
389 void do_tty_hangup(void *data)
391 struct tty_struct *tty = (struct tty_struct *) data;
392 struct file * filp;
393 struct task_struct *p;
395 if (!tty)
396 return;
398 /* inuse_filps is protected by the single kernel lock */
399 lock_kernel();
401 check_tty_count(tty, "do_tty_hangup");
402 for (filp = inuse_filps; filp; filp = filp->f_next) {
403 if (filp->private_data != tty)
404 continue;
405 if (!filp->f_dentry)
406 continue;
407 if (!filp->f_dentry->d_inode)
408 continue;
409 if (filp->f_dentry->d_inode->i_rdev == CONSOLE_DEV ||
410 filp->f_dentry->d_inode->i_rdev == SYSCONS_DEV)
411 continue;
412 if (filp->f_op != &tty_fops)
413 continue;
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.. */
420 unsigned long flags;
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
438 * N_TTY.
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);
449 if (i < 0)
450 printk("do_tty_hangup: N_TTY open: error %d\n",
451 -i);
455 read_lock(&tasklist_lock);
456 for_each_task(p) {
457 if ((tty->session > 0) && (p->session == tty->session) &&
458 p->leader) {
459 send_sig(SIGHUP,p,1);
460 send_sig(SIGCONT,p,1);
461 if (tty->pgrp > 0)
462 p->tty_old_pgrp = tty->pgrp;
464 if (p->tty == tty)
465 p->tty = NULL;
467 read_unlock(&tasklist_lock);
469 tty->flags = 0;
470 tty->session = 0;
471 tty->pgrp = -1;
472 tty->ctrl_status = 0;
473 if (tty->driver.hangup)
474 (tty->driver.hangup)(tty);
475 unlock_kernel();
478 void tty_hangup(struct tty_struct * tty)
480 #ifdef TTY_DEBUG_HANGUP
481 char buf[64];
483 printk("%s hangup...\n", tty_name(tty, buf));
484 #endif
485 queue_task(&tty->tq_hangup, &tq_scheduler);
488 void tty_vhangup(struct tty_struct * tty)
490 #ifdef TTY_DEBUG_HANGUP
491 char buf[64];
493 printk("%s vhangup...\n", tty_name(tty, buf));
494 #endif
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
511 * session group.
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;
520 int tty_pgrp = -1;
522 if (tty) {
523 tty_pgrp = tty->pgrp;
524 if (on_exit && tty->driver.type != TTY_DRIVER_TYPE_PTY)
525 tty_vhangup(tty);
526 } else {
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);
531 return;
533 if (tty_pgrp > 0) {
534 kill_pg(tty_pgrp, SIGHUP, on_exit);
535 if (!on_exit)
536 kill_pg(tty_pgrp, SIGCONT, on_exit);
539 current->tty_old_pgrp = 0;
540 tty->session = 0;
541 tty->pgrp = -1;
543 read_lock(&tasklist_lock);
544 for_each_task(p)
545 if (p->session == current->session)
546 p->tty = NULL;
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)
558 if (tty->stopped)
559 return;
560 tty->stopped = 1;
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)
573 return;
574 tty->stopped = 0;
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,
589 loff_t *ppos)
591 int i;
592 struct tty_struct * tty;
593 struct inode *inode;
595 /* Can't seek (pread) on ttys. */
596 if (ppos != &file->f_pos)
597 return -ESPIPE;
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"))
602 return -EIO;
603 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
604 return -EIO;
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. */
610 #if 0
611 if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
612 (tty->pgrp > 0) &&
613 (current->tty == tty) &&
614 (tty->pgrp != current->pgrp))
615 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
616 return -EIO;
617 else {
618 (void) kill_pg(current->pgrp, SIGTTIN, 1);
619 return -ERESTARTSYS;
621 #endif
622 if (tty->ldisc.read)
623 i = (tty->ldisc.read)(tty,file,buf,count);
624 else
625 i = -EIO;
626 if (i > 0)
627 inode->i_atime = CURRENT_TIME;
628 return i;
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,
638 struct file *file,
639 const unsigned char *buf,
640 size_t count)
642 ssize_t ret = 0, written = 0;
644 for (;;) {
645 unsigned long size = PAGE_SIZE*2;
646 if (size > count)
647 size = count;
648 ret = write(tty, file, buf, size);
649 if (ret <= 0)
650 break;
651 written += ret;
652 buf += ret;
653 count -= ret;
654 if (!count)
655 break;
656 ret = -ERESTARTSYS;
657 if (signal_pending(current))
658 break;
659 if (current->need_resched)
660 schedule();
662 if (written) {
663 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
664 ret = written;
666 return ret;
670 static ssize_t tty_write(struct file * file, const char * buf, size_t count,
671 loff_t *ppos)
673 int is_console;
674 struct tty_struct * tty;
675 struct inode *inode;
677 /* Can't seek (pwrite) on ttys. */
678 if (ppos != &file->f_pos)
679 return -ESPIPE;
682 * For now, we redirect writes from /dev/console as
683 * well as /dev/tty0.
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)
690 tty = redirect;
691 else
692 tty = (struct tty_struct *)file->private_data;
693 if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
694 return -EIO;
695 if (!tty || !tty->driver.write || (test_bit(TTY_IO_ERROR, &tty->flags)))
696 return -EIO;
697 #if 0
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))
701 return -EIO;
702 if (!is_ignored(SIGTTOU)) {
703 (void) kill_pg(current->pgrp, SIGTTOU, 1);
704 return -ERESTARTSYS;
707 #endif
708 if (!tty->ldisc.write)
709 return -EIO;
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)
719 down(&tty_sem);
722 static void up_tty_sem(int index)
724 up(&tty_sem);
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;
741 int retval=0;
742 int idx;
744 driver = get_tty_driver(device);
745 if (!driver)
746 return -ENODEV;
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.
754 down_tty_sem(idx);
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.)
768 o_tty = NULL;
769 tp = o_tp = NULL;
770 ltp = o_ltp = NULL;
772 tty = (struct tty_struct*) get_free_page(GFP_KERNEL);
773 if(!tty)
774 goto fail_no_mem;
775 initialize_tty_struct(tty);
776 tty->device = device;
777 tty->driver = *driver;
779 tp_loc = &driver->termios[idx];
780 if (!*tp_loc) {
781 tp = (struct termios *) kmalloc(sizeof(struct termios),
782 GFP_KERNEL);
783 if (!tp)
784 goto free_mem_out;
785 *tp = driver->init_termios;
788 ltp_loc = &driver->termios_locked[idx];
789 if (!*ltp_loc) {
790 ltp = (struct termios *) kmalloc(sizeof(struct termios),
791 GFP_KERNEL);
792 if (!ltp)
793 goto free_mem_out;
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);
799 if (!o_tty)
800 goto free_mem_out;
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];
807 if (!*o_tp_loc) {
808 o_tp = (struct termios *)
809 kmalloc(sizeof(struct termios), GFP_KERNEL);
810 if (!o_tp)
811 goto free_mem_out;
812 *o_tp = driver->other->init_termios;
815 o_ltp_loc = &driver->other->termios_locked[idx];
816 if (!*o_ltp_loc) {
817 o_ltp = (struct termios *)
818 kmalloc(sizeof(struct termios), GFP_KERNEL);
819 if (!o_ltp)
820 goto free_mem_out;
821 memset(o_ltp, 0, sizeof(struct termios));
825 * Everything allocated ... set up the o_tty structure.
827 driver->other->table[idx] = o_tty;
828 if (!*o_tp_loc)
829 *o_tp_loc = o_tp;
830 if (!*o_ltp_loc)
831 *o_ltp_loc = o_ltp;
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)
836 o_tty->count++;
838 /* Establish the links in both directions */
839 tty->link = o_tty;
840 o_tty->link = tty;
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;
849 if (!*tp_loc)
850 *tp_loc = tp;
851 if (!*ltp_loc)
852 *ltp_loc = ltp;
853 tty->termios = *tp_loc;
854 tty->termios_locked = *ltp_loc;
855 (*driver->refcount)++;
856 tty->count++;
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);
865 if (retval)
866 goto release_mem_out;
868 if (o_tty && o_tty->ldisc.open) {
869 retval = (o_tty->ldisc.open)(o_tty);
870 if (retval) {
871 if (tty->ldisc.close)
872 (tty->ldisc.close)(tty);
873 goto release_mem_out;
876 goto success;
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.
884 fast_track:
885 if (test_bit(TTY_CLOSING, &tty->flags)) {
886 retval = -EIO;
887 goto end_init;
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.
895 if (tty->count) {
896 retval = -EIO;
897 goto end_init;
899 tty->link->count++;
901 tty->count++;
902 tty->driver = *driver; /* N.B. why do this every time?? */
904 success:
905 *ret_tty = tty;
907 /* All paths come through here to release the semaphore */
908 end_init:
909 up_tty_sem(idx);
910 return retval;
912 /* Release locally allocated memory ... nothing placed in slots */
913 free_mem_out:
914 if (o_tp)
915 kfree_s(o_tp, sizeof(struct termios));
916 if (o_tty)
917 free_page((unsigned long) o_tty);
918 if (ltp)
919 kfree_s(ltp, sizeof(struct termios));
920 if (tp)
921 kfree_s(tp, sizeof(struct termios));
922 free_page((unsigned long) tty);
924 fail_no_mem:
925 retval = -ENOMEM;
926 goto end_init;
928 /* call the tty release_mem routine to clean out this slot */
929 release_mem_out:
930 printk("init_dev: ldisc open failed, clearing slot %d\n", idx);
931 release_mem(tty, idx);
932 goto end_init;
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;
942 struct termios *tp;
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));
951 o_tty->magic = 0;
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));
962 tty->magic = 0;
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;
979 int idx;
980 char buf[64];
982 tty = (struct tty_struct *)filp->private_data;
983 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "release_dev"))
984 return;
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);
993 o_tty = tty->link;
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));
999 return;
1001 if (tty != tty->driver.table[idx]) {
1002 printk("release_dev: driver.table[%d] not tty for (%s)\n",
1003 idx, kdevname(tty->device));
1004 return;
1006 if (tty->termios != tty->driver.termios[idx]) {
1007 printk("release_dev: driver.termios[%d] not termios "
1008 "for (%s)\n",
1009 idx, kdevname(tty->device));
1010 return;
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));
1016 return;
1018 #endif
1020 #ifdef TTY_DEBUG_HANGUP
1021 printk("release_dev of %s (tty count=%d)...", tty_name(tty, buf),
1022 tty->count);
1023 #endif
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 ("
1029 "%s)\n",
1030 idx, kdevname(tty->device));
1031 return;
1033 if (o_tty->termios != tty->driver.other->termios[idx]) {
1034 printk("release_dev: other->termios[%d] not o_termios "
1035 "for (%s)\n",
1036 idx, kdevname(tty->device));
1037 return;
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));
1044 return;
1046 if (o_tty->link != tty) {
1047 printk("release_dev: bad pty pointers\n");
1048 return;
1051 #endif
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
1061 * structure.
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,
1067 * so we do it now.
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.
1073 while (1) {
1074 tty_closing = tty->count <= 1;
1075 o_tty_closing = o_tty &&
1076 (o_tty->count <= (pty_master ? 1 : 0));
1077 do_sleep = 0;
1079 if (tty_closing) {
1080 if (waitqueue_active(&tty->read_wait)) {
1081 wake_up(&tty->read_wait);
1082 do_sleep++;
1084 if (waitqueue_active(&tty->write_wait)) {
1085 wake_up(&tty->write_wait);
1086 do_sleep++;
1089 if (o_tty_closing) {
1090 if (waitqueue_active(&o_tty->read_wait)) {
1091 wake_up(&o_tty->read_wait);
1092 do_sleep++;
1094 if (waitqueue_active(&o_tty->write_wait)) {
1095 wake_up(&o_tty->write_wait);
1096 do_sleep++;
1099 if (!do_sleep)
1100 break;
1102 printk("release_dev: %s: read/write wait queue active!\n",
1103 tty_name(tty, buf));
1104 schedule();
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.
1113 if (pty_master) {
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));
1117 o_tty->count = 0;
1120 if (--tty->count < 0) {
1121 printk("release_dev: bad tty->count (%d) for %s\n",
1122 tty->count, tty_name(tty, buf));
1123 tty->count = 0;
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.
1133 if(tty_closing)
1134 set_bit(TTY_CLOSING, &tty->flags);
1135 if(o_tty_closing)
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);
1147 for_each_task(p) {
1148 if (p->tty == tty || (o_tty && p->tty == o_tty))
1149 p->tty = NULL;
1151 read_unlock(&tasklist_lock);
1153 if (redirect == tty || (o_tty && redirect == o_tty))
1154 redirect = NULL;
1157 /* check whether both sides are closing ... */
1158 if (!tty_closing || (o_tty && !o_tty_closing))
1159 return;
1160 filp->private_data = 0;
1162 #ifdef TTY_DEBUG_HANGUP
1163 printk("freeing tty structure...");
1164 #endif
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;
1174 if (o_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;
1208 int noctty, retval;
1209 kdev_t device;
1210 unsigned short saved_flags;
1211 char buf[64];
1213 saved_flags = filp->f_flags;
1214 retry_open:
1215 noctty = filp->f_flags & O_NOCTTY;
1216 device = inode->i_rdev;
1217 if (device == TTY_DEV) {
1218 if (!current->tty)
1219 return -ENXIO;
1220 device = current->tty->device;
1221 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1222 /* noctty = 1; */
1224 #ifdef CONFIG_VT
1225 if (device == CONSOLE_DEV) {
1226 extern int fg_console;
1227 device = MKDEV(TTY_MAJOR, fg_console + 1);
1228 noctty = 1;
1230 #endif
1231 if (device == SYSCONS_DEV) {
1232 struct console *c = console_drivers;
1233 while(c && !c->device)
1234 c = c->next;
1235 if (!c)
1236 return -ENODEV;
1237 device = c->device(c);
1238 noctty = 1;
1240 #ifdef CONFIG_UNIX98_PTYS
1241 if (device == PTMX_DEV) {
1242 /* find a free pty. */
1243 int major, minor;
1244 struct tty_driver *driver;
1246 /* find a device that is not in use. */
1247 retval = -1;
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 ;
1252 minor++) {
1253 device = MKDEV(driver->major, minor);
1254 if (!init_dev(device, &tty)) goto ptmx_found; /* ok! */
1257 return -EIO; /* no free ptys */
1258 ptmx_found:
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));
1262 noctty = 1;
1263 goto init_dev_done;
1265 #endif
1267 retval = init_dev(device, &tty);
1268 if (retval)
1269 return retval;
1271 /* N.B. this error exit may leave filp->f_flags with O_NONBLOCK set */
1272 init_dev_done:
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)
1277 noctty = 1;
1278 #ifdef TTY_DEBUG_HANGUP
1279 printk("opening %s...", tty_name(tty, buf));
1280 #endif
1281 if (tty->driver.open)
1282 retval = tty->driver.open(tty, filp);
1283 else
1284 retval = -ENODEV;
1285 filp->f_flags = saved_flags;
1287 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1288 retval = -EBUSY;
1290 if (retval) {
1291 #ifdef TTY_DEBUG_HANGUP
1292 printk("error %d in opening %s...", retval,
1293 tty_name(tty, buf));
1294 #endif
1296 release_dev(filp);
1297 if (retval != -ERESTARTSYS)
1298 return retval;
1299 if (signal_pending(current))
1300 return retval;
1301 schedule();
1303 * Need to reset f_op in case a hangup happened.
1305 filp->f_op = &tty_fops;
1306 goto retry_open;
1308 if (!noctty &&
1309 current->leader &&
1310 !current->tty &&
1311 tty->session == 0) {
1312 current->tty = tty;
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;
1321 if (nr_warns < 5) {
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));
1327 nr_warns++;
1330 return 0;
1333 static int tty_release(struct inode * inode, struct file * filp)
1335 release_dev(filp);
1336 return 0;
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"))
1345 return 0;
1347 if (tty->ldisc.poll)
1348 return (tty->ldisc.poll)(tty, filp, wait);
1349 return 0;
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)
1364 break;
1367 if (on) {
1368 if (fa) {
1369 fa->fa_fd = fd;
1370 return 0;
1372 fa = (struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1373 if (!fa)
1374 return -ENOMEM;
1375 fa->magic = FASYNC_MAGIC;
1376 fa->fa_file = filp;
1377 fa->fa_fd = fd;
1378 save_flags(flags);
1379 cli();
1380 fa->fa_next = *fapp;
1381 *fapp = fa;
1382 restore_flags(flags);
1383 return 1;
1385 if (!fa)
1386 return 0;
1387 save_flags(flags);
1388 cli();
1389 *fp = fa->fa_next;
1390 restore_flags(flags);
1391 kfree(fa);
1392 return 1;
1395 static int tty_fasync(int fd, struct file * filp, int on)
1397 struct tty_struct * tty;
1398 int retval;
1400 tty = (struct tty_struct *)filp->private_data;
1401 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_fasync"))
1402 return 0;
1404 retval = fasync_helper(fd, filp, on, &tty->fasync);
1405 if (retval <= 0)
1406 return retval;
1408 if (on) {
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;
1416 } else {
1417 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1418 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1420 return 0;
1423 static int tiocsti(struct tty_struct *tty, char * arg)
1425 char ch, mbz = 0;
1427 if ((current->tty != tty) && !suser())
1428 return -EPERM;
1429 if (get_user(ch, arg))
1430 return -EFAULT;
1431 tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1432 return 0;
1435 static int tiocgwinsz(struct tty_struct *tty, struct winsize * arg)
1437 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
1438 return -EFAULT;
1439 return 0;
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)))
1448 return -EFAULT;
1449 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
1450 return 0;
1451 if (tty->pgrp > 0)
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;
1457 return 0;
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) {
1464 if (!suser())
1465 return -EPERM;
1466 redirect = NULL;
1467 return 0;
1469 if (redirect)
1470 return -EBUSY;
1471 redirect = real_tty;
1472 return 0;
1476 static int fionbio(struct file *file, int *arg)
1478 int nonblock;
1480 if (get_user(nonblock, arg))
1481 return -EFAULT;
1483 if (nonblock)
1484 file->f_flags |= O_NONBLOCK;
1485 else
1486 file->f_flags &= ~O_NONBLOCK;
1487 return 0;
1490 static int tiocsctty(struct tty_struct *tty, int arg)
1492 if (current->leader &&
1493 (current->session == tty->session))
1494 return 0;
1496 * The process must be a session leader and
1497 * not have a controlling tty already.
1499 if (!current->leader || current->tty)
1500 return -EPERM;
1501 if (tty->session > 0) {
1503 * This tty is already the controlling
1504 * tty for another session group!
1506 if ((arg == 1) && suser()) {
1508 * Steal it away
1510 struct task_struct *p;
1512 read_lock(&tasklist_lock);
1513 for_each_task(p)
1514 if (p->tty == tty)
1515 p->tty = NULL;
1516 read_unlock(&tasklist_lock);
1517 } else
1518 return -EPERM;
1520 current->tty = tty;
1521 current->tty_old_pgrp = 0;
1522 tty->session = current->session;
1523 tty->pgrp = current->pgrp;
1524 return 0;
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)
1534 return -ENOTTY;
1535 return put_user(real_tty->pgrp, arg);
1538 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1540 pid_t pgrp;
1541 int retval = tty_check_change(real_tty);
1543 if (retval == -EIO)
1544 return -ENOTTY;
1545 if (retval)
1546 return retval;
1547 if (!current->tty ||
1548 (current->tty != real_tty) ||
1549 (real_tty->session != current->session))
1550 return -ENOTTY;
1551 get_user(pgrp, (pid_t *) arg);
1552 if (pgrp < 0)
1553 return -EINVAL;
1554 if (session_of_pgrp(pgrp) != current->session)
1555 return -EPERM;
1556 real_tty->pgrp = pgrp;
1557 return 0;
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)
1567 return -ENOTTY;
1568 if (real_tty->session <= 0)
1569 return -ENOTTY;
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)))
1576 return -EFAULT;
1577 return 0;
1580 static int tiocsetd(struct tty_struct *tty, int *arg)
1582 int retval, ldisc;
1584 retval = get_user(ldisc, arg);
1585 if (retval)
1586 return retval;
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))
1597 schedule();
1598 tty->driver.break_ctl(tty, 0);
1599 if (signal_pending(current))
1600 return -EINTR;
1601 return 0;
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;
1611 int retval;
1613 tty = (struct tty_struct *)file->private_data;
1614 if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1615 return -EINVAL;
1617 real_tty = tty;
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) {
1626 switch(cmd) {
1627 case TIOCSBRK:
1628 case TIOCCBRK:
1629 if (tty->driver.ioctl)
1630 return tty->driver.ioctl(tty, file, cmd, arg);
1631 return -EINVAL;
1633 /* These two ioctl's always return success; even if */
1634 /* the driver doesn't support them. */
1635 case TCSBRK:
1636 case TCSBRKP:
1637 if (!tty->driver.ioctl)
1638 return 0;
1639 retval = tty->driver.ioctl(tty, file, cmd, arg);
1640 if (retval == -ENOIOCTLCMD)
1641 retval = 0;
1642 return retval;
1647 * Factor out some common prep work
1649 switch (cmd) {
1650 case TIOCSETD:
1651 case TIOCSBRK:
1652 case TIOCCBRK:
1653 case TCSBRK:
1654 case TCSBRKP:
1655 retval = tty_check_change(tty);
1656 if (retval)
1657 return retval;
1658 if (cmd != TIOCCBRK) {
1659 tty_wait_until_sent(tty, 0);
1660 if (signal_pending(current))
1661 return -EINTR;
1663 break;
1666 switch (cmd) {
1667 case TIOCSTI:
1668 return tiocsti(tty, (char *)arg);
1669 case TIOCGWINSZ:
1670 return tiocgwinsz(tty, (struct winsize *) arg);
1671 case TIOCSWINSZ:
1672 return tiocswinsz(tty, real_tty, (struct winsize *) arg);
1673 case TIOCCONS:
1674 return tioccons(tty, real_tty);
1675 case FIONBIO:
1676 return fionbio(file, (int *) arg);
1677 case TIOCEXCL:
1678 set_bit(TTY_EXCLUSIVE, &tty->flags);
1679 return 0;
1680 case TIOCNXCL:
1681 clear_bit(TTY_EXCLUSIVE, &tty->flags);
1682 return 0;
1683 case TIOCNOTTY:
1684 if (current->tty != tty)
1685 return -ENOTTY;
1686 if (current->leader)
1687 disassociate_ctty(0);
1688 current->tty = NULL;
1689 return 0;
1690 case TIOCSCTTY:
1691 return tiocsctty(tty, arg);
1692 case TIOCGPGRP:
1693 return tiocgpgrp(tty, real_tty, (pid_t *) arg);
1694 case TIOCSPGRP:
1695 return tiocspgrp(tty, real_tty, (pid_t *) arg);
1696 case TIOCGSID:
1697 return tiocgsid(tty, real_tty, (pid_t *) arg);
1698 case TIOCGETD:
1699 return put_user(tty->ldisc.num, (int *) arg);
1700 case TIOCSETD:
1701 return tiocsetd(tty, (int *) arg);
1702 #ifdef CONFIG_VT
1703 case TIOCLINUX:
1704 return tioclinux(tty, arg);
1705 #endif
1706 case TIOCTTYGSTRUCT:
1707 return tiocttygstruct(tty, (struct tty_struct *) arg);
1710 * Break handling
1712 case TIOCSBRK: /* Turn break on, unconditionally */
1713 tty->driver.break_ctl(tty, -1);
1714 return 0;
1716 case TIOCCBRK: /* Turn break off, unconditionally */
1717 tty->driver.break_ctl(tty, 0);
1718 return 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
1723 * all by anyone?
1725 if (!arg)
1726 return send_break(tty, HZ/4);
1727 return 0;
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)
1734 return retval;
1736 if (tty->ldisc.ioctl) {
1737 int retval = (tty->ldisc.ioctl)(tty, file, cmd, arg);
1738 if (retval != -ENOIOCTLCMD)
1739 return retval;
1741 return -EINVAL;
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
1755 * to spawn.
1757 void do_SAK( struct tty_struct *tty)
1759 #ifdef TTY_SOFT_SAK
1760 tty_hangup(tty);
1761 #else
1762 struct task_struct *p;
1763 int session;
1764 int i;
1765 struct file *filp;
1767 if (!tty)
1768 return;
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);
1775 for_each_task(p) {
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);
1785 break;
1790 read_unlock(&tasklist_lock);
1791 #endif
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_;
1801 unsigned char *cp;
1802 char *fp;
1803 int count;
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;
1814 } else {
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;
1847 i = cflag & CBAUD;
1848 if (i & CBAUDEX) {
1849 i &= ~CBAUDEX;
1850 if (i < 1 || i > 4)
1851 tty->termios->c_cflag &= ~CBAUDEX;
1852 else
1853 i += 15;
1855 if (i==15 && tty->alt_speed) {
1856 if (!tty->warned) {
1857 printk("Use of setserial/setrocket to set SPD_* flags is deprecated\n");
1858 tty->warned = 1;
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);
1870 else
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];
1882 tty->pgrp = -1;
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)
1905 int error;
1907 if (driver->flags & TTY_DRIVER_INSTALLED)
1908 return 0;
1910 error = register_chrdev(driver->major, driver->name, &tty_fops);
1911 if (error < 0)
1912 return error;
1913 else if(driver->major == 0)
1914 driver->major = error;
1916 if (!driver->put_char)
1917 driver->put_char = tty_default_put_char;
1919 driver->prev = 0;
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);
1926 #endif
1927 return error;
1931 * Called by a tty driver to unregister itself.
1933 int tty_unregister_driver(struct tty_driver *driver)
1935 int retval;
1936 struct tty_driver *p;
1937 int found = 0;
1938 const char *othername = NULL;
1940 if (*driver->refcount)
1941 return -EBUSY;
1943 for (p = tty_drivers; p; p = p->next) {
1944 if (p == driver)
1945 found++;
1946 else if (p->major == driver->major)
1947 othername = p->name;
1950 if (!found)
1951 return -ENOENT;
1953 if (othername == NULL) {
1954 retval = unregister_chrdev(driver->major, driver->name);
1955 if (retval)
1956 return retval;
1957 } else
1958 register_chrdev(driver->major, othername, &tty_fops);
1960 if (driver->prev)
1961 driver->prev->next = driver->next;
1962 else
1963 tty_drivers = driver->next;
1965 if (driver->next)
1966 driver->next->prev = driver->prev;
1968 #ifdef CONFIG_PROC_FS
1969 proc_tty_unregister_driver(driver);
1970 #endif
1971 return 0;
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
1979 * later.
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..
2003 #ifdef CONFIG_VT
2004 kmem_start = con_init(kmem_start);
2005 #endif
2006 #ifdef CONFIG_SERIAL_CONSOLE
2007 kmem_start = serial_console_init(kmem_start, kmem_end);
2008 #endif
2009 return kmem_start;
2012 static struct tty_driver dev_tty_driver, dev_syscons_driver, dev_ptmx_driver;
2013 #ifdef CONFIG_VT
2014 static struct tty_driver dev_console_driver;
2015 #endif
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
2030 * appropriately.
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");
2068 #ifdef CONFIG_VT
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");
2079 kbd_init();
2080 #endif
2081 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2082 espserial_init();
2083 #endif
2084 #ifdef CONFIG_SERIAL
2085 rs_init();
2086 #endif
2087 #ifdef CONFIG_MAC_SERIAL
2088 macserial_init();
2089 #endif
2090 #ifdef CONFIG_ROCKETPORT
2091 rp_init();
2092 #endif
2093 #ifdef CONFIG_CYCLADES
2094 cy_init();
2095 #endif
2096 #ifdef CONFIG_STALLION
2097 stl_init();
2098 #endif
2099 #ifdef CONFIG_ISTALLION
2100 stli_init();
2101 #endif
2102 #ifdef CONFIG_DIGI
2103 pcxe_init();
2104 #endif
2105 #ifdef CONFIG_DIGIEPCA
2106 pc_init();
2107 #endif
2108 #ifdef CONFIG_RISCOM8
2109 riscom8_init();
2110 #endif
2111 #ifdef CONFIG_SPECIALIX
2112 specialix_init();
2113 #endif
2114 #ifdef CONFIG_8xx
2115 rs_8xx_init();
2116 #endif /* CONFIG_8xx */
2117 pty_init();
2118 #ifdef CONFIG_VT
2119 vcs_init();
2120 #endif
2121 return 0;