POSIX changes
authorMike Karels <karels@ucbvax.Berkeley.EDU>
Sun, 12 Nov 1989 02:31:24 +0000 (18:31 -0800)
committerMike Karels <karels@ucbvax.Berkeley.EDU>
Sun, 12 Nov 1989 02:31:24 +0000 (18:31 -0800)
SCCS-vsn: sys/kern/kern_sig.c 7.11

usr/src/sys/kern/kern_sig.c

index 58b9902..0f68ca9 100644 (file)
  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
- *     @(#)kern_sig.c  7.10 (Berkeley) %G%
+ *     @(#)kern_sig.c  7.11 (Berkeley) %G%
  */
 
 #include "param.h"
 #include "systm.h"
  */
 
 #include "param.h"
 #include "systm.h"
-#include "user.h"
+#include "syscontext.h"        /* XXX */
 #include "vnode.h"
 #include "proc.h"
 #include "timeb.h"
 #include "vnode.h"
 #include "proc.h"
 #include "timeb.h"
 #include "uio.h"
 #include "file.h"
 #include "kernel.h"
 #include "uio.h"
 #include "file.h"
 #include "kernel.h"
+#include "wait.h"
 
 #include "machine/reg.h"
 #include "machine/pte.h"
 #include "machine/psl.h"
 #include "machine/mtpr.h"
 
 
 #include "machine/reg.h"
 #include "machine/pte.h"
 #include "machine/psl.h"
 #include "machine/mtpr.h"
 
-#define        cantmask        (sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP))
 #define        stopsigmask     (sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
                        sigmask(SIGTTIN)|sigmask(SIGTTOU))
 #define        stopsigmask     (sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
                        sigmask(SIGTTIN)|sigmask(SIGTTOU))
+#define defaultignmask (sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \
+                       sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO))
 
 /*
 
 /*
- * Generalized interface signal handler.
+ * Can the current process (u.u_procp) send the specified signal
+ * to the specified process?
  */
  */
-sigvec()
+#define CANSIGNAL(p, signo) \
+       (u.u_uid == 0 || \
+           u.u_uid == (p)->p_uid || u.u_uid == (p)->p_ruid || \
+           u.u_procp->p_ruid == (p)->p_uid || \
+           u.u_procp->p_ruid == (p)->p_ruid || \
+           ((signo) == SIGCONT && (p)->p_session == u.u_procp->p_session))
+
+sigaction()
 {
        register struct a {
                int     signo;
 {
        register struct a {
                int     signo;
-               struct  sigvec *nsv;
-               struct  sigvec *osv;
+               struct  sigaction *nsa;
+               struct  sigaction *osa;
        } *uap = (struct a  *)u.u_ap;
        } *uap = (struct a  *)u.u_ap;
-       struct sigvec vec;
-       register struct sigvec *sv;
+       struct sigaction vec;
+       register struct sigaction *sa;
        register int sig;
        register int sig;
-       int bit;
+       int bit, error;
 
        sig = uap->signo;
 
        sig = uap->signo;
-       if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) {
-               u.u_error = EINVAL;
-               return;
-       }
-       sv = &vec;
-       if (uap->osv) {
-               sv->sv_handler = u.u_signal[sig];
-               sv->sv_mask = u.u_sigmask[sig];
+       if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
+               RETURN (EINVAL);
+       sa = &vec;
+       if (uap->osa) {
+               sa->sa_handler = u.u_signal[sig];
+               sa->sa_mask = u.u_sigmask[sig];
                bit = sigmask(sig);
                bit = sigmask(sig);
-               sv->sv_flags = 0;
+               sa->sa_flags = 0;
                if ((u.u_sigonstack & bit) != 0)
                if ((u.u_sigonstack & bit) != 0)
-                       sv->sv_flags |= SV_ONSTACK;
-               if ((u.u_sigintr & bit) != 0)
-                       sv->sv_flags |= SV_INTERRUPT;
-               u.u_error =
-                   copyout((caddr_t)sv, (caddr_t)uap->osv, sizeof (vec));
-               if (u.u_error)
-                       return;
+                       sa->sa_flags |= SA_ONSTACK;
+               if ((u.u_sigintr & bit) == 0)
+                       sa->sa_flags |= SA_RESTART;
+               if (u.u_procp->p_flag & SNOCLDSTOP)
+                       sa->sa_flags |= SA_NOCLDSTOP;
+               if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
+                   sizeof (vec)))
+                       RETURN (error);
        }
        }
-       if (uap->nsv) {
-               u.u_error =
-                   copyin((caddr_t)uap->nsv, (caddr_t)sv, sizeof (vec));
-               if (u.u_error)
-                       return;
-               if (sig == SIGCONT && sv->sv_handler == SIG_IGN) {
-                       u.u_error = EINVAL;
-                       return;
-               }
-               setsigvec(sig, sv);
+       if (uap->nsa) {
+               if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
+                   sizeof (vec)))
+                       RETURN (error);
+               setsigvec(sig, sa);
        }
        }
+       RETURN (0);
 }
 
 }
 
-setsigvec(sig, sv)
+setsigvec(sig, sa)
        int sig;
        int sig;
-       register struct sigvec *sv;
+       register struct sigaction *sa;
 {
        register struct proc *p;
        register int bit;
 {
        register struct proc *p;
        register int bit;
@@ -104,23 +109,37 @@ setsigvec(sig, sv)
         * Change setting atomically.
         */
        (void) splhigh();
         * Change setting atomically.
         */
        (void) splhigh();
-       u.u_signal[sig] = sv->sv_handler;
-       u.u_sigmask[sig] = sv->sv_mask &~ cantmask;
-       if (sv->sv_flags & SV_INTERRUPT)
+       u.u_signal[sig] = sa->sa_handler;
+       u.u_sigmask[sig] = sa->sa_mask &~ sigcantmask;
+       if ((sa->sa_flags & SA_RESTART) == 0)
                u.u_sigintr |= bit;
        else
                u.u_sigintr &= ~bit;
                u.u_sigintr |= bit;
        else
                u.u_sigintr &= ~bit;
-       if (sv->sv_flags & SV_ONSTACK)
+       if (sa->sa_flags & SA_ONSTACK)
                u.u_sigonstack |= bit;
        else
                u.u_sigonstack &= ~bit;
                u.u_sigonstack |= bit;
        else
                u.u_sigonstack &= ~bit;
-       if (sv->sv_handler == SIG_IGN) {
+       if (sig == SIGCHLD) {
+               if (sa->sa_flags & SA_NOCLDSTOP)
+                       p->p_flag |= SNOCLDSTOP;
+               else
+                       p->p_flag &= ~SNOCLDSTOP;
+       }
+       /*
+        * Set bit in p_sigignore for signals that are set to SIG_IGN,
+        * and for signals set to SIG_DFL where the default is to ignore.
+        * However, don't put SIGCONT in p_sigignore,
+        * as we have to restart the process.
+        */
+       if (sa->sa_handler == SIG_IGN ||
+          (bit & defaultignmask && sa->sa_handler == SIG_DFL)) {
                p->p_sig &= ~bit;               /* never to be seen again */
                p->p_sig &= ~bit;               /* never to be seen again */
-               p->p_sigignore |= bit;
+               if (sig != SIGCONT)
+                       p->p_sigignore |= bit;  /* easier in psignal */
                p->p_sigcatch &= ~bit;
        } else {
                p->p_sigignore &= ~bit;
                p->p_sigcatch &= ~bit;
        } else {
                p->p_sigignore &= ~bit;
-               if (sv->sv_handler == SIG_DFL)
+               if (sa->sa_handler == SIG_DFL)
                        p->p_sigcatch &= ~bit;
                else
                        p->p_sigcatch |= bit;
                        p->p_sigcatch &= ~bit;
                else
                        p->p_sigcatch |= bit;
@@ -128,7 +147,142 @@ setsigvec(sig, sv)
        (void) spl0();
 }
 
        (void) spl0();
 }
 
-sigblock()
+/*
+ * Initialize signal state for process 0;
+ * set to ignore signals that are ignored by default.
+ */
+siginit(p)
+       struct proc *p;
+{
+
+       p->p_sigignore = defaultignmask &~ sigmask(SIGCONT);
+}
+
+/*
+ * Reset signals for an exec of the specified process.
+ */
+execsigs(p)
+       register struct proc *p;
+{
+       register int nc, mask;
+
+       /*
+        * Reset caught signals.  Held signals remain held
+        * through p_sigmask (unless they were caught,
+        * and are now ignored by default).
+        */
+       while (p->p_sigcatch) {
+               nc = ffs((long)p->p_sigcatch);
+               mask = sigmask(nc);
+               p->p_sigcatch &= ~mask;
+               if (mask & defaultignmask) {
+                       if (nc != SIGCONT)
+                               p->p_sigignore |= mask;
+                       p->p_sig &= ~mask;
+               }
+               u.u_signal[nc] = SIG_DFL;
+       }
+       /*
+        * Reset stack state to the user stack.
+        * Clear set of signals caught on the signal stack.
+        */
+       u.u_onstack = 0;
+       u.u_sigsp = 0;
+       u.u_sigonstack = 0;
+}
+
+/*
+ * Manipulate signal mask.
+ * Note that we receive new mask, not pointer,
+ * and return old mask as return value;
+ * the library stub does the rest.
+ */
+sigprocmask()
+{
+       struct a {
+               int     how;
+               sigset_t mask;
+       } *uap = (struct a *)u.u_ap;
+       register struct proc *p = u.u_procp;
+       int error = 0;
+
+       u.u_r.r_val1 = p->p_sigmask;
+       (void) splhigh();
+
+       switch (uap->how) {
+       case SIG_BLOCK:
+               p->p_sigmask |= uap->mask &~ sigcantmask;
+               break;
+
+       case SIG_UNBLOCK:
+               p->p_sigmask &= ~uap->mask;
+               break;
+
+       case SIG_SETMASK:
+               p->p_sigmask = uap->mask &~ sigcantmask;
+               break;
+       
+       default:
+               error = EINVAL;
+               break;
+       }
+       (void) spl0();
+       RETURN (error);
+}
+
+sigpending()
+{
+
+       u.u_r.r_val1 = u.u_procp->p_sig;
+       RETURN (0);
+}
+
+#ifdef COMPAT_43
+/*
+ * Generalized interface signal handler, 4.3-compatible.
+ */
+osigvec()
+{
+       register struct a {
+               int     signo;
+               struct  sigvec *nsv;
+               struct  sigvec *osv;
+       } *uap = (struct a  *)u.u_ap;
+       struct sigvec vec;
+       register struct sigvec *sv;
+       register int sig;
+       int bit, error;
+
+       sig = uap->signo;
+       if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
+               RETURN (EINVAL);
+       sv = &vec;
+       if (uap->osv) {
+               *(sig_t *)&sv->sv_handler = u.u_signal[sig];
+               sv->sv_mask = u.u_sigmask[sig];
+               bit = sigmask(sig);
+               sv->sv_flags = 0;
+               if ((u.u_sigonstack & bit) != 0)
+                       sv->sv_flags |= SV_ONSTACK;
+               if ((u.u_sigintr & bit) != 0)
+                       sv->sv_flags |= SV_INTERRUPT;
+               if (u.u_procp->p_flag & SNOCLDSTOP)
+                       sv->sv_flags |= SA_NOCLDSTOP;
+               if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
+                   sizeof (vec)))
+                       RETURN (error);
+       }
+       if (uap->nsv) {
+               if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
+                   sizeof (vec)))
+                       RETURN (error);
+               sv->sv_flags ^= SA_RESTART;     /* opposite of SV_INTERRUPT */
+               setsigvec(sig, sv);
+       }
+       RETURN (0);
+}
+
+osigblock()
 {
        struct a {
                int     mask;
 {
        struct a {
                int     mask;
@@ -137,11 +291,12 @@ sigblock()
 
        (void) splhigh();
        u.u_r.r_val1 = p->p_sigmask;
 
        (void) splhigh();
        u.u_r.r_val1 = p->p_sigmask;
-       p->p_sigmask |= uap->mask &~ cantmask;
+       p->p_sigmask |= uap->mask &~ sigcantmask;
        (void) spl0();
        (void) spl0();
+       RETURN (0);
 }
 
 }
 
-sigsetmask()
+osigsetmask()
 {
        struct a {
                int     mask;
 {
        struct a {
                int     mask;
@@ -150,14 +305,21 @@ sigsetmask()
 
        (void) splhigh();
        u.u_r.r_val1 = p->p_sigmask;
 
        (void) splhigh();
        u.u_r.r_val1 = p->p_sigmask;
-       p->p_sigmask = uap->mask &~ cantmask;
+       p->p_sigmask = uap->mask &~ sigcantmask;
        (void) spl0();
        (void) spl0();
+       RETURN (0);
 }
 }
+#endif
 
 
-sigpause()
+/*
+ * Suspend process until signal, providing mask to be set
+ * in the meantime.  Note nonstandard calling convention:
+ * libc stub passes mask, not pointer, to save a copyin.
+ */
+sigsuspend()
 {
        struct a {
 {
        struct a {
-               int     mask;
+               sigset_t mask;
        } *uap = (struct a *)u.u_ap;
        register struct proc *p = u.u_procp;
 
        } *uap = (struct a *)u.u_ap;
        register struct proc *p = u.u_procp;
 
@@ -170,12 +332,11 @@ sigpause()
         */
        u.u_oldmask = p->p_sigmask;
        p->p_flag |= SOMASK;
         */
        u.u_oldmask = p->p_sigmask;
        p->p_flag |= SOMASK;
-       p->p_sigmask = uap->mask &~ cantmask;
+       p->p_sigmask = uap->mask &~ sigcantmask;
        for (;;)
                sleep((caddr_t)&u, PSLEP);
        /*NOTREACHED*/
 }
        for (;;)
                sleep((caddr_t)&u, PSLEP);
        /*NOTREACHED*/
 }
-#undef cantmask
 
 sigstack()
 {
 
 sigstack()
 {
@@ -184,19 +345,15 @@ sigstack()
                struct  sigstack *oss;
        } *uap = (struct a *)u.u_ap;
        struct sigstack ss;
                struct  sigstack *oss;
        } *uap = (struct a *)u.u_ap;
        struct sigstack ss;
-
-       if (uap->oss) {
-               u.u_error = copyout((caddr_t)&u.u_sigstack, (caddr_t)uap->oss, 
-                   sizeof (struct sigstack));
-               if (u.u_error)
-                       return;
-       }
-       if (uap->nss) {
-               u.u_error =
-                   copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss));
-               if (u.u_error == 0)
-                       u.u_sigstack = ss;
-       }
+       int error = 0;
+
+       if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack,
+           (caddr_t)uap->oss, sizeof (struct sigstack))))
+               RETURN (error);
+       if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
+           sizeof (ss))) == 0)
+               u.u_sigstack = ss;
+       RETURN (error);
 }
 
 kill()
 }
 
 kill()
@@ -207,60 +364,50 @@ kill()
        } *uap = (struct a *)u.u_ap;
        register struct proc *p;
 
        } *uap = (struct a *)u.u_ap;
        register struct proc *p;
 
-       if (uap->signo < 0 || uap->signo > NSIG) {
-               u.u_error = EINVAL;
-               return;
-       }
+       if ((unsigned) uap->signo >= NSIG)
+               RETURN (EINVAL);
        if (uap->pid > 0) {
                /* kill single process */
                p = pfind(uap->pid);
        if (uap->pid > 0) {
                /* kill single process */
                p = pfind(uap->pid);
-               if (p == 0) {
-                       u.u_error = ESRCH;
-                       return;
-               }
-               if (u.u_uid && u.u_uid != p->p_uid)
-                       u.u_error = EPERM;
-               else if (uap->signo)
+               if (p == 0)
+                       RETURN (ESRCH);
+               if (!CANSIGNAL(p, uap->signo))
+                       RETURN (EPERM);
+               if (uap->signo)
                        psignal(p, uap->signo);
                        psignal(p, uap->signo);
-               return;
+               RETURN (0);
        }
        switch (uap->pid) {
        case -1:                /* broadcast signal */
        }
        switch (uap->pid) {
        case -1:                /* broadcast signal */
-               u.u_error = killpg1(uap->signo, 0, 1);
-               break;
+               RETURN (killpg1(uap->signo, 0, 1));
        case 0:                 /* signal own process group */
        case 0:                 /* signal own process group */
-               u.u_error = killpg1(uap->signo, 0, 0);
-               break;
+               RETURN (killpg1(uap->signo, 0, 0));
        default:                /* negative explicit process group */
        default:                /* negative explicit process group */
-               u.u_error = killpg1(uap->signo, -uap->pid, 0);
-               break;
+               RETURN (killpg1(uap->signo, -uap->pid, 0));
        }
        }
-       return;
+       /* NOTREACHED */
 }
 
 }
 
-killpg()
+#ifdef COMPAT_43
+okillpg()
 {
        register struct a {
                int     pgid;
                int     signo;
        } *uap = (struct a *)u.u_ap;
 
 {
        register struct a {
                int     pgid;
                int     signo;
        } *uap = (struct a *)u.u_ap;
 
-       if (uap->signo < 0 || uap->signo > NSIG) {
-               u.u_error = EINVAL;
-               return;
-       }
-       u.u_error = killpg1(uap->signo, uap->pgid, 0);
+       if ((unsigned) uap->signo >= NSIG)
+               RETURN (EINVAL);
+       RETURN (killpg1(uap->signo, uap->pgid, 0));
 }
 }
-
-/* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */
+#endif
 
 killpg1(signo, pgid, all)
        int signo, pgid, all;
 {
        register struct proc *p;
        struct pgrp *pgrp;
 
 killpg1(signo, pgid, all)
        int signo, pgid, all;
 {
        register struct proc *p;
        struct pgrp *pgrp;
-       int f = 0, error = 0;
-
+       int f = 0, error = ESRCH;
        
        if (all)        
                /* 
        
        if (all)        
                /* 
@@ -268,9 +415,7 @@ killpg1(signo, pgid, all)
                 */
                for (p = allproc; p != NULL; p = p->p_nxt) {
                        if (p->p_ppid == 0 || p->p_flag&SSYS || 
                 */
                for (p = allproc; p != NULL; p = p->p_nxt) {
                        if (p->p_ppid == 0 || p->p_flag&SSYS || 
-                           p == u.u_procp ||
-                          (u.u_uid && u.u_uid != p->p_uid && 
-                          !(signo == SIGCONT && inferior(p))))
+                           p == u.u_procp || !CANSIGNAL(p, signo))
                                continue;
                        f++;
                        if (signo)
                                continue;
                        f++;
                        if (signo)
@@ -285,25 +430,18 @@ killpg1(signo, pgid, all)
                else {
                        pgrp = pgfind(pgid);
                        if (pgrp == NULL)
                else {
                        pgrp = pgfind(pgid);
                        if (pgrp == NULL)
-                               return(ESRCH);
+                               return (ESRCH);
                }
                }
-               if (!(pgrp->pg_jobc) && 
-                    (signo==SIGTTIN || signo==SIGTTOU || signo==SIGTSTP))
-                       return(EPERM);
                for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
                for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
-                       if (p->p_ppid == 0 || p->p_flag&SSYS)
+                       if (p->p_ppid == 0 || p->p_flag&SSYS ||
+                           !CANSIGNAL(p, signo))
                                continue;
                                continue;
-                       if (u.u_uid && u.u_uid != p->p_uid && 
-                          !(signo == SIGCONT && inferior(p))) {
-                               error = EPERM;
-                               continue;
-                       }
                        f++;
                        if (signo)
                                psignal(p, signo);
                }
        }
                        f++;
                        if (signo)
                                psignal(p, signo);
                }
        }
-       return (error ? error : (f == 0 ? ESRCH : 0));
+       return (f ? 0 : error);
 }
 
 /*
 }
 
 /*
@@ -313,25 +451,43 @@ killpg1(signo, pgid, all)
  */
 gsignal(pgid, sig)
 {
  */
 gsignal(pgid, sig)
 {
-       register struct pgrp *pgrp;
+       struct pgrp *pgrp;
 
 
-       if (!pgid)
-               return;
-       if ((pgrp = pgfind(pgid)) == NULL)
-               return;
-       pgsignal(pgrp, sig);
+       if (pgid && (pgrp = pgfind(pgid)))
+               pgsignal(pgrp, sig);
 }
 
 pgsignal(pgrp, sig)
 }
 
 pgsignal(pgrp, sig)
-       register struct pgrp *pgrp;
+       struct pgrp *pgrp;
 {
        register struct proc *p;
 
 {
        register struct proc *p;
 
-       if (!(pgrp->pg_jobc) && 
-            (sig==SIGTTIN || sig==SIGTTOU || sig==SIGTSTP))
-               return;
-       for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
+       for (p = pgrp->pg_mem; p; p = p->p_pgrpnxt)
+               psignal(p, sig);
+}
+
+/*
+ * Send a signal caused by a trap to the current process.
+ * If it will be caught immediately, deliver it with correct code.
+ * Otherwise, post it normally.
+ */
+trapsignal(sig, code)
+       register int sig;
+       unsigned code;
+{
+       register struct proc *p = u.u_procp;
+       int mask;
+
+       mask = sigmask(sig);
+       if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 &&
+           (p->p_sigmask & mask) == 0) {
+               u.u_ru.ru_nsignals++;
+               sendsig(u.u_signal[sig], sig, p->p_sigmask, code);
+               p->p_sigmask |= u.u_sigmask[sig] | mask;
+       } else {
+               u.u_arg[1] = code;      /* XXX for core dump/debugger */
                psignal(p, sig);
                psignal(p, sig);
+       }
 }
 
 /*
 }
 
 /*
@@ -343,10 +499,13 @@ psignal(p, sig)
        register int sig;
 {
        register int s;
        register int sig;
 {
        register int s;
-       register int (*action)();
+       register sig_t action;
        int mask;
 
        int mask;
 
-       if ((unsigned)sig >= NSIG)
+       if ((unsigned)sig >= NSIG || sig == 0)
+               panic("psignal sig");
+       if (p->p_pgrp->pg_jobc == 0 && 
+            (sig == SIGTTIN || sig == SIGTTOU || sig == SIGTSTP))
                return;
        mask = sigmask(sig);
 
                return;
        mask = sigmask(sig);
 
@@ -359,6 +518,9 @@ psignal(p, sig)
                /*
                 * If the signal is being ignored,
                 * then we forget about it immediately.
                /*
                 * If the signal is being ignored,
                 * then we forget about it immediately.
+                * (Note: we don't set SIGCONT in p_sigignore,
+                * and if it is set to SIG_IGN,
+                * action will be SIG_DFL here.)
                 */
                if (p->p_sigignore & mask)
                        return;
                 */
                if (p->p_sigignore & mask)
                        return;
@@ -369,37 +531,36 @@ psignal(p, sig)
                else
                        action = SIG_DFL;
        }
                else
                        action = SIG_DFL;
        }
-       if (sig) {
-               switch (sig) {
-
-               case SIGTERM:
-                       if ((p->p_flag&STRC) || action != SIG_DFL)
-                               break;
-                       /* fall into ... */
+       switch (sig) {
 
 
-               case SIGKILL:
-                       if (p->p_nice > NZERO)
-                               p->p_nice = NZERO;
+       case SIGTERM:
+               if ((p->p_flag&STRC) || action != SIG_DFL)
                        break;
                        break;
+               /* FALLTHROUGH */
 
 
-               case SIGCONT:
-                       p->p_sig &= ~stopsigmask;
-                       break;
+       case SIGKILL:
+               if (p->p_nice > NZERO)
+                       p->p_nice = NZERO;
+               break;
 
 
-               case SIGTSTP:
-               case SIGTTIN:
-               case SIGTTOU:
-                       /*FALLTHROUGH*/
-               case SIGSTOP:
-                       p->p_sig &= ~sigmask(SIGCONT);
-                       break;
-               }
-               p->p_sig |= mask;
+       case SIGCONT:
+               p->p_sig &= ~stopsigmask;
+               break;
+
+       case SIGTSTP:
+       case SIGTTIN:
+       case SIGTTOU:
+       case SIGSTOP:
+               p->p_sig &= ~sigmask(SIGCONT);
+               break;
        }
        }
+       p->p_sig |= mask;
+
        /*
        /*
-        * Defer further processing for signals which are held.
+        * Defer further processing for signals which are held,
+        * except that stopped processes must be continued by SIGCONT.
         */
         */
-       if (action == SIG_HOLD)
+       if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP))
                return;
        s = splhigh();
        switch (p->p_stat) {
                return;
        s = splhigh();
        switch (p->p_stat) {
@@ -420,18 +581,15 @@ psignal(p, sig)
                 */
                if (p->p_flag&STRC)
                        goto run;
                 */
                if (p->p_flag&STRC)
                        goto run;
-               switch (sig) {
-
-               case SIGSTOP:
-               case SIGTSTP:
-               case SIGTTIN:
-               case SIGTTOU:
-                       /*
-                        * These are the signals which by default
-                        * stop a process.
-                        */
+               /*
+                * When a sleeping process receives a stop
+                * signal, process immediately if possible.
+                * All other (caught or default) signals
+                * cause the process to run.
+                */
+               if (mask & stopsigmask) {
                        if (action != SIG_DFL)
                        if (action != SIG_DFL)
-                               goto run;
+                               goto runfast;
                        /*
                         * If a child in vfork(), stopping could
                         * cause deadlock.
                        /*
                         * If a child in vfork(), stopping could
                         * cause deadlock.
@@ -440,30 +598,12 @@ psignal(p, sig)
                                goto out;
                        p->p_sig &= ~mask;
                        p->p_cursig = sig;
                                goto out;
                        p->p_sig &= ~mask;
                        p->p_cursig = sig;
-                       psignal(p->p_pptr, SIGCHLD);
+                       if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
+                               psignal(p->p_pptr, SIGCHLD);
                        stop(p);
                        goto out;
                        stop(p);
                        goto out;
-
-               case SIGIO:
-               case SIGURG:
-               case SIGCHLD:
-               case SIGWINCH:
-                       /*
-                        * These signals are special in that they
-                        * don't get propogated... if the process
-                        * isn't interested, forget it.
-                        */
-                       if (action != SIG_DFL)
-                               goto run;
-                       p->p_sig &= ~mask;              /* take it away */
-                       goto out;
-
-               default:
-                       /*
-                        * All other signals cause the process to run
-                        */
-                       goto run;
-               }
+               } else
+                       goto runfast;
                /*NOTREACHED*/
 
        case SSTOP:
                /*NOTREACHED*/
 
        case SSTOP:
@@ -479,16 +619,25 @@ psignal(p, sig)
                        /*
                         * Kill signal always sets processes running.
                         */
                        /*
                         * Kill signal always sets processes running.
                         */
-                       goto run;
+                       goto runfast;
 
                case SIGCONT:
                        /*
 
                case SIGCONT:
                        /*
+                        * If SIGCONT is default (or ignored), we continue
+                        * the process but don't leave the signal in p_sig,
+                        * as it has no further action.  If SIGCONT is held,
+                        * continue the process and leave the signal in p_sig.
                         * If the process catches SIGCONT, let it handle
                         * the signal itself.  If it isn't waiting on
                         * an event, then it goes back to run state.
                         * Otherwise, process goes back to sleep state.
                         */
                         * If the process catches SIGCONT, let it handle
                         * the signal itself.  If it isn't waiting on
                         * an event, then it goes back to run state.
                         * Otherwise, process goes back to sleep state.
                         */
-                       if (action != SIG_DFL || p->p_wchan == 0)
+                       p->p_cursig = 0;        /* ??? XXX */
+                       if (action == SIG_DFL)
+                               p->p_sig &= ~mask;
+                       if (action == SIG_CATCH)
+                               goto runfast;
+                       if (p->p_wchan == 0)
                                goto run;
                        p->p_stat = SSLEEP;
                        goto out;
                                goto run;
                        p->p_stat = SSLEEP;
                        goto out;
@@ -529,12 +678,14 @@ psignal(p, sig)
                goto out;
        }
        /*NOTREACHED*/
                goto out;
        }
        /*NOTREACHED*/
-run:
+
+runfast:
        /*
         * Raise priority to at least PUSER.
         */
        if (p->p_pri > PUSER)
                p->p_pri = PUSER;
        /*
         * Raise priority to at least PUSER.
         */
        if (p->p_pri > PUSER)
                p->p_pri = PUSER;
+run:
        setrun(p);
 out:
        splx(s);
        setrun(p);
 out:
        splx(s);
@@ -547,29 +698,28 @@ out:
  * This is asked at least once each time a process enters the
  * system (though this can usually be done without actually
  * calling issig by checking the pending signal masks.)
  * This is asked at least once each time a process enters the
  * system (though this can usually be done without actually
  * calling issig by checking the pending signal masks.)
- * A signal does not do anything
- * directly to a process; it sets
+ * Most signals do not do anything
+ * directly to a process; they set
  * a flag that asks the process to
  * do something to itself.
  */
 issig()
 {
        register struct proc *p;
  * a flag that asks the process to
  * do something to itself.
  */
 issig()
 {
        register struct proc *p;
-       register int sig;
-       int sigbits, mask;
+       register int sig, mask;
 
        p = u.u_procp;
        for (;;) {
 
        p = u.u_procp;
        for (;;) {
-               sigbits = p->p_sig &~ p->p_sigmask;
-               if ((p->p_flag&STRC) == 0)
-                       sigbits &= ~p->p_sigignore;
+               mask = p->p_sig &~ p->p_sigmask;
                if (p->p_flag&SVFORK)
                if (p->p_flag&SVFORK)
-                       sigbits &= ~stopsigmask;
-               if (sigbits == 0)
+                       mask &= ~stopsigmask;
+               if (mask == 0)
                        break;
                        break;
-               sig = ffs((long)sigbits);
+               sig = ffs((long)mask);
                mask = sigmask(sig);
                p->p_sig &= ~mask;              /* take the signal! */
                mask = sigmask(sig);
                p->p_sig &= ~mask;              /* take the signal! */
+               if (mask & p->p_sigignore && (p->p_flag&STRC) == 0)
+                       continue;       /* only if STRC was on when posted */
                p->p_cursig = sig;
                if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
                        /*
                p->p_cursig = sig;
                if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
                        /*
@@ -619,44 +769,32 @@ issig()
                         * Don't take default actions on system processes.
                         */
                        if (p->p_ppid == 0)
                         * Don't take default actions on system processes.
                         */
                        if (p->p_ppid == 0)
-                               break;
-                       switch (sig) {
-
-                       case SIGTSTP:
-                       case SIGTTIN:
-                       case SIGTTOU:
-                       case SIGSTOP:
+                               continue;
+                       if (mask & stopsigmask) {
                                if (p->p_flag&STRC)
                                        continue;
                                if (p->p_flag&STRC)
                                        continue;
-                               psignal(p->p_pptr, SIGCHLD);
                                stop(p);
                                stop(p);
+                               if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
+                                       psignal(p->p_pptr, SIGCHLD);
                                swtch();
                                continue;
                                swtch();
                                continue;
-
-                       case SIGCONT:
-                       case SIGCHLD:
-                       case SIGURG:
-                       case SIGIO:
-                       case SIGWINCH:
+                       } else if (mask & defaultignmask) {
                                /*
                                /*
-                                * These signals are normally not
-                                * sent if the action is the default.
+                                * Except for SIGCONT, shouldn't get here.
+                                * Default action is to ignore; drop it.
                                 */
                                continue;               /* == ignore */
                                 */
                                continue;               /* == ignore */
-
-                       default:
+                       } else
                                goto send;
                                goto send;
-                       }
                        /*NOTREACHED*/
 
                        /*NOTREACHED*/
 
-               case SIG_HOLD:
                case SIG_IGN:
                        /*
                case SIG_IGN:
                        /*
-                        * Masking above should prevent us
-                        * ever trying to take action on a held
-                        * or ignored signal, unless process is traced.
+                        * Masking above should prevent us ever trying
+                        * to take action on an ignored signal other
+                        * than SIGCONT, unless process is traced.
                         */
                         */
-                       if ((p->p_flag&STRC) == 0)
+                       if (sig != SIGCONT && (p->p_flag&STRC) == 0)
                                printf("issig\n");
                        continue;
 
                                printf("issig\n");
                        continue;
 
@@ -700,7 +838,7 @@ stop(p)
  * Perform the action specified by
  * the current signal.
  * The usual sequence is:
  * Perform the action specified by
  * the current signal.
  * The usual sequence is:
- *     if (issig())
+ *     if (p->p_cursig || ISSIG(p))
  *             psig();
  * The signal bit has already been cleared by issig,
  * and the current signal number stored in p->p_cursig.
  *             psig();
  * The signal bit has already been cleared by issig,
  * and the current signal number stored in p->p_cursig.
@@ -708,72 +846,67 @@ stop(p)
 psig()
 {
        register struct proc *p = u.u_procp;
 psig()
 {
        register struct proc *p = u.u_procp;
-       register int sig = p->p_cursig;
-       int mask = sigmask(sig), returnmask;
-       register int (*action)();
-
-       if (sig == 0)
-               panic("psig");
-       action = u.u_signal[sig];
-       if (action != SIG_DFL) {
-               if (action == SIG_IGN || (p->p_sigmask & mask))
-                       panic("psig action");
-               u.u_error = 0;
-               /*
-                * Set the new mask value and also defer further
-                * occurences of this signal (unless we're simulating
-                * the old signal facilities). 
-                *
-                * Special case: user has done a sigpause.  Here the
-                * current mask is not of interest, but rather the
-                * mask from before the sigpause is what we want restored
-                * after the signal processing is completed.
-                */
-               (void) splhigh();
-               if (p->p_flag & SOUSIG) {
-                       if (sig != SIGILL && sig != SIGTRAP) {
-                               u.u_signal[sig] = SIG_DFL;
-                               p->p_sigcatch &= ~mask;
-                       }
-                       mask = 0;
+       register int sig;
+       int mask, returnmask;
+       register sig_t action;
+
+       do {
+               sig = p->p_cursig;
+               mask = sigmask(sig);
+               if (sig == 0)
+                       panic("psig");
+               action = u.u_signal[sig];
+               if (action != SIG_DFL) {
+#ifdef DIAGNOSTIC
+                       if (action == SIG_IGN || (p->p_sigmask & mask))
+                               panic("psig action");
+#endif
+                       u.u_error = 0;
+                       /*
+                        * Set the new mask value and also defer further
+                        * occurences of this signal.
+                        *
+                        * Special case: user has done a sigpause.  Here the
+                        * current mask is not of interest, but rather the
+                        * mask from before the sigpause is what we want
+                        * restored after the signal processing is completed.
+                        */
+                       (void) splhigh();
+                       if (p->p_flag & SOMASK) {
+                               returnmask = u.u_oldmask;
+                               p->p_flag &= ~SOMASK;
+                       } else
+                               returnmask = p->p_sigmask;
+                       p->p_sigmask |= u.u_sigmask[sig] | mask;
+                       (void) spl0();
+                       u.u_ru.ru_nsignals++;
+                       sendsig(action, sig, returnmask, 0);
+                       p->p_cursig = 0;
+                       continue;
                }
                }
-               if (p->p_flag & SOMASK) {
-                       returnmask = u.u_oldmask;
-                       p->p_flag &= ~SOMASK;
-               } else
-                       returnmask = p->p_sigmask;
-               p->p_sigmask |= u.u_sigmask[sig] | mask;
-               (void) spl0();
-               u.u_ru.ru_nsignals++;
-               sendsig(action, sig, returnmask);
-               p->p_cursig = 0;
-               return;
-       }
-       u.u_acflag |= AXSIG;
-       switch (sig) {
+               u.u_acflag |= AXSIG;
+               switch (sig) {
 
 
-       case SIGILL:
-       case SIGIOT:
-       case SIGBUS:
-       case SIGQUIT:
-       case SIGTRAP:
-       case SIGEMT:
-       case SIGFPE:
-       case SIGSEGV:
-       case SIGSYS:
-               u.u_arg[0] = sig;
-               if (core() == 0)
-                       sig += 0200;
-       }
-       exit(sig);
+               case SIGILL:
+               case SIGIOT:
+               case SIGBUS:
+               case SIGQUIT:
+               case SIGTRAP:
+               case SIGEMT:
+               case SIGFPE:
+               case SIGSEGV:
+               case SIGSYS:
+                       u.u_arg[0] = sig;
+                       if (core() == 0)
+                               sig |= WCOREFLAG;
+               }
+               exit(W_EXITCODE(0, sig));
+               /* NOTREACHED */
+       } while (ISSIG(p));
 }
 
 /*
 }
 
 /*
- * Create a core image on the file "core"
- * If you are looking for protection glitches,
- * there are probably a wealth of them here
- * when this occurs to a suid command.
- *
+ * Create a core image on the file "core".
  * It writes UPAGES block of the
  * user.h area followed by the entire
  * data+stack segments.
  * It writes UPAGES block of the
  * user.h area followed by the entire
  * data+stack segments.
@@ -781,19 +914,20 @@ psig()
 core()
 {
        register struct vnode *vp;
 core()
 {
        register struct vnode *vp;
+       register struct proc *p = u.u_procp;
        register struct nameidata *ndp = &u.u_nd;
        struct vattr vattr;
        int error;
 
        register struct nameidata *ndp = &u.u_nd;
        struct vattr vattr;
        int error;
 
-       if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid)
+       if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid)
                return (EFAULT);
        if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
            u.u_rlimit[RLIMIT_CORE].rlim_cur)
                return (EFAULT);
                return (EFAULT);
        if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
            u.u_rlimit[RLIMIT_CORE].rlim_cur)
                return (EFAULT);
-       if (u.u_procp->p_textp) {
-               VOP_LOCK(u.u_procp->p_textp->x_vptr);
-               error = VOP_ACCESS(u.u_procp->p_textp->x_vptr, VREAD, u.u_cred);
-               VOP_UNLOCK(u.u_procp->p_textp->x_vptr);
+       if (p->p_textp) {
+               VOP_LOCK(p->p_textp->x_vptr);
+               error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred);
+               VOP_UNLOCK(p->p_textp->x_vptr);
                if (error)
                        return (EFAULT);
        }
                if (error)
                        return (EFAULT);
        }
@@ -825,15 +959,26 @@ core()
            UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
        if (error == 0)
                error = vn_rdwr(UIO_WRITE, vp,
            UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
        if (error == 0)
                error = vn_rdwr(UIO_WRITE, vp,
-                   (caddr_t)ctob(dptov(u.u_procp, 0)),
+                   (caddr_t)ctob(dptov(p, 0)),
                    (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
                    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
        if (error == 0)
                error = vn_rdwr(UIO_WRITE, vp,
                    (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
                    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
        if (error == 0)
                error = vn_rdwr(UIO_WRITE, vp,
-                   (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)),
+                   (caddr_t)ctob(sptov(p, u.u_ssize - 1)),
                    (int)ctob(u.u_ssize),
                    (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE,
                    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
        vput(vp);
        return (error);
 }
                    (int)ctob(u.u_ssize),
                    (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE,
                    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
        vput(vp);
        return (error);
 }
+
+/*
+ * Nonexistent system call-- signal process (may want to handle it).
+ * Flag error in case process won't see signal immediately (blocked or ignored).
+ */
+nosys()
+{
+
+       psignal(u.u_procp, SIGSYS);
+       u.u_error = EINVAL;
+}