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