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