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