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*39513Skarels * @(#)kern_sig.c 7.11 (Berkeley) 11/11/89 1823374Smckusick */ 197421Sroot 2017092Sbloom #include "param.h" 2117092Sbloom #include "systm.h" 22*39513Skarels #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 "mount.h" 2917092Sbloom #include "text.h" 3017092Sbloom #include "seg.h" 3117092Sbloom #include "vm.h" 3217092Sbloom #include "acct.h" 3317092Sbloom #include "uio.h" 3437580Smckusick #include "file.h" 3517092Sbloom #include "kernel.h" 36*39513Skarels #include "wait.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)) 45*39513Skarels #define defaultignmask (sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \ 46*39513Skarels sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO)) 4712951Ssam 4817013Smckusick /* 49*39513Skarels * Can the current process (u.u_procp) send the specified signal 50*39513Skarels * to the specified process? 5117013Smckusick */ 52*39513Skarels #define CANSIGNAL(p, signo) \ 53*39513Skarels (u.u_uid == 0 || \ 54*39513Skarels u.u_uid == (p)->p_uid || u.u_uid == (p)->p_ruid || \ 55*39513Skarels u.u_procp->p_ruid == (p)->p_uid || \ 56*39513Skarels u.u_procp->p_ruid == (p)->p_ruid || \ 57*39513Skarels ((signo) == SIGCONT && (p)->p_session == u.u_procp->p_session)) 58*39513Skarels 59*39513Skarels sigaction() 607421Sroot { 6112951Ssam register struct a { 6212882Ssam int signo; 63*39513Skarels struct sigaction *nsa; 64*39513Skarels struct sigaction *osa; 6512882Ssam } *uap = (struct a *)u.u_ap; 66*39513Skarels struct sigaction vec; 67*39513Skarels register struct sigaction *sa; 6812882Ssam register int sig; 69*39513Skarels int bit, error; 707421Sroot 7112882Ssam sig = uap->signo; 72*39513Skarels if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) 73*39513Skarels RETURN (EINVAL); 74*39513Skarels sa = &vec; 75*39513Skarels if (uap->osa) { 76*39513Skarels sa->sa_handler = u.u_signal[sig]; 77*39513Skarels sa->sa_mask = u.u_sigmask[sig]; 7818308Smckusick bit = sigmask(sig); 79*39513Skarels sa->sa_flags = 0; 8018308Smckusick if ((u.u_sigonstack & bit) != 0) 81*39513Skarels sa->sa_flags |= SA_ONSTACK; 82*39513Skarels if ((u.u_sigintr & bit) == 0) 83*39513Skarels sa->sa_flags |= SA_RESTART; 84*39513Skarels if (u.u_procp->p_flag & SNOCLDSTOP) 85*39513Skarels sa->sa_flags |= SA_NOCLDSTOP; 86*39513Skarels if (error = copyout((caddr_t)sa, (caddr_t)uap->osa, 87*39513Skarels sizeof (vec))) 88*39513Skarels RETURN (error); 8912951Ssam } 90*39513Skarels if (uap->nsa) { 91*39513Skarels if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa, 92*39513Skarels sizeof (vec))) 93*39513Skarels RETURN (error); 94*39513Skarels setsigvec(sig, sa); 9512951Ssam } 96*39513Skarels RETURN (0); 977421Sroot } 987421Sroot 99*39513Skarels setsigvec(sig, sa) 10012951Ssam int sig; 101*39513Skarels 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(); 112*39513Skarels u.u_signal[sig] = sa->sa_handler; 113*39513Skarels u.u_sigmask[sig] = sa->sa_mask &~ sigcantmask; 114*39513Skarels if ((sa->sa_flags & SA_RESTART) == 0) 11518308Smckusick u.u_sigintr |= bit; 11618308Smckusick else 11718308Smckusick u.u_sigintr &= ~bit; 118*39513Skarels if (sa->sa_flags & SA_ONSTACK) 11912951Ssam u.u_sigonstack |= bit; 12012951Ssam else 12112951Ssam u.u_sigonstack &= ~bit; 122*39513Skarels if (sig == SIGCHLD) { 123*39513Skarels if (sa->sa_flags & SA_NOCLDSTOP) 124*39513Skarels p->p_flag |= SNOCLDSTOP; 125*39513Skarels else 126*39513Skarels p->p_flag &= ~SNOCLDSTOP; 127*39513Skarels } 128*39513Skarels /* 129*39513Skarels * Set bit in p_sigignore for signals that are set to SIG_IGN, 130*39513Skarels * and for signals set to SIG_DFL where the default is to ignore. 131*39513Skarels * However, don't put SIGCONT in p_sigignore, 132*39513Skarels * as we have to restart the process. 133*39513Skarels */ 134*39513Skarels if (sa->sa_handler == SIG_IGN || 135*39513Skarels (bit & defaultignmask && sa->sa_handler == SIG_DFL)) { 13612951Ssam p->p_sig &= ~bit; /* never to be seen again */ 137*39513Skarels if (sig != SIGCONT) 138*39513Skarels p->p_sigignore |= bit; /* easier in psignal */ 13912951Ssam p->p_sigcatch &= ~bit; 14012882Ssam } else { 14112951Ssam p->p_sigignore &= ~bit; 142*39513Skarels if (sa->sa_handler == SIG_DFL) 14312951Ssam p->p_sigcatch &= ~bit; 14412882Ssam else 14512951Ssam p->p_sigcatch |= bit; 14612882Ssam } 14712882Ssam (void) spl0(); 14812882Ssam } 14912882Ssam 150*39513Skarels /* 151*39513Skarels * Initialize signal state for process 0; 152*39513Skarels * set to ignore signals that are ignored by default. 153*39513Skarels */ 154*39513Skarels siginit(p) 155*39513Skarels struct proc *p; 1567421Sroot { 157*39513Skarels 158*39513Skarels p->p_sigignore = defaultignmask &~ sigmask(SIGCONT); 159*39513Skarels } 160*39513Skarels 161*39513Skarels /* 162*39513Skarels * Reset signals for an exec of the specified process. 163*39513Skarels */ 164*39513Skarels execsigs(p) 165*39513Skarels register struct proc *p; 166*39513Skarels { 167*39513Skarels register int nc, mask; 168*39513Skarels 169*39513Skarels /* 170*39513Skarels * Reset caught signals. Held signals remain held 171*39513Skarels * through p_sigmask (unless they were caught, 172*39513Skarels * and are now ignored by default). 173*39513Skarels */ 174*39513Skarels while (p->p_sigcatch) { 175*39513Skarels nc = ffs((long)p->p_sigcatch); 176*39513Skarels mask = sigmask(nc); 177*39513Skarels p->p_sigcatch &= ~mask; 178*39513Skarels if (mask & defaultignmask) { 179*39513Skarels if (nc != SIGCONT) 180*39513Skarels p->p_sigignore |= mask; 181*39513Skarels p->p_sig &= ~mask; 182*39513Skarels } 183*39513Skarels u.u_signal[nc] = SIG_DFL; 184*39513Skarels } 185*39513Skarels /* 186*39513Skarels * Reset stack state to the user stack. 187*39513Skarels * Clear set of signals caught on the signal stack. 188*39513Skarels */ 189*39513Skarels u.u_onstack = 0; 190*39513Skarels u.u_sigsp = 0; 191*39513Skarels u.u_sigonstack = 0; 192*39513Skarels } 193*39513Skarels 194*39513Skarels /* 195*39513Skarels * Manipulate signal mask. 196*39513Skarels * Note that we receive new mask, not pointer, 197*39513Skarels * and return old mask as return value; 198*39513Skarels * the library stub does the rest. 199*39513Skarels */ 200*39513Skarels sigprocmask() 201*39513Skarels { 20212882Ssam struct a { 203*39513Skarels int how; 204*39513Skarels sigset_t mask; 205*39513Skarels } *uap = (struct a *)u.u_ap; 206*39513Skarels register struct proc *p = u.u_procp; 207*39513Skarels int error = 0; 208*39513Skarels 209*39513Skarels u.u_r.r_val1 = p->p_sigmask; 210*39513Skarels (void) splhigh(); 211*39513Skarels 212*39513Skarels switch (uap->how) { 213*39513Skarels case SIG_BLOCK: 214*39513Skarels p->p_sigmask |= uap->mask &~ sigcantmask; 215*39513Skarels break; 216*39513Skarels 217*39513Skarels case SIG_UNBLOCK: 218*39513Skarels p->p_sigmask &= ~uap->mask; 219*39513Skarels break; 220*39513Skarels 221*39513Skarels case SIG_SETMASK: 222*39513Skarels p->p_sigmask = uap->mask &~ sigcantmask; 223*39513Skarels break; 224*39513Skarels 225*39513Skarels default: 226*39513Skarels error = EINVAL; 227*39513Skarels break; 228*39513Skarels } 229*39513Skarels (void) spl0(); 230*39513Skarels RETURN (error); 231*39513Skarels } 232*39513Skarels 233*39513Skarels sigpending() 234*39513Skarels { 235*39513Skarels 236*39513Skarels u.u_r.r_val1 = u.u_procp->p_sig; 237*39513Skarels RETURN (0); 238*39513Skarels } 239*39513Skarels 240*39513Skarels #ifdef COMPAT_43 241*39513Skarels /* 242*39513Skarels * Generalized interface signal handler, 4.3-compatible. 243*39513Skarels */ 244*39513Skarels osigvec() 245*39513Skarels { 246*39513Skarels register struct a { 247*39513Skarels int signo; 248*39513Skarels struct sigvec *nsv; 249*39513Skarels struct sigvec *osv; 250*39513Skarels } *uap = (struct a *)u.u_ap; 251*39513Skarels struct sigvec vec; 252*39513Skarels register struct sigvec *sv; 253*39513Skarels register int sig; 254*39513Skarels int bit, error; 255*39513Skarels 256*39513Skarels sig = uap->signo; 257*39513Skarels if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) 258*39513Skarels RETURN (EINVAL); 259*39513Skarels sv = &vec; 260*39513Skarels if (uap->osv) { 261*39513Skarels *(sig_t *)&sv->sv_handler = u.u_signal[sig]; 262*39513Skarels sv->sv_mask = u.u_sigmask[sig]; 263*39513Skarels bit = sigmask(sig); 264*39513Skarels sv->sv_flags = 0; 265*39513Skarels if ((u.u_sigonstack & bit) != 0) 266*39513Skarels sv->sv_flags |= SV_ONSTACK; 267*39513Skarels if ((u.u_sigintr & bit) != 0) 268*39513Skarels sv->sv_flags |= SV_INTERRUPT; 269*39513Skarels if (u.u_procp->p_flag & SNOCLDSTOP) 270*39513Skarels sv->sv_flags |= SA_NOCLDSTOP; 271*39513Skarels if (error = copyout((caddr_t)sv, (caddr_t)uap->osv, 272*39513Skarels sizeof (vec))) 273*39513Skarels RETURN (error); 274*39513Skarels } 275*39513Skarels if (uap->nsv) { 276*39513Skarels if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv, 277*39513Skarels sizeof (vec))) 278*39513Skarels RETURN (error); 279*39513Skarels sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */ 280*39513Skarels setsigvec(sig, sv); 281*39513Skarels } 282*39513Skarels RETURN (0); 283*39513Skarels } 284*39513Skarels 285*39513Skarels osigblock() 286*39513Skarels { 287*39513Skarels 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; 294*39513Skarels p->p_sigmask |= uap->mask &~ sigcantmask; 29512882Ssam (void) spl0(); 296*39513Skarels RETURN (0); 2977499Sroot } 2987499Sroot 299*39513Skarels 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; 308*39513Skarels p->p_sigmask = uap->mask &~ sigcantmask; 30912882Ssam (void) spl0(); 310*39513Skarels RETURN (0); 3117499Sroot } 312*39513Skarels #endif 3137499Sroot 314*39513Skarels /* 315*39513Skarels * Suspend process until signal, providing mask to be set 316*39513Skarels * in the meantime. Note nonstandard calling convention: 317*39513Skarels * libc stub passes mask, not pointer, to save a copyin. 318*39513Skarels */ 319*39513Skarels sigsuspend() 3207499Sroot { 32112882Ssam struct a { 322*39513Skarels 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; 335*39513Skarels p->p_sigmask = uap->mask &~ sigcantmask; 33612882Ssam for (;;) 33712882Ssam sleep((caddr_t)&u, PSLEP); 33812882Ssam /*NOTREACHED*/ 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; 348*39513Skarels int error = 0; 3497499Sroot 350*39513Skarels if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack, 351*39513Skarels (caddr_t)uap->oss, sizeof (struct sigstack)))) 352*39513Skarels RETURN (error); 353*39513Skarels if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, 354*39513Skarels sizeof (ss))) == 0) 355*39513Skarels u.u_sigstack = ss; 356*39513Skarels 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 367*39513Skarels if ((unsigned) uap->signo >= NSIG) 368*39513Skarels RETURN (EINVAL); 36918336Smckusick if (uap->pid > 0) { 37018336Smckusick /* kill single process */ 37118336Smckusick p = pfind(uap->pid); 372*39513Skarels if (p == 0) 373*39513Skarels RETURN (ESRCH); 374*39513Skarels if (!CANSIGNAL(p, uap->signo)) 375*39513Skarels RETURN (EPERM); 376*39513Skarels if (uap->signo) 37718336Smckusick psignal(p, uap->signo); 378*39513Skarels RETURN (0); 37918336Smckusick } 38018336Smckusick switch (uap->pid) { 38118336Smckusick case -1: /* broadcast signal */ 382*39513Skarels RETURN (killpg1(uap->signo, 0, 1)); 38318336Smckusick case 0: /* signal own process group */ 384*39513Skarels RETURN (killpg1(uap->signo, 0, 0)); 38518336Smckusick default: /* negative explicit process group */ 386*39513Skarels RETURN (killpg1(uap->signo, -uap->pid, 0)); 38718336Smckusick } 388*39513Skarels /* NOTREACHED */ 3898032Sroot } 3908032Sroot 391*39513Skarels #ifdef COMPAT_43 392*39513Skarels okillpg() 3938032Sroot { 3949989Ssam register struct a { 39537581Smckusick int pgid; 3969989Ssam int signo; 3979989Ssam } *uap = (struct a *)u.u_ap; 3988032Sroot 399*39513Skarels if ((unsigned) uap->signo >= NSIG) 400*39513Skarels RETURN (EINVAL); 401*39513Skarels RETURN (killpg1(uap->signo, uap->pgid, 0)); 4028032Sroot } 403*39513Skarels #endif 4048032Sroot 40537581Smckusick killpg1(signo, pgid, all) 40637581Smckusick int signo, pgid, all; 4079989Ssam { 4089989Ssam register struct proc *p; 40937581Smckusick struct pgrp *pgrp; 410*39513Skarels 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 || 418*39513Skarels 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) 433*39513Skarels return (ESRCH); 43437581Smckusick } 43537581Smckusick for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) { 436*39513Skarels if (p->p_ppid == 0 || p->p_flag&SSYS || 437*39513Skarels !CANSIGNAL(p, signo)) 43837581Smckusick continue; 43937581Smckusick f++; 44037581Smckusick if (signo) 44137581Smckusick psignal(p, signo); 44218336Smckusick } 4437421Sroot } 444*39513Skarels return (f ? 0 : error); 4457421Sroot } 4467421Sroot 4477421Sroot /* 4487421Sroot * Send the specified signal to 44937581Smckusick * all processes with 'pgid' as 4507421Sroot * process group. 4517421Sroot */ 45237581Smckusick gsignal(pgid, sig) 4537421Sroot { 454*39513Skarels struct pgrp *pgrp; 4557421Sroot 456*39513Skarels if (pgid && (pgrp = pgfind(pgid))) 457*39513Skarels pgsignal(pgrp, sig); 4587421Sroot } 4597421Sroot 46037581Smckusick pgsignal(pgrp, sig) 461*39513Skarels struct pgrp *pgrp; 46237581Smckusick { 46337581Smckusick register struct proc *p; 46437581Smckusick 465*39513Skarels for (p = pgrp->pg_mem; p; p = p->p_pgrpnxt) 46637581Smckusick psignal(p, sig); 46737581Smckusick } 46837581Smckusick 4697421Sroot /* 470*39513Skarels * Send a signal caused by a trap to the current process. 471*39513Skarels * If it will be caught immediately, deliver it with correct code. 472*39513Skarels * Otherwise, post it normally. 473*39513Skarels */ 474*39513Skarels trapsignal(sig, code) 475*39513Skarels register int sig; 476*39513Skarels unsigned code; 477*39513Skarels { 478*39513Skarels register struct proc *p = u.u_procp; 479*39513Skarels int mask; 480*39513Skarels 481*39513Skarels mask = sigmask(sig); 482*39513Skarels if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 && 483*39513Skarels (p->p_sigmask & mask) == 0) { 484*39513Skarels u.u_ru.ru_nsignals++; 485*39513Skarels sendsig(u.u_signal[sig], sig, p->p_sigmask, code); 486*39513Skarels p->p_sigmask |= u.u_sigmask[sig] | mask; 487*39513Skarels } else { 488*39513Skarels u.u_arg[1] = code; /* XXX for core dump/debugger */ 489*39513Skarels psignal(p, sig); 490*39513Skarels } 491*39513Skarels } 492*39513Skarels 493*39513Skarels /* 4947421Sroot * Send the specified signal to 4957421Sroot * the specified process. 4967421Sroot */ 4977421Sroot psignal(p, sig) 4987421Sroot register struct proc *p; 4997421Sroot register int sig; 5007421Sroot { 5017421Sroot register int s; 502*39513Skarels register sig_t action; 50317153Sbloom int mask; 5047421Sroot 505*39513Skarels if ((unsigned)sig >= NSIG || sig == 0) 506*39513Skarels panic("psignal sig"); 507*39513Skarels if (p->p_pgrp->pg_jobc == 0 && 508*39513Skarels (sig == SIGTTIN || sig == SIGTTOU || sig == SIGTSTP)) 5097421Sroot return; 51017153Sbloom mask = sigmask(sig); 5117421Sroot 5127421Sroot /* 5137421Sroot * If proc is traced, always give parent a chance. 5147421Sroot */ 5157421Sroot if (p->p_flag & STRC) 5167421Sroot action = SIG_DFL; 5177421Sroot else { 5187421Sroot /* 51912882Ssam * If the signal is being ignored, 52012882Ssam * then we forget about it immediately. 521*39513Skarels * (Note: we don't set SIGCONT in p_sigignore, 522*39513Skarels * and if it is set to SIG_IGN, 523*39513Skarels * action will be SIG_DFL here.) 5247421Sroot */ 52517153Sbloom if (p->p_sigignore & mask) 5267421Sroot return; 52717153Sbloom if (p->p_sigmask & mask) 52812882Ssam action = SIG_HOLD; 52917153Sbloom else if (p->p_sigcatch & mask) 53012882Ssam action = SIG_CATCH; 53112882Ssam else 53212882Ssam action = SIG_DFL; 5337421Sroot } 534*39513Skarels switch (sig) { 5357421Sroot 536*39513Skarels case SIGTERM: 537*39513Skarels if ((p->p_flag&STRC) || action != SIG_DFL) 5387421Sroot break; 539*39513Skarels /* FALLTHROUGH */ 5407421Sroot 541*39513Skarels case SIGKILL: 542*39513Skarels if (p->p_nice > NZERO) 543*39513Skarels p->p_nice = NZERO; 544*39513Skarels break; 5457421Sroot 546*39513Skarels case SIGCONT: 547*39513Skarels p->p_sig &= ~stopsigmask; 548*39513Skarels break; 549*39513Skarels 550*39513Skarels case SIGTSTP: 551*39513Skarels case SIGTTIN: 552*39513Skarels case SIGTTOU: 553*39513Skarels case SIGSTOP: 554*39513Skarels p->p_sig &= ~sigmask(SIGCONT); 555*39513Skarels break; 5567421Sroot } 557*39513Skarels p->p_sig |= mask; 558*39513Skarels 5597421Sroot /* 560*39513Skarels * Defer further processing for signals which are held, 561*39513Skarels * except that stopped processes must be continued by SIGCONT. 5627421Sroot */ 563*39513Skarels if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP)) 5647421Sroot return; 56517153Sbloom s = splhigh(); 5667421Sroot switch (p->p_stat) { 5677421Sroot 5687421Sroot case SSLEEP: 5697421Sroot /* 5707421Sroot * If process is sleeping at negative priority 5717421Sroot * we can't interrupt the sleep... the signal will 5727421Sroot * be noticed when the process returns through 5737421Sroot * trap() or syscall(). 5747421Sroot */ 5757421Sroot if (p->p_pri <= PZERO) 5767421Sroot goto out; 5777421Sroot /* 5787421Sroot * Process is sleeping and traced... make it runnable 5797421Sroot * so it can discover the signal in issig() and stop 5807421Sroot * for the parent. 5817421Sroot */ 5827421Sroot if (p->p_flag&STRC) 5837421Sroot goto run; 584*39513Skarels /* 585*39513Skarels * When a sleeping process receives a stop 586*39513Skarels * signal, process immediately if possible. 587*39513Skarels * All other (caught or default) signals 588*39513Skarels * cause the process to run. 589*39513Skarels */ 590*39513Skarels if (mask & stopsigmask) { 5917421Sroot if (action != SIG_DFL) 592*39513Skarels goto runfast; 5937421Sroot /* 5947421Sroot * If a child in vfork(), stopping could 5957421Sroot * cause deadlock. 5967421Sroot */ 5977421Sroot if (p->p_flag&SVFORK) 5987421Sroot goto out; 59917153Sbloom p->p_sig &= ~mask; 6007421Sroot p->p_cursig = sig; 601*39513Skarels if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0) 602*39513Skarels psignal(p->p_pptr, SIGCHLD); 6037421Sroot stop(p); 6047421Sroot goto out; 605*39513Skarels } else 606*39513Skarels goto runfast; 6077421Sroot /*NOTREACHED*/ 6087421Sroot 6097421Sroot case SSTOP: 6107421Sroot /* 6117421Sroot * If traced process is already stopped, 6127421Sroot * then no further action is necessary. 6137421Sroot */ 6147421Sroot if (p->p_flag&STRC) 6157421Sroot goto out; 6167421Sroot switch (sig) { 6177421Sroot 6187421Sroot case SIGKILL: 6197421Sroot /* 6207421Sroot * Kill signal always sets processes running. 6217421Sroot */ 622*39513Skarels goto runfast; 6237421Sroot 6247421Sroot case SIGCONT: 6257421Sroot /* 626*39513Skarels * If SIGCONT is default (or ignored), we continue 627*39513Skarels * the process but don't leave the signal in p_sig, 628*39513Skarels * as it has no further action. If SIGCONT is held, 629*39513Skarels * continue the process and leave the signal in p_sig. 6307421Sroot * If the process catches SIGCONT, let it handle 6317421Sroot * the signal itself. If it isn't waiting on 6327421Sroot * an event, then it goes back to run state. 6337421Sroot * Otherwise, process goes back to sleep state. 6347421Sroot */ 635*39513Skarels p->p_cursig = 0; /* ??? XXX */ 636*39513Skarels if (action == SIG_DFL) 637*39513Skarels p->p_sig &= ~mask; 638*39513Skarels if (action == SIG_CATCH) 639*39513Skarels goto runfast; 640*39513Skarels if (p->p_wchan == 0) 6417421Sroot goto run; 6427421Sroot p->p_stat = SSLEEP; 6437421Sroot goto out; 6447421Sroot 6457421Sroot case SIGSTOP: 6467421Sroot case SIGTSTP: 6477421Sroot case SIGTTIN: 6487421Sroot case SIGTTOU: 6497421Sroot /* 6507421Sroot * Already stopped, don't need to stop again. 6517421Sroot * (If we did the shell could get confused.) 6527421Sroot */ 65317153Sbloom p->p_sig &= ~mask; /* take it away */ 6547421Sroot goto out; 6557421Sroot 6567421Sroot default: 6577421Sroot /* 6587421Sroot * If process is sleeping interruptibly, then 6597421Sroot * unstick it so that when it is continued 6607421Sroot * it can look at the signal. 6617421Sroot * But don't setrun the process as its not to 6627421Sroot * be unstopped by the signal alone. 6637421Sroot */ 6647421Sroot if (p->p_wchan && p->p_pri > PZERO) 6657421Sroot unsleep(p); 6667421Sroot goto out; 6677421Sroot } 6687421Sroot /*NOTREACHED*/ 6697421Sroot 6707421Sroot default: 6717421Sroot /* 6727421Sroot * SRUN, SIDL, SZOMB do nothing with the signal, 6737421Sroot * other than kicking ourselves if we are running. 6747421Sroot * It will either never be noticed, or noticed very soon. 6757421Sroot */ 6767421Sroot if (p == u.u_procp && !noproc) 6777421Sroot aston(); 6787421Sroot goto out; 6797421Sroot } 6807421Sroot /*NOTREACHED*/ 681*39513Skarels 682*39513Skarels runfast: 6837421Sroot /* 6847421Sroot * Raise priority to at least PUSER. 6857421Sroot */ 6867421Sroot if (p->p_pri > PUSER) 68717399Skarels p->p_pri = PUSER; 688*39513Skarels run: 6897421Sroot setrun(p); 6907421Sroot out: 6917421Sroot splx(s); 6927421Sroot } 6937421Sroot 6947421Sroot /* 6957421Sroot * Returns true if the current 6967421Sroot * process has a signal to process. 6977421Sroot * The signal to process is put in p_cursig. 6987421Sroot * This is asked at least once each time a process enters the 6997421Sroot * system (though this can usually be done without actually 7007421Sroot * calling issig by checking the pending signal masks.) 701*39513Skarels * Most signals do not do anything 702*39513Skarels * directly to a process; they set 7037421Sroot * a flag that asks the process to 7047421Sroot * do something to itself. 7057421Sroot */ 7067421Sroot issig() 7077421Sroot { 7087421Sroot register struct proc *p; 709*39513Skarels register int sig, mask; 7107421Sroot 7117421Sroot p = u.u_procp; 7127421Sroot for (;;) { 713*39513Skarels mask = p->p_sig &~ p->p_sigmask; 7147421Sroot if (p->p_flag&SVFORK) 715*39513Skarels mask &= ~stopsigmask; 716*39513Skarels if (mask == 0) 7177421Sroot break; 718*39513Skarels sig = ffs((long)mask); 71917153Sbloom mask = sigmask(sig); 72017153Sbloom p->p_sig &= ~mask; /* take the signal! */ 721*39513Skarels if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) 722*39513Skarels continue; /* only if STRC was on when posted */ 7237421Sroot p->p_cursig = sig; 72412882Ssam if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) { 7257421Sroot /* 7267421Sroot * If traced, always stop, and stay 7277421Sroot * stopped until released by the parent. 7287421Sroot */ 72918331Skarels psignal(p->p_pptr, SIGCHLD); 7307421Sroot do { 7317421Sroot stop(p); 7327421Sroot swtch(); 7337421Sroot } while (!procxmt() && p->p_flag&STRC); 7347421Sroot 7357421Sroot /* 73614782Ssam * If the traced bit got turned off, 73714782Ssam * then put the signal taken above back into p_sig 73814782Ssam * and go back up to the top to rescan signals. 73914782Ssam * This ensures that p_sig* and u_signal are consistent. 7407421Sroot */ 74114782Ssam if ((p->p_flag&STRC) == 0) { 74217153Sbloom p->p_sig |= mask; 7437421Sroot continue; 7447421Sroot } 7457421Sroot 7467421Sroot /* 7477421Sroot * If parent wants us to take the signal, 7487421Sroot * then it will leave it in p->p_cursig; 7497421Sroot * otherwise we just look for signals again. 7507421Sroot */ 7517421Sroot sig = p->p_cursig; 7527421Sroot if (sig == 0) 7537421Sroot continue; 75414782Ssam 75514782Ssam /* 75614782Ssam * If signal is being masked put it back 75714782Ssam * into p_sig and look for other signals. 75814782Ssam */ 75917153Sbloom mask = sigmask(sig); 76017153Sbloom if (p->p_sigmask & mask) { 76117153Sbloom p->p_sig |= mask; 76214782Ssam continue; 76314782Ssam } 7647421Sroot } 76524901Skarels switch ((int)u.u_signal[sig]) { 7667421Sroot 7677421Sroot case SIG_DFL: 7687421Sroot /* 7697421Sroot * Don't take default actions on system processes. 7707421Sroot */ 7717421Sroot if (p->p_ppid == 0) 772*39513Skarels continue; 773*39513Skarels if (mask & stopsigmask) { 7747421Sroot if (p->p_flag&STRC) 7757421Sroot continue; 7767421Sroot stop(p); 777*39513Skarels if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0) 778*39513Skarels psignal(p->p_pptr, SIGCHLD); 7797421Sroot swtch(); 7807421Sroot continue; 781*39513Skarels } else if (mask & defaultignmask) { 7827421Sroot /* 783*39513Skarels * Except for SIGCONT, shouldn't get here. 784*39513Skarels * Default action is to ignore; drop it. 7857421Sroot */ 7867421Sroot continue; /* == ignore */ 787*39513Skarels } else 7887421Sroot goto send; 7897421Sroot /*NOTREACHED*/ 7907421Sroot 7917421Sroot case SIG_IGN: 7927421Sroot /* 793*39513Skarels * Masking above should prevent us ever trying 794*39513Skarels * to take action on an ignored signal other 795*39513Skarels * than SIGCONT, unless process is traced. 7967421Sroot */ 797*39513Skarels if (sig != SIGCONT && (p->p_flag&STRC) == 0) 7987421Sroot printf("issig\n"); 7997421Sroot continue; 8007421Sroot 8017421Sroot default: 8027421Sroot /* 8037421Sroot * This signal has an action, let 8047421Sroot * psig process it. 8057421Sroot */ 8067421Sroot goto send; 8077421Sroot } 8087421Sroot /*NOTREACHED*/ 8097421Sroot } 8107421Sroot /* 8117421Sroot * Didn't find a signal to send. 8127421Sroot */ 8137421Sroot p->p_cursig = 0; 8147421Sroot return (0); 8157421Sroot 8167421Sroot send: 8177421Sroot /* 8187421Sroot * Let psig process the signal. 8197421Sroot */ 8207421Sroot return (sig); 8217421Sroot } 8227421Sroot 8237421Sroot /* 8247421Sroot * Put the argument process into the stopped 82518331Skarels * state and notify the parent via wakeup. 82618331Skarels * Signals are handled elsewhere. 8277421Sroot */ 8287421Sroot stop(p) 8297421Sroot register struct proc *p; 8307421Sroot { 8317421Sroot 8327421Sroot p->p_stat = SSTOP; 8337421Sroot p->p_flag &= ~SWTED; 8347421Sroot wakeup((caddr_t)p->p_pptr); 8357421Sroot } 8367421Sroot 8377421Sroot /* 8387421Sroot * Perform the action specified by 8397421Sroot * the current signal. 8407421Sroot * The usual sequence is: 841*39513Skarels * if (p->p_cursig || ISSIG(p)) 8427421Sroot * psig(); 8437421Sroot * The signal bit has already been cleared by issig, 8447421Sroot * and the current signal number stored in p->p_cursig. 8457421Sroot */ 8467421Sroot psig() 8477421Sroot { 84812882Ssam register struct proc *p = u.u_procp; 849*39513Skarels register int sig; 850*39513Skarels int mask, returnmask; 851*39513Skarels register sig_t action; 8527421Sroot 853*39513Skarels do { 854*39513Skarels sig = p->p_cursig; 855*39513Skarels mask = sigmask(sig); 856*39513Skarels if (sig == 0) 857*39513Skarels panic("psig"); 858*39513Skarels action = u.u_signal[sig]; 859*39513Skarels if (action != SIG_DFL) { 860*39513Skarels #ifdef DIAGNOSTIC 861*39513Skarels if (action == SIG_IGN || (p->p_sigmask & mask)) 862*39513Skarels panic("psig action"); 863*39513Skarels #endif 864*39513Skarels u.u_error = 0; 865*39513Skarels /* 866*39513Skarels * Set the new mask value and also defer further 867*39513Skarels * occurences of this signal. 868*39513Skarels * 869*39513Skarels * Special case: user has done a sigpause. Here the 870*39513Skarels * current mask is not of interest, but rather the 871*39513Skarels * mask from before the sigpause is what we want 872*39513Skarels * restored after the signal processing is completed. 873*39513Skarels */ 874*39513Skarels (void) splhigh(); 875*39513Skarels if (p->p_flag & SOMASK) { 876*39513Skarels returnmask = u.u_oldmask; 877*39513Skarels p->p_flag &= ~SOMASK; 878*39513Skarels } else 879*39513Skarels returnmask = p->p_sigmask; 880*39513Skarels p->p_sigmask |= u.u_sigmask[sig] | mask; 881*39513Skarels (void) spl0(); 882*39513Skarels u.u_ru.ru_nsignals++; 883*39513Skarels sendsig(action, sig, returnmask, 0); 884*39513Skarels p->p_cursig = 0; 885*39513Skarels continue; 8867421Sroot } 887*39513Skarels u.u_acflag |= AXSIG; 888*39513Skarels switch (sig) { 8897421Sroot 890*39513Skarels case SIGILL: 891*39513Skarels case SIGIOT: 892*39513Skarels case SIGBUS: 893*39513Skarels case SIGQUIT: 894*39513Skarels case SIGTRAP: 895*39513Skarels case SIGEMT: 896*39513Skarels case SIGFPE: 897*39513Skarels case SIGSEGV: 898*39513Skarels case SIGSYS: 899*39513Skarels u.u_arg[0] = sig; 900*39513Skarels if (core() == 0) 901*39513Skarels sig |= WCOREFLAG; 902*39513Skarels } 903*39513Skarels exit(W_EXITCODE(0, sig)); 904*39513Skarels /* NOTREACHED */ 905*39513Skarels } while (ISSIG(p)); 9067421Sroot } 9077421Sroot 9087421Sroot /* 909*39513Skarels * Create a core image on the file "core". 9107421Sroot * It writes UPAGES block of the 9117421Sroot * user.h area followed by the entire 9127421Sroot * data+stack segments. 9137421Sroot */ 9147421Sroot core() 9157421Sroot { 91637728Smckusick register struct vnode *vp; 917*39513Skarels register struct proc *p = u.u_procp; 91816692Smckusick register struct nameidata *ndp = &u.u_nd; 91937580Smckusick struct vattr vattr; 92037580Smckusick int error; 9217421Sroot 922*39513Skarels if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid) 92337580Smckusick return (EFAULT); 92437580Smckusick if (ctob(UPAGES + u.u_dsize + u.u_ssize) >= 9258032Sroot u.u_rlimit[RLIMIT_CORE].rlim_cur) 92637580Smckusick return (EFAULT); 927*39513Skarels if (p->p_textp) { 928*39513Skarels VOP_LOCK(p->p_textp->x_vptr); 929*39513Skarels error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred); 930*39513Skarels VOP_UNLOCK(p->p_textp->x_vptr); 93137580Smckusick if (error) 93237580Smckusick return (EFAULT); 93337580Smckusick } 93416692Smckusick ndp->ni_segflg = UIO_SYSSPACE; 93516692Smckusick ndp->ni_dirp = "core"; 93637580Smckusick if (error = vn_open(ndp, FCREAT|FWRITE, 0644)) 93737580Smckusick return (error); 93837580Smckusick vp = ndp->ni_vp; 93938394Smckusick VOP_LOCK(vp); 94037580Smckusick if (vp->v_type != VREG || 94137728Smckusick VOP_GETATTR(vp, &vattr, u.u_cred) || 94237580Smckusick vattr.va_nlink != 1) { 94338394Smckusick vput(vp); 94438394Smckusick return (EFAULT); 9457818Sroot } 94630290Ssam #ifdef MMAP 94730290Ssam { register int fd; 94838394Smckusick /* unmap funky devices in the user's address space */ 94930290Ssam for (fd = 0; fd < u.u_lastfile; fd++) 95030290Ssam if (u.u_ofile[fd] && (u.u_pofile[fd] & UF_MAPPED)) 95130290Ssam munmapfd(fd); 95230290Ssam } 95330290Ssam #endif 95437580Smckusick vattr_null(&vattr); 95537580Smckusick vattr.va_size = 0; 95637728Smckusick VOP_SETATTR(vp, &vattr, u.u_cred); 9577818Sroot u.u_acflag |= ACORE; 95837580Smckusick error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0, 95938394Smckusick UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 96037580Smckusick if (error == 0) 96137580Smckusick error = vn_rdwr(UIO_WRITE, vp, 962*39513Skarels (caddr_t)ctob(dptov(p, 0)), 96338394Smckusick (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE, 96438394Smckusick IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 96537580Smckusick if (error == 0) 96637580Smckusick error = vn_rdwr(UIO_WRITE, vp, 967*39513Skarels (caddr_t)ctob(sptov(p, u.u_ssize - 1)), 96826354Skarels (int)ctob(u.u_ssize), 96938394Smckusick (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE, 97038394Smckusick IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0); 97138394Smckusick vput(vp); 97237580Smckusick return (error); 9737421Sroot } 974*39513Skarels 975*39513Skarels /* 976*39513Skarels * Nonexistent system call-- signal process (may want to handle it). 977*39513Skarels * Flag error in case process won't see signal immediately (blocked or ignored). 978*39513Skarels */ 979*39513Skarels nosys() 980*39513Skarels { 981*39513Skarels 982*39513Skarels psignal(u.u_procp, SIGSYS); 983*39513Skarels u.u_error = EINVAL; 984*39513Skarels } 985