2 * Copyright (c) 2000-2004 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>
85 #include <sys/mount.h>
87 #include <bsm/audit_kernel.h>
89 #include <kern/cpu_number.h>
92 #include <sys/user.h> /* for coredump */
93 #include <kern/ast.h> /* for APC support */
94 #include <kern/thread.h>
95 #include <kern/sched_prim.h>
96 #include <kern/thread_call.h>
97 #include <mach/exception.h>
99 extern void doexception(int exc
, int code
, int sub
);
101 void stop
__P((struct proc
*p
));
102 int cansignal
__P((struct proc
*, struct pcred
*, struct proc
*, int));
103 int killpg1
__P((struct proc
*, int, int, int));
104 void sigexit_locked
__P((struct proc
*, int));
105 int setsigvec
__P((struct proc
*, int, struct __sigaction
*));
106 void exit1
__P((struct proc
*, int, int *));
107 int signal_lock
__P((struct proc
*));
108 int signal_unlock
__P((struct proc
*));
109 void signal_setast
__P((thread_act_t
));
110 void psignal_lock
__P((struct proc
*, int, int));
111 void psignal_uthread
__P((thread_act_t
, int));
112 kern_return_t
do_bsdexception(int, int, int);
114 static int filt_sigattach(struct knote
*kn
);
115 static void filt_sigdetach(struct knote
*kn
);
116 static int filt_signal(struct knote
*kn
, long hint
);
118 struct filterops sig_filtops
=
119 { 0, filt_sigattach
, filt_sigdetach
, filt_signal
};
122 void ram_printf
__P((int));
124 unsigned int rdebug_proc
=0;
131 #endif /* SIGNAL_DEBUG */
134 signal_lock(struct proc
*p
)
141 int register sp
, *fp
, numsaved
;
143 __asm__
volatile("mr %0,r1" : "=r" (sp
));
145 fp
= (int *)*((int *)sp
);
146 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
147 p
->lockpc
[numsaved
] = fp
[2];
154 #endif /* SIGNAL_DEBUG */
155 #endif /* DIAGNOSTIC */
158 /* TBD: check p last arg */
159 error
= lockmgr(&p
->signal_lock
, LK_EXCLUSIVE
, 0, (struct proc
*)p
);
166 signal_unlock(struct proc
*p
)
172 int register sp
, *fp
, numsaved
;
174 __asm__
volatile("mr %0,r1" : "=r" (sp
));
176 fp
= (int *)*((int *)sp
);
177 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
178 p
->unlockpc
[numsaved
] = fp
[2];
185 #endif /* SIGNAL_DEBUG */
186 #endif /* DIAGNOSTIC */
188 /* TBD: check p last arg */
189 return(lockmgr(&p
->signal_lock
, LK_RELEASE
, (simple_lock_t
)0, (struct proc
*)p
));
193 signal_setast(sig_actthread
)
194 thread_act_t sig_actthread
;
196 act_set_astbsd(sig_actthread
);
200 * Can process p, with pcred pc, send the signal signum to process q?
203 cansignal(p
, pc
, q
, signum
)
209 /* you can signal yourself */
213 if (pc
->pc_ucred
->cr_uid
== 0)
214 return (1); /* root can always signal */
216 if (signum
== SIGCONT
&& q
->p_session
== p
->p_session
)
217 return (1); /* SIGCONT in session */
220 * Using kill(), only certain signals can be sent to setugid
223 if (q
->p_flag
& P_SUGID
) {
236 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
237 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
238 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
239 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
246 * because the P_SUGID test exists, this has extra tests which
249 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
250 pc
->p_ruid
== q
->p_cred
->p_svuid
||
251 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
252 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_svuid
||
253 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
254 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
259 struct sigaction_args
{
261 struct __sigaction
*nsa
;
262 struct sigaction
*osa
;
267 sigaction(p
, uap
, retval
)
269 register struct sigaction_args
*uap
;
272 struct sigaction vec
;
273 struct __sigaction __vec
;
275 register struct sigaction
*sa
;
276 register struct sigacts
*ps
= p
->p_sigacts
;
280 signum
= uap
->signum
;
281 if (signum
<= 0 || signum
>= NSIG
||
282 signum
== SIGKILL
|| signum
== SIGSTOP
)
286 sa
->sa_handler
= ps
->ps_sigact
[signum
];
287 sa
->sa_mask
= ps
->ps_catchmask
[signum
];
288 bit
= sigmask(signum
);
290 if ((ps
->ps_sigonstack
& bit
) != 0)
291 sa
->sa_flags
|= SA_ONSTACK
;
292 if ((ps
->ps_sigintr
& bit
) == 0)
293 sa
->sa_flags
|= SA_RESTART
;
294 if (ps
->ps_siginfo
& bit
)
295 sa
->sa_flags
|= SA_SIGINFO
;
296 if (ps
->ps_signodefer
& bit
)
297 sa
->sa_flags
|= SA_NODEFER
;
298 if (ps
->ps_64regset
& bit
)
299 sa
->sa_flags
|= SA_64REGSET
;
300 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDSTOP
))
301 sa
->sa_flags
|= SA_NOCLDSTOP
;
302 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDWAIT
))
303 sa
->sa_flags
|= SA_NOCLDWAIT
;
304 if (error
= copyout((caddr_t
)sa
, (caddr_t
)uap
->osa
,
309 if (error
= copyin((caddr_t
)uap
->nsa
, (caddr_t
)&__vec
,
312 error
= setsigvec(p
, signum
, &__vec
);
317 /* Routines to manipulate bits on all threads */
319 clear_procsiglist(struct proc
*p
, int bit
)
321 struct uthread
* uth
;
326 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
327 thact
= p
->p_vforkact
;
328 uth
= (struct uthread
*)get_bsdthread_info(thact
);
330 uth
->uu_siglist
&= ~bit
;
332 p
->p_siglist
&= ~bit
;
337 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
338 uth
->uu_siglist
&= ~bit
;
340 p
->p_siglist
&= ~bit
;
346 unblock_procsigmask(struct proc
*p
, int bit
)
348 struct uthread
* uth
;
352 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
353 thact
= p
->p_vforkact
;
354 uth
= (struct uthread
*)get_bsdthread_info(thact
);
356 uth
->uu_sigmask
&= ~bit
;
358 p
->p_sigmask
&= ~bit
;
362 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
363 uth
->uu_sigmask
&= ~bit
;
365 p
->p_sigmask
&= ~bit
;
372 block_procsigmask(struct proc
*p
, int bit
)
374 struct uthread
* uth
;
378 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
379 thact
= p
->p_vforkact
;
380 uth
= (struct uthread
*)get_bsdthread_info(thact
);
382 uth
->uu_sigmask
|= bit
;
388 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
389 uth
->uu_sigmask
|= bit
;
396 set_procsigmask(struct proc
*p
, int bit
)
398 struct uthread
* uth
;
402 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
403 thact
= p
->p_vforkact
;
404 uth
= (struct uthread
*)get_bsdthread_info(thact
);
406 uth
->uu_sigmask
= bit
;
412 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
413 uth
->uu_sigmask
= bit
;
421 setsigvec(p
, signum
, sa
)
422 register struct proc
*p
;
424 register struct __sigaction
*sa
;
426 register struct sigacts
*ps
= p
->p_sigacts
;
429 if ((signum
== SIGKILL
|| signum
== SIGSTOP
) &&
430 sa
->sa_handler
!= SIG_DFL
)
432 bit
= sigmask(signum
);
434 * Change setting atomically.
436 ps
->ps_sigact
[signum
] = sa
->sa_handler
;
437 ps
->ps_trampact
[signum
] = (sig_t
) sa
->sa_tramp
;
438 ps
->ps_catchmask
[signum
] = sa
->sa_mask
&~ sigcantmask
;
439 if (sa
->sa_flags
& SA_SIGINFO
)
440 ps
->ps_siginfo
|= bit
;
442 ps
->ps_siginfo
&= ~bit
;
443 if (sa
->sa_flags
& SA_64REGSET
)
444 ps
->ps_64regset
|= bit
;
446 ps
->ps_64regset
&= ~bit
;
447 if ((sa
->sa_flags
& SA_RESTART
) == 0)
448 ps
->ps_sigintr
|= bit
;
450 ps
->ps_sigintr
&= ~bit
;
451 if (sa
->sa_flags
& SA_ONSTACK
)
452 ps
->ps_sigonstack
|= bit
;
454 ps
->ps_sigonstack
&= ~bit
;
455 if (sa
->sa_flags
& SA_USERTRAMP
)
456 ps
->ps_usertramp
|= bit
;
458 ps
->ps_usertramp
&= ~bit
;
459 if (sa
->sa_flags
& SA_RESETHAND
)
460 ps
->ps_sigreset
|= bit
;
462 ps
->ps_sigreset
&= ~bit
;
463 if (sa
->sa_flags
& SA_NODEFER
)
464 ps
->ps_signodefer
|= bit
;
466 ps
->ps_signodefer
&= ~bit
;
467 if (signum
== SIGCHLD
) {
468 if (sa
->sa_flags
& SA_NOCLDSTOP
)
469 p
->p_flag
|= P_NOCLDSTOP
;
471 p
->p_flag
&= ~P_NOCLDSTOP
;
472 if ((sa
->sa_flags
& SA_NOCLDWAIT
) || (sa
->sa_handler
== SIG_IGN
))
473 p
->p_flag
|= P_NOCLDWAIT
;
475 p
->p_flag
&= ~P_NOCLDWAIT
;
479 if (signum
== SIGFPE
) {
480 if (sa
->sa_handler
== SIG_DFL
|| sa
->sa_handler
== SIG_IGN
)
481 thread_enable_fpe(current_act(), 0);
483 thread_enable_fpe(current_act(), 1);
487 * Set bit in p_sigignore for signals that are set to SIG_IGN,
488 * and for signals set to SIG_DFL where the default is to ignore.
489 * However, don't put SIGCONT in p_sigignore,
490 * as we have to restart the process.
492 if (sa
->sa_handler
== SIG_IGN
||
493 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
495 clear_procsiglist(p
, bit
);
496 if (signum
!= SIGCONT
)
497 p
->p_sigignore
|= bit
; /* easier in psignal */
498 p
->p_sigcatch
&= ~bit
;
500 p
->p_sigignore
&= ~bit
;
501 if (sa
->sa_handler
== SIG_DFL
)
502 p
->p_sigcatch
&= ~bit
;
504 p
->p_sigcatch
|= bit
;
510 * Initialize signal state for process 0;
511 * set to ignore signals that are ignored by default.
519 for (i
= 0; i
< NSIG
; i
++)
520 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
521 p
->p_sigignore
|= sigmask(i
);
525 * Reset signals for an exec of the specified process.
529 register struct proc
*p
;
530 register thread_act_t thr_act
;
532 register struct sigacts
*ps
= p
->p_sigacts
;
533 register int nc
, mask
;
537 * Reset caught signals. Held signals remain held
538 * through p_sigmask (unless they were caught,
539 * and are now ignored by default).
541 while (p
->p_sigcatch
) {
542 nc
= ffs((long)p
->p_sigcatch
);
544 p
->p_sigcatch
&= ~mask
;
545 if (sigprop
[nc
] & SA_IGNORE
) {
547 p
->p_sigignore
|= mask
;
549 ut
= (struct uthread
*)get_bsdthread_info(thr_act
);
550 ut
->uu_siglist
&= ~mask
;
551 p
->p_siglist
&= ~mask
;
553 clear_procsiglist(p
, mask
);
555 ps
->ps_sigact
[nc
] = SIG_DFL
;
558 * Reset stack state to the user stack.
559 * Clear set of signals caught on the signal stack.
561 ps
->ps_sigstk
.ss_flags
= SA_DISABLE
;
562 ps
->ps_sigstk
.ss_size
= 0;
563 ps
->ps_sigstk
.ss_sp
= 0;
568 * Manipulate signal mask.
569 * Note that we receive new mask, not pointer,
570 * and return old mask as return value;
571 * the library stub does the rest.
573 struct sigprocmask_args
{
579 sigprocmask(p
, uap
, retval
)
580 register struct proc
*p
;
581 struct sigprocmask_args
*uap
;
585 sigset_t oldmask
, nmask
;
586 sigset_t
* omask
= uap
->omask
;
589 ut
= (struct uthread
*)get_bsdthread_info(current_act());
590 oldmask
= ut
->uu_sigmask
;
592 if (uap
->mask
== (sigset_t
*)0) {
593 /* just want old mask */
596 error
= copyin((caddr_t
)uap
->mask
, &nmask
, sizeof(sigset_t
));
602 block_procsigmask(p
, (nmask
& ~sigcantmask
));
603 signal_setast(current_act());
607 unblock_procsigmask(p
, (nmask
& ~sigcantmask
));
608 signal_setast(current_act());
612 set_procsigmask(p
, (nmask
& ~sigcantmask
));
613 signal_setast(current_act());
622 copyout(&oldmask
, omask
, sizeof(sigset_t
));
626 struct sigpending_args
{
630 sigpending(p
, uap
, retval
)
632 register struct sigpending_args
*uap
;
638 ut
= (struct uthread
*)get_bsdthread_info(current_act());
639 pendlist
= ut
->uu_siglist
;
642 copyout(&pendlist
, uap
->osv
, sizeof(sigset_t
));
648 * Generalized interface signal handler, 4.3-compatible.
650 struct osigvec_args
{
657 osigvec(p
, uap
, retval
)
659 register struct osigvec_args
*uap
;
664 register struct sigacts
*ps
= p
->p_sigacts
;
665 register struct sigvec
*sv
;
670 signum
= uap
->signum
;
671 if (signum
<= 0 || signum
>= NSIG
||
672 signum
== SIGKILL
|| signum
== SIGSTOP
)
676 *(sig_t
*)&sv
->sv_handler
= ps
->ps_sigact
[signum
];
677 sv
->sv_mask
= ps
->ps_catchmask
[signum
];
678 bit
= sigmask(signum
);
680 if ((ps
->ps_sigonstack
& bit
) != 0)
681 sv
->sv_flags
|= SV_ONSTACK
;
682 if ((ps
->ps_sigintr
& bit
) != 0)
683 sv
->sv_flags
|= SV_INTERRUPT
;
684 if (p
->p_flag
& P_NOCLDSTOP
)
685 sv
->sv_flags
|= SA_NOCLDSTOP
;
686 if (error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->osv
,
691 if (error
= copyin((caddr_t
)uap
->nsv
, (caddr_t
)sv
,
694 sv
->sv_flags
^= SA_RESTART
; /* opposite of SV_INTERRUPT */
695 error
= setsigvec(p
, signum
, (struct sigaction
*)sv
);
703 struct osigblock_args
{
707 osigblock(p
, uap
, retval
)
708 register struct proc
*p
;
709 struct osigblock_args
*uap
;
712 struct uthread
* uth
= get_bsdthread_info(current_act());
714 *retval
= uth
->uu_sigmask
;
715 uth
->uu_sigmask
|= (uap
->mask
& ~sigcantmask
);
719 struct osigsetmask_args
{
723 osigsetmask(p
, uap
, retval
)
725 struct osigsetmask_args
*uap
;
728 struct uthread
* uth
= get_bsdthread_info(current_act());
730 *retval
= uth
->uu_sigmask
;
731 uth
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
734 #endif /* COMPAT_43 */
737 * Suspend process until signal, providing mask to be set
738 * in the meantime. Note nonstandard calling convention:
739 * libc stub passes mask, not pointer, to save a copyin.
745 struct uthread
*ut
= get_bsdthread_info(current_act());
746 unix_syscall_return(EINTR
);
749 struct sigsuspend_args
{
755 sigsuspend(p
, uap
, retval
)
756 register struct proc
*p
;
757 struct sigsuspend_args
*uap
;
760 register struct sigacts
*ps
= p
->p_sigacts
;
763 ut
= (struct uthread
*)get_bsdthread_info(current_act());
766 * When returning from sigpause, we want
767 * the old mask to be restored after the
768 * signal handler has finished. Thus, we
769 * save it here and mark the sigacts structure
772 ut
->uu_oldmask
= ut
->uu_sigmask
;
773 ut
->uu_flag
|= USAS_OLDMASK
;
774 ut
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
775 (void) tsleep0((caddr_t
) p
, PPAUSE
|PCATCH
, "pause", 0, sigcontinue
);
776 /* always return EINTR rather than ERESTART... */
780 struct __disable_thsignal_args
{
785 __disable_threadsignal(p
, uap
, retval
)
787 register struct __disable_thsignal_args
*uap
;
792 uth
= (struct uthread
*)get_bsdthread_info(current_act());
794 /* No longer valid to have any signal delivered */
796 uth
->uu_flag
|= UNO_SIGMASK
;
803 struct pthread_kill_args
{
809 __pthread_kill(p
, uap
, retval
)
811 register struct pthread_kill_args
*uap
;
814 thread_act_t target_act
;
816 int signum
= uap
->sig
;
819 target_act
= (thread_act_t
)port_name_to_act(uap
->thread_port
);
821 if (target_act
== THR_ACT_NULL
)
823 if ((u_int
)signum
>= NSIG
) {
828 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
830 if (uth
->uu_flag
& UNO_SIGMASK
) {
836 psignal_uthread(target_act
, signum
);
838 act_deallocate(target_act
);
843 struct pthread_sigmask_args
{
849 pthread_sigmask(p
, uap
, retval
)
850 register struct proc
*p
;
851 register struct pthread_sigmask_args
*uap
;
855 const sigset_t
*set
= uap
->set
;
856 sigset_t
* oset
= uap
->oset
;
862 ut
= (struct uthread
*)get_bsdthread_info(current_act());
863 oldset
= ut
->uu_sigmask
;
865 if (set
== (sigset_t
*) 0) {
866 /* need only old mask */
870 error
= copyin((caddr_t
)set
, (caddr_t
)&nset
, sizeof(sigset_t
));
876 ut
->uu_sigmask
|= (nset
& ~sigcantmask
);
880 ut
->uu_sigmask
&= ~(nset
);
881 signal_setast(current_act());
885 ut
->uu_sigmask
= (nset
& ~sigcantmask
);
886 signal_setast(current_act());
895 copyout((caddr_t
)&oldset
, (caddr_t
)oset
, sizeof(sigset_t
));
901 struct sigwait_args
{
907 sigwait(p
, uap
, retval
)
908 register struct proc
*p
;
909 register struct sigwait_args
*uap
;
912 register struct sigacts
*ps
= p
->p_sigacts
;
922 ut
= (struct uthread
*)get_bsdthread_info(current_act());
924 if (uap
->set
== (const sigset_t
*)0)
927 error
= copyin((caddr_t
)uap
->set
, (caddr_t
)&mask
, sizeof(sigset_t
));
931 siglist
= (mask
& ~sigcantmask
);
937 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
941 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
942 if (sigw
= uth
->uu_siglist
& siglist
) {
949 /* The signal was pending on a thread */
953 * When returning from sigwait, we want
954 * the old mask to be restored after the
955 * signal handler has finished. Thus, we
956 * save it here and mark the sigacts structure
959 ut
->uu_oldmask
= ut
->uu_sigmask
;
960 ut
->uu_flag
|= USAS_OLDMASK
;
961 if (siglist
== (sigset_t
)0)
963 /* SIGKILL and SIGSTOP are not maskable as well */
964 ut
->uu_sigmask
= ~(siglist
|sigcantmask
);
965 ut
->uu_sigwait
= siglist
;
966 /* No Continuations for now */
967 error
= tsleep((caddr_t
)&ut
->uu_sigwait
, PPAUSE
|PCATCH
, "pause", 0);
969 if ((error
== EINTR
) || (error
== ERESTART
))
972 sigw
= (ut
->uu_sigwait
& siglist
);
973 ut
->uu_sigmask
= ut
->uu_oldmask
;
975 ut
->uu_flag
&= ~USAS_OLDMASK
;
979 signum
= ffs((unsigned int)sigw
);
981 panic("sigwait with no signal wakeup");
982 ut
->uu_siglist
&= ~(sigmask(signum
));
984 error
= copyout(&signum
, uap
->sig
, sizeof(int));
992 struct osigstack_args
{
993 struct sigstack
*nss
;
994 struct sigstack
*oss
;
999 osigstack(p
, uap
, retval
)
1001 register struct osigstack_args
*uap
;
1005 struct sigacts
*psp
;
1009 ss
.ss_sp
= psp
->ps_sigstk
.ss_sp
;
1010 ss
.ss_onstack
= psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
;
1011 if (uap
->oss
&& (error
= copyout((caddr_t
)&ss
,
1012 (caddr_t
)uap
->oss
, sizeof (struct sigstack
))))
1014 if (uap
->nss
&& (error
= copyin((caddr_t
)uap
->nss
,
1015 (caddr_t
)&ss
, sizeof (ss
))) == 0) {
1016 psp
->ps_sigstk
.ss_sp
= ss
.ss_sp
;
1017 psp
->ps_sigstk
.ss_size
= 0;
1018 psp
->ps_sigstk
.ss_flags
|= ss
.ss_onstack
& SA_ONSTACK
;
1019 psp
->ps_flags
|= SAS_ALTSTACK
;
1023 #endif /* COMPAT_43 */
1025 struct sigaltstack_args
{
1026 struct sigaltstack
*nss
;
1027 struct sigaltstack
*oss
;
1031 sigaltstack(p
, uap
, retval
)
1033 register struct sigaltstack_args
*uap
;
1036 struct sigacts
*psp
;
1037 struct sigaltstack ss
;
1041 if ((psp
->ps_flags
& SAS_ALTSTACK
) == 0)
1042 psp
->ps_sigstk
.ss_flags
|= SA_DISABLE
;
1043 if (uap
->oss
&& (error
= copyout((caddr_t
)&psp
->ps_sigstk
,
1044 (caddr_t
)uap
->oss
, sizeof (struct sigaltstack
))))
1048 if (error
= copyin((caddr_t
)uap
->nss
, (caddr_t
)&ss
,
1051 if ((ss
.ss_flags
& ~SA_DISABLE
) != 0) {
1055 if (ss
.ss_flags
& SA_DISABLE
) {
1056 if (psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
)
1058 psp
->ps_flags
&= ~SAS_ALTSTACK
;
1059 psp
->ps_sigstk
.ss_flags
= ss
.ss_flags
;
1062 /* The older stacksize was 8K, enforce that one so no compat problems */
1063 #define OLDMINSIGSTKSZ 8*1024
1064 if (ss
.ss_size
< OLDMINSIGSTKSZ
)
1066 psp
->ps_flags
|= SAS_ALTSTACK
;
1077 kill(cp
, uap
, retval
)
1078 register struct proc
*cp
;
1079 register struct kill_args
*uap
;
1082 register struct proc
*p
;
1083 register struct pcred
*pc
= cp
->p_cred
;
1085 AUDIT_ARG(pid
, uap
->pid
);
1086 AUDIT_ARG(signum
, uap
->signum
);
1087 if ((u_int
)uap
->signum
>= NSIG
)
1090 /* kill single process */
1091 if ((p
= pfind(uap
->pid
)) == NULL
) {
1092 if ((p
= pzfind(uap
->pid
)) != NULL
) {
1094 * IEEE Std 1003.1-2001: return success
1095 * when killing a zombie.
1101 AUDIT_ARG(process
, p
);
1102 if (!cansignal(cp
, pc
, p
, uap
->signum
))
1105 psignal(p
, uap
->signum
);
1109 case -1: /* broadcast signal */
1110 return (killpg1(cp
, uap
->signum
, 0, 1));
1111 case 0: /* signal own process group */
1112 return (killpg1(cp
, uap
->signum
, 0, 0));
1113 default: /* negative explicit process group */
1114 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0));
1120 struct okillpg_args
{
1126 okillpg(p
, uap
, retval
)
1128 register struct okillpg_args
*uap
;
1132 AUDIT_ARG(pid
, uap
->pgid
);
1133 AUDIT_ARG(signum
, uap
->signum
);
1134 if ((u_int
)uap
->signum
>= NSIG
)
1136 return (killpg1(p
, uap
->signum
, uap
->pgid
, 0));
1138 #endif /* COMPAT_43 */
1141 * Common code for kill process group/broadcast kill.
1142 * cp is calling process.
1145 killpg1(cp
, signum
, pgid
, all
)
1146 register struct proc
*cp
;
1147 int signum
, pgid
, all
;
1149 register struct proc
*p
;
1150 register struct pcred
*pc
= cp
->p_cred
;
1158 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1159 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1160 p
== cp
|| !cansignal(cp
, pc
, p
, signum
))
1169 * zero pgid means send to my process group.
1173 pgrp
= pgfind(pgid
);
1177 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1178 p
= p
->p_pglist
.le_next
) {
1179 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1180 p
->p_stat
== SZOMB
||
1181 !cansignal(cp
, pc
, p
, signum
))
1188 return (nfound
? 0 : ESRCH
);
1192 * Send a signal to a process group.
1195 gsignal(pgid
, signum
)
1200 if (pgid
&& (pgrp
= pgfind(pgid
)))
1201 pgsignal(pgrp
, signum
, 0);
1205 * Send a signal to a process group. If checktty is 1,
1206 * limit to members which have a controlling terminal.
1209 pgsignal(pgrp
, signum
, checkctty
)
1211 int signum
, checkctty
;
1213 register struct proc
*p
;
1216 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1217 p
= p
->p_pglist
.le_next
)
1218 if (checkctty
== 0 || p
->p_flag
& P_CONTROLT
)
1223 * Send signal to a backgrounded process blocked due to tty access
1224 * In FreeBSD, the backgrounded process wakes up every second and
1225 * discovers whether it is foregounded or not. In our case, we block
1226 * the thread in tsleep as we want to avoid storm of processes as well
1227 * as the suspend is only at AST level
1230 tty_pgsignal(pgrp
, signum
)
1234 register struct proc
*p
;
1237 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1238 p
= p
->p_pglist
.le_next
)
1239 if ((p
->p_flag
& P_TTYSLEEP
) && (p
->p_flag
& P_CONTROLT
))
1244 * Send a signal caused by a trap to a specific thread.
1247 threadsignal(sig_actthread
, signum
, code
)
1248 register thread_act_t sig_actthread
;
1249 register int signum
;
1252 register struct uthread
*uth
;
1253 register struct task
* sig_task
;
1254 register struct proc
*p
;
1257 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1260 mask
= sigmask(signum
);
1261 if ((mask
& threadmask
) == 0)
1263 sig_task
= get_threadtask(sig_actthread
);
1264 p
= (struct proc
*)(get_bsdtask_info(sig_task
));
1266 uth
= get_bsdthread_info(sig_actthread
);
1267 if (uth
&& (uth
->uu_flag
& P_VFORK
))
1270 if (!(p
->p_flag
& P_TRACED
) && (p
->p_sigignore
& mask
))
1273 uth
->uu_siglist
|= mask
;
1274 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1275 uth
->uu_code
= code
;
1276 /* mark on process as well */
1277 signal_setast(sig_actthread
);
1283 register struct proc
*p
;
1284 register int signum
;
1286 psignal_lock(p
, signum
, 1);
1290 psignal_vfork(p
, new_task
, thr_act
, signum
)
1291 register struct proc
*p
;
1293 thread_act_t thr_act
;
1294 register int signum
;
1298 register int s
, prop
;
1299 register sig_t action
;
1302 struct uthread
*uth
;
1304 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1305 panic("psignal signal number");
1306 mask
= sigmask(signum
);
1307 prop
= sigprop
[signum
];
1310 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1313 #endif /* SIGNAL_DEBUG */
1315 if ((new_task
== TASK_NULL
) || (thr_act
== (thread_act_t
)NULL
) || is_kerneltask(new_task
))
1319 uth
= get_bsdthread_info(thr_act
);
1323 * proc is traced, always give parent a chance.
1327 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1328 (p
->p_flag
& P_TRACED
) == 0)
1331 if (prop
& SA_CONT
) {
1332 p
->p_siglist
&= ~stopsigmask
;
1333 uth
->uu_siglist
&= ~stopsigmask
;
1336 if (prop
& SA_STOP
) {
1338 * If sending a tty stop signal to a member of an orphaned
1339 * process group, discard the signal here if the action
1340 * is default; don't stop the process below if sleeping,
1341 * and don't clear any pending SIGCONT.
1343 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1346 uth
->uu_siglist
&= ~contsigmask
;
1347 p
->p_siglist
&= ~contsigmask
;
1349 uth
->uu_siglist
|= mask
;
1350 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1352 /* Deliver signal to the activation passed in */
1353 act_set_astbsd(thr_act
);
1356 * SIGKILL priority twiddling moved here from above because
1357 * it needs sig_thread. Could merge it into large switch
1358 * below if we didn't care about priority for tracing
1359 * as SIGKILL's action is always SIG_DFL.
1361 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1366 * This Process is traced - wake it up (if not already
1367 * stopped) so that it can discover the signal in
1368 * issig() and stop for the parent.
1370 if (p
->p_flag
& P_TRACED
) {
1371 if (p
->p_stat
!= SSTOP
)
1378 * If we're being traced (possibly because someone attached us
1379 * while we were stopped), check for a signal from the debugger.
1381 if (p
->p_stat
== SSTOP
) {
1382 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
1383 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1384 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1389 * setrunnable(p) in BSD
1398 get_signalthread(struct proc
*p
, int signum
)
1400 struct uthread
*uth
;
1401 thread_act_t thr_act
;
1402 sigset_t mask
= sigmask(signum
);
1403 thread_act_t sig_thread_act
;
1404 struct task
* sig_task
= p
->task
;
1407 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
1408 sig_thread_act
= p
->p_vforkact
;
1409 kret
= check_actforsig(sig_task
, sig_thread_act
, 1);
1410 if (kret
== KERN_SUCCESS
)
1411 return(sig_thread_act
);
1413 return(THR_ACT_NULL
);
1416 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1417 if(((uth
->uu_flag
& UNO_SIGMASK
)== 0) &&
1418 (((uth
->uu_sigmask
& mask
) == 0) || (uth
->uu_sigwait
& mask
))) {
1419 if (check_actforsig(p
->task
, uth
->uu_act
, 1) == KERN_SUCCESS
)
1420 return(uth
->uu_act
);
1423 if (get_signalact(p
->task
, &thr_act
, 1) == KERN_SUCCESS
) {
1427 return(THR_ACT_NULL
);
1431 * Send the signal to the process. If the signal has an action, the action
1432 * is usually performed by the target process rather than the caller; we add
1433 * the signal to the set of pending signals for the process.
1436 * o When a stop signal is sent to a sleeping process that takes the
1437 * default action, the process is stopped without awakening it.
1438 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1439 * regardless of the signal action (eg, blocked or ignored).
1441 * Other ignored signals are discarded immediately.
1444 psignal_lock(p
, signum
, withlock
)
1445 register struct proc
*p
;
1446 register int signum
;
1447 register int withlock
;
1449 register int s
, prop
;
1450 register sig_t action
;
1451 thread_act_t sig_thread_act
;
1452 register task_t sig_task
;
1454 struct uthread
*uth
;
1458 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1459 panic("psignal signal number");
1460 mask
= sigmask(signum
);
1461 prop
= sigprop
[signum
];
1464 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1467 #endif /* SIGNAL_DEBUG */
1469 if (thread_funnel_get() == (funnel_t
*)network_flock
) {
1471 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1474 * We will need the task pointer later. Grab it now to
1475 * check for a zombie process. Also don't send signals
1476 * to kernel internal tasks.
1478 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1480 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1485 KNOTE(&p
->p_klist
, NOTE_SIGNAL
| signum
);
1489 * do not send signals to the process that has the thread
1490 * doing a reboot(). Not doing so will mark that thread aborted
1491 * and can cause IO failures wich will cause data loss.
1493 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1495 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1503 * Deliver the signal to the first thread in the task. This
1504 * allows single threaded applications which use signals to
1505 * be able to be linked with multithreaded libraries. We have
1506 * an implicit reference to the current thread, but need
1507 * an explicit one otherwise. The thread reference keeps
1508 * the corresponding task data structures around too. This
1509 * reference is released by thread_deallocate.
1512 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1515 /* If successful return with ast set */
1516 sig_thread_act
= get_signalthread(p
, signum
);
1518 if (sig_thread_act
== THR_ACT_NULL
) {
1520 /* if it is sigkill, may be we should
1521 * inject a thread to terminate
1525 #endif /* SIGNAL_DEBUG */
1529 uth
= get_bsdthread_info(sig_thread_act
);
1532 * If proc is traced, always give parent a chance.
1534 if (p
->p_flag
& P_TRACED
)
1538 * If the signal is being ignored,
1539 * then we forget about it immediately.
1540 * (Note: we don't set SIGCONT in p_sigignore,
1541 * and if it is set to SIG_IGN,
1542 * action will be SIG_DFL here.)
1544 if (p
->p_sigignore
& mask
)
1546 if (uth
->uu_sigwait
& mask
)
1548 if (uth
->uu_sigmask
& mask
)
1550 else if (p
->p_sigcatch
& mask
)
1556 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1557 (p
->p_flag
& P_TRACED
) == 0)
1560 if (prop
& SA_CONT
) {
1561 uth
->uu_siglist
&= ~stopsigmask
;
1562 p
->p_siglist
&= ~stopsigmask
;
1565 if (prop
& SA_STOP
) {
1567 * If sending a tty stop signal to a member of an orphaned
1568 * process group, discard the signal here if the action
1569 * is default; don't stop the process below if sleeping,
1570 * and don't clear any pending SIGCONT.
1572 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1575 uth
->uu_siglist
&= ~contsigmask
;
1576 p
->p_siglist
&= ~contsigmask
;
1578 uth
->uu_siglist
|= mask
;
1579 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1583 * Defer further processing for signals which are held,
1584 * except that stopped processes must be continued by SIGCONT.
1586 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
)) {
1590 * SIGKILL priority twiddling moved here from above because
1591 * it needs sig_thread. Could merge it into large switch
1592 * below if we didn't care about priority for tracing
1593 * as SIGKILL's action is always SIG_DFL.
1595 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1600 * Process is traced - wake it up (if not already
1601 * stopped) so that it can discover the signal in
1602 * issig() and stop for the parent.
1604 if (p
->p_flag
& P_TRACED
) {
1605 if (p
->p_stat
!= SSTOP
)
1611 if (action
== SIG_WAIT
) {
1612 uth
->uu_sigwait
= mask
;
1613 uth
->uu_siglist
&= ~mask
;
1614 p
->p_siglist
&= ~mask
;
1615 wakeup(&uth
->uu_sigwait
);
1616 /* if it is SIGCONT resume whole process */
1618 (void) task_resume(sig_task
);
1622 if (action
!= SIG_DFL
) {
1624 * User wants to catch the signal.
1625 * Wake up the thread, but don't un-suspend it
1626 * (except for SIGCONT).
1628 if (prop
& SA_CONT
) {
1629 if (p
->p_flag
& P_TTYSLEEP
) {
1630 p
->p_flag
&= ~P_TTYSLEEP
;
1631 wakeup(&p
->p_siglist
);
1633 (void) task_resume(sig_task
);
1636 } else if (p
->p_stat
== SSTOP
)
1640 /* Default action - varies */
1641 if (mask
& stopsigmask
) {
1643 * These are the signals which by default
1646 * Don't clog system with children of init
1647 * stopped from the keyboard.
1649 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1650 psignal_lock(p
, SIGKILL
, 0);
1651 uth
->uu_siglist
&= ~mask
;
1652 p
->p_siglist
&= ~mask
;
1658 * if task hasn't already been stopped by
1661 uth
->uu_siglist
&= ~mask
;
1662 p
->p_siglist
&= ~mask
;
1663 if (p
->p_stat
!= SSTOP
) {
1664 p
->p_xstat
= signum
;
1666 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1667 struct proc
*pp
= p
->p_pptr
;
1669 pp
->si_pid
= p
->p_pid
;
1670 pp
->si_status
= p
->p_xstat
;
1671 pp
->si_code
= CLD_STOPPED
;
1672 pp
->si_uid
= p
->p_cred
->p_ruid
;
1673 psignal(pp
, SIGCHLD
);
1681 * Signals ignored by default have been dealt
1682 * with already, since their bits are on in
1688 * Kill signal always sets process running and
1692 * Process will be running after 'run'
1696 thread_abort(sig_thread_act
);
1702 * Let the process run. If it's sleeping on an
1703 * event, it remains so.
1705 if (p
->p_flag
& P_TTYSLEEP
) {
1706 p
->p_flag
&= ~P_TTYSLEEP
;
1707 wakeup(&p
->p_siglist
);
1709 (void) task_resume(sig_task
);
1711 uth
->uu_siglist
&= ~mask
;
1712 p
->p_siglist
&= ~mask
;
1719 * All other signals wake up the process, but don't
1722 if (p
->p_stat
== SSTOP
)
1730 * If we're being traced (possibly because someone attached us
1731 * while we were stopped), check for a signal from the debugger.
1733 if (p
->p_stat
== SSTOP
) {
1734 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
1735 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1738 * setrunnable(p) in BSD and
1739 * Wake up the thread if it is interruptible.
1742 thread_abort_safely(sig_thread_act
);
1748 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1752 /* psignal_lock(p, signum, withlock ) */
1754 psignal_uthread(thr_act
, signum
)
1755 thread_act_t thr_act
;
1759 register int s
, prop
;
1760 register sig_t action
;
1761 thread_act_t sig_thread_act
;
1762 register task_t sig_task
;
1764 struct uthread
*uth
;
1768 p
= (struct proc
*)get_bsdtask_info(get_threadtask(thr_act
));
1769 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1770 panic("Invalid signal number in psignal_uthread");
1771 mask
= sigmask(signum
);
1772 prop
= sigprop
[signum
];
1775 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1778 #endif /* SIGNAL_DEBUG */
1781 * We will need the task pointer later. Grab it now to
1782 * check for a zombie process. Also don't send signals
1783 * to kernel internal tasks.
1785 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1789 sig_thread_act
= thr_act
;
1791 * do not send signals to the process that has the thread
1792 * doing a reboot(). Not doing so will mark that thread aborted
1793 * and can cause IO failures wich will cause data loss.
1795 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1802 * Deliver the signal to the first thread in the task. This
1803 * allows single threaded applications which use signals to
1804 * be able to be linked with multithreaded libraries. We have
1805 * an implicit reference to the current thread, but need
1806 * an explicit one otherwise. The thread reference keeps
1807 * the corresponding task data structures around too. This
1808 * reference is released by thread_deallocate.
1811 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1814 kret
= check_actforsig(sig_task
, sig_thread_act
, 1);
1816 if (kret
!= KERN_SUCCESS
) {
1822 uth
= get_bsdthread_info(sig_thread_act
);
1825 * If proc is traced, always give parent a chance.
1827 if (p
->p_flag
& P_TRACED
)
1831 * If the signal is being ignored,
1832 * then we forget about it immediately.
1833 * (Note: we don't set SIGCONT in p_sigignore,
1834 * and if it is set to SIG_IGN,
1835 * action will be SIG_DFL here.)
1837 if (p
->p_sigignore
& mask
)
1839 if (uth
->uu_sigwait
& mask
)
1841 if (uth
->uu_sigmask
& mask
)
1843 else if (p
->p_sigcatch
& mask
)
1849 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1850 (p
->p_flag
& P_TRACED
) == 0)
1853 if (prop
& SA_CONT
) {
1854 uth
->uu_siglist
&= ~stopsigmask
;
1855 p
->p_siglist
&= ~stopsigmask
;
1858 if (prop
& SA_STOP
) {
1860 * If sending a tty stop signal to a member of an orphaned
1861 * process group, discard the signal here if the action
1862 * is default; don't stop the process below if sleeping,
1863 * and don't clear any pending SIGCONT.
1865 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1868 uth
->uu_siglist
&= ~contsigmask
;
1869 p
->p_siglist
&= ~contsigmask
;
1871 uth
->uu_siglist
|= mask
;
1872 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1875 * Defer further processing for signals which are held,
1876 * except that stopped processes must be continued by SIGCONT.
1878 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
))
1882 * SIGKILL priority twiddling moved here from above because
1883 * it needs sig_thread. Could merge it into large switch
1884 * below if we didn't care about priority for tracing
1885 * as SIGKILL's action is always SIG_DFL.
1887 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1892 * Process is traced - wake it up (if not already
1893 * stopped) so that it can discover the signal in
1894 * issig() and stop for the parent.
1896 if (p
->p_flag
& P_TRACED
) {
1897 if (p
->p_stat
!= SSTOP
)
1903 if (action
== SIG_WAIT
) {
1904 uth
->uu_sigwait
= mask
;
1905 uth
->uu_siglist
&= ~mask
;
1906 p
->p_siglist
&= ~mask
;
1907 wakeup(&uth
->uu_sigwait
);
1908 /* if it is SIGCONT resume whole process */
1910 (void) task_resume(sig_task
);
1914 if (action
!= SIG_DFL
) {
1916 * User wants to catch the signal.
1917 * Wake up the thread, but don't un-suspend it
1918 * (except for SIGCONT).
1921 (void) task_resume(sig_task
);
1924 /* Default action - varies */
1925 if (mask
& stopsigmask
) {
1927 * These are the signals which by default
1930 * Don't clog system with children of init
1931 * stopped from the keyboard.
1933 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1934 psignal_lock(p
, SIGKILL
, 0);
1935 uth
->uu_siglist
&= ~mask
;
1936 p
->p_siglist
&= ~mask
;
1942 * if task hasn't already been stopped by
1945 uth
->uu_siglist
&= ~mask
;
1946 p
->p_siglist
&= ~mask
;
1947 if (p
->p_stat
!= SSTOP
) {
1948 p
->p_xstat
= signum
;
1949 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1950 struct proc
*pp
= p
->p_pptr
;
1952 pp
->si_pid
= p
->p_pid
;
1953 pp
->si_status
= p
->p_xstat
;
1954 pp
->si_code
= CLD_STOPPED
;
1955 pp
->si_uid
= p
->p_cred
->p_ruid
;
1956 psignal(pp
, SIGCHLD
);
1965 * Signals ignored by default have been dealt
1966 * with already, since their bits are on in
1972 * Kill signal always sets process running and
1976 * Process will be running after 'run'
1980 thread_abort(sig_thread_act
);
1986 * Let the process run. If it's sleeping on an
1987 * event, it remains so.
1989 if (p
->p_flag
& P_TTYSLEEP
) {
1990 p
->p_flag
&= ~P_TTYSLEEP
;
1991 wakeup(&p
->p_siglist
);
1993 (void) task_resume(sig_task
);
1995 uth
->uu_siglist
&= ~mask
;
1996 p
->p_siglist
&= ~mask
;
2002 * All other signals wake up the process, but don't
2011 * If we're being traced (possibly because someone attached us
2012 * while we were stopped), check for a signal from the debugger.
2014 if (p
->p_stat
== SSTOP
) {
2015 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
2016 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
2017 p
->p_siglist
|= sigmask(p
->p_xstat
);
2021 * setrunnable(p) in BSD and
2022 * Wake up the thread if it is interruptible.
2025 thread_abort_safely(sig_thread_act
);
2037 thread_t self
= current_act();
2039 p
->exit_thread
= self
;
2040 (void) task_suspend(p
->task
);
2047 thread_t self
= current_act();
2049 while (p
->sigwait
|| p
->exit_thread
) {
2050 if (p
->exit_thread
) {
2051 if (p
->exit_thread
!= self
) {
2053 * Already exiting - no signals.
2059 if(assert_wait_possible()) {
2060 assert_wait((caddr_t
)&p
->sigwait_thread
,
2061 (THREAD_INTERRUPTIBLE
));
2064 thread_block(THREAD_CONTINUE_NULL
);
2066 if (thread_should_abort(self
)) {
2068 * Terminate request - clean up.
2077 * If the current process has received a signal (should be caught or cause
2078 * termination, should interrupt current syscall), return the signal number.
2079 * Stop signals with default action are processed immediately, then cleared;
2080 * they aren't returned. This is checked after each entry to the system for
2081 * a syscall or trap (though this can usually be done without calling issignal
2082 * by checking the pending signal masks in the CURSIG macro.) The normal call
2085 * while (signum = CURSIG(curproc))
2090 register struct proc
*p
;
2092 register int signum
, mask
, prop
, sigbits
;
2093 task_t task
= p
->task
;
2094 thread_act_t cur_act
;
2096 struct uthread
* ut
;
2100 cur_act
= current_act();
2103 if(rdebug_proc
&& (p
== rdebug_proc
)) {
2106 #endif /* SIGNAL_DEBUG */
2110 * Try to grab the signal lock.
2112 if (sig_try_locked(p
) <= 0) {
2117 ut
= get_bsdthread_info(cur_act
);
2119 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2121 if (p
->p_flag
& P_PPWAIT
)
2122 sigbits
&= ~stopsigmask
;
2123 if (sigbits
== 0) { /* no signal to send */
2127 signum
= ffs((long)sigbits
);
2128 mask
= sigmask(signum
);
2129 prop
= sigprop
[signum
];
2132 * We should see pending but ignored signals
2133 * only if P_TRACED was on when they were posted.
2135 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2136 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2137 p
->p_siglist
&= ~mask
; /* take the signal! */
2140 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2142 register task_t task
;
2144 * If traced, always stop, and stay
2145 * stopped until released by the debugger.
2147 /* ptrace debugging */
2148 p
->p_xstat
= signum
;
2150 if (p
->p_flag
& P_SIGEXC
) {
2152 p
->sigwait_thread
= cur_act
;
2154 p
->p_flag
&= ~P_WAITED
;
2155 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2156 p
->p_siglist
&= ~mask
; /* clear the old signal */
2158 do_bsdexception(EXC_SOFTWARE
, EXC_SOFT_SIGNAL
, signum
);
2161 // panic("Unsupportef gdb option \n");;
2162 pp
->si_pid
= p
->p_pid
;
2163 pp
->si_status
= p
->p_xstat
;
2164 pp
->si_code
= CLD_TRAPPED
;
2165 pp
->si_uid
= p
->p_cred
->p_ruid
;
2166 psignal(pp
, SIGCHLD
);
2168 * XXX Have to really stop for debuggers;
2169 * XXX stop() doesn't do the right thing.
2170 * XXX Inline the task_suspend because we
2171 * XXX have to diddle Unix state in the
2177 p
->sigwait_thread
= cur_act
;
2179 p
->p_flag
&= ~P_WAITED
;
2180 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2181 p
->p_siglist
&= ~mask
; /* clear the old signal */
2183 wakeup((caddr_t
)p
->p_pptr
);
2185 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
2186 thread_block(THREAD_CONTINUE_NULL
);
2191 p
->sigwait_thread
= NULL
;
2192 wakeup((caddr_t
)&p
->sigwait_thread
);
2195 * This code is to detect when gdb is killed
2196 * even as the traced program is attached.
2197 * pgsignal would get the SIGKILL to traced program
2198 * That's what we are trying to see (I hope)
2200 if (ut
->uu_siglist
& sigmask(SIGKILL
)) {
2202 * Wait event may still be outstanding;
2203 * clear it, since sig_lock_to_exit will
2206 clear_wait(current_act(), THREAD_INTERRUPTED
);
2207 sig_lock_to_exit(p
);
2209 * Since this thread will be resumed
2210 * to allow the current syscall to
2211 * be completed, must save u_qsave
2212 * before calling exit(). (Since exit()
2213 * calls closef() which can trash u_qsave.)
2216 exit1(p
,signum
, (int *)NULL
);
2221 * We may have to quit
2223 if (thread_should_abort(current_act())) {
2228 * If parent wants us to take the signal,
2229 * then it will leave it in p->p_xstat;
2230 * otherwise we just look for signals again.
2232 signum
= p
->p_xstat
;
2236 * Put the new signal into p_siglist. If the
2237 * signal is being masked, look for other signals.
2239 mask
= sigmask(signum
);
2240 ut
->uu_siglist
|= mask
;
2241 p
->p_siglist
|= mask
; /* just for lame ones looking here */
2242 if (ut
->uu_sigmask
& mask
)
2247 * Decide whether the signal should be returned.
2248 * Return the signal's number, or fall through
2249 * to clear it from the pending mask.
2252 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2256 * Don't take default actions on system processes.
2258 if (p
->p_pptr
->p_pid
== 0) {
2261 * Are you sure you want to ignore SIGSEGV
2264 printf("Process (pid %d) got signal %d\n",
2267 break; /* == ignore */
2271 * If there is a pending stop signal to process
2272 * with default action, stop here,
2273 * then clear the signal. However,
2274 * if process is member of an orphaned
2275 * process group, ignore tty stop signals.
2277 if (prop
& SA_STOP
) {
2278 if (p
->p_flag
& P_TRACED
||
2279 (p
->p_pgrp
->pg_jobc
== 0 &&
2281 break; /* == ignore */
2282 if (p
->p_stat
!= SSTOP
) {
2283 p
->p_xstat
= signum
;
2285 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
2287 pp
->si_pid
= p
->p_pid
;
2288 pp
->si_status
= p
->p_xstat
;
2289 pp
->si_code
= CLD_STOPPED
;
2290 pp
->si_uid
= p
->p_cred
->p_ruid
;
2291 psignal(pp
, SIGCHLD
);
2295 } else if (prop
& SA_IGNORE
) {
2297 * Except for SIGCONT, shouldn't get here.
2298 * Default action is to ignore; drop it.
2300 break; /* == ignore */
2302 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2303 p
->p_siglist
&= ~mask
; /* take the signal! */
2311 * Masking above should prevent us ever trying
2312 * to take action on an ignored signal other
2313 * than SIGCONT, unless process is traced.
2315 if ((prop
& SA_CONT
) == 0 &&
2316 (p
->p_flag
& P_TRACED
) == 0)
2317 printf("issignal\n");
2318 break; /* == ignore */
2322 * This signal has an action, let
2323 * postsig() process it.
2325 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2326 p
->p_siglist
&= ~mask
; /* take the signal! */
2330 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2331 p
->p_siglist
&= ~mask
; /* take the signal! */
2336 /* called from _sleep */
2339 register struct proc
*p
;
2341 register int signum
, mask
, prop
, sigbits
;
2342 task_t task
= p
->task
;
2343 thread_act_t cur_act
;
2345 struct uthread
* ut
;
2349 cur_act
= current_act();
2351 ut
= get_bsdthread_info(cur_act
);
2353 if (ut
->uu_siglist
== 0)
2356 if (((ut
->uu_siglist
& ~ut
->uu_sigmask
) == 0) && ((p
->p_flag
& P_TRACED
) == 0))
2359 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2362 if (p
->p_flag
& P_PPWAIT
)
2363 sigbits
&= ~stopsigmask
;
2364 if (sigbits
== 0) { /* no signal to send */
2368 signum
= ffs((long)sigbits
);
2369 mask
= sigmask(signum
);
2370 prop
= sigprop
[signum
];
2373 * We should see pending but ignored signals
2374 * only if P_TRACED was on when they were posted.
2376 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2379 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2381 * Put the new signal into p_siglist. If the
2382 * signal is being masked, look for other signals.
2384 mask
= sigmask(signum
);
2385 if (ut
->uu_sigmask
& mask
)
2391 * Decide whether the signal should be returned.
2392 * Return the signal's number, or fall through
2393 * to clear it from the pending mask.
2396 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2400 * Don't take default actions on system processes.
2402 if (p
->p_pptr
->p_pid
== 0) {
2405 * Are you sure you want to ignore SIGSEGV
2408 printf("Process (pid %d) got signal %d\n",
2411 break; /* == ignore */
2415 * If there is a pending stop signal to process
2416 * with default action, stop here,
2417 * then clear the signal. However,
2418 * if process is member of an orphaned
2419 * process group, ignore tty stop signals.
2421 if (prop
& SA_STOP
) {
2422 if (p
->p_flag
& P_TRACED
||
2423 (p
->p_pgrp
->pg_jobc
== 0 &&
2425 break; /* == ignore */
2428 } else if (prop
& SA_IGNORE
) {
2430 * Except for SIGCONT, shouldn't get here.
2431 * Default action is to ignore; drop it.
2433 break; /* == ignore */
2441 * Masking above should prevent us ever trying
2442 * to take action on an ignored signal other
2443 * than SIGCONT, unless process is traced.
2445 if ((prop
& SA_CONT
) == 0 &&
2446 (p
->p_flag
& P_TRACED
) == 0)
2447 printf("issignal\n");
2448 break; /* == ignore */
2452 * This signal has an action, let
2453 * postsig() process it.
2457 sigbits
&= ~mask
; /* take the signal! */
2463 * Put the argument process into the stopped state and notify the parent
2464 * via wakeup. Signals are handled elsewhere. The process must not be
2469 register struct proc
*p
;
2472 p
->p_flag
&= ~P_WAITED
;
2473 if (p
->p_pptr
->p_stat
!= SSTOP
)
2474 wakeup((caddr_t
)p
->p_pptr
);
2475 (void) task_suspend(p
->task
); /*XXX*/
2479 * Take the action for the specified signal
2480 * from the current set of pending signals.
2484 register int signum
;
2486 register struct proc
*p
= current_proc();
2487 register struct sigacts
*ps
= p
->p_sigacts
;
2488 register sig_t action
;
2490 int mask
, returnmask
;
2491 struct uthread
* ut
;
2497 * This must be called on master cpu
2499 if (cpu_number() != master_cpu
)
2500 panic("psig not on master");
2505 * Try to grab the signal lock.
2507 if (sig_try_locked(p
) <= 0) {
2512 ut
= (struct uthread
*)get_bsdthread_info(current_act());
2513 mask
= sigmask(signum
);
2514 ut
->uu_siglist
&= ~mask
;
2515 p
->p_siglist
&= ~mask
;
2516 action
= ps
->ps_sigact
[signum
];
2518 if (KTRPOINT(p
, KTR_PSIG
))
2519 ktrpsig(p
->p_tracep
,
2520 signum
, action
, ut
->uu_flag
& USAS_OLDMASK
?
2521 &ut
->uu_oldmask
: &ut
->uu_sigmask
, 0, -1);
2523 if (action
== SIG_DFL
) {
2525 * Default action, where the default is to kill
2526 * the process. (Other cases were ignored above.)
2528 /* called with signal_lock() held */
2529 sigexit_locked(p
, signum
);
2534 * If we get here, the signal must be caught.
2537 if (action
== SIG_IGN
|| (ut
->uu_sigmask
& mask
))
2539 "postsig: processing masked or ignored signal\n");
2542 * Set the new mask value and also defer further
2543 * occurences of this signal.
2545 * Special case: user has done a sigpause. Here the
2546 * current mask is not of interest, but rather the
2547 * mask from before the sigpause is what we want
2548 * restored after the signal processing is completed.
2550 if (ut
->uu_flag
& USAS_OLDMASK
) {
2551 returnmask
= ut
->uu_oldmask
;
2552 ut
->uu_flag
&= ~USAS_OLDMASK
;
2555 returnmask
= ut
->uu_sigmask
;
2556 ut
->uu_sigmask
|= ps
->ps_catchmask
[signum
];
2557 if ((ps
->ps_signodefer
& mask
) == 0)
2558 ut
->uu_sigmask
|= mask
;
2559 if ((signum
!= SIGILL
) && (signum
!= SIGTRAP
) && (ps
->ps_sigreset
& mask
)) {
2560 if ((signum
!= SIGCONT
) && (sigprop
[signum
] & SA_IGNORE
))
2561 p
->p_sigignore
|= mask
;
2562 ps
->ps_sigact
[signum
] = SIG_DFL
;
2563 ps
->ps_siginfo
&= ~mask
;
2564 ps
->ps_signodefer
&= ~mask
;
2567 /* Needs to disable to run in user mode */
2568 if (signum
== SIGFPE
) {
2569 thread_enable_fpe(current_act(), 0);
2571 #endif /* __ppc__ */
2573 if (ps
->ps_sig
!= signum
) {
2579 p
->p_stats
->p_ru
.ru_nsignals
++;
2580 sendsig(p
, action
, signum
, returnmask
, code
);
2586 * Force the current process to exit with the specified signal, dumping core
2587 * if appropriate. We bypass the normal tests for masked and caught signals,
2588 * allowing unrecoverable failures to terminate the process without changing
2589 * signal state. Mark the accounting record with the signal termination.
2590 * If dumping core, save the signal number for the debugger. Calls exit and
2593 /* called with signal lock */
2595 sigexit_locked(p
, signum
)
2596 register struct proc
*p
;
2600 sig_lock_to_exit(p
);
2601 p
->p_acflag
|= AXSIG
;
2602 if (sigprop
[signum
] & SA_CORE
) {
2603 p
->p_sigacts
->ps_sig
= signum
;
2604 if (coredump(p
) == 0)
2605 signum
|= WCOREFLAG
;
2608 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
2614 filt_sigattach(struct knote
*kn
)
2616 struct proc
*p
= current_proc();
2618 kn
->kn_ptr
.p_proc
= p
;
2619 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
2621 /* XXX lock the proc here while adding to the list? */
2622 KNOTE_ATTACH(&p
->p_klist
, kn
);
2628 filt_sigdetach(struct knote
*kn
)
2630 struct proc
*p
= kn
->kn_ptr
.p_proc
;
2632 KNOTE_DETACH(&p
->p_klist
, kn
);
2636 * signal knotes are shared with proc knotes, so we apply a mask to
2637 * the hint in order to differentiate them from process hints. This
2638 * could be avoided by using a signal-specific knote list, but probably
2639 * isn't worth the trouble.
2642 filt_signal(struct knote
*kn
, long hint
)
2645 if (hint
& NOTE_SIGNAL
) {
2646 hint
&= ~NOTE_SIGNAL
;
2648 if (kn
->kn_id
== hint
)
2651 return (kn
->kn_data
!= 0);
2655 bsd_ast(thread_act_t thr_act
)
2657 struct proc
*p
= current_proc();
2658 struct uthread
*ut
= get_bsdthread_info(thr_act
);
2661 boolean_t funnel_state
;
2662 static bsd_init_done
= 0;
2667 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2669 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
2670 pc
= get_useraddr();
2671 addupc_task(p
, pc
, 1);
2672 p
->p_flag
&= ~P_OWEUPC
;
2675 if (CHECK_SIGNALS(p
, current_act(), ut
)) {
2676 while (signum
= issignal(p
))
2679 if (!bsd_init_done
) {
2680 extern void bsdinit_task(void);
2686 (void) thread_funnel_set(kernel_flock
, FALSE
);
2690 * Follwing routines are called using callout from bsd_hardclock
2691 * so that psignals are called in a thread context and are funneled
2694 psignal_vtalarm(struct proc
*p
)
2696 boolean_t funnel_state
;
2700 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2701 psignal_lock(p
, SIGVTALRM
, 1);
2702 (void) thread_funnel_set(kernel_flock
, FALSE
);
2706 psignal_xcpu(struct proc
*p
)
2708 boolean_t funnel_state
;
2712 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2713 psignal_lock(p
, SIGXCPU
, 1);
2714 (void) thread_funnel_set(kernel_flock
, FALSE
);
2718 psignal_sigprof(struct proc
*p
)
2720 boolean_t funnel_state
;
2724 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2725 psignal_lock(p
, SIGPROF
, 1);
2726 (void) thread_funnel_set(kernel_flock
, FALSE
);
2729 /* ptrace set runnalbe */
2731 pt_setrunnable(struct proc
*p
)
2737 if (p
->p_flag
& P_TRACED
) {
2740 wakeup((caddr_t
)&(p
->sigwait
));
2752 exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
2753 extern kern_return_t
bsd_exception(int, exception_data_type_t codes
[], int);
2757 return(bsd_exception(exc
, codes
, 2));