1*12969Ssam /* kern_sig.c 5.21 83/06/10 */ 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 1748032Sroot kill() 1758032Sroot { 17612882Ssam register struct a { 17712882Ssam int pid; 17812882Ssam int signo; 17912882Ssam } *uap = (struct a *)u.u_ap; 1808032Sroot 18112882Ssam u.u_error = kill1(0, uap->signo, uap->pid); 1828032Sroot } 1838032Sroot 1848032Sroot killpg() 1858032Sroot { 1869989Ssam register struct a { 1879989Ssam int pgrp; 1889989Ssam int signo; 1899989Ssam } *uap = (struct a *)u.u_ap; 1908032Sroot 19112750Ssam u.u_error = kill1(1, uap->signo, uap->pgrp); 1928032Sroot } 1938032Sroot 19412882Ssam /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */ 19512882Ssam 19612750Ssam kill1(ispgrp, signo, who) 1979989Ssam int ispgrp, signo, who; 1989989Ssam { 1999989Ssam register struct proc *p; 2009989Ssam int f, priv = 0; 2019989Ssam 20212835Ssam if (signo < 0 || signo > NSIG) 2039989Ssam return (EINVAL); 2049989Ssam if (who > 0 && !ispgrp) { 2059989Ssam p = pfind(who); 2069989Ssam if (p == 0 || u.u_uid && u.u_uid != p->p_uid) 2079989Ssam return (ESRCH); 20812835Ssam if (signo) 20912835Ssam psignal(p, signo); 2109989Ssam return (0); 2117421Sroot } 2129989Ssam if (who == -1 && u.u_uid == 0) 2139989Ssam priv++, who = 0, ispgrp = 1; /* like sending to pgrp */ 2149989Ssam else if (who == 0) { 2157421Sroot /* 2167421Sroot * Zero process id means send to my process group. 2177421Sroot */ 2189989Ssam ispgrp = 1; 2199989Ssam who = u.u_procp->p_pgrp; 2209989Ssam if (who == 0) 2219989Ssam return (EINVAL); 2227421Sroot } 2239989Ssam for (f = 0, p = proc; p < procNPROC; p++) { 2247421Sroot if (p->p_stat == NULL) 2257421Sroot continue; 2269989Ssam if (!ispgrp) { 2279989Ssam if (p->p_pid != who) 2287421Sroot continue; 2299989Ssam } else if (p->p_pgrp != who && priv == 0 || p->p_ppid == 0 || 2309989Ssam (p->p_flag&SSYS) || (priv && p == u.u_procp)) 2317421Sroot continue; 2327421Sroot if (u.u_uid != 0 && u.u_uid != p->p_uid && 2339989Ssam (signo != SIGCONT || !inferior(p))) 2347421Sroot continue; 2357421Sroot f++; 23612835Ssam if (signo) 23712835Ssam psignal(p, signo); 2387421Sroot } 23912750Ssam return (f == 0 ? ESRCH : 0); 2407421Sroot } 2417421Sroot 2427421Sroot /* 2437421Sroot * Send the specified signal to 2447421Sroot * all processes with 'pgrp' as 2457421Sroot * process group. 2467421Sroot */ 2477421Sroot gsignal(pgrp, sig) 2487421Sroot register int pgrp; 2497421Sroot { 2507421Sroot register struct proc *p; 2517421Sroot 2527421Sroot if (pgrp == 0) 2537421Sroot return; 2547421Sroot for(p = proc; p < procNPROC; p++) 2557421Sroot if (p->p_pgrp == pgrp) 2567421Sroot psignal(p, sig); 2577421Sroot } 2587421Sroot 2597421Sroot /* 2607421Sroot * Send the specified signal to 2617421Sroot * the specified process. 2627421Sroot */ 2637421Sroot psignal(p, sig) 2647421Sroot register struct proc *p; 2657421Sroot register int sig; 2667421Sroot { 2677421Sroot register int s; 2687421Sroot register int (*action)(); 26912882Ssam int sigmask; 2707421Sroot 2717421Sroot if ((unsigned)sig >= NSIG) 2727421Sroot return; 27312882Ssam sigmask = 1 << (sig-1); 2747421Sroot 2757421Sroot /* 2767421Sroot * If proc is traced, always give parent a chance. 2777421Sroot */ 2787421Sroot if (p->p_flag & STRC) 2797421Sroot action = SIG_DFL; 2807421Sroot else { 2817421Sroot /* 28212882Ssam * If the signal is being ignored, 28312882Ssam * then we forget about it immediately. 2847421Sroot */ 28512882Ssam if (p->p_sigignore & sigmask) 2867421Sroot return; 28712882Ssam if (p->p_sigmask & sigmask) 28812882Ssam action = SIG_HOLD; 28912882Ssam else if (p->p_sigcatch & sigmask) 29012882Ssam action = SIG_CATCH; 29112882Ssam else 29212882Ssam action = SIG_DFL; 2937421Sroot } 2947421Sroot #define mask(sig) (1<<(sig-1)) 2957421Sroot #define stops (mask(SIGSTOP)|mask(SIGTSTP)|mask(SIGTTIN)|mask(SIGTTOU)) 2967421Sroot if (sig) { 2977421Sroot p->p_sig |= sigmask; 2987421Sroot switch (sig) { 2997421Sroot 3007421Sroot case SIGTERM: 30112882Ssam if ((p->p_flag&STRC) || action != SIG_DFL) 3027421Sroot break; 3037421Sroot /* fall into ... */ 3047421Sroot 3057421Sroot case SIGKILL: 3067421Sroot if (p->p_nice > NZERO) 3077421Sroot p->p_nice = NZERO; 3087421Sroot break; 3097421Sroot 3107421Sroot case SIGCONT: 3117421Sroot p->p_sig &= ~stops; 3127421Sroot break; 3137421Sroot 3147421Sroot case SIGSTOP: 3157421Sroot case SIGTSTP: 3167421Sroot case SIGTTIN: 3177421Sroot case SIGTTOU: 3187421Sroot p->p_sig &= ~mask(SIGCONT); 3197421Sroot break; 3207421Sroot } 3217421Sroot } 3227421Sroot #undef mask 3237421Sroot #undef stops 3247421Sroot /* 3257421Sroot * Defer further processing for signals which are held. 3267421Sroot */ 3277421Sroot if (action == SIG_HOLD) 3287421Sroot return; 3297421Sroot s = spl6(); 3307421Sroot switch (p->p_stat) { 3317421Sroot 3327421Sroot case SSLEEP: 3337421Sroot /* 3347421Sroot * If process is sleeping at negative priority 3357421Sroot * we can't interrupt the sleep... the signal will 3367421Sroot * be noticed when the process returns through 3377421Sroot * trap() or syscall(). 3387421Sroot */ 3397421Sroot if (p->p_pri <= PZERO) 3407421Sroot goto out; 3417421Sroot /* 3427421Sroot * Process is sleeping and traced... make it runnable 3437421Sroot * so it can discover the signal in issig() and stop 3447421Sroot * for the parent. 3457421Sroot */ 3467421Sroot if (p->p_flag&STRC) 3477421Sroot goto run; 3487421Sroot switch (sig) { 3497421Sroot 3507421Sroot case SIGSTOP: 3517421Sroot case SIGTSTP: 3527421Sroot case SIGTTIN: 3537421Sroot case SIGTTOU: 3547421Sroot /* 3557421Sroot * These are the signals which by default 3567421Sroot * stop a process. 3577421Sroot */ 3587421Sroot if (action != SIG_DFL) 3597421Sroot goto run; 3607421Sroot /* 3617421Sroot * Don't clog system with children of init 3627421Sroot * stopped from the keyboard. 3637421Sroot */ 3647421Sroot if (sig != SIGSTOP && p->p_pptr == &proc[1]) { 3657421Sroot psignal(p, SIGKILL); 3667421Sroot p->p_sig &= ~sigmask; 3677421Sroot splx(s); 3687421Sroot return; 3697421Sroot } 3707421Sroot /* 3717421Sroot * If a child in vfork(), stopping could 3727421Sroot * cause deadlock. 3737421Sroot */ 3747421Sroot if (p->p_flag&SVFORK) 3757421Sroot goto out; 3767421Sroot p->p_sig &= ~sigmask; 3777421Sroot p->p_cursig = sig; 3787421Sroot stop(p); 3797421Sroot goto out; 3807421Sroot 3817421Sroot case SIGIO: 3827421Sroot case SIGURG: 3837421Sroot case SIGCHLD: 3847421Sroot /* 3857421Sroot * These signals are special in that they 3867421Sroot * don't get propogated... if the process 3877421Sroot * isn't interested, forget it. 3887421Sroot */ 3897421Sroot if (action != SIG_DFL) 3907421Sroot goto run; 3917421Sroot p->p_sig &= ~sigmask; /* take it away */ 3927421Sroot goto out; 3937421Sroot 3947421Sroot default: 3957421Sroot /* 3967421Sroot * All other signals cause the process to run 3977421Sroot */ 3987421Sroot goto run; 3997421Sroot } 4007421Sroot /*NOTREACHED*/ 4017421Sroot 4027421Sroot case SSTOP: 4037421Sroot /* 4047421Sroot * If traced process is already stopped, 4057421Sroot * then no further action is necessary. 4067421Sroot */ 4077421Sroot if (p->p_flag&STRC) 4087421Sroot goto out; 4097421Sroot switch (sig) { 4107421Sroot 4117421Sroot case SIGKILL: 4127421Sroot /* 4137421Sroot * Kill signal always sets processes running. 4147421Sroot */ 4157421Sroot goto run; 4167421Sroot 4177421Sroot case SIGCONT: 4187421Sroot /* 4197421Sroot * If the process catches SIGCONT, let it handle 4207421Sroot * the signal itself. If it isn't waiting on 4217421Sroot * an event, then it goes back to run state. 4227421Sroot * Otherwise, process goes back to sleep state. 4237421Sroot */ 4247421Sroot if (action != SIG_DFL || p->p_wchan == 0) 4257421Sroot goto run; 4267421Sroot p->p_stat = SSLEEP; 4277421Sroot goto out; 4287421Sroot 4297421Sroot case SIGSTOP: 4307421Sroot case SIGTSTP: 4317421Sroot case SIGTTIN: 4327421Sroot case SIGTTOU: 4337421Sroot /* 4347421Sroot * Already stopped, don't need to stop again. 4357421Sroot * (If we did the shell could get confused.) 4367421Sroot */ 4377421Sroot p->p_sig &= ~sigmask; /* take it away */ 4387421Sroot goto out; 4397421Sroot 4407421Sroot default: 4417421Sroot /* 4427421Sroot * If process is sleeping interruptibly, then 4437421Sroot * unstick it so that when it is continued 4447421Sroot * it can look at the signal. 4457421Sroot * But don't setrun the process as its not to 4467421Sroot * be unstopped by the signal alone. 4477421Sroot */ 4487421Sroot if (p->p_wchan && p->p_pri > PZERO) 4497421Sroot unsleep(p); 4507421Sroot goto out; 4517421Sroot } 4527421Sroot /*NOTREACHED*/ 4537421Sroot 4547421Sroot default: 4557421Sroot /* 4567421Sroot * SRUN, SIDL, SZOMB do nothing with the signal, 4577421Sroot * other than kicking ourselves if we are running. 4587421Sroot * It will either never be noticed, or noticed very soon. 4597421Sroot */ 4607421Sroot if (p == u.u_procp && !noproc) 4618444Sroot #include "../vax/mtpr.h" 4627421Sroot aston(); 4637421Sroot goto out; 4647421Sroot } 4657421Sroot /*NOTREACHED*/ 4667421Sroot run: 4677421Sroot /* 4687421Sroot * Raise priority to at least PUSER. 4697421Sroot */ 4707421Sroot if (p->p_pri > PUSER) 4717421Sroot if ((p != u.u_procp || noproc) && p->p_stat == SRUN && 4727421Sroot (p->p_flag & SLOAD)) { 4737421Sroot remrq(p); 4747421Sroot p->p_pri = PUSER; 4757421Sroot setrq(p); 4767421Sroot } else 4777421Sroot p->p_pri = PUSER; 4787421Sroot setrun(p); 4797421Sroot out: 4807421Sroot splx(s); 4817421Sroot } 4827421Sroot 4837421Sroot /* 4847421Sroot * Returns true if the current 4857421Sroot * process has a signal to process. 4867421Sroot * The signal to process is put in p_cursig. 4877421Sroot * This is asked at least once each time a process enters the 4887421Sroot * system (though this can usually be done without actually 4897421Sroot * calling issig by checking the pending signal masks.) 4907421Sroot * A signal does not do anything 4917421Sroot * directly to a process; it sets 4927421Sroot * a flag that asks the process to 4937421Sroot * do something to itself. 4947421Sroot */ 4957421Sroot issig() 4967421Sroot { 4977421Sroot register struct proc *p; 4987421Sroot register int sig; 49912882Ssam int sigbits, sigmask; 5007421Sroot 5017421Sroot p = u.u_procp; 5027421Sroot for (;;) { 5037421Sroot sigbits = p->p_sig; 5047421Sroot if ((p->p_flag&STRC) == 0) 50512882Ssam sigbits &= ~(p->p_sigignore | p->p_sigmask); 5067421Sroot if (p->p_flag&SVFORK) 5077421Sroot #define bit(a) (1<<(a-1)) 5087421Sroot sigbits &= ~(bit(SIGSTOP)|bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU)); 5097421Sroot if (sigbits == 0) 5107421Sroot break; 51112882Ssam sig = ffs(sigbits); 51212882Ssam sigmask = 1 << (sig-1); 5137421Sroot p->p_sig &= ~sigmask; /* take the signal! */ 5147421Sroot p->p_cursig = sig; 51512882Ssam if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) { 5167421Sroot /* 5177421Sroot * If traced, always stop, and stay 5187421Sroot * stopped until released by the parent. 5197421Sroot */ 5207421Sroot do { 5217421Sroot stop(p); 5227421Sroot swtch(); 5237421Sroot } while (!procxmt() && p->p_flag&STRC); 5247421Sroot 5257421Sroot /* 526*12969Ssam * If the traced bit got turned off or signal 527*12969Ssam * is being masked, then put the signal taken 528*12969Ssam * above back into p_sig and go back up to the 529*12969Ssam * top to rescan signals. This ensures that 530*12969Ssam * p_sig* and u_signal are consistent. 5317421Sroot */ 532*12969Ssam if ((p->p_flag&STRC) == 0 || (p->p_sigmask & sigmask)) { 5337421Sroot p->p_sig |= sigmask; 5347421Sroot continue; 5357421Sroot } 5367421Sroot 5377421Sroot /* 5387421Sroot * If parent wants us to take the signal, 5397421Sroot * then it will leave it in p->p_cursig; 5407421Sroot * otherwise we just look for signals again. 5417421Sroot */ 5427421Sroot sig = p->p_cursig; 5437421Sroot if (sig == 0) 5447421Sroot continue; 5457421Sroot } 5467421Sroot switch (u.u_signal[sig]) { 5477421Sroot 5487421Sroot case SIG_DFL: 5497421Sroot /* 5507421Sroot * Don't take default actions on system processes. 5517421Sroot */ 5527421Sroot if (p->p_ppid == 0) 5537421Sroot break; 5547421Sroot switch (sig) { 5557421Sroot 5567421Sroot case SIGTSTP: 5577421Sroot case SIGTTIN: 5587421Sroot case SIGTTOU: 5597421Sroot /* 5607421Sroot * Children of init aren't allowed to stop 5617421Sroot * on signals from the keyboard. 5627421Sroot */ 5637421Sroot if (p->p_pptr == &proc[1]) { 5647421Sroot psignal(p, SIGKILL); 5657421Sroot continue; 5667421Sroot } 5677421Sroot /* fall into ... */ 5687421Sroot 5697421Sroot case SIGSTOP: 5707421Sroot if (p->p_flag&STRC) 5717421Sroot continue; 5727421Sroot stop(p); 5737421Sroot swtch(); 5747421Sroot continue; 5757421Sroot 5767421Sroot case SIGCONT: 5777421Sroot case SIGCHLD: 57812882Ssam case SIGURG: 57912951Ssam case SIGIO: 5807421Sroot /* 5817421Sroot * These signals are normally not 5827421Sroot * sent if the action is the default. 5837421Sroot */ 5847421Sroot continue; /* == ignore */ 5857421Sroot 5867421Sroot default: 5877421Sroot goto send; 5887421Sroot } 5897421Sroot /*NOTREACHED*/ 5907421Sroot 5917421Sroot case SIG_HOLD: 5927421Sroot case SIG_IGN: 5937421Sroot /* 5947421Sroot * Masking above should prevent us 5957421Sroot * ever trying to take action on a held 5967421Sroot * or ignored signal, unless process is traced. 5977421Sroot */ 5987421Sroot if ((p->p_flag&STRC) == 0) 5997421Sroot printf("issig\n"); 6007421Sroot continue; 6017421Sroot 6027421Sroot default: 6037421Sroot /* 6047421Sroot * This signal has an action, let 6057421Sroot * psig process it. 6067421Sroot */ 6077421Sroot goto send; 6087421Sroot } 6097421Sroot /*NOTREACHED*/ 6107421Sroot } 6117421Sroot /* 6127421Sroot * Didn't find a signal to send. 6137421Sroot */ 6147421Sroot p->p_cursig = 0; 6157421Sroot return (0); 6167421Sroot 6177421Sroot send: 6187421Sroot /* 6197421Sroot * Let psig process the signal. 6207421Sroot */ 6217421Sroot return (sig); 6227421Sroot } 6237421Sroot 6247421Sroot /* 6257421Sroot * Put the argument process into the stopped 6267421Sroot * state and notify the parent via wakeup and/or signal. 6277421Sroot */ 6287421Sroot stop(p) 6297421Sroot register struct proc *p; 6307421Sroot { 6317421Sroot 6327421Sroot p->p_stat = SSTOP; 6337421Sroot p->p_flag &= ~SWTED; 6347421Sroot wakeup((caddr_t)p->p_pptr); 6357421Sroot /* 6367421Sroot * Avoid sending signal to parent if process is traced 6377421Sroot */ 6387421Sroot if (p->p_flag&STRC) 6397421Sroot return; 6407421Sroot psignal(p->p_pptr, SIGCHLD); 6417421Sroot } 6427421Sroot 6437421Sroot /* 6447421Sroot * Perform the action specified by 6457421Sroot * the current signal. 6467421Sroot * The usual sequence is: 6477421Sroot * if (issig()) 6487421Sroot * psig(); 6497421Sroot * The signal bit has already been cleared by issig, 6507421Sroot * and the current signal number stored in p->p_cursig. 6517421Sroot */ 6527421Sroot psig() 6537421Sroot { 65412882Ssam register struct proc *p = u.u_procp; 65512882Ssam register int sig = p->p_cursig; 65612882Ssam int sigmask = 1 << (sig - 1), returnmask; 6577421Sroot register int (*action)(); 6587421Sroot 65912882Ssam if (sig == 0) 6607421Sroot panic("psig"); 66112882Ssam action = u.u_signal[sig]; 6627421Sroot if (action != SIG_DFL) { 66312882Ssam if (action == SIG_IGN || (p->p_sigmask & sigmask)) 6647421Sroot panic("psig action"); 6657421Sroot u.u_error = 0; 6667421Sroot /* 66712882Ssam * Set the new mask value and also defer further 66812882Ssam * occurences of this signal (unless we're simulating 66912882Ssam * the old signal facilities). 67012882Ssam * 67112882Ssam * Special case: user has done a sigpause. Here the 67212882Ssam * current mask is not of interest, but rather the 67312882Ssam * mask from before the sigpause is what we want restored 67412882Ssam * after the signal processing is completed. 6757421Sroot */ 67612882Ssam (void) spl6(); 67712882Ssam if (p->p_flag & SOUSIG) { 67812882Ssam if (sig != SIGILL && sig != SIGTRAP) { 67912882Ssam u.u_signal[sig] = SIG_DFL; 68012882Ssam p->p_sigcatch &= ~sigmask; 68112882Ssam } 68212882Ssam sigmask = 0; 6837421Sroot } 68412882Ssam if (p->p_flag & SOMASK) { 68512882Ssam returnmask = u.u_oldmask; 68612882Ssam p->p_flag &= ~SOMASK; 68712882Ssam } else 68812882Ssam returnmask = p->p_sigmask; 68912951Ssam p->p_sigmask |= u.u_sigmask[sig] | sigmask; 69012882Ssam (void) spl0(); 6918032Sroot u.u_ru.ru_nsignals++; 69212882Ssam sendsig(action, sig, returnmask); 69312882Ssam p->p_cursig = 0; 6947421Sroot return; 6957421Sroot } 6967421Sroot u.u_acflag |= AXSIG; 69712882Ssam switch (sig) { 6987421Sroot 6997421Sroot case SIGILL: 7007421Sroot case SIGIOT: 7017421Sroot case SIGBUS: 7027421Sroot case SIGQUIT: 7037421Sroot case SIGTRAP: 7047421Sroot case SIGEMT: 7057421Sroot case SIGFPE: 7067421Sroot case SIGSEGV: 7077421Sroot case SIGSYS: 70812882Ssam u.u_arg[0] = sig; 7097421Sroot if (core()) 71012882Ssam sig += 0200; 7117421Sroot } 71212882Ssam exit(sig); 7137421Sroot } 7147421Sroot 7157421Sroot /* 7167421Sroot * Create a core image on the file "core" 7177421Sroot * If you are looking for protection glitches, 7187421Sroot * there are probably a wealth of them here 7197421Sroot * when this occurs to a suid command. 7207421Sroot * 7217421Sroot * It writes UPAGES block of the 7227421Sroot * user.h area followed by the entire 7237421Sroot * data+stack segments. 7247421Sroot */ 7257421Sroot core() 7267421Sroot { 7277421Sroot register struct inode *ip; 7287421Sroot extern schar(); 7297421Sroot 73012639Ssam if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid) 7317818Sroot return (0); 7328032Sroot if (ctob(UPAGES+u.u_dsize+u.u_ssize) >= 7338032Sroot u.u_rlimit[RLIMIT_CORE].rlim_cur) 7347421Sroot return (0); 7357421Sroot u.u_error = 0; 7367421Sroot u.u_dirp = "core"; 7379160Ssam ip = namei(schar, CREATE, 1); 7387421Sroot if (ip == NULL) { 7397421Sroot if (u.u_error) 7407421Sroot return (0); 74112639Ssam ip = maknode(0644); 7427421Sroot if (ip==NULL) 7437421Sroot return (0); 7447421Sroot } 7457818Sroot if (access(ip, IWRITE) || 7467818Sroot (ip->i_mode&IFMT) != IFREG || 7477818Sroot ip->i_nlink != 1) { 7487421Sroot u.u_error = EFAULT; 7497818Sroot goto out; 7507818Sroot } 7519160Ssam itrunc(ip, (u_long)0); 7527818Sroot u.u_acflag |= ACORE; 75312882Ssam u.u_error = rdwri(UIO_WRITE, ip, 75412882Ssam (caddr_t)&u, 75512882Ssam ctob(UPAGES), 75612882Ssam 0, 1, (int *)0); 7578101Sroot if (u.u_error == 0) 7588644Sroot u.u_error = rdwri(UIO_WRITE, ip, 7598967Sroot (caddr_t)ctob(dptov(u.u_procp, 0)), 7608967Sroot ctob(u.u_dsize), 7618644Sroot ctob(UPAGES), 0, (int *)0); 7628101Sroot if (u.u_error == 0) 7638644Sroot u.u_error = rdwri(UIO_WRITE, ip, 7648967Sroot (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)), 7658967Sroot ctob(u.u_ssize), 7668644Sroot ctob(UPAGES)+ctob(u.u_dsize), 0, (int *)0); 7677818Sroot out: 7687421Sroot iput(ip); 7697818Sroot return (u.u_error == 0); 7707421Sroot } 771