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