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