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