c492023c0a567247ec6fddca6793830a97178cd1
[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 ;;; These should probably be somewhere, but I don't know where.
15 (defconstant sig_dfl 0)
16 (defconstant sig_ign 1)
17 \f
18 ;;;; system calls that deal with signals
19
20 ;;; Send the signal SIGNAL to the process with process id PID. SIGNAL
21 ;;; should be a valid signal number
22 #!-sb-fluid (declaim (inline real-unix-kill))
23 (sb!alien:define-alien-routine ("kill" unix-kill) sb!alien:int
24   (pid sb!alien:int)
25   (signal sb!alien:int))
26
27 ;;; Send the signal SIGNAL to the all the process in process group
28 ;;; PGRP. SIGNAL should be a valid signal number
29 #!-sb-fluid (declaim (inline real-unix-killpg))
30 (sb!alien:define-alien-routine ("killpg" unix-killpg) sb!alien:int
31   (pgrp sb!alien:int)
32   (signal sb!alien:int))
33
34 ;;; Reset the current set of masked signals (those being blocked from
35 ;;; delivery).
36 ;;;
37 ;;; (Note: CMU CL had a more general SIGSETMASK call and a SIGMASK
38 ;;; operator to create masks, but since we only ever reset to 0, we no
39 ;;; longer support it. If you need it, you can pull it out of the CMU
40 ;;; CL sources, or the old SBCL sources; but you might also consider
41 ;;; doing things the SBCL way and moving this kind of C-level work
42 ;;; down to C wrapper functions.)
43
44 ;;; When inappropriate build options are used, this also prints messages
45 ;;; listing the signals that were masked
46 (sb!alien:define-alien-routine "reset_signal_mask" sb!alien:void)
47 \f
48 ;;;; C routines that actually do all the work of establishing signal handlers
49 (sb!alien:define-alien-routine ("install_handler" install-handler)
50                                sb!alien:unsigned-long
51   (signal sb!alien:int)
52   (handler sb!alien:unsigned-long))
53
54 ;;;; interface to enabling and disabling signal handlers
55
56 (defun enable-interrupt (signal handler)
57   (declare (type (or function fixnum (member :default :ignore)) handler))
58   (without-gcing
59    (let ((result (install-handler signal
60                                   (case handler
61                                     (:default sig_dfl)
62                                     (:ignore sig_ign)
63                                     (t
64                                      (sb!kernel:get-lisp-obj-address
65                                       handler))))))
66      (cond ((= result sig_dfl) :default)
67            ((= result sig_ign) :ignore)
68            (t (the (or function fixnum) (sb!kernel:make-lisp-obj result)))))))
69
70 (defun default-interrupt (signal)
71   (enable-interrupt signal :default))
72
73 (defun ignore-interrupt (signal)
74   (enable-interrupt signal :ignore))
75 \f
76 ;;;; default LISP signal handlers
77 ;;;;
78 ;;;; Most of these just call ERROR to report the presence of the signal.
79
80 ;;; SIGINT is handled like BREAK, except that ANSI BREAK ignores
81 ;;; *DEBUGGER-HOOK*, but we want SIGINT's BREAK to respect it, so that
82 ;;; SIGINT in --disable-debugger mode will cleanly terminate the system
83 ;;; (by respecting the *DEBUGGER-HOOK* established in that mode).
84 (defun sigint-%break (format-string &rest format-arguments)
85   (apply #'%break 'sigint format-string format-arguments))
86
87 (eval-when (:compile-toplevel :execute)
88   (sb!xc:defmacro define-signal-handler (name
89                                          what
90                                          &optional (function 'error))
91     `(defun ,name (signal info context)
92        (declare (ignore signal info))
93        (declare (type system-area-pointer context))
94        (/show "in Lisp-level signal handler" ,(symbol-name name) (sap-int context))
95        (,function ,(concatenate 'simple-string what " at #X~X")
96                   (with-alien ((context (* os-context-t) context))
97                     (sap-int (sb!vm:context-pc context)))))))
98
99 (define-signal-handler sigint-handler "interrupted" sigint-%break)
100 (define-signal-handler sigill-handler "illegal instruction")
101 (define-signal-handler sigtrap-handler "breakpoint/trap")
102 (define-signal-handler sigiot-handler "SIGIOT")
103 #!-linux
104 (define-signal-handler sigemt-handler "SIGEMT")
105 (define-signal-handler sigbus-handler "bus error")
106 (define-signal-handler sigsegv-handler "segmentation violation")
107 #!-linux
108 (define-signal-handler sigsys-handler "bad argument to a system call")
109 (define-signal-handler sigpipe-handler "SIGPIPE")
110
111 (defun sigalrm-handler (signal info context)
112   (declare (ignore signal info context))
113   (declare (type system-area-pointer context))
114   (cerror "Continue" 'sb!ext::timeout))
115
116 (defun sigquit-handler (signal code context)
117   (declare (ignore signal code context))
118   (throw 'sb!impl::toplevel-catcher nil))
119
120 (defun sb!kernel:signal-cold-init-or-reinit ()
121   #!+sb-doc
122   "Enable all the default signals that Lisp knows how to deal with."
123   (enable-interrupt sigint #'sigint-handler)
124   (enable-interrupt sigquit #'sigquit-handler)
125   (enable-interrupt sigill #'sigill-handler)
126   (enable-interrupt sigtrap #'sigtrap-handler)
127   (enable-interrupt sigiot #'sigiot-handler)
128   #!-linux
129   (enable-interrupt sigemt #'sigemt-handler)
130   (enable-interrupt sigfpe #'sb!vm:sigfpe-handler)
131   (enable-interrupt sigbus #'sigbus-handler)
132   (enable-interrupt sigsegv #'sigsegv-handler)
133   #!-linux
134   (enable-interrupt sigsys #'sigsys-handler)
135   (enable-interrupt sigpipe #'sigpipe-handler)
136   (enable-interrupt sigalrm #'sigalrm-handler)
137   (values))
138 \f
139 ;;;; etc.
140
141 ;;; CMU CL comment:
142 ;;;   Magically converted by the compiler into a break instruction.
143 (defun receive-pending-interrupt ()
144   (receive-pending-interrupt))
145 \f
146 ;;; stale code which I'm insufficiently motivated to test -- WHN 19990714
147 #|
148 ;;;; WITH-ENABLED-INTERRUPTS
149
150 (defmacro with-enabled-interrupts (interrupt-list &body body)
151   #!+sb-doc
152   "With-enabled-interrupts ({(interrupt function)}*) {form}*
153    Establish function as a handler for the Unix signal interrupt which
154    should be a number between 1 and 31 inclusive."
155   (let ((il (gensym))
156         (it (gensym)))
157     `(let ((,il NIL))
158        (unwind-protect
159            (progn
160              ,@(do* ((item interrupt-list (cdr item))
161                      (intr (caar item) (caar item))
162                      (ifcn (cadar item) (cadar item))
163                      (forms NIL))
164                     ((null item) (nreverse forms))
165                  (when (symbolp intr)
166                    (setq intr (symbol-value intr)))
167                  (push `(push `(,,intr ,(enable-interrupt ,intr ,ifcn)) ,il)
168                        forms))
169              ,@body)
170          (dolist (,it (nreverse ,il))
171            (enable-interrupt (car ,it) (cadr ,it)))))))
172 |#