xref: /original-bsd/sys/kern/kern_sig.c (revision 4670e840)
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.55 (Berkeley) 03/02/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 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
375 		/* void */;
376 	/* always return EINTR rather than ERESTART... */
377 	return (EINTR);
378 }
379 
380 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
381 struct osigstack_args {
382 	struct	sigstack *nss;
383 	struct	sigstack *oss;
384 };
385 /* ARGSUSED */
386 osigstack(p, uap, retval)
387 	struct proc *p;
388 	register struct osigstack_args *uap;
389 	int *retval;
390 {
391 	struct sigstack ss;
392 	struct sigacts *psp;
393 	int error = 0;
394 
395 	psp = p->p_sigacts;
396 	ss.ss_sp = psp->ps_sigstk.ss_base;
397 	ss.ss_onstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
398 	if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
399 	    sizeof (struct sigstack))))
400 		return (error);
401 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
402 	    sizeof (ss))) == 0) {
403 		psp->ps_sigstk.ss_base = ss.ss_sp;
404 		psp->ps_sigstk.ss_size = 0;
405 		psp->ps_sigstk.ss_flags |= ss.ss_onstack & SA_ONSTACK;
406 		psp->ps_flags |= SAS_ALTSTACK;
407 	}
408 	return (error);
409 }
410 #endif /* COMPAT_43 || COMPAT_SUNOS */
411 
412 struct sigaltstack_args {
413 	struct	sigaltstack *nss;
414 	struct	sigaltstack *oss;
415 };
416 /* ARGSUSED */
417 sigaltstack(p, uap, retval)
418 	struct proc *p;
419 	register struct sigaltstack_args *uap;
420 	int *retval;
421 {
422 	struct sigacts *psp;
423 	struct sigaltstack ss;
424 	int error;
425 
426 	psp = p->p_sigacts;
427 	if ((psp->ps_flags & SAS_ALTSTACK) == 0)
428 		psp->ps_sigstk.ss_flags |= SA_DISABLE;
429 	if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
430 	    (caddr_t)uap->oss, sizeof (struct sigaltstack))))
431 		return (error);
432 	if (uap->nss == 0)
433 		return (0);
434 	if (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss)))
435 		return (error);
436 	if (ss.ss_flags & SA_DISABLE) {
437 		if (psp->ps_sigstk.ss_flags & SA_ONSTACK)
438 			return (EINVAL);
439 		psp->ps_flags &= ~SAS_ALTSTACK;
440 		psp->ps_sigstk.ss_flags = ss.ss_flags;
441 		return (0);
442 	}
443 	if (ss.ss_size < MINSIGSTKSZ)
444 		return (ENOMEM);
445 	psp->ps_flags |= SAS_ALTSTACK;
446 	psp->ps_sigstk= ss;
447 	return (0);
448 }
449 
450 struct kill_args {
451 	int	pid;
452 	int	signo;
453 };
454 /* ARGSUSED */
455 kill(cp, uap, retval)
456 	register struct proc *cp;
457 	register struct kill_args *uap;
458 	int *retval;
459 {
460 	register struct proc *p;
461 	register struct pcred *pc = cp->p_cred;
462 
463 	if ((unsigned) uap->signo >= NSIG)
464 		return (EINVAL);
465 	if (uap->pid > 0) {
466 		/* kill single process */
467 		p = pfind(uap->pid);
468 		if (p == 0)
469 			return (ESRCH);
470 		if (!CANSIGNAL(cp, pc, p, uap->signo))
471 			return (EPERM);
472 		if (uap->signo)
473 			psignal(p, uap->signo);
474 		return (0);
475 	}
476 	switch (uap->pid) {
477 	case -1:		/* broadcast signal */
478 		return (killpg1(cp, uap->signo, 0, 1));
479 	case 0:			/* signal own process group */
480 		return (killpg1(cp, uap->signo, 0, 0));
481 	default:		/* negative explicit process group */
482 		return (killpg1(cp, uap->signo, -uap->pid, 0));
483 	}
484 	/* NOTREACHED */
485 }
486 
487 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
488 struct okillpg_args {
489 	int	pgid;
490 	int	signo;
491 };
492 /* ARGSUSED */
493 okillpg(p, uap, retval)
494 	struct proc *p;
495 	register struct okillpg_args *uap;
496 	int *retval;
497 {
498 
499 	if ((unsigned) uap->signo >= NSIG)
500 		return (EINVAL);
501 	return (killpg1(p, uap->signo, uap->pgid, 0));
502 }
503 #endif /* COMPAT_43 || COMPAT_SUNOS */
504 
505 /*
506  * Common code for kill process group/broadcast kill.
507  * cp is calling process.
508  */
509 killpg1(cp, signo, pgid, all)
510 	register struct proc *cp;
511 	int signo, pgid, all;
512 {
513 	register struct proc *p;
514 	register struct pcred *pc = cp->p_cred;
515 	struct pgrp *pgrp;
516 	int nfound = 0;
517 
518 	if (all)
519 		/*
520 		 * broadcast
521 		 */
522 		for (p = (struct proc *)allproc; p != NULL; p = p->p_nxt) {
523 			if (p->p_pid <= 1 || p->p_flag & SSYS ||
524 			    p == cp || !CANSIGNAL(cp, pc, p, signo))
525 				continue;
526 			nfound++;
527 			if (signo)
528 				psignal(p, signo);
529 		}
530 	else {
531 		if (pgid == 0)
532 			/*
533 			 * zero pgid means send to my process group.
534 			 */
535 			pgrp = cp->p_pgrp;
536 		else {
537 			pgrp = pgfind(pgid);
538 			if (pgrp == NULL)
539 				return (ESRCH);
540 		}
541 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
542 			if (p->p_pid <= 1 || p->p_flag & SSYS ||
543 			    p->p_stat == SZOMB || !CANSIGNAL(cp, pc, p, signo))
544 				continue;
545 			nfound++;
546 			if (signo)
547 				psignal(p, signo);
548 		}
549 	}
550 	return (nfound ? 0 : ESRCH);
551 }
552 
553 /*
554  * Send the specified signal to
555  * all processes with 'pgid' as
556  * process group.
557  */
558 void
559 gsignal(pgid, sig)
560 	int pgid, sig;
561 {
562 	struct pgrp *pgrp;
563 
564 	if (pgid && (pgrp = pgfind(pgid)))
565 		pgsignal(pgrp, sig, 0);
566 }
567 
568 /*
569  * Send sig to every member of a process group.
570  * If checktty is 1, limit to members which have a controlling
571  * terminal.
572  */
573 void
574 pgsignal(pgrp, sig, checkctty)
575 	struct pgrp *pgrp;
576 	int sig, checkctty;
577 {
578 	register struct proc *p;
579 
580 	if (pgrp)
581 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
582 			if (checkctty == 0 || p->p_flag & SCTTY)
583 				psignal(p, sig);
584 }
585 
586 /*
587  * Send a signal caused by a trap to the current process.
588  * If it will be caught immediately, deliver it with correct code.
589  * Otherwise, post it normally.
590  */
591 void
592 trapsignal(p, sig, code)
593 	struct proc *p;
594 	register int sig;
595 	unsigned code;
596 {
597 	register struct sigacts *ps = p->p_sigacts;
598 	int mask;
599 
600 	mask = sigmask(sig);
601 	if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 &&
602 	    (p->p_sigmask & mask) == 0) {
603 		p->p_stats->p_ru.ru_nsignals++;
604 #ifdef KTRACE
605 		if (KTRPOINT(p, KTR_PSIG))
606 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[sig],
607 				p->p_sigmask, code);
608 #endif
609 		sendsig(ps->ps_sigact[sig], sig, p->p_sigmask, code);
610 		p->p_sigmask |= ps->ps_catchmask[sig] | mask;
611 	} else {
612 		ps->ps_code = code;	/* XXX for core dump/debugger */
613 		psignal(p, sig);
614 	}
615 }
616 
617 /*
618  * Send the specified signal to the specified process.
619  * If the signal has an action, the action is usually performed
620  * by the target process rather than the caller; we simply add
621  * the signal to the set of pending signals for the process.
622  * Exceptions:
623  *   o When a stop signal is sent to a sleeping process that takes the default
624  *     action, the process is stopped without awakening it.
625  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
626  *     regardless of the signal action (eg, blocked or ignored).
627  * Other ignored signals are discarded immediately.
628  */
629 void
630 psignal(p, sig)
631 	register struct proc *p;
632 	register int sig;
633 {
634 	register int s, prop;
635 	register sig_t action;
636 	int mask;
637 
638 	if ((unsigned)sig >= NSIG || sig == 0)
639 		panic("psignal sig");
640 	mask = sigmask(sig);
641 	prop = sigprop[sig];
642 
643 	/*
644 	 * If proc is traced, always give parent a chance.
645 	 */
646 	if (p->p_flag & STRC)
647 		action = SIG_DFL;
648 	else {
649 		/*
650 		 * If the signal is being ignored,
651 		 * then we forget about it immediately.
652 		 * (Note: we don't set SIGCONT in p_sigignore,
653 		 * and if it is set to SIG_IGN,
654 		 * action will be SIG_DFL here.)
655 		 */
656 		if (p->p_sigignore & mask)
657 			return;
658 		if (p->p_sigmask & mask)
659 			action = SIG_HOLD;
660 		else if (p->p_sigcatch & mask)
661 			action = SIG_CATCH;
662 		else
663 			action = SIG_DFL;
664 	}
665 
666 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
667 	    (p->p_flag & STRC) == 0)
668 		p->p_nice = NZERO;
669 
670 	if (prop & SA_CONT)
671 		p->p_sig &= ~stopsigmask;
672 
673 	if (prop & SA_STOP) {
674 		/*
675 		 * If sending a tty stop signal to a member of an orphaned
676 		 * process group, discard the signal here if the action
677 		 * is default; don't stop the process below if sleeping,
678 		 * and don't clear any pending SIGCONT.
679 		 */
680 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
681 		    action == SIG_DFL)
682 		        return;
683 		p->p_sig &= ~contsigmask;
684 	}
685 	p->p_sig |= mask;
686 
687 	/*
688 	 * Defer further processing for signals which are held,
689 	 * except that stopped processes must be continued by SIGCONT.
690 	 */
691 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
692 		return;
693 	s = splhigh();
694 	switch (p->p_stat) {
695 
696 	case SSLEEP:
697 		/*
698 		 * If process is sleeping uninterruptibly
699 		 * we can't interrupt the sleep... the signal will
700 		 * be noticed when the process returns through
701 		 * trap() or syscall().
702 		 */
703 		if ((p->p_flag & SSINTR) == 0)
704 			goto out;
705 		/*
706 		 * Process is sleeping and traced... make it runnable
707 		 * so it can discover the signal in issig() and stop
708 		 * for the parent.
709 		 */
710 		if (p->p_flag & STRC)
711 			goto run;
712 		/*
713 		 * If SIGCONT is default (or ignored) and process is
714 		 * asleep, we are finished; the process should not
715 		 * be awakened.
716 		 */
717 		if ((prop & SA_CONT) && action == SIG_DFL) {
718 			p->p_sig &= ~mask;
719 			goto out;
720 		}
721 		/*
722 		 * When a sleeping process receives a stop
723 		 * signal, process immediately if possible.
724 		 * All other (caught or default) signals
725 		 * cause the process to run.
726 		 */
727 		if (prop & SA_STOP) {
728 			if (action != SIG_DFL)
729 				goto runfast;
730 			/*
731 			 * If a child holding parent blocked,
732 			 * stopping could cause deadlock.
733 			 */
734 			if (p->p_flag & SPPWAIT)
735 				goto out;
736 			p->p_sig &= ~mask;
737 			p->p_xstat = sig;
738 			if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
739 				psignal(p->p_pptr, SIGCHLD);
740 			stop(p);
741 			goto out;
742 		} else
743 			goto runfast;
744 		/*NOTREACHED*/
745 
746 	case SSTOP:
747 		/*
748 		 * If traced process is already stopped,
749 		 * then no further action is necessary.
750 		 */
751 		if (p->p_flag & STRC)
752 			goto out;
753 
754 		/*
755 		 * Kill signal always sets processes running.
756 		 */
757 		if (sig == SIGKILL)
758 			goto runfast;
759 
760 		if (prop & SA_CONT) {
761 			/*
762 			 * If SIGCONT is default (or ignored), we continue
763 			 * the process but don't leave the signal in p_sig,
764 			 * as it has no further action.  If SIGCONT is held,
765 			 * continue the process and leave the signal in p_sig.
766 			 * If the process catches SIGCONT, let it handle
767 			 * the signal itself.  If it isn't waiting on
768 			 * an event, then it goes back to run state.
769 			 * Otherwise, process goes back to sleep state.
770 			 */
771 			if (action == SIG_DFL)
772 				p->p_sig &= ~mask;
773 			if (action == SIG_CATCH)
774 				goto runfast;
775 			if (p->p_wchan == 0)
776 				goto run;
777 			p->p_stat = SSLEEP;
778 			goto out;
779 		}
780 
781 		if (prop & SA_STOP) {
782 			/*
783 			 * Already stopped, don't need to stop again.
784 			 * (If we did the shell could get confused.)
785 			 */
786 			p->p_sig &= ~mask;		/* take it away */
787 			goto out;
788 		}
789 
790 		/*
791 		 * If process is sleeping interruptibly, then
792 		 * simulate a wakeup so that when it is continued,
793 		 * it will be made runnable and can look at the signal.
794 		 * But don't setrun the process, leave it stopped.
795 		 */
796 		if (p->p_wchan && p->p_flag & SSINTR)
797 			unsleep(p);
798 		goto out;
799 
800 	default:
801 		/*
802 		 * SRUN, SIDL, SZOMB do nothing with the signal,
803 		 * other than kicking ourselves if we are running.
804 		 * It will either never be noticed, or noticed very soon.
805 		 */
806 		if (p == curproc)
807 			signotify(p);
808 		goto out;
809 	}
810 	/*NOTREACHED*/
811 
812 runfast:
813 	/*
814 	 * Raise priority to at least PUSER.
815 	 */
816 	if (p->p_pri > PUSER)
817 		p->p_pri = PUSER;
818 run:
819 	setrun(p);
820 out:
821 	splx(s);
822 }
823 
824 /*
825  * If the current process has a signal to process (should be caught
826  * or cause termination, should interrupt current syscall),
827  * return the signal number.  Stop signals with default action
828  * are processed immediately, then cleared; they aren't returned.
829  * This is checked after each entry to the system for a syscall
830  * or trap (though this can usually be done without actually calling
831  * issig by checking the pending signal masks in the CURSIG macro.)
832  * The normal call sequence is
833  *
834  *	while (sig = CURSIG(curproc))
835  *		psig(sig);
836  */
837 issig(p)
838 	register struct proc *p;
839 {
840 	register int sig, mask, prop;
841 
842 	for (;;) {
843 		mask = p->p_sig &~ p->p_sigmask;
844 		if (p->p_flag & SPPWAIT)
845 			mask &= ~stopsigmask;
846 		if (mask == 0)	 	/* no signal to send */
847 			return (0);
848 		sig = ffs((long)mask);
849 		mask = sigmask(sig);
850 		prop = sigprop[sig];
851 		/*
852 		 * We should see pending but ignored signals
853 		 * only if STRC was on when they were posted.
854 		 */
855 		if (mask & p->p_sigignore && (p->p_flag & STRC) == 0) {
856 			p->p_sig &= ~mask;
857 			continue;
858 		}
859 		if (p->p_flag & STRC && (p->p_flag & SPPWAIT) == 0) {
860 			/*
861 			 * If traced, always stop, and stay
862 			 * stopped until released by the parent.
863 			 */
864 			p->p_xstat = sig;
865 			psignal(p->p_pptr, SIGCHLD);
866 			do {
867 				stop(p);
868 				swtch();
869 			} while (!procxmt(p) && p->p_flag & STRC);
870 
871 			/*
872 			 * If the traced bit got turned off,
873 			 * go back up to the top to rescan signals.
874 			 * This ensures that p_sig* and ps_sigact
875 			 * are consistent.
876 			 */
877 			if ((p->p_flag & STRC) == 0)
878 				continue;
879 
880 			/*
881 			 * If parent wants us to take the signal,
882 			 * then it will leave it in p->p_xstat;
883 			 * otherwise we just look for signals again.
884 			 */
885 			p->p_sig &= ~mask;	/* clear the old signal */
886 			sig = p->p_xstat;
887 			if (sig == 0)
888 				continue;
889 
890 			/*
891 			 * Put the new signal into p_sig.
892 			 * If signal is being masked,
893 			 * look for other signals.
894 			 */
895 			mask = sigmask(sig);
896 			p->p_sig |= mask;
897 			if (p->p_sigmask & mask)
898 				continue;
899 		}
900 
901 		/*
902 		 * Decide whether the signal should be returned.
903 		 * Return the signal's number, or fall through
904 		 * to clear it from the pending mask.
905 		 */
906 		switch ((int)p->p_sigacts->ps_sigact[sig]) {
907 
908 		case SIG_DFL:
909 			/*
910 			 * Don't take default actions on system processes.
911 			 */
912 			if (p->p_pid <= 1) {
913 #ifdef DIAGNOSTIC
914 				/*
915 				 * Are you sure you want to ignore SIGSEGV
916 				 * in init? XXX
917 				 */
918 				printf("Process (pid %d) got signal %d\n",
919 					p->p_pid, sig);
920 #endif
921 				break;		/* == ignore */
922 			}
923 			/*
924 			 * If there is a pending stop signal to process
925 			 * with default action, stop here,
926 			 * then clear the signal.  However,
927 			 * if process is member of an orphaned
928 			 * process group, ignore tty stop signals.
929 			 */
930 			if (prop & SA_STOP) {
931 				if (p->p_flag & STRC ||
932 		    		    (p->p_pgrp->pg_jobc == 0 &&
933 				    prop & SA_TTYSTOP))
934 					break;	/* == ignore */
935 				p->p_xstat = sig;
936 				stop(p);
937 				if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
938 					psignal(p->p_pptr, SIGCHLD);
939 				swtch();
940 				break;
941 			} else if (prop & SA_IGNORE) {
942 				/*
943 				 * Except for SIGCONT, shouldn't get here.
944 				 * Default action is to ignore; drop it.
945 				 */
946 				break;		/* == ignore */
947 			} else
948 				return (sig);
949 			/*NOTREACHED*/
950 
951 		case SIG_IGN:
952 			/*
953 			 * Masking above should prevent us ever trying
954 			 * to take action on an ignored signal other
955 			 * than SIGCONT, unless process is traced.
956 			 */
957 			if ((prop & SA_CONT) == 0 && (p->p_flag & STRC) == 0)
958 				printf("issig\n");
959 			break;		/* == ignore */
960 
961 		default:
962 			/*
963 			 * This signal has an action, let
964 			 * psig process it.
965 			 */
966 			return (sig);
967 		}
968 		p->p_sig &= ~mask;		/* take the signal! */
969 	}
970 	/* NOTREACHED */
971 }
972 
973 /*
974  * Put the argument process into the stopped
975  * state and notify the parent via wakeup.
976  * Signals are handled elsewhere.
977  * The process must not be on the run queue.
978  */
979 stop(p)
980 	register struct proc *p;
981 {
982 
983 	p->p_stat = SSTOP;
984 	p->p_flag &= ~SWTED;
985 	wakeup((caddr_t)p->p_pptr);
986 }
987 
988 /*
989  * Take the action for the specified signal
990  * from the current set of pending signals.
991  */
992 void
993 psig(sig)
994 	register int sig;
995 {
996 	register struct proc *p = curproc;
997 	register struct sigacts *ps = p->p_sigacts;
998 	register sig_t action;
999 	int mask, returnmask;
1000 
1001 #ifdef DIAGNOSTIC
1002 	if (sig == 0)
1003 		panic("psig");
1004 #endif
1005 	mask = sigmask(sig);
1006 	p->p_sig &= ~mask;
1007 	action = ps->ps_sigact[sig];
1008 #ifdef KTRACE
1009 	if (KTRPOINT(p, KTR_PSIG))
1010 		ktrpsig(p->p_tracep, sig, action, ps->ps_flags & SAS_OLDMASK ?
1011 		    ps->ps_oldmask : p->p_sigmask, 0);
1012 #endif
1013 	if (action == SIG_DFL) {
1014 		/*
1015 		 * Default action, where the default is to kill
1016 		 * the process.  (Other cases were ignored above.)
1017 		 */
1018 		sigexit(p, sig);
1019 		/* NOTREACHED */
1020 	} else {
1021 		/*
1022 		 * If we get here, the signal must be caught.
1023 		 */
1024 #ifdef DIAGNOSTIC
1025 		if (action == SIG_IGN || (p->p_sigmask & mask))
1026 			panic("psig action");
1027 #endif
1028 		/*
1029 		 * Set the new mask value and also defer further
1030 		 * occurences of this signal.
1031 		 *
1032 		 * Special case: user has done a sigpause.  Here the
1033 		 * current mask is not of interest, but rather the
1034 		 * mask from before the sigpause is what we want
1035 		 * restored after the signal processing is completed.
1036 		 */
1037 		(void) splhigh();
1038 		if (ps->ps_flags & SAS_OLDMASK) {
1039 			returnmask = ps->ps_oldmask;
1040 			ps->ps_flags &= ~SAS_OLDMASK;
1041 		} else
1042 			returnmask = p->p_sigmask;
1043 		p->p_sigmask |= ps->ps_catchmask[sig] | mask;
1044 		(void) spl0();
1045 		p->p_stats->p_ru.ru_nsignals++;
1046 		sendsig(action, sig, returnmask, 0);
1047 	}
1048 }
1049 
1050 /*
1051  * Kill the current process for stated reason.
1052  */
1053 killproc(p, why)
1054 	struct proc *p;
1055 	char *why;
1056 {
1057 
1058 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1059 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1060 	psignal(p, SIGKILL);
1061 }
1062 
1063 /*
1064  * Force the current process to exit with the specified
1065  * signal, dumping core if appropriate.  We bypass the normal
1066  * tests for masked and caught signals, allowing unrecoverable
1067  * failures to terminate the process without changing signal state.
1068  * Mark the accounting record with the signal termination.
1069  * If dumping core, save the signal number for the debugger.
1070  * Calls exit and does not return.
1071  */
1072 sigexit(p, sig)
1073 	register struct proc *p;
1074 	int sig;
1075 {
1076 
1077 	p->p_acflag |= AXSIG;
1078 	if (sigprop[sig] & SA_CORE) {
1079 		p->p_sigacts->ps_sig = sig;
1080 		if (coredump(p) == 0)
1081 			sig |= WCOREFLAG;
1082 	}
1083 	exit1(p, W_EXITCODE(0, sig));
1084 	/* NOTREACHED */
1085 }
1086 
1087 /*
1088  * Create a core dump.
1089  * The file name is "core.progname".
1090  * Core dumps are not created if the process is setuid.
1091  */
1092 coredump(p)
1093 	register struct proc *p;
1094 {
1095 	register struct vnode *vp;
1096 	register struct pcred *pcred = p->p_cred;
1097 	register struct ucred *cred = pcred->pc_ucred;
1098 	register struct vmspace *vm = p->p_vmspace;
1099 	struct vattr vattr;
1100 	int error, error1;
1101 	struct nameidata nd;
1102 	char name[MAXCOMLEN+6];	/* core.progname */
1103 
1104 	if (pcred->p_svuid != pcred->p_ruid ||
1105 	    pcred->p_svgid != pcred->p_rgid)
1106 		return (EFAULT);
1107 	if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >=
1108 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1109 		return (EFAULT);
1110 	sprintf(name, "core.%s", p->p_comm);
1111 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1112 	if (error = vn_open(&nd, O_CREAT|FWRITE, 0644))
1113 		return (error);
1114 	vp = nd.ni_vp;
1115 	if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred, p) ||
1116 	    vattr.va_nlink != 1) {
1117 		error = EFAULT;
1118 		goto out;
1119 	}
1120 	VATTR_NULL(&vattr);
1121 	vattr.va_size = 0;
1122 	LEASE_CHECK(vp, p, cred, LEASE_WRITE);
1123 	VOP_SETATTR(vp, &vattr, cred, p);
1124 	p->p_acflag |= ACORE;
1125 	bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1126 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1127 	error = cpu_coredump(p, vp, cred);
1128 	if (error == 0)
1129 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1130 		    (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
1131 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1132 	if (error == 0)
1133 		error = vn_rdwr(UIO_WRITE, vp,
1134 		    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1135 		    round_page(ctob(vm->vm_ssize)),
1136 		    (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE,
1137 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1138 out:
1139 	VOP_UNLOCK(vp);
1140 	error1 = vn_close(vp, FWRITE, cred, p);
1141 	if (error == 0)
1142 		error = error1;
1143 	return (error);
1144 }
1145 
1146 /*
1147  * Nonexistent system call-- signal process (may want to handle it).
1148  * Flag error in case process won't see signal immediately (blocked or ignored).
1149  */
1150 struct nosys_args {
1151 	int	dummy;
1152 };
1153 /* ARGSUSED */
1154 nosys(p, args, retval)
1155 	struct proc *p;
1156 	struct nosys_args *args;
1157 	int *retval;
1158 {
1159 
1160 	psignal(p, SIGSYS);
1161 	return (EINVAL);
1162 }
1163