123374Smckusick /* 237580Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337580Smckusick * All rights reserved. 423374Smckusick * 537580Smckusick * Redistribution and use in source and binary forms are permitted 637580Smckusick * provided that the above copyright notice and this paragraph are 737580Smckusick * duplicated in all such forms and that any documentation, 837580Smckusick * advertising materials, and other materials related to such 937580Smckusick * distribution and use acknowledge that the software was developed 1037580Smckusick * by the University of California, Berkeley. The name of the 1137580Smckusick * University may not be used to endorse or promote products derived 1237580Smckusick * from this software without specific prior written permission. 1337580Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1437580Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1537580Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1637580Smckusick * 17*42920Skarels * @(#)kern_sig.c 7.18 (Berkeley) 06/05/90 1823374Smckusick */ 197421Sroot 2017092Sbloom #include "param.h" 2117092Sbloom #include "systm.h" 2239513Skarels #include "syscontext.h" /* XXX */ 2337580Smckusick #include "vnode.h" 2417092Sbloom #include "proc.h" 2517092Sbloom #include "timeb.h" 2617092Sbloom #include "times.h" 2717092Sbloom #include "buf.h" 2817092Sbloom #include "text.h" 2917092Sbloom #include "seg.h" 3017092Sbloom #include "vm.h" 3117092Sbloom #include "acct.h" 3217092Sbloom #include "uio.h" 3337580Smckusick #include "file.h" 3417092Sbloom #include "kernel.h" 3539513Skarels #include "wait.h" 3640807Smarc #include "ktrace.h" 377421Sroot 3837581Smckusick #include "machine/reg.h" 3937581Smckusick #include "machine/pte.h" 4037581Smckusick #include "machine/psl.h" 4137581Smckusick #include "machine/mtpr.h" 4237581Smckusick 4342437Skarels #define ttystopsigmask (sigmask(SIGTSTP)|sigmask(SIGTTIN)|sigmask(SIGTTOU)) 4442437Skarels #define stopsigmask (sigmask(SIGSTOP)|ttystopsigmask) 4539513Skarels #define defaultignmask (sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \ 4639513Skarels sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO)) 4712951Ssam 4817013Smckusick /* 49*42920Skarels * Can process p send the signal signo to process q? 5017013Smckusick */ 51*42920Skarels #define CANSIGNAL(p, q, signo) \ 52*42920Skarels ((p)->p_uid == 0 || \ 53*42920Skarels (p)->p_ruid == (q)->p_ruid || (p)->p_uid == (q)->p_ruid || \ 54*42920Skarels (p)->p_ruid == (q)->p_uid || (p)->p_uid == (q)->p_uid || \ 55*42920Skarels ((signo) == SIGCONT && (q)->p_session == (p)->p_session)) 5639513Skarels 57*42920Skarels /* ARGSUSED */ 58*42920Skarels sigaction(p, uap, retval) 59*42920Skarels struct proc *p; 60*42920Skarels register struct args { 6112882Ssam int signo; 6239513Skarels struct sigaction *nsa; 6339513Skarels struct sigaction *osa; 64*42920Skarels } *uap; 65*42920Skarels int *retval; 66*42920Skarels { 6739513Skarels struct sigaction vec; 6839513Skarels register struct sigaction *sa; 6912882Ssam register int sig; 7039513Skarels int bit, error; 717421Sroot 7212882Ssam sig = uap->signo; 7339513Skarels if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) 7439513Skarels RETURN (EINVAL); 7539513Skarels sa = &vec; 7639513Skarels if (uap->osa) { 7739513Skarels sa->sa_handler = u.u_signal[sig]; 7839513Skarels sa->sa_mask = u.u_sigmask[sig]; 7918308Smckusick bit = sigmask(sig); 8039513Skarels sa->sa_flags = 0; 8118308Smckusick if ((u.u_sigonstack & bit) != 0) 8239513Skarels sa->sa_flags |= SA_ONSTACK; 8339513Skarels if ((u.u_sigintr & bit) == 0) 8439513Skarels sa->sa_flags |= SA_RESTART; 85*42920Skarels if (p->p_flag & SNOCLDSTOP) 8639513Skarels sa->sa_flags |= SA_NOCLDSTOP; 8739513Skarels if (error = copyout((caddr_t)sa, (caddr_t)uap->osa, 8839513Skarels sizeof (vec))) 8939513Skarels RETURN (error); 9012951Ssam } 9139513Skarels if (uap->nsa) { 9239513Skarels if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa, 9339513Skarels sizeof (vec))) 9439513Skarels RETURN (error); 95*42920Skarels setsigvec(p, sig, sa); 9612951Ssam } 9739513Skarels RETURN (0); 987421Sroot } 997421Sroot 100*42920Skarels setsigvec(p, sig, sa) 101*42920Skarels register struct proc *p; 10212951Ssam int sig; 10339513Skarels register struct sigaction *sa; 10412882Ssam { 10512951Ssam register int bit; 10612882Ssam 10717153Sbloom bit = sigmask(sig); 10812882Ssam /* 10912882Ssam * Change setting atomically. 11012882Ssam */ 11117153Sbloom (void) splhigh(); 11239513Skarels u.u_signal[sig] = sa->sa_handler; 11339513Skarels u.u_sigmask[sig] = sa->sa_mask &~ sigcantmask; 11439513Skarels if ((sa->sa_flags & SA_RESTART) == 0) 11518308Smckusick u.u_sigintr |= bit; 11618308Smckusick else 11718308Smckusick u.u_sigintr &= ~bit; 11839513Skarels if (sa->sa_flags & SA_ONSTACK) 11912951Ssam u.u_sigonstack |= bit; 12012951Ssam else 12112951Ssam u.u_sigonstack &= ~bit; 12239513Skarels if (sig == SIGCHLD) { 12339513Skarels if (sa->sa_flags & SA_NOCLDSTOP) 12439513Skarels p->p_flag |= SNOCLDSTOP; 12539513Skarels else 12639513Skarels p->p_flag &= ~SNOCLDSTOP; 12739513Skarels } 12839513Skarels /* 12939513Skarels * Set bit in p_sigignore for signals that are set to SIG_IGN, 13039513Skarels * and for signals set to SIG_DFL where the default is to ignore. 13139513Skarels * However, don't put SIGCONT in p_sigignore, 13239513Skarels * as we have to restart the process. 13339513Skarels */ 13439513Skarels if (sa->sa_handler == SIG_IGN || 13539513Skarels (bit & defaultignmask && sa->sa_handler == SIG_DFL)) { 13612951Ssam p->p_sig &= ~bit; /* never to be seen again */ 13739513Skarels if (sig != SIGCONT) 13839513Skarels p->p_sigignore |= bit; /* easier in psignal */ 13912951Ssam p->p_sigcatch &= ~bit; 14012882Ssam } else { 14112951Ssam p->p_sigignore &= ~bit; 14239513Skarels if (sa->sa_handler == SIG_DFL) 14312951Ssam p->p_sigcatch &= ~bit; 14412882Ssam else 14512951Ssam p->p_sigcatch |= bit; 14612882Ssam } 14712882Ssam (void) spl0(); 14812882Ssam } 14912882Ssam 15039513Skarels /* 15139513Skarels * Initialize signal state for process 0; 15239513Skarels * set to ignore signals that are ignored by default. 15339513Skarels */ 15439513Skarels siginit(p) 15539513Skarels struct proc *p; 1567421Sroot { 15739513Skarels 15839513Skarels p->p_sigignore = defaultignmask &~ sigmask(SIGCONT); 15939513Skarels } 16039513Skarels 16139513Skarels /* 16239513Skarels * Reset signals for an exec of the specified process. 16339513Skarels */ 16439513Skarels execsigs(p) 16539513Skarels register struct proc *p; 16639513Skarels { 16739513Skarels register int nc, mask; 16839513Skarels 16939513Skarels /* 17039513Skarels * Reset caught signals. Held signals remain held 17139513Skarels * through p_sigmask (unless they were caught, 17239513Skarels * and are now ignored by default). 17339513Skarels */ 17439513Skarels while (p->p_sigcatch) { 17539513Skarels nc = ffs((long)p->p_sigcatch); 17639513Skarels mask = sigmask(nc); 17739513Skarels p->p_sigcatch &= ~mask; 17839513Skarels if (mask & defaultignmask) { 17939513Skarels if (nc != SIGCONT) 18039513Skarels p->p_sigignore |= mask; 18139513Skarels p->p_sig &= ~mask; 18239513Skarels } 18339513Skarels u.u_signal[nc] = SIG_DFL; 18439513Skarels } 18539513Skarels /* 18639513Skarels * Reset stack state to the user stack. 18739513Skarels * Clear set of signals caught on the signal stack. 18839513Skarels */ 18939513Skarels u.u_onstack = 0; 19039513Skarels u.u_sigsp = 0; 19139513Skarels u.u_sigonstack = 0; 19239513Skarels } 19339513Skarels 19439513Skarels /* 19539513Skarels * Manipulate signal mask. 19639513Skarels * Note that we receive new mask, not pointer, 19739513Skarels * and return old mask as return value; 19839513Skarels * the library stub does the rest. 19939513Skarels */ 200*42920Skarels sigprocmask(p, uap, retval) 201*42920Skarels register struct proc *p; 202*42920Skarels struct args { 20339513Skarels int how; 20439513Skarels sigset_t mask; 205*42920Skarels } *uap; 206*42920Skarels int *retval; 207*42920Skarels { 20839513Skarels int error = 0; 20939513Skarels 210*42920Skarels *retval = p->p_sigmask; 21139513Skarels (void) splhigh(); 21239513Skarels 21339513Skarels switch (uap->how) { 21439513Skarels case SIG_BLOCK: 21539513Skarels p->p_sigmask |= uap->mask &~ sigcantmask; 21639513Skarels break; 21739513Skarels 21839513Skarels case SIG_UNBLOCK: 21939513Skarels p->p_sigmask &= ~uap->mask; 22039513Skarels break; 22139513Skarels 22239513Skarels case SIG_SETMASK: 22339513Skarels p->p_sigmask = uap->mask &~ sigcantmask; 22439513Skarels break; 22539513Skarels 22639513Skarels default: 22739513Skarels error = EINVAL; 22839513Skarels break; 22939513Skarels } 23039513Skarels (void) spl0(); 23139513Skarels RETURN (error); 23239513Skarels } 23339513Skarels 234*42920Skarels /* ARGSUSED */ 235*42920Skarels sigpending(p, uap, retval) 236*42920Skarels struct proc *p; 237*42920Skarels void *uap; 238*42920Skarels int *retval; 23939513Skarels { 24039513Skarels 241*42920Skarels *retval = p->p_sig; 24239513Skarels RETURN (0); 24339513Skarels } 24439513Skarels 24539513Skarels #ifdef COMPAT_43 24639513Skarels /* 24739513Skarels * Generalized interface signal handler, 4.3-compatible. 24839513Skarels */ 249*42920Skarels /* ARGSUSED */ 250*42920Skarels osigvec(p, uap, retval) 251*42920Skarels struct proc *p; 252*42920Skarels register struct args { 25339513Skarels int signo; 25439513Skarels struct sigvec *nsv; 25539513Skarels struct sigvec *osv; 256*42920Skarels } *uap; 257*42920Skarels int *retval; 258*42920Skarels { 25939513Skarels struct sigvec vec; 26039513Skarels register struct sigvec *sv; 26139513Skarels register int sig; 26239513Skarels int bit, error; 26339513Skarels 26439513Skarels sig = uap->signo; 26539513Skarels if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) 26639513Skarels RETURN (EINVAL); 26739513Skarels sv = &vec; 26839513Skarels if (uap->osv) { 26939513Skarels *(sig_t *)&sv->sv_handler = u.u_signal[sig]; 27039513Skarels sv->sv_mask = u.u_sigmask[sig]; 27139513Skarels bit = sigmask(sig); 27239513Skarels sv->sv_flags = 0; 27339513Skarels if ((u.u_sigonstack & bit) != 0) 27439513Skarels sv->sv_flags |= SV_ONSTACK; 27539513Skarels if ((u.u_sigintr & bit) != 0) 27639513Skarels sv->sv_flags |= SV_INTERRUPT; 277*42920Skarels if (p->p_flag & SNOCLDSTOP) 27839513Skarels sv->sv_flags |= SA_NOCLDSTOP; 27939513Skarels if (error = copyout((caddr_t)sv, (caddr_t)uap->osv, 28039513Skarels sizeof (vec))) 28139513Skarels RETURN (error); 28239513Skarels } 28339513Skarels if (uap->nsv) { 28439513Skarels if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv, 28539513Skarels sizeof (vec))) 28639513Skarels RETURN (error); 28739513Skarels sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 288*42920Skarels setsigvec(p, sig, (struct sigaction *)sv); 28939513Skarels } 29039513Skarels RETURN (0); 29139513Skarels } 29239513Skarels 293*42920Skarels osigblock(p, uap, retval) 294*42920Skarels register struct proc *p; 295*42920Skarels struct args { 296*42920Skarels int mask; 297*42920Skarels } *uap; 298*42920Skarels int *retval; 29939513Skarels { 3007499Sroot 30117153Sbloom (void) splhigh(); 302*42920Skarels *retval = p->p_sigmask; 30339513Skarels p->p_sigmask |= uap->mask &~ sigcantmask; 30412882Ssam (void) spl0(); 30539513Skarels RETURN (0); 3067499Sroot } 3077499Sroot 308*42920Skarels osigsetmask(p, uap, retval) 309*42920Skarels struct proc *p; 310*42920Skarels struct args { 311*42920Skarels int mask; 312*42920Skarels } *uap; 313*42920Skarels int *retval; 3147499Sroot { 3157499Sroot 31617153Sbloom (void) splhigh(); 317*42920Skarels *retval = p->p_sigmask; 31839513Skarels p->p_sigmask = uap->mask &~ sigcantmask; 31912882Ssam (void) spl0(); 32039513Skarels RETURN (0); 3217499Sroot } 32239513Skarels #endif 3237499Sroot 32439513Skarels /* 32539513Skarels * Suspend process until signal, providing mask to be set 32639513Skarels * in the meantime. Note nonstandard calling convention: 32739513Skarels * libc stub passes mask, not pointer, to save a copyin. 32839513Skarels */ 329*42920Skarels /* ARGSUSED */ 330*42920Skarels sigsuspend(p, uap, retval) 331*42920Skarels register struct proc *p; 332*42920Skarels struct args { 333*42920Skarels sigset_t mask; 334*42920Skarels } *uap; 335*42920Skarels int *retval; 3367499Sroot { 3377499Sroot 33812882Ssam /* 33912882Ssam * When returning from sigpause, we want 34012882Ssam * the old mask to be restored after the 34112882Ssam * signal handler has finished. Thus, we 34212882Ssam * save it here and mark the proc structure 34312882Ssam * to indicate this (should be in u.). 34412882Ssam */ 34512882Ssam u.u_oldmask = p->p_sigmask; 34612882Ssam p->p_flag |= SOMASK; 34739513Skarels p->p_sigmask = uap->mask &~ sigcantmask; 34840807Smarc (void) tsleep((caddr_t)&u, PPAUSE | PCATCH, "pause", 0); 34940807Smarc /* always return EINTR rather than ERESTART... */ 35040807Smarc RETURN (EINTR); 3517499Sroot } 3527499Sroot 353*42920Skarels /* ARGSUSED */ 354*42920Skarels sigstack(p, uap, retval) 355*42920Skarels struct proc *p; 356*42920Skarels register struct args { 35712951Ssam struct sigstack *nss; 35812951Ssam struct sigstack *oss; 359*42920Skarels } *uap; 360*42920Skarels int *retval; 361*42920Skarels { 36212951Ssam struct sigstack ss; 36339513Skarels int error = 0; 3647499Sroot 36539513Skarels if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack, 36639513Skarels (caddr_t)uap->oss, sizeof (struct sigstack)))) 36739513Skarels RETURN (error); 36839513Skarels if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, 36939513Skarels sizeof (ss))) == 0) 37039513Skarels u.u_sigstack = ss; 37139513Skarels RETURN (error); 3727499Sroot } 3737499Sroot 374*42920Skarels /* ARGSUSED */ 375*42920Skarels kill(cp, uap, retval) 376*42920Skarels register struct proc *cp; 377*42920Skarels register struct args { 37812882Ssam int pid; 37912882Ssam int signo; 380*42920Skarels } *uap; 381*42920Skarels int *retval; 382*42920Skarels { 38318336Smckusick register struct proc *p; 3848032Sroot 38539513Skarels if ((unsigned) uap->signo >= NSIG) 38639513Skarels RETURN (EINVAL); 38718336Smckusick if (uap->pid > 0) { 38818336Smckusick /* kill single process */ 38918336Smckusick p = pfind(uap->pid); 39039513Skarels if (p == 0) 39139513Skarels RETURN (ESRCH); 392*42920Skarels if (!CANSIGNAL(cp, p, uap->signo)) 39339513Skarels RETURN (EPERM); 39439513Skarels if (uap->signo) 39518336Smckusick psignal(p, uap->signo); 39639513Skarels RETURN (0); 39718336Smckusick } 39818336Smckusick switch (uap->pid) { 39918336Smckusick case -1: /* broadcast signal */ 400*42920Skarels RETURN (killpg1(cp, uap->signo, 0, 1)); 40118336Smckusick case 0: /* signal own process group */ 402*42920Skarels RETURN (killpg1(cp, uap->signo, 0, 0)); 40318336Smckusick default: /* negative explicit process group */ 404*42920Skarels RETURN (killpg1(cp, uap->signo, -uap->pid, 0)); 40518336Smckusick } 40639513Skarels /* NOTREACHED */ 4078032Sroot } 4088032Sroot 40939513Skarels #ifdef COMPAT_43 410*42920Skarels /* ARGSUSED */ 411*42920Skarels okillpg(p, uap, retval) 412*42920Skarels struct proc *p; 413*42920Skarels register struct args { 41437581Smckusick int pgid; 4159989Ssam int signo; 416*42920Skarels } *uap; 417*42920Skarels int *retval; 418*42920Skarels { 4198032Sroot 42039513Skarels if ((unsigned) uap->signo >= NSIG) 42139513Skarels RETURN (EINVAL); 422*42920Skarels RETURN (killpg1(p, uap->signo, uap->pgid, 0)); 4238032Sroot } 42439513Skarels #endif 4258032Sroot 426*42920Skarels /* 427*42920Skarels * Common code for kill process group/broadcast kill. 428*42920Skarels * cp is calling process. 429*42920Skarels */ 430*42920Skarels killpg1(cp, signo, pgid, all) 431*42920Skarels register struct proc *cp; 43237581Smckusick int signo, pgid, all; 4339989Ssam { 4349989Ssam register struct proc *p; 43537581Smckusick struct pgrp *pgrp; 43639513Skarels int f = 0, error = ESRCH; 43737581Smckusick 43837581Smckusick if (all) 43937581Smckusick /* 44037581Smckusick * broadcast 4417421Sroot */ 44237581Smckusick for (p = allproc; p != NULL; p = p->p_nxt) { 44337581Smckusick if (p->p_ppid == 0 || p->p_flag&SSYS || 444*42920Skarels p == u.u_procp || !CANSIGNAL(cp, p, signo)) 44537581Smckusick continue; 44637581Smckusick f++; 44737581Smckusick if (signo) 44837581Smckusick psignal(p, signo); 44937581Smckusick } 45037581Smckusick else { 45137581Smckusick if (pgid == 0) 45237581Smckusick /* 45337581Smckusick * zero pgid means send to my process group. 45437581Smckusick */ 45537581Smckusick pgrp = u.u_procp->p_pgrp; 45637581Smckusick else { 45737581Smckusick pgrp = pgfind(pgid); 45837581Smckusick if (pgrp == NULL) 45939513Skarels return (ESRCH); 46037581Smckusick } 46137581Smckusick for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) { 46239513Skarels if (p->p_ppid == 0 || p->p_flag&SSYS || 463*42920Skarels !CANSIGNAL(cp, p, signo)) 46437581Smckusick continue; 46537581Smckusick f++; 46637581Smckusick if (signo) 46737581Smckusick psignal(p, signo); 46818336Smckusick } 4697421Sroot } 47039513Skarels return (f ? 0 : error); 4717421Sroot } 4727421Sroot 473*42920Skarels /* 4747421Sroot * Send the specified signal to 47537581Smckusick * all processes with 'pgid' as 4767421Sroot * process group. 4777421Sroot */ 47837581Smckusick gsignal(pgid, sig) 4797421Sroot { 48039513Skarels struct pgrp *pgrp; 4817421Sroot 48239513Skarels if (pgid && (pgrp = pgfind(pgid))) 48342207Smarc pgsignal(pgrp, sig, 0); 4847421Sroot } 485*42920Skarels 48640807Smarc /* 48742207Smarc * Send sig to every member of a process group. 48842207Smarc * If checktty is 1, limit to members which have a controlling 48942207Smarc * terminal. 49040807Smarc */ 49142207Smarc pgsignal(pgrp, sig, checkctty) 49239513Skarels struct pgrp *pgrp; 49337581Smckusick { 49437581Smckusick register struct proc *p; 49537581Smckusick 49640807Smarc if (pgrp) 49740807Smarc for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) 49842207Smarc if (checkctty == 0 || p->p_flag&SCTTY) 49942207Smarc psignal(p, sig); 50037581Smckusick } 50137581Smckusick 5027421Sroot /* 50339513Skarels * Send a signal caused by a trap to the current process. 50439513Skarels * If it will be caught immediately, deliver it with correct code. 50539513Skarels * Otherwise, post it normally. 50639513Skarels */ 50739513Skarels trapsignal(sig, code) 50839513Skarels register int sig; 50939513Skarels unsigned code; 51039513Skarels { 511*42920Skarels register struct proc *p = u.u_procp; /* XXX */ 51239513Skarels int mask; 51339513Skarels 51439513Skarels mask = sigmask(sig); 51539513Skarels if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 && 51639513Skarels (p->p_sigmask & mask) == 0) { 51739513Skarels u.u_ru.ru_nsignals++; 51840807Smarc #ifdef KTRACE 51940807Smarc if (KTRPOINT(p, KTR_PSIG)) 52040807Smarc ktrpsig(p->p_tracep, sig, u.u_signal[sig], 52140807Smarc p->p_sigmask, code); 52240807Smarc #endif 52339513Skarels sendsig(u.u_signal[sig], sig, p->p_sigmask, code); 52439513Skarels p->p_sigmask |= u.u_sigmask[sig] | mask; 52539513Skarels } else { 52639513Skarels u.u_arg[1] = code; /* XXX for core dump/debugger */ 52739513Skarels psignal(p, sig); 52839513Skarels } 52939513Skarels } 53039513Skarels 53139513Skarels /* 53240807Smarc * Send the specified signal to the specified process. 53340807Smarc * Most signals do not do anything directly to a process; 53440807Smarc * they set a flag that asks the process to do something to itself. 53540807Smarc * Exceptions: 53640807Smarc * o When a stop signal is sent to a sleeping process that takes the default 53740807Smarc * action, the process is stopped without awakening it. 53840807Smarc * o SIGCONT restarts stopped processes (or puts them back to sleep) 53940807Smarc * regardless of the signal action (eg, blocked or ignored). 54040807Smarc * Other ignored signals are discarded immediately. 5417421Sroot */ 5427421Sroot psignal(p, sig) 5437421Sroot register struct proc *p; 5447421Sroot register int sig; 5457421Sroot { 5467421Sroot register int s; 54739513Skarels register sig_t action; 54817153Sbloom int mask; 5497421Sroot 55039513Skarels if ((unsigned)sig >= NSIG || sig == 0) 55139513Skarels panic("psignal sig"); 55217153Sbloom mask = sigmask(sig); 5537421Sroot 5547421Sroot /* 5557421Sroot * If proc is traced, always give parent a chance. 5567421Sroot */ 5577421Sroot if (p->p_flag & STRC) 5587421Sroot action = SIG_DFL; 5597421Sroot else { 5607421Sroot /* 56112882Ssam * If the signal is being ignored, 56212882Ssam * then we forget about it immediately. 56339513Skarels * (Note: we don't set SIGCONT in p_sigignore, 56439513Skarels * and if it is set to SIG_IGN, 56539513Skarels * action will be SIG_DFL here.) 5667421Sroot */ 56717153Sbloom if (p->p_sigignore & mask) 5687421Sroot return; 56917153Sbloom if (p->p_sigmask & mask) 57012882Ssam action = SIG_HOLD; 57117153Sbloom else if (p->p_sigcatch & mask) 57212882Ssam action = SIG_CATCH; 57342437Skarels else 57412882Ssam action = SIG_DFL; 5757421Sroot } 57639513Skarels switch (sig) { 5777421Sroot 57839513Skarels case SIGTERM: 57939513Skarels if ((p->p_flag&STRC) || action != SIG_DFL) 5807421Sroot break; 58139513Skarels /* FALLTHROUGH */ 5827421Sroot 58339513Skarels case SIGKILL: 58439513Skarels if (p->p_nice > NZERO) 58539513Skarels p->p_nice = NZERO; 58639513Skarels break; 5877421Sroot 58839513Skarels case SIGCONT: 58939513Skarels p->p_sig &= ~stopsigmask; 59039513Skarels break; 59139513Skarels 59239513Skarels case SIGTSTP: 59339513Skarels case SIGTTIN: 59439513Skarels case SIGTTOU: 59539513Skarels case SIGSTOP: 59639513Skarels p->p_sig &= ~sigmask(SIGCONT); 59739513Skarels break; 5987421Sroot } 59939513Skarels p->p_sig |= mask; 60039513Skarels 6017421Sroot /* 60239513Skarels * Defer further processing for signals which are held, 60339513Skarels * except that stopped processes must be continued by SIGCONT. 6047421Sroot */ 60539513Skarels if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP)) 6067421Sroot return; 60717153Sbloom s = splhigh(); 6087421Sroot switch (p->p_stat) { 6097421Sroot 6107421Sroot case SSLEEP: 6117421Sroot /* 61240807Smarc * If process is sleeping uninterruptibly 6137421Sroot * we can't interrupt the sleep... the signal will 6147421Sroot * be noticed when the process returns through 6157421Sroot * trap() or syscall(). 6167421Sroot */ 61740807Smarc if ((p->p_flag & SSINTR) == 0) 6187421Sroot goto out; 6197421Sroot /* 6207421Sroot * Process is sleeping and traced... make it runnable 6217421Sroot * so it can discover the signal in issig() and stop 6227421Sroot * for the parent. 6237421Sroot */ 6247421Sroot if (p->p_flag&STRC) 6257421Sroot goto run; 62639513Skarels /* 62739513Skarels * When a sleeping process receives a stop 62839513Skarels * signal, process immediately if possible. 62939513Skarels * All other (caught or default) signals 63039513Skarels * cause the process to run. 63139513Skarels */ 63239513Skarels if (mask & stopsigmask) { 6337421Sroot if (action != SIG_DFL) 63439513Skarels goto runfast; 6357421Sroot /* 6367421Sroot * If a child in vfork(), stopping could 6377421Sroot * cause deadlock. 6387421Sroot */ 6397421Sroot if (p->p_flag&SVFORK) 6407421Sroot goto out; 64117153Sbloom p->p_sig &= ~mask; 6427421Sroot p->p_cursig = sig; 64339513Skarels if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0) 64439513Skarels psignal(p->p_pptr, SIGCHLD); 6457421Sroot stop(p); 6467421Sroot goto out; 64739513Skarels } else 64839513Skarels goto runfast; 6497421Sroot /*NOTREACHED*/ 6507421Sroot 6517421Sroot case SSTOP: 6527421Sroot /* 6537421Sroot * If traced process is already stopped, 6547421Sroot * then no further action is necessary. 6557421Sroot */ 6567421Sroot if (p->p_flag&STRC) 6577421Sroot goto out; 6587421Sroot switch (sig) { 6597421Sroot 6607421Sroot case SIGKILL: 6617421Sroot /* 6627421Sroot * Kill signal always sets processes running. 6637421Sroot */ 66439513Skarels goto runfast; 6657421Sroot 6667421Sroot case SIGCONT: 6677421Sroot /* 66839513Skarels * If SIGCONT is default (or ignored), we continue 66939513Skarels * the process but don't leave the signal in p_sig, 67039513Skarels * as it has no further action. If SIGCONT is held, 67139513Skarels * continue the process and leave the signal in p_sig. 6727421Sroot * If the process catches SIGCONT, let it handle 6737421Sroot * the signal itself. If it isn't waiting on 6747421Sroot * an event, then it goes back to run state. 6757421Sroot * Otherwise, process goes back to sleep state. 6767421Sroot */ 67739513Skarels p->p_cursig = 0; /* ??? XXX */ 67839513Skarels if (action == SIG_DFL) 67939513Skarels p->p_sig &= ~mask; 68039513Skarels if (action == SIG_CATCH) 68139513Skarels goto runfast; 68239513Skarels if (p->p_wchan == 0) 6837421Sroot goto run; 6847421Sroot p->p_stat = SSLEEP; 6857421Sroot goto out; 6867421Sroot 6877421Sroot case SIGSTOP: 6887421Sroot case SIGTSTP: 6897421Sroot case SIGTTIN: 6907421Sroot case SIGTTOU: 6917421Sroot /* 6927421Sroot * Already stopped, don't need to stop again. 6937421Sroot * (If we did the shell could get confused.) 6947421Sroot */ 69517153Sbloom p->p_sig &= ~mask; /* take it away */ 6967421Sroot goto out; 6977421Sroot 6987421Sroot default: 6997421Sroot /* 7007421Sroot * If process is sleeping interruptibly, then 70140807Smarc * simulate a wakeup so that when it is continued, 70240807Smarc * it will be made runnable and can look at the signal. 70340807Smarc * But don't setrun the process, leave it stopped. 7047421Sroot */ 70540807Smarc if (p->p_wchan && p->p_flag & SSINTR) 7067421Sroot unsleep(p); 7077421Sroot goto out; 7087421Sroot } 7097421Sroot /*NOTREACHED*/ 7107421Sroot 7117421Sroot default: 7127421Sroot /* 7137421Sroot * SRUN, SIDL, SZOMB do nothing with the signal, 7147421Sroot * other than kicking ourselves if we are running. 7157421Sroot * It will either never be noticed, or noticed very soon. 7167421Sroot */ 7177421Sroot if (p == u.u_procp && !noproc) 7187421Sroot aston(); 7197421Sroot goto out; 7207421Sroot } 7217421Sroot /*NOTREACHED*/ 72239513Skarels 72339513Skarels runfast: 7247421Sroot /* 7257421Sroot * Raise priority to at least PUSER. 7267421Sroot */ 7277421Sroot if (p->p_pri > PUSER) 72817399Skarels p->p_pri = PUSER; 72939513Skarels run: 7307421Sroot setrun(p); 7317421Sroot out: 7327421Sroot splx(s); 7337421Sroot } 7347421Sroot 7357421Sroot /* 73640807Smarc * If the current process has a signal to process (should be caught 73740807Smarc * or cause termination, should interrupt current syscall), 73840807Smarc * return the signal number. Stop signals with default action 73940807Smarc * are processed immediately, then cleared; they aren't returned. 7407421Sroot * This is asked at least once each time a process enters the 7417421Sroot * system (though this can usually be done without actually 7427421Sroot * calling issig by checking the pending signal masks.) 7437421Sroot */ 7447421Sroot issig() 7457421Sroot { 7467421Sroot register struct proc *p; 74739513Skarels register int sig, mask; 7487421Sroot 7497421Sroot p = u.u_procp; 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 */ 77140807Smarc p->p_cursig = sig; 77218331Skarels psignal(p->p_pptr, SIGCHLD); 7737421Sroot do { 7747421Sroot stop(p); 7757421Sroot swtch(); 7767421Sroot } while (!procxmt() && 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, 7887421Sroot * then it will leave it in p->p_cursig; 7897421Sroot * otherwise we just look for signals again. 7907421Sroot */ 79140807Smarc p->p_sig &= ~mask; /* clear the old signal */ 7927421Sroot sig = p->p_cursig; 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 */ 83240807Smarc p->p_cursig = 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 */ 89140807Smarc psig(sig) 89240807Smarc register int sig; 8937421Sroot { 89412882Ssam register struct proc *p = u.u_procp; 89539513Skarels int mask, returnmask; 89639513Skarels register sig_t action; 8977421Sroot 89839513Skarels do { 89940807Smarc #ifdef DIAGNOSTIC 90039513Skarels if (sig == 0) 90139513Skarels panic("psig"); 90240807Smarc #endif 90340807Smarc mask = sigmask(sig); 90440807Smarc p->p_sig &= ~mask; 90539513Skarels action = u.u_signal[sig]; 90640807Smarc #ifdef KTRACE 90740807Smarc if (KTRPOINT(p, KTR_PSIG)) 90840807Smarc ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ? 90940807Smarc u.u_oldmask : p->p_sigmask, 0); 91040807Smarc #endif 91139513Skarels if (action != SIG_DFL) { 91239513Skarels #ifdef DIAGNOSTIC 91339513Skarels if (action == SIG_IGN || (p->p_sigmask & mask)) 91439513Skarels panic("psig action"); 91539513Skarels #endif 91639513Skarels u.u_error = 0; 91739513Skarels /* 91839513Skarels * Set the new mask value and also defer further 91939513Skarels * occurences of this signal. 92039513Skarels * 92139513Skarels * Special case: user has done a sigpause. Here the 92239513Skarels * current mask is not of interest, but rather the 92339513Skarels * mask from before the sigpause is what we want 92439513Skarels * restored after the signal processing is completed. 92539513Skarels */ 92639513Skarels (void) splhigh(); 92739513Skarels if (p->p_flag & SOMASK) { 92839513Skarels returnmask = u.u_oldmask; 92939513Skarels p->p_flag &= ~SOMASK; 93039513Skarels } else 93139513Skarels returnmask = p->p_sigmask; 93239513Skarels p->p_sigmask |= u.u_sigmask[sig] | mask; 93339513Skarels (void) spl0(); 93439513Skarels u.u_ru.ru_nsignals++; 93539513Skarels sendsig(action, sig, returnmask, 0); 93639513Skarels continue; 9377421Sroot } 93839513Skarels u.u_acflag |= AXSIG; 93939513Skarels switch (sig) { 9407421Sroot 94139513Skarels case SIGILL: 94239513Skarels case SIGIOT: 94339513Skarels case SIGBUS: 94439513Skarels case SIGQUIT: 94539513Skarels case SIGTRAP: 94639513Skarels case SIGEMT: 94739513Skarels case SIGFPE: 94839513Skarels case SIGSEGV: 94939513Skarels case SIGSYS: 95039513Skarels u.u_arg[0] = sig; 95139513Skarels if (core() == 0) 95239513Skarels sig |= WCOREFLAG; 95339513Skarels } 95439513Skarels exit(W_EXITCODE(0, sig)); 95539513Skarels /* NOTREACHED */ 95640807Smarc } while (sig = CURSIG(p)); 9577421Sroot } 9587421Sroot 9597421Sroot /* 96039513Skarels * Create a core image on the file "core". 9617421Sroot * It writes UPAGES block of the 9627421Sroot * user.h area followed by the entire 9637421Sroot * data+stack segments. 9647421Sroot */ 9657421Sroot core() 9667421Sroot { 96737728Smckusick register struct vnode *vp; 96839513Skarels register struct proc *p = u.u_procp; 96916692Smckusick register struct nameidata *ndp = &u.u_nd; 97037580Smckusick struct vattr vattr; 97137580Smckusick int error; 9727421Sroot 97339513Skarels if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid) 97437580Smckusick return (EFAULT); 97537580Smckusick if (ctob(UPAGES + u.u_dsize + u.u_ssize) >= 9768032Sroot u.u_rlimit[RLIMIT_CORE].rlim_cur) 97737580Smckusick return (EFAULT); 97839513Skarels if (p->p_textp) { 97939513Skarels VOP_LOCK(p->p_textp->x_vptr); 98039513Skarels error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred); 98139513Skarels VOP_UNLOCK(p->p_textp->x_vptr); 98237580Smckusick if (error) 98337580Smckusick return (EFAULT); 98437580Smckusick } 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 } 99742004Smckusick #ifdef MAPMEM 99842004Smckusick mmcore(); 99930290Ssam #endif 100041362Smckusick VATTR_NULL(&vattr); 100137580Smckusick vattr.va_size = 0; 100237728Smckusick VOP_SETATTR(vp, &vattr, u.u_cred); 10037818Sroot u.u_acflag |= ACORE; 100442004Smckusick #ifdef HPUXCOMPAT 100542004Smckusick /* 100642004Smckusick * BLETCH! If we loaded from an HPUX format binary file 100742004Smckusick * we have to dump an HPUX style user struct so that the 100842004Smckusick * HPUX debuggers can grok it. 100942004Smckusick */ 101042004Smckusick if (u.u_pcb.pcb_flags & PCB_HPUXBIN) 101142004Smckusick error = hpuxdumpu(vp, ndp->ni_cred); 101242004Smckusick else 101342004Smckusick #endif 101437580Smckusick error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0, 101538394Smckusick UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 101637580Smckusick if (error == 0) 101737580Smckusick error = vn_rdwr(UIO_WRITE, vp, 101839513Skarels (caddr_t)ctob(dptov(p, 0)), 101938394Smckusick (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE, 102038394Smckusick IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 102137580Smckusick if (error == 0) 102237580Smckusick error = vn_rdwr(UIO_WRITE, vp, 102339513Skarels (caddr_t)ctob(sptov(p, u.u_ssize - 1)), 102426354Skarels (int)ctob(u.u_ssize), 102538394Smckusick (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE, 102638394Smckusick IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 102738394Smckusick vput(vp); 102837580Smckusick return (error); 10297421Sroot } 103039513Skarels 103139513Skarels /* 103239513Skarels * Nonexistent system call-- signal process (may want to handle it). 103339513Skarels * Flag error in case process won't see signal immediately (blocked or ignored). 103439513Skarels */ 103539513Skarels nosys() 103639513Skarels { 103739513Skarels 103839513Skarels psignal(u.u_procp, SIGSYS); 103939513Skarels u.u_error = EINVAL; 104039513Skarels } 1041