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