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