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