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