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