0.7.2.6:
[sbcl.git] / src / runtime / interrupt.c
1 /*
2  * interrupt-handling magic
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <signal.h>
20
21 #include "runtime.h"
22 #include "arch.h"
23 #include "sbcl.h"
24 #include "os.h"
25 #include "interrupt.h"
26 #include "globals.h"
27 #include "lispregs.h"
28 #include "validate.h"
29 #include "monitor.h"
30 #include "gc.h"
31 #include "alloc.h"
32 #include "dynbind.h"
33 #include "interr.h"
34
35 void sigaddset_blockable(sigset_t *s)
36 {
37     sigaddset(s, SIGHUP);
38     sigaddset(s, SIGINT);
39     sigaddset(s, SIGQUIT);
40     sigaddset(s, SIGPIPE);
41     sigaddset(s, SIGALRM);
42     sigaddset(s, SIGURG);
43     sigaddset(s, SIGFPE);
44     sigaddset(s, SIGTSTP);
45     sigaddset(s, SIGCHLD);
46     sigaddset(s, SIGIO);
47     sigaddset(s, SIGXCPU);
48     sigaddset(s, SIGXFSZ);
49     sigaddset(s, SIGVTALRM);
50     sigaddset(s, SIGPROF);
51     sigaddset(s, SIGWINCH);
52     sigaddset(s, SIGUSR1);
53     sigaddset(s, SIGUSR2);
54 }
55
56 /* When we catch an internal error, should we pass it back to Lisp to
57  * be handled in a high-level way? (Early in cold init, the answer is
58  * 'no', because Lisp is still too brain-dead to handle anything.
59  * After sufficient initialization has been completed, the answer
60  * becomes 'yes'.) */
61 boolean internal_errors_enabled = 0;
62
63 os_context_t *lisp_interrupt_contexts[MAX_INTERRUPTS];
64
65 /* As far as I can tell, what's going on here is:
66  *
67  * In the case of most signals, when Lisp asks us to handle the
68  * signal, the outermost handler (the one actually passed to UNIX) is
69  * either interrupt_handle_now(..) or interrupt_handle_later(..).
70  * In that case, the Lisp-level handler is stored in interrupt_handlers[..]
71  * and interrupt_low_level_handlers[..] is cleared.
72  *
73  * However, some signals need special handling, e.g. 
74  *
75  * o the SIGSEGV (for e.g. Linux) or SIGBUS (for e.g. FreeBSD) used by the
76  *   garbage collector to detect violations of write protection,
77  *   because some cases of such signals (e.g. GC-related violations of
78  *   write protection) are handled at C level and never passed on to
79  *   Lisp. For such signals, we still store any Lisp-level handler
80  *   in interrupt_handlers[..], but for the outermost handle we use
81  *   the value from interrupt_low_level_handlers[..], instead of the
82  *   ordinary interrupt_handle_now(..) or interrupt_handle_later(..).
83  *
84  * o the SIGTRAP (Linux/Alpha) which Lisp code uses to handle breakpoints,
85  *   pseudo-atomic sections, and some classes of error (e.g. "function
86  *   not defined").  This never goes anywhere near the Lisp handlers at all.
87  *   See runtime/alpha-arch.c and code/signal.lisp 
88  * 
89  * - WHN 20000728, dan 20010128 */
90
91
92 void (*interrupt_low_level_handlers[NSIG]) (int, siginfo_t*, void*) = {0};
93 union interrupt_handler interrupt_handlers[NSIG];
94
95 /* signal number, siginfo_t, and old mask information for pending signal
96  *
97  * pending_signal=0 when there is no pending signal. */
98 static int pending_signal = 0;
99 static siginfo_t pending_info;
100 static sigset_t pending_mask;
101
102 static boolean maybe_gc_pending = 0;
103 \f
104 /*
105  * utility routines used by various signal handlers
106  */
107
108 void
109 fake_foreign_function_call(os_context_t *context)
110 {
111     int context_index;
112 #ifndef __i386__
113     lispobj oldcont;
114 #endif
115
116     /* Get current Lisp state from context. */
117 #ifdef reg_ALLOC
118     dynamic_space_free_pointer =
119         (lispobj *)(*os_context_register_addr(context, reg_ALLOC));
120 #ifdef alpha
121     if ((long)dynamic_space_free_pointer & 1) {
122         lose("dead in fake_foreign_function_call, context = %x", context);
123     }
124 #endif
125 #endif
126 #ifdef reg_BSP
127     current_binding_stack_pointer =
128         (lispobj *)(*os_context_register_addr(context, reg_BSP));
129 #endif
130
131 #ifndef __i386__
132     /* Build a fake stack frame. */
133     current_control_frame_pointer =
134         (lispobj *)(*os_context_register_addr(context, reg_CSP));
135     if ((lispobj *)(*os_context_register_addr(context, reg_CFP))
136         == current_control_frame_pointer) {
137         /* There is a small window during call where the callee's
138          * frame isn't built yet. */
139         if (lowtag_of(*os_context_register_addr(context, reg_CODE))
140             == FUN_POINTER_LOWTAG) {
141             /* We have called, but not built the new frame, so
142              * build it for them. */
143             current_control_frame_pointer[0] =
144                 *os_context_register_addr(context, reg_OCFP);
145             current_control_frame_pointer[1] =
146                 *os_context_register_addr(context, reg_LRA);
147             current_control_frame_pointer += 8;
148             /* Build our frame on top of it. */
149             oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
150         }
151         else {
152             /* We haven't yet called, build our frame as if the
153              * partial frame wasn't there. */
154             oldcont = (lispobj)(*os_context_register_addr(context, reg_OCFP));
155         }
156     }
157     /* ### We can't tell whether we are still in the caller if it had
158      * to reg_ALLOCate the stack frame due to stack arguments. */
159     /* ### Can anything strange happen during return? */
160     else {
161         /* normal case */
162         oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
163     }
164
165     current_control_stack_pointer = current_control_frame_pointer + 8;
166
167     current_control_frame_pointer[0] = oldcont;
168     current_control_frame_pointer[1] = NIL;
169     current_control_frame_pointer[2] =
170         (lispobj)(*os_context_register_addr(context, reg_CODE));
171 #endif
172
173     /* Do dynamic binding of the active interrupt context index
174      * and save the context in the context array. */
175     context_index = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
176     /* FIXME: Ick! Why use abstract "make_fixnum" in some places if
177      * you're going to convert from fixnum by bare >>2 in other
178      * places? Use fixnum_value(..) here, and look for other places
179      * which do bare >> and << for fixnum_value and make_fixnum. */
180
181     if (context_index >= MAX_INTERRUPTS) {
182         lose("maximum interrupt nesting depth (%d) exceeded",
183              MAX_INTERRUPTS);
184     }
185
186     bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,
187                   make_fixnum(context_index + 1));
188
189     lisp_interrupt_contexts[context_index] = context;
190
191     /* no longer in Lisp now */
192     foreign_function_call_active = 1;
193 }
194
195 void
196 undo_fake_foreign_function_call(os_context_t *context)
197 {
198     /* Block all blockable signals. */
199     sigset_t block;
200     sigemptyset(&block);
201     sigaddset_blockable(&block);
202     sigprocmask(SIG_BLOCK, &block, 0);
203
204     /* going back into Lisp */
205     foreign_function_call_active = 0;
206
207     /* Undo dynamic binding. */
208     /* ### Do I really need to unbind_to_here()? */
209     /* FIXME: Is this to undo the binding of
210      * FREE_INTERRUPT_CONTEXT_INDEX? If so, we should say so. And
211      * perhaps yes, unbind_to_here() really would be clearer and less
212      * fragile.. */
213     /* dan (2001.08.10) thinks the above supposition is probably correct */
214     unbind();
215
216 #ifdef reg_ALLOC
217     /* Put the dynamic space free pointer back into the context. */
218     *os_context_register_addr(context, reg_ALLOC) =
219         (unsigned long) dynamic_space_free_pointer;
220 #endif
221 }
222
223 /* a handler for the signal caused by execution of a trap opcode
224  * signalling an internal error */
225 void
226 interrupt_internal_error(int signal, siginfo_t *info, os_context_t *context,
227                          boolean continuable)
228 {
229     lispobj context_sap = 0;
230
231     fake_foreign_function_call(context);
232
233     /* Allocate the SAP object while the interrupts are still
234      * disabled. */
235     if (internal_errors_enabled) {
236         context_sap = alloc_sap(context);
237     }
238
239     sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
240
241     if (internal_errors_enabled) {
242         SHOW("in interrupt_internal_error");
243 #if QSHOW
244         /* Display some rudimentary debugging information about the
245          * error, so that even if the Lisp error handler gets badly
246          * confused, we have a chance to determine what's going on. */
247         describe_internal_error(context);
248 #endif
249         funcall2(SymbolFunction(INTERNAL_ERROR), context_sap,
250                  continuable ? T : NIL);
251     } else {
252         describe_internal_error(context);
253         /* There's no good way to recover from an internal error
254          * before the Lisp error handling mechanism is set up. */
255         lose("internal error too early in init, can't recover");
256     }
257     undo_fake_foreign_function_call(context);
258     if (continuable) {
259         arch_skip_instruction(context);
260     }
261 }
262
263 /* This function handles pending interrupts.  Note that in C/kernel
264  * terms we dealt with the signal already; we just haven't decided
265  * whether to call a Lisp handler or do a GC or something like that.
266  * If it helps, you can think of pending_{signal,mask,info} as a
267  * one-element queue of signals that we have acknowledged but not
268  * processed */
269
270 void
271 interrupt_handle_pending(os_context_t *context)
272 {
273 #ifndef __i386__
274     boolean were_in_lisp = !foreign_function_call_active;
275 #endif
276
277     SetSymbolValue(INTERRUPT_PENDING, NIL);
278
279     if (maybe_gc_pending) {
280         maybe_gc_pending = 0;
281 #ifndef __i386__
282         if (were_in_lisp)
283 #endif
284         {
285             fake_foreign_function_call(context);
286         }
287         funcall0(SymbolFunction(MAYBE_GC));
288 #ifndef __i386__
289         if (were_in_lisp)
290 #endif
291         {
292             undo_fake_foreign_function_call(context);
293         }
294     }
295
296     /* FIXME: This isn't very clear. It would be good to reverse
297      * engineer it and rewrite the code more clearly, or write a clear
298      * explanation of what's going on in the comments, or both.
299      *
300      * WHN's question 1a: How come we unconditionally copy from
301      * pending_mask into the context, and then test whether
302      * pending_signal is set?
303      * 
304      * WHN's question 1b: If pending_signal wasn't set, how could
305      * pending_mask be valid?
306      * 
307      * Dan Barlow's reply (sbcl-devel 2001-03-13): And the answer is -
308      * or appears to be - because interrupt_maybe_gc set it that way
309      * (look in the #ifndef __i386__ bit). We can't GC during a
310      * pseudo-atomic, so we set maybe_gc_pending=1 and
311      * arch_set_pseudo_atomic_interrupted(..) When we come out of
312      * pseudo_atomic we're marked as interrupted, so we call
313      * interrupt_handle_pending, which does the GC using the pending
314      * context (it needs a context so that it has registers to use as
315      * GC roots) then notices there's no actual interrupt handler to
316      * call, so doesn't. That's the second question [1b] answered,
317      * anyway. Why we still need to copy the pending_mask into the
318      * context given that we're now done with the context anyway, I
319      * couldn't say. */
320 #if 0
321     memcpy(os_context_sigmask_addr(context), &pending_mask, 
322            4 /* sizeof(sigset_t) */ );
323 #endif
324     sigemptyset(&pending_mask);
325     if (pending_signal) {
326         int signal = pending_signal;
327         siginfo_t info;
328         memcpy(&info, &pending_info, sizeof(siginfo_t));
329         pending_signal = 0;
330         interrupt_handle_now(signal, &info, context);
331     }
332 }
333 \f
334 /*
335  * the two main signal handlers:
336  *   interrupt_handle_now(..)
337  *   maybe_now_maybe_later(..)
338  */
339
340 void
341 interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
342 {
343     os_context_t *context = (os_context_t*)void_context;
344 #ifndef __i386__
345     boolean were_in_lisp;
346 #endif
347     union interrupt_handler handler;
348
349     /* FIXME: The CMU CL we forked off of had this Linux-only
350      * operation here. Newer CMU CLs (e.g. 18c) have hairier
351      * Linux/i386-only logic here. SBCL seems to be more reliable
352      * without anything here. However, if we start supporting code
353      * which sets the rounding mode, then we may want to do something
354      * special to force the rounding mode back to some standard value
355      * here, so that ISRs can have a standard environment. (OTOH, if
356      * rounding modes are under user control, then perhaps we should
357      * leave this up to the user.)
358      *
359      * In the absence of a test case to show that this is really a
360      * problem, we just suppress this code completely (just like the
361      * parallel code in maybe_now_maybe_later).
362      * #ifdef __linux__
363      *    SET_FPU_CONTROL_WORD(context->__fpregs_mem.cw);
364      * #endif */
365
366     handler = interrupt_handlers[signal];
367
368     if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
369         return;
370     }
371     
372 #ifndef __i386__
373     were_in_lisp = !foreign_function_call_active;
374     if (were_in_lisp)
375 #endif
376     {
377         fake_foreign_function_call(context);
378     }
379
380 #ifdef QSHOW_SIGNALS
381     FSHOW((stderr,
382            "/entering interrupt_handle_now(%d, info, context)\n",
383            signal));
384 #endif
385
386     if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
387
388         /* This can happen if someone tries to ignore or default one
389          * of the signals we need for runtime support, and the runtime
390          * support decides to pass on it. */
391         lose("no handler for signal %d in interrupt_handle_now(..)", signal);
392
393     } else if (lowtag_of(handler.lisp) == FUN_POINTER_LOWTAG) {
394
395         /* Allocate the SAPs while the interrupts are still disabled.
396          * (FIXME: Why? This is the way it was done in CMU CL, and it
397          * even had the comment noting that this is the way it was
398          * done, but no motivation..) */
399         lispobj info_sap,context_sap = alloc_sap(context);
400         info_sap = alloc_sap(info);
401         /* Allow signals again. */
402         sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
403
404 #ifdef QSHOW_SIGNALS
405         SHOW("calling Lisp-level handler");
406 #endif
407
408         funcall3(handler.lisp,
409                  make_fixnum(signal),
410                  info_sap,
411                  context_sap);
412     } else {
413
414 #ifdef QSHOW_SIGNALS
415         SHOW("calling C-level handler");
416 #endif
417
418         /* Allow signals again. */
419         sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
420         
421         (*handler.c)(signal, info, void_context);
422     }
423
424 #ifndef __i386__
425     if (were_in_lisp)
426 #endif
427     {
428         undo_fake_foreign_function_call(context);
429     }
430
431 #ifdef QSHOW_SIGNALS
432     FSHOW((stderr,
433            "/returning from interrupt_handle_now(%d, info, context)\n",
434            signal));
435 #endif
436 }
437
438 static void
439 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
440 {
441     os_context_t *context = (os_context_t*)void_context;
442
443     /* FIXME: See Debian cmucl 2.4.17, and mail from DTC on the CMU CL
444      * mailing list 23 Oct 1999, for changes in FPU handling at
445      * interrupt time which should be ported into SBCL. Also see the
446      * analogous logic at the head of interrupt_handle_now for
447      * more related FIXME stuff. 
448      *
449      * For now, we just suppress this code completely.
450      * #ifdef __linux__
451      *    SET_FPU_CONTROL_WORD(context->__fpregs_mem.cw);
452      * #endif */
453
454     /* see comments at top of code/signal.lisp for what's going on here
455      * with INTERRUPTS_ENABLED/INTERRUPT_HANDLE_NOW 
456      */
457     if (SymbolValue(INTERRUPTS_ENABLED) == NIL) {
458
459         /* FIXME: This code is exactly the same as the code in the
460          * other leg of the if(..), and should be factored out into
461          * a shared function. */
462         pending_signal = signal;
463         memcpy(&pending_info, info, sizeof(siginfo_t));
464         memcpy(&pending_mask,
465                os_context_sigmask_addr(context),
466                sizeof(sigset_t));
467         sigaddset_blockable(os_context_sigmask_addr(context));
468         SetSymbolValue(INTERRUPT_PENDING, T);
469
470     } else if (
471 #ifndef __i386__
472                (!foreign_function_call_active) &&
473 #endif
474                arch_pseudo_atomic_atomic(context)) {
475
476         /* FIXME: It would probably be good to replace these bare
477          * memcpy(..) calls with calls to cpy_siginfo_t and
478          * cpy_sigset_t, so that we only have to get the sizeof
479          * expressions right in one place, and after that static type
480          * checking takes over. */
481         pending_signal = signal;
482         memcpy(&pending_info, info, sizeof(siginfo_t));
483         memcpy(&pending_mask,
484                os_context_sigmask_addr(context),
485                sizeof(sigset_t));
486         sigaddset_blockable(os_context_sigmask_addr(context));
487
488         arch_set_pseudo_atomic_interrupted(context);
489
490     } else {
491         interrupt_handle_now(signal, info, context);
492     }
493 }
494 \f
495 /*
496  * stuff to detect and handle hitting the GC trigger
497  */
498
499 #ifndef INTERNAL_GC_TRIGGER
500 static boolean
501 gc_trigger_hit(int signal, siginfo_t *info, os_context_t *context)
502 {
503     if (current_auto_gc_trigger == NULL)
504         return 0;
505     else{
506         lispobj *badaddr=(lispobj *)arch_get_bad_addr(signal,
507                                                       info,
508                                                       context);
509
510         return (badaddr >= current_auto_gc_trigger &&
511                 badaddr < current_dynamic_space + DYNAMIC_SPACE_SIZE);
512     }
513 }
514 #endif
515
516 #ifndef __i386__
517 /* This function gets called from the SIGSEGV (for e.g. Linux or
518  * OpenBSD) or SIGBUS (for e.g. FreeBSD) handler. Here we check
519  * whether the signal was due to treading on the mprotect()ed zone -
520  * and if so, arrange for a GC to happen. */
521 boolean
522 interrupt_maybe_gc(int signal, siginfo_t *info, void *void_context)
523 {
524     os_context_t *context=(os_context_t *) void_context;
525
526     if (!foreign_function_call_active
527 #ifndef INTERNAL_GC_TRIGGER
528         && gc_trigger_hit(signal, info, context)
529 #endif
530         ) {
531 #ifndef INTERNAL_GC_TRIGGER
532         clear_auto_gc_trigger();
533 #endif
534
535         if (arch_pseudo_atomic_atomic(context)) {
536             /* don't GC during an atomic operation.  Instead, copy the 
537              * signal mask somewhere safe.  interrupt_handle_pending
538              * will detect pending_signal==0 and know to do a GC with the
539              * signal context instead of calling a Lisp-level handler */
540             maybe_gc_pending = 1;
541             if (pending_signal == 0) {
542                 /* FIXME: This copy-pending_mask-then-sigaddset_blockable
543                  * idiom occurs over and over. It should be factored out
544                  * into a function with a descriptive name. */
545                 memcpy(&pending_mask,
546                        os_context_sigmask_addr(context),
547                        sizeof(sigset_t));
548                 sigaddset_blockable(os_context_sigmask_addr(context));
549             }
550             arch_set_pseudo_atomic_interrupted(context);
551         }
552         else {
553             fake_foreign_function_call(context);
554             funcall0(SymbolFunction(MAYBE_GC));
555             undo_fake_foreign_function_call(context);
556         }
557
558         return 1;
559     } else {
560         return 0;
561     }
562 }
563 #endif
564 \f
565 /*
566  * noise to install handlers
567  */
568
569 /*
570  * what low-level signal handlers looked like before
571  * undoably_install_low_level_interrupt_handler() got involved
572  */
573 struct low_level_signal_handler_state {
574     int was_modified;
575     void (*handler)(int, siginfo_t*, void*);
576 } old_low_level_signal_handler_states[NSIG];
577
578 void
579 uninstall_low_level_interrupt_handlers_atexit(void)
580 {
581     int signal;
582     for (signal = 0; signal < NSIG; ++signal) {
583         struct low_level_signal_handler_state
584             *old_low_level_signal_handler_state =
585             old_low_level_signal_handler_states + signal;
586         if (old_low_level_signal_handler_state->was_modified) {
587             struct sigaction sa;
588             sa.sa_sigaction = old_low_level_signal_handler_state->handler;
589             sigemptyset(&sa.sa_mask);
590             sa.sa_flags = SA_SIGINFO | SA_RESTART; 
591             sigaction(signal, &sa, NULL);
592         }
593     }
594 }
595
596 /* Undoably install a special low-level handler for signal; or if
597  * handler is SIG_DFL, remove any special handling for signal.
598  *
599  * The "undoably" aspect is because we also arrange with atexit() for
600  * the handler to be restored to its old value. This is for tidiness:
601  * it shouldn't matter much ordinarily, but it does remove a window
602  * where e.g. memory fault signals (SIGSEGV or SIGBUS, which in
603  * ordinary operation of SBCL are sent to the generational garbage
604  * collector, then possibly onward to Lisp code) or SIGINT (which is
605  * ordinarily passed to Lisp code) could otherwise be handled
606  * bizarrely/brokenly because the Lisp code would try to deal with
607  * them using machinery (like stream output buffers) which has already
608  * been dismantled. */
609 void
610 undoably_install_low_level_interrupt_handler (int signal,
611                                               void handler(int,
612                                                            siginfo_t*,
613                                                            void*))
614 {
615     struct sigaction sa;
616     struct low_level_signal_handler_state *old_low_level_signal_handler_state =
617         old_low_level_signal_handler_states + signal;
618
619     if (0 > signal || signal >= NSIG) {
620         lose("bad signal number %d", signal);
621     }
622
623     sa.sa_sigaction = handler;
624     sigemptyset(&sa.sa_mask);
625     sigaddset_blockable(&sa.sa_mask);
626     sa.sa_flags = SA_SIGINFO | SA_RESTART;
627
628     /* In the case of interrupt handlers which are modified more than
629      * once, we only save the original unmodified copy. */
630     if (!old_low_level_signal_handler_state->was_modified) {
631         struct sigaction *old_handler =
632             (struct sigaction*) &old_low_level_signal_handler_state->handler;
633         old_low_level_signal_handler_state->was_modified = 1;
634         sigaction(signal, &sa, old_handler);
635     } else {
636         sigaction(signal, &sa, NULL);
637     }
638
639     interrupt_low_level_handlers[signal] =
640         (ARE_SAME_HANDLER(handler, SIG_DFL) ? 0 : handler);
641 }
642
643 /* This is called from Lisp. */
644 unsigned long
645 install_handler(int signal, void handler(int, siginfo_t*, void*))
646 {
647     struct sigaction sa;
648     sigset_t old, new;
649     union interrupt_handler oldhandler;
650
651     FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
652
653     sigemptyset(&new);
654     sigaddset(&new, signal);
655     sigprocmask(SIG_BLOCK, &new, &old);
656
657     sigemptyset(&new);
658     sigaddset_blockable(&new);
659
660     FSHOW((stderr, "/interrupt_low_level_handlers[signal]=%d\n",
661            interrupt_low_level_handlers[signal]));
662     if (interrupt_low_level_handlers[signal]==0) {
663         if (ARE_SAME_HANDLER(handler, SIG_DFL) ||
664             ARE_SAME_HANDLER(handler, SIG_IGN)) {
665             sa.sa_sigaction = handler;
666         } else if (sigismember(&new, signal)) {
667             sa.sa_sigaction = maybe_now_maybe_later;
668         } else {
669             sa.sa_sigaction = interrupt_handle_now;
670         }
671
672         sigemptyset(&sa.sa_mask);
673         sigaddset_blockable(&sa.sa_mask);
674         sa.sa_flags = SA_SIGINFO | SA_RESTART;
675
676         sigaction(signal, &sa, NULL);
677     }
678
679     oldhandler = interrupt_handlers[signal];
680     interrupt_handlers[signal].c = handler;
681
682     sigprocmask(SIG_SETMASK, &old, 0);
683
684     FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
685
686     return (unsigned long)oldhandler.lisp;
687 }
688
689 void
690 interrupt_init(void)
691 {
692     int i;
693
694     SHOW("entering interrupt_init()");
695
696     /* Set up for recovery from any installed low-level handlers. */
697     atexit(&uninstall_low_level_interrupt_handlers_atexit);
698
699     /* Set up high level handler information. */
700     for (i = 0; i < NSIG; i++) {
701         interrupt_handlers[i].c =
702             /* (The cast here blasts away the distinction between
703              * SA_SIGACTION-style three-argument handlers and
704              * signal(..)-style one-argument handlers, which is OK
705              * because it works to call the 1-argument form where the
706              * 3-argument form is expected.) */
707             (void (*)(int, siginfo_t*, void*))SIG_DFL;
708     }
709
710     SHOW("returning from interrupt_init()");
711 }