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. tty_register_ldisc
  5. tty_set_ldisc
  6. get_tty_driver
  7. tty_check_change
  8. hung_up_tty_read
  9. hung_up_tty_write
  10. hung_up_tty_select
  11. hung_up_tty_ioctl
  12. tty_lseek
  13. do_tty_hangup
  14. tty_hangup
  15. tty_vhangup
  16. tty_hung_up_p
  17. disassociate_ctty
  18. vt_waitactive
  19. complete_change_console
  20. change_console
  21. wait_for_keypress
  22. stop_tty
  23. start_tty
  24. tty_read
  25. tty_write
  26. init_dev
  27. release_dev
  28. tty_open
  29. tty_release
  30. tty_select
  31. tty_fasync
  32. do_get_ps_info
  33. tty_ioctl
  34. do_SAK
  35. flush_to_ldisc
  36. initialize_tty_struct
  37. tty_default_put_char
  38. tty_register_driver
  39. console_init
  40. tty_init

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

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