1*13227Ssam /* kern_sig.c 5.22 83/06/21 */ 27421Sroot 39755Ssam #include "../machine/reg.h" 49755Ssam #include "../machine/pte.h" 59755Ssam #include "../machine/psl.h" 69755Ssam 77421Sroot #include "../h/param.h" 87421Sroot #include "../h/systm.h" 97421Sroot #include "../h/dir.h" 107421Sroot #include "../h/user.h" 117421Sroot #include "../h/inode.h" 127421Sroot #include "../h/proc.h" 137421Sroot #include "../h/timeb.h" 147421Sroot #include "../h/times.h" 157421Sroot #include "../h/conf.h" 167421Sroot #include "../h/buf.h" 177421Sroot #include "../h/mount.h" 187421Sroot #include "../h/text.h" 197421Sroot #include "../h/seg.h" 207421Sroot #include "../h/vm.h" 217421Sroot #include "../h/acct.h" 227818Sroot #include "../h/uio.h" 238117Sroot #include "../h/kernel.h" 249160Ssam #include "../h/nami.h" 257421Sroot 2612951Ssam #define mask(s) (1 << ((s)-1)) 2712951Ssam #define cantmask (mask(SIGKILL)|mask(SIGCONT)|mask(SIGSTOP)) 2812951Ssam 297499Sroot sigvec() 307421Sroot { 3112951Ssam register struct a { 3212882Ssam int signo; 3312951Ssam struct sigvec *nsv; 3412951Ssam struct sigvec *osv; 3512882Ssam } *uap = (struct a *)u.u_ap; 3612951Ssam struct sigvec vec; 3712951Ssam register struct sigvec *sv; 3812882Ssam register int sig; 397421Sroot 4012882Ssam sig = uap->signo; 4112951Ssam if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) { 4212882Ssam u.u_error = EINVAL; 4312882Ssam return; 4412882Ssam } 4512951Ssam sv = &vec; 4612951Ssam if (uap->osv) { 4712951Ssam sv->sv_handler = u.u_signal[sig]; 4812951Ssam sv->sv_mask = u.u_sigmask[sig]; 4912951Ssam sv->sv_onstack = (u.u_sigonstack & mask(sig)) != 0; 5012951Ssam u.u_error = 5112951Ssam copyout((caddr_t)sv, (caddr_t)uap->osv, sizeof (vec)); 5212951Ssam if (u.u_error) 5312951Ssam return; 5412951Ssam } 5512951Ssam if (uap->nsv) { 5612951Ssam u.u_error = 5712951Ssam copyin((caddr_t)uap->nsv, (caddr_t)sv, sizeof (vec)); 5812951Ssam if (u.u_error) 5912951Ssam return; 6012951Ssam if (sig == SIGCONT && sv->sv_handler == SIG_IGN) { 6112951Ssam u.u_error = EINVAL; 6212951Ssam return; 6312951Ssam } 6412951Ssam setsigvec(sig, sv); 6512951Ssam } 667421Sroot } 677421Sroot 6812951Ssam setsigvec(sig, sv) 6912951Ssam int sig; 7012951Ssam register struct sigvec *sv; 7112882Ssam { 7212882Ssam register struct proc *p; 7312951Ssam register int bit; 7412882Ssam 7512951Ssam bit = mask(sig); 7612882Ssam p = u.u_procp; 7712882Ssam /* 7812882Ssam * Change setting atomically. 7912882Ssam */ 8012882Ssam (void) spl6(); 8112951Ssam u.u_signal[sig] = sv->sv_handler; 8212951Ssam u.u_sigmask[sig] = sv->sv_mask &~ cantmask; 8312951Ssam if (sv->sv_onstack) 8412951Ssam u.u_sigonstack |= bit; 8512951Ssam else 8612951Ssam u.u_sigonstack &= ~bit; 8712951Ssam if (sv->sv_handler == SIG_IGN) { 8812951Ssam p->p_sig &= ~bit; /* never to be seen again */ 8912951Ssam p->p_sigignore |= bit; 9012951Ssam p->p_sigcatch &= ~bit; 9112882Ssam } else { 9212951Ssam p->p_sigignore &= ~bit; 9312951Ssam if (sv->sv_handler == SIG_DFL) 9412951Ssam p->p_sigcatch &= ~bit; 9512882Ssam else 9612951Ssam p->p_sigcatch |= bit; 9712882Ssam } 9812882Ssam (void) spl0(); 9912882Ssam } 10012882Ssam 1017499Sroot sigblock() 1027421Sroot { 10312882Ssam struct a { 10412951Ssam int sigmask; 10512882Ssam } *uap = (struct a *)u.u_ap; 10612951Ssam register struct proc *p = u.u_procp; 1077499Sroot 10812882Ssam (void) spl6(); 10912882Ssam u.u_r.r_val1 = p->p_sigmask; 11012951Ssam p->p_sigmask |= uap->sigmask &~ cantmask; 11112882Ssam (void) spl0(); 1127499Sroot } 1137499Sroot 1147499Sroot sigsetmask() 1157499Sroot { 11612882Ssam struct a { 11712951Ssam int sigmask; 11812882Ssam } *uap = (struct a *)u.u_ap; 11912882Ssam register struct proc *p = u.u_procp; 1207499Sroot 12112882Ssam (void) spl6(); 12212882Ssam u.u_r.r_val1 = p->p_sigmask; 12312951Ssam p->p_sigmask = uap->sigmask &~ cantmask; 12412882Ssam (void) spl0(); 1257499Sroot } 1267499Sroot 1277499Sroot sigpause() 1287499Sroot { 12912882Ssam struct a { 13012951Ssam int sigmask; 13112882Ssam } *uap = (struct a *)u.u_ap; 13212882Ssam register struct proc *p = u.u_procp; 1337499Sroot 13412882Ssam /* 13512882Ssam * When returning from sigpause, we want 13612882Ssam * the old mask to be restored after the 13712882Ssam * signal handler has finished. Thus, we 13812882Ssam * save it here and mark the proc structure 13912882Ssam * to indicate this (should be in u.). 14012882Ssam */ 14112882Ssam u.u_oldmask = p->p_sigmask; 14212882Ssam p->p_flag |= SOMASK; 14312951Ssam p->p_sigmask = uap->sigmask &~ cantmask; 14412882Ssam for (;;) 14512882Ssam sleep((caddr_t)&u, PSLEP); 14612882Ssam /*NOTREACHED*/ 1477499Sroot } 14812951Ssam #undef cantmask 14912951Ssam #undef mask 1507499Sroot 1517499Sroot sigstack() 1527499Sroot { 15312951Ssam register struct a { 15412882Ssam caddr_t asp; 15512951Ssam struct sigstack *nss; 15612951Ssam struct sigstack *oss; 15712882Ssam } *uap = (struct a *)u.u_ap; 15812951Ssam struct sigstack ss; 1597499Sroot 16012951Ssam if (uap->oss) { 16112951Ssam u.u_error = copyout((caddr_t)&u.u_sigstack, (caddr_t)uap->oss, 16212951Ssam sizeof (struct sigstack)); 16312951Ssam if (u.u_error) 16412951Ssam return; 16512951Ssam } 16612951Ssam if (uap->nss) { 16712951Ssam u.u_error = 16812951Ssam copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss)); 16912951Ssam if (u.u_error == 0) 17012951Ssam u.u_sigstack = ss; 17112951Ssam } 1727499Sroot } 1737499Sroot 174*13227Ssam /* KILL SHOULD BE UPDATED */ 175*13227Ssam 1768032Sroot kill() 1778032Sroot { 17812882Ssam register struct a { 17912882Ssam int pid; 18012882Ssam int signo; 18112882Ssam } *uap = (struct a *)u.u_ap; 1828032Sroot 183*13227Ssam u.u_error = kill1(uap->signo < 0, 184*13227Ssam uap->signo < 0 ? -uap->signo : uap->signo, uap->pid); 1858032Sroot } 1868032Sroot 1878032Sroot killpg() 1888032Sroot { 1899989Ssam register struct a { 1909989Ssam int pgrp; 1919989Ssam int signo; 1929989Ssam } *uap = (struct a *)u.u_ap; 1938032Sroot 19412750Ssam u.u_error = kill1(1, uap->signo, uap->pgrp); 1958032Sroot } 1968032Sroot 19712882Ssam /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */ 19812882Ssam 19912750Ssam kill1(ispgrp, signo, who) 2009989Ssam int ispgrp, signo, who; 2019989Ssam { 2029989Ssam register struct proc *p; 2039989Ssam int f, priv = 0; 2049989Ssam 20512835Ssam if (signo < 0 || signo > NSIG) 2069989Ssam return (EINVAL); 2079989Ssam if (who > 0 && !ispgrp) { 2089989Ssam p = pfind(who); 2099989Ssam if (p == 0 || u.u_uid && u.u_uid != p->p_uid) 2109989Ssam return (ESRCH); 21112835Ssam if (signo) 21212835Ssam psignal(p, signo); 2139989Ssam return (0); 2147421Sroot } 2159989Ssam if (who == -1 && u.u_uid == 0) 2169989Ssam priv++, who = 0, ispgrp = 1; /* like sending to pgrp */ 2179989Ssam else if (who == 0) { 2187421Sroot /* 2197421Sroot * Zero process id means send to my process group. 2207421Sroot */ 2219989Ssam ispgrp = 1; 2229989Ssam who = u.u_procp->p_pgrp; 2239989Ssam if (who == 0) 2249989Ssam return (EINVAL); 2257421Sroot } 2269989Ssam for (f = 0, p = proc; p < procNPROC; p++) { 2277421Sroot if (p->p_stat == NULL) 2287421Sroot continue; 2299989Ssam if (!ispgrp) { 2309989Ssam if (p->p_pid != who) 2317421Sroot continue; 2329989Ssam } else if (p->p_pgrp != who && priv == 0 || p->p_ppid == 0 || 2339989Ssam (p->p_flag&SSYS) || (priv && p == u.u_procp)) 2347421Sroot continue; 2357421Sroot if (u.u_uid != 0 && u.u_uid != p->p_uid && 2369989Ssam (signo != SIGCONT || !inferior(p))) 2377421Sroot continue; 2387421Sroot f++; 23912835Ssam if (signo) 24012835Ssam psignal(p, signo); 2417421Sroot } 24212750Ssam return (f == 0 ? ESRCH : 0); 2437421Sroot } 2447421Sroot 2457421Sroot /* 2467421Sroot * Send the specified signal to 2477421Sroot * all processes with 'pgrp' as 2487421Sroot * process group. 2497421Sroot */ 2507421Sroot gsignal(pgrp, sig) 2517421Sroot register int pgrp; 2527421Sroot { 2537421Sroot register struct proc *p; 2547421Sroot 2557421Sroot if (pgrp == 0) 2567421Sroot return; 2577421Sroot for(p = proc; p < procNPROC; p++) 2587421Sroot if (p->p_pgrp == pgrp) 2597421Sroot psignal(p, sig); 2607421Sroot } 2617421Sroot 2627421Sroot /* 2637421Sroot * Send the specified signal to 2647421Sroot * the specified process. 2657421Sroot */ 2667421Sroot psignal(p, sig) 2677421Sroot register struct proc *p; 2687421Sroot register int sig; 2697421Sroot { 2707421Sroot register int s; 2717421Sroot register int (*action)(); 27212882Ssam int sigmask; 2737421Sroot 2747421Sroot if ((unsigned)sig >= NSIG) 2757421Sroot return; 27612882Ssam sigmask = 1 << (sig-1); 2777421Sroot 2787421Sroot /* 2797421Sroot * If proc is traced, always give parent a chance. 2807421Sroot */ 2817421Sroot if (p->p_flag & STRC) 2827421Sroot action = SIG_DFL; 2837421Sroot else { 2847421Sroot /* 28512882Ssam * If the signal is being ignored, 28612882Ssam * then we forget about it immediately. 2877421Sroot */ 28812882Ssam if (p->p_sigignore & sigmask) 2897421Sroot return; 29012882Ssam if (p->p_sigmask & sigmask) 29112882Ssam action = SIG_HOLD; 29212882Ssam else if (p->p_sigcatch & sigmask) 29312882Ssam action = SIG_CATCH; 29412882Ssam else 29512882Ssam action = SIG_DFL; 2967421Sroot } 2977421Sroot #define mask(sig) (1<<(sig-1)) 2987421Sroot #define stops (mask(SIGSTOP)|mask(SIGTSTP)|mask(SIGTTIN)|mask(SIGTTOU)) 2997421Sroot if (sig) { 3007421Sroot p->p_sig |= sigmask; 3017421Sroot switch (sig) { 3027421Sroot 3037421Sroot case SIGTERM: 30412882Ssam if ((p->p_flag&STRC) || action != SIG_DFL) 3057421Sroot break; 3067421Sroot /* fall into ... */ 3077421Sroot 3087421Sroot case SIGKILL: 3097421Sroot if (p->p_nice > NZERO) 3107421Sroot p->p_nice = NZERO; 3117421Sroot break; 3127421Sroot 3137421Sroot case SIGCONT: 3147421Sroot p->p_sig &= ~stops; 3157421Sroot break; 3167421Sroot 3177421Sroot case SIGSTOP: 3187421Sroot case SIGTSTP: 3197421Sroot case SIGTTIN: 3207421Sroot case SIGTTOU: 3217421Sroot p->p_sig &= ~mask(SIGCONT); 3227421Sroot break; 3237421Sroot } 3247421Sroot } 3257421Sroot #undef mask 3267421Sroot #undef stops 3277421Sroot /* 3287421Sroot * Defer further processing for signals which are held. 3297421Sroot */ 3307421Sroot if (action == SIG_HOLD) 3317421Sroot return; 3327421Sroot s = spl6(); 3337421Sroot switch (p->p_stat) { 3347421Sroot 3357421Sroot case SSLEEP: 3367421Sroot /* 3377421Sroot * If process is sleeping at negative priority 3387421Sroot * we can't interrupt the sleep... the signal will 3397421Sroot * be noticed when the process returns through 3407421Sroot * trap() or syscall(). 3417421Sroot */ 3427421Sroot if (p->p_pri <= PZERO) 3437421Sroot goto out; 3447421Sroot /* 3457421Sroot * Process is sleeping and traced... make it runnable 3467421Sroot * so it can discover the signal in issig() and stop 3477421Sroot * for the parent. 3487421Sroot */ 3497421Sroot if (p->p_flag&STRC) 3507421Sroot goto run; 3517421Sroot switch (sig) { 3527421Sroot 3537421Sroot case SIGSTOP: 3547421Sroot case SIGTSTP: 3557421Sroot case SIGTTIN: 3567421Sroot case SIGTTOU: 3577421Sroot /* 3587421Sroot * These are the signals which by default 3597421Sroot * stop a process. 3607421Sroot */ 3617421Sroot if (action != SIG_DFL) 3627421Sroot goto run; 3637421Sroot /* 3647421Sroot * Don't clog system with children of init 3657421Sroot * stopped from the keyboard. 3667421Sroot */ 3677421Sroot if (sig != SIGSTOP && p->p_pptr == &proc[1]) { 3687421Sroot psignal(p, SIGKILL); 3697421Sroot p->p_sig &= ~sigmask; 3707421Sroot splx(s); 3717421Sroot return; 3727421Sroot } 3737421Sroot /* 3747421Sroot * If a child in vfork(), stopping could 3757421Sroot * cause deadlock. 3767421Sroot */ 3777421Sroot if (p->p_flag&SVFORK) 3787421Sroot goto out; 3797421Sroot p->p_sig &= ~sigmask; 3807421Sroot p->p_cursig = sig; 3817421Sroot stop(p); 3827421Sroot goto out; 3837421Sroot 3847421Sroot case SIGIO: 3857421Sroot case SIGURG: 3867421Sroot case SIGCHLD: 3877421Sroot /* 3887421Sroot * These signals are special in that they 3897421Sroot * don't get propogated... if the process 3907421Sroot * isn't interested, forget it. 3917421Sroot */ 3927421Sroot if (action != SIG_DFL) 3937421Sroot goto run; 3947421Sroot p->p_sig &= ~sigmask; /* take it away */ 3957421Sroot goto out; 3967421Sroot 3977421Sroot default: 3987421Sroot /* 3997421Sroot * All other signals cause the process to run 4007421Sroot */ 4017421Sroot goto run; 4027421Sroot } 4037421Sroot /*NOTREACHED*/ 4047421Sroot 4057421Sroot case SSTOP: 4067421Sroot /* 4077421Sroot * If traced process is already stopped, 4087421Sroot * then no further action is necessary. 4097421Sroot */ 4107421Sroot if (p->p_flag&STRC) 4117421Sroot goto out; 4127421Sroot switch (sig) { 4137421Sroot 4147421Sroot case SIGKILL: 4157421Sroot /* 4167421Sroot * Kill signal always sets processes running. 4177421Sroot */ 4187421Sroot goto run; 4197421Sroot 4207421Sroot case SIGCONT: 4217421Sroot /* 4227421Sroot * If the process catches SIGCONT, let it handle 4237421Sroot * the signal itself. If it isn't waiting on 4247421Sroot * an event, then it goes back to run state. 4257421Sroot * Otherwise, process goes back to sleep state. 4267421Sroot */ 4277421Sroot if (action != SIG_DFL || p->p_wchan == 0) 4287421Sroot goto run; 4297421Sroot p->p_stat = SSLEEP; 4307421Sroot goto out; 4317421Sroot 4327421Sroot case SIGSTOP: 4337421Sroot case SIGTSTP: 4347421Sroot case SIGTTIN: 4357421Sroot case SIGTTOU: 4367421Sroot /* 4377421Sroot * Already stopped, don't need to stop again. 4387421Sroot * (If we did the shell could get confused.) 4397421Sroot */ 4407421Sroot p->p_sig &= ~sigmask; /* take it away */ 4417421Sroot goto out; 4427421Sroot 4437421Sroot default: 4447421Sroot /* 4457421Sroot * If process is sleeping interruptibly, then 4467421Sroot * unstick it so that when it is continued 4477421Sroot * it can look at the signal. 4487421Sroot * But don't setrun the process as its not to 4497421Sroot * be unstopped by the signal alone. 4507421Sroot */ 4517421Sroot if (p->p_wchan && p->p_pri > PZERO) 4527421Sroot unsleep(p); 4537421Sroot goto out; 4547421Sroot } 4557421Sroot /*NOTREACHED*/ 4567421Sroot 4577421Sroot default: 4587421Sroot /* 4597421Sroot * SRUN, SIDL, SZOMB do nothing with the signal, 4607421Sroot * other than kicking ourselves if we are running. 4617421Sroot * It will either never be noticed, or noticed very soon. 4627421Sroot */ 4637421Sroot if (p == u.u_procp && !noproc) 4648444Sroot #include "../vax/mtpr.h" 4657421Sroot aston(); 4667421Sroot goto out; 4677421Sroot } 4687421Sroot /*NOTREACHED*/ 4697421Sroot run: 4707421Sroot /* 4717421Sroot * Raise priority to at least PUSER. 4727421Sroot */ 4737421Sroot if (p->p_pri > PUSER) 4747421Sroot if ((p != u.u_procp || noproc) && p->p_stat == SRUN && 4757421Sroot (p->p_flag & SLOAD)) { 4767421Sroot remrq(p); 4777421Sroot p->p_pri = PUSER; 4787421Sroot setrq(p); 4797421Sroot } else 4807421Sroot p->p_pri = PUSER; 4817421Sroot setrun(p); 4827421Sroot out: 4837421Sroot splx(s); 4847421Sroot } 4857421Sroot 4867421Sroot /* 4877421Sroot * Returns true if the current 4887421Sroot * process has a signal to process. 4897421Sroot * The signal to process is put in p_cursig. 4907421Sroot * This is asked at least once each time a process enters the 4917421Sroot * system (though this can usually be done without actually 4927421Sroot * calling issig by checking the pending signal masks.) 4937421Sroot * A signal does not do anything 4947421Sroot * directly to a process; it sets 4957421Sroot * a flag that asks the process to 4967421Sroot * do something to itself. 4977421Sroot */ 4987421Sroot issig() 4997421Sroot { 5007421Sroot register struct proc *p; 5017421Sroot register int sig; 50212882Ssam int sigbits, sigmask; 5037421Sroot 5047421Sroot p = u.u_procp; 5057421Sroot for (;;) { 5067421Sroot sigbits = p->p_sig; 5077421Sroot if ((p->p_flag&STRC) == 0) 50812882Ssam sigbits &= ~(p->p_sigignore | p->p_sigmask); 5097421Sroot if (p->p_flag&SVFORK) 5107421Sroot #define bit(a) (1<<(a-1)) 5117421Sroot sigbits &= ~(bit(SIGSTOP)|bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU)); 5127421Sroot if (sigbits == 0) 5137421Sroot break; 51412882Ssam sig = ffs(sigbits); 51512882Ssam sigmask = 1 << (sig-1); 5167421Sroot p->p_sig &= ~sigmask; /* take the signal! */ 5177421Sroot p->p_cursig = sig; 51812882Ssam if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) { 5197421Sroot /* 5207421Sroot * If traced, always stop, and stay 5217421Sroot * stopped until released by the parent. 5227421Sroot */ 5237421Sroot do { 5247421Sroot stop(p); 5257421Sroot swtch(); 5267421Sroot } while (!procxmt() && p->p_flag&STRC); 5277421Sroot 5287421Sroot /* 52912969Ssam * If the traced bit got turned off or signal 53012969Ssam * is being masked, then put the signal taken 53112969Ssam * above back into p_sig and go back up to the 53212969Ssam * top to rescan signals. This ensures that 53312969Ssam * p_sig* and u_signal are consistent. 5347421Sroot */ 53512969Ssam if ((p->p_flag&STRC) == 0 || (p->p_sigmask & sigmask)) { 5367421Sroot p->p_sig |= sigmask; 5377421Sroot continue; 5387421Sroot } 5397421Sroot 5407421Sroot /* 5417421Sroot * If parent wants us to take the signal, 5427421Sroot * then it will leave it in p->p_cursig; 5437421Sroot * otherwise we just look for signals again. 5447421Sroot */ 5457421Sroot sig = p->p_cursig; 5467421Sroot if (sig == 0) 5477421Sroot continue; 5487421Sroot } 5497421Sroot switch (u.u_signal[sig]) { 5507421Sroot 5517421Sroot case SIG_DFL: 5527421Sroot /* 5537421Sroot * Don't take default actions on system processes. 5547421Sroot */ 5557421Sroot if (p->p_ppid == 0) 5567421Sroot break; 5577421Sroot switch (sig) { 5587421Sroot 5597421Sroot case SIGTSTP: 5607421Sroot case SIGTTIN: 5617421Sroot case SIGTTOU: 5627421Sroot /* 5637421Sroot * Children of init aren't allowed to stop 5647421Sroot * on signals from the keyboard. 5657421Sroot */ 5667421Sroot if (p->p_pptr == &proc[1]) { 5677421Sroot psignal(p, SIGKILL); 5687421Sroot continue; 5697421Sroot } 5707421Sroot /* fall into ... */ 5717421Sroot 5727421Sroot case SIGSTOP: 5737421Sroot if (p->p_flag&STRC) 5747421Sroot continue; 5757421Sroot stop(p); 5767421Sroot swtch(); 5777421Sroot continue; 5787421Sroot 5797421Sroot case SIGCONT: 5807421Sroot case SIGCHLD: 58112882Ssam case SIGURG: 58212951Ssam case SIGIO: 5837421Sroot /* 5847421Sroot * These signals are normally not 5857421Sroot * sent if the action is the default. 5867421Sroot */ 5877421Sroot continue; /* == ignore */ 5887421Sroot 5897421Sroot default: 5907421Sroot goto send; 5917421Sroot } 5927421Sroot /*NOTREACHED*/ 5937421Sroot 5947421Sroot case SIG_HOLD: 5957421Sroot case SIG_IGN: 5967421Sroot /* 5977421Sroot * Masking above should prevent us 5987421Sroot * ever trying to take action on a held 5997421Sroot * or ignored signal, unless process is traced. 6007421Sroot */ 6017421Sroot if ((p->p_flag&STRC) == 0) 6027421Sroot printf("issig\n"); 6037421Sroot continue; 6047421Sroot 6057421Sroot default: 6067421Sroot /* 6077421Sroot * This signal has an action, let 6087421Sroot * psig process it. 6097421Sroot */ 6107421Sroot goto send; 6117421Sroot } 6127421Sroot /*NOTREACHED*/ 6137421Sroot } 6147421Sroot /* 6157421Sroot * Didn't find a signal to send. 6167421Sroot */ 6177421Sroot p->p_cursig = 0; 6187421Sroot return (0); 6197421Sroot 6207421Sroot send: 6217421Sroot /* 6227421Sroot * Let psig process the signal. 6237421Sroot */ 6247421Sroot return (sig); 6257421Sroot } 6267421Sroot 6277421Sroot /* 6287421Sroot * Put the argument process into the stopped 6297421Sroot * state and notify the parent via wakeup and/or signal. 6307421Sroot */ 6317421Sroot stop(p) 6327421Sroot register struct proc *p; 6337421Sroot { 6347421Sroot 6357421Sroot p->p_stat = SSTOP; 6367421Sroot p->p_flag &= ~SWTED; 6377421Sroot wakeup((caddr_t)p->p_pptr); 6387421Sroot /* 6397421Sroot * Avoid sending signal to parent if process is traced 6407421Sroot */ 6417421Sroot if (p->p_flag&STRC) 6427421Sroot return; 6437421Sroot psignal(p->p_pptr, SIGCHLD); 6447421Sroot } 6457421Sroot 6467421Sroot /* 6477421Sroot * Perform the action specified by 6487421Sroot * the current signal. 6497421Sroot * The usual sequence is: 6507421Sroot * if (issig()) 6517421Sroot * psig(); 6527421Sroot * The signal bit has already been cleared by issig, 6537421Sroot * and the current signal number stored in p->p_cursig. 6547421Sroot */ 6557421Sroot psig() 6567421Sroot { 65712882Ssam register struct proc *p = u.u_procp; 65812882Ssam register int sig = p->p_cursig; 65912882Ssam int sigmask = 1 << (sig - 1), returnmask; 6607421Sroot register int (*action)(); 6617421Sroot 66212882Ssam if (sig == 0) 6637421Sroot panic("psig"); 66412882Ssam action = u.u_signal[sig]; 6657421Sroot if (action != SIG_DFL) { 66612882Ssam if (action == SIG_IGN || (p->p_sigmask & sigmask)) 6677421Sroot panic("psig action"); 6687421Sroot u.u_error = 0; 6697421Sroot /* 67012882Ssam * Set the new mask value and also defer further 67112882Ssam * occurences of this signal (unless we're simulating 67212882Ssam * the old signal facilities). 67312882Ssam * 67412882Ssam * Special case: user has done a sigpause. Here the 67512882Ssam * current mask is not of interest, but rather the 67612882Ssam * mask from before the sigpause is what we want restored 67712882Ssam * after the signal processing is completed. 6787421Sroot */ 67912882Ssam (void) spl6(); 68012882Ssam if (p->p_flag & SOUSIG) { 68112882Ssam if (sig != SIGILL && sig != SIGTRAP) { 68212882Ssam u.u_signal[sig] = SIG_DFL; 68312882Ssam p->p_sigcatch &= ~sigmask; 68412882Ssam } 68512882Ssam sigmask = 0; 6867421Sroot } 68712882Ssam if (p->p_flag & SOMASK) { 68812882Ssam returnmask = u.u_oldmask; 68912882Ssam p->p_flag &= ~SOMASK; 69012882Ssam } else 69112882Ssam returnmask = p->p_sigmask; 69212951Ssam p->p_sigmask |= u.u_sigmask[sig] | sigmask; 69312882Ssam (void) spl0(); 6948032Sroot u.u_ru.ru_nsignals++; 69512882Ssam sendsig(action, sig, returnmask); 69612882Ssam p->p_cursig = 0; 6977421Sroot return; 6987421Sroot } 6997421Sroot u.u_acflag |= AXSIG; 70012882Ssam switch (sig) { 7017421Sroot 7027421Sroot case SIGILL: 7037421Sroot case SIGIOT: 7047421Sroot case SIGBUS: 7057421Sroot case SIGQUIT: 7067421Sroot case SIGTRAP: 7077421Sroot case SIGEMT: 7087421Sroot case SIGFPE: 7097421Sroot case SIGSEGV: 7107421Sroot case SIGSYS: 71112882Ssam u.u_arg[0] = sig; 7127421Sroot if (core()) 71312882Ssam sig += 0200; 7147421Sroot } 71512882Ssam exit(sig); 7167421Sroot } 7177421Sroot 7187421Sroot /* 7197421Sroot * Create a core image on the file "core" 7207421Sroot * If you are looking for protection glitches, 7217421Sroot * there are probably a wealth of them here 7227421Sroot * when this occurs to a suid command. 7237421Sroot * 7247421Sroot * It writes UPAGES block of the 7257421Sroot * user.h area followed by the entire 7267421Sroot * data+stack segments. 7277421Sroot */ 7287421Sroot core() 7297421Sroot { 7307421Sroot register struct inode *ip; 7317421Sroot extern schar(); 7327421Sroot 73312639Ssam if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid) 7347818Sroot return (0); 7358032Sroot if (ctob(UPAGES+u.u_dsize+u.u_ssize) >= 7368032Sroot u.u_rlimit[RLIMIT_CORE].rlim_cur) 7377421Sroot return (0); 7387421Sroot u.u_error = 0; 7397421Sroot u.u_dirp = "core"; 7409160Ssam ip = namei(schar, CREATE, 1); 7417421Sroot if (ip == NULL) { 7427421Sroot if (u.u_error) 7437421Sroot return (0); 74412639Ssam ip = maknode(0644); 7457421Sroot if (ip==NULL) 7467421Sroot return (0); 7477421Sroot } 7487818Sroot if (access(ip, IWRITE) || 7497818Sroot (ip->i_mode&IFMT) != IFREG || 7507818Sroot ip->i_nlink != 1) { 7517421Sroot u.u_error = EFAULT; 7527818Sroot goto out; 7537818Sroot } 7549160Ssam itrunc(ip, (u_long)0); 7557818Sroot u.u_acflag |= ACORE; 75612882Ssam u.u_error = rdwri(UIO_WRITE, ip, 75712882Ssam (caddr_t)&u, 75812882Ssam ctob(UPAGES), 75912882Ssam 0, 1, (int *)0); 7608101Sroot if (u.u_error == 0) 7618644Sroot u.u_error = rdwri(UIO_WRITE, ip, 7628967Sroot (caddr_t)ctob(dptov(u.u_procp, 0)), 7638967Sroot ctob(u.u_dsize), 7648644Sroot ctob(UPAGES), 0, (int *)0); 7658101Sroot if (u.u_error == 0) 7668644Sroot u.u_error = rdwri(UIO_WRITE, ip, 7678967Sroot (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)), 7688967Sroot ctob(u.u_ssize), 7698644Sroot ctob(UPAGES)+ctob(u.u_dsize), 0, (int *)0); 7707818Sroot out: 7717421Sroot iput(ip); 7727818Sroot return (u.u_error == 0); 7737421Sroot } 774