2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995-1998 Apple Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
63 #define SIGPROP /* include signal properties table */
64 #include <sys/param.h>
65 #include <sys/signalvar.h>
66 #include <sys/resourcevar.h>
67 #include <sys/namei.h>
68 #include <sys/vnode.h>
70 #include <sys/systm.h>
71 #include <sys/timeb.h>
72 #include <sys/times.h>
76 #include <sys/kernel.h>
78 #include <sys/ktrace.h>
79 #include <sys/syslog.h>
83 #include <sys/mount.h>
85 #include <kern/cpu_number.h>
88 #include <sys/user.h> /* for coredump */
89 #include <kern/ast.h> /* for APC support */
90 #include <kern/thread.h>
91 #include <kern/thread_call.h>
93 void stop
__P((struct proc
*p
));
94 int cansignal
__P((struct proc
*, struct pcred
*, struct proc
*, int));
95 int killpg1
__P((struct proc
*, int, int, int));
96 void sigexit_locked
__P((struct proc
*, int));
97 void setsigvec
__P((struct proc
*, int, struct sigaction
*));
98 void exit1
__P((struct proc
*, int, int *));
99 int signal_lock
__P((struct proc
*));
100 int signal_unlock
__P((struct proc
*));
101 void signal_setast
__P((thread_act_t
*));
102 void signal_clearast
__P((thread_act_t
*));
103 void psignal_lock
__P((struct proc
*, int, int, int));
106 void ram_printf
__P((int));
108 unsigned int rdebug_proc
=0;
116 #endif /* SIGNAL_DEBUG */
118 signal_lock(struct proc
*p
)
124 int register sp
, *fp
, numsaved
;
126 __asm__
volatile("mr %0,r1" : "=r" (sp
));
128 fp
= (int *)*((int *)sp
);
129 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
130 p
->lockpc
[numsaved
] = fp
[2];
137 #endif /* SIGNAL_DEBUG */
140 error
= lockmgr(&p
->signal_lock
, LK_EXCLUSIVE
, 0, (struct proc
*)0);
147 signal_unlock(struct proc
*p
)
152 int register sp
, *fp
, numsaved
;
154 __asm__
volatile("mr %0,r1" : "=r" (sp
));
156 fp
= (int *)*((int *)sp
);
157 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
158 p
->unlockpc
[numsaved
] = fp
[2];
165 #endif /* SIGNAL_DEBUG */
167 return(lockmgr(&p
->signal_lock
, LK_RELEASE
, (simple_lock_t
)0, (struct proc
*)0));
171 signal_setast(sig_actthread
)
172 thread_act_t
*sig_actthread
;
174 thread_ast_set(sig_actthread
, AST_BSD
);
175 if ((thread_act_t
*)current_act() == sig_actthread
)
180 signal_clearast(sig_actthread
)
181 thread_act_t
*sig_actthread
;
183 thread_ast_clear(sig_actthread
, AST_BSD
);
184 if ((thread_act_t
*)current_act() == sig_actthread
)
189 * Can process p, with pcred pc, send the signal signum to process q?
192 cansignal(p
, pc
, q
, signum
)
198 if (pc
->pc_ucred
->cr_uid
== 0)
199 return (1); /* root can always signal */
201 if (signum
== SIGCONT
&& q
->p_session
== p
->p_session
)
202 return (1); /* SIGCONT in session */
205 * Using kill(), only certain signals can be sent to setugid
208 if (q
->p_flag
& P_SUGID
) {
221 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
222 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
223 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
224 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
231 * because the P_SUGID test exists, this has extra tests which
234 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
235 pc
->p_ruid
== q
->p_cred
->p_svuid
||
236 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
237 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_svuid
||
238 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
239 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
244 struct sigaction_args
{
246 struct sigaction
*nsa
;
247 struct sigaction
*osa
;
252 sigaction(p
, uap
, retval
)
254 register struct sigaction_args
*uap
;
257 struct sigaction vec
;
258 register struct sigaction
*sa
;
259 register struct sigacts
*ps
= p
->p_sigacts
;
263 signum
= uap
->signum
;
264 if (signum
<= 0 || signum
>= NSIG
||
265 signum
== SIGKILL
|| signum
== SIGSTOP
)
269 sa
->sa_handler
= ps
->ps_sigact
[signum
];
270 sa
->sa_mask
= ps
->ps_catchmask
[signum
];
271 bit
= sigmask(signum
);
273 if ((ps
->ps_sigonstack
& bit
) != 0)
274 sa
->sa_flags
|= SA_ONSTACK
;
275 if ((ps
->ps_sigintr
& bit
) == 0)
276 sa
->sa_flags
|= SA_RESTART
;
277 if (p
->p_flag
& P_NOCLDSTOP
)
278 sa
->sa_flags
|= SA_NOCLDSTOP
;
279 if (error
= copyout((caddr_t
)sa
, (caddr_t
)uap
->osa
,
284 if (error
= copyin((caddr_t
)uap
->nsa
, (caddr_t
)sa
,
287 setsigvec(p
, signum
, sa
);
293 reset_sigbits(thread_act_t th_act
, int bit
)
296 ut
= get_bsdthread_info(th_act
);
303 clear_sigbits (struct proc
*p
, int bit
)
305 task_t task
= p
->task
;
307 p
->p_siglist
&= ~(bit
);
308 task_act_iterate_wth_args(task
, reset_sigbits
, bit
);
314 setsigvec(p
, signum
, sa
)
315 register struct proc
*p
;
317 register struct sigaction
*sa
;
319 register struct sigacts
*ps
= p
->p_sigacts
;
322 bit
= sigmask(signum
);
324 * Change setting atomically.
326 ps
->ps_sigact
[signum
] = sa
->sa_handler
;
327 ps
->ps_catchmask
[signum
] = sa
->sa_mask
&~ sigcantmask
;
328 if ((sa
->sa_flags
& SA_RESTART
) == 0)
329 ps
->ps_sigintr
|= bit
;
331 ps
->ps_sigintr
&= ~bit
;
332 if (sa
->sa_flags
& SA_ONSTACK
)
333 ps
->ps_sigonstack
|= bit
;
335 ps
->ps_sigonstack
&= ~bit
;
336 if (sa
->sa_flags
& SA_USERTRAMP
)
337 ps
->ps_usertramp
|= bit
;
339 ps
->ps_usertramp
&= ~bit
;
340 if (signum
== SIGCHLD
) {
341 if (sa
->sa_flags
& SA_NOCLDSTOP
)
342 p
->p_flag
|= P_NOCLDSTOP
;
344 p
->p_flag
&= ~P_NOCLDSTOP
;
347 * Set bit in p_sigignore for signals that are set to SIG_IGN,
348 * and for signals set to SIG_DFL where the default is to ignore.
349 * However, don't put SIGCONT in p_sigignore,
350 * as we have to restart the process.
352 if (sa
->sa_handler
== SIG_IGN
||
353 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
354 p
->p_siglist
&= ~bit
; /* never to be seen again */
356 * If this is a thread signal, clean out the
359 if (bit
& threadmask
) {
360 register task_t task
= p
->task
;
362 task_act_iterate_wth_args(task
, reset_sigbits
, bit
);
364 if (signum
!= SIGCONT
)
365 p
->p_sigignore
|= bit
; /* easier in psignal */
366 p
->p_sigcatch
&= ~bit
;
368 p
->p_sigignore
&= ~bit
;
369 if (sa
->sa_handler
== SIG_DFL
)
370 p
->p_sigcatch
&= ~bit
;
372 p
->p_sigcatch
|= bit
;
377 * Initialize signal state for process 0;
378 * set to ignore signals that are ignored by default.
386 for (i
= 0; i
< NSIG
; i
++)
387 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
388 p
->p_sigignore
|= sigmask(i
);
392 * Reset signals for an exec of the specified process.
396 register struct proc
*p
;
398 register struct sigacts
*ps
= p
->p_sigacts
;
399 register int nc
, mask
;
402 * Reset caught signals. Held signals remain held
403 * through p_sigmask (unless they were caught,
404 * and are now ignored by default).
406 while (p
->p_sigcatch
) {
407 nc
= ffs((long)p
->p_sigcatch
);
409 p
->p_sigcatch
&= ~mask
;
410 if (sigprop
[nc
] & SA_IGNORE
) {
412 p
->p_sigignore
|= mask
;
413 p
->p_siglist
&= ~mask
;
415 ps
->ps_sigact
[nc
] = SIG_DFL
;
418 * Reset stack state to the user stack.
419 * Clear set of signals caught on the signal stack.
421 ps
->ps_sigstk
.ss_flags
= SA_DISABLE
;
422 ps
->ps_sigstk
.ss_size
= 0;
423 ps
->ps_sigstk
.ss_sp
= 0;
428 * Manipulate signal mask.
429 * Note that we receive new mask, not pointer,
430 * and return old mask as return value;
431 * the library stub does the rest.
433 struct sigprocmask_args
{
438 sigprocmask(p
, uap
, retval
)
439 register struct proc
*p
;
440 struct sigprocmask_args
*uap
;
445 *retval
= p
->p_sigmask
;
449 p
->p_sigmask
|= uap
->mask
&~ sigcantmask
;
453 p
->p_sigmask
&= ~(uap
->mask
);
454 signal_setast(current_act());
458 p
->p_sigmask
= uap
->mask
&~ sigcantmask
;
459 signal_setast(current_act());
471 sigpending(p
, uap
, retval
)
477 *retval
= p
->p_siglist
;
483 * Generalized interface signal handler, 4.3-compatible.
485 struct osigvec_args
{
492 osigvec(p
, uap
, retval
)
494 register struct osigvec_args
*uap
;
498 register struct sigacts
*ps
= p
->p_sigacts
;
499 register struct sigvec
*sv
;
503 signum
= uap
->signum
;
504 if (signum
<= 0 || signum
>= NSIG
||
505 signum
== SIGKILL
|| signum
== SIGSTOP
)
509 *(sig_t
*)&sv
->sv_handler
= ps
->ps_sigact
[signum
];
510 sv
->sv_mask
= ps
->ps_catchmask
[signum
];
511 bit
= sigmask(signum
);
513 if ((ps
->ps_sigonstack
& bit
) != 0)
514 sv
->sv_flags
|= SV_ONSTACK
;
515 if ((ps
->ps_sigintr
& bit
) != 0)
516 sv
->sv_flags
|= SV_INTERRUPT
;
517 if (p
->p_flag
& P_NOCLDSTOP
)
518 sv
->sv_flags
|= SA_NOCLDSTOP
;
519 if (error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->osv
,
524 if (error
= copyin((caddr_t
)uap
->nsv
, (caddr_t
)sv
,
527 sv
->sv_flags
^= SA_RESTART
; /* opposite of SV_INTERRUPT */
528 setsigvec(p
, signum
, (struct sigaction
*)sv
);
533 struct osigblock_args
{
537 osigblock(p
, uap
, retval
)
538 register struct proc
*p
;
539 struct osigblock_args
*uap
;
543 *retval
= p
->p_sigmask
;
544 p
->p_sigmask
|= uap
->mask
&~ sigcantmask
;
548 struct osigsetmask_args
{
552 osigsetmask(p
, uap
, retval
)
554 struct osigsetmask_args
*uap
;
558 *retval
= p
->p_sigmask
;
559 p
->p_sigmask
= uap
->mask
&~ sigcantmask
;
562 #endif /* COMPAT_43 */
565 * Suspend process until signal, providing mask to be set
566 * in the meantime. Note nonstandard calling convention:
567 * libc stub passes mask, not pointer, to save a copyin.
573 unix_syscall_return(EINTR
);
576 struct sigsuspend_args
{
582 sigsuspend(p
, uap
, retval
)
583 register struct proc
*p
;
584 struct sigsuspend_args
*uap
;
587 register struct sigacts
*ps
= p
->p_sigacts
;
590 * When returning from sigpause, we want
591 * the old mask to be restored after the
592 * signal handler has finished. Thus, we
593 * save it here and mark the sigacts structure
596 ps
->ps_oldmask
= p
->p_sigmask
;
597 ps
->ps_flags
|= SAS_OLDMASK
;
598 p
->p_sigmask
= uap
->mask
&~ sigcantmask
;
599 (void) tsleep0((caddr_t
) p
, PPAUSE
|PCATCH
, "pause", 0, sigcontinue
);
600 /* always return EINTR rather than ERESTART... */
605 struct osigstack_args
{
606 struct sigstack
*nss
;
607 struct sigstack
*oss
;
611 osigstack(p
, uap
, retval
)
613 register struct osigstack_args
*uap
;
621 ss
.ss_sp
= psp
->ps_sigstk
.ss_sp
;
622 ss
.ss_onstack
= psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
;
623 if (uap
->oss
&& (error
= copyout((caddr_t
)&ss
,
624 (caddr_t
)uap
->oss
, sizeof (struct sigstack
))))
626 if (uap
->nss
&& (error
= copyin((caddr_t
)uap
->nss
,
627 (caddr_t
)&ss
, sizeof (ss
))) == 0) {
628 psp
->ps_sigstk
.ss_sp
= ss
.ss_sp
;
629 psp
->ps_sigstk
.ss_size
= 0;
630 psp
->ps_sigstk
.ss_flags
|= ss
.ss_onstack
& SA_ONSTACK
;
631 psp
->ps_flags
|= SAS_ALTSTACK
;
635 #endif /* COMPAT_43 */
637 struct sigaltstack_args
{
638 struct sigaltstack
*nss
;
639 struct sigaltstack
*oss
;
643 sigaltstack(p
, uap
, retval
)
645 register struct sigaltstack_args
*uap
;
649 struct sigaltstack ss
;
653 if ((psp
->ps_flags
& SAS_ALTSTACK
) == 0)
654 psp
->ps_sigstk
.ss_flags
|= SA_DISABLE
;
655 if (uap
->oss
&& (error
= copyout((caddr_t
)&psp
->ps_sigstk
,
656 (caddr_t
)uap
->oss
, sizeof (struct sigaltstack
))))
660 if (error
= copyin((caddr_t
)uap
->nss
, (caddr_t
)&ss
,
663 if (ss
.ss_flags
& SA_DISABLE
) {
664 if (psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
)
666 psp
->ps_flags
&= ~SAS_ALTSTACK
;
667 psp
->ps_sigstk
.ss_flags
= ss
.ss_flags
;
670 if (ss
.ss_size
< MINSIGSTKSZ
)
672 psp
->ps_flags
|= SAS_ALTSTACK
;
683 kill(cp
, uap
, retval
)
684 register struct proc
*cp
;
685 register struct kill_args
*uap
;
688 register struct proc
*p
;
689 register struct pcred
*pc
= cp
->p_cred
;
691 if ((u_int
)uap
->signum
>= NSIG
)
694 /* kill single process */
695 if ((p
= pfind(uap
->pid
)) == NULL
)
697 if (!cansignal(cp
, pc
, p
, uap
->signum
))
700 psignal(p
, uap
->signum
);
704 case -1: /* broadcast signal */
705 return (killpg1(cp
, uap
->signum
, 0, 1));
706 case 0: /* signal own process group */
707 return (killpg1(cp
, uap
->signum
, 0, 0));
708 default: /* negative explicit process group */
709 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0));
715 struct okillpg_args
{
721 okillpg(p
, uap
, retval
)
723 register struct okillpg_args
*uap
;
727 if ((u_int
)uap
->signum
>= NSIG
)
729 return (killpg1(p
, uap
->signum
, uap
->pgid
, 0));
731 #endif /* COMPAT_43 */
734 * Common code for kill process group/broadcast kill.
735 * cp is calling process.
738 killpg1(cp
, signum
, pgid
, all
)
739 register struct proc
*cp
;
740 int signum
, pgid
, all
;
742 register struct proc
*p
;
743 register struct pcred
*pc
= cp
->p_cred
;
751 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
752 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
753 p
== cp
|| !cansignal(cp
, pc
, p
, signum
))
762 * zero pgid means send to my process group.
770 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
771 p
= p
->p_pglist
.le_next
) {
772 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
773 p
->p_stat
== SZOMB
||
774 !cansignal(cp
, pc
, p
, signum
))
781 return (nfound
? 0 : ESRCH
);
785 * Send a signal to a process group.
788 gsignal(pgid
, signum
)
793 if (pgid
&& (pgrp
= pgfind(pgid
)))
794 pgsignal(pgrp
, signum
, 0);
798 * Send a signal to a process group. If checktty is 1,
799 * limit to members which have a controlling terminal.
802 pgsignal(pgrp
, signum
, checkctty
)
804 int signum
, checkctty
;
806 register struct proc
*p
;
809 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
810 p
= p
->p_pglist
.le_next
)
811 if (checkctty
== 0 || p
->p_flag
& P_CONTROLT
)
816 * Send a signal caused by a trap to a specific thread.
819 threadsignal(sig_actthread
, signum
, code
)
820 register thread_act_t
*sig_actthread
;
824 register struct uthread
*uth
;
825 register struct task
* sig_task
;
826 register struct proc
*p
;
829 if ((u_int
)signum
>= NSIG
|| signum
== 0)
832 mask
= sigmask(signum
);
833 if ((mask
& threadmask
) == 0)
835 sig_task
= get_threadtask(sig_actthread
);
836 p
= (struct proc
*)(get_bsdtask_info(sig_task
));
838 uth
= get_bsdthread_info(sig_actthread
);
839 if (uth
&& (uth
->uu_flag
& P_VFORK
))
842 if (!(p
->p_flag
& P_TRACED
) && (p
->p_sigignore
& mask
))
847 /* mark on process as well */
848 p
->p_siglist
|= mask
;
849 signal_setast(sig_actthread
);
855 register struct proc
*p
;
857 boolean_t funnel_state
;
858 register int sigbits
, mask
, signum
;
860 thread_funnel_set(kernel_flock
, TRUE
);
862 if (p
->p_sigpending
== 0)
869 sigbits
= p
->p_sigpending
;
872 signum
= ffs((long)sigbits
);
873 mask
= sigmask(signum
);
874 p
->p_sigpending
&= ~mask
;
876 psignal_lock(p
, signum
, 0, 0);
880 p
->p_flag
&= ~P_SIGTHR
;
882 thread_funnel_set(kernel_flock
, FALSE
);
887 register struct proc
*p
;
890 psignal_lock(p
, signum
, 1, 1);
895 psignal_vfork(p
, new_task
, thr_act
, signum
)
896 register struct proc
*p
;
898 thread_act_t thr_act
;
903 register int s
, prop
;
904 register sig_t action
;
908 if ((u_int
)signum
>= NSIG
|| signum
== 0)
909 panic("psignal signal number");
910 mask
= sigmask(signum
);
911 prop
= sigprop
[signum
];
914 if(rdebug_proc
&& (p
== rdebug_proc
)) {
917 #endif /* SIGNAL_DEBUG */
919 if ((new_task
== TASK_NULL
) || (thr_act
== (thread_act_t
)NULL
) || is_kerneltask(new_task
))
926 * proc is traced, always give parent a chance.
930 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
931 (p
->p_flag
& P_TRACED
) == 0)
935 p
->p_siglist
&= ~stopsigmask
;
937 if (prop
& SA_STOP
) {
939 * If sending a tty stop signal to a member of an orphaned
940 * process group, discard the signal here if the action
941 * is default; don't stop the process below if sleeping,
942 * and don't clear any pending SIGCONT.
944 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
947 p
->p_siglist
&= ~contsigmask
;
949 p
->p_siglist
|= mask
;
951 /* Deliver signal to the activation passed in */
952 thread_ast_set(thr_act
, AST_BSD
);
955 * SIGKILL priority twiddling moved here from above because
956 * it needs sig_thread. Could merge it into large switch
957 * below if we didn't care about priority for tracing
958 * as SIGKILL's action is always SIG_DFL.
960 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
964 * we need to make changes here to get nice to work
965 * reset priority to BASEPRI_USER
971 * This Process is traced - wake it up (if not already
972 * stopped) so that it can discover the signal in
973 * issig() and stop for the parent.
975 if (p
->p_flag
& P_TRACED
) {
976 if (p
->p_stat
!= SSTOP
)
983 * If we're being traced (possibly because someone attached us
984 * while we were stopped), check for a signal from the debugger.
986 if (p
->p_stat
== SSTOP
) {
987 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
988 p
->p_siglist
|= sigmask(p
->p_xstat
);
992 * setrunnable(p) in BSD
1001 * Send the signal to the process. If the signal has an action, the action
1002 * is usually performed by the target process rather than the caller; we add
1003 * the signal to the set of pending signals for the process.
1006 * o When a stop signal is sent to a sleeping process that takes the
1007 * default action, the process is stopped without awakening it.
1008 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1009 * regardless of the signal action (eg, blocked or ignored).
1011 * Other ignored signals are discarded immediately.
1014 psignal_lock(p
, signum
, withlock
, pend
)
1015 register struct proc
*p
;
1016 register int signum
;
1017 register int withlock
;
1020 register int s
, prop
;
1021 register sig_t action
;
1022 thread_act_t sig_thread_act
;
1023 thread_t sig_thread
;
1024 register task_t sig_task
;
1025 register thread_t cur_thread
;
1026 thread_act_t
*cur_act
;
1030 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1031 panic("psignal signal number");
1032 mask
= sigmask(signum
);
1033 prop
= sigprop
[signum
];
1036 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1039 #endif /* SIGNAL_DEBUG */
1041 * We will need the task pointer later. Grab it now to
1042 * check for a zombie process. Also don't send signals
1043 * to kernel internal tasks.
1045 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
))
1049 * do not send signals to the process that has the thread
1050 * doing a reboot(). Not doing so will mark that thread aborted
1051 * and can cause IO failures wich will cause data loss.
1053 if (ISSET(p
->p_flag
, P_REBOOT
))
1057 * if the traced process is blocked waiting for
1058 * gdb then do not block the caller just pend
1059 * the signal. Setup a callout to process the
1060 * pended signal if not alreadu set
1062 if (pend
&& (p
->p_flag
& P_TRACED
) && p
->sigwait
) {
1063 p
->p_sigpending
|= mask
;
1064 if (!(p
->p_flag
& P_SIGTHR
)) {
1065 p
->p_flag
|= P_SIGTHR
;
1066 thread_call_func((thread_call_func_t
)psignal_pend
, p
,
1076 * If proc is traced, always give parent a chance.
1078 if (p
->p_flag
& P_TRACED
)
1082 * If the signal is being ignored,
1083 * then we forget about it immediately.
1084 * (Note: we don't set SIGCONT in p_sigignore,
1085 * and if it is set to SIG_IGN,
1086 * action will be SIG_DFL here.)
1088 if (p
->p_sigignore
& mask
)
1090 if (p
->p_sigmask
& mask
)
1092 else if (p
->p_sigcatch
& mask
)
1098 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1099 (p
->p_flag
& P_TRACED
) == 0)
1103 p
->p_siglist
&= ~stopsigmask
;
1105 if (prop
& SA_STOP
) {
1107 * If sending a tty stop signal to a member of an orphaned
1108 * process group, discard the signal here if the action
1109 * is default; don't stop the process below if sleeping,
1110 * and don't clear any pending SIGCONT.
1112 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1115 p
->p_siglist
&= ~contsigmask
;
1117 p
->p_siglist
|= mask
;
1120 * Defer further processing for signals which are held,
1121 * except that stopped processes must be continued by SIGCONT.
1123 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
))
1127 * Deliver the signal to the first thread in the task. This
1128 * allows single threaded applications which use signals to
1129 * be able to be linked with multithreaded libraries. We have
1130 * an implicit reference to the current_thread, but need
1131 * an explicit one otherwise. The thread reference keeps
1132 * the corresponding task data structures around too. This
1133 * reference is released by thread_deallocate.
1136 cur_thread
= current_thread(); /* this is a shuttle */
1137 cur_act
= current_act();
1139 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
1140 sig_thread_act
= p
->p_vforkact
;
1142 kret
= check_actforsig(sig_task
, sig_thread_act
, &sig_thread
, 1);
1143 if (kret
== KERN_SUCCESS
) {
1144 goto psig_foundthread
;
1148 /* If successful return with ast set */
1149 kret
= (kern_return_t
)get_signalact(sig_task
,
1150 &sig_thread_act
, &sig_thread
, 1);
1152 if ((kret
!= KERN_SUCCESS
) || (sig_thread_act
== THREAD_NULL
)) {
1154 /* if it is sigkill, may be we should
1155 * inject a thread to terminate
1158 printf("WARNING: no activation in psignal\n");
1162 #endif /* SIGNAL_DEBUG */
1167 if (sig_thread
== THREAD_NULL
) {
1169 printf("WARNING: valid act; but no shutte in psignal\n");
1172 /* FIXME : NO VALID SHUTTLE */
1178 * SIGKILL priority twiddling moved here from above because
1179 * it needs sig_thread. Could merge it into large switch
1180 * below if we didn't care about priority for tracing
1181 * as SIGKILL's action is always SIG_DFL.
1183 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1187 * we need to make changes here to get nice to work
1188 * reset priority to BASEPRI_USER
1194 * Process is traced - wake it up (if not already
1195 * stopped) so that it can discover the signal in
1196 * issig() and stop for the parent.
1198 if (p
->p_flag
& P_TRACED
) {
1199 if (p
->p_stat
!= SSTOP
)
1205 if (action
!= SIG_DFL
) {
1207 * User wants to catch the signal.
1208 * Wake up the thread, but don't un-suspend it
1209 * (except for SIGCONT).
1212 (void) task_resume(sig_task
);
1215 /* Default action - varies */
1216 if (mask
& stopsigmask
) {
1218 * These are the signals which by default
1221 * Don't clog system with children of init
1222 * stopped from the keyboard.
1224 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1225 psignal_lock(p
, SIGKILL
, 0, 1);
1226 p
->p_siglist
&= ~mask
;
1233 if (!is_thread_running(sig_thread
)) {
1234 /* Thread is not running
1235 * If task hasn't already been stopped by
1236 * a signal, stop it.
1238 p
->p_siglist
&= ~mask
;
1239 if (get_task_userstop(sig_task
) == 0) {
1241 * p_cursig must not be set, because
1242 * it will be psig()'d if it is not
1243 * zero, and the signal is being
1244 * handled here. But save the signal
1245 * in p_stopsig so WUNTRACED
1246 * option to wait can find it.
1248 p
->p_xstat
= signum
;
1249 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0)
1250 psignal(p
->p_pptr
, SIGCHLD
);
1254 /* unconditional check is bad */
1255 signal_clearast(sig_thread_act
);
1259 if (p
->p_stat
!= SZOMB
)
1260 signal_setast(cur_act
);
1267 * Signals ignored by default have been dealt
1268 * with already, since their bits are on in
1274 * Kill signal always sets process running and
1278 * Process will be running after 'run'
1282 thread_abort(sig_thread_act
);
1288 * Let the process run. If it's sleeping on an
1289 * event, it remains so.
1291 if (p
->p_flag
& P_TTYSLEEP
) {
1292 p
->p_flag
&= ~P_TTYSLEEP
;
1293 wakeup(&p
->p_siglist
);
1295 (void) task_resume(sig_task
);
1297 p
->p_siglist
&= ~mask
;
1300 /* do not clear AST as tcsh is sendig SIGTERM followed by
1301 * SIGCONT and the ast was getting cleared unconditinally
1302 * This is not right.
1304 signal_clearast(sig_thread_act
);
1310 * All other signals wake up the process, but don't
1319 * If we're being traced (possibly because someone attached us
1320 * while we were stopped), check for a signal from the debugger.
1322 if (p
->p_stat
== SSTOP
) {
1323 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
1324 p
->p_siglist
|= sigmask(p
->p_xstat
);
1328 * setrunnable(p) in BSD
1333 * Wake up the thread if it is interruptible.
1335 thread_abort_safely(sig_thread_act
);
1345 thread_t self
= current_thread();
1347 p
->exit_thread
= self
;
1348 (void) task_suspend(p
->task
);
1355 thread_t self
= current_thread();
1357 while (p
->sigwait
|| p
->exit_thread
) {
1358 if (p
->exit_thread
) {
1359 if (p
->exit_thread
!= self
) {
1361 * Already exiting - no signals.
1363 thread_abort(current_act());
1367 if(assert_wait_possible()) {
1368 assert_wait((caddr_t
)&p
->sigwait_thread
,
1369 (THREAD_INTERRUPTIBLE
));
1374 if (thread_should_abort(self
)) {
1376 * Terminate request - clean up.
1385 * If the current process has received a signal (should be caught or cause
1386 * termination, should interrupt current syscall), return the signal number.
1387 * Stop signals with default action are processed immediately, then cleared;
1388 * they aren't returned. This is checked after each entry to the system for
1389 * a syscall or trap (though this can usually be done without calling issignal
1390 * by checking the pending signal masks in the CURSIG macro.) The normal call
1393 * while (signum = CURSIG(curproc))
1398 register struct proc
*p
;
1400 register int signum
, mask
, prop
, sigbits
;
1401 task_t task
= p
->task
;
1402 thread_t cur_thread
;
1403 thread_act_t cur_act
;
1405 struct uthread
* ut
;
1408 cur_thread
= current_thread();
1409 cur_act
= current_act();
1415 * Try to grab the signal lock.
1417 if (sig_try_locked(p
) <= 0) {
1422 ut
= get_bsdthread_info(cur_act
);
1424 sigbits
= (ut
->uu_sig
|p
->p_siglist
) & ~p
->p_sigmask
;
1426 if (p
->p_flag
& P_PPWAIT
)
1427 sigbits
&= ~stopsigmask
;
1428 if (sigbits
== 0) { /* no signal to send */
1432 signum
= ffs((long)sigbits
);
1433 mask
= sigmask(signum
);
1434 prop
= sigprop
[signum
];
1436 if (mask
& threadmask
) {
1437 /* we can take this signal */
1438 ut
->uu_sig
&= ~mask
;
1442 * We should see pending but ignored signals
1443 * only if P_TRACED was on when they were posted.
1445 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
1446 p
->p_siglist
&= ~mask
; /* take the signal! */
1449 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
1451 register task_t task
;
1453 * If traced, always stop, and stay
1454 * stopped until released by the debugger.
1456 /* ptrace debugging */
1457 p
->p_xstat
= signum
;
1458 psignal(p
->p_pptr
, SIGCHLD
);
1460 * XXX Have to really stop for debuggers;
1461 * XXX stop() doesn't do the right thing.
1462 * XXX Inline the task_suspend because we
1463 * XXX have to diddle Unix state in the
1469 p
->sigwait_thread
= cur_act
;
1471 p
->p_flag
&= ~P_WAITED
;
1472 p
->p_siglist
&= ~mask
; /* clear the old signal */
1474 wakeup((caddr_t
)p
->p_pptr
);
1475 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
1478 p
->sigwait_thread
= NULL
;
1479 wakeup((caddr_t
)&p
->sigwait_thread
);
1482 * This code is to detect when gdb is killed
1483 * even as the traced program is attached.
1484 * pgsignal would get the SIGKILL to traced program
1485 * That's what we are trying to see (I hope)
1487 if (p
->p_siglist
& sigmask(SIGKILL
)) {
1489 * Wait event may still be outstanding;
1490 * clear it, since sig_lock_to_exit will
1493 clear_wait(current_thread(), THREAD_INTERRUPTED
);
1494 sig_lock_to_exit(p
);
1496 * Since this thread will be resumed
1497 * to allow the current syscall to
1498 * be completed, must save u_qsave
1499 * before calling exit(). (Since exit()
1500 * calls closef() which can trash u_qsave.)
1503 exit1(p
,signum
, (int *)NULL
);
1508 * We may have to quit
1510 if (thread_should_abort(current_thread())) {
1515 * If parent wants us to take the signal,
1516 * then it will leave it in p->p_xstat;
1517 * otherwise we just look for signals again.
1519 signum
= p
->p_xstat
;
1523 * Put the new signal into p_siglist. If the
1524 * signal is being masked, look for other signals.
1526 mask
= sigmask(signum
);
1527 if (mask
& threadmask
)
1530 p
->p_siglist
|= mask
;
1531 if (p
->p_sigmask
& mask
)
1536 * Decide whether the signal should be returned.
1537 * Return the signal's number, or fall through
1538 * to clear it from the pending mask.
1541 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
1545 * Don't take default actions on system processes.
1547 if (p
->p_pptr
->p_pid
== 0) {
1550 * Are you sure you want to ignore SIGSEGV
1553 printf("Process (pid %d) got signal %d\n",
1556 break; /* == ignore */
1560 * If there is a pending stop signal to process
1561 * with default action, stop here,
1562 * then clear the signal. However,
1563 * if process is member of an orphaned
1564 * process group, ignore tty stop signals.
1566 if (prop
& SA_STOP
) {
1567 if (p
->p_flag
& P_TRACED
||
1568 (p
->p_pgrp
->pg_jobc
== 0 &&
1570 break; /* == ignore */
1571 p
->p_xstat
= signum
;
1573 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0)
1574 psignal(p
->p_pptr
, SIGCHLD
);
1577 * We may have to quit
1579 if (thread_should_abort(current_thread())) {
1584 } else if (prop
& SA_IGNORE
) {
1586 * Except for SIGCONT, shouldn't get here.
1587 * Default action is to ignore; drop it.
1589 break; /* == ignore */
1591 p
->p_siglist
&= ~mask
; /* take the signal! */
1592 p
->p_sigpending
&= ~mask
; /* take the pending signal */
1600 * Masking above should prevent us ever trying
1601 * to take action on an ignored signal other
1602 * than SIGCONT, unless process is traced.
1604 if ((prop
& SA_CONT
) == 0 &&
1605 (p
->p_flag
& P_TRACED
) == 0)
1606 printf("issignal\n");
1607 break; /* == ignore */
1611 * This signal has an action, let
1612 * postsig() process it.
1614 p
->p_siglist
&= ~mask
; /* take the signal! */
1615 p
->p_sigpending
&= ~mask
; /* take the pending signal */
1619 p
->p_siglist
&= ~mask
; /* take the signal! */
1620 p
->p_sigpending
&= ~mask
; /* take the pending signal */
1625 /* called from _sleep */
1628 register struct proc
*p
;
1630 register int signum
, mask
, prop
, sigbits
;
1631 task_t task
= p
->task
;
1632 thread_t cur_thread
;
1633 thread_act_t cur_act
;
1635 struct uthread
* ut
;
1638 if (p
->p_siglist
== 0)
1641 if (((p
->p_siglist
& ~p
->p_sigmask
) == 0) && ((p
->p_flag
& P_TRACED
) == 0))
1644 cur_thread
= current_thread();
1645 cur_act
= current_act();
1647 ut
= get_bsdthread_info(cur_act
);
1649 sigbits
= (ut
->uu_sig
| p
->p_siglist
) & ~p
->p_sigmask
;
1652 if (p
->p_flag
& P_PPWAIT
)
1653 sigbits
&= ~stopsigmask
;
1654 if (sigbits
== 0) { /* no signal to send */
1658 signum
= ffs((long)sigbits
);
1659 mask
= sigmask(signum
);
1660 prop
= sigprop
[signum
];
1663 * We should see pending but ignored signals
1664 * only if P_TRACED was on when they were posted.
1666 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
1669 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
1671 * Put the new signal into p_siglist. If the
1672 * signal is being masked, look for other signals.
1674 mask
= sigmask(signum
);
1675 if (p
->p_sigmask
& mask
)
1681 * Decide whether the signal should be returned.
1682 * Return the signal's number, or fall through
1683 * to clear it from the pending mask.
1686 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
1690 * Don't take default actions on system processes.
1692 if (p
->p_pptr
->p_pid
== 0) {
1695 * Are you sure you want to ignore SIGSEGV
1698 printf("Process (pid %d) got signal %d\n",
1701 break; /* == ignore */
1705 * If there is a pending stop signal to process
1706 * with default action, stop here,
1707 * then clear the signal. However,
1708 * if process is member of an orphaned
1709 * process group, ignore tty stop signals.
1711 if (prop
& SA_STOP
) {
1712 if (p
->p_flag
& P_TRACED
||
1713 (p
->p_pgrp
->pg_jobc
== 0 &&
1715 break; /* == ignore */
1718 } else if (prop
& SA_IGNORE
) {
1720 * Except for SIGCONT, shouldn't get here.
1721 * Default action is to ignore; drop it.
1723 break; /* == ignore */
1731 * Masking above should prevent us ever trying
1732 * to take action on an ignored signal other
1733 * than SIGCONT, unless process is traced.
1735 if ((prop
& SA_CONT
) == 0 &&
1736 (p
->p_flag
& P_TRACED
) == 0)
1737 printf("issignal\n");
1738 break; /* == ignore */
1742 * This signal has an action, let
1743 * postsig() process it.
1747 sigbits
&= ~mask
; /* take the signal! */
1753 * Put the argument process into the stopped state and notify the parent
1754 * via wakeup. Signals are handled elsewhere. The process must not be
1759 register struct proc
*p
;
1762 p
->p_flag
&= ~P_WAITED
;
1763 wakeup((caddr_t
)p
->p_pptr
);
1764 (void) task_suspend(p
->task
); /*XXX*/
1768 * Take the action for the specified signal
1769 * from the current set of pending signals.
1773 register int signum
;
1775 register struct proc
*p
= current_proc();
1776 register struct sigacts
*ps
= p
->p_sigacts
;
1777 register sig_t action
;
1779 int mask
, returnmask
;
1785 * This must be called on master cpu
1787 if (cpu_number() != master_cpu
)
1788 panic("psig not on master");
1793 * Try to grab the signal lock.
1795 if (sig_try_locked(p
) <= 0) {
1800 mask
= sigmask(signum
);
1801 p
->p_siglist
&= ~mask
;
1802 action
= ps
->ps_sigact
[signum
];
1804 if (KTRPOINT(p
, KTR_PSIG
))
1805 ktrpsig(p
->p_tracep
,
1806 signum
, action
, ps
->ps_flags
& SAS_OLDMASK
?
1807 ps
->ps_oldmask
: p
->p_sigmask
, 0);
1809 if (action
== SIG_DFL
) {
1811 * Default action, where the default is to kill
1812 * the process. (Other cases were ignored above.)
1814 /* called with signal_lock() held */
1815 sigexit_locked(p
, signum
);
1820 * If we get here, the signal must be caught.
1823 if (action
== SIG_IGN
|| (p
->p_sigmask
& mask
))
1825 "postsig: processing masked or ignored signal\n");
1828 * Set the new mask value and also defer further
1829 * occurences of this signal.
1831 * Special case: user has done a sigpause. Here the
1832 * current mask is not of interest, but rather the
1833 * mask from before the sigpause is what we want
1834 * restored after the signal processing is completed.
1836 if (ps
->ps_flags
& SAS_OLDMASK
) {
1837 returnmask
= ps
->ps_oldmask
;
1838 ps
->ps_flags
&= ~SAS_OLDMASK
;
1840 returnmask
= p
->p_sigmask
;
1841 p
->p_sigmask
|= ps
->ps_catchmask
[signum
] | mask
;
1842 if (ps
->ps_sig
!= signum
) {
1848 p
->p_stats
->p_ru
.ru_nsignals
++;
1849 sendsig(p
, action
, signum
, returnmask
, code
);
1855 * Force the current process to exit with the specified signal, dumping core
1856 * if appropriate. We bypass the normal tests for masked and caught signals,
1857 * allowing unrecoverable failures to terminate the process without changing
1858 * signal state. Mark the accounting record with the signal termination.
1859 * If dumping core, save the signal number for the debugger. Calls exit and
1862 /* called with signal lock */
1864 sigexit_locked(p
, signum
)
1865 register struct proc
*p
;
1869 sig_lock_to_exit(p
);
1870 p
->p_acflag
|= AXSIG
;
1871 if (sigprop
[signum
] & SA_CORE
) {
1872 p
->p_sigacts
->ps_sig
= signum
;
1873 if (coredump(p
) == 0)
1874 signum
|= WCOREFLAG
;
1877 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
1882 bsd_ast(thread_act_t thr_act
)
1884 struct proc
*p
= current_proc();
1885 struct uthread
*ut
= get_bsdthread_info(thr_act
);
1888 boolean_t funnel_state
;
1893 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1895 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
1896 pc
= get_useraddr();
1897 addupc_task(p
, pc
, 1);
1898 p
->p_flag
&= ~P_OWEUPC
;
1901 if (CHECK_SIGNALS(p
, current_thread(), ut
)) {
1902 while (signum
= issignal(p
))
1907 (void) thread_funnel_set(kernel_flock
, FALSE
);
1911 * Follwing routines are called using callout from bsd_hardclock
1912 * so that psignals are called in a thread context and are funneled
1915 psignal_vtalarm(struct proc
*p
)
1917 boolean_t funnel_state
;
1921 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1922 psignal_lock(p
, SIGVTALRM
, 1, 1);
1923 (void) thread_funnel_set(kernel_flock
, FALSE
);
1927 psignal_xcpu(struct proc
*p
)
1929 boolean_t funnel_state
;
1933 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1934 psignal_lock(p
, SIGXCPU
, 1, 1);
1935 (void) thread_funnel_set(kernel_flock
, FALSE
);
1939 psignal_sigprof(struct proc
*p
)
1941 boolean_t funnel_state
;
1945 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1946 psignal_lock(p
, SIGPROF
, 1, 1);
1947 (void) thread_funnel_set(kernel_flock
, FALSE
);
1950 /* ptrace set runnalbe */
1952 pt_setrunnable(struct proc
*p
)
1958 if (p
->p_flag
& P_TRACED
) {
1961 wakeup((caddr_t
)&(p
->sigwait
));