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)
15 (with-unique-names (empty)
17 ;; KLUDGE: Whatever is on the PCL stacks before the interrupt
18 ;; handler runs doesn't really matter, since we're not on the
19 ;; same call stack, really -- and if we don't bind these (esp.
20 ;; the cache one) we can get a bogus metacircle if an interrupt
21 ;; handler calls a GF that was being computed when the interrupt
23 ((sb!pcl::*cache-miss-values-stack* nil)
24 (sb!pcl::*dfun-miss-gfs-on-stack* nil)
25 ;; Unless we do this, ADJUST-ARRAY and SORT would need to
26 ;; disable interrupts.
28 (sb!impl::*zap-array-data-temp* ,empty)
29 (sb!impl::*merge-sort-temp-vector* ,empty))
32 ;;; Evaluate CLEANUP-FORMS iff PROTECTED-FORM does a non-local exit.
33 (defmacro nlx-protect (protected-form &rest cleanup-froms)
34 (with-unique-names (completep)
35 `(let ((,completep nil))
39 (allow-with-interrupts
45 (defun invoke-interruption (function)
47 ;; Reset signal mask: the C-side handler has blocked all
48 ;; deferrable signals before funcalling into lisp. They are to be
49 ;; unblocked the first time interrupts are enabled. With this
50 ;; mechanism there are no extra frames on the stack from a
51 ;; previous signal handler when the next signal is delivered
52 ;; provided there is no WITH-INTERRUPTS.
53 (let ((*unblock-deferrables-on-enabling-interrupts-p* t))
54 (with-interrupt-bindings
55 (let ((sb!debug:*stack-top-hint*
56 (nth-value 1 (sb!kernel:find-interrupted-name-and-frame))))
57 (allow-with-interrupts
58 (nlx-protect (funcall function)
59 ;; We've been running with deferrables
60 ;; blocked in Lisp called by a C signal
61 ;; handler. If we return normally the sigmask
62 ;; in the interrupted context is restored.
63 ;; However, if we do an nlx the operating
64 ;; system will not restore it for us.
65 (when *unblock-deferrables-on-enabling-interrupts-p*
66 ;; This means that storms of interrupts
67 ;; doing an nlx can still run out of stack.
68 (unblock-deferrable-signals)))))))))
70 (defmacro in-interruption ((&key) &body body)
72 "Convenience macro on top of INVOKE-INTERRUPTION."
73 `(dx-flet ((interruption () ,@body))
74 (invoke-interruption #'interruption)))
76 ;;;; system calls that deal with signals
78 ;;; Send the signal SIGNAL to the process with process id PID. SIGNAL
79 ;;; should be a valid signal number
80 #!-sb-fluid (declaim (inline real-unix-kill))
81 (sb!alien:define-alien-routine ("kill" unix-kill) sb!alien:int
83 (signal sb!alien:int))
85 ;;; Send the signal SIGNAL to the all the process in process group
86 ;;; PGRP. SIGNAL should be a valid signal number
87 #!-sb-fluid (declaim (inline real-unix-killpg))
88 (sb!alien:define-alien-routine ("killpg" unix-killpg) sb!alien:int
90 (signal sb!alien:int))
92 ;;; Reset the current set of masked signals (those being blocked from
95 ;;; (Note: CMU CL had a more general SIGSETMASK call and a SIGMASK
96 ;;; operator to create masks, but since we only ever reset to 0, we no
97 ;;; longer support it. If you need it, you can pull it out of the CMU
98 ;;; CL sources, or the old SBCL sources; but you might also consider
99 ;;; doing things the SBCL way and moving this kind of C-level work
100 ;;; down to C wrapper functions.)
102 (sb!alien:define-alien-routine "unblock_deferrable_signals" sb!alien:void)
103 (sb!alien:define-alien-routine "unblock_gc_signals" sb!alien:void)
106 ;;;; C routines that actually do all the work of establishing signal handlers
107 (sb!alien:define-alien-routine ("install_handler" install-handler)
108 sb!alien:unsigned-long
109 (signal sb!alien:int)
110 (handler sb!alien:unsigned-long))
112 ;;;; interface to enabling and disabling signal handlers
114 (defun enable-interrupt (signal handler)
115 (declare (type (or function fixnum (member :default :ignore)) handler))
116 (/show0 "enable-interrupt")
117 (flet ((run-handler (&rest args)
118 (declare (truly-dynamic-extent args))
120 (apply handler args))))
122 (let ((result (install-handler signal
127 (sb!kernel:get-lisp-obj-address
129 (cond ((= result sig-dfl) :default)
130 ((= result sig-ign) :ignore)
131 (t (the (or function fixnum)
132 (sb!kernel:make-lisp-obj result))))))))
134 (defun default-interrupt (signal)
135 (enable-interrupt signal :default))
137 (defun ignore-interrupt (signal)
138 (enable-interrupt signal :ignore))
140 ;;;; default LISP signal handlers
142 ;;;; Most of these just call ERROR to report the presence of the signal.
144 ;;; SIGINT is handled like BREAK, except that ANSI BREAK ignores
145 ;;; *DEBUGGER-HOOK*, but we want SIGINT's BREAK to respect it, so that
146 ;;; SIGINT in --disable-debugger mode will cleanly terminate the system
147 ;;; (by respecting the *DEBUGGER-HOOK* established in that mode).
148 (eval-when (:compile-toplevel :execute)
149 (sb!xc:defmacro define-signal-handler (name what &optional (function 'error))
150 `(defun ,name (signal info context)
151 (declare (ignore signal info))
152 (declare (type system-area-pointer context))
153 (/show "in Lisp-level signal handler" ,(symbol-name name)
156 (,function ,(concatenate 'simple-string what " at #X~X")
157 (with-alien ((context (* os-context-t) context))
158 (sap-int (sb!vm:context-pc context))))))))
160 (define-signal-handler sigill-handler "illegal instruction")
162 (define-signal-handler sigemt-handler "SIGEMT")
163 (define-signal-handler sigbus-handler "bus error")
165 (define-signal-handler sigsys-handler "bad argument to a system call")
167 (defun sigint-handler (signal info context)
168 (declare (ignore signal info))
169 (declare (type system-area-pointer context))
170 (/show "in Lisp-level SIGINT handler" (sap-int context))
171 (flet ((interrupt-it ()
172 (with-alien ((context (* os-context-t) context))
174 (%break 'sigint 'interactive-interrupt
176 :address (sap-int (sb!vm:context-pc context)))))))
177 (sb!thread:interrupt-thread (sb!thread::foreground-thread)
180 (defun sigalrm-handler (signal info context)
181 (declare (ignore signal info context))
182 (declare (type system-area-pointer context))
183 (sb!impl::run-expired-timers))
185 (defun sigterm-handler (signal code context)
186 (declare (ignore signal code context))
187 (sb!thread::terminate-session)
190 ;;; SIGPIPE is not used in SBCL for its original purpose, instead it's
191 ;;; for signalling a thread that it should look at its interruption
192 ;;; queue. The handler (RUN_INTERRUPTION) just returns if there is
193 ;;; nothing to do so it's safe to receive spurious SIGPIPEs coming
195 (defun sigpipe-handler (signal code context)
196 (declare (ignore signal code context))
197 (sb!thread::run-interruption))
199 (defun sb!kernel:signal-cold-init-or-reinit ()
201 "Enable all the default signals that Lisp knows how to deal with."
202 (enable-interrupt sigint #'sigint-handler)
203 (enable-interrupt sigterm #'sigterm-handler)
204 (enable-interrupt sigill #'sigill-handler)
206 (enable-interrupt sigemt #'sigemt-handler)
207 (enable-interrupt sigfpe #'sb!vm:sigfpe-handler)
208 (enable-interrupt sigbus #'sigbus-handler)
210 (enable-interrupt sigsys #'sigsys-handler)
211 (enable-interrupt sigalrm #'sigalrm-handler)
212 (enable-interrupt sigpipe #'sigpipe-handler)
213 #!+hpux (ignore-interrupt sigxcpu)
215 (unblock-deferrable-signals)
220 ;;; extract si_code from siginfo_t
221 (sb!alien:define-alien-routine ("siginfo_code" siginfo-code) sb!alien:int
222 (info system-area-pointer))
225 ;;; Magically converted by the compiler into a break instruction.
226 (defun receive-pending-interrupt ()
227 (receive-pending-interrupt))