Convert signal number back to symbolic identifiers when possible (PR#395). Wrong handler was returned by Sys.signal (PR#226).

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@3536 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
master
Xavier Leroy 2001-06-19 08:43:11 +00:00
parent 7fac15e142
commit 5dea37e252
1 changed files with 21 additions and 12 deletions

View File

@ -54,6 +54,8 @@ value signal_handlers = 0;
void (*enter_blocking_section_hook)() = NULL; void (*enter_blocking_section_hook)() = NULL;
void (*leave_blocking_section_hook)() = NULL; void (*leave_blocking_section_hook)() = NULL;
static int rev_convert_signal_number(int signo);
/* Execute a signal handler immediately. */ /* Execute a signal handler immediately. */
void execute_signal(int signal_number, int in_signal_handler) void execute_signal(int signal_number, int in_signal_handler)
@ -68,7 +70,7 @@ void execute_signal(int signal_number, int in_signal_handler)
sigprocmask(SIG_BLOCK, &sigs, &sigs); sigprocmask(SIG_BLOCK, &sigs, &sigs);
#endif #endif
res = callback_exn(Field(signal_handlers, signal_number), res = callback_exn(Field(signal_handlers, signal_number),
Val_int(signal_number)); Val_int(rev_convert_signal_number(signal_number)));
#ifdef POSIX_SIGNALS #ifdef POSIX_SIGNALS
if (! in_signal_handler) { if (! in_signal_handler) {
/* Restore the original signal mask */ /* Restore the original signal mask */
@ -269,18 +271,27 @@ int convert_signal_number(int signo)
return signo; return signo;
} }
static int rev_convert_signal_number(int signo)
{
int i;
for (i = 0; i < sizeof(posix_signals) / sizeof(int); i++)
if (signo == posix_signals[i]) return -i - 1;
return signo;
}
#ifndef NSIG #ifndef NSIG
#define NSIG 32 #define NSIG 32
#endif #endif
value install_signal_handler(value signal_number, value action) /* ML */ value install_signal_handler(value signal_number, value action) /* ML */
{ {
CAMLparam2 (signal_number, action);
int sig; int sig;
void (*act)(int signo), (*oldact)(int signo); void (*act)(int signo), (*oldact)(int signo);
#ifdef POSIX_SIGNALS #ifdef POSIX_SIGNALS
struct sigaction sigact, oldsigact; struct sigaction sigact, oldsigact;
#endif #endif
value res; CAMLlocal1 (res);
sig = convert_signal_number(Int_val(signal_number)); sig = convert_signal_number(Int_val(signal_number));
if (sig < 0 || sig >= NSIG) if (sig < 0 || sig >= NSIG)
@ -293,16 +304,7 @@ value install_signal_handler(value signal_number, value action) /* ML */
act = SIG_IGN; act = SIG_IGN;
break; break;
default: /* Signal_handle */ default: /* Signal_handle */
if (signal_handlers == 0) { act = handle_signal;
int i;
Begin_root (action);
signal_handlers = alloc_tuple(NSIG);
End_roots();
for (i = 0; i < NSIG; i++) Field(signal_handlers, i) = Val_int(0);
register_global_root(&signal_handlers);
}
modify(&Field(signal_handlers, sig), Field(action, 0));
act = (void (*)(int)) handle_signal;
break; break;
} }
#ifdef POSIX_SIGNALS #ifdef POSIX_SIGNALS
@ -323,6 +325,13 @@ value install_signal_handler(value signal_number, value action) /* ML */
res = Val_int(1); /* Signal_ignore */ res = Val_int(1); /* Signal_ignore */
else else
res = Val_int(0); /* Signal_default */ res = Val_int(0); /* Signal_default */
if (Is_block(action)) {
if (signal_handlers == 0) {
signal_handlers = alloc(NSIG, 0);
register_global_root(&signal_handlers);
}
modify(&Field(signal_handlers, sig), Field(action, 0));
}
return res; return res;
} }