xref: /netbsd-src/sys/kern/kern_sig.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: kern_sig.c,v 1.85 1998/11/13 17:23:52 mycroft Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
41  */
42 
43 #include "opt_ktrace.h"
44 #include "opt_uvm.h"
45 #include "opt_compat_sunos.h"
46 
47 #define	SIGPROP		/* include signal properties table */
48 #include <sys/param.h>
49 #include <sys/signalvar.h>
50 #include <sys/resourcevar.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/proc.h>
54 #include <sys/systm.h>
55 #include <sys/timeb.h>
56 #include <sys/times.h>
57 #include <sys/buf.h>
58 #include <sys/acct.h>
59 #include <sys/file.h>
60 #include <sys/kernel.h>
61 #include <sys/wait.h>
62 #include <sys/ktrace.h>
63 #include <sys/syslog.h>
64 #include <sys/stat.h>
65 #include <sys/core.h>
66 #include <sys/ptrace.h>
67 #include <sys/filedesc.h>
68 
69 #include <sys/mount.h>
70 #include <sys/syscallargs.h>
71 
72 #include <machine/cpu.h>
73 
74 #include <vm/vm.h>
75 #include <sys/user.h>		/* for coredump */
76 
77 #if defined(UVM)
78 #include <uvm/uvm_extern.h>
79 #endif
80 
81 void stop __P((struct proc *p));
82 void killproc __P((struct proc *, char *));
83 
84 sigset_t contsigmask, stopsigmask, sigcantmask;
85 
86 /*
87  * Can process p, with pcred pc, send the signal signum to process q?
88  */
89 #define CANSIGNAL(p, pc, q, signum) \
90 	((pc)->pc_ucred->cr_uid == 0 || \
91 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
92 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
93 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
94 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
95 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
96 
97 int
98 sigaction1(p, signum, nsa, osa)
99 	struct proc *p;
100 	int signum;
101 	const struct sigaction *nsa;
102 	struct sigaction *osa;
103 {
104 	register struct sigacts *ps = p->p_sigacts;
105 	int prop;
106 
107 	if (signum <= 0 || signum >= NSIG)
108 		return (EINVAL);
109 
110 	if (osa)
111 		*osa = ps->ps_sigact[signum];
112 
113 	if (nsa) {
114 		if (nsa->sa_flags & ~SA_ALLBITS)
115 			return (EINVAL);
116 
117 		prop = sigprop[signum];
118 		if (prop & SA_CANTMASK)
119 			return (EINVAL);
120 
121 		(void) splhigh();
122 		ps->ps_sigact[signum] = *nsa;
123 		sigminusset(&sigcantmask, &ps->ps_sigact[signum].sa_mask);
124 		if ((prop & SA_NORESET) != 0)
125 			ps->ps_sigact[signum].sa_flags &= ~SA_RESETHAND;
126 		if (signum == SIGCHLD) {
127 			if (nsa->sa_flags & SA_NOCLDSTOP)
128 				p->p_flag |= P_NOCLDSTOP;
129 			else
130 				p->p_flag &= ~P_NOCLDSTOP;
131 			if (nsa->sa_flags & SA_NOCLDWAIT) {
132 				/*
133 				 * Paranoia: since SA_NOCLDWAIT is implemented
134 				 * by reparenting the dying child to PID 1 (and
135 				 * trust it to reap the zombie), PID 1 itself is
136 				 * forbidden to set SA_NOCLDWAIT.
137 				 */
138 				if (p->p_pid == 1)
139 					p->p_flag &= ~P_NOCLDWAIT;
140 				else
141 					p->p_flag |= P_NOCLDWAIT;
142 			} else
143 				p->p_flag &= ~P_NOCLDWAIT;
144 		}
145 		if ((nsa->sa_flags & SA_NODEFER) == 0)
146 			sigaddset(&ps->ps_sigact[signum].sa_mask, signum);
147 		else
148 			sigdelset(&ps->ps_sigact[signum].sa_mask, signum);
149 		/*
150 	 	* Set bit in p_sigignore for signals that are set to SIG_IGN,
151 	 	* and for signals set to SIG_DFL where the default is to ignore.
152 	 	* However, don't put SIGCONT in p_sigignore,
153 	 	* as we have to restart the process.
154 	 	*/
155 		if (nsa->sa_handler == SIG_IGN ||
156 		    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
157 			sigdelset(&p->p_siglist, signum);	/* never to be seen again */
158 			if (signum != SIGCONT)
159 				sigaddset(&p->p_sigignore, signum);	/* easier in psignal */
160 			sigdelset(&p->p_sigcatch, signum);
161 		} else {
162 			sigdelset(&p->p_sigignore, signum);
163 			if (nsa->sa_handler == SIG_DFL)
164 				sigdelset(&p->p_sigcatch, signum);
165 			else
166 				sigaddset(&p->p_sigcatch, signum);
167 		}
168 		(void) spl0();
169 	}
170 
171 	return (0);
172 }
173 
174 /* ARGSUSED */
175 int
176 sys___sigaction14(p, v, retval)
177 	struct proc *p;
178 	void *v;
179 	register_t *retval;
180 {
181 	register struct sys___sigaction14_args /* {
182 		syscallarg(int) signum;
183 		syscallarg(const struct sigaction *) nsa;
184 		syscallarg(struct sigaction *) osa;
185 	} */ *uap = v;
186 	struct sigaction nsa, osa;
187 	int error;
188 
189 	if (SCARG(uap, nsa)) {
190 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
191 		if (error)
192 			return (error);
193 	}
194 	error = sigaction1(p, SCARG(uap, signum),
195 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0);
196 	if (error)
197 		return (error);
198 	if (SCARG(uap, osa)) {
199 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
200 		if (error)
201 			return (error);
202 	}
203 	return (0);
204 }
205 
206 /*
207  * Initialize signal state for process 0;
208  * set to ignore signals that are ignored by default and disable the signal
209  * stack.
210  */
211 void
212 siginit(p)
213 	struct proc *p;
214 {
215 	register struct sigacts *ps = p->p_sigacts;
216 	register int signum;
217 	int prop;
218 
219 	sigemptyset(&contsigmask);
220 	sigemptyset(&stopsigmask);
221 	sigemptyset(&sigcantmask);
222 	for (signum = 1; signum < NSIG; signum++) {
223 		prop = sigprop[signum];
224 		if (prop & SA_CONT)
225 			sigaddset(&contsigmask, signum);
226 		if (prop & SA_STOP)
227 			sigaddset(&stopsigmask, signum);
228 		if (prop & SA_CANTMASK)
229 			sigaddset(&sigcantmask, signum);
230 		if (prop & SA_IGNORE && signum != SIGCONT)
231 			sigaddset(&p->p_sigignore, signum);
232 		sigemptyset(&ps->ps_sigact[signum].sa_mask);
233 		ps->ps_sigact[signum].sa_flags = SA_RESTART;
234 	}
235 	sigemptyset(&p->p_sigcatch);
236 	p->p_flag &= ~P_NOCLDSTOP;
237 
238 	/*
239 	 * Reset stack state to the user stack.
240 	 */
241 	ps->ps_sigstk.ss_flags = SS_DISABLE;
242 	ps->ps_sigstk.ss_size = 0;
243 	ps->ps_sigstk.ss_sp = 0;
244 }
245 
246 /*
247  * Reset signals for an exec of the specified process.
248  */
249 void
250 execsigs(p)
251 	register struct proc *p;
252 {
253 	register struct sigacts *ps = p->p_sigacts;
254 	register int signum;
255 	int prop;
256 
257 	/*
258 	 * Reset caught signals.  Held signals remain held
259 	 * through p_sigmask (unless they were caught,
260 	 * and are now ignored by default).
261 	 */
262 	for (signum = 1; signum < NSIG; signum++) {
263 		if (sigismember(&p->p_sigcatch, signum)) {
264 			prop = sigprop[signum];
265 			if (prop & SA_IGNORE) {
266 				if ((prop & SA_CONT) == 0)
267 					sigaddset(&p->p_sigignore, signum);
268 				sigdelset(&p->p_siglist, signum);
269 			}
270 			ps->ps_sigact[signum].sa_handler = SIG_DFL;
271 		}
272 		sigemptyset(&ps->ps_sigact[signum].sa_mask);
273 		ps->ps_sigact[signum].sa_flags = SA_RESTART;
274 	}
275 	sigemptyset(&p->p_sigcatch);
276 	p->p_flag &= ~P_NOCLDSTOP;
277 
278 	/*
279 	 * Reset stack state to the user stack.
280 	 */
281 	ps->ps_sigstk.ss_flags = SS_DISABLE;
282 	ps->ps_sigstk.ss_size = 0;
283 	ps->ps_sigstk.ss_sp = 0;
284 }
285 
286 int
287 sigprocmask1(p, how, nss, oss)
288 	struct proc *p;
289 	int how;
290 	const sigset_t *nss;
291 	sigset_t *oss;
292 {
293 
294 	if (oss)
295 		*oss = p->p_sigmask;
296 
297 	if (nss) {
298 		(void) splhigh();
299 		switch (how) {
300 		case SIG_BLOCK:
301 			sigplusset(nss, &p->p_sigmask);
302 			break;
303 		case SIG_UNBLOCK:
304 			sigminusset(nss, &p->p_sigmask);
305 			p->p_sigcheck = 1;
306 			break;
307 		case SIG_SETMASK:
308 			p->p_sigmask = *nss;
309 			p->p_sigcheck = 1;
310 			break;
311 		default:
312 			return (EINVAL);
313 		}
314 		sigminusset(&sigcantmask, &p->p_sigmask);
315 		(void) spl0();
316 	}
317 
318 	return (0);
319 }
320 
321 /*
322  * Manipulate signal mask.
323  * Note that we receive new mask, not pointer,
324  * and return old mask as return value;
325  * the library stub does the rest.
326  */
327 int
328 sys___sigprocmask14(p, v, retval)
329 	register struct proc *p;
330 	void *v;
331 	register_t *retval;
332 {
333 	struct sys___sigprocmask14_args /* {
334 		syscallarg(int) how;
335 		syscallarg(const sigset_t *) set;
336 		syscallarg(sigset_t *) oset;
337 	} */ *uap = v;
338 	sigset_t nss, oss;
339 	int error;
340 
341 	if (SCARG(uap, set)) {
342 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
343 		if (error)
344 			return (error);
345 	}
346 	error = sigprocmask1(p, SCARG(uap, how),
347 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
348 	if (error)
349 		return (error);
350 	if (SCARG(uap, oset)) {
351 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
352 		if (error)
353 			return (error);
354 	}
355 	return (0);
356 }
357 
358 void
359 sigpending1(p, ss)
360 	struct proc *p;
361 	sigset_t *ss;
362 {
363 
364 	*ss = p->p_siglist;
365 	sigminusset(&p->p_sigmask, ss);
366 }
367 
368 /* ARGSUSED */
369 int
370 sys___sigpending14(p, v, retval)
371 	struct proc *p;
372 	void *v;
373 	register_t *retval;
374 {
375 	register struct sys___sigpending14_args /* {
376 		syscallarg(sigset_t *) set;
377 	} */ *uap = v;
378 	sigset_t ss;
379 
380 	sigpending1(p, &ss);
381 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
382 }
383 
384 int
385 sigsuspend1(p, ss)
386 	struct proc *p;
387 	const sigset_t *ss;
388 {
389 	register struct sigacts *ps = p->p_sigacts;
390 
391 	if (ss) {
392 		/*
393 		 * When returning from sigpause, we want
394 		 * the old mask to be restored after the
395 		 * signal handler has finished.  Thus, we
396 		 * save it here and mark the sigacts structure
397 		 * to indicate this.
398 		 */
399 		ps->ps_oldmask = p->p_sigmask;
400 		ps->ps_flags |= SAS_OLDMASK;
401 		(void) splhigh();
402 		p->p_sigmask = *ss;
403 		p->p_sigcheck = 1;
404 		sigminusset(&sigcantmask, &p->p_sigmask);
405 		(void) spl0();
406 	}
407 
408 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
409 		/* void */;
410 	/* always return EINTR rather than ERESTART... */
411 	return (EINTR);
412 }
413 
414 /*
415  * Suspend process until signal, providing mask to be set
416  * in the meantime.  Note nonstandard calling convention:
417  * libc stub passes mask, not pointer, to save a copyin.
418  */
419 /* ARGSUSED */
420 int
421 sys___sigsuspend14(p, v, retval)
422 	register struct proc *p;
423 	void *v;
424 	register_t *retval;
425 {
426 	struct sys___sigsuspend14_args /* {
427 		syscallarg(const sigset_t *) set;
428 	} */ *uap = v;
429 	sigset_t ss;
430 	int error;
431 
432 	if (SCARG(uap, set)) {
433 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
434 		if (error)
435 			return (error);
436 	}
437 
438 	return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
439 }
440 
441 int
442 sigaltstack1(p, nss, oss)
443 	struct proc *p;
444 	const struct sigaltstack *nss;
445 	struct sigaltstack *oss;
446 {
447 	register struct sigacts *ps = p->p_sigacts;
448 
449 	if (oss)
450 		*oss = ps->ps_sigstk;
451 
452 	if (nss) {
453 		if (nss->ss_flags & ~SS_ALLBITS)
454 			return (EINVAL);
455 
456 		if (nss->ss_flags & SS_DISABLE) {
457 			if (ps->ps_sigstk.ss_flags & SS_ONSTACK)
458 				return (EINVAL);
459 		} else {
460 			if (nss->ss_size < MINSIGSTKSZ)
461 				return (ENOMEM);
462 		}
463 		ps->ps_sigstk = *nss;
464 	}
465 
466 	return (0);
467 }
468 
469 /* ARGSUSED */
470 int
471 sys___sigaltstack14(p, v, retval)
472 	struct proc *p;
473 	void *v;
474 	register_t *retval;
475 {
476 	register struct sys___sigaltstack14_args /* {
477 		syscallarg(const struct sigaltstack *) nss;
478 		syscallarg(struct sigaltstack *) oss;
479 	} */ *uap = v;
480 	struct sigaltstack nss, oss;
481 	int error;
482 
483 	if (SCARG(uap, nss)) {
484 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
485 		if (error)
486 			return (error);
487 	}
488 	error = sigaltstack1(p,
489 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
490 	if (error)
491 		return (error);
492 	if (SCARG(uap, oss)) {
493 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
494 		if (error)
495 			return (error);
496 	}
497 	return (0);
498 }
499 
500 /* ARGSUSED */
501 int
502 sys_kill(cp, v, retval)
503 	register struct proc *cp;
504 	void *v;
505 	register_t *retval;
506 {
507 	register struct sys_kill_args /* {
508 		syscallarg(int) pid;
509 		syscallarg(int) signum;
510 	} */ *uap = v;
511 	register struct proc *p;
512 	register struct pcred *pc = cp->p_cred;
513 
514 	if ((u_int)SCARG(uap, signum) >= NSIG)
515 		return (EINVAL);
516 	if (SCARG(uap, pid) > 0) {
517 		/* kill single process */
518 		if ((p = pfind(SCARG(uap, pid))) == NULL)
519 			return (ESRCH);
520 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
521 			return (EPERM);
522 		if (SCARG(uap, signum))
523 			psignal(p, SCARG(uap, signum));
524 		return (0);
525 	}
526 	switch (SCARG(uap, pid)) {
527 	case -1:		/* broadcast signal */
528 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
529 	case 0:			/* signal own process group */
530 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
531 	default:		/* negative explicit process group */
532 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
533 	}
534 	/* NOTREACHED */
535 }
536 
537 /*
538  * Common code for kill process group/broadcast kill.
539  * cp is calling process.
540  */
541 int
542 killpg1(cp, signum, pgid, all)
543 	register struct proc *cp;
544 	int signum, pgid, all;
545 {
546 	register struct proc *p;
547 	register struct pcred *pc = cp->p_cred;
548 	struct pgrp *pgrp;
549 	int nfound = 0;
550 
551 	if (all)
552 		/*
553 		 * broadcast
554 		 */
555 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
556 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
557 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
558 				continue;
559 			nfound++;
560 			if (signum)
561 				psignal(p, signum);
562 		}
563 	else {
564 		if (pgid == 0)
565 			/*
566 			 * zero pgid means send to my process group.
567 			 */
568 			pgrp = cp->p_pgrp;
569 		else {
570 			pgrp = pgfind(pgid);
571 			if (pgrp == NULL)
572 				return (ESRCH);
573 		}
574 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
575 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
576 			    !CANSIGNAL(cp, pc, p, signum))
577 				continue;
578 			nfound++;
579 			if (signum && p->p_stat != SZOMB)
580 				psignal(p, signum);
581 		}
582 	}
583 	return (nfound ? 0 : ESRCH);
584 }
585 
586 /*
587  * Send a signal to a process group.
588  */
589 void
590 gsignal(pgid, signum)
591 	int pgid, signum;
592 {
593 	struct pgrp *pgrp;
594 
595 	if (pgid && (pgrp = pgfind(pgid)))
596 		pgsignal(pgrp, signum, 0);
597 }
598 
599 /*
600  * Send a signal to a process group. If checktty is 1,
601  * limit to members which have a controlling terminal.
602  */
603 void
604 pgsignal(pgrp, signum, checkctty)
605 	struct pgrp *pgrp;
606 	int signum, checkctty;
607 {
608 	register struct proc *p;
609 
610 	if (pgrp)
611 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
612 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
613 				psignal(p, signum);
614 }
615 
616 /*
617  * Send a signal caused by a trap to the current process.
618  * If it will be caught immediately, deliver it with correct code.
619  * Otherwise, post it normally.
620  */
621 void
622 trapsignal(p, signum, code)
623 	struct proc *p;
624 	register int signum;
625 	u_long code;
626 {
627 	register struct sigacts *ps = p->p_sigacts;
628 
629 	if ((p->p_flag & P_TRACED) == 0 &&
630 	    sigismember(&p->p_sigcatch, signum) &&
631 	    !sigismember(&p->p_sigmask, signum)) {
632 		p->p_stats->p_ru.ru_nsignals++;
633 #ifdef KTRACE
634 		if (KTRPOINT(p, KTR_PSIG))
635 			ktrpsig(p->p_tracep, signum,
636 			    ps->ps_sigact[signum].sa_handler, &p->p_sigmask,
637 			    code);
638 #endif
639 		(*p->p_emul->e_sendsig)(ps->ps_sigact[signum].sa_handler,
640 		    signum, &p->p_sigmask, code);
641 		(void) splhigh();
642 		sigplusset(&ps->ps_sigact[signum].sa_mask, &p->p_sigmask);
643 		if (ps->ps_sigact[signum].sa_flags & SA_RESETHAND) {
644 			sigdelset(&p->p_sigcatch, signum);
645 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
646 				sigaddset(&p->p_sigignore, signum);
647 			ps->ps_sigact[signum].sa_handler = SIG_DFL;
648 		}
649 		(void) spl0();
650 	} else {
651 		ps->ps_code = code;	/* XXX for core dump/debugger */
652 		ps->ps_sig = signum;	/* XXX to verify code */
653 		psignal(p, signum);
654 	}
655 }
656 
657 /*
658  * Send the signal to the process.  If the signal has an action, the action
659  * is usually performed by the target process rather than the caller; we add
660  * the signal to the set of pending signals for the process.
661  *
662  * Exceptions:
663  *   o When a stop signal is sent to a sleeping process that takes the
664  *     default action, the process is stopped without awakening it.
665  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
666  *     regardless of the signal action (eg, blocked or ignored).
667  *
668  * Other ignored signals are discarded immediately.
669  */
670 void
671 psignal(p, signum)
672 	register struct proc *p;
673 	register int signum;
674 {
675 	register int s, prop;
676 	register sig_t action;
677 
678 #ifdef DIAGNOSTIC
679 	if (signum <= 0 || signum >= NSIG)
680 		panic("psignal signal number");
681 #endif
682 	prop = sigprop[signum];
683 
684 	/*
685 	 * If proc is traced, always give parent a chance.
686 	 */
687 	if (p->p_flag & P_TRACED)
688 		action = SIG_DFL;
689 	else {
690 		/*
691 		 * If the signal is being ignored,
692 		 * then we forget about it immediately.
693 		 * (Note: we don't set SIGCONT in p_sigignore,
694 		 * and if it is set to SIG_IGN,
695 		 * action will be SIG_DFL here.)
696 		 */
697 		if (sigismember(&p->p_sigignore, signum))
698 			return;
699 		if (sigismember(&p->p_sigmask, signum))
700 			action = SIG_HOLD;
701 		else if (sigismember(&p->p_sigcatch, signum))
702 			action = SIG_CATCH;
703 		else {
704 			action = SIG_DFL;
705 
706 			if (prop & SA_KILL && p->p_nice > NZERO)
707 				p->p_nice = NZERO;
708 
709 			/*
710 			 * If sending a tty stop signal to a member of an
711 			 * orphaned process group, discard the signal here if
712 			 * the action is default; don't stop the process below
713 			 * if sleeping, and don't clear any pending SIGCONT.
714 			 */
715 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
716 				return;
717 		}
718 	}
719 
720 	if (prop & SA_CONT)
721 		sigminusset(&stopsigmask, &p->p_siglist);
722 
723 	if (prop & SA_STOP)
724 		sigminusset(&contsigmask, &p->p_siglist);
725 
726 	sigaddset(&p->p_siglist, signum);
727 	p->p_sigcheck = 1;
728 
729 	/*
730 	 * Defer further processing for signals which are held,
731 	 * except that stopped processes must be continued by SIGCONT.
732 	 */
733 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
734 		return;
735 	s = splhigh();
736 	switch (p->p_stat) {
737 
738 	case SSLEEP:
739 		/*
740 		 * If process is sleeping uninterruptibly
741 		 * we can't interrupt the sleep... the signal will
742 		 * be noticed when the process returns through
743 		 * trap() or syscall().
744 		 */
745 		if ((p->p_flag & P_SINTR) == 0)
746 			goto out;
747 		/*
748 		 * Process is sleeping and traced... make it runnable
749 		 * so it can discover the signal in issignal() and stop
750 		 * for the parent.
751 		 */
752 		if (p->p_flag & P_TRACED)
753 			goto run;
754 		/*
755 		 * If SIGCONT is default (or ignored) and process is
756 		 * asleep, we are finished; the process should not
757 		 * be awakened.
758 		 */
759 		if ((prop & SA_CONT) && action == SIG_DFL) {
760 			sigdelset(&p->p_siglist, signum);
761 			goto out;
762 		}
763 		/*
764 		 * When a sleeping process receives a stop
765 		 * signal, process immediately if possible.
766 		 */
767 		if ((prop & SA_STOP) && action == SIG_DFL) {
768 			/*
769 			 * If a child holding parent blocked,
770 			 * stopping could cause deadlock.
771 			 */
772 			if (p->p_flag & P_PPWAIT)
773 				goto out;
774 			sigdelset(&p->p_siglist, signum);
775 			p->p_xstat = signum;
776 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
777 				psignal(p->p_pptr, SIGCHLD);
778 			stop(p);
779 			goto out;
780 		}
781 		/*
782 		 * All other (caught or default) signals
783 		 * cause the process to run.
784 		 */
785 		goto runfast;
786 		/*NOTREACHED*/
787 
788 	case SSTOP:
789 		/*
790 		 * If traced process is already stopped,
791 		 * then no further action is necessary.
792 		 */
793 		if (p->p_flag & P_TRACED)
794 			goto out;
795 
796 		/*
797 		 * Kill signal always sets processes running.
798 		 */
799 		if (signum == SIGKILL)
800 			goto runfast;
801 
802 		if (prop & SA_CONT) {
803 			/*
804 			 * If SIGCONT is default (or ignored), we continue the
805 			 * process but don't leave the signal in p_siglist, as
806 			 * it has no further action.  If SIGCONT is held, we
807 			 * continue the process and leave the signal in
808 			 * p_siglist.  If the process catches SIGCONT, let it
809 			 * handle the signal itself.  If it isn't waiting on
810 			 * an event, then it goes back to run state.
811 			 * Otherwise, process goes back to sleep state.
812 			 */
813 			if (action == SIG_DFL)
814 				sigdelset(&p->p_siglist, signum);
815 			if (action == SIG_CATCH)
816 				goto runfast;
817 			if (p->p_wchan == 0)
818 				goto run;
819 			p->p_stat = SSLEEP;
820 			goto out;
821 		}
822 
823 		if (prop & SA_STOP) {
824 			/*
825 			 * Already stopped, don't need to stop again.
826 			 * (If we did the shell could get confused.)
827 			 */
828 			sigdelset(&p->p_siglist, signum);
829 			goto out;
830 		}
831 
832 		/*
833 		 * If process is sleeping interruptibly, then simulate a
834 		 * wakeup so that when it is continued, it will be made
835 		 * runnable and can look at the signal.  But don't make
836 		 * the process runnable, leave it stopped.
837 		 */
838 		if (p->p_wchan && p->p_flag & P_SINTR)
839 			unsleep(p);
840 		goto out;
841 
842 	default:
843 		/*
844 		 * SRUN, SIDL, SZOMB do nothing with the signal,
845 		 * other than kicking ourselves if we are running.
846 		 * It will either never be noticed, or noticed very soon.
847 		 */
848 		if (p == curproc)
849 			signotify(p);
850 		goto out;
851 	}
852 	/*NOTREACHED*/
853 
854 runfast:
855 	/*
856 	 * Raise priority to at least PUSER.
857 	 */
858 	if (p->p_priority > PUSER)
859 		p->p_priority = PUSER;
860 run:
861 	setrunnable(p);
862 out:
863 	splx(s);
864 }
865 
866 static __inline int firstsig __P((const sigset_t *));
867 
868 static __inline int
869 firstsig(ss)
870 	const sigset_t *ss;
871 {
872 	int sig;
873 
874 	sig = ffs(ss->__bits[0]);
875 	if (sig != 0)
876 		return (sig);
877 #if NSIG > 33
878 	sig = ffs(ss->__bits[1]);
879 	if (sig != 0)
880 		return (sig + 32);
881 #endif
882 #if NSIG > 65
883 	sig = ffs(ss->__bits[2]);
884 	if (sig != 0)
885 		return (sig + 64);
886 #endif
887 #if NSIG > 97
888 	sig = ffs(ss->__bits[3]);
889 	if (sig != 0)
890 		return (sig + 96);
891 #endif
892 	return (0);
893 }
894 
895 /*
896  * If the current process has received a signal (should be caught or cause
897  * termination, should interrupt current syscall), return the signal number.
898  * Stop signals with default action are processed immediately, then cleared;
899  * they aren't returned.  This is checked after each entry to the system for
900  * a syscall or trap (though this can usually be done without calling issignal
901  * by checking the pending signal masks in the CURSIG macro.) The normal call
902  * sequence is
903  *
904  *	while (signum = CURSIG(curproc))
905  *		postsig(signum);
906  */
907 int
908 issignal(p)
909 	register struct proc *p;
910 {
911 	register int signum, prop;
912 	sigset_t ss;
913 
914 	for (;;) {
915 		sigpending1(p, &ss);
916 		if (p->p_flag & P_PPWAIT)
917 			sigminusset(&stopsigmask, &ss);
918 		signum = firstsig(&ss);
919 		if (signum == 0) {		 	/* no signal to send */
920 			p->p_sigcheck = 0;
921 			return (0);
922 		}
923 		sigdelset(&p->p_siglist, signum);	/* take the signal! */
924 
925 		/*
926 		 * We should see pending but ignored signals
927 		 * only if P_TRACED was on when they were posted.
928 		 */
929 		if (sigismember(&p->p_sigignore, signum) &&
930 		    (p->p_flag & P_TRACED) == 0)
931 			continue;
932 
933 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
934 			/*
935 			 * If traced, always stop, and stay
936 			 * stopped until released by the debugger.
937 			 */
938 			p->p_xstat = signum;
939 			if ((p->p_flag & P_FSTRACE) == 0)
940 				psignal(p->p_pptr, SIGCHLD);
941 			do {
942 				stop(p);
943 				mi_switch();
944 			} while (!trace_req(p) && p->p_flag & P_TRACED);
945 
946 			/*
947 			 * If we are no longer being traced, or the parent
948 			 * didn't give us a signal, look for more signals.
949 			 */
950 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
951 				continue;
952 
953 			/*
954 			 * If the new signal is being masked, look for other
955 			 * signals.
956 			 */
957 			signum = p->p_xstat;
958 			/* `p->p_siglist |= mask' is done in setrunnable(). */
959 			if (sigismember(&p->p_sigmask, signum))
960 				continue;
961 			sigdelset(&p->p_siglist, signum);	/* take the signal! */
962 		}
963 
964 		prop = sigprop[signum];
965 
966 		/*
967 		 * Decide whether the signal should be returned.
968 		 * Return the signal's number, or fall through
969 		 * to clear it from the pending mask.
970 		 */
971 		switch ((long)p->p_sigacts->ps_sigact[signum].sa_handler) {
972 
973 		case (long)SIG_DFL:
974 			/*
975 			 * Don't take default actions on system processes.
976 			 */
977 			if (p->p_pid <= 1) {
978 #ifdef DIAGNOSTIC
979 				/*
980 				 * Are you sure you want to ignore SIGSEGV
981 				 * in init? XXX
982 				 */
983 				printf("Process (pid %d) got signal %d\n",
984 				    p->p_pid, signum);
985 #endif
986 				break;		/* == ignore */
987 			}
988 			/*
989 			 * If there is a pending stop signal to process
990 			 * with default action, stop here,
991 			 * then clear the signal.  However,
992 			 * if process is member of an orphaned
993 			 * process group, ignore tty stop signals.
994 			 */
995 			if (prop & SA_STOP) {
996 				if (p->p_flag & P_TRACED ||
997 		    		    (p->p_pgrp->pg_jobc == 0 &&
998 				    prop & SA_TTYSTOP))
999 					break;	/* == ignore */
1000 				p->p_xstat = signum;
1001 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1002 					psignal(p->p_pptr, SIGCHLD);
1003 				stop(p);
1004 				mi_switch();
1005 				break;
1006 			} else if (prop & SA_IGNORE) {
1007 				/*
1008 				 * Except for SIGCONT, shouldn't get here.
1009 				 * Default action is to ignore; drop it.
1010 				 */
1011 				break;		/* == ignore */
1012 			} else
1013 				goto keep;
1014 			/*NOTREACHED*/
1015 
1016 		case (long)SIG_IGN:
1017 			/*
1018 			 * Masking above should prevent us ever trying
1019 			 * to take action on an ignored signal other
1020 			 * than SIGCONT, unless process is traced.
1021 			 */
1022 			if ((prop & SA_CONT) == 0 &&
1023 			    (p->p_flag & P_TRACED) == 0)
1024 				printf("issignal\n");
1025 			break;		/* == ignore */
1026 
1027 		default:
1028 			/*
1029 			 * This signal has an action, let
1030 			 * postsig() process it.
1031 			 */
1032 			goto keep;
1033 		}
1034 	}
1035 	/* NOTREACHED */
1036 
1037 keep:
1038 	sigaddset(&p->p_siglist, signum);	/* leave the signal for later */
1039 	p->p_sigcheck = 1;
1040 	return (signum);
1041 }
1042 
1043 /*
1044  * Put the argument process into the stopped state and notify the parent
1045  * via wakeup.  Signals are handled elsewhere.  The process must not be
1046  * on the run queue.
1047  */
1048 void
1049 stop(p)
1050 	register struct proc *p;
1051 {
1052 
1053 	p->p_stat = SSTOP;
1054 	p->p_flag &= ~P_WAITED;
1055 	wakeup((caddr_t)p->p_pptr);
1056 }
1057 
1058 /*
1059  * Take the action for the specified signal
1060  * from the current set of pending signals.
1061  */
1062 void
1063 postsig(signum)
1064 	register int signum;
1065 {
1066 	register struct proc *p = curproc;
1067 	register struct sigacts *ps = p->p_sigacts;
1068 	register sig_t action;
1069 	u_long code;
1070 	sigset_t *returnmask;
1071 
1072 #ifdef DIAGNOSTIC
1073 	if (signum == 0)
1074 		panic("postsig");
1075 #endif
1076 	sigdelset(&p->p_siglist, signum);
1077 	action = ps->ps_sigact[signum].sa_handler;
1078 #ifdef KTRACE
1079 	if (KTRPOINT(p, KTR_PSIG))
1080 		ktrpsig(p->p_tracep,
1081 		    signum, action, ps->ps_flags & SAS_OLDMASK ?
1082 		    &ps->ps_oldmask : &p->p_sigmask, 0);
1083 #endif
1084 	if (action == SIG_DFL) {
1085 		/*
1086 		 * Default action, where the default is to kill
1087 		 * the process.  (Other cases were ignored above.)
1088 		 */
1089 		sigexit(p, signum);
1090 		/* NOTREACHED */
1091 	} else {
1092 		/*
1093 		 * If we get here, the signal must be caught.
1094 		 */
1095 #ifdef DIAGNOSTIC
1096 		if (action == SIG_IGN || sigismember(&p->p_sigmask, signum))
1097 			panic("postsig action");
1098 #endif
1099 		/*
1100 		 * Set the new mask value and also defer further
1101 		 * occurences of this signal.
1102 		 *
1103 		 * Special case: user has done a sigpause.  Here the
1104 		 * current mask is not of interest, but rather the
1105 		 * mask from before the sigpause is what we want
1106 		 * restored after the signal processing is completed.
1107 		 */
1108 		if (ps->ps_flags & SAS_OLDMASK) {
1109 			returnmask = &ps->ps_oldmask;
1110 			ps->ps_flags &= ~SAS_OLDMASK;
1111 		} else
1112 			returnmask = &p->p_sigmask;
1113 		p->p_stats->p_ru.ru_nsignals++;
1114 		if (ps->ps_sig != signum) {
1115 			code = 0;
1116 		} else {
1117 			code = ps->ps_code;
1118 			ps->ps_code = 0;
1119 			ps->ps_sig = 0;
1120 		}
1121 		(*p->p_emul->e_sendsig)(action, signum, returnmask, code);
1122 		(void) splhigh();
1123 		sigplusset(&ps->ps_sigact[signum].sa_mask, &p->p_sigmask);
1124 		if (ps->ps_sigact[signum].sa_flags & SA_RESETHAND) {
1125 			sigdelset(&p->p_sigcatch, signum);
1126 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1127 				sigaddset(&p->p_sigignore, signum);
1128 			ps->ps_sigact[signum].sa_handler = SIG_DFL;
1129 		}
1130 		(void) spl0();
1131 	}
1132 }
1133 
1134 /*
1135  * Kill the current process for stated reason.
1136  */
1137 void
1138 killproc(p, why)
1139 	struct proc *p;
1140 	char *why;
1141 {
1142 
1143 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1144 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1145 	psignal(p, SIGKILL);
1146 }
1147 
1148 /*
1149  * Force the current process to exit with the specified signal, dumping core
1150  * if appropriate.  We bypass the normal tests for masked and caught signals,
1151  * allowing unrecoverable failures to terminate the process without changing
1152  * signal state.  Mark the accounting record with the signal termination.
1153  * If dumping core, save the signal number for the debugger.  Calls exit and
1154  * does not return.
1155  */
1156 void
1157 sigexit(p, signum)
1158 	register struct proc *p;
1159 	int signum;
1160 {
1161 
1162 	p->p_acflag |= AXSIG;
1163 	if (sigprop[signum] & SA_CORE) {
1164 		p->p_sigacts->ps_sig = signum;
1165 		if (coredump(p) == 0)
1166 			signum |= WCOREFLAG;
1167 	}
1168 	exit1(p, W_EXITCODE(0, signum));
1169 	/* NOTREACHED */
1170 }
1171 
1172 /*
1173  * Dump core, into a file named "progname.core" or "core" (depending on the
1174  * value of shortcorename), unless the process was setuid/setgid.
1175  */
1176 int
1177 coredump(p)
1178 	register struct proc *p;
1179 {
1180 	register struct vnode *vp;
1181 	register struct vmspace *vm = p->p_vmspace;
1182 	register struct ucred *cred = p->p_cred->pc_ucred;
1183 	struct nameidata nd;
1184 	struct vattr vattr;
1185 	int error, error1;
1186 	char name[MAXCOMLEN+6];		/* progname.core */
1187 	struct core core;
1188 	extern int shortcorename;
1189 
1190 	/*
1191 	 * Make sure the process has not set-id, to prevent data leaks.
1192 	 */
1193 	if (p->p_flag & P_SUGID)
1194 		return (EPERM);
1195 
1196 	/*
1197 	 * Refuse to core if the data + stack + user size is larger than
1198 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
1199 	 * data.
1200 	 */
1201 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1202 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1203 		return (EFBIG);		/* better error code? */
1204 
1205 	/*
1206 	 * The core dump will go in the current working directory.  Make
1207 	 * sure that the directory is still there and that the mount flags
1208 	 * allow us to write core dumps there.
1209 	 */
1210 	vp = p->p_fd->fd_cdir;
1211 	if (vp->v_mount == NULL ||
1212 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1213 		return (EPERM);
1214 
1215 	if (shortcorename)
1216 		sprintf(name, "core");
1217 	else
1218 		sprintf(name, "%s.core", p->p_comm);
1219 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1220 	error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR | S_IWUSR);
1221 	if (error)
1222 		return (error);
1223 	vp = nd.ni_vp;
1224 
1225 	/* Don't dump to non-regular files or files with links. */
1226 	if (vp->v_type != VREG ||
1227 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1228 		error = EINVAL;
1229 		goto out;
1230 	}
1231 	VATTR_NULL(&vattr);
1232 	vattr.va_size = 0;
1233 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1234 	VOP_SETATTR(vp, &vattr, cred, p);
1235 	p->p_acflag |= ACORE;
1236 #if 0
1237 	/*
1238 	 * XXX
1239 	 * It would be nice if we at least dumped the signal state (and made it
1240 	 * available at run time to the debugger, as well), but this code
1241 	 * hasn't actually had any effect for a long time, since we don't dump
1242 	 * the user area.  For now, it's dead.
1243 	 */
1244 	memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc));
1245 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1246 #endif
1247 
1248 	core.c_midmag = 0;
1249 	strncpy(core.c_name, p->p_comm, MAXCOMLEN);
1250 	core.c_nseg = 0;
1251 	core.c_signo = p->p_sigacts->ps_sig;
1252 	core.c_ucode = p->p_sigacts->ps_code;
1253 	core.c_cpusize = 0;
1254 	core.c_tsize = (u_long)ctob(vm->vm_tsize);
1255 	core.c_dsize = (u_long)ctob(vm->vm_dsize);
1256 	core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
1257 	error = cpu_coredump(p, vp, cred, &core);
1258 	if (error)
1259 		goto out;
1260 	if (core.c_midmag == 0) {
1261 		/* XXX
1262 		 * cpu_coredump() didn't bother to set the magic; assume
1263 		 * this is a request to do a traditional dump. cpu_coredump()
1264 		 * is still responsible for setting sensible values in
1265 		 * the core header.
1266 		 */
1267 		if (core.c_cpusize == 0)
1268 			core.c_cpusize = USPACE; /* Just in case */
1269 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1270 		    (int)core.c_dsize,
1271 		    (off_t)core.c_cpusize, UIO_USERSPACE,
1272 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1273 		if (error)
1274 			goto out;
1275 		error = vn_rdwr(UIO_WRITE, vp,
1276 		    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1277 		    core.c_ssize,
1278 		    (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
1279 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1280 	} else {
1281 		/*
1282 		 * vm_coredump() spits out all appropriate segments.
1283 		 * All that's left to do is to write the core header.
1284 		 */
1285 #if defined(UVM)
1286 		error = uvm_coredump(p, vp, cred, &core);
1287 #else
1288 		error = vm_coredump(p, vp, cred, &core);
1289 #endif
1290 		if (error)
1291 			goto out;
1292 		error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
1293 		    (int)core.c_hdrsize, (off_t)0,
1294 		    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1295 	}
1296 out:
1297 	VOP_UNLOCK(vp, 0);
1298 	error1 = vn_close(vp, FWRITE, cred, p);
1299 	if (error == 0)
1300 		error = error1;
1301 	return (error);
1302 }
1303 
1304 /*
1305  * Nonexistent system call-- signal process (may want to handle it).
1306  * Flag error in case process won't see signal immediately (blocked or ignored).
1307  */
1308 /* ARGSUSED */
1309 int
1310 sys_nosys(p, v, retval)
1311 	struct proc *p;
1312 	void *v;
1313 	register_t *retval;
1314 {
1315 
1316 	psignal(p, SIGSYS);
1317 	return (ENOSYS);
1318 }
1319