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