xref: /original-bsd/sys/kern/kern_sig.c (revision 907a1f7d)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)kern_sig.c	7.20 (Berkeley) 06/21/90
18  */
19 
20 #include "param.h"
21 #include "systm.h"
22 #include "syscontext.h"	/* XXX */
23 #include "vnode.h"
24 #include "proc.h"
25 #include "timeb.h"
26 #include "times.h"
27 #include "buf.h"
28 #include "text.h"
29 #include "seg.h"
30 #include "vm.h"
31 #include "acct.h"
32 #include "uio.h"
33 #include "file.h"
34 #include "kernel.h"
35 #include "wait.h"
36 #include "ktrace.h"
37 
38 #include "machine/reg.h"
39 #include "machine/pte.h"
40 #include "machine/psl.h"
41 #include "machine/mtpr.h"
42 
43 #define	ttystopsigmask	(sigmask(SIGTSTP)|sigmask(SIGTTIN)|sigmask(SIGTTOU))
44 #define	stopsigmask	(sigmask(SIGSTOP)|ttystopsigmask)
45 #define defaultignmask	(sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \
46 			sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO))
47 
48 /*
49  * Can process p send the signal signo to process q?
50  */
51 #define CANSIGNAL(p, q, signo) \
52 	((p)->p_uid == 0 || \
53 	    (p)->p_ruid == (q)->p_ruid || (p)->p_uid == (q)->p_ruid || \
54 	    (p)->p_ruid == (q)->p_uid || (p)->p_uid == (q)->p_uid || \
55 	    ((signo) == SIGCONT && (q)->p_session == (p)->p_session))
56 
57 /* ARGSUSED */
58 sigaction(p, uap, retval)
59 	struct proc *p;
60 	register struct args {
61 		int	signo;
62 		struct	sigaction *nsa;
63 		struct	sigaction *osa;
64 	} *uap;
65 	int *retval;
66 {
67 	struct sigaction vec;
68 	register struct sigaction *sa;
69 	register int sig;
70 	int bit, error;
71 
72 	sig = uap->signo;
73 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
74 		RETURN (EINVAL);
75 	sa = &vec;
76 	if (uap->osa) {
77 		sa->sa_handler = u.u_signal[sig];
78 		sa->sa_mask = u.u_sigmask[sig];
79 		bit = sigmask(sig);
80 		sa->sa_flags = 0;
81 		if ((u.u_sigonstack & bit) != 0)
82 			sa->sa_flags |= SA_ONSTACK;
83 		if ((u.u_sigintr & bit) == 0)
84 			sa->sa_flags |= SA_RESTART;
85 		if (p->p_flag & SNOCLDSTOP)
86 			sa->sa_flags |= SA_NOCLDSTOP;
87 		if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
88 		    sizeof (vec)))
89 			RETURN (error);
90 	}
91 	if (uap->nsa) {
92 		if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
93 		    sizeof (vec)))
94 			RETURN (error);
95 		setsigvec(p, sig, sa);
96 	}
97 	RETURN (0);
98 }
99 
100 setsigvec(p, sig, sa)
101 	register struct proc *p;
102 	int sig;
103 	register struct sigaction *sa;
104 {
105 	register int bit;
106 
107 	bit = sigmask(sig);
108 	/*
109 	 * Change setting atomically.
110 	 */
111 	(void) splhigh();
112 	u.u_signal[sig] = sa->sa_handler;
113 	u.u_sigmask[sig] = sa->sa_mask &~ sigcantmask;
114 	if ((sa->sa_flags & SA_RESTART) == 0)
115 		u.u_sigintr |= bit;
116 	else
117 		u.u_sigintr &= ~bit;
118 	if (sa->sa_flags & SA_ONSTACK)
119 		u.u_sigonstack |= bit;
120 	else
121 		u.u_sigonstack &= ~bit;
122 	if (sig == SIGCHLD) {
123 		if (sa->sa_flags & SA_NOCLDSTOP)
124 			p->p_flag |= SNOCLDSTOP;
125 		else
126 			p->p_flag &= ~SNOCLDSTOP;
127 	}
128 	/*
129 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
130 	 * and for signals set to SIG_DFL where the default is to ignore.
131 	 * However, don't put SIGCONT in p_sigignore,
132 	 * as we have to restart the process.
133 	 */
134 	if (sa->sa_handler == SIG_IGN ||
135 	   (bit & defaultignmask && sa->sa_handler == SIG_DFL)) {
136 		p->p_sig &= ~bit;		/* never to be seen again */
137 		if (sig != SIGCONT)
138 			p->p_sigignore |= bit;	/* easier in psignal */
139 		p->p_sigcatch &= ~bit;
140 	} else {
141 		p->p_sigignore &= ~bit;
142 		if (sa->sa_handler == SIG_DFL)
143 			p->p_sigcatch &= ~bit;
144 		else
145 			p->p_sigcatch |= bit;
146 	}
147 	(void) spl0();
148 }
149 
150 /*
151  * Initialize signal state for process 0;
152  * set to ignore signals that are ignored by default.
153  */
154 siginit(p)
155 	struct proc *p;
156 {
157 
158 	p->p_sigignore = defaultignmask &~ sigmask(SIGCONT);
159 }
160 
161 /*
162  * Reset signals for an exec of the specified process.
163  */
164 execsigs(p)
165 	register struct proc *p;
166 {
167 	register int nc, mask;
168 
169 	/*
170 	 * Reset caught signals.  Held signals remain held
171 	 * through p_sigmask (unless they were caught,
172 	 * and are now ignored by default).
173 	 */
174 	while (p->p_sigcatch) {
175 		nc = ffs((long)p->p_sigcatch);
176 		mask = sigmask(nc);
177 		p->p_sigcatch &= ~mask;
178 		if (mask & defaultignmask) {
179 			if (nc != SIGCONT)
180 				p->p_sigignore |= mask;
181 			p->p_sig &= ~mask;
182 		}
183 		u.u_signal[nc] = SIG_DFL;
184 	}
185 	/*
186 	 * Reset stack state to the user stack.
187 	 * Clear set of signals caught on the signal stack.
188 	 */
189 	u.u_onstack = 0;
190 	u.u_sigsp = 0;
191 	u.u_sigonstack = 0;
192 }
193 
194 /*
195  * Manipulate signal mask.
196  * Note that we receive new mask, not pointer,
197  * and return old mask as return value;
198  * the library stub does the rest.
199  */
200 sigprocmask(p, uap, retval)
201 	register struct proc *p;
202 	struct args {
203 		int	how;
204 		sigset_t mask;
205 	} *uap;
206 	int *retval;
207 {
208 	int error = 0;
209 
210 	*retval = p->p_sigmask;
211 	(void) splhigh();
212 
213 	switch (uap->how) {
214 	case SIG_BLOCK:
215 		p->p_sigmask |= uap->mask &~ sigcantmask;
216 		break;
217 
218 	case SIG_UNBLOCK:
219 		p->p_sigmask &= ~uap->mask;
220 		break;
221 
222 	case SIG_SETMASK:
223 		p->p_sigmask = uap->mask &~ sigcantmask;
224 		break;
225 
226 	default:
227 		error = EINVAL;
228 		break;
229 	}
230 	(void) spl0();
231 	RETURN (error);
232 }
233 
234 /* ARGSUSED */
235 sigpending(p, uap, retval)
236 	struct proc *p;
237 	void *uap;
238 	int *retval;
239 {
240 
241 	*retval = p->p_sig;
242 	RETURN (0);
243 }
244 
245 #ifdef COMPAT_43
246 /*
247  * Generalized interface signal handler, 4.3-compatible.
248  */
249 /* ARGSUSED */
250 osigvec(p, uap, retval)
251 	struct proc *p;
252 	register struct args {
253 		int	signo;
254 		struct	sigvec *nsv;
255 		struct	sigvec *osv;
256 	} *uap;
257 	int *retval;
258 {
259 	struct sigvec vec;
260 	register struct sigvec *sv;
261 	register int sig;
262 	int bit, error;
263 
264 	sig = uap->signo;
265 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
266 		RETURN (EINVAL);
267 	sv = &vec;
268 	if (uap->osv) {
269 		*(sig_t *)&sv->sv_handler = u.u_signal[sig];
270 		sv->sv_mask = u.u_sigmask[sig];
271 		bit = sigmask(sig);
272 		sv->sv_flags = 0;
273 		if ((u.u_sigonstack & bit) != 0)
274 			sv->sv_flags |= SV_ONSTACK;
275 		if ((u.u_sigintr & bit) != 0)
276 			sv->sv_flags |= SV_INTERRUPT;
277 		if (p->p_flag & SNOCLDSTOP)
278 			sv->sv_flags |= SA_NOCLDSTOP;
279 		if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
280 		    sizeof (vec)))
281 			RETURN (error);
282 	}
283 	if (uap->nsv) {
284 		if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
285 		    sizeof (vec)))
286 			RETURN (error);
287 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
288 		setsigvec(p, sig, (struct sigaction *)sv);
289 	}
290 	RETURN (0);
291 }
292 
293 osigblock(p, uap, retval)
294 	register struct proc *p;
295 	struct args {
296 		int	mask;
297 	} *uap;
298 	int *retval;
299 {
300 
301 	(void) splhigh();
302 	*retval = p->p_sigmask;
303 	p->p_sigmask |= uap->mask &~ sigcantmask;
304 	(void) spl0();
305 	RETURN (0);
306 }
307 
308 osigsetmask(p, uap, retval)
309 	struct proc *p;
310 	struct args {
311 		int	mask;
312 	} *uap;
313 	int *retval;
314 {
315 
316 	(void) splhigh();
317 	*retval = p->p_sigmask;
318 	p->p_sigmask = uap->mask &~ sigcantmask;
319 	(void) spl0();
320 	RETURN (0);
321 }
322 #endif
323 
324 /*
325  * Suspend process until signal, providing mask to be set
326  * in the meantime.  Note nonstandard calling convention:
327  * libc stub passes mask, not pointer, to save a copyin.
328  */
329 /* ARGSUSED */
330 sigsuspend(p, uap, retval)
331 	register struct proc *p;
332 	struct args {
333 		sigset_t mask;
334 	} *uap;
335 	int *retval;
336 {
337 
338 	/*
339 	 * When returning from sigpause, we want
340 	 * the old mask to be restored after the
341 	 * signal handler has finished.  Thus, we
342 	 * save it here and mark the proc structure
343 	 * to indicate this (should be in u.).
344 	 */
345 	u.u_oldmask = p->p_sigmask;
346 	p->p_flag |= SOMASK;
347 	p->p_sigmask = uap->mask &~ sigcantmask;
348 	(void) tsleep((caddr_t)&u, PPAUSE | PCATCH, "pause", 0);
349 	/* always return EINTR rather than ERESTART... */
350 	RETURN (EINTR);
351 }
352 
353 /* ARGSUSED */
354 sigstack(p, uap, retval)
355 	struct proc *p;
356 	register struct args {
357 		struct	sigstack *nss;
358 		struct	sigstack *oss;
359 	} *uap;
360 	int *retval;
361 {
362 	struct sigstack ss;
363 	int error = 0;
364 
365 	if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack,
366 	    (caddr_t)uap->oss, sizeof (struct sigstack))))
367 		RETURN (error);
368 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
369 	    sizeof (ss))) == 0)
370 		u.u_sigstack = ss;
371 	RETURN (error);
372 }
373 
374 /* ARGSUSED */
375 kill(cp, uap, retval)
376 	register struct proc *cp;
377 	register struct args {
378 		int	pid;
379 		int	signo;
380 	} *uap;
381 	int *retval;
382 {
383 	register struct proc *p;
384 
385 	if ((unsigned) uap->signo >= NSIG)
386 		RETURN (EINVAL);
387 	if (uap->pid > 0) {
388 		/* kill single process */
389 		p = pfind(uap->pid);
390 		if (p == 0)
391 			RETURN (ESRCH);
392 		if (!CANSIGNAL(cp, p, uap->signo))
393 			RETURN (EPERM);
394 		if (uap->signo)
395 			psignal(p, uap->signo);
396 		RETURN (0);
397 	}
398 	switch (uap->pid) {
399 	case -1:		/* broadcast signal */
400 		RETURN (killpg1(cp, uap->signo, 0, 1));
401 	case 0:			/* signal own process group */
402 		RETURN (killpg1(cp, uap->signo, 0, 0));
403 	default:		/* negative explicit process group */
404 		RETURN (killpg1(cp, uap->signo, -uap->pid, 0));
405 	}
406 	/* NOTREACHED */
407 }
408 
409 #ifdef COMPAT_43
410 /* ARGSUSED */
411 okillpg(p, uap, retval)
412 	struct proc *p;
413 	register struct args {
414 		int	pgid;
415 		int	signo;
416 	} *uap;
417 	int *retval;
418 {
419 
420 	if ((unsigned) uap->signo >= NSIG)
421 		RETURN (EINVAL);
422 	RETURN (killpg1(p, uap->signo, uap->pgid, 0));
423 }
424 #endif
425 
426 /*
427  * Common code for kill process group/broadcast kill.
428  * cp is calling process.
429  */
430 killpg1(cp, signo, pgid, all)
431 	register struct proc *cp;
432 	int signo, pgid, all;
433 {
434 	register struct proc *p;
435 	struct pgrp *pgrp;
436 	int f = 0;
437 
438 	if (all)
439 		/*
440 		 * broadcast
441 		 */
442 		for (p = allproc; p != NULL; p = p->p_nxt) {
443 			if (p->p_ppid == 0 || p->p_flag&SSYS ||
444 			    p == u.u_procp || !CANSIGNAL(cp, p, signo))
445 				continue;
446 			f++;
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 = u.u_procp->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_ppid == 0 || p->p_flag&SSYS ||
463 			    !CANSIGNAL(cp, p, signo))
464 				continue;
465 			f++;
466 			if (signo)
467 				psignal(p, signo);
468 		}
469 	}
470 	return (f ? 0 : ESRCH);
471 }
472 
473 /*
474  * Send the specified signal to
475  * all processes with 'pgid' as
476  * process group.
477  */
478 gsignal(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 pgsignal(pgrp, sig, checkctty)
492 	struct pgrp *pgrp;
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 trapsignal(sig, code)
508 	register int sig;
509 	unsigned code;
510 {
511 	register struct proc *p = u.u_procp;	/* XXX */
512 	int mask;
513 
514 	mask = sigmask(sig);
515 	if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 &&
516 	    (p->p_sigmask & mask) == 0) {
517 		u.u_ru.ru_nsignals++;
518 #ifdef KTRACE
519 		if (KTRPOINT(p, KTR_PSIG))
520 			ktrpsig(p->p_tracep, sig, u.u_signal[sig],
521 				p->p_sigmask, code);
522 #endif
523 		sendsig(u.u_signal[sig], sig, p->p_sigmask, code);
524 		p->p_sigmask |= u.u_sigmask[sig] | mask;
525 	} else {
526 		u.u_arg[1] = code;	/* XXX for core dump/debugger */
527 		psignal(p, sig);
528 	}
529 }
530 
531 /*
532  * Send the specified signal to the specified process.
533  * Most signals do not do anything directly to a process;
534  * they set a flag that asks the process to do something to itself.
535  * Exceptions:
536  *   o When a stop signal is sent to a sleeping process that takes the default
537  *     action, the process is stopped without awakening it.
538  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
539  *     regardless of the signal action (eg, blocked or ignored).
540  * Other ignored signals are discarded immediately.
541  */
542 psignal(p, sig)
543 	register struct proc *p;
544 	register int sig;
545 {
546 	register int s;
547 	register sig_t action;
548 	int mask;
549 
550 	if ((unsigned)sig >= NSIG || sig == 0)
551 		panic("psignal sig");
552 	mask = sigmask(sig);
553 
554 	/*
555 	 * If proc is traced, always give parent a chance.
556 	 */
557 	if (p->p_flag & STRC)
558 		action = SIG_DFL;
559 	else {
560 		/*
561 		 * If the signal is being ignored,
562 		 * then we forget about it immediately.
563 		 * (Note: we don't set SIGCONT in p_sigignore,
564 		 * and if it is set to SIG_IGN,
565 		 * action will be SIG_DFL here.)
566 		 */
567 		if (p->p_sigignore & mask)
568 			return;
569 		if (p->p_sigmask & mask)
570 			action = SIG_HOLD;
571 		else if (p->p_sigcatch & mask)
572 			action = SIG_CATCH;
573 		else
574 			action = SIG_DFL;
575 	}
576 	switch (sig) {
577 
578 	case SIGTERM:
579 		if ((p->p_flag&STRC) || action != SIG_DFL)
580 			break;
581 		/* FALLTHROUGH */
582 
583 	case SIGKILL:
584 		if (p->p_nice > NZERO)
585 			p->p_nice = NZERO;
586 		break;
587 
588 	case SIGCONT:
589 		p->p_sig &= ~stopsigmask;
590 		break;
591 
592 	case SIGTSTP:
593 	case SIGTTIN:
594 	case SIGTTOU:
595 	case SIGSTOP:
596 		p->p_sig &= ~sigmask(SIGCONT);
597 		break;
598 	}
599 	p->p_sig |= mask;
600 
601 	/*
602 	 * Defer further processing for signals which are held,
603 	 * except that stopped processes must be continued by SIGCONT.
604 	 */
605 	if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP))
606 		return;
607 	s = splhigh();
608 	switch (p->p_stat) {
609 
610 	case SSLEEP:
611 		/*
612 		 * If process is sleeping uninterruptibly
613 		 * we can't interrupt the sleep... the signal will
614 		 * be noticed when the process returns through
615 		 * trap() or syscall().
616 		 */
617 		if ((p->p_flag & SSINTR) == 0)
618 			goto out;
619 		/*
620 		 * Process is sleeping and traced... make it runnable
621 		 * so it can discover the signal in issig() and stop
622 		 * for the parent.
623 		 */
624 		if (p->p_flag&STRC)
625 			goto run;
626 		/*
627 		 * When a sleeping process receives a stop
628 		 * signal, process immediately if possible.
629 		 * All other (caught or default) signals
630 		 * cause the process to run.
631 		 */
632 		if (mask & stopsigmask) {
633 			if (action != SIG_DFL)
634 				goto runfast;
635 			/*
636 			 * If a child in vfork(), stopping could
637 			 * cause deadlock.
638 			 */
639 			if (p->p_flag&SVFORK)
640 				goto out;
641 			p->p_sig &= ~mask;
642 			p->p_cursig = sig;
643 			if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
644 				psignal(p->p_pptr, SIGCHLD);
645 			stop(p);
646 			goto out;
647 		} else
648 			goto runfast;
649 		/*NOTREACHED*/
650 
651 	case SSTOP:
652 		/*
653 		 * If traced process is already stopped,
654 		 * then no further action is necessary.
655 		 */
656 		if (p->p_flag&STRC)
657 			goto out;
658 		switch (sig) {
659 
660 		case SIGKILL:
661 			/*
662 			 * Kill signal always sets processes running.
663 			 */
664 			goto runfast;
665 
666 		case SIGCONT:
667 			/*
668 			 * If SIGCONT is default (or ignored), we continue
669 			 * the process but don't leave the signal in p_sig,
670 			 * as it has no further action.  If SIGCONT is held,
671 			 * continue the process and leave the signal in p_sig.
672 			 * If the process catches SIGCONT, let it handle
673 			 * the signal itself.  If it isn't waiting on
674 			 * an event, then it goes back to run state.
675 			 * Otherwise, process goes back to sleep state.
676 			 */
677 			p->p_cursig = 0;	/* ??? XXX */
678 			if (action == SIG_DFL)
679 				p->p_sig &= ~mask;
680 			if (action == SIG_CATCH)
681 				goto runfast;
682 			if (p->p_wchan == 0)
683 				goto run;
684 			p->p_stat = SSLEEP;
685 			goto out;
686 
687 		case SIGSTOP:
688 		case SIGTSTP:
689 		case SIGTTIN:
690 		case SIGTTOU:
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 		default:
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 		/*NOTREACHED*/
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 == u.u_procp && !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 asked at least once each time a process enters the
741  * system (though this can usually be done without actually
742  * calling issig by checking the pending signal masks.)
743  */
744 issig()
745 {
746 	register struct proc *p = u.u_procp;		/* XXX */
747 	register int sig, mask;
748 
749 	for (;;) {
750 		mask = p->p_sig &~ p->p_sigmask;
751 		if (p->p_flag&SVFORK)
752 			mask &= ~stopsigmask;
753 		if (mask == 0)	 	/* no signal to send */
754 			return (0);
755 		sig = ffs((long)mask);
756 		mask = sigmask(sig);
757 		/*
758 		 * We should see pending but ignored signals
759 		 * only if STRC was on when they were posted.
760 		 */
761 		if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
762 			p->p_sig &= ~mask;
763 			continue;
764 		}
765 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
766 			/*
767 			 * If traced, always stop, and stay
768 			 * stopped until released by the parent.
769 			 */
770 			p->p_cursig = sig;
771 			psignal(p->p_pptr, SIGCHLD);
772 			do {
773 				stop(p);
774 				swtch();
775 			} while (!procxmt(p) && p->p_flag&STRC);
776 
777 			/*
778 			 * If the traced bit got turned off,
779 			 * go back up to the top to rescan signals.
780 			 * This ensures that p_sig* and u_signal are consistent.
781 			 */
782 			if ((p->p_flag&STRC) == 0)
783 				continue;
784 
785 			/*
786 			 * If parent wants us to take the signal,
787 			 * then it will leave it in p->p_cursig;
788 			 * otherwise we just look for signals again.
789 			 */
790 			p->p_sig &= ~mask;	/* clear the old signal */
791 			sig = p->p_cursig;
792 			if (sig == 0)
793 				continue;
794 
795 			/*
796 			 * Put the new signal into p_sig.
797 			 * If signal is being masked,
798 			 * look for other signals.
799 			 */
800 			mask = sigmask(sig);
801 			p->p_sig |= mask;
802 			if (p->p_sigmask & mask)
803 				continue;
804 		}
805 
806 		/*
807 		 * Decide whether the signal should be returned.
808 		 * Return the signal's number, or fall through
809 		 * to clear it from the pending mask.
810 		 */
811 		switch ((int)u.u_signal[sig]) {
812 
813 		case SIG_DFL:
814 			/*
815 			 * Don't take default actions on system processes.
816 			 */
817 			if (p->p_ppid == 0)
818 				break;		/* == ignore */
819 			/*
820 			 * If there is a pending stop signal to process
821 			 * with default action, stop here,
822 			 * then clear the signal.  However,
823 			 * if process is member of an orphaned
824 			 * process group, ignore tty stop signals.
825 			 */
826 			if (mask & stopsigmask) {
827 				if (p->p_flag&STRC ||
828 		    		    (p->p_pgrp->pg_jobc == 0 &&
829 				    mask & ttystopsigmask))
830 					break;	/* == ignore */
831 				p->p_cursig = sig;
832 				stop(p);
833 				if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
834 					psignal(p->p_pptr, SIGCHLD);
835 				swtch();
836 				break;
837 			} else if (mask & defaultignmask) {
838 				/*
839 				 * Except for SIGCONT, shouldn't get here.
840 				 * Default action is to ignore; drop it.
841 				 */
842 				break;		/* == ignore */
843 			} else
844 				return (sig);
845 			/*NOTREACHED*/
846 
847 		case SIG_IGN:
848 			/*
849 			 * Masking above should prevent us ever trying
850 			 * to take action on an ignored signal other
851 			 * than SIGCONT, unless process is traced.
852 			 */
853 			if (sig != SIGCONT && (p->p_flag&STRC) == 0)
854 				printf("issig\n");
855 			break;		/* == ignore */
856 
857 		default:
858 			/*
859 			 * This signal has an action, let
860 			 * psig process it.
861 			 */
862 			return (sig);
863 		}
864 		p->p_sig &= ~mask;		/* take the signal! */
865 	}
866 	/* NOTREACHED */
867 }
868 
869 /*
870  * Put the argument process into the stopped
871  * state and notify the parent via wakeup.
872  * Signals are handled elsewhere.
873  * The process must not be on the run queue.
874  */
875 stop(p)
876 	register struct proc *p;
877 {
878 
879 	p->p_stat = SSTOP;
880 	p->p_flag &= ~SWTED;
881 	wakeup((caddr_t)p->p_pptr);
882 }
883 
884 /*
885  * Perform the action specified by the current signal.
886  * The usual sequence is:
887  *	if (sig = CURSIG(p))
888  *		psig(sig);
889  */
890 psig(sig)
891 	register int sig;
892 {
893 	register struct proc *p = u.u_procp;
894 	int mask, returnmask;
895 	register sig_t action;
896 
897 	do {
898 #ifdef DIAGNOSTIC
899 		if (sig == 0)
900 			panic("psig");
901 #endif
902 		mask = sigmask(sig);
903 		p->p_sig &= ~mask;
904 		action = u.u_signal[sig];
905 #ifdef KTRACE
906 		if (KTRPOINT(p, KTR_PSIG))
907 			ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ?
908 				u.u_oldmask : p->p_sigmask, 0);
909 #endif
910 		if (action != SIG_DFL) {
911 #ifdef DIAGNOSTIC
912 			if (action == SIG_IGN || (p->p_sigmask & mask))
913 				panic("psig action");
914 #endif
915 			u.u_error = 0;
916 			/*
917 			 * Set the new mask value and also defer further
918 			 * occurences of this signal.
919 			 *
920 			 * Special case: user has done a sigpause.  Here the
921 			 * current mask is not of interest, but rather the
922 			 * mask from before the sigpause is what we want
923 			 * restored after the signal processing is completed.
924 			 */
925 			(void) splhigh();
926 			if (p->p_flag & SOMASK) {
927 				returnmask = u.u_oldmask;
928 				p->p_flag &= ~SOMASK;
929 			} else
930 				returnmask = p->p_sigmask;
931 			p->p_sigmask |= u.u_sigmask[sig] | mask;
932 			(void) spl0();
933 			u.u_ru.ru_nsignals++;
934 			sendsig(action, sig, returnmask, 0);
935 			continue;
936 		}
937 		u.u_acflag |= AXSIG;
938 		switch (sig) {
939 
940 		case SIGILL:
941 		case SIGIOT:
942 		case SIGBUS:
943 		case SIGQUIT:
944 		case SIGTRAP:
945 		case SIGEMT:
946 		case SIGFPE:
947 		case SIGSEGV:
948 		case SIGSYS:
949 			u.u_arg[0] = sig;
950 			if (core() == 0)
951 				sig |= WCOREFLAG;
952 		}
953 		exit(p, W_EXITCODE(0, sig));
954 		/* NOTREACHED */
955 	} while (sig = CURSIG(p));
956 }
957 
958 /*
959  * Create a core image on the file "core".
960  * It writes UPAGES block of the
961  * user.h area followed by the entire
962  * data+stack segments.
963  */
964 core()
965 {
966 	register struct vnode *vp;
967 	register struct proc *p = u.u_procp;
968 	register struct nameidata *ndp = &u.u_nd;
969 	struct vattr vattr;
970 	int error;
971 
972 	if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid)
973 		return (EFAULT);
974 	if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
975 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
976 		return (EFAULT);
977 	if (p->p_textp) {
978 		VOP_LOCK(p->p_textp->x_vptr);
979 		error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred);
980 		VOP_UNLOCK(p->p_textp->x_vptr);
981 		if (error)
982 			return (EFAULT);
983 	}
984 	ndp->ni_segflg = UIO_SYSSPACE;
985 	ndp->ni_dirp = "core";
986 	if (error = vn_open(ndp, FCREAT|FWRITE, 0644))
987 		return (error);
988 	vp = ndp->ni_vp;
989 	VOP_LOCK(vp);
990 	if (vp->v_type != VREG ||
991 	    VOP_GETATTR(vp, &vattr, u.u_cred) ||
992 	    vattr.va_nlink != 1) {
993 		vput(vp);
994 		return (EFAULT);
995 	}
996 #ifdef MAPMEM
997 	if (error = mmcore(p)) {
998 		vput(vp);
999 		return (error);
1000 	}
1001 #endif
1002 	VATTR_NULL(&vattr);
1003 	vattr.va_size = 0;
1004 	VOP_SETATTR(vp, &vattr, u.u_cred);
1005 	u.u_acflag |= ACORE;
1006 #ifdef HPUXCOMPAT
1007 	/*
1008 	 * BLETCH!  If we loaded from an HPUX format binary file
1009 	 * we have to dump an HPUX style user struct so that the
1010 	 * HPUX debuggers can grok it.
1011 	 */
1012 	if (u.u_pcb.pcb_flags & PCB_HPUXBIN)
1013 		error = hpuxdumpu(vp, ndp->ni_cred);
1014 	else
1015 #endif
1016 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0,
1017 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
1018 	if (error == 0)
1019 		error = vn_rdwr(UIO_WRITE, vp,
1020 		    (caddr_t)ctob(dptov(p, 0)),
1021 		    (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
1022 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
1023 	if (error == 0)
1024 		error = vn_rdwr(UIO_WRITE, vp,
1025 		    (caddr_t)ctob(sptov(p, u.u_ssize - 1)),
1026 		    (int)ctob(u.u_ssize),
1027 		    (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE,
1028 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
1029 	vput(vp);
1030 	return (error);
1031 }
1032 
1033 /*
1034  * Nonexistent system call-- signal process (may want to handle it).
1035  * Flag error in case process won't see signal immediately (blocked or ignored).
1036  */
1037 /* ARGSUSED */
1038 nosys(p, args, retval)
1039 	struct proc *p;
1040 	void *args;
1041 	int *retval;
1042 {
1043 
1044 	psignal(p, SIGSYS);
1045 	RETURN (EINVAL);
1046 }
1047