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 */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
#include <signal.h>
|
|
|
|
#include "alloc.h"
|
1996-11-08 06:46:01 -08:00
|
|
|
#include "callback.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "config.h"
|
|
|
|
#include "fail.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "roots.h"
|
|
|
|
#include "signals.h"
|
2005-07-29 05:11:01 -07:00
|
|
|
#include "signals_machdep.h"
|
1998-08-08 09:52:33 -07:00
|
|
|
#include "sys.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2005-07-29 05:11:01 -07:00
|
|
|
#ifndef NSIG
|
|
|
|
#define NSIG 64
|
|
|
|
#endif
|
|
|
|
|
2001-11-05 08:10:12 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
typedef void (*sighandler)(int sig);
|
2004-01-01 08:42:43 -08:00
|
|
|
extern sighandler caml_win32_signal(int sig, sighandler action);
|
|
|
|
#define signal(sig,act) caml_win32_signal(sig,act)
|
2001-11-05 08:10:12 -08:00
|
|
|
#endif
|
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
CAMLexport intnat volatile caml_pending_signals[NSIG];
|
2004-01-05 12:26:19 -08:00
|
|
|
CAMLexport int volatile caml_something_to_do = 0;
|
2004-01-01 08:42:43 -08:00
|
|
|
int volatile caml_force_major_slice = 0;
|
|
|
|
value caml_signal_handlers = 0;
|
2004-01-05 12:26:19 -08:00
|
|
|
CAMLexport void (* volatile caml_async_action_hook)(void) = NULL;
|
1999-12-23 09:33:46 -08:00
|
|
|
|
2005-07-31 05:31:03 -07:00
|
|
|
static void caml_process_pending_signals(void)
|
1999-12-23 09:33:46 -08:00
|
|
|
{
|
2005-07-31 05:31:03 -07:00
|
|
|
int signal_num;
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat signal_state;
|
2005-07-31 05:31:03 -07:00
|
|
|
|
|
|
|
for (signal_num = 0; signal_num < NSIG; signal_num++) {
|
|
|
|
Read_and_clear(signal_state, caml_pending_signals[signal_num]);
|
|
|
|
if (signal_state) caml_execute_signal(signal_num, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void caml_process_event(void)
|
|
|
|
{
|
1999-12-23 09:33:46 -08:00
|
|
|
void (*async_action)(void);
|
2005-07-29 05:11:01 -07:00
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
if (caml_force_major_slice) caml_minor_collection ();
|
2003-12-31 06:20:40 -08:00
|
|
|
/* FIXME should be [caml_check_urgent_gc] */
|
2005-07-31 05:31:03 -07:00
|
|
|
caml_process_pending_signals();
|
2005-07-29 05:11:01 -07:00
|
|
|
Read_and_clear(async_action, caml_async_action_hook);
|
1999-12-23 09:33:46 -08:00
|
|
|
if (async_action != NULL) (*async_action)();
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
static intnat volatile caml_async_signal_mode = 0;
|
2005-07-29 05:11:01 -07:00
|
|
|
|
|
|
|
static void caml_enter_blocking_section_default(void)
|
|
|
|
{
|
|
|
|
Assert (caml_async_signal_mode == 0);
|
|
|
|
caml_async_signal_mode = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void caml_leave_blocking_section_default(void)
|
|
|
|
{
|
|
|
|
Assert (caml_async_signal_mode == 1);
|
|
|
|
caml_async_signal_mode = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int caml_try_leave_blocking_section_default(void)
|
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat res;
|
2005-07-29 05:11:01 -07:00
|
|
|
Read_and_clear(res, caml_async_signal_mode);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLexport void (*caml_enter_blocking_section_hook)(void) =
|
|
|
|
caml_enter_blocking_section_default;
|
|
|
|
CAMLexport void (*caml_leave_blocking_section_hook)(void) =
|
|
|
|
caml_leave_blocking_section_default;
|
|
|
|
CAMLexport int (*caml_try_leave_blocking_section_hook)(void) =
|
|
|
|
caml_try_leave_blocking_section_default;
|
|
|
|
|
2005-04-17 01:23:51 -07:00
|
|
|
CAMLexport int caml_rev_convert_signal_number(int signo);
|
2000-11-27 08:10:49 -08:00
|
|
|
|
2005-10-12 05:33:47 -07:00
|
|
|
/* Execute a signal handler immediately */
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_execute_signal(int signal_number, int in_signal_handler)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1999-06-05 05:02:48 -07:00
|
|
|
value res;
|
|
|
|
#ifdef POSIX_SIGNALS
|
|
|
|
sigset_t sigs;
|
|
|
|
/* Block the signal before executing the handler, and record in sigs
|
|
|
|
the original signal mask */
|
|
|
|
sigemptyset(&sigs);
|
|
|
|
sigaddset(&sigs, signal_number);
|
|
|
|
sigprocmask(SIG_BLOCK, &sigs, &sigs);
|
|
|
|
#endif
|
2005-04-17 01:23:51 -07:00
|
|
|
res = caml_callback_exn(
|
|
|
|
Field(caml_signal_handlers, signal_number),
|
|
|
|
Val_int(caml_rev_convert_signal_number(signal_number)));
|
1999-06-05 05:02:48 -07:00
|
|
|
#ifdef POSIX_SIGNALS
|
|
|
|
if (! in_signal_handler) {
|
|
|
|
/* Restore the original signal mask */
|
|
|
|
sigprocmask(SIG_SETMASK, &sigs, NULL);
|
|
|
|
} else if (Is_exception_result(res)) {
|
|
|
|
/* Restore the original signal mask and unblock the signal itself */
|
|
|
|
sigdelset(&sigs, signal_number);
|
|
|
|
sigprocmask(SIG_SETMASK, &sigs, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
2004-01-01 08:42:43 -08:00
|
|
|
if (Is_exception_result(res)) caml_raise(Extract_exception(res));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2005-10-12 05:33:47 -07:00
|
|
|
/* Record the delivery of a signal, and arrange so that caml_process_event
|
|
|
|
is called as soon as possible. */
|
|
|
|
|
|
|
|
void caml_record_signal(int signal_number)
|
|
|
|
{
|
|
|
|
caml_pending_signals[signal_number] = 1;
|
|
|
|
caml_something_to_do = 1;
|
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
static void handle_signal(int signal_number)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1999-06-05 05:02:48 -07:00
|
|
|
#if !defined(POSIX_SIGNALS) && !defined(BSD_SIGNALS)
|
1995-05-04 03:15:53 -07:00
|
|
|
signal(signal_number, handle_signal);
|
|
|
|
#endif
|
2005-07-29 05:11:01 -07:00
|
|
|
if (signal_number < 0 || signal_number >= NSIG) return;
|
|
|
|
if (caml_try_leave_blocking_section_hook()) {
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_execute_signal(signal_number, 1);
|
2005-07-29 05:11:01 -07:00
|
|
|
caml_enter_blocking_section_hook();
|
1995-05-04 03:15:53 -07:00
|
|
|
}else{
|
2005-10-12 05:33:47 -07:00
|
|
|
caml_record_signal(signal_number);
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_urge_major_slice (void)
|
1995-12-22 08:48:17 -08:00
|
|
|
{
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_force_major_slice = 1;
|
|
|
|
caml_something_to_do = 1;
|
1995-12-22 08:48:17 -08:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_enter_blocking_section(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2005-07-31 05:31:03 -07:00
|
|
|
int i;
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat pending;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
while (1){
|
2005-07-29 05:11:01 -07:00
|
|
|
/* Process all pending signals now */
|
2005-07-31 05:31:03 -07:00
|
|
|
caml_process_pending_signals();
|
2005-07-29 05:11:01 -07:00
|
|
|
caml_enter_blocking_section_hook ();
|
|
|
|
/* Check again for pending signals. */
|
|
|
|
pending = 0;
|
2005-07-31 05:31:03 -07:00
|
|
|
for (i = 0; i < NSIG; i++) pending |= caml_pending_signals[i];
|
2005-07-29 05:11:01 -07:00
|
|
|
/* If none, done; otherwise, try again */
|
|
|
|
if (!pending) break;
|
|
|
|
caml_leave_blocking_section_hook ();
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_leave_blocking_section(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2005-07-29 05:11:01 -07:00
|
|
|
caml_leave_blocking_section_hook ();
|
2005-07-31 05:31:03 -07:00
|
|
|
caml_process_pending_signals();
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
1998-08-13 08:58:08 -07:00
|
|
|
static 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
|
|
|
};
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport int caml_convert_signal_number(int signo)
|
1998-08-13 08:58:08 -07:00
|
|
|
{
|
|
|
|
if (signo < 0 && signo >= -(sizeof(posix_signals) / sizeof(int)))
|
|
|
|
return posix_signals[-signo-1];
|
|
|
|
else
|
|
|
|
return signo;
|
|
|
|
}
|
|
|
|
|
2005-04-17 01:23:51 -07:00
|
|
|
CAMLexport int caml_rev_convert_signal_number(int signo)
|
2000-11-27 08:10:49 -08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < sizeof(posix_signals) / sizeof(int); i++)
|
|
|
|
if (signo == posix_signals[i]) return -i - 1;
|
|
|
|
return signo;
|
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLprim value caml_install_signal_handler(value signal_number, value action)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam2 (signal_number, action);
|
1995-08-10 01:21:42 -07:00
|
|
|
int sig;
|
1998-08-08 09:52:33 -07:00
|
|
|
void (*act)(int signo), (*oldact)(int signo);
|
1995-08-10 01:21:42 -07:00
|
|
|
#ifdef POSIX_SIGNALS
|
1998-08-08 09:52:33 -07:00
|
|
|
struct sigaction sigact, oldsigact;
|
1995-08-10 01:21:42 -07:00
|
|
|
#endif
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLlocal1 (res);
|
1995-08-10 01:21:42 -07:00
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
sig = caml_convert_signal_number(Int_val(signal_number));
|
1995-08-10 01:21:42 -07:00
|
|
|
if (sig < 0 || sig >= NSIG)
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_invalid_argument("Sys.signal: unavailable signal");
|
1995-08-10 01:21:42 -07:00
|
|
|
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 */
|
|
|
|
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;
|
2003-12-15 08:29:53 -08:00
|
|
|
if (sigaction(sig, &sigact, &oldsigact) == -1) caml_sys_error(NO_ARG);
|
1998-08-08 09:52:33 -07:00
|
|
|
oldact = oldsigact.sa_handler;
|
1995-08-10 01:21:42 -07:00
|
|
|
#else
|
1998-08-08 09:52:33 -07:00
|
|
|
oldact = signal(sig, act);
|
2003-12-15 08:29:53 -08:00
|
|
|
if (oldact == SIG_ERR) caml_sys_error(NO_ARG);
|
1995-08-10 01:21:42 -07:00
|
|
|
#endif
|
1998-08-08 09:52:33 -07:00
|
|
|
if (oldact == handle_signal) {
|
2003-12-29 14:15:02 -08:00
|
|
|
res = caml_alloc_small (1, 0); /* Signal_handle */
|
2004-01-01 08:42:43 -08:00
|
|
|
Field(res, 0) = Field(caml_signal_handlers, sig);
|
1998-08-08 09:52:33 -07:00
|
|
|
}
|
|
|
|
else if (oldact == SIG_IGN)
|
|
|
|
res = Val_int(1); /* Signal_ignore */
|
|
|
|
else
|
|
|
|
res = Val_int(0); /* Signal_default */
|
2000-11-27 08:10:49 -08:00
|
|
|
if (Is_block(action)) {
|
2004-01-01 08:42:43 -08:00
|
|
|
if (caml_signal_handlers == 0) {
|
|
|
|
caml_signal_handlers = caml_alloc(NSIG, 0);
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_register_global_root(&caml_signal_handlers);
|
2000-11-27 08:10:49 -08:00
|
|
|
}
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_modify(&Field(caml_signal_handlers, sig), Field(action, 0));
|
2000-11-27 08:10:49 -08:00
|
|
|
}
|
2005-07-31 05:31:03 -07:00
|
|
|
caml_process_pending_signals();
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLreturn (res);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|