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));
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 = sig_task->proc; */
832 p
= (struct proc
*)(get_bsdtask_info(sig_task
));
834 if (!(p
->p_flag
& P_TRACED
) && (p
->p_sigignore
& mask
))
837 uth
= get_bsdthread_info(sig_actthread
);
840 /* mark on process as well */
841 p
->p_siglist
|= mask
;
842 signal_setast(sig_actthread
);
848 register struct proc
*p
;
850 boolean_t funnel_state
;
851 register int sigbits
, mask
, signum
;
853 thread_funnel_set(kernel_flock
, TRUE
);
855 if (p
->p_sigpending
== 0)
862 sigbits
= p
->p_sigpending
;
865 signum
= ffs((long)sigbits
);
866 mask
= sigmask(signum
);
867 p
->p_sigpending
&= ~mask
;
869 psignal_lock(p
, signum
, 0, 0);
873 p
->p_flag
&= ~P_SIGTHR
;
875 thread_funnel_set(kernel_flock
, FALSE
);
880 register struct proc
*p
;
883 psignal_lock(p
, signum
, 1, 1);
888 * Send the signal to the process. If the signal has an action, the action
889 * is usually performed by the target process rather than the caller; we add
890 * the signal to the set of pending signals for the process.
893 * o When a stop signal is sent to a sleeping process that takes the
894 * default action, the process is stopped without awakening it.
895 * o SIGCONT restarts stopped processes (or puts them back to sleep)
896 * regardless of the signal action (eg, blocked or ignored).
898 * Other ignored signals are discarded immediately.
901 psignal_lock(p
, signum
, withlock
, pend
)
902 register struct proc
*p
;
904 register int withlock
;
907 register int s
, prop
;
908 register sig_t action
;
909 thread_act_t sig_thread_act
;
911 register task_t sig_task
;
912 register thread_t cur_thread
;
913 thread_act_t
*cur_act
;
917 if ((u_int
)signum
>= NSIG
|| signum
== 0)
918 panic("psignal signal number");
919 mask
= sigmask(signum
);
920 prop
= sigprop
[signum
];
923 if(rdebug_proc
&& (p
== rdebug_proc
)) {
926 #endif /* SIGNAL_DEBUG */
928 * We will need the task pointer later. Grab it now to
929 * check for a zombie process. Also don't send signals
930 * to kernel internal tasks.
932 if (((sig_task
= p
->task
) == TASK_NULL
) || is_kerneltask(sig_task
))
936 * do not send signals to the process that has the thread
937 * doing a reboot(). Not doing so will mark that thread aborted
938 * and can cause IO failures wich will cause data loss.
940 if (ISSET(p
->p_flag
, P_REBOOT
))
944 * if the traced process is blocked waiting for
945 * gdb then do not block the caller just pend
946 * the signal. Setup a callout to process the
947 * pended signal if not alreadu set
949 if (pend
&& (p
->p_flag
& P_TRACED
) && p
->sigwait
) {
950 p
->p_sigpending
|= mask
;
951 if (!(p
->p_flag
& P_SIGTHR
)) {
952 p
->p_flag
|= P_SIGTHR
;
953 thread_call_func((thread_call_func_t
)psignal_pend
, p
,
963 * If proc is traced, always give parent a chance.
965 if (p
->p_flag
& P_TRACED
)
969 * If the signal is being ignored,
970 * then we forget about it immediately.
971 * (Note: we don't set SIGCONT in p_sigignore,
972 * and if it is set to SIG_IGN,
973 * action will be SIG_DFL here.)
975 if (p
->p_sigignore
& mask
)
977 if (p
->p_sigmask
& mask
)
979 else if (p
->p_sigcatch
& mask
)
985 if (p
->p_nice
> NZERO
&& action
== SIG_DFL
&& (prop
& SA_KILL
) &&
986 (p
->p_flag
& P_TRACED
) == 0)
990 p
->p_siglist
&= ~stopsigmask
;
992 if (prop
& SA_STOP
) {
994 * If sending a tty stop signal to a member of an orphaned
995 * process group, discard the signal here if the action
996 * is default; don't stop the process below if sleeping,
997 * and don't clear any pending SIGCONT.
999 if (prop
& SA_TTYSTOP
&& p
->p_pgrp
->pg_jobc
== 0 &&
1002 p
->p_siglist
&= ~contsigmask
;
1004 p
->p_siglist
|= mask
;
1007 * Defer further processing for signals which are held,
1008 * except that stopped processes must be continued by SIGCONT.
1010 if (action
== SIG_HOLD
&& ((prop
& SA_CONT
) == 0 || p
->p_stat
!= SSTOP
))
1014 * Deliver the signal to the first thread in the task. This
1015 * allows single threaded applications which use signals to
1016 * be able to be linked with multithreaded libraries. We have
1017 * an implicit reference to the current_thread, but need
1018 * an explicit one otherwise. The thread reference keeps
1019 * the corresponding task data structures around too. This
1020 * reference is released by thread_deallocate.
1023 cur_thread
= current_thread(); /* this is a shuttle */
1024 cur_act
= current_act();
1026 /* If successful return with ast set */
1027 kret
= (kern_return_t
)get_signalact(sig_task
,
1028 &sig_thread_act
, &sig_thread
, 1);
1030 if ((kret
!= KERN_SUCCESS
) || (sig_thread_act
== THREAD_NULL
)) {
1032 /* if it is sigkill, may be we should
1033 * inject a thread to terminate
1035 printf("WARNING: no activation in psignal\n");
1038 #endif /* SIGNAL_DEBUG */
1042 if (sig_thread
== THREAD_NULL
) {
1043 printf("WARNING: valid act; but no shutte in psignal\n");
1045 /* FIXME : NO VALID SHUTTLE */
1051 * SIGKILL priority twiddling moved here from above because
1052 * it needs sig_thread. Could merge it into large switch
1053 * below if we didn't care about priority for tracing
1054 * as SIGKILL's action is always SIG_DFL.
1056 if ((signum
== SIGKILL
) && (p
->p_nice
> NZERO
)) {
1060 * we need to make changes here to get nice to work
1061 * reset priority to BASEPRI_USER
1067 * Process is traced - wake it up (if not already
1068 * stopped) so that it can discover the signal in
1069 * issig() and stop for the parent.
1071 if (p
->p_flag
& P_TRACED
) {
1072 if (p
->p_stat
!= SSTOP
)
1078 if (action
!= SIG_DFL
) {
1080 * User wants to catch the signal.
1081 * Wake up the thread, but don't un-suspend it
1082 * (except for SIGCONT).
1085 (void) task_resume(sig_task
);
1088 /* Default action - varies */
1089 if (mask
& stopsigmask
) {
1091 * These are the signals which by default
1094 * Don't clog system with children of init
1095 * stopped from the keyboard.
1097 if (!(prop
& SA_STOP
) && p
->p_pptr
== initproc
) {
1098 psignal_lock(p
, SIGKILL
, 0, 1);
1099 p
->p_siglist
&= ~mask
;
1106 if (!is_thread_running(sig_thread
)) {
1107 /* Thread is not running
1108 * If task hasn't already been stopped by
1109 * a signal, stop it.
1111 p
->p_siglist
&= ~mask
;
1112 if (get_task_userstop(sig_task
) == 0) {
1114 * p_cursig must not be set, because
1115 * it will be psig()'d if it is not
1116 * zero, and the signal is being
1117 * handled here. But save the signal
1118 * in p_stopsig so WUNTRACED
1119 * option to wait can find it.
1121 p
->p_xstat
= signum
;
1122 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0)
1123 psignal(p
->p_pptr
, SIGCHLD
);
1127 /* unconditional check is bad */
1128 signal_clearast(sig_thread_act
);
1132 if (p
->p_stat
!= SZOMB
)
1133 signal_setast(cur_act
);
1140 * Signals ignored by default have been dealt
1141 * with already, since their bits are on in
1147 * Kill signal always sets process running and
1151 * Process will be running after 'run'
1155 thread_abort(sig_thread_act
);
1161 * Let the process run. If it's sleeping on an
1162 * event, it remains so.
1164 if (p
->p_flag
& P_TTYSLEEP
) {
1165 p
->p_flag
&= ~P_TTYSLEEP
;
1166 wakeup(&p
->p_siglist
);
1168 (void) task_resume(sig_task
);
1170 p
->p_siglist
&= ~mask
;
1173 /* do not clear AST as tcsh is sendig SIGTERM followed by
1174 * SIGCONT and the ast was getting cleared unconditinally
1175 * This is not right.
1177 signal_clearast(sig_thread_act
);
1183 * All other signals wake up the process, but don't
1192 * If we're being traced (possibly because someone attached us
1193 * while we were stopped), check for a signal from the debugger.
1195 if (p
->p_stat
== SSTOP
) {
1196 if ((p
->p_flag
& P_TRACED
) != 0 && p
->p_xstat
!= 0)
1197 p
->p_siglist
|= sigmask(p
->p_xstat
);
1201 * setrunnable(p) in BSD
1206 * Wake up the thread if it is interruptible.
1208 thread_abort_safely(sig_thread_act
);
1218 thread_t self
= current_thread();
1220 p
->exit_thread
= self
;
1221 (void) task_suspend(p
->task
);
1228 thread_t self
= current_thread();
1230 while (p
->sigwait
|| p
->exit_thread
) {
1231 if (p
->exit_thread
) {
1232 if (p
->exit_thread
!= self
) {
1234 * Already exiting - no signals.
1236 thread_abort(current_act());
1240 if(assert_wait_possible()) {
1241 assert_wait((caddr_t
)&p
->sigwait_thread
,
1242 (THREAD_INTERRUPTIBLE
));
1247 if (thread_should_abort(self
)) {
1249 * Terminate request - clean up.
1258 * If the current process has received a signal (should be caught or cause
1259 * termination, should interrupt current syscall), return the signal number.
1260 * Stop signals with default action are processed immediately, then cleared;
1261 * they aren't returned. This is checked after each entry to the system for
1262 * a syscall or trap (though this can usually be done without calling issignal
1263 * by checking the pending signal masks in the CURSIG macro.) The normal call
1266 * while (signum = CURSIG(curproc))
1271 register struct proc
*p
;
1273 register int signum
, mask
, prop
, sigbits
;
1274 task_t task
= p
->task
;
1275 thread_t cur_thread
;
1276 thread_act_t cur_act
;
1278 struct uthread
* ut
;
1281 cur_thread
= current_thread();
1282 cur_act
= current_act();
1288 * Try to grab the signal lock.
1290 if (sig_try_locked(p
) <= 0) {
1295 ut
= get_bsdthread_info(cur_act
);
1297 sigbits
= (ut
->uu_sig
|p
->p_siglist
) & ~p
->p_sigmask
;
1299 if (p
->p_flag
& P_PPWAIT
)
1300 sigbits
&= ~stopsigmask
;
1301 if (sigbits
== 0) { /* no signal to send */
1305 signum
= ffs((long)sigbits
);
1306 mask
= sigmask(signum
);
1307 prop
= sigprop
[signum
];
1309 if (mask
& threadmask
) {
1310 /* we can take this signal */
1311 ut
->uu_sig
&= ~mask
;
1315 * We should see pending but ignored signals
1316 * only if P_TRACED was on when they were posted.
1318 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
1319 p
->p_siglist
&= ~mask
; /* take the signal! */
1322 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
1324 register task_t task
;
1326 * If traced, always stop, and stay
1327 * stopped until released by the debugger.
1329 /* ptrace debugging */
1330 p
->p_xstat
= signum
;
1331 psignal(p
->p_pptr
, SIGCHLD
);
1333 * XXX Have to really stop for debuggers;
1334 * XXX stop() doesn't do the right thing.
1335 * XXX Inline the task_suspend because we
1336 * XXX have to diddle Unix state in the
1342 p
->sigwait_thread
= cur_act
;
1344 p
->p_flag
&= ~P_WAITED
;
1345 p
->p_siglist
&= ~mask
; /* clear the old signal */
1347 wakeup((caddr_t
)p
->p_pptr
);
1348 assert_wait((caddr_t
)&p
->sigwait
, (THREAD_INTERRUPTIBLE
));
1351 p
->sigwait_thread
= NULL
;
1352 wakeup((caddr_t
)&p
->sigwait_thread
);
1355 * This code is to detect when gdb is killed
1356 * even as the traced program is attached.
1357 * pgsignal would get the SIGKILL to traced program
1358 * That's what we are trying to see (I hope)
1360 if (p
->p_siglist
& sigmask(SIGKILL
)) {
1362 * Wait event may still be outstanding;
1363 * clear it, since sig_lock_to_exit will
1366 clear_wait(current_thread(), THREAD_INTERRUPTED
);
1367 sig_lock_to_exit(p
);
1369 * Since this thread will be resumed
1370 * to allow the current syscall to
1371 * be completed, must save u_qsave
1372 * before calling exit(). (Since exit()
1373 * calls closef() which can trash u_qsave.)
1381 * We may have to quit
1383 if (thread_should_abort(current_thread())) {
1388 * If parent wants us to take the signal,
1389 * then it will leave it in p->p_xstat;
1390 * otherwise we just look for signals again.
1392 signum
= p
->p_xstat
;
1396 * Put the new signal into p_siglist. If the
1397 * signal is being masked, look for other signals.
1399 mask
= sigmask(signum
);
1400 if (mask
& threadmask
)
1403 p
->p_siglist
|= mask
;
1404 if (p
->p_sigmask
& mask
)
1409 * Decide whether the signal should be returned.
1410 * Return the signal's number, or fall through
1411 * to clear it from the pending mask.
1414 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
1418 * Don't take default actions on system processes.
1420 if (p
->p_pptr
->p_pid
== 0) {
1423 * Are you sure you want to ignore SIGSEGV
1426 printf("Process (pid %d) got signal %d\n",
1429 break; /* == ignore */
1433 * If there is a pending stop signal to process
1434 * with default action, stop here,
1435 * then clear the signal. However,
1436 * if process is member of an orphaned
1437 * process group, ignore tty stop signals.
1439 if (prop
& SA_STOP
) {
1440 if (p
->p_flag
& P_TRACED
||
1441 (p
->p_pgrp
->pg_jobc
== 0 &&
1443 break; /* == ignore */
1444 p
->p_xstat
= signum
;
1446 if ((p
->p_pptr
->p_flag
& P_NOCLDSTOP
) == 0)
1447 psignal(p
->p_pptr
, SIGCHLD
);
1450 * We may have to quit
1452 if (thread_should_abort(current_thread())) {
1457 } else if (prop
& SA_IGNORE
) {
1459 * Except for SIGCONT, shouldn't get here.
1460 * Default action is to ignore; drop it.
1462 break; /* == ignore */
1471 * Masking above should prevent us ever trying
1472 * to take action on an ignored signal other
1473 * than SIGCONT, unless process is traced.
1475 if ((prop
& SA_CONT
) == 0 &&
1476 (p
->p_flag
& P_TRACED
) == 0)
1477 printf("issignal\n");
1478 break; /* == ignore */
1482 * This signal has an action, let
1483 * postsig() process it.
1488 p
->p_siglist
&= ~mask
; /* take the signal! */
1489 p
->p_sigpending
&= ~mask
; /* take the pending signal */
1494 /* called from _sleep */
1497 register struct proc
*p
;
1499 register int signum
, mask
, prop
, sigbits
;
1500 task_t task
= p
->task
;
1501 thread_t cur_thread
;
1502 thread_act_t cur_act
;
1504 struct uthread
* ut
;
1507 if (p
->p_siglist
== 0)
1510 if (((p
->p_siglist
& ~p
->p_sigmask
) == 0) && ((p
->p_flag
& P_TRACED
) == 0))
1513 cur_thread
= current_thread();
1514 cur_act
= current_act();
1516 ut
= get_bsdthread_info(cur_act
);
1518 sigbits
= (ut
->uu_sig
| p
->p_siglist
) & ~p
->p_sigmask
;
1521 if (p
->p_flag
& P_PPWAIT
)
1522 sigbits
&= ~stopsigmask
;
1523 if (sigbits
== 0) { /* no signal to send */
1527 signum
= ffs((long)sigbits
);
1528 mask
= sigmask(signum
);
1529 prop
= sigprop
[signum
];
1532 * We should see pending but ignored signals
1533 * only if P_TRACED was on when they were posted.
1535 if (mask
& p
->p_sigignore
&& (p
->p_flag
& P_TRACED
) == 0) {
1538 if (p
->p_flag
& P_TRACED
&& (p
->p_flag
& P_PPWAIT
) == 0) {
1540 * Put the new signal into p_siglist. If the
1541 * signal is being masked, look for other signals.
1543 mask
= sigmask(signum
);
1544 if (p
->p_sigmask
& mask
)
1550 * Decide whether the signal should be returned.
1551 * Return the signal's number, or fall through
1552 * to clear it from the pending mask.
1555 switch ((long)p
->p_sigacts
->ps_sigact
[signum
]) {
1559 * Don't take default actions on system processes.
1561 if (p
->p_pptr
->p_pid
== 0) {
1564 * Are you sure you want to ignore SIGSEGV
1567 printf("Process (pid %d) got signal %d\n",
1570 break; /* == ignore */
1574 * If there is a pending stop signal to process
1575 * with default action, stop here,
1576 * then clear the signal. However,
1577 * if process is member of an orphaned
1578 * process group, ignore tty stop signals.
1580 if (prop
& SA_STOP
) {
1581 if (p
->p_flag
& P_TRACED
||
1582 (p
->p_pgrp
->pg_jobc
== 0 &&
1584 break; /* == ignore */
1587 } else if (prop
& SA_IGNORE
) {
1589 * Except for SIGCONT, shouldn't get here.
1590 * Default action is to ignore; drop it.
1592 break; /* == ignore */
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.
1616 sigbits
&= ~mask
; /* take the signal! */
1622 * Put the argument process into the stopped state and notify the parent
1623 * via wakeup. Signals are handled elsewhere. The process must not be
1628 register struct proc
*p
;
1631 p
->p_flag
&= ~P_WAITED
;
1632 wakeup((caddr_t
)p
->p_pptr
);
1633 (void) task_suspend(p
->task
); /*XXX*/
1637 * Take the action for the specified signal
1638 * from the current set of pending signals.
1642 register int signum
;
1644 register struct proc
*p
= current_proc();
1645 register struct sigacts
*ps
= p
->p_sigacts
;
1646 register sig_t action
;
1648 int mask
, returnmask
;
1654 * This must be called on master cpu
1656 if (cpu_number() != master_cpu
)
1657 panic("psig not on master");
1662 * Try to grab the signal lock.
1664 if (sig_try_locked(p
) <= 0) {
1669 mask
= sigmask(signum
);
1670 p
->p_siglist
&= ~mask
;
1671 action
= ps
->ps_sigact
[signum
];
1673 if (KTRPOINT(p
, KTR_PSIG
))
1674 ktrpsig(p
->p_tracep
,
1675 signum
, action
, ps
->ps_flags
& SAS_OLDMASK
?
1676 ps
->ps_oldmask
: p
->p_sigmask
, 0);
1678 if (action
== SIG_DFL
) {
1680 * Default action, where the default is to kill
1681 * the process. (Other cases were ignored above.)
1683 /* called with signal_lock() held */
1684 sigexit_locked(p
, signum
);
1689 * If we get here, the signal must be caught.
1692 if (action
== SIG_IGN
|| (p
->p_sigmask
& mask
))
1694 "postsig: processing masked or ignored signal\n");
1697 * Set the new mask value and also defer further
1698 * occurences of this signal.
1700 * Special case: user has done a sigpause. Here the
1701 * current mask is not of interest, but rather the
1702 * mask from before the sigpause is what we want
1703 * restored after the signal processing is completed.
1705 if (ps
->ps_flags
& SAS_OLDMASK
) {
1706 returnmask
= ps
->ps_oldmask
;
1707 ps
->ps_flags
&= ~SAS_OLDMASK
;
1709 returnmask
= p
->p_sigmask
;
1710 p
->p_sigmask
|= ps
->ps_catchmask
[signum
] | mask
;
1711 if (ps
->ps_sig
!= signum
) {
1717 p
->p_stats
->p_ru
.ru_nsignals
++;
1718 sendsig(p
, action
, signum
, returnmask
, code
);
1724 * Force the current process to exit with the specified signal, dumping core
1725 * if appropriate. We bypass the normal tests for masked and caught signals,
1726 * allowing unrecoverable failures to terminate the process without changing
1727 * signal state. Mark the accounting record with the signal termination.
1728 * If dumping core, save the signal number for the debugger. Calls exit and
1731 /* called with signal lock */
1733 sigexit_locked(p
, signum
)
1734 register struct proc
*p
;
1738 sig_lock_to_exit(p
);
1739 p
->p_acflag
|= AXSIG
;
1740 if (sigprop
[signum
] & SA_CORE
) {
1741 p
->p_sigacts
->ps_sig
= signum
;
1742 if (coredump(p
) == 0)
1743 signum
|= WCOREFLAG
;
1746 exit1(p
, W_EXITCODE(0, signum
));
1751 bsd_ast(thread_act_t thr_act
)
1753 struct proc
*p
= current_proc();
1754 struct uthread
*ut
= get_bsdthread_info(thr_act
);
1757 boolean_t funnel_state
;
1762 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1764 if ((p
->p_flag
& P_OWEUPC
) && (p
->p_flag
& P_PROFIL
)) {
1765 pc
= get_useraddr();
1766 addupc_task(p
, pc
, 1);
1767 p
->p_flag
&= ~P_OWEUPC
;
1770 if (CHECK_SIGNALS(p
, current_thread(), ut
)) {
1771 while (signum
= issignal(p
))
1776 (void) thread_funnel_set(kernel_flock
, FALSE
);
1780 * Follwing routines are called using callout from bsd_hardclock
1781 * so that psignals are called in a thread context and are funneled
1784 psignal_vtalarm(struct proc
*p
)
1786 boolean_t funnel_state
;
1790 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1791 psignal_lock(p
, SIGVTALRM
, 1, 1);
1792 (void) thread_funnel_set(kernel_flock
, FALSE
);
1796 psignal_xcpu(struct proc
*p
)
1798 boolean_t funnel_state
;
1802 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1803 psignal_lock(p
, SIGXCPU
, 1, 1);
1804 (void) thread_funnel_set(kernel_flock
, FALSE
);
1808 psignal_sigprof(struct proc
*p
)
1810 boolean_t funnel_state
;
1814 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1815 psignal_lock(p
, SIGPROF
, 1, 1);
1816 (void) thread_funnel_set(kernel_flock
, FALSE
);
1819 /* ptrace set runnalbe */
1821 pt_setrunnable(struct proc
*p
)
1827 if (p
->p_flag
& P_TRACED
) {
1830 wakeup((caddr_t
)&(p
->sigwait
));