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