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/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
, 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 (uc
->cr_ruid
== my_cred
->cr_ruid
||
338 uc
->cr_ruid
== my_cred
->cr_svuid
||
339 kauth_cred_getuid(uc
) == my_cred
->cr_ruid
||
340 kauth_cred_getuid(uc
) == my_cred
->cr_svuid
) {
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 * onnon-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 if (signum
== SIGFPE
) {
628 if (sa
->sa_handler
== SIG_DFL
|| sa
->sa_handler
== SIG_IGN
)
629 thread_enable_fpe(thread
, 0);
631 thread_enable_fpe(thread
, 1);
635 * Set bit in p_sigignore for signals that are set to SIG_IGN,
636 * and for signals set to SIG_DFL where the default is to ignore.
637 * However, don't put SIGCONT in p_sigignore,
638 * as we have to restart the process.
640 if (sa
->sa_handler
== SIG_IGN
||
641 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
643 clear_procsiglist(p
, bit
, in_sigstart
);
644 if (signum
!= SIGCONT
)
645 p
->p_sigignore
|= bit
; /* easier in psignal */
646 p
->p_sigcatch
&= ~bit
;
648 p
->p_sigignore
&= ~bit
;
649 if (sa
->sa_handler
== SIG_DFL
)
650 p
->p_sigcatch
&= ~bit
;
652 p
->p_sigcatch
|= bit
;
658 * Initialize signal state for process 0;
659 * set to ignore signals that are ignored by default.
666 for (i
= 0; i
< NSIG
; i
++)
667 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
668 p
->p_sigignore
|= sigmask(i
);
672 * Reset signals for an exec of the specified process.
675 execsigs(proc_t p
, thread_t thread
)
677 struct sigacts
*ps
= p
->p_sigacts
;
681 ut
= (struct uthread
*)get_bsdthread_info(thread
);
684 * transfer saved signal states from the process
685 * back to the current thread.
687 * NOTE: We do this without the process locked,
688 * because we are guaranteed to be single-threaded
689 * by this point in exec and the p_siglist is
690 * only accessed by threads inside the process.
692 ut
->uu_siglist
|= p
->p_siglist
;
696 * Reset caught signals. Held signals remain held
697 * through p_sigmask (unless they were caught,
698 * and are now ignored by default).
700 while (p
->p_sigcatch
) {
701 nc
= ffs((long)p
->p_sigcatch
);
703 p
->p_sigcatch
&= ~mask
;
704 if (sigprop
[nc
] & SA_IGNORE
) {
706 p
->p_sigignore
|= mask
;
707 ut
->uu_siglist
&= ~mask
;
709 ps
->ps_sigact
[nc
] = SIG_DFL
;
713 * Reset stack state to the user stack.
714 * Clear set of signals caught on the signal stack.
717 ut
->uu_sigstk
.ss_flags
= SA_DISABLE
;
718 ut
->uu_sigstk
.ss_size
= 0;
719 ut
->uu_sigstk
.ss_sp
= USER_ADDR_NULL
;
720 ut
->uu_flag
&= ~UT_ALTSTACK
;
722 ps
->ps_sigonstack
= 0;
726 * Manipulate signal mask.
727 * Note that we receive new mask, not pointer,
728 * and return old mask as return value;
729 * the library stub does the rest.
732 sigprocmask(proc_t p
, struct sigprocmask_args
*uap
, __unused
int32_t *retval
)
735 sigset_t oldmask
, nmask
;
736 user_addr_t omask
= uap
->omask
;
739 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
740 oldmask
= ut
->uu_sigmask
;
742 if (uap
->mask
== USER_ADDR_NULL
) {
743 /* just want old mask */
746 error
= copyin(uap
->mask
, &nmask
, sizeof(sigset_t
));
752 block_procsigmask(p
, (nmask
& ~sigcantmask
));
753 signal_setast(current_thread());
757 unblock_procsigmask(p
, (nmask
& ~sigcantmask
));
758 signal_setast(current_thread());
762 set_procsigmask(p
, (nmask
& ~sigcantmask
));
763 signal_setast(current_thread());
771 if (!error
&& omask
!= USER_ADDR_NULL
)
772 copyout(&oldmask
, omask
, sizeof(sigset_t
));
777 sigpending(__unused proc_t p
, struct sigpending_args
*uap
, __unused
int32_t *retval
)
782 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
783 pendlist
= ut
->uu_siglist
;
786 copyout(&pendlist
, uap
->osv
, sizeof(sigset_t
));
791 * Suspend process until signal, providing mask to be set
792 * in the meantime. Note nonstandard calling convention:
793 * libc stub passes mask, not pointer, to save a copyin.
797 sigcontinue(__unused
int error
)
799 // struct uthread *ut = get_bsdthread_info(current_thread());
800 unix_syscall_return(EINTR
);
804 sigsuspend(proc_t p
, struct sigsuspend_args
*uap
, int32_t *retval
)
806 __pthread_testcancel(1);
807 return(sigsuspend_nocancel(p
, (struct sigsuspend_nocancel_args
*)uap
, retval
));
811 sigsuspend_nocancel(proc_t p
, struct sigsuspend_nocancel_args
*uap
, __unused
int32_t *retval
)
815 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
818 * When returning from sigpause, we want
819 * the old mask to be restored after the
820 * signal handler has finished. Thus, we
821 * save it here and mark the sigacts structure
824 ut
->uu_oldmask
= ut
->uu_sigmask
;
825 ut
->uu_flag
|= UT_SAS_OLDMASK
;
826 ut
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
827 (void) tsleep0((caddr_t
) p
, PPAUSE
|PCATCH
, "pause", 0, sigcontinue
);
828 /* always return EINTR rather than ERESTART... */
834 __disable_threadsignal(__unused proc_t p
,
835 __unused
struct __disable_threadsignal_args
*uap
,
836 __unused
int32_t *retval
)
840 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
842 /* No longer valid to have any signal delivered */
843 uth
->uu_flag
|= (UT_NO_SIGMASK
| UT_CANCELDISABLE
);
850 __pthread_testcancel(int presyscall
)
853 thread_t self
= current_thread();
854 struct uthread
* uthread
;
856 uthread
= (struct uthread
*)get_bsdthread_info(self
);
859 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
861 if ((uthread
->uu_flag
& (UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
862 if(presyscall
!= 0) {
863 unix_syscall_return(EINTR
);
866 thread_abort_safely(self
);
873 __pthread_markcancel(__unused proc_t p
,
874 struct __pthread_markcancel_args
*uap
, __unused
int32_t *retval
)
876 thread_act_t target_act
;
880 target_act
= (thread_act_t
)port_name_to_thread(uap
->thread_port
);
882 if (target_act
== THR_ACT_NULL
)
885 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
887 /* if the thread is in vfork do not cancel */
888 if ((uth
->uu_flag
& (UT_VFORK
| UT_CANCEL
| UT_CANCELED
)) == 0) {
889 uth
->uu_flag
|= (UT_CANCEL
| UT_NO_SIGMASK
);
890 if (((uth
->uu_flag
& UT_NOTCANCELPT
) == 0)
891 && ((uth
->uu_flag
& UT_CANCELDISABLE
) == 0))
892 thread_abort_safely(target_act
);
895 thread_deallocate(target_act
);
899 /* if action =0 ; return the cancellation state ,
900 * if marked for cancellation, make the thread canceled
901 * if action = 1 ; Enable the cancel handling
902 * if action = 2; Disable the cancel handling
905 __pthread_canceled(__unused proc_t p
,
906 struct __pthread_canceled_args
*uap
, __unused
int32_t *retval
)
910 int action
= uap
->action
;
912 thread
= current_thread();
913 uth
= (struct uthread
*)get_bsdthread_info(thread
);
917 uth
->uu_flag
&= ~UT_CANCELDISABLE
;
920 uth
->uu_flag
|= UT_CANCELDISABLE
;
924 /* if the thread is in vfork do not cancel */
925 if((uth
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
926 uth
->uu_flag
&= ~UT_CANCEL
;
927 uth
->uu_flag
|= (UT_CANCELED
| UT_NO_SIGMASK
);
936 __posix_sem_syscall_return(kern_return_t kern_result
)
940 if (kern_result
== KERN_SUCCESS
)
942 else if (kern_result
== KERN_ABORTED
)
944 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
948 unix_syscall_return(error
);
949 /* does not return */
952 #if OLD_SEMWAIT_SIGNAL
958 * EFAULT if timespec is NULL
961 __old_semwait_signal(proc_t p
, struct __old_semwait_signal_args
*uap
,
964 __pthread_testcancel(0);
965 return(__old_semwait_signal_nocancel(p
, (struct __old_semwait_signal_nocancel_args
*)uap
, retval
));
969 __old_semwait_signal_nocancel(proc_t p
, struct __old_semwait_signal_nocancel_args
*uap
,
970 __unused
int32_t *retval
)
973 kern_return_t kern_result
;
975 mach_timespec_t then
;
977 struct user_timespec ts
;
978 boolean_t truncated_timeout
= FALSE
;
982 if (IS_64BIT_PROCESS(p
)) {
983 struct user64_timespec ts64
;
984 error
= copyin(uap
->ts
, &ts64
, sizeof(ts64
));
985 ts
.tv_sec
= ts64
.tv_sec
;
986 ts
.tv_nsec
= ts64
.tv_nsec
;
988 struct user32_timespec ts32
;
989 error
= copyin(uap
->ts
, &ts32
, sizeof(ts32
));
990 ts
.tv_sec
= ts32
.tv_sec
;
991 ts
.tv_nsec
= ts32
.tv_nsec
;
998 if ((ts
.tv_sec
& 0xFFFFFFFF00000000ULL
) != 0) {
999 ts
.tv_sec
= 0xFFFFFFFF;
1001 truncated_timeout
= TRUE
;
1004 if (uap
->relative
) {
1005 then
.tv_sec
= ts
.tv_sec
;
1006 then
.tv_nsec
= ts
.tv_nsec
;
1010 /* if time has elapsed, set time to null timepsec to bailout rightaway */
1011 if (now
.tv_sec
== ts
.tv_sec
?
1012 now
.tv_nsec
> ts
.tv_nsec
:
1013 now
.tv_sec
> ts
.tv_sec
) {
1017 then
.tv_sec
= ts
.tv_sec
- now
.tv_sec
;
1018 then
.tv_nsec
= ts
.tv_nsec
- now
.tv_nsec
;
1019 if (then
.tv_nsec
< 0) {
1020 then
.tv_nsec
+= NSEC_PER_SEC
;
1026 if (uap
->mutex_sem
== 0)
1027 kern_result
= semaphore_timedwait_trap_internal((mach_port_name_t
)uap
->cond_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
1029 kern_result
= semaphore_timedwait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
1033 if (uap
->mutex_sem
== 0)
1034 kern_result
= semaphore_wait_trap_internal(uap
->cond_sem
, __posix_sem_syscall_return
);
1037 kern_result
= semaphore_wait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, __posix_sem_syscall_return
);
1040 if (kern_result
== KERN_SUCCESS
&& !truncated_timeout
)
1042 else if (kern_result
== KERN_SUCCESS
&& truncated_timeout
)
1043 return(EINTR
); /* simulate an exceptional condition because Mach doesn't support a longer timeout */
1044 else if (kern_result
== KERN_ABORTED
)
1046 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
1051 #endif /* OLD_SEMWAIT_SIGNAL*/
1054 * Returns: 0 Success
1058 * EFAULT if timespec is NULL
1061 __semwait_signal(proc_t p
, struct __semwait_signal_args
*uap
,
1064 __pthread_testcancel(0);
1065 return(__semwait_signal_nocancel(p
, (struct __semwait_signal_nocancel_args
*)uap
, retval
));
1069 __semwait_signal_nocancel(__unused proc_t p
, struct __semwait_signal_nocancel_args
*uap
,
1070 __unused
int32_t *retval
)
1073 kern_return_t kern_result
;
1074 mach_timespec_t then
;
1075 struct timespec now
;
1076 struct user_timespec ts
;
1077 boolean_t truncated_timeout
= FALSE
;
1081 ts
.tv_sec
= uap
->tv_sec
;
1082 ts
.tv_nsec
= uap
->tv_nsec
;
1084 if ((ts
.tv_sec
& 0xFFFFFFFF00000000ULL
) != 0) {
1085 ts
.tv_sec
= 0xFFFFFFFF;
1087 truncated_timeout
= TRUE
;
1090 if (uap
->relative
) {
1091 then
.tv_sec
= ts
.tv_sec
;
1092 then
.tv_nsec
= ts
.tv_nsec
;
1096 /* if time has elapsed, set time to null timepsec to bailout rightaway */
1097 if (now
.tv_sec
== ts
.tv_sec
?
1098 now
.tv_nsec
> ts
.tv_nsec
:
1099 now
.tv_sec
> ts
.tv_sec
) {
1103 then
.tv_sec
= ts
.tv_sec
- now
.tv_sec
;
1104 then
.tv_nsec
= ts
.tv_nsec
- now
.tv_nsec
;
1105 if (then
.tv_nsec
< 0) {
1106 then
.tv_nsec
+= NSEC_PER_SEC
;
1112 if (uap
->mutex_sem
== 0)
1113 kern_result
= semaphore_timedwait_trap_internal((mach_port_name_t
)uap
->cond_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
1115 kern_result
= semaphore_timedwait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
1119 if (uap
->mutex_sem
== 0)
1120 kern_result
= semaphore_wait_trap_internal(uap
->cond_sem
, __posix_sem_syscall_return
);
1123 kern_result
= semaphore_wait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, __posix_sem_syscall_return
);
1126 if (kern_result
== KERN_SUCCESS
&& !truncated_timeout
)
1128 else if (kern_result
== KERN_SUCCESS
&& truncated_timeout
)
1129 return(EINTR
); /* simulate an exceptional condition because Mach doesn't support a longer timeout */
1130 else if (kern_result
== KERN_ABORTED
)
1132 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
1140 __pthread_kill(__unused proc_t p
, struct __pthread_kill_args
*uap
,
1141 __unused
int32_t *retval
)
1143 thread_t target_act
;
1145 int signum
= uap
->sig
;
1146 struct uthread
*uth
;
1148 target_act
= (thread_t
)port_name_to_thread(uap
->thread_port
);
1150 if (target_act
== THREAD_NULL
)
1152 if ((u_int
)signum
>= NSIG
) {
1157 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
1159 if (uth
->uu_flag
& UT_NO_SIGMASK
) {
1165 psignal_uthread(target_act
, signum
);
1167 thread_deallocate(target_act
);
1173 __pthread_sigmask(__unused proc_t p
, struct __pthread_sigmask_args
*uap
,
1174 __unused
int32_t *retval
)
1176 user_addr_t set
= uap
->set
;
1177 user_addr_t oset
= uap
->oset
;
1183 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1184 oldset
= ut
->uu_sigmask
;
1186 if (set
== USER_ADDR_NULL
) {
1187 /* need only old mask */
1191 error
= copyin(set
, &nset
, sizeof(sigset_t
));
1197 ut
->uu_sigmask
|= (nset
& ~sigcantmask
);
1201 ut
->uu_sigmask
&= ~(nset
);
1202 signal_setast(current_thread());
1206 ut
->uu_sigmask
= (nset
& ~sigcantmask
);
1207 signal_setast(current_thread());
1215 if (!error
&& oset
!= USER_ADDR_NULL
)
1216 copyout(&oldset
, oset
, sizeof(sigset_t
));
1222 * Returns: 0 Success
1228 __sigwait(proc_t p
, struct __sigwait_args
*uap
, int32_t *retval
)
1230 __pthread_testcancel(1);
1231 return(__sigwait_nocancel(p
, (struct __sigwait_nocancel_args
*)uap
, retval
));
1235 __sigwait_nocancel(proc_t p
, struct __sigwait_nocancel_args
*uap
, __unused
int32_t *retval
)
1238 struct uthread
*uth
;
1245 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1247 if (uap
->set
== USER_ADDR_NULL
)
1250 error
= copyin(uap
->set
, &mask
, sizeof(sigset_t
));
1254 siglist
= (mask
& ~sigcantmask
);
1260 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
1264 proc_signalstart(p
, 1);
1265 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1266 if ( (sigw
= uth
->uu_siglist
& siglist
) ) {
1270 proc_signalend(p
, 1);
1274 /* The signal was pending on a thread */
1278 * When returning from sigwait, we want
1279 * the old mask to be restored after the
1280 * signal handler has finished. Thus, we
1281 * save it here and mark the sigacts structure
1284 uth
= ut
; /* wait for it to be delivered to us */
1285 ut
->uu_oldmask
= ut
->uu_sigmask
;
1286 ut
->uu_flag
|= UT_SAS_OLDMASK
;
1287 if (siglist
== (sigset_t
)0) {
1291 /* SIGKILL and SIGSTOP are not maskable as well */
1292 ut
->uu_sigmask
= ~(siglist
|sigcantmask
);
1293 ut
->uu_sigwait
= siglist
;
1295 /* No Continuations for now */
1296 error
= msleep((caddr_t
)&ut
->uu_sigwait
, &p
->p_mlock
, PPAUSE
|PCATCH
, "pause", 0);
1298 if (error
== ERESTART
)
1301 sigw
= (ut
->uu_sigwait
& siglist
);
1302 ut
->uu_sigmask
= ut
->uu_oldmask
;
1304 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
1308 signum
= ffs((unsigned int)sigw
);
1310 panic("sigwait with no signal wakeup");
1311 /* Clear the pending signal in the thread it was delivered */
1312 uth
->uu_siglist
&= ~(sigmask(signum
));
1315 DTRACE_PROC2(signal__clear
, int, signum
, siginfo_t
*, &(ut
->t_dtrace_siginfo
));
1319 if (uap
->sig
!= USER_ADDR_NULL
)
1320 error
= copyout(&signum
, uap
->sig
, sizeof(int));
1329 sigaltstack(__unused proc_t p
, struct sigaltstack_args
*uap
, __unused
int32_t *retval
)
1331 struct kern_sigaltstack ss
;
1332 struct kern_sigaltstack
*pstk
;
1334 struct uthread
*uth
;
1337 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
1339 pstk
= &uth
->uu_sigstk
;
1340 if ((uth
->uu_flag
& UT_ALTSTACK
) == 0)
1341 uth
->uu_sigstk
.ss_flags
|= SA_DISABLE
;
1342 onstack
= pstk
->ss_flags
& SA_ONSTACK
;
1344 if (IS_64BIT_PROCESS(p
)) {
1345 struct user64_sigaltstack ss64
;
1346 sigaltstack_kern_to_user64(pstk
, &ss64
);
1347 error
= copyout(&ss64
, uap
->oss
, sizeof(ss64
));
1349 struct user32_sigaltstack ss32
;
1350 sigaltstack_kern_to_user32(pstk
, &ss32
);
1351 error
= copyout(&ss32
, uap
->oss
, sizeof(ss32
));
1356 if (uap
->nss
== USER_ADDR_NULL
)
1358 if (IS_64BIT_PROCESS(p
)) {
1359 struct user64_sigaltstack ss64
;
1360 error
= copyin(uap
->nss
, &ss64
, sizeof(ss64
));
1361 sigaltstack_user64_to_kern(&ss64
, &ss
);
1363 struct user32_sigaltstack ss32
;
1364 error
= copyin(uap
->nss
, &ss32
, sizeof(ss32
));
1365 sigaltstack_user32_to_kern(&ss32
, &ss
);
1369 if ((ss
.ss_flags
& ~SA_DISABLE
) != 0) {
1373 if (ss
.ss_flags
& SA_DISABLE
) {
1374 /* if we are here we are not in the signal handler ;so no need to check */
1375 if (uth
->uu_sigstk
.ss_flags
& SA_ONSTACK
)
1377 uth
->uu_flag
&= ~UT_ALTSTACK
;
1378 uth
->uu_sigstk
.ss_flags
= ss
.ss_flags
;
1383 /* The older stacksize was 8K, enforce that one so no compat problems */
1384 #define OLDMINSIGSTKSZ 8*1024
1385 if (ss
.ss_size
< OLDMINSIGSTKSZ
)
1387 uth
->uu_flag
|= UT_ALTSTACK
;
1393 kill(proc_t cp
, struct kill_args
*uap
, __unused
int32_t *retval
)
1396 kauth_cred_t uc
= kauth_cred_get();
1397 int posix
= uap
->posix
; /* !0 if posix behaviour desired */
1399 AUDIT_ARG(pid
, uap
->pid
);
1400 AUDIT_ARG(signum
, uap
->signum
);
1402 if ((u_int
)uap
->signum
>= NSIG
)
1405 /* kill single process */
1406 if ((p
= proc_find(uap
->pid
)) == NULL
) {
1407 if ((p
= pzfind(uap
->pid
)) != NULL
) {
1409 * IEEE Std 1003.1-2001: return success
1410 * when killing a zombie.
1416 AUDIT_ARG(process
, p
);
1417 if (!cansignal(cp
, uc
, p
, uap
->signum
, 0)) {
1422 psignal(p
, uap
->signum
);
1427 case -1: /* broadcast signal */
1428 return (killpg1(cp
, uap
->signum
, 0, 1, posix
));
1429 case 0: /* signal own process group */
1430 return (killpg1(cp
, uap
->signum
, 0, 0, posix
));
1431 default: /* negative explicit process group */
1432 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0, posix
));
1438 killpg1_filt(proc_t p
, void * arg
)
1440 struct killpg1_filtargs
* kfargp
= (struct killpg1_filtargs
*)arg
;
1441 proc_t cp
= kfargp
->cp
;
1442 int posix
= kfargp
->posix
;
1445 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1446 (!posix
&& p
== cp
))
1454 killpg1_pgrpfilt(proc_t p
, __unused
void * arg
)
1456 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1457 (p
->p_stat
== SZOMB
))
1466 killpg1_callback(proc_t p
, void * arg
)
1468 struct killpg1_iterargs
* kargp
= (struct killpg1_iterargs
*)arg
;
1469 proc_t cp
= kargp
->cp
;
1470 kauth_cred_t uc
= kargp
->uc
; /* refcounted by the caller safe to use internal fields */
1471 int signum
= kargp
->signum
;
1472 int * nfoundp
= kargp
->nfoundp
;
1477 if ((kargp
->zombie
!= 0) && ((p
->p_listflag
& P_LIST_EXITED
) == P_LIST_EXITED
))
1482 error
= cansignal(cp
, uc
, p
, signum
, zombie
);
1485 if (error
!= 0 && nfoundp
!= NULL
) {
1490 if (cansignal(cp
, uc
, p
, signum
, 0) == 0)
1491 return(PROC_RETURNED
);
1493 if (nfoundp
!= NULL
) {
1501 return(PROC_RETURNED
);
1505 * Common code for kill process group/broadcast kill.
1506 * cp is calling process.
1509 killpg1(proc_t cp
, int signum
, int pgid
, int all
, int posix
)
1514 struct killpg1_iterargs karg
;
1515 struct killpg1_filtargs kfarg
;
1518 uc
= kauth_cred_proc_ref(cp
);
1523 kfarg
.posix
= posix
;
1528 karg
.nfoundp
= &nfound
;
1529 karg
.signum
= signum
;
1532 proc_iterate((PROC_ALLPROCLIST
| PROC_ZOMBPROCLIST
), killpg1_callback
, &karg
, killpg1_filt
, (void *)&kfarg
);
1537 * zero pgid means send to my process group.
1539 pgrp
= proc_pgrp(cp
);
1541 pgrp
= pgfind(pgid
);
1548 karg
.nfoundp
= &nfound
;
1550 karg
.signum
= signum
;
1555 /* PGRP_DROPREF drops the pgrp refernce */
1556 pgrp_iterate(pgrp
, PGRP_BLOCKITERATE
| PGRP_DROPREF
, killpg1_callback
, &karg
,
1557 killpg1_pgrpfilt
, NULL
);
1559 error
= (nfound
? 0 : (posix
? EPERM
: ESRCH
));
1561 kauth_cred_unref(&uc
);
1567 * Send a signal to a process group.
1570 gsignal(int pgid
, int signum
)
1574 if (pgid
&& (pgrp
= pgfind(pgid
))) {
1575 pgsignal(pgrp
, signum
, 0);
1581 * Send a signal to a process group. If checkctty is 1,
1582 * limit to members which have a controlling terminal.
1586 pgsignal_filt(proc_t p
, void * arg
)
1588 int checkctty
= *(int*)arg
;
1590 if ((checkctty
== 0) || p
->p_flag
& P_CONTROLT
)
1598 pgsignal_callback(proc_t p
, void * arg
)
1600 int signum
= *(int*)arg
;
1603 return(PROC_RETURNED
);
1608 pgsignal(struct pgrp
*pgrp
, int signum
, int checkctty
)
1610 if (pgrp
!= PGRP_NULL
) {
1611 pgrp_iterate(pgrp
, PGRP_BLOCKITERATE
, pgsignal_callback
, &signum
, pgsignal_filt
, &checkctty
);
1617 tty_pgsignal(struct tty
*tp
, int signum
, int checkctty
)
1622 if (pg
!= PGRP_NULL
) {
1623 pgrp_iterate(pg
, PGRP_BLOCKITERATE
, pgsignal_callback
, &signum
, pgsignal_filt
, &checkctty
);
1628 * Send a signal caused by a trap to a specific thread.
1631 threadsignal(thread_t sig_actthread
, int signum
, mach_exception_code_t code
)
1633 struct uthread
*uth
;
1634 struct task
* sig_task
;
1638 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1641 mask
= sigmask(signum
);
1642 if ((mask
& threadmask
) == 0)
1644 sig_task
= get_threadtask(sig_actthread
);
1645 p
= (proc_t
)(get_bsdtask_info(sig_task
));
1647 uth
= get_bsdthread_info(sig_actthread
);
1648 if (uth
&& (uth
->uu_flag
& UT_VFORK
))
1652 if (!(p
->p_lflag
& P_LTRACED
) && (p
->p_sigignore
& mask
)) {
1657 uth
->uu_siglist
|= mask
;
1658 uth
->uu_code
= code
;
1661 /* mark on process as well */
1662 signal_setast(sig_actthread
);
1665 static kern_return_t
1666 get_signalthread(proc_t p
, int signum
, thread_t
* thr
)
1668 struct uthread
*uth
;
1669 sigset_t mask
= sigmask(signum
);
1670 thread_t sig_thread
;
1671 struct task
* sig_task
= p
->task
;
1676 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
1677 sig_thread
= p
->p_vforkact
;
1678 kret
= check_actforsig(sig_task
, sig_thread
, 1);
1679 if (kret
== KERN_SUCCESS
) {
1681 return(KERN_SUCCESS
);
1683 return(KERN_FAILURE
);
1687 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1688 if(((uth
->uu_flag
& UT_NO_SIGMASK
)== 0) &&
1689 (((uth
->uu_sigmask
& mask
) == 0) || (uth
->uu_sigwait
& mask
))) {
1690 if (check_actforsig(p
->task
, uth
->uu_context
.vc_thread
, 1) == KERN_SUCCESS
) {
1691 *thr
= uth
->uu_context
.vc_thread
;
1693 return(KERN_SUCCESS
);
1698 if (get_signalact(p
->task
, thr
, 1) == KERN_SUCCESS
) {
1699 return(KERN_SUCCESS
);
1702 return(KERN_FAILURE
);
1706 * Send the signal to the process. If the signal has an action, the action
1707 * is usually performed by the target process rather than the caller; we add
1708 * the signal to the set of pending signals for the process.
1711 * o When a stop signal is sent to a sleeping process that takes the
1712 * default action, the process is stopped without awakening it.
1713 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1714 * regardless of the signal action (eg, blocked or ignored).
1716 * Other ignored signals are discarded immediately.
1719 psignal_internal(proc_t p
, task_t task
, thread_t thread
, int flavor
, int signum
)
1722 sig_t action
= NULL
;
1724 thread_t sig_thread
;
1725 register task_t sig_task
;
1727 struct uthread
*uth
;
1731 kauth_cred_t my_cred
;
1733 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1734 panic("psignal signal number");
1735 mask
= sigmask(signum
);
1736 prop
= sigprop
[signum
];
1739 if(rdebug_proc
&& (p
!= PROC_NULL
) && (p
== rdebug_proc
)) {
1742 #endif /* SIGNAL_DEBUG */
1745 * We will need the task pointer later. Grab it now to
1746 * check for a zombie process. Also don't send signals
1747 * to kernel internal tasks.
1749 if (flavor
& PSIG_VFORK
) {
1751 sig_thread
= thread
;
1753 } else if (flavor
& PSIG_THREAD
) {
1754 sig_task
= get_threadtask(thread
);
1755 sig_thread
= thread
;
1756 sig_proc
= (proc_t
)get_bsdtask_info(sig_task
);
1760 sig_thread
= (struct thread
*)0;
1762 if (((sig_task
== TASK_NULL
) || is_kerneltask(sig_task
))) {
1767 * do not send signals to the process that has the thread
1768 * doing a reboot(). Not doing so will mark that thread aborted
1769 * and can cause IO failures wich will cause data loss.
1771 if (ISSET(sig_proc
->p_flag
, P_REBOOT
)) {
1775 if( (flavor
& (PSIG_VFORK
| PSIG_THREAD
)) == 0) {
1776 proc_knote(sig_proc
, NOTE_SIGNAL
| signum
);
1780 if ((flavor
& PSIG_LOCKED
)== 0)
1781 proc_signalstart(sig_proc
, 0);
1784 * Deliver the signal to the first thread in the task. This
1785 * allows single threaded applications which use signals to
1786 * be able to be linked with multithreaded libraries. We have
1787 * an implicit reference to the current thread, but need
1788 * an explicit one otherwise. The thread reference keeps
1789 * the corresponding task data structures around too. This
1790 * reference is released by thread_deallocate.
1794 if (((flavor
& PSIG_VFORK
) == 0) && ((sig_proc
->p_lflag
& P_LTRACED
) == 0) && (sig_proc
->p_sigignore
& mask
)) {
1795 DTRACE_PROC3(signal__discard
, thread_t
, sig_thread
, proc_t
, sig_proc
, int, signum
);
1799 if (flavor
& PSIG_VFORK
) {
1801 act_set_astbsd(sig_thread
);
1802 kret
= KERN_SUCCESS
;
1803 } else if (flavor
& PSIG_THREAD
) {
1804 /* If successful return with ast set */
1805 kret
= check_actforsig(sig_task
, sig_thread
, 1);
1807 /* If successful return with ast set */
1808 kret
= get_signalthread(sig_proc
, signum
, &sig_thread
);
1810 if (kret
!= KERN_SUCCESS
) {
1813 #endif /* SIGNAL_DEBUG */
1818 uth
= get_bsdthread_info(sig_thread
);
1821 * If proc is traced, always give parent a chance.
1824 if ((flavor
& PSIG_VFORK
) == 0) {
1825 if (sig_proc
->p_lflag
& P_LTRACED
)
1829 * If the signal is being ignored,
1830 * then we forget about it immediately.
1831 * (Note: we don't set SIGCONT in p_sigignore,
1832 * and if it is set to SIG_IGN,
1833 * action will be SIG_DFL here.)
1835 if (sig_proc
->p_sigignore
& mask
)
1837 if (uth
->uu_sigwait
& mask
)
1838 action
= KERN_SIG_WAIT
;
1839 else if (uth
->uu_sigmask
& mask
)
1840 action
= KERN_SIG_HOLD
;
1841 else if (sig_proc
->p_sigcatch
& mask
)
1842 action
= KERN_SIG_CATCH
;
1849 proc_lock(sig_proc
);
1851 if (sig_proc
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1852 (sig_proc
->p_lflag
& P_LTRACED
) == 0)
1853 sig_proc
->p_nice
= NZERO
;
1856 uth
->uu_siglist
&= ~stopsigmask
;
1858 if (prop
& SA_STOP
) {
1861 * If sending a tty stop signal to a member of an orphaned
1862 * process group, discard the signal here if the action
1863 * is default; don't stop the process below if sleeping,
1864 * and don't clear any pending SIGCONT.
1866 proc_unlock(sig_proc
);
1867 pg
= proc_pgrp(sig_proc
);
1868 if (prop
& SA_TTYSTOP
&& pg
->pg_jobc
== 0 &&
1869 action
== SIG_DFL
) {
1874 proc_lock(sig_proc
);
1875 uth
->uu_siglist
&= ~contsigmask
;
1878 uth
->uu_siglist
|= mask
;
1880 * Repost AST incase sigthread has processed
1881 * ast and missed signal post.
1883 if (action
== KERN_SIG_CATCH
)
1884 act_set_astbsd(sig_thread
);
1888 * Defer further processing for signals which are held,
1889 * except that stopped processes must be continued by SIGCONT.
1891 /* vfork will not go thru as action is SIG_DFL */
1892 if ((action
== KERN_SIG_HOLD
) && ((prop
& SA_CONT
) == 0 || sig_proc
->p_stat
!= SSTOP
)) {
1893 proc_unlock(sig_proc
);
1897 * SIGKILL priority twiddling moved here from above because
1898 * it needs sig_thread. Could merge it into large switch
1899 * below if we didn't care about priority for tracing
1900 * as SIGKILL's action is always SIG_DFL.
1902 if ((signum
== SIGKILL
) && (sig_proc
->p_nice
> NZERO
)) {
1903 sig_proc
->p_nice
= NZERO
;
1907 * Process is traced - wake it up (if not already
1908 * stopped) so that it can discover the signal in
1909 * issig() and stop for the parent.
1911 if (sig_proc
->p_lflag
& P_LTRACED
) {
1912 if (sig_proc
->p_stat
!= SSTOP
)
1915 proc_unlock(sig_proc
);
1919 if ((flavor
& PSIG_VFORK
) != 0)
1922 if (action
== KERN_SIG_WAIT
) {
1925 * DTrace proc signal-clear returns a siginfo_t. Collect the needed info.
1927 r_uid
= kauth_getruid(); /* per thread credential; protected by our thread context */
1929 bzero((caddr_t
)&(uth
->t_dtrace_siginfo
), sizeof(uth
->t_dtrace_siginfo
));
1931 uth
->t_dtrace_siginfo
.si_signo
= signum
;
1932 uth
->t_dtrace_siginfo
.si_pid
= current_proc()->p_pid
;
1933 uth
->t_dtrace_siginfo
.si_status
= W_EXITCODE(signum
, 0);
1934 uth
->t_dtrace_siginfo
.si_uid
= r_uid
;
1935 uth
->t_dtrace_siginfo
.si_code
= 0;
1937 uth
->uu_sigwait
= mask
;
1938 uth
->uu_siglist
&= ~mask
;
1939 wakeup(&uth
->uu_sigwait
);
1940 /* if it is SIGCONT resume whole process */
1941 if (prop
& SA_CONT
) {
1942 OSBitOrAtomic(P_CONTINUED
, &sig_proc
->p_flag
);
1943 sig_proc
->p_contproc
= current_proc()->p_pid
;
1945 proc_unlock(sig_proc
);
1946 (void) task_resume(sig_task
);
1949 proc_unlock(sig_proc
);
1953 if (action
!= SIG_DFL
) {
1955 * User wants to catch the signal.
1956 * Wake up the thread, but don't un-suspend it
1957 * (except for SIGCONT).
1959 if (prop
& SA_CONT
) {
1960 OSBitOrAtomic(P_CONTINUED
, &sig_proc
->p_flag
);
1961 proc_unlock(sig_proc
);
1962 (void) task_resume(sig_task
);
1963 proc_lock(sig_proc
);
1964 sig_proc
->p_stat
= SRUN
;
1965 } else if (sig_proc
->p_stat
== SSTOP
) {
1966 proc_unlock(sig_proc
);
1970 * Fill out siginfo structure information to pass to the
1971 * signalled process/thread sigaction handler, when it
1972 * wakes up. si_code is 0 because this is an ordinary
1973 * signal, not a SIGCHLD, and so si_status is the signal
1974 * number itself, instead of the child process exit status.
1975 * We shift this left because it will be shifted right before
1976 * it is passed to user space. kind of ugly to use W_EXITCODE
1977 * this way, but it beats defining a new macro.
1979 * Note: Avoid the SIGCHLD recursion case!
1981 if (signum
!= SIGCHLD
) {
1982 proc_unlock(sig_proc
);
1983 r_uid
= kauth_getruid();
1984 proc_lock(sig_proc
);
1986 sig_proc
->si_pid
= current_proc()->p_pid
;
1987 sig_proc
->si_status
= W_EXITCODE(signum
, 0);
1988 sig_proc
->si_uid
= r_uid
;
1989 sig_proc
->si_code
= 0;
1994 /* Default action - varies */
1995 if (mask
& stopsigmask
) {
1997 * These are the signals which by default
2000 * Don't clog system with children of init
2001 * stopped from the keyboard.
2003 if (!(prop
& SA_STOP
) && sig_proc
->p_pptr
== initproc
) {
2004 proc_unlock(sig_proc
);
2005 psignal_locked(sig_proc
, SIGKILL
);
2006 proc_lock(sig_proc
);
2007 uth
->uu_siglist
&= ~mask
;
2008 proc_unlock(sig_proc
);
2014 * if task hasn't already been stopped by
2017 uth
->uu_siglist
&= ~mask
;
2018 if (sig_proc
->p_stat
!= SSTOP
) {
2019 sig_proc
->p_xstat
= signum
;
2020 sig_proc
->p_stat
= SSTOP
;
2021 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &sig_proc
->p_flag
);
2022 sig_proc
->p_lflag
&= ~P_LWAITED
;
2023 proc_unlock(sig_proc
);
2025 pp
= proc_parentholdref(sig_proc
);
2027 if (( pp
!= PROC_NULL
) && ((pp
->p_flag
& P_NOCLDSTOP
) == 0)) {
2029 my_cred
= kauth_cred_proc_ref(sig_proc
);
2030 r_uid
= my_cred
->cr_ruid
;
2031 kauth_cred_unref(&my_cred
);
2033 proc_lock(sig_proc
);
2034 pp
->si_pid
= sig_proc
->p_pid
;
2036 * POSIX: sigaction for a stopped child
2037 * when sent to the parent must set the
2038 * child's signal number into si_status.
2040 if (signum
!= SIGSTOP
)
2041 pp
->si_status
= WEXITSTATUS(sig_proc
->p_xstat
);
2043 pp
->si_status
= W_EXITCODE(signum
, signum
);
2044 pp
->si_code
= CLD_STOPPED
;
2046 proc_unlock(sig_proc
);
2048 psignal(pp
, SIGCHLD
);
2050 if (pp
!= PROC_NULL
)
2051 proc_parentdropref(pp
, 0);
2053 proc_unlock(sig_proc
);
2057 DTRACE_PROC3(signal__send
, thread_t
, sig_thread
, proc_t
, p
, int, signum
);
2060 * enters switch with sig_proc lock held but dropped when
2061 * gets out of switch
2065 * Signals ignored by default have been dealt
2066 * with already, since their bits are on in
2072 * Kill signal always sets process running and
2076 * Process will be running after 'run'
2078 sig_proc
->p_stat
= SRUN
;
2079 proc_unlock(sig_proc
);
2080 thread_abort(sig_thread
);
2086 * Let the process run. If it's sleeping on an
2087 * event, it remains so.
2089 OSBitOrAtomic(P_CONTINUED
, &sig_proc
->p_flag
);
2090 sig_proc
->p_contproc
= sig_proc
->p_pid
;
2092 proc_unlock(sig_proc
);
2093 (void) task_resume(sig_task
);
2094 proc_lock(sig_proc
);
2096 * When processing a SIGCONT, we need to check
2097 * to see if there are signals pending that
2098 * were not delivered because we had been
2099 * previously stopped. If that's the case,
2100 * we need to thread_abort_safely() to trigger
2101 * interruption of the current system call to
2102 * cause their handlers to fire. If it's only
2103 * the SIGCONT, then don't wake up.
2105 if (((flavor
& (PSIG_VFORK
|PSIG_THREAD
)) == 0) && (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~sig_proc
->p_sigignore
) & ~mask
)) {
2106 uth
->uu_siglist
&= ~mask
;
2107 sig_proc
->p_stat
= SRUN
;
2111 uth
->uu_siglist
&= ~mask
;
2112 sig_proc
->p_stat
= SRUN
;
2113 proc_unlock(sig_proc
);
2118 * A signal which has a default action of killing
2119 * the process, and for which there is no handler,
2120 * needs to act like SIGKILL
2122 if (((flavor
& (PSIG_VFORK
|PSIG_THREAD
)) == 0) && (action
== SIG_DFL
) && (prop
& SA_KILL
)) {
2123 sig_proc
->p_stat
= SRUN
;
2124 proc_unlock(sig_proc
);
2125 thread_abort(sig_thread
);
2130 * All other signals wake up the process, but don't
2133 if (sig_proc
->p_stat
== SSTOP
) {
2134 proc_unlock(sig_proc
);
2144 * If we're being traced (possibly because someone attached us
2145 * while we were stopped), check for a signal from the debugger.
2147 if (sig_proc
->p_stat
== SSTOP
) {
2148 if ((sig_proc
->p_lflag
& P_LTRACED
) != 0 && sig_proc
->p_xstat
!= 0)
2149 uth
->uu_siglist
|= sigmask(sig_proc
->p_xstat
);
2150 if ((flavor
& PSIG_VFORK
) != 0) {
2151 sig_proc
->p_stat
= SRUN
;
2153 proc_unlock(sig_proc
);
2156 * setrunnable(p) in BSD and
2157 * Wake up the thread if it is interruptible.
2159 sig_proc
->p_stat
= SRUN
;
2160 proc_unlock(sig_proc
);
2161 if ((flavor
& PSIG_VFORK
) == 0)
2162 thread_abort_safely(sig_thread
);
2165 if ((flavor
& PSIG_LOCKED
)== 0) {
2166 proc_signalend(sig_proc
, 0);
2171 psignal(proc_t p
, int signum
)
2173 psignal_internal(p
, NULL
, NULL
, 0, signum
);
2177 psignal_locked(proc_t p
, int signum
)
2179 psignal_internal(p
, NULL
, NULL
, PSIG_LOCKED
, signum
);
2183 psignal_vfork(proc_t p
, task_t new_task
, thread_t thread
, int signum
)
2185 psignal_internal(p
, new_task
, thread
, PSIG_VFORK
, signum
);
2189 psignal_uthread(thread_t thread
, int signum
)
2191 psignal_internal(PROC_NULL
, TASK_NULL
, thread
, PSIG_THREAD
, signum
);
2196 * If the current process has received a signal (should be caught or cause
2197 * termination, should interrupt current syscall), return the signal number.
2198 * Stop signals with default action are processed immediately, then cleared;
2199 * they aren't returned. This is checked after each entry to the system for
2200 * a syscall or trap (though this can usually be done without calling issignal
2201 * by checking the pending signal masks in the CURSIG macro.) The normal call
2204 * while (signum = CURSIG(curproc))
2210 int signum
, mask
, prop
, sigbits
;
2212 struct uthread
* ut
;
2214 kauth_cred_t my_cred
;
2218 cur_act
= current_thread();
2221 if(rdebug_proc
&& (p
== rdebug_proc
)) {
2224 #endif /* SIGNAL_DEBUG */
2228 * Try to grab the signal lock.
2230 if (sig_try_locked(p
) <= 0) {
2235 proc_signalstart(p
, 1);
2237 ut
= get_bsdthread_info(cur_act
);
2239 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2241 if (p
->p_lflag
& P_LPPWAIT
)
2242 sigbits
&= ~stopsigmask
;
2243 if (sigbits
== 0) { /* no signal to send */
2248 signum
= ffs((long)sigbits
);
2249 mask
= sigmask(signum
);
2250 prop
= sigprop
[signum
];
2253 * We should see pending but ignored signals
2254 * only if P_LTRACED was on when they were posted.
2256 if (mask
& p
->p_sigignore
&& (p
->p_lflag
& P_LTRACED
) == 0) {
2257 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2260 if (p
->p_lflag
& P_LTRACED
&& (p
->p_lflag
& P_LPPWAIT
) == 0) {
2263 * If traced, always stop, and stay
2264 * stopped until released by the debugger.
2266 /* ptrace debugging */
2267 p
->p_xstat
= signum
;
2269 if (p
->p_lflag
& P_LSIGEXC
) {
2271 p
->sigwait_thread
= cur_act
;
2273 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
2274 p
->p_lflag
&= ~P_LWAITED
;
2275 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2276 proc_signalend(p
, 1);
2278 do_bsdexception(EXC_SOFTWARE
, EXC_SOFT_SIGNAL
, signum
);
2280 proc_signalstart(p
, 1);
2283 my_cred
= kauth_cred_proc_ref(p
);
2284 r_uid
= my_cred
->cr_ruid
;
2285 kauth_cred_unref(&my_cred
);
2287 pp
= proc_parentholdref(p
);
2288 if (pp
!= PROC_NULL
) {
2291 pp
->si_pid
= p
->p_pid
;
2292 pp
->si_status
= p
->p_xstat
;
2293 pp
->si_code
= CLD_TRAPPED
;
2300 * XXX Have to really stop for debuggers;
2301 * XXX stop() doesn't do the right thing.
2302 * XXX Inline the task_suspend because we
2303 * XXX have to diddle Unix state in the
2311 p
->sigwait_thread
= cur_act
;
2313 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
2314 p
->p_lflag
&= ~P_LWAITED
;
2315 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2317 proc_signalend(p
, 1);
2320 if (pp
!= PROC_NULL
) {
2321 psignal(pp
, SIGCHLD
);
2323 wakeup((caddr_t
)pp
);
2324 proc_parentdropref(pp
, 1);
2328 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
2329 thread_block(THREAD_CONTINUE_NULL
);
2331 proc_signalstart(p
, 1);
2335 p
->sigwait_thread
= NULL
;
2336 wakeup((caddr_t
)&p
->sigwait_thread
);
2339 * This code is to detect when gdb is killed
2340 * even as the traced program is attached.
2341 * pgsignal would get the SIGKILL to traced program
2342 * That's what we are trying to see (I hope)
2344 if (ut
->uu_siglist
& sigmask(SIGKILL
)) {
2346 * Wait event may still be outstanding;
2347 * clear it, since sig_lock_to_exit will
2350 clear_wait(current_thread(), THREAD_INTERRUPTED
);
2351 sig_lock_to_exit(p
);
2353 * Since this thread will be resumed
2354 * to allow the current syscall to
2355 * be completed, must save u_qsave
2356 * before calling exit(). (Since exit()
2357 * calls closef() which can trash u_qsave.)
2359 proc_signalend(p
, 1);
2361 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_FRCEXIT
) | DBG_FUNC_NONE
,
2362 p
->p_pid
, W_EXITCODE(0, SIGKILL
), 2, 0, 0);
2363 exit1(p
, W_EXITCODE(0, SIGKILL
), (int *)NULL
);
2368 * We may have to quit
2370 if (thread_should_abort(current_thread())) {
2375 * If parent wants us to take the signal,
2376 * then it will leave it in p->p_xstat;
2377 * otherwise we just look for signals again.
2379 signum
= p
->p_xstat
;
2383 * Put the new signal into p_siglist. If the
2384 * signal is being masked, look for other signals.
2386 mask
= sigmask(signum
);
2387 ut
->uu_siglist
|= mask
;
2388 if (ut
->uu_sigmask
& mask
)
2393 * Decide whether the signal should be returned.
2394 * Return the signal's number, or fall through
2395 * to clear it from the pending mask.
2398 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2402 * Don't take default actions on system processes.
2404 if (p
->p_ppid
== 0) {
2407 * Are you sure you want to ignore SIGSEGV
2410 printf("Process (pid %d) got signal %d\n",
2413 break; /* == ignore */
2417 * If there is a pending stop signal to process
2418 * with default action, stop here,
2419 * then clear the signal. However,
2420 * if process is member of an orphaned
2421 * process group, ignore tty stop signals.
2423 if (prop
& SA_STOP
) {
2428 if (p
->p_lflag
& P_LTRACED
||
2429 (pg
->pg_jobc
== 0 &&
2430 prop
& SA_TTYSTOP
)) {
2433 break; /* == ignore */
2436 if (p
->p_stat
!= SSTOP
) {
2438 p
->p_xstat
= signum
;
2441 p
->p_lflag
&= ~P_LWAITED
;
2444 pp
= proc_parentholdref(p
);
2446 if ((pp
!= PROC_NULL
) && ((pp
->p_flag
& P_NOCLDSTOP
) == 0)) {
2447 my_cred
= kauth_cred_proc_ref(p
);
2448 r_uid
= my_cred
->cr_ruid
;
2449 kauth_cred_unref(&my_cred
);
2452 pp
->si_pid
= p
->p_pid
;
2453 pp
->si_status
= WEXITSTATUS(p
->p_xstat
);
2454 pp
->si_code
= CLD_STOPPED
;
2458 psignal(pp
, SIGCHLD
);
2460 if (pp
!= PROC_NULL
)
2461 proc_parentdropref(pp
, 0);
2465 } else if (prop
& SA_IGNORE
) {
2467 * Except for SIGCONT, shouldn't get here.
2468 * Default action is to ignore; drop it.
2470 break; /* == ignore */
2472 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2482 * Masking above should prevent us ever trying
2483 * to take action on an ignored signal other
2484 * than SIGCONT, unless process is traced.
2486 if ((prop
& SA_CONT
) == 0 &&
2487 (p
->p_lflag
& P_LTRACED
) == 0)
2488 printf("issignal\n");
2489 break; /* == ignore */
2493 * This signal has an action, let
2494 * postsig() process it.
2496 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2500 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2504 proc_signalend(p
,1);
2509 /* called from _sleep */
2513 int signum
, mask
, prop
, sigbits
;
2515 struct uthread
* ut
;
2519 cur_act
= current_thread();
2521 ut
= get_bsdthread_info(cur_act
);
2523 if (ut
->uu_siglist
== 0)
2526 if (((ut
->uu_siglist
& ~ut
->uu_sigmask
) == 0) && ((p
->p_lflag
& P_LTRACED
) == 0))
2529 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2532 if (p
->p_lflag
& P_LPPWAIT
)
2533 sigbits
&= ~stopsigmask
;
2534 if (sigbits
== 0) { /* no signal to send */
2538 signum
= ffs((long)sigbits
);
2539 mask
= sigmask(signum
);
2540 prop
= sigprop
[signum
];
2543 * We should see pending but ignored signals
2544 * only if P_LTRACED was on when they were posted.
2546 if (mask
& p
->p_sigignore
&& (p
->p_lflag
& P_LTRACED
) == 0) {
2549 if (p
->p_lflag
& P_LTRACED
&& (p
->p_lflag
& P_LPPWAIT
) == 0) {
2551 * Put the new signal into p_siglist. If the
2552 * signal is being masked, look for other signals.
2554 mask
= sigmask(signum
);
2555 if (ut
->uu_sigmask
& mask
)
2561 * Decide whether the signal should be returned.
2562 * Return the signal's number, or fall through
2563 * to clear it from the pending mask.
2566 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2570 * Don't take default actions on system processes.
2572 if (p
->p_ppid
== 0) {
2575 * Are you sure you want to ignore SIGSEGV
2578 printf("Process (pid %d) got signal %d\n",
2581 break; /* == ignore */
2585 * If there is a pending stop signal to process
2586 * with default action, stop here,
2587 * then clear the signal. However,
2588 * if process is member of an orphaned
2589 * process group, ignore tty stop signals.
2591 if (prop
& SA_STOP
) {
2596 if (p
->p_lflag
& P_LTRACED
||
2597 (pg
->pg_jobc
== 0 &&
2598 prop
& SA_TTYSTOP
)) {
2600 break; /* == ignore */
2605 } else if (prop
& SA_IGNORE
) {
2607 * Except for SIGCONT, shouldn't get here.
2608 * Default action is to ignore; drop it.
2610 break; /* == ignore */
2618 * Masking above should prevent us ever trying
2619 * to take action on an ignored signal other
2620 * than SIGCONT, unless process is traced.
2622 if ((prop
& SA_CONT
) == 0 &&
2623 (p
->p_lflag
& P_LTRACED
) == 0)
2624 printf("issignal\n");
2625 break; /* == ignore */
2629 * This signal has an action, let
2630 * postsig() process it.
2634 sigbits
&= ~mask
; /* take the signal! */
2640 * Put the argument process into the stopped state and notify the parent
2641 * via wakeup. Signals are handled elsewhere. The process must not be
2645 stop(proc_t p
, proc_t parent
)
2647 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
2648 if ((parent
!= PROC_NULL
) && (parent
->p_stat
!= SSTOP
)) {
2650 wakeup((caddr_t
)parent
);
2653 (void) task_suspend(p
->task
); /*XXX*/
2657 * Take the action for the specified signal
2658 * from the current set of pending signals.
2663 proc_t p
= current_proc();
2664 struct sigacts
*ps
= p
->p_sigacts
;
2665 user_addr_t catcher
;
2667 int mask
, returnmask
;
2668 struct uthread
* ut
;
2674 * This must be called on master cpu
2676 if (cpu_number() != master_cpu
)
2677 panic("psig not on master");
2682 * Try to grab the signal lock.
2684 if (sig_try_locked(p
) <= 0) {
2689 proc_signalstart(p
, 1);
2691 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2692 mask
= sigmask(signum
);
2693 ut
->uu_siglist
&= ~mask
;
2694 catcher
= ps
->ps_sigact
[signum
];
2695 if (catcher
== SIG_DFL
) {
2697 * Default catcher, where the default is to kill
2698 * the process. (Other cases were ignored above.)
2700 sig_lock_to_exit(p
);
2701 p
->p_acflag
|= AXSIG
;
2702 if (sigprop
[signum
] & SA_CORE
) {
2703 p
->p_sigacts
->ps_sig
= signum
;
2704 proc_signalend(p
, 1);
2706 if (coredump(p
) == 0)
2707 signum
|= WCOREFLAG
;
2709 proc_signalend(p
, 1);
2714 bzero((caddr_t
)&(ut
->t_dtrace_siginfo
), sizeof(ut
->t_dtrace_siginfo
));
2716 ut
->t_dtrace_siginfo
.si_signo
= signum
;
2717 ut
->t_dtrace_siginfo
.si_pid
= p
->si_pid
;
2718 ut
->t_dtrace_siginfo
.si_uid
= p
->si_uid
;
2719 ut
->t_dtrace_siginfo
.si_status
= WEXITSTATUS(p
->si_status
);
2721 DTRACE_PROC3(signal__handle
, int, signum
, siginfo_t
*, &(ut
->t_dtrace_siginfo
),
2722 void (*)(void), SIG_DFL
);
2725 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_FRCEXIT
) | DBG_FUNC_NONE
,
2726 p
->p_pid
, W_EXITCODE(0, signum
), 3, 0, 0);
2727 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
2731 * If we get here, the signal must be caught.
2734 if (catcher
== SIG_IGN
|| (ut
->uu_sigmask
& mask
))
2736 "postsig: processing masked or ignored signal\n");
2740 * Set the new mask value and also defer further
2741 * occurences of this signal.
2743 * Special case: user has done a sigpause. Here the
2744 * current mask is not of interest, but rather the
2745 * mask from before the sigpause is what we want
2746 * restored after the signal processing is completed.
2748 if (ut
->uu_flag
& UT_SAS_OLDMASK
) {
2749 returnmask
= ut
->uu_oldmask
;
2750 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
2753 returnmask
= ut
->uu_sigmask
;
2754 ut
->uu_sigmask
|= ps
->ps_catchmask
[signum
];
2755 if ((ps
->ps_signodefer
& mask
) == 0)
2756 ut
->uu_sigmask
|= mask
;
2757 if ((signum
!= SIGILL
) && (signum
!= SIGTRAP
) && (ps
->ps_sigreset
& mask
)) {
2758 if ((signum
!= SIGCONT
) && (sigprop
[signum
] & SA_IGNORE
))
2759 p
->p_sigignore
|= mask
;
2760 ps
->ps_sigact
[signum
] = SIG_DFL
;
2761 ps
->ps_siginfo
&= ~mask
;
2762 ps
->ps_signodefer
&= ~mask
;
2765 /* Needs to disable to run in user mode */
2766 if (signum
== SIGFPE
) {
2767 thread_enable_fpe(current_thread(), 0);
2769 #endif /* __ppc__ */
2771 if (ps
->ps_sig
!= signum
) {
2777 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_nsignals
);
2778 sendsig(p
, catcher
, signum
, returnmask
, code
);
2780 proc_signalend(p
, 1);
2785 * Attach a signal knote to the list of knotes for this process.
2787 * Signal knotes share the knote list with proc knotes. This
2788 * could be avoided by using a signal-specific knote list, but
2789 * probably isn't worth the trouble.
2793 filt_sigattach(struct knote
*kn
)
2795 proc_t p
= current_proc(); /* can attach only to oneself */
2799 kn
->kn_ptr
.p_proc
= p
;
2800 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
2802 KNOTE_ATTACH(&p
->p_klist
, kn
);
2804 proc_klist_unlock();
2810 * remove the knote from the process list, if it hasn't already
2811 * been removed by exit processing.
2815 filt_sigdetach(struct knote
*kn
)
2817 proc_t p
= kn
->kn_ptr
.p_proc
;
2820 kn
->kn_ptr
.p_proc
= NULL
;
2821 KNOTE_DETACH(&p
->p_klist
, kn
);
2822 proc_klist_unlock();
2826 * Post an event to the signal filter. Because we share the same list
2827 * as process knotes, we have to filter out and handle only signal events.
2829 * We assume that we process fdfree() before we post the NOTE_EXIT for
2830 * a process during exit. Therefore, since signal filters can only be
2831 * set up "in-process", we should have already torn down the kqueue
2832 * hosting the EVFILT_SIGNAL knote and should never see NOTE_EXIT.
2835 filt_signal(struct knote
*kn
, long hint
)
2838 if (hint
& NOTE_SIGNAL
) {
2839 hint
&= ~NOTE_SIGNAL
;
2841 if (kn
->kn_id
== (unsigned int)hint
)
2843 } else if (hint
& NOTE_EXIT
) {
2844 panic("filt_signal: detected NOTE_EXIT event");
2847 return (kn
->kn_data
!= 0);
2851 filt_signaltouch(struct knote
*kn
, struct kevent64_s
*kev
, long type
)
2855 case EVENT_REGISTER
:
2856 kn
->kn_sfflags
= kev
->fflags
;
2857 kn
->kn_sdata
= kev
->data
;
2860 *kev
= kn
->kn_kevent
;
2861 if (kn
->kn_flags
& EV_CLEAR
) {
2867 panic("filt_machporttouch() - invalid type (%ld)", type
);
2870 proc_klist_unlock();
2874 bsd_ast(thread_t thread
)
2876 proc_t p
= current_proc();
2877 struct uthread
*ut
= get_bsdthread_info(thread
);
2880 static int bsd_init_done
= 0;
2885 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
2886 pc
= get_useraddr();
2887 addupc_task(p
, pc
, 1);
2888 OSBitAndAtomic(~((uint32_t)P_OWEUPC
), &p
->p_flag
);
2891 if (timerisset(&p
->p_vtimer_user
.it_value
)) {
2894 task_vtimer_update(p
->task
, TASK_VTIMER_USER
, µsecs
);
2896 if (!itimerdecr(p
, &p
->p_vtimer_user
, microsecs
)) {
2897 if (timerisset(&p
->p_vtimer_user
.it_value
))
2898 task_vtimer_set(p
->task
, TASK_VTIMER_USER
);
2900 task_vtimer_clear(p
->task
, TASK_VTIMER_USER
);
2902 psignal(p
, SIGVTALRM
);
2906 if (timerisset(&p
->p_vtimer_prof
.it_value
)) {
2909 task_vtimer_update(p
->task
, TASK_VTIMER_PROF
, µsecs
);
2911 if (!itimerdecr(p
, &p
->p_vtimer_prof
, microsecs
)) {
2912 if (timerisset(&p
->p_vtimer_prof
.it_value
))
2913 task_vtimer_set(p
->task
, TASK_VTIMER_PROF
);
2915 task_vtimer_clear(p
->task
, TASK_VTIMER_PROF
);
2917 psignal(p
, SIGPROF
);
2921 if (timerisset(&p
->p_rlim_cpu
)) {
2924 task_vtimer_update(p
->task
, TASK_VTIMER_RLIM
, (uint32_t *) &tv
.tv_usec
);
2927 if (p
->p_rlim_cpu
.tv_sec
> 0 || p
->p_rlim_cpu
.tv_usec
> tv
.tv_usec
) {
2929 timersub(&p
->p_rlim_cpu
, &tv
, &p
->p_rlim_cpu
);
2933 timerclear(&p
->p_rlim_cpu
);
2936 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
2938 psignal(p
, SIGXCPU
);
2943 if (ut
->t_dtrace_sig
) {
2944 uint8_t dt_action_sig
= ut
->t_dtrace_sig
;
2945 ut
->t_dtrace_sig
= 0;
2946 psignal(p
, dt_action_sig
);
2948 if (ut
->t_dtrace_stop
) {
2949 ut
->t_dtrace_stop
= 0;
2950 psignal(p
, SIGSTOP
);
2952 #endif /* CONFIG_DTRACE */
2954 if (CHECK_SIGNALS(p
, current_thread(), ut
)) {
2955 while ( (signum
= issignal(p
)) )
2959 if (!bsd_init_done
) {
2966 /* ptrace set runnable */
2968 pt_setrunnable(proc_t p
)
2974 if (p
->p_lflag
& P_LTRACED
) {
2979 wakeup((caddr_t
)&(p
->sigwait
));
2980 if ((p
->p_lflag
& P_LSIGEXC
) == 0) { // 5878479
2993 mach_exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
2997 return(bsd_exception(exc
, codes
, 2));
3001 proc_pendingsignals(proc_t p
, sigset_t mask
)
3003 struct uthread
* uth
;
3008 /* If the process is in proc exit return no signal info */
3009 if (p
->p_lflag
& P_LPEXIT
) {
3013 if ((p
->p_lflag
& P_LINVFORK
) && p
->p_vforkact
) {
3015 uth
= (struct uthread
*)get_bsdthread_info(th
);
3017 bits
= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
3023 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
3024 bits
|= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
3032 thread_issignal(proc_t p
, thread_t th
, sigset_t mask
)
3034 struct uthread
* uth
;
3038 uth
= (struct uthread
*)get_bsdthread_info(th
);
3040 bits
= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
3047 * Allow external reads of the sigprop array.
3050 hassigprop(int sig
, int prop
)
3052 return (sigprop
[sig
] & prop
);
3056 pgsigio(pid_t pgid
, int sig
)
3058 proc_t p
= PROC_NULL
;
3061 gsignal(-(pgid
), sig
);
3063 else if (pgid
> 0 && (p
= proc_find(pgid
)) != 0)
3071 proc_signalstart(proc_t p
, int locked
)
3075 while ((p
->p_lflag
& P_LINSIGNAL
) == P_LINSIGNAL
) {
3076 p
->p_lflag
|= P_LSIGNALWAIT
;
3077 msleep(&p
->p_sigmask
, &p
->p_mlock
, 0, "proc_signstart", NULL
);
3079 p
->p_lflag
|= P_LINSIGNAL
;
3084 int sp
, *fp
, numsaved
;
3086 __asm__
volatile("mr %0,r1" : "=r" (sp
));
3088 fp
= (int *)*((int *)sp
);
3089 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
3090 p
->lockpc
[numsaved
] = fp
[2];
3096 #endif /* __ppc__ */
3097 #endif /* SIGNAL_DEBUG */
3098 #endif /* DIAGNOSTIC */
3099 p
->p_signalholder
= current_thread();
3106 proc_signalend(proc_t p
, int locked
)
3110 p
->p_lflag
&= ~P_LINSIGNAL
;
3116 int sp
, *fp
, numsaved
;
3118 __asm__
volatile("mr %0,r1" : "=r" (sp
));
3120 fp
= (int *)*((int *)sp
);
3121 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
3122 p
->unlockpc
[numsaved
] = fp
[2];
3128 #endif /* __ppc__ */
3129 #endif /* SIGNAL_DEBUG */
3130 #endif /* DIAGNOSTIC */
3132 if ((p
->p_lflag
& P_LSIGNALWAIT
) == P_LSIGNALWAIT
) {
3133 p
->p_lflag
&= ~P_LSIGNALWAIT
;
3134 wakeup(&p
->p_sigmask
);
3136 p
->p_signalholder
= NULL
;
3143 sig_lock_to_exit(proc_t p
)
3145 thread_t self
= current_thread();
3147 p
->exit_thread
= self
;
3149 (void) task_suspend(p
->task
);
3154 sig_try_locked(proc_t p
)
3156 thread_t self
= current_thread();
3158 while (p
->sigwait
|| p
->exit_thread
) {
3159 if (p
->exit_thread
) {
3162 msleep((caddr_t
)&p
->sigwait_thread
, &p
->p_mlock
, PCATCH
| PDROP
, 0, 0);
3163 if (thread_should_abort(self
)) {
3165 * Terminate request - clean up.