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