7394695713d07dc98e9e7ba53a91491d64c2a24d
[sbcl.git] / src / code / target-signal.lisp
1 ;;;; code for handling UNIX signals
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!UNIX")
13
14 (defmacro with-interrupt-bindings (&body body)
15   `(let*
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
21        ;; hit.
22        ((sb!pcl::*cache-miss-values-stack* nil)
23         (sb!pcl::*dfun-miss-gfs-on-stack* nil))
24      ,@body))
25
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))
30        (without-interrupts
31          (unwind-protect
32               (progn
33                 (allow-with-interrupts
34                   ,protected-form)
35                 (setq ,completep t))
36            (unless ,completep
37              ,@cleanup-froms))))))
38
39 (defun invoke-interruption (function)
40   (without-interrupts
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           (sb!debug:*stack-top-hint* (or sb!debug:*stack-top-hint* 'invoke-interruption)))
49       (with-interrupt-bindings
50         (sb!thread::without-thread-waiting-for (:already-without-interrupts t)
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)))))))))
63
64 (defmacro in-interruption ((&key) &body body)
65   #!+sb-doc
66   "Convenience macro on top of INVOKE-INTERRUPTION."
67   `(dx-flet ((interruption () ,@body))
68      (invoke-interruption #'interruption)))
69 \f
70 ;;;; system calls that deal with signals
71
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
76   (pid sb!alien:int)
77   (signal sb!alien:int))
78
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
83   (pgrp sb!alien:int)
84   (signal sb!alien:int))
85
86 ;;; Reset the current set of masked signals (those being blocked from
87 ;;; delivery).
88 ;;;
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.)
95
96 (declaim (inline %unblock-deferrable-signals %unblock-gc-signals))
97 (sb!alien:define-alien-routine ("unblock_deferrable_signals"
98                                 %unblock-deferrable-signals)
99     sb!alien:void
100   (where sb!alien:unsigned-long)
101   (old sb!alien:unsigned-long))
102 (sb!alien:define-alien-routine ("unblock_gc_signals" %unblock-gc-signals)
103     sb!alien:void
104   (where sb!alien:unsigned-long)
105   (old sb!alien:unsigned-long))
106
107 (defun unblock-deferrable-signals ()
108   (%unblock-deferrable-signals 0 0))
109
110 (defun unblock-gc-signals ()
111   (%unblock-gc-signals 0 0))
112
113 \f
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))
119
120 ;;;; interface to enabling and disabling signal handlers
121
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))
127            (in-interruption ()
128              (apply handler args))))
129     (without-gcing
130       (let ((result (install-handler signal
131                                      (case handler
132                                        (:default sig-dfl)
133                                        (:ignore sig-ign)
134                                        (t
135                                         (sb!kernel:get-lisp-obj-address
136                                          #'run-handler))))))
137         (cond ((= result sig-dfl) :default)
138               ((= result sig-ign) :ignore)
139               (t (the (or function fixnum)
140                    (sb!kernel:make-lisp-obj result))))))))
141
142 (defun default-interrupt (signal)
143   (enable-interrupt signal :default))
144
145 (defun ignore-interrupt (signal)
146   (enable-interrupt signal :ignore))
147 \f
148 ;;;; default LISP signal handlers
149 ;;;;
150 ;;;; Most of these just call ERROR to report the presence of the signal.
151
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)
162               (sap-int context))
163        (with-interrupts
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))))))))
167
168 (define-signal-handler sigill-handler "illegal instruction")
169 #!-linux
170 (define-signal-handler sigemt-handler "SIGEMT")
171 (define-signal-handler sigbus-handler "bus error")
172 #!-linux
173 (define-signal-handler sigsys-handler "bad argument to a system call")
174
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))
181              (with-interrupts
182                (let ((int (make-condition 'interactive-interrupt
183                                           :context context
184                                           :address (sap-int (sb!vm:context-pc context)))))
185                  ;; First SIGNAL, so that handlers can run.
186                  (signal int)
187                  ;; Then enter the debugger like BREAK.
188                  (%break 'sigint int))))))
189     (sb!thread:interrupt-thread (sb!thread::foreground-thread)
190                                 #'interrupt-it)))
191
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))
196
197 (defun sigterm-handler (signal code context)
198   (declare (ignore signal code context))
199   (sb!ext:exit))
200
201 ;;; SIGPIPE is not used in SBCL for its original purpose, instead it's
202 ;;; for signalling a thread that it should look at its interruption
203 ;;; queue. The handler (RUN_INTERRUPTION) just returns if there is
204 ;;; nothing to do so it's safe to receive spurious SIGPIPEs coming
205 ;;; from the kernel.
206 (defun sigpipe-handler (signal code context)
207   (declare (ignore signal code context))
208   (sb!thread::run-interruption))
209
210 ;;; the handler for SIGCHLD signals for RUN-PROGRAM
211 (defun sigchld-handler  (signal code context)
212   (declare (ignore signal code context))
213   (sb!impl::get-processes-status-changes))
214
215 (defun sb!kernel:signal-cold-init-or-reinit ()
216   #!+sb-doc
217   "Enable all the default signals that Lisp knows how to deal with."
218   (enable-interrupt sigint #'sigint-handler)
219   (enable-interrupt sigterm #'sigterm-handler)
220   (enable-interrupt sigill #'sigill-handler)
221   #!-linux
222   (enable-interrupt sigemt #'sigemt-handler)
223   (enable-interrupt sigfpe #'sb!vm:sigfpe-handler)
224   (enable-interrupt sigbus #'sigbus-handler)
225   #!-linux
226   (enable-interrupt sigsys #'sigsys-handler)
227   (enable-interrupt sigalrm #'sigalrm-handler)
228   (enable-interrupt sigpipe #'sigpipe-handler)
229   (enable-interrupt sigchld #'sigchld-handler)
230   #!+hpux (ignore-interrupt sigxcpu)
231   (unblock-gc-signals)
232   (unblock-deferrable-signals)
233   (values))
234 \f
235 ;;;; etc.
236
237 ;;; extract si_code from siginfo_t
238 (sb!alien:define-alien-routine ("siginfo_code" siginfo-code) sb!alien:int
239   (info system-area-pointer))
240
241 ;;; CMU CL comment:
242 ;;;   Magically converted by the compiler into a break instruction.
243 (defun receive-pending-interrupt ()
244   (receive-pending-interrupt))