xref: /freebsd/sys/kern/kern_sig.c (revision c68159a6d8eede11766cf13896d0f7670dbd51aa)
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  * $FreeBSD$
40  */
41 
42 #include "opt_compat.h"
43 #include "opt_ktrace.h"
44 
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/sysproto.h>
48 #include <sys/systm.h>
49 #include <sys/signalvar.h>
50 #include <sys/resourcevar.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/event.h>
54 #include <sys/proc.h>
55 #include <sys/pioctl.h>
56 #include <sys/acct.h>
57 #include <sys/fcntl.h>
58 #include <sys/ipl.h>
59 #include <sys/mutex.h>
60 #include <sys/wait.h>
61 #include <sys/ktr.h>
62 #include <sys/ktrace.h>
63 #include <sys/syslog.h>
64 #include <sys/stat.h>
65 #include <sys/sysent.h>
66 #include <sys/sysctl.h>
67 #include <sys/malloc.h>
68 
69 #include <machine/cpu.h>
70 #include <machine/smp.h>
71 
72 #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
73 
74 static int coredump	__P((struct proc *));
75 static int do_sigaction	__P((struct proc *p, int sig, struct sigaction *act,
76 			     struct sigaction *oact, int old));
77 static int do_sigprocmask __P((struct proc *p, int how, sigset_t *set,
78 			       sigset_t *oset, int old));
79 static char *expand_name __P((const char *, uid_t, pid_t));
80 static int killpg1	__P((struct proc *cp, int sig, int pgid, int all));
81 static int sig_ffs	__P((sigset_t *set));
82 static int sigprop	__P((int sig));
83 static void stop	__P((struct proc *));
84 
85 static int	filt_sigattach(struct knote *kn);
86 static void	filt_sigdetach(struct knote *kn);
87 static int	filt_signal(struct knote *kn, long hint);
88 
89 struct filterops sig_filtops =
90 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
91 
92 static int	kern_logsigexit = 1;
93 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
94     &kern_logsigexit, 0,
95     "Log processes quitting on abnormal signals to syslog(3)");
96 
97 /*
98  * Can process p, with pcred pc, send the signal sig to process q?
99  */
100 #define CANSIGNAL(p, q, sig) \
101 	(!p_can(p, q, P_CAN_KILL, NULL) || \
102 	((sig) == SIGCONT && (q)->p_session == (p)->p_session))
103 
104 /*
105  * Policy -- Can real uid ruid with ucred uc send a signal to process q?
106  */
107 #define CANSIGIO(ruid, uc, q) \
108 	((uc)->cr_uid == 0 || \
109 	    (ruid) == (q)->p_cred->p_ruid || \
110 	    (uc)->cr_uid == (q)->p_cred->p_ruid || \
111 	    (ruid) == (q)->p_ucred->cr_uid || \
112 	    (uc)->cr_uid == (q)->p_ucred->cr_uid)
113 
114 int sugid_coredump;
115 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
116     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
117 
118 static int	do_coredump = 1;
119 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
120 	&do_coredump, 0, "Enable/Disable coredumps");
121 
122 /*
123  * Signal properties and actions.
124  * The array below categorizes the signals and their default actions
125  * according to the following properties:
126  */
127 #define	SA_KILL		0x01		/* terminates process by default */
128 #define	SA_CORE		0x02		/* ditto and coredumps */
129 #define	SA_STOP		0x04		/* suspend process */
130 #define	SA_TTYSTOP	0x08		/* ditto, from tty */
131 #define	SA_IGNORE	0x10		/* ignore by default */
132 #define	SA_CONT		0x20		/* continue if suspended */
133 #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
134 
135 static int sigproptbl[NSIG] = {
136         SA_KILL,                /* SIGHUP */
137         SA_KILL,                /* SIGINT */
138         SA_KILL|SA_CORE,        /* SIGQUIT */
139         SA_KILL|SA_CORE,        /* SIGILL */
140         SA_KILL|SA_CORE,        /* SIGTRAP */
141         SA_KILL|SA_CORE,        /* SIGABRT */
142         SA_KILL|SA_CORE,        /* SIGEMT */
143         SA_KILL|SA_CORE,        /* SIGFPE */
144         SA_KILL,                /* SIGKILL */
145         SA_KILL|SA_CORE,        /* SIGBUS */
146         SA_KILL|SA_CORE,        /* SIGSEGV */
147         SA_KILL|SA_CORE,        /* SIGSYS */
148         SA_KILL,                /* SIGPIPE */
149         SA_KILL,                /* SIGALRM */
150         SA_KILL,                /* SIGTERM */
151         SA_IGNORE,              /* SIGURG */
152         SA_STOP,                /* SIGSTOP */
153         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
154         SA_IGNORE|SA_CONT,      /* SIGCONT */
155         SA_IGNORE,              /* SIGCHLD */
156         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
157         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
158         SA_IGNORE,              /* SIGIO */
159         SA_KILL,                /* SIGXCPU */
160         SA_KILL,                /* SIGXFSZ */
161         SA_KILL,                /* SIGVTALRM */
162         SA_KILL,                /* SIGPROF */
163         SA_IGNORE,              /* SIGWINCH  */
164         SA_IGNORE,              /* SIGINFO */
165         SA_KILL,                /* SIGUSR1 */
166         SA_KILL,                /* SIGUSR2 */
167 };
168 
169 /*
170  * Determine signal that should be delivered to process p, the current
171  * process, 0 if none.  If there is a pending stop signal with default
172  * action, the process stops in issignal().
173  *
174  * MP SAFE.
175  */
176 int
177 CURSIG(struct proc *p)
178 {
179 	sigset_t tmpset;
180 	int r;
181 
182 	if (SIGISEMPTY(p->p_siglist))
183 		return (0);
184 	tmpset = p->p_siglist;
185 	SIGSETNAND(tmpset, p->p_sigmask);
186 	if (SIGISEMPTY(tmpset) && (p->p_flag & P_TRACED) == 0)
187 		return (0);
188 	mtx_enter(&Giant, MTX_DEF);
189 	r = issignal(p);
190 	mtx_exit(&Giant, MTX_DEF);
191 	return (r);
192 }
193 
194 static __inline int
195 sigprop(int sig)
196 {
197 
198 	if (sig > 0 && sig < NSIG)
199 		return (sigproptbl[_SIG_IDX(sig)]);
200 	return (0);
201 }
202 
203 static __inline int
204 sig_ffs(sigset_t *set)
205 {
206 	int i;
207 
208 	for (i = 0; i < _SIG_WORDS; i++)
209 		if (set->__bits[i])
210 			return (ffs(set->__bits[i]) + (i * 32));
211 	return (0);
212 }
213 
214 /*
215  * do_sigaction
216  * sigaction
217  * osigaction
218  */
219 static int
220 do_sigaction(p, sig, act, oact, old)
221 	struct proc *p;
222 	register int sig;
223 	struct sigaction *act, *oact;
224 	int old;
225 {
226 	register struct sigacts *ps = p->p_sigacts;
227 
228 	if (sig <= 0 || sig > _SIG_MAXSIG)
229 		return (EINVAL);
230 
231 	if (oact) {
232 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
233 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
234 		oact->sa_flags = 0;
235 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
236 			oact->sa_flags |= SA_ONSTACK;
237 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
238 			oact->sa_flags |= SA_RESTART;
239 		if (SIGISMEMBER(ps->ps_sigreset, sig))
240 			oact->sa_flags |= SA_RESETHAND;
241 		if (SIGISMEMBER(ps->ps_signodefer, sig))
242 			oact->sa_flags |= SA_NODEFER;
243 		if (SIGISMEMBER(ps->ps_siginfo, sig))
244 			oact->sa_flags |= SA_SIGINFO;
245 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
246 			oact->sa_flags |= SA_NOCLDSTOP;
247 		if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
248 			oact->sa_flags |= SA_NOCLDWAIT;
249 	}
250 	if (act) {
251 		if ((sig == SIGKILL || sig == SIGSTOP) &&
252 		    act->sa_handler != SIG_DFL)
253 			return (EINVAL);
254 
255 		/*
256 		 * Change setting atomically.
257 		 */
258 		(void) splhigh();
259 
260 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
261 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
262 		if (act->sa_flags & SA_SIGINFO) {
263 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
264 			SIGADDSET(ps->ps_siginfo, sig);
265 		} else {
266 			ps->ps_sigact[_SIG_IDX(sig)] =
267 			    (__sighandler_t *)act->sa_sigaction;
268 			SIGDELSET(ps->ps_siginfo, sig);
269 		}
270 		if (!(act->sa_flags & SA_RESTART))
271 			SIGADDSET(ps->ps_sigintr, sig);
272 		else
273 			SIGDELSET(ps->ps_sigintr, sig);
274 		if (act->sa_flags & SA_ONSTACK)
275 			SIGADDSET(ps->ps_sigonstack, sig);
276 		else
277 			SIGDELSET(ps->ps_sigonstack, sig);
278 		if (act->sa_flags & SA_RESETHAND)
279 			SIGADDSET(ps->ps_sigreset, sig);
280 		else
281 			SIGDELSET(ps->ps_sigreset, sig);
282 		if (act->sa_flags & SA_NODEFER)
283 			SIGADDSET(ps->ps_signodefer, sig);
284 		else
285 			SIGDELSET(ps->ps_signodefer, sig);
286 #ifdef COMPAT_SUNOS
287 		if (act->sa_flags & SA_USERTRAMP)
288 			SIGADDSET(ps->ps_usertramp, sig);
289 		else
290 			SIGDELSET(ps->ps_usertramp, seg);
291 #endif
292 		if (sig == SIGCHLD) {
293 			if (act->sa_flags & SA_NOCLDSTOP)
294 				p->p_procsig->ps_flag |= PS_NOCLDSTOP;
295 			else
296 				p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
297 			if (act->sa_flags & SA_NOCLDWAIT) {
298 				/*
299 				 * Paranoia: since SA_NOCLDWAIT is implemented
300 				 * by reparenting the dying child to PID 1 (and
301 				 * trust it to reap the zombie), PID 1 itself
302 				 * is forbidden to set SA_NOCLDWAIT.
303 				 */
304 				if (p->p_pid == 1)
305 					p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
306 				else
307 					p->p_procsig->ps_flag |= PS_NOCLDWAIT;
308 			} else
309 				p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
310 		}
311 		/*
312 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
313 		 * and for signals set to SIG_DFL where the default is to
314 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
315 		 * have to restart the process.
316 		 */
317 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
318 		    (sigprop(sig) & SA_IGNORE &&
319 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
320 			/* never to be seen again */
321 			SIGDELSET(p->p_siglist, sig);
322 			if (sig != SIGCONT)
323 				/* easier in psignal */
324 				SIGADDSET(p->p_sigignore, sig);
325 			SIGDELSET(p->p_sigcatch, sig);
326 		} else {
327 			SIGDELSET(p->p_sigignore, sig);
328 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
329 				SIGDELSET(p->p_sigcatch, sig);
330 			else
331 				SIGADDSET(p->p_sigcatch, sig);
332 		}
333 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
334 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
335 			SIGDELSET(ps->ps_osigset, sig);
336 		else
337 			SIGADDSET(ps->ps_osigset, sig);
338 
339 		(void) spl0();
340 	}
341 	return (0);
342 }
343 
344 #ifndef _SYS_SYSPROTO_H_
345 struct sigaction_args {
346 	int	sig;
347 	struct	sigaction *act;
348 	struct	sigaction *oact;
349 };
350 #endif
351 /* ARGSUSED */
352 int
353 sigaction(p, uap)
354 	struct proc *p;
355 	register struct sigaction_args *uap;
356 {
357 	struct sigaction act, oact;
358 	register struct sigaction *actp, *oactp;
359 	int error;
360 
361 	actp = (uap->act != NULL) ? &act : NULL;
362 	oactp = (uap->oact != NULL) ? &oact : NULL;
363 	if (actp) {
364 		error = copyin(uap->act, actp, sizeof(act));
365 		if (error)
366 			return (error);
367 	}
368 	error = do_sigaction(p, uap->sig, actp, oactp, 0);
369 	if (oactp && !error) {
370 		error = copyout(oactp, uap->oact, sizeof(oact));
371 	}
372 	return (error);
373 }
374 
375 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
376 #ifndef _SYS_SYSPROTO_H_
377 struct osigaction_args {
378 	int	signum;
379 	struct	osigaction *nsa;
380 	struct	osigaction *osa;
381 };
382 #endif
383 /* ARGSUSED */
384 int
385 osigaction(p, uap)
386 	struct proc *p;
387 	register struct osigaction_args *uap;
388 {
389 	struct osigaction sa;
390 	struct sigaction nsa, osa;
391 	register struct sigaction *nsap, *osap;
392 	int error;
393 
394 	if (uap->signum <= 0 || uap->signum >= ONSIG)
395 		return (EINVAL);
396 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
397 	osap = (uap->osa != NULL) ? &osa : NULL;
398 	if (nsap) {
399 		error = copyin(uap->nsa, &sa, sizeof(sa));
400 		if (error)
401 			return (error);
402 		nsap->sa_handler = sa.sa_handler;
403 		nsap->sa_flags = sa.sa_flags;
404 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
405 	}
406 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
407 	if (osap && !error) {
408 		sa.sa_handler = osap->sa_handler;
409 		sa.sa_flags = osap->sa_flags;
410 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
411 		error = copyout(&sa, uap->osa, sizeof(sa));
412 	}
413 	return (error);
414 }
415 #endif /* COMPAT_43 */
416 
417 /*
418  * Initialize signal state for process 0;
419  * set to ignore signals that are ignored by default.
420  */
421 void
422 siginit(p)
423 	struct proc *p;
424 {
425 	register int i;
426 
427 	for (i = 1; i <= NSIG; i++)
428 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
429 			SIGADDSET(p->p_sigignore, i);
430 }
431 
432 /*
433  * Reset signals for an exec of the specified process.
434  */
435 void
436 execsigs(p)
437 	register struct proc *p;
438 {
439 	register struct sigacts *ps = p->p_sigacts;
440 	register int sig;
441 
442 	/*
443 	 * Reset caught signals.  Held signals remain held
444 	 * through p_sigmask (unless they were caught,
445 	 * and are now ignored by default).
446 	 */
447 	while (SIGNOTEMPTY(p->p_sigcatch)) {
448 		sig = sig_ffs(&p->p_sigcatch);
449 		SIGDELSET(p->p_sigcatch, sig);
450 		if (sigprop(sig) & SA_IGNORE) {
451 			if (sig != SIGCONT)
452 				SIGADDSET(p->p_sigignore, sig);
453 			SIGDELSET(p->p_siglist, sig);
454 		}
455 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
456 	}
457 	/*
458 	 * Reset stack state to the user stack.
459 	 * Clear set of signals caught on the signal stack.
460 	 */
461 	p->p_sigstk.ss_flags = SS_DISABLE;
462 	p->p_sigstk.ss_size = 0;
463 	p->p_sigstk.ss_sp = 0;
464 	/*
465 	 * Reset no zombies if child dies flag as Solaris does.
466 	 */
467 	p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
468 }
469 
470 /*
471  * do_sigprocmask() - MP SAFE ONLY IF p == curproc
472  *
473  *	Manipulate signal mask.  This routine is MP SAFE *ONLY* if
474  *	p == curproc.  Also remember that in order to remain MP SAFE
475  *	no spl*() calls may be made.
476  */
477 static int
478 do_sigprocmask(p, how, set, oset, old)
479 	struct proc *p;
480 	int how;
481 	sigset_t *set, *oset;
482 	int old;
483 {
484 	int error;
485 
486 	if (oset != NULL)
487 		*oset = p->p_sigmask;
488 
489 	error = 0;
490 	if (set != NULL) {
491 		switch (how) {
492 		case SIG_BLOCK:
493 			SIG_CANTMASK(*set);
494 			SIGSETOR(p->p_sigmask, *set);
495 			break;
496 		case SIG_UNBLOCK:
497 			SIGSETNAND(p->p_sigmask, *set);
498 			break;
499 		case SIG_SETMASK:
500 			SIG_CANTMASK(*set);
501 			if (old)
502 				SIGSETLO(p->p_sigmask, *set);
503 			else
504 				p->p_sigmask = *set;
505 			break;
506 		default:
507 			error = EINVAL;
508 			break;
509 		}
510 	}
511 	return (error);
512 }
513 
514 /*
515  * sigprocmask() - MP SAFE
516  */
517 
518 #ifndef _SYS_SYSPROTO_H_
519 struct sigprocmask_args {
520 	int	how;
521 	const sigset_t *set;
522 	sigset_t *oset;
523 };
524 #endif
525 int
526 sigprocmask(p, uap)
527 	register struct proc *p;
528 	struct sigprocmask_args *uap;
529 {
530 	sigset_t set, oset;
531 	sigset_t *setp, *osetp;
532 	int error;
533 
534 	setp = (uap->set != NULL) ? &set : NULL;
535 	osetp = (uap->oset != NULL) ? &oset : NULL;
536 	if (setp) {
537 		error = copyin(uap->set, setp, sizeof(set));
538 		if (error)
539 			return (error);
540 	}
541 	error = do_sigprocmask(p, uap->how, setp, osetp, 0);
542 	if (osetp && !error) {
543 		error = copyout(osetp, uap->oset, sizeof(oset));
544 	}
545 	return (error);
546 }
547 
548 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
549 /*
550  * osigprocmask() - MP SAFE
551  */
552 #ifndef _SYS_SYSPROTO_H_
553 struct osigprocmask_args {
554 	int	how;
555 	osigset_t mask;
556 };
557 #endif
558 int
559 osigprocmask(p, uap)
560 	register struct proc *p;
561 	struct osigprocmask_args *uap;
562 {
563 	sigset_t set, oset;
564 	int error;
565 
566 	OSIG2SIG(uap->mask, set);
567 	error = do_sigprocmask(p, uap->how, &set, &oset, 1);
568 	SIG2OSIG(oset, p->p_retval[0]);
569 	return (error);
570 }
571 #endif /* COMPAT_43 */
572 
573 #ifndef _SYS_SYSPROTO_H_
574 struct sigpending_args {
575 	sigset_t	*set;
576 };
577 #endif
578 /* ARGSUSED */
579 int
580 sigpending(p, uap)
581 	struct proc *p;
582 	struct sigpending_args *uap;
583 {
584 
585 	return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
586 }
587 
588 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
589 #ifndef _SYS_SYSPROTO_H_
590 struct osigpending_args {
591 	int	dummy;
592 };
593 #endif
594 /* ARGSUSED */
595 int
596 osigpending(p, uap)
597 	struct proc *p;
598 	struct osigpending_args *uap;
599 {
600 
601 	SIG2OSIG(p->p_siglist, p->p_retval[0]);
602 	return (0);
603 }
604 #endif /* COMPAT_43 */
605 
606 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
607 /*
608  * Generalized interface signal handler, 4.3-compatible.
609  */
610 #ifndef _SYS_SYSPROTO_H_
611 struct osigvec_args {
612 	int	signum;
613 	struct	sigvec *nsv;
614 	struct	sigvec *osv;
615 };
616 #endif
617 /* ARGSUSED */
618 int
619 osigvec(p, uap)
620 	struct proc *p;
621 	register struct osigvec_args *uap;
622 {
623 	struct sigvec vec;
624 	struct sigaction nsa, osa;
625 	register struct sigaction *nsap, *osap;
626 	int error;
627 
628 	if (uap->signum <= 0 || uap->signum >= ONSIG)
629 		return (EINVAL);
630 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
631 	osap = (uap->osv != NULL) ? &osa : NULL;
632 	if (nsap) {
633 		error = copyin(uap->nsv, &vec, sizeof(vec));
634 		if (error)
635 			return (error);
636 		nsap->sa_handler = vec.sv_handler;
637 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
638 		nsap->sa_flags = vec.sv_flags;
639 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
640 #ifdef COMPAT_SUNOS
641 		nsap->sa_flags |= SA_USERTRAMP;
642 #endif
643 	}
644 	error = do_sigaction(p, uap->signum, nsap, osap, 1);
645 	if (osap && !error) {
646 		vec.sv_handler = osap->sa_handler;
647 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
648 		vec.sv_flags = osap->sa_flags;
649 		vec.sv_flags &= ~SA_NOCLDWAIT;
650 		vec.sv_flags ^= SA_RESTART;
651 #ifdef COMPAT_SUNOS
652 		vec.sv_flags &= ~SA_NOCLDSTOP;
653 #endif
654 		error = copyout(&vec, uap->osv, sizeof(vec));
655 	}
656 	return (error);
657 }
658 
659 #ifndef _SYS_SYSPROTO_H_
660 struct osigblock_args {
661 	int	mask;
662 };
663 #endif
664 int
665 osigblock(p, uap)
666 	register struct proc *p;
667 	struct osigblock_args *uap;
668 {
669 	sigset_t set;
670 
671 	OSIG2SIG(uap->mask, set);
672 	SIG_CANTMASK(set);
673 	(void) splhigh();
674 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
675 	SIGSETOR(p->p_sigmask, set);
676 	(void) spl0();
677 	return (0);
678 }
679 
680 #ifndef _SYS_SYSPROTO_H_
681 struct osigsetmask_args {
682 	int	mask;
683 };
684 #endif
685 int
686 osigsetmask(p, uap)
687 	struct proc *p;
688 	struct osigsetmask_args *uap;
689 {
690 	sigset_t set;
691 
692 	OSIG2SIG(uap->mask, set);
693 	SIG_CANTMASK(set);
694 	(void) splhigh();
695 	SIG2OSIG(p->p_sigmask, p->p_retval[0]);
696 	SIGSETLO(p->p_sigmask, set);
697 	(void) spl0();
698 	return (0);
699 }
700 #endif /* COMPAT_43 || COMPAT_SUNOS */
701 
702 /*
703  * Suspend process until signal, providing mask to be set
704  * in the meantime.  Note nonstandard calling convention:
705  * libc stub passes mask, not pointer, to save a copyin.
706  */
707 #ifndef _SYS_SYSPROTO_H_
708 struct sigsuspend_args {
709 	const sigset_t *sigmask;
710 };
711 #endif
712 /* ARGSUSED */
713 int
714 sigsuspend(p, uap)
715 	register struct proc *p;
716 	struct sigsuspend_args *uap;
717 {
718 	sigset_t mask;
719 	register struct sigacts *ps = p->p_sigacts;
720 	int error;
721 
722 	error = copyin(uap->sigmask, &mask, sizeof(mask));
723 	if (error)
724 		return (error);
725 
726 	/*
727 	 * When returning from sigsuspend, we want
728 	 * the old mask to be restored after the
729 	 * signal handler has finished.  Thus, we
730 	 * save it here and mark the sigacts structure
731 	 * to indicate this.
732 	 */
733 	p->p_oldsigmask = p->p_sigmask;
734 	p->p_flag |= P_OLDMASK;
735 
736 	SIG_CANTMASK(mask);
737 	p->p_sigmask = mask;
738 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
739 		/* void */;
740 	/* always return EINTR rather than ERESTART... */
741 	return (EINTR);
742 }
743 
744 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
745 #ifndef _SYS_SYSPROTO_H_
746 struct osigsuspend_args {
747 	osigset_t mask;
748 };
749 #endif
750 /* ARGSUSED */
751 int
752 osigsuspend(p, uap)
753 	register struct proc *p;
754 	struct osigsuspend_args *uap;
755 {
756 	sigset_t mask;
757 	register struct sigacts *ps = p->p_sigacts;
758 
759 	p->p_oldsigmask = p->p_sigmask;
760 	p->p_flag |= P_OLDMASK;
761 	OSIG2SIG(uap->mask, mask);
762 	SIG_CANTMASK(mask);
763 	SIGSETLO(p->p_sigmask, mask);
764 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
765 		/* void */;
766 	/* always return EINTR rather than ERESTART... */
767 	return (EINTR);
768 }
769 #endif /* COMPAT_43 */
770 
771 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
772 #ifndef _SYS_SYSPROTO_H_
773 struct osigstack_args {
774 	struct	sigstack *nss;
775 	struct	sigstack *oss;
776 };
777 #endif
778 /* ARGSUSED */
779 int
780 osigstack(p, uap)
781 	struct proc *p;
782 	register struct osigstack_args *uap;
783 {
784 	struct sigstack ss;
785 	int error;
786 
787 	if (uap->oss != NULL) {
788 		ss.ss_sp = p->p_sigstk.ss_sp;
789 		ss.ss_onstack = sigonstack(cpu_getstack(p));
790 		error = copyout(&ss, uap->oss, sizeof(struct sigstack));
791 		if (error)
792 			return (error);
793 	}
794 
795 	if (uap->nss != NULL) {
796 		if ((error = copyin(uap->nss, &ss, sizeof(ss))) != 0)
797 			return (error);
798 		p->p_sigstk.ss_sp = ss.ss_sp;
799 		p->p_sigstk.ss_size = 0;
800 		p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
801 		p->p_flag |= P_ALTSTACK;
802 	}
803 	return (0);
804 }
805 #endif /* COMPAT_43 || COMPAT_SUNOS */
806 
807 #ifndef _SYS_SYSPROTO_H_
808 struct sigaltstack_args {
809 	stack_t	*ss;
810 	stack_t	*oss;
811 };
812 #endif
813 /* ARGSUSED */
814 int
815 sigaltstack(p, uap)
816 	struct proc *p;
817 	register struct sigaltstack_args *uap;
818 {
819 	stack_t ss;
820 	int error, oonstack;
821 
822 	oonstack = sigonstack(cpu_getstack(p));
823 
824 	if (uap->oss != NULL) {
825 		ss = p->p_sigstk;
826 		ss.ss_flags = (p->p_flag & P_ALTSTACK)
827 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
828 		if ((error = copyout(&ss, uap->oss, sizeof(stack_t))) != 0)
829 			return (error);
830 	}
831 
832 	if (uap->ss != NULL) {
833 		if (oonstack)
834 			return (EPERM);
835 		if ((error = copyin(uap->ss, &ss, sizeof(ss))) != 0)
836 			return (error);
837 		if ((ss.ss_flags & ~SS_DISABLE) != 0)
838 			return (EINVAL);
839 		if (!(ss.ss_flags & SS_DISABLE)) {
840 			if (ss.ss_size < p->p_sysent->sv_minsigstksz)
841 				return (ENOMEM);
842 			p->p_sigstk = ss;
843 			p->p_flag |= P_ALTSTACK;
844 		} else
845 			p->p_flag &= ~P_ALTSTACK;
846 	}
847 	return (0);
848 }
849 
850 /*
851  * Common code for kill process group/broadcast kill.
852  * cp is calling process.
853  */
854 int
855 killpg1(cp, sig, pgid, all)
856 	register struct proc *cp;
857 	int sig, pgid, all;
858 {
859 	register struct proc *p;
860 	struct pgrp *pgrp;
861 	int nfound = 0;
862 
863 	if (all) {
864 		/*
865 		 * broadcast
866 		 */
867 		ALLPROC_LOCK(AP_SHARED);
868 		LIST_FOREACH(p, &allproc, p_list) {
869 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
870 			    p == cp || !CANSIGNAL(cp, p, sig))
871 				continue;
872 			nfound++;
873 			if (sig)
874 				psignal(p, sig);
875 		}
876 		ALLPROC_LOCK(AP_RELEASE);
877 	} else {
878 		if (pgid == 0)
879 			/*
880 			 * zero pgid means send to my process group.
881 			 */
882 			pgrp = cp->p_pgrp;
883 		else {
884 			pgrp = pgfind(pgid);
885 			if (pgrp == NULL)
886 				return (ESRCH);
887 		}
888 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
889 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
890 			    p->p_stat == SZOMB ||
891 			    !CANSIGNAL(cp, p, sig))
892 				continue;
893 			nfound++;
894 			if (sig)
895 				psignal(p, sig);
896 		}
897 	}
898 	return (nfound ? 0 : ESRCH);
899 }
900 
901 #ifndef _SYS_SYSPROTO_H_
902 struct kill_args {
903 	int	pid;
904 	int	signum;
905 };
906 #endif
907 /* ARGSUSED */
908 int
909 kill(cp, uap)
910 	register struct proc *cp;
911 	register struct kill_args *uap;
912 {
913 	register struct proc *p;
914 
915 	if ((u_int)uap->signum > _SIG_MAXSIG)
916 		return (EINVAL);
917 	if (uap->pid > 0) {
918 		/* kill single process */
919 		if ((p = pfind(uap->pid)) == NULL)
920 			return (ESRCH);
921 		if (!CANSIGNAL(cp, p, uap->signum))
922 			return (EPERM);
923 		if (uap->signum)
924 			psignal(p, uap->signum);
925 		return (0);
926 	}
927 	switch (uap->pid) {
928 	case -1:		/* broadcast signal */
929 		return (killpg1(cp, uap->signum, 0, 1));
930 	case 0:			/* signal own process group */
931 		return (killpg1(cp, uap->signum, 0, 0));
932 	default:		/* negative explicit process group */
933 		return (killpg1(cp, uap->signum, -uap->pid, 0));
934 	}
935 	/* NOTREACHED */
936 }
937 
938 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
939 #ifndef _SYS_SYSPROTO_H_
940 struct okillpg_args {
941 	int	pgid;
942 	int	signum;
943 };
944 #endif
945 /* ARGSUSED */
946 int
947 okillpg(p, uap)
948 	struct proc *p;
949 	register struct okillpg_args *uap;
950 {
951 
952 	if ((u_int)uap->signum > _SIG_MAXSIG)
953 		return (EINVAL);
954 	return (killpg1(p, uap->signum, uap->pgid, 0));
955 }
956 #endif /* COMPAT_43 || COMPAT_SUNOS */
957 
958 /*
959  * Send a signal to a process group.
960  */
961 void
962 gsignal(pgid, sig)
963 	int pgid, sig;
964 {
965 	struct pgrp *pgrp;
966 
967 	if (pgid && (pgrp = pgfind(pgid)))
968 		pgsignal(pgrp, sig, 0);
969 }
970 
971 /*
972  * Send a signal to a process group.  If checktty is 1,
973  * limit to members which have a controlling terminal.
974  */
975 void
976 pgsignal(pgrp, sig, checkctty)
977 	struct pgrp *pgrp;
978 	int sig, checkctty;
979 {
980 	register struct proc *p;
981 
982 	if (pgrp)
983 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
984 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
985 				psignal(p, sig);
986 }
987 
988 /*
989  * Send a signal caused by a trap to the current process.
990  * If it will be caught immediately, deliver it with correct code.
991  * Otherwise, post it normally.
992  */
993 void
994 trapsignal(p, sig, code)
995 	struct proc *p;
996 	register int sig;
997 	u_long code;
998 {
999 	register struct sigacts *ps = p->p_sigacts;
1000 
1001 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1002 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1003 		p->p_stats->p_ru.ru_nsignals++;
1004 #ifdef KTRACE
1005 		if (KTRPOINT(p, KTR_PSIG))
1006 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
1007 				&p->p_sigmask, code);
1008 #endif
1009 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
1010 						&p->p_sigmask, code);
1011 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1012 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
1013 			SIGADDSET(p->p_sigmask, sig);
1014 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1015 			/*
1016 			 * See do_sigaction() for origin of this code.
1017 			 */
1018 			SIGDELSET(p->p_sigcatch, sig);
1019 			if (sig != SIGCONT &&
1020 			    sigprop(sig) & SA_IGNORE)
1021 				SIGADDSET(p->p_sigignore, sig);
1022 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1023 		}
1024 	} else {
1025 		p->p_code = code;	/* XXX for core dump/debugger */
1026 		p->p_sig = sig;		/* XXX to verify code */
1027 		psignal(p, sig);
1028 	}
1029 }
1030 
1031 /*
1032  * Send the signal to the process.  If the signal has an action, the action
1033  * is usually performed by the target process rather than the caller; we add
1034  * the signal to the set of pending signals for the process.
1035  *
1036  * Exceptions:
1037  *   o When a stop signal is sent to a sleeping process that takes the
1038  *     default action, the process is stopped without awakening it.
1039  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1040  *     regardless of the signal action (eg, blocked or ignored).
1041  *
1042  * Other ignored signals are discarded immediately.
1043  */
1044 void
1045 psignal(p, sig)
1046 	register struct proc *p;
1047 	register int sig;
1048 {
1049 	register int s, prop;
1050 	register sig_t action;
1051 
1052 	if (sig > _SIG_MAXSIG || sig <= 0) {
1053 		printf("psignal: signal %d\n", sig);
1054 		panic("psignal signal number");
1055 	}
1056 
1057 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1058 
1059 	prop = sigprop(sig);
1060 
1061 	/*
1062 	 * If proc is traced, always give parent a chance;
1063 	 * if signal event is tracked by procfs, give *that*
1064 	 * a chance, as well.
1065 	 */
1066 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1067 		action = SIG_DFL;
1068 	else {
1069 		/*
1070 		 * If the signal is being ignored,
1071 		 * then we forget about it immediately.
1072 		 * (Note: we don't set SIGCONT in p_sigignore,
1073 		 * and if it is set to SIG_IGN,
1074 		 * action will be SIG_DFL here.)
1075 		 */
1076 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1077 			return;
1078 		if (SIGISMEMBER(p->p_sigmask, sig))
1079 			action = SIG_HOLD;
1080 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1081 			action = SIG_CATCH;
1082 		else
1083 			action = SIG_DFL;
1084 	}
1085 
1086 	mtx_enter(&sched_lock, MTX_SPIN);
1087 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1088 	    (p->p_flag & P_TRACED) == 0)
1089 		p->p_nice = NZERO;
1090 	mtx_exit(&sched_lock, MTX_SPIN);
1091 
1092 	if (prop & SA_CONT)
1093 		SIG_STOPSIGMASK(p->p_siglist);
1094 
1095 	if (prop & SA_STOP) {
1096 		/*
1097 		 * If sending a tty stop signal to a member of an orphaned
1098 		 * process group, discard the signal here if the action
1099 		 * is default; don't stop the process below if sleeping,
1100 		 * and don't clear any pending SIGCONT.
1101 		 */
1102 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1103 		    action == SIG_DFL)
1104 		        return;
1105 		SIG_CONTSIGMASK(p->p_siglist);
1106 	}
1107 	SIGADDSET(p->p_siglist, sig);
1108 
1109 	/*
1110 	 * Defer further processing for signals which are held,
1111 	 * except that stopped processes must be continued by SIGCONT.
1112 	 */
1113 	mtx_enter(&sched_lock, MTX_SPIN);
1114 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
1115 		mtx_exit(&sched_lock, MTX_SPIN);
1116 		return;
1117 	}
1118 	s = splhigh();
1119 	switch (p->p_stat) {
1120 
1121 	case SSLEEP:
1122 		/*
1123 		 * If process is sleeping uninterruptibly
1124 		 * we can't interrupt the sleep... the signal will
1125 		 * be noticed when the process returns through
1126 		 * trap() or syscall().
1127 		 */
1128 		if ((p->p_flag & P_SINTR) == 0) {
1129 			mtx_exit(&sched_lock, MTX_SPIN);
1130 			goto out;
1131 		}
1132 		/*
1133 		 * Process is sleeping and traced... make it runnable
1134 		 * so it can discover the signal in issignal() and stop
1135 		 * for the parent.
1136 		 */
1137 		if (p->p_flag & P_TRACED)
1138 			goto run;
1139 		mtx_exit(&sched_lock, MTX_SPIN);
1140 		/*
1141 		 * If SIGCONT is default (or ignored) and process is
1142 		 * asleep, we are finished; the process should not
1143 		 * be awakened.
1144 		 */
1145 		if ((prop & SA_CONT) && action == SIG_DFL) {
1146 			SIGDELSET(p->p_siglist, sig);
1147 			goto out;
1148 		}
1149 		/*
1150 		 * When a sleeping process receives a stop
1151 		 * signal, process immediately if possible.
1152 		 * All other (caught or default) signals
1153 		 * cause the process to run.
1154 		 */
1155 		if (prop & SA_STOP) {
1156 			if (action != SIG_DFL)
1157 				goto runfast;
1158 			/*
1159 			 * If a child holding parent blocked,
1160 			 * stopping could cause deadlock.
1161 			 */
1162 			if (p->p_flag & P_PPWAIT)
1163 				goto out;
1164 			SIGDELSET(p->p_siglist, sig);
1165 			p->p_xstat = sig;
1166 			PROCTREE_LOCK(PT_SHARED);
1167 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1168 				psignal(p->p_pptr, SIGCHLD);
1169 			stop(p);
1170 			PROCTREE_LOCK(PT_RELEASE);
1171 			goto out;
1172 		} else
1173 			goto runfast;
1174 		/* NOTREACHED */
1175 
1176 	case SSTOP:
1177 		/*
1178 		 * If traced process is already stopped,
1179 		 * then no further action is necessary.
1180 		 */
1181 		if (p->p_flag & P_TRACED) {
1182 			mtx_exit(&sched_lock, MTX_SPIN);
1183 			goto out;
1184 		}
1185 		mtx_exit(&sched_lock, MTX_SPIN);
1186 
1187 		/*
1188 		 * Kill signal always sets processes running.
1189 		 */
1190 		if (sig == SIGKILL)
1191 			goto runfast;
1192 
1193 		if (prop & SA_CONT) {
1194 			/*
1195 			 * If SIGCONT is default (or ignored), we continue the
1196 			 * process but don't leave the signal in p_siglist, as
1197 			 * it has no further action.  If SIGCONT is held, we
1198 			 * continue the process and leave the signal in
1199 			 * p_siglist.  If the process catches SIGCONT, let it
1200 			 * handle the signal itself.  If it isn't waiting on
1201 			 * an event, then it goes back to run state.
1202 			 * Otherwise, process goes back to sleep state.
1203 			 */
1204 			if (action == SIG_DFL)
1205 				SIGDELSET(p->p_siglist, sig);
1206 			if (action == SIG_CATCH)
1207 				goto runfast;
1208 			mtx_enter(&sched_lock, MTX_SPIN);
1209 			if (p->p_wchan == 0)
1210 				goto run;
1211 			p->p_stat = SSLEEP;
1212 			mtx_exit(&sched_lock, MTX_SPIN);
1213 			goto out;
1214 		}
1215 
1216 		if (prop & SA_STOP) {
1217 			/*
1218 			 * Already stopped, don't need to stop again.
1219 			 * (If we did the shell could get confused.)
1220 			 */
1221 			SIGDELSET(p->p_siglist, sig);
1222 			goto out;
1223 		}
1224 
1225 		/*
1226 		 * If process is sleeping interruptibly, then simulate a
1227 		 * wakeup so that when it is continued, it will be made
1228 		 * runnable and can look at the signal.  But don't make
1229 		 * the process runnable, leave it stopped.
1230 		 */
1231 		mtx_enter(&sched_lock, MTX_SPIN);
1232 		if (p->p_wchan && p->p_flag & P_SINTR)
1233 			unsleep(p);
1234 		mtx_exit(&sched_lock, MTX_SPIN);
1235 		goto out;
1236 
1237 	default:
1238 		/*
1239 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1240 		 * other than kicking ourselves if we are running.
1241 		 * It will either never be noticed, or noticed very soon.
1242 		 */
1243 		if (p == curproc) {
1244 			mtx_exit(&sched_lock, MTX_SPIN);
1245 			signotify(p);
1246 		}
1247 #ifdef SMP
1248 		else if (p->p_stat == SRUN) {
1249 			mtx_exit(&sched_lock, MTX_SPIN);
1250 			forward_signal(p);
1251 		}
1252 #endif
1253 		else
1254 			mtx_exit(&sched_lock, MTX_SPIN);
1255 		goto out;
1256 	}
1257 	/*NOTREACHED*/
1258 
1259 runfast:
1260 	/*
1261 	 * Raise priority to at least PUSER.
1262 	 */
1263 	mtx_enter(&sched_lock, MTX_SPIN);
1264 	if (p->p_priority > PUSER)
1265 		p->p_priority = PUSER;
1266 run:
1267 	/* If we jump here, sched_lock has to be owned. */
1268 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1269 	setrunnable(p);
1270 	mtx_exit(&sched_lock, MTX_SPIN);
1271 out:
1272 	/* If we jump here, sched_lock should not be owned. */
1273 	mtx_assert(&sched_lock, MA_NOTOWNED);
1274 	splx(s);
1275 }
1276 
1277 /*
1278  * If the current process has received a signal (should be caught or cause
1279  * termination, should interrupt current syscall), return the signal number.
1280  * Stop signals with default action are processed immediately, then cleared;
1281  * they aren't returned.  This is checked after each entry to the system for
1282  * a syscall or trap (though this can usually be done without calling issignal
1283  * by checking the pending signal masks in the CURSIG macro.) The normal call
1284  * sequence is
1285  *
1286  *	while (sig = CURSIG(curproc))
1287  *		postsig(sig);
1288  */
1289 int
1290 issignal(p)
1291 	register struct proc *p;
1292 {
1293 	sigset_t mask;
1294 	register int sig, prop;
1295 
1296 	for (;;) {
1297 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1298 
1299 		mask = p->p_siglist;
1300 		SIGSETNAND(mask, p->p_sigmask);
1301 		if (p->p_flag & P_PPWAIT)
1302 			SIG_STOPSIGMASK(mask);
1303 		if (!SIGNOTEMPTY(mask))	 	/* no signal to send */
1304 			return (0);
1305 		sig = sig_ffs(&mask);
1306 		prop = sigprop(sig);
1307 
1308 		STOPEVENT(p, S_SIG, sig);
1309 
1310 		/*
1311 		 * We should see pending but ignored signals
1312 		 * only if P_TRACED was on when they were posted.
1313 		 */
1314 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1315 			SIGDELSET(p->p_siglist, sig);
1316 			continue;
1317 		}
1318 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1319 			/*
1320 			 * If traced, always stop, and stay
1321 			 * stopped until released by the parent.
1322 			 */
1323 			p->p_xstat = sig;
1324 			PROCTREE_LOCK(PT_SHARED);
1325 			psignal(p->p_pptr, SIGCHLD);
1326 			do {
1327 				stop(p);
1328 				PROCTREE_LOCK(PT_RELEASE);
1329 				mtx_enter(&sched_lock, MTX_SPIN);
1330 				DROP_GIANT_NOSWITCH();
1331 				mi_switch();
1332 				mtx_exit(&sched_lock, MTX_SPIN);
1333 				PICKUP_GIANT();
1334 				PROCTREE_LOCK(PT_SHARED);
1335 			} while (!trace_req(p)
1336 				 && p->p_flag & P_TRACED);
1337 			PROCTREE_LOCK(PT_RELEASE);
1338 
1339 			/*
1340 			 * If the traced bit got turned off, go back up
1341 			 * to the top to rescan signals.  This ensures
1342 			 * that p_sig* and ps_sigact are consistent.
1343 			 */
1344 			if ((p->p_flag & P_TRACED) == 0)
1345 				continue;
1346 
1347 			/*
1348 			 * If parent wants us to take the signal,
1349 			 * then it will leave it in p->p_xstat;
1350 			 * otherwise we just look for signals again.
1351 			 */
1352 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
1353 			sig = p->p_xstat;
1354 			if (sig == 0)
1355 				continue;
1356 
1357 			/*
1358 			 * Put the new signal into p_siglist.  If the
1359 			 * signal is being masked, look for other signals.
1360 			 */
1361 			SIGADDSET(p->p_siglist, sig);
1362 			if (SIGISMEMBER(p->p_sigmask, sig))
1363 				continue;
1364 		}
1365 
1366 		/*
1367 		 * Decide whether the signal should be returned.
1368 		 * Return the signal's number, or fall through
1369 		 * to clear it from the pending mask.
1370 		 */
1371 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1372 
1373 		case (int)SIG_DFL:
1374 			/*
1375 			 * Don't take default actions on system processes.
1376 			 */
1377 			if (p->p_pid <= 1) {
1378 #ifdef DIAGNOSTIC
1379 				/*
1380 				 * Are you sure you want to ignore SIGSEGV
1381 				 * in init? XXX
1382 				 */
1383 				printf("Process (pid %lu) got signal %d\n",
1384 					(u_long)p->p_pid, sig);
1385 #endif
1386 				break;		/* == ignore */
1387 			}
1388 			/*
1389 			 * If there is a pending stop signal to process
1390 			 * with default action, stop here,
1391 			 * then clear the signal.  However,
1392 			 * if process is member of an orphaned
1393 			 * process group, ignore tty stop signals.
1394 			 */
1395 			if (prop & SA_STOP) {
1396 				if (p->p_flag & P_TRACED ||
1397 		    		    (p->p_pgrp->pg_jobc == 0 &&
1398 				    prop & SA_TTYSTOP))
1399 					break;	/* == ignore */
1400 				p->p_xstat = sig;
1401 				PROCTREE_LOCK(PT_SHARED);
1402 				stop(p);
1403 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1404 					psignal(p->p_pptr, SIGCHLD);
1405 				PROCTREE_LOCK(PT_RELEASE);
1406 				mtx_enter(&sched_lock, MTX_SPIN);
1407 				DROP_GIANT_NOSWITCH();
1408 				mi_switch();
1409 				mtx_exit(&sched_lock, MTX_SPIN);
1410 				PICKUP_GIANT();
1411 				break;
1412 			} else if (prop & SA_IGNORE) {
1413 				/*
1414 				 * Except for SIGCONT, shouldn't get here.
1415 				 * Default action is to ignore; drop it.
1416 				 */
1417 				break;		/* == ignore */
1418 			} else
1419 				return (sig);
1420 			/*NOTREACHED*/
1421 
1422 		case (int)SIG_IGN:
1423 			/*
1424 			 * Masking above should prevent us ever trying
1425 			 * to take action on an ignored signal other
1426 			 * than SIGCONT, unless process is traced.
1427 			 */
1428 			if ((prop & SA_CONT) == 0 &&
1429 			    (p->p_flag & P_TRACED) == 0)
1430 				printf("issignal\n");
1431 			break;		/* == ignore */
1432 
1433 		default:
1434 			/*
1435 			 * This signal has an action, let
1436 			 * postsig() process it.
1437 			 */
1438 			return (sig);
1439 		}
1440 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1441 	}
1442 	/* NOTREACHED */
1443 }
1444 
1445 /*
1446  * Put the argument process into the stopped state and notify the parent
1447  * via wakeup.  Signals are handled elsewhere.  The process must not be
1448  * on the run queue.  Must be called with at least a shared hold of the
1449  * proctree lock.
1450  */
1451 void
1452 stop(p)
1453 	register struct proc *p;
1454 {
1455 
1456 	PROCTREE_ASSERT(PT_SHARED);
1457 	mtx_enter(&sched_lock, MTX_SPIN);
1458 	p->p_stat = SSTOP;
1459 	p->p_flag &= ~P_WAITED;
1460 	wakeup((caddr_t)p->p_pptr);
1461 	mtx_exit(&sched_lock, MTX_SPIN);
1462 }
1463 
1464 /*
1465  * Take the action for the specified signal
1466  * from the current set of pending signals.
1467  */
1468 void
1469 postsig(sig)
1470 	register int sig;
1471 {
1472 	register struct proc *p = curproc;
1473 	struct sigacts *ps = p->p_sigacts;
1474 	sig_t action;
1475 	sigset_t returnmask;
1476 	int code;
1477 
1478 	KASSERT(sig != 0, ("postsig"));
1479 
1480 	SIGDELSET(p->p_siglist, sig);
1481 	action = ps->ps_sigact[_SIG_IDX(sig)];
1482 #ifdef KTRACE
1483 	if (KTRPOINT(p, KTR_PSIG))
1484 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1485 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1486 #endif
1487 	STOPEVENT(p, S_SIG, sig);
1488 
1489 	if (action == SIG_DFL) {
1490 		/*
1491 		 * Default action, where the default is to kill
1492 		 * the process.  (Other cases were ignored above.)
1493 		 */
1494 		sigexit(p, sig);
1495 		/* NOTREACHED */
1496 	} else {
1497 		/*
1498 		 * If we get here, the signal must be caught.
1499 		 */
1500 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1501 		    ("postsig action"));
1502 		/*
1503 		 * Set the new mask value and also defer further
1504 		 * occurrences of this signal.
1505 		 *
1506 		 * Special case: user has done a sigsuspend.  Here the
1507 		 * current mask is not of interest, but rather the
1508 		 * mask from before the sigsuspend is what we want
1509 		 * restored after the signal processing is completed.
1510 		 */
1511 		(void) splhigh();
1512 		if (p->p_flag & P_OLDMASK) {
1513 			returnmask = p->p_oldsigmask;
1514 			p->p_flag &= ~P_OLDMASK;
1515 		} else
1516 			returnmask = p->p_sigmask;
1517 
1518 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1519 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
1520 			SIGADDSET(p->p_sigmask, sig);
1521 
1522 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1523 			/*
1524 			 * See do_sigaction() for origin of this code.
1525 			 */
1526 			SIGDELSET(p->p_sigcatch, sig);
1527 			if (sig != SIGCONT &&
1528 			    sigprop(sig) & SA_IGNORE)
1529 				SIGADDSET(p->p_sigignore, sig);
1530 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1531 		}
1532 		(void) spl0();
1533 		p->p_stats->p_ru.ru_nsignals++;
1534 		if (p->p_sig != sig) {
1535 			code = 0;
1536 		} else {
1537 			code = p->p_code;
1538 			p->p_code = 0;
1539 			p->p_sig = 0;
1540 		}
1541 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1542 	}
1543 }
1544 
1545 /*
1546  * Kill the current process for stated reason.
1547  */
1548 void
1549 killproc(p, why)
1550 	struct proc *p;
1551 	char *why;
1552 {
1553 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
1554 		p, p->p_pid, p->p_comm);
1555 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1556 		p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1557 	psignal(p, SIGKILL);
1558 }
1559 
1560 /*
1561  * Force the current process to exit with the specified signal, dumping core
1562  * if appropriate.  We bypass the normal tests for masked and caught signals,
1563  * allowing unrecoverable failures to terminate the process without changing
1564  * signal state.  Mark the accounting record with the signal termination.
1565  * If dumping core, save the signal number for the debugger.  Calls exit and
1566  * does not return.
1567  */
1568 void
1569 sigexit(p, sig)
1570 	register struct proc *p;
1571 	int sig;
1572 {
1573 
1574 	p->p_acflag |= AXSIG;
1575 	if (sigprop(sig) & SA_CORE) {
1576 		p->p_sig = sig;
1577 		/*
1578 		 * Log signals which would cause core dumps
1579 		 * (Log as LOG_INFO to appease those who don't want
1580 		 * these messages.)
1581 		 * XXX : Todo, as well as euid, write out ruid too
1582 		 */
1583 		if (coredump(p) == 0)
1584 			sig |= WCOREFLAG;
1585 		if (kern_logsigexit)
1586 			log(LOG_INFO,
1587 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
1588 			    p->p_pid, p->p_comm,
1589 			    p->p_cred && p->p_ucred ? p->p_ucred->cr_uid : -1,
1590 			    sig &~ WCOREFLAG,
1591 			    sig & WCOREFLAG ? " (core dumped)" : "");
1592 	}
1593 	exit1(p, W_EXITCODE(0, sig));
1594 	/* NOTREACHED */
1595 }
1596 
1597 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1598 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1599 	      sizeof(corefilename), "process corefile name format string");
1600 
1601 /*
1602  * expand_name(name, uid, pid)
1603  * Expand the name described in corefilename, using name, uid, and pid.
1604  * corefilename is a printf-like string, with three format specifiers:
1605  *	%N	name of process ("name")
1606  *	%P	process id (pid)
1607  *	%U	user id (uid)
1608  * For example, "%N.core" is the default; they can be disabled completely
1609  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1610  * This is controlled by the sysctl variable kern.corefile (see above).
1611  */
1612 
1613 static char *
1614 expand_name(name, uid, pid)
1615 const char *name; uid_t uid; pid_t pid; {
1616 	char *temp;
1617 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1618 	int i, n;
1619 	char *format = corefilename;
1620 	size_t namelen;
1621 
1622 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1623 	if (temp == NULL)
1624 		return NULL;
1625 	namelen = strlen(name);
1626 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1627 		int l;
1628 		switch (format[i]) {
1629 		case '%':	/* Format character */
1630 			i++;
1631 			switch (format[i]) {
1632 			case '%':
1633 				temp[n++] = '%';
1634 				break;
1635 			case 'N':	/* process name */
1636 				if ((n + namelen) > MAXPATHLEN) {
1637 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1638 					    pid, name, uid, temp, name);
1639 					free(temp, M_TEMP);
1640 					return NULL;
1641 				}
1642 				memcpy(temp+n, name, namelen);
1643 				n += namelen;
1644 				break;
1645 			case 'P':	/* process id */
1646 				l = sprintf(buf, "%u", pid);
1647 				if ((n + l) > MAXPATHLEN) {
1648 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1649 					    pid, name, uid, temp, name);
1650 					free(temp, M_TEMP);
1651 					return NULL;
1652 				}
1653 				memcpy(temp+n, buf, l);
1654 				n += l;
1655 				break;
1656 			case 'U':	/* user id */
1657 				l = sprintf(buf, "%u", uid);
1658 				if ((n + l) > MAXPATHLEN) {
1659 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1660 					    pid, name, uid, temp, name);
1661 					free(temp, M_TEMP);
1662 					return NULL;
1663 				}
1664 				memcpy(temp+n, buf, l);
1665 				n += l;
1666 				break;
1667 			default:
1668 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1669 			}
1670 			break;
1671 		default:
1672 			temp[n++] = format[i];
1673 		}
1674 	}
1675 	temp[n] = '\0';
1676 	return temp;
1677 }
1678 
1679 /*
1680  * Dump a process' core.  The main routine does some
1681  * policy checking, and creates the name of the coredump;
1682  * then it passes on a vnode and a size limit to the process-specific
1683  * coredump routine if there is one; if there _is not_ one, it returns
1684  * ENOSYS; otherwise it returns the error from the process-specific routine.
1685  */
1686 
1687 static int
1688 coredump(p)
1689 	register struct proc *p;
1690 {
1691 	register struct vnode *vp;
1692 	register struct ucred *cred = p->p_ucred;
1693 	struct nameidata nd;
1694 	struct vattr vattr;
1695 	int error, error1, flags;
1696 	struct mount *mp;
1697 	char *name;			/* name of corefile */
1698 	off_t limit;
1699 
1700 	STOPEVENT(p, S_CORE, 0);
1701 
1702 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1703 		return (EFAULT);
1704 
1705 	/*
1706 	 * Note that the bulk of limit checking is done after
1707 	 * the corefile is created.  The exception is if the limit
1708 	 * for corefiles is 0, in which case we don't bother
1709 	 * creating the corefile at all.  This layout means that
1710 	 * a corefile is truncated instead of not being created,
1711 	 * if it is larger than the limit.
1712 	 */
1713 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1714 	if (limit == 0)
1715 		return 0;
1716 
1717 restart:
1718 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1719 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1720 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
1721 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1722 	free(name, M_TEMP);
1723 	if (error)
1724 		return (error);
1725 	NDFREE(&nd, NDF_ONLY_PNBUF);
1726 	vp = nd.ni_vp;
1727 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1728 		VOP_UNLOCK(vp, 0, p);
1729 		if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
1730 			return (error);
1731 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1732 			return (error);
1733 		goto restart;
1734 	}
1735 
1736 	/* Don't dump to non-regular files or files with links. */
1737 	if (vp->v_type != VREG ||
1738 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1739 		error = EFAULT;
1740 		goto out;
1741 	}
1742 	VATTR_NULL(&vattr);
1743 	vattr.va_size = 0;
1744 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1745 	VOP_SETATTR(vp, &vattr, cred, p);
1746 	p->p_acflag |= ACORE;
1747 
1748 	error = p->p_sysent->sv_coredump ?
1749 	  p->p_sysent->sv_coredump(p, vp, limit) :
1750 	  ENOSYS;
1751 
1752 out:
1753 	VOP_UNLOCK(vp, 0, p);
1754 	vn_finished_write(mp);
1755 	error1 = vn_close(vp, FWRITE, cred, p);
1756 	if (error == 0)
1757 		error = error1;
1758 	return (error);
1759 }
1760 
1761 /*
1762  * Nonexistent system call-- signal process (may want to handle it).
1763  * Flag error in case process won't see signal immediately (blocked or ignored).
1764  */
1765 #ifndef _SYS_SYSPROTO_H_
1766 struct nosys_args {
1767 	int	dummy;
1768 };
1769 #endif
1770 /* ARGSUSED */
1771 int
1772 nosys(p, args)
1773 	struct proc *p;
1774 	struct nosys_args *args;
1775 {
1776 
1777 	psignal(p, SIGSYS);
1778 	return (EINVAL);
1779 }
1780 
1781 /*
1782  * Send a signal to a SIGIO or SIGURG to a process or process group using
1783  * stored credentials rather than those of the current process.
1784  */
1785 void
1786 pgsigio(sigio, sig, checkctty)
1787 	struct sigio *sigio;
1788 	int sig, checkctty;
1789 {
1790 	if (sigio == NULL)
1791 		return;
1792 
1793 	if (sigio->sio_pgid > 0) {
1794 		if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1795 		             sigio->sio_proc))
1796 			psignal(sigio->sio_proc, sig);
1797 	} else if (sigio->sio_pgid < 0) {
1798 		struct proc *p;
1799 
1800 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1801 			if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1802 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1803 				psignal(p, sig);
1804 	}
1805 }
1806 
1807 static int
1808 filt_sigattach(struct knote *kn)
1809 {
1810 	struct proc *p = curproc;
1811 
1812 	kn->kn_ptr.p_proc = p;
1813 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1814 
1815 	/* XXX lock the proc here while adding to the list? */
1816 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1817 
1818 	return (0);
1819 }
1820 
1821 static void
1822 filt_sigdetach(struct knote *kn)
1823 {
1824 	struct proc *p = kn->kn_ptr.p_proc;
1825 
1826 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1827 }
1828 
1829 /*
1830  * signal knotes are shared with proc knotes, so we apply a mask to
1831  * the hint in order to differentiate them from process hints.  This
1832  * could be avoided by using a signal-specific knote list, but probably
1833  * isn't worth the trouble.
1834  */
1835 static int
1836 filt_signal(struct knote *kn, long hint)
1837 {
1838 
1839 	if (hint & NOTE_SIGNAL) {
1840 		hint &= ~NOTE_SIGNAL;
1841 
1842 		if (kn->kn_id == hint)
1843 			kn->kn_data++;
1844 	}
1845 	return (kn->kn_data != 0);
1846 }
1847