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