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