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