tty: implement system 5 style process groups
authorAlan Cox <alan@linux.intel.com>
Thu, 21 May 2015 22:03:05 +0000 (23:03 +0100)
committerAlan Cox <alan@linux.intel.com>
Thu, 21 May 2015 22:03:05 +0000 (23:03 +0100)
This completes (I think) the sketched out implementation in the code.

Kernel/include/tty.h
Kernel/process.c
Kernel/start.c
Kernel/syscall_proc.c
Kernel/tty.c

index 7adee42..1a87684 100644 (file)
@@ -172,6 +172,7 @@ extern int tty_read(uint8_t minor, uint8_t rawflag, uint8_t flag);
 extern int tty_write(uint8_t minor, uint8_t rawflag, uint8_t flag);
 extern int tty_open(uint8_t minor, uint16_t flag);
 extern int tty_close(uint8_t minor);
+extern void tty_exit(void);
 extern int tty_ioctl(uint8_t minor, uarg_t request, char *data);
 
 extern void tty_hangup(uint8_t minor);
index 1474962..cea8c33 100644 (file)
@@ -3,6 +3,7 @@
 #undef DEBUGREALLYHARD         /* turn on getproc dumping */
 
 #include <kernel.h>
+#include <tty.h>
 #include <kdata.h>
 #include <printf.h>
 #include <audio.h>
@@ -277,6 +278,7 @@ ptptr ptab_alloc(void)
                        udata.u_error = ENOMEM;
                        newp = NULL;
                 }
+                newp->p_pgrp = udata.u_ptab->p_pgrp;
        }
        irqrestore(irq);
        if (newp)
@@ -585,12 +587,20 @@ void doexit(int16_t val, int16_t val2)
        memcpy(&(udata.u_ptab->p_priority), &udata.u_utime,
               2 * sizeof(clock_t));
 
-       /* See if we have any children. Set child's parents to our parent */
        for (p = ptab; p < ptab_end; ++p) {
-               if (p->p_status && p->p_pptr == udata.u_ptab
-                   && p != udata.u_ptab)
+               if (p == udata.u_ptab)
+                       continue;
+               /* Set any child's parents to our parent */
+               if (p->p_status && p->p_pptr == udata.u_ptab)
                        p->p_pptr = udata.u_ptab->p_pptr;
+               /* Send SIGHUP to any pgrp members and remove
+                  them from our pgrp */
+                if (p->p_pgrp == udata.u_ptab->p_pid) {
+                       p->p_pgrp = 0;
+                       ssig(p, SIGHUP);
+               }
        }
+       tty_exit();
        irqrestore(irq);
 #ifdef DEBUG
        kprintf
index 7bdeaa2..ee8f6ee 100644 (file)
@@ -242,7 +242,7 @@ void fuzix_main(void)
 
        tty_init();
 
-       if (d_open(TTYDEV, 0) != 0)
+       if (d_open(TTYDEV, O_NOCTTY) != 0)
                panic("no tty");
 
        /* Sign on messages */
index e3244d6..dfce06f 100644 (file)
@@ -549,6 +549,7 @@ setpgrp (void)                    Function 53
 arg_t _setpgrp(void)
 {
        udata.u_ptab->p_pgrp = udata.u_ptab->p_pid;
+       udata.u_ptab->p_tty = 0;
        return (0);
 }
 
index 0b44630..5ee924f 100644 (file)
@@ -167,9 +167,8 @@ int tty_open(uint8_t minor, uint16_t flag)
                return -1;
         }
 
-
        /* If there is no controlling tty for the process, establish it */
-       if (!udata.u_ptab->p_tty && !(flag & O_NOCTTY)) {
+       if (!udata.u_ptab->p_tty && !t->pgrp && !(flag & O_NOCTTY)) {
                udata.u_ptab->p_tty = minor;
                t->pgrp = udata.u_ptab->p_pgrp;
        }
@@ -194,13 +193,24 @@ int tty_open(uint8_t minor, uint16_t flag)
 int tty_close(uint8_t minor)
 {
        /* 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;
+        }
         /* If we were hung up then the last opener has gone away */
         ttydata[minor].flag &= ~TTYF_DEAD;
        return (0);
 }
 
+/* If the group owner for the tty dies, the tty loses its group */
+void tty_exit(void)
+{
+        uint8_t t = udata.u_ptab->p_tty;
+        uint16_t *pgrp = &ttydata[t].pgrp;
+        if (t && *pgrp == udata.u_ptab->p_pgrp && *pgrp == udata.u_ptab->p_pid)
+                *pgrp = 0;
+}
+
 int tty_ioctl(uint8_t minor, uarg_t request, char *data)
 {                              /* Data in User Space */
         struct tty *t;
@@ -412,6 +422,7 @@ void tty_hangup(uint8_t minor)
         struct tty *t = &ttydata[minor];
         /* Kill users */
         sgrpsig(t->pgrp, SIGHUP);
+        t->pgrp = 0;
         /* Stop any new I/O with errors */
         t->flag |= TTYF_DEAD;
         /* Wake up read/write */