kern_sig.c revision 1.348 1 /* $NetBSD: kern_sig.c,v 1.348 2018/05/28 13:12:54 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.348 2018/05/28 13:12:54 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
908 if (ISSET(p->p_slflag, PSL_TRACED) && (p->p_pptr != p->p_opptr)) {
909 p->p_xsig = signo;
910 p->p_sigctx.ps_faked = true; // XXX
911 p->p_sigctx.ps_info._signo = signo;
912 p->p_sigctx.ps_info._code = ksi->ksi_code;
913 sigswitch(0, signo, false);
914 // XXX ktrpoint(KTR_PSIG)
915 mutex_exit(p->p_lock);
916 return;
917 }
918
919 mask = &l->l_sigmask;
920 ps = p->p_sigacts;
921
922 const bool caught = sigismember(&p->p_sigctx.ps_sigcatch, signo);
923 const bool masked = sigismember(mask, signo);
924 if (caught && !masked) {
925 mutex_exit(proc_lock);
926 l->l_ru.ru_nsignals++;
927 kpsendsig(l, ksi, mask);
928 mutex_exit(p->p_lock);
929 if (ktrpoint(KTR_PSIG)) {
930 if (p->p_emul->e_ktrpsig)
931 p->p_emul->e_ktrpsig(signo,
932 SIGACTION_PS(ps, signo).sa_handler,
933 mask, ksi);
934 else
935 ktrpsig(signo,
936 SIGACTION_PS(ps, signo).sa_handler,
937 mask, ksi);
938 }
939 return;
940 }
941
942 /*
943 * If the signal is masked or ignored, then unmask it and
944 * reset it to the default action so that the process or
945 * its tracer will be notified.
946 */
947 const bool ignored = SIGACTION_PS(ps, signo).sa_handler == SIG_IGN;
948 if (masked || ignored) {
949 mutex_enter(&ps->sa_mutex);
950 sigdelset(mask, signo);
951 sigdelset(&p->p_sigctx.ps_sigcatch, signo);
952 sigdelset(&p->p_sigctx.ps_sigignore, signo);
953 sigdelset(&SIGACTION_PS(ps, signo).sa_mask, signo);
954 SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
955 mutex_exit(&ps->sa_mutex);
956 }
957
958 kpsignal2(p, ksi);
959 mutex_exit(p->p_lock);
960 mutex_exit(proc_lock);
961 }
962
963 /*
964 * Fill in signal information and signal the parent for a child status change.
965 */
966 void
967 child_psignal(struct proc *p, int mask)
968 {
969 ksiginfo_t ksi;
970 struct proc *q;
971 int xsig;
972
973 KASSERT(mutex_owned(proc_lock));
974 KASSERT(mutex_owned(p->p_lock));
975
976 xsig = p->p_xsig;
977
978 KSI_INIT(&ksi);
979 ksi.ksi_signo = SIGCHLD;
980 ksi.ksi_code = (xsig == SIGCONT ? CLD_CONTINUED : CLD_STOPPED);
981 ksi.ksi_pid = p->p_pid;
982 ksi.ksi_uid = kauth_cred_geteuid(p->p_cred);
983 ksi.ksi_status = xsig;
984 ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
985 ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
986
987 q = p->p_pptr;
988
989 mutex_exit(p->p_lock);
990 mutex_enter(q->p_lock);
991
992 if ((q->p_sflag & mask) == 0)
993 kpsignal2(q, &ksi);
994
995 mutex_exit(q->p_lock);
996 mutex_enter(p->p_lock);
997 }
998
999 void
1000 psignal(struct proc *p, int signo)
1001 {
1002 ksiginfo_t ksi;
1003
1004 KASSERT(!cpu_intr_p());
1005 KASSERT(mutex_owned(proc_lock));
1006
1007 KSI_INIT_EMPTY(&ksi);
1008 ksi.ksi_signo = signo;
1009 mutex_enter(p->p_lock);
1010 kpsignal2(p, &ksi);
1011 mutex_exit(p->p_lock);
1012 }
1013
1014 void
1015 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
1016 {
1017 fdfile_t *ff;
1018 file_t *fp;
1019 fdtab_t *dt;
1020
1021 KASSERT(!cpu_intr_p());
1022 KASSERT(mutex_owned(proc_lock));
1023
1024 if ((p->p_sflag & PS_WEXIT) == 0 && data) {
1025 size_t fd;
1026 filedesc_t *fdp = p->p_fd;
1027
1028 /* XXXSMP locking */
1029 ksi->ksi_fd = -1;
1030 dt = fdp->fd_dt;
1031 for (fd = 0; fd < dt->dt_nfiles; fd++) {
1032 if ((ff = dt->dt_ff[fd]) == NULL)
1033 continue;
1034 if ((fp = ff->ff_file) == NULL)
1035 continue;
1036 if (fp->f_data == data) {
1037 ksi->ksi_fd = fd;
1038 break;
1039 }
1040 }
1041 }
1042 mutex_enter(p->p_lock);
1043 kpsignal2(p, ksi);
1044 mutex_exit(p->p_lock);
1045 }
1046
1047 /*
1048 * sigismasked:
1049 *
1050 * Returns true if signal is ignored or masked for the specified LWP.
1051 */
1052 int
1053 sigismasked(struct lwp *l, int sig)
1054 {
1055 struct proc *p = l->l_proc;
1056
1057 return sigismember(&p->p_sigctx.ps_sigignore, sig) ||
1058 sigismember(&l->l_sigmask, sig);
1059 }
1060
1061 /*
1062 * sigpost:
1063 *
1064 * Post a pending signal to an LWP. Returns non-zero if the LWP may
1065 * be able to take the signal.
1066 */
1067 static int
1068 sigpost(struct lwp *l, sig_t action, int prop, int sig)
1069 {
1070 int rv, masked;
1071 struct proc *p = l->l_proc;
1072
1073 KASSERT(mutex_owned(p->p_lock));
1074
1075 /*
1076 * If the LWP is on the way out, sigclear() will be busy draining all
1077 * pending signals. Don't give it more.
1078 */
1079 if (l->l_refcnt == 0)
1080 return 0;
1081
1082 SDT_PROBE(proc, kernel, , signal__send, l, p, sig, 0, 0);
1083
1084 /*
1085 * Have the LWP check for signals. This ensures that even if no LWP
1086 * is found to take the signal immediately, it should be taken soon.
1087 */
1088 lwp_lock(l);
1089 l->l_flag |= LW_PENDSIG;
1090
1091 /*
1092 * SIGCONT can be masked, but if LWP is stopped, it needs restart.
1093 * Note: SIGKILL and SIGSTOP cannot be masked.
1094 */
1095 masked = sigismember(&l->l_sigmask, sig);
1096 if (masked && ((prop & SA_CONT) == 0 || l->l_stat != LSSTOP)) {
1097 lwp_unlock(l);
1098 return 0;
1099 }
1100
1101 /*
1102 * If killing the process, make it run fast.
1103 */
1104 if (__predict_false((prop & SA_KILL) != 0) &&
1105 action == SIG_DFL && l->l_priority < MAXPRI_USER) {
1106 KASSERT(l->l_class == SCHED_OTHER);
1107 lwp_changepri(l, MAXPRI_USER);
1108 }
1109
1110 /*
1111 * If the LWP is running or on a run queue, then we win. If it's
1112 * sleeping interruptably, wake it and make it take the signal. If
1113 * the sleep isn't interruptable, then the chances are it will get
1114 * to see the signal soon anyhow. If suspended, it can't take the
1115 * signal right now. If it's LWP private or for all LWPs, save it
1116 * for later; otherwise punt.
1117 */
1118 rv = 0;
1119
1120 switch (l->l_stat) {
1121 case LSRUN:
1122 case LSONPROC:
1123 lwp_need_userret(l);
1124 rv = 1;
1125 break;
1126
1127 case LSSLEEP:
1128 if ((l->l_flag & LW_SINTR) != 0) {
1129 /* setrunnable() will release the lock. */
1130 setrunnable(l);
1131 return 1;
1132 }
1133 break;
1134
1135 case LSSUSPENDED:
1136 if ((prop & SA_KILL) != 0 && (l->l_flag & LW_WCORE) != 0) {
1137 /* lwp_continue() will release the lock. */
1138 lwp_continue(l);
1139 return 1;
1140 }
1141 break;
1142
1143 case LSSTOP:
1144 if ((prop & SA_STOP) != 0)
1145 break;
1146
1147 /*
1148 * If the LWP is stopped and we are sending a continue
1149 * signal, then start it again.
1150 */
1151 if ((prop & SA_CONT) != 0) {
1152 if (l->l_wchan != NULL) {
1153 l->l_stat = LSSLEEP;
1154 p->p_nrlwps++;
1155 rv = 1;
1156 break;
1157 }
1158 /* setrunnable() will release the lock. */
1159 setrunnable(l);
1160 return 1;
1161 } else if (l->l_wchan == NULL || (l->l_flag & LW_SINTR) != 0) {
1162 /* setrunnable() will release the lock. */
1163 setrunnable(l);
1164 return 1;
1165 }
1166 break;
1167
1168 default:
1169 break;
1170 }
1171
1172 lwp_unlock(l);
1173 return rv;
1174 }
1175
1176 /*
1177 * Notify an LWP that it has a pending signal.
1178 */
1179 void
1180 signotify(struct lwp *l)
1181 {
1182 KASSERT(lwp_locked(l, NULL));
1183
1184 l->l_flag |= LW_PENDSIG;
1185 lwp_need_userret(l);
1186 }
1187
1188 /*
1189 * Find an LWP within process p that is waiting on signal ksi, and hand
1190 * it on.
1191 */
1192 static int
1193 sigunwait(struct proc *p, const ksiginfo_t *ksi)
1194 {
1195 struct lwp *l;
1196 int signo;
1197
1198 KASSERT(mutex_owned(p->p_lock));
1199
1200 signo = ksi->ksi_signo;
1201
1202 if (ksi->ksi_lid != 0) {
1203 /*
1204 * Signal came via _lwp_kill(). Find the LWP and see if
1205 * it's interested.
1206 */
1207 if ((l = lwp_find(p, ksi->ksi_lid)) == NULL)
1208 return 0;
1209 if (l->l_sigwaited == NULL ||
1210 !sigismember(&l->l_sigwaitset, signo))
1211 return 0;
1212 } else {
1213 /*
1214 * Look for any LWP that may be interested.
1215 */
1216 LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) {
1217 KASSERT(l->l_sigwaited != NULL);
1218 if (sigismember(&l->l_sigwaitset, signo))
1219 break;
1220 }
1221 }
1222
1223 if (l != NULL) {
1224 l->l_sigwaited->ksi_info = ksi->ksi_info;
1225 l->l_sigwaited = NULL;
1226 LIST_REMOVE(l, l_sigwaiter);
1227 cv_signal(&l->l_sigcv);
1228 return 1;
1229 }
1230
1231 return 0;
1232 }
1233
1234 /*
1235 * Send the signal to the process. If the signal has an action, the action
1236 * is usually performed by the target process rather than the caller; we add
1237 * the signal to the set of pending signals for the process.
1238 *
1239 * Exceptions:
1240 * o When a stop signal is sent to a sleeping process that takes the
1241 * default action, the process is stopped without awakening it.
1242 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1243 * regardless of the signal action (eg, blocked or ignored).
1244 *
1245 * Other ignored signals are discarded immediately.
1246 */
1247 int
1248 kpsignal2(struct proc *p, ksiginfo_t *ksi)
1249 {
1250 int prop, signo = ksi->ksi_signo;
1251 struct lwp *l = NULL;
1252 ksiginfo_t *kp;
1253 lwpid_t lid;
1254 sig_t action;
1255 bool toall, debtrap = false;
1256 int error = 0;
1257
1258 KASSERT(!cpu_intr_p());
1259 KASSERT(mutex_owned(proc_lock));
1260 KASSERT(mutex_owned(p->p_lock));
1261 KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
1262 KASSERT(signo > 0 && signo < NSIG);
1263
1264 /*
1265 * If the process is being created by fork, is a zombie or is
1266 * exiting, then just drop the signal here and bail out.
1267 */
1268 if (p->p_stat == SIDL && signo == SIGTRAP
1269 && (p->p_slflag & PSL_TRACED)) {
1270 /* allow an initial SIGTRAP for traced processes */
1271 debtrap = true;
1272 } else if (p->p_stat != SACTIVE && p->p_stat != SSTOP) {
1273 return 0;
1274 }
1275
1276 /* XXX for core dump/debugger */
1277 p->p_sigctx.ps_lwp = ksi->ksi_lid;
1278 p->p_sigctx.ps_info = ksi->ksi_info;
1279
1280 /*
1281 * Notify any interested parties of the signal.
1282 */
1283 KNOTE(&p->p_klist, NOTE_SIGNAL | signo);
1284
1285 /*
1286 * Some signals including SIGKILL must act on the entire process.
1287 */
1288 kp = NULL;
1289 prop = sigprop[signo];
1290 toall = ((prop & SA_TOALL) != 0);
1291 lid = toall ? 0 : ksi->ksi_lid;
1292
1293 /*
1294 * If proc is traced, always give parent a chance.
1295 */
1296 if (p->p_slflag & PSL_TRACED) {
1297 action = SIG_DFL;
1298
1299 if (lid == 0) {
1300 /*
1301 * If the process is being traced and the signal
1302 * is being caught, make sure to save any ksiginfo.
1303 */
1304 if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1305 goto discard;
1306 if ((error = sigput(&p->p_sigpend, p, kp)) != 0)
1307 goto out;
1308 }
1309 } else {
1310
1311 /*
1312 * If the signal is being ignored, then drop it. Note: we
1313 * don't set SIGCONT in ps_sigignore, and if it is set to
1314 * SIG_IGN, action will be SIG_DFL here.
1315 */
1316 if (sigismember(&p->p_sigctx.ps_sigignore, signo))
1317 goto discard;
1318
1319 else if (sigismember(&p->p_sigctx.ps_sigcatch, signo))
1320 action = SIG_CATCH;
1321 else {
1322 action = SIG_DFL;
1323
1324 /*
1325 * If sending a tty stop signal to a member of an
1326 * orphaned process group, discard the signal here if
1327 * the action is default; don't stop the process below
1328 * if sleeping, and don't clear any pending SIGCONT.
1329 */
1330 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
1331 goto discard;
1332
1333 if (prop & SA_KILL && p->p_nice > NZERO)
1334 p->p_nice = NZERO;
1335 }
1336 }
1337
1338 /*
1339 * If stopping or continuing a process, discard any pending
1340 * signals that would do the inverse.
1341 */
1342 if ((prop & (SA_CONT | SA_STOP)) != 0) {
1343 ksiginfoq_t kq;
1344
1345 ksiginfo_queue_init(&kq);
1346 if ((prop & SA_CONT) != 0)
1347 sigclear(&p->p_sigpend, &stopsigmask, &kq);
1348 if ((prop & SA_STOP) != 0)
1349 sigclear(&p->p_sigpend, &contsigmask, &kq);
1350 ksiginfo_queue_drain(&kq); /* XXXSMP */
1351 }
1352
1353 /*
1354 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
1355 * please!), check if any LWPs are waiting on it. If yes, pass on
1356 * the signal info. The signal won't be processed further here.
1357 */
1358 if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) &&
1359 p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 &&
1360 sigunwait(p, ksi))
1361 goto discard;
1362
1363 /*
1364 * XXXSMP Should be allocated by the caller, we're holding locks
1365 * here.
1366 */
1367 if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1368 goto discard;
1369
1370 /*
1371 * LWP private signals are easy - just find the LWP and post
1372 * the signal to it.
1373 */
1374 if (lid != 0) {
1375 if (__predict_false(debtrap)) {
1376 l = LIST_FIRST(&p->p_lwps);
1377 if (l->l_lid != lid)
1378 l = NULL;
1379 } else {
1380 l = lwp_find(p, lid);
1381 }
1382 if (l != NULL) {
1383 if ((error = sigput(&l->l_sigpend, p, kp)) != 0)
1384 goto out;
1385 membar_producer();
1386 if (sigpost(l, action, prop, kp->ksi_signo) != 0)
1387 signo = -1;
1388 }
1389 goto out;
1390 }
1391
1392 /*
1393 * Some signals go to all LWPs, even if posted with _lwp_kill()
1394 * or for an SA process.
1395 */
1396 if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
1397 if ((p->p_slflag & PSL_TRACED) != 0)
1398 goto deliver;
1399
1400 /*
1401 * If SIGCONT is default (or ignored) and process is
1402 * asleep, we are finished; the process should not
1403 * be awakened.
1404 */
1405 if ((prop & SA_CONT) != 0 && action == SIG_DFL)
1406 goto out;
1407 } else {
1408 /*
1409 * Process is stopped or stopping.
1410 * - If traced, then no action is needed, unless killing.
1411 * - Run the process only if sending SIGCONT or SIGKILL.
1412 */
1413 if ((p->p_slflag & PSL_TRACED) != 0 && signo != SIGKILL) {
1414 goto out;
1415 }
1416 if ((prop & SA_CONT) != 0 || signo == SIGKILL) {
1417 /*
1418 * Re-adjust p_nstopchild if the process was
1419 * stopped but not yet collected by its parent.
1420 */
1421 if (p->p_stat == SSTOP && !p->p_waited)
1422 p->p_pptr->p_nstopchild--;
1423 p->p_stat = SACTIVE;
1424 p->p_sflag &= ~PS_STOPPING;
1425 if (p->p_slflag & PSL_TRACED) {
1426 KASSERT(signo == SIGKILL);
1427 goto deliver;
1428 }
1429 /*
1430 * Do not make signal pending if SIGCONT is default.
1431 *
1432 * If the process catches SIGCONT, let it handle the
1433 * signal itself (if waiting on event - process runs,
1434 * otherwise continues sleeping).
1435 */
1436 if ((prop & SA_CONT) != 0) {
1437 p->p_xsig = SIGCONT;
1438 p->p_sflag |= PS_CONTINUED;
1439 child_psignal(p, 0);
1440 if (action == SIG_DFL) {
1441 KASSERT(signo != SIGKILL);
1442 goto deliver;
1443 }
1444 }
1445 } else if ((prop & SA_STOP) != 0) {
1446 /*
1447 * Already stopped, don't need to stop again.
1448 * (If we did the shell could get confused.)
1449 */
1450 goto out;
1451 }
1452 }
1453 /*
1454 * Make signal pending.
1455 */
1456 KASSERT((p->p_slflag & PSL_TRACED) == 0);
1457 if ((error = sigput(&p->p_sigpend, p, kp)) != 0)
1458 goto out;
1459 deliver:
1460 /*
1461 * Before we set LW_PENDSIG on any LWP, ensure that the signal is
1462 * visible on the per process list (for sigispending()). This
1463 * is unlikely to be needed in practice, but...
1464 */
1465 membar_producer();
1466
1467 /*
1468 * Try to find an LWP that can take the signal.
1469 */
1470 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1471 if (sigpost(l, action, prop, kp->ksi_signo) && !toall)
1472 break;
1473 }
1474 signo = -1;
1475 out:
1476 /*
1477 * If the ksiginfo wasn't used, then bin it. XXXSMP freeing memory
1478 * with locks held. The caller should take care of this.
1479 */
1480 ksiginfo_free(kp);
1481 if (signo == -1)
1482 return error;
1483 discard:
1484 SDT_PROBE(proc, kernel, , signal__discard, l, p, signo, 0, 0);
1485 return error;
1486 }
1487
1488 void
1489 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
1490 {
1491 struct proc *p = l->l_proc;
1492
1493 KASSERT(mutex_owned(p->p_lock));
1494 (*p->p_emul->e_sendsig)(ksi, mask);
1495 }
1496
1497 /*
1498 * Stop any LWPs sleeping interruptably.
1499 */
1500 static void
1501 proc_stop_lwps(struct proc *p)
1502 {
1503 struct lwp *l;
1504
1505 KASSERT(mutex_owned(p->p_lock));
1506 KASSERT((p->p_sflag & PS_STOPPING) != 0);
1507
1508 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1509 lwp_lock(l);
1510 if (l->l_stat == LSSLEEP && (l->l_flag & LW_SINTR) != 0) {
1511 l->l_stat = LSSTOP;
1512 p->p_nrlwps--;
1513 }
1514 lwp_unlock(l);
1515 }
1516 }
1517
1518 /*
1519 * Finish stopping of a process. Mark it stopped and notify the parent.
1520 *
1521 * Drop p_lock briefly if PS_NOTIFYSTOP is set and ppsig is true.
1522 */
1523 static void
1524 proc_stop_done(struct proc *p, int ppmask)
1525 {
1526
1527 KASSERT(mutex_owned(proc_lock));
1528 KASSERT(mutex_owned(p->p_lock));
1529 KASSERT((p->p_sflag & PS_STOPPING) != 0);
1530 KASSERT(p->p_nrlwps == 0 || (p->p_nrlwps == 1 && p == curproc));
1531
1532 p->p_sflag &= ~PS_STOPPING;
1533 p->p_stat = SSTOP;
1534 p->p_waited = 0;
1535 p->p_pptr->p_nstopchild++;
1536 if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
1537 /* child_psignal drops p_lock briefly. */
1538 child_psignal(p, ppmask);
1539 cv_broadcast(&p->p_pptr->p_waitcv);
1540 }
1541 }
1542
1543 /*
1544 * Stop the current process and switch away when being stopped or traced.
1545 */
1546 void
1547 sigswitch(int ppmask, int signo, bool relock)
1548 {
1549 struct lwp *l = curlwp;
1550 struct proc *p = l->l_proc;
1551 int biglocks;
1552
1553 KASSERT(mutex_owned(p->p_lock));
1554 KASSERT(l->l_stat == LSONPROC);
1555 KASSERT(p->p_nrlwps > 0);
1556
1557 /*
1558 * On entry we know that the process needs to stop. If it's
1559 * the result of a 'sideways' stop signal that has been sourced
1560 * through issignal(), then stop other LWPs in the process too.
1561 */
1562 if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
1563 KASSERT(signo != 0);
1564 proc_stop(p, signo);
1565 KASSERT(p->p_nrlwps > 0);
1566 }
1567
1568 /*
1569 * If we are the last live LWP, and the stop was a result of
1570 * a new signal, then signal the parent.
1571 */
1572 if ((p->p_sflag & PS_STOPPING) != 0) {
1573 if (relock && !mutex_tryenter(proc_lock)) {
1574 mutex_exit(p->p_lock);
1575 mutex_enter(proc_lock);
1576 mutex_enter(p->p_lock);
1577 }
1578
1579 if (p->p_nrlwps == 1 && (p->p_sflag & PS_STOPPING) != 0) {
1580 /*
1581 * Note that proc_stop_done() can drop
1582 * p->p_lock briefly.
1583 */
1584 proc_stop_done(p, ppmask);
1585 }
1586
1587 mutex_exit(proc_lock);
1588 }
1589
1590 /*
1591 * Unlock and switch away.
1592 */
1593 KERNEL_UNLOCK_ALL(l, &biglocks);
1594 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
1595 p->p_nrlwps--;
1596 lwp_lock(l);
1597 KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSLEEP);
1598 l->l_stat = LSSTOP;
1599 lwp_unlock(l);
1600 }
1601
1602 mutex_exit(p->p_lock);
1603 lwp_lock(l);
1604 mi_switch(l);
1605 KERNEL_LOCK(biglocks, l);
1606 mutex_enter(p->p_lock);
1607 }
1608
1609 /*
1610 * Check for a signal from the debugger.
1611 */
1612 static int
1613 sigchecktrace(void)
1614 {
1615 struct lwp *l = curlwp;
1616 struct proc *p = l->l_proc;
1617 int signo;
1618
1619 KASSERT(mutex_owned(p->p_lock));
1620
1621 /* If there's a pending SIGKILL, process it immediately. */
1622 if (sigismember(&p->p_sigpend.sp_set, SIGKILL))
1623 return 0;
1624
1625 /*
1626 * If we are no longer being traced, or the parent didn't
1627 * give us a signal, or we're stopping, look for more signals.
1628 */
1629 if ((p->p_slflag & PSL_TRACED) == 0 || p->p_xsig == 0 ||
1630 (p->p_sflag & PS_STOPPING) != 0)
1631 return 0;
1632
1633 /*
1634 * If the new signal is being masked, look for other signals.
1635 * `p->p_sigctx.ps_siglist |= mask' is done in setrunnable().
1636 */
1637 signo = p->p_xsig;
1638 p->p_xsig = 0;
1639 if (sigismember(&l->l_sigmask, signo)) {
1640 signo = 0;
1641 }
1642 return signo;
1643 }
1644
1645 /*
1646 * If the current process has received a signal (should be caught or cause
1647 * termination, should interrupt current syscall), return the signal number.
1648 *
1649 * Stop signals with default action are processed immediately, then cleared;
1650 * they aren't returned. This is checked after each entry to the system for
1651 * a syscall or trap.
1652 *
1653 * We will also return -1 if the process is exiting and the current LWP must
1654 * follow suit.
1655 */
1656 int
1657 issignal(struct lwp *l)
1658 {
1659 struct proc *p;
1660 int signo, prop;
1661 sigpend_t *sp;
1662 sigset_t ss;
1663
1664 p = l->l_proc;
1665 sp = NULL;
1666 signo = 0;
1667
1668 KASSERT(p == curproc);
1669 KASSERT(mutex_owned(p->p_lock));
1670
1671 for (;;) {
1672 /* Discard any signals that we have decided not to take. */
1673 if (signo != 0) {
1674 (void)sigget(sp, NULL, signo, NULL);
1675 }
1676
1677 /*
1678 * If the process is stopped/stopping, then stop ourselves
1679 * now that we're on the kernel/userspace boundary. When
1680 * we awaken, check for a signal from the debugger.
1681 */
1682 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
1683 sigswitch(PS_NOCLDSTOP, 0, true);
1684 signo = sigchecktrace();
1685 } else
1686 signo = 0;
1687
1688 /* Signals from the debugger are "out of band". */
1689 sp = NULL;
1690
1691 /*
1692 * If the debugger didn't provide a signal, find a pending
1693 * signal from our set. Check per-LWP signals first, and
1694 * then per-process.
1695 */
1696 if (signo == 0) {
1697 sp = &l->l_sigpend;
1698 ss = sp->sp_set;
1699 if ((p->p_lflag & PL_PPWAIT) != 0)
1700 sigminusset(&vforksigmask, &ss);
1701 sigminusset(&l->l_sigmask, &ss);
1702
1703 if ((signo = firstsig(&ss)) == 0) {
1704 sp = &p->p_sigpend;
1705 ss = sp->sp_set;
1706 if ((p->p_lflag & PL_PPWAIT) != 0)
1707 sigminusset(&vforksigmask, &ss);
1708 sigminusset(&l->l_sigmask, &ss);
1709
1710 if ((signo = firstsig(&ss)) == 0) {
1711 /*
1712 * No signal pending - clear the
1713 * indicator and bail out.
1714 */
1715 lwp_lock(l);
1716 l->l_flag &= ~LW_PENDSIG;
1717 lwp_unlock(l);
1718 sp = NULL;
1719 break;
1720 }
1721 }
1722 }
1723
1724 /*
1725 * We should see pending but ignored signals only if
1726 * we are being traced.
1727 */
1728 if (sigismember(&p->p_sigctx.ps_sigignore, signo) &&
1729 (p->p_slflag & PSL_TRACED) == 0) {
1730 /* Discard the signal. */
1731 continue;
1732 }
1733
1734 /*
1735 * If traced, always stop, and stay stopped until released
1736 * by the debugger. If the our parent is our debugger waiting
1737 * for us and we vforked, don't hang as we could deadlock.
1738 */
1739 if (ISSET(p->p_slflag, PSL_TRACED) && signo != SIGKILL &&
1740 !(ISSET(p->p_lflag, PL_PPWAIT) &&
1741 (p->p_pptr == p->p_opptr))) {
1742 /*
1743 * Take the signal, but don't remove it from the
1744 * siginfo queue, because the debugger can send
1745 * it later.
1746 */
1747 if (sp)
1748 sigdelset(&sp->sp_set, signo);
1749 p->p_xsig = signo;
1750
1751 /* Handling of signal trace */
1752 sigswitch(0, signo, true);
1753
1754 /* Check for a signal from the debugger. */
1755 if ((signo = sigchecktrace()) == 0)
1756 continue;
1757
1758 /* Signals from the debugger are "out of band". */
1759 sp = NULL;
1760 }
1761
1762 prop = sigprop[signo];
1763
1764 /*
1765 * Decide whether the signal should be returned.
1766 */
1767 switch ((long)SIGACTION(p, signo).sa_handler) {
1768 case (long)SIG_DFL:
1769 /*
1770 * Don't take default actions on system processes.
1771 */
1772 if (p->p_pid <= 1) {
1773 #ifdef DIAGNOSTIC
1774 /*
1775 * Are you sure you want to ignore SIGSEGV
1776 * in init? XXX
1777 */
1778 printf_nolog("Process (pid %d) got sig %d\n",
1779 p->p_pid, signo);
1780 #endif
1781 continue;
1782 }
1783
1784 /*
1785 * If there is a pending stop signal to process with
1786 * default action, stop here, then clear the signal.
1787 * However, if process is member of an orphaned
1788 * process group, ignore tty stop signals.
1789 */
1790 if (prop & SA_STOP) {
1791 /*
1792 * XXX Don't hold proc_lock for p_lflag,
1793 * but it's not a big deal.
1794 */
1795 if ((ISSET(p->p_slflag, PSL_TRACED) &&
1796 !(ISSET(p->p_lflag, PL_PPWAIT) &&
1797 (p->p_pptr == p->p_opptr))) ||
1798 ((p->p_lflag & PL_ORPHANPG) != 0 &&
1799 prop & SA_TTYSTOP)) {
1800 /* Ignore the signal. */
1801 continue;
1802 }
1803 /* Take the signal. */
1804 (void)sigget(sp, NULL, signo, NULL);
1805 p->p_xsig = signo;
1806 p->p_sflag &= ~PS_CONTINUED;
1807 signo = 0;
1808 sigswitch(PS_NOCLDSTOP, p->p_xsig, true);
1809 } else if (prop & SA_IGNORE) {
1810 /*
1811 * Except for SIGCONT, shouldn't get here.
1812 * Default action is to ignore; drop it.
1813 */
1814 continue;
1815 }
1816 break;
1817
1818 case (long)SIG_IGN:
1819 #ifdef DEBUG_ISSIGNAL
1820 /*
1821 * Masking above should prevent us ever trying
1822 * to take action on an ignored signal other
1823 * than SIGCONT, unless process is traced.
1824 */
1825 if ((prop & SA_CONT) == 0 &&
1826 (p->p_slflag & PSL_TRACED) == 0)
1827 printf_nolog("issignal\n");
1828 #endif
1829 continue;
1830
1831 default:
1832 /*
1833 * This signal has an action, let postsig() process
1834 * it.
1835 */
1836 break;
1837 }
1838
1839 break;
1840 }
1841
1842 l->l_sigpendset = sp;
1843 return signo;
1844 }
1845
1846 /*
1847 * Take the action for the specified signal
1848 * from the current set of pending signals.
1849 */
1850 void
1851 postsig(int signo)
1852 {
1853 struct lwp *l;
1854 struct proc *p;
1855 struct sigacts *ps;
1856 sig_t action;
1857 sigset_t *returnmask;
1858 ksiginfo_t ksi;
1859
1860 l = curlwp;
1861 p = l->l_proc;
1862 ps = p->p_sigacts;
1863
1864 KASSERT(mutex_owned(p->p_lock));
1865 KASSERT(signo > 0);
1866
1867 /*
1868 * Set the new mask value and also defer further occurrences of this
1869 * signal.
1870 *
1871 * Special case: user has done a sigsuspend. Here the current mask is
1872 * not of interest, but rather the mask from before the sigsuspend is
1873 * what we want restored after the signal processing is completed.
1874 */
1875 if (l->l_sigrestore) {
1876 returnmask = &l->l_sigoldmask;
1877 l->l_sigrestore = 0;
1878 } else
1879 returnmask = &l->l_sigmask;
1880
1881 /*
1882 * Commit to taking the signal before releasing the mutex.
1883 */
1884 action = SIGACTION_PS(ps, signo).sa_handler;
1885 l->l_ru.ru_nsignals++;
1886 if (l->l_sigpendset == NULL) {
1887 /* From the debugger */
1888 if (p->p_sigctx.ps_faked &&
1889 signo == p->p_sigctx.ps_info._signo) {
1890 KSI_INIT(&ksi);
1891 ksi.ksi_info = p->p_sigctx.ps_info;
1892 ksi.ksi_lid = p->p_sigctx.ps_lwp;
1893 p->p_sigctx.ps_faked = false;
1894 } else {
1895 if (!siggetinfo(&l->l_sigpend, &ksi, signo))
1896 (void)siggetinfo(&p->p_sigpend, &ksi, signo);
1897 }
1898 } else
1899 sigget(l->l_sigpendset, &ksi, signo, NULL);
1900
1901 if (ktrpoint(KTR_PSIG)) {
1902 mutex_exit(p->p_lock);
1903 if (p->p_emul->e_ktrpsig)
1904 p->p_emul->e_ktrpsig(signo, action,
1905 returnmask, &ksi);
1906 else
1907 ktrpsig(signo, action, returnmask, &ksi);
1908 mutex_enter(p->p_lock);
1909 }
1910
1911 SDT_PROBE(proc, kernel, , signal__handle, signo, &ksi, action, 0, 0);
1912
1913 if (action == SIG_DFL) {
1914 /*
1915 * Default action, where the default is to kill
1916 * the process. (Other cases were ignored above.)
1917 */
1918 sigexit(l, signo);
1919 return;
1920 }
1921
1922 /*
1923 * If we get here, the signal must be caught.
1924 */
1925 #ifdef DIAGNOSTIC
1926 if (action == SIG_IGN || sigismember(&l->l_sigmask, signo))
1927 panic("postsig action");
1928 #endif
1929
1930 kpsendsig(l, &ksi, returnmask);
1931 }
1932
1933 /*
1934 * sendsig:
1935 *
1936 * Default signal delivery method for NetBSD.
1937 */
1938 void
1939 sendsig(const struct ksiginfo *ksi, const sigset_t *mask)
1940 {
1941 struct sigacts *sa;
1942 int sig;
1943
1944 sig = ksi->ksi_signo;
1945 sa = curproc->p_sigacts;
1946
1947 switch (sa->sa_sigdesc[sig].sd_vers) {
1948 case 0:
1949 case 1:
1950 /* Compat for 1.6 and earlier. */
1951 if (sendsig_sigcontext_vec == NULL) {
1952 break;
1953 }
1954 (*sendsig_sigcontext_vec)(ksi, mask);
1955 return;
1956 case 2:
1957 case 3:
1958 sendsig_siginfo(ksi, mask);
1959 return;
1960 default:
1961 break;
1962 }
1963
1964 printf("sendsig: bad version %d\n", sa->sa_sigdesc[sig].sd_vers);
1965 sigexit(curlwp, SIGILL);
1966 }
1967
1968 /*
1969 * sendsig_reset:
1970 *
1971 * Reset the signal action. Called from emulation specific sendsig()
1972 * before unlocking to deliver the signal.
1973 */
1974 void
1975 sendsig_reset(struct lwp *l, int signo)
1976 {
1977 struct proc *p = l->l_proc;
1978 struct sigacts *ps = p->p_sigacts;
1979
1980 KASSERT(mutex_owned(p->p_lock));
1981
1982 p->p_sigctx.ps_lwp = 0;
1983 memset(&p->p_sigctx.ps_info, 0, sizeof(p->p_sigctx.ps_info));
1984
1985 mutex_enter(&ps->sa_mutex);
1986 sigplusset(&SIGACTION_PS(ps, signo).sa_mask, &l->l_sigmask);
1987 if (SIGACTION_PS(ps, signo).sa_flags & SA_RESETHAND) {
1988 sigdelset(&p->p_sigctx.ps_sigcatch, signo);
1989 if (signo != SIGCONT && sigprop[signo] & SA_IGNORE)
1990 sigaddset(&p->p_sigctx.ps_sigignore, signo);
1991 SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
1992 }
1993 mutex_exit(&ps->sa_mutex);
1994 }
1995
1996 /*
1997 * Kill the current process for stated reason.
1998 */
1999 void
2000 killproc(struct proc *p, const char *why)
2001 {
2002
2003 KASSERT(mutex_owned(proc_lock));
2004
2005 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
2006 uprintf_locked("sorry, pid %d was killed: %s\n", p->p_pid, why);
2007 psignal(p, SIGKILL);
2008 }
2009
2010 /*
2011 * Force the current process to exit with the specified signal, dumping core
2012 * if appropriate. We bypass the normal tests for masked and caught
2013 * signals, allowing unrecoverable failures to terminate the process without
2014 * changing signal state. Mark the accounting record with the signal
2015 * termination. If dumping core, save the signal number for the debugger.
2016 * Calls exit and does not return.
2017 */
2018 void
2019 sigexit(struct lwp *l, int signo)
2020 {
2021 int exitsig, error, docore;
2022 struct proc *p;
2023 struct lwp *t;
2024
2025 p = l->l_proc;
2026
2027 KASSERT(mutex_owned(p->p_lock));
2028 KERNEL_UNLOCK_ALL(l, NULL);
2029
2030 /*
2031 * Don't permit coredump() multiple times in the same process.
2032 * Call back into sigexit, where we will be suspended until
2033 * the deed is done. Note that this is a recursive call, but
2034 * LW_WCORE will prevent us from coming back this way.
2035 */
2036 if ((p->p_sflag & PS_WCORE) != 0) {
2037 lwp_lock(l);
2038 l->l_flag |= (LW_WCORE | LW_WEXIT | LW_WSUSPEND);
2039 lwp_unlock(l);
2040 mutex_exit(p->p_lock);
2041 lwp_userret(l);
2042 panic("sigexit 1");
2043 /* NOTREACHED */
2044 }
2045
2046 /* If process is already on the way out, then bail now. */
2047 if ((p->p_sflag & PS_WEXIT) != 0) {
2048 mutex_exit(p->p_lock);
2049 lwp_exit(l);
2050 panic("sigexit 2");
2051 /* NOTREACHED */
2052 }
2053
2054 /*
2055 * Prepare all other LWPs for exit. If dumping core, suspend them
2056 * so that their registers are available long enough to be dumped.
2057 */
2058 if ((docore = (sigprop[signo] & SA_CORE)) != 0) {
2059 p->p_sflag |= PS_WCORE;
2060 for (;;) {
2061 LIST_FOREACH(t, &p->p_lwps, l_sibling) {
2062 lwp_lock(t);
2063 if (t == l) {
2064 t->l_flag &= ~LW_WSUSPEND;
2065 lwp_unlock(t);
2066 continue;
2067 }
2068 t->l_flag |= (LW_WCORE | LW_WEXIT);
2069 lwp_suspend(l, t);
2070 }
2071
2072 if (p->p_nrlwps == 1)
2073 break;
2074
2075 /*
2076 * Kick any LWPs sitting in lwp_wait1(), and wait
2077 * for everyone else to stop before proceeding.
2078 */
2079 p->p_nlwpwait++;
2080 cv_broadcast(&p->p_lwpcv);
2081 cv_wait(&p->p_lwpcv, p->p_lock);
2082 p->p_nlwpwait--;
2083 }
2084 }
2085
2086 exitsig = signo;
2087 p->p_acflag |= AXSIG;
2088 memset(&p->p_sigctx.ps_info, 0, sizeof(p->p_sigctx.ps_info));
2089 p->p_sigctx.ps_info._signo = signo;
2090 p->p_sigctx.ps_info._code = SI_NOINFO;
2091
2092 if (docore) {
2093 mutex_exit(p->p_lock);
2094 error = (*coredump_vec)(l, NULL);
2095
2096 if (kern_logsigexit) {
2097 int uid = l->l_cred ?
2098 (int)kauth_cred_geteuid(l->l_cred) : -1;
2099
2100 if (error)
2101 log(LOG_INFO, lognocoredump, p->p_pid,
2102 p->p_comm, uid, signo, error);
2103 else
2104 log(LOG_INFO, logcoredump, p->p_pid,
2105 p->p_comm, uid, signo);
2106 }
2107
2108 #ifdef PAX_SEGVGUARD
2109 pax_segvguard(l, p->p_textvp, p->p_comm, true);
2110 #endif /* PAX_SEGVGUARD */
2111 /* Acquire the sched state mutex. exit1() will release it. */
2112 mutex_enter(p->p_lock);
2113 if (error == 0)
2114 p->p_sflag |= PS_COREDUMP;
2115 }
2116
2117 /* No longer dumping core. */
2118 p->p_sflag &= ~PS_WCORE;
2119
2120 exit1(l, 0, exitsig);
2121 /* NOTREACHED */
2122 }
2123
2124 /*
2125 * Put process 'p' into the stopped state and optionally, notify the parent.
2126 */
2127 void
2128 proc_stop(struct proc *p, int signo)
2129 {
2130 struct lwp *l;
2131
2132 KASSERT(mutex_owned(p->p_lock));
2133
2134 /*
2135 * First off, set the stopping indicator and bring all sleeping
2136 * LWPs to a halt so they are included in p->p_nrlwps. We musn't
2137 * unlock between here and the p->p_nrlwps check below.
2138 */
2139 p->p_sflag |= PS_STOPPING | PS_NOTIFYSTOP;
2140 membar_producer();
2141
2142 proc_stop_lwps(p);
2143
2144 /*
2145 * If there are no LWPs available to take the signal, then we
2146 * signal the parent process immediately. Otherwise, the last
2147 * LWP to stop will take care of it.
2148 */
2149
2150 if (p->p_nrlwps == 0) {
2151 proc_stop_done(p, PS_NOCLDSTOP);
2152 } else {
2153 /*
2154 * Have the remaining LWPs come to a halt, and trigger
2155 * proc_stop_callout() to ensure that they do.
2156 */
2157 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2158 sigpost(l, SIG_DFL, SA_STOP, signo);
2159 }
2160 callout_schedule(&proc_stop_ch, 1);
2161 }
2162 }
2163
2164 /*
2165 * When stopping a process, we do not immediatly set sleeping LWPs stopped,
2166 * but wait for them to come to a halt at the kernel-user boundary. This is
2167 * to allow LWPs to release any locks that they may hold before stopping.
2168 *
2169 * Non-interruptable sleeps can be long, and there is the potential for an
2170 * LWP to begin sleeping interruptably soon after the process has been set
2171 * stopping (PS_STOPPING). These LWPs will not notice that the process is
2172 * stopping, and so complete halt of the process and the return of status
2173 * information to the parent could be delayed indefinitely.
2174 *
2175 * To handle this race, proc_stop_callout() runs once per tick while there
2176 * are stopping processes in the system. It sets LWPs that are sleeping
2177 * interruptably into the LSSTOP state.
2178 *
2179 * Note that we are not concerned about keeping all LWPs stopped while the
2180 * process is stopped: stopped LWPs can awaken briefly to handle signals.
2181 * What we do need to ensure is that all LWPs in a stopping process have
2182 * stopped at least once, so that notification can be sent to the parent
2183 * process.
2184 */
2185 static void
2186 proc_stop_callout(void *cookie)
2187 {
2188 bool more, restart;
2189 struct proc *p;
2190
2191 (void)cookie;
2192
2193 do {
2194 restart = false;
2195 more = false;
2196
2197 mutex_enter(proc_lock);
2198 PROCLIST_FOREACH(p, &allproc) {
2199 mutex_enter(p->p_lock);
2200
2201 if ((p->p_sflag & PS_STOPPING) == 0) {
2202 mutex_exit(p->p_lock);
2203 continue;
2204 }
2205
2206 /* Stop any LWPs sleeping interruptably. */
2207 proc_stop_lwps(p);
2208 if (p->p_nrlwps == 0) {
2209 /*
2210 * We brought the process to a halt.
2211 * Mark it as stopped and notify the
2212 * parent.
2213 */
2214 if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
2215 /*
2216 * Note that proc_stop_done() will
2217 * drop p->p_lock briefly.
2218 * Arrange to restart and check
2219 * all processes again.
2220 */
2221 restart = true;
2222 }
2223 proc_stop_done(p, PS_NOCLDSTOP);
2224 } else
2225 more = true;
2226
2227 mutex_exit(p->p_lock);
2228 if (restart)
2229 break;
2230 }
2231 mutex_exit(proc_lock);
2232 } while (restart);
2233
2234 /*
2235 * If we noted processes that are stopping but still have
2236 * running LWPs, then arrange to check again in 1 tick.
2237 */
2238 if (more)
2239 callout_schedule(&proc_stop_ch, 1);
2240 }
2241
2242 /*
2243 * Given a process in state SSTOP, set the state back to SACTIVE and
2244 * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
2245 */
2246 void
2247 proc_unstop(struct proc *p)
2248 {
2249 struct lwp *l;
2250 int sig;
2251
2252 KASSERT(mutex_owned(proc_lock));
2253 KASSERT(mutex_owned(p->p_lock));
2254
2255 p->p_stat = SACTIVE;
2256 p->p_sflag &= ~PS_STOPPING;
2257 sig = p->p_xsig;
2258
2259 if (!p->p_waited)
2260 p->p_pptr->p_nstopchild--;
2261
2262 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2263 lwp_lock(l);
2264 if (l->l_stat != LSSTOP) {
2265 lwp_unlock(l);
2266 continue;
2267 }
2268 if (l->l_wchan == NULL) {
2269 setrunnable(l);
2270 continue;
2271 }
2272 if (sig && (l->l_flag & LW_SINTR) != 0) {
2273 setrunnable(l);
2274 sig = 0;
2275 } else {
2276 l->l_stat = LSSLEEP;
2277 p->p_nrlwps++;
2278 lwp_unlock(l);
2279 }
2280 }
2281 }
2282
2283 void
2284 proc_stoptrace(int trapno)
2285 {
2286 struct lwp *l = curlwp;
2287 struct proc *p = l->l_proc;
2288 struct sigacts *ps;
2289 sigset_t *mask;
2290 sig_t action;
2291 ksiginfo_t ksi;
2292 const int signo = SIGTRAP;
2293
2294 KASSERT((trapno == TRAP_SCE) || (trapno == TRAP_SCX));
2295
2296 KSI_INIT_TRAP(&ksi);
2297 ksi.ksi_lid = l->l_lid;
2298 ksi.ksi_info._signo = signo;
2299 ksi.ksi_info._code = trapno;
2300
2301 mutex_enter(p->p_lock);
2302
2303 /* Needed for ktrace */
2304 ps = p->p_sigacts;
2305 action = SIGACTION_PS(ps, signo).sa_handler;
2306 mask = &l->l_sigmask;
2307
2308 /* initproc (PID1) cannot became a debugger */
2309 KASSERT(p->p_pptr != initproc);
2310
2311 KASSERT(ISSET(p->p_slflag, PSL_TRACED));
2312 KASSERT(ISSET(p->p_slflag, PSL_SYSCALL));
2313
2314 p->p_xsig = signo;
2315 p->p_sigctx.ps_lwp = ksi.ksi_lid;
2316 p->p_sigctx.ps_info = ksi.ksi_info;
2317 sigswitch(0, signo, true);
2318 mutex_exit(p->p_lock);
2319
2320 if (ktrpoint(KTR_PSIG)) {
2321 if (p->p_emul->e_ktrpsig)
2322 p->p_emul->e_ktrpsig(signo, action, mask, &ksi);
2323 else
2324 ktrpsig(signo, action, mask, &ksi);
2325 }
2326 }
2327
2328 static int
2329 filt_sigattach(struct knote *kn)
2330 {
2331 struct proc *p = curproc;
2332
2333 kn->kn_obj = p;
2334 kn->kn_flags |= EV_CLEAR; /* automatically set */
2335
2336 mutex_enter(p->p_lock);
2337 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2338 mutex_exit(p->p_lock);
2339
2340 return 0;
2341 }
2342
2343 static void
2344 filt_sigdetach(struct knote *kn)
2345 {
2346 struct proc *p = kn->kn_obj;
2347
2348 mutex_enter(p->p_lock);
2349 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2350 mutex_exit(p->p_lock);
2351 }
2352
2353 /*
2354 * Signal knotes are shared with proc knotes, so we apply a mask to
2355 * the hint in order to differentiate them from process hints. This
2356 * could be avoided by using a signal-specific knote list, but probably
2357 * isn't worth the trouble.
2358 */
2359 static int
2360 filt_signal(struct knote *kn, long hint)
2361 {
2362
2363 if (hint & NOTE_SIGNAL) {
2364 hint &= ~NOTE_SIGNAL;
2365
2366 if (kn->kn_id == hint)
2367 kn->kn_data++;
2368 }
2369 return (kn->kn_data != 0);
2370 }
2371
2372 const struct filterops sig_filtops = {
2373 .f_isfd = 0,
2374 .f_attach = filt_sigattach,
2375 .f_detach = filt_sigdetach,
2376 .f_event = filt_signal,
2377 };
2378