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