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