Sat Nov 7 20:45:20 2015 UTC ()
Pull up following revision(s) (requested by pgoyette in ticket #1980):
	sys/kern/kern_sig.c: revision 1.321
When delivering a signal, it's possible that the process's state in
p_stat is SACTIVE yet p_sflag is PS_STOPPING (while waiting for other
lwp's to stop).  In that case, we don't want to adjust the parent's
p_nstopchild count.
Found by Robert Elz.


(snj)
diff -r1.289.4.8 -r1.289.4.9 src/sys/kern/kern_sig.c

cvs diff -r1.289.4.8 -r1.289.4.9 src/sys/kern/kern_sig.c (switch to unified diff)

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