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 ;;; These should probably be somewhere, but I don't know where.
15 (defconstant sig_dfl 0)
16 (defconstant sig_ign 1)
18 ;;;; system calls that deal with signals
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
25 (signal sb!alien:int))
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
32 (signal sb!alien:int))
34 ;;; Reset the current set of masked signals (those being blocked from
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.)
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)
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
52 (handler sb!alien:unsigned-long))
54 ;;;; interface to enabling and disabling signal handlers
56 (defun enable-interrupt (signal handler)
57 (declare (type (or function fixnum (member :default :ignore)) handler))
58 (/show0 "enable-interrupt")
60 (let ((result (install-handler signal
65 (sb!kernel:get-lisp-obj-address
67 (cond ((= result sig_dfl) :default)
68 ((= result sig_ign) :ignore)
69 (t (the (or function fixnum) (sb!kernel:make-lisp-obj result)))))))
71 (defun default-interrupt (signal)
72 (enable-interrupt signal :default))
74 (defun ignore-interrupt (signal)
75 (enable-interrupt signal :ignore))
77 ;;;; default LISP signal handlers
79 ;;;; Most of these just call ERROR to report the presence of the signal.
81 ;;; SIGINT is handled like BREAK, except that ANSI BREAK ignores
82 ;;; *DEBUGGER-HOOK*, but we want SIGINT's BREAK to respect it, so that
83 ;;; SIGINT in --disable-debugger mode will cleanly terminate the system
84 ;;; (by respecting the *DEBUGGER-HOOK* established in that mode).
85 (defun sigint-%break (format-string &rest format-arguments)
86 (apply #'%break 'sigint format-string format-arguments))
88 (eval-when (:compile-toplevel :execute)
89 (sb!xc:defmacro define-signal-handler (name
91 &optional (function 'error))
92 `(defun ,name (signal info context)
93 (declare (ignore signal info))
94 (declare (type system-area-pointer context))
95 (/show "in Lisp-level signal handler" ,(symbol-name name) (sap-int context))
96 (,function ,(concatenate 'simple-string what " at #X~X")
97 (with-alien ((context (* os-context-t) context))
98 (sap-int (sb!vm:context-pc context)))))))
100 (define-signal-handler sigint-handler "interrupted" sigint-%break)
101 (define-signal-handler sigill-handler "illegal instruction")
102 (define-signal-handler sigtrap-handler "breakpoint/trap")
103 (define-signal-handler sigiot-handler "SIGIOT")
105 (define-signal-handler sigemt-handler "SIGEMT")
106 (define-signal-handler sigbus-handler "bus error")
107 (define-signal-handler sigsegv-handler "segmentation violation")
109 (define-signal-handler sigsys-handler "bad argument to a system call")
110 (define-signal-handler sigpipe-handler "SIGPIPE")
112 (defun sigalrm-handler (signal info context)
113 (declare (ignore signal info context))
114 (declare (type system-area-pointer context))
115 (cerror "Continue" 'sb!ext::timeout))
117 (defun sigquit-handler (signal code context)
118 (declare (ignore signal code context))
119 (throw 'sb!impl::toplevel-catcher nil))
121 (defun sb!kernel:signal-cold-init-or-reinit ()
123 "Enable all the default signals that Lisp knows how to deal with."
124 (enable-interrupt sigint #'sigint-handler)
125 (enable-interrupt sigquit #'sigquit-handler)
126 (enable-interrupt sigill #'sigill-handler)
127 (enable-interrupt sigtrap #'sigtrap-handler)
128 (enable-interrupt sigiot #'sigiot-handler)
130 (enable-interrupt sigemt #'sigemt-handler)
131 (enable-interrupt sigfpe #'sb!vm:sigfpe-handler)
132 (enable-interrupt sigbus #'sigbus-handler)
133 (enable-interrupt sigsegv #'sigsegv-handler)
135 (enable-interrupt sigsys #'sigsys-handler)
136 (enable-interrupt sigpipe #'sigpipe-handler)
137 (enable-interrupt sigalrm #'sigalrm-handler)
143 ;;; Magically converted by the compiler into a break instruction.
144 (defun receive-pending-interrupt ()
145 (receive-pending-interrupt))
147 ;;; stale code which I'm insufficiently motivated to test -- WHN 19990714
149 ;;;; WITH-ENABLED-INTERRUPTS
151 (defmacro with-enabled-interrupts (interrupt-list &body body)
153 "With-enabled-interrupts ({(interrupt function)}*) {form}*
154 Establish function as a handler for the Unix signal interrupt which
155 should be a number between 1 and 31 inclusive."
161 ,@(do* ((item interrupt-list (cdr item))
162 (intr (caar item) (caar item))
163 (ifcn (cadar item) (cadar item))
165 ((null item) (nreverse forms))
167 (setq intr (symbol-value intr)))
168 (push `(push `(,,intr ,(enable-interrupt ,intr ,ifcn)) ,il)
171 (dolist (,it (nreverse ,il))
172 (enable-interrupt (car ,it) (cadr ,it)))))))