tty: handle close better
authorAlan Cox <alan@linux.intel.com>
Thu, 21 May 2015 22:58:49 +0000 (23:58 +0100)
committerAlan Cox <alan@linux.intel.com>
Thu, 21 May 2015 22:58:49 +0000 (23:58 +0100)
Kernel/include/tty.h
Kernel/tty.c

index 1a87684..4702018 100644 (file)
@@ -151,10 +151,10 @@ struct termios {
    the data indexed off a single register */
 struct tty {
     /* Put flag first: makes it cheaper when short of registers */
-    uint8_t flag;              /* Use uint8 pad - makes the whole struct
+    uint8_t flag;              /* make the whole struct
                                    24 byte - a nice number for CPUs with no 
                                    multiplier */
-    uint8_t pad0;
+    uint8_t users;
 #define TTYF_STOP      1
 #define TTYF_DISCARD   2
 #define TTYF_DEAD      4
index 5ee924f..61e9ab7 100644 (file)
@@ -172,6 +172,10 @@ int tty_open(uint8_t minor, uint16_t flag)
                udata.u_ptab->p_tty = minor;
                t->pgrp = udata.u_ptab->p_pgrp;
        }
+       if (t->users) {
+               t->users++;
+               return 0;
+        }
        tty_setup(minor);
        if ((t->termios.c_cflag & CLOCAL) || (flag & O_NDELAY))
                return 0;
@@ -187,18 +191,21 @@ int tty_open(uint8_t minor, uint16_t flag)
                 t->flag &= ~TTYF_DEAD;
                 return -1;
         }
+        t->users++;
         return 0;
 }
 
 int tty_close(uint8_t minor)
 {
+        struct tty *t = &ttydata[minor];
+        if (--t->users)
+                return 0;
        /* If we are closing the controlling tty, make note */
-       if (minor == udata.u_ptab->p_tty) {
+       if (minor == udata.u_ptab->p_tty)
                udata.u_ptab->p_tty = 0;
-               ttydata[minor].pgrp = 0;
-        }
+       t->pgrp = 0;
         /* If we were hung up then the last opener has gone away */
-        ttydata[minor].flag &= ~TTYF_DEAD;
+        t->flag &= ~TTYF_DEAD;
        return (0);
 }