1*18331Skarels /* kern_sig.c 6.12 85/03/13 */ 27421Sroot 39755Ssam #include "../machine/reg.h" 49755Ssam #include "../machine/pte.h" 59755Ssam #include "../machine/psl.h" 69755Ssam 717092Sbloom #include "param.h" 817092Sbloom #include "systm.h" 917092Sbloom #include "dir.h" 1017092Sbloom #include "user.h" 1117092Sbloom #include "inode.h" 1217092Sbloom #include "proc.h" 1317092Sbloom #include "timeb.h" 1417092Sbloom #include "times.h" 1517092Sbloom #include "conf.h" 1617092Sbloom #include "buf.h" 1717092Sbloom #include "mount.h" 1817092Sbloom #include "text.h" 1917092Sbloom #include "seg.h" 2017092Sbloom #include "vm.h" 2117092Sbloom #include "acct.h" 2217092Sbloom #include "uio.h" 2317092Sbloom #include "kernel.h" 247421Sroot 2517153Sbloom #define cantmask (sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP)) 2612951Ssam 2717013Smckusick /* 2817013Smckusick * Generalized interface signal handler. 2917013Smckusick */ 307499Sroot sigvec() 317421Sroot { 3212951Ssam register struct a { 3312882Ssam int signo; 3412951Ssam struct sigvec *nsv; 3512951Ssam struct sigvec *osv; 3612882Ssam } *uap = (struct a *)u.u_ap; 3712951Ssam struct sigvec vec; 3812951Ssam register struct sigvec *sv; 3912882Ssam register int sig; 4018308Smckusick int bit; 417421Sroot 4212882Ssam sig = uap->signo; 4312951Ssam if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) { 4412882Ssam u.u_error = EINVAL; 4512882Ssam return; 4612882Ssam } 4712951Ssam sv = &vec; 4812951Ssam if (uap->osv) { 4912951Ssam sv->sv_handler = u.u_signal[sig]; 5012951Ssam sv->sv_mask = u.u_sigmask[sig]; 5118308Smckusick bit = sigmask(sig); 5218308Smckusick sv->sv_flags = 0; 5318308Smckusick if ((u.u_sigonstack & bit) != 0) 5418308Smckusick sv->sv_flags |= SV_ONSTACK; 5518308Smckusick if ((u.u_sigintr & bit) != 0) 5618308Smckusick sv->sv_flags |= SV_INTERRUPT; 5712951Ssam u.u_error = 5812951Ssam copyout((caddr_t)sv, (caddr_t)uap->osv, sizeof (vec)); 5912951Ssam if (u.u_error) 6012951Ssam return; 6112951Ssam } 6212951Ssam if (uap->nsv) { 6312951Ssam u.u_error = 6412951Ssam copyin((caddr_t)uap->nsv, (caddr_t)sv, sizeof (vec)); 6512951Ssam if (u.u_error) 6612951Ssam return; 6712951Ssam if (sig == SIGCONT && sv->sv_handler == SIG_IGN) { 6812951Ssam u.u_error = EINVAL; 6912951Ssam return; 7012951Ssam } 7112951Ssam setsigvec(sig, sv); 7212951Ssam } 737421Sroot } 747421Sroot 7512951Ssam setsigvec(sig, sv) 7612951Ssam int sig; 7712951Ssam register struct sigvec *sv; 7812882Ssam { 7912882Ssam register struct proc *p; 8012951Ssam register int bit; 8112882Ssam 8217153Sbloom bit = sigmask(sig); 8312882Ssam p = u.u_procp; 8412882Ssam /* 8512882Ssam * Change setting atomically. 8612882Ssam */ 8717153Sbloom (void) splhigh(); 8812951Ssam u.u_signal[sig] = sv->sv_handler; 8912951Ssam u.u_sigmask[sig] = sv->sv_mask &~ cantmask; 9018308Smckusick if (sv->sv_flags & SV_INTERRUPT) 9118308Smckusick u.u_sigintr |= bit; 9218308Smckusick else 9318308Smckusick u.u_sigintr &= ~bit; 9418308Smckusick if (sv->sv_flags & SV_ONSTACK) 9512951Ssam u.u_sigonstack |= bit; 9612951Ssam else 9712951Ssam u.u_sigonstack &= ~bit; 9812951Ssam if (sv->sv_handler == SIG_IGN) { 9912951Ssam p->p_sig &= ~bit; /* never to be seen again */ 10012951Ssam p->p_sigignore |= bit; 10112951Ssam p->p_sigcatch &= ~bit; 10212882Ssam } else { 10312951Ssam p->p_sigignore &= ~bit; 10412951Ssam if (sv->sv_handler == SIG_DFL) 10512951Ssam p->p_sigcatch &= ~bit; 10612882Ssam else 10712951Ssam p->p_sigcatch |= bit; 10812882Ssam } 10912882Ssam (void) spl0(); 11012882Ssam } 11112882Ssam 1127499Sroot sigblock() 1137421Sroot { 11412882Ssam struct a { 11517153Sbloom int mask; 11612882Ssam } *uap = (struct a *)u.u_ap; 11712951Ssam register struct proc *p = u.u_procp; 1187499Sroot 11917153Sbloom (void) splhigh(); 12012882Ssam u.u_r.r_val1 = p->p_sigmask; 12117153Sbloom p->p_sigmask |= uap->mask &~ cantmask; 12212882Ssam (void) spl0(); 1237499Sroot } 1247499Sroot 1257499Sroot sigsetmask() 1267499Sroot { 12712882Ssam struct a { 12817153Sbloom int mask; 12912882Ssam } *uap = (struct a *)u.u_ap; 13012882Ssam register struct proc *p = u.u_procp; 1317499Sroot 13217153Sbloom (void) splhigh(); 13312882Ssam u.u_r.r_val1 = p->p_sigmask; 13417153Sbloom p->p_sigmask = uap->mask &~ cantmask; 13512882Ssam (void) spl0(); 1367499Sroot } 1377499Sroot 1387499Sroot sigpause() 1397499Sroot { 14012882Ssam struct a { 14117153Sbloom int mask; 14212882Ssam } *uap = (struct a *)u.u_ap; 14312882Ssam register struct proc *p = u.u_procp; 1447499Sroot 14512882Ssam /* 14612882Ssam * When returning from sigpause, we want 14712882Ssam * the old mask to be restored after the 14812882Ssam * signal handler has finished. Thus, we 14912882Ssam * save it here and mark the proc structure 15012882Ssam * to indicate this (should be in u.). 15112882Ssam */ 15212882Ssam u.u_oldmask = p->p_sigmask; 15312882Ssam p->p_flag |= SOMASK; 15417153Sbloom p->p_sigmask = uap->mask &~ cantmask; 15512882Ssam for (;;) 15612882Ssam sleep((caddr_t)&u, PSLEP); 15712882Ssam /*NOTREACHED*/ 1587499Sroot } 15912951Ssam #undef cantmask 1607499Sroot 1617499Sroot sigstack() 1627499Sroot { 16312951Ssam register struct a { 16412951Ssam struct sigstack *nss; 16512951Ssam struct sigstack *oss; 16612882Ssam } *uap = (struct a *)u.u_ap; 16712951Ssam struct sigstack ss; 1687499Sroot 16912951Ssam if (uap->oss) { 17012951Ssam u.u_error = copyout((caddr_t)&u.u_sigstack, (caddr_t)uap->oss, 17112951Ssam sizeof (struct sigstack)); 17212951Ssam if (u.u_error) 17312951Ssam return; 17412951Ssam } 17512951Ssam if (uap->nss) { 17612951Ssam u.u_error = 17712951Ssam copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss)); 17812951Ssam if (u.u_error == 0) 17912951Ssam u.u_sigstack = ss; 18012951Ssam } 1817499Sroot } 1827499Sroot 18313227Ssam /* KILL SHOULD BE UPDATED */ 18413227Ssam 1858032Sroot kill() 1868032Sroot { 18712882Ssam register struct a { 18812882Ssam int pid; 18912882Ssam int signo; 19012882Ssam } *uap = (struct a *)u.u_ap; 1918032Sroot 19213227Ssam u.u_error = kill1(uap->signo < 0, 19313227Ssam uap->signo < 0 ? -uap->signo : uap->signo, uap->pid); 1948032Sroot } 1958032Sroot 1968032Sroot killpg() 1978032Sroot { 1989989Ssam register struct a { 1999989Ssam int pgrp; 2009989Ssam int signo; 2019989Ssam } *uap = (struct a *)u.u_ap; 2028032Sroot 20312750Ssam u.u_error = kill1(1, uap->signo, uap->pgrp); 2048032Sroot } 2058032Sroot 20612882Ssam /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */ 20712882Ssam 20812750Ssam kill1(ispgrp, signo, who) 2099989Ssam int ispgrp, signo, who; 2109989Ssam { 2119989Ssam register struct proc *p; 2129989Ssam int f, priv = 0; 2139989Ssam 21412835Ssam if (signo < 0 || signo > NSIG) 2159989Ssam return (EINVAL); 2169989Ssam if (who > 0 && !ispgrp) { 2179989Ssam p = pfind(who); 21814926Smckusick if (p == 0) 2199989Ssam return (ESRCH); 22014926Smckusick if (u.u_uid && u.u_uid != p->p_uid) 22114926Smckusick return (EPERM); 22212835Ssam if (signo) 22312835Ssam psignal(p, signo); 2249989Ssam return (0); 2257421Sroot } 2269989Ssam if (who == -1 && u.u_uid == 0) 2279989Ssam priv++, who = 0, ispgrp = 1; /* like sending to pgrp */ 2289989Ssam else if (who == 0) { 2297421Sroot /* 2307421Sroot * Zero process id means send to my process group. 2317421Sroot */ 2329989Ssam ispgrp = 1; 2339989Ssam who = u.u_procp->p_pgrp; 2349989Ssam if (who == 0) 2359989Ssam return (EINVAL); 2367421Sroot } 23716531Skarels for (f = 0, p = allproc; p != NULL; p = p->p_nxt) { 2389989Ssam if (!ispgrp) { 2399989Ssam if (p->p_pid != who) 2407421Sroot continue; 2419989Ssam } else if (p->p_pgrp != who && priv == 0 || p->p_ppid == 0 || 2429989Ssam (p->p_flag&SSYS) || (priv && p == u.u_procp)) 2437421Sroot continue; 2447421Sroot if (u.u_uid != 0 && u.u_uid != p->p_uid && 2459989Ssam (signo != SIGCONT || !inferior(p))) 2467421Sroot continue; 2477421Sroot f++; 24812835Ssam if (signo) 24912835Ssam psignal(p, signo); 2507421Sroot } 25112750Ssam return (f == 0 ? ESRCH : 0); 2527421Sroot } 2537421Sroot 2547421Sroot /* 2557421Sroot * Send the specified signal to 2567421Sroot * all processes with 'pgrp' as 2577421Sroot * process group. 2587421Sroot */ 2597421Sroot gsignal(pgrp, sig) 2607421Sroot register int pgrp; 2617421Sroot { 2627421Sroot register struct proc *p; 2637421Sroot 2647421Sroot if (pgrp == 0) 2657421Sroot return; 26616531Skarels for (p = allproc; p != NULL; p = p->p_nxt) 2677421Sroot if (p->p_pgrp == pgrp) 2687421Sroot psignal(p, sig); 2697421Sroot } 2707421Sroot 2717421Sroot /* 2727421Sroot * Send the specified signal to 2737421Sroot * the specified process. 2747421Sroot */ 2757421Sroot psignal(p, sig) 2767421Sroot register struct proc *p; 2777421Sroot register int sig; 2787421Sroot { 2797421Sroot register int s; 2807421Sroot register int (*action)(); 28117153Sbloom int mask; 2827421Sroot 2837421Sroot if ((unsigned)sig >= NSIG) 2847421Sroot return; 28517153Sbloom mask = sigmask(sig); 2867421Sroot 2877421Sroot /* 2887421Sroot * If proc is traced, always give parent a chance. 2897421Sroot */ 2907421Sroot if (p->p_flag & STRC) 2917421Sroot action = SIG_DFL; 2927421Sroot else { 2937421Sroot /* 29412882Ssam * If the signal is being ignored, 29512882Ssam * then we forget about it immediately. 2967421Sroot */ 29717153Sbloom if (p->p_sigignore & mask) 2987421Sroot return; 29917153Sbloom if (p->p_sigmask & mask) 30012882Ssam action = SIG_HOLD; 30117153Sbloom else if (p->p_sigcatch & mask) 30212882Ssam action = SIG_CATCH; 30312882Ssam else 30412882Ssam action = SIG_DFL; 3057421Sroot } 30617153Sbloom #define stops (sigmask(SIGSTOP)|sigmask(SIGTSTP)| \ 30717153Sbloom sigmask(SIGTTIN)|sigmask(SIGTTOU)) 3087421Sroot if (sig) { 30917153Sbloom p->p_sig |= mask; 3107421Sroot switch (sig) { 3117421Sroot 3127421Sroot case SIGTERM: 31312882Ssam if ((p->p_flag&STRC) || action != SIG_DFL) 3147421Sroot break; 3157421Sroot /* fall into ... */ 3167421Sroot 3177421Sroot case SIGKILL: 3187421Sroot if (p->p_nice > NZERO) 3197421Sroot p->p_nice = NZERO; 3207421Sroot break; 3217421Sroot 3227421Sroot case SIGCONT: 3237421Sroot p->p_sig &= ~stops; 3247421Sroot break; 3257421Sroot 3267421Sroot case SIGSTOP: 3277421Sroot case SIGTSTP: 3287421Sroot case SIGTTIN: 3297421Sroot case SIGTTOU: 33017153Sbloom p->p_sig &= ~sigmask(SIGCONT); 3317421Sroot break; 3327421Sroot } 3337421Sroot } 3347421Sroot #undef stops 3357421Sroot /* 3367421Sroot * Defer further processing for signals which are held. 3377421Sroot */ 3387421Sroot if (action == SIG_HOLD) 3397421Sroot return; 34017153Sbloom s = splhigh(); 3417421Sroot switch (p->p_stat) { 3427421Sroot 3437421Sroot case SSLEEP: 3447421Sroot /* 3457421Sroot * If process is sleeping at negative priority 3467421Sroot * we can't interrupt the sleep... the signal will 3477421Sroot * be noticed when the process returns through 3487421Sroot * trap() or syscall(). 3497421Sroot */ 3507421Sroot if (p->p_pri <= PZERO) 3517421Sroot goto out; 3527421Sroot /* 3537421Sroot * Process is sleeping and traced... make it runnable 3547421Sroot * so it can discover the signal in issig() and stop 3557421Sroot * for the parent. 3567421Sroot */ 3577421Sroot if (p->p_flag&STRC) 3587421Sroot goto run; 3597421Sroot switch (sig) { 3607421Sroot 3617421Sroot case SIGSTOP: 3627421Sroot case SIGTSTP: 3637421Sroot case SIGTTIN: 3647421Sroot case SIGTTOU: 3657421Sroot /* 3667421Sroot * These are the signals which by default 3677421Sroot * stop a process. 3687421Sroot */ 3697421Sroot if (action != SIG_DFL) 3707421Sroot goto run; 3717421Sroot /* 3727421Sroot * Don't clog system with children of init 3737421Sroot * stopped from the keyboard. 3747421Sroot */ 3757421Sroot if (sig != SIGSTOP && p->p_pptr == &proc[1]) { 3767421Sroot psignal(p, SIGKILL); 37717153Sbloom p->p_sig &= ~mask; 3787421Sroot splx(s); 3797421Sroot return; 3807421Sroot } 3817421Sroot /* 3827421Sroot * If a child in vfork(), stopping could 3837421Sroot * cause deadlock. 3847421Sroot */ 3857421Sroot if (p->p_flag&SVFORK) 3867421Sroot goto out; 38717153Sbloom p->p_sig &= ~mask; 3887421Sroot p->p_cursig = sig; 389*18331Skarels psignal(p->p_pptr, SIGCHLD); 3907421Sroot stop(p); 3917421Sroot goto out; 3927421Sroot 3937421Sroot case SIGIO: 3947421Sroot case SIGURG: 3957421Sroot case SIGCHLD: 39617597Sbloom case SIGWINCH: 3977421Sroot /* 3987421Sroot * These signals are special in that they 3997421Sroot * don't get propogated... if the process 4007421Sroot * isn't interested, forget it. 4017421Sroot */ 4027421Sroot if (action != SIG_DFL) 4037421Sroot goto run; 40417153Sbloom p->p_sig &= ~mask; /* take it away */ 4057421Sroot goto out; 4067421Sroot 4077421Sroot default: 4087421Sroot /* 4097421Sroot * All other signals cause the process to run 4107421Sroot */ 4117421Sroot goto run; 4127421Sroot } 4137421Sroot /*NOTREACHED*/ 4147421Sroot 4157421Sroot case SSTOP: 4167421Sroot /* 4177421Sroot * If traced process is already stopped, 4187421Sroot * then no further action is necessary. 4197421Sroot */ 4207421Sroot if (p->p_flag&STRC) 4217421Sroot goto out; 4227421Sroot switch (sig) { 4237421Sroot 4247421Sroot case SIGKILL: 4257421Sroot /* 4267421Sroot * Kill signal always sets processes running. 4277421Sroot */ 4287421Sroot goto run; 4297421Sroot 4307421Sroot case SIGCONT: 4317421Sroot /* 4327421Sroot * If the process catches SIGCONT, let it handle 4337421Sroot * the signal itself. If it isn't waiting on 4347421Sroot * an event, then it goes back to run state. 4357421Sroot * Otherwise, process goes back to sleep state. 4367421Sroot */ 4377421Sroot if (action != SIG_DFL || p->p_wchan == 0) 4387421Sroot goto run; 4397421Sroot p->p_stat = SSLEEP; 4407421Sroot goto out; 4417421Sroot 4427421Sroot case SIGSTOP: 4437421Sroot case SIGTSTP: 4447421Sroot case SIGTTIN: 4457421Sroot case SIGTTOU: 4467421Sroot /* 4477421Sroot * Already stopped, don't need to stop again. 4487421Sroot * (If we did the shell could get confused.) 4497421Sroot */ 45017153Sbloom p->p_sig &= ~mask; /* take it away */ 4517421Sroot goto out; 4527421Sroot 4537421Sroot default: 4547421Sroot /* 4557421Sroot * If process is sleeping interruptibly, then 4567421Sroot * unstick it so that when it is continued 4577421Sroot * it can look at the signal. 4587421Sroot * But don't setrun the process as its not to 4597421Sroot * be unstopped by the signal alone. 4607421Sroot */ 4617421Sroot if (p->p_wchan && p->p_pri > PZERO) 4627421Sroot unsleep(p); 4637421Sroot goto out; 4647421Sroot } 4657421Sroot /*NOTREACHED*/ 4667421Sroot 4677421Sroot default: 4687421Sroot /* 4697421Sroot * SRUN, SIDL, SZOMB do nothing with the signal, 4707421Sroot * other than kicking ourselves if we are running. 4717421Sroot * It will either never be noticed, or noticed very soon. 4727421Sroot */ 4737421Sroot if (p == u.u_procp && !noproc) 4748444Sroot #include "../vax/mtpr.h" 4757421Sroot aston(); 4767421Sroot goto out; 4777421Sroot } 4787421Sroot /*NOTREACHED*/ 4797421Sroot run: 4807421Sroot /* 4817421Sroot * Raise priority to at least PUSER. 4827421Sroot */ 4837421Sroot if (p->p_pri > PUSER) 48417399Skarels p->p_pri = PUSER; 4857421Sroot setrun(p); 4867421Sroot out: 4877421Sroot splx(s); 4887421Sroot } 4897421Sroot 4907421Sroot /* 4917421Sroot * Returns true if the current 4927421Sroot * process has a signal to process. 4937421Sroot * The signal to process is put in p_cursig. 4947421Sroot * This is asked at least once each time a process enters the 4957421Sroot * system (though this can usually be done without actually 4967421Sroot * calling issig by checking the pending signal masks.) 4977421Sroot * A signal does not do anything 4987421Sroot * directly to a process; it sets 4997421Sroot * a flag that asks the process to 5007421Sroot * do something to itself. 5017421Sroot */ 5027421Sroot issig() 5037421Sroot { 5047421Sroot register struct proc *p; 5057421Sroot register int sig; 50617153Sbloom int sigbits, mask; 5077421Sroot 5087421Sroot p = u.u_procp; 5097421Sroot for (;;) { 51014782Ssam sigbits = p->p_sig &~ p->p_sigmask; 5117421Sroot if ((p->p_flag&STRC) == 0) 51214782Ssam sigbits &= ~p->p_sigignore; 5137421Sroot if (p->p_flag&SVFORK) 5147421Sroot #define bit(a) (1<<(a-1)) 5157421Sroot sigbits &= ~(bit(SIGSTOP)|bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU)); 5167421Sroot if (sigbits == 0) 5177421Sroot break; 51812882Ssam sig = ffs(sigbits); 51917153Sbloom mask = sigmask(sig); 52017153Sbloom p->p_sig &= ~mask; /* take the signal! */ 5217421Sroot p->p_cursig = sig; 52212882Ssam if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) { 5237421Sroot /* 5247421Sroot * If traced, always stop, and stay 5257421Sroot * stopped until released by the parent. 5267421Sroot */ 527*18331Skarels psignal(p->p_pptr, SIGCHLD); 5287421Sroot do { 5297421Sroot stop(p); 5307421Sroot swtch(); 5317421Sroot } while (!procxmt() && p->p_flag&STRC); 5327421Sroot 5337421Sroot /* 53414782Ssam * If the traced bit got turned off, 53514782Ssam * then put the signal taken above back into p_sig 53614782Ssam * and go back up to the top to rescan signals. 53714782Ssam * This ensures that p_sig* and u_signal are consistent. 5387421Sroot */ 53914782Ssam if ((p->p_flag&STRC) == 0) { 54017153Sbloom p->p_sig |= mask; 5417421Sroot continue; 5427421Sroot } 5437421Sroot 5447421Sroot /* 5457421Sroot * If parent wants us to take the signal, 5467421Sroot * then it will leave it in p->p_cursig; 5477421Sroot * otherwise we just look for signals again. 5487421Sroot */ 5497421Sroot sig = p->p_cursig; 5507421Sroot if (sig == 0) 5517421Sroot continue; 55214782Ssam 55314782Ssam /* 55414782Ssam * If signal is being masked put it back 55514782Ssam * into p_sig and look for other signals. 55614782Ssam */ 55717153Sbloom mask = sigmask(sig); 55817153Sbloom if (p->p_sigmask & mask) { 55917153Sbloom p->p_sig |= mask; 56014782Ssam continue; 56114782Ssam } 5627421Sroot } 5637421Sroot switch (u.u_signal[sig]) { 5647421Sroot 5657421Sroot case SIG_DFL: 5667421Sroot /* 5677421Sroot * Don't take default actions on system processes. 5687421Sroot */ 5697421Sroot if (p->p_ppid == 0) 5707421Sroot break; 5717421Sroot switch (sig) { 5727421Sroot 5737421Sroot case SIGTSTP: 5747421Sroot case SIGTTIN: 5757421Sroot case SIGTTOU: 5767421Sroot /* 5777421Sroot * Children of init aren't allowed to stop 5787421Sroot * on signals from the keyboard. 5797421Sroot */ 5807421Sroot if (p->p_pptr == &proc[1]) { 5817421Sroot psignal(p, SIGKILL); 5827421Sroot continue; 5837421Sroot } 5847421Sroot /* fall into ... */ 5857421Sroot 5867421Sroot case SIGSTOP: 5877421Sroot if (p->p_flag&STRC) 5887421Sroot continue; 589*18331Skarels psignal(p->p_pptr, SIGCHLD); 5907421Sroot stop(p); 5917421Sroot swtch(); 5927421Sroot continue; 5937421Sroot 5947421Sroot case SIGCONT: 5957421Sroot case SIGCHLD: 59612882Ssam case SIGURG: 59712951Ssam case SIGIO: 59817597Sbloom case SIGWINCH: 5997421Sroot /* 6007421Sroot * These signals are normally not 6017421Sroot * sent if the action is the default. 6027421Sroot */ 6037421Sroot continue; /* == ignore */ 6047421Sroot 6057421Sroot default: 6067421Sroot goto send; 6077421Sroot } 6087421Sroot /*NOTREACHED*/ 6097421Sroot 6107421Sroot case SIG_HOLD: 6117421Sroot case SIG_IGN: 6127421Sroot /* 6137421Sroot * Masking above should prevent us 6147421Sroot * ever trying to take action on a held 6157421Sroot * or ignored signal, unless process is traced. 6167421Sroot */ 6177421Sroot if ((p->p_flag&STRC) == 0) 6187421Sroot printf("issig\n"); 6197421Sroot continue; 6207421Sroot 6217421Sroot default: 6227421Sroot /* 6237421Sroot * This signal has an action, let 6247421Sroot * psig process it. 6257421Sroot */ 6267421Sroot goto send; 6277421Sroot } 6287421Sroot /*NOTREACHED*/ 6297421Sroot } 6307421Sroot /* 6317421Sroot * Didn't find a signal to send. 6327421Sroot */ 6337421Sroot p->p_cursig = 0; 6347421Sroot return (0); 6357421Sroot 6367421Sroot send: 6377421Sroot /* 6387421Sroot * Let psig process the signal. 6397421Sroot */ 6407421Sroot return (sig); 6417421Sroot } 6427421Sroot 6437421Sroot /* 6447421Sroot * Put the argument process into the stopped 645*18331Skarels * state and notify the parent via wakeup. 646*18331Skarels * Signals are handled elsewhere. 6477421Sroot */ 6487421Sroot stop(p) 6497421Sroot register struct proc *p; 6507421Sroot { 6517421Sroot 6527421Sroot p->p_stat = SSTOP; 6537421Sroot p->p_flag &= ~SWTED; 6547421Sroot wakeup((caddr_t)p->p_pptr); 6557421Sroot } 6567421Sroot 6577421Sroot /* 6587421Sroot * Perform the action specified by 6597421Sroot * the current signal. 6607421Sroot * The usual sequence is: 6617421Sroot * if (issig()) 6627421Sroot * psig(); 6637421Sroot * The signal bit has already been cleared by issig, 6647421Sroot * and the current signal number stored in p->p_cursig. 6657421Sroot */ 6667421Sroot psig() 6677421Sroot { 66812882Ssam register struct proc *p = u.u_procp; 66912882Ssam register int sig = p->p_cursig; 67017153Sbloom int mask = sigmask(sig), returnmask; 6717421Sroot register int (*action)(); 6727421Sroot 67312882Ssam if (sig == 0) 6747421Sroot panic("psig"); 67512882Ssam action = u.u_signal[sig]; 6767421Sroot if (action != SIG_DFL) { 67717153Sbloom if (action == SIG_IGN || (p->p_sigmask & mask)) 6787421Sroot panic("psig action"); 6797421Sroot u.u_error = 0; 6807421Sroot /* 68112882Ssam * Set the new mask value and also defer further 68212882Ssam * occurences of this signal (unless we're simulating 68312882Ssam * the old signal facilities). 68412882Ssam * 68512882Ssam * Special case: user has done a sigpause. Here the 68612882Ssam * current mask is not of interest, but rather the 68712882Ssam * mask from before the sigpause is what we want restored 68812882Ssam * after the signal processing is completed. 6897421Sroot */ 69017153Sbloom (void) splhigh(); 69112882Ssam if (p->p_flag & SOUSIG) { 69212882Ssam if (sig != SIGILL && sig != SIGTRAP) { 69312882Ssam u.u_signal[sig] = SIG_DFL; 69417153Sbloom p->p_sigcatch &= ~mask; 69512882Ssam } 69617153Sbloom mask = 0; 6977421Sroot } 69812882Ssam if (p->p_flag & SOMASK) { 69912882Ssam returnmask = u.u_oldmask; 70012882Ssam p->p_flag &= ~SOMASK; 70112882Ssam } else 70212882Ssam returnmask = p->p_sigmask; 70317153Sbloom p->p_sigmask |= u.u_sigmask[sig] | mask; 70412882Ssam (void) spl0(); 7058032Sroot u.u_ru.ru_nsignals++; 70612882Ssam sendsig(action, sig, returnmask); 70712882Ssam p->p_cursig = 0; 7087421Sroot return; 7097421Sroot } 7107421Sroot u.u_acflag |= AXSIG; 71112882Ssam switch (sig) { 7127421Sroot 7137421Sroot case SIGILL: 7147421Sroot case SIGIOT: 7157421Sroot case SIGBUS: 7167421Sroot case SIGQUIT: 7177421Sroot case SIGTRAP: 7187421Sroot case SIGEMT: 7197421Sroot case SIGFPE: 7207421Sroot case SIGSEGV: 7217421Sroot case SIGSYS: 72212882Ssam u.u_arg[0] = sig; 7237421Sroot if (core()) 72412882Ssam sig += 0200; 7257421Sroot } 72612882Ssam exit(sig); 7277421Sroot } 7287421Sroot 7297421Sroot /* 7307421Sroot * Create a core image on the file "core" 7317421Sroot * If you are looking for protection glitches, 7327421Sroot * there are probably a wealth of them here 7337421Sroot * when this occurs to a suid command. 7347421Sroot * 7357421Sroot * It writes UPAGES block of the 7367421Sroot * user.h area followed by the entire 7377421Sroot * data+stack segments. 7387421Sroot */ 7397421Sroot core() 7407421Sroot { 7417421Sroot register struct inode *ip; 74216692Smckusick register struct nameidata *ndp = &u.u_nd; 7437421Sroot 74412639Ssam if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid) 7457818Sroot return (0); 7468032Sroot if (ctob(UPAGES+u.u_dsize+u.u_ssize) >= 7478032Sroot u.u_rlimit[RLIMIT_CORE].rlim_cur) 7487421Sroot return (0); 7497421Sroot u.u_error = 0; 75016692Smckusick ndp->ni_nameiop = CREATE | FOLLOW; 75116692Smckusick ndp->ni_segflg = UIO_SYSSPACE; 75216692Smckusick ndp->ni_dirp = "core"; 75316692Smckusick ip = namei(ndp); 7547421Sroot if (ip == NULL) { 7557421Sroot if (u.u_error) 7567421Sroot return (0); 75716692Smckusick ip = maknode(0644, ndp); 7587421Sroot if (ip==NULL) 7597421Sroot return (0); 7607421Sroot } 7617818Sroot if (access(ip, IWRITE) || 7627818Sroot (ip->i_mode&IFMT) != IFREG || 7637818Sroot ip->i_nlink != 1) { 7647421Sroot u.u_error = EFAULT; 7657818Sroot goto out; 7667818Sroot } 7679160Ssam itrunc(ip, (u_long)0); 7687818Sroot u.u_acflag |= ACORE; 76912882Ssam u.u_error = rdwri(UIO_WRITE, ip, 77012882Ssam (caddr_t)&u, 77112882Ssam ctob(UPAGES), 77212882Ssam 0, 1, (int *)0); 7738101Sroot if (u.u_error == 0) 7748644Sroot u.u_error = rdwri(UIO_WRITE, ip, 7758967Sroot (caddr_t)ctob(dptov(u.u_procp, 0)), 7768967Sroot ctob(u.u_dsize), 7778644Sroot ctob(UPAGES), 0, (int *)0); 7788101Sroot if (u.u_error == 0) 7798644Sroot u.u_error = rdwri(UIO_WRITE, ip, 7808967Sroot (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)), 7818967Sroot ctob(u.u_ssize), 7828644Sroot ctob(UPAGES)+ctob(u.u_dsize), 0, (int *)0); 7837818Sroot out: 7847421Sroot iput(ip); 7857818Sroot return (u.u_error == 0); 7867421Sroot } 787