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);
116 static int filt_sigattach(struct knote
*kn
);
117 static void filt_sigdetach(struct knote
*kn
);
118 static int filt_signal(struct knote
*kn
, long hint
);
120 struct filterops sig_filtops
=
121 { 0, filt_sigattach
, filt_sigdetach
, filt_signal
};
124 void ram_printf
__P((int));
126 unsigned int rdebug_proc
=0;
133 #endif /* SIGNAL_DEBUG */
136 signal_lock(struct proc
*p
)
143 int register sp
, *fp
, numsaved
;
145 __asm__
volatile("mr %0,r1" : "=r" (sp
));
147 fp
= (int *)*((int *)sp
);
148 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
149 p
->lockpc
[numsaved
] = fp
[2];
156 #endif /* SIGNAL_DEBUG */
157 #endif /* DIAGNOSTIC */
160 /* TBD: check p last arg */
161 error
= lockmgr(&p
->signal_lock
, LK_EXCLUSIVE
, 0, (struct proc
*)p
);
168 signal_unlock(struct proc
*p
)
174 int register sp
, *fp
, numsaved
;
176 __asm__
volatile("mr %0,r1" : "=r" (sp
));
178 fp
= (int *)*((int *)sp
);
179 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
180 p
->unlockpc
[numsaved
] = fp
[2];
187 #endif /* SIGNAL_DEBUG */
188 #endif /* DIAGNOSTIC */
190 /* TBD: check p last arg */
191 return(lockmgr(&p
->signal_lock
, LK_RELEASE
, (simple_lock_t
)0, (struct proc
*)p
));
195 signal_setast(sig_actthread
)
196 thread_act_t sig_actthread
;
198 act_set_astbsd(sig_actthread
);
202 * Can process p, with pcred pc, send the signal signum to process q?
205 cansignal(p
, pc
, q
, signum
)
211 /* you can signal yourself */
215 if (pc
->pc_ucred
->cr_uid
== 0)
216 return (1); /* root can always signal */
218 if (signum
== SIGCONT
&& q
->p_session
== p
->p_session
)
219 return (1); /* SIGCONT in session */
222 * Using kill(), only certain signals can be sent to setugid
225 if (q
->p_flag
& P_SUGID
) {
238 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
239 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
240 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
241 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
248 * because the P_SUGID test exists, this has extra tests which
251 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
252 pc
->p_ruid
== q
->p_cred
->p_svuid
||
253 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
254 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_svuid
||
255 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
256 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
261 struct sigaction_args
{
263 struct __sigaction
*nsa
;
264 struct sigaction
*osa
;
269 sigaction(p
, uap
, retval
)
271 register struct sigaction_args
*uap
;
274 struct sigaction vec
;
275 struct __sigaction __vec
;
277 register struct sigaction
*sa
;
278 register struct sigacts
*ps
= p
->p_sigacts
;
282 signum
= uap
->signum
;
283 if (signum
<= 0 || signum
>= NSIG
||
284 signum
== SIGKILL
|| signum
== SIGSTOP
)
288 sa
->sa_handler
= ps
->ps_sigact
[signum
];
289 sa
->sa_mask
= ps
->ps_catchmask
[signum
];
290 bit
= sigmask(signum
);
292 if ((ps
->ps_sigonstack
& bit
) != 0)
293 sa
->sa_flags
|= SA_ONSTACK
;
294 if ((ps
->ps_sigintr
& bit
) == 0)
295 sa
->sa_flags
|= SA_RESTART
;
296 if (ps
->ps_siginfo
& bit
)
297 sa
->sa_flags
|= SA_SIGINFO
;
298 if (ps
->ps_signodefer
& bit
)
299 sa
->sa_flags
|= SA_NODEFER
;
300 if (ps
->ps_64regset
& bit
)
301 sa
->sa_flags
|= SA_64REGSET
;
302 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDSTOP
))
303 sa
->sa_flags
|= SA_NOCLDSTOP
;
304 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDWAIT
))
305 sa
->sa_flags
|= SA_NOCLDWAIT
;
306 if (error
= copyout((caddr_t
)sa
, (caddr_t
)uap
->osa
,
311 if (error
= copyin((caddr_t
)uap
->nsa
, (caddr_t
)&__vec
,
314 error
= setsigvec(p
, signum
, &__vec
);
319 /* Routines to manipulate bits on all threads */
321 clear_procsiglist(struct proc
*p
, int bit
)
323 struct uthread
* uth
;
328 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
329 thact
= p
->p_vforkact
;
330 uth
= (struct uthread
*)get_bsdthread_info(thact
);
332 uth
->uu_siglist
&= ~bit
;
334 p
->p_siglist
&= ~bit
;
339 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
340 uth
->uu_siglist
&= ~bit
;
342 p
->p_siglist
&= ~bit
;
348 unblock_procsigmask(struct proc
*p
, int bit
)
350 struct uthread
* uth
;
354 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
355 thact
= p
->p_vforkact
;
356 uth
= (struct uthread
*)get_bsdthread_info(thact
);
358 uth
->uu_sigmask
&= ~bit
;
360 p
->p_sigmask
&= ~bit
;
364 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
365 uth
->uu_sigmask
&= ~bit
;
367 p
->p_sigmask
&= ~bit
;
374 block_procsigmask(struct proc
*p
, int bit
)
376 struct uthread
* uth
;
380 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
381 thact
= p
->p_vforkact
;
382 uth
= (struct uthread
*)get_bsdthread_info(thact
);
384 uth
->uu_sigmask
|= bit
;
390 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
391 uth
->uu_sigmask
|= bit
;
398 set_procsigmask(struct proc
*p
, int bit
)
400 struct uthread
* uth
;
404 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
405 thact
= p
->p_vforkact
;
406 uth
= (struct uthread
*)get_bsdthread_info(thact
);
408 uth
->uu_sigmask
= bit
;
414 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
415 uth
->uu_sigmask
= bit
;
423 setsigvec(p
, signum
, sa
)
424 register struct proc
*p
;
426 register struct __sigaction
*sa
;
428 register struct sigacts
*ps
= p
->p_sigacts
;
431 if ((signum
== SIGKILL
|| signum
== SIGSTOP
) &&
432 sa
->sa_handler
!= SIG_DFL
)
434 bit
= sigmask(signum
);
436 * Change setting atomically.
438 ps
->ps_sigact
[signum
] = sa
->sa_handler
;
439 ps
->ps_trampact
[signum
] = (sig_t
) sa
->sa_tramp
;
440 ps
->ps_catchmask
[signum
] = sa
->sa_mask
&~ sigcantmask
;
441 if (sa
->sa_flags
& SA_SIGINFO
)
442 ps
->ps_siginfo
|= bit
;
444 ps
->ps_siginfo
&= ~bit
;
445 if (sa
->sa_flags
& SA_64REGSET
)
446 ps
->ps_64regset
|= bit
;
448 ps
->ps_64regset
&= ~bit
;
449 if ((sa
->sa_flags
& SA_RESTART
) == 0)
450 ps
->ps_sigintr
|= bit
;
452 ps
->ps_sigintr
&= ~bit
;
453 if (sa
->sa_flags
& SA_ONSTACK
)
454 ps
->ps_sigonstack
|= bit
;
456 ps
->ps_sigonstack
&= ~bit
;
457 if (sa
->sa_flags
& SA_USERTRAMP
)
458 ps
->ps_usertramp
|= bit
;
460 ps
->ps_usertramp
&= ~bit
;
461 if (sa
->sa_flags
& SA_RESETHAND
)
462 ps
->ps_sigreset
|= bit
;
464 ps
->ps_sigreset
&= ~bit
;
465 if (sa
->sa_flags
& SA_NODEFER
)
466 ps
->ps_signodefer
|= bit
;
468 ps
->ps_signodefer
&= ~bit
;
469 if (signum
== SIGCHLD
) {
470 if (sa
->sa_flags
& SA_NOCLDSTOP
)
471 p
->p_flag
|= P_NOCLDSTOP
;
473 p
->p_flag
&= ~P_NOCLDSTOP
;
474 if ((sa
->sa_flags
& SA_NOCLDWAIT
) || (sa
->sa_handler
== SIG_IGN
))
475 p
->p_flag
|= P_NOCLDWAIT
;
477 p
->p_flag
&= ~P_NOCLDWAIT
;
481 if (signum
== SIGFPE
) {
482 if (sa
->sa_handler
== SIG_DFL
|| sa
->sa_handler
== SIG_IGN
)
483 thread_enable_fpe(current_act(), 0);
485 thread_enable_fpe(current_act(), 1);
489 * Set bit in p_sigignore for signals that are set to SIG_IGN,
490 * and for signals set to SIG_DFL where the default is to ignore.
491 * However, don't put SIGCONT in p_sigignore,
492 * as we have to restart the process.
494 if (sa
->sa_handler
== SIG_IGN
||
495 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
497 clear_procsiglist(p
, bit
);
498 if (signum
!= SIGCONT
)
499 p
->p_sigignore
|= bit
; /* easier in psignal */
500 p
->p_sigcatch
&= ~bit
;
502 p
->p_sigignore
&= ~bit
;
503 if (sa
->sa_handler
== SIG_DFL
)
504 p
->p_sigcatch
&= ~bit
;
506 p
->p_sigcatch
|= bit
;
512 * Initialize signal state for process 0;
513 * set to ignore signals that are ignored by default.
521 for (i
= 0; i
< NSIG
; i
++)
522 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
523 p
->p_sigignore
|= sigmask(i
);
527 * Reset signals for an exec of the specified process.
531 register struct proc
*p
;
532 register thread_act_t thr_act
;
534 register struct sigacts
*ps
= p
->p_sigacts
;
535 register int nc
, mask
;
539 * Reset caught signals. Held signals remain held
540 * through p_sigmask (unless they were caught,
541 * and are now ignored by default).
543 while (p
->p_sigcatch
) {
544 nc
= ffs((long)p
->p_sigcatch
);
546 p
->p_sigcatch
&= ~mask
;
547 if (sigprop
[nc
] & SA_IGNORE
) {
549 p
->p_sigignore
|= mask
;
551 ut
= (struct uthread
*)get_bsdthread_info(thr_act
);
552 ut
->uu_siglist
&= ~mask
;
553 p
->p_siglist
&= ~mask
;
555 clear_procsiglist(p
, mask
);
557 ps
->ps_sigact
[nc
] = SIG_DFL
;
560 * Reset stack state to the user stack.
561 * Clear set of signals caught on the signal stack.
563 ps
->ps_sigstk
.ss_flags
= SA_DISABLE
;
564 ps
->ps_sigstk
.ss_size
= 0;
565 ps
->ps_sigstk
.ss_sp
= 0;
570 * Manipulate signal mask.
571 * Note that we receive new mask, not pointer,
572 * and return old mask as return value;
573 * the library stub does the rest.
575 struct sigprocmask_args
{
581 sigprocmask(p
, uap
, retval
)
582 register struct proc
*p
;
583 struct sigprocmask_args
*uap
;
587 sigset_t oldmask
, nmask
;
588 sigset_t
* omask
= uap
->omask
;
591 ut
= (struct uthread
*)get_bsdthread_info(current_act());
592 oldmask
= ut
->uu_sigmask
;
594 if (uap
->mask
== (sigset_t
*)0) {
595 /* just want old mask */
598 error
= copyin((caddr_t
)uap
->mask
, &nmask
, sizeof(sigset_t
));
604 block_procsigmask(p
, (nmask
& ~sigcantmask
));
605 signal_setast(current_act());
609 unblock_procsigmask(p
, (nmask
& ~sigcantmask
));
610 signal_setast(current_act());
614 set_procsigmask(p
, (nmask
& ~sigcantmask
));
615 signal_setast(current_act());
624 copyout(&oldmask
, omask
, sizeof(sigset_t
));
628 struct sigpending_args
{
632 sigpending(p
, uap
, retval
)
634 register struct sigpending_args
*uap
;
640 ut
= (struct uthread
*)get_bsdthread_info(current_act());
641 pendlist
= ut
->uu_siglist
;
644 copyout(&pendlist
, uap
->osv
, sizeof(sigset_t
));
650 * Generalized interface signal handler, 4.3-compatible.
652 struct osigvec_args
{
659 osigvec(p
, uap
, retval
)
661 register struct osigvec_args
*uap
;
666 register struct sigacts
*ps
= p
->p_sigacts
;
667 register struct sigvec
*sv
;
672 signum
= uap
->signum
;
673 if (signum
<= 0 || signum
>= NSIG
||
674 signum
== SIGKILL
|| signum
== SIGSTOP
)
678 *(sig_t
*)&sv
->sv_handler
= ps
->ps_sigact
[signum
];
679 sv
->sv_mask
= ps
->ps_catchmask
[signum
];
680 bit
= sigmask(signum
);
682 if ((ps
->ps_sigonstack
& bit
) != 0)
683 sv
->sv_flags
|= SV_ONSTACK
;
684 if ((ps
->ps_sigintr
& bit
) != 0)
685 sv
->sv_flags
|= SV_INTERRUPT
;
686 if (p
->p_flag
& P_NOCLDSTOP
)
687 sv
->sv_flags
|= SA_NOCLDSTOP
;
688 if (error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->osv
,
693 if (error
= copyin((caddr_t
)uap
->nsv
, (caddr_t
)sv
,
696 sv
->sv_flags
^= SA_RESTART
; /* opposite of SV_INTERRUPT */
697 error
= setsigvec(p
, signum
, (struct sigaction
*)sv
);
705 struct osigblock_args
{
709 osigblock(p
, uap
, retval
)
710 register struct proc
*p
;
711 struct osigblock_args
*uap
;
714 struct uthread
* uth
= get_bsdthread_info(current_act());
716 *retval
= uth
->uu_sigmask
;
717 uth
->uu_sigmask
|= (uap
->mask
& ~sigcantmask
);
721 struct osigsetmask_args
{
725 osigsetmask(p
, uap
, retval
)
727 struct osigsetmask_args
*uap
;
730 struct uthread
* uth
= get_bsdthread_info(current_act());
732 *retval
= uth
->uu_sigmask
;
733 uth
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
736 #endif /* COMPAT_43 */
739 * Suspend process until signal, providing mask to be set
740 * in the meantime. Note nonstandard calling convention:
741 * libc stub passes mask, not pointer, to save a copyin.
747 struct uthread
*ut
= get_bsdthread_info(current_act());
748 unix_syscall_return(EINTR
);
751 struct sigsuspend_args
{
757 sigsuspend(p
, uap
, retval
)
758 register struct proc
*p
;
759 struct sigsuspend_args
*uap
;
762 register struct sigacts
*ps
= p
->p_sigacts
;
765 ut
= (struct uthread
*)get_bsdthread_info(current_act());
768 * When returning from sigpause, we want
769 * the old mask to be restored after the
770 * signal handler has finished. Thus, we
771 * save it here and mark the sigacts structure
774 ut
->uu_oldmask
= ut
->uu_sigmask
;
775 ut
->uu_flag
|= USAS_OLDMASK
;
776 ut
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
777 (void) tsleep0((caddr_t
) p
, PPAUSE
|PCATCH
, "pause", 0, sigcontinue
);
778 /* always return EINTR rather than ERESTART... */
782 struct __disable_thsignal_args
{
787 __disable_threadsignal(p
, uap
, retval
)
789 register struct __disable_thsignal_args
*uap
;
794 uth
= (struct uthread
*)get_bsdthread_info(current_act());
796 /* No longer valid to have any signal delivered */
798 uth
->uu_flag
|= UNO_SIGMASK
;
805 struct pthread_kill_args
{
811 __pthread_kill(p
, uap
, retval
)
813 register struct pthread_kill_args
*uap
;
816 thread_act_t target_act
;
818 int signum
= uap
->sig
;
821 target_act
= (thread_act_t
)port_name_to_act(uap
->thread_port
);
823 if (target_act
== THR_ACT_NULL
)
825 if ((u_int
)signum
>= NSIG
) {
830 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
832 if (uth
->uu_flag
& UNO_SIGMASK
) {
838 psignal_uthread(target_act
, signum
);
840 act_deallocate(target_act
);
845 struct pthread_sigmask_args
{
851 pthread_sigmask(p
, uap
, retval
)
852 register struct proc
*p
;
853 register struct pthread_sigmask_args
*uap
;
857 const sigset_t
*set
= uap
->set
;
858 sigset_t
* oset
= uap
->oset
;
864 ut
= (struct uthread
*)get_bsdthread_info(current_act());
865 oldset
= ut
->uu_sigmask
;
867 if (set
== (sigset_t
*) 0) {
868 /* need only old mask */
872 error
= copyin((caddr_t
)set
, (caddr_t
)&nset
, sizeof(sigset_t
));
878 ut
->uu_sigmask
|= (nset
& ~sigcantmask
);
882 ut
->uu_sigmask
&= ~(nset
);
883 signal_setast(current_act());
887 ut
->uu_sigmask
= (nset
& ~sigcantmask
);
888 signal_setast(current_act());
897 copyout((caddr_t
)&oldset
, (caddr_t
)oset
, sizeof(sigset_t
));
903 struct sigwait_args
{
909 sigwait(p
, uap
, retval
)
910 register struct proc
*p
;
911 register struct sigwait_args
*uap
;
914 register struct sigacts
*ps
= p
->p_sigacts
;
924 ut
= (struct uthread
*)get_bsdthread_info(current_act());
926 if (uap
->set
== (const sigset_t
*)0)
929 error
= copyin((caddr_t
)uap
->set
, (caddr_t
)&mask
, sizeof(sigset_t
));
933 siglist
= (mask
& ~sigcantmask
);
939 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
943 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
944 if (sigw
= uth
->uu_siglist
& siglist
) {
951 /* The signal was pending on a thread */
955 * When returning from sigwait, we want
956 * the old mask to be restored after the
957 * signal handler has finished. Thus, we
958 * save it here and mark the sigacts structure
961 ut
->uu_oldmask
= ut
->uu_sigmask
;
962 ut
->uu_flag
|= USAS_OLDMASK
;
963 if (siglist
== (sigset_t
)0)
965 /* SIGKILL and SIGSTOP are not maskable as well */
966 ut
->uu_sigmask
= ~(siglist
|sigcantmask
);
967 ut
->uu_sigwait
= siglist
;
968 /* No Continuations for now */
969 error
= tsleep((caddr_t
)&ut
->uu_sigwait
, PPAUSE
|PCATCH
, "pause", 0);
971 if ((error
== EINTR
) || (error
== ERESTART
))
974 sigw
= (ut
->uu_sigwait
& siglist
);
975 ut
->uu_sigmask
= ut
->uu_oldmask
;
977 ut
->uu_flag
&= ~USAS_OLDMASK
;
981 signum
= ffs((unsigned int)sigw
);
983 panic("sigwait with no signal wakeup");
984 ut
->uu_siglist
&= ~(sigmask(signum
));
986 error
= copyout(&signum
, uap
->sig
, sizeof(int));
994 struct osigstack_args
{
995 struct sigstack
*nss
;
996 struct sigstack
*oss
;
1001 osigstack(p
, uap
, retval
)
1003 register struct osigstack_args
*uap
;
1007 struct sigacts
*psp
;
1011 ss
.ss_sp
= psp
->ps_sigstk
.ss_sp
;
1012 ss
.ss_onstack
= psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
;
1013 if (uap
->oss
&& (error
= copyout((caddr_t
)&ss
,
1014 (caddr_t
)uap
->oss
, sizeof (struct sigstack
))))
1016 if (uap
->nss
&& (error
= copyin((caddr_t
)uap
->nss
,
1017 (caddr_t
)&ss
, sizeof (ss
))) == 0) {
1018 psp
->ps_sigstk
.ss_sp
= ss
.ss_sp
;
1019 psp
->ps_sigstk
.ss_size
= 0;
1020 psp
->ps_sigstk
.ss_flags
|= ss
.ss_onstack
& SA_ONSTACK
;
1021 psp
->ps_flags
|= SAS_ALTSTACK
;
1025 #endif /* COMPAT_43 */
1027 struct sigaltstack_args
{
1028 struct sigaltstack
*nss
;
1029 struct sigaltstack
*oss
;
1033 sigaltstack(p
, uap
, retval
)
1035 register struct sigaltstack_args
*uap
;
1038 struct sigacts
*psp
;
1039 struct sigaltstack ss
;
1043 if ((psp
->ps_flags
& SAS_ALTSTACK
) == 0)
1044 psp
->ps_sigstk
.ss_flags
|= SA_DISABLE
;
1045 if (uap
->oss
&& (error
= copyout((caddr_t
)&psp
->ps_sigstk
,
1046 (caddr_t
)uap
->oss
, sizeof (struct sigaltstack
))))
1050 if (error
= copyin((caddr_t
)uap
->nss
, (caddr_t
)&ss
,
1053 if ((ss
.ss_flags
& ~SA_DISABLE
) != 0) {
1057 if (ss
.ss_flags
& SA_DISABLE
) {
1058 if (psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
)
1060 psp
->ps_flags
&= ~SAS_ALTSTACK
;
1061 psp
->ps_sigstk
.ss_flags
= ss
.ss_flags
;
1064 /* The older stacksize was 8K, enforce that one so no compat problems */
1065 #define OLDMINSIGSTKSZ 8*1024
1066 if (ss
.ss_size
< OLDMINSIGSTKSZ
)
1068 psp
->ps_flags
|= SAS_ALTSTACK
;
1079 kill(cp
, uap
, retval
)
1080 register struct proc
*cp
;
1081 register struct kill_args
*uap
;
1084 register struct proc
*p
;
1085 register struct pcred
*pc
= cp
->p_cred
;
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 if (!cansignal(cp
, pc
, p
, uap
->signum
))
1104 psignal(p
, uap
->signum
);
1108 case -1: /* broadcast signal */
1109 return (killpg1(cp
, uap
->signum
, 0, 1));
1110 case 0: /* signal own process group */
1111 return (killpg1(cp
, uap
->signum
, 0, 0));
1112 default: /* negative explicit process group */
1113 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0));
1119 struct okillpg_args
{
1125 okillpg(p
, uap
, retval
)
1127 register struct okillpg_args
*uap
;
1131 if ((u_int
)uap
->signum
>= NSIG
)
1133 return (killpg1(p
, uap
->signum
, uap
->pgid
, 0));
1135 #endif /* COMPAT_43 */
1138 * Common code for kill process group/broadcast kill.
1139 * cp is calling process.
1142 killpg1(cp
, signum
, pgid
, all
)
1143 register struct proc
*cp
;
1144 int signum
, pgid
, all
;
1146 register struct proc
*p
;
1147 register struct pcred
*pc
= cp
->p_cred
;
1155 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1156 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1157 p
== cp
|| !cansignal(cp
, pc
, p
, signum
))
1166 * zero pgid means send to my process group.
1170 pgrp
= pgfind(pgid
);
1174 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1175 p
= p
->p_pglist
.le_next
) {
1176 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1177 p
->p_stat
== SZOMB
||
1178 !cansignal(cp
, pc
, p
, signum
))
1185 return (nfound
? 0 : ESRCH
);
1189 * Send a signal to a process group.
1192 gsignal(pgid
, signum
)
1197 if (pgid
&& (pgrp
= pgfind(pgid
)))
1198 pgsignal(pgrp
, signum
, 0);
1202 * Send a signal to a process group. If checktty is 1,
1203 * limit to members which have a controlling terminal.
1206 pgsignal(pgrp
, signum
, checkctty
)
1208 int signum
, checkctty
;
1210 register struct proc
*p
;
1213 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1214 p
= p
->p_pglist
.le_next
)
1215 if (checkctty
== 0 || p
->p_flag
& P_CONTROLT
)
1220 * Send signal to a backgrounded process blocked due to tty access
1221 * In FreeBSD, the backgrounded process wakes up every second and
1222 * discovers whether it is foregounded or not. In our case, we block
1223 * the thread in tsleep as we want to avoid storm of processes as well
1224 * as the suspend is only at AST level
1227 tty_pgsignal(pgrp
, signum
)
1231 register struct proc
*p
;
1234 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1235 p
= p
->p_pglist
.le_next
)
1236 if ((p
->p_flag
& P_TTYSLEEP
) && (p
->p_flag
& P_CONTROLT
))
1241 * Send a signal caused by a trap to a specific thread.
1244 threadsignal(sig_actthread
, signum
, code
)
1245 register thread_act_t sig_actthread
;
1246 register int signum
;
1249 register struct uthread
*uth
;
1250 register struct task
* sig_task
;
1251 register struct proc
*p
;
1254 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1257 mask
= sigmask(signum
);
1258 if ((mask
& threadmask
) == 0)
1260 sig_task
= get_threadtask(sig_actthread
);
1261 p
= (struct proc
*)(get_bsdtask_info(sig_task
));
1263 uth
= get_bsdthread_info(sig_actthread
);
1264 if (uth
&& (uth
->uu_flag
& P_VFORK
))
1267 if (!(p
->p_flag
& P_TRACED
) && (p
->p_sigignore
& mask
))
1270 uth
->uu_siglist
|= mask
;
1271 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1272 uth
->uu_code
= code
;
1273 /* mark on process as well */
1274 signal_setast(sig_actthread
);
1280 register struct proc
*p
;
1281 register int signum
;
1283 psignal_lock(p
, signum
, 1);
1287 psignal_vfork(p
, new_task
, thr_act
, signum
)
1288 register struct proc
*p
;
1290 thread_act_t thr_act
;
1291 register int signum
;
1295 register int s
, prop
;
1296 register sig_t action
;
1299 struct uthread
*uth
;
1301 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1302 panic("psignal signal number");
1303 mask
= sigmask(signum
);
1304 prop
= sigprop
[signum
];
1307 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1310 #endif /* SIGNAL_DEBUG */
1312 if ((new_task
== TASK_NULL
) || (thr_act
== (thread_act_t
)NULL
) || is_kerneltask(new_task
))
1316 uth
= get_bsdthread_info(thr_act
);
1320 * proc is traced, always give parent a chance.
1324 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1325 (p
->p_flag
& P_TRACED
) == 0)
1328 if (prop
& SA_CONT
) {
1329 p
->p_siglist
&= ~stopsigmask
;
1330 uth
->uu_siglist
&= ~stopsigmask
;
1333 if (prop
& SA_STOP
) {
1335 * If sending a tty stop signal to a member of an orphaned
1336 * process group, discard the signal here if the action
1337 * is default; don't stop the process below if sleeping,
1338 * and don't clear any pending SIGCONT.
1340 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1343 uth
->uu_siglist
&= ~contsigmask
;
1344 p
->p_siglist
&= ~contsigmask
;
1346 uth
->uu_siglist
|= mask
;
1347 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1349 /* Deliver signal to the activation passed in */
1350 act_set_astbsd(thr_act
);
1353 * SIGKILL priority twiddling moved here from above because
1354 * it needs sig_thread. Could merge it into large switch
1355 * below if we didn't care about priority for tracing
1356 * as SIGKILL's action is always SIG_DFL.
1358 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1363 * This Process is traced - wake it up (if not already
1364 * stopped) so that it can discover the signal in
1365 * issig() and stop for the parent.
1367 if (p
->p_flag
& P_TRACED
) {
1368 if (p
->p_stat
!= SSTOP
)
1375 * If we're being traced (possibly because someone attached us
1376 * while we were stopped), check for a signal from the debugger.
1378 if (p
->p_stat
== SSTOP
) {
1379 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
1380 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1381 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1386 * setrunnable(p) in BSD
1395 get_signalthread(struct proc
*p
, int signum
)
1397 struct uthread
*uth
;
1398 thread_act_t thr_act
;
1399 sigset_t mask
= sigmask(signum
);
1400 thread_act_t sig_thread_act
;
1401 struct task
* sig_task
= p
->task
;
1404 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
1405 sig_thread_act
= p
->p_vforkact
;
1406 kret
= check_actforsig(sig_task
, sig_thread_act
, 1);
1407 if (kret
== KERN_SUCCESS
)
1408 return(sig_thread_act
);
1410 return(THR_ACT_NULL
);
1413 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1414 if(((uth
->uu_flag
& UNO_SIGMASK
)== 0) &&
1415 (((uth
->uu_sigmask
& mask
) == 0) || (uth
->uu_sigwait
& mask
))) {
1416 if (check_actforsig(p
->task
, uth
->uu_act
, 1) == KERN_SUCCESS
)
1417 return(uth
->uu_act
);
1420 if (get_signalact(p
->task
, &thr_act
, 1) == KERN_SUCCESS
) {
1424 return(THR_ACT_NULL
);
1428 * Send the signal to the process. If the signal has an action, the action
1429 * is usually performed by the target process rather than the caller; we add
1430 * the signal to the set of pending signals for the process.
1433 * o When a stop signal is sent to a sleeping process that takes the
1434 * default action, the process is stopped without awakening it.
1435 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1436 * regardless of the signal action (eg, blocked or ignored).
1438 * Other ignored signals are discarded immediately.
1441 psignal_lock(p
, signum
, withlock
)
1442 register struct proc
*p
;
1443 register int signum
;
1444 register int withlock
;
1446 register int s
, prop
;
1447 register sig_t action
;
1448 thread_act_t sig_thread_act
;
1449 register task_t sig_task
;
1451 struct uthread
*uth
;
1455 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1456 panic("psignal signal number");
1457 mask
= sigmask(signum
);
1458 prop
= sigprop
[signum
];
1461 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1464 #endif /* SIGNAL_DEBUG */
1466 if (thread_funnel_get() == (funnel_t
*)network_flock
) {
1468 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1471 * We will need the task pointer later. Grab it now to
1472 * check for a zombie process. Also don't send signals
1473 * to kernel internal tasks.
1475 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1477 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1482 KNOTE(&p
->p_klist
, NOTE_SIGNAL
| signum
);
1486 * do not send signals to the process that has the thread
1487 * doing a reboot(). Not doing so will mark that thread aborted
1488 * and can cause IO failures wich will cause data loss.
1490 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1492 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1500 * Deliver the signal to the first thread in the task. This
1501 * allows single threaded applications which use signals to
1502 * be able to be linked with multithreaded libraries. We have
1503 * an implicit reference to the current thread, but need
1504 * an explicit one otherwise. The thread reference keeps
1505 * the corresponding task data structures around too. This
1506 * reference is released by thread_deallocate.
1509 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1512 /* If successful return with ast set */
1513 sig_thread_act
= get_signalthread(p
, signum
);
1515 if (sig_thread_act
== THR_ACT_NULL
) {
1517 /* if it is sigkill, may be we should
1518 * inject a thread to terminate
1522 #endif /* SIGNAL_DEBUG */
1526 uth
= get_bsdthread_info(sig_thread_act
);
1529 * If proc is traced, always give parent a chance.
1531 if (p
->p_flag
& P_TRACED
)
1535 * If the signal is being ignored,
1536 * then we forget about it immediately.
1537 * (Note: we don't set SIGCONT in p_sigignore,
1538 * and if it is set to SIG_IGN,
1539 * action will be SIG_DFL here.)
1541 if (p
->p_sigignore
& mask
)
1543 if (uth
->uu_sigwait
& mask
)
1545 if (uth
->uu_sigmask
& mask
)
1547 else if (p
->p_sigcatch
& mask
)
1553 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1554 (p
->p_flag
& P_TRACED
) == 0)
1557 if (prop
& SA_CONT
) {
1558 uth
->uu_siglist
&= ~stopsigmask
;
1559 p
->p_siglist
&= ~stopsigmask
;
1562 if (prop
& SA_STOP
) {
1564 * If sending a tty stop signal to a member of an orphaned
1565 * process group, discard the signal here if the action
1566 * is default; don't stop the process below if sleeping,
1567 * and don't clear any pending SIGCONT.
1569 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1572 uth
->uu_siglist
&= ~contsigmask
;
1573 p
->p_siglist
&= ~contsigmask
;
1575 uth
->uu_siglist
|= mask
;
1576 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1580 * Defer further processing for signals which are held,
1581 * except that stopped processes must be continued by SIGCONT.
1583 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
)) {
1587 * SIGKILL priority twiddling moved here from above because
1588 * it needs sig_thread. Could merge it into large switch
1589 * below if we didn't care about priority for tracing
1590 * as SIGKILL's action is always SIG_DFL.
1592 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1597 * Process is traced - wake it up (if not already
1598 * stopped) so that it can discover the signal in
1599 * issig() and stop for the parent.
1601 if (p
->p_flag
& P_TRACED
) {
1602 if (p
->p_stat
!= SSTOP
)
1608 if (action
== SIG_WAIT
) {
1609 uth
->uu_sigwait
= mask
;
1610 uth
->uu_siglist
&= ~mask
;
1611 p
->p_siglist
&= ~mask
;
1612 wakeup(&uth
->uu_sigwait
);
1613 /* if it is SIGCONT resume whole process */
1615 (void) task_resume(sig_task
);
1619 if (action
!= SIG_DFL
) {
1621 * User wants to catch the signal.
1622 * Wake up the thread, but don't un-suspend it
1623 * (except for SIGCONT).
1625 if (prop
& SA_CONT
) {
1626 if (p
->p_flag
& P_TTYSLEEP
) {
1627 p
->p_flag
&= ~P_TTYSLEEP
;
1628 wakeup(&p
->p_siglist
);
1630 (void) task_resume(sig_task
);
1636 /* Default action - varies */
1637 if (mask
& stopsigmask
) {
1639 * These are the signals which by default
1642 * Don't clog system with children of init
1643 * stopped from the keyboard.
1645 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1646 psignal_lock(p
, SIGKILL
, 0);
1647 uth
->uu_siglist
&= ~mask
;
1648 p
->p_siglist
&= ~mask
;
1654 * if task hasn't already been stopped by
1657 uth
->uu_siglist
&= ~mask
;
1658 p
->p_siglist
&= ~mask
;
1659 if (p
->p_stat
!= SSTOP
) {
1660 p
->p_xstat
= signum
;
1662 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1663 struct proc
*pp
= p
->p_pptr
;
1665 pp
->si_pid
= p
->p_pid
;
1666 pp
->si_status
= p
->p_xstat
;
1667 pp
->si_code
= CLD_STOPPED
;
1668 pp
->si_uid
= p
->p_cred
->p_ruid
;
1669 psignal(pp
, SIGCHLD
);
1677 * Signals ignored by default have been dealt
1678 * with already, since their bits are on in
1684 * Kill signal always sets process running and
1688 * Process will be running after 'run'
1692 thread_abort(sig_thread_act
);
1698 * Let the process run. If it's sleeping on an
1699 * event, it remains so.
1701 if (p
->p_flag
& P_TTYSLEEP
) {
1702 p
->p_flag
&= ~P_TTYSLEEP
;
1703 wakeup(&p
->p_siglist
);
1705 (void) task_resume(sig_task
);
1707 uth
->uu_siglist
&= ~mask
;
1708 p
->p_siglist
&= ~mask
;
1715 * All other signals wake up the process, but don't
1724 * If we're being traced (possibly because someone attached us
1725 * while we were stopped), check for a signal from the debugger.
1727 if (p
->p_stat
== SSTOP
) {
1728 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
1729 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1732 * setrunnable(p) in BSD and
1733 * Wake up the thread if it is interruptible.
1736 thread_abort_safely(sig_thread_act
);
1742 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1746 /* psignal_lock(p, signum, withlock ) */
1748 psignal_uthread(thr_act
, signum
)
1749 thread_act_t thr_act
;
1753 register int s
, prop
;
1754 register sig_t action
;
1755 thread_act_t sig_thread_act
;
1756 register task_t sig_task
;
1758 struct uthread
*uth
;
1762 p
= (struct proc
*)get_bsdtask_info(get_threadtask(thr_act
));
1763 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1764 panic("Invalid signal number in psignal_uthread");
1765 mask
= sigmask(signum
);
1766 prop
= sigprop
[signum
];
1769 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1772 #endif /* SIGNAL_DEBUG */
1775 * We will need the task pointer later. Grab it now to
1776 * check for a zombie process. Also don't send signals
1777 * to kernel internal tasks.
1779 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1783 sig_thread_act
= thr_act
;
1785 * do not send signals to the process that has the thread
1786 * doing a reboot(). Not doing so will mark that thread aborted
1787 * and can cause IO failures wich will cause data loss.
1789 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1796 * Deliver the signal to the first thread in the task. This
1797 * allows single threaded applications which use signals to
1798 * be able to be linked with multithreaded libraries. We have
1799 * an implicit reference to the current thread, but need
1800 * an explicit one otherwise. The thread reference keeps
1801 * the corresponding task data structures around too. This
1802 * reference is released by thread_deallocate.
1805 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1808 kret
= check_actforsig(sig_task
, sig_thread_act
, 1);
1810 if (kret
!= KERN_SUCCESS
) {
1816 uth
= get_bsdthread_info(sig_thread_act
);
1819 * If proc is traced, always give parent a chance.
1821 if (p
->p_flag
& P_TRACED
)
1825 * If the signal is being ignored,
1826 * then we forget about it immediately.
1827 * (Note: we don't set SIGCONT in p_sigignore,
1828 * and if it is set to SIG_IGN,
1829 * action will be SIG_DFL here.)
1831 if (p
->p_sigignore
& mask
)
1833 if (uth
->uu_sigwait
& mask
)
1835 if (uth
->uu_sigmask
& mask
)
1837 else if (p
->p_sigcatch
& mask
)
1843 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1844 (p
->p_flag
& P_TRACED
) == 0)
1847 if (prop
& SA_CONT
) {
1848 uth
->uu_siglist
&= ~stopsigmask
;
1849 p
->p_siglist
&= ~stopsigmask
;
1852 if (prop
& SA_STOP
) {
1854 * If sending a tty stop signal to a member of an orphaned
1855 * process group, discard the signal here if the action
1856 * is default; don't stop the process below if sleeping,
1857 * and don't clear any pending SIGCONT.
1859 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1862 uth
->uu_siglist
&= ~contsigmask
;
1863 p
->p_siglist
&= ~contsigmask
;
1865 uth
->uu_siglist
|= mask
;
1866 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1869 * Defer further processing for signals which are held,
1870 * except that stopped processes must be continued by SIGCONT.
1872 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
))
1876 * SIGKILL priority twiddling moved here from above because
1877 * it needs sig_thread. Could merge it into large switch
1878 * below if we didn't care about priority for tracing
1879 * as SIGKILL's action is always SIG_DFL.
1881 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1886 * Process is traced - wake it up (if not already
1887 * stopped) so that it can discover the signal in
1888 * issig() and stop for the parent.
1890 if (p
->p_flag
& P_TRACED
) {
1891 if (p
->p_stat
!= SSTOP
)
1897 if (action
== SIG_WAIT
) {
1898 uth
->uu_sigwait
= mask
;
1899 uth
->uu_siglist
&= ~mask
;
1900 p
->p_siglist
&= ~mask
;
1901 wakeup(&uth
->uu_sigwait
);
1902 /* if it is SIGCONT resume whole process */
1904 (void) task_resume(sig_task
);
1908 if (action
!= SIG_DFL
) {
1910 * User wants to catch the signal.
1911 * Wake up the thread, but don't un-suspend it
1912 * (except for SIGCONT).
1915 (void) task_resume(sig_task
);
1918 /* Default action - varies */
1919 if (mask
& stopsigmask
) {
1921 * These are the signals which by default
1924 * Don't clog system with children of init
1925 * stopped from the keyboard.
1927 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1928 psignal_lock(p
, SIGKILL
, 0);
1929 uth
->uu_siglist
&= ~mask
;
1930 p
->p_siglist
&= ~mask
;
1936 * if task hasn't already been stopped by
1939 uth
->uu_siglist
&= ~mask
;
1940 p
->p_siglist
&= ~mask
;
1941 if (p
->p_stat
!= SSTOP
) {
1942 p
->p_xstat
= signum
;
1943 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1944 struct proc
*pp
= p
->p_pptr
;
1946 pp
->si_pid
= p
->p_pid
;
1947 pp
->si_status
= p
->p_xstat
;
1948 pp
->si_code
= CLD_STOPPED
;
1949 pp
->si_uid
= p
->p_cred
->p_ruid
;
1950 psignal(pp
, SIGCHLD
);
1959 * Signals ignored by default have been dealt
1960 * with already, since their bits are on in
1966 * Kill signal always sets process running and
1970 * Process will be running after 'run'
1974 thread_abort(sig_thread_act
);
1980 * Let the process run. If it's sleeping on an
1981 * event, it remains so.
1983 if (p
->p_flag
& P_TTYSLEEP
) {
1984 p
->p_flag
&= ~P_TTYSLEEP
;
1985 wakeup(&p
->p_siglist
);
1987 (void) task_resume(sig_task
);
1989 uth
->uu_siglist
&= ~mask
;
1990 p
->p_siglist
&= ~mask
;
1996 * All other signals wake up the process, but don't
2005 * If we're being traced (possibly because someone attached us
2006 * while we were stopped), check for a signal from the debugger.
2008 if (p
->p_stat
== SSTOP
) {
2009 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
2010 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
2011 p
->p_siglist
|= sigmask(p
->p_xstat
);
2015 * setrunnable(p) in BSD and
2016 * Wake up the thread if it is interruptible.
2019 thread_abort_safely(sig_thread_act
);
2031 thread_t self
= current_act();
2033 p
->exit_thread
= self
;
2034 (void) task_suspend(p
->task
);
2041 thread_t self
= current_act();
2043 while (p
->sigwait
|| p
->exit_thread
) {
2044 if (p
->exit_thread
) {
2045 if (p
->exit_thread
!= self
) {
2047 * Already exiting - no signals.
2053 if(assert_wait_possible()) {
2054 assert_wait((caddr_t
)&p
->sigwait_thread
,
2055 (THREAD_INTERRUPTIBLE
));
2058 thread_block(THREAD_CONTINUE_NULL
);
2060 if (thread_should_abort(self
)) {
2062 * Terminate request - clean up.
2071 * If the current process has received a signal (should be caught or cause
2072 * termination, should interrupt current syscall), return the signal number.
2073 * Stop signals with default action are processed immediately, then cleared;
2074 * they aren't returned. This is checked after each entry to the system for
2075 * a syscall or trap (though this can usually be done without calling issignal
2076 * by checking the pending signal masks in the CURSIG macro.) The normal call
2079 * while (signum = CURSIG(curproc))
2084 register struct proc
*p
;
2086 register int signum
, mask
, prop
, sigbits
;
2087 task_t task
= p
->task
;
2088 thread_act_t cur_act
;
2090 struct uthread
* ut
;
2094 cur_act
= current_act();
2097 if(rdebug_proc
&& (p
== rdebug_proc
)) {
2100 #endif /* SIGNAL_DEBUG */
2104 * Try to grab the signal lock.
2106 if (sig_try_locked(p
) <= 0) {
2111 ut
= get_bsdthread_info(cur_act
);
2113 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2115 if (p
->p_flag
& P_PPWAIT
)
2116 sigbits
&= ~stopsigmask
;
2117 if (sigbits
== 0) { /* no signal to send */
2121 signum
= ffs((long)sigbits
);
2122 mask
= sigmask(signum
);
2123 prop
= sigprop
[signum
];
2126 * We should see pending but ignored signals
2127 * only if P_TRACED was on when they were posted.
2129 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2130 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2131 p
->p_siglist
&= ~mask
; /* take the signal! */
2134 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2136 register task_t task
;
2138 * If traced, always stop, and stay
2139 * stopped until released by the debugger.
2141 /* ptrace debugging */
2142 p
->p_xstat
= signum
;
2144 if (p
->p_flag
& P_SIGEXC
) {
2146 p
->sigwait_thread
= cur_act
;
2148 p
->p_flag
&= ~P_WAITED
;
2149 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2150 p
->p_siglist
&= ~mask
; /* clear the old signal */
2152 do_bsdexception(EXC_SOFTWARE
, EXC_SOFT_SIGNAL
, signum
);
2155 // panic("Unsupportef gdb option \n");;
2156 pp
->si_pid
= p
->p_pid
;
2157 pp
->si_status
= p
->p_xstat
;
2158 pp
->si_code
= CLD_TRAPPED
;
2159 pp
->si_uid
= p
->p_cred
->p_ruid
;
2160 psignal(pp
, SIGCHLD
);
2162 * XXX Have to really stop for debuggers;
2163 * XXX stop() doesn't do the right thing.
2164 * XXX Inline the task_suspend because we
2165 * XXX have to diddle Unix state in the
2171 p
->sigwait_thread
= cur_act
;
2173 p
->p_flag
&= ~P_WAITED
;
2174 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2175 p
->p_siglist
&= ~mask
; /* clear the old signal */
2177 wakeup((caddr_t
)p
->p_pptr
);
2179 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
2180 thread_block(THREAD_CONTINUE_NULL
);
2185 p
->sigwait_thread
= NULL
;
2186 wakeup((caddr_t
)&p
->sigwait_thread
);
2189 * This code is to detect when gdb is killed
2190 * even as the traced program is attached.
2191 * pgsignal would get the SIGKILL to traced program
2192 * That's what we are trying to see (I hope)
2194 if (ut
->uu_siglist
& sigmask(SIGKILL
)) {
2196 * Wait event may still be outstanding;
2197 * clear it, since sig_lock_to_exit will
2200 clear_wait(current_act(), THREAD_INTERRUPTED
);
2201 sig_lock_to_exit(p
);
2203 * Since this thread will be resumed
2204 * to allow the current syscall to
2205 * be completed, must save u_qsave
2206 * before calling exit(). (Since exit()
2207 * calls closef() which can trash u_qsave.)
2210 exit1(p
,signum
, (int *)NULL
);
2215 * We may have to quit
2217 if (thread_should_abort(current_act())) {
2222 * If parent wants us to take the signal,
2223 * then it will leave it in p->p_xstat;
2224 * otherwise we just look for signals again.
2226 signum
= p
->p_xstat
;
2230 * Put the new signal into p_siglist. If the
2231 * signal is being masked, look for other signals.
2233 mask
= sigmask(signum
);
2234 ut
->uu_siglist
|= mask
;
2235 p
->p_siglist
|= mask
; /* just for lame ones looking here */
2236 if (ut
->uu_sigmask
& mask
)
2241 * Decide whether the signal should be returned.
2242 * Return the signal's number, or fall through
2243 * to clear it from the pending mask.
2246 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2250 * Don't take default actions on system processes.
2252 if (p
->p_pptr
->p_pid
== 0) {
2255 * Are you sure you want to ignore SIGSEGV
2258 printf("Process (pid %d) got signal %d\n",
2261 break; /* == ignore */
2265 * If there is a pending stop signal to process
2266 * with default action, stop here,
2267 * then clear the signal. However,
2268 * if process is member of an orphaned
2269 * process group, ignore tty stop signals.
2271 if (prop
& SA_STOP
) {
2272 if (p
->p_flag
& P_TRACED
||
2273 (p
->p_pgrp
->pg_jobc
== 0 &&
2275 break; /* == ignore */
2276 if (p
->p_stat
!= SSTOP
) {
2277 p
->p_xstat
= signum
;
2279 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
2281 pp
->si_pid
= p
->p_pid
;
2282 pp
->si_status
= p
->p_xstat
;
2283 pp
->si_code
= CLD_STOPPED
;
2284 pp
->si_uid
= p
->p_cred
->p_ruid
;
2285 psignal(pp
, SIGCHLD
);
2289 } else if (prop
& SA_IGNORE
) {
2291 * Except for SIGCONT, shouldn't get here.
2292 * Default action is to ignore; drop it.
2294 break; /* == ignore */
2296 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2297 p
->p_siglist
&= ~mask
; /* take the signal! */
2305 * Masking above should prevent us ever trying
2306 * to take action on an ignored signal other
2307 * than SIGCONT, unless process is traced.
2309 if ((prop
& SA_CONT
) == 0 &&
2310 (p
->p_flag
& P_TRACED
) == 0)
2311 printf("issignal\n");
2312 break; /* == ignore */
2316 * This signal has an action, let
2317 * postsig() process it.
2319 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2320 p
->p_siglist
&= ~mask
; /* take the signal! */
2324 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2325 p
->p_siglist
&= ~mask
; /* take the signal! */
2330 /* called from _sleep */
2333 register struct proc
*p
;
2335 register int signum
, mask
, prop
, sigbits
;
2336 task_t task
= p
->task
;
2337 thread_act_t cur_act
;
2339 struct uthread
* ut
;
2343 cur_act
= current_act();
2345 ut
= get_bsdthread_info(cur_act
);
2347 if (ut
->uu_siglist
== 0)
2350 if (((ut
->uu_siglist
& ~ut
->uu_sigmask
) == 0) && ((p
->p_flag
& P_TRACED
) == 0))
2353 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2356 if (p
->p_flag
& P_PPWAIT
)
2357 sigbits
&= ~stopsigmask
;
2358 if (sigbits
== 0) { /* no signal to send */
2362 signum
= ffs((long)sigbits
);
2363 mask
= sigmask(signum
);
2364 prop
= sigprop
[signum
];
2367 * We should see pending but ignored signals
2368 * only if P_TRACED was on when they were posted.
2370 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2373 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2375 * Put the new signal into p_siglist. If the
2376 * signal is being masked, look for other signals.
2378 mask
= sigmask(signum
);
2379 if (ut
->uu_sigmask
& mask
)
2385 * Decide whether the signal should be returned.
2386 * Return the signal's number, or fall through
2387 * to clear it from the pending mask.
2390 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2394 * Don't take default actions on system processes.
2396 if (p
->p_pptr
->p_pid
== 0) {
2399 * Are you sure you want to ignore SIGSEGV
2402 printf("Process (pid %d) got signal %d\n",
2405 break; /* == ignore */
2409 * If there is a pending stop signal to process
2410 * with default action, stop here,
2411 * then clear the signal. However,
2412 * if process is member of an orphaned
2413 * process group, ignore tty stop signals.
2415 if (prop
& SA_STOP
) {
2416 if (p
->p_flag
& P_TRACED
||
2417 (p
->p_pgrp
->pg_jobc
== 0 &&
2419 break; /* == ignore */
2422 } else if (prop
& SA_IGNORE
) {
2424 * Except for SIGCONT, shouldn't get here.
2425 * Default action is to ignore; drop it.
2427 break; /* == ignore */
2435 * Masking above should prevent us ever trying
2436 * to take action on an ignored signal other
2437 * than SIGCONT, unless process is traced.
2439 if ((prop
& SA_CONT
) == 0 &&
2440 (p
->p_flag
& P_TRACED
) == 0)
2441 printf("issignal\n");
2442 break; /* == ignore */
2446 * This signal has an action, let
2447 * postsig() process it.
2451 sigbits
&= ~mask
; /* take the signal! */
2457 * Put the argument process into the stopped state and notify the parent
2458 * via wakeup. Signals are handled elsewhere. The process must not be
2463 register struct proc
*p
;
2466 p
->p_flag
&= ~P_WAITED
;
2467 wakeup((caddr_t
)p
->p_pptr
);
2468 (void) task_suspend(p
->task
); /*XXX*/
2472 * Take the action for the specified signal
2473 * from the current set of pending signals.
2477 register int signum
;
2479 register struct proc
*p
= current_proc();
2480 register struct sigacts
*ps
= p
->p_sigacts
;
2481 register sig_t action
;
2483 int mask
, returnmask
;
2484 struct uthread
* ut
;
2490 * This must be called on master cpu
2492 if (cpu_number() != master_cpu
)
2493 panic("psig not on master");
2498 * Try to grab the signal lock.
2500 if (sig_try_locked(p
) <= 0) {
2505 ut
= (struct uthread
*)get_bsdthread_info(current_act());
2506 mask
= sigmask(signum
);
2507 ut
->uu_siglist
&= ~mask
;
2508 p
->p_siglist
&= ~mask
;
2509 action
= ps
->ps_sigact
[signum
];
2511 if (KTRPOINT(p
, KTR_PSIG
))
2512 ktrpsig(p
->p_tracep
,
2513 signum
, action
, ut
->uu_flag
& USAS_OLDMASK
?
2514 &ut
->uu_oldmask
: &ut
->uu_sigmask
, 0, -1);
2516 if (action
== SIG_DFL
) {
2518 * Default action, where the default is to kill
2519 * the process. (Other cases were ignored above.)
2521 /* called with signal_lock() held */
2522 sigexit_locked(p
, signum
);
2527 * If we get here, the signal must be caught.
2530 if (action
== SIG_IGN
|| (ut
->uu_sigmask
& mask
))
2532 "postsig: processing masked or ignored signal\n");
2535 * Set the new mask value and also defer further
2536 * occurences of this signal.
2538 * Special case: user has done a sigpause. Here the
2539 * current mask is not of interest, but rather the
2540 * mask from before the sigpause is what we want
2541 * restored after the signal processing is completed.
2543 if (ut
->uu_flag
& USAS_OLDMASK
) {
2544 returnmask
= ut
->uu_oldmask
;
2545 ut
->uu_flag
&= ~USAS_OLDMASK
;
2548 returnmask
= ut
->uu_sigmask
;
2549 ut
->uu_sigmask
|= ps
->ps_catchmask
[signum
];
2550 if ((ps
->ps_signodefer
& mask
) == 0)
2551 ut
->uu_sigmask
|= mask
;
2552 if ((signum
!= SIGILL
) && (signum
!= SIGTRAP
) && (ps
->ps_sigreset
& mask
)) {
2553 if ((signum
!= SIGCONT
) && (sigprop
[signum
] & SA_IGNORE
))
2554 p
->p_sigignore
|= mask
;
2555 ps
->ps_sigact
[signum
] = SIG_DFL
;
2556 ps
->ps_siginfo
&= ~mask
;
2557 ps
->ps_signodefer
&= ~mask
;
2560 /* Needs to disable to run in user mode */
2561 if (signum
== SIGFPE
) {
2562 thread_enable_fpe(current_act(), 0);
2564 #endif /* __ppc__ */
2566 if (ps
->ps_sig
!= signum
) {
2572 p
->p_stats
->p_ru
.ru_nsignals
++;
2573 sendsig(p
, action
, signum
, returnmask
, code
);
2579 * Force the current process to exit with the specified signal, dumping core
2580 * if appropriate. We bypass the normal tests for masked and caught signals,
2581 * allowing unrecoverable failures to terminate the process without changing
2582 * signal state. Mark the accounting record with the signal termination.
2583 * If dumping core, save the signal number for the debugger. Calls exit and
2586 /* called with signal lock */
2588 sigexit_locked(p
, signum
)
2589 register struct proc
*p
;
2593 sig_lock_to_exit(p
);
2594 p
->p_acflag
|= AXSIG
;
2595 if (sigprop
[signum
] & SA_CORE
) {
2596 p
->p_sigacts
->ps_sig
= signum
;
2597 if (coredump(p
) == 0)
2598 signum
|= WCOREFLAG
;
2601 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
2607 filt_sigattach(struct knote
*kn
)
2609 struct proc
*p
= current_proc();
2611 kn
->kn_ptr
.p_proc
= p
;
2612 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
2614 /* XXX lock the proc here while adding to the list? */
2615 KNOTE_ATTACH(&p
->p_klist
, kn
);
2621 filt_sigdetach(struct knote
*kn
)
2623 struct proc
*p
= kn
->kn_ptr
.p_proc
;
2625 KNOTE_DETACH(&p
->p_klist
, kn
);
2629 * signal knotes are shared with proc knotes, so we apply a mask to
2630 * the hint in order to differentiate them from process hints. This
2631 * could be avoided by using a signal-specific knote list, but probably
2632 * isn't worth the trouble.
2635 filt_signal(struct knote
*kn
, long hint
)
2638 if (hint
& NOTE_SIGNAL
) {
2639 hint
&= ~NOTE_SIGNAL
;
2641 if (kn
->kn_id
== hint
)
2644 return (kn
->kn_data
!= 0);
2648 bsd_ast(thread_act_t thr_act
)
2650 struct proc
*p
= current_proc();
2651 struct uthread
*ut
= get_bsdthread_info(thr_act
);
2654 boolean_t funnel_state
;
2655 static bsd_init_done
= 0;
2660 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2662 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
2663 pc
= get_useraddr();
2664 addupc_task(p
, pc
, 1);
2665 p
->p_flag
&= ~P_OWEUPC
;
2668 if (CHECK_SIGNALS(p
, current_act(), ut
)) {
2669 while (signum
= issignal(p
))
2672 if (!bsd_init_done
) {
2673 extern void bsdinit_task(void);
2679 (void) thread_funnel_set(kernel_flock
, FALSE
);
2683 * Follwing routines are called using callout from bsd_hardclock
2684 * so that psignals are called in a thread context and are funneled
2687 psignal_vtalarm(struct proc
*p
)
2689 boolean_t funnel_state
;
2693 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2694 psignal_lock(p
, SIGVTALRM
, 1);
2695 (void) thread_funnel_set(kernel_flock
, FALSE
);
2699 psignal_xcpu(struct proc
*p
)
2701 boolean_t funnel_state
;
2705 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2706 psignal_lock(p
, SIGXCPU
, 1);
2707 (void) thread_funnel_set(kernel_flock
, FALSE
);
2711 psignal_sigprof(struct proc
*p
)
2713 boolean_t funnel_state
;
2717 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2718 psignal_lock(p
, SIGPROF
, 1);
2719 (void) thread_funnel_set(kernel_flock
, FALSE
);
2722 /* ptrace set runnalbe */
2724 pt_setrunnable(struct proc
*p
)
2730 if (p
->p_flag
& P_TRACED
) {
2733 wakeup((caddr_t
)&(p
->sigwait
));
2745 exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
2746 extern kern_return_t
bsd_exception(int, exception_data_type_t codes
[], int);
2750 return(bsd_exception(exc
, codes
, 2));