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

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