Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.130
      1  1.130   thorpej /*	$NetBSD: kern_sig.c,v 1.130 2003/01/18 10:06:29 thorpej Exp $	*/
      2   1.29       cgd 
      3   1.29       cgd /*
      4   1.29       cgd  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5   1.29       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.29       cgd  * (c) UNIX System Laboratories, Inc.
      7   1.29       cgd  * All or some portions of this file are derived from material licensed
      8   1.29       cgd  * to the University of California by American Telephone and Telegraph
      9   1.29       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10   1.29       cgd  * the permission of UNIX System Laboratories, Inc.
     11   1.29       cgd  *
     12   1.29       cgd  * Redistribution and use in source and binary forms, with or without
     13   1.29       cgd  * modification, are permitted provided that the following conditions
     14   1.29       cgd  * are met:
     15   1.29       cgd  * 1. Redistributions of source code must retain the above copyright
     16   1.29       cgd  *    notice, this list of conditions and the following disclaimer.
     17   1.29       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.29       cgd  *    notice, this list of conditions and the following disclaimer in the
     19   1.29       cgd  *    documentation and/or other materials provided with the distribution.
     20   1.29       cgd  * 3. All advertising materials mentioning features or use of this software
     21   1.29       cgd  *    must display the following acknowledgement:
     22   1.29       cgd  *	This product includes software developed by the University of
     23   1.29       cgd  *	California, Berkeley and its contributors.
     24   1.29       cgd  * 4. Neither the name of the University nor the names of its contributors
     25   1.29       cgd  *    may be used to endorse or promote products derived from this software
     26   1.29       cgd  *    without specific prior written permission.
     27   1.29       cgd  *
     28   1.29       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   1.29       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   1.29       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   1.29       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   1.29       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   1.29       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   1.29       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   1.29       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   1.29       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   1.29       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   1.29       cgd  * SUCH DAMAGE.
     39   1.29       cgd  *
     40   1.71      fvdl  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
     41   1.29       cgd  */
     42  1.116     lukem 
     43  1.116     lukem #include <sys/cdefs.h>
     44  1.130   thorpej __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.130 2003/01/18 10:06:29 thorpej Exp $");
     45   1.70       mrg 
     46   1.73   thorpej #include "opt_ktrace.h"
     47   1.74   thorpej #include "opt_compat_sunos.h"
     48   1.95       eeh #include "opt_compat_netbsd32.h"
     49   1.29       cgd 
     50   1.29       cgd #define	SIGPROP		/* include signal properties table */
     51   1.29       cgd #include <sys/param.h>
     52   1.29       cgd #include <sys/signalvar.h>
     53   1.29       cgd #include <sys/resourcevar.h>
     54   1.29       cgd #include <sys/namei.h>
     55   1.29       cgd #include <sys/vnode.h>
     56   1.29       cgd #include <sys/proc.h>
     57   1.29       cgd #include <sys/systm.h>
     58   1.29       cgd #include <sys/timeb.h>
     59   1.29       cgd #include <sys/times.h>
     60   1.29       cgd #include <sys/buf.h>
     61   1.29       cgd #include <sys/acct.h>
     62   1.29       cgd #include <sys/file.h>
     63   1.29       cgd #include <sys/kernel.h>
     64   1.29       cgd #include <sys/wait.h>
     65   1.29       cgd #include <sys/ktrace.h>
     66   1.29       cgd #include <sys/syslog.h>
     67   1.29       cgd #include <sys/stat.h>
     68   1.29       cgd #include <sys/core.h>
     69   1.59       cgd #include <sys/filedesc.h>
     70   1.89   thorpej #include <sys/malloc.h>
     71   1.89   thorpej #include <sys/pool.h>
     72  1.130   thorpej #include <sys/ucontext.h>
     73  1.130   thorpej #include <sys/sa.h>
     74  1.130   thorpej #include <sys/savar.h>
     75  1.118   thorpej #include <sys/exec.h>
     76   1.29       cgd 
     77   1.32       cgd #include <sys/mount.h>
     78   1.32       cgd #include <sys/syscallargs.h>
     79   1.32       cgd 
     80   1.29       cgd #include <machine/cpu.h>
     81   1.29       cgd 
     82   1.29       cgd #include <sys/user.h>		/* for coredump */
     83   1.52  christos 
     84   1.69       mrg #include <uvm/uvm_extern.h>
     85   1.69       mrg 
     86  1.112     lukem static void	proc_stop(struct proc *p);
     87  1.112     lukem static int	build_corename(struct proc *, char [MAXPATHLEN]);
     88  1.112     lukem sigset_t	contsigmask, stopsigmask, sigcantmask;
     89   1.29       cgd 
     90  1.112     lukem struct pool	sigacts_pool;	/* memory pool for sigacts structures */
     91  1.130   thorpej struct pool	siginfo_pool;	/* memory pool for siginfo structures */
     92   1.89   thorpej 
     93   1.29       cgd /*
     94   1.29       cgd  * Can process p, with pcred pc, send the signal signum to process q?
     95   1.29       cgd  */
     96  1.112     lukem #define	CANSIGNAL(p, pc, q, signum) \
     97   1.29       cgd 	((pc)->pc_ucred->cr_uid == 0 || \
     98   1.29       cgd 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
     99   1.29       cgd 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
    100   1.29       cgd 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
    101   1.29       cgd 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
    102   1.29       cgd 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
    103   1.29       cgd 
    104   1.89   thorpej /*
    105   1.89   thorpej  * Initialize signal-related data structures.
    106   1.89   thorpej  */
    107   1.89   thorpej void
    108  1.112     lukem signal_init(void)
    109   1.89   thorpej {
    110  1.112     lukem 
    111   1.89   thorpej 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
    112  1.120   thorpej 	    &pool_allocator_nointr);
    113  1.130   thorpej 	pool_init(&siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
    114  1.130   thorpej 	    &pool_allocator_nointr);
    115   1.89   thorpej }
    116   1.89   thorpej 
    117   1.89   thorpej /*
    118  1.109  jdolecek  * Create an initial sigctx structure, using the same signal state
    119  1.109  jdolecek  * as p. If 'share' is set, share the sigctx_proc part, otherwise just
    120  1.109  jdolecek  * copy it from parent.
    121   1.89   thorpej  */
    122  1.109  jdolecek void
    123  1.112     lukem sigactsinit(struct proc *np, struct proc *pp, int share)
    124   1.89   thorpej {
    125   1.89   thorpej 	struct sigacts *ps;
    126   1.89   thorpej 
    127  1.109  jdolecek 	if (share) {
    128  1.109  jdolecek 		np->p_sigacts = pp->p_sigacts;
    129  1.109  jdolecek 		pp->p_sigacts->sa_refcnt++;
    130  1.109  jdolecek 	} else {
    131  1.109  jdolecek 		ps = pool_get(&sigacts_pool, PR_WAITOK);
    132  1.109  jdolecek 		if (pp)
    133  1.109  jdolecek 			memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
    134  1.109  jdolecek 		else
    135  1.109  jdolecek 			memset(ps, '\0', sizeof(struct sigacts));
    136  1.109  jdolecek 		ps->sa_refcnt = 1;
    137  1.109  jdolecek 		np->p_sigacts = ps;
    138  1.109  jdolecek 	}
    139   1.89   thorpej }
    140   1.89   thorpej 
    141   1.89   thorpej /*
    142  1.109  jdolecek  * Make this process not share its sigctx, maintaining all
    143   1.89   thorpej  * signal state.
    144   1.89   thorpej  */
    145   1.89   thorpej void
    146  1.112     lukem sigactsunshare(struct proc *p)
    147   1.89   thorpej {
    148  1.109  jdolecek 	struct sigacts *oldps;
    149   1.89   thorpej 
    150  1.109  jdolecek 	if (p->p_sigacts->sa_refcnt == 1)
    151   1.89   thorpej 		return;
    152   1.89   thorpej 
    153  1.109  jdolecek 	oldps = p->p_sigacts;
    154  1.109  jdolecek 	sigactsinit(p, NULL, 0);
    155  1.109  jdolecek 
    156  1.109  jdolecek 	if (--oldps->sa_refcnt == 0)
    157  1.109  jdolecek 		pool_put(&sigacts_pool, oldps);
    158   1.89   thorpej }
    159   1.89   thorpej 
    160   1.89   thorpej /*
    161  1.109  jdolecek  * Release a sigctx structure.
    162   1.89   thorpej  */
    163   1.89   thorpej void
    164  1.112     lukem sigactsfree(struct proc *p)
    165   1.89   thorpej {
    166  1.112     lukem 	struct sigacts *ps;
    167   1.89   thorpej 
    168  1.112     lukem 	ps = p->p_sigacts;
    169  1.109  jdolecek 	if (--ps->sa_refcnt > 0)
    170   1.89   thorpej 		return;
    171   1.89   thorpej 
    172   1.89   thorpej 	pool_put(&sigacts_pool, ps);
    173   1.89   thorpej }
    174   1.89   thorpej 
    175   1.79   mycroft int
    176  1.112     lukem sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
    177  1.121   thorpej 	struct sigaction *osa, void *tramp, int vers)
    178   1.79   mycroft {
    179  1.112     lukem 	struct sigacts	*ps;
    180  1.112     lukem 	int		prop;
    181   1.79   mycroft 
    182  1.112     lukem 	ps = p->p_sigacts;
    183   1.79   mycroft 	if (signum <= 0 || signum >= NSIG)
    184   1.79   mycroft 		return (EINVAL);
    185   1.79   mycroft 
    186  1.121   thorpej 	/*
    187  1.121   thorpej 	 * Trampoline ABI version 0 is reserved for the legacy
    188  1.121   thorpej 	 * kernel-provided on-stack trampoline.  Conversely, if
    189  1.121   thorpej 	 * we are using a non-0 ABI version, we must have a
    190  1.121   thorpej 	 * trampoline.
    191  1.121   thorpej 	 */
    192  1.121   thorpej 	if ((vers != 0 && tramp == NULL) ||
    193  1.121   thorpej 	    (vers == 0 && tramp != NULL))
    194  1.121   thorpej 		return (EINVAL);
    195  1.121   thorpej 
    196   1.79   mycroft 	if (osa)
    197  1.109  jdolecek 		*osa = SIGACTION_PS(ps, signum);
    198   1.79   mycroft 
    199   1.79   mycroft 	if (nsa) {
    200   1.79   mycroft 		if (nsa->sa_flags & ~SA_ALLBITS)
    201   1.79   mycroft 			return (EINVAL);
    202   1.79   mycroft 
    203   1.79   mycroft 		prop = sigprop[signum];
    204   1.79   mycroft 		if (prop & SA_CANTMASK)
    205   1.79   mycroft 			return (EINVAL);
    206   1.79   mycroft 
    207  1.105   thorpej 		(void) splsched();	/* XXXSMP */
    208  1.109  jdolecek 		SIGACTION_PS(ps, signum) = *nsa;
    209  1.121   thorpej 		ps->sa_sigdesc[signum].sd_tramp = tramp;
    210  1.121   thorpej 		ps->sa_sigdesc[signum].sd_vers = vers;
    211  1.109  jdolecek 		sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
    212   1.79   mycroft 		if ((prop & SA_NORESET) != 0)
    213  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
    214   1.79   mycroft 		if (signum == SIGCHLD) {
    215   1.79   mycroft 			if (nsa->sa_flags & SA_NOCLDSTOP)
    216   1.79   mycroft 				p->p_flag |= P_NOCLDSTOP;
    217   1.79   mycroft 			else
    218   1.79   mycroft 				p->p_flag &= ~P_NOCLDSTOP;
    219   1.82     enami 			if (nsa->sa_flags & SA_NOCLDWAIT) {
    220   1.81  christos 				/*
    221   1.81  christos 				 * Paranoia: since SA_NOCLDWAIT is implemented
    222   1.81  christos 				 * by reparenting the dying child to PID 1 (and
    223  1.112     lukem 				 * trust it to reap the zombie), PID 1 itself
    224  1.112     lukem 				 * is forbidden to set SA_NOCLDWAIT.
    225   1.81  christos 				 */
    226   1.81  christos 				if (p->p_pid == 1)
    227   1.81  christos 					p->p_flag &= ~P_NOCLDWAIT;
    228   1.81  christos 				else
    229   1.81  christos 					p->p_flag |= P_NOCLDWAIT;
    230   1.81  christos 			} else
    231   1.81  christos 				p->p_flag &= ~P_NOCLDWAIT;
    232   1.79   mycroft 		}
    233   1.79   mycroft 		if ((nsa->sa_flags & SA_NODEFER) == 0)
    234  1.109  jdolecek 			sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    235   1.79   mycroft 		else
    236  1.109  jdolecek 			sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    237   1.79   mycroft 		/*
    238  1.112     lukem 	 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
    239  1.112     lukem 		 * SIG_IGN, and for signals set to SIG_DFL where the default is
    240  1.112     lukem 		 * to ignore. However, don't put SIGCONT in
    241  1.112     lukem 		 * p_sigctx.ps_sigignore, as we have to restart the process.
    242  1.112     lukem 	 	 */
    243   1.79   mycroft 		if (nsa->sa_handler == SIG_IGN ||
    244   1.79   mycroft 		    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
    245  1.112     lukem 						/* never to be seen again */
    246  1.112     lukem 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    247  1.112     lukem 			if (signum != SIGCONT) {
    248  1.112     lukem 						/* easier in psignal */
    249  1.112     lukem 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    250  1.112     lukem 			}
    251  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    252   1.79   mycroft 		} else {
    253  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigignore, signum);
    254   1.79   mycroft 			if (nsa->sa_handler == SIG_DFL)
    255  1.109  jdolecek 				sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    256   1.79   mycroft 			else
    257  1.109  jdolecek 				sigaddset(&p->p_sigctx.ps_sigcatch, signum);
    258   1.79   mycroft 		}
    259   1.79   mycroft 		(void) spl0();
    260   1.79   mycroft 	}
    261   1.79   mycroft 
    262   1.79   mycroft 	return (0);
    263   1.79   mycroft }
    264   1.79   mycroft 
    265   1.29       cgd /* ARGSUSED */
    266   1.52  christos int
    267  1.130   thorpej sys___sigaction14(struct lwp *l, void *v, register_t *retval)
    268   1.48   thorpej {
    269   1.98  augustss 	struct sys___sigaction14_args /* {
    270  1.112     lukem 		syscallarg(int)				signum;
    271  1.112     lukem 		syscallarg(const struct sigaction *)	nsa;
    272  1.112     lukem 		syscallarg(struct sigaction *)		osa;
    273   1.48   thorpej 	} */ *uap = v;
    274  1.130   thorpej 	struct proc		*p;
    275  1.112     lukem 	struct sigaction	nsa, osa;
    276  1.112     lukem 	int			error;
    277   1.29       cgd 
    278   1.79   mycroft 	if (SCARG(uap, nsa)) {
    279   1.79   mycroft 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    280   1.52  christos 		if (error)
    281   1.29       cgd 			return (error);
    282   1.29       cgd 	}
    283  1.130   thorpej 	p = l->l_proc;
    284   1.79   mycroft 	error = sigaction1(p, SCARG(uap, signum),
    285  1.121   thorpej 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    286  1.121   thorpej 	    NULL, 0);
    287  1.121   thorpej 	if (error)
    288  1.121   thorpej 		return (error);
    289  1.121   thorpej 	if (SCARG(uap, osa)) {
    290  1.121   thorpej 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    291  1.121   thorpej 		if (error)
    292  1.121   thorpej 			return (error);
    293  1.121   thorpej 	}
    294  1.121   thorpej 	return (0);
    295  1.121   thorpej }
    296  1.121   thorpej 
    297  1.121   thorpej /* ARGSUSED */
    298  1.121   thorpej int
    299  1.130   thorpej sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
    300  1.121   thorpej {
    301  1.121   thorpej 	struct sys___sigaction_sigtramp_args /* {
    302  1.121   thorpej 		syscallarg(int)				signum;
    303  1.121   thorpej 		syscallarg(const struct sigaction *)	nsa;
    304  1.121   thorpej 		syscallarg(struct sigaction *)		osa;
    305  1.121   thorpej 		syscallarg(void *)			tramp;
    306  1.121   thorpej 		syscallarg(int)				vers;
    307  1.121   thorpej 	} */ *uap = v;
    308  1.130   thorpej 	struct proc *p = l->l_proc;
    309  1.121   thorpej 	struct sigaction nsa, osa;
    310  1.121   thorpej 	int error;
    311  1.121   thorpej 
    312  1.121   thorpej 	if (SCARG(uap, nsa)) {
    313  1.121   thorpej 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    314  1.121   thorpej 		if (error)
    315  1.121   thorpej 			return (error);
    316  1.121   thorpej 	}
    317  1.121   thorpej 	error = sigaction1(p, SCARG(uap, signum),
    318  1.121   thorpej 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    319  1.121   thorpej 	    SCARG(uap, tramp), SCARG(uap, vers));
    320   1.79   mycroft 	if (error)
    321   1.79   mycroft 		return (error);
    322   1.79   mycroft 	if (SCARG(uap, osa)) {
    323   1.79   mycroft 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    324   1.52  christos 		if (error)
    325   1.29       cgd 			return (error);
    326   1.29       cgd 	}
    327   1.29       cgd 	return (0);
    328   1.29       cgd }
    329   1.29       cgd 
    330   1.29       cgd /*
    331   1.29       cgd  * Initialize signal state for process 0;
    332   1.79   mycroft  * set to ignore signals that are ignored by default and disable the signal
    333   1.79   mycroft  * stack.
    334   1.29       cgd  */
    335   1.29       cgd void
    336  1.112     lukem siginit(struct proc *p)
    337   1.29       cgd {
    338  1.112     lukem 	struct sigacts	*ps;
    339  1.112     lukem 	int		signum, prop;
    340   1.79   mycroft 
    341  1.112     lukem 	ps = p->p_sigacts;
    342   1.79   mycroft 	sigemptyset(&contsigmask);
    343   1.79   mycroft 	sigemptyset(&stopsigmask);
    344   1.79   mycroft 	sigemptyset(&sigcantmask);
    345   1.85   mycroft 	for (signum = 1; signum < NSIG; signum++) {
    346   1.79   mycroft 		prop = sigprop[signum];
    347   1.79   mycroft 		if (prop & SA_CONT)
    348   1.79   mycroft 			sigaddset(&contsigmask, signum);
    349   1.79   mycroft 		if (prop & SA_STOP)
    350   1.79   mycroft 			sigaddset(&stopsigmask, signum);
    351   1.79   mycroft 		if (prop & SA_CANTMASK)
    352   1.79   mycroft 			sigaddset(&sigcantmask, signum);
    353   1.79   mycroft 		if (prop & SA_IGNORE && signum != SIGCONT)
    354  1.109  jdolecek 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
    355  1.109  jdolecek 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    356  1.109  jdolecek 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    357   1.79   mycroft 	}
    358  1.109  jdolecek 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    359   1.79   mycroft 	p->p_flag &= ~P_NOCLDSTOP;
    360   1.29       cgd 
    361   1.79   mycroft 	/*
    362   1.79   mycroft 	 * Reset stack state to the user stack.
    363   1.79   mycroft 	 */
    364  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    365  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_size = 0;
    366  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    367   1.89   thorpej 
    368   1.89   thorpej 	/* One reference. */
    369  1.109  jdolecek 	ps->sa_refcnt = 1;
    370   1.29       cgd }
    371   1.29       cgd 
    372   1.29       cgd /*
    373   1.29       cgd  * Reset signals for an exec of the specified process.
    374   1.29       cgd  */
    375   1.29       cgd void
    376  1.112     lukem execsigs(struct proc *p)
    377   1.29       cgd {
    378  1.112     lukem 	struct sigacts	*ps;
    379  1.112     lukem 	int		signum, prop;
    380   1.29       cgd 
    381  1.115   thorpej 	sigactsunshare(p);
    382  1.115   thorpej 
    383  1.112     lukem 	ps = p->p_sigacts;
    384  1.115   thorpej 
    385   1.29       cgd 	/*
    386   1.29       cgd 	 * Reset caught signals.  Held signals remain held
    387  1.109  jdolecek 	 * through p_sigctx.ps_sigmask (unless they were caught,
    388   1.29       cgd 	 * and are now ignored by default).
    389   1.29       cgd 	 */
    390   1.85   mycroft 	for (signum = 1; signum < NSIG; signum++) {
    391  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
    392   1.79   mycroft 			prop = sigprop[signum];
    393   1.79   mycroft 			if (prop & SA_IGNORE) {
    394   1.79   mycroft 				if ((prop & SA_CONT) == 0)
    395  1.112     lukem 					sigaddset(&p->p_sigctx.ps_sigignore,
    396  1.112     lukem 					    signum);
    397  1.109  jdolecek 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    398   1.79   mycroft 			}
    399  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    400   1.29       cgd 		}
    401  1.109  jdolecek 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    402  1.109  jdolecek 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    403   1.29       cgd 	}
    404  1.109  jdolecek 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    405   1.79   mycroft 	p->p_flag &= ~P_NOCLDSTOP;
    406   1.79   mycroft 
    407   1.29       cgd 	/*
    408   1.29       cgd 	 * Reset stack state to the user stack.
    409   1.29       cgd 	 */
    410  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    411  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_size = 0;
    412  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    413   1.29       cgd }
    414   1.29       cgd 
    415   1.79   mycroft int
    416  1.112     lukem sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
    417   1.79   mycroft {
    418   1.79   mycroft 
    419   1.79   mycroft 	if (oss)
    420  1.109  jdolecek 		*oss = p->p_sigctx.ps_sigmask;
    421   1.79   mycroft 
    422   1.79   mycroft 	if (nss) {
    423  1.105   thorpej 		(void)splsched();	/* XXXSMP */
    424   1.79   mycroft 		switch (how) {
    425   1.79   mycroft 		case SIG_BLOCK:
    426  1.109  jdolecek 			sigplusset(nss, &p->p_sigctx.ps_sigmask);
    427   1.79   mycroft 			break;
    428   1.79   mycroft 		case SIG_UNBLOCK:
    429  1.109  jdolecek 			sigminusset(nss, &p->p_sigctx.ps_sigmask);
    430  1.110   thorpej 			CHECKSIGS(p);
    431   1.79   mycroft 			break;
    432   1.79   mycroft 		case SIG_SETMASK:
    433  1.109  jdolecek 			p->p_sigctx.ps_sigmask = *nss;
    434  1.110   thorpej 			CHECKSIGS(p);
    435   1.79   mycroft 			break;
    436   1.79   mycroft 		default:
    437  1.104   thorpej 			(void)spl0();	/* XXXSMP */
    438   1.79   mycroft 			return (EINVAL);
    439   1.79   mycroft 		}
    440  1.109  jdolecek 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    441  1.104   thorpej 		(void)spl0();		/* XXXSMP */
    442   1.79   mycroft 	}
    443   1.79   mycroft 
    444   1.79   mycroft 	return (0);
    445   1.79   mycroft }
    446   1.79   mycroft 
    447   1.29       cgd /*
    448   1.29       cgd  * Manipulate signal mask.
    449   1.29       cgd  * Note that we receive new mask, not pointer,
    450   1.29       cgd  * and return old mask as return value;
    451   1.29       cgd  * the library stub does the rest.
    452   1.29       cgd  */
    453   1.52  christos int
    454  1.130   thorpej sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
    455   1.48   thorpej {
    456   1.79   mycroft 	struct sys___sigprocmask14_args /* {
    457  1.112     lukem 		syscallarg(int)			how;
    458  1.112     lukem 		syscallarg(const sigset_t *)	set;
    459  1.112     lukem 		syscallarg(sigset_t *)		oset;
    460   1.48   thorpej 	} */ *uap = v;
    461  1.130   thorpej 	struct proc	*p;
    462  1.112     lukem 	sigset_t	nss, oss;
    463  1.112     lukem 	int		error;
    464   1.29       cgd 
    465   1.79   mycroft 	if (SCARG(uap, set)) {
    466   1.79   mycroft 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
    467   1.79   mycroft 		if (error)
    468   1.79   mycroft 			return (error);
    469   1.79   mycroft 	}
    470  1.130   thorpej 	p = l->l_proc;
    471   1.79   mycroft 	error = sigprocmask1(p, SCARG(uap, how),
    472   1.79   mycroft 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
    473   1.79   mycroft 	if (error)
    474   1.79   mycroft 		return (error);
    475   1.79   mycroft 	if (SCARG(uap, oset)) {
    476   1.79   mycroft 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
    477   1.79   mycroft 		if (error)
    478   1.79   mycroft 			return (error);
    479   1.79   mycroft 	}
    480   1.79   mycroft 	return (0);
    481   1.79   mycroft }
    482   1.79   mycroft 
    483   1.79   mycroft void
    484  1.112     lukem sigpending1(struct proc *p, sigset_t *ss)
    485   1.79   mycroft {
    486   1.29       cgd 
    487  1.109  jdolecek 	*ss = p->p_sigctx.ps_siglist;
    488  1.109  jdolecek 	sigminusset(&p->p_sigctx.ps_sigmask, ss);
    489   1.29       cgd }
    490   1.29       cgd 
    491   1.29       cgd /* ARGSUSED */
    492   1.52  christos int
    493  1.130   thorpej sys___sigpending14(struct lwp *l, void *v, register_t *retval)
    494   1.29       cgd {
    495   1.98  augustss 	struct sys___sigpending14_args /* {
    496  1.112     lukem 		syscallarg(sigset_t *)	set;
    497   1.79   mycroft 	} */ *uap = v;
    498  1.130   thorpej 	struct proc	*p;
    499  1.130   thorpej 	sigset_t	ss;
    500   1.79   mycroft 
    501  1.130   thorpej 	p = l->l_proc;
    502   1.79   mycroft 	sigpending1(p, &ss);
    503   1.79   mycroft 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
    504   1.79   mycroft }
    505   1.79   mycroft 
    506   1.79   mycroft int
    507  1.112     lukem sigsuspend1(struct proc *p, const sigset_t *ss)
    508   1.79   mycroft {
    509  1.112     lukem 	struct sigacts *ps;
    510   1.29       cgd 
    511  1.112     lukem 	ps = p->p_sigacts;
    512   1.79   mycroft 	if (ss) {
    513   1.79   mycroft 		/*
    514   1.79   mycroft 		 * When returning from sigpause, we want
    515   1.79   mycroft 		 * the old mask to be restored after the
    516   1.79   mycroft 		 * signal handler has finished.  Thus, we
    517  1.109  jdolecek 		 * save it here and mark the sigctx structure
    518   1.79   mycroft 		 * to indicate this.
    519   1.79   mycroft 		 */
    520  1.109  jdolecek 		p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
    521  1.109  jdolecek 		p->p_sigctx.ps_flags |= SAS_OLDMASK;
    522  1.105   thorpej 		(void) splsched();	/* XXXSMP */
    523  1.109  jdolecek 		p->p_sigctx.ps_sigmask = *ss;
    524  1.110   thorpej 		CHECKSIGS(p);
    525  1.109  jdolecek 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    526  1.104   thorpej 		(void) spl0();		/* XXXSMP */
    527   1.79   mycroft 	}
    528   1.79   mycroft 
    529   1.79   mycroft 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
    530   1.79   mycroft 		/* void */;
    531   1.79   mycroft 	/* always return EINTR rather than ERESTART... */
    532   1.79   mycroft 	return (EINTR);
    533   1.29       cgd }
    534   1.29       cgd 
    535   1.29       cgd /*
    536   1.29       cgd  * Suspend process until signal, providing mask to be set
    537   1.29       cgd  * in the meantime.  Note nonstandard calling convention:
    538   1.29       cgd  * libc stub passes mask, not pointer, to save a copyin.
    539   1.29       cgd  */
    540   1.29       cgd /* ARGSUSED */
    541   1.29       cgd int
    542  1.130   thorpej sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
    543   1.48   thorpej {
    544   1.79   mycroft 	struct sys___sigsuspend14_args /* {
    545  1.112     lukem 		syscallarg(const sigset_t *)	set;
    546   1.48   thorpej 	} */ *uap = v;
    547  1.130   thorpej 	struct proc	*p;
    548  1.112     lukem 	sigset_t	ss;
    549  1.112     lukem 	int		error;
    550   1.79   mycroft 
    551   1.79   mycroft 	if (SCARG(uap, set)) {
    552   1.79   mycroft 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
    553   1.79   mycroft 		if (error)
    554   1.79   mycroft 			return (error);
    555   1.79   mycroft 	}
    556   1.79   mycroft 
    557  1.130   thorpej 	p = l->l_proc;
    558   1.79   mycroft 	return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
    559   1.79   mycroft }
    560   1.79   mycroft 
    561   1.79   mycroft int
    562  1.112     lukem sigaltstack1(struct proc *p, const struct sigaltstack *nss,
    563  1.112     lukem 	struct sigaltstack *oss)
    564   1.79   mycroft {
    565  1.112     lukem 
    566   1.79   mycroft 	if (oss)
    567  1.109  jdolecek 		*oss = p->p_sigctx.ps_sigstk;
    568   1.79   mycroft 
    569   1.79   mycroft 	if (nss) {
    570   1.79   mycroft 		if (nss->ss_flags & ~SS_ALLBITS)
    571   1.79   mycroft 			return (EINVAL);
    572   1.79   mycroft 
    573   1.79   mycroft 		if (nss->ss_flags & SS_DISABLE) {
    574  1.109  jdolecek 			if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
    575   1.79   mycroft 				return (EINVAL);
    576   1.79   mycroft 		} else {
    577   1.79   mycroft 			if (nss->ss_size < MINSIGSTKSZ)
    578   1.79   mycroft 				return (ENOMEM);
    579   1.79   mycroft 		}
    580  1.109  jdolecek 		p->p_sigctx.ps_sigstk = *nss;
    581   1.79   mycroft 	}
    582   1.79   mycroft 
    583   1.79   mycroft 	return (0);
    584   1.29       cgd }
    585   1.29       cgd 
    586   1.29       cgd /* ARGSUSED */
    587   1.52  christos int
    588  1.130   thorpej sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
    589   1.48   thorpej {
    590   1.98  augustss 	struct sys___sigaltstack14_args /* {
    591  1.112     lukem 		syscallarg(const struct sigaltstack *)	nss;
    592  1.112     lukem 		syscallarg(struct sigaltstack *)	oss;
    593   1.48   thorpej 	} */ *uap = v;
    594  1.130   thorpej 	struct proc		*p;
    595  1.112     lukem 	struct sigaltstack	nss, oss;
    596  1.112     lukem 	int			error;
    597   1.29       cgd 
    598   1.79   mycroft 	if (SCARG(uap, nss)) {
    599   1.79   mycroft 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    600   1.79   mycroft 		if (error)
    601   1.79   mycroft 			return (error);
    602   1.79   mycroft 	}
    603  1.130   thorpej 	p = l->l_proc;
    604   1.79   mycroft 	error = sigaltstack1(p,
    605   1.79   mycroft 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    606   1.52  christos 	if (error)
    607   1.29       cgd 		return (error);
    608   1.79   mycroft 	if (SCARG(uap, oss)) {
    609   1.79   mycroft 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    610   1.79   mycroft 		if (error)
    611   1.79   mycroft 			return (error);
    612   1.29       cgd 	}
    613   1.29       cgd 	return (0);
    614   1.29       cgd }
    615   1.29       cgd 
    616   1.29       cgd /* ARGSUSED */
    617   1.29       cgd int
    618  1.130   thorpej sys_kill(struct lwp *l, void *v, register_t *retval)
    619   1.48   thorpej {
    620   1.98  augustss 	struct sys_kill_args /* {
    621  1.112     lukem 		syscallarg(int)	pid;
    622  1.112     lukem 		syscallarg(int)	signum;
    623   1.48   thorpej 	} */ *uap = v;
    624  1.130   thorpej 	struct proc	*cp, *p;
    625  1.112     lukem 	struct pcred	*pc;
    626   1.29       cgd 
    627  1.130   thorpej 	cp = l->l_proc;
    628  1.112     lukem 	pc = cp->p_cred;
    629   1.32       cgd 	if ((u_int)SCARG(uap, signum) >= NSIG)
    630   1.29       cgd 		return (EINVAL);
    631   1.32       cgd 	if (SCARG(uap, pid) > 0) {
    632   1.29       cgd 		/* kill single process */
    633   1.32       cgd 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    634   1.29       cgd 			return (ESRCH);
    635   1.32       cgd 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
    636   1.29       cgd 			return (EPERM);
    637   1.32       cgd 		if (SCARG(uap, signum))
    638   1.32       cgd 			psignal(p, SCARG(uap, signum));
    639   1.29       cgd 		return (0);
    640   1.29       cgd 	}
    641   1.32       cgd 	switch (SCARG(uap, pid)) {
    642   1.29       cgd 	case -1:		/* broadcast signal */
    643   1.32       cgd 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
    644   1.29       cgd 	case 0:			/* signal own process group */
    645   1.32       cgd 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
    646   1.29       cgd 	default:		/* negative explicit process group */
    647   1.32       cgd 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
    648   1.29       cgd 	}
    649   1.29       cgd 	/* NOTREACHED */
    650   1.29       cgd }
    651   1.29       cgd 
    652   1.29       cgd /*
    653   1.29       cgd  * Common code for kill process group/broadcast kill.
    654   1.29       cgd  * cp is calling process.
    655   1.29       cgd  */
    656   1.52  christos int
    657  1.112     lukem killpg1(struct proc *cp, int signum, int pgid, int all)
    658   1.29       cgd {
    659  1.112     lukem 	struct proc	*p;
    660  1.112     lukem 	struct pcred	*pc;
    661  1.112     lukem 	struct pgrp	*pgrp;
    662  1.112     lukem 	int		nfound;
    663   1.29       cgd 
    664  1.112     lukem 	pc = cp->p_cred;
    665  1.112     lukem 	nfound = 0;
    666   1.91   thorpej 	if (all) {
    667   1.29       cgd 		/*
    668   1.29       cgd 		 * broadcast
    669   1.29       cgd 		 */
    670   1.92   thorpej 		proclist_lock_read();
    671  1.124      matt 		LIST_FOREACH(p, &allproc, p_list) {
    672   1.29       cgd 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    673   1.29       cgd 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
    674   1.29       cgd 				continue;
    675   1.29       cgd 			nfound++;
    676   1.29       cgd 			if (signum)
    677   1.29       cgd 				psignal(p, signum);
    678   1.29       cgd 		}
    679   1.91   thorpej 		proclist_unlock_read();
    680   1.91   thorpej 	} else {
    681   1.29       cgd 		if (pgid == 0)
    682   1.29       cgd 			/*
    683   1.29       cgd 			 * zero pgid means send to my process group.
    684   1.29       cgd 			 */
    685   1.29       cgd 			pgrp = cp->p_pgrp;
    686   1.29       cgd 		else {
    687   1.29       cgd 			pgrp = pgfind(pgid);
    688   1.29       cgd 			if (pgrp == NULL)
    689   1.29       cgd 				return (ESRCH);
    690   1.29       cgd 		}
    691  1.124      matt 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
    692   1.29       cgd 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    693   1.29       cgd 			    !CANSIGNAL(cp, pc, p, signum))
    694   1.29       cgd 				continue;
    695   1.29       cgd 			nfound++;
    696   1.90   thorpej 			if (signum && P_ZOMBIE(p) == 0)
    697   1.29       cgd 				psignal(p, signum);
    698   1.29       cgd 		}
    699   1.29       cgd 	}
    700   1.29       cgd 	return (nfound ? 0 : ESRCH);
    701   1.29       cgd }
    702   1.29       cgd 
    703   1.29       cgd /*
    704   1.29       cgd  * Send a signal to a process group.
    705   1.29       cgd  */
    706   1.29       cgd void
    707  1.112     lukem gsignal(int pgid, int signum)
    708   1.29       cgd {
    709   1.29       cgd 	struct pgrp *pgrp;
    710   1.29       cgd 
    711   1.29       cgd 	if (pgid && (pgrp = pgfind(pgid)))
    712   1.29       cgd 		pgsignal(pgrp, signum, 0);
    713   1.29       cgd }
    714   1.29       cgd 
    715   1.29       cgd /*
    716   1.71      fvdl  * Send a signal to a process group. If checktty is 1,
    717   1.29       cgd  * limit to members which have a controlling terminal.
    718   1.29       cgd  */
    719   1.29       cgd void
    720  1.112     lukem pgsignal(struct pgrp *pgrp, int signum, int checkctty)
    721   1.29       cgd {
    722   1.98  augustss 	struct proc *p;
    723   1.29       cgd 
    724   1.29       cgd 	if (pgrp)
    725  1.124      matt 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
    726   1.29       cgd 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
    727   1.29       cgd 				psignal(p, signum);
    728   1.29       cgd }
    729   1.29       cgd 
    730   1.29       cgd /*
    731   1.29       cgd  * Send a signal caused by a trap to the current process.
    732   1.29       cgd  * If it will be caught immediately, deliver it with correct code.
    733   1.29       cgd  * Otherwise, post it normally.
    734   1.29       cgd  */
    735   1.29       cgd void
    736  1.130   thorpej trapsignal(struct lwp *l, int signum, u_long code)
    737   1.29       cgd {
    738  1.130   thorpej 	struct proc	*p;
    739  1.130   thorpej 	struct sigacts	*ps;
    740   1.29       cgd 
    741  1.130   thorpej 	p = l->l_proc;
    742  1.112     lukem 	ps = p->p_sigacts;
    743   1.79   mycroft 	if ((p->p_flag & P_TRACED) == 0 &&
    744  1.109  jdolecek 	    sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
    745  1.109  jdolecek 	    !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
    746   1.29       cgd 		p->p_stats->p_ru.ru_nsignals++;
    747   1.29       cgd #ifdef KTRACE
    748   1.29       cgd 		if (KTRPOINT(p, KTR_PSIG))
    749  1.100  sommerfe 			ktrpsig(p, signum,
    750  1.109  jdolecek 			    SIGACTION_PS(ps, signum).sa_handler,
    751  1.109  jdolecek 			    &p->p_sigctx.ps_sigmask, code);
    752   1.29       cgd #endif
    753  1.130   thorpej 		psendsig(l, signum, &p->p_sigctx.ps_sigmask, code);
    754  1.105   thorpej 		(void) splsched();	/* XXXSMP */
    755  1.112     lukem 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
    756  1.112     lukem 		    &p->p_sigctx.ps_sigmask);
    757  1.109  jdolecek 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
    758  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    759   1.45   mycroft 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
    760  1.109  jdolecek 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    761  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    762   1.45   mycroft 		}
    763  1.104   thorpej 		(void) spl0();		/* XXXSMP */
    764   1.29       cgd 	} else {
    765  1.109  jdolecek 		p->p_sigctx.ps_code = code;	/* XXX for core dump/debugger */
    766  1.109  jdolecek 		p->p_sigctx.ps_sig = signum;	/* XXX to verify code */
    767   1.29       cgd 		psignal(p, signum);
    768   1.29       cgd 	}
    769   1.29       cgd }
    770   1.29       cgd 
    771   1.29       cgd /*
    772   1.29       cgd  * Send the signal to the process.  If the signal has an action, the action
    773   1.29       cgd  * is usually performed by the target process rather than the caller; we add
    774   1.29       cgd  * the signal to the set of pending signals for the process.
    775   1.29       cgd  *
    776   1.29       cgd  * Exceptions:
    777   1.29       cgd  *   o When a stop signal is sent to a sleeping process that takes the
    778   1.29       cgd  *     default action, the process is stopped without awakening it.
    779   1.29       cgd  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
    780   1.29       cgd  *     regardless of the signal action (eg, blocked or ignored).
    781   1.29       cgd  *
    782   1.29       cgd  * Other ignored signals are discarded immediately.
    783  1.104   thorpej  *
    784  1.104   thorpej  * XXXSMP: Invoked as psignal() or sched_psignal().
    785   1.29       cgd  */
    786   1.29       cgd void
    787  1.112     lukem psignal1(struct proc *p, int signum,
    788  1.112     lukem 	int dolock)		/* XXXSMP: works, but icky */
    789   1.29       cgd {
    790  1.130   thorpej 	struct lwp *l, *suspended;
    791  1.130   thorpej 	int	s = 0, prop, allsusp;
    792  1.112     lukem 	sig_t	action;
    793   1.29       cgd 
    794   1.79   mycroft #ifdef DIAGNOSTIC
    795   1.79   mycroft 	if (signum <= 0 || signum >= NSIG)
    796   1.29       cgd 		panic("psignal signal number");
    797  1.104   thorpej 
    798  1.104   thorpej 	/* XXXSMP: works, but icky */
    799  1.104   thorpej 	if (dolock)
    800  1.104   thorpej 		SCHED_ASSERT_UNLOCKED();
    801  1.104   thorpej 	else
    802  1.104   thorpej 		SCHED_ASSERT_LOCKED();
    803   1.79   mycroft #endif
    804  1.126  jdolecek 	/*
    805  1.126  jdolecek 	 * Notify any interested parties in the signal.
    806  1.126  jdolecek 	 */
    807  1.126  jdolecek 	KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
    808  1.126  jdolecek 
    809   1.29       cgd 	prop = sigprop[signum];
    810   1.29       cgd 
    811   1.29       cgd 	/*
    812   1.29       cgd 	 * If proc is traced, always give parent a chance.
    813   1.29       cgd 	 */
    814   1.29       cgd 	if (p->p_flag & P_TRACED)
    815   1.29       cgd 		action = SIG_DFL;
    816   1.29       cgd 	else {
    817   1.29       cgd 		/*
    818   1.29       cgd 		 * If the signal is being ignored,
    819   1.29       cgd 		 * then we forget about it immediately.
    820  1.109  jdolecek 		 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
    821   1.29       cgd 		 * and if it is set to SIG_IGN,
    822   1.29       cgd 		 * action will be SIG_DFL here.)
    823   1.29       cgd 		 */
    824  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigignore, signum))
    825   1.29       cgd 			return;
    826  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigmask, signum))
    827   1.29       cgd 			action = SIG_HOLD;
    828  1.109  jdolecek 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
    829   1.29       cgd 			action = SIG_CATCH;
    830   1.44   mycroft 		else {
    831   1.29       cgd 			action = SIG_DFL;
    832   1.44   mycroft 
    833   1.44   mycroft 			if (prop & SA_KILL && p->p_nice > NZERO)
    834   1.44   mycroft 				p->p_nice = NZERO;
    835   1.44   mycroft 
    836   1.44   mycroft 			/*
    837   1.44   mycroft 			 * If sending a tty stop signal to a member of an
    838   1.44   mycroft 			 * orphaned process group, discard the signal here if
    839   1.44   mycroft 			 * the action is default; don't stop the process below
    840   1.44   mycroft 			 * if sleeping, and don't clear any pending SIGCONT.
    841   1.44   mycroft 			 */
    842   1.44   mycroft 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
    843   1.44   mycroft 				return;
    844   1.44   mycroft 		}
    845   1.29       cgd 	}
    846   1.29       cgd 
    847   1.29       cgd 	if (prop & SA_CONT)
    848  1.109  jdolecek 		sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
    849   1.29       cgd 
    850   1.44   mycroft 	if (prop & SA_STOP)
    851  1.109  jdolecek 		sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
    852   1.44   mycroft 
    853  1.109  jdolecek 	sigaddset(&p->p_sigctx.ps_siglist, signum);
    854  1.110   thorpej 
    855  1.110   thorpej 	/* CHECKSIGS() is "inlined" here. */
    856  1.109  jdolecek 	p->p_sigctx.ps_sigcheck = 1;
    857   1.29       cgd 
    858   1.29       cgd 	/*
    859   1.29       cgd 	 * Defer further processing for signals which are held,
    860   1.29       cgd 	 * except that stopped processes must be continued by SIGCONT.
    861   1.29       cgd 	 */
    862   1.29       cgd 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
    863   1.29       cgd 		return;
    864  1.104   thorpej 	/* XXXSMP: works, but icky */
    865  1.104   thorpej 	if (dolock)
    866  1.104   thorpej 		SCHED_LOCK(s);
    867  1.104   thorpej 
    868  1.130   thorpej 	if (p->p_nrlwps > 0) {
    869   1.29       cgd 		/*
    870  1.130   thorpej 		 * At least one LWP is running or on a run queue.
    871  1.130   thorpej 		 * The signal will be noticed when one of them returns
    872  1.130   thorpej 		 * to userspace.
    873   1.29       cgd 		 */
    874  1.130   thorpej 		signotify(p);
    875  1.130   thorpej 		/*
    876  1.130   thorpej 		 * The signal will be noticed very soon.
    877   1.29       cgd 		 */
    878  1.130   thorpej 		goto out;
    879  1.130   thorpej 	} else {
    880  1.130   thorpej 		/* Process is sleeping or stopped */
    881  1.130   thorpej 		if (p->p_flag & P_SA) {
    882  1.130   thorpej 			l = p->p_sa->sa_idle;
    883  1.130   thorpej 		} else {
    884  1.130   thorpej 			/*
    885  1.130   thorpej 			 * Find out if any of the sleeps are interruptable,
    886  1.130   thorpej 			 * and if all the live LWPs remaining are suspended.
    887  1.130   thorpej 			 */
    888  1.130   thorpej 			allsusp = 1;
    889  1.130   thorpej 			for (l = LIST_FIRST(&p->p_lwps);
    890  1.130   thorpej 			     l != NULL;
    891  1.130   thorpej 			     l = LIST_NEXT(l, l_sibling)) {
    892  1.130   thorpej 				if (l->l_stat == LSSLEEP &&
    893  1.130   thorpej 				    l->l_flag & L_SINTR)
    894  1.130   thorpej 					break;
    895  1.130   thorpej 				if (l->l_stat == LSSUSPENDED)
    896  1.130   thorpej 					suspended = l;
    897  1.130   thorpej 				else if ((l->l_stat != LSZOMB) &&
    898  1.130   thorpej 				         (l->l_stat != LSDEAD))
    899  1.130   thorpej 					allsusp = 0;
    900  1.130   thorpej 			}
    901   1.29       cgd 		}
    902  1.130   thorpej 		if (p->p_stat == SACTIVE) {
    903  1.130   thorpej 			/* All LWPs must be sleeping */
    904  1.130   thorpej 			KDASSERT(((p->p_flag & P_SA) == 0) || (l != NULL));
    905  1.130   thorpej 
    906  1.130   thorpej 			if (l != NULL && (p->p_flag & P_TRACED))
    907  1.130   thorpej 				goto run;
    908  1.130   thorpej 
    909   1.29       cgd 			/*
    910  1.130   thorpej 			 * If SIGCONT is default (or ignored) and process is
    911  1.130   thorpej 			 * asleep, we are finished; the process should not
    912  1.130   thorpej 			 * be awakened.
    913   1.29       cgd 			 */
    914  1.130   thorpej 			if ((prop & SA_CONT) && action == SIG_DFL) {
    915  1.130   thorpej 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    916   1.29       cgd 				goto out;
    917  1.130   thorpej 			}
    918  1.130   thorpej 
    919  1.130   thorpej 			/*
    920  1.130   thorpej 			 * When a sleeping process receives a stop
    921  1.130   thorpej 			 * signal, process immediately if possible.
    922  1.130   thorpej 			 */
    923  1.130   thorpej 			if ((prop & SA_STOP) && action == SIG_DFL) {
    924  1.104   thorpej 				/*
    925  1.130   thorpej 				 * If a child holding parent blocked,
    926  1.130   thorpej 				 * stopping could cause deadlock.
    927  1.104   thorpej 				 */
    928  1.130   thorpej 				if (p->p_flag & P_PPWAIT)
    929  1.130   thorpej 					goto out;
    930  1.130   thorpej 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    931  1.130   thorpej 				p->p_xstat = signum;
    932  1.130   thorpej 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
    933  1.130   thorpej 					/*
    934  1.130   thorpej 					 * XXXSMP: recursive call; don't lock
    935  1.130   thorpej 					 * the second time around.
    936  1.130   thorpej 					 */
    937  1.130   thorpej 					sched_psignal(p->p_pptr, SIGCHLD);
    938  1.130   thorpej 				}
    939  1.130   thorpej 				proc_stop(p);	/* XXXSMP: recurse? */
    940  1.130   thorpej 				goto out;
    941  1.104   thorpej 			}
    942   1.29       cgd 
    943  1.130   thorpej 			if (l == NULL) {
    944  1.130   thorpej 				/*
    945  1.130   thorpej 				 * Special case: SIGKILL of a process
    946  1.130   thorpej 				 * which is entirely composed of
    947  1.130   thorpej 				 * suspended LWPs should succeed. We
    948  1.130   thorpej 				 * make this happen by unsuspending one of
    949  1.130   thorpej 				 * them.
    950  1.130   thorpej 				 */
    951  1.130   thorpej 				if (allsusp && (signum == SIGKILL))
    952  1.130   thorpej 					lwp_continue(suspended);
    953  1.130   thorpej 				goto out;
    954  1.130   thorpej 			}
    955  1.130   thorpej 			/*
    956  1.130   thorpej 			 * All other (caught or default) signals
    957  1.130   thorpej 			 * cause the process to run.
    958  1.130   thorpej 			 */
    959   1.29       cgd 			goto runfast;
    960  1.130   thorpej 			/*NOTREACHED*/
    961  1.130   thorpej 		} else if (p->p_stat == SSTOP) {
    962  1.130   thorpej 			/* Process is stopped */
    963  1.130   thorpej 			/*
    964  1.130   thorpej 			 * If traced process is already stopped,
    965  1.130   thorpej 			 * then no further action is necessary.
    966  1.130   thorpej 			 */
    967  1.130   thorpej 			if (p->p_flag & P_TRACED)
    968  1.130   thorpej 				goto out;
    969   1.29       cgd 
    970   1.29       cgd 			/*
    971  1.130   thorpej 			 * Kill signal always sets processes running,
    972  1.130   thorpej 			 * if possible.
    973   1.29       cgd 			 */
    974  1.130   thorpej 			if (signum == SIGKILL) {
    975  1.130   thorpej 				l = proc_unstop(p);
    976  1.130   thorpej 				if (l)
    977  1.130   thorpej 					goto runfast;
    978  1.130   thorpej 				/* XXX should this be possible? */
    979  1.130   thorpej 				goto out;
    980  1.130   thorpej 			}
    981  1.130   thorpej 
    982  1.130   thorpej 			if (prop & SA_CONT) {
    983  1.130   thorpej 				/*
    984  1.130   thorpej 				 * If SIGCONT is default (or ignored),
    985  1.130   thorpej 				 * we continue the process but don't
    986  1.130   thorpej 				 * leave the signal in ps_siglist, as
    987  1.130   thorpej 				 * it has no further action.  If
    988  1.130   thorpej 				 * SIGCONT is held, we continue the
    989  1.130   thorpej 				 * process and leave the signal in
    990  1.130   thorpej 				 * ps_siglist.  If the process catches
    991  1.130   thorpej 				 * SIGCONT, let it handle the signal
    992  1.130   thorpej 				 * itself.  If it isn't waiting on an
    993  1.130   thorpej 				 * event, then it goes back to run
    994  1.130   thorpej 				 * state.  Otherwise, process goes
    995  1.130   thorpej 				 * back to sleep state.
    996  1.130   thorpej 				 */
    997  1.130   thorpej 				if (action == SIG_DFL)
    998  1.130   thorpej 					sigdelset(&p->p_sigctx.ps_siglist,
    999  1.130   thorpej 					signum);
   1000  1.130   thorpej 				l = proc_unstop(p);
   1001  1.130   thorpej 				/*
   1002  1.130   thorpej 				 * XXX see note in proc_unstop(). SIGKILL
   1003  1.130   thorpej 				 * XXX and SIGCONT have conflicting needs.
   1004  1.130   thorpej 				 */
   1005  1.130   thorpej 				if (l && (l->l_stat == LSSLEEP))
   1006  1.130   thorpej 					l = NULL;
   1007  1.130   thorpej 				if (l && (action == SIG_CATCH))
   1008  1.130   thorpej 					goto runfast;
   1009  1.130   thorpej 				if (l)
   1010  1.130   thorpej 					goto run;
   1011  1.130   thorpej 				goto out;
   1012  1.130   thorpej 			}
   1013  1.130   thorpej 
   1014  1.130   thorpej 			if (prop & SA_STOP) {
   1015  1.130   thorpej 				/*
   1016  1.130   thorpej 				 * Already stopped, don't need to stop again.
   1017  1.130   thorpej 				 * (If we did the shell could get confused.)
   1018  1.130   thorpej 				 */
   1019  1.109  jdolecek 				sigdelset(&p->p_sigctx.ps_siglist, signum);
   1020  1.130   thorpej 				goto out;
   1021  1.130   thorpej 			}
   1022   1.29       cgd 
   1023   1.29       cgd 			/*
   1024  1.130   thorpej 			 * If process is sleeping interruptibly, then
   1025  1.130   thorpej 			 * simulate a wakeup so that when it is
   1026  1.130   thorpej 			 * continued, it will be made runnable and can
   1027  1.130   thorpej 			 * look at the signal.  But don't make the
   1028  1.130   thorpej 			 * process runnable, leave it stopped.
   1029   1.29       cgd 			 */
   1030  1.130   thorpej 			if (l)
   1031  1.130   thorpej 				unsleep(l);
   1032   1.29       cgd 			goto out;
   1033  1.130   thorpej 		} else {
   1034  1.130   thorpej 			/* Else what? */
   1035  1.130   thorpej 			panic("psignal: Invalid process state.");
   1036   1.29       cgd 		}
   1037   1.29       cgd 	}
   1038   1.29       cgd 	/*NOTREACHED*/
   1039   1.29       cgd 
   1040  1.112     lukem  runfast:
   1041   1.29       cgd 	/*
   1042   1.29       cgd 	 * Raise priority to at least PUSER.
   1043   1.29       cgd 	 */
   1044  1.130   thorpej 	if (l->l_priority > PUSER)
   1045  1.130   thorpej 		l->l_priority = PUSER;
   1046  1.112     lukem  run:
   1047  1.130   thorpej 	setrunnable(l);		/* XXXSMP: recurse? */
   1048  1.112     lukem  out:
   1049  1.104   thorpej 	/* XXXSMP: works, but icky */
   1050  1.104   thorpej 	if (dolock)
   1051  1.104   thorpej 		SCHED_UNLOCK(s);
   1052   1.29       cgd }
   1053   1.29       cgd 
   1054  1.130   thorpej void
   1055  1.130   thorpej psendsig(struct lwp *l, int sig, sigset_t *mask, u_long code)
   1056  1.130   thorpej {
   1057  1.130   thorpej 	struct proc *p = l->l_proc;
   1058  1.130   thorpej 	struct lwp *le, *li;
   1059  1.130   thorpej 	siginfo_t *si;
   1060  1.130   thorpej 
   1061  1.130   thorpej 	if (p->p_flag & P_SA) {
   1062  1.130   thorpej 		si = pool_get(&siginfo_pool, PR_WAITOK);
   1063  1.130   thorpej 		si->si_signo = sig;
   1064  1.130   thorpej 		si->si_errno = 0;
   1065  1.130   thorpej 		si->si_code = code;
   1066  1.130   thorpej 		le = li = NULL;
   1067  1.130   thorpej 		if (code)
   1068  1.130   thorpej 			le = l;
   1069  1.130   thorpej 		else
   1070  1.130   thorpej 			li = l;
   1071  1.130   thorpej 
   1072  1.130   thorpej 		sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
   1073  1.130   thorpej 			    sizeof(siginfo_t), si);
   1074  1.130   thorpej 		return;
   1075  1.130   thorpej 	}
   1076  1.130   thorpej 
   1077  1.130   thorpej 	(*p->p_emul->e_sendsig)(sig, mask, code);
   1078  1.130   thorpej }
   1079  1.130   thorpej 
   1080  1.112     lukem static __inline int firstsig(const sigset_t *);
   1081   1.79   mycroft 
   1082   1.79   mycroft static __inline int
   1083  1.112     lukem firstsig(const sigset_t *ss)
   1084   1.79   mycroft {
   1085   1.79   mycroft 	int sig;
   1086   1.79   mycroft 
   1087   1.79   mycroft 	sig = ffs(ss->__bits[0]);
   1088   1.79   mycroft 	if (sig != 0)
   1089   1.79   mycroft 		return (sig);
   1090   1.79   mycroft #if NSIG > 33
   1091   1.79   mycroft 	sig = ffs(ss->__bits[1]);
   1092   1.79   mycroft 	if (sig != 0)
   1093   1.79   mycroft 		return (sig + 32);
   1094   1.79   mycroft #endif
   1095   1.79   mycroft #if NSIG > 65
   1096   1.79   mycroft 	sig = ffs(ss->__bits[2]);
   1097   1.79   mycroft 	if (sig != 0)
   1098   1.79   mycroft 		return (sig + 64);
   1099   1.79   mycroft #endif
   1100   1.79   mycroft #if NSIG > 97
   1101   1.79   mycroft 	sig = ffs(ss->__bits[3]);
   1102   1.79   mycroft 	if (sig != 0)
   1103   1.79   mycroft 		return (sig + 96);
   1104   1.79   mycroft #endif
   1105   1.79   mycroft 	return (0);
   1106   1.79   mycroft }
   1107   1.79   mycroft 
   1108   1.29       cgd /*
   1109   1.29       cgd  * If the current process has received a signal (should be caught or cause
   1110   1.29       cgd  * termination, should interrupt current syscall), return the signal number.
   1111   1.29       cgd  * Stop signals with default action are processed immediately, then cleared;
   1112   1.29       cgd  * they aren't returned.  This is checked after each entry to the system for
   1113   1.29       cgd  * a syscall or trap (though this can usually be done without calling issignal
   1114   1.29       cgd  * by checking the pending signal masks in the CURSIG macro.) The normal call
   1115   1.29       cgd  * sequence is
   1116   1.29       cgd  *
   1117  1.130   thorpej  *	while (signum = CURSIG(curlwp))
   1118   1.29       cgd  *		postsig(signum);
   1119   1.29       cgd  */
   1120   1.29       cgd int
   1121  1.130   thorpej issignal(struct lwp *l)
   1122   1.29       cgd {
   1123  1.130   thorpej 	struct proc	*p = l->l_proc;
   1124  1.127       scw 	int		s = 0, signum, prop;
   1125  1.130   thorpej 	int		dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
   1126  1.112     lukem 	sigset_t	ss;
   1127   1.29       cgd 
   1128  1.130   thorpej 	if (p->p_stat == SSTOP) {
   1129  1.130   thorpej 		/*
   1130  1.130   thorpej 		 * The process is stopped/stopping. Stop ourselves now that
   1131  1.130   thorpej 		 * we're on the kernel/userspace boundary.
   1132  1.130   thorpej 		 */
   1133  1.130   thorpej 		if (dolock)
   1134  1.130   thorpej 			SCHED_LOCK(s);
   1135  1.130   thorpej 		l->l_stat = LSSTOP;
   1136  1.130   thorpej 		p->p_nrlwps--;
   1137  1.130   thorpej 		if (p->p_flag & P_TRACED)
   1138  1.130   thorpej 			goto sigtraceswitch;
   1139  1.130   thorpej 		else
   1140  1.130   thorpej 			goto sigswitch;
   1141  1.130   thorpej 	}
   1142   1.29       cgd 	for (;;) {
   1143   1.79   mycroft 		sigpending1(p, &ss);
   1144   1.29       cgd 		if (p->p_flag & P_PPWAIT)
   1145   1.79   mycroft 			sigminusset(&stopsigmask, &ss);
   1146   1.79   mycroft 		signum = firstsig(&ss);
   1147   1.79   mycroft 		if (signum == 0) {		 	/* no signal to send */
   1148  1.109  jdolecek 			p->p_sigctx.ps_sigcheck = 0;
   1149  1.119  christos 			if (locked && dolock)
   1150  1.119  christos 				SCHED_LOCK(s);
   1151   1.29       cgd 			return (0);
   1152   1.79   mycroft 		}
   1153  1.112     lukem 							/* take the signal! */
   1154  1.112     lukem 		sigdelset(&p->p_sigctx.ps_siglist, signum);
   1155   1.42   mycroft 
   1156   1.29       cgd 		/*
   1157   1.29       cgd 		 * We should see pending but ignored signals
   1158   1.29       cgd 		 * only if P_TRACED was on when they were posted.
   1159   1.29       cgd 		 */
   1160  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
   1161   1.79   mycroft 		    (p->p_flag & P_TRACED) == 0)
   1162   1.29       cgd 			continue;
   1163   1.42   mycroft 
   1164   1.29       cgd 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
   1165   1.29       cgd 			/*
   1166   1.29       cgd 			 * If traced, always stop, and stay
   1167   1.29       cgd 			 * stopped until released by the debugger.
   1168   1.29       cgd 			 */
   1169   1.29       cgd 			p->p_xstat = signum;
   1170   1.66   mycroft 			if ((p->p_flag & P_FSTRACE) == 0)
   1171  1.119  christos 				psignal1(p->p_pptr, SIGCHLD, dolock);
   1172  1.119  christos 			if (dolock)
   1173  1.119  christos 				SCHED_LOCK(s);
   1174  1.114   nathanw 			proc_stop(p);
   1175  1.130   thorpej 		sigtraceswitch:
   1176  1.130   thorpej 			mi_switch(l, NULL);
   1177  1.114   nathanw 			SCHED_ASSERT_UNLOCKED();
   1178  1.119  christos 			if (dolock)
   1179  1.119  christos 				splx(s);
   1180  1.119  christos 			else
   1181  1.119  christos 				dolock = 1;
   1182   1.29       cgd 
   1183   1.29       cgd 			/*
   1184   1.42   mycroft 			 * If we are no longer being traced, or the parent
   1185   1.42   mycroft 			 * didn't give us a signal, look for more signals.
   1186   1.29       cgd 			 */
   1187   1.42   mycroft 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
   1188   1.29       cgd 				continue;
   1189   1.29       cgd 
   1190   1.29       cgd 			/*
   1191   1.42   mycroft 			 * If the new signal is being masked, look for other
   1192   1.42   mycroft 			 * signals.
   1193   1.29       cgd 			 */
   1194   1.42   mycroft 			signum = p->p_xstat;
   1195  1.130   thorpej 			p->p_xstat = 0;
   1196  1.112     lukem 			/*
   1197  1.112     lukem 			 * `p->p_sigctx.ps_siglist |= mask' is done
   1198  1.112     lukem 			 * in setrunnable().
   1199  1.112     lukem 			 */
   1200  1.109  jdolecek 			if (sigismember(&p->p_sigctx.ps_sigmask, signum))
   1201   1.29       cgd 				continue;
   1202  1.112     lukem 							/* take the signal! */
   1203  1.112     lukem 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1204   1.29       cgd 		}
   1205   1.29       cgd 
   1206   1.42   mycroft 		prop = sigprop[signum];
   1207   1.42   mycroft 
   1208   1.29       cgd 		/*
   1209   1.29       cgd 		 * Decide whether the signal should be returned.
   1210   1.29       cgd 		 * Return the signal's number, or fall through
   1211   1.29       cgd 		 * to clear it from the pending mask.
   1212   1.29       cgd 		 */
   1213  1.109  jdolecek 		switch ((long)SIGACTION(p, signum).sa_handler) {
   1214   1.29       cgd 
   1215   1.33       cgd 		case (long)SIG_DFL:
   1216   1.29       cgd 			/*
   1217   1.29       cgd 			 * Don't take default actions on system processes.
   1218   1.29       cgd 			 */
   1219   1.29       cgd 			if (p->p_pid <= 1) {
   1220   1.29       cgd #ifdef DIAGNOSTIC
   1221   1.29       cgd 				/*
   1222   1.29       cgd 				 * Are you sure you want to ignore SIGSEGV
   1223   1.29       cgd 				 * in init? XXX
   1224   1.29       cgd 				 */
   1225   1.57  christos 				printf("Process (pid %d) got signal %d\n",
   1226   1.29       cgd 				    p->p_pid, signum);
   1227   1.29       cgd #endif
   1228   1.29       cgd 				break;		/* == ignore */
   1229   1.29       cgd 			}
   1230   1.29       cgd 			/*
   1231   1.29       cgd 			 * If there is a pending stop signal to process
   1232   1.29       cgd 			 * with default action, stop here,
   1233   1.29       cgd 			 * then clear the signal.  However,
   1234   1.29       cgd 			 * if process is member of an orphaned
   1235   1.29       cgd 			 * process group, ignore tty stop signals.
   1236   1.29       cgd 			 */
   1237   1.29       cgd 			if (prop & SA_STOP) {
   1238   1.29       cgd 				if (p->p_flag & P_TRACED ||
   1239   1.29       cgd 		    		    (p->p_pgrp->pg_jobc == 0 &&
   1240   1.29       cgd 				    prop & SA_TTYSTOP))
   1241   1.29       cgd 					break;	/* == ignore */
   1242   1.29       cgd 				p->p_xstat = signum;
   1243   1.29       cgd 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
   1244  1.119  christos 					psignal1(p->p_pptr, SIGCHLD, dolock);
   1245  1.119  christos 				if (dolock)
   1246  1.119  christos 					SCHED_LOCK(s);
   1247  1.104   thorpej 				proc_stop(p);
   1248  1.130   thorpej 			sigswitch:
   1249  1.130   thorpej 				mi_switch(l, NULL);
   1250  1.104   thorpej 				SCHED_ASSERT_UNLOCKED();
   1251  1.119  christos 				if (dolock)
   1252  1.119  christos 					splx(s);
   1253  1.119  christos 				else
   1254  1.119  christos 					dolock = 1;
   1255   1.29       cgd 				break;
   1256   1.29       cgd 			} else if (prop & SA_IGNORE) {
   1257   1.29       cgd 				/*
   1258   1.29       cgd 				 * Except for SIGCONT, shouldn't get here.
   1259   1.29       cgd 				 * Default action is to ignore; drop it.
   1260   1.29       cgd 				 */
   1261   1.29       cgd 				break;		/* == ignore */
   1262   1.29       cgd 			} else
   1263   1.42   mycroft 				goto keep;
   1264   1.29       cgd 			/*NOTREACHED*/
   1265   1.29       cgd 
   1266   1.33       cgd 		case (long)SIG_IGN:
   1267   1.29       cgd 			/*
   1268   1.29       cgd 			 * Masking above should prevent us ever trying
   1269   1.29       cgd 			 * to take action on an ignored signal other
   1270   1.29       cgd 			 * than SIGCONT, unless process is traced.
   1271   1.29       cgd 			 */
   1272  1.128  jdolecek #ifdef DEBUG_ISSIGNAL
   1273   1.29       cgd 			if ((prop & SA_CONT) == 0 &&
   1274   1.29       cgd 			    (p->p_flag & P_TRACED) == 0)
   1275   1.57  christos 				printf("issignal\n");
   1276  1.128  jdolecek #endif
   1277   1.29       cgd 			break;		/* == ignore */
   1278   1.29       cgd 
   1279   1.29       cgd 		default:
   1280   1.29       cgd 			/*
   1281   1.29       cgd 			 * This signal has an action, let
   1282   1.29       cgd 			 * postsig() process it.
   1283   1.29       cgd 			 */
   1284   1.42   mycroft 			goto keep;
   1285   1.29       cgd 		}
   1286   1.29       cgd 	}
   1287   1.29       cgd 	/* NOTREACHED */
   1288   1.42   mycroft 
   1289  1.112     lukem  keep:
   1290  1.112     lukem 						/* leave the signal for later */
   1291  1.112     lukem 	sigaddset(&p->p_sigctx.ps_siglist, signum);
   1292  1.110   thorpej 	CHECKSIGS(p);
   1293  1.119  christos 	if (locked && dolock)
   1294  1.119  christos 		SCHED_LOCK(s);
   1295   1.42   mycroft 	return (signum);
   1296   1.29       cgd }
   1297   1.29       cgd 
   1298   1.29       cgd /*
   1299   1.29       cgd  * Put the argument process into the stopped state and notify the parent
   1300   1.29       cgd  * via wakeup.  Signals are handled elsewhere.  The process must not be
   1301   1.29       cgd  * on the run queue.
   1302   1.29       cgd  */
   1303  1.104   thorpej static void
   1304  1.112     lukem proc_stop(struct proc *p)
   1305   1.29       cgd {
   1306  1.130   thorpej 	struct lwp *l;
   1307   1.29       cgd 
   1308  1.104   thorpej 	SCHED_ASSERT_LOCKED();
   1309  1.104   thorpej 
   1310  1.130   thorpej 	/* XXX lock process LWP state */
   1311   1.29       cgd 	p->p_stat = SSTOP;
   1312   1.29       cgd 	p->p_flag &= ~P_WAITED;
   1313  1.130   thorpej 
   1314  1.130   thorpej 	/*
   1315  1.130   thorpej 	 * Put as many LWP's as possible in stopped state.
   1316  1.130   thorpej 	 * Sleeping ones will notice the stopped state as they try to
   1317  1.130   thorpej 	 * return to userspace.
   1318  1.130   thorpej 	 */
   1319  1.130   thorpej 
   1320  1.130   thorpej 	for (l = LIST_FIRST(&p->p_lwps); l != NULL;
   1321  1.130   thorpej 	     l = LIST_NEXT(l, l_sibling)) {
   1322  1.130   thorpej 		if (l->l_stat == LSONPROC) {
   1323  1.130   thorpej 			/* XXX SMP this assumes that a LWP that is LSONPROC
   1324  1.130   thorpej 			 * is curlwp and hence is about to be mi_switched
   1325  1.130   thorpej 			 * away; the only callers of proc_stop() are:
   1326  1.130   thorpej 			 * - psignal
   1327  1.130   thorpej 			 * - issignal()
   1328  1.130   thorpej 			 * For the former, proc_stop() is only called when
   1329  1.130   thorpej 			 * no processes are running, so we don't worry.
   1330  1.130   thorpej 			 * For the latter, proc_stop() is called right
   1331  1.130   thorpej 			 * before mi_switch().
   1332  1.130   thorpej 			 */
   1333  1.130   thorpej 			l->l_stat = LSSTOP;
   1334  1.130   thorpej 			p->p_nrlwps--;
   1335  1.130   thorpej 		} else if (l->l_stat == LSRUN) {
   1336  1.130   thorpej 			/* Remove LWP from the run queue */
   1337  1.130   thorpej 			remrunqueue(l);
   1338  1.130   thorpej 			l->l_stat = LSSTOP;
   1339  1.130   thorpej 			p->p_nrlwps--;
   1340  1.130   thorpej 		} else if ((l->l_stat == LSSLEEP) ||
   1341  1.130   thorpej 		    (l->l_stat == LSSUSPENDED) ||
   1342  1.130   thorpej 		    (l->l_stat == LSZOMB) ||
   1343  1.130   thorpej 		    (l->l_stat == LSDEAD)) {
   1344  1.130   thorpej 			/*
   1345  1.130   thorpej 			 * Don't do anything; let sleeping LWPs
   1346  1.130   thorpej 			 * discover the stopped state of the process
   1347  1.130   thorpej 			 * on their way out of the kernel; otherwise,
   1348  1.130   thorpej 			 * things like NFS threads that sleep with
   1349  1.130   thorpej 			 * locks will block the rest of the system
   1350  1.130   thorpej 			 * from getting any work done.
   1351  1.130   thorpej 			 *
   1352  1.130   thorpej 			 * Suspended/dead/zombie LWPs aren't going
   1353  1.130   thorpej 			 * anywhere, so we don't need to touch them.
   1354  1.130   thorpej 			 */
   1355  1.130   thorpej 		}
   1356  1.130   thorpej #ifdef DIAGNOSTIC
   1357  1.130   thorpej 		else {
   1358  1.130   thorpej 			panic("proc_stop: process %d lwp %d "
   1359  1.130   thorpej 			      "in unstoppable state %d.\n",
   1360  1.130   thorpej 			    p->p_pid, l->l_lid, l->l_stat);
   1361  1.130   thorpej 		}
   1362  1.130   thorpej #endif
   1363  1.130   thorpej 	}
   1364  1.130   thorpej 	/* XXX unlock process LWP state */
   1365  1.130   thorpej 
   1366  1.104   thorpej 	sched_wakeup((caddr_t)p->p_pptr);
   1367   1.29       cgd }
   1368   1.29       cgd 
   1369  1.130   thorpej struct lwp *
   1370  1.130   thorpej proc_unstop(p)
   1371  1.130   thorpej 	struct proc *p;
   1372  1.130   thorpej {
   1373  1.130   thorpej 	struct lwp *l, *lr = NULL;
   1374  1.130   thorpej 
   1375  1.130   thorpej 	SCHED_ASSERT_LOCKED();
   1376  1.130   thorpej 
   1377  1.130   thorpej 	/*
   1378  1.130   thorpej 	 * Our caller will want to invoke setrunnable() on whatever we return,
   1379  1.130   thorpej 	 * provided that it isn't NULL.
   1380  1.130   thorpej 	 */
   1381  1.130   thorpej 
   1382  1.130   thorpej 	p->p_stat = SACTIVE;
   1383  1.130   thorpej 	/*
   1384  1.130   thorpej 	 * For the benefit of SIGKILL, return the idle LWP if there's
   1385  1.130   thorpej 	 * nothing better (and if there is an idle LWP, there
   1386  1.130   thorpej 	 * shouldn't be anything better.
   1387  1.130   thorpej 	 * XXX This is bad for SIGCONT; SIGSTOP/SIGCONT shouldn't
   1388  1.130   thorpej 	 * XXX noticably affect the state of a process, idling or
   1389  1.130   thorpej 	 * XXX not. We work around this in the SIGCONT handling in
   1390  1.130   thorpej 	 * XXX psignal().
   1391  1.130   thorpej 	 */
   1392  1.130   thorpej 	if (p->p_flag & P_SA)
   1393  1.130   thorpej 		lr = p->p_sa->sa_idle; /* OK if this is NULL. */
   1394  1.130   thorpej 	for (l = LIST_FIRST(&p->p_lwps); l != NULL;
   1395  1.130   thorpej 	     l = LIST_NEXT(l, l_sibling))
   1396  1.130   thorpej 		if (l->l_stat == LSSTOP) {
   1397  1.130   thorpej 			if (l->l_wchan == 0) {
   1398  1.130   thorpej 				if (lr == NULL || l == lr)
   1399  1.130   thorpej 					lr = l;
   1400  1.130   thorpej 				else
   1401  1.130   thorpej 					setrunnable(l);
   1402  1.130   thorpej 			} else
   1403  1.130   thorpej 				l->l_stat = LSSLEEP;
   1404  1.130   thorpej 		}
   1405  1.130   thorpej 
   1406  1.130   thorpej 	return lr;
   1407  1.130   thorpej }
   1408  1.130   thorpej 
   1409   1.29       cgd /*
   1410   1.29       cgd  * Take the action for the specified signal
   1411   1.29       cgd  * from the current set of pending signals.
   1412   1.29       cgd  */
   1413   1.29       cgd void
   1414  1.112     lukem postsig(int signum)
   1415   1.29       cgd {
   1416  1.130   thorpej 	struct lwp *l;
   1417  1.112     lukem 	struct proc	*p;
   1418  1.112     lukem 	struct sigacts	*ps;
   1419  1.112     lukem 	sig_t		action;
   1420  1.112     lukem 	u_long		code;
   1421  1.112     lukem 	sigset_t	*returnmask;
   1422   1.29       cgd 
   1423  1.130   thorpej 	l = curlwp;
   1424  1.130   thorpej 	p = l->l_proc;
   1425  1.112     lukem 	ps = p->p_sigacts;
   1426   1.29       cgd #ifdef DIAGNOSTIC
   1427   1.29       cgd 	if (signum == 0)
   1428   1.29       cgd 		panic("postsig");
   1429   1.29       cgd #endif
   1430  1.106   thorpej 
   1431  1.130   thorpej 	KERNEL_PROC_LOCK(l);
   1432  1.106   thorpej 
   1433  1.109  jdolecek 	sigdelset(&p->p_sigctx.ps_siglist, signum);
   1434  1.109  jdolecek 	action = SIGACTION_PS(ps, signum).sa_handler;
   1435   1.29       cgd #ifdef KTRACE
   1436   1.29       cgd 	if (KTRPOINT(p, KTR_PSIG))
   1437  1.100  sommerfe 		ktrpsig(p,
   1438  1.109  jdolecek 		    signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
   1439  1.109  jdolecek 		    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
   1440   1.29       cgd #endif
   1441   1.29       cgd 	if (action == SIG_DFL) {
   1442   1.29       cgd 		/*
   1443   1.29       cgd 		 * Default action, where the default is to kill
   1444   1.29       cgd 		 * the process.  (Other cases were ignored above.)
   1445   1.29       cgd 		 */
   1446  1.130   thorpej 		sigexit(l, signum);
   1447   1.29       cgd 		/* NOTREACHED */
   1448   1.29       cgd 	} else {
   1449   1.29       cgd 		/*
   1450   1.29       cgd 		 * If we get here, the signal must be caught.
   1451   1.29       cgd 		 */
   1452   1.29       cgd #ifdef DIAGNOSTIC
   1453  1.112     lukem 		if (action == SIG_IGN ||
   1454  1.112     lukem 		    sigismember(&p->p_sigctx.ps_sigmask, signum))
   1455   1.29       cgd 			panic("postsig action");
   1456   1.29       cgd #endif
   1457   1.29       cgd 		/*
   1458   1.29       cgd 		 * Set the new mask value and also defer further
   1459   1.29       cgd 		 * occurences of this signal.
   1460   1.29       cgd 		 *
   1461   1.29       cgd 		 * Special case: user has done a sigpause.  Here the
   1462   1.29       cgd 		 * current mask is not of interest, but rather the
   1463   1.29       cgd 		 * mask from before the sigpause is what we want
   1464   1.29       cgd 		 * restored after the signal processing is completed.
   1465   1.29       cgd 		 */
   1466  1.109  jdolecek 		if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
   1467  1.109  jdolecek 			returnmask = &p->p_sigctx.ps_oldmask;
   1468  1.109  jdolecek 			p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
   1469   1.29       cgd 		} else
   1470  1.109  jdolecek 			returnmask = &p->p_sigctx.ps_sigmask;
   1471   1.29       cgd 		p->p_stats->p_ru.ru_nsignals++;
   1472  1.109  jdolecek 		if (p->p_sigctx.ps_sig != signum) {
   1473   1.29       cgd 			code = 0;
   1474   1.29       cgd 		} else {
   1475  1.109  jdolecek 			code = p->p_sigctx.ps_code;
   1476  1.109  jdolecek 			p->p_sigctx.ps_code = 0;
   1477  1.109  jdolecek 			p->p_sigctx.ps_sig = 0;
   1478   1.29       cgd 		}
   1479  1.130   thorpej 		psendsig(l, signum, returnmask, code);
   1480  1.105   thorpej 		(void) splsched();	/* XXXSMP */
   1481  1.112     lukem 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
   1482  1.112     lukem 		    &p->p_sigctx.ps_sigmask);
   1483  1.109  jdolecek 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
   1484  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
   1485   1.79   mycroft 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
   1486  1.109  jdolecek 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
   1487  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
   1488   1.79   mycroft 		}
   1489  1.104   thorpej 		(void) spl0();		/* XXXSMP */
   1490   1.29       cgd 	}
   1491  1.106   thorpej 
   1492  1.130   thorpej 	KERNEL_PROC_UNLOCK(l);
   1493   1.29       cgd }
   1494   1.29       cgd 
   1495   1.29       cgd /*
   1496   1.29       cgd  * Kill the current process for stated reason.
   1497   1.29       cgd  */
   1498   1.52  christos void
   1499  1.122      manu killproc(struct proc *p, const char *why)
   1500   1.29       cgd {
   1501   1.29       cgd 
   1502   1.29       cgd 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
   1503   1.29       cgd 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
   1504   1.29       cgd 	psignal(p, SIGKILL);
   1505   1.29       cgd }
   1506   1.29       cgd 
   1507   1.29       cgd /*
   1508   1.29       cgd  * Force the current process to exit with the specified signal, dumping core
   1509   1.29       cgd  * if appropriate.  We bypass the normal tests for masked and caught signals,
   1510   1.29       cgd  * allowing unrecoverable failures to terminate the process without changing
   1511   1.29       cgd  * signal state.  Mark the accounting record with the signal termination.
   1512   1.29       cgd  * If dumping core, save the signal number for the debugger.  Calls exit and
   1513   1.29       cgd  * does not return.
   1514   1.29       cgd  */
   1515   1.96      fair 
   1516   1.97      fair #if defined(DEBUG)
   1517   1.96      fair int	kern_logsigexit = 1;	/* not static to make public for sysctl */
   1518   1.96      fair #else
   1519   1.96      fair int	kern_logsigexit = 0;	/* not static to make public for sysctl */
   1520   1.96      fair #endif
   1521   1.96      fair 
   1522  1.102  sommerfe static	const char logcoredump[] =
   1523   1.96      fair 	"pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
   1524  1.102  sommerfe static	const char lognocoredump[] =
   1525   1.96      fair 	"pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
   1526   1.96      fair 
   1527  1.130   thorpej /* Wrapper function for use in p_userret */
   1528  1.130   thorpej static void
   1529  1.130   thorpej lwp_coredump_hook(struct lwp *l, void *arg)
   1530  1.130   thorpej {
   1531  1.130   thorpej 	int s;
   1532  1.130   thorpej 
   1533  1.130   thorpej 	/*
   1534  1.130   thorpej 	 * Suspend ourselves, so that the kernel stack and therefore
   1535  1.130   thorpej 	 * the userland registers saved in the trapframe are around
   1536  1.130   thorpej 	 * for coredump() to write them out.
   1537  1.130   thorpej 	 */
   1538  1.130   thorpej 	KERNEL_PROC_LOCK(l);
   1539  1.130   thorpej 	l->l_flag &= ~L_DETACHED;
   1540  1.130   thorpej 	SCHED_LOCK(s);
   1541  1.130   thorpej 	l->l_stat = LSSUSPENDED;
   1542  1.130   thorpej 	l->l_proc->p_nrlwps--;
   1543  1.130   thorpej 	/* XXX NJWLWP check if this makes sense here: */
   1544  1.130   thorpej 	l->l_proc->p_stats->p_ru.ru_nvcsw++;
   1545  1.130   thorpej 	mi_switch(l, NULL);
   1546  1.130   thorpej 	SCHED_ASSERT_UNLOCKED();
   1547  1.130   thorpej 	splx(s);
   1548  1.130   thorpej 
   1549  1.130   thorpej 	lwp_exit(l);
   1550  1.130   thorpej }
   1551  1.130   thorpej 
   1552   1.52  christos void
   1553  1.130   thorpej sigexit(struct lwp *l, int signum)
   1554   1.29       cgd {
   1555  1.130   thorpej 	struct proc	*p;
   1556  1.130   thorpej 	int		error, exitsig;
   1557  1.130   thorpej 
   1558  1.130   thorpej 	p = l->l_proc;
   1559  1.130   thorpej 
   1560  1.130   thorpej 	/*
   1561  1.130   thorpej 	 * Don't permit coredump() or exit1() multiple times
   1562  1.130   thorpej 	 * in the same process.
   1563  1.130   thorpej 	 */
   1564  1.130   thorpej 	if (p->p_flag & P_WEXIT)
   1565  1.130   thorpej 		(*p->p_userret)(l, p->p_userret_arg);
   1566  1.130   thorpej 	p->p_flag |= P_WEXIT;
   1567  1.130   thorpej 	/* We don't want to switch away from exiting. */
   1568  1.130   thorpej 	/* XXX multiprocessor: stop LWPs on other processors. */
   1569  1.130   thorpej 	if (l->l_flag & L_SA) {
   1570  1.130   thorpej 		l->l_flag &= ~L_SA;
   1571  1.130   thorpej 		p->p_flag &= ~P_SA;
   1572  1.130   thorpej 	}
   1573  1.130   thorpej 
   1574  1.130   thorpej 	/* Make other LWPs stick around long enough to be dumped */
   1575  1.130   thorpej 	p->p_userret = lwp_coredump_hook;
   1576  1.130   thorpej 	p->p_userret_arg = NULL;
   1577   1.96      fair 
   1578  1.112     lukem 	exitsig = signum;
   1579   1.29       cgd 	p->p_acflag |= AXSIG;
   1580   1.29       cgd 	if (sigprop[signum] & SA_CORE) {
   1581  1.109  jdolecek 		p->p_sigctx.ps_sig = signum;
   1582  1.130   thorpej 		if ((error = coredump(l)) == 0)
   1583  1.102  sommerfe 			exitsig |= WCOREFLAG;
   1584  1.102  sommerfe 
   1585  1.102  sommerfe 		if (kern_logsigexit) {
   1586  1.123   thorpej 			/* XXX What if we ever have really large UIDs? */
   1587  1.102  sommerfe 			int uid = p->p_cred && p->p_ucred ?
   1588  1.123   thorpej 				(int) p->p_ucred->cr_uid : -1;
   1589  1.102  sommerfe 
   1590  1.102  sommerfe 			if (error)
   1591  1.102  sommerfe 				log(LOG_INFO, lognocoredump, p->p_pid,
   1592  1.102  sommerfe 				    p->p_comm, uid, signum, error);
   1593  1.102  sommerfe 			else
   1594  1.102  sommerfe 				log(LOG_INFO, logcoredump, p->p_pid,
   1595  1.102  sommerfe 				    p->p_comm, uid, signum);
   1596   1.96      fair 		}
   1597   1.96      fair 
   1598   1.29       cgd 	}
   1599   1.96      fair 
   1600  1.130   thorpej 	exit1(l, W_EXITCODE(0, exitsig));
   1601   1.29       cgd 	/* NOTREACHED */
   1602   1.29       cgd }
   1603   1.29       cgd 
   1604   1.29       cgd /*
   1605   1.75   nathanw  * Dump core, into a file named "progname.core" or "core" (depending on the
   1606   1.75   nathanw  * value of shortcorename), unless the process was setuid/setgid.
   1607   1.29       cgd  */
   1608   1.29       cgd int
   1609  1.130   thorpej coredump(struct lwp *l)
   1610   1.29       cgd {
   1611  1.112     lukem 	struct vnode		*vp;
   1612  1.130   thorpej 	struct proc		*p;
   1613  1.112     lukem 	struct vmspace		*vm;
   1614  1.112     lukem 	struct ucred		*cred;
   1615  1.112     lukem 	struct nameidata	nd;
   1616  1.112     lukem 	struct vattr		vattr;
   1617  1.112     lukem 	int			error, error1;
   1618  1.112     lukem 	char			name[MAXPATHLEN];
   1619  1.112     lukem 
   1620  1.130   thorpej 	p = l->l_proc;
   1621  1.112     lukem 	vm = p->p_vmspace;
   1622  1.112     lukem 	cred = p->p_cred->pc_ucred;
   1623   1.29       cgd 
   1624   1.59       cgd 	/*
   1625   1.59       cgd 	 * Make sure the process has not set-id, to prevent data leaks.
   1626   1.59       cgd 	 */
   1627   1.58       mrg 	if (p->p_flag & P_SUGID)
   1628   1.59       cgd 		return (EPERM);
   1629   1.59       cgd 
   1630   1.59       cgd 	/*
   1631   1.59       cgd 	 * Refuse to core if the data + stack + user size is larger than
   1632   1.59       cgd 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
   1633   1.59       cgd 	 * data.
   1634   1.59       cgd 	 */
   1635   1.30   deraadt 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
   1636   1.29       cgd 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
   1637   1.59       cgd 		return (EFBIG);		/* better error code? */
   1638   1.59       cgd 
   1639   1.59       cgd 	/*
   1640   1.59       cgd 	 * The core dump will go in the current working directory.  Make
   1641   1.80        pk 	 * sure that the directory is still there and that the mount flags
   1642   1.80        pk 	 * allow us to write core dumps there.
   1643   1.59       cgd 	 */
   1644   1.88   thorpej 	vp = p->p_cwdi->cwdi_cdir;
   1645   1.80        pk 	if (vp->v_mount == NULL ||
   1646   1.80        pk 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
   1647   1.59       cgd 		return (EPERM);
   1648   1.59       cgd 
   1649  1.100  sommerfe 	error = build_corename(p, name);
   1650   1.94    bouyer 	if (error)
   1651   1.94    bouyer 		return error;
   1652   1.94    bouyer 
   1653   1.93    bouyer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
   1654  1.129  christos 	error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
   1655   1.52  christos 	if (error)
   1656   1.29       cgd 		return (error);
   1657   1.29       cgd 	vp = nd.ni_vp;
   1658   1.29       cgd 
   1659   1.29       cgd 	/* Don't dump to non-regular files or files with links. */
   1660   1.29       cgd 	if (vp->v_type != VREG ||
   1661   1.29       cgd 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
   1662   1.59       cgd 		error = EINVAL;
   1663   1.29       cgd 		goto out;
   1664   1.29       cgd 	}
   1665   1.29       cgd 	VATTR_NULL(&vattr);
   1666   1.29       cgd 	vattr.va_size = 0;
   1667   1.37   mycroft 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1668   1.29       cgd 	VOP_SETATTR(vp, &vattr, cred, p);
   1669   1.29       cgd 	p->p_acflag |= ACORE;
   1670   1.95       eeh 
   1671  1.118   thorpej 	/* Now dump the actual core file. */
   1672  1.130   thorpej 	error = (*p->p_execsw->es_coredump)(l, vp, cred);
   1673  1.112     lukem  out:
   1674   1.71      fvdl 	VOP_UNLOCK(vp, 0);
   1675   1.29       cgd 	error1 = vn_close(vp, FWRITE, cred, p);
   1676   1.29       cgd 	if (error == 0)
   1677   1.29       cgd 		error = error1;
   1678   1.29       cgd 	return (error);
   1679   1.29       cgd }
   1680   1.29       cgd 
   1681   1.29       cgd /*
   1682   1.29       cgd  * Nonexistent system call-- signal process (may want to handle it).
   1683   1.29       cgd  * Flag error in case process won't see signal immediately (blocked or ignored).
   1684   1.29       cgd  */
   1685   1.29       cgd /* ARGSUSED */
   1686   1.29       cgd int
   1687  1.130   thorpej sys_nosys(struct lwp *l, void *v, register_t *retval)
   1688   1.29       cgd {
   1689  1.130   thorpej 	struct proc 	*p;
   1690   1.29       cgd 
   1691  1.130   thorpej 	p = l->l_proc;
   1692   1.29       cgd 	psignal(p, SIGSYS);
   1693   1.36       cgd 	return (ENOSYS);
   1694   1.94    bouyer }
   1695   1.94    bouyer 
   1696   1.94    bouyer static int
   1697  1.112     lukem build_corename(struct proc *p, char dst[MAXPATHLEN])
   1698   1.94    bouyer {
   1699  1.112     lukem 	const char	*s;
   1700  1.112     lukem 	char		*d, *end;
   1701  1.112     lukem 	int		i;
   1702  1.100  sommerfe 
   1703  1.107     enami 	for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
   1704   1.94    bouyer 	    *s != '\0'; s++) {
   1705   1.94    bouyer 		if (*s == '%') {
   1706  1.107     enami 			switch (*(s + 1)) {
   1707   1.94    bouyer 			case 'n':
   1708  1.107     enami 				i = snprintf(d, end - d, "%s", p->p_comm);
   1709   1.94    bouyer 				break;
   1710   1.94    bouyer 			case 'p':
   1711  1.107     enami 				i = snprintf(d, end - d, "%d", p->p_pid);
   1712   1.94    bouyer 				break;
   1713   1.94    bouyer 			case 'u':
   1714  1.107     enami 				i = snprintf(d, end - d, "%s",
   1715  1.100  sommerfe 				    p->p_pgrp->pg_session->s_login);
   1716   1.94    bouyer 				break;
   1717   1.94    bouyer 			case 't':
   1718  1.107     enami 				i = snprintf(d, end - d, "%ld",
   1719  1.100  sommerfe 				    p->p_stats->p_start.tv_sec);
   1720   1.94    bouyer 				break;
   1721   1.94    bouyer 			default:
   1722   1.94    bouyer 				goto copy;
   1723   1.94    bouyer 			}
   1724   1.94    bouyer 			d += i;
   1725   1.94    bouyer 			s++;
   1726   1.94    bouyer 		} else {
   1727  1.112     lukem  copy:			*d = *s;
   1728   1.94    bouyer 			d++;
   1729   1.94    bouyer 		}
   1730  1.107     enami 		if (d >= end)
   1731  1.107     enami 			return (ENAMETOOLONG);
   1732   1.94    bouyer 	}
   1733   1.94    bouyer 	*d = '\0';
   1734  1.130   thorpej 	return 0;
   1735  1.130   thorpej }
   1736  1.130   thorpej 
   1737  1.130   thorpej void
   1738  1.130   thorpej getucontext(struct lwp *l, ucontext_t *ucp)
   1739  1.130   thorpej {
   1740  1.130   thorpej 	struct proc	*p;
   1741  1.130   thorpej 
   1742  1.130   thorpej 	p = l->l_proc;
   1743  1.130   thorpej 
   1744  1.130   thorpej 	ucp->uc_flags = 0;
   1745  1.130   thorpej 	ucp->uc_link = l->l_ctxlink;
   1746  1.130   thorpej 
   1747  1.130   thorpej 	(void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
   1748  1.130   thorpej 	ucp->uc_flags |= _UC_SIGMASK;
   1749  1.130   thorpej 
   1750  1.130   thorpej 	/*
   1751  1.130   thorpej 	 * The (unsupplied) definition of the `current execution stack'
   1752  1.130   thorpej 	 * in the System V Interface Definition appears to allow returning
   1753  1.130   thorpej 	 * the main context stack.
   1754  1.130   thorpej 	 */
   1755  1.130   thorpej 	if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
   1756  1.130   thorpej 		ucp->uc_stack.ss_sp = (void *)USRSTACK;
   1757  1.130   thorpej 		ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
   1758  1.130   thorpej 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
   1759  1.130   thorpej 	} else {
   1760  1.130   thorpej 		/* Simply copy alternate signal execution stack. */
   1761  1.130   thorpej 		ucp->uc_stack = p->p_sigctx.ps_sigstk;
   1762  1.130   thorpej 	}
   1763  1.130   thorpej 	ucp->uc_flags |= _UC_STACK;
   1764  1.130   thorpej 
   1765  1.130   thorpej 	cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
   1766  1.130   thorpej }
   1767  1.130   thorpej 
   1768  1.130   thorpej /* ARGSUSED */
   1769  1.130   thorpej int
   1770  1.130   thorpej sys_getcontext(struct lwp *l, void *v, register_t *retval)
   1771  1.130   thorpej {
   1772  1.130   thorpej 	struct sys_getcontext_args /* {
   1773  1.130   thorpej 		syscallarg(struct __ucontext *) ucp;
   1774  1.130   thorpej 	} */ *uap = v;
   1775  1.130   thorpej 	ucontext_t uc;
   1776  1.130   thorpej 
   1777  1.130   thorpej 	getucontext(l, &uc);
   1778  1.130   thorpej 
   1779  1.130   thorpej 	return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
   1780  1.130   thorpej }
   1781  1.130   thorpej 
   1782  1.130   thorpej int
   1783  1.130   thorpej setucontext(struct lwp *l, const ucontext_t *ucp)
   1784  1.130   thorpej {
   1785  1.130   thorpej 	struct proc	*p;
   1786  1.130   thorpej 	int		error;
   1787  1.130   thorpej 
   1788  1.130   thorpej 	p = l->l_proc;
   1789  1.130   thorpej 	if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
   1790  1.130   thorpej 		return (error);
   1791  1.130   thorpej 	l->l_ctxlink = ucp->uc_link;
   1792  1.130   thorpej 	/*
   1793  1.130   thorpej 	 * We might want to take care of the stack portion here but currently
   1794  1.130   thorpej 	 * don't; see the comment in getucontext().
   1795  1.130   thorpej 	 */
   1796  1.130   thorpej 	if ((ucp->uc_flags & _UC_SIGMASK) != 0)
   1797  1.130   thorpej 		sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
   1798  1.130   thorpej 
   1799  1.130   thorpej 	return 0;
   1800  1.130   thorpej }
   1801  1.130   thorpej 
   1802  1.130   thorpej /* ARGSUSED */
   1803  1.130   thorpej int
   1804  1.130   thorpej sys_setcontext(struct lwp *l, void *v, register_t *retval)
   1805  1.130   thorpej {
   1806  1.130   thorpej 	struct sys_setcontext_args /* {
   1807  1.130   thorpej 		syscallarg(const ucontext_t *) ucp;
   1808  1.130   thorpej 	} */ *uap = v;
   1809  1.130   thorpej 	ucontext_t uc;
   1810  1.130   thorpej 	int error;
   1811  1.130   thorpej 
   1812  1.130   thorpej 	if (SCARG(uap, ucp) == NULL)	/* i.e. end of uc_link chain */
   1813  1.130   thorpej 		exit1(l, W_EXITCODE(0, 0));
   1814  1.130   thorpej 	else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
   1815  1.130   thorpej 	    (error = setucontext(l, &uc)) != 0)
   1816  1.130   thorpej 		return (error);
   1817  1.130   thorpej 
   1818  1.130   thorpej 	return (EJUSTRETURN);
   1819  1.108  jdolecek }
   1820  1.130   thorpej 
   1821  1.108  jdolecek 
   1822  1.108  jdolecek /*
   1823  1.108  jdolecek  * Returns true if signal is ignored or masked for passed process.
   1824  1.108  jdolecek  */
   1825  1.108  jdolecek int
   1826  1.112     lukem sigismasked(struct proc *p, int sig)
   1827  1.108  jdolecek {
   1828  1.112     lukem 
   1829  1.117     enami 	return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
   1830  1.117     enami 	    sigismember(&p->p_sigctx.ps_sigmask, sig));
   1831   1.29       cgd }
   1832  1.126  jdolecek 
   1833  1.126  jdolecek static int
   1834  1.126  jdolecek filt_sigattach(struct knote *kn)
   1835  1.126  jdolecek {
   1836  1.126  jdolecek 	struct proc *p = curproc;
   1837  1.126  jdolecek 
   1838  1.126  jdolecek 	kn->kn_ptr.p_proc = p;
   1839  1.126  jdolecek 	kn->kn_flags |= EV_CLEAR;               /* automatically set */
   1840  1.126  jdolecek 
   1841  1.126  jdolecek 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
   1842  1.126  jdolecek 
   1843  1.126  jdolecek 	return (0);
   1844  1.126  jdolecek }
   1845  1.126  jdolecek 
   1846  1.126  jdolecek static void
   1847  1.126  jdolecek filt_sigdetach(struct knote *kn)
   1848  1.126  jdolecek {
   1849  1.126  jdolecek 	struct proc *p = kn->kn_ptr.p_proc;
   1850  1.126  jdolecek 
   1851  1.126  jdolecek 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
   1852  1.126  jdolecek }
   1853  1.126  jdolecek 
   1854  1.126  jdolecek /*
   1855  1.126  jdolecek  * signal knotes are shared with proc knotes, so we apply a mask to
   1856  1.126  jdolecek  * the hint in order to differentiate them from process hints.  This
   1857  1.126  jdolecek  * could be avoided by using a signal-specific knote list, but probably
   1858  1.126  jdolecek  * isn't worth the trouble.
   1859  1.126  jdolecek  */
   1860  1.126  jdolecek static int
   1861  1.126  jdolecek filt_signal(struct knote *kn, long hint)
   1862  1.126  jdolecek {
   1863  1.126  jdolecek 
   1864  1.126  jdolecek 	if (hint & NOTE_SIGNAL) {
   1865  1.126  jdolecek 		hint &= ~NOTE_SIGNAL;
   1866  1.126  jdolecek 
   1867  1.126  jdolecek 		if (kn->kn_id == hint)
   1868  1.126  jdolecek 			kn->kn_data++;
   1869  1.126  jdolecek 	}
   1870  1.126  jdolecek 	return (kn->kn_data != 0);
   1871  1.126  jdolecek }
   1872  1.126  jdolecek 
   1873  1.126  jdolecek const struct filterops sig_filtops = {
   1874  1.126  jdolecek 	0, filt_sigattach, filt_sigdetach, filt_signal
   1875  1.126  jdolecek };
   1876