root/drivers/char/pty.c

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

DEFINITIONS

This source file includes following definitions.
  1. pty_close
  2. pty_unthrottle
  3. pty_write
  4. pty_write_room
  5. pty_chars_in_buffer
  6. pty_flush_buffer
  7. pty_open
  8. pty_init

   1 /*
   2  *  linux/drivers/char/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/interrupt.h>
  18 #include <linux/tty.h>
  19 #include <linux/tty_flip.h>
  20 #include <linux/fcntl.h>
  21 #include <linux/string.h>
  22 #include <linux/major.h>
  23 #include <linux/mm.h>
  24 
  25 #include <asm/segment.h>
  26 #include <asm/system.h>
  27 #include <asm/bitops.h>
  28 
  29 struct pty_struct {
  30         int     magic;
  31         struct wait_queue * open_wait;
  32 };
  33 
  34 #define PTY_MAGIC 0x5001
  35 
  36 #define PTY_BUF_SIZE PAGE_SIZE/2
  37 
  38 /*
  39  * tmp_buf is used as a temporary buffer by pty_write.  We need to
  40  * lock it in case the memcpy_fromfs blocks while swapping in a page,
  41  * and some other program tries to do a pty write at the same time.
  42  * Since the lock will only come under contention when the system is
  43  * swapping and available memory is low, it makes sense to share one
  44  * buffer across all the PTY's, since it significantly saves memory if
  45  * large numbers of PTY's are open.
  46  */
  47 static unsigned char *tmp_buf;
  48 static struct semaphore tmp_buf_sem = MUTEX;
  49 
  50 struct tty_driver pty_driver, pty_slave_driver;
  51 struct tty_driver old_pty_driver, old_pty_slave_driver;
  52 static int pty_refcount;
  53 
  54 static struct tty_struct *pty_table[NR_PTYS];
  55 static struct termios *pty_termios[NR_PTYS];
  56 static struct termios *pty_termios_locked[NR_PTYS];
  57 static struct tty_struct *ttyp_table[NR_PTYS];
  58 static struct termios *ttyp_termios[NR_PTYS];
  59 static struct termios *ttyp_termios_locked[NR_PTYS];
  60 static struct pty_struct pty_state[NR_PTYS];
  61 
  62 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
  63 
  64 static void pty_close(struct tty_struct * tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
  65 {
  66         if (!tty)
  67                 return;
  68         if (tty->driver.subtype == PTY_TYPE_MASTER) {
  69                 if (tty->count > 1)
  70                         printk("master pty_close: count = %d!!\n", tty->count);
  71         } else {
  72                 if (tty->count > 2)
  73                         return;
  74         }
  75         wake_up_interruptible(&tty->read_wait);
  76         wake_up_interruptible(&tty->write_wait);
  77         if (!tty->link)
  78                 return;
  79         wake_up_interruptible(&tty->link->read_wait);
  80         wake_up_interruptible(&tty->link->write_wait);
  81         if (tty->driver.subtype == PTY_TYPE_MASTER) {
  82                 tty_hangup(tty->link);
  83                 set_bit(TTY_SLAVE_CLOSED, &tty->flags);
  84         } else {
  85                 start_tty(tty);
  86                 set_bit(TTY_SLAVE_CLOSED, &tty->link->flags);
  87         }
  88 }
  89 
  90 /*
  91  * The unthrottle routine is called by the line discipline to signal
  92  * that it can receive more characters.  For PTY's, the TTY_THROTTLED
  93  * flag is always set, to force the line discipline to always call the
  94  * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE 
  95  * characters in the queue.  This is necessary since each time this
  96  * happens, we need to wake up any sleeping processes that could be
  97  * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  98  * for the pty buffer to be drained.
  99  */
 100 static void pty_unthrottle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 101 {
 102         struct tty_struct *o_tty = tty->link;
 103 
 104         if (!o_tty)
 105                 return;
 106 
 107         if ((o_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
 108             o_tty->ldisc.write_wakeup)
 109                 (o_tty->ldisc.write_wakeup)(o_tty);
 110         wake_up_interruptible(&o_tty->write_wait);
 111         set_bit(TTY_THROTTLED, &tty->flags);
 112 }
 113 
 114 static int pty_write(struct tty_struct * tty, int from_user,
     /* [previous][next][first][last][top][bottom][index][help] */
 115                        const unsigned char *buf, int count)
 116 {
 117         struct tty_struct *to = tty->link;
 118         int     c=0, n, r;
 119         char    *temp_buffer;
 120 
 121         if (!to || tty->stopped)
 122                 return 0;
 123         
 124         if (from_user) {
 125                 down(&tmp_buf_sem);
 126                 temp_buffer = tmp_buf +
 127                         ((tty->driver.subtype-1) * PTY_BUF_SIZE);
 128                 while (count > 0) {
 129                         n = MIN(count, PTY_BUF_SIZE);
 130                         memcpy_fromfs(temp_buffer, buf, n);
 131                         r = to->ldisc.receive_room(to);
 132                         if (r <= 0)
 133                                 break;
 134                         n = MIN(n, r);
 135                         to->ldisc.receive_buf(to, temp_buffer, 0, n);
 136                         buf += n;  c+= n;
 137                         count -= n;
 138                 }
 139                 up(&tmp_buf_sem);
 140         } else {
 141                 c = MIN(count, to->ldisc.receive_room(to));
 142                 to->ldisc.receive_buf(to, buf, 0, c);
 143         }
 144         
 145         return c;
 146 }
 147 
 148 static int pty_write_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 149 {
 150         struct tty_struct *to = tty->link;
 151 
 152         if (!to || tty->stopped)
 153                 return 0;
 154 
 155         return to->ldisc.receive_room(to);
 156 }
 157 
 158 static int pty_chars_in_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 159 {
 160         struct tty_struct *to = tty->link;
 161 
 162         if (!to || !to->ldisc.chars_in_buffer)
 163                 return 0;
 164 
 165         return to->ldisc.chars_in_buffer(to);
 166 }
 167 
 168 static void pty_flush_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 169 {
 170         struct tty_struct *to = tty->link;
 171         
 172         if (!to)
 173                 return;
 174         
 175         if (to->ldisc.flush_buffer)
 176                 to->ldisc.flush_buffer(to);
 177         
 178         if (to->packet) {
 179                 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
 180                 wake_up_interruptible(&to->read_wait);
 181         }
 182 }
 183 
 184 int pty_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 185 {
 186         int     line;
 187         struct  pty_struct *pty;
 188         
 189         if (!tty || !tty->link)
 190                 return -ENODEV;
 191         line = MINOR(tty->device) - tty->driver.minor_start;
 192         if ((line < 0) || (line >= NR_PTYS))
 193                 return -ENODEV;
 194         pty = pty_state + line;
 195         tty->driver_data = pty;
 196 
 197         if (!tmp_buf) {
 198                 unsigned long page = get_free_page(GFP_KERNEL);
 199                 if (!tmp_buf) {
 200                         if (!page)
 201                                 return -ENOMEM;
 202                         tmp_buf = (unsigned char *) page;
 203                 } else
 204                         free_page(page);
 205         }
 206 
 207         if (tty->driver.subtype == PTY_TYPE_SLAVE)
 208                 clear_bit(TTY_SLAVE_CLOSED, &tty->link->flags);
 209         wake_up_interruptible(&pty->open_wait);
 210         set_bit(TTY_THROTTLED, &tty->flags);
 211         if (filp->f_flags & O_NDELAY)
 212                 return 0;
 213         while (test_bit(TTY_SLAVE_CLOSED, &tty->link->flags) &&
 214                !(current->signal & ~current->blocked))
 215                 interruptible_sleep_on(&pty->open_wait);
 216         if (!tty->link->count)
 217                 return -ERESTARTSYS;
 218         return 0;
 219 }
 220 
 221 int pty_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 222 {
 223         memset(&pty_state, 0, sizeof(pty_state));
 224         memset(&pty_driver, 0, sizeof(struct tty_driver));
 225         pty_driver.magic = TTY_DRIVER_MAGIC;
 226         pty_driver.name = "pty";
 227         pty_driver.major = PTY_MASTER_MAJOR;
 228         pty_driver.minor_start = 0;
 229         pty_driver.num = NR_PTYS;
 230         pty_driver.type = TTY_DRIVER_TYPE_PTY;
 231         pty_driver.subtype = PTY_TYPE_MASTER;
 232         pty_driver.init_termios = tty_std_termios;
 233         pty_driver.init_termios.c_iflag = 0;
 234         pty_driver.init_termios.c_oflag = 0;
 235         pty_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
 236         pty_driver.init_termios.c_lflag = 0;
 237         pty_driver.flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
 238         pty_driver.refcount = &pty_refcount;
 239         pty_driver.table = pty_table;
 240         pty_driver.termios = pty_termios;
 241         pty_driver.termios_locked = pty_termios_locked;
 242         pty_driver.other = &pty_slave_driver;
 243 
 244         pty_driver.open = pty_open;
 245         pty_driver.close = pty_close;
 246         pty_driver.write = pty_write;
 247         pty_driver.write_room = pty_write_room;
 248         pty_driver.flush_buffer = pty_flush_buffer;
 249         pty_driver.chars_in_buffer = pty_chars_in_buffer;
 250         pty_driver.unthrottle = pty_unthrottle;
 251 
 252         pty_slave_driver = pty_driver;
 253         pty_slave_driver.name = "ttyp";
 254         pty_slave_driver.subtype = PTY_TYPE_SLAVE;
 255         pty_slave_driver.major = PTY_SLAVE_MAJOR;
 256         pty_slave_driver.minor_start = 0;
 257         pty_slave_driver.init_termios = tty_std_termios;
 258         pty_slave_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
 259         pty_slave_driver.table = ttyp_table;
 260         pty_slave_driver.termios = ttyp_termios;
 261         pty_slave_driver.termios_locked = ttyp_termios_locked;
 262         pty_slave_driver.other = &pty_driver;
 263 
 264         old_pty_driver = pty_driver;
 265         old_pty_driver.major = TTY_MAJOR;
 266         old_pty_driver.minor_start = 128;
 267         old_pty_driver.num = (NR_PTYS > 64) ? 64 : NR_PTYS;
 268         old_pty_driver.other = &old_pty_slave_driver;
 269         
 270         old_pty_slave_driver = pty_slave_driver;
 271         old_pty_slave_driver.major = TTY_MAJOR;
 272         old_pty_slave_driver.minor_start = 192;
 273         old_pty_slave_driver.num = (NR_PTYS > 64) ? 64 : NR_PTYS;
 274         old_pty_slave_driver.other = &old_pty_driver;
 275 
 276         tmp_buf = 0;
 277 
 278         if (tty_register_driver(&pty_driver))
 279                 panic("Couldn't register pty driver");
 280         if (tty_register_driver(&pty_slave_driver))
 281                 panic("Couldn't register pty slave driver");
 282         if (tty_register_driver(&old_pty_driver))
 283                 panic("Couldn't register compat pty driver");
 284         if (tty_register_driver(&old_pty_slave_driver))
 285                 panic("Couldn't register compat pty slave driver");
 286         
 287         return 0;
 288 }

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