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*43364Smckusick * @(#)kern_sig.c 7.20 (Berkeley) 06/21/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 /* 4942920Skarels * Can process p send the signal signo to process q? 5017013Smckusick */ 5142920Skarels #define CANSIGNAL(p, q, signo) \ 5242920Skarels ((p)->p_uid == 0 || \ 5342920Skarels (p)->p_ruid == (q)->p_ruid || (p)->p_uid == (q)->p_ruid || \ 5442920Skarels (p)->p_ruid == (q)->p_uid || (p)->p_uid == (q)->p_uid || \ 5542920Skarels ((signo) == SIGCONT && (q)->p_session == (p)->p_session)) 5639513Skarels 5742920Skarels /* ARGSUSED */ 5842920Skarels sigaction(p, uap, retval) 5942920Skarels struct proc *p; 6042920Skarels register struct args { 6112882Ssam int signo; 6239513Skarels struct sigaction *nsa; 6339513Skarels struct sigaction *osa; 6442920Skarels } *uap; 6542920Skarels int *retval; 6642920Skarels { 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; 8542920Skarels 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); 9542920Skarels setsigvec(p, sig, sa); 9612951Ssam } 9739513Skarels RETURN (0); 987421Sroot } 997421Sroot 10042920Skarels setsigvec(p, sig, sa) 10142920Skarels 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 */ 20042920Skarels sigprocmask(p, uap, retval) 20142920Skarels register struct proc *p; 20242920Skarels struct args { 20339513Skarels int how; 20439513Skarels sigset_t mask; 20542920Skarels } *uap; 20642920Skarels int *retval; 20742920Skarels { 20839513Skarels int error = 0; 20939513Skarels 21042920Skarels *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 23442920Skarels /* ARGSUSED */ 23542920Skarels sigpending(p, uap, retval) 23642920Skarels struct proc *p; 23742920Skarels void *uap; 23842920Skarels int *retval; 23939513Skarels { 24039513Skarels 24142920Skarels *retval = p->p_sig; 24239513Skarels RETURN (0); 24339513Skarels } 24439513Skarels 24539513Skarels #ifdef COMPAT_43 24639513Skarels /* 24739513Skarels * Generalized interface signal handler, 4.3-compatible. 24839513Skarels */ 24942920Skarels /* ARGSUSED */ 25042920Skarels osigvec(p, uap, retval) 25142920Skarels struct proc *p; 25242920Skarels register struct args { 25339513Skarels int signo; 25439513Skarels struct sigvec *nsv; 25539513Skarels struct sigvec *osv; 25642920Skarels } *uap; 25742920Skarels int *retval; 25842920Skarels { 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; 27742920Skarels 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 */ 28842920Skarels setsigvec(p, sig, (struct sigaction *)sv); 28939513Skarels } 29039513Skarels RETURN (0); 29139513Skarels } 29239513Skarels 29342920Skarels osigblock(p, uap, retval) 29442920Skarels register struct proc *p; 29542920Skarels struct args { 29642920Skarels int mask; 29742920Skarels } *uap; 29842920Skarels int *retval; 29939513Skarels { 3007499Sroot 30117153Sbloom (void) splhigh(); 30242920Skarels *retval = p->p_sigmask; 30339513Skarels p->p_sigmask |= uap->mask &~ sigcantmask; 30412882Ssam (void) spl0(); 30539513Skarels RETURN (0); 3067499Sroot } 3077499Sroot 30842920Skarels osigsetmask(p, uap, retval) 30942920Skarels struct proc *p; 31042920Skarels struct args { 31142920Skarels int mask; 31242920Skarels } *uap; 31342920Skarels int *retval; 3147499Sroot { 3157499Sroot 31617153Sbloom (void) splhigh(); 31742920Skarels *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 */ 32942920Skarels /* ARGSUSED */ 33042920Skarels sigsuspend(p, uap, retval) 33142920Skarels register struct proc *p; 33242920Skarels struct args { 33342920Skarels sigset_t mask; 33442920Skarels } *uap; 33542920Skarels 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 35342920Skarels /* ARGSUSED */ 35442920Skarels sigstack(p, uap, retval) 35542920Skarels struct proc *p; 35642920Skarels register struct args { 35712951Ssam struct sigstack *nss; 35812951Ssam struct sigstack *oss; 35942920Skarels } *uap; 36042920Skarels int *retval; 36142920Skarels { 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 37442920Skarels /* ARGSUSED */ 37542920Skarels kill(cp, uap, retval) 37642920Skarels register struct proc *cp; 37742920Skarels register struct args { 37812882Ssam int pid; 37912882Ssam int signo; 38042920Skarels } *uap; 38142920Skarels int *retval; 38242920Skarels { 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); 39242920Skarels 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 */ 40042920Skarels RETURN (killpg1(cp, uap->signo, 0, 1)); 40118336Smckusick case 0: /* signal own process group */ 40242920Skarels RETURN (killpg1(cp, uap->signo, 0, 0)); 40318336Smckusick default: /* negative explicit process group */ 40442920Skarels RETURN (killpg1(cp, uap->signo, -uap->pid, 0)); 40518336Smckusick } 40639513Skarels /* NOTREACHED */ 4078032Sroot } 4088032Sroot 40939513Skarels #ifdef COMPAT_43 41042920Skarels /* ARGSUSED */ 41142920Skarels okillpg(p, uap, retval) 41242920Skarels struct proc *p; 41342920Skarels register struct args { 41437581Smckusick int pgid; 4159989Ssam int signo; 41642920Skarels } *uap; 41742920Skarels int *retval; 41842920Skarels { 4198032Sroot 42039513Skarels if ((unsigned) uap->signo >= NSIG) 42139513Skarels RETURN (EINVAL); 42242920Skarels RETURN (killpg1(p, uap->signo, uap->pgid, 0)); 4238032Sroot } 42439513Skarels #endif 4258032Sroot 42642920Skarels /* 42742920Skarels * Common code for kill process group/broadcast kill. 42842920Skarels * cp is calling process. 42942920Skarels */ 43042920Skarels killpg1(cp, signo, pgid, all) 43142920Skarels register struct proc *cp; 43237581Smckusick int signo, pgid, all; 4339989Ssam { 4349989Ssam register struct proc *p; 43537581Smckusick struct pgrp *pgrp; 436*43364Smckusick int f = 0; 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 || 44442920Skarels 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 || 46342920Skarels !CANSIGNAL(cp, p, signo)) 46437581Smckusick continue; 46537581Smckusick f++; 46637581Smckusick if (signo) 46737581Smckusick psignal(p, signo); 46818336Smckusick } 4697421Sroot } 470*43364Smckusick return (f ? 0 : ESRCH); 4717421Sroot } 4727421Sroot 47342920Skarels /* 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 } 48542920Skarels 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 { 51142920Skarels 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 { 74642926Smckusick register struct proc *p = u.u_procp; /* XXX */ 74739513Skarels register int sig, mask; 7487421Sroot 7497421Sroot for (;;) { 75039513Skarels mask = p->p_sig &~ p->p_sigmask; 7517421Sroot if (p->p_flag&SVFORK) 75239513Skarels mask &= ~stopsigmask; 75340807Smarc if (mask == 0) /* no signal to send */ 75440807Smarc return (0); 75539513Skarels sig = ffs((long)mask); 75617153Sbloom mask = sigmask(sig); 75740807Smarc /* 75840807Smarc * We should see pending but ignored signals 75940807Smarc * only if STRC was on when they were posted. 76040807Smarc */ 76140807Smarc if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) { 76240807Smarc p->p_sig &= ~mask; 76340807Smarc continue; 76440807Smarc } 76512882Ssam if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) { 7667421Sroot /* 7677421Sroot * If traced, always stop, and stay 7687421Sroot * stopped until released by the parent. 7697421Sroot */ 77040807Smarc p->p_cursig = sig; 77118331Skarels psignal(p->p_pptr, SIGCHLD); 7727421Sroot do { 7737421Sroot stop(p); 7747421Sroot swtch(); 77542926Smckusick } while (!procxmt(p) && p->p_flag&STRC); 7767421Sroot 7777421Sroot /* 77814782Ssam * If the traced bit got turned off, 77940807Smarc * go back up to the top to rescan signals. 78014782Ssam * This ensures that p_sig* and u_signal are consistent. 7817421Sroot */ 78240807Smarc if ((p->p_flag&STRC) == 0) 7837421Sroot continue; 7847421Sroot 7857421Sroot /* 7867421Sroot * If parent wants us to take the signal, 7877421Sroot * then it will leave it in p->p_cursig; 7887421Sroot * otherwise we just look for signals again. 7897421Sroot */ 79040807Smarc p->p_sig &= ~mask; /* clear the old signal */ 7917421Sroot sig = p->p_cursig; 7927421Sroot if (sig == 0) 7937421Sroot continue; 79414782Ssam 79514782Ssam /* 79640807Smarc * Put the new signal into p_sig. 79740807Smarc * If signal is being masked, 79840807Smarc * look for other signals. 79914782Ssam */ 80017153Sbloom mask = sigmask(sig); 80140807Smarc p->p_sig |= mask; 80240807Smarc if (p->p_sigmask & mask) 80314782Ssam continue; 8047421Sroot } 80540807Smarc 80640807Smarc /* 80740807Smarc * Decide whether the signal should be returned. 80840807Smarc * Return the signal's number, or fall through 80940807Smarc * to clear it from the pending mask. 81040807Smarc */ 81124901Skarels switch ((int)u.u_signal[sig]) { 8127421Sroot 8137421Sroot case SIG_DFL: 8147421Sroot /* 8157421Sroot * Don't take default actions on system processes. 8167421Sroot */ 8177421Sroot if (p->p_ppid == 0) 81840807Smarc break; /* == ignore */ 81940807Smarc /* 82040807Smarc * If there is a pending stop signal to process 82140807Smarc * with default action, stop here, 82242437Skarels * then clear the signal. However, 82342437Skarels * if process is member of an orphaned 82442437Skarels * process group, ignore tty stop signals. 82540807Smarc */ 82639513Skarels if (mask & stopsigmask) { 82742437Skarels if (p->p_flag&STRC || 82842437Skarels (p->p_pgrp->pg_jobc == 0 && 82942437Skarels mask & ttystopsigmask)) 83040807Smarc break; /* == ignore */ 83140807Smarc p->p_cursig = sig; 8327421Sroot stop(p); 83339513Skarels if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0) 83439513Skarels psignal(p->p_pptr, SIGCHLD); 8357421Sroot swtch(); 83640807Smarc break; 83739513Skarels } else if (mask & defaultignmask) { 8387421Sroot /* 83939513Skarels * Except for SIGCONT, shouldn't get here. 84039513Skarels * Default action is to ignore; drop it. 8417421Sroot */ 84240807Smarc break; /* == ignore */ 84339513Skarels } else 84440807Smarc return (sig); 8457421Sroot /*NOTREACHED*/ 8467421Sroot 8477421Sroot case SIG_IGN: 8487421Sroot /* 84939513Skarels * Masking above should prevent us ever trying 85039513Skarels * to take action on an ignored signal other 85139513Skarels * than SIGCONT, unless process is traced. 8527421Sroot */ 85339513Skarels if (sig != SIGCONT && (p->p_flag&STRC) == 0) 8547421Sroot printf("issig\n"); 85540807Smarc break; /* == ignore */ 8567421Sroot 8577421Sroot default: 8587421Sroot /* 8597421Sroot * This signal has an action, let 8607421Sroot * psig process it. 8617421Sroot */ 86240807Smarc return (sig); 8637421Sroot } 86440807Smarc p->p_sig &= ~mask; /* take the signal! */ 8657421Sroot } 86640807Smarc /* NOTREACHED */ 8677421Sroot } 8687421Sroot 8697421Sroot /* 8707421Sroot * Put the argument process into the stopped 87118331Skarels * state and notify the parent via wakeup. 87218331Skarels * Signals are handled elsewhere. 87340807Smarc * The process must not be on the run queue. 8747421Sroot */ 8757421Sroot stop(p) 8767421Sroot register struct proc *p; 8777421Sroot { 8787421Sroot 8797421Sroot p->p_stat = SSTOP; 8807421Sroot p->p_flag &= ~SWTED; 8817421Sroot wakeup((caddr_t)p->p_pptr); 8827421Sroot } 8837421Sroot 8847421Sroot /* 88540807Smarc * Perform the action specified by the current signal. 8867421Sroot * The usual sequence is: 88740807Smarc * if (sig = CURSIG(p)) 88840807Smarc * psig(sig); 8897421Sroot */ 89040807Smarc psig(sig) 89140807Smarc register int sig; 8927421Sroot { 89312882Ssam register struct proc *p = u.u_procp; 89439513Skarels int mask, returnmask; 89539513Skarels register sig_t action; 8967421Sroot 89739513Skarels do { 89840807Smarc #ifdef DIAGNOSTIC 89939513Skarels if (sig == 0) 90039513Skarels panic("psig"); 90140807Smarc #endif 90240807Smarc mask = sigmask(sig); 90340807Smarc p->p_sig &= ~mask; 90439513Skarels action = u.u_signal[sig]; 90540807Smarc #ifdef KTRACE 90640807Smarc if (KTRPOINT(p, KTR_PSIG)) 90740807Smarc ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ? 90840807Smarc u.u_oldmask : p->p_sigmask, 0); 90940807Smarc #endif 91039513Skarels if (action != SIG_DFL) { 91139513Skarels #ifdef DIAGNOSTIC 91239513Skarels if (action == SIG_IGN || (p->p_sigmask & mask)) 91339513Skarels panic("psig action"); 91439513Skarels #endif 91539513Skarels u.u_error = 0; 91639513Skarels /* 91739513Skarels * Set the new mask value and also defer further 91839513Skarels * occurences of this signal. 91939513Skarels * 92039513Skarels * Special case: user has done a sigpause. Here the 92139513Skarels * current mask is not of interest, but rather the 92239513Skarels * mask from before the sigpause is what we want 92339513Skarels * restored after the signal processing is completed. 92439513Skarels */ 92539513Skarels (void) splhigh(); 92639513Skarels if (p->p_flag & SOMASK) { 92739513Skarels returnmask = u.u_oldmask; 92839513Skarels p->p_flag &= ~SOMASK; 92939513Skarels } else 93039513Skarels returnmask = p->p_sigmask; 93139513Skarels p->p_sigmask |= u.u_sigmask[sig] | mask; 93239513Skarels (void) spl0(); 93339513Skarels u.u_ru.ru_nsignals++; 93439513Skarels sendsig(action, sig, returnmask, 0); 93539513Skarels continue; 9367421Sroot } 93739513Skarels u.u_acflag |= AXSIG; 93839513Skarels switch (sig) { 9397421Sroot 94039513Skarels case SIGILL: 94139513Skarels case SIGIOT: 94239513Skarels case SIGBUS: 94339513Skarels case SIGQUIT: 94439513Skarels case SIGTRAP: 94539513Skarels case SIGEMT: 94639513Skarels case SIGFPE: 94739513Skarels case SIGSEGV: 94839513Skarels case SIGSYS: 94939513Skarels u.u_arg[0] = sig; 95039513Skarels if (core() == 0) 95139513Skarels sig |= WCOREFLAG; 95239513Skarels } 95342926Smckusick exit(p, W_EXITCODE(0, sig)); 95439513Skarels /* NOTREACHED */ 95540807Smarc } while (sig = CURSIG(p)); 9567421Sroot } 9577421Sroot 9587421Sroot /* 95939513Skarels * Create a core image on the file "core". 9607421Sroot * It writes UPAGES block of the 9617421Sroot * user.h area followed by the entire 9627421Sroot * data+stack segments. 9637421Sroot */ 9647421Sroot core() 9657421Sroot { 96637728Smckusick register struct vnode *vp; 96739513Skarels register struct proc *p = u.u_procp; 96816692Smckusick register struct nameidata *ndp = &u.u_nd; 96937580Smckusick struct vattr vattr; 97037580Smckusick int error; 9717421Sroot 97239513Skarels if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid) 97337580Smckusick return (EFAULT); 97437580Smckusick if (ctob(UPAGES + u.u_dsize + u.u_ssize) >= 9758032Sroot u.u_rlimit[RLIMIT_CORE].rlim_cur) 97637580Smckusick return (EFAULT); 97739513Skarels if (p->p_textp) { 97839513Skarels VOP_LOCK(p->p_textp->x_vptr); 97939513Skarels error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred); 98039513Skarels VOP_UNLOCK(p->p_textp->x_vptr); 98137580Smckusick if (error) 98237580Smckusick return (EFAULT); 98337580Smckusick } 98416692Smckusick ndp->ni_segflg = UIO_SYSSPACE; 98516692Smckusick ndp->ni_dirp = "core"; 98637580Smckusick if (error = vn_open(ndp, FCREAT|FWRITE, 0644)) 98737580Smckusick return (error); 98837580Smckusick vp = ndp->ni_vp; 98938394Smckusick VOP_LOCK(vp); 99037580Smckusick if (vp->v_type != VREG || 99137728Smckusick VOP_GETATTR(vp, &vattr, u.u_cred) || 99237580Smckusick vattr.va_nlink != 1) { 99338394Smckusick vput(vp); 99438394Smckusick return (EFAULT); 9957818Sroot } 99642004Smckusick #ifdef MAPMEM 99742926Smckusick if (error = mmcore(p)) { 99842926Smckusick vput(vp); 99942926Smckusick return (error); 100042926Smckusick } 100130290Ssam #endif 100241362Smckusick VATTR_NULL(&vattr); 100337580Smckusick vattr.va_size = 0; 100437728Smckusick VOP_SETATTR(vp, &vattr, u.u_cred); 10057818Sroot u.u_acflag |= ACORE; 100642004Smckusick #ifdef HPUXCOMPAT 100742004Smckusick /* 100842004Smckusick * BLETCH! If we loaded from an HPUX format binary file 100942004Smckusick * we have to dump an HPUX style user struct so that the 101042004Smckusick * HPUX debuggers can grok it. 101142004Smckusick */ 101242004Smckusick if (u.u_pcb.pcb_flags & PCB_HPUXBIN) 101342004Smckusick error = hpuxdumpu(vp, ndp->ni_cred); 101442004Smckusick else 101542004Smckusick #endif 101637580Smckusick error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0, 101738394Smckusick UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 101837580Smckusick if (error == 0) 101937580Smckusick error = vn_rdwr(UIO_WRITE, vp, 102039513Skarels (caddr_t)ctob(dptov(p, 0)), 102138394Smckusick (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE, 102238394Smckusick IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 102337580Smckusick if (error == 0) 102437580Smckusick error = vn_rdwr(UIO_WRITE, vp, 102539513Skarels (caddr_t)ctob(sptov(p, u.u_ssize - 1)), 102626354Skarels (int)ctob(u.u_ssize), 102738394Smckusick (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE, 102838394Smckusick IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 102938394Smckusick vput(vp); 103037580Smckusick return (error); 10317421Sroot } 103239513Skarels 103339513Skarels /* 103439513Skarels * Nonexistent system call-- signal process (may want to handle it). 103539513Skarels * Flag error in case process won't see signal immediately (blocked or ignored). 103639513Skarels */ 1037*43364Smckusick /* ARGSUSED */ 1038*43364Smckusick nosys(p, args, retval) 1039*43364Smckusick struct proc *p; 1040*43364Smckusick void *args; 1041*43364Smckusick int *retval; 104239513Skarels { 104339513Skarels 1044*43364Smckusick psignal(p, SIGSYS); 1045*43364Smckusick RETURN (EINVAL); 104639513Skarels } 1047