xref: /original-bsd/sys/kern/kern_sig.c (revision 04d5f635)
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.35 (Berkeley) 06/28/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 "machine/cpu.h"
29 
30 #include "vm/vm.h"
31 #include "kinfo_proc.h"
32 #include "user.h"		/* for coredump */
33 
34 /*
35  * Can process p, with pcred pc, send the signal signo to process q?
36  */
37 #define CANSIGNAL(p, pc, q, signo) \
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 	    ((signo) == SIGCONT && (q)->p_session == (p)->p_session))
44 
45 /* ARGSUSED */
46 sigaction(p, uap, retval)
47 	struct proc *p;
48 	register struct args {
49 		int	signo;
50 		struct	sigaction *nsa;
51 		struct	sigaction *osa;
52 	} *uap;
53 	int *retval;
54 {
55 	struct sigaction vec;
56 	register struct sigaction *sa;
57 	register struct sigacts *ps = p->p_sigacts;
58 	register int sig;
59 	int bit, error;
60 
61 	sig = uap->signo;
62 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
63 		return (EINVAL);
64 	sa = &vec;
65 	if (uap->osa) {
66 		sa->sa_handler = ps->ps_sigact[sig];
67 		sa->sa_mask = ps->ps_catchmask[sig];
68 		bit = sigmask(sig);
69 		sa->sa_flags = 0;
70 		if ((ps->ps_sigonstack & bit) != 0)
71 			sa->sa_flags |= SA_ONSTACK;
72 		if ((ps->ps_sigintr & bit) == 0)
73 			sa->sa_flags |= SA_RESTART;
74 		if (p->p_flag & SNOCLDSTOP)
75 			sa->sa_flags |= SA_NOCLDSTOP;
76 		if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
77 		    sizeof (vec)))
78 			return (error);
79 	}
80 	if (uap->nsa) {
81 		if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
82 		    sizeof (vec)))
83 			return (error);
84 		setsigvec(p, sig, sa);
85 	}
86 	return (0);
87 }
88 
89 setsigvec(p, sig, sa)
90 	register struct proc *p;
91 	int sig;
92 	register struct sigaction *sa;
93 {
94 	register struct sigacts *ps = p->p_sigacts;
95 	register int bit;
96 
97 	bit = sigmask(sig);
98 	/*
99 	 * Change setting atomically.
100 	 */
101 	(void) splhigh();
102 	ps->ps_sigact[sig] = sa->sa_handler;
103 	ps->ps_catchmask[sig] = sa->sa_mask &~ sigcantmask;
104 	if ((sa->sa_flags & SA_RESTART) == 0)
105 		ps->ps_sigintr |= bit;
106 	else
107 		ps->ps_sigintr &= ~bit;
108 	if (sa->sa_flags & SA_ONSTACK)
109 		ps->ps_sigonstack |= bit;
110 	else
111 		ps->ps_sigonstack &= ~bit;
112 	if (sig == SIGCHLD) {
113 		if (sa->sa_flags & SA_NOCLDSTOP)
114 			p->p_flag |= SNOCLDSTOP;
115 		else
116 			p->p_flag &= ~SNOCLDSTOP;
117 	}
118 	/*
119 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
120 	 * and for signals set to SIG_DFL where the default is to ignore.
121 	 * However, don't put SIGCONT in p_sigignore,
122 	 * as we have to restart the process.
123 	 */
124 	if (sa->sa_handler == SIG_IGN ||
125 	    (sigprop[sig] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
126 		p->p_sig &= ~bit;		/* never to be seen again */
127 		if (sig != SIGCONT)
128 			p->p_sigignore |= bit;	/* easier in psignal */
129 		p->p_sigcatch &= ~bit;
130 	} else {
131 		p->p_sigignore &= ~bit;
132 		if (sa->sa_handler == SIG_DFL)
133 			p->p_sigcatch &= ~bit;
134 		else
135 			p->p_sigcatch |= bit;
136 	}
137 	(void) spl0();
138 }
139 
140 /*
141  * Initialize signal state for process 0;
142  * set to ignore signals that are ignored by default.
143  */
144 void
145 siginit(p)
146 	struct proc *p;
147 {
148 	register int i;
149 
150 	for (i = 0; i < NSIG; i++)
151 		if (sigprop[i] & SA_IGNORE && i != SIGCONT)
152 			p->p_sigignore |= sigmask(i);
153 }
154 
155 /*
156  * Reset signals for an exec of the specified process.
157  */
158 void
159 execsigs(p)
160 	register struct proc *p;
161 {
162 	register struct sigacts *ps = p->p_sigacts;
163 	register int nc, mask;
164 
165 	/*
166 	 * Reset caught signals.  Held signals remain held
167 	 * through p_sigmask (unless they were caught,
168 	 * and are now ignored by default).
169 	 */
170 	while (p->p_sigcatch) {
171 		nc = ffs((long)p->p_sigcatch);
172 		mask = sigmask(nc);
173 		p->p_sigcatch &= ~mask;
174 		if (sigprop[nc] & SA_IGNORE) {
175 			if (nc != SIGCONT)
176 				p->p_sigignore |= mask;
177 			p->p_sig &= ~mask;
178 		}
179 		ps->ps_sigact[nc] = SIG_DFL;
180 	}
181 	/*
182 	 * Reset stack state to the user stack.
183 	 * Clear set of signals caught on the signal stack.
184 	 */
185 	ps->ps_onstack = 0;
186 	ps->ps_sigsp = 0;
187 	ps->ps_sigonstack = 0;
188 }
189 
190 /*
191  * Manipulate signal mask.
192  * Note that we receive new mask, not pointer,
193  * and return old mask as return value;
194  * the library stub does the rest.
195  */
196 sigprocmask(p, uap, retval)
197 	register struct proc *p;
198 	struct args {
199 		int	how;
200 		sigset_t mask;
201 	} *uap;
202 	int *retval;
203 {
204 	int error = 0;
205 
206 	*retval = p->p_sigmask;
207 	(void) splhigh();
208 
209 	switch (uap->how) {
210 	case SIG_BLOCK:
211 		p->p_sigmask |= uap->mask &~ sigcantmask;
212 		break;
213 
214 	case SIG_UNBLOCK:
215 		p->p_sigmask &= ~uap->mask;
216 		break;
217 
218 	case SIG_SETMASK:
219 		p->p_sigmask = uap->mask &~ sigcantmask;
220 		break;
221 
222 	default:
223 		error = EINVAL;
224 		break;
225 	}
226 	(void) spl0();
227 	return (error);
228 }
229 
230 /* ARGSUSED */
231 sigpending(p, uap, retval)
232 	struct proc *p;
233 	void *uap;
234 	int *retval;
235 {
236 
237 	*retval = p->p_sig;
238 	return (0);
239 }
240 
241 #ifdef COMPAT_43
242 /*
243  * Generalized interface signal handler, 4.3-compatible.
244  */
245 /* ARGSUSED */
246 osigvec(p, uap, retval)
247 	struct proc *p;
248 	register struct args {
249 		int	signo;
250 		struct	sigvec *nsv;
251 		struct	sigvec *osv;
252 	} *uap;
253 	int *retval;
254 {
255 	struct sigvec vec;
256 	register struct sigacts *ps = p->p_sigacts;
257 	register struct sigvec *sv;
258 	register int sig;
259 	int bit, error;
260 
261 	sig = uap->signo;
262 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
263 		return (EINVAL);
264 	sv = &vec;
265 	if (uap->osv) {
266 		*(sig_t *)&sv->sv_handler = ps->ps_sigact[sig];
267 		sv->sv_mask = ps->ps_catchmask[sig];
268 		bit = sigmask(sig);
269 		sv->sv_flags = 0;
270 		if ((ps->ps_sigonstack & bit) != 0)
271 			sv->sv_flags |= SV_ONSTACK;
272 		if ((ps->ps_sigintr & bit) != 0)
273 			sv->sv_flags |= SV_INTERRUPT;
274 		if (p->p_flag & SNOCLDSTOP)
275 			sv->sv_flags |= SA_NOCLDSTOP;
276 		if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
277 		    sizeof (vec)))
278 			return (error);
279 	}
280 	if (uap->nsv) {
281 		if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
282 		    sizeof (vec)))
283 			return (error);
284 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
285 		setsigvec(p, sig, (struct sigaction *)sv);
286 	}
287 	return (0);
288 }
289 
290 osigblock(p, uap, retval)
291 	register struct proc *p;
292 	struct args {
293 		int	mask;
294 	} *uap;
295 	int *retval;
296 {
297 
298 	(void) splhigh();
299 	*retval = p->p_sigmask;
300 	p->p_sigmask |= uap->mask &~ sigcantmask;
301 	(void) spl0();
302 	return (0);
303 }
304 
305 osigsetmask(p, uap, retval)
306 	struct proc *p;
307 	struct args {
308 		int	mask;
309 	} *uap;
310 	int *retval;
311 {
312 
313 	(void) splhigh();
314 	*retval = p->p_sigmask;
315 	p->p_sigmask = uap->mask &~ sigcantmask;
316 	(void) spl0();
317 	return (0);
318 }
319 #endif
320 
321 /*
322  * Suspend process until signal, providing mask to be set
323  * in the meantime.  Note nonstandard calling convention:
324  * libc stub passes mask, not pointer, to save a copyin.
325  */
326 /* ARGSUSED */
327 sigsuspend(p, uap, retval)
328 	register struct proc *p;
329 	struct args {
330 		sigset_t mask;
331 	} *uap;
332 	int *retval;
333 {
334 	register struct sigacts *ps = p->p_sigacts;
335 
336 	/*
337 	 * When returning from sigpause, we want
338 	 * the old mask to be restored after the
339 	 * signal handler has finished.  Thus, we
340 	 * save it here and mark the proc structure
341 	 * to indicate this (should be in sigacts).
342 	 */
343 	ps->ps_oldmask = p->p_sigmask;
344 	ps->ps_flags |= SA_OLDMASK;
345 	p->p_sigmask = uap->mask &~ sigcantmask;
346 	(void) tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0);
347 	/* always return EINTR rather than ERESTART... */
348 	return (EINTR);
349 }
350 
351 /* ARGSUSED */
352 sigstack(p, uap, retval)
353 	struct proc *p;
354 	register struct args {
355 		struct	sigstack *nss;
356 		struct	sigstack *oss;
357 	} *uap;
358 	int *retval;
359 {
360 	struct sigstack ss;
361 	int error = 0;
362 
363 	if (uap->oss && (error = copyout((caddr_t)&p->p_sigacts->ps_sigstack,
364 	    (caddr_t)uap->oss, sizeof (struct sigstack))))
365 		return (error);
366 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
367 	    sizeof (ss))) == 0)
368 		p->p_sigacts->ps_sigstack = ss;
369 	return (error);
370 }
371 
372 /* ARGSUSED */
373 kill(cp, uap, retval)
374 	register struct proc *cp;
375 	register struct args {
376 		int	pid;
377 		int	signo;
378 	} *uap;
379 	int *retval;
380 {
381 	register struct proc *p;
382 	register struct pcred *pc = cp->p_cred;
383 
384 	if ((unsigned) uap->signo >= NSIG)
385 		return (EINVAL);
386 	if (uap->pid > 0) {
387 		/* kill single process */
388 		p = pfind(uap->pid);
389 		if (p == 0)
390 			return (ESRCH);
391 		if (!CANSIGNAL(cp, pc, p, uap->signo))
392 			return (EPERM);
393 		if (uap->signo)
394 			psignal(p, uap->signo);
395 		return (0);
396 	}
397 	switch (uap->pid) {
398 	case -1:		/* broadcast signal */
399 		return (killpg1(cp, uap->signo, 0, 1));
400 	case 0:			/* signal own process group */
401 		return (killpg1(cp, uap->signo, 0, 0));
402 	default:		/* negative explicit process group */
403 		return (killpg1(cp, uap->signo, -uap->pid, 0));
404 	}
405 	/* NOTREACHED */
406 }
407 
408 #ifdef COMPAT_43
409 /* ARGSUSED */
410 okillpg(p, uap, retval)
411 	struct proc *p;
412 	register struct args {
413 		int	pgid;
414 		int	signo;
415 	} *uap;
416 	int *retval;
417 {
418 
419 	if ((unsigned) uap->signo >= NSIG)
420 		return (EINVAL);
421 	return (killpg1(p, uap->signo, uap->pgid, 0));
422 }
423 #endif
424 
425 /*
426  * Common code for kill process group/broadcast kill.
427  * cp is calling process.
428  */
429 killpg1(cp, signo, pgid, all)
430 	register struct proc *cp;
431 	int signo, pgid, all;
432 {
433 	register struct proc *p;
434 	register struct pcred *pc = cp->p_cred;
435 	struct pgrp *pgrp;
436 	int nfound = 0;
437 
438 	if (all)
439 		/*
440 		 * broadcast
441 		 */
442 		for (p = allproc; p != NULL; p = p->p_nxt) {
443 			if (p->p_pid <= 1 || p->p_flag&SSYS ||
444 			    p == cp || !CANSIGNAL(cp, pc, p, signo))
445 				continue;
446 			nfound++;
447 			if (signo)
448 				psignal(p, signo);
449 		}
450 	else {
451 		if (pgid == 0)
452 			/*
453 			 * zero pgid means send to my process group.
454 			 */
455 			pgrp = cp->p_pgrp;
456 		else {
457 			pgrp = pgfind(pgid);
458 			if (pgrp == NULL)
459 				return (ESRCH);
460 		}
461 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
462 			if (p->p_pid <= 1 || p->p_flag&SSYS ||
463 			    p->p_stat == SZOMB || !CANSIGNAL(cp, pc, p, signo))
464 				continue;
465 			nfound++;
466 			if (signo)
467 				psignal(p, signo);
468 		}
469 	}
470 	return (nfound ? 0 : ESRCH);
471 }
472 
473 /*
474  * Send the specified signal to
475  * all processes with 'pgid' as
476  * process group.
477  */
478 void
479 gsignal(pgid, sig)
480 	int pgid, sig;
481 {
482 	struct pgrp *pgrp;
483 
484 	if (pgid && (pgrp = pgfind(pgid)))
485 		pgsignal(pgrp, sig, 0);
486 }
487 
488 /*
489  * Send sig to every member of a process group.
490  * If checktty is 1, limit to members which have a controlling
491  * terminal.
492  */
493 void
494 pgsignal(pgrp, sig, checkctty)
495 	struct pgrp *pgrp;
496 	int sig, checkctty;
497 {
498 	register struct proc *p;
499 
500 	if (pgrp)
501 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
502 			if (checkctty == 0 || p->p_flag&SCTTY)
503 				psignal(p, sig);
504 }
505 
506 /*
507  * Send a signal caused by a trap to the current process.
508  * If it will be caught immediately, deliver it with correct code.
509  * Otherwise, post it normally.
510  */
511 void
512 trapsignal(p, sig, code)
513 	struct proc *p;
514 	register int sig;
515 	unsigned code;
516 {
517 	register struct sigacts *ps = p->p_sigacts;
518 	int mask;
519 
520 	mask = sigmask(sig);
521 	if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 &&
522 	    (p->p_sigmask & mask) == 0) {
523 		p->p_stats->p_ru.ru_nsignals++;
524 #ifdef KTRACE
525 		if (KTRPOINT(p, KTR_PSIG))
526 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[sig],
527 				p->p_sigmask, code);
528 #endif
529 		sendsig(ps->ps_sigact[sig], sig, p->p_sigmask, code);
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)
718 			signotify(p);
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 ps_sigact
786 			 * are consistent.
787 			 */
788 			if ((p->p_flag&STRC) == 0)
789 				continue;
790 
791 			/*
792 			 * If parent wants us to take the signal,
793 			 * then it will leave it in p->p_xstat;
794 			 * otherwise we just look for signals again.
795 			 */
796 			p->p_sig &= ~mask;	/* clear the old signal */
797 			sig = p->p_xstat;
798 			if (sig == 0)
799 				continue;
800 
801 			/*
802 			 * Put the new signal into p_sig.
803 			 * If signal is being masked,
804 			 * look for other signals.
805 			 */
806 			mask = sigmask(sig);
807 			p->p_sig |= mask;
808 			if (p->p_sigmask & mask)
809 				continue;
810 		}
811 
812 		/*
813 		 * Decide whether the signal should be returned.
814 		 * Return the signal's number, or fall through
815 		 * to clear it from the pending mask.
816 		 */
817 		switch ((int)p->p_sigacts->ps_sigact[sig]) {
818 
819 		case SIG_DFL:
820 			/*
821 			 * Don't take default actions on system processes.
822 			 */
823 			if (p->p_pid <= 1)
824 				break;		/* == ignore */
825 			/*
826 			 * If there is a pending stop signal to process
827 			 * with default action, stop here,
828 			 * then clear the signal.  However,
829 			 * if process is member of an orphaned
830 			 * process group, ignore tty stop signals.
831 			 */
832 			if (prop & SA_STOP) {
833 				if (p->p_flag&STRC ||
834 		    		    (p->p_pgrp->pg_jobc == 0 &&
835 				    prop & SA_TTYSTOP))
836 					break;	/* == ignore */
837 				p->p_xstat = sig;
838 				stop(p);
839 				if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
840 					psignal(p->p_pptr, SIGCHLD);
841 				swtch();
842 				break;
843 			} else if (prop & SA_IGNORE) {
844 				/*
845 				 * Except for SIGCONT, shouldn't get here.
846 				 * Default action is to ignore; drop it.
847 				 */
848 				break;		/* == ignore */
849 			} else
850 				return (sig);
851 			/*NOTREACHED*/
852 
853 		case SIG_IGN:
854 			/*
855 			 * Masking above should prevent us ever trying
856 			 * to take action on an ignored signal other
857 			 * than SIGCONT, unless process is traced.
858 			 */
859 			if ((prop & SA_CONT) == 0 && (p->p_flag&STRC) == 0)
860 				printf("issig\n");
861 			break;		/* == ignore */
862 
863 		default:
864 			/*
865 			 * This signal has an action, let
866 			 * psig process it.
867 			 */
868 			return (sig);
869 		}
870 		p->p_sig &= ~mask;		/* take the signal! */
871 	}
872 	/* NOTREACHED */
873 }
874 
875 /*
876  * Put the argument process into the stopped
877  * state and notify the parent via wakeup.
878  * Signals are handled elsewhere.
879  * The process must not be on the run queue.
880  */
881 stop(p)
882 	register struct proc *p;
883 {
884 
885 	p->p_stat = SSTOP;
886 	p->p_flag &= ~SWTED;
887 	wakeup((caddr_t)p->p_pptr);
888 }
889 
890 /*
891  * Take the action for the specified signal
892  * from the current set of pending signals.
893  */
894 void
895 psig(sig)
896 	register int sig;
897 {
898 	register struct proc *p = curproc;
899 	register struct sigacts *ps = p->p_sigacts;
900 	register sig_t action;
901 	int mask, returnmask;
902 
903 #ifdef DIAGNOSTIC
904 	if (sig == 0)
905 		panic("psig");
906 #endif
907 	mask = sigmask(sig);
908 	p->p_sig &= ~mask;
909 	action = ps->ps_sigact[sig];
910 #ifdef KTRACE
911 	if (KTRPOINT(p, KTR_PSIG))
912 		ktrpsig(p->p_tracep, sig, action, ps->ps_flags & SA_OLDMASK ?
913 		    ps->ps_oldmask : p->p_sigmask, 0);
914 #endif
915 	if (action == SIG_DFL) {
916 		/*
917 		 * Default action, where the default is to kill
918 		 * the process.  (Other cases were ignored above.)
919 		 */
920 		sigexit(p, sig);
921 		/* NOTREACHED */
922 	} else {
923 		/*
924 		 * If we get here, the signal must be caught.
925 		 */
926 #ifdef DIAGNOSTIC
927 		if (action == SIG_IGN || (p->p_sigmask & mask))
928 			panic("psig action");
929 #endif
930 		/*
931 		 * Set the new mask value and also defer further
932 		 * occurences of this signal.
933 		 *
934 		 * Special case: user has done a sigpause.  Here the
935 		 * current mask is not of interest, but rather the
936 		 * mask from before the sigpause is what we want
937 		 * restored after the signal processing is completed.
938 		 */
939 		(void) splhigh();
940 		if (ps->ps_flags & SA_OLDMASK) {
941 			returnmask = ps->ps_oldmask;
942 			ps->ps_flags &= ~SA_OLDMASK;
943 		} else
944 			returnmask = p->p_sigmask;
945 		p->p_sigmask |= ps->ps_catchmask[sig] | mask;
946 		(void) spl0();
947 		p->p_stats->p_ru.ru_nsignals++;
948 		sendsig(action, sig, returnmask, 0);
949 	}
950 }
951 
952 /*
953  * Force the current process to exit with the specified
954  * signal, dumping core if appropriate.  We bypass the normal
955  * tests for masked and caught signals, allowing unrecoverable
956  * failures to terminate the process without changing signal state.
957  * Mark the accounting record with the signal termination.
958  * If dumping core, save the signal number for the debugger.
959  * Calls exit and does not return.
960  */
961 sigexit(p, sig)
962 	register struct proc *p;
963 	int sig;
964 {
965 
966 	p->p_acflag |= AXSIG;
967 	if (sigprop[sig] & SA_CORE) {
968 		p->p_sigacts->ps_sig = sig;
969 		if (coredump(p) == 0)
970 			sig |= WCOREFLAG;
971 	}
972 	exit(p, W_EXITCODE(0, sig));
973 	/* NOTREACHED */
974 }
975 
976 /*
977  * Create a core dump.
978  * The file name is "core.progname".
979  * Core dumps are not created if the process is setuid.
980  */
981 coredump(p)
982 	register struct proc *p;
983 {
984 	register struct vnode *vp;
985 	register struct pcred *pcred = p->p_cred;
986 	register struct ucred *cred = pcred->pc_ucred;
987 	register struct vmspace *vm = p->p_vmspace;
988 	struct vattr vattr;
989 	int error, error1;
990 	struct nameidata nd;
991 	char name[MAXCOMLEN+6];	/* core.progname */
992 
993 	if (pcred->p_svuid != pcred->p_ruid ||
994 	    pcred->p_svgid != pcred->p_rgid)
995 		return (EFAULT);
996 	if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >=
997 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
998 		return (EFAULT);
999 	sprintf(name, "core.%s", p->p_comm);
1000 	nd.ni_dirp = name;
1001 	nd.ni_segflg = UIO_SYSSPACE;
1002 	if (error = vn_open(&nd, p, O_CREAT|FWRITE, 0644))
1003 		return (error);
1004 	vp = nd.ni_vp;
1005 	if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred, p) ||
1006 	    vattr.va_nlink != 1) {
1007 		error = EFAULT;
1008 		goto out;
1009 	}
1010 	VATTR_NULL(&vattr);
1011 	vattr.va_size = 0;
1012 	VOP_SETATTR(vp, &vattr, cred, p);
1013 	p->p_acflag |= ACORE;
1014 	bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1015 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1016 #ifdef HPUXCOMPAT
1017 	/*
1018 	 * BLETCH!  If we loaded from an HPUX format binary file
1019 	 * we have to dump an HPUX style user struct so that the
1020 	 * HPUX debuggers can grok it.
1021 	 */
1022 	if (p->p_addr->u_pcb.pcb_flags & PCB_HPUXBIN)
1023 		error = hpuxdumpu(vp, cred);
1024 	else
1025 #endif
1026 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
1027 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) NULL,
1028 	    p);
1029 	if (error == 0)
1030 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1031 		    (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
1032 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1033 	if (error == 0)
1034 		error = vn_rdwr(UIO_WRITE, vp,
1035 		    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1036 		    round_page(ctob(vm->vm_ssize)),
1037 		    (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE,
1038 		    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1039 out:
1040 	VOP_UNLOCK(vp);
1041 	error1 = vn_close(vp, FWRITE, cred, p);
1042 	if (error == 0)
1043 		error = error1;
1044 	return (error);
1045 }
1046 
1047 /*
1048  * Nonexistent system call-- signal process (may want to handle it).
1049  * Flag error in case process won't see signal immediately (blocked or ignored).
1050  */
1051 /* ARGSUSED */
1052 nosys(p, args, retval)
1053 	struct proc *p;
1054 	void *args;
1055 	int *retval;
1056 {
1057 
1058 	psignal(p, SIGSYS);
1059 	return (EINVAL);
1060 }
1061