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