VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / char / tty_io.c
index 0c76237..91f530c 100644 (file)
@@ -91,6 +91,7 @@
 #include <linux/module.h>
 #include <linux/smp_lock.h>
 #include <linux/device.h>
+#include <linux/idr.h>
 
 #include <asm/uaccess.h>
 #include <asm/system.h>
@@ -128,6 +129,8 @@ DECLARE_MUTEX(tty_sem);
 #ifdef CONFIG_UNIX98_PTYS
 extern struct tty_driver *ptm_driver;  /* Unix98 pty masters; for /dev/ptmx */
 extern int pty_limit;          /* Config limit on Unix98 ptys */
+static DEFINE_IDR(allocated_ptys);
+static DECLARE_MUTEX(allocated_ptys_lock);
 #endif
 
 extern void disable_early_printk(void);
@@ -342,18 +345,12 @@ EXPORT_SYMBOL(tty_check_change);
 static ssize_t hung_up_tty_read(struct file * file, char __user * buf,
                                size_t count, loff_t *ppos)
 {
-       /* Can't seek (pread) on ttys.  */
-       if (ppos != &file->f_pos)
-               return -ESPIPE;
        return 0;
 }
 
 static ssize_t hung_up_tty_write(struct file * file, const char __user * buf,
                                 size_t count, loff_t *ppos)
 {
-       /* Can't seek (pwrite) on ttys.  */
-       if (ppos != &file->f_pos)
-               return -ESPIPE;
        return -EIO;
 }
 
@@ -645,10 +642,6 @@ static ssize_t tty_read(struct file * file, char __user * buf, size_t count,
        struct tty_struct * tty;
        struct inode *inode;
 
-       /* Can't seek (pread) on ttys.  */
-       if (ppos != &file->f_pos)
-               return -ESPIPE;
-
        tty = (struct tty_struct *)file->private_data;
        inode = file->f_dentry->d_inode;
        if (tty_paranoia_check(tty, inode, "tty_read"))
@@ -723,10 +716,6 @@ static ssize_t tty_write(struct file * file, const char __user * buf, size_t cou
        struct tty_struct * tty;
        struct inode *inode = file->f_dentry->d_inode;
 
-       /* Can't seek (pwrite) on ttys.  */
-       if (ppos != &file->f_pos)
-               return -ESPIPE;
-
        tty = (struct tty_struct *)file->private_data;
        if (tty_paranoia_check(tty, inode, "tty_write"))
                return -EIO;
@@ -752,9 +741,6 @@ ssize_t redirected_tty_write(struct file * file, const char __user * buf, size_t
 
        if (p) {
                ssize_t res;
-               /* Can't seek (pwrite) on ttys.  */
-               if (ppos != &file->f_pos)
-                       return -ESPIPE;
                res = vfs_write(p, buf, count, &p->f_pos);
                fput(p);
                return res;
@@ -1065,6 +1051,7 @@ static void release_dev(struct file * filp)
 {
        struct tty_struct *tty, *o_tty;
        int     pty_master, tty_closing, o_tty_closing, do_sleep;
+       int     devpts_master, devpts;
        int     idx;
        char    buf[64];
        
@@ -1079,6 +1066,8 @@ static void release_dev(struct file * filp)
        idx = tty->index;
        pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
                      tty->driver->subtype == PTY_TYPE_MASTER);
+       devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
+       devpts_master = pty_master && devpts;
        o_tty = tty->link;
 
 #ifdef TTY_PARANOIA_CHECK
@@ -1295,11 +1284,21 @@ static void release_dev(struct file * filp)
                o_tty->ldisc = ldiscs[N_TTY];
        }
 
-       /* 
+       /*
         * The release_mem function takes care of the details of clearing
         * the slots and preserving the termios structure.
         */
        release_mem(tty, idx);
+
+#ifdef CONFIG_UNIX98_PTYS
+       /* Make this pty number available for reallocation */
+       if (devpts) {
+               down(&allocated_ptys_lock);
+               idr_remove(&allocated_ptys, idx);
+               up(&allocated_ptys_lock);
+       }
+#endif
+
 }
 
 /*
@@ -1322,8 +1321,13 @@ static int tty_open(struct inode * inode, struct file * filp)
        int index;
        dev_t device = inode->i_rdev;
        unsigned short saved_flags = filp->f_flags;
+
+       nonseekable_open(inode, filp);
 retry_open:
        noctty = filp->f_flags & O_NOCTTY;
+       index  = -1;
+       retval = 0;
+
        if (device == MKDEV(TTYAUX_MAJOR,0)) {
                if (!current->signal->tty)
                        return -ENXIO;
@@ -1344,13 +1348,8 @@ retry_open:
        }
 #endif
        if (device == MKDEV(TTYAUX_MAJOR,1)) {
-               struct console *c = console_drivers;
-               for (c = console_drivers; c; c = c->next) {
-                       if (!c->device)
-                               continue;
-                       driver = c->device(c, &index);
-                       if (!driver)
-                               continue;
+               driver = console_device(&index);
+               if (driver) {
                        /* Don't let /dev/console block */
                        filp->f_flags |= O_NONBLOCK;
                        noctty = 1;
@@ -1361,24 +1360,40 @@ retry_open:
 
 #ifdef CONFIG_UNIX98_PTYS
        if (device == MKDEV(TTYAUX_MAJOR,2)) {
+               int idr_ret;
+
                /* find a device that is not in use. */
-               static int next_ptmx_dev = 0;
-               retval = -1;
+               down(&allocated_ptys_lock);
+               if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
+                       up(&allocated_ptys_lock);
+                       return -ENOMEM;
+               }
+               idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
+               if (idr_ret < 0) {
+                       up(&allocated_ptys_lock);
+                       if (idr_ret == -EAGAIN)
+                               return -ENOMEM;
+                       return -EIO;
+               }
+               if (index >= pty_limit) {
+                       idr_remove(&allocated_ptys, index);
+                       up(&allocated_ptys_lock);
+                       return -EIO;
+               }
+               up(&allocated_ptys_lock);
+
                driver = ptm_driver;
-               while (driver->refcount < pty_limit) {
-                       index = next_ptmx_dev;
-                       next_ptmx_dev = (next_ptmx_dev+1) % driver->num;
-                       if (!init_dev(driver, index, &tty))
-                               goto ptmx_found; /* ok! */
+               retval = init_dev(driver, index, &tty);
+               if (retval) {
+                       down(&allocated_ptys_lock);
+                       idr_remove(&allocated_ptys, index);
+                       up(&allocated_ptys_lock);
+                       return retval;
                }
-               return -EIO; /* no free ptys */
-       ptmx_found:
+
                set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
-               if (devpts_pty_new(tty->link)) {
-                       /* BADNESS - need to destroy both ptm and pts! */
-                       return -ENOMEM;
-               }
-               noctty = 1;
+               if (devpts_pty_new(tty->link))
+                       retval = -ENOMEM;
        } else
 #endif
        {
@@ -1400,10 +1415,12 @@ got_driver:
 #ifdef TTY_DEBUG_HANGUP
        printk(KERN_DEBUG "opening %s...", tty->name);
 #endif
-       if (tty->driver->open)
-               retval = tty->driver->open(tty, filp);
-       else
-               retval = -ENODEV;
+       if (!retval) {
+               if (tty->driver->open)
+                       retval = tty->driver->open(tty, filp);
+               else
+                       retval = -ENODEV;
+       }
        filp->f_flags = saved_flags;
 
        if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
@@ -1415,6 +1432,14 @@ got_driver:
                       tty->name);
 #endif
 
+#ifdef CONFIG_UNIX98_PTYS
+               if (index != -1) {
+                       down(&allocated_ptys_lock);
+                       idr_remove(&allocated_ptys, index);
+                       up(&allocated_ptys_lock);
+               }
+#endif
+
                release_dev(filp);
                if (retval != -ERESTARTSYS)
                        return retval;