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. 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 discpline 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);
  70 extern int paste_selection(struct tty_struct *tty);
  71 #endif /* CONFIG_SELECTION */
  72 extern int do_screendump(int arg);
  73 
  74 struct termios tty_std_termios;         /* for the benefit of tty drivers  */
  75 struct tty_driver *tty_drivers;         /* linked list of tty drivers */
  76 struct tty_ldisc ldiscs[NR_LDISCS];     /* line disc dispatch table     */
  77 
  78 /*
  79  * fg_console is the current virtual console,
  80  * redirect is the pseudo-tty that console output
  81  * is redirected to if asked by TIOCCONS.
  82  */
  83 int fg_console = 0;
  84 struct tty_struct * redirect = NULL;
  85 struct wait_queue * keypress_wait = NULL;
  86 
  87 static void initialize_tty_struct(struct tty_struct *tty);
  88 
  89 static int tty_read(struct inode *, struct file *, char *, int);
  90 static int tty_write(struct inode *, struct file *, char *, int);
  91 static int tty_select(struct inode *, struct file *, int, select_table *);
  92 static int tty_open(struct inode *, struct file *);
  93 static void tty_release(struct inode *, struct file *);
  94 static int tty_ioctl(struct inode * inode, struct file * file,
  95                      unsigned int cmd, unsigned long arg);
  96 static int tty_fasync(struct inode * inode, struct file * filp, int on);
  97 
  98 #ifndef MIN
  99 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
 100 #endif
 101 
 102 /*
 103  * These two routines return the name of tty.  tty_name() should NOT
 104  * be used in interrupt drivers, since it's not re-entrant.  Use
 105  * _tty_name() instead.
 106  */
 107 char *_tty_name(struct tty_struct *tty, char *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 108 {
 109         sprintf(buf, "%s%d", tty->driver.name,
 110                 MINOR(tty->device) - tty->driver.minor_start +
 111                 tty->driver.name_base);
 112         return buf;
 113 }
 114 
 115 char *tty_name(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 116 {
 117         static char buf[64];
 118 
 119         return(_tty_name(tty, buf));
 120 }
 121 
 122 #define TTY_PARANOIA_CHECK
 123 
 124 inline int tty_paranoia_check(struct tty_struct *tty, dev_t device,
     /* [previous][next][first][last][top][bottom][index][help] */
 125                               const char *routine)
 126 {
 127 #ifdef TTY_PARANOIA_CHECK
 128         static const char *badmagic =
 129                 "Warning: bad magic number for tty struct (%d, %d) in %s\n";
 130         static const char *badtty =
 131                 "Warning: null TTY for (%d, %d) in %s\n";
 132 
 133         if (!tty) {
 134                 printk(badtty, MAJOR(device), MINOR(device), routine);
 135                 return 1;
 136         }
 137         if (tty->magic != TTY_MAGIC) {
 138                 printk(badmagic, MAJOR(device), MINOR(device), routine);
 139                 return 1;
 140         }
 141 #endif
 142         return 0;
 143 }
 144 
 145 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
     /* [previous][next][first][last][top][bottom][index][help] */
 146 {
 147         if (disc < N_TTY || disc >= NR_LDISCS)
 148                 return -EINVAL;
 149         
 150         if (new_ldisc) {
 151                 ldiscs[disc] = *new_ldisc;
 152                 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
 153                 ldiscs[disc].num = disc;
 154         } else
 155                 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
 156         
 157         return 0;
 158 }
 159 
 160 /* Set the discipline of a tty line. */
 161 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
     /* [previous][next][first][last][top][bottom][index][help] */
 162 {
 163         int     retval;
 164         struct  tty_ldisc o_ldisc;
 165 
 166         if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS) ||
 167             !(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
 168                 return -EINVAL;
 169 
 170         if (tty->ldisc.num == ldisc)
 171                 return 0;       /* We are already in the desired discipline */
 172         o_ldisc = tty->ldisc;
 173 
 174         /* Shutdown the current discipline. */
 175         if (tty->ldisc.close)
 176                 (tty->ldisc.close)(tty);
 177 
 178         /* Now set up the new line discipline. */
 179         tty->ldisc = ldiscs[ldisc];
 180         tty->termios->c_line = ldisc;
 181         if (tty->ldisc.open) {
 182                 retval = (tty->ldisc.open)(tty);
 183                 if (retval >= 0)
 184                         return retval;
 185                 
 186                 tty->ldisc = o_ldisc;
 187                 tty->termios->c_line = tty->ldisc.num;
 188                 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
 189                         tty->ldisc = ldiscs[N_TTY];
 190                         tty->termios->c_line = N_TTY;
 191                         if (tty->ldisc.open) {
 192                                 int r = tty->ldisc.open(tty);
 193 
 194                                 if (r < 0)
 195                                         panic("Couldn't open N_TTY ldisc for "
 196                                               "%s --- error %d.",
 197                                               tty_name(tty), r);
 198                         }
 199                 }
 200                 return retval;
 201         }
 202         return 0;
 203 }
 204 
 205 /*
 206  * This routine returns a tty driver structure, given a device number
 207  */
 208 struct tty_driver *get_tty_driver(dev_t device)
     /* [previous][next][first][last][top][bottom][index][help] */
 209 {
 210         int     major, minor;
 211         struct tty_driver *p;
 212         
 213         minor = MINOR(device);
 214         major = MAJOR(device);
 215 
 216         for (p = tty_drivers; p; p = p->next) {
 217                 if (p->major != major)
 218                         continue;
 219                 if (minor < p->minor_start)
 220                         continue;
 221                 if (minor >= p->minor_start + p->num)
 222                         continue;
 223                 return p;
 224         }
 225         return NULL;
 226 }
 227 
 228 /*
 229  * If we try to write to, or set the state of, a terminal and we're
 230  * not in the foreground, send a SIGTTOU.  If the signal is blocked or
 231  * ignored, go ahead and perform the operation.  (POSIX 7.2)
 232  */
 233 int tty_check_change(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235         if (current->tty != tty)
 236                 return 0;
 237         if (tty->pgrp <= 0) {
 238                 printk("tty_check_change: tty->pgrp <= 0!\n");
 239                 return 0;
 240         }
 241         if (current->pgrp == tty->pgrp)
 242                 return 0;
 243         if (is_ignored(SIGTTOU))
 244                 return 0;
 245         if (is_orphaned_pgrp(current->pgrp))
 246                 return -EIO;
 247         (void) kill_pg(current->pgrp,SIGTTOU,1);
 248         return -ERESTARTSYS;
 249 }
 250 
 251 static int hung_up_tty_read(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 252 {
 253         return 0;
 254 }
 255 
 256 static int hung_up_tty_write(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 257 {
 258         return -EIO;
 259 }
 260 
 261 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] */
 262 {
 263         return 1;
 264 }
 265 
 266 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 267                              unsigned int cmd, unsigned long arg)
 268 {
 269         return -EIO;
 270 }
 271 
 272 static int tty_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
     /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274         return -ESPIPE;
 275 }
 276 
 277 static struct file_operations tty_fops = {
 278         tty_lseek,
 279         tty_read,
 280         tty_write,
 281         NULL,           /* tty_readdir */
 282         tty_select,
 283         tty_ioctl,
 284         NULL,           /* tty_mmap */
 285         tty_open,
 286         tty_release,
 287         NULL,           /* tty_fsync */
 288         tty_fasync
 289 };
 290 
 291 static struct file_operations hung_up_tty_fops = {
 292         tty_lseek,
 293         hung_up_tty_read,
 294         hung_up_tty_write,
 295         NULL,           /* hung_up_tty_readdir */
 296         hung_up_tty_select,
 297         hung_up_tty_ioctl,
 298         NULL,           /* hung_up_tty_mmap */
 299         NULL,           /* hung_up_tty_open */
 300         tty_release,    /* hung_up_tty_release */
 301         NULL,           /* hung_up_tty_fsync  */
 302         NULL            /* hung_up_tty_fasync */
 303 };
 304 
 305 void do_tty_hangup(struct tty_struct * tty, struct file_operations *fops)
     /* [previous][next][first][last][top][bottom][index][help] */
 306 {
 307         int i;
 308         struct file * filp;
 309         struct task_struct *p;
 310 
 311         if (!tty)
 312                 return;
 313         for (filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
 314                 if (!filp->f_count)
 315                         continue;
 316                 if (filp->private_data != tty)
 317                         continue;
 318                 if (filp->f_inode && filp->f_inode->i_rdev == CONSOLE_DEV)
 319                         continue;
 320                 if (filp->f_op != &tty_fops)
 321                         continue;
 322                 tty_fasync(filp->f_inode, filp, 0);
 323                 filp->f_op = fops;
 324         }
 325         
 326         if (tty->ldisc.flush_buffer)
 327                 tty->ldisc.flush_buffer(tty);
 328         if (tty->driver.flush_buffer)
 329                 tty->driver.flush_buffer(tty);
 330         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
 331             tty->ldisc.write_wakeup)
 332                 (tty->ldisc.write_wakeup)(tty);
 333         wake_up_interruptible(&tty->write_wait);
 334         wake_up_interruptible(&tty->read_wait);
 335 
 336         /*
 337          * Shutdown the current line discipline, and reset it to
 338          * N_TTY.
 339          */
 340         if (tty->ldisc.num != ldiscs[N_TTY].num) {
 341                 if (tty->ldisc.close)
 342                         (tty->ldisc.close)(tty);
 343                 tty->ldisc = ldiscs[N_TTY];
 344                 tty->termios->c_line = N_TTY;
 345                 if (tty->ldisc.open) {
 346                         i = (tty->ldisc.open)(tty);
 347                         if (i < 0)
 348                                 printk("do_tty_hangup: N_TTY open: error %d\n",
 349                                        -i);
 350                 }
 351         }
 352         
 353         if (tty->session > 0) {
 354                 kill_sl(tty->session,SIGHUP,1);
 355                 kill_sl(tty->session,SIGCONT,1);
 356         }
 357         tty->flags = 0;
 358         tty->session = 0;
 359         tty->pgrp = -1;
 360         for_each_task(p) {
 361                 if (p->tty == tty)
 362                         p->tty = NULL;
 363         }
 364         if (tty->driver.hangup)
 365                 (tty->driver.hangup)(tty);
 366 }
 367 
 368 void tty_hangup(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 369 {
 370 #ifdef TTY_DEBUG_HANGUP
 371         printk("%s hangup...\n", tty_name(tty));
 372 #endif
 373         do_tty_hangup(tty, &hung_up_tty_fops);
 374 }
 375 
 376 void tty_vhangup(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 377 {
 378 #ifdef TTY_DEBUG_HANGUP
 379         printk("%s vhangup...\n", tty_name(tty));
 380 #endif
 381         do_tty_hangup(tty, &hung_up_tty_fops);
 382 }
 383 
 384 int tty_hung_up_p(struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 385 {
 386         return (filp->f_op == &hung_up_tty_fops);
 387 }
 388 
 389 /*
 390  * This function is typically called only by the session leader, when
 391  * it wants to dissassociate itself from its controlling tty.
 392  *
 393  * It performs the following functions:
 394  *      (1)  Sends a SIGHUP and SIGCONT to the foreground process group
 395  *      (2)  Clears the tty from being controlling the session
 396  *      (3)  Clears the controlling tty for all processes in the
 397  *              session group.
 398  */
 399 void disassociate_ctty(int priv)
     /* [previous][next][first][last][top][bottom][index][help] */
 400 {
 401         struct tty_struct *tty = current->tty;
 402         struct task_struct *p;
 403 
 404         if (!tty)
 405                 return;
 406 
 407         if (tty->pgrp > 0) {
 408                 kill_pg(tty->pgrp, SIGHUP, priv);
 409                 kill_pg(tty->pgrp, SIGCONT, priv);
 410         }
 411         tty->session = 0;
 412         tty->pgrp = -1;
 413 
 414         for_each_task(p)
 415                 if (p->session == current->session)
 416                         p->tty = NULL;
 417 }
 418 
 419 /*
 420  * Sometimes we want to wait until a particular VT has been activated. We
 421  * do it in a very simple manner. Everybody waits on a single queue and
 422  * get woken up at once. Those that are satisfied go on with their business,
 423  * while those not ready go back to sleep. Seems overkill to add a wait
 424  * to each vt just for this - usually this does nothing!
 425  */
 426 static struct wait_queue *vt_activate_queue = NULL;
 427 
 428 /*
 429  * Sleeps until a vt is activated, or the task is interrupted. Returns
 430  * 0 if activation, -1 if interrupted.
 431  */
 432 int vt_waitactive(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 433 {
 434         interruptible_sleep_on(&vt_activate_queue);
 435         return (current->signal & ~current->blocked) ? -1 : 0;
 436 }
 437 
 438 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
 439 
 440 extern int kill_proc(int pid, int sig, int priv);
 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 = 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 = 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         retval = -EAGAIN;
 735         if (driver->type == TTY_DRIVER_TYPE_PTY &&
 736             driver->subtype == PTY_TYPE_MASTER &&
 737             *tty_loc && (*tty_loc)->count)
 738                 goto end_init;
 739 repeat:
 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                 if (tty->ldisc.open) {
 817                         retval = (tty->ldisc.open)(tty);
 818                         if (retval < 0)
 819                                 goto end_init;
 820                 }
 821                 tty = NULL;
 822         }
 823         if (driver->type == TTY_DRIVER_TYPE_PTY) {
 824                 if (!*o_tp_loc) {
 825                         *o_tp_loc = o_tp;
 826                         o_tp = NULL;
 827                 }
 828                 if (!*o_ltp_loc) {
 829                         *o_ltp_loc = o_ltp;
 830                         o_ltp = NULL;
 831                 }
 832                 if (!*o_tty_loc) {
 833                         o_tty->termios = *o_tp_loc;
 834                         o_tty->termios_locked = *o_ltp_loc;
 835                         *o_tty_loc = o_tty;
 836                         (*driver->other->refcount)++;
 837                         if (o_tty->ldisc.open) {
 838                                 retval = (o_tty->ldisc.open)(o_tty);
 839                                 if (retval < 0)
 840                                         goto end_init;
 841                         }
 842                         o_tty = NULL;
 843                 }
 844                 (*tty_loc)->link = *o_tty_loc;
 845                 (*o_tty_loc)->link = *tty_loc;
 846         }
 847         (*tty_loc)->count++;
 848         (*tty_loc)->driver = *driver;
 849         if (driver->type == TTY_DRIVER_TYPE_PTY &&
 850             driver->subtype == PTY_TYPE_MASTER)
 851                 (*o_tty_loc)->count++;
 852         *ret_tty = *tty_loc;
 853         retval = 0;
 854 end_init:
 855         if (tty)
 856                 free_page((unsigned long) tty);
 857         if (o_tty)
 858                 free_page((unsigned long) o_tty);
 859         if (tp)
 860                 kfree_s(tp, sizeof(struct termios));
 861         if (o_tp)
 862                 kfree_s(o_tp, sizeof(struct termios));
 863         if (ltp)
 864                 kfree_s(ltp, sizeof(struct termios));
 865         if (o_ltp)
 866                 kfree_s(o_ltp, sizeof(struct termios));
 867         return retval;
 868 }
 869 
 870 /*
 871  * Even releasing the tty structures is a tricky business.. We have
 872  * to be very careful that the structures are all released at the
 873  * same time, as interrupts might otherwise get the wrong pointers.
 874  */
 875 static void release_dev(struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 876 {
 877         struct tty_struct *tty, *o_tty;
 878         struct termios *tp, *o_tp, *ltp, *o_ltp;
 879         struct task_struct **p;
 880         int     idx;
 881         
 882 
 883         tty = filp->private_data;
 884         if (tty_paranoia_check(tty, filp->f_inode->i_rdev, "release_dev"))
 885                 return;
 886 
 887         tty_fasync(filp->f_inode, filp, 0);
 888 
 889         tp = tty->termios;
 890         ltp = tty->termios_locked;
 891 
 892         idx = MINOR(tty->device) - tty->driver.minor_start;
 893 #ifdef TTY_PARANOIA_CHECK
 894         if (idx < 0 || idx >= tty->driver.num) {
 895                 printk("release_dev: bad idx when trying to free (%d, %d)\n",
 896                        MAJOR(tty->device), MINOR(tty->device));
 897                 return;
 898         }
 899         if (tty != tty->driver.table[idx]) {
 900                 printk("release_dev: driver.table[%d] not tty for (%d, %d)\n",
 901                        idx, MAJOR(tty->device), MINOR(tty->device));
 902                 return;
 903         }
 904         if (tp != tty->driver.termios[idx]) {
 905                 printk("release_dev: driver.termios[%d] not termios for (%d, %d)\n",
 906                        idx, MAJOR(tty->device), MINOR(tty->device));
 907                 return;
 908         }
 909         if (ltp != tty->driver.termios_locked[idx]) {
 910                 printk("release_dev: driver.termios_locked[%d] not termios_locked for (%d, %d)\n",
 911                        idx, MAJOR(tty->device), MINOR(tty->device));
 912                 return;
 913         }
 914 #endif
 915 
 916 #ifdef TTY_DEBUG_HANGUP
 917         printk("release_dev of %s (tty count=%d)...", tty_name(tty),
 918                tty->count);
 919 #endif
 920 
 921         o_tty = tty->link;
 922         o_tp = (o_tty) ? o_tty->termios : NULL;
 923         o_ltp = (o_tty) ? o_tty->termios_locked : NULL;
 924 
 925 #ifdef TTY_PARANOIA_CHECK
 926         if (tty->driver.other) {
 927                 if (o_tty != tty->driver.other->table[idx]) {
 928                         printk("release_dev: other->table[%d] not o_tty for (%d, %d)\n",
 929                                idx, MAJOR(tty->device), MINOR(tty->device));
 930                         return;
 931                 }
 932                 if (o_tp != tty->driver.other->termios[idx]) {
 933                         printk("release_dev: other->termios[%d] not o_termios for (%d, %d)\n",
 934                                idx, MAJOR(tty->device), MINOR(tty->device));
 935                         return;
 936                 }
 937                 if (o_ltp != tty->driver.other->termios_locked[idx]) {
 938                         printk("release_dev: other->termios_locked[%d] not o_termios_locked for (%d, %d)\n",
 939                                idx, MAJOR(tty->device), MINOR(tty->device));
 940                         return;
 941                 }
 942 
 943                 if (o_tty->link != tty) {
 944                         printk("release_dev: bad pty pointers\n");
 945                         return;
 946                 }
 947         }
 948 #endif
 949         
 950         if (tty->driver.close)
 951                 tty->driver.close(tty, filp);
 952         if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
 953             tty->driver.subtype == PTY_TYPE_MASTER) {
 954                 if (--tty->link->count < 0) {
 955                         printk("release_dev: bad pty slave count (%d) for %s\n",
 956                                tty->count, tty_name(tty));
 957                         tty->link->count = 0;
 958                 }
 959         }
 960         if (--tty->count < 0) {
 961                 printk("release_dev: bad tty->count (%d) for %s\n",
 962                        tty->count, tty_name(tty));
 963                 tty->count = 0;
 964         }
 965         if (tty->count)
 966                 return;
 967         
 968         /*
 969          * Make sure there aren't any processes that still think this
 970          * tty is their controlling tty.
 971          */
 972         for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
 973                 if ((*p) && (*p)->tty == tty)
 974                 (*p)->tty = NULL;
 975         }
 976 
 977         if (o_tty) {
 978                 if (o_tty->count)
 979                         return;
 980                 tty->driver.other->table[idx] = NULL;
 981                 tty->driver.other->termios[idx] = NULL;
 982                 tty->driver.other->termios_locked[idx] = NULL;
 983         }
 984         
 985 #ifdef TTY_DEBUG_HANGUP
 986         printk("freeing tty structure...");
 987 #endif
 988 
 989         /*
 990          * Shutdown the current line discipline, and reset it to
 991          * N_TTY.
 992          */
 993         if (tty->ldisc.close)
 994                 (tty->ldisc.close)(tty);
 995         tty->ldisc = ldiscs[N_TTY];
 996         tty->termios->c_line = N_TTY;
 997         
 998         tty->driver.table[idx] = NULL;
 999         if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1000                 tty->driver.termios[idx] = NULL;
1001                 tty->driver.termios_locked[idx] = NULL;
1002                 kfree_s(tp, sizeof(struct termios));
1003                 kfree_s(ltp, sizeof(struct termios));
1004         }
1005         if (tty == redirect || o_tty == redirect)
1006                 redirect = NULL;
1007         /*
1008          * Make sure that the tty's task queue isn't activated.  If it
1009          * is, take it out of the linked list.
1010          */
1011         cli();
1012         if (tty->flip.tqueue.sync) {
1013                 struct tq_struct *tq, *prev;
1014 
1015                 for (tq=tq_timer, prev=0; tq; prev=tq, tq=tq->next) {
1016                         if (tq == &tty->flip.tqueue) {
1017                                 if (prev)
1018                                         prev->next = tq->next;
1019                                 else
1020                                         tq_timer = tq->next;
1021                                 break;
1022                         }
1023                 }
1024         }
1025         sti();
1026         tty->magic = 0;
1027         (*tty->driver.refcount)--;
1028         free_page((unsigned long) tty);
1029         if (o_tty) {
1030                 o_tty->magic = 0;
1031                 (*o_tty->driver.refcount)--;
1032                 free_page((unsigned long) o_tty);
1033         }
1034         if (o_tp)
1035                 kfree_s(o_tp, sizeof(struct termios));
1036         if (o_ltp)
1037                 kfree_s(o_ltp, sizeof(struct termios));
1038 }
1039 
1040 /*
1041  * tty_open and tty_release keep up the tty count that contains the
1042  * number of opens done on a tty. We cannot use the inode-count, as
1043  * different inodes might point to the same tty.
1044  *
1045  * Open-counting is needed for pty masters, as well as for keeping
1046  * track of serial lines: DTR is dropped when the last close happens.
1047  * (This is not done solely through tty->count, now.  - Ted 1/27/92)
1048  *
1049  * The termios state of a pty is reset on first open so that
1050  * settings don't persist across reuse.
1051  */
1052 static int tty_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1053 {
1054         struct tty_struct *tty;
1055         int minor;
1056         int noctty, retval;
1057         dev_t device;
1058 
1059 retry_open:
1060         noctty = filp->f_flags & O_NOCTTY;
1061         device = inode->i_rdev;
1062         if (device == TTY_DEV) {
1063                 if (!current->tty)
1064                         return -ENXIO;
1065                 device = current->tty->device;
1066                 /* noctty = 1; */
1067         }
1068         if (device == CONSOLE_DEV) {
1069                 device = MKDEV(TTY_MAJOR, fg_console+1);
1070                 noctty = 1;
1071         }
1072         minor = MINOR(device);
1073         
1074         retval = init_dev(device, &tty);
1075         filp->private_data = tty;
1076         if (retval)
1077                 return retval;
1078         if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1079             tty->driver.subtype == PTY_TYPE_MASTER)
1080                 noctty = 1;
1081 #ifdef TTY_DEBUG_HANGUP
1082         printk("opening %s...", tty_name(tty));
1083 #endif
1084         if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1085                 retval = -EBUSY;
1086         else if (tty->driver.open)
1087                 retval = tty->driver.open(tty, filp);
1088         else
1089                 retval = -ENODEV;
1090 
1091         if (retval) {
1092 #ifdef TTY_DEBUG_HANGUP
1093                 printk("error %d in opening %s...", retval, tty_name(tty));
1094 #endif
1095 
1096                 release_dev(filp);
1097                 if (retval != -ERESTARTSYS)
1098                         return retval;
1099                 if (current->signal & ~current->blocked)
1100                         return retval;
1101                 schedule();
1102                 /*
1103                  * Need to reset f_op in case a hangup happened.
1104                  */
1105                 filp->f_op = &tty_fops;
1106                 goto retry_open;
1107         }
1108         if (!noctty &&
1109             current->leader &&
1110             !current->tty &&
1111             tty->session == 0) {
1112                 current->tty = tty;
1113                 tty->session = current->session;
1114                 tty->pgrp = current->pgrp;
1115         }
1116         return 0;
1117 }
1118 
1119 /*
1120  * Note that releasing a pty master also releases the child, so
1121  * we have to make the redirection checks after that and on both
1122  * sides of a pty.
1123  */
1124 static void tty_release(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1125 {
1126         release_dev(filp);
1127 }
1128 
1129 static int tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1130 {
1131         struct tty_struct * tty;
1132 
1133         tty = filp->private_data;
1134         if (tty_paranoia_check(tty, inode->i_rdev, "tty_select"))
1135                 return 0;
1136 
1137         if (tty->ldisc.select)
1138                 return (tty->ldisc.select)(tty, inode, filp, sel_type, wait);
1139         return 0;
1140 }
1141 
1142 static int tty_fasync(struct inode * inode, struct file * filp, int on)
     /* [previous][next][first][last][top][bottom][index][help] */
1143 {
1144         struct tty_struct * tty;
1145         struct fasync_struct *fa, *prev;
1146 
1147         tty = filp->private_data;
1148         if (tty_paranoia_check(tty, inode->i_rdev, "tty_fasync"))
1149                 return 0;
1150 
1151         for (fa = tty->fasync, prev = 0; fa; prev= fa, fa = fa->fa_next) {
1152                 if (fa->fa_file == filp)
1153                         break;
1154         }
1155 
1156         if (on) {
1157                 if (fa)
1158                         return 0;
1159                 fa = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1160                 if (!fa)
1161                         return -ENOMEM;
1162                 fa->magic = FASYNC_MAGIC;
1163                 fa->fa_file = filp;
1164                 fa->fa_next = tty->fasync;
1165                 tty->fasync = fa;
1166                 if (!tty->read_wait)
1167                         tty->minimum_to_wake = 1;
1168         } else {
1169                 if (!fa)
1170                         return 0;
1171                 if (prev)
1172                         prev->fa_next = fa->fa_next;
1173                 else
1174                         tty->fasync = fa->fa_next;
1175                 kfree_s(fa, sizeof(struct fasync_struct));
1176                 if (!tty->fasync && !tty->read_wait)
1177                         tty->minimum_to_wake = N_TTY_BUF_SIZE;
1178         }
1179         return 0;       
1180 }
1181 
1182 /*
1183  * XXX does anyone use this anymore?!?
1184  */
1185 static int do_get_ps_info(int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1186 {
1187         struct tstruct {
1188                 int flag;
1189                 int present[NR_TASKS];
1190                 struct task_struct tasks[NR_TASKS];
1191         };
1192         struct tstruct *ts = (struct tstruct *)arg;
1193         struct task_struct **p;
1194         char *c, *d;
1195         int i, n = 0;
1196         
1197         i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct tstruct));
1198         if (i)
1199                 return i;
1200         for (p = &FIRST_TASK ; p <= &LAST_TASK ; p++, n++)
1201                 if (*p)
1202                 {
1203                         c = (char *)(*p);
1204                         d = (char *)(ts->tasks+n);
1205                         for (i=0 ; i<sizeof(struct task_struct) ; i++)
1206                                 put_fs_byte(*c++, d++);
1207                         put_fs_long(1, (unsigned long *)(ts->present+n));
1208                 }
1209                 else    
1210                         put_fs_long(0, (unsigned long *)(ts->present+n));
1211         return(0);                      
1212 }
1213 
1214 static int tty_ioctl(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1215                      unsigned int cmd, unsigned long arg)
1216 {
1217         int     retval;
1218         struct tty_struct * tty;
1219         struct tty_struct * real_tty;
1220         struct winsize tmp_ws;
1221         pid_t pgrp;
1222         unsigned char   ch;
1223         char    mbz = 0;
1224         
1225         tty = file->private_data;
1226         if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1227                 return -EINVAL;
1228 
1229         if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1230             tty->driver.subtype == PTY_TYPE_MASTER)
1231                 real_tty = tty->link;
1232         else
1233                 real_tty = tty;
1234 
1235         switch (cmd) {
1236                 case TIOCSTI:
1237                         if ((current->tty != tty) && !suser())
1238                                 return -EPERM;
1239                         ch = get_fs_byte((char *) arg);
1240                         tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1241                         return 0;
1242                 case TIOCGWINSZ:
1243                         retval = verify_area(VERIFY_WRITE, (void *) arg,
1244                                              sizeof (struct winsize));
1245                         if (retval)
1246                                 return retval;
1247                         memcpy_tofs((struct winsize *) arg, &tty->winsize,
1248                                     sizeof (struct winsize));
1249                         return 0;
1250                 case TIOCSWINSZ:
1251                         memcpy_fromfs(&tmp_ws, (struct winsize *) arg,
1252                                       sizeof (struct winsize));
1253                         if (memcmp(&tmp_ws, &tty->winsize,
1254                                    sizeof(struct winsize))) {
1255                                 if (tty->pgrp > 0)
1256                                         kill_pg(tty->pgrp, SIGWINCH, 1);
1257                                 if ((real_tty->pgrp != tty->pgrp) &&
1258                                     (real_tty->pgrp > 0))
1259                                         kill_pg(real_tty->pgrp, SIGWINCH, 1);
1260                         }
1261                         tty->winsize = tmp_ws;
1262                         real_tty->winsize = tmp_ws;
1263                         return 0;
1264                 case TIOCCONS:
1265                         if (tty->driver.type == TTY_DRIVER_TYPE_CONSOLE) {
1266                                 if (!suser())
1267                                         return -EPERM;
1268                                 redirect = NULL;
1269                                 return 0;
1270                         }
1271                         if (redirect)
1272                                 return -EBUSY;
1273                         redirect = real_tty;
1274                         return 0;
1275                 case FIONBIO:
1276                         arg = get_fs_long((unsigned long *) arg);
1277                         if (arg)
1278                                 file->f_flags |= O_NONBLOCK;
1279                         else
1280                                 file->f_flags &= ~O_NONBLOCK;
1281                         return 0;
1282                 case TIOCEXCL:
1283                         set_bit(TTY_EXCLUSIVE, &tty->flags);
1284                         return 0;
1285                 case TIOCNXCL:
1286                         clear_bit(TTY_EXCLUSIVE, &tty->flags);
1287                         return 0;
1288                 case TIOCNOTTY:
1289                         if (current->tty != tty)
1290                                 return -ENOTTY;
1291                         if (current->leader)
1292                                 disassociate_ctty(0);
1293                         current->tty = NULL;
1294                         return 0;
1295                 case TIOCSCTTY:
1296                         if (current->leader &&
1297                             (current->session == tty->session))
1298                                 return 0;
1299                         /*
1300                          * The process must be a session leader and
1301                          * not have a controlling tty already.
1302                          */
1303                         if (!current->leader || current->tty)
1304                                 return -EPERM;
1305                         if (tty->session > 0) {
1306                                 /*
1307                                  * This tty is already the controlling
1308                                  * tty for another session group!
1309                                  */
1310                                 if ((arg == 1) && suser()) {
1311                                         /*
1312                                          * Steal it away
1313                                          */
1314                                         struct task_struct *p;
1315 
1316                                         for_each_task(p)
1317                                                 if (p->tty == tty)
1318                                                         p->tty = NULL;
1319                                 } else
1320                                         return -EPERM;
1321                         }
1322                         current->tty = tty;
1323                         tty->session = current->session;
1324                         tty->pgrp = current->pgrp;
1325                         return 0;
1326                 case TIOCGPGRP:
1327                         /*
1328                          * (tty == real_tty) is a cheap way of
1329                          * testing if the tty is NOT a master pty.
1330                          */
1331                         if (tty == real_tty && current->tty != real_tty)
1332                                 return -ENOTTY;
1333                         retval = verify_area(VERIFY_WRITE, (void *) arg,
1334                                              sizeof (pid_t));
1335                         if (retval)
1336                                 return retval;
1337                         put_fs_long(real_tty->pgrp, (pid_t *) arg);
1338                         return 0;
1339                 case TIOCSPGRP:
1340                         retval = tty_check_change(real_tty);
1341                         if (retval)
1342                                 return retval;
1343                         if (!current->tty ||
1344                             (current->tty != real_tty) ||
1345                             (real_tty->session != current->session))
1346                                 return -ENOTTY;
1347                         pgrp = get_fs_long((pid_t *) arg);
1348                         if (pgrp < 0)
1349                                 return -EINVAL;
1350                         if (session_of_pgrp(pgrp) != current->session)
1351                                 return -EPERM;
1352                         real_tty->pgrp = pgrp;
1353                         return 0;
1354                 case TIOCGETD:
1355                         retval = verify_area(VERIFY_WRITE, (void *) arg,
1356                                              sizeof (unsigned long));
1357                         if (retval)
1358                                 return retval;
1359                         put_fs_long(tty->ldisc.num, (unsigned long *) arg);
1360                         return 0;
1361                 case TIOCSETD:
1362                         retval = tty_check_change(tty);
1363                         if (retval)
1364                                 return retval;
1365                         arg = get_fs_long((unsigned long *) arg);
1366                         return tty_set_ldisc(tty, arg);
1367                 case TIOCLINUX:
1368                         switch (get_fs_byte((char *)arg))
1369                         {
1370                                 case 0: 
1371                                         return do_screendump(arg);
1372                                 case 1:
1373                                         printk("Deprecated TIOCLINUX (1) ioctl\n");
1374                                         return do_get_ps_info(arg);
1375 #ifdef CONFIG_SELECTION
1376                                 case 2:
1377                                         return set_selection(arg);
1378                                 case 3:
1379                                         return paste_selection(tty);
1380                                 case 4:
1381                                         unblank_screen();
1382                                         return 0;
1383 #endif /* CONFIG_SELECTION */
1384                                 default: 
1385                                         return -EINVAL;
1386                         }
1387                 default:
1388                         if (tty->driver.ioctl) {
1389                                 retval = (tty->driver.ioctl)(tty, file,
1390                                                              cmd, arg);
1391                                 if (retval != -ENOIOCTLCMD)
1392                                         return retval;
1393                         }
1394                         if (tty->ldisc.ioctl) {
1395                                 retval = (tty->ldisc.ioctl)(tty, file,
1396                                                             cmd, arg);
1397                                 if (retval != -ENOIOCTLCMD)
1398                                         return retval;
1399                         }
1400                         return -EINVAL;
1401                 }
1402 }
1403 
1404 
1405 /*
1406  * This implements the "Secure Attention Key" ---  the idea is to
1407  * prevent trojan horses by killing all processes associated with this
1408  * tty when the user hits the "Secure Attention Key".  Required for
1409  * super-paranoid applications --- see the Orange Book for more details.
1410  * 
1411  * This code could be nicer; ideally it should send a HUP, wait a few
1412  * seconds, then send a INT, and then a KILL signal.  But you then
1413  * have to coordinate with the init process, since all processes associated
1414  * with the current tty must be dead before the new getty is allowed
1415  * to spawn.
1416  */
1417 void do_SAK( struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1418 {
1419 #ifdef TTY_SOFT_SAK
1420         tty_hangup(tty);
1421 #else
1422         struct task_struct **p;
1423         int session = tty->session;
1424         int             i;
1425         struct file     *filp;
1426         
1427         if (tty->ldisc.flush_buffer)
1428                 tty->ldisc.flush_buffer(tty);
1429         if (tty->driver.flush_buffer)
1430                 tty->driver.flush_buffer(tty);
1431         for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1432                 if (!(*p))
1433                         continue;
1434                 if (((*p)->tty == tty) ||
1435                     ((session > 0) && ((*p)->session == session)))
1436                         send_sig(SIGKILL, *p, 1);
1437                 else {
1438                         for (i=0; i < NR_OPEN; i++) {
1439                                 filp = (*p)->files->fd[i];
1440                                 if (filp && (filp->f_op == &tty_fops) &&
1441                                     (filp->private_data == tty)) {
1442                                         send_sig(SIGKILL, *p, 1);
1443                                         break;
1444                                 }
1445                         }
1446                 }
1447         }
1448 #endif
1449 }
1450 
1451 /*
1452  * This routine is called out of the software interrupt to flush data
1453  * from the flip buffer to the line discpline.
1454  */
1455 static void flush_to_ldisc(void *private)
     /* [previous][next][first][last][top][bottom][index][help] */
1456 {
1457         struct tty_struct *tty = private;
1458         unsigned char   *cp;
1459         char            *fp;
1460         int             count;
1461 
1462         if (tty->flip.buf_num) {
1463                 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1464                 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1465                 tty->flip.buf_num = 0;
1466 
1467                 cli();
1468                 tty->flip.char_buf_ptr = tty->flip.char_buf;
1469                 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1470         } else {
1471                 cp = tty->flip.char_buf;
1472                 fp = tty->flip.flag_buf;
1473                 tty->flip.buf_num = 1;
1474 
1475                 cli();
1476                 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1477                 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1478         }
1479         count = tty->flip.count;
1480         tty->flip.count = 0;
1481         sti();
1482         
1483 #if 0
1484         if (count > tty->max_flip_cnt)
1485                 tty->max_flip_cnt = count;
1486 #endif
1487         tty->ldisc.receive_buf(tty, cp, fp, count);
1488 }
1489 
1490 /*
1491  * This subroutine initializes a tty structure.
1492  */
1493 static void initialize_tty_struct(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1494 {
1495         memset(tty, 0, sizeof(struct tty_struct));
1496         tty->magic = TTY_MAGIC;
1497         tty->ldisc = ldiscs[N_TTY];
1498         tty->pgrp = -1;
1499         tty->flip.char_buf_ptr = tty->flip.char_buf;
1500         tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1501         tty->flip.tqueue.routine = flush_to_ldisc;
1502         tty->flip.tqueue.data = tty;
1503 }
1504 
1505 /*
1506  * The default put_char routine if the driver did not define one.
1507  */
1508 void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
1509 {
1510         tty->driver.write(tty, 0, &ch, 1);
1511 }
1512 
1513 /*
1514  * Called by a tty driver to register itself.
1515  */
1516 int tty_register_driver(struct tty_driver *driver)
     /* [previous][next][first][last][top][bottom][index][help] */
1517 {
1518         if (driver->flags & TTY_DRIVER_INSTALLED)
1519                 return 0;
1520         /*
1521          * XXX need to check to see if major device already
1522          * registered, and then handle error checking.
1523          */
1524         (void) register_chrdev(driver->major, driver->name, &tty_fops);
1525 
1526         if (!driver->put_char)
1527                 driver->put_char = tty_default_put_char;
1528         
1529         driver->prev = 0;
1530         driver->next = tty_drivers;
1531         tty_drivers->prev = driver;
1532         tty_drivers = driver;
1533         return 0;
1534 }
1535 
1536 long tty_init(long kmem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1537 {
1538         if (sizeof(struct tty_struct) > PAGE_SIZE)
1539                 panic("size of tty structure > PAGE_SIZE!");
1540         if (register_chrdev(TTY_MAJOR,"tty",&tty_fops))
1541                 panic("unable to get major %d for tty device", TTY_MAJOR);
1542         if (register_chrdev(TTYAUX_MAJOR,"tty",&tty_fops))
1543                 panic("unable to get major %d for tty device", TTYAUX_MAJOR);
1544         tty_drivers = 0;
1545 
1546         /* Setup the default TTY line discipline. */
1547         memset(ldiscs, 0, sizeof(ldiscs));
1548         (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1549 
1550         /*
1551          * Set up the standard termios.  Individual tty drivers may 
1552          * deviate from this; this is used as a template.
1553          */
1554         memset(&tty_std_termios, 0, sizeof(struct termios));
1555         memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
1556         tty_std_termios.c_iflag = ICRNL | IXON;
1557         tty_std_termios.c_oflag = OPOST | ONLCR;
1558         tty_std_termios.c_cflag = B38400 | CS8 | CREAD;
1559         tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
1560                 ECHOCTL | ECHOKE | IEXTEN;
1561         
1562         kmem_start = con_init(kmem_start);
1563         kmem_start = kbd_init(kmem_start);
1564         kmem_start = rs_init(kmem_start);
1565         kmem_start = pty_init(kmem_start);
1566         return kmem_start;
1567 }

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