xref: /freebsd/sys/kern/kern_sig.c (revision dce6e6518b85561495cff38a3074a69d29d58a55)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_compat.h"
45 #include "opt_ktrace.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/signalvar.h>
50 #include <sys/vnode.h>
51 #include <sys/acct.h>
52 #include <sys/condvar.h>
53 #include <sys/event.h>
54 #include <sys/fcntl.h>
55 #include <sys/kernel.h>
56 #include <sys/kse.h>
57 #include <sys/ktr.h>
58 #include <sys/ktrace.h>
59 #include <sys/lock.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
63 #include <sys/proc.h>
64 #include <sys/pioctl.h>
65 #include <sys/resourcevar.h>
66 #include <sys/smp.h>
67 #include <sys/stat.h>
68 #include <sys/sx.h>
69 #include <sys/syscallsubr.h>
70 #include <sys/sysctl.h>
71 #include <sys/sysent.h>
72 #include <sys/syslog.h>
73 #include <sys/sysproto.h>
74 #include <sys/unistd.h>
75 #include <sys/wait.h>
76 
77 #include <machine/cpu.h>
78 
79 #if defined (__alpha__) && !defined(COMPAT_43)
80 #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
81 #endif
82 
83 #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
84 
85 static int	coredump(struct thread *);
86 static char	*expand_name(const char *, uid_t, pid_t);
87 static int	killpg1(struct thread *td, int sig, int pgid, int all);
88 static int	issignal(struct thread *p);
89 static int	sigprop(int sig);
90 static void	stop(struct proc *);
91 static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
92 static int	filt_sigattach(struct knote *kn);
93 static void	filt_sigdetach(struct knote *kn);
94 static int	filt_signal(struct knote *kn, long hint);
95 static struct thread *sigtd(struct proc *p, int sig, int prop);
96 static int	kern_sigtimedwait(struct thread *td, sigset_t set,
97 				siginfo_t *info, struct timespec *timeout);
98 static void	do_tdsignal(struct thread *td, int sig, sigtarget_t target);
99 
100 struct filterops sig_filtops =
101 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
102 
103 static int	kern_logsigexit = 1;
104 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
105     &kern_logsigexit, 0,
106     "Log processes quitting on abnormal signals to syslog(3)");
107 
108 /*
109  * Policy -- Can ucred cr1 send SIGIO to process cr2?
110  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
111  * in the right situations.
112  */
113 #define CANSIGIO(cr1, cr2) \
114 	((cr1)->cr_uid == 0 || \
115 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
116 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
117 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
118 	    (cr1)->cr_uid == (cr2)->cr_uid)
119 
120 int sugid_coredump;
121 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
122     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
123 
124 static int	do_coredump = 1;
125 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
126 	&do_coredump, 0, "Enable/Disable coredumps");
127 
128 /*
129  * Signal properties and actions.
130  * The array below categorizes the signals and their default actions
131  * according to the following properties:
132  */
133 #define	SA_KILL		0x01		/* terminates process by default */
134 #define	SA_CORE		0x02		/* ditto and coredumps */
135 #define	SA_STOP		0x04		/* suspend process */
136 #define	SA_TTYSTOP	0x08		/* ditto, from tty */
137 #define	SA_IGNORE	0x10		/* ignore by default */
138 #define	SA_CONT		0x20		/* continue if suspended */
139 #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
140 #define	SA_PROC		0x80		/* deliverable to any thread */
141 
142 static int sigproptbl[NSIG] = {
143         SA_KILL|SA_PROC,		/* SIGHUP */
144         SA_KILL|SA_PROC,		/* SIGINT */
145         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
146         SA_KILL|SA_CORE,		/* SIGILL */
147         SA_KILL|SA_CORE,		/* SIGTRAP */
148         SA_KILL|SA_CORE,		/* SIGABRT */
149         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
150         SA_KILL|SA_CORE,		/* SIGFPE */
151         SA_KILL|SA_PROC,		/* SIGKILL */
152         SA_KILL|SA_CORE,		/* SIGBUS */
153         SA_KILL|SA_CORE,		/* SIGSEGV */
154         SA_KILL|SA_CORE,		/* SIGSYS */
155         SA_KILL|SA_PROC,		/* SIGPIPE */
156         SA_KILL|SA_PROC,		/* SIGALRM */
157         SA_KILL|SA_PROC,		/* SIGTERM */
158         SA_IGNORE|SA_PROC,		/* SIGURG */
159         SA_STOP|SA_PROC,		/* SIGSTOP */
160         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
161         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
162         SA_IGNORE|SA_PROC,		/* SIGCHLD */
163         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
164         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
165         SA_IGNORE|SA_PROC,		/* SIGIO */
166         SA_KILL,			/* SIGXCPU */
167         SA_KILL,			/* SIGXFSZ */
168         SA_KILL|SA_PROC,		/* SIGVTALRM */
169         SA_KILL|SA_PROC,		/* SIGPROF */
170         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
171         SA_IGNORE|SA_PROC,		/* SIGINFO */
172         SA_KILL|SA_PROC,		/* SIGUSR1 */
173         SA_KILL|SA_PROC,		/* SIGUSR2 */
174 };
175 
176 /*
177  * Determine signal that should be delivered to process p, the current
178  * process, 0 if none.  If there is a pending stop signal with default
179  * action, the process stops in issignal().
180  * XXXKSE   the check for a pending stop is not done under KSE
181  *
182  * MP SAFE.
183  */
184 int
185 cursig(struct thread *td)
186 {
187 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
188 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
189 	mtx_assert(&sched_lock, MA_NOTOWNED);
190 	return (SIGPENDING(td) ? issignal(td) : 0);
191 }
192 
193 /*
194  * Arrange for ast() to handle unmasked pending signals on return to user
195  * mode.  This must be called whenever a signal is added to td_siglist or
196  * unmasked in td_sigmask.
197  */
198 void
199 signotify(struct thread *td)
200 {
201 	struct proc *p;
202 	sigset_t set, saved;
203 
204 	p = td->td_proc;
205 
206 	PROC_LOCK_ASSERT(p, MA_OWNED);
207 
208 	/*
209 	 * If our mask changed we may have to move signal that were
210 	 * previously masked by all threads to our siglist.
211 	 */
212 	set = p->p_siglist;
213 	if (p->p_flag & P_SA)
214 		saved = p->p_siglist;
215 	SIGSETNAND(set, td->td_sigmask);
216 	SIGSETNAND(p->p_siglist, set);
217 	SIGSETOR(td->td_siglist, set);
218 
219 	if (SIGPENDING(td)) {
220 		mtx_lock_spin(&sched_lock);
221 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
222 		mtx_unlock_spin(&sched_lock);
223 	}
224 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
225 		if (SIGSETEQ(saved, p->p_siglist))
226 			return;
227 		else {
228 			/* pending set changed */
229 			p->p_flag |= P_SIGEVENT;
230 			wakeup(&p->p_siglist);
231 		}
232 	}
233 }
234 
235 int
236 sigonstack(size_t sp)
237 {
238 	struct proc *p = curthread->td_proc;
239 
240 	PROC_LOCK_ASSERT(p, MA_OWNED);
241 	return ((p->p_flag & P_ALTSTACK) ?
242 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
243 	    ((p->p_sigstk.ss_size == 0) ? (p->p_sigstk.ss_flags & SS_ONSTACK) :
244 		((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size))
245 #else
246 	    ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size)
247 #endif
248 	    : 0);
249 }
250 
251 static __inline int
252 sigprop(int sig)
253 {
254 
255 	if (sig > 0 && sig < NSIG)
256 		return (sigproptbl[_SIG_IDX(sig)]);
257 	return (0);
258 }
259 
260 int
261 sig_ffs(sigset_t *set)
262 {
263 	int i;
264 
265 	for (i = 0; i < _SIG_WORDS; i++)
266 		if (set->__bits[i])
267 			return (ffs(set->__bits[i]) + (i * 32));
268 	return (0);
269 }
270 
271 /*
272  * kern_sigaction
273  * sigaction
274  * freebsd4_sigaction
275  * osigaction
276  *
277  * MPSAFE
278  */
279 int
280 kern_sigaction(td, sig, act, oact, flags)
281 	struct thread *td;
282 	register int sig;
283 	struct sigaction *act, *oact;
284 	int flags;
285 {
286 	struct sigacts *ps;
287 	struct thread *td0;
288 	struct proc *p = td->td_proc;
289 
290 	if (!_SIG_VALID(sig))
291 		return (EINVAL);
292 
293 	PROC_LOCK(p);
294 	ps = p->p_sigacts;
295 	mtx_lock(&ps->ps_mtx);
296 	if (oact) {
297 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
298 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
299 		oact->sa_flags = 0;
300 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
301 			oact->sa_flags |= SA_ONSTACK;
302 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
303 			oact->sa_flags |= SA_RESTART;
304 		if (SIGISMEMBER(ps->ps_sigreset, sig))
305 			oact->sa_flags |= SA_RESETHAND;
306 		if (SIGISMEMBER(ps->ps_signodefer, sig))
307 			oact->sa_flags |= SA_NODEFER;
308 		if (SIGISMEMBER(ps->ps_siginfo, sig))
309 			oact->sa_flags |= SA_SIGINFO;
310 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
311 			oact->sa_flags |= SA_NOCLDSTOP;
312 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
313 			oact->sa_flags |= SA_NOCLDWAIT;
314 	}
315 	if (act) {
316 		if ((sig == SIGKILL || sig == SIGSTOP) &&
317 		    act->sa_handler != SIG_DFL) {
318 			mtx_unlock(&ps->ps_mtx);
319 			PROC_UNLOCK(p);
320 			return (EINVAL);
321 		}
322 
323 		/*
324 		 * Change setting atomically.
325 		 */
326 
327 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
328 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
329 		if (act->sa_flags & SA_SIGINFO) {
330 			ps->ps_sigact[_SIG_IDX(sig)] =
331 			    (__sighandler_t *)act->sa_sigaction;
332 			SIGADDSET(ps->ps_siginfo, sig);
333 		} else {
334 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
335 			SIGDELSET(ps->ps_siginfo, sig);
336 		}
337 		if (!(act->sa_flags & SA_RESTART))
338 			SIGADDSET(ps->ps_sigintr, sig);
339 		else
340 			SIGDELSET(ps->ps_sigintr, sig);
341 		if (act->sa_flags & SA_ONSTACK)
342 			SIGADDSET(ps->ps_sigonstack, sig);
343 		else
344 			SIGDELSET(ps->ps_sigonstack, sig);
345 		if (act->sa_flags & SA_RESETHAND)
346 			SIGADDSET(ps->ps_sigreset, sig);
347 		else
348 			SIGDELSET(ps->ps_sigreset, sig);
349 		if (act->sa_flags & SA_NODEFER)
350 			SIGADDSET(ps->ps_signodefer, sig);
351 		else
352 			SIGDELSET(ps->ps_signodefer, sig);
353 #ifdef COMPAT_SUNOS
354 		if (act->sa_flags & SA_USERTRAMP)
355 			SIGADDSET(ps->ps_usertramp, sig);
356 		else
357 			SIGDELSET(ps->ps_usertramp, sig);
358 #endif
359 		if (sig == SIGCHLD) {
360 			if (act->sa_flags & SA_NOCLDSTOP)
361 				ps->ps_flag |= PS_NOCLDSTOP;
362 			else
363 				ps->ps_flag &= ~PS_NOCLDSTOP;
364 			if (act->sa_flags & SA_NOCLDWAIT) {
365 				/*
366 				 * Paranoia: since SA_NOCLDWAIT is implemented
367 				 * by reparenting the dying child to PID 1 (and
368 				 * trust it to reap the zombie), PID 1 itself
369 				 * is forbidden to set SA_NOCLDWAIT.
370 				 */
371 				if (p->p_pid == 1)
372 					ps->ps_flag &= ~PS_NOCLDWAIT;
373 				else
374 					ps->ps_flag |= PS_NOCLDWAIT;
375 			} else
376 				ps->ps_flag &= ~PS_NOCLDWAIT;
377 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
378 				ps->ps_flag |= PS_CLDSIGIGN;
379 			else
380 				ps->ps_flag &= ~PS_CLDSIGIGN;
381 		}
382 		/*
383 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
384 		 * and for signals set to SIG_DFL where the default is to
385 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
386 		 * have to restart the process.
387 		 */
388 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
389 		    (sigprop(sig) & SA_IGNORE &&
390 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
391 			if ((p->p_flag & P_SA) &&
392 			     SIGISMEMBER(p->p_siglist, sig)) {
393 				p->p_flag |= P_SIGEVENT;
394 				wakeup(&p->p_siglist);
395 			}
396 			/* never to be seen again */
397 			SIGDELSET(p->p_siglist, sig);
398 			FOREACH_THREAD_IN_PROC(p, td0)
399 				SIGDELSET(td0->td_siglist, sig);
400 			if (sig != SIGCONT)
401 				/* easier in psignal */
402 				SIGADDSET(ps->ps_sigignore, sig);
403 			SIGDELSET(ps->ps_sigcatch, sig);
404 		} else {
405 			SIGDELSET(ps->ps_sigignore, sig);
406 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
407 				SIGDELSET(ps->ps_sigcatch, sig);
408 			else
409 				SIGADDSET(ps->ps_sigcatch, sig);
410 		}
411 #ifdef COMPAT_FREEBSD4
412 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
413 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
414 		    (flags & KSA_FREEBSD4) == 0)
415 			SIGDELSET(ps->ps_freebsd4, sig);
416 		else
417 			SIGADDSET(ps->ps_freebsd4, sig);
418 #endif
419 #ifdef COMPAT_43
420 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
421 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
422 		    (flags & KSA_OSIGSET) == 0)
423 			SIGDELSET(ps->ps_osigset, sig);
424 		else
425 			SIGADDSET(ps->ps_osigset, sig);
426 #endif
427 	}
428 	mtx_unlock(&ps->ps_mtx);
429 	PROC_UNLOCK(p);
430 	return (0);
431 }
432 
433 #ifndef _SYS_SYSPROTO_H_
434 struct sigaction_args {
435 	int	sig;
436 	struct	sigaction *act;
437 	struct	sigaction *oact;
438 };
439 #endif
440 /*
441  * MPSAFE
442  */
443 int
444 sigaction(td, uap)
445 	struct thread *td;
446 	register struct sigaction_args *uap;
447 {
448 	struct sigaction act, oact;
449 	register struct sigaction *actp, *oactp;
450 	int error;
451 
452 	actp = (uap->act != NULL) ? &act : NULL;
453 	oactp = (uap->oact != NULL) ? &oact : NULL;
454 	if (actp) {
455 		error = copyin(uap->act, actp, sizeof(act));
456 		if (error)
457 			return (error);
458 	}
459 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
460 	if (oactp && !error)
461 		error = copyout(oactp, uap->oact, sizeof(oact));
462 	return (error);
463 }
464 
465 #ifdef COMPAT_FREEBSD4
466 #ifndef _SYS_SYSPROTO_H_
467 struct freebsd4_sigaction_args {
468 	int	sig;
469 	struct	sigaction *act;
470 	struct	sigaction *oact;
471 };
472 #endif
473 /*
474  * MPSAFE
475  */
476 int
477 freebsd4_sigaction(td, uap)
478 	struct thread *td;
479 	register struct freebsd4_sigaction_args *uap;
480 {
481 	struct sigaction act, oact;
482 	register struct sigaction *actp, *oactp;
483 	int error;
484 
485 
486 	actp = (uap->act != NULL) ? &act : NULL;
487 	oactp = (uap->oact != NULL) ? &oact : NULL;
488 	if (actp) {
489 		error = copyin(uap->act, actp, sizeof(act));
490 		if (error)
491 			return (error);
492 	}
493 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
494 	if (oactp && !error)
495 		error = copyout(oactp, uap->oact, sizeof(oact));
496 	return (error);
497 }
498 #endif	/* COMAPT_FREEBSD4 */
499 
500 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
501 #ifndef _SYS_SYSPROTO_H_
502 struct osigaction_args {
503 	int	signum;
504 	struct	osigaction *nsa;
505 	struct	osigaction *osa;
506 };
507 #endif
508 /*
509  * MPSAFE
510  */
511 int
512 osigaction(td, uap)
513 	struct thread *td;
514 	register struct osigaction_args *uap;
515 {
516 	struct osigaction sa;
517 	struct sigaction nsa, osa;
518 	register struct sigaction *nsap, *osap;
519 	int error;
520 
521 	if (uap->signum <= 0 || uap->signum >= ONSIG)
522 		return (EINVAL);
523 
524 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
525 	osap = (uap->osa != NULL) ? &osa : NULL;
526 
527 	if (nsap) {
528 		error = copyin(uap->nsa, &sa, sizeof(sa));
529 		if (error)
530 			return (error);
531 		nsap->sa_handler = sa.sa_handler;
532 		nsap->sa_flags = sa.sa_flags;
533 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
534 	}
535 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
536 	if (osap && !error) {
537 		sa.sa_handler = osap->sa_handler;
538 		sa.sa_flags = osap->sa_flags;
539 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
540 		error = copyout(&sa, uap->osa, sizeof(sa));
541 	}
542 	return (error);
543 }
544 
545 #if !defined(__i386__) && !defined(__alpha__)
546 /* Avoid replicating the same stub everywhere */
547 int
548 osigreturn(td, uap)
549 	struct thread *td;
550 	struct osigreturn_args *uap;
551 {
552 
553 	return (nosys(td, (struct nosys_args *)uap));
554 }
555 #endif
556 #endif /* COMPAT_43 */
557 
558 /*
559  * Initialize signal state for process 0;
560  * set to ignore signals that are ignored by default.
561  */
562 void
563 siginit(p)
564 	struct proc *p;
565 {
566 	register int i;
567 	struct sigacts *ps;
568 
569 	PROC_LOCK(p);
570 	ps = p->p_sigacts;
571 	mtx_lock(&ps->ps_mtx);
572 	for (i = 1; i <= NSIG; i++)
573 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
574 			SIGADDSET(ps->ps_sigignore, i);
575 	mtx_unlock(&ps->ps_mtx);
576 	PROC_UNLOCK(p);
577 }
578 
579 /*
580  * Reset signals for an exec of the specified process.
581  */
582 void
583 execsigs(p)
584 	register struct proc *p;
585 {
586 	register struct sigacts *ps;
587 	register int sig;
588 
589 	/*
590 	 * Reset caught signals.  Held signals remain held
591 	 * through td_sigmask (unless they were caught,
592 	 * and are now ignored by default).
593 	 */
594 	PROC_LOCK_ASSERT(p, MA_OWNED);
595 	ps = p->p_sigacts;
596 	mtx_lock(&ps->ps_mtx);
597 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
598 		sig = sig_ffs(&ps->ps_sigcatch);
599 		SIGDELSET(ps->ps_sigcatch, sig);
600 		if (sigprop(sig) & SA_IGNORE) {
601 			if (sig != SIGCONT)
602 				SIGADDSET(ps->ps_sigignore, sig);
603 			SIGDELSET(p->p_siglist, sig);
604 			/*
605 			 * There is only one thread at this point.
606 			 */
607 			SIGDELSET(FIRST_THREAD_IN_PROC(p)->td_siglist, sig);
608 		}
609 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
610 	}
611 	/*
612 	 * Clear out the td's sigmask.  Normal processes use the proc sigmask.
613 	 */
614 	SIGEMPTYSET(FIRST_THREAD_IN_PROC(p)->td_sigmask);
615 	/*
616 	 * Reset stack state to the user stack.
617 	 * Clear set of signals caught on the signal stack.
618 	 */
619 	p->p_sigstk.ss_flags = SS_DISABLE;
620 	p->p_sigstk.ss_size = 0;
621 	p->p_sigstk.ss_sp = 0;
622 	p->p_flag &= ~P_ALTSTACK;
623 	/*
624 	 * Reset no zombies if child dies flag as Solaris does.
625 	 */
626 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
627 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
628 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
629 	mtx_unlock(&ps->ps_mtx);
630 }
631 
632 /*
633  * kern_sigprocmask()
634  *
635  *	Manipulate signal mask.
636  */
637 int
638 kern_sigprocmask(td, how, set, oset, old)
639 	struct thread *td;
640 	int how;
641 	sigset_t *set, *oset;
642 	int old;
643 {
644 	int error;
645 
646 	PROC_LOCK(td->td_proc);
647 	if (oset != NULL)
648 		*oset = td->td_sigmask;
649 
650 	error = 0;
651 	if (set != NULL) {
652 		switch (how) {
653 		case SIG_BLOCK:
654 			SIG_CANTMASK(*set);
655 			SIGSETOR(td->td_sigmask, *set);
656 			break;
657 		case SIG_UNBLOCK:
658 			SIGSETNAND(td->td_sigmask, *set);
659 			signotify(td);
660 			break;
661 		case SIG_SETMASK:
662 			SIG_CANTMASK(*set);
663 			if (old)
664 				SIGSETLO(td->td_sigmask, *set);
665 			else
666 				td->td_sigmask = *set;
667 			signotify(td);
668 			break;
669 		default:
670 			error = EINVAL;
671 			break;
672 		}
673 	}
674 	PROC_UNLOCK(td->td_proc);
675 	return (error);
676 }
677 
678 /*
679  * sigprocmask() - MP SAFE
680  */
681 
682 #ifndef _SYS_SYSPROTO_H_
683 struct sigprocmask_args {
684 	int	how;
685 	const sigset_t *set;
686 	sigset_t *oset;
687 };
688 #endif
689 int
690 sigprocmask(td, uap)
691 	register struct thread *td;
692 	struct sigprocmask_args *uap;
693 {
694 	sigset_t set, oset;
695 	sigset_t *setp, *osetp;
696 	int error;
697 
698 	setp = (uap->set != NULL) ? &set : NULL;
699 	osetp = (uap->oset != NULL) ? &oset : NULL;
700 	if (setp) {
701 		error = copyin(uap->set, setp, sizeof(set));
702 		if (error)
703 			return (error);
704 	}
705 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
706 	if (osetp && !error) {
707 		error = copyout(osetp, uap->oset, sizeof(oset));
708 	}
709 	return (error);
710 }
711 
712 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
713 /*
714  * osigprocmask() - MP SAFE
715  */
716 #ifndef _SYS_SYSPROTO_H_
717 struct osigprocmask_args {
718 	int	how;
719 	osigset_t mask;
720 };
721 #endif
722 int
723 osigprocmask(td, uap)
724 	register struct thread *td;
725 	struct osigprocmask_args *uap;
726 {
727 	sigset_t set, oset;
728 	int error;
729 
730 	OSIG2SIG(uap->mask, set);
731 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
732 	SIG2OSIG(oset, td->td_retval[0]);
733 	return (error);
734 }
735 #endif /* COMPAT_43 */
736 
737 #ifndef _SYS_SYSPROTO_H_
738 struct sigpending_args {
739 	sigset_t	*set;
740 };
741 #endif
742 /*
743  * MPSAFE
744  */
745 int
746 sigwait(struct thread *td, struct sigwait_args *uap)
747 {
748 	siginfo_t info;
749 	sigset_t set;
750 	int error;
751 
752 	error = copyin(uap->set, &set, sizeof(set));
753 	if (error)
754 		return (error);
755 
756 	error = kern_sigtimedwait(td, set, &info, NULL);
757 	if (error)
758 		return (error);
759 
760 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
761 	/* Repost if we got an error. */
762 	if (error && info.si_signo) {
763 		PROC_LOCK(td->td_proc);
764 		tdsignal(td, info.si_signo, SIGTARGET_TD);
765 		PROC_UNLOCK(td->td_proc);
766 	}
767 	return (error);
768 }
769 /*
770  * MPSAFE
771  */
772 int
773 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
774 {
775 	struct timespec ts;
776 	struct timespec *timeout;
777 	sigset_t set;
778 	siginfo_t info;
779 	int error;
780 
781 	if (uap->timeout) {
782 		error = copyin(uap->timeout, &ts, sizeof(ts));
783 		if (error)
784 			return (error);
785 
786 		timeout = &ts;
787 	} else
788 		timeout = NULL;
789 
790 	error = copyin(uap->set, &set, sizeof(set));
791 	if (error)
792 		return (error);
793 
794 	error = kern_sigtimedwait(td, set, &info, timeout);
795 	if (error)
796 		return (error);
797 
798 	if (uap->info)
799 		error = copyout(&info, uap->info, sizeof(info));
800 	/* Repost if we got an error. */
801 	if (error && info.si_signo) {
802 		PROC_LOCK(td->td_proc);
803 		tdsignal(td, info.si_signo, SIGTARGET_TD);
804 		PROC_UNLOCK(td->td_proc);
805 	} else {
806 		td->td_retval[0] = info.si_signo;
807 	}
808 	return (error);
809 }
810 
811 /*
812  * MPSAFE
813  */
814 int
815 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
816 {
817 	siginfo_t info;
818 	sigset_t set;
819 	int error;
820 
821 	error = copyin(uap->set, &set, sizeof(set));
822 	if (error)
823 		return (error);
824 
825 	error = kern_sigtimedwait(td, set, &info, NULL);
826 	if (error)
827 		return (error);
828 
829 	if (uap->info)
830 		error = copyout(&info, uap->info, sizeof(info));
831 	/* Repost if we got an error. */
832 	if (error && info.si_signo) {
833 		PROC_LOCK(td->td_proc);
834 		tdsignal(td, info.si_signo, SIGTARGET_TD);
835 		PROC_UNLOCK(td->td_proc);
836 	} else {
837 		td->td_retval[0] = info.si_signo;
838 	}
839 	return (error);
840 }
841 
842 static int
843 kern_sigtimedwait(struct thread *td, sigset_t set, siginfo_t *info,
844     struct timespec *timeout)
845 {
846 	register struct sigacts *ps;
847 	sigset_t oldmask;
848 	struct proc *p;
849 	int error;
850 	int sig;
851 	int hz;
852 
853 	p = td->td_proc;
854 	error = 0;
855 	sig = 0;
856 	SIG_CANTMASK(set);
857 
858 	PROC_LOCK(p);
859 	ps = p->p_sigacts;
860 	oldmask = td->td_sigmask;
861 	SIGFILLSET(td->td_sigmask);
862 	SIG_CANTMASK(td->td_sigmask);
863 	SIGSETNAND(td->td_sigmask, set);
864 	signotify(td);
865 
866 	mtx_lock(&ps->ps_mtx);
867 	sig = cursig(td);
868 	if (sig)
869 		goto out;
870 
871 	/*
872 	 * POSIX says this must be checked after looking for pending
873 	 * signals.
874 	 */
875 	if (timeout) {
876 		struct timeval tv;
877 
878 		if (timeout->tv_nsec > 1000000000) {
879 			error = EINVAL;
880 			goto out;
881 		}
882 		if (timeout->tv_sec == 0 && timeout->tv_nsec == 0)
883 			goto nosleep;
884 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
885 		hz = tvtohz(&tv);
886 	} else
887 		hz = 0;
888 
889 	mtx_unlock(&ps->ps_mtx);
890 	error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "pause", hz);
891 	mtx_lock(&ps->ps_mtx);
892 	if (error == EINTR)
893 		error = 0;
894 	else if (error)
895 		goto out;
896 nosleep:
897 	sig = cursig(td);
898 
899 out:
900 	td->td_sigmask = oldmask;
901 	if (sig) {
902 		sig_t action;
903 
904 		action = ps->ps_sigact[_SIG_IDX(sig)];
905 		mtx_unlock(&ps->ps_mtx);
906 #ifdef KTRACE
907 		if (KTRPOINT(td, KTR_PSIG))
908 			ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
909 			    &td->td_oldsigmask : &td->td_sigmask, 0);
910 #endif
911 		_STOPEVENT(p, S_SIG, sig);
912 
913 		if (action == SIG_DFL)
914 			sigexit(td, sig);
915 			/* NOTREACHED */
916 
917 		SIGDELSET(td->td_siglist, sig);
918 		info->si_signo = sig;
919 		info->si_code = 0;
920 	} else
921 		mtx_unlock(&ps->ps_mtx);
922 	PROC_UNLOCK(p);
923 	return (error);
924 }
925 
926 /*
927  * MPSAFE
928  */
929 int
930 sigpending(td, uap)
931 	struct thread *td;
932 	struct sigpending_args *uap;
933 {
934 	struct proc *p = td->td_proc;
935 	sigset_t siglist;
936 
937 	PROC_LOCK(p);
938 	siglist = p->p_siglist;
939 	SIGSETOR(siglist, td->td_siglist);
940 	PROC_UNLOCK(p);
941 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
942 }
943 
944 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
945 #ifndef _SYS_SYSPROTO_H_
946 struct osigpending_args {
947 	int	dummy;
948 };
949 #endif
950 /*
951  * MPSAFE
952  */
953 int
954 osigpending(td, uap)
955 	struct thread *td;
956 	struct osigpending_args *uap;
957 {
958 	struct proc *p = td->td_proc;
959 	sigset_t siglist;
960 
961 	PROC_LOCK(p);
962 	siglist = p->p_siglist;
963 	SIGSETOR(siglist, td->td_siglist);
964 	PROC_UNLOCK(p);
965 	SIG2OSIG(siglist, td->td_retval[0]);
966 	return (0);
967 }
968 #endif /* COMPAT_43 */
969 
970 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
971 /*
972  * Generalized interface signal handler, 4.3-compatible.
973  */
974 #ifndef _SYS_SYSPROTO_H_
975 struct osigvec_args {
976 	int	signum;
977 	struct	sigvec *nsv;
978 	struct	sigvec *osv;
979 };
980 #endif
981 /*
982  * MPSAFE
983  */
984 /* ARGSUSED */
985 int
986 osigvec(td, uap)
987 	struct thread *td;
988 	register struct osigvec_args *uap;
989 {
990 	struct sigvec vec;
991 	struct sigaction nsa, osa;
992 	register struct sigaction *nsap, *osap;
993 	int error;
994 
995 	if (uap->signum <= 0 || uap->signum >= ONSIG)
996 		return (EINVAL);
997 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
998 	osap = (uap->osv != NULL) ? &osa : NULL;
999 	if (nsap) {
1000 		error = copyin(uap->nsv, &vec, sizeof(vec));
1001 		if (error)
1002 			return (error);
1003 		nsap->sa_handler = vec.sv_handler;
1004 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1005 		nsap->sa_flags = vec.sv_flags;
1006 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1007 #ifdef COMPAT_SUNOS
1008 		nsap->sa_flags |= SA_USERTRAMP;
1009 #endif
1010 	}
1011 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1012 	if (osap && !error) {
1013 		vec.sv_handler = osap->sa_handler;
1014 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
1015 		vec.sv_flags = osap->sa_flags;
1016 		vec.sv_flags &= ~SA_NOCLDWAIT;
1017 		vec.sv_flags ^= SA_RESTART;
1018 #ifdef COMPAT_SUNOS
1019 		vec.sv_flags &= ~SA_NOCLDSTOP;
1020 #endif
1021 		error = copyout(&vec, uap->osv, sizeof(vec));
1022 	}
1023 	return (error);
1024 }
1025 
1026 #ifndef _SYS_SYSPROTO_H_
1027 struct osigblock_args {
1028 	int	mask;
1029 };
1030 #endif
1031 /*
1032  * MPSAFE
1033  */
1034 int
1035 osigblock(td, uap)
1036 	register struct thread *td;
1037 	struct osigblock_args *uap;
1038 {
1039 	struct proc *p = td->td_proc;
1040 	sigset_t set;
1041 
1042 	OSIG2SIG(uap->mask, set);
1043 	SIG_CANTMASK(set);
1044 	PROC_LOCK(p);
1045 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1046 	SIGSETOR(td->td_sigmask, set);
1047 	PROC_UNLOCK(p);
1048 	return (0);
1049 }
1050 
1051 #ifndef _SYS_SYSPROTO_H_
1052 struct osigsetmask_args {
1053 	int	mask;
1054 };
1055 #endif
1056 /*
1057  * MPSAFE
1058  */
1059 int
1060 osigsetmask(td, uap)
1061 	struct thread *td;
1062 	struct osigsetmask_args *uap;
1063 {
1064 	struct proc *p = td->td_proc;
1065 	sigset_t set;
1066 
1067 	OSIG2SIG(uap->mask, set);
1068 	SIG_CANTMASK(set);
1069 	PROC_LOCK(p);
1070 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1071 	SIGSETLO(td->td_sigmask, set);
1072 	signotify(td);
1073 	PROC_UNLOCK(p);
1074 	return (0);
1075 }
1076 #endif /* COMPAT_43 || COMPAT_SUNOS */
1077 
1078 /*
1079  * Suspend process until signal, providing mask to be set
1080  * in the meantime.  Note nonstandard calling convention:
1081  * libc stub passes mask, not pointer, to save a copyin.
1082  ***** XXXKSE this doesn't make sense under KSE.
1083  ***** Do we suspend the thread or all threads in the process?
1084  ***** How do we suspend threads running NOW on another processor?
1085  */
1086 #ifndef _SYS_SYSPROTO_H_
1087 struct sigsuspend_args {
1088 	const sigset_t *sigmask;
1089 };
1090 #endif
1091 /*
1092  * MPSAFE
1093  */
1094 /* ARGSUSED */
1095 int
1096 sigsuspend(td, uap)
1097 	struct thread *td;
1098 	struct sigsuspend_args *uap;
1099 {
1100 	sigset_t mask;
1101 	int error;
1102 
1103 	error = copyin(uap->sigmask, &mask, sizeof(mask));
1104 	if (error)
1105 		return (error);
1106 	return (kern_sigsuspend(td, mask));
1107 }
1108 
1109 int
1110 kern_sigsuspend(struct thread *td, sigset_t mask)
1111 {
1112 	struct proc *p = td->td_proc;
1113 
1114 	/*
1115 	 * When returning from sigsuspend, we want
1116 	 * the old mask to be restored after the
1117 	 * signal handler has finished.  Thus, we
1118 	 * save it here and mark the sigacts structure
1119 	 * to indicate this.
1120 	 */
1121 	PROC_LOCK(p);
1122 	td->td_oldsigmask = td->td_sigmask;
1123 	td->td_pflags |= TDP_OLDMASK;
1124 	SIG_CANTMASK(mask);
1125 	td->td_sigmask = mask;
1126 	signotify(td);
1127 	while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
1128 		/* void */;
1129 	PROC_UNLOCK(p);
1130 	/* always return EINTR rather than ERESTART... */
1131 	return (EINTR);
1132 }
1133 
1134 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
1135 #ifndef _SYS_SYSPROTO_H_
1136 struct osigsuspend_args {
1137 	osigset_t mask;
1138 };
1139 #endif
1140 /*
1141  * MPSAFE
1142  */
1143 /* ARGSUSED */
1144 int
1145 osigsuspend(td, uap)
1146 	struct thread *td;
1147 	struct osigsuspend_args *uap;
1148 {
1149 	struct proc *p = td->td_proc;
1150 	sigset_t mask;
1151 
1152 	PROC_LOCK(p);
1153 	td->td_oldsigmask = td->td_sigmask;
1154 	td->td_pflags |= TDP_OLDMASK;
1155 	OSIG2SIG(uap->mask, mask);
1156 	SIG_CANTMASK(mask);
1157 	SIGSETLO(td->td_sigmask, mask);
1158 	signotify(td);
1159 	while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1160 		/* void */;
1161 	PROC_UNLOCK(p);
1162 	/* always return EINTR rather than ERESTART... */
1163 	return (EINTR);
1164 }
1165 #endif /* COMPAT_43 */
1166 
1167 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1168 #ifndef _SYS_SYSPROTO_H_
1169 struct osigstack_args {
1170 	struct	sigstack *nss;
1171 	struct	sigstack *oss;
1172 };
1173 #endif
1174 /*
1175  * MPSAFE
1176  */
1177 /* ARGSUSED */
1178 int
1179 osigstack(td, uap)
1180 	struct thread *td;
1181 	register struct osigstack_args *uap;
1182 {
1183 	struct proc *p = td->td_proc;
1184 	struct sigstack nss, oss;
1185 	int error = 0;
1186 
1187 	if (uap->nss != NULL) {
1188 		error = copyin(uap->nss, &nss, sizeof(nss));
1189 		if (error)
1190 			return (error);
1191 	}
1192 	PROC_LOCK(p);
1193 	oss.ss_sp = p->p_sigstk.ss_sp;
1194 	oss.ss_onstack = sigonstack(cpu_getstack(td));
1195 	if (uap->nss != NULL) {
1196 		p->p_sigstk.ss_sp = nss.ss_sp;
1197 		p->p_sigstk.ss_size = 0;
1198 		p->p_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1199 		p->p_flag |= P_ALTSTACK;
1200 	}
1201 	PROC_UNLOCK(p);
1202 	if (uap->oss != NULL)
1203 		error = copyout(&oss, uap->oss, sizeof(oss));
1204 
1205 	return (error);
1206 }
1207 #endif /* COMPAT_43 || COMPAT_SUNOS */
1208 
1209 #ifndef _SYS_SYSPROTO_H_
1210 struct sigaltstack_args {
1211 	stack_t	*ss;
1212 	stack_t	*oss;
1213 };
1214 #endif
1215 /*
1216  * MPSAFE
1217  */
1218 /* ARGSUSED */
1219 int
1220 sigaltstack(td, uap)
1221 	struct thread *td;
1222 	register struct sigaltstack_args *uap;
1223 {
1224 	stack_t ss, oss;
1225 	int error;
1226 
1227 	if (uap->ss != NULL) {
1228 		error = copyin(uap->ss, &ss, sizeof(ss));
1229 		if (error)
1230 			return (error);
1231 	}
1232 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1233 	    (uap->oss != NULL) ? &oss : NULL);
1234 	if (error)
1235 		return (error);
1236 	if (uap->oss != NULL)
1237 		error = copyout(&oss, uap->oss, sizeof(stack_t));
1238 	return (error);
1239 }
1240 
1241 int
1242 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1243 {
1244 	struct proc *p = td->td_proc;
1245 	int oonstack;
1246 
1247 	PROC_LOCK(p);
1248 	oonstack = sigonstack(cpu_getstack(td));
1249 
1250 	if (oss != NULL) {
1251 		*oss = p->p_sigstk;
1252 		oss->ss_flags = (p->p_flag & P_ALTSTACK)
1253 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1254 	}
1255 
1256 	if (ss != NULL) {
1257 		if (oonstack) {
1258 			PROC_UNLOCK(p);
1259 			return (EPERM);
1260 		}
1261 		if ((ss->ss_flags & ~SS_DISABLE) != 0) {
1262 			PROC_UNLOCK(p);
1263 			return (EINVAL);
1264 		}
1265 		if (!(ss->ss_flags & SS_DISABLE)) {
1266 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1267 				PROC_UNLOCK(p);
1268 				return (ENOMEM);
1269 			}
1270 			p->p_sigstk = *ss;
1271 			p->p_flag |= P_ALTSTACK;
1272 		} else {
1273 			p->p_flag &= ~P_ALTSTACK;
1274 		}
1275 	}
1276 	PROC_UNLOCK(p);
1277 	return (0);
1278 }
1279 
1280 /*
1281  * Common code for kill process group/broadcast kill.
1282  * cp is calling process.
1283  */
1284 static int
1285 killpg1(td, sig, pgid, all)
1286 	register struct thread *td;
1287 	int sig, pgid, all;
1288 {
1289 	register struct proc *p;
1290 	struct pgrp *pgrp;
1291 	int nfound = 0;
1292 
1293 	if (all) {
1294 		/*
1295 		 * broadcast
1296 		 */
1297 		sx_slock(&allproc_lock);
1298 		LIST_FOREACH(p, &allproc, p_list) {
1299 			PROC_LOCK(p);
1300 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1301 			    p == td->td_proc) {
1302 				PROC_UNLOCK(p);
1303 				continue;
1304 			}
1305 			if (p_cansignal(td, p, sig) == 0) {
1306 				nfound++;
1307 				if (sig)
1308 					psignal(p, sig);
1309 			}
1310 			PROC_UNLOCK(p);
1311 		}
1312 		sx_sunlock(&allproc_lock);
1313 	} else {
1314 		sx_slock(&proctree_lock);
1315 		if (pgid == 0) {
1316 			/*
1317 			 * zero pgid means send to my process group.
1318 			 */
1319 			pgrp = td->td_proc->p_pgrp;
1320 			PGRP_LOCK(pgrp);
1321 		} else {
1322 			pgrp = pgfind(pgid);
1323 			if (pgrp == NULL) {
1324 				sx_sunlock(&proctree_lock);
1325 				return (ESRCH);
1326 			}
1327 		}
1328 		sx_sunlock(&proctree_lock);
1329 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1330 			PROC_LOCK(p);
1331 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1332 				PROC_UNLOCK(p);
1333 				continue;
1334 			}
1335 			if (p->p_state == PRS_ZOMBIE) {
1336 				PROC_UNLOCK(p);
1337 				continue;
1338 			}
1339 			if (p_cansignal(td, p, sig) == 0) {
1340 				nfound++;
1341 				if (sig)
1342 					psignal(p, sig);
1343 			}
1344 			PROC_UNLOCK(p);
1345 		}
1346 		PGRP_UNLOCK(pgrp);
1347 	}
1348 	return (nfound ? 0 : ESRCH);
1349 }
1350 
1351 #ifndef _SYS_SYSPROTO_H_
1352 struct kill_args {
1353 	int	pid;
1354 	int	signum;
1355 };
1356 #endif
1357 /*
1358  * MPSAFE
1359  */
1360 /* ARGSUSED */
1361 int
1362 kill(td, uap)
1363 	register struct thread *td;
1364 	register struct kill_args *uap;
1365 {
1366 	register struct proc *p;
1367 	int error;
1368 
1369 	if ((u_int)uap->signum > _SIG_MAXSIG)
1370 		return (EINVAL);
1371 
1372 	if (uap->pid > 0) {
1373 		/* kill single process */
1374 		if ((p = pfind(uap->pid)) == NULL)
1375 			return (ESRCH);
1376 		error = p_cansignal(td, p, uap->signum);
1377 		if (error == 0 && uap->signum)
1378 			psignal(p, uap->signum);
1379 		PROC_UNLOCK(p);
1380 		return (error);
1381 	}
1382 	switch (uap->pid) {
1383 	case -1:		/* broadcast signal */
1384 		return (killpg1(td, uap->signum, 0, 1));
1385 	case 0:			/* signal own process group */
1386 		return (killpg1(td, uap->signum, 0, 0));
1387 	default:		/* negative explicit process group */
1388 		return (killpg1(td, uap->signum, -uap->pid, 0));
1389 	}
1390 	/* NOTREACHED */
1391 }
1392 
1393 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1394 #ifndef _SYS_SYSPROTO_H_
1395 struct okillpg_args {
1396 	int	pgid;
1397 	int	signum;
1398 };
1399 #endif
1400 /*
1401  * MPSAFE
1402  */
1403 /* ARGSUSED */
1404 int
1405 okillpg(td, uap)
1406 	struct thread *td;
1407 	register struct okillpg_args *uap;
1408 {
1409 
1410 	if ((u_int)uap->signum > _SIG_MAXSIG)
1411 		return (EINVAL);
1412 	return (killpg1(td, uap->signum, uap->pgid, 0));
1413 }
1414 #endif /* COMPAT_43 || COMPAT_SUNOS */
1415 
1416 /*
1417  * Send a signal to a process group.
1418  */
1419 void
1420 gsignal(pgid, sig)
1421 	int pgid, sig;
1422 {
1423 	struct pgrp *pgrp;
1424 
1425 	if (pgid != 0) {
1426 		sx_slock(&proctree_lock);
1427 		pgrp = pgfind(pgid);
1428 		sx_sunlock(&proctree_lock);
1429 		if (pgrp != NULL) {
1430 			pgsignal(pgrp, sig, 0);
1431 			PGRP_UNLOCK(pgrp);
1432 		}
1433 	}
1434 }
1435 
1436 /*
1437  * Send a signal to a process group.  If checktty is 1,
1438  * limit to members which have a controlling terminal.
1439  */
1440 void
1441 pgsignal(pgrp, sig, checkctty)
1442 	struct pgrp *pgrp;
1443 	int sig, checkctty;
1444 {
1445 	register struct proc *p;
1446 
1447 	if (pgrp) {
1448 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1449 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1450 			PROC_LOCK(p);
1451 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
1452 				psignal(p, sig);
1453 			PROC_UNLOCK(p);
1454 		}
1455 	}
1456 }
1457 
1458 /*
1459  * Send a signal caused by a trap to the current thread.
1460  * If it will be caught immediately, deliver it with correct code.
1461  * Otherwise, post it normally.
1462  *
1463  * MPSAFE
1464  */
1465 void
1466 trapsignal(struct thread *td, int sig, u_long code)
1467 {
1468 	struct sigacts *ps;
1469 	struct proc *p;
1470 	siginfo_t siginfo;
1471 	int error;
1472 
1473 	p = td->td_proc;
1474 	if (td->td_flags & TDF_SA) {
1475 		thread_user_enter(p, td);
1476 		PROC_LOCK(p);
1477 		if (td->td_mailbox) {
1478 			SIGDELSET(td->td_sigmask, sig);
1479 			mtx_lock_spin(&sched_lock);
1480 			/*
1481 			 * don't psignal to the thread, it only can accept
1482 			 * its own sync signal.
1483 			 */
1484 			td->td_flags |= TDF_NOSIGPOST;
1485 			/*
1486 			 * Force scheduling an upcall, so UTS has chance to
1487 			 * process the signal before thread runs again in
1488 			 * userland.
1489 			 */
1490 			if (td->td_upcall)
1491 				td->td_upcall->ku_flags |= KUF_DOUPCALL;
1492 			mtx_unlock_spin(&sched_lock);
1493 		}
1494 	} else {
1495 		PROC_LOCK(p);
1496 	}
1497 	ps = p->p_sigacts;
1498 	mtx_lock(&ps->ps_mtx);
1499 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1500 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1501 		p->p_stats->p_ru.ru_nsignals++;
1502 #ifdef KTRACE
1503 		if (KTRPOINT(curthread, KTR_PSIG))
1504 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1505 			    &td->td_sigmask, code);
1506 #endif
1507 		if (!(td->td_flags & TDF_SA))
1508 			(*p->p_sysent->sv_sendsig)(
1509 				ps->ps_sigact[_SIG_IDX(sig)], sig,
1510 				&td->td_sigmask, code);
1511 		else {
1512 			thread_siginfo(sig, code, &siginfo);
1513 			mtx_unlock(&ps->ps_mtx);
1514 			PROC_UNLOCK(p);
1515 			error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
1516 			    sizeof(siginfo));
1517 			PROC_LOCK(p);
1518 			if (error)
1519 				sigexit(td, SIGILL);
1520 			SIGADDSET(td->td_sigmask, sig);
1521 			mtx_lock(&ps->ps_mtx);
1522 		}
1523 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1524 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
1525 			SIGADDSET(td->td_sigmask, sig);
1526 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1527 			/*
1528 			 * See kern_sigaction() for origin of this code.
1529 			 */
1530 			SIGDELSET(ps->ps_sigcatch, sig);
1531 			if (sig != SIGCONT &&
1532 			    sigprop(sig) & SA_IGNORE)
1533 				SIGADDSET(ps->ps_sigignore, sig);
1534 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1535 		}
1536 		mtx_unlock(&ps->ps_mtx);
1537 	} else {
1538 		mtx_unlock(&ps->ps_mtx);
1539 		p->p_code = code;	/* XXX for core dump/debugger */
1540 		p->p_sig = sig;		/* XXX to verify code */
1541 		tdsignal(td, sig, SIGTARGET_TD);
1542 	}
1543 	PROC_UNLOCK(p);
1544 }
1545 
1546 static struct thread *
1547 sigtd(struct proc *p, int sig, int prop)
1548 {
1549 	struct thread *td;
1550 
1551 	PROC_LOCK_ASSERT(p, MA_OWNED);
1552 
1553 	/*
1554 	 * If we know the signal is bound for a specific thread then we
1555 	 * assume that we are in that threads context.  This is the case
1556 	 * for SIGXCPU, SIGILL, etc.  Otherwise someone did a kill() from
1557 	 * userland and the real thread doesn't actually matter.
1558 	 */
1559 	if ((prop & SA_PROC) != 0 && curthread->td_proc == p)
1560 		return (curthread);
1561 
1562 	/*
1563 	 * We should search for the first thread that is blocked in
1564 	 * sigsuspend with this signal unmasked.
1565 	 */
1566 
1567 	/* XXX */
1568 
1569 	/*
1570 	 * Find the first thread in the proc that doesn't have this signal
1571 	 * masked.
1572 	 */
1573 	FOREACH_THREAD_IN_PROC(p, td)
1574 		if (!SIGISMEMBER(td->td_sigmask, sig) &&
1575 		    !(td->td_flags & TDF_NOSIGPOST))
1576 			return (td);
1577 
1578 	return (FIRST_THREAD_IN_PROC(p));
1579 }
1580 
1581 /*
1582  * Send the signal to the process.  If the signal has an action, the action
1583  * is usually performed by the target process rather than the caller; we add
1584  * the signal to the set of pending signals for the process.
1585  *
1586  * Exceptions:
1587  *   o When a stop signal is sent to a sleeping process that takes the
1588  *     default action, the process is stopped without awakening it.
1589  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1590  *     regardless of the signal action (eg, blocked or ignored).
1591  *
1592  * Other ignored signals are discarded immediately.
1593  *
1594  * MPSAFE
1595  */
1596 void
1597 psignal(struct proc *p, int sig)
1598 {
1599 	struct thread *td;
1600 	int prop;
1601 
1602 	PROC_LOCK_ASSERT(p, MA_OWNED);
1603 	prop = sigprop(sig);
1604 
1605 	/*
1606 	 * Find a thread to deliver the signal to.
1607 	 */
1608 	td = sigtd(p, sig, prop);
1609 
1610 	tdsignal(td, sig, SIGTARGET_P);
1611 }
1612 
1613 /*
1614  * MPSAFE
1615  */
1616 void
1617 tdsignal(struct thread *td, int sig, sigtarget_t target)
1618 {
1619 	sigset_t saved;
1620 	struct proc *p = td->td_proc;
1621 
1622 	if (p->p_flag & P_SA)
1623 		saved = p->p_siglist;
1624 	do_tdsignal(td, sig, target);
1625 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
1626 		if (SIGSETEQ(saved, p->p_siglist))
1627 			return;
1628 		else {
1629 			/* pending set changed */
1630 			p->p_flag |= P_SIGEVENT;
1631 			wakeup(&p->p_siglist);
1632 		}
1633 	}
1634 }
1635 
1636 static void
1637 do_tdsignal(struct thread *td, int sig, sigtarget_t target)
1638 {
1639 	struct proc *p;
1640 	register sig_t action;
1641 	sigset_t *siglist;
1642 	struct thread *td0;
1643 	register int prop;
1644 	struct sigacts *ps;
1645 
1646 	KASSERT(_SIG_VALID(sig),
1647 	    ("tdsignal(): invalid signal %d\n", sig));
1648 
1649 	p = td->td_proc;
1650 	ps = p->p_sigacts;
1651 
1652 	PROC_LOCK_ASSERT(p, MA_OWNED);
1653 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1654 
1655 	prop = sigprop(sig);
1656 
1657 	/*
1658 	 * If the signal is blocked and not destined for this thread, then
1659 	 * assign it to the process so that we can find it later in the first
1660 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
1661 	 */
1662 	siglist = (target != SIGTARGET_TD && SIGISMEMBER(td->td_sigmask, sig)) ?
1663 	    &p->p_siglist : &td->td_siglist;
1664 
1665 	/*
1666 	 * If proc is traced, always give parent a chance;
1667 	 * if signal event is tracked by procfs, give *that*
1668 	 * a chance, as well.
1669 	 */
1670 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1671 		action = SIG_DFL;
1672 	} else {
1673 		/*
1674 		 * If the signal is being ignored,
1675 		 * then we forget about it immediately.
1676 		 * (Note: we don't set SIGCONT in ps_sigignore,
1677 		 * and if it is set to SIG_IGN,
1678 		 * action will be SIG_DFL here.)
1679 		 */
1680 		mtx_lock(&ps->ps_mtx);
1681 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
1682 		    (p->p_flag & P_WEXIT)) {
1683 			mtx_unlock(&ps->ps_mtx);
1684 			return;
1685 		}
1686 		if (SIGISMEMBER(td->td_sigmask, sig))
1687 			action = SIG_HOLD;
1688 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1689 			action = SIG_CATCH;
1690 		else
1691 			action = SIG_DFL;
1692 		mtx_unlock(&ps->ps_mtx);
1693 	}
1694 
1695 	if (prop & SA_CONT) {
1696 		SIG_STOPSIGMASK(p->p_siglist);
1697 		/*
1698 		 * XXX Should investigate leaving STOP and CONT sigs only in
1699 		 * the proc's siglist.
1700 		 */
1701 		FOREACH_THREAD_IN_PROC(p, td0)
1702 			SIG_STOPSIGMASK(td0->td_siglist);
1703 	}
1704 
1705 	if (prop & SA_STOP) {
1706 		/*
1707 		 * If sending a tty stop signal to a member of an orphaned
1708 		 * process group, discard the signal here if the action
1709 		 * is default; don't stop the process below if sleeping,
1710 		 * and don't clear any pending SIGCONT.
1711 		 */
1712 		if ((prop & SA_TTYSTOP) &&
1713 		    (p->p_pgrp->pg_jobc == 0) &&
1714 		    (action == SIG_DFL))
1715 		        return;
1716 		SIG_CONTSIGMASK(p->p_siglist);
1717 		FOREACH_THREAD_IN_PROC(p, td0)
1718 			SIG_CONTSIGMASK(td0->td_siglist);
1719 		p->p_flag &= ~P_CONTINUED;
1720 	}
1721 	SIGADDSET(*siglist, sig);
1722 	signotify(td);			/* uses schedlock */
1723 	/*
1724 	 * Defer further processing for signals which are held,
1725 	 * except that stopped processes must be continued by SIGCONT.
1726 	 */
1727 	if (action == SIG_HOLD &&
1728 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
1729 		return;
1730 	/*
1731 	 * Some signals have a process-wide effect and a per-thread
1732 	 * component.  Most processing occurs when the process next
1733 	 * tries to cross the user boundary, however there are some
1734 	 * times when processing needs to be done immediatly, such as
1735 	 * waking up threads so that they can cross the user boundary.
1736 	 * We try do the per-process part here.
1737 	 */
1738 	if (P_SHOULDSTOP(p)) {
1739 		/*
1740 		 * The process is in stopped mode. All the threads should be
1741 		 * either winding down or already on the suspended queue.
1742 		 */
1743 		if (p->p_flag & P_TRACED) {
1744 			/*
1745 			 * The traced process is already stopped,
1746 			 * so no further action is necessary.
1747 			 * No signal can restart us.
1748 			 */
1749 			goto out;
1750 		}
1751 
1752 		if (sig == SIGKILL) {
1753 			/*
1754 			 * SIGKILL sets process running.
1755 			 * It will die elsewhere.
1756 			 * All threads must be restarted.
1757 			 */
1758 			p->p_flag &= ~P_STOPPED;
1759 			goto runfast;
1760 		}
1761 
1762 		if (prop & SA_CONT) {
1763 			/*
1764 			 * If SIGCONT is default (or ignored), we continue the
1765 			 * process but don't leave the signal in siglist as
1766 			 * it has no further action.  If SIGCONT is held, we
1767 			 * continue the process and leave the signal in
1768 			 * siglist.  If the process catches SIGCONT, let it
1769 			 * handle the signal itself.  If it isn't waiting on
1770 			 * an event, it goes back to run state.
1771 			 * Otherwise, process goes back to sleep state.
1772 			 */
1773 			p->p_flag &= ~P_STOPPED_SIG;
1774 			p->p_flag |= P_CONTINUED;
1775 			if (action == SIG_DFL) {
1776 				SIGDELSET(*siglist, sig);
1777 			} else if (action == SIG_CATCH) {
1778 				/*
1779 				 * The process wants to catch it so it needs
1780 				 * to run at least one thread, but which one?
1781 				 * It would seem that the answer would be to
1782 				 * run an upcall in the next KSE to run, and
1783 				 * deliver the signal that way. In a NON KSE
1784 				 * process, we need to make sure that the
1785 				 * single thread is runnable asap.
1786 				 * XXXKSE for now however, make them all run.
1787 				 */
1788 				goto runfast;
1789 			}
1790 			/*
1791 			 * The signal is not ignored or caught.
1792 			 */
1793 			mtx_lock_spin(&sched_lock);
1794 			thread_unsuspend(p);
1795 			mtx_unlock_spin(&sched_lock);
1796 			goto out;
1797 		}
1798 
1799 		if (prop & SA_STOP) {
1800 			/*
1801 			 * Already stopped, don't need to stop again
1802 			 * (If we did the shell could get confused).
1803 			 * Just make sure the signal STOP bit set.
1804 			 */
1805 			p->p_flag |= P_STOPPED_SIG;
1806 			SIGDELSET(*siglist, sig);
1807 			goto out;
1808 		}
1809 
1810 		/*
1811 		 * All other kinds of signals:
1812 		 * If a thread is sleeping interruptibly, simulate a
1813 		 * wakeup so that when it is continued it will be made
1814 		 * runnable and can look at the signal.  However, don't make
1815 		 * the PROCESS runnable, leave it stopped.
1816 		 * It may run a bit until it hits a thread_suspend_check().
1817 		 */
1818 		mtx_lock_spin(&sched_lock);
1819 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR)) {
1820 			if (td->td_flags & TDF_CVWAITQ)
1821 				cv_abort(td);
1822 			else
1823 				abortsleep(td);
1824 		}
1825 		mtx_unlock_spin(&sched_lock);
1826 		goto out;
1827 		/*
1828 		 * XXXKSE  What about threads that are waiting on mutexes?
1829 		 * Shouldn't they abort too?
1830 		 * No, hopefully mutexes are short lived.. They'll
1831 		 * eventually hit thread_suspend_check().
1832 		 */
1833 	}  else if (p->p_state == PRS_NORMAL) {
1834 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
1835 			!(prop & SA_STOP)) {
1836 			mtx_lock_spin(&sched_lock);
1837 			tdsigwakeup(td, sig, action);
1838 			mtx_unlock_spin(&sched_lock);
1839 			goto out;
1840 		}
1841 		if (prop & SA_STOP) {
1842 			if (p->p_flag & P_PPWAIT)
1843 				goto out;
1844 			p->p_flag |= P_STOPPED_SIG;
1845 			p->p_xstat = sig;
1846 			mtx_lock_spin(&sched_lock);
1847 			FOREACH_THREAD_IN_PROC(p, td0) {
1848 				if (TD_IS_SLEEPING(td0) &&
1849 				    (td0->td_flags & TDF_SINTR) &&
1850 				    !TD_IS_SUSPENDED(td0)) {
1851 					thread_suspend_one(td0);
1852 				} else if (td != td0) {
1853 					td0->td_flags |= TDF_ASTPENDING;
1854 				}
1855 			}
1856 			thread_stopped(p);
1857 			if (p->p_numthreads == p->p_suspcount) {
1858 				SIGDELSET(p->p_siglist, p->p_xstat);
1859 				FOREACH_THREAD_IN_PROC(p, td0)
1860 					SIGDELSET(td0->td_siglist, p->p_xstat);
1861 			}
1862 			mtx_unlock_spin(&sched_lock);
1863 			goto out;
1864 		}
1865 		else
1866 			goto runfast;
1867 		/* NOTREACHED */
1868 	} else {
1869 		/* Not in "NORMAL" state. discard the signal. */
1870 		SIGDELSET(*siglist, sig);
1871 		goto out;
1872 	}
1873 
1874 	/*
1875 	 * The process is not stopped so we need to apply the signal to all the
1876 	 * running threads.
1877 	 */
1878 
1879 runfast:
1880 	mtx_lock_spin(&sched_lock);
1881 	tdsigwakeup(td, sig, action);
1882 	thread_unsuspend(p);
1883 	mtx_unlock_spin(&sched_lock);
1884 out:
1885 	/* If we jump here, sched_lock should not be owned. */
1886 	mtx_assert(&sched_lock, MA_NOTOWNED);
1887 }
1888 
1889 /*
1890  * The force of a signal has been directed against a single
1891  * thread. We need to see what we can do about knocking it
1892  * out of any sleep it may be in etc.
1893  */
1894 static void
1895 tdsigwakeup(struct thread *td, int sig, sig_t action)
1896 {
1897 	struct proc *p = td->td_proc;
1898 	register int prop;
1899 
1900 	PROC_LOCK_ASSERT(p, MA_OWNED);
1901 	mtx_assert(&sched_lock, MA_OWNED);
1902 	prop = sigprop(sig);
1903 	/*
1904 	 * Bring the priority of a thread up if we want it to get
1905 	 * killed in this lifetime.
1906 	 */
1907 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1908 		if (td->td_priority > PUSER) {
1909 			td->td_priority = PUSER;
1910 		}
1911 	}
1912 	if (TD_IS_SLEEPING(td)) {
1913 		/*
1914 		 * If thread is sleeping uninterruptibly
1915 		 * we can't interrupt the sleep... the signal will
1916 		 * be noticed when the process returns through
1917 		 * trap() or syscall().
1918 		 */
1919 		if ((td->td_flags & TDF_SINTR) == 0) {
1920 			return;
1921 		}
1922 		/*
1923 		 * Process is sleeping and traced.  Make it runnable
1924 		 * so it can discover the signal in issignal() and stop
1925 		 * for its parent.
1926 		 */
1927 		if (p->p_flag & P_TRACED) {
1928 			p->p_flag &= ~P_STOPPED_TRACE;
1929 		} else {
1930 
1931 			/*
1932 			 * If SIGCONT is default (or ignored) and process is
1933 			 * asleep, we are finished; the process should not
1934 			 * be awakened.
1935 			 */
1936 			if ((prop & SA_CONT) && action == SIG_DFL) {
1937 				SIGDELSET(p->p_siglist, sig);
1938 				/*
1939 				 * It may be on either list in this state.
1940 				 * Remove from both for now.
1941 				 */
1942 				SIGDELSET(td->td_siglist, sig);
1943 				return;
1944 			}
1945 
1946 			/*
1947 			 * Raise priority to at least PUSER.
1948 			 */
1949 			if (td->td_priority > PUSER) {
1950 				td->td_priority = PUSER;
1951 			}
1952 		}
1953 		if (td->td_flags & TDF_CVWAITQ)
1954 			cv_abort(td);
1955 		else
1956 			abortsleep(td);
1957 	}
1958 #ifdef SMP
1959 	  else {
1960 		/*
1961 		 * Other states do nothing with the signal immediatly,
1962 		 * other than kicking ourselves if we are running.
1963 		 * It will either never be noticed, or noticed very soon.
1964 		 */
1965 		if (TD_IS_RUNNING(td) && td != curthread) {
1966 			forward_signal(td);
1967 		}
1968 	  }
1969 #endif
1970 }
1971 
1972 /*
1973  * If the current process has received a signal (should be caught or cause
1974  * termination, should interrupt current syscall), return the signal number.
1975  * Stop signals with default action are processed immediately, then cleared;
1976  * they aren't returned.  This is checked after each entry to the system for
1977  * a syscall or trap (though this can usually be done without calling issignal
1978  * by checking the pending signal masks in cursig.) The normal call
1979  * sequence is
1980  *
1981  *	while (sig = cursig(curthread))
1982  *		postsig(sig);
1983  */
1984 static int
1985 issignal(td)
1986 	struct thread *td;
1987 {
1988 	struct proc *p;
1989 	struct sigacts *ps;
1990 	sigset_t sigpending;
1991 	int sig, prop;
1992 	struct thread *td0;
1993 
1994 	p = td->td_proc;
1995 	ps = p->p_sigacts;
1996 	mtx_assert(&ps->ps_mtx, MA_OWNED);
1997 	PROC_LOCK_ASSERT(p, MA_OWNED);
1998 	for (;;) {
1999 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2000 
2001 		sigpending = td->td_siglist;
2002 		SIGSETNAND(sigpending, td->td_sigmask);
2003 
2004 		if (p->p_flag & P_PPWAIT)
2005 			SIG_STOPSIGMASK(sigpending);
2006 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2007 			return (0);
2008 		sig = sig_ffs(&sigpending);
2009 
2010 		_STOPEVENT(p, S_SIG, sig);
2011 
2012 		/*
2013 		 * We should see pending but ignored signals
2014 		 * only if P_TRACED was on when they were posted.
2015 		 */
2016 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2017 			SIGDELSET(td->td_siglist, sig);
2018 			continue;
2019 		}
2020 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2021 			/*
2022 			 * If traced, always stop.
2023 			 */
2024 			mtx_unlock(&ps->ps_mtx);
2025 			WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2026 			    &p->p_mtx.mtx_object, "Stopping for traced signal");
2027 			p->p_xstat = sig;
2028 			PROC_LOCK(p->p_pptr);
2029 			psignal(p->p_pptr, SIGCHLD);
2030 			PROC_UNLOCK(p->p_pptr);
2031 			mtx_lock_spin(&sched_lock);
2032 			stop(p);	/* uses schedlock too eventually */
2033 			thread_suspend_one(td);
2034 			PROC_UNLOCK(p);
2035 			DROP_GIANT();
2036 			p->p_stats->p_ru.ru_nivcsw++;
2037 			mi_switch();
2038 			mtx_unlock_spin(&sched_lock);
2039 			PICKUP_GIANT();
2040 			PROC_LOCK(p);
2041 			mtx_lock(&ps->ps_mtx);
2042 
2043 			/*
2044 			 * If parent wants us to take the signal,
2045 			 * then it will leave it in p->p_xstat;
2046 			 * otherwise we just look for signals again.
2047 			 */
2048 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
2049 			sig = p->p_xstat;
2050 			if (sig == 0)
2051 				continue;
2052 
2053 			/*
2054 			 * If the traced bit got turned off, go back up
2055 			 * to the top to rescan signals.  This ensures
2056 			 * that p_sig* and p_sigact are consistent.
2057 			 */
2058 			if ((p->p_flag & P_TRACED) == 0)
2059 				continue;
2060 
2061 			/*
2062 			 * Put the new signal into td_siglist.  If the
2063 			 * signal is being masked, look for other signals.
2064 			 */
2065 			SIGADDSET(td->td_siglist, sig);
2066 			if (SIGISMEMBER(td->td_sigmask, sig))
2067 				continue;
2068 			signotify(td);
2069 		}
2070 
2071 		prop = sigprop(sig);
2072 
2073 		/*
2074 		 * Decide whether the signal should be returned.
2075 		 * Return the signal's number, or fall through
2076 		 * to clear it from the pending mask.
2077 		 */
2078 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2079 
2080 		case (intptr_t)SIG_DFL:
2081 			/*
2082 			 * Don't take default actions on system processes.
2083 			 */
2084 			if (p->p_pid <= 1) {
2085 #ifdef DIAGNOSTIC
2086 				/*
2087 				 * Are you sure you want to ignore SIGSEGV
2088 				 * in init? XXX
2089 				 */
2090 				printf("Process (pid %lu) got signal %d\n",
2091 					(u_long)p->p_pid, sig);
2092 #endif
2093 				break;		/* == ignore */
2094 			}
2095 			/*
2096 			 * If there is a pending stop signal to process
2097 			 * with default action, stop here,
2098 			 * then clear the signal.  However,
2099 			 * if process is member of an orphaned
2100 			 * process group, ignore tty stop signals.
2101 			 */
2102 			if (prop & SA_STOP) {
2103 				if (p->p_flag & P_TRACED ||
2104 		    		    (p->p_pgrp->pg_jobc == 0 &&
2105 				     prop & SA_TTYSTOP))
2106 					break;	/* == ignore */
2107 				mtx_unlock(&ps->ps_mtx);
2108 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2109 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2110 				p->p_flag |= P_STOPPED_SIG;
2111 				p->p_xstat = sig;
2112 				mtx_lock_spin(&sched_lock);
2113 				FOREACH_THREAD_IN_PROC(p, td0) {
2114 					if (TD_IS_SLEEPING(td0) &&
2115 					    (td0->td_flags & TDF_SINTR) &&
2116 					    !TD_IS_SUSPENDED(td0)) {
2117 						thread_suspend_one(td0);
2118 					} else if (td != td0) {
2119 						td0->td_flags |= TDF_ASTPENDING;
2120 					}
2121 				}
2122 				thread_stopped(p);
2123 				thread_suspend_one(td);
2124 				PROC_UNLOCK(p);
2125 				DROP_GIANT();
2126 				p->p_stats->p_ru.ru_nivcsw++;
2127 				mi_switch();
2128 				mtx_unlock_spin(&sched_lock);
2129 				PICKUP_GIANT();
2130 				PROC_LOCK(p);
2131 				mtx_lock(&ps->ps_mtx);
2132 				break;
2133 			} else if (prop & SA_IGNORE) {
2134 				/*
2135 				 * Except for SIGCONT, shouldn't get here.
2136 				 * Default action is to ignore; drop it.
2137 				 */
2138 				break;		/* == ignore */
2139 			} else
2140 				return (sig);
2141 			/*NOTREACHED*/
2142 
2143 		case (intptr_t)SIG_IGN:
2144 			/*
2145 			 * Masking above should prevent us ever trying
2146 			 * to take action on an ignored signal other
2147 			 * than SIGCONT, unless process is traced.
2148 			 */
2149 			if ((prop & SA_CONT) == 0 &&
2150 			    (p->p_flag & P_TRACED) == 0)
2151 				printf("issignal\n");
2152 			break;		/* == ignore */
2153 
2154 		default:
2155 			/*
2156 			 * This signal has an action, let
2157 			 * postsig() process it.
2158 			 */
2159 			return (sig);
2160 		}
2161 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2162 	}
2163 	/* NOTREACHED */
2164 }
2165 
2166 /*
2167  * Put the argument process into the stopped state and notify the parent
2168  * via wakeup.  Signals are handled elsewhere.  The process must not be
2169  * on the run queue.  Must be called with the proc p locked and the scheduler
2170  * lock held.
2171  */
2172 static void
2173 stop(struct proc *p)
2174 {
2175 
2176 	PROC_LOCK_ASSERT(p, MA_OWNED);
2177 	p->p_flag |= P_STOPPED_SIG;
2178 	p->p_flag &= ~P_WAITED;
2179 	wakeup(p->p_pptr);
2180 }
2181 
2182 /*
2183  * MPSAFE
2184  */
2185 void
2186 thread_stopped(struct proc *p)
2187 {
2188 	struct proc *p1 = curthread->td_proc;
2189 	struct sigacts *ps;
2190 	int n;
2191 
2192 	PROC_LOCK_ASSERT(p, MA_OWNED);
2193 	mtx_assert(&sched_lock, MA_OWNED);
2194 	n = p->p_suspcount;
2195 	if (p == p1)
2196 		n++;
2197 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2198 		mtx_unlock_spin(&sched_lock);
2199 		stop(p);
2200 		PROC_LOCK(p->p_pptr);
2201 		ps = p->p_pptr->p_sigacts;
2202 		mtx_lock(&ps->ps_mtx);
2203 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2204 			mtx_unlock(&ps->ps_mtx);
2205 			psignal(p->p_pptr, SIGCHLD);
2206 		} else
2207 			mtx_unlock(&ps->ps_mtx);
2208 		PROC_UNLOCK(p->p_pptr);
2209 		mtx_lock_spin(&sched_lock);
2210 	}
2211 }
2212 
2213 /*
2214  * Take the action for the specified signal
2215  * from the current set of pending signals.
2216  */
2217 void
2218 postsig(sig)
2219 	register int sig;
2220 {
2221 	struct thread *td = curthread;
2222 	register struct proc *p = td->td_proc;
2223 	struct sigacts *ps;
2224 	sig_t action;
2225 	sigset_t returnmask;
2226 	int code;
2227 
2228 	KASSERT(sig != 0, ("postsig"));
2229 
2230 	PROC_LOCK_ASSERT(p, MA_OWNED);
2231 	ps = p->p_sigacts;
2232 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2233 	SIGDELSET(td->td_siglist, sig);
2234 	action = ps->ps_sigact[_SIG_IDX(sig)];
2235 #ifdef KTRACE
2236 	if (KTRPOINT(td, KTR_PSIG))
2237 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2238 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2239 #endif
2240 	_STOPEVENT(p, S_SIG, sig);
2241 
2242 	if (action == SIG_DFL) {
2243 		/*
2244 		 * Default action, where the default is to kill
2245 		 * the process.  (Other cases were ignored above.)
2246 		 */
2247 		mtx_unlock(&ps->ps_mtx);
2248 		sigexit(td, sig);
2249 		/* NOTREACHED */
2250 	} else {
2251 		/*
2252 		 * If we get here, the signal must be caught.
2253 		 */
2254 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2255 		    ("postsig action"));
2256 		/*
2257 		 * Set the new mask value and also defer further
2258 		 * occurrences of this signal.
2259 		 *
2260 		 * Special case: user has done a sigsuspend.  Here the
2261 		 * current mask is not of interest, but rather the
2262 		 * mask from before the sigsuspend is what we want
2263 		 * restored after the signal processing is completed.
2264 		 */
2265 		if (td->td_pflags & TDP_OLDMASK) {
2266 			returnmask = td->td_oldsigmask;
2267 			td->td_pflags &= ~TDP_OLDMASK;
2268 		} else
2269 			returnmask = td->td_sigmask;
2270 
2271 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2272 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
2273 			SIGADDSET(td->td_sigmask, sig);
2274 
2275 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2276 			/*
2277 			 * See kern_sigaction() for origin of this code.
2278 			 */
2279 			SIGDELSET(ps->ps_sigcatch, sig);
2280 			if (sig != SIGCONT &&
2281 			    sigprop(sig) & SA_IGNORE)
2282 				SIGADDSET(ps->ps_sigignore, sig);
2283 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2284 		}
2285 		p->p_stats->p_ru.ru_nsignals++;
2286 		if (p->p_sig != sig) {
2287 			code = 0;
2288 		} else {
2289 			code = p->p_code;
2290 			p->p_code = 0;
2291 			p->p_sig = 0;
2292 		}
2293 		if (td->td_flags & TDF_SA)
2294 			thread_signal_add(curthread, sig);
2295 		else
2296 			(*p->p_sysent->sv_sendsig)(action, sig,
2297 			    &returnmask, code);
2298 	}
2299 }
2300 
2301 /*
2302  * Kill the current process for stated reason.
2303  */
2304 void
2305 killproc(p, why)
2306 	struct proc *p;
2307 	char *why;
2308 {
2309 
2310 	PROC_LOCK_ASSERT(p, MA_OWNED);
2311 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2312 		p, p->p_pid, p->p_comm);
2313 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2314 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2315 	psignal(p, SIGKILL);
2316 }
2317 
2318 /*
2319  * Force the current process to exit with the specified signal, dumping core
2320  * if appropriate.  We bypass the normal tests for masked and caught signals,
2321  * allowing unrecoverable failures to terminate the process without changing
2322  * signal state.  Mark the accounting record with the signal termination.
2323  * If dumping core, save the signal number for the debugger.  Calls exit and
2324  * does not return.
2325  *
2326  * MPSAFE
2327  */
2328 void
2329 sigexit(td, sig)
2330 	struct thread *td;
2331 	int sig;
2332 {
2333 	struct proc *p = td->td_proc;
2334 
2335 	PROC_LOCK_ASSERT(p, MA_OWNED);
2336 	p->p_acflag |= AXSIG;
2337 	if (sigprop(sig) & SA_CORE) {
2338 		p->p_sig = sig;
2339 		/*
2340 		 * Log signals which would cause core dumps
2341 		 * (Log as LOG_INFO to appease those who don't want
2342 		 * these messages.)
2343 		 * XXX : Todo, as well as euid, write out ruid too
2344 		 */
2345 		PROC_UNLOCK(p);
2346 		if (!mtx_owned(&Giant))
2347 			mtx_lock(&Giant);
2348 		if (coredump(td) == 0)
2349 			sig |= WCOREFLAG;
2350 		if (kern_logsigexit)
2351 			log(LOG_INFO,
2352 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
2353 			    p->p_pid, p->p_comm,
2354 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
2355 			    sig &~ WCOREFLAG,
2356 			    sig & WCOREFLAG ? " (core dumped)" : "");
2357 	} else {
2358 		PROC_UNLOCK(p);
2359 		if (!mtx_owned(&Giant))
2360 			mtx_lock(&Giant);
2361 	}
2362 	exit1(td, W_EXITCODE(0, sig));
2363 	/* NOTREACHED */
2364 }
2365 
2366 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2367 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2368 	      sizeof(corefilename), "process corefile name format string");
2369 
2370 /*
2371  * expand_name(name, uid, pid)
2372  * Expand the name described in corefilename, using name, uid, and pid.
2373  * corefilename is a printf-like string, with three format specifiers:
2374  *	%N	name of process ("name")
2375  *	%P	process id (pid)
2376  *	%U	user id (uid)
2377  * For example, "%N.core" is the default; they can be disabled completely
2378  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2379  * This is controlled by the sysctl variable kern.corefile (see above).
2380  */
2381 
2382 static char *
2383 expand_name(name, uid, pid)
2384 	const char *name;
2385 	uid_t uid;
2386 	pid_t pid;
2387 {
2388 	const char *format, *appendstr;
2389 	char *temp;
2390 	char buf[11];		/* Buffer for pid/uid -- max 4B */
2391 	size_t i, l, n;
2392 
2393 	format = corefilename;
2394 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2395 	if (temp == NULL)
2396 		return (NULL);
2397 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2398 		switch (format[i]) {
2399 		case '%':	/* Format character */
2400 			i++;
2401 			switch (format[i]) {
2402 			case '%':
2403 				appendstr = "%";
2404 				break;
2405 			case 'N':	/* process name */
2406 				appendstr = name;
2407 				break;
2408 			case 'P':	/* process id */
2409 				sprintf(buf, "%u", pid);
2410 				appendstr = buf;
2411 				break;
2412 			case 'U':	/* user id */
2413 				sprintf(buf, "%u", uid);
2414 				appendstr = buf;
2415 				break;
2416 			default:
2417 				appendstr = "";
2418 			  	log(LOG_ERR,
2419 				    "Unknown format character %c in `%s'\n",
2420 				    format[i], format);
2421 			}
2422 			l = strlen(appendstr);
2423 			if ((n + l) >= MAXPATHLEN)
2424 				goto toolong;
2425 			memcpy(temp + n, appendstr, l);
2426 			n += l;
2427 			break;
2428 		default:
2429 			temp[n++] = format[i];
2430 		}
2431 	}
2432 	if (format[i] != '\0')
2433 		goto toolong;
2434 	return (temp);
2435 toolong:
2436 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
2437 	    (long)pid, name, (u_long)uid);
2438 	free(temp, M_TEMP);
2439 	return (NULL);
2440 }
2441 
2442 /*
2443  * Dump a process' core.  The main routine does some
2444  * policy checking, and creates the name of the coredump;
2445  * then it passes on a vnode and a size limit to the process-specific
2446  * coredump routine if there is one; if there _is not_ one, it returns
2447  * ENOSYS; otherwise it returns the error from the process-specific routine.
2448  */
2449 
2450 static int
2451 coredump(struct thread *td)
2452 {
2453 	struct proc *p = td->td_proc;
2454 	register struct vnode *vp;
2455 	register struct ucred *cred = td->td_ucred;
2456 	struct flock lf;
2457 	struct nameidata nd;
2458 	struct vattr vattr;
2459 	int error, error1, flags;
2460 	struct mount *mp;
2461 	char *name;			/* name of corefile */
2462 	off_t limit;
2463 
2464 	PROC_LOCK(p);
2465 	_STOPEVENT(p, S_CORE, 0);
2466 
2467 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2468 		PROC_UNLOCK(p);
2469 		return (EFAULT);
2470 	}
2471 
2472 	/*
2473 	 * Note that the bulk of limit checking is done after
2474 	 * the corefile is created.  The exception is if the limit
2475 	 * for corefiles is 0, in which case we don't bother
2476 	 * creating the corefile at all.  This layout means that
2477 	 * a corefile is truncated instead of not being created,
2478 	 * if it is larger than the limit.
2479 	 */
2480 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2481 	if (limit == 0) {
2482 		PROC_UNLOCK(p);
2483 		return 0;
2484 	}
2485 	PROC_UNLOCK(p);
2486 
2487 restart:
2488 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2489 	if (name == NULL)
2490 		return (EINVAL);
2491 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2492 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2493 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
2494 	free(name, M_TEMP);
2495 	if (error)
2496 		return (error);
2497 	NDFREE(&nd, NDF_ONLY_PNBUF);
2498 	vp = nd.ni_vp;
2499 
2500 	/* Don't dump to non-regular files or files with links. */
2501 	if (vp->v_type != VREG ||
2502 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2503 		VOP_UNLOCK(vp, 0, td);
2504 		error = EFAULT;
2505 		goto out2;
2506 	}
2507 
2508 	VOP_UNLOCK(vp, 0, td);
2509 	lf.l_whence = SEEK_SET;
2510 	lf.l_start = 0;
2511 	lf.l_len = 0;
2512 	lf.l_type = F_WRLCK;
2513 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
2514 	if (error)
2515 		goto out2;
2516 
2517 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2518 		lf.l_type = F_UNLCK;
2519 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2520 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2521 			return (error);
2522 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2523 			return (error);
2524 		goto restart;
2525 	}
2526 
2527 	VATTR_NULL(&vattr);
2528 	vattr.va_size = 0;
2529 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2530 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2531 	VOP_SETATTR(vp, &vattr, cred, td);
2532 	VOP_UNLOCK(vp, 0, td);
2533 	PROC_LOCK(p);
2534 	p->p_acflag |= ACORE;
2535 	PROC_UNLOCK(p);
2536 
2537 	error = p->p_sysent->sv_coredump ?
2538 	  p->p_sysent->sv_coredump(td, vp, limit) :
2539 	  ENOSYS;
2540 
2541 	lf.l_type = F_UNLCK;
2542 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2543 	vn_finished_write(mp);
2544 out2:
2545 	error1 = vn_close(vp, FWRITE, cred, td);
2546 	if (error == 0)
2547 		error = error1;
2548 	return (error);
2549 }
2550 
2551 /*
2552  * Nonexistent system call-- signal process (may want to handle it).
2553  * Flag error in case process won't see signal immediately (blocked or ignored).
2554  */
2555 #ifndef _SYS_SYSPROTO_H_
2556 struct nosys_args {
2557 	int	dummy;
2558 };
2559 #endif
2560 /*
2561  * MPSAFE
2562  */
2563 /* ARGSUSED */
2564 int
2565 nosys(td, args)
2566 	struct thread *td;
2567 	struct nosys_args *args;
2568 {
2569 	struct proc *p = td->td_proc;
2570 
2571 	PROC_LOCK(p);
2572 	psignal(p, SIGSYS);
2573 	PROC_UNLOCK(p);
2574 	return (ENOSYS);
2575 }
2576 
2577 /*
2578  * Send a SIGIO or SIGURG signal to a process or process group using
2579  * stored credentials rather than those of the current process.
2580  */
2581 void
2582 pgsigio(sigiop, sig, checkctty)
2583 	struct sigio **sigiop;
2584 	int sig, checkctty;
2585 {
2586 	struct sigio *sigio;
2587 
2588 	SIGIO_LOCK();
2589 	sigio = *sigiop;
2590 	if (sigio == NULL) {
2591 		SIGIO_UNLOCK();
2592 		return;
2593 	}
2594 	if (sigio->sio_pgid > 0) {
2595 		PROC_LOCK(sigio->sio_proc);
2596 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
2597 			psignal(sigio->sio_proc, sig);
2598 		PROC_UNLOCK(sigio->sio_proc);
2599 	} else if (sigio->sio_pgid < 0) {
2600 		struct proc *p;
2601 
2602 		PGRP_LOCK(sigio->sio_pgrp);
2603 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2604 			PROC_LOCK(p);
2605 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2606 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
2607 				psignal(p, sig);
2608 			PROC_UNLOCK(p);
2609 		}
2610 		PGRP_UNLOCK(sigio->sio_pgrp);
2611 	}
2612 	SIGIO_UNLOCK();
2613 }
2614 
2615 static int
2616 filt_sigattach(struct knote *kn)
2617 {
2618 	struct proc *p = curproc;
2619 
2620 	kn->kn_ptr.p_proc = p;
2621 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2622 
2623 	PROC_LOCK(p);
2624 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2625 	PROC_UNLOCK(p);
2626 
2627 	return (0);
2628 }
2629 
2630 static void
2631 filt_sigdetach(struct knote *kn)
2632 {
2633 	struct proc *p = kn->kn_ptr.p_proc;
2634 
2635 	PROC_LOCK(p);
2636 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2637 	PROC_UNLOCK(p);
2638 }
2639 
2640 /*
2641  * signal knotes are shared with proc knotes, so we apply a mask to
2642  * the hint in order to differentiate them from process hints.  This
2643  * could be avoided by using a signal-specific knote list, but probably
2644  * isn't worth the trouble.
2645  */
2646 static int
2647 filt_signal(struct knote *kn, long hint)
2648 {
2649 
2650 	if (hint & NOTE_SIGNAL) {
2651 		hint &= ~NOTE_SIGNAL;
2652 
2653 		if (kn->kn_id == hint)
2654 			kn->kn_data++;
2655 	}
2656 	return (kn->kn_data != 0);
2657 }
2658 
2659 struct sigacts *
2660 sigacts_alloc(void)
2661 {
2662 	struct sigacts *ps;
2663 
2664 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
2665 	ps->ps_refcnt = 1;
2666 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
2667 	return (ps);
2668 }
2669 
2670 void
2671 sigacts_free(struct sigacts *ps)
2672 {
2673 
2674 	mtx_lock(&ps->ps_mtx);
2675 	ps->ps_refcnt--;
2676 	if (ps->ps_refcnt == 0) {
2677 		mtx_destroy(&ps->ps_mtx);
2678 		free(ps, M_SUBPROC);
2679 	} else
2680 		mtx_unlock(&ps->ps_mtx);
2681 }
2682 
2683 struct sigacts *
2684 sigacts_hold(struct sigacts *ps)
2685 {
2686 	mtx_lock(&ps->ps_mtx);
2687 	ps->ps_refcnt++;
2688 	mtx_unlock(&ps->ps_mtx);
2689 	return (ps);
2690 }
2691 
2692 void
2693 sigacts_copy(struct sigacts *dest, struct sigacts *src)
2694 {
2695 
2696 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
2697 	mtx_lock(&src->ps_mtx);
2698 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
2699 	mtx_unlock(&src->ps_mtx);
2700 }
2701 
2702 int
2703 sigacts_shared(struct sigacts *ps)
2704 {
2705 	int shared;
2706 
2707 	mtx_lock(&ps->ps_mtx);
2708 	shared = ps->ps_refcnt > 1;
2709 	mtx_unlock(&ps->ps_mtx);
2710 	return (shared);
2711 }
2712