2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995-1998 Apple Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1989, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
71 #define SIGPROP /* include signal properties table */
72 #include <sys/param.h>
73 #include <sys/resourcevar.h>
74 #include <sys/proc_internal.h>
75 #include <sys/kauth.h>
76 #include <sys/systm.h>
77 #include <sys/timeb.h>
78 #include <sys/times.h>
80 #include <sys/file_internal.h>
81 #include <sys/kernel.h>
83 #include <sys/signalvar.h>
85 #include <sys/ktrace.h>
87 #include <sys/syslog.h>
90 #include <sys/kdebug.h>
92 #include <sys/mount.h>
93 #include <sys/sysproto.h>
95 #include <bsm/audit_kernel.h>
97 #include <machine/spl.h>
99 #include <kern/cpu_number.h>
102 #include <sys/user.h> /* for coredump */
103 #include <kern/ast.h> /* for APC support */
104 #include <kern/lock.h>
105 #include <kern/task.h> /* extern void *get_bsdtask_info(task_t); */
106 #include <kern/thread.h>
107 #include <kern/sched_prim.h>
108 #include <kern/thread_call.h>
109 #include <mach/exception.h>
110 #include <mach/task.h>
111 #include <mach/thread_act.h>
114 * Missing prototypes that Mach should export
118 extern int thread_enable_fpe(thread_t act
, int onoff
);
119 extern void unix_syscall_return(int error
);
120 extern thread_t
port_name_to_thread(mach_port_name_t port_name
);
121 extern kern_return_t
check_actforsig(task_t task
, thread_t thread
, int setast
);
122 extern kern_return_t
get_signalact(task_t
, thread_t
*, int);
123 extern boolean_t
thread_should_abort(thread_t
);
124 extern unsigned int get_useraddr(void);
130 extern void doexception(int exc
, int code
, int sub
);
132 void stop(struct proc
*p
);
133 int cansignal(struct proc
*, kauth_cred_t
, struct proc
*, int);
134 int killpg1(struct proc
*, int, int, int);
135 void sigexit_locked(struct proc
*, int);
136 int setsigvec(struct proc
*, int, struct __user_sigaction
*);
137 void exit1(struct proc
*, int, int *);
138 void psignal_uthread(thread_t
, int);
139 kern_return_t
do_bsdexception(int, int, int);
140 void __posix_sem_syscall_return(kern_return_t
);
142 /* implementations in osfmk/kern/sync_sema.c. We do not want port.h in this scope, so void * them */
143 kern_return_t
semaphore_timedwait_signal_trap_internal(void *, void *,time_t, int32_t, void (*)(int));
144 kern_return_t
semaphore_timedwait_trap_internal(void *, time_t, int32_t, void (*)(int));
145 kern_return_t
semaphore_wait_signal_trap_internal(void *, void *, void (*)(int));
146 kern_return_t
semaphore_wait_trap_internal(void *, void (*)(int));
148 static int filt_sigattach(struct knote
*kn
);
149 static void filt_sigdetach(struct knote
*kn
);
150 static int filt_signal(struct knote
*kn
, long hint
);
152 struct filterops sig_filtops
=
153 { 0, filt_sigattach
, filt_sigdetach
, filt_signal
};
157 * NOTE: Source and target may *NOT* overlap! (target is smaller)
160 sigaltstack_64to32(struct user_sigaltstack
*in
, struct sigaltstack
*out
)
162 out
->ss_sp
= CAST_DOWN(void *,in
->ss_sp
);
163 out
->ss_size
= in
->ss_size
;
164 out
->ss_flags
= in
->ss_flags
;
168 * NOTE: Source and target may are permitted to overlap! (source is smaller);
169 * this works because we copy fields in order from the end of the struct to
173 sigaltstack_32to64(struct sigaltstack
*in
, struct user_sigaltstack
*out
)
175 out
->ss_flags
= in
->ss_flags
;
176 out
->ss_size
= in
->ss_size
;
177 out
->ss_sp
= CAST_USER_ADDR_T(in
->ss_sp
);
181 sigaction_64to32(struct user_sigaction
*in
, struct sigaction
*out
)
183 /* This assumes 32 bit __sa_handler is of type sig_t */
184 out
->__sigaction_u
.__sa_handler
= CAST_DOWN(sig_t
,in
->__sigaction_u
.__sa_handler
);
185 out
->sa_mask
= in
->sa_mask
;
186 out
->sa_flags
= in
->sa_flags
;
190 __sigaction_32to64(struct __sigaction
*in
, struct __user_sigaction
*out
)
192 out
->__sigaction_u
.__sa_handler
= CAST_USER_ADDR_T(in
->__sigaction_u
.__sa_handler
);
193 out
->sa_tramp
= CAST_USER_ADDR_T(in
->sa_tramp
);
194 out
->sa_mask
= in
->sa_mask
;
195 out
->sa_flags
= in
->sa_flags
;
200 void ram_printf(int);
202 unsigned int rdebug_proc
=0;
209 #endif /* SIGNAL_DEBUG */
212 signal_lock(struct proc
*p
)
219 int register sp
, *fp
, numsaved
;
221 __asm__
volatile("mr %0,r1" : "=r" (sp
));
223 fp
= (int *)*((int *)sp
);
224 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
225 p
->lockpc
[numsaved
] = fp
[2];
232 #endif /* SIGNAL_DEBUG */
233 #endif /* DIAGNOSTIC */
236 error
= lockmgr((struct lock__bsd__
*)&p
->signal_lock
[0], LK_EXCLUSIVE
, 0, (struct proc
*)0);
243 signal_unlock(struct proc
*p
)
249 int register sp
, *fp
, numsaved
;
251 __asm__
volatile("mr %0,r1" : "=r" (sp
));
253 fp
= (int *)*((int *)sp
);
254 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
255 p
->unlockpc
[numsaved
] = fp
[2];
262 #endif /* SIGNAL_DEBUG */
263 #endif /* DIAGNOSTIC */
265 /* TBD: check p last arg */
266 return(lockmgr((struct lock__bsd__
*)&p
->signal_lock
[0], LK_RELEASE
, (simple_lock_t
)0, (struct proc
*)0));
270 signal_setast(sig_actthread
)
271 thread_t sig_actthread
;
273 act_set_astbsd(sig_actthread
);
277 * Can process p, with ucred uc, send the signal signum to process q?
280 cansignal(p
, uc
, q
, signum
)
286 /* you can signal yourself */
290 if (!suser(uc
, NULL
))
291 return (1); /* root can always signal */
293 if (signum
== SIGCONT
&& q
->p_session
== p
->p_session
)
294 return (1); /* SIGCONT in session */
297 * Using kill(), only certain signals can be sent to setugid
300 if (q
->p_flag
& P_SUGID
) {
313 if (uc
->cr_ruid
== q
->p_ucred
->cr_ruid
||
314 kauth_cred_getuid(uc
) == q
->p_ucred
->cr_ruid
||
315 uc
->cr_ruid
== kauth_cred_getuid(q
->p_ucred
) ||
316 kauth_cred_getuid(uc
) == kauth_cred_getuid(q
->p_ucred
))
323 * because the P_SUGID test exists, this has extra tests which
326 if (uc
->cr_ruid
== q
->p_ucred
->cr_ruid
||
327 uc
->cr_ruid
== q
->p_ucred
->cr_svuid
||
328 kauth_cred_getuid(uc
) == q
->p_ucred
->cr_ruid
||
329 kauth_cred_getuid(uc
) == q
->p_ucred
->cr_svuid
||
330 uc
->cr_ruid
== kauth_cred_getuid(q
->p_ucred
) ||
331 kauth_cred_getuid(uc
) == kauth_cred_getuid(q
->p_ucred
))
339 sigaction(struct proc
*p
, register struct sigaction_args
*uap
, __unused register_t
*retval
)
341 struct user_sigaction vec
;
342 struct __user_sigaction __vec
;
344 struct user_sigaction
*sa
= &vec
;
345 register struct sigacts
*ps
= p
->p_sigacts
;
350 signum
= uap
->signum
;
351 if (signum
<= 0 || signum
>= NSIG
||
352 signum
== SIGKILL
|| signum
== SIGSTOP
)
356 sa
->sa_handler
= ps
->ps_sigact
[signum
];
357 sa
->sa_mask
= ps
->ps_catchmask
[signum
];
358 bit
= sigmask(signum
);
360 if ((ps
->ps_sigonstack
& bit
) != 0)
361 sa
->sa_flags
|= SA_ONSTACK
;
362 if ((ps
->ps_sigintr
& bit
) == 0)
363 sa
->sa_flags
|= SA_RESTART
;
364 if (ps
->ps_siginfo
& bit
)
365 sa
->sa_flags
|= SA_SIGINFO
;
366 if (ps
->ps_signodefer
& bit
)
367 sa
->sa_flags
|= SA_NODEFER
;
368 if (ps
->ps_64regset
& bit
)
369 sa
->sa_flags
|= SA_64REGSET
;
370 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDSTOP
))
371 sa
->sa_flags
|= SA_NOCLDSTOP
;
372 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDWAIT
))
373 sa
->sa_flags
|= SA_NOCLDWAIT
;
375 if (IS_64BIT_PROCESS(p
)) {
376 error
= copyout(sa
, uap
->osa
, sizeof(struct user_sigaction
));
378 struct sigaction vec32
;
379 sigaction_64to32(sa
, &vec32
);
380 error
= copyout(&vec32
, uap
->osa
, sizeof(struct sigaction
));
386 if (IS_64BIT_PROCESS(p
)) {
387 error
= copyin(uap
->nsa
, &__vec
, sizeof(struct __user_sigaction
));
389 struct __sigaction __vec32
;
390 error
= copyin(uap
->nsa
, &__vec32
, sizeof(struct __sigaction
));
391 __sigaction_32to64(&__vec32
, &__vec
);
395 error
= setsigvec(p
, signum
, &__vec
);
400 /* Routines to manipulate bits on all threads */
402 clear_procsiglist(struct proc
*p
, int bit
)
404 struct uthread
* uth
;
409 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
410 thact
= p
->p_vforkact
;
411 uth
= (struct uthread
*)get_bsdthread_info(thact
);
413 uth
->uu_siglist
&= ~bit
;
415 p
->p_siglist
&= ~bit
;
420 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
421 uth
->uu_siglist
&= ~bit
;
423 p
->p_siglist
&= ~bit
;
430 unblock_procsigmask(struct proc
*p
, int bit
)
432 struct uthread
* uth
;
436 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
437 thact
= p
->p_vforkact
;
438 uth
= (struct uthread
*)get_bsdthread_info(thact
);
440 uth
->uu_sigmask
&= ~bit
;
442 p
->p_sigmask
&= ~bit
;
446 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
447 uth
->uu_sigmask
&= ~bit
;
449 p
->p_sigmask
&= ~bit
;
456 block_procsigmask(struct proc
*p
, int bit
)
458 struct uthread
* uth
;
462 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
463 thact
= p
->p_vforkact
;
464 uth
= (struct uthread
*)get_bsdthread_info(thact
);
466 uth
->uu_sigmask
|= bit
;
472 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
473 uth
->uu_sigmask
|= bit
;
481 set_procsigmask(struct proc
*p
, int bit
)
483 struct uthread
* uth
;
487 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
488 thact
= p
->p_vforkact
;
489 uth
= (struct uthread
*)get_bsdthread_info(thact
);
491 uth
->uu_sigmask
= bit
;
497 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
498 uth
->uu_sigmask
= bit
;
505 /* XXX should be static? */
507 setsigvec(struct proc
*p
, int signum
, struct __user_sigaction
*sa
)
509 register struct sigacts
*ps
= p
->p_sigacts
;
512 if ((signum
== SIGKILL
|| signum
== SIGSTOP
) &&
513 sa
->sa_handler
!= SIG_DFL
)
515 bit
= sigmask(signum
);
517 * Change setting atomically.
519 ps
->ps_sigact
[signum
] = sa
->sa_handler
;
520 ps
->ps_trampact
[signum
] = sa
->sa_tramp
;
521 ps
->ps_catchmask
[signum
] = sa
->sa_mask
&~ sigcantmask
;
522 if (sa
->sa_flags
& SA_SIGINFO
)
523 ps
->ps_siginfo
|= bit
;
525 ps
->ps_siginfo
&= ~bit
;
526 if (sa
->sa_flags
& SA_64REGSET
)
527 ps
->ps_64regset
|= bit
;
529 ps
->ps_64regset
&= ~bit
;
530 if ((sa
->sa_flags
& SA_RESTART
) == 0)
531 ps
->ps_sigintr
|= bit
;
533 ps
->ps_sigintr
&= ~bit
;
534 if (sa
->sa_flags
& SA_ONSTACK
)
535 ps
->ps_sigonstack
|= bit
;
537 ps
->ps_sigonstack
&= ~bit
;
538 if (sa
->sa_flags
& SA_USERTRAMP
)
539 ps
->ps_usertramp
|= bit
;
541 ps
->ps_usertramp
&= ~bit
;
542 if (sa
->sa_flags
& SA_RESETHAND
)
543 ps
->ps_sigreset
|= bit
;
545 ps
->ps_sigreset
&= ~bit
;
546 if (sa
->sa_flags
& SA_NODEFER
)
547 ps
->ps_signodefer
|= bit
;
549 ps
->ps_signodefer
&= ~bit
;
550 if (signum
== SIGCHLD
) {
551 if (sa
->sa_flags
& SA_NOCLDSTOP
)
552 p
->p_flag
|= P_NOCLDSTOP
;
554 p
->p_flag
&= ~P_NOCLDSTOP
;
555 if ((sa
->sa_flags
& SA_NOCLDWAIT
) || (sa
->sa_handler
== SIG_IGN
))
556 p
->p_flag
|= P_NOCLDWAIT
;
558 p
->p_flag
&= ~P_NOCLDWAIT
;
562 if (signum
== SIGFPE
) {
563 if (sa
->sa_handler
== SIG_DFL
|| sa
->sa_handler
== SIG_IGN
)
564 thread_enable_fpe(current_thread(), 0);
566 thread_enable_fpe(current_thread(), 1);
570 * Set bit in p_sigignore for signals that are set to SIG_IGN,
571 * and for signals set to SIG_DFL where the default is to ignore.
572 * However, don't put SIGCONT in p_sigignore,
573 * as we have to restart the process.
575 if (sa
->sa_handler
== SIG_IGN
||
576 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
578 clear_procsiglist(p
, bit
);
579 if (signum
!= SIGCONT
)
580 p
->p_sigignore
|= bit
; /* easier in psignal */
581 p
->p_sigcatch
&= ~bit
;
583 p
->p_sigignore
&= ~bit
;
584 if (sa
->sa_handler
== SIG_DFL
)
585 p
->p_sigcatch
&= ~bit
;
587 p
->p_sigcatch
|= bit
;
593 * Initialize signal state for process 0;
594 * set to ignore signals that are ignored by default.
602 for (i
= 0; i
< NSIG
; i
++)
603 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
604 p
->p_sigignore
|= sigmask(i
);
608 * Reset signals for an exec of the specified process.
612 register struct proc
*p
;
613 register thread_t thr_act
;
615 register struct sigacts
*ps
= p
->p_sigacts
;
616 register int nc
, mask
;
617 struct uthread
*ut
= (struct uthread
*)0;
620 ut
= (struct uthread
*)get_bsdthread_info(thr_act
);
623 * Reset caught signals. Held signals remain held
624 * through p_sigmask (unless they were caught,
625 * and are now ignored by default).
627 while (p
->p_sigcatch
) {
628 nc
= ffs((long)p
->p_sigcatch
);
630 p
->p_sigcatch
&= ~mask
;
631 if (sigprop
[nc
] & SA_IGNORE
) {
633 p
->p_sigignore
|= mask
;
635 ut
->uu_siglist
&= ~mask
;
636 p
->p_siglist
&= ~mask
;
638 clear_procsiglist(p
, mask
);
640 ps
->ps_sigact
[nc
] = SIG_DFL
;
643 * Reset stack state to the user stack.
644 * Clear set of signals caught on the signal stack.
646 ps
->ps_sigstk
.ss_flags
= SA_DISABLE
;
647 ps
->ps_sigstk
.ss_size
= 0;
648 ps
->ps_sigstk
.ss_sp
= USER_ADDR_NULL
;
651 ut
->uu_sigstk
.ss_flags
= SA_DISABLE
;
652 ut
->uu_sigstk
.ss_size
= 0;
653 ut
->uu_sigstk
.ss_sp
= USER_ADDR_NULL
;
654 ut
->uu_flag
&= ~UT_ALTSTACK
;
656 ps
->ps_sigonstack
= 0;
660 * Manipulate signal mask.
661 * Note that we receive new mask, not pointer,
662 * and return old mask as return value;
663 * the library stub does the rest.
666 sigprocmask(register struct proc
*p
, struct sigprocmask_args
*uap
, __unused register_t
*retval
)
669 sigset_t oldmask
, nmask
;
670 user_addr_t omask
= uap
->omask
;
673 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
674 oldmask
= ut
->uu_sigmask
;
676 if (uap
->mask
== USER_ADDR_NULL
) {
677 /* just want old mask */
680 error
= copyin(uap
->mask
, &nmask
, sizeof(sigset_t
));
686 block_procsigmask(p
, (nmask
& ~sigcantmask
));
687 signal_setast(current_thread());
691 unblock_procsigmask(p
, (nmask
& ~sigcantmask
));
692 signal_setast(current_thread());
696 set_procsigmask(p
, (nmask
& ~sigcantmask
));
697 signal_setast(current_thread());
705 if (!error
&& omask
!= USER_ADDR_NULL
)
706 copyout(&oldmask
, omask
, sizeof(sigset_t
));
711 sigpending(__unused
struct proc
*p
, register struct sigpending_args
*uap
, __unused register_t
*retval
)
716 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
717 pendlist
= ut
->uu_siglist
;
720 copyout(&pendlist
, uap
->osv
, sizeof(sigset_t
));
726 * Suspend process until signal, providing mask to be set
727 * in the meantime. Note nonstandard calling convention:
728 * libc stub passes mask, not pointer, to save a copyin.
732 sigcontinue(__unused
int error
)
734 // struct uthread *ut = get_bsdthread_info(current_thread());
735 unix_syscall_return(EINTR
);
739 sigsuspend(register struct proc
*p
, struct sigsuspend_args
*uap
, __unused register_t
*retval
)
743 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
746 * When returning from sigpause, we want
747 * the old mask to be restored after the
748 * signal handler has finished. Thus, we
749 * save it here and mark the sigacts structure
752 ut
->uu_oldmask
= ut
->uu_sigmask
;
753 ut
->uu_flag
|= UT_SAS_OLDMASK
;
754 ut
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
755 (void) tsleep0((caddr_t
) p
, PPAUSE
|PCATCH
, "pause", 0, sigcontinue
);
756 /* always return EINTR rather than ERESTART... */
762 __disable_threadsignal(struct proc
*p
,
763 __unused
register struct __disable_threadsignal_args
*uap
,
764 __unused register_t
*retval
)
768 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
770 /* No longer valid to have any signal delivered */
772 uth
->uu_flag
|= UT_NO_SIGMASK
;
781 __pthread_markcancel(p
, uap
, retval
)
783 register struct __pthread_markcancel_args
*uap
;
786 thread_act_t target_act
;
790 target_act
= (thread_act_t
)port_name_to_thread(uap
->thread_port
);
792 if (target_act
== THR_ACT_NULL
)
795 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
797 /* if the thread is in vfork do not cancel */
798 if ((uth
->uu_flag
& (P_VFORK
| UT_CANCEL
| UT_CANCELED
)) == 0) {
799 uth
->uu_flag
|= (UT_CANCEL
| UT_NO_SIGMASK
);
800 if (((uth
->uu_flag
& UT_NOTCANCELPT
) == 0)
801 && ((uth
->uu_flag
& UT_CANCELDISABLE
) == 0))
802 thread_abort_safely(target_act
);
805 thread_deallocate(target_act
);
809 /* if action =0 ; return the cancellation state ,
810 * if marked for cancellation, make the thread canceled
811 * if action = 1 ; Enable the cancel handling
812 * if action = 2; Disable the cancel handling
815 __pthread_canceled(p
, uap
, retval
)
817 register struct __pthread_canceled_args
*uap
;
820 thread_act_t thr_act
;
822 int action
= uap
->action
;
824 thr_act
= current_act();
825 uth
= (struct uthread
*)get_bsdthread_info(thr_act
);
829 uth
->uu_flag
&= ~UT_CANCELDISABLE
;
832 uth
->uu_flag
|= UT_CANCELDISABLE
;
836 /* if the thread is in vfork do not cancel */
837 if((uth
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
838 uth
->uu_flag
&= ~UT_CANCEL
;
839 uth
->uu_flag
|= (UT_CANCELED
| UT_NO_SIGMASK
);
848 __posix_sem_syscall_return(kern_return_t kern_result
)
852 if (kern_result
== KERN_SUCCESS
)
854 else if (kern_result
== KERN_ABORTED
)
856 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
860 unix_syscall_return(error
);
861 /* does not return */
866 __semwait_signal(p
, uap
, retval
)
868 register struct __semwait_signal_args
*uap
;
872 kern_return_t kern_result
;
873 mach_timespec_t then
;
879 then
.tv_sec
= uap
->tv_sec
;
880 then
.tv_nsec
= uap
->tv_nsec
;
883 then
.tv_sec
= uap
->tv_sec
- now
.tv_sec
;
884 then
.tv_nsec
= uap
->tv_nsec
- now
.tv_nsec
;
885 if (then
.tv_nsec
< 0) {
886 then
.tv_nsec
+= NSEC_PER_SEC
;
891 if (uap
->mutex_sem
== (void *)NULL
)
892 kern_result
= semaphore_timedwait_trap_internal(uap
->cond_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
894 kern_result
= semaphore_timedwait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
898 if (uap
->mutex_sem
== (void *)NULL
)
899 kern_result
= semaphore_wait_trap_internal(uap
->cond_sem
, __posix_sem_syscall_return
);
902 kern_result
= semaphore_wait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, __posix_sem_syscall_return
);
906 if (kern_result
== KERN_SUCCESS
)
908 else if (kern_result
== KERN_ABORTED
)
910 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
918 __pthread_kill(__unused
struct proc
*p
,
919 register struct __pthread_kill_args
*uap
,
920 __unused register_t
*retval
)
924 int signum
= uap
->sig
;
927 target_act
= (thread_t
)port_name_to_thread(uap
->thread_port
);
929 if (target_act
== THREAD_NULL
)
931 if ((u_int
)signum
>= NSIG
) {
936 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
938 if (uth
->uu_flag
& UT_NO_SIGMASK
) {
944 psignal_uthread(target_act
, signum
);
946 thread_deallocate(target_act
);
952 pthread_sigmask(__unused
register struct proc
*p
,
953 register struct pthread_sigmask_args
*uap
,
954 __unused register_t
*retval
)
956 user_addr_t set
= uap
->set
;
957 user_addr_t oset
= uap
->oset
;
963 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
964 oldset
= ut
->uu_sigmask
;
966 if (set
== USER_ADDR_NULL
) {
967 /* need only old mask */
971 error
= copyin(set
, &nset
, sizeof(sigset_t
));
977 ut
->uu_sigmask
|= (nset
& ~sigcantmask
);
981 ut
->uu_sigmask
&= ~(nset
);
982 signal_setast(current_thread());
986 ut
->uu_sigmask
= (nset
& ~sigcantmask
);
987 signal_setast(current_thread());
995 if (!error
&& oset
!= USER_ADDR_NULL
)
996 copyout(&oldset
, oset
, sizeof(sigset_t
));
1003 sigwait(register struct proc
*p
, register struct sigwait_args
*uap
, __unused register_t
*retval
)
1006 struct uthread
*uth
;
1013 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1015 if (uap
->set
== USER_ADDR_NULL
)
1018 error
= copyin(uap
->set
, &mask
, sizeof(sigset_t
));
1022 siglist
= (mask
& ~sigcantmask
);
1028 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
1032 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1033 if ( (sigw
= uth
->uu_siglist
& siglist
) ) {
1040 /* The signal was pending on a thread */
1044 * When returning from sigwait, we want
1045 * the old mask to be restored after the
1046 * signal handler has finished. Thus, we
1047 * save it here and mark the sigacts structure
1050 ut
->uu_oldmask
= ut
->uu_sigmask
;
1051 ut
->uu_flag
|= UT_SAS_OLDMASK
;
1052 if (siglist
== (sigset_t
)0)
1054 /* SIGKILL and SIGSTOP are not maskable as well */
1055 ut
->uu_sigmask
= ~(siglist
|sigcantmask
);
1056 ut
->uu_sigwait
= siglist
;
1057 /* No Continuations for now */
1058 error
= tsleep((caddr_t
)&ut
->uu_sigwait
, PPAUSE
|PCATCH
, "pause", 0);
1060 if ((error
== EINTR
) || (error
== ERESTART
))
1063 sigw
= (ut
->uu_sigwait
& siglist
);
1064 ut
->uu_sigmask
= ut
->uu_oldmask
;
1066 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
1070 signum
= ffs((unsigned int)sigw
);
1072 panic("sigwait with no signal wakeup");
1073 ut
->uu_siglist
&= ~(sigmask(signum
));
1074 if (uap
->sig
!= USER_ADDR_NULL
)
1075 error
= copyout(&signum
, uap
->sig
, sizeof(int));
1084 sigaltstack(struct proc
*p
, register struct sigaltstack_args
*uap
, __unused register_t
*retval
)
1086 struct sigacts
*psp
;
1087 struct user_sigaltstack
*pstk
;
1088 struct user_sigaltstack ss
;
1089 struct uthread
*uth
;
1090 int uthsigaltstack
= 0;
1093 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
1094 uthsigaltstack
= p
->p_lflag
& P_LTHSIGSTACK
;
1097 if (uthsigaltstack
!= 0) {
1098 pstk
= &uth
->uu_sigstk
;
1099 if ((uth
->uu_flag
& UT_ALTSTACK
) == 0)
1100 uth
->uu_sigstk
.ss_flags
|= SA_DISABLE
;
1102 pstk
= &psp
->ps_sigstk
;
1103 if ((psp
->ps_flags
& SAS_ALTSTACK
) == 0)
1104 psp
->ps_sigstk
.ss_flags
|= SA_DISABLE
;
1107 if (IS_64BIT_PROCESS(p
)) {
1108 error
= copyout(pstk
, uap
->oss
, sizeof(struct user_sigaltstack
));
1110 struct sigaltstack ss32
;
1111 sigaltstack_64to32(pstk
, &ss32
);
1112 error
= copyout(&ss32
, uap
->oss
, sizeof(struct sigaltstack
));
1117 if (uap
->nss
== USER_ADDR_NULL
)
1119 if (IS_64BIT_PROCESS(p
)) {
1120 error
= copyin(uap
->nss
, &ss
, sizeof(struct user_sigaltstack
));
1122 struct sigaltstack ss32
;
1123 error
= copyin(uap
->nss
, &ss32
, sizeof(struct sigaltstack
));
1124 sigaltstack_32to64(&ss32
,&ss
);
1128 if ((ss
.ss_flags
& ~SA_DISABLE
) != 0) {
1132 if (ss
.ss_flags
& SA_DISABLE
) {
1133 if (uthsigaltstack
!= 0) {
1134 /* if we are here we are not in the signal handler ;so no need to check */
1135 if (uth
->uu_sigstk
.ss_flags
& SA_ONSTACK
)
1137 uth
->uu_flag
&= ~UT_ALTSTACK
;
1138 uth
->uu_sigstk
.ss_flags
= ss
.ss_flags
;
1140 if (psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
)
1142 psp
->ps_flags
&= ~SAS_ALTSTACK
;
1143 psp
->ps_sigstk
.ss_flags
= ss
.ss_flags
;
1148 /* The older stacksize was 8K, enforce that one so no compat problems */
1149 #define OLDMINSIGSTKSZ 8*1024
1150 if (ss
.ss_size
< OLDMINSIGSTKSZ
)
1152 if (uthsigaltstack
!= 0) {
1153 uth
->uu_flag
|= UT_ALTSTACK
;
1156 psp
->ps_flags
|= SAS_ALTSTACK
;
1163 kill(struct proc
*cp
, struct kill_args
*uap
, __unused register_t
*retval
)
1165 register struct proc
*p
;
1166 kauth_cred_t uc
= kauth_cred_get();
1168 AUDIT_ARG(pid
, uap
->pid
);
1169 AUDIT_ARG(signum
, uap
->signum
);
1171 if ((u_int
)uap
->signum
>= NSIG
)
1174 /* kill single process */
1175 if ((p
= proc_findref(uap
->pid
)) == NULL
) {
1176 if ((p
= pzfind(uap
->pid
)) != NULL
) {
1178 * IEEE Std 1003.1-2001: return success
1179 * when killing a zombie.
1185 AUDIT_ARG(process
, p
);
1186 if (!cansignal(cp
, uc
, p
, uap
->signum
)) {
1191 psignal(p
, uap
->signum
);
1196 case -1: /* broadcast signal */
1197 return (killpg1(cp
, uap
->signum
, 0, 1));
1198 case 0: /* signal own process group */
1199 return (killpg1(cp
, uap
->signum
, 0, 0));
1200 default: /* negative explicit process group */
1201 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0));
1208 * Common code for kill process group/broadcast kill.
1209 * cp is calling process.
1212 killpg1(cp
, signum
, pgid
, all
)
1213 register struct proc
*cp
;
1214 int signum
, pgid
, all
;
1216 register struct proc
*p
;
1217 kauth_cred_t uc
= cp
->p_ucred
;
1225 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1226 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1227 p
== cp
|| !cansignal(cp
, uc
, p
, signum
))
1236 * zero pgid means send to my process group.
1240 pgrp
= pgfind(pgid
);
1244 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1245 p
= p
->p_pglist
.le_next
) {
1246 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1247 p
->p_stat
== SZOMB
||
1248 !cansignal(cp
, uc
, p
, signum
))
1255 return (nfound
? 0 : ESRCH
);
1259 * Send a signal to a process group.
1262 gsignal(pgid
, signum
)
1267 if (pgid
&& (pgrp
= pgfind(pgid
)))
1268 pgsignal(pgrp
, signum
, 0);
1272 * Send a signal to a process group. If checktty is 1,
1273 * limit to members which have a controlling terminal.
1276 pgsignal(pgrp
, signum
, checkctty
)
1278 int signum
, checkctty
;
1280 register struct proc
*p
;
1283 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1284 p
= p
->p_pglist
.le_next
)
1285 if (checkctty
== 0 || p
->p_flag
& P_CONTROLT
)
1290 * Send signal to a backgrounded process blocked due to tty access
1291 * In FreeBSD, the backgrounded process wakes up every second and
1292 * discovers whether it is foregounded or not. In our case, we block
1293 * the thread in tsleep as we want to avoid storm of processes as well
1294 * as the suspend is only at AST level
1297 tty_pgsignal(pgrp
, signum
)
1301 register struct proc
*p
;
1304 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1305 p
= p
->p_pglist
.le_next
)
1306 if ((p
->p_flag
& P_TTYSLEEP
) && (p
->p_flag
& P_CONTROLT
))
1311 * Send a signal caused by a trap to a specific thread.
1314 threadsignal(thread_t sig_actthread
, int signum
, u_long code
)
1316 register struct uthread
*uth
;
1317 register struct task
* sig_task
;
1318 register struct proc
*p
;
1321 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1324 mask
= sigmask(signum
);
1325 if ((mask
& threadmask
) == 0)
1327 sig_task
= get_threadtask(sig_actthread
);
1328 p
= (struct proc
*)(get_bsdtask_info(sig_task
));
1330 uth
= get_bsdthread_info(sig_actthread
);
1331 if (uth
&& (uth
->uu_flag
& UT_VFORK
))
1334 if (!(p
->p_flag
& P_TRACED
) && (p
->p_sigignore
& mask
))
1337 uth
->uu_siglist
|= mask
;
1338 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1339 uth
->uu_code
= code
;
1340 /* mark on process as well */
1341 signal_setast(sig_actthread
);
1347 register struct proc
*p
;
1348 register int signum
;
1350 psignal_lock(p
, signum
, 1);
1354 psignal_vfork(struct proc
*p
, task_t new_task
, thread_t thr_act
, int signum
)
1357 register sig_t action
;
1359 struct uthread
*uth
;
1361 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1362 panic("psignal signal number");
1363 mask
= sigmask(signum
);
1364 prop
= sigprop
[signum
];
1367 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1370 #endif /* SIGNAL_DEBUG */
1372 if ((new_task
== TASK_NULL
) || (thr_act
== (thread_t
)NULL
) || is_kerneltask(new_task
))
1376 uth
= get_bsdthread_info(thr_act
);
1380 * proc is traced, always give parent a chance.
1384 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1385 (p
->p_flag
& P_TRACED
) == 0)
1388 if (prop
& SA_CONT
) {
1389 p
->p_siglist
&= ~stopsigmask
;
1390 uth
->uu_siglist
&= ~stopsigmask
;
1393 if (prop
& SA_STOP
) {
1395 * If sending a tty stop signal to a member of an orphaned
1396 * process group, discard the signal here if the action
1397 * is default; don't stop the process below if sleeping,
1398 * and don't clear any pending SIGCONT.
1400 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1403 uth
->uu_siglist
&= ~contsigmask
;
1404 p
->p_siglist
&= ~contsigmask
;
1406 uth
->uu_siglist
|= mask
;
1407 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1409 /* Deliver signal to the activation passed in */
1410 act_set_astbsd(thr_act
);
1413 * SIGKILL priority twiddling moved here from above because
1414 * it needs sig_thread. Could merge it into large switch
1415 * below if we didn't care about priority for tracing
1416 * as SIGKILL's action is always SIG_DFL.
1418 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1423 * This Process is traced - wake it up (if not already
1424 * stopped) so that it can discover the signal in
1425 * issig() and stop for the parent.
1427 if (p
->p_flag
& P_TRACED
) {
1428 if (p
->p_stat
!= SSTOP
)
1435 * If we're being traced (possibly because someone attached us
1436 * while we were stopped), check for a signal from the debugger.
1438 if (p
->p_stat
== SSTOP
) {
1439 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
1440 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1441 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1446 * setrunnable(p) in BSD
1455 get_signalthread(struct proc
*p
, int signum
)
1457 struct uthread
*uth
;
1459 sigset_t mask
= sigmask(signum
);
1460 thread_t sig_thread_act
;
1461 struct task
* sig_task
= p
->task
;
1464 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
1465 sig_thread_act
= p
->p_vforkact
;
1466 kret
= check_actforsig(sig_task
, sig_thread_act
, 1);
1467 if (kret
== KERN_SUCCESS
)
1468 return(sig_thread_act
);
1470 return(THREAD_NULL
);
1473 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1474 if(((uth
->uu_flag
& UT_NO_SIGMASK
)== 0) &&
1475 (((uth
->uu_sigmask
& mask
) == 0) || (uth
->uu_sigwait
& mask
))) {
1476 if (check_actforsig(p
->task
, uth
->uu_act
, 1) == KERN_SUCCESS
)
1477 return(uth
->uu_act
);
1480 if (get_signalact(p
->task
, &thr_act
, 1) == KERN_SUCCESS
) {
1484 return(THREAD_NULL
);
1488 * Send the signal to the process. If the signal has an action, the action
1489 * is usually performed by the target process rather than the caller; we add
1490 * the signal to the set of pending signals for the process.
1493 * o When a stop signal is sent to a sleeping process that takes the
1494 * default action, the process is stopped without awakening it.
1495 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1496 * regardless of the signal action (eg, blocked or ignored).
1498 * Other ignored signals are discarded immediately.
1501 psignal_lock(p
, signum
, withlock
)
1502 register struct proc
*p
;
1503 register int signum
;
1504 register int withlock
;
1507 register sig_t action
;
1508 thread_t sig_thread_act
;
1509 register task_t sig_task
;
1511 struct uthread
*uth
;
1512 boolean_t funnel_state
= FALSE
;
1515 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1516 panic("psignal signal number");
1517 mask
= sigmask(signum
);
1518 prop
= sigprop
[signum
];
1521 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1524 #endif /* SIGNAL_DEBUG */
1526 if (thread_funnel_get() == (funnel_t
*)0) {
1528 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1531 * We will need the task pointer later. Grab it now to
1532 * check for a zombie process. Also don't send signals
1533 * to kernel internal tasks.
1535 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1537 thread_funnel_set(kernel_flock
, funnel_state
);
1541 KNOTE(&p
->p_klist
, NOTE_SIGNAL
| signum
);
1544 * do not send signals to the process that has the thread
1545 * doing a reboot(). Not doing so will mark that thread aborted
1546 * and can cause IO failures wich will cause data loss.
1548 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1550 thread_funnel_set(kernel_flock
, funnel_state
);
1558 * Deliver the signal to the first thread in the task. This
1559 * allows single threaded applications which use signals to
1560 * be able to be linked with multithreaded libraries. We have
1561 * an implicit reference to the current thread, but need
1562 * an explicit one otherwise. The thread reference keeps
1563 * the corresponding task data structures around too. This
1564 * reference is released by thread_deallocate.
1567 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1570 /* If successful return with ast set */
1571 sig_thread_act
= get_signalthread(p
, signum
);
1573 if (sig_thread_act
== THREAD_NULL
) {
1575 * if it is sigkill, may be we should
1576 * inject a thread to terminate
1580 #endif /* SIGNAL_DEBUG */
1584 uth
= get_bsdthread_info(sig_thread_act
);
1587 * If proc is traced, always give parent a chance.
1589 if (p
->p_flag
& P_TRACED
)
1593 * If the signal is being ignored,
1594 * then we forget about it immediately.
1595 * (Note: we don't set SIGCONT in p_sigignore,
1596 * and if it is set to SIG_IGN,
1597 * action will be SIG_DFL here.)
1599 if (p
->p_sigignore
& mask
)
1601 /* sigwait takes precedence */
1602 if (uth
->uu_sigwait
& mask
)
1603 action
= KERN_SIG_WAIT
;
1604 else if (uth
->uu_sigmask
& mask
)
1605 action
= KERN_SIG_HOLD
;
1606 else if (p
->p_sigcatch
& mask
)
1607 action
= KERN_SIG_CATCH
;
1612 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1613 (p
->p_flag
& P_TRACED
) == 0)
1616 if (prop
& SA_CONT
) {
1617 uth
->uu_siglist
&= ~stopsigmask
;
1618 p
->p_siglist
&= ~stopsigmask
;
1621 if (prop
& SA_STOP
) {
1623 * If sending a tty stop signal to a member of an orphaned
1624 * process group, discard the signal here if the action
1625 * is default; don't stop the process below if sleeping,
1626 * and don't clear any pending SIGCONT.
1628 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1631 uth
->uu_siglist
&= ~contsigmask
;
1632 p
->p_siglist
&= ~contsigmask
;
1634 uth
->uu_siglist
|= mask
;
1635 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1639 * Defer further processing for signals which are held,
1640 * except that stopped processes must be continued by SIGCONT.
1642 if (action
== KERN_SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
)) {
1646 * SIGKILL priority twiddling moved here from above because
1647 * it needs sig_thread. Could merge it into large switch
1648 * below if we didn't care about priority for tracing
1649 * as SIGKILL's action is always SIG_DFL.
1651 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1656 * Process is traced - wake it up (if not already
1657 * stopped) so that it can discover the signal in
1658 * issig() and stop for the parent.
1660 if (p
->p_flag
& P_TRACED
) {
1661 if (p
->p_stat
!= SSTOP
)
1667 if (action
== KERN_SIG_WAIT
) {
1668 uth
->uu_sigwait
= mask
;
1669 uth
->uu_siglist
&= ~mask
;
1670 p
->p_siglist
&= ~mask
;
1671 wakeup(&uth
->uu_sigwait
);
1672 /* if it is SIGCONT resume whole process */
1673 if (prop
& SA_CONT
) {
1674 p
->p_flag
|= P_CONTINUED
;
1675 (void) task_resume(sig_task
);
1680 if (action
!= SIG_DFL
) {
1682 * User wants to catch the signal.
1683 * Wake up the thread, but don't un-suspend it
1684 * (except for SIGCONT).
1686 if (prop
& SA_CONT
) {
1687 if (p
->p_flag
& P_TTYSLEEP
) {
1688 p
->p_flag
&= ~P_TTYSLEEP
;
1689 wakeup(&p
->p_siglist
);
1691 p
->p_flag
|= P_CONTINUED
;
1692 (void) task_resume(sig_task
);
1695 } else if (p
->p_stat
== SSTOP
)
1699 /* Default action - varies */
1700 if (mask
& stopsigmask
) {
1702 * These are the signals which by default
1705 * Don't clog system with children of init
1706 * stopped from the keyboard.
1708 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1709 psignal_lock(p
, SIGKILL
, 0);
1710 uth
->uu_siglist
&= ~mask
;
1711 p
->p_siglist
&= ~mask
;
1717 * if task hasn't already been stopped by
1720 uth
->uu_siglist
&= ~mask
;
1721 p
->p_siglist
&= ~mask
;
1722 if (p
->p_stat
!= SSTOP
) {
1723 p
->p_xstat
= signum
;
1725 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1726 struct proc
*pp
= p
->p_pptr
;
1728 pp
->si_pid
= p
->p_pid
;
1729 pp
->si_status
= p
->p_xstat
;
1730 pp
->si_code
= CLD_STOPPED
;
1731 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
1732 psignal(pp
, SIGCHLD
);
1740 * Signals ignored by default have been dealt
1741 * with already, since their bits are on in
1747 * Kill signal always sets process running and
1751 * Process will be running after 'run'
1755 thread_abort(sig_thread_act
);
1761 * Let the process run. If it's sleeping on an
1762 * event, it remains so.
1764 if (p
->p_flag
& P_TTYSLEEP
) {
1765 p
->p_flag
&= ~P_TTYSLEEP
;
1766 wakeup(&p
->p_siglist
);
1768 p
->p_flag
|= P_CONTINUED
;
1769 (void) task_resume(sig_task
);
1771 uth
->uu_siglist
&= ~mask
;
1772 p
->p_siglist
&= ~mask
;
1779 * All other signals wake up the process, but don't
1782 if (p
->p_stat
== SSTOP
)
1790 * If we're being traced (possibly because someone attached us
1791 * while we were stopped), check for a signal from the debugger.
1793 if (p
->p_stat
== SSTOP
) {
1794 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
1795 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1798 * setrunnable(p) in BSD and
1799 * Wake up the thread if it is interruptible.
1802 thread_abort_safely(sig_thread_act
);
1808 thread_funnel_set(kernel_flock
, funnel_state
);
1812 /* psignal_lock(p, signum, withlock ) */
1814 psignal_uthread(thr_act
, signum
)
1820 register sig_t action
;
1821 thread_t sig_thread_act
;
1822 register task_t sig_task
;
1824 struct uthread
*uth
;
1828 p
= (struct proc
*)get_bsdtask_info(get_threadtask(thr_act
));
1829 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1830 panic("Invalid signal number in psignal_uthread");
1831 mask
= sigmask(signum
);
1832 prop
= sigprop
[signum
];
1835 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1838 #endif /* SIGNAL_DEBUG */
1841 * We will need the task pointer later. Grab it now to
1842 * check for a zombie process. Also don't send signals
1843 * to kernel internal tasks.
1845 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1849 sig_thread_act
= thr_act
;
1851 * do not send signals to the process that has the thread
1852 * doing a reboot(). Not doing so will mark that thread aborted
1853 * and can cause IO failures wich will cause data loss.
1855 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1862 * Deliver the signal to the first thread in the task. This
1863 * allows single threaded applications which use signals to
1864 * be able to be linked with multithreaded libraries. We have
1865 * an implicit reference to the current thread, but need
1866 * an explicit one otherwise. The thread reference keeps
1867 * the corresponding task data structures around too. This
1868 * reference is released by thread_deallocate.
1871 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1874 kret
= check_actforsig(sig_task
, sig_thread_act
, 1);
1876 if (kret
!= KERN_SUCCESS
) {
1882 uth
= get_bsdthread_info(sig_thread_act
);
1885 * If proc is traced, always give parent a chance.
1887 if (p
->p_flag
& P_TRACED
)
1891 * If the signal is being ignored,
1892 * then we forget about it immediately.
1893 * (Note: we don't set SIGCONT in p_sigignore,
1894 * and if it is set to SIG_IGN,
1895 * action will be SIG_DFL here.)
1897 if (p
->p_sigignore
& mask
)
1899 /* sigwait takes precedence */
1900 if (uth
->uu_sigwait
& mask
)
1901 action
= KERN_SIG_WAIT
;
1902 else if (uth
->uu_sigmask
& mask
)
1903 action
= KERN_SIG_HOLD
;
1904 else if (p
->p_sigcatch
& mask
)
1905 action
= KERN_SIG_CATCH
;
1910 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1911 (p
->p_flag
& P_TRACED
) == 0)
1914 if (prop
& SA_CONT
) {
1915 uth
->uu_siglist
&= ~stopsigmask
;
1916 p
->p_siglist
&= ~stopsigmask
;
1919 if (prop
& SA_STOP
) {
1921 * If sending a tty stop signal to a member of an orphaned
1922 * process group, discard the signal here if the action
1923 * is default; don't stop the process below if sleeping,
1924 * and don't clear any pending SIGCONT.
1926 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1929 uth
->uu_siglist
&= ~contsigmask
;
1930 p
->p_siglist
&= ~contsigmask
;
1932 uth
->uu_siglist
|= mask
;
1933 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1936 * Defer further processing for signals which are held,
1937 * except that stopped processes must be continued by SIGCONT.
1939 if (action
== KERN_SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
))
1943 * SIGKILL priority twiddling moved here from above because
1944 * it needs sig_thread. Could merge it into large switch
1945 * below if we didn't care about priority for tracing
1946 * as SIGKILL's action is always SIG_DFL.
1948 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1953 * Process is traced - wake it up (if not already
1954 * stopped) so that it can discover the signal in
1955 * issig() and stop for the parent.
1957 if (p
->p_flag
& P_TRACED
) {
1958 if (p
->p_stat
!= SSTOP
)
1964 if (action
== KERN_SIG_WAIT
) {
1965 uth
->uu_sigwait
= mask
;
1966 uth
->uu_siglist
&= ~mask
;
1967 p
->p_siglist
&= ~mask
;
1968 wakeup(&uth
->uu_sigwait
);
1969 /* if it is SIGCONT resume whole process */
1970 if (prop
& SA_CONT
) {
1971 p
->p_flag
|= P_CONTINUED
;
1972 (void) task_resume(sig_task
);
1977 if (action
!= SIG_DFL
) {
1979 * User wants to catch the signal.
1980 * Wake up the thread, but don't un-suspend it
1981 * (except for SIGCONT).
1983 if (prop
& SA_CONT
) {
1984 p
->p_flag
|= P_CONTINUED
;
1985 (void) task_resume(sig_task
);
1989 /* Default action - varies */
1990 if (mask
& stopsigmask
) {
1992 * These are the signals which by default
1995 * Don't clog system with children of init
1996 * stopped from the keyboard.
1998 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1999 psignal_lock(p
, SIGKILL
, 0);
2000 uth
->uu_siglist
&= ~mask
;
2001 p
->p_siglist
&= ~mask
;
2007 * if task hasn't already been stopped by
2010 uth
->uu_siglist
&= ~mask
;
2011 p
->p_siglist
&= ~mask
;
2012 if (p
->p_stat
!= SSTOP
) {
2013 p
->p_xstat
= signum
;
2014 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
2015 struct proc
*pp
= p
->p_pptr
;
2017 pp
->si_pid
= p
->p_pid
;
2018 pp
->si_status
= p
->p_xstat
;
2019 pp
->si_code
= CLD_STOPPED
;
2020 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
2021 psignal(pp
, SIGCHLD
);
2030 * Signals ignored by default have been dealt
2031 * with already, since their bits are on in
2037 * Kill signal always sets process running and
2041 * Process will be running after 'run'
2045 thread_abort(sig_thread_act
);
2051 * Let the process run. If it's sleeping on an
2052 * event, it remains so.
2054 if (p
->p_flag
& P_TTYSLEEP
) {
2055 p
->p_flag
&= ~P_TTYSLEEP
;
2056 wakeup(&p
->p_siglist
);
2058 p
->p_flag
|= P_CONTINUED
;
2059 (void) task_resume(sig_task
);
2061 uth
->uu_siglist
&= ~mask
;
2062 p
->p_siglist
&= ~mask
;
2068 * All other signals wake up the process, but don't
2077 * If we're being traced (possibly because someone attached us
2078 * while we were stopped), check for a signal from the debugger.
2080 if (p
->p_stat
== SSTOP
) {
2081 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
2082 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
2083 p
->p_siglist
|= sigmask(p
->p_xstat
);
2087 * setrunnable(p) in BSD and
2088 * Wake up the thread if it is interruptible.
2091 thread_abort_safely(sig_thread_act
);
2100 sig_lock_to_exit(struct proc
*p
)
2102 thread_t self
= current_thread();
2104 p
->exit_thread
= self
;
2105 (void) task_suspend(p
->task
);
2109 sig_try_locked(struct proc
*p
)
2111 thread_t self
= current_thread();
2113 while (p
->sigwait
|| p
->exit_thread
) {
2114 if (p
->exit_thread
) {
2115 if (p
->exit_thread
!= self
) {
2117 * Already exiting - no signals.
2123 if(assert_wait_possible()) {
2124 assert_wait((caddr_t
)&p
->sigwait_thread
,
2125 (THREAD_INTERRUPTIBLE
));
2128 thread_block(THREAD_CONTINUE_NULL
);
2130 if (thread_should_abort(self
)) {
2132 * Terminate request - clean up.
2141 * If the current process has received a signal (should be caught or cause
2142 * termination, should interrupt current syscall), return the signal number.
2143 * Stop signals with default action are processed immediately, then cleared;
2144 * they aren't returned. This is checked after each entry to the system for
2145 * a syscall or trap (though this can usually be done without calling issignal
2146 * by checking the pending signal masks in the CURSIG macro.) The normal call
2149 * while (signum = CURSIG(curproc))
2154 register struct proc
*p
;
2156 register int signum
, mask
, prop
, sigbits
;
2158 struct uthread
* ut
;
2161 cur_act
= current_thread();
2164 if(rdebug_proc
&& (p
== rdebug_proc
)) {
2167 #endif /* SIGNAL_DEBUG */
2171 * Try to grab the signal lock.
2173 if (sig_try_locked(p
) <= 0) {
2178 ut
= get_bsdthread_info(cur_act
);
2180 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2182 if (p
->p_flag
& P_PPWAIT
)
2183 sigbits
&= ~stopsigmask
;
2184 if (sigbits
== 0) { /* no signal to send */
2188 signum
= ffs((long)sigbits
);
2189 mask
= sigmask(signum
);
2190 prop
= sigprop
[signum
];
2193 * We should see pending but ignored signals
2194 * only if P_TRACED was on when they were posted.
2196 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2197 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2198 p
->p_siglist
&= ~mask
; /* take the signal! */
2201 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2202 register task_t task
;
2204 * If traced, always stop, and stay
2205 * stopped until released by the debugger.
2207 /* ptrace debugging */
2208 p
->p_xstat
= signum
;
2210 if (p
->p_flag
& P_SIGEXC
) {
2212 p
->sigwait_thread
= cur_act
;
2214 p
->p_flag
&= ~(P_WAITED
|P_CONTINUED
);
2215 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2216 p
->p_siglist
&= ~mask
; /* clear the old signal */
2218 do_bsdexception(EXC_SOFTWARE
, EXC_SOFT_SIGNAL
, signum
);
2221 // panic("Unsupportef gdb option \n");;
2222 pp
->si_pid
= p
->p_pid
;
2223 pp
->si_status
= p
->p_xstat
;
2224 pp
->si_code
= CLD_TRAPPED
;
2225 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
2226 psignal(pp
, SIGCHLD
);
2228 * XXX Have to really stop for debuggers;
2229 * XXX stop() doesn't do the right thing.
2230 * XXX Inline the task_suspend because we
2231 * XXX have to diddle Unix state in the
2237 p
->sigwait_thread
= cur_act
;
2239 p
->p_flag
&= ~(P_WAITED
|P_CONTINUED
);
2240 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2241 p
->p_siglist
&= ~mask
; /* clear the old signal */
2243 wakeup((caddr_t
)p
->p_pptr
);
2245 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
2246 thread_block(THREAD_CONTINUE_NULL
);
2251 p
->sigwait_thread
= NULL
;
2252 wakeup((caddr_t
)&p
->sigwait_thread
);
2255 * This code is to detect when gdb is killed
2256 * even as the traced program is attached.
2257 * pgsignal would get the SIGKILL to traced program
2258 * That's what we are trying to see (I hope)
2260 if (ut
->uu_siglist
& sigmask(SIGKILL
)) {
2262 * Wait event may still be outstanding;
2263 * clear it, since sig_lock_to_exit will
2266 clear_wait(current_thread(), THREAD_INTERRUPTED
);
2267 sig_lock_to_exit(p
);
2269 * Since this thread will be resumed
2270 * to allow the current syscall to
2271 * be completed, must save u_qsave
2272 * before calling exit(). (Since exit()
2273 * calls closef() which can trash u_qsave.)
2276 exit1(p
,signum
, (int *)NULL
);
2281 * We may have to quit
2283 if (thread_should_abort(current_thread())) {
2288 * If parent wants us to take the signal,
2289 * then it will leave it in p->p_xstat;
2290 * otherwise we just look for signals again.
2292 signum
= p
->p_xstat
;
2296 * Put the new signal into p_siglist. If the
2297 * signal is being masked, look for other signals.
2299 mask
= sigmask(signum
);
2300 ut
->uu_siglist
|= mask
;
2301 p
->p_siglist
|= mask
; /* just for lame ones looking here */
2302 if (ut
->uu_sigmask
& mask
)
2307 * Decide whether the signal should be returned.
2308 * Return the signal's number, or fall through
2309 * to clear it from the pending mask.
2312 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2316 * Don't take default actions on system processes.
2318 if (p
->p_pptr
->p_pid
== 0) {
2321 * Are you sure you want to ignore SIGSEGV
2324 printf("Process (pid %d) got signal %d\n",
2327 break; /* == ignore */
2331 * If there is a pending stop signal to process
2332 * with default action, stop here,
2333 * then clear the signal. However,
2334 * if process is member of an orphaned
2335 * process group, ignore tty stop signals.
2337 if (prop
& SA_STOP
) {
2338 if (p
->p_flag
& P_TRACED
||
2339 (p
->p_pgrp
->pg_jobc
== 0 &&
2341 break; /* == ignore */
2342 if (p
->p_stat
!= SSTOP
) {
2343 p
->p_xstat
= signum
;
2345 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
2347 pp
->si_pid
= p
->p_pid
;
2348 pp
->si_status
= p
->p_xstat
;
2349 pp
->si_code
= CLD_STOPPED
;
2350 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
2351 psignal(pp
, SIGCHLD
);
2355 } else if (prop
& SA_IGNORE
) {
2357 * Except for SIGCONT, shouldn't get here.
2358 * Default action is to ignore; drop it.
2360 break; /* == ignore */
2362 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2363 p
->p_siglist
&= ~mask
; /* take the signal! */
2371 * Masking above should prevent us ever trying
2372 * to take action on an ignored signal other
2373 * than SIGCONT, unless process is traced.
2375 if ((prop
& SA_CONT
) == 0 &&
2376 (p
->p_flag
& P_TRACED
) == 0)
2377 printf("issignal\n");
2378 break; /* == ignore */
2382 * This signal has an action, let
2383 * postsig() process it.
2385 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2386 p
->p_siglist
&= ~mask
; /* take the signal! */
2390 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2391 p
->p_siglist
&= ~mask
; /* take the signal! */
2396 /* called from _sleep */
2399 register struct proc
*p
;
2401 register int signum
, mask
, prop
, sigbits
;
2403 struct uthread
* ut
;
2407 cur_act
= current_thread();
2409 ut
= get_bsdthread_info(cur_act
);
2411 if (ut
->uu_siglist
== 0)
2414 if (((ut
->uu_siglist
& ~ut
->uu_sigmask
) == 0) && ((p
->p_flag
& P_TRACED
) == 0))
2417 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2420 if (p
->p_flag
& P_PPWAIT
)
2421 sigbits
&= ~stopsigmask
;
2422 if (sigbits
== 0) { /* no signal to send */
2426 signum
= ffs((long)sigbits
);
2427 mask
= sigmask(signum
);
2428 prop
= sigprop
[signum
];
2431 * We should see pending but ignored signals
2432 * only if P_TRACED was on when they were posted.
2434 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2437 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2439 * Put the new signal into p_siglist. If the
2440 * signal is being masked, look for other signals.
2442 mask
= sigmask(signum
);
2443 if (ut
->uu_sigmask
& mask
)
2449 * Decide whether the signal should be returned.
2450 * Return the signal's number, or fall through
2451 * to clear it from the pending mask.
2454 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2458 * Don't take default actions on system processes.
2460 if (p
->p_pptr
->p_pid
== 0) {
2463 * Are you sure you want to ignore SIGSEGV
2466 printf("Process (pid %d) got signal %d\n",
2469 break; /* == ignore */
2473 * If there is a pending stop signal to process
2474 * with default action, stop here,
2475 * then clear the signal. However,
2476 * if process is member of an orphaned
2477 * process group, ignore tty stop signals.
2479 if (prop
& SA_STOP
) {
2480 if (p
->p_flag
& P_TRACED
||
2481 (p
->p_pgrp
->pg_jobc
== 0 &&
2483 break; /* == ignore */
2486 } else if (prop
& SA_IGNORE
) {
2488 * Except for SIGCONT, shouldn't get here.
2489 * Default action is to ignore; drop it.
2491 break; /* == ignore */
2499 * Masking above should prevent us ever trying
2500 * to take action on an ignored signal other
2501 * than SIGCONT, unless process is traced.
2503 if ((prop
& SA_CONT
) == 0 &&
2504 (p
->p_flag
& P_TRACED
) == 0)
2505 printf("issignal\n");
2506 break; /* == ignore */
2510 * This signal has an action, let
2511 * postsig() process it.
2515 sigbits
&= ~mask
; /* take the signal! */
2521 * Put the argument process into the stopped state and notify the parent
2522 * via wakeup. Signals are handled elsewhere. The process must not be
2527 register struct proc
*p
;
2530 p
->p_flag
&= ~(P_WAITED
|P_CONTINUED
);
2531 if (p
->p_pptr
->p_stat
!= SSTOP
)
2532 wakeup((caddr_t
)p
->p_pptr
);
2533 (void) task_suspend(p
->task
); /*XXX*/
2537 * Take the action for the specified signal
2538 * from the current set of pending signals.
2543 struct proc
*p
= current_proc();
2544 struct sigacts
*ps
= p
->p_sigacts
;
2545 user_addr_t catcher
;
2547 int mask
, returnmask
;
2548 struct uthread
* ut
;
2554 * This must be called on master cpu
2556 if (cpu_number() != master_cpu
)
2557 panic("psig not on master");
2562 * Try to grab the signal lock.
2564 if (sig_try_locked(p
) <= 0) {
2569 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2570 mask
= sigmask(signum
);
2571 ut
->uu_siglist
&= ~mask
;
2572 p
->p_siglist
&= ~mask
;
2573 catcher
= ps
->ps_sigact
[signum
];
2575 //LP64: catcher argument is a 64 bit user space handler address
2576 if (KTRPOINT(p
, KTR_PSIG
))
2577 ktrpsig(p
->p_tracep
,
2578 signum
, CAST_DOWN(void *,catcher
), ut
->uu_flag
& UT_SAS_OLDMASK
?
2579 &ut
->uu_oldmask
: &ut
->uu_sigmask
, 0);
2581 if (catcher
== SIG_DFL
) {
2583 * Default catcher, where the default is to kill
2584 * the process. (Other cases were ignored above.)
2586 /* called with signal_lock() held */
2587 sigexit_locked(p
, signum
);
2592 * If we get here, the signal must be caught.
2595 if (catcher
== SIG_IGN
|| (ut
->uu_sigmask
& mask
))
2597 "postsig: processing masked or ignored signal\n");
2600 * Set the new mask value and also defer further
2601 * occurences of this signal.
2603 * Special case: user has done a sigpause. Here the
2604 * current mask is not of interest, but rather the
2605 * mask from before the sigpause is what we want
2606 * restored after the signal processing is completed.
2608 if (ut
->uu_flag
& UT_SAS_OLDMASK
) {
2609 returnmask
= ut
->uu_oldmask
;
2610 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
2613 returnmask
= ut
->uu_sigmask
;
2614 ut
->uu_sigmask
|= ps
->ps_catchmask
[signum
];
2615 if ((ps
->ps_signodefer
& mask
) == 0)
2616 ut
->uu_sigmask
|= mask
;
2617 if ((signum
!= SIGILL
) && (signum
!= SIGTRAP
) && (ps
->ps_sigreset
& mask
)) {
2618 if ((signum
!= SIGCONT
) && (sigprop
[signum
] & SA_IGNORE
))
2619 p
->p_sigignore
|= mask
;
2620 ps
->ps_sigact
[signum
] = SIG_DFL
;
2621 ps
->ps_siginfo
&= ~mask
;
2622 ps
->ps_signodefer
&= ~mask
;
2625 /* Needs to disable to run in user mode */
2626 if (signum
== SIGFPE
) {
2627 thread_enable_fpe(current_thread(), 0);
2629 #endif /* __ppc__ */
2631 if (ps
->ps_sig
!= signum
) {
2637 p
->p_stats
->p_ru
.ru_nsignals
++;
2638 sendsig(p
, catcher
, signum
, returnmask
, code
);
2644 * Force the current process to exit with the specified signal, dumping core
2645 * if appropriate. We bypass the normal tests for masked and caught signals,
2646 * allowing unrecoverable failures to terminate the process without changing
2647 * signal state. Mark the accounting record with the signal termination.
2648 * If dumping core, save the signal number for the debugger. Calls exit and
2651 /* called with signal lock */
2653 sigexit_locked(p
, signum
)
2654 register struct proc
*p
;
2658 sig_lock_to_exit(p
);
2659 p
->p_acflag
|= AXSIG
;
2660 if (sigprop
[signum
] & SA_CORE
) {
2661 p
->p_sigacts
->ps_sig
= signum
;
2663 if (coredump(p
) == 0)
2664 signum
|= WCOREFLAG
;
2668 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
2674 filt_sigattach(struct knote
*kn
)
2676 struct proc
*p
= current_proc();
2677 boolean_t funnel_state
;
2679 kn
->kn_ptr
.p_proc
= p
;
2680 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
2682 /* Take the funnel to protect the proc while adding to the list */
2683 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2684 KNOTE_ATTACH(&p
->p_klist
, kn
);
2685 thread_funnel_set(kernel_flock
, funnel_state
);
2691 filt_sigdetach(struct knote
*kn
)
2693 struct proc
*p
= kn
->kn_ptr
.p_proc
;
2694 boolean_t funnel_state
;
2696 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2697 KNOTE_DETACH(&p
->p_klist
, kn
);
2698 thread_funnel_set(kernel_flock
, funnel_state
);
2702 * signal knotes are shared with proc knotes, so we apply a mask to
2703 * the hint in order to differentiate them from process hints. This
2704 * could be avoided by using a signal-specific knote list, but probably
2705 * isn't worth the trouble.
2708 filt_signal(struct knote
*kn
, long hint
)
2711 if (hint
& NOTE_SIGNAL
) {
2712 hint
&= ~NOTE_SIGNAL
;
2714 if (kn
->kn_id
== (unsigned int)hint
)
2717 return (kn
->kn_data
!= 0);
2722 bsd_ast(thread_t thr_act
)
2724 struct proc
*p
= current_proc();
2725 struct uthread
*ut
= get_bsdthread_info(thr_act
);
2728 boolean_t funnel_state
;
2729 static int bsd_init_done
= 0;
2734 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2736 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
2737 pc
= get_useraddr();
2738 addupc_task(p
, pc
, 1);
2739 p
->p_flag
&= ~P_OWEUPC
;
2742 if (CHECK_SIGNALS(p
, current_thread(), ut
)) {
2743 while ( (signum
= issignal(p
)) )
2746 if (!bsd_init_done
) {
2751 (void) thread_funnel_set(kernel_flock
, FALSE
);
2755 * Follwing routines are called using callout from bsd_hardclock
2756 * so that psignals are called in a thread context and are funneled
2759 psignal_vtalarm(struct proc
*p
)
2761 boolean_t funnel_state
;
2765 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2766 psignal_lock(p
, SIGVTALRM
, 1);
2767 (void) thread_funnel_set(kernel_flock
, FALSE
);
2771 psignal_xcpu(struct proc
*p
)
2773 boolean_t funnel_state
;
2777 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2778 psignal_lock(p
, SIGXCPU
, 1);
2779 (void) thread_funnel_set(kernel_flock
, FALSE
);
2783 psignal_sigprof(struct proc
*p
)
2785 boolean_t funnel_state
;
2789 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2790 psignal_lock(p
, SIGPROF
, 1);
2791 (void) thread_funnel_set(kernel_flock
, FALSE
);
2794 /* ptrace set runnalbe */
2796 pt_setrunnable(struct proc
*p
)
2802 if (p
->p_flag
& P_TRACED
) {
2805 wakeup((caddr_t
)&(p
->sigwait
));
2818 exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
2822 return(bsd_exception(exc
, codes
, 2));
2826 proc_pendingsignals(struct proc
*p
, sigset_t mask
)
2828 struct uthread
* uth
;
2833 /* If the process is in proc exit return no signal info */
2834 if (p
->p_lflag
& P_LPEXIT
)
2837 /* duplicate the signal lock code to enable recursion; as exit
2838 * holds the lock too long. All this code is being reworked
2839 * this is just a workaround for regressions till new code
2843 error
= lockmgr((struct lock__bsd__
*)&p
->signal_lock
[0], (LK_EXCLUSIVE
| LK_CANRECURSE
), 0, (struct proc
*)0);
2847 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
2849 uth
= (struct uthread
*)get_bsdthread_info(th
);
2851 bits
= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
2857 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
2858 bits
|= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
2866 thread_issignal(proc_t p
, thread_t th
, sigset_t mask
)
2868 struct uthread
* uth
;
2872 uth
= (struct uthread
*)get_bsdthread_info(th
);
2874 bits
= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);