root/kernel/chr_drv/pty.c

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

DEFINITIONS

This source file includes following definitions.
  1. pty_close
  2. pty_copy
  3. pty_write
  4. pty_open

   1 /*
   2  *  linux/kernel/chr_drv/pty.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  *      pty.c
   9  *
  10  * This module exports the following pty function:
  11  * 
  12  *      int  pty_open(struct tty_struct * tty, struct file * filp);
  13  */
  14 
  15 #include <linux/errno.h>
  16 #include <linux/sched.h>
  17 #include <linux/tty.h>
  18 #include <linux/fcntl.h>
  19 #include <linux/interrupt.h>
  20 
  21 #include <asm/system.h>
  22 #include <asm/bitops.h>
  23 
  24 static void pty_close(struct tty_struct * tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26         if (!tty)
  27                 return;
  28         if (IS_A_PTY_MASTER(tty->line)) {
  29                 if (tty->count > 1)
  30                         return;
  31         } else {
  32                 if (tty->count > 2)
  33                         return;
  34         }
  35         wake_up_interruptible(&tty->secondary.proc_list);
  36         wake_up_interruptible(&tty->read_q.proc_list);
  37         wake_up_interruptible(&tty->write_q.proc_list);
  38         if (!tty->link)
  39                 return;
  40         wake_up_interruptible(&tty->link->secondary.proc_list);
  41         wake_up_interruptible(&tty->link->read_q.proc_list);
  42         wake_up_interruptible(&tty->link->write_q.proc_list);
  43         if (IS_A_PTY_MASTER(tty->line)) {
  44                 tty_hangup(tty->link);
  45                 flush_input(tty);
  46                 flush_output(tty);
  47         }
  48 }
  49 
  50 static inline void pty_copy(struct tty_struct * from, struct tty_struct * to)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52         int c;
  53 
  54         while (!from->stopped && !EMPTY(&from->write_q)) {
  55                 if (FULL(&to->read_q)) {
  56                         if (FULL(&to->secondary))
  57                                 break;
  58                         TTY_READ_FLUSH(to);
  59                         continue;
  60                 }
  61                 c = get_tty_queue(&from->write_q);
  62                 put_tty_queue(c, &to->read_q);
  63                 if (current->signal & ~current->blocked)
  64                         break;
  65         }
  66         TTY_READ_FLUSH(to);
  67         wake_up_interruptible(&from->write_q.proc_list);
  68         if (from->write_data_cnt) {
  69                 set_bit(from->line, &tty_check_write);
  70                 mark_bh(TTY_BH);
  71         }
  72 }
  73 
  74 /*
  75  * This routine gets called when tty_write has put something into
  76  * the write_queue. It copies the input to the output-queue of it's
  77  * slave.
  78  */
  79 static void pty_write(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81         if (tty->link)
  82                 pty_copy(tty,tty->link);
  83 }
  84 
  85 int pty_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
  86 {
  87         if (!tty || !tty->link)
  88                 return -ENODEV;
  89         if (IS_A_PTY_MASTER(tty->line))
  90                 clear_bit(TTY_SLAVE_OPENED, &tty->flags);
  91         else
  92                 set_bit(TTY_SLAVE_OPENED, &tty->link->flags);
  93         tty->write = tty->link->write = pty_write;
  94         tty->close = tty->link->close = pty_close;
  95         wake_up_interruptible(&tty->read_q.proc_list);
  96         if (filp->f_flags & O_NDELAY)
  97                 return 0;
  98         while (!tty->link->count && !(current->signal & ~current->blocked))
  99                 interruptible_sleep_on(&tty->link->read_q.proc_list);
 100         if (!tty->link->count)
 101                 return -ERESTARTSYS;
 102         return 0;
 103 }

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