2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995-1998 Apple Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1989, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
29 * (c) UNIX System Laboratories, Inc.
30 * All or some portions of this file are derived from material licensed
31 * to the University of California by American Telephone and Telegraph
32 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
33 * the permission of UNIX System Laboratories, Inc.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
66 #define SIGPROP /* include signal properties table */
67 #include <sys/param.h>
68 #include <sys/resourcevar.h>
69 #include <sys/namei.h>
70 #include <sys/vnode.h>
72 #include <sys/systm.h>
73 #include <sys/timeb.h>
74 #include <sys/times.h>
78 #include <sys/kernel.h>
80 #include <sys/signalvar.h>
82 #include <sys/ktrace.h>
84 #include <sys/syslog.h>
87 #include <sys/kdebug.h>
89 #include <sys/mount.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/thread.h>
97 #include <kern/sched_prim.h>
98 #include <kern/thread_call.h>
99 #include <mach/exception.h>
101 extern void doexception(int exc
, int code
, int sub
);
103 void stop
__P((struct proc
*p
));
104 int cansignal
__P((struct proc
*, struct pcred
*, struct proc
*, int));
105 int killpg1
__P((struct proc
*, int, int, int));
106 void sigexit_locked
__P((struct proc
*, int));
107 int setsigvec
__P((struct proc
*, int, struct __sigaction
*));
108 void exit1
__P((struct proc
*, int, int *));
109 int signal_lock
__P((struct proc
*));
110 int signal_unlock
__P((struct proc
*));
111 void signal_setast
__P((thread_act_t
));
112 void psignal_lock
__P((struct proc
*, int, int));
113 void psignal_uthread
__P((thread_act_t
, int));
114 kern_return_t
do_bsdexception(int, int, int);
117 void ram_printf
__P((int));
119 unsigned int rdebug_proc
=0;
126 #endif /* SIGNAL_DEBUG */
129 signal_lock(struct proc
*p
)
136 int register sp
, *fp
, numsaved
;
138 __asm__
volatile("mr %0,r1" : "=r" (sp
));
140 fp
= (int *)*((int *)sp
);
141 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
142 p
->lockpc
[numsaved
] = fp
[2];
149 #endif /* SIGNAL_DEBUG */
150 #endif /* DIAGNOSTIC */
153 /* TBD: check p last arg */
154 error
= lockmgr(&p
->signal_lock
, LK_EXCLUSIVE
, 0, (struct proc
*)p
);
161 signal_unlock(struct proc
*p
)
167 int register sp
, *fp
, numsaved
;
169 __asm__
volatile("mr %0,r1" : "=r" (sp
));
171 fp
= (int *)*((int *)sp
);
172 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
173 p
->unlockpc
[numsaved
] = fp
[2];
180 #endif /* SIGNAL_DEBUG */
181 #endif /* DIAGNOSTIC */
183 /* TBD: check p last arg */
184 return(lockmgr(&p
->signal_lock
, LK_RELEASE
, (simple_lock_t
)0, (struct proc
*)p
));
188 signal_setast(sig_actthread
)
189 thread_act_t sig_actthread
;
191 act_set_astbsd(sig_actthread
);
195 * Can process p, with pcred pc, send the signal signum to process q?
198 cansignal(p
, pc
, q
, signum
)
204 /* you can signal yourself */
208 if (pc
->pc_ucred
->cr_uid
== 0)
209 return (1); /* root can always signal */
211 if (signum
== SIGCONT
&& q
->p_session
== p
->p_session
)
212 return (1); /* SIGCONT in session */
215 * Using kill(), only certain signals can be sent to setugid
218 if (q
->p_flag
& P_SUGID
) {
231 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
232 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
233 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
234 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
241 * because the P_SUGID test exists, this has extra tests which
244 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
245 pc
->p_ruid
== q
->p_cred
->p_svuid
||
246 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
247 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_svuid
||
248 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
249 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
254 struct sigaction_args
{
256 struct __sigaction
*nsa
;
257 struct sigaction
*osa
;
262 sigaction(p
, uap
, retval
)
264 register struct sigaction_args
*uap
;
267 struct sigaction vec
;
268 struct __sigaction __vec
;
270 register struct sigaction
*sa
;
271 register struct sigacts
*ps
= p
->p_sigacts
;
275 signum
= uap
->signum
;
276 if (signum
<= 0 || signum
>= NSIG
||
277 signum
== SIGKILL
|| signum
== SIGSTOP
)
281 sa
->sa_handler
= ps
->ps_sigact
[signum
];
282 sa
->sa_mask
= ps
->ps_catchmask
[signum
];
283 bit
= sigmask(signum
);
285 if ((ps
->ps_sigonstack
& bit
) != 0)
286 sa
->sa_flags
|= SA_ONSTACK
;
287 if ((ps
->ps_sigintr
& bit
) == 0)
288 sa
->sa_flags
|= SA_RESTART
;
289 if (ps
->ps_siginfo
& bit
)
290 sa
->sa_flags
|= SA_SIGINFO
;
291 if (ps
->ps_signodefer
& bit
)
292 sa
->sa_flags
|= SA_NODEFER
;
293 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDSTOP
))
294 sa
->sa_flags
|= SA_NOCLDSTOP
;
295 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDWAIT
))
296 sa
->sa_flags
|= SA_NOCLDWAIT
;
297 if (error
= copyout((caddr_t
)sa
, (caddr_t
)uap
->osa
,
302 if (error
= copyin((caddr_t
)uap
->nsa
, (caddr_t
)&__vec
,
305 error
= setsigvec(p
, signum
, &__vec
);
310 /* Routines to manipulate bits on all threads */
312 clear_procsiglist(struct proc
*p
, int bit
)
314 struct uthread
* uth
;
319 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
320 thact
= p
->p_vforkact
;
321 uth
= (struct uthread
*)get_bsdthread_info(thact
);
323 uth
->uu_siglist
&= ~bit
;
325 p
->p_siglist
&= ~bit
;
330 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
331 uth
->uu_siglist
&= ~bit
;
333 p
->p_siglist
&= ~bit
;
339 unblock_procsigmask(struct proc
*p
, int bit
)
341 struct uthread
* uth
;
345 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
346 thact
= p
->p_vforkact
;
347 uth
= (struct uthread
*)get_bsdthread_info(thact
);
349 uth
->uu_sigmask
&= ~bit
;
351 p
->p_sigmask
&= ~bit
;
355 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
356 uth
->uu_sigmask
&= ~bit
;
358 p
->p_sigmask
&= ~bit
;
365 block_procsigmask(struct proc
*p
, int bit
)
367 struct uthread
* uth
;
371 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
372 thact
= p
->p_vforkact
;
373 uth
= (struct uthread
*)get_bsdthread_info(thact
);
375 uth
->uu_sigmask
|= bit
;
381 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
382 uth
->uu_sigmask
|= bit
;
389 set_procsigmask(struct proc
*p
, int bit
)
391 struct uthread
* uth
;
395 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
396 thact
= p
->p_vforkact
;
397 uth
= (struct uthread
*)get_bsdthread_info(thact
);
399 uth
->uu_sigmask
= bit
;
405 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
406 uth
->uu_sigmask
= bit
;
414 setsigvec(p
, signum
, sa
)
415 register struct proc
*p
;
417 register struct __sigaction
*sa
;
419 register struct sigacts
*ps
= p
->p_sigacts
;
422 if ((signum
== SIGKILL
|| signum
== SIGSTOP
) &&
423 sa
->sa_handler
!= SIG_DFL
)
425 bit
= sigmask(signum
);
427 * Change setting atomically.
429 ps
->ps_sigact
[signum
] = sa
->sa_handler
;
430 ps
->ps_trampact
[signum
] = sa
->sa_tramp
;
431 ps
->ps_catchmask
[signum
] = sa
->sa_mask
&~ sigcantmask
;
432 if (sa
->sa_flags
& SA_SIGINFO
)
433 ps
->ps_siginfo
|= bit
;
435 ps
->ps_siginfo
&= ~bit
;
436 if ((sa
->sa_flags
& SA_RESTART
) == 0)
437 ps
->ps_sigintr
|= bit
;
439 ps
->ps_sigintr
&= ~bit
;
440 if (sa
->sa_flags
& SA_ONSTACK
)
441 ps
->ps_sigonstack
|= bit
;
443 ps
->ps_sigonstack
&= ~bit
;
444 if (sa
->sa_flags
& SA_USERTRAMP
)
445 ps
->ps_usertramp
|= bit
;
447 ps
->ps_usertramp
&= ~bit
;
448 if (sa
->sa_flags
& SA_RESETHAND
)
449 ps
->ps_sigreset
|= bit
;
451 ps
->ps_sigreset
&= ~bit
;
452 if (sa
->sa_flags
& SA_NODEFER
)
453 ps
->ps_signodefer
|= bit
;
455 ps
->ps_signodefer
&= ~bit
;
456 if (signum
== SIGCHLD
) {
457 if (sa
->sa_flags
& SA_NOCLDSTOP
)
458 p
->p_flag
|= P_NOCLDSTOP
;
460 p
->p_flag
&= ~P_NOCLDSTOP
;
461 if ((sa
->sa_flags
& SA_NOCLDWAIT
) || (sa
->sa_handler
== SIG_IGN
))
462 p
->p_flag
|= P_NOCLDWAIT
;
464 p
->p_flag
&= ~P_NOCLDWAIT
;
468 if (signum
== SIGFPE
) {
469 if (sa
->sa_handler
== SIG_DFL
|| sa
->sa_handler
== SIG_IGN
)
470 thread_enable_fpe(current_act(), 0);
472 thread_enable_fpe(current_act(), 1);
476 * Set bit in p_sigignore for signals that are set to SIG_IGN,
477 * and for signals set to SIG_DFL where the default is to ignore.
478 * However, don't put SIGCONT in p_sigignore,
479 * as we have to restart the process.
481 if (sa
->sa_handler
== SIG_IGN
||
482 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
484 clear_procsiglist(p
, bit
);
485 if (signum
!= SIGCONT
)
486 p
->p_sigignore
|= bit
; /* easier in psignal */
487 p
->p_sigcatch
&= ~bit
;
489 p
->p_sigignore
&= ~bit
;
490 if (sa
->sa_handler
== SIG_DFL
)
491 p
->p_sigcatch
&= ~bit
;
493 p
->p_sigcatch
|= bit
;
499 * Initialize signal state for process 0;
500 * set to ignore signals that are ignored by default.
508 for (i
= 0; i
< NSIG
; i
++)
509 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
510 p
->p_sigignore
|= sigmask(i
);
514 * Reset signals for an exec of the specified process.
518 register struct proc
*p
;
519 register thread_act_t thr_act
;
521 register struct sigacts
*ps
= p
->p_sigacts
;
522 register int nc
, mask
;
526 * Reset caught signals. Held signals remain held
527 * through p_sigmask (unless they were caught,
528 * and are now ignored by default).
530 while (p
->p_sigcatch
) {
531 nc
= ffs((long)p
->p_sigcatch
);
533 p
->p_sigcatch
&= ~mask
;
534 if (sigprop
[nc
] & SA_IGNORE
) {
536 p
->p_sigignore
|= mask
;
538 ut
= (struct uthread
*)get_bsdthread_info(thr_act
);
539 ut
->uu_siglist
&= ~mask
;
540 p
->p_siglist
&= ~mask
;
542 clear_procsiglist(p
, mask
);
544 ps
->ps_sigact
[nc
] = SIG_DFL
;
547 * Reset stack state to the user stack.
548 * Clear set of signals caught on the signal stack.
550 ps
->ps_sigstk
.ss_flags
= SA_DISABLE
;
551 ps
->ps_sigstk
.ss_size
= 0;
552 ps
->ps_sigstk
.ss_sp
= 0;
557 * Manipulate signal mask.
558 * Note that we receive new mask, not pointer,
559 * and return old mask as return value;
560 * the library stub does the rest.
562 struct sigprocmask_args
{
568 sigprocmask(p
, uap
, retval
)
569 register struct proc
*p
;
570 struct sigprocmask_args
*uap
;
574 sigset_t oldmask
, nmask
;
575 sigset_t
* omask
= uap
->omask
;
578 ut
= (struct uthread
*)get_bsdthread_info(current_act());
579 oldmask
= ut
->uu_sigmask
;
581 if (uap
->mask
== (sigset_t
*)0) {
582 /* just want old mask */
585 error
= copyin((caddr_t
)uap
->mask
, &nmask
, sizeof(sigset_t
));
591 block_procsigmask(p
, (nmask
& ~sigcantmask
));
592 signal_setast(current_act());
596 unblock_procsigmask(p
, (nmask
& ~sigcantmask
));
597 signal_setast(current_act());
601 set_procsigmask(p
, (nmask
& ~sigcantmask
));
602 signal_setast(current_act());
611 copyout(&oldmask
, omask
, sizeof(sigset_t
));
615 struct sigpending_args
{
619 sigpending(p
, uap
, retval
)
621 register struct sigpending_args
*uap
;
627 ut
= (struct uthread
*)get_bsdthread_info(current_act());
628 pendlist
= ut
->uu_siglist
;
631 copyout(&pendlist
, uap
->osv
, sizeof(sigset_t
));
637 * Generalized interface signal handler, 4.3-compatible.
639 struct osigvec_args
{
646 osigvec(p
, uap
, retval
)
648 register struct osigvec_args
*uap
;
653 register struct sigacts
*ps
= p
->p_sigacts
;
654 register struct sigvec
*sv
;
658 panic("osigvec: notsupp");
660 signum
= uap
->signum
;
661 if (signum
<= 0 || signum
>= NSIG
||
662 signum
== SIGKILL
|| signum
== SIGSTOP
)
666 *(sig_t
*)&sv
->sv_handler
= ps
->ps_sigact
[signum
];
667 sv
->sv_mask
= ps
->ps_catchmask
[signum
];
668 bit
= sigmask(signum
);
670 if ((ps
->ps_sigonstack
& bit
) != 0)
671 sv
->sv_flags
|= SV_ONSTACK
;
672 if ((ps
->ps_sigintr
& bit
) != 0)
673 sv
->sv_flags
|= SV_INTERRUPT
;
674 if (p
->p_flag
& P_NOCLDSTOP
)
675 sv
->sv_flags
|= SA_NOCLDSTOP
;
676 if (error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->osv
,
681 if (error
= copyin((caddr_t
)uap
->nsv
, (caddr_t
)sv
,
684 sv
->sv_flags
^= SA_RESTART
; /* opposite of SV_INTERRUPT */
685 error
= setsigvec(p
, signum
, (struct sigaction
*)sv
);
691 struct osigblock_args
{
695 osigblock(p
, uap
, retval
)
696 register struct proc
*p
;
697 struct osigblock_args
*uap
;
700 struct uthread
* uth
= get_bsdthread_info(current_act());
702 *retval
= uth
->uu_sigmask
;
703 uth
->uu_sigmask
|= (uap
->mask
& ~sigcantmask
);
707 struct osigsetmask_args
{
711 osigsetmask(p
, uap
, retval
)
713 struct osigsetmask_args
*uap
;
716 struct uthread
* uth
= get_bsdthread_info(current_act());
718 *retval
= uth
->uu_sigmask
;
719 uth
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
722 #endif /* COMPAT_43 */
725 * Suspend process until signal, providing mask to be set
726 * in the meantime. Note nonstandard calling convention:
727 * libc stub passes mask, not pointer, to save a copyin.
733 struct uthread
*ut
= get_bsdthread_info(current_act());
734 unix_syscall_return(EINTR
);
737 struct sigsuspend_args
{
743 sigsuspend(p
, uap
, retval
)
744 register struct proc
*p
;
745 struct sigsuspend_args
*uap
;
748 register struct sigacts
*ps
= p
->p_sigacts
;
751 ut
= (struct uthread
*)get_bsdthread_info(current_act());
754 * When returning from sigpause, we want
755 * the old mask to be restored after the
756 * signal handler has finished. Thus, we
757 * save it here and mark the sigacts structure
760 ut
->uu_oldmask
= ut
->uu_sigmask
;
761 ut
->uu_flag
|= USAS_OLDMASK
;
762 ut
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
763 (void) tsleep0((caddr_t
) p
, PPAUSE
|PCATCH
, "pause", 0, sigcontinue
);
764 /* always return EINTR rather than ERESTART... */
768 struct __disable_thsignal_args
{
773 __disable_threadsignal(p
, uap
, retval
)
775 register struct __disable_thsignal_args
*uap
;
780 uth
= (struct uthread
*)get_bsdthread_info(current_act());
782 /* No longer valid to have any signal delivered */
784 uth
->uu_flag
|= UNO_SIGMASK
;
791 struct pthread_kill_args
{
797 __pthread_kill(p
, uap
, retval
)
799 register struct pthread_kill_args
*uap
;
802 thread_act_t target_act
;
804 int signum
= uap
->sig
;
807 target_act
= (thread_act_t
)port_name_to_act(uap
->thread_port
);
809 if (target_act
== THR_ACT_NULL
)
811 if ((u_int
)signum
>= NSIG
) {
816 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
817 { void *tht
= getshuttle_thread(target_act
);
819 if (uth
->uu_flag
& UNO_SIGMASK
) {
825 psignal_uthread(target_act
, signum
);
827 act_deallocate(target_act
);
832 struct pthread_sigmask_args
{
838 pthread_sigmask(p
, uap
, retval
)
839 register struct proc
*p
;
840 register struct pthread_sigmask_args
*uap
;
844 const sigset_t
*set
= uap
->set
;
845 sigset_t
* oset
= uap
->oset
;
851 ut
= (struct uthread
*)get_bsdthread_info(current_act());
852 oldset
= ut
->uu_sigmask
;
854 if (set
== (sigset_t
*) 0) {
855 /* need only old mask */
859 error
= copyin((caddr_t
)set
, (caddr_t
)&nset
, sizeof(sigset_t
));
865 ut
->uu_sigmask
|= (nset
& ~sigcantmask
);
869 ut
->uu_sigmask
&= ~(nset
);
870 signal_setast(current_act());
874 ut
->uu_sigmask
= (nset
& ~sigcantmask
);
875 signal_setast(current_act());
884 copyout((caddr_t
)&oldset
, (caddr_t
)oset
, sizeof(sigset_t
));
890 struct sigwait_args
{
896 sigwait(p
, uap
, retval
)
897 register struct proc
*p
;
898 register struct sigwait_args
*uap
;
901 register struct sigacts
*ps
= p
->p_sigacts
;
911 ut
= (struct uthread
*)get_bsdthread_info(current_act());
913 if (uap
->set
== (const sigset_t
*)0)
916 error
= copyin((caddr_t
)uap
->set
, (caddr_t
)&mask
, sizeof(sigset_t
));
920 siglist
= (mask
& ~sigcantmask
);
926 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
930 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
931 if (sigw
= uth
->uu_siglist
& siglist
) {
938 /* The signal was pending on a thread */
942 * When returning from sigwait, we want
943 * the old mask to be restored after the
944 * signal handler has finished. Thus, we
945 * save it here and mark the sigacts structure
948 ut
->uu_oldmask
= ut
->uu_sigmask
;
949 ut
->uu_flag
|= USAS_OLDMASK
;
950 if (siglist
== (sigset_t
)0)
952 /* SIGKILL and SIGSTOP are not maskable as well */
953 ut
->uu_sigmask
= ~(siglist
|sigcantmask
);
954 ut
->uu_sigwait
= siglist
;
955 /* No Continuations for now */
956 error
= tsleep((caddr_t
)&ut
->uu_sigwait
, PPAUSE
|PCATCH
, "pause", 0);
958 if ((error
== EINTR
) || (error
== ERESTART
))
961 sigw
= (ut
->uu_sigwait
& siglist
);
962 ut
->uu_sigmask
= ut
->uu_oldmask
;
964 ut
->uu_flag
&= ~USAS_OLDMASK
;
968 signum
= ffs((unsigned int)sigw
);
970 panic("sigwait with no signal wakeup");
971 ut
->uu_siglist
&= ~(sigmask(signum
));
973 error
= copyout(&signum
, uap
->sig
, sizeof(int));
981 struct osigstack_args
{
982 struct sigstack
*nss
;
983 struct sigstack
*oss
;
988 osigstack(p
, uap
, retval
)
990 register struct osigstack_args
*uap
;
998 ss
.ss_sp
= psp
->ps_sigstk
.ss_sp
;
999 ss
.ss_onstack
= psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
;
1000 if (uap
->oss
&& (error
= copyout((caddr_t
)&ss
,
1001 (caddr_t
)uap
->oss
, sizeof (struct sigstack
))))
1003 if (uap
->nss
&& (error
= copyin((caddr_t
)uap
->nss
,
1004 (caddr_t
)&ss
, sizeof (ss
))) == 0) {
1005 psp
->ps_sigstk
.ss_sp
= ss
.ss_sp
;
1006 psp
->ps_sigstk
.ss_size
= 0;
1007 psp
->ps_sigstk
.ss_flags
|= ss
.ss_onstack
& SA_ONSTACK
;
1008 psp
->ps_flags
|= SAS_ALTSTACK
;
1012 #endif /* COMPAT_43 */
1014 struct sigaltstack_args
{
1015 struct sigaltstack
*nss
;
1016 struct sigaltstack
*oss
;
1020 sigaltstack(p
, uap
, retval
)
1022 register struct sigaltstack_args
*uap
;
1025 struct sigacts
*psp
;
1026 struct sigaltstack ss
;
1030 if ((psp
->ps_flags
& SAS_ALTSTACK
) == 0)
1031 psp
->ps_sigstk
.ss_flags
|= SA_DISABLE
;
1032 if (uap
->oss
&& (error
= copyout((caddr_t
)&psp
->ps_sigstk
,
1033 (caddr_t
)uap
->oss
, sizeof (struct sigaltstack
))))
1037 if (error
= copyin((caddr_t
)uap
->nss
, (caddr_t
)&ss
,
1040 if ((ss
.ss_flags
& ~SA_DISABLE
) != 0) {
1044 if (ss
.ss_flags
& SA_DISABLE
) {
1045 if (psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
)
1047 psp
->ps_flags
&= ~SAS_ALTSTACK
;
1048 psp
->ps_sigstk
.ss_flags
= ss
.ss_flags
;
1051 if (ss
.ss_size
< MINSIGSTKSZ
)
1053 psp
->ps_flags
|= SAS_ALTSTACK
;
1064 kill(cp
, uap
, retval
)
1065 register struct proc
*cp
;
1066 register struct kill_args
*uap
;
1069 register struct proc
*p
;
1070 register struct pcred
*pc
= cp
->p_cred
;
1072 if ((u_int
)uap
->signum
>= NSIG
)
1075 /* kill single process */
1076 if ((p
= pfind(uap
->pid
)) == NULL
)
1078 if (!cansignal(cp
, pc
, p
, uap
->signum
))
1081 psignal(p
, uap
->signum
);
1085 case -1: /* broadcast signal */
1086 return (killpg1(cp
, uap
->signum
, 0, 1));
1087 case 0: /* signal own process group */
1088 return (killpg1(cp
, uap
->signum
, 0, 0));
1089 default: /* negative explicit process group */
1090 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0));
1096 struct okillpg_args
{
1102 okillpg(p
, uap
, retval
)
1104 register struct okillpg_args
*uap
;
1108 if ((u_int
)uap
->signum
>= NSIG
)
1110 return (killpg1(p
, uap
->signum
, uap
->pgid
, 0));
1112 #endif /* COMPAT_43 */
1115 * Common code for kill process group/broadcast kill.
1116 * cp is calling process.
1119 killpg1(cp
, signum
, pgid
, all
)
1120 register struct proc
*cp
;
1121 int signum
, pgid
, all
;
1123 register struct proc
*p
;
1124 register struct pcred
*pc
= cp
->p_cred
;
1132 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1133 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1134 p
== cp
|| !cansignal(cp
, pc
, p
, signum
))
1143 * zero pgid means send to my process group.
1147 pgrp
= pgfind(pgid
);
1151 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1152 p
= p
->p_pglist
.le_next
) {
1153 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1154 p
->p_stat
== SZOMB
||
1155 !cansignal(cp
, pc
, p
, signum
))
1162 return (nfound
? 0 : ESRCH
);
1166 * Send a signal to a process group.
1169 gsignal(pgid
, signum
)
1174 if (pgid
&& (pgrp
= pgfind(pgid
)))
1175 pgsignal(pgrp
, signum
, 0);
1179 * Send a signal to a process group. If checktty is 1,
1180 * limit to members which have a controlling terminal.
1183 pgsignal(pgrp
, signum
, checkctty
)
1185 int signum
, checkctty
;
1187 register struct proc
*p
;
1190 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1191 p
= p
->p_pglist
.le_next
)
1192 if (checkctty
== 0 || p
->p_flag
& P_CONTROLT
)
1197 * Send signal to a backgrounded process blocked due to tty access
1198 * In FreeBSD, the backgrounded process wakes up every second and
1199 * discovers whether it is foregounded or not. In our case, we block
1200 * the thread in tsleep as we want to avoid storm of processes as well
1201 * as the suspend is only at AST level
1204 tty_pgsignal(pgrp
, signum
)
1208 register struct proc
*p
;
1211 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1212 p
= p
->p_pglist
.le_next
)
1213 if ((p
->p_flag
& P_TTYSLEEP
) && (p
->p_flag
& P_CONTROLT
))
1218 * Send a signal caused by a trap to a specific thread.
1221 threadsignal(sig_actthread
, signum
, code
)
1222 register thread_act_t sig_actthread
;
1223 register int signum
;
1226 register struct uthread
*uth
;
1227 register struct task
* sig_task
;
1228 register struct proc
*p
;
1231 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1234 mask
= sigmask(signum
);
1235 if ((mask
& threadmask
) == 0)
1237 sig_task
= get_threadtask(sig_actthread
);
1238 p
= (struct proc
*)(get_bsdtask_info(sig_task
));
1240 uth
= get_bsdthread_info(sig_actthread
);
1241 if (uth
&& (uth
->uu_flag
& P_VFORK
))
1244 if (!(p
->p_flag
& P_TRACED
) && (p
->p_sigignore
& mask
))
1247 uth
->uu_siglist
|= mask
;
1248 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1249 uth
->uu_code
= code
;
1250 /* mark on process as well */
1251 signal_setast(sig_actthread
);
1257 register struct proc
*p
;
1258 register int signum
;
1260 psignal_lock(p
, signum
, 1);
1264 psignal_vfork(p
, new_task
, thr_act
, signum
)
1265 register struct proc
*p
;
1267 thread_act_t thr_act
;
1268 register int signum
;
1272 register int s
, prop
;
1273 register sig_t action
;
1276 struct uthread
*uth
;
1278 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1279 panic("psignal signal number");
1280 mask
= sigmask(signum
);
1281 prop
= sigprop
[signum
];
1284 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1287 #endif /* SIGNAL_DEBUG */
1289 if ((new_task
== TASK_NULL
) || (thr_act
== (thread_act_t
)NULL
) || is_kerneltask(new_task
))
1293 uth
= get_bsdthread_info(thr_act
);
1297 * proc is traced, always give parent a chance.
1301 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1302 (p
->p_flag
& P_TRACED
) == 0)
1305 if (prop
& SA_CONT
) {
1306 p
->p_siglist
&= ~stopsigmask
;
1307 uth
->uu_siglist
&= ~stopsigmask
;
1310 if (prop
& SA_STOP
) {
1312 * If sending a tty stop signal to a member of an orphaned
1313 * process group, discard the signal here if the action
1314 * is default; don't stop the process below if sleeping,
1315 * and don't clear any pending SIGCONT.
1317 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1320 uth
->uu_siglist
&= ~contsigmask
;
1321 p
->p_siglist
&= ~contsigmask
;
1323 uth
->uu_siglist
|= mask
;
1324 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1326 /* Deliver signal to the activation passed in */
1327 act_set_astbsd(thr_act
);
1330 * SIGKILL priority twiddling moved here from above because
1331 * it needs sig_thread. Could merge it into large switch
1332 * below if we didn't care about priority for tracing
1333 * as SIGKILL's action is always SIG_DFL.
1335 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1340 * This Process is traced - wake it up (if not already
1341 * stopped) so that it can discover the signal in
1342 * issig() and stop for the parent.
1344 if (p
->p_flag
& P_TRACED
) {
1345 if (p
->p_stat
!= SSTOP
)
1352 * If we're being traced (possibly because someone attached us
1353 * while we were stopped), check for a signal from the debugger.
1355 if (p
->p_stat
== SSTOP
) {
1356 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
1357 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1358 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1363 * setrunnable(p) in BSD
1372 get_signalthread(struct proc
*p
, int signum
)
1374 struct uthread
*uth
;
1375 thread_act_t thr_act
;
1376 sigset_t mask
= sigmask(signum
);
1377 thread_act_t sig_thread_act
;
1378 struct task
* sig_task
= p
->task
;
1379 thread_t sig_thread
;
1382 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
1383 sig_thread_act
= p
->p_vforkact
;
1384 kret
= check_actforsig(sig_task
, sig_thread_act
, &sig_thread
, 1);
1385 if (kret
== KERN_SUCCESS
)
1386 return(sig_thread_act
);
1388 return(THR_ACT_NULL
);
1391 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1392 if(((uth
->uu_flag
& UNO_SIGMASK
)== 0) &&
1393 (((uth
->uu_sigmask
& mask
) == 0) || (uth
->uu_sigwait
& mask
))) {
1394 if (check_actforsig(p
->task
, uth
->uu_act
, NULL
, 1) == KERN_SUCCESS
)
1395 return(uth
->uu_act
);
1398 if (get_signalact(p
->task
, &thr_act
, NULL
, 1) == KERN_SUCCESS
) {
1402 return(THR_ACT_NULL
);
1406 * Send the signal to the process. If the signal has an action, the action
1407 * is usually performed by the target process rather than the caller; we add
1408 * the signal to the set of pending signals for the process.
1411 * o When a stop signal is sent to a sleeping process that takes the
1412 * default action, the process is stopped without awakening it.
1413 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1414 * regardless of the signal action (eg, blocked or ignored).
1416 * Other ignored signals are discarded immediately.
1419 psignal_lock(p
, signum
, withlock
)
1420 register struct proc
*p
;
1421 register int signum
;
1422 register int withlock
;
1424 register int s
, prop
;
1425 register sig_t action
;
1426 thread_act_t sig_thread_act
;
1427 thread_t sig_thread
;
1428 register task_t sig_task
;
1429 register thread_t cur_thread
;
1430 thread_act_t cur_act
;
1432 struct uthread
*uth
;
1436 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1437 panic("psignal signal number");
1438 mask
= sigmask(signum
);
1439 prop
= sigprop
[signum
];
1442 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1445 #endif /* SIGNAL_DEBUG */
1447 if (thread_funnel_get() == (funnel_t
*)network_flock
) {
1449 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1452 * We will need the task pointer later. Grab it now to
1453 * check for a zombie process. Also don't send signals
1454 * to kernel internal tasks.
1456 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1458 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1463 * do not send signals to the process that has the thread
1464 * doing a reboot(). Not doing so will mark that thread aborted
1465 * and can cause IO failures wich will cause data loss.
1467 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1469 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1477 * Deliver the signal to the first thread in the task. This
1478 * allows single threaded applications which use signals to
1479 * be able to be linked with multithreaded libraries. We have
1480 * an implicit reference to the current_thread, but need
1481 * an explicit one otherwise. The thread reference keeps
1482 * the corresponding task data structures around too. This
1483 * reference is released by thread_deallocate.
1486 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1489 cur_thread
= current_thread(); /* this is a shuttle */
1490 cur_act
= current_act();
1492 /* If successful return with ast set */
1493 sig_thread_act
= get_signalthread(p
, signum
);
1495 if (sig_thread_act
== THR_ACT_NULL
) {
1497 /* if it is sigkill, may be we should
1498 * inject a thread to terminate
1502 #endif /* SIGNAL_DEBUG */
1506 uth
= get_bsdthread_info(sig_thread_act
);
1509 * If proc is traced, always give parent a chance.
1511 if (p
->p_flag
& P_TRACED
)
1515 * If the signal is being ignored,
1516 * then we forget about it immediately.
1517 * (Note: we don't set SIGCONT in p_sigignore,
1518 * and if it is set to SIG_IGN,
1519 * action will be SIG_DFL here.)
1521 if (p
->p_sigignore
& mask
)
1523 if (uth
->uu_sigwait
& mask
)
1525 if (uth
->uu_sigmask
& mask
)
1527 else if (p
->p_sigcatch
& mask
)
1533 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1534 (p
->p_flag
& P_TRACED
) == 0)
1537 if (prop
& SA_CONT
) {
1538 uth
->uu_siglist
&= ~stopsigmask
;
1539 p
->p_siglist
&= ~stopsigmask
;
1542 if (prop
& SA_STOP
) {
1544 * If sending a tty stop signal to a member of an orphaned
1545 * process group, discard the signal here if the action
1546 * is default; don't stop the process below if sleeping,
1547 * and don't clear any pending SIGCONT.
1549 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1552 uth
->uu_siglist
&= ~contsigmask
;
1553 p
->p_siglist
&= ~contsigmask
;
1555 uth
->uu_siglist
|= mask
;
1556 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1560 * Defer further processing for signals which are held,
1561 * except that stopped processes must be continued by SIGCONT.
1563 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
)) {
1567 * SIGKILL priority twiddling moved here from above because
1568 * it needs sig_thread. Could merge it into large switch
1569 * below if we didn't care about priority for tracing
1570 * as SIGKILL's action is always SIG_DFL.
1572 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1577 * Process is traced - wake it up (if not already
1578 * stopped) so that it can discover the signal in
1579 * issig() and stop for the parent.
1581 if (p
->p_flag
& P_TRACED
) {
1582 if (p
->p_stat
!= SSTOP
)
1588 if (action
== SIG_WAIT
) {
1589 uth
->uu_sigwait
= mask
;
1590 uth
->uu_siglist
&= ~mask
;
1591 p
->p_siglist
&= ~mask
;
1592 wakeup(&uth
->uu_sigwait
);
1593 /* if it is SIGCONT resume whole process */
1595 (void) task_resume(sig_task
);
1599 if (action
!= SIG_DFL
) {
1601 * User wants to catch the signal.
1602 * Wake up the thread, but don't un-suspend it
1603 * (except for SIGCONT).
1606 (void) task_resume(sig_task
);
1609 /* Default action - varies */
1610 if (mask
& stopsigmask
) {
1612 * These are the signals which by default
1615 * Don't clog system with children of init
1616 * stopped from the keyboard.
1618 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1619 psignal_lock(p
, SIGKILL
, 0);
1620 uth
->uu_siglist
&= ~mask
;
1621 p
->p_siglist
&= ~mask
;
1627 * if task hasn't already been stopped by
1630 uth
->uu_siglist
&= ~mask
;
1631 p
->p_siglist
&= ~mask
;
1632 if (p
->p_stat
!= SSTOP
) {
1633 p
->p_xstat
= signum
;
1635 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1636 struct proc
*pp
= p
->p_pptr
;
1638 pp
->si_pid
= p
->p_pid
;
1639 pp
->si_status
= p
->p_xstat
;
1640 pp
->si_code
= CLD_STOPPED
;
1641 pp
->si_uid
= p
->p_cred
->p_ruid
;
1642 psignal(pp
, SIGCHLD
);
1650 * Signals ignored by default have been dealt
1651 * with already, since their bits are on in
1657 * Kill signal always sets process running and
1661 * Process will be running after 'run'
1665 thread_abort(sig_thread_act
);
1671 * Let the process run. If it's sleeping on an
1672 * event, it remains so.
1674 if (p
->p_flag
& P_TTYSLEEP
) {
1675 p
->p_flag
&= ~P_TTYSLEEP
;
1676 wakeup(&p
->p_siglist
);
1678 (void) task_resume(sig_task
);
1680 uth
->uu_siglist
&= ~mask
;
1681 p
->p_siglist
&= ~mask
;
1688 * All other signals wake up the process, but don't
1697 * If we're being traced (possibly because someone attached us
1698 * while we were stopped), check for a signal from the debugger.
1700 if (p
->p_stat
== SSTOP
) {
1701 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
1702 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1705 * setrunnable(p) in BSD and
1706 * Wake up the thread if it is interruptible.
1709 thread_abort_safely(sig_thread_act
);
1715 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1719 /* psignal_lock(p, signum, withlock ) */
1721 psignal_uthread(thr_act
, signum
)
1722 thread_act_t thr_act
;
1726 register int s
, prop
;
1727 register sig_t action
;
1728 thread_act_t sig_thread_act
;
1729 thread_t sig_thread
;
1730 register task_t sig_task
;
1731 register thread_t cur_thread
;
1732 thread_act_t cur_act
;
1734 struct uthread
*uth
;
1738 p
= (struct proc
*)get_bsdtask_info(get_threadtask(thr_act
));
1739 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1740 panic("Invalid signal number in psignal_uthread");
1741 mask
= sigmask(signum
);
1742 prop
= sigprop
[signum
];
1745 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1748 #endif /* SIGNAL_DEBUG */
1751 * We will need the task pointer later. Grab it now to
1752 * check for a zombie process. Also don't send signals
1753 * to kernel internal tasks.
1755 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1759 sig_thread_act
= thr_act
;
1761 * do not send signals to the process that has the thread
1762 * doing a reboot(). Not doing so will mark that thread aborted
1763 * and can cause IO failures wich will cause data loss.
1765 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1772 * Deliver the signal to the first thread in the task. This
1773 * allows single threaded applications which use signals to
1774 * be able to be linked with multithreaded libraries. We have
1775 * an implicit reference to the current_thread, but need
1776 * an explicit one otherwise. The thread reference keeps
1777 * the corresponding task data structures around too. This
1778 * reference is released by thread_deallocate.
1781 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1784 cur_thread
= current_thread(); /* this is a shuttle */
1785 cur_act
= current_act();
1787 kret
= check_actforsig(sig_task
, sig_thread_act
, &sig_thread
, 1);
1789 if (kret
!= KERN_SUCCESS
) {
1795 uth
= get_bsdthread_info(sig_thread_act
);
1798 * If proc is traced, always give parent a chance.
1800 if (p
->p_flag
& P_TRACED
)
1804 * If the signal is being ignored,
1805 * then we forget about it immediately.
1806 * (Note: we don't set SIGCONT in p_sigignore,
1807 * and if it is set to SIG_IGN,
1808 * action will be SIG_DFL here.)
1810 if (p
->p_sigignore
& mask
)
1812 if (uth
->uu_sigwait
& mask
)
1814 if (uth
->uu_sigmask
& mask
)
1816 else if (p
->p_sigcatch
& mask
)
1822 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1823 (p
->p_flag
& P_TRACED
) == 0)
1826 if (prop
& SA_CONT
) {
1827 uth
->uu_siglist
&= ~stopsigmask
;
1828 p
->p_siglist
&= ~stopsigmask
;
1831 if (prop
& SA_STOP
) {
1833 * If sending a tty stop signal to a member of an orphaned
1834 * process group, discard the signal here if the action
1835 * is default; don't stop the process below if sleeping,
1836 * and don't clear any pending SIGCONT.
1838 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1841 uth
->uu_siglist
&= ~contsigmask
;
1842 p
->p_siglist
&= ~contsigmask
;
1844 uth
->uu_siglist
|= mask
;
1845 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1848 * Defer further processing for signals which are held,
1849 * except that stopped processes must be continued by SIGCONT.
1851 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
))
1855 * SIGKILL priority twiddling moved here from above because
1856 * it needs sig_thread. Could merge it into large switch
1857 * below if we didn't care about priority for tracing
1858 * as SIGKILL's action is always SIG_DFL.
1860 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1865 * Process is traced - wake it up (if not already
1866 * stopped) so that it can discover the signal in
1867 * issig() and stop for the parent.
1869 if (p
->p_flag
& P_TRACED
) {
1870 if (p
->p_stat
!= SSTOP
)
1876 if (action
== SIG_WAIT
) {
1877 uth
->uu_sigwait
= mask
;
1878 uth
->uu_siglist
&= ~mask
;
1879 p
->p_siglist
&= ~mask
;
1880 wakeup(&uth
->uu_sigwait
);
1881 /* if it is SIGCONT resume whole process */
1883 (void) task_resume(sig_task
);
1887 if (action
!= SIG_DFL
) {
1889 * User wants to catch the signal.
1890 * Wake up the thread, but don't un-suspend it
1891 * (except for SIGCONT).
1894 (void) task_resume(sig_task
);
1897 /* Default action - varies */
1898 if (mask
& stopsigmask
) {
1900 * These are the signals which by default
1903 * Don't clog system with children of init
1904 * stopped from the keyboard.
1906 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1907 psignal_lock(p
, SIGKILL
, 0);
1908 uth
->uu_siglist
&= ~mask
;
1909 p
->p_siglist
&= ~mask
;
1915 * if task hasn't already been stopped by
1918 uth
->uu_siglist
&= ~mask
;
1919 p
->p_siglist
&= ~mask
;
1920 if (p
->p_stat
!= SSTOP
) {
1921 p
->p_xstat
= signum
;
1922 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1923 struct proc
*pp
= p
->p_pptr
;
1925 pp
->si_pid
= p
->p_pid
;
1926 pp
->si_status
= p
->p_xstat
;
1927 pp
->si_code
= CLD_STOPPED
;
1928 pp
->si_uid
= p
->p_cred
->p_ruid
;
1929 psignal(pp
, SIGCHLD
);
1938 * Signals ignored by default have been dealt
1939 * with already, since their bits are on in
1945 * Kill signal always sets process running and
1949 * Process will be running after 'run'
1953 thread_abort(sig_thread_act
);
1959 * Let the process run. If it's sleeping on an
1960 * event, it remains so.
1962 if (p
->p_flag
& P_TTYSLEEP
) {
1963 p
->p_flag
&= ~P_TTYSLEEP
;
1964 wakeup(&p
->p_siglist
);
1966 (void) task_resume(sig_task
);
1968 uth
->uu_siglist
&= ~mask
;
1969 p
->p_siglist
&= ~mask
;
1975 * All other signals wake up the process, but don't
1984 * If we're being traced (possibly because someone attached us
1985 * while we were stopped), check for a signal from the debugger.
1987 if (p
->p_stat
== SSTOP
) {
1988 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
1989 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1990 p
->p_siglist
|= sigmask(p
->p_xstat
);
1994 * setrunnable(p) in BSD and
1995 * Wake up the thread if it is interruptible.
1998 thread_abort_safely(sig_thread_act
);
2010 thread_t self
= current_thread();
2012 p
->exit_thread
= self
;
2013 (void) task_suspend(p
->task
);
2020 thread_t self
= current_thread();
2022 while (p
->sigwait
|| p
->exit_thread
) {
2023 if (p
->exit_thread
) {
2024 if (p
->exit_thread
!= self
) {
2026 * Already exiting - no signals.
2028 thread_abort(current_act());
2032 if(assert_wait_possible()) {
2033 assert_wait((caddr_t
)&p
->sigwait_thread
,
2034 (THREAD_INTERRUPTIBLE
));
2037 thread_block(THREAD_CONTINUE_NULL
);
2039 if (thread_should_abort(self
)) {
2041 * Terminate request - clean up.
2050 * If the current process has received a signal (should be caught or cause
2051 * termination, should interrupt current syscall), return the signal number.
2052 * Stop signals with default action are processed immediately, then cleared;
2053 * they aren't returned. This is checked after each entry to the system for
2054 * a syscall or trap (though this can usually be done without calling issignal
2055 * by checking the pending signal masks in the CURSIG macro.) The normal call
2058 * while (signum = CURSIG(curproc))
2063 register struct proc
*p
;
2065 register int signum
, mask
, prop
, sigbits
;
2066 task_t task
= p
->task
;
2067 thread_t cur_thread
;
2068 thread_act_t cur_act
;
2070 struct uthread
* ut
;
2074 cur_thread
= current_thread();
2075 cur_act
= current_act();
2078 if(rdebug_proc
&& (p
== rdebug_proc
)) {
2081 #endif /* SIGNAL_DEBUG */
2085 * Try to grab the signal lock.
2087 if (sig_try_locked(p
) <= 0) {
2092 ut
= get_bsdthread_info(cur_act
);
2094 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2096 if (p
->p_flag
& P_PPWAIT
)
2097 sigbits
&= ~stopsigmask
;
2098 if (sigbits
== 0) { /* no signal to send */
2102 signum
= ffs((long)sigbits
);
2103 mask
= sigmask(signum
);
2104 prop
= sigprop
[signum
];
2107 * We should see pending but ignored signals
2108 * only if P_TRACED was on when they were posted.
2110 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2111 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2112 p
->p_siglist
&= ~mask
; /* take the signal! */
2115 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2117 register task_t task
;
2119 * If traced, always stop, and stay
2120 * stopped until released by the debugger.
2122 /* ptrace debugging */
2123 p
->p_xstat
= signum
;
2125 if (p
->p_flag
& P_SIGEXC
) {
2127 p
->sigwait_thread
= cur_act
;
2129 p
->p_flag
&= ~P_WAITED
;
2130 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2131 p
->p_siglist
&= ~mask
; /* clear the old signal */
2133 do_bsdexception(EXC_SOFTWARE
, EXC_SOFT_SIGNAL
, signum
);
2136 pp
->si_pid
= p
->p_pid
;
2137 pp
->si_status
= p
->p_xstat
;
2138 pp
->si_code
= CLD_TRAPPED
;
2139 pp
->si_uid
= p
->p_cred
->p_ruid
;
2140 psignal(pp
, SIGCHLD
);
2142 * XXX Have to really stop for debuggers;
2143 * XXX stop() doesn't do the right thing.
2144 * XXX Inline the task_suspend because we
2145 * XXX have to diddle Unix state in the
2151 p
->sigwait_thread
= cur_act
;
2153 p
->p_flag
&= ~P_WAITED
;
2154 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2155 p
->p_siglist
&= ~mask
; /* clear the old signal */
2157 wakeup((caddr_t
)p
->p_pptr
);
2159 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
2160 thread_block(THREAD_CONTINUE_NULL
);
2165 p
->sigwait_thread
= NULL
;
2166 wakeup((caddr_t
)&p
->sigwait_thread
);
2169 * This code is to detect when gdb is killed
2170 * even as the traced program is attached.
2171 * pgsignal would get the SIGKILL to traced program
2172 * That's what we are trying to see (I hope)
2174 if (ut
->uu_siglist
& sigmask(SIGKILL
)) {
2176 * Wait event may still be outstanding;
2177 * clear it, since sig_lock_to_exit will
2180 clear_wait(current_thread(), THREAD_INTERRUPTED
);
2181 sig_lock_to_exit(p
);
2183 * Since this thread will be resumed
2184 * to allow the current syscall to
2185 * be completed, must save u_qsave
2186 * before calling exit(). (Since exit()
2187 * calls closef() which can trash u_qsave.)
2190 exit1(p
,signum
, (int *)NULL
);
2195 * We may have to quit
2197 if (thread_should_abort(current_thread())) {
2202 * If parent wants us to take the signal,
2203 * then it will leave it in p->p_xstat;
2204 * otherwise we just look for signals again.
2206 signum
= p
->p_xstat
;
2210 * Put the new signal into p_siglist. If the
2211 * signal is being masked, look for other signals.
2213 mask
= sigmask(signum
);
2214 ut
->uu_siglist
|= mask
;
2215 p
->p_siglist
|= mask
; /* just for lame ones looking here */
2216 if (ut
->uu_sigmask
& mask
)
2221 * Decide whether the signal should be returned.
2222 * Return the signal's number, or fall through
2223 * to clear it from the pending mask.
2226 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2230 * Don't take default actions on system processes.
2232 if (p
->p_pptr
->p_pid
== 0) {
2235 * Are you sure you want to ignore SIGSEGV
2238 printf("Process (pid %d) got signal %d\n",
2241 break; /* == ignore */
2245 * If there is a pending stop signal to process
2246 * with default action, stop here,
2247 * then clear the signal. However,
2248 * if process is member of an orphaned
2249 * process group, ignore tty stop signals.
2251 if (prop
& SA_STOP
) {
2252 if (p
->p_flag
& P_TRACED
||
2253 (p
->p_pgrp
->pg_jobc
== 0 &&
2255 break; /* == ignore */
2256 if (p
->p_stat
!= SSTOP
) {
2257 p
->p_xstat
= signum
;
2259 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
2261 pp
->si_pid
= p
->p_pid
;
2262 pp
->si_status
= p
->p_xstat
;
2263 pp
->si_code
= CLD_STOPPED
;
2264 pp
->si_uid
= p
->p_cred
->p_ruid
;
2265 psignal(pp
, SIGCHLD
);
2269 } else if (prop
& SA_IGNORE
) {
2271 * Except for SIGCONT, shouldn't get here.
2272 * Default action is to ignore; drop it.
2274 break; /* == ignore */
2276 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2277 p
->p_siglist
&= ~mask
; /* take the signal! */
2285 * Masking above should prevent us ever trying
2286 * to take action on an ignored signal other
2287 * than SIGCONT, unless process is traced.
2289 if ((prop
& SA_CONT
) == 0 &&
2290 (p
->p_flag
& P_TRACED
) == 0)
2291 printf("issignal\n");
2292 break; /* == ignore */
2296 * This signal has an action, let
2297 * postsig() process it.
2299 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2300 p
->p_siglist
&= ~mask
; /* take the signal! */
2304 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2305 p
->p_siglist
&= ~mask
; /* take the signal! */
2310 /* called from _sleep */
2313 register struct proc
*p
;
2315 register int signum
, mask
, prop
, sigbits
;
2316 task_t task
= p
->task
;
2317 thread_t cur_thread
;
2318 thread_act_t cur_act
;
2320 struct uthread
* ut
;
2324 cur_thread
= current_thread();
2325 cur_act
= current_act();
2327 ut
= get_bsdthread_info(cur_act
);
2329 if (ut
->uu_siglist
== 0)
2332 if (((ut
->uu_siglist
& ~ut
->uu_sigmask
) == 0) && ((p
->p_flag
& P_TRACED
) == 0))
2335 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2338 if (p
->p_flag
& P_PPWAIT
)
2339 sigbits
&= ~stopsigmask
;
2340 if (sigbits
== 0) { /* no signal to send */
2344 signum
= ffs((long)sigbits
);
2345 mask
= sigmask(signum
);
2346 prop
= sigprop
[signum
];
2349 * We should see pending but ignored signals
2350 * only if P_TRACED was on when they were posted.
2352 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2355 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2357 * Put the new signal into p_siglist. If the
2358 * signal is being masked, look for other signals.
2360 mask
= sigmask(signum
);
2361 if (ut
->uu_sigmask
& mask
)
2367 * Decide whether the signal should be returned.
2368 * Return the signal's number, or fall through
2369 * to clear it from the pending mask.
2372 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2376 * Don't take default actions on system processes.
2378 if (p
->p_pptr
->p_pid
== 0) {
2381 * Are you sure you want to ignore SIGSEGV
2384 printf("Process (pid %d) got signal %d\n",
2387 break; /* == ignore */
2391 * If there is a pending stop signal to process
2392 * with default action, stop here,
2393 * then clear the signal. However,
2394 * if process is member of an orphaned
2395 * process group, ignore tty stop signals.
2397 if (prop
& SA_STOP
) {
2398 if (p
->p_flag
& P_TRACED
||
2399 (p
->p_pgrp
->pg_jobc
== 0 &&
2401 break; /* == ignore */
2404 } else if (prop
& SA_IGNORE
) {
2406 * Except for SIGCONT, shouldn't get here.
2407 * Default action is to ignore; drop it.
2409 break; /* == ignore */
2417 * Masking above should prevent us ever trying
2418 * to take action on an ignored signal other
2419 * than SIGCONT, unless process is traced.
2421 if ((prop
& SA_CONT
) == 0 &&
2422 (p
->p_flag
& P_TRACED
) == 0)
2423 printf("issignal\n");
2424 break; /* == ignore */
2428 * This signal has an action, let
2429 * postsig() process it.
2433 sigbits
&= ~mask
; /* take the signal! */
2439 * Put the argument process into the stopped state and notify the parent
2440 * via wakeup. Signals are handled elsewhere. The process must not be
2445 register struct proc
*p
;
2448 p
->p_flag
&= ~P_WAITED
;
2449 wakeup((caddr_t
)p
->p_pptr
);
2450 (void) task_suspend(p
->task
); /*XXX*/
2454 * Take the action for the specified signal
2455 * from the current set of pending signals.
2459 register int signum
;
2461 register struct proc
*p
= current_proc();
2462 register struct sigacts
*ps
= p
->p_sigacts
;
2463 register sig_t action
;
2465 int mask
, returnmask
;
2466 struct uthread
* ut
;
2472 * This must be called on master cpu
2474 if (cpu_number() != master_cpu
)
2475 panic("psig not on master");
2480 * Try to grab the signal lock.
2482 if (sig_try_locked(p
) <= 0) {
2487 ut
= (struct uthread
*)get_bsdthread_info(current_act());
2488 mask
= sigmask(signum
);
2489 ut
->uu_siglist
&= ~mask
;
2490 p
->p_siglist
&= ~mask
;
2491 action
= ps
->ps_sigact
[signum
];
2493 if (KTRPOINT(p
, KTR_PSIG
))
2494 ktrpsig(p
->p_tracep
,
2495 signum
, action
, ut
->uu_flag
& USAS_OLDMASK
?
2496 &ut
->uu_oldmask
: &ut
->uu_sigmask
, 0, -1);
2498 if (action
== SIG_DFL
) {
2500 * Default action, where the default is to kill
2501 * the process. (Other cases were ignored above.)
2503 /* called with signal_lock() held */
2504 sigexit_locked(p
, signum
);
2509 * If we get here, the signal must be caught.
2512 if (action
== SIG_IGN
|| (ut
->uu_sigmask
& mask
))
2514 "postsig: processing masked or ignored signal\n");
2517 * Set the new mask value and also defer further
2518 * occurences of this signal.
2520 * Special case: user has done a sigpause. Here the
2521 * current mask is not of interest, but rather the
2522 * mask from before the sigpause is what we want
2523 * restored after the signal processing is completed.
2525 if (ut
->uu_flag
& USAS_OLDMASK
) {
2526 returnmask
= ut
->uu_oldmask
;
2527 ut
->uu_flag
&= ~USAS_OLDMASK
;
2530 returnmask
= ut
->uu_sigmask
;
2531 ut
->uu_sigmask
|= ps
->ps_catchmask
[signum
];
2532 if ((ps
->ps_signodefer
& mask
) == 0)
2533 ut
->uu_sigmask
|= mask
;
2534 if ((signum
!= SIGILL
) && (signum
!= SIGTRAP
) && (ps
->ps_sigreset
& mask
)) {
2535 if ((signum
!= SIGCONT
) && (sigprop
[signum
] & SA_IGNORE
))
2536 p
->p_sigignore
|= mask
;
2537 ps
->ps_sigact
[signum
] = SIG_DFL
;
2538 ps
->ps_siginfo
&= ~mask
;
2539 ps
->ps_signodefer
&= ~mask
;
2542 /* Needs to disable to run in user mode */
2543 if (signum
== SIGFPE
) {
2544 thread_enable_fpe(current_act(), 0);
2546 #endif /* __ppc__ */
2548 if (ps
->ps_sig
!= signum
) {
2554 p
->p_stats
->p_ru
.ru_nsignals
++;
2555 sendsig(p
, action
, signum
, returnmask
, code
);
2561 * Force the current process to exit with the specified signal, dumping core
2562 * if appropriate. We bypass the normal tests for masked and caught signals,
2563 * allowing unrecoverable failures to terminate the process without changing
2564 * signal state. Mark the accounting record with the signal termination.
2565 * If dumping core, save the signal number for the debugger. Calls exit and
2568 /* called with signal lock */
2570 sigexit_locked(p
, signum
)
2571 register struct proc
*p
;
2575 sig_lock_to_exit(p
);
2576 p
->p_acflag
|= AXSIG
;
2577 if (sigprop
[signum
] & SA_CORE
) {
2578 p
->p_sigacts
->ps_sig
= signum
;
2579 if (coredump(p
) == 0)
2580 signum
|= WCOREFLAG
;
2583 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
2588 bsd_ast(thread_act_t thr_act
)
2590 struct proc
*p
= current_proc();
2591 struct uthread
*ut
= get_bsdthread_info(thr_act
);
2594 boolean_t funnel_state
;
2595 static bsd_init_done
= 0;
2600 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2602 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
2603 pc
= get_useraddr();
2604 addupc_task(p
, pc
, 1);
2605 p
->p_flag
&= ~P_OWEUPC
;
2608 if (CHECK_SIGNALS(p
, current_thread(), ut
)) {
2609 while (signum
= issignal(p
))
2612 if (!bsd_init_done
) {
2613 extern void bsdinit_task(void);
2619 (void) thread_funnel_set(kernel_flock
, FALSE
);
2623 * Follwing routines are called using callout from bsd_hardclock
2624 * so that psignals are called in a thread context and are funneled
2627 psignal_vtalarm(struct proc
*p
)
2629 boolean_t funnel_state
;
2633 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2634 psignal_lock(p
, SIGVTALRM
, 1);
2635 (void) thread_funnel_set(kernel_flock
, FALSE
);
2639 psignal_xcpu(struct proc
*p
)
2641 boolean_t funnel_state
;
2645 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2646 psignal_lock(p
, SIGXCPU
, 1);
2647 (void) thread_funnel_set(kernel_flock
, FALSE
);
2651 psignal_sigprof(struct proc
*p
)
2653 boolean_t funnel_state
;
2657 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2658 psignal_lock(p
, SIGPROF
, 1);
2659 (void) thread_funnel_set(kernel_flock
, FALSE
);
2662 /* ptrace set runnalbe */
2664 pt_setrunnable(struct proc
*p
)
2670 if (p
->p_flag
& P_TRACED
) {
2673 wakeup((caddr_t
)&(p
->sigwait
));
2685 exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
2686 extern kern_return_t
bsd_exception(int, exception_data_type_t codes
[], int);
2690 return(bsd_exception(exc
, codes
, 2));