1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
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
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
/* Signal handling, code common to the bytecode and native systems */
|
|
|
|
|
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
|
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
/* The set of pending signals (received but not yet processed) */
|
2001-11-05 08:10:12 -08:00
|
|
|
|
2006-04-16 16:28:22 -07:00
|
|
|
CAMLexport intnat volatile caml_signals_are_pending = 0;
|
2005-09-22 07:21:50 -07:00
|
|
|
CAMLexport intnat volatile caml_pending_signals[NSIG];
|
1999-12-23 09:33:46 -08:00
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
/* Execute all pending signals */
|
|
|
|
|
|
|
|
void caml_process_pending_signals(void)
|
1999-12-23 09:33:46 -08:00
|
|
|
{
|
2006-04-16 16:28:22 -07:00
|
|
|
int i;
|
2005-07-31 05:31:03 -07:00
|
|
|
|
2006-04-16 16:28:22 -07:00
|
|
|
if (caml_signals_are_pending) {
|
|
|
|
caml_signals_are_pending = 0;
|
|
|
|
for (i = 0; i < NSIG; i++) {
|
|
|
|
if (caml_pending_signals[i]) {
|
|
|
|
caml_pending_signals[i] = 0;
|
|
|
|
caml_execute_signal(i, 0);
|
|
|
|
}
|
|
|
|
}
|
2005-07-31 05:31:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
/* Record the delivery of a signal, and arrange for it to be processed
|
|
|
|
as soon as possible:
|
|
|
|
- in bytecode: via caml_something_to_do, processed in caml_process_event
|
|
|
|
- in native-code: by playing with the allocation limit, processed
|
|
|
|
in caml_garbage_collection
|
|
|
|
*/
|
2005-07-29 05:11:01 -07:00
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
void caml_record_signal(int signal_number)
|
|
|
|
{
|
|
|
|
caml_pending_signals[signal_number] = 1;
|
|
|
|
caml_signals_are_pending = 1;
|
|
|
|
#ifndef NATIVE_CODE
|
|
|
|
caml_something_to_do = 1;
|
|
|
|
#else
|
|
|
|
caml_young_limit = caml_young_end;
|
|
|
|
#endif
|
1999-12-23 09:33:46 -08:00
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
/* Management of blocking sections. */
|
|
|
|
|
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;
|
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
CAMLexport void caml_enter_blocking_section(void)
|
|
|
|
{
|
|
|
|
while (1){
|
|
|
|
/* Process all pending signals now */
|
|
|
|
caml_process_pending_signals();
|
|
|
|
caml_enter_blocking_section_hook ();
|
|
|
|
/* Check again for pending signals.
|
|
|
|
If none, done; otherwise, try again */
|
|
|
|
if (! caml_signals_are_pending) break;
|
|
|
|
caml_leave_blocking_section_hook ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLexport void caml_leave_blocking_section(void)
|
|
|
|
{
|
|
|
|
caml_leave_blocking_section_hook ();
|
|
|
|
caml_process_pending_signals();
|
|
|
|
}
|
2000-11-27 08:10:49 -08:00
|
|
|
|
2005-10-12 05:33:47 -07:00
|
|
|
/* Execute a signal handler immediately */
|
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
static value caml_signal_handlers = 0;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
/* Arrange for a garbage collection to be performed as soon as possible */
|
2005-10-12 05:33:47 -07:00
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
int volatile caml_force_major_slice = 0;
|
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;
|
2007-02-23 01:29:45 -08:00
|
|
|
#ifndef NATIVE_CODE
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_something_to_do = 1;
|
2007-02-23 01:29:45 -08:00
|
|
|
#else
|
|
|
|
caml_young_limit = caml_young_end;
|
|
|
|
/* This is only moderately effective on ports that cache [caml_young_limit]
|
|
|
|
in a register, since [caml_modify] is called directly, not through
|
|
|
|
[caml_c_call], so it may take a while before the register is reloaded
|
|
|
|
from [caml_young_limit]. */
|
|
|
|
#endif
|
1995-12-22 08:48:17 -08:00
|
|
|
}
|
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
/* OS-independent numbering of 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;
|
|
|
|
}
|
|
|
|
|
2007-02-23 01:29:45 -08:00
|
|
|
/* Installation of a signal handler (as per [Sys.signal]) */
|
|
|
|
|
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);
|
|
|
|
CAMLlocal1 (res);
|
2007-02-23 01:29:45 -08:00
|
|
|
int sig, act, oldact;
|
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));
|
2010-01-22 04:48:24 -08: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 */
|
2007-02-23 01:29:45 -08:00
|
|
|
act = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
break;
|
1995-08-10 01:21:42 -07:00
|
|
|
case Val_int(1): /* Signal_ignore */
|
2007-02-23 01:29:45 -08:00
|
|
|
act = 1;
|
1995-05-04 03:15:53 -07:00
|
|
|
break;
|
1995-08-10 01:21:42 -07:00
|
|
|
default: /* Signal_handle */
|
2007-02-23 01:29:45 -08:00
|
|
|
act = 2;
|
1995-05-04 03:15:53 -07:00
|
|
|
break;
|
|
|
|
}
|
2007-02-23 01:29:45 -08:00
|
|
|
oldact = caml_set_signal_action(sig, act);
|
|
|
|
switch (oldact) {
|
|
|
|
case 0: /* was Signal_default */
|
|
|
|
res = Val_int(0);
|
|
|
|
break;
|
|
|
|
case 1: /* was Signal_ignore */
|
|
|
|
res = Val_int(1);
|
|
|
|
break;
|
|
|
|
case 2: /* was Signal_handle */
|
|
|
|
res = caml_alloc_small (1, 0);
|
2004-01-01 08:42:43 -08:00
|
|
|
Field(res, 0) = Field(caml_signal_handlers, sig);
|
2007-02-23 01:29:45 -08:00
|
|
|
break;
|
|
|
|
default: /* error in caml_set_signal_action */
|
|
|
|
caml_sys_error(NO_ARG);
|
1998-08-08 09:52:33 -07:00
|
|
|
}
|
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
|
|
|
}
|