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