xref: /freebsd/sys/kern/kern_sig.c (revision 48991a368427cadb9cdac39581d1676c29619c52)
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.13 1995/11/12 06:43:00 bde 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 extern int killpg1	__P((struct proc *cp, int signum, int pgid, int all));
68 extern void killproc	__P((struct proc *p, char *why));
69 extern void stop	__P((struct proc *));
70 
71 /*
72  * Can process p, with pcred pc, send the signal signum to process q?
73  */
74 #define CANSIGNAL(p, pc, q, signum) \
75 	((pc)->pc_ucred->cr_uid == 0 || \
76 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
77 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
78 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
79 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
80 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
81 
82 #ifndef _SYS_SYSPROTO_H_
83 struct sigaction_args {
84 	int	signum;
85 	struct	sigaction *nsa;
86 	struct	sigaction *osa;
87 };
88 #endif
89 /* ARGSUSED */
90 int
91 sigaction(p, uap, retval)
92 	struct proc *p;
93 	register struct sigaction_args *uap;
94 	int *retval;
95 {
96 	struct sigaction vec;
97 	register struct sigaction *sa;
98 	register struct sigacts *ps = p->p_sigacts;
99 	register int signum;
100 	int bit, error;
101 
102 	signum = uap->signum;
103 	if (signum <= 0 || signum >= NSIG ||
104 	    signum == SIGKILL || signum == SIGSTOP)
105 		return (EINVAL);
106 	sa = &vec;
107 	if (uap->osa) {
108 		sa->sa_handler = ps->ps_sigact[signum];
109 		sa->sa_mask = ps->ps_catchmask[signum];
110 		bit = sigmask(signum);
111 		sa->sa_flags = 0;
112 		if ((ps->ps_sigonstack & bit) != 0)
113 			sa->sa_flags |= SA_ONSTACK;
114 		if ((ps->ps_sigintr & bit) == 0)
115 			sa->sa_flags |= SA_RESTART;
116 		if ((ps->ps_nodefer & bit) != 0)
117 			sa->sa_flags |= SA_NODEFER;
118 		if (p->p_flag & P_NOCLDSTOP)
119 			sa->sa_flags |= SA_NOCLDSTOP;
120 		if ((error = copyout((caddr_t)sa, (caddr_t)uap->osa,
121 		    sizeof (vec))))
122 			return (error);
123 	}
124 	if (uap->nsa) {
125 		if ((error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
126 		    sizeof (vec))))
127 			return (error);
128 		setsigvec(p, signum, sa);
129 	}
130 	return (0);
131 }
132 
133 void
134 setsigvec(p, signum, sa)
135 	register struct proc *p;
136 	int signum;
137 	register struct sigaction *sa;
138 {
139 	register struct sigacts *ps = p->p_sigacts;
140 	register int bit;
141 
142 	bit = sigmask(signum);
143 	/*
144 	 * Change setting atomically.
145 	 */
146 	(void) splhigh();
147 	ps->ps_sigact[signum] = sa->sa_handler;
148 	ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
149 	if ((sa->sa_flags & SA_RESTART) == 0)
150 		ps->ps_sigintr |= bit;
151 	else
152 		ps->ps_sigintr &= ~bit;
153 	if (sa->sa_flags & SA_ONSTACK)
154 		ps->ps_sigonstack |= bit;
155 	else
156 		ps->ps_sigonstack &= ~bit;
157 	if (sa->sa_flags & SA_NODEFER)
158 		ps->ps_nodefer |= bit;
159 	else
160 		ps->ps_nodefer &= ~bit;
161 #ifdef COMPAT_SUNOS
162 	if (sa->sa_flags & SA_USERTRAMP)
163 		ps->ps_usertramp |= bit;
164 	else
165 		ps->ps_usertramp &= ~bit;
166 #endif
167 	if (signum == SIGCHLD) {
168 		if (sa->sa_flags & SA_NOCLDSTOP)
169 			p->p_flag |= P_NOCLDSTOP;
170 		else
171 			p->p_flag &= ~P_NOCLDSTOP;
172 	}
173 	/*
174 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
175 	 * and for signals set to SIG_DFL where the default is to ignore.
176 	 * However, don't put SIGCONT in p_sigignore,
177 	 * as we have to restart the process.
178 	 */
179 	if (sa->sa_handler == SIG_IGN ||
180 	    (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
181 		p->p_siglist &= ~bit;		/* never to be seen again */
182 		if (signum != SIGCONT)
183 			p->p_sigignore |= bit;	/* easier in psignal */
184 		p->p_sigcatch &= ~bit;
185 	} else {
186 		p->p_sigignore &= ~bit;
187 		if (sa->sa_handler == SIG_DFL)
188 			p->p_sigcatch &= ~bit;
189 		else
190 			p->p_sigcatch |= bit;
191 	}
192 	(void) spl0();
193 }
194 
195 /*
196  * Initialize signal state for process 0;
197  * set to ignore signals that are ignored by default.
198  */
199 void
200 siginit(p)
201 	struct proc *p;
202 {
203 	register int i;
204 
205 	for (i = 0; i < NSIG; i++)
206 		if (sigprop[i] & SA_IGNORE && i != SIGCONT)
207 			p->p_sigignore |= sigmask(i);
208 }
209 
210 /*
211  * Reset signals for an exec of the specified process.
212  */
213 void
214 execsigs(p)
215 	register struct proc *p;
216 {
217 	register struct sigacts *ps = p->p_sigacts;
218 	register int nc, mask;
219 
220 	/*
221 	 * Reset caught signals.  Held signals remain held
222 	 * through p_sigmask (unless they were caught,
223 	 * and are now ignored by default).
224 	 */
225 	while (p->p_sigcatch) {
226 		nc = ffs((long)p->p_sigcatch);
227 		mask = sigmask(nc);
228 		p->p_sigcatch &= ~mask;
229 		if (sigprop[nc] & SA_IGNORE) {
230 			if (nc != SIGCONT)
231 				p->p_sigignore |= mask;
232 			p->p_siglist &= ~mask;
233 		}
234 		ps->ps_sigact[nc] = SIG_DFL;
235 	}
236 	/*
237 	 * Reset stack state to the user stack.
238 	 * Clear set of signals caught on the signal stack.
239 	 */
240 	ps->ps_sigstk.ss_flags = SA_DISABLE;
241 	ps->ps_sigstk.ss_size = 0;
242 	ps->ps_sigstk.ss_sp = 0;
243 	ps->ps_flags = 0;
244 }
245 
246 /*
247  * Manipulate signal mask.
248  * Note that we receive new mask, not pointer,
249  * and return old mask as return value;
250  * the library stub does the rest.
251  */
252 #ifndef _SYS_SYSPROTO_H_
253 struct sigprocmask_args {
254 	int	how;
255 	sigset_t mask;
256 };
257 #endif
258 int
259 sigprocmask(p, uap, retval)
260 	register struct proc *p;
261 	struct sigprocmask_args *uap;
262 	int *retval;
263 {
264 	int error = 0;
265 
266 	*retval = p->p_sigmask;
267 	(void) splhigh();
268 
269 	switch (uap->how) {
270 	case SIG_BLOCK:
271 		p->p_sigmask |= uap->mask &~ sigcantmask;
272 		break;
273 
274 	case SIG_UNBLOCK:
275 		p->p_sigmask &= ~uap->mask;
276 		break;
277 
278 	case SIG_SETMASK:
279 		p->p_sigmask = uap->mask &~ sigcantmask;
280 		break;
281 
282 	default:
283 		error = EINVAL;
284 		break;
285 	}
286 	(void) spl0();
287 	return (error);
288 }
289 
290 #ifndef _SYS_SYSPROTO_H_
291 struct sigpending_args {
292 	int	dummy;
293 };
294 #endif
295 /* ARGSUSED */
296 int
297 sigpending(p, uap, retval)
298 	struct proc *p;
299 	struct sigpending_args *uap;
300 	int *retval;
301 {
302 
303 	*retval = p->p_siglist;
304 	return (0);
305 }
306 
307 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
308 /*
309  * Generalized interface signal handler, 4.3-compatible.
310  */
311 #ifndef _SYS_SYSPROTO_H_
312 struct osigvec_args {
313 	int	signum;
314 	struct	sigvec *nsv;
315 	struct	sigvec *osv;
316 };
317 #endif
318 /* ARGSUSED */
319 int
320 osigvec(p, uap, retval)
321 	struct proc *p;
322 	register struct osigvec_args *uap;
323 	int *retval;
324 {
325 	struct sigvec vec;
326 	register struct sigacts *ps = p->p_sigacts;
327 	register struct sigvec *sv;
328 	register int signum;
329 	int bit, error;
330 
331 	signum = uap->signum;
332 	if (signum <= 0 || signum >= NSIG ||
333 	    signum == SIGKILL || signum == SIGSTOP)
334 		return (EINVAL);
335 	sv = &vec;
336 	if (uap->osv) {
337 		*(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
338 		sv->sv_mask = ps->ps_catchmask[signum];
339 		bit = sigmask(signum);
340 		sv->sv_flags = 0;
341 		if ((ps->ps_sigonstack & bit) != 0)
342 			sv->sv_flags |= SV_ONSTACK;
343 		if ((ps->ps_sigintr & bit) != 0)
344 			sv->sv_flags |= SV_INTERRUPT;
345 #ifndef COMPAT_SUNOS
346 		if (p->p_flag & P_NOCLDSTOP)
347 			sv->sv_flags |= SA_NOCLDSTOP;
348 #endif
349 		if ((error = copyout((caddr_t)sv, (caddr_t)uap->osv,
350 		    sizeof (vec))))
351 			return (error);
352 	}
353 	if (uap->nsv) {
354 		if ((error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
355 		    sizeof (vec))))
356 			return (error);
357 #ifdef COMPAT_SUNOS
358 		/*
359 		 * SunOS uses this bit (4, aka SA_DISABLE) as SV_RESETHAND,
360 		 * `reset to SIG_DFL on delivery'. We have no such option
361 		 * now or ever!
362 		 */
363 		if (sv->sv_flags & SA_DISABLE)
364 			return (EINVAL);
365 		sv->sv_flags |= SA_USERTRAMP;
366 #endif
367 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
368 		setsigvec(p, signum, (struct sigaction *)sv);
369 	}
370 	return (0);
371 }
372 
373 #ifndef _SYS_SYSPROTO_H_
374 struct osigblock_args {
375 	int	mask;
376 };
377 #endif
378 int
379 osigblock(p, uap, retval)
380 	register struct proc *p;
381 	struct osigblock_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 
392 #ifndef _SYS_SYSPROTO_H_
393 struct osigsetmask_args {
394 	int	mask;
395 };
396 #endif
397 int
398 osigsetmask(p, uap, retval)
399 	struct proc *p;
400 	struct osigsetmask_args *uap;
401 	int *retval;
402 {
403 
404 	(void) splhigh();
405 	*retval = p->p_sigmask;
406 	p->p_sigmask = uap->mask &~ sigcantmask;
407 	(void) spl0();
408 	return (0);
409 }
410 #endif /* COMPAT_43 || COMPAT_SUNOS */
411 
412 /*
413  * Suspend process until signal, providing mask to be set
414  * in the meantime.  Note nonstandard calling convention:
415  * libc stub passes mask, not pointer, to save a copyin.
416  */
417 #ifndef _SYS_SYSPROTO_H_
418 struct sigsuspend_args {
419 	sigset_t mask;
420 };
421 #endif
422 /* ARGSUSED */
423 int
424 sigsuspend(p, uap, retval)
425 	register struct proc *p;
426 	struct sigsuspend_args *uap;
427 	int *retval;
428 {
429 	register struct sigacts *ps = p->p_sigacts;
430 
431 	/*
432 	 * When returning from sigpause, we want
433 	 * the old mask to be restored after the
434 	 * signal handler has finished.  Thus, we
435 	 * save it here and mark the sigacts structure
436 	 * to indicate this.
437 	 */
438 	ps->ps_oldmask = p->p_sigmask;
439 	ps->ps_flags |= SAS_OLDMASK;
440 	p->p_sigmask = uap->mask &~ sigcantmask;
441 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
442 		/* void */;
443 	/* always return EINTR rather than ERESTART... */
444 	return (EINTR);
445 }
446 
447 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
448 #ifndef _SYS_SYSPROTO_H_
449 struct osigstack_args {
450 	struct	sigstack *nss;
451 	struct	sigstack *oss;
452 };
453 #endif
454 /* ARGSUSED */
455 int
456 osigstack(p, uap, retval)
457 	struct proc *p;
458 	register struct osigstack_args *uap;
459 	int *retval;
460 {
461 	struct sigstack ss;
462 	struct sigacts *psp;
463 	int error = 0;
464 
465 	psp = p->p_sigacts;
466 	ss.ss_sp = psp->ps_sigstk.ss_sp;
467 	ss.ss_onstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
468 	if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
469 	    sizeof (struct sigstack))))
470 		return (error);
471 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
472 	    sizeof (ss))) == 0) {
473 		psp->ps_sigstk.ss_sp = ss.ss_sp;
474 		psp->ps_sigstk.ss_size = 0;
475 		psp->ps_sigstk.ss_flags |= ss.ss_onstack & SA_ONSTACK;
476 		psp->ps_flags |= SAS_ALTSTACK;
477 	}
478 	return (error);
479 }
480 #endif /* COMPAT_43 || COMPAT_SUNOS */
481 
482 #ifndef _SYS_SYSPROTO_H_
483 struct sigaltstack_args {
484 	struct	sigaltstack *nss;
485 	struct	sigaltstack *oss;
486 };
487 #endif
488 /* ARGSUSED */
489 int
490 sigaltstack(p, uap, retval)
491 	struct proc *p;
492 	register struct sigaltstack_args *uap;
493 	int *retval;
494 {
495 	struct sigacts *psp;
496 	struct sigaltstack ss;
497 	int error;
498 
499 	psp = p->p_sigacts;
500 	if ((psp->ps_flags & SAS_ALTSTACK) == 0)
501 		psp->ps_sigstk.ss_flags |= SA_DISABLE;
502 	if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
503 	    (caddr_t)uap->oss, sizeof (struct sigaltstack))))
504 		return (error);
505 	if (uap->nss == 0)
506 		return (0);
507 	if ((error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss))))
508 		return (error);
509 	if (ss.ss_flags & SA_DISABLE) {
510 		if (psp->ps_sigstk.ss_flags & SA_ONSTACK)
511 			return (EINVAL);
512 		psp->ps_flags &= ~SAS_ALTSTACK;
513 		psp->ps_sigstk.ss_flags = ss.ss_flags;
514 		return (0);
515 	}
516 	if (ss.ss_size < MINSIGSTKSZ)
517 		return (ENOMEM);
518 	psp->ps_flags |= SAS_ALTSTACK;
519 	psp->ps_sigstk= ss;
520 	return (0);
521 }
522 
523 /*
524  * Common code for kill process group/broadcast kill.
525  * cp is calling process.
526  */
527 int
528 killpg1(cp, signum, pgid, all)
529 	register struct proc *cp;
530 	int signum, pgid, all;
531 {
532 	register struct proc *p;
533 	register struct pcred *pc = cp->p_cred;
534 	struct pgrp *pgrp;
535 	int nfound = 0;
536 
537 	if (all)
538 		/*
539 		 * broadcast
540 		 */
541 		for (p = (struct proc *)allproc; p != NULL; p = p->p_next) {
542 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
543 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
544 				continue;
545 			nfound++;
546 			if (signum)
547 				psignal(p, signum);
548 		}
549 	else {
550 		if (pgid == 0)
551 			/*
552 			 * zero pgid means send to my process group.
553 			 */
554 			pgrp = cp->p_pgrp;
555 		else {
556 			pgrp = pgfind(pgid);
557 			if (pgrp == NULL)
558 				return (ESRCH);
559 		}
560 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
561 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
562 			    p->p_stat == SZOMB ||
563 			    !CANSIGNAL(cp, pc, p, signum))
564 				continue;
565 			nfound++;
566 			if (signum)
567 				psignal(p, signum);
568 		}
569 	}
570 	return (nfound ? 0 : ESRCH);
571 }
572 
573 #ifndef _SYS_SYSPROTO_H_
574 struct kill_args {
575 	int	pid;
576 	int	signum;
577 };
578 #endif
579 /* ARGSUSED */
580 int
581 kill(cp, uap, retval)
582 	register struct proc *cp;
583 	register struct kill_args *uap;
584 	int *retval;
585 {
586 	register struct proc *p;
587 	register struct pcred *pc = cp->p_cred;
588 
589 	if ((u_int)uap->signum >= NSIG)
590 		return (EINVAL);
591 	if (uap->pid > 0) {
592 		/* kill single process */
593 		if ((p = pfind(uap->pid)) == NULL)
594 			return (ESRCH);
595 		if (!CANSIGNAL(cp, pc, p, uap->signum))
596 			return (EPERM);
597 		if (uap->signum)
598 			psignal(p, uap->signum);
599 		return (0);
600 	}
601 	switch (uap->pid) {
602 	case -1:		/* broadcast signal */
603 		return (killpg1(cp, uap->signum, 0, 1));
604 	case 0:			/* signal own process group */
605 		return (killpg1(cp, uap->signum, 0, 0));
606 	default:		/* negative explicit process group */
607 		return (killpg1(cp, uap->signum, -uap->pid, 0));
608 	}
609 	/* NOTREACHED */
610 }
611 
612 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
613 #ifndef _SYS_SYSPROTO_H_
614 struct okillpg_args {
615 	int	pgid;
616 	int	signum;
617 };
618 #endif
619 /* ARGSUSED */
620 int
621 okillpg(p, uap, retval)
622 	struct proc *p;
623 	register struct okillpg_args *uap;
624 	int *retval;
625 {
626 
627 	if ((u_int)uap->signum >= NSIG)
628 		return (EINVAL);
629 	return (killpg1(p, uap->signum, uap->pgid, 0));
630 }
631 #endif /* COMPAT_43 || COMPAT_SUNOS */
632 
633 /*
634  * Send a signal to a process group.
635  */
636 void
637 gsignal(pgid, signum)
638 	int pgid, signum;
639 {
640 	struct pgrp *pgrp;
641 
642 	if (pgid && (pgrp = pgfind(pgid)))
643 		pgsignal(pgrp, signum, 0);
644 }
645 
646 /*
647  * Send a signal to a  process group.  If checktty is 1,
648  * limit to members which have a controlling terminal.
649  */
650 void
651 pgsignal(pgrp, signum, checkctty)
652 	struct pgrp *pgrp;
653 	int signum, checkctty;
654 {
655 	register struct proc *p;
656 
657 	if (pgrp)
658 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
659 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
660 				psignal(p, signum);
661 }
662 
663 /*
664  * Send a signal caused by a trap to the current process.
665  * If it will be caught immediately, deliver it with correct code.
666  * Otherwise, post it normally.
667  */
668 void
669 trapsignal(p, signum, code)
670 	struct proc *p;
671 	register int signum;
672 	u_int code;
673 {
674 	register struct sigacts *ps = p->p_sigacts;
675 	int mask;
676 
677 	mask = sigmask(signum);
678 	if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
679 	    (p->p_sigmask & mask) == 0) {
680 		p->p_stats->p_ru.ru_nsignals++;
681 #ifdef KTRACE
682 		if (KTRPOINT(p, KTR_PSIG))
683 			ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
684 				p->p_sigmask, code);
685 #endif
686 		sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, code);
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