xref: /csrg-svn/sys/kern/kern_sig.c (revision 40807)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)kern_sig.c	7.12 (Berkeley) 04/05/90
18  */
19 
20 #include "param.h"
21 #include "systm.h"
22 #include "syscontext.h"	/* XXX */
23 #include "vnode.h"
24 #include "proc.h"
25 #include "timeb.h"
26 #include "times.h"
27 #include "buf.h"
28 #include "mount.h"
29 #include "text.h"
30 #include "seg.h"
31 #include "vm.h"
32 #include "acct.h"
33 #include "uio.h"
34 #include "file.h"
35 #include "kernel.h"
36 #include "wait.h"
37 #include "ktrace.h"
38 
39 #include "machine/reg.h"
40 #include "machine/pte.h"
41 #include "machine/psl.h"
42 #include "machine/mtpr.h"
43 
44 #define	stopsigmask	(sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
45 			sigmask(SIGTTIN)|sigmask(SIGTTOU))
46 #define defaultignmask	(sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \
47 			sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO))
48 
49 /*
50  * Can the current process (u.u_procp) send the specified signal
51  * to the specified process?
52  */
53 #define CANSIGNAL(p, signo) \
54 	(u.u_uid == 0 || \
55 	    u.u_uid == (p)->p_uid || u.u_uid == (p)->p_ruid || \
56 	    u.u_procp->p_ruid == (p)->p_uid || \
57 	    u.u_procp->p_ruid == (p)->p_ruid || \
58 	    ((signo) == SIGCONT && (p)->p_session == u.u_procp->p_session))
59 
60 sigaction()
61 {
62 	register struct a {
63 		int	signo;
64 		struct	sigaction *nsa;
65 		struct	sigaction *osa;
66 	} *uap = (struct a  *)u.u_ap;
67 	struct sigaction vec;
68 	register struct sigaction *sa;
69 	register int sig;
70 	int bit, error;
71 
72 	sig = uap->signo;
73 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
74 		RETURN (EINVAL);
75 	sa = &vec;
76 	if (uap->osa) {
77 		sa->sa_handler = u.u_signal[sig];
78 		sa->sa_mask = u.u_sigmask[sig];
79 		bit = sigmask(sig);
80 		sa->sa_flags = 0;
81 		if ((u.u_sigonstack & bit) != 0)
82 			sa->sa_flags |= SA_ONSTACK;
83 		if ((u.u_sigintr & bit) == 0)
84 			sa->sa_flags |= SA_RESTART;
85 		if (u.u_procp->p_flag & SNOCLDSTOP)
86 			sa->sa_flags |= SA_NOCLDSTOP;
87 		if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
88 		    sizeof (vec)))
89 			RETURN (error);
90 	}
91 	if (uap->nsa) {
92 		if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
93 		    sizeof (vec)))
94 			RETURN (error);
95 		setsigvec(sig, sa);
96 	}
97 	RETURN (0);
98 }
99 
100 setsigvec(sig, sa)
101 	int sig;
102 	register struct sigaction *sa;
103 {
104 	register struct proc *p;
105 	register int bit;
106 
107 	bit = sigmask(sig);
108 	p = u.u_procp;
109 	/*
110 	 * Change setting atomically.
111 	 */
112 	(void) splhigh();
113 	u.u_signal[sig] = sa->sa_handler;
114 	u.u_sigmask[sig] = sa->sa_mask &~ sigcantmask;
115 	if ((sa->sa_flags & SA_RESTART) == 0)
116 		u.u_sigintr |= bit;
117 	else
118 		u.u_sigintr &= ~bit;
119 	if (sa->sa_flags & SA_ONSTACK)
120 		u.u_sigonstack |= bit;
121 	else
122 		u.u_sigonstack &= ~bit;
123 	if (sig == SIGCHLD) {
124 		if (sa->sa_flags & SA_NOCLDSTOP)
125 			p->p_flag |= SNOCLDSTOP;
126 		else
127 			p->p_flag &= ~SNOCLDSTOP;
128 	}
129 	/*
130 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
131 	 * and for signals set to SIG_DFL where the default is to ignore.
132 	 * However, don't put SIGCONT in p_sigignore,
133 	 * as we have to restart the process.
134 	 */
135 	if (sa->sa_handler == SIG_IGN ||
136 	   (bit & defaultignmask && sa->sa_handler == SIG_DFL)) {
137 		p->p_sig &= ~bit;		/* never to be seen again */
138 		if (sig != SIGCONT)
139 			p->p_sigignore |= bit;	/* easier in psignal */
140 		p->p_sigcatch &= ~bit;
141 	} else {
142 		p->p_sigignore &= ~bit;
143 		if (sa->sa_handler == SIG_DFL)
144 			p->p_sigcatch &= ~bit;
145 		else
146 			p->p_sigcatch |= bit;
147 	}
148 	(void) spl0();
149 }
150 
151 /*
152  * Initialize signal state for process 0;
153  * set to ignore signals that are ignored by default.
154  */
155 siginit(p)
156 	struct proc *p;
157 {
158 
159 	p->p_sigignore = defaultignmask &~ sigmask(SIGCONT);
160 }
161 
162 /*
163  * Reset signals for an exec of the specified process.
164  */
165 execsigs(p)
166 	register struct proc *p;
167 {
168 	register int nc, mask;
169 
170 	/*
171 	 * Reset caught signals.  Held signals remain held
172 	 * through p_sigmask (unless they were caught,
173 	 * and are now ignored by default).
174 	 */
175 	while (p->p_sigcatch) {
176 		nc = ffs((long)p->p_sigcatch);
177 		mask = sigmask(nc);
178 		p->p_sigcatch &= ~mask;
179 		if (mask & defaultignmask) {
180 			if (nc != SIGCONT)
181 				p->p_sigignore |= mask;
182 			p->p_sig &= ~mask;
183 		}
184 		u.u_signal[nc] = SIG_DFL;
185 	}
186 	/*
187 	 * Reset stack state to the user stack.
188 	 * Clear set of signals caught on the signal stack.
189 	 */
190 	u.u_onstack = 0;
191 	u.u_sigsp = 0;
192 	u.u_sigonstack = 0;
193 }
194 
195 /*
196  * Manipulate signal mask.
197  * Note that we receive new mask, not pointer,
198  * and return old mask as return value;
199  * the library stub does the rest.
200  */
201 sigprocmask()
202 {
203 	struct a {
204 		int	how;
205 		sigset_t mask;
206 	} *uap = (struct a *)u.u_ap;
207 	register struct proc *p = u.u_procp;
208 	int error = 0;
209 
210 	u.u_r.r_val1 = p->p_sigmask;
211 	(void) splhigh();
212 
213 	switch (uap->how) {
214 	case SIG_BLOCK:
215 		p->p_sigmask |= uap->mask &~ sigcantmask;
216 		break;
217 
218 	case SIG_UNBLOCK:
219 		p->p_sigmask &= ~uap->mask;
220 		break;
221 
222 	case SIG_SETMASK:
223 		p->p_sigmask = uap->mask &~ sigcantmask;
224 		break;
225 
226 	default:
227 		error = EINVAL;
228 		break;
229 	}
230 	(void) spl0();
231 	RETURN (error);
232 }
233 
234 sigpending()
235 {
236 
237 	u.u_r.r_val1 = u.u_procp->p_sig;
238 	RETURN (0);
239 }
240 
241 #ifdef COMPAT_43
242 /*
243  * Generalized interface signal handler, 4.3-compatible.
244  */
245 osigvec()
246 {
247 	register struct a {
248 		int	signo;
249 		struct	sigvec *nsv;
250 		struct	sigvec *osv;
251 	} *uap = (struct a  *)u.u_ap;
252 	struct sigvec vec;
253 	register struct sigvec *sv;
254 	register int sig;
255 	int bit, error;
256 
257 	sig = uap->signo;
258 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
259 		RETURN (EINVAL);
260 	sv = &vec;
261 	if (uap->osv) {
262 		*(sig_t *)&sv->sv_handler = u.u_signal[sig];
263 		sv->sv_mask = u.u_sigmask[sig];
264 		bit = sigmask(sig);
265 		sv->sv_flags = 0;
266 		if ((u.u_sigonstack & bit) != 0)
267 			sv->sv_flags |= SV_ONSTACK;
268 		if ((u.u_sigintr & bit) != 0)
269 			sv->sv_flags |= SV_INTERRUPT;
270 		if (u.u_procp->p_flag & SNOCLDSTOP)
271 			sv->sv_flags |= SA_NOCLDSTOP;
272 		if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
273 		    sizeof (vec)))
274 			RETURN (error);
275 	}
276 	if (uap->nsv) {
277 		if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
278 		    sizeof (vec)))
279 			RETURN (error);
280 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
281 		setsigvec(sig, sv);
282 	}
283 	RETURN (0);
284 }
285 
286 osigblock()
287 {
288 	struct a {
289 		int	mask;
290 	} *uap = (struct a *)u.u_ap;
291 	register struct proc *p = u.u_procp;
292 
293 	(void) splhigh();
294 	u.u_r.r_val1 = p->p_sigmask;
295 	p->p_sigmask |= uap->mask &~ sigcantmask;
296 	(void) spl0();
297 	RETURN (0);
298 }
299 
300 osigsetmask()
301 {
302 	struct a {
303 		int	mask;
304 	} *uap = (struct a *)u.u_ap;
305 	register struct proc *p = u.u_procp;
306 
307 	(void) splhigh();
308 	u.u_r.r_val1 = p->p_sigmask;
309 	p->p_sigmask = uap->mask &~ sigcantmask;
310 	(void) spl0();
311 	RETURN (0);
312 }
313 #endif
314 
315 /*
316  * Suspend process until signal, providing mask to be set
317  * in the meantime.  Note nonstandard calling convention:
318  * libc stub passes mask, not pointer, to save a copyin.
319  */
320 sigsuspend()
321 {
322 	struct a {
323 		sigset_t mask;
324 	} *uap = (struct a *)u.u_ap;
325 	register struct proc *p = u.u_procp;
326 
327 	/*
328 	 * When returning from sigpause, we want
329 	 * the old mask to be restored after the
330 	 * signal handler has finished.  Thus, we
331 	 * save it here and mark the proc structure
332 	 * to indicate this (should be in u.).
333 	 */
334 	u.u_oldmask = p->p_sigmask;
335 	p->p_flag |= SOMASK;
336 	p->p_sigmask = uap->mask &~ sigcantmask;
337 	(void) tsleep((caddr_t)&u, PPAUSE | PCATCH, "pause", 0);
338 	/* always return EINTR rather than ERESTART... */
339 	RETURN (EINTR);
340 }
341 
342 sigstack()
343 {
344 	register struct a {
345 		struct	sigstack *nss;
346 		struct	sigstack *oss;
347 	} *uap = (struct a *)u.u_ap;
348 	struct sigstack ss;
349 	int error = 0;
350 
351 	if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack,
352 	    (caddr_t)uap->oss, sizeof (struct sigstack))))
353 		RETURN (error);
354 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
355 	    sizeof (ss))) == 0)
356 		u.u_sigstack = ss;
357 	RETURN (error);
358 }
359 
360 kill()
361 {
362 	register struct a {
363 		int	pid;
364 		int	signo;
365 	} *uap = (struct a *)u.u_ap;
366 	register struct proc *p;
367 
368 	if ((unsigned) uap->signo >= NSIG)
369 		RETURN (EINVAL);
370 	if (uap->pid > 0) {
371 		/* kill single process */
372 		p = pfind(uap->pid);
373 		if (p == 0)
374 			RETURN (ESRCH);
375 		if (!CANSIGNAL(p, uap->signo))
376 			RETURN (EPERM);
377 		if (uap->signo)
378 			psignal(p, uap->signo);
379 		RETURN (0);
380 	}
381 	switch (uap->pid) {
382 	case -1:		/* broadcast signal */
383 		RETURN (killpg1(uap->signo, 0, 1));
384 	case 0:			/* signal own process group */
385 		RETURN (killpg1(uap->signo, 0, 0));
386 	default:		/* negative explicit process group */
387 		RETURN (killpg1(uap->signo, -uap->pid, 0));
388 	}
389 	/* NOTREACHED */
390 }
391 
392 #ifdef COMPAT_43
393 okillpg()
394 {
395 	register struct a {
396 		int	pgid;
397 		int	signo;
398 	} *uap = (struct a *)u.u_ap;
399 
400 	if ((unsigned) uap->signo >= NSIG)
401 		RETURN (EINVAL);
402 	RETURN (killpg1(uap->signo, uap->pgid, 0));
403 }
404 #endif
405 
406 killpg1(signo, pgid, all)
407 	int signo, pgid, all;
408 {
409 	register struct proc *p;
410 	struct pgrp *pgrp;
411 	int f = 0, error = ESRCH;
412 
413 	if (all)
414 		/*
415 		 * broadcast
416 		 */
417 		for (p = allproc; p != NULL; p = p->p_nxt) {
418 			if (p->p_ppid == 0 || p->p_flag&SSYS ||
419 			    p == u.u_procp || !CANSIGNAL(p, signo))
420 				continue;
421 			f++;
422 			if (signo)
423 				psignal(p, signo);
424 		}
425 	else {
426 		if (pgid == 0)
427 			/*
428 			 * zero pgid means send to my process group.
429 			 */
430 			pgrp = u.u_procp->p_pgrp;
431 		else {
432 			pgrp = pgfind(pgid);
433 			if (pgrp == NULL)
434 				return (ESRCH);
435 		}
436 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
437 			if (p->p_ppid == 0 || p->p_flag&SSYS ||
438 			    !CANSIGNAL(p, signo))
439 				continue;
440 			f++;
441 			if (signo)
442 				psignal(p, signo);
443 		}
444 	}
445 	return (f ? 0 : error);
446 }
447 
448 /* XXX - to be removed, as soon as sockets are changed to operate on pgrps
449  * Send the specified signal to
450  * all processes with 'pgid' as
451  * process group.
452  */
453 gsignal(pgid, sig)
454 {
455 	struct pgrp *pgrp;
456 
457 	if (pgid && (pgrp = pgfind(pgid)))
458 		pgsignal(pgrp, sig);
459 }
460 /*
461  * Send sig to all all members of the process group
462  */
463 pgsignal(pgrp, sig)
464 	struct pgrp *pgrp;
465 {
466 	register struct proc *p;
467 
468 	if (pgrp)
469 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
470 			psignal(p, sig);
471 }
472 
473 /*
474  * Send a signal caused by a trap to the current process.
475  * If it will be caught immediately, deliver it with correct code.
476  * Otherwise, post it normally.
477  */
478 trapsignal(sig, code)
479 	register int sig;
480 	unsigned code;
481 {
482 	register struct proc *p = u.u_procp;
483 	int mask;
484 
485 	mask = sigmask(sig);
486 	if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 &&
487 	    (p->p_sigmask & mask) == 0) {
488 		u.u_ru.ru_nsignals++;
489 #ifdef KTRACE
490 		if (KTRPOINT(p, KTR_PSIG))
491 			ktrpsig(p->p_tracep, sig, u.u_signal[sig],
492 				p->p_sigmask, code);
493 #endif
494 		sendsig(u.u_signal[sig], sig, p->p_sigmask, code);
495 		p->p_sigmask |= u.u_sigmask[sig] | mask;
496 	} else {
497 		u.u_arg[1] = code;	/* XXX for core dump/debugger */
498 		psignal(p, sig);
499 	}
500 }
501 
502 /*
503  * Send the specified signal to the specified process.
504  * Most signals do not do anything directly to a process;
505  * they set a flag that asks the process to do something to itself.
506  * Exceptions:
507  *   o When a stop signal is sent to a sleeping process that takes the default
508  *     action, the process is stopped without awakening it.
509  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
510  *     regardless of the signal action (eg, blocked or ignored).
511  * Other ignored signals are discarded immediately.
512  */
513 psignal(p, sig)
514 	register struct proc *p;
515 	register int sig;
516 {
517 	register int s;
518 	register sig_t action;
519 	int mask;
520 
521 	if ((unsigned)sig >= NSIG || sig == 0)
522 		panic("psignal sig");
523 	mask = sigmask(sig);
524 
525 	/*
526 	 * If proc is traced, always give parent a chance.
527 	 */
528 	if (p->p_flag & STRC)
529 		action = SIG_DFL;
530 	else {
531 		/*
532 		 * If the signal is being ignored,
533 		 * then we forget about it immediately.
534 		 * (Note: we don't set SIGCONT in p_sigignore,
535 		 * and if it is set to SIG_IGN,
536 		 * action will be SIG_DFL here.)
537 		 */
538 		if (p->p_sigignore & mask)
539 			return;
540 		if (p->p_sigmask & mask)
541 			action = SIG_HOLD;
542 		else if (p->p_sigcatch & mask)
543 			action = SIG_CATCH;
544 		else {
545 			if (p->p_pgrp->pg_jobc == 0 && (sig == SIGTTIN ||
546 			    sig == SIGTTOU || sig == SIGTSTP))
547 				return;
548 			action = SIG_DFL;
549 		}
550 	}
551 	switch (sig) {
552 
553 	case SIGTERM:
554 		if ((p->p_flag&STRC) || action != SIG_DFL)
555 			break;
556 		/* FALLTHROUGH */
557 
558 	case SIGKILL:
559 		if (p->p_nice > NZERO)
560 			p->p_nice = NZERO;
561 		break;
562 
563 	case SIGCONT:
564 		p->p_sig &= ~stopsigmask;
565 		break;
566 
567 	case SIGTSTP:
568 	case SIGTTIN:
569 	case SIGTTOU:
570 	case SIGSTOP:
571 		p->p_sig &= ~sigmask(SIGCONT);
572 		break;
573 	}
574 	p->p_sig |= mask;
575 
576 	/*
577 	 * Defer further processing for signals which are held,
578 	 * except that stopped processes must be continued by SIGCONT.
579 	 */
580 	if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP))
581 		return;
582 	s = splhigh();
583 	switch (p->p_stat) {
584 
585 	case SSLEEP:
586 		/*
587 		 * If process is sleeping uninterruptibly
588 		 * we can't interrupt the sleep... the signal will
589 		 * be noticed when the process returns through
590 		 * trap() or syscall().
591 		 */
592 		if ((p->p_flag & SSINTR) == 0)
593 			goto out;
594 		/*
595 		 * Process is sleeping and traced... make it runnable
596 		 * so it can discover the signal in issig() and stop
597 		 * for the parent.
598 		 */
599 		if (p->p_flag&STRC)
600 			goto run;
601 		/*
602 		 * When a sleeping process receives a stop
603 		 * signal, process immediately if possible.
604 		 * All other (caught or default) signals
605 		 * cause the process to run.
606 		 */
607 		if (mask & stopsigmask) {
608 			if (action != SIG_DFL)
609 				goto runfast;
610 			/*
611 			 * If a child in vfork(), stopping could
612 			 * cause deadlock.
613 			 */
614 			if (p->p_flag&SVFORK)
615 				goto out;
616 			p->p_sig &= ~mask;
617 			p->p_cursig = sig;
618 			if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
619 				psignal(p->p_pptr, SIGCHLD);
620 			stop(p);
621 			goto out;
622 		} else
623 			goto runfast;
624 		/*NOTREACHED*/
625 
626 	case SSTOP:
627 		/*
628 		 * If traced process is already stopped,
629 		 * then no further action is necessary.
630 		 */
631 		if (p->p_flag&STRC)
632 			goto out;
633 		switch (sig) {
634 
635 		case SIGKILL:
636 			/*
637 			 * Kill signal always sets processes running.
638 			 */
639 			goto runfast;
640 
641 		case SIGCONT:
642 			/*
643 			 * If SIGCONT is default (or ignored), we continue
644 			 * the process but don't leave the signal in p_sig,
645 			 * as it has no further action.  If SIGCONT is held,
646 			 * continue the process and leave the signal in p_sig.
647 			 * If the process catches SIGCONT, let it handle
648 			 * the signal itself.  If it isn't waiting on
649 			 * an event, then it goes back to run state.
650 			 * Otherwise, process goes back to sleep state.
651 			 */
652 			p->p_cursig = 0;	/* ??? XXX */
653 			if (action == SIG_DFL)
654 				p->p_sig &= ~mask;
655 			if (action == SIG_CATCH)
656 				goto runfast;
657 			if (p->p_wchan == 0)
658 				goto run;
659 			p->p_stat = SSLEEP;
660 			goto out;
661 
662 		case SIGSTOP:
663 		case SIGTSTP:
664 		case SIGTTIN:
665 		case SIGTTOU:
666 			/*
667 			 * Already stopped, don't need to stop again.
668 			 * (If we did the shell could get confused.)
669 			 */
670 			p->p_sig &= ~mask;		/* take it away */
671 			goto out;
672 
673 		default:
674 			/*
675 			 * If process is sleeping interruptibly, then
676 			 * simulate a wakeup so that when it is continued,
677 			 * it will be made runnable and can look at the signal.
678 			 * But don't setrun the process, leave it stopped.
679 			 */
680 			if (p->p_wchan && p->p_flag & SSINTR)
681 				unsleep(p);
682 			goto out;
683 		}
684 		/*NOTREACHED*/
685 
686 	default:
687 		/*
688 		 * SRUN, SIDL, SZOMB do nothing with the signal,
689 		 * other than kicking ourselves if we are running.
690 		 * It will either never be noticed, or noticed very soon.
691 		 */
692 		if (p == u.u_procp && !noproc)
693 			aston();
694 		goto out;
695 	}
696 	/*NOTREACHED*/
697 
698 runfast:
699 	/*
700 	 * Raise priority to at least PUSER.
701 	 */
702 	if (p->p_pri > PUSER)
703 		p->p_pri = PUSER;
704 run:
705 	setrun(p);
706 out:
707 	splx(s);
708 }
709 
710 /*
711  * If the current process has a signal to process (should be caught
712  * or cause termination, should interrupt current syscall),
713  * return the signal number.  Stop signals with default action
714  * are processed immediately, then cleared; they aren't returned.
715  * This is asked at least once each time a process enters the
716  * system (though this can usually be done without actually
717  * calling issig by checking the pending signal masks.)
718  */
719 issig()
720 {
721 	register struct proc *p;
722 	register int sig, mask;
723 
724 	p = u.u_procp;
725 	for (;;) {
726 		mask = p->p_sig &~ p->p_sigmask;
727 		if (p->p_flag&SVFORK)
728 			mask &= ~stopsigmask;
729 		if (mask == 0)	 	/* no signal to send */
730 			return (0);
731 		sig = ffs((long)mask);
732 		mask = sigmask(sig);
733 		/*
734 		 * We should see pending but ignored signals
735 		 * only if STRC was on when they were posted.
736 		 */
737 		if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
738 			p->p_sig &= ~mask;
739 			continue;
740 		}
741 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
742 			/*
743 			 * If traced, always stop, and stay
744 			 * stopped until released by the parent.
745 			 */
746 			p->p_cursig = sig;
747 			psignal(p->p_pptr, SIGCHLD);
748 			do {
749 				stop(p);
750 				swtch();
751 			} while (!procxmt() && p->p_flag&STRC);
752 
753 			/*
754 			 * If the traced bit got turned off,
755 			 * go back up to the top to rescan signals.
756 			 * This ensures that p_sig* and u_signal are consistent.
757 			 */
758 			if ((p->p_flag&STRC) == 0)
759 				continue;
760 
761 			/*
762 			 * If parent wants us to take the signal,
763 			 * then it will leave it in p->p_cursig;
764 			 * otherwise we just look for signals again.
765 			 */
766 			p->p_sig &= ~mask;	/* clear the old signal */
767 			sig = p->p_cursig;
768 			if (sig == 0)
769 				continue;
770 
771 			/*
772 			 * Put the new signal into p_sig.
773 			 * If signal is being masked,
774 			 * look for other signals.
775 			 */
776 			mask = sigmask(sig);
777 			p->p_sig |= mask;
778 			if (p->p_sigmask & mask)
779 				continue;
780 		}
781 
782 		/*
783 		 * Decide whether the signal should be returned.
784 		 * Return the signal's number, or fall through
785 		 * to clear it from the pending mask.
786 		 */
787 		switch ((int)u.u_signal[sig]) {
788 
789 		case SIG_DFL:
790 			/*
791 			 * Don't take default actions on system processes.
792 			 */
793 			if (p->p_ppid == 0)
794 				break;		/* == ignore */
795 			/*
796 			 * If there is a pending stop signal to process
797 			 * with default action, stop here,
798 			 * then clear the signal.
799 			 */
800 			if (mask & stopsigmask) {
801 				if (p->p_flag&STRC)
802 					break;	/* == ignore */
803 				p->p_cursig = sig;
804 				stop(p);
805 				if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
806 					psignal(p->p_pptr, SIGCHLD);
807 				swtch();
808 				break;
809 			} else if (mask & defaultignmask) {
810 				/*
811 				 * Except for SIGCONT, shouldn't get here.
812 				 * Default action is to ignore; drop it.
813 				 */
814 				break;		/* == ignore */
815 			} else
816 				return (sig);
817 			/*NOTREACHED*/
818 
819 		case SIG_IGN:
820 			/*
821 			 * Masking above should prevent us ever trying
822 			 * to take action on an ignored signal other
823 			 * than SIGCONT, unless process is traced.
824 			 */
825 			if (sig != SIGCONT && (p->p_flag&STRC) == 0)
826 				printf("issig\n");
827 			break;		/* == ignore */
828 
829 		default:
830 			/*
831 			 * This signal has an action, let
832 			 * psig process it.
833 			 */
834 			return (sig);
835 		}
836 		p->p_sig &= ~mask;		/* take the signal! */
837 	}
838 	/* NOTREACHED */
839 }
840 
841 /*
842  * Put the argument process into the stopped
843  * state and notify the parent via wakeup.
844  * Signals are handled elsewhere.
845  * The process must not be on the run queue.
846  */
847 stop(p)
848 	register struct proc *p;
849 {
850 
851 	p->p_stat = SSTOP;
852 	p->p_flag &= ~SWTED;
853 	wakeup((caddr_t)p->p_pptr);
854 }
855 
856 /*
857  * Perform the action specified by the current signal.
858  * The usual sequence is:
859  *	if (sig = CURSIG(p))
860  *		psig(sig);
861  */
862 psig(sig)
863 	register int sig;
864 {
865 	register struct proc *p = u.u_procp;
866 	int mask, returnmask;
867 	register sig_t action;
868 
869 	do {
870 #ifdef DIAGNOSTIC
871 		if (sig == 0)
872 			panic("psig");
873 #endif
874 		mask = sigmask(sig);
875 		p->p_sig &= ~mask;
876 		action = u.u_signal[sig];
877 #ifdef KTRACE
878 		if (KTRPOINT(p, KTR_PSIG))
879 			ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ?
880 				u.u_oldmask : p->p_sigmask, 0);
881 #endif
882 		if (action != SIG_DFL) {
883 #ifdef DIAGNOSTIC
884 			if (action == SIG_IGN || (p->p_sigmask & mask))
885 				panic("psig action");
886 #endif
887 			u.u_error = 0;
888 			/*
889 			 * Set the new mask value and also defer further
890 			 * occurences of this signal.
891 			 *
892 			 * Special case: user has done a sigpause.  Here the
893 			 * current mask is not of interest, but rather the
894 			 * mask from before the sigpause is what we want
895 			 * restored after the signal processing is completed.
896 			 */
897 			(void) splhigh();
898 			if (p->p_flag & SOMASK) {
899 				returnmask = u.u_oldmask;
900 				p->p_flag &= ~SOMASK;
901 			} else
902 				returnmask = p->p_sigmask;
903 			p->p_sigmask |= u.u_sigmask[sig] | mask;
904 			(void) spl0();
905 			u.u_ru.ru_nsignals++;
906 			sendsig(action, sig, returnmask, 0);
907 			continue;
908 		}
909 		u.u_acflag |= AXSIG;
910 		switch (sig) {
911 
912 		case SIGILL:
913 		case SIGIOT:
914 		case SIGBUS:
915 		case SIGQUIT:
916 		case SIGTRAP:
917 		case SIGEMT:
918 		case SIGFPE:
919 		case SIGSEGV:
920 		case SIGSYS:
921 			u.u_arg[0] = sig;
922 			if (core() == 0)
923 				sig |= WCOREFLAG;
924 		}
925 		exit(W_EXITCODE(0, sig));
926 		/* NOTREACHED */
927 	} while (sig = CURSIG(p));
928 }
929 
930 /*
931  * Create a core image on the file "core".
932  * It writes UPAGES block of the
933  * user.h area followed by the entire
934  * data+stack segments.
935  */
936 core()
937 {
938 	register struct vnode *vp;
939 	register struct proc *p = u.u_procp;
940 	register struct nameidata *ndp = &u.u_nd;
941 	struct vattr vattr;
942 	int error;
943 
944 	if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid)
945 		return (EFAULT);
946 	if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
947 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
948 		return (EFAULT);
949 	if (p->p_textp) {
950 		VOP_LOCK(p->p_textp->x_vptr);
951 		error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred);
952 		VOP_UNLOCK(p->p_textp->x_vptr);
953 		if (error)
954 			return (EFAULT);
955 	}
956 	ndp->ni_segflg = UIO_SYSSPACE;
957 	ndp->ni_dirp = "core";
958 	if (error = vn_open(ndp, FCREAT|FWRITE, 0644))
959 		return (error);
960 	vp = ndp->ni_vp;
961 	VOP_LOCK(vp);
962 	if (vp->v_type != VREG ||
963 	    VOP_GETATTR(vp, &vattr, u.u_cred) ||
964 	    vattr.va_nlink != 1) {
965 		vput(vp);
966 		return (EFAULT);
967 	}
968 #ifdef MMAP
969 	{ register int fd;
970 	/* unmap funky devices in the user's address space */
971 	for (fd = 0; fd < u.u_lastfile; fd++)
972 		if (u.u_ofile[fd] && (u.u_pofile[fd] & UF_MAPPED))
973 			munmapfd(fd);
974 	}
975 #endif
976 	vattr_null(&vattr);
977 	vattr.va_size = 0;
978 	VOP_SETATTR(vp, &vattr, u.u_cred);
979 	u.u_acflag |= ACORE;
980 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0,
981 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
982 	if (error == 0)
983 		error = vn_rdwr(UIO_WRITE, vp,
984 		    (caddr_t)ctob(dptov(p, 0)),
985 		    (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
986 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
987 	if (error == 0)
988 		error = vn_rdwr(UIO_WRITE, vp,
989 		    (caddr_t)ctob(sptov(p, u.u_ssize - 1)),
990 		    (int)ctob(u.u_ssize),
991 		    (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE,
992 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
993 	vput(vp);
994 	return (error);
995 }
996 
997 /*
998  * Nonexistent system call-- signal process (may want to handle it).
999  * Flag error in case process won't see signal immediately (blocked or ignored).
1000  */
1001 nosys()
1002 {
1003 
1004 	psignal(u.u_procp, SIGSYS);
1005 	u.u_error = EINVAL;
1006 }
1007