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