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