From 852c749a5e16e374087bc2408306c171729b56b5 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Thu, 21 May 2015 23:03:05 +0100 Subject: [PATCH] tty: implement system 5 style process groups This completes (I think) the sketched out implementation in the code. --- Kernel/include/tty.h | 1 + Kernel/process.c | 16 +++++++++++++--- Kernel/start.c | 2 +- Kernel/syscall_proc.c | 1 + Kernel/tty.c | 17 ++++++++++++++--- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Kernel/include/tty.h b/Kernel/include/tty.h index 7adee42d..1a87684d 100644 --- a/Kernel/include/tty.h +++ b/Kernel/include/tty.h @@ -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); diff --git a/Kernel/process.c b/Kernel/process.c index 14749626..cea8c33a 100644 --- a/Kernel/process.c +++ b/Kernel/process.c @@ -3,6 +3,7 @@ #undef DEBUGREALLYHARD /* turn on getproc dumping */ #include +#include #include #include #include @@ -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 diff --git a/Kernel/start.c b/Kernel/start.c index 7bdeaa29..ee8f6eec 100644 --- a/Kernel/start.c +++ b/Kernel/start.c @@ -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 */ diff --git a/Kernel/syscall_proc.c b/Kernel/syscall_proc.c index e3244d6d..dfce06f7 100644 --- a/Kernel/syscall_proc.c +++ b/Kernel/syscall_proc.c @@ -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); } diff --git a/Kernel/tty.c b/Kernel/tty.c index 0b446303..5ee924f4 100644 --- a/Kernel/tty.c +++ b/Kernel/tty.c @@ -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 */ -- 2.34.1