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