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")
17 ;;; These should probably be somewhere, but I don't know where.
18 (defconstant sig_dfl 0)
19 (defconstant sig_ign 1)
21 ;;;; system calls that deal with signals
23 #!-sb-fluid (declaim (inline real-unix-kill))
24 (sb!alien:def-alien-routine ("kill" real-unix-kill) sb!c-call:int
26 (signal sb!c-call:int))
28 (defun unix-kill (pid signal)
30 "Unix-kill sends the signal signal to the process with process
31 id pid. Signal should be a valid signal number or a keyword of the
32 standard UNIX signal name."
33 (real-unix-kill pid (unix-signal-number signal)))
35 #!-sb-fluid (declaim (inline real-unix-killpg))
36 (sb!alien:def-alien-routine ("killpg" real-unix-killpg) sb!c-call:int
38 (signal sb!c-call:int))
40 (defun unix-killpg (pgrp signal)
42 "Unix-killpg sends the signal signal to the all the process in process
43 group PGRP. Signal should be a valid signal number or a keyword of
44 the standard UNIX signal name."
45 (real-unix-killpg pgrp (unix-signal-number signal)))
47 (sb!alien:def-alien-routine ("sigblock" unix-sigblock) sb!c-call:unsigned-long
49 "Unix-sigblock cause the signals specified in mask to be
50 added to the set of signals currently being blocked from
51 delivery. The macro sigmask is provided to create masks."
52 (mask sb!c-call:unsigned-long))
54 (sb!alien:def-alien-routine ("sigpause" unix-sigpause) sb!c-call:void
56 "Unix-sigpause sets the set of masked signals to its argument
57 and then waits for a signal to arrive, restoring the previous
58 mask upon its return."
59 (mask sb!c-call:unsigned-long))
61 (sb!alien:def-alien-routine ("sigsetmask" unix-sigsetmask)
62 sb!c-call:unsigned-long
64 "Unix-sigsetmask sets the current set of masked signals (those
65 begin blocked from delivery) to the argument. The macro sigmask
66 can be used to create the mask. The previous value of the signal
68 (mask sb!c-call:unsigned-long))
70 ;;;; C routines that actually do all the work of establishing signal handlers
71 (sb!alien:def-alien-routine ("install_handler" install-handler)
72 sb!c-call:unsigned-long
73 (signal sb!c-call:int)
74 (handler sb!c-call:unsigned-long))
76 ;;;; interface to enabling and disabling signal handlers
78 (defun enable-interrupt (signal handler)
79 (declare (type (or function (member :default :ignore)) handler))
81 (let ((result (install-handler (unix-signal-number signal)
86 (sb!kernel:get-lisp-obj-address
88 (cond ((= result sig_dfl) :default)
89 ((= result sig_ign) :ignore)
90 (t (the function (sb!kernel:make-lisp-obj result)))))))
92 (defun default-interrupt (signal)
93 (enable-interrupt signal :default))
95 (defun ignore-interrupt (signal)
96 (enable-interrupt signal :ignore))
98 ;;;; default LISP signal handlers
100 ;;;; Most of these just call ERROR to report the presence of the signal.
102 (eval-when (:compile-toplevel :execute)
103 (sb!xc:defmacro define-signal-handler (name
105 &optional (function 'error))
106 `(defun ,name (signal info context)
107 (declare (ignore signal info))
108 (declare (type system-area-pointer context))
109 (/show "in Lisp-level signal handler" (sap-int context))
110 (,function ,(concatenate 'simple-string what " at #X~X")
111 (with-alien ((context (* os-context-t) context))
112 (sap-int (sb!vm:context-pc context)))))))
114 (define-signal-handler sigint-handler "interrupted" break)
115 (define-signal-handler sigill-handler "illegal instruction")
116 (define-signal-handler sigtrap-handler "breakpoint/trap")
117 (define-signal-handler sigiot-handler "SIGIOT")
119 (define-signal-handler sigemt-handler "SIGEMT")
120 (define-signal-handler sigbus-handler "bus error")
121 (define-signal-handler sigsegv-handler "segmentation violation")
123 (define-signal-handler sigsys-handler "bad argument to a system call")
124 (define-signal-handler sigpipe-handler "SIGPIPE")
125 (define-signal-handler sigalrm-handler "SIGALRM")
127 (defun sigquit-handler (signal code context)
128 (declare (ignore signal code context))
129 (throw 'sb!impl::top-level-catcher nil))
131 (defun sb!kernel:signal-cold-init-or-reinit ()
133 "Enable all the default signals that Lisp knows how to deal with."
134 (enable-interrupt :sigint #'sigint-handler)
135 (enable-interrupt :sigquit #'sigquit-handler)
136 (enable-interrupt :sigill #'sigill-handler)
137 (enable-interrupt :sigtrap #'sigtrap-handler)
138 (enable-interrupt :sigiot #'sigiot-handler)
140 (enable-interrupt :sigemt #'sigemt-handler)
141 (enable-interrupt :sigfpe #'sb!vm:sigfpe-handler)
142 (enable-interrupt :sigbus #'sigbus-handler)
143 (enable-interrupt :sigsegv #'sigsegv-handler)
145 (enable-interrupt :sigsys #'sigsys-handler)
146 (enable-interrupt :sigpipe #'sigpipe-handler)
147 (enable-interrupt :sigalrm #'sigalrm-handler)
150 ;;; stale code which I'm insufficiently motivated to test -- WHN 19990714
152 ;;;; WITH-ENABLED-INTERRUPTS
154 (defmacro with-enabled-interrupts (interrupt-list &body body)
156 "With-enabled-interrupts ({(interrupt function)}*) {form}*
157 Establish function as a handler for the Unix signal interrupt which
158 should be a number between 1 and 31 inclusive."
164 ,@(do* ((item interrupt-list (cdr item))
165 (intr (caar item) (caar item))
166 (ifcn (cadar item) (cadar item))
168 ((null item) (nreverse forms))
170 (setq intr (symbol-value intr)))
171 (push `(push `(,,intr ,(enable-interrupt ,intr ,ifcn)) ,il)
174 (dolist (,it (nreverse ,il))
175 (enable-interrupt (car ,it) (cadr ,it)))))))