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

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