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