1 ;;;; code for handling UNIX signals
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!UNIX")
14 (defmacro with-interrupt-bindings
(&body body
)
16 ;; KLUDGE: Whatever is on the PCL stacks before the interrupt
17 ;; handler runs doesn't really matter, since we're not on the
18 ;; same call stack, really -- and if we don't bind these (esp.
19 ;; the cache one) we can get a bogus metacircle if an interrupt
20 ;; handler calls a GF that was being computed when the interrupt
22 ((sb!pcl
::*cache-miss-values-stack
* nil
)
23 (sb!pcl
::*dfun-miss-gfs-on-stack
* nil
))
26 ;;; Evaluate CLEANUP-FORMS iff PROTECTED-FORM does a non-local exit.
27 (defmacro nlx-protect
(protected-form &rest cleanup-froms
)
28 (with-unique-names (completep)
29 `(let ((,completep nil
))
33 (allow-with-interrupts
39 (defun invoke-interruption (function)
41 ;; Reset signal mask: the C-side handler has blocked all
42 ;; deferrable signals before funcalling into lisp. They are to be
43 ;; unblocked the first time interrupts are enabled. With this
44 ;; mechanism there are no extra frames on the stack from a
45 ;; previous signal handler when the next signal is delivered
46 ;; provided there is no WITH-INTERRUPTS.
47 (let ((*unblock-deferrables-on-enabling-interrupts-p
* t
))
48 (with-interrupt-bindings
49 (let ((sb!debug
:*stack-top-hint
*
50 (nth-value 1 (sb!kernel
:find-interrupted-name-and-frame
))))
51 (allow-with-interrupts
52 (nlx-protect (funcall function
)
53 ;; We've been running with deferrables
54 ;; blocked in Lisp called by a C signal
55 ;; handler. If we return normally the sigmask
56 ;; in the interrupted context is restored.
57 ;; However, if we do an nlx the operating
58 ;; system will not restore it for us.
59 (when *unblock-deferrables-on-enabling-interrupts-p
*
60 ;; This means that storms of interrupts
61 ;; doing an nlx can still run out of stack.
62 (unblock-deferrable-signals)))))))))
64 (defmacro in-interruption
((&key
) &body body
)
66 "Convenience macro on top of INVOKE-INTERRUPTION."
67 `(dx-flet ((interruption () ,@body
))
68 (invoke-interruption #'interruption
)))
70 ;;;; system calls that deal with signals
72 ;;; Send the signal SIGNAL to the process with process id PID. SIGNAL
73 ;;; should be a valid signal number
74 #!-sb-fluid
(declaim (inline real-unix-kill
))
75 (sb!alien
:define-alien-routine
("kill" unix-kill
) sb
!alien
:int
77 (signal sb
!alien
:int
))
79 ;;; Send the signal SIGNAL to the all the process in process group
80 ;;; PGRP. SIGNAL should be a valid signal number
81 #!-sb-fluid
(declaim (inline real-unix-killpg
))
82 (sb!alien
:define-alien-routine
("killpg" unix-killpg
) sb
!alien
:int
84 (signal sb
!alien
:int
))
86 ;;; Reset the current set of masked signals (those being blocked from
89 ;;; (Note: CMU CL had a more general SIGSETMASK call and a SIGMASK
90 ;;; operator to create masks, but since we only ever reset to 0, we no
91 ;;; longer support it. If you need it, you can pull it out of the CMU
92 ;;; CL sources, or the old SBCL sources; but you might also consider
93 ;;; doing things the SBCL way and moving this kind of C-level work
94 ;;; down to C wrapper functions.)
96 (declaim (inline %unblock-deferrable-signals %unblock-gc-signals
))
97 (sb!alien
:define-alien-routine
("unblock_deferrable_signals"
98 %unblock-deferrable-signals
)
100 (where sb
!alien
:unsigned-long
)
101 (old sb
!alien
:unsigned-long
))
102 (sb!alien
:define-alien-routine
("unblock_gc_signals" %unblock-gc-signals
)
104 (where sb
!alien
:unsigned-long
)
105 (old sb
!alien
:unsigned-long
))
107 (defun unblock-deferrable-signals ()
108 (%unblock-deferrable-signals
0 0))
110 (defun unblock-gc-signals ()
111 (%unblock-gc-signals
0 0))
114 ;;;; C routines that actually do all the work of establishing signal handlers
115 (sb!alien
:define-alien-routine
("install_handler" install-handler
)
116 sb
!alien
:unsigned-long
117 (signal sb
!alien
:int
)
118 (handler sb
!alien
:unsigned-long
))
120 ;;;; interface to enabling and disabling signal handlers
122 (defun enable-interrupt (signal handler
)
123 (declare (type (or function fixnum
(member :default
:ignore
)) handler
))
124 (/show0
"enable-interrupt")
125 (flet ((run-handler (&rest args
)
126 (declare (truly-dynamic-extent args
))
128 (apply handler args
))))
130 (let ((result (install-handler signal
135 (sb!kernel
:get-lisp-obj-address
137 (cond ((= result sig-dfl
) :default
)
138 ((= result sig-ign
) :ignore
)
139 (t (the (or function fixnum
)
140 (sb!kernel
:make-lisp-obj result
))))))))
142 (defun default-interrupt (signal)
143 (enable-interrupt signal
:default
))
145 (defun ignore-interrupt (signal)
146 (enable-interrupt signal
:ignore
))
148 ;;;; default LISP signal handlers
150 ;;;; Most of these just call ERROR to report the presence of the signal.
152 ;;; SIGINT is handled like BREAK, except that ANSI BREAK ignores
153 ;;; *DEBUGGER-HOOK*, but we want SIGINT's BREAK to respect it, so that
154 ;;; SIGINT in --disable-debugger mode will cleanly terminate the system
155 ;;; (by respecting the *DEBUGGER-HOOK* established in that mode).
156 (eval-when (:compile-toplevel
:execute
)
157 (sb!xc
:defmacro define-signal-handler
(name what
&optional
(function 'error
))
158 `(defun ,name
(signal info context
)
159 (declare (ignore signal info
))
160 (declare (type system-area-pointer context
))
161 (/show
"in Lisp-level signal handler" ,(symbol-name name
)
164 (,function
,(concatenate 'simple-string what
" at #X~X")
165 (with-alien ((context (* os-context-t
) context
))
166 (sap-int (sb!vm
:context-pc context
))))))))
168 (define-signal-handler sigill-handler
"illegal instruction")
170 (define-signal-handler sigemt-handler
"SIGEMT")
171 (define-signal-handler sigbus-handler
"bus error")
173 (define-signal-handler sigsys-handler
"bad argument to a system call")
175 (defun sigint-handler (signal info context
)
176 (declare (ignore signal info
))
177 (declare (type system-area-pointer context
))
178 (/show
"in Lisp-level SIGINT handler" (sap-int context
))
179 (flet ((interrupt-it ()
180 (with-alien ((context (* os-context-t
) context
))
182 (let ((int (make-condition 'interactive-interrupt
184 :address
(sap-int (sb!vm
:context-pc context
)))))
185 ;; First SIGNAL, so that handlers can run.
187 ;; Then enter the debugger like BREAK.
188 (%break
'sigint int
))))))
189 (sb!thread
:interrupt-thread
(sb!thread
::foreground-thread
)
192 (defun sigalrm-handler (signal info context
)
193 (declare (ignore signal info context
))
194 (declare (type system-area-pointer context
))
195 (sb!impl
::run-expired-timers
))
197 (defun sigterm-handler (signal code context
)
198 (declare (ignore signal code context
))
199 (sb!thread
::terminate-session
)
202 ;;; SIGPIPE is not used in SBCL for its original purpose, instead it's
203 ;;; for signalling a thread that it should look at its interruption
204 ;;; queue. The handler (RUN_INTERRUPTION) just returns if there is
205 ;;; nothing to do so it's safe to receive spurious SIGPIPEs coming
207 (defun sigpipe-handler (signal code context
)
208 (declare (ignore signal code context
))
209 (sb!thread
::run-interruption
))
211 ;;; the handler for SIGCHLD signals for RUN-PROGRAM
212 (defun sigchld-handler (signal code context
)
213 (declare (ignore signal code context
))
214 (sb!impl
::get-processes-status-changes
))
216 (defun sb!kernel
:signal-cold-init-or-reinit
()
218 "Enable all the default signals that Lisp knows how to deal with."
219 (enable-interrupt sigint
#'sigint-handler
)
220 (enable-interrupt sigterm
#'sigterm-handler
)
221 (enable-interrupt sigill
#'sigill-handler
)
223 (enable-interrupt sigemt
#'sigemt-handler
)
224 (enable-interrupt sigfpe
#'sb
!vm
:sigfpe-handler
)
225 (enable-interrupt sigbus
#'sigbus-handler
)
227 (enable-interrupt sigsys
#'sigsys-handler
)
228 (enable-interrupt sigalrm
#'sigalrm-handler
)
229 (enable-interrupt sigpipe
#'sigpipe-handler
)
230 (enable-interrupt sigchld
#'sigchld-handler
)
231 #!+hpux
(ignore-interrupt sigxcpu
)
233 (unblock-deferrable-signals)
238 ;;; extract si_code from siginfo_t
239 (sb!alien
:define-alien-routine
("siginfo_code" siginfo-code
) sb
!alien
:int
240 (info system-area-pointer
))
243 ;;; Magically converted by the compiler into a break instruction.
244 (defun receive-pending-interrupt ()
245 (receive-pending-interrupt))