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