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