xref: /csrg-svn/sys/kern/kern_sig.c (revision 57533)
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.52 (Berkeley) 01/14/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/kinfo_proc.h>
32 #include <sys/user.h>		/* for coredump */
33 
34 /*
35  * Can process p, with pcred pc, send the signal signo to process q?
36  */
37 #define CANSIGNAL(p, pc, q, signo) \
38 	((pc)->pc_ucred->cr_uid == 0 || \
39 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
40 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
41 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
42 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
43 	    ((signo) == SIGCONT && (q)->p_session == (p)->p_session))
44 
45 struct sigaction_args {
46 	int	signo;
47 	struct	sigaction *nsa;
48 	struct	sigaction *osa;
49 };
50 /* ARGSUSED */
51 sigaction(p, uap, retval)
52 	struct proc *p;
53 	register struct sigaction_args *uap;
54 	int *retval;
55 {
56 	struct sigaction vec;
57 	register struct sigaction *sa;
58 	register struct sigacts *ps = p->p_sigacts;
59 	register int sig;
60 	int bit, error;
61 
62 	sig = uap->signo;
63 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
64 		return (EINVAL);
65 	sa = &vec;
66 	if (uap->osa) {
67 		sa->sa_handler = ps->ps_sigact[sig];
68 		sa->sa_mask = ps->ps_catchmask[sig];
69 		bit = sigmask(sig);
70 		sa->sa_flags = 0;
71 		if ((ps->ps_sigonstack & bit) != 0)
72 			sa->sa_flags |= SA_ONSTACK;
73 		if ((ps->ps_sigintr & bit) == 0)
74 			sa->sa_flags |= SA_RESTART;
75 		if (p->p_flag & SNOCLDSTOP)
76 			sa->sa_flags |= SA_NOCLDSTOP;
77 		if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
78 		    sizeof (vec)))
79 			return (error);
80 	}
81 	if (uap->nsa) {
82 		if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
83 		    sizeof (vec)))
84 			return (error);
85 		setsigvec(p, sig, sa);
86 	}
87 	return (0);
88 }
89 
90 setsigvec(p, sig, sa)
91 	register struct proc *p;
92 	int sig;
93 	register struct sigaction *sa;
94 {
95 	register struct sigacts *ps = p->p_sigacts;
96 	register int bit;
97 
98 	bit = sigmask(sig);
99 	/*
100 	 * Change setting atomically.
101 	 */
102 	(void) splhigh();
103 	ps->ps_sigact[sig] = sa->sa_handler;
104 	ps->ps_catchmask[sig] = sa->sa_mask &~ sigcantmask;
105 	if ((sa->sa_flags & SA_RESTART) == 0)
106 		ps->ps_sigintr |= bit;
107 	else
108 		ps->ps_sigintr &= ~bit;
109 	if (sa->sa_flags & SA_ONSTACK)
110 		ps->ps_sigonstack |= bit;
111 	else
112 		ps->ps_sigonstack &= ~bit;
113 #ifdef COMPAT_SUNOS
114 	if (sa->sa_flags & SA_USERTRAMP)
115 		ps->ps_usertramp |= bit;
116 	else
117 		ps->ps_usertramp &= ~bit;
118 #endif
119 	if (sig == SIGCHLD) {
120 		if (sa->sa_flags & SA_NOCLDSTOP)
121 			p->p_flag |= SNOCLDSTOP;
122 		else
123 			p->p_flag &= ~SNOCLDSTOP;
124 	}
125 	/*
126 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
127 	 * and for signals set to SIG_DFL where the default is to ignore.
128 	 * However, don't put SIGCONT in p_sigignore,
129 	 * as we have to restart the process.
130 	 */
131 	if (sa->sa_handler == SIG_IGN ||
132 	    (sigprop[sig] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
133 		p->p_sig &= ~bit;		/* never to be seen again */
134 		if (sig != SIGCONT)
135 			p->p_sigignore |= bit;	/* easier in psignal */
136 		p->p_sigcatch &= ~bit;
137 	} else {
138 		p->p_sigignore &= ~bit;
139 		if (sa->sa_handler == SIG_DFL)
140 			p->p_sigcatch &= ~bit;
141 		else
142 			p->p_sigcatch |= bit;
143 	}
144 	(void) spl0();
145 }
146 
147 /*
148  * Initialize signal state for process 0;
149  * set to ignore signals that are ignored by default.
150  */
151 void
152 siginit(p)
153 	struct proc *p;
154 {
155 	register int i;
156 
157 	for (i = 0; i < NSIG; i++)
158 		if (sigprop[i] & SA_IGNORE && i != SIGCONT)
159 			p->p_sigignore |= sigmask(i);
160 }
161 
162 /*
163  * Reset signals for an exec of the specified process.
164  */
165 void
166 execsigs(p)
167 	register struct proc *p;
168 {
169 	register struct sigacts *ps = p->p_sigacts;
170 	register int nc, mask;
171 
172 	/*
173 	 * Reset caught signals.  Held signals remain held
174 	 * through p_sigmask (unless they were caught,
175 	 * and are now ignored by default).
176 	 */
177 	while (p->p_sigcatch) {
178 		nc = ffs((long)p->p_sigcatch);
179 		mask = sigmask(nc);
180 		p->p_sigcatch &= ~mask;
181 		if (sigprop[nc] & SA_IGNORE) {
182 			if (nc != SIGCONT)
183 				p->p_sigignore |= mask;
184 			p->p_sig &= ~mask;
185 		}
186 		ps->ps_sigact[nc] = SIG_DFL;
187 	}
188 	/*
189 	 * Reset stack state to the user stack.
190 	 * Clear set of signals caught on the signal stack.
191 	 */
192 	ps->ps_sigstk.ss_flags = SA_DISABLE;
193 	ps->ps_sigstk.ss_size = 0;
194 	ps->ps_sigstk.ss_base = 0;
195 	ps->ps_flags = 0;
196 }
197 
198 /*
199  * Manipulate signal mask.
200  * Note that we receive new mask, not pointer,
201  * and return old mask as return value;
202  * the library stub does the rest.
203  */
204 struct sigprocmask_args {
205 	int	how;
206 	sigset_t mask;
207 };
208 sigprocmask(p, uap, retval)
209 	register struct proc *p;
210 	struct sigprocmask_args *uap;
211 	int *retval;
212 {
213 	int error = 0;
214 
215 	*retval = p->p_sigmask;
216 	(void) splhigh();
217 
218 	switch (uap->how) {
219 	case SIG_BLOCK:
220 		p->p_sigmask |= uap->mask &~ sigcantmask;
221 		break;
222 
223 	case SIG_UNBLOCK:
224 		p->p_sigmask &= ~uap->mask;
225 		break;
226 
227 	case SIG_SETMASK:
228 		p->p_sigmask = uap->mask &~ sigcantmask;
229 		break;
230 
231 	default:
232 		error = EINVAL;
233 		break;
234 	}
235 	(void) spl0();
236 	return (error);
237 }
238 
239 struct sigpending_args {
240 	int	dummy;
241 };
242 /* ARGSUSED */
243 sigpending(p, uap, retval)
244 	struct proc *p;
245 	struct sigpending_args *uap;
246 	int *retval;
247 {
248 
249 	*retval = p->p_sig;
250 	return (0);
251 }
252 
253 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
254 /*
255  * Generalized interface signal handler, 4.3-compatible.
256  */
257 struct osigvec_args {
258 	int	signo;
259 	struct	sigvec *nsv;
260 	struct	sigvec *osv;
261 };
262 /* ARGSUSED */
263 osigvec(p, uap, retval)
264 	struct proc *p;
265 	register struct osigvec_args *uap;
266 	int *retval;
267 {
268 	struct sigvec vec;
269 	register struct sigacts *ps = p->p_sigacts;
270 	register struct sigvec *sv;
271 	register int sig;
272 	int bit, error;
273 
274 	sig = uap->signo;
275 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
276 		return (EINVAL);
277 	sv = &vec;
278 	if (uap->osv) {
279 		*(sig_t *)&sv->sv_handler = ps->ps_sigact[sig];
280 		sv->sv_mask = ps->ps_catchmask[sig];
281 		bit = sigmask(sig);
282 		sv->sv_flags = 0;
283 		if ((ps->ps_sigonstack & bit) != 0)
284 			sv->sv_flags |= SV_ONSTACK;
285 		if ((ps->ps_sigintr & bit) != 0)
286 			sv->sv_flags |= SV_INTERRUPT;
287 #ifndef COMPAT_SUNOS
288 		if (p->p_flag & SNOCLDSTOP)
289 			sv->sv_flags |= SA_NOCLDSTOP;
290 #endif
291 		if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
292 		    sizeof (vec)))
293 			return (error);
294 	}
295 	if (uap->nsv) {
296 		if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
297 		    sizeof (vec)))
298 			return (error);
299 #ifdef COMPAT_SUNOS
300 		/*
301 		 * SunOS uses this bit (4, aka SA_DISABLE) as SV_RESETHAND,
302 		 * `reset to SIG_DFL on delivery'. We have no such option
303 		 * now or ever!
304 		 */
305 		if (sv->sv_flags & SA_DISABLE)
306 			return (EINVAL);
307 		sv->sv_flags |= SA_USERTRAMP;
308 #endif
309 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
310 		setsigvec(p, sig, (struct sigaction *)sv);
311 	}
312 	return (0);
313 }
314 
315 struct osigblock_args {
316 	int	mask;
317 };
318 osigblock(p, uap, retval)
319 	register struct proc *p;
320 	struct osigblock_args *uap;
321 	int *retval;
322 {
323 
324 	(void) splhigh();
325 	*retval = p->p_sigmask;
326 	p->p_sigmask |= uap->mask &~ sigcantmask;
327 	(void) spl0();
328 	return (0);
329 }
330 
331 struct osigsetmask_args {
332 	int	mask;
333 };
334 osigsetmask(p, uap, retval)
335 	struct proc *p;
336 	struct osigsetmask_args *uap;
337 	int *retval;
338 {
339 
340 	(void) splhigh();
341 	*retval = p->p_sigmask;
342 	p->p_sigmask = uap->mask &~ sigcantmask;
343 	(void) spl0();
344 	return (0);
345 }
346 #endif /* COMPAT_43 || COMPAT_SUNOS */
347 
348 /*
349  * Suspend process until signal, providing mask to be set
350  * in the meantime.  Note nonstandard calling convention:
351  * libc stub passes mask, not pointer, to save a copyin.
352  */
353 struct sigsuspend_args {
354 	sigset_t mask;
355 };
356 /* ARGSUSED */
357 sigsuspend(p, uap, retval)
358 	register struct proc *p;
359 	struct sigsuspend_args *uap;
360 	int *retval;
361 {
362 	register struct sigacts *ps = p->p_sigacts;
363 
364 	/*
365 	 * When returning from sigpause, we want
366 	 * the old mask to be restored after the
367 	 * signal handler has finished.  Thus, we
368 	 * save it here and mark the sigacts structure
369 	 * to indicate this.
370 	 */
371 	ps->ps_oldmask = p->p_sigmask;
372 	ps->ps_flags |= SAS_OLDMASK;
373 	p->p_sigmask = uap->mask &~ sigcantmask;
374 	(void) tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0);
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 && (sig == SIGKILL ||
666 	    sig == SIGTERM && (p->p_flag&STRC || action != SIG_DFL)))
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 		 * When a sleeping process receives a stop
713 		 * signal, process immediately if possible.
714 		 * All other (caught or default) signals
715 		 * cause the process to run.
716 		 */
717 		if (prop & SA_STOP) {
718 			if (action != SIG_DFL)
719 				goto runfast;
720 			/*
721 			 * If a child holding parent blocked,
722 			 * stopping could cause deadlock.
723 			 */
724 			if (p->p_flag&SPPWAIT)
725 				goto out;
726 			p->p_sig &= ~mask;
727 			p->p_xstat = sig;
728 			if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
729 				psignal(p->p_pptr, SIGCHLD);
730 			stop(p);
731 			goto out;
732 		} else
733 			goto runfast;
734 		/*NOTREACHED*/
735 
736 	case SSTOP:
737 		/*
738 		 * If traced process is already stopped,
739 		 * then no further action is necessary.
740 		 */
741 		if (p->p_flag&STRC)
742 			goto out;
743 
744 		/*
745 		 * Kill signal always sets processes running.
746 		 */
747 		if (sig == SIGKILL)
748 			goto runfast;
749 
750 		if (prop & SA_CONT) {
751 			/*
752 			 * If SIGCONT is default (or ignored), we continue
753 			 * the process but don't leave the signal in p_sig,
754 			 * as it has no further action.  If SIGCONT is held,
755 			 * continue the process and leave the signal in p_sig.
756 			 * If the process catches SIGCONT, let it handle
757 			 * the signal itself.  If it isn't waiting on
758 			 * an event, then it goes back to run state.
759 			 * Otherwise, process goes back to sleep state.
760 			 */
761 			if (action == SIG_DFL)
762 				p->p_sig &= ~mask;
763 			if (action == SIG_CATCH)
764 				goto runfast;
765 			if (p->p_wchan == 0)
766 				goto run;
767 			p->p_stat = SSLEEP;
768 			goto out;
769 		}
770 
771 		if (prop & SA_STOP) {
772 			/*
773 			 * Already stopped, don't need to stop again.
774 			 * (If we did the shell could get confused.)
775 			 */
776 			p->p_sig &= ~mask;		/* take it away */
777 			goto out;
778 		}
779 
780 		/*
781 		 * If process is sleeping interruptibly, then
782 		 * simulate a wakeup so that when it is continued,
783 		 * it will be made runnable and can look at the signal.
784 		 * But don't setrun the process, leave it stopped.
785 		 */
786 		if (p->p_wchan && p->p_flag & SSINTR)
787 			unsleep(p);
788 		goto out;
789 
790 	default:
791 		/*
792 		 * SRUN, SIDL, SZOMB do nothing with the signal,
793 		 * other than kicking ourselves if we are running.
794 		 * It will either never be noticed, or noticed very soon.
795 		 */
796 		if (p == curproc)
797 			signotify(p);
798 		goto out;
799 	}
800 	/*NOTREACHED*/
801 
802 runfast:
803 	/*
804 	 * Raise priority to at least PUSER.
805 	 */
806 	if (p->p_pri > PUSER)
807 		p->p_pri = PUSER;
808 run:
809 	setrun(p);
810 out:
811 	splx(s);
812 }
813 
814 /*
815  * If the current process has a signal to process (should be caught
816  * or cause termination, should interrupt current syscall),
817  * return the signal number.  Stop signals with default action
818  * are processed immediately, then cleared; they aren't returned.
819  * This is checked after each entry to the system for a syscall
820  * or trap (though this can usually be done without actually calling
821  * issig by checking the pending signal masks in the CURSIG macro.)
822  * The normal call sequence is
823  *
824  *	while (sig = CURSIG(curproc))
825  *		psig(sig);
826  */
827 issig(p)
828 	register struct proc *p;
829 {
830 	register int sig, mask, prop;
831 
832 	for (;;) {
833 		mask = p->p_sig &~ p->p_sigmask;
834 		if (p->p_flag&SPPWAIT)
835 			mask &= ~stopsigmask;
836 		if (mask == 0)	 	/* no signal to send */
837 			return (0);
838 		sig = ffs((long)mask);
839 		mask = sigmask(sig);
840 		prop = sigprop[sig];
841 		/*
842 		 * We should see pending but ignored signals
843 		 * only if STRC was on when they were posted.
844 		 */
845 		if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
846 			p->p_sig &= ~mask;
847 			continue;
848 		}
849 		if (p->p_flag&STRC && (p->p_flag&SPPWAIT) == 0) {
850 			/*
851 			 * If traced, always stop, and stay
852 			 * stopped until released by the parent.
853 			 */
854 			p->p_xstat = sig;
855 			psignal(p->p_pptr, SIGCHLD);
856 			do {
857 				stop(p);
858 				swtch();
859 			} while (!procxmt(p) && p->p_flag&STRC);
860 
861 			/*
862 			 * If the traced bit got turned off,
863 			 * go back up to the top to rescan signals.
864 			 * This ensures that p_sig* and ps_sigact
865 			 * are consistent.
866 			 */
867 			if ((p->p_flag&STRC) == 0)
868 				continue;
869 
870 			/*
871 			 * If parent wants us to take the signal,
872 			 * then it will leave it in p->p_xstat;
873 			 * otherwise we just look for signals again.
874 			 */
875 			p->p_sig &= ~mask;	/* clear the old signal */
876 			sig = p->p_xstat;
877 			if (sig == 0)
878 				continue;
879 
880 			/*
881 			 * Put the new signal into p_sig.
882 			 * If signal is being masked,
883 			 * look for other signals.
884 			 */
885 			mask = sigmask(sig);
886 			p->p_sig |= mask;
887 			if (p->p_sigmask & mask)
888 				continue;
889 		}
890 
891 		/*
892 		 * Decide whether the signal should be returned.
893 		 * Return the signal's number, or fall through
894 		 * to clear it from the pending mask.
895 		 */
896 		switch ((int)p->p_sigacts->ps_sigact[sig]) {
897 
898 		case SIG_DFL:
899 			/*
900 			 * Don't take default actions on system processes.
901 			 */
902 			if (p->p_pid <= 1) {
903 #ifdef DIAGNOSTIC
904 				/*
905 				 * Are you sure you want to ignore SIGSEGV
906 				 * in init? XXX
907 				 */
908 				printf("Process (pid %d) got signal %d\n",
909 					p->p_pid, sig);
910 #endif
911 				break;		/* == ignore */
912 			}
913 			/*
914 			 * If there is a pending stop signal to process
915 			 * with default action, stop here,
916 			 * then clear the signal.  However,
917 			 * if process is member of an orphaned
918 			 * process group, ignore tty stop signals.
919 			 */
920 			if (prop & SA_STOP) {
921 				if (p->p_flag&STRC ||
922 		    		    (p->p_pgrp->pg_jobc == 0 &&
923 				    prop & SA_TTYSTOP))
924 					break;	/* == ignore */
925 				p->p_xstat = sig;
926 				stop(p);
927 				if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
928 					psignal(p->p_pptr, SIGCHLD);
929 				swtch();
930 				break;
931 			} else if (prop & SA_IGNORE) {
932 				/*
933 				 * Except for SIGCONT, shouldn't get here.
934 				 * Default action is to ignore; drop it.
935 				 */
936 				break;		/* == ignore */
937 			} else
938 				return (sig);
939 			/*NOTREACHED*/
940 
941 		case SIG_IGN:
942 			/*
943 			 * Masking above should prevent us ever trying
944 			 * to take action on an ignored signal other
945 			 * than SIGCONT, unless process is traced.
946 			 */
947 			if ((prop & SA_CONT) == 0 && (p->p_flag&STRC) == 0)
948 				printf("issig\n");
949 			break;		/* == ignore */
950 
951 		default:
952 			/*
953 			 * This signal has an action, let
954 			 * psig process it.
955 			 */
956 			return (sig);
957 		}
958 		p->p_sig &= ~mask;		/* take the signal! */
959 	}
960 	/* NOTREACHED */
961 }
962 
963 /*
964  * Put the argument process into the stopped
965  * state and notify the parent via wakeup.
966  * Signals are handled elsewhere.
967  * The process must not be on the run queue.
968  */
969 stop(p)
970 	register struct proc *p;
971 {
972 
973 	p->p_stat = SSTOP;
974 	p->p_flag &= ~SWTED;
975 	wakeup((caddr_t)p->p_pptr);
976 }
977 
978 /*
979  * Take the action for the specified signal
980  * from the current set of pending signals.
981  */
982 void
983 psig(sig)
984 	register int sig;
985 {
986 	register struct proc *p = curproc;
987 	register struct sigacts *ps = p->p_sigacts;
988 	register sig_t action;
989 	int mask, returnmask;
990 
991 #ifdef DIAGNOSTIC
992 	if (sig == 0)
993 		panic("psig");
994 #endif
995 	mask = sigmask(sig);
996 	p->p_sig &= ~mask;
997 	action = ps->ps_sigact[sig];
998 #ifdef KTRACE
999 	if (KTRPOINT(p, KTR_PSIG))
1000 		ktrpsig(p->p_tracep, sig, action, ps->ps_flags & SAS_OLDMASK ?
1001 		    ps->ps_oldmask : p->p_sigmask, 0);
1002 #endif
1003 	if (action == SIG_DFL) {
1004 		/*
1005 		 * Default action, where the default is to kill
1006 		 * the process.  (Other cases were ignored above.)
1007 		 */
1008 		sigexit(p, sig);
1009 		/* NOTREACHED */
1010 	} else {
1011 		/*
1012 		 * If we get here, the signal must be caught.
1013 		 */
1014 #ifdef DIAGNOSTIC
1015 		if (action == SIG_IGN || (p->p_sigmask & mask))
1016 			panic("psig action");
1017 #endif
1018 		/*
1019 		 * Set the new mask value and also defer further
1020 		 * occurences of this signal.
1021 		 *
1022 		 * Special case: user has done a sigpause.  Here the
1023 		 * current mask is not of interest, but rather the
1024 		 * mask from before the sigpause is what we want
1025 		 * restored after the signal processing is completed.
1026 		 */
1027 		(void) splhigh();
1028 		if (ps->ps_flags & SAS_OLDMASK) {
1029 			returnmask = ps->ps_oldmask;
1030 			ps->ps_flags &= ~SAS_OLDMASK;
1031 		} else
1032 			returnmask = p->p_sigmask;
1033 		p->p_sigmask |= ps->ps_catchmask[sig] | mask;
1034 		(void) spl0();
1035 		p->p_stats->p_ru.ru_nsignals++;
1036 		sendsig(action, sig, returnmask, 0);
1037 	}
1038 }
1039 
1040 /*
1041  * Kill the current process for stated reason.
1042  */
1043 killproc(p, why)
1044 	struct proc *p;
1045 	char *why;
1046 {
1047 
1048 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1049 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1050 	psignal(p, SIGKILL);
1051 }
1052 
1053 /*
1054  * Force the current process to exit with the specified
1055  * signal, dumping core if appropriate.  We bypass the normal
1056  * tests for masked and caught signals, allowing unrecoverable
1057  * failures to terminate the process without changing signal state.
1058  * Mark the accounting record with the signal termination.
1059  * If dumping core, save the signal number for the debugger.
1060  * Calls exit and does not return.
1061  */
1062 sigexit(p, sig)
1063 	register struct proc *p;
1064 	int sig;
1065 {
1066 
1067 	p->p_acflag |= AXSIG;
1068 	if (sigprop[sig] & SA_CORE) {
1069 		p->p_sigacts->ps_sig = sig;
1070 		if (coredump(p) == 0)
1071 			sig |= WCOREFLAG;
1072 	}
1073 	exit(p, W_EXITCODE(0, sig));
1074 	/* NOTREACHED */
1075 }
1076 
1077 /*
1078  * Create a core dump.
1079  * The file name is "core.progname".
1080  * Core dumps are not created if the process is setuid.
1081  */
1082 coredump(p)
1083 	register struct proc *p;
1084 {
1085 	register struct vnode *vp;
1086 	register struct pcred *pcred = p->p_cred;
1087 	register struct ucred *cred = pcred->pc_ucred;
1088 	register struct vmspace *vm = p->p_vmspace;
1089 	struct vattr vattr;
1090 	int error, error1;
1091 	struct nameidata nd;
1092 	char name[MAXCOMLEN+6];	/* core.progname */
1093 
1094 	if (pcred->p_svuid != pcred->p_ruid ||
1095 	    pcred->p_svgid != pcred->p_rgid)
1096 		return (EFAULT);
1097 	if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >=
1098 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1099 		return (EFAULT);
1100 	sprintf(name, "core.%s", p->p_comm);
1101 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1102 	if (error = vn_open(&nd, O_CREAT|FWRITE, 0644))
1103 		return (error);
1104 	vp = nd.ni_vp;
1105 	if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred, p) ||
1106 	    vattr.va_nlink != 1) {
1107 		error = EFAULT;
1108 		goto out;
1109 	}
1110 	VATTR_NULL(&vattr);
1111 	vattr.va_size = 0;
1112 	LEASE_CHECK(vp, p, cred, LEASE_WRITE);
1113 	VOP_SETATTR(vp, &vattr, cred, p);
1114 	p->p_acflag |= ACORE;
1115 	bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1116 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1117 	error = cpu_coredump(p, vp, cred);
1118 	if (error == 0)
1119 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1120 		    (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
1121 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1122 	if (error == 0)
1123 		error = vn_rdwr(UIO_WRITE, vp,
1124 		    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1125 		    round_page(ctob(vm->vm_ssize)),
1126 		    (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE,
1127 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1128 out:
1129 	VOP_UNLOCK(vp);
1130 	error1 = vn_close(vp, FWRITE, cred, p);
1131 	if (error == 0)
1132 		error = error1;
1133 	return (error);
1134 }
1135 
1136 /*
1137  * Nonexistent system call-- signal process (may want to handle it).
1138  * Flag error in case process won't see signal immediately (blocked or ignored).
1139  */
1140 struct nosys_args {
1141 	int	dummy;
1142 };
1143 /* ARGSUSED */
1144 nosys(p, args, retval)
1145 	struct proc *p;
1146 	struct nosys_args *args;
1147 	int *retval;
1148 {
1149 
1150 	psignal(p, SIGSYS);
1151 	return (EINVAL);
1152 }
1153