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