xref: /csrg-svn/sys/kern/kern_sig.c (revision 44440)
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.23 (Berkeley) 06/28/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 	case SIGSTOP:
586 		p->p_sig &= ~sigmask(SIGCONT);
587 		break;
588 	}
589 	p->p_sig |= mask;
590 
591 	/*
592 	 * Defer further processing for signals which are held,
593 	 * except that stopped processes must be continued by SIGCONT.
594 	 */
595 	if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP))
596 		return;
597 	s = splhigh();
598 	switch (p->p_stat) {
599 
600 	case SSLEEP:
601 		/*
602 		 * If process is sleeping uninterruptibly
603 		 * we can't interrupt the sleep... the signal will
604 		 * be noticed when the process returns through
605 		 * trap() or syscall().
606 		 */
607 		if ((p->p_flag & SSINTR) == 0)
608 			goto out;
609 		/*
610 		 * Process is sleeping and traced... make it runnable
611 		 * so it can discover the signal in issig() and stop
612 		 * for the parent.
613 		 */
614 		if (p->p_flag&STRC)
615 			goto run;
616 		/*
617 		 * When a sleeping process receives a stop
618 		 * signal, process immediately if possible.
619 		 * All other (caught or default) signals
620 		 * cause the process to run.
621 		 */
622 		if (mask & stopsigmask) {
623 			if (action != SIG_DFL)
624 				goto runfast;
625 			/*
626 			 * If a child in vfork(), stopping could
627 			 * cause deadlock.
628 			 */
629 			if (p->p_flag&SVFORK)
630 				goto out;
631 			p->p_sig &= ~mask;
632 			p->p_xstat = sig;
633 			if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
634 				psignal(p->p_pptr, SIGCHLD);
635 			stop(p);
636 			goto out;
637 		} else
638 			goto runfast;
639 		/*NOTREACHED*/
640 
641 	case SSTOP:
642 		/*
643 		 * If traced process is already stopped,
644 		 * then no further action is necessary.
645 		 */
646 		if (p->p_flag&STRC)
647 			goto out;
648 		switch (sig) {
649 
650 		case SIGKILL:
651 			/*
652 			 * Kill signal always sets processes running.
653 			 */
654 			goto runfast;
655 
656 		case SIGCONT:
657 			/*
658 			 * If SIGCONT is default (or ignored), we continue
659 			 * the process but don't leave the signal in p_sig,
660 			 * as it has no further action.  If SIGCONT is held,
661 			 * continue the process and leave the signal in p_sig.
662 			 * If the process catches SIGCONT, let it handle
663 			 * the signal itself.  If it isn't waiting on
664 			 * an event, then it goes back to run state.
665 			 * Otherwise, process goes back to sleep state.
666 			 */
667 			if (action == SIG_DFL)
668 				p->p_sig &= ~mask;
669 			if (action == SIG_CATCH)
670 				goto runfast;
671 			if (p->p_wchan == 0)
672 				goto run;
673 			p->p_stat = SSLEEP;
674 			goto out;
675 
676 		case SIGSTOP:
677 		case SIGTSTP:
678 		case SIGTTIN:
679 		case SIGTTOU:
680 			/*
681 			 * Already stopped, don't need to stop again.
682 			 * (If we did the shell could get confused.)
683 			 */
684 			p->p_sig &= ~mask;		/* take it away */
685 			goto out;
686 
687 		default:
688 			/*
689 			 * If process is sleeping interruptibly, then
690 			 * simulate a wakeup so that when it is continued,
691 			 * it will be made runnable and can look at the signal.
692 			 * But don't setrun the process, leave it stopped.
693 			 */
694 			if (p->p_wchan && p->p_flag & SSINTR)
695 				unsleep(p);
696 			goto out;
697 		}
698 		/*NOTREACHED*/
699 
700 	default:
701 		/*
702 		 * SRUN, SIDL, SZOMB do nothing with the signal,
703 		 * other than kicking ourselves if we are running.
704 		 * It will either never be noticed, or noticed very soon.
705 		 */
706 		if (p == u.u_procp && !noproc)
707 			aston();
708 		goto out;
709 	}
710 	/*NOTREACHED*/
711 
712 runfast:
713 	/*
714 	 * Raise priority to at least PUSER.
715 	 */
716 	if (p->p_pri > PUSER)
717 		p->p_pri = PUSER;
718 run:
719 	setrun(p);
720 out:
721 	splx(s);
722 }
723 
724 /*
725  * If the current process has a signal to process (should be caught
726  * or cause termination, should interrupt current syscall),
727  * return the signal number.  Stop signals with default action
728  * are processed immediately, then cleared; they aren't returned.
729  * This is asked at least once each time a process enters the
730  * system (though this can usually be done without actually
731  * calling issig by checking the pending signal masks.)
732  */
733 issig()
734 {
735 	register struct proc *p = u.u_procp;		/* XXX */
736 	register int sig, mask;
737 
738 	for (;;) {
739 		mask = p->p_sig &~ p->p_sigmask;
740 		if (p->p_flag&SVFORK)
741 			mask &= ~stopsigmask;
742 		if (mask == 0)	 	/* no signal to send */
743 			return (0);
744 		sig = ffs((long)mask);
745 		mask = sigmask(sig);
746 		/*
747 		 * We should see pending but ignored signals
748 		 * only if STRC was on when they were posted.
749 		 */
750 		if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
751 			p->p_sig &= ~mask;
752 			continue;
753 		}
754 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
755 			/*
756 			 * If traced, always stop, and stay
757 			 * stopped until released by the parent.
758 			 */
759 			p->p_xstat = sig;
760 			psignal(p->p_pptr, SIGCHLD);
761 			do {
762 				stop(p);
763 				swtch();
764 			} while (!procxmt(p) && p->p_flag&STRC);
765 
766 			/*
767 			 * If the traced bit got turned off,
768 			 * go back up to the top to rescan signals.
769 			 * This ensures that p_sig* and u_signal are consistent.
770 			 */
771 			if ((p->p_flag&STRC) == 0)
772 				continue;
773 
774 			/*
775 			 * If parent wants us to take the signal,
776 			 * then it will leave it in p->p_xstat;
777 			 * otherwise we just look for signals again.
778 			 */
779 			p->p_sig &= ~mask;	/* clear the old signal */
780 			sig = p->p_xstat;
781 			if (sig == 0)
782 				continue;
783 
784 			/*
785 			 * Put the new signal into p_sig.
786 			 * If signal is being masked,
787 			 * look for other signals.
788 			 */
789 			mask = sigmask(sig);
790 			p->p_sig |= mask;
791 			if (p->p_sigmask & mask)
792 				continue;
793 		}
794 
795 		/*
796 		 * Decide whether the signal should be returned.
797 		 * Return the signal's number, or fall through
798 		 * to clear it from the pending mask.
799 		 */
800 		switch ((int)u.u_signal[sig]) {
801 
802 		case SIG_DFL:
803 			/*
804 			 * Don't take default actions on system processes.
805 			 */
806 			if (p->p_ppid == 0)
807 				break;		/* == ignore */
808 			/*
809 			 * If there is a pending stop signal to process
810 			 * with default action, stop here,
811 			 * then clear the signal.  However,
812 			 * if process is member of an orphaned
813 			 * process group, ignore tty stop signals.
814 			 */
815 			if (mask & stopsigmask) {
816 				if (p->p_flag&STRC ||
817 		    		    (p->p_pgrp->pg_jobc == 0 &&
818 				    mask & ttystopsigmask))
819 					break;	/* == ignore */
820 				p->p_xstat = sig;
821 				stop(p);
822 				if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
823 					psignal(p->p_pptr, SIGCHLD);
824 				swtch();
825 				break;
826 			} else if (mask & defaultignmask) {
827 				/*
828 				 * Except for SIGCONT, shouldn't get here.
829 				 * Default action is to ignore; drop it.
830 				 */
831 				break;		/* == ignore */
832 			} else
833 				return (sig);
834 			/*NOTREACHED*/
835 
836 		case SIG_IGN:
837 			/*
838 			 * Masking above should prevent us ever trying
839 			 * to take action on an ignored signal other
840 			 * than SIGCONT, unless process is traced.
841 			 */
842 			if (sig != SIGCONT && (p->p_flag&STRC) == 0)
843 				printf("issig\n");
844 			break;		/* == ignore */
845 
846 		default:
847 			/*
848 			 * This signal has an action, let
849 			 * psig process it.
850 			 */
851 			return (sig);
852 		}
853 		p->p_sig &= ~mask;		/* take the signal! */
854 	}
855 	/* NOTREACHED */
856 }
857 
858 /*
859  * Put the argument process into the stopped
860  * state and notify the parent via wakeup.
861  * Signals are handled elsewhere.
862  * The process must not be on the run queue.
863  */
864 stop(p)
865 	register struct proc *p;
866 {
867 
868 	p->p_stat = SSTOP;
869 	p->p_flag &= ~SWTED;
870 	wakeup((caddr_t)p->p_pptr);
871 }
872 
873 /*
874  * Perform the action specified by the current signal.
875  * The usual sequence is:
876  *	if (sig = CURSIG(p))
877  *		psig(sig);
878  */
879 psig(sig)
880 	register int sig;
881 {
882 	register struct proc *p = u.u_procp;
883 	int mask, returnmask;
884 	register sig_t action;
885 
886 	do {
887 #ifdef DIAGNOSTIC
888 		if (sig == 0)
889 			panic("psig");
890 #endif
891 		mask = sigmask(sig);
892 		p->p_sig &= ~mask;
893 		action = u.u_signal[sig];
894 #ifdef KTRACE
895 		if (KTRPOINT(p, KTR_PSIG))
896 			ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ?
897 				u.u_oldmask : p->p_sigmask, 0);
898 #endif
899 		if (action != SIG_DFL) {
900 #ifdef DIAGNOSTIC
901 			if (action == SIG_IGN || (p->p_sigmask & mask))
902 				panic("psig action");
903 #endif
904 			/*
905 			 * Set the new mask value and also defer further
906 			 * occurences of this signal.
907 			 *
908 			 * Special case: user has done a sigpause.  Here the
909 			 * current mask is not of interest, but rather the
910 			 * mask from before the sigpause is what we want
911 			 * restored after the signal processing is completed.
912 			 */
913 			(void) splhigh();
914 			if (p->p_flag & SOMASK) {
915 				returnmask = u.u_oldmask;
916 				p->p_flag &= ~SOMASK;
917 			} else
918 				returnmask = p->p_sigmask;
919 			p->p_sigmask |= u.u_sigmask[sig] | mask;
920 			(void) spl0();
921 			u.u_ru.ru_nsignals++;
922 			sendsig(action, sig, returnmask, 0);
923 			continue;
924 		}
925 		u.u_acflag |= AXSIG;
926 		switch (sig) {
927 
928 		case SIGILL:
929 		case SIGIOT:
930 		case SIGBUS:
931 		case SIGQUIT:
932 		case SIGTRAP:
933 		case SIGEMT:
934 		case SIGFPE:
935 		case SIGSEGV:
936 		case SIGSYS:
937 			u.u_sig = sig;
938 			if (core() == 0)
939 				sig |= WCOREFLAG;
940 		}
941 		exit(p, W_EXITCODE(0, sig));
942 		/* NOTREACHED */
943 	} while (sig = CURSIG(p));
944 }
945 
946 /*
947  * Create a core image on the file "core".
948  * It writes UPAGES block of the
949  * user.h area followed by the entire
950  * data+stack segments.
951  */
952 core()
953 {
954 	register struct vnode *vp;
955 	register struct proc *p = u.u_procp;
956 	register struct nameidata *ndp = &u.u_nd;
957 	struct vattr vattr;
958 	int error;
959 
960 	if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid)
961 		return (EFAULT);
962 	if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
963 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
964 		return (EFAULT);
965 	if (p->p_textp) {
966 		VOP_LOCK(p->p_textp->x_vptr);
967 		error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred);
968 		VOP_UNLOCK(p->p_textp->x_vptr);
969 		if (error)
970 			return (EFAULT);
971 	}
972 	ndp->ni_segflg = UIO_SYSSPACE;
973 	ndp->ni_dirp = "core";
974 	if (error = vn_open(ndp, FCREAT|FWRITE, 0644))
975 		return (error);
976 	vp = ndp->ni_vp;
977 	VOP_LOCK(vp);
978 	if (vp->v_type != VREG ||
979 	    VOP_GETATTR(vp, &vattr, u.u_cred) ||
980 	    vattr.va_nlink != 1) {
981 		vput(vp);
982 		return (EFAULT);
983 	}
984 #ifdef MAPMEM
985 	if (error = mmcore(p)) {
986 		vput(vp);
987 		return (error);
988 	}
989 #endif
990 	VATTR_NULL(&vattr);
991 	vattr.va_size = 0;
992 	VOP_SETATTR(vp, &vattr, u.u_cred);
993 	u.u_acflag |= ACORE;
994 #ifdef HPUXCOMPAT
995 	/*
996 	 * BLETCH!  If we loaded from an HPUX format binary file
997 	 * we have to dump an HPUX style user struct so that the
998 	 * HPUX debuggers can grok it.
999 	 */
1000 	if (u.u_pcb.pcb_flags & PCB_HPUXBIN)
1001 		error = hpuxdumpu(vp, ndp->ni_cred);
1002 	else
1003 #endif
1004 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0,
1005 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
1006 	if (error == 0)
1007 		error = vn_rdwr(UIO_WRITE, vp,
1008 		    (caddr_t)ctob(dptov(p, 0)),
1009 		    (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
1010 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
1011 	if (error == 0)
1012 		error = vn_rdwr(UIO_WRITE, vp,
1013 		    (caddr_t)ctob(sptov(p, u.u_ssize - 1)),
1014 		    (int)ctob(u.u_ssize),
1015 		    (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE,
1016 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
1017 	vput(vp);
1018 	return (error);
1019 }
1020 
1021 /*
1022  * Nonexistent system call-- signal process (may want to handle it).
1023  * Flag error in case process won't see signal immediately (blocked or ignored).
1024  */
1025 /* ARGSUSED */
1026 nosys(p, args, retval)
1027 	struct proc *p;
1028 	void *args;
1029 	int *retval;
1030 {
1031 
1032 	psignal(p, SIGSYS);
1033 	return (EINVAL);
1034 }
1035