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