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