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