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