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

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