2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995-1998 Apple Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
63 #define SIGPROP /* include signal properties table */
64 #include <sys/param.h>
65 #include <sys/resourcevar.h>
66 #include <sys/proc_internal.h>
67 #include <sys/kauth.h>
68 #include <sys/systm.h>
69 #include <sys/timeb.h>
70 #include <sys/times.h>
72 #include <sys/file_internal.h>
73 #include <sys/kernel.h>
75 #include <sys/signalvar.h>
77 #include <sys/ktrace.h>
79 #include <sys/syslog.h>
82 #include <sys/kdebug.h>
84 #include <sys/mount.h>
85 #include <sys/sysproto.h>
87 #include <bsm/audit_kernel.h>
89 #include <machine/spl.h>
91 #include <kern/cpu_number.h>
94 #include <sys/user.h> /* for coredump */
95 #include <kern/ast.h> /* for APC support */
96 #include <kern/lock.h>
97 #include <kern/task.h> /* extern void *get_bsdtask_info(task_t); */
98 #include <kern/thread.h>
99 #include <kern/sched_prim.h>
100 #include <kern/thread_call.h>
101 #include <mach/exception.h>
102 #include <mach/task.h>
103 #include <mach/thread_act.h>
106 * Missing prototypes that Mach should export
110 extern int thread_enable_fpe(thread_t act
, int onoff
);
111 extern void unix_syscall_return(int error
);
112 extern thread_t
port_name_to_thread(mach_port_name_t port_name
);
113 extern kern_return_t
check_actforsig(task_t task
, thread_t thread
, int setast
);
114 extern kern_return_t
get_signalact(task_t
, thread_t
*, int);
115 extern boolean_t
thread_should_abort(thread_t
);
116 extern unsigned int get_useraddr(void);
122 extern void doexception(int exc
, int code
, int sub
);
124 void stop(struct proc
*p
);
125 int cansignal(struct proc
*, kauth_cred_t
, struct proc
*, int);
126 int killpg1(struct proc
*, int, int, int);
127 void sigexit_locked(struct proc
*, int);
128 int setsigvec(struct proc
*, int, struct __user_sigaction
*);
129 void exit1(struct proc
*, int, int *);
130 void psignal_uthread(thread_t
, int);
131 kern_return_t
do_bsdexception(int, int, int);
132 void __posix_sem_syscall_return(kern_return_t
);
134 /* implementations in osfmk/kern/sync_sema.c. We do not want port.h in this scope, so void * them */
135 kern_return_t
semaphore_timedwait_signal_trap_internal(void *, void *,time_t, int32_t, void (*)(int));
136 kern_return_t
semaphore_timedwait_trap_internal(void *, time_t, int32_t, void (*)(int));
137 kern_return_t
semaphore_wait_signal_trap_internal(void *, void *, void (*)(int));
138 kern_return_t
semaphore_wait_trap_internal(void *, void (*)(int));
140 static int filt_sigattach(struct knote
*kn
);
141 static void filt_sigdetach(struct knote
*kn
);
142 static int filt_signal(struct knote
*kn
, long hint
);
144 struct filterops sig_filtops
=
145 { 0, filt_sigattach
, filt_sigdetach
, filt_signal
};
149 * NOTE: Source and target may *NOT* overlap! (target is smaller)
152 sigaltstack_64to32(struct user_sigaltstack
*in
, struct sigaltstack
*out
)
154 out
->ss_sp
= CAST_DOWN(void *,in
->ss_sp
);
155 out
->ss_size
= in
->ss_size
;
156 out
->ss_flags
= in
->ss_flags
;
160 * NOTE: Source and target may are permitted to overlap! (source is smaller);
161 * this works because we copy fields in order from the end of the struct to
165 sigaltstack_32to64(struct sigaltstack
*in
, struct user_sigaltstack
*out
)
167 out
->ss_flags
= in
->ss_flags
;
168 out
->ss_size
= in
->ss_size
;
169 out
->ss_sp
= CAST_USER_ADDR_T(in
->ss_sp
);
173 sigaction_64to32(struct user_sigaction
*in
, struct sigaction
*out
)
175 /* This assumes 32 bit __sa_handler is of type sig_t */
176 out
->__sigaction_u
.__sa_handler
= CAST_DOWN(sig_t
,in
->__sigaction_u
.__sa_handler
);
177 out
->sa_mask
= in
->sa_mask
;
178 out
->sa_flags
= in
->sa_flags
;
182 __sigaction_32to64(struct __sigaction
*in
, struct __user_sigaction
*out
)
184 out
->__sigaction_u
.__sa_handler
= CAST_USER_ADDR_T(in
->__sigaction_u
.__sa_handler
);
185 out
->sa_tramp
= CAST_USER_ADDR_T(in
->sa_tramp
);
186 out
->sa_mask
= in
->sa_mask
;
187 out
->sa_flags
= in
->sa_flags
;
192 void ram_printf(int);
194 unsigned int rdebug_proc
=0;
201 #endif /* SIGNAL_DEBUG */
204 signal_lock(struct proc
*p
)
211 int register sp
, *fp
, numsaved
;
213 __asm__
volatile("mr %0,r1" : "=r" (sp
));
215 fp
= (int *)*((int *)sp
);
216 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
217 p
->lockpc
[numsaved
] = fp
[2];
224 #endif /* SIGNAL_DEBUG */
225 #endif /* DIAGNOSTIC */
228 error
= lockmgr((struct lock__bsd__
*)&p
->signal_lock
[0], LK_EXCLUSIVE
, 0, (struct proc
*)0);
235 signal_unlock(struct proc
*p
)
241 int register sp
, *fp
, numsaved
;
243 __asm__
volatile("mr %0,r1" : "=r" (sp
));
245 fp
= (int *)*((int *)sp
);
246 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
247 p
->unlockpc
[numsaved
] = fp
[2];
254 #endif /* SIGNAL_DEBUG */
255 #endif /* DIAGNOSTIC */
257 /* TBD: check p last arg */
258 return(lockmgr((struct lock__bsd__
*)&p
->signal_lock
[0], LK_RELEASE
, (simple_lock_t
)0, (struct proc
*)0));
262 signal_setast(sig_actthread
)
263 thread_t sig_actthread
;
265 act_set_astbsd(sig_actthread
);
269 * Can process p, with ucred uc, send the signal signum to process q?
272 cansignal(p
, uc
, q
, signum
)
278 /* you can signal yourself */
282 if (!suser(uc
, NULL
))
283 return (1); /* root can always signal */
285 if (signum
== SIGCONT
&& q
->p_session
== p
->p_session
)
286 return (1); /* SIGCONT in session */
289 * Using kill(), only certain signals can be sent to setugid
292 if (q
->p_flag
& P_SUGID
) {
305 if (uc
->cr_ruid
== q
->p_ucred
->cr_ruid
||
306 kauth_cred_getuid(uc
) == q
->p_ucred
->cr_ruid
||
307 uc
->cr_ruid
== kauth_cred_getuid(q
->p_ucred
) ||
308 kauth_cred_getuid(uc
) == kauth_cred_getuid(q
->p_ucred
))
315 * because the P_SUGID test exists, this has extra tests which
318 if (uc
->cr_ruid
== q
->p_ucred
->cr_ruid
||
319 uc
->cr_ruid
== q
->p_ucred
->cr_svuid
||
320 kauth_cred_getuid(uc
) == q
->p_ucred
->cr_ruid
||
321 kauth_cred_getuid(uc
) == q
->p_ucred
->cr_svuid
||
322 uc
->cr_ruid
== kauth_cred_getuid(q
->p_ucred
) ||
323 kauth_cred_getuid(uc
) == kauth_cred_getuid(q
->p_ucred
))
331 sigaction(struct proc
*p
, register struct sigaction_args
*uap
, __unused register_t
*retval
)
333 struct user_sigaction vec
;
334 struct __user_sigaction __vec
;
336 struct user_sigaction
*sa
= &vec
;
337 register struct sigacts
*ps
= p
->p_sigacts
;
342 signum
= uap
->signum
;
343 if (signum
<= 0 || signum
>= NSIG
||
344 signum
== SIGKILL
|| signum
== SIGSTOP
)
348 sa
->sa_handler
= ps
->ps_sigact
[signum
];
349 sa
->sa_mask
= ps
->ps_catchmask
[signum
];
350 bit
= sigmask(signum
);
352 if ((ps
->ps_sigonstack
& bit
) != 0)
353 sa
->sa_flags
|= SA_ONSTACK
;
354 if ((ps
->ps_sigintr
& bit
) == 0)
355 sa
->sa_flags
|= SA_RESTART
;
356 if (ps
->ps_siginfo
& bit
)
357 sa
->sa_flags
|= SA_SIGINFO
;
358 if (ps
->ps_signodefer
& bit
)
359 sa
->sa_flags
|= SA_NODEFER
;
360 if (ps
->ps_64regset
& bit
)
361 sa
->sa_flags
|= SA_64REGSET
;
362 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDSTOP
))
363 sa
->sa_flags
|= SA_NOCLDSTOP
;
364 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDWAIT
))
365 sa
->sa_flags
|= SA_NOCLDWAIT
;
367 if (IS_64BIT_PROCESS(p
)) {
368 error
= copyout(sa
, uap
->osa
, sizeof(struct user_sigaction
));
370 struct sigaction vec32
;
371 sigaction_64to32(sa
, &vec32
);
372 error
= copyout(&vec32
, uap
->osa
, sizeof(struct sigaction
));
378 if (IS_64BIT_PROCESS(p
)) {
379 error
= copyin(uap
->nsa
, &__vec
, sizeof(struct __user_sigaction
));
381 struct __sigaction __vec32
;
382 error
= copyin(uap
->nsa
, &__vec32
, sizeof(struct __sigaction
));
383 __sigaction_32to64(&__vec32
, &__vec
);
387 error
= setsigvec(p
, signum
, &__vec
);
392 /* Routines to manipulate bits on all threads */
394 clear_procsiglist(struct proc
*p
, int bit
)
396 struct uthread
* uth
;
401 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
402 thact
= p
->p_vforkact
;
403 uth
= (struct uthread
*)get_bsdthread_info(thact
);
405 uth
->uu_siglist
&= ~bit
;
407 p
->p_siglist
&= ~bit
;
412 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
413 uth
->uu_siglist
&= ~bit
;
415 p
->p_siglist
&= ~bit
;
422 unblock_procsigmask(struct proc
*p
, int bit
)
424 struct uthread
* uth
;
428 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
429 thact
= p
->p_vforkact
;
430 uth
= (struct uthread
*)get_bsdthread_info(thact
);
432 uth
->uu_sigmask
&= ~bit
;
434 p
->p_sigmask
&= ~bit
;
438 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
439 uth
->uu_sigmask
&= ~bit
;
441 p
->p_sigmask
&= ~bit
;
448 block_procsigmask(struct proc
*p
, int bit
)
450 struct uthread
* uth
;
454 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
455 thact
= p
->p_vforkact
;
456 uth
= (struct uthread
*)get_bsdthread_info(thact
);
458 uth
->uu_sigmask
|= bit
;
464 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
465 uth
->uu_sigmask
|= bit
;
473 set_procsigmask(struct proc
*p
, int bit
)
475 struct uthread
* uth
;
479 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
480 thact
= p
->p_vforkact
;
481 uth
= (struct uthread
*)get_bsdthread_info(thact
);
483 uth
->uu_sigmask
= bit
;
489 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
490 uth
->uu_sigmask
= bit
;
497 /* XXX should be static? */
499 setsigvec(struct proc
*p
, int signum
, struct __user_sigaction
*sa
)
501 register struct sigacts
*ps
= p
->p_sigacts
;
504 if ((signum
== SIGKILL
|| signum
== SIGSTOP
) &&
505 sa
->sa_handler
!= SIG_DFL
)
507 bit
= sigmask(signum
);
509 * Change setting atomically.
511 ps
->ps_sigact
[signum
] = sa
->sa_handler
;
512 ps
->ps_trampact
[signum
] = sa
->sa_tramp
;
513 ps
->ps_catchmask
[signum
] = sa
->sa_mask
&~ sigcantmask
;
514 if (sa
->sa_flags
& SA_SIGINFO
)
515 ps
->ps_siginfo
|= bit
;
517 ps
->ps_siginfo
&= ~bit
;
518 if (sa
->sa_flags
& SA_64REGSET
)
519 ps
->ps_64regset
|= bit
;
521 ps
->ps_64regset
&= ~bit
;
522 if ((sa
->sa_flags
& SA_RESTART
) == 0)
523 ps
->ps_sigintr
|= bit
;
525 ps
->ps_sigintr
&= ~bit
;
526 if (sa
->sa_flags
& SA_ONSTACK
)
527 ps
->ps_sigonstack
|= bit
;
529 ps
->ps_sigonstack
&= ~bit
;
530 if (sa
->sa_flags
& SA_USERTRAMP
)
531 ps
->ps_usertramp
|= bit
;
533 ps
->ps_usertramp
&= ~bit
;
534 if (sa
->sa_flags
& SA_RESETHAND
)
535 ps
->ps_sigreset
|= bit
;
537 ps
->ps_sigreset
&= ~bit
;
538 if (sa
->sa_flags
& SA_NODEFER
)
539 ps
->ps_signodefer
|= bit
;
541 ps
->ps_signodefer
&= ~bit
;
542 if (signum
== SIGCHLD
) {
543 if (sa
->sa_flags
& SA_NOCLDSTOP
)
544 p
->p_flag
|= P_NOCLDSTOP
;
546 p
->p_flag
&= ~P_NOCLDSTOP
;
547 if ((sa
->sa_flags
& SA_NOCLDWAIT
) || (sa
->sa_handler
== SIG_IGN
))
548 p
->p_flag
|= P_NOCLDWAIT
;
550 p
->p_flag
&= ~P_NOCLDWAIT
;
554 if (signum
== SIGFPE
) {
555 if (sa
->sa_handler
== SIG_DFL
|| sa
->sa_handler
== SIG_IGN
)
556 thread_enable_fpe(current_thread(), 0);
558 thread_enable_fpe(current_thread(), 1);
562 * Set bit in p_sigignore for signals that are set to SIG_IGN,
563 * and for signals set to SIG_DFL where the default is to ignore.
564 * However, don't put SIGCONT in p_sigignore,
565 * as we have to restart the process.
567 if (sa
->sa_handler
== SIG_IGN
||
568 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
570 clear_procsiglist(p
, bit
);
571 if (signum
!= SIGCONT
)
572 p
->p_sigignore
|= bit
; /* easier in psignal */
573 p
->p_sigcatch
&= ~bit
;
575 p
->p_sigignore
&= ~bit
;
576 if (sa
->sa_handler
== SIG_DFL
)
577 p
->p_sigcatch
&= ~bit
;
579 p
->p_sigcatch
|= bit
;
585 * Initialize signal state for process 0;
586 * set to ignore signals that are ignored by default.
594 for (i
= 0; i
< NSIG
; i
++)
595 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
596 p
->p_sigignore
|= sigmask(i
);
600 * Reset signals for an exec of the specified process.
604 register struct proc
*p
;
605 register thread_t thr_act
;
607 register struct sigacts
*ps
= p
->p_sigacts
;
608 register int nc
, mask
;
609 struct uthread
*ut
= (struct uthread
*)0;
612 ut
= (struct uthread
*)get_bsdthread_info(thr_act
);
615 * Reset caught signals. Held signals remain held
616 * through p_sigmask (unless they were caught,
617 * and are now ignored by default).
619 while (p
->p_sigcatch
) {
620 nc
= ffs((long)p
->p_sigcatch
);
622 p
->p_sigcatch
&= ~mask
;
623 if (sigprop
[nc
] & SA_IGNORE
) {
625 p
->p_sigignore
|= mask
;
627 ut
->uu_siglist
&= ~mask
;
628 p
->p_siglist
&= ~mask
;
630 clear_procsiglist(p
, mask
);
632 ps
->ps_sigact
[nc
] = SIG_DFL
;
635 * Reset stack state to the user stack.
636 * Clear set of signals caught on the signal stack.
638 ps
->ps_sigstk
.ss_flags
= SA_DISABLE
;
639 ps
->ps_sigstk
.ss_size
= 0;
640 ps
->ps_sigstk
.ss_sp
= USER_ADDR_NULL
;
643 ut
->uu_sigstk
.ss_flags
= SA_DISABLE
;
644 ut
->uu_sigstk
.ss_size
= 0;
645 ut
->uu_sigstk
.ss_sp
= USER_ADDR_NULL
;
646 ut
->uu_flag
&= ~UT_ALTSTACK
;
648 ps
->ps_sigonstack
= 0;
652 * Manipulate signal mask.
653 * Note that we receive new mask, not pointer,
654 * and return old mask as return value;
655 * the library stub does the rest.
658 sigprocmask(register struct proc
*p
, struct sigprocmask_args
*uap
, __unused register_t
*retval
)
661 sigset_t oldmask
, nmask
;
662 user_addr_t omask
= uap
->omask
;
665 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
666 oldmask
= ut
->uu_sigmask
;
668 if (uap
->mask
== USER_ADDR_NULL
) {
669 /* just want old mask */
672 error
= copyin(uap
->mask
, &nmask
, sizeof(sigset_t
));
678 block_procsigmask(p
, (nmask
& ~sigcantmask
));
679 signal_setast(current_thread());
683 unblock_procsigmask(p
, (nmask
& ~sigcantmask
));
684 signal_setast(current_thread());
688 set_procsigmask(p
, (nmask
& ~sigcantmask
));
689 signal_setast(current_thread());
697 if (!error
&& omask
!= USER_ADDR_NULL
)
698 copyout(&oldmask
, omask
, sizeof(sigset_t
));
703 sigpending(__unused
struct proc
*p
, register struct sigpending_args
*uap
, __unused register_t
*retval
)
708 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
709 pendlist
= ut
->uu_siglist
;
712 copyout(&pendlist
, uap
->osv
, sizeof(sigset_t
));
718 * Suspend process until signal, providing mask to be set
719 * in the meantime. Note nonstandard calling convention:
720 * libc stub passes mask, not pointer, to save a copyin.
724 sigcontinue(__unused
int error
)
726 // struct uthread *ut = get_bsdthread_info(current_thread());
727 unix_syscall_return(EINTR
);
731 sigsuspend(register struct proc
*p
, struct sigsuspend_args
*uap
, __unused register_t
*retval
)
735 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
738 * When returning from sigpause, we want
739 * the old mask to be restored after the
740 * signal handler has finished. Thus, we
741 * save it here and mark the sigacts structure
744 ut
->uu_oldmask
= ut
->uu_sigmask
;
745 ut
->uu_flag
|= UT_SAS_OLDMASK
;
746 ut
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
747 (void) tsleep0((caddr_t
) p
, PPAUSE
|PCATCH
, "pause", 0, sigcontinue
);
748 /* always return EINTR rather than ERESTART... */
754 __disable_threadsignal(struct proc
*p
,
755 __unused
register struct __disable_threadsignal_args
*uap
,
756 __unused register_t
*retval
)
760 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
762 /* No longer valid to have any signal delivered */
764 uth
->uu_flag
|= UT_NO_SIGMASK
;
773 __pthread_markcancel(p
, uap
, retval
)
775 register struct __pthread_markcancel_args
*uap
;
778 thread_act_t target_act
;
782 target_act
= (thread_act_t
)port_name_to_thread(uap
->thread_port
);
784 if (target_act
== THR_ACT_NULL
)
787 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
789 /* if the thread is in vfork do not cancel */
790 if ((uth
->uu_flag
& (P_VFORK
| UT_CANCEL
| UT_CANCELED
)) == 0) {
791 uth
->uu_flag
|= (UT_CANCEL
| UT_NO_SIGMASK
);
792 if (((uth
->uu_flag
& UT_NOTCANCELPT
) == 0)
793 && ((uth
->uu_flag
& UT_CANCELDISABLE
) == 0))
794 thread_abort_safely(target_act
);
797 thread_deallocate(target_act
);
801 /* if action =0 ; return the cancellation state ,
802 * if marked for cancellation, make the thread canceled
803 * if action = 1 ; Enable the cancel handling
804 * if action = 2; Disable the cancel handling
807 __pthread_canceled(p
, uap
, retval
)
809 register struct __pthread_canceled_args
*uap
;
812 thread_act_t thr_act
;
814 int action
= uap
->action
;
816 thr_act
= current_act();
817 uth
= (struct uthread
*)get_bsdthread_info(thr_act
);
821 uth
->uu_flag
&= ~UT_CANCELDISABLE
;
824 uth
->uu_flag
|= UT_CANCELDISABLE
;
828 /* if the thread is in vfork do not cancel */
829 if((uth
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
830 uth
->uu_flag
&= ~UT_CANCEL
;
831 uth
->uu_flag
|= (UT_CANCELED
| UT_NO_SIGMASK
);
840 __posix_sem_syscall_return(kern_return_t kern_result
)
844 if (kern_result
== KERN_SUCCESS
)
846 else if (kern_result
== KERN_ABORTED
)
848 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
852 unix_syscall_return(error
);
853 /* does not return */
858 __semwait_signal(p
, uap
, retval
)
860 register struct __semwait_signal_args
*uap
;
864 kern_return_t kern_result
;
865 mach_timespec_t then
;
871 then
.tv_sec
= uap
->tv_sec
;
872 then
.tv_nsec
= uap
->tv_nsec
;
875 then
.tv_sec
= uap
->tv_sec
- now
.tv_sec
;
876 then
.tv_nsec
= uap
->tv_nsec
- now
.tv_nsec
;
877 if (then
.tv_nsec
< 0) {
878 then
.tv_nsec
+= NSEC_PER_SEC
;
883 if (uap
->mutex_sem
== (void *)NULL
)
884 kern_result
= semaphore_timedwait_trap_internal(uap
->cond_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
886 kern_result
= semaphore_timedwait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, then
.tv_sec
, then
.tv_nsec
, __posix_sem_syscall_return
);
890 if (uap
->mutex_sem
== (void *)NULL
)
891 kern_result
= semaphore_wait_trap_internal(uap
->cond_sem
, __posix_sem_syscall_return
);
894 kern_result
= semaphore_wait_signal_trap_internal(uap
->cond_sem
, uap
->mutex_sem
, __posix_sem_syscall_return
);
898 if (kern_result
== KERN_SUCCESS
)
900 else if (kern_result
== KERN_ABORTED
)
902 else if (kern_result
== KERN_OPERATION_TIMED_OUT
)
910 __pthread_kill(__unused
struct proc
*p
,
911 register struct __pthread_kill_args
*uap
,
912 __unused register_t
*retval
)
916 int signum
= uap
->sig
;
919 target_act
= (thread_t
)port_name_to_thread(uap
->thread_port
);
921 if (target_act
== THREAD_NULL
)
923 if ((u_int
)signum
>= NSIG
) {
928 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
930 if (uth
->uu_flag
& UT_NO_SIGMASK
) {
936 psignal_uthread(target_act
, signum
);
938 thread_deallocate(target_act
);
944 pthread_sigmask(__unused
register struct proc
*p
,
945 register struct pthread_sigmask_args
*uap
,
946 __unused register_t
*retval
)
948 user_addr_t set
= uap
->set
;
949 user_addr_t oset
= uap
->oset
;
955 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
956 oldset
= ut
->uu_sigmask
;
958 if (set
== USER_ADDR_NULL
) {
959 /* need only old mask */
963 error
= copyin(set
, &nset
, sizeof(sigset_t
));
969 ut
->uu_sigmask
|= (nset
& ~sigcantmask
);
973 ut
->uu_sigmask
&= ~(nset
);
974 signal_setast(current_thread());
978 ut
->uu_sigmask
= (nset
& ~sigcantmask
);
979 signal_setast(current_thread());
987 if (!error
&& oset
!= USER_ADDR_NULL
)
988 copyout(&oldset
, oset
, sizeof(sigset_t
));
995 sigwait(register struct proc
*p
, register struct sigwait_args
*uap
, __unused register_t
*retval
)
1005 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1007 if (uap
->set
== USER_ADDR_NULL
)
1010 error
= copyin(uap
->set
, &mask
, sizeof(sigset_t
));
1014 siglist
= (mask
& ~sigcantmask
);
1020 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
1024 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1025 if ( (sigw
= uth
->uu_siglist
& siglist
) ) {
1032 /* The signal was pending on a thread */
1036 * When returning from sigwait, we want
1037 * the old mask to be restored after the
1038 * signal handler has finished. Thus, we
1039 * save it here and mark the sigacts structure
1042 ut
->uu_oldmask
= ut
->uu_sigmask
;
1043 ut
->uu_flag
|= UT_SAS_OLDMASK
;
1044 if (siglist
== (sigset_t
)0)
1046 /* SIGKILL and SIGSTOP are not maskable as well */
1047 ut
->uu_sigmask
= ~(siglist
|sigcantmask
);
1048 ut
->uu_sigwait
= siglist
;
1049 /* No Continuations for now */
1050 error
= tsleep((caddr_t
)&ut
->uu_sigwait
, PPAUSE
|PCATCH
, "pause", 0);
1052 if ((error
== EINTR
) || (error
== ERESTART
))
1055 sigw
= (ut
->uu_sigwait
& siglist
);
1056 ut
->uu_sigmask
= ut
->uu_oldmask
;
1058 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
1062 signum
= ffs((unsigned int)sigw
);
1064 panic("sigwait with no signal wakeup");
1065 ut
->uu_siglist
&= ~(sigmask(signum
));
1066 if (uap
->sig
!= USER_ADDR_NULL
)
1067 error
= copyout(&signum
, uap
->sig
, sizeof(int));
1076 sigaltstack(struct proc
*p
, register struct sigaltstack_args
*uap
, __unused register_t
*retval
)
1078 struct sigacts
*psp
;
1079 struct user_sigaltstack
*pstk
;
1080 struct user_sigaltstack ss
;
1081 struct uthread
*uth
;
1082 int uthsigaltstack
= 0;
1085 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
1086 uthsigaltstack
= p
->p_lflag
& P_LTHSIGSTACK
;
1089 if (uthsigaltstack
!= 0) {
1090 pstk
= &uth
->uu_sigstk
;
1091 if ((uth
->uu_flag
& UT_ALTSTACK
) == 0)
1092 uth
->uu_sigstk
.ss_flags
|= SA_DISABLE
;
1094 pstk
= &psp
->ps_sigstk
;
1095 if ((psp
->ps_flags
& SAS_ALTSTACK
) == 0)
1096 psp
->ps_sigstk
.ss_flags
|= SA_DISABLE
;
1099 if (IS_64BIT_PROCESS(p
)) {
1100 error
= copyout(pstk
, uap
->oss
, sizeof(struct user_sigaltstack
));
1102 struct sigaltstack ss32
;
1103 sigaltstack_64to32(pstk
, &ss32
);
1104 error
= copyout(&ss32
, uap
->oss
, sizeof(struct sigaltstack
));
1109 if (uap
->nss
== USER_ADDR_NULL
)
1111 if (IS_64BIT_PROCESS(p
)) {
1112 error
= copyin(uap
->nss
, &ss
, sizeof(struct user_sigaltstack
));
1114 struct sigaltstack ss32
;
1115 error
= copyin(uap
->nss
, &ss32
, sizeof(struct sigaltstack
));
1116 sigaltstack_32to64(&ss32
,&ss
);
1120 if ((ss
.ss_flags
& ~SA_DISABLE
) != 0) {
1124 if (ss
.ss_flags
& SA_DISABLE
) {
1125 if (uthsigaltstack
!= 0) {
1126 /* if we are here we are not in the signal handler ;so no need to check */
1127 if (uth
->uu_sigstk
.ss_flags
& SA_ONSTACK
)
1129 uth
->uu_flag
&= ~UT_ALTSTACK
;
1130 uth
->uu_sigstk
.ss_flags
= ss
.ss_flags
;
1132 if (psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
)
1134 psp
->ps_flags
&= ~SAS_ALTSTACK
;
1135 psp
->ps_sigstk
.ss_flags
= ss
.ss_flags
;
1140 /* The older stacksize was 8K, enforce that one so no compat problems */
1141 #define OLDMINSIGSTKSZ 8*1024
1142 if (ss
.ss_size
< OLDMINSIGSTKSZ
)
1144 if (uthsigaltstack
!= 0) {
1145 uth
->uu_flag
|= UT_ALTSTACK
;
1148 psp
->ps_flags
|= SAS_ALTSTACK
;
1155 kill(struct proc
*cp
, struct kill_args
*uap
, __unused register_t
*retval
)
1157 register struct proc
*p
;
1158 kauth_cred_t uc
= kauth_cred_get();
1160 AUDIT_ARG(pid
, uap
->pid
);
1161 AUDIT_ARG(signum
, uap
->signum
);
1163 if ((u_int
)uap
->signum
>= NSIG
)
1166 /* kill single process */
1167 if ((p
= proc_findref(uap
->pid
)) == NULL
) {
1168 if ((p
= pzfind(uap
->pid
)) != NULL
) {
1170 * IEEE Std 1003.1-2001: return success
1171 * when killing a zombie.
1177 AUDIT_ARG(process
, p
);
1178 if (!cansignal(cp
, uc
, p
, uap
->signum
)) {
1183 psignal(p
, uap
->signum
);
1188 case -1: /* broadcast signal */
1189 return (killpg1(cp
, uap
->signum
, 0, 1));
1190 case 0: /* signal own process group */
1191 return (killpg1(cp
, uap
->signum
, 0, 0));
1192 default: /* negative explicit process group */
1193 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0));
1200 * Common code for kill process group/broadcast kill.
1201 * cp is calling process.
1204 killpg1(cp
, signum
, pgid
, all
)
1205 register struct proc
*cp
;
1206 int signum
, pgid
, all
;
1208 register struct proc
*p
;
1209 kauth_cred_t uc
= cp
->p_ucred
;
1217 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1218 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1219 p
== cp
|| !cansignal(cp
, uc
, p
, signum
))
1228 * zero pgid means send to my process group.
1232 pgrp
= pgfind(pgid
);
1236 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1237 p
= p
->p_pglist
.le_next
) {
1238 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1239 p
->p_stat
== SZOMB
||
1240 !cansignal(cp
, uc
, p
, signum
))
1247 return (nfound
? 0 : ESRCH
);
1251 * Send a signal to a process group.
1254 gsignal(pgid
, signum
)
1259 if (pgid
&& (pgrp
= pgfind(pgid
)))
1260 pgsignal(pgrp
, signum
, 0);
1264 * Send a signal to a process group. If checktty is 1,
1265 * limit to members which have a controlling terminal.
1268 pgsignal(pgrp
, signum
, checkctty
)
1270 int signum
, checkctty
;
1272 register struct proc
*p
;
1275 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1276 p
= p
->p_pglist
.le_next
)
1277 if (checkctty
== 0 || p
->p_flag
& P_CONTROLT
)
1282 * Send signal to a backgrounded process blocked due to tty access
1283 * In FreeBSD, the backgrounded process wakes up every second and
1284 * discovers whether it is foregounded or not. In our case, we block
1285 * the thread in tsleep as we want to avoid storm of processes as well
1286 * as the suspend is only at AST level
1289 tty_pgsignal(pgrp
, signum
)
1293 register struct proc
*p
;
1296 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1297 p
= p
->p_pglist
.le_next
)
1298 if ((p
->p_flag
& P_TTYSLEEP
) && (p
->p_flag
& P_CONTROLT
))
1303 * Send a signal caused by a trap to a specific thread.
1306 threadsignal(thread_t sig_actthread
, int signum
, u_long code
)
1308 register struct uthread
*uth
;
1309 register struct task
* sig_task
;
1310 register struct proc
*p
;
1313 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1316 mask
= sigmask(signum
);
1317 if ((mask
& threadmask
) == 0)
1319 sig_task
= get_threadtask(sig_actthread
);
1320 p
= (struct proc
*)(get_bsdtask_info(sig_task
));
1322 uth
= get_bsdthread_info(sig_actthread
);
1323 if (uth
&& (uth
->uu_flag
& UT_VFORK
))
1326 if (!(p
->p_flag
& P_TRACED
) && (p
->p_sigignore
& mask
))
1329 uth
->uu_siglist
|= mask
;
1330 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1331 uth
->uu_code
= code
;
1332 /* mark on process as well */
1333 signal_setast(sig_actthread
);
1339 register struct proc
*p
;
1340 register int signum
;
1342 psignal_lock(p
, signum
, 1);
1346 psignal_vfork(struct proc
*p
, task_t new_task
, thread_t thr_act
, int signum
)
1349 register sig_t action
;
1351 struct uthread
*uth
;
1353 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1354 panic("psignal signal number");
1355 mask
= sigmask(signum
);
1356 prop
= sigprop
[signum
];
1359 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1362 #endif /* SIGNAL_DEBUG */
1364 if ((new_task
== TASK_NULL
) || (thr_act
== (thread_t
)NULL
) || is_kerneltask(new_task
))
1368 uth
= get_bsdthread_info(thr_act
);
1372 * proc is traced, always give parent a chance.
1376 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1377 (p
->p_flag
& P_TRACED
) == 0)
1380 if (prop
& SA_CONT
) {
1381 p
->p_siglist
&= ~stopsigmask
;
1382 uth
->uu_siglist
&= ~stopsigmask
;
1385 if (prop
& SA_STOP
) {
1387 * If sending a tty stop signal to a member of an orphaned
1388 * process group, discard the signal here if the action
1389 * is default; don't stop the process below if sleeping,
1390 * and don't clear any pending SIGCONT.
1392 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1395 uth
->uu_siglist
&= ~contsigmask
;
1396 p
->p_siglist
&= ~contsigmask
;
1398 uth
->uu_siglist
|= mask
;
1399 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1401 /* Deliver signal to the activation passed in */
1402 act_set_astbsd(thr_act
);
1405 * SIGKILL priority twiddling moved here from above because
1406 * it needs sig_thread. Could merge it into large switch
1407 * below if we didn't care about priority for tracing
1408 * as SIGKILL's action is always SIG_DFL.
1410 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1415 * This Process is traced - wake it up (if not already
1416 * stopped) so that it can discover the signal in
1417 * issig() and stop for the parent.
1419 if (p
->p_flag
& P_TRACED
) {
1420 if (p
->p_stat
!= SSTOP
)
1427 * If we're being traced (possibly because someone attached us
1428 * while we were stopped), check for a signal from the debugger.
1430 if (p
->p_stat
== SSTOP
) {
1431 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
1432 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1433 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1438 * setrunnable(p) in BSD
1447 get_signalthread(struct proc
*p
, int signum
)
1449 struct uthread
*uth
;
1451 sigset_t mask
= sigmask(signum
);
1452 thread_t sig_thread_act
;
1453 struct task
* sig_task
= p
->task
;
1456 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
1457 sig_thread_act
= p
->p_vforkact
;
1458 kret
= check_actforsig(sig_task
, sig_thread_act
, 1);
1459 if (kret
== KERN_SUCCESS
)
1460 return(sig_thread_act
);
1462 return(THREAD_NULL
);
1465 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1466 if(((uth
->uu_flag
& UT_NO_SIGMASK
)== 0) &&
1467 (((uth
->uu_sigmask
& mask
) == 0) || (uth
->uu_sigwait
& mask
))) {
1468 if (check_actforsig(p
->task
, uth
->uu_act
, 1) == KERN_SUCCESS
)
1469 return(uth
->uu_act
);
1472 if (get_signalact(p
->task
, &thr_act
, 1) == KERN_SUCCESS
) {
1476 return(THREAD_NULL
);
1480 * Send the signal to the process. If the signal has an action, the action
1481 * is usually performed by the target process rather than the caller; we add
1482 * the signal to the set of pending signals for the process.
1485 * o When a stop signal is sent to a sleeping process that takes the
1486 * default action, the process is stopped without awakening it.
1487 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1488 * regardless of the signal action (eg, blocked or ignored).
1490 * Other ignored signals are discarded immediately.
1493 psignal_lock(p
, signum
, withlock
)
1494 register struct proc
*p
;
1495 register int signum
;
1496 register int withlock
;
1499 register sig_t action
;
1500 thread_t sig_thread_act
;
1501 register task_t sig_task
;
1503 struct uthread
*uth
;
1504 boolean_t funnel_state
= FALSE
;
1507 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1508 panic("psignal signal number");
1509 mask
= sigmask(signum
);
1510 prop
= sigprop
[signum
];
1513 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1516 #endif /* SIGNAL_DEBUG */
1518 if (thread_funnel_get() == (funnel_t
*)0) {
1520 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1523 * We will need the task pointer later. Grab it now to
1524 * check for a zombie process. Also don't send signals
1525 * to kernel internal tasks.
1527 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1529 thread_funnel_set(kernel_flock
, funnel_state
);
1533 KNOTE(&p
->p_klist
, NOTE_SIGNAL
| signum
);
1536 * do not send signals to the process that has the thread
1537 * doing a reboot(). Not doing so will mark that thread aborted
1538 * and can cause IO failures wich will cause data loss.
1540 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1542 thread_funnel_set(kernel_flock
, funnel_state
);
1550 * Deliver the signal to the first thread in the task. This
1551 * allows single threaded applications which use signals to
1552 * be able to be linked with multithreaded libraries. We have
1553 * an implicit reference to the current thread, but need
1554 * an explicit one otherwise. The thread reference keeps
1555 * the corresponding task data structures around too. This
1556 * reference is released by thread_deallocate.
1559 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1562 /* If successful return with ast set */
1563 sig_thread_act
= get_signalthread(p
, signum
);
1565 if (sig_thread_act
== THREAD_NULL
) {
1567 * if it is sigkill, may be we should
1568 * inject a thread to terminate
1572 #endif /* SIGNAL_DEBUG */
1576 uth
= get_bsdthread_info(sig_thread_act
);
1579 * If proc is traced, always give parent a chance.
1581 if (p
->p_flag
& P_TRACED
)
1585 * If the signal is being ignored,
1586 * then we forget about it immediately.
1587 * (Note: we don't set SIGCONT in p_sigignore,
1588 * and if it is set to SIG_IGN,
1589 * action will be SIG_DFL here.)
1591 if (p
->p_sigignore
& mask
)
1593 /* sigwait takes precedence */
1594 if (uth
->uu_sigwait
& mask
)
1595 action
= KERN_SIG_WAIT
;
1596 else if (uth
->uu_sigmask
& mask
)
1597 action
= KERN_SIG_HOLD
;
1598 else if (p
->p_sigcatch
& mask
)
1599 action
= KERN_SIG_CATCH
;
1604 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1605 (p
->p_flag
& P_TRACED
) == 0)
1608 if (prop
& SA_CONT
) {
1609 uth
->uu_siglist
&= ~stopsigmask
;
1610 p
->p_siglist
&= ~stopsigmask
;
1613 if (prop
& SA_STOP
) {
1615 * If sending a tty stop signal to a member of an orphaned
1616 * process group, discard the signal here if the action
1617 * is default; don't stop the process below if sleeping,
1618 * and don't clear any pending SIGCONT.
1620 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1623 uth
->uu_siglist
&= ~contsigmask
;
1624 p
->p_siglist
&= ~contsigmask
;
1626 uth
->uu_siglist
|= mask
;
1627 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1631 * Defer further processing for signals which are held,
1632 * except that stopped processes must be continued by SIGCONT.
1634 if (action
== KERN_SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
)) {
1638 * SIGKILL priority twiddling moved here from above because
1639 * it needs sig_thread. Could merge it into large switch
1640 * below if we didn't care about priority for tracing
1641 * as SIGKILL's action is always SIG_DFL.
1643 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1648 * Process is traced - wake it up (if not already
1649 * stopped) so that it can discover the signal in
1650 * issig() and stop for the parent.
1652 if (p
->p_flag
& P_TRACED
) {
1653 if (p
->p_stat
!= SSTOP
)
1659 if (action
== KERN_SIG_WAIT
) {
1660 uth
->uu_sigwait
= mask
;
1661 uth
->uu_siglist
&= ~mask
;
1662 p
->p_siglist
&= ~mask
;
1663 wakeup(&uth
->uu_sigwait
);
1664 /* if it is SIGCONT resume whole process */
1665 if (prop
& SA_CONT
) {
1666 p
->p_flag
|= P_CONTINUED
;
1667 (void) task_resume(sig_task
);
1672 if (action
!= SIG_DFL
) {
1674 * User wants to catch the signal.
1675 * Wake up the thread, but don't un-suspend it
1676 * (except for SIGCONT).
1678 if (prop
& SA_CONT
) {
1679 if (p
->p_flag
& P_TTYSLEEP
) {
1680 p
->p_flag
&= ~P_TTYSLEEP
;
1681 wakeup(&p
->p_siglist
);
1683 p
->p_flag
|= P_CONTINUED
;
1684 (void) task_resume(sig_task
);
1687 } else if (p
->p_stat
== SSTOP
)
1691 /* Default action - varies */
1692 if (mask
& stopsigmask
) {
1694 * These are the signals which by default
1697 * Don't clog system with children of init
1698 * stopped from the keyboard.
1700 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1701 psignal_lock(p
, SIGKILL
, 0);
1702 uth
->uu_siglist
&= ~mask
;
1703 p
->p_siglist
&= ~mask
;
1709 * if task hasn't already been stopped by
1712 uth
->uu_siglist
&= ~mask
;
1713 p
->p_siglist
&= ~mask
;
1714 if (p
->p_stat
!= SSTOP
) {
1715 p
->p_xstat
= signum
;
1717 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1718 struct proc
*pp
= p
->p_pptr
;
1720 pp
->si_pid
= p
->p_pid
;
1721 pp
->si_status
= p
->p_xstat
;
1722 pp
->si_code
= CLD_STOPPED
;
1723 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
1724 psignal(pp
, SIGCHLD
);
1732 * Signals ignored by default have been dealt
1733 * with already, since their bits are on in
1739 * Kill signal always sets process running and
1743 * Process will be running after 'run'
1747 thread_abort(sig_thread_act
);
1753 * Let the process run. If it's sleeping on an
1754 * event, it remains so.
1756 if (p
->p_flag
& P_TTYSLEEP
) {
1757 p
->p_flag
&= ~P_TTYSLEEP
;
1758 wakeup(&p
->p_siglist
);
1760 p
->p_flag
|= P_CONTINUED
;
1761 (void) task_resume(sig_task
);
1763 uth
->uu_siglist
&= ~mask
;
1764 p
->p_siglist
&= ~mask
;
1771 * All other signals wake up the process, but don't
1774 if (p
->p_stat
== SSTOP
)
1782 * If we're being traced (possibly because someone attached us
1783 * while we were stopped), check for a signal from the debugger.
1785 if (p
->p_stat
== SSTOP
) {
1786 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
1787 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1790 * setrunnable(p) in BSD and
1791 * Wake up the thread if it is interruptible.
1794 thread_abort_safely(sig_thread_act
);
1800 thread_funnel_set(kernel_flock
, funnel_state
);
1804 /* psignal_lock(p, signum, withlock ) */
1806 psignal_uthread(thr_act
, signum
)
1812 register sig_t action
;
1813 thread_t sig_thread_act
;
1814 register task_t sig_task
;
1816 struct uthread
*uth
;
1820 p
= (struct proc
*)get_bsdtask_info(get_threadtask(thr_act
));
1821 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1822 panic("Invalid signal number in psignal_uthread");
1823 mask
= sigmask(signum
);
1824 prop
= sigprop
[signum
];
1827 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1830 #endif /* SIGNAL_DEBUG */
1833 * We will need the task pointer later. Grab it now to
1834 * check for a zombie process. Also don't send signals
1835 * to kernel internal tasks.
1837 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1841 sig_thread_act
= thr_act
;
1843 * do not send signals to the process that has the thread
1844 * doing a reboot(). Not doing so will mark that thread aborted
1845 * and can cause IO failures wich will cause data loss.
1847 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1854 * Deliver the signal to the first thread in the task. This
1855 * allows single threaded applications which use signals to
1856 * be able to be linked with multithreaded libraries. We have
1857 * an implicit reference to the current thread, but need
1858 * an explicit one otherwise. The thread reference keeps
1859 * the corresponding task data structures around too. This
1860 * reference is released by thread_deallocate.
1863 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1866 kret
= check_actforsig(sig_task
, sig_thread_act
, 1);
1868 if (kret
!= KERN_SUCCESS
) {
1874 uth
= get_bsdthread_info(sig_thread_act
);
1877 * If proc is traced, always give parent a chance.
1879 if (p
->p_flag
& P_TRACED
)
1883 * If the signal is being ignored,
1884 * then we forget about it immediately.
1885 * (Note: we don't set SIGCONT in p_sigignore,
1886 * and if it is set to SIG_IGN,
1887 * action will be SIG_DFL here.)
1889 if (p
->p_sigignore
& mask
)
1891 /* sigwait takes precedence */
1892 if (uth
->uu_sigwait
& mask
)
1893 action
= KERN_SIG_WAIT
;
1894 else if (uth
->uu_sigmask
& mask
)
1895 action
= KERN_SIG_HOLD
;
1896 else if (p
->p_sigcatch
& mask
)
1897 action
= KERN_SIG_CATCH
;
1902 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1903 (p
->p_flag
& P_TRACED
) == 0)
1906 if (prop
& SA_CONT
) {
1907 uth
->uu_siglist
&= ~stopsigmask
;
1908 p
->p_siglist
&= ~stopsigmask
;
1911 if (prop
& SA_STOP
) {
1913 * If sending a tty stop signal to a member of an orphaned
1914 * process group, discard the signal here if the action
1915 * is default; don't stop the process below if sleeping,
1916 * and don't clear any pending SIGCONT.
1918 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1921 uth
->uu_siglist
&= ~contsigmask
;
1922 p
->p_siglist
&= ~contsigmask
;
1924 uth
->uu_siglist
|= mask
;
1925 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1928 * Defer further processing for signals which are held,
1929 * except that stopped processes must be continued by SIGCONT.
1931 if (action
== KERN_SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
))
1935 * SIGKILL priority twiddling moved here from above because
1936 * it needs sig_thread. Could merge it into large switch
1937 * below if we didn't care about priority for tracing
1938 * as SIGKILL's action is always SIG_DFL.
1940 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1945 * Process is traced - wake it up (if not already
1946 * stopped) so that it can discover the signal in
1947 * issig() and stop for the parent.
1949 if (p
->p_flag
& P_TRACED
) {
1950 if (p
->p_stat
!= SSTOP
)
1956 if (action
== KERN_SIG_WAIT
) {
1957 uth
->uu_sigwait
= mask
;
1958 uth
->uu_siglist
&= ~mask
;
1959 p
->p_siglist
&= ~mask
;
1960 wakeup(&uth
->uu_sigwait
);
1961 /* if it is SIGCONT resume whole process */
1962 if (prop
& SA_CONT
) {
1963 p
->p_flag
|= P_CONTINUED
;
1964 (void) task_resume(sig_task
);
1969 if (action
!= SIG_DFL
) {
1971 * User wants to catch the signal.
1972 * Wake up the thread, but don't un-suspend it
1973 * (except for SIGCONT).
1975 if (prop
& SA_CONT
) {
1976 p
->p_flag
|= P_CONTINUED
;
1977 (void) task_resume(sig_task
);
1981 /* Default action - varies */
1982 if (mask
& stopsigmask
) {
1984 * These are the signals which by default
1987 * Don't clog system with children of init
1988 * stopped from the keyboard.
1990 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1991 psignal_lock(p
, SIGKILL
, 0);
1992 uth
->uu_siglist
&= ~mask
;
1993 p
->p_siglist
&= ~mask
;
1999 * if task hasn't already been stopped by
2002 uth
->uu_siglist
&= ~mask
;
2003 p
->p_siglist
&= ~mask
;
2004 if (p
->p_stat
!= SSTOP
) {
2005 p
->p_xstat
= signum
;
2006 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
2007 struct proc
*pp
= p
->p_pptr
;
2009 pp
->si_pid
= p
->p_pid
;
2010 pp
->si_status
= p
->p_xstat
;
2011 pp
->si_code
= CLD_STOPPED
;
2012 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
2013 psignal(pp
, SIGCHLD
);
2022 * Signals ignored by default have been dealt
2023 * with already, since their bits are on in
2029 * Kill signal always sets process running and
2033 * Process will be running after 'run'
2037 thread_abort(sig_thread_act
);
2043 * Let the process run. If it's sleeping on an
2044 * event, it remains so.
2046 if (p
->p_flag
& P_TTYSLEEP
) {
2047 p
->p_flag
&= ~P_TTYSLEEP
;
2048 wakeup(&p
->p_siglist
);
2050 p
->p_flag
|= P_CONTINUED
;
2051 (void) task_resume(sig_task
);
2053 uth
->uu_siglist
&= ~mask
;
2054 p
->p_siglist
&= ~mask
;
2060 * All other signals wake up the process, but don't
2069 * If we're being traced (possibly because someone attached us
2070 * while we were stopped), check for a signal from the debugger.
2072 if (p
->p_stat
== SSTOP
) {
2073 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
2074 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
2075 p
->p_siglist
|= sigmask(p
->p_xstat
);
2079 * setrunnable(p) in BSD and
2080 * Wake up the thread if it is interruptible.
2083 thread_abort_safely(sig_thread_act
);
2092 sig_lock_to_exit(struct proc
*p
)
2094 thread_t self
= current_thread();
2096 p
->exit_thread
= self
;
2097 (void) task_suspend(p
->task
);
2101 sig_try_locked(struct proc
*p
)
2103 thread_t self
= current_thread();
2105 while (p
->sigwait
|| p
->exit_thread
) {
2106 if (p
->exit_thread
) {
2107 if (p
->exit_thread
!= self
) {
2109 * Already exiting - no signals.
2115 if(assert_wait_possible()) {
2116 assert_wait((caddr_t
)&p
->sigwait_thread
,
2117 (THREAD_INTERRUPTIBLE
));
2120 thread_block(THREAD_CONTINUE_NULL
);
2122 if (thread_should_abort(self
)) {
2124 * Terminate request - clean up.
2133 * If the current process has received a signal (should be caught or cause
2134 * termination, should interrupt current syscall), return the signal number.
2135 * Stop signals with default action are processed immediately, then cleared;
2136 * they aren't returned. This is checked after each entry to the system for
2137 * a syscall or trap (though this can usually be done without calling issignal
2138 * by checking the pending signal masks in the CURSIG macro.) The normal call
2141 * while (signum = CURSIG(curproc))
2146 register struct proc
*p
;
2148 register int signum
, mask
, prop
, sigbits
;
2150 struct uthread
* ut
;
2153 cur_act
= current_thread();
2156 if(rdebug_proc
&& (p
== rdebug_proc
)) {
2159 #endif /* SIGNAL_DEBUG */
2163 * Try to grab the signal lock.
2165 if (sig_try_locked(p
) <= 0) {
2170 ut
= get_bsdthread_info(cur_act
);
2172 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2174 if (p
->p_flag
& P_PPWAIT
)
2175 sigbits
&= ~stopsigmask
;
2176 if (sigbits
== 0) { /* no signal to send */
2180 signum
= ffs((long)sigbits
);
2181 mask
= sigmask(signum
);
2182 prop
= sigprop
[signum
];
2185 * We should see pending but ignored signals
2186 * only if P_TRACED was on when they were posted.
2188 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2189 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2190 p
->p_siglist
&= ~mask
; /* take the signal! */
2193 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2194 register task_t task
;
2196 * If traced, always stop, and stay
2197 * stopped until released by the debugger.
2199 /* ptrace debugging */
2200 p
->p_xstat
= signum
;
2202 if (p
->p_flag
& P_SIGEXC
) {
2204 p
->sigwait_thread
= cur_act
;
2206 p
->p_flag
&= ~(P_WAITED
|P_CONTINUED
);
2207 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2208 p
->p_siglist
&= ~mask
; /* clear the old signal */
2210 do_bsdexception(EXC_SOFTWARE
, EXC_SOFT_SIGNAL
, signum
);
2213 // panic("Unsupportef gdb option \n");;
2214 pp
->si_pid
= p
->p_pid
;
2215 pp
->si_status
= p
->p_xstat
;
2216 pp
->si_code
= CLD_TRAPPED
;
2217 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
2218 psignal(pp
, SIGCHLD
);
2220 * XXX Have to really stop for debuggers;
2221 * XXX stop() doesn't do the right thing.
2222 * XXX Inline the task_suspend because we
2223 * XXX have to diddle Unix state in the
2229 p
->sigwait_thread
= cur_act
;
2231 p
->p_flag
&= ~(P_WAITED
|P_CONTINUED
);
2232 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2233 p
->p_siglist
&= ~mask
; /* clear the old signal */
2235 wakeup((caddr_t
)p
->p_pptr
);
2237 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
2238 thread_block(THREAD_CONTINUE_NULL
);
2243 p
->sigwait_thread
= NULL
;
2244 wakeup((caddr_t
)&p
->sigwait_thread
);
2247 * This code is to detect when gdb is killed
2248 * even as the traced program is attached.
2249 * pgsignal would get the SIGKILL to traced program
2250 * That's what we are trying to see (I hope)
2252 if (ut
->uu_siglist
& sigmask(SIGKILL
)) {
2254 * Wait event may still be outstanding;
2255 * clear it, since sig_lock_to_exit will
2258 clear_wait(current_thread(), THREAD_INTERRUPTED
);
2259 sig_lock_to_exit(p
);
2261 * Since this thread will be resumed
2262 * to allow the current syscall to
2263 * be completed, must save u_qsave
2264 * before calling exit(). (Since exit()
2265 * calls closef() which can trash u_qsave.)
2268 exit1(p
,signum
, (int *)NULL
);
2273 * We may have to quit
2275 if (thread_should_abort(current_thread())) {
2280 * If parent wants us to take the signal,
2281 * then it will leave it in p->p_xstat;
2282 * otherwise we just look for signals again.
2284 signum
= p
->p_xstat
;
2288 * Put the new signal into p_siglist. If the
2289 * signal is being masked, look for other signals.
2291 mask
= sigmask(signum
);
2292 ut
->uu_siglist
|= mask
;
2293 p
->p_siglist
|= mask
; /* just for lame ones looking here */
2294 if (ut
->uu_sigmask
& mask
)
2299 * Decide whether the signal should be returned.
2300 * Return the signal's number, or fall through
2301 * to clear it from the pending mask.
2304 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2308 * Don't take default actions on system processes.
2310 if (p
->p_pptr
->p_pid
== 0) {
2313 * Are you sure you want to ignore SIGSEGV
2316 printf("Process (pid %d) got signal %d\n",
2319 break; /* == ignore */
2323 * If there is a pending stop signal to process
2324 * with default action, stop here,
2325 * then clear the signal. However,
2326 * if process is member of an orphaned
2327 * process group, ignore tty stop signals.
2329 if (prop
& SA_STOP
) {
2330 if (p
->p_flag
& P_TRACED
||
2331 (p
->p_pgrp
->pg_jobc
== 0 &&
2333 break; /* == ignore */
2334 if (p
->p_stat
!= SSTOP
) {
2335 p
->p_xstat
= signum
;
2337 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
2339 pp
->si_pid
= p
->p_pid
;
2340 pp
->si_status
= p
->p_xstat
;
2341 pp
->si_code
= CLD_STOPPED
;
2342 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
2343 psignal(pp
, SIGCHLD
);
2347 } else if (prop
& SA_IGNORE
) {
2349 * Except for SIGCONT, shouldn't get here.
2350 * Default action is to ignore; drop it.
2352 break; /* == ignore */
2354 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2355 p
->p_siglist
&= ~mask
; /* take the signal! */
2363 * Masking above should prevent us ever trying
2364 * to take action on an ignored signal other
2365 * than SIGCONT, unless process is traced.
2367 if ((prop
& SA_CONT
) == 0 &&
2368 (p
->p_flag
& P_TRACED
) == 0)
2369 printf("issignal\n");
2370 break; /* == ignore */
2374 * This signal has an action, let
2375 * postsig() process it.
2377 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2378 p
->p_siglist
&= ~mask
; /* take the signal! */
2382 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2383 p
->p_siglist
&= ~mask
; /* take the signal! */
2388 /* called from _sleep */
2391 register struct proc
*p
;
2393 register int signum
, mask
, prop
, sigbits
;
2395 struct uthread
* ut
;
2399 cur_act
= current_thread();
2401 ut
= get_bsdthread_info(cur_act
);
2403 if (ut
->uu_siglist
== 0)
2406 if (((ut
->uu_siglist
& ~ut
->uu_sigmask
) == 0) && ((p
->p_flag
& P_TRACED
) == 0))
2409 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2412 if (p
->p_flag
& P_PPWAIT
)
2413 sigbits
&= ~stopsigmask
;
2414 if (sigbits
== 0) { /* no signal to send */
2418 signum
= ffs((long)sigbits
);
2419 mask
= sigmask(signum
);
2420 prop
= sigprop
[signum
];
2423 * We should see pending but ignored signals
2424 * only if P_TRACED was on when they were posted.
2426 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2429 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2431 * Put the new signal into p_siglist. If the
2432 * signal is being masked, look for other signals.
2434 mask
= sigmask(signum
);
2435 if (ut
->uu_sigmask
& mask
)
2441 * Decide whether the signal should be returned.
2442 * Return the signal's number, or fall through
2443 * to clear it from the pending mask.
2446 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2450 * Don't take default actions on system processes.
2452 if (p
->p_pptr
->p_pid
== 0) {
2455 * Are you sure you want to ignore SIGSEGV
2458 printf("Process (pid %d) got signal %d\n",
2461 break; /* == ignore */
2465 * If there is a pending stop signal to process
2466 * with default action, stop here,
2467 * then clear the signal. However,
2468 * if process is member of an orphaned
2469 * process group, ignore tty stop signals.
2471 if (prop
& SA_STOP
) {
2472 if (p
->p_flag
& P_TRACED
||
2473 (p
->p_pgrp
->pg_jobc
== 0 &&
2475 break; /* == ignore */
2478 } else if (prop
& SA_IGNORE
) {
2480 * Except for SIGCONT, shouldn't get here.
2481 * Default action is to ignore; drop it.
2483 break; /* == ignore */
2491 * Masking above should prevent us ever trying
2492 * to take action on an ignored signal other
2493 * than SIGCONT, unless process is traced.
2495 if ((prop
& SA_CONT
) == 0 &&
2496 (p
->p_flag
& P_TRACED
) == 0)
2497 printf("issignal\n");
2498 break; /* == ignore */
2502 * This signal has an action, let
2503 * postsig() process it.
2507 sigbits
&= ~mask
; /* take the signal! */
2513 * Put the argument process into the stopped state and notify the parent
2514 * via wakeup. Signals are handled elsewhere. The process must not be
2519 register struct proc
*p
;
2522 p
->p_flag
&= ~(P_WAITED
|P_CONTINUED
);
2523 if (p
->p_pptr
->p_stat
!= SSTOP
)
2524 wakeup((caddr_t
)p
->p_pptr
);
2525 (void) task_suspend(p
->task
); /*XXX*/
2529 * Take the action for the specified signal
2530 * from the current set of pending signals.
2535 struct proc
*p
= current_proc();
2536 struct sigacts
*ps
= p
->p_sigacts
;
2537 user_addr_t catcher
;
2539 int mask
, returnmask
;
2540 struct uthread
* ut
;
2546 * This must be called on master cpu
2548 if (cpu_number() != master_cpu
)
2549 panic("psig not on master");
2554 * Try to grab the signal lock.
2556 if (sig_try_locked(p
) <= 0) {
2561 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2562 mask
= sigmask(signum
);
2563 ut
->uu_siglist
&= ~mask
;
2564 p
->p_siglist
&= ~mask
;
2565 catcher
= ps
->ps_sigact
[signum
];
2567 //LP64: catcher argument is a 64 bit user space handler address
2568 if (KTRPOINT(p
, KTR_PSIG
))
2569 ktrpsig(p
->p_tracep
,
2570 signum
, CAST_DOWN(void *,catcher
), ut
->uu_flag
& UT_SAS_OLDMASK
?
2571 &ut
->uu_oldmask
: &ut
->uu_sigmask
, 0);
2573 if (catcher
== SIG_DFL
) {
2575 * Default catcher, where the default is to kill
2576 * the process. (Other cases were ignored above.)
2578 /* called with signal_lock() held */
2579 sigexit_locked(p
, signum
);
2584 * If we get here, the signal must be caught.
2587 if (catcher
== SIG_IGN
|| (ut
->uu_sigmask
& mask
))
2589 "postsig: processing masked or ignored signal\n");
2592 * Set the new mask value and also defer further
2593 * occurences of this signal.
2595 * Special case: user has done a sigpause. Here the
2596 * current mask is not of interest, but rather the
2597 * mask from before the sigpause is what we want
2598 * restored after the signal processing is completed.
2600 if (ut
->uu_flag
& UT_SAS_OLDMASK
) {
2601 returnmask
= ut
->uu_oldmask
;
2602 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
2605 returnmask
= ut
->uu_sigmask
;
2606 ut
->uu_sigmask
|= ps
->ps_catchmask
[signum
];
2607 if ((ps
->ps_signodefer
& mask
) == 0)
2608 ut
->uu_sigmask
|= mask
;
2609 if ((signum
!= SIGILL
) && (signum
!= SIGTRAP
) && (ps
->ps_sigreset
& mask
)) {
2610 if ((signum
!= SIGCONT
) && (sigprop
[signum
] & SA_IGNORE
))
2611 p
->p_sigignore
|= mask
;
2612 ps
->ps_sigact
[signum
] = SIG_DFL
;
2613 ps
->ps_siginfo
&= ~mask
;
2614 ps
->ps_signodefer
&= ~mask
;
2617 /* Needs to disable to run in user mode */
2618 if (signum
== SIGFPE
) {
2619 thread_enable_fpe(current_thread(), 0);
2621 #endif /* __ppc__ */
2623 if (ps
->ps_sig
!= signum
) {
2629 p
->p_stats
->p_ru
.ru_nsignals
++;
2630 sendsig(p
, catcher
, signum
, returnmask
, code
);
2636 * Force the current process to exit with the specified signal, dumping core
2637 * if appropriate. We bypass the normal tests for masked and caught signals,
2638 * allowing unrecoverable failures to terminate the process without changing
2639 * signal state. Mark the accounting record with the signal termination.
2640 * If dumping core, save the signal number for the debugger. Calls exit and
2643 /* called with signal lock */
2645 sigexit_locked(p
, signum
)
2646 register struct proc
*p
;
2650 sig_lock_to_exit(p
);
2651 p
->p_acflag
|= AXSIG
;
2652 if (sigprop
[signum
] & SA_CORE
) {
2653 p
->p_sigacts
->ps_sig
= signum
;
2655 if (coredump(p
) == 0)
2656 signum
|= WCOREFLAG
;
2660 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
2666 filt_sigattach(struct knote
*kn
)
2668 struct proc
*p
= current_proc();
2669 boolean_t funnel_state
;
2671 kn
->kn_ptr
.p_proc
= p
;
2672 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
2674 /* Take the funnel to protect the proc while adding to the list */
2675 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2676 KNOTE_ATTACH(&p
->p_klist
, kn
);
2677 thread_funnel_set(kernel_flock
, funnel_state
);
2683 filt_sigdetach(struct knote
*kn
)
2685 struct proc
*p
= kn
->kn_ptr
.p_proc
;
2686 boolean_t funnel_state
;
2688 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2689 KNOTE_DETACH(&p
->p_klist
, kn
);
2690 thread_funnel_set(kernel_flock
, funnel_state
);
2694 * signal knotes are shared with proc knotes, so we apply a mask to
2695 * the hint in order to differentiate them from process hints. This
2696 * could be avoided by using a signal-specific knote list, but probably
2697 * isn't worth the trouble.
2700 filt_signal(struct knote
*kn
, long hint
)
2703 if (hint
& NOTE_SIGNAL
) {
2704 hint
&= ~NOTE_SIGNAL
;
2706 if (kn
->kn_id
== (unsigned int)hint
)
2709 return (kn
->kn_data
!= 0);
2714 bsd_ast(thread_t thr_act
)
2716 struct proc
*p
= current_proc();
2717 struct uthread
*ut
= get_bsdthread_info(thr_act
);
2720 boolean_t funnel_state
;
2721 static int bsd_init_done
= 0;
2726 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2728 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
2729 pc
= get_useraddr();
2730 addupc_task(p
, pc
, 1);
2731 p
->p_flag
&= ~P_OWEUPC
;
2734 if (CHECK_SIGNALS(p
, current_thread(), ut
)) {
2735 while ( (signum
= issignal(p
)) )
2738 if (!bsd_init_done
) {
2743 (void) thread_funnel_set(kernel_flock
, FALSE
);
2747 * Follwing routines are called using callout from bsd_hardclock
2748 * so that psignals are called in a thread context and are funneled
2751 psignal_vtalarm(struct proc
*p
)
2753 boolean_t funnel_state
;
2757 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2758 psignal_lock(p
, SIGVTALRM
, 1);
2759 (void) thread_funnel_set(kernel_flock
, FALSE
);
2763 psignal_xcpu(struct proc
*p
)
2765 boolean_t funnel_state
;
2769 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2770 psignal_lock(p
, SIGXCPU
, 1);
2771 (void) thread_funnel_set(kernel_flock
, FALSE
);
2775 psignal_sigprof(struct proc
*p
)
2777 boolean_t funnel_state
;
2781 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2782 psignal_lock(p
, SIGPROF
, 1);
2783 (void) thread_funnel_set(kernel_flock
, FALSE
);
2786 /* ptrace set runnalbe */
2788 pt_setrunnable(struct proc
*p
)
2794 if (p
->p_flag
& P_TRACED
) {
2797 wakeup((caddr_t
)&(p
->sigwait
));
2810 exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
2814 return(bsd_exception(exc
, codes
, 2));
2818 proc_pendingsignals(struct proc
*p
, sigset_t mask
)
2820 struct uthread
* uth
;
2825 /* If the process is in proc exit return no signal info */
2826 if (p
->p_lflag
& P_LPEXIT
)
2829 /* duplicate the signal lock code to enable recursion; as exit
2830 * holds the lock too long. All this code is being reworked
2831 * this is just a workaround for regressions till new code
2835 error
= lockmgr((struct lock__bsd__
*)&p
->signal_lock
[0], (LK_EXCLUSIVE
| LK_CANRECURSE
), 0, (struct proc
*)0);
2839 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
2841 uth
= (struct uthread
*)get_bsdthread_info(th
);
2843 bits
= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
2849 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
2850 bits
|= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);
2858 thread_issignal(proc_t p
, thread_t th
, sigset_t mask
)
2860 struct uthread
* uth
;
2864 uth
= (struct uthread
*)get_bsdthread_info(th
);
2866 bits
= (((uth
->uu_siglist
& ~uth
->uu_sigmask
) & ~p
->p_sigignore
) & mask
);