xref: /netbsd-src/sys/kern/kern_sig.c (revision bada23909e740596d0a3785a73bd3583a9807fb8)
1 /*	$NetBSD: kern_sig.c,v 1.86 1999/02/13 15:25:51 christos 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 			(void)spl0();
313 			return (EINVAL);
314 		}
315 		sigminusset(&sigcantmask, &p->p_sigmask);
316 		(void)spl0();
317 	}
318 
319 	return (0);
320 }
321 
322 /*
323  * Manipulate signal mask.
324  * Note that we receive new mask, not pointer,
325  * and return old mask as return value;
326  * the library stub does the rest.
327  */
328 int
329 sys___sigprocmask14(p, v, retval)
330 	register struct proc *p;
331 	void *v;
332 	register_t *retval;
333 {
334 	struct sys___sigprocmask14_args /* {
335 		syscallarg(int) how;
336 		syscallarg(const sigset_t *) set;
337 		syscallarg(sigset_t *) oset;
338 	} */ *uap = v;
339 	sigset_t nss, oss;
340 	int error;
341 
342 	if (SCARG(uap, set)) {
343 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
344 		if (error)
345 			return (error);
346 	}
347 	error = sigprocmask1(p, SCARG(uap, how),
348 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
349 	if (error)
350 		return (error);
351 	if (SCARG(uap, oset)) {
352 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
353 		if (error)
354 			return (error);
355 	}
356 	return (0);
357 }
358 
359 void
360 sigpending1(p, ss)
361 	struct proc *p;
362 	sigset_t *ss;
363 {
364 
365 	*ss = p->p_siglist;
366 	sigminusset(&p->p_sigmask, ss);
367 }
368 
369 /* ARGSUSED */
370 int
371 sys___sigpending14(p, v, retval)
372 	struct proc *p;
373 	void *v;
374 	register_t *retval;
375 {
376 	register struct sys___sigpending14_args /* {
377 		syscallarg(sigset_t *) set;
378 	} */ *uap = v;
379 	sigset_t ss;
380 
381 	sigpending1(p, &ss);
382 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
383 }
384 
385 int
386 sigsuspend1(p, ss)
387 	struct proc *p;
388 	const sigset_t *ss;
389 {
390 	register struct sigacts *ps = p->p_sigacts;
391 
392 	if (ss) {
393 		/*
394 		 * When returning from sigpause, we want
395 		 * the old mask to be restored after the
396 		 * signal handler has finished.  Thus, we
397 		 * save it here and mark the sigacts structure
398 		 * to indicate this.
399 		 */
400 		ps->ps_oldmask = p->p_sigmask;
401 		ps->ps_flags |= SAS_OLDMASK;
402 		(void) splhigh();
403 		p->p_sigmask = *ss;
404 		p->p_sigcheck = 1;
405 		sigminusset(&sigcantmask, &p->p_sigmask);
406 		(void) spl0();
407 	}
408 
409 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
410 		/* void */;
411 	/* always return EINTR rather than ERESTART... */
412 	return (EINTR);
413 }
414 
415 /*
416  * Suspend process until signal, providing mask to be set
417  * in the meantime.  Note nonstandard calling convention:
418  * libc stub passes mask, not pointer, to save a copyin.
419  */
420 /* ARGSUSED */
421 int
422 sys___sigsuspend14(p, v, retval)
423 	register struct proc *p;
424 	void *v;
425 	register_t *retval;
426 {
427 	struct sys___sigsuspend14_args /* {
428 		syscallarg(const sigset_t *) set;
429 	} */ *uap = v;
430 	sigset_t ss;
431 	int error;
432 
433 	if (SCARG(uap, set)) {
434 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
435 		if (error)
436 			return (error);
437 	}
438 
439 	return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
440 }
441 
442 int
443 sigaltstack1(p, nss, oss)
444 	struct proc *p;
445 	const struct sigaltstack *nss;
446 	struct sigaltstack *oss;
447 {
448 	register struct sigacts *ps = p->p_sigacts;
449 
450 	if (oss)
451 		*oss = ps->ps_sigstk;
452 
453 	if (nss) {
454 		if (nss->ss_flags & ~SS_ALLBITS)
455 			return (EINVAL);
456 
457 		if (nss->ss_flags & SS_DISABLE) {
458 			if (ps->ps_sigstk.ss_flags & SS_ONSTACK)
459 				return (EINVAL);
460 		} else {
461 			if (nss->ss_size < MINSIGSTKSZ)
462 				return (ENOMEM);
463 		}
464 		ps->ps_sigstk = *nss;
465 	}
466 
467 	return (0);
468 }
469 
470 /* ARGSUSED */
471 int
472 sys___sigaltstack14(p, v, retval)
473 	struct proc *p;
474 	void *v;
475 	register_t *retval;
476 {
477 	register struct sys___sigaltstack14_args /* {
478 		syscallarg(const struct sigaltstack *) nss;
479 		syscallarg(struct sigaltstack *) oss;
480 	} */ *uap = v;
481 	struct sigaltstack nss, oss;
482 	int error;
483 
484 	if (SCARG(uap, nss)) {
485 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
486 		if (error)
487 			return (error);
488 	}
489 	error = sigaltstack1(p,
490 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
491 	if (error)
492 		return (error);
493 	if (SCARG(uap, oss)) {
494 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
495 		if (error)
496 			return (error);
497 	}
498 	return (0);
499 }
500 
501 /* ARGSUSED */
502 int
503 sys_kill(cp, v, retval)
504 	register struct proc *cp;
505 	void *v;
506 	register_t *retval;
507 {
508 	register struct sys_kill_args /* {
509 		syscallarg(int) pid;
510 		syscallarg(int) signum;
511 	} */ *uap = v;
512 	register struct proc *p;
513 	register struct pcred *pc = cp->p_cred;
514 
515 	if ((u_int)SCARG(uap, signum) >= NSIG)
516 		return (EINVAL);
517 	if (SCARG(uap, pid) > 0) {
518 		/* kill single process */
519 		if ((p = pfind(SCARG(uap, pid))) == NULL)
520 			return (ESRCH);
521 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
522 			return (EPERM);
523 		if (SCARG(uap, signum))
524 			psignal(p, SCARG(uap, signum));
525 		return (0);
526 	}
527 	switch (SCARG(uap, pid)) {
528 	case -1:		/* broadcast signal */
529 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
530 	case 0:			/* signal own process group */
531 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
532 	default:		/* negative explicit process group */
533 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
534 	}
535 	/* NOTREACHED */
536 }
537 
538 /*
539  * Common code for kill process group/broadcast kill.
540  * cp is calling process.
541  */
542 int
543 killpg1(cp, signum, pgid, all)
544 	register struct proc *cp;
545 	int signum, pgid, all;
546 {
547 	register struct proc *p;
548 	register struct pcred *pc = cp->p_cred;
549 	struct pgrp *pgrp;
550 	int nfound = 0;
551 
552 	if (all)
553 		/*
554 		 * broadcast
555 		 */
556 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
557 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
558 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
559 				continue;
560 			nfound++;
561 			if (signum)
562 				psignal(p, signum);
563 		}
564 	else {
565 		if (pgid == 0)
566 			/*
567 			 * zero pgid means send to my process group.
568 			 */
569 			pgrp = cp->p_pgrp;
570 		else {
571 			pgrp = pgfind(pgid);
572 			if (pgrp == NULL)
573 				return (ESRCH);
574 		}
575 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
576 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
577 			    !CANSIGNAL(cp, pc, p, signum))
578 				continue;
579 			nfound++;
580 			if (signum && p->p_stat != SZOMB)
581 				psignal(p, signum);
582 		}
583 	}
584 	return (nfound ? 0 : ESRCH);
585 }
586 
587 /*
588  * Send a signal to a process group.
589  */
590 void
591 gsignal(pgid, signum)
592 	int pgid, signum;
593 {
594 	struct pgrp *pgrp;
595 
596 	if (pgid && (pgrp = pgfind(pgid)))
597 		pgsignal(pgrp, signum, 0);
598 }
599 
600 /*
601  * Send a signal to a process group. If checktty is 1,
602  * limit to members which have a controlling terminal.
603  */
604 void
605 pgsignal(pgrp, signum, checkctty)
606 	struct pgrp *pgrp;
607 	int signum, checkctty;
608 {
609 	register struct proc *p;
610 
611 	if (pgrp)
612 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
613 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
614 				psignal(p, signum);
615 }
616 
617 /*
618  * Send a signal caused by a trap to the current process.
619  * If it will be caught immediately, deliver it with correct code.
620  * Otherwise, post it normally.
621  */
622 void
623 trapsignal(p, signum, code)
624 	struct proc *p;
625 	register int signum;
626 	u_long code;
627 {
628 	register struct sigacts *ps = p->p_sigacts;
629 
630 	if ((p->p_flag & P_TRACED) == 0 &&
631 	    sigismember(&p->p_sigcatch, signum) &&
632 	    !sigismember(&p->p_sigmask, signum)) {
633 		p->p_stats->p_ru.ru_nsignals++;
634 #ifdef KTRACE
635 		if (KTRPOINT(p, KTR_PSIG))
636 			ktrpsig(p->p_tracep, signum,
637 			    ps->ps_sigact[signum].sa_handler, &p->p_sigmask,
638 			    code);
639 #endif
640 		(*p->p_emul->e_sendsig)(ps->ps_sigact[signum].sa_handler,
641 		    signum, &p->p_sigmask, code);
642 		(void) splhigh();
643 		sigplusset(&ps->ps_sigact[signum].sa_mask, &p->p_sigmask);
644 		if (ps->ps_sigact[signum].sa_flags & SA_RESETHAND) {
645 			sigdelset(&p->p_sigcatch, signum);
646 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
647 				sigaddset(&p->p_sigignore, signum);
648 			ps->ps_sigact[signum].sa_handler = SIG_DFL;
649 		}
650 		(void) spl0();
651 	} else {
652 		ps->ps_code = code;	/* XXX for core dump/debugger */
653 		ps->ps_sig = signum;	/* XXX to verify code */
654 		psignal(p, signum);
655 	}
656 }
657 
658 /*
659  * Send the signal to the process.  If the signal has an action, the action
660  * is usually performed by the target process rather than the caller; we add
661  * the signal to the set of pending signals for the process.
662  *
663  * Exceptions:
664  *   o When a stop signal is sent to a sleeping process that takes the
665  *     default action, the process is stopped without awakening it.
666  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
667  *     regardless of the signal action (eg, blocked or ignored).
668  *
669  * Other ignored signals are discarded immediately.
670  */
671 void
672 psignal(p, signum)
673 	register struct proc *p;
674 	register int signum;
675 {
676 	register int s, prop;
677 	register sig_t action;
678 
679 #ifdef DIAGNOSTIC
680 	if (signum <= 0 || signum >= NSIG)
681 		panic("psignal signal number");
682 #endif
683 	prop = sigprop[signum];
684 
685 	/*
686 	 * If proc is traced, always give parent a chance.
687 	 */
688 	if (p->p_flag & P_TRACED)
689 		action = SIG_DFL;
690 	else {
691 		/*
692 		 * If the signal is being ignored,
693 		 * then we forget about it immediately.
694 		 * (Note: we don't set SIGCONT in p_sigignore,
695 		 * and if it is set to SIG_IGN,
696 		 * action will be SIG_DFL here.)
697 		 */
698 		if (sigismember(&p->p_sigignore, signum))
699 			return;
700 		if (sigismember(&p->p_sigmask, signum))
701 			action = SIG_HOLD;
702 		else if (sigismember(&p->p_sigcatch, signum))
703 			action = SIG_CATCH;
704 		else {
705 			action = SIG_DFL;
706 
707 			if (prop & SA_KILL && p->p_nice > NZERO)
708 				p->p_nice = NZERO;
709 
710 			/*
711 			 * If sending a tty stop signal to a member of an
712 			 * orphaned process group, discard the signal here if
713 			 * the action is default; don't stop the process below
714 			 * if sleeping, and don't clear any pending SIGCONT.
715 			 */
716 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
717 				return;
718 		}
719 	}
720 
721 	if (prop & SA_CONT)
722 		sigminusset(&stopsigmask, &p->p_siglist);
723 
724 	if (prop & SA_STOP)
725 		sigminusset(&contsigmask, &p->p_siglist);
726 
727 	sigaddset(&p->p_siglist, signum);
728 	p->p_sigcheck = 1;
729 
730 	/*
731 	 * Defer further processing for signals which are held,
732 	 * except that stopped processes must be continued by SIGCONT.
733 	 */
734 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
735 		return;
736 	s = splhigh();
737 	switch (p->p_stat) {
738 
739 	case SSLEEP:
740 		/*
741 		 * If process is sleeping uninterruptibly
742 		 * we can't interrupt the sleep... the signal will
743 		 * be noticed when the process returns through
744 		 * trap() or syscall().
745 		 */
746 		if ((p->p_flag & P_SINTR) == 0)
747 			goto out;
748 		/*
749 		 * Process is sleeping and traced... make it runnable
750 		 * so it can discover the signal in issignal() and stop
751 		 * for the parent.
752 		 */
753 		if (p->p_flag & P_TRACED)
754 			goto run;
755 		/*
756 		 * If SIGCONT is default (or ignored) and process is
757 		 * asleep, we are finished; the process should not
758 		 * be awakened.
759 		 */
760 		if ((prop & SA_CONT) && action == SIG_DFL) {
761 			sigdelset(&p->p_siglist, signum);
762 			goto out;
763 		}
764 		/*
765 		 * When a sleeping process receives a stop
766 		 * signal, process immediately if possible.
767 		 */
768 		if ((prop & SA_STOP) && action == SIG_DFL) {
769 			/*
770 			 * If a child holding parent blocked,
771 			 * stopping could cause deadlock.
772 			 */
773 			if (p->p_flag & P_PPWAIT)
774 				goto out;
775 			sigdelset(&p->p_siglist, signum);
776 			p->p_xstat = signum;
777 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
778 				psignal(p->p_pptr, SIGCHLD);
779 			stop(p);
780 			goto out;
781 		}
782 		/*
783 		 * All other (caught or default) signals
784 		 * cause the process to run.
785 		 */
786 		goto runfast;
787 		/*NOTREACHED*/
788 
789 	case SSTOP:
790 		/*
791 		 * If traced process is already stopped,
792 		 * then no further action is necessary.
793 		 */
794 		if (p->p_flag & P_TRACED)
795 			goto out;
796 
797 		/*
798 		 * Kill signal always sets processes running.
799 		 */
800 		if (signum == SIGKILL)
801 			goto runfast;
802 
803 		if (prop & SA_CONT) {
804 			/*
805 			 * If SIGCONT is default (or ignored), we continue the
806 			 * process but don't leave the signal in p_siglist, as
807 			 * it has no further action.  If SIGCONT is held, we
808 			 * continue the process and leave the signal in
809 			 * p_siglist.  If the process catches SIGCONT, let it
810 			 * handle the signal itself.  If it isn't waiting on
811 			 * an event, then it goes back to run state.
812 			 * Otherwise, process goes back to sleep state.
813 			 */
814 			if (action == SIG_DFL)
815 				sigdelset(&p->p_siglist, signum);
816 			if (action == SIG_CATCH)
817 				goto runfast;
818 			if (p->p_wchan == 0)
819 				goto run;
820 			p->p_stat = SSLEEP;
821 			goto out;
822 		}
823 
824 		if (prop & SA_STOP) {
825 			/*
826 			 * Already stopped, don't need to stop again.
827 			 * (If we did the shell could get confused.)
828 			 */
829 			sigdelset(&p->p_siglist, signum);
830 			goto out;
831 		}
832 
833 		/*
834 		 * If process is sleeping interruptibly, then simulate a
835 		 * wakeup so that when it is continued, it will be made
836 		 * runnable and can look at the signal.  But don't make
837 		 * the process runnable, leave it stopped.
838 		 */
839 		if (p->p_wchan && p->p_flag & P_SINTR)
840 			unsleep(p);
841 		goto out;
842 
843 	default:
844 		/*
845 		 * SRUN, SIDL, SZOMB do nothing with the signal,
846 		 * other than kicking ourselves if we are running.
847 		 * It will either never be noticed, or noticed very soon.
848 		 */
849 		if (p == curproc)
850 			signotify(p);
851 		goto out;
852 	}
853 	/*NOTREACHED*/
854 
855 runfast:
856 	/*
857 	 * Raise priority to at least PUSER.
858 	 */
859 	if (p->p_priority > PUSER)
860 		p->p_priority = PUSER;
861 run:
862 	setrunnable(p);
863 out:
864 	splx(s);
865 }
866 
867 static __inline int firstsig __P((const sigset_t *));
868 
869 static __inline int
870 firstsig(ss)
871 	const sigset_t *ss;
872 {
873 	int sig;
874 
875 	sig = ffs(ss->__bits[0]);
876 	if (sig != 0)
877 		return (sig);
878 #if NSIG > 33
879 	sig = ffs(ss->__bits[1]);
880 	if (sig != 0)
881 		return (sig + 32);
882 #endif
883 #if NSIG > 65
884 	sig = ffs(ss->__bits[2]);
885 	if (sig != 0)
886 		return (sig + 64);
887 #endif
888 #if NSIG > 97
889 	sig = ffs(ss->__bits[3]);
890 	if (sig != 0)
891 		return (sig + 96);
892 #endif
893 	return (0);
894 }
895 
896 /*
897  * If the current process has received a signal (should be caught or cause
898  * termination, should interrupt current syscall), return the signal number.
899  * Stop signals with default action are processed immediately, then cleared;
900  * they aren't returned.  This is checked after each entry to the system for
901  * a syscall or trap (though this can usually be done without calling issignal
902  * by checking the pending signal masks in the CURSIG macro.) The normal call
903  * sequence is
904  *
905  *	while (signum = CURSIG(curproc))
906  *		postsig(signum);
907  */
908 int
909 issignal(p)
910 	register struct proc *p;
911 {
912 	register int signum, prop;
913 	sigset_t ss;
914 
915 	for (;;) {
916 		sigpending1(p, &ss);
917 		if (p->p_flag & P_PPWAIT)
918 			sigminusset(&stopsigmask, &ss);
919 		signum = firstsig(&ss);
920 		if (signum == 0) {		 	/* no signal to send */
921 			p->p_sigcheck = 0;
922 			return (0);
923 		}
924 		sigdelset(&p->p_siglist, signum);	/* take the signal! */
925 
926 		/*
927 		 * We should see pending but ignored signals
928 		 * only if P_TRACED was on when they were posted.
929 		 */
930 		if (sigismember(&p->p_sigignore, signum) &&
931 		    (p->p_flag & P_TRACED) == 0)
932 			continue;
933 
934 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
935 			/*
936 			 * If traced, always stop, and stay
937 			 * stopped until released by the debugger.
938 			 */
939 			p->p_xstat = signum;
940 			if ((p->p_flag & P_FSTRACE) == 0)
941 				psignal(p->p_pptr, SIGCHLD);
942 			do {
943 				stop(p);
944 				mi_switch();
945 			} while (!trace_req(p) && p->p_flag & P_TRACED);
946 
947 			/*
948 			 * If we are no longer being traced, or the parent
949 			 * didn't give us a signal, look for more signals.
950 			 */
951 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
952 				continue;
953 
954 			/*
955 			 * If the new signal is being masked, look for other
956 			 * signals.
957 			 */
958 			signum = p->p_xstat;
959 			/* `p->p_siglist |= mask' is done in setrunnable(). */
960 			if (sigismember(&p->p_sigmask, signum))
961 				continue;
962 			sigdelset(&p->p_siglist, signum);	/* take the signal! */
963 		}
964 
965 		prop = sigprop[signum];
966 
967 		/*
968 		 * Decide whether the signal should be returned.
969 		 * Return the signal's number, or fall through
970 		 * to clear it from the pending mask.
971 		 */
972 		switch ((long)p->p_sigacts->ps_sigact[signum].sa_handler) {
973 
974 		case (long)SIG_DFL:
975 			/*
976 			 * Don't take default actions on system processes.
977 			 */
978 			if (p->p_pid <= 1) {
979 #ifdef DIAGNOSTIC
980 				/*
981 				 * Are you sure you want to ignore SIGSEGV
982 				 * in init? XXX
983 				 */
984 				printf("Process (pid %d) got signal %d\n",
985 				    p->p_pid, signum);
986 #endif
987 				break;		/* == ignore */
988 			}
989 			/*
990 			 * If there is a pending stop signal to process
991 			 * with default action, stop here,
992 			 * then clear the signal.  However,
993 			 * if process is member of an orphaned
994 			 * process group, ignore tty stop signals.
995 			 */
996 			if (prop & SA_STOP) {
997 				if (p->p_flag & P_TRACED ||
998 		    		    (p->p_pgrp->pg_jobc == 0 &&
999 				    prop & SA_TTYSTOP))
1000 					break;	/* == ignore */
1001 				p->p_xstat = signum;
1002 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1003 					psignal(p->p_pptr, SIGCHLD);
1004 				stop(p);
1005 				mi_switch();
1006 				break;
1007 			} else if (prop & SA_IGNORE) {
1008 				/*
1009 				 * Except for SIGCONT, shouldn't get here.
1010 				 * Default action is to ignore; drop it.
1011 				 */
1012 				break;		/* == ignore */
1013 			} else
1014 				goto keep;
1015 			/*NOTREACHED*/
1016 
1017 		case (long)SIG_IGN:
1018 			/*
1019 			 * Masking above should prevent us ever trying
1020 			 * to take action on an ignored signal other
1021 			 * than SIGCONT, unless process is traced.
1022 			 */
1023 			if ((prop & SA_CONT) == 0 &&
1024 			    (p->p_flag & P_TRACED) == 0)
1025 				printf("issignal\n");
1026 			break;		/* == ignore */
1027 
1028 		default:
1029 			/*
1030 			 * This signal has an action, let
1031 			 * postsig() process it.
1032 			 */
1033 			goto keep;
1034 		}
1035 	}
1036 	/* NOTREACHED */
1037 
1038 keep:
1039 	sigaddset(&p->p_siglist, signum);	/* leave the signal for later */
1040 	p->p_sigcheck = 1;
1041 	return (signum);
1042 }
1043 
1044 /*
1045  * Put the argument process into the stopped state and notify the parent
1046  * via wakeup.  Signals are handled elsewhere.  The process must not be
1047  * on the run queue.
1048  */
1049 void
1050 stop(p)
1051 	register struct proc *p;
1052 {
1053 
1054 	p->p_stat = SSTOP;
1055 	p->p_flag &= ~P_WAITED;
1056 	wakeup((caddr_t)p->p_pptr);
1057 }
1058 
1059 /*
1060  * Take the action for the specified signal
1061  * from the current set of pending signals.
1062  */
1063 void
1064 postsig(signum)
1065 	register int signum;
1066 {
1067 	register struct proc *p = curproc;
1068 	register struct sigacts *ps = p->p_sigacts;
1069 	register sig_t action;
1070 	u_long code;
1071 	sigset_t *returnmask;
1072 
1073 #ifdef DIAGNOSTIC
1074 	if (signum == 0)
1075 		panic("postsig");
1076 #endif
1077 	sigdelset(&p->p_siglist, signum);
1078 	action = ps->ps_sigact[signum].sa_handler;
1079 #ifdef KTRACE
1080 	if (KTRPOINT(p, KTR_PSIG))
1081 		ktrpsig(p->p_tracep,
1082 		    signum, action, ps->ps_flags & SAS_OLDMASK ?
1083 		    &ps->ps_oldmask : &p->p_sigmask, 0);
1084 #endif
1085 	if (action == SIG_DFL) {
1086 		/*
1087 		 * Default action, where the default is to kill
1088 		 * the process.  (Other cases were ignored above.)
1089 		 */
1090 		sigexit(p, signum);
1091 		/* NOTREACHED */
1092 	} else {
1093 		/*
1094 		 * If we get here, the signal must be caught.
1095 		 */
1096 #ifdef DIAGNOSTIC
1097 		if (action == SIG_IGN || sigismember(&p->p_sigmask, signum))
1098 			panic("postsig action");
1099 #endif
1100 		/*
1101 		 * Set the new mask value and also defer further
1102 		 * occurences of this signal.
1103 		 *
1104 		 * Special case: user has done a sigpause.  Here the
1105 		 * current mask is not of interest, but rather the
1106 		 * mask from before the sigpause is what we want
1107 		 * restored after the signal processing is completed.
1108 		 */
1109 		if (ps->ps_flags & SAS_OLDMASK) {
1110 			returnmask = &ps->ps_oldmask;
1111 			ps->ps_flags &= ~SAS_OLDMASK;
1112 		} else
1113 			returnmask = &p->p_sigmask;
1114 		p->p_stats->p_ru.ru_nsignals++;
1115 		if (ps->ps_sig != signum) {
1116 			code = 0;
1117 		} else {
1118 			code = ps->ps_code;
1119 			ps->ps_code = 0;
1120 			ps->ps_sig = 0;
1121 		}
1122 		(*p->p_emul->e_sendsig)(action, signum, returnmask, code);
1123 		(void) splhigh();
1124 		sigplusset(&ps->ps_sigact[signum].sa_mask, &p->p_sigmask);
1125 		if (ps->ps_sigact[signum].sa_flags & SA_RESETHAND) {
1126 			sigdelset(&p->p_sigcatch, signum);
1127 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1128 				sigaddset(&p->p_sigignore, signum);
1129 			ps->ps_sigact[signum].sa_handler = SIG_DFL;
1130 		}
1131 		(void) spl0();
1132 	}
1133 }
1134 
1135 /*
1136  * Kill the current process for stated reason.
1137  */
1138 void
1139 killproc(p, why)
1140 	struct proc *p;
1141 	char *why;
1142 {
1143 
1144 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1145 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1146 	psignal(p, SIGKILL);
1147 }
1148 
1149 /*
1150  * Force the current process to exit with the specified signal, dumping core
1151  * if appropriate.  We bypass the normal tests for masked and caught signals,
1152  * allowing unrecoverable failures to terminate the process without changing
1153  * signal state.  Mark the accounting record with the signal termination.
1154  * If dumping core, save the signal number for the debugger.  Calls exit and
1155  * does not return.
1156  */
1157 void
1158 sigexit(p, signum)
1159 	register struct proc *p;
1160 	int signum;
1161 {
1162 
1163 	p->p_acflag |= AXSIG;
1164 	if (sigprop[signum] & SA_CORE) {
1165 		p->p_sigacts->ps_sig = signum;
1166 		if (coredump(p) == 0)
1167 			signum |= WCOREFLAG;
1168 	}
1169 	exit1(p, W_EXITCODE(0, signum));
1170 	/* NOTREACHED */
1171 }
1172 
1173 /*
1174  * Dump core, into a file named "progname.core" or "core" (depending on the
1175  * value of shortcorename), unless the process was setuid/setgid.
1176  */
1177 int
1178 coredump(p)
1179 	register struct proc *p;
1180 {
1181 	register struct vnode *vp;
1182 	register struct vmspace *vm = p->p_vmspace;
1183 	register struct ucred *cred = p->p_cred->pc_ucred;
1184 	struct nameidata nd;
1185 	struct vattr vattr;
1186 	int error, error1;
1187 	char name[MAXCOMLEN+6];		/* progname.core */
1188 	struct core core;
1189 	extern int shortcorename;
1190 
1191 	/*
1192 	 * Make sure the process has not set-id, to prevent data leaks.
1193 	 */
1194 	if (p->p_flag & P_SUGID)
1195 		return (EPERM);
1196 
1197 	/*
1198 	 * Refuse to core if the data + stack + user size is larger than
1199 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
1200 	 * data.
1201 	 */
1202 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1203 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1204 		return (EFBIG);		/* better error code? */
1205 
1206 	/*
1207 	 * The core dump will go in the current working directory.  Make
1208 	 * sure that the directory is still there and that the mount flags
1209 	 * allow us to write core dumps there.
1210 	 */
1211 	vp = p->p_fd->fd_cdir;
1212 	if (vp->v_mount == NULL ||
1213 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1214 		return (EPERM);
1215 
1216 	if (shortcorename)
1217 		sprintf(name, "core");
1218 	else
1219 		sprintf(name, "%s.core", p->p_comm);
1220 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1221 	error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR | S_IWUSR);
1222 	if (error)
1223 		return (error);
1224 	vp = nd.ni_vp;
1225 
1226 	/* Don't dump to non-regular files or files with links. */
1227 	if (vp->v_type != VREG ||
1228 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1229 		error = EINVAL;
1230 		goto out;
1231 	}
1232 	VATTR_NULL(&vattr);
1233 	vattr.va_size = 0;
1234 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1235 	VOP_SETATTR(vp, &vattr, cred, p);
1236 	p->p_acflag |= ACORE;
1237 #if 0
1238 	/*
1239 	 * XXX
1240 	 * It would be nice if we at least dumped the signal state (and made it
1241 	 * available at run time to the debugger, as well), but this code
1242 	 * hasn't actually had any effect for a long time, since we don't dump
1243 	 * the user area.  For now, it's dead.
1244 	 */
1245 	memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc));
1246 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1247 #endif
1248 
1249 	core.c_midmag = 0;
1250 	strncpy(core.c_name, p->p_comm, MAXCOMLEN);
1251 	core.c_nseg = 0;
1252 	core.c_signo = p->p_sigacts->ps_sig;
1253 	core.c_ucode = p->p_sigacts->ps_code;
1254 	core.c_cpusize = 0;
1255 	core.c_tsize = (u_long)ctob(vm->vm_tsize);
1256 	core.c_dsize = (u_long)ctob(vm->vm_dsize);
1257 	core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
1258 	error = cpu_coredump(p, vp, cred, &core);
1259 	if (error)
1260 		goto out;
1261 	if (core.c_midmag == 0) {
1262 		/* XXX
1263 		 * cpu_coredump() didn't bother to set the magic; assume
1264 		 * this is a request to do a traditional dump. cpu_coredump()
1265 		 * is still responsible for setting sensible values in
1266 		 * the core header.
1267 		 */
1268 		if (core.c_cpusize == 0)
1269 			core.c_cpusize = USPACE; /* Just in case */
1270 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1271 		    (int)core.c_dsize,
1272 		    (off_t)core.c_cpusize, UIO_USERSPACE,
1273 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1274 		if (error)
1275 			goto out;
1276 		error = vn_rdwr(UIO_WRITE, vp,
1277 		    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1278 		    core.c_ssize,
1279 		    (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
1280 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1281 	} else {
1282 		/*
1283 		 * vm_coredump() spits out all appropriate segments.
1284 		 * All that's left to do is to write the core header.
1285 		 */
1286 #if defined(UVM)
1287 		error = uvm_coredump(p, vp, cred, &core);
1288 #else
1289 		error = vm_coredump(p, vp, cred, &core);
1290 #endif
1291 		if (error)
1292 			goto out;
1293 		error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
1294 		    (int)core.c_hdrsize, (off_t)0,
1295 		    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1296 	}
1297 out:
1298 	VOP_UNLOCK(vp, 0);
1299 	error1 = vn_close(vp, FWRITE, cred, p);
1300 	if (error == 0)
1301 		error = error1;
1302 	return (error);
1303 }
1304 
1305 /*
1306  * Nonexistent system call-- signal process (may want to handle it).
1307  * Flag error in case process won't see signal immediately (blocked or ignored).
1308  */
1309 /* ARGSUSED */
1310 int
1311 sys_nosys(p, v, retval)
1312 	struct proc *p;
1313 	void *v;
1314 	register_t *retval;
1315 {
1316 
1317 	psignal(p, SIGSYS);
1318 	return (ENOSYS);
1319 }
1320