xref: /csrg-svn/sys/kern/kern_sig.c (revision 44405)
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.22 (Berkeley) 06/28/90
18  */
19 
20 #include "param.h"
21 #include "systm.h"
22 #include "user.h"
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_code = 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_xstat = 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 			if (action == SIG_DFL)
678 				p->p_sig &= ~mask;
679 			if (action == SIG_CATCH)
680 				goto runfast;
681 			if (p->p_wchan == 0)
682 				goto run;
683 			p->p_stat = SSLEEP;
684 			goto out;
685 
686 		case SIGSTOP:
687 		case SIGTSTP:
688 		case SIGTTIN:
689 		case SIGTTOU:
690 			/*
691 			 * Already stopped, don't need to stop again.
692 			 * (If we did the shell could get confused.)
693 			 */
694 			p->p_sig &= ~mask;		/* take it away */
695 			goto out;
696 
697 		default:
698 			/*
699 			 * If process is sleeping interruptibly, then
700 			 * simulate a wakeup so that when it is continued,
701 			 * it will be made runnable and can look at the signal.
702 			 * But don't setrun the process, leave it stopped.
703 			 */
704 			if (p->p_wchan && p->p_flag & SSINTR)
705 				unsleep(p);
706 			goto out;
707 		}
708 		/*NOTREACHED*/
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 == u.u_procp && !noproc)
717 			aston();
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 asked at least once each time a process enters the
740  * system (though this can usually be done without actually
741  * calling issig by checking the pending signal masks.)
742  */
743 issig()
744 {
745 	register struct proc *p = u.u_procp;		/* XXX */
746 	register int sig, mask;
747 
748 	for (;;) {
749 		mask = p->p_sig &~ p->p_sigmask;
750 		if (p->p_flag&SVFORK)
751 			mask &= ~stopsigmask;
752 		if (mask == 0)	 	/* no signal to send */
753 			return (0);
754 		sig = ffs((long)mask);
755 		mask = sigmask(sig);
756 		/*
757 		 * We should see pending but ignored signals
758 		 * only if STRC was on when they were posted.
759 		 */
760 		if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
761 			p->p_sig &= ~mask;
762 			continue;
763 		}
764 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
765 			/*
766 			 * If traced, always stop, and stay
767 			 * stopped until released by the parent.
768 			 */
769 			p->p_xstat = sig;
770 			psignal(p->p_pptr, SIGCHLD);
771 			do {
772 				stop(p);
773 				swtch();
774 			} while (!procxmt(p) && p->p_flag&STRC);
775 
776 			/*
777 			 * If the traced bit got turned off,
778 			 * go back up to the top to rescan signals.
779 			 * This ensures that p_sig* and u_signal are consistent.
780 			 */
781 			if ((p->p_flag&STRC) == 0)
782 				continue;
783 
784 			/*
785 			 * If parent wants us to take the signal,
786 			 * then it will leave it in p->p_xstat;
787 			 * otherwise we just look for signals again.
788 			 */
789 			p->p_sig &= ~mask;	/* clear the old signal */
790 			sig = p->p_xstat;
791 			if (sig == 0)
792 				continue;
793 
794 			/*
795 			 * Put the new signal into p_sig.
796 			 * If signal is being masked,
797 			 * look for other signals.
798 			 */
799 			mask = sigmask(sig);
800 			p->p_sig |= mask;
801 			if (p->p_sigmask & mask)
802 				continue;
803 		}
804 
805 		/*
806 		 * Decide whether the signal should be returned.
807 		 * Return the signal's number, or fall through
808 		 * to clear it from the pending mask.
809 		 */
810 		switch ((int)u.u_signal[sig]) {
811 
812 		case SIG_DFL:
813 			/*
814 			 * Don't take default actions on system processes.
815 			 */
816 			if (p->p_ppid == 0)
817 				break;		/* == ignore */
818 			/*
819 			 * If there is a pending stop signal to process
820 			 * with default action, stop here,
821 			 * then clear the signal.  However,
822 			 * if process is member of an orphaned
823 			 * process group, ignore tty stop signals.
824 			 */
825 			if (mask & stopsigmask) {
826 				if (p->p_flag&STRC ||
827 		    		    (p->p_pgrp->pg_jobc == 0 &&
828 				    mask & ttystopsigmask))
829 					break;	/* == ignore */
830 				p->p_xstat = sig;
831 				stop(p);
832 				if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
833 					psignal(p->p_pptr, SIGCHLD);
834 				swtch();
835 				break;
836 			} else if (mask & defaultignmask) {
837 				/*
838 				 * Except for SIGCONT, shouldn't get here.
839 				 * Default action is to ignore; drop it.
840 				 */
841 				break;		/* == ignore */
842 			} else
843 				return (sig);
844 			/*NOTREACHED*/
845 
846 		case SIG_IGN:
847 			/*
848 			 * Masking above should prevent us ever trying
849 			 * to take action on an ignored signal other
850 			 * than SIGCONT, unless process is traced.
851 			 */
852 			if (sig != SIGCONT && (p->p_flag&STRC) == 0)
853 				printf("issig\n");
854 			break;		/* == ignore */
855 
856 		default:
857 			/*
858 			 * This signal has an action, let
859 			 * psig process it.
860 			 */
861 			return (sig);
862 		}
863 		p->p_sig &= ~mask;		/* take the signal! */
864 	}
865 	/* NOTREACHED */
866 }
867 
868 /*
869  * Put the argument process into the stopped
870  * state and notify the parent via wakeup.
871  * Signals are handled elsewhere.
872  * The process must not be on the run queue.
873  */
874 stop(p)
875 	register struct proc *p;
876 {
877 
878 	p->p_stat = SSTOP;
879 	p->p_flag &= ~SWTED;
880 	wakeup((caddr_t)p->p_pptr);
881 }
882 
883 /*
884  * Perform the action specified by the current signal.
885  * The usual sequence is:
886  *	if (sig = CURSIG(p))
887  *		psig(sig);
888  */
889 psig(sig)
890 	register int sig;
891 {
892 	register struct proc *p = u.u_procp;
893 	int mask, returnmask;
894 	register sig_t action;
895 
896 	do {
897 #ifdef DIAGNOSTIC
898 		if (sig == 0)
899 			panic("psig");
900 #endif
901 		mask = sigmask(sig);
902 		p->p_sig &= ~mask;
903 		action = u.u_signal[sig];
904 #ifdef KTRACE
905 		if (KTRPOINT(p, KTR_PSIG))
906 			ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ?
907 				u.u_oldmask : p->p_sigmask, 0);
908 #endif
909 		if (action != SIG_DFL) {
910 #ifdef DIAGNOSTIC
911 			if (action == SIG_IGN || (p->p_sigmask & mask))
912 				panic("psig action");
913 #endif
914 			/*
915 			 * Set the new mask value and also defer further
916 			 * occurences of this signal.
917 			 *
918 			 * Special case: user has done a sigpause.  Here the
919 			 * current mask is not of interest, but rather the
920 			 * mask from before the sigpause is what we want
921 			 * restored after the signal processing is completed.
922 			 */
923 			(void) splhigh();
924 			if (p->p_flag & SOMASK) {
925 				returnmask = u.u_oldmask;
926 				p->p_flag &= ~SOMASK;
927 			} else
928 				returnmask = p->p_sigmask;
929 			p->p_sigmask |= u.u_sigmask[sig] | mask;
930 			(void) spl0();
931 			u.u_ru.ru_nsignals++;
932 			sendsig(action, sig, returnmask, 0);
933 			continue;
934 		}
935 		u.u_acflag |= AXSIG;
936 		switch (sig) {
937 
938 		case SIGILL:
939 		case SIGIOT:
940 		case SIGBUS:
941 		case SIGQUIT:
942 		case SIGTRAP:
943 		case SIGEMT:
944 		case SIGFPE:
945 		case SIGSEGV:
946 		case SIGSYS:
947 			u.u_sig = sig;
948 			if (core() == 0)
949 				sig |= WCOREFLAG;
950 		}
951 		exit(p, W_EXITCODE(0, sig));
952 		/* NOTREACHED */
953 	} while (sig = CURSIG(p));
954 }
955 
956 /*
957  * Create a core image on the file "core".
958  * It writes UPAGES block of the
959  * user.h area followed by the entire
960  * data+stack segments.
961  */
962 core()
963 {
964 	register struct vnode *vp;
965 	register struct proc *p = u.u_procp;
966 	register struct nameidata *ndp = &u.u_nd;
967 	struct vattr vattr;
968 	int error;
969 
970 	if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid)
971 		return (EFAULT);
972 	if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
973 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
974 		return (EFAULT);
975 	if (p->p_textp) {
976 		VOP_LOCK(p->p_textp->x_vptr);
977 		error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred);
978 		VOP_UNLOCK(p->p_textp->x_vptr);
979 		if (error)
980 			return (EFAULT);
981 	}
982 	ndp->ni_segflg = UIO_SYSSPACE;
983 	ndp->ni_dirp = "core";
984 	if (error = vn_open(ndp, FCREAT|FWRITE, 0644))
985 		return (error);
986 	vp = ndp->ni_vp;
987 	VOP_LOCK(vp);
988 	if (vp->v_type != VREG ||
989 	    VOP_GETATTR(vp, &vattr, u.u_cred) ||
990 	    vattr.va_nlink != 1) {
991 		vput(vp);
992 		return (EFAULT);
993 	}
994 #ifdef MAPMEM
995 	if (error = mmcore(p)) {
996 		vput(vp);
997 		return (error);
998 	}
999 #endif
1000 	VATTR_NULL(&vattr);
1001 	vattr.va_size = 0;
1002 	VOP_SETATTR(vp, &vattr, u.u_cred);
1003 	u.u_acflag |= ACORE;
1004 #ifdef HPUXCOMPAT
1005 	/*
1006 	 * BLETCH!  If we loaded from an HPUX format binary file
1007 	 * we have to dump an HPUX style user struct so that the
1008 	 * HPUX debuggers can grok it.
1009 	 */
1010 	if (u.u_pcb.pcb_flags & PCB_HPUXBIN)
1011 		error = hpuxdumpu(vp, ndp->ni_cred);
1012 	else
1013 #endif
1014 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0,
1015 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
1016 	if (error == 0)
1017 		error = vn_rdwr(UIO_WRITE, vp,
1018 		    (caddr_t)ctob(dptov(p, 0)),
1019 		    (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
1020 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
1021 	if (error == 0)
1022 		error = vn_rdwr(UIO_WRITE, vp,
1023 		    (caddr_t)ctob(sptov(p, u.u_ssize - 1)),
1024 		    (int)ctob(u.u_ssize),
1025 		    (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE,
1026 		    IO_NODELOCKED|IO_UNIT, ndp->ni_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