xref: /original-bsd/sys/kern/kern_sig.c (revision b485b642)
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.11 (Berkeley) 11/11/89
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 "mount.h"
29 #include "text.h"
30 #include "seg.h"
31 #include "vm.h"
32 #include "acct.h"
33 #include "uio.h"
34 #include "file.h"
35 #include "kernel.h"
36 #include "wait.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	stopsigmask	(sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
44 			sigmask(SIGTTIN)|sigmask(SIGTTOU))
45 #define defaultignmask	(sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \
46 			sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO))
47 
48 /*
49  * Can the current process (u.u_procp) send the specified signal
50  * to the specified process?
51  */
52 #define CANSIGNAL(p, signo) \
53 	(u.u_uid == 0 || \
54 	    u.u_uid == (p)->p_uid || u.u_uid == (p)->p_ruid || \
55 	    u.u_procp->p_ruid == (p)->p_uid || \
56 	    u.u_procp->p_ruid == (p)->p_ruid || \
57 	    ((signo) == SIGCONT && (p)->p_session == u.u_procp->p_session))
58 
59 sigaction()
60 {
61 	register struct a {
62 		int	signo;
63 		struct	sigaction *nsa;
64 		struct	sigaction *osa;
65 	} *uap = (struct a  *)u.u_ap;
66 	struct sigaction vec;
67 	register struct sigaction *sa;
68 	register int sig;
69 	int bit, error;
70 
71 	sig = uap->signo;
72 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
73 		RETURN (EINVAL);
74 	sa = &vec;
75 	if (uap->osa) {
76 		sa->sa_handler = u.u_signal[sig];
77 		sa->sa_mask = u.u_sigmask[sig];
78 		bit = sigmask(sig);
79 		sa->sa_flags = 0;
80 		if ((u.u_sigonstack & bit) != 0)
81 			sa->sa_flags |= SA_ONSTACK;
82 		if ((u.u_sigintr & bit) == 0)
83 			sa->sa_flags |= SA_RESTART;
84 		if (u.u_procp->p_flag & SNOCLDSTOP)
85 			sa->sa_flags |= SA_NOCLDSTOP;
86 		if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
87 		    sizeof (vec)))
88 			RETURN (error);
89 	}
90 	if (uap->nsa) {
91 		if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
92 		    sizeof (vec)))
93 			RETURN (error);
94 		setsigvec(sig, sa);
95 	}
96 	RETURN (0);
97 }
98 
99 setsigvec(sig, sa)
100 	int sig;
101 	register struct sigaction *sa;
102 {
103 	register struct proc *p;
104 	register int bit;
105 
106 	bit = sigmask(sig);
107 	p = u.u_procp;
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()
201 {
202 	struct a {
203 		int	how;
204 		sigset_t mask;
205 	} *uap = (struct a *)u.u_ap;
206 	register struct proc *p = u.u_procp;
207 	int error = 0;
208 
209 	u.u_r.r_val1 = p->p_sigmask;
210 	(void) splhigh();
211 
212 	switch (uap->how) {
213 	case SIG_BLOCK:
214 		p->p_sigmask |= uap->mask &~ sigcantmask;
215 		break;
216 
217 	case SIG_UNBLOCK:
218 		p->p_sigmask &= ~uap->mask;
219 		break;
220 
221 	case SIG_SETMASK:
222 		p->p_sigmask = uap->mask &~ sigcantmask;
223 		break;
224 
225 	default:
226 		error = EINVAL;
227 		break;
228 	}
229 	(void) spl0();
230 	RETURN (error);
231 }
232 
233 sigpending()
234 {
235 
236 	u.u_r.r_val1 = u.u_procp->p_sig;
237 	RETURN (0);
238 }
239 
240 #ifdef COMPAT_43
241 /*
242  * Generalized interface signal handler, 4.3-compatible.
243  */
244 osigvec()
245 {
246 	register struct a {
247 		int	signo;
248 		struct	sigvec *nsv;
249 		struct	sigvec *osv;
250 	} *uap = (struct a  *)u.u_ap;
251 	struct sigvec vec;
252 	register struct sigvec *sv;
253 	register int sig;
254 	int bit, error;
255 
256 	sig = uap->signo;
257 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
258 		RETURN (EINVAL);
259 	sv = &vec;
260 	if (uap->osv) {
261 		*(sig_t *)&sv->sv_handler = u.u_signal[sig];
262 		sv->sv_mask = u.u_sigmask[sig];
263 		bit = sigmask(sig);
264 		sv->sv_flags = 0;
265 		if ((u.u_sigonstack & bit) != 0)
266 			sv->sv_flags |= SV_ONSTACK;
267 		if ((u.u_sigintr & bit) != 0)
268 			sv->sv_flags |= SV_INTERRUPT;
269 		if (u.u_procp->p_flag & SNOCLDSTOP)
270 			sv->sv_flags |= SA_NOCLDSTOP;
271 		if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
272 		    sizeof (vec)))
273 			RETURN (error);
274 	}
275 	if (uap->nsv) {
276 		if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
277 		    sizeof (vec)))
278 			RETURN (error);
279 		sv->sv_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
280 		setsigvec(sig, sv);
281 	}
282 	RETURN (0);
283 }
284 
285 osigblock()
286 {
287 	struct a {
288 		int	mask;
289 	} *uap = (struct a *)u.u_ap;
290 	register struct proc *p = u.u_procp;
291 
292 	(void) splhigh();
293 	u.u_r.r_val1 = p->p_sigmask;
294 	p->p_sigmask |= uap->mask &~ sigcantmask;
295 	(void) spl0();
296 	RETURN (0);
297 }
298 
299 osigsetmask()
300 {
301 	struct a {
302 		int	mask;
303 	} *uap = (struct a *)u.u_ap;
304 	register struct proc *p = u.u_procp;
305 
306 	(void) splhigh();
307 	u.u_r.r_val1 = p->p_sigmask;
308 	p->p_sigmask = uap->mask &~ sigcantmask;
309 	(void) spl0();
310 	RETURN (0);
311 }
312 #endif
313 
314 /*
315  * Suspend process until signal, providing mask to be set
316  * in the meantime.  Note nonstandard calling convention:
317  * libc stub passes mask, not pointer, to save a copyin.
318  */
319 sigsuspend()
320 {
321 	struct a {
322 		sigset_t mask;
323 	} *uap = (struct a *)u.u_ap;
324 	register struct proc *p = u.u_procp;
325 
326 	/*
327 	 * When returning from sigpause, we want
328 	 * the old mask to be restored after the
329 	 * signal handler has finished.  Thus, we
330 	 * save it here and mark the proc structure
331 	 * to indicate this (should be in u.).
332 	 */
333 	u.u_oldmask = p->p_sigmask;
334 	p->p_flag |= SOMASK;
335 	p->p_sigmask = uap->mask &~ sigcantmask;
336 	for (;;)
337 		sleep((caddr_t)&u, PSLEP);
338 	/*NOTREACHED*/
339 }
340 
341 sigstack()
342 {
343 	register struct a {
344 		struct	sigstack *nss;
345 		struct	sigstack *oss;
346 	} *uap = (struct a *)u.u_ap;
347 	struct sigstack ss;
348 	int error = 0;
349 
350 	if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack,
351 	    (caddr_t)uap->oss, sizeof (struct sigstack))))
352 		RETURN (error);
353 	if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
354 	    sizeof (ss))) == 0)
355 		u.u_sigstack = ss;
356 	RETURN (error);
357 }
358 
359 kill()
360 {
361 	register struct a {
362 		int	pid;
363 		int	signo;
364 	} *uap = (struct a *)u.u_ap;
365 	register struct proc *p;
366 
367 	if ((unsigned) uap->signo >= NSIG)
368 		RETURN (EINVAL);
369 	if (uap->pid > 0) {
370 		/* kill single process */
371 		p = pfind(uap->pid);
372 		if (p == 0)
373 			RETURN (ESRCH);
374 		if (!CANSIGNAL(p, uap->signo))
375 			RETURN (EPERM);
376 		if (uap->signo)
377 			psignal(p, uap->signo);
378 		RETURN (0);
379 	}
380 	switch (uap->pid) {
381 	case -1:		/* broadcast signal */
382 		RETURN (killpg1(uap->signo, 0, 1));
383 	case 0:			/* signal own process group */
384 		RETURN (killpg1(uap->signo, 0, 0));
385 	default:		/* negative explicit process group */
386 		RETURN (killpg1(uap->signo, -uap->pid, 0));
387 	}
388 	/* NOTREACHED */
389 }
390 
391 #ifdef COMPAT_43
392 okillpg()
393 {
394 	register struct a {
395 		int	pgid;
396 		int	signo;
397 	} *uap = (struct a *)u.u_ap;
398 
399 	if ((unsigned) uap->signo >= NSIG)
400 		RETURN (EINVAL);
401 	RETURN (killpg1(uap->signo, uap->pgid, 0));
402 }
403 #endif
404 
405 killpg1(signo, pgid, all)
406 	int signo, pgid, all;
407 {
408 	register struct proc *p;
409 	struct pgrp *pgrp;
410 	int f = 0, error = ESRCH;
411 
412 	if (all)
413 		/*
414 		 * broadcast
415 		 */
416 		for (p = allproc; p != NULL; p = p->p_nxt) {
417 			if (p->p_ppid == 0 || p->p_flag&SSYS ||
418 			    p == u.u_procp || !CANSIGNAL(p, signo))
419 				continue;
420 			f++;
421 			if (signo)
422 				psignal(p, signo);
423 		}
424 	else {
425 		if (pgid == 0)
426 			/*
427 			 * zero pgid means send to my process group.
428 			 */
429 			pgrp = u.u_procp->p_pgrp;
430 		else {
431 			pgrp = pgfind(pgid);
432 			if (pgrp == NULL)
433 				return (ESRCH);
434 		}
435 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
436 			if (p->p_ppid == 0 || p->p_flag&SSYS ||
437 			    !CANSIGNAL(p, signo))
438 				continue;
439 			f++;
440 			if (signo)
441 				psignal(p, signo);
442 		}
443 	}
444 	return (f ? 0 : error);
445 }
446 
447 /*
448  * Send the specified signal to
449  * all processes with 'pgid' as
450  * process group.
451  */
452 gsignal(pgid, sig)
453 {
454 	struct pgrp *pgrp;
455 
456 	if (pgid && (pgrp = pgfind(pgid)))
457 		pgsignal(pgrp, sig);
458 }
459 
460 pgsignal(pgrp, sig)
461 	struct pgrp *pgrp;
462 {
463 	register struct proc *p;
464 
465 	for (p = pgrp->pg_mem; p; p = p->p_pgrpnxt)
466 		psignal(p, sig);
467 }
468 
469 /*
470  * Send a signal caused by a trap to the current process.
471  * If it will be caught immediately, deliver it with correct code.
472  * Otherwise, post it normally.
473  */
474 trapsignal(sig, code)
475 	register int sig;
476 	unsigned code;
477 {
478 	register struct proc *p = u.u_procp;
479 	int mask;
480 
481 	mask = sigmask(sig);
482 	if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 &&
483 	    (p->p_sigmask & mask) == 0) {
484 		u.u_ru.ru_nsignals++;
485 		sendsig(u.u_signal[sig], sig, p->p_sigmask, code);
486 		p->p_sigmask |= u.u_sigmask[sig] | mask;
487 	} else {
488 		u.u_arg[1] = code;	/* XXX for core dump/debugger */
489 		psignal(p, sig);
490 	}
491 }
492 
493 /*
494  * Send the specified signal to
495  * the specified process.
496  */
497 psignal(p, sig)
498 	register struct proc *p;
499 	register int sig;
500 {
501 	register int s;
502 	register sig_t action;
503 	int mask;
504 
505 	if ((unsigned)sig >= NSIG || sig == 0)
506 		panic("psignal sig");
507 	if (p->p_pgrp->pg_jobc == 0 &&
508 	     (sig == SIGTTIN || sig == SIGTTOU || sig == SIGTSTP))
509 		return;
510 	mask = sigmask(sig);
511 
512 	/*
513 	 * If proc is traced, always give parent a chance.
514 	 */
515 	if (p->p_flag & STRC)
516 		action = SIG_DFL;
517 	else {
518 		/*
519 		 * If the signal is being ignored,
520 		 * then we forget about it immediately.
521 		 * (Note: we don't set SIGCONT in p_sigignore,
522 		 * and if it is set to SIG_IGN,
523 		 * action will be SIG_DFL here.)
524 		 */
525 		if (p->p_sigignore & mask)
526 			return;
527 		if (p->p_sigmask & mask)
528 			action = SIG_HOLD;
529 		else if (p->p_sigcatch & mask)
530 			action = SIG_CATCH;
531 		else
532 			action = SIG_DFL;
533 	}
534 	switch (sig) {
535 
536 	case SIGTERM:
537 		if ((p->p_flag&STRC) || action != SIG_DFL)
538 			break;
539 		/* FALLTHROUGH */
540 
541 	case SIGKILL:
542 		if (p->p_nice > NZERO)
543 			p->p_nice = NZERO;
544 		break;
545 
546 	case SIGCONT:
547 		p->p_sig &= ~stopsigmask;
548 		break;
549 
550 	case SIGTSTP:
551 	case SIGTTIN:
552 	case SIGTTOU:
553 	case SIGSTOP:
554 		p->p_sig &= ~sigmask(SIGCONT);
555 		break;
556 	}
557 	p->p_sig |= mask;
558 
559 	/*
560 	 * Defer further processing for signals which are held,
561 	 * except that stopped processes must be continued by SIGCONT.
562 	 */
563 	if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP))
564 		return;
565 	s = splhigh();
566 	switch (p->p_stat) {
567 
568 	case SSLEEP:
569 		/*
570 		 * If process is sleeping at negative priority
571 		 * we can't interrupt the sleep... the signal will
572 		 * be noticed when the process returns through
573 		 * trap() or syscall().
574 		 */
575 		if (p->p_pri <= PZERO)
576 			goto out;
577 		/*
578 		 * Process is sleeping and traced... make it runnable
579 		 * so it can discover the signal in issig() and stop
580 		 * for the parent.
581 		 */
582 		if (p->p_flag&STRC)
583 			goto run;
584 		/*
585 		 * When a sleeping process receives a stop
586 		 * signal, process immediately if possible.
587 		 * All other (caught or default) signals
588 		 * cause the process to run.
589 		 */
590 		if (mask & stopsigmask) {
591 			if (action != SIG_DFL)
592 				goto runfast;
593 			/*
594 			 * If a child in vfork(), stopping could
595 			 * cause deadlock.
596 			 */
597 			if (p->p_flag&SVFORK)
598 				goto out;
599 			p->p_sig &= ~mask;
600 			p->p_cursig = sig;
601 			if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
602 				psignal(p->p_pptr, SIGCHLD);
603 			stop(p);
604 			goto out;
605 		} else
606 			goto runfast;
607 		/*NOTREACHED*/
608 
609 	case SSTOP:
610 		/*
611 		 * If traced process is already stopped,
612 		 * then no further action is necessary.
613 		 */
614 		if (p->p_flag&STRC)
615 			goto out;
616 		switch (sig) {
617 
618 		case SIGKILL:
619 			/*
620 			 * Kill signal always sets processes running.
621 			 */
622 			goto runfast;
623 
624 		case SIGCONT:
625 			/*
626 			 * If SIGCONT is default (or ignored), we continue
627 			 * the process but don't leave the signal in p_sig,
628 			 * as it has no further action.  If SIGCONT is held,
629 			 * continue the process and leave the signal in p_sig.
630 			 * If the process catches SIGCONT, let it handle
631 			 * the signal itself.  If it isn't waiting on
632 			 * an event, then it goes back to run state.
633 			 * Otherwise, process goes back to sleep state.
634 			 */
635 			p->p_cursig = 0;	/* ??? XXX */
636 			if (action == SIG_DFL)
637 				p->p_sig &= ~mask;
638 			if (action == SIG_CATCH)
639 				goto runfast;
640 			if (p->p_wchan == 0)
641 				goto run;
642 			p->p_stat = SSLEEP;
643 			goto out;
644 
645 		case SIGSTOP:
646 		case SIGTSTP:
647 		case SIGTTIN:
648 		case SIGTTOU:
649 			/*
650 			 * Already stopped, don't need to stop again.
651 			 * (If we did the shell could get confused.)
652 			 */
653 			p->p_sig &= ~mask;		/* take it away */
654 			goto out;
655 
656 		default:
657 			/*
658 			 * If process is sleeping interruptibly, then
659 			 * unstick it so that when it is continued
660 			 * it can look at the signal.
661 			 * But don't setrun the process as its not to
662 			 * be unstopped by the signal alone.
663 			 */
664 			if (p->p_wchan && p->p_pri > PZERO)
665 				unsleep(p);
666 			goto out;
667 		}
668 		/*NOTREACHED*/
669 
670 	default:
671 		/*
672 		 * SRUN, SIDL, SZOMB do nothing with the signal,
673 		 * other than kicking ourselves if we are running.
674 		 * It will either never be noticed, or noticed very soon.
675 		 */
676 		if (p == u.u_procp && !noproc)
677 			aston();
678 		goto out;
679 	}
680 	/*NOTREACHED*/
681 
682 runfast:
683 	/*
684 	 * Raise priority to at least PUSER.
685 	 */
686 	if (p->p_pri > PUSER)
687 		p->p_pri = PUSER;
688 run:
689 	setrun(p);
690 out:
691 	splx(s);
692 }
693 
694 /*
695  * Returns true if the current
696  * process has a signal to process.
697  * The signal to process is put in p_cursig.
698  * This is asked at least once each time a process enters the
699  * system (though this can usually be done without actually
700  * calling issig by checking the pending signal masks.)
701  * Most signals do not do anything
702  * directly to a process; they set
703  * a flag that asks the process to
704  * do something to itself.
705  */
706 issig()
707 {
708 	register struct proc *p;
709 	register int sig, mask;
710 
711 	p = u.u_procp;
712 	for (;;) {
713 		mask = p->p_sig &~ p->p_sigmask;
714 		if (p->p_flag&SVFORK)
715 			mask &= ~stopsigmask;
716 		if (mask == 0)
717 			break;
718 		sig = ffs((long)mask);
719 		mask = sigmask(sig);
720 		p->p_sig &= ~mask;		/* take the signal! */
721 		if (mask & p->p_sigignore && (p->p_flag&STRC) == 0)
722 			continue;	/* only if STRC was on when posted */
723 		p->p_cursig = sig;
724 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
725 			/*
726 			 * If traced, always stop, and stay
727 			 * stopped until released by the parent.
728 			 */
729 			psignal(p->p_pptr, SIGCHLD);
730 			do {
731 				stop(p);
732 				swtch();
733 			} while (!procxmt() && p->p_flag&STRC);
734 
735 			/*
736 			 * If the traced bit got turned off,
737 			 * then put the signal taken above back into p_sig
738 			 * and go back up to the top to rescan signals.
739 			 * This ensures that p_sig* and u_signal are consistent.
740 			 */
741 			if ((p->p_flag&STRC) == 0) {
742 				p->p_sig |= mask;
743 				continue;
744 			}
745 
746 			/*
747 			 * If parent wants us to take the signal,
748 			 * then it will leave it in p->p_cursig;
749 			 * otherwise we just look for signals again.
750 			 */
751 			sig = p->p_cursig;
752 			if (sig == 0)
753 				continue;
754 
755 			/*
756 			 * If signal is being masked put it back
757 			 * into p_sig and look for other signals.
758 			 */
759 			mask = sigmask(sig);
760 			if (p->p_sigmask & mask) {
761 				p->p_sig |= mask;
762 				continue;
763 			}
764 		}
765 		switch ((int)u.u_signal[sig]) {
766 
767 		case SIG_DFL:
768 			/*
769 			 * Don't take default actions on system processes.
770 			 */
771 			if (p->p_ppid == 0)
772 				continue;
773 			if (mask & stopsigmask) {
774 				if (p->p_flag&STRC)
775 					continue;
776 				stop(p);
777 				if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
778 					psignal(p->p_pptr, SIGCHLD);
779 				swtch();
780 				continue;
781 			} else if (mask & defaultignmask) {
782 				/*
783 				 * Except for SIGCONT, shouldn't get here.
784 				 * Default action is to ignore; drop it.
785 				 */
786 				continue;		/* == ignore */
787 			} else
788 				goto send;
789 			/*NOTREACHED*/
790 
791 		case SIG_IGN:
792 			/*
793 			 * Masking above should prevent us ever trying
794 			 * to take action on an ignored signal other
795 			 * than SIGCONT, unless process is traced.
796 			 */
797 			if (sig != SIGCONT && (p->p_flag&STRC) == 0)
798 				printf("issig\n");
799 			continue;
800 
801 		default:
802 			/*
803 			 * This signal has an action, let
804 			 * psig process it.
805 			 */
806 			goto send;
807 		}
808 		/*NOTREACHED*/
809 	}
810 	/*
811 	 * Didn't find a signal to send.
812 	 */
813 	p->p_cursig = 0;
814 	return (0);
815 
816 send:
817 	/*
818 	 * Let psig process the signal.
819 	 */
820 	return (sig);
821 }
822 
823 /*
824  * Put the argument process into the stopped
825  * state and notify the parent via wakeup.
826  * Signals are handled elsewhere.
827  */
828 stop(p)
829 	register struct proc *p;
830 {
831 
832 	p->p_stat = SSTOP;
833 	p->p_flag &= ~SWTED;
834 	wakeup((caddr_t)p->p_pptr);
835 }
836 
837 /*
838  * Perform the action specified by
839  * the current signal.
840  * The usual sequence is:
841  *	if (p->p_cursig || ISSIG(p))
842  *		psig();
843  * The signal bit has already been cleared by issig,
844  * and the current signal number stored in p->p_cursig.
845  */
846 psig()
847 {
848 	register struct proc *p = u.u_procp;
849 	register int sig;
850 	int mask, returnmask;
851 	register sig_t action;
852 
853 	do {
854 		sig = p->p_cursig;
855 		mask = sigmask(sig);
856 		if (sig == 0)
857 			panic("psig");
858 		action = u.u_signal[sig];
859 		if (action != SIG_DFL) {
860 #ifdef DIAGNOSTIC
861 			if (action == SIG_IGN || (p->p_sigmask & mask))
862 				panic("psig action");
863 #endif
864 			u.u_error = 0;
865 			/*
866 			 * Set the new mask value and also defer further
867 			 * occurences of this signal.
868 			 *
869 			 * Special case: user has done a sigpause.  Here the
870 			 * current mask is not of interest, but rather the
871 			 * mask from before the sigpause is what we want
872 			 * restored after the signal processing is completed.
873 			 */
874 			(void) splhigh();
875 			if (p->p_flag & SOMASK) {
876 				returnmask = u.u_oldmask;
877 				p->p_flag &= ~SOMASK;
878 			} else
879 				returnmask = p->p_sigmask;
880 			p->p_sigmask |= u.u_sigmask[sig] | mask;
881 			(void) spl0();
882 			u.u_ru.ru_nsignals++;
883 			sendsig(action, sig, returnmask, 0);
884 			p->p_cursig = 0;
885 			continue;
886 		}
887 		u.u_acflag |= AXSIG;
888 		switch (sig) {
889 
890 		case SIGILL:
891 		case SIGIOT:
892 		case SIGBUS:
893 		case SIGQUIT:
894 		case SIGTRAP:
895 		case SIGEMT:
896 		case SIGFPE:
897 		case SIGSEGV:
898 		case SIGSYS:
899 			u.u_arg[0] = sig;
900 			if (core() == 0)
901 				sig |= WCOREFLAG;
902 		}
903 		exit(W_EXITCODE(0, sig));
904 		/* NOTREACHED */
905 	} while (ISSIG(p));
906 }
907 
908 /*
909  * Create a core image on the file "core".
910  * It writes UPAGES block of the
911  * user.h area followed by the entire
912  * data+stack segments.
913  */
914 core()
915 {
916 	register struct vnode *vp;
917 	register struct proc *p = u.u_procp;
918 	register struct nameidata *ndp = &u.u_nd;
919 	struct vattr vattr;
920 	int error;
921 
922 	if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid)
923 		return (EFAULT);
924 	if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
925 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
926 		return (EFAULT);
927 	if (p->p_textp) {
928 		VOP_LOCK(p->p_textp->x_vptr);
929 		error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred);
930 		VOP_UNLOCK(p->p_textp->x_vptr);
931 		if (error)
932 			return (EFAULT);
933 	}
934 	ndp->ni_segflg = UIO_SYSSPACE;
935 	ndp->ni_dirp = "core";
936 	if (error = vn_open(ndp, FCREAT|FWRITE, 0644))
937 		return (error);
938 	vp = ndp->ni_vp;
939 	VOP_LOCK(vp);
940 	if (vp->v_type != VREG ||
941 	    VOP_GETATTR(vp, &vattr, u.u_cred) ||
942 	    vattr.va_nlink != 1) {
943 		vput(vp);
944 		return (EFAULT);
945 	}
946 #ifdef MMAP
947 	{ register int fd;
948 	/* unmap funky devices in the user's address space */
949 	for (fd = 0; fd < u.u_lastfile; fd++)
950 		if (u.u_ofile[fd] && (u.u_pofile[fd] & UF_MAPPED))
951 			munmapfd(fd);
952 	}
953 #endif
954 	vattr_null(&vattr);
955 	vattr.va_size = 0;
956 	VOP_SETATTR(vp, &vattr, u.u_cred);
957 	u.u_acflag |= ACORE;
958 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0,
959 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
960 	if (error == 0)
961 		error = vn_rdwr(UIO_WRITE, vp,
962 		    (caddr_t)ctob(dptov(p, 0)),
963 		    (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
964 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
965 	if (error == 0)
966 		error = vn_rdwr(UIO_WRITE, vp,
967 		    (caddr_t)ctob(sptov(p, u.u_ssize - 1)),
968 		    (int)ctob(u.u_ssize),
969 		    (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE,
970 		    IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
971 	vput(vp);
972 	return (error);
973 }
974 
975 /*
976  * Nonexistent system call-- signal process (may want to handle it).
977  * Flag error in case process won't see signal immediately (blocked or ignored).
978  */
979 nosys()
980 {
981 
982 	psignal(u.u_procp, SIGSYS);
983 	u.u_error = EINVAL;
984 }
985