Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.251
      1 /*	$NetBSD: kern_sig.c,v 1.251 2007/03/09 14:11:25 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.251 2007/03/09 14:11:25 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(bool, 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 		KASSERT(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 	KASSERT(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 &= ~(PK_NOCLDWAIT | PK_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 	KASSERT(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 	KASSERT(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 	KASSERT(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 	KASSERT(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 	mutex_enter(&proclist_lock);
    812 	if (all) {
    813 		/*
    814 		 * broadcast
    815 		 */
    816 		PROCLIST_FOREACH(p, &allproc) {
    817 			if (p->p_pid <= 1 || p->p_flag & PK_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 & PK_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 	mutex_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 	KASSERT(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 	KASSERT(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 	KASSERT(mutex_owned(&proclist_mutex));
    951 	KASSERT(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 	KASSERT(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 	KASSERT(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 	KASSERT(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 |= LW_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 killing the process, make it run fast.
   1069 	 */
   1070 	if (__predict_false((prop & SA_KILL) != 0) &&
   1071 	    action == SIG_DFL && l->l_priority > PUSER)
   1072 		lwp_changepri(l, PUSER);
   1073 
   1074 	/*
   1075 	 * If the LWP is running or on a run queue, then we win.  If it's
   1076 	 * sleeping interruptably, wake it and make it take the signal.  If
   1077 	 * the sleep isn't interruptable, then the chances are it will get
   1078 	 * to see the signal soon anyhow.  If suspended, it can't take the
   1079 	 * signal right now.  If it's LWP private or for all LWPs, save it
   1080 	 * for later; otherwise punt.
   1081 	 */
   1082 	rv = 0;
   1083 
   1084 	switch (l->l_stat) {
   1085 	case LSRUN:
   1086 	case LSONPROC:
   1087 		lwp_need_userret(l);
   1088 		rv = 1;
   1089 		break;
   1090 
   1091 	case LSSLEEP:
   1092 		if ((l->l_flag & LW_SINTR) != 0) {
   1093 			/* setrunnable() will release the lock. */
   1094 			setrunnable(l);
   1095 			return 1;
   1096 		}
   1097 		break;
   1098 
   1099 	case LSSUSPENDED:
   1100 		if ((prop & SA_KILL) != 0) {
   1101 			/* lwp_continue() will release the lock. */
   1102 			lwp_continue(l);
   1103 			return 1;
   1104 		}
   1105 		break;
   1106 
   1107 	case LSSTOP:
   1108 		if ((prop & SA_STOP) != 0)
   1109 			break;
   1110 
   1111 		/*
   1112 		 * If the LWP is stopped and we are sending a continue
   1113 		 * signal, then start it again.
   1114 		 */
   1115 		if ((prop & SA_CONT) != 0) {
   1116 			if (l->l_wchan != NULL) {
   1117 				l->l_stat = LSSLEEP;
   1118 				l->l_proc->p_nrlwps++;
   1119 				rv = 1;
   1120 				break;
   1121 			}
   1122 			/* setrunnable() will release the lock. */
   1123 			setrunnable(l);
   1124 			return 1;
   1125 		} else if (l->l_wchan == NULL || (l->l_flag & LW_SINTR) != 0) {
   1126 			/* setrunnable() will release the lock. */
   1127 			setrunnable(l);
   1128 			return 1;
   1129 		}
   1130 		break;
   1131 
   1132 	default:
   1133 		break;
   1134 	}
   1135 
   1136 	lwp_unlock(l);
   1137 	return rv;
   1138 }
   1139 
   1140 /*
   1141  * Notify an LWP that it has a pending signal.
   1142  */
   1143 void
   1144 signotify(struct lwp *l)
   1145 {
   1146 	KASSERT(lwp_locked(l, NULL));
   1147 
   1148 	l->l_flag |= LW_PENDSIG;
   1149 	lwp_need_userret(l);
   1150 }
   1151 
   1152 /*
   1153  * Find an LWP within process p that is waiting on signal ksi, and hand
   1154  * it on.
   1155  */
   1156 int
   1157 sigunwait(struct proc *p, const ksiginfo_t *ksi)
   1158 {
   1159 	struct lwp *l;
   1160 	int signo;
   1161 
   1162 	KASSERT(mutex_owned(&p->p_smutex));
   1163 
   1164 	signo = ksi->ksi_signo;
   1165 
   1166 	if (ksi->ksi_lid != 0) {
   1167 		/*
   1168 		 * Signal came via _lwp_kill().  Find the LWP and see if
   1169 		 * it's interested.
   1170 		 */
   1171 		if ((l = lwp_find(p, ksi->ksi_lid)) == NULL)
   1172 			return 0;
   1173 		if (l->l_sigwaited == NULL ||
   1174 		    !sigismember(&l->l_sigwaitset, signo))
   1175 			return 0;
   1176 	} else {
   1177 		/*
   1178 		 * Look for any LWP that may be interested.
   1179 		 */
   1180 		LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) {
   1181 			KASSERT(l->l_sigwaited != NULL);
   1182 			if (sigismember(&l->l_sigwaitset, signo))
   1183 				break;
   1184 		}
   1185 	}
   1186 
   1187 	if (l != NULL) {
   1188 		l->l_sigwaited->ksi_info = ksi->ksi_info;
   1189 		l->l_sigwaited = NULL;
   1190 		LIST_REMOVE(l, l_sigwaiter);
   1191 		cv_signal(&l->l_sigcv);
   1192 		return 1;
   1193 	}
   1194 
   1195 	return 0;
   1196 }
   1197 
   1198 /*
   1199  * Send the signal to the process.  If the signal has an action, the action
   1200  * is usually performed by the target process rather than the caller; we add
   1201  * the signal to the set of pending signals for the process.
   1202  *
   1203  * Exceptions:
   1204  *   o When a stop signal is sent to a sleeping process that takes the
   1205  *     default action, the process is stopped without awakening it.
   1206  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
   1207  *     regardless of the signal action (eg, blocked or ignored).
   1208  *
   1209  * Other ignored signals are discarded immediately.
   1210  */
   1211 void
   1212 kpsignal2(struct proc *p, ksiginfo_t *ksi)
   1213 {
   1214 	int prop, lid, toall, signo = ksi->ksi_signo;
   1215 	struct lwp *l;
   1216 	ksiginfo_t *kp;
   1217 	ksiginfoq_t kq;
   1218 	sig_t action;
   1219 
   1220 	KASSERT(mutex_owned(&proclist_mutex));
   1221 	KASSERT(mutex_owned(&p->p_smutex));
   1222 	KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
   1223 	KASSERT(signo > 0 && signo < NSIG);
   1224 
   1225 	/*
   1226 	 * If the process is being created by fork, is a zombie or is
   1227 	 * exiting, then just drop the signal here and bail out.
   1228 	 */
   1229 	if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
   1230 		return;
   1231 
   1232 	/*
   1233 	 * Notify any interested parties of the signal.
   1234 	 */
   1235 	KNOTE(&p->p_klist, NOTE_SIGNAL | signo);
   1236 
   1237 	/*
   1238 	 * Some signals including SIGKILL must act on the entire process.
   1239 	 */
   1240 	kp = NULL;
   1241 	prop = sigprop[signo];
   1242 	toall = ((prop & SA_TOALL) != 0);
   1243 
   1244 	if (toall)
   1245 		lid = 0;
   1246 	else
   1247 		lid = ksi->ksi_lid;
   1248 
   1249 	/*
   1250 	 * If proc is traced, always give parent a chance.
   1251 	 */
   1252 	if (p->p_slflag & PSL_TRACED) {
   1253 		action = SIG_DFL;
   1254 
   1255 		if (lid == 0) {
   1256 			/*
   1257 			 * If the process is being traced and the signal
   1258 			 * is being caught, make sure to save any ksiginfo.
   1259 			 */
   1260 			if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
   1261 				return;
   1262 			sigput(&p->p_sigpend, p, kp);
   1263 		}
   1264 	} else {
   1265 		/*
   1266 		 * If the signal was the result of a trap and is not being
   1267 		 * caught, then reset it to default action so that the
   1268 		 * process dumps core immediately.
   1269 		 */
   1270 		if (KSI_TRAP_P(ksi)) {
   1271 			if (!sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
   1272 				sigdelset(&p->p_sigctx.ps_sigignore, signo);
   1273 				SIGACTION(p, signo).sa_handler = SIG_DFL;
   1274 			}
   1275 		}
   1276 
   1277 		/*
   1278 		 * If the signal is being ignored, then drop it.  Note: we
   1279 		 * don't set SIGCONT in ps_sigignore, and if it is set to
   1280 		 * SIG_IGN, action will be SIG_DFL here.
   1281 		 */
   1282 		if (sigismember(&p->p_sigctx.ps_sigignore, signo))
   1283 			return;
   1284 
   1285 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signo))
   1286 			action = SIG_CATCH;
   1287 		else {
   1288 			action = SIG_DFL;
   1289 
   1290 			/*
   1291 			 * If sending a tty stop signal to a member of an
   1292 			 * orphaned process group, discard the signal here if
   1293 			 * the action is default; don't stop the process below
   1294 			 * if sleeping, and don't clear any pending SIGCONT.
   1295 			 */
   1296 			if (prop & SA_TTYSTOP &&
   1297 			    (p->p_sflag & PS_ORPHANPG) != 0)
   1298 				return;
   1299 
   1300 			if (prop & SA_KILL && p->p_nice > NZERO)
   1301 				p->p_nice = NZERO;
   1302 		}
   1303 	}
   1304 
   1305 	/*
   1306 	 * If stopping or continuing a process, discard any pending
   1307 	 * signals that would do the inverse.
   1308 	 */
   1309 	if ((prop & (SA_CONT | SA_STOP)) != 0) {
   1310 		ksiginfo_queue_init(&kq);
   1311 		if ((prop & SA_CONT) != 0)
   1312 			sigclear(&p->p_sigpend, &stopsigmask, &kq);
   1313 		if ((prop & SA_STOP) != 0)
   1314 			sigclear(&p->p_sigpend, &contsigmask, &kq);
   1315 		ksiginfo_queue_drain(&kq);	/* XXXSMP */
   1316 	}
   1317 
   1318 	/*
   1319 	 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
   1320 	 * please!), check if any LWPs are waiting on it.  If yes, pass on
   1321 	 * the signal info.  The signal won't be processed further here.
   1322 	 */
   1323 	if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) &&
   1324 	    p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 &&
   1325 	    sigunwait(p, ksi))
   1326 		return;
   1327 
   1328 	/*
   1329 	 * XXXSMP Should be allocated by the caller, we're holding locks
   1330 	 * here.
   1331 	 */
   1332 	if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
   1333 		return;
   1334 
   1335 	/*
   1336 	 * LWP private signals are easy - just find the LWP and post
   1337 	 * the signal to it.
   1338 	 */
   1339 	if (lid != 0) {
   1340 		l = lwp_find(p, lid);
   1341 		if (l != NULL) {
   1342 			sigput(&l->l_sigpend, p, kp);
   1343 			mb_write();
   1344 			(void)sigpost(l, action, prop, kp->ksi_signo);
   1345 		}
   1346 		goto out;
   1347 	}
   1348 
   1349 	/*
   1350 	 * Some signals go to all LWPs, even if posted with _lwp_kill().
   1351 	 */
   1352 	if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
   1353 		if ((p->p_slflag & PSL_TRACED) != 0)
   1354 			goto deliver;
   1355 
   1356 		/*
   1357 		 * If SIGCONT is default (or ignored) and process is
   1358 		 * asleep, we are finished; the process should not
   1359 		 * be awakened.
   1360 		 */
   1361 		if ((prop & SA_CONT) != 0 && action == SIG_DFL)
   1362 			goto out;
   1363 
   1364 		if ((prop & SA_STOP) != 0 && action == SIG_DFL) {
   1365 			/*
   1366 			 * If a child holding parent blocked, stopping could
   1367 			 * cause deadlock: discard the signal.
   1368 			 */
   1369 			if ((p->p_sflag & PS_PPWAIT) == 0) {
   1370 				p->p_xstat = signo;
   1371 				proc_stop(p, 1, signo);
   1372 			}
   1373 			goto out;
   1374 		} else {
   1375 			/*
   1376 			 * Stop signals with the default action are handled
   1377 			 * specially in issignal(), and so are not enqueued.
   1378 			 */
   1379 			sigput(&p->p_sigpend, p, kp);
   1380 		}
   1381 	} else {
   1382 		/*
   1383 		 * Process is stopped or stopping.  If traced, then no
   1384 		 * further action is necessary.
   1385 		 */
   1386 		if ((p->p_slflag & PSL_TRACED) != 0 && signo != SIGKILL)
   1387 			goto out;
   1388 
   1389 		if ((prop & (SA_CONT | SA_KILL)) != 0) {
   1390 			/*
   1391 			 * Re-adjust p_nstopchild if the process wasn't
   1392 			 * collected by its parent.
   1393 			 */
   1394 			p->p_stat = SACTIVE;
   1395 			p->p_sflag &= ~PS_STOPPING;
   1396 			if (!p->p_waited)
   1397 				p->p_pptr->p_nstopchild--;
   1398 
   1399 			/*
   1400 			 * If SIGCONT is default (or ignored), we continue
   1401 			 * the process but don't leave the signal in
   1402 			 * ps_siglist, as it has no further action.  If
   1403 			 * SIGCONT is held, we continue the process and
   1404 			 * leave the signal in ps_siglist.  If the process
   1405 			 * catches SIGCONT, let it handle the signal itself.
   1406 			 * If it isn't waiting on an event, then it goes
   1407 			 * back to run state.  Otherwise, process goes back
   1408 			 * to sleep state.
   1409 			 */
   1410 			if ((prop & SA_CONT) == 0 || action != SIG_DFL)
   1411 				sigput(&p->p_sigpend, p, kp);
   1412 		} else if ((prop & SA_STOP) != 0) {
   1413 			/*
   1414 			 * Already stopped, don't need to stop again.
   1415 			 * (If we did the shell could get confused.)
   1416 			 */
   1417 			goto out;
   1418 		} else
   1419 			sigput(&p->p_sigpend, p, kp);
   1420 	}
   1421 
   1422  deliver:
   1423 	/*
   1424 	 * Before we set L_PENDSIG on any LWP, ensure that the signal is
   1425 	 * visible on the per process list (for sigispending()).  This
   1426 	 * is unlikely to be needed in practice, but...
   1427 	 */
   1428 	mb_write();
   1429 
   1430 	/*
   1431 	 * Try to find an LWP that can take the signal.
   1432 	 */
   1433 	LIST_FOREACH(l, &p->p_lwps, l_sibling)
   1434 		if (sigpost(l, action, prop, kp->ksi_signo) && !toall)
   1435 			break;
   1436 
   1437  out:
   1438  	/*
   1439  	 * If the ksiginfo wasn't used, then bin it.  XXXSMP freeing memory
   1440  	 * with locks held.  The caller should take care of this.
   1441  	 */
   1442  	ksiginfo_free(kp);
   1443 }
   1444 
   1445 void
   1446 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
   1447 {
   1448 	struct proc *p = l->l_proc;
   1449 
   1450 	KASSERT(mutex_owned(&p->p_smutex));
   1451 
   1452 	(*p->p_emul->e_sendsig)(ksi, mask);
   1453 }
   1454 
   1455 /*
   1456  * Stop the current process and switch away when being stopped or traced.
   1457  */
   1458 void
   1459 sigswitch(bool ppsig, int ppmask, int signo)
   1460 {
   1461 	struct lwp *l = curlwp, *l2;
   1462 	struct proc *p = l->l_proc;
   1463 #ifdef MULTIPROCESSOR
   1464 	int biglocks;
   1465 #endif
   1466 
   1467 	KASSERT(mutex_owned(&p->p_smutex));
   1468 	KASSERT(l->l_stat == LSONPROC);
   1469 	KASSERT(p->p_nrlwps > 0);
   1470 
   1471 	/*
   1472 	 * On entry we know that the process needs to stop.  If it's
   1473 	 * the result of a 'sideways' stop signal that has been sourced
   1474 	 * through issignal(), then stop other LWPs in the process too.
   1475 	 */
   1476 	if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
   1477 		/*
   1478 		 * Set the stopping indicator and bring all sleeping LWPs
   1479 		 * to a halt so they are included in p->p_nrlwps
   1480 		 */
   1481 		p->p_sflag |= (PS_STOPPING | PS_NOTIFYSTOP);
   1482 		mb_write();
   1483 
   1484 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1485 			lwp_lock(l2);
   1486 			if (l2->l_stat == LSSLEEP &&
   1487 			    (l2->l_flag & LW_SINTR) != 0) {
   1488 				l2->l_stat = LSSTOP;
   1489 				p->p_nrlwps--;
   1490 			}
   1491 			lwp_unlock(l2);
   1492 		}
   1493 
   1494 		/*
   1495 		 * Have the remaining LWPs come to a halt, and trigger
   1496 		 * proc_stop_callout() to ensure that they do.
   1497 		 */
   1498 		KASSERT(signo != 0);
   1499 		KASSERT(p->p_nrlwps > 0);
   1500 
   1501 		if (p->p_nrlwps > 1) {
   1502 			LIST_FOREACH(l2, &p->p_lwps, l_sibling)
   1503 				sigpost(l2, SIG_DFL, SA_STOP, signo);
   1504 			callout_schedule(&proc_stop_ch, 1);
   1505 		}
   1506 	}
   1507 
   1508 	/*
   1509 	 * If we are the last live LWP, and the stop was a result of
   1510 	 * a new signal, then signal the parent.
   1511 	 */
   1512 	if ((p->p_sflag & PS_STOPPING) != 0) {
   1513 		if (!mutex_tryenter(&proclist_mutex)) {
   1514 			mutex_exit(&p->p_smutex);
   1515 			mutex_enter(&proclist_mutex);
   1516 			mutex_enter(&p->p_smutex);
   1517 		}
   1518 
   1519 		if (p->p_nrlwps == 1 && (p->p_sflag & PS_STOPPING) != 0) {
   1520 			p->p_sflag &= ~PS_STOPPING;
   1521 			p->p_stat = SSTOP;
   1522 			p->p_waited = 0;
   1523 			p->p_pptr->p_nstopchild++;
   1524 			if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
   1525 				/*
   1526 				 * Note that child_psignal() will drop
   1527 				 * p->p_smutex briefly.
   1528 				 */
   1529 				if (ppsig)
   1530 					child_psignal(p, ppmask);
   1531 				cv_broadcast(&p->p_pptr->p_waitcv);
   1532 			}
   1533 		}
   1534 
   1535 		mutex_exit(&proclist_mutex);
   1536 	}
   1537 
   1538 	/*
   1539 	 * Unlock and switch away.
   1540 	 */
   1541 	KERNEL_UNLOCK_ALL(l, &biglocks);
   1542 	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
   1543 		p->p_nrlwps--;
   1544 		lwp_lock(l);
   1545 		KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSLEEP);
   1546 		l->l_stat = LSSTOP;
   1547 		lwp_unlock(l);
   1548 	}
   1549 
   1550 	mutex_exit(&p->p_smutex);
   1551 	lwp_lock(l);
   1552 	mi_switch(l, NULL);
   1553 	KERNEL_LOCK(biglocks, l);
   1554 	mutex_enter(&p->p_smutex);
   1555 }
   1556 
   1557 /*
   1558  * Check for a signal from the debugger.
   1559  */
   1560 int
   1561 sigchecktrace(sigpend_t **spp)
   1562 {
   1563 	struct lwp *l = curlwp;
   1564 	struct proc *p = l->l_proc;
   1565 	int signo;
   1566 
   1567 	KASSERT(mutex_owned(&p->p_smutex));
   1568 
   1569 	/*
   1570 	 * If we are no longer being traced, or the parent didn't
   1571 	 * give us a signal, look for more signals.
   1572 	 */
   1573 	if ((p->p_slflag & PSL_TRACED) == 0 || p->p_xstat == 0)
   1574 		return 0;
   1575 
   1576 	/* If there's a pending SIGKILL, process it immediately. */
   1577 	if (sigismember(&p->p_sigpend.sp_set, SIGKILL))
   1578 		return 0;
   1579 
   1580 	/*
   1581 	 * If the new signal is being masked, look for other signals.
   1582 	 * `p->p_sigctx.ps_siglist |= mask' is done in setrunnable().
   1583 	 */
   1584 	signo = p->p_xstat;
   1585 	p->p_xstat = 0;
   1586 	if ((sigprop[signo] & SA_TOLWP) != 0)
   1587 		*spp = &l->l_sigpend;
   1588 	else
   1589 		*spp = &p->p_sigpend;
   1590 	if (sigismember(&l->l_sigmask, signo))
   1591 		signo = 0;
   1592 
   1593 	return signo;
   1594 }
   1595 
   1596 /*
   1597  * If the current process has received a signal (should be caught or cause
   1598  * termination, should interrupt current syscall), return the signal number.
   1599  *
   1600  * Stop signals with default action are processed immediately, then cleared;
   1601  * they aren't returned.  This is checked after each entry to the system for
   1602  * a syscall or trap.
   1603  *
   1604  * We will also return -1 if the process is exiting and the current LWP must
   1605  * follow suit.
   1606  *
   1607  * Note that we may be called while on a sleep queue, so MUST NOT sleep.  We
   1608  * can switch away, though.
   1609  */
   1610 int
   1611 issignal(struct lwp *l)
   1612 {
   1613 	struct proc *p = l->l_proc;
   1614 	int signo = 0, prop;
   1615 	sigpend_t *sp = NULL;
   1616 	sigset_t ss;
   1617 
   1618 	KASSERT(mutex_owned(&p->p_smutex));
   1619 
   1620 	for (;;) {
   1621 		/* Discard any signals that we have decided not to take. */
   1622 		if (signo != 0)
   1623 			(void)sigget(sp, NULL, signo, NULL);
   1624 
   1625 		/*
   1626 		 * If the process is stopped/stopping, then stop ourselves
   1627 		 * now that we're on the kernel/userspace boundary.  When
   1628 		 * we awaken, check for a signal from the debugger.
   1629 		 */
   1630 		if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
   1631 			sigswitch(true, PS_NOCLDSTOP, 0);
   1632 			signo = sigchecktrace(&sp);
   1633 		} else
   1634 			signo = 0;
   1635 
   1636 		/*
   1637 		 * If the debugger didn't provide a signal, find a pending
   1638 		 * signal from our set.  Check per-LWP signals first, and
   1639 		 * then per-process.
   1640 		 */
   1641 		if (signo == 0) {
   1642 			sp = &l->l_sigpend;
   1643 			ss = sp->sp_set;
   1644 			if ((p->p_sflag & PS_PPWAIT) != 0)
   1645 				sigminusset(&stopsigmask, &ss);
   1646 			sigminusset(&l->l_sigmask, &ss);
   1647 
   1648 			if ((signo = firstsig(&ss)) == 0) {
   1649 				sp = &p->p_sigpend;
   1650 				ss = sp->sp_set;
   1651 				if ((p->p_sflag & PS_PPWAIT) != 0)
   1652 					sigminusset(&stopsigmask, &ss);
   1653 				sigminusset(&l->l_sigmask, &ss);
   1654 
   1655 				if ((signo = firstsig(&ss)) == 0) {
   1656 					/*
   1657 					 * No signal pending - clear the
   1658 					 * indicator and bail out.
   1659 					 */
   1660 					lwp_lock(l);
   1661 					l->l_flag &= ~LW_PENDSIG;
   1662 					lwp_unlock(l);
   1663 					sp = NULL;
   1664 					break;
   1665 				}
   1666 			}
   1667 		}
   1668 
   1669 		/*
   1670 		 * We should see pending but ignored signals only if
   1671 		 * we are being traced.
   1672 		 */
   1673 		if (sigismember(&p->p_sigctx.ps_sigignore, signo) &&
   1674 		    (p->p_slflag & PSL_TRACED) == 0) {
   1675 			/* Discard the signal. */
   1676 			continue;
   1677 		}
   1678 
   1679 		/*
   1680 		 * If traced, always stop, and stay stopped until released
   1681 		 * by the debugger.  If the our parent process is waiting
   1682 		 * for us, don't hang as we could deadlock.
   1683 		 */
   1684 		if ((p->p_slflag & PSL_TRACED) != 0 &&
   1685 		    (p->p_sflag & PS_PPWAIT) == 0 && signo != SIGKILL) {
   1686 			/* Take the signal. */
   1687 			(void)sigget(sp, NULL, signo, NULL);
   1688 			p->p_xstat = signo;
   1689 
   1690 			/* Emulation-specific handling of signal trace */
   1691 			if (p->p_emul->e_tracesig == NULL ||
   1692 			    (*p->p_emul->e_tracesig)(p, signo) == 0)
   1693 				sigswitch(!(p->p_slflag & PSL_FSTRACE), 0,
   1694 				    signo);
   1695 
   1696 			/* Check for a signal from the debugger. */
   1697 			if ((signo = sigchecktrace(&sp)) == 0)
   1698 				continue;
   1699 		}
   1700 
   1701 		prop = sigprop[signo];
   1702 
   1703 		/*
   1704 		 * Decide whether the signal should be returned.
   1705 		 */
   1706 		switch ((long)SIGACTION(p, signo).sa_handler) {
   1707 		case (long)SIG_DFL:
   1708 			/*
   1709 			 * Don't take default actions on system processes.
   1710 			 */
   1711 			if (p->p_pid <= 1) {
   1712 #ifdef DIAGNOSTIC
   1713 				/*
   1714 				 * Are you sure you want to ignore SIGSEGV
   1715 				 * in init? XXX
   1716 				 */
   1717 				printf_nolog("Process (pid %d) got sig %d\n",
   1718 				    p->p_pid, signo);
   1719 #endif
   1720 				continue;
   1721 			}
   1722 
   1723 			/*
   1724 			 * If there is a pending stop signal to process with
   1725 			 * default action, stop here, then clear the signal.
   1726 			 * However, if process is member of an orphaned
   1727 			 * process group, ignore tty stop signals.
   1728 			 */
   1729 			if (prop & SA_STOP) {
   1730 				if (p->p_slflag & PSL_TRACED ||
   1731 		    		    ((p->p_sflag & PS_ORPHANPG) != 0 &&
   1732 				    prop & SA_TTYSTOP)) {
   1733 				    	/* Ignore the signal. */
   1734 					continue;
   1735 				}
   1736 				/* Take the signal. */
   1737 				(void)sigget(sp, NULL, signo, NULL);
   1738 				p->p_xstat = signo;
   1739 				signo = 0;
   1740 				sigswitch(true, PS_NOCLDSTOP, p->p_xstat);
   1741 			} else if (prop & SA_IGNORE) {
   1742 				/*
   1743 				 * Except for SIGCONT, shouldn't get here.
   1744 				 * Default action is to ignore; drop it.
   1745 				 */
   1746 				continue;
   1747 			}
   1748 			break;
   1749 
   1750 		case (long)SIG_IGN:
   1751 #ifdef DEBUG_ISSIGNAL
   1752 			/*
   1753 			 * Masking above should prevent us ever trying
   1754 			 * to take action on an ignored signal other
   1755 			 * than SIGCONT, unless process is traced.
   1756 			 */
   1757 			if ((prop & SA_CONT) == 0 &&
   1758 			    (p->p_slflag & PSL_TRACED) == 0)
   1759 				printf_nolog("issignal\n");
   1760 #endif
   1761 			continue;
   1762 
   1763 		default:
   1764 			/*
   1765 			 * This signal has an action, let postsig() process
   1766 			 * it.
   1767 			 */
   1768 			break;
   1769 		}
   1770 
   1771 		break;
   1772 	}
   1773 
   1774 	l->l_sigpendset = sp;
   1775 	return signo;
   1776 }
   1777 
   1778 /*
   1779  * Take the action for the specified signal
   1780  * from the current set of pending signals.
   1781  */
   1782 void
   1783 postsig(int signo)
   1784 {
   1785 	struct lwp	*l;
   1786 	struct proc	*p;
   1787 	struct sigacts	*ps;
   1788 	sig_t		action;
   1789 	sigset_t	*returnmask;
   1790 	ksiginfo_t	ksi;
   1791 
   1792 	l = curlwp;
   1793 	p = l->l_proc;
   1794 	ps = p->p_sigacts;
   1795 
   1796 	KASSERT(mutex_owned(&p->p_smutex));
   1797 	KASSERT(signo > 0);
   1798 
   1799 	/*
   1800 	 * Set the new mask value and also defer further occurrences of this
   1801 	 * signal.
   1802 	 *
   1803 	 * Special case: user has done a sigpause.  Here the current mask is
   1804 	 * not of interest, but rather the mask from before the sigpause is
   1805 	 * what we want restored after the signal processing is completed.
   1806 	 */
   1807 	if (l->l_sigrestore) {
   1808 		returnmask = &l->l_sigoldmask;
   1809 		l->l_sigrestore = 0;
   1810 	} else
   1811 		returnmask = &l->l_sigmask;
   1812 
   1813 	/*
   1814 	 * Commit to taking the signal before releasing the mutex.
   1815 	 */
   1816 	action = SIGACTION_PS(ps, signo).sa_handler;
   1817 	p->p_stats->p_ru.ru_nsignals++;
   1818 	sigget(l->l_sigpendset, &ksi, signo, NULL);
   1819 
   1820 #ifdef KTRACE
   1821 	if (KTRPOINT(p, KTR_PSIG)) {
   1822 		mutex_exit(&p->p_smutex);
   1823 		ktrpsig(l, signo, action, returnmask, NULL);
   1824 		mutex_enter(&p->p_smutex);
   1825 	}
   1826 #endif
   1827 
   1828 	if (action == SIG_DFL) {
   1829 		/*
   1830 		 * Default action, where the default is to kill
   1831 		 * the process.  (Other cases were ignored above.)
   1832 		 */
   1833 		sigexit(l, signo);
   1834 		return;
   1835 	}
   1836 
   1837 	/*
   1838 	 * If we get here, the signal must be caught.
   1839 	 */
   1840 #ifdef DIAGNOSTIC
   1841 	if (action == SIG_IGN || sigismember(&l->l_sigmask, signo))
   1842 		panic("postsig action");
   1843 #endif
   1844 
   1845 	kpsendsig(l, &ksi, returnmask);
   1846 }
   1847 
   1848 /*
   1849  * sendsig_reset:
   1850  *
   1851  *	Reset the signal action.  Called from emulation specific sendsig()
   1852  *	before unlocking to deliver the signal.
   1853  */
   1854 void
   1855 sendsig_reset(struct lwp *l, int signo)
   1856 {
   1857 	struct proc *p = l->l_proc;
   1858 	struct sigacts *ps = p->p_sigacts;
   1859 
   1860 	KASSERT(mutex_owned(&p->p_smutex));
   1861 
   1862 	p->p_sigctx.ps_lwp = 0;
   1863 	p->p_sigctx.ps_code = 0;
   1864 	p->p_sigctx.ps_signo = 0;
   1865 
   1866 	sigplusset(&SIGACTION_PS(ps, signo).sa_mask, &l->l_sigmask);
   1867 	if (SIGACTION_PS(ps, signo).sa_flags & SA_RESETHAND) {
   1868 		sigdelset(&p->p_sigctx.ps_sigcatch, signo);
   1869 		if (signo != SIGCONT && sigprop[signo] & SA_IGNORE)
   1870 			sigaddset(&p->p_sigctx.ps_sigignore, signo);
   1871 		SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
   1872 	}
   1873 }
   1874 
   1875 /*
   1876  * Kill the current process for stated reason.
   1877  */
   1878 void
   1879 killproc(struct proc *p, const char *why)
   1880 {
   1881 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
   1882 	uprintf_locked("sorry, pid %d was killed: %s\n", p->p_pid, why);
   1883 	mutex_enter(&proclist_mutex);	/* XXXSMP */
   1884 	psignal(p, SIGKILL);
   1885 	mutex_exit(&proclist_mutex);	/* XXXSMP */
   1886 }
   1887 
   1888 /*
   1889  * Force the current process to exit with the specified signal, dumping core
   1890  * if appropriate.  We bypass the normal tests for masked and caught
   1891  * signals, allowing unrecoverable failures to terminate the process without
   1892  * changing signal state.  Mark the accounting record with the signal
   1893  * termination.  If dumping core, save the signal number for the debugger.
   1894  * Calls exit and does not return.
   1895  */
   1896 void
   1897 sigexit(struct lwp *l, int signo)
   1898 {
   1899 	int exitsig, error, docore;
   1900 	struct proc *p;
   1901 	struct lwp *t;
   1902 
   1903 	p = l->l_proc;
   1904 
   1905 	KASSERT(mutex_owned(&p->p_smutex));
   1906 	KERNEL_UNLOCK_ALL(l, NULL);
   1907 
   1908 	/*
   1909 	 * Don't permit coredump() multiple times in the same process.
   1910 	 * Call back into sigexit, where we will be suspended until
   1911 	 * the deed is done.  Note that this is a recursive call, but
   1912 	 * LW_WCORE will prevent us from coming back this way.
   1913 	 */
   1914 	if ((p->p_sflag & PS_WCORE) != 0) {
   1915 		lwp_lock(l);
   1916 		l->l_flag |= (LW_WCORE | LW_WEXIT | LW_WSUSPEND);
   1917 		lwp_unlock(l);
   1918 		mutex_exit(&p->p_smutex);
   1919 		lwp_userret(l);
   1920 #ifdef DIAGNOSTIC
   1921 		panic("sigexit");
   1922 #endif
   1923 		/* NOTREACHED */
   1924 	}
   1925 
   1926 	/*
   1927 	 * Prepare all other LWPs for exit.  If dumping core, suspend them
   1928 	 * so that their registers are available long enough to be dumped.
   1929  	 */
   1930 	if ((docore = (sigprop[signo] & SA_CORE)) != 0) {
   1931 		p->p_sflag |= PS_WCORE;
   1932 		for (;;) {
   1933 			LIST_FOREACH(t, &p->p_lwps, l_sibling) {
   1934 				lwp_lock(t);
   1935 				if (t == l) {
   1936 					t->l_flag &= ~LW_WSUSPEND;
   1937 					lwp_unlock(t);
   1938 					continue;
   1939 				}
   1940 				t->l_flag |= (LW_WCORE | LW_WEXIT);
   1941 				lwp_suspend(l, t);
   1942 			}
   1943 
   1944 			if (p->p_nrlwps == 1)
   1945 				break;
   1946 
   1947 			/*
   1948 			 * Kick any LWPs sitting in lwp_wait1(), and wait
   1949 			 * for everyone else to stop before proceeding.
   1950 			 */
   1951 			p->p_nlwpwait++;
   1952 			cv_broadcast(&p->p_lwpcv);
   1953 			cv_wait(&p->p_lwpcv, &p->p_smutex);
   1954 			p->p_nlwpwait--;
   1955 		}
   1956 	}
   1957 
   1958 	exitsig = signo;
   1959 	p->p_acflag |= AXSIG;
   1960 	p->p_sigctx.ps_signo = signo;
   1961 	mutex_exit(&p->p_smutex);
   1962 
   1963 	KERNEL_LOCK(1, l);
   1964 
   1965 	if (docore) {
   1966 		if ((error = coredump(l, NULL)) == 0)
   1967 			exitsig |= WCOREFLAG;
   1968 
   1969 		if (kern_logsigexit) {
   1970 			int uid = l->l_cred ?
   1971 			    (int)kauth_cred_geteuid(l->l_cred) : -1;
   1972 
   1973 			if (error)
   1974 				log(LOG_INFO, lognocoredump, p->p_pid,
   1975 				    p->p_comm, uid, signo, error);
   1976 			else
   1977 				log(LOG_INFO, logcoredump, p->p_pid,
   1978 				    p->p_comm, uid, signo);
   1979 		}
   1980 
   1981 #ifdef PAX_SEGVGUARD
   1982 		pax_segvguard(l, p->p_textvp, p->p_comm, true);
   1983 #endif /* PAX_SEGVGUARD */
   1984 	}
   1985 
   1986 	/* Acquire the sched state mutex.  exit1() will release it. */
   1987 	mutex_enter(&p->p_smutex);
   1988 
   1989 	/* No longer dumping core. */
   1990 	p->p_sflag &= ~PS_WCORE;
   1991 
   1992 	exit1(l, W_EXITCODE(0, exitsig));
   1993 	/* NOTREACHED */
   1994 }
   1995 
   1996 /*
   1997  * Put process 'p' into the stopped state and optionally, notify the parent.
   1998  */
   1999 void
   2000 proc_stop(struct proc *p, int notify, int signo)
   2001 {
   2002 	struct lwp *l;
   2003 
   2004 	KASSERT(mutex_owned(&proclist_mutex));
   2005 	KASSERT(mutex_owned(&p->p_smutex));
   2006 
   2007 	/*
   2008 	 * First off, set the stopping indicator and bring all sleeping
   2009 	 * LWPs to a halt so they are included in p->p_nrlwps.  We musn't
   2010 	 * unlock between here and the p->p_nrlwps check below.
   2011 	 */
   2012 	p->p_sflag |= PS_STOPPING;
   2013 	mb_write();
   2014 
   2015 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2016 		lwp_lock(l);
   2017 		if (l->l_stat == LSSLEEP && (l->l_flag & LW_SINTR) != 0) {
   2018 			l->l_stat = LSSTOP;
   2019 			p->p_nrlwps--;
   2020 		}
   2021 		lwp_unlock(l);
   2022 	}
   2023 
   2024 	/*
   2025 	 * If there are no LWPs available to take the signal, then we
   2026 	 * signal the parent process immediately.  Otherwise, the last
   2027 	 * LWP to stop will take care of it.
   2028 	 */
   2029 	if (notify)
   2030 		p->p_sflag |= PS_NOTIFYSTOP;
   2031 	else
   2032 		p->p_sflag &= ~PS_NOTIFYSTOP;
   2033 
   2034 	if (p->p_nrlwps == 0) {
   2035 		p->p_sflag &= ~PS_STOPPING;
   2036 		p->p_stat = SSTOP;
   2037 		p->p_waited = 0;
   2038 		p->p_pptr->p_nstopchild++;
   2039 
   2040 		if (notify) {
   2041 			child_psignal(p, PS_NOCLDSTOP);
   2042 			cv_broadcast(&p->p_pptr->p_waitcv);
   2043 		}
   2044 	} else {
   2045 		/*
   2046 		 * Have the remaining LWPs come to a halt, and trigger
   2047 		 * proc_stop_callout() to ensure that they do.
   2048 		 */
   2049 		LIST_FOREACH(l, &p->p_lwps, l_sibling)
   2050 			sigpost(l, SIG_DFL, SA_STOP, signo);
   2051 		callout_schedule(&proc_stop_ch, 1);
   2052 	}
   2053 }
   2054 
   2055 /*
   2056  * When stopping a process, we do not immediatly set sleeping LWPs stopped,
   2057  * but wait for them to come to a halt at the kernel-user boundary.  This is
   2058  * to allow LWPs to release any locks that they may hold before stopping.
   2059  *
   2060  * Non-interruptable sleeps can be long, and there is the potential for an
   2061  * LWP to begin sleeping interruptably soon after the process has been set
   2062  * stopping (PS_STOPPING).  These LWPs will not notice that the process is
   2063  * stopping, and so complete halt of the process and the return of status
   2064  * information to the parent could be delayed indefinitely.
   2065  *
   2066  * To handle this race, proc_stop_callout() runs once per tick while there
   2067  * are stopping processes it the system.  It sets LWPs that are sleeping
   2068  * interruptably into the LSSTOP state.
   2069  *
   2070  * Note that we are not concerned about keeping all LWPs stopped while the
   2071  * process is stopped: stopped LWPs can awaken briefly to handle signals.
   2072  * What we do need to ensure is that all LWPs in a stopping process have
   2073  * stopped at least once, so that notification can be sent to the parent
   2074  * process.
   2075  */
   2076 static void
   2077 proc_stop_callout(void *cookie)
   2078 {
   2079 	bool more, restart;
   2080 	struct proc *p;
   2081 	struct lwp *l;
   2082 
   2083 	(void)cookie;
   2084 
   2085 	do {
   2086 		restart = false;
   2087 		more = false;
   2088 
   2089 		mutex_enter(&proclist_mutex);
   2090 		PROCLIST_FOREACH(p, &allproc) {
   2091 			mutex_enter(&p->p_smutex);
   2092 
   2093 			if ((p->p_sflag & PS_STOPPING) == 0) {
   2094 				mutex_exit(&p->p_smutex);
   2095 				continue;
   2096 			}
   2097 
   2098 			/* Stop any LWPs sleeping interruptably. */
   2099 			LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2100 				lwp_lock(l);
   2101 				if (l->l_stat == LSSLEEP &&
   2102 				    (l->l_flag & LW_SINTR) != 0) {
   2103 				    	l->l_stat = LSSTOP;
   2104 				    	p->p_nrlwps--;
   2105 				}
   2106 				lwp_unlock(l);
   2107 			}
   2108 
   2109 			if (p->p_nrlwps == 0) {
   2110 				/*
   2111 				 * We brought the process to a halt.
   2112 				 * Mark it as stopped and notify the
   2113 				 * parent.
   2114 				 */
   2115 				p->p_sflag &= ~PS_STOPPING;
   2116 				p->p_stat = SSTOP;
   2117 				p->p_waited = 0;
   2118 				p->p_pptr->p_nstopchild++;
   2119 				if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
   2120 					/*
   2121 					 * Note that child_psignal() will
   2122 					 * drop p->p_smutex briefly.
   2123 					 * Arrange to restart and check
   2124 					 * all processes again.
   2125 					 */
   2126 					restart = true;
   2127 					child_psignal(p, PS_NOCLDSTOP);
   2128 					cv_broadcast(&p->p_pptr->p_waitcv);
   2129 				}
   2130 			} else
   2131 				more = true;
   2132 
   2133 			mutex_exit(&p->p_smutex);
   2134 			if (restart)
   2135 				break;
   2136 		}
   2137 		mutex_exit(&proclist_mutex);
   2138 	} while (restart);
   2139 
   2140 	/*
   2141 	 * If we noted processes that are stopping but still have
   2142 	 * running LWPs, then arrange to check again in 1 tick.
   2143 	 */
   2144 	if (more)
   2145 		callout_schedule(&proc_stop_ch, 1);
   2146 }
   2147 
   2148 /*
   2149  * Given a process in state SSTOP, set the state back to SACTIVE and
   2150  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
   2151  */
   2152 void
   2153 proc_unstop(struct proc *p)
   2154 {
   2155 	struct lwp *l;
   2156 	int sig;
   2157 
   2158 	KASSERT(mutex_owned(&proclist_mutex));
   2159 	KASSERT(mutex_owned(&p->p_smutex));
   2160 
   2161 	p->p_stat = SACTIVE;
   2162 	p->p_sflag &= ~PS_STOPPING;
   2163 	sig = p->p_xstat;
   2164 
   2165 	if (!p->p_waited)
   2166 		p->p_pptr->p_nstopchild--;
   2167 
   2168 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2169 		lwp_lock(l);
   2170 		if (l->l_stat != LSSTOP) {
   2171 			lwp_unlock(l);
   2172 			continue;
   2173 		}
   2174 		if (l->l_wchan == NULL) {
   2175 			setrunnable(l);
   2176 			continue;
   2177 		}
   2178 		if (sig && (l->l_flag & LW_SINTR) != 0) {
   2179 		        setrunnable(l);
   2180 		        sig = 0;
   2181 		} else {
   2182 			l->l_stat = LSSLEEP;
   2183 			p->p_nrlwps++;
   2184 			lwp_unlock(l);
   2185 		}
   2186 	}
   2187 }
   2188 
   2189 static int
   2190 filt_sigattach(struct knote *kn)
   2191 {
   2192 	struct proc *p = curproc;
   2193 
   2194 	kn->kn_ptr.p_proc = p;
   2195 	kn->kn_flags |= EV_CLEAR;               /* automatically set */
   2196 
   2197 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
   2198 
   2199 	return (0);
   2200 }
   2201 
   2202 static void
   2203 filt_sigdetach(struct knote *kn)
   2204 {
   2205 	struct proc *p = kn->kn_ptr.p_proc;
   2206 
   2207 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
   2208 }
   2209 
   2210 /*
   2211  * signal knotes are shared with proc knotes, so we apply a mask to
   2212  * the hint in order to differentiate them from process hints.  This
   2213  * could be avoided by using a signal-specific knote list, but probably
   2214  * isn't worth the trouble.
   2215  */
   2216 static int
   2217 filt_signal(struct knote *kn, long hint)
   2218 {
   2219 
   2220 	if (hint & NOTE_SIGNAL) {
   2221 		hint &= ~NOTE_SIGNAL;
   2222 
   2223 		if (kn->kn_id == hint)
   2224 			kn->kn_data++;
   2225 	}
   2226 	return (kn->kn_data != 0);
   2227 }
   2228 
   2229 const struct filterops sig_filtops = {
   2230 	0, filt_sigattach, filt_sigdetach, filt_signal
   2231 };
   2232