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 <security/audit/audit.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/task.h> /* extern void *get_bsdtask_info(task_t); */
105 #include <kern/thread.h>
106 #include <kern/sched_prim.h>
107 #include <kern/thread_call.h>
108 #include <mach/exception.h>
109 #include <mach/task.h>
110 #include <mach/thread_act.h>
111 #include <libkern/OSAtomic.h>
116 * Missing prototypes that Mach should export
120 extern int thread_enable_fpe(thread_t act
, int onoff
);
121 extern thread_t
port_name_to_thread(mach_port_name_t port_name
);
122 extern kern_return_t
get_signalact(task_t
, thread_t
*, int);
123 extern unsigned int get_useraddr(void);
124 extern kern_return_t
task_suspend_internal(task_t
);
125 extern kern_return_t
task_resume_internal(task_t
);
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
, thread_t
, int, struct __kern_sigaction
*, boolean_t in_sigstart
);
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
);
151 static void filt_signaltouch(struct knote
*kn
, struct kevent64_s
*kev
,
154 struct filterops sig_filtops
= {
155 .f_attach
= filt_sigattach
,
156 .f_detach
= filt_sigdetach
,
157 .f_event
= filt_signal
,
158 .f_touch
= filt_signaltouch
,
161 /* structures and fns for killpg1 iterartion callback and filters */
162 struct killpg1_filtargs
{
167 struct killpg1_iterargs
{
175 static int killpg1_filt(proc_t p
, void * arg
);
176 static int killpg1_pgrpfilt(proc_t p
, __unused
void * arg
);
177 static int killpg1_callback(proc_t p
, void * arg
);
179 static int pgsignal_filt(proc_t p
, void * arg
);
180 static int pgsignal_callback(proc_t p
, void * arg
);
181 static kern_return_t
get_signalthread(proc_t
, int, thread_t
*);
184 /* flags for psignal_internal */
185 #define PSIG_LOCKED 0x1
186 #define PSIG_VFORK 0x2
187 #define PSIG_THREAD 0x4
190 static void psignal_internal(proc_t p
, task_t task
, thread_t thread
, int flavor
, int signum
);
193 * NOTE: Source and target may *NOT* overlap! (target is smaller)
196 sigaltstack_kern_to_user32(struct kern_sigaltstack
*in
, struct user32_sigaltstack
*out
)
198 out
->ss_sp
= CAST_DOWN_EXPLICIT(user32_addr_t
, in
->ss_sp
);
199 out
->ss_size
= CAST_DOWN_EXPLICIT(user32_size_t
, in
->ss_size
);
200 out
->ss_flags
= in
->ss_flags
;
204 sigaltstack_kern_to_user64(struct kern_sigaltstack
*in
, struct user64_sigaltstack
*out
)
206 out
->ss_sp
= in
->ss_sp
;
207 out
->ss_size
= in
->ss_size
;
208 out
->ss_flags
= in
->ss_flags
;
212 * NOTE: Source and target may are permitted to overlap! (source is smaller);
213 * this works because we copy fields in order from the end of the struct to
217 sigaltstack_user32_to_kern(struct user32_sigaltstack
*in
, struct kern_sigaltstack
*out
)
219 out
->ss_flags
= in
->ss_flags
;
220 out
->ss_size
= in
->ss_size
;
221 out
->ss_sp
= CAST_USER_ADDR_T(in
->ss_sp
);
224 sigaltstack_user64_to_kern(struct user64_sigaltstack
*in
, struct kern_sigaltstack
*out
)
226 out
->ss_flags
= in
->ss_flags
;
227 out
->ss_size
= in
->ss_size
;
228 out
->ss_sp
= in
->ss_sp
;
232 sigaction_kern_to_user32(struct kern_sigaction
*in
, struct user32_sigaction
*out
)
234 /* This assumes 32 bit __sa_handler is of type sig_t */
235 out
->__sigaction_u
.__sa_handler
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->__sigaction_u
.__sa_handler
);
236 out
->sa_mask
= in
->sa_mask
;
237 out
->sa_flags
= in
->sa_flags
;
240 sigaction_kern_to_user64(struct kern_sigaction
*in
, struct user64_sigaction
*out
)
242 /* This assumes 32 bit __sa_handler is of type sig_t */
243 out
->__sigaction_u
.__sa_handler
= in
->__sigaction_u
.__sa_handler
;
244 out
->sa_mask
= in
->sa_mask
;
245 out
->sa_flags
= in
->sa_flags
;
249 __sigaction_user32_to_kern(struct __user32_sigaction
*in
, struct __kern_sigaction
*out
)
251 out
->__sigaction_u
.__sa_handler
= CAST_USER_ADDR_T(in
->__sigaction_u
.__sa_handler
);
252 out
->sa_tramp
= CAST_USER_ADDR_T(in
->sa_tramp
);
253 out
->sa_mask
= in
->sa_mask
;
254 out
->sa_flags
= in
->sa_flags
;
258 __sigaction_user64_to_kern(struct __user64_sigaction
*in
, struct __kern_sigaction
*out
)
260 out
->__sigaction_u
.__sa_handler
= in
->__sigaction_u
.__sa_handler
;
261 out
->sa_tramp
= in
->sa_tramp
;
262 out
->sa_mask
= in
->sa_mask
;
263 out
->sa_flags
= in
->sa_flags
;
267 void ram_printf(int);
269 unsigned int rdebug_proc
=0;
276 #endif /* SIGNAL_DEBUG */
280 signal_setast(thread_t sig_actthread
)
282 act_set_astbsd(sig_actthread
);
286 * Can process p, with ucred uc, send the signal signum to process q?
287 * uc is refcounted by the caller so internal fileds can be used safely
288 * when called with zombie arg, list lock is held
291 cansignal(proc_t p
, kauth_cred_t uc
, proc_t q
, int signum
, int zombie
)
293 kauth_cred_t my_cred
;
294 struct session
* p_sessp
= SESSION_NULL
;
295 struct session
* q_sessp
= SESSION_NULL
;
299 error
= mac_proc_check_signal(p
, q
, signum
);
304 /* you can signal yourself */
308 if (!suser(uc
, NULL
))
309 return (1); /* root can always signal */
313 if (p
->p_pgrp
!= PGRP_NULL
)
314 p_sessp
= p
->p_pgrp
->pg_session
;
315 if (q
->p_pgrp
!= PGRP_NULL
)
316 q_sessp
= q
->p_pgrp
->pg_session
;
318 if (signum
== SIGCONT
&& q_sessp
== p_sessp
) {
321 return (1); /* SIGCONT in session */
328 * If the real or effective UID of the sender matches the real
329 * or saved UID of the target, permit the signal to
333 my_cred
= kauth_cred_proc_ref(q
);
335 my_cred
= proc_ucred(q
);
337 if (kauth_cred_getruid(uc
) == kauth_cred_getruid(my_cred
) ||
338 kauth_cred_getruid(uc
) == kauth_cred_getsvuid(my_cred
) ||
339 kauth_cred_getuid(uc
) == kauth_cred_getruid(my_cred
) ||
340 kauth_cred_getuid(uc
) == kauth_cred_getsvuid(my_cred
)) {
342 kauth_cred_unref(&my_cred
);
347 kauth_cred_unref(&my_cred
);
359 * Notes: Uses current thread as a parameter to inform PPC to enable
360 * FPU exceptions via setsigvec(); this operation is not proxy
365 sigaction(proc_t p
, struct sigaction_args
*uap
, __unused
int32_t *retval
)
367 struct kern_sigaction vec
;
368 struct __kern_sigaction __vec
;
370 struct kern_sigaction
*sa
= &vec
;
371 struct sigacts
*ps
= p
->p_sigacts
;
376 signum
= uap
->signum
;
377 if (signum
<= 0 || signum
>= NSIG
||
378 signum
== SIGKILL
|| signum
== SIGSTOP
)
382 sa
->sa_handler
= ps
->ps_sigact
[signum
];
383 sa
->sa_mask
= ps
->ps_catchmask
[signum
];
384 bit
= sigmask(signum
);
386 if ((ps
->ps_sigonstack
& bit
) != 0)
387 sa
->sa_flags
|= SA_ONSTACK
;
388 if ((ps
->ps_sigintr
& bit
) == 0)
389 sa
->sa_flags
|= SA_RESTART
;
390 if (ps
->ps_siginfo
& bit
)
391 sa
->sa_flags
|= SA_SIGINFO
;
392 if (ps
->ps_signodefer
& bit
)
393 sa
->sa_flags
|= SA_NODEFER
;
394 if (ps
->ps_64regset
& bit
)
395 sa
->sa_flags
|= SA_64REGSET
;
396 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDSTOP
))
397 sa
->sa_flags
|= SA_NOCLDSTOP
;
398 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDWAIT
))
399 sa
->sa_flags
|= SA_NOCLDWAIT
;
401 if (IS_64BIT_PROCESS(p
)) {
402 struct user64_sigaction vec64
;
404 sigaction_kern_to_user64(sa
, &vec64
);
405 error
= copyout(&vec64
, uap
->osa
, sizeof(vec64
));
407 struct user32_sigaction vec32
;
409 sigaction_kern_to_user32(sa
, &vec32
);
410 error
= copyout(&vec32
, uap
->osa
, sizeof(vec32
));
416 if (IS_64BIT_PROCESS(p
)) {
417 struct __user64_sigaction __vec64
;
419 error
= copyin(uap
->nsa
, &__vec64
, sizeof(__vec64
));
420 __sigaction_user64_to_kern(&__vec64
, &__vec
);
422 struct __user32_sigaction __vec32
;
424 error
= copyin(uap
->nsa
, &__vec32
, sizeof(__vec32
));
425 __sigaction_user32_to_kern(&__vec32
, &__vec
);
429 __vec
.sa_flags
&= SA_USERSPACE_MASK
; /* Only pass on valid sa_flags */
430 error
= setsigvec(p
, current_thread(), signum
, &__vec
, FALSE
);
435 /* Routines to manipulate bits on all threads */
437 clear_procsiglist(proc_t p
, int bit
, boolean_t in_signalstart
)
439 struct uthread
* uth
;
444 proc_signalstart(p
, 1);
446 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
447 thact
= p
->p_vforkact
;
448 uth
= (struct uthread
*)get_bsdthread_info(thact
);
450 uth
->uu_siglist
&= ~bit
;
453 proc_signalend(p
, 1);
458 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
459 uth
->uu_siglist
&= ~bit
;
461 p
->p_siglist
&= ~bit
;
463 proc_signalend(p
, 1);
471 unblock_procsigmask(proc_t p
, int bit
)
473 struct uthread
* uth
;
477 proc_signalstart(p
, 1);
479 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
480 thact
= p
->p_vforkact
;
481 uth
= (struct uthread
*)get_bsdthread_info(thact
);
483 uth
->uu_sigmask
&= ~bit
;
485 p
->p_sigmask
&= ~bit
;
486 proc_signalend(p
, 1);
490 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
491 uth
->uu_sigmask
&= ~bit
;
493 p
->p_sigmask
&= ~bit
;
495 proc_signalend(p
, 1);
501 block_procsigmask(proc_t p
, int bit
)
503 struct uthread
* uth
;
507 proc_signalstart(p
, 1);
509 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
510 thact
= p
->p_vforkact
;
511 uth
= (struct uthread
*)get_bsdthread_info(thact
);
513 uth
->uu_sigmask
|= bit
;
516 proc_signalend(p
, 1);
520 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
521 uth
->uu_sigmask
|= bit
;
525 proc_signalend(p
, 1);
531 set_procsigmask(proc_t p
, int bit
)
533 struct uthread
* uth
;
537 proc_signalstart(p
, 1);
539 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
540 thact
= p
->p_vforkact
;
541 uth
= (struct uthread
*)get_bsdthread_info(thact
);
543 uth
->uu_sigmask
= bit
;
546 proc_signalend(p
, 1);
550 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
551 uth
->uu_sigmask
= bit
;
554 proc_signalend(p
, 1);
560 /* XXX should be static? */
562 * Notes: The thread parameter is used in the PPC case to select the
563 * thread on which the floating point exception will be enabled
564 * or disabled. We can't simply take current_thread(), since
565 * this is called from posix_spawn() on the not currently running
566 * process/thread pair.
568 * We mark thread as unused to alow compilation without warning
569 * on non-PPC platforms.
572 setsigvec(proc_t p
, __unused thread_t thread
, int signum
, struct __kern_sigaction
*sa
, boolean_t in_sigstart
)
574 struct sigacts
*ps
= p
->p_sigacts
;
577 if ((signum
== SIGKILL
|| signum
== SIGSTOP
) &&
578 sa
->sa_handler
!= SIG_DFL
)
580 bit
= sigmask(signum
);
582 * Change setting atomically.
584 ps
->ps_sigact
[signum
] = sa
->sa_handler
;
585 ps
->ps_trampact
[signum
] = sa
->sa_tramp
;
586 ps
->ps_catchmask
[signum
] = sa
->sa_mask
&~ sigcantmask
;
587 if (sa
->sa_flags
& SA_SIGINFO
)
588 ps
->ps_siginfo
|= bit
;
590 ps
->ps_siginfo
&= ~bit
;
591 if (sa
->sa_flags
& SA_64REGSET
)
592 ps
->ps_64regset
|= bit
;
594 ps
->ps_64regset
&= ~bit
;
595 if ((sa
->sa_flags
& SA_RESTART
) == 0)
596 ps
->ps_sigintr
|= bit
;
598 ps
->ps_sigintr
&= ~bit
;
599 if (sa
->sa_flags
& SA_ONSTACK
)
600 ps
->ps_sigonstack
|= bit
;
602 ps
->ps_sigonstack
&= ~bit
;
603 if (sa
->sa_flags
& SA_USERTRAMP
)
604 ps
->ps_usertramp
|= bit
;
606 ps
->ps_usertramp
&= ~bit
;
607 if (sa
->sa_flags
& SA_RESETHAND
)
608 ps
->ps_sigreset
|= bit
;
610 ps
->ps_sigreset
&= ~bit
;
611 if (sa
->sa_flags
& SA_NODEFER
)
612 ps
->ps_signodefer
|= bit
;
614 ps
->ps_signodefer
&= ~bit
;
615 if (signum
== SIGCHLD
) {
616 if (sa
->sa_flags
& SA_NOCLDSTOP
)
617 OSBitOrAtomic(P_NOCLDSTOP
, &p
->p_flag
);
619 OSBitAndAtomic(~((uint32_t)P_NOCLDSTOP
), &p
->p_flag
);
620 if ((sa
->sa_flags
& SA_NOCLDWAIT
) || (sa
->sa_handler
== SIG_IGN
))
621 OSBitOrAtomic(P_NOCLDWAIT
, &p
->p_flag
);
623 OSBitAndAtomic(~((uint32_t)P_NOCLDWAIT
), &p
->p_flag
);
627 * Set bit in p_sigignore for signals that are set to SIG_IGN,
628 * and for signals set to SIG_DFL where the default is to ignore.
629 * However, don't put SIGCONT in p_sigignore,
630 * as we have to restart the process.
632 if (sa
->sa_handler
== SIG_IGN
||
633 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
635 clear_procsiglist(p
, bit
, in_sigstart
);
636 if (signum
!= SIGCONT
)
637 p
->p_sigignore
|= bit
; /* easier in psignal */
638 p
->p_sigcatch
&= ~bit
;
640 p
->p_sigignore
&= ~bit
;
641 if (sa
->sa_handler
== SIG_DFL
)
642 p
->p_sigcatch
&= ~bit
;
644 p
->p_sigcatch
|= bit
;
650 * Initialize signal state for process 0;
651 * set to ignore signals that are ignored by default.
658 for (i
= 1; i
< NSIG
; i
++)
659 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
660 p
->p_sigignore
|= sigmask(i
);
664 * Reset signals for an exec of the specified process.
667 execsigs(proc_t p
, thread_t thread
)
669 struct sigacts
*ps
= p
->p_sigacts
;
673 ut
= (struct uthread
*)get_bsdthread_info(thread
);
676 * transfer saved signal states from the process
677 * back to the current thread.
679 * NOTE: We do this without the process locked,
680 * because we are guaranteed to be single-threaded
681 * by this point in exec and the p_siglist is
682 * only accessed by threads inside the process.
684 ut
->uu_siglist
|= p
->p_siglist
;
688 * Reset caught signals. Held signals remain held
689 * through p_sigmask (unless they were caught,
690 * and are now ignored by default).
692 while (p
->p_sigcatch
) {
693 nc
= ffs((long)p
->p_sigcatch
);
695 p
->p_sigcatch
&= ~mask
;
696 if (sigprop
[nc
] & SA_IGNORE
) {
698 p
->p_sigignore
|= mask
;
699 ut
->uu_siglist
&= ~mask
;
701 ps
->ps_sigact
[nc
] = SIG_DFL
;
705 * Reset stack state to the user stack.
706 * Clear set of signals caught on the signal stack.
709 ut
->uu_sigstk
.ss_flags
= SA_DISABLE
;
710 ut
->uu_sigstk
.ss_size
= 0;
711 ut
->uu_sigstk
.ss_sp
= USER_ADDR_NULL
;
712 ut
->uu_flag
&= ~UT_ALTSTACK
;
714 ps
->ps_sigonstack
= 0;
718 * Manipulate signal mask.
719 * Note that we receive new mask, not pointer,
720 * and return old mask as return value;
721 * the library stub does the rest.
724 sigprocmask(proc_t p
, struct sigprocmask_args
*uap
, __unused
int32_t *retval
)
727 sigset_t oldmask
, nmask
;
728 user_addr_t omask
= uap
->omask
;
731 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
732 oldmask
= ut
->uu_sigmask
;
734 if (uap
->mask
== USER_ADDR_NULL
) {
735 /* just want old mask */
738 error
= copyin(uap
->mask
, &nmask
, sizeof(sigset_t
));
744 block_procsigmask(p
, (nmask
& ~sigcantmask
));
745 signal_setast(current_thread());
749 unblock_procsigmask(p
, (nmask
& ~sigcantmask
));
750 signal_setast(current_thread());
754 set_procsigmask(p
, (nmask
& ~sigcantmask
));
755 signal_setast(current_thread());
763 if (!error
&& omask
!= USER_ADDR_NULL
)
764 copyout(&oldmask
, omask
, sizeof(sigset_t
));
769 sigpending(__unused proc_t p
, struct sigpending_args
*uap
, __unused
int32_t *retval
)
774 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
775 pendlist
= ut
->uu_siglist
;
778 copyout(&pendlist
, uap
->osv
, sizeof(sigset_t
));
783 * Suspend process until signal, providing mask to be set
784 * in the meantime. Note nonstandard calling convention:
785 * libc stub passes mask, not pointer, to save a copyin.
789 sigcontinue(__unused
int error
)
791 // struct uthread *ut = get_bsdthread_info(current_thread());
792 unix_syscall_return(EINTR
);
796 sigsuspend(proc_t p
, struct sigsuspend_args
*uap
, int32_t *retval
)
798 __pthread_testcancel(1);
799 return(sigsuspend_nocancel(p
, (struct sigsuspend_nocancel_args
*)uap
, retval
));
803 sigsuspend_nocancel(proc_t p
, struct sigsuspend_nocancel_args
*uap
, __unused
int32_t *retval
)
807 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
810 * When returning from sigpause, we want
811 * the old mask to be restored after the
812 * signal handler has finished. Thus, we
813 * save it here and mark the sigacts structure
816 ut
->uu_oldmask
= ut
->uu_sigmask
;
817 ut
->uu_flag
|= UT_SAS_OLDMASK
;
818 ut
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
819 (void) tsleep0((caddr_t
) p
, PPAUSE
|PCATCH
, "pause", 0, sigcontinue
);
820 /* always return EINTR rather than ERESTART... */
826 __disable_threadsignal(__unused proc_t p
,
827 __unused
struct __disable_threadsignal_args
*uap
,
828 __unused
int32_t *retval
)
832 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
834 /* No longer valid to have any signal delivered */
835 uth
->uu_flag
|= (UT_NO_SIGMASK
| UT_CANCELDISABLE
);
842 __pthread_testcancel(int presyscall
)
845 thread_t self
= current_thread();
846 struct uthread
* uthread
;
848 uthread
= (struct uthread
*)get_bsdthread_info(self
);
851 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
853 if ((uthread
->uu_flag
& (UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
854 if(presyscall
!= 0) {
855 unix_syscall_return(EINTR
);
858 thread_abort_safely(self
);
865 __pthread_markcancel(__unused proc_t p
,
866 struct __pthread_markcancel_args
*uap
, __unused
int32_t *retval
)
868 thread_act_t target_act
;
872 target_act
= (thread_act_t
)port_name_to_thread(uap
->thread_port
);
874 if (target_act
== THR_ACT_NULL
)
877 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
879 /* if the thread is in vfork do not cancel */
880 if ((uth
->uu_flag
& (UT_VFORK
| UT_CANCEL
| UT_CANCELED
)) == 0) {
881 uth
->uu_flag
|= (UT_CANCEL
| UT_NO_SIGMASK
);
882 if (((uth
->uu_flag
& UT_NOTCANCELPT
) == 0)
883 && ((uth
->uu_flag
& UT_CANCELDISABLE
) == 0))
884 thread_abort_safely(target_act
);
887 thread_deallocate(target_act
);
891 /* if action =0 ; return the cancellation state ,
892 * if marked for cancellation, make the thread canceled
893 * if action = 1 ; Enable the cancel handling
894 * if action = 2; Disable the cancel handling
897 __pthread_canceled(__unused proc_t p
,
898 struct __pthread_canceled_args
*uap
, __unused
int32_t *retval
)
902 int action
= uap
->action
;
904 thread
= current_thread();
905 uth
= (struct uthread
*)get_bsdthread_info(thread
);
909 uth
->uu_flag
&= ~UT_CANCELDISABLE
;
912 uth
->uu_flag
|= UT_CANCELDISABLE
;
916 /* if the thread is in vfork do not cancel */
917 if((uth
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
918 uth
->uu_flag
&= ~UT_CANCEL
;
919 uth
->uu_flag
|= (UT_CANCELED
| UT_NO_SIGMASK
);
928 __posix_sem_syscall_return(kern_return_t kern_result
)
932 if (kern_result
== KERN_SUCCESS
)
934 else if (kern_result
== KERN_ABORTED
)
936 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
940 unix_syscall_return(error
);
941 /* does not return */
944 #if OLD_SEMWAIT_SIGNAL
950 * EFAULT if timespec is NULL
953 __old_semwait_signal(proc_t p
, struct __old_semwait_signal_args
*uap
,
956 __pthread_testcancel(0);
957 return(__old_semwait_signal_nocancel(p
, (struct __old_semwait_signal_nocancel_args
*)uap
, retval
));
961 __old_semwait_signal_nocancel(proc_t p
, struct __old_semwait_signal_nocancel_args
*uap
,
962 __unused
int32_t *retval
)
965 kern_return_t kern_result
;
967 mach_timespec_t then
;
969 struct user_timespec ts
;
970 boolean_t truncated_timeout
= FALSE
;
974 if (IS_64BIT_PROCESS(p
)) {
975 struct user64_timespec ts64
;
976 error
= copyin(uap
->ts
, &ts64
, sizeof(ts64
));
977 ts
.tv_sec
= ts64
.tv_sec
;
978 ts
.tv_nsec
= ts64
.tv_nsec
;
980 struct user32_timespec ts32
;
981 error
= copyin(uap
->ts
, &ts32
, sizeof(ts32
));
982 ts
.tv_sec
= ts32
.tv_sec
;
983 ts
.tv_nsec
= ts32
.tv_nsec
;
990 if ((ts
.tv_sec
& 0xFFFFFFFF00000000ULL
) != 0) {
991 ts
.tv_sec
= 0xFFFFFFFF;
993 truncated_timeout
= TRUE
;
997 then
.tv_sec
= ts
.tv_sec
;
998 then
.tv_nsec
= ts
.tv_nsec
;
1002 /* if time has elapsed, set time to null timepsec to bailout rightaway */
1003 if (now
.tv_sec
== ts
.tv_sec
?
1004 now
.tv_nsec
> ts
.tv_nsec
:
1005 now
.tv_sec
> ts
.tv_sec
) {
1009 then
.tv_sec
= ts
.tv_sec
- now
.tv_sec
;
1010 then
.tv_nsec
= ts
.tv_nsec
- now
.tv_nsec
;
1011 if (then
.tv_nsec
< 0) {
1012 then
.tv_nsec
+= NSEC_PER_SEC
;
1018 if (uap
->mutex_sem
== 0)
1019 kern_result
= semaphore_timedwait_trap_internal((mach_port_name_t
)uap
->cond_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
1021 kern_result
= semaphore_timedwait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
1025 if (uap
->mutex_sem
== 0)
1026 kern_result
= semaphore_wait_trap_internal(uap
->cond_sem
, __posix_sem_syscall_return
);
1029 kern_result
= semaphore_wait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, __posix_sem_syscall_return
);
1032 if (kern_result
== KERN_SUCCESS
&& !truncated_timeout
)
1034 else if (kern_result
== KERN_SUCCESS
&& truncated_timeout
)
1035 return(EINTR
); /* simulate an exceptional condition because Mach doesn't support a longer timeout */
1036 else if (kern_result
== KERN_ABORTED
)
1038 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
1043 #endif /* OLD_SEMWAIT_SIGNAL*/
1046 * Returns: 0 Success
1050 * EFAULT if timespec is NULL
1053 __semwait_signal(proc_t p
, struct __semwait_signal_args
*uap
,
1056 __pthread_testcancel(0);
1057 return(__semwait_signal_nocancel(p
, (struct __semwait_signal_nocancel_args
*)uap
, retval
));
1061 __semwait_signal_nocancel(__unused proc_t p
, struct __semwait_signal_nocancel_args
*uap
,
1062 __unused
int32_t *retval
)
1065 kern_return_t kern_result
;
1066 mach_timespec_t then
;
1067 struct timespec now
;
1068 struct user_timespec ts
;
1069 boolean_t truncated_timeout
= FALSE
;
1073 ts
.tv_sec
= uap
->tv_sec
;
1074 ts
.tv_nsec
= uap
->tv_nsec
;
1076 if ((ts
.tv_sec
& 0xFFFFFFFF00000000ULL
) != 0) {
1077 ts
.tv_sec
= 0xFFFFFFFF;
1079 truncated_timeout
= TRUE
;
1082 if (uap
->relative
) {
1083 then
.tv_sec
= ts
.tv_sec
;
1084 then
.tv_nsec
= ts
.tv_nsec
;
1088 /* if time has elapsed, set time to null timepsec to bailout rightaway */
1089 if (now
.tv_sec
== ts
.tv_sec
?
1090 now
.tv_nsec
> ts
.tv_nsec
:
1091 now
.tv_sec
> ts
.tv_sec
) {
1095 then
.tv_sec
= ts
.tv_sec
- now
.tv_sec
;
1096 then
.tv_nsec
= ts
.tv_nsec
- now
.tv_nsec
;
1097 if (then
.tv_nsec
< 0) {
1098 then
.tv_nsec
+= NSEC_PER_SEC
;
1104 if (uap
->mutex_sem
== 0)
1105 kern_result
= semaphore_timedwait_trap_internal((mach_port_name_t
)uap
->cond_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
1107 kern_result
= semaphore_timedwait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
1111 if (uap
->mutex_sem
== 0)
1112 kern_result
= semaphore_wait_trap_internal(uap
->cond_sem
, __posix_sem_syscall_return
);
1115 kern_result
= semaphore_wait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, __posix_sem_syscall_return
);
1118 if (kern_result
== KERN_SUCCESS
&& !truncated_timeout
)
1120 else if (kern_result
== KERN_SUCCESS
&& truncated_timeout
)
1121 return(EINTR
); /* simulate an exceptional condition because Mach doesn't support a longer timeout */
1122 else if (kern_result
== KERN_ABORTED
)
1124 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
1132 __pthread_kill(__unused proc_t p
, struct __pthread_kill_args
*uap
,
1133 __unused
int32_t *retval
)
1135 thread_t target_act
;
1137 int signum
= uap
->sig
;
1138 struct uthread
*uth
;
1140 target_act
= (thread_t
)port_name_to_thread(uap
->thread_port
);
1142 if (target_act
== THREAD_NULL
)
1144 if ((u_int
)signum
>= NSIG
) {
1149 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
1151 if (uth
->uu_flag
& UT_NO_SIGMASK
) {
1157 psignal_uthread(target_act
, signum
);
1159 thread_deallocate(target_act
);
1165 __pthread_sigmask(__unused proc_t p
, struct __pthread_sigmask_args
*uap
,
1166 __unused
int32_t *retval
)
1168 user_addr_t set
= uap
->set
;
1169 user_addr_t oset
= uap
->oset
;
1175 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1176 oldset
= ut
->uu_sigmask
;
1178 if (set
== USER_ADDR_NULL
) {
1179 /* need only old mask */
1183 error
= copyin(set
, &nset
, sizeof(sigset_t
));
1189 ut
->uu_sigmask
|= (nset
& ~sigcantmask
);
1193 ut
->uu_sigmask
&= ~(nset
);
1194 signal_setast(current_thread());
1198 ut
->uu_sigmask
= (nset
& ~sigcantmask
);
1199 signal_setast(current_thread());
1207 if (!error
&& oset
!= USER_ADDR_NULL
)
1208 copyout(&oldset
, oset
, sizeof(sigset_t
));
1214 * Returns: 0 Success
1220 __sigwait(proc_t p
, struct __sigwait_args
*uap
, int32_t *retval
)
1222 __pthread_testcancel(1);
1223 return(__sigwait_nocancel(p
, (struct __sigwait_nocancel_args
*)uap
, retval
));
1227 __sigwait_nocancel(proc_t p
, struct __sigwait_nocancel_args
*uap
, __unused
int32_t *retval
)
1230 struct uthread
*uth
;
1237 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1239 if (uap
->set
== USER_ADDR_NULL
)
1242 error
= copyin(uap
->set
, &mask
, sizeof(sigset_t
));
1246 siglist
= (mask
& ~sigcantmask
);
1252 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
1256 proc_signalstart(p
, 1);
1257 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1258 if ( (sigw
= uth
->uu_siglist
& siglist
) ) {
1262 proc_signalend(p
, 1);
1266 /* The signal was pending on a thread */
1270 * When returning from sigwait, we want
1271 * the old mask to be restored after the
1272 * signal handler has finished. Thus, we
1273 * save it here and mark the sigacts structure
1276 uth
= ut
; /* wait for it to be delivered to us */
1277 ut
->uu_oldmask
= ut
->uu_sigmask
;
1278 ut
->uu_flag
|= UT_SAS_OLDMASK
;
1279 if (siglist
== (sigset_t
)0) {
1283 /* SIGKILL and SIGSTOP are not maskable as well */
1284 ut
->uu_sigmask
= ~(siglist
|sigcantmask
);
1285 ut
->uu_sigwait
= siglist
;
1287 /* No Continuations for now */
1288 error
= msleep((caddr_t
)&ut
->uu_sigwait
, &p
->p_mlock
, PPAUSE
|PCATCH
, "pause", 0);
1290 if (error
== ERESTART
)
1293 sigw
= (ut
->uu_sigwait
& siglist
);
1294 ut
->uu_sigmask
= ut
->uu_oldmask
;
1296 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
1300 signum
= ffs((unsigned int)sigw
);
1302 panic("sigwait with no signal wakeup");
1303 /* Clear the pending signal in the thread it was delivered */
1304 uth
->uu_siglist
&= ~(sigmask(signum
));
1307 DTRACE_PROC2(signal__clear
, int, signum
, siginfo_t
*, &(ut
->t_dtrace_siginfo
));
1311 if (uap
->sig
!= USER_ADDR_NULL
)
1312 error
= copyout(&signum
, uap
->sig
, sizeof(int));
1321 sigaltstack(__unused proc_t p
, struct sigaltstack_args
*uap
, __unused
int32_t *retval
)
1323 struct kern_sigaltstack ss
;
1324 struct kern_sigaltstack
*pstk
;
1326 struct uthread
*uth
;
1329 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
1331 pstk
= &uth
->uu_sigstk
;
1332 if ((uth
->uu_flag
& UT_ALTSTACK
) == 0)
1333 uth
->uu_sigstk
.ss_flags
|= SA_DISABLE
;
1334 onstack
= pstk
->ss_flags
& SA_ONSTACK
;
1336 if (IS_64BIT_PROCESS(p
)) {
1337 struct user64_sigaltstack ss64
;
1338 sigaltstack_kern_to_user64(pstk
, &ss64
);
1339 error
= copyout(&ss64
, uap
->oss
, sizeof(ss64
));
1341 struct user32_sigaltstack ss32
;
1342 sigaltstack_kern_to_user32(pstk
, &ss32
);
1343 error
= copyout(&ss32
, uap
->oss
, sizeof(ss32
));
1348 if (uap
->nss
== USER_ADDR_NULL
)
1350 if (IS_64BIT_PROCESS(p
)) {
1351 struct user64_sigaltstack ss64
;
1352 error
= copyin(uap
->nss
, &ss64
, sizeof(ss64
));
1353 sigaltstack_user64_to_kern(&ss64
, &ss
);
1355 struct user32_sigaltstack ss32
;
1356 error
= copyin(uap
->nss
, &ss32
, sizeof(ss32
));
1357 sigaltstack_user32_to_kern(&ss32
, &ss
);
1361 if ((ss
.ss_flags
& ~SA_DISABLE
) != 0) {
1365 if (ss
.ss_flags
& SA_DISABLE
) {
1366 /* if we are here we are not in the signal handler ;so no need to check */
1367 if (uth
->uu_sigstk
.ss_flags
& SA_ONSTACK
)
1369 uth
->uu_flag
&= ~UT_ALTSTACK
;
1370 uth
->uu_sigstk
.ss_flags
= ss
.ss_flags
;
1375 /* The older stacksize was 8K, enforce that one so no compat problems */
1376 #define OLDMINSIGSTKSZ 8*1024
1377 if (ss
.ss_size
< OLDMINSIGSTKSZ
)
1379 uth
->uu_flag
|= UT_ALTSTACK
;
1385 kill(proc_t cp
, struct kill_args
*uap
, __unused
int32_t *retval
)
1388 kauth_cred_t uc
= kauth_cred_get();
1389 int posix
= uap
->posix
; /* !0 if posix behaviour desired */
1391 AUDIT_ARG(pid
, uap
->pid
);
1392 AUDIT_ARG(signum
, uap
->signum
);
1394 if ((u_int
)uap
->signum
>= NSIG
)
1397 /* kill single process */
1398 if ((p
= proc_find(uap
->pid
)) == NULL
) {
1399 if ((p
= pzfind(uap
->pid
)) != NULL
) {
1401 * IEEE Std 1003.1-2001: return success
1402 * when killing a zombie.
1408 AUDIT_ARG(process
, p
);
1409 if (!cansignal(cp
, uc
, p
, uap
->signum
, 0)) {
1414 psignal(p
, uap
->signum
);
1419 case -1: /* broadcast signal */
1420 return (killpg1(cp
, uap
->signum
, 0, 1, posix
));
1421 case 0: /* signal own process group */
1422 return (killpg1(cp
, uap
->signum
, 0, 0, posix
));
1423 default: /* negative explicit process group */
1424 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0, posix
));
1430 killpg1_filt(proc_t p
, void * arg
)
1432 struct killpg1_filtargs
* kfargp
= (struct killpg1_filtargs
*)arg
;
1433 proc_t cp
= kfargp
->cp
;
1434 int posix
= kfargp
->posix
;
1437 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1438 (!posix
&& p
== cp
))
1446 killpg1_pgrpfilt(proc_t p
, __unused
void * arg
)
1448 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1449 (p
->p_stat
== SZOMB
))
1458 killpg1_callback(proc_t p
, void * arg
)
1460 struct killpg1_iterargs
* kargp
= (struct killpg1_iterargs
*)arg
;
1461 proc_t cp
= kargp
->cp
;
1462 kauth_cred_t uc
= kargp
->uc
; /* refcounted by the caller safe to use internal fields */
1463 int signum
= kargp
->signum
;
1464 int * nfoundp
= kargp
->nfoundp
;
1469 if ((kargp
->zombie
!= 0) && ((p
->p_listflag
& P_LIST_EXITED
) == P_LIST_EXITED
))
1474 error
= cansignal(cp
, uc
, p
, signum
, zombie
);
1477 if (error
!= 0 && nfoundp
!= NULL
) {
1482 if (cansignal(cp
, uc
, p
, signum
, 0) == 0)
1483 return(PROC_RETURNED
);
1485 if (nfoundp
!= NULL
) {
1493 return(PROC_RETURNED
);
1497 * Common code for kill process group/broadcast kill.
1498 * cp is calling process.
1501 killpg1(proc_t cp
, int signum
, int pgid
, int all
, int posix
)
1506 struct killpg1_iterargs karg
;
1507 struct killpg1_filtargs kfarg
;
1510 uc
= kauth_cred_proc_ref(cp
);
1515 kfarg
.posix
= posix
;
1520 karg
.nfoundp
= &nfound
;
1521 karg
.signum
= signum
;
1524 proc_iterate((PROC_ALLPROCLIST
| PROC_ZOMBPROCLIST
), killpg1_callback
, &karg
, killpg1_filt
, (void *)&kfarg
);
1529 * zero pgid means send to my process group.
1531 pgrp
= proc_pgrp(cp
);
1533 pgrp
= pgfind(pgid
);
1540 karg
.nfoundp
= &nfound
;
1542 karg
.signum
= signum
;
1547 /* PGRP_DROPREF drops the pgrp refernce */
1548 pgrp_iterate(pgrp
, PGRP_BLOCKITERATE
| PGRP_DROPREF
, killpg1_callback
, &karg
,
1549 killpg1_pgrpfilt
, NULL
);
1551 error
= (nfound
? 0 : (posix
? EPERM
: ESRCH
));
1553 kauth_cred_unref(&uc
);
1559 * Send a signal to a process group.
1562 gsignal(int pgid
, int signum
)
1566 if (pgid
&& (pgrp
= pgfind(pgid
))) {
1567 pgsignal(pgrp
, signum
, 0);
1573 * Send a signal to a process group. If checkctty is 1,
1574 * limit to members which have a controlling terminal.
1578 pgsignal_filt(proc_t p
, void * arg
)
1580 int checkctty
= *(int*)arg
;
1582 if ((checkctty
== 0) || p
->p_flag
& P_CONTROLT
)
1590 pgsignal_callback(proc_t p
, void * arg
)
1592 int signum
= *(int*)arg
;
1595 return(PROC_RETURNED
);
1600 pgsignal(struct pgrp
*pgrp
, int signum
, int checkctty
)
1602 if (pgrp
!= PGRP_NULL
) {
1603 pgrp_iterate(pgrp
, PGRP_BLOCKITERATE
, pgsignal_callback
, &signum
, pgsignal_filt
, &checkctty
);
1609 tty_pgsignal(struct tty
*tp
, int signum
, int checkctty
)
1614 if (pg
!= PGRP_NULL
) {
1615 pgrp_iterate(pg
, PGRP_BLOCKITERATE
, pgsignal_callback
, &signum
, pgsignal_filt
, &checkctty
);
1620 * Send a signal caused by a trap to a specific thread.
1623 threadsignal(thread_t sig_actthread
, int signum
, mach_exception_code_t code
)
1625 struct uthread
*uth
;
1626 struct task
* sig_task
;
1630 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1633 mask
= sigmask(signum
);
1634 if ((mask
& threadmask
) == 0)
1636 sig_task
= get_threadtask(sig_actthread
);
1637 p
= (proc_t
)(get_bsdtask_info(sig_task
));
1639 uth
= get_bsdthread_info(sig_actthread
);
1640 if (uth
->uu_flag
& UT_VFORK
)
1644 if (!(p
->p_lflag
& P_LTRACED
) && (p
->p_sigignore
& mask
)) {
1649 uth
->uu_siglist
|= mask
;
1650 uth
->uu_code
= code
;
1653 /* mark on process as well */
1654 signal_setast(sig_actthread
);
1657 static kern_return_t
1658 get_signalthread(proc_t p
, int signum
, thread_t
* thr
)
1660 struct uthread
*uth
;
1661 sigset_t mask
= sigmask(signum
);
1662 thread_t sig_thread
;
1663 struct task
* sig_task
= p
->task
;
1668 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
1669 sig_thread
= p
->p_vforkact
;
1670 kret
= check_actforsig(sig_task
, sig_thread
, 1);
1671 if (kret
== KERN_SUCCESS
) {
1673 return(KERN_SUCCESS
);
1675 return(KERN_FAILURE
);
1679 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1680 if(((uth
->uu_flag
& UT_NO_SIGMASK
)== 0) &&
1681 (((uth
->uu_sigmask
& mask
) == 0) || (uth
->uu_sigwait
& mask
))) {
1682 if (check_actforsig(p
->task
, uth
->uu_context
.vc_thread
, 1) == KERN_SUCCESS
) {
1683 *thr
= uth
->uu_context
.vc_thread
;
1685 return(KERN_SUCCESS
);
1690 if (get_signalact(p
->task
, thr
, 1) == KERN_SUCCESS
) {
1691 return(KERN_SUCCESS
);
1694 return(KERN_FAILURE
);
1698 * Send the signal to the process. If the signal has an action, the action
1699 * is usually performed by the target process rather than the caller; we add
1700 * the signal to the set of pending signals for the process.
1703 * o When a stop signal is sent to a sleeping process that takes the
1704 * default action, the process is stopped without awakening it.
1705 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1706 * regardless of the signal action (eg, blocked or ignored).
1708 * Other ignored signals are discarded immediately.
1711 psignal_internal(proc_t p
, task_t task
, thread_t thread
, int flavor
, int signum
)
1714 user_addr_t action
= USER_ADDR_NULL
;
1716 thread_t sig_thread
;
1717 register task_t sig_task
;
1719 struct uthread
*uth
;
1723 kauth_cred_t my_cred
;
1725 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1726 panic("psignal signal number");
1727 mask
= sigmask(signum
);
1728 prop
= sigprop
[signum
];
1731 if(rdebug_proc
&& (p
!= PROC_NULL
) && (p
== rdebug_proc
)) {
1734 #endif /* SIGNAL_DEBUG */
1737 * We will need the task pointer later. Grab it now to
1738 * check for a zombie process. Also don't send signals
1739 * to kernel internal tasks.
1741 if (flavor
& PSIG_VFORK
) {
1743 sig_thread
= thread
;
1745 } else if (flavor
& PSIG_THREAD
) {
1746 sig_task
= get_threadtask(thread
);
1747 sig_thread
= thread
;
1748 sig_proc
= (proc_t
)get_bsdtask_info(sig_task
);
1751 sig_thread
= (struct thread
*)0;
1755 if ((sig_task
== TASK_NULL
) || is_kerneltask(sig_task
))
1759 * do not send signals to the process that has the thread
1760 * doing a reboot(). Not doing so will mark that thread aborted
1761 * and can cause IO failures wich will cause data loss. There's
1762 * also no need to send a signal to a process that is in the middle
1763 * of being torn down.
1765 if (ISSET(sig_proc
->p_flag
, P_REBOOT
) ||
1766 ISSET(sig_proc
->p_lflag
, P_LEXIT
))
1769 if( (flavor
& (PSIG_VFORK
| PSIG_THREAD
)) == 0) {
1770 proc_knote(sig_proc
, NOTE_SIGNAL
| signum
);
1773 if ((flavor
& PSIG_LOCKED
)== 0)
1774 proc_signalstart(sig_proc
, 0);
1777 * Deliver the signal to the first thread in the task. This
1778 * allows single threaded applications which use signals to
1779 * be able to be linked with multithreaded libraries. We have
1780 * an implicit reference to the current thread, but need
1781 * an explicit one otherwise. The thread reference keeps
1782 * the corresponding task data structures around too. This
1783 * reference is released by thread_deallocate.
1787 if (((flavor
& PSIG_VFORK
) == 0) && ((sig_proc
->p_lflag
& P_LTRACED
) == 0) && (sig_proc
->p_sigignore
& mask
)) {
1788 DTRACE_PROC3(signal__discard
, thread_t
, sig_thread
, proc_t
, sig_proc
, int, signum
);
1792 if (flavor
& PSIG_VFORK
) {
1794 act_set_astbsd(sig_thread
);
1795 kret
= KERN_SUCCESS
;
1796 } else if (flavor
& PSIG_THREAD
) {
1797 /* If successful return with ast set */
1798 kret
= check_actforsig(sig_task
, sig_thread
, 1);
1800 /* If successful return with ast set */
1801 kret
= get_signalthread(sig_proc
, signum
, &sig_thread
);
1803 if (kret
!= KERN_SUCCESS
) {
1806 #endif /* SIGNAL_DEBUG */
1811 uth
= get_bsdthread_info(sig_thread
);
1814 * If proc is traced, always give parent a chance.
1817 if ((flavor
& PSIG_VFORK
) == 0) {
1818 if (sig_proc
->p_lflag
& P_LTRACED
)
1822 * If the signal is being ignored,
1823 * then we forget about it immediately.
1824 * (Note: we don't set SIGCONT in p_sigignore,
1825 * and if it is set to SIG_IGN,
1826 * action will be SIG_DFL here.)
1828 if (sig_proc
->p_sigignore
& mask
)
1830 if (uth
->uu_sigwait
& mask
)
1831 action
= KERN_SIG_WAIT
;
1832 else if (uth
->uu_sigmask
& mask
)
1833 action
= KERN_SIG_HOLD
;
1834 else if (sig_proc
->p_sigcatch
& mask
)
1835 action
= KERN_SIG_CATCH
;
1842 proc_lock(sig_proc
);
1844 if (sig_proc
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1845 (sig_proc
->p_lflag
& P_LTRACED
) == 0)
1846 sig_proc
->p_nice
= NZERO
;
1849 uth
->uu_siglist
&= ~stopsigmask
;
1851 if (prop
& SA_STOP
) {
1854 * If sending a tty stop signal to a member of an orphaned
1855 * process group, discard the signal here if the action
1856 * is default; don't stop the process below if sleeping,
1857 * and don't clear any pending SIGCONT.
1859 proc_unlock(sig_proc
);
1860 pg
= proc_pgrp(sig_proc
);
1861 if (prop
& SA_TTYSTOP
&& pg
->pg_jobc
== 0 &&
1862 action
== SIG_DFL
) {
1867 proc_lock(sig_proc
);
1868 uth
->uu_siglist
&= ~contsigmask
;
1871 uth
->uu_siglist
|= mask
;
1873 * Repost AST incase sigthread has processed
1874 * ast and missed signal post.
1876 if (action
== KERN_SIG_CATCH
)
1877 act_set_astbsd(sig_thread
);
1881 * Defer further processing for signals which are held,
1882 * except that stopped processes must be continued by SIGCONT.
1884 /* vfork will not go thru as action is SIG_DFL */
1885 if ((action
== KERN_SIG_HOLD
) && ((prop
& SA_CONT
) == 0 || sig_proc
->p_stat
!= SSTOP
)) {
1886 proc_unlock(sig_proc
);
1890 * SIGKILL priority twiddling moved here from above because
1891 * it needs sig_thread. Could merge it into large switch
1892 * below if we didn't care about priority for tracing
1893 * as SIGKILL's action is always SIG_DFL.
1895 if ((signum
== SIGKILL
) && (sig_proc
->p_nice
> NZERO
)) {
1896 sig_proc
->p_nice
= NZERO
;
1900 * Process is traced - wake it up (if not already
1901 * stopped) so that it can discover the signal in
1902 * issig() and stop for the parent.
1904 if (sig_proc
->p_lflag
& P_LTRACED
) {
1905 if (sig_proc
->p_stat
!= SSTOP
)
1908 proc_unlock(sig_proc
);
1912 if ((flavor
& PSIG_VFORK
) != 0)
1915 if (action
== KERN_SIG_WAIT
) {
1918 * DTrace proc signal-clear returns a siginfo_t. Collect the needed info.
1920 r_uid
= kauth_getruid(); /* per thread credential; protected by our thread context */
1922 bzero((caddr_t
)&(uth
->t_dtrace_siginfo
), sizeof(uth
->t_dtrace_siginfo
));
1924 uth
->t_dtrace_siginfo
.si_signo
= signum
;
1925 uth
->t_dtrace_siginfo
.si_pid
= current_proc()->p_pid
;
1926 uth
->t_dtrace_siginfo
.si_status
= W_EXITCODE(signum
, 0);
1927 uth
->t_dtrace_siginfo
.si_uid
= r_uid
;
1928 uth
->t_dtrace_siginfo
.si_code
= 0;
1930 uth
->uu_sigwait
= mask
;
1931 uth
->uu_siglist
&= ~mask
;
1932 wakeup(&uth
->uu_sigwait
);
1933 /* if it is SIGCONT resume whole process */
1934 if (prop
& SA_CONT
) {
1935 OSBitOrAtomic(P_CONTINUED
, &sig_proc
->p_flag
);
1936 sig_proc
->p_contproc
= current_proc()->p_pid
;
1938 proc_unlock(sig_proc
);
1939 (void) task_resume_internal(sig_task
);
1942 proc_unlock(sig_proc
);
1946 if (action
!= SIG_DFL
) {
1948 * User wants to catch the signal.
1949 * Wake up the thread, but don't un-suspend it
1950 * (except for SIGCONT).
1952 if (prop
& SA_CONT
) {
1953 OSBitOrAtomic(P_CONTINUED
, &sig_proc
->p_flag
);
1954 proc_unlock(sig_proc
);
1955 (void) task_resume_internal(sig_task
);
1956 proc_lock(sig_proc
);
1957 sig_proc
->p_stat
= SRUN
;
1958 } else if (sig_proc
->p_stat
== SSTOP
) {
1959 proc_unlock(sig_proc
);
1963 * Fill out siginfo structure information to pass to the
1964 * signalled process/thread sigaction handler, when it
1965 * wakes up. si_code is 0 because this is an ordinary
1966 * signal, not a SIGCHLD, and so si_status is the signal
1967 * number itself, instead of the child process exit status.
1968 * We shift this left because it will be shifted right before
1969 * it is passed to user space. kind of ugly to use W_EXITCODE
1970 * this way, but it beats defining a new macro.
1972 * Note: Avoid the SIGCHLD recursion case!
1974 if (signum
!= SIGCHLD
) {
1975 proc_unlock(sig_proc
);
1976 r_uid
= kauth_getruid();
1977 proc_lock(sig_proc
);
1979 sig_proc
->si_pid
= current_proc()->p_pid
;
1980 sig_proc
->si_status
= W_EXITCODE(signum
, 0);
1981 sig_proc
->si_uid
= r_uid
;
1982 sig_proc
->si_code
= 0;
1987 /* Default action - varies */
1988 if (mask
& stopsigmask
) {
1990 * These are the signals which by default
1993 * Don't clog system with children of init
1994 * stopped from the keyboard.
1996 if (!(prop
& SA_STOP
) && sig_proc
->p_pptr
== initproc
) {
1997 proc_unlock(sig_proc
);
1998 psignal_locked(sig_proc
, SIGKILL
);
1999 proc_lock(sig_proc
);
2000 uth
->uu_siglist
&= ~mask
;
2001 proc_unlock(sig_proc
);
2007 * if task hasn't already been stopped by
2010 uth
->uu_siglist
&= ~mask
;
2011 if (sig_proc
->p_stat
!= SSTOP
) {
2012 sig_proc
->p_xstat
= signum
;
2013 sig_proc
->p_stat
= SSTOP
;
2014 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &sig_proc
->p_flag
);
2015 sig_proc
->p_lflag
&= ~P_LWAITED
;
2016 proc_unlock(sig_proc
);
2018 pp
= proc_parentholdref(sig_proc
);
2020 if (( pp
!= PROC_NULL
) && ((pp
->p_flag
& P_NOCLDSTOP
) == 0)) {
2022 my_cred
= kauth_cred_proc_ref(sig_proc
);
2023 r_uid
= kauth_cred_getruid(my_cred
);
2024 kauth_cred_unref(&my_cred
);
2026 proc_lock(sig_proc
);
2027 pp
->si_pid
= sig_proc
->p_pid
;
2029 * POSIX: sigaction for a stopped child
2030 * when sent to the parent must set the
2031 * child's signal number into si_status.
2033 if (signum
!= SIGSTOP
)
2034 pp
->si_status
= WEXITSTATUS(sig_proc
->p_xstat
);
2036 pp
->si_status
= W_EXITCODE(signum
, signum
);
2037 pp
->si_code
= CLD_STOPPED
;
2039 proc_unlock(sig_proc
);
2041 psignal(pp
, SIGCHLD
);
2043 if (pp
!= PROC_NULL
)
2044 proc_parentdropref(pp
, 0);
2046 proc_unlock(sig_proc
);
2050 DTRACE_PROC3(signal__send
, thread_t
, sig_thread
, proc_t
, p
, int, signum
);
2053 * enters switch with sig_proc lock held but dropped when
2054 * gets out of switch
2058 * Signals ignored by default have been dealt
2059 * with already, since their bits are on in
2065 * Kill signal always sets process running and
2069 * Process will be running after 'run'
2071 sig_proc
->p_stat
= SRUN
;
2073 * In scenarios where suspend/resume are racing
2074 * the signal we are missing AST_BSD by the time
2075 * we get here, set again to avoid races. This
2076 * was the scenario with spindump enabled shutdowns.
2077 * We would need to cover this approp down the line.
2079 act_set_astbsd(sig_thread
);
2080 thread_abort(sig_thread
);
2081 proc_unlock(sig_proc
);
2087 * Let the process run. If it's sleeping on an
2088 * event, it remains so.
2090 OSBitOrAtomic(P_CONTINUED
, &sig_proc
->p_flag
);
2091 sig_proc
->p_contproc
= sig_proc
->p_pid
;
2093 proc_unlock(sig_proc
);
2094 (void) task_resume_internal(sig_task
);
2095 proc_lock(sig_proc
);
2097 * When processing a SIGCONT, we need to check
2098 * to see if there are signals pending that
2099 * were not delivered because we had been
2100 * previously stopped. If that's the case,
2101 * we need to thread_abort_safely() to trigger
2102 * interruption of the current system call to
2103 * cause their handlers to fire. If it's only
2104 * the SIGCONT, then don't wake up.
2106 if (((flavor
& (PSIG_VFORK
|PSIG_THREAD
)) == 0) && (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~sig_proc
->p_sigignore
) & ~mask
)) {
2107 uth
->uu_siglist
&= ~mask
;
2108 sig_proc
->p_stat
= SRUN
;
2112 uth
->uu_siglist
&= ~mask
;
2113 sig_proc
->p_stat
= SRUN
;
2114 proc_unlock(sig_proc
);
2119 * A signal which has a default action of killing
2120 * the process, and for which there is no handler,
2121 * needs to act like SIGKILL
2123 if (((flavor
& (PSIG_VFORK
|PSIG_THREAD
)) == 0) && (action
== SIG_DFL
) && (prop
& SA_KILL
)) {
2124 sig_proc
->p_stat
= SRUN
;
2125 proc_unlock(sig_proc
);
2126 thread_abort(sig_thread
);
2131 * All other signals wake up the process, but don't
2134 if (sig_proc
->p_stat
== SSTOP
) {
2135 proc_unlock(sig_proc
);
2145 * If we're being traced (possibly because someone attached us
2146 * while we were stopped), check for a signal from the debugger.
2148 if (sig_proc
->p_stat
== SSTOP
) {
2149 if ((sig_proc
->p_lflag
& P_LTRACED
) != 0 && sig_proc
->p_xstat
!= 0)
2150 uth
->uu_siglist
|= sigmask(sig_proc
->p_xstat
);
2151 if ((flavor
& PSIG_VFORK
) != 0) {
2152 sig_proc
->p_stat
= SRUN
;
2154 proc_unlock(sig_proc
);
2157 * setrunnable(p) in BSD and
2158 * Wake up the thread if it is interruptible.
2160 sig_proc
->p_stat
= SRUN
;
2161 proc_unlock(sig_proc
);
2162 if ((flavor
& PSIG_VFORK
) == 0)
2163 thread_abort_safely(sig_thread
);
2166 if ((flavor
& PSIG_LOCKED
)== 0) {
2167 proc_signalend(sig_proc
, 0);
2172 psignal(proc_t p
, int signum
)
2174 psignal_internal(p
, NULL
, NULL
, 0, signum
);
2178 psignal_locked(proc_t p
, int signum
)
2180 psignal_internal(p
, NULL
, NULL
, PSIG_LOCKED
, signum
);
2184 psignal_vfork(proc_t p
, task_t new_task
, thread_t thread
, int signum
)
2186 psignal_internal(p
, new_task
, thread
, PSIG_VFORK
, signum
);
2190 psignal_uthread(thread_t thread
, int signum
)
2192 psignal_internal(PROC_NULL
, TASK_NULL
, thread
, PSIG_THREAD
, signum
);
2197 * If the current process has received a signal (should be caught or cause
2198 * termination, should interrupt current syscall), return the signal number.
2199 * Stop signals with default action are processed immediately, then cleared;
2200 * they aren't returned. This is checked after each entry to the system for
2201 * a syscall or trap (though this can usually be done without calling issignal
2202 * by checking the pending signal masks in the CURSIG macro.) The normal call
2205 * while (signum = CURSIG(curproc))
2209 issignal_locked(proc_t p
)
2211 int signum
, mask
, prop
, sigbits
;
2213 struct uthread
* ut
;
2215 kauth_cred_t my_cred
;
2219 cur_act
= current_thread();
2222 if(rdebug_proc
&& (p
== rdebug_proc
)) {
2225 #endif /* SIGNAL_DEBUG */
2228 * Try to grab the signal lock.
2230 if (sig_try_locked(p
) <= 0) {
2234 proc_signalstart(p
, 1);
2236 ut
= get_bsdthread_info(cur_act
);
2238 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2240 if (p
->p_lflag
& P_LPPWAIT
)
2241 sigbits
&= ~stopsigmask
;
2242 if (sigbits
== 0) { /* no signal to send */
2247 signum
= ffs((long)sigbits
);
2248 mask
= sigmask(signum
);
2249 prop
= sigprop
[signum
];
2252 * We should see pending but ignored signals
2253 * only if P_LTRACED was on when they were posted.
2255 if (mask
& p
->p_sigignore
&& (p
->p_lflag
& P_LTRACED
) == 0) {
2256 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2259 if (p
->p_lflag
& P_LTRACED
&& (p
->p_lflag
& P_LPPWAIT
) == 0) {
2262 * If traced, always stop, and stay
2263 * stopped until released by the debugger.
2265 /* ptrace debugging */
2266 p
->p_xstat
= signum
;
2268 if (p
->p_lflag
& P_LSIGEXC
) {
2270 p
->sigwait_thread
= cur_act
;
2272 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
2273 p
->p_lflag
&= ~P_LWAITED
;
2274 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2275 proc_signalend(p
, 1);
2277 do_bsdexception(EXC_SOFTWARE
, EXC_SOFT_SIGNAL
, signum
);
2279 proc_signalstart(p
, 1);
2282 my_cred
= kauth_cred_proc_ref(p
);
2283 r_uid
= kauth_cred_getruid(my_cred
);
2284 kauth_cred_unref(&my_cred
);
2286 pp
= proc_parentholdref(p
);
2287 if (pp
!= PROC_NULL
) {
2290 pp
->si_pid
= p
->p_pid
;
2291 pp
->si_status
= p
->p_xstat
;
2292 pp
->si_code
= CLD_TRAPPED
;
2299 * XXX Have to really stop for debuggers;
2300 * XXX stop() doesn't do the right thing.
2303 task_suspend_internal(task
);
2307 p
->sigwait_thread
= cur_act
;
2309 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
2310 p
->p_lflag
&= ~P_LWAITED
;
2311 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2313 proc_signalend(p
, 1);
2316 if (pp
!= PROC_NULL
) {
2317 psignal(pp
, SIGCHLD
);
2319 wakeup((caddr_t
)pp
);
2320 proc_parentdropref(pp
, 1);
2324 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
2325 thread_block(THREAD_CONTINUE_NULL
);
2327 proc_signalstart(p
, 1);
2331 p
->sigwait_thread
= NULL
;
2332 wakeup((caddr_t
)&p
->sigwait_thread
);
2335 * This code is to detect when gdb is killed
2336 * even as the traced program is attached.
2337 * pgsignal would get the SIGKILL to traced program
2338 * That's what we are trying to see (I hope)
2340 if (ut
->uu_siglist
& sigmask(SIGKILL
)) {
2342 * Wait event may still be outstanding;
2343 * clear it, since sig_lock_to_exit will
2346 clear_wait(current_thread(), THREAD_INTERRUPTED
);
2347 sig_lock_to_exit(p
);
2349 * Since this thread will be resumed
2350 * to allow the current syscall to
2351 * be completed, must save u_qsave
2352 * before calling exit(). (Since exit()
2353 * calls closef() which can trash u_qsave.)
2355 proc_signalend(p
, 1);
2357 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_FRCEXIT
) | DBG_FUNC_NONE
,
2358 p
->p_pid
, W_EXITCODE(0, SIGKILL
), 2, 0, 0);
2359 exit1(p
, W_EXITCODE(0, SIGKILL
), (int *)NULL
);
2365 * We may have to quit
2367 if (thread_should_abort(current_thread())) {
2372 * If parent wants us to take the signal,
2373 * then it will leave it in p->p_xstat;
2374 * otherwise we just look for signals again.
2376 signum
= p
->p_xstat
;
2380 * Put the new signal into p_siglist. If the
2381 * signal is being masked, look for other signals.
2383 mask
= sigmask(signum
);
2384 ut
->uu_siglist
|= mask
;
2385 if (ut
->uu_sigmask
& mask
)
2390 * Decide whether the signal should be returned.
2391 * Return the signal's number, or fall through
2392 * to clear it from the pending mask.
2395 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2399 * Don't take default actions on system processes.
2401 if (p
->p_ppid
== 0) {
2404 * Are you sure you want to ignore SIGSEGV
2407 printf("Process (pid %d) got signal %d\n",
2410 break; /* == ignore */
2414 * If there is a pending stop signal to process
2415 * with default action, stop here,
2416 * then clear the signal. However,
2417 * if process is member of an orphaned
2418 * process group, ignore tty stop signals.
2420 if (prop
& SA_STOP
) {
2425 if (p
->p_lflag
& P_LTRACED
||
2426 (pg
->pg_jobc
== 0 &&
2427 prop
& SA_TTYSTOP
)) {
2430 break; /* == ignore */
2433 if (p
->p_stat
!= SSTOP
) {
2435 p
->p_xstat
= signum
;
2438 p
->p_lflag
&= ~P_LWAITED
;
2441 pp
= proc_parentholdref(p
);
2443 if ((pp
!= PROC_NULL
) && ((pp
->p_flag
& P_NOCLDSTOP
) == 0)) {
2444 my_cred
= kauth_cred_proc_ref(p
);
2445 r_uid
= kauth_cred_getruid(my_cred
);
2446 kauth_cred_unref(&my_cred
);
2449 pp
->si_pid
= p
->p_pid
;
2450 pp
->si_status
= WEXITSTATUS(p
->p_xstat
);
2451 pp
->si_code
= CLD_STOPPED
;
2455 psignal(pp
, SIGCHLD
);
2457 if (pp
!= PROC_NULL
)
2458 proc_parentdropref(pp
, 0);
2462 } else if (prop
& SA_IGNORE
) {
2464 * Except for SIGCONT, shouldn't get here.
2465 * Default action is to ignore; drop it.
2467 break; /* == ignore */
2469 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2479 * Masking above should prevent us ever trying
2480 * to take action on an ignored signal other
2481 * than SIGCONT, unless process is traced.
2483 if ((prop
& SA_CONT
) == 0 &&
2484 (p
->p_lflag
& P_LTRACED
) == 0)
2485 printf("issignal\n");
2486 break; /* == ignore */
2490 * This signal has an action, let
2491 * postsig() process it.
2493 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2497 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2501 proc_signalend(p
, 1);
2505 /* called from _sleep */
2509 int signum
, mask
, prop
, sigbits
;
2511 struct uthread
* ut
;
2515 cur_act
= current_thread();
2517 ut
= get_bsdthread_info(cur_act
);
2519 if (ut
->uu_siglist
== 0)
2522 if (((ut
->uu_siglist
& ~ut
->uu_sigmask
) == 0) && ((p
->p_lflag
& P_LTRACED
) == 0))
2525 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2528 if (p
->p_lflag
& P_LPPWAIT
)
2529 sigbits
&= ~stopsigmask
;
2530 if (sigbits
== 0) { /* no signal to send */
2534 signum
= ffs((long)sigbits
);
2535 mask
= sigmask(signum
);
2536 prop
= sigprop
[signum
];
2537 sigbits
&= ~mask
; /* take the signal out */
2540 * We should see pending but ignored signals
2541 * only if P_LTRACED was on when they were posted.
2543 if (mask
& p
->p_sigignore
&& (p
->p_lflag
& P_LTRACED
) == 0) {
2547 if (p
->p_lflag
& P_LTRACED
&& (p
->p_lflag
& P_LPPWAIT
) == 0) {
2552 * Decide whether the signal should be returned.
2553 * Return the signal's number, or fall through
2554 * to clear it from the pending mask.
2557 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2561 * Don't take default actions on system processes.
2563 if (p
->p_ppid
== 0) {
2566 * Are you sure you want to ignore SIGSEGV
2569 printf("Process (pid %d) got signal %d\n",
2572 break; /* == ignore */
2576 * If there is a pending stop signal to process
2577 * with default action, stop here,
2578 * then clear the signal. However,
2579 * if process is member of an orphaned
2580 * process group, ignore tty stop signals.
2582 if (prop
& SA_STOP
) {
2587 if (p
->p_lflag
& P_LTRACED
||
2588 (pg
->pg_jobc
== 0 &&
2589 prop
& SA_TTYSTOP
)) {
2591 break; /* == ignore */
2596 } else if (prop
& SA_IGNORE
) {
2598 * Except for SIGCONT, shouldn't get here.
2599 * Default action is to ignore; drop it.
2601 break; /* == ignore */
2609 * Masking above should prevent us ever trying
2610 * to take action on an ignored signal other
2611 * than SIGCONT, unless process is traced.
2613 if ((prop
& SA_CONT
) == 0 &&
2614 (p
->p_lflag
& P_LTRACED
) == 0)
2615 printf("issignal\n");
2616 break; /* == ignore */
2620 * This signal has an action, let
2621 * postsig() process it.
2630 * Put the argument process into the stopped state and notify the parent
2631 * via wakeup. Signals are handled elsewhere. The process must not be
2635 stop(proc_t p
, proc_t parent
)
2637 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
2638 if ((parent
!= PROC_NULL
) && (parent
->p_stat
!= SSTOP
)) {
2640 wakeup((caddr_t
)parent
);
2643 (void) task_suspend_internal(p
->task
);
2647 * Take the action for the specified signal
2648 * from the current set of pending signals.
2651 postsig_locked(int signum
)
2653 proc_t p
= current_proc();
2654 struct sigacts
*ps
= p
->p_sigacts
;
2655 user_addr_t catcher
;
2657 int mask
, returnmask
;
2658 struct uthread
* ut
;
2664 * This must be called on master cpu
2666 if (cpu_number() != master_cpu
)
2667 panic("psig not on master");
2671 * Try to grab the signal lock.
2673 if (sig_try_locked(p
) <= 0) {
2677 proc_signalstart(p
, 1);
2679 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2680 mask
= sigmask(signum
);
2681 ut
->uu_siglist
&= ~mask
;
2682 catcher
= ps
->ps_sigact
[signum
];
2683 if (catcher
== SIG_DFL
) {
2685 * Default catcher, where the default is to kill
2686 * the process. (Other cases were ignored above.)
2688 sig_lock_to_exit(p
);
2689 p
->p_acflag
|= AXSIG
;
2690 if (sigprop
[signum
] & SA_CORE
) {
2691 p
->p_sigacts
->ps_sig
= signum
;
2692 proc_signalend(p
, 1);
2694 if (coredump(p
, 0, 0) == 0)
2695 signum
|= WCOREFLAG
;
2697 proc_signalend(p
, 1);
2702 bzero((caddr_t
)&(ut
->t_dtrace_siginfo
), sizeof(ut
->t_dtrace_siginfo
));
2704 ut
->t_dtrace_siginfo
.si_signo
= signum
;
2705 ut
->t_dtrace_siginfo
.si_pid
= p
->si_pid
;
2706 ut
->t_dtrace_siginfo
.si_uid
= p
->si_uid
;
2707 ut
->t_dtrace_siginfo
.si_status
= WEXITSTATUS(p
->si_status
);
2709 /* Fire DTrace proc:::fault probe when signal is generated by hardware. */
2711 case SIGILL
: case SIGBUS
: case SIGSEGV
: case SIGFPE
: case SIGTRAP
:
2712 DTRACE_PROC2(fault
, int, (int)(ut
->uu_code
), siginfo_t
*, &(ut
->t_dtrace_siginfo
));
2719 DTRACE_PROC3(signal__handle
, int, signum
, siginfo_t
*, &(ut
->t_dtrace_siginfo
),
2720 void (*)(void), SIG_DFL
);
2723 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_FRCEXIT
) | DBG_FUNC_NONE
,
2724 p
->p_pid
, W_EXITCODE(0, signum
), 3, 0, 0);
2725 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
2730 * If we get here, the signal must be caught.
2733 if (catcher
== SIG_IGN
|| (ut
->uu_sigmask
& mask
))
2735 "postsig: processing masked or ignored signal\n");
2739 * Set the new mask value and also defer further
2740 * occurences of this signal.
2742 * Special case: user has done a sigpause. Here the
2743 * current mask is not of interest, but rather the
2744 * mask from before the sigpause is what we want
2745 * restored after the signal processing is completed.
2747 if (ut
->uu_flag
& UT_SAS_OLDMASK
) {
2748 returnmask
= ut
->uu_oldmask
;
2749 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
2752 returnmask
= ut
->uu_sigmask
;
2753 ut
->uu_sigmask
|= ps
->ps_catchmask
[signum
];
2754 if ((ps
->ps_signodefer
& mask
) == 0)
2755 ut
->uu_sigmask
|= mask
;
2756 if ((signum
!= SIGILL
) && (signum
!= SIGTRAP
) && (ps
->ps_sigreset
& mask
)) {
2757 if ((signum
!= SIGCONT
) && (sigprop
[signum
] & SA_IGNORE
))
2758 p
->p_sigignore
|= mask
;
2759 ps
->ps_sigact
[signum
] = SIG_DFL
;
2760 ps
->ps_siginfo
&= ~mask
;
2761 ps
->ps_signodefer
&= ~mask
;
2764 if (ps
->ps_sig
!= signum
) {
2770 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_nsignals
);
2771 sendsig(p
, catcher
, signum
, returnmask
, code
);
2773 proc_signalend(p
, 1);
2777 * Attach a signal knote to the list of knotes for this process.
2779 * Signal knotes share the knote list with proc knotes. This
2780 * could be avoided by using a signal-specific knote list, but
2781 * probably isn't worth the trouble.
2785 filt_sigattach(struct knote
*kn
)
2787 proc_t p
= current_proc(); /* can attach only to oneself */
2791 kn
->kn_ptr
.p_proc
= p
;
2792 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
2794 KNOTE_ATTACH(&p
->p_klist
, kn
);
2796 proc_klist_unlock();
2802 * remove the knote from the process list, if it hasn't already
2803 * been removed by exit processing.
2807 filt_sigdetach(struct knote
*kn
)
2809 proc_t p
= kn
->kn_ptr
.p_proc
;
2812 kn
->kn_ptr
.p_proc
= NULL
;
2813 KNOTE_DETACH(&p
->p_klist
, kn
);
2814 proc_klist_unlock();
2818 * Post an event to the signal filter. Because we share the same list
2819 * as process knotes, we have to filter out and handle only signal events.
2821 * We assume that we process fdfree() before we post the NOTE_EXIT for
2822 * a process during exit. Therefore, since signal filters can only be
2823 * set up "in-process", we should have already torn down the kqueue
2824 * hosting the EVFILT_SIGNAL knote and should never see NOTE_EXIT.
2827 filt_signal(struct knote
*kn
, long hint
)
2830 if (hint
& NOTE_SIGNAL
) {
2831 hint
&= ~NOTE_SIGNAL
;
2833 if (kn
->kn_id
== (unsigned int)hint
)
2835 } else if (hint
& NOTE_EXIT
) {
2836 panic("filt_signal: detected NOTE_EXIT event");
2839 return (kn
->kn_data
!= 0);
2843 filt_signaltouch(struct knote
*kn
, struct kevent64_s
*kev
, long type
)
2847 case EVENT_REGISTER
:
2848 kn
->kn_sfflags
= kev
->fflags
;
2849 kn
->kn_sdata
= kev
->data
;
2852 *kev
= kn
->kn_kevent
;
2853 if (kn
->kn_flags
& EV_CLEAR
) {
2859 panic("filt_machporttouch() - invalid type (%ld)", type
);
2862 proc_klist_unlock();
2866 bsd_ast(thread_t thread
)
2868 proc_t p
= current_proc();
2869 struct uthread
*ut
= get_bsdthread_info(thread
);
2872 static int bsd_init_done
= 0;
2877 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
2878 pc
= get_useraddr();
2879 addupc_task(p
, pc
, 1);
2880 OSBitAndAtomic(~((uint32_t)P_OWEUPC
), &p
->p_flag
);
2883 if (timerisset(&p
->p_vtimer_user
.it_value
)) {
2886 task_vtimer_update(p
->task
, TASK_VTIMER_USER
, µsecs
);
2888 if (!itimerdecr(p
, &p
->p_vtimer_user
, microsecs
)) {
2889 if (timerisset(&p
->p_vtimer_user
.it_value
))
2890 task_vtimer_set(p
->task
, TASK_VTIMER_USER
);
2892 task_vtimer_clear(p
->task
, TASK_VTIMER_USER
);
2894 psignal(p
, SIGVTALRM
);
2898 if (timerisset(&p
->p_vtimer_prof
.it_value
)) {
2901 task_vtimer_update(p
->task
, TASK_VTIMER_PROF
, µsecs
);
2903 if (!itimerdecr(p
, &p
->p_vtimer_prof
, microsecs
)) {
2904 if (timerisset(&p
->p_vtimer_prof
.it_value
))
2905 task_vtimer_set(p
->task
, TASK_VTIMER_PROF
);
2907 task_vtimer_clear(p
->task
, TASK_VTIMER_PROF
);
2909 psignal(p
, SIGPROF
);
2913 if (timerisset(&p
->p_rlim_cpu
)) {
2916 task_vtimer_update(p
->task
, TASK_VTIMER_RLIM
, (uint32_t *) &tv
.tv_usec
);
2919 if (p
->p_rlim_cpu
.tv_sec
> 0 || p
->p_rlim_cpu
.tv_usec
> tv
.tv_usec
) {
2921 timersub(&p
->p_rlim_cpu
, &tv
, &p
->p_rlim_cpu
);
2925 timerclear(&p
->p_rlim_cpu
);
2928 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
2930 psignal(p
, SIGXCPU
);
2935 if (ut
->t_dtrace_sig
) {
2936 uint8_t dt_action_sig
= ut
->t_dtrace_sig
;
2937 ut
->t_dtrace_sig
= 0;
2938 psignal(p
, dt_action_sig
);
2941 if (ut
->t_dtrace_stop
) {
2942 ut
->t_dtrace_stop
= 0;
2944 p
->p_dtrace_stop
= 1;
2946 (void)task_suspend_internal(p
->task
);
2949 if (ut
->t_dtrace_resumepid
) {
2950 proc_t resumeproc
= proc_find(ut
->t_dtrace_resumepid
);
2951 ut
->t_dtrace_resumepid
= 0;
2952 if (resumeproc
!= PROC_NULL
) {
2953 proc_lock(resumeproc
);
2954 /* We only act on processes stopped by dtrace */
2955 if (resumeproc
->p_dtrace_stop
) {
2956 resumeproc
->p_dtrace_stop
= 0;
2957 proc_unlock(resumeproc
);
2958 task_resume_internal(resumeproc
->task
);
2961 proc_unlock(resumeproc
);
2963 proc_rele(resumeproc
);
2967 #endif /* CONFIG_DTRACE */
2970 if (CHECK_SIGNALS(p
, current_thread(), ut
)) {
2971 while ( (signum
= issignal_locked(p
)) )
2972 postsig_locked(signum
);
2976 if (!bsd_init_done
) {
2983 /* ptrace set runnable */
2985 pt_setrunnable(proc_t p
)
2991 if (p
->p_lflag
& P_LTRACED
) {
2996 wakeup((caddr_t
)&(p
->sigwait
));
2997 if ((p
->p_lflag
& P_LSIGEXC
) == 0) { // 5878479
3010 mach_exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
3014 return(bsd_exception(exc
, codes
, 2));
3018 proc_pendingsignals(proc_t p
, sigset_t mask
)
3020 struct uthread
* uth
;
3025 /* If the process is in proc exit return no signal info */
3026 if (p
->p_lflag
& P_LPEXIT
) {
3030 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
3032 uth
= (struct uthread
*)get_bsdthread_info(th
);
3034 bits
= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
3040 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
3041 bits
|= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
3049 thread_issignal(proc_t p
, thread_t th
, sigset_t mask
)
3051 struct uthread
* uth
;
3055 uth
= (struct uthread
*)get_bsdthread_info(th
);
3057 bits
= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
3064 * Allow external reads of the sigprop array.
3067 hassigprop(int sig
, int prop
)
3069 return (sigprop
[sig
] & prop
);
3073 pgsigio(pid_t pgid
, int sig
)
3075 proc_t p
= PROC_NULL
;
3078 gsignal(-(pgid
), sig
);
3080 else if (pgid
> 0 && (p
= proc_find(pgid
)) != 0)
3087 proc_signalstart(proc_t p
, int locked
)
3092 if(p
->p_signalholder
== current_thread())
3093 panic("proc_signalstart: thread attempting to signal a process for which it holds the signal lock");
3096 while ((p
->p_lflag
& P_LINSIGNAL
) == P_LINSIGNAL
)
3097 msleep(&p
->p_sigmask
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
3100 p
->p_lflag
|= P_LINSIGNAL
;
3101 p
->p_signalholder
= current_thread();
3107 proc_signalend(proc_t p
, int locked
)
3111 p
->p_lflag
&= ~P_LINSIGNAL
;
3113 if (p
->p_sigwaitcnt
> 0)
3114 wakeup(&p
->p_sigmask
);
3116 p
->p_signalholder
= NULL
;
3122 sig_lock_to_exit(proc_t p
)
3124 thread_t self
= current_thread();
3126 p
->exit_thread
= self
;
3130 task_wait(p
->task
, FALSE
);
3136 sig_try_locked(proc_t p
)
3138 thread_t self
= current_thread();
3140 while (p
->sigwait
|| p
->exit_thread
) {
3141 if (p
->exit_thread
) {
3144 msleep((caddr_t
)&p
->sigwait_thread
, &p
->p_mlock
, PCATCH
| PDROP
, 0, 0);
3145 if (thread_should_abort(self
)) {
3147 * Terminate request - clean up.