1 /* $NetBSD: kern_sig.c,v 1.301 2009/12/20 04:49:09 rmind Exp $ */
4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
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.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
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.
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
65 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.301 2009/12/20 04:49:09 rmind Exp $");
71 #include "opt_ptrace.h"
72 #include "opt_compat_sunos.h"
73 #include "opt_compat_netbsd.h"
74 #include "opt_compat_netbsd32.h"
78 #define SIGPROP /* include signal properties table */
79 #include <sys/param.h>
80 #include <sys/signalvar.h>
82 #include <sys/systm.h>
84 #include <sys/ktrace.h>
85 #include <sys/syslog.h>
86 #include <sys/filedesc.h>
89 #include <sys/ucontext.h>
91 #include <sys/savar.h>
93 #include <sys/kauth.h>
95 #include <sys/callout.h>
96 #include <sys/atomic.h>
98 #include <sys/module.h>
102 #endif /* PAX_SEGVGUARD */
105 #include <uvm/uvm_extern.h>
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);
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
;
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
;
127 static struct pool_allocator sigactspool_allocator
= {
128 .pa_alloc
= sigacts_poolpage_alloc
,
129 .pa_free
= sigacts_poolpage_free
133 int kern_logsigexit
= 1;
135 int kern_logsigexit
= 0;
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";
143 static kauth_listener_t signal_listener
;
146 signal_listener_cb(kauth_cred_t cred
, kauth_action_t action
, void *cookie
,
147 void *arg0
, void *arg1
, void *arg2
, void *arg3
)
152 result
= KAUTH_RESULT_DEFER
;
154 signum
= (int)(unsigned long)arg1
;
156 if (action
!= KAUTH_PROCESS_SIGNAL
)
159 if (kauth_cred_uidmatch(cred
, p
->p_cred
) ||
160 (signum
== SIGCONT
&& (curproc
->p_session
== p
->p_session
)))
161 result
= KAUTH_RESULT_ALLOW
;
169 * Initialize global signal-related data structures.
175 sigactspool_allocator
.pa_pagesz
= (PAGE_SIZE
)*2;
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
);
181 siginfo_cache
= pool_cache_init(sizeof(siginfo_t
), 0, 0, 0,
182 "siginfo", NULL
, IPL_NONE
, NULL
, NULL
, NULL
);
184 ksiginfo_cache
= pool_cache_init(sizeof(ksiginfo_t
), 0, 0, 0,
185 "ksiginfo", NULL
, IPL_VM
, NULL
, NULL
, NULL
);
187 exechook_establish(ksiginfo_exechook
, NULL
);
189 callout_init(&proc_stop_ch
, CALLOUT_MPSAFE
);
190 callout_setfunc(&proc_stop_ch
, proc_stop_callout
, NULL
);
192 signal_listener
= kauth_listen_scope(KAUTH_SCOPE_PROCESS
,
193 signal_listener_cb
, NULL
);
197 * sigacts_poolpage_alloc:
199 * Allocate a page for the sigacts memory pool.
202 sigacts_poolpage_alloc(struct pool
*pp
, int flags
)
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
)
212 * sigacts_poolpage_free:
214 * Free a page on behalf of the sigacts memory pool.
217 sigacts_poolpage_free(struct pool
*pp
, void *v
)
220 uvm_km_free(kernel_map
, (vaddr_t
)v
, PAGE_SIZE
* 2, UVM_KMF_WIRED
);
226 * Create an initial sigacts structure, using the same signal state
227 * as of specified process. If 'share' is set, share the sigacts by
228 * holding a reference, otherwise just copy it from parent.
231 sigactsinit(struct proc
*pp
, int share
)
233 struct sigacts
*ps
= pp
->p_sigacts
, *ps2
;
235 if (__predict_false(share
)) {
236 atomic_inc_uint(&ps
->sa_refcnt
);
239 ps2
= pool_cache_get(sigacts_cache
, PR_WAITOK
);
240 mutex_init(&ps2
->sa_mutex
, MUTEX_DEFAULT
, IPL_SCHED
);
243 mutex_enter(&ps
->sa_mutex
);
244 memcpy(ps2
->sa_sigdesc
, ps
->sa_sigdesc
, sizeof(ps2
->sa_sigdesc
));
245 mutex_exit(&ps
->sa_mutex
);
252 * Make this process not share its sigacts, maintaining all signal state.
255 sigactsunshare(struct proc
*p
)
257 struct sigacts
*ps
, *oldps
= p
->p_sigacts
;
259 if (__predict_true(oldps
->sa_refcnt
== 1))
262 ps
= pool_cache_get(sigacts_cache
, PR_WAITOK
);
263 mutex_init(&ps
->sa_mutex
, MUTEX_DEFAULT
, IPL_SCHED
);
264 memset(ps
->sa_sigdesc
, 0, sizeof(ps
->sa_sigdesc
));
274 * Release a sigacts structure.
277 sigactsfree(struct sigacts
*ps
)
280 if (atomic_dec_uint_nv(&ps
->sa_refcnt
) == 0) {
281 mutex_destroy(&ps
->sa_mutex
);
282 pool_cache_put(sigacts_cache
, ps
);
289 * Initialize signal state for process 0; set to ignore signals that
290 * are ignored by default and disable the signal stack. Locking not
291 * required as the system is still cold.
294 siginit(struct proc
*p
)
301 sigemptyset(&contsigmask
);
302 sigemptyset(&stopsigmask
);
303 sigemptyset(&sigcantmask
);
304 for (signo
= 1; signo
< NSIG
; signo
++) {
305 prop
= sigprop
[signo
];
307 sigaddset(&contsigmask
, signo
);
309 sigaddset(&stopsigmask
, signo
);
310 if (prop
& SA_CANTMASK
)
311 sigaddset(&sigcantmask
, signo
);
312 if (prop
& SA_IGNORE
&& signo
!= SIGCONT
)
313 sigaddset(&p
->p_sigctx
.ps_sigignore
, signo
);
314 sigemptyset(&SIGACTION_PS(ps
, signo
).sa_mask
);
315 SIGACTION_PS(ps
, signo
).sa_flags
= SA_RESTART
;
317 sigemptyset(&p
->p_sigctx
.ps_sigcatch
);
318 p
->p_sflag
&= ~PS_NOCLDSTOP
;
320 ksiginfo_queue_init(&p
->p_sigpend
.sp_info
);
321 sigemptyset(&p
->p_sigpend
.sp_set
);
324 * Reset per LWP state.
326 l
= LIST_FIRST(&p
->p_lwps
);
327 l
->l_sigwaited
= NULL
;
328 l
->l_sigstk
.ss_flags
= SS_DISABLE
;
329 l
->l_sigstk
.ss_size
= 0;
330 l
->l_sigstk
.ss_sp
= 0;
331 ksiginfo_queue_init(&l
->l_sigpend
.sp_info
);
332 sigemptyset(&l
->l_sigpend
.sp_set
);
341 * Reset signals for an exec of the specified process.
344 execsigs(struct proc
*p
)
352 KASSERT(p
->p_nlwps
== 1);
358 * Reset caught signals. Held signals remain held through
359 * l->l_sigmask (unless they were caught, and are now ignored
362 * No need to lock yet, the process has only one LWP and
363 * at this point the sigacts are private to the process.
366 for (signo
= 1; signo
< NSIG
; signo
++) {
367 if (sigismember(&p
->p_sigctx
.ps_sigcatch
, signo
)) {
368 prop
= sigprop
[signo
];
369 if (prop
& SA_IGNORE
) {
370 if ((prop
& SA_CONT
) == 0)
371 sigaddset(&p
->p_sigctx
.ps_sigignore
,
373 sigaddset(&tset
, signo
);
375 SIGACTION_PS(ps
, signo
).sa_handler
= SIG_DFL
;
377 sigemptyset(&SIGACTION_PS(ps
, signo
).sa_mask
);
378 SIGACTION_PS(ps
, signo
).sa_flags
= SA_RESTART
;
380 ksiginfo_queue_init(&kq
);
382 mutex_enter(p
->p_lock
);
383 sigclearall(p
, &tset
, &kq
);
384 sigemptyset(&p
->p_sigctx
.ps_sigcatch
);
387 * Reset no zombies if child dies flag as Solaris does.
389 p
->p_flag
&= ~(PK_NOCLDWAIT
| PK_CLDSIGIGN
);
390 if (SIGACTION_PS(ps
, SIGCHLD
).sa_handler
== SIG_IGN
)
391 SIGACTION_PS(ps
, SIGCHLD
).sa_handler
= SIG_DFL
;
394 * Reset per-LWP state.
396 l
= LIST_FIRST(&p
->p_lwps
);
397 l
->l_sigwaited
= NULL
;
398 l
->l_sigstk
.ss_flags
= SS_DISABLE
;
399 l
->l_sigstk
.ss_size
= 0;
400 l
->l_sigstk
.ss_sp
= 0;
401 ksiginfo_queue_init(&l
->l_sigpend
.sp_info
);
402 sigemptyset(&l
->l_sigpend
.sp_set
);
403 mutex_exit(p
->p_lock
);
405 ksiginfo_queue_drain(&kq
);
411 * Free all pending ksiginfo entries from a process on exec.
412 * Additionally, drain any unused ksiginfo structures in the
413 * system back to the pool.
415 * XXX This should not be a hook, every process has signals.
418 ksiginfo_exechook(struct proc
*p
, void *v
)
422 ksiginfo_queue_init(&kq
);
424 mutex_enter(p
->p_lock
);
425 sigclearall(p
, NULL
, &kq
);
426 mutex_exit(p
->p_lock
);
428 ksiginfo_queue_drain(&kq
);
434 * Allocate a new ksiginfo structure from the pool, and optionally copy
435 * an existing one. If the existing ksiginfo_t is from the pool, and
436 * has not been queued somewhere, then just return it. Additionally,
437 * if the existing ksiginfo_t does not contain any information beyond
438 * the signal number, then just return it.
441 ksiginfo_alloc(struct proc
*p
, ksiginfo_t
*ok
, int flags
)
446 if ((ok
->ksi_flags
& (KSI_QUEUED
| KSI_FROMPOOL
)) ==
453 kp
= pool_cache_get(ksiginfo_cache
, flags
);
456 printf("Out of memory allocating ksiginfo for pid %d\n",
463 memcpy(kp
, ok
, sizeof(*kp
));
464 kp
->ksi_flags
&= ~KSI_QUEUED
;
468 kp
->ksi_flags
|= KSI_FROMPOOL
;
476 * If the given ksiginfo_t is from the pool and has not been queued,
480 ksiginfo_free(ksiginfo_t
*kp
)
483 if ((kp
->ksi_flags
& (KSI_QUEUED
| KSI_FROMPOOL
)) != KSI_FROMPOOL
)
485 pool_cache_put(ksiginfo_cache
, kp
);
489 * ksiginfo_queue_drain:
491 * Drain a non-empty ksiginfo_t queue.
494 ksiginfo_queue_drain0(ksiginfoq_t
*kq
)
498 KASSERT(!CIRCLEQ_EMPTY(kq
));
500 while (!CIRCLEQ_EMPTY(kq
)) {
501 ksi
= CIRCLEQ_FIRST(kq
);
502 CIRCLEQ_REMOVE(kq
, ksi
, ksi_list
);
503 pool_cache_put(ksiginfo_cache
, ksi
);
510 * Fetch the first pending signal from a set. Optionally, also fetch
511 * or manufacture a ksiginfo element. Returns the number of the first
512 * pending signal, or zero.
515 sigget(sigpend_t
*sp
, ksiginfo_t
*out
, int signo
, const sigset_t
*mask
)
520 /* If there's no pending set, the signal is from the debugger. */
524 /* Construct mask from signo, and 'mask'. */
528 __sigandset(&sp
->sp_set
, &tset
);
532 /* If there are no signals pending - return. */
533 if ((signo
= firstsig(&tset
)) == 0)
536 KASSERT(sigismember(&sp
->sp_set
, signo
));
539 sigdelset(&sp
->sp_set
, signo
);
541 /* Find siginfo and copy it out. */
542 CIRCLEQ_FOREACH(ksi
, &sp
->sp_info
, ksi_list
) {
543 if (ksi
->ksi_signo
!= signo
)
545 CIRCLEQ_REMOVE(&sp
->sp_info
, ksi
, ksi_list
);
546 KASSERT((ksi
->ksi_flags
& KSI_FROMPOOL
) != 0);
547 KASSERT((ksi
->ksi_flags
& KSI_QUEUED
) != 0);
548 ksi
->ksi_flags
&= ~KSI_QUEUED
;
550 memcpy(out
, ksi
, sizeof(*out
));
551 out
->ksi_flags
&= ~(KSI_FROMPOOL
| KSI_QUEUED
);
553 ksiginfo_free(ksi
); /* XXXSMP */
557 /* If there is no siginfo, then manufacture it. */
560 out
->ksi_info
._signo
= signo
;
561 out
->ksi_info
._code
= SI_NOINFO
;
569 * Append a new ksiginfo element to the list of pending ksiginfo's.
572 sigput(sigpend_t
*sp
, struct proc
*p
, ksiginfo_t
*ksi
)
576 KASSERT(mutex_owned(p
->p_lock
));
577 KASSERT((ksi
->ksi_flags
& KSI_QUEUED
) == 0);
579 sigaddset(&sp
->sp_set
, ksi
->ksi_signo
);
582 * If there is no siginfo, we are done.
584 if (KSI_EMPTY_P(ksi
))
587 KASSERT((ksi
->ksi_flags
& KSI_FROMPOOL
) != 0);
589 #ifdef notyet /* XXX: QUEUING */
590 if (ksi
->ksi_signo
< SIGRTMIN
)
593 CIRCLEQ_FOREACH(kp
, &sp
->sp_info
, ksi_list
) {
594 if (kp
->ksi_signo
== ksi
->ksi_signo
) {
596 kp
->ksi_flags
|= KSI_QUEUED
;
602 ksi
->ksi_flags
|= KSI_QUEUED
;
603 CIRCLEQ_INSERT_TAIL(&sp
->sp_info
, ksi
, ksi_list
);
609 * Clear all pending signals in the specified set.
612 sigclear(sigpend_t
*sp
, const sigset_t
*mask
, ksiginfoq_t
*kq
)
614 ksiginfo_t
*ksi
, *next
;
617 sigemptyset(&sp
->sp_set
);
619 sigminusset(mask
, &sp
->sp_set
);
621 ksi
= CIRCLEQ_FIRST(&sp
->sp_info
);
622 for (; ksi
!= (void *)&sp
->sp_info
; ksi
= next
) {
623 next
= CIRCLEQ_NEXT(ksi
, ksi_list
);
624 if (mask
== NULL
|| sigismember(mask
, ksi
->ksi_signo
)) {
625 CIRCLEQ_REMOVE(&sp
->sp_info
, ksi
, ksi_list
);
626 KASSERT((ksi
->ksi_flags
& KSI_FROMPOOL
) != 0);
627 KASSERT((ksi
->ksi_flags
& KSI_QUEUED
) != 0);
628 CIRCLEQ_INSERT_TAIL(kq
, ksi
, ksi_list
);
636 * Clear all pending signals in the specified set from a process and
640 sigclearall(struct proc
*p
, const sigset_t
*mask
, ksiginfoq_t
*kq
)
644 KASSERT(mutex_owned(p
->p_lock
));
646 sigclear(&p
->p_sigpend
, mask
, kq
);
648 LIST_FOREACH(l
, &p
->p_lwps
, l_sibling
) {
649 sigclear(&l
->l_sigpend
, mask
, kq
);
656 * Return true if there are pending signals for the current LWP. May
657 * be called unlocked provided that LW_PENDSIG is set, and that the
658 * signal has been posted to the appopriate queue before LW_PENDSIG is
662 sigispending(struct lwp
*l
, int signo
)
664 struct proc
*p
= l
->l_proc
;
669 tset
= l
->l_sigpend
.sp_set
;
670 sigplusset(&p
->p_sigpend
.sp_set
, &tset
);
671 sigminusset(&p
->p_sigctx
.ps_sigignore
, &tset
);
672 sigminusset(&l
->l_sigmask
, &tset
);
675 if (firstsig(&tset
) != 0)
677 } else if (sigismember(&tset
, signo
))
688 * Allocate a new siginfo_t structure from the pool.
691 siginfo_alloc(int flags
)
694 return pool_cache_get(siginfo_cache
, flags
);
700 * Return a siginfo_t structure to the pool.
703 siginfo_free(void *arg
)
706 pool_cache_put(siginfo_cache
, arg
);
712 getucontext(struct lwp
*l
, ucontext_t
*ucp
)
714 struct proc
*p
= l
->l_proc
;
716 KASSERT(mutex_owned(p
->p_lock
));
719 ucp
->uc_link
= l
->l_ctxlink
;
723 ucp
->uc_sigmask
= p
->p_sa
->sa_sigmask
;
726 ucp
->uc_sigmask
= l
->l_sigmask
;
727 ucp
->uc_flags
|= _UC_SIGMASK
;
730 * The (unsupplied) definition of the `current execution stack'
731 * in the System V Interface Definition appears to allow returning
732 * the main context stack.
734 if ((l
->l_sigstk
.ss_flags
& SS_ONSTACK
) == 0) {
735 ucp
->uc_stack
.ss_sp
= (void *)l
->l_proc
->p_stackbase
;
736 ucp
->uc_stack
.ss_size
= ctob(l
->l_proc
->p_vmspace
->vm_ssize
);
737 ucp
->uc_stack
.ss_flags
= 0; /* XXX, def. is Very Fishy */
739 /* Simply copy alternate signal execution stack. */
740 ucp
->uc_stack
= l
->l_sigstk
;
742 ucp
->uc_flags
|= _UC_STACK
;
743 mutex_exit(p
->p_lock
);
744 cpu_getmcontext(l
, &ucp
->uc_mcontext
, &ucp
->uc_flags
);
745 mutex_enter(p
->p_lock
);
750 * Get a ucontext_t for use in SA upcall generation.
751 * Teweaked version of getucontext(). We 1) do not take p_lock, 2)
752 * fudge things with uc_link (which is usually NULL for libpthread
753 * code), and 3) we report an empty signal mask.
756 getucontext_sa(struct lwp
*l
, ucontext_t
*ucp
)
759 ucp
->uc_link
= l
->l_ctxlink
;
761 sigemptyset(&ucp
->uc_sigmask
);
762 ucp
->uc_flags
|= _UC_SIGMASK
;
765 * The (unsupplied) definition of the `current execution stack'
766 * in the System V Interface Definition appears to allow returning
767 * the main context stack.
769 if ((l
->l_sigstk
.ss_flags
& SS_ONSTACK
) == 0) {
770 ucp
->uc_stack
.ss_sp
= (void *)l
->l_proc
->p_stackbase
;
771 ucp
->uc_stack
.ss_size
= ctob(l
->l_proc
->p_vmspace
->vm_ssize
);
772 ucp
->uc_stack
.ss_flags
= 0; /* XXX, def. is Very Fishy */
774 /* Simply copy alternate signal execution stack. */
775 ucp
->uc_stack
= l
->l_sigstk
;
777 ucp
->uc_flags
|= _UC_STACK
;
778 cpu_getmcontext(l
, &ucp
->uc_mcontext
, &ucp
->uc_flags
);
782 setucontext(struct lwp
*l
, const ucontext_t
*ucp
)
784 struct proc
*p
= l
->l_proc
;
787 KASSERT(mutex_owned(p
->p_lock
));
789 if ((ucp
->uc_flags
& _UC_SIGMASK
) != 0) {
790 error
= sigprocmask1(l
, SIG_SETMASK
, &ucp
->uc_sigmask
, NULL
);
795 mutex_exit(p
->p_lock
);
796 error
= cpu_setmcontext(l
, &ucp
->uc_mcontext
, ucp
->uc_flags
);
797 mutex_enter(p
->p_lock
);
801 l
->l_ctxlink
= ucp
->uc_link
;
804 * If there was stack information, update whether or not we are
805 * still running on an alternate signal stack.
807 if ((ucp
->uc_flags
& _UC_STACK
) != 0) {
808 if (ucp
->uc_stack
.ss_flags
& SS_ONSTACK
)
809 l
->l_sigstk
.ss_flags
|= SS_ONSTACK
;
811 l
->l_sigstk
.ss_flags
&= ~SS_ONSTACK
;
818 * killpg1: common code for kill process group/broadcast kill.
821 killpg1(struct lwp
*l
, ksiginfo_t
*ksi
, int pgid
, int all
)
827 int signo
= ksi
->ksi_signo
;
833 mutex_enter(proc_lock
);
838 PROCLIST_FOREACH(p
, &allproc
) {
839 if (p
->p_pid
<= 1 || p
== cp
||
840 p
->p_flag
& (PK_SYSTEM
|PK_MARKER
))
842 mutex_enter(p
->p_lock
);
843 if (kauth_authorize_process(pc
,
844 KAUTH_PROCESS_SIGNAL
, p
, KAUTH_ARG(signo
), NULL
,
850 mutex_exit(p
->p_lock
);
854 /* Zero pgid means send to my process group. */
857 pgrp
= pg_find(pgid
, PFIND_LOCKED
);
861 LIST_FOREACH(p
, &pgrp
->pg_members
, p_pglist
) {
862 if (p
->p_pid
<= 1 || p
->p_flag
& PK_SYSTEM
)
864 mutex_enter(p
->p_lock
);
865 if (kauth_authorize_process(pc
, KAUTH_PROCESS_SIGNAL
,
866 p
, KAUTH_ARG(signo
), NULL
, NULL
) == 0) {
868 if (signo
&& P_ZOMBIE(p
) == 0)
871 mutex_exit(p
->p_lock
);
875 mutex_exit(proc_lock
);
876 return nfound
? 0 : ESRCH
;
880 * Send a signal to a process group. If checktty is set, limit to members
881 * which have a controlling terminal.
884 pgsignal(struct pgrp
*pgrp
, int sig
, int checkctty
)
888 KASSERT(!cpu_intr_p());
889 KASSERT(mutex_owned(proc_lock
));
891 KSI_INIT_EMPTY(&ksi
);
893 kpgsignal(pgrp
, &ksi
, NULL
, checkctty
);
897 kpgsignal(struct pgrp
*pgrp
, ksiginfo_t
*ksi
, void *data
, int checkctty
)
901 KASSERT(!cpu_intr_p());
902 KASSERT(mutex_owned(proc_lock
));
903 KASSERT(pgrp
!= NULL
);
905 LIST_FOREACH(p
, &pgrp
->pg_members
, p_pglist
)
906 if (checkctty
== 0 || p
->p_lflag
& PL_CONTROLT
)
907 kpsignal(p
, ksi
, data
);
911 * Send a signal caused by a trap to the current LWP. If it will be caught
912 * immediately, deliver it with correct code. Otherwise, post it normally.
915 trapsignal(struct lwp
*l
, ksiginfo_t
*ksi
)
919 int signo
= ksi
->ksi_signo
;
922 KASSERT(KSI_TRAP_P(ksi
));
924 ksi
->ksi_lid
= l
->l_lid
;
927 KASSERT(!cpu_intr_p());
928 mutex_enter(proc_lock
);
929 mutex_enter(p
->p_lock
);
930 mask
= (p
->p_sa
!= NULL
) ? &p
->p_sa
->sa_sigmask
: &l
->l_sigmask
;
932 if ((p
->p_slflag
& PSL_TRACED
) == 0 &&
933 sigismember(&p
->p_sigctx
.ps_sigcatch
, signo
) &&
934 !sigismember(mask
, signo
)) {
935 mutex_exit(proc_lock
);
936 l
->l_ru
.ru_nsignals
++;
937 kpsendsig(l
, ksi
, mask
);
938 mutex_exit(p
->p_lock
);
939 ktrpsig(signo
, SIGACTION_PS(ps
, signo
).sa_handler
, mask
, ksi
);
941 /* XXX for core dump/debugger */
942 p
->p_sigctx
.ps_lwp
= l
->l_lid
;
943 p
->p_sigctx
.ps_signo
= ksi
->ksi_signo
;
944 p
->p_sigctx
.ps_code
= ksi
->ksi_trap
;
946 mutex_exit(p
->p_lock
);
947 mutex_exit(proc_lock
);
952 * Fill in signal information and signal the parent for a child status change.
955 child_psignal(struct proc
*p
, int mask
)
961 KASSERT(mutex_owned(proc_lock
));
962 KASSERT(mutex_owned(p
->p_lock
));
967 ksi
.ksi_signo
= SIGCHLD
;
968 ksi
.ksi_code
= (xstat
== SIGCONT
? CLD_CONTINUED
: CLD_STOPPED
);
969 ksi
.ksi_pid
= p
->p_pid
;
970 ksi
.ksi_uid
= kauth_cred_geteuid(p
->p_cred
);
971 ksi
.ksi_status
= xstat
;
972 ksi
.ksi_utime
= p
->p_stats
->p_ru
.ru_utime
.tv_sec
;
973 ksi
.ksi_stime
= p
->p_stats
->p_ru
.ru_stime
.tv_sec
;
977 mutex_exit(p
->p_lock
);
978 mutex_enter(q
->p_lock
);
980 if ((q
->p_sflag
& mask
) == 0)
983 mutex_exit(q
->p_lock
);
984 mutex_enter(p
->p_lock
);
988 psignal(struct proc
*p
, int signo
)
992 KASSERT(!cpu_intr_p());
993 KASSERT(mutex_owned(proc_lock
));
995 KSI_INIT_EMPTY(&ksi
);
996 ksi
.ksi_signo
= signo
;
997 mutex_enter(p
->p_lock
);
999 mutex_exit(p
->p_lock
);
1003 kpsignal(struct proc
*p
, ksiginfo_t
*ksi
, void *data
)
1009 KASSERT(!cpu_intr_p());
1010 KASSERT(mutex_owned(proc_lock
));
1012 if ((p
->p_sflag
& PS_WEXIT
) == 0 && data
) {
1014 filedesc_t
*fdp
= p
->p_fd
;
1016 /* XXXSMP locking */
1019 for (fd
= 0; fd
< dt
->dt_nfiles
; fd
++) {
1020 if ((ff
= dt
->dt_ff
[fd
]) == NULL
)
1022 if ((fp
= ff
->ff_file
) == NULL
)
1024 if (fp
->f_data
== data
) {
1030 mutex_enter(p
->p_lock
);
1032 mutex_exit(p
->p_lock
);
1038 * Returns true if signal is ignored or masked for the specified LWP.
1041 sigismasked(struct lwp
*l
, int sig
)
1043 struct proc
*p
= l
->l_proc
;
1045 return (sigismember(&p
->p_sigctx
.ps_sigignore
, sig
) ||
1046 sigismember(&l
->l_sigmask
, sig
)
1048 || ((p
->p_sa
!= NULL
) && sigismember(&p
->p_sa
->sa_sigmask
, sig
))
1049 #endif /* KERN_SA */
1056 * Post a pending signal to an LWP. Returns non-zero if the LWP may
1057 * be able to take the signal.
1060 sigpost(struct lwp
*l
, sig_t action
, int prop
, int sig
, int idlecheck
)
1063 struct proc
*p
= l
->l_proc
;
1065 KASSERT(mutex_owned(p
->p_lock
));
1068 * If the LWP is on the way out, sigclear() will be busy draining all
1069 * pending signals. Don't give it more.
1071 if (l
->l_refcnt
== 0)
1075 * Have the LWP check for signals. This ensures that even if no LWP
1076 * is found to take the signal immediately, it should be taken soon.
1079 l
->l_flag
|= LW_PENDSIG
;
1082 * When sending signals to SA processes, we first try to find an
1083 * idle VP to take it.
1085 if (idlecheck
&& (l
->l_flag
& (LW_SA_IDLE
| LW_SA_YIELD
)) == 0) {
1091 * SIGCONT can be masked, but if LWP is stopped, it needs restart.
1092 * Note: SIGKILL and SIGSTOP cannot be masked.
1095 if (p
->p_sa
!= NULL
)
1096 masked
= sigismember(&p
->p_sa
->sa_sigmask
, sig
);
1099 masked
= sigismember(&l
->l_sigmask
, sig
);
1100 if (masked
&& ((prop
& SA_CONT
) == 0 || l
->l_stat
!= LSSTOP
)) {
1106 * If killing the process, make it run fast.
1108 if (__predict_false((prop
& SA_KILL
) != 0) &&
1109 action
== SIG_DFL
&& l
->l_priority
< MAXPRI_USER
) {
1110 KASSERT(l
->l_class
== SCHED_OTHER
);
1111 lwp_changepri(l
, MAXPRI_USER
);
1115 * If the LWP is running or on a run queue, then we win. If it's
1116 * sleeping interruptably, wake it and make it take the signal. If
1117 * the sleep isn't interruptable, then the chances are it will get
1118 * to see the signal soon anyhow. If suspended, it can't take the
1119 * signal right now. If it's LWP private or for all LWPs, save it
1120 * for later; otherwise punt.
1124 switch (l
->l_stat
) {
1127 lwp_need_userret(l
);
1132 if ((l
->l_flag
& LW_SINTR
) != 0) {
1133 /* setrunnable() will release the lock. */
1140 if ((prop
& SA_KILL
) != 0) {
1141 /* lwp_continue() will release the lock. */
1148 if ((prop
& SA_STOP
) != 0)
1152 * If the LWP is stopped and we are sending a continue
1153 * signal, then start it again.
1155 if ((prop
& SA_CONT
) != 0) {
1156 if (l
->l_wchan
!= NULL
) {
1157 l
->l_stat
= LSSLEEP
;
1162 /* setrunnable() will release the lock. */
1165 } else if (l
->l_wchan
== NULL
|| (l
->l_flag
& LW_SINTR
) != 0) {
1166 /* setrunnable() will release the lock. */
1181 * Notify an LWP that it has a pending signal.
1184 signotify(struct lwp
*l
)
1186 KASSERT(lwp_locked(l
, NULL
));
1188 l
->l_flag
|= LW_PENDSIG
;
1189 lwp_need_userret(l
);
1193 * Find an LWP within process p that is waiting on signal ksi, and hand
1197 sigunwait(struct proc
*p
, const ksiginfo_t
*ksi
)
1202 KASSERT(mutex_owned(p
->p_lock
));
1204 signo
= ksi
->ksi_signo
;
1206 if (ksi
->ksi_lid
!= 0) {
1208 * Signal came via _lwp_kill(). Find the LWP and see if
1211 if ((l
= lwp_find(p
, ksi
->ksi_lid
)) == NULL
)
1213 if (l
->l_sigwaited
== NULL
||
1214 !sigismember(&l
->l_sigwaitset
, signo
))
1218 * Look for any LWP that may be interested.
1220 LIST_FOREACH(l
, &p
->p_sigwaiters
, l_sigwaiter
) {
1221 KASSERT(l
->l_sigwaited
!= NULL
);
1222 if (sigismember(&l
->l_sigwaitset
, signo
))
1228 l
->l_sigwaited
->ksi_info
= ksi
->ksi_info
;
1229 l
->l_sigwaited
= NULL
;
1230 LIST_REMOVE(l
, l_sigwaiter
);
1231 cv_signal(&l
->l_sigcv
);
1239 * Send the signal to the process. If the signal has an action, the action
1240 * is usually performed by the target process rather than the caller; we add
1241 * the signal to the set of pending signals for the process.
1244 * o When a stop signal is sent to a sleeping process that takes the
1245 * default action, the process is stopped without awakening it.
1246 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1247 * regardless of the signal action (eg, blocked or ignored).
1249 * Other ignored signals are discarded immediately.
1252 kpsignal2(struct proc
*p
, ksiginfo_t
*ksi
)
1254 int prop
, signo
= ksi
->ksi_signo
;
1262 KASSERT(!cpu_intr_p());
1263 KASSERT(mutex_owned(proc_lock
));
1264 KASSERT(mutex_owned(p
->p_lock
));
1265 KASSERT((ksi
->ksi_flags
& KSI_QUEUED
) == 0);
1266 KASSERT(signo
> 0 && signo
< NSIG
);
1269 * If the process is being created by fork, is a zombie or is
1270 * exiting, then just drop the signal here and bail out.
1272 if (p
->p_stat
!= SACTIVE
&& p
->p_stat
!= SSTOP
)
1276 * Notify any interested parties of the signal.
1278 KNOTE(&p
->p_klist
, NOTE_SIGNAL
| signo
);
1281 * Some signals including SIGKILL must act on the entire process.
1284 prop
= sigprop
[signo
];
1285 toall
= ((prop
& SA_TOALL
) != 0);
1286 lid
= toall
? 0 : ksi
->ksi_lid
;
1289 * If proc is traced, always give parent a chance.
1291 if (p
->p_slflag
& PSL_TRACED
) {
1296 * If the process is being traced and the signal
1297 * is being caught, make sure to save any ksiginfo.
1299 if ((kp
= ksiginfo_alloc(p
, ksi
, PR_NOWAIT
)) == NULL
)
1301 sigput(&p
->p_sigpend
, p
, kp
);
1305 * If the signal was the result of a trap and is not being
1306 * caught, then reset it to default action so that the
1307 * process dumps core immediately.
1309 if (KSI_TRAP_P(ksi
)) {
1311 mutex_enter(&sa
->sa_mutex
);
1312 if (!sigismember(&p
->p_sigctx
.ps_sigcatch
, signo
)) {
1313 sigdelset(&p
->p_sigctx
.ps_sigignore
, signo
);
1314 SIGACTION(p
, signo
).sa_handler
= SIG_DFL
;
1316 mutex_exit(&sa
->sa_mutex
);
1320 * If the signal is being ignored, then drop it. Note: we
1321 * don't set SIGCONT in ps_sigignore, and if it is set to
1322 * SIG_IGN, action will be SIG_DFL here.
1324 if (sigismember(&p
->p_sigctx
.ps_sigignore
, signo
))
1327 else if (sigismember(&p
->p_sigctx
.ps_sigcatch
, signo
))
1333 * If sending a tty stop signal to a member of an
1334 * orphaned process group, discard the signal here if
1335 * the action is default; don't stop the process below
1336 * if sleeping, and don't clear any pending SIGCONT.
1338 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0)
1341 if (prop
& SA_KILL
&& p
->p_nice
> NZERO
)
1347 * If stopping or continuing a process, discard any pending
1348 * signals that would do the inverse.
1350 if ((prop
& (SA_CONT
| SA_STOP
)) != 0) {
1353 ksiginfo_queue_init(&kq
);
1354 if ((prop
& SA_CONT
) != 0)
1355 sigclear(&p
->p_sigpend
, &stopsigmask
, &kq
);
1356 if ((prop
& SA_STOP
) != 0)
1357 sigclear(&p
->p_sigpend
, &contsigmask
, &kq
);
1358 ksiginfo_queue_drain(&kq
); /* XXXSMP */
1362 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
1363 * please!), check if any LWPs are waiting on it. If yes, pass on
1364 * the signal info. The signal won't be processed further here.
1366 if ((prop
& SA_CANTMASK
) == 0 && !LIST_EMPTY(&p
->p_sigwaiters
) &&
1367 p
->p_stat
== SACTIVE
&& (p
->p_sflag
& PS_STOPPING
) == 0 &&
1372 * XXXSMP Should be allocated by the caller, we're holding locks
1375 if (kp
== NULL
&& (kp
= ksiginfo_alloc(p
, ksi
, PR_NOWAIT
)) == NULL
)
1379 * LWP private signals are easy - just find the LWP and post
1383 l
= lwp_find(p
, lid
);
1385 sigput(&l
->l_sigpend
, p
, kp
);
1387 (void)sigpost(l
, action
, prop
, kp
->ksi_signo
, 0);
1393 * Some signals go to all LWPs, even if posted with _lwp_kill()
1394 * or for an SA process.
1396 if (p
->p_stat
== SACTIVE
&& (p
->p_sflag
& PS_STOPPING
) == 0) {
1397 if ((p
->p_slflag
& PSL_TRACED
) != 0)
1401 * If SIGCONT is default (or ignored) and process is
1402 * asleep, we are finished; the process should not
1405 if ((prop
& SA_CONT
) != 0 && action
== SIG_DFL
)
1409 * Process is stopped or stopping.
1410 * - If traced, then no action is needed, unless killing.
1411 * - Run the process only if sending SIGCONT or SIGKILL.
1413 if ((p
->p_slflag
& PSL_TRACED
) != 0 && signo
!= SIGKILL
) {
1416 if ((prop
& SA_CONT
) != 0 || signo
== SIGKILL
) {
1418 * Re-adjust p_nstopchild if the process wasn't
1419 * collected by its parent.
1421 p
->p_stat
= SACTIVE
;
1422 p
->p_sflag
&= ~PS_STOPPING
;
1424 p
->p_pptr
->p_nstopchild
--;
1426 if (p
->p_slflag
& PSL_TRACED
) {
1427 KASSERT(signo
== SIGKILL
);
1431 * Do not make signal pending if SIGCONT is default.
1433 * If the process catches SIGCONT, let it handle the
1434 * signal itself (if waiting on event - process runs,
1435 * otherwise continues sleeping).
1437 if ((prop
& SA_CONT
) != 0 && action
== SIG_DFL
) {
1438 KASSERT(signo
!= SIGKILL
);
1441 } else if ((prop
& SA_STOP
) != 0) {
1443 * Already stopped, don't need to stop again.
1444 * (If we did the shell could get confused.)
1450 * Make signal pending.
1452 KASSERT((p
->p_slflag
& PSL_TRACED
) == 0);
1453 sigput(&p
->p_sigpend
, p
, kp
);
1457 * Before we set LW_PENDSIG on any LWP, ensure that the signal is
1458 * visible on the per process list (for sigispending()). This
1459 * is unlikely to be needed in practice, but...
1464 * Try to find an LWP that can take the signal.
1467 if ((p
->p_sa
!= NULL
) && !toall
) {
1468 struct sadata_vp
*vp
;
1470 * If we're in this delivery path, we are delivering a
1471 * signal that needs to go to one thread in the process.
1473 * In the SA case, we try to find an idle LWP that can take
1474 * the signal. If that fails, only then do we consider
1475 * interrupting active LWPs. Since the signal's going to
1476 * just one thread, we need only look at "blessed" lwps,
1477 * so scan the vps for them.
1480 SLIST_FOREACH(vp
, &p
->p_sa
->sa_vps
, savp_next
) {
1482 if (sigpost(l
, action
, prop
, kp
->ksi_signo
, 1))
1487 SLIST_FOREACH(vp
, &p
->p_sa
->sa_vps
, savp_next
) {
1489 if (sigpost(l
, action
, prop
, kp
->ksi_signo
, 0))
1493 /* Delivered, skip next. */
1497 LIST_FOREACH(l
, &p
->p_lwps
, l_sibling
) {
1498 if (sigpost(l
, action
, prop
, kp
->ksi_signo
, 0) && !toall
)
1503 * If the ksiginfo wasn't used, then bin it. XXXSMP freeing memory
1504 * with locks held. The caller should take care of this.
1510 kpsendsig(struct lwp
*l
, const ksiginfo_t
*ksi
, const sigset_t
*mask
)
1512 struct proc
*p
= l
->l_proc
;
1514 struct lwp
*le
, *li
;
1517 #endif /* KERN_SA */
1519 KASSERT(mutex_owned(p
->p_lock
));
1522 if (p
->p_sflag
& PS_SA
) {
1523 /* f indicates if we should clear LP_SA_NOBLOCK */
1524 f
= ~l
->l_pflag
& LP_SA_NOBLOCK
;
1525 l
->l_pflag
|= LP_SA_NOBLOCK
;
1527 mutex_exit(p
->p_lock
);
1528 /* XXXUPSXXX What if not on sa_vp? */
1530 * WRS: I think it won't matter, beyond the
1531 * question of what exactly we do with a signal
1532 * to a blocked user thread. Also, we try hard to always
1533 * send signals to blessed lwps, so we would only send
1534 * to a non-blessed lwp under special circumstances.
1536 si
= siginfo_alloc(PR_WAITOK
);
1538 si
->_info
= ksi
->ksi_info
;
1541 * Figure out if we're the innocent victim or the main
1545 if (KSI_TRAP_P(ksi
))
1549 if (sa_upcall(l
, SA_UPCALL_SIGNAL
| SA_UPCALL_DEFER
, le
, li
,
1550 sizeof(*si
), si
, siginfo_free
) != 0) {
1553 if (KSI_TRAP_P(ksi
))
1554 /* XXX What dowe do here? The signal
1560 mutex_enter(p
->p_lock
);
1563 #endif /* KERN_SA */
1565 (*p
->p_emul
->e_sendsig
)(ksi
, mask
);
1569 * Stop any LWPs sleeping interruptably.
1572 proc_stop_lwps(struct proc
*p
)
1576 KASSERT(mutex_owned(p
->p_lock
));
1577 KASSERT((p
->p_sflag
& PS_STOPPING
) != 0);
1579 LIST_FOREACH(l
, &p
->p_lwps
, l_sibling
) {
1581 if (l
->l_stat
== LSSLEEP
&& (l
->l_flag
& LW_SINTR
) != 0) {
1590 * Finish stopping of a process. Mark it stopped and notify the parent.
1592 * Drop p_lock briefly if PS_NOTIFYSTOP is set and ppsig is true.
1595 proc_stop_done(struct proc
*p
, bool ppsig
, int ppmask
)
1598 KASSERT(mutex_owned(proc_lock
));
1599 KASSERT(mutex_owned(p
->p_lock
));
1600 KASSERT((p
->p_sflag
& PS_STOPPING
) != 0);
1601 KASSERT(p
->p_nrlwps
== 0 || (p
->p_nrlwps
== 1 && p
== curproc
));
1603 p
->p_sflag
&= ~PS_STOPPING
;
1606 p
->p_pptr
->p_nstopchild
++;
1607 if ((p
->p_sflag
& PS_NOTIFYSTOP
) != 0) {
1609 /* child_psignal drops p_lock briefly. */
1610 child_psignal(p
, ppmask
);
1612 cv_broadcast(&p
->p_pptr
->p_waitcv
);
1617 * Stop the current process and switch away when being stopped or traced.
1620 sigswitch(bool ppsig
, int ppmask
, int signo
)
1622 struct lwp
*l
= curlwp
;
1623 struct proc
*p
= l
->l_proc
;
1626 KASSERT(mutex_owned(p
->p_lock
));
1627 KASSERT(l
->l_stat
== LSONPROC
);
1628 KASSERT(p
->p_nrlwps
> 0);
1631 * On entry we know that the process needs to stop. If it's
1632 * the result of a 'sideways' stop signal that has been sourced
1633 * through issignal(), then stop other LWPs in the process too.
1635 if (p
->p_stat
== SACTIVE
&& (p
->p_sflag
& PS_STOPPING
) == 0) {
1636 KASSERT(signo
!= 0);
1637 proc_stop(p
, 1, signo
);
1638 KASSERT(p
->p_nrlwps
> 0);
1642 * If we are the last live LWP, and the stop was a result of
1643 * a new signal, then signal the parent.
1645 if ((p
->p_sflag
& PS_STOPPING
) != 0) {
1646 if (!mutex_tryenter(proc_lock
)) {
1647 mutex_exit(p
->p_lock
);
1648 mutex_enter(proc_lock
);
1649 mutex_enter(p
->p_lock
);
1652 if (p
->p_nrlwps
== 1 && (p
->p_sflag
& PS_STOPPING
) != 0) {
1654 * Note that proc_stop_done() can drop
1655 * p->p_lock briefly.
1657 proc_stop_done(p
, ppsig
, ppmask
);
1660 mutex_exit(proc_lock
);
1664 * Unlock and switch away.
1666 KERNEL_UNLOCK_ALL(l
, &biglocks
);
1667 if (p
->p_stat
== SSTOP
|| (p
->p_sflag
& PS_STOPPING
) != 0) {
1670 KASSERT(l
->l_stat
== LSONPROC
|| l
->l_stat
== LSSLEEP
);
1675 mutex_exit(p
->p_lock
);
1678 KERNEL_LOCK(biglocks
, l
);
1679 mutex_enter(p
->p_lock
);
1683 * Check for a signal from the debugger.
1688 struct lwp
*l
= curlwp
;
1689 struct proc
*p
= l
->l_proc
;
1693 KASSERT(mutex_owned(p
->p_lock
));
1695 /* If there's a pending SIGKILL, process it immediately. */
1696 if (sigismember(&p
->p_sigpend
.sp_set
, SIGKILL
))
1700 * If we are no longer being traced, or the parent didn't
1701 * give us a signal, look for more signals.
1703 if ((p
->p_slflag
& PSL_TRACED
) == 0 || p
->p_xstat
== 0)
1707 * If the new signal is being masked, look for other signals.
1708 * `p->p_sigctx.ps_siglist |= mask' is done in setrunnable().
1712 mask
= (p
->p_sa
!= NULL
) ? &p
->p_sa
->sa_sigmask
: &l
->l_sigmask
;
1713 if (sigismember(mask
, signo
))
1720 * If the current process has received a signal (should be caught or cause
1721 * termination, should interrupt current syscall), return the signal number.
1723 * Stop signals with default action are processed immediately, then cleared;
1724 * they aren't returned. This is checked after each entry to the system for
1725 * a syscall or trap.
1727 * We will also return -1 if the process is exiting and the current LWP must
1731 issignal(struct lwp
*l
)
1742 KASSERT(p
== curproc
);
1743 KASSERT(mutex_owned(p
->p_lock
));
1746 /* Discard any signals that we have decided not to take. */
1748 (void)sigget(sp
, NULL
, signo
, NULL
);
1750 /* Bail out if we do not own the virtual processor */
1751 if (l
->l_flag
& LW_SA
&& l
->l_savp
->savp_lwp
!= l
)
1755 * If the process is stopped/stopping, then stop ourselves
1756 * now that we're on the kernel/userspace boundary. When
1757 * we awaken, check for a signal from the debugger.
1759 if (p
->p_stat
== SSTOP
|| (p
->p_sflag
& PS_STOPPING
) != 0) {
1760 sigswitch(true, PS_NOCLDSTOP
, 0);
1761 signo
= sigchecktrace();
1765 /* Signals from the debugger are "out of band". */
1769 * If the debugger didn't provide a signal, find a pending
1770 * signal from our set. Check per-LWP signals first, and
1776 if ((p
->p_lflag
& PL_PPWAIT
) != 0)
1777 sigminusset(&stopsigmask
, &ss
);
1778 sigminusset(&l
->l_sigmask
, &ss
);
1780 if ((signo
= firstsig(&ss
)) == 0) {
1783 if ((p
->p_lflag
& PL_PPWAIT
) != 0)
1784 sigminusset(&stopsigmask
, &ss
);
1785 sigminusset(&l
->l_sigmask
, &ss
);
1787 if ((signo
= firstsig(&ss
)) == 0) {
1789 * No signal pending - clear the
1790 * indicator and bail out.
1793 l
->l_flag
&= ~LW_PENDSIG
;
1802 * We should see pending but ignored signals only if
1803 * we are being traced.
1805 if (sigismember(&p
->p_sigctx
.ps_sigignore
, signo
) &&
1806 (p
->p_slflag
& PSL_TRACED
) == 0) {
1807 /* Discard the signal. */
1812 * If traced, always stop, and stay stopped until released
1813 * by the debugger. If the our parent process is waiting
1814 * for us, don't hang as we could deadlock.
1816 if ((p
->p_slflag
& PSL_TRACED
) != 0 &&
1817 (p
->p_lflag
& PL_PPWAIT
) == 0 && signo
!= SIGKILL
) {
1818 /* Take the signal. */
1819 (void)sigget(sp
, NULL
, signo
, NULL
);
1822 /* Emulation-specific handling of signal trace */
1823 if (p
->p_emul
->e_tracesig
== NULL
||
1824 (*p
->p_emul
->e_tracesig
)(p
, signo
) == 0)
1825 sigswitch(!(p
->p_slflag
& PSL_FSTRACE
), 0,
1828 /* Check for a signal from the debugger. */
1829 if ((signo
= sigchecktrace()) == 0)
1832 /* Signals from the debugger are "out of band". */
1836 prop
= sigprop
[signo
];
1839 * Decide whether the signal should be returned.
1841 switch ((long)SIGACTION(p
, signo
).sa_handler
) {
1844 * Don't take default actions on system processes.
1846 if (p
->p_pid
<= 1) {
1849 * Are you sure you want to ignore SIGSEGV
1852 printf_nolog("Process (pid %d) got sig %d\n",
1859 * If there is a pending stop signal to process with
1860 * default action, stop here, then clear the signal.
1861 * However, if process is member of an orphaned
1862 * process group, ignore tty stop signals.
1864 if (prop
& SA_STOP
) {
1866 * XXX Don't hold proc_lock for p_lflag,
1867 * but it's not a big deal.
1869 if (p
->p_slflag
& PSL_TRACED
||
1870 ((p
->p_lflag
& PL_ORPHANPG
) != 0 &&
1871 prop
& SA_TTYSTOP
)) {
1872 /* Ignore the signal. */
1875 /* Take the signal. */
1876 (void)sigget(sp
, NULL
, signo
, NULL
);
1879 sigswitch(true, PS_NOCLDSTOP
, p
->p_xstat
);
1880 } else if (prop
& SA_IGNORE
) {
1882 * Except for SIGCONT, shouldn't get here.
1883 * Default action is to ignore; drop it.
1890 #ifdef DEBUG_ISSIGNAL
1892 * Masking above should prevent us ever trying
1893 * to take action on an ignored signal other
1894 * than SIGCONT, unless process is traced.
1896 if ((prop
& SA_CONT
) == 0 &&
1897 (p
->p_slflag
& PSL_TRACED
) == 0)
1898 printf_nolog("issignal\n");
1904 * This signal has an action, let postsig() process
1913 l
->l_sigpendset
= sp
;
1918 * Take the action for the specified signal
1919 * from the current set of pending signals.
1928 sigset_t
*returnmask
;
1935 KASSERT(mutex_owned(p
->p_lock
));
1939 * Set the new mask value and also defer further occurrences of this
1942 * Special case: user has done a sigsuspend. Here the current mask is
1943 * not of interest, but rather the mask from before the sigsuspend is
1944 * what we want restored after the signal processing is completed.
1946 if (l
->l_sigrestore
) {
1947 returnmask
= &l
->l_sigoldmask
;
1948 l
->l_sigrestore
= 0;
1950 returnmask
= &l
->l_sigmask
;
1953 * Commit to taking the signal before releasing the mutex.
1955 action
= SIGACTION_PS(ps
, signo
).sa_handler
;
1956 l
->l_ru
.ru_nsignals
++;
1957 sigget(l
->l_sigpendset
, &ksi
, signo
, NULL
);
1959 if (ktrpoint(KTR_PSIG
)) {
1960 mutex_exit(p
->p_lock
);
1961 ktrpsig(signo
, action
, returnmask
, &ksi
);
1962 mutex_enter(p
->p_lock
);
1965 if (action
== SIG_DFL
) {
1967 * Default action, where the default is to kill
1968 * the process. (Other cases were ignored above.)
1975 * If we get here, the signal must be caught.
1978 if (action
== SIG_IGN
|| sigismember(&l
->l_sigmask
, signo
))
1979 panic("postsig action");
1982 kpsendsig(l
, &ksi
, returnmask
);
1988 * Default signal delivery method for NetBSD.
1991 sendsig(const struct ksiginfo
*ksi
, const sigset_t
*mask
)
1996 sig
= ksi
->ksi_signo
;
1997 sa
= curproc
->p_sigacts
;
1999 switch (sa
->sa_sigdesc
[sig
].sd_vers
) {
2002 /* Compat for 1.6 and earlier. */
2003 if (sendsig_sigcontext_vec
== NULL
) {
2006 (*sendsig_sigcontext_vec
)(ksi
, mask
);
2010 sendsig_siginfo(ksi
, mask
);
2016 printf("sendsig: bad version %d\n", sa
->sa_sigdesc
[sig
].sd_vers
);
2017 sigexit(curlwp
, SIGILL
);
2023 * Reset the signal action. Called from emulation specific sendsig()
2024 * before unlocking to deliver the signal.
2027 sendsig_reset(struct lwp
*l
, int signo
)
2029 struct proc
*p
= l
->l_proc
;
2030 struct sigacts
*ps
= p
->p_sigacts
;
2033 KASSERT(mutex_owned(p
->p_lock
));
2035 p
->p_sigctx
.ps_lwp
= 0;
2036 p
->p_sigctx
.ps_code
= 0;
2037 p
->p_sigctx
.ps_signo
= 0;
2039 mask
= (p
->p_sa
!= NULL
) ? &p
->p_sa
->sa_sigmask
: &l
->l_sigmask
;
2041 mutex_enter(&ps
->sa_mutex
);
2042 sigplusset(&SIGACTION_PS(ps
, signo
).sa_mask
, mask
);
2043 if (SIGACTION_PS(ps
, signo
).sa_flags
& SA_RESETHAND
) {
2044 sigdelset(&p
->p_sigctx
.ps_sigcatch
, signo
);
2045 if (signo
!= SIGCONT
&& sigprop
[signo
] & SA_IGNORE
)
2046 sigaddset(&p
->p_sigctx
.ps_sigignore
, signo
);
2047 SIGACTION_PS(ps
, signo
).sa_handler
= SIG_DFL
;
2049 mutex_exit(&ps
->sa_mutex
);
2053 * Kill the current process for stated reason.
2056 killproc(struct proc
*p
, const char *why
)
2059 KASSERT(mutex_owned(proc_lock
));
2061 log(LOG_ERR
, "pid %d was killed: %s\n", p
->p_pid
, why
);
2062 uprintf_locked("sorry, pid %d was killed: %s\n", p
->p_pid
, why
);
2063 psignal(p
, SIGKILL
);
2067 * Force the current process to exit with the specified signal, dumping core
2068 * if appropriate. We bypass the normal tests for masked and caught
2069 * signals, allowing unrecoverable failures to terminate the process without
2070 * changing signal state. Mark the accounting record with the signal
2071 * termination. If dumping core, save the signal number for the debugger.
2072 * Calls exit and does not return.
2075 sigexit(struct lwp
*l
, int signo
)
2077 int exitsig
, error
, docore
;
2083 KASSERT(mutex_owned(p
->p_lock
));
2084 KERNEL_UNLOCK_ALL(l
, NULL
);
2087 * Don't permit coredump() multiple times in the same process.
2088 * Call back into sigexit, where we will be suspended until
2089 * the deed is done. Note that this is a recursive call, but
2090 * LW_WCORE will prevent us from coming back this way.
2092 if ((p
->p_sflag
& PS_WCORE
) != 0) {
2094 l
->l_flag
|= (LW_WCORE
| LW_WEXIT
| LW_WSUSPEND
);
2096 mutex_exit(p
->p_lock
);
2102 /* If process is already on the way out, then bail now. */
2103 if ((p
->p_sflag
& PS_WEXIT
) != 0) {
2104 mutex_exit(p
->p_lock
);
2111 * Prepare all other LWPs for exit. If dumping core, suspend them
2112 * so that their registers are available long enough to be dumped.
2114 if ((docore
= (sigprop
[signo
] & SA_CORE
)) != 0) {
2115 p
->p_sflag
|= PS_WCORE
;
2117 LIST_FOREACH(t
, &p
->p_lwps
, l_sibling
) {
2120 t
->l_flag
&= ~LW_WSUSPEND
;
2124 t
->l_flag
|= (LW_WCORE
| LW_WEXIT
);
2128 if (p
->p_nrlwps
== 1)
2132 * Kick any LWPs sitting in lwp_wait1(), and wait
2133 * for everyone else to stop before proceeding.
2136 cv_broadcast(&p
->p_lwpcv
);
2137 cv_wait(&p
->p_lwpcv
, p
->p_lock
);
2143 p
->p_acflag
|= AXSIG
;
2144 p
->p_sigctx
.ps_signo
= signo
;
2147 mutex_exit(p
->p_lock
);
2148 if ((error
= (*coredump_vec
)(l
, NULL
)) == 0)
2149 exitsig
|= WCOREFLAG
;
2151 if (kern_logsigexit
) {
2152 int uid
= l
->l_cred
?
2153 (int)kauth_cred_geteuid(l
->l_cred
) : -1;
2156 log(LOG_INFO
, lognocoredump
, p
->p_pid
,
2157 p
->p_comm
, uid
, signo
, error
);
2159 log(LOG_INFO
, logcoredump
, p
->p_pid
,
2160 p
->p_comm
, uid
, signo
);
2163 #ifdef PAX_SEGVGUARD
2164 pax_segvguard(l
, p
->p_textvp
, p
->p_comm
, true);
2165 #endif /* PAX_SEGVGUARD */
2166 /* Acquire the sched state mutex. exit1() will release it. */
2167 mutex_enter(p
->p_lock
);
2170 /* No longer dumping core. */
2171 p
->p_sflag
&= ~PS_WCORE
;
2173 exit1(l
, W_EXITCODE(0, exitsig
));
2178 * Put process 'p' into the stopped state and optionally, notify the parent.
2181 proc_stop(struct proc
*p
, int notify
, int signo
)
2185 KASSERT(mutex_owned(p
->p_lock
));
2188 * First off, set the stopping indicator and bring all sleeping
2189 * LWPs to a halt so they are included in p->p_nrlwps. We musn't
2190 * unlock between here and the p->p_nrlwps check below.
2192 p
->p_sflag
|= PS_STOPPING
;
2194 p
->p_sflag
|= PS_NOTIFYSTOP
;
2196 p
->p_sflag
&= ~PS_NOTIFYSTOP
;
2202 * If there are no LWPs available to take the signal, then we
2203 * signal the parent process immediately. Otherwise, the last
2204 * LWP to stop will take care of it.
2207 if (p
->p_nrlwps
== 0) {
2208 proc_stop_done(p
, true, PS_NOCLDSTOP
);
2211 * Have the remaining LWPs come to a halt, and trigger
2212 * proc_stop_callout() to ensure that they do.
2214 LIST_FOREACH(l
, &p
->p_lwps
, l_sibling
)
2215 sigpost(l
, SIG_DFL
, SA_STOP
, signo
, 0);
2216 callout_schedule(&proc_stop_ch
, 1);
2221 * When stopping a process, we do not immediatly set sleeping LWPs stopped,
2222 * but wait for them to come to a halt at the kernel-user boundary. This is
2223 * to allow LWPs to release any locks that they may hold before stopping.
2225 * Non-interruptable sleeps can be long, and there is the potential for an
2226 * LWP to begin sleeping interruptably soon after the process has been set
2227 * stopping (PS_STOPPING). These LWPs will not notice that the process is
2228 * stopping, and so complete halt of the process and the return of status
2229 * information to the parent could be delayed indefinitely.
2231 * To handle this race, proc_stop_callout() runs once per tick while there
2232 * are stopping processes in the system. It sets LWPs that are sleeping
2233 * interruptably into the LSSTOP state.
2235 * Note that we are not concerned about keeping all LWPs stopped while the
2236 * process is stopped: stopped LWPs can awaken briefly to handle signals.
2237 * What we do need to ensure is that all LWPs in a stopping process have
2238 * stopped at least once, so that notification can be sent to the parent
2242 proc_stop_callout(void *cookie
)
2253 mutex_enter(proc_lock
);
2254 PROCLIST_FOREACH(p
, &allproc
) {
2255 if ((p
->p_flag
& PK_MARKER
) != 0)
2257 mutex_enter(p
->p_lock
);
2259 if ((p
->p_sflag
& PS_STOPPING
) == 0) {
2260 mutex_exit(p
->p_lock
);
2264 /* Stop any LWPs sleeping interruptably. */
2266 if (p
->p_nrlwps
== 0) {
2268 * We brought the process to a halt.
2269 * Mark it as stopped and notify the
2272 if ((p
->p_sflag
& PS_NOTIFYSTOP
) != 0) {
2274 * Note that proc_stop_done() will
2275 * drop p->p_lock briefly.
2276 * Arrange to restart and check
2277 * all processes again.
2281 proc_stop_done(p
, true, PS_NOCLDSTOP
);
2285 mutex_exit(p
->p_lock
);
2289 mutex_exit(proc_lock
);
2293 * If we noted processes that are stopping but still have
2294 * running LWPs, then arrange to check again in 1 tick.
2297 callout_schedule(&proc_stop_ch
, 1);
2301 * Given a process in state SSTOP, set the state back to SACTIVE and
2302 * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
2305 proc_unstop(struct proc
*p
)
2310 KASSERT(mutex_owned(proc_lock
));
2311 KASSERT(mutex_owned(p
->p_lock
));
2313 p
->p_stat
= SACTIVE
;
2314 p
->p_sflag
&= ~PS_STOPPING
;
2318 p
->p_pptr
->p_nstopchild
--;
2320 LIST_FOREACH(l
, &p
->p_lwps
, l_sibling
) {
2322 if (l
->l_stat
!= LSSTOP
) {
2326 if (l
->l_wchan
== NULL
) {
2330 if (sig
&& (l
->l_flag
& LW_SINTR
) != 0) {
2334 l
->l_stat
= LSSLEEP
;
2342 filt_sigattach(struct knote
*kn
)
2344 struct proc
*p
= curproc
;
2347 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
2349 mutex_enter(p
->p_lock
);
2350 SLIST_INSERT_HEAD(&p
->p_klist
, kn
, kn_selnext
);
2351 mutex_exit(p
->p_lock
);
2357 filt_sigdetach(struct knote
*kn
)
2359 struct proc
*p
= kn
->kn_obj
;
2361 mutex_enter(p
->p_lock
);
2362 SLIST_REMOVE(&p
->p_klist
, kn
, knote
, kn_selnext
);
2363 mutex_exit(p
->p_lock
);
2367 * Signal knotes are shared with proc knotes, so we apply a mask to
2368 * the hint in order to differentiate them from process hints. This
2369 * could be avoided by using a signal-specific knote list, but probably
2370 * isn't worth the trouble.
2373 filt_signal(struct knote
*kn
, long hint
)
2376 if (hint
& NOTE_SIGNAL
) {
2377 hint
&= ~NOTE_SIGNAL
;
2379 if (kn
->kn_id
== hint
)
2382 return (kn
->kn_data
!= 0);
2385 const struct filterops sig_filtops
= {
2386 0, filt_sigattach
, filt_sigdetach
, filt_signal