root/drivers/char/tty_io.c

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

DEFINITIONS

This source file includes following definitions.
  1. tty_register_ldisc
  2. put_tty_queue
  3. get_tty_queue
  4. tty_read_raw_data
  5. tty_write_flush
  6. tty_read_flush
  7. hung_up_tty_read
  8. hung_up_tty_write
  9. hung_up_tty_select
  10. hung_up_tty_ioctl
  11. tty_lseek
  12. do_tty_hangup
  13. tty_hangup
  14. tty_vhangup
  15. tty_hung_up_p
  16. disassociate_ctty
  17. vt_waitactive
  18. complete_change_console
  19. change_console
  20. wait_for_keypress
  21. stop_tty
  22. start_tty
  23. opost
  24. echo_char
  25. eraser
  26. isig
  27. copy_to_cooked
  28. is_ignored
  29. input_available_p
  30. read_chan
  31. write_chan
  32. tty_read
  33. tty_write
  34. init_dev
  35. release_dev
  36. tty_open
  37. tty_release
  38. tty_select
  39. normal_select
  40. do_SAK
  41. tty_write_data
  42. tty_bh_routine
  43. initialize_tty_struct
  44. initialize_termios
  45. tty_init

   1 /*
   2  *  linux/kernel/tty_io.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
   9  * or rs-channels. It also implements echoing, cooked mode etc.
  10  *
  11  * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
  12  *
  13  * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
  14  * tty_struct and tty_queue structures.  Previously there was a array
  15  * of 256 tty_struct's which was statically allocated, and the
  16  * tty_queue structures were allocated at boot time.  Both are now
  17  * dynamically allocated only when the tty is open.
  18  *
  19  * Also restructured routines so that there is more of a separation
  20  * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
  21  * the low-level tty routines (serial.c, pty.c, console.c).  This
  22  * makes for cleaner and more compact code.  -TYT, 9/17/92 
  23  *
  24  * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
  25  * which can be dynamically activated and de-activated by the line
  26  * discipline handling modules (like SLIP).
  27  *
  28  * NOTE: pay no attention to the line discpline code (yet); its
  29  * interface is still subject to change in this version...
  30  * -- TYT, 1/31/92
  31  *
  32  * Added functionality to the OPOST tty handling.  No delays, but all
  33  * other bits should be there.
  34  *      -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
  35  *
  36  * Rewrote canonical mode and added more termios flags.
  37  *      -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
  38  */
  39 
  40 #include <linux/types.h>
  41 #include <linux/major.h>
  42 #include <linux/errno.h>
  43 #include <linux/signal.h>
  44 #include <linux/fcntl.h>
  45 #include <linux/sched.h>
  46 #include <linux/tty.h>
  47 #include <linux/timer.h>
  48 #include <linux/ctype.h>
  49 #include <linux/kd.h>
  50 #include <linux/mm.h>
  51 #include <linux/string.h>
  52 #include <linux/keyboard.h>
  53 #include <linux/malloc.h>
  54 
  55 #include <asm/segment.h>
  56 #include <asm/system.h>
  57 #include <asm/bitops.h>
  58 
  59 #include "vt_kern.h"
  60 
  61 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
  62 
  63 #define MAX_TTYS 256
  64 
  65 struct tty_struct *tty_table[MAX_TTYS];
  66 struct termios *tty_termios[MAX_TTYS];  /* We need to keep the termios state */
  67                                         /* around, even when a tty is closed */
  68 struct termios *termios_locked[MAX_TTYS]; /* Bitfield of locked termios flags*/
  69 struct tty_ldisc ldiscs[NR_LDISCS];     /* line disc dispatch table     */
  70 int tty_check_write[MAX_TTYS/32];       /* bitfield for the bh handler */
  71 
  72 /*
  73  * fg_console is the current virtual console,
  74  * redirect is the pseudo-tty that console output
  75  * is redirected to if asked by TIOCCONS.
  76  */
  77 int fg_console = 0;
  78 struct tty_struct * redirect = NULL;
  79 struct wait_queue * keypress_wait = NULL;
  80 
  81 static void initialize_tty_struct(int line, struct tty_struct *tty);
  82 static void initialize_termios(int line, struct termios *tp);
  83 
  84 static int tty_read(struct inode *, struct file *, char *, int);
  85 static int tty_write(struct inode *, struct file *, char *, int);
  86 static int tty_select(struct inode *, struct file *, int, select_table *);
  87 static int tty_open(struct inode *, struct file *);
  88 static void tty_release(struct inode *, struct file *);
  89 
  90 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92         if (disc < N_TTY || disc >= NR_LDISCS)
  93                 return -EINVAL;
  94         
  95         if (new_ldisc) {
  96                 ldiscs[disc] = *new_ldisc;
  97                 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
  98         } else
  99                 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
 100         
 101         return 0;
 102 }
 103 
 104 void put_tty_queue(unsigned char c, struct tty_queue * queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 105 {
 106         int head;
 107         unsigned long flags;
 108 
 109         save_flags(flags);
 110         cli();
 111         head = (queue->head + 1) & (TTY_BUF_SIZE-1);
 112         if (head != queue->tail) {
 113                 queue->buf[queue->head] = c;
 114                 queue->head = head;
 115         }
 116         restore_flags(flags);
 117 }
 118 
 119 int get_tty_queue(struct tty_queue * queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121         int result = -1;
 122         unsigned long flags;
 123 
 124         save_flags(flags);
 125         cli();
 126         if (queue->tail != queue->head) {
 127                 result = queue->buf[queue->tail];
 128                 INC(queue->tail);
 129         }
 130         restore_flags(flags);
 131         return result;
 132 }
 133 
 134 /*
 135  * This routine copies out a maximum of buflen characters from the
 136  * read_q; it is a convenience for line disciplines so they can grab a
 137  * large block of data without calling get_tty_char directly.  It
 138  * returns the number of characters actually read. Return terminates
 139  * if an error character is read from the queue and the return value
 140  * is negated.
 141  */
 142 int tty_read_raw_data(struct tty_struct *tty, unsigned char *bufp, int buflen)
     /* [previous][next][first][last][top][bottom][index][help] */
 143 {
 144         int     result = 0;
 145         unsigned char   *p = bufp;
 146         unsigned long flags;
 147         int head, tail;
 148         int ok = 1;
 149 
 150         save_flags(flags);
 151         cli();
 152         tail = tty->read_q.tail;
 153         head = tty->read_q.head;
 154         while ((result < buflen) && (tail!=head) && ok) {
 155                 ok = !clear_bit (tail, &tty->readq_flags);
 156                 *p++ =  tty->read_q.buf[tail++];
 157                 tail &= TTY_BUF_SIZE-1;
 158                 result++;
 159         }
 160         tty->read_q.tail = tail;
 161         restore_flags(flags);
 162         return (ok) ? result : -result;
 163 }
 164 
 165 
 166 void tty_write_flush(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168         if (!tty->write || EMPTY(&tty->write_q))
 169                 return;
 170         if (set_bit(TTY_WRITE_BUSY,&tty->flags))
 171                 return;
 172         tty->write(tty);
 173         if (!clear_bit(TTY_WRITE_BUSY,&tty->flags))
 174                 printk("tty_write_flush: bit already cleared\n");
 175 }
 176 
 177 void tty_read_flush(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 178 {
 179         if (!tty || EMPTY(&tty->read_q))
 180                 return;
 181         if (set_bit(TTY_READ_BUSY, &tty->flags))
 182                 return;
 183         ldiscs[tty->disc].handler(tty);
 184         if (!clear_bit(TTY_READ_BUSY, &tty->flags))
 185                 printk("tty_read_flush: bit already cleared\n");
 186 }
 187 
 188 static int hung_up_tty_read(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 189 {
 190         return 0;
 191 }
 192 
 193 static int hung_up_tty_write(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 194 {
 195         return -EIO;
 196 }
 197 
 198 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] */
 199 {
 200         return 1;
 201 }
 202 
 203 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 204                              unsigned int cmd, unsigned long arg)
 205 {
 206         return -EIO;
 207 }
 208 
 209 static int tty_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
     /* [previous][next][first][last][top][bottom][index][help] */
 210 {
 211         return -ESPIPE;
 212 }
 213 
 214 static struct file_operations tty_fops = {
 215         tty_lseek,
 216         tty_read,
 217         tty_write,
 218         NULL,           /* tty_readdir */
 219         tty_select,
 220         tty_ioctl,
 221         NULL,           /* tty_mmap */
 222         tty_open,
 223         tty_release
 224 };
 225 
 226 static struct file_operations hung_up_tty_fops = {
 227         tty_lseek,
 228         hung_up_tty_read,
 229         hung_up_tty_write,
 230         NULL,           /* hung_up_tty_readdir */
 231         hung_up_tty_select,
 232         hung_up_tty_ioctl,
 233         NULL,           /* hung_up_tty_mmap */
 234         NULL,           /* hung_up_tty_open */
 235         tty_release     /* hung_up_tty_release */
 236 };
 237 
 238 void do_tty_hangup(struct tty_struct * tty, struct file_operations *fops)
     /* [previous][next][first][last][top][bottom][index][help] */
 239 {
 240         int i;
 241         struct file * filp;
 242         struct task_struct *p;
 243         int dev;
 244 
 245         if (!tty)
 246                 return;
 247         dev = MKDEV(TTY_MAJOR,tty->line);
 248         for (filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
 249                 if (!filp->f_count)
 250                         continue;
 251                 if (filp->f_rdev != dev)
 252                         continue;
 253                 if (filp->f_inode && filp->f_inode->i_rdev == CONSOLE_DEV)
 254                         continue;
 255                 if (filp->f_op != &tty_fops)
 256                         continue;
 257                 filp->f_op = fops;
 258         }
 259         flush_input(tty);
 260         flush_output(tty);
 261         wake_up_interruptible(&tty->secondary.proc_list);
 262         if (tty->session > 0)
 263                 kill_sl(tty->session,SIGHUP,1);
 264         tty->session = 0;
 265         tty->pgrp = -1;
 266         for_each_task(p) {
 267                 if (p->tty == tty->line)
 268                         p->tty = -1;
 269         }
 270         if (tty->hangup)
 271                 (tty->hangup)(tty);
 272 }
 273 
 274 void tty_hangup(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 275 {
 276 #ifdef TTY_DEBUG_HANGUP
 277         printk("tty%d hangup...\n", tty->line);
 278 #endif
 279         do_tty_hangup(tty, &hung_up_tty_fops);
 280 }
 281 
 282 void tty_vhangup(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 283 {
 284 #ifdef TTY_DEBUG_HANGUP
 285         printk("tty%d vhangup...\n", tty->line);
 286 #endif
 287         do_tty_hangup(tty, &hung_up_tty_fops);
 288 }
 289 
 290 int tty_hung_up_p(struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 291 {
 292         return (filp->f_op == &hung_up_tty_fops);
 293 }
 294 
 295 /*
 296  * This function is typically called only by the session leader, when
 297  * it wants to dissassociate itself from its controlling tty.
 298  *
 299  * It performs the following functions:
 300  *      (1)  Sends a SIGHUP to the foreground process group
 301  *      (2)  Clears the tty from being controlling the session
 302  *      (3)  Clears the controlling tty for all processes in the
 303  *              session group.
 304  */
 305 void disassociate_ctty(int priv)
     /* [previous][next][first][last][top][bottom][index][help] */
 306 {
 307         struct tty_struct *tty;
 308         struct task_struct *p;
 309 
 310         if (current->tty >= 0) {
 311                 tty = tty_table[current->tty];
 312                 if (tty) {
 313                         if (tty->pgrp > 0)
 314                                 kill_pg(tty->pgrp, SIGHUP, priv);
 315                         tty->session = 0;
 316                         tty->pgrp = -1;
 317                 } else
 318                         printk("disassociate_ctty: ctty is NULL?!?");
 319         }
 320 
 321         for_each_task(p)
 322                 if (p->session == current->session)
 323                         p->tty = -1;
 324 }
 325 
 326 /*
 327  * Sometimes we want to wait until a particular VT has been activated. We
 328  * do it in a very simple manner. Everybody waits on a single queue and
 329  * get woken up at once. Those that are satisfied go on with their business,
 330  * while those not ready go back to sleep. Seems overkill to add a wait
 331  * to each vt just for this - usually this does nothing!
 332  */
 333 static struct wait_queue *vt_activate_queue = NULL;
 334 
 335 /*
 336  * Sleeps until a vt is activated, or the task is interrupted. Returns
 337  * 0 if activation, -1 if interrupted.
 338  */
 339 int vt_waitactive(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 340 {
 341         interruptible_sleep_on(&vt_activate_queue);
 342         return (current->signal & ~current->blocked) ? -1 : 0;
 343 }
 344 
 345 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
 346 
 347 extern int kill_proc(int pid, int sig, int priv);
 348 
 349 /*
 350  * Performs the back end of a vt switch
 351  */
 352 void complete_change_console(unsigned int new_console)
     /* [previous][next][first][last][top][bottom][index][help] */
 353 {
 354         unsigned char old_vc_mode;
 355 
 356         if (new_console == fg_console || new_console >= NR_CONSOLES)
 357                 return;
 358 
 359         /*
 360          * If we're switching, we could be going from KD_GRAPHICS to
 361          * KD_TEXT mode or vice versa, which means we need to blank or
 362          * unblank the screen later.
 363          */
 364         old_vc_mode = vt_cons[fg_console].vc_mode;
 365         update_screen(new_console);
 366 
 367         /*
 368          * If this new console is under process control, send it a signal
 369          * telling it that it has acquired. Also check if it has died and
 370          * clean up (similar to logic employed in change_console())
 371          */
 372         if (vt_cons[new_console].vt_mode.mode == VT_PROCESS)
 373         {
 374                 /*
 375                  * Send the signal as privileged - kill_proc() will
 376                  * tell us if the process has gone or something else
 377                  * is awry
 378                  */
 379                 if (kill_proc(vt_cons[new_console].vt_pid,
 380                               vt_cons[new_console].vt_mode.acqsig,
 381                               1) != 0)
 382                 {
 383                 /*
 384                  * The controlling process has died, so we revert back to
 385                  * normal operation. In this case, we'll also change back
 386                  * to KD_TEXT mode. I'm not sure if this is strictly correct
 387                  * but it saves the agony when the X server dies and the screen
 388                  * remains blanked due to KD_GRAPHICS! It would be nice to do
 389                  * this outside of VT_PROCESS but there is no single process
 390                  * to account for and tracking tty count may be undesirable.
 391                  */
 392                         vt_cons[new_console].vc_mode = KD_TEXT;
 393                         clr_vc_kbd_flag(kbd_table + new_console, VC_RAW);
 394                         vt_cons[new_console].vt_mode.mode = VT_AUTO;
 395                         vt_cons[new_console].vt_mode.waitv = 0;
 396                         vt_cons[new_console].vt_mode.relsig = 0;
 397                         vt_cons[new_console].vt_mode.acqsig = 0;
 398                         vt_cons[new_console].vt_mode.frsig = 0;
 399                         vt_cons[new_console].vt_pid = -1;
 400                         vt_cons[new_console].vt_newvt = -1;
 401                 }
 402         }
 403 
 404         /*
 405          * We do this here because the controlling process above may have
 406          * gone, and so there is now a new vc_mode
 407          */
 408         if (old_vc_mode != vt_cons[new_console].vc_mode)
 409         {
 410                 if (vt_cons[new_console].vc_mode == KD_TEXT)
 411                         unblank_screen();
 412                 else {
 413                         timer_active &= ~(1<<BLANK_TIMER);
 414                         blank_screen();
 415                 }
 416         }
 417 
 418         /*
 419          * Wake anyone waiting for their VT to activate
 420          */
 421         vt_wake_waitactive();
 422         return;
 423 }
 424 
 425 /*
 426  * Performs the front-end of a vt switch
 427  */
 428 void change_console(unsigned int new_console)
     /* [previous][next][first][last][top][bottom][index][help] */
 429 {
 430         if (new_console == fg_console || new_console >= NR_CONSOLES)
 431                 return;
 432 
 433         /*
 434          * If this vt is in process mode, then we need to handshake with
 435          * that process before switching. Essentially, we store where that
 436          * vt wants to switch to and wait for it to tell us when it's done
 437          * (via VT_RELDISP ioctl).
 438          *
 439          * We also check to see if the controlling process still exists.
 440          * If it doesn't, we reset this vt to auto mode and continue.
 441          * This is a cheap way to track process control. The worst thing
 442          * that can happen is: we send a signal to a process, it dies, and
 443          * the switch gets "lost" waiting for a response; hopefully, the
 444          * user will try again, we'll detect the process is gone (unless
 445          * the user waits just the right amount of time :-) and revert the
 446          * vt to auto control.
 447          */
 448         if (vt_cons[fg_console].vt_mode.mode == VT_PROCESS)
 449         {
 450                 /*
 451                  * Send the signal as privileged - kill_proc() will
 452                  * tell us if the process has gone or something else
 453                  * is awry
 454                  */
 455                 if (kill_proc(vt_cons[fg_console].vt_pid,
 456                               vt_cons[fg_console].vt_mode.relsig,
 457                               1) == 0)
 458                 {
 459                         /*
 460                          * It worked. Mark the vt to switch to and
 461                          * return. The process needs to send us a
 462                          * VT_RELDISP ioctl to complete the switch.
 463                          */
 464                         vt_cons[fg_console].vt_newvt = new_console;
 465                         return;
 466                 }
 467 
 468                 /*
 469                  * The controlling process has died, so we revert back to
 470                  * normal operation. In this case, we'll also change back
 471                  * to KD_TEXT mode. I'm not sure if this is strictly correct
 472                  * but it saves the agony when the X server dies and the screen
 473                  * remains blanked due to KD_GRAPHICS! It would be nice to do
 474                  * this outside of VT_PROCESS but there is no single process
 475                  * to account for and tracking tty count may be undesirable.
 476                  */
 477                 vt_cons[fg_console].vc_mode = KD_TEXT;
 478                 clr_vc_kbd_flag(kbd_table + fg_console, VC_RAW);
 479                 vt_cons[fg_console].vt_mode.mode = VT_AUTO;
 480                 vt_cons[fg_console].vt_mode.waitv = 0;
 481                 vt_cons[fg_console].vt_mode.relsig = 0;
 482                 vt_cons[fg_console].vt_mode.acqsig = 0;
 483                 vt_cons[fg_console].vt_mode.frsig = 0;
 484                 vt_cons[fg_console].vt_pid = -1;
 485                 vt_cons[fg_console].vt_newvt = -1;
 486                 /*
 487                  * Fall through to normal (VT_AUTO) handling of the switch...
 488                  */
 489         }
 490 
 491         /*
 492          * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
 493          */
 494         if (vt_cons[fg_console].vc_mode == KD_GRAPHICS)
 495                 return;
 496 
 497         complete_change_console(new_console);
 498 }
 499 
 500 void wait_for_keypress(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 501 {
 502         sleep_on(&keypress_wait);
 503 }
 504 
 505 void stop_tty(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 506 {
 507         if (tty->stopped)
 508                 return;
 509         tty->stopped = 1;
 510         if (tty->link && tty->link->packet) {
 511                 tty->ctrl_status &= ~TIOCPKT_START;
 512                 tty->ctrl_status |= TIOCPKT_STOP;
 513                 wake_up_interruptible(&tty->link->secondary.proc_list);
 514         }
 515         if (tty->stop)
 516                 (tty->stop)(tty);
 517         if (IS_A_CONSOLE(tty->line)) {
 518                 set_vc_kbd_flag(kbd_table + fg_console, VC_SCROLLOCK);
 519                 set_leds();
 520         }
 521 }
 522 
 523 void start_tty(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 524 {
 525         if (!tty->stopped)
 526                 return;
 527         tty->stopped = 0;
 528         if (tty->link && tty->link->packet) {
 529                 tty->ctrl_status &= ~TIOCPKT_STOP;
 530                 tty->ctrl_status |= TIOCPKT_START;
 531                 wake_up_interruptible(&tty->link->secondary.proc_list);
 532         }
 533         if (tty->start)
 534                 (tty->start)(tty);
 535         if (IS_A_CONSOLE(tty->line)) {
 536                 clr_vc_kbd_flag(kbd_table + fg_console, VC_SCROLLOCK);
 537                 set_leds();
 538         }
 539         TTY_WRITE_FLUSH(tty);
 540 }
 541 
 542 /* Perform OPOST processing.  Returns -1 when the write_q becomes full
 543    and the character must be retried. */
 544 
 545 static int opost(unsigned char c, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 546 {
 547         if (FULL(&tty->write_q))
 548                 return -1;
 549         if (O_OPOST(tty)) {
 550                 switch (c) {
 551                 case '\n':
 552                         if (O_ONLRET(tty))
 553                                 tty->column = 0;
 554                         if (O_ONLCR(tty)) {
 555                                 if (LEFT(&tty->write_q) < 2)
 556                                         return -1;
 557                                 put_tty_queue('\r', &tty->write_q);
 558                                 tty->column = 0;
 559                         }
 560                         tty->canon_column = tty->column;
 561                         break;
 562                 case '\r':
 563                         if (O_ONOCR(tty) && tty->column == 0)
 564                                 return 0;
 565                         if (O_OCRNL(tty)) {
 566                                 c = '\n';
 567                                 if (O_ONLRET(tty))
 568                                         tty->canon_column = tty->column = 0;
 569                                 break;
 570                         }
 571                         tty->canon_column = tty->column = 0;
 572                         break;
 573                 case '\t':
 574                         if (O_TABDLY(tty) == XTABS) {
 575                                 if (LEFT(&tty->write_q) < 8)
 576                                         return -1;
 577                                 do
 578                                         put_tty_queue(' ', &tty->write_q);
 579                                 while (++tty->column % 8);
 580                                 return 0;
 581                         }
 582                         tty->column = (tty->column | 7) + 1;
 583                         break;
 584                 case '\b':
 585                         if (tty->column > 0)
 586                                 tty->column--;
 587                         break;
 588                 default:
 589                         if (O_OLCUC(tty))
 590                                 c = toupper(c);
 591                         if (!iscntrl(c))
 592                                 tty->column++;
 593                         break;
 594                 }
 595         }
 596         put_tty_queue(c, &tty->write_q);
 597         return 0;
 598 }
 599 
 600 /* Must be called only when L_ECHO(tty) is true. */
 601 
 602 static void echo_char(unsigned char c, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 603 {
 604         if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
 605                 opost('^', tty);
 606                 opost(c ^ 0100, tty);
 607         } else
 608                 opost(c, tty);
 609 }
 610 
 611 static void eraser(unsigned char c, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 612 {
 613         enum { ERASE, WERASE, KILL } kill_type;
 614         int seen_alnums;
 615 
 616         if (tty->secondary.head == tty->canon_head) {
 617                 /* opost('\a', tty); */         /* what do you think? */
 618                 return;
 619         }
 620         if (c == ERASE_CHAR(tty))
 621                 kill_type = ERASE;
 622         else if (c == WERASE_CHAR(tty))
 623                 kill_type = WERASE;
 624         else {
 625                 if (!L_ECHO(tty)) {
 626                         tty->secondary.head = tty->canon_head;
 627                         return;
 628                 }
 629                 if (!L_ECHOK(tty) || !L_ECHOKE(tty)) {
 630                         tty->secondary.head = tty->canon_head;
 631                         if (tty->erasing) {
 632                                 opost('/', tty);
 633                                 tty->erasing = 0;
 634                         }
 635                         echo_char(KILL_CHAR(tty), tty);
 636                         /* Add a newline if ECHOK is on and ECHOKE is off. */
 637                         if (L_ECHOK(tty))
 638                                 opost('\n', tty);
 639                         return;
 640                 }
 641                 kill_type = KILL;
 642         }
 643 
 644         seen_alnums = 0;
 645         while (tty->secondary.head != tty->canon_head) {
 646                 c = LAST(&tty->secondary);
 647                 if (kill_type == WERASE) {
 648                         /* Equivalent to BSD's ALTWERASE. */
 649                         if (isalnum(c) || c == '_')
 650                                 seen_alnums++;
 651                         else if (seen_alnums)
 652                                 break;
 653                 }
 654                 DEC(tty->secondary.head);
 655                 if (L_ECHO(tty)) {
 656                         if (L_ECHOPRT(tty)) {
 657                                 if (!tty->erasing) {
 658                                         opost('\\', tty);
 659                                         tty->erasing = 1;
 660                                 }
 661                                 echo_char(c, tty);
 662                         } else if (!L_ECHOE(tty)) {
 663                                 echo_char(ERASE_CHAR(tty), tty);
 664                         } else if (c == '\t') {
 665                                 unsigned int col = tty->canon_column;
 666                                 unsigned long tail = tty->canon_head;
 667 
 668                                 /* Find the column of the last char. */
 669                                 while (tail != tty->secondary.head) {
 670                                         c = tty->secondary.buf[tail];
 671                                         if (c == '\t')
 672                                                 col = (col | 7) + 1;
 673                                         else if (iscntrl(c)) {
 674                                                 if (L_ECHOCTL(tty))
 675                                                         col += 2;
 676                                         } else
 677                                                 col++;
 678                                         INC(tail);
 679                                 }
 680 
 681                                 /* Now backup to that column. */
 682                                 while (tty->column > col) {
 683                                         /* Can't use opost here. */
 684                                         put_tty_queue('\b', &tty->write_q);
 685                                         tty->column--;
 686                                 }
 687                         } else {
 688                                 if (iscntrl(c) && L_ECHOCTL(tty)) {
 689                                         opost('\b', tty);
 690                                         opost(' ', tty);
 691                                         opost('\b', tty);
 692                                 }
 693                                 if (!iscntrl(c) || L_ECHOCTL(tty)) {
 694                                         opost('\b', tty);
 695                                         opost(' ', tty);
 696                                         opost('\b', tty);
 697                                 }
 698                         }
 699                 }
 700                 if (kill_type == ERASE)
 701                         break;
 702         }
 703         if (tty->erasing && tty->secondary.head == tty->canon_head) {
 704                 opost('/', tty);
 705                 tty->erasing = 0;
 706         }
 707 }
 708 
 709 static void isig(int sig, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 710 {
 711         kill_pg(tty->pgrp, sig, 1);
 712         if (!L_NOFLSH(tty)) {
 713                 flush_input(tty);
 714                 flush_output(tty);
 715         }
 716 }
 717 
 718 static void copy_to_cooked(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 719 {
 720         int c, special_flag;
 721         unsigned long flags;
 722 
 723         if (!tty) {
 724                 printk("copy_to_cooked: called with NULL tty\n");
 725                 return;
 726         }
 727         if (!tty->write) {
 728                 printk("copy_to_cooked: tty %d has null write routine\n",
 729                        tty->line);
 730         }
 731         while (1) {
 732                 /*
 733                  * Check to see how much room we have left in the
 734                  * secondary queue.  Send a throttle command or abort
 735                  * if necessary.
 736                  */
 737                 c = LEFT(&tty->secondary);
 738                 if (tty->throttle && (c < SQ_THRESHOLD_LW)
 739                     && !set_bit(TTY_SQ_THROTTLED, &tty->flags))
 740                         tty->throttle(tty, TTY_THROTTLE_SQ_FULL);
 741                 if (c == 0)
 742                         break;
 743                 save_flags(flags); cli();
 744                 if (!EMPTY(&tty->read_q)) {
 745                         c = tty->read_q.buf[tty->read_q.tail];
 746                         special_flag = clear_bit(tty->read_q.tail,
 747                                                  &tty->readq_flags);
 748                         INC(tty->read_q.tail);
 749                         restore_flags(flags);
 750                 } else {
 751                         restore_flags(flags);
 752                         break;
 753                 }
 754                 if (special_flag) {
 755                         tty->char_error = c;
 756                         continue;
 757                 }
 758                 if (tty->char_error) {
 759                         if (tty->char_error == TTY_BREAK) {
 760                                 tty->char_error = 0;
 761                                 if (I_IGNBRK(tty))
 762                                         continue;
 763                                 /* A break is handled by the lower levels. */
 764                                 if (I_BRKINT(tty))
 765                                         continue;
 766                                 if (I_PARMRK(tty)) {
 767                                         put_tty_queue('\377', &tty->secondary);
 768                                         put_tty_queue('\0', &tty->secondary);
 769                                 }
 770                                 put_tty_queue('\0', &tty->secondary);
 771                                 continue;
 772                         }
 773                         if (tty->char_error == TTY_OVERRUN) {
 774                                 tty->char_error = 0;
 775                                 printk("tty%d: input overrun\n", tty->line);
 776                                 continue;
 777                         }
 778                         /* Must be a parity or frame error */
 779                         tty->char_error = 0;
 780                         if (I_IGNPAR(tty)) {
 781                                 continue;
 782                         }
 783                         if (I_PARMRK(tty)) {
 784                                 put_tty_queue('\377', &tty->secondary);
 785                                 put_tty_queue('\0', &tty->secondary);
 786                                 put_tty_queue(c, &tty->secondary);
 787                         } else
 788                                 put_tty_queue('\0', &tty->secondary);
 789                         continue;
 790                 }
 791                 if (I_ISTRIP(tty))
 792                         c &= 0x7f;
 793                 if (!tty->lnext) {
 794                         if (c == '\r') {
 795                                 if (I_IGNCR(tty))
 796                                         continue;
 797                                 if (I_ICRNL(tty))
 798                                         c = '\n';
 799                         } else if (c == '\n' && I_INLCR(tty))
 800                                 c = '\r';
 801                 }
 802                 if (I_IUCLC(tty) && L_IEXTEN(tty))
 803                         c=tolower(c);
 804                 if (c == __DISABLED_CHAR)
 805                         tty->lnext = 1;
 806                 if (L_ICANON(tty) && !tty->lnext) {
 807                         if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
 808                             (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
 809                                 eraser(c, tty);
 810                                 continue;
 811                         }
 812                         if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
 813                                 tty->lnext = 1;
 814                                 if (L_ECHO(tty)) {
 815                                         if (tty->erasing) {
 816                                                 opost('/', tty);
 817                                                 tty->erasing = 0;
 818                                         }
 819                                         if (L_ECHOCTL(tty)) {
 820                                                 opost('^', tty);
 821                                                 opost('\b', tty);
 822                                         }
 823                                 }
 824                                 continue;
 825                         }
 826                         if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
 827                             L_IEXTEN(tty)) {
 828                                 unsigned long tail = tty->canon_head;
 829 
 830                                 if (tty->erasing) {
 831                                         opost('/', tty);
 832                                         tty->erasing = 0;
 833                                 }
 834                                 echo_char(c, tty);
 835                                 opost('\n', tty);
 836                                 while (tail != tty->secondary.head) {
 837                                         echo_char(tty->secondary.buf[tail],
 838                                                   tty);
 839                                         INC(tail);
 840                                 }
 841                                 continue;
 842                         }
 843                 }
 844                 if (I_IXON(tty) && !tty->lnext) {
 845                         if ((tty->stopped && I_IXANY(tty) && L_IEXTEN(tty)) ||
 846                             c == START_CHAR(tty)) {
 847                                 start_tty(tty);
 848                                 continue;
 849                         }
 850                         if (c == STOP_CHAR(tty)) {
 851                                 stop_tty(tty);
 852                                 continue;
 853                         }
 854                 }
 855                 if (L_ISIG(tty) && !tty->lnext) {
 856                         if (c == INTR_CHAR(tty)) {
 857                                 isig(SIGINT, tty);
 858                                 continue;
 859                         }
 860                         if (c == QUIT_CHAR(tty)) {
 861                                 isig(SIGQUIT, tty);
 862                                 continue;
 863                         }
 864                         if (c == SUSP_CHAR(tty)) {
 865                                 if (!is_orphaned_pgrp(tty->pgrp))
 866                                         isig(SIGTSTP, tty);
 867                                 continue;
 868                         }
 869                 }
 870 
 871                 if (tty->erasing) {
 872                         opost('/', tty);
 873                         tty->erasing = 0;
 874                 }
 875                 if (c == '\n' && !tty->lnext) {
 876                         if (L_ECHO(tty) || (L_ICANON(tty) && L_ECHONL(tty)))
 877                                 opost('\n', tty);
 878                 } else if (L_ECHO(tty)) {
 879                         /* Don't echo the EOF char in canonical mode.  Sun
 880                            handles this differently by echoing the char and
 881                            then backspacing, but that's a hack. */
 882                         if (c != EOF_CHAR(tty) || !L_ICANON(tty) ||
 883                             tty->lnext) {
 884                                 /* Record the column of first canon char. */
 885                                 if (tty->canon_head == tty->secondary.head)
 886                                         tty->canon_column = tty->column;
 887                                 echo_char(c, tty);
 888                         }
 889                 }
 890 
 891                 if (I_PARMRK(tty) && c == (unsigned char) '\377' &&
 892                     (c != EOF_CHAR(tty) || !L_ICANON(tty) || tty->lnext))
 893                         put_tty_queue(c, &tty->secondary);
 894 
 895                 if (L_ICANON(tty) && !tty->lnext &&
 896                     (c == '\n' || c == EOF_CHAR(tty) || c == EOL_CHAR(tty) ||
 897                      (c == EOL2_CHAR(tty) && L_IEXTEN(tty)))) {
 898                         if (c == EOF_CHAR(tty))
 899                                 c = __DISABLED_CHAR;
 900                         set_bit(tty->secondary.head, &tty->secondary_flags);
 901                         put_tty_queue(c, &tty->secondary);
 902                         tty->canon_head = tty->secondary.head;
 903                         tty->canon_data++;
 904                 } else
 905                         put_tty_queue(c, &tty->secondary);
 906                 tty->lnext = 0;
 907         }
 908         if (!EMPTY(&tty->write_q))
 909                 TTY_WRITE_FLUSH(tty);
 910         if (L_ICANON(tty) ? tty->canon_data : !EMPTY(&tty->secondary))
 911                 wake_up_interruptible(&tty->secondary.proc_list);
 912 
 913         if (tty->throttle && (LEFT(&tty->read_q) >= RQ_THRESHOLD_HW)
 914             && clear_bit(TTY_RQ_THROTTLED, &tty->flags))
 915                 tty->throttle(tty, TTY_THROTTLE_RQ_AVAIL);
 916 }
 917 
 918 int is_ignored(int sig)
     /* [previous][next][first][last][top][bottom][index][help] */
 919 {
 920         return ((current->blocked & (1<<(sig-1))) ||
 921                 (current->sigaction[sig-1].sa_handler == SIG_IGN));
 922 }
 923 
 924 static inline int input_available_p(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 925 {
 926         /* Avoid calling TTY_READ_FLUSH unnecessarily. */
 927         if (L_ICANON(tty)) {
 928                 if (tty->canon_data || FULL(&tty->read_q))
 929                         return 1;
 930         } else if (!EMPTY(&tty->secondary))
 931                 return 1;
 932 
 933         /* Shuffle any pending data down the queues. */
 934         TTY_READ_FLUSH(tty);
 935         if (tty->link)
 936                 TTY_WRITE_FLUSH(tty->link);
 937 
 938         if (L_ICANON(tty)) {
 939                 if (tty->canon_data || FULL(&tty->read_q))
 940                         return 1;
 941         } else if (!EMPTY(&tty->secondary))
 942                 return 1;
 943         return 0;
 944 }
 945 
 946 static int read_chan(struct tty_struct *tty, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
 947                      unsigned char *buf, unsigned int nr)
 948 {
 949         struct wait_queue wait = { current, NULL };
 950         int c;
 951         unsigned char *b = buf;
 952         int minimum, time;
 953         int retval = 0;
 954 
 955         if (L_ICANON(tty)) {
 956                 minimum = time = 0;
 957                 current->timeout = (unsigned long) -1;
 958         } else {
 959                 time = (HZ / 10) * TIME_CHAR(tty);
 960                 minimum = MIN_CHAR(tty);
 961                 if (minimum)
 962                         current->timeout = (unsigned long) -1;
 963                 else {
 964                         if (time) {
 965                                 current->timeout = time + jiffies;
 966                                 time = 0;
 967                         } else
 968                                 current->timeout = 0;
 969                         minimum = 1;
 970                 }
 971         }
 972 
 973         add_wait_queue(&tty->secondary.proc_list, &wait);
 974         while (1) {
 975                 /* Job control check -- must be done at start and after
 976                    every sleep (POSIX.1 7.1.1.4). */
 977                 /* don't stop on /dev/console */
 978                 if (file->f_inode->i_rdev != CONSOLE_DEV &&
 979                     current->tty == tty->line) {
 980                         if (tty->pgrp <= 0)
 981                                 printk("read_chan: tty->pgrp <= 0!\n");
 982                         else if (current->pgrp != tty->pgrp) {
 983                                 if (is_ignored(SIGTTIN) ||
 984                                     is_orphaned_pgrp(current->pgrp)) {
 985                                         retval = -EIO;
 986                                         break;
 987                                 }
 988                                 kill_pg(current->pgrp, SIGTTIN, 1);
 989                                 retval = -ERESTARTSYS;
 990                                 break;
 991                         }
 992                 }
 993                 /* First test for status change. */
 994                 if (tty->packet && tty->link->ctrl_status) {
 995                         if (b != buf)
 996                                 break;
 997                         put_fs_byte(tty->link->ctrl_status, b++);
 998                         tty->link->ctrl_status = 0;
 999                         break;
1000                 }
1001                 /* This statement must be first before checking for input
1002                    so that any interrupt will set the state back to
1003                    TASK_RUNNING. */
1004                 current->state = TASK_INTERRUPTIBLE;
1005                 if (!input_available_p(tty)) {
1006                         if (tty->flags & (1 << TTY_SLAVE_CLOSED)) {
1007                                 retval = -EIO;
1008                                 break;
1009                         }
1010                         if (tty_hung_up_p(file))
1011                                 break;
1012                         if (!current->timeout)
1013                                 break;
1014                         if (file->f_flags & O_NONBLOCK) {
1015                                 retval = -EAGAIN;
1016                                 break;
1017                         }
1018                         if (current->signal & ~current->blocked) {
1019                                 retval = -ERESTARTSYS;
1020                                 break;
1021                         }
1022                         schedule();
1023                         continue;
1024                 }
1025                 current->state = TASK_RUNNING;
1026 
1027                 /* Deal with packet mode. */
1028                 if (tty->packet && b == buf) {
1029                         put_fs_byte(TIOCPKT_DATA, b++);
1030                         nr--;
1031                 }
1032 
1033                 while (nr > 0) {
1034                         int eol;
1035 
1036                         cli();
1037                         if (EMPTY(&tty->secondary)) {
1038                                 sti();
1039                                 break;
1040                         }
1041                         eol = clear_bit(tty->secondary.tail,
1042                                         &tty->secondary_flags);
1043                         c = tty->secondary.buf[tty->secondary.tail];
1044                         INC(tty->secondary.tail);
1045                         sti();
1046                         if (eol) {
1047                                 if (--tty->canon_data < 0) {
1048                                         printk("read_chan: canon_data < 0!\n");
1049                                         tty->canon_data = 0;
1050                                 }
1051                                 if (c == __DISABLED_CHAR)
1052                                         break;
1053                                 put_fs_byte(c, b++);
1054                                 nr--;
1055                                 break;
1056                         }
1057                         put_fs_byte(c, b++);
1058                         nr--;
1059                 }
1060 
1061                 /* If there is enough space in the secondary queue now, let the
1062                    low-level driver know. */
1063                 if (tty->throttle && (LEFT(&tty->secondary) >= SQ_THRESHOLD_HW)
1064                     && !clear_bit(TTY_SQ_THROTTLED, &tty->flags))
1065                         tty->throttle(tty, TTY_THROTTLE_SQ_AVAIL);
1066 
1067                 /* XXX packet mode's status byte is mistakenly counted */
1068                 if (b - buf >= minimum || !nr)
1069                         break;
1070                 if (time)
1071                         current->timeout = time + jiffies;
1072         }
1073         remove_wait_queue(&tty->secondary.proc_list, &wait);
1074         current->state = TASK_RUNNING;
1075         current->timeout = 0;
1076         return (b - buf) ? b - buf : retval;
1077 }
1078 
1079 static int write_chan(struct tty_struct * tty, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1080                       unsigned char * buf, unsigned int nr)
1081 {
1082         struct wait_queue wait = { current, NULL };
1083         int c;
1084         unsigned char *b = buf;
1085         int retval = 0;
1086 
1087         /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1088         if (L_TOSTOP(tty) && file->f_inode->i_rdev != CONSOLE_DEV) {
1089                 retval = check_change(tty, tty->line);
1090                 if (retval)
1091                         return retval;
1092         }
1093 
1094         add_wait_queue(&tty->write_q.proc_list, &wait);
1095         while (1) {
1096                 current->state = TASK_INTERRUPTIBLE;
1097                 if (current->signal & ~current->blocked) {
1098                         retval = -ERESTARTSYS;
1099                         break;
1100                 }
1101                 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1102                         retval = -EIO;
1103                         break;
1104                 }
1105                 while (nr > 0) {
1106                         c = get_fs_byte(b);
1107                         /* Care is needed here: opost() can abort even
1108                            if the write_q is not full. */
1109                         if (opost(c, tty) < 0)
1110                                 break;
1111                         b++; nr--;
1112                 }
1113                 TTY_WRITE_FLUSH(tty);
1114                 if (!nr)
1115                         break;
1116                 if (EMPTY(&tty->write_q) && !need_resched)
1117                         continue;
1118                 schedule();
1119         }
1120         current->state = TASK_RUNNING;
1121         remove_wait_queue(&tty->write_q.proc_list, &wait);
1122         return (b - buf) ? b - buf : retval;
1123 }
1124 
1125 static int tty_read(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1126 {
1127         int i, dev;
1128         struct tty_struct * tty;
1129 
1130         dev = file->f_rdev;
1131         if (MAJOR(dev) != TTY_MAJOR) {
1132                 printk("tty_read: bad pseudo-major nr #%d\n", MAJOR(dev));
1133                 return -EINVAL;
1134         }
1135         dev = MINOR(dev);
1136         tty = TTY_TABLE(dev);
1137         if (!tty || (tty->flags & (1 << TTY_IO_ERROR)))
1138                 return -EIO;
1139 
1140         /* This check not only needs to be done before reading, but also
1141            whenever read_chan() gets woken up after sleeping, so I've
1142            moved it to there.  This should only be done for the N_TTY
1143            line discipline, anyway.  Same goes for write_chan(). -- jlc. */
1144 #if 0
1145         if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
1146             (tty->pgrp > 0) &&
1147             (current->tty == dev) &&
1148             (tty->pgrp != current->pgrp))
1149                 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
1150                         return -EIO;
1151                 else {
1152                         (void) kill_pg(current->pgrp, SIGTTIN, 1);
1153                         return -ERESTARTSYS;
1154                 }
1155 #endif
1156         if (ldiscs[tty->disc].read)
1157                 /* XXX casts are for what kernel-wide prototypes should be. */
1158                 i = (ldiscs[tty->disc].read)(tty,file,(unsigned char *)buf,(unsigned int)count);
1159         else
1160                 i = -EIO;
1161         if (i > 0)
1162                 inode->i_atime = CURRENT_TIME;
1163         return i;
1164 }
1165 
1166 static int tty_write(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1167 {
1168         int dev, i, is_console;
1169         struct tty_struct * tty;
1170 
1171         dev = file->f_rdev;
1172         is_console = (inode->i_rdev == CONSOLE_DEV);
1173         if (MAJOR(dev) != TTY_MAJOR) {
1174                 printk("tty_write: pseudo-major != TTY_MAJOR\n");
1175                 return -EINVAL;
1176         }
1177         dev = MINOR(dev);
1178         if (is_console && redirect)
1179                 tty = redirect;
1180         else
1181                 tty = TTY_TABLE(dev);
1182         if (!tty || !tty->write || (tty->flags & (1 << TTY_IO_ERROR)))
1183                 return -EIO;
1184 #if 0
1185         if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
1186             (current->tty == dev) && (tty->pgrp != current->pgrp)) {
1187                 if (is_orphaned_pgrp(current->pgrp))
1188                         return -EIO;
1189                 if (!is_ignored(SIGTTOU)) {
1190                         (void) kill_pg(current->pgrp, SIGTTOU, 1);
1191                         return -ERESTARTSYS;
1192                 }
1193         }
1194 #endif
1195         if (ldiscs[tty->disc].write)
1196                 /* XXX casts are for what kernel-wide prototypes should be. */
1197                 i = (ldiscs[tty->disc].write)(tty,file,(unsigned char *)buf,(unsigned int)count);
1198         else
1199                 i = -EIO;
1200         if (i > 0)
1201                 inode->i_mtime = CURRENT_TIME;
1202         return i;
1203 }
1204 
1205 /*
1206  * This is so ripe with races that you should *really* not touch this
1207  * unless you know exactly what you are doing. All the changes have to be
1208  * made atomically, or there may be incorrect pointers all over the place.
1209  */
1210 static int init_dev(int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1211 {
1212         struct tty_struct *tty, *o_tty;
1213         struct termios *tp, *o_tp, *ltp, *o_ltp;
1214         int retval;
1215         int o_dev;
1216 
1217         o_dev = PTY_OTHER(dev);
1218         tty = o_tty = NULL;
1219         tp = o_tp = NULL;
1220         ltp = o_ltp = NULL;
1221 repeat:
1222         retval = -EAGAIN;
1223         if (IS_A_PTY_MASTER(dev) && tty_table[dev] && tty_table[dev]->count)
1224                 goto end_init;
1225         retval = -ENOMEM;
1226         if (!tty_table[dev] && !tty) {
1227                 if (!(tty = (struct tty_struct*) get_free_page(GFP_KERNEL)))
1228                         goto end_init;
1229                 initialize_tty_struct(dev, tty);
1230                 goto repeat;
1231         }
1232         if (!tty_termios[dev] && !tp) {
1233                 tp = (struct termios *) kmalloc(sizeof(struct termios),
1234                                                 GFP_KERNEL);
1235                 if (!tp)
1236                         goto end_init;
1237                 initialize_termios(dev, tp);
1238                 goto repeat;
1239         }
1240         if (!termios_locked[dev] && !ltp) {
1241                 ltp = (struct termios *) kmalloc(sizeof(struct termios),
1242                                                  GFP_KERNEL);
1243                 if (!ltp)
1244                         goto end_init;
1245                 memset(ltp, 0, sizeof(struct termios));
1246                 goto repeat;
1247         }
1248         if (IS_A_PTY(dev)) {
1249                 if (!tty_table[o_dev] && !o_tty) {
1250                         o_tty = (struct tty_struct *)
1251                                 get_free_page(GFP_KERNEL);
1252                         if (!o_tty)
1253                                 goto end_init;
1254                         initialize_tty_struct(o_dev, o_tty);
1255                         goto repeat;
1256                 }
1257                 if (!tty_termios[o_dev] && !o_tp) {
1258                         o_tp = (struct termios *)
1259                                 kmalloc(sizeof(struct termios), GFP_KERNEL);
1260                         if (!o_tp)
1261                                 goto end_init;
1262                         initialize_termios(o_dev, o_tp);
1263                         goto repeat;
1264                 }
1265                 if (!termios_locked[o_dev] && !o_ltp) {
1266                         o_ltp = (struct termios *)
1267                                 kmalloc(sizeof(struct termios), GFP_KERNEL);
1268                         if (!o_ltp)
1269                                 goto end_init;
1270                         memset(o_ltp, 0, sizeof(struct termios));
1271                         goto repeat;
1272                 }
1273                 
1274         }
1275         /* Now we have allocated all the structures: update all the pointers.. */
1276         if (!tty_termios[dev]) {
1277                 tty_termios[dev] = tp;
1278                 tp = NULL;
1279         }
1280         if (!tty_table[dev]) {
1281                 tty->termios = tty_termios[dev];
1282                 tty_table[dev] = tty;
1283                 tty = NULL;
1284         }
1285         if (!termios_locked[dev]) {
1286                 termios_locked[dev] = ltp;
1287                 ltp = NULL;
1288         }
1289         if (IS_A_PTY(dev)) {
1290                 if (!tty_termios[o_dev]) {
1291                         tty_termios[o_dev] = o_tp;
1292                         o_tp = NULL;
1293                 }
1294                 if (!termios_locked[o_dev]) {
1295                         termios_locked[o_dev] = o_ltp;
1296                         o_ltp = NULL;
1297                 }
1298                 if (!tty_table[o_dev]) {
1299                         o_tty->termios = tty_termios[o_dev];
1300                         tty_table[o_dev] = o_tty;
1301                         o_tty = NULL;
1302                 }
1303                 tty_table[dev]->link = tty_table[o_dev];
1304                 tty_table[o_dev]->link = tty_table[dev];
1305         }
1306         tty_table[dev]->count++;
1307         if (IS_A_PTY_MASTER(dev))
1308                 tty_table[o_dev]->count++;
1309         retval = 0;
1310 end_init:
1311         if (tty)
1312                 free_page((unsigned long) tty);
1313         if (o_tty)
1314                 free_page((unsigned long) o_tty);
1315         if (tp)
1316                 kfree_s(tp, sizeof(struct termios));
1317         if (o_tp)
1318                 kfree_s(o_tp, sizeof(struct termios));
1319         if (ltp)
1320                 kfree_s(ltp, sizeof(struct termios));
1321         if (o_ltp)
1322                 kfree_s(o_ltp, sizeof(struct termios));
1323         return retval;
1324 }
1325 
1326 /*
1327  * Even releasing the tty structures is a tricky business.. We have
1328  * to be very careful that the structures are all released at the
1329  * same time, as interrupts might otherwise get the wrong pointers.
1330  */
1331 static void release_dev(int dev, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1332 {
1333         struct tty_struct *tty, *o_tty;
1334         struct termios *tp, *o_tp;
1335         struct task_struct **p;
1336 
1337         tty = tty_table[dev];
1338         tp = tty_termios[dev];
1339         o_tty = NULL;
1340         o_tp = NULL;
1341         if (!tty) {
1342                 printk("release_dev: tty_table[%d] was NULL\n", dev);
1343                 return;
1344         }
1345         if (!tp) {
1346                 printk("release_dev: tty_termios[%d] was NULL\n", dev);
1347                 return;
1348         }
1349 #ifdef TTY_DEBUG_HANGUP
1350         printk("release_dev of tty%d (tty count=%d)...", dev, tty->count);
1351 #endif
1352         if (IS_A_PTY(dev)) {
1353                 o_tty = tty_table[PTY_OTHER(dev)];
1354                 o_tp = tty_termios[PTY_OTHER(dev)];
1355                 if (!o_tty) {
1356                         printk("release_dev: pty pair(%d) was NULL\n", dev);
1357                         return;
1358                 }
1359                 if (!o_tp) {
1360                         printk("release_dev: pty pair(%d) termios was NULL\n", dev);
1361                         return;
1362                 }
1363                 if (tty->link != o_tty || o_tty->link != tty) {
1364                         printk("release_dev: bad pty pointers\n");
1365                         return;
1366                 }
1367         }
1368         tty->write_data_cnt = 0; /* Clear out pending trash */
1369         if (tty->close)
1370                 tty->close(tty, filp);
1371         if (IS_A_PTY_MASTER(dev)) {
1372                 if (--tty->link->count < 0) {
1373                         printk("release_dev: bad tty slave count (dev = %d): %d\n",
1374                                dev, tty->count);
1375                         tty->link->count = 0;
1376                 }
1377         }
1378         if (--tty->count < 0) {
1379                 printk("release_dev: bad tty_table[%d]->count: %d\n",
1380                        dev, tty->count);
1381                 tty->count = 0;
1382         }
1383         if (tty->count)
1384                 return;
1385         
1386 #ifdef TTY_DEBUG_HANGUP
1387         printk("freeing tty structure...");
1388 #endif
1389 
1390         /*
1391          * Make sure there aren't any processes that still think this
1392          * tty is their controlling tty.
1393          */
1394         for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1395                 if ((*p) && (*p)->tty == tty->line)
1396                 (*p)->tty = -1;
1397         }
1398 
1399         /*
1400          * Shutdown the current line discipline, and reset it to
1401          * N_TTY.
1402          */
1403         if (ldiscs[tty->disc].close != NULL)
1404                 ldiscs[tty->disc].close(tty);
1405         tty->disc = N_TTY;
1406         tty->termios->c_line = N_TTY;
1407         
1408         if (o_tty) {
1409                 if (o_tty->count)
1410                         return;
1411                 else {
1412                         tty_table[PTY_OTHER(dev)] = NULL;
1413                         tty_termios[PTY_OTHER(dev)] = NULL;
1414                 }
1415         }
1416         tty_table[dev] = NULL;
1417         if (IS_A_PTY(dev)) {
1418                 tty_termios[dev] = NULL;
1419                 kfree_s(tp, sizeof(struct termios));
1420         }
1421         if (tty == redirect || o_tty == redirect)
1422                 redirect = NULL;
1423         free_page((unsigned long) tty);
1424         if (o_tty)
1425                 free_page((unsigned long) o_tty);
1426         if (o_tp)
1427                 kfree_s(o_tp, sizeof(struct termios));
1428 }
1429 
1430 /*
1431  * tty_open and tty_release keep up the tty count that contains the
1432  * number of opens done on a tty. We cannot use the inode-count, as
1433  * different inodes might point to the same tty.
1434  *
1435  * Open-counting is needed for pty masters, as well as for keeping
1436  * track of serial lines: DTR is dropped when the last close happens.
1437  * (This is not done solely through tty->count, now.  - Ted 1/27/92)
1438  *
1439  * The termios state of a pty is reset on first open so that
1440  * settings don't persist across reuse.
1441  */
1442 static int tty_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1443 {
1444         struct tty_struct *tty;
1445         int major, minor;
1446         int noctty, retval;
1447 
1448 retry_open:
1449         minor = MINOR(inode->i_rdev);
1450         major = MAJOR(inode->i_rdev);
1451         noctty = filp->f_flags & O_NOCTTY;
1452         if (major == TTYAUX_MAJOR) {
1453                 if (!minor) {
1454                         major = TTY_MAJOR;
1455                         minor = current->tty;
1456                 }
1457                 /* noctty = 1; */
1458         } else if (major == TTY_MAJOR) {
1459                 if (!minor) {
1460                         minor = fg_console + 1;
1461                         noctty = 1;
1462                 }
1463         } else {
1464                 printk("Bad major #%d in tty_open\n", MAJOR(inode->i_rdev));
1465                 return -ENODEV;
1466         }
1467         if (minor <= 0)
1468                 return -ENXIO;
1469         if (IS_A_PTY_MASTER(minor))
1470                 noctty = 1;
1471         filp->f_rdev = (major << 8) | minor;
1472         retval = init_dev(minor);
1473         if (retval)
1474                 return retval;
1475         tty = tty_table[minor];
1476 #ifdef TTY_DEBUG_HANGUP
1477         printk("opening tty%d...", tty->line);
1478 #endif
1479         if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1480                 return -EBUSY;
1481 
1482 #if 0
1483         /* clean up the packet stuff. */
1484         /*
1485          *  Why is this not done in init_dev?  Right here, if another 
1486          * process opens up a tty in packet mode, all the packet 
1487          * variables get cleared.  Come to think of it, is anything 
1488          * using the packet mode at all???  - Ted, 1/27/93
1489          *
1490          * Not to worry, a pty master can only be opened once.
1491          * And rlogind and telnetd both use packet mode.  -- jrs
1492          *
1493          * Not needed.  These are cleared in initialize_tty_struct. -- jlc
1494          */
1495         tty->ctrl_status = 0;
1496         tty->packet = 0;
1497 #endif
1498 
1499         if (tty->open) {
1500                 retval = tty->open(tty, filp);
1501         } else {
1502                 retval = -ENODEV;
1503         }
1504         if (retval) {
1505 #ifdef TTY_DEBUG_HANGUP
1506                 printk("error %d in opening tty%d...", retval, tty->line);
1507 #endif
1508 
1509                 release_dev(minor, filp);
1510                 if (retval != -ERESTARTSYS)
1511                         return retval;
1512                 if (current->signal & ~current->blocked)
1513                         return retval;
1514                 schedule();
1515                 goto retry_open;
1516         }
1517         if (!noctty &&
1518             current->leader &&
1519             current->tty<0 &&
1520             tty->session==0) {
1521                 current->tty = minor;
1522                 tty->session = current->session;
1523                 tty->pgrp = current->pgrp;
1524         }
1525         filp->f_rdev = MKDEV(TTY_MAJOR,minor); /* Set it to something normal */
1526         return 0;
1527 }
1528 
1529 /*
1530  * Note that releasing a pty master also releases the child, so
1531  * we have to make the redirection checks after that and on both
1532  * sides of a pty.
1533  */
1534 static void tty_release(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1535 {
1536         int dev;
1537 
1538         dev = filp->f_rdev;
1539         if (MAJOR(dev) != TTY_MAJOR) {
1540                 printk("tty_release: tty pseudo-major != TTY_MAJOR\n");
1541                 return;
1542         }
1543         dev = MINOR(filp->f_rdev);
1544         if (!dev) {
1545                 printk("tty_release: bad f_rdev\n");
1546                 return;
1547         }
1548         release_dev(dev, filp);
1549 }
1550 
1551 static int tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1552 {
1553         int dev;
1554         struct tty_struct * tty;
1555 
1556         dev = filp->f_rdev;
1557         if (MAJOR(dev) != TTY_MAJOR) {
1558                 printk("tty_select: tty pseudo-major != TTY_MAJOR\n");
1559                 return 0;
1560         }
1561         dev = MINOR(filp->f_rdev);
1562         tty = TTY_TABLE(dev);
1563         if (!tty) {
1564                 printk("tty_select: tty struct for dev %d was NULL\n", dev);
1565                 return 0;
1566         }
1567         if (ldiscs[tty->disc].select)
1568                 return (ldiscs[tty->disc].select)(tty, inode, filp,
1569                                                   sel_type, wait);
1570         return 0;
1571 }
1572 
1573 static int normal_select(struct tty_struct * tty, struct inode * inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1574                          struct file * file, int sel_type, select_table *wait)
1575 {
1576         switch (sel_type) {
1577                 case SEL_IN:
1578                         if (input_available_p(tty))
1579                                 return 1;
1580                         /* fall through */
1581                 case SEL_EX:
1582                         if (tty->packet && tty->link->ctrl_status)
1583                                 return 1;
1584                         if (tty->flags & (1 << TTY_SLAVE_CLOSED))
1585                                 return 1;
1586                         if (tty_hung_up_p(file))
1587                                 return 1;
1588                         select_wait(&tty->secondary.proc_list, wait);
1589                         return 0;
1590                 case SEL_OUT:
1591                         if (!FULL(&tty->write_q))
1592                                 return 1;
1593                         select_wait(&tty->write_q.proc_list, wait);
1594                         return 0;
1595         }
1596         return 0;
1597 }
1598 
1599 /*
1600  * This implements the "Secure Attention Key" ---  the idea is to
1601  * prevent trojan horses by killing all processes associated with this
1602  * tty when the user hits the "Secure Attention Key".  Required for
1603  * super-paranoid applications --- see the Orange Book for more details.
1604  * 
1605  * This code could be nicer; ideally it should send a HUP, wait a few
1606  * seconds, then send a INT, and then a KILL signal.  But you then
1607  * have to coordinate with the init process, since all processes associated
1608  * with the current tty must be dead before the new getty is allowed
1609  * to spawn.
1610  */
1611 void do_SAK( struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1612 {
1613 #ifdef TTY_SOFT_SAK
1614         tty_hangup(tty);
1615 #else
1616         struct task_struct **p;
1617         int line = tty->line;
1618         int session = tty->session;
1619         int             i;
1620         struct file     *filp;
1621         
1622         flush_input(tty);
1623         flush_output(tty);
1624         for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1625                 if (!(*p))
1626                         continue;
1627                 if (((*p)->tty == line) ||
1628                     ((session > 0) && ((*p)->session == session)))
1629                         send_sig(SIGKILL, *p, 1);
1630                 else {
1631                         for (i=0; i < NR_OPEN; i++) {
1632                                 filp = (*p)->filp[i];
1633                                 if (filp && (filp->f_op == &tty_fops) &&
1634                                     (MINOR(filp->f_rdev) == line)) {
1635                                         send_sig(SIGKILL, *p, 1);
1636                                         break;
1637                                 }
1638                         }
1639                 }
1640         }
1641 #endif
1642 }
1643 
1644 /*
1645  * This routine allows a kernel routine to send a large chunk of data
1646  * to a particular tty; if all of the data can be queued up for ouput
1647  * immediately, tty_write_data() will return 0.  If, however, not all
1648  * of the data can be immediately queued for delivery, the number of
1649  * bytes left to be queued up will be returned, and the rest of the
1650  * data will be queued up when there is room.  The callback function
1651  * will be called (with the argument callarg) when the last of the
1652  * data is finally in the queue.
1653  *
1654  * Note that the callback routine will _not_ be called if all of the
1655  * data could be queued immediately.  This is to avoid a problem with
1656  * the kernel stack getting too deep, which might happen if the
1657  * callback routine calls tty_write_data with itself as an argument.
1658  */
1659 int tty_write_data(struct tty_struct *tty, char *bufp, int buflen,
     /* [previous][next][first][last][top][bottom][index][help] */
1660                     void (*callback)(void * data), void * callarg)
1661 {
1662         int head, tail, count;
1663         unsigned long flags;
1664         char *p;
1665 
1666 #define VLEFT ((tail-head-1)&(TTY_BUF_SIZE-1))
1667 
1668         save_flags(flags);
1669         cli();
1670         if (tty->write_data_cnt) {
1671                 restore_flags(flags);
1672                 return -EBUSY;
1673         }
1674 
1675         head = tty->write_q.head;
1676         tail = tty->write_q.tail;
1677         count = buflen;
1678         p = bufp;
1679 
1680         while (count && VLEFT > 0) {
1681                 tty->write_q.buf[head++] = *p++;
1682                 head &= TTY_BUF_SIZE-1;
1683                 count--;
1684         }
1685         tty->write_q.head = head;
1686         if (count) {
1687                 tty->write_data_cnt = count;
1688                 tty->write_data_ptr = (unsigned char *) p;
1689                 tty->write_data_callback = callback;
1690                 tty->write_data_arg = callarg;
1691         }
1692         restore_flags(flags);
1693         tty->write(tty);
1694         return count;
1695 }
1696 
1697 /*
1698  * This routine routine is called after an interrupt has drained a
1699  * tty's write queue, so that there is more space for data waiting to
1700  * be sent in tty->write_data_ptr.
1701  *
1702  * tty_check_write[8] is a bitstring which indicates which ttys
1703  * needs to be processed.
1704  */
1705 void tty_bh_routine(void * unused)
     /* [previous][next][first][last][top][bottom][index][help] */
1706 {
1707         int     i, j, line, mask;
1708         int     head, tail, count;
1709         unsigned char * p;
1710         struct tty_struct * tty;
1711 
1712         for (i = 0, line = 0; i < MAX_TTYS / 32; i++) {
1713                 if (!tty_check_write[i]) {
1714                         line += 32;
1715                         continue;
1716                 }
1717                 for (j=0, mask=0; j < 32; j++, line++, mask <<= 1) {
1718                         if (clear_bit(j, &tty_check_write[i])) {
1719                                 tty = tty_table[line];
1720                                 if (!tty || !tty->write_data_cnt)
1721                                         continue;
1722                                 cli();
1723                                 head = tty->write_q.head;
1724                                 tail = tty->write_q.tail;
1725                                 count = tty->write_data_cnt;
1726                                 p = tty->write_data_ptr;
1727 
1728                                 while (count && VLEFT > 0) {
1729                                         tty->write_q.buf[head++] = *p++;
1730                                         head &= TTY_BUF_SIZE-1;
1731                                         count--;
1732                                 }
1733                                 tty->write_q.head = head;
1734                                 tty->write_data_ptr = p;
1735                                 tty->write_data_cnt = count;
1736                                 sti();
1737                                 if (!count)
1738                                         (tty->write_data_callback)
1739                                                 (tty->write_data_arg);
1740                         }
1741                 }
1742         }
1743         
1744 }
1745 
1746 /*
1747  * This subroutine initializes a tty structure.  We have to set up
1748  * things correctly for each different type of tty.
1749  */
1750 static void initialize_tty_struct(int line, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1751 {
1752         memset(tty, 0, sizeof(struct tty_struct));
1753         tty->line = line;
1754         tty->disc = N_TTY;
1755         tty->pgrp = -1;
1756         if (IS_A_CONSOLE(line)) {
1757                 tty->open = con_open;
1758                 tty->winsize.ws_row = video_num_lines;
1759                 tty->winsize.ws_col = video_num_columns;
1760         } else if IS_A_SERIAL(line) {
1761                 tty->open = rs_open;
1762         } else if IS_A_PTY(line) {
1763                 tty->open = pty_open;
1764         }
1765 }
1766 
1767 static void initialize_termios(int line, struct termios * tp)
     /* [previous][next][first][last][top][bottom][index][help] */
1768 {
1769         memset(tp, 0, sizeof(struct termios));
1770         memcpy(tp->c_cc, INIT_C_CC, NCCS);
1771         if (IS_A_CONSOLE(line) || IS_A_PTY_SLAVE(line)) {
1772                 tp->c_iflag = ICRNL | IXON;
1773                 tp->c_oflag = OPOST | ONLCR;
1774                 tp->c_cflag = B38400 | CS8 | CREAD;
1775                 tp->c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
1776                         ECHOCTL | ECHOKE | IEXTEN;
1777         } else if (IS_A_SERIAL(line)) {
1778                 tp->c_iflag = ICRNL | IXON;
1779                 tp->c_oflag = OPOST | ONLCR | XTABS;
1780                 tp->c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1781                 tp->c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
1782                         ECHOCTL | ECHOKE | IEXTEN;
1783         } else if (IS_A_PTY_MASTER(line))
1784                 tp->c_cflag = B9600 | CS8 | CREAD;
1785 }
1786 
1787 static struct tty_ldisc tty_ldisc_N_TTY = {
1788         0,                      /* flags */
1789         NULL,                   /* open */
1790         NULL,                   /* close */
1791         read_chan,              /* read */
1792         write_chan,             /* write */
1793         NULL,                   /* ioctl */
1794         normal_select,          /* select */
1795         copy_to_cooked          /* handler */
1796 };
1797 
1798         
1799 long tty_init(long kmem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1800 {
1801         int i;
1802 
1803         if (sizeof(struct tty_struct) > PAGE_SIZE)
1804                 panic("size of tty structure > PAGE_SIZE!");
1805         if (register_chrdev(TTY_MAJOR,"tty",&tty_fops))
1806                 panic("unable to get major %d for tty device", TTY_MAJOR);
1807         if (register_chrdev(TTYAUX_MAJOR,"tty",&tty_fops))
1808                 panic("unable to get major %d for tty device", TTYAUX_MAJOR);
1809         for (i=0 ; i< MAX_TTYS ; i++) {
1810                 tty_table[i] =  0;
1811                 tty_termios[i] = 0;
1812         }
1813         memset(tty_check_write, 0, sizeof(tty_check_write));
1814         bh_base[TTY_BH].routine = tty_bh_routine;
1815 
1816         /* Setup the default TTY line discipline. */
1817         memset(ldiscs, 0, sizeof(ldiscs));
1818         (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1819 
1820         kmem_start = kbd_init(kmem_start);
1821         kmem_start = con_init(kmem_start);
1822         kmem_start = rs_init(kmem_start);
1823         return kmem_start;
1824 }

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