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