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