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*42207Smarc * @(#)kern_sig.c 7.16 (Berkeley) 05/17/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 4326276Skarels #define stopsigmask (sigmask(SIGSTOP)|sigmask(SIGTSTP)| \ 4426276Skarels sigmask(SIGTTIN)|sigmask(SIGTTOU)) 4539513Skarels #define defaultignmask (sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \ 4639513Skarels sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO)) 4712951Ssam 4817013Smckusick /* 4939513Skarels * Can the current process (u.u_procp) send the specified signal 5039513Skarels * to the specified process? 5117013Smckusick */ 5239513Skarels #define CANSIGNAL(p, signo) \ 5339513Skarels (u.u_uid == 0 || \ 5439513Skarels u.u_uid == (p)->p_uid || u.u_uid == (p)->p_ruid || \ 5539513Skarels u.u_procp->p_ruid == (p)->p_uid || \ 5639513Skarels u.u_procp->p_ruid == (p)->p_ruid || \ 5739513Skarels ((signo) == SIGCONT && (p)->p_session == u.u_procp->p_session)) 5839513Skarels 5939513Skarels sigaction() 607421Sroot { 6112951Ssam register struct a { 6212882Ssam int signo; 6339513Skarels struct sigaction *nsa; 6439513Skarels struct sigaction *osa; 6512882Ssam } *uap = (struct a *)u.u_ap; 6639513Skarels struct sigaction vec; 6739513Skarels register struct sigaction *sa; 6812882Ssam register int sig; 6939513Skarels int bit, error; 707421Sroot 7112882Ssam sig = uap->signo; 7239513Skarels if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) 7339513Skarels RETURN (EINVAL); 7439513Skarels sa = &vec; 7539513Skarels if (uap->osa) { 7639513Skarels sa->sa_handler = u.u_signal[sig]; 7739513Skarels sa->sa_mask = u.u_sigmask[sig]; 7818308Smckusick bit = sigmask(sig); 7939513Skarels sa->sa_flags = 0; 8018308Smckusick if ((u.u_sigonstack & bit) != 0) 8139513Skarels sa->sa_flags |= SA_ONSTACK; 8239513Skarels if ((u.u_sigintr & bit) == 0) 8339513Skarels sa->sa_flags |= SA_RESTART; 8439513Skarels if (u.u_procp->p_flag & SNOCLDSTOP) 8539513Skarels sa->sa_flags |= SA_NOCLDSTOP; 8639513Skarels if (error = copyout((caddr_t)sa, (caddr_t)uap->osa, 8739513Skarels sizeof (vec))) 8839513Skarels RETURN (error); 8912951Ssam } 9039513Skarels if (uap->nsa) { 9139513Skarels if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa, 9239513Skarels sizeof (vec))) 9339513Skarels RETURN (error); 9439513Skarels setsigvec(sig, sa); 9512951Ssam } 9639513Skarels RETURN (0); 977421Sroot } 987421Sroot 9939513Skarels setsigvec(sig, sa) 10012951Ssam int sig; 10139513Skarels register struct sigaction *sa; 10212882Ssam { 10312882Ssam register struct proc *p; 10412951Ssam register int bit; 10512882Ssam 10617153Sbloom bit = sigmask(sig); 10712882Ssam p = u.u_procp; 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 */ 20039513Skarels sigprocmask() 20139513Skarels { 20212882Ssam struct a { 20339513Skarels int how; 20439513Skarels sigset_t mask; 20539513Skarels } *uap = (struct a *)u.u_ap; 20639513Skarels register struct proc *p = u.u_procp; 20739513Skarels int error = 0; 20839513Skarels 20939513Skarels u.u_r.r_val1 = p->p_sigmask; 21039513Skarels (void) splhigh(); 21139513Skarels 21239513Skarels switch (uap->how) { 21339513Skarels case SIG_BLOCK: 21439513Skarels p->p_sigmask |= uap->mask &~ sigcantmask; 21539513Skarels break; 21639513Skarels 21739513Skarels case SIG_UNBLOCK: 21839513Skarels p->p_sigmask &= ~uap->mask; 21939513Skarels break; 22039513Skarels 22139513Skarels case SIG_SETMASK: 22239513Skarels p->p_sigmask = uap->mask &~ sigcantmask; 22339513Skarels break; 22439513Skarels 22539513Skarels default: 22639513Skarels error = EINVAL; 22739513Skarels break; 22839513Skarels } 22939513Skarels (void) spl0(); 23039513Skarels RETURN (error); 23139513Skarels } 23239513Skarels 23339513Skarels sigpending() 23439513Skarels { 23539513Skarels 23639513Skarels u.u_r.r_val1 = u.u_procp->p_sig; 23739513Skarels RETURN (0); 23839513Skarels } 23939513Skarels 24039513Skarels #ifdef COMPAT_43 24139513Skarels /* 24239513Skarels * Generalized interface signal handler, 4.3-compatible. 24339513Skarels */ 24439513Skarels osigvec() 24539513Skarels { 24639513Skarels register struct a { 24739513Skarels int signo; 24839513Skarels struct sigvec *nsv; 24939513Skarels struct sigvec *osv; 25039513Skarels } *uap = (struct a *)u.u_ap; 25139513Skarels struct sigvec vec; 25239513Skarels register struct sigvec *sv; 25339513Skarels register int sig; 25439513Skarels int bit, error; 25539513Skarels 25639513Skarels sig = uap->signo; 25739513Skarels if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) 25839513Skarels RETURN (EINVAL); 25939513Skarels sv = &vec; 26039513Skarels if (uap->osv) { 26139513Skarels *(sig_t *)&sv->sv_handler = u.u_signal[sig]; 26239513Skarels sv->sv_mask = u.u_sigmask[sig]; 26339513Skarels bit = sigmask(sig); 26439513Skarels sv->sv_flags = 0; 26539513Skarels if ((u.u_sigonstack & bit) != 0) 26639513Skarels sv->sv_flags |= SV_ONSTACK; 26739513Skarels if ((u.u_sigintr & bit) != 0) 26839513Skarels sv->sv_flags |= SV_INTERRUPT; 26939513Skarels if (u.u_procp->p_flag & SNOCLDSTOP) 27039513Skarels sv->sv_flags |= SA_NOCLDSTOP; 27139513Skarels if (error = copyout((caddr_t)sv, (caddr_t)uap->osv, 27239513Skarels sizeof (vec))) 27339513Skarels RETURN (error); 27439513Skarels } 27539513Skarels if (uap->nsv) { 27639513Skarels if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv, 27739513Skarels sizeof (vec))) 27839513Skarels RETURN (error); 27939513Skarels sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 28042004Smckusick setsigvec(sig, (struct sigaction *)sv); 28139513Skarels } 28239513Skarels RETURN (0); 28339513Skarels } 28439513Skarels 28539513Skarels osigblock() 28639513Skarels { 28739513Skarels struct a { 28817153Sbloom int mask; 28912882Ssam } *uap = (struct a *)u.u_ap; 29012951Ssam register struct proc *p = u.u_procp; 2917499Sroot 29217153Sbloom (void) splhigh(); 29312882Ssam u.u_r.r_val1 = p->p_sigmask; 29439513Skarels p->p_sigmask |= uap->mask &~ sigcantmask; 29512882Ssam (void) spl0(); 29639513Skarels RETURN (0); 2977499Sroot } 2987499Sroot 29939513Skarels osigsetmask() 3007499Sroot { 30112882Ssam struct a { 30217153Sbloom int mask; 30312882Ssam } *uap = (struct a *)u.u_ap; 30412882Ssam register struct proc *p = u.u_procp; 3057499Sroot 30617153Sbloom (void) splhigh(); 30712882Ssam u.u_r.r_val1 = p->p_sigmask; 30839513Skarels p->p_sigmask = uap->mask &~ sigcantmask; 30912882Ssam (void) spl0(); 31039513Skarels RETURN (0); 3117499Sroot } 31239513Skarels #endif 3137499Sroot 31439513Skarels /* 31539513Skarels * Suspend process until signal, providing mask to be set 31639513Skarels * in the meantime. Note nonstandard calling convention: 31739513Skarels * libc stub passes mask, not pointer, to save a copyin. 31839513Skarels */ 31939513Skarels sigsuspend() 3207499Sroot { 32112882Ssam struct a { 32239513Skarels sigset_t mask; 32312882Ssam } *uap = (struct a *)u.u_ap; 32412882Ssam register struct proc *p = u.u_procp; 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... */ 33840807Smarc RETURN (EINTR); 3397499Sroot } 3407499Sroot 3417499Sroot sigstack() 3427499Sroot { 34312951Ssam register struct a { 34412951Ssam struct sigstack *nss; 34512951Ssam struct sigstack *oss; 34612882Ssam } *uap = (struct a *)u.u_ap; 34712951Ssam struct sigstack ss; 34839513Skarels int error = 0; 3497499Sroot 35039513Skarels if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack, 35139513Skarels (caddr_t)uap->oss, sizeof (struct sigstack)))) 35239513Skarels RETURN (error); 35339513Skarels if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, 35439513Skarels sizeof (ss))) == 0) 35539513Skarels u.u_sigstack = ss; 35639513Skarels RETURN (error); 3577499Sroot } 3587499Sroot 3598032Sroot kill() 3608032Sroot { 36112882Ssam register struct a { 36212882Ssam int pid; 36312882Ssam int signo; 36412882Ssam } *uap = (struct a *)u.u_ap; 36518336Smckusick register struct proc *p; 3668032Sroot 36739513Skarels if ((unsigned) uap->signo >= NSIG) 36839513Skarels RETURN (EINVAL); 36918336Smckusick if (uap->pid > 0) { 37018336Smckusick /* kill single process */ 37118336Smckusick p = pfind(uap->pid); 37239513Skarels if (p == 0) 37339513Skarels RETURN (ESRCH); 37439513Skarels if (!CANSIGNAL(p, uap->signo)) 37539513Skarels RETURN (EPERM); 37639513Skarels if (uap->signo) 37718336Smckusick psignal(p, uap->signo); 37839513Skarels RETURN (0); 37918336Smckusick } 38018336Smckusick switch (uap->pid) { 38118336Smckusick case -1: /* broadcast signal */ 38239513Skarels RETURN (killpg1(uap->signo, 0, 1)); 38318336Smckusick case 0: /* signal own process group */ 38439513Skarels RETURN (killpg1(uap->signo, 0, 0)); 38518336Smckusick default: /* negative explicit process group */ 38639513Skarels RETURN (killpg1(uap->signo, -uap->pid, 0)); 38718336Smckusick } 38839513Skarels /* NOTREACHED */ 3898032Sroot } 3908032Sroot 39139513Skarels #ifdef COMPAT_43 39239513Skarels okillpg() 3938032Sroot { 3949989Ssam register struct a { 39537581Smckusick int pgid; 3969989Ssam int signo; 3979989Ssam } *uap = (struct a *)u.u_ap; 3988032Sroot 39939513Skarels if ((unsigned) uap->signo >= NSIG) 40039513Skarels RETURN (EINVAL); 40139513Skarels RETURN (killpg1(uap->signo, uap->pgid, 0)); 4028032Sroot } 40339513Skarels #endif 4048032Sroot 40537581Smckusick killpg1(signo, pgid, all) 40637581Smckusick int signo, pgid, all; 4079989Ssam { 4089989Ssam register struct proc *p; 40937581Smckusick struct pgrp *pgrp; 41039513Skarels int f = 0, error = ESRCH; 41137581Smckusick 41237581Smckusick if (all) 41337581Smckusick /* 41437581Smckusick * broadcast 4157421Sroot */ 41637581Smckusick for (p = allproc; p != NULL; p = p->p_nxt) { 41737581Smckusick if (p->p_ppid == 0 || p->p_flag&SSYS || 41839513Skarels p == u.u_procp || !CANSIGNAL(p, signo)) 41937581Smckusick continue; 42037581Smckusick f++; 42137581Smckusick if (signo) 42237581Smckusick psignal(p, signo); 42337581Smckusick } 42437581Smckusick else { 42537581Smckusick if (pgid == 0) 42637581Smckusick /* 42737581Smckusick * zero pgid means send to my process group. 42837581Smckusick */ 42937581Smckusick pgrp = u.u_procp->p_pgrp; 43037581Smckusick else { 43137581Smckusick pgrp = pgfind(pgid); 43237581Smckusick if (pgrp == NULL) 43339513Skarels return (ESRCH); 43437581Smckusick } 43537581Smckusick for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) { 43639513Skarels if (p->p_ppid == 0 || p->p_flag&SSYS || 43739513Skarels !CANSIGNAL(p, signo)) 43837581Smckusick continue; 43937581Smckusick f++; 44037581Smckusick if (signo) 44137581Smckusick psignal(p, signo); 44218336Smckusick } 4437421Sroot } 44439513Skarels return (f ? 0 : error); 4457421Sroot } 4467421Sroot 44740807Smarc /* XXX - to be removed, as soon as sockets are changed to operate on pgrps 4487421Sroot * Send the specified signal to 44937581Smckusick * all processes with 'pgid' as 4507421Sroot * process group. 4517421Sroot */ 45237581Smckusick gsignal(pgid, sig) 4537421Sroot { 45439513Skarels struct pgrp *pgrp; 4557421Sroot 45639513Skarels if (pgid && (pgrp = pgfind(pgid))) 457*42207Smarc pgsignal(pgrp, sig, 0); 4587421Sroot } 45940807Smarc /* 460*42207Smarc * Send sig to every member of a process group. 461*42207Smarc * If checktty is 1, limit to members which have a controlling 462*42207Smarc * terminal. 46340807Smarc */ 464*42207Smarc pgsignal(pgrp, sig, checkctty) 46539513Skarels struct pgrp *pgrp; 46637581Smckusick { 46737581Smckusick register struct proc *p; 46837581Smckusick 46940807Smarc if (pgrp) 47040807Smarc for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) 471*42207Smarc if (checkctty == 0 || p->p_flag&SCTTY) 472*42207Smarc psignal(p, sig); 47337581Smckusick } 47437581Smckusick 4757421Sroot /* 47639513Skarels * Send a signal caused by a trap to the current process. 47739513Skarels * If it will be caught immediately, deliver it with correct code. 47839513Skarels * Otherwise, post it normally. 47939513Skarels */ 48039513Skarels trapsignal(sig, code) 48139513Skarels register int sig; 48239513Skarels unsigned code; 48339513Skarels { 48439513Skarels register struct proc *p = u.u_procp; 48539513Skarels int mask; 48639513Skarels 48739513Skarels mask = sigmask(sig); 48839513Skarels if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 && 48939513Skarels (p->p_sigmask & mask) == 0) { 49039513Skarels u.u_ru.ru_nsignals++; 49140807Smarc #ifdef KTRACE 49240807Smarc if (KTRPOINT(p, KTR_PSIG)) 49340807Smarc ktrpsig(p->p_tracep, sig, u.u_signal[sig], 49440807Smarc p->p_sigmask, code); 49540807Smarc #endif 49639513Skarels sendsig(u.u_signal[sig], sig, p->p_sigmask, code); 49739513Skarels p->p_sigmask |= u.u_sigmask[sig] | mask; 49839513Skarels } else { 49939513Skarels u.u_arg[1] = code; /* XXX for core dump/debugger */ 50039513Skarels psignal(p, sig); 50139513Skarels } 50239513Skarels } 50339513Skarels 50439513Skarels /* 50540807Smarc * Send the specified signal to the specified process. 50640807Smarc * Most signals do not do anything directly to a process; 50740807Smarc * they set a flag that asks the process to do something to itself. 50840807Smarc * Exceptions: 50940807Smarc * o When a stop signal is sent to a sleeping process that takes the default 51040807Smarc * action, the process is stopped without awakening it. 51140807Smarc * o SIGCONT restarts stopped processes (or puts them back to sleep) 51240807Smarc * regardless of the signal action (eg, blocked or ignored). 51340807Smarc * Other ignored signals are discarded immediately. 5147421Sroot */ 5157421Sroot psignal(p, sig) 5167421Sroot register struct proc *p; 5177421Sroot register int sig; 5187421Sroot { 5197421Sroot register int s; 52039513Skarels register sig_t action; 52117153Sbloom int mask; 5227421Sroot 52339513Skarels if ((unsigned)sig >= NSIG || sig == 0) 52439513Skarels panic("psignal sig"); 52517153Sbloom mask = sigmask(sig); 5267421Sroot 5277421Sroot /* 5287421Sroot * If proc is traced, always give parent a chance. 5297421Sroot */ 5307421Sroot if (p->p_flag & STRC) 5317421Sroot action = SIG_DFL; 5327421Sroot else { 5337421Sroot /* 53412882Ssam * If the signal is being ignored, 53512882Ssam * then we forget about it immediately. 53639513Skarels * (Note: we don't set SIGCONT in p_sigignore, 53739513Skarels * and if it is set to SIG_IGN, 53839513Skarels * action will be SIG_DFL here.) 5397421Sroot */ 54017153Sbloom if (p->p_sigignore & mask) 5417421Sroot return; 54217153Sbloom if (p->p_sigmask & mask) 54312882Ssam action = SIG_HOLD; 54417153Sbloom else if (p->p_sigcatch & mask) 54512882Ssam action = SIG_CATCH; 54640807Smarc else { 54740807Smarc if (p->p_pgrp->pg_jobc == 0 && (sig == SIGTTIN || 54840807Smarc sig == SIGTTOU || sig == SIGTSTP)) 54940807Smarc return; 55012882Ssam action = SIG_DFL; 55140807Smarc } 5527421Sroot } 55339513Skarels switch (sig) { 5547421Sroot 55539513Skarels case SIGTERM: 55639513Skarels if ((p->p_flag&STRC) || action != SIG_DFL) 5577421Sroot break; 55839513Skarels /* FALLTHROUGH */ 5597421Sroot 56039513Skarels case SIGKILL: 56139513Skarels if (p->p_nice > NZERO) 56239513Skarels p->p_nice = NZERO; 56339513Skarels break; 5647421Sroot 56539513Skarels case SIGCONT: 56639513Skarels p->p_sig &= ~stopsigmask; 56739513Skarels break; 56839513Skarels 56939513Skarels case SIGTSTP: 57039513Skarels case SIGTTIN: 57139513Skarels case SIGTTOU: 57239513Skarels case SIGSTOP: 57339513Skarels p->p_sig &= ~sigmask(SIGCONT); 57439513Skarels break; 5757421Sroot } 57639513Skarels p->p_sig |= mask; 57739513Skarels 5787421Sroot /* 57939513Skarels * Defer further processing for signals which are held, 58039513Skarels * except that stopped processes must be continued by SIGCONT. 5817421Sroot */ 58239513Skarels if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP)) 5837421Sroot return; 58417153Sbloom s = splhigh(); 5857421Sroot switch (p->p_stat) { 5867421Sroot 5877421Sroot case SSLEEP: 5887421Sroot /* 58940807Smarc * If process is sleeping uninterruptibly 5907421Sroot * we can't interrupt the sleep... the signal will 5917421Sroot * be noticed when the process returns through 5927421Sroot * trap() or syscall(). 5937421Sroot */ 59440807Smarc if ((p->p_flag & SSINTR) == 0) 5957421Sroot goto out; 5967421Sroot /* 5977421Sroot * Process is sleeping and traced... make it runnable 5987421Sroot * so it can discover the signal in issig() and stop 5997421Sroot * for the parent. 6007421Sroot */ 6017421Sroot if (p->p_flag&STRC) 6027421Sroot goto run; 60339513Skarels /* 60439513Skarels * When a sleeping process receives a stop 60539513Skarels * signal, process immediately if possible. 60639513Skarels * All other (caught or default) signals 60739513Skarels * cause the process to run. 60839513Skarels */ 60939513Skarels if (mask & stopsigmask) { 6107421Sroot if (action != SIG_DFL) 61139513Skarels goto runfast; 6127421Sroot /* 6137421Sroot * If a child in vfork(), stopping could 6147421Sroot * cause deadlock. 6157421Sroot */ 6167421Sroot if (p->p_flag&SVFORK) 6177421Sroot goto out; 61817153Sbloom p->p_sig &= ~mask; 6197421Sroot p->p_cursig = sig; 62039513Skarels if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0) 62139513Skarels psignal(p->p_pptr, SIGCHLD); 6227421Sroot stop(p); 6237421Sroot goto out; 62439513Skarels } else 62539513Skarels goto runfast; 6267421Sroot /*NOTREACHED*/ 6277421Sroot 6287421Sroot case SSTOP: 6297421Sroot /* 6307421Sroot * If traced process is already stopped, 6317421Sroot * then no further action is necessary. 6327421Sroot */ 6337421Sroot if (p->p_flag&STRC) 6347421Sroot goto out; 6357421Sroot switch (sig) { 6367421Sroot 6377421Sroot case SIGKILL: 6387421Sroot /* 6397421Sroot * Kill signal always sets processes running. 6407421Sroot */ 64139513Skarels goto runfast; 6427421Sroot 6437421Sroot case SIGCONT: 6447421Sroot /* 64539513Skarels * If SIGCONT is default (or ignored), we continue 64639513Skarels * the process but don't leave the signal in p_sig, 64739513Skarels * as it has no further action. If SIGCONT is held, 64839513Skarels * continue the process and leave the signal in p_sig. 6497421Sroot * If the process catches SIGCONT, let it handle 6507421Sroot * the signal itself. If it isn't waiting on 6517421Sroot * an event, then it goes back to run state. 6527421Sroot * Otherwise, process goes back to sleep state. 6537421Sroot */ 65439513Skarels p->p_cursig = 0; /* ??? XXX */ 65539513Skarels if (action == SIG_DFL) 65639513Skarels p->p_sig &= ~mask; 65739513Skarels if (action == SIG_CATCH) 65839513Skarels goto runfast; 65939513Skarels if (p->p_wchan == 0) 6607421Sroot goto run; 6617421Sroot p->p_stat = SSLEEP; 6627421Sroot goto out; 6637421Sroot 6647421Sroot case SIGSTOP: 6657421Sroot case SIGTSTP: 6667421Sroot case SIGTTIN: 6677421Sroot case SIGTTOU: 6687421Sroot /* 6697421Sroot * Already stopped, don't need to stop again. 6707421Sroot * (If we did the shell could get confused.) 6717421Sroot */ 67217153Sbloom p->p_sig &= ~mask; /* take it away */ 6737421Sroot goto out; 6747421Sroot 6757421Sroot default: 6767421Sroot /* 6777421Sroot * If process is sleeping interruptibly, then 67840807Smarc * simulate a wakeup so that when it is continued, 67940807Smarc * it will be made runnable and can look at the signal. 68040807Smarc * But don't setrun the process, leave it stopped. 6817421Sroot */ 68240807Smarc if (p->p_wchan && p->p_flag & SSINTR) 6837421Sroot unsleep(p); 6847421Sroot goto out; 6857421Sroot } 6867421Sroot /*NOTREACHED*/ 6877421Sroot 6887421Sroot default: 6897421Sroot /* 6907421Sroot * SRUN, SIDL, SZOMB do nothing with the signal, 6917421Sroot * other than kicking ourselves if we are running. 6927421Sroot * It will either never be noticed, or noticed very soon. 6937421Sroot */ 6947421Sroot if (p == u.u_procp && !noproc) 6957421Sroot aston(); 6967421Sroot goto out; 6977421Sroot } 6987421Sroot /*NOTREACHED*/ 69939513Skarels 70039513Skarels runfast: 7017421Sroot /* 7027421Sroot * Raise priority to at least PUSER. 7037421Sroot */ 7047421Sroot if (p->p_pri > PUSER) 70517399Skarels p->p_pri = PUSER; 70639513Skarels run: 7077421Sroot setrun(p); 7087421Sroot out: 7097421Sroot splx(s); 7107421Sroot } 7117421Sroot 7127421Sroot /* 71340807Smarc * If the current process has a signal to process (should be caught 71440807Smarc * or cause termination, should interrupt current syscall), 71540807Smarc * return the signal number. Stop signals with default action 71640807Smarc * are processed immediately, then cleared; they aren't returned. 7177421Sroot * This is asked at least once each time a process enters the 7187421Sroot * system (though this can usually be done without actually 7197421Sroot * calling issig by checking the pending signal masks.) 7207421Sroot */ 7217421Sroot issig() 7227421Sroot { 7237421Sroot register struct proc *p; 72439513Skarels register int sig, mask; 7257421Sroot 7267421Sroot p = u.u_procp; 7277421Sroot for (;;) { 72839513Skarels mask = p->p_sig &~ p->p_sigmask; 7297421Sroot if (p->p_flag&SVFORK) 73039513Skarels mask &= ~stopsigmask; 73140807Smarc if (mask == 0) /* no signal to send */ 73240807Smarc return (0); 73339513Skarels sig = ffs((long)mask); 73417153Sbloom mask = sigmask(sig); 73540807Smarc /* 73640807Smarc * We should see pending but ignored signals 73740807Smarc * only if STRC was on when they were posted. 73840807Smarc */ 73940807Smarc if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) { 74040807Smarc p->p_sig &= ~mask; 74140807Smarc continue; 74240807Smarc } 74312882Ssam if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) { 7447421Sroot /* 7457421Sroot * If traced, always stop, and stay 7467421Sroot * stopped until released by the parent. 7477421Sroot */ 74840807Smarc p->p_cursig = sig; 74918331Skarels psignal(p->p_pptr, SIGCHLD); 7507421Sroot do { 7517421Sroot stop(p); 7527421Sroot swtch(); 7537421Sroot } while (!procxmt() && p->p_flag&STRC); 7547421Sroot 7557421Sroot /* 75614782Ssam * If the traced bit got turned off, 75740807Smarc * go back up to the top to rescan signals. 75814782Ssam * This ensures that p_sig* and u_signal are consistent. 7597421Sroot */ 76040807Smarc if ((p->p_flag&STRC) == 0) 7617421Sroot continue; 7627421Sroot 7637421Sroot /* 7647421Sroot * If parent wants us to take the signal, 7657421Sroot * then it will leave it in p->p_cursig; 7667421Sroot * otherwise we just look for signals again. 7677421Sroot */ 76840807Smarc p->p_sig &= ~mask; /* clear the old signal */ 7697421Sroot sig = p->p_cursig; 7707421Sroot if (sig == 0) 7717421Sroot continue; 77214782Ssam 77314782Ssam /* 77440807Smarc * Put the new signal into p_sig. 77540807Smarc * If signal is being masked, 77640807Smarc * look for other signals. 77714782Ssam */ 77817153Sbloom mask = sigmask(sig); 77940807Smarc p->p_sig |= mask; 78040807Smarc if (p->p_sigmask & mask) 78114782Ssam continue; 7827421Sroot } 78340807Smarc 78440807Smarc /* 78540807Smarc * Decide whether the signal should be returned. 78640807Smarc * Return the signal's number, or fall through 78740807Smarc * to clear it from the pending mask. 78840807Smarc */ 78924901Skarels switch ((int)u.u_signal[sig]) { 7907421Sroot 7917421Sroot case SIG_DFL: 7927421Sroot /* 7937421Sroot * Don't take default actions on system processes. 7947421Sroot */ 7957421Sroot if (p->p_ppid == 0) 79640807Smarc break; /* == ignore */ 79740807Smarc /* 79840807Smarc * If there is a pending stop signal to process 79940807Smarc * with default action, stop here, 80040807Smarc * then clear the signal. 80140807Smarc */ 80239513Skarels if (mask & stopsigmask) { 8037421Sroot if (p->p_flag&STRC) 80440807Smarc break; /* == ignore */ 80540807Smarc p->p_cursig = sig; 8067421Sroot stop(p); 80739513Skarels if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0) 80839513Skarels psignal(p->p_pptr, SIGCHLD); 8097421Sroot swtch(); 81040807Smarc break; 81139513Skarels } else if (mask & defaultignmask) { 8127421Sroot /* 81339513Skarels * Except for SIGCONT, shouldn't get here. 81439513Skarels * Default action is to ignore; drop it. 8157421Sroot */ 81640807Smarc break; /* == ignore */ 81739513Skarels } else 81840807Smarc return (sig); 8197421Sroot /*NOTREACHED*/ 8207421Sroot 8217421Sroot case SIG_IGN: 8227421Sroot /* 82339513Skarels * Masking above should prevent us ever trying 82439513Skarels * to take action on an ignored signal other 82539513Skarels * than SIGCONT, unless process is traced. 8267421Sroot */ 82739513Skarels if (sig != SIGCONT && (p->p_flag&STRC) == 0) 8287421Sroot printf("issig\n"); 82940807Smarc break; /* == ignore */ 8307421Sroot 8317421Sroot default: 8327421Sroot /* 8337421Sroot * This signal has an action, let 8347421Sroot * psig process it. 8357421Sroot */ 83640807Smarc return (sig); 8377421Sroot } 83840807Smarc p->p_sig &= ~mask; /* take the signal! */ 8397421Sroot } 84040807Smarc /* NOTREACHED */ 8417421Sroot } 8427421Sroot 8437421Sroot /* 8447421Sroot * Put the argument process into the stopped 84518331Skarels * state and notify the parent via wakeup. 84618331Skarels * Signals are handled elsewhere. 84740807Smarc * The process must not be on the run queue. 8487421Sroot */ 8497421Sroot stop(p) 8507421Sroot register struct proc *p; 8517421Sroot { 8527421Sroot 8537421Sroot p->p_stat = SSTOP; 8547421Sroot p->p_flag &= ~SWTED; 8557421Sroot wakeup((caddr_t)p->p_pptr); 8567421Sroot } 8577421Sroot 8587421Sroot /* 85940807Smarc * Perform the action specified by the current signal. 8607421Sroot * The usual sequence is: 86140807Smarc * if (sig = CURSIG(p)) 86240807Smarc * psig(sig); 8637421Sroot */ 86440807Smarc psig(sig) 86540807Smarc register int sig; 8667421Sroot { 86712882Ssam register struct proc *p = u.u_procp; 86839513Skarels int mask, returnmask; 86939513Skarels register sig_t action; 8707421Sroot 87139513Skarels do { 87240807Smarc #ifdef DIAGNOSTIC 87339513Skarels if (sig == 0) 87439513Skarels panic("psig"); 87540807Smarc #endif 87640807Smarc mask = sigmask(sig); 87740807Smarc p->p_sig &= ~mask; 87839513Skarels action = u.u_signal[sig]; 87940807Smarc #ifdef KTRACE 88040807Smarc if (KTRPOINT(p, KTR_PSIG)) 88140807Smarc ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ? 88240807Smarc u.u_oldmask : p->p_sigmask, 0); 88340807Smarc #endif 88439513Skarels if (action != SIG_DFL) { 88539513Skarels #ifdef DIAGNOSTIC 88639513Skarels if (action == SIG_IGN || (p->p_sigmask & mask)) 88739513Skarels panic("psig action"); 88839513Skarels #endif 88939513Skarels u.u_error = 0; 89039513Skarels /* 89139513Skarels * Set the new mask value and also defer further 89239513Skarels * occurences of this signal. 89339513Skarels * 89439513Skarels * Special case: user has done a sigpause. Here the 89539513Skarels * current mask is not of interest, but rather the 89639513Skarels * mask from before the sigpause is what we want 89739513Skarels * restored after the signal processing is completed. 89839513Skarels */ 89939513Skarels (void) splhigh(); 90039513Skarels if (p->p_flag & SOMASK) { 90139513Skarels returnmask = u.u_oldmask; 90239513Skarels p->p_flag &= ~SOMASK; 90339513Skarels } else 90439513Skarels returnmask = p->p_sigmask; 90539513Skarels p->p_sigmask |= u.u_sigmask[sig] | mask; 90639513Skarels (void) spl0(); 90739513Skarels u.u_ru.ru_nsignals++; 90839513Skarels sendsig(action, sig, returnmask, 0); 90939513Skarels continue; 9107421Sroot } 91139513Skarels u.u_acflag |= AXSIG; 91239513Skarels switch (sig) { 9137421Sroot 91439513Skarels case SIGILL: 91539513Skarels case SIGIOT: 91639513Skarels case SIGBUS: 91739513Skarels case SIGQUIT: 91839513Skarels case SIGTRAP: 91939513Skarels case SIGEMT: 92039513Skarels case SIGFPE: 92139513Skarels case SIGSEGV: 92239513Skarels case SIGSYS: 92339513Skarels u.u_arg[0] = sig; 92439513Skarels if (core() == 0) 92539513Skarels sig |= WCOREFLAG; 92639513Skarels } 92739513Skarels exit(W_EXITCODE(0, sig)); 92839513Skarels /* NOTREACHED */ 92940807Smarc } while (sig = CURSIG(p)); 9307421Sroot } 9317421Sroot 9327421Sroot /* 93339513Skarels * Create a core image on the file "core". 9347421Sroot * It writes UPAGES block of the 9357421Sroot * user.h area followed by the entire 9367421Sroot * data+stack segments. 9377421Sroot */ 9387421Sroot core() 9397421Sroot { 94037728Smckusick register struct vnode *vp; 94139513Skarels register struct proc *p = u.u_procp; 94216692Smckusick register struct nameidata *ndp = &u.u_nd; 94337580Smckusick struct vattr vattr; 94437580Smckusick int error; 9457421Sroot 94639513Skarels if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid) 94737580Smckusick return (EFAULT); 94837580Smckusick if (ctob(UPAGES + u.u_dsize + u.u_ssize) >= 9498032Sroot u.u_rlimit[RLIMIT_CORE].rlim_cur) 95037580Smckusick return (EFAULT); 95139513Skarels if (p->p_textp) { 95239513Skarels VOP_LOCK(p->p_textp->x_vptr); 95339513Skarels error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred); 95439513Skarels VOP_UNLOCK(p->p_textp->x_vptr); 95537580Smckusick if (error) 95637580Smckusick return (EFAULT); 95737580Smckusick } 95816692Smckusick ndp->ni_segflg = UIO_SYSSPACE; 95916692Smckusick ndp->ni_dirp = "core"; 96037580Smckusick if (error = vn_open(ndp, FCREAT|FWRITE, 0644)) 96137580Smckusick return (error); 96237580Smckusick vp = ndp->ni_vp; 96338394Smckusick VOP_LOCK(vp); 96437580Smckusick if (vp->v_type != VREG || 96537728Smckusick VOP_GETATTR(vp, &vattr, u.u_cred) || 96637580Smckusick vattr.va_nlink != 1) { 96738394Smckusick vput(vp); 96838394Smckusick return (EFAULT); 9697818Sroot } 97042004Smckusick #ifdef MAPMEM 97142004Smckusick mmcore(); 97230290Ssam #endif 97341362Smckusick VATTR_NULL(&vattr); 97437580Smckusick vattr.va_size = 0; 97537728Smckusick VOP_SETATTR(vp, &vattr, u.u_cred); 9767818Sroot u.u_acflag |= ACORE; 97742004Smckusick #ifdef HPUXCOMPAT 97842004Smckusick /* 97942004Smckusick * BLETCH! If we loaded from an HPUX format binary file 98042004Smckusick * we have to dump an HPUX style user struct so that the 98142004Smckusick * HPUX debuggers can grok it. 98242004Smckusick */ 98342004Smckusick if (u.u_pcb.pcb_flags & PCB_HPUXBIN) 98442004Smckusick error = hpuxdumpu(vp, ndp->ni_cred); 98542004Smckusick else 98642004Smckusick #endif 98737580Smckusick error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0, 98838394Smckusick UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 98937580Smckusick if (error == 0) 99037580Smckusick error = vn_rdwr(UIO_WRITE, vp, 99139513Skarels (caddr_t)ctob(dptov(p, 0)), 99238394Smckusick (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE, 99338394Smckusick IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 99437580Smckusick if (error == 0) 99537580Smckusick error = vn_rdwr(UIO_WRITE, vp, 99639513Skarels (caddr_t)ctob(sptov(p, u.u_ssize - 1)), 99726354Skarels (int)ctob(u.u_ssize), 99838394Smckusick (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE, 99938394Smckusick IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 100038394Smckusick vput(vp); 100137580Smckusick return (error); 10027421Sroot } 100339513Skarels 100439513Skarels /* 100539513Skarels * Nonexistent system call-- signal process (may want to handle it). 100639513Skarels * Flag error in case process won't see signal immediately (blocked or ignored). 100739513Skarels */ 100839513Skarels nosys() 100939513Skarels { 101039513Skarels 101139513Skarels psignal(u.u_procp, SIGSYS); 101239513Skarels u.u_error = EINVAL; 101339513Skarels } 1014