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