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