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