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 (defun invoke-interruption (function)
16 ;; Reset signal mask: the C-side handler has blocked all
17 ;; deferrable interrupts before arranging return to lisp. This is
18 ;; safe because we can't get a pending interrupt before we unblock
21 ;; FIXME: Should we not reset the _entire_ mask, just restore it
22 ;; to the state before we got the interrupt?
24 (allow-with-interrupts (funcall function
))))
26 (defmacro in-interruption
((&rest args
) &body body
)
28 "Convenience macro on top of INVOKE-INTERRUPTION."
29 `(invoke-interruption (lambda () ,@body
) ,@args
))
31 ;;;; system calls that deal with signals
33 ;;; Send the signal SIGNAL to the process with process id PID. SIGNAL
34 ;;; should be a valid signal number
35 #!-sb-fluid
(declaim (inline real-unix-kill
))
36 (sb!alien
:define-alien-routine
("kill" unix-kill
) sb
!alien
:int
38 (signal sb
!alien
:int
))
40 ;;; Send the signal SIGNAL to the all the process in process group
41 ;;; PGRP. SIGNAL should be a valid signal number
42 #!-sb-fluid
(declaim (inline real-unix-killpg
))
43 (sb!alien
:define-alien-routine
("killpg" unix-killpg
) sb
!alien
:int
45 (signal sb
!alien
:int
))
47 ;;; Reset the current set of masked signals (those being blocked from
50 ;;; (Note: CMU CL had a more general SIGSETMASK call and a SIGMASK
51 ;;; operator to create masks, but since we only ever reset to 0, we no
52 ;;; longer support it. If you need it, you can pull it out of the CMU
53 ;;; CL sources, or the old SBCL sources; but you might also consider
54 ;;; doing things the SBCL way and moving this kind of C-level work
55 ;;; down to C wrapper functions.)
57 ;;; When inappropriate build options are used, this also prints messages
58 ;;; listing the signals that were masked
59 (sb!alien
:define-alien-routine
"reset_signal_mask" sb
!alien
:void
)
62 ;;;; C routines that actually do all the work of establishing signal handlers
63 (sb!alien
:define-alien-routine
("install_handler" install-handler
)
64 sb
!alien
:unsigned-long
66 (handler sb
!alien
:unsigned-long
))
68 ;;;; interface to enabling and disabling signal handlers
70 (defun enable-interrupt (signal handler
)
71 (declare (type (or function fixnum
(member :default
:ignore
)) handler
))
72 (/show0
"enable-interrupt")
73 (flet ((run-handler (&rest args
)
75 (apply handler args
))))
77 (let ((result (install-handler signal
82 (sb!kernel
:get-lisp-obj-address
84 (cond ((= result sig-dfl
) :default
)
85 ((= result sig-ign
) :ignore
)
86 (t (the (or function fixnum
)
87 (sb!kernel
:make-lisp-obj result
))))))))
89 (defun default-interrupt (signal)
90 (enable-interrupt signal
:default
))
92 (defun ignore-interrupt (signal)
93 (enable-interrupt signal
:ignore
))
95 ;;;; default LISP signal handlers
97 ;;;; Most of these just call ERROR to report the presence of the signal.
99 ;;; SIGINT is handled like BREAK, except that ANSI BREAK ignores
100 ;;; *DEBUGGER-HOOK*, but we want SIGINT's BREAK to respect it, so that
101 ;;; SIGINT in --disable-debugger mode will cleanly terminate the system
102 ;;; (by respecting the *DEBUGGER-HOOK* established in that mode).
103 (eval-when (:compile-toplevel
:execute
)
104 (sb!xc
:defmacro define-signal-handler
(name what
&optional
(function 'error
))
105 `(defun ,name
(signal info context
)
106 (declare (ignore signal info
))
107 (declare (type system-area-pointer context
))
108 (/show
"in Lisp-level signal handler" ,(symbol-name name
)
111 (,function
,(concatenate 'simple-string what
" at #X~X")
112 (with-alien ((context (* os-context-t
) context
))
113 (sap-int (sb!vm
:context-pc context
))))))))
115 (define-signal-handler sigill-handler
"illegal instruction")
117 (define-signal-handler sigemt-handler
"SIGEMT")
118 (define-signal-handler sigbus-handler
"bus error")
119 (define-signal-handler sigsegv-handler
"segmentation violation")
121 (define-signal-handler sigsys-handler
"bad argument to a system call")
123 (defun sigint-handler (signal info context
)
124 (declare (ignore signal info
))
125 (declare (type system-area-pointer context
))
126 (/show
"in Lisp-level SIGINT handler" (sap-int context
))
127 (flet ((interrupt-it ()
128 (with-alien ((context (* os-context-t
) context
))
129 (%break
'sigint
'interactive-interrupt
131 :address
(sap-int (sb!vm
:context-pc context
))))))
132 (sb!thread
:interrupt-thread
(sb!thread
::foreground-thread
)
135 (defun sigalrm-handler (signal info context
)
136 (declare (ignore signal info context
))
137 (declare (type system-area-pointer context
))
138 (sb!impl
::run-expired-timers
))
140 (defun sigterm-handler (signal code context
)
141 (declare (ignore signal code context
))
142 (sb!thread
::terminate-session
)
145 ;; Also known as SIGABRT.
146 (defun sigiot-handler (signal code context
)
147 (declare (ignore signal code context
))
150 (defun sb!kernel
:signal-cold-init-or-reinit
()
152 "Enable all the default signals that Lisp knows how to deal with."
153 (enable-interrupt sigint
#'sigint-handler
)
154 (enable-interrupt sigterm
#'sigterm-handler
)
155 (enable-interrupt sigill
#'sigill-handler
)
156 (enable-interrupt sigiot
#'sigiot-handler
)
158 (enable-interrupt sigemt
#'sigemt-handler
)
159 (enable-interrupt sigfpe
#'sb
!vm
:sigfpe-handler
)
160 (enable-interrupt sigbus
#'sigbus-handler
)
161 (enable-interrupt sigsegv
#'sigsegv-handler
)
163 (enable-interrupt sigsys
#'sigsys-handler
)
164 (ignore-interrupt sigpipe
)
165 (enable-interrupt sigalrm
#'sigalrm-handler
)
166 (sb!unix
::reset-signal-mask
)
171 ;;; extract si_code from siginfo_t
172 (sb!alien
:define-alien-routine
("siginfo_code" siginfo-code
) sb
!alien
:int
173 (info system-area-pointer
))
176 ;;; Magically converted by the compiler into a break instruction.
177 (defun receive-pending-interrupt ()
178 (receive-pending-interrupt))