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