1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* Automatique. Distributed only by permission. */
|
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
#include <signal.h>
|
|
|
|
#include "alloc.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "fail.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "roots.h"
|
|
|
|
#include "signals.h"
|
|
|
|
|
1996-03-18 04:07:43 -08:00
|
|
|
Volatile int async_signal_mode = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
Volatile int pending_signal = 0;
|
1995-12-22 08:48:17 -08:00
|
|
|
Volatile int something_to_do = 0;
|
|
|
|
Volatile int force_major_slice = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
value signal_handlers = 0;
|
1996-04-01 07:24:38 -08:00
|
|
|
void (*enter_blocking_section_hook)() = NULL;
|
|
|
|
void (*leave_blocking_section_hook)() = NULL;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
static void execute_signal(signal_number)
|
|
|
|
int signal_number;
|
|
|
|
{
|
|
|
|
Assert (!async_signal_mode);
|
|
|
|
callback(Field(signal_handlers, signal_number), Val_int(signal_number));
|
|
|
|
}
|
|
|
|
|
|
|
|
void handle_signal(signal_number)
|
|
|
|
int signal_number;
|
|
|
|
{
|
1995-08-10 01:21:42 -07:00
|
|
|
#ifndef POSIX_SIGNALS
|
1995-05-04 03:15:53 -07:00
|
|
|
#ifndef BSD_SIGNALS
|
|
|
|
signal(signal_number, handle_signal);
|
1995-08-10 01:21:42 -07:00
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
if (async_signal_mode){
|
|
|
|
leave_blocking_section ();
|
|
|
|
execute_signal(signal_number);
|
|
|
|
enter_blocking_section ();
|
|
|
|
}else{
|
|
|
|
pending_signal = signal_number;
|
|
|
|
something_to_do = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-12-22 08:48:17 -08:00
|
|
|
void urge_major_slice ()
|
|
|
|
{
|
|
|
|
force_major_slice = 1;
|
|
|
|
something_to_do = 1;
|
|
|
|
}
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
void enter_blocking_section()
|
|
|
|
{
|
|
|
|
int temp;
|
|
|
|
|
|
|
|
while (1){
|
|
|
|
Assert (!async_signal_mode);
|
|
|
|
/* If a signal arrives between the next two instructions,
|
|
|
|
it will be lost. */
|
|
|
|
temp = pending_signal; pending_signal = 0;
|
|
|
|
if (temp) execute_signal(temp);
|
|
|
|
async_signal_mode = 1;
|
|
|
|
if (!pending_signal) break;
|
|
|
|
async_signal_mode = 0;
|
|
|
|
}
|
1996-04-01 07:24:38 -08:00
|
|
|
if (enter_blocking_section_hook != NULL) enter_blocking_section_hook();
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function may be called from outside a blocking section. */
|
|
|
|
void leave_blocking_section()
|
|
|
|
{
|
1996-04-01 07:24:38 -08:00
|
|
|
if (leave_blocking_section_hook != NULL) leave_blocking_section_hook();
|
1995-05-04 03:15:53 -07:00
|
|
|
async_signal_mode = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SIGABRT
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGABRT -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGALRM
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGALRM -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGFPE
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGFPE -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGHUP
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGHUP -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGILL
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGILL -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGINT
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGINT -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGKILL
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGKILL -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGPIPE
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGPIPE -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGQUIT
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGQUIT -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGSEGV
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGSEGV -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGTERM
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGTERM -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGUSR1
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGUSR1 -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGUSR2
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGUSR2 -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGCHLD
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGCHLD -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGCONT
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGCONT -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGSTOP
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGSTOP -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGTSTP
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGTSTP -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGTTIN
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGTTIN -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SIGTTOU
|
1995-08-10 01:21:42 -07:00
|
|
|
#define SIGTTOU -1
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
1995-10-30 02:21:28 -08:00
|
|
|
#ifndef SIGVTALRM
|
|
|
|
#define SIGVTALRM -1
|
|
|
|
#endif
|
1996-04-18 09:29:57 -07:00
|
|
|
#ifndef SIGPROF
|
|
|
|
#define SIGPROF -1
|
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-05-09 02:34:59 -07:00
|
|
|
int posix_signals[] = {
|
1995-05-04 03:15:53 -07:00
|
|
|
SIGABRT, SIGALRM, SIGFPE, SIGHUP, SIGILL, SIGINT, SIGKILL, SIGPIPE,
|
|
|
|
SIGQUIT, SIGSEGV, SIGTERM, SIGUSR1, SIGUSR2, SIGCHLD, SIGCONT,
|
1996-04-18 09:29:57 -07:00
|
|
|
SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGVTALRM, SIGPROF
|
1995-05-04 03:15:53 -07:00
|
|
|
};
|
|
|
|
|
1995-08-10 01:21:42 -07:00
|
|
|
#ifndef NSIG
|
|
|
|
#define NSIG 32
|
|
|
|
#endif
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
value install_signal_handler(signal_number, action) /* ML */
|
|
|
|
value signal_number, action;
|
|
|
|
{
|
1995-08-10 01:21:42 -07:00
|
|
|
int sig;
|
|
|
|
void (*act)();
|
|
|
|
#ifdef POSIX_SIGNALS
|
|
|
|
struct sigaction sigact;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sig = Int_val(signal_number);
|
|
|
|
if (sig < 0) sig = posix_signals[-sig-1];
|
|
|
|
if (sig < 0 || sig >= NSIG)
|
|
|
|
invalid_argument("Sys.signal: unavailable signal");
|
|
|
|
switch(action) {
|
|
|
|
case Val_int(0): /* Signal_default */
|
|
|
|
act = SIG_DFL;
|
1995-05-04 03:15:53 -07:00
|
|
|
break;
|
1995-08-10 01:21:42 -07:00
|
|
|
case Val_int(1): /* Signal_ignore */
|
|
|
|
act = SIG_IGN;
|
1995-05-04 03:15:53 -07:00
|
|
|
break;
|
1995-08-10 01:21:42 -07:00
|
|
|
default: /* Signal_handle */
|
1995-05-04 03:15:53 -07:00
|
|
|
if (signal_handlers == 0) {
|
|
|
|
int i;
|
|
|
|
Push_roots(r, 1);
|
|
|
|
r[0] = action;
|
1995-08-10 01:21:42 -07:00
|
|
|
signal_handlers = alloc_tuple(NSIG);
|
1995-05-04 03:15:53 -07:00
|
|
|
action = r[0];
|
|
|
|
Pop_roots();
|
1995-08-10 01:21:42 -07:00
|
|
|
for (i = 0; i < NSIG; i++) Field(signal_handlers, i) = Val_int(0);
|
1995-05-04 03:15:53 -07:00
|
|
|
register_global_root(&signal_handlers);
|
|
|
|
}
|
|
|
|
modify(&Field(signal_handlers, sig), Field(action, 0));
|
1995-08-10 01:21:42 -07:00
|
|
|
act = handle_signal;
|
1995-05-04 03:15:53 -07:00
|
|
|
break;
|
|
|
|
}
|
1995-08-10 01:21:42 -07:00
|
|
|
#ifdef POSIX_SIGNALS
|
|
|
|
sigact.sa_handler = act;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigact.sa_flags = 0;
|
|
|
|
sigaction(sig, &sigact, NULL);
|
|
|
|
#else
|
|
|
|
signal(sig, act);
|
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
return Val_unit;
|
|
|
|
}
|