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