root/drivers/char/tty_io.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. _tty_name
  2. tty_name
  3. tty_paranoia_check
  4. check_tty_count
  5. tty_register_ldisc
  6. tty_set_ldisc
  7. get_tty_driver
  8. tty_check_change
  9. hung_up_tty_read
  10. hung_up_tty_write
  11. hung_up_tty_select
  12. hung_up_tty_ioctl
  13. tty_lseek
  14. do_tty_hangup
  15. tty_hangup
  16. tty_vhangup
  17. tty_hung_up_p
  18. disassociate_ctty
  19. vt_waitactive
  20. reset_vc
  21. complete_change_console
  22. change_console
  23. wait_for_keypress
  24. stop_tty
  25. start_tty
  26. tty_read
  27. tty_write
  28. init_dev
  29. release_dev
  30. tty_open
  31. tty_release
  32. tty_select
  33. tty_fasync
  34. do_get_ps_info
  35. tty_ioctl
  36. do_SAK
  37. flush_to_ldisc
  38. initialize_tty_struct
  39. tty_default_put_char
  40. tty_register_driver
  41. tty_unregister_driver
  42. console_init
  43. tty_init

   1 /*
   2  *  linux/drivers/char/tty_io.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   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.
  10  *
  11  * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
  12  *
  13  * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
  14  * tty_struct and tty_queue structures.  Previously there was a 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.
  18  *
  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 
  23  *
  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).
  27  *
  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
  31  *
  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.
  35  *
  36  * Rewrote canonical mode and added more termios flags.
  37  *      -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
  38  */
  39 
  40 #include <linux/types.h>
  41 #include <linux/major.h>
  42 #include <linux/errno.h>
  43 #include <linux/signal.h>
  44 #include <linux/fcntl.h>
  45 #include <linux/sched.h>
  46 #include <linux/interrupt.h>
  47 #include <linux/tty.h>
  48 #include <linux/tty_flip.h>
  49 #include <linux/timer.h>
  50 #include <linux/ctype.h>
  51 #include <linux/kd.h>
  52 #include <linux/mm.h>
  53 #include <linux/string.h>
  54 #include <linux/malloc.h>
  55 #include <linux/config.h>
  56 
  57 #include <asm/segment.h>
  58 #include <asm/system.h>
  59 #include <asm/bitops.h>
  60 
  61 #include "kbd_kern.h"
  62 #include "vt_kern.h"
  63 #include "selection.h"
  64 
  65 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
  66 #define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
  67 
  68 #undef TTY_DEBUG_HANGUP
  69 
  70 #define TTY_PARANOIA_CHECK
  71 #define CHECK_TTY_COUNT
  72 
  73 extern void do_blank_screen(int nopowersave);
  74 extern void do_unblank_screen(void);
  75 extern void set_vesa_blanking(const unsigned long arg);
  76 
  77 struct termios tty_std_termios;         /* for the benefit of tty drivers  */
  78 struct tty_driver *tty_drivers = NULL;  /* linked list of tty drivers */
  79 struct tty_ldisc ldiscs[NR_LDISCS];     /* line disc dispatch table     */
  80 
  81 /*
  82  * fg_console is the current virtual console,
  83  * last_console is the last used one
  84  * redirect is the pseudo-tty that console output
  85  * is redirected to if asked by TIOCCONS.
  86  */
  87 int fg_console = 0;
  88 int last_console = 0;
  89 struct tty_struct * redirect = NULL;
  90 struct wait_queue * keypress_wait = NULL;
  91 
  92 static void initialize_tty_struct(struct tty_struct *tty);
  93 
  94 static int tty_read(struct inode *, struct file *, char *, int);
  95 static int tty_write(struct inode *, struct file *, char *, int);
  96 static int tty_select(struct inode *, struct file *, int, select_table *);
  97 static int tty_open(struct inode *, struct file *);
  98 static void tty_release(struct inode *, struct file *);
  99 static int tty_ioctl(struct inode * inode, struct file * file,
 100                      unsigned int cmd, unsigned long arg);
 101 static int tty_fasync(struct inode * inode, struct file * filp, int on);
 102 
 103 #ifndef MIN
 104 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
 105 #endif
 106 
 107 /*
 108  * These two routines return the name of tty.  tty_name() should NOT
 109  * be used in interrupt drivers, since it's not re-entrant.  Use
 110  * _tty_name() instead.
 111  */
 112 char *_tty_name(struct tty_struct *tty, char *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 113 {
 114         if (tty)
 115                 sprintf(buf, "%s%d", tty->driver.name,
 116                         MINOR(tty->device) - tty->driver.minor_start +
 117                         tty->driver.name_base);
 118         else
 119                 strcpy(buf, "NULL tty");
 120         return buf;
 121 }
 122 
 123 char *tty_name(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125         static char buf[64];
 126 
 127         return(_tty_name(tty, buf));
 128 }
 129 
 130 inline int tty_paranoia_check(struct tty_struct *tty, dev_t device,
     /* [previous][next][first][last][top][bottom][index][help] */
 131                               const char *routine)
 132 {
 133 #ifdef TTY_PARANOIA_CHECK
 134         static const char *badmagic =
 135                 "Warning: bad magic number for tty struct (%d, %d) in %s\n";
 136         static const char *badtty =
 137                 "Warning: null TTY for (%d, %d) in %s\n";
 138 
 139         if (!tty) {
 140                 printk(badtty, MAJOR(device), MINOR(device), routine);
 141                 return 1;
 142         }
 143         if (tty->magic != TTY_MAGIC) {
 144                 printk(badmagic, MAJOR(device), MINOR(device), routine);
 145                 return 1;
 146         }
 147 #endif
 148         return 0;
 149 }
 150 
 151 static int check_tty_count(struct tty_struct *tty, const char *routine)
     /* [previous][next][first][last][top][bottom][index][help] */
 152 {
 153 #ifdef CHECK_TTY_COUNT
 154         struct file *f;
 155         int i, count = 0;
 156         
 157         for (f = first_file, i=0; i<nr_files; i++, f = f->f_next) {
 158                 if (!f->f_count)
 159                         continue;
 160                 if (f->private_data == tty) {
 161                         count++;
 162                 }
 163         }
 164         if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
 165             tty->driver.subtype == PTY_TYPE_SLAVE &&
 166             tty->link && tty->link->count)
 167                 count++;
 168         if (tty->count != count) {
 169                 printk("Warning: dev (%d, %d) tty->count(%d) != #fd's(%d) in %s\n",
 170                        MAJOR(tty->device), MINOR(tty->device), tty->count,
 171                        count, routine);
 172                 return count;
 173        }        
 174 #endif
 175         return 0;
 176 }
 177 
 178 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
     /* [previous][next][first][last][top][bottom][index][help] */
 179 {
 180         if (disc < N_TTY || disc >= NR_LDISCS)
 181                 return -EINVAL;
 182         
 183         if (new_ldisc) {
 184                 ldiscs[disc] = *new_ldisc;
 185                 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
 186                 ldiscs[disc].num = disc;
 187         } else
 188                 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
 189         
 190         return 0;
 191 }
 192 
 193 /* Set the discipline of a tty line. */
 194 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
     /* [previous][next][first][last][top][bottom][index][help] */
 195 {
 196         int     retval = 0;
 197         struct  tty_ldisc o_ldisc;
 198 
 199         if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS) ||
 200             !(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
 201                 return -EINVAL;
 202 
 203         if (tty->ldisc.num == ldisc)
 204                 return 0;       /* We are already in the desired discipline */
 205         o_ldisc = tty->ldisc;
 206 
 207         /* Shutdown the current discipline. */
 208         if (tty->ldisc.close)
 209                 (tty->ldisc.close)(tty);
 210 
 211         /* Now set up the new line discipline. */
 212         tty->ldisc = ldiscs[ldisc];
 213         tty->termios->c_line = ldisc;
 214         if (tty->ldisc.open)
 215                 retval = (tty->ldisc.open)(tty);
 216         if (retval < 0) {
 217                 tty->ldisc = o_ldisc;
 218                 tty->termios->c_line = tty->ldisc.num;
 219                 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
 220                         tty->ldisc = ldiscs[N_TTY];
 221                         tty->termios->c_line = N_TTY;
 222                         if (tty->ldisc.open) {
 223                                 int r = tty->ldisc.open(tty);
 224 
 225                                 if (r < 0)
 226                                         panic("Couldn't open N_TTY ldisc for "
 227                                               "%s --- error %d.",
 228                                               tty_name(tty), r);
 229                         }
 230                 }
 231         }
 232         if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
 233                 tty->driver.set_ldisc(tty);
 234         return retval;
 235 }
 236 
 237 /*
 238  * This routine returns a tty driver structure, given a device number
 239  */
 240 struct tty_driver *get_tty_driver(dev_t device)
     /* [previous][next][first][last][top][bottom][index][help] */
 241 {
 242         int     major, minor;
 243         struct tty_driver *p;
 244         
 245         minor = MINOR(device);
 246         major = MAJOR(device);
 247 
 248         for (p = tty_drivers; p; p = p->next) {
 249                 if (p->major != major)
 250                         continue;
 251                 if (minor < p->minor_start)
 252                         continue;
 253                 if (minor >= p->minor_start + p->num)
 254                         continue;
 255                 return p;
 256         }
 257         return NULL;
 258 }
 259 
 260 /*
 261  * If we try to write to, or set the state of, a terminal and we're
 262  * not in the foreground, send a SIGTTOU.  If the signal is blocked or
 263  * ignored, go ahead and perform the operation.  (POSIX 7.2)
 264  */
 265 int tty_check_change(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 266 {
 267         if (current->tty != tty)
 268                 return 0;
 269         if (tty->pgrp <= 0) {
 270                 printk("tty_check_change: tty->pgrp <= 0!\n");
 271                 return 0;
 272         }
 273         if (current->pgrp == tty->pgrp)
 274                 return 0;
 275         if (is_ignored(SIGTTOU))
 276                 return 0;
 277         if (is_orphaned_pgrp(current->pgrp))
 278                 return -EIO;
 279         (void) kill_pg(current->pgrp,SIGTTOU,1);
 280         return -ERESTARTSYS;
 281 }
 282 
 283 static int hung_up_tty_read(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 284 {
 285         return 0;
 286 }
 287 
 288 static int hung_up_tty_write(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 289 {
 290         return -EIO;
 291 }
 292 
 293 static int hung_up_tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 294 {
 295         return 1;
 296 }
 297 
 298 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 299                              unsigned int cmd, unsigned long arg)
 300 {
 301         return -EIO;
 302 }
 303 
 304 static int tty_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
     /* [previous][next][first][last][top][bottom][index][help] */
 305 {
 306         return -ESPIPE;
 307 }
 308 
 309 static struct file_operations tty_fops = {
 310         tty_lseek,
 311         tty_read,
 312         tty_write,
 313         NULL,           /* tty_readdir */
 314         tty_select,
 315         tty_ioctl,
 316         NULL,           /* tty_mmap */
 317         tty_open,
 318         tty_release,
 319         NULL,           /* tty_fsync */
 320         tty_fasync
 321 };
 322 
 323 static struct file_operations hung_up_tty_fops = {
 324         tty_lseek,
 325         hung_up_tty_read,
 326         hung_up_tty_write,
 327         NULL,           /* hung_up_tty_readdir */
 328         hung_up_tty_select,
 329         hung_up_tty_ioctl,
 330         NULL,           /* hung_up_tty_mmap */
 331         NULL,           /* hung_up_tty_open */
 332         tty_release,    /* hung_up_tty_release */
 333         NULL,           /* hung_up_tty_fsync  */
 334         NULL            /* hung_up_tty_fasync */
 335 };
 336 
 337 void do_tty_hangup(struct tty_struct * tty, struct file_operations *fops)
     /* [previous][next][first][last][top][bottom][index][help] */
 338 {
 339         int i;
 340         struct file * filp;
 341         struct task_struct *p;
 342 
 343         if (!tty)
 344                 return;
 345         check_tty_count(tty, "do_tty_hangup");
 346         for (filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
 347                 if (!filp->f_count)
 348                         continue;
 349                 if (filp->private_data != tty)
 350                         continue;
 351                 if (filp->f_inode && filp->f_inode->i_rdev == CONSOLE_DEV)
 352                         continue;
 353                 if (filp->f_op != &tty_fops)
 354                         continue;
 355                 tty_fasync(filp->f_inode, filp, 0);
 356                 filp->f_op = fops;
 357         }
 358         
 359         if (tty->ldisc.flush_buffer)
 360                 tty->ldisc.flush_buffer(tty);
 361         if (tty->driver.flush_buffer)
 362                 tty->driver.flush_buffer(tty);
 363         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
 364             tty->ldisc.write_wakeup)
 365                 (tty->ldisc.write_wakeup)(tty);
 366         wake_up_interruptible(&tty->write_wait);
 367         wake_up_interruptible(&tty->read_wait);
 368 
 369         /*
 370          * Shutdown the current line discipline, and reset it to
 371          * N_TTY.
 372          */
 373         if (tty->ldisc.num != ldiscs[N_TTY].num) {
 374                 if (tty->ldisc.close)
 375                         (tty->ldisc.close)(tty);
 376                 tty->ldisc = ldiscs[N_TTY];
 377                 tty->termios->c_line = N_TTY;
 378                 if (tty->ldisc.open) {
 379                         i = (tty->ldisc.open)(tty);
 380                         if (i < 0)
 381                                 printk("do_tty_hangup: N_TTY open: error %d\n",
 382                                        -i);
 383                 }
 384         }
 385         
 386         for_each_task(p) {
 387                 if ((tty->session > 0) && (p->session == tty->session) &&
 388                     p->leader) {
 389                         send_sig(SIGHUP,p,1);
 390                         send_sig(SIGCONT,p,1);
 391                         if (tty->pgrp > 0)
 392                                 p->tty_old_pgrp = tty->pgrp;
 393                 }
 394                 if (p->tty == tty)
 395                         p->tty = NULL;
 396         }
 397         tty->flags = 0;
 398         tty->session = 0;
 399         tty->pgrp = -1;
 400         tty->ctrl_status = 0;
 401         if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
 402                 *tty->termios = tty->driver.init_termios;
 403         if (tty->driver.hangup)
 404                 (tty->driver.hangup)(tty);
 405 }
 406 
 407 void tty_hangup(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 408 {
 409 #ifdef TTY_DEBUG_HANGUP
 410         printk("%s hangup...\n", tty_name(tty));
 411 #endif
 412         do_tty_hangup(tty, &hung_up_tty_fops);
 413 }
 414 
 415 void tty_vhangup(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 416 {
 417 #ifdef TTY_DEBUG_HANGUP
 418         printk("%s vhangup...\n", tty_name(tty));
 419 #endif
 420         do_tty_hangup(tty, &hung_up_tty_fops);
 421 }
 422 
 423 int tty_hung_up_p(struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 424 {
 425         return (filp->f_op == &hung_up_tty_fops);
 426 }
 427 
 428 /*
 429  * This function is typically called only by the session leader, when
 430  * it wants to disassociate itself from its controlling tty.
 431  *
 432  * It performs the following functions:
 433  *      (1)  Sends a SIGHUP and SIGCONT to the foreground process group
 434  *      (2)  Clears the tty from being controlling the session
 435  *      (3)  Clears the controlling tty for all processes in the
 436  *              session group.
 437  */
 438 void disassociate_ctty(int priv)
     /* [previous][next][first][last][top][bottom][index][help] */
 439 {
 440         struct tty_struct *tty = current->tty;
 441         struct task_struct *p;
 442 
 443         if (!tty) {
 444                 if (current->tty_old_pgrp) {
 445                         kill_pg(current->tty_old_pgrp, SIGHUP, priv);
 446                         kill_pg(current->tty_old_pgrp, SIGCONT, priv);
 447                 }
 448                 return;
 449         }
 450         if (tty->pgrp > 0) {
 451                 kill_pg(tty->pgrp, SIGHUP, priv);
 452                 kill_pg(tty->pgrp, SIGCONT, priv);
 453         }
 454 
 455         current->tty_old_pgrp = 0;
 456         tty->session = 0;
 457         tty->pgrp = -1;
 458 
 459         for_each_task(p)
 460                 if (p->session == current->session)
 461                         p->tty = NULL;
 462 }
 463 
 464 /*
 465  * Sometimes we want to wait until a particular VT has been activated. We
 466  * do it in a very simple manner. Everybody waits on a single queue and
 467  * get woken up at once. Those that are satisfied go on with their business,
 468  * while those not ready go back to sleep. Seems overkill to add a wait
 469  * to each vt just for this - usually this does nothing!
 470  */
 471 static struct wait_queue *vt_activate_queue = NULL;
 472 
 473 /*
 474  * Sleeps until a vt is activated, or the task is interrupted. Returns
 475  * 0 if activation, -1 if interrupted.
 476  */
 477 int vt_waitactive(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 478 {
 479         interruptible_sleep_on(&vt_activate_queue);
 480         return (current->signal & ~current->blocked) ? -1 : 0;
 481 }
 482 
 483 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
 484 
 485 void reset_vc(unsigned int new_console)
     /* [previous][next][first][last][top][bottom][index][help] */
 486 {
 487         vt_cons[new_console]->vc_mode = KD_TEXT;
 488         kbd_table[new_console].kbdmode = VC_XLATE;
 489         vt_cons[new_console]->vt_mode.mode = VT_AUTO;
 490         vt_cons[new_console]->vt_mode.waitv = 0;
 491         vt_cons[new_console]->vt_mode.relsig = 0;
 492         vt_cons[new_console]->vt_mode.acqsig = 0;
 493         vt_cons[new_console]->vt_mode.frsig = 0;
 494         vt_cons[new_console]->vt_pid = -1;
 495         vt_cons[new_console]->vt_newvt = -1;
 496 }
 497 
 498 /*
 499  * Performs the back end of a vt switch
 500  */
 501 void complete_change_console(unsigned int new_console)
     /* [previous][next][first][last][top][bottom][index][help] */
 502 {
 503         unsigned char old_vc_mode;
 504 
 505         if (new_console == fg_console)
 506                 return;
 507         if (!vc_cons_allocated(new_console))
 508                 return;
 509         last_console = fg_console;
 510 
 511         /*
 512          * If we're switching, we could be going from KD_GRAPHICS to
 513          * KD_TEXT mode or vice versa, which means we need to blank or
 514          * unblank the screen later.
 515          */
 516         old_vc_mode = vt_cons[fg_console]->vc_mode;
 517         update_screen(new_console);
 518 
 519         /*
 520          * If this new console is under process control, send it a signal
 521          * telling it that it has acquired. Also check if it has died and
 522          * clean up (similar to logic employed in change_console())
 523          */
 524         if (vt_cons[new_console]->vt_mode.mode == VT_PROCESS)
 525         {
 526                 /*
 527                  * Send the signal as privileged - kill_proc() will
 528                  * tell us if the process has gone or something else
 529                  * is awry
 530                  */
 531                 if (kill_proc(vt_cons[new_console]->vt_pid,
 532                               vt_cons[new_console]->vt_mode.acqsig,
 533                               1) != 0)
 534                 {
 535                 /*
 536                  * The controlling process has died, so we revert back to
 537                  * normal operation. In this case, we'll also change back
 538                  * to KD_TEXT mode. I'm not sure if this is strictly correct
 539                  * but it saves the agony when the X server dies and the screen
 540                  * remains blanked due to KD_GRAPHICS! It would be nice to do
 541                  * this outside of VT_PROCESS but there is no single process
 542                  * to account for and tracking tty count may be undesirable.
 543                  */
 544                         reset_vc(new_console);
 545                 }
 546         }
 547 
 548         /*
 549          * We do this here because the controlling process above may have
 550          * gone, and so there is now a new vc_mode
 551          */
 552         if (old_vc_mode != vt_cons[new_console]->vc_mode)
 553         {
 554                 if (vt_cons[new_console]->vc_mode == KD_TEXT)
 555                         do_unblank_screen();
 556                 else
 557                         do_blank_screen(1);
 558         }
 559 
 560         /*
 561          * Wake anyone waiting for their VT to activate
 562          */
 563         vt_wake_waitactive();
 564         return;
 565 }
 566 
 567 /*
 568  * Performs the front-end of a vt switch
 569  */
 570 void change_console(unsigned int new_console)
     /* [previous][next][first][last][top][bottom][index][help] */
 571 {
 572         if (new_console == fg_console)
 573                 return;
 574         if (!vc_cons_allocated(new_console))
 575                 return;
 576 
 577         /*
 578          * If this vt is in process mode, then we need to handshake with
 579          * that process before switching. Essentially, we store where that
 580          * vt wants to switch to and wait for it to tell us when it's done
 581          * (via VT_RELDISP ioctl).
 582          *
 583          * We also check to see if the controlling process still exists.
 584          * If it doesn't, we reset this vt to auto mode and continue.
 585          * This is a cheap way to track process control. The worst thing
 586          * that can happen is: we send a signal to a process, it dies, and
 587          * the switch gets "lost" waiting for a response; hopefully, the
 588          * user will try again, we'll detect the process is gone (unless
 589          * the user waits just the right amount of time :-) and revert the
 590          * vt to auto control.
 591          */
 592         if (vt_cons[fg_console]->vt_mode.mode == VT_PROCESS)
 593         {
 594                 /*
 595                  * Send the signal as privileged - kill_proc() will
 596                  * tell us if the process has gone or something else
 597                  * is awry
 598                  */
 599                 if (kill_proc(vt_cons[fg_console]->vt_pid,
 600                               vt_cons[fg_console]->vt_mode.relsig,
 601                               1) == 0)
 602                 {
 603                         /*
 604                          * It worked. Mark the vt to switch to and
 605                          * return. The process needs to send us a
 606                          * VT_RELDISP ioctl to complete the switch.
 607                          */
 608                         vt_cons[fg_console]->vt_newvt = new_console;
 609                         return;
 610                 }
 611 
 612                 /*
 613                  * The controlling process has died, so we revert back to
 614                  * normal operation. In this case, we'll also change back
 615                  * to KD_TEXT mode. I'm not sure if this is strictly correct
 616                  * but it saves the agony when the X server dies and the screen
 617                  * remains blanked due to KD_GRAPHICS! It would be nice to do
 618                  * this outside of VT_PROCESS but there is no single process
 619                  * to account for and tracking tty count may be undesirable.
 620                  */
 621                 reset_vc(fg_console);
 622 
 623                 /*
 624                  * Fall through to normal (VT_AUTO) handling of the switch...
 625                  */
 626         }
 627 
 628         /*
 629          * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
 630          */
 631         if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
 632                 return;
 633 
 634         complete_change_console(new_console);
 635 }
 636 
 637 void wait_for_keypress(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 638 {
 639         sleep_on(&keypress_wait);
 640 }
 641 
 642 void stop_tty(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 643 {
 644         if (tty->stopped)
 645                 return;
 646         tty->stopped = 1;
 647         if (tty->link && tty->link->packet) {
 648                 tty->ctrl_status &= ~TIOCPKT_START;
 649                 tty->ctrl_status |= TIOCPKT_STOP;
 650                 wake_up_interruptible(&tty->link->read_wait);
 651         }
 652         if (tty->driver.stop)
 653                 (tty->driver.stop)(tty);
 654 }
 655 
 656 void start_tty(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 657 {
 658         if (!tty->stopped)
 659                 return;
 660         tty->stopped = 0;
 661         if (tty->link && tty->link->packet) {
 662                 tty->ctrl_status &= ~TIOCPKT_STOP;
 663                 tty->ctrl_status |= TIOCPKT_START;
 664                 wake_up_interruptible(&tty->link->read_wait);
 665         }
 666         if (tty->driver.start)
 667                 (tty->driver.start)(tty);
 668         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
 669             tty->ldisc.write_wakeup)
 670                 (tty->ldisc.write_wakeup)(tty);
 671         wake_up_interruptible(&tty->write_wait);
 672 }
 673 
 674 static int tty_read(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 675 {
 676         int i;
 677         struct tty_struct * tty;
 678 
 679         tty = (struct tty_struct *)file->private_data;
 680         if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
 681                 return -EIO;
 682         if (!tty || (tty->flags & (1 << TTY_IO_ERROR)))
 683                 return -EIO;
 684 
 685         /* This check not only needs to be done before reading, but also
 686            whenever read_chan() gets woken up after sleeping, so I've
 687            moved it to there.  This should only be done for the N_TTY
 688            line discipline, anyway.  Same goes for write_chan(). -- jlc. */
 689 #if 0
 690         if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
 691             (tty->pgrp > 0) &&
 692             (current->tty == tty) &&
 693             (tty->pgrp != current->pgrp))
 694                 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
 695                         return -EIO;
 696                 else {
 697                         (void) kill_pg(current->pgrp, SIGTTIN, 1);
 698                         return -ERESTARTSYS;
 699                 }
 700 #endif
 701         if (tty->ldisc.read)
 702                 /* XXX casts are for what kernel-wide prototypes should be. */
 703                 i = (tty->ldisc.read)(tty,file,(unsigned char *)buf,(unsigned int)count);
 704         else
 705                 i = -EIO;
 706         if (i > 0)
 707                 inode->i_atime = CURRENT_TIME;
 708         return i;
 709 }
 710 
 711 static int tty_write(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 712 {
 713         int i, is_console;
 714         struct tty_struct * tty;
 715 
 716         is_console = (inode->i_rdev == CONSOLE_DEV);
 717 
 718         if (is_console && redirect)
 719                 tty = redirect;
 720         else
 721                 tty = (struct tty_struct *)file->private_data;
 722         if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
 723                 return -EIO;
 724         if (!tty || !tty->driver.write || (tty->flags & (1 << TTY_IO_ERROR)))
 725                 return -EIO;
 726 #if 0
 727         if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
 728             (current->tty == tty) && (tty->pgrp != current->pgrp)) {
 729                 if (is_orphaned_pgrp(current->pgrp))
 730                         return -EIO;
 731                 if (!is_ignored(SIGTTOU)) {
 732                         (void) kill_pg(current->pgrp, SIGTTOU, 1);
 733                         return -ERESTARTSYS;
 734                 }
 735         }
 736 #endif
 737         if (tty->ldisc.write)
 738                 /* XXX casts are for what kernel-wide prototypes should be. */
 739                 i = (tty->ldisc.write)(tty,file,(unsigned char *)buf,(unsigned int)count);
 740         else
 741                 i = -EIO;
 742         if (i > 0)
 743                 inode->i_mtime = CURRENT_TIME;
 744         return i;
 745 }
 746 
 747 /*
 748  * This is so ripe with races that you should *really* not touch this
 749  * unless you know exactly what you are doing. All the changes have to be
 750  * made atomically, or there may be incorrect pointers all over the place.
 751  */
 752 static int init_dev(dev_t device, struct tty_struct **ret_tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 753 {
 754         struct tty_struct *tty, **tty_loc, *o_tty, **o_tty_loc;
 755         struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
 756         struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
 757         struct tty_driver *driver;      
 758         int retval;
 759         int idx;
 760 
 761         driver = get_tty_driver(device);
 762         if (!driver)
 763                 return -ENODEV;
 764 
 765         idx = MINOR(device) - driver->minor_start;
 766         tty = o_tty = NULL;
 767         tp = o_tp = NULL;
 768         ltp = o_ltp = NULL;
 769         o_tty_loc = NULL;
 770         o_tp_loc = o_ltp_loc = NULL;
 771 
 772         tty_loc = &driver->table[idx];
 773         tp_loc = &driver->termios[idx];
 774         ltp_loc = &driver->termios_locked[idx];
 775 
 776 repeat:
 777         retval = -EAGAIN;
 778         if (driver->type == TTY_DRIVER_TYPE_PTY &&
 779             driver->subtype == PTY_TYPE_MASTER &&
 780             *tty_loc && (*tty_loc)->count)
 781                 goto end_init;
 782         retval = -ENOMEM;
 783         if (!*tty_loc && !tty) {
 784                 if (!(tty = (struct tty_struct*) get_free_page(GFP_KERNEL)))
 785                         goto end_init;
 786                 initialize_tty_struct(tty);
 787                 tty->device = device;
 788                 tty->driver = *driver;
 789                 goto repeat;
 790         }
 791         if (!*tp_loc && !tp) {
 792                 tp = (struct termios *) kmalloc(sizeof(struct termios),
 793                                                 GFP_KERNEL);
 794                 if (!tp)
 795                         goto end_init;
 796                 *tp = driver->init_termios;
 797                 goto repeat;
 798         }
 799         if (!*ltp_loc && !ltp) {
 800                 ltp = (struct termios *) kmalloc(sizeof(struct termios),
 801                                                  GFP_KERNEL);
 802                 if (!ltp)
 803                         goto end_init;
 804                 memset(ltp, 0, sizeof(struct termios));
 805                 goto repeat;
 806         }
 807         if (driver->type == TTY_DRIVER_TYPE_PTY) {
 808                 o_tty_loc = &driver->other->table[idx];
 809                 o_tp_loc = &driver->other->termios[idx];
 810                 o_ltp_loc = &driver->other->termios_locked[idx];
 811 
 812                 if (!*o_tty_loc && !o_tty) {
 813                         dev_t   o_device;
 814                         
 815                         o_tty = (struct tty_struct *)
 816                                 get_free_page(GFP_KERNEL);
 817                         if (!o_tty)
 818                                 goto end_init;
 819                         o_device = MKDEV(driver->other->major,
 820                                          driver->other->minor_start + idx);
 821                         initialize_tty_struct(o_tty);
 822                         o_tty->device = o_device;
 823                         o_tty->driver = *driver->other;
 824                         goto repeat;
 825                 }
 826                 if (!*o_tp_loc && !o_tp) {
 827                         o_tp = (struct termios *)
 828                                 kmalloc(sizeof(struct termios), GFP_KERNEL);
 829                         if (!o_tp)
 830                                 goto end_init;
 831                         *o_tp = driver->other->init_termios;
 832                         goto repeat;
 833                 }
 834                 if (!*o_ltp_loc && !o_ltp) {
 835                         o_ltp = (struct termios *)
 836                                 kmalloc(sizeof(struct termios), GFP_KERNEL);
 837                         if (!o_ltp)
 838                                 goto end_init;
 839                         memset(o_ltp, 0, sizeof(struct termios));
 840                         goto repeat;
 841                 }
 842                 
 843         }
 844         /* Now we have allocated all the structures: update all the pointers.. */
 845         if (!*tp_loc) {
 846                 *tp_loc = tp;
 847                 tp = NULL;
 848         }
 849         if (!*ltp_loc) {
 850                 *ltp_loc = ltp;
 851                 ltp = NULL;
 852         }
 853         if (!*tty_loc) {
 854                 tty->termios = *tp_loc;
 855                 tty->termios_locked = *ltp_loc;
 856                 *tty_loc = tty;
 857                 (*driver->refcount)++;
 858                 (*tty_loc)->count++;
 859                 if (tty->ldisc.open) {
 860                         retval = (tty->ldisc.open)(tty);
 861                         if (retval < 0) {
 862                                 (*tty_loc)->count--;
 863                                 tty = NULL;
 864                                 goto end_init;
 865                         }
 866                 }
 867                 tty = NULL;
 868         } else
 869                 (*tty_loc)->count++;
 870         if (driver->type == TTY_DRIVER_TYPE_PTY) {
 871                 if (!*o_tp_loc) {
 872                         *o_tp_loc = o_tp;
 873                         o_tp = NULL;
 874                 }
 875                 if (!*o_ltp_loc) {
 876                         *o_ltp_loc = o_ltp;
 877                         o_ltp = NULL;
 878                 }
 879                 if (!*o_tty_loc) {
 880                         o_tty->termios = *o_tp_loc;
 881                         o_tty->termios_locked = *o_ltp_loc;
 882                         *o_tty_loc = o_tty;
 883                         (*driver->other->refcount)++;
 884                         if (o_tty->ldisc.open) {
 885                                 retval = (o_tty->ldisc.open)(o_tty);
 886                                 if (retval < 0) {
 887                                         (*tty_loc)->count--;
 888                                         o_tty = NULL;
 889                                         goto end_init;
 890                                 }
 891                         }
 892                         o_tty = NULL;
 893                 }
 894                 (*tty_loc)->link = *o_tty_loc;
 895                 (*o_tty_loc)->link = *tty_loc;
 896                 if (driver->subtype == PTY_TYPE_MASTER)
 897                         (*o_tty_loc)->count++;
 898         }
 899         (*tty_loc)->driver = *driver;
 900         *ret_tty = *tty_loc;
 901         retval = 0;
 902 end_init:
 903         if (tty)
 904                 free_page((unsigned long) tty);
 905         if (o_tty)
 906                 free_page((unsigned long) o_tty);
 907         if (tp)
 908                 kfree_s(tp, sizeof(struct termios));
 909         if (o_tp)
 910                 kfree_s(o_tp, sizeof(struct termios));
 911         if (ltp)
 912                 kfree_s(ltp, sizeof(struct termios));
 913         if (o_ltp)
 914                 kfree_s(o_ltp, sizeof(struct termios));
 915         return retval;
 916 }
 917 
 918 /*
 919  * Even releasing the tty structures is a tricky business.. We have
 920  * to be very careful that the structures are all released at the
 921  * same time, as interrupts might otherwise get the wrong pointers.
 922  */
 923 static void release_dev(struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 924 {
 925         struct tty_struct *tty, *o_tty;
 926         struct termios *tp, *o_tp, *ltp, *o_ltp;
 927         struct task_struct **p;
 928         int     idx;
 929         
 930         tty = (struct tty_struct *)filp->private_data;
 931         if (tty_paranoia_check(tty, filp->f_inode->i_rdev, "release_dev"))
 932                 return;
 933 
 934         check_tty_count(tty, "release_dev");
 935 
 936         tty_fasync(filp->f_inode, filp, 0);
 937 
 938         tp = tty->termios;
 939         ltp = tty->termios_locked;
 940 
 941         idx = MINOR(tty->device) - tty->driver.minor_start;
 942 #ifdef TTY_PARANOIA_CHECK
 943         if (idx < 0 || idx >= tty->driver.num) {
 944                 printk("release_dev: bad idx when trying to free (%d, %d)\n",
 945                        MAJOR(tty->device), MINOR(tty->device));
 946                 return;
 947         }
 948         if (tty != tty->driver.table[idx]) {
 949                 printk("release_dev: driver.table[%d] not tty for (%d, %d)\n",
 950                        idx, MAJOR(tty->device), MINOR(tty->device));
 951                 return;
 952         }
 953         if (tp != tty->driver.termios[idx]) {
 954                 printk("release_dev: driver.termios[%d] not termios for (%d, %d)\n",
 955                        idx, MAJOR(tty->device), MINOR(tty->device));
 956                 return;
 957         }
 958         if (ltp != tty->driver.termios_locked[idx]) {
 959                 printk("release_dev: driver.termios_locked[%d] not termios_locked for (%d, %d)\n",
 960                        idx, MAJOR(tty->device), MINOR(tty->device));
 961                 return;
 962         }
 963 #endif
 964 
 965 #ifdef TTY_DEBUG_HANGUP
 966         printk("release_dev of %s (tty count=%d)...", tty_name(tty),
 967                tty->count);
 968 #endif
 969 
 970         o_tty = tty->link;
 971         o_tp = (o_tty) ? o_tty->termios : NULL;
 972         o_ltp = (o_tty) ? o_tty->termios_locked : NULL;
 973 
 974 #ifdef TTY_PARANOIA_CHECK
 975         if (tty->driver.other) {
 976                 if (o_tty != tty->driver.other->table[idx]) {
 977                         printk("release_dev: other->table[%d] not o_tty for (%d, %d)\n",
 978                                idx, MAJOR(tty->device), MINOR(tty->device));
 979                         return;
 980                 }
 981                 if (o_tp != tty->driver.other->termios[idx]) {
 982                         printk("release_dev: other->termios[%d] not o_termios for (%d, %d)\n",
 983                                idx, MAJOR(tty->device), MINOR(tty->device));
 984                         return;
 985                 }
 986                 if (o_ltp != tty->driver.other->termios_locked[idx]) {
 987                         printk("release_dev: other->termios_locked[%d] not o_termios_locked for (%d, %d)\n",
 988                                idx, MAJOR(tty->device), MINOR(tty->device));
 989                         return;
 990                 }
 991 
 992                 if (o_tty->link != tty) {
 993                         printk("release_dev: bad pty pointers\n");
 994                         return;
 995                 }
 996         }
 997 #endif
 998         
 999         if (tty->driver.close)
1000                 tty->driver.close(tty, filp);
1001         if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1002             tty->driver.subtype == PTY_TYPE_MASTER) {
1003                 if (--tty->link->count < 0) {
1004                         printk("release_dev: bad pty slave count (%d) for %s\n",
1005                                tty->count, tty_name(tty));
1006                         tty->link->count = 0;
1007                 }
1008         }
1009         if (--tty->count < 0) {
1010                 printk("release_dev: bad tty->count (%d) for %s\n",
1011                        tty->count, tty_name(tty));
1012                 tty->count = 0;
1013         }
1014         if (tty->count)
1015                 return;
1016 
1017         if (o_tty) {
1018                 if (o_tty->count)
1019                         return;
1020                 tty->driver.other->table[idx] = NULL;
1021                 tty->driver.other->termios[idx] = NULL;
1022                 kfree_s(o_tp, sizeof(struct termios));
1023         }
1024         
1025 #ifdef TTY_DEBUG_HANGUP
1026         printk("freeing tty structure...");
1027 #endif
1028 
1029         /*
1030          * Make sure there aren't any processes that still think this
1031          * tty is their controlling tty.
1032          */
1033         for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1034                 if (*p == 0)
1035                         continue;
1036                 if ((*p)->tty == tty)
1037                         (*p)->tty = NULL;
1038                 if (o_tty && (*p)->tty == o_tty)
1039                         (*p)->tty = NULL;
1040         }
1041 
1042         /*
1043          * Shutdown the current line discipline, and reset it to
1044          * N_TTY.
1045          */
1046         if (tty->ldisc.close)
1047                 (tty->ldisc.close)(tty);
1048         tty->ldisc = ldiscs[N_TTY];
1049         tty->termios->c_line = N_TTY;
1050         if (o_tty) {
1051                 if (o_tty->ldisc.close)
1052                         (o_tty->ldisc.close)(o_tty);
1053                 o_tty->ldisc = ldiscs[N_TTY];
1054                 o_tty->termios->c_line = N_TTY;
1055         }
1056         
1057         tty->driver.table[idx] = NULL;
1058         if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1059                 tty->driver.termios[idx] = NULL;
1060                 kfree_s(tp, sizeof(struct termios));
1061         }
1062         if (tty == redirect || o_tty == redirect)
1063                 redirect = NULL;
1064         /*
1065          * Make sure that the tty's task queue isn't activated.  If it
1066          * is, take it out of the linked list.
1067          */
1068         cli();
1069         if (tty->flip.tqueue.sync) {
1070                 struct tq_struct *tq, *prev;
1071 
1072                 for (tq=tq_timer, prev=0; tq; prev=tq, tq=tq->next) {
1073                         if (tq == &tty->flip.tqueue) {
1074                                 if (prev)
1075                                         prev->next = tq->next;
1076                                 else
1077                                         tq_timer = tq->next;
1078                                 break;
1079                         }
1080                 }
1081         }
1082         sti();
1083         tty->magic = 0;
1084         (*tty->driver.refcount)--;
1085         free_page((unsigned long) tty);
1086         filp->private_data = 0;
1087         if (o_tty) {
1088                 o_tty->magic = 0;
1089                 (*o_tty->driver.refcount)--;
1090                 free_page((unsigned long) o_tty);
1091         }
1092 }
1093 
1094 /*
1095  * tty_open and tty_release keep up the tty count that contains the
1096  * number of opens done on a tty. We cannot use the inode-count, as
1097  * different inodes might point to the same tty.
1098  *
1099  * Open-counting is needed for pty masters, as well as for keeping
1100  * track of serial lines: DTR is dropped when the last close happens.
1101  * (This is not done solely through tty->count, now.  - Ted 1/27/92)
1102  *
1103  * The termios state of a pty is reset on first open so that
1104  * settings don't persist across reuse.
1105  */
1106 static int tty_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1107 {
1108         struct tty_struct *tty;
1109         int minor;
1110         int noctty, retval;
1111         dev_t device;
1112 
1113 retry_open:
1114         noctty = filp->f_flags & O_NOCTTY;
1115         device = inode->i_rdev;
1116         if (device == TTY_DEV) {
1117                 if (!current->tty)
1118                         return -ENXIO;
1119                 device = current->tty->device;
1120                 /* noctty = 1; */
1121         }
1122         if (device == CONSOLE_DEV) {
1123                 device = MKDEV(TTY_MAJOR, fg_console+1);
1124                 noctty = 1;
1125         }
1126         minor = MINOR(device);
1127         
1128         retval = init_dev(device, &tty);
1129         if (retval)
1130                 return retval;
1131         filp->private_data = tty;
1132         check_tty_count(tty, "tty_open");
1133         if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1134             tty->driver.subtype == PTY_TYPE_MASTER)
1135                 noctty = 1;
1136 #ifdef TTY_DEBUG_HANGUP
1137         printk("opening %s...", tty_name(tty));
1138 #endif
1139         if (tty->driver.open)
1140                 retval = tty->driver.open(tty, filp);
1141         else
1142                 retval = -ENODEV;
1143 
1144         if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1145                 retval = -EBUSY;
1146 
1147         if (retval) {
1148 #ifdef TTY_DEBUG_HANGUP
1149                 printk("error %d in opening %s...", retval, tty_name(tty));
1150 #endif
1151 
1152                 release_dev(filp);
1153                 if (retval != -ERESTARTSYS)
1154                         return retval;
1155                 if (current->signal & ~current->blocked)
1156                         return retval;
1157                 schedule();
1158                 /*
1159                  * Need to reset f_op in case a hangup happened.
1160                  */
1161                 filp->f_op = &tty_fops;
1162                 goto retry_open;
1163         }
1164         if (!noctty &&
1165             current->leader &&
1166             !current->tty &&
1167             tty->session == 0) {
1168                 current->tty = tty;
1169                 current->tty_old_pgrp = 0;
1170                 tty->session = current->session;
1171                 tty->pgrp = current->pgrp;
1172         }
1173         return 0;
1174 }
1175 
1176 /*
1177  * Note that releasing a pty master also releases the child, so
1178  * we have to make the redirection checks after that and on both
1179  * sides of a pty.
1180  */
1181 static void tty_release(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1182 {
1183         release_dev(filp);
1184 }
1185 
1186 static int tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1187 {
1188         struct tty_struct * tty;
1189 
1190         tty = (struct tty_struct *)filp->private_data;
1191         if (tty_paranoia_check(tty, inode->i_rdev, "tty_select"))
1192                 return 0;
1193 
1194         if (tty->ldisc.select)
1195                 return (tty->ldisc.select)(tty, inode, filp, sel_type, wait);
1196         return 0;
1197 }
1198 
1199 static int tty_fasync(struct inode * inode, struct file * filp, int on)
     /* [previous][next][first][last][top][bottom][index][help] */
1200 {
1201         struct tty_struct * tty;
1202         struct fasync_struct *fa, *prev;
1203 
1204         tty = (struct tty_struct *)filp->private_data;
1205         if (tty_paranoia_check(tty, inode->i_rdev, "tty_fasync"))
1206                 return 0;
1207 
1208         for (fa = tty->fasync, prev = 0; fa; prev= fa, fa = fa->fa_next) {
1209                 if (fa->fa_file == filp)
1210                         break;
1211         }
1212 
1213         if (on) {
1214                 if (fa)
1215                         return 0;
1216                 fa = (struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1217                 if (!fa)
1218                         return -ENOMEM;
1219                 fa->magic = FASYNC_MAGIC;
1220                 fa->fa_file = filp;
1221                 fa->fa_next = tty->fasync;
1222                 tty->fasync = fa;
1223                 if (!tty->read_wait)
1224                         tty->minimum_to_wake = 1;
1225                 if (filp->f_owner == 0) {
1226                         if (tty->pgrp)
1227                                 filp->f_owner = -tty->pgrp;
1228                         else
1229                                 filp->f_owner = current->pid;
1230                 }
1231         } else {
1232                 if (!fa)
1233                         return 0;
1234                 if (prev)
1235                         prev->fa_next = fa->fa_next;
1236                 else
1237                         tty->fasync = fa->fa_next;
1238                 kfree_s(fa, sizeof(struct fasync_struct));
1239                 if (!tty->fasync && !tty->read_wait)
1240                         tty->minimum_to_wake = N_TTY_BUF_SIZE;
1241         }
1242         return 0;       
1243 }
1244 
1245 #if 0
1246 /*
1247  * XXX does anyone use this anymore?!?
1248  */
1249 static int do_get_ps_info(unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1250 {
1251         struct tstruct {
1252                 int flag;
1253                 int present[NR_TASKS];
1254                 struct task_struct tasks[NR_TASKS];
1255         };
1256         struct tstruct *ts = (struct tstruct *)arg;
1257         struct task_struct **p;
1258         char *c, *d;
1259         int i, n = 0;
1260         
1261         i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct tstruct));
1262         if (i)
1263                 return i;
1264         for (p = &FIRST_TASK ; p <= &LAST_TASK ; p++, n++)
1265                 if (*p)
1266                 {
1267                         c = (char *)(*p);
1268                         d = (char *)(ts->tasks+n);
1269                         for (i=0 ; i<sizeof(struct task_struct) ; i++)
1270                                 put_fs_byte(*c++, d++);
1271                         put_fs_long(1, (unsigned long *)(ts->present+n));
1272                 }
1273                 else    
1274                         put_fs_long(0, (unsigned long *)(ts->present+n));
1275         return(0);                      
1276 }
1277 #endif
1278 
1279 static int tty_ioctl(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1280                      unsigned int cmd, unsigned long arg)
1281 {
1282         int     retval;
1283         struct tty_struct * tty;
1284         struct tty_struct * real_tty;
1285         struct winsize tmp_ws;
1286         pid_t pgrp;
1287         unsigned char   ch;
1288         char    mbz = 0;
1289         
1290         tty = (struct tty_struct *)file->private_data;
1291         if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1292                 return -EINVAL;
1293 
1294         if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1295             tty->driver.subtype == PTY_TYPE_MASTER)
1296                 real_tty = tty->link;
1297         else
1298                 real_tty = tty;
1299 
1300         switch (cmd) {
1301                 case TIOCSTI:
1302                         if ((current->tty != tty) && !suser())
1303                                 return -EPERM;
1304                         retval = verify_area(VERIFY_READ, (void *) arg, 1);
1305                         if (retval)
1306                                 return retval;
1307                         ch = get_fs_byte((char *) arg);
1308                         tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1309                         return 0;
1310                 case TIOCGWINSZ:
1311                         retval = verify_area(VERIFY_WRITE, (void *) arg,
1312                                              sizeof (struct winsize));
1313                         if (retval)
1314                                 return retval;
1315                         memcpy_tofs((struct winsize *) arg, &tty->winsize,
1316                                     sizeof (struct winsize));
1317                         return 0;
1318                 case TIOCSWINSZ:
1319                         retval = verify_area(VERIFY_READ, (void *) arg,
1320                                              sizeof (struct winsize));
1321                         if (retval)
1322                                 return retval;                  
1323                         memcpy_fromfs(&tmp_ws, (struct winsize *) arg,
1324                                       sizeof (struct winsize));
1325                         if (memcmp(&tmp_ws, &tty->winsize,
1326                                    sizeof(struct winsize))) {
1327                                 if (tty->pgrp > 0)
1328                                         kill_pg(tty->pgrp, SIGWINCH, 1);
1329                                 if ((real_tty->pgrp != tty->pgrp) &&
1330                                     (real_tty->pgrp > 0))
1331                                         kill_pg(real_tty->pgrp, SIGWINCH, 1);
1332                         }
1333                         tty->winsize = tmp_ws;
1334                         real_tty->winsize = tmp_ws;
1335                         return 0;
1336                 case TIOCCONS:
1337                         if (tty->driver.type == TTY_DRIVER_TYPE_CONSOLE) {
1338                                 if (!suser())
1339                                         return -EPERM;
1340                                 redirect = NULL;
1341                                 return 0;
1342                         }
1343                         if (redirect)
1344                                 return -EBUSY;
1345                         redirect = real_tty;
1346                         return 0;
1347                 case FIONBIO:
1348                         retval = verify_area(VERIFY_READ, (void *) arg, sizeof(long));
1349                         if (retval)
1350                                 return retval;
1351                         arg = get_fs_long((unsigned long *) arg);
1352                         if (arg)
1353                                 file->f_flags |= O_NONBLOCK;
1354                         else
1355                                 file->f_flags &= ~O_NONBLOCK;
1356                         return 0;
1357                 case TIOCEXCL:
1358                         set_bit(TTY_EXCLUSIVE, &tty->flags);
1359                         return 0;
1360                 case TIOCNXCL:
1361                         clear_bit(TTY_EXCLUSIVE, &tty->flags);
1362                         return 0;
1363                 case TIOCNOTTY:
1364                         if (current->tty != tty)
1365                                 return -ENOTTY;
1366                         if (current->leader)
1367                                 disassociate_ctty(0);
1368                         current->tty = NULL;
1369                         return 0;
1370                 case TIOCSCTTY:
1371                         if (current->leader &&
1372                             (current->session == tty->session))
1373                                 return 0;
1374                         /*
1375                          * The process must be a session leader and
1376                          * not have a controlling tty already.
1377                          */
1378                         if (!current->leader || current->tty)
1379                                 return -EPERM;
1380                         if (tty->session > 0) {
1381                                 /*
1382                                  * This tty is already the controlling
1383                                  * tty for another session group!
1384                                  */
1385                                 if ((arg == 1) && suser()) {
1386                                         /*
1387                                          * Steal it away
1388                                          */
1389                                         struct task_struct *p;
1390 
1391                                         for_each_task(p)
1392                                                 if (p->tty == tty)
1393                                                         p->tty = NULL;
1394                                 } else
1395                                         return -EPERM;
1396                         }
1397                         current->tty = tty;
1398                         current->tty_old_pgrp = 0;
1399                         tty->session = current->session;
1400                         tty->pgrp = current->pgrp;
1401                         return 0;
1402                 case TIOCGPGRP:
1403                         /*
1404                          * (tty == real_tty) is a cheap way of
1405                          * testing if the tty is NOT a master pty.
1406                          */
1407                         if (tty == real_tty && current->tty != real_tty)
1408                                 return -ENOTTY;
1409                         retval = verify_area(VERIFY_WRITE, (void *) arg,
1410                                              sizeof (pid_t));
1411                         if (retval)
1412                                 return retval;
1413                         put_fs_long(real_tty->pgrp, (pid_t *) arg);
1414                         return 0;
1415                 case TIOCSPGRP:
1416                         retval = tty_check_change(real_tty);
1417                         if (retval)
1418                                 return retval;
1419                         if (!current->tty ||
1420                             (current->tty != real_tty) ||
1421                             (real_tty->session != current->session))
1422                                 return -ENOTTY;
1423                         pgrp = get_fs_long((pid_t *) arg);
1424                         if (pgrp < 0)
1425                                 return -EINVAL;
1426                         if (session_of_pgrp(pgrp) != current->session)
1427                                 return -EPERM;
1428                         real_tty->pgrp = pgrp;
1429                         return 0;
1430                 case TIOCGETD:
1431                         retval = verify_area(VERIFY_WRITE, (void *) arg,
1432                                              sizeof (unsigned long));
1433                         if (retval)
1434                                 return retval;
1435                         put_fs_long(tty->ldisc.num, (unsigned long *) arg);
1436                         return 0;
1437                 case TIOCSETD:
1438                         retval = tty_check_change(tty);
1439                         if (retval)
1440                                 return retval;
1441                         arg = get_fs_long((unsigned long *) arg);
1442                         return tty_set_ldisc(tty, arg);
1443                 case TIOCLINUX:
1444                         if (tty->driver.type != TTY_DRIVER_TYPE_CONSOLE)
1445                                 return -EINVAL;
1446                         if (current->tty != tty && !suser())
1447                                 return -EPERM;
1448                         retval = verify_area(VERIFY_READ, (void *) arg, 1);
1449                         if (retval)
1450                                 return retval;
1451                         switch (retval = get_fs_byte((char *)arg))
1452                         {
1453                                 case 0:
1454                                 case 8:
1455                                 case 9:
1456                                         printk("TIOCLINUX (0/8/9) ioctl is gone - use /dev/vcs\n");
1457                                         return -EINVAL;
1458 #if 0
1459                                 case 1:
1460                                         printk("Deprecated TIOCLINUX (1) ioctl\n");
1461                                         return do_get_ps_info(arg);
1462 #endif
1463                                 case 2:
1464                                         return set_selection(arg, tty);
1465                                 case 3:
1466                                         return paste_selection(tty);
1467                                 case 4:
1468                                         do_unblank_screen();
1469                                         return 0;
1470                                 case 5:
1471                                         return sel_loadlut(arg);
1472                                 case 6:
1473                         /*
1474                          * Make it possible to react to Shift+Mousebutton.
1475                          * Note that 'shift_state' is an undocumented
1476                          * kernel-internal variable; programs not closely
1477                          * related to the kernel should not use this.
1478                          */
1479                                         put_fs_byte(shift_state,arg);
1480                                         return 0;
1481                                 case 7:
1482                                         put_fs_byte(mouse_reporting(),arg);
1483                                         return 0;
1484                                 case 10:
1485                                         set_vesa_blanking(arg);
1486                                         return 0;
1487                                 default: 
1488                                         return -EINVAL;
1489                         }
1490 
1491                 case TIOCTTYGSTRUCT:
1492                         retval = verify_area(VERIFY_WRITE, (void *) arg,
1493                                                 sizeof(struct tty_struct));
1494                         if (retval)
1495                                 return retval;
1496                         memcpy_tofs((struct tty_struct *) arg,
1497                                     tty, sizeof(struct tty_struct));
1498                         return 0;
1499                 default:
1500                         if (tty->driver.ioctl) {
1501                                 retval = (tty->driver.ioctl)(tty, file,
1502                                                              cmd, arg);
1503                                 if (retval != -ENOIOCTLCMD)
1504                                         return retval;
1505                         }
1506                         if (tty->ldisc.ioctl) {
1507                                 retval = (tty->ldisc.ioctl)(tty, file,
1508                                                             cmd, arg);
1509                                 if (retval != -ENOIOCTLCMD)
1510                                         return retval;
1511                         }
1512                         return -EINVAL;
1513                 }
1514 }
1515 
1516 
1517 /*
1518  * This implements the "Secure Attention Key" ---  the idea is to
1519  * prevent trojan horses by killing all processes associated with this
1520  * tty when the user hits the "Secure Attention Key".  Required for
1521  * super-paranoid applications --- see the Orange Book for more details.
1522  * 
1523  * This code could be nicer; ideally it should send a HUP, wait a few
1524  * seconds, then send a INT, and then a KILL signal.  But you then
1525  * have to coordinate with the init process, since all processes associated
1526  * with the current tty must be dead before the new getty is allowed
1527  * to spawn.
1528  */
1529 void do_SAK( struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1530 {
1531 #ifdef TTY_SOFT_SAK
1532         tty_hangup(tty);
1533 #else
1534         struct task_struct **p;
1535         int session;
1536         int             i;
1537         struct file     *filp;
1538         
1539         if (!tty)
1540                 return;
1541         session  = tty->session;
1542         if (tty->ldisc.flush_buffer)
1543                 tty->ldisc.flush_buffer(tty);
1544         if (tty->driver.flush_buffer)
1545                 tty->driver.flush_buffer(tty);
1546         for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1547                 if (!(*p))
1548                         continue;
1549                 if (((*p)->tty == tty) ||
1550                     ((session > 0) && ((*p)->session == session)))
1551                         send_sig(SIGKILL, *p, 1);
1552                 else {
1553                         for (i=0; i < NR_OPEN; i++) {
1554                                 filp = (*p)->files->fd[i];
1555                                 if (filp && (filp->f_op == &tty_fops) &&
1556                                     (filp->private_data == tty)) {
1557                                         send_sig(SIGKILL, *p, 1);
1558                                         break;
1559                                 }
1560                         }
1561                 }
1562         }
1563 #endif
1564 }
1565 
1566 /*
1567  * This routine is called out of the software interrupt to flush data
1568  * from the flip buffer to the line discipline.
1569  */
1570 static void flush_to_ldisc(void *private_)
     /* [previous][next][first][last][top][bottom][index][help] */
1571 {
1572         struct tty_struct *tty = (struct tty_struct *) private_;
1573         unsigned char   *cp;
1574         char            *fp;
1575         int             count;
1576 
1577         if (tty->flip.buf_num) {
1578                 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1579                 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1580                 tty->flip.buf_num = 0;
1581 
1582                 cli();
1583                 tty->flip.char_buf_ptr = tty->flip.char_buf;
1584                 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1585         } else {
1586                 cp = tty->flip.char_buf;
1587                 fp = tty->flip.flag_buf;
1588                 tty->flip.buf_num = 1;
1589 
1590                 cli();
1591                 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1592                 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1593         }
1594         count = tty->flip.count;
1595         tty->flip.count = 0;
1596         sti();
1597         
1598 #if 0
1599         if (count > tty->max_flip_cnt)
1600                 tty->max_flip_cnt = count;
1601 #endif
1602         tty->ldisc.receive_buf(tty, cp, fp, count);
1603 }
1604 
1605 /*
1606  * This subroutine initializes a tty structure.
1607  */
1608 static void initialize_tty_struct(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1609 {
1610         memset(tty, 0, sizeof(struct tty_struct));
1611         tty->magic = TTY_MAGIC;
1612         tty->ldisc = ldiscs[N_TTY];
1613         tty->pgrp = -1;
1614         tty->flip.char_buf_ptr = tty->flip.char_buf;
1615         tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1616         tty->flip.tqueue.routine = flush_to_ldisc;
1617         tty->flip.tqueue.data = tty;
1618 }
1619 
1620 /*
1621  * The default put_char routine if the driver did not define one.
1622  */
1623 void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
1624 {
1625         tty->driver.write(tty, 0, &ch, 1);
1626 }
1627 
1628 /*
1629  * Called by a tty driver to register itself.
1630  */
1631 int tty_register_driver(struct tty_driver *driver)
     /* [previous][next][first][last][top][bottom][index][help] */
1632 {
1633         int error;
1634 
1635         if (driver->flags & TTY_DRIVER_INSTALLED)
1636                 return 0;
1637 
1638         error = register_chrdev(driver->major, driver->name, &tty_fops);
1639         if (error < 0)
1640                 return error;
1641         else if(driver->major == 0)
1642                 driver->major = error;
1643 
1644         if (!driver->put_char)
1645                 driver->put_char = tty_default_put_char;
1646         
1647         driver->prev = 0;
1648         driver->next = tty_drivers;
1649         if (tty_drivers) tty_drivers->prev = driver;
1650         tty_drivers = driver;
1651         return error;
1652 }
1653 
1654 /*
1655  * Called by a tty driver to unregister itself.
1656  */
1657 int tty_unregister_driver(struct tty_driver *driver)
     /* [previous][next][first][last][top][bottom][index][help] */
1658 {
1659         int     retval;
1660         struct tty_driver *p;
1661         int     found = 0;
1662         int     major_inuse = 0;
1663         
1664         if (*driver->refcount)
1665                 return -EBUSY;
1666 
1667         for (p = tty_drivers; p; p = p->next) {
1668                 if (p == driver)
1669                         found++;
1670                 else if (p->major == driver->major)
1671                         major_inuse++;
1672         }
1673 
1674         if (!major_inuse) {
1675                 retval = unregister_chrdev(driver->major, driver->name);
1676                 if (retval)
1677                         return retval;
1678         }
1679 
1680         if (driver->prev)
1681                 driver->prev->next = driver->next;
1682         else
1683                 tty_drivers = driver->next;
1684         
1685         if (driver->next)
1686                 driver->next->prev = driver->prev;
1687 
1688         return 0;
1689 }
1690 
1691 
1692 /*
1693  * Initialize the console device. This is called *early*, so
1694  * we can't necessarily depend on lots of kernel help here.
1695  * Just do some early initializations, and do the complex setup
1696  * later.
1697  */
1698 long console_init(long kmem_start, long kmem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1699 {
1700         /* Setup the default TTY line discipline. */
1701         memset(ldiscs, 0, sizeof(ldiscs));
1702         (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1703 
1704         /*
1705          * Set up the standard termios.  Individual tty drivers may 
1706          * deviate from this; this is used as a template.
1707          */
1708         memset(&tty_std_termios, 0, sizeof(struct termios));
1709         memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
1710         tty_std_termios.c_iflag = ICRNL | IXON;
1711         tty_std_termios.c_oflag = OPOST | ONLCR;
1712         tty_std_termios.c_cflag = B38400 | CS8 | CREAD;
1713         tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
1714                 ECHOCTL | ECHOKE | IEXTEN;
1715 
1716         /*
1717          * set up the console device so that later boot sequences can 
1718          * inform about problems etc..
1719          */
1720         return con_init(kmem_start);
1721 }
1722 
1723 /*
1724  * Ok, now we can initialize the rest of the tty devices and can count
1725  * on memory allocations, interrupts etc..
1726  */
1727 long tty_init(long kmem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1728 {
1729         if (sizeof(struct tty_struct) > PAGE_SIZE)
1730                 panic("size of tty structure > PAGE_SIZE!");
1731         if (register_chrdev(TTY_MAJOR,"tty",&tty_fops))
1732                 panic("unable to get major %d for tty device", TTY_MAJOR);
1733         if (register_chrdev(TTYAUX_MAJOR,"cua",&tty_fops))
1734                 panic("unable to get major %d for tty device", TTYAUX_MAJOR);
1735 
1736         kmem_start = kbd_init(kmem_start);
1737         kmem_start = rs_init(kmem_start);
1738 #ifdef CONFIG_CYCLADES
1739         kmem_start = cy_init(kmem_start);
1740 #endif
1741         kmem_start = pty_init(kmem_start);
1742         kmem_start = vcs_init(kmem_start);
1743         return kmem_start;
1744 }

/* [previous][next][first][last][top][bottom][index][help] */