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

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