Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.121
      1  1.121   thorpej /*	$NetBSD: kern_sig.c,v 1.121 2002/07/04 23:32:14 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.121   thorpej __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.121 2002/07/04 23:32:14 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.118   thorpej #include <sys/exec.h>
     73   1.29       cgd 
     74   1.32       cgd #include <sys/mount.h>
     75   1.32       cgd #include <sys/syscallargs.h>
     76   1.32       cgd 
     77   1.29       cgd #include <machine/cpu.h>
     78   1.29       cgd 
     79   1.29       cgd #include <sys/user.h>		/* for coredump */
     80   1.52  christos 
     81   1.69       mrg #include <uvm/uvm_extern.h>
     82   1.69       mrg 
     83  1.112     lukem static void	proc_stop(struct proc *p);
     84  1.112     lukem void		killproc(struct proc *, char *);
     85  1.112     lukem static int	build_corename(struct proc *, char [MAXPATHLEN]);
     86  1.112     lukem sigset_t	contsigmask, stopsigmask, sigcantmask;
     87   1.29       cgd 
     88  1.112     lukem struct pool	sigacts_pool;	/* memory pool for sigacts structures */
     89   1.89   thorpej 
     90   1.29       cgd /*
     91   1.29       cgd  * Can process p, with pcred pc, send the signal signum to process q?
     92   1.29       cgd  */
     93  1.112     lukem #define	CANSIGNAL(p, pc, q, signum) \
     94   1.29       cgd 	((pc)->pc_ucred->cr_uid == 0 || \
     95   1.29       cgd 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
     96   1.29       cgd 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
     97   1.29       cgd 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
     98   1.29       cgd 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
     99   1.29       cgd 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
    100   1.29       cgd 
    101   1.89   thorpej /*
    102   1.89   thorpej  * Initialize signal-related data structures.
    103   1.89   thorpej  */
    104   1.89   thorpej void
    105  1.112     lukem signal_init(void)
    106   1.89   thorpej {
    107  1.112     lukem 
    108   1.89   thorpej 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
    109  1.120   thorpej 	    &pool_allocator_nointr);
    110   1.89   thorpej }
    111   1.89   thorpej 
    112   1.89   thorpej /*
    113  1.109  jdolecek  * Create an initial sigctx structure, using the same signal state
    114  1.109  jdolecek  * as p. If 'share' is set, share the sigctx_proc part, otherwise just
    115  1.109  jdolecek  * copy it from parent.
    116   1.89   thorpej  */
    117  1.109  jdolecek void
    118  1.112     lukem sigactsinit(struct proc *np, struct proc *pp, int share)
    119   1.89   thorpej {
    120   1.89   thorpej 	struct sigacts *ps;
    121   1.89   thorpej 
    122  1.109  jdolecek 	if (share) {
    123  1.109  jdolecek 		np->p_sigacts = pp->p_sigacts;
    124  1.109  jdolecek 		pp->p_sigacts->sa_refcnt++;
    125  1.109  jdolecek 	} else {
    126  1.109  jdolecek 		ps = pool_get(&sigacts_pool, PR_WAITOK);
    127  1.109  jdolecek 		if (pp)
    128  1.109  jdolecek 			memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
    129  1.109  jdolecek 		else
    130  1.109  jdolecek 			memset(ps, '\0', sizeof(struct sigacts));
    131  1.109  jdolecek 		ps->sa_refcnt = 1;
    132  1.109  jdolecek 		np->p_sigacts = ps;
    133  1.109  jdolecek 	}
    134   1.89   thorpej }
    135   1.89   thorpej 
    136   1.89   thorpej /*
    137  1.109  jdolecek  * Make this process not share its sigctx, maintaining all
    138   1.89   thorpej  * signal state.
    139   1.89   thorpej  */
    140   1.89   thorpej void
    141  1.112     lukem sigactsunshare(struct proc *p)
    142   1.89   thorpej {
    143  1.109  jdolecek 	struct sigacts *oldps;
    144   1.89   thorpej 
    145  1.109  jdolecek 	if (p->p_sigacts->sa_refcnt == 1)
    146   1.89   thorpej 		return;
    147   1.89   thorpej 
    148  1.109  jdolecek 	oldps = p->p_sigacts;
    149  1.109  jdolecek 	sigactsinit(p, NULL, 0);
    150  1.109  jdolecek 
    151  1.109  jdolecek 	if (--oldps->sa_refcnt == 0)
    152  1.109  jdolecek 		pool_put(&sigacts_pool, oldps);
    153   1.89   thorpej }
    154   1.89   thorpej 
    155   1.89   thorpej /*
    156  1.109  jdolecek  * Release a sigctx structure.
    157   1.89   thorpej  */
    158   1.89   thorpej void
    159  1.112     lukem sigactsfree(struct proc *p)
    160   1.89   thorpej {
    161  1.112     lukem 	struct sigacts *ps;
    162   1.89   thorpej 
    163  1.112     lukem 	ps = p->p_sigacts;
    164  1.109  jdolecek 	if (--ps->sa_refcnt > 0)
    165   1.89   thorpej 		return;
    166   1.89   thorpej 
    167   1.89   thorpej 	pool_put(&sigacts_pool, ps);
    168   1.89   thorpej }
    169   1.89   thorpej 
    170   1.79   mycroft int
    171  1.112     lukem sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
    172  1.121   thorpej 	struct sigaction *osa, void *tramp, int vers)
    173   1.79   mycroft {
    174  1.112     lukem 	struct sigacts	*ps;
    175  1.112     lukem 	int		prop;
    176   1.79   mycroft 
    177  1.112     lukem 	ps = p->p_sigacts;
    178   1.79   mycroft 	if (signum <= 0 || signum >= NSIG)
    179   1.79   mycroft 		return (EINVAL);
    180   1.79   mycroft 
    181  1.121   thorpej 	/*
    182  1.121   thorpej 	 * Trampoline ABI version 0 is reserved for the legacy
    183  1.121   thorpej 	 * kernel-provided on-stack trampoline.  Conversely, if
    184  1.121   thorpej 	 * we are using a non-0 ABI version, we must have a
    185  1.121   thorpej 	 * trampoline.
    186  1.121   thorpej 	 */
    187  1.121   thorpej 	if ((vers != 0 && tramp == NULL) ||
    188  1.121   thorpej 	    (vers == 0 && tramp != NULL))
    189  1.121   thorpej 		return (EINVAL);
    190  1.121   thorpej 
    191   1.79   mycroft 	if (osa)
    192  1.109  jdolecek 		*osa = SIGACTION_PS(ps, signum);
    193   1.79   mycroft 
    194   1.79   mycroft 	if (nsa) {
    195   1.79   mycroft 		if (nsa->sa_flags & ~SA_ALLBITS)
    196   1.79   mycroft 			return (EINVAL);
    197   1.79   mycroft 
    198   1.79   mycroft 		prop = sigprop[signum];
    199   1.79   mycroft 		if (prop & SA_CANTMASK)
    200   1.79   mycroft 			return (EINVAL);
    201   1.79   mycroft 
    202  1.105   thorpej 		(void) splsched();	/* XXXSMP */
    203  1.109  jdolecek 		SIGACTION_PS(ps, signum) = *nsa;
    204  1.121   thorpej 		ps->sa_sigdesc[signum].sd_tramp = tramp;
    205  1.121   thorpej 		ps->sa_sigdesc[signum].sd_vers = vers;
    206  1.109  jdolecek 		sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
    207   1.79   mycroft 		if ((prop & SA_NORESET) != 0)
    208  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
    209   1.79   mycroft 		if (signum == SIGCHLD) {
    210   1.79   mycroft 			if (nsa->sa_flags & SA_NOCLDSTOP)
    211   1.79   mycroft 				p->p_flag |= P_NOCLDSTOP;
    212   1.79   mycroft 			else
    213   1.79   mycroft 				p->p_flag &= ~P_NOCLDSTOP;
    214   1.82     enami 			if (nsa->sa_flags & SA_NOCLDWAIT) {
    215   1.81  christos 				/*
    216   1.81  christos 				 * Paranoia: since SA_NOCLDWAIT is implemented
    217   1.81  christos 				 * by reparenting the dying child to PID 1 (and
    218  1.112     lukem 				 * trust it to reap the zombie), PID 1 itself
    219  1.112     lukem 				 * is forbidden to set SA_NOCLDWAIT.
    220   1.81  christos 				 */
    221   1.81  christos 				if (p->p_pid == 1)
    222   1.81  christos 					p->p_flag &= ~P_NOCLDWAIT;
    223   1.81  christos 				else
    224   1.81  christos 					p->p_flag |= P_NOCLDWAIT;
    225   1.81  christos 			} else
    226   1.81  christos 				p->p_flag &= ~P_NOCLDWAIT;
    227   1.79   mycroft 		}
    228   1.79   mycroft 		if ((nsa->sa_flags & SA_NODEFER) == 0)
    229  1.109  jdolecek 			sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    230   1.79   mycroft 		else
    231  1.109  jdolecek 			sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    232   1.79   mycroft 		/*
    233  1.112     lukem 	 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
    234  1.112     lukem 		 * SIG_IGN, and for signals set to SIG_DFL where the default is
    235  1.112     lukem 		 * to ignore. However, don't put SIGCONT in
    236  1.112     lukem 		 * p_sigctx.ps_sigignore, as we have to restart the process.
    237  1.112     lukem 	 	 */
    238   1.79   mycroft 		if (nsa->sa_handler == SIG_IGN ||
    239   1.79   mycroft 		    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
    240  1.112     lukem 						/* never to be seen again */
    241  1.112     lukem 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    242  1.112     lukem 			if (signum != SIGCONT) {
    243  1.112     lukem 						/* easier in psignal */
    244  1.112     lukem 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    245  1.112     lukem 			}
    246  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    247   1.79   mycroft 		} else {
    248  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigignore, signum);
    249   1.79   mycroft 			if (nsa->sa_handler == SIG_DFL)
    250  1.109  jdolecek 				sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    251   1.79   mycroft 			else
    252  1.109  jdolecek 				sigaddset(&p->p_sigctx.ps_sigcatch, signum);
    253   1.79   mycroft 		}
    254   1.79   mycroft 		(void) spl0();
    255   1.79   mycroft 	}
    256   1.79   mycroft 
    257   1.79   mycroft 	return (0);
    258   1.79   mycroft }
    259   1.79   mycroft 
    260   1.29       cgd /* ARGSUSED */
    261   1.52  christos int
    262  1.112     lukem sys___sigaction14(struct proc *p, void *v, register_t *retval)
    263   1.48   thorpej {
    264   1.98  augustss 	struct sys___sigaction14_args /* {
    265  1.112     lukem 		syscallarg(int)				signum;
    266  1.112     lukem 		syscallarg(const struct sigaction *)	nsa;
    267  1.112     lukem 		syscallarg(struct sigaction *)		osa;
    268   1.48   thorpej 	} */ *uap = v;
    269  1.112     lukem 	struct sigaction	nsa, osa;
    270  1.112     lukem 	int			error;
    271   1.29       cgd 
    272   1.79   mycroft 	if (SCARG(uap, nsa)) {
    273   1.79   mycroft 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    274   1.52  christos 		if (error)
    275   1.29       cgd 			return (error);
    276   1.29       cgd 	}
    277   1.79   mycroft 	error = sigaction1(p, SCARG(uap, signum),
    278  1.121   thorpej 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    279  1.121   thorpej 	    NULL, 0);
    280  1.121   thorpej 	if (error)
    281  1.121   thorpej 		return (error);
    282  1.121   thorpej 	if (SCARG(uap, osa)) {
    283  1.121   thorpej 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    284  1.121   thorpej 		if (error)
    285  1.121   thorpej 			return (error);
    286  1.121   thorpej 	}
    287  1.121   thorpej 	return (0);
    288  1.121   thorpej }
    289  1.121   thorpej 
    290  1.121   thorpej /* ARGSUSED */
    291  1.121   thorpej int
    292  1.121   thorpej sys___sigaction_sigtramp(struct proc *p, void *v, register_t *retval)
    293  1.121   thorpej {
    294  1.121   thorpej 	struct sys___sigaction_sigtramp_args /* {
    295  1.121   thorpej 		syscallarg(int)				signum;
    296  1.121   thorpej 		syscallarg(const struct sigaction *)	nsa;
    297  1.121   thorpej 		syscallarg(struct sigaction *)		osa;
    298  1.121   thorpej 		syscallarg(void *)			tramp;
    299  1.121   thorpej 		syscallarg(int)				vers;
    300  1.121   thorpej 	} */ *uap = v;
    301  1.121   thorpej 	struct sigaction nsa, osa;
    302  1.121   thorpej 	int error;
    303  1.121   thorpej 
    304  1.121   thorpej 	if (SCARG(uap, nsa)) {
    305  1.121   thorpej 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    306  1.121   thorpej 		if (error)
    307  1.121   thorpej 			return (error);
    308  1.121   thorpej 	}
    309  1.121   thorpej 	error = sigaction1(p, SCARG(uap, signum),
    310  1.121   thorpej 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    311  1.121   thorpej 	    SCARG(uap, tramp), SCARG(uap, vers));
    312   1.79   mycroft 	if (error)
    313   1.79   mycroft 		return (error);
    314   1.79   mycroft 	if (SCARG(uap, osa)) {
    315   1.79   mycroft 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    316   1.52  christos 		if (error)
    317   1.29       cgd 			return (error);
    318   1.29       cgd 	}
    319   1.29       cgd 	return (0);
    320   1.29       cgd }
    321   1.29       cgd 
    322   1.29       cgd /*
    323   1.29       cgd  * Initialize signal state for process 0;
    324   1.79   mycroft  * set to ignore signals that are ignored by default and disable the signal
    325   1.79   mycroft  * stack.
    326   1.29       cgd  */
    327   1.29       cgd void
    328  1.112     lukem siginit(struct proc *p)
    329   1.29       cgd {
    330  1.112     lukem 	struct sigacts	*ps;
    331  1.112     lukem 	int		signum, prop;
    332   1.79   mycroft 
    333  1.112     lukem 	ps = p->p_sigacts;
    334   1.79   mycroft 	sigemptyset(&contsigmask);
    335   1.79   mycroft 	sigemptyset(&stopsigmask);
    336   1.79   mycroft 	sigemptyset(&sigcantmask);
    337   1.85   mycroft 	for (signum = 1; signum < NSIG; signum++) {
    338   1.79   mycroft 		prop = sigprop[signum];
    339   1.79   mycroft 		if (prop & SA_CONT)
    340   1.79   mycroft 			sigaddset(&contsigmask, signum);
    341   1.79   mycroft 		if (prop & SA_STOP)
    342   1.79   mycroft 			sigaddset(&stopsigmask, signum);
    343   1.79   mycroft 		if (prop & SA_CANTMASK)
    344   1.79   mycroft 			sigaddset(&sigcantmask, signum);
    345   1.79   mycroft 		if (prop & SA_IGNORE && signum != SIGCONT)
    346  1.109  jdolecek 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
    347  1.109  jdolecek 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    348  1.109  jdolecek 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    349   1.79   mycroft 	}
    350  1.109  jdolecek 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    351   1.79   mycroft 	p->p_flag &= ~P_NOCLDSTOP;
    352   1.29       cgd 
    353   1.79   mycroft 	/*
    354   1.79   mycroft 	 * Reset stack state to the user stack.
    355   1.79   mycroft 	 */
    356  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    357  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_size = 0;
    358  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    359   1.89   thorpej 
    360   1.89   thorpej 	/* One reference. */
    361  1.109  jdolecek 	ps->sa_refcnt = 1;
    362   1.29       cgd }
    363   1.29       cgd 
    364   1.29       cgd /*
    365   1.29       cgd  * Reset signals for an exec of the specified process.
    366   1.29       cgd  */
    367   1.29       cgd void
    368  1.112     lukem execsigs(struct proc *p)
    369   1.29       cgd {
    370  1.112     lukem 	struct sigacts	*ps;
    371  1.112     lukem 	int		signum, prop;
    372   1.29       cgd 
    373  1.115   thorpej 	sigactsunshare(p);
    374  1.115   thorpej 
    375  1.112     lukem 	ps = p->p_sigacts;
    376  1.115   thorpej 
    377   1.29       cgd 	/*
    378   1.29       cgd 	 * Reset caught signals.  Held signals remain held
    379  1.109  jdolecek 	 * through p_sigctx.ps_sigmask (unless they were caught,
    380   1.29       cgd 	 * and are now ignored by default).
    381   1.29       cgd 	 */
    382   1.85   mycroft 	for (signum = 1; signum < NSIG; signum++) {
    383  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
    384   1.79   mycroft 			prop = sigprop[signum];
    385   1.79   mycroft 			if (prop & SA_IGNORE) {
    386   1.79   mycroft 				if ((prop & SA_CONT) == 0)
    387  1.112     lukem 					sigaddset(&p->p_sigctx.ps_sigignore,
    388  1.112     lukem 					    signum);
    389  1.109  jdolecek 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    390   1.79   mycroft 			}
    391  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    392   1.29       cgd 		}
    393  1.109  jdolecek 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    394  1.109  jdolecek 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    395   1.29       cgd 	}
    396  1.109  jdolecek 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    397   1.79   mycroft 	p->p_flag &= ~P_NOCLDSTOP;
    398   1.79   mycroft 
    399   1.29       cgd 	/*
    400   1.29       cgd 	 * Reset stack state to the user stack.
    401   1.29       cgd 	 */
    402  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    403  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_size = 0;
    404  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    405   1.29       cgd }
    406   1.29       cgd 
    407   1.79   mycroft int
    408  1.112     lukem sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
    409   1.79   mycroft {
    410   1.79   mycroft 
    411   1.79   mycroft 	if (oss)
    412  1.109  jdolecek 		*oss = p->p_sigctx.ps_sigmask;
    413   1.79   mycroft 
    414   1.79   mycroft 	if (nss) {
    415  1.105   thorpej 		(void)splsched();	/* XXXSMP */
    416   1.79   mycroft 		switch (how) {
    417   1.79   mycroft 		case SIG_BLOCK:
    418  1.109  jdolecek 			sigplusset(nss, &p->p_sigctx.ps_sigmask);
    419   1.79   mycroft 			break;
    420   1.79   mycroft 		case SIG_UNBLOCK:
    421  1.109  jdolecek 			sigminusset(nss, &p->p_sigctx.ps_sigmask);
    422  1.110   thorpej 			CHECKSIGS(p);
    423   1.79   mycroft 			break;
    424   1.79   mycroft 		case SIG_SETMASK:
    425  1.109  jdolecek 			p->p_sigctx.ps_sigmask = *nss;
    426  1.110   thorpej 			CHECKSIGS(p);
    427   1.79   mycroft 			break;
    428   1.79   mycroft 		default:
    429  1.104   thorpej 			(void)spl0();	/* XXXSMP */
    430   1.79   mycroft 			return (EINVAL);
    431   1.79   mycroft 		}
    432  1.109  jdolecek 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    433  1.104   thorpej 		(void)spl0();		/* XXXSMP */
    434   1.79   mycroft 	}
    435   1.79   mycroft 
    436   1.79   mycroft 	return (0);
    437   1.79   mycroft }
    438   1.79   mycroft 
    439   1.29       cgd /*
    440   1.29       cgd  * Manipulate signal mask.
    441   1.29       cgd  * Note that we receive new mask, not pointer,
    442   1.29       cgd  * and return old mask as return value;
    443   1.29       cgd  * the library stub does the rest.
    444   1.29       cgd  */
    445   1.52  christos int
    446  1.112     lukem sys___sigprocmask14(struct proc *p, void *v, register_t *retval)
    447   1.48   thorpej {
    448   1.79   mycroft 	struct sys___sigprocmask14_args /* {
    449  1.112     lukem 		syscallarg(int)			how;
    450  1.112     lukem 		syscallarg(const sigset_t *)	set;
    451  1.112     lukem 		syscallarg(sigset_t *)		oset;
    452   1.48   thorpej 	} */ *uap = v;
    453  1.112     lukem 	sigset_t	nss, oss;
    454  1.112     lukem 	int		error;
    455   1.29       cgd 
    456   1.79   mycroft 	if (SCARG(uap, set)) {
    457   1.79   mycroft 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
    458   1.79   mycroft 		if (error)
    459   1.79   mycroft 			return (error);
    460   1.79   mycroft 	}
    461   1.79   mycroft 	error = sigprocmask1(p, SCARG(uap, how),
    462   1.79   mycroft 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
    463   1.79   mycroft 	if (error)
    464   1.79   mycroft 		return (error);
    465   1.79   mycroft 	if (SCARG(uap, oset)) {
    466   1.79   mycroft 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
    467   1.79   mycroft 		if (error)
    468   1.79   mycroft 			return (error);
    469   1.79   mycroft 	}
    470   1.79   mycroft 	return (0);
    471   1.79   mycroft }
    472   1.79   mycroft 
    473   1.79   mycroft void
    474  1.112     lukem sigpending1(struct proc *p, sigset_t *ss)
    475   1.79   mycroft {
    476   1.29       cgd 
    477  1.109  jdolecek 	*ss = p->p_sigctx.ps_siglist;
    478  1.109  jdolecek 	sigminusset(&p->p_sigctx.ps_sigmask, ss);
    479   1.29       cgd }
    480   1.29       cgd 
    481   1.29       cgd /* ARGSUSED */
    482   1.52  christos int
    483  1.112     lukem sys___sigpending14(struct proc *p, void *v, register_t *retval)
    484   1.29       cgd {
    485   1.98  augustss 	struct sys___sigpending14_args /* {
    486  1.112     lukem 		syscallarg(sigset_t *)	set;
    487   1.79   mycroft 	} */ *uap = v;
    488   1.79   mycroft 	sigset_t ss;
    489   1.79   mycroft 
    490   1.79   mycroft 	sigpending1(p, &ss);
    491   1.79   mycroft 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
    492   1.79   mycroft }
    493   1.79   mycroft 
    494   1.79   mycroft int
    495  1.112     lukem sigsuspend1(struct proc *p, const sigset_t *ss)
    496   1.79   mycroft {
    497  1.112     lukem 	struct sigacts *ps;
    498   1.29       cgd 
    499  1.112     lukem 	ps = p->p_sigacts;
    500   1.79   mycroft 	if (ss) {
    501   1.79   mycroft 		/*
    502   1.79   mycroft 		 * When returning from sigpause, we want
    503   1.79   mycroft 		 * the old mask to be restored after the
    504   1.79   mycroft 		 * signal handler has finished.  Thus, we
    505  1.109  jdolecek 		 * save it here and mark the sigctx structure
    506   1.79   mycroft 		 * to indicate this.
    507   1.79   mycroft 		 */
    508  1.109  jdolecek 		p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
    509  1.109  jdolecek 		p->p_sigctx.ps_flags |= SAS_OLDMASK;
    510  1.105   thorpej 		(void) splsched();	/* XXXSMP */
    511  1.109  jdolecek 		p->p_sigctx.ps_sigmask = *ss;
    512  1.110   thorpej 		CHECKSIGS(p);
    513  1.109  jdolecek 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    514  1.104   thorpej 		(void) spl0();		/* XXXSMP */
    515   1.79   mycroft 	}
    516   1.79   mycroft 
    517   1.79   mycroft 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
    518   1.79   mycroft 		/* void */;
    519   1.79   mycroft 	/* always return EINTR rather than ERESTART... */
    520   1.79   mycroft 	return (EINTR);
    521   1.29       cgd }
    522   1.29       cgd 
    523   1.29       cgd /*
    524   1.29       cgd  * Suspend process until signal, providing mask to be set
    525   1.29       cgd  * in the meantime.  Note nonstandard calling convention:
    526   1.29       cgd  * libc stub passes mask, not pointer, to save a copyin.
    527   1.29       cgd  */
    528   1.29       cgd /* ARGSUSED */
    529   1.29       cgd int
    530  1.112     lukem sys___sigsuspend14(struct proc *p, void *v, register_t *retval)
    531   1.48   thorpej {
    532   1.79   mycroft 	struct sys___sigsuspend14_args /* {
    533  1.112     lukem 		syscallarg(const sigset_t *)	set;
    534   1.48   thorpej 	} */ *uap = v;
    535  1.112     lukem 	sigset_t	ss;
    536  1.112     lukem 	int		error;
    537   1.79   mycroft 
    538   1.79   mycroft 	if (SCARG(uap, set)) {
    539   1.79   mycroft 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
    540   1.79   mycroft 		if (error)
    541   1.79   mycroft 			return (error);
    542   1.79   mycroft 	}
    543   1.79   mycroft 
    544   1.79   mycroft 	return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
    545   1.79   mycroft }
    546   1.79   mycroft 
    547   1.79   mycroft int
    548  1.112     lukem sigaltstack1(struct proc *p, const struct sigaltstack *nss,
    549  1.112     lukem 	struct sigaltstack *oss)
    550   1.79   mycroft {
    551  1.112     lukem 
    552   1.79   mycroft 	if (oss)
    553  1.109  jdolecek 		*oss = p->p_sigctx.ps_sigstk;
    554   1.79   mycroft 
    555   1.79   mycroft 	if (nss) {
    556   1.79   mycroft 		if (nss->ss_flags & ~SS_ALLBITS)
    557   1.79   mycroft 			return (EINVAL);
    558   1.79   mycroft 
    559   1.79   mycroft 		if (nss->ss_flags & SS_DISABLE) {
    560  1.109  jdolecek 			if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
    561   1.79   mycroft 				return (EINVAL);
    562   1.79   mycroft 		} else {
    563   1.79   mycroft 			if (nss->ss_size < MINSIGSTKSZ)
    564   1.79   mycroft 				return (ENOMEM);
    565   1.79   mycroft 		}
    566  1.109  jdolecek 		p->p_sigctx.ps_sigstk = *nss;
    567   1.79   mycroft 	}
    568   1.79   mycroft 
    569   1.79   mycroft 	return (0);
    570   1.29       cgd }
    571   1.29       cgd 
    572   1.29       cgd /* ARGSUSED */
    573   1.52  christos int
    574  1.112     lukem sys___sigaltstack14(struct proc *p, void *v, register_t *retval)
    575   1.48   thorpej {
    576   1.98  augustss 	struct sys___sigaltstack14_args /* {
    577  1.112     lukem 		syscallarg(const struct sigaltstack *)	nss;
    578  1.112     lukem 		syscallarg(struct sigaltstack *)	oss;
    579   1.48   thorpej 	} */ *uap = v;
    580  1.112     lukem 	struct sigaltstack	nss, oss;
    581  1.112     lukem 	int			error;
    582   1.29       cgd 
    583   1.79   mycroft 	if (SCARG(uap, nss)) {
    584   1.79   mycroft 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    585   1.79   mycroft 		if (error)
    586   1.79   mycroft 			return (error);
    587   1.79   mycroft 	}
    588   1.79   mycroft 	error = sigaltstack1(p,
    589   1.79   mycroft 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    590   1.52  christos 	if (error)
    591   1.29       cgd 		return (error);
    592   1.79   mycroft 	if (SCARG(uap, oss)) {
    593   1.79   mycroft 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    594   1.79   mycroft 		if (error)
    595   1.79   mycroft 			return (error);
    596   1.29       cgd 	}
    597   1.29       cgd 	return (0);
    598   1.29       cgd }
    599   1.29       cgd 
    600   1.29       cgd /* ARGSUSED */
    601   1.29       cgd int
    602  1.112     lukem sys_kill(struct proc *cp, void *v, register_t *retval)
    603   1.48   thorpej {
    604   1.98  augustss 	struct sys_kill_args /* {
    605  1.112     lukem 		syscallarg(int)	pid;
    606  1.112     lukem 		syscallarg(int)	signum;
    607   1.48   thorpej 	} */ *uap = v;
    608  1.112     lukem 	struct proc	*p;
    609  1.112     lukem 	struct pcred	*pc;
    610   1.29       cgd 
    611  1.112     lukem 	pc = cp->p_cred;
    612   1.32       cgd 	if ((u_int)SCARG(uap, signum) >= NSIG)
    613   1.29       cgd 		return (EINVAL);
    614   1.32       cgd 	if (SCARG(uap, pid) > 0) {
    615   1.29       cgd 		/* kill single process */
    616   1.32       cgd 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    617   1.29       cgd 			return (ESRCH);
    618   1.32       cgd 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
    619   1.29       cgd 			return (EPERM);
    620   1.32       cgd 		if (SCARG(uap, signum))
    621   1.32       cgd 			psignal(p, SCARG(uap, signum));
    622   1.29       cgd 		return (0);
    623   1.29       cgd 	}
    624   1.32       cgd 	switch (SCARG(uap, pid)) {
    625   1.29       cgd 	case -1:		/* broadcast signal */
    626   1.32       cgd 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
    627   1.29       cgd 	case 0:			/* signal own process group */
    628   1.32       cgd 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
    629   1.29       cgd 	default:		/* negative explicit process group */
    630   1.32       cgd 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
    631   1.29       cgd 	}
    632   1.29       cgd 	/* NOTREACHED */
    633   1.29       cgd }
    634   1.29       cgd 
    635   1.29       cgd /*
    636   1.29       cgd  * Common code for kill process group/broadcast kill.
    637   1.29       cgd  * cp is calling process.
    638   1.29       cgd  */
    639   1.52  christos int
    640  1.112     lukem killpg1(struct proc *cp, int signum, int pgid, int all)
    641   1.29       cgd {
    642  1.112     lukem 	struct proc	*p;
    643  1.112     lukem 	struct pcred	*pc;
    644  1.112     lukem 	struct pgrp	*pgrp;
    645  1.112     lukem 	int		nfound;
    646   1.29       cgd 
    647  1.112     lukem 	pc = cp->p_cred;
    648  1.112     lukem 	nfound = 0;
    649   1.91   thorpej 	if (all) {
    650   1.29       cgd 		/*
    651   1.29       cgd 		 * broadcast
    652   1.29       cgd 		 */
    653   1.92   thorpej 		proclist_lock_read();
    654   1.31   mycroft 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    655   1.29       cgd 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    656   1.29       cgd 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
    657   1.29       cgd 				continue;
    658   1.29       cgd 			nfound++;
    659   1.29       cgd 			if (signum)
    660   1.29       cgd 				psignal(p, signum);
    661   1.29       cgd 		}
    662   1.91   thorpej 		proclist_unlock_read();
    663   1.91   thorpej 	} else {
    664   1.29       cgd 		if (pgid == 0)
    665   1.29       cgd 			/*
    666   1.29       cgd 			 * zero pgid means send to my process group.
    667   1.29       cgd 			 */
    668   1.29       cgd 			pgrp = cp->p_pgrp;
    669   1.29       cgd 		else {
    670   1.29       cgd 			pgrp = pgfind(pgid);
    671   1.29       cgd 			if (pgrp == NULL)
    672   1.29       cgd 				return (ESRCH);
    673   1.29       cgd 		}
    674  1.112     lukem 		for (p = pgrp->pg_members.lh_first;
    675  1.112     lukem 		    p != 0;
    676  1.112     lukem 		    p = p->p_pglist.le_next) {
    677   1.29       cgd 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    678   1.29       cgd 			    !CANSIGNAL(cp, pc, p, signum))
    679   1.29       cgd 				continue;
    680   1.29       cgd 			nfound++;
    681   1.90   thorpej 			if (signum && P_ZOMBIE(p) == 0)
    682   1.29       cgd 				psignal(p, signum);
    683   1.29       cgd 		}
    684   1.29       cgd 	}
    685   1.29       cgd 	return (nfound ? 0 : ESRCH);
    686   1.29       cgd }
    687   1.29       cgd 
    688   1.29       cgd /*
    689   1.29       cgd  * Send a signal to a process group.
    690   1.29       cgd  */
    691   1.29       cgd void
    692  1.112     lukem gsignal(int pgid, int signum)
    693   1.29       cgd {
    694   1.29       cgd 	struct pgrp *pgrp;
    695   1.29       cgd 
    696   1.29       cgd 	if (pgid && (pgrp = pgfind(pgid)))
    697   1.29       cgd 		pgsignal(pgrp, signum, 0);
    698   1.29       cgd }
    699   1.29       cgd 
    700   1.29       cgd /*
    701   1.71      fvdl  * Send a signal to a process group. If checktty is 1,
    702   1.29       cgd  * limit to members which have a controlling terminal.
    703   1.29       cgd  */
    704   1.29       cgd void
    705  1.112     lukem pgsignal(struct pgrp *pgrp, int signum, int checkctty)
    706   1.29       cgd {
    707   1.98  augustss 	struct proc *p;
    708   1.29       cgd 
    709   1.29       cgd 	if (pgrp)
    710  1.112     lukem 		for (p = pgrp->pg_members.lh_first; p != 0;
    711  1.112     lukem 		    p = p->p_pglist.le_next)
    712   1.29       cgd 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
    713   1.29       cgd 				psignal(p, signum);
    714   1.29       cgd }
    715   1.29       cgd 
    716   1.29       cgd /*
    717   1.29       cgd  * Send a signal caused by a trap to the current process.
    718   1.29       cgd  * If it will be caught immediately, deliver it with correct code.
    719   1.29       cgd  * Otherwise, post it normally.
    720   1.29       cgd  */
    721   1.29       cgd void
    722  1.112     lukem trapsignal(struct proc *p, int signum, u_long code)
    723   1.29       cgd {
    724  1.112     lukem 	struct sigacts *ps;
    725   1.29       cgd 
    726  1.112     lukem 	ps = p->p_sigacts;
    727   1.79   mycroft 	if ((p->p_flag & P_TRACED) == 0 &&
    728  1.109  jdolecek 	    sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
    729  1.109  jdolecek 	    !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
    730   1.29       cgd 		p->p_stats->p_ru.ru_nsignals++;
    731   1.29       cgd #ifdef KTRACE
    732   1.29       cgd 		if (KTRPOINT(p, KTR_PSIG))
    733  1.100  sommerfe 			ktrpsig(p, signum,
    734  1.109  jdolecek 			    SIGACTION_PS(ps, signum).sa_handler,
    735  1.109  jdolecek 			    &p->p_sigctx.ps_sigmask, code);
    736   1.29       cgd #endif
    737  1.121   thorpej 		(*p->p_emul->e_sendsig)(signum, &p->p_sigctx.ps_sigmask,
    738  1.121   thorpej 		    code);
    739  1.105   thorpej 		(void) splsched();	/* XXXSMP */
    740  1.112     lukem 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
    741  1.112     lukem 		    &p->p_sigctx.ps_sigmask);
    742  1.109  jdolecek 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
    743  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    744   1.45   mycroft 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
    745  1.109  jdolecek 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    746  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    747   1.45   mycroft 		}
    748  1.104   thorpej 		(void) spl0();		/* XXXSMP */
    749   1.29       cgd 	} else {
    750  1.109  jdolecek 		p->p_sigctx.ps_code = code;	/* XXX for core dump/debugger */
    751  1.109  jdolecek 		p->p_sigctx.ps_sig = signum;	/* XXX to verify code */
    752   1.29       cgd 		psignal(p, signum);
    753   1.29       cgd 	}
    754   1.29       cgd }
    755   1.29       cgd 
    756   1.29       cgd /*
    757   1.29       cgd  * Send the signal to the process.  If the signal has an action, the action
    758   1.29       cgd  * is usually performed by the target process rather than the caller; we add
    759   1.29       cgd  * the signal to the set of pending signals for the process.
    760   1.29       cgd  *
    761   1.29       cgd  * Exceptions:
    762   1.29       cgd  *   o When a stop signal is sent to a sleeping process that takes the
    763   1.29       cgd  *     default action, the process is stopped without awakening it.
    764   1.29       cgd  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
    765   1.29       cgd  *     regardless of the signal action (eg, blocked or ignored).
    766   1.29       cgd  *
    767   1.29       cgd  * Other ignored signals are discarded immediately.
    768  1.104   thorpej  *
    769  1.104   thorpej  * XXXSMP: Invoked as psignal() or sched_psignal().
    770   1.29       cgd  */
    771   1.29       cgd void
    772  1.112     lukem psignal1(struct proc *p, int signum,
    773  1.112     lukem 	int dolock)		/* XXXSMP: works, but icky */
    774   1.29       cgd {
    775  1.112     lukem 	int	s, prop;
    776  1.112     lukem 	sig_t	action;
    777   1.29       cgd 
    778   1.79   mycroft #ifdef DIAGNOSTIC
    779   1.79   mycroft 	if (signum <= 0 || signum >= NSIG)
    780   1.29       cgd 		panic("psignal signal number");
    781  1.104   thorpej 
    782  1.104   thorpej 	/* XXXSMP: works, but icky */
    783  1.104   thorpej 	if (dolock)
    784  1.104   thorpej 		SCHED_ASSERT_UNLOCKED();
    785  1.104   thorpej 	else
    786  1.104   thorpej 		SCHED_ASSERT_LOCKED();
    787   1.79   mycroft #endif
    788   1.29       cgd 	prop = sigprop[signum];
    789   1.29       cgd 
    790   1.29       cgd 	/*
    791   1.29       cgd 	 * If proc is traced, always give parent a chance.
    792   1.29       cgd 	 */
    793   1.29       cgd 	if (p->p_flag & P_TRACED)
    794   1.29       cgd 		action = SIG_DFL;
    795   1.29       cgd 	else {
    796   1.29       cgd 		/*
    797   1.29       cgd 		 * If the signal is being ignored,
    798   1.29       cgd 		 * then we forget about it immediately.
    799  1.109  jdolecek 		 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
    800   1.29       cgd 		 * and if it is set to SIG_IGN,
    801   1.29       cgd 		 * action will be SIG_DFL here.)
    802   1.29       cgd 		 */
    803  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigignore, signum))
    804   1.29       cgd 			return;
    805  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigmask, signum))
    806   1.29       cgd 			action = SIG_HOLD;
    807  1.109  jdolecek 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
    808   1.29       cgd 			action = SIG_CATCH;
    809   1.44   mycroft 		else {
    810   1.29       cgd 			action = SIG_DFL;
    811   1.44   mycroft 
    812   1.44   mycroft 			if (prop & SA_KILL && p->p_nice > NZERO)
    813   1.44   mycroft 				p->p_nice = NZERO;
    814   1.44   mycroft 
    815   1.44   mycroft 			/*
    816   1.44   mycroft 			 * If sending a tty stop signal to a member of an
    817   1.44   mycroft 			 * orphaned process group, discard the signal here if
    818   1.44   mycroft 			 * the action is default; don't stop the process below
    819   1.44   mycroft 			 * if sleeping, and don't clear any pending SIGCONT.
    820   1.44   mycroft 			 */
    821   1.44   mycroft 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
    822   1.44   mycroft 				return;
    823   1.44   mycroft 		}
    824   1.29       cgd 	}
    825   1.29       cgd 
    826   1.29       cgd 	if (prop & SA_CONT)
    827  1.109  jdolecek 		sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
    828   1.29       cgd 
    829   1.44   mycroft 	if (prop & SA_STOP)
    830  1.109  jdolecek 		sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
    831   1.44   mycroft 
    832  1.109  jdolecek 	sigaddset(&p->p_sigctx.ps_siglist, signum);
    833  1.110   thorpej 
    834  1.110   thorpej 	/* CHECKSIGS() is "inlined" here. */
    835  1.109  jdolecek 	p->p_sigctx.ps_sigcheck = 1;
    836   1.29       cgd 
    837   1.29       cgd 	/*
    838   1.29       cgd 	 * Defer further processing for signals which are held,
    839   1.29       cgd 	 * except that stopped processes must be continued by SIGCONT.
    840   1.29       cgd 	 */
    841   1.29       cgd 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
    842   1.29       cgd 		return;
    843  1.104   thorpej 
    844  1.104   thorpej 	/* XXXSMP: works, but icky */
    845  1.104   thorpej 	if (dolock)
    846  1.104   thorpej 		SCHED_LOCK(s);
    847  1.104   thorpej 
    848   1.29       cgd 	switch (p->p_stat) {
    849   1.29       cgd 	case SSLEEP:
    850   1.29       cgd 		/*
    851   1.29       cgd 		 * If process is sleeping uninterruptibly
    852   1.29       cgd 		 * we can't interrupt the sleep... the signal will
    853   1.29       cgd 		 * be noticed when the process returns through
    854   1.29       cgd 		 * trap() or syscall().
    855   1.29       cgd 		 */
    856   1.29       cgd 		if ((p->p_flag & P_SINTR) == 0)
    857   1.29       cgd 			goto out;
    858   1.29       cgd 		/*
    859   1.29       cgd 		 * Process is sleeping and traced... make it runnable
    860   1.29       cgd 		 * so it can discover the signal in issignal() and stop
    861   1.29       cgd 		 * for the parent.
    862   1.29       cgd 		 */
    863   1.29       cgd 		if (p->p_flag & P_TRACED)
    864   1.29       cgd 			goto run;
    865   1.29       cgd 		/*
    866   1.29       cgd 		 * If SIGCONT is default (or ignored) and process is
    867   1.29       cgd 		 * asleep, we are finished; the process should not
    868   1.29       cgd 		 * be awakened.
    869   1.29       cgd 		 */
    870   1.29       cgd 		if ((prop & SA_CONT) && action == SIG_DFL) {
    871  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    872   1.29       cgd 			goto out;
    873   1.29       cgd 		}
    874   1.29       cgd 		/*
    875   1.29       cgd 		 * When a sleeping process receives a stop
    876   1.29       cgd 		 * signal, process immediately if possible.
    877   1.29       cgd 		 */
    878   1.34   mycroft 		if ((prop & SA_STOP) && action == SIG_DFL) {
    879   1.29       cgd 			/*
    880   1.29       cgd 			 * If a child holding parent blocked,
    881   1.29       cgd 			 * stopping could cause deadlock.
    882   1.29       cgd 			 */
    883   1.29       cgd 			if (p->p_flag & P_PPWAIT)
    884   1.29       cgd 				goto out;
    885  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    886   1.29       cgd 			p->p_xstat = signum;
    887  1.104   thorpej 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
    888  1.104   thorpej 				/*
    889  1.104   thorpej 				 * XXXSMP: recursive call; don't lock
    890  1.104   thorpej 				 * the second time around.
    891  1.104   thorpej 				 */
    892  1.104   thorpej 				sched_psignal(p->p_pptr, SIGCHLD);
    893  1.104   thorpej 			}
    894  1.104   thorpej 			proc_stop(p);	/* XXXSMP: recurse? */
    895   1.29       cgd 			goto out;
    896   1.34   mycroft 		}
    897   1.34   mycroft 		/*
    898   1.34   mycroft 		 * All other (caught or default) signals
    899   1.34   mycroft 		 * cause the process to run.
    900   1.34   mycroft 		 */
    901   1.34   mycroft 		goto runfast;
    902   1.29       cgd 		/*NOTREACHED*/
    903   1.29       cgd 
    904   1.29       cgd 	case SSTOP:
    905   1.29       cgd 		/*
    906   1.29       cgd 		 * If traced process is already stopped,
    907   1.29       cgd 		 * then no further action is necessary.
    908   1.29       cgd 		 */
    909   1.29       cgd 		if (p->p_flag & P_TRACED)
    910   1.29       cgd 			goto out;
    911   1.29       cgd 
    912   1.29       cgd 		/*
    913   1.29       cgd 		 * Kill signal always sets processes running.
    914   1.29       cgd 		 */
    915   1.29       cgd 		if (signum == SIGKILL)
    916   1.29       cgd 			goto runfast;
    917   1.29       cgd 
    918   1.29       cgd 		if (prop & SA_CONT) {
    919   1.29       cgd 			/*
    920   1.29       cgd 			 * If SIGCONT is default (or ignored), we continue the
    921  1.109  jdolecek 			 * process but don't leave the signal in p_sigctx.ps_siglist, as
    922   1.29       cgd 			 * it has no further action.  If SIGCONT is held, we
    923   1.29       cgd 			 * continue the process and leave the signal in
    924  1.109  jdolecek 			 * p_sigctx.ps_siglist.  If the process catches SIGCONT, let it
    925   1.29       cgd 			 * handle the signal itself.  If it isn't waiting on
    926   1.29       cgd 			 * an event, then it goes back to run state.
    927   1.29       cgd 			 * Otherwise, process goes back to sleep state.
    928   1.29       cgd 			 */
    929   1.29       cgd 			if (action == SIG_DFL)
    930  1.109  jdolecek 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    931   1.29       cgd 			if (action == SIG_CATCH)
    932   1.29       cgd 				goto runfast;
    933   1.29       cgd 			if (p->p_wchan == 0)
    934   1.29       cgd 				goto run;
    935   1.29       cgd 			p->p_stat = SSLEEP;
    936   1.29       cgd 			goto out;
    937   1.29       cgd 		}
    938   1.29       cgd 
    939   1.29       cgd 		if (prop & SA_STOP) {
    940   1.29       cgd 			/*
    941   1.29       cgd 			 * Already stopped, don't need to stop again.
    942   1.29       cgd 			 * (If we did the shell could get confused.)
    943   1.29       cgd 			 */
    944  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    945   1.29       cgd 			goto out;
    946   1.29       cgd 		}
    947   1.29       cgd 
    948   1.29       cgd 		/*
    949   1.29       cgd 		 * If process is sleeping interruptibly, then simulate a
    950   1.29       cgd 		 * wakeup so that when it is continued, it will be made
    951   1.29       cgd 		 * runnable and can look at the signal.  But don't make
    952   1.29       cgd 		 * the process runnable, leave it stopped.
    953   1.29       cgd 		 */
    954   1.29       cgd 		if (p->p_wchan && p->p_flag & P_SINTR)
    955   1.29       cgd 			unsleep(p);
    956   1.29       cgd 		goto out;
    957  1.110   thorpej #ifdef __HAVE_AST_PERPROC
    958  1.110   thorpej 	case SONPROC:
    959  1.110   thorpej 	case SRUN:
    960  1.110   thorpej 	case SIDL:
    961  1.110   thorpej 		/*
    962  1.110   thorpej 		 * SONPROC: We're running, notice the signal when
    963  1.110   thorpej 		 * we return back to userspace.
    964  1.110   thorpej 		 *
    965  1.110   thorpej 		 * SRUN, SIDL: Notice the signal when we run again
    966  1.110   thorpej 		 * and return to back to userspace.
    967  1.110   thorpej 		 */
    968  1.110   thorpej 		signotify(p);
    969  1.110   thorpej 		goto out;
    970   1.29       cgd 
    971  1.110   thorpej 	default:
    972  1.110   thorpej 		/*
    973  1.110   thorpej 		 * SDEAD, SZOMB: The signal will never be noticed.
    974  1.110   thorpej 		 */
    975  1.110   thorpej 		goto out;
    976  1.110   thorpej #else /* ! __HAVE_AST_PERPROC */
    977   1.99   thorpej 	case SONPROC:
    978   1.99   thorpej 		/*
    979   1.99   thorpej 		 * We're running; notice the signal.
    980   1.99   thorpej 		 */
    981   1.99   thorpej 		signotify(p);
    982   1.99   thorpej 		goto out;
    983   1.99   thorpej 
    984   1.29       cgd 	default:
    985   1.29       cgd 		/*
    986   1.99   thorpej 		 * SRUN, SIDL, SDEAD, SZOMB do nothing with the signal.
    987   1.29       cgd 		 * It will either never be noticed, or noticed very soon.
    988   1.29       cgd 		 */
    989   1.29       cgd 		goto out;
    990  1.110   thorpej #endif /* __HAVE_AST_PERPROC */
    991   1.29       cgd 	}
    992   1.29       cgd 	/*NOTREACHED*/
    993   1.29       cgd 
    994  1.112     lukem  runfast:
    995   1.29       cgd 	/*
    996   1.29       cgd 	 * Raise priority to at least PUSER.
    997   1.29       cgd 	 */
    998   1.29       cgd 	if (p->p_priority > PUSER)
    999   1.29       cgd 		p->p_priority = PUSER;
   1000  1.112     lukem  run:
   1001  1.104   thorpej 	setrunnable(p);		/* XXXSMP: recurse? */
   1002  1.112     lukem  out:
   1003  1.104   thorpej 	/* XXXSMP: works, but icky */
   1004  1.104   thorpej 	if (dolock)
   1005  1.104   thorpej 		SCHED_UNLOCK(s);
   1006   1.29       cgd }
   1007   1.29       cgd 
   1008  1.112     lukem static __inline int firstsig(const sigset_t *);
   1009   1.79   mycroft 
   1010   1.79   mycroft static __inline int
   1011  1.112     lukem firstsig(const sigset_t *ss)
   1012   1.79   mycroft {
   1013   1.79   mycroft 	int sig;
   1014   1.79   mycroft 
   1015   1.79   mycroft 	sig = ffs(ss->__bits[0]);
   1016   1.79   mycroft 	if (sig != 0)
   1017   1.79   mycroft 		return (sig);
   1018   1.79   mycroft #if NSIG > 33
   1019   1.79   mycroft 	sig = ffs(ss->__bits[1]);
   1020   1.79   mycroft 	if (sig != 0)
   1021   1.79   mycroft 		return (sig + 32);
   1022   1.79   mycroft #endif
   1023   1.79   mycroft #if NSIG > 65
   1024   1.79   mycroft 	sig = ffs(ss->__bits[2]);
   1025   1.79   mycroft 	if (sig != 0)
   1026   1.79   mycroft 		return (sig + 64);
   1027   1.79   mycroft #endif
   1028   1.79   mycroft #if NSIG > 97
   1029   1.79   mycroft 	sig = ffs(ss->__bits[3]);
   1030   1.79   mycroft 	if (sig != 0)
   1031   1.79   mycroft 		return (sig + 96);
   1032   1.79   mycroft #endif
   1033   1.79   mycroft 	return (0);
   1034   1.79   mycroft }
   1035   1.79   mycroft 
   1036   1.29       cgd /*
   1037   1.29       cgd  * If the current process has received a signal (should be caught or cause
   1038   1.29       cgd  * termination, should interrupt current syscall), return the signal number.
   1039   1.29       cgd  * Stop signals with default action are processed immediately, then cleared;
   1040   1.29       cgd  * they aren't returned.  This is checked after each entry to the system for
   1041   1.29       cgd  * a syscall or trap (though this can usually be done without calling issignal
   1042   1.29       cgd  * by checking the pending signal masks in the CURSIG macro.) The normal call
   1043   1.29       cgd  * sequence is
   1044   1.29       cgd  *
   1045   1.29       cgd  *	while (signum = CURSIG(curproc))
   1046   1.29       cgd  *		postsig(signum);
   1047   1.29       cgd  */
   1048   1.29       cgd int
   1049  1.112     lukem issignal(struct proc *p)
   1050   1.29       cgd {
   1051  1.112     lukem 	int		s, signum, prop;
   1052  1.119  christos 	int		dolock = (p->p_flag & P_SINTR) == 0, locked = !dolock;
   1053  1.112     lukem 	sigset_t	ss;
   1054   1.29       cgd 
   1055   1.29       cgd 	for (;;) {
   1056   1.79   mycroft 		sigpending1(p, &ss);
   1057   1.29       cgd 		if (p->p_flag & P_PPWAIT)
   1058   1.79   mycroft 			sigminusset(&stopsigmask, &ss);
   1059   1.79   mycroft 		signum = firstsig(&ss);
   1060   1.79   mycroft 		if (signum == 0) {		 	/* no signal to send */
   1061  1.109  jdolecek 			p->p_sigctx.ps_sigcheck = 0;
   1062  1.119  christos 			if (locked && dolock)
   1063  1.119  christos 				SCHED_LOCK(s);
   1064   1.29       cgd 			return (0);
   1065   1.79   mycroft 		}
   1066  1.112     lukem 							/* take the signal! */
   1067  1.112     lukem 		sigdelset(&p->p_sigctx.ps_siglist, signum);
   1068   1.42   mycroft 
   1069   1.29       cgd 		/*
   1070   1.29       cgd 		 * We should see pending but ignored signals
   1071   1.29       cgd 		 * only if P_TRACED was on when they were posted.
   1072   1.29       cgd 		 */
   1073  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
   1074   1.79   mycroft 		    (p->p_flag & P_TRACED) == 0)
   1075   1.29       cgd 			continue;
   1076   1.42   mycroft 
   1077   1.29       cgd 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
   1078   1.29       cgd 			/*
   1079   1.29       cgd 			 * If traced, always stop, and stay
   1080   1.29       cgd 			 * stopped until released by the debugger.
   1081   1.29       cgd 			 */
   1082   1.29       cgd 			p->p_xstat = signum;
   1083   1.66   mycroft 			if ((p->p_flag & P_FSTRACE) == 0)
   1084  1.119  christos 				psignal1(p->p_pptr, SIGCHLD, dolock);
   1085  1.119  christos 			if (dolock)
   1086  1.119  christos 				SCHED_LOCK(s);
   1087  1.114   nathanw 			proc_stop(p);
   1088  1.114   nathanw 			mi_switch(p);
   1089  1.114   nathanw 			SCHED_ASSERT_UNLOCKED();
   1090  1.119  christos 			if (dolock)
   1091  1.119  christos 				splx(s);
   1092  1.119  christos 			else
   1093  1.119  christos 				dolock = 1;
   1094   1.29       cgd 
   1095   1.29       cgd 			/*
   1096   1.42   mycroft 			 * If we are no longer being traced, or the parent
   1097   1.42   mycroft 			 * didn't give us a signal, look for more signals.
   1098   1.29       cgd 			 */
   1099   1.42   mycroft 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
   1100   1.29       cgd 				continue;
   1101   1.29       cgd 
   1102   1.29       cgd 			/*
   1103   1.42   mycroft 			 * If the new signal is being masked, look for other
   1104   1.42   mycroft 			 * signals.
   1105   1.29       cgd 			 */
   1106   1.42   mycroft 			signum = p->p_xstat;
   1107  1.112     lukem 			/*
   1108  1.112     lukem 			 * `p->p_sigctx.ps_siglist |= mask' is done
   1109  1.112     lukem 			 * in setrunnable().
   1110  1.112     lukem 			 */
   1111  1.109  jdolecek 			if (sigismember(&p->p_sigctx.ps_sigmask, signum))
   1112   1.29       cgd 				continue;
   1113  1.112     lukem 							/* take the signal! */
   1114  1.112     lukem 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1115   1.29       cgd 		}
   1116   1.29       cgd 
   1117   1.42   mycroft 		prop = sigprop[signum];
   1118   1.42   mycroft 
   1119   1.29       cgd 		/*
   1120   1.29       cgd 		 * Decide whether the signal should be returned.
   1121   1.29       cgd 		 * Return the signal's number, or fall through
   1122   1.29       cgd 		 * to clear it from the pending mask.
   1123   1.29       cgd 		 */
   1124  1.109  jdolecek 		switch ((long)SIGACTION(p, signum).sa_handler) {
   1125   1.29       cgd 
   1126   1.33       cgd 		case (long)SIG_DFL:
   1127   1.29       cgd 			/*
   1128   1.29       cgd 			 * Don't take default actions on system processes.
   1129   1.29       cgd 			 */
   1130   1.29       cgd 			if (p->p_pid <= 1) {
   1131   1.29       cgd #ifdef DIAGNOSTIC
   1132   1.29       cgd 				/*
   1133   1.29       cgd 				 * Are you sure you want to ignore SIGSEGV
   1134   1.29       cgd 				 * in init? XXX
   1135   1.29       cgd 				 */
   1136   1.57  christos 				printf("Process (pid %d) got signal %d\n",
   1137   1.29       cgd 				    p->p_pid, signum);
   1138   1.29       cgd #endif
   1139   1.29       cgd 				break;		/* == ignore */
   1140   1.29       cgd 			}
   1141   1.29       cgd 			/*
   1142   1.29       cgd 			 * If there is a pending stop signal to process
   1143   1.29       cgd 			 * with default action, stop here,
   1144   1.29       cgd 			 * then clear the signal.  However,
   1145   1.29       cgd 			 * if process is member of an orphaned
   1146   1.29       cgd 			 * process group, ignore tty stop signals.
   1147   1.29       cgd 			 */
   1148   1.29       cgd 			if (prop & SA_STOP) {
   1149   1.29       cgd 				if (p->p_flag & P_TRACED ||
   1150   1.29       cgd 		    		    (p->p_pgrp->pg_jobc == 0 &&
   1151   1.29       cgd 				    prop & SA_TTYSTOP))
   1152   1.29       cgd 					break;	/* == ignore */
   1153   1.29       cgd 				p->p_xstat = signum;
   1154   1.29       cgd 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
   1155  1.119  christos 					psignal1(p->p_pptr, SIGCHLD, dolock);
   1156  1.119  christos 				if (dolock)
   1157  1.119  christos 					SCHED_LOCK(s);
   1158  1.104   thorpej 				proc_stop(p);
   1159  1.100  sommerfe 				mi_switch(p);
   1160  1.104   thorpej 				SCHED_ASSERT_UNLOCKED();
   1161  1.119  christos 				if (dolock)
   1162  1.119  christos 					splx(s);
   1163  1.119  christos 				else
   1164  1.119  christos 					dolock = 1;
   1165   1.29       cgd 				break;
   1166   1.29       cgd 			} else if (prop & SA_IGNORE) {
   1167   1.29       cgd 				/*
   1168   1.29       cgd 				 * Except for SIGCONT, shouldn't get here.
   1169   1.29       cgd 				 * Default action is to ignore; drop it.
   1170   1.29       cgd 				 */
   1171   1.29       cgd 				break;		/* == ignore */
   1172   1.29       cgd 			} else
   1173   1.42   mycroft 				goto keep;
   1174   1.29       cgd 			/*NOTREACHED*/
   1175   1.29       cgd 
   1176   1.33       cgd 		case (long)SIG_IGN:
   1177   1.29       cgd 			/*
   1178   1.29       cgd 			 * Masking above should prevent us ever trying
   1179   1.29       cgd 			 * to take action on an ignored signal other
   1180   1.29       cgd 			 * than SIGCONT, unless process is traced.
   1181   1.29       cgd 			 */
   1182   1.29       cgd 			if ((prop & SA_CONT) == 0 &&
   1183   1.29       cgd 			    (p->p_flag & P_TRACED) == 0)
   1184   1.57  christos 				printf("issignal\n");
   1185   1.29       cgd 			break;		/* == ignore */
   1186   1.29       cgd 
   1187   1.29       cgd 		default:
   1188   1.29       cgd 			/*
   1189   1.29       cgd 			 * This signal has an action, let
   1190   1.29       cgd 			 * postsig() process it.
   1191   1.29       cgd 			 */
   1192   1.42   mycroft 			goto keep;
   1193   1.29       cgd 		}
   1194   1.29       cgd 	}
   1195   1.29       cgd 	/* NOTREACHED */
   1196   1.42   mycroft 
   1197  1.112     lukem  keep:
   1198  1.112     lukem 						/* leave the signal for later */
   1199  1.112     lukem 	sigaddset(&p->p_sigctx.ps_siglist, signum);
   1200  1.110   thorpej 	CHECKSIGS(p);
   1201  1.119  christos 	if (locked && dolock)
   1202  1.119  christos 		SCHED_LOCK(s);
   1203   1.42   mycroft 	return (signum);
   1204   1.29       cgd }
   1205   1.29       cgd 
   1206   1.29       cgd /*
   1207   1.29       cgd  * Put the argument process into the stopped state and notify the parent
   1208   1.29       cgd  * via wakeup.  Signals are handled elsewhere.  The process must not be
   1209   1.29       cgd  * on the run queue.
   1210   1.29       cgd  */
   1211  1.104   thorpej static void
   1212  1.112     lukem proc_stop(struct proc *p)
   1213   1.29       cgd {
   1214   1.29       cgd 
   1215  1.104   thorpej 	SCHED_ASSERT_LOCKED();
   1216  1.104   thorpej 
   1217   1.29       cgd 	p->p_stat = SSTOP;
   1218   1.29       cgd 	p->p_flag &= ~P_WAITED;
   1219  1.104   thorpej 	sched_wakeup((caddr_t)p->p_pptr);
   1220   1.29       cgd }
   1221   1.29       cgd 
   1222   1.29       cgd /*
   1223   1.29       cgd  * Take the action for the specified signal
   1224   1.29       cgd  * from the current set of pending signals.
   1225   1.29       cgd  */
   1226   1.29       cgd void
   1227  1.112     lukem postsig(int signum)
   1228   1.29       cgd {
   1229  1.112     lukem 	struct proc	*p;
   1230  1.112     lukem 	struct sigacts	*ps;
   1231  1.112     lukem 	sig_t		action;
   1232  1.112     lukem 	u_long		code;
   1233  1.112     lukem 	sigset_t	*returnmask;
   1234   1.29       cgd 
   1235  1.112     lukem 	p = curproc;
   1236  1.112     lukem 	ps = p->p_sigacts;
   1237   1.29       cgd #ifdef DIAGNOSTIC
   1238   1.29       cgd 	if (signum == 0)
   1239   1.29       cgd 		panic("postsig");
   1240   1.29       cgd #endif
   1241  1.106   thorpej 
   1242  1.106   thorpej 	KERNEL_PROC_LOCK(p);
   1243  1.106   thorpej 
   1244  1.109  jdolecek 	sigdelset(&p->p_sigctx.ps_siglist, signum);
   1245  1.109  jdolecek 	action = SIGACTION_PS(ps, signum).sa_handler;
   1246   1.29       cgd #ifdef KTRACE
   1247   1.29       cgd 	if (KTRPOINT(p, KTR_PSIG))
   1248  1.100  sommerfe 		ktrpsig(p,
   1249  1.109  jdolecek 		    signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
   1250  1.109  jdolecek 		    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
   1251   1.29       cgd #endif
   1252   1.29       cgd 	if (action == SIG_DFL) {
   1253   1.29       cgd 		/*
   1254   1.29       cgd 		 * Default action, where the default is to kill
   1255   1.29       cgd 		 * the process.  (Other cases were ignored above.)
   1256   1.29       cgd 		 */
   1257   1.29       cgd 		sigexit(p, signum);
   1258   1.29       cgd 		/* NOTREACHED */
   1259   1.29       cgd 	} else {
   1260   1.29       cgd 		/*
   1261   1.29       cgd 		 * If we get here, the signal must be caught.
   1262   1.29       cgd 		 */
   1263   1.29       cgd #ifdef DIAGNOSTIC
   1264  1.112     lukem 		if (action == SIG_IGN ||
   1265  1.112     lukem 		    sigismember(&p->p_sigctx.ps_sigmask, signum))
   1266   1.29       cgd 			panic("postsig action");
   1267   1.29       cgd #endif
   1268   1.29       cgd 		/*
   1269   1.29       cgd 		 * Set the new mask value and also defer further
   1270   1.29       cgd 		 * occurences of this signal.
   1271   1.29       cgd 		 *
   1272   1.29       cgd 		 * Special case: user has done a sigpause.  Here the
   1273   1.29       cgd 		 * current mask is not of interest, but rather the
   1274   1.29       cgd 		 * mask from before the sigpause is what we want
   1275   1.29       cgd 		 * restored after the signal processing is completed.
   1276   1.29       cgd 		 */
   1277  1.109  jdolecek 		if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
   1278  1.109  jdolecek 			returnmask = &p->p_sigctx.ps_oldmask;
   1279  1.109  jdolecek 			p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
   1280   1.29       cgd 		} else
   1281  1.109  jdolecek 			returnmask = &p->p_sigctx.ps_sigmask;
   1282   1.29       cgd 		p->p_stats->p_ru.ru_nsignals++;
   1283  1.109  jdolecek 		if (p->p_sigctx.ps_sig != signum) {
   1284   1.29       cgd 			code = 0;
   1285   1.29       cgd 		} else {
   1286  1.109  jdolecek 			code = p->p_sigctx.ps_code;
   1287  1.109  jdolecek 			p->p_sigctx.ps_code = 0;
   1288  1.109  jdolecek 			p->p_sigctx.ps_sig = 0;
   1289   1.29       cgd 		}
   1290  1.121   thorpej 		(*p->p_emul->e_sendsig)(signum, returnmask, code);
   1291  1.105   thorpej 		(void) splsched();	/* XXXSMP */
   1292  1.112     lukem 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
   1293  1.112     lukem 		    &p->p_sigctx.ps_sigmask);
   1294  1.109  jdolecek 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
   1295  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
   1296   1.79   mycroft 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
   1297  1.109  jdolecek 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
   1298  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
   1299   1.79   mycroft 		}
   1300  1.104   thorpej 		(void) spl0();		/* XXXSMP */
   1301   1.29       cgd 	}
   1302  1.106   thorpej 
   1303  1.106   thorpej 	KERNEL_PROC_UNLOCK(p);
   1304   1.29       cgd }
   1305   1.29       cgd 
   1306   1.29       cgd /*
   1307   1.29       cgd  * Kill the current process for stated reason.
   1308   1.29       cgd  */
   1309   1.52  christos void
   1310  1.112     lukem killproc(struct proc *p, char *why)
   1311   1.29       cgd {
   1312   1.29       cgd 
   1313   1.29       cgd 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
   1314   1.29       cgd 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
   1315   1.29       cgd 	psignal(p, SIGKILL);
   1316   1.29       cgd }
   1317   1.29       cgd 
   1318   1.29       cgd /*
   1319   1.29       cgd  * Force the current process to exit with the specified signal, dumping core
   1320   1.29       cgd  * if appropriate.  We bypass the normal tests for masked and caught signals,
   1321   1.29       cgd  * allowing unrecoverable failures to terminate the process without changing
   1322   1.29       cgd  * signal state.  Mark the accounting record with the signal termination.
   1323   1.29       cgd  * If dumping core, save the signal number for the debugger.  Calls exit and
   1324   1.29       cgd  * does not return.
   1325   1.29       cgd  */
   1326   1.96      fair 
   1327   1.97      fair #if defined(DEBUG)
   1328   1.96      fair int	kern_logsigexit = 1;	/* not static to make public for sysctl */
   1329   1.96      fair #else
   1330   1.96      fair int	kern_logsigexit = 0;	/* not static to make public for sysctl */
   1331   1.96      fair #endif
   1332   1.96      fair 
   1333  1.102  sommerfe static	const char logcoredump[] =
   1334   1.96      fair 	"pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
   1335  1.102  sommerfe static	const char lognocoredump[] =
   1336   1.96      fair 	"pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
   1337   1.96      fair 
   1338   1.52  christos void
   1339  1.112     lukem sigexit(struct proc *p, int signum)
   1340   1.29       cgd {
   1341  1.112     lukem 	int	error, exitsig;
   1342   1.96      fair 
   1343  1.112     lukem 	exitsig = signum;
   1344   1.29       cgd 	p->p_acflag |= AXSIG;
   1345   1.29       cgd 	if (sigprop[signum] & SA_CORE) {
   1346  1.109  jdolecek 		p->p_sigctx.ps_sig = signum;
   1347  1.102  sommerfe 		if ((error = coredump(p)) == 0)
   1348  1.102  sommerfe 			exitsig |= WCOREFLAG;
   1349  1.102  sommerfe 
   1350  1.102  sommerfe 		if (kern_logsigexit) {
   1351  1.102  sommerfe 			int uid = p->p_cred && p->p_ucred ?
   1352  1.102  sommerfe 				p->p_ucred->cr_uid : -1;
   1353  1.102  sommerfe 
   1354  1.102  sommerfe 			if (error)
   1355  1.102  sommerfe 				log(LOG_INFO, lognocoredump, p->p_pid,
   1356  1.102  sommerfe 				    p->p_comm, uid, signum, error);
   1357  1.102  sommerfe 			else
   1358  1.102  sommerfe 				log(LOG_INFO, logcoredump, p->p_pid,
   1359  1.102  sommerfe 				    p->p_comm, uid, signum);
   1360   1.96      fair 		}
   1361   1.96      fair 
   1362   1.29       cgd 	}
   1363   1.96      fair 
   1364  1.102  sommerfe 	exit1(p, W_EXITCODE(0, exitsig));
   1365   1.29       cgd 	/* NOTREACHED */
   1366   1.29       cgd }
   1367   1.29       cgd 
   1368   1.29       cgd /*
   1369   1.75   nathanw  * Dump core, into a file named "progname.core" or "core" (depending on the
   1370   1.75   nathanw  * value of shortcorename), unless the process was setuid/setgid.
   1371   1.29       cgd  */
   1372   1.29       cgd int
   1373  1.112     lukem coredump(struct proc *p)
   1374   1.29       cgd {
   1375  1.112     lukem 	struct vnode		*vp;
   1376  1.112     lukem 	struct vmspace		*vm;
   1377  1.112     lukem 	struct ucred		*cred;
   1378  1.112     lukem 	struct nameidata	nd;
   1379  1.112     lukem 	struct vattr		vattr;
   1380  1.112     lukem 	int			error, error1;
   1381  1.112     lukem 	char			name[MAXPATHLEN];
   1382  1.112     lukem 
   1383  1.112     lukem 	vm = p->p_vmspace;
   1384  1.112     lukem 	cred = p->p_cred->pc_ucred;
   1385   1.29       cgd 
   1386   1.59       cgd 	/*
   1387   1.59       cgd 	 * Make sure the process has not set-id, to prevent data leaks.
   1388   1.59       cgd 	 */
   1389   1.58       mrg 	if (p->p_flag & P_SUGID)
   1390   1.59       cgd 		return (EPERM);
   1391   1.59       cgd 
   1392   1.59       cgd 	/*
   1393   1.59       cgd 	 * Refuse to core if the data + stack + user size is larger than
   1394   1.59       cgd 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
   1395   1.59       cgd 	 * data.
   1396   1.59       cgd 	 */
   1397   1.30   deraadt 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
   1398   1.29       cgd 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
   1399   1.59       cgd 		return (EFBIG);		/* better error code? */
   1400   1.59       cgd 
   1401   1.59       cgd 	/*
   1402   1.59       cgd 	 * The core dump will go in the current working directory.  Make
   1403   1.80        pk 	 * sure that the directory is still there and that the mount flags
   1404   1.80        pk 	 * allow us to write core dumps there.
   1405   1.59       cgd 	 */
   1406   1.88   thorpej 	vp = p->p_cwdi->cwdi_cdir;
   1407   1.80        pk 	if (vp->v_mount == NULL ||
   1408   1.80        pk 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
   1409   1.59       cgd 		return (EPERM);
   1410   1.59       cgd 
   1411  1.100  sommerfe 	error = build_corename(p, name);
   1412   1.94    bouyer 	if (error)
   1413   1.94    bouyer 		return error;
   1414   1.94    bouyer 
   1415   1.93    bouyer 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
   1416   1.93    bouyer 	error = vn_open(&nd, O_CREAT | FWRITE | FNOSYMLINK, S_IRUSR | S_IWUSR);
   1417   1.52  christos 	if (error)
   1418   1.29       cgd 		return (error);
   1419   1.29       cgd 	vp = nd.ni_vp;
   1420   1.29       cgd 
   1421   1.29       cgd 	/* Don't dump to non-regular files or files with links. */
   1422   1.29       cgd 	if (vp->v_type != VREG ||
   1423   1.29       cgd 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
   1424   1.59       cgd 		error = EINVAL;
   1425   1.29       cgd 		goto out;
   1426   1.29       cgd 	}
   1427   1.29       cgd 	VATTR_NULL(&vattr);
   1428   1.29       cgd 	vattr.va_size = 0;
   1429   1.37   mycroft 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1430   1.29       cgd 	VOP_SETATTR(vp, &vattr, cred, p);
   1431   1.29       cgd 	p->p_acflag |= ACORE;
   1432   1.95       eeh 
   1433  1.118   thorpej 	/* Now dump the actual core file. */
   1434  1.118   thorpej 	error = (*p->p_execsw->es_coredump)(p, vp, cred);
   1435  1.112     lukem  out:
   1436   1.71      fvdl 	VOP_UNLOCK(vp, 0);
   1437   1.29       cgd 	error1 = vn_close(vp, FWRITE, cred, p);
   1438   1.29       cgd 	if (error == 0)
   1439   1.29       cgd 		error = error1;
   1440   1.29       cgd 	return (error);
   1441   1.29       cgd }
   1442   1.29       cgd 
   1443   1.29       cgd /*
   1444   1.29       cgd  * Nonexistent system call-- signal process (may want to handle it).
   1445   1.29       cgd  * Flag error in case process won't see signal immediately (blocked or ignored).
   1446   1.29       cgd  */
   1447   1.29       cgd /* ARGSUSED */
   1448   1.29       cgd int
   1449  1.112     lukem sys_nosys(struct proc *p, void *v, register_t *retval)
   1450   1.29       cgd {
   1451   1.29       cgd 
   1452   1.29       cgd 	psignal(p, SIGSYS);
   1453   1.36       cgd 	return (ENOSYS);
   1454   1.94    bouyer }
   1455   1.94    bouyer 
   1456   1.94    bouyer static int
   1457  1.112     lukem build_corename(struct proc *p, char dst[MAXPATHLEN])
   1458   1.94    bouyer {
   1459  1.112     lukem 	const char	*s;
   1460  1.112     lukem 	char		*d, *end;
   1461  1.112     lukem 	int		i;
   1462  1.100  sommerfe 
   1463  1.107     enami 	for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
   1464   1.94    bouyer 	    *s != '\0'; s++) {
   1465   1.94    bouyer 		if (*s == '%') {
   1466  1.107     enami 			switch (*(s + 1)) {
   1467   1.94    bouyer 			case 'n':
   1468  1.107     enami 				i = snprintf(d, end - d, "%s", p->p_comm);
   1469   1.94    bouyer 				break;
   1470   1.94    bouyer 			case 'p':
   1471  1.107     enami 				i = snprintf(d, end - d, "%d", p->p_pid);
   1472   1.94    bouyer 				break;
   1473   1.94    bouyer 			case 'u':
   1474  1.107     enami 				i = snprintf(d, end - d, "%s",
   1475  1.100  sommerfe 				    p->p_pgrp->pg_session->s_login);
   1476   1.94    bouyer 				break;
   1477   1.94    bouyer 			case 't':
   1478  1.107     enami 				i = snprintf(d, end - d, "%ld",
   1479  1.100  sommerfe 				    p->p_stats->p_start.tv_sec);
   1480   1.94    bouyer 				break;
   1481   1.94    bouyer 			default:
   1482   1.94    bouyer 				goto copy;
   1483   1.94    bouyer 			}
   1484   1.94    bouyer 			d += i;
   1485   1.94    bouyer 			s++;
   1486   1.94    bouyer 		} else {
   1487  1.112     lukem  copy:			*d = *s;
   1488   1.94    bouyer 			d++;
   1489   1.94    bouyer 		}
   1490  1.107     enami 		if (d >= end)
   1491  1.107     enami 			return (ENAMETOOLONG);
   1492   1.94    bouyer 	}
   1493   1.94    bouyer 	*d = '\0';
   1494  1.107     enami 	return (0);
   1495  1.108  jdolecek }
   1496  1.108  jdolecek 
   1497  1.108  jdolecek /*
   1498  1.108  jdolecek  * Returns true if signal is ignored or masked for passed process.
   1499  1.108  jdolecek  */
   1500  1.108  jdolecek int
   1501  1.112     lukem sigismasked(struct proc *p, int sig)
   1502  1.108  jdolecek {
   1503  1.112     lukem 
   1504  1.117     enami 	return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
   1505  1.117     enami 	    sigismember(&p->p_sigctx.ps_sigmask, sig));
   1506   1.29       cgd }
   1507