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
)
123 int register sp
, *fp
, numsaved
;
125 __asm__
volatile("mr %0,r1" : "=r" (sp
));
127 fp
= (int *)*((int *)sp
);
128 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
129 p
->lockpc
[numsaved
] = fp
[2];
136 #endif /* SIGNAL_DEBUG */
138 return(lockmgr(&p
->signal_lock
, LK_EXCLUSIVE
, 0, (struct proc
*)0));
142 signal_unlock(struct proc
*p
)
147 int register sp
, *fp
, numsaved
;
149 __asm__
volatile("mr %0,r1" : "=r" (sp
));
151 fp
= (int *)*((int *)sp
);
152 for (numsaved
= 0; numsaved
< 3; numsaved
++) {
153 p
->unlockpc
[numsaved
] = fp
[2];
160 #endif /* SIGNAL_DEBUG */
162 return(lockmgr(&p
->signal_lock
, LK_RELEASE
, (simple_lock_t
)0, (struct proc
*)0));
166 signal_setast(sig_actthread
)
167 thread_act_t
*sig_actthread
;
169 thread_ast_set(sig_actthread
, AST_BSD
);
170 if ((thread_act_t
*)current_act() == sig_actthread
)
175 signal_clearast(sig_actthread
)
176 thread_act_t
*sig_actthread
;
178 thread_ast_clear(sig_actthread
, AST_BSD
);
179 if ((thread_act_t
*)current_act() == sig_actthread
)
184 * Can process p, with pcred pc, send the signal signum to process q?
187 cansignal(p
, pc
, q
, signum
)
193 if (pc
->pc_ucred
->cr_uid
== 0)
194 return (1); /* root can always signal */
196 if (signum
== SIGCONT
&& q
->p_session
== p
->p_session
)
197 return (1); /* SIGCONT in session */
200 * Using kill(), only certain signals can be sent to setugid
203 if (q
->p_flag
& P_SUGID
) {
216 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
217 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
218 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
219 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
226 * because the P_SUGID test exists, this has extra tests which
229 if (pc
->p_ruid
== q
->p_cred
->p_ruid
||
230 pc
->p_ruid
== q
->p_cred
->p_svuid
||
231 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_ruid
||
232 pc
->pc_ucred
->cr_uid
== q
->p_cred
->p_svuid
||
233 pc
->p_ruid
== q
->p_ucred
->cr_uid
||
234 pc
->pc_ucred
->cr_uid
== q
->p_ucred
->cr_uid
)
239 struct sigaction_args
{
241 struct sigaction
*nsa
;
242 struct sigaction
*osa
;
247 sigaction(p
, uap
, retval
)
249 register struct sigaction_args
*uap
;
252 struct sigaction vec
;
253 register struct sigaction
*sa
;
254 register struct sigacts
*ps
= p
->p_sigacts
;
258 signum
= uap
->signum
;
259 if (signum
<= 0 || signum
>= NSIG
||
260 signum
== SIGKILL
|| signum
== SIGSTOP
)
264 sa
->sa_handler
= ps
->ps_sigact
[signum
];
265 sa
->sa_mask
= ps
->ps_catchmask
[signum
];
266 bit
= sigmask(signum
);
268 if ((ps
->ps_sigonstack
& bit
) != 0)
269 sa
->sa_flags
|= SA_ONSTACK
;
270 if ((ps
->ps_sigintr
& bit
) == 0)
271 sa
->sa_flags
|= SA_RESTART
;
272 if (p
->p_flag
& P_NOCLDSTOP
)
273 sa
->sa_flags
|= SA_NOCLDSTOP
;
274 if (error
= copyout((caddr_t
)sa
, (caddr_t
)uap
->osa
,
279 if (error
= copyin((caddr_t
)uap
->nsa
, (caddr_t
)sa
,
282 setsigvec(p
, signum
, sa
);
288 reset_sigbits(thread_act_t th_act
, int bit
)
291 ut
= get_bsdthread_info(th_act
);
298 clear_sigbits (struct proc
*p
, int bit
)
300 task_t task
= p
->task
;
302 p
->p_siglist
&= ~(bit
);
303 task_act_iterate_wth_args(task
, reset_sigbits
, bit
);
309 setsigvec(p
, signum
, sa
)
310 register struct proc
*p
;
312 register struct sigaction
*sa
;
314 register struct sigacts
*ps
= p
->p_sigacts
;
317 bit
= sigmask(signum
);
319 * Change setting atomically.
321 ps
->ps_sigact
[signum
] = sa
->sa_handler
;
322 ps
->ps_catchmask
[signum
] = sa
->sa_mask
&~ sigcantmask
;
323 if ((sa
->sa_flags
& SA_RESTART
) == 0)
324 ps
->ps_sigintr
|= bit
;
326 ps
->ps_sigintr
&= ~bit
;
327 if (sa
->sa_flags
& SA_ONSTACK
)
328 ps
->ps_sigonstack
|= bit
;
330 ps
->ps_sigonstack
&= ~bit
;
331 if (sa
->sa_flags
& SA_USERTRAMP
)
332 ps
->ps_usertramp
|= bit
;
334 ps
->ps_usertramp
&= ~bit
;
335 if (signum
== SIGCHLD
) {
336 if (sa
->sa_flags
& SA_NOCLDSTOP
)
337 p
->p_flag
|= P_NOCLDSTOP
;
339 p
->p_flag
&= ~P_NOCLDSTOP
;
342 * Set bit in p_sigignore for signals that are set to SIG_IGN,
343 * and for signals set to SIG_DFL where the default is to ignore.
344 * However, don't put SIGCONT in p_sigignore,
345 * as we have to restart the process.
347 if (sa
->sa_handler
== SIG_IGN
||
348 (sigprop
[signum
] & SA_IGNORE
&& sa
->sa_handler
== SIG_DFL
)) {
349 p
->p_siglist
&= ~bit
; /* never to be seen again */
351 * If this is a thread signal, clean out the
354 if (bit
& threadmask
) {
355 register task_t task
= p
->task
;
357 task_act_iterate_wth_args(task
, reset_sigbits
, bit
);
359 if (signum
!= SIGCONT
)
360 p
->p_sigignore
|= bit
; /* easier in psignal */
361 p
->p_sigcatch
&= ~bit
;
363 p
->p_sigignore
&= ~bit
;
364 if (sa
->sa_handler
== SIG_DFL
)
365 p
->p_sigcatch
&= ~bit
;
367 p
->p_sigcatch
|= bit
;
372 * Initialize signal state for process 0;
373 * set to ignore signals that are ignored by default.
381 for (i
= 0; i
< NSIG
; i
++)
382 if (sigprop
[i
] & SA_IGNORE
&& i
!= SIGCONT
)
383 p
->p_sigignore
|= sigmask(i
);
387 * Reset signals for an exec of the specified process.
391 register struct proc
*p
;
393 register struct sigacts
*ps
= p
->p_sigacts
;
394 register int nc
, mask
;
397 * Reset caught signals. Held signals remain held
398 * through p_sigmask (unless they were caught,
399 * and are now ignored by default).
401 while (p
->p_sigcatch
) {
402 nc
= ffs((long)p
->p_sigcatch
);
404 p
->p_sigcatch
&= ~mask
;
405 if (sigprop
[nc
] & SA_IGNORE
) {
407 p
->p_sigignore
|= mask
;
408 p
->p_siglist
&= ~mask
;
410 ps
->ps_sigact
[nc
] = SIG_DFL
;
413 * Reset stack state to the user stack.
414 * Clear set of signals caught on the signal stack.
416 ps
->ps_sigstk
.ss_flags
= SA_DISABLE
;
417 ps
->ps_sigstk
.ss_size
= 0;
418 ps
->ps_sigstk
.ss_sp
= 0;
423 * Manipulate signal mask.
424 * Note that we receive new mask, not pointer,
425 * and return old mask as return value;
426 * the library stub does the rest.
428 struct sigprocmask_args
{
433 sigprocmask(p
, uap
, retval
)
434 register struct proc
*p
;
435 struct sigprocmask_args
*uap
;
440 *retval
= p
->p_sigmask
;
444 p
->p_sigmask
|= uap
->mask
&~ sigcantmask
;
448 p
->p_sigmask
&= ~(uap
->mask
);
449 signal_setast(current_act());
453 p
->p_sigmask
= uap
->mask
&~ sigcantmask
;
454 signal_setast(current_act());
466 sigpending(p
, uap
, retval
)
472 *retval
= p
->p_siglist
;
478 * Generalized interface signal handler, 4.3-compatible.
480 struct osigvec_args
{
487 osigvec(p
, uap
, retval
)
489 register struct osigvec_args
*uap
;
493 register struct sigacts
*ps
= p
->p_sigacts
;
494 register struct sigvec
*sv
;
498 signum
= uap
->signum
;
499 if (signum
<= 0 || signum
>= NSIG
||
500 signum
== SIGKILL
|| signum
== SIGSTOP
)
504 *(sig_t
*)&sv
->sv_handler
= ps
->ps_sigact
[signum
];
505 sv
->sv_mask
= ps
->ps_catchmask
[signum
];
506 bit
= sigmask(signum
);
508 if ((ps
->ps_sigonstack
& bit
) != 0)
509 sv
->sv_flags
|= SV_ONSTACK
;
510 if ((ps
->ps_sigintr
& bit
) != 0)
511 sv
->sv_flags
|= SV_INTERRUPT
;
512 if (p
->p_flag
& P_NOCLDSTOP
)
513 sv
->sv_flags
|= SA_NOCLDSTOP
;
514 if (error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->osv
,
519 if (error
= copyin((caddr_t
)uap
->nsv
, (caddr_t
)sv
,
522 sv
->sv_flags
^= SA_RESTART
; /* opposite of SV_INTERRUPT */
523 setsigvec(p
, signum
, (struct sigaction
*)sv
);
528 struct osigblock_args
{
532 osigblock(p
, uap
, retval
)
533 register struct proc
*p
;
534 struct osigblock_args
*uap
;
538 *retval
= p
->p_sigmask
;
539 p
->p_sigmask
|= uap
->mask
&~ sigcantmask
;
543 struct osigsetmask_args
{
547 osigsetmask(p
, uap
, retval
)
549 struct osigsetmask_args
*uap
;
553 *retval
= p
->p_sigmask
;
554 p
->p_sigmask
= uap
->mask
&~ sigcantmask
;
557 #endif /* COMPAT_43 */
560 * Suspend process until signal, providing mask to be set
561 * in the meantime. Note nonstandard calling convention:
562 * libc stub passes mask, not pointer, to save a copyin.
568 unix_syscall_return(EINTR
);
571 struct sigsuspend_args
{
577 sigsuspend(p
, uap
, retval
)
578 register struct proc
*p
;
579 struct sigsuspend_args
*uap
;
582 register struct sigacts
*ps
= p
->p_sigacts
;
585 * When returning from sigpause, we want
586 * the old mask to be restored after the
587 * signal handler has finished. Thus, we
588 * save it here and mark the sigacts structure
591 ps
->ps_oldmask
= p
->p_sigmask
;
592 ps
->ps_flags
|= SAS_OLDMASK
;
593 p
->p_sigmask
= uap
->mask
&~ sigcantmask
;
594 (void) tsleep0((caddr_t
) p
, PPAUSE
|PCATCH
, "pause", 0, sigcontinue
);
595 /* always return EINTR rather than ERESTART... */
600 struct osigstack_args
{
601 struct sigstack
*nss
;
602 struct sigstack
*oss
;
606 osigstack(p
, uap
, retval
)
608 register struct osigstack_args
*uap
;
616 ss
.ss_sp
= psp
->ps_sigstk
.ss_sp
;
617 ss
.ss_onstack
= psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
;
618 if (uap
->oss
&& (error
= copyout((caddr_t
)&ss
,
619 (caddr_t
)uap
->oss
, sizeof (struct sigstack
))))
621 if (uap
->nss
&& (error
= copyin((caddr_t
)uap
->nss
,
622 (caddr_t
)&ss
, sizeof (ss
))) == 0) {
623 psp
->ps_sigstk
.ss_sp
= ss
.ss_sp
;
624 psp
->ps_sigstk
.ss_size
= 0;
625 psp
->ps_sigstk
.ss_flags
|= ss
.ss_onstack
& SA_ONSTACK
;
626 psp
->ps_flags
|= SAS_ALTSTACK
;
630 #endif /* COMPAT_43 */
632 struct sigaltstack_args
{
633 struct sigaltstack
*nss
;
634 struct sigaltstack
*oss
;
638 sigaltstack(p
, uap
, retval
)
640 register struct sigaltstack_args
*uap
;
644 struct sigaltstack ss
;
648 if ((psp
->ps_flags
& SAS_ALTSTACK
) == 0)
649 psp
->ps_sigstk
.ss_flags
|= SA_DISABLE
;
650 if (uap
->oss
&& (error
= copyout((caddr_t
)&psp
->ps_sigstk
,
651 (caddr_t
)uap
->oss
, sizeof (struct sigaltstack
))))
655 if (error
= copyin((caddr_t
)uap
->nss
, (caddr_t
)&ss
,
658 if (ss
.ss_flags
& SA_DISABLE
) {
659 if (psp
->ps_sigstk
.ss_flags
& SA_ONSTACK
)
661 psp
->ps_flags
&= ~SAS_ALTSTACK
;
662 psp
->ps_sigstk
.ss_flags
= ss
.ss_flags
;
665 if (ss
.ss_size
< MINSIGSTKSZ
)
667 psp
->ps_flags
|= SAS_ALTSTACK
;
678 kill(cp
, uap
, retval
)
679 register struct proc
*cp
;
680 register struct kill_args
*uap
;
683 register struct proc
*p
;
684 register struct pcred
*pc
= cp
->p_cred
;
686 if ((u_int
)uap
->signum
>= NSIG
)
689 /* kill single process */
690 if ((p
= pfind(uap
->pid
)) == NULL
)
692 if (!cansignal(cp
, pc
, p
, uap
->signum
))
695 psignal(p
, uap
->signum
);
699 case -1: /* broadcast signal */
700 return (killpg1(cp
, uap
->signum
, 0, 1));
701 case 0: /* signal own process group */
702 return (killpg1(cp
, uap
->signum
, 0, 0));
703 default: /* negative explicit process group */
704 return (killpg1(cp
, uap
->signum
, -(uap
->pid
), 0));
710 struct okillpg_args
{
716 okillpg(p
, uap
, retval
)
718 register struct okillpg_args
*uap
;
722 if ((u_int
)uap
->signum
>= NSIG
)
724 return (killpg1(p
, uap
->signum
, uap
->pgid
, 0));
726 #endif /* COMPAT_43 */
729 * Common code for kill process group/broadcast kill.
730 * cp is calling process.
733 killpg1(cp
, signum
, pgid
, all
)
734 register struct proc
*cp
;
735 int signum
, pgid
, all
;
737 register struct proc
*p
;
738 register struct pcred
*pc
= cp
->p_cred
;
746 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
747 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
748 p
== cp
|| !cansignal(cp
, pc
, p
, signum
))
757 * zero pgid means send to my process group.
765 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
766 p
= p
->p_pglist
.le_next
) {
767 if (p
->p_pid
<= 1 || p
->p_flag
& P_SYSTEM
||
768 p
->p_stat
== SZOMB
||
769 !cansignal(cp
, pc
, p
, signum
))
776 return (nfound
? 0 : ESRCH
);
780 * Send a signal to a process group.
783 gsignal(pgid
, signum
)
788 if (pgid
&& (pgrp
= pgfind(pgid
)))
789 pgsignal(pgrp
, signum
, 0);
793 * Send a signal to a process group. If checktty is 1,
794 * limit to members which have a controlling terminal.
797 pgsignal(pgrp
, signum
, checkctty
)
799 int signum
, checkctty
;
801 register struct proc
*p
;
804 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
805 p
= p
->p_pglist
.le_next
)
806 if (checkctty
== 0 || p
->p_flag
& P_CONTROLT
)
811 * Send a signal caused by a trap to a specific thread.
814 threadsignal(sig_actthread
, signum
, code
)
815 register thread_act_t
*sig_actthread
;
819 register struct uthread
*uth
;
820 register struct task
* sig_task
;
821 register struct proc
*p
;
824 if ((u_int
)signum
>= NSIG
|| signum
== 0)
827 mask
= sigmask(signum
);
828 if ((mask
& threadmask
) == 0)
830 sig_task
= get_threadtask(sig_actthread
);
831 p
= (struct proc
*)(get_bsdtask_info(sig_task
));
833 uth
= get_bsdthread_info(sig_actthread
);
834 if (uth
&& (uth
->uu_flag
& P_VFORK
))
837 if (!(p
->p_flag
& P_TRACED
) && (p
->p_sigignore
& mask
))
842 /* mark on process as well */
843 p
->p_siglist
|= mask
;
844 signal_setast(sig_actthread
);
850 register struct proc
*p
;
852 boolean_t funnel_state
;
853 register int sigbits
, mask
, signum
;
855 thread_funnel_set(kernel_flock
, TRUE
);
857 if (p
->p_sigpending
== 0)
864 sigbits
= p
->p_sigpending
;
867 signum
= ffs((long)sigbits
);
868 mask
= sigmask(signum
);
869 p
->p_sigpending
&= ~mask
;
871 psignal_lock(p
, signum
, 0, 0);
875 p
->p_flag
&= ~P_SIGTHR
;
877 thread_funnel_set(kernel_flock
, FALSE
);
882 register struct proc
*p
;
885 psignal_lock(p
, signum
, 1, 1);
890 psignal_vfork(p
, new_task
, thr_act
, signum
)
891 register struct proc
*p
;
893 thread_act_t thr_act
;
898 register int s
, prop
;
899 register sig_t action
;
903 if ((u_int
)signum
>= NSIG
|| signum
== 0)
904 panic("psignal signal number");
905 mask
= sigmask(signum
);
906 prop
= sigprop
[signum
];
909 if(rdebug_proc
&& (p
== rdebug_proc
)) {
912 #endif /* SIGNAL_DEBUG */
914 if ((new_task
== TASK_NULL
) || (thr_act
== (thread_act_t
)NULL
) || is_kerneltask(new_task
))
921 * proc is traced, always give parent a chance.
925 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
926 (p
->p_flag
& P_TRACED
) == 0)
930 p
->p_siglist
&= ~stopsigmask
;
932 if (prop
& SA_STOP
) {
934 * If sending a tty stop signal to a member of an orphaned
935 * process group, discard the signal here if the action
936 * is default; don't stop the process below if sleeping,
937 * and don't clear any pending SIGCONT.
939 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
942 p
->p_siglist
&= ~contsigmask
;
944 p
->p_siglist
|= mask
;
946 /* Deliver signal to the activation passed in */
947 thread_ast_set(thr_act
, AST_BSD
);
950 * SIGKILL priority twiddling moved here from above because
951 * it needs sig_thread. Could merge it into large switch
952 * below if we didn't care about priority for tracing
953 * as SIGKILL's action is always SIG_DFL.
955 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
959 * we need to make changes here to get nice to work
960 * reset priority to BASEPRI_USER
966 * This Process is traced - wake it up (if not already
967 * stopped) so that it can discover the signal in
968 * issig() and stop for the parent.
970 if (p
->p_flag
& P_TRACED
) {
971 if (p
->p_stat
!= SSTOP
)
978 * If we're being traced (possibly because someone attached us
979 * while we were stopped), check for a signal from the debugger.
981 if (p
->p_stat
== SSTOP
) {
982 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
983 p
->p_siglist
|= sigmask(p
->p_xstat
);
987 * setrunnable(p) in BSD
996 * Send the signal to the process. If the signal has an action, the action
997 * is usually performed by the target process rather than the caller; we add
998 * the signal to the set of pending signals for the process.
1001 * o When a stop signal is sent to a sleeping process that takes the
1002 * default action, the process is stopped without awakening it.
1003 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1004 * regardless of the signal action (eg, blocked or ignored).
1006 * Other ignored signals are discarded immediately.
1009 psignal_lock(p
, signum
, withlock
, pend
)
1010 register struct proc
*p
;
1011 register int signum
;
1012 register int withlock
;
1015 register int s
, prop
;
1016 register sig_t action
;
1017 thread_act_t sig_thread_act
;
1018 thread_t sig_thread
;
1019 register task_t sig_task
;
1020 register thread_t cur_thread
;
1021 thread_act_t
*cur_act
;
1025 if ((u_int
)signum
>= NSIG
|| signum
== 0)
1026 panic("psignal signal number");
1027 mask
= sigmask(signum
);
1028 prop
= sigprop
[signum
];
1031 if(rdebug_proc
&& (p
== rdebug_proc
)) {
1034 #endif /* SIGNAL_DEBUG */
1036 * We will need the task pointer later. Grab it now to
1037 * check for a zombie process. Also don't send signals
1038 * to kernel internal tasks.
1040 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
))
1044 * do not send signals to the process that has the thread
1045 * doing a reboot(). Not doing so will mark that thread aborted
1046 * and can cause IO failures wich will cause data loss.
1048 if (ISSET(p
->p_flag
, P_REBOOT
))
1052 * if the traced process is blocked waiting for
1053 * gdb then do not block the caller just pend
1054 * the signal. Setup a callout to process the
1055 * pended signal if not alreadu set
1057 if (pend
&& (p
->p_flag
& P_TRACED
) && p
->sigwait
) {
1058 p
->p_sigpending
|= mask
;
1059 if (!(p
->p_flag
& P_SIGTHR
)) {
1060 p
->p_flag
|= P_SIGTHR
;
1061 thread_call_func((thread_call_func_t
)psignal_pend
, p
,
1071 * If proc is traced, always give parent a chance.
1073 if (p
->p_flag
& P_TRACED
)
1077 * If the signal is being ignored,
1078 * then we forget about it immediately.
1079 * (Note: we don't set SIGCONT in p_sigignore,
1080 * and if it is set to SIG_IGN,
1081 * action will be SIG_DFL here.)
1083 if (p
->p_sigignore
& mask
)
1085 if (p
->p_sigmask
& mask
)
1087 else if (p
->p_sigcatch
& mask
)
1093 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
1094 (p
->p_flag
& P_TRACED
) == 0)
1098 p
->p_siglist
&= ~stopsigmask
;
1100 if (prop
& SA_STOP
) {
1102 * If sending a tty stop signal to a member of an orphaned
1103 * process group, discard the signal here if the action
1104 * is default; don't stop the process below if sleeping,
1105 * and don't clear any pending SIGCONT.
1107 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1110 p
->p_siglist
&= ~contsigmask
;
1112 p
->p_siglist
|= mask
;
1115 * Defer further processing for signals which are held,
1116 * except that stopped processes must be continued by SIGCONT.
1118 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
))
1122 * Deliver the signal to the first thread in the task. This
1123 * allows single threaded applications which use signals to
1124 * be able to be linked with multithreaded libraries. We have
1125 * an implicit reference to the current_thread, but need
1126 * an explicit one otherwise. The thread reference keeps
1127 * the corresponding task data structures around too. This
1128 * reference is released by thread_deallocate.
1131 cur_thread
= current_thread(); /* this is a shuttle */
1132 cur_act
= current_act();
1134 if ((p
->p_flag
& P_INVFORK
) && p
->p_vforkact
) {
1135 sig_thread_act
= p
->p_vforkact
;
1137 kret
= check_actforsig(sig_task
, sig_thread_act
, &sig_thread
, 1);
1138 if (kret
== KERN_SUCCESS
) {
1139 goto psig_foundthread
;
1143 /* If successful return with ast set */
1144 kret
= (kern_return_t
)get_signalact(sig_task
,
1145 &sig_thread_act
, &sig_thread
, 1);
1147 if ((kret
!= KERN_SUCCESS
) || (sig_thread_act
== THREAD_NULL
)) {
1149 /* if it is sigkill, may be we should
1150 * inject a thread to terminate
1153 printf("WARNING: no activation in psignal\n");
1157 #endif /* SIGNAL_DEBUG */
1162 if (sig_thread
== THREAD_NULL
) {
1164 printf("WARNING: valid act; but no shutte in psignal\n");
1167 /* FIXME : NO VALID SHUTTLE */
1173 * SIGKILL priority twiddling moved here from above because
1174 * it needs sig_thread. Could merge it into large switch
1175 * below if we didn't care about priority for tracing
1176 * as SIGKILL's action is always SIG_DFL.
1178 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1182 * we need to make changes here to get nice to work
1183 * reset priority to BASEPRI_USER
1189 * Process is traced - wake it up (if not already
1190 * stopped) so that it can discover the signal in
1191 * issig() and stop for the parent.
1193 if (p
->p_flag
& P_TRACED
) {
1194 if (p
->p_stat
!= SSTOP
)
1200 if (action
!= SIG_DFL
) {
1202 * User wants to catch the signal.
1203 * Wake up the thread, but don't un-suspend it
1204 * (except for SIGCONT).
1207 (void) task_resume(sig_task
);
1210 /* Default action - varies */
1211 if (mask
& stopsigmask
) {
1213 * These are the signals which by default
1216 * Don't clog system with children of init
1217 * stopped from the keyboard.
1219 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1220 psignal_lock(p
, SIGKILL
, 0, 1);
1221 p
->p_siglist
&= ~mask
;
1228 if (!is_thread_running(sig_thread
)) {
1229 /* Thread is not running
1230 * If task hasn't already been stopped by
1231 * a signal, stop it.
1233 p
->p_siglist
&= ~mask
;
1234 if (get_task_userstop(sig_task
) == 0) {
1236 * p_cursig must not be set, because
1237 * it will be psig()'d if it is not
1238 * zero, and the signal is being
1239 * handled here. But save the signal
1240 * in p_stopsig so WUNTRACED
1241 * option to wait can find it.
1243 p
->p_xstat
= signum
;
1244 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0)
1245 psignal(p
->p_pptr
, SIGCHLD
);
1249 /* unconditional check is bad */
1250 signal_clearast(sig_thread_act
);
1254 if (p
->p_stat
!= SZOMB
)
1255 signal_setast(cur_act
);
1262 * Signals ignored by default have been dealt
1263 * with already, since their bits are on in
1269 * Kill signal always sets process running and
1273 * Process will be running after 'run'
1277 thread_abort(sig_thread_act
);
1283 * Let the process run. If it's sleeping on an
1284 * event, it remains so.
1286 if (p
->p_flag
& P_TTYSLEEP
) {
1287 p
->p_flag
&= ~P_TTYSLEEP
;
1288 wakeup(&p
->p_siglist
);
1290 (void) task_resume(sig_task
);
1292 p
->p_siglist
&= ~mask
;
1295 /* do not clear AST as tcsh is sendig SIGTERM followed by
1296 * SIGCONT and the ast was getting cleared unconditinally
1297 * This is not right.
1299 signal_clearast(sig_thread_act
);
1305 * All other signals wake up the process, but don't
1314 * If we're being traced (possibly because someone attached us
1315 * while we were stopped), check for a signal from the debugger.
1317 if (p
->p_stat
== SSTOP
) {
1318 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
1319 p
->p_siglist
|= sigmask(p
->p_xstat
);
1323 * setrunnable(p) in BSD
1328 * Wake up the thread if it is interruptible.
1330 thread_abort_safely(sig_thread_act
);
1340 thread_t self
= current_thread();
1342 p
->exit_thread
= self
;
1343 (void) task_suspend(p
->task
);
1350 thread_t self
= current_thread();
1352 while (p
->sigwait
|| p
->exit_thread
) {
1353 if (p
->exit_thread
) {
1354 if (p
->exit_thread
!= self
) {
1356 * Already exiting - no signals.
1358 thread_abort(current_act());
1362 if(assert_wait_possible()) {
1363 assert_wait((caddr_t
)&p
->sigwait_thread
,
1364 (THREAD_INTERRUPTIBLE
));
1369 if (thread_should_abort(self
)) {
1371 * Terminate request - clean up.
1380 * If the current process has received a signal (should be caught or cause
1381 * termination, should interrupt current syscall), return the signal number.
1382 * Stop signals with default action are processed immediately, then cleared;
1383 * they aren't returned. This is checked after each entry to the system for
1384 * a syscall or trap (though this can usually be done without calling issignal
1385 * by checking the pending signal masks in the CURSIG macro.) The normal call
1388 * while (signum = CURSIG(curproc))
1393 register struct proc
*p
;
1395 register int signum
, mask
, prop
, sigbits
;
1396 task_t task
= p
->task
;
1397 thread_t cur_thread
;
1398 thread_act_t cur_act
;
1400 struct uthread
* ut
;
1403 cur_thread
= current_thread();
1404 cur_act
= current_act();
1410 * Try to grab the signal lock.
1412 if (sig_try_locked(p
) <= 0) {
1417 ut
= get_bsdthread_info(cur_act
);
1419 sigbits
= (ut
->uu_sig
|p
->p_siglist
) & ~p
->p_sigmask
;
1421 if (p
->p_flag
& P_PPWAIT
)
1422 sigbits
&= ~stopsigmask
;
1423 if (sigbits
== 0) { /* no signal to send */
1427 signum
= ffs((long)sigbits
);
1428 mask
= sigmask(signum
);
1429 prop
= sigprop
[signum
];
1431 if (mask
& threadmask
) {
1432 /* we can take this signal */
1433 ut
->uu_sig
&= ~mask
;
1437 * We should see pending but ignored signals
1438 * only if P_TRACED was on when they were posted.
1440 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
1441 p
->p_siglist
&= ~mask
; /* take the signal! */
1444 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
1446 register task_t task
;
1448 * If traced, always stop, and stay
1449 * stopped until released by the debugger.
1451 /* ptrace debugging */
1452 p
->p_xstat
= signum
;
1453 psignal(p
->p_pptr
, SIGCHLD
);
1455 * XXX Have to really stop for debuggers;
1456 * XXX stop() doesn't do the right thing.
1457 * XXX Inline the task_suspend because we
1458 * XXX have to diddle Unix state in the
1464 p
->sigwait_thread
= cur_act
;
1466 p
->p_flag
&= ~P_WAITED
;
1467 p
->p_siglist
&= ~mask
; /* clear the old signal */
1469 wakeup((caddr_t
)p
->p_pptr
);
1470 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
1473 p
->sigwait_thread
= NULL
;
1474 wakeup((caddr_t
)&p
->sigwait_thread
);
1477 * This code is to detect when gdb is killed
1478 * even as the traced program is attached.
1479 * pgsignal would get the SIGKILL to traced program
1480 * That's what we are trying to see (I hope)
1482 if (p
->p_siglist
& sigmask(SIGKILL
)) {
1484 * Wait event may still be outstanding;
1485 * clear it, since sig_lock_to_exit will
1488 clear_wait(current_thread(), THREAD_INTERRUPTED
);
1489 sig_lock_to_exit(p
);
1491 * Since this thread will be resumed
1492 * to allow the current syscall to
1493 * be completed, must save u_qsave
1494 * before calling exit(). (Since exit()
1495 * calls closef() which can trash u_qsave.)
1498 exit1(p
,signum
, (int *)NULL
);
1503 * We may have to quit
1505 if (thread_should_abort(current_thread())) {
1510 * If parent wants us to take the signal,
1511 * then it will leave it in p->p_xstat;
1512 * otherwise we just look for signals again.
1514 signum
= p
->p_xstat
;
1518 * Put the new signal into p_siglist. If the
1519 * signal is being masked, look for other signals.
1521 mask
= sigmask(signum
);
1522 if (mask
& threadmask
)
1525 p
->p_siglist
|= mask
;
1526 if (p
->p_sigmask
& mask
)
1531 * Decide whether the signal should be returned.
1532 * Return the signal's number, or fall through
1533 * to clear it from the pending mask.
1536 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
1540 * Don't take default actions on system processes.
1542 if (p
->p_pptr
->p_pid
== 0) {
1545 * Are you sure you want to ignore SIGSEGV
1548 printf("Process (pid %d) got signal %d\n",
1551 break; /* == ignore */
1555 * If there is a pending stop signal to process
1556 * with default action, stop here,
1557 * then clear the signal. However,
1558 * if process is member of an orphaned
1559 * process group, ignore tty stop signals.
1561 if (prop
& SA_STOP
) {
1562 if (p
->p_flag
& P_TRACED
||
1563 (p
->p_pgrp
->pg_jobc
== 0 &&
1565 break; /* == ignore */
1566 p
->p_xstat
= signum
;
1568 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0)
1569 psignal(p
->p_pptr
, SIGCHLD
);
1572 * We may have to quit
1574 if (thread_should_abort(current_thread())) {
1579 } else if (prop
& SA_IGNORE
) {
1581 * Except for SIGCONT, shouldn't get here.
1582 * Default action is to ignore; drop it.
1584 break; /* == ignore */
1586 p
->p_siglist
&= ~mask
; /* take the signal! */
1587 p
->p_sigpending
&= ~mask
; /* take the pending signal */
1595 * Masking above should prevent us ever trying
1596 * to take action on an ignored signal other
1597 * than SIGCONT, unless process is traced.
1599 if ((prop
& SA_CONT
) == 0 &&
1600 (p
->p_flag
& P_TRACED
) == 0)
1601 printf("issignal\n");
1602 break; /* == ignore */
1606 * This signal has an action, let
1607 * postsig() process it.
1609 p
->p_siglist
&= ~mask
; /* take the signal! */
1610 p
->p_sigpending
&= ~mask
; /* take the pending signal */
1614 p
->p_siglist
&= ~mask
; /* take the signal! */
1615 p
->p_sigpending
&= ~mask
; /* take the pending signal */
1620 /* called from _sleep */
1623 register struct proc
*p
;
1625 register int signum
, mask
, prop
, sigbits
;
1626 task_t task
= p
->task
;
1627 thread_t cur_thread
;
1628 thread_act_t cur_act
;
1630 struct uthread
* ut
;
1633 if (p
->p_siglist
== 0)
1636 if (((p
->p_siglist
& ~p
->p_sigmask
) == 0) && ((p
->p_flag
& P_TRACED
) == 0))
1639 cur_thread
= current_thread();
1640 cur_act
= current_act();
1642 ut
= get_bsdthread_info(cur_act
);
1644 sigbits
= (ut
->uu_sig
| p
->p_siglist
) & ~p
->p_sigmask
;
1647 if (p
->p_flag
& P_PPWAIT
)
1648 sigbits
&= ~stopsigmask
;
1649 if (sigbits
== 0) { /* no signal to send */
1653 signum
= ffs((long)sigbits
);
1654 mask
= sigmask(signum
);
1655 prop
= sigprop
[signum
];
1658 * We should see pending but ignored signals
1659 * only if P_TRACED was on when they were posted.
1661 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
1664 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
1666 * Put the new signal into p_siglist. If the
1667 * signal is being masked, look for other signals.
1669 mask
= sigmask(signum
);
1670 if (p
->p_sigmask
& mask
)
1676 * Decide whether the signal should be returned.
1677 * Return the signal's number, or fall through
1678 * to clear it from the pending mask.
1681 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
1685 * Don't take default actions on system processes.
1687 if (p
->p_pptr
->p_pid
== 0) {
1690 * Are you sure you want to ignore SIGSEGV
1693 printf("Process (pid %d) got signal %d\n",
1696 break; /* == ignore */
1700 * If there is a pending stop signal to process
1701 * with default action, stop here,
1702 * then clear the signal. However,
1703 * if process is member of an orphaned
1704 * process group, ignore tty stop signals.
1706 if (prop
& SA_STOP
) {
1707 if (p
->p_flag
& P_TRACED
||
1708 (p
->p_pgrp
->pg_jobc
== 0 &&
1710 break; /* == ignore */
1713 } else if (prop
& SA_IGNORE
) {
1715 * Except for SIGCONT, shouldn't get here.
1716 * Default action is to ignore; drop it.
1718 break; /* == ignore */
1726 * Masking above should prevent us ever trying
1727 * to take action on an ignored signal other
1728 * than SIGCONT, unless process is traced.
1730 if ((prop
& SA_CONT
) == 0 &&
1731 (p
->p_flag
& P_TRACED
) == 0)
1732 printf("issignal\n");
1733 break; /* == ignore */
1737 * This signal has an action, let
1738 * postsig() process it.
1742 sigbits
&= ~mask
; /* take the signal! */
1748 * Put the argument process into the stopped state and notify the parent
1749 * via wakeup. Signals are handled elsewhere. The process must not be
1754 register struct proc
*p
;
1757 p
->p_flag
&= ~P_WAITED
;
1758 wakeup((caddr_t
)p
->p_pptr
);
1759 (void) task_suspend(p
->task
); /*XXX*/
1763 * Take the action for the specified signal
1764 * from the current set of pending signals.
1768 register int signum
;
1770 register struct proc
*p
= current_proc();
1771 register struct sigacts
*ps
= p
->p_sigacts
;
1772 register sig_t action
;
1774 int mask
, returnmask
;
1780 * This must be called on master cpu
1782 if (cpu_number() != master_cpu
)
1783 panic("psig not on master");
1788 * Try to grab the signal lock.
1790 if (sig_try_locked(p
) <= 0) {
1795 mask
= sigmask(signum
);
1796 p
->p_siglist
&= ~mask
;
1797 action
= ps
->ps_sigact
[signum
];
1799 if (KTRPOINT(p
, KTR_PSIG
))
1800 ktrpsig(p
->p_tracep
,
1801 signum
, action
, ps
->ps_flags
& SAS_OLDMASK
?
1802 ps
->ps_oldmask
: p
->p_sigmask
, 0);
1804 if (action
== SIG_DFL
) {
1806 * Default action, where the default is to kill
1807 * the process. (Other cases were ignored above.)
1809 /* called with signal_lock() held */
1810 sigexit_locked(p
, signum
);
1815 * If we get here, the signal must be caught.
1818 if (action
== SIG_IGN
|| (p
->p_sigmask
& mask
))
1820 "postsig: processing masked or ignored signal\n");
1823 * Set the new mask value and also defer further
1824 * occurences of this signal.
1826 * Special case: user has done a sigpause. Here the
1827 * current mask is not of interest, but rather the
1828 * mask from before the sigpause is what we want
1829 * restored after the signal processing is completed.
1831 if (ps
->ps_flags
& SAS_OLDMASK
) {
1832 returnmask
= ps
->ps_oldmask
;
1833 ps
->ps_flags
&= ~SAS_OLDMASK
;
1835 returnmask
= p
->p_sigmask
;
1836 p
->p_sigmask
|= ps
->ps_catchmask
[signum
] | mask
;
1837 if (ps
->ps_sig
!= signum
) {
1843 p
->p_stats
->p_ru
.ru_nsignals
++;
1844 sendsig(p
, action
, signum
, returnmask
, code
);
1850 * Force the current process to exit with the specified signal, dumping core
1851 * if appropriate. We bypass the normal tests for masked and caught signals,
1852 * allowing unrecoverable failures to terminate the process without changing
1853 * signal state. Mark the accounting record with the signal termination.
1854 * If dumping core, save the signal number for the debugger. Calls exit and
1857 /* called with signal lock */
1859 sigexit_locked(p
, signum
)
1860 register struct proc
*p
;
1864 sig_lock_to_exit(p
);
1865 p
->p_acflag
|= AXSIG
;
1866 if (sigprop
[signum
] & SA_CORE
) {
1867 p
->p_sigacts
->ps_sig
= signum
;
1868 if (coredump(p
) == 0)
1869 signum
|= WCOREFLAG
;
1872 exit1(p
, W_EXITCODE(0, signum
), (int *)NULL
);
1877 bsd_ast(thread_act_t thr_act
)
1879 struct proc
*p
= current_proc();
1880 struct uthread
*ut
= get_bsdthread_info(thr_act
);
1883 boolean_t funnel_state
;
1888 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1890 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
1891 pc
= get_useraddr();
1892 addupc_task(p
, pc
, 1);
1893 p
->p_flag
&= ~P_OWEUPC
;
1896 if (CHECK_SIGNALS(p
, current_thread(), ut
)) {
1897 while (signum
= issignal(p
))
1902 (void) thread_funnel_set(kernel_flock
, FALSE
);
1906 * Follwing routines are called using callout from bsd_hardclock
1907 * so that psignals are called in a thread context and are funneled
1910 psignal_vtalarm(struct proc
*p
)
1912 boolean_t funnel_state
;
1916 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1917 psignal_lock(p
, SIGVTALRM
, 1, 1);
1918 (void) thread_funnel_set(kernel_flock
, FALSE
);
1922 psignal_xcpu(struct proc
*p
)
1924 boolean_t funnel_state
;
1928 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1929 psignal_lock(p
, SIGXCPU
, 1, 1);
1930 (void) thread_funnel_set(kernel_flock
, FALSE
);
1934 psignal_sigprof(struct proc
*p
)
1936 boolean_t funnel_state
;
1940 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1941 psignal_lock(p
, SIGPROF
, 1, 1);
1942 (void) thread_funnel_set(kernel_flock
, FALSE
);
1945 /* ptrace set runnalbe */
1947 pt_setrunnable(struct proc
*p
)
1953 if (p
->p_flag
& P_TRACED
) {
1956 wakeup((caddr_t
)&(p
->sigwait
));