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