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