xref: /original-bsd/sys/kern/kern_sig.c (revision f66b11a8)
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.54 (Berkeley) 02/25/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 && (sig == SIGKILL ||
667 	    sig == SIGTERM && (p->p_flag&STRC || action != SIG_DFL)))
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 		 * When a sleeping process receives a stop
714 		 * signal, process immediately if possible.
715 		 * All other (caught or default) signals
716 		 * cause the process to run.
717 		 */
718 		if (prop & SA_STOP) {
719 			if (action != SIG_DFL)
720 				goto runfast;
721 			/*
722 			 * If a child holding parent blocked,
723 			 * stopping could cause deadlock.
724 			 */
725 			if (p->p_flag&SPPWAIT)
726 				goto out;
727 			p->p_sig &= ~mask;
728 			p->p_xstat = sig;
729 			if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
730 				psignal(p->p_pptr, SIGCHLD);
731 			stop(p);
732 			goto out;
733 		} else
734 			goto runfast;
735 		/*NOTREACHED*/
736 
737 	case SSTOP:
738 		/*
739 		 * If traced process is already stopped,
740 		 * then no further action is necessary.
741 		 */
742 		if (p->p_flag&STRC)
743 			goto out;
744 
745 		/*
746 		 * Kill signal always sets processes running.
747 		 */
748 		if (sig == SIGKILL)
749 			goto runfast;
750 
751 		if (prop & SA_CONT) {
752 			/*
753 			 * If SIGCONT is default (or ignored), we continue
754 			 * the process but don't leave the signal in p_sig,
755 			 * as it has no further action.  If SIGCONT is held,
756 			 * continue the process and leave the signal in p_sig.
757 			 * If the process catches SIGCONT, let it handle
758 			 * the signal itself.  If it isn't waiting on
759 			 * an event, then it goes back to run state.
760 			 * Otherwise, process goes back to sleep state.
761 			 */
762 			if (action == SIG_DFL)
763 				p->p_sig &= ~mask;
764 			if (action == SIG_CATCH)
765 				goto runfast;
766 			if (p->p_wchan == 0)
767 				goto run;
768 			p->p_stat = SSLEEP;
769 			goto out;
770 		}
771 
772 		if (prop & SA_STOP) {
773 			/*
774 			 * Already stopped, don't need to stop again.
775 			 * (If we did the shell could get confused.)
776 			 */
777 			p->p_sig &= ~mask;		/* take it away */
778 			goto out;
779 		}
780 
781 		/*
782 		 * If process is sleeping interruptibly, then
783 		 * simulate a wakeup so that when it is continued,
784 		 * it will be made runnable and can look at the signal.
785 		 * But don't setrun the process, leave it stopped.
786 		 */
787 		if (p->p_wchan && p->p_flag & SSINTR)
788 			unsleep(p);
789 		goto out;
790 
791 	default:
792 		/*
793 		 * SRUN, SIDL, SZOMB do nothing with the signal,
794 		 * other than kicking ourselves if we are running.
795 		 * It will either never be noticed, or noticed very soon.
796 		 */
797 		if (p == curproc)
798 			signotify(p);
799 		goto out;
800 	}
801 	/*NOTREACHED*/
802 
803 runfast:
804 	/*
805 	 * Raise priority to at least PUSER.
806 	 */
807 	if (p->p_pri > PUSER)
808 		p->p_pri = PUSER;
809 run:
810 	setrun(p);
811 out:
812 	splx(s);
813 }
814 
815 /*
816  * If the current process has a signal to process (should be caught
817  * or cause termination, should interrupt current syscall),
818  * return the signal number.  Stop signals with default action
819  * are processed immediately, then cleared; they aren't returned.
820  * This is checked after each entry to the system for a syscall
821  * or trap (though this can usually be done without actually calling
822  * issig by checking the pending signal masks in the CURSIG macro.)
823  * The normal call sequence is
824  *
825  *	while (sig = CURSIG(curproc))
826  *		psig(sig);
827  */
828 issig(p)
829 	register struct proc *p;
830 {
831 	register int sig, mask, prop;
832 
833 	for (;;) {
834 		mask = p->p_sig &~ p->p_sigmask;
835 		if (p->p_flag&SPPWAIT)
836 			mask &= ~stopsigmask;
837 		if (mask == 0)	 	/* no signal to send */
838 			return (0);
839 		sig = ffs((long)mask);
840 		mask = sigmask(sig);
841 		prop = sigprop[sig];
842 		/*
843 		 * We should see pending but ignored signals
844 		 * only if STRC was on when they were posted.
845 		 */
846 		if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
847 			p->p_sig &= ~mask;
848 			continue;
849 		}
850 		if (p->p_flag&STRC && (p->p_flag&SPPWAIT) == 0) {
851 			/*
852 			 * If traced, always stop, and stay
853 			 * stopped until released by the parent.
854 			 */
855 			p->p_xstat = sig;
856 			psignal(p->p_pptr, SIGCHLD);
857 			do {
858 				stop(p);
859 				swtch();
860 			} while (!procxmt(p) && p->p_flag&STRC);
861 
862 			/*
863 			 * If the traced bit got turned off,
864 			 * go back up to the top to rescan signals.
865 			 * This ensures that p_sig* and ps_sigact
866 			 * are consistent.
867 			 */
868 			if ((p->p_flag&STRC) == 0)
869 				continue;
870 
871 			/*
872 			 * If parent wants us to take the signal,
873 			 * then it will leave it in p->p_xstat;
874 			 * otherwise we just look for signals again.
875 			 */
876 			p->p_sig &= ~mask;	/* clear the old signal */
877 			sig = p->p_xstat;
878 			if (sig == 0)
879 				continue;
880 
881 			/*
882 			 * Put the new signal into p_sig.
883 			 * If signal is being masked,
884 			 * look for other signals.
885 			 */
886 			mask = sigmask(sig);
887 			p->p_sig |= mask;
888 			if (p->p_sigmask & mask)
889 				continue;
890 		}
891 
892 		/*
893 		 * Decide whether the signal should be returned.
894 		 * Return the signal's number, or fall through
895 		 * to clear it from the pending mask.
896 		 */
897 		switch ((int)p->p_sigacts->ps_sigact[sig]) {
898 
899 		case SIG_DFL:
900 			/*
901 			 * Don't take default actions on system processes.
902 			 */
903 			if (p->p_pid <= 1) {
904 #ifdef DIAGNOSTIC
905 				/*
906 				 * Are you sure you want to ignore SIGSEGV
907 				 * in init? XXX
908 				 */
909 				printf("Process (pid %d) got signal %d\n",
910 					p->p_pid, sig);
911 #endif
912 				break;		/* == ignore */
913 			}
914 			/*
915 			 * If there is a pending stop signal to process
916 			 * with default action, stop here,
917 			 * then clear the signal.  However,
918 			 * if process is member of an orphaned
919 			 * process group, ignore tty stop signals.
920 			 */
921 			if (prop & SA_STOP) {
922 				if (p->p_flag&STRC ||
923 		    		    (p->p_pgrp->pg_jobc == 0 &&
924 				    prop & SA_TTYSTOP))
925 					break;	/* == ignore */
926 				p->p_xstat = sig;
927 				stop(p);
928 				if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
929 					psignal(p->p_pptr, SIGCHLD);
930 				swtch();
931 				break;
932 			} else if (prop & SA_IGNORE) {
933 				/*
934 				 * Except for SIGCONT, shouldn't get here.
935 				 * Default action is to ignore; drop it.
936 				 */
937 				break;		/* == ignore */
938 			} else
939 				return (sig);
940 			/*NOTREACHED*/
941 
942 		case SIG_IGN:
943 			/*
944 			 * Masking above should prevent us ever trying
945 			 * to take action on an ignored signal other
946 			 * than SIGCONT, unless process is traced.
947 			 */
948 			if ((prop & SA_CONT) == 0 && (p->p_flag&STRC) == 0)
949 				printf("issig\n");
950 			break;		/* == ignore */
951 
952 		default:
953 			/*
954 			 * This signal has an action, let
955 			 * psig process it.
956 			 */
957 			return (sig);
958 		}
959 		p->p_sig &= ~mask;		/* take the signal! */
960 	}
961 	/* NOTREACHED */
962 }
963 
964 /*
965  * Put the argument process into the stopped
966  * state and notify the parent via wakeup.
967  * Signals are handled elsewhere.
968  * The process must not be on the run queue.
969  */
970 stop(p)
971 	register struct proc *p;
972 {
973 
974 	p->p_stat = SSTOP;
975 	p->p_flag &= ~SWTED;
976 	wakeup((caddr_t)p->p_pptr);
977 }
978 
979 /*
980  * Take the action for the specified signal
981  * from the current set of pending signals.
982  */
983 void
984 psig(sig)
985 	register int sig;
986 {
987 	register struct proc *p = curproc;
988 	register struct sigacts *ps = p->p_sigacts;
989 	register sig_t action;
990 	int mask, returnmask;
991 
992 #ifdef DIAGNOSTIC
993 	if (sig == 0)
994 		panic("psig");
995 #endif
996 	mask = sigmask(sig);
997 	p->p_sig &= ~mask;
998 	action = ps->ps_sigact[sig];
999 #ifdef KTRACE
1000 	if (KTRPOINT(p, KTR_PSIG))
1001 		ktrpsig(p->p_tracep, sig, action, ps->ps_flags & SAS_OLDMASK ?
1002 		    ps->ps_oldmask : p->p_sigmask, 0);
1003 #endif
1004 	if (action == SIG_DFL) {
1005 		/*
1006 		 * Default action, where the default is to kill
1007 		 * the process.  (Other cases were ignored above.)
1008 		 */
1009 		sigexit(p, sig);
1010 		/* NOTREACHED */
1011 	} else {
1012 		/*
1013 		 * If we get here, the signal must be caught.
1014 		 */
1015 #ifdef DIAGNOSTIC
1016 		if (action == SIG_IGN || (p->p_sigmask & mask))
1017 			panic("psig action");
1018 #endif
1019 		/*
1020 		 * Set the new mask value and also defer further
1021 		 * occurences of this signal.
1022 		 *
1023 		 * Special case: user has done a sigpause.  Here the
1024 		 * current mask is not of interest, but rather the
1025 		 * mask from before the sigpause is what we want
1026 		 * restored after the signal processing is completed.
1027 		 */
1028 		(void) splhigh();
1029 		if (ps->ps_flags & SAS_OLDMASK) {
1030 			returnmask = ps->ps_oldmask;
1031 			ps->ps_flags &= ~SAS_OLDMASK;
1032 		} else
1033 			returnmask = p->p_sigmask;
1034 		p->p_sigmask |= ps->ps_catchmask[sig] | mask;
1035 		(void) spl0();
1036 		p->p_stats->p_ru.ru_nsignals++;
1037 		sendsig(action, sig, returnmask, 0);
1038 	}
1039 }
1040 
1041 /*
1042  * Kill the current process for stated reason.
1043  */
1044 killproc(p, why)
1045 	struct proc *p;
1046 	char *why;
1047 {
1048 
1049 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1050 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1051 	psignal(p, SIGKILL);
1052 }
1053 
1054 /*
1055  * Force the current process to exit with the specified
1056  * signal, dumping core if appropriate.  We bypass the normal
1057  * tests for masked and caught signals, allowing unrecoverable
1058  * failures to terminate the process without changing signal state.
1059  * Mark the accounting record with the signal termination.
1060  * If dumping core, save the signal number for the debugger.
1061  * Calls exit and does not return.
1062  */
1063 sigexit(p, sig)
1064 	register struct proc *p;
1065 	int sig;
1066 {
1067 
1068 	p->p_acflag |= AXSIG;
1069 	if (sigprop[sig] & SA_CORE) {
1070 		p->p_sigacts->ps_sig = sig;
1071 		if (coredump(p) == 0)
1072 			sig |= WCOREFLAG;
1073 	}
1074 	exit1(p, W_EXITCODE(0, sig));
1075 	/* NOTREACHED */
1076 }
1077 
1078 /*
1079  * Create a core dump.
1080  * The file name is "core.progname".
1081  * Core dumps are not created if the process is setuid.
1082  */
1083 coredump(p)
1084 	register struct proc *p;
1085 {
1086 	register struct vnode *vp;
1087 	register struct pcred *pcred = p->p_cred;
1088 	register struct ucred *cred = pcred->pc_ucred;
1089 	register struct vmspace *vm = p->p_vmspace;
1090 	struct vattr vattr;
1091 	int error, error1;
1092 	struct nameidata nd;
1093 	char name[MAXCOMLEN+6];	/* core.progname */
1094 
1095 	if (pcred->p_svuid != pcred->p_ruid ||
1096 	    pcred->p_svgid != pcred->p_rgid)
1097 		return (EFAULT);
1098 	if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >=
1099 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1100 		return (EFAULT);
1101 	sprintf(name, "core.%s", p->p_comm);
1102 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1103 	if (error = vn_open(&nd, O_CREAT|FWRITE, 0644))
1104 		return (error);
1105 	vp = nd.ni_vp;
1106 	if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred, p) ||
1107 	    vattr.va_nlink != 1) {
1108 		error = EFAULT;
1109 		goto out;
1110 	}
1111 	VATTR_NULL(&vattr);
1112 	vattr.va_size = 0;
1113 	LEASE_CHECK(vp, p, cred, LEASE_WRITE);
1114 	VOP_SETATTR(vp, &vattr, cred, p);
1115 	p->p_acflag |= ACORE;
1116 	bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1117 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1118 	error = cpu_coredump(p, vp, cred);
1119 	if (error == 0)
1120 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1121 		    (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
1122 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1123 	if (error == 0)
1124 		error = vn_rdwr(UIO_WRITE, vp,
1125 		    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1126 		    round_page(ctob(vm->vm_ssize)),
1127 		    (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE,
1128 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1129 out:
1130 	VOP_UNLOCK(vp);
1131 	error1 = vn_close(vp, FWRITE, cred, p);
1132 	if (error == 0)
1133 		error = error1;
1134 	return (error);
1135 }
1136 
1137 /*
1138  * Nonexistent system call-- signal process (may want to handle it).
1139  * Flag error in case process won't see signal immediately (blocked or ignored).
1140  */
1141 struct nosys_args {
1142 	int	dummy;
1143 };
1144 /* ARGSUSED */
1145 nosys(p, args, retval)
1146 	struct proc *p;
1147 	struct nosys_args *args;
1148 	int *retval;
1149 {
1150 
1151 	psignal(p, SIGSYS);
1152 	return (EINVAL);
1153 }
1154