Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.245
      1 /*	$NetBSD: kern_sig.c,v 1.245 2007/02/16 00:35:20 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      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 NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  * (c) UNIX System Laboratories, Inc.
     43  * All or some portions of this file are derived from material licensed
     44  * to the University of California by American Telephone and Telegraph
     45  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     46  * the permission of UNIX System Laboratories, Inc.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.245 2007/02/16 00:35:20 ad Exp $");
     77 
     78 #include "opt_ktrace.h"
     79 #include "opt_ptrace.h"
     80 #include "opt_multiprocessor.h"
     81 #include "opt_compat_sunos.h"
     82 #include "opt_compat_netbsd.h"
     83 #include "opt_compat_netbsd32.h"
     84 #include "opt_pax.h"
     85 
     86 #define	SIGPROP		/* include signal properties table */
     87 #include <sys/param.h>
     88 #include <sys/signalvar.h>
     89 #include <sys/proc.h>
     90 #include <sys/systm.h>
     91 #include <sys/wait.h>
     92 #include <sys/ktrace.h>
     93 #include <sys/syslog.h>
     94 #include <sys/filedesc.h>
     95 #include <sys/file.h>
     96 #include <sys/malloc.h>
     97 #include <sys/pool.h>
     98 #include <sys/ucontext.h>
     99 #include <sys/exec.h>
    100 #include <sys/kauth.h>
    101 #include <sys/acct.h>
    102 #include <sys/callout.h>
    103 
    104 #include <machine/cpu.h>
    105 
    106 #ifdef PAX_SEGVGUARD
    107 #include <sys/pax.h>
    108 #endif /* PAX_SEGVGUARD */
    109 
    110 #include <uvm/uvm.h>
    111 #include <uvm/uvm_extern.h>
    112 
    113 static void	ksiginfo_exechook(struct proc *, void *);
    114 static void	proc_stop_callout(void *);
    115 
    116 int	sigunwait(struct proc *, const ksiginfo_t *);
    117 void	sigput(sigpend_t *, struct proc *, ksiginfo_t *);
    118 int	sigpost(struct lwp *, sig_t, int, int);
    119 int	sigchecktrace(sigpend_t **);
    120 void	sigswitch(boolean_t, int, int);
    121 void	sigrealloc(ksiginfo_t *);
    122 
    123 sigset_t	contsigmask, stopsigmask, sigcantmask;
    124 struct pool	sigacts_pool;	/* memory pool for sigacts structures */
    125 static void	sigacts_poolpage_free(struct pool *, void *);
    126 static void	*sigacts_poolpage_alloc(struct pool *, int);
    127 static struct	callout proc_stop_ch;
    128 
    129 static struct pool_allocator sigactspool_allocator = {
    130         .pa_alloc = sigacts_poolpage_alloc,
    131 	.pa_free = sigacts_poolpage_free,
    132 };
    133 
    134 #ifdef DEBUG
    135 int	kern_logsigexit = 1;
    136 #else
    137 int	kern_logsigexit = 0;
    138 #endif
    139 
    140 static	const char logcoredump[] =
    141     "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
    142 static	const char lognocoredump[] =
    143     "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
    144 
    145 POOL_INIT(siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
    146     &pool_allocator_nointr);
    147 POOL_INIT(ksiginfo_pool, sizeof(ksiginfo_t), 0, 0, 0, "ksiginfo", NULL);
    148 
    149 /*
    150  * signal_init:
    151  *
    152  * 	Initialize global signal-related data structures.
    153  */
    154 void
    155 signal_init(void)
    156 {
    157 
    158 	sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2;
    159 
    160 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
    161 	    sizeof(struct sigacts) > PAGE_SIZE ?
    162 	    &sigactspool_allocator : &pool_allocator_nointr);
    163 
    164 	exechook_establish(ksiginfo_exechook, NULL);
    165 
    166 	callout_init(&proc_stop_ch);
    167 	callout_setfunc(&proc_stop_ch, proc_stop_callout, NULL);
    168 }
    169 
    170 /*
    171  * sigacts_poolpage_alloc:
    172  *
    173  *	 Allocate a page for the sigacts memory pool.
    174  */
    175 static void *
    176 sigacts_poolpage_alloc(struct pool *pp, int flags)
    177 {
    178 
    179 	return (void *)uvm_km_alloc(kernel_map,
    180 	    (PAGE_SIZE)*2, (PAGE_SIZE)*2,
    181 	    ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
    182 	    | UVM_KMF_WIRED);
    183 }
    184 
    185 /*
    186  * sigacts_poolpage_free:
    187  *
    188  *	 Free a page on behalf of the sigacts memory pool.
    189  */
    190 static void
    191 sigacts_poolpage_free(struct pool *pp, void *v)
    192 {
    193         uvm_km_free(kernel_map, (vaddr_t)v, (PAGE_SIZE)*2, UVM_KMF_WIRED);
    194 }
    195 
    196 /*
    197  * sigactsinit:
    198  *
    199  *	 Create an initial sigctx structure, using the same signal state as
    200  *	 p.  If 'share' is set, share the sigctx_proc part, otherwise just
    201  *	 copy it from parent.
    202  */
    203 struct sigacts *
    204 sigactsinit(struct proc *pp, int share)
    205 {
    206 	struct sigacts *ps;
    207 
    208 	if (pp != NULL) {
    209 		LOCK_ASSERT(mutex_owned(&pp->p_smutex));
    210 	}
    211 
    212 	if (share) {
    213 		ps = pp->p_sigacts;
    214 		mutex_enter(&ps->sa_mutex);
    215 		ps->sa_refcnt++;
    216 		mutex_exit(&ps->sa_mutex);
    217 	} else {
    218 		if (pp)
    219 			mutex_exit(&pp->p_smutex);
    220 		ps = pool_get(&sigacts_pool, PR_WAITOK);
    221 		mutex_init(&ps->sa_mutex, MUTEX_SPIN, IPL_NONE);
    222 		if (pp) {
    223 			mutex_enter(&pp->p_smutex);
    224 			memcpy(&ps->sa_sigdesc, pp->p_sigacts->sa_sigdesc,
    225 			    sizeof(ps->sa_sigdesc));
    226 		} else
    227 			memset(&ps->sa_sigdesc, 0, sizeof(ps->sa_sigdesc));
    228 		ps->sa_refcnt = 1;
    229 	}
    230 
    231 	return ps;
    232 }
    233 
    234 /*
    235  * sigactsunshare:
    236  *
    237  *	Make this process not share its sigctx, maintaining all
    238  *	signal state.
    239  */
    240 void
    241 sigactsunshare(struct proc *p)
    242 {
    243 	struct sigacts *ps, *oldps;
    244 	int refcnt;
    245 
    246 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    247 
    248 	oldps = p->p_sigacts;
    249 
    250 	mutex_enter(&oldps->sa_mutex);
    251 	refcnt = oldps->sa_refcnt;
    252 	mutex_exit(&oldps->sa_mutex);
    253 	if (refcnt == 1)
    254 		return;
    255 
    256 	mutex_exit(&p->p_smutex);
    257 	ps = sigactsinit(NULL, 0);
    258 	mutex_enter(&p->p_smutex);
    259 	p->p_sigacts = ps;
    260 
    261 	sigactsfree(oldps);
    262 }
    263 
    264 /*
    265  * sigactsfree;
    266  *
    267  *	Release a sigctx structure.
    268  */
    269 void
    270 sigactsfree(struct sigacts *ps)
    271 {
    272 	int refcnt;
    273 
    274 	mutex_enter(&ps->sa_mutex);
    275 	refcnt = --ps->sa_refcnt;
    276 	mutex_exit(&ps->sa_mutex);
    277 
    278 	if (refcnt == 0) {
    279 		mutex_destroy(&ps->sa_mutex);
    280 		pool_put(&sigacts_pool, ps);
    281 	}
    282 }
    283 
    284 /*
    285  * siginit:
    286  *
    287  *	Initialize signal state for process 0; set to ignore signals that
    288  *	are ignored by default and disable the signal stack.  Locking not
    289  *	required as the system is still cold.
    290  */
    291 void
    292 siginit(struct proc *p)
    293 {
    294 	struct lwp *l;
    295 	struct sigacts *ps;
    296 	int signo, prop;
    297 
    298 	ps = p->p_sigacts;
    299 	sigemptyset(&contsigmask);
    300 	sigemptyset(&stopsigmask);
    301 	sigemptyset(&sigcantmask);
    302 	for (signo = 1; signo < NSIG; signo++) {
    303 		prop = sigprop[signo];
    304 		if (prop & SA_CONT)
    305 			sigaddset(&contsigmask, signo);
    306 		if (prop & SA_STOP)
    307 			sigaddset(&stopsigmask, signo);
    308 		if (prop & SA_CANTMASK)
    309 			sigaddset(&sigcantmask, signo);
    310 		if (prop & SA_IGNORE && signo != SIGCONT)
    311 			sigaddset(&p->p_sigctx.ps_sigignore, signo);
    312 		sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
    313 		SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
    314 	}
    315 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    316 	p->p_sflag &= ~PS_NOCLDSTOP;
    317 
    318 	ksiginfo_queue_init(&p->p_sigpend.sp_info);
    319 	sigemptyset(&p->p_sigpend.sp_set);
    320 
    321 	/*
    322 	 * Reset per LWP state.
    323 	 */
    324 	l = LIST_FIRST(&p->p_lwps);
    325 	l->l_sigwaited = NULL;
    326 	l->l_sigstk.ss_flags = SS_DISABLE;
    327 	l->l_sigstk.ss_size = 0;
    328 	l->l_sigstk.ss_sp = 0;
    329 	ksiginfo_queue_init(&l->l_sigpend.sp_info);
    330 	sigemptyset(&l->l_sigpend.sp_set);
    331 
    332 	/* One reference. */
    333 	ps->sa_refcnt = 1;
    334 }
    335 
    336 /*
    337  * execsigs:
    338  *
    339  *	Reset signals for an exec of the specified process.
    340  */
    341 void
    342 execsigs(struct proc *p)
    343 {
    344 	struct sigacts *ps;
    345 	struct lwp *l;
    346 	int signo, prop;
    347 	sigset_t tset;
    348 	ksiginfoq_t kq;
    349 
    350 	KASSERT(p->p_nlwps == 1);
    351 
    352 	mutex_enter(&p->p_smutex);
    353 
    354 	sigactsunshare(p);
    355 
    356 	ps = p->p_sigacts;
    357 
    358 	/*
    359 	 * Reset caught signals.  Held signals remain held through
    360 	 * l->l_sigmask (unless they were caught, and are now ignored
    361 	 * by default).
    362 	 */
    363 	sigemptyset(&tset);
    364 	for (signo = 1; signo < NSIG; signo++) {
    365 		if (sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
    366 			prop = sigprop[signo];
    367 			if (prop & SA_IGNORE) {
    368 				if ((prop & SA_CONT) == 0)
    369 					sigaddset(&p->p_sigctx.ps_sigignore,
    370 					    signo);
    371 				sigaddset(&tset, signo);
    372 			}
    373 			SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
    374 		}
    375 		sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
    376 		SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
    377 	}
    378 	ksiginfo_queue_init(&kq);
    379 	sigclearall(p, &tset, &kq);
    380 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    381 
    382 	/*
    383 	 * Reset no zombies if child dies flag as Solaris does.
    384 	 */
    385 	p->p_flag &= ~(P_NOCLDWAIT | P_CLDSIGIGN);
    386 	if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN)
    387 		SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL;
    388 
    389 	/*
    390 	 * Reset per-LWP state.
    391 	 */
    392 	l = LIST_FIRST(&p->p_lwps);
    393 	l->l_sigwaited = NULL;
    394 	l->l_sigstk.ss_flags = SS_DISABLE;
    395 	l->l_sigstk.ss_size = 0;
    396 	l->l_sigstk.ss_sp = 0;
    397 	ksiginfo_queue_init(&l->l_sigpend.sp_info);
    398 	sigemptyset(&l->l_sigpend.sp_set);
    399 
    400 	mutex_exit(&p->p_smutex);
    401 	ksiginfo_queue_drain(&kq);
    402 }
    403 
    404 /*
    405  * ksiginfo_exechook:
    406  *
    407  *	Free all pending ksiginfo entries from a process on exec.
    408  *	Additionally, drain any unused ksiginfo structures in the
    409  *	system back to the pool.
    410  *
    411  *	XXX This should not be a hook, every process has signals.
    412  */
    413 static void
    414 ksiginfo_exechook(struct proc *p, void *v)
    415 {
    416 	ksiginfoq_t kq;
    417 
    418 	ksiginfo_queue_init(&kq);
    419 
    420 	mutex_enter(&p->p_smutex);
    421 	sigclearall(p, NULL, &kq);
    422 	mutex_exit(&p->p_smutex);
    423 
    424 	ksiginfo_queue_drain(&kq);
    425 }
    426 
    427 /*
    428  * ksiginfo_alloc:
    429  *
    430  *	Allocate a new ksiginfo structure from the pool, and optionally copy
    431  *	an existing one.  If the existing ksiginfo_t is from the pool, and
    432  *	has not been queued somewhere, then just return it.  Additionally,
    433  *	if the existing ksiginfo_t does not contain any information beyond
    434  *	the signal number, then just return it.
    435  */
    436 ksiginfo_t *
    437 ksiginfo_alloc(struct proc *p, ksiginfo_t *ok, int flags)
    438 {
    439 	ksiginfo_t *kp;
    440 	int s;
    441 
    442 	if (ok != NULL) {
    443 		if ((ok->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) ==
    444 		    KSI_FROMPOOL)
    445 		    	return ok;
    446 		if (KSI_EMPTY_P(ok))
    447 			return ok;
    448 	}
    449 
    450 	s = splsoftclock();
    451 	kp = pool_get(&ksiginfo_pool, flags);
    452 	splx(s);
    453 	if (kp == NULL) {
    454 #ifdef DIAGNOSTIC
    455 		printf("Out of memory allocating ksiginfo for pid %d\n",
    456 		    p->p_pid);
    457 #endif
    458 		return NULL;
    459 	}
    460 
    461 	if (ok != NULL) {
    462 		memcpy(kp, ok, sizeof(*kp));
    463 		kp->ksi_flags &= ~KSI_QUEUED;
    464 	} else
    465 		KSI_INIT_EMPTY(kp);
    466 
    467 	kp->ksi_flags |= KSI_FROMPOOL;
    468 
    469 	return kp;
    470 }
    471 
    472 /*
    473  * ksiginfo_free:
    474  *
    475  *	If the given ksiginfo_t is from the pool and has not been queued,
    476  *	then free it.
    477  */
    478 void
    479 ksiginfo_free(ksiginfo_t *kp)
    480 {
    481 	int s;
    482 
    483 	if ((kp->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) != KSI_FROMPOOL)
    484 		return;
    485 	s = splsoftclock();
    486 	pool_put(&ksiginfo_pool, kp);
    487 	splx(s);
    488 }
    489 
    490 /*
    491  * ksiginfo_queue_drain:
    492  *
    493  *	Drain a non-empty ksiginfo_t queue.
    494  */
    495 void
    496 ksiginfo_queue_drain0(ksiginfoq_t *kq)
    497 {
    498 	ksiginfo_t *ksi;
    499 	int s;
    500 
    501 	KASSERT(!CIRCLEQ_EMPTY(kq));
    502 
    503 	KERNEL_LOCK(1, curlwp);		/* XXXSMP */
    504 	while (!CIRCLEQ_EMPTY(kq)) {
    505 		ksi = CIRCLEQ_FIRST(kq);
    506 		CIRCLEQ_REMOVE(kq, ksi, ksi_list);
    507 		s = splsoftclock();
    508 		pool_put(&ksiginfo_pool, ksi);
    509 		splx(s);
    510 	}
    511 	KERNEL_UNLOCK_ONE(curlwp);	/* XXXSMP */
    512 }
    513 
    514 /*
    515  * sigget:
    516  *
    517  *	Fetch the first pending signal from a set.  Optionally, also fetch
    518  *	or manufacture a ksiginfo element.  Returns the number of the first
    519  *	pending signal, or zero.
    520  */
    521 int
    522 sigget(sigpend_t *sp, ksiginfo_t *out, int signo, sigset_t *mask)
    523 {
    524         ksiginfo_t *ksi;
    525 	sigset_t tset;
    526 
    527 	/* If there's no pending set, the signal is from the debugger. */
    528 	if (sp == NULL) {
    529 		if (out != NULL) {
    530 			KSI_INIT(out);
    531 			out->ksi_info._signo = signo;
    532 			out->ksi_info._code = SI_USER;
    533 		}
    534 		return signo;
    535 	}
    536 
    537 	/* Construct mask from signo, and 'mask'. */
    538 	if (signo == 0) {
    539 		if (mask != NULL) {
    540 			tset = *mask;
    541 			__sigandset(&sp->sp_set, &tset);
    542 		} else
    543 			tset = sp->sp_set;
    544 
    545 		/* If there are no signals pending, that's it. */
    546 		if ((signo = firstsig(&tset)) == 0)
    547 			return 0;
    548 	} else {
    549 		KASSERT(sigismember(&sp->sp_set, signo));
    550 	}
    551 
    552 	sigdelset(&sp->sp_set, signo);
    553 
    554 	/* Find siginfo and copy it out. */
    555 	CIRCLEQ_FOREACH(ksi, &sp->sp_info, ksi_list) {
    556 		if (ksi->ksi_signo == signo) {
    557 			CIRCLEQ_REMOVE(&sp->sp_info, ksi, ksi_list);
    558 			KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
    559 			KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
    560 			ksi->ksi_flags &= ~KSI_QUEUED;
    561 			if (out != NULL) {
    562 				memcpy(out, ksi, sizeof(*out));
    563 				out->ksi_flags &= ~(KSI_FROMPOOL | KSI_QUEUED);
    564 			}
    565 			ksiginfo_free(ksi);
    566 			return signo;
    567 		}
    568 	}
    569 
    570 	/* If there's no siginfo, then manufacture it. */
    571 	if (out != NULL) {
    572 		KSI_INIT(out);
    573 		out->ksi_info._signo = signo;
    574 		out->ksi_info._code = SI_USER;
    575 	}
    576 
    577 	return signo;
    578 }
    579 
    580 /*
    581  * sigput:
    582  *
    583  *	Append a new ksiginfo element to the list of pending ksiginfo's, if
    584  *	we need to (e.g. SA_SIGINFO was requested).
    585  */
    586 void
    587 sigput(sigpend_t *sp, struct proc *p, ksiginfo_t *ksi)
    588 {
    589 	ksiginfo_t *kp;
    590 	struct sigaction *sa = &SIGACTION_PS(p->p_sigacts, ksi->ksi_signo);
    591 
    592 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    593 	KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
    594 
    595 	sigaddset(&sp->sp_set, ksi->ksi_signo);
    596 
    597 	/*
    598 	 * If siginfo is not required, or there is none, then just mark the
    599 	 * signal as pending.
    600 	 */
    601 	if ((sa->sa_flags & SA_SIGINFO) == 0 || KSI_EMPTY_P(ksi))
    602 		return;
    603 
    604 	KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
    605 
    606 #ifdef notyet	/* XXX: QUEUING */
    607 	if (ksi->ksi_signo < SIGRTMIN)
    608 #endif
    609 	{
    610 		CIRCLEQ_FOREACH(kp, &sp->sp_info, ksi_list) {
    611 			if (kp->ksi_signo == ksi->ksi_signo) {
    612 				KSI_COPY(ksi, kp);
    613 				kp->ksi_flags |= KSI_QUEUED;
    614 				return;
    615 			}
    616 		}
    617 	}
    618 
    619 	ksi->ksi_flags |= KSI_QUEUED;
    620 	CIRCLEQ_INSERT_TAIL(&sp->sp_info, ksi, ksi_list);
    621 }
    622 
    623 /*
    624  * sigclear:
    625  *
    626  *	Clear all pending signals in the specified set.
    627  */
    628 void
    629 sigclear(sigpend_t *sp, sigset_t *mask, ksiginfoq_t *kq)
    630 {
    631 	ksiginfo_t *ksi, *next;
    632 
    633 	if (mask == NULL)
    634 		sigemptyset(&sp->sp_set);
    635 	else
    636 		sigminusset(mask, &sp->sp_set);
    637 
    638 	ksi = CIRCLEQ_FIRST(&sp->sp_info);
    639 	for (; ksi != (void *)&sp->sp_info; ksi = next) {
    640 		next = CIRCLEQ_NEXT(ksi, ksi_list);
    641 		if (mask == NULL || sigismember(mask, ksi->ksi_signo)) {
    642 			CIRCLEQ_REMOVE(&sp->sp_info, ksi, ksi_list);
    643 			KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
    644 			KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
    645 			CIRCLEQ_INSERT_TAIL(kq, ksi, ksi_list);
    646 		}
    647 	}
    648 }
    649 
    650 /*
    651  * sigclearall:
    652  *
    653  *	Clear all pending signals in the specified set from a process and
    654  *	its LWPs.
    655  */
    656 void
    657 sigclearall(struct proc *p, sigset_t *mask, ksiginfoq_t *kq)
    658 {
    659 	struct lwp *l;
    660 
    661 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    662 
    663 	sigclear(&p->p_sigpend, mask, kq);
    664 
    665 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    666 		sigclear(&l->l_sigpend, mask, kq);
    667 	}
    668 }
    669 
    670 /*
    671  * sigispending:
    672  *
    673  *	Return true if there are pending signals for the current LWP.  May
    674  *	be called unlocked provided that L_PENDSIG is set, and that the
    675  *	signal has been posted to the appopriate queue before L_PENDSIG is
    676  *	set.
    677  */
    678 int
    679 sigispending(struct lwp *l, int signo)
    680 {
    681 	struct proc *p = l->l_proc;
    682 	sigset_t tset;
    683 
    684 	mb_read();
    685 
    686 	tset = l->l_sigpend.sp_set;
    687 	sigplusset(&p->p_sigpend.sp_set, &tset);
    688 	sigminusset(&p->p_sigctx.ps_sigignore, &tset);
    689 	sigminusset(&l->l_sigmask, &tset);
    690 
    691 	if (signo == 0) {
    692 		if (firstsig(&tset) != 0)
    693 			return EINTR;
    694 	} else if (sigismember(&tset, signo))
    695 		return EINTR;
    696 
    697 	return 0;
    698 }
    699 
    700 /*
    701  * siginfo_alloc:
    702  *
    703  *	 Allocate a new siginfo_t structure from the pool.
    704  */
    705 siginfo_t *
    706 siginfo_alloc(int flags)
    707 {
    708 
    709 	return pool_get(&siginfo_pool, flags);
    710 }
    711 
    712 /*
    713  * siginfo_free:
    714  *
    715  *	 Return a siginfo_t structure to the pool.
    716  */
    717 void
    718 siginfo_free(void *arg)
    719 {
    720 
    721 	pool_put(&siginfo_pool, arg);
    722 }
    723 
    724 void
    725 getucontext(struct lwp *l, ucontext_t *ucp)
    726 {
    727 	struct proc *p = l->l_proc;
    728 
    729 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    730 
    731 	ucp->uc_flags = 0;
    732 	ucp->uc_link = l->l_ctxlink;
    733 
    734 	ucp->uc_sigmask = l->l_sigmask;
    735 	ucp->uc_flags |= _UC_SIGMASK;
    736 
    737 	/*
    738 	 * The (unsupplied) definition of the `current execution stack'
    739 	 * in the System V Interface Definition appears to allow returning
    740 	 * the main context stack.
    741 	 *
    742 	 * XXXLWP this is borken for multiple LWPs.
    743 	 */
    744 	if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
    745 		ucp->uc_stack.ss_sp = (void *)USRSTACK;
    746 		ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize);
    747 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
    748 	} else {
    749 		/* Simply copy alternate signal execution stack. */
    750 		ucp->uc_stack = l->l_sigstk;
    751 	}
    752 	ucp->uc_flags |= _UC_STACK;
    753 	mutex_exit(&p->p_smutex);
    754 	cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
    755 	mutex_enter(&p->p_smutex);
    756 }
    757 
    758 int
    759 setucontext(struct lwp *l, const ucontext_t *ucp)
    760 {
    761 	struct proc *p = l->l_proc;
    762 	int error;
    763 
    764 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    765 
    766 	if ((ucp->uc_flags & _UC_SIGMASK) != 0) {
    767 		error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL);
    768 		if (error != 0)
    769 			return error;
    770 	}
    771 
    772 	mutex_exit(&p->p_smutex);
    773 	error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags);
    774 	mutex_enter(&p->p_smutex);
    775 	if (error != 0)
    776 		return (error);
    777 
    778 	l->l_ctxlink = ucp->uc_link;
    779 
    780 	/*
    781 	 * If there was stack information, update whether or not we are
    782 	 * still running on an alternate signal stack.
    783 	 */
    784 	if ((ucp->uc_flags & _UC_STACK) != 0) {
    785 		if (ucp->uc_stack.ss_flags & SS_ONSTACK)
    786 			l->l_sigstk.ss_flags |= SS_ONSTACK;
    787 		else
    788 			l->l_sigstk.ss_flags &= ~SS_ONSTACK;
    789 	}
    790 
    791 	return 0;
    792 }
    793 
    794 /*
    795  * Common code for kill process group/broadcast kill.  cp is calling
    796  * process.
    797  */
    798 int
    799 killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all)
    800 {
    801 	struct proc	*p, *cp;
    802 	kauth_cred_t	pc;
    803 	struct pgrp	*pgrp;
    804 	int		nfound;
    805 	int		signo = ksi->ksi_signo;
    806 
    807 	cp = l->l_proc;
    808 	pc = l->l_cred;
    809 	nfound = 0;
    810 
    811 	rw_enter(&proclist_lock, RW_READER);
    812 	if (all) {
    813 		/*
    814 		 * broadcast
    815 		 */
    816 		PROCLIST_FOREACH(p, &allproc) {
    817 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || p == cp)
    818 				continue;
    819 			mutex_enter(&p->p_mutex);
    820 			if (kauth_authorize_process(pc,
    821 			    KAUTH_PROCESS_CANSIGNAL, p,
    822 			    (void *)(uintptr_t)signo, NULL, NULL) == 0) {
    823 				nfound++;
    824 				if (signo) {
    825 					mutex_enter(&proclist_mutex);
    826 					mutex_enter(&p->p_smutex);
    827 					kpsignal2(p, ksi);
    828 					mutex_exit(&p->p_smutex);
    829 					mutex_exit(&proclist_mutex);
    830 				}
    831 			}
    832 			mutex_exit(&p->p_mutex);
    833 		}
    834 	} else {
    835 		if (pgid == 0)
    836 			/*
    837 			 * zero pgid means send to my process group.
    838 			 */
    839 			pgrp = cp->p_pgrp;
    840 		else {
    841 			pgrp = pg_find(pgid, PFIND_LOCKED);
    842 			if (pgrp == NULL)
    843 				goto out;
    844 		}
    845 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
    846 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM)
    847 				continue;
    848 			mutex_enter(&p->p_mutex);
    849 			if (kauth_authorize_process(pc, KAUTH_PROCESS_CANSIGNAL,
    850 			    p, (void *)(uintptr_t)signo, NULL, NULL) == 0) {
    851 				nfound++;
    852 				if (signo) {
    853 					mutex_enter(&proclist_mutex);
    854 					mutex_enter(&p->p_smutex);
    855 					if (P_ZOMBIE(p) == 0)
    856 						kpsignal2(p, ksi);
    857 					mutex_exit(&p->p_smutex);
    858 					mutex_exit(&proclist_mutex);
    859 				}
    860 			}
    861 			mutex_exit(&p->p_mutex);
    862 		}
    863 	}
    864   out:
    865 	rw_exit(&proclist_lock);
    866 	return (nfound ? 0 : ESRCH);
    867 }
    868 
    869 /*
    870  * Send a signal to a process group. If checktty is 1, limit to members
    871  * which have a controlling terminal.
    872  */
    873 void
    874 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
    875 {
    876 	ksiginfo_t ksi;
    877 
    878 	LOCK_ASSERT(mutex_owned(&proclist_mutex));
    879 
    880 	KSI_INIT_EMPTY(&ksi);
    881 	ksi.ksi_signo = sig;
    882 	kpgsignal(pgrp, &ksi, NULL, checkctty);
    883 }
    884 
    885 void
    886 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
    887 {
    888 	struct proc *p;
    889 
    890 	LOCK_ASSERT(mutex_owned(&proclist_mutex));
    891 
    892 	if (pgrp)
    893 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
    894 			if (checkctty == 0 || p->p_lflag & PL_CONTROLT)
    895 				kpsignal(p, ksi, data);
    896 }
    897 
    898 /*
    899  * Send a signal caused by a trap to the current LWP.  If it will be caught
    900  * immediately, deliver it with correct code.  Otherwise, post it normally.
    901  */
    902 void
    903 trapsignal(struct lwp *l, ksiginfo_t *ksi)
    904 {
    905 	struct proc	*p;
    906 	struct sigacts	*ps;
    907 	int signo = ksi->ksi_signo;
    908 
    909 	KASSERT(KSI_TRAP_P(ksi));
    910 
    911 	ksi->ksi_lid = l->l_lid;
    912 	p = l->l_proc;
    913 
    914 	mutex_enter(&proclist_mutex);
    915 	mutex_enter(&p->p_smutex);
    916 	ps = p->p_sigacts;
    917 	if ((p->p_slflag & PSL_TRACED) == 0 &&
    918 	    sigismember(&p->p_sigctx.ps_sigcatch, signo) &&
    919 	    !sigismember(&l->l_sigmask, signo)) {
    920 		mutex_exit(&proclist_mutex);
    921 		p->p_stats->p_ru.ru_nsignals++;
    922 		kpsendsig(l, ksi, &l->l_sigmask);
    923 		mutex_exit(&p->p_smutex);
    924 #ifdef KTRACE
    925 		if (KTRPOINT(p, KTR_PSIG))
    926 			ktrpsig(l, signo, SIGACTION_PS(ps, signo).sa_handler,
    927 			    &l->l_sigmask, ksi);
    928 #endif
    929 	} else {
    930 		/* XXX for core dump/debugger */
    931 		p->p_sigctx.ps_lwp = l->l_lid;
    932 		p->p_sigctx.ps_signo = ksi->ksi_signo;
    933 		p->p_sigctx.ps_code = ksi->ksi_trap;
    934 		kpsignal2(p, ksi);
    935 		mutex_exit(&proclist_mutex);
    936 		mutex_exit(&p->p_smutex);
    937 	}
    938 }
    939 
    940 /*
    941  * Fill in signal information and signal the parent for a child status change.
    942  */
    943 void
    944 child_psignal(struct proc *p, int mask)
    945 {
    946 	ksiginfo_t ksi;
    947 	struct proc *q;
    948 	int xstat;
    949 
    950 	LOCK_ASSERT(mutex_owned(&proclist_mutex));
    951 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    952 
    953 	xstat = p->p_xstat;
    954 
    955 	KSI_INIT(&ksi);
    956 	ksi.ksi_signo = SIGCHLD;
    957 	ksi.ksi_code = (xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED);
    958 	ksi.ksi_pid = p->p_pid;
    959 	ksi.ksi_uid = kauth_cred_geteuid(p->p_cred);
    960 	ksi.ksi_status = xstat;
    961 	ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
    962 	ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
    963 
    964 	q = p->p_pptr;
    965 
    966 	mutex_exit(&p->p_smutex);
    967 	mutex_enter(&q->p_smutex);
    968 
    969 	if ((q->p_sflag & mask) == 0)
    970 		kpsignal2(q, &ksi);
    971 
    972 	mutex_exit(&q->p_smutex);
    973 	mutex_enter(&p->p_smutex);
    974 }
    975 
    976 void
    977 psignal(struct proc *p, int signo)
    978 {
    979 	ksiginfo_t ksi;
    980 
    981 	LOCK_ASSERT(mutex_owned(&proclist_mutex));
    982 
    983 	KSI_INIT_EMPTY(&ksi);
    984 	ksi.ksi_signo = signo;
    985 	mutex_enter(&p->p_smutex);
    986 	kpsignal2(p, &ksi);
    987 	mutex_exit(&p->p_smutex);
    988 }
    989 
    990 void
    991 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
    992 {
    993 
    994 	LOCK_ASSERT(mutex_owned(&proclist_mutex));
    995 
    996 	/* XXXSMP Why is this here? */
    997 	if ((p->p_sflag & PS_WEXIT) == 0 && data) {
    998 		size_t fd;
    999 		struct filedesc *fdp = p->p_fd;
   1000 
   1001 		ksi->ksi_fd = -1;
   1002 		for (fd = 0; fd < fdp->fd_nfiles; fd++) {
   1003 			struct file *fp = fdp->fd_ofiles[fd];
   1004 			/* XXX: lock? */
   1005 			if (fp && fp->f_data == data) {
   1006 				ksi->ksi_fd = fd;
   1007 				break;
   1008 			}
   1009 		}
   1010 	}
   1011 	mutex_enter(&p->p_smutex);
   1012 	kpsignal2(p, ksi);
   1013 	mutex_exit(&p->p_smutex);
   1014 }
   1015 
   1016 /*
   1017  * sigismasked:
   1018  *
   1019  *	 Returns true if signal is ignored or masked for the specified LWP.
   1020  */
   1021 int
   1022 sigismasked(struct lwp *l, int sig)
   1023 {
   1024 	struct proc *p = l->l_proc;
   1025 
   1026 	return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
   1027 	    sigismember(&l->l_sigmask, sig));
   1028 }
   1029 
   1030 /*
   1031  * sigpost:
   1032  *
   1033  *	 Post a pending signal to an LWP.  Returns non-zero if the LWP was
   1034  *	 able to take the signal.
   1035  */
   1036 int
   1037 sigpost(struct lwp *l, sig_t action, int prop, int sig)
   1038 {
   1039 	int rv, masked;
   1040 
   1041 	LOCK_ASSERT(mutex_owned(&l->l_proc->p_smutex));
   1042 
   1043 	/*
   1044 	 * If the LWP is on the way out, sigclear() will be busy draining all
   1045 	 * pending signals.  Don't give it more.
   1046 	 */
   1047 	if (l->l_refcnt == 0)
   1048 		return 0;
   1049 
   1050 	lwp_lock(l);
   1051 
   1052 	/*
   1053 	 * Have the LWP check for signals.  This ensures that even if no LWP
   1054 	 * is found to take the signal immediately, it should be taken soon.
   1055 	 */
   1056 	l->l_flag |= L_PENDSIG;
   1057 
   1058 	/*
   1059 	 * SIGCONT can be masked, but must always restart stopped LWPs.
   1060 	 */
   1061 	masked = sigismember(&l->l_sigmask, sig);
   1062 	if (masked && ((prop & SA_CONT) == 0 || l->l_stat != LSSTOP)) {
   1063 		lwp_unlock(l);
   1064 		return 0;
   1065 	}
   1066 
   1067 	/*
   1068 	 * If the LWP is running or on a run queue, then we win.  If it's
   1069 	 * sleeping interruptably, wake it and make it take the signal.  If
   1070 	 * the sleep isn't interruptable, then the chances are it will get
   1071 	 * to see the signal soon anyhow.  If suspended, it can't take the
   1072 	 * signal right now.  If it's LWP private or for all LWPs, save it
   1073 	 * for later; otherwise punt.
   1074 	 */
   1075 	rv = 0;
   1076 
   1077 	switch (l->l_stat) {
   1078 	case LSRUN:
   1079 	case LSONPROC:
   1080 		lwp_need_userret(l);
   1081 		rv = 1;
   1082 		break;
   1083 
   1084 	case LSSLEEP:
   1085 		if ((l->l_flag & L_SINTR) != 0) {
   1086 			/* setrunnable() will release the lock. */
   1087 			setrunnable(l);
   1088 			return 1;
   1089 		}
   1090 		break;
   1091 
   1092 	case LSSUSPENDED:
   1093 		if ((prop & SA_KILL) != 0) {
   1094 			/* lwp_continue() will release the lock. */
   1095 			lwp_continue(l);
   1096 			return 1;
   1097 		}
   1098 		break;
   1099 
   1100 	case LSSTOP:
   1101 		if ((prop & SA_STOP) != 0)
   1102 			break;
   1103 
   1104 		/*
   1105 		 * If the LWP is stopped and we are sending a continue
   1106 		 * signal, then start it again.
   1107 		 */
   1108 		if ((prop & SA_CONT) != 0) {
   1109 			if (l->l_wchan != NULL) {
   1110 				l->l_stat = LSSLEEP;
   1111 				l->l_proc->p_nrlwps++;
   1112 				rv = 1;
   1113 				break;
   1114 			}
   1115 			/* setrunnable() will release the lock. */
   1116 			setrunnable(l);
   1117 			return 1;
   1118 		} else if (l->l_wchan == NULL || (l->l_flag & L_SINTR) != 0) {
   1119 			/* setrunnable() will release the lock. */
   1120 			setrunnable(l);
   1121 			return 1;
   1122 		}
   1123 		break;
   1124 
   1125 	default:
   1126 		break;
   1127 	}
   1128 
   1129 	lwp_unlock(l);
   1130 	return rv;
   1131 }
   1132 
   1133 /*
   1134  * Notify an LWP that it has a pending signal.
   1135  */
   1136 void
   1137 signotify(struct lwp *l)
   1138 {
   1139 	LOCK_ASSERT(lwp_locked(l, NULL));
   1140 
   1141 	l->l_flag |= L_PENDSIG;
   1142 	lwp_need_userret(l);
   1143 }
   1144 
   1145 /*
   1146  * Find an LWP within process p that is waiting on signal ksi, and hand
   1147  * it on.
   1148  */
   1149 int
   1150 sigunwait(struct proc *p, const ksiginfo_t *ksi)
   1151 {
   1152 	struct lwp *l;
   1153 	int signo;
   1154 
   1155 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1156 
   1157 	signo = ksi->ksi_signo;
   1158 
   1159 	if (ksi->ksi_lid != 0) {
   1160 		/*
   1161 		 * Signal came via _lwp_kill().  Find the LWP and see if
   1162 		 * it's interested.
   1163 		 */
   1164 		if ((l = lwp_find(p, ksi->ksi_lid)) == NULL)
   1165 			return 0;
   1166 		if (l->l_sigwaited == NULL ||
   1167 		    !sigismember(&l->l_sigwaitset, signo))
   1168 			return 0;
   1169 	} else {
   1170 		/*
   1171 		 * Look for any LWP that may be interested.
   1172 		 */
   1173 		LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) {
   1174 			KASSERT(l->l_sigwaited != NULL);
   1175 			if (sigismember(&l->l_sigwaitset, signo))
   1176 				break;
   1177 		}
   1178 	}
   1179 
   1180 	if (l != NULL) {
   1181 		l->l_sigwaited->ksi_info = ksi->ksi_info;
   1182 		l->l_sigwaited = NULL;
   1183 		LIST_REMOVE(l, l_sigwaiter);
   1184 		cv_signal(&l->l_sigcv);
   1185 		return 1;
   1186 	}
   1187 
   1188 	return 0;
   1189 }
   1190 
   1191 /*
   1192  * Send the signal to the process.  If the signal has an action, the action
   1193  * is usually performed by the target process rather than the caller; we add
   1194  * the signal to the set of pending signals for the process.
   1195  *
   1196  * Exceptions:
   1197  *   o When a stop signal is sent to a sleeping process that takes the
   1198  *     default action, the process is stopped without awakening it.
   1199  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
   1200  *     regardless of the signal action (eg, blocked or ignored).
   1201  *
   1202  * Other ignored signals are discarded immediately.
   1203  */
   1204 void
   1205 kpsignal2(struct proc *p, ksiginfo_t *ksi)
   1206 {
   1207 	int prop, lid, toall, signo = ksi->ksi_signo;
   1208 	struct lwp *l;
   1209 	ksiginfo_t *kp;
   1210 	ksiginfoq_t kq;
   1211 	sig_t action;
   1212 
   1213 	LOCK_ASSERT(mutex_owned(&proclist_mutex));
   1214 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1215 	KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
   1216 	KASSERT(signo > 0 && signo < NSIG);
   1217 
   1218 	/*
   1219 	 * If the process is being created by fork, is a zombie or is
   1220 	 * exiting, then just drop the signal here and bail out.
   1221 	 */
   1222 	if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
   1223 		return;
   1224 
   1225 	/*
   1226 	 * Notify any interested parties of the signal.
   1227 	 */
   1228 	KNOTE(&p->p_klist, NOTE_SIGNAL | signo);
   1229 
   1230 	/*
   1231 	 * Some signals including SIGKILL must act on the entire process.
   1232 	 */
   1233 	kp = NULL;
   1234 	prop = sigprop[signo];
   1235 	toall = ((prop & SA_TOALL) != 0);
   1236 
   1237 	if (toall)
   1238 		lid = 0;
   1239 	else
   1240 		lid = ksi->ksi_lid;
   1241 
   1242 	/*
   1243 	 * If proc is traced, always give parent a chance.
   1244 	 */
   1245 	if (p->p_slflag & PSL_TRACED) {
   1246 		action = SIG_DFL;
   1247 
   1248 		if (lid == 0) {
   1249 			/*
   1250 			 * If the process is being traced and the signal
   1251 			 * is being caught, make sure to save any ksiginfo.
   1252 			 */
   1253 			if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
   1254 				return;
   1255 			sigput(&p->p_sigpend, p, kp);
   1256 		}
   1257 	} else {
   1258 		/*
   1259 		 * If the signal was the result of a trap and is not being
   1260 		 * caught, then reset it to default action so that the
   1261 		 * process dumps core immediately.
   1262 		 */
   1263 		if (KSI_TRAP_P(ksi)) {
   1264 			if (!sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
   1265 				sigdelset(&p->p_sigctx.ps_sigignore, signo);
   1266 				SIGACTION(p, signo).sa_handler = SIG_DFL;
   1267 			}
   1268 		}
   1269 
   1270 		/*
   1271 		 * If the signal is being ignored, then drop it.  Note: we
   1272 		 * don't set SIGCONT in ps_sigignore, and if it is set to
   1273 		 * SIG_IGN, action will be SIG_DFL here.
   1274 		 */
   1275 		if (sigismember(&p->p_sigctx.ps_sigignore, signo))
   1276 			return;
   1277 
   1278 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signo))
   1279 			action = SIG_CATCH;
   1280 		else {
   1281 			action = SIG_DFL;
   1282 
   1283 			/*
   1284 			 * If sending a tty stop signal to a member of an
   1285 			 * orphaned process group, discard the signal here if
   1286 			 * the action is default; don't stop the process below
   1287 			 * if sleeping, and don't clear any pending SIGCONT.
   1288 			 */
   1289 			if (prop & SA_TTYSTOP &&
   1290 			    (p->p_sflag & PS_ORPHANPG) != 0)
   1291 				return;
   1292 
   1293 			if (prop & SA_KILL && p->p_nice > NZERO)
   1294 				p->p_nice = NZERO;
   1295 		}
   1296 	}
   1297 
   1298 	/*
   1299 	 * If stopping or continuing a process, discard any pending
   1300 	 * signals that would do the inverse.
   1301 	 */
   1302 	if ((prop & (SA_CONT | SA_STOP)) != 0) {
   1303 		ksiginfo_queue_init(&kq);
   1304 		if ((prop & SA_CONT) != 0)
   1305 			sigclear(&p->p_sigpend, &stopsigmask, &kq);
   1306 		if ((prop & SA_STOP) != 0)
   1307 			sigclear(&p->p_sigpend, &contsigmask, &kq);
   1308 		ksiginfo_queue_drain(&kq);	/* XXXSMP */
   1309 	}
   1310 
   1311 	/*
   1312 	 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
   1313 	 * please!), check if any LWPs are waiting on it.  If yes, pass on
   1314 	 * the signal info.  The signal won't be processed further here.
   1315 	 */
   1316 	if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) &&
   1317 	    p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 &&
   1318 	    sigunwait(p, ksi))
   1319 		return;
   1320 
   1321 	/*
   1322 	 * XXXSMP Should be allocated by the caller, we're holding locks
   1323 	 * here.
   1324 	 */
   1325 	if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
   1326 		return;
   1327 
   1328 	/*
   1329 	 * LWP private signals are easy - just find the LWP and post
   1330 	 * the signal to it.
   1331 	 */
   1332 	if (lid != 0) {
   1333 		l = lwp_find(p, lid);
   1334 		if (l != NULL) {
   1335 			sigput(&l->l_sigpend, p, kp);
   1336 			mb_write();
   1337 			(void)sigpost(l, action, prop, kp->ksi_signo);
   1338 		}
   1339 		goto out;
   1340 	}
   1341 
   1342 	/*
   1343 	 * Some signals go to all LWPs, even for SA processes.
   1344 	 */
   1345 	if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
   1346 		if ((p->p_slflag & PSL_TRACED) != 0)
   1347 			goto deliver;
   1348 
   1349 		/*
   1350 		 * If SIGCONT is default (or ignored) and process is
   1351 		 * asleep, we are finished; the process should not
   1352 		 * be awakened.
   1353 		 */
   1354 		if ((prop & SA_CONT) != 0 && action == SIG_DFL)
   1355 			goto out;
   1356 
   1357 		if ((prop & SA_STOP) != 0 && action == SIG_DFL) {
   1358 			/*
   1359 			 * If a child holding parent blocked, stopping could
   1360 			 * cause deadlock: discard the signal.
   1361 			 */
   1362 			if ((p->p_sflag & PS_PPWAIT) == 0) {
   1363 				p->p_xstat = signo;
   1364 				proc_stop(p, 1, signo);
   1365 			}
   1366 			goto out;
   1367 		} else {
   1368 			/*
   1369 			 * Stop signals with the default action are handled
   1370 			 * specially in issignal(), and so are not enqueued.
   1371 			 */
   1372 			sigput(&p->p_sigpend, p, kp);
   1373 		}
   1374 	} else {
   1375 		/*
   1376 		 * Process is stopped.  If traced, then no further action is
   1377 		 * necessary.
   1378 		 */
   1379 		if ((p->p_slflag & PSL_TRACED) != 0 && signo != SIGKILL)
   1380 			goto out;
   1381 
   1382 		if ((prop & (SA_CONT | SA_KILL)) != 0) {
   1383 			/*
   1384 			 * Re-adjust p_nstopchild if the process wasn't
   1385 			 * collected by its parent.
   1386 			 */
   1387 			p->p_stat = SACTIVE;
   1388 			p->p_sflag &= ~PS_STOPPING;
   1389 			if (!p->p_waited)
   1390 				p->p_pptr->p_nstopchild--;
   1391 
   1392 			/*
   1393 			 * If SIGCONT is default (or ignored), we continue
   1394 			 * the process but don't leave the signal in
   1395 			 * ps_siglist, as it has no further action.  If
   1396 			 * SIGCONT is held, we continue the process and
   1397 			 * leave the signal in ps_siglist.  If the process
   1398 			 * catches SIGCONT, let it handle the signal itself.
   1399 			 * If it isn't waiting on an event, then it goes
   1400 			 * back to run state.  Otherwise, process goes back
   1401 			 * to sleep state.
   1402 			 */
   1403 			if ((prop & SA_CONT) == 0 || action != SIG_DFL)
   1404 				sigput(&p->p_sigpend, p, kp);
   1405 		} else if ((prop & SA_STOP) != 0) {
   1406 			/*
   1407 			 * Already stopped, don't need to stop again.
   1408 			 * (If we did the shell could get confused.)
   1409 			 */
   1410 			goto out;
   1411 		} else
   1412 			sigput(&p->p_sigpend, p, kp);
   1413 	}
   1414 
   1415  deliver:
   1416 	/*
   1417 	 * Before we set L_PENDSIG on any LWP, ensure that the signal is
   1418 	 * visible on the per process list (for sigispending()).  This
   1419 	 * is unlikely to be needed in practice, but...
   1420 	 */
   1421 	mb_write();
   1422 
   1423 	/*
   1424 	 * Try to find an LWP that can take the signal.
   1425 	 */
   1426 	LIST_FOREACH(l, &p->p_lwps, l_sibling)
   1427 		if (sigpost(l, action, prop, kp->ksi_signo) && !toall)
   1428 			break;
   1429 
   1430  out:
   1431  	/*
   1432  	 * If the ksiginfo wasn't, then bin it.  XXXSMP freeing memory with
   1433  	 * locks held.
   1434  	 */
   1435  	ksiginfo_free(kp);
   1436 }
   1437 
   1438 void
   1439 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
   1440 {
   1441 	struct proc *p = l->l_proc;
   1442 
   1443 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1444 
   1445 	(*p->p_emul->e_sendsig)(ksi, mask);
   1446 }
   1447 
   1448 /*
   1449  * Stop the current process and switch away when being stopped or traced.
   1450  */
   1451 void
   1452 sigswitch(boolean_t ppsig, int ppmask, int signo)
   1453 {
   1454 	struct lwp *l = curlwp, *l2;
   1455 	struct proc *p = l->l_proc;
   1456 #ifdef MULTIPROCESSOR
   1457 	int biglocks;
   1458 #endif
   1459 
   1460 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1461 
   1462 	/*
   1463 	 * On entry we know that the process needs to stop.  If it's
   1464 	 * the result of a 'sideways' stop signal that has been sourced
   1465 	 * through issignal(), then stop other LWPs in the process too.
   1466 	 */
   1467 	if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
   1468 		/*
   1469 		 * Set the stopping indicator and bring all sleeping LWPs
   1470 		 * to a halt so they are included in p->p_nrlwps
   1471 		 */
   1472 		p->p_sflag |= (PS_STOPPING | PS_NOTIFYSTOP);
   1473 		mb_write();
   1474 
   1475 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1476 			lwp_lock(l2);
   1477 			if (l2->l_stat == LSSLEEP &&
   1478 			    (l2->l_flag & L_SINTR) != 0) {
   1479 				l2->l_stat = LSSTOP;
   1480 				p->p_nrlwps--;
   1481 			}
   1482 			lwp_unlock(l2);
   1483 		}
   1484 
   1485 		/*
   1486 		 * Have the remaining LWPs come to a halt, and trigger
   1487 		 * proc_stop_callout() to ensure that they do.
   1488 		 */
   1489 		KASSERT(signo != 0);
   1490 
   1491 		if (p->p_nrlwps > 1) {
   1492 			LIST_FOREACH(l2, &p->p_lwps, l_sibling)
   1493 				sigpost(l2, SIG_DFL, SA_STOP, signo);
   1494 			callout_schedule(&proc_stop_ch, 1);
   1495 		}
   1496 	}
   1497 
   1498 	/*
   1499 	 * If we are the last live LWP, and the stop was a result of
   1500 	 * a new signal, then signal the parent.
   1501 	 */
   1502 	if ((p->p_sflag & PS_STOPPING) != 0) {
   1503 		if (!mutex_tryenter(&proclist_mutex)) {
   1504 			mutex_exit(&p->p_smutex);
   1505 			mutex_enter(&proclist_mutex);
   1506 			mutex_enter(&p->p_smutex);
   1507 		}
   1508 
   1509 		if (p->p_nrlwps == 1 && (p->p_sflag & PS_STOPPING) != 0) {
   1510 			p->p_sflag &= ~PS_STOPPING;
   1511 			p->p_stat = SSTOP;
   1512 			p->p_waited = 0;
   1513 			p->p_pptr->p_nstopchild++;
   1514 			if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
   1515 				/*
   1516 				 * Note that child_psignal() will drop
   1517 				 * p->p_smutex briefly.
   1518 				 */
   1519 				if (ppsig)
   1520 					child_psignal(p, ppmask);
   1521 				cv_broadcast(&p->p_pptr->p_waitcv);
   1522 			}
   1523 		}
   1524 
   1525 		mutex_exit(&proclist_mutex);
   1526 	}
   1527 
   1528 	/*
   1529 	 * Unlock and switch away.
   1530 	 */
   1531 	KERNEL_UNLOCK_ALL(l, &biglocks);
   1532 	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
   1533 		p->p_nrlwps--;
   1534 		lwp_lock(l);
   1535 		KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSLEEP);
   1536 		l->l_stat = LSSTOP;
   1537 		lwp_unlock(l);
   1538 	}
   1539 
   1540 	mutex_exit(&p->p_smutex);
   1541 	lwp_lock(l);
   1542 	mi_switch(l, NULL);
   1543 	KERNEL_LOCK(biglocks, l);
   1544 	mutex_enter(&p->p_smutex);
   1545 }
   1546 
   1547 /*
   1548  * Check for a signal from the debugger.
   1549  */
   1550 int
   1551 sigchecktrace(sigpend_t **spp)
   1552 {
   1553 	struct lwp *l = curlwp;
   1554 	struct proc *p = l->l_proc;
   1555 	int signo;
   1556 
   1557 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1558 
   1559 	/*
   1560 	 * If we are no longer being traced, or the parent didn't
   1561 	 * give us a signal, look for more signals.
   1562 	 */
   1563 	if ((p->p_slflag & PSL_TRACED) == 0 || p->p_xstat == 0)
   1564 		return 0;
   1565 
   1566 	/* If there's a pending SIGKILL, process it immediately. */
   1567 	if (sigismember(&p->p_sigpend.sp_set, SIGKILL))
   1568 		return 0;
   1569 
   1570 	/*
   1571 	 * If the new signal is being masked, look for other signals.
   1572 	 * `p->p_sigctx.ps_siglist |= mask' is done in setrunnable().
   1573 	 */
   1574 	signo = p->p_xstat;
   1575 	p->p_xstat = 0;
   1576 	if ((sigprop[signo] & SA_TOLWP) != 0)
   1577 		*spp = &l->l_sigpend;
   1578 	else
   1579 		*spp = &p->p_sigpend;
   1580 	if (sigismember(&l->l_sigmask, signo))
   1581 		signo = 0;
   1582 
   1583 	return signo;
   1584 }
   1585 
   1586 /*
   1587  * If the current process has received a signal (should be caught or cause
   1588  * termination, should interrupt current syscall), return the signal number.
   1589  *
   1590  * Stop signals with default action are processed immediately, then cleared;
   1591  * they aren't returned.  This is checked after each entry to the system for
   1592  * a syscall or trap.
   1593  *
   1594  * We will also return -1 if the process is exiting and the current LWP must
   1595  * follow suit.
   1596  *
   1597  * Note that we may be called while on a sleep queue, so MUST NOT sleep.  We
   1598  * can switch away, though.
   1599  */
   1600 int
   1601 issignal(struct lwp *l)
   1602 {
   1603 	struct proc *p = l->l_proc;
   1604 	int signo = 0, prop;
   1605 	sigpend_t *sp = NULL;
   1606 	sigset_t ss;
   1607 
   1608 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1609 
   1610 	for (;;) {
   1611 		/* Discard any signals that we have decided not to take. */
   1612 		if (signo != 0)
   1613 			(void)sigget(sp, NULL, signo, NULL);
   1614 
   1615 		/*
   1616 		 * If the process is stopped/stopping, then stop ourselves
   1617 		 * now that we're on the kernel/userspace boundary.  When
   1618 		 * we awaken, check for a signal from the debugger.
   1619 		 */
   1620 		if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
   1621 			sigswitch(TRUE, PS_NOCLDSTOP, 0);
   1622 			signo = sigchecktrace(&sp);
   1623 		} else
   1624 			signo = 0;
   1625 
   1626 		/*
   1627 		 * If the debugger didn't provide a signal, find a pending
   1628 		 * signal from our set.  Check per-LWP signals first, and
   1629 		 * then per-process.
   1630 		 */
   1631 		if (signo == 0) {
   1632 			sp = &l->l_sigpend;
   1633 			ss = sp->sp_set;
   1634 			if ((p->p_sflag & PS_PPWAIT) != 0)
   1635 				sigminusset(&stopsigmask, &ss);
   1636 			sigminusset(&l->l_sigmask, &ss);
   1637 
   1638 			if ((signo = firstsig(&ss)) == 0) {
   1639 				sp = &p->p_sigpend;
   1640 				ss = sp->sp_set;
   1641 				if ((p->p_sflag & PS_PPWAIT) != 0)
   1642 					sigminusset(&stopsigmask, &ss);
   1643 				sigminusset(&l->l_sigmask, &ss);
   1644 
   1645 				if ((signo = firstsig(&ss)) == 0) {
   1646 					/*
   1647 					 * No signal pending - clear the
   1648 					 * indicator and bail out.
   1649 					 */
   1650 					lwp_lock(l);
   1651 					l->l_flag &= ~L_PENDSIG;
   1652 					lwp_unlock(l);
   1653 					sp = NULL;
   1654 					break;
   1655 				}
   1656 			}
   1657 		}
   1658 
   1659 		/*
   1660 		 * We should see pending but ignored signals only if
   1661 		 * we are being traced.
   1662 		 */
   1663 		if (sigismember(&p->p_sigctx.ps_sigignore, signo) &&
   1664 		    (p->p_slflag & PSL_TRACED) == 0) {
   1665 			/* Discard the signal. */
   1666 			continue;
   1667 		}
   1668 
   1669 		/*
   1670 		 * If traced, always stop, and stay stopped until released
   1671 		 * by the debugger.  If the our parent process is waiting
   1672 		 * for us, don't hang as we could deadlock.
   1673 		 */
   1674 		if ((p->p_slflag & PSL_TRACED) != 0 &&
   1675 		    (p->p_sflag & PS_PPWAIT) == 0 && signo != SIGKILL) {
   1676 			/* Take the signal. */
   1677 			(void)sigget(sp, NULL, signo, NULL);
   1678 			p->p_xstat = signo;
   1679 
   1680 			/* Emulation-specific handling of signal trace */
   1681 			if (p->p_emul->e_tracesig == NULL ||
   1682 			    (*p->p_emul->e_tracesig)(p, signo) == 0)
   1683 				sigswitch(!(p->p_slflag & PSL_FSTRACE), 0,
   1684 				    signo);
   1685 
   1686 			/* Check for a signal from the debugger. */
   1687 			if ((signo = sigchecktrace(&sp)) == 0)
   1688 				continue;
   1689 		}
   1690 
   1691 		prop = sigprop[signo];
   1692 
   1693 		/*
   1694 		 * Decide whether the signal should be returned.
   1695 		 */
   1696 		switch ((long)SIGACTION(p, signo).sa_handler) {
   1697 		case (long)SIG_DFL:
   1698 			/*
   1699 			 * Don't take default actions on system processes.
   1700 			 */
   1701 			if (p->p_pid <= 1) {
   1702 #ifdef DIAGNOSTIC
   1703 				/*
   1704 				 * Are you sure you want to ignore SIGSEGV
   1705 				 * in init? XXX
   1706 				 */
   1707 				printf_nolog("Process (pid %d) got sig %d\n",
   1708 				    p->p_pid, signo);
   1709 #endif
   1710 				continue;
   1711 			}
   1712 
   1713 			/*
   1714 			 * If there is a pending stop signal to process with
   1715 			 * default action, stop here, then clear the signal.
   1716 			 * However, if process is member of an orphaned
   1717 			 * process group, ignore tty stop signals.
   1718 			 */
   1719 			if (prop & SA_STOP) {
   1720 				if (p->p_slflag & PSL_TRACED ||
   1721 		    		    ((p->p_sflag & PS_ORPHANPG) != 0 &&
   1722 				    prop & SA_TTYSTOP)) {
   1723 				    	/* Ignore the signal. */
   1724 					continue;
   1725 				}
   1726 				/* Take the signal. */
   1727 				(void)sigget(sp, NULL, signo, NULL);
   1728 				p->p_xstat = signo;
   1729 				signo = 0;
   1730 				sigswitch(TRUE, PS_NOCLDSTOP, p->p_xstat);
   1731 			} else if (prop & SA_IGNORE) {
   1732 				/*
   1733 				 * Except for SIGCONT, shouldn't get here.
   1734 				 * Default action is to ignore; drop it.
   1735 				 */
   1736 				continue;
   1737 			}
   1738 			break;
   1739 
   1740 		case (long)SIG_IGN:
   1741 #ifdef DEBUG_ISSIGNAL
   1742 			/*
   1743 			 * Masking above should prevent us ever trying
   1744 			 * to take action on an ignored signal other
   1745 			 * than SIGCONT, unless process is traced.
   1746 			 */
   1747 			if ((prop & SA_CONT) == 0 &&
   1748 			    (p->p_slflag & PSL_TRACED) == 0)
   1749 				printf_nolog("issignal\n");
   1750 #endif
   1751 			continue;
   1752 
   1753 		default:
   1754 			/*
   1755 			 * This signal has an action, let postsig() process
   1756 			 * it.
   1757 			 */
   1758 			break;
   1759 		}
   1760 
   1761 		break;
   1762 	}
   1763 
   1764 	l->l_sigpendset = sp;
   1765 	return signo;
   1766 }
   1767 
   1768 /*
   1769  * Take the action for the specified signal
   1770  * from the current set of pending signals.
   1771  */
   1772 void
   1773 postsig(int signo)
   1774 {
   1775 	struct lwp	*l;
   1776 	struct proc	*p;
   1777 	struct sigacts	*ps;
   1778 	sig_t		action;
   1779 	sigset_t	*returnmask;
   1780 	ksiginfo_t	ksi;
   1781 
   1782 	l = curlwp;
   1783 	p = l->l_proc;
   1784 	ps = p->p_sigacts;
   1785 
   1786 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1787 	KASSERT(signo > 0);
   1788 
   1789 	/*
   1790 	 * Set the new mask value and also defer further occurrences of this
   1791 	 * signal.
   1792 	 *
   1793 	 * Special case: user has done a sigpause.  Here the current mask is
   1794 	 * not of interest, but rather the mask from before the sigpause is
   1795 	 * what we want restored after the signal processing is completed.
   1796 	 */
   1797 	if (l->l_sigrestore) {
   1798 		returnmask = &l->l_sigoldmask;
   1799 		l->l_sigrestore = 0;
   1800 	} else
   1801 		returnmask = &l->l_sigmask;
   1802 
   1803 	/*
   1804 	 * Commit to taking the signal before releasing the mutex.
   1805 	 */
   1806 	action = SIGACTION_PS(ps, signo).sa_handler;
   1807 	p->p_stats->p_ru.ru_nsignals++;
   1808 	sigget(l->l_sigpendset, &ksi, signo, NULL);
   1809 
   1810 #ifdef KTRACE
   1811 	if (KTRPOINT(p, KTR_PSIG)) {
   1812 		mutex_exit(&p->p_smutex);
   1813 		ktrpsig(l, signo, action, returnmask, NULL);
   1814 		mutex_enter(&p->p_smutex);
   1815 	}
   1816 #endif
   1817 
   1818 	if (action == SIG_DFL) {
   1819 		/*
   1820 		 * Default action, where the default is to kill
   1821 		 * the process.  (Other cases were ignored above.)
   1822 		 */
   1823 		sigexit(l, signo);
   1824 		return;
   1825 	}
   1826 
   1827 	/*
   1828 	 * If we get here, the signal must be caught.
   1829 	 */
   1830 #ifdef DIAGNOSTIC
   1831 	if (action == SIG_IGN || sigismember(&l->l_sigmask, signo))
   1832 		panic("postsig action");
   1833 #endif
   1834 
   1835 	kpsendsig(l, &ksi, returnmask);
   1836 }
   1837 
   1838 /*
   1839  * sendsig_reset:
   1840  *
   1841  *	Reset the signal action.  Called from emulation specific sendsig()
   1842  *	before unlocking to deliver the signal.
   1843  */
   1844 void
   1845 sendsig_reset(struct lwp *l, int signo)
   1846 {
   1847 	struct proc *p = l->l_proc;
   1848 	struct sigacts *ps = p->p_sigacts;
   1849 
   1850 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1851 
   1852 	p->p_sigctx.ps_lwp = 0;
   1853 	p->p_sigctx.ps_code = 0;
   1854 	p->p_sigctx.ps_signo = 0;
   1855 
   1856 	sigplusset(&SIGACTION_PS(ps, signo).sa_mask, &l->l_sigmask);
   1857 	if (SIGACTION_PS(ps, signo).sa_flags & SA_RESETHAND) {
   1858 		sigdelset(&p->p_sigctx.ps_sigcatch, signo);
   1859 		if (signo != SIGCONT && sigprop[signo] & SA_IGNORE)
   1860 			sigaddset(&p->p_sigctx.ps_sigignore, signo);
   1861 		SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
   1862 	}
   1863 }
   1864 
   1865 /*
   1866  * Kill the current process for stated reason.
   1867  */
   1868 void
   1869 killproc(struct proc *p, const char *why)
   1870 {
   1871 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
   1872 	uprintf_locked("sorry, pid %d was killed: %s\n", p->p_pid, why);
   1873 	mutex_enter(&proclist_mutex);	/* XXXSMP */
   1874 	psignal(p, SIGKILL);
   1875 	mutex_exit(&proclist_mutex);	/* XXXSMP */
   1876 }
   1877 
   1878 /*
   1879  * Force the current process to exit with the specified signal, dumping core
   1880  * if appropriate.  We bypass the normal tests for masked and caught
   1881  * signals, allowing unrecoverable failures to terminate the process without
   1882  * changing signal state.  Mark the accounting record with the signal
   1883  * termination.  If dumping core, save the signal number for the debugger.
   1884  * Calls exit and does not return.
   1885  */
   1886 void
   1887 sigexit(struct lwp *l, int signo)
   1888 {
   1889 	int exitsig, error, docore;
   1890 	struct proc *p;
   1891 	struct lwp *t;
   1892 
   1893 	p = l->l_proc;
   1894 
   1895 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1896 	KERNEL_UNLOCK_ALL(l, NULL);
   1897 
   1898 	/*
   1899 	 * Don't permit coredump() multiple times in the same process.
   1900 	 * Call back into sigexit, where we will be suspended until
   1901 	 * the deed is done.  Note that this is a recursive call, but
   1902 	 * L_WCORE will prevent us from coming back this way.
   1903 	 */
   1904 	if ((p->p_sflag & PS_WCORE) != 0) {
   1905 		lwp_lock(l);
   1906 		l->l_flag |= (L_WCORE | L_WEXIT | L_WSUSPEND);
   1907 		lwp_unlock(l);
   1908 		mutex_exit(&p->p_smutex);
   1909 		lwp_userret(l);
   1910 #ifdef DIAGNOSTIC
   1911 		panic("sigexit");
   1912 #endif
   1913 		/* NOTREACHED */
   1914 	}
   1915 
   1916 	/*
   1917 	 * Prepare all other LWPs for exit.  If dumping core, suspend them
   1918 	 * so that their registers are available long enough to be dumped.
   1919  	 */
   1920 	if ((docore = (sigprop[signo] & SA_CORE)) != 0) {
   1921 		p->p_sflag |= PS_WCORE;
   1922 		for (;;) {
   1923 			LIST_FOREACH(t, &p->p_lwps, l_sibling) {
   1924 				lwp_lock(t);
   1925 				if (t == l) {
   1926 					t->l_flag &= ~L_WSUSPEND;
   1927 					lwp_unlock(t);
   1928 					continue;
   1929 				}
   1930 				t->l_flag |= (L_WCORE | L_WEXIT);
   1931 				lwp_suspend(l, t);
   1932 			}
   1933 
   1934 			if (p->p_nrlwps == 1)
   1935 				break;
   1936 
   1937 			/*
   1938 			 * Kick any LWPs sitting in lwp_wait1(), and wait
   1939 			 * for everyone else to stop before proceeding.
   1940 			 */
   1941 			p->p_nlwpwait++;
   1942 			cv_broadcast(&p->p_lwpcv);
   1943 			cv_wait(&p->p_lwpcv, &p->p_smutex);
   1944 			p->p_nlwpwait--;
   1945 		}
   1946 	}
   1947 
   1948 	exitsig = signo;
   1949 	p->p_acflag |= AXSIG;
   1950 	p->p_sigctx.ps_signo = signo;
   1951 	mutex_exit(&p->p_smutex);
   1952 
   1953 	KERNEL_LOCK(1, l);
   1954 
   1955 	if (docore) {
   1956 		if ((error = coredump(l, NULL)) == 0)
   1957 			exitsig |= WCOREFLAG;
   1958 
   1959 		if (kern_logsigexit) {
   1960 			int uid = l->l_cred ?
   1961 			    (int)kauth_cred_geteuid(l->l_cred) : -1;
   1962 
   1963 			if (error)
   1964 				log(LOG_INFO, lognocoredump, p->p_pid,
   1965 				    p->p_comm, uid, signo, error);
   1966 			else
   1967 				log(LOG_INFO, logcoredump, p->p_pid,
   1968 				    p->p_comm, uid, signo);
   1969 		}
   1970 
   1971 #ifdef PAX_SEGVGUARD
   1972 		pax_segvguard(l, p->p_textvp, p->p_comm, TRUE);
   1973 #endif /* PAX_SEGVGUARD */
   1974 	}
   1975 
   1976 	/* Acquire the sched state mutex.  exit1() will release it. */
   1977 	mutex_enter(&p->p_smutex);
   1978 
   1979 	/* No longer dumping core. */
   1980 	p->p_sflag &= ~PS_WCORE;
   1981 
   1982 	exit1(l, W_EXITCODE(0, exitsig));
   1983 	/* NOTREACHED */
   1984 }
   1985 
   1986 /*
   1987  * Put process 'p' into the stopped state and optionally, notify the parent.
   1988  */
   1989 void
   1990 proc_stop(struct proc *p, int notify, int signo)
   1991 {
   1992 	struct lwp *l;
   1993 
   1994 	LOCK_ASSERT(mutex_owned(&proclist_mutex));
   1995 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1996 
   1997 	/*
   1998 	 * First off, set the stopping indicator and bring all sleeping
   1999 	 * LWPs to a halt so they are included in p->p_nrlwps.  We musn't
   2000 	 * unlock between here and the p->p_nrlwps check below.
   2001 	 */
   2002 	p->p_sflag |= PS_STOPPING;
   2003 	mb_write();
   2004 
   2005 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2006 		lwp_lock(l);
   2007 		if (l->l_stat == LSSLEEP && (l->l_flag & L_SINTR) != 0) {
   2008 			l->l_stat = LSSTOP;
   2009 			p->p_nrlwps--;
   2010 		}
   2011 		lwp_unlock(l);
   2012 	}
   2013 
   2014 	/*
   2015 	 * If there are no LWPs available to take the signal, then we
   2016 	 * signal the parent process immediately.  Otherwise, the last
   2017 	 * LWP to stop will take care of it.
   2018 	 */
   2019 	if (notify)
   2020 		p->p_sflag |= PS_NOTIFYSTOP;
   2021 	else
   2022 		p->p_sflag &= ~PS_NOTIFYSTOP;
   2023 
   2024 	if (p->p_nrlwps == 0) {
   2025 		p->p_sflag &= ~PS_STOPPING;
   2026 		p->p_stat = SSTOP;
   2027 		p->p_waited = 0;
   2028 		p->p_pptr->p_nstopchild++;
   2029 
   2030 		if (notify) {
   2031 			child_psignal(p, PS_NOCLDSTOP);
   2032 			cv_broadcast(&p->p_pptr->p_waitcv);
   2033 		}
   2034 	} else {
   2035 		/*
   2036 		 * Have the remaining LWPs come to a halt, and trigger
   2037 		 * proc_stop_callout() to ensure that they do.
   2038 		 */
   2039 		LIST_FOREACH(l, &p->p_lwps, l_sibling)
   2040 			sigpost(l, SIG_DFL, SA_STOP, signo);
   2041 		callout_schedule(&proc_stop_ch, 1);
   2042 	}
   2043 }
   2044 
   2045 /*
   2046  * When stopping a process, we do not immediatly set sleeping LWPs stopped,
   2047  * but wait for them to come to a halt at the kernel-user boundary.  This is
   2048  * to allow LWPs to release any locks that they may hold before stopping.
   2049  *
   2050  * Non-interruptable sleeps can be long, and there is the potential for an
   2051  * LWP to begin sleeping interruptably soon after the process has been set
   2052  * stopping (PS_STOPPING).  These LWPs will not notice that the process is
   2053  * stopping, and so complete halt of the process and the return of status
   2054  * information to the parent could be delayed indefinitely.
   2055  *
   2056  * To handle this race, proc_stop_callout() runs once per tick while there
   2057  * are stopping processes it the system.  It sets LWPs that are sleeping
   2058  * interruptably into the LSSTOP state.
   2059  *
   2060  * Note that we are not concerned about keeping all LWPs stopped while the
   2061  * process is stopped: stopped LWPs can awaken briefly to handle signals.
   2062  * What we do need to ensure is that all LWPs in a stopping process have
   2063  * stopped at least once, so that notification can be sent to the parent
   2064  * process.
   2065  */
   2066 static void
   2067 proc_stop_callout(void *cookie)
   2068 {
   2069 	boolean_t more, restart;
   2070 	struct proc *p;
   2071 	struct lwp *l;
   2072 
   2073 	(void)cookie;
   2074 
   2075 	do {
   2076 		restart = FALSE;
   2077 		more = FALSE;
   2078 
   2079 		mutex_enter(&proclist_mutex);
   2080 		PROCLIST_FOREACH(p, &allproc) {
   2081 			mutex_enter(&p->p_smutex);
   2082 
   2083 			if ((p->p_sflag & PS_STOPPING) == 0) {
   2084 				mutex_exit(&p->p_smutex);
   2085 				continue;
   2086 			}
   2087 
   2088 			/* Stop any LWPs sleeping interruptably. */
   2089 			LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2090 				lwp_lock(l);
   2091 				if (l->l_stat == LSSLEEP &&
   2092 				    (l->l_flag & L_SINTR) != 0) {
   2093 				    	l->l_stat = LSSTOP;
   2094 				    	p->p_nrlwps--;
   2095 				}
   2096 				lwp_unlock(l);
   2097 			}
   2098 
   2099 			if (p->p_nrlwps == 0) {
   2100 				/*
   2101 				 * We brought the process to a halt.
   2102 				 * Mark it as stopped and notify the
   2103 				 * parent.
   2104 				 */
   2105 				p->p_sflag &= ~PS_STOPPING;
   2106 				p->p_stat = SSTOP;
   2107 				p->p_waited = 0;
   2108 				p->p_pptr->p_nstopchild++;
   2109 				if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
   2110 					/*
   2111 					 * Note that child_psignal() will
   2112 					 * drop p->p_smutex briefly.
   2113 					 * Arrange to restart and check
   2114 					 * all processes again.
   2115 					 */
   2116 					restart = TRUE;
   2117 					child_psignal(p, PS_NOCLDSTOP);
   2118 					cv_broadcast(&p->p_pptr->p_waitcv);
   2119 				}
   2120 			} else
   2121 				more = TRUE;
   2122 
   2123 			mutex_exit(&p->p_smutex);
   2124 			if (restart)
   2125 				break;
   2126 		}
   2127 		mutex_exit(&proclist_mutex);
   2128 	} while (restart);
   2129 
   2130 	/*
   2131 	 * If we noted processes that are stopping but still have
   2132 	 * running LWPs, then arrange to check again in 1 tick.
   2133 	 */
   2134 	if (more)
   2135 		callout_schedule(&proc_stop_ch, 1);
   2136 }
   2137 
   2138 /*
   2139  * Given a process in state SSTOP, set the state back to SACTIVE and
   2140  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
   2141  */
   2142 void
   2143 proc_unstop(struct proc *p)
   2144 {
   2145 	struct lwp *l;
   2146 	int sig;
   2147 
   2148 	LOCK_ASSERT(mutex_owned(&proclist_mutex));
   2149 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   2150 
   2151 	p->p_stat = SACTIVE;
   2152 	p->p_sflag &= ~PS_STOPPING;
   2153 	sig = p->p_xstat;
   2154 
   2155 	if (!p->p_waited)
   2156 		p->p_pptr->p_nstopchild--;
   2157 
   2158 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2159 		lwp_lock(l);
   2160 		if (l->l_stat != LSSTOP) {
   2161 			lwp_unlock(l);
   2162 			continue;
   2163 		}
   2164 		if (l->l_wchan == NULL) {
   2165 			setrunnable(l);
   2166 			continue;
   2167 		}
   2168 		l->l_stat = LSSLEEP;
   2169 		if (sig && (l->l_flag & L_SINTR) != 0) {
   2170 		        setrunnable(l);
   2171 		        sig = 0;
   2172 		} else
   2173 			lwp_unlock(l);
   2174 	}
   2175 }
   2176 
   2177 static int
   2178 filt_sigattach(struct knote *kn)
   2179 {
   2180 	struct proc *p = curproc;
   2181 
   2182 	kn->kn_ptr.p_proc = p;
   2183 	kn->kn_flags |= EV_CLEAR;               /* automatically set */
   2184 
   2185 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
   2186 
   2187 	return (0);
   2188 }
   2189 
   2190 static void
   2191 filt_sigdetach(struct knote *kn)
   2192 {
   2193 	struct proc *p = kn->kn_ptr.p_proc;
   2194 
   2195 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
   2196 }
   2197 
   2198 /*
   2199  * signal knotes are shared with proc knotes, so we apply a mask to
   2200  * the hint in order to differentiate them from process hints.  This
   2201  * could be avoided by using a signal-specific knote list, but probably
   2202  * isn't worth the trouble.
   2203  */
   2204 static int
   2205 filt_signal(struct knote *kn, long hint)
   2206 {
   2207 
   2208 	if (hint & NOTE_SIGNAL) {
   2209 		hint &= ~NOTE_SIGNAL;
   2210 
   2211 		if (kn->kn_id == hint)
   2212 			kn->kn_data++;
   2213 	}
   2214 	return (kn->kn_data != 0);
   2215 }
   2216 
   2217 const struct filterops sig_filtops = {
   2218 	0, filt_sigattach, filt_sigdetach, filt_signal
   2219 };
   2220