root/drivers/char/tty_io.c

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

DEFINITIONS

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

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

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