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         set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  82         if (tty->driver.subtype == PTY_TYPE_MASTER) {
  83                 tty_hangup(tty->link);
  84                 set_bit(TTY_OTHER_CLOSED, &tty->flags);
  85         }
  86 }
  87 
  88 /*
  89  * The unthrottle routine is called by the line discipline to signal
  90  * that it can receive more characters.  For PTY's, the TTY_THROTTLED
  91  * flag is always set, to force the line discipline to always call the
  92  * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE 
  93  * characters in the queue.  This is necessary since each time this
  94  * happens, we need to wake up any sleeping processes that could be
  95  * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  96  * for the pty buffer to be drained.
  97  */
  98 static void pty_unthrottle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
  99 {
 100         struct tty_struct *o_tty = tty->link;
 101 
 102         if (!o_tty)
 103                 return;
 104 
 105         if ((o_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
 106             o_tty->ldisc.write_wakeup)
 107                 (o_tty->ldisc.write_wakeup)(o_tty);
 108         wake_up_interruptible(&o_tty->write_wait);
 109         set_bit(TTY_THROTTLED, &tty->flags);
 110 }
 111 
 112 static int pty_write(struct tty_struct * tty, int from_user,
     /* [previous][next][first][last][top][bottom][index][help] */
 113                        const unsigned char *buf, int count)
 114 {
 115         struct tty_struct *to = tty->link;
 116         int     c=0, n, r;
 117         char    *temp_buffer;
 118 
 119         if (!to || tty->stopped)
 120                 return 0;
 121         
 122         if (from_user) {
 123                 down(&tmp_buf_sem);
 124                 temp_buffer = tmp_buf +
 125                         ((tty->driver.subtype-1) * PTY_BUF_SIZE);
 126                 while (count > 0) {
 127                         n = MIN(count, PTY_BUF_SIZE);
 128                         memcpy_fromfs(temp_buffer, buf, n);
 129                         r = to->ldisc.receive_room(to);
 130                         if (r <= 0)
 131                                 break;
 132                         n = MIN(n, r);
 133                         to->ldisc.receive_buf(to, temp_buffer, 0, n);
 134                         buf += n;  c+= n;
 135                         count -= n;
 136                 }
 137                 up(&tmp_buf_sem);
 138         } else {
 139                 c = MIN(count, to->ldisc.receive_room(to));
 140                 to->ldisc.receive_buf(to, buf, 0, c);
 141         }
 142         
 143         return c;
 144 }
 145 
 146 static int pty_write_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148         struct tty_struct *to = tty->link;
 149 
 150         if (!to || tty->stopped)
 151                 return 0;
 152 
 153         return to->ldisc.receive_room(to);
 154 }
 155 
 156 static int pty_chars_in_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 157 {
 158         struct tty_struct *to = tty->link;
 159 
 160         if (!to || !to->ldisc.chars_in_buffer)
 161                 return 0;
 162 
 163         return to->ldisc.chars_in_buffer(to);
 164 }
 165 
 166 static void pty_flush_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168         struct tty_struct *to = tty->link;
 169         
 170         if (!to)
 171                 return;
 172         
 173         if (to->ldisc.flush_buffer)
 174                 to->ldisc.flush_buffer(to);
 175         
 176         if (to->packet) {
 177                 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
 178                 wake_up_interruptible(&to->read_wait);
 179         }
 180 }
 181 
 182 int pty_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 183 {
 184         struct wait_queue wait = { current, NULL };
 185         int     retval;
 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         clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
 208         wake_up_interruptible(&pty->open_wait);
 209         set_bit(TTY_THROTTLED, &tty->flags);
 210         if (filp->f_flags & O_NDELAY)
 211                 return 0;
 212         /*
 213          * If we're opening the master pty, just return.  If we're
 214          * trying to open the slave pty, then we have to wait for the
 215          * master pty to open.
 216          */
 217         if (tty->driver.subtype == PTY_TYPE_MASTER)
 218                 return 0;
 219         retval = 0;
 220 #if PTY_SLAVE_WAITS_ON_OPEN
 221         add_wait_queue(&pty->open_wait, &wait);
 222         while (1) {
 223                 if (current->signal & ~current->blocked) {
 224                         retval = -ERESTARTSYS;
 225                         break;
 226                 }
 227                 /*
 228                  * Block until the master is open...
 229                  */
 230                 current->state = TASK_INTERRUPTIBLE;
 231                 if (tty->link->count &&
 232                     !test_bit(TTY_OTHER_CLOSED, &tty->flags))
 233                         break;
 234                 schedule();
 235         }
 236         current->state = TASK_RUNNING;
 237         remove_wait_queue(&pty->open_wait, &wait);
 238 #else
 239         if (!tty->link->count || test_bit(TTY_OTHER_CLOSED, &tty->flags))
 240                 retval = -EPERM;
 241 #endif
 242         return retval;
 243 }
 244 
 245 int pty_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 246 {
 247         memset(&pty_state, 0, sizeof(pty_state));
 248         memset(&pty_driver, 0, sizeof(struct tty_driver));
 249         pty_driver.magic = TTY_DRIVER_MAGIC;
 250         pty_driver.name = "pty";
 251         pty_driver.major = PTY_MASTER_MAJOR;
 252         pty_driver.minor_start = 0;
 253         pty_driver.num = NR_PTYS;
 254         pty_driver.type = TTY_DRIVER_TYPE_PTY;
 255         pty_driver.subtype = PTY_TYPE_MASTER;
 256         pty_driver.init_termios = tty_std_termios;
 257         pty_driver.init_termios.c_iflag = 0;
 258         pty_driver.init_termios.c_oflag = 0;
 259         pty_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
 260         pty_driver.init_termios.c_lflag = 0;
 261         pty_driver.flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
 262         pty_driver.refcount = &pty_refcount;
 263         pty_driver.table = pty_table;
 264         pty_driver.termios = pty_termios;
 265         pty_driver.termios_locked = pty_termios_locked;
 266         pty_driver.other = &pty_slave_driver;
 267 
 268         pty_driver.open = pty_open;
 269         pty_driver.close = pty_close;
 270         pty_driver.write = pty_write;
 271         pty_driver.write_room = pty_write_room;
 272         pty_driver.flush_buffer = pty_flush_buffer;
 273         pty_driver.chars_in_buffer = pty_chars_in_buffer;
 274         pty_driver.unthrottle = pty_unthrottle;
 275 
 276         pty_slave_driver = pty_driver;
 277         pty_slave_driver.name = "ttyp";
 278         pty_slave_driver.subtype = PTY_TYPE_SLAVE;
 279         pty_slave_driver.major = PTY_SLAVE_MAJOR;
 280         pty_slave_driver.minor_start = 0;
 281         pty_slave_driver.init_termios = tty_std_termios;
 282         pty_slave_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
 283         pty_slave_driver.table = ttyp_table;
 284         pty_slave_driver.termios = ttyp_termios;
 285         pty_slave_driver.termios_locked = ttyp_termios_locked;
 286         pty_slave_driver.other = &pty_driver;
 287 
 288         old_pty_driver = pty_driver;
 289         old_pty_driver.major = TTY_MAJOR;
 290         old_pty_driver.minor_start = 128;
 291         old_pty_driver.num = (NR_PTYS > 64) ? 64 : NR_PTYS;
 292         old_pty_driver.other = &old_pty_slave_driver;
 293         
 294         old_pty_slave_driver = pty_slave_driver;
 295         old_pty_slave_driver.major = TTY_MAJOR;
 296         old_pty_slave_driver.minor_start = 192;
 297         old_pty_slave_driver.num = (NR_PTYS > 64) ? 64 : NR_PTYS;
 298         old_pty_slave_driver.other = &old_pty_driver;
 299 
 300         tmp_buf = 0;
 301 
 302         if (tty_register_driver(&pty_driver))
 303                 panic("Couldn't register pty driver");
 304         if (tty_register_driver(&pty_slave_driver))
 305                 panic("Couldn't register pty slave driver");
 306         if (tty_register_driver(&old_pty_driver))
 307                 panic("Couldn't register compat pty driver");
 308         if (tty_register_driver(&old_pty_slave_driver))
 309                 panic("Couldn't register compat pty slave driver");
 310         
 311         return 0;
 312 }

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