Home | History | Annotate | Line # | Download | only in kern
kern_sig.c revision 1.174
      1  1.174  jdolecek /*	$NetBSD: kern_sig.c,v 1.174 2003/11/01 17:59:57 jdolecek 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.146       agc  * 3. Neither the name of the University nor the names of its contributors
     21   1.29       cgd  *    may be used to endorse or promote products derived from this software
     22   1.29       cgd  *    without specific prior written permission.
     23   1.29       cgd  *
     24   1.29       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.29       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.29       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.29       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.29       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.29       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.29       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.29       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.29       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.29       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.29       cgd  * SUCH DAMAGE.
     35   1.29       cgd  *
     36   1.71      fvdl  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
     37   1.29       cgd  */
     38  1.116     lukem 
     39  1.116     lukem #include <sys/cdefs.h>
     40  1.174  jdolecek __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.174 2003/11/01 17:59:57 jdolecek Exp $");
     41   1.70       mrg 
     42   1.73   thorpej #include "opt_ktrace.h"
     43   1.74   thorpej #include "opt_compat_sunos.h"
     44  1.158  christos #include "opt_compat_netbsd.h"
     45   1.95       eeh #include "opt_compat_netbsd32.h"
     46   1.29       cgd 
     47   1.29       cgd #define	SIGPROP		/* include signal properties table */
     48   1.29       cgd #include <sys/param.h>
     49   1.29       cgd #include <sys/signalvar.h>
     50   1.29       cgd #include <sys/resourcevar.h>
     51   1.29       cgd #include <sys/namei.h>
     52   1.29       cgd #include <sys/vnode.h>
     53   1.29       cgd #include <sys/proc.h>
     54   1.29       cgd #include <sys/systm.h>
     55   1.29       cgd #include <sys/timeb.h>
     56   1.29       cgd #include <sys/times.h>
     57   1.29       cgd #include <sys/buf.h>
     58   1.29       cgd #include <sys/acct.h>
     59   1.29       cgd #include <sys/file.h>
     60   1.29       cgd #include <sys/kernel.h>
     61   1.29       cgd #include <sys/wait.h>
     62   1.29       cgd #include <sys/ktrace.h>
     63   1.29       cgd #include <sys/syslog.h>
     64   1.29       cgd #include <sys/stat.h>
     65   1.29       cgd #include <sys/core.h>
     66   1.59       cgd #include <sys/filedesc.h>
     67   1.89   thorpej #include <sys/malloc.h>
     68   1.89   thorpej #include <sys/pool.h>
     69  1.130   thorpej #include <sys/ucontext.h>
     70  1.130   thorpej #include <sys/sa.h>
     71  1.130   thorpej #include <sys/savar.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.151  christos static void	child_psignal(struct proc *, int);
     84  1.151  christos static void	proc_stop(struct proc *);
     85  1.112     lukem static int	build_corename(struct proc *, char [MAXPATHLEN]);
     86  1.152  christos static void	ksiginfo_exithook(struct proc *, void *);
     87  1.160  christos static void	ksiginfo_put(struct proc *, const ksiginfo_t *);
     88  1.152  christos static ksiginfo_t *ksiginfo_get(struct proc *, int);
     89  1.160  christos static void	kpsignal2(struct proc *, const ksiginfo_t *, int);
     90  1.152  christos 
     91  1.112     lukem sigset_t	contsigmask, stopsigmask, sigcantmask;
     92   1.29       cgd 
     93  1.112     lukem struct pool	sigacts_pool;	/* memory pool for sigacts structures */
     94  1.130   thorpej struct pool	siginfo_pool;	/* memory pool for siginfo structures */
     95  1.152  christos struct pool	ksiginfo_pool;	/* memory pool for ksiginfo structures */
     96   1.89   thorpej 
     97   1.29       cgd /*
     98   1.29       cgd  * Can process p, with pcred pc, send the signal signum to process q?
     99   1.29       cgd  */
    100  1.112     lukem #define	CANSIGNAL(p, pc, q, signum) \
    101   1.29       cgd 	((pc)->pc_ucred->cr_uid == 0 || \
    102   1.29       cgd 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
    103   1.29       cgd 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
    104   1.29       cgd 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
    105   1.29       cgd 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
    106   1.29       cgd 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
    107   1.29       cgd 
    108   1.89   thorpej /*
    109  1.155  christos  * Remove and return the first ksiginfo element that matches our requested
    110  1.155  christos  * signal, or return NULL if one not found.
    111  1.152  christos  */
    112  1.152  christos static ksiginfo_t *
    113  1.152  christos ksiginfo_get(struct proc *p, int signo)
    114  1.152  christos {
    115  1.155  christos 	ksiginfo_t *ksi;
    116  1.168        pk 	int s;
    117  1.152  christos 
    118  1.168        pk 	s = splsoftclock();
    119  1.155  christos 	simple_lock(&p->p_sigctx.ps_silock);
    120  1.155  christos 	CIRCLEQ_FOREACH(ksi, &p->p_sigctx.ps_siginfo, ksi_list) {
    121  1.155  christos 		if (ksi->ksi_signo == signo) {
    122  1.155  christos 			CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
    123  1.168        pk 			goto out;
    124  1.155  christos 		}
    125  1.152  christos 	}
    126  1.168        pk 	ksi = NULL;
    127  1.168        pk out:
    128  1.155  christos 	simple_unlock(&p->p_sigctx.ps_silock);
    129  1.168        pk 	splx(s);
    130  1.168        pk 	return ksi;
    131  1.152  christos }
    132  1.152  christos 
    133  1.155  christos /*
    134  1.155  christos  * Append a new ksiginfo element to the list of pending ksiginfo's, if
    135  1.155  christos  * we need to (SA_SIGINFO was requested). We replace non RT signals if
    136  1.155  christos  * they already existed in the queue and we add new entries for RT signals,
    137  1.155  christos  * or for non RT signals with non-existing entries.
    138  1.155  christos  */
    139  1.152  christos static void
    140  1.160  christos ksiginfo_put(struct proc *p, const ksiginfo_t *ksi)
    141  1.152  christos {
    142  1.155  christos 	ksiginfo_t *kp;
    143  1.155  christos 	struct sigaction *sa = &SIGACTION_PS(p->p_sigacts, ksi->ksi_signo);
    144  1.167        pk 	int s;
    145  1.152  christos 
    146  1.155  christos 	if ((sa->sa_flags & SA_SIGINFO) == 0)
    147  1.155  christos 		return;
    148  1.152  christos 
    149  1.167        pk 	s = splsoftclock();
    150  1.155  christos 	simple_lock(&p->p_sigctx.ps_silock);
    151  1.152  christos #ifdef notyet	/* XXX: QUEUING */
    152  1.155  christos 	if (ksi->ksi_signo < SIGRTMIN)
    153  1.152  christos #endif
    154  1.155  christos 	{
    155  1.155  christos 		CIRCLEQ_FOREACH(kp, &p->p_sigctx.ps_siginfo, ksi_list) {
    156  1.155  christos 			if (kp->ksi_signo == ksi->ksi_signo) {
    157  1.174  jdolecek 				KSI_COPY(ksi, kp);
    158  1.167        pk 				goto out;
    159  1.155  christos 			}
    160  1.155  christos 		}
    161  1.153  christos 	}
    162  1.155  christos 	kp = pool_get(&ksiginfo_pool, PR_NOWAIT);
    163  1.157  christos 	if (kp == NULL) {
    164  1.157  christos #ifdef DIAGNOSTIC
    165  1.157  christos 		printf("Out of memory allocating siginfo for pid %d\n",
    166  1.157  christos 		    p->p_pid);
    167  1.157  christos #endif
    168  1.167        pk 		goto out;
    169  1.157  christos 	}
    170  1.155  christos 	*kp = *ksi;
    171  1.155  christos 	CIRCLEQ_INSERT_TAIL(&p->p_sigctx.ps_siginfo, kp, ksi_list);
    172  1.167        pk out:
    173  1.155  christos 	simple_unlock(&p->p_sigctx.ps_silock);
    174  1.167        pk 	splx(s);
    175  1.152  christos }
    176  1.152  christos 
    177  1.152  christos /*
    178  1.152  christos  * free all pending ksiginfo on exit
    179  1.152  christos  */
    180  1.152  christos static void
    181  1.152  christos ksiginfo_exithook(struct proc *p, void *v)
    182  1.152  christos {
    183  1.168        pk 	int s;
    184  1.152  christos 
    185  1.168        pk 	s = splsoftclock();
    186  1.155  christos 	simple_lock(&p->p_sigctx.ps_silock);
    187  1.155  christos 	while (!CIRCLEQ_EMPTY(&p->p_sigctx.ps_siginfo)) {
    188  1.155  christos 		ksiginfo_t *ksi = CIRCLEQ_FIRST(&p->p_sigctx.ps_siginfo);
    189  1.155  christos 		CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
    190  1.152  christos 		pool_put(&ksiginfo_pool, ksi);
    191  1.152  christos 	}
    192  1.155  christos 	simple_unlock(&p->p_sigctx.ps_silock);
    193  1.168        pk 	splx(s);
    194  1.152  christos }
    195  1.152  christos 
    196  1.152  christos /*
    197   1.89   thorpej  * Initialize signal-related data structures.
    198   1.89   thorpej  */
    199   1.89   thorpej void
    200  1.112     lukem signal_init(void)
    201   1.89   thorpej {
    202   1.89   thorpej 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
    203  1.120   thorpej 	    &pool_allocator_nointr);
    204  1.130   thorpej 	pool_init(&siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
    205  1.130   thorpej 	    &pool_allocator_nointr);
    206  1.152  christos 	pool_init(&ksiginfo_pool, sizeof(ksiginfo_t), 0, 0, 0, "ksiginfo",
    207  1.152  christos 	    NULL);
    208  1.152  christos 	exithook_establish(ksiginfo_exithook, NULL);
    209  1.152  christos 	exechook_establish(ksiginfo_exithook, NULL);
    210   1.89   thorpej }
    211   1.89   thorpej 
    212   1.89   thorpej /*
    213  1.109  jdolecek  * Create an initial sigctx structure, using the same signal state
    214  1.109  jdolecek  * as p. If 'share' is set, share the sigctx_proc part, otherwise just
    215  1.109  jdolecek  * copy it from parent.
    216   1.89   thorpej  */
    217  1.109  jdolecek void
    218  1.112     lukem sigactsinit(struct proc *np, struct proc *pp, int share)
    219   1.89   thorpej {
    220   1.89   thorpej 	struct sigacts *ps;
    221   1.89   thorpej 
    222  1.109  jdolecek 	if (share) {
    223  1.109  jdolecek 		np->p_sigacts = pp->p_sigacts;
    224  1.109  jdolecek 		pp->p_sigacts->sa_refcnt++;
    225  1.109  jdolecek 	} else {
    226  1.109  jdolecek 		ps = pool_get(&sigacts_pool, PR_WAITOK);
    227  1.109  jdolecek 		if (pp)
    228  1.109  jdolecek 			memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
    229  1.109  jdolecek 		else
    230  1.109  jdolecek 			memset(ps, '\0', sizeof(struct sigacts));
    231  1.109  jdolecek 		ps->sa_refcnt = 1;
    232  1.109  jdolecek 		np->p_sigacts = ps;
    233  1.109  jdolecek 	}
    234   1.89   thorpej }
    235   1.89   thorpej 
    236   1.89   thorpej /*
    237  1.109  jdolecek  * Make this process not share its sigctx, maintaining all
    238   1.89   thorpej  * signal state.
    239   1.89   thorpej  */
    240   1.89   thorpej void
    241  1.112     lukem sigactsunshare(struct proc *p)
    242   1.89   thorpej {
    243  1.109  jdolecek 	struct sigacts *oldps;
    244   1.89   thorpej 
    245  1.109  jdolecek 	if (p->p_sigacts->sa_refcnt == 1)
    246   1.89   thorpej 		return;
    247   1.89   thorpej 
    248  1.109  jdolecek 	oldps = p->p_sigacts;
    249  1.109  jdolecek 	sigactsinit(p, NULL, 0);
    250  1.109  jdolecek 
    251  1.109  jdolecek 	if (--oldps->sa_refcnt == 0)
    252  1.109  jdolecek 		pool_put(&sigacts_pool, oldps);
    253   1.89   thorpej }
    254   1.89   thorpej 
    255   1.89   thorpej /*
    256  1.109  jdolecek  * Release a sigctx structure.
    257   1.89   thorpej  */
    258   1.89   thorpej void
    259  1.112     lukem sigactsfree(struct proc *p)
    260   1.89   thorpej {
    261  1.112     lukem 	struct sigacts *ps;
    262   1.89   thorpej 
    263  1.112     lukem 	ps = p->p_sigacts;
    264  1.109  jdolecek 	if (--ps->sa_refcnt > 0)
    265   1.89   thorpej 		return;
    266   1.89   thorpej 
    267   1.89   thorpej 	pool_put(&sigacts_pool, ps);
    268   1.89   thorpej }
    269   1.89   thorpej 
    270   1.79   mycroft int
    271  1.112     lukem sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
    272  1.162      matt 	struct sigaction *osa, const void *tramp, int vers)
    273   1.79   mycroft {
    274  1.112     lukem 	struct sigacts	*ps;
    275  1.112     lukem 	int		prop;
    276   1.79   mycroft 
    277  1.112     lukem 	ps = p->p_sigacts;
    278   1.79   mycroft 	if (signum <= 0 || signum >= NSIG)
    279   1.79   mycroft 		return (EINVAL);
    280   1.79   mycroft 
    281  1.121   thorpej 	/*
    282  1.121   thorpej 	 * Trampoline ABI version 0 is reserved for the legacy
    283  1.162      matt 	 * kernel-provided on-stack trampoline.  Conversely, if we are
    284  1.162      matt 	 * using a non-0 ABI version, we must have a trampoline.  Only
    285  1.163  christos 	 * validate the vers if a new sigaction was supplied. Emulations
    286  1.163  christos 	 * use legacy kernel trampolines with version 0, alternatively
    287  1.163  christos 	 * check for that too.
    288  1.121   thorpej 	 */
    289  1.121   thorpej 	if ((vers != 0 && tramp == NULL) ||
    290  1.161      matt #ifdef SIGTRAMP_VALID
    291  1.163  christos 	    (nsa != NULL &&
    292  1.163  christos 	    ((vers == 0) ?
    293  1.163  christos 		(p->p_emul->e_sigcode == NULL) :
    294  1.163  christos 		!SIGTRAMP_VALID(vers))) ||
    295  1.161      matt #endif
    296  1.121   thorpej 	    (vers == 0 && tramp != NULL))
    297  1.121   thorpej 		return (EINVAL);
    298  1.121   thorpej 
    299   1.79   mycroft 	if (osa)
    300  1.109  jdolecek 		*osa = SIGACTION_PS(ps, signum);
    301   1.79   mycroft 
    302   1.79   mycroft 	if (nsa) {
    303   1.79   mycroft 		if (nsa->sa_flags & ~SA_ALLBITS)
    304   1.79   mycroft 			return (EINVAL);
    305  1.149    kleink 
    306  1.149    kleink #ifndef __HAVE_SIGINFO
    307  1.149    kleink 		if (nsa->sa_flags & SA_SIGINFO)
    308  1.149    kleink 			return (EINVAL);
    309  1.149    kleink #endif
    310   1.79   mycroft 
    311   1.79   mycroft 		prop = sigprop[signum];
    312   1.79   mycroft 		if (prop & SA_CANTMASK)
    313   1.79   mycroft 			return (EINVAL);
    314   1.79   mycroft 
    315  1.105   thorpej 		(void) splsched();	/* XXXSMP */
    316  1.109  jdolecek 		SIGACTION_PS(ps, signum) = *nsa;
    317  1.121   thorpej 		ps->sa_sigdesc[signum].sd_tramp = tramp;
    318  1.121   thorpej 		ps->sa_sigdesc[signum].sd_vers = vers;
    319  1.109  jdolecek 		sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
    320   1.79   mycroft 		if ((prop & SA_NORESET) != 0)
    321  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
    322   1.79   mycroft 		if (signum == SIGCHLD) {
    323   1.79   mycroft 			if (nsa->sa_flags & SA_NOCLDSTOP)
    324   1.79   mycroft 				p->p_flag |= P_NOCLDSTOP;
    325   1.79   mycroft 			else
    326   1.79   mycroft 				p->p_flag &= ~P_NOCLDSTOP;
    327   1.82     enami 			if (nsa->sa_flags & SA_NOCLDWAIT) {
    328   1.81  christos 				/*
    329   1.81  christos 				 * Paranoia: since SA_NOCLDWAIT is implemented
    330   1.81  christos 				 * by reparenting the dying child to PID 1 (and
    331  1.112     lukem 				 * trust it to reap the zombie), PID 1 itself
    332  1.112     lukem 				 * is forbidden to set SA_NOCLDWAIT.
    333   1.81  christos 				 */
    334   1.81  christos 				if (p->p_pid == 1)
    335   1.81  christos 					p->p_flag &= ~P_NOCLDWAIT;
    336   1.81  christos 				else
    337   1.81  christos 					p->p_flag |= P_NOCLDWAIT;
    338   1.81  christos 			} else
    339   1.81  christos 				p->p_flag &= ~P_NOCLDWAIT;
    340   1.79   mycroft 		}
    341   1.79   mycroft 		if ((nsa->sa_flags & SA_NODEFER) == 0)
    342  1.109  jdolecek 			sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    343   1.79   mycroft 		else
    344  1.109  jdolecek 			sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    345   1.79   mycroft 		/*
    346  1.112     lukem 	 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
    347  1.112     lukem 		 * SIG_IGN, and for signals set to SIG_DFL where the default is
    348  1.112     lukem 		 * to ignore. However, don't put SIGCONT in
    349  1.112     lukem 		 * p_sigctx.ps_sigignore, as we have to restart the process.
    350  1.112     lukem 	 	 */
    351   1.79   mycroft 		if (nsa->sa_handler == SIG_IGN ||
    352   1.79   mycroft 		    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
    353  1.112     lukem 						/* never to be seen again */
    354  1.112     lukem 			sigdelset(&p->p_sigctx.ps_siglist, signum);
    355  1.112     lukem 			if (signum != SIGCONT) {
    356  1.112     lukem 						/* easier in psignal */
    357  1.112     lukem 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    358  1.112     lukem 			}
    359  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    360   1.79   mycroft 		} else {
    361  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigignore, signum);
    362   1.79   mycroft 			if (nsa->sa_handler == SIG_DFL)
    363  1.109  jdolecek 				sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    364   1.79   mycroft 			else
    365  1.109  jdolecek 				sigaddset(&p->p_sigctx.ps_sigcatch, signum);
    366   1.79   mycroft 		}
    367   1.79   mycroft 		(void) spl0();
    368   1.79   mycroft 	}
    369   1.79   mycroft 
    370   1.79   mycroft 	return (0);
    371   1.79   mycroft }
    372   1.79   mycroft 
    373  1.158  christos #ifdef COMPAT_16
    374   1.29       cgd /* ARGSUSED */
    375   1.52  christos int
    376  1.158  christos compat_16_sys___sigaction14(struct lwp *l, void *v, register_t *retval)
    377   1.48   thorpej {
    378  1.159   nathanw 	struct compat_16_sys___sigaction14_args /* {
    379  1.112     lukem 		syscallarg(int)				signum;
    380  1.112     lukem 		syscallarg(const struct sigaction *)	nsa;
    381  1.112     lukem 		syscallarg(struct sigaction *)		osa;
    382   1.48   thorpej 	} */ *uap = v;
    383  1.130   thorpej 	struct proc		*p;
    384  1.112     lukem 	struct sigaction	nsa, osa;
    385  1.112     lukem 	int			error;
    386   1.29       cgd 
    387   1.79   mycroft 	if (SCARG(uap, nsa)) {
    388   1.79   mycroft 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    389   1.52  christos 		if (error)
    390   1.29       cgd 			return (error);
    391   1.29       cgd 	}
    392  1.130   thorpej 	p = l->l_proc;
    393   1.79   mycroft 	error = sigaction1(p, SCARG(uap, signum),
    394  1.121   thorpej 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    395  1.121   thorpej 	    NULL, 0);
    396  1.121   thorpej 	if (error)
    397  1.121   thorpej 		return (error);
    398  1.121   thorpej 	if (SCARG(uap, osa)) {
    399  1.121   thorpej 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    400  1.121   thorpej 		if (error)
    401  1.121   thorpej 			return (error);
    402  1.121   thorpej 	}
    403  1.121   thorpej 	return (0);
    404  1.121   thorpej }
    405  1.158  christos #endif
    406  1.121   thorpej 
    407  1.121   thorpej /* ARGSUSED */
    408  1.121   thorpej int
    409  1.130   thorpej sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
    410  1.121   thorpej {
    411  1.121   thorpej 	struct sys___sigaction_sigtramp_args /* {
    412  1.121   thorpej 		syscallarg(int)				signum;
    413  1.121   thorpej 		syscallarg(const struct sigaction *)	nsa;
    414  1.121   thorpej 		syscallarg(struct sigaction *)		osa;
    415  1.121   thorpej 		syscallarg(void *)			tramp;
    416  1.121   thorpej 		syscallarg(int)				vers;
    417  1.121   thorpej 	} */ *uap = v;
    418  1.130   thorpej 	struct proc *p = l->l_proc;
    419  1.121   thorpej 	struct sigaction nsa, osa;
    420  1.121   thorpej 	int error;
    421  1.121   thorpej 
    422  1.121   thorpej 	if (SCARG(uap, nsa)) {
    423  1.121   thorpej 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
    424  1.121   thorpej 		if (error)
    425  1.121   thorpej 			return (error);
    426  1.121   thorpej 	}
    427  1.121   thorpej 	error = sigaction1(p, SCARG(uap, signum),
    428  1.121   thorpej 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    429  1.121   thorpej 	    SCARG(uap, tramp), SCARG(uap, vers));
    430   1.79   mycroft 	if (error)
    431   1.79   mycroft 		return (error);
    432   1.79   mycroft 	if (SCARG(uap, osa)) {
    433   1.79   mycroft 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
    434   1.52  christos 		if (error)
    435   1.29       cgd 			return (error);
    436   1.29       cgd 	}
    437   1.29       cgd 	return (0);
    438   1.29       cgd }
    439   1.29       cgd 
    440   1.29       cgd /*
    441   1.29       cgd  * Initialize signal state for process 0;
    442   1.79   mycroft  * set to ignore signals that are ignored by default and disable the signal
    443   1.79   mycroft  * stack.
    444   1.29       cgd  */
    445   1.29       cgd void
    446  1.112     lukem siginit(struct proc *p)
    447   1.29       cgd {
    448  1.112     lukem 	struct sigacts	*ps;
    449  1.112     lukem 	int		signum, prop;
    450   1.79   mycroft 
    451  1.112     lukem 	ps = p->p_sigacts;
    452   1.79   mycroft 	sigemptyset(&contsigmask);
    453   1.79   mycroft 	sigemptyset(&stopsigmask);
    454   1.79   mycroft 	sigemptyset(&sigcantmask);
    455   1.85   mycroft 	for (signum = 1; signum < NSIG; signum++) {
    456   1.79   mycroft 		prop = sigprop[signum];
    457   1.79   mycroft 		if (prop & SA_CONT)
    458   1.79   mycroft 			sigaddset(&contsigmask, signum);
    459   1.79   mycroft 		if (prop & SA_STOP)
    460   1.79   mycroft 			sigaddset(&stopsigmask, signum);
    461   1.79   mycroft 		if (prop & SA_CANTMASK)
    462   1.79   mycroft 			sigaddset(&sigcantmask, signum);
    463   1.79   mycroft 		if (prop & SA_IGNORE && signum != SIGCONT)
    464  1.109  jdolecek 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
    465  1.109  jdolecek 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    466  1.109  jdolecek 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    467   1.79   mycroft 	}
    468  1.109  jdolecek 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    469  1.171  jdolecek 	p->p_sigctx.ps_sigwaited = NULL;
    470   1.79   mycroft 	p->p_flag &= ~P_NOCLDSTOP;
    471   1.29       cgd 
    472   1.79   mycroft 	/*
    473   1.79   mycroft 	 * Reset stack state to the user stack.
    474   1.79   mycroft 	 */
    475  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    476  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_size = 0;
    477  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    478   1.89   thorpej 
    479   1.89   thorpej 	/* One reference. */
    480  1.109  jdolecek 	ps->sa_refcnt = 1;
    481   1.29       cgd }
    482   1.29       cgd 
    483   1.29       cgd /*
    484   1.29       cgd  * Reset signals for an exec of the specified process.
    485   1.29       cgd  */
    486   1.29       cgd void
    487  1.112     lukem execsigs(struct proc *p)
    488   1.29       cgd {
    489  1.112     lukem 	struct sigacts	*ps;
    490  1.112     lukem 	int		signum, prop;
    491   1.29       cgd 
    492  1.115   thorpej 	sigactsunshare(p);
    493  1.115   thorpej 
    494  1.112     lukem 	ps = p->p_sigacts;
    495  1.115   thorpej 
    496   1.29       cgd 	/*
    497   1.29       cgd 	 * Reset caught signals.  Held signals remain held
    498  1.109  jdolecek 	 * through p_sigctx.ps_sigmask (unless they were caught,
    499   1.29       cgd 	 * and are now ignored by default).
    500   1.29       cgd 	 */
    501   1.85   mycroft 	for (signum = 1; signum < NSIG; signum++) {
    502  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
    503   1.79   mycroft 			prop = sigprop[signum];
    504   1.79   mycroft 			if (prop & SA_IGNORE) {
    505   1.79   mycroft 				if ((prop & SA_CONT) == 0)
    506  1.112     lukem 					sigaddset(&p->p_sigctx.ps_sigignore,
    507  1.112     lukem 					    signum);
    508  1.109  jdolecek 				sigdelset(&p->p_sigctx.ps_siglist, signum);
    509   1.79   mycroft 			}
    510  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    511   1.29       cgd 		}
    512  1.109  jdolecek 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
    513  1.109  jdolecek 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
    514   1.29       cgd 	}
    515  1.109  jdolecek 	sigemptyset(&p->p_sigctx.ps_sigcatch);
    516  1.171  jdolecek 	p->p_sigctx.ps_sigwaited = NULL;
    517   1.79   mycroft 	p->p_flag &= ~P_NOCLDSTOP;
    518   1.79   mycroft 
    519   1.29       cgd 	/*
    520   1.29       cgd 	 * Reset stack state to the user stack.
    521   1.29       cgd 	 */
    522  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
    523  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_size = 0;
    524  1.109  jdolecek 	p->p_sigctx.ps_sigstk.ss_sp = 0;
    525   1.29       cgd }
    526   1.29       cgd 
    527   1.79   mycroft int
    528  1.112     lukem sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
    529   1.79   mycroft {
    530   1.79   mycroft 
    531   1.79   mycroft 	if (oss)
    532  1.109  jdolecek 		*oss = p->p_sigctx.ps_sigmask;
    533   1.79   mycroft 
    534   1.79   mycroft 	if (nss) {
    535  1.105   thorpej 		(void)splsched();	/* XXXSMP */
    536   1.79   mycroft 		switch (how) {
    537   1.79   mycroft 		case SIG_BLOCK:
    538  1.109  jdolecek 			sigplusset(nss, &p->p_sigctx.ps_sigmask);
    539   1.79   mycroft 			break;
    540   1.79   mycroft 		case SIG_UNBLOCK:
    541  1.109  jdolecek 			sigminusset(nss, &p->p_sigctx.ps_sigmask);
    542  1.110   thorpej 			CHECKSIGS(p);
    543   1.79   mycroft 			break;
    544   1.79   mycroft 		case SIG_SETMASK:
    545  1.109  jdolecek 			p->p_sigctx.ps_sigmask = *nss;
    546  1.110   thorpej 			CHECKSIGS(p);
    547   1.79   mycroft 			break;
    548   1.79   mycroft 		default:
    549  1.104   thorpej 			(void)spl0();	/* XXXSMP */
    550   1.79   mycroft 			return (EINVAL);
    551   1.79   mycroft 		}
    552  1.109  jdolecek 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    553  1.104   thorpej 		(void)spl0();		/* XXXSMP */
    554   1.79   mycroft 	}
    555   1.79   mycroft 
    556   1.79   mycroft 	return (0);
    557   1.79   mycroft }
    558   1.79   mycroft 
    559   1.29       cgd /*
    560   1.29       cgd  * Manipulate signal mask.
    561   1.29       cgd  * Note that we receive new mask, not pointer,
    562   1.29       cgd  * and return old mask as return value;
    563   1.29       cgd  * the library stub does the rest.
    564   1.29       cgd  */
    565   1.52  christos int
    566  1.130   thorpej sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
    567   1.48   thorpej {
    568   1.79   mycroft 	struct sys___sigprocmask14_args /* {
    569  1.112     lukem 		syscallarg(int)			how;
    570  1.112     lukem 		syscallarg(const sigset_t *)	set;
    571  1.112     lukem 		syscallarg(sigset_t *)		oset;
    572   1.48   thorpej 	} */ *uap = v;
    573  1.130   thorpej 	struct proc	*p;
    574  1.112     lukem 	sigset_t	nss, oss;
    575  1.112     lukem 	int		error;
    576   1.29       cgd 
    577   1.79   mycroft 	if (SCARG(uap, set)) {
    578   1.79   mycroft 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
    579   1.79   mycroft 		if (error)
    580   1.79   mycroft 			return (error);
    581   1.79   mycroft 	}
    582  1.130   thorpej 	p = l->l_proc;
    583   1.79   mycroft 	error = sigprocmask1(p, SCARG(uap, how),
    584   1.79   mycroft 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
    585   1.79   mycroft 	if (error)
    586   1.79   mycroft 		return (error);
    587   1.79   mycroft 	if (SCARG(uap, oset)) {
    588   1.79   mycroft 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
    589   1.79   mycroft 		if (error)
    590   1.79   mycroft 			return (error);
    591   1.79   mycroft 	}
    592   1.79   mycroft 	return (0);
    593   1.79   mycroft }
    594   1.79   mycroft 
    595   1.79   mycroft void
    596  1.112     lukem sigpending1(struct proc *p, sigset_t *ss)
    597   1.79   mycroft {
    598   1.29       cgd 
    599  1.109  jdolecek 	*ss = p->p_sigctx.ps_siglist;
    600  1.109  jdolecek 	sigminusset(&p->p_sigctx.ps_sigmask, ss);
    601   1.29       cgd }
    602   1.29       cgd 
    603   1.29       cgd /* ARGSUSED */
    604   1.52  christos int
    605  1.130   thorpej sys___sigpending14(struct lwp *l, void *v, register_t *retval)
    606   1.29       cgd {
    607   1.98  augustss 	struct sys___sigpending14_args /* {
    608  1.112     lukem 		syscallarg(sigset_t *)	set;
    609   1.79   mycroft 	} */ *uap = v;
    610  1.130   thorpej 	struct proc	*p;
    611  1.130   thorpej 	sigset_t	ss;
    612   1.79   mycroft 
    613  1.130   thorpej 	p = l->l_proc;
    614   1.79   mycroft 	sigpending1(p, &ss);
    615   1.79   mycroft 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
    616   1.79   mycroft }
    617   1.79   mycroft 
    618   1.79   mycroft int
    619  1.112     lukem sigsuspend1(struct proc *p, const sigset_t *ss)
    620   1.79   mycroft {
    621  1.112     lukem 	struct sigacts *ps;
    622   1.29       cgd 
    623  1.112     lukem 	ps = p->p_sigacts;
    624   1.79   mycroft 	if (ss) {
    625   1.79   mycroft 		/*
    626   1.79   mycroft 		 * When returning from sigpause, we want
    627   1.79   mycroft 		 * the old mask to be restored after the
    628   1.79   mycroft 		 * signal handler has finished.  Thus, we
    629  1.109  jdolecek 		 * save it here and mark the sigctx structure
    630   1.79   mycroft 		 * to indicate this.
    631   1.79   mycroft 		 */
    632  1.109  jdolecek 		p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
    633  1.109  jdolecek 		p->p_sigctx.ps_flags |= SAS_OLDMASK;
    634  1.105   thorpej 		(void) splsched();	/* XXXSMP */
    635  1.109  jdolecek 		p->p_sigctx.ps_sigmask = *ss;
    636  1.110   thorpej 		CHECKSIGS(p);
    637  1.109  jdolecek 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
    638  1.104   thorpej 		(void) spl0();		/* XXXSMP */
    639   1.79   mycroft 	}
    640   1.79   mycroft 
    641   1.79   mycroft 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
    642  1.145   nathanw 		/* void */;
    643  1.144      fvdl 
    644   1.79   mycroft 	/* always return EINTR rather than ERESTART... */
    645   1.79   mycroft 	return (EINTR);
    646   1.29       cgd }
    647   1.29       cgd 
    648   1.29       cgd /*
    649   1.29       cgd  * Suspend process until signal, providing mask to be set
    650   1.29       cgd  * in the meantime.  Note nonstandard calling convention:
    651   1.29       cgd  * libc stub passes mask, not pointer, to save a copyin.
    652   1.29       cgd  */
    653   1.29       cgd /* ARGSUSED */
    654   1.29       cgd int
    655  1.130   thorpej sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
    656   1.48   thorpej {
    657   1.79   mycroft 	struct sys___sigsuspend14_args /* {
    658  1.112     lukem 		syscallarg(const sigset_t *)	set;
    659   1.48   thorpej 	} */ *uap = v;
    660  1.130   thorpej 	struct proc	*p;
    661  1.112     lukem 	sigset_t	ss;
    662  1.112     lukem 	int		error;
    663   1.79   mycroft 
    664   1.79   mycroft 	if (SCARG(uap, set)) {
    665   1.79   mycroft 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
    666   1.79   mycroft 		if (error)
    667   1.79   mycroft 			return (error);
    668   1.79   mycroft 	}
    669   1.79   mycroft 
    670  1.130   thorpej 	p = l->l_proc;
    671   1.79   mycroft 	return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
    672   1.79   mycroft }
    673   1.79   mycroft 
    674   1.79   mycroft int
    675  1.112     lukem sigaltstack1(struct proc *p, const struct sigaltstack *nss,
    676  1.112     lukem 	struct sigaltstack *oss)
    677   1.79   mycroft {
    678  1.112     lukem 
    679   1.79   mycroft 	if (oss)
    680  1.109  jdolecek 		*oss = p->p_sigctx.ps_sigstk;
    681   1.79   mycroft 
    682   1.79   mycroft 	if (nss) {
    683   1.79   mycroft 		if (nss->ss_flags & ~SS_ALLBITS)
    684   1.79   mycroft 			return (EINVAL);
    685   1.79   mycroft 
    686   1.79   mycroft 		if (nss->ss_flags & SS_DISABLE) {
    687  1.109  jdolecek 			if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
    688   1.79   mycroft 				return (EINVAL);
    689   1.79   mycroft 		} else {
    690   1.79   mycroft 			if (nss->ss_size < MINSIGSTKSZ)
    691   1.79   mycroft 				return (ENOMEM);
    692   1.79   mycroft 		}
    693  1.109  jdolecek 		p->p_sigctx.ps_sigstk = *nss;
    694   1.79   mycroft 	}
    695   1.79   mycroft 
    696   1.79   mycroft 	return (0);
    697   1.29       cgd }
    698   1.29       cgd 
    699   1.29       cgd /* ARGSUSED */
    700   1.52  christos int
    701  1.130   thorpej sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
    702   1.48   thorpej {
    703   1.98  augustss 	struct sys___sigaltstack14_args /* {
    704  1.112     lukem 		syscallarg(const struct sigaltstack *)	nss;
    705  1.112     lukem 		syscallarg(struct sigaltstack *)	oss;
    706   1.48   thorpej 	} */ *uap = v;
    707  1.130   thorpej 	struct proc		*p;
    708  1.112     lukem 	struct sigaltstack	nss, oss;
    709  1.112     lukem 	int			error;
    710   1.29       cgd 
    711   1.79   mycroft 	if (SCARG(uap, nss)) {
    712   1.79   mycroft 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    713   1.79   mycroft 		if (error)
    714   1.79   mycroft 			return (error);
    715   1.79   mycroft 	}
    716  1.130   thorpej 	p = l->l_proc;
    717   1.79   mycroft 	error = sigaltstack1(p,
    718   1.79   mycroft 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    719   1.52  christos 	if (error)
    720   1.29       cgd 		return (error);
    721   1.79   mycroft 	if (SCARG(uap, oss)) {
    722   1.79   mycroft 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    723   1.79   mycroft 		if (error)
    724   1.79   mycroft 			return (error);
    725   1.29       cgd 	}
    726   1.29       cgd 	return (0);
    727   1.29       cgd }
    728   1.29       cgd 
    729   1.29       cgd /* ARGSUSED */
    730   1.29       cgd int
    731  1.130   thorpej sys_kill(struct lwp *l, void *v, register_t *retval)
    732   1.48   thorpej {
    733   1.98  augustss 	struct sys_kill_args /* {
    734  1.112     lukem 		syscallarg(int)	pid;
    735  1.112     lukem 		syscallarg(int)	signum;
    736   1.48   thorpej 	} */ *uap = v;
    737  1.130   thorpej 	struct proc	*cp, *p;
    738  1.112     lukem 	struct pcred	*pc;
    739  1.148  christos 	ksiginfo_t	ksi;
    740   1.29       cgd 
    741  1.130   thorpej 	cp = l->l_proc;
    742  1.112     lukem 	pc = cp->p_cred;
    743   1.32       cgd 	if ((u_int)SCARG(uap, signum) >= NSIG)
    744   1.29       cgd 		return (EINVAL);
    745  1.148  christos 	memset(&ksi, 0, sizeof(ksi));
    746  1.148  christos 	ksi.ksi_signo = SCARG(uap, signum);
    747  1.148  christos 	ksi.ksi_code = SI_USER;
    748  1.148  christos 	ksi.ksi_pid = cp->p_pid;
    749  1.148  christos 	ksi.ksi_uid = cp->p_ucred->cr_uid;
    750   1.32       cgd 	if (SCARG(uap, pid) > 0) {
    751   1.29       cgd 		/* kill single process */
    752   1.32       cgd 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    753   1.29       cgd 			return (ESRCH);
    754   1.32       cgd 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
    755   1.29       cgd 			return (EPERM);
    756   1.32       cgd 		if (SCARG(uap, signum))
    757  1.160  christos 			kpsignal2(p, &ksi, 1);
    758   1.29       cgd 		return (0);
    759   1.29       cgd 	}
    760   1.32       cgd 	switch (SCARG(uap, pid)) {
    761   1.29       cgd 	case -1:		/* broadcast signal */
    762  1.148  christos 		return (killpg1(cp, &ksi, 0, 1));
    763   1.29       cgd 	case 0:			/* signal own process group */
    764  1.148  christos 		return (killpg1(cp, &ksi, 0, 0));
    765   1.29       cgd 	default:		/* negative explicit process group */
    766  1.148  christos 		return (killpg1(cp, &ksi, -SCARG(uap, pid), 0));
    767   1.29       cgd 	}
    768   1.29       cgd 	/* NOTREACHED */
    769   1.29       cgd }
    770   1.29       cgd 
    771   1.29       cgd /*
    772   1.29       cgd  * Common code for kill process group/broadcast kill.
    773   1.29       cgd  * cp is calling process.
    774   1.29       cgd  */
    775   1.52  christos int
    776  1.148  christos killpg1(struct proc *cp, ksiginfo_t *ksi, int pgid, int all)
    777   1.29       cgd {
    778  1.112     lukem 	struct proc	*p;
    779  1.112     lukem 	struct pcred	*pc;
    780  1.112     lukem 	struct pgrp	*pgrp;
    781  1.112     lukem 	int		nfound;
    782  1.148  christos 	int		signum = ksi->ksi_signo;
    783   1.29       cgd 
    784  1.112     lukem 	pc = cp->p_cred;
    785  1.112     lukem 	nfound = 0;
    786   1.91   thorpej 	if (all) {
    787   1.29       cgd 		/*
    788   1.29       cgd 		 * broadcast
    789   1.29       cgd 		 */
    790   1.92   thorpej 		proclist_lock_read();
    791  1.124      matt 		LIST_FOREACH(p, &allproc, p_list) {
    792   1.29       cgd 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    793   1.29       cgd 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
    794   1.29       cgd 				continue;
    795   1.29       cgd 			nfound++;
    796   1.29       cgd 			if (signum)
    797  1.160  christos 				kpsignal2(p, ksi, 1);
    798   1.29       cgd 		}
    799   1.91   thorpej 		proclist_unlock_read();
    800   1.91   thorpej 	} else {
    801   1.29       cgd 		if (pgid == 0)
    802   1.29       cgd 			/*
    803   1.29       cgd 			 * zero pgid means send to my process group.
    804   1.29       cgd 			 */
    805   1.29       cgd 			pgrp = cp->p_pgrp;
    806   1.29       cgd 		else {
    807   1.29       cgd 			pgrp = pgfind(pgid);
    808   1.29       cgd 			if (pgrp == NULL)
    809   1.29       cgd 				return (ESRCH);
    810   1.29       cgd 		}
    811  1.124      matt 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
    812   1.29       cgd 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
    813   1.29       cgd 			    !CANSIGNAL(cp, pc, p, signum))
    814   1.29       cgd 				continue;
    815   1.29       cgd 			nfound++;
    816   1.90   thorpej 			if (signum && P_ZOMBIE(p) == 0)
    817  1.160  christos 				kpsignal2(p, ksi, 1);
    818   1.29       cgd 		}
    819   1.29       cgd 	}
    820   1.29       cgd 	return (nfound ? 0 : ESRCH);
    821   1.29       cgd }
    822   1.29       cgd 
    823   1.29       cgd /*
    824   1.29       cgd  * Send a signal to a process group.
    825   1.29       cgd  */
    826   1.29       cgd void
    827  1.112     lukem gsignal(int pgid, int signum)
    828   1.29       cgd {
    829  1.148  christos 	ksiginfo_t ksi;
    830  1.148  christos 	memset(&ksi, 0, sizeof(ksi));
    831  1.148  christos 	ksi.ksi_signo = signum;
    832  1.148  christos 	kgsignal(pgid, &ksi, NULL);
    833  1.148  christos }
    834  1.148  christos 
    835  1.148  christos void
    836  1.148  christos kgsignal(int pgid, ksiginfo_t *ksi, void *data)
    837  1.148  christos {
    838   1.29       cgd 	struct pgrp *pgrp;
    839   1.29       cgd 
    840   1.29       cgd 	if (pgid && (pgrp = pgfind(pgid)))
    841  1.148  christos 		kpgsignal(pgrp, ksi, data, 0);
    842   1.29       cgd }
    843   1.29       cgd 
    844   1.29       cgd /*
    845   1.71      fvdl  * Send a signal to a process group. If checktty is 1,
    846   1.29       cgd  * limit to members which have a controlling terminal.
    847   1.29       cgd  */
    848   1.29       cgd void
    849  1.148  christos pgsignal(struct pgrp *pgrp, int sig, int checkctty)
    850  1.148  christos {
    851  1.148  christos 	ksiginfo_t ksi;
    852  1.148  christos 	memset(&ksi, 0, sizeof(ksi));
    853  1.148  christos 	ksi.ksi_signo = sig;
    854  1.148  christos 	kpgsignal(pgrp, &ksi, NULL, checkctty);
    855  1.148  christos }
    856  1.148  christos 
    857  1.148  christos void
    858  1.148  christos kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
    859   1.29       cgd {
    860   1.98  augustss 	struct proc *p;
    861   1.29       cgd 
    862   1.29       cgd 	if (pgrp)
    863  1.124      matt 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
    864   1.29       cgd 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
    865  1.148  christos 				kpsignal(p, ksi, data);
    866   1.29       cgd }
    867   1.29       cgd 
    868   1.29       cgd /*
    869   1.29       cgd  * Send a signal caused by a trap to the current process.
    870   1.29       cgd  * If it will be caught immediately, deliver it with correct code.
    871   1.29       cgd  * Otherwise, post it normally.
    872   1.29       cgd  */
    873  1.148  christos #ifndef __HAVE_SIGINFO
    874  1.160  christos void _trapsignal(struct lwp *, const ksiginfo_t *);
    875   1.29       cgd void
    876  1.130   thorpej trapsignal(struct lwp *l, int signum, u_long code)
    877   1.29       cgd {
    878  1.148  christos #define trapsignal _trapsignal
    879  1.148  christos 	ksiginfo_t ksi;
    880  1.166   thorpej 
    881  1.166   thorpej 	KSI_INIT_TRAP(&ksi);
    882  1.148  christos 	ksi.ksi_signo = signum;
    883  1.148  christos 	ksi.ksi_trap = (int)code;
    884  1.148  christos 	trapsignal(l, &ksi);
    885  1.148  christos }
    886  1.148  christos #endif
    887  1.148  christos 
    888  1.148  christos void
    889  1.160  christos trapsignal(struct lwp *l, const ksiginfo_t *ksi)
    890  1.148  christos {
    891  1.130   thorpej 	struct proc	*p;
    892  1.130   thorpej 	struct sigacts	*ps;
    893  1.148  christos 	int signum = ksi->ksi_signo;
    894   1.29       cgd 
    895  1.166   thorpej 	KASSERT(KSI_TRAP_P(ksi));
    896  1.166   thorpej 
    897  1.130   thorpej 	p = l->l_proc;
    898  1.112     lukem 	ps = p->p_sigacts;
    899   1.79   mycroft 	if ((p->p_flag & P_TRACED) == 0 &&
    900  1.109  jdolecek 	    sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
    901  1.109  jdolecek 	    !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
    902   1.29       cgd 		p->p_stats->p_ru.ru_nsignals++;
    903   1.29       cgd #ifdef KTRACE
    904   1.29       cgd 		if (KTRPOINT(p, KTR_PSIG))
    905  1.148  christos 			ktrpsig(p, signum, SIGACTION_PS(ps, signum).sa_handler,
    906  1.157  christos 			    &p->p_sigctx.ps_sigmask, ksi);
    907   1.29       cgd #endif
    908  1.148  christos 		kpsendsig(l, ksi, &p->p_sigctx.ps_sigmask);
    909  1.105   thorpej 		(void) splsched();	/* XXXSMP */
    910  1.112     lukem 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
    911  1.112     lukem 		    &p->p_sigctx.ps_sigmask);
    912  1.109  jdolecek 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
    913  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    914   1.45   mycroft 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
    915  1.109  jdolecek 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
    916  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
    917   1.45   mycroft 		}
    918  1.104   thorpej 		(void) spl0();		/* XXXSMP */
    919   1.29       cgd 	} else {
    920  1.152  christos 		p->p_sigctx.ps_lwp = l->l_lid;
    921  1.148  christos 		/* XXX for core dump/debugger */
    922  1.152  christos 		p->p_sigctx.ps_signo = ksi->ksi_signo;
    923  1.152  christos 		p->p_sigctx.ps_code = ksi->ksi_trap;
    924  1.160  christos 		kpsignal2(p, ksi, 1);
    925   1.29       cgd 	}
    926   1.29       cgd }
    927   1.29       cgd 
    928   1.29       cgd /*
    929  1.151  christos  * Fill in signal information and signal the parent for a child status change.
    930  1.151  christos  */
    931  1.151  christos static void
    932  1.151  christos child_psignal(struct proc *p, int dolock)
    933  1.151  christos {
    934  1.151  christos 	ksiginfo_t ksi;
    935  1.151  christos 
    936  1.151  christos 	(void)memset(&ksi, 0, sizeof(ksi));
    937  1.151  christos 	ksi.ksi_signo = SIGCHLD;
    938  1.151  christos 	ksi.ksi_code = p->p_xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED;
    939  1.151  christos 	ksi.ksi_pid = p->p_pid;
    940  1.151  christos 	ksi.ksi_uid = p->p_ucred->cr_uid;
    941  1.151  christos 	ksi.ksi_status = p->p_xstat;
    942  1.151  christos 	ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
    943  1.151  christos 	ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
    944  1.160  christos 	kpsignal2(p->p_pptr, &ksi, dolock);
    945  1.151  christos }
    946  1.151  christos 
    947  1.151  christos /*
    948   1.29       cgd  * Send the signal to the process.  If the signal has an action, the action
    949   1.29       cgd  * is usually performed by the target process rather than the caller; we add
    950   1.29       cgd  * the signal to the set of pending signals for the process.
    951   1.29       cgd  *
    952   1.29       cgd  * Exceptions:
    953   1.29       cgd  *   o When a stop signal is sent to a sleeping process that takes the
    954   1.29       cgd  *     default action, the process is stopped without awakening it.
    955   1.29       cgd  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
    956   1.29       cgd  *     regardless of the signal action (eg, blocked or ignored).
    957   1.29       cgd  *
    958   1.29       cgd  * Other ignored signals are discarded immediately.
    959  1.104   thorpej  *
    960  1.104   thorpej  * XXXSMP: Invoked as psignal() or sched_psignal().
    961   1.29       cgd  */
    962   1.29       cgd void
    963  1.148  christos psignal1(struct proc *p, int signum, int dolock)
    964  1.148  christos {
    965  1.165   thorpej 	ksiginfo_t ksi;
    966  1.165   thorpej 
    967  1.165   thorpej 	memset(&ksi, 0, sizeof(ksi));
    968  1.165   thorpej 	ksi.ksi_signo = signum;
    969  1.165   thorpej 	kpsignal2(p, &ksi, dolock);
    970  1.148  christos }
    971  1.148  christos 
    972  1.148  christos void
    973  1.160  christos kpsignal1(struct proc *p, ksiginfo_t *ksi, void *data, int dolock)
    974  1.160  christos {
    975  1.165   thorpej 
    976  1.164  christos 	if ((p->p_flag & P_WEXIT) == 0 && data) {
    977  1.160  christos 		size_t fd;
    978  1.160  christos 		struct filedesc *fdp = p->p_fd;
    979  1.165   thorpej 
    980  1.160  christos 		ksi->ksi_fd = -1;
    981  1.160  christos 		for (fd = 0; fd < fdp->fd_nfiles; fd++) {
    982  1.160  christos 			struct file *fp = fdp->fd_ofiles[fd];
    983  1.160  christos 			/* XXX: lock? */
    984  1.160  christos 			if (fp && fp->f_data == data) {
    985  1.160  christos 				ksi->ksi_fd = fd;
    986  1.160  christos 				break;
    987  1.160  christos 			}
    988  1.160  christos 		}
    989  1.160  christos 	}
    990  1.160  christos 	kpsignal2(p, ksi, dolock);
    991  1.160  christos }
    992  1.160  christos 
    993  1.160  christos static void
    994  1.160  christos kpsignal2(struct proc *p, const ksiginfo_t *ksi, int dolock)
    995   1.29       cgd {
    996  1.170  christos 	struct lwp *l, *suspended = NULL;
    997  1.130   thorpej 	int	s = 0, prop, allsusp;
    998  1.112     lukem 	sig_t	action;
    999  1.148  christos 	int	signum = ksi->ksi_signo;
   1000   1.29       cgd 
   1001   1.79   mycroft #ifdef DIAGNOSTIC
   1002   1.79   mycroft 	if (signum <= 0 || signum >= NSIG)
   1003  1.148  christos 		panic("psignal signal number %d", signum);
   1004  1.148  christos 
   1005  1.104   thorpej 	/* XXXSMP: works, but icky */
   1006  1.104   thorpej 	if (dolock)
   1007  1.104   thorpej 		SCHED_ASSERT_UNLOCKED();
   1008  1.104   thorpej 	else
   1009  1.104   thorpej 		SCHED_ASSERT_LOCKED();
   1010   1.79   mycroft #endif
   1011  1.148  christos 
   1012  1.148  christos 
   1013  1.126  jdolecek 	/*
   1014  1.126  jdolecek 	 * Notify any interested parties in the signal.
   1015  1.126  jdolecek 	 */
   1016  1.126  jdolecek 	KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
   1017  1.126  jdolecek 
   1018   1.29       cgd 	prop = sigprop[signum];
   1019   1.29       cgd 
   1020   1.29       cgd 	/*
   1021   1.29       cgd 	 * If proc is traced, always give parent a chance.
   1022   1.29       cgd 	 */
   1023   1.29       cgd 	if (p->p_flag & P_TRACED)
   1024   1.29       cgd 		action = SIG_DFL;
   1025   1.29       cgd 	else {
   1026   1.29       cgd 		/*
   1027   1.29       cgd 		 * If the signal is being ignored,
   1028   1.29       cgd 		 * then we forget about it immediately.
   1029  1.109  jdolecek 		 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
   1030   1.29       cgd 		 * and if it is set to SIG_IGN,
   1031   1.29       cgd 		 * action will be SIG_DFL here.)
   1032   1.29       cgd 		 */
   1033  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigignore, signum))
   1034   1.29       cgd 			return;
   1035  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigmask, signum))
   1036   1.29       cgd 			action = SIG_HOLD;
   1037  1.109  jdolecek 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
   1038   1.29       cgd 			action = SIG_CATCH;
   1039   1.44   mycroft 		else {
   1040   1.29       cgd 			action = SIG_DFL;
   1041   1.44   mycroft 
   1042   1.44   mycroft 			if (prop & SA_KILL && p->p_nice > NZERO)
   1043   1.44   mycroft 				p->p_nice = NZERO;
   1044   1.44   mycroft 
   1045   1.44   mycroft 			/*
   1046   1.44   mycroft 			 * If sending a tty stop signal to a member of an
   1047   1.44   mycroft 			 * orphaned process group, discard the signal here if
   1048   1.44   mycroft 			 * the action is default; don't stop the process below
   1049   1.44   mycroft 			 * if sleeping, and don't clear any pending SIGCONT.
   1050   1.44   mycroft 			 */
   1051   1.44   mycroft 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
   1052   1.44   mycroft 				return;
   1053   1.44   mycroft 		}
   1054   1.29       cgd 	}
   1055   1.29       cgd 
   1056   1.29       cgd 	if (prop & SA_CONT)
   1057  1.109  jdolecek 		sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
   1058   1.29       cgd 
   1059   1.44   mycroft 	if (prop & SA_STOP)
   1060  1.109  jdolecek 		sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
   1061   1.44   mycroft 
   1062   1.29       cgd 	/*
   1063  1.135  jdolecek 	 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
   1064  1.171  jdolecek 	 * please!), check if anything waits on it. If yes, save the
   1065  1.171  jdolecek 	 * info into provided ps_sigwaited, and wake-up the waiter.
   1066  1.135  jdolecek 	 * The signal won't be processed further here.
   1067  1.135  jdolecek 	 */
   1068  1.135  jdolecek 	if ((prop & SA_CANTMASK) == 0
   1069  1.171  jdolecek 	    && p->p_sigctx.ps_sigwaited
   1070  1.171  jdolecek 	    && sigismember(p->p_sigctx.ps_sigwait, signum)
   1071  1.152  christos 	    && p->p_stat != SSTOP) {
   1072  1.171  jdolecek 		p->p_sigctx.ps_sigwaited->ksi_info = ksi->ksi_info;
   1073  1.171  jdolecek 		p->p_sigctx.ps_sigwaited = NULL;
   1074  1.135  jdolecek 		if (dolock)
   1075  1.135  jdolecek 			wakeup_one(&p->p_sigctx.ps_sigwait);
   1076  1.135  jdolecek 		else
   1077  1.135  jdolecek 			sched_wakeup(&p->p_sigctx.ps_sigwait);
   1078  1.135  jdolecek 		return;
   1079  1.135  jdolecek 	}
   1080  1.135  jdolecek 
   1081  1.171  jdolecek 	sigaddset(&p->p_sigctx.ps_siglist, signum);
   1082  1.171  jdolecek 
   1083  1.171  jdolecek 	/* CHECKSIGS() is "inlined" here. */
   1084  1.171  jdolecek 	p->p_sigctx.ps_sigcheck = 1;
   1085  1.171  jdolecek 
   1086  1.135  jdolecek 	/*
   1087   1.29       cgd 	 * Defer further processing for signals which are held,
   1088   1.29       cgd 	 * except that stopped processes must be continued by SIGCONT.
   1089   1.29       cgd 	 */
   1090  1.152  christos 	if (action == SIG_HOLD &&
   1091  1.152  christos 	    ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) {
   1092  1.155  christos 		ksiginfo_put(p, ksi);
   1093   1.29       cgd 		return;
   1094  1.152  christos 	}
   1095  1.104   thorpej 	/* XXXSMP: works, but icky */
   1096  1.104   thorpej 	if (dolock)
   1097  1.104   thorpej 		SCHED_LOCK(s);
   1098  1.104   thorpej 
   1099  1.144      fvdl 	/* XXXUPSXXX LWPs might go to sleep without passing signal handling */
   1100  1.147      fvdl 	if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)
   1101  1.147      fvdl 	    && !((p->p_flag & P_SA) && (p->p_sa->sa_idle != NULL))) {
   1102   1.29       cgd 		/*
   1103  1.130   thorpej 		 * At least one LWP is running or on a run queue.
   1104  1.130   thorpej 		 * The signal will be noticed when one of them returns
   1105  1.130   thorpej 		 * to userspace.
   1106   1.29       cgd 		 */
   1107  1.130   thorpej 		signotify(p);
   1108  1.130   thorpej 		/*
   1109  1.130   thorpej 		 * The signal will be noticed very soon.
   1110   1.29       cgd 		 */
   1111  1.130   thorpej 		goto out;
   1112  1.130   thorpej 	} else {
   1113  1.130   thorpej 		/* Process is sleeping or stopped */
   1114  1.130   thorpej 		if (p->p_flag & P_SA) {
   1115  1.144      fvdl 			struct lwp *l2 = p->p_sa->sa_vp;
   1116  1.144      fvdl 			l = NULL;
   1117  1.144      fvdl 			allsusp = 1;
   1118  1.144      fvdl 
   1119  1.148  christos 			if ((l2->l_stat == LSSLEEP) && (l2->l_flag & L_SINTR))
   1120  1.144      fvdl 				l = l2;
   1121  1.144      fvdl 			else if (l2->l_stat == LSSUSPENDED)
   1122  1.144      fvdl 				suspended = l2;
   1123  1.144      fvdl 			else if ((l2->l_stat != LSZOMB) &&
   1124  1.144      fvdl 				 (l2->l_stat != LSDEAD))
   1125  1.144      fvdl 				allsusp = 0;
   1126  1.130   thorpej 		} else {
   1127  1.130   thorpej 			/*
   1128  1.130   thorpej 			 * Find out if any of the sleeps are interruptable,
   1129  1.130   thorpej 			 * and if all the live LWPs remaining are suspended.
   1130  1.130   thorpej 			 */
   1131  1.130   thorpej 			allsusp = 1;
   1132  1.131  jdolecek 			LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1133  1.130   thorpej 				if (l->l_stat == LSSLEEP &&
   1134  1.130   thorpej 				    l->l_flag & L_SINTR)
   1135  1.130   thorpej 					break;
   1136  1.130   thorpej 				if (l->l_stat == LSSUSPENDED)
   1137  1.130   thorpej 					suspended = l;
   1138  1.130   thorpej 				else if ((l->l_stat != LSZOMB) &&
   1139  1.130   thorpej 				         (l->l_stat != LSDEAD))
   1140  1.130   thorpej 					allsusp = 0;
   1141  1.130   thorpej 			}
   1142   1.29       cgd 		}
   1143  1.130   thorpej 		if (p->p_stat == SACTIVE) {
   1144  1.147      fvdl 
   1145  1.130   thorpej 
   1146  1.130   thorpej 			if (l != NULL && (p->p_flag & P_TRACED))
   1147  1.130   thorpej 				goto run;
   1148  1.130   thorpej 
   1149   1.29       cgd 			/*
   1150  1.130   thorpej 			 * If SIGCONT is default (or ignored) and process is
   1151  1.130   thorpej 			 * asleep, we are finished; the process should not
   1152  1.130   thorpej 			 * be awakened.
   1153   1.29       cgd 			 */
   1154  1.130   thorpej 			if ((prop & SA_CONT) && action == SIG_DFL) {
   1155  1.130   thorpej 				sigdelset(&p->p_sigctx.ps_siglist, signum);
   1156  1.152  christos 				goto done;
   1157  1.130   thorpej 			}
   1158  1.130   thorpej 
   1159  1.130   thorpej 			/*
   1160  1.130   thorpej 			 * When a sleeping process receives a stop
   1161  1.130   thorpej 			 * signal, process immediately if possible.
   1162  1.130   thorpej 			 */
   1163  1.130   thorpej 			if ((prop & SA_STOP) && action == SIG_DFL) {
   1164  1.104   thorpej 				/*
   1165  1.130   thorpej 				 * If a child holding parent blocked,
   1166  1.130   thorpej 				 * stopping could cause deadlock.
   1167  1.104   thorpej 				 */
   1168  1.152  christos 				if (p->p_flag & P_PPWAIT) {
   1169  1.130   thorpej 					goto out;
   1170  1.152  christos 				}
   1171  1.130   thorpej 				sigdelset(&p->p_sigctx.ps_siglist, signum);
   1172  1.130   thorpej 				p->p_xstat = signum;
   1173  1.130   thorpej 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
   1174  1.130   thorpej 					/*
   1175  1.130   thorpej 					 * XXXSMP: recursive call; don't lock
   1176  1.130   thorpej 					 * the second time around.
   1177  1.130   thorpej 					 */
   1178  1.151  christos 					child_psignal(p, 0);
   1179  1.130   thorpej 				}
   1180  1.130   thorpej 				proc_stop(p);	/* XXXSMP: recurse? */
   1181  1.152  christos 				goto done;
   1182  1.104   thorpej 			}
   1183   1.29       cgd 
   1184  1.130   thorpej 			if (l == NULL) {
   1185  1.130   thorpej 				/*
   1186  1.130   thorpej 				 * Special case: SIGKILL of a process
   1187  1.130   thorpej 				 * which is entirely composed of
   1188  1.130   thorpej 				 * suspended LWPs should succeed. We
   1189  1.130   thorpej 				 * make this happen by unsuspending one of
   1190  1.130   thorpej 				 * them.
   1191  1.130   thorpej 				 */
   1192  1.130   thorpej 				if (allsusp && (signum == SIGKILL))
   1193  1.130   thorpej 					lwp_continue(suspended);
   1194  1.152  christos 				goto done;
   1195  1.130   thorpej 			}
   1196  1.130   thorpej 			/*
   1197  1.130   thorpej 			 * All other (caught or default) signals
   1198  1.130   thorpej 			 * cause the process to run.
   1199  1.130   thorpej 			 */
   1200   1.29       cgd 			goto runfast;
   1201  1.130   thorpej 			/*NOTREACHED*/
   1202  1.130   thorpej 		} else if (p->p_stat == SSTOP) {
   1203  1.130   thorpej 			/* Process is stopped */
   1204  1.130   thorpej 			/*
   1205  1.130   thorpej 			 * If traced process is already stopped,
   1206  1.130   thorpej 			 * then no further action is necessary.
   1207  1.130   thorpej 			 */
   1208  1.130   thorpej 			if (p->p_flag & P_TRACED)
   1209  1.152  christos 				goto done;
   1210   1.29       cgd 
   1211   1.29       cgd 			/*
   1212  1.130   thorpej 			 * Kill signal always sets processes running,
   1213  1.130   thorpej 			 * if possible.
   1214   1.29       cgd 			 */
   1215  1.130   thorpej 			if (signum == SIGKILL) {
   1216  1.130   thorpej 				l = proc_unstop(p);
   1217  1.130   thorpej 				if (l)
   1218  1.130   thorpej 					goto runfast;
   1219  1.152  christos 				goto done;
   1220  1.130   thorpej 			}
   1221  1.130   thorpej 
   1222  1.130   thorpej 			if (prop & SA_CONT) {
   1223  1.130   thorpej 				/*
   1224  1.130   thorpej 				 * If SIGCONT is default (or ignored),
   1225  1.130   thorpej 				 * we continue the process but don't
   1226  1.130   thorpej 				 * leave the signal in ps_siglist, as
   1227  1.130   thorpej 				 * it has no further action.  If
   1228  1.130   thorpej 				 * SIGCONT is held, we continue the
   1229  1.130   thorpej 				 * process and leave the signal in
   1230  1.130   thorpej 				 * ps_siglist.  If the process catches
   1231  1.130   thorpej 				 * SIGCONT, let it handle the signal
   1232  1.130   thorpej 				 * itself.  If it isn't waiting on an
   1233  1.130   thorpej 				 * event, then it goes back to run
   1234  1.130   thorpej 				 * state.  Otherwise, process goes
   1235  1.130   thorpej 				 * back to sleep state.
   1236  1.130   thorpej 				 */
   1237  1.130   thorpej 				if (action == SIG_DFL)
   1238  1.130   thorpej 					sigdelset(&p->p_sigctx.ps_siglist,
   1239  1.130   thorpej 					signum);
   1240  1.130   thorpej 				l = proc_unstop(p);
   1241  1.130   thorpej 				if (l && (action == SIG_CATCH))
   1242  1.130   thorpej 					goto runfast;
   1243  1.130   thorpej 				goto out;
   1244  1.130   thorpej 			}
   1245  1.130   thorpej 
   1246  1.130   thorpej 			if (prop & SA_STOP) {
   1247  1.130   thorpej 				/*
   1248  1.130   thorpej 				 * Already stopped, don't need to stop again.
   1249  1.130   thorpej 				 * (If we did the shell could get confused.)
   1250  1.130   thorpej 				 */
   1251  1.109  jdolecek 				sigdelset(&p->p_sigctx.ps_siglist, signum);
   1252  1.152  christos 				goto done;
   1253  1.130   thorpej 			}
   1254   1.29       cgd 
   1255   1.29       cgd 			/*
   1256  1.133   nathanw 			 * If a lwp is sleeping interruptibly, then
   1257  1.133   nathanw 			 * wake it up; it will run until the kernel
   1258  1.133   nathanw 			 * boundary, where it will stop in issignal(),
   1259  1.133   nathanw 			 * since p->p_stat is still SSTOP. When the
   1260  1.133   nathanw 			 * process is continued, it will be made
   1261  1.133   nathanw 			 * runnable and can look at the signal.
   1262   1.29       cgd 			 */
   1263  1.130   thorpej 			if (l)
   1264  1.133   nathanw 				goto run;
   1265   1.29       cgd 			goto out;
   1266  1.130   thorpej 		} else {
   1267  1.130   thorpej 			/* Else what? */
   1268  1.131  jdolecek 			panic("psignal: Invalid process state %d.",
   1269  1.131  jdolecek 				p->p_stat);
   1270   1.29       cgd 		}
   1271   1.29       cgd 	}
   1272   1.29       cgd 	/*NOTREACHED*/
   1273   1.29       cgd 
   1274  1.112     lukem  runfast:
   1275  1.152  christos 	if (action == SIG_CATCH) {
   1276  1.155  christos 		ksiginfo_put(p, ksi);
   1277  1.152  christos 		action = SIG_HOLD;
   1278  1.152  christos 	}
   1279   1.29       cgd 	/*
   1280   1.29       cgd 	 * Raise priority to at least PUSER.
   1281   1.29       cgd 	 */
   1282  1.130   thorpej 	if (l->l_priority > PUSER)
   1283  1.130   thorpej 		l->l_priority = PUSER;
   1284  1.112     lukem  run:
   1285  1.152  christos 	if (action == SIG_CATCH) {
   1286  1.155  christos 		ksiginfo_put(p, ksi);
   1287  1.152  christos 		action = SIG_HOLD;
   1288  1.152  christos 	}
   1289  1.144      fvdl 
   1290  1.130   thorpej 	setrunnable(l);		/* XXXSMP: recurse? */
   1291  1.112     lukem  out:
   1292  1.152  christos 	if (action == SIG_CATCH)
   1293  1.155  christos 		ksiginfo_put(p, ksi);
   1294  1.152  christos  done:
   1295  1.104   thorpej 	/* XXXSMP: works, but icky */
   1296  1.104   thorpej 	if (dolock)
   1297  1.104   thorpej 		SCHED_UNLOCK(s);
   1298   1.29       cgd }
   1299   1.29       cgd 
   1300  1.130   thorpej void
   1301  1.160  christos kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
   1302  1.130   thorpej {
   1303  1.130   thorpej 	struct proc *p = l->l_proc;
   1304  1.130   thorpej 	struct lwp *le, *li;
   1305  1.150        cl 	siginfo_t *si;
   1306  1.150        cl 	int f;
   1307  1.130   thorpej 
   1308  1.130   thorpej 	if (p->p_flag & P_SA) {
   1309  1.144      fvdl 
   1310  1.144      fvdl 		/* XXXUPSXXX What if not on sa_vp ? */
   1311  1.144      fvdl 
   1312  1.150        cl 		f = l->l_flag & L_SA;
   1313  1.144      fvdl 		l->l_flag &= ~L_SA;
   1314  1.130   thorpej 		si = pool_get(&siginfo_pool, PR_WAITOK);
   1315  1.166   thorpej 		si->_info = ksi->ksi_info;
   1316  1.130   thorpej 		le = li = NULL;
   1317  1.166   thorpej 		if (KSI_TRAP_P(ksi))
   1318  1.130   thorpej 			le = l;
   1319  1.130   thorpej 		else
   1320  1.130   thorpej 			li = l;
   1321  1.130   thorpej 
   1322  1.130   thorpej 		sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
   1323  1.130   thorpej 			    sizeof(siginfo_t), si);
   1324  1.150        cl 		l->l_flag |= f;
   1325  1.130   thorpej 		return;
   1326  1.130   thorpej 	}
   1327  1.130   thorpej 
   1328  1.148  christos #ifdef __HAVE_SIGINFO
   1329  1.148  christos 	(*p->p_emul->e_sendsig)(ksi, mask);
   1330  1.148  christos #else
   1331  1.166   thorpej 	(*p->p_emul->e_sendsig)(ksi->ksi_signo, mask, KSI_TRAPCODE(ksi));
   1332  1.148  christos #endif
   1333  1.130   thorpej }
   1334  1.130   thorpej 
   1335  1.112     lukem static __inline int firstsig(const sigset_t *);
   1336   1.79   mycroft 
   1337   1.79   mycroft static __inline int
   1338  1.112     lukem firstsig(const sigset_t *ss)
   1339   1.79   mycroft {
   1340   1.79   mycroft 	int sig;
   1341   1.79   mycroft 
   1342   1.79   mycroft 	sig = ffs(ss->__bits[0]);
   1343   1.79   mycroft 	if (sig != 0)
   1344   1.79   mycroft 		return (sig);
   1345   1.79   mycroft #if NSIG > 33
   1346   1.79   mycroft 	sig = ffs(ss->__bits[1]);
   1347   1.79   mycroft 	if (sig != 0)
   1348   1.79   mycroft 		return (sig + 32);
   1349   1.79   mycroft #endif
   1350   1.79   mycroft #if NSIG > 65
   1351   1.79   mycroft 	sig = ffs(ss->__bits[2]);
   1352   1.79   mycroft 	if (sig != 0)
   1353   1.79   mycroft 		return (sig + 64);
   1354   1.79   mycroft #endif
   1355   1.79   mycroft #if NSIG > 97
   1356   1.79   mycroft 	sig = ffs(ss->__bits[3]);
   1357   1.79   mycroft 	if (sig != 0)
   1358   1.79   mycroft 		return (sig + 96);
   1359   1.79   mycroft #endif
   1360   1.79   mycroft 	return (0);
   1361   1.79   mycroft }
   1362   1.79   mycroft 
   1363   1.29       cgd /*
   1364   1.29       cgd  * If the current process has received a signal (should be caught or cause
   1365   1.29       cgd  * termination, should interrupt current syscall), return the signal number.
   1366   1.29       cgd  * Stop signals with default action are processed immediately, then cleared;
   1367   1.29       cgd  * they aren't returned.  This is checked after each entry to the system for
   1368   1.29       cgd  * a syscall or trap (though this can usually be done without calling issignal
   1369   1.29       cgd  * by checking the pending signal masks in the CURSIG macro.) The normal call
   1370   1.29       cgd  * sequence is
   1371   1.29       cgd  *
   1372  1.130   thorpej  *	while (signum = CURSIG(curlwp))
   1373   1.29       cgd  *		postsig(signum);
   1374   1.29       cgd  */
   1375   1.29       cgd int
   1376  1.130   thorpej issignal(struct lwp *l)
   1377   1.29       cgd {
   1378  1.130   thorpej 	struct proc	*p = l->l_proc;
   1379  1.127       scw 	int		s = 0, signum, prop;
   1380  1.130   thorpej 	int		dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
   1381  1.112     lukem 	sigset_t	ss;
   1382   1.29       cgd 
   1383  1.144      fvdl 	if (l->l_flag & L_SA) {
   1384  1.144      fvdl 		struct sadata *sa = p->p_sa;
   1385  1.144      fvdl 
   1386  1.144      fvdl 		/* Bail out if we do not own the virtual processor */
   1387  1.144      fvdl 		if (sa->sa_vp != l)
   1388  1.144      fvdl 			return 0;
   1389  1.144      fvdl 	}
   1390  1.144      fvdl 
   1391  1.130   thorpej 	if (p->p_stat == SSTOP) {
   1392  1.130   thorpej 		/*
   1393  1.130   thorpej 		 * The process is stopped/stopping. Stop ourselves now that
   1394  1.130   thorpej 		 * we're on the kernel/userspace boundary.
   1395  1.130   thorpej 		 */
   1396  1.130   thorpej 		if (dolock)
   1397  1.130   thorpej 			SCHED_LOCK(s);
   1398  1.130   thorpej 		l->l_stat = LSSTOP;
   1399  1.130   thorpej 		p->p_nrlwps--;
   1400  1.130   thorpej 		if (p->p_flag & P_TRACED)
   1401  1.130   thorpej 			goto sigtraceswitch;
   1402  1.130   thorpej 		else
   1403  1.130   thorpej 			goto sigswitch;
   1404  1.130   thorpej 	}
   1405   1.29       cgd 	for (;;) {
   1406   1.79   mycroft 		sigpending1(p, &ss);
   1407   1.29       cgd 		if (p->p_flag & P_PPWAIT)
   1408   1.79   mycroft 			sigminusset(&stopsigmask, &ss);
   1409   1.79   mycroft 		signum = firstsig(&ss);
   1410   1.79   mycroft 		if (signum == 0) {		 	/* no signal to send */
   1411  1.109  jdolecek 			p->p_sigctx.ps_sigcheck = 0;
   1412  1.119  christos 			if (locked && dolock)
   1413  1.119  christos 				SCHED_LOCK(s);
   1414   1.29       cgd 			return (0);
   1415   1.79   mycroft 		}
   1416  1.112     lukem 							/* take the signal! */
   1417  1.112     lukem 		sigdelset(&p->p_sigctx.ps_siglist, signum);
   1418   1.42   mycroft 
   1419   1.29       cgd 		/*
   1420   1.29       cgd 		 * We should see pending but ignored signals
   1421   1.29       cgd 		 * only if P_TRACED was on when they were posted.
   1422   1.29       cgd 		 */
   1423  1.109  jdolecek 		if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
   1424   1.79   mycroft 		    (p->p_flag & P_TRACED) == 0)
   1425   1.29       cgd 			continue;
   1426   1.42   mycroft 
   1427   1.29       cgd 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
   1428   1.29       cgd 			/*
   1429   1.29       cgd 			 * If traced, always stop, and stay
   1430   1.29       cgd 			 * stopped until released by the debugger.
   1431   1.29       cgd 			 */
   1432   1.29       cgd 			p->p_xstat = signum;
   1433   1.66   mycroft 			if ((p->p_flag & P_FSTRACE) == 0)
   1434  1.151  christos 				child_psignal(p, dolock);
   1435  1.119  christos 			if (dolock)
   1436  1.119  christos 				SCHED_LOCK(s);
   1437  1.114   nathanw 			proc_stop(p);
   1438  1.130   thorpej 		sigtraceswitch:
   1439  1.130   thorpej 			mi_switch(l, NULL);
   1440  1.114   nathanw 			SCHED_ASSERT_UNLOCKED();
   1441  1.119  christos 			if (dolock)
   1442  1.119  christos 				splx(s);
   1443  1.119  christos 			else
   1444  1.119  christos 				dolock = 1;
   1445   1.29       cgd 
   1446   1.29       cgd 			/*
   1447   1.42   mycroft 			 * If we are no longer being traced, or the parent
   1448   1.42   mycroft 			 * didn't give us a signal, look for more signals.
   1449   1.29       cgd 			 */
   1450   1.42   mycroft 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
   1451   1.29       cgd 				continue;
   1452   1.29       cgd 
   1453   1.29       cgd 			/*
   1454   1.42   mycroft 			 * If the new signal is being masked, look for other
   1455   1.42   mycroft 			 * signals.
   1456   1.29       cgd 			 */
   1457   1.42   mycroft 			signum = p->p_xstat;
   1458  1.130   thorpej 			p->p_xstat = 0;
   1459  1.112     lukem 			/*
   1460  1.112     lukem 			 * `p->p_sigctx.ps_siglist |= mask' is done
   1461  1.112     lukem 			 * in setrunnable().
   1462  1.112     lukem 			 */
   1463  1.109  jdolecek 			if (sigismember(&p->p_sigctx.ps_sigmask, signum))
   1464   1.29       cgd 				continue;
   1465  1.112     lukem 							/* take the signal! */
   1466  1.112     lukem 			sigdelset(&p->p_sigctx.ps_siglist, signum);
   1467   1.29       cgd 		}
   1468   1.29       cgd 
   1469   1.42   mycroft 		prop = sigprop[signum];
   1470   1.42   mycroft 
   1471   1.29       cgd 		/*
   1472   1.29       cgd 		 * Decide whether the signal should be returned.
   1473   1.29       cgd 		 * Return the signal's number, or fall through
   1474   1.29       cgd 		 * to clear it from the pending mask.
   1475   1.29       cgd 		 */
   1476  1.109  jdolecek 		switch ((long)SIGACTION(p, signum).sa_handler) {
   1477   1.29       cgd 
   1478   1.33       cgd 		case (long)SIG_DFL:
   1479   1.29       cgd 			/*
   1480   1.29       cgd 			 * Don't take default actions on system processes.
   1481   1.29       cgd 			 */
   1482   1.29       cgd 			if (p->p_pid <= 1) {
   1483   1.29       cgd #ifdef DIAGNOSTIC
   1484   1.29       cgd 				/*
   1485   1.29       cgd 				 * Are you sure you want to ignore SIGSEGV
   1486   1.29       cgd 				 * in init? XXX
   1487   1.29       cgd 				 */
   1488   1.57  christos 				printf("Process (pid %d) got signal %d\n",
   1489   1.29       cgd 				    p->p_pid, signum);
   1490   1.29       cgd #endif
   1491   1.29       cgd 				break;		/* == ignore */
   1492   1.29       cgd 			}
   1493   1.29       cgd 			/*
   1494   1.29       cgd 			 * If there is a pending stop signal to process
   1495   1.29       cgd 			 * with default action, stop here,
   1496   1.29       cgd 			 * then clear the signal.  However,
   1497   1.29       cgd 			 * if process is member of an orphaned
   1498   1.29       cgd 			 * process group, ignore tty stop signals.
   1499   1.29       cgd 			 */
   1500   1.29       cgd 			if (prop & SA_STOP) {
   1501   1.29       cgd 				if (p->p_flag & P_TRACED ||
   1502   1.29       cgd 		    		    (p->p_pgrp->pg_jobc == 0 &&
   1503   1.29       cgd 				    prop & SA_TTYSTOP))
   1504   1.29       cgd 					break;	/* == ignore */
   1505   1.29       cgd 				p->p_xstat = signum;
   1506   1.29       cgd 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
   1507  1.151  christos 					child_psignal(p, dolock);
   1508  1.119  christos 				if (dolock)
   1509  1.119  christos 					SCHED_LOCK(s);
   1510  1.104   thorpej 				proc_stop(p);
   1511  1.130   thorpej 			sigswitch:
   1512  1.130   thorpej 				mi_switch(l, NULL);
   1513  1.104   thorpej 				SCHED_ASSERT_UNLOCKED();
   1514  1.119  christos 				if (dolock)
   1515  1.119  christos 					splx(s);
   1516  1.119  christos 				else
   1517  1.119  christos 					dolock = 1;
   1518   1.29       cgd 				break;
   1519   1.29       cgd 			} else if (prop & SA_IGNORE) {
   1520   1.29       cgd 				/*
   1521   1.29       cgd 				 * Except for SIGCONT, shouldn't get here.
   1522   1.29       cgd 				 * Default action is to ignore; drop it.
   1523   1.29       cgd 				 */
   1524   1.29       cgd 				break;		/* == ignore */
   1525   1.29       cgd 			} else
   1526   1.42   mycroft 				goto keep;
   1527   1.29       cgd 			/*NOTREACHED*/
   1528   1.29       cgd 
   1529   1.33       cgd 		case (long)SIG_IGN:
   1530   1.29       cgd 			/*
   1531   1.29       cgd 			 * Masking above should prevent us ever trying
   1532   1.29       cgd 			 * to take action on an ignored signal other
   1533   1.29       cgd 			 * than SIGCONT, unless process is traced.
   1534   1.29       cgd 			 */
   1535  1.128  jdolecek #ifdef DEBUG_ISSIGNAL
   1536   1.29       cgd 			if ((prop & SA_CONT) == 0 &&
   1537   1.29       cgd 			    (p->p_flag & P_TRACED) == 0)
   1538   1.57  christos 				printf("issignal\n");
   1539  1.128  jdolecek #endif
   1540   1.29       cgd 			break;		/* == ignore */
   1541   1.29       cgd 
   1542   1.29       cgd 		default:
   1543   1.29       cgd 			/*
   1544   1.29       cgd 			 * This signal has an action, let
   1545   1.29       cgd 			 * postsig() process it.
   1546   1.29       cgd 			 */
   1547   1.42   mycroft 			goto keep;
   1548   1.29       cgd 		}
   1549   1.29       cgd 	}
   1550   1.29       cgd 	/* NOTREACHED */
   1551   1.42   mycroft 
   1552  1.112     lukem  keep:
   1553  1.112     lukem 						/* leave the signal for later */
   1554  1.112     lukem 	sigaddset(&p->p_sigctx.ps_siglist, signum);
   1555  1.110   thorpej 	CHECKSIGS(p);
   1556  1.119  christos 	if (locked && dolock)
   1557  1.119  christos 		SCHED_LOCK(s);
   1558   1.42   mycroft 	return (signum);
   1559   1.29       cgd }
   1560   1.29       cgd 
   1561   1.29       cgd /*
   1562   1.29       cgd  * Put the argument process into the stopped state and notify the parent
   1563   1.29       cgd  * via wakeup.  Signals are handled elsewhere.  The process must not be
   1564   1.29       cgd  * on the run queue.
   1565   1.29       cgd  */
   1566  1.104   thorpej static void
   1567  1.112     lukem proc_stop(struct proc *p)
   1568   1.29       cgd {
   1569  1.130   thorpej 	struct lwp *l;
   1570   1.29       cgd 
   1571  1.104   thorpej 	SCHED_ASSERT_LOCKED();
   1572  1.104   thorpej 
   1573  1.130   thorpej 	/* XXX lock process LWP state */
   1574   1.29       cgd 	p->p_stat = SSTOP;
   1575   1.29       cgd 	p->p_flag &= ~P_WAITED;
   1576  1.130   thorpej 
   1577  1.130   thorpej 	/*
   1578  1.130   thorpej 	 * Put as many LWP's as possible in stopped state.
   1579  1.130   thorpej 	 * Sleeping ones will notice the stopped state as they try to
   1580  1.130   thorpej 	 * return to userspace.
   1581  1.130   thorpej 	 */
   1582  1.130   thorpej 
   1583  1.132  jdolecek 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1584  1.144      fvdl 		if ((l->l_stat == LSONPROC) && (l == curlwp)) {
   1585  1.130   thorpej 			/* XXX SMP this assumes that a LWP that is LSONPROC
   1586  1.130   thorpej 			 * is curlwp and hence is about to be mi_switched
   1587  1.130   thorpej 			 * away; the only callers of proc_stop() are:
   1588  1.130   thorpej 			 * - psignal
   1589  1.130   thorpej 			 * - issignal()
   1590  1.130   thorpej 			 * For the former, proc_stop() is only called when
   1591  1.130   thorpej 			 * no processes are running, so we don't worry.
   1592  1.130   thorpej 			 * For the latter, proc_stop() is called right
   1593  1.130   thorpej 			 * before mi_switch().
   1594  1.130   thorpej 			 */
   1595  1.130   thorpej 			l->l_stat = LSSTOP;
   1596  1.130   thorpej 			p->p_nrlwps--;
   1597  1.144      fvdl 		}
   1598  1.144      fvdl 		 else if ( (l->l_stat == LSSLEEP) && (l->l_flag & L_SINTR)) {
   1599  1.144      fvdl 			setrunnable(l);
   1600  1.144      fvdl 		}
   1601  1.144      fvdl 
   1602  1.144      fvdl /* !!!UPS!!! FIX ME */
   1603  1.144      fvdl #if 0
   1604  1.144      fvdl else if (l->l_stat == LSRUN) {
   1605  1.130   thorpej 			/* Remove LWP from the run queue */
   1606  1.130   thorpej 			remrunqueue(l);
   1607  1.130   thorpej 			l->l_stat = LSSTOP;
   1608  1.130   thorpej 			p->p_nrlwps--;
   1609  1.130   thorpej 		} else if ((l->l_stat == LSSLEEP) ||
   1610  1.130   thorpej 		    (l->l_stat == LSSUSPENDED) ||
   1611  1.130   thorpej 		    (l->l_stat == LSZOMB) ||
   1612  1.130   thorpej 		    (l->l_stat == LSDEAD)) {
   1613  1.130   thorpej 			/*
   1614  1.130   thorpej 			 * Don't do anything; let sleeping LWPs
   1615  1.130   thorpej 			 * discover the stopped state of the process
   1616  1.130   thorpej 			 * on their way out of the kernel; otherwise,
   1617  1.130   thorpej 			 * things like NFS threads that sleep with
   1618  1.130   thorpej 			 * locks will block the rest of the system
   1619  1.130   thorpej 			 * from getting any work done.
   1620  1.130   thorpej 			 *
   1621  1.130   thorpej 			 * Suspended/dead/zombie LWPs aren't going
   1622  1.130   thorpej 			 * anywhere, so we don't need to touch them.
   1623  1.130   thorpej 			 */
   1624  1.130   thorpej 		}
   1625  1.130   thorpej #ifdef DIAGNOSTIC
   1626  1.130   thorpej 		else {
   1627  1.130   thorpej 			panic("proc_stop: process %d lwp %d "
   1628  1.130   thorpej 			      "in unstoppable state %d.\n",
   1629  1.130   thorpej 			    p->p_pid, l->l_lid, l->l_stat);
   1630  1.130   thorpej 		}
   1631  1.130   thorpej #endif
   1632  1.144      fvdl #endif
   1633  1.130   thorpej 	}
   1634  1.130   thorpej 	/* XXX unlock process LWP state */
   1635  1.144      fvdl 
   1636  1.104   thorpej 	sched_wakeup((caddr_t)p->p_pptr);
   1637   1.29       cgd }
   1638   1.29       cgd 
   1639  1.133   nathanw /*
   1640  1.133   nathanw  * Given a process in state SSTOP, set the state back to SACTIVE and
   1641  1.133   nathanw  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
   1642  1.133   nathanw  *
   1643  1.133   nathanw  * If no LWPs ended up runnable (and therefore able to take a signal),
   1644  1.133   nathanw  * return a LWP that is sleeping interruptably. The caller can wake
   1645  1.133   nathanw  * that LWP up to take a signal.
   1646  1.133   nathanw  */
   1647  1.130   thorpej struct lwp *
   1648  1.139     skrll proc_unstop(struct proc *p)
   1649  1.130   thorpej {
   1650  1.130   thorpej 	struct lwp *l, *lr = NULL;
   1651  1.133   nathanw 	int cantake = 0;
   1652  1.130   thorpej 
   1653  1.130   thorpej 	SCHED_ASSERT_LOCKED();
   1654  1.130   thorpej 
   1655  1.130   thorpej 	/*
   1656  1.140   nathanw 	 * Our caller wants to be informed if there are only sleeping
   1657  1.140   nathanw 	 * and interruptable LWPs left after we have run so that it
   1658  1.140   nathanw 	 * can invoke setrunnable() if required - return one of the
   1659  1.140   nathanw 	 * interruptable LWPs if this is the case.
   1660  1.130   thorpej 	 */
   1661  1.130   thorpej 
   1662  1.130   thorpej 	p->p_stat = SACTIVE;
   1663  1.133   nathanw 	if (p->p_flag & P_SA) {
   1664  1.140   nathanw 		/*
   1665  1.140   nathanw 		 * Preferentially select the idle LWP as the interruptable
   1666  1.140   nathanw 		 * LWP to return if it exists.
   1667  1.140   nathanw 		 */
   1668  1.140   nathanw 		lr = p->p_sa->sa_idle;
   1669  1.140   nathanw 		if (lr != NULL)
   1670  1.140   nathanw 			cantake = 1;
   1671  1.133   nathanw 	}
   1672  1.132  jdolecek 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1673  1.140   nathanw 		if (l->l_stat == LSRUN) {
   1674  1.140   nathanw 			lr = NULL;
   1675  1.133   nathanw 			cantake = 1;
   1676  1.140   nathanw 		}
   1677  1.132  jdolecek 		if (l->l_stat != LSSTOP)
   1678  1.132  jdolecek 			continue;
   1679  1.132  jdolecek 
   1680  1.133   nathanw 		if (l->l_wchan != NULL) {
   1681  1.133   nathanw 			l->l_stat = LSSLEEP;
   1682  1.133   nathanw 			if ((cantake == 0) && (l->l_flag & L_SINTR)) {
   1683  1.132  jdolecek 				lr = l;
   1684  1.133   nathanw 				cantake = 1;
   1685  1.133   nathanw 			}
   1686  1.133   nathanw 		} else {
   1687  1.140   nathanw 			setrunnable(l);
   1688  1.140   nathanw 			lr = NULL;
   1689  1.133   nathanw 			cantake = 1;
   1690  1.133   nathanw 		}
   1691  1.132  jdolecek 	}
   1692  1.130   thorpej 
   1693  1.130   thorpej 	return lr;
   1694  1.130   thorpej }
   1695  1.130   thorpej 
   1696   1.29       cgd /*
   1697   1.29       cgd  * Take the action for the specified signal
   1698   1.29       cgd  * from the current set of pending signals.
   1699   1.29       cgd  */
   1700   1.29       cgd void
   1701  1.112     lukem postsig(int signum)
   1702   1.29       cgd {
   1703  1.130   thorpej 	struct lwp *l;
   1704  1.112     lukem 	struct proc	*p;
   1705  1.112     lukem 	struct sigacts	*ps;
   1706  1.112     lukem 	sig_t		action;
   1707  1.112     lukem 	sigset_t	*returnmask;
   1708   1.29       cgd 
   1709  1.130   thorpej 	l = curlwp;
   1710  1.130   thorpej 	p = l->l_proc;
   1711  1.112     lukem 	ps = p->p_sigacts;
   1712   1.29       cgd #ifdef DIAGNOSTIC
   1713   1.29       cgd 	if (signum == 0)
   1714   1.29       cgd 		panic("postsig");
   1715   1.29       cgd #endif
   1716  1.106   thorpej 
   1717  1.130   thorpej 	KERNEL_PROC_LOCK(l);
   1718  1.106   thorpej 
   1719  1.109  jdolecek 	sigdelset(&p->p_sigctx.ps_siglist, signum);
   1720  1.109  jdolecek 	action = SIGACTION_PS(ps, signum).sa_handler;
   1721  1.157  christos 	if (action == SIG_DFL) {
   1722   1.29       cgd #ifdef KTRACE
   1723  1.157  christos 		if (KTRPOINT(p, KTR_PSIG))
   1724  1.157  christos 			ktrpsig(p, signum, action,
   1725  1.157  christos 			    p->p_sigctx.ps_flags & SAS_OLDMASK ?
   1726  1.157  christos 			    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
   1727  1.157  christos 			    NULL);
   1728   1.29       cgd #endif
   1729   1.29       cgd 		/*
   1730   1.29       cgd 		 * Default action, where the default is to kill
   1731   1.29       cgd 		 * the process.  (Other cases were ignored above.)
   1732   1.29       cgd 		 */
   1733  1.130   thorpej 		sigexit(l, signum);
   1734   1.29       cgd 		/* NOTREACHED */
   1735   1.29       cgd 	} else {
   1736  1.152  christos 		ksiginfo_t *ksi;
   1737   1.29       cgd 		/*
   1738   1.29       cgd 		 * If we get here, the signal must be caught.
   1739   1.29       cgd 		 */
   1740   1.29       cgd #ifdef DIAGNOSTIC
   1741  1.112     lukem 		if (action == SIG_IGN ||
   1742  1.112     lukem 		    sigismember(&p->p_sigctx.ps_sigmask, signum))
   1743   1.29       cgd 			panic("postsig action");
   1744   1.29       cgd #endif
   1745   1.29       cgd 		/*
   1746   1.29       cgd 		 * Set the new mask value and also defer further
   1747  1.138       wiz 		 * occurrences of this signal.
   1748   1.29       cgd 		 *
   1749   1.29       cgd 		 * Special case: user has done a sigpause.  Here the
   1750   1.29       cgd 		 * current mask is not of interest, but rather the
   1751   1.29       cgd 		 * mask from before the sigpause is what we want
   1752   1.29       cgd 		 * restored after the signal processing is completed.
   1753   1.29       cgd 		 */
   1754  1.109  jdolecek 		if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
   1755  1.109  jdolecek 			returnmask = &p->p_sigctx.ps_oldmask;
   1756  1.109  jdolecek 			p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
   1757   1.29       cgd 		} else
   1758  1.109  jdolecek 			returnmask = &p->p_sigctx.ps_sigmask;
   1759   1.29       cgd 		p->p_stats->p_ru.ru_nsignals++;
   1760  1.152  christos 		ksi = ksiginfo_get(p, signum);
   1761  1.157  christos #ifdef KTRACE
   1762  1.157  christos 		if (KTRPOINT(p, KTR_PSIG))
   1763  1.157  christos 			ktrpsig(p, signum, action,
   1764  1.157  christos 			    p->p_sigctx.ps_flags & SAS_OLDMASK ?
   1765  1.157  christos 			    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
   1766  1.157  christos 			    ksi);
   1767  1.157  christos #endif
   1768  1.152  christos 		if (ksi == NULL) {
   1769  1.152  christos 			ksiginfo_t ksi1;
   1770  1.152  christos 			/*
   1771  1.152  christos 			 * we did not save any siginfo for this, either
   1772  1.152  christos 			 * because the signal was not caught, or because the
   1773  1.152  christos 			 * user did not request SA_SIGINFO
   1774  1.152  christos 			 */
   1775  1.152  christos 			(void)memset(&ksi1, 0, sizeof(ksi1));
   1776  1.152  christos 			ksi1.ksi_signo = signum;
   1777  1.152  christos 			kpsendsig(l, &ksi1, returnmask);
   1778   1.29       cgd 		} else {
   1779  1.152  christos 			kpsendsig(l, ksi, returnmask);
   1780  1.152  christos 			pool_put(&ksiginfo_pool, ksi);
   1781  1.152  christos 		}
   1782  1.152  christos 		p->p_sigctx.ps_lwp = 0;
   1783  1.152  christos 		p->p_sigctx.ps_code = 0;
   1784  1.152  christos 		p->p_sigctx.ps_signo = 0;
   1785  1.105   thorpej 		(void) splsched();	/* XXXSMP */
   1786  1.112     lukem 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
   1787  1.112     lukem 		    &p->p_sigctx.ps_sigmask);
   1788  1.109  jdolecek 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
   1789  1.109  jdolecek 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
   1790   1.79   mycroft 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
   1791  1.109  jdolecek 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
   1792  1.109  jdolecek 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
   1793   1.79   mycroft 		}
   1794  1.104   thorpej 		(void) spl0();		/* XXXSMP */
   1795   1.29       cgd 	}
   1796  1.106   thorpej 
   1797  1.130   thorpej 	KERNEL_PROC_UNLOCK(l);
   1798   1.29       cgd }
   1799   1.29       cgd 
   1800   1.29       cgd /*
   1801   1.29       cgd  * Kill the current process for stated reason.
   1802   1.29       cgd  */
   1803   1.52  christos void
   1804  1.122      manu killproc(struct proc *p, const char *why)
   1805   1.29       cgd {
   1806   1.29       cgd 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
   1807   1.29       cgd 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
   1808   1.29       cgd 	psignal(p, SIGKILL);
   1809   1.29       cgd }
   1810   1.29       cgd 
   1811   1.29       cgd /*
   1812   1.29       cgd  * Force the current process to exit with the specified signal, dumping core
   1813   1.29       cgd  * if appropriate.  We bypass the normal tests for masked and caught signals,
   1814   1.29       cgd  * allowing unrecoverable failures to terminate the process without changing
   1815   1.29       cgd  * signal state.  Mark the accounting record with the signal termination.
   1816   1.29       cgd  * If dumping core, save the signal number for the debugger.  Calls exit and
   1817   1.29       cgd  * does not return.
   1818   1.29       cgd  */
   1819   1.96      fair 
   1820   1.97      fair #if defined(DEBUG)
   1821   1.96      fair int	kern_logsigexit = 1;	/* not static to make public for sysctl */
   1822   1.96      fair #else
   1823   1.96      fair int	kern_logsigexit = 0;	/* not static to make public for sysctl */
   1824   1.96      fair #endif
   1825   1.96      fair 
   1826  1.102  sommerfe static	const char logcoredump[] =
   1827   1.96      fair 	"pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
   1828  1.102  sommerfe static	const char lognocoredump[] =
   1829   1.96      fair 	"pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
   1830   1.96      fair 
   1831  1.130   thorpej /* Wrapper function for use in p_userret */
   1832  1.130   thorpej static void
   1833  1.130   thorpej lwp_coredump_hook(struct lwp *l, void *arg)
   1834  1.130   thorpej {
   1835  1.130   thorpej 	int s;
   1836  1.130   thorpej 
   1837  1.130   thorpej 	/*
   1838  1.130   thorpej 	 * Suspend ourselves, so that the kernel stack and therefore
   1839  1.130   thorpej 	 * the userland registers saved in the trapframe are around
   1840  1.130   thorpej 	 * for coredump() to write them out.
   1841  1.130   thorpej 	 */
   1842  1.130   thorpej 	KERNEL_PROC_LOCK(l);
   1843  1.130   thorpej 	l->l_flag &= ~L_DETACHED;
   1844  1.130   thorpej 	SCHED_LOCK(s);
   1845  1.130   thorpej 	l->l_stat = LSSUSPENDED;
   1846  1.130   thorpej 	l->l_proc->p_nrlwps--;
   1847  1.130   thorpej 	/* XXX NJWLWP check if this makes sense here: */
   1848  1.130   thorpej 	l->l_proc->p_stats->p_ru.ru_nvcsw++;
   1849  1.130   thorpej 	mi_switch(l, NULL);
   1850  1.130   thorpej 	SCHED_ASSERT_UNLOCKED();
   1851  1.130   thorpej 	splx(s);
   1852  1.130   thorpej 
   1853  1.130   thorpej 	lwp_exit(l);
   1854  1.130   thorpej }
   1855  1.130   thorpej 
   1856   1.52  christos void
   1857  1.130   thorpej sigexit(struct lwp *l, int signum)
   1858   1.29       cgd {
   1859  1.130   thorpej 	struct proc	*p;
   1860  1.144      fvdl #if 0
   1861  1.136   nathanw 	struct lwp	*l2;
   1862  1.144      fvdl #endif
   1863  1.130   thorpej 	int		error, exitsig;
   1864  1.130   thorpej 
   1865  1.130   thorpej 	p = l->l_proc;
   1866  1.130   thorpej 
   1867  1.130   thorpej 	/*
   1868  1.130   thorpej 	 * Don't permit coredump() or exit1() multiple times
   1869  1.130   thorpej 	 * in the same process.
   1870  1.130   thorpej 	 */
   1871  1.136   nathanw 	if (p->p_flag & P_WEXIT) {
   1872  1.136   nathanw 		KERNEL_PROC_UNLOCK(l);
   1873  1.130   thorpej 		(*p->p_userret)(l, p->p_userret_arg);
   1874  1.136   nathanw 	}
   1875  1.130   thorpej 	p->p_flag |= P_WEXIT;
   1876  1.130   thorpej 	/* We don't want to switch away from exiting. */
   1877  1.130   thorpej 	/* XXX multiprocessor: stop LWPs on other processors. */
   1878  1.144      fvdl #if 0
   1879  1.136   nathanw 	if (p->p_flag & P_SA) {
   1880  1.136   nathanw 		LIST_FOREACH(l2, &p->p_lwps, l_sibling)
   1881  1.136   nathanw 		    l2->l_flag &= ~L_SA;
   1882  1.130   thorpej 		p->p_flag &= ~P_SA;
   1883  1.130   thorpej 	}
   1884  1.144      fvdl #endif
   1885  1.130   thorpej 
   1886  1.130   thorpej 	/* Make other LWPs stick around long enough to be dumped */
   1887  1.130   thorpej 	p->p_userret = lwp_coredump_hook;
   1888  1.130   thorpej 	p->p_userret_arg = NULL;
   1889   1.96      fair 
   1890  1.112     lukem 	exitsig = signum;
   1891   1.29       cgd 	p->p_acflag |= AXSIG;
   1892   1.29       cgd 	if (sigprop[signum] & SA_CORE) {
   1893  1.152  christos 		p->p_sigctx.ps_signo = signum;
   1894  1.130   thorpej 		if ((error = coredump(l)) == 0)
   1895  1.102  sommerfe 			exitsig |= WCOREFLAG;
   1896  1.102  sommerfe 
   1897  1.102  sommerfe 		if (kern_logsigexit) {
   1898  1.123   thorpej 			/* XXX What if we ever have really large UIDs? */
   1899  1.102  sommerfe 			int uid = p->p_cred && p->p_ucred ?
   1900  1.123   thorpej 				(int) p->p_ucred->cr_uid : -1;
   1901  1.102  sommerfe 
   1902  1.102  sommerfe 			if (error)
   1903  1.102  sommerfe 				log(LOG_INFO, lognocoredump, p->p_pid,
   1904  1.102  sommerfe 				    p->p_comm, uid, signum, error);
   1905  1.102  sommerfe 			else
   1906  1.102  sommerfe 				log(LOG_INFO, logcoredump, p->p_pid,
   1907  1.102  sommerfe 				    p->p_comm, uid, signum);
   1908   1.96      fair 		}
   1909   1.96      fair 
   1910   1.29       cgd 	}
   1911   1.96      fair 
   1912  1.130   thorpej 	exit1(l, W_EXITCODE(0, exitsig));
   1913   1.29       cgd 	/* NOTREACHED */
   1914   1.29       cgd }
   1915   1.29       cgd 
   1916   1.29       cgd /*
   1917   1.75   nathanw  * Dump core, into a file named "progname.core" or "core" (depending on the
   1918   1.75   nathanw  * value of shortcorename), unless the process was setuid/setgid.
   1919   1.29       cgd  */
   1920   1.29       cgd int
   1921  1.130   thorpej coredump(struct lwp *l)
   1922   1.29       cgd {
   1923  1.112     lukem 	struct vnode		*vp;
   1924  1.130   thorpej 	struct proc		*p;
   1925  1.112     lukem 	struct vmspace		*vm;
   1926  1.112     lukem 	struct ucred		*cred;
   1927  1.112     lukem 	struct nameidata	nd;
   1928  1.112     lukem 	struct vattr		vattr;
   1929  1.169   hannken 	struct mount		*mp;
   1930  1.112     lukem 	int			error, error1;
   1931  1.112     lukem 	char			name[MAXPATHLEN];
   1932  1.112     lukem 
   1933  1.130   thorpej 	p = l->l_proc;
   1934  1.112     lukem 	vm = p->p_vmspace;
   1935  1.112     lukem 	cred = p->p_cred->pc_ucred;
   1936   1.29       cgd 
   1937   1.59       cgd 	/*
   1938   1.59       cgd 	 * Make sure the process has not set-id, to prevent data leaks.
   1939   1.59       cgd 	 */
   1940   1.58       mrg 	if (p->p_flag & P_SUGID)
   1941   1.59       cgd 		return (EPERM);
   1942   1.59       cgd 
   1943   1.59       cgd 	/*
   1944   1.59       cgd 	 * Refuse to core if the data + stack + user size is larger than
   1945   1.59       cgd 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
   1946   1.59       cgd 	 * data.
   1947   1.59       cgd 	 */
   1948   1.30   deraadt 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
   1949   1.29       cgd 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
   1950   1.59       cgd 		return (EFBIG);		/* better error code? */
   1951   1.59       cgd 
   1952  1.169   hannken restart:
   1953   1.59       cgd 	/*
   1954   1.59       cgd 	 * The core dump will go in the current working directory.  Make
   1955   1.80        pk 	 * sure that the directory is still there and that the mount flags
   1956   1.80        pk 	 * allow us to write core dumps there.
   1957   1.59       cgd 	 */
   1958   1.88   thorpej 	vp = p->p_cwdi->cwdi_cdir;
   1959   1.80        pk 	if (vp->v_mount == NULL ||
   1960   1.80        pk 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
   1961   1.59       cgd 		return (EPERM);
   1962   1.59       cgd 
   1963  1.100  sommerfe 	error = build_corename(p, name);
   1964   1.94    bouyer 	if (error)
   1965   1.94    bouyer 		return error;
   1966   1.94    bouyer 
   1967  1.143      fvdl 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
   1968  1.129  christos 	error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
   1969   1.52  christos 	if (error)
   1970   1.29       cgd 		return (error);
   1971   1.29       cgd 	vp = nd.ni_vp;
   1972   1.29       cgd 
   1973  1.169   hannken 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
   1974  1.169   hannken 		VOP_UNLOCK(vp, 0);
   1975  1.169   hannken 		if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
   1976  1.169   hannken 			return (error);
   1977  1.169   hannken 		if ((error = vn_start_write(NULL, &mp,
   1978  1.169   hannken 		    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
   1979  1.169   hannken 			return (error);
   1980  1.169   hannken 		goto restart;
   1981  1.169   hannken 	}
   1982  1.169   hannken 
   1983   1.29       cgd 	/* Don't dump to non-regular files or files with links. */
   1984   1.29       cgd 	if (vp->v_type != VREG ||
   1985  1.143      fvdl 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
   1986   1.59       cgd 		error = EINVAL;
   1987   1.29       cgd 		goto out;
   1988   1.29       cgd 	}
   1989   1.29       cgd 	VATTR_NULL(&vattr);
   1990   1.29       cgd 	vattr.va_size = 0;
   1991  1.143      fvdl 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1992  1.143      fvdl 	VOP_SETATTR(vp, &vattr, cred, p);
   1993   1.29       cgd 	p->p_acflag |= ACORE;
   1994   1.95       eeh 
   1995  1.118   thorpej 	/* Now dump the actual core file. */
   1996  1.130   thorpej 	error = (*p->p_execsw->es_coredump)(l, vp, cred);
   1997  1.112     lukem  out:
   1998   1.71      fvdl 	VOP_UNLOCK(vp, 0);
   1999  1.169   hannken 	vn_finished_write(mp, 0);
   2000  1.143      fvdl 	error1 = vn_close(vp, FWRITE, cred, p);
   2001   1.29       cgd 	if (error == 0)
   2002   1.29       cgd 		error = error1;
   2003   1.29       cgd 	return (error);
   2004   1.29       cgd }
   2005   1.29       cgd 
   2006   1.29       cgd /*
   2007   1.29       cgd  * Nonexistent system call-- signal process (may want to handle it).
   2008   1.29       cgd  * Flag error in case process won't see signal immediately (blocked or ignored).
   2009   1.29       cgd  */
   2010   1.29       cgd /* ARGSUSED */
   2011   1.29       cgd int
   2012  1.130   thorpej sys_nosys(struct lwp *l, void *v, register_t *retval)
   2013   1.29       cgd {
   2014  1.130   thorpej 	struct proc 	*p;
   2015   1.29       cgd 
   2016  1.130   thorpej 	p = l->l_proc;
   2017   1.29       cgd 	psignal(p, SIGSYS);
   2018   1.36       cgd 	return (ENOSYS);
   2019   1.94    bouyer }
   2020   1.94    bouyer 
   2021   1.94    bouyer static int
   2022  1.112     lukem build_corename(struct proc *p, char dst[MAXPATHLEN])
   2023   1.94    bouyer {
   2024  1.112     lukem 	const char	*s;
   2025  1.112     lukem 	char		*d, *end;
   2026  1.112     lukem 	int		i;
   2027  1.100  sommerfe 
   2028  1.107     enami 	for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
   2029   1.94    bouyer 	    *s != '\0'; s++) {
   2030   1.94    bouyer 		if (*s == '%') {
   2031  1.107     enami 			switch (*(s + 1)) {
   2032   1.94    bouyer 			case 'n':
   2033  1.107     enami 				i = snprintf(d, end - d, "%s", p->p_comm);
   2034   1.94    bouyer 				break;
   2035   1.94    bouyer 			case 'p':
   2036  1.107     enami 				i = snprintf(d, end - d, "%d", p->p_pid);
   2037   1.94    bouyer 				break;
   2038   1.94    bouyer 			case 'u':
   2039  1.134       dsl 				i = snprintf(d, end - d, "%.*s",
   2040  1.134       dsl 				    (int)sizeof p->p_pgrp->pg_session->s_login,
   2041  1.100  sommerfe 				    p->p_pgrp->pg_session->s_login);
   2042   1.94    bouyer 				break;
   2043   1.94    bouyer 			case 't':
   2044  1.107     enami 				i = snprintf(d, end - d, "%ld",
   2045  1.100  sommerfe 				    p->p_stats->p_start.tv_sec);
   2046   1.94    bouyer 				break;
   2047   1.94    bouyer 			default:
   2048   1.94    bouyer 				goto copy;
   2049   1.94    bouyer 			}
   2050   1.94    bouyer 			d += i;
   2051   1.94    bouyer 			s++;
   2052   1.94    bouyer 		} else {
   2053  1.112     lukem  copy:			*d = *s;
   2054   1.94    bouyer 			d++;
   2055   1.94    bouyer 		}
   2056  1.107     enami 		if (d >= end)
   2057  1.107     enami 			return (ENAMETOOLONG);
   2058   1.94    bouyer 	}
   2059   1.94    bouyer 	*d = '\0';
   2060  1.130   thorpej 	return 0;
   2061  1.130   thorpej }
   2062  1.130   thorpej 
   2063  1.130   thorpej void
   2064  1.130   thorpej getucontext(struct lwp *l, ucontext_t *ucp)
   2065  1.130   thorpej {
   2066  1.130   thorpej 	struct proc	*p;
   2067  1.130   thorpej 
   2068  1.130   thorpej 	p = l->l_proc;
   2069  1.130   thorpej 
   2070  1.130   thorpej 	ucp->uc_flags = 0;
   2071  1.130   thorpej 	ucp->uc_link = l->l_ctxlink;
   2072  1.130   thorpej 
   2073  1.130   thorpej 	(void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
   2074  1.130   thorpej 	ucp->uc_flags |= _UC_SIGMASK;
   2075  1.130   thorpej 
   2076  1.130   thorpej 	/*
   2077  1.130   thorpej 	 * The (unsupplied) definition of the `current execution stack'
   2078  1.130   thorpej 	 * in the System V Interface Definition appears to allow returning
   2079  1.130   thorpej 	 * the main context stack.
   2080  1.130   thorpej 	 */
   2081  1.130   thorpej 	if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
   2082  1.130   thorpej 		ucp->uc_stack.ss_sp = (void *)USRSTACK;
   2083  1.130   thorpej 		ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
   2084  1.130   thorpej 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
   2085  1.130   thorpej 	} else {
   2086  1.130   thorpej 		/* Simply copy alternate signal execution stack. */
   2087  1.130   thorpej 		ucp->uc_stack = p->p_sigctx.ps_sigstk;
   2088  1.130   thorpej 	}
   2089  1.130   thorpej 	ucp->uc_flags |= _UC_STACK;
   2090  1.130   thorpej 
   2091  1.130   thorpej 	cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
   2092  1.130   thorpej }
   2093  1.130   thorpej 
   2094  1.130   thorpej /* ARGSUSED */
   2095  1.130   thorpej int
   2096  1.130   thorpej sys_getcontext(struct lwp *l, void *v, register_t *retval)
   2097  1.130   thorpej {
   2098  1.130   thorpej 	struct sys_getcontext_args /* {
   2099  1.130   thorpej 		syscallarg(struct __ucontext *) ucp;
   2100  1.130   thorpej 	} */ *uap = v;
   2101  1.130   thorpej 	ucontext_t uc;
   2102  1.130   thorpej 
   2103  1.130   thorpej 	getucontext(l, &uc);
   2104  1.130   thorpej 
   2105  1.130   thorpej 	return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
   2106  1.130   thorpej }
   2107  1.130   thorpej 
   2108  1.130   thorpej int
   2109  1.130   thorpej setucontext(struct lwp *l, const ucontext_t *ucp)
   2110  1.130   thorpej {
   2111  1.130   thorpej 	struct proc	*p;
   2112  1.130   thorpej 	int		error;
   2113  1.130   thorpej 
   2114  1.130   thorpej 	p = l->l_proc;
   2115  1.130   thorpej 	if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
   2116  1.130   thorpej 		return (error);
   2117  1.130   thorpej 	l->l_ctxlink = ucp->uc_link;
   2118  1.130   thorpej 	/*
   2119  1.130   thorpej 	 * We might want to take care of the stack portion here but currently
   2120  1.130   thorpej 	 * don't; see the comment in getucontext().
   2121  1.130   thorpej 	 */
   2122  1.130   thorpej 	if ((ucp->uc_flags & _UC_SIGMASK) != 0)
   2123  1.130   thorpej 		sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
   2124  1.130   thorpej 
   2125  1.130   thorpej 	return 0;
   2126  1.130   thorpej }
   2127  1.130   thorpej 
   2128  1.130   thorpej /* ARGSUSED */
   2129  1.130   thorpej int
   2130  1.130   thorpej sys_setcontext(struct lwp *l, void *v, register_t *retval)
   2131  1.130   thorpej {
   2132  1.130   thorpej 	struct sys_setcontext_args /* {
   2133  1.130   thorpej 		syscallarg(const ucontext_t *) ucp;
   2134  1.130   thorpej 	} */ *uap = v;
   2135  1.130   thorpej 	ucontext_t uc;
   2136  1.130   thorpej 	int error;
   2137  1.130   thorpej 
   2138  1.130   thorpej 	if (SCARG(uap, ucp) == NULL)	/* i.e. end of uc_link chain */
   2139  1.130   thorpej 		exit1(l, W_EXITCODE(0, 0));
   2140  1.130   thorpej 	else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
   2141  1.130   thorpej 	    (error = setucontext(l, &uc)) != 0)
   2142  1.130   thorpej 		return (error);
   2143  1.130   thorpej 
   2144  1.130   thorpej 	return (EJUSTRETURN);
   2145  1.108  jdolecek }
   2146  1.130   thorpej 
   2147  1.135  jdolecek /*
   2148  1.135  jdolecek  * sigtimedwait(2) system call, used also for implementation
   2149  1.135  jdolecek  * of sigwaitinfo() and sigwait().
   2150  1.135  jdolecek  *
   2151  1.135  jdolecek  * This only handles single LWP in signal wait. libpthread provides
   2152  1.135  jdolecek  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
   2153  1.135  jdolecek  */
   2154  1.135  jdolecek int
   2155  1.135  jdolecek sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
   2156  1.135  jdolecek {
   2157  1.135  jdolecek 	struct sys___sigtimedwait_args /* {
   2158  1.135  jdolecek 		syscallarg(const sigset_t *) set;
   2159  1.135  jdolecek 		syscallarg(siginfo_t *) info;
   2160  1.135  jdolecek 		syscallarg(struct timespec *) timeout;
   2161  1.135  jdolecek 	} */ *uap = v;
   2162  1.173  jdolecek 	sigset_t *waitset, twaitset;
   2163  1.135  jdolecek 	struct proc *p = l->l_proc;
   2164  1.135  jdolecek 	int error, signum, s;
   2165  1.135  jdolecek 	int timo = 0;
   2166  1.135  jdolecek 	struct timeval tvstart;
   2167  1.135  jdolecek 	struct timespec ts;
   2168  1.171  jdolecek 	ksiginfo_t *ksi;
   2169  1.135  jdolecek 
   2170  1.173  jdolecek 	MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK);
   2171  1.173  jdolecek 
   2172  1.173  jdolecek 	if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t)))) {
   2173  1.173  jdolecek 		FREE(waitset, M_TEMP);
   2174  1.135  jdolecek 		return (error);
   2175  1.173  jdolecek 	}
   2176  1.135  jdolecek 
   2177  1.135  jdolecek 	/*
   2178  1.135  jdolecek 	 * Silently ignore SA_CANTMASK signals. psignal1() would
   2179  1.135  jdolecek 	 * ignore SA_CANTMASK signals in waitset, we do this
   2180  1.135  jdolecek 	 * only for the below siglist check.
   2181  1.135  jdolecek 	 */
   2182  1.173  jdolecek 	sigminusset(&sigcantmask, waitset);
   2183  1.135  jdolecek 
   2184  1.135  jdolecek 	/*
   2185  1.135  jdolecek 	 * First scan siglist and check if there is signal from
   2186  1.135  jdolecek 	 * our waitset already pending.
   2187  1.135  jdolecek 	 */
   2188  1.173  jdolecek 	twaitset = *waitset;
   2189  1.135  jdolecek 	__sigandset(&p->p_sigctx.ps_siglist, &twaitset);
   2190  1.135  jdolecek 	if ((signum = firstsig(&twaitset))) {
   2191  1.135  jdolecek 		/* found pending signal */
   2192  1.135  jdolecek 		sigdelset(&p->p_sigctx.ps_siglist, signum);
   2193  1.171  jdolecek 		ksi = ksiginfo_get(p, signum);
   2194  1.171  jdolecek 		if (!ksi) {
   2195  1.171  jdolecek 			/* No queued siginfo, manufacture one */
   2196  1.171  jdolecek 			ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
   2197  1.171  jdolecek 			KSI_INIT(ksi);
   2198  1.171  jdolecek 			ksi->ksi_info._signo = signum;
   2199  1.171  jdolecek 			ksi->ksi_info._code = SI_USER;
   2200  1.171  jdolecek 		}
   2201  1.171  jdolecek 
   2202  1.135  jdolecek 		goto sig;
   2203  1.135  jdolecek 	}
   2204  1.135  jdolecek 
   2205  1.135  jdolecek 	/*
   2206  1.135  jdolecek 	 * Calculate timeout, if it was specified.
   2207  1.135  jdolecek 	 */
   2208  1.135  jdolecek 	if (SCARG(uap, timeout)) {
   2209  1.135  jdolecek 		uint64_t ms;
   2210  1.135  jdolecek 
   2211  1.135  jdolecek 		if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))))
   2212  1.135  jdolecek 			return (error);
   2213  1.135  jdolecek 
   2214  1.135  jdolecek 		ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
   2215  1.135  jdolecek 		timo = mstohz(ms);
   2216  1.135  jdolecek 		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
   2217  1.135  jdolecek 			timo = 1;
   2218  1.135  jdolecek 		if (timo <= 0)
   2219  1.135  jdolecek 			return (EAGAIN);
   2220  1.135  jdolecek 
   2221  1.135  jdolecek 		/*
   2222  1.135  jdolecek 		 * Remember current mono_time, it would be used in
   2223  1.135  jdolecek 		 * ECANCELED/ERESTART case.
   2224  1.135  jdolecek 		 */
   2225  1.135  jdolecek 		s = splclock();
   2226  1.135  jdolecek 		tvstart = mono_time;
   2227  1.135  jdolecek 		splx(s);
   2228  1.135  jdolecek 	}
   2229  1.135  jdolecek 
   2230  1.135  jdolecek 	/*
   2231  1.173  jdolecek 	 * Setup ps_sigwait list. Pass pointer to malloced memory
   2232  1.173  jdolecek 	 * here; it's not possible to pass pointer to a structure
   2233  1.173  jdolecek 	 * on current process's stack, the current process might
   2234  1.173  jdolecek 	 * be swapped out at the time the signal would get delivered.
   2235  1.135  jdolecek 	 */
   2236  1.171  jdolecek 	ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
   2237  1.171  jdolecek 	p->p_sigctx.ps_sigwaited = ksi;
   2238  1.173  jdolecek 	p->p_sigctx.ps_sigwait = waitset;
   2239  1.135  jdolecek 
   2240  1.135  jdolecek 	/*
   2241  1.135  jdolecek 	 * Wait for signal to arrive. We can either be woken up or
   2242  1.135  jdolecek 	 * time out.
   2243  1.135  jdolecek 	 */
   2244  1.135  jdolecek 	error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
   2245  1.135  jdolecek 
   2246  1.135  jdolecek 	/*
   2247  1.171  jdolecek 	 * Need to find out if we woke as a result of lwp_wakeup()
   2248  1.171  jdolecek 	 * or a signal outside our wait set.
   2249  1.135  jdolecek 	 */
   2250  1.171  jdolecek 	if (error == EINTR && p->p_sigctx.ps_sigwaited
   2251  1.171  jdolecek 	    && !firstsig(&p->p_sigctx.ps_siglist)) {
   2252  1.171  jdolecek 		/* wakeup via _lwp_wakeup() */
   2253  1.171  jdolecek 		error = ECANCELED;
   2254  1.171  jdolecek 	} else if (!error && p->p_sigctx.ps_sigwaited) {
   2255  1.171  jdolecek 		/* spurious wakeup - arrange for syscall restart */
   2256  1.171  jdolecek 		error = ERESTART;
   2257  1.171  jdolecek 		goto fail;
   2258  1.135  jdolecek 	}
   2259  1.135  jdolecek 
   2260  1.135  jdolecek 	/*
   2261  1.171  jdolecek 	 * On error, clear sigwait indication. psignal1() clears it
   2262  1.135  jdolecek 	 * in !error case.
   2263  1.135  jdolecek 	 */
   2264  1.135  jdolecek 	if (error) {
   2265  1.171  jdolecek 		p->p_sigctx.ps_sigwaited = NULL;
   2266  1.135  jdolecek 
   2267  1.135  jdolecek 		/*
   2268  1.135  jdolecek 		 * If the sleep was interrupted (either by signal or wakeup),
   2269  1.135  jdolecek 		 * update the timeout and copyout new value back.
   2270  1.135  jdolecek 		 * It would be used when the syscall would be restarted
   2271  1.135  jdolecek 		 * or called again.
   2272  1.135  jdolecek 		 */
   2273  1.135  jdolecek 		if (timo && (error == ERESTART || error == ECANCELED)) {
   2274  1.135  jdolecek 			struct timeval tvnow, tvtimo;
   2275  1.135  jdolecek 			int err;
   2276  1.135  jdolecek 
   2277  1.135  jdolecek 			s = splclock();
   2278  1.135  jdolecek 			tvnow = mono_time;
   2279  1.135  jdolecek 			splx(s);
   2280  1.135  jdolecek 
   2281  1.135  jdolecek 			TIMESPEC_TO_TIMEVAL(&tvtimo, &ts);
   2282  1.135  jdolecek 
   2283  1.135  jdolecek 			/* compute how much time has passed since start */
   2284  1.135  jdolecek 			timersub(&tvnow, &tvstart, &tvnow);
   2285  1.135  jdolecek 			/* substract passed time from timeout */
   2286  1.135  jdolecek 			timersub(&tvtimo, &tvnow, &tvtimo);
   2287  1.135  jdolecek 
   2288  1.171  jdolecek 			if (tvtimo.tv_sec < 0) {
   2289  1.171  jdolecek 				error = EAGAIN;
   2290  1.171  jdolecek 				goto fail;
   2291  1.171  jdolecek 			}
   2292  1.171  jdolecek 
   2293  1.135  jdolecek 			TIMEVAL_TO_TIMESPEC(&tvtimo, &ts);
   2294  1.135  jdolecek 
   2295  1.135  jdolecek 			/* copy updated timeout to userland */
   2296  1.171  jdolecek 			if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts)))) {
   2297  1.171  jdolecek 				error = err;
   2298  1.171  jdolecek 				goto fail;
   2299  1.171  jdolecek 			}
   2300  1.135  jdolecek 		}
   2301  1.135  jdolecek 
   2302  1.171  jdolecek 		goto fail;
   2303  1.135  jdolecek 	}
   2304  1.135  jdolecek 
   2305  1.135  jdolecek 	/*
   2306  1.135  jdolecek 	 * If a signal from the wait set arrived, copy it to userland.
   2307  1.171  jdolecek 	 * Copy only the used part of siginfo, the padding part is
   2308  1.171  jdolecek 	 * left unchanged (userland is not supposed to touch it anyway).
   2309  1.135  jdolecek 	 */
   2310  1.135  jdolecek  sig:
   2311  1.171  jdolecek 	error = copyout(&ksi->ksi_info, SCARG(uap, info), sizeof(ksi->ksi_info));
   2312  1.135  jdolecek 
   2313  1.171  jdolecek  fail:
   2314  1.173  jdolecek 	FREE(waitset, M_TEMP);
   2315  1.171  jdolecek 	pool_put(&ksiginfo_pool, ksi);
   2316  1.171  jdolecek 	p->p_sigctx.ps_sigwait = NULL;
   2317  1.135  jdolecek 
   2318  1.171  jdolecek 	return (error);
   2319  1.135  jdolecek }
   2320  1.108  jdolecek 
   2321  1.108  jdolecek /*
   2322  1.108  jdolecek  * Returns true if signal is ignored or masked for passed process.
   2323  1.108  jdolecek  */
   2324  1.108  jdolecek int
   2325  1.112     lukem sigismasked(struct proc *p, int sig)
   2326  1.108  jdolecek {
   2327  1.112     lukem 
   2328  1.117     enami 	return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
   2329  1.117     enami 	    sigismember(&p->p_sigctx.ps_sigmask, sig));
   2330   1.29       cgd }
   2331  1.126  jdolecek 
   2332  1.126  jdolecek static int
   2333  1.126  jdolecek filt_sigattach(struct knote *kn)
   2334  1.126  jdolecek {
   2335  1.126  jdolecek 	struct proc *p = curproc;
   2336  1.126  jdolecek 
   2337  1.126  jdolecek 	kn->kn_ptr.p_proc = p;
   2338  1.126  jdolecek 	kn->kn_flags |= EV_CLEAR;               /* automatically set */
   2339  1.126  jdolecek 
   2340  1.126  jdolecek 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
   2341  1.126  jdolecek 
   2342  1.126  jdolecek 	return (0);
   2343  1.126  jdolecek }
   2344  1.126  jdolecek 
   2345  1.126  jdolecek static void
   2346  1.126  jdolecek filt_sigdetach(struct knote *kn)
   2347  1.126  jdolecek {
   2348  1.126  jdolecek 	struct proc *p = kn->kn_ptr.p_proc;
   2349  1.126  jdolecek 
   2350  1.126  jdolecek 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
   2351  1.126  jdolecek }
   2352  1.126  jdolecek 
   2353  1.126  jdolecek /*
   2354  1.126  jdolecek  * signal knotes are shared with proc knotes, so we apply a mask to
   2355  1.126  jdolecek  * the hint in order to differentiate them from process hints.  This
   2356  1.126  jdolecek  * could be avoided by using a signal-specific knote list, but probably
   2357  1.126  jdolecek  * isn't worth the trouble.
   2358  1.126  jdolecek  */
   2359  1.126  jdolecek static int
   2360  1.126  jdolecek filt_signal(struct knote *kn, long hint)
   2361  1.126  jdolecek {
   2362  1.126  jdolecek 
   2363  1.126  jdolecek 	if (hint & NOTE_SIGNAL) {
   2364  1.126  jdolecek 		hint &= ~NOTE_SIGNAL;
   2365  1.126  jdolecek 
   2366  1.126  jdolecek 		if (kn->kn_id == hint)
   2367  1.126  jdolecek 			kn->kn_data++;
   2368  1.126  jdolecek 	}
   2369  1.126  jdolecek 	return (kn->kn_data != 0);
   2370  1.126  jdolecek }
   2371  1.126  jdolecek 
   2372  1.126  jdolecek const struct filterops sig_filtops = {
   2373  1.126  jdolecek 	0, filt_sigattach, filt_sigdetach, filt_signal
   2374  1.126  jdolecek };
   2375