2 * Copyright (c) 1995-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1989, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
31 * (c) UNIX System Laboratories, Inc.
32 * All or some portions of this file are derived from material licensed
33 * to the University of California by American Telephone and Telegraph
34 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
35 * the permission of UNIX System Laboratories, Inc.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. 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.7 (Berkeley) 4/18/94
68 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
69 * support for mandatory and extensible security protections. This notice
70 * is included in support of clause 2.2 (b) of the Apple Public License,
74 #define SIGPROP /* include signal properties table */
75 #include <sys/param.h>
76 #include <sys/resourcevar.h>
77 #include <sys/proc_internal.h>
78 #include <sys/kauth.h>
79 #include <sys/systm.h>
80 #include <sys/timeb.h>
81 #include <sys/times.h>
83 #include <sys/file_internal.h>
84 #include <sys/kernel.h>
86 #include <sys/signalvar.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>
112 #include <libkern/OSAtomic.h>
117 * Missing prototypes that Mach should export
121 extern int thread_enable_fpe(thread_t act
, int onoff
);
122 extern thread_t
port_name_to_thread(mach_port_name_t port_name
);
123 extern kern_return_t
get_signalact(task_t
, thread_t
*, int);
124 extern boolean_t
thread_should_abort(thread_t
);
125 extern unsigned int get_useraddr(void);
131 extern void doexception(int exc
, mach_exception_code_t code
,
132 mach_exception_subcode_t sub
);
134 static void stop(proc_t
, proc_t
);
135 int cansignal(proc_t
, kauth_cred_t
, proc_t
, int, int);
136 int killpg1(proc_t
, int, int, int, int);
137 int setsigvec(proc_t
, int, struct __user_sigaction
*);
138 static 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(mach_port_name_t
, mach_port_name_t
, unsigned int, clock_res_t
, void (*)(kern_return_t
));
144 kern_return_t
semaphore_timedwait_trap_internal(mach_port_name_t
, unsigned int, clock_res_t
, void (*)(kern_return_t
));
145 kern_return_t
semaphore_wait_signal_trap_internal(mach_port_name_t
, mach_port_name_t
, void (*)(kern_return_t
));
146 kern_return_t
semaphore_wait_trap_internal(mach_port_name_t
, void (*)(kern_return_t
));
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
};
155 /* structures and fns for killpg1 iterartion callback and filters */
156 struct killpg1_filtargs
{
161 struct killpg1_iterargs
{
168 static int killpg1_filt(proc_t p
, void * arg
);
169 static int killpg1_pgrpfilt(proc_t p
, __unused
void * arg
);
170 static int killpg1_callback(proc_t p
, void * arg
);
172 static int pgsignal_filt(proc_t p
, void * arg
);
173 static int pgsignal_callback(proc_t p
, void * arg
);
174 static kern_return_t
get_signalthread(proc_t
, int, thread_t
*);
177 /* flags for psignal_internal */
178 #define PSIG_LOCKED 0x1
179 #define PSIG_VFORK 0x2
180 #define PSIG_THREAD 0x4
183 static void psignal_internal(proc_t p
, task_t task
, thread_t thread
, int flavor
, int signum
);
186 * NOTE: Source and target may *NOT* overlap! (target is smaller)
189 sigaltstack_64to32(struct user_sigaltstack
*in
, struct sigaltstack
*out
)
191 out
->ss_sp
= CAST_DOWN(void *,in
->ss_sp
);
192 out
->ss_size
= in
->ss_size
;
193 out
->ss_flags
= in
->ss_flags
;
197 * NOTE: Source and target may are permitted to overlap! (source is smaller);
198 * this works because we copy fields in order from the end of the struct to
202 sigaltstack_32to64(struct sigaltstack
*in
, struct user_sigaltstack
*out
)
204 out
->ss_flags
= in
->ss_flags
;
205 out
->ss_size
= in
->ss_size
;
206 out
->ss_sp
= CAST_USER_ADDR_T(in
->ss_sp
);
210 sigaction_64to32(struct user_sigaction
*in
, struct sigaction
*out
)
212 /* This assumes 32 bit __sa_handler is of type sig_t */
213 out
->__sigaction_u
.__sa_handler
= CAST_DOWN(sig_t
,in
->__sigaction_u
.__sa_handler
);
214 out
->sa_mask
= in
->sa_mask
;
215 out
->sa_flags
= in
->sa_flags
;
219 __sigaction_32to64(struct __sigaction
*in
, struct __user_sigaction
*out
)
221 out
->__sigaction_u
.__sa_handler
= CAST_USER_ADDR_T(in
->__sigaction_u
.__sa_handler
);
222 out
->sa_tramp
= CAST_USER_ADDR_T(in
->sa_tramp
);
223 out
->sa_mask
= in
->sa_mask
;
224 out
->sa_flags
= in
->sa_flags
;
229 void ram_printf(int);
231 unsigned int rdebug_proc
=0;
238 #endif /* SIGNAL_DEBUG */
242 signal_setast(thread_t sig_actthread
)
244 act_set_astbsd(sig_actthread
);
248 * Can process p, with ucred uc, send the signal signum to process q?
249 * uc is refcounted by the caller so internal fileds can be used safely
250 * when called with zombie arg, list lock is held
253 cansignal(proc_t p
, kauth_cred_t uc
, proc_t q
, int signum
, int zombie
)
255 kauth_cred_t my_cred
;
256 struct session
* p_sessp
= SESSION_NULL
;
257 struct session
* q_sessp
= SESSION_NULL
;
261 error
= mac_proc_check_signal(p
, q
, signum
);
266 /* you can signal yourself */
270 if (!suser(uc
, NULL
))
271 return (1); /* root can always signal */
275 if (p
->p_pgrp
!= PGRP_NULL
)
276 p_sessp
= p
->p_pgrp
->pg_session
;
277 if (q
->p_pgrp
!= PGRP_NULL
)
278 q_sessp
= q
->p_pgrp
->pg_session
;
280 if (signum
== SIGCONT
&& q_sessp
== p_sessp
) {
283 return (1); /* SIGCONT in session */
290 * If the real or effective UID of the sender matches the real,
291 * effective, or ssaved UID of the target, permit the signal to
295 my_cred
= kauth_cred_proc_ref(q
);
297 my_cred
= proc_ucred(q
);
299 if (uc
->cr_ruid
== my_cred
->cr_ruid
||
300 uc
->cr_ruid
== my_cred
->cr_svuid
||
301 kauth_cred_getuid(uc
) == my_cred
->cr_ruid
||
302 kauth_cred_getuid(uc
) == my_cred
->cr_svuid
||
303 uc
->cr_ruid
== kauth_cred_getuid(my_cred
) ||
304 kauth_cred_getuid(uc
) == kauth_cred_getuid(my_cred
)) {
306 kauth_cred_unref(&my_cred
);
311 kauth_cred_unref(&my_cred
);
325 sigaction(proc_t p
, struct sigaction_args
*uap
, __unused register_t
*retval
)
327 struct user_sigaction vec
;
328 struct __user_sigaction __vec
;
330 struct user_sigaction
*sa
= &vec
;
331 struct sigacts
*ps
= p
->p_sigacts
;
336 signum
= uap
->signum
;
337 if (signum
<= 0 || signum
>= NSIG
||
338 signum
== SIGKILL
|| signum
== SIGSTOP
)
342 sa
->sa_handler
= ps
->ps_sigact
[signum
];
343 sa
->sa_mask
= ps
->ps_catchmask
[signum
];
344 bit
= sigmask(signum
);
346 if ((ps
->ps_sigonstack
& bit
) != 0)
347 sa
->sa_flags
|= SA_ONSTACK
;
348 if ((ps
->ps_sigintr
& bit
) == 0)
349 sa
->sa_flags
|= SA_RESTART
;
350 if (ps
->ps_siginfo
& bit
)
351 sa
->sa_flags
|= SA_SIGINFO
;
352 if (ps
->ps_signodefer
& bit
)
353 sa
->sa_flags
|= SA_NODEFER
;
354 if (ps
->ps_64regset
& bit
)
355 sa
->sa_flags
|= SA_64REGSET
;
356 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDSTOP
))
357 sa
->sa_flags
|= SA_NOCLDSTOP
;
358 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDWAIT
))
359 sa
->sa_flags
|= SA_NOCLDWAIT
;
361 if (IS_64BIT_PROCESS(p
)) {
362 error
= copyout(sa
, uap
->osa
, sizeof(struct user_sigaction
));
364 struct sigaction vec32
;
365 sigaction_64to32(sa
, &vec32
);
366 error
= copyout(&vec32
, uap
->osa
, sizeof(struct sigaction
));
372 if (IS_64BIT_PROCESS(p
)) {
373 error
= copyin(uap
->nsa
, &__vec
, sizeof(struct __user_sigaction
));
375 struct __sigaction __vec32
;
376 error
= copyin(uap
->nsa
, &__vec32
, sizeof(struct __sigaction
));
377 __sigaction_32to64(&__vec32
, &__vec
);
381 __vec
.sa_flags
&= SA_USERSPACE_MASK
; /* Only pass on valid sa_flags */
382 error
= setsigvec(p
, signum
, &__vec
);
387 /* Routines to manipulate bits on all threads */
389 clear_procsiglist(proc_t p
, int bit
)
391 struct uthread
* uth
;
395 proc_signalstart(p
, 1);
397 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
398 thact
= p
->p_vforkact
;
399 uth
= (struct uthread
*)get_bsdthread_info(thact
);
401 uth
->uu_siglist
&= ~bit
;
403 proc_signalend(p
, 1);
408 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
409 uth
->uu_siglist
&= ~bit
;
412 proc_signalend(p
, 1);
420 unblock_procsigmask(proc_t p
, int bit
)
422 struct uthread
* uth
;
426 proc_signalstart(p
, 1);
428 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
429 thact
= p
->p_vforkact
;
430 uth
= (struct uthread
*)get_bsdthread_info(thact
);
432 uth
->uu_sigmask
&= ~bit
;
434 p
->p_sigmask
&= ~bit
;
435 proc_signalend(p
, 1);
439 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
440 uth
->uu_sigmask
&= ~bit
;
442 p
->p_sigmask
&= ~bit
;
444 proc_signalend(p
, 1);
450 block_procsigmask(proc_t p
, int bit
)
452 struct uthread
* uth
;
456 proc_signalstart(p
, 1);
458 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
459 thact
= p
->p_vforkact
;
460 uth
= (struct uthread
*)get_bsdthread_info(thact
);
462 uth
->uu_sigmask
|= bit
;
465 proc_signalend(p
, 1);
469 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
470 uth
->uu_sigmask
|= bit
;
474 proc_signalend(p
, 1);
480 set_procsigmask(proc_t p
, int bit
)
482 struct uthread
* uth
;
486 proc_signalstart(p
, 1);
488 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
489 thact
= p
->p_vforkact
;
490 uth
= (struct uthread
*)get_bsdthread_info(thact
);
492 uth
->uu_sigmask
= bit
;
495 proc_signalend(p
, 1);
499 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
500 uth
->uu_sigmask
= bit
;
503 proc_signalend(p
, 1);
509 /* XXX should be static? */
511 setsigvec(proc_t p
, int signum
, struct __user_sigaction
*sa
)
513 struct sigacts
*ps
= p
->p_sigacts
;
516 if ((signum
== SIGKILL
|| signum
== SIGSTOP
) &&
517 sa
->sa_handler
!= SIG_DFL
)
519 bit
= sigmask(signum
);
521 * Change setting atomically.
523 ps
->ps_sigact
[signum
] = sa
->sa_handler
;
524 ps
->ps_trampact
[signum
] = sa
->sa_tramp
;
525 ps
->ps_catchmask
[signum
] = sa
->sa_mask
&~ sigcantmask
;
526 if (sa
->sa_flags
& SA_SIGINFO
)
527 ps
->ps_siginfo
|= bit
;
529 ps
->ps_siginfo
&= ~bit
;
530 if (sa
->sa_flags
& SA_64REGSET
)
531 ps
->ps_64regset
|= bit
;
533 ps
->ps_64regset
&= ~bit
;
534 if ((sa
->sa_flags
& SA_RESTART
) == 0)
535 ps
->ps_sigintr
|= bit
;
537 ps
->ps_sigintr
&= ~bit
;
538 if (sa
->sa_flags
& SA_ONSTACK
)
539 ps
->ps_sigonstack
|= bit
;
541 ps
->ps_sigonstack
&= ~bit
;
542 if (sa
->sa_flags
& SA_USERTRAMP
)
543 ps
->ps_usertramp
|= bit
;
545 ps
->ps_usertramp
&= ~bit
;
546 if (sa
->sa_flags
& SA_RESETHAND
)
547 ps
->ps_sigreset
|= bit
;
549 ps
->ps_sigreset
&= ~bit
;
550 if (sa
->sa_flags
& SA_NODEFER
)
551 ps
->ps_signodefer
|= bit
;
553 ps
->ps_signodefer
&= ~bit
;
554 if (signum
== SIGCHLD
) {
555 if (sa
->sa_flags
& SA_NOCLDSTOP
)
556 OSBitOrAtomic(P_NOCLDSTOP
, (UInt32
*)&p
->p_flag
);
558 OSBitAndAtomic(~((uint32_t)P_NOCLDSTOP
), (UInt32
*)&p
->p_flag
);
559 if ((sa
->sa_flags
& SA_NOCLDWAIT
) || (sa
->sa_handler
== SIG_IGN
))
560 OSBitOrAtomic(P_NOCLDWAIT
, (UInt32
*)&p
->p_flag
);
562 OSBitAndAtomic(~((uint32_t)P_NOCLDWAIT
), (UInt32
*)&p
->p_flag
);
566 if (signum
== SIGFPE
) {
567 if (sa
->sa_handler
== SIG_DFL
|| sa
->sa_handler
== SIG_IGN
)
568 thread_enable_fpe(current_thread(), 0);
570 thread_enable_fpe(current_thread(), 1);
574 * Set bit in p_sigignore for signals that are set to SIG_IGN,
575 * and for signals set to SIG_DFL where the default is to ignore.
576 * However, don't put SIGCONT in p_sigignore,
577 * as we have to restart the process.
579 if (sa
->sa_handler
== SIG_IGN
||
580 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
582 clear_procsiglist(p
, bit
);
583 if (signum
!= SIGCONT
)
584 p
->p_sigignore
|= bit
; /* easier in psignal */
585 p
->p_sigcatch
&= ~bit
;
587 p
->p_sigignore
&= ~bit
;
588 if (sa
->sa_handler
== SIG_DFL
)
589 p
->p_sigcatch
&= ~bit
;
591 p
->p_sigcatch
|= bit
;
597 * Initialize signal state for process 0;
598 * set to ignore signals that are ignored by default.
605 for (i
= 0; i
< NSIG
; i
++)
606 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
607 p
->p_sigignore
|= sigmask(i
);
611 * Reset signals for an exec of the specified process.
614 execsigs(proc_t p
, thread_t thread
)
616 struct sigacts
*ps
= p
->p_sigacts
;
620 ut
= (struct uthread
*)get_bsdthread_info(thread
);
622 * Reset caught signals. Held signals remain held
623 * through p_sigmask (unless they were caught,
624 * and are now ignored by default).
626 while (p
->p_sigcatch
) {
627 nc
= ffs((long)p
->p_sigcatch
);
629 p
->p_sigcatch
&= ~mask
;
630 if (sigprop
[nc
] & SA_IGNORE
) {
632 p
->p_sigignore
|= mask
;
634 ut
->uu_siglist
&= ~mask
;
636 clear_procsiglist(p
, mask
);
638 ps
->ps_sigact
[nc
] = SIG_DFL
;
641 * Reset stack state to the user stack.
642 * Clear set of signals caught on the signal stack.
645 ut
->uu_sigstk
.ss_flags
= SA_DISABLE
;
646 ut
->uu_sigstk
.ss_size
= 0;
647 ut
->uu_sigstk
.ss_sp
= USER_ADDR_NULL
;
648 ut
->uu_flag
&= ~UT_ALTSTACK
;
650 ps
->ps_sigonstack
= 0;
654 * Manipulate signal mask.
655 * Note that we receive new mask, not pointer,
656 * and return old mask as return value;
657 * the library stub does the rest.
660 sigprocmask(proc_t p
, struct sigprocmask_args
*uap
, __unused register_t
*retval
)
663 sigset_t oldmask
, nmask
;
664 user_addr_t omask
= uap
->omask
;
667 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
668 oldmask
= ut
->uu_sigmask
;
670 if (uap
->mask
== USER_ADDR_NULL
) {
671 /* just want old mask */
674 error
= copyin(uap
->mask
, &nmask
, sizeof(sigset_t
));
680 block_procsigmask(p
, (nmask
& ~sigcantmask
));
681 signal_setast(current_thread());
685 unblock_procsigmask(p
, (nmask
& ~sigcantmask
));
686 signal_setast(current_thread());
690 set_procsigmask(p
, (nmask
& ~sigcantmask
));
691 signal_setast(current_thread());
699 if (!error
&& omask
!= USER_ADDR_NULL
)
700 copyout(&oldmask
, omask
, sizeof(sigset_t
));
705 sigpending(__unused proc_t p
, struct sigpending_args
*uap
, __unused register_t
*retval
)
710 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
711 pendlist
= ut
->uu_siglist
;
714 copyout(&pendlist
, uap
->osv
, sizeof(sigset_t
));
719 * Suspend process until signal, providing mask to be set
720 * in the meantime. Note nonstandard calling convention:
721 * libc stub passes mask, not pointer, to save a copyin.
725 sigcontinue(__unused
int error
)
727 // struct uthread *ut = get_bsdthread_info(current_thread());
728 unix_syscall_return(EINTR
);
732 sigsuspend(proc_t p
, struct sigsuspend_args
*uap
, register_t
*retval
)
734 __pthread_testcancel(1);
735 return(sigsuspend_nocancel(p
, (struct sigsuspend_nocancel_args
*)uap
, retval
));
739 sigsuspend_nocancel(proc_t p
, struct sigsuspend_nocancel_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(__unused proc_t p
,
763 __unused
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 */
771 uth
->uu_flag
|= (UT_NO_SIGMASK
| UT_CANCELDISABLE
);
778 __pthread_testcancel(int presyscall
)
781 thread_t self
= current_thread();
782 struct uthread
* uthread
;
784 uthread
= (struct uthread
*)get_bsdthread_info(self
);
787 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
789 if ((uthread
->uu_flag
& (UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
790 if(presyscall
!= 0) {
791 unix_syscall_return(EINTR
);
794 thread_abort_safely(self
);
801 __pthread_markcancel(__unused proc_t p
,
802 struct __pthread_markcancel_args
*uap
, __unused register_t
*retval
)
804 thread_act_t target_act
;
808 target_act
= (thread_act_t
)port_name_to_thread(uap
->thread_port
);
810 if (target_act
== THR_ACT_NULL
)
813 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
815 /* if the thread is in vfork do not cancel */
816 if ((uth
->uu_flag
& (UT_VFORK
| UT_CANCEL
| UT_CANCELED
)) == 0) {
817 uth
->uu_flag
|= (UT_CANCEL
| UT_NO_SIGMASK
);
818 if (((uth
->uu_flag
& UT_NOTCANCELPT
) == 0)
819 && ((uth
->uu_flag
& UT_CANCELDISABLE
) == 0))
820 thread_abort_safely(target_act
);
823 thread_deallocate(target_act
);
827 /* if action =0 ; return the cancellation state ,
828 * if marked for cancellation, make the thread canceled
829 * if action = 1 ; Enable the cancel handling
830 * if action = 2; Disable the cancel handling
833 __pthread_canceled(__unused proc_t p
,
834 struct __pthread_canceled_args
*uap
, __unused register_t
*retval
)
838 int action
= uap
->action
;
840 thread
= current_thread();
841 uth
= (struct uthread
*)get_bsdthread_info(thread
);
845 uth
->uu_flag
&= ~UT_CANCELDISABLE
;
848 uth
->uu_flag
|= UT_CANCELDISABLE
;
852 /* if the thread is in vfork do not cancel */
853 if((uth
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
854 uth
->uu_flag
&= ~UT_CANCEL
;
855 uth
->uu_flag
|= (UT_CANCELED
| UT_NO_SIGMASK
);
864 __posix_sem_syscall_return(kern_return_t kern_result
)
868 if (kern_result
== KERN_SUCCESS
)
870 else if (kern_result
== KERN_ABORTED
)
872 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
876 unix_syscall_return(error
);
877 /* does not return */
887 __semwait_signal(__unused proc_t p
, struct __semwait_signal_args
*uap
,
890 __pthread_testcancel(0);
891 return(__semwait_signal_nocancel(p
, (struct __semwait_signal_nocancel_args
*)uap
, retval
));
895 __semwait_signal_nocancel(__unused proc_t p
, struct __semwait_signal_nocancel_args
*uap
,
896 __unused register_t
*retval
)
899 kern_return_t kern_result
;
900 mach_timespec_t then
;
906 then
.tv_sec
= uap
->tv_sec
;
907 then
.tv_nsec
= uap
->tv_nsec
;
910 then
.tv_sec
= uap
->tv_sec
- now
.tv_sec
;
911 then
.tv_nsec
= uap
->tv_nsec
- now
.tv_nsec
;
912 if (then
.tv_nsec
< 0) {
913 then
.tv_nsec
+= NSEC_PER_SEC
;
916 /* if time has elapsed, set time to null timepsec to bailout rightaway */
917 if ((int)then
.tv_sec
< 0) {
923 if (uap
->mutex_sem
== 0)
924 kern_result
= semaphore_timedwait_trap_internal((mach_port_name_t
)uap
->cond_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
926 kern_result
= semaphore_timedwait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
930 if (uap
->mutex_sem
== 0)
931 kern_result
= semaphore_wait_trap_internal(uap
->cond_sem
, __posix_sem_syscall_return
);
934 kern_result
= semaphore_wait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, __posix_sem_syscall_return
);
937 if (kern_result
== KERN_SUCCESS
)
939 else if (kern_result
== KERN_ABORTED
)
941 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
948 __pthread_kill(__unused proc_t p
, struct __pthread_kill_args
*uap
,
949 __unused register_t
*retval
)
953 int signum
= uap
->sig
;
956 target_act
= (thread_t
)port_name_to_thread(uap
->thread_port
);
958 if (target_act
== THREAD_NULL
)
960 if ((u_int
)signum
>= NSIG
) {
965 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
967 if (uth
->uu_flag
& UT_NO_SIGMASK
) {
973 psignal_uthread(target_act
, signum
);
975 thread_deallocate(target_act
);
981 __pthread_sigmask(__unused proc_t p
, struct __pthread_sigmask_args
*uap
,
982 __unused register_t
*retval
)
984 user_addr_t set
= uap
->set
;
985 user_addr_t oset
= uap
->oset
;
991 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
992 oldset
= ut
->uu_sigmask
;
994 if (set
== USER_ADDR_NULL
) {
995 /* need only old mask */
999 error
= copyin(set
, &nset
, sizeof(sigset_t
));
1005 ut
->uu_sigmask
|= (nset
& ~sigcantmask
);
1009 ut
->uu_sigmask
&= ~(nset
);
1010 signal_setast(current_thread());
1014 ut
->uu_sigmask
= (nset
& ~sigcantmask
);
1015 signal_setast(current_thread());
1023 if (!error
&& oset
!= USER_ADDR_NULL
)
1024 copyout(&oldset
, oset
, sizeof(sigset_t
));
1030 * Returns: 0 Success
1036 __sigwait(proc_t p
, struct __sigwait_args
*uap
, register_t
*retval
)
1038 __pthread_testcancel(1);
1039 return(__sigwait_nocancel(p
, (struct __sigwait_nocancel_args
*)uap
, retval
));
1043 __sigwait_nocancel(proc_t p
, struct __sigwait_nocancel_args
*uap
, __unused register_t
*retval
)
1046 struct uthread
*uth
;
1053 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1055 if (uap
->set
== USER_ADDR_NULL
)
1058 error
= copyin(uap
->set
, &mask
, sizeof(sigset_t
));
1062 siglist
= (mask
& ~sigcantmask
);
1068 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
1072 proc_signalstart(p
, 1);
1073 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1074 if ( (sigw
= uth
->uu_siglist
& siglist
) ) {
1078 proc_signalend(p
, 1);
1082 /* The signal was pending on a thread */
1086 * When returning from sigwait, we want
1087 * the old mask to be restored after the
1088 * signal handler has finished. Thus, we
1089 * save it here and mark the sigacts structure
1092 uth
= ut
; /* wait for it to be delivered to us */
1093 ut
->uu_oldmask
= ut
->uu_sigmask
;
1094 ut
->uu_flag
|= UT_SAS_OLDMASK
;
1095 if (siglist
== (sigset_t
)0) {
1099 /* SIGKILL and SIGSTOP are not maskable as well */
1100 ut
->uu_sigmask
= ~(siglist
|sigcantmask
);
1101 ut
->uu_sigwait
= siglist
;
1103 /* No Continuations for now */
1104 error
= msleep((caddr_t
)&ut
->uu_sigwait
, &p
->p_mlock
, PPAUSE
|PCATCH
, "pause", 0);
1106 if ((error
== EINTR
) || (error
== ERESTART
))
1109 sigw
= (ut
->uu_sigwait
& siglist
);
1110 ut
->uu_sigmask
= ut
->uu_oldmask
;
1112 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
1116 signum
= ffs((unsigned int)sigw
);
1118 panic("sigwait with no signal wakeup");
1119 /* Clear the pending signal in the thread it was delivered */
1120 uth
->uu_siglist
&= ~(sigmask(signum
));
1122 if (uap
->sig
!= USER_ADDR_NULL
)
1123 error
= copyout(&signum
, uap
->sig
, sizeof(int));
1132 sigaltstack(__unused proc_t p
, struct sigaltstack_args
*uap
, __unused register_t
*retval
)
1134 struct user_sigaltstack ss
;
1135 struct user_sigaltstack
*pstk
;
1137 struct uthread
*uth
;
1140 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
1142 pstk
= &uth
->uu_sigstk
;
1143 if ((uth
->uu_flag
& UT_ALTSTACK
) == 0)
1144 uth
->uu_sigstk
.ss_flags
|= SA_DISABLE
;
1145 onstack
= pstk
->ss_flags
& SA_ONSTACK
;
1147 if (IS_64BIT_PROCESS(p
)) {
1148 error
= copyout(pstk
, uap
->oss
, sizeof(struct user_sigaltstack
));
1150 struct sigaltstack ss32
;
1151 sigaltstack_64to32(pstk
, &ss32
);
1152 error
= copyout(&ss32
, uap
->oss
, sizeof(struct sigaltstack
));
1157 if (uap
->nss
== USER_ADDR_NULL
)
1159 if (IS_64BIT_PROCESS(p
)) {
1160 error
= copyin(uap
->nss
, &ss
, sizeof(struct user_sigaltstack
));
1162 struct sigaltstack ss32
;
1163 error
= copyin(uap
->nss
, &ss32
, sizeof(struct sigaltstack
));
1164 sigaltstack_32to64(&ss32
,&ss
);
1168 if ((ss
.ss_flags
& ~SA_DISABLE
) != 0) {
1172 if (ss
.ss_flags
& SA_DISABLE
) {
1173 /* if we are here we are not in the signal handler ;so no need to check */
1174 if (uth
->uu_sigstk
.ss_flags
& SA_ONSTACK
)
1176 uth
->uu_flag
&= ~UT_ALTSTACK
;
1177 uth
->uu_sigstk
.ss_flags
= ss
.ss_flags
;
1182 /* The older stacksize was 8K, enforce that one so no compat problems */
1183 #define OLDMINSIGSTKSZ 8*1024
1184 if (ss
.ss_size
< OLDMINSIGSTKSZ
)
1186 uth
->uu_flag
|= UT_ALTSTACK
;
1192 kill(proc_t cp
, struct kill_args
*uap
, __unused register_t
*retval
)
1195 kauth_cred_t uc
= kauth_cred_get();
1196 int posix
= uap
->posix
; /* !0 if posix behaviour desired */
1198 AUDIT_ARG(pid
, uap
->pid
);
1199 AUDIT_ARG(signum
, uap
->signum
);
1201 if ((u_int
)uap
->signum
>= NSIG
)
1204 /* kill single process */
1205 if ((p
= proc_find(uap
->pid
)) == NULL
) {
1206 if ((p
= pzfind(uap
->pid
)) != NULL
) {
1208 * IEEE Std 1003.1-2001: return success
1209 * when killing a zombie.
1215 AUDIT_ARG(process
, p
);
1216 if (!cansignal(cp
, uc
, p
, uap
->signum
, 0)) {
1221 psignal(p
, uap
->signum
);
1226 case -1: /* broadcast signal */
1227 return (killpg1(cp
, uap
->signum
, 0, 1, posix
));
1228 case 0: /* signal own process group */
1229 return (killpg1(cp
, uap
->signum
, 0, 0, posix
));
1230 default: /* negative explicit process group */
1231 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0, posix
));
1237 killpg1_filt(proc_t p
, void * arg
)
1239 struct killpg1_filtargs
* kfargp
= (struct killpg1_filtargs
*)arg
;
1240 proc_t cp
= kfargp
->cp
;
1241 int posix
= kfargp
->posix
;
1244 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1245 (!posix
&& p
== cp
))
1253 killpg1_pgrpfilt(proc_t p
, __unused
void * arg
)
1255 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1256 (p
->p_stat
== SZOMB
))
1265 killpg1_callback(proc_t p
, void * arg
)
1267 struct killpg1_iterargs
* kargp
= (struct killpg1_iterargs
*)arg
;
1268 proc_t cp
= kargp
->cp
;
1269 kauth_cred_t uc
= kargp
->uc
; /* refcounted by the caller safe to use internal fields */
1270 int signum
= kargp
->signum
;
1271 int * nfoundp
= kargp
->nfoundp
;
1275 if (cansignal(cp
, uc
, p
, signum
, 0) == 0)
1276 return(PROC_RETURNED
);
1278 if (nfoundp
!= NULL
) {
1285 return(PROC_RETURNED
);
1290 * Common code for kill process group/broadcast kill.
1291 * cp is calling process.
1294 killpg1(proc_t cp
, int signum
, int pgid
, int all
, int posix
)
1300 struct killpg1_iterargs karg
;
1301 struct killpg1_filtargs kfarg
;
1304 uc
= kauth_cred_proc_ref(cp
);
1309 kfarg
.posix
= posix
;
1314 karg
.nfoundp
= &nfound
;
1315 karg
.signum
= signum
;
1317 proc_iterate(PROC_ALLPROCLIST
, killpg1_callback
, &karg
, killpg1_filt
, (void *)&kfarg
);
1319 * Signalling zombies is a no-op, but they must be counted
1320 * among those processes which have been signalled, since
1321 * they are still members of the process group.
1326 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1327 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1328 (!posix
&& p
== cp
) || !cansignal(cp
, uc
, p
, signum
, 1))
1338 * zero pgid means send to my process group.
1340 pgrp
= proc_pgrp(cp
);
1342 pgrp
= pgfind(pgid
);
1349 karg
.nfoundp
= &nfound
;
1351 karg
.signum
= signum
;
1355 /* PGRP_DROPREF drops the pgrp refernce */
1356 pgrp_iterate(pgrp
, PGRP_BLOCKITERATE
| PGRP_DROPREF
, killpg1_callback
, &karg
,
1357 killpg1_pgrpfilt
, NULL
);
1359 error
= (nfound
? 0 : (posix
? EPERM
: ESRCH
));
1361 kauth_cred_unref(&uc
);
1367 * Send a signal to a process group.
1370 gsignal(int pgid
, int signum
)
1374 if (pgid
&& (pgrp
= pgfind(pgid
))) {
1375 pgsignal(pgrp
, signum
, 0);
1381 * Send a signal to a process group. If checkctty is 1,
1382 * limit to members which have a controlling terminal.
1386 pgsignal_filt(proc_t p
, void * arg
)
1388 int checkctty
= (int)arg
;
1390 if ((checkctty
== 0) || p
->p_flag
& P_CONTROLT
)
1398 pgsignal_callback(proc_t p
, void * arg
)
1400 int signum
= (int)arg
;
1403 return(PROC_RETURNED
);
1408 pgsignal(struct pgrp
*pgrp
, int signum
, int checkctty
)
1410 if (pgrp
!= PGRP_NULL
) {
1411 pgrp_iterate(pgrp
, PGRP_BLOCKITERATE
, pgsignal_callback
, (void *)signum
, pgsignal_filt
, (void *)checkctty
);
1417 tty_pgsignal(struct tty
*tp
, int signum
, int checkctty
)
1422 if (pg
!= PGRP_NULL
) {
1423 pgrp_iterate(pg
, PGRP_BLOCKITERATE
, pgsignal_callback
, (void *)signum
, pgsignal_filt
, (void *)checkctty
);
1428 * Send a signal caused by a trap to a specific thread.
1431 threadsignal(thread_t sig_actthread
, int signum
, mach_exception_code_t code
)
1433 struct uthread
*uth
;
1434 struct task
* sig_task
;
1438 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1441 mask
= sigmask(signum
);
1442 if ((mask
& threadmask
) == 0)
1444 sig_task
= get_threadtask(sig_actthread
);
1445 p
= (proc_t
)(get_bsdtask_info(sig_task
));
1447 uth
= get_bsdthread_info(sig_actthread
);
1448 if (uth
&& (uth
->uu_flag
& UT_VFORK
))
1452 if (!(p
->p_lflag
& P_LTRACED
) && (p
->p_sigignore
& mask
)) {
1457 uth
->uu_siglist
|= mask
;
1458 uth
->uu_code
= code
;
1461 /* mark on process as well */
1462 signal_setast(sig_actthread
);
1465 static kern_return_t
1466 get_signalthread(proc_t p
, int signum
, thread_t
* thr
)
1468 struct uthread
*uth
;
1469 sigset_t mask
= sigmask(signum
);
1470 thread_t sig_thread
;
1471 struct task
* sig_task
= p
->task
;
1476 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
1477 sig_thread
= p
->p_vforkact
;
1478 kret
= check_actforsig(sig_task
, sig_thread
, 1);
1479 if (kret
== KERN_SUCCESS
) {
1481 return(KERN_SUCCESS
);
1483 return(KERN_FAILURE
);
1487 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1488 if(((uth
->uu_flag
& UT_NO_SIGMASK
)== 0) &&
1489 (((uth
->uu_sigmask
& mask
) == 0) || (uth
->uu_sigwait
& mask
))) {
1490 if (check_actforsig(p
->task
, uth
->uu_context
.vc_thread
, 1) == KERN_SUCCESS
) {
1491 *thr
= uth
->uu_context
.vc_thread
;
1493 return(KERN_SUCCESS
);
1498 if (get_signalact(p
->task
, thr
, 1) == KERN_SUCCESS
) {
1499 return(KERN_SUCCESS
);
1502 return(KERN_FAILURE
);
1506 * Send the signal to the process. If the signal has an action, the action
1507 * is usually performed by the target process rather than the caller; we add
1508 * the signal to the set of pending signals for the process.
1511 * o When a stop signal is sent to a sleeping process that takes the
1512 * default action, the process is stopped without awakening it.
1513 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1514 * regardless of the signal action (eg, blocked or ignored).
1516 * Other ignored signals are discarded immediately.
1519 psignal_internal(proc_t p
, task_t task
, thread_t thread
, int flavor
, int signum
)
1522 sig_t action
= NULL
;
1524 thread_t sig_thread
;
1525 register task_t sig_task
;
1527 struct uthread
*uth
;
1531 kauth_cred_t my_cred
;
1533 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1534 panic("psignal signal number");
1535 mask
= sigmask(signum
);
1536 prop
= sigprop
[signum
];
1539 if(rdebug_proc
&& (p
!= PROC_NULL
) && (p
== rdebug_proc
)) {
1542 #endif /* SIGNAL_DEBUG */
1545 * We will need the task pointer later. Grab it now to
1546 * check for a zombie process. Also don't send signals
1547 * to kernel internal tasks.
1549 if (flavor
& PSIG_VFORK
) {
1551 sig_thread
= thread
;
1553 } else if (flavor
& PSIG_THREAD
) {
1554 sig_task
= get_threadtask(thread
);
1555 sig_thread
= thread
;
1556 sig_proc
= (proc_t
)get_bsdtask_info(sig_task
);
1560 sig_thread
= (struct thread
*)0;
1562 if (((sig_task
== TASK_NULL
) || is_kerneltask(sig_task
))) {
1567 * do not send signals to the process that has the thread
1568 * doing a reboot(). Not doing so will mark that thread aborted
1569 * and can cause IO failures wich will cause data loss.
1571 if (ISSET(sig_proc
->p_flag
, P_REBOOT
)) {
1575 if( (flavor
& (PSIG_VFORK
| PSIG_THREAD
)) == 0) {
1576 proc_knote(sig_proc
, NOTE_SIGNAL
| signum
);
1580 if ((flavor
& PSIG_LOCKED
)== 0)
1581 proc_signalstart(sig_proc
, 0);
1584 * Deliver the signal to the first thread in the task. This
1585 * allows single threaded applications which use signals to
1586 * be able to be linked with multithreaded libraries. We have
1587 * an implicit reference to the current thread, but need
1588 * an explicit one otherwise. The thread reference keeps
1589 * the corresponding task data structures around too. This
1590 * reference is released by thread_deallocate.
1594 if (((flavor
& PSIG_VFORK
) == 0) && ((sig_proc
->p_lflag
& P_LTRACED
) == 0) && (sig_proc
->p_sigignore
& mask
)) {
1595 DTRACE_PROC3(signal__discard
, thread_t
, sig_thread
, proc_t
, sig_proc
, int, signum
);
1599 if (flavor
& PSIG_VFORK
) {
1601 act_set_astbsd(sig_thread
);
1602 kret
= KERN_SUCCESS
;
1603 } else if (flavor
& PSIG_THREAD
) {
1604 /* If successful return with ast set */
1605 kret
= check_actforsig(sig_task
, sig_thread
, 1);
1607 /* If successful return with ast set */
1608 kret
= get_signalthread(sig_proc
, signum
, &sig_thread
);
1610 if (kret
!= KERN_SUCCESS
) {
1613 #endif /* SIGNAL_DEBUG */
1618 uth
= get_bsdthread_info(sig_thread
);
1621 * If proc is traced, always give parent a chance.
1624 if ((flavor
& PSIG_VFORK
) == 0) {
1625 if (sig_proc
->p_lflag
& P_LTRACED
)
1629 * If the signal is being ignored,
1630 * then we forget about it immediately.
1631 * (Note: we don't set SIGCONT in p_sigignore,
1632 * and if it is set to SIG_IGN,
1633 * action will be SIG_DFL here.)
1635 if (sig_proc
->p_sigignore
& mask
)
1637 if (uth
->uu_sigwait
& mask
)
1638 action
= KERN_SIG_WAIT
;
1639 else if (uth
->uu_sigmask
& mask
)
1640 action
= KERN_SIG_HOLD
;
1641 else if (sig_proc
->p_sigcatch
& mask
)
1642 action
= KERN_SIG_CATCH
;
1649 proc_lock(sig_proc
);
1651 if (sig_proc
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1652 (sig_proc
->p_lflag
& P_LTRACED
) == 0)
1653 sig_proc
->p_nice
= NZERO
;
1656 uth
->uu_siglist
&= ~stopsigmask
;
1658 if (prop
& SA_STOP
) {
1661 * If sending a tty stop signal to a member of an orphaned
1662 * process group, discard the signal here if the action
1663 * is default; don't stop the process below if sleeping,
1664 * and don't clear any pending SIGCONT.
1666 proc_unlock(sig_proc
);
1667 pg
= proc_pgrp(sig_proc
);
1668 if (prop
& SA_TTYSTOP
&& pg
->pg_jobc
== 0 &&
1669 action
== SIG_DFL
) {
1674 proc_lock(sig_proc
);
1675 uth
->uu_siglist
&= ~contsigmask
;
1678 uth
->uu_siglist
|= mask
;
1680 * Repost AST incase sigthread has processed
1681 * ast and missed signal post.
1683 if (action
== KERN_SIG_CATCH
)
1684 act_set_astbsd(sig_thread
);
1688 * Defer further processing for signals which are held,
1689 * except that stopped processes must be continued by SIGCONT.
1691 /* vfork will not go thru as action is SIG_DFL */
1692 if ((action
== KERN_SIG_HOLD
) && ((prop
& SA_CONT
) == 0 || sig_proc
->p_stat
!= SSTOP
)) {
1693 proc_unlock(sig_proc
);
1697 * SIGKILL priority twiddling moved here from above because
1698 * it needs sig_thread. Could merge it into large switch
1699 * below if we didn't care about priority for tracing
1700 * as SIGKILL's action is always SIG_DFL.
1702 if ((signum
== SIGKILL
) && (sig_proc
->p_nice
> NZERO
)) {
1703 sig_proc
->p_nice
= NZERO
;
1707 * Process is traced - wake it up (if not already
1708 * stopped) so that it can discover the signal in
1709 * issig() and stop for the parent.
1711 if (sig_proc
->p_lflag
& P_LTRACED
) {
1712 if (sig_proc
->p_stat
!= SSTOP
)
1715 proc_unlock(sig_proc
);
1719 if ((flavor
& PSIG_VFORK
) != 0)
1722 if (action
== KERN_SIG_WAIT
) {
1723 uth
->uu_sigwait
= mask
;
1724 uth
->uu_siglist
&= ~mask
;
1725 wakeup(&uth
->uu_sigwait
);
1726 /* if it is SIGCONT resume whole process */
1727 if (prop
& SA_CONT
) {
1728 OSBitOrAtomic(P_CONTINUED
, (UInt32
*)&sig_proc
->p_flag
);
1729 sig_proc
->p_contproc
= current_proc()->p_pid
;
1731 proc_unlock(sig_proc
);
1732 (void) task_resume(sig_task
);
1735 proc_unlock(sig_proc
);
1739 if (action
!= SIG_DFL
) {
1741 * User wants to catch the signal.
1742 * Wake up the thread, but don't un-suspend it
1743 * (except for SIGCONT).
1745 if (prop
& SA_CONT
) {
1746 OSBitOrAtomic(P_CONTINUED
, (UInt32
*)&sig_proc
->p_flag
);
1747 proc_unlock(sig_proc
);
1748 (void) task_resume(sig_task
);
1749 proc_lock(sig_proc
);
1750 sig_proc
->p_stat
= SRUN
;
1751 } else if (sig_proc
->p_stat
== SSTOP
) {
1752 proc_unlock(sig_proc
);
1756 * Fill out siginfo structure information to pass to the
1757 * signalled process/thread sigaction handler, when it
1758 * wakes up. si_code is 0 because this is an ordinary
1759 * signal, not a SIGCHLD, and so si_status is the signal
1760 * number itself, instead of the child process exit status.
1761 * We shift this left because it will be shifted right before
1762 * it is passed to user space. kind of ugly to use W_EXITCODE
1763 * this way, but it beats defining a new macro.
1765 * Note: Avoid the SIGCHLD recursion case!
1767 if (signum
!= SIGCHLD
) {
1768 proc_unlock(sig_proc
);
1769 r_uid
= kauth_getruid();
1770 proc_lock(sig_proc
);
1772 sig_proc
->si_pid
= current_proc()->p_pid
;
1773 sig_proc
->si_status
= W_EXITCODE(signum
, 0);
1774 sig_proc
->si_uid
= r_uid
;
1775 sig_proc
->si_code
= 0;
1780 /* Default action - varies */
1781 if (mask
& stopsigmask
) {
1783 * These are the signals which by default
1786 * Don't clog system with children of init
1787 * stopped from the keyboard.
1789 if (!(prop
& SA_STOP
) && sig_proc
->p_pptr
== initproc
) {
1790 proc_unlock(sig_proc
);
1791 psignal_locked(sig_proc
, SIGKILL
);
1792 proc_lock(sig_proc
);
1793 uth
->uu_siglist
&= ~mask
;
1794 proc_unlock(sig_proc
);
1800 * if task hasn't already been stopped by
1803 uth
->uu_siglist
&= ~mask
;
1804 if (sig_proc
->p_stat
!= SSTOP
) {
1805 sig_proc
->p_xstat
= signum
;
1806 sig_proc
->p_stat
= SSTOP
;
1807 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), (UInt32
*)&sig_proc
->p_flag
);
1808 sig_proc
->p_lflag
&= ~P_LWAITED
;
1809 proc_unlock(sig_proc
);
1811 pp
= proc_parentholdref(sig_proc
);
1813 if (( pp
!= PROC_NULL
) && ((pp
->p_flag
& P_NOCLDSTOP
) == 0)) {
1815 my_cred
= kauth_cred_proc_ref(sig_proc
);
1816 r_uid
= my_cred
->cr_ruid
;
1817 kauth_cred_unref(&my_cred
);
1819 proc_lock(sig_proc
);
1820 pp
->si_pid
= sig_proc
->p_pid
;
1822 * POSIX: sigaction for a stopped child
1823 * when sent to the parent must set the
1824 * child's signal number into si_status.
1826 if (signum
!= SIGSTOP
)
1827 pp
->si_status
= WEXITSTATUS(sig_proc
->p_xstat
);
1829 pp
->si_status
= W_EXITCODE(signum
, signum
);
1830 pp
->si_code
= CLD_STOPPED
;
1832 proc_unlock(sig_proc
);
1834 psignal(pp
, SIGCHLD
);
1836 if (pp
!= PROC_NULL
)
1837 proc_parentdropref(pp
, 0);
1839 proc_unlock(sig_proc
);
1843 DTRACE_PROC3(signal__send
, thread_t
, sig_thread
, proc_t
, p
, int, signum
);
1846 * enters switch with sig_proc lock held but dropped when
1847 * gets out of switch
1851 * Signals ignored by default have been dealt
1852 * with already, since their bits are on in
1858 * Kill signal always sets process running and
1862 * Process will be running after 'run'
1864 sig_proc
->p_stat
= SRUN
;
1865 proc_unlock(sig_proc
);
1866 thread_abort(sig_thread
);
1872 * Let the process run. If it's sleeping on an
1873 * event, it remains so.
1875 OSBitOrAtomic(P_CONTINUED
, (UInt32
*)&sig_proc
->p_flag
);
1876 sig_proc
->p_contproc
= sig_proc
->p_pid
;
1878 proc_unlock(sig_proc
);
1879 (void) task_resume(sig_task
);
1880 proc_lock(sig_proc
);
1882 * When processing a SIGCONT, we need to check
1883 * to see if there are signals pending that
1884 * were not delivered because we had been
1885 * previously stopped. If that's the case,
1886 * we need to thread_abort_safely() to trigger
1887 * interruption of the current system call to
1888 * cause their handlers to fire. If it's only
1889 * the SIGCONT, then don't wake up.
1891 if (((flavor
& (PSIG_VFORK
|PSIG_THREAD
)) == 0) && (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~sig_proc
->p_sigignore
) & ~mask
)) {
1892 uth
->uu_siglist
&= ~mask
;
1893 sig_proc
->p_stat
= SRUN
;
1897 uth
->uu_siglist
&= ~mask
;
1898 sig_proc
->p_stat
= SRUN
;
1899 proc_unlock(sig_proc
);
1904 * A signal which has a default action of killing
1905 * the process, and for which there is no handler,
1906 * needs to act like SIGKILL
1908 if (((flavor
& (PSIG_VFORK
|PSIG_THREAD
)) == 0) && (action
== SIG_DFL
) && (prop
& SA_KILL
)) {
1909 sig_proc
->p_stat
= SRUN
;
1910 proc_unlock(sig_proc
);
1911 thread_abort(sig_thread
);
1916 * All other signals wake up the process, but don't
1919 if (sig_proc
->p_stat
== SSTOP
) {
1920 proc_unlock(sig_proc
);
1930 * If we're being traced (possibly because someone attached us
1931 * while we were stopped), check for a signal from the debugger.
1933 if (sig_proc
->p_stat
== SSTOP
) {
1934 if ((sig_proc
->p_lflag
& P_LTRACED
) != 0 && sig_proc
->p_xstat
!= 0)
1935 uth
->uu_siglist
|= sigmask(sig_proc
->p_xstat
);
1936 if ((flavor
& PSIG_VFORK
) != 0) {
1937 sig_proc
->p_stat
= SRUN
;
1939 proc_unlock(sig_proc
);
1942 * setrunnable(p) in BSD and
1943 * Wake up the thread if it is interruptible.
1945 sig_proc
->p_stat
= SRUN
;
1946 proc_unlock(sig_proc
);
1947 if ((flavor
& PSIG_VFORK
) == 0)
1948 thread_abort_safely(sig_thread
);
1951 if ((flavor
& PSIG_LOCKED
)== 0) {
1952 proc_signalend(sig_proc
, 0);
1957 psignal(proc_t p
, int signum
)
1959 psignal_internal(p
, NULL
, NULL
, 0, signum
);
1963 psignal_locked(proc_t p
, int signum
)
1965 psignal_internal(p
, NULL
, NULL
, PSIG_LOCKED
, signum
);
1969 psignal_vfork(proc_t p
, task_t new_task
, thread_t thread
, int signum
)
1971 psignal_internal(p
, new_task
, thread
, PSIG_VFORK
, signum
);
1975 psignal_uthread(thread_t thread
, int signum
)
1977 psignal_internal(PROC_NULL
, TASK_NULL
, thread
, PSIG_THREAD
, signum
);
1982 * If the current process has received a signal (should be caught or cause
1983 * termination, should interrupt current syscall), return the signal number.
1984 * Stop signals with default action are processed immediately, then cleared;
1985 * they aren't returned. This is checked after each entry to the system for
1986 * a syscall or trap (though this can usually be done without calling issignal
1987 * by checking the pending signal masks in the CURSIG macro.) The normal call
1990 * while (signum = CURSIG(curproc))
1996 int signum
, mask
, prop
, sigbits
;
1998 struct uthread
* ut
;
2000 kauth_cred_t my_cred
;
2004 cur_act
= current_thread();
2007 if(rdebug_proc
&& (p
== rdebug_proc
)) {
2010 #endif /* SIGNAL_DEBUG */
2014 * Try to grab the signal lock.
2016 if (sig_try_locked(p
) <= 0) {
2021 proc_signalstart(p
, 1);
2023 ut
= get_bsdthread_info(cur_act
);
2025 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2027 if (p
->p_lflag
& P_LPPWAIT
)
2028 sigbits
&= ~stopsigmask
;
2029 if (sigbits
== 0) { /* no signal to send */
2034 signum
= ffs((long)sigbits
);
2035 mask
= sigmask(signum
);
2036 prop
= sigprop
[signum
];
2039 * We should see pending but ignored signals
2040 * only if P_LTRACED was on when they were posted.
2042 if (mask
& p
->p_sigignore
&& (p
->p_lflag
& P_LTRACED
) == 0) {
2043 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2046 if (p
->p_lflag
& P_LTRACED
&& (p
->p_lflag
& P_LPPWAIT
) == 0) {
2049 * If traced, always stop, and stay
2050 * stopped until released by the debugger.
2052 /* ptrace debugging */
2053 p
->p_xstat
= signum
;
2055 if (p
->p_lflag
& P_LSIGEXC
) {
2057 p
->sigwait_thread
= cur_act
;
2059 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), (UInt32
*)&p
->p_flag
);
2060 p
->p_lflag
&= ~P_LWAITED
;
2061 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2062 proc_signalend(p
, 1);
2064 do_bsdexception(EXC_SOFTWARE
, EXC_SOFT_SIGNAL
, signum
);
2066 proc_signalstart(p
, 1);
2069 my_cred
= kauth_cred_proc_ref(p
);
2070 r_uid
= my_cred
->cr_ruid
;
2071 kauth_cred_unref(&my_cred
);
2073 pp
= proc_parentholdref(p
);
2074 if (pp
!= PROC_NULL
) {
2077 pp
->si_pid
= p
->p_pid
;
2078 pp
->si_status
= p
->p_xstat
;
2079 pp
->si_code
= CLD_TRAPPED
;
2086 * XXX Have to really stop for debuggers;
2087 * XXX stop() doesn't do the right thing.
2088 * XXX Inline the task_suspend because we
2089 * XXX have to diddle Unix state in the
2097 p
->sigwait_thread
= cur_act
;
2099 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), (UInt32
*)&p
->p_flag
);
2100 p
->p_lflag
&= ~P_LWAITED
;
2101 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2103 proc_signalend(p
, 1);
2106 if (pp
!= PROC_NULL
) {
2107 psignal(pp
, SIGCHLD
);
2109 wakeup((caddr_t
)pp
);
2110 proc_parentdropref(pp
, 1);
2114 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
2115 thread_block(THREAD_CONTINUE_NULL
);
2117 proc_signalstart(p
, 1);
2121 p
->sigwait_thread
= NULL
;
2122 wakeup((caddr_t
)&p
->sigwait_thread
);
2125 * This code is to detect when gdb is killed
2126 * even as the traced program is attached.
2127 * pgsignal would get the SIGKILL to traced program
2128 * That's what we are trying to see (I hope)
2130 if (ut
->uu_siglist
& sigmask(SIGKILL
)) {
2132 * Wait event may still be outstanding;
2133 * clear it, since sig_lock_to_exit will
2136 clear_wait(current_thread(), THREAD_INTERRUPTED
);
2137 sig_lock_to_exit(p
);
2139 * Since this thread will be resumed
2140 * to allow the current syscall to
2141 * be completed, must save u_qsave
2142 * before calling exit(). (Since exit()
2143 * calls closef() which can trash u_qsave.)
2145 proc_signalend(p
, 1);
2147 exit1(p
, W_EXITCODE(0, SIGKILL
), (int *)NULL
);
2152 * We may have to quit
2154 if (thread_should_abort(current_thread())) {
2159 * If parent wants us to take the signal,
2160 * then it will leave it in p->p_xstat;
2161 * otherwise we just look for signals again.
2163 signum
= p
->p_xstat
;
2167 * Put the new signal into p_siglist. If the
2168 * signal is being masked, look for other signals.
2170 mask
= sigmask(signum
);
2171 ut
->uu_siglist
|= mask
;
2172 if (ut
->uu_sigmask
& mask
)
2177 * Decide whether the signal should be returned.
2178 * Return the signal's number, or fall through
2179 * to clear it from the pending mask.
2182 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2186 * Don't take default actions on system processes.
2188 if (p
->p_ppid
== 0) {
2191 * Are you sure you want to ignore SIGSEGV
2194 printf("Process (pid %d) got signal %d\n",
2197 break; /* == ignore */
2201 * If there is a pending stop signal to process
2202 * with default action, stop here,
2203 * then clear the signal. However,
2204 * if process is member of an orphaned
2205 * process group, ignore tty stop signals.
2207 if (prop
& SA_STOP
) {
2212 if (p
->p_lflag
& P_LTRACED
||
2213 (pg
->pg_jobc
== 0 &&
2214 prop
& SA_TTYSTOP
)) {
2217 break; /* == ignore */
2220 if (p
->p_stat
!= SSTOP
) {
2222 p
->p_xstat
= signum
;
2225 p
->p_lflag
&= ~P_LWAITED
;
2228 pp
= proc_parentholdref(p
);
2230 if ((pp
!= PROC_NULL
) && ((pp
->p_flag
& P_NOCLDSTOP
) == 0)) {
2231 my_cred
= kauth_cred_proc_ref(p
);
2232 r_uid
= my_cred
->cr_ruid
;
2233 kauth_cred_unref(&my_cred
);
2236 pp
->si_pid
= p
->p_pid
;
2237 pp
->si_status
= WEXITSTATUS(p
->p_xstat
);
2238 pp
->si_code
= CLD_STOPPED
;
2242 psignal(pp
, SIGCHLD
);
2244 if (pp
!= PROC_NULL
)
2245 proc_parentdropref(pp
, 0);
2249 } else if (prop
& SA_IGNORE
) {
2251 * Except for SIGCONT, shouldn't get here.
2252 * Default action is to ignore; drop it.
2254 break; /* == ignore */
2256 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2266 * Masking above should prevent us ever trying
2267 * to take action on an ignored signal other
2268 * than SIGCONT, unless process is traced.
2270 if ((prop
& SA_CONT
) == 0 &&
2271 (p
->p_lflag
& P_LTRACED
) == 0)
2272 printf("issignal\n");
2273 break; /* == ignore */
2277 * This signal has an action, let
2278 * postsig() process it.
2280 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2284 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2288 proc_signalend(p
,1);
2293 /* called from _sleep */
2297 int signum
, mask
, prop
, sigbits
;
2299 struct uthread
* ut
;
2303 cur_act
= current_thread();
2305 ut
= get_bsdthread_info(cur_act
);
2307 if (ut
->uu_siglist
== 0)
2310 if (((ut
->uu_siglist
& ~ut
->uu_sigmask
) == 0) && ((p
->p_lflag
& P_LTRACED
) == 0))
2313 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2316 if (p
->p_lflag
& P_LPPWAIT
)
2317 sigbits
&= ~stopsigmask
;
2318 if (sigbits
== 0) { /* no signal to send */
2322 signum
= ffs((long)sigbits
);
2323 mask
= sigmask(signum
);
2324 prop
= sigprop
[signum
];
2327 * We should see pending but ignored signals
2328 * only if P_LTRACED was on when they were posted.
2330 if (mask
& p
->p_sigignore
&& (p
->p_lflag
& P_LTRACED
) == 0) {
2333 if (p
->p_lflag
& P_LTRACED
&& (p
->p_lflag
& P_LPPWAIT
) == 0) {
2335 * Put the new signal into p_siglist. If the
2336 * signal is being masked, look for other signals.
2338 mask
= sigmask(signum
);
2339 if (ut
->uu_sigmask
& mask
)
2345 * Decide whether the signal should be returned.
2346 * Return the signal's number, or fall through
2347 * to clear it from the pending mask.
2350 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2354 * Don't take default actions on system processes.
2356 if (p
->p_ppid
== 0) {
2359 * Are you sure you want to ignore SIGSEGV
2362 printf("Process (pid %d) got signal %d\n",
2365 break; /* == ignore */
2369 * If there is a pending stop signal to process
2370 * with default action, stop here,
2371 * then clear the signal. However,
2372 * if process is member of an orphaned
2373 * process group, ignore tty stop signals.
2375 if (prop
& SA_STOP
) {
2380 if (p
->p_lflag
& P_LTRACED
||
2381 (pg
->pg_jobc
== 0 &&
2382 prop
& SA_TTYSTOP
)) {
2384 break; /* == ignore */
2389 } else if (prop
& SA_IGNORE
) {
2391 * Except for SIGCONT, shouldn't get here.
2392 * Default action is to ignore; drop it.
2394 break; /* == ignore */
2402 * Masking above should prevent us ever trying
2403 * to take action on an ignored signal other
2404 * than SIGCONT, unless process is traced.
2406 if ((prop
& SA_CONT
) == 0 &&
2407 (p
->p_lflag
& P_LTRACED
) == 0)
2408 printf("issignal\n");
2409 break; /* == ignore */
2413 * This signal has an action, let
2414 * postsig() process it.
2418 sigbits
&= ~mask
; /* take the signal! */
2424 * Put the argument process into the stopped state and notify the parent
2425 * via wakeup. Signals are handled elsewhere. The process must not be
2429 stop(proc_t p
, proc_t parent
)
2431 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), (UInt32
*)&p
->p_flag
);
2432 if ((parent
!= PROC_NULL
) && (parent
->p_stat
!= SSTOP
)) {
2434 wakeup((caddr_t
)parent
);
2437 (void) task_suspend(p
->task
); /*XXX*/
2441 * Take the action for the specified signal
2442 * from the current set of pending signals.
2447 proc_t p
= current_proc();
2448 struct sigacts
*ps
= p
->p_sigacts
;
2449 user_addr_t catcher
;
2451 int mask
, returnmask
;
2452 struct uthread
* ut
;
2458 * This must be called on master cpu
2460 if (cpu_number() != master_cpu
)
2461 panic("psig not on master");
2466 * Try to grab the signal lock.
2468 if (sig_try_locked(p
) <= 0) {
2473 proc_signalstart(p
, 1);
2475 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2476 mask
= sigmask(signum
);
2477 ut
->uu_siglist
&= ~mask
;
2478 catcher
= ps
->ps_sigact
[signum
];
2479 if (catcher
== SIG_DFL
) {
2481 * Default catcher, where the default is to kill
2482 * the process. (Other cases were ignored above.)
2485 bzero((caddr_t
)&sinfo
, sizeof(siginfo_t
));
2487 sig_lock_to_exit(p
);
2488 p
->p_acflag
|= AXSIG
;
2489 if (sigprop
[signum
] & SA_CORE
) {
2490 p
->p_sigacts
->ps_sig
= signum
;
2491 proc_signalend(p
, 1);
2493 if (coredump(p
) == 0)
2494 signum
|= WCOREFLAG
;
2496 proc_signalend(p
, 1);
2500 sinfo
.si_signo
= signum
;
2501 sinfo
.si_pid
= p
->si_pid
;
2502 sinfo
.si_uid
= p
->si_uid
;
2503 sinfo
.si_status
= WEXITSTATUS(p
->si_status
);
2505 DTRACE_PROC3(signal__handle
, int, signum
, siginfo_t
*, &sinfo
,
2506 void (*)(void), SIG_DFL
);
2508 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
2512 * If we get here, the signal must be caught.
2515 if (catcher
== SIG_IGN
|| (ut
->uu_sigmask
& mask
))
2517 "postsig: processing masked or ignored signal\n");
2521 * Set the new mask value and also defer further
2522 * occurences of this signal.
2524 * Special case: user has done a sigpause. Here the
2525 * current mask is not of interest, but rather the
2526 * mask from before the sigpause is what we want
2527 * restored after the signal processing is completed.
2529 if (ut
->uu_flag
& UT_SAS_OLDMASK
) {
2530 returnmask
= ut
->uu_oldmask
;
2531 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
2534 returnmask
= ut
->uu_sigmask
;
2535 ut
->uu_sigmask
|= ps
->ps_catchmask
[signum
];
2536 if ((ps
->ps_signodefer
& mask
) == 0)
2537 ut
->uu_sigmask
|= mask
;
2538 if ((signum
!= SIGILL
) && (signum
!= SIGTRAP
) && (ps
->ps_sigreset
& mask
)) {
2539 if ((signum
!= SIGCONT
) && (sigprop
[signum
] & SA_IGNORE
))
2540 p
->p_sigignore
|= mask
;
2541 ps
->ps_sigact
[signum
] = SIG_DFL
;
2542 ps
->ps_siginfo
&= ~mask
;
2543 ps
->ps_signodefer
&= ~mask
;
2546 /* Needs to disable to run in user mode */
2547 if (signum
== SIGFPE
) {
2548 thread_enable_fpe(current_thread(), 0);
2550 #endif /* __ppc__ */
2552 if (ps
->ps_sig
!= signum
) {
2558 OSIncrementAtomic(&p
->p_stats
->p_ru
.ru_nsignals
);
2559 sendsig(p
, catcher
, signum
, returnmask
, code
);
2561 proc_signalend(p
, 1);
2566 * Attach a signal knote to the list of knotes for this process.
2568 * Signal knotes share the knote list with proc knotes. This
2569 * could be avoided by using a signal-specific knote list, but
2570 * probably isn't worth the trouble.
2574 filt_sigattach(struct knote
*kn
)
2576 proc_t p
= current_proc(); /* can attach only to oneself */
2580 kn
->kn_ptr
.p_proc
= p
;
2581 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
2583 KNOTE_ATTACH(&p
->p_klist
, kn
);
2585 proc_klist_unlock();
2591 * remove the knote from the process list, if it hasn't already
2592 * been removed by exit processing.
2596 filt_sigdetach(struct knote
*kn
)
2598 proc_t p
= kn
->kn_ptr
.p_proc
;
2601 kn
->kn_ptr
.p_proc
= NULL
;
2602 KNOTE_DETACH(&p
->p_klist
, kn
);
2603 proc_klist_unlock();
2607 * Post an event to the signal filter. Because we share the same list
2608 * as process knotes, we have to filter out and handle only signal events.
2610 * We assume that we process fdfree() before we post the NOTE_EXIT for
2611 * a process during exit. Therefore, since signal filters can only be
2612 * set up "in-process", we should have already torn down the kqueue
2613 * hosting the EVFILT_SIGNAL knote and should never see NOTE_EXIT.
2616 filt_signal(struct knote
*kn
, long hint
)
2619 if (hint
& NOTE_SIGNAL
) {
2620 hint
&= ~NOTE_SIGNAL
;
2622 if (kn
->kn_id
== (unsigned int)hint
)
2624 } else if (hint
& NOTE_EXIT
) {
2625 panic("filt_signal: detected NOTE_EXIT event");
2628 return (kn
->kn_data
!= 0);
2632 bsd_ast(thread_t thread
)
2634 proc_t p
= current_proc();
2635 struct uthread
*ut
= get_bsdthread_info(thread
);
2638 static int bsd_init_done
= 0;
2643 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
2644 pc
= get_useraddr();
2645 addupc_task(p
, pc
, 1);
2646 OSBitAndAtomic(~((uint32_t)P_OWEUPC
), (UInt32
*)&p
->p_flag
);
2649 if (timerisset(&p
->p_vtimer_user
.it_value
)) {
2652 task_vtimer_update(p
->task
, TASK_VTIMER_USER
, µsecs
);
2654 if (!itimerdecr(p
, &p
->p_vtimer_user
, microsecs
)) {
2655 if (timerisset(&p
->p_vtimer_user
.it_value
))
2656 task_vtimer_set(p
->task
, TASK_VTIMER_USER
);
2658 task_vtimer_clear(p
->task
, TASK_VTIMER_USER
);
2660 psignal(p
, SIGVTALRM
);
2664 if (timerisset(&p
->p_vtimer_prof
.it_value
)) {
2667 task_vtimer_update(p
->task
, TASK_VTIMER_PROF
, µsecs
);
2669 if (!itimerdecr(p
, &p
->p_vtimer_prof
, microsecs
)) {
2670 if (timerisset(&p
->p_vtimer_prof
.it_value
))
2671 task_vtimer_set(p
->task
, TASK_VTIMER_PROF
);
2673 task_vtimer_clear(p
->task
, TASK_VTIMER_PROF
);
2675 psignal(p
, SIGPROF
);
2679 if (timerisset(&p
->p_rlim_cpu
)) {
2682 task_vtimer_update(p
->task
, TASK_VTIMER_RLIM
, (uint32_t *) &tv
.tv_usec
);
2685 if (p
->p_rlim_cpu
.tv_sec
> 0 || p
->p_rlim_cpu
.tv_usec
> tv
.tv_usec
) {
2687 timersub(&p
->p_rlim_cpu
, &tv
, &p
->p_rlim_cpu
);
2691 timerclear(&p
->p_rlim_cpu
);
2694 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
2696 psignal(p
, SIGXCPU
);
2700 if (CHECK_SIGNALS(p
, current_thread(), ut
)) {
2701 while ( (signum
= issignal(p
)) )
2705 if (!bsd_init_done
) {
2712 /* ptrace set runnalbe */
2714 pt_setrunnable(proc_t p
)
2720 if (p
->p_lflag
& P_LTRACED
) {
2725 wakeup((caddr_t
)&(p
->sigwait
));
2737 mach_exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
2741 return(bsd_exception(exc
, codes
, 2));
2745 proc_pendingsignals(proc_t p
, sigset_t mask
)
2747 struct uthread
* uth
;
2752 /* If the process is in proc exit return no signal info */
2753 if (p
->p_lflag
& P_LPEXIT
) {
2757 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
2759 uth
= (struct uthread
*)get_bsdthread_info(th
);
2761 bits
= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
2767 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
2768 bits
|= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
2776 thread_issignal(proc_t p
, thread_t th
, sigset_t mask
)
2778 struct uthread
* uth
;
2782 uth
= (struct uthread
*)get_bsdthread_info(th
);
2784 bits
= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
2791 * Allow external reads of the sigprop array.
2794 hassigprop(int sig
, int prop
)
2796 return (sigprop
[sig
] & prop
);
2800 pgsigio(pid_t pgid
, int sig
)
2802 proc_t p
= PROC_NULL
;
2805 gsignal(-(pgid
), sig
);
2807 else if (pgid
> 0 && (p
= proc_find(pgid
)) != 0)
2815 proc_signalstart(proc_t p
, int locked
)
2819 while ((p
->p_lflag
& P_LINSIGNAL
) == P_LINSIGNAL
) {
2820 p
->p_lflag
|= P_LSIGNALWAIT
;
2821 msleep(&p
->p_sigmask
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
2823 p
->p_lflag
|= P_LINSIGNAL
;
2828 int sp
, *fp
, numsaved
;
2830 __asm__
volatile("mr %0,r1" : "=r" (sp
));
2832 fp
= (int *)*((int *)sp
);
2833 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
2834 p
->lockpc
[numsaved
] = fp
[2];
2840 #endif /* __ppc__ */
2841 #endif /* SIGNAL_DEBUG */
2842 #endif /* DIAGNOSTIC */
2843 p
->p_signalholder
= current_thread();
2850 proc_signalend(proc_t p
, int locked
)
2854 p
->p_lflag
&= ~P_LINSIGNAL
;
2860 int sp
, *fp
, numsaved
;
2862 __asm__
volatile("mr %0,r1" : "=r" (sp
));
2864 fp
= (int *)*((int *)sp
);
2865 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
2866 p
->unlockpc
[numsaved
] = fp
[2];
2872 #endif /* __ppc__ */
2873 #endif /* SIGNAL_DEBUG */
2874 #endif /* DIAGNOSTIC */
2876 if ((p
->p_lflag
& P_LSIGNALWAIT
) == P_LSIGNALWAIT
) {
2877 p
->p_lflag
&= ~P_LSIGNALWAIT
;
2878 wakeup(&p
->p_sigmask
);
2880 p
->p_signalholder
= NULL
;
2887 sig_lock_to_exit(proc_t p
)
2889 thread_t self
= current_thread();
2891 p
->exit_thread
= self
;
2893 (void) task_suspend(p
->task
);
2898 sig_try_locked(proc_t p
)
2900 thread_t self
= current_thread();
2902 while (p
->sigwait
|| p
->exit_thread
) {
2903 if (p
->exit_thread
) {
2906 msleep((caddr_t
)&p
->sigwait_thread
, &p
->p_mlock
, PCATCH
| PDROP
, 0, 0);
2907 if (thread_should_abort(self
)) {
2909 * Terminate request - clean up.