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