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