1996-08-22 16:13:14 -04:00
|
|
|
;;; Copyright (c) 1993 by Olin Shivers.
|
|
|
|
;;; Signal handler system
|
|
|
|
|
|
|
|
;;; The principal trickiness here is that we have to interface to Unix signals
|
|
|
|
;;; *through* an intermediate interface, the S48 vm's idea of interrupts.
|
|
|
|
;;; So there is a difference between delivering a signal to the underlying
|
|
|
|
;;; Unix process and delivering it to the program that runs on the VM.
|
1996-09-12 00:34:28 -04:00
|
|
|
;;;
|
|
|
|
;;; One effect is that we have two separate codes for the same thing -- the
|
|
|
|
;;; Unix signal code, and the S48 interrupt value. E.g., SIGNAL/TSTP and
|
|
|
|
;;; INTERRUPT/TSTP.
|
1996-08-22 16:13:14 -04:00
|
|
|
|
|
|
|
;;; These system calls can return EINTR or restart. In order for the S48 vm's
|
|
|
|
;;; interrupt system to detect a signal and invoke the handler, they *must*
|
|
|
|
;;; return EINTR, and this must cause a return from C to Scheme.
|
|
|
|
;;;
|
|
|
|
;;; open close dup2 accept connect
|
|
|
|
;;; read recv recvfrom recvmsg
|
|
|
|
;;; write send sendto sendmsg
|
|
|
|
;;; select
|
|
|
|
;;; wait
|
|
|
|
;;; fcntl* ioctl
|
|
|
|
;;; sigsuspend
|
|
|
|
;;; HP-UX, but I don't use: poll lockf msem_lock msgsnd msgrcv semop
|
|
|
|
;;;
|
|
|
|
;;; * Only during a F_SETLKW
|
1996-09-12 00:34:28 -04:00
|
|
|
;;;
|
|
|
|
;;; From rts/interrupt.scm (package interrupts, interface interrupts-interface)
|
1996-11-11 12:05:54 -05:00
|
|
|
;;; WITH-INTERRUPTS INTERRUPT-HANDLERS SET-ENABLED-INTERRUPTS !
|
1996-09-12 00:34:28 -04:00
|
|
|
;;; ENABLED-INTERRUPTS
|
|
|
|
;;; Must define WITH-INTERRUPTS* and WITH-INTERRUPTS.
|
1996-08-22 16:13:14 -04:00
|
|
|
|
|
|
|
(foreign-source
|
|
|
|
"extern int errno;"
|
|
|
|
""
|
|
|
|
"/* Make sure foreign-function stubs interface to the C funs correctly: */"
|
|
|
|
"#include \"sighandlers1.h\""
|
|
|
|
"" "")
|
|
|
|
|
1996-09-12 00:34:28 -04:00
|
|
|
;;; Map a Unix async signal to its S48 interrupt value.
|
|
|
|
;;; -1 => Not defined.
|
|
|
|
(define-foreign %signal->interrupt (sig2interrupt (integer sig))
|
|
|
|
integer)
|
|
|
|
|
|
|
|
(define (signal->interrupt sig)
|
|
|
|
(let ((int (%signal->interrupt sig)))
|
|
|
|
(if (>= int 0) int
|
|
|
|
(error "Unix signal has no Scheme 48 interrupt." sig))))
|
|
|
|
|
1996-08-22 16:13:14 -04:00
|
|
|
|
|
|
|
(define (interrupt-set . interrupts)
|
|
|
|
(let lp ((ints interrupts) (ans 0))
|
|
|
|
(if (pair? ints)
|
|
|
|
(lp (cdr ints) (bitwise-ior ans (arithmetic-shift 1 (- (car ints) 1))))
|
|
|
|
ans)))
|
|
|
|
|
1996-11-11 12:05:54 -05:00
|
|
|
;;; I'm trying to be consistent about the ! suffix -- I don't use it
|
|
|
|
;;; when frobbing process state. This is not a great rule; perhaps I
|
|
|
|
;;; should change it.
|
|
|
|
(define set-enabled-interrupts set-enabled-interrupts!)
|
|
|
|
|
1996-09-12 00:34:28 -04:00
|
|
|
(define-simple-syntax (with-enabled-interrupts mask body ...)
|
|
|
|
(with-interrupts mask (lambda () body ...)))
|
1996-08-22 16:13:14 -04:00
|
|
|
|
1996-09-12 00:34:28 -04:00
|
|
|
(define with-enabled-interrupts* with-interrupts)
|
1996-08-22 16:13:14 -04:00
|
|
|
|
1996-11-10 08:58:46 -05:00
|
|
|
(define interrupt/alarm (enum interrupt alarm))
|
|
|
|
(define interrupt/keyboard (enum interrupt keyboard))
|
|
|
|
(define interrupt/memory-shortage (enum interrupt memory-shortage))
|
|
|
|
(define interrupt/chld (enum interrupt chld))
|
|
|
|
(define interrupt/cont (enum interrupt cont))
|
|
|
|
(define interrupt/hup (enum interrupt hup))
|
|
|
|
(define interrupt/quit (enum interrupt quit))
|
|
|
|
(define interrupt/term (enum interrupt term))
|
|
|
|
(define interrupt/tstp (enum interrupt tstp))
|
|
|
|
(define interrupt/usr1 (enum interrupt usr1))
|
|
|
|
(define interrupt/usr2 (enum interrupt usr2))
|
|
|
|
(define interrupt/info (enum interrupt info))
|
|
|
|
(define interrupt/io (enum interrupt io))
|
|
|
|
(define interrupt/poll (enum interrupt poll))
|
|
|
|
(define interrupt/prof (enum interrupt prof))
|
|
|
|
(define interrupt/pwr (enum interrupt pwr))
|
|
|
|
(define interrupt/urg (enum interrupt urg))
|
|
|
|
(define interrupt/vtalrm (enum interrupt vtalrm))
|
|
|
|
(define interrupt/winch (enum interrupt winch))
|
|
|
|
(define interrupt/xcpu (enum interrupt xcpu))
|
|
|
|
(define interrupt/xfsz (enum interrupt xfsz))
|
|
|
|
|
|
|
|
(define interrupt/int interrupt/keyboard)
|
|
|
|
(define interrupt/alrm interrupt/alarm)
|
|
|
|
|
1996-08-22 16:13:14 -04:00
|
|
|
|
|
|
|
;;; Get/Set signal handlers
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;;; When you set a signal's handler to "default," if the default for that
|
|
|
|
;;; signal is something other than "ignore," we actually install this guy.
|
|
|
|
;;; When he is called by the S48 interrupt system, he'll magically make
|
|
|
|
;;; the default action happen (by calling C code that *really* sets the
|
|
|
|
;;; handler to SIGDFL, and then re-sending the signal). This basically
|
|
|
|
;;; terminates the process, since if the default isn't "ignore," it's always
|
|
|
|
;;; "terminate" of some kind. Doing it this way means the exit code given
|
|
|
|
;;; to our waiting parent proc correctly reflects how we died, and also
|
|
|
|
;;; makes the core dump happen if it should. Details, details.
|
|
|
|
|
|
|
|
(define-foreign %do-default-sigaction (do_default_sigaction (fixnum signal))
|
|
|
|
ignore)
|
|
|
|
|
1996-11-10 08:58:46 -05:00
|
|
|
(define default-int-handler-vec
|
|
|
|
;; Non-Unix-signal interrupts just get their default values from
|
|
|
|
;; the current value of I-H.
|
|
|
|
(let ((v (copy-vector interrupt-handlers)))
|
|
|
|
(do ((sig 31 (- sig 1))) ; For each Unix signal
|
|
|
|
((< sig 0)) ; make & install a default
|
|
|
|
(let ((i (%signal->interrupt sig))) ; signal handler.
|
|
|
|
(if (>= i 0) ; Don't mess with non-signal interrupts.
|
|
|
|
(vector-set! v i (if (memv sig signals-ignored-by-default)
|
|
|
|
(lambda (enabled-interrupts) #f)
|
|
|
|
(lambda (enabled-interrupts)
|
|
|
|
(%do-default-sigaction sig)))))))
|
|
|
|
v))
|
1996-09-12 00:34:28 -04:00
|
|
|
|
|
|
|
;;; HANDLER is #f (ignore), #t (default), or a procedure taking an integer
|
|
|
|
;;; argument. The interrupt is delivered to a procedure by (1) setting the
|
|
|
|
;;; ENABLED-INTERRUPTS register to 0 (i.e., blocking all interrupts), and (2)
|
|
|
|
;;; applying the procedure to the previous value of the ENABLED-INTERRUPTS
|
1996-11-10 08:58:46 -05:00
|
|
|
;;; register. If the procedure returns normally, the ENABLED-INTERRUPTS
|
|
|
|
;;; register will be restored to its previous value.
|
1996-09-12 00:34:28 -04:00
|
|
|
|
1996-11-07 10:59:31 -05:00
|
|
|
;;; This handler does nothing -- used when the handler is #f.
|
|
|
|
(define (noop-sig-handler enabled-interrupts) #f)
|
|
|
|
|
1996-11-11 11:21:53 -05:00
|
|
|
(define (set-interrupt-handler int handler)
|
1996-11-10 08:58:46 -05:00
|
|
|
(let ((ohandler (interrupt-handler int)))
|
|
|
|
(vector-set! interrupt-handlers int
|
|
|
|
(case handler
|
|
|
|
((#t) (vector-ref default-int-handler-vec int))
|
|
|
|
((#f) noop-sig-handler)
|
|
|
|
(else handler)))
|
1997-03-09 02:28:20 -05:00
|
|
|
|
|
|
|
(cond ((and (not handler) ohandler ; Toggling from something
|
|
|
|
(int->signal int)) => ; to ignored.
|
|
|
|
(lambda (sig)
|
|
|
|
(%set-unix-signal-handler sig 0)))
|
1998-06-16 14:05:44 -04:00
|
|
|
|
1997-03-09 02:28:20 -05:00
|
|
|
((and handler (not ohandler) ; Toggling from ignored
|
|
|
|
(int->signal int)) => ; to something.
|
1998-06-16 14:05:44 -04:00
|
|
|
(lambda (sig)
|
|
|
|
(%set-unix-signal-handler sig 2))))
|
1997-03-09 02:28:20 -05:00
|
|
|
|
1996-11-10 08:58:46 -05:00
|
|
|
ohandler))
|
|
|
|
|
|
|
|
(define (interrupt-handler int)
|
|
|
|
(let ((handler (vector-ref interrupt-handlers int)))
|
|
|
|
(cond ((eq? handler (vector-ref default-int-handler-vec int)) #t)
|
1996-11-07 10:59:31 -05:00
|
|
|
((eq? handler noop-sig-handler) #f)
|
|
|
|
(else handler))))
|
1996-08-22 16:13:14 -04:00
|
|
|
|
1996-09-12 00:34:28 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; Set the Unix signal handler. One doesn't usually use this; one usually
|
|
|
|
;;; uses the S48 VM's interrupt system.
|
|
|
|
;;; HANDLER-CODE: 0 => ignore, 1 => default, 2 => S48 VM
|
|
|
|
;;; Returns equivalent code, additionally 3 => other handler.
|
|
|
|
;;; Raises an error exception if there's a problem.
|
|
|
|
|
1996-11-11 11:21:53 -05:00
|
|
|
(define (%set-unix-signal-handler sig handler-code)
|
1996-09-12 00:34:28 -04:00
|
|
|
(check-arg (lambda (x) (and (integer? sig) (< 0 sig 32)))
|
|
|
|
sig
|
1996-11-11 11:21:53 -05:00
|
|
|
%set-unix-signal-handler)
|
1996-09-12 00:34:28 -04:00
|
|
|
(check-arg (lambda (x) (and (integer? handler-code) (<= 0 handler-code 2)))
|
|
|
|
handler-code
|
1996-11-11 11:21:53 -05:00
|
|
|
%set-unix-signal-handler)
|
1996-09-12 00:34:28 -04:00
|
|
|
(let retry ()
|
1997-03-10 18:46:42 -05:00
|
|
|
(receive (err old-hc old-flags)
|
|
|
|
(%%set-unix-signal-handler sig handler-code 0)
|
1996-09-12 00:34:28 -04:00
|
|
|
(cond ((not err) old-hc)
|
|
|
|
((= err errno/intr) (retry))
|
1996-11-11 11:21:53 -05:00
|
|
|
(else (errno-error err %set-unix-signal-handler sig handler-code))))))
|
1996-09-12 00:34:28 -04:00
|
|
|
|
1996-11-11 11:21:53 -05:00
|
|
|
(define-foreign %%set-unix-signal-handler
|
1996-09-12 00:34:28 -04:00
|
|
|
(scsh_set_sig (fixnum sig) (fixnum hc) (fixnum flags))
|
|
|
|
desc ; #f or errno
|
|
|
|
integer ; previous handler-code
|
|
|
|
integer) ; previous handler flags
|
|
|
|
|
|
|
|
(define (%unix-signal-handler sig)
|
|
|
|
(check-arg (lambda (x) (and (integer? sig) (< 0 sig 32)))
|
|
|
|
sig
|
|
|
|
%unix-signal-handler)
|
|
|
|
(let retry ()
|
|
|
|
(receive (err hc flags) (%%unix-signal-handler sig)
|
|
|
|
(cond ((not err) hc)
|
|
|
|
((= err errno/intr) (retry))
|
|
|
|
(else (errno-error err %unix-signal-handler sig))))))
|
|
|
|
|
|
|
|
(define-foreign %%unix-signal-handler (scsh_get_sig (fixnum sig))
|
1996-08-22 16:13:14 -04:00
|
|
|
desc ; #f or errno
|
1996-09-12 00:34:28 -04:00
|
|
|
integer ; previous handler-code
|
|
|
|
integer) ; previous handler flags
|
|
|
|
|
|
|
|
(define-foreign %install-unix-scsh-handlers (install_scsh_handlers) ignore)
|
|
|
|
|
|
|
|
(define-foreign %%get-int-handlers (get_int_handlers) desc)
|
1996-08-22 16:13:14 -04:00
|
|
|
|
1996-09-12 00:34:28 -04:00
|
|
|
(define (%install-scsh-handlers)
|
|
|
|
(do ((sig 32 (- sig 1)))
|
|
|
|
((< sig 0))
|
|
|
|
(let ((i (%signal->interrupt sig)))
|
1996-11-10 08:58:46 -05:00
|
|
|
(if (not (or (= i -1)
|
|
|
|
(= sig signal/int) ; Leave ^c and
|
|
|
|
(= sig signal/alrm))) ; alarm handlers alone.
|
1996-09-12 00:34:28 -04:00
|
|
|
(vector-set! interrupt-handlers i
|
1996-11-10 08:58:46 -05:00
|
|
|
(vector-ref default-int-handler-vec i))))))
|
1997-03-09 02:28:20 -05:00
|
|
|
|
1997-03-09 23:57:18 -05:00
|
|
|
;;; I am ashamed to say the 33 below is completely bogus.
|
|
|
|
;;; What we want is a value that is 1 + max interrupt value.
|
|
|
|
|
1997-03-09 02:28:20 -05:00
|
|
|
(define int->sig-vec
|
1997-03-09 23:57:18 -05:00
|
|
|
(let ((v (make-vector 33 #f)))
|
1997-03-09 02:28:20 -05:00
|
|
|
(do ((sig 32 (- sig 1)))
|
|
|
|
((< sig 0))
|
|
|
|
(let ((i (%signal->interrupt sig)))
|
1997-03-09 23:57:18 -05:00
|
|
|
(if (not (= i -1)) (vector-set! v i sig))))
|
1997-03-09 02:28:20 -05:00
|
|
|
v))
|
|
|
|
|
|
|
|
(define (int->signal i) (and (<= 0 i 32) (vector-ref int->sig-vec i)))
|