stk/Src/signal.c

406 lines
9.7 KiB
C
Raw Normal View History

1996-09-27 06:29:02 -04:00
/*
*
* s i g n a l . c -- Signal handling
*
1999-09-05 07:16:41 -04:00
* Copyright <EFBFBD> 1993-1999 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
1996-09-27 06:29:02 -04:00
*
*
1999-09-05 07:16:41 -04:00
* Permission to use, copy, modify, distribute,and license this
* software and its documentation for any purpose is hereby granted,
* provided that existing copyright notices are retained in all
* copies and that this notice is included verbatim in any
* distributions. No written agreement, license, or royalty fee is
* required for any of the authorized uses.
* This software is provided ``AS IS'' without express or implied
* warranty.
1996-09-27 06:29:02 -04:00
*
* Author: Erick Gallesio [eg@unice.fr]
* Creation date: 10-Oct-1995 07:55
1999-09-05 07:16:41 -04:00
* Last file update: 3-Sep-1999 20:22 (eg)
1996-09-27 06:29:02 -04:00
*
*/
#include "stk.h"
#include <signal.h>
#ifdef HAVE_SIGACTION
1998-04-10 06:59:06 -04:00
static sigset_t global_sigset;
1996-09-27 06:29:02 -04:00
# ifdef SA_RESTART
# define SA_FLAGS SA_RESTART
# else
# define SA_FLAGS 0
# endif
#endif
1999-02-02 06:13:40 -05:00
int STk_control_C = 0;
1996-09-27 06:29:02 -04:00
static SCM signals[MAX_SIGNAL];
/* Convention: signals[i] can be
* - #f if signal is ignored
* - #t if signal is set to default
* - a list of handlers
*/
1998-04-10 06:59:06 -04:00
static void execute_signal_handlers(int sig, SCM handlers)
{
SCM arg = LIST1(STk_makeinteger(sig));
for ( ; CONSP(handlers); handlers = CDR(handlers)) {
1999-09-05 07:16:41 -04:00
if (Apply(CAR(handlers), arg) == Sym_break) break;
1998-04-10 06:59:06 -04:00
}
}
#ifdef SIGSEGV
static void handle_sigsegv_signal(void)
{
SCM l = signals[SIGSEGV];
if (l == NIL) {
/* User has not redefined SIGSEGV action */
#ifdef HAVE_SIGACTION
/* See comment in procedure above */
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGSEGV);
sigprocmask(SIG_UNBLOCK, &set, NULL);
#endif
/* SEGV occurs generally when there is an error in the
* interpreter. We cannot do a lot of things here, excepted
* signalling this fact and go back to toplevel
*/
Err("Segmentation violation: Returning to toplevel", NIL);
1996-09-27 06:29:02 -04:00
}
1998-04-10 06:59:06 -04:00
else
execute_signal_handlers(SIGSEGV, l);
1996-09-27 06:29:02 -04:00
}
1998-04-10 06:59:06 -04:00
#endif
1996-09-27 06:29:02 -04:00
1999-02-02 06:13:40 -05:00
void STk_handle_sigint_signal(void)
{
SCM l = signals[SIGINT];
if (l == NIL) {
/* User has not redefined ^C action */
#ifdef HAVE_SIGACTION
/* Empty the signal mask before calling a longjmp and don't let the
* system (at least Linux) a chance to restore the mask
*/
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigprocmask(SIG_UNBLOCK, &set, NULL);
#endif
longjmp(STk_err_handler->j, JMP_INTERRUPT);
}
else
execute_signal_handlers(SIGINT, l);
}
/*===========================================================================*\
*
* STk_handle_signal
*
\*===========================================================================*/
1996-09-27 06:29:02 -04:00
void STk_handle_signal(int sig)
{
#ifndef HAVE_SIGACTION
if (sig < MAX_SYSTEM_SIG)
signal(sig, STk_handle_signal);
#endif
if (sig == SIGINT) { /* SIGINT */
1999-02-02 06:13:40 -05:00
Puts("*** Interrupt ***\n", STk_curr_eport); Flush(STk_curr_eport);
STk_control_C = 1; STk_eval_flag = 1;
1996-09-27 06:29:02 -04:00
}
1998-04-10 06:59:06 -04:00
#ifdef SIGSEGV
else if (sig == SIGSEGV) { /* SIGSEGV */
1999-02-02 06:13:40 -05:00
handle_sigsegv_signal();
1996-09-27 06:29:02 -04:00
}
1998-04-10 06:59:06 -04:00
#endif
else
execute_signal_handlers(sig, signals[sig]);
1996-09-27 06:29:02 -04:00
}
1998-04-10 06:59:06 -04:00
1996-09-27 06:29:02 -04:00
static SCM set_handler(long s, SCM proc)
{
void (*handler)();
if (BOOLEANP(proc))
/* Special case #f means ignore and #t means default */
handler = (proc == Truth) ? SIG_DFL: SIG_IGN;
else
/* Use our handler */
handler = STk_handle_signal;
1998-04-10 06:59:06 -04:00
if (s < MAX_SYSTEM_SIG)
1996-09-27 06:29:02 -04:00
#ifdef HAVE_SIGACTION
1998-04-10 06:59:06 -04:00
{
struct sigaction sigact;
sigfillset(&(sigact.sa_mask));
/* FIXME: => handler non interruptible mais est-ce un probl<62>me en pratique? */
/* sigdelset(&(sigact.sa_mask), SIGINT); */
sigact.sa_handler = handler;
sigact.sa_flags = SA_FLAGS;
sigaction(s, &sigact, NULL);
}
1996-09-27 06:29:02 -04:00
#else
1998-04-10 06:59:06 -04:00
signal(s, handler);
1996-09-27 06:29:02 -04:00
#endif
return signals[s] = proc;
}
PRIMITIVE STk_set_signal_handler(SCM sig, SCM proc)
{
long s = STk_integer_value(sig);
1999-02-02 06:13:40 -05:00
ENTER_PRIMITIVE("set-signal-handler!");
if (s == LONG_MIN || s < 0 || s >= MAX_SIGNAL) Serror("bad signal number", sig);
1996-09-27 06:29:02 -04:00
if (BOOLEANP(proc))
return set_handler(s, proc);
else {
1999-02-02 06:13:40 -05:00
if (STk_procedurep(proc) == Ntruth) Serror("bad procedure", proc);
1996-09-27 06:29:02 -04:00
return set_handler(s, LIST1(proc));
}
}
PRIMITIVE STk_add_signal_handler(SCM sig, SCM proc)
{
long s = STk_integer_value(sig);
1999-02-02 06:13:40 -05:00
ENTER_PRIMITIVE("add-signal-handler!");
if (s == LONG_MIN || s < 0 || s >= MAX_SIGNAL) Serror("bad signal number", sig);
1996-09-27 06:29:02 -04:00
1999-02-02 06:13:40 -05:00
if (STk_procedurep(proc) == Ntruth) Serror("bad procedure", proc);
1996-09-27 06:29:02 -04:00
if (BOOLEANP(signals[s]))
/* We add a handler on a defaulted or ignored signal */
return set_handler(s, LIST1(proc));
else
/* Just add the new signal in front of the list */
return signals[s] = Cons(proc,signals[s]);
}
PRIMITIVE STk_get_signal_handlers(SCM sig)
{
if (sig == UNBOUND) { /* Return all handlers */
SCM z;
z = STk_makevect(MAX_SIGNAL, NULL);
memcpy(VECT(z), signals, MAX_SIGNAL * sizeof(SCM));
return z;
}
else { /* Return handlers of sig only */
long s = STk_integer_value(sig);
if (s == LONG_MIN || s < 0 || s >= MAX_SIGNAL)
STk_err("get-signal-handlers: bad signal number", sig);
return signals[s];
}
}
1999-02-02 06:13:40 -05:00
PRIMITIVE STk_send_signal(SCM sig)
{
long s = STk_integer_value(sig);
ENTER_PRIMITIVE("send-signal");
if (s < 0 || s >= MAX_SIGNAL) Serror("bad signal number", sig);
STk_handle_signal(s);
return UNDEFINED;
}
1996-09-27 06:29:02 -04:00
void STk_mark_signal_table(void)
{
int i;
for (i = 0; i < MAX_SIGNAL; i++)
STk_gc_mark(signals[i]);
}
1998-04-10 06:59:06 -04:00
void STk_ignore_signals(void) /* Block all signals */
{
#ifdef HAVE_SIGACTION
sigset_t new;
sigfillset(&new);
sigprocmask(SIG_BLOCK, &new, &global_sigset);
#endif
}
void STk_allow_signals(void) /* Restore signals as before ignore_signals */
{
#ifdef HAVE_SIGACTION
sigprocmask(SIG_SETMASK, &global_sigset, NULL);
#endif
}
void STk_signal_GC(void)
{
SCM old = signals[SIGHADGC];
STk_ignore_signals();
/* Ignore this signal while executing the handler */
signals[SIGHADGC] = Ntruth;
execute_signal_handlers(SIGHADGC, old);
signals[SIGHADGC] = old;
STk_allow_signals();
}
/*******************************************************************************/
1996-09-27 06:29:02 -04:00
void STk_init_signal(void)
{
int i;
for (i = 0; i < MAX_SIGNAL; i++) {
set_handler(i, (
#ifdef SIGTSTP
1999-02-02 06:13:40 -05:00
(i==SIGTSTP) || /* ^Z is set to SIG_DFLT to allow program supension */
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGABRT
(i== SIGABRT) || /* Really abort when receiving ABORT (loops otherwise)*/
#endif
#ifdef SIGQUIT
(i == SIGQUIT) || /* ditto for QUIT */
#endif
0) ? Truth : NIL);
}
/*
* POSIX.1 signals
*/
#ifdef SIGABRT
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGABRT", STk_makeinteger(SIGABRT));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGALRM
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGALRM", STk_makeinteger(SIGALRM));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGFPE
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGFPE", STk_makeinteger(SIGFPE));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGHUP
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGHUP", STk_makeinteger(SIGHUP));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGILL
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGILL", STk_makeinteger(SIGILL));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGINT
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGINT", STk_makeinteger(SIGINT));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGKILL
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGKILL", STk_makeinteger(SIGKILL));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGPIPE
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGPIPE", STk_makeinteger(SIGPIPE));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGQUIT
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGQUIT", STk_makeinteger(SIGQUIT));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGSEGV
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGSEGV", STk_makeinteger(SIGSEGV));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGTERM
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGTERM", STk_makeinteger(SIGTERM));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGUSR1
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGUSR1", STk_makeinteger(SIGUSR1));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGUSR2
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGUSR2", STk_makeinteger(SIGUSR2));
1996-09-27 06:29:02 -04:00
#endif
/*
* Following signals exist only on system which support Job Control
*/
#ifdef SIGCHLD
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGCHLD", STk_makeinteger(SIGCHLD));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGCONT
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGCONT", STk_makeinteger(SIGCONT));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGSTOP
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGSTOP", STk_makeinteger(SIGSTOP));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGTSTP
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGTSTP", STk_makeinteger(SIGTSTP));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGTTIN
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGTTIN", STk_makeinteger(SIGTTIN));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGTTOU
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGTTOU", STk_makeinteger(SIGTTOU));
1996-09-27 06:29:02 -04:00
#endif
/*
1998-04-10 06:59:06 -04:00
* Non POSIX signals stolen on Sun and Linux
1996-09-27 06:29:02 -04:00
*/
#ifdef SIGTRAP
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGTRAP", STk_makeinteger(SIGTRAP));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGIOT
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGIOT", STk_makeinteger(SIGIOT));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGEMT
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGEMT", STk_makeinteger(SIGEMT));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGBUS
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGBUS", STk_makeinteger(SIGBUS));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGSYS
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGSYS", STk_makeinteger(SIGSYS));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGURG
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGURG", STk_makeinteger(SIGURG));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGCLD
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGCLD", STk_makeinteger(SIGCLD));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGIO
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGIO", STk_makeinteger(SIGIO));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGPOLL
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGPOLL", STk_makeinteger(SIGPOLL));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGXCPU
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGXCPU", STk_makeinteger(SIGXCPU));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGXFSZ
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGXFSZ", STk_makeinteger(SIGXFSZ));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGVTALRM
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGVTALRM", STk_makeinteger(SIGVTALRM));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGPROF
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGPROF", STk_makeinteger(SIGPROF));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGWINCH
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGWINCH", STk_makeinteger(SIGWINCH));
1996-09-27 06:29:02 -04:00
#endif
#ifdef SIGLOST
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGLOST", STk_makeinteger(SIGLOST));
1996-09-27 06:29:02 -04:00
#endif
/* Add GC signal */
1998-04-10 06:59:06 -04:00
STk_define_scheme_variable("SIGHADGC", STk_makeinteger(SIGHADGC));
1996-09-27 06:29:02 -04:00
}