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