2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995-1998 Apple Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1989, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
29 * (c) UNIX System Laboratories, Inc.
30 * All or some portions of this file are derived from material licensed
31 * to the University of California by American Telephone and Telegraph
32 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
33 * the permission of UNIX System Laboratories, Inc.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
66 #define SIGPROP /* include signal properties table */
67 #include <sys/param.h>
68 #include <sys/resourcevar.h>
69 #include <sys/namei.h>
70 #include <sys/vnode.h>
72 #include <sys/systm.h>
73 #include <sys/timeb.h>
74 #include <sys/times.h>
78 #include <sys/kernel.h>
80 #include <sys/signalvar.h>
82 #include <sys/ktrace.h>
84 #include <sys/syslog.h>
87 #include <sys/kdebug.h>
89 #include <sys/mount.h>
91 #include <kern/cpu_number.h>
94 #include <sys/user.h> /* for coredump */
95 #include <kern/ast.h> /* for APC support */
96 #include <kern/thread.h>
97 #include <kern/sched_prim.h>
98 #include <kern/thread_call.h>
99 #include <mach/exception.h>
101 extern void doexception(int exc
, int code
, int sub
);
103 void stop
__P((struct proc
*p
));
104 int cansignal
__P((struct proc
*, struct pcred
*, struct proc
*, int));
105 int killpg1
__P((struct proc
*, int, int, int));
106 void sigexit_locked
__P((struct proc
*, int));
107 int setsigvec
__P((struct proc
*, int, struct __sigaction
*));
108 void exit1
__P((struct proc
*, int, int *));
109 int signal_lock
__P((struct proc
*));
110 int signal_unlock
__P((struct proc
*));
111 void signal_setast
__P((thread_act_t
));
112 void psignal_lock
__P((struct proc
*, int, int));
113 void psignal_uthread
__P((thread_act_t
, int));
114 kern_return_t
do_bsdexception(int, int, int);
117 void ram_printf
__P((int));
119 unsigned int rdebug_proc
=0;
126 #endif /* SIGNAL_DEBUG */
129 signal_lock(struct proc
*p
)
136 int register sp
, *fp
, numsaved
;
138 __asm__
volatile("mr %0,r1" : "=r" (sp
));
140 fp
= (int *)*((int *)sp
);
141 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
142 p
->lockpc
[numsaved
] = fp
[2];
149 #endif /* SIGNAL_DEBUG */
150 #endif /* DIAGNOSTIC */
153 /* TBD: check p last arg */
154 error
= lockmgr(&p
->signal_lock
, LK_EXCLUSIVE
, 0, (struct proc
*)p
);
161 signal_unlock(struct proc
*p
)
167 int register sp
, *fp
, numsaved
;
169 __asm__
volatile("mr %0,r1" : "=r" (sp
));
171 fp
= (int *)*((int *)sp
);
172 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
173 p
->unlockpc
[numsaved
] = fp
[2];
180 #endif /* SIGNAL_DEBUG */
181 #endif /* DIAGNOSTIC */
183 /* TBD: check p last arg */
184 return(lockmgr(&p
->signal_lock
, LK_RELEASE
, (simple_lock_t
)0, (struct proc
*)p
));
188 signal_setast(sig_actthread
)
189 thread_act_t sig_actthread
;
191 act_set_astbsd(sig_actthread
);
195 * Can process p, with pcred pc, send the signal signum to process q?
198 cansignal(p
, pc
, q
, signum
)
204 /* you can signal yourself */
208 if (pc
->pc_ucred
->cr_uid
== 0)
209 return (1); /* root can always signal */
211 if (signum
== SIGCONT
&& q
->p_session
== p
->p_session
)
212 return (1); /* SIGCONT in session */
215 * Using kill(), only certain signals can be sent to setugid
218 if (q
->p_flag
& P_SUGID
) {
231 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
232 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
233 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
234 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
241 * because the P_SUGID test exists, this has extra tests which
244 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
245 pc
->p_ruid
== q
->p_cred
->p_svuid
||
246 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
247 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_svuid
||
248 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
249 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
254 struct sigaction_args
{
256 struct __sigaction
*nsa
;
257 struct sigaction
*osa
;
262 sigaction(p
, uap
, retval
)
264 register struct sigaction_args
*uap
;
267 struct sigaction vec
;
268 struct __sigaction __vec
;
270 register struct sigaction
*sa
;
271 register struct sigacts
*ps
= p
->p_sigacts
;
275 signum
= uap
->signum
;
276 if (signum
<= 0 || signum
>= NSIG
||
277 signum
== SIGKILL
|| signum
== SIGSTOP
)
281 sa
->sa_handler
= ps
->ps_sigact
[signum
];
282 sa
->sa_mask
= ps
->ps_catchmask
[signum
];
283 bit
= sigmask(signum
);
285 if ((ps
->ps_sigonstack
& bit
) != 0)
286 sa
->sa_flags
|= SA_ONSTACK
;
287 if ((ps
->ps_sigintr
& bit
) == 0)
288 sa
->sa_flags
|= SA_RESTART
;
289 if (ps
->ps_siginfo
& bit
)
290 sa
->sa_flags
|= SA_SIGINFO
;
291 if (ps
->ps_signodefer
& bit
)
292 sa
->sa_flags
|= SA_NODEFER
;
293 if (ps
->ps_64regset
& bit
)
294 sa
->sa_flags
|= SA_64REGSET
;
295 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDSTOP
))
296 sa
->sa_flags
|= SA_NOCLDSTOP
;
297 if ((signum
== SIGCHLD
) && (p
->p_flag
& P_NOCLDWAIT
))
298 sa
->sa_flags
|= SA_NOCLDWAIT
;
299 if (error
= copyout((caddr_t
)sa
, (caddr_t
)uap
->osa
,
304 if (error
= copyin((caddr_t
)uap
->nsa
, (caddr_t
)&__vec
,
307 error
= setsigvec(p
, signum
, &__vec
);
312 /* Routines to manipulate bits on all threads */
314 clear_procsiglist(struct proc
*p
, int bit
)
316 struct uthread
* uth
;
321 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
322 thact
= p
->p_vforkact
;
323 uth
= (struct uthread
*)get_bsdthread_info(thact
);
325 uth
->uu_siglist
&= ~bit
;
327 p
->p_siglist
&= ~bit
;
332 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
333 uth
->uu_siglist
&= ~bit
;
335 p
->p_siglist
&= ~bit
;
341 unblock_procsigmask(struct proc
*p
, int bit
)
343 struct uthread
* uth
;
347 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
348 thact
= p
->p_vforkact
;
349 uth
= (struct uthread
*)get_bsdthread_info(thact
);
351 uth
->uu_sigmask
&= ~bit
;
353 p
->p_sigmask
&= ~bit
;
357 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
358 uth
->uu_sigmask
&= ~bit
;
360 p
->p_sigmask
&= ~bit
;
367 block_procsigmask(struct proc
*p
, int bit
)
369 struct uthread
* uth
;
373 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
374 thact
= p
->p_vforkact
;
375 uth
= (struct uthread
*)get_bsdthread_info(thact
);
377 uth
->uu_sigmask
|= bit
;
383 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
384 uth
->uu_sigmask
|= bit
;
391 set_procsigmask(struct proc
*p
, int bit
)
393 struct uthread
* uth
;
397 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
398 thact
= p
->p_vforkact
;
399 uth
= (struct uthread
*)get_bsdthread_info(thact
);
401 uth
->uu_sigmask
= bit
;
407 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
408 uth
->uu_sigmask
= bit
;
416 setsigvec(p
, signum
, sa
)
417 register struct proc
*p
;
419 register struct __sigaction
*sa
;
421 register struct sigacts
*ps
= p
->p_sigacts
;
424 if ((signum
== SIGKILL
|| signum
== SIGSTOP
) &&
425 sa
->sa_handler
!= SIG_DFL
)
427 bit
= sigmask(signum
);
429 * Change setting atomically.
431 ps
->ps_sigact
[signum
] = sa
->sa_handler
;
432 ps
->ps_trampact
[signum
] = sa
->sa_tramp
;
433 ps
->ps_catchmask
[signum
] = sa
->sa_mask
&~ sigcantmask
;
434 if (sa
->sa_flags
& SA_SIGINFO
)
435 ps
->ps_siginfo
|= bit
;
437 ps
->ps_siginfo
&= ~bit
;
438 if (sa
->sa_flags
& SA_64REGSET
)
439 ps
->ps_64regset
|= bit
;
441 ps
->ps_64regset
&= ~bit
;
442 if ((sa
->sa_flags
& SA_RESTART
) == 0)
443 ps
->ps_sigintr
|= bit
;
445 ps
->ps_sigintr
&= ~bit
;
446 if (sa
->sa_flags
& SA_ONSTACK
)
447 ps
->ps_sigonstack
|= bit
;
449 ps
->ps_sigonstack
&= ~bit
;
450 if (sa
->sa_flags
& SA_USERTRAMP
)
451 ps
->ps_usertramp
|= bit
;
453 ps
->ps_usertramp
&= ~bit
;
454 if (sa
->sa_flags
& SA_RESETHAND
)
455 ps
->ps_sigreset
|= bit
;
457 ps
->ps_sigreset
&= ~bit
;
458 if (sa
->sa_flags
& SA_NODEFER
)
459 ps
->ps_signodefer
|= bit
;
461 ps
->ps_signodefer
&= ~bit
;
462 if (signum
== SIGCHLD
) {
463 if (sa
->sa_flags
& SA_NOCLDSTOP
)
464 p
->p_flag
|= P_NOCLDSTOP
;
466 p
->p_flag
&= ~P_NOCLDSTOP
;
467 if ((sa
->sa_flags
& SA_NOCLDWAIT
) || (sa
->sa_handler
== SIG_IGN
))
468 p
->p_flag
|= P_NOCLDWAIT
;
470 p
->p_flag
&= ~P_NOCLDWAIT
;
474 if (signum
== SIGFPE
) {
475 if (sa
->sa_handler
== SIG_DFL
|| sa
->sa_handler
== SIG_IGN
)
476 thread_enable_fpe(current_act(), 0);
478 thread_enable_fpe(current_act(), 1);
482 * Set bit in p_sigignore for signals that are set to SIG_IGN,
483 * and for signals set to SIG_DFL where the default is to ignore.
484 * However, don't put SIGCONT in p_sigignore,
485 * as we have to restart the process.
487 if (sa
->sa_handler
== SIG_IGN
||
488 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
490 clear_procsiglist(p
, bit
);
491 if (signum
!= SIGCONT
)
492 p
->p_sigignore
|= bit
; /* easier in psignal */
493 p
->p_sigcatch
&= ~bit
;
495 p
->p_sigignore
&= ~bit
;
496 if (sa
->sa_handler
== SIG_DFL
)
497 p
->p_sigcatch
&= ~bit
;
499 p
->p_sigcatch
|= bit
;
505 * Initialize signal state for process 0;
506 * set to ignore signals that are ignored by default.
514 for (i
= 0; i
< NSIG
; i
++)
515 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
516 p
->p_sigignore
|= sigmask(i
);
520 * Reset signals for an exec of the specified process.
524 register struct proc
*p
;
525 register thread_act_t thr_act
;
527 register struct sigacts
*ps
= p
->p_sigacts
;
528 register int nc
, mask
;
532 * Reset caught signals. Held signals remain held
533 * through p_sigmask (unless they were caught,
534 * and are now ignored by default).
536 while (p
->p_sigcatch
) {
537 nc
= ffs((long)p
->p_sigcatch
);
539 p
->p_sigcatch
&= ~mask
;
540 if (sigprop
[nc
] & SA_IGNORE
) {
542 p
->p_sigignore
|= mask
;
544 ut
= (struct uthread
*)get_bsdthread_info(thr_act
);
545 ut
->uu_siglist
&= ~mask
;
546 p
->p_siglist
&= ~mask
;
548 clear_procsiglist(p
, mask
);
550 ps
->ps_sigact
[nc
] = SIG_DFL
;
553 * Reset stack state to the user stack.
554 * Clear set of signals caught on the signal stack.
556 ps
->ps_sigstk
.ss_flags
= SA_DISABLE
;
557 ps
->ps_sigstk
.ss_size
= 0;
558 ps
->ps_sigstk
.ss_sp
= 0;
563 * Manipulate signal mask.
564 * Note that we receive new mask, not pointer,
565 * and return old mask as return value;
566 * the library stub does the rest.
568 struct sigprocmask_args
{
574 sigprocmask(p
, uap
, retval
)
575 register struct proc
*p
;
576 struct sigprocmask_args
*uap
;
580 sigset_t oldmask
, nmask
;
581 sigset_t
* omask
= uap
->omask
;
584 ut
= (struct uthread
*)get_bsdthread_info(current_act());
585 oldmask
= ut
->uu_sigmask
;
587 if (uap
->mask
== (sigset_t
*)0) {
588 /* just want old mask */
591 error
= copyin((caddr_t
)uap
->mask
, &nmask
, sizeof(sigset_t
));
597 block_procsigmask(p
, (nmask
& ~sigcantmask
));
598 signal_setast(current_act());
602 unblock_procsigmask(p
, (nmask
& ~sigcantmask
));
603 signal_setast(current_act());
607 set_procsigmask(p
, (nmask
& ~sigcantmask
));
608 signal_setast(current_act());
617 copyout(&oldmask
, omask
, sizeof(sigset_t
));
621 struct sigpending_args
{
625 sigpending(p
, uap
, retval
)
627 register struct sigpending_args
*uap
;
633 ut
= (struct uthread
*)get_bsdthread_info(current_act());
634 pendlist
= ut
->uu_siglist
;
637 copyout(&pendlist
, uap
->osv
, sizeof(sigset_t
));
643 * Generalized interface signal handler, 4.3-compatible.
645 struct osigvec_args
{
652 osigvec(p
, uap
, retval
)
654 register struct osigvec_args
*uap
;
659 register struct sigacts
*ps
= p
->p_sigacts
;
660 register struct sigvec
*sv
;
664 panic("osigvec: notsupp");
666 signum
= uap
->signum
;
667 if (signum
<= 0 || signum
>= NSIG
||
668 signum
== SIGKILL
|| signum
== SIGSTOP
)
672 *(sig_t
*)&sv
->sv_handler
= ps
->ps_sigact
[signum
];
673 sv
->sv_mask
= ps
->ps_catchmask
[signum
];
674 bit
= sigmask(signum
);
676 if ((ps
->ps_sigonstack
& bit
) != 0)
677 sv
->sv_flags
|= SV_ONSTACK
;
678 if ((ps
->ps_sigintr
& bit
) != 0)
679 sv
->sv_flags
|= SV_INTERRUPT
;
680 if (p
->p_flag
& P_NOCLDSTOP
)
681 sv
->sv_flags
|= SA_NOCLDSTOP
;
682 if (error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->osv
,
687 if (error
= copyin((caddr_t
)uap
->nsv
, (caddr_t
)sv
,
690 sv
->sv_flags
^= SA_RESTART
; /* opposite of SV_INTERRUPT */
691 error
= setsigvec(p
, signum
, (struct sigaction
*)sv
);
697 struct osigblock_args
{
701 osigblock(p
, uap
, retval
)
702 register struct proc
*p
;
703 struct osigblock_args
*uap
;
706 struct uthread
* uth
= get_bsdthread_info(current_act());
708 *retval
= uth
->uu_sigmask
;
709 uth
->uu_sigmask
|= (uap
->mask
& ~sigcantmask
);
713 struct osigsetmask_args
{
717 osigsetmask(p
, uap
, retval
)
719 struct osigsetmask_args
*uap
;
722 struct uthread
* uth
= get_bsdthread_info(current_act());
724 *retval
= uth
->uu_sigmask
;
725 uth
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
728 #endif /* COMPAT_43 */
731 * Suspend process until signal, providing mask to be set
732 * in the meantime. Note nonstandard calling convention:
733 * libc stub passes mask, not pointer, to save a copyin.
739 struct uthread
*ut
= get_bsdthread_info(current_act());
740 unix_syscall_return(EINTR
);
743 struct sigsuspend_args
{
749 sigsuspend(p
, uap
, retval
)
750 register struct proc
*p
;
751 struct sigsuspend_args
*uap
;
754 register struct sigacts
*ps
= p
->p_sigacts
;
757 ut
= (struct uthread
*)get_bsdthread_info(current_act());
760 * When returning from sigpause, we want
761 * the old mask to be restored after the
762 * signal handler has finished. Thus, we
763 * save it here and mark the sigacts structure
766 ut
->uu_oldmask
= ut
->uu_sigmask
;
767 ut
->uu_flag
|= USAS_OLDMASK
;
768 ut
->uu_sigmask
= (uap
->mask
& ~sigcantmask
);
769 (void) tsleep0((caddr_t
) p
, PPAUSE
|PCATCH
, "pause", 0, sigcontinue
);
770 /* always return EINTR rather than ERESTART... */
774 struct __disable_thsignal_args
{
779 __disable_threadsignal(p
, uap
, retval
)
781 register struct __disable_thsignal_args
*uap
;
786 uth
= (struct uthread
*)get_bsdthread_info(current_act());
788 /* No longer valid to have any signal delivered */
790 uth
->uu_flag
|= UNO_SIGMASK
;
797 struct pthread_kill_args
{
803 __pthread_kill(p
, uap
, retval
)
805 register struct pthread_kill_args
*uap
;
808 thread_act_t target_act
;
810 int signum
= uap
->sig
;
813 target_act
= (thread_act_t
)port_name_to_act(uap
->thread_port
);
815 if (target_act
== THR_ACT_NULL
)
817 if ((u_int
)signum
>= NSIG
) {
822 uth
= (struct uthread
*)get_bsdthread_info(target_act
);
823 { void *tht
= getshuttle_thread(target_act
);
825 if (uth
->uu_flag
& UNO_SIGMASK
) {
831 psignal_uthread(target_act
, signum
);
833 act_deallocate(target_act
);
838 struct pthread_sigmask_args
{
844 pthread_sigmask(p
, uap
, retval
)
845 register struct proc
*p
;
846 register struct pthread_sigmask_args
*uap
;
850 const sigset_t
*set
= uap
->set
;
851 sigset_t
* oset
= uap
->oset
;
857 ut
= (struct uthread
*)get_bsdthread_info(current_act());
858 oldset
= ut
->uu_sigmask
;
860 if (set
== (sigset_t
*) 0) {
861 /* need only old mask */
865 error
= copyin((caddr_t
)set
, (caddr_t
)&nset
, sizeof(sigset_t
));
871 ut
->uu_sigmask
|= (nset
& ~sigcantmask
);
875 ut
->uu_sigmask
&= ~(nset
);
876 signal_setast(current_act());
880 ut
->uu_sigmask
= (nset
& ~sigcantmask
);
881 signal_setast(current_act());
890 copyout((caddr_t
)&oldset
, (caddr_t
)oset
, sizeof(sigset_t
));
896 struct sigwait_args
{
902 sigwait(p
, uap
, retval
)
903 register struct proc
*p
;
904 register struct sigwait_args
*uap
;
907 register struct sigacts
*ps
= p
->p_sigacts
;
917 ut
= (struct uthread
*)get_bsdthread_info(current_act());
919 if (uap
->set
== (const sigset_t
*)0)
922 error
= copyin((caddr_t
)uap
->set
, (caddr_t
)&mask
, sizeof(sigset_t
));
926 siglist
= (mask
& ~sigcantmask
);
932 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
936 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
937 if (sigw
= uth
->uu_siglist
& siglist
) {
944 /* The signal was pending on a thread */
948 * When returning from sigwait, we want
949 * the old mask to be restored after the
950 * signal handler has finished. Thus, we
951 * save it here and mark the sigacts structure
954 ut
->uu_oldmask
= ut
->uu_sigmask
;
955 ut
->uu_flag
|= USAS_OLDMASK
;
956 if (siglist
== (sigset_t
)0)
958 /* SIGKILL and SIGSTOP are not maskable as well */
959 ut
->uu_sigmask
= ~(siglist
|sigcantmask
);
960 ut
->uu_sigwait
= siglist
;
961 /* No Continuations for now */
962 error
= tsleep((caddr_t
)&ut
->uu_sigwait
, PPAUSE
|PCATCH
, "pause", 0);
964 if ((error
== EINTR
) || (error
== ERESTART
))
967 sigw
= (ut
->uu_sigwait
& siglist
);
968 ut
->uu_sigmask
= ut
->uu_oldmask
;
970 ut
->uu_flag
&= ~USAS_OLDMASK
;
974 signum
= ffs((unsigned int)sigw
);
976 panic("sigwait with no signal wakeup");
977 ut
->uu_siglist
&= ~(sigmask(signum
));
979 error
= copyout(&signum
, uap
->sig
, sizeof(int));
987 struct osigstack_args
{
988 struct sigstack
*nss
;
989 struct sigstack
*oss
;
994 osigstack(p
, uap
, retval
)
996 register struct osigstack_args
*uap
;
1000 struct sigacts
*psp
;
1004 ss
.ss_sp
= psp
->ps_sigstk
.ss_sp
;
1005 ss
.ss_onstack
= psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
;
1006 if (uap
->oss
&& (error
= copyout((caddr_t
)&ss
,
1007 (caddr_t
)uap
->oss
, sizeof (struct sigstack
))))
1009 if (uap
->nss
&& (error
= copyin((caddr_t
)uap
->nss
,
1010 (caddr_t
)&ss
, sizeof (ss
))) == 0) {
1011 psp
->ps_sigstk
.ss_sp
= ss
.ss_sp
;
1012 psp
->ps_sigstk
.ss_size
= 0;
1013 psp
->ps_sigstk
.ss_flags
|= ss
.ss_onstack
& SA_ONSTACK
;
1014 psp
->ps_flags
|= SAS_ALTSTACK
;
1018 #endif /* COMPAT_43 */
1020 struct sigaltstack_args
{
1021 struct sigaltstack
*nss
;
1022 struct sigaltstack
*oss
;
1026 sigaltstack(p
, uap
, retval
)
1028 register struct sigaltstack_args
*uap
;
1031 struct sigacts
*psp
;
1032 struct sigaltstack ss
;
1036 if ((psp
->ps_flags
& SAS_ALTSTACK
) == 0)
1037 psp
->ps_sigstk
.ss_flags
|= SA_DISABLE
;
1038 if (uap
->oss
&& (error
= copyout((caddr_t
)&psp
->ps_sigstk
,
1039 (caddr_t
)uap
->oss
, sizeof (struct sigaltstack
))))
1043 if (error
= copyin((caddr_t
)uap
->nss
, (caddr_t
)&ss
,
1046 if ((ss
.ss_flags
& ~SA_DISABLE
) != 0) {
1050 if (ss
.ss_flags
& SA_DISABLE
) {
1051 if (psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
)
1053 psp
->ps_flags
&= ~SAS_ALTSTACK
;
1054 psp
->ps_sigstk
.ss_flags
= ss
.ss_flags
;
1057 if (ss
.ss_size
< MINSIGSTKSZ
)
1059 psp
->ps_flags
|= SAS_ALTSTACK
;
1070 kill(cp
, uap
, retval
)
1071 register struct proc
*cp
;
1072 register struct kill_args
*uap
;
1075 register struct proc
*p
;
1076 register struct pcred
*pc
= cp
->p_cred
;
1078 if ((u_int
)uap
->signum
>= NSIG
)
1081 /* kill single process */
1082 if ((p
= pfind(uap
->pid
)) == NULL
)
1084 if (!cansignal(cp
, pc
, p
, uap
->signum
))
1087 psignal(p
, uap
->signum
);
1091 case -1: /* broadcast signal */
1092 return (killpg1(cp
, uap
->signum
, 0, 1));
1093 case 0: /* signal own process group */
1094 return (killpg1(cp
, uap
->signum
, 0, 0));
1095 default: /* negative explicit process group */
1096 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0));
1102 struct okillpg_args
{
1108 okillpg(p
, uap
, retval
)
1110 register struct okillpg_args
*uap
;
1114 if ((u_int
)uap
->signum
>= NSIG
)
1116 return (killpg1(p
, uap
->signum
, uap
->pgid
, 0));
1118 #endif /* COMPAT_43 */
1121 * Common code for kill process group/broadcast kill.
1122 * cp is calling process.
1125 killpg1(cp
, signum
, pgid
, all
)
1126 register struct proc
*cp
;
1127 int signum
, pgid
, all
;
1129 register struct proc
*p
;
1130 register struct pcred
*pc
= cp
->p_cred
;
1138 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
1139 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1140 p
== cp
|| !cansignal(cp
, pc
, p
, signum
))
1149 * zero pgid means send to my process group.
1153 pgrp
= pgfind(pgid
);
1157 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1158 p
= p
->p_pglist
.le_next
) {
1159 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
1160 p
->p_stat
== SZOMB
||
1161 !cansignal(cp
, pc
, p
, signum
))
1168 return (nfound
? 0 : ESRCH
);
1172 * Send a signal to a process group.
1175 gsignal(pgid
, signum
)
1180 if (pgid
&& (pgrp
= pgfind(pgid
)))
1181 pgsignal(pgrp
, signum
, 0);
1185 * Send a signal to a process group. If checktty is 1,
1186 * limit to members which have a controlling terminal.
1189 pgsignal(pgrp
, signum
, checkctty
)
1191 int signum
, checkctty
;
1193 register struct proc
*p
;
1196 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1197 p
= p
->p_pglist
.le_next
)
1198 if (checkctty
== 0 || p
->p_flag
& P_CONTROLT
)
1203 * Send signal to a backgrounded process blocked due to tty access
1204 * In FreeBSD, the backgrounded process wakes up every second and
1205 * discovers whether it is foregounded or not. In our case, we block
1206 * the thread in tsleep as we want to avoid storm of processes as well
1207 * as the suspend is only at AST level
1210 tty_pgsignal(pgrp
, signum
)
1214 register struct proc
*p
;
1217 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
1218 p
= p
->p_pglist
.le_next
)
1219 if ((p
->p_flag
& P_TTYSLEEP
) && (p
->p_flag
& P_CONTROLT
))
1224 * Send a signal caused by a trap to a specific thread.
1227 threadsignal(sig_actthread
, signum
, code
)
1228 register thread_act_t sig_actthread
;
1229 register int signum
;
1232 register struct uthread
*uth
;
1233 register struct task
* sig_task
;
1234 register struct proc
*p
;
1237 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1240 mask
= sigmask(signum
);
1241 if ((mask
& threadmask
) == 0)
1243 sig_task
= get_threadtask(sig_actthread
);
1244 p
= (struct proc
*)(get_bsdtask_info(sig_task
));
1246 uth
= get_bsdthread_info(sig_actthread
);
1247 if (uth
&& (uth
->uu_flag
& P_VFORK
))
1250 if (!(p
->p_flag
& P_TRACED
) && (p
->p_sigignore
& mask
))
1253 uth
->uu_siglist
|= mask
;
1254 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1255 uth
->uu_code
= code
;
1256 /* mark on process as well */
1257 signal_setast(sig_actthread
);
1263 register struct proc
*p
;
1264 register int signum
;
1266 psignal_lock(p
, signum
, 1);
1270 psignal_vfork(p
, new_task
, thr_act
, signum
)
1271 register struct proc
*p
;
1273 thread_act_t thr_act
;
1274 register int signum
;
1278 register int s
, prop
;
1279 register sig_t action
;
1282 struct uthread
*uth
;
1284 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1285 panic("psignal signal number");
1286 mask
= sigmask(signum
);
1287 prop
= sigprop
[signum
];
1290 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1293 #endif /* SIGNAL_DEBUG */
1295 if ((new_task
== TASK_NULL
) || (thr_act
== (thread_act_t
)NULL
) || is_kerneltask(new_task
))
1299 uth
= get_bsdthread_info(thr_act
);
1303 * proc is traced, always give parent a chance.
1307 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1308 (p
->p_flag
& P_TRACED
) == 0)
1311 if (prop
& SA_CONT
) {
1312 p
->p_siglist
&= ~stopsigmask
;
1313 uth
->uu_siglist
&= ~stopsigmask
;
1316 if (prop
& SA_STOP
) {
1318 * If sending a tty stop signal to a member of an orphaned
1319 * process group, discard the signal here if the action
1320 * is default; don't stop the process below if sleeping,
1321 * and don't clear any pending SIGCONT.
1323 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1326 uth
->uu_siglist
&= ~contsigmask
;
1327 p
->p_siglist
&= ~contsigmask
;
1329 uth
->uu_siglist
|= mask
;
1330 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1332 /* Deliver signal to the activation passed in */
1333 act_set_astbsd(thr_act
);
1336 * SIGKILL priority twiddling moved here from above because
1337 * it needs sig_thread. Could merge it into large switch
1338 * below if we didn't care about priority for tracing
1339 * as SIGKILL's action is always SIG_DFL.
1341 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1346 * This Process is traced - wake it up (if not already
1347 * stopped) so that it can discover the signal in
1348 * issig() and stop for the parent.
1350 if (p
->p_flag
& P_TRACED
) {
1351 if (p
->p_stat
!= SSTOP
)
1358 * If we're being traced (possibly because someone attached us
1359 * while we were stopped), check for a signal from the debugger.
1361 if (p
->p_stat
== SSTOP
) {
1362 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
1363 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1364 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1369 * setrunnable(p) in BSD
1378 get_signalthread(struct proc
*p
, int signum
)
1380 struct uthread
*uth
;
1381 thread_act_t thr_act
;
1382 sigset_t mask
= sigmask(signum
);
1383 thread_act_t sig_thread_act
;
1384 struct task
* sig_task
= p
->task
;
1385 thread_t sig_thread
;
1388 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
1389 sig_thread_act
= p
->p_vforkact
;
1390 kret
= check_actforsig(sig_task
, sig_thread_act
, &sig_thread
, 1);
1391 if (kret
== KERN_SUCCESS
)
1392 return(sig_thread_act
);
1394 return(THR_ACT_NULL
);
1397 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
1398 if(((uth
->uu_flag
& UNO_SIGMASK
)== 0) &&
1399 (((uth
->uu_sigmask
& mask
) == 0) || (uth
->uu_sigwait
& mask
))) {
1400 if (check_actforsig(p
->task
, uth
->uu_act
, NULL
, 1) == KERN_SUCCESS
)
1401 return(uth
->uu_act
);
1404 if (get_signalact(p
->task
, &thr_act
, NULL
, 1) == KERN_SUCCESS
) {
1408 return(THR_ACT_NULL
);
1412 * Send the signal to the process. If the signal has an action, the action
1413 * is usually performed by the target process rather than the caller; we add
1414 * the signal to the set of pending signals for the process.
1417 * o When a stop signal is sent to a sleeping process that takes the
1418 * default action, the process is stopped without awakening it.
1419 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1420 * regardless of the signal action (eg, blocked or ignored).
1422 * Other ignored signals are discarded immediately.
1425 psignal_lock(p
, signum
, withlock
)
1426 register struct proc
*p
;
1427 register int signum
;
1428 register int withlock
;
1430 register int s
, prop
;
1431 register sig_t action
;
1432 thread_act_t sig_thread_act
;
1433 thread_t sig_thread
;
1434 register task_t sig_task
;
1435 register thread_t cur_thread
;
1436 thread_act_t cur_act
;
1438 struct uthread
*uth
;
1442 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1443 panic("psignal signal number");
1444 mask
= sigmask(signum
);
1445 prop
= sigprop
[signum
];
1448 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1451 #endif /* SIGNAL_DEBUG */
1453 if (thread_funnel_get() == (funnel_t
*)network_flock
) {
1455 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1458 * We will need the task pointer later. Grab it now to
1459 * check for a zombie process. Also don't send signals
1460 * to kernel internal tasks.
1462 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1464 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1469 * do not send signals to the process that has the thread
1470 * doing a reboot(). Not doing so will mark that thread aborted
1471 * and can cause IO failures wich will cause data loss.
1473 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1475 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1483 * Deliver the signal to the first thread in the task. This
1484 * allows single threaded applications which use signals to
1485 * be able to be linked with multithreaded libraries. We have
1486 * an implicit reference to the current_thread, but need
1487 * an explicit one otherwise. The thread reference keeps
1488 * the corresponding task data structures around too. This
1489 * reference is released by thread_deallocate.
1492 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1495 cur_thread
= current_thread(); /* this is a shuttle */
1496 cur_act
= current_act();
1498 /* If successful return with ast set */
1499 sig_thread_act
= get_signalthread(p
, signum
);
1501 if (sig_thread_act
== THR_ACT_NULL
) {
1503 /* if it is sigkill, may be we should
1504 * inject a thread to terminate
1508 #endif /* SIGNAL_DEBUG */
1512 uth
= get_bsdthread_info(sig_thread_act
);
1515 * If proc is traced, always give parent a chance.
1517 if (p
->p_flag
& P_TRACED
)
1521 * If the signal is being ignored,
1522 * then we forget about it immediately.
1523 * (Note: we don't set SIGCONT in p_sigignore,
1524 * and if it is set to SIG_IGN,
1525 * action will be SIG_DFL here.)
1527 if (p
->p_sigignore
& mask
)
1529 if (uth
->uu_sigwait
& mask
)
1531 if (uth
->uu_sigmask
& mask
)
1533 else if (p
->p_sigcatch
& mask
)
1539 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1540 (p
->p_flag
& P_TRACED
) == 0)
1543 if (prop
& SA_CONT
) {
1544 uth
->uu_siglist
&= ~stopsigmask
;
1545 p
->p_siglist
&= ~stopsigmask
;
1548 if (prop
& SA_STOP
) {
1550 * If sending a tty stop signal to a member of an orphaned
1551 * process group, discard the signal here if the action
1552 * is default; don't stop the process below if sleeping,
1553 * and don't clear any pending SIGCONT.
1555 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1558 uth
->uu_siglist
&= ~contsigmask
;
1559 p
->p_siglist
&= ~contsigmask
;
1561 uth
->uu_siglist
|= mask
;
1562 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1566 * Defer further processing for signals which are held,
1567 * except that stopped processes must be continued by SIGCONT.
1569 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
)) {
1573 * SIGKILL priority twiddling moved here from above because
1574 * it needs sig_thread. Could merge it into large switch
1575 * below if we didn't care about priority for tracing
1576 * as SIGKILL's action is always SIG_DFL.
1578 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1583 * Process is traced - wake it up (if not already
1584 * stopped) so that it can discover the signal in
1585 * issig() and stop for the parent.
1587 if (p
->p_flag
& P_TRACED
) {
1588 if (p
->p_stat
!= SSTOP
)
1594 if (action
== SIG_WAIT
) {
1595 uth
->uu_sigwait
= mask
;
1596 uth
->uu_siglist
&= ~mask
;
1597 p
->p_siglist
&= ~mask
;
1598 wakeup(&uth
->uu_sigwait
);
1599 /* if it is SIGCONT resume whole process */
1601 (void) task_resume(sig_task
);
1605 if (action
!= SIG_DFL
) {
1607 * User wants to catch the signal.
1608 * Wake up the thread, but don't un-suspend it
1609 * (except for SIGCONT).
1612 (void) task_resume(sig_task
);
1615 /* Default action - varies */
1616 if (mask
& stopsigmask
) {
1618 * These are the signals which by default
1621 * Don't clog system with children of init
1622 * stopped from the keyboard.
1624 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1625 psignal_lock(p
, SIGKILL
, 0);
1626 uth
->uu_siglist
&= ~mask
;
1627 p
->p_siglist
&= ~mask
;
1633 * if task hasn't already been stopped by
1636 uth
->uu_siglist
&= ~mask
;
1637 p
->p_siglist
&= ~mask
;
1638 if (p
->p_stat
!= SSTOP
) {
1639 p
->p_xstat
= signum
;
1641 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1642 struct proc
*pp
= p
->p_pptr
;
1644 pp
->si_pid
= p
->p_pid
;
1645 pp
->si_status
= p
->p_xstat
;
1646 pp
->si_code
= CLD_STOPPED
;
1647 pp
->si_uid
= p
->p_cred
->p_ruid
;
1648 psignal(pp
, SIGCHLD
);
1656 * Signals ignored by default have been dealt
1657 * with already, since their bits are on in
1663 * Kill signal always sets process running and
1667 * Process will be running after 'run'
1671 thread_abort(sig_thread_act
);
1677 * Let the process run. If it's sleeping on an
1678 * event, it remains so.
1680 if (p
->p_flag
& P_TTYSLEEP
) {
1681 p
->p_flag
&= ~P_TTYSLEEP
;
1682 wakeup(&p
->p_siglist
);
1684 (void) task_resume(sig_task
);
1686 uth
->uu_siglist
&= ~mask
;
1687 p
->p_siglist
&= ~mask
;
1694 * All other signals wake up the process, but don't
1703 * If we're being traced (possibly because someone attached us
1704 * while we were stopped), check for a signal from the debugger.
1706 if (p
->p_stat
== SSTOP
) {
1707 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
1708 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1711 * setrunnable(p) in BSD and
1712 * Wake up the thread if it is interruptible.
1715 thread_abort_safely(sig_thread_act
);
1721 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1725 /* psignal_lock(p, signum, withlock ) */
1727 psignal_uthread(thr_act
, signum
)
1728 thread_act_t thr_act
;
1732 register int s
, prop
;
1733 register sig_t action
;
1734 thread_act_t sig_thread_act
;
1735 thread_t sig_thread
;
1736 register task_t sig_task
;
1737 register thread_t cur_thread
;
1738 thread_act_t cur_act
;
1740 struct uthread
*uth
;
1744 p
= (struct proc
*)get_bsdtask_info(get_threadtask(thr_act
));
1745 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1746 panic("Invalid signal number in psignal_uthread");
1747 mask
= sigmask(signum
);
1748 prop
= sigprop
[signum
];
1751 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1754 #endif /* SIGNAL_DEBUG */
1757 * We will need the task pointer later. Grab it now to
1758 * check for a zombie process. Also don't send signals
1759 * to kernel internal tasks.
1761 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
)) {
1765 sig_thread_act
= thr_act
;
1767 * do not send signals to the process that has the thread
1768 * doing a reboot(). Not doing so will mark that thread aborted
1769 * and can cause IO failures wich will cause data loss.
1771 if (ISSET(p
->p_flag
, P_REBOOT
)) {
1778 * Deliver the signal to the first thread in the task. This
1779 * allows single threaded applications which use signals to
1780 * be able to be linked with multithreaded libraries. We have
1781 * an implicit reference to the current_thread, but need
1782 * an explicit one otherwise. The thread reference keeps
1783 * the corresponding task data structures around too. This
1784 * reference is released by thread_deallocate.
1787 if (((p
->p_flag
& P_TRACED
) == 0) && (p
->p_sigignore
& mask
))
1790 cur_thread
= current_thread(); /* this is a shuttle */
1791 cur_act
= current_act();
1793 kret
= check_actforsig(sig_task
, sig_thread_act
, &sig_thread
, 1);
1795 if (kret
!= KERN_SUCCESS
) {
1801 uth
= get_bsdthread_info(sig_thread_act
);
1804 * If proc is traced, always give parent a chance.
1806 if (p
->p_flag
& P_TRACED
)
1810 * If the signal is being ignored,
1811 * then we forget about it immediately.
1812 * (Note: we don't set SIGCONT in p_sigignore,
1813 * and if it is set to SIG_IGN,
1814 * action will be SIG_DFL here.)
1816 if (p
->p_sigignore
& mask
)
1818 if (uth
->uu_sigwait
& mask
)
1820 if (uth
->uu_sigmask
& mask
)
1822 else if (p
->p_sigcatch
& mask
)
1828 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1829 (p
->p_flag
& P_TRACED
) == 0)
1832 if (prop
& SA_CONT
) {
1833 uth
->uu_siglist
&= ~stopsigmask
;
1834 p
->p_siglist
&= ~stopsigmask
;
1837 if (prop
& SA_STOP
) {
1839 * If sending a tty stop signal to a member of an orphaned
1840 * process group, discard the signal here if the action
1841 * is default; don't stop the process below if sleeping,
1842 * and don't clear any pending SIGCONT.
1844 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1847 uth
->uu_siglist
&= ~contsigmask
;
1848 p
->p_siglist
&= ~contsigmask
;
1850 uth
->uu_siglist
|= mask
;
1851 p
->p_siglist
|= mask
; /* just for lame ones looking here */
1854 * Defer further processing for signals which are held,
1855 * except that stopped processes must be continued by SIGCONT.
1857 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
))
1861 * SIGKILL priority twiddling moved here from above because
1862 * it needs sig_thread. Could merge it into large switch
1863 * below if we didn't care about priority for tracing
1864 * as SIGKILL's action is always SIG_DFL.
1866 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1871 * Process is traced - wake it up (if not already
1872 * stopped) so that it can discover the signal in
1873 * issig() and stop for the parent.
1875 if (p
->p_flag
& P_TRACED
) {
1876 if (p
->p_stat
!= SSTOP
)
1882 if (action
== SIG_WAIT
) {
1883 uth
->uu_sigwait
= mask
;
1884 uth
->uu_siglist
&= ~mask
;
1885 p
->p_siglist
&= ~mask
;
1886 wakeup(&uth
->uu_sigwait
);
1887 /* if it is SIGCONT resume whole process */
1889 (void) task_resume(sig_task
);
1893 if (action
!= SIG_DFL
) {
1895 * User wants to catch the signal.
1896 * Wake up the thread, but don't un-suspend it
1897 * (except for SIGCONT).
1900 (void) task_resume(sig_task
);
1903 /* Default action - varies */
1904 if (mask
& stopsigmask
) {
1906 * These are the signals which by default
1909 * Don't clog system with children of init
1910 * stopped from the keyboard.
1912 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1913 psignal_lock(p
, SIGKILL
, 0);
1914 uth
->uu_siglist
&= ~mask
;
1915 p
->p_siglist
&= ~mask
;
1921 * if task hasn't already been stopped by
1924 uth
->uu_siglist
&= ~mask
;
1925 p
->p_siglist
&= ~mask
;
1926 if (p
->p_stat
!= SSTOP
) {
1927 p
->p_xstat
= signum
;
1928 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
1929 struct proc
*pp
= p
->p_pptr
;
1931 pp
->si_pid
= p
->p_pid
;
1932 pp
->si_status
= p
->p_xstat
;
1933 pp
->si_code
= CLD_STOPPED
;
1934 pp
->si_uid
= p
->p_cred
->p_ruid
;
1935 psignal(pp
, SIGCHLD
);
1944 * Signals ignored by default have been dealt
1945 * with already, since their bits are on in
1951 * Kill signal always sets process running and
1955 * Process will be running after 'run'
1959 thread_abort(sig_thread_act
);
1965 * Let the process run. If it's sleeping on an
1966 * event, it remains so.
1968 if (p
->p_flag
& P_TTYSLEEP
) {
1969 p
->p_flag
&= ~P_TTYSLEEP
;
1970 wakeup(&p
->p_siglist
);
1972 (void) task_resume(sig_task
);
1974 uth
->uu_siglist
&= ~mask
;
1975 p
->p_siglist
&= ~mask
;
1981 * All other signals wake up the process, but don't
1990 * If we're being traced (possibly because someone attached us
1991 * while we were stopped), check for a signal from the debugger.
1993 if (p
->p_stat
== SSTOP
) {
1994 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0) {
1995 uth
->uu_siglist
|= sigmask(p
->p_xstat
);
1996 p
->p_siglist
|= sigmask(p
->p_xstat
);
2000 * setrunnable(p) in BSD and
2001 * Wake up the thread if it is interruptible.
2004 thread_abort_safely(sig_thread_act
);
2016 thread_t self
= current_thread();
2018 p
->exit_thread
= self
;
2019 (void) task_suspend(p
->task
);
2026 thread_t self
= current_thread();
2028 while (p
->sigwait
|| p
->exit_thread
) {
2029 if (p
->exit_thread
) {
2030 if (p
->exit_thread
!= self
) {
2032 * Already exiting - no signals.
2034 thread_abort(current_act());
2038 if(assert_wait_possible()) {
2039 assert_wait((caddr_t
)&p
->sigwait_thread
,
2040 (THREAD_INTERRUPTIBLE
));
2043 thread_block(THREAD_CONTINUE_NULL
);
2045 if (thread_should_abort(self
)) {
2047 * Terminate request - clean up.
2056 * If the current process has received a signal (should be caught or cause
2057 * termination, should interrupt current syscall), return the signal number.
2058 * Stop signals with default action are processed immediately, then cleared;
2059 * they aren't returned. This is checked after each entry to the system for
2060 * a syscall or trap (though this can usually be done without calling issignal
2061 * by checking the pending signal masks in the CURSIG macro.) The normal call
2064 * while (signum = CURSIG(curproc))
2069 register struct proc
*p
;
2071 register int signum
, mask
, prop
, sigbits
;
2072 task_t task
= p
->task
;
2073 thread_t cur_thread
;
2074 thread_act_t cur_act
;
2076 struct uthread
* ut
;
2080 cur_thread
= current_thread();
2081 cur_act
= current_act();
2084 if(rdebug_proc
&& (p
== rdebug_proc
)) {
2087 #endif /* SIGNAL_DEBUG */
2091 * Try to grab the signal lock.
2093 if (sig_try_locked(p
) <= 0) {
2098 ut
= get_bsdthread_info(cur_act
);
2100 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2102 if (p
->p_flag
& P_PPWAIT
)
2103 sigbits
&= ~stopsigmask
;
2104 if (sigbits
== 0) { /* no signal to send */
2108 signum
= ffs((long)sigbits
);
2109 mask
= sigmask(signum
);
2110 prop
= sigprop
[signum
];
2113 * We should see pending but ignored signals
2114 * only if P_TRACED was on when they were posted.
2116 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2117 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2118 p
->p_siglist
&= ~mask
; /* take the signal! */
2121 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2123 register task_t task
;
2125 * If traced, always stop, and stay
2126 * stopped until released by the debugger.
2128 /* ptrace debugging */
2129 p
->p_xstat
= signum
;
2131 if (p
->p_flag
& P_SIGEXC
) {
2133 p
->sigwait_thread
= cur_act
;
2135 p
->p_flag
&= ~P_WAITED
;
2136 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2137 p
->p_siglist
&= ~mask
; /* clear the old signal */
2139 do_bsdexception(EXC_SOFTWARE
, EXC_SOFT_SIGNAL
, signum
);
2142 // panic("Unsupportef gdb option \n");;
2143 pp
->si_pid
= p
->p_pid
;
2144 pp
->si_status
= p
->p_xstat
;
2145 pp
->si_code
= CLD_TRAPPED
;
2146 pp
->si_uid
= p
->p_cred
->p_ruid
;
2147 psignal(pp
, SIGCHLD
);
2149 * XXX Have to really stop for debuggers;
2150 * XXX stop() doesn't do the right thing.
2151 * XXX Inline the task_suspend because we
2152 * XXX have to diddle Unix state in the
2158 p
->sigwait_thread
= cur_act
;
2160 p
->p_flag
&= ~P_WAITED
;
2161 ut
->uu_siglist
&= ~mask
; /* clear the old signal */
2162 p
->p_siglist
&= ~mask
; /* clear the old signal */
2164 wakeup((caddr_t
)p
->p_pptr
);
2166 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
2167 thread_block(THREAD_CONTINUE_NULL
);
2172 p
->sigwait_thread
= NULL
;
2173 wakeup((caddr_t
)&p
->sigwait_thread
);
2176 * This code is to detect when gdb is killed
2177 * even as the traced program is attached.
2178 * pgsignal would get the SIGKILL to traced program
2179 * That's what we are trying to see (I hope)
2181 if (ut
->uu_siglist
& sigmask(SIGKILL
)) {
2183 * Wait event may still be outstanding;
2184 * clear it, since sig_lock_to_exit will
2187 clear_wait(current_thread(), THREAD_INTERRUPTED
);
2188 sig_lock_to_exit(p
);
2190 * Since this thread will be resumed
2191 * to allow the current syscall to
2192 * be completed, must save u_qsave
2193 * before calling exit(). (Since exit()
2194 * calls closef() which can trash u_qsave.)
2197 exit1(p
,signum
, (int *)NULL
);
2202 * We may have to quit
2204 if (thread_should_abort(current_thread())) {
2209 * If parent wants us to take the signal,
2210 * then it will leave it in p->p_xstat;
2211 * otherwise we just look for signals again.
2213 signum
= p
->p_xstat
;
2217 * Put the new signal into p_siglist. If the
2218 * signal is being masked, look for other signals.
2220 mask
= sigmask(signum
);
2221 ut
->uu_siglist
|= mask
;
2222 p
->p_siglist
|= mask
; /* just for lame ones looking here */
2223 if (ut
->uu_sigmask
& mask
)
2228 * Decide whether the signal should be returned.
2229 * Return the signal's number, or fall through
2230 * to clear it from the pending mask.
2233 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2237 * Don't take default actions on system processes.
2239 if (p
->p_pptr
->p_pid
== 0) {
2242 * Are you sure you want to ignore SIGSEGV
2245 printf("Process (pid %d) got signal %d\n",
2248 break; /* == ignore */
2252 * If there is a pending stop signal to process
2253 * with default action, stop here,
2254 * then clear the signal. However,
2255 * if process is member of an orphaned
2256 * process group, ignore tty stop signals.
2258 if (prop
& SA_STOP
) {
2259 if (p
->p_flag
& P_TRACED
||
2260 (p
->p_pgrp
->pg_jobc
== 0 &&
2262 break; /* == ignore */
2263 if (p
->p_stat
!= SSTOP
) {
2264 p
->p_xstat
= signum
;
2266 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0) {
2268 pp
->si_pid
= p
->p_pid
;
2269 pp
->si_status
= p
->p_xstat
;
2270 pp
->si_code
= CLD_STOPPED
;
2271 pp
->si_uid
= p
->p_cred
->p_ruid
;
2272 psignal(pp
, SIGCHLD
);
2276 } else if (prop
& SA_IGNORE
) {
2278 * Except for SIGCONT, shouldn't get here.
2279 * Default action is to ignore; drop it.
2281 break; /* == ignore */
2283 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2284 p
->p_siglist
&= ~mask
; /* take the signal! */
2292 * Masking above should prevent us ever trying
2293 * to take action on an ignored signal other
2294 * than SIGCONT, unless process is traced.
2296 if ((prop
& SA_CONT
) == 0 &&
2297 (p
->p_flag
& P_TRACED
) == 0)
2298 printf("issignal\n");
2299 break; /* == ignore */
2303 * This signal has an action, let
2304 * postsig() process it.
2306 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2307 p
->p_siglist
&= ~mask
; /* take the signal! */
2311 ut
->uu_siglist
&= ~mask
; /* take the signal! */
2312 p
->p_siglist
&= ~mask
; /* take the signal! */
2317 /* called from _sleep */
2320 register struct proc
*p
;
2322 register int signum
, mask
, prop
, sigbits
;
2323 task_t task
= p
->task
;
2324 thread_t cur_thread
;
2325 thread_act_t cur_act
;
2327 struct uthread
* ut
;
2331 cur_thread
= current_thread();
2332 cur_act
= current_act();
2334 ut
= get_bsdthread_info(cur_act
);
2336 if (ut
->uu_siglist
== 0)
2339 if (((ut
->uu_siglist
& ~ut
->uu_sigmask
) == 0) && ((p
->p_flag
& P_TRACED
) == 0))
2342 sigbits
= ut
->uu_siglist
& ~ut
->uu_sigmask
;
2345 if (p
->p_flag
& P_PPWAIT
)
2346 sigbits
&= ~stopsigmask
;
2347 if (sigbits
== 0) { /* no signal to send */
2351 signum
= ffs((long)sigbits
);
2352 mask
= sigmask(signum
);
2353 prop
= sigprop
[signum
];
2356 * We should see pending but ignored signals
2357 * only if P_TRACED was on when they were posted.
2359 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
2362 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
2364 * Put the new signal into p_siglist. If the
2365 * signal is being masked, look for other signals.
2367 mask
= sigmask(signum
);
2368 if (ut
->uu_sigmask
& mask
)
2374 * Decide whether the signal should be returned.
2375 * Return the signal's number, or fall through
2376 * to clear it from the pending mask.
2379 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
2383 * Don't take default actions on system processes.
2385 if (p
->p_pptr
->p_pid
== 0) {
2388 * Are you sure you want to ignore SIGSEGV
2391 printf("Process (pid %d) got signal %d\n",
2394 break; /* == ignore */
2398 * If there is a pending stop signal to process
2399 * with default action, stop here,
2400 * then clear the signal. However,
2401 * if process is member of an orphaned
2402 * process group, ignore tty stop signals.
2404 if (prop
& SA_STOP
) {
2405 if (p
->p_flag
& P_TRACED
||
2406 (p
->p_pgrp
->pg_jobc
== 0 &&
2408 break; /* == ignore */
2411 } else if (prop
& SA_IGNORE
) {
2413 * Except for SIGCONT, shouldn't get here.
2414 * Default action is to ignore; drop it.
2416 break; /* == ignore */
2424 * Masking above should prevent us ever trying
2425 * to take action on an ignored signal other
2426 * than SIGCONT, unless process is traced.
2428 if ((prop
& SA_CONT
) == 0 &&
2429 (p
->p_flag
& P_TRACED
) == 0)
2430 printf("issignal\n");
2431 break; /* == ignore */
2435 * This signal has an action, let
2436 * postsig() process it.
2440 sigbits
&= ~mask
; /* take the signal! */
2446 * Put the argument process into the stopped state and notify the parent
2447 * via wakeup. Signals are handled elsewhere. The process must not be
2452 register struct proc
*p
;
2455 p
->p_flag
&= ~P_WAITED
;
2456 wakeup((caddr_t
)p
->p_pptr
);
2457 (void) task_suspend(p
->task
); /*XXX*/
2461 * Take the action for the specified signal
2462 * from the current set of pending signals.
2466 register int signum
;
2468 register struct proc
*p
= current_proc();
2469 register struct sigacts
*ps
= p
->p_sigacts
;
2470 register sig_t action
;
2472 int mask
, returnmask
;
2473 struct uthread
* ut
;
2479 * This must be called on master cpu
2481 if (cpu_number() != master_cpu
)
2482 panic("psig not on master");
2487 * Try to grab the signal lock.
2489 if (sig_try_locked(p
) <= 0) {
2494 ut
= (struct uthread
*)get_bsdthread_info(current_act());
2495 mask
= sigmask(signum
);
2496 ut
->uu_siglist
&= ~mask
;
2497 p
->p_siglist
&= ~mask
;
2498 action
= ps
->ps_sigact
[signum
];
2500 if (KTRPOINT(p
, KTR_PSIG
))
2501 ktrpsig(p
->p_tracep
,
2502 signum
, action
, ut
->uu_flag
& USAS_OLDMASK
?
2503 &ut
->uu_oldmask
: &ut
->uu_sigmask
, 0, -1);
2505 if (action
== SIG_DFL
) {
2507 * Default action, where the default is to kill
2508 * the process. (Other cases were ignored above.)
2510 /* called with signal_lock() held */
2511 sigexit_locked(p
, signum
);
2516 * If we get here, the signal must be caught.
2519 if (action
== SIG_IGN
|| (ut
->uu_sigmask
& mask
))
2521 "postsig: processing masked or ignored signal\n");
2524 * Set the new mask value and also defer further
2525 * occurences of this signal.
2527 * Special case: user has done a sigpause. Here the
2528 * current mask is not of interest, but rather the
2529 * mask from before the sigpause is what we want
2530 * restored after the signal processing is completed.
2532 if (ut
->uu_flag
& USAS_OLDMASK
) {
2533 returnmask
= ut
->uu_oldmask
;
2534 ut
->uu_flag
&= ~USAS_OLDMASK
;
2537 returnmask
= ut
->uu_sigmask
;
2538 ut
->uu_sigmask
|= ps
->ps_catchmask
[signum
];
2539 if ((ps
->ps_signodefer
& mask
) == 0)
2540 ut
->uu_sigmask
|= mask
;
2541 if ((signum
!= SIGILL
) && (signum
!= SIGTRAP
) && (ps
->ps_sigreset
& mask
)) {
2542 if ((signum
!= SIGCONT
) && (sigprop
[signum
] & SA_IGNORE
))
2543 p
->p_sigignore
|= mask
;
2544 ps
->ps_sigact
[signum
] = SIG_DFL
;
2545 ps
->ps_siginfo
&= ~mask
;
2546 ps
->ps_signodefer
&= ~mask
;
2549 /* Needs to disable to run in user mode */
2550 if (signum
== SIGFPE
) {
2551 thread_enable_fpe(current_act(), 0);
2553 #endif /* __ppc__ */
2555 if (ps
->ps_sig
!= signum
) {
2561 p
->p_stats
->p_ru
.ru_nsignals
++;
2562 sendsig(p
, action
, signum
, returnmask
, code
);
2568 * Force the current process to exit with the specified signal, dumping core
2569 * if appropriate. We bypass the normal tests for masked and caught signals,
2570 * allowing unrecoverable failures to terminate the process without changing
2571 * signal state. Mark the accounting record with the signal termination.
2572 * If dumping core, save the signal number for the debugger. Calls exit and
2575 /* called with signal lock */
2577 sigexit_locked(p
, signum
)
2578 register struct proc
*p
;
2582 sig_lock_to_exit(p
);
2583 p
->p_acflag
|= AXSIG
;
2584 if (sigprop
[signum
] & SA_CORE
) {
2585 p
->p_sigacts
->ps_sig
= signum
;
2586 if (coredump(p
) == 0)
2587 signum
|= WCOREFLAG
;
2590 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
2595 bsd_ast(thread_act_t thr_act
)
2597 struct proc
*p
= current_proc();
2598 struct uthread
*ut
= get_bsdthread_info(thr_act
);
2601 boolean_t funnel_state
;
2602 static bsd_init_done
= 0;
2607 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2609 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
2610 pc
= get_useraddr();
2611 addupc_task(p
, pc
, 1);
2612 p
->p_flag
&= ~P_OWEUPC
;
2615 if (CHECK_SIGNALS(p
, current_thread(), ut
)) {
2616 while (signum
= issignal(p
))
2619 if (!bsd_init_done
) {
2620 extern void bsdinit_task(void);
2626 (void) thread_funnel_set(kernel_flock
, FALSE
);
2630 * Follwing routines are called using callout from bsd_hardclock
2631 * so that psignals are called in a thread context and are funneled
2634 psignal_vtalarm(struct proc
*p
)
2636 boolean_t funnel_state
;
2640 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2641 psignal_lock(p
, SIGVTALRM
, 1);
2642 (void) thread_funnel_set(kernel_flock
, FALSE
);
2646 psignal_xcpu(struct proc
*p
)
2648 boolean_t funnel_state
;
2652 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2653 psignal_lock(p
, SIGXCPU
, 1);
2654 (void) thread_funnel_set(kernel_flock
, FALSE
);
2658 psignal_sigprof(struct proc
*p
)
2660 boolean_t funnel_state
;
2664 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2665 psignal_lock(p
, SIGPROF
, 1);
2666 (void) thread_funnel_set(kernel_flock
, FALSE
);
2669 /* ptrace set runnalbe */
2671 pt_setrunnable(struct proc
*p
)
2677 if (p
->p_flag
& P_TRACED
) {
2680 wakeup((caddr_t
)&(p
->sigwait
));
2692 exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
2693 extern kern_return_t
bsd_exception(int, exception_data_type_t codes
[], int);
2697 return(bsd_exception(exc
, codes
, 2));