123374Smckusick /* 237580Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337580Smckusick * All rights reserved. 423374Smckusick * 544440Sbostic * %sccs.include.redist.c% 637580Smckusick * 7*46008Swilliam * @(#)kern_sig.c 7.26 (Berkeley) 01/18/91 823374Smckusick */ 97421Sroot 1017092Sbloom #include "param.h" 1117092Sbloom #include "systm.h" 1244405Skarels #include "user.h" 1337580Smckusick #include "vnode.h" 1417092Sbloom #include "proc.h" 1517092Sbloom #include "timeb.h" 1617092Sbloom #include "times.h" 1717092Sbloom #include "buf.h" 1817092Sbloom #include "seg.h" 1917092Sbloom #include "acct.h" 2017092Sbloom #include "uio.h" 2137580Smckusick #include "file.h" 2217092Sbloom #include "kernel.h" 2339513Skarels #include "wait.h" 2440807Smarc #include "ktrace.h" 257421Sroot 2637581Smckusick #include "machine/reg.h" 2737581Smckusick #include "machine/psl.h" 2837581Smckusick #include "machine/mtpr.h" 2945741Smckusick #include "../vm/vm_param.h" 3037581Smckusick 3142437Skarels #define ttystopsigmask (sigmask(SIGTSTP)|sigmask(SIGTTIN)|sigmask(SIGTTOU)) 3242437Skarels #define stopsigmask (sigmask(SIGSTOP)|ttystopsigmask) 3339513Skarels #define defaultignmask (sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \ 3439513Skarels sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO)) 3512951Ssam 3617013Smckusick /* 3742920Skarels * Can process p send the signal signo to process q? 3817013Smckusick */ 3942920Skarels #define CANSIGNAL(p, q, signo) \ 4042920Skarels ((p)->p_uid == 0 || \ 4142920Skarels (p)->p_ruid == (q)->p_ruid || (p)->p_uid == (q)->p_ruid || \ 4242920Skarels (p)->p_ruid == (q)->p_uid || (p)->p_uid == (q)->p_uid || \ 4342920Skarels ((signo) == SIGCONT && (q)->p_session == (p)->p_session)) 4439513Skarels 4542920Skarels /* ARGSUSED */ 4642920Skarels sigaction(p, uap, retval) 4742920Skarels struct proc *p; 4842920Skarels register struct args { 4912882Ssam int signo; 5039513Skarels struct sigaction *nsa; 5139513Skarels struct sigaction *osa; 5242920Skarels } *uap; 5342920Skarels int *retval; 5442920Skarels { 5539513Skarels struct sigaction vec; 5639513Skarels register struct sigaction *sa; 5712882Ssam register int sig; 5839513Skarels int bit, error; 597421Sroot 6012882Ssam sig = uap->signo; 6139513Skarels if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) 6244405Skarels return (EINVAL); 6339513Skarels sa = &vec; 6439513Skarels if (uap->osa) { 6539513Skarels sa->sa_handler = u.u_signal[sig]; 6639513Skarels sa->sa_mask = u.u_sigmask[sig]; 6718308Smckusick bit = sigmask(sig); 6839513Skarels sa->sa_flags = 0; 6918308Smckusick if ((u.u_sigonstack & bit) != 0) 7039513Skarels sa->sa_flags |= SA_ONSTACK; 7139513Skarels if ((u.u_sigintr & bit) == 0) 7239513Skarels sa->sa_flags |= SA_RESTART; 7342920Skarels if (p->p_flag & SNOCLDSTOP) 7439513Skarels sa->sa_flags |= SA_NOCLDSTOP; 7539513Skarels if (error = copyout((caddr_t)sa, (caddr_t)uap->osa, 7639513Skarels sizeof (vec))) 7744405Skarels return (error); 7812951Ssam } 7939513Skarels if (uap->nsa) { 8039513Skarels if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa, 8139513Skarels sizeof (vec))) 8244405Skarels return (error); 8342920Skarels setsigvec(p, sig, sa); 8412951Ssam } 8544405Skarels return (0); 867421Sroot } 877421Sroot 8842920Skarels setsigvec(p, sig, sa) 8942920Skarels register struct proc *p; 9012951Ssam int sig; 9139513Skarels register struct sigaction *sa; 9212882Ssam { 9312951Ssam register int bit; 9412882Ssam 9517153Sbloom bit = sigmask(sig); 9612882Ssam /* 9712882Ssam * Change setting atomically. 9812882Ssam */ 9917153Sbloom (void) splhigh(); 10039513Skarels u.u_signal[sig] = sa->sa_handler; 10139513Skarels u.u_sigmask[sig] = sa->sa_mask &~ sigcantmask; 10239513Skarels if ((sa->sa_flags & SA_RESTART) == 0) 10318308Smckusick u.u_sigintr |= bit; 10418308Smckusick else 10518308Smckusick u.u_sigintr &= ~bit; 10639513Skarels if (sa->sa_flags & SA_ONSTACK) 10712951Ssam u.u_sigonstack |= bit; 10812951Ssam else 10912951Ssam u.u_sigonstack &= ~bit; 11039513Skarels if (sig == SIGCHLD) { 11139513Skarels if (sa->sa_flags & SA_NOCLDSTOP) 11239513Skarels p->p_flag |= SNOCLDSTOP; 11339513Skarels else 11439513Skarels p->p_flag &= ~SNOCLDSTOP; 11539513Skarels } 11639513Skarels /* 11739513Skarels * Set bit in p_sigignore for signals that are set to SIG_IGN, 11839513Skarels * and for signals set to SIG_DFL where the default is to ignore. 11939513Skarels * However, don't put SIGCONT in p_sigignore, 12039513Skarels * as we have to restart the process. 12139513Skarels */ 12239513Skarels if (sa->sa_handler == SIG_IGN || 12339513Skarels (bit & defaultignmask && sa->sa_handler == SIG_DFL)) { 12412951Ssam p->p_sig &= ~bit; /* never to be seen again */ 12539513Skarels if (sig != SIGCONT) 12639513Skarels p->p_sigignore |= bit; /* easier in psignal */ 12712951Ssam p->p_sigcatch &= ~bit; 12812882Ssam } else { 12912951Ssam p->p_sigignore &= ~bit; 13039513Skarels if (sa->sa_handler == SIG_DFL) 13112951Ssam p->p_sigcatch &= ~bit; 13212882Ssam else 13312951Ssam p->p_sigcatch |= bit; 13412882Ssam } 13512882Ssam (void) spl0(); 13612882Ssam } 13712882Ssam 13839513Skarels /* 13939513Skarels * Initialize signal state for process 0; 14039513Skarels * set to ignore signals that are ignored by default. 14139513Skarels */ 14239513Skarels siginit(p) 14339513Skarels struct proc *p; 1447421Sroot { 14539513Skarels 14639513Skarels p->p_sigignore = defaultignmask &~ sigmask(SIGCONT); 14739513Skarels } 14839513Skarels 14939513Skarels /* 15039513Skarels * Reset signals for an exec of the specified process. 15139513Skarels */ 15239513Skarels execsigs(p) 15339513Skarels register struct proc *p; 15439513Skarels { 15539513Skarels register int nc, mask; 15639513Skarels 15739513Skarels /* 15839513Skarels * Reset caught signals. Held signals remain held 15939513Skarels * through p_sigmask (unless they were caught, 16039513Skarels * and are now ignored by default). 16139513Skarels */ 16239513Skarels while (p->p_sigcatch) { 16339513Skarels nc = ffs((long)p->p_sigcatch); 16439513Skarels mask = sigmask(nc); 16539513Skarels p->p_sigcatch &= ~mask; 16639513Skarels if (mask & defaultignmask) { 16739513Skarels if (nc != SIGCONT) 16839513Skarels p->p_sigignore |= mask; 16939513Skarels p->p_sig &= ~mask; 17039513Skarels } 17139513Skarels u.u_signal[nc] = SIG_DFL; 17239513Skarels } 17339513Skarels /* 17439513Skarels * Reset stack state to the user stack. 17539513Skarels * Clear set of signals caught on the signal stack. 17639513Skarels */ 17739513Skarels u.u_onstack = 0; 17839513Skarels u.u_sigsp = 0; 17939513Skarels u.u_sigonstack = 0; 18039513Skarels } 18139513Skarels 18239513Skarels /* 18339513Skarels * Manipulate signal mask. 18439513Skarels * Note that we receive new mask, not pointer, 18539513Skarels * and return old mask as return value; 18639513Skarels * the library stub does the rest. 18739513Skarels */ 18842920Skarels sigprocmask(p, uap, retval) 18942920Skarels register struct proc *p; 19042920Skarels struct args { 19139513Skarels int how; 19239513Skarels sigset_t mask; 19342920Skarels } *uap; 19442920Skarels int *retval; 19542920Skarels { 19639513Skarels int error = 0; 19739513Skarels 19842920Skarels *retval = p->p_sigmask; 19939513Skarels (void) splhigh(); 20039513Skarels 20139513Skarels switch (uap->how) { 20239513Skarels case SIG_BLOCK: 20339513Skarels p->p_sigmask |= uap->mask &~ sigcantmask; 20439513Skarels break; 20539513Skarels 20639513Skarels case SIG_UNBLOCK: 20739513Skarels p->p_sigmask &= ~uap->mask; 20839513Skarels break; 20939513Skarels 21039513Skarels case SIG_SETMASK: 21139513Skarels p->p_sigmask = uap->mask &~ sigcantmask; 21239513Skarels break; 21339513Skarels 21439513Skarels default: 21539513Skarels error = EINVAL; 21639513Skarels break; 21739513Skarels } 21839513Skarels (void) spl0(); 21944405Skarels return (error); 22039513Skarels } 22139513Skarels 22242920Skarels /* ARGSUSED */ 22342920Skarels sigpending(p, uap, retval) 22442920Skarels struct proc *p; 22542920Skarels void *uap; 22642920Skarels int *retval; 22739513Skarels { 22839513Skarels 22942920Skarels *retval = p->p_sig; 23044405Skarels return (0); 23139513Skarels } 23239513Skarels 23339513Skarels #ifdef COMPAT_43 23439513Skarels /* 23539513Skarels * Generalized interface signal handler, 4.3-compatible. 23639513Skarels */ 23742920Skarels /* ARGSUSED */ 23842920Skarels osigvec(p, uap, retval) 23942920Skarels struct proc *p; 24042920Skarels register struct args { 24139513Skarels int signo; 24239513Skarels struct sigvec *nsv; 24339513Skarels struct sigvec *osv; 24442920Skarels } *uap; 24542920Skarels int *retval; 24642920Skarels { 24739513Skarels struct sigvec vec; 24839513Skarels register struct sigvec *sv; 24939513Skarels register int sig; 25039513Skarels int bit, error; 25139513Skarels 25239513Skarels sig = uap->signo; 25339513Skarels if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) 25444405Skarels return (EINVAL); 25539513Skarels sv = &vec; 25639513Skarels if (uap->osv) { 25739513Skarels *(sig_t *)&sv->sv_handler = u.u_signal[sig]; 25839513Skarels sv->sv_mask = u.u_sigmask[sig]; 25939513Skarels bit = sigmask(sig); 26039513Skarels sv->sv_flags = 0; 26139513Skarels if ((u.u_sigonstack & bit) != 0) 26239513Skarels sv->sv_flags |= SV_ONSTACK; 26339513Skarels if ((u.u_sigintr & bit) != 0) 26439513Skarels sv->sv_flags |= SV_INTERRUPT; 26542920Skarels if (p->p_flag & SNOCLDSTOP) 26639513Skarels sv->sv_flags |= SA_NOCLDSTOP; 26739513Skarels if (error = copyout((caddr_t)sv, (caddr_t)uap->osv, 26839513Skarels sizeof (vec))) 26944405Skarels return (error); 27039513Skarels } 27139513Skarels if (uap->nsv) { 27239513Skarels if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv, 27339513Skarels sizeof (vec))) 27444405Skarels return (error); 27539513Skarels sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 27642920Skarels setsigvec(p, sig, (struct sigaction *)sv); 27739513Skarels } 27844405Skarels return (0); 27939513Skarels } 28039513Skarels 28142920Skarels osigblock(p, uap, retval) 28242920Skarels register struct proc *p; 28342920Skarels struct args { 28442920Skarels int mask; 28542920Skarels } *uap; 28642920Skarels int *retval; 28739513Skarels { 2887499Sroot 28917153Sbloom (void) splhigh(); 29042920Skarels *retval = p->p_sigmask; 29139513Skarels p->p_sigmask |= uap->mask &~ sigcantmask; 29212882Ssam (void) spl0(); 29344405Skarels return (0); 2947499Sroot } 2957499Sroot 29642920Skarels osigsetmask(p, uap, retval) 29742920Skarels struct proc *p; 29842920Skarels struct args { 29942920Skarels int mask; 30042920Skarels } *uap; 30142920Skarels int *retval; 3027499Sroot { 3037499Sroot 30417153Sbloom (void) splhigh(); 30542920Skarels *retval = p->p_sigmask; 30639513Skarels p->p_sigmask = uap->mask &~ sigcantmask; 30712882Ssam (void) spl0(); 30844405Skarels return (0); 3097499Sroot } 31039513Skarels #endif 3117499Sroot 31239513Skarels /* 31339513Skarels * Suspend process until signal, providing mask to be set 31439513Skarels * in the meantime. Note nonstandard calling convention: 31539513Skarels * libc stub passes mask, not pointer, to save a copyin. 31639513Skarels */ 31742920Skarels /* ARGSUSED */ 31842920Skarels sigsuspend(p, uap, retval) 31942920Skarels register struct proc *p; 32042920Skarels struct args { 32142920Skarels sigset_t mask; 32242920Skarels } *uap; 32342920Skarels int *retval; 3247499Sroot { 3257499Sroot 32612882Ssam /* 32712882Ssam * When returning from sigpause, we want 32812882Ssam * the old mask to be restored after the 32912882Ssam * signal handler has finished. Thus, we 33012882Ssam * save it here and mark the proc structure 33112882Ssam * to indicate this (should be in u.). 33212882Ssam */ 33312882Ssam u.u_oldmask = p->p_sigmask; 33412882Ssam p->p_flag |= SOMASK; 33539513Skarels p->p_sigmask = uap->mask &~ sigcantmask; 33640807Smarc (void) tsleep((caddr_t)&u, PPAUSE | PCATCH, "pause", 0); 33740807Smarc /* always return EINTR rather than ERESTART... */ 33844405Skarels return (EINTR); 3397499Sroot } 3407499Sroot 34142920Skarels /* ARGSUSED */ 34242920Skarels sigstack(p, uap, retval) 34342920Skarels struct proc *p; 34442920Skarels register struct args { 34512951Ssam struct sigstack *nss; 34612951Ssam struct sigstack *oss; 34742920Skarels } *uap; 34842920Skarels int *retval; 34942920Skarels { 35012951Ssam struct sigstack ss; 35139513Skarels int error = 0; 3527499Sroot 35339513Skarels if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack, 35439513Skarels (caddr_t)uap->oss, sizeof (struct sigstack)))) 35544405Skarels return (error); 35639513Skarels if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, 35739513Skarels sizeof (ss))) == 0) 35839513Skarels u.u_sigstack = ss; 35944405Skarels return (error); 3607499Sroot } 3617499Sroot 36242920Skarels /* ARGSUSED */ 36342920Skarels kill(cp, uap, retval) 36442920Skarels register struct proc *cp; 36542920Skarels register struct args { 36612882Ssam int pid; 36712882Ssam int signo; 36842920Skarels } *uap; 36942920Skarels int *retval; 37042920Skarels { 37118336Smckusick register struct proc *p; 3728032Sroot 37339513Skarels if ((unsigned) uap->signo >= NSIG) 37444405Skarels return (EINVAL); 37518336Smckusick if (uap->pid > 0) { 37618336Smckusick /* kill single process */ 37718336Smckusick p = pfind(uap->pid); 37839513Skarels if (p == 0) 37944405Skarels return (ESRCH); 38042920Skarels if (!CANSIGNAL(cp, p, uap->signo)) 38144405Skarels return (EPERM); 38239513Skarels if (uap->signo) 38318336Smckusick psignal(p, uap->signo); 38444405Skarels return (0); 38518336Smckusick } 38618336Smckusick switch (uap->pid) { 38718336Smckusick case -1: /* broadcast signal */ 38844405Skarels return (killpg1(cp, uap->signo, 0, 1)); 38918336Smckusick case 0: /* signal own process group */ 39044405Skarels return (killpg1(cp, uap->signo, 0, 0)); 39118336Smckusick default: /* negative explicit process group */ 39244405Skarels return (killpg1(cp, uap->signo, -uap->pid, 0)); 39318336Smckusick } 39439513Skarels /* NOTREACHED */ 3958032Sroot } 3968032Sroot 39739513Skarels #ifdef COMPAT_43 39842920Skarels /* ARGSUSED */ 39942920Skarels okillpg(p, uap, retval) 40042920Skarels struct proc *p; 40142920Skarels register struct args { 40237581Smckusick int pgid; 4039989Ssam int signo; 40442920Skarels } *uap; 40542920Skarels int *retval; 40642920Skarels { 4078032Sroot 40839513Skarels if ((unsigned) uap->signo >= NSIG) 40944405Skarels return (EINVAL); 41044405Skarels return (killpg1(p, uap->signo, uap->pgid, 0)); 4118032Sroot } 41239513Skarels #endif 4138032Sroot 41442920Skarels /* 41542920Skarels * Common code for kill process group/broadcast kill. 41642920Skarels * cp is calling process. 41742920Skarels */ 41842920Skarels killpg1(cp, signo, pgid, all) 41942920Skarels register struct proc *cp; 42037581Smckusick int signo, pgid, all; 4219989Ssam { 4229989Ssam register struct proc *p; 42337581Smckusick struct pgrp *pgrp; 42443364Smckusick int f = 0; 42537581Smckusick 42637581Smckusick if (all) 42737581Smckusick /* 42837581Smckusick * broadcast 4297421Sroot */ 43037581Smckusick for (p = allproc; p != NULL; p = p->p_nxt) { 43137581Smckusick if (p->p_ppid == 0 || p->p_flag&SSYS || 43242920Skarels p == u.u_procp || !CANSIGNAL(cp, p, signo)) 43337581Smckusick continue; 43437581Smckusick f++; 43537581Smckusick if (signo) 43637581Smckusick psignal(p, signo); 43737581Smckusick } 43837581Smckusick else { 43937581Smckusick if (pgid == 0) 44037581Smckusick /* 44137581Smckusick * zero pgid means send to my process group. 44237581Smckusick */ 44337581Smckusick pgrp = u.u_procp->p_pgrp; 44437581Smckusick else { 44537581Smckusick pgrp = pgfind(pgid); 44637581Smckusick if (pgrp == NULL) 44739513Skarels return (ESRCH); 44837581Smckusick } 44937581Smckusick for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) { 45039513Skarels if (p->p_ppid == 0 || p->p_flag&SSYS || 45142920Skarels !CANSIGNAL(cp, p, signo)) 45237581Smckusick continue; 45337581Smckusick f++; 45437581Smckusick if (signo) 45537581Smckusick psignal(p, signo); 45618336Smckusick } 4577421Sroot } 45843364Smckusick return (f ? 0 : ESRCH); 4597421Sroot } 4607421Sroot 46142920Skarels /* 4627421Sroot * Send the specified signal to 46337581Smckusick * all processes with 'pgid' as 4647421Sroot * process group. 4657421Sroot */ 46637581Smckusick gsignal(pgid, sig) 4677421Sroot { 46839513Skarels struct pgrp *pgrp; 4697421Sroot 47039513Skarels if (pgid && (pgrp = pgfind(pgid))) 47142207Smarc pgsignal(pgrp, sig, 0); 4727421Sroot } 47342920Skarels 47440807Smarc /* 47542207Smarc * Send sig to every member of a process group. 47642207Smarc * If checktty is 1, limit to members which have a controlling 47742207Smarc * terminal. 47840807Smarc */ 47942207Smarc pgsignal(pgrp, sig, checkctty) 48039513Skarels struct pgrp *pgrp; 48137581Smckusick { 48237581Smckusick register struct proc *p; 48337581Smckusick 48440807Smarc if (pgrp) 48540807Smarc for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) 48642207Smarc if (checkctty == 0 || p->p_flag&SCTTY) 48742207Smarc psignal(p, sig); 48837581Smckusick } 48937581Smckusick 4907421Sroot /* 49139513Skarels * Send a signal caused by a trap to the current process. 49239513Skarels * If it will be caught immediately, deliver it with correct code. 49339513Skarels * Otherwise, post it normally. 49439513Skarels */ 49539513Skarels trapsignal(sig, code) 49639513Skarels register int sig; 49739513Skarels unsigned code; 49839513Skarels { 49942920Skarels register struct proc *p = u.u_procp; /* XXX */ 50039513Skarels int mask; 50139513Skarels 50239513Skarels mask = sigmask(sig); 50339513Skarels if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 && 50439513Skarels (p->p_sigmask & mask) == 0) { 50539513Skarels u.u_ru.ru_nsignals++; 50640807Smarc #ifdef KTRACE 50740807Smarc if (KTRPOINT(p, KTR_PSIG)) 50840807Smarc ktrpsig(p->p_tracep, sig, u.u_signal[sig], 50940807Smarc p->p_sigmask, code); 51040807Smarc #endif 511*46008Swilliam #if defined(i386) 512*46008Swilliam sendsig(u.u_signal[sig], sig, p->p_sigmask, code, 0x100); 513*46008Swilliam #else 51439513Skarels sendsig(u.u_signal[sig], sig, p->p_sigmask, code); 515*46008Swilliam #endif 51639513Skarels p->p_sigmask |= u.u_sigmask[sig] | mask; 51739513Skarels } else { 51843895Skarels u.u_code = code; /* XXX for core dump/debugger */ 51939513Skarels psignal(p, sig); 52039513Skarels } 52139513Skarels } 52239513Skarels 52339513Skarels /* 52440807Smarc * Send the specified signal to the specified process. 52540807Smarc * Most signals do not do anything directly to a process; 52640807Smarc * they set a flag that asks the process to do something to itself. 52740807Smarc * Exceptions: 52840807Smarc * o When a stop signal is sent to a sleeping process that takes the default 52940807Smarc * action, the process is stopped without awakening it. 53040807Smarc * o SIGCONT restarts stopped processes (or puts them back to sleep) 53140807Smarc * regardless of the signal action (eg, blocked or ignored). 53240807Smarc * Other ignored signals are discarded immediately. 5337421Sroot */ 5347421Sroot psignal(p, sig) 5357421Sroot register struct proc *p; 5367421Sroot register int sig; 5377421Sroot { 5387421Sroot register int s; 53939513Skarels register sig_t action; 54017153Sbloom int mask; 5417421Sroot 54239513Skarels if ((unsigned)sig >= NSIG || sig == 0) 54339513Skarels panic("psignal sig"); 54417153Sbloom mask = sigmask(sig); 5457421Sroot 5467421Sroot /* 5477421Sroot * If proc is traced, always give parent a chance. 5487421Sroot */ 5497421Sroot if (p->p_flag & STRC) 5507421Sroot action = SIG_DFL; 5517421Sroot else { 5527421Sroot /* 55312882Ssam * If the signal is being ignored, 55412882Ssam * then we forget about it immediately. 55539513Skarels * (Note: we don't set SIGCONT in p_sigignore, 55639513Skarels * and if it is set to SIG_IGN, 55739513Skarels * action will be SIG_DFL here.) 5587421Sroot */ 55917153Sbloom if (p->p_sigignore & mask) 5607421Sroot return; 56117153Sbloom if (p->p_sigmask & mask) 56212882Ssam action = SIG_HOLD; 56317153Sbloom else if (p->p_sigcatch & mask) 56412882Ssam action = SIG_CATCH; 56542437Skarels else 56612882Ssam action = SIG_DFL; 5677421Sroot } 56839513Skarels switch (sig) { 5697421Sroot 57039513Skarels case SIGTERM: 57139513Skarels if ((p->p_flag&STRC) || action != SIG_DFL) 5727421Sroot break; 57339513Skarels /* FALLTHROUGH */ 5747421Sroot 57539513Skarels case SIGKILL: 57639513Skarels if (p->p_nice > NZERO) 57739513Skarels p->p_nice = NZERO; 57839513Skarels break; 5797421Sroot 58039513Skarels case SIGCONT: 58139513Skarels p->p_sig &= ~stopsigmask; 58239513Skarels break; 58339513Skarels 58439513Skarels case SIGTSTP: 58539513Skarels case SIGTTIN: 58639513Skarels case SIGTTOU: 58745672Skarels /* 58845672Skarels * If sending a tty stop signal to a member of an orphaned 58945672Skarels * process group, discard the signal here if the action 59045672Skarels * is default; don't stop the process below if sleeping, 59145672Skarels * and don't clear any pending SIGCONT. 59245672Skarels */ 59345672Skarels if (p->p_pgrp->pg_jobc == 0 && action == SIG_DFL) 59445741Smckusick return; 59545672Skarels /* FALLTHROUGH */ 59645672Skarels 59739513Skarels case SIGSTOP: 59839513Skarels p->p_sig &= ~sigmask(SIGCONT); 59939513Skarels break; 6007421Sroot } 60139513Skarels p->p_sig |= mask; 60239513Skarels 6037421Sroot /* 60439513Skarels * Defer further processing for signals which are held, 60539513Skarels * except that stopped processes must be continued by SIGCONT. 6067421Sroot */ 60739513Skarels if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP)) 6087421Sroot return; 60917153Sbloom s = splhigh(); 6107421Sroot switch (p->p_stat) { 6117421Sroot 6127421Sroot case SSLEEP: 6137421Sroot /* 61440807Smarc * If process is sleeping uninterruptibly 6157421Sroot * we can't interrupt the sleep... the signal will 6167421Sroot * be noticed when the process returns through 6177421Sroot * trap() or syscall(). 6187421Sroot */ 61940807Smarc if ((p->p_flag & SSINTR) == 0) 6207421Sroot goto out; 6217421Sroot /* 6227421Sroot * Process is sleeping and traced... make it runnable 6237421Sroot * so it can discover the signal in issig() and stop 6247421Sroot * for the parent. 6257421Sroot */ 6267421Sroot if (p->p_flag&STRC) 6277421Sroot goto run; 62839513Skarels /* 62939513Skarels * When a sleeping process receives a stop 63039513Skarels * signal, process immediately if possible. 63139513Skarels * All other (caught or default) signals 63239513Skarels * cause the process to run. 63339513Skarels */ 63439513Skarels if (mask & stopsigmask) { 6357421Sroot if (action != SIG_DFL) 63639513Skarels goto runfast; 6377421Sroot /* 6387421Sroot * If a child in vfork(), stopping could 6397421Sroot * cause deadlock. 6407421Sroot */ 6417421Sroot if (p->p_flag&SVFORK) 6427421Sroot goto out; 64317153Sbloom p->p_sig &= ~mask; 64443895Skarels p->p_xstat = sig; 64539513Skarels if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0) 64639513Skarels psignal(p->p_pptr, SIGCHLD); 6477421Sroot stop(p); 6487421Sroot goto out; 64939513Skarels } else 65039513Skarels goto runfast; 6517421Sroot /*NOTREACHED*/ 6527421Sroot 6537421Sroot case SSTOP: 6547421Sroot /* 6557421Sroot * If traced process is already stopped, 6567421Sroot * then no further action is necessary. 6577421Sroot */ 6587421Sroot if (p->p_flag&STRC) 6597421Sroot goto out; 6607421Sroot switch (sig) { 6617421Sroot 6627421Sroot case SIGKILL: 6637421Sroot /* 6647421Sroot * Kill signal always sets processes running. 6657421Sroot */ 66639513Skarels goto runfast; 6677421Sroot 6687421Sroot case SIGCONT: 6697421Sroot /* 67039513Skarels * If SIGCONT is default (or ignored), we continue 67139513Skarels * the process but don't leave the signal in p_sig, 67239513Skarels * as it has no further action. If SIGCONT is held, 67339513Skarels * continue the process and leave the signal in p_sig. 6747421Sroot * If the process catches SIGCONT, let it handle 6757421Sroot * the signal itself. If it isn't waiting on 6767421Sroot * an event, then it goes back to run state. 6777421Sroot * Otherwise, process goes back to sleep state. 6787421Sroot */ 67939513Skarels if (action == SIG_DFL) 68039513Skarels p->p_sig &= ~mask; 68139513Skarels if (action == SIG_CATCH) 68239513Skarels goto runfast; 68339513Skarels if (p->p_wchan == 0) 6847421Sroot goto run; 6857421Sroot p->p_stat = SSLEEP; 6867421Sroot goto out; 6877421Sroot 6887421Sroot case SIGSTOP: 6897421Sroot case SIGTSTP: 6907421Sroot case SIGTTIN: 6917421Sroot case SIGTTOU: 6927421Sroot /* 6937421Sroot * Already stopped, don't need to stop again. 6947421Sroot * (If we did the shell could get confused.) 6957421Sroot */ 69617153Sbloom p->p_sig &= ~mask; /* take it away */ 6977421Sroot goto out; 6987421Sroot 6997421Sroot default: 7007421Sroot /* 7017421Sroot * If process is sleeping interruptibly, then 70240807Smarc * simulate a wakeup so that when it is continued, 70340807Smarc * it will be made runnable and can look at the signal. 70440807Smarc * But don't setrun the process, leave it stopped. 7057421Sroot */ 70640807Smarc if (p->p_wchan && p->p_flag & SSINTR) 7077421Sroot unsleep(p); 7087421Sroot goto out; 7097421Sroot } 7107421Sroot /*NOTREACHED*/ 7117421Sroot 7127421Sroot default: 7137421Sroot /* 7147421Sroot * SRUN, SIDL, SZOMB do nothing with the signal, 7157421Sroot * other than kicking ourselves if we are running. 7167421Sroot * It will either never be noticed, or noticed very soon. 7177421Sroot */ 7187421Sroot if (p == u.u_procp && !noproc) 7197421Sroot aston(); 7207421Sroot goto out; 7217421Sroot } 7227421Sroot /*NOTREACHED*/ 72339513Skarels 72439513Skarels runfast: 7257421Sroot /* 7267421Sroot * Raise priority to at least PUSER. 7277421Sroot */ 7287421Sroot if (p->p_pri > PUSER) 72917399Skarels p->p_pri = PUSER; 73039513Skarels run: 7317421Sroot setrun(p); 7327421Sroot out: 7337421Sroot splx(s); 7347421Sroot } 7357421Sroot 7367421Sroot /* 73740807Smarc * If the current process has a signal to process (should be caught 73840807Smarc * or cause termination, should interrupt current syscall), 73940807Smarc * return the signal number. Stop signals with default action 74040807Smarc * are processed immediately, then cleared; they aren't returned. 7417421Sroot * This is asked at least once each time a process enters the 7427421Sroot * system (though this can usually be done without actually 7437421Sroot * calling issig by checking the pending signal masks.) 7447421Sroot */ 7457421Sroot issig() 7467421Sroot { 74742926Smckusick register struct proc *p = u.u_procp; /* XXX */ 74839513Skarels register int sig, mask; 7497421Sroot 7507421Sroot for (;;) { 75139513Skarels mask = p->p_sig &~ p->p_sigmask; 7527421Sroot if (p->p_flag&SVFORK) 75339513Skarels mask &= ~stopsigmask; 75440807Smarc if (mask == 0) /* no signal to send */ 75540807Smarc return (0); 75639513Skarels sig = ffs((long)mask); 75717153Sbloom mask = sigmask(sig); 75840807Smarc /* 75940807Smarc * We should see pending but ignored signals 76040807Smarc * only if STRC was on when they were posted. 76140807Smarc */ 76240807Smarc if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) { 76340807Smarc p->p_sig &= ~mask; 76440807Smarc continue; 76540807Smarc } 76612882Ssam if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) { 7677421Sroot /* 7687421Sroot * If traced, always stop, and stay 7697421Sroot * stopped until released by the parent. 7707421Sroot */ 77143895Skarels p->p_xstat = sig; 77218331Skarels psignal(p->p_pptr, SIGCHLD); 7737421Sroot do { 7747421Sroot stop(p); 7757421Sroot swtch(); 77642926Smckusick } while (!procxmt(p) && p->p_flag&STRC); 7777421Sroot 7787421Sroot /* 77914782Ssam * If the traced bit got turned off, 78040807Smarc * go back up to the top to rescan signals. 78114782Ssam * This ensures that p_sig* and u_signal are consistent. 7827421Sroot */ 78340807Smarc if ((p->p_flag&STRC) == 0) 7847421Sroot continue; 7857421Sroot 7867421Sroot /* 7877421Sroot * If parent wants us to take the signal, 78843895Skarels * then it will leave it in p->p_xstat; 7897421Sroot * otherwise we just look for signals again. 7907421Sroot */ 79140807Smarc p->p_sig &= ~mask; /* clear the old signal */ 79243895Skarels sig = p->p_xstat; 7937421Sroot if (sig == 0) 7947421Sroot continue; 79514782Ssam 79614782Ssam /* 79740807Smarc * Put the new signal into p_sig. 79840807Smarc * If signal is being masked, 79940807Smarc * look for other signals. 80014782Ssam */ 80117153Sbloom mask = sigmask(sig); 80240807Smarc p->p_sig |= mask; 80340807Smarc if (p->p_sigmask & mask) 80414782Ssam continue; 8057421Sroot } 80640807Smarc 80740807Smarc /* 80840807Smarc * Decide whether the signal should be returned. 80940807Smarc * Return the signal's number, or fall through 81040807Smarc * to clear it from the pending mask. 81140807Smarc */ 81224901Skarels switch ((int)u.u_signal[sig]) { 8137421Sroot 8147421Sroot case SIG_DFL: 8157421Sroot /* 8167421Sroot * Don't take default actions on system processes. 8177421Sroot */ 8187421Sroot if (p->p_ppid == 0) 81940807Smarc break; /* == ignore */ 82040807Smarc /* 82140807Smarc * If there is a pending stop signal to process 82240807Smarc * with default action, stop here, 82342437Skarels * then clear the signal. However, 82442437Skarels * if process is member of an orphaned 82542437Skarels * process group, ignore tty stop signals. 82640807Smarc */ 82739513Skarels if (mask & stopsigmask) { 82842437Skarels if (p->p_flag&STRC || 82942437Skarels (p->p_pgrp->pg_jobc == 0 && 83042437Skarels mask & ttystopsigmask)) 83140807Smarc break; /* == ignore */ 83243895Skarels p->p_xstat = sig; 8337421Sroot stop(p); 83439513Skarels if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0) 83539513Skarels psignal(p->p_pptr, SIGCHLD); 8367421Sroot swtch(); 83740807Smarc break; 83839513Skarels } else if (mask & defaultignmask) { 8397421Sroot /* 84039513Skarels * Except for SIGCONT, shouldn't get here. 84139513Skarels * Default action is to ignore; drop it. 8427421Sroot */ 84340807Smarc break; /* == ignore */ 84439513Skarels } else 84540807Smarc return (sig); 8467421Sroot /*NOTREACHED*/ 8477421Sroot 8487421Sroot case SIG_IGN: 8497421Sroot /* 85039513Skarels * Masking above should prevent us ever trying 85139513Skarels * to take action on an ignored signal other 85239513Skarels * than SIGCONT, unless process is traced. 8537421Sroot */ 85439513Skarels if (sig != SIGCONT && (p->p_flag&STRC) == 0) 8557421Sroot printf("issig\n"); 85640807Smarc break; /* == ignore */ 8577421Sroot 8587421Sroot default: 8597421Sroot /* 8607421Sroot * This signal has an action, let 8617421Sroot * psig process it. 8627421Sroot */ 86340807Smarc return (sig); 8647421Sroot } 86540807Smarc p->p_sig &= ~mask; /* take the signal! */ 8667421Sroot } 86740807Smarc /* NOTREACHED */ 8687421Sroot } 8697421Sroot 8707421Sroot /* 8717421Sroot * Put the argument process into the stopped 87218331Skarels * state and notify the parent via wakeup. 87318331Skarels * Signals are handled elsewhere. 87440807Smarc * The process must not be on the run queue. 8757421Sroot */ 8767421Sroot stop(p) 8777421Sroot register struct proc *p; 8787421Sroot { 8797421Sroot 8807421Sroot p->p_stat = SSTOP; 8817421Sroot p->p_flag &= ~SWTED; 8827421Sroot wakeup((caddr_t)p->p_pptr); 8837421Sroot } 8847421Sroot 8857421Sroot /* 88640807Smarc * Perform the action specified by the current signal. 8877421Sroot * The usual sequence is: 88840807Smarc * if (sig = CURSIG(p)) 88940807Smarc * psig(sig); 8907421Sroot */ 891*46008Swilliam #if defined(i386) 892*46008Swilliam psig(sig, flags) 893*46008Swilliam #else 89440807Smarc psig(sig) 895*46008Swilliam #endif 89640807Smarc register int sig; 8977421Sroot { 89812882Ssam register struct proc *p = u.u_procp; 89939513Skarels int mask, returnmask; 90039513Skarels register sig_t action; 9017421Sroot 90239513Skarels do { 90340807Smarc #ifdef DIAGNOSTIC 90439513Skarels if (sig == 0) 90539513Skarels panic("psig"); 90640807Smarc #endif 90740807Smarc mask = sigmask(sig); 90840807Smarc p->p_sig &= ~mask; 90939513Skarels action = u.u_signal[sig]; 91040807Smarc #ifdef KTRACE 91140807Smarc if (KTRPOINT(p, KTR_PSIG)) 91240807Smarc ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ? 91340807Smarc u.u_oldmask : p->p_sigmask, 0); 91440807Smarc #endif 91539513Skarels if (action != SIG_DFL) { 91639513Skarels #ifdef DIAGNOSTIC 91739513Skarels if (action == SIG_IGN || (p->p_sigmask & mask)) 91839513Skarels panic("psig action"); 91939513Skarels #endif 92039513Skarels /* 92139513Skarels * Set the new mask value and also defer further 92239513Skarels * occurences of this signal. 92339513Skarels * 92439513Skarels * Special case: user has done a sigpause. Here the 92539513Skarels * current mask is not of interest, but rather the 92639513Skarels * mask from before the sigpause is what we want 92739513Skarels * restored after the signal processing is completed. 92839513Skarels */ 92939513Skarels (void) splhigh(); 93039513Skarels if (p->p_flag & SOMASK) { 93139513Skarels returnmask = u.u_oldmask; 93239513Skarels p->p_flag &= ~SOMASK; 93339513Skarels } else 93439513Skarels returnmask = p->p_sigmask; 93539513Skarels p->p_sigmask |= u.u_sigmask[sig] | mask; 93639513Skarels (void) spl0(); 93739513Skarels u.u_ru.ru_nsignals++; 938*46008Swilliam #if defined(i386) 939*46008Swilliam sendsig(action, sig, returnmask, 0, flags); 940*46008Swilliam #else 94139513Skarels sendsig(action, sig, returnmask, 0); 942*46008Swilliam #endif 94339513Skarels continue; 9447421Sroot } 94539513Skarels u.u_acflag |= AXSIG; 94639513Skarels switch (sig) { 9477421Sroot 94839513Skarels case SIGILL: 94939513Skarels case SIGIOT: 95039513Skarels case SIGBUS: 95139513Skarels case SIGQUIT: 95239513Skarels case SIGTRAP: 95339513Skarels case SIGEMT: 95439513Skarels case SIGFPE: 95539513Skarels case SIGSEGV: 95639513Skarels case SIGSYS: 95743895Skarels u.u_sig = sig; 95839513Skarels if (core() == 0) 95939513Skarels sig |= WCOREFLAG; 96039513Skarels } 96142926Smckusick exit(p, W_EXITCODE(0, sig)); 96239513Skarels /* NOTREACHED */ 96340807Smarc } while (sig = CURSIG(p)); 9647421Sroot } 9657421Sroot 9667421Sroot /* 96739513Skarels * Create a core image on the file "core". 9687421Sroot * It writes UPAGES block of the 9697421Sroot * user.h area followed by the entire 9707421Sroot * data+stack segments. 9717421Sroot */ 9727421Sroot core() 9737421Sroot { 97437728Smckusick register struct vnode *vp; 97539513Skarels register struct proc *p = u.u_procp; 97616692Smckusick register struct nameidata *ndp = &u.u_nd; 97737580Smckusick struct vattr vattr; 97837580Smckusick int error; 9797421Sroot 98039513Skarels if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid) 98137580Smckusick return (EFAULT); 98237580Smckusick if (ctob(UPAGES + u.u_dsize + u.u_ssize) >= 9838032Sroot u.u_rlimit[RLIMIT_CORE].rlim_cur) 98437580Smckusick return (EFAULT); 98516692Smckusick ndp->ni_segflg = UIO_SYSSPACE; 98616692Smckusick ndp->ni_dirp = "core"; 98737580Smckusick if (error = vn_open(ndp, FCREAT|FWRITE, 0644)) 98837580Smckusick return (error); 98937580Smckusick vp = ndp->ni_vp; 99038394Smckusick VOP_LOCK(vp); 99137580Smckusick if (vp->v_type != VREG || 99237728Smckusick VOP_GETATTR(vp, &vattr, u.u_cred) || 99337580Smckusick vattr.va_nlink != 1) { 99438394Smckusick vput(vp); 99538394Smckusick return (EFAULT); 9967818Sroot } 99741362Smckusick VATTR_NULL(&vattr); 99837580Smckusick vattr.va_size = 0; 99937728Smckusick VOP_SETATTR(vp, &vattr, u.u_cred); 10007818Sroot u.u_acflag |= ACORE; 100142004Smckusick #ifdef HPUXCOMPAT 100242004Smckusick /* 100342004Smckusick * BLETCH! If we loaded from an HPUX format binary file 100442004Smckusick * we have to dump an HPUX style user struct so that the 100542004Smckusick * HPUX debuggers can grok it. 100642004Smckusick */ 100742004Smckusick if (u.u_pcb.pcb_flags & PCB_HPUXBIN) 100842004Smckusick error = hpuxdumpu(vp, ndp->ni_cred); 100942004Smckusick else 101042004Smckusick #endif 101137580Smckusick error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0, 101238394Smckusick UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 101337580Smckusick if (error == 0) 101445741Smckusick error = vn_rdwr(UIO_WRITE, vp, u.u_daddr, 101538394Smckusick (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE, 101638394Smckusick IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 101737580Smckusick if (error == 0) 101837580Smckusick error = vn_rdwr(UIO_WRITE, vp, 101945741Smckusick trunc_page(USRSTACK - ctob(u.u_ssize)), 102045741Smckusick round_page(ctob(u.u_ssize)), 102138394Smckusick (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE, 102238394Smckusick IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 102338394Smckusick vput(vp); 102437580Smckusick return (error); 10257421Sroot } 102639513Skarels 102739513Skarels /* 102839513Skarels * Nonexistent system call-- signal process (may want to handle it). 102939513Skarels * Flag error in case process won't see signal immediately (blocked or ignored). 103039513Skarels */ 103143364Smckusick /* ARGSUSED */ 103243364Smckusick nosys(p, args, retval) 103343364Smckusick struct proc *p; 103443364Smckusick void *args; 103543364Smckusick int *retval; 103639513Skarels { 103739513Skarels 103843364Smckusick psignal(p, SIGSYS); 103944405Skarels return (EINVAL); 104039513Skarels } 1041