1 /*	$NetBSD: kern_sig.c,v 1.327 2016/04/28 00:37:39 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1989, 1991, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  * (c) UNIX System Laboratories, Inc.
36  * All or some portions of this file are derived from material licensed
37  * to the University of California by American Telephone and Telegraph
38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39  * the permission of UNIX System Laboratories, Inc.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
66  */
67 
68 /*
69  * Signal subsystem.
70  */
71 
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.327 2016/04/28 00:37:39 christos Exp $");
74 
75 #include "opt_ptrace.h"
76 #include "opt_dtrace.h"
77 #include "opt_compat_sunos.h"
78 #include "opt_compat_netbsd.h"
79 #include "opt_compat_netbsd32.h"
80 #include "opt_pax.h"
81 
82 #define	SIGPROP		/* include signal properties table */
83 #include <sys/param.h>
84 #include <sys/signalvar.h>
85 #include <sys/proc.h>
86 #include <sys/systm.h>
87 #include <sys/wait.h>
88 #include <sys/ktrace.h>
89 #include <sys/syslog.h>
90 #include <sys/filedesc.h>
91 #include <sys/file.h>
92 #include <sys/pool.h>
93 #include <sys/ucontext.h>
94 #include <sys/exec.h>
95 #include <sys/kauth.h>
96 #include <sys/acct.h>
97 #include <sys/callout.h>
98 #include <sys/atomic.h>
99 #include <sys/cpu.h>
100 #include <sys/module.h>
101 #include <sys/sdt.h>
102 
103 #ifdef PAX_SEGVGUARD
104 #include <sys/pax.h>
105 #endif /* PAX_SEGVGUARD */
106 
107 #include <uvm/uvm_extern.h>
108 
109 static pool_cache_t	sigacts_cache	__read_mostly;
110 static pool_cache_t	ksiginfo_cache	__read_mostly;
111 static callout_t	proc_stop_ch	__cacheline_aligned;
112 
113 sigset_t		contsigmask	__cacheline_aligned;
114 static sigset_t		stopsigmask	__cacheline_aligned;
115 sigset_t		sigcantmask	__cacheline_aligned;
116 
117 static void	ksiginfo_exechook(struct proc *, void *);
118 static void	proc_stop_callout(void *);
119 static int	sigchecktrace(void);
120 static int	sigpost(struct lwp *, sig_t, int, int);
121 static void	sigput(sigpend_t *, struct proc *, ksiginfo_t *);
122 static int	sigunwait(struct proc *, const ksiginfo_t *);
123 static void	sigswitch(bool, int, int);
124 
125 static void	sigacts_poolpage_free(struct pool *, void *);
126 static void	*sigacts_poolpage_alloc(struct pool *, int);
127 
128 void (*sendsig_sigcontext_vec)(const struct ksiginfo *, const sigset_t *);
129 int (*coredump_vec)(struct lwp *, const char *) =
130     (int (*)(struct lwp *, const char *))enosys;
131 
132 /*
133  * DTrace SDT provider definitions
134  */
135 SDT_PROVIDER_DECLARE(proc);
136 SDT_PROBE_DEFINE3(proc, kernel, , signal__send,
137     "struct lwp *", 	/* target thread */
138     "struct proc *", 	/* target process */
139     "int");		/* signal */
140 SDT_PROBE_DEFINE3(proc, kernel, , signal__discard,
141     "struct lwp *",	/* target thread */
142     "struct proc *",	/* target process */
143     "int");  		/* signal */
144 SDT_PROBE_DEFINE3(proc, kernel, , signal__handle,
145     "int", 		/* signal */
146     "ksiginfo_t *", 	/* signal info */
147     "void (*)(void)");	/* handler address */
148 
149 
150 static struct pool_allocator sigactspool_allocator = {
151 	.pa_alloc = sigacts_poolpage_alloc,
152 	.pa_free = sigacts_poolpage_free
153 };
154 
155 #ifdef DEBUG
156 int	kern_logsigexit = 1;
157 #else
158 int	kern_logsigexit = 0;
159 #endif
160 
161 static const char logcoredump[] =
162     "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
163 static const char lognocoredump[] =
164     "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
165 
166 static kauth_listener_t signal_listener;
167 
168 static int
signal_listener_cb(kauth_cred_t cred,kauth_action_t action,void * cookie,void * arg0,void * arg1,void * arg2,void * arg3)169 signal_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
170     void *arg0, void *arg1, void *arg2, void *arg3)
171 {
172 	struct proc *p;
173 	int result, signum;
174 
175 	result = KAUTH_RESULT_DEFER;
176 	p = arg0;
177 	signum = (int)(unsigned long)arg1;
178 
179 	if (action != KAUTH_PROCESS_SIGNAL)
180 		return result;
181 
182 	if (kauth_cred_uidmatch(cred, p->p_cred) ||
183 	    (signum == SIGCONT && (curproc->p_session == p->p_session)))
184 		result = KAUTH_RESULT_ALLOW;
185 
186 	return result;
187 }
188 
189 /*
190  * signal_init:
191  *
192  *	Initialize global signal-related data structures.
193  */
194 void
signal_init(void)195 signal_init(void)
196 {
197 
198 	sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2;
199 
200 	sigacts_cache = pool_cache_init(sizeof(struct sigacts), 0, 0, 0,
201 	    "sigacts", sizeof(struct sigacts) > PAGE_SIZE ?
202 	    &sigactspool_allocator : NULL, IPL_NONE, NULL, NULL, NULL);
203 	ksiginfo_cache = pool_cache_init(sizeof(ksiginfo_t), 0, 0, 0,
204 	    "ksiginfo", NULL, IPL_VM, NULL, NULL, NULL);
205 
206 	exechook_establish(ksiginfo_exechook, NULL);
207 
208 	callout_init(&proc_stop_ch, CALLOUT_MPSAFE);
209 	callout_setfunc(&proc_stop_ch, proc_stop_callout, NULL);
210 
211 	signal_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
212 	    signal_listener_cb, NULL);
213 }
214 
215 /*
216  * sigacts_poolpage_alloc:
217  *
218  *	Allocate a page for the sigacts memory pool.
219  */
220 static void *
sigacts_poolpage_alloc(struct pool * pp,int flags)221 sigacts_poolpage_alloc(struct pool *pp, int flags)
222 {
223 
224 	return (void *)uvm_km_alloc(kernel_map,
225 	    PAGE_SIZE * 2, PAGE_SIZE * 2,
226 	    ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
227 	    | UVM_KMF_WIRED);
228 }
229 
230 /*
231  * sigacts_poolpage_free:
232  *
233  *	Free a page on behalf of the sigacts memory pool.
234  */
235 static void
sigacts_poolpage_free(struct pool * pp,void * v)236 sigacts_poolpage_free(struct pool *pp, void *v)
237 {
238 
239 	uvm_km_free(kernel_map, (vaddr_t)v, PAGE_SIZE * 2, UVM_KMF_WIRED);
240 }
241 
242 /*
243  * sigactsinit:
244  *
245  *	Create an initial sigacts structure, using the same signal state
246  *	as of specified process.  If 'share' is set, share the sigacts by
247  *	holding a reference, otherwise just copy it from parent.
248  */
249 struct sigacts *
sigactsinit(struct proc * pp,int share)250 sigactsinit(struct proc *pp, int share)
251 {
252 	struct sigacts *ps = pp->p_sigacts, *ps2;
253 
254 	if (__predict_false(share)) {
255 		atomic_inc_uint(&ps->sa_refcnt);
256 		return ps;
257 	}
258 	ps2 = pool_cache_get(sigacts_cache, PR_WAITOK);
259 	mutex_init(&ps2->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
260 	ps2->sa_refcnt = 1;
261 
262 	mutex_enter(&ps->sa_mutex);
263 	memcpy(ps2->sa_sigdesc, ps->sa_sigdesc, sizeof(ps2->sa_sigdesc));
264 	mutex_exit(&ps->sa_mutex);
265 	return ps2;
266 }
267 
268 /*
269  * sigactsunshare:
270  *
271  *	Make this process not share its sigacts, maintaining all signal state.
272  */
273 void
sigactsunshare(struct proc * p)274 sigactsunshare(struct proc *p)
275 {
276 	struct sigacts *ps, *oldps = p->p_sigacts;
277 
278 	if (__predict_true(oldps->sa_refcnt == 1))
279 		return;
280 
281 	ps = pool_cache_get(sigacts_cache, PR_WAITOK);
282 	mutex_init(&ps->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
283 	memcpy(ps->sa_sigdesc, oldps->sa_sigdesc, sizeof(ps->sa_sigdesc));
284 	ps->sa_refcnt = 1;
285 
286 	p->p_sigacts = ps;
287 	sigactsfree(oldps);
288 }
289 
290 /*
291  * sigactsfree;
292  *
293  *	Release a sigacts structure.
294  */
295 void
sigactsfree(struct sigacts * ps)296 sigactsfree(struct sigacts *ps)
297 {
298 
299 	if (atomic_dec_uint_nv(&ps->sa_refcnt) == 0) {
300 		mutex_destroy(&ps->sa_mutex);
301 		pool_cache_put(sigacts_cache, ps);
302 	}
303 }
304 
305 /*
306  * siginit:
307  *
308  *	Initialize signal state for process 0; set to ignore signals that
309  *	are ignored by default and disable the signal stack.  Locking not
310  *	required as the system is still cold.
311  */
312 void
siginit(struct proc * p)313 siginit(struct proc *p)
314 {
315 	struct lwp *l;
316 	struct sigacts *ps;
317 	int signo, prop;
318 
319 	ps = p->p_sigacts;
320 	sigemptyset(&contsigmask);
321 	sigemptyset(&stopsigmask);
322 	sigemptyset(&sigcantmask);
323 	for (signo = 1; signo < NSIG; signo++) {
324 		prop = sigprop[signo];
325 		if (prop & SA_CONT)
326 			sigaddset(&contsigmask, signo);
327 		if (prop & SA_STOP)
328 			sigaddset(&stopsigmask, signo);
329 		if (prop & SA_CANTMASK)
330 			sigaddset(&sigcantmask, signo);
331 		if (prop & SA_IGNORE && signo != SIGCONT)
332 			sigaddset(&p->p_sigctx.ps_sigignore, signo);
333 		sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
334 		SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
335 	}
336 	sigemptyset(&p->p_sigctx.ps_sigcatch);
337 	p->p_sflag &= ~PS_NOCLDSTOP;
338 
339 	ksiginfo_queue_init(&p->p_sigpend.sp_info);
340 	sigemptyset(&p->p_sigpend.sp_set);
341 
342 	/*
343 	 * Reset per LWP state.
344 	 */
345 	l = LIST_FIRST(&p->p_lwps);
346 	l->l_sigwaited = NULL;
347 	l->l_sigstk.ss_flags = SS_DISABLE;
348 	l->l_sigstk.ss_size = 0;
349 	l->l_sigstk.ss_sp = 0;
350 	ksiginfo_queue_init(&l->l_sigpend.sp_info);
351 	sigemptyset(&l->l_sigpend.sp_set);
352 
353 	/* One reference. */
354 	ps->sa_refcnt = 1;
355 }
356 
357 /*
358  * execsigs:
359  *
360  *	Reset signals for an exec of the specified process.
361  */
362 void
execsigs(struct proc * p)363 execsigs(struct proc *p)
364 {
365 	struct sigacts *ps;
366 	struct lwp *l;
367 	int signo, prop;
368 	sigset_t tset;
369 	ksiginfoq_t kq;
370 
371 	KASSERT(p->p_nlwps == 1);
372 
373 	sigactsunshare(p);
374 	ps = p->p_sigacts;
375 
376 	/*
377 	 * Reset caught signals.  Held signals remain held through
378 	 * l->l_sigmask (unless they were caught, and are now ignored
379 	 * by default).
380 	 *
381 	 * No need to lock yet, the process has only one LWP and
382 	 * at this point the sigacts are private to the process.
383 	 */
384 	sigemptyset(&tset);
385 	for (signo = 1; signo < NSIG; signo++) {
386 		if (sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
387 			prop = sigprop[signo];
388 			if (prop & SA_IGNORE) {
389 				if ((prop & SA_CONT) == 0)
390 					sigaddset(&p->p_sigctx.ps_sigignore,
391 					    signo);
392 				sigaddset(&tset, signo);
393 			}
394 			SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
395 		}
396 		sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
397 		SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
398 	}
399 	ksiginfo_queue_init(&kq);
400 
401 	mutex_enter(p->p_lock);
402 	sigclearall(p, &tset, &kq);
403 	sigemptyset(&p->p_sigctx.ps_sigcatch);
404 
405 	/*
406 	 * Reset no zombies if child dies flag as Solaris does.
407 	 */
408 	p->p_flag &= ~(PK_NOCLDWAIT | PK_CLDSIGIGN);
409 	if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN)
410 		SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL;
411 
412 	/*
413 	 * Reset per-LWP state.
414 	 */
415 	l = LIST_FIRST(&p->p_lwps);
416 	l->l_sigwaited = NULL;
417 	l->l_sigstk.ss_flags = SS_DISABLE;
418 	l->l_sigstk.ss_size = 0;
419 	l->l_sigstk.ss_sp = 0;
420 	ksiginfo_queue_init(&l->l_sigpend.sp_info);
421 	sigemptyset(&l->l_sigpend.sp_set);
422 	mutex_exit(p->p_lock);
423 
424 	ksiginfo_queue_drain(&kq);
425 }
426 
427 /*
428  * ksiginfo_exechook:
429  *
430  *	Free all pending ksiginfo entries from a process on exec.
431  *	Additionally, drain any unused ksiginfo structures in the
432  *	system back to the pool.
433  *
434  *	XXX This should not be a hook, every process has signals.
435  */
436 static void
ksiginfo_exechook(struct proc * p,void * v)437 ksiginfo_exechook(struct proc *p, void *v)
438 {
439 	ksiginfoq_t kq;
440 
441 	ksiginfo_queue_init(&kq);
442 
443 	mutex_enter(p->p_lock);
444 	sigclearall(p, NULL, &kq);
445 	mutex_exit(p->p_lock);
446 
447 	ksiginfo_queue_drain(&kq);
448 }
449 
450 /*
451  * ksiginfo_alloc:
452  *
453  *	Allocate a new ksiginfo structure from the pool, and optionally copy
454  *	an existing one.  If the existing ksiginfo_t is from the pool, and
455  *	has not been queued somewhere, then just return it.  Additionally,
456  *	if the existing ksiginfo_t does not contain any information beyond
457  *	the signal number, then just return it.
458  */
459 ksiginfo_t *
ksiginfo_alloc(struct proc * p,ksiginfo_t * ok,int flags)460 ksiginfo_alloc(struct proc *p, ksiginfo_t *ok, int flags)
461 {
462 	ksiginfo_t *kp;
463 
464 	if (ok != NULL) {
465 		if ((ok->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) ==
466 		    KSI_FROMPOOL)
467 			return ok;
468 		if (KSI_EMPTY_P(ok))
469 			return ok;
470 	}
471 
472 	kp = pool_cache_get(ksiginfo_cache, flags);
473 	if (kp == NULL) {
474 #ifdef DIAGNOSTIC
475 		printf("Out of memory allocating ksiginfo for pid %d\n",
476 		    p->p_pid);
477 #endif
478 		return NULL;
479 	}
480 
481 	if (ok != NULL) {
482 		memcpy(kp, ok, sizeof(*kp));
483 		kp->ksi_flags &= ~KSI_QUEUED;
484 	} else
485 		KSI_INIT_EMPTY(kp);
486 
487 	kp->ksi_flags |= KSI_FROMPOOL;
488 
489 	return kp;
490 }
491 
492 /*
493  * ksiginfo_free:
494  *
495  *	If the given ksiginfo_t is from the pool and has not been queued,
496  *	then free it.
497  */
498 void
ksiginfo_free(ksiginfo_t * kp)499 ksiginfo_free(ksiginfo_t *kp)
500 {
501 
502 	if ((kp->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) != KSI_FROMPOOL)
503 		return;
504 	pool_cache_put(ksiginfo_cache, kp);
505 }
506 
507 /*
508  * ksiginfo_queue_drain:
509  *
510  *	Drain a non-empty ksiginfo_t queue.
511  */
512 void
ksiginfo_queue_drain0(ksiginfoq_t * kq)513 ksiginfo_queue_drain0(ksiginfoq_t *kq)
514 {
515 	ksiginfo_t *ksi;
516 
517 	KASSERT(!TAILQ_EMPTY(kq));
518 
519 	while (!TAILQ_EMPTY(kq)) {
520 		ksi = TAILQ_FIRST(kq);
521 		TAILQ_REMOVE(kq, ksi, ksi_list);
522 		pool_cache_put(ksiginfo_cache, ksi);
523 	}
524 }
525 
526 static bool
siggetinfo(sigpend_t * sp,ksiginfo_t * out,int signo)527 siggetinfo(sigpend_t *sp, ksiginfo_t *out, int signo)
528 {
529 	ksiginfo_t *ksi;
530 
531 	if (sp == NULL)
532 		goto out;
533 
534 	/* Find siginfo and copy it out. */
535 	TAILQ_FOREACH(ksi, &sp->sp_info, ksi_list) {
536 		if (ksi->ksi_signo != signo)
537 			continue;
538 		TAILQ_REMOVE(&sp->sp_info, ksi, ksi_list);
539 		KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
540 		KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
541 		ksi->ksi_flags &= ~KSI_QUEUED;
542 		if (out != NULL) {
543 			memcpy(out, ksi, sizeof(*out));
544 			out->ksi_flags &= ~(KSI_FROMPOOL | KSI_QUEUED);
545 		}
546 		ksiginfo_free(ksi);	/* XXXSMP */
547 		return true;
548 	}
549 
550 out:
551 	/* If there is no siginfo, then manufacture it. */
552 	if (out != NULL) {
553 		KSI_INIT(out);
554 		out->ksi_info._signo = signo;
555 		out->ksi_info._code = SI_NOINFO;
556 	}
557 	return false;
558 }
559 
560 /*
561  * sigget:
562  *
563  *	Fetch the first pending signal from a set.  Optionally, also fetch
564  *	or manufacture a ksiginfo element.  Returns the number of the first
565  *	pending signal, or zero.
566  */
567 int
sigget(sigpend_t * sp,ksiginfo_t * out,int signo,const sigset_t * mask)568 sigget(sigpend_t *sp, ksiginfo_t *out, int signo, const sigset_t *mask)
569 {
570 	sigset_t tset;
571 
572 	/* If there's no pending set, the signal is from the debugger. */
573 	if (sp == NULL)
574 		goto out;
575 
576 	/* Construct mask from signo, and 'mask'. */
577 	if (signo == 0) {
578 		if (mask != NULL) {
579 			tset = *mask;
580 			__sigandset(&sp->sp_set, &tset);
581 		} else
582 			tset = sp->sp_set;
583 
584 		/* If there are no signals pending - return. */
585 		if ((signo = firstsig(&tset)) == 0)
586 			goto out;
587 	} else {
588 		KASSERT(sigismember(&sp->sp_set, signo));
589 	}
590 
591 	sigdelset(&sp->sp_set, signo);
592 out:
593 	(void)siggetinfo(sp, out, signo);
594 	return signo;
595 }
596 
597 /*
598  * sigput:
599  *
600  *	Append a new ksiginfo element to the list of pending ksiginfo's.
601  */
602 static void
sigput(sigpend_t * sp,struct proc * p,ksiginfo_t * ksi)603 sigput(sigpend_t *sp, struct proc *p, ksiginfo_t *ksi)
604 {
605 	ksiginfo_t *kp;
606 
607 	KASSERT(mutex_owned(p->p_lock));
608 	KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
609 
610 	sigaddset(&sp->sp_set, ksi->ksi_signo);
611 
612 	/*
613 	 * If there is no siginfo, we are done.
614 	 */
615 	if (KSI_EMPTY_P(ksi))
616 		return;
617 
618 	KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
619 
620 #ifdef notyet	/* XXX: QUEUING */
621 	if (ksi->ksi_signo < SIGRTMIN)
622 #endif
623 	{
624 		TAILQ_FOREACH(kp, &sp->sp_info, ksi_list) {
625 			if (kp->ksi_signo == ksi->ksi_signo) {
626 				KSI_COPY(ksi, kp);
627 				kp->ksi_flags |= KSI_QUEUED;
628 				return;
629 			}
630 		}
631 	}
632 
633 	ksi->ksi_flags |= KSI_QUEUED;
634 	TAILQ_INSERT_TAIL(&sp->sp_info, ksi, ksi_list);
635 }
636 
637 /*
638  * sigclear:
639  *
640  *	Clear all pending signals in the specified set.
641  */
642 void
sigclear(sigpend_t * sp,const sigset_t * mask,ksiginfoq_t * kq)643 sigclear(sigpend_t *sp, const sigset_t *mask, ksiginfoq_t *kq)
644 {
645 	ksiginfo_t *ksi, *next;
646 
647 	if (mask == NULL)
648 		sigemptyset(&sp->sp_set);
649 	else
650 		sigminusset(mask, &sp->sp_set);
651 
652 	TAILQ_FOREACH_SAFE(ksi, &sp->sp_info, ksi_list, next) {
653 		if (mask == NULL || sigismember(mask, ksi->ksi_signo)) {
654 			TAILQ_REMOVE(&sp->sp_info, ksi, ksi_list);
655 			KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
656 			KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
657 			TAILQ_INSERT_TAIL(kq, ksi, ksi_list);
658 		}
659 	}
660 }
661 
662 /*
663  * sigclearall:
664  *
665  *	Clear all pending signals in the specified set from a process and
666  *	its LWPs.
667  */
668 void
sigclearall(struct proc * p,const sigset_t * mask,ksiginfoq_t * kq)669 sigclearall(struct proc *p, const sigset_t *mask, ksiginfoq_t *kq)
670 {
671 	struct lwp *l;
672 
673 	KASSERT(mutex_owned(p->p_lock));
674 
675 	sigclear(&p->p_sigpend, mask, kq);
676 
677 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
678 		sigclear(&l->l_sigpend, mask, kq);
679 	}
680 }
681 
682 /*
683  * sigispending:
684  *
685  *	Return the first signal number if there are pending signals for the
686  *	current LWP.  May be called unlocked provided that LW_PENDSIG is set,
687  *	and that the signal has been posted to the appopriate queue before
688  *	LW_PENDSIG is set.
689  */
690 int
sigispending(struct lwp * l,int signo)691 sigispending(struct lwp *l, int signo)
692 {
693 	struct proc *p = l->l_proc;
694 	sigset_t tset;
695 
696 	membar_consumer();
697 
698 	tset = l->l_sigpend.sp_set;
699 	sigplusset(&p->p_sigpend.sp_set, &tset);
700 	sigminusset(&p->p_sigctx.ps_sigignore, &tset);
701 	sigminusset(&l->l_sigmask, &tset);
702 
703 	if (signo == 0) {
704 		return firstsig(&tset);
705 	}
706 	return sigismember(&tset, signo) ? signo : 0;
707 }
708 
709 void
getucontext(struct lwp * l,ucontext_t * ucp)710 getucontext(struct lwp *l, ucontext_t *ucp)
711 {
712 	struct proc *p = l->l_proc;
713 
714 	KASSERT(mutex_owned(p->p_lock));
715 
716 	ucp->uc_flags = 0;
717 	ucp->uc_link = l->l_ctxlink;
718 	ucp->uc_sigmask = l->l_sigmask;
719 	ucp->uc_flags |= _UC_SIGMASK;
720 
721 	/*
722 	 * The (unsupplied) definition of the `current execution stack'
723 	 * in the System V Interface Definition appears to allow returning
724 	 * the main context stack.
725 	 */
726 	if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
727 		ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase;
728 		ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize);
729 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
730 	} else {
731 		/* Simply copy alternate signal execution stack. */
732 		ucp->uc_stack = l->l_sigstk;
733 	}
734 	ucp->uc_flags |= _UC_STACK;
735 	mutex_exit(p->p_lock);
736 	cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
737 	mutex_enter(p->p_lock);
738 }
739 
740 int
setucontext(struct lwp * l,const ucontext_t * ucp)741 setucontext(struct lwp *l, const ucontext_t *ucp)
742 {
743 	struct proc *p = l->l_proc;
744 	int error;
745 
746 	KASSERT(mutex_owned(p->p_lock));
747 
748 	if ((ucp->uc_flags & _UC_SIGMASK) != 0) {
749 		error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL);
750 		if (error != 0)
751 			return error;
752 	}
753 
754 	mutex_exit(p->p_lock);
755 	error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags);
756 	mutex_enter(p->p_lock);
757 	if (error != 0)
758 		return (error);
759 
760 	l->l_ctxlink = ucp->uc_link;
761 
762 	/*
763 	 * If there was stack information, update whether or not we are
764 	 * still running on an alternate signal stack.
765 	 */
766 	if ((ucp->uc_flags & _UC_STACK) != 0) {
767 		if (ucp->uc_stack.ss_flags & SS_ONSTACK)
768 			l->l_sigstk.ss_flags |= SS_ONSTACK;
769 		else
770 			l->l_sigstk.ss_flags &= ~SS_ONSTACK;
771 	}
772 
773 	return 0;
774 }
775 
776 /*
777  * killpg1: common code for kill process group/broadcast kill.
778  */
779 int
killpg1(struct lwp * l,ksiginfo_t * ksi,int pgid,int all)780 killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all)
781 {
782 	struct proc	*p, *cp;
783 	kauth_cred_t	pc;
784 	struct pgrp	*pgrp;
785 	int		nfound;
786 	int		signo = ksi->ksi_signo;
787 
788 	cp = l->l_proc;
789 	pc = l->l_cred;
790 	nfound = 0;
791 
792 	mutex_enter(proc_lock);
793 	if (all) {
794 		/*
795 		 * Broadcast.
796 		 */
797 		PROCLIST_FOREACH(p, &allproc) {
798 			if (p->p_pid <= 1 || p == cp ||
799 			    (p->p_flag & PK_SYSTEM) != 0)
800 				continue;
801 			mutex_enter(p->p_lock);
802 			if (kauth_authorize_process(pc,
803 			    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signo), NULL,
804 			    NULL) == 0) {
805 				nfound++;
806 				if (signo)
807 					kpsignal2(p, ksi);
808 			}
809 			mutex_exit(p->p_lock);
810 		}
811 	} else {
812 		if (pgid == 0)
813 			/* Zero pgid means send to my process group. */
814 			pgrp = cp->p_pgrp;
815 		else {
816 			pgrp = pgrp_find(pgid);
817 			if (pgrp == NULL)
818 				goto out;
819 		}
820 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
821 			if (p->p_pid <= 1 || p->p_flag & PK_SYSTEM)
822 				continue;
823 			mutex_enter(p->p_lock);
824 			if (kauth_authorize_process(pc, KAUTH_PROCESS_SIGNAL,
825 			    p, KAUTH_ARG(signo), NULL, NULL) == 0) {
826 				nfound++;
827 				if (signo && P_ZOMBIE(p) == 0)
828 					kpsignal2(p, ksi);
829 			}
830 			mutex_exit(p->p_lock);
831 		}
832 	}
833 out:
834 	mutex_exit(proc_lock);
835 	return nfound ? 0 : ESRCH;
836 }
837 
838 /*
839  * Send a signal to a process group.  If checktty is set, limit to members
840  * which have a controlling terminal.
841  */
842 void
pgsignal(struct pgrp * pgrp,int sig,int checkctty)843 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
844 {
845 	ksiginfo_t ksi;
846 
847 	KASSERT(!cpu_intr_p());
848 	KASSERT(mutex_owned(proc_lock));
849 
850 	KSI_INIT_EMPTY(&ksi);
851 	ksi.ksi_signo = sig;
852 	kpgsignal(pgrp, &ksi, NULL, checkctty);
853 }
854 
855 void
kpgsignal(struct pgrp * pgrp,ksiginfo_t * ksi,void * data,int checkctty)856 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
857 {
858 	struct proc *p;
859 
860 	KASSERT(!cpu_intr_p());
861 	KASSERT(mutex_owned(proc_lock));
862 	KASSERT(pgrp != NULL);
863 
864 	LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
865 		if (checkctty == 0 || p->p_lflag & PL_CONTROLT)
866 			kpsignal(p, ksi, data);
867 }
868 
869 /*
870  * Send a signal caused by a trap to the current LWP.  If it will be caught
871  * immediately, deliver it with correct code.  Otherwise, post it normally.
872  */
873 void
trapsignal(struct lwp * l,ksiginfo_t * ksi)874 trapsignal(struct lwp *l, ksiginfo_t *ksi)
875 {
876 	struct proc	*p;
877 	struct sigacts	*ps;
878 	int signo = ksi->ksi_signo;
879 	sigset_t *mask;
880 
881 	KASSERT(KSI_TRAP_P(ksi));
882 
883 	ksi->ksi_lid = l->l_lid;
884 	p = l->l_proc;
885 
886 	KASSERT(!cpu_intr_p());
887 	mutex_enter(proc_lock);
888 	mutex_enter(p->p_lock);
889 	mask = &l->l_sigmask;
890 	ps = p->p_sigacts;
891 
892 	if ((p->p_slflag & PSL_TRACED) == 0 &&
893 	    sigismember(&p->p_sigctx.ps_sigcatch, signo) &&
894 	    !sigismember(mask, signo)) {
895 		mutex_exit(proc_lock);
896 		l->l_ru.ru_nsignals++;
897 		kpsendsig(l, ksi, mask);
898 		mutex_exit(p->p_lock);
899 		if (ktrpoint(KTR_PSIG)) {
900 			ktrpsig(signo, SIGACTION_PS(ps, signo).sa_handler,
901 			    mask, ksi);
902 		}
903 	} else {
904 		/* XXX for core dump/debugger */
905 		p->p_sigctx.ps_lwp = l->l_lid;
906 		p->p_sigctx.ps_signo = ksi->ksi_signo;
907 		p->p_sigctx.ps_code = ksi->ksi_trap;
908 		kpsignal2(p, ksi);
909 		mutex_exit(p->p_lock);
910 		mutex_exit(proc_lock);
911 	}
912 }
913 
914 /*
915  * Fill in signal information and signal the parent for a child status change.
916  */
917 void
child_psignal(struct proc * p,int mask)918 child_psignal(struct proc *p, int mask)
919 {
920 	ksiginfo_t ksi;
921 	struct proc *q;
922 	int xsig;
923 
924 	KASSERT(mutex_owned(proc_lock));
925 	KASSERT(mutex_owned(p->p_lock));
926 
927 	xsig = p->p_xsig;
928 
929 	KSI_INIT(&ksi);
930 	ksi.ksi_signo = SIGCHLD;
931 	ksi.ksi_code = (xsig == SIGCONT ? CLD_CONTINUED : CLD_STOPPED);
932 	ksi.ksi_pid = p->p_pid;
933 	ksi.ksi_uid = kauth_cred_geteuid(p->p_cred);
934 	ksi.ksi_status = xsig;
935 	ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
936 	ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
937 
938 	q = p->p_pptr;
939 
940 	mutex_exit(p->p_lock);
941 	mutex_enter(q->p_lock);
942 
943 	if ((q->p_sflag & mask) == 0)
944 		kpsignal2(q, &ksi);
945 
946 	mutex_exit(q->p_lock);
947 	mutex_enter(p->p_lock);
948 }
949 
950 void
psignal(struct proc * p,int signo)951 psignal(struct proc *p, int signo)
952 {
953 	ksiginfo_t ksi;
954 
955 	KASSERT(!cpu_intr_p());
956 	KASSERT(mutex_owned(proc_lock));
957 
958 	KSI_INIT_EMPTY(&ksi);
959 	ksi.ksi_signo = signo;
960 	mutex_enter(p->p_lock);
961 	kpsignal2(p, &ksi);
962 	mutex_exit(p->p_lock);
963 }
964 
965 void
kpsignal(struct proc * p,ksiginfo_t * ksi,void * data)966 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
967 {
968 	fdfile_t *ff;
969 	file_t *fp;
970 	fdtab_t *dt;
971 
972 	KASSERT(!cpu_intr_p());
973 	KASSERT(mutex_owned(proc_lock));
974 
975 	if ((p->p_sflag & PS_WEXIT) == 0 && data) {
976 		size_t fd;
977 		filedesc_t *fdp = p->p_fd;
978 
979 		/* XXXSMP locking */
980 		ksi->ksi_fd = -1;
981 		dt = fdp->fd_dt;
982 		for (fd = 0; fd < dt->dt_nfiles; fd++) {
983 			if ((ff = dt->dt_ff[fd]) == NULL)
984 				continue;
985 			if ((fp = ff->ff_file) == NULL)
986 				continue;
987 			if (fp->f_data == data) {
988 				ksi->ksi_fd = fd;
989 				break;
990 			}
991 		}
992 	}
993 	mutex_enter(p->p_lock);
994 	kpsignal2(p, ksi);
995 	mutex_exit(p->p_lock);
996 }
997 
998 /*
999  * sigismasked:
1000  *
1001  *	Returns true if signal is ignored or masked for the specified LWP.
1002  */
1003 int
sigismasked(struct lwp * l,int sig)1004 sigismasked(struct lwp *l, int sig)
1005 {
1006 	struct proc *p = l->l_proc;
1007 
1008 	return sigismember(&p->p_sigctx.ps_sigignore, sig) ||
1009 	    sigismember(&l->l_sigmask, sig);
1010 }
1011 
1012 /*
1013  * sigpost:
1014  *
1015  *	Post a pending signal to an LWP.  Returns non-zero if the LWP may
1016  *	be able to take the signal.
1017  */
1018 static int
sigpost(struct lwp * l,sig_t action,int prop,int sig)1019 sigpost(struct lwp *l, sig_t action, int prop, int sig)
1020 {
1021 	int rv, masked;
1022 	struct proc *p = l->l_proc;
1023 
1024 	KASSERT(mutex_owned(p->p_lock));
1025 
1026 	/*
1027 	 * If the LWP is on the way out, sigclear() will be busy draining all
1028 	 * pending signals.  Don't give it more.
1029 	 */
1030 	if (l->l_refcnt == 0)
1031 		return 0;
1032 
1033 	SDT_PROBE(proc, kernel, , signal__send, l, p, sig, 0, 0);
1034 
1035 	/*
1036 	 * Have the LWP check for signals.  This ensures that even if no LWP
1037 	 * is found to take the signal immediately, it should be taken soon.
1038 	 */
1039 	lwp_lock(l);
1040 	l->l_flag |= LW_PENDSIG;
1041 
1042 	/*
1043 	 * SIGCONT can be masked, but if LWP is stopped, it needs restart.
1044 	 * Note: SIGKILL and SIGSTOP cannot be masked.
1045 	 */
1046 	masked = sigismember(&l->l_sigmask, sig);
1047 	if (masked && ((prop & SA_CONT) == 0 || l->l_stat != LSSTOP)) {
1048 		lwp_unlock(l);
1049 		return 0;
1050 	}
1051 
1052 	/*
1053 	 * If killing the process, make it run fast.
1054 	 */
1055 	if (__predict_false((prop & SA_KILL) != 0) &&
1056 	    action == SIG_DFL && l->l_priority < MAXPRI_USER) {
1057 		KASSERT(l->l_class == SCHED_OTHER);
1058 		lwp_changepri(l, MAXPRI_USER);
1059 	}
1060 
1061 	/*
1062 	 * If the LWP is running or on a run queue, then we win.  If it's
1063 	 * sleeping interruptably, wake it and make it take the signal.  If
1064 	 * the sleep isn't interruptable, then the chances are it will get
1065 	 * to see the signal soon anyhow.  If suspended, it can't take the
1066 	 * signal right now.  If it's LWP private or for all LWPs, save it
1067 	 * for later; otherwise punt.
1068 	 */
1069 	rv = 0;
1070 
1071 	switch (l->l_stat) {
1072 	case LSRUN:
1073 	case LSONPROC:
1074 		lwp_need_userret(l);
1075 		rv = 1;
1076 		break;
1077 
1078 	case LSSLEEP:
1079 		if ((l->l_flag & LW_SINTR) != 0) {
1080 			/* setrunnable() will release the lock. */
1081 			setrunnable(l);
1082 			return 1;
1083 		}
1084 		break;
1085 
1086 	case LSSUSPENDED:
1087 		if ((prop & SA_KILL) != 0 && (l->l_flag & LW_WCORE) != 0) {
1088 			/* lwp_continue() will release the lock. */
1089 			lwp_continue(l);
1090 			return 1;
1091 		}
1092 		break;
1093 
1094 	case LSSTOP:
1095 		if ((prop & SA_STOP) != 0)
1096 			break;
1097 
1098 		/*
1099 		 * If the LWP is stopped and we are sending a continue
1100 		 * signal, then start it again.
1101 		 */
1102 		if ((prop & SA_CONT) != 0) {
1103 			if (l->l_wchan != NULL) {
1104 				l->l_stat = LSSLEEP;
1105 				p->p_nrlwps++;
1106 				rv = 1;
1107 				break;
1108 			}
1109 			/* setrunnable() will release the lock. */
1110 			setrunnable(l);
1111 			return 1;
1112 		} else if (l->l_wchan == NULL || (l->l_flag & LW_SINTR) != 0) {
1113 			/* setrunnable() will release the lock. */
1114 			setrunnable(l);
1115 			return 1;
1116 		}
1117 		break;
1118 
1119 	default:
1120 		break;
1121 	}
1122 
1123 	lwp_unlock(l);
1124 	return rv;
1125 }
1126 
1127 /*
1128  * Notify an LWP that it has a pending signal.
1129  */
1130 void
signotify(struct lwp * l)1131 signotify(struct lwp *l)
1132 {
1133 	KASSERT(lwp_locked(l, NULL));
1134 
1135 	l->l_flag |= LW_PENDSIG;
1136 	lwp_need_userret(l);
1137 }
1138 
1139 /*
1140  * Find an LWP within process p that is waiting on signal ksi, and hand
1141  * it on.
1142  */
1143 static int
sigunwait(struct proc * p,const ksiginfo_t * ksi)1144 sigunwait(struct proc *p, const ksiginfo_t *ksi)
1145 {
1146 	struct lwp *l;
1147 	int signo;
1148 
1149 	KASSERT(mutex_owned(p->p_lock));
1150 
1151 	signo = ksi->ksi_signo;
1152 
1153 	if (ksi->ksi_lid != 0) {
1154 		/*
1155 		 * Signal came via _lwp_kill().  Find the LWP and see if
1156 		 * it's interested.
1157 		 */
1158 		if ((l = lwp_find(p, ksi->ksi_lid)) == NULL)
1159 			return 0;
1160 		if (l->l_sigwaited == NULL ||
1161 		    !sigismember(&l->l_sigwaitset, signo))
1162 			return 0;
1163 	} else {
1164 		/*
1165 		 * Look for any LWP that may be interested.
1166 		 */
1167 		LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) {
1168 			KASSERT(l->l_sigwaited != NULL);
1169 			if (sigismember(&l->l_sigwaitset, signo))
1170 				break;
1171 		}
1172 	}
1173 
1174 	if (l != NULL) {
1175 		l->l_sigwaited->ksi_info = ksi->ksi_info;
1176 		l->l_sigwaited = NULL;
1177 		LIST_REMOVE(l, l_sigwaiter);
1178 		cv_signal(&l->l_sigcv);
1179 		return 1;
1180 	}
1181 
1182 	return 0;
1183 }
1184 
1185 /*
1186  * Send the signal to the process.  If the signal has an action, the action
1187  * is usually performed by the target process rather than the caller; we add
1188  * the signal to the set of pending signals for the process.
1189  *
1190  * Exceptions:
1191  *   o When a stop signal is sent to a sleeping process that takes the
1192  *     default action, the process is stopped without awakening it.
1193  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1194  *     regardless of the signal action (eg, blocked or ignored).
1195  *
1196  * Other ignored signals are discarded immediately.
1197  */
1198 void
kpsignal2(struct proc * p,ksiginfo_t * ksi)1199 kpsignal2(struct proc *p, ksiginfo_t *ksi)
1200 {
1201 	int prop, signo = ksi->ksi_signo;
1202 	struct sigacts *sa;
1203 	struct lwp *l = NULL;
1204 	ksiginfo_t *kp;
1205 	lwpid_t lid;
1206 	sig_t action;
1207 	bool toall;
1208 
1209 	KASSERT(!cpu_intr_p());
1210 	KASSERT(mutex_owned(proc_lock));
1211 	KASSERT(mutex_owned(p->p_lock));
1212 	KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
1213 	KASSERT(signo > 0 && signo < NSIG);
1214 
1215 	/*
1216 	 * If the process is being created by fork, is a zombie or is
1217 	 * exiting, then just drop the signal here and bail out.
1218 	 */
1219 	if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1220 		return;
1221 
1222 	/*
1223 	 * Notify any interested parties of the signal.
1224 	 */
1225 	KNOTE(&p->p_klist, NOTE_SIGNAL | signo);
1226 
1227 	/*
1228 	 * Some signals including SIGKILL must act on the entire process.
1229 	 */
1230 	kp = NULL;
1231 	prop = sigprop[signo];
1232 	toall = ((prop & SA_TOALL) != 0);
1233 	lid = toall ? 0 : ksi->ksi_lid;
1234 
1235 	/*
1236 	 * If proc is traced, always give parent a chance.
1237 	 */
1238 	if (p->p_slflag & PSL_TRACED) {
1239 		action = SIG_DFL;
1240 
1241 		if (lid == 0) {
1242 			/*
1243 			 * If the process is being traced and the signal
1244 			 * is being caught, make sure to save any ksiginfo.
1245 			 */
1246 			if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1247 				goto discard;
1248 			sigput(&p->p_sigpend, p, kp);
1249 		}
1250 	} else {
1251 		/*
1252 		 * If the signal was the result of a trap and is not being
1253 		 * caught, then reset it to default action so that the
1254 		 * process dumps core immediately.
1255 		 */
1256 		if (KSI_TRAP_P(ksi)) {
1257 			sa = p->p_sigacts;
1258 			mutex_enter(&sa->sa_mutex);
1259 			if (!sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
1260 				sigdelset(&p->p_sigctx.ps_sigignore, signo);
1261 				SIGACTION(p, signo).sa_handler = SIG_DFL;
1262 			}
1263 			mutex_exit(&sa->sa_mutex);
1264 		}
1265 
1266 		/*
1267 		 * If the signal is being ignored, then drop it.  Note: we
1268 		 * don't set SIGCONT in ps_sigignore, and if it is set to
1269 		 * SIG_IGN, action will be SIG_DFL here.
1270 		 */
1271 		if (sigismember(&p->p_sigctx.ps_sigignore, signo))
1272 			goto discard;
1273 
1274 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signo))
1275 			action = SIG_CATCH;
1276 		else {
1277 			action = SIG_DFL;
1278 
1279 			/*
1280 			 * If sending a tty stop signal to a member of an
1281 			 * orphaned process group, discard the signal here if
1282 			 * the action is default; don't stop the process below
1283 			 * if sleeping, and don't clear any pending SIGCONT.
1284 			 */
1285 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
1286 				goto discard;
1287 
1288 			if (prop & SA_KILL && p->p_nice > NZERO)
1289 				p->p_nice = NZERO;
1290 		}
1291 	}
1292 
1293 	/*
1294 	 * If stopping or continuing a process, discard any pending
1295 	 * signals that would do the inverse.
1296 	 */
1297 	if ((prop & (SA_CONT | SA_STOP)) != 0) {
1298 		ksiginfoq_t kq;
1299 
1300 		ksiginfo_queue_init(&kq);
1301 		if ((prop & SA_CONT) != 0)
1302 			sigclear(&p->p_sigpend, &stopsigmask, &kq);
1303 		if ((prop & SA_STOP) != 0)
1304 			sigclear(&p->p_sigpend, &contsigmask, &kq);
1305 		ksiginfo_queue_drain(&kq);	/* XXXSMP */
1306 	}
1307 
1308 	/*
1309 	 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
1310 	 * please!), check if any LWPs are waiting on it.  If yes, pass on
1311 	 * the signal info.  The signal won't be processed further here.
1312 	 */
1313 	if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) &&
1314 	    p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 &&
1315 	    sigunwait(p, ksi))
1316 		goto discard;
1317 
1318 	/*
1319 	 * XXXSMP Should be allocated by the caller, we're holding locks
1320 	 * here.
1321 	 */
1322 	if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1323 		goto discard;
1324 
1325 	/*
1326 	 * LWP private signals are easy - just find the LWP and post
1327 	 * the signal to it.
1328 	 */
1329 	if (lid != 0) {
1330 		l = lwp_find(p, lid);
1331 		if (l != NULL) {
1332 			sigput(&l->l_sigpend, p, kp);
1333 			membar_producer();
1334 			(void)sigpost(l, action, prop, kp->ksi_signo);
1335 		}
1336 		goto out;
1337 	}
1338 
1339 	/*
1340 	 * Some signals go to all LWPs, even if posted with _lwp_kill()
1341 	 * or for an SA process.
1342 	 */
1343 	if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
1344 		if ((p->p_slflag & PSL_TRACED) != 0)
1345 			goto deliver;
1346 
1347 		/*
1348 		 * If SIGCONT is default (or ignored) and process is
1349 		 * asleep, we are finished; the process should not
1350 		 * be awakened.
1351 		 */
1352 		if ((prop & SA_CONT) != 0 && action == SIG_DFL)
1353 			goto out;
1354 	} else {
1355 		/*
1356 		 * Process is stopped or stopping.
1357 		 * - If traced, then no action is needed, unless killing.
1358 		 * - Run the process only if sending SIGCONT or SIGKILL.
1359 		 */
1360 		if ((p->p_slflag & PSL_TRACED) != 0 && signo != SIGKILL) {
1361 			goto out;
1362 		}
1363 		if ((prop & SA_CONT) != 0 || signo == SIGKILL) {
1364 			/*
1365 			 * Re-adjust p_nstopchild if the process was
1366 			 * stopped but not yet collected by its parent.
1367 			 */
1368 			if (p->p_stat == SSTOP && !p->p_waited)
1369 				p->p_pptr->p_nstopchild--;
1370 			p->p_stat = SACTIVE;
1371 			p->p_sflag &= ~PS_STOPPING;
1372 			if (p->p_slflag & PSL_TRACED) {
1373 				KASSERT(signo == SIGKILL);
1374 				goto deliver;
1375 			}
1376 			/*
1377 			 * Do not make signal pending if SIGCONT is default.
1378 			 *
1379 			 * If the process catches SIGCONT, let it handle the
1380 			 * signal itself (if waiting on event - process runs,
1381 			 * otherwise continues sleeping).
1382 			 */
1383 			if ((prop & SA_CONT) != 0) {
1384 				p->p_xsig = SIGCONT;
1385 				p->p_sflag |= PS_CONTINUED;
1386 				child_psignal(p, 0);
1387 				if (action == SIG_DFL) {
1388 					KASSERT(signo != SIGKILL);
1389 					goto deliver;
1390 				}
1391 			}
1392 		} else if ((prop & SA_STOP) != 0) {
1393 			/*
1394 			 * Already stopped, don't need to stop again.
1395 			 * (If we did the shell could get confused.)
1396 			 */
1397 			goto out;
1398 		}
1399 	}
1400 	/*
1401 	 * Make signal pending.
1402 	 */
1403 	KASSERT((p->p_slflag & PSL_TRACED) == 0);
1404 	sigput(&p->p_sigpend, p, kp);
1405 
1406 deliver:
1407 	/*
1408 	 * Before we set LW_PENDSIG on any LWP, ensure that the signal is
1409 	 * visible on the per process list (for sigispending()).  This
1410 	 * is unlikely to be needed in practice, but...
1411 	 */
1412 	membar_producer();
1413 
1414 	/*
1415 	 * Try to find an LWP that can take the signal.
1416 	 */
1417 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1418 		if (sigpost(l, action, prop, kp->ksi_signo) && !toall)
1419 			break;
1420 	}
1421 	signo = -1;
1422 out:
1423 	/*
1424 	 * If the ksiginfo wasn't used, then bin it.  XXXSMP freeing memory
1425 	 * with locks held.  The caller should take care of this.
1426 	 */
1427 	ksiginfo_free(kp);
1428 	if (signo == -1)
1429 		return;
1430 discard:
1431 	SDT_PROBE(proc, kernel, , signal__discard, l, p, signo, 0, 0);
1432 }
1433 
1434 void
kpsendsig(struct lwp * l,const ksiginfo_t * ksi,const sigset_t * mask)1435 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
1436 {
1437 	struct proc *p = l->l_proc;
1438 
1439 	KASSERT(mutex_owned(p->p_lock));
1440 	(*p->p_emul->e_sendsig)(ksi, mask);
1441 }
1442 
1443 /*
1444  * Stop any LWPs sleeping interruptably.
1445  */
1446 static void
proc_stop_lwps(struct proc * p)1447 proc_stop_lwps(struct proc *p)
1448 {
1449 	struct lwp *l;
1450 
1451 	KASSERT(mutex_owned(p->p_lock));
1452 	KASSERT((p->p_sflag & PS_STOPPING) != 0);
1453 
1454 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1455 		lwp_lock(l);
1456 		if (l->l_stat == LSSLEEP && (l->l_flag & LW_SINTR) != 0) {
1457 			l->l_stat = LSSTOP;
1458 			p->p_nrlwps--;
1459 		}
1460 		lwp_unlock(l);
1461 	}
1462 }
1463 
1464 /*
1465  * Finish stopping of a process.  Mark it stopped and notify the parent.
1466  *
1467  * Drop p_lock briefly if PS_NOTIFYSTOP is set and ppsig is true.
1468  */
1469 static void
proc_stop_done(struct proc * p,bool ppsig,int ppmask)1470 proc_stop_done(struct proc *p, bool ppsig, int ppmask)
1471 {
1472 
1473 	KASSERT(mutex_owned(proc_lock));
1474 	KASSERT(mutex_owned(p->p_lock));
1475 	KASSERT((p->p_sflag & PS_STOPPING) != 0);
1476 	KASSERT(p->p_nrlwps == 0 || (p->p_nrlwps == 1 && p == curproc));
1477 
1478 	p->p_sflag &= ~PS_STOPPING;
1479 	p->p_stat = SSTOP;
1480 	p->p_waited = 0;
1481 	p->p_pptr->p_nstopchild++;
1482 	if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
1483 		if (ppsig) {
1484 			/* child_psignal drops p_lock briefly. */
1485 			child_psignal(p, ppmask);
1486 		}
1487 		cv_broadcast(&p->p_pptr->p_waitcv);
1488 	}
1489 }
1490 
1491 /*
1492  * Stop the current process and switch away when being stopped or traced.
1493  */
1494 static void
sigswitch(bool ppsig,int ppmask,int signo)1495 sigswitch(bool ppsig, int ppmask, int signo)
1496 {
1497 	struct lwp *l = curlwp;
1498 	struct proc *p = l->l_proc;
1499 	int biglocks;
1500 
1501 	KASSERT(mutex_owned(p->p_lock));
1502 	KASSERT(l->l_stat == LSONPROC);
1503 	KASSERT(p->p_nrlwps > 0);
1504 
1505 	/*
1506 	 * On entry we know that the process needs to stop.  If it's
1507 	 * the result of a 'sideways' stop signal that has been sourced
1508 	 * through issignal(), then stop other LWPs in the process too.
1509 	 */
1510 	if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
1511 		KASSERT(signo != 0);
1512 		proc_stop(p, 1, signo);
1513 		KASSERT(p->p_nrlwps > 0);
1514 	}
1515 
1516 	/*
1517 	 * If we are the last live LWP, and the stop was a result of
1518 	 * a new signal, then signal the parent.
1519 	 */
1520 	if ((p->p_sflag & PS_STOPPING) != 0) {
1521 		if (!mutex_tryenter(proc_lock)) {
1522 			mutex_exit(p->p_lock);
1523 			mutex_enter(proc_lock);
1524 			mutex_enter(p->p_lock);
1525 		}
1526 
1527 		if (p->p_nrlwps == 1 && (p->p_sflag & PS_STOPPING) != 0) {
1528 			/*
1529 			 * Note that proc_stop_done() can drop
1530 			 * p->p_lock briefly.
1531 			 */
1532 			proc_stop_done(p, ppsig, ppmask);
1533 		}
1534 
1535 		mutex_exit(proc_lock);
1536 	}
1537 
1538 	/*
1539 	 * Unlock and switch away.
1540 	 */
1541 	KERNEL_UNLOCK_ALL(l, &biglocks);
1542 	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
1543 		p->p_nrlwps--;
1544 		lwp_lock(l);
1545 		KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSLEEP);
1546 		l->l_stat = LSSTOP;
1547 		lwp_unlock(l);
1548 	}
1549 
1550 	mutex_exit(p->p_lock);
1551 	lwp_lock(l);
1552 	mi_switch(l);
1553 	KERNEL_LOCK(biglocks, l);
1554 	mutex_enter(p->p_lock);
1555 }
1556 
1557 /*
1558  * Check for a signal from the debugger.
1559  */
1560 static int
sigchecktrace(void)1561 sigchecktrace(void)
1562 {
1563 	struct lwp *l = curlwp;
1564 	struct proc *p = l->l_proc;
1565 	int signo;
1566 
1567 	KASSERT(mutex_owned(p->p_lock));
1568 
1569 	/* If there's a pending SIGKILL, process it immediately. */
1570 	if (sigismember(&p->p_sigpend.sp_set, SIGKILL))
1571 		return 0;
1572 
1573 	/*
1574 	 * If we are no longer being traced, or the parent didn't
1575 	 * give us a signal, or we're stopping, look for more signals.
1576 	 */
1577 	if ((p->p_slflag & PSL_TRACED) == 0 || p->p_xsig == 0 ||
1578 	    (p->p_sflag & PS_STOPPING) != 0)
1579 		return 0;
1580 
1581 	/*
1582 	 * If the new signal is being masked, look for other signals.
1583 	 * `p->p_sigctx.ps_siglist |= mask' is done in setrunnable().
1584 	 */
1585 	signo = p->p_xsig;
1586 	p->p_xsig = 0;
1587 	if (sigismember(&l->l_sigmask, signo)) {
1588 		signo = 0;
1589 	}
1590 	return signo;
1591 }
1592 
1593 /*
1594  * If the current process has received a signal (should be caught or cause
1595  * termination, should interrupt current syscall), return the signal number.
1596  *
1597  * Stop signals with default action are processed immediately, then cleared;
1598  * they aren't returned.  This is checked after each entry to the system for
1599  * a syscall or trap.
1600  *
1601  * We will also return -1 if the process is exiting and the current LWP must
1602  * follow suit.
1603  */
1604 int
issignal(struct lwp * l)1605 issignal(struct lwp *l)
1606 {
1607 	struct proc *p;
1608 	int signo, prop;
1609 	sigpend_t *sp;
1610 	sigset_t ss;
1611 
1612 	p = l->l_proc;
1613 	sp = NULL;
1614 	signo = 0;
1615 
1616 	KASSERT(p == curproc);
1617 	KASSERT(mutex_owned(p->p_lock));
1618 
1619 	for (;;) {
1620 		/* Discard any signals that we have decided not to take. */
1621 		if (signo != 0) {
1622 			(void)sigget(sp, NULL, signo, NULL);
1623 		}
1624 
1625 		/*
1626 		 * If the process is stopped/stopping, then stop ourselves
1627 		 * now that we're on the kernel/userspace boundary.  When
1628 		 * we awaken, check for a signal from the debugger.
1629 		 */
1630 		if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
1631 			sigswitch(true, PS_NOCLDSTOP, 0);
1632 			signo = sigchecktrace();
1633 		} else
1634 			signo = 0;
1635 
1636 		/* Signals from the debugger are "out of band". */
1637 		sp = NULL;
1638 
1639 		/*
1640 		 * If the debugger didn't provide a signal, find a pending
1641 		 * signal from our set.  Check per-LWP signals first, and
1642 		 * then per-process.
1643 		 */
1644 		if (signo == 0) {
1645 			sp = &l->l_sigpend;
1646 			ss = sp->sp_set;
1647 			if ((p->p_lflag & PL_PPWAIT) != 0)
1648 				sigminusset(&stopsigmask, &ss);
1649 			sigminusset(&l->l_sigmask, &ss);
1650 
1651 			if ((signo = firstsig(&ss)) == 0) {
1652 				sp = &p->p_sigpend;
1653 				ss = sp->sp_set;
1654 				if ((p->p_lflag & PL_PPWAIT) != 0)
1655 					sigminusset(&stopsigmask, &ss);
1656 				sigminusset(&l->l_sigmask, &ss);
1657 
1658 				if ((signo = firstsig(&ss)) == 0) {
1659 					/*
1660 					 * No signal pending - clear the
1661 					 * indicator and bail out.
1662 					 */
1663 					lwp_lock(l);
1664 					l->l_flag &= ~LW_PENDSIG;
1665 					lwp_unlock(l);
1666 					sp = NULL;
1667 					break;
1668 				}
1669 			}
1670 		}
1671 
1672 		/*
1673 		 * We should see pending but ignored signals only if
1674 		 * we are being traced.
1675 		 */
1676 		if (sigismember(&p->p_sigctx.ps_sigignore, signo) &&
1677 		    (p->p_slflag & PSL_TRACED) == 0) {
1678 			/* Discard the signal. */
1679 			continue;
1680 		}
1681 
1682 		/*
1683 		 * If traced, always stop, and stay stopped until released
1684 		 * by the debugger.  If the our parent process is waiting
1685 		 * for us, don't hang as we could deadlock.
1686 		 */
1687 		if ((p->p_slflag & PSL_TRACED) != 0 &&
1688 		    (p->p_lflag & PL_PPWAIT) == 0 && signo != SIGKILL) {
1689 			/*
1690 			 * Take the signal, but don't remove it from the
1691 			 * siginfo queue, because the debugger can send
1692 			 * it later.
1693 			 */
1694 			if (sp)
1695 				sigdelset(&sp->sp_set, signo);
1696 			p->p_xsig = signo;
1697 
1698 			/* Emulation-specific handling of signal trace */
1699 			if (p->p_emul->e_tracesig == NULL ||
1700 			    (*p->p_emul->e_tracesig)(p, signo) == 0)
1701 				sigswitch(!(p->p_slflag & PSL_FSTRACE), 0,
1702 				    signo);
1703 
1704 			/* Check for a signal from the debugger. */
1705 			if ((signo = sigchecktrace()) == 0)
1706 				continue;
1707 
1708 			/* Signals from the debugger are "out of band". */
1709 			sp = NULL;
1710 		}
1711 
1712 		prop = sigprop[signo];
1713 
1714 		/*
1715 		 * Decide whether the signal should be returned.
1716 		 */
1717 		switch ((long)SIGACTION(p, signo).sa_handler) {
1718 		case (long)SIG_DFL:
1719 			/*
1720 			 * Don't take default actions on system processes.
1721 			 */
1722 			if (p->p_pid <= 1) {
1723 #ifdef DIAGNOSTIC
1724 				/*
1725 				 * Are you sure you want to ignore SIGSEGV
1726 				 * in init? XXX
1727 				 */
1728 				printf_nolog("Process (pid %d) got sig %d\n",
1729 				    p->p_pid, signo);
1730 #endif
1731 				continue;
1732 			}
1733 
1734 			/*
1735 			 * If there is a pending stop signal to process with
1736 			 * default action, stop here, then clear the signal.
1737 			 * However, if process is member of an orphaned
1738 			 * process group, ignore tty stop signals.
1739 			 */
1740 			if (prop & SA_STOP) {
1741 				/*
1742 				 * XXX Don't hold proc_lock for p_lflag,
1743 				 * but it's not a big deal.
1744 				 */
1745 				if (p->p_slflag & PSL_TRACED ||
1746 				    ((p->p_lflag & PL_ORPHANPG) != 0 &&
1747 				    prop & SA_TTYSTOP)) {
1748 					/* Ignore the signal. */
1749 					continue;
1750 				}
1751 				/* Take the signal. */
1752 				(void)sigget(sp, NULL, signo, NULL);
1753 				p->p_xsig = signo;
1754 				p->p_sflag &= ~PS_CONTINUED;
1755 				signo = 0;
1756 				sigswitch(true, PS_NOCLDSTOP, p->p_xsig);
1757 			} else if (prop & SA_IGNORE) {
1758 				/*
1759 				 * Except for SIGCONT, shouldn't get here.
1760 				 * Default action is to ignore; drop it.
1761 				 */
1762 				continue;
1763 			}
1764 			break;
1765 
1766 		case (long)SIG_IGN:
1767 #ifdef DEBUG_ISSIGNAL
1768 			/*
1769 			 * Masking above should prevent us ever trying
1770 			 * to take action on an ignored signal other
1771 			 * than SIGCONT, unless process is traced.
1772 			 */
1773 			if ((prop & SA_CONT) == 0 &&
1774 			    (p->p_slflag & PSL_TRACED) == 0)
1775 				printf_nolog("issignal\n");
1776 #endif
1777 			continue;
1778 
1779 		default:
1780 			/*
1781 			 * This signal has an action, let postsig() process
1782 			 * it.
1783 			 */
1784 			break;
1785 		}
1786 
1787 		break;
1788 	}
1789 
1790 	l->l_sigpendset = sp;
1791 	return signo;
1792 }
1793 
1794 /*
1795  * Take the action for the specified signal
1796  * from the current set of pending signals.
1797  */
1798 void
postsig(int signo)1799 postsig(int signo)
1800 {
1801 	struct lwp	*l;
1802 	struct proc	*p;
1803 	struct sigacts	*ps;
1804 	sig_t		action;
1805 	sigset_t	*returnmask;
1806 	ksiginfo_t	ksi;
1807 
1808 	l = curlwp;
1809 	p = l->l_proc;
1810 	ps = p->p_sigacts;
1811 
1812 	KASSERT(mutex_owned(p->p_lock));
1813 	KASSERT(signo > 0);
1814 
1815 	/*
1816 	 * Set the new mask value and also defer further occurrences of this
1817 	 * signal.
1818 	 *
1819 	 * Special case: user has done a sigsuspend.  Here the current mask is
1820 	 * not of interest, but rather the mask from before the sigsuspend is
1821 	 * what we want restored after the signal processing is completed.
1822 	 */
1823 	if (l->l_sigrestore) {
1824 		returnmask = &l->l_sigoldmask;
1825 		l->l_sigrestore = 0;
1826 	} else
1827 		returnmask = &l->l_sigmask;
1828 
1829 	/*
1830 	 * Commit to taking the signal before releasing the mutex.
1831 	 */
1832 	action = SIGACTION_PS(ps, signo).sa_handler;
1833 	l->l_ru.ru_nsignals++;
1834 	if (l->l_sigpendset == NULL) {
1835 		/* From the debugger */
1836 		if (!siggetinfo(&l->l_sigpend, &ksi, signo))
1837 			(void)siggetinfo(&p->p_sigpend, &ksi, signo);
1838 	} else
1839 		sigget(l->l_sigpendset, &ksi, signo, NULL);
1840 
1841 	if (ktrpoint(KTR_PSIG)) {
1842 		mutex_exit(p->p_lock);
1843 		ktrpsig(signo, action, returnmask, &ksi);
1844 		mutex_enter(p->p_lock);
1845 	}
1846 
1847 	SDT_PROBE(proc, kernel, , signal__handle, signo, &ksi, action, 0, 0);
1848 
1849 	if (action == SIG_DFL) {
1850 		/*
1851 		 * Default action, where the default is to kill
1852 		 * the process.  (Other cases were ignored above.)
1853 		 */
1854 		sigexit(l, signo);
1855 		return;
1856 	}
1857 
1858 	/*
1859 	 * If we get here, the signal must be caught.
1860 	 */
1861 #ifdef DIAGNOSTIC
1862 	if (action == SIG_IGN || sigismember(&l->l_sigmask, signo))
1863 		panic("postsig action");
1864 #endif
1865 
1866 	kpsendsig(l, &ksi, returnmask);
1867 }
1868 
1869 /*
1870  * sendsig:
1871  *
1872  *	Default signal delivery method for NetBSD.
1873  */
1874 void
sendsig(const struct ksiginfo * ksi,const sigset_t * mask)1875 sendsig(const struct ksiginfo *ksi, const sigset_t *mask)
1876 {
1877 	struct sigacts *sa;
1878 	int sig;
1879 
1880 	sig = ksi->ksi_signo;
1881 	sa = curproc->p_sigacts;
1882 
1883 	switch (sa->sa_sigdesc[sig].sd_vers)  {
1884 	case 0:
1885 	case 1:
1886 		/* Compat for 1.6 and earlier. */
1887 		if (sendsig_sigcontext_vec == NULL) {
1888 			break;
1889 		}
1890 		(*sendsig_sigcontext_vec)(ksi, mask);
1891 		return;
1892 	case 2:
1893 	case 3:
1894 		sendsig_siginfo(ksi, mask);
1895 		return;
1896 	default:
1897 		break;
1898 	}
1899 
1900 	printf("sendsig: bad version %d\n", sa->sa_sigdesc[sig].sd_vers);
1901 	sigexit(curlwp, SIGILL);
1902 }
1903 
1904 /*
1905  * sendsig_reset:
1906  *
1907  *	Reset the signal action.  Called from emulation specific sendsig()
1908  *	before unlocking to deliver the signal.
1909  */
1910 void
sendsig_reset(struct lwp * l,int signo)1911 sendsig_reset(struct lwp *l, int signo)
1912 {
1913 	struct proc *p = l->l_proc;
1914 	struct sigacts *ps = p->p_sigacts;
1915 
1916 	KASSERT(mutex_owned(p->p_lock));
1917 
1918 	p->p_sigctx.ps_lwp = 0;
1919 	p->p_sigctx.ps_code = 0;
1920 	p->p_sigctx.ps_signo = 0;
1921 
1922 	mutex_enter(&ps->sa_mutex);
1923 	sigplusset(&SIGACTION_PS(ps, signo).sa_mask, &l->l_sigmask);
1924 	if (SIGACTION_PS(ps, signo).sa_flags & SA_RESETHAND) {
1925 		sigdelset(&p->p_sigctx.ps_sigcatch, signo);
1926 		if (signo != SIGCONT && sigprop[signo] & SA_IGNORE)
1927 			sigaddset(&p->p_sigctx.ps_sigignore, signo);
1928 		SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
1929 	}
1930 	mutex_exit(&ps->sa_mutex);
1931 }
1932 
1933 /*
1934  * Kill the current process for stated reason.
1935  */
1936 void
killproc(struct proc * p,const char * why)1937 killproc(struct proc *p, const char *why)
1938 {
1939 
1940 	KASSERT(mutex_owned(proc_lock));
1941 
1942 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1943 	uprintf_locked("sorry, pid %d was killed: %s\n", p->p_pid, why);
1944 	psignal(p, SIGKILL);
1945 }
1946 
1947 /*
1948  * Force the current process to exit with the specified signal, dumping core
1949  * if appropriate.  We bypass the normal tests for masked and caught
1950  * signals, allowing unrecoverable failures to terminate the process without
1951  * changing signal state.  Mark the accounting record with the signal
1952  * termination.  If dumping core, save the signal number for the debugger.
1953  * Calls exit and does not return.
1954  */
1955 void
sigexit(struct lwp * l,int signo)1956 sigexit(struct lwp *l, int signo)
1957 {
1958 	int exitsig, error, docore;
1959 	struct proc *p;
1960 	struct lwp *t;
1961 
1962 	p = l->l_proc;
1963 
1964 	KASSERT(mutex_owned(p->p_lock));
1965 	KERNEL_UNLOCK_ALL(l, NULL);
1966 
1967 	/*
1968 	 * Don't permit coredump() multiple times in the same process.
1969 	 * Call back into sigexit, where we will be suspended until
1970 	 * the deed is done.  Note that this is a recursive call, but
1971 	 * LW_WCORE will prevent us from coming back this way.
1972 	 */
1973 	if ((p->p_sflag & PS_WCORE) != 0) {
1974 		lwp_lock(l);
1975 		l->l_flag |= (LW_WCORE | LW_WEXIT | LW_WSUSPEND);
1976 		lwp_unlock(l);
1977 		mutex_exit(p->p_lock);
1978 		lwp_userret(l);
1979 		panic("sigexit 1");
1980 		/* NOTREACHED */
1981 	}
1982 
1983 	/* If process is already on the way out, then bail now. */
1984 	if ((p->p_sflag & PS_WEXIT) != 0) {
1985 		mutex_exit(p->p_lock);
1986 		lwp_exit(l);
1987 		panic("sigexit 2");
1988 		/* NOTREACHED */
1989 	}
1990 
1991 	/*
1992 	 * Prepare all other LWPs for exit.  If dumping core, suspend them
1993 	 * so that their registers are available long enough to be dumped.
1994  	 */
1995 	if ((docore = (sigprop[signo] & SA_CORE)) != 0) {
1996 		p->p_sflag |= PS_WCORE;
1997 		for (;;) {
1998 			LIST_FOREACH(t, &p->p_lwps, l_sibling) {
1999 				lwp_lock(t);
2000 				if (t == l) {
2001 					t->l_flag &= ~LW_WSUSPEND;
2002 					lwp_unlock(t);
2003 					continue;
2004 				}
2005 				t->l_flag |= (LW_WCORE | LW_WEXIT);
2006 				lwp_suspend(l, t);
2007 			}
2008 
2009 			if (p->p_nrlwps == 1)
2010 				break;
2011 
2012 			/*
2013 			 * Kick any LWPs sitting in lwp_wait1(), and wait
2014 			 * for everyone else to stop before proceeding.
2015 			 */
2016 			p->p_nlwpwait++;
2017 			cv_broadcast(&p->p_lwpcv);
2018 			cv_wait(&p->p_lwpcv, p->p_lock);
2019 			p->p_nlwpwait--;
2020 		}
2021 	}
2022 
2023 	exitsig = signo;
2024 	p->p_acflag |= AXSIG;
2025 	p->p_sigctx.ps_signo = signo;
2026 
2027 	if (docore) {
2028 		mutex_exit(p->p_lock);
2029 		error = (*coredump_vec)(l, NULL);
2030 
2031 		if (kern_logsigexit) {
2032 			int uid = l->l_cred ?
2033 			    (int)kauth_cred_geteuid(l->l_cred) : -1;
2034 
2035 			if (error)
2036 				log(LOG_INFO, lognocoredump, p->p_pid,
2037 				    p->p_comm, uid, signo, error);
2038 			else
2039 				log(LOG_INFO, logcoredump, p->p_pid,
2040 				    p->p_comm, uid, signo);
2041 		}
2042 
2043 #ifdef PAX_SEGVGUARD
2044 		pax_segvguard(l, p->p_textvp, p->p_comm, true);
2045 #endif /* PAX_SEGVGUARD */
2046 		/* Acquire the sched state mutex.  exit1() will release it. */
2047 		mutex_enter(p->p_lock);
2048 		if (error == 0)
2049 			p->p_sflag |= PS_COREDUMP;
2050 	}
2051 
2052 	/* No longer dumping core. */
2053 	p->p_sflag &= ~PS_WCORE;
2054 
2055 	exit1(l, 0, exitsig);
2056 	/* NOTREACHED */
2057 }
2058 
2059 /*
2060  * Put process 'p' into the stopped state and optionally, notify the parent.
2061  */
2062 void
proc_stop(struct proc * p,int notify,int signo)2063 proc_stop(struct proc *p, int notify, int signo)
2064 {
2065 	struct lwp *l;
2066 
2067 	KASSERT(mutex_owned(p->p_lock));
2068 
2069 	/*
2070 	 * First off, set the stopping indicator and bring all sleeping
2071 	 * LWPs to a halt so they are included in p->p_nrlwps.  We musn't
2072 	 * unlock between here and the p->p_nrlwps check below.
2073 	 */
2074 	p->p_sflag |= PS_STOPPING;
2075 	if (notify)
2076 		p->p_sflag |= PS_NOTIFYSTOP;
2077 	else
2078 		p->p_sflag &= ~PS_NOTIFYSTOP;
2079 	membar_producer();
2080 
2081 	proc_stop_lwps(p);
2082 
2083 	/*
2084 	 * If there are no LWPs available to take the signal, then we
2085 	 * signal the parent process immediately.  Otherwise, the last
2086 	 * LWP to stop will take care of it.
2087 	 */
2088 
2089 	if (p->p_nrlwps == 0) {
2090 		proc_stop_done(p, true, PS_NOCLDSTOP);
2091 	} else {
2092 		/*
2093 		 * Have the remaining LWPs come to a halt, and trigger
2094 		 * proc_stop_callout() to ensure that they do.
2095 		 */
2096 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2097 			sigpost(l, SIG_DFL, SA_STOP, signo);
2098 		}
2099 		callout_schedule(&proc_stop_ch, 1);
2100 	}
2101 }
2102 
2103 /*
2104  * When stopping a process, we do not immediatly set sleeping LWPs stopped,
2105  * but wait for them to come to a halt at the kernel-user boundary.  This is
2106  * to allow LWPs to release any locks that they may hold before stopping.
2107  *
2108  * Non-interruptable sleeps can be long, and there is the potential for an
2109  * LWP to begin sleeping interruptably soon after the process has been set
2110  * stopping (PS_STOPPING).  These LWPs will not notice that the process is
2111  * stopping, and so complete halt of the process and the return of status
2112  * information to the parent could be delayed indefinitely.
2113  *
2114  * To handle this race, proc_stop_callout() runs once per tick while there
2115  * are stopping processes in the system.  It sets LWPs that are sleeping
2116  * interruptably into the LSSTOP state.
2117  *
2118  * Note that we are not concerned about keeping all LWPs stopped while the
2119  * process is stopped: stopped LWPs can awaken briefly to handle signals.
2120  * What we do need to ensure is that all LWPs in a stopping process have
2121  * stopped at least once, so that notification can be sent to the parent
2122  * process.
2123  */
2124 static void
proc_stop_callout(void * cookie)2125 proc_stop_callout(void *cookie)
2126 {
2127 	bool more, restart;
2128 	struct proc *p;
2129 
2130 	(void)cookie;
2131 
2132 	do {
2133 		restart = false;
2134 		more = false;
2135 
2136 		mutex_enter(proc_lock);
2137 		PROCLIST_FOREACH(p, &allproc) {
2138 			mutex_enter(p->p_lock);
2139 
2140 			if ((p->p_sflag & PS_STOPPING) == 0) {
2141 				mutex_exit(p->p_lock);
2142 				continue;
2143 			}
2144 
2145 			/* Stop any LWPs sleeping interruptably. */
2146 			proc_stop_lwps(p);
2147 			if (p->p_nrlwps == 0) {
2148 				/*
2149 				 * We brought the process to a halt.
2150 				 * Mark it as stopped and notify the
2151 				 * parent.
2152 				 */
2153 				if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
2154 					/*
2155 					 * Note that proc_stop_done() will
2156 					 * drop p->p_lock briefly.
2157 					 * Arrange to restart and check
2158 					 * all processes again.
2159 					 */
2160 					restart = true;
2161 				}
2162 				proc_stop_done(p, true, PS_NOCLDSTOP);
2163 			} else
2164 				more = true;
2165 
2166 			mutex_exit(p->p_lock);
2167 			if (restart)
2168 				break;
2169 		}
2170 		mutex_exit(proc_lock);
2171 	} while (restart);
2172 
2173 	/*
2174 	 * If we noted processes that are stopping but still have
2175 	 * running LWPs, then arrange to check again in 1 tick.
2176 	 */
2177 	if (more)
2178 		callout_schedule(&proc_stop_ch, 1);
2179 }
2180 
2181 /*
2182  * Given a process in state SSTOP, set the state back to SACTIVE and
2183  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
2184  */
2185 void
proc_unstop(struct proc * p)2186 proc_unstop(struct proc *p)
2187 {
2188 	struct lwp *l;
2189 	int sig;
2190 
2191 	KASSERT(mutex_owned(proc_lock));
2192 	KASSERT(mutex_owned(p->p_lock));
2193 
2194 	p->p_stat = SACTIVE;
2195 	p->p_sflag &= ~PS_STOPPING;
2196 	sig = p->p_xsig;
2197 
2198 	if (!p->p_waited)
2199 		p->p_pptr->p_nstopchild--;
2200 
2201 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2202 		lwp_lock(l);
2203 		if (l->l_stat != LSSTOP) {
2204 			lwp_unlock(l);
2205 			continue;
2206 		}
2207 		if (l->l_wchan == NULL) {
2208 			setrunnable(l);
2209 			continue;
2210 		}
2211 		if (sig && (l->l_flag & LW_SINTR) != 0) {
2212 			setrunnable(l);
2213 			sig = 0;
2214 		} else {
2215 			l->l_stat = LSSLEEP;
2216 			p->p_nrlwps++;
2217 			lwp_unlock(l);
2218 		}
2219 	}
2220 }
2221 
2222 static int
filt_sigattach(struct knote * kn)2223 filt_sigattach(struct knote *kn)
2224 {
2225 	struct proc *p = curproc;
2226 
2227 	kn->kn_obj = p;
2228 	kn->kn_flags |= EV_CLEAR;	/* automatically set */
2229 
2230 	mutex_enter(p->p_lock);
2231 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2232 	mutex_exit(p->p_lock);
2233 
2234 	return 0;
2235 }
2236 
2237 static void
filt_sigdetach(struct knote * kn)2238 filt_sigdetach(struct knote *kn)
2239 {
2240 	struct proc *p = kn->kn_obj;
2241 
2242 	mutex_enter(p->p_lock);
2243 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2244 	mutex_exit(p->p_lock);
2245 }
2246 
2247 /*
2248  * Signal knotes are shared with proc knotes, so we apply a mask to
2249  * the hint in order to differentiate them from process hints.  This
2250  * could be avoided by using a signal-specific knote list, but probably
2251  * isn't worth the trouble.
2252  */
2253 static int
filt_signal(struct knote * kn,long hint)2254 filt_signal(struct knote *kn, long hint)
2255 {
2256 
2257 	if (hint & NOTE_SIGNAL) {
2258 		hint &= ~NOTE_SIGNAL;
2259 
2260 		if (kn->kn_id == hint)
2261 			kn->kn_data++;
2262 	}
2263 	return (kn->kn_data != 0);
2264 }
2265 
2266 const struct filterops sig_filtops = {
2267 	0, filt_sigattach, filt_sigdetach, filt_signal
2268 };
2269