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/namei.h>
67 #include <sys/vnode.h>
69 #include <sys/systm.h>
70 #include <sys/timeb.h>
71 #include <sys/times.h>
75 #include <sys/kernel.h>
77 #include <sys/signalvar.h>
79 #include <sys/ktrace.h>
81 #include <sys/syslog.h>
84 #include <sys/kdebug.h>
86 #include <sys/mount.h>
88 #include <kern/cpu_number.h>
91 #include <sys/user.h> /* for coredump */
92 #include <kern/ast.h> /* for APC support */
93 #include <kern/thread.h>
94 #include <kern/sched_prim.h>
95 #include <kern/thread_call.h>
96 #include <mach/exception.h>
98 extern void doexception(int exc
, int code
, int sub
);
100 void stop
__P((struct proc
*p
));
101 int cansignal
__P((struct proc
*, struct pcred
*, struct proc
*, int));
102 int killpg1
__P((struct proc
*, int, int, int));
103 void sigexit_locked
__P((struct proc
*, int));
104 int setsigvec
__P((struct proc
*, int, struct __sigaction
*));
105 void exit1
__P((struct proc
*, int, int *));
106 int signal_lock
__P((struct proc
*));
107 int signal_unlock
__P((struct proc
*));
108 void signal_setast
__P((thread_act_t
));
109 void psignal_lock
__P((struct proc
*, int, int));
110 void psignal_uthread
__P((thread_act_t
, int));
111 kern_return_t
do_bsdexception(int, int, int);
114 void ram_printf
__P((int));
116 unsigned int rdebug_proc
=0;
123 #endif /* SIGNAL_DEBUG */
126 signal_lock(struct proc
*p
)
133 int register sp
, *fp
, numsaved
;
135 __asm__
volatile("mr %0,r1" : "=r" (sp
));
137 fp
= (int *)*((int *)sp
);
138 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
139 p
->lockpc
[numsaved
] = fp
[2];
146 #endif /* SIGNAL_DEBUG */
147 #endif /* DIAGNOSTIC */
150 /* TBD: check p last arg */
151 error
= lockmgr(&p
->signal_lock
, LK_EXCLUSIVE
, 0, (struct proc
*)p
);
158 signal_unlock(struct proc
*p
)
164 int register sp
, *fp
, numsaved
;
166 __asm__
volatile("mr %0,r1" : "=r" (sp
));
168 fp
= (int *)*((int *)sp
);
169 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
170 p
->unlockpc
[numsaved
] = fp
[2];
177 #endif /* SIGNAL_DEBUG */
178 #endif /* DIAGNOSTIC */
180 /* TBD: check p last arg */
181 return(lockmgr(&p
->signal_lock
, LK_RELEASE
, (simple_lock_t
)0, (struct proc
*)p
));
185 signal_setast(sig_actthread
)
186 thread_act_t sig_actthread
;
188 act_set_astbsd(sig_actthread
);
192 * Can process p, with pcred pc, send the signal signum to process q?
195 cansignal(p
, pc
, q
, signum
)
201 /* you can signal yourself */
205 if (pc
->pc_ucred
->cr_uid
== 0)
206 return (1); /* root can always signal */
208 if (signum
== SIGCONT
&& q
->p_session
== p
->p_session
)
209 return (1); /* SIGCONT in session */
212 * Using kill(), only certain signals can be sent to setugid
215 if (q
->p_flag
& P_SUGID
) {
228 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
229 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
230 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
231 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
238 * because the P_SUGID test exists, this has extra tests which
241 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
242 pc
->p_ruid
== q
->p_cred
->p_svuid
||
243 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
244 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_svuid
||
245 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
246 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
251 struct sigaction_args
{
253 struct __sigaction
*nsa
;
254 struct sigaction
*osa
;
259 sigaction(p
, uap
, retval
)
261 register struct sigaction_args
*uap
;
264 struct sigaction vec
;
265 struct __sigaction __vec
;
267 register struct sigaction
*sa
;
268 register struct sigacts
*ps
= p
->p_sigacts
;
272 signum
= uap
->signum
;
273 if (signum
<= 0 || signum
>= NSIG
||
274 signum
== SIGKILL
|| signum
== SIGSTOP
)
278 sa
->sa_handler
= ps
->ps_sigact
[signum
];
279 sa
->sa_mask
= ps
->ps_catchmask
[signum
];
280 bit
= sigmask(signum
);
282 if ((ps
->ps_sigonstack
& bit
) != 0)
283 sa
->sa_flags
|= SA_ONSTACK
;
284 if ((ps
->ps_sigintr
& bit
) == 0)
285 sa
->sa_flags
|= SA_RESTART
;
286 if (ps
->ps_siginfo
& bit
)
287 sa
->sa_flags
|= SA_SIGINFO
;
288 if (ps
->ps_signodefer
& bit
)
289 sa
->sa_flags
|= SA_NODEFER
;
290 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDSTOP
))
291 sa
->sa_flags
|= SA_NOCLDSTOP
;
292 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDWAIT
))
293 sa
->sa_flags
|= SA_NOCLDWAIT
;
294 if (error
= copyout((caddr_t
)sa
, (caddr_t
)uap
->osa
,
299 if (error
= copyin((caddr_t
)uap
->nsa
, (caddr_t
)&__vec
,
302 error
= setsigvec(p
, signum
, &__vec
);
307 /* Routines to manipulate bits on all threads */
309 clear_procsiglist(struct proc
*p
, int bit
)
311 struct uthread
* uth
;
316 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
317 thact
= p
->p_vforkact
;
318 uth
= (struct uthread
*)get_bsdthread_info(thact
);
320 uth
->uu_siglist
&= ~bit
;
322 p
->p_siglist
&= ~bit
;
327 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
328 uth
->uu_siglist
&= ~bit
;
330 p
->p_siglist
&= ~bit
;
336 unblock_procsigmask(struct proc
*p
, int bit
)
338 struct uthread
* uth
;
342 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
343 thact
= p
->p_vforkact
;
344 uth
= (struct uthread
*)get_bsdthread_info(thact
);
346 uth
->uu_sigmask
&= ~bit
;
348 p
->p_sigmask
&= ~bit
;
352 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
353 uth
->uu_sigmask
&= ~bit
;
355 p
->p_sigmask
&= ~bit
;
362 block_procsigmask(struct proc
*p
, int bit
)
364 struct uthread
* uth
;
368 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
369 thact
= p
->p_vforkact
;
370 uth
= (struct uthread
*)get_bsdthread_info(thact
);
372 uth
->uu_sigmask
|= bit
;
378 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
379 uth
->uu_sigmask
|= bit
;
386 set_procsigmask(struct proc
*p
, int bit
)
388 struct uthread
* uth
;
392 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
393 thact
= p
->p_vforkact
;
394 uth
= (struct uthread
*)get_bsdthread_info(thact
);
396 uth
->uu_sigmask
= bit
;
402 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
403 uth
->uu_sigmask
= bit
;
411 setsigvec(p
, signum
, sa
)
412 register struct proc
*p
;
414 register struct __sigaction
*sa
;
416 register struct sigacts
*ps
= p
->p_sigacts
;
419 if ((signum
== SIGKILL
|| signum
== SIGSTOP
) &&
420 sa
->sa_handler
!= SIG_DFL
)
422 bit
= sigmask(signum
);
424 * Change setting atomically.
426 ps
->ps_sigact
[signum
] = sa
->sa_handler
;
427 ps
->ps_trampact
[signum
] = sa
->sa_tramp
;
428 ps
->ps_catchmask
[signum
] = sa
->sa_mask
&~ sigcantmask
;
429 if (sa
->sa_flags
& SA_SIGINFO
)
430 ps
->ps_siginfo
|= bit
;
432 ps
->ps_siginfo
&= ~bit
;
433 if ((sa
->sa_flags
& SA_RESTART
) == 0)
434 ps
->ps_sigintr
|= bit
;
436 ps
->ps_sigintr
&= ~bit
;
437 if (sa
->sa_flags
& SA_ONSTACK
)
438 ps
->ps_sigonstack
|= bit
;
440 ps
->ps_sigonstack
&= ~bit
;
441 if (sa
->sa_flags
& SA_USERTRAMP
)
442 ps
->ps_usertramp
|= bit
;
444 ps
->ps_usertramp
&= ~bit
;
445 if (sa
->sa_flags
& SA_RESETHAND
)
446 ps
->ps_sigreset
|= bit
;
448 ps
->ps_sigreset
&= ~bit
;
449 if (sa
->sa_flags
& SA_NODEFER
)
450 ps
->ps_signodefer
|= bit
;
452 ps
->ps_signodefer
&= ~bit
;
453 if (signum
== SIGCHLD
) {
454 if (sa
->sa_flags
& SA_NOCLDSTOP
)
455 p
->p_flag
|= P_NOCLDSTOP
;
457 p
->p_flag
&= ~P_NOCLDSTOP
;
458 if ((sa
->sa_flags
& SA_NOCLDWAIT
) || (sa
->sa_handler
== SIG_IGN
))
459 p
->p_flag
|= P_NOCLDWAIT
;
461 p
->p_flag
&= ~P_NOCLDWAIT
;
465 if (signum
== SIGFPE
) {
466 if (sa
->sa_handler
== SIG_DFL
|| sa
->sa_handler
== SIG_IGN
)
467 thread_enable_fpe(current_act(), 0);
469 thread_enable_fpe(current_act(), 1);
473 * Set bit in p_sigignore for signals that are set to SIG_IGN,
474 * and for signals set to SIG_DFL where the default is to ignore.
475 * However, don't put SIGCONT in p_sigignore,
476 * as we have to restart the process.
478 if (sa
->sa_handler
== SIG_IGN
||
479 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
481 clear_procsiglist(p
, bit
);
482 if (signum
!= SIGCONT
)
483 p
->p_sigignore
|= bit
; /* easier in psignal */
484 p
->p_sigcatch
&= ~bit
;
486 p
->p_sigignore
&= ~bit
;
487 if (sa
->sa_handler
== SIG_DFL
)
488 p
->p_sigcatch
&= ~bit
;
490 p
->p_sigcatch
|= bit
;
496 * Initialize signal state for process 0;
497 * set to ignore signals that are ignored by default.
505 for (i
= 0; i
< NSIG
; i
++)
506 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
507 p
->p_sigignore
|= sigmask(i
);
511 * Reset signals for an exec of the specified process.
515 register struct proc
*p
;
516 register thread_act_t thr_act
;
518 register struct sigacts
*ps
= p
->p_sigacts
;
519 register int nc
, mask
;
523 * Reset caught signals. Held signals remain held
524 * through p_sigmask (unless they were caught,
525 * and are now ignored by default).
527 while (p
->p_sigcatch
) {
528 nc
= ffs((long)p
->p_sigcatch
);
530 p
->p_sigcatch
&= ~mask
;
531 if (sigprop
[nc
] & SA_IGNORE
) {
533 p
->p_sigignore
|= mask
;
535 ut
= (struct uthread
*)get_bsdthread_info(thr_act
);
536 ut
->uu_siglist
&= ~mask
;
537 p
->p_siglist
&= ~mask
;
539 clear_procsiglist(p
, mask
);
541 ps
->ps_sigact
[nc
] = SIG_DFL
;
544 * Reset stack state to the user stack.
545 * Clear set of signals caught on the signal stack.
547 ps
->ps_sigstk
.ss_flags
= SA_DISABLE
;
548 ps
->ps_sigstk
.ss_size
= 0;
549 ps
->ps_sigstk
.ss_sp
= 0;
554 * Manipulate signal mask.
555 * Note that we receive new mask, not pointer,
556 * and return old mask as return value;
557 * the library stub does the rest.
559 struct sigprocmask_args
{
565 sigprocmask(p
, uap
, retval
)
566 register struct proc
*p
;
567 struct sigprocmask_args
*uap
;
571 sigset_t oldmask
, nmask
;
572 sigset_t
* omask
= uap
->omask
;
575 ut
= (struct uthread
*)get_bsdthread_info(current_act());
576 oldmask
= ut
->uu_sigmask
;
578 if (uap
->mask
== (sigset_t
*)0) {
579 /* just want old mask */
582 error
= copyin((caddr_t
)uap
->mask
, &nmask
, sizeof(sigset_t
));
588 block_procsigmask(p
, (nmask
& ~sigcantmask
));
589 signal_setast(current_act());
593 unblock_procsigmask(p
, (nmask
& ~sigcantmask
));
594 signal_setast(current_act());
598 set_procsigmask(p
, (nmask
& ~sigcantmask
));
599 signal_setast(current_act());
608 copyout(&oldmask
, omask
, sizeof(sigset_t
));
612 struct sigpending_args
{
616 sigpending(p
, uap
, retval
)
618 register struct sigpending_args
*uap
;
624 ut
= (struct uthread
*)get_bsdthread_info(current_act());
625 pendlist
= ut
->uu_siglist
;
628 copyout(&pendlist
, uap
->osv
, sizeof(sigset_t
));
634 * Generalized interface signal handler, 4.3-compatible.
636 struct osigvec_args
{
643 osigvec(p
, uap
, retval
)
645 register struct osigvec_args
*uap
;
650 register struct sigacts
*ps
= p
->p_sigacts
;
651 register struct sigvec
*sv
;
655 panic("osigvec: notsupp");
657 signum
= uap
->signum
;
658 if (signum
<= 0 || signum
>= NSIG
||
659 signum
== SIGKILL
|| signum
== SIGSTOP
)
663 *(sig_t
*)&sv
->sv_handler
= ps
->ps_sigact
[signum
];
664 sv
->sv_mask
= ps
->ps_catchmask
[signum
];
665 bit
= sigmask(signum
);
667 if ((ps
->ps_sigonstack
& bit
) != 0)
668 sv
->sv_flags
|= SV_ONSTACK
;
669 if ((ps
->ps_sigintr
& bit
) != 0)
670 sv
->sv_flags
|= SV_INTERRUPT
;
671 if (p
->p_flag
& P_NOCLDSTOP
)
672 sv
->sv_flags
|= SA_NOCLDSTOP
;
673 if (error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->osv
,
678 if (error
= copyin((caddr_t
)uap
->nsv
, (caddr_t
)sv
,
681 sv
->sv_flags
^= SA_RESTART
; /* opposite of SV_INTERRUPT */
682 error
= setsigvec(p
, signum
, (struct sigaction
*)sv
);
688 struct osigblock_args
{
692 osigblock(p
, uap
, retval
)
693 register struct proc
*p
;
694 struct osigblock_args
*uap
;
697 struct uthread
* uth
= get_bsdthread_info(current_act());
699 *retval
= uth
->uu_sigmask
;
700 uth
->uu_sigmask
|= (uap
->mask
& ~sigcantmask
);
704 struct osigsetmask_args
{
708 osigsetmask(p
, uap
, retval
)
710 struct osigsetmask_args
*uap
;
713 struct uthread
* uth
= get_bsdthread_info(current_act());
715 *retval
= uth
->uu_sigmask
;
716 uth
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
719 #endif /* COMPAT_43 */
722 * Suspend process until signal, providing mask to be set
723 * in the meantime. Note nonstandard calling convention:
724 * libc stub passes mask, not pointer, to save a copyin.
730 struct uthread
*ut
= get_bsdthread_info(current_act());
731 unix_syscall_return(EINTR
);
734 struct sigsuspend_args
{
740 sigsuspend(p
, uap
, retval
)
741 register struct proc
*p
;
742 struct sigsuspend_args
*uap
;
745 register struct sigacts
*ps
= p
->p_sigacts
;
748 ut
= (struct uthread
*)get_bsdthread_info(current_act());
751 * When returning from sigpause, we want
752 * the old mask to be restored after the
753 * signal handler has finished. Thus, we
754 * save it here and mark the sigacts structure
757 ut
->uu_oldmask
= ut
->uu_sigmask
;
758 ut
->uu_flag
|= USAS_OLDMASK
;
759 ut
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
760 (void) tsleep0((caddr_t
) p
, PPAUSE
|PCATCH
, "pause", 0, sigcontinue
);
761 /* always return EINTR rather than ERESTART... */
765 struct __disable_thsignal_args
{
770 __disable_threadsignal(p
, uap
, retval
)
772 register struct __disable_thsignal_args
*uap
;
777 uth
= (struct uthread
*)get_bsdthread_info(current_act());
779 /* No longer valid to have any signal delivered */
781 uth
->uu_flag
|= UNO_SIGMASK
;
788 struct pthread_kill_args
{
794 __pthread_kill(p
, uap
, retval
)
796 register struct pthread_kill_args
*uap
;
799 thread_act_t target_act
;
801 int signum
= uap
->sig
;
804 target_act
= (thread_act_t
)port_name_to_act(uap
->thread_port
);
806 if (target_act
== THR_ACT_NULL
)
808 if ((u_int
)signum
>= NSIG
) {
813 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
814 { void *tht
= getshuttle_thread(target_act
);
816 if (uth
->uu_flag
& UNO_SIGMASK
) {
822 psignal_uthread(target_act
, signum
);
824 act_deallocate(target_act
);
829 struct pthread_sigmask_args
{
835 pthread_sigmask(p
, uap
, retval
)
836 register struct proc
*p
;
837 register struct pthread_sigmask_args
*uap
;
841 const sigset_t
*set
= uap
->set
;
842 sigset_t
* oset
= uap
->oset
;
848 ut
= (struct uthread
*)get_bsdthread_info(current_act());
849 oldset
= ut
->uu_sigmask
;
851 if (set
== (sigset_t
*) 0) {
852 /* need only old mask */
856 error
= copyin((caddr_t
)set
, (caddr_t
)&nset
, sizeof(sigset_t
));
862 ut
->uu_sigmask
|= (nset
& ~sigcantmask
);
866 ut
->uu_sigmask
&= ~(nset
);
867 signal_setast(current_act());
871 ut
->uu_sigmask
= (nset
& ~sigcantmask
);
872 signal_setast(current_act());
881 copyout((caddr_t
)&oldset
, (caddr_t
)oset
, sizeof(sigset_t
));
887 struct sigwait_args
{
893 sigwait(p
, uap
, retval
)
894 register struct proc
*p
;
895 register struct sigwait_args
*uap
;
898 register struct sigacts
*ps
= p
->p_sigacts
;
908 ut
= (struct uthread
*)get_bsdthread_info(current_act());
910 if (uap
->set
== (const sigset_t
*)0)
913 error
= copyin((caddr_t
)uap
->set
, (caddr_t
)&mask
, sizeof(sigset_t
));
917 siglist
= (mask
& ~sigcantmask
);
923 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
927 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
928 if (sigw
= uth
->uu_siglist
& siglist
) {
935 /* The signal was pending on a thread */
939 * When returning from sigwait, we want
940 * the old mask to be restored after the
941 * signal handler has finished. Thus, we
942 * save it here and mark the sigacts structure
945 ut
->uu_oldmask
= ut
->uu_sigmask
;
946 ut
->uu_flag
|= USAS_OLDMASK
;
947 if (siglist
== (sigset_t
)0)
949 /* SIGKILL and SIGSTOP are not maskable as well */
950 ut
->uu_sigmask
= ~(siglist
|sigcantmask
);
951 ut
->uu_sigwait
= siglist
;
952 /* No Continuations for now */
953 error
= tsleep((caddr_t
)&ut
->uu_sigwait
, PPAUSE
|PCATCH
, "pause", 0);
955 if ((error
== EINTR
) || (error
== ERESTART
))
958 sigw
= (ut
->uu_sigwait
& siglist
);
959 ut
->uu_sigmask
= ut
->uu_oldmask
;
961 ut
->uu_flag
&= ~USAS_OLDMASK
;
965 signum
= ffs((unsigned int)sigw
);
967 panic("sigwait with no signal wakeup");
968 ut
->uu_siglist
&= ~(sigmask(signum
));
970 error
= copyout(&signum
, uap
->sig
, sizeof(int));
978 struct osigstack_args
{
979 struct sigstack
*nss
;
980 struct sigstack
*oss
;
985 osigstack(p
, uap
, retval
)
987 register struct osigstack_args
*uap
;
995 ss
.ss_sp
= psp
->ps_sigstk
.ss_sp
;
996 ss
.ss_onstack
= psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
;
997 if (uap
->oss
&& (error
= copyout((caddr_t
)&ss
,
998 (caddr_t
)uap
->oss
, sizeof (struct sigstack
))))
1000 if (uap
->nss
&& (error
= copyin((caddr_t
)uap
->nss
,
1001 (caddr_t
)&ss
, sizeof (ss
))) == 0) {
1002 psp
->ps_sigstk
.ss_sp
= ss
.ss_sp
;
1003 psp
->ps_sigstk
.ss_size
= 0;
1004 psp
->ps_sigstk
.ss_flags
|= ss
.ss_onstack
& SA_ONSTACK
;
1005 psp
->ps_flags
|= SAS_ALTSTACK
;
1009 #endif /* COMPAT_43 */
1011 struct sigaltstack_args
{
1012 struct sigaltstack
*nss
;
1013 struct sigaltstack
*oss
;
1017 sigaltstack(p
, uap
, retval
)
1019 register struct sigaltstack_args
*uap
;
1022 struct sigacts
*psp
;
1023 struct sigaltstack ss
;
1027 if ((psp
->ps_flags
& SAS_ALTSTACK
) == 0)
1028 psp
->ps_sigstk
.ss_flags
|= SA_DISABLE
;
1029 if (uap
->oss
&& (error
= copyout((caddr_t
)&psp
->ps_sigstk
,
1030 (caddr_t
)uap
->oss
, sizeof (struct sigaltstack
))))
1034 if (error
= copyin((caddr_t
)uap
->nss
, (caddr_t
)&ss
,
1037 if ((ss
.ss_flags
& ~SA_DISABLE
) != 0) {
1041 if (ss
.ss_flags
& SA_DISABLE
) {
1042 if (psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
)
1044 psp
->ps_flags
&= ~SAS_ALTSTACK
;
1045 psp
->ps_sigstk
.ss_flags
= ss
.ss_flags
;
1048 if (ss
.ss_size
< MINSIGSTKSZ
)
1050 psp
->ps_flags
|= SAS_ALTSTACK
;
1061 kill(cp
, uap
, retval
)
1062 register struct proc
*cp
;
1063 register struct kill_args
*uap
;
1066 register struct proc
*p
;
1067 register struct pcred
*pc
= cp
->p_cred
;
1069 if ((u_int
)uap
->signum
>= NSIG
)
1072 /* kill single process */
1073 if ((p
= pfind(uap
->pid
)) == NULL
)
1075 if (!cansignal(cp
, pc
, p
, uap
->signum
))
1078 psignal(p
, uap
->signum
);
1082 case -1: /* broadcast signal */
1083 return (killpg1(cp
, uap
->signum
, 0, 1));
1084 case 0: /* signal own process group */
1085 return (killpg1(cp
, uap
->signum
, 0, 0));
1086 default: /* negative explicit process group */
1087 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0));
1093 struct okillpg_args
{
1099 okillpg(p
, uap
, retval
)
1101 register struct okillpg_args
*uap
;
1105 if ((u_int
)uap
->signum
>= NSIG
)
1107 return (killpg1(p
, uap
->signum
, uap
->pgid
, 0));
1109 #endif /* COMPAT_43 */
1112 * Common code for kill process group/broadcast kill.
1113 * cp is calling process.
1116 killpg1(cp
, signum
, pgid
, all
)
1117 register struct proc
*cp
;
1118 int signum
, pgid
, all
;
1120 register struct proc
*p
;
1121 register struct pcred
*pc
= cp
->p_cred
;
1129 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1130 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1131 p
== cp
|| !cansignal(cp
, pc
, p
, signum
))
1140 * zero pgid means send to my process group.
1144 pgrp
= pgfind(pgid
);
1148 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1149 p
= p
->p_pglist
.le_next
) {
1150 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1151 p
->p_stat
== SZOMB
||
1152 !cansignal(cp
, pc
, p
, signum
))
1159 return (nfound
? 0 : ESRCH
);
1163 * Send a signal to a process group.
1166 gsignal(pgid
, signum
)
1171 if (pgid
&& (pgrp
= pgfind(pgid
)))
1172 pgsignal(pgrp
, signum
, 0);
1176 * Send a signal to a process group. If checktty is 1,
1177 * limit to members which have a controlling terminal.
1180 pgsignal(pgrp
, signum
, checkctty
)
1182 int signum
, checkctty
;
1184 register struct proc
*p
;
1187 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1188 p
= p
->p_pglist
.le_next
)
1189 if (checkctty
== 0 || p
->p_flag
& P_CONTROLT
)
1194 * Send signal to a backgrounded process blocked due to tty access
1195 * In FreeBSD, the backgrounded process wakes up every second and
1196 * discovers whether it is foregounded or not. In our case, we block
1197 * the thread in tsleep as we want to avoid storm of processes as well
1198 * as the suspend is only at AST level
1201 tty_pgsignal(pgrp
, signum
)
1205 register struct proc
*p
;
1208 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1209 p
= p
->p_pglist
.le_next
)
1210 if ((p
->p_flag
& P_TTYSLEEP
) && (p
->p_flag
& P_CONTROLT
))
1215 * Send a signal caused by a trap to a specific thread.
1218 threadsignal(sig_actthread
, signum
, code
)
1219 register thread_act_t sig_actthread
;
1220 register int signum
;
1223 register struct uthread
*uth
;
1224 register struct task
* sig_task
;
1225 register struct proc
*p
;
1228 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1231 mask
= sigmask(signum
);
1232 if ((mask
& threadmask
) == 0)
1234 sig_task
= get_threadtask(sig_actthread
);
1235 p
= (struct proc
*)(get_bsdtask_info(sig_task
));
1237 uth
= get_bsdthread_info(sig_actthread
);
1238 if (uth
&& (uth
->uu_flag
& P_VFORK
))
1241 if (!(p
->p_flag
& P_TRACED
) && (p
->p_sigignore
& mask
))
1244 uth
->uu_siglist
|= mask
;
1245 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1246 uth
->uu_code
= code
;
1247 /* mark on process as well */
1248 signal_setast(sig_actthread
);
1254 register struct proc
*p
;
1255 register int signum
;
1257 psignal_lock(p
, signum
, 1);
1261 psignal_vfork(p
, new_task
, thr_act
, signum
)
1262 register struct proc
*p
;
1264 thread_act_t thr_act
;
1265 register int signum
;
1269 register int s
, prop
;
1270 register sig_t action
;
1273 struct uthread
*uth
;
1275 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1276 panic("psignal signal number");
1277 mask
= sigmask(signum
);
1278 prop
= sigprop
[signum
];
1281 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1284 #endif /* SIGNAL_DEBUG */
1286 if ((new_task
== TASK_NULL
) || (thr_act
== (thread_act_t
)NULL
) || is_kerneltask(new_task
))
1290 uth
= get_bsdthread_info(thr_act
);
1294 * proc is traced, always give parent a chance.
1298 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1299 (p
->p_flag
& P_TRACED
) == 0)
1302 if (prop
& SA_CONT
) {
1303 p
->p_siglist
&= ~stopsigmask
;
1304 uth
->uu_siglist
&= ~stopsigmask
;
1307 if (prop
& SA_STOP
) {
1309 * If sending a tty stop signal to a member of an orphaned
1310 * process group, discard the signal here if the action
1311 * is default; don't stop the process below if sleeping,
1312 * and don't clear any pending SIGCONT.
1314 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1317 uth
->uu_siglist
&= ~contsigmask
;
1318 p
->p_siglist
&= ~contsigmask
;
1320 uth
->uu_siglist
|= mask
;
1321 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1323 /* Deliver signal to the activation passed in */
1324 act_set_astbsd(thr_act
);
1327 * SIGKILL priority twiddling moved here from above because
1328 * it needs sig_thread. Could merge it into large switch
1329 * below if we didn't care about priority for tracing
1330 * as SIGKILL's action is always SIG_DFL.
1332 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1337 * This Process is traced - wake it up (if not already
1338 * stopped) so that it can discover the signal in
1339 * issig() and stop for the parent.
1341 if (p
->p_flag
& P_TRACED
) {
1342 if (p
->p_stat
!= SSTOP
)
1349 * If we're being traced (possibly because someone attached us
1350 * while we were stopped), check for a signal from the debugger.
1352 if (p
->p_stat
== SSTOP
) {
1353 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
1354 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1355 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1360 * setrunnable(p) in BSD
1369 get_signalthread(struct proc
*p
, int signum
)
1371 struct uthread
*uth
;
1372 thread_act_t thr_act
;
1373 sigset_t mask
= sigmask(signum
);
1374 thread_act_t sig_thread_act
;
1375 struct task
* sig_task
= p
->task
;
1376 thread_t sig_thread
;
1379 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
1380 sig_thread_act
= p
->p_vforkact
;
1381 kret
= check_actforsig(sig_task
, sig_thread_act
, &sig_thread
, 1);
1382 if (kret
== KERN_SUCCESS
)
1383 return(sig_thread_act
);
1385 return(THR_ACT_NULL
);
1388 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1389 if(((uth
->uu_flag
& UNO_SIGMASK
)== 0) &&
1390 (((uth
->uu_sigmask
& mask
) == 0) || (uth
->uu_sigwait
& mask
))) {
1391 if (check_actforsig(p
->task
, uth
->uu_act
, NULL
, 1) == KERN_SUCCESS
)
1392 return(uth
->uu_act
);
1395 if (get_signalact(p
->task
, &thr_act
, NULL
, 1) == KERN_SUCCESS
) {
1399 return(THR_ACT_NULL
);
1403 * Send the signal to the process. If the signal has an action, the action
1404 * is usually performed by the target process rather than the caller; we add
1405 * the signal to the set of pending signals for the process.
1408 * o When a stop signal is sent to a sleeping process that takes the
1409 * default action, the process is stopped without awakening it.
1410 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1411 * regardless of the signal action (eg, blocked or ignored).
1413 * Other ignored signals are discarded immediately.
1416 psignal_lock(p
, signum
, withlock
)
1417 register struct proc
*p
;
1418 register int signum
;
1419 register int withlock
;
1421 register int s
, prop
;
1422 register sig_t action
;
1423 thread_act_t sig_thread_act
;
1424 thread_t sig_thread
;
1425 register task_t sig_task
;
1426 register thread_t cur_thread
;
1427 thread_act_t cur_act
;
1429 struct uthread
*uth
;
1433 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1434 panic("psignal signal number");
1435 mask
= sigmask(signum
);
1436 prop
= sigprop
[signum
];
1439 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1442 #endif /* SIGNAL_DEBUG */
1444 if (thread_funnel_get() == (funnel_t
*)network_flock
) {
1446 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1449 * We will need the task pointer later. Grab it now to
1450 * check for a zombie process. Also don't send signals
1451 * to kernel internal tasks.
1453 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1455 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1460 * do not send signals to the process that has the thread
1461 * doing a reboot(). Not doing so will mark that thread aborted
1462 * and can cause IO failures wich will cause data loss.
1464 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1466 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1474 * Deliver the signal to the first thread in the task. This
1475 * allows single threaded applications which use signals to
1476 * be able to be linked with multithreaded libraries. We have
1477 * an implicit reference to the current_thread, but need
1478 * an explicit one otherwise. The thread reference keeps
1479 * the corresponding task data structures around too. This
1480 * reference is released by thread_deallocate.
1483 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1486 cur_thread
= current_thread(); /* this is a shuttle */
1487 cur_act
= current_act();
1489 /* If successful return with ast set */
1490 sig_thread_act
= get_signalthread(p
, signum
);
1492 if (sig_thread_act
== THR_ACT_NULL
) {
1494 /* if it is sigkill, may be we should
1495 * inject a thread to terminate
1499 #endif /* SIGNAL_DEBUG */
1503 uth
= get_bsdthread_info(sig_thread_act
);
1506 * If proc is traced, always give parent a chance.
1508 if (p
->p_flag
& P_TRACED
)
1512 * If the signal is being ignored,
1513 * then we forget about it immediately.
1514 * (Note: we don't set SIGCONT in p_sigignore,
1515 * and if it is set to SIG_IGN,
1516 * action will be SIG_DFL here.)
1518 if (p
->p_sigignore
& mask
)
1520 if (uth
->uu_sigwait
& mask
)
1522 if (uth
->uu_sigmask
& mask
)
1524 else if (p
->p_sigcatch
& mask
)
1530 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1531 (p
->p_flag
& P_TRACED
) == 0)
1534 if (prop
& SA_CONT
) {
1535 uth
->uu_siglist
&= ~stopsigmask
;
1536 p
->p_siglist
&= ~stopsigmask
;
1539 if (prop
& SA_STOP
) {
1541 * If sending a tty stop signal to a member of an orphaned
1542 * process group, discard the signal here if the action
1543 * is default; don't stop the process below if sleeping,
1544 * and don't clear any pending SIGCONT.
1546 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1549 uth
->uu_siglist
&= ~contsigmask
;
1550 p
->p_siglist
&= ~contsigmask
;
1552 uth
->uu_siglist
|= mask
;
1553 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1557 * Defer further processing for signals which are held,
1558 * except that stopped processes must be continued by SIGCONT.
1560 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
)) {
1564 * SIGKILL priority twiddling moved here from above because
1565 * it needs sig_thread. Could merge it into large switch
1566 * below if we didn't care about priority for tracing
1567 * as SIGKILL's action is always SIG_DFL.
1569 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1574 * Process is traced - wake it up (if not already
1575 * stopped) so that it can discover the signal in
1576 * issig() and stop for the parent.
1578 if (p
->p_flag
& P_TRACED
) {
1579 if (p
->p_stat
!= SSTOP
)
1585 if (action
== SIG_WAIT
) {
1586 uth
->uu_sigwait
= mask
;
1587 uth
->uu_siglist
&= ~mask
;
1588 p
->p_siglist
&= ~mask
;
1589 wakeup(&uth
->uu_sigwait
);
1590 /* if it is SIGCONT resume whole process */
1592 (void) task_resume(sig_task
);
1596 if (action
!= SIG_DFL
) {
1598 * User wants to catch the signal.
1599 * Wake up the thread, but don't un-suspend it
1600 * (except for SIGCONT).
1603 (void) task_resume(sig_task
);
1606 /* Default action - varies */
1607 if (mask
& stopsigmask
) {
1609 * These are the signals which by default
1612 * Don't clog system with children of init
1613 * stopped from the keyboard.
1615 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1616 psignal_lock(p
, SIGKILL
, 0);
1617 uth
->uu_siglist
&= ~mask
;
1618 p
->p_siglist
&= ~mask
;
1624 * if task hasn't already been stopped by
1627 uth
->uu_siglist
&= ~mask
;
1628 p
->p_siglist
&= ~mask
;
1629 if (p
->p_stat
!= SSTOP
) {
1630 p
->p_xstat
= signum
;
1632 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1633 struct proc
*pp
= p
->p_pptr
;
1635 pp
->si_pid
= p
->p_pid
;
1636 pp
->si_status
= p
->p_xstat
;
1637 pp
->si_code
= CLD_STOPPED
;
1638 pp
->si_uid
= p
->p_cred
->p_ruid
;
1639 psignal(pp
, SIGCHLD
);
1647 * Signals ignored by default have been dealt
1648 * with already, since their bits are on in
1654 * Kill signal always sets process running and
1658 * Process will be running after 'run'
1662 thread_abort(sig_thread_act
);
1668 * Let the process run. If it's sleeping on an
1669 * event, it remains so.
1671 if (p
->p_flag
& P_TTYSLEEP
) {
1672 p
->p_flag
&= ~P_TTYSLEEP
;
1673 wakeup(&p
->p_siglist
);
1675 (void) task_resume(sig_task
);
1677 uth
->uu_siglist
&= ~mask
;
1678 p
->p_siglist
&= ~mask
;
1685 * All other signals wake up the process, but don't
1694 * If we're being traced (possibly because someone attached us
1695 * while we were stopped), check for a signal from the debugger.
1697 if (p
->p_stat
== SSTOP
) {
1698 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
1699 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1702 * setrunnable(p) in BSD and
1703 * Wake up the thread if it is interruptible.
1706 thread_abort_safely(sig_thread_act
);
1712 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1716 /* psignal_lock(p, signum, withlock ) */
1718 psignal_uthread(thr_act
, signum
)
1719 thread_act_t thr_act
;
1723 register int s
, prop
;
1724 register sig_t action
;
1725 thread_act_t sig_thread_act
;
1726 thread_t sig_thread
;
1727 register task_t sig_task
;
1728 register thread_t cur_thread
;
1729 thread_act_t cur_act
;
1731 struct uthread
*uth
;
1735 p
= (struct proc
*)get_bsdtask_info(get_threadtask(thr_act
));
1736 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1737 panic("Invalid signal number in psignal_uthread");
1738 mask
= sigmask(signum
);
1739 prop
= sigprop
[signum
];
1742 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1745 #endif /* SIGNAL_DEBUG */
1748 * We will need the task pointer later. Grab it now to
1749 * check for a zombie process. Also don't send signals
1750 * to kernel internal tasks.
1752 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1756 sig_thread_act
= thr_act
;
1758 * do not send signals to the process that has the thread
1759 * doing a reboot(). Not doing so will mark that thread aborted
1760 * and can cause IO failures wich will cause data loss.
1762 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1769 * Deliver the signal to the first thread in the task. This
1770 * allows single threaded applications which use signals to
1771 * be able to be linked with multithreaded libraries. We have
1772 * an implicit reference to the current_thread, but need
1773 * an explicit one otherwise. The thread reference keeps
1774 * the corresponding task data structures around too. This
1775 * reference is released by thread_deallocate.
1778 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1781 cur_thread
= current_thread(); /* this is a shuttle */
1782 cur_act
= current_act();
1784 kret
= check_actforsig(sig_task
, sig_thread_act
, &sig_thread
, 1);
1786 if (kret
!= KERN_SUCCESS
) {
1792 uth
= get_bsdthread_info(sig_thread_act
);
1795 * If proc is traced, always give parent a chance.
1797 if (p
->p_flag
& P_TRACED
)
1801 * If the signal is being ignored,
1802 * then we forget about it immediately.
1803 * (Note: we don't set SIGCONT in p_sigignore,
1804 * and if it is set to SIG_IGN,
1805 * action will be SIG_DFL here.)
1807 if (p
->p_sigignore
& mask
)
1809 if (uth
->uu_sigwait
& mask
)
1811 if (uth
->uu_sigmask
& mask
)
1813 else if (p
->p_sigcatch
& mask
)
1819 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1820 (p
->p_flag
& P_TRACED
) == 0)
1823 if (prop
& SA_CONT
) {
1824 uth
->uu_siglist
&= ~stopsigmask
;
1825 p
->p_siglist
&= ~stopsigmask
;
1828 if (prop
& SA_STOP
) {
1830 * If sending a tty stop signal to a member of an orphaned
1831 * process group, discard the signal here if the action
1832 * is default; don't stop the process below if sleeping,
1833 * and don't clear any pending SIGCONT.
1835 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1838 uth
->uu_siglist
&= ~contsigmask
;
1839 p
->p_siglist
&= ~contsigmask
;
1841 uth
->uu_siglist
|= mask
;
1842 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1845 * Defer further processing for signals which are held,
1846 * except that stopped processes must be continued by SIGCONT.
1848 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
))
1852 * SIGKILL priority twiddling moved here from above because
1853 * it needs sig_thread. Could merge it into large switch
1854 * below if we didn't care about priority for tracing
1855 * as SIGKILL's action is always SIG_DFL.
1857 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1862 * Process is traced - wake it up (if not already
1863 * stopped) so that it can discover the signal in
1864 * issig() and stop for the parent.
1866 if (p
->p_flag
& P_TRACED
) {
1867 if (p
->p_stat
!= SSTOP
)
1873 if (action
== SIG_WAIT
) {
1874 uth
->uu_sigwait
= mask
;
1875 uth
->uu_siglist
&= ~mask
;
1876 p
->p_siglist
&= ~mask
;
1877 wakeup(&uth
->uu_sigwait
);
1878 /* if it is SIGCONT resume whole process */
1880 (void) task_resume(sig_task
);
1884 if (action
!= SIG_DFL
) {
1886 * User wants to catch the signal.
1887 * Wake up the thread, but don't un-suspend it
1888 * (except for SIGCONT).
1891 (void) task_resume(sig_task
);
1894 /* Default action - varies */
1895 if (mask
& stopsigmask
) {
1897 * These are the signals which by default
1900 * Don't clog system with children of init
1901 * stopped from the keyboard.
1903 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1904 psignal_lock(p
, SIGKILL
, 0);
1905 uth
->uu_siglist
&= ~mask
;
1906 p
->p_siglist
&= ~mask
;
1912 * if task hasn't already been stopped by
1915 uth
->uu_siglist
&= ~mask
;
1916 p
->p_siglist
&= ~mask
;
1917 if (p
->p_stat
!= SSTOP
) {
1918 p
->p_xstat
= signum
;
1919 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1920 struct proc
*pp
= p
->p_pptr
;
1922 pp
->si_pid
= p
->p_pid
;
1923 pp
->si_status
= p
->p_xstat
;
1924 pp
->si_code
= CLD_STOPPED
;
1925 pp
->si_uid
= p
->p_cred
->p_ruid
;
1926 psignal(pp
, SIGCHLD
);
1935 * Signals ignored by default have been dealt
1936 * with already, since their bits are on in
1942 * Kill signal always sets process running and
1946 * Process will be running after 'run'
1950 thread_abort(sig_thread_act
);
1956 * Let the process run. If it's sleeping on an
1957 * event, it remains so.
1959 if (p
->p_flag
& P_TTYSLEEP
) {
1960 p
->p_flag
&= ~P_TTYSLEEP
;
1961 wakeup(&p
->p_siglist
);
1963 (void) task_resume(sig_task
);
1965 uth
->uu_siglist
&= ~mask
;
1966 p
->p_siglist
&= ~mask
;
1972 * All other signals wake up the process, but don't
1981 * If we're being traced (possibly because someone attached us
1982 * while we were stopped), check for a signal from the debugger.
1984 if (p
->p_stat
== SSTOP
) {
1985 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
1986 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1987 p
->p_siglist
|= sigmask(p
->p_xstat
);
1991 * setrunnable(p) in BSD and
1992 * Wake up the thread if it is interruptible.
1995 thread_abort_safely(sig_thread_act
);
2007 thread_t self
= current_thread();
2009 p
->exit_thread
= self
;
2010 (void) task_suspend(p
->task
);
2017 thread_t self
= current_thread();
2019 while (p
->sigwait
|| p
->exit_thread
) {
2020 if (p
->exit_thread
) {
2021 if (p
->exit_thread
!= self
) {
2023 * Already exiting - no signals.
2025 thread_abort(current_act());
2029 if(assert_wait_possible()) {
2030 assert_wait((caddr_t
)&p
->sigwait_thread
,
2031 (THREAD_INTERRUPTIBLE
));
2034 thread_block(THREAD_CONTINUE_NULL
);
2036 if (thread_should_abort(self
)) {
2038 * Terminate request - clean up.
2047 * If the current process has received a signal (should be caught or cause
2048 * termination, should interrupt current syscall), return the signal number.
2049 * Stop signals with default action are processed immediately, then cleared;
2050 * they aren't returned. This is checked after each entry to the system for
2051 * a syscall or trap (though this can usually be done without calling issignal
2052 * by checking the pending signal masks in the CURSIG macro.) The normal call
2055 * while (signum = CURSIG(curproc))
2060 register struct proc
*p
;
2062 register int signum
, mask
, prop
, sigbits
;
2063 task_t task
= p
->task
;
2064 thread_t cur_thread
;
2065 thread_act_t cur_act
;
2067 struct uthread
* ut
;
2071 cur_thread
= current_thread();
2072 cur_act
= current_act();
2075 if(rdebug_proc
&& (p
== rdebug_proc
)) {
2078 #endif /* SIGNAL_DEBUG */
2082 * Try to grab the signal lock.
2084 if (sig_try_locked(p
) <= 0) {
2089 ut
= get_bsdthread_info(cur_act
);
2091 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2093 if (p
->p_flag
& P_PPWAIT
)
2094 sigbits
&= ~stopsigmask
;
2095 if (sigbits
== 0) { /* no signal to send */
2099 signum
= ffs((long)sigbits
);
2100 mask
= sigmask(signum
);
2101 prop
= sigprop
[signum
];
2104 * We should see pending but ignored signals
2105 * only if P_TRACED was on when they were posted.
2107 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2108 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2109 p
->p_siglist
&= ~mask
; /* take the signal! */
2112 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2114 register task_t task
;
2116 * If traced, always stop, and stay
2117 * stopped until released by the debugger.
2119 /* ptrace debugging */
2120 p
->p_xstat
= signum
;
2122 if (p
->p_flag
& P_SIGEXC
) {
2124 p
->sigwait_thread
= cur_act
;
2126 p
->p_flag
&= ~P_WAITED
;
2127 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2128 p
->p_siglist
&= ~mask
; /* clear the old signal */
2130 do_bsdexception(EXC_SOFTWARE
, EXC_SOFT_SIGNAL
, signum
);
2133 pp
->si_pid
= p
->p_pid
;
2134 pp
->si_status
= p
->p_xstat
;
2135 pp
->si_code
= CLD_TRAPPED
;
2136 pp
->si_uid
= p
->p_cred
->p_ruid
;
2137 psignal(pp
, SIGCHLD
);
2139 * XXX Have to really stop for debuggers;
2140 * XXX stop() doesn't do the right thing.
2141 * XXX Inline the task_suspend because we
2142 * XXX have to diddle Unix state in the
2148 p
->sigwait_thread
= cur_act
;
2150 p
->p_flag
&= ~P_WAITED
;
2151 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2152 p
->p_siglist
&= ~mask
; /* clear the old signal */
2154 wakeup((caddr_t
)p
->p_pptr
);
2156 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
2157 thread_block(THREAD_CONTINUE_NULL
);
2162 p
->sigwait_thread
= NULL
;
2163 wakeup((caddr_t
)&p
->sigwait_thread
);
2166 * This code is to detect when gdb is killed
2167 * even as the traced program is attached.
2168 * pgsignal would get the SIGKILL to traced program
2169 * That's what we are trying to see (I hope)
2171 if (ut
->uu_siglist
& sigmask(SIGKILL
)) {
2173 * Wait event may still be outstanding;
2174 * clear it, since sig_lock_to_exit will
2177 clear_wait(current_thread(), THREAD_INTERRUPTED
);
2178 sig_lock_to_exit(p
);
2180 * Since this thread will be resumed
2181 * to allow the current syscall to
2182 * be completed, must save u_qsave
2183 * before calling exit(). (Since exit()
2184 * calls closef() which can trash u_qsave.)
2187 exit1(p
,signum
, (int *)NULL
);
2192 * We may have to quit
2194 if (thread_should_abort(current_thread())) {
2199 * If parent wants us to take the signal,
2200 * then it will leave it in p->p_xstat;
2201 * otherwise we just look for signals again.
2203 signum
= p
->p_xstat
;
2207 * Put the new signal into p_siglist. If the
2208 * signal is being masked, look for other signals.
2210 mask
= sigmask(signum
);
2211 ut
->uu_siglist
|= mask
;
2212 p
->p_siglist
|= mask
; /* just for lame ones looking here */
2213 if (ut
->uu_sigmask
& mask
)
2218 * Decide whether the signal should be returned.
2219 * Return the signal's number, or fall through
2220 * to clear it from the pending mask.
2223 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2227 * Don't take default actions on system processes.
2229 if (p
->p_pptr
->p_pid
== 0) {
2232 * Are you sure you want to ignore SIGSEGV
2235 printf("Process (pid %d) got signal %d\n",
2238 break; /* == ignore */
2242 * If there is a pending stop signal to process
2243 * with default action, stop here,
2244 * then clear the signal. However,
2245 * if process is member of an orphaned
2246 * process group, ignore tty stop signals.
2248 if (prop
& SA_STOP
) {
2249 if (p
->p_flag
& P_TRACED
||
2250 (p
->p_pgrp
->pg_jobc
== 0 &&
2252 break; /* == ignore */
2253 if (p
->p_stat
!= SSTOP
) {
2254 p
->p_xstat
= signum
;
2256 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
2258 pp
->si_pid
= p
->p_pid
;
2259 pp
->si_status
= p
->p_xstat
;
2260 pp
->si_code
= CLD_STOPPED
;
2261 pp
->si_uid
= p
->p_cred
->p_ruid
;
2262 psignal(pp
, SIGCHLD
);
2266 } else if (prop
& SA_IGNORE
) {
2268 * Except for SIGCONT, shouldn't get here.
2269 * Default action is to ignore; drop it.
2271 break; /* == ignore */
2273 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2274 p
->p_siglist
&= ~mask
; /* take the signal! */
2282 * Masking above should prevent us ever trying
2283 * to take action on an ignored signal other
2284 * than SIGCONT, unless process is traced.
2286 if ((prop
& SA_CONT
) == 0 &&
2287 (p
->p_flag
& P_TRACED
) == 0)
2288 printf("issignal\n");
2289 break; /* == ignore */
2293 * This signal has an action, let
2294 * postsig() process it.
2296 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2297 p
->p_siglist
&= ~mask
; /* take the signal! */
2301 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2302 p
->p_siglist
&= ~mask
; /* take the signal! */
2307 /* called from _sleep */
2310 register struct proc
*p
;
2312 register int signum
, mask
, prop
, sigbits
;
2313 task_t task
= p
->task
;
2314 thread_t cur_thread
;
2315 thread_act_t cur_act
;
2317 struct uthread
* ut
;
2321 cur_thread
= current_thread();
2322 cur_act
= current_act();
2324 ut
= get_bsdthread_info(cur_act
);
2326 if (ut
->uu_siglist
== 0)
2329 if (((ut
->uu_siglist
& ~ut
->uu_sigmask
) == 0) && ((p
->p_flag
& P_TRACED
) == 0))
2332 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2335 if (p
->p_flag
& P_PPWAIT
)
2336 sigbits
&= ~stopsigmask
;
2337 if (sigbits
== 0) { /* no signal to send */
2341 signum
= ffs((long)sigbits
);
2342 mask
= sigmask(signum
);
2343 prop
= sigprop
[signum
];
2346 * We should see pending but ignored signals
2347 * only if P_TRACED was on when they were posted.
2349 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2352 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2354 * Put the new signal into p_siglist. If the
2355 * signal is being masked, look for other signals.
2357 mask
= sigmask(signum
);
2358 if (ut
->uu_sigmask
& mask
)
2364 * Decide whether the signal should be returned.
2365 * Return the signal's number, or fall through
2366 * to clear it from the pending mask.
2369 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2373 * Don't take default actions on system processes.
2375 if (p
->p_pptr
->p_pid
== 0) {
2378 * Are you sure you want to ignore SIGSEGV
2381 printf("Process (pid %d) got signal %d\n",
2384 break; /* == ignore */
2388 * If there is a pending stop signal to process
2389 * with default action, stop here,
2390 * then clear the signal. However,
2391 * if process is member of an orphaned
2392 * process group, ignore tty stop signals.
2394 if (prop
& SA_STOP
) {
2395 if (p
->p_flag
& P_TRACED
||
2396 (p
->p_pgrp
->pg_jobc
== 0 &&
2398 break; /* == ignore */
2401 } else if (prop
& SA_IGNORE
) {
2403 * Except for SIGCONT, shouldn't get here.
2404 * Default action is to ignore; drop it.
2406 break; /* == ignore */
2414 * Masking above should prevent us ever trying
2415 * to take action on an ignored signal other
2416 * than SIGCONT, unless process is traced.
2418 if ((prop
& SA_CONT
) == 0 &&
2419 (p
->p_flag
& P_TRACED
) == 0)
2420 printf("issignal\n");
2421 break; /* == ignore */
2425 * This signal has an action, let
2426 * postsig() process it.
2430 sigbits
&= ~mask
; /* take the signal! */
2436 * Put the argument process into the stopped state and notify the parent
2437 * via wakeup. Signals are handled elsewhere. The process must not be
2442 register struct proc
*p
;
2445 p
->p_flag
&= ~P_WAITED
;
2446 wakeup((caddr_t
)p
->p_pptr
);
2447 (void) task_suspend(p
->task
); /*XXX*/
2451 * Take the action for the specified signal
2452 * from the current set of pending signals.
2456 register int signum
;
2458 register struct proc
*p
= current_proc();
2459 register struct sigacts
*ps
= p
->p_sigacts
;
2460 register sig_t action
;
2462 int mask
, returnmask
;
2463 struct uthread
* ut
;
2469 * This must be called on master cpu
2471 if (cpu_number() != master_cpu
)
2472 panic("psig not on master");
2477 * Try to grab the signal lock.
2479 if (sig_try_locked(p
) <= 0) {
2484 ut
= (struct uthread
*)get_bsdthread_info(current_act());
2485 mask
= sigmask(signum
);
2486 ut
->uu_siglist
&= ~mask
;
2487 p
->p_siglist
&= ~mask
;
2488 action
= ps
->ps_sigact
[signum
];
2490 if (KTRPOINT(p
, KTR_PSIG
))
2491 ktrpsig(p
->p_tracep
,
2492 signum
, action
, ut
->uu_flag
& USAS_OLDMASK
?
2493 &ut
->uu_oldmask
: &ut
->uu_sigmask
, 0, -1);
2495 if (action
== SIG_DFL
) {
2497 * Default action, where the default is to kill
2498 * the process. (Other cases were ignored above.)
2500 /* called with signal_lock() held */
2501 sigexit_locked(p
, signum
);
2506 * If we get here, the signal must be caught.
2509 if (action
== SIG_IGN
|| (ut
->uu_sigmask
& mask
))
2511 "postsig: processing masked or ignored signal\n");
2514 * Set the new mask value and also defer further
2515 * occurences of this signal.
2517 * Special case: user has done a sigpause. Here the
2518 * current mask is not of interest, but rather the
2519 * mask from before the sigpause is what we want
2520 * restored after the signal processing is completed.
2522 if (ut
->uu_flag
& USAS_OLDMASK
) {
2523 returnmask
= ut
->uu_oldmask
;
2524 ut
->uu_flag
&= ~USAS_OLDMASK
;
2527 returnmask
= ut
->uu_sigmask
;
2528 ut
->uu_sigmask
|= ps
->ps_catchmask
[signum
];
2529 if ((ps
->ps_signodefer
& mask
) == 0)
2530 ut
->uu_sigmask
|= mask
;
2531 if ((signum
!= SIGILL
) && (signum
!= SIGTRAP
) && (ps
->ps_sigreset
& mask
)) {
2532 if ((signum
!= SIGCONT
) && (sigprop
[signum
] & SA_IGNORE
))
2533 p
->p_sigignore
|= mask
;
2534 ps
->ps_sigact
[signum
] = SIG_DFL
;
2535 ps
->ps_siginfo
&= ~mask
;
2536 ps
->ps_signodefer
&= ~mask
;
2539 /* Needs to disable to run in user mode */
2540 if (signum
== SIGFPE
) {
2541 thread_enable_fpe(current_act(), 0);
2543 #endif /* __ppc__ */
2545 if (ps
->ps_sig
!= signum
) {
2551 p
->p_stats
->p_ru
.ru_nsignals
++;
2552 sendsig(p
, action
, signum
, returnmask
, code
);
2558 * Force the current process to exit with the specified signal, dumping core
2559 * if appropriate. We bypass the normal tests for masked and caught signals,
2560 * allowing unrecoverable failures to terminate the process without changing
2561 * signal state. Mark the accounting record with the signal termination.
2562 * If dumping core, save the signal number for the debugger. Calls exit and
2565 /* called with signal lock */
2567 sigexit_locked(p
, signum
)
2568 register struct proc
*p
;
2572 sig_lock_to_exit(p
);
2573 p
->p_acflag
|= AXSIG
;
2574 if (sigprop
[signum
] & SA_CORE
) {
2575 p
->p_sigacts
->ps_sig
= signum
;
2576 if (coredump(p
) == 0)
2577 signum
|= WCOREFLAG
;
2580 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
2585 bsd_ast(thread_act_t thr_act
)
2587 struct proc
*p
= current_proc();
2588 struct uthread
*ut
= get_bsdthread_info(thr_act
);
2591 boolean_t funnel_state
;
2592 static bsd_init_done
= 0;
2597 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2599 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
2600 pc
= get_useraddr();
2601 addupc_task(p
, pc
, 1);
2602 p
->p_flag
&= ~P_OWEUPC
;
2605 if (CHECK_SIGNALS(p
, current_thread(), ut
)) {
2606 while (signum
= issignal(p
))
2609 if (!bsd_init_done
) {
2610 extern void bsdinit_task(void);
2616 (void) thread_funnel_set(kernel_flock
, FALSE
);
2620 * Follwing routines are called using callout from bsd_hardclock
2621 * so that psignals are called in a thread context and are funneled
2624 psignal_vtalarm(struct proc
*p
)
2626 boolean_t funnel_state
;
2630 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2631 psignal_lock(p
, SIGVTALRM
, 1);
2632 (void) thread_funnel_set(kernel_flock
, FALSE
);
2636 psignal_xcpu(struct proc
*p
)
2638 boolean_t funnel_state
;
2642 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2643 psignal_lock(p
, SIGXCPU
, 1);
2644 (void) thread_funnel_set(kernel_flock
, FALSE
);
2648 psignal_sigprof(struct proc
*p
)
2650 boolean_t funnel_state
;
2654 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2655 psignal_lock(p
, SIGPROF
, 1);
2656 (void) thread_funnel_set(kernel_flock
, FALSE
);
2659 /* ptrace set runnalbe */
2661 pt_setrunnable(struct proc
*p
)
2667 if (p
->p_flag
& P_TRACED
) {
2670 wakeup((caddr_t
)&(p
->sigwait
));
2682 exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
2683 extern kern_return_t
bsd_exception(int, exception_data_type_t codes
[], int);
2687 return(bsd_exception(exc
, codes
, 2));