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