| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* unixint.d -- Unix interrupt interface. |
| 6 |
* |
| 7 |
* Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya. |
| 8 |
* Copyright (c) 1990, Giuseppe Attardi. |
| 9 |
* Copyright (c) 2001, Juan Jose Garcia Ripoll. |
| 10 |
* Copyright (c) 2016, Daniel Kochmański. |
| 11 |
* |
| 12 |
* See file 'LICENSE' for the copyright details. |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
/********************************************************************** |
| 17 |
* HOW WE HANDLE SIGNALS AND EXCEPTIONS |
| 18 |
* |
| 19 |
* (the following should be correlated with the manual) |
| 20 |
* |
| 21 |
* POSIX contemplates the notion of "signals", which are events that |
| 22 |
* cause a process or a thread to be interrupted. Windows uses the |
| 23 |
* term "exception", which includes also a more general kind of |
| 24 |
* errors. |
| 25 |
* |
| 26 |
* In both cases the consequence is that a thread or process may be |
| 27 |
* interrupted at any time, either by causes which are intrinsic to |
| 28 |
* them (synchronous signals), such as floating point exceptions, or |
| 29 |
* extrinsic (asynchronous signals), such as the process being aborted |
| 30 |
* by the user. |
| 31 |
* |
| 32 |
* Of course, those interruptions are not always welcome. When the |
| 33 |
* interrupt is delivered and a handler is invoked, the thread or even |
| 34 |
* the whole program may be in an inconsistent state. For instance the |
| 35 |
* thread may have acquired a lock, or it may be in the process of |
| 36 |
* filling the fields of a structure. Understanding this POSIX |
| 37 |
* restricts severely what functions can be called from a signal |
| 38 |
* handler, thereby limiting its usefulness. |
| 39 |
* |
| 40 |
* There is a simple solution, which ECL uses, and which is to mark |
| 41 |
* sections of code which are interruptible, and in which it is safe |
| 42 |
* for the handler to run arbitrary code, protect anything else. In |
| 43 |
* principle this "marking" can be done using POSIX functions such as |
| 44 |
* pthread_sigmask() or sigprocmask(). |
| 45 |
* |
| 46 |
* However in practice this is slow, as it involves at least a |
| 47 |
* function call, resolving thread-local variables, etc, etc, and it |
| 48 |
* will not work in Windows. Furthermore, sometimes we want signals to |
| 49 |
* be detected but not to be immediately processed. For instance, when |
| 50 |
* reading from the terminal we want to be able to interrupt the |
| 51 |
* process, but we can not execute the code from the handler, since |
| 52 |
* read() may leave the input stream in an inconsistent, or even |
| 53 |
* locked state. |
| 54 |
* |
| 55 |
* Our approach is slightly different: we install our own signal |
| 56 |
* hander which reads a single, thread-local variable stored in the |
| 57 |
* ecl_process_env()->disable_interrupts. If the variable marks that |
| 58 |
* signals should be postponed, then the information about the signal |
| 59 |
* is queued. Otherwise the appropriate code is executed: for instance |
| 60 |
* invoking the debugger, jumping to a condition handler, quitting, |
| 61 |
* etc. |
| 62 |
*/ |
| 63 |
|
| 64 |
#ifdef __sun__ |
| 65 |
/* For SA_SIGINFO in Solaris. We could have used _XOPEN_SOURCE=600, but |
| 66 |
* this requires C99 and the default GCC for Solaris (3.4.3) does not |
| 67 |
* support this C standard. */ |
| 68 |
# define __EXTENSIONS__ |
| 69 |
#endif |
| 70 |
#include <errno.h> |
| 71 |
#include <string.h> |
| 72 |
#include <stdio.h> |
| 73 |
#include <signal.h> |
| 74 |
|
| 75 |
#if defined(_MSC_VER) || defined(__MINGW32__) |
| 76 |
# include <windows.h> |
| 77 |
#endif |
| 78 |
#if !defined(_MSC_VER) |
| 79 |
# include <unistd.h> |
| 80 |
#endif |
| 81 |
|
| 82 |
#include <ecl/ecl.h> |
| 83 |
|
| 84 |
#ifdef ECL_USE_MPROTECT |
| 85 |
# ifndef SA_SIGINFO |
| 86 |
# error "We cannot use the mmap code without siginfo" |
| 87 |
# endif |
| 88 |
# include <sys/mman.h> |
| 89 |
#endif |
| 90 |
|
| 91 |
#include <ecl/internal.h> |
| 92 |
#include <ecl/ecl-inl.h> |
| 93 |
#include <ecl/impl/math_fenv.h> |
| 94 |
|
| 95 |
static struct { |
| 96 |
int code; |
| 97 |
char *name; |
| 98 |
cl_object handler; |
| 99 |
} known_signals[] = { |
| 100 |
#ifdef SIGHUP |
| 101 |
{ SIGHUP, "+SIGHUP+", ECL_NIL}, |
| 102 |
#endif |
| 103 |
#ifdef SIGINT |
| 104 |
{ SIGINT, "+SIGINT+", @'si::terminal-interrupt'}, |
| 105 |
#endif |
| 106 |
#ifdef SIGQUIT |
| 107 |
{ SIGQUIT, "+SIGQUIT+", ECL_NIL}, |
| 108 |
#endif |
| 109 |
#ifdef SIGILL |
| 110 |
{ SIGILL, "+SIGILL+", @'ext::illegal-instruction'}, |
| 111 |
#endif |
| 112 |
#ifdef SIGTRAP |
| 113 |
{ SIGTRAP, "+SIGTRAP+", ECL_NIL}, |
| 114 |
#endif |
| 115 |
#ifdef SIGABRT |
| 116 |
{ SIGABRT, "+SIGABRT+", ECL_NIL}, |
| 117 |
#endif |
| 118 |
#ifdef SIGEMT |
| 119 |
{ SIGEMT, "+SIGEMT+", ECL_NIL}, |
| 120 |
#endif |
| 121 |
#ifdef SIGFPE |
| 122 |
{ SIGFPE, "+SIGFPE+", ECL_NIL}, |
| 123 |
#endif |
| 124 |
#ifdef SIGKILL |
| 125 |
{ SIGKILL, "+SIGKILL+", ECL_NIL}, |
| 126 |
#endif |
| 127 |
#ifdef SIGBUS |
| 128 |
{ SIGBUS, "+SIGBUS+", @'ext::segmentation-violation'}, |
| 129 |
#endif |
| 130 |
#ifdef SIGSEGV |
| 131 |
{ SIGSEGV, "+SIGSEGV+", @'ext::segmentation-violation'}, |
| 132 |
#endif |
| 133 |
#ifdef SIGSYS |
| 134 |
{ SIGSYS, "+SIGSYS+", ECL_NIL}, |
| 135 |
#endif |
| 136 |
#ifdef SIGPIPE |
| 137 |
{ SIGPIPE, "+SIGPIPE+", ECL_NIL}, |
| 138 |
#endif |
| 139 |
#ifdef SIGALRM |
| 140 |
{ SIGALRM, "+SIGALRM+", ECL_NIL}, |
| 141 |
#endif |
| 142 |
#ifdef SIGTERM |
| 143 |
{ SIGTERM, "+SIGTERM+", ECL_NIL}, |
| 144 |
#endif |
| 145 |
#ifdef SIGURG |
| 146 |
{ SIGURG, "+SIGURG+", ECL_NIL}, |
| 147 |
#endif |
| 148 |
#ifdef SIGSTOP |
| 149 |
{ SIGSTOP, "+SIGSTOP+", ECL_NIL}, |
| 150 |
#endif |
| 151 |
#ifdef SIGTSTP |
| 152 |
{ SIGTSTP, "+SIGTSTP+", ECL_NIL}, |
| 153 |
#endif |
| 154 |
#ifdef SIGCONT |
| 155 |
{ SIGCONT, "+SIGCONT+", ECL_NIL}, |
| 156 |
#endif |
| 157 |
#ifdef SIGCHLD |
| 158 |
{ SIGCHLD, "+SIGCHLD+", ECL_NIL}, |
| 159 |
#endif |
| 160 |
#ifdef SIGTTIN |
| 161 |
{ SIGTTIN, "+SIGTTIN+", ECL_NIL}, |
| 162 |
#endif |
| 163 |
#ifdef SIGTTOU |
| 164 |
{ SIGTTOU, "+SIGTTOU+", ECL_NIL}, |
| 165 |
#endif |
| 166 |
#ifdef SIGIO |
| 167 |
{ SIGIO, "+SIGIO+", ECL_NIL}, |
| 168 |
#endif |
| 169 |
#ifdef SIGXCPU |
| 170 |
{ SIGXCPU, "+SIGXCPU+", ECL_NIL}, |
| 171 |
#endif |
| 172 |
#ifdef SIGXFSZ |
| 173 |
{ SIGXFSZ, "+SIGXFSZ+", ECL_NIL}, |
| 174 |
#endif |
| 175 |
#ifdef SIGVTALRM |
| 176 |
{ SIGVTALRM, "+SIGVTALRM+", ECL_NIL}, |
| 177 |
#endif |
| 178 |
#ifdef SIGPROF |
| 179 |
{ SIGPROF, "+SIGPROF+", ECL_NIL}, |
| 180 |
#endif |
| 181 |
#ifdef SIGWINCH |
| 182 |
{ SIGWINCH, "+SIGWINCH+", ECL_NIL}, |
| 183 |
#endif |
| 184 |
#ifdef SIGINFO |
| 185 |
{ SIGINFO, "+SIGINFO+", ECL_NIL}, |
| 186 |
#endif |
| 187 |
#ifdef SIGUSR1 |
| 188 |
{ SIGUSR1, "+SIGUSR1+", ECL_NIL}, |
| 189 |
#endif |
| 190 |
#ifdef SIGUSR2 |
| 191 |
{ SIGUSR2, "+SIGUSR2+", ECL_NIL}, |
| 192 |
#endif |
| 193 |
#ifdef SIGTHR |
| 194 |
{ SIGTHR, "+SIGTHR+", ECL_NIL}, |
| 195 |
#endif |
| 196 |
{ -1, "", ECL_NIL } |
| 197 |
}; |
| 198 |
|
| 199 |
#ifdef HAVE_SIGPROCMASK |
| 200 |
static sigset_t main_thread_sigmask; |
| 201 |
# define handler_fn_prototype(name, sig, info, aux) name(sig, info, aux) |
| 202 |
# define call_handler(name, sig, info, aux) name(sig, info, aux) |
| 203 |
# define reinstall_signal(x,y) |
| 204 |
# define copy_siginfo(x,y) memcpy(x, y, sizeof(struct sigaction)) |
| 205 |
static void |
| 206 |
mysignal(int code, void *handler) |
| 207 |
{ |
| 208 |
struct sigaction action; |
| 209 |
sigaction(code, NULL, &action); |
| 210 |
if (handler == SIG_IGN || handler == SIG_DFL) { |
| 211 |
action.sa_handler = handler; |
| 212 |
} else { |
| 213 |
#ifdef SA_SIGINFO |
| 214 |
/* void (*handler)(int, siginfo_t *, void*) */ |
| 215 |
action.sa_sigaction = handler; |
| 216 |
action.sa_flags = SA_SIGINFO; |
| 217 |
#else |
| 218 |
/* void (*handler)(int) */ |
| 219 |
action.sa_handler = handler; |
| 220 |
action.sa_flags = 0; |
| 221 |
#endif |
| 222 |
sigfillset(&action.sa_mask); |
| 223 |
} |
| 224 |
sigaction(code, &action, NULL); |
| 225 |
} |
| 226 |
#else /* HAVE_SIGPROCMASK */ |
| 227 |
# define handler_fn_prototype(name, sig, info, aux) name(sig) |
| 228 |
# define call_handler(name, sig, info, aux) name(sig) |
| 229 |
# define mysignal(x,y) signal(x,y) |
| 230 |
# define reinstall_signal(x,y) signal(x,y) |
| 231 |
# define copy_siginfo(x,y) |
| 232 |
#endif |
| 233 |
|
| 234 |
static bool |
| 235 |
zombie_process(cl_env_ptr the_env) |
| 236 |
{ |
| 237 |
#ifdef ECL_THREADS |
| 238 |
if (the_env == NULL) { |
| 239 |
return 1; |
| 240 |
} else { |
| 241 |
/* When we are exiting a thread, we simply ignore all signals. */ |
| 242 |
cl_object process = the_env->own_process; |
| 243 |
return (process->process.phase == ECL_PROCESS_INACTIVE); |
| 244 |
} |
| 245 |
#else |
| 246 |
return !the_env; |
| 247 |
#endif |
| 248 |
} |
| 249 |
|
| 250 |
static ECL_INLINE bool |
| 251 |
interrupts_disabled_by_C(cl_env_ptr the_env) |
| 252 |
{ |
| 253 |
return the_env->disable_interrupts; |
| 254 |
} |
| 255 |
|
| 256 |
static ECL_INLINE bool |
| 257 |
interrupts_disabled_by_lisp(cl_env_ptr the_env) |
| 258 |
{ |
| 259 |
return !ecl_option_values[ECL_OPT_BOOTED] || |
| 260 |
Null(ECL_SYM_VAL(the_env, @'ext::*interrupts-enabled*')); |
| 261 |
} |
| 262 |
|
| 263 |
static void early_signal_error() ecl_attr_noreturn; |
| 264 |
|
| 265 |
static void |
| 266 |
early_signal_error() |
| 267 |
{ |
| 268 |
ecl_internal_error("Got signal before environment was installed" |
| 269 |
" on our thread"); |
| 270 |
} |
| 271 |
|
| 272 |
static void illegal_signal_code(cl_object code) ecl_attr_noreturn; |
| 273 |
|
| 274 |
static void |
| 275 |
illegal_signal_code(cl_object code) |
| 276 |
{ |
| 277 |
FEerror("Unknown signal code: ~D", 1, code); |
| 278 |
} |
| 279 |
|
| 280 |
/* On platforms in which mprotect() works, we block all write access |
| 281 |
* to the environment for a cheap check of pending interrupts. On |
| 282 |
* other platforms we change the value of disable_interrupts to 3, so |
| 283 |
* that we detect changes. */ |
| 284 |
static ECL_INLINE void |
| 285 |
set_guard_page(cl_env_ptr the_env) |
| 286 |
{ |
| 287 |
#if defined(ECL_USE_MPROTECT) |
| 288 |
if (mprotect(the_env, sizeof(*the_env), PROT_READ) < 0) { |
| 289 |
ecl_internal_error("Unable to mprotect environment."); |
| 290 |
} |
| 291 |
#elif defined(ECL_USE_GUARD_PAGE) |
| 292 |
if (!VirtualProtect(the_env, sizeof(*the_env), PAGE_GUARD, NULL)) { |
| 293 |
ecl_internal_error("Unable to mprotect environment."); |
| 294 |
} |
| 295 |
#endif |
| 296 |
} |
| 297 |
|
| 298 |
static cl_object pop_signal(cl_env_ptr env); |
| 299 |
|
| 300 |
#define unblock_signal(env, sig) |
| 301 |
#ifdef HAVE_SIGPROCMASK |
| 302 |
# undef unblock_signal |
| 303 |
static void |
| 304 |
unblock_signal(cl_env_ptr the_env, int signal) |
| 305 |
{ |
| 306 |
/* |
| 307 |
* We do not really "unblock" the signal, but rather restore |
| 308 |
* ECL's default sigmask. |
| 309 |
*/ |
| 310 |
# ifdef ECL_THREADS |
| 311 |
pthread_sigmask(SIG_SETMASK, the_env->default_sigmask, NULL); |
| 312 |
# else |
| 313 |
sigprocmask(SIG_SETMASK, the_env->default_sigmask, NULL); |
| 314 |
# endif |
| 315 |
} |
| 316 |
#endif |
| 317 |
|
| 318 |
ecl_def_ct_base_string(str_ignore_signal,"Ignore signal",13,static,const); |
| 319 |
|
| 320 |
static void |
| 321 |
handle_signal_now(cl_object signal_code) |
| 322 |
{ |
| 323 |
switch (ecl_t_of(signal_code)) { |
| 324 |
case t_fixnum: |
| 325 |
cl_cerror(4, str_ignore_signal, @'ext::unix-signal-received', |
| 326 |
@':code', signal_code); |
| 327 |
break; |
| 328 |
case t_symbol: |
| 329 |
/* |
| 330 |
* When we bind a handler to a signal, it may either |
| 331 |
* be a function, a symbol denoting a function or |
| 332 |
* a symbol denoting a condition. |
| 333 |
*/ |
| 334 |
if (cl_find_class(2, signal_code, ECL_NIL) != ECL_NIL) { |
| 335 |
cl_cerror(2, str_ignore_signal, signal_code); |
| 336 |
break; |
| 337 |
} |
| 338 |
/* fallthrough */ |
| 339 |
case t_cfun: |
| 340 |
case t_cfunfixed: |
| 341 |
case t_cclosure: |
| 342 |
case t_bytecodes: |
| 343 |
case t_bclosure: |
| 344 |
_ecl_funcall1(signal_code); |
| 345 |
default: |
| 346 |
break; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
cl_object |
| 351 |
si_handle_signal(cl_object signal_code) |
| 352 |
{ |
| 353 |
const cl_env_ptr the_env = ecl_process_env(); |
| 354 |
handle_signal_now(signal_code); |
| 355 |
ecl_return0(the_env); |
| 356 |
} |
| 357 |
|
| 358 |
static void |
| 359 |
handle_all_queued(cl_env_ptr env) |
| 360 |
{ |
| 361 |
while (env->interrupt_struct->pending_interrupt != ECL_NIL) { |
| 362 |
handle_signal_now(pop_signal(env)); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
static void |
| 367 |
handle_all_queued_interrupt_safe(cl_env_ptr env) |
| 368 |
{ |
| 369 |
/* We have to save and later restore thread-local variables to |
| 370 |
* ensure that they don't get overwritten by the interrupting |
| 371 |
* code */ |
| 372 |
/* INV: - IHS stack manipulations are interrupt safe |
| 373 |
* - The rest of the thread local variables are |
| 374 |
* guaranteed to be used in an interrupt safe way. This |
| 375 |
* is not true for the compiler environment and ffi |
| 376 |
* data, but it is unclear whether the DFFI or compiler |
| 377 |
* are thread safe anyway. */ |
| 378 |
cl_object fun = env->function; |
| 379 |
cl_index nvalues = env->nvalues; |
| 380 |
cl_object values[ECL_MULTIPLE_VALUES_LIMIT]; |
| 381 |
memcpy(values, env->values, ECL_MULTIPLE_VALUES_LIMIT*sizeof(cl_object)); |
| 382 |
cl_object stack_frame = env->stack_frame; |
| 383 |
cl_object packages_to_be_created = env->packages_to_be_created; |
| 384 |
cl_object packages_to_be_created_p = env->packages_to_be_created_p; |
| 385 |
/* bignum registers need some special handling, because their |
| 386 |
* contents are allocated as uncollectable memory. If we did |
| 387 |
* not init and clear them before calling the interrupting |
| 388 |
* code we would risk memory leaks. */ |
| 389 |
cl_object big_register[ECL_BIGNUM_REGISTER_NUMBER]; |
| 390 |
memcpy(big_register, env->big_register, ECL_BIGNUM_REGISTER_NUMBER*sizeof(cl_object)); |
| 391 |
ecl_init_bignum_registers(env); |
| 392 |
/* We might have been interrupted while we push/pop in the stack. Increasing |
| 393 |
* env->run_stack.top ensures that we don't overwrite the topmost stack |
| 394 |
* value. */ |
| 395 |
env->run_stack.top++; |
| 396 |
/* We also need to save and restore the (top+1)'th frame and |
| 397 |
* binding stack value to prevent overwriting it. |
| 398 |
* INV: Due to the stack safety areas we don't need to check |
| 399 |
* for env->frs/bds_limit */ |
| 400 |
struct ecl_frame top_frame; |
| 401 |
memcpy(&top_frame, env->frs_stack.top+1, sizeof(struct ecl_frame)); |
| 402 |
struct ecl_bds_frame top_binding; |
| 403 |
memcpy(&top_binding, env->bds_stack.top+1, sizeof(struct ecl_bds_frame)); |
| 404 |
/* Finally we can handle the queued signals ... */ |
| 405 |
handle_all_queued(env); |
| 406 |
/* ... and restore everything again */ |
| 407 |
memcpy(env->bds_stack.top+1, &top_binding, sizeof(struct ecl_bds_frame)); |
| 408 |
memcpy(env->frs_stack.top+1, &top_frame, sizeof(struct ecl_frame)); |
| 409 |
env->run_stack.top--; |
| 410 |
ecl_clear_bignum_registers(env); |
| 411 |
memcpy(env->big_register, big_register, ECL_BIGNUM_REGISTER_NUMBER*sizeof(cl_object)); |
| 412 |
env->packages_to_be_created_p = packages_to_be_created_p; |
| 413 |
env->packages_to_be_created = packages_to_be_created; |
| 414 |
env->stack_frame = stack_frame; |
| 415 |
memcpy(env->values, values, ECL_MULTIPLE_VALUES_LIMIT*sizeof(cl_object)); |
| 416 |
env->nvalues = nvalues; |
| 417 |
env->function = fun; |
| 418 |
} |
| 419 |
|
| 420 |
static void |
| 421 |
queue_signal(cl_env_ptr env, cl_object code, int allocate) |
| 422 |
{ |
| 423 |
/* Note: We don't use ECL_WITH_NATIVE_LOCK_BEGIN/END here |
| 424 |
* since it checks for pending interrupts after unlocking the |
| 425 |
* mutex. This would lead to the interrupt being handled |
| 426 |
* immediately when queueing an interrupt for the current |
| 427 |
* thread, even when interrupts are disabled. */ |
| 428 |
|
| 429 |
/* INV: interrupts are disabled, therefore the lock will |
| 430 |
* always be released */ |
| 431 |
#ifdef ECL_THREADS |
| 432 |
ecl_mutex_lock(&env->interrupt_struct->signal_queue_lock); |
| 433 |
#endif |
| 434 |
|
| 435 |
cl_object record; |
| 436 |
if (allocate) { |
| 437 |
record = ecl_list1(ECL_NIL); |
| 438 |
} else { |
| 439 |
record = env->interrupt_struct->signal_queue; |
| 440 |
if (record != ECL_NIL) { |
| 441 |
env->interrupt_struct->signal_queue = ECL_CONS_CDR(record); |
| 442 |
} |
| 443 |
} |
| 444 |
if (record != ECL_NIL) { |
| 445 |
ECL_RPLACA(record, code); |
| 446 |
ECL_RPLACD(record, ECL_NIL); |
| 447 |
env->interrupt_struct->pending_interrupt = |
| 448 |
ecl_nconc(env->interrupt_struct->pending_interrupt, record); |
| 449 |
} |
| 450 |
|
| 451 |
#ifdef ECL_THREADS |
| 452 |
ecl_mutex_unlock(&env->interrupt_struct->signal_queue_lock); |
| 453 |
#endif |
| 454 |
} |
| 455 |
|
| 456 |
static cl_object |
| 457 |
pop_signal(cl_env_ptr env) |
| 458 |
{ |
| 459 |
cl_object record, value; |
| 460 |
/* Note: We don't use ECL_WITH_NATIVE_LOCK_BEGIN/END here |
| 461 |
* since it checks for pending interrupts after unlocking the |
| 462 |
* mutex. This would lead to handle_all_queued and pop_signal |
| 463 |
* being called again and the interrupts being handled in the |
| 464 |
* wrong order. */ |
| 465 |
|
| 466 |
ecl_disable_interrupts_env(env); |
| 467 |
#ifdef ECL_THREADS |
| 468 |
ecl_mutex_lock(&env->interrupt_struct->signal_queue_lock); |
| 469 |
#endif |
| 470 |
|
| 471 |
if (env->interrupt_struct->pending_interrupt == ECL_NIL) { |
| 472 |
value = ECL_NIL; |
| 473 |
} else { |
| 474 |
record = env->interrupt_struct->pending_interrupt; |
| 475 |
value = ECL_CONS_CAR(record); |
| 476 |
env->interrupt_struct->pending_interrupt = ECL_CONS_CDR(record); |
| 477 |
/* Save some conses for future use, to avoid allocating */ |
| 478 |
if (ECL_SYMBOLP(value) || ECL_FIXNUMP(value)) { |
| 479 |
ECL_RPLACD(record, env->interrupt_struct->signal_queue); |
| 480 |
env->interrupt_struct->signal_queue = record; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
#ifdef ECL_THREADS |
| 485 |
ecl_mutex_unlock(&env->interrupt_struct->signal_queue_lock); |
| 486 |
#endif |
| 487 |
ecl_enable_interrupts_env(env); |
| 488 |
return value; |
| 489 |
} |
| 490 |
|
| 491 |
static void |
| 492 |
handle_or_queue(cl_env_ptr the_env, cl_object signal_code, int code) |
| 493 |
{ |
| 494 |
if (Null(signal_code) || signal_code == NULL) |
| 495 |
return; |
| 496 |
/* |
| 497 |
* If interrupts are disabled by lisp we are not so eager on |
| 498 |
* detecting when the interrupts become enabled again. We |
| 499 |
* queue the signal and are done with that. |
| 500 |
*/ |
| 501 |
if (interrupts_disabled_by_lisp(the_env)) { |
| 502 |
queue_signal(the_env, signal_code, 0); |
| 503 |
} |
| 504 |
/* |
| 505 |
* If interrupts are disabled by C, and we have not pushed a |
| 506 |
* pending signal, save this signal and return. |
| 507 |
*/ |
| 508 |
else if (interrupts_disabled_by_C(the_env)) { |
| 509 |
the_env->disable_interrupts = 3; |
| 510 |
queue_signal(the_env, signal_code, 0); |
| 511 |
set_guard_page(the_env); |
| 512 |
} |
| 513 |
/* |
| 514 |
* If interrupts are enabled, that means we are in a safe area |
| 515 |
* and may execute arbitrary lisp code. We can thus call the |
| 516 |
* appropriate handlers. |
| 517 |
*/ |
| 518 |
else { |
| 519 |
if (code) unblock_signal(the_env, code); |
| 520 |
si_trap_fpe(@'last', ECL_T); /* Clear FPE exception flag */ |
| 521 |
handle_signal_now(signal_code); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
static void |
| 526 |
handler_fn_prototype(non_evil_signal_handler, int sig, siginfo_t *siginfo, void *data) |
| 527 |
{ |
| 528 |
int old_errno = errno; |
| 529 |
cl_env_ptr the_env; |
| 530 |
cl_object signal_object; |
| 531 |
reinstall_signal(sig, non_evil_signal_handler); |
| 532 |
/* The lisp environment might not be installed. */ |
| 533 |
the_env = ecl_process_env(); |
| 534 |
unlikely_if (zombie_process(the_env)) |
| 535 |
return; |
| 536 |
signal_object = ecl_gethash_safe(ecl_make_fixnum(sig), |
| 537 |
cl_core.known_signals, |
| 538 |
ECL_NIL); |
| 539 |
handle_or_queue(the_env, signal_object, sig); |
| 540 |
errno = old_errno; |
| 541 |
} |
| 542 |
|
| 543 |
static void |
| 544 |
handler_fn_prototype(evil_signal_handler, int sig, siginfo_t *siginfo, void *data) |
| 545 |
{ |
| 546 |
int old_errno = errno; |
| 547 |
cl_env_ptr the_env; |
| 548 |
cl_object signal_object; |
| 549 |
reinstall_signal(sig, evil_signal_handler); |
| 550 |
/* The lisp environment might not be installed. */ |
| 551 |
the_env = ecl_process_env(); |
| 552 |
unlikely_if (zombie_process(the_env)) |
| 553 |
return; |
| 554 |
signal_object = ecl_gethash_safe(ecl_make_fixnum(sig), |
| 555 |
cl_core.known_signals, |
| 556 |
ECL_NIL); |
| 557 |
handle_signal_now(signal_object); |
| 558 |
errno = old_errno; |
| 559 |
} |
| 560 |
|
| 561 |
#if defined(ECL_THREADS) && defined(HAVE_SIGPROCMASK) |
| 562 |
typedef struct { |
| 563 |
cl_object process; |
| 564 |
int signo; |
| 565 |
} signal_thread_message; |
| 566 |
static cl_object signal_thread_process = ECL_NIL; |
| 567 |
static signal_thread_message signal_thread_msg; |
| 568 |
static ecl_mutex_t signal_thread_lock; |
| 569 |
static int signal_thread_pipe[2] = {-1,-1}; |
| 570 |
|
| 571 |
static void |
| 572 |
handler_fn_prototype(deferred_signal_handler, int sig, siginfo_t *siginfo, void *data) |
| 573 |
{ |
| 574 |
int old_errno = errno; |
| 575 |
cl_env_ptr the_env; |
| 576 |
signal_thread_message msg; |
| 577 |
reinstall_signal(sig, deferred_signal_handler); |
| 578 |
/* The lisp environment might not be installed. */ |
| 579 |
the_env = ecl_process_env(); |
| 580 |
unlikely_if (zombie_process(the_env)) |
| 581 |
return; |
| 582 |
msg.signo = sig; |
| 583 |
msg.process = the_env->own_process; |
| 584 |
if (msg.process == signal_thread_process) { |
| 585 |
/* The signal handling thread may also receive signals. In |
| 586 |
* this case we do not use the pipe, but just copy the message |
| 587 |
* Note that read() will abort the thread will get notified. */ |
| 588 |
signal_thread_msg = msg; |
| 589 |
} else if (signal_thread_pipe[1] > 0) { |
| 590 |
ecl_mutex_lock(&signal_thread_lock); |
| 591 |
write(signal_thread_pipe[1], &msg, sizeof(msg)); |
| 592 |
ecl_mutex_unlock(&signal_thread_lock); |
| 593 |
} else { |
| 594 |
/* Nothing to do. There is no way to handle this signal because |
| 595 |
* the responsible thread is not running */ |
| 596 |
} |
| 597 |
errno = old_errno; |
| 598 |
} |
| 599 |
|
| 600 |
static cl_object |
| 601 |
asynchronous_signal_servicing_thread() |
| 602 |
{ |
| 603 |
const cl_env_ptr the_env = ecl_process_env(); |
| 604 |
int interrupt_signal = -1; |
| 605 |
/* |
| 606 |
* We block all signals except the usual interrupt thread and |
| 607 |
* GC signals (including SIGSEGV and SIGSEGV which are needed |
| 608 |
* when the GC runs in incremental mode). |
| 609 |
*/ |
| 610 |
{ |
| 611 |
sigset_t handled_set; |
| 612 |
sigfillset(&handled_set); |
| 613 |
if (ecl_option_values[ECL_OPT_TRAP_INTERRUPT_SIGNAL]) { |
| 614 |
interrupt_signal = |
| 615 |
ecl_option_values[ECL_OPT_THREAD_INTERRUPT_SIGNAL]; |
| 616 |
sigdelset(&handled_set, interrupt_signal); |
| 617 |
sigdelset(&handled_set, GC_get_suspend_signal()); |
| 618 |
sigdelset(&handled_set, GC_get_thr_restart_signal()); |
| 619 |
sigdelset(&handled_set, SIGSEGV); |
| 620 |
sigdelset(&handled_set, SIGBUS); |
| 621 |
} |
| 622 |
pthread_sigmask(SIG_BLOCK, &handled_set, NULL); |
| 623 |
} |
| 624 |
/* |
| 625 |
* We create the object for communication. We need a lock to prevent other |
| 626 |
* threads from writing before the pipe is created. |
| 627 |
*/ |
| 628 |
ecl_mutex_lock(&signal_thread_lock); |
| 629 |
pipe(signal_thread_pipe); |
| 630 |
ecl_mutex_unlock(&signal_thread_lock); |
| 631 |
signal_thread_msg.process = ECL_NIL; |
| 632 |
for (;;) { |
| 633 |
cl_object signal_code; |
| 634 |
signal_thread_msg.process = ECL_NIL; |
| 635 |
if (read(signal_thread_pipe[0], &signal_thread_msg, |
| 636 |
sizeof(signal_thread_msg)) < 0) |
| 637 |
{ |
| 638 |
/* Either the pipe errs or we have received an interrupt |
| 639 |
* from a different thread */ |
| 640 |
if (errno != EINTR || |
| 641 |
signal_thread_msg.process != the_env->own_process) |
| 642 |
break; |
| 643 |
} |
| 644 |
/* We have queued ourselves an interrupt event */ |
| 645 |
if (signal_thread_msg.signo == interrupt_signal && |
| 646 |
signal_thread_msg.process == the_env->own_process) { |
| 647 |
break; |
| 648 |
} |
| 649 |
signal_code = ecl_gethash_safe(ecl_make_fixnum(signal_thread_msg.signo), |
| 650 |
cl_core.known_signals, |
| 651 |
ECL_NIL); |
| 652 |
if (!Null(signal_code)) { |
| 653 |
mp_process_run_function(3, @'si::handle-signal', |
| 654 |
@'si::handle-signal', |
| 655 |
signal_code); |
| 656 |
} |
| 657 |
} |
| 658 |
# if defined(ECL_USE_MPROTECT) |
| 659 |
/* We might have protected our own environment */ |
| 660 |
mprotect(the_env, sizeof(*the_env), PROT_READ | PROT_WRITE); |
| 661 |
# endif /* ECL_USE_MPROTECT */ |
| 662 |
close(signal_thread_pipe[0]); |
| 663 |
close(signal_thread_pipe[1]); |
| 664 |
ecl_return0(the_env); |
| 665 |
} |
| 666 |
#endif /* ECL_THREADS && !ECL_MS_WINDOWS_HOST */ |
| 667 |
|
| 668 |
#if defined(ECL_THREADS) && !defined(ECL_MS_WINDOWS_HOST) |
| 669 |
static void |
| 670 |
handler_fn_prototype(process_interrupt_handler, int sig, siginfo_t *siginfo, void *data) |
| 671 |
{ |
| 672 |
int old_errno = errno; |
| 673 |
cl_env_ptr the_env; |
| 674 |
reinstall_signal(sig, process_interrupt_handler); |
| 675 |
/* The lisp environment might not be installed. */ |
| 676 |
the_env = ecl_process_env(); |
| 677 |
if (zombie_process(the_env)) |
| 678 |
return; |
| 679 |
if (!Null(the_env->interrupt_struct->pending_interrupt)) { |
| 680 |
if (interrupts_disabled_by_C(the_env)) { |
| 681 |
set_guard_page(the_env); |
| 682 |
} else if (!interrupts_disabled_by_lisp(the_env)) { |
| 683 |
unblock_signal(the_env, sig); |
| 684 |
handle_all_queued_interrupt_safe(the_env); |
| 685 |
} |
| 686 |
} |
| 687 |
errno = old_errno; |
| 688 |
} |
| 689 |
#endif /* ECL_THREADS && !ECL_MS_WINDOWS_HOST */ |
| 690 |
|
| 691 |
static void |
| 692 |
handler_fn_prototype(fpe_signal_handler, int sig, siginfo_t *info, void *data) |
| 693 |
{ |
| 694 |
int code; |
| 695 |
cl_object condition; |
| 696 |
cl_env_ptr the_env; |
| 697 |
reinstall_signal(sig, fpe_signal_handler); |
| 698 |
/* The lisp environment might not be installed. */ |
| 699 |
unlikely_if (!ecl_option_values[ECL_OPT_BOOTED]) { |
| 700 |
early_signal_error(); |
| 701 |
} |
| 702 |
the_env = ecl_process_env(); |
| 703 |
unlikely_if (zombie_process(the_env)) |
| 704 |
return; |
| 705 |
condition = @'arithmetic-error'; |
| 706 |
code = 0; |
| 707 |
#ifdef _MSC_VER |
| 708 |
switch (_fpecode) { |
| 709 |
case _FPE_INVALID: |
| 710 |
condition = @'floating-point-invalid-operation'; |
| 711 |
code = FE_INVALID; |
| 712 |
break; |
| 713 |
case _FPE_OVERFLOW: |
| 714 |
condition = @'floating-point-overflow'; |
| 715 |
code = FE_OVERFLOW; |
| 716 |
break; |
| 717 |
case _FPE_UNDERFLOW: |
| 718 |
condition = @'floating-point-underflow'; |
| 719 |
code = FE_UNDERFLOW; |
| 720 |
break; |
| 721 |
case _FPE_ZERODIVIDE: |
| 722 |
condition = @'division-by-zero'; |
| 723 |
code = FE_DIVBYZERO; |
| 724 |
break; |
| 725 |
} |
| 726 |
#else /* !_MSC_VER */ |
| 727 |
# if defined(HAVE_FENV_H) & !defined(ECL_AVOID_FENV_H) |
| 728 |
code = fetestexcept(FE_ALL_EXCEPT); |
| 729 |
if (code & FE_DIVBYZERO) { |
| 730 |
condition = @'division-by-zero'; |
| 731 |
code = FE_DIVBYZERO; |
| 732 |
} else if (code & FE_INVALID) { |
| 733 |
condition = @'floating-point-invalid-operation'; |
| 734 |
code = FE_INVALID; |
| 735 |
} else if (code & FE_OVERFLOW) { |
| 736 |
condition = @'floating-point-overflow'; |
| 737 |
code = FE_OVERFLOW; |
| 738 |
} else if (code & FE_UNDERFLOW) { |
| 739 |
condition = @'floating-point-underflow'; |
| 740 |
code = FE_UNDERFLOW; |
| 741 |
} else if (code & FE_INEXACT) { |
| 742 |
condition = @'floating-point-inexact'; |
| 743 |
code = FE_INEXACT; |
| 744 |
} |
| 745 |
feclearexcept(FE_ALL_EXCEPT); |
| 746 |
# endif |
| 747 |
#endif /* !_MSC_VER */ |
| 748 |
#if defined(SA_SIGINFO) && !defined(NACL) |
| 749 |
if (info) { |
| 750 |
if (info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV) { |
| 751 |
condition = @'division-by-zero'; |
| 752 |
code = FE_DIVBYZERO; |
| 753 |
} else if (info->si_code == FPE_FLTOVF) { |
| 754 |
condition = @'floating-point-overflow'; |
| 755 |
code = FE_OVERFLOW; |
| 756 |
} else if (info->si_code == FPE_FLTUND) { |
| 757 |
condition = @'floating-point-underflow'; |
| 758 |
code = FE_UNDERFLOW; |
| 759 |
} else if (info->si_code == FPE_FLTRES) { |
| 760 |
condition = @'floating-point-inexact'; |
| 761 |
code = FE_INEXACT; |
| 762 |
} else if (info->si_code == FPE_FLTINV) { |
| 763 |
condition = @'floating-point-invalid-operation'; |
| 764 |
code = FE_INVALID; |
| 765 |
} |
| 766 |
} |
| 767 |
#endif /* SA_SIGINFO */ |
| 768 |
/* |
| 769 |
if (code && !(code & the_env->trap_fpe_bits)) |
| 770 |
condition = ECL_NIL; |
| 771 |
*/ |
| 772 |
si_trap_fpe(@'last', ECL_T); /* Clear FPE exception flag */ |
| 773 |
unblock_signal(the_env, code); |
| 774 |
handle_signal_now(condition); |
| 775 |
/* We will not reach past this point. */ |
| 776 |
} |
| 777 |
|
| 778 |
static void |
| 779 |
handler_fn_prototype(sigsegv_handler, int sig, siginfo_t *info, void *aux) |
| 780 |
{ |
| 781 |
int old_errno = errno; |
| 782 |
static const char *stack_overflow_msg = |
| 783 |
"\n;;;\n;;; Stack overflow.\n" |
| 784 |
";;; Jumping to the outermost toplevel prompt\n" |
| 785 |
";;;\n\n"; |
| 786 |
static const char *segv_msg = |
| 787 |
"\n;;;\n" |
| 788 |
";;; Detected access to protected memory, " |
| 789 |
"also known as 'bus or segmentation fault'.\n" |
| 790 |
";;; Jumping to the outermost toplevel prompt\n" |
| 791 |
";;;\n\n"; |
| 792 |
static const char *interrupt_msg = |
| 793 |
"\n;;;\n;;; Internal error:\n" |
| 794 |
";;; Detected write access to the environment while " |
| 795 |
"interrupts were disabled. Usually this is caused by " |
| 796 |
"a missing call to ecl_enable_interrupts.\n" |
| 797 |
";;;\n\n"; |
| 798 |
cl_env_ptr the_env; |
| 799 |
reinstall_signal(sig, sigsegv_handler); |
| 800 |
/* The lisp environment might not be installed. */ |
| 801 |
unlikely_if (!ecl_option_values[ECL_OPT_BOOTED]) { |
| 802 |
early_signal_error(); |
| 803 |
} |
| 804 |
the_env = ecl_process_env(); |
| 805 |
unlikely_if (zombie_process(the_env)) |
| 806 |
return; |
| 807 |
#if defined(SA_SIGINFO) && !defined(NACL) |
| 808 |
# if defined(ECL_USE_MPROTECT) |
| 809 |
/* We access disable_interrupts when the environment was |
| 810 |
* protected. That means there was a pending signal. */ |
| 811 |
if (((char*)&the_env->disable_interrupts <= (char*)info->si_addr) && |
| 812 |
((char*)info->si_addr < (char*)(&the_env->disable_interrupts+1))) |
| 813 |
{ |
| 814 |
unblock_signal(the_env, sig); |
| 815 |
mprotect(the_env, sizeof(*the_env), PROT_READ | PROT_WRITE); |
| 816 |
the_env->disable_interrupts = 0; |
| 817 |
handle_all_queued_interrupt_safe(the_env); |
| 818 |
return; |
| 819 |
} else if (the_env->disable_interrupts && |
| 820 |
((char*)(&the_env->disable_interrupts+1) <= (char*)info->si_addr) && |
| 821 |
((char*)info->si_addr < (char*)(the_env+1))) { |
| 822 |
unblock_signal(the_env, sig); |
| 823 |
mprotect(the_env, sizeof(*the_env), PROT_READ | PROT_WRITE); |
| 824 |
the_env->disable_interrupts = 0; |
| 825 |
ecl_unrecoverable_error(the_env, interrupt_msg); |
| 826 |
return; |
| 827 |
} |
| 828 |
# endif /* ECL_USE_MPROTECT */ |
| 829 |
# ifdef ECL_DOWN_STACK |
| 830 |
if (sig == SIGSEGV && |
| 831 |
(char*)info->si_addr > the_env->c_stack.max && |
| 832 |
(char*)info->si_addr <= the_env->c_stack.org) { |
| 833 |
unblock_signal(the_env, sig); |
| 834 |
ecl_unrecoverable_error(the_env, stack_overflow_msg); |
| 835 |
return; |
| 836 |
} |
| 837 |
# else |
| 838 |
if (sig == SIGSEGV && |
| 839 |
(char*)info->si_addr < the_env->c_stack.max && |
| 840 |
(char*)info->si_addr >= the_env->c_stack.org) { |
| 841 |
unblock_signal(the_env, sig); |
| 842 |
ecl_unrecoverable_error(the_env, stack_overflow_msg); |
| 843 |
return; |
| 844 |
} |
| 845 |
# endif /* ECL_DOWN_STACK */ |
| 846 |
/* Do not attempt an error handler if we nest two serious |
| 847 |
* errors in the same thread */ |
| 848 |
if (the_env->fault_address == info->si_addr) { |
| 849 |
the_env->fault_address = info->si_addr; |
| 850 |
unblock_signal(the_env, sig); |
| 851 |
ecl_unrecoverable_error(the_env, segv_msg); |
| 852 |
} else { |
| 853 |
the_env->fault_address = info->si_addr; |
| 854 |
handle_or_queue(the_env, @'ext::segmentation-violation', sig); |
| 855 |
} |
| 856 |
#else |
| 857 |
/* |
| 858 |
* We cannot distinguish between a stack overflow and a simple |
| 859 |
* access violation. Thus we assume the worst case and jump to |
| 860 |
* the outermost handler. |
| 861 |
*/ |
| 862 |
unblock_signal(the_env, sig); |
| 863 |
ecl_unrecoverable_error(the_env, segv_msg); |
| 864 |
#endif /* SA_SIGINFO */ |
| 865 |
errno = old_errno; |
| 866 |
} |
| 867 |
|
| 868 |
cl_object |
| 869 |
si_check_pending_interrupts(void) |
| 870 |
{ |
| 871 |
const cl_env_ptr the_env = ecl_process_env(); |
| 872 |
handle_all_queued(ecl_process_env()); |
| 873 |
ecl_return0(the_env); |
| 874 |
} |
| 875 |
|
| 876 |
void |
| 877 |
ecl_check_pending_interrupts(cl_env_ptr env) |
| 878 |
{ |
| 879 |
if (env->interrupt_struct->pending_interrupt != ECL_NIL) |
| 880 |
handle_all_queued_interrupt_safe(env); |
| 881 |
} |
| 882 |
|
| 883 |
static cl_object |
| 884 |
do_catch_signal(int code, cl_object action, cl_object process) |
| 885 |
{ |
| 886 |
if (action == ECL_NIL || action == @':ignore') { |
| 887 |
mysignal(code, SIG_IGN); |
| 888 |
return ECL_T; |
| 889 |
} else if (action == @':default') { |
| 890 |
mysignal(code, SIG_DFL); |
| 891 |
return ECL_T; |
| 892 |
} else if (action == @':mask' || action == @':unmask') { |
| 893 |
#ifdef HAVE_SIGPROCMASK |
| 894 |
# ifdef ECL_THREADS |
| 895 |
/* When a process object is supplied, the changes take care |
| 896 |
* on the process structure and will only take effect when |
| 897 |
* the process is enabled. */ |
| 898 |
if (ecl_t_of(process) == t_process) { |
| 899 |
cl_env_ptr env = process->process.env; |
| 900 |
sigset_t *handled_set = (sigset_t *)env->default_sigmask; |
| 901 |
if (action == @':mask') { |
| 902 |
sigaddset(handled_set, code); |
| 903 |
} else { |
| 904 |
sigdelset(handled_set, code); |
| 905 |
} |
| 906 |
return ECL_T; |
| 907 |
} else { |
| 908 |
sigset_t handled_set; |
| 909 |
pthread_sigmask(SIG_SETMASK, NULL, &handled_set); |
| 910 |
if (action == @':mask') { |
| 911 |
sigaddset(&handled_set, code); |
| 912 |
} else { |
| 913 |
sigdelset(&handled_set, code); |
| 914 |
} |
| 915 |
pthread_sigmask(SIG_SETMASK, &handled_set, NULL); |
| 916 |
return ECL_T; |
| 917 |
} |
| 918 |
# else |
| 919 |
{ |
| 920 |
sigset_t handled_set; |
| 921 |
sigprocmask(SIG_SETMASK, NULL, &handled_set); |
| 922 |
if (action == @':mask') { |
| 923 |
sigaddset(&handled_set, code); |
| 924 |
} else { |
| 925 |
sigdelset(&handled_set, code); |
| 926 |
} |
| 927 |
sigprocmask(SIG_SETMASK, &handled_set, NULL); |
| 928 |
return ECL_T; |
| 929 |
} |
| 930 |
# endif /* !ECL_THREADS */ |
| 931 |
#else /* !HAVE_SIGPROCMASK */ |
| 932 |
return ECL_NIL; |
| 933 |
#endif /* !HAVE_SIGPROCMASK */ |
| 934 |
} else if (action == ECL_T || action == @':catch') { |
| 935 |
if (code == SIGSEGV) { |
| 936 |
mysignal(code, sigsegv_handler); |
| 937 |
} |
| 938 |
#ifdef SIGBUS |
| 939 |
else if (code == SIGBUS) { |
| 940 |
mysignal(code, sigsegv_handler); |
| 941 |
} |
| 942 |
#endif |
| 943 |
#ifdef SIGILL |
| 944 |
else if (code == SIGILL) { |
| 945 |
mysignal(SIGILL, evil_signal_handler); |
| 946 |
} |
| 947 |
#endif |
| 948 |
else { |
| 949 |
mysignal(code, non_evil_signal_handler); |
| 950 |
} |
| 951 |
return ECL_T; |
| 952 |
} else { |
| 953 |
FEerror("Unknown 2nd argument to EXT:CATCH-SIGNAL: ~A", 1, |
| 954 |
action); |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
cl_object |
| 959 |
si_get_signal_handler(cl_object code) |
| 960 |
{ |
| 961 |
const cl_env_ptr the_env = ecl_process_env(); |
| 962 |
cl_object handler = ecl_gethash_safe(code, cl_core.known_signals, OBJNULL); |
| 963 |
unlikely_if (handler == OBJNULL) { |
| 964 |
illegal_signal_code(code); |
| 965 |
} |
| 966 |
ecl_return0(the_env); |
| 967 |
} |
| 968 |
|
| 969 |
cl_object |
| 970 |
si_set_signal_handler(cl_object code, cl_object handler) |
| 971 |
{ |
| 972 |
const cl_env_ptr the_env = ecl_process_env(); |
| 973 |
cl_object action = ecl_gethash_safe(code, cl_core.known_signals, OBJNULL); |
| 974 |
unlikely_if (action == OBJNULL) { |
| 975 |
illegal_signal_code(code); |
| 976 |
} |
| 977 |
ecl_sethash(code, cl_core.known_signals, handler); |
| 978 |
si_catch_signal(2, code, ECL_T); |
| 979 |
ecl_return0(the_env); |
| 980 |
} |
| 981 |
|
| 982 |
@(defun ext::catch-signal (code flag &key process) |
| 983 |
@ |
| 984 |
{ |
| 985 |
const cl_env_ptr the_env = ecl_process_env(); |
| 986 |
int code_int; |
| 987 |
unlikely_if (ecl_gethash_safe(code, cl_core.known_signals, OBJNULL) == OBJNULL) { |
| 988 |
illegal_signal_code(code); |
| 989 |
} |
| 990 |
code_int = ecl_fixnum(code); |
| 991 |
#ifdef GBC_BOEHM |
| 992 |
# ifdef SIGSEGV |
| 993 |
unlikely_if ((code == ecl_make_fixnum(SIGSEGV)) && |
| 994 |
ecl_option_values[ECL_OPT_INCREMENTAL_GC]) |
| 995 |
FEerror("It is not allowed to change the behavior of SIGSEGV.", |
| 996 |
0); |
| 997 |
# endif |
| 998 |
# ifdef SIGBUS |
| 999 |
unlikely_if (code_int == SIGBUS) |
| 1000 |
FEerror("It is not allowed to change the behavior of SIGBUS.", |
| 1001 |
0); |
| 1002 |
# endif |
| 1003 |
#endif |
| 1004 |
#if defined(ECL_THREADS) && !defined(ECL_MS_WINDOWS_HOST) |
| 1005 |
unlikely_if (code_int == ecl_option_values[ECL_OPT_THREAD_INTERRUPT_SIGNAL]) { |
| 1006 |
FEerror("It is not allowed to change the behavior of signal ~D", 1, |
| 1007 |
code); |
| 1008 |
} |
| 1009 |
#endif |
| 1010 |
#ifdef SIGFPE |
| 1011 |
unlikely_if (code_int == SIGFPE) { |
| 1012 |
FEerror("The signal handler for SIGPFE cannot be uninstalled. Use SI:TRAP-FPE instead.", 0); |
| 1013 |
} |
| 1014 |
#endif |
| 1015 |
ecl_return1(the_env, do_catch_signal(code_int, flag, process)); |
| 1016 |
} |
| 1017 |
@) |
| 1018 |
|
| 1019 |
#ifdef ECL_THREADS |
| 1020 |
# ifdef ECL_WINDOWS_THREADS |
| 1021 |
static VOID CALLBACK |
| 1022 |
wakeup_function(ULONG_PTR foo) |
| 1023 |
{ |
| 1024 |
/* Write into the process environment to trigger an exception */ |
| 1025 |
cl_env_ptr env = ecl_process_env(); |
| 1026 |
volatile cl_index i = env->nvalues; |
| 1027 |
env->nvalues = i; |
| 1028 |
} |
| 1029 |
|
| 1030 |
static VOID CALLBACK |
| 1031 |
wakeup_noop(ULONG_PTR foo) |
| 1032 |
{ |
| 1033 |
} |
| 1034 |
# endif |
| 1035 |
|
| 1036 |
static bool |
| 1037 |
do_interrupt_thread(cl_object process) |
| 1038 |
{ |
| 1039 |
# ifdef ECL_WINDOWS_THREADS |
| 1040 |
# ifndef ECL_USE_GUARD_PAGE |
| 1041 |
# error "Cannot implement ecl_interrupt_process without guard pages" |
| 1042 |
# endif |
| 1043 |
HANDLE thread = process->process.thread; |
| 1044 |
CONTEXT context; |
| 1045 |
void *trap_address = process->process.env; |
| 1046 |
DWORD guard = PAGE_GUARD | PAGE_READWRITE; |
| 1047 |
int ok = 1; |
| 1048 |
if (SuspendThread(thread) == (DWORD)-1) { |
| 1049 |
FEwin32_error("Unable to suspend thread ~A", 1, |
| 1050 |
process); |
| 1051 |
ok = 0; |
| 1052 |
goto EXIT; |
| 1053 |
} |
| 1054 |
process->process.interrupt = ECL_T; |
| 1055 |
if (!VirtualProtect(process->process.env, |
| 1056 |
sizeof(struct cl_env_struct), |
| 1057 |
guard, |
| 1058 |
&guard)) |
| 1059 |
{ |
| 1060 |
FEwin32_error("Unable to protect memory from thread ~A", |
| 1061 |
1, process); |
| 1062 |
ok = 0; |
| 1063 |
} |
| 1064 |
RESUME: |
| 1065 |
if (!QueueUserAPC(wakeup_function, thread, 0)) { |
| 1066 |
FEwin32_error("Unable to queue APC call to thread ~A", |
| 1067 |
1, process); |
| 1068 |
ok = 0; |
| 1069 |
} |
| 1070 |
if (ResumeThread(thread) == (DWORD)-1) { |
| 1071 |
FEwin32_error("Unable to resume thread ~A", 1, |
| 1072 |
process); |
| 1073 |
ok = 0; |
| 1074 |
goto EXIT; |
| 1075 |
} |
| 1076 |
EXIT: |
| 1077 |
return ok; |
| 1078 |
# else |
| 1079 |
int signal = ecl_option_values[ECL_OPT_THREAD_INTERRUPT_SIGNAL]; |
| 1080 |
if (pthread_kill(process->process.thread, signal)) { |
| 1081 |
FElibc_error("Unable to interrupt process ~A", 1, |
| 1082 |
process); |
| 1083 |
} |
| 1084 |
return 1; |
| 1085 |
# endif |
| 1086 |
} |
| 1087 |
|
| 1088 |
void |
| 1089 |
ecl_interrupt_process(cl_object process, cl_object function) |
| 1090 |
{ |
| 1091 |
/* |
| 1092 |
* We first ensure that the process is active and running |
| 1093 |
* and past the initialization phase, where it has set up |
| 1094 |
* the environment. Then: |
| 1095 |
* - In Windows it sets up a trap in the stack, so that the |
| 1096 |
* uncaught exception handler can catch it and process it. |
| 1097 |
* - In POSIX systems it sends a user level interrupt to |
| 1098 |
* the thread, which then decides how to act. |
| 1099 |
* |
| 1100 |
* If FUNCTION is NIL, we just intend to wake up the process |
| 1101 |
* from some call to ecl_musleep() Queue the interrupt for any |
| 1102 |
* process stage that can potentially receive a signal */ |
| 1103 |
if (!Null(function) && |
| 1104 |
(process->process.phase >= ECL_PROCESS_BOOTING)) |
| 1105 |
{ |
| 1106 |
cl_env_ptr the_env = ecl_process_env(); |
| 1107 |
function = si_coerce_to_function(function); |
| 1108 |
/* queue_signal must be called with disabled |
| 1109 |
* interrupts for the current process */ |
| 1110 |
ecl_disable_interrupts_env(the_env); |
| 1111 |
queue_signal(process->process.env, function, 1); |
| 1112 |
ecl_enable_interrupts_env(the_env); |
| 1113 |
} |
| 1114 |
/* ... but only deliver if the process is still alive */ |
| 1115 |
if (process->process.phase == ECL_PROCESS_ACTIVE) |
| 1116 |
do_interrupt_thread(process); |
| 1117 |
} |
| 1118 |
|
| 1119 |
void |
| 1120 |
ecl_wakeup_process(cl_object process) |
| 1121 |
{ |
| 1122 |
# ifdef ECL_WINDOWS_THREADS |
| 1123 |
HANDLE thread = process->process.thread; |
| 1124 |
if (!QueueUserAPC(wakeup_noop, thread, 0)) { |
| 1125 |
FEwin32_error("Unable to queue APC call to thread ~A", |
| 1126 |
1, process); |
| 1127 |
} |
| 1128 |
# else |
| 1129 |
do_interrupt_thread(process); |
| 1130 |
# endif |
| 1131 |
} |
| 1132 |
#endif /* ECL_THREADS */ |
| 1133 |
|
| 1134 |
#ifdef ECL_WINDOWS_THREADS |
| 1135 |
static LPTOP_LEVEL_EXCEPTION_FILTER old_W32_exception_filter = NULL; |
| 1136 |
|
| 1137 |
LONG WINAPI |
| 1138 |
_ecl_w32_exception_filter(struct _EXCEPTION_POINTERS* ep) |
| 1139 |
{ |
| 1140 |
LONG excpt_result; |
| 1141 |
cl_env_ptr the_env = ecl_process_env(); |
| 1142 |
|
| 1143 |
excpt_result = EXCEPTION_CONTINUE_EXECUTION; |
| 1144 |
switch (ep->ExceptionRecord->ExceptionCode) |
| 1145 |
{ |
| 1146 |
/* Access to guard page */ |
| 1147 |
case STATUS_GUARD_PAGE_VIOLATION: { |
| 1148 |
cl_object process = the_env->own_process; |
| 1149 |
if (!Null(process->process.interrupt)) { |
| 1150 |
process->process.interrupt = ECL_NIL; |
| 1151 |
handle_all_queued_interrupt_safe(the_env); |
| 1152 |
} |
| 1153 |
return EXCEPTION_CONTINUE_EXECUTION; |
| 1154 |
} |
| 1155 |
/* Catch all arithmetic exceptions */ |
| 1156 |
case EXCEPTION_INT_DIVIDE_BY_ZERO: |
| 1157 |
feclearexcept(FE_ALL_EXCEPT); |
| 1158 |
handle_signal_now(@'division-by-zero'); |
| 1159 |
return EXCEPTION_CONTINUE_EXECUTION; |
| 1160 |
case EXCEPTION_INT_OVERFLOW: |
| 1161 |
feclearexcept(FE_ALL_EXCEPT); |
| 1162 |
handle_signal_now(@'arithmetic-error'); |
| 1163 |
return EXCEPTION_CONTINUE_EXECUTION; |
| 1164 |
case EXCEPTION_FLT_DIVIDE_BY_ZERO: |
| 1165 |
feclearexcept(FE_ALL_EXCEPT); |
| 1166 |
handle_signal_now(@'floating-point-overflow'); |
| 1167 |
return EXCEPTION_CONTINUE_EXECUTION; |
| 1168 |
case EXCEPTION_FLT_OVERFLOW: |
| 1169 |
feclearexcept(FE_ALL_EXCEPT); |
| 1170 |
handle_signal_now(@'floating-point-overflow'); |
| 1171 |
return EXCEPTION_CONTINUE_EXECUTION; |
| 1172 |
case EXCEPTION_FLT_UNDERFLOW: |
| 1173 |
feclearexcept(FE_ALL_EXCEPT); |
| 1174 |
handle_signal_now(@'floating-point-underflow'); |
| 1175 |
return EXCEPTION_CONTINUE_EXECUTION; |
| 1176 |
case EXCEPTION_FLT_INEXACT_RESULT: |
| 1177 |
feclearexcept(FE_ALL_EXCEPT); |
| 1178 |
handle_signal_now(@'floating-point-inexact'); |
| 1179 |
return EXCEPTION_CONTINUE_EXECUTION; |
| 1180 |
case EXCEPTION_FLT_DENORMAL_OPERAND: |
| 1181 |
case EXCEPTION_FLT_INVALID_OPERATION: |
| 1182 |
feclearexcept(FE_ALL_EXCEPT); |
| 1183 |
handle_signal_now(@'floating-point-invalid-operation'); |
| 1184 |
return EXCEPTION_CONTINUE_EXECUTION; |
| 1185 |
case EXCEPTION_FLT_STACK_CHECK: |
| 1186 |
handle_signal_now(@'arithmetic-error'); |
| 1187 |
return EXCEPTION_CONTINUE_EXECUTION; |
| 1188 |
/* Catch segmentation fault */ |
| 1189 |
case EXCEPTION_ACCESS_VIOLATION: |
| 1190 |
handle_signal_now(@'ext::segmentation-violation'); |
| 1191 |
return EXCEPTION_CONTINUE_EXECUTION; |
| 1192 |
/* Catch illegal instruction */ |
| 1193 |
case EXCEPTION_ILLEGAL_INSTRUCTION: |
| 1194 |
handle_signal_now(@'ext::illegal-instruction'); |
| 1195 |
return EXCEPTION_CONTINUE_EXECUTION; |
| 1196 |
/* Do not catch anything else */ |
| 1197 |
default: |
| 1198 |
excpt_result = EXCEPTION_CONTINUE_SEARCH; |
| 1199 |
break; |
| 1200 |
} |
| 1201 |
if (old_W32_exception_filter) |
| 1202 |
return old_W32_exception_filter(ep); |
| 1203 |
return excpt_result; |
| 1204 |
} |
| 1205 |
|
| 1206 |
static cl_object |
| 1207 |
W32_handle_in_new_thread(cl_object signal_code) |
| 1208 |
{ |
| 1209 |
int outside_ecl = ecl_import_current_thread(@'si::handle-signal', ECL_NIL); |
| 1210 |
mp_process_run_function(3, @'si::handle-signal', |
| 1211 |
@'si::handle-signal', |
| 1212 |
signal_code); |
| 1213 |
if (outside_ecl) ecl_release_current_thread(); |
| 1214 |
} |
| 1215 |
|
| 1216 |
BOOL WINAPI W32_console_ctrl_handler(DWORD type) |
| 1217 |
{ |
| 1218 |
switch (type) { |
| 1219 |
case CTRL_C_EVENT: |
| 1220 |
case CTRL_BREAK_EVENT: { |
| 1221 |
cl_object function = |
| 1222 |
ECL_SYM_FUN(@'si::terminal-interrupt'); |
| 1223 |
if (function) |
| 1224 |
W32_handle_in_new_thread(function); |
| 1225 |
return TRUE; |
| 1226 |
} |
| 1227 |
case CTRL_CLOSE_EVENT: |
| 1228 |
case CTRL_LOGOFF_EVENT: |
| 1229 |
case CTRL_SHUTDOWN_EVENT: { |
| 1230 |
cl_object function = |
| 1231 |
ECL_SYM_FUN(@'ext::quit'); |
| 1232 |
if (function) |
| 1233 |
W32_handle_in_new_thread(function); |
| 1234 |
return TRUE; |
| 1235 |
} |
| 1236 |
default: |
| 1237 |
return FALSE; |
| 1238 |
} |
| 1239 |
} |
| 1240 |
#endif /* ECL_WINDOWS_THREADS */ |
| 1241 |
|
| 1242 |
cl_object |
| 1243 |
si_trap_fpe(cl_object condition, cl_object flag) |
| 1244 |
{ |
| 1245 |
cl_env_ptr the_env = ecl_process_env(); |
| 1246 |
const int all = FE_ALL_EXCEPT; |
| 1247 |
int bits = 0; |
| 1248 |
if (condition == @'last') { |
| 1249 |
bits = the_env->trap_fpe_bits; |
| 1250 |
} else { |
| 1251 |
if (condition == ECL_T) |
| 1252 |
bits = FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INVALID; |
| 1253 |
else if (condition == @'division-by-zero') |
| 1254 |
bits = FE_DIVBYZERO; |
| 1255 |
else if (condition == @'floating-point-overflow') |
| 1256 |
bits = FE_OVERFLOW; |
| 1257 |
else if (condition == @'floating-point-underflow') |
| 1258 |
bits = FE_UNDERFLOW; |
| 1259 |
else if (condition == @'floating-point-invalid-operation') |
| 1260 |
bits = FE_INVALID; |
| 1261 |
else if (condition == @'floating-point-inexact') |
| 1262 |
bits = FE_INEXACT; |
| 1263 |
else if (ECL_FIXNUMP(condition)) |
| 1264 |
bits = ecl_fixnum(condition) & all; |
| 1265 |
else |
| 1266 |
FEerror("Unknown condition to EXT:TRAP-FPE: ~s", 1, condition); |
| 1267 |
|
| 1268 |
if (flag == ECL_NIL) { |
| 1269 |
bits = the_env->trap_fpe_bits & ~bits; |
| 1270 |
} else { |
| 1271 |
bits = the_env->trap_fpe_bits | bits; |
| 1272 |
} |
| 1273 |
} |
| 1274 |
#if !defined(ECL_AVOID_FPE_H) |
| 1275 |
# ifdef HAVE_FENV_H |
| 1276 |
feclearexcept(all); |
| 1277 |
# endif |
| 1278 |
# if defined(ECL_MS_WINDOWS_HOST) |
| 1279 |
_fpreset(); |
| 1280 |
# endif |
| 1281 |
# ifdef HAVE_FEENABLEEXCEPT |
| 1282 |
fedisableexcept(all & ~bits); |
| 1283 |
feenableexcept(all & bits); |
| 1284 |
# endif |
| 1285 |
#endif |
| 1286 |
the_env->trap_fpe_bits = bits; |
| 1287 |
ecl_return1(the_env, ecl_make_fixnum(bits)); |
| 1288 |
} |
| 1289 |
|
| 1290 |
/* |
| 1291 |
* In this code we decide whether to install a process-wide signal |
| 1292 |
* handler for each of the asynchronous signals (SIGINT, SIGTERM, |
| 1293 |
* SIGCHLD...) or we block the signal and let the background thread |
| 1294 |
* detect and process them. |
| 1295 |
*/ |
| 1296 |
static void |
| 1297 |
install_asynchronous_signal_handlers() |
| 1298 |
{ |
| 1299 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 1300 |
# define async_handler(signal,handler,mask) |
| 1301 |
#else |
| 1302 |
# if defined(ECL_THREADS) && defined(HAVE_SIGPROCMASK) |
| 1303 |
# define async_handler(signal,handler,mask) { \ |
| 1304 |
if (ecl_option_values[ECL_OPT_SIGNAL_HANDLING_THREAD]) { \ |
| 1305 |
mysignal(signal, deferred_signal_handler); \ |
| 1306 |
} else { \ |
| 1307 |
mysignal(signal,handler); \ |
| 1308 |
}} |
| 1309 |
# else |
| 1310 |
# define async_handler(signal,handler,mask) \ |
| 1311 |
mysignal(signal,handler) |
| 1312 |
# endif |
| 1313 |
#endif |
| 1314 |
#ifdef HAVE_SIGPROCMASK |
| 1315 |
sigset_t *sigmask = cl_core.default_sigmask = &main_thread_sigmask; |
| 1316 |
cl_core.default_sigmask_bytes = sizeof(sigset_t); |
| 1317 |
# ifdef ECL_THREADS |
| 1318 |
pthread_sigmask(SIG_SETMASK, NULL, sigmask); |
| 1319 |
# else |
| 1320 |
sigprocmask(SIG_SETMASK, NULL, sigmask); |
| 1321 |
# endif |
| 1322 |
#endif |
| 1323 |
#if defined(ECL_THREADS) && defined(HAVE_SIGPROCMASK) |
| 1324 |
ecl_mutex_init(&signal_thread_lock, TRUE); |
| 1325 |
#endif |
| 1326 |
#ifdef SIGINT |
| 1327 |
if (ecl_option_values[ECL_OPT_TRAP_SIGINT]) { |
| 1328 |
async_handler(SIGINT, non_evil_signal_handler, sigmask); |
| 1329 |
} |
| 1330 |
#endif |
| 1331 |
#ifdef HAVE_SIGPROCMASK |
| 1332 |
# if defined(ECL_THREADS) |
| 1333 |
pthread_sigmask(SIG_SETMASK, sigmask, NULL); |
| 1334 |
# else |
| 1335 |
sigprocmask(SIG_SETMASK, sigmask, NULL); |
| 1336 |
# endif |
| 1337 |
#endif |
| 1338 |
#ifdef ECL_WINDOWS_THREADS |
| 1339 |
old_W32_exception_filter = |
| 1340 |
SetUnhandledExceptionFilter(_ecl_w32_exception_filter); |
| 1341 |
if (ecl_option_values[ECL_OPT_TRAP_SIGINT]) { |
| 1342 |
SetConsoleCtrlHandler(W32_console_ctrl_handler, TRUE); |
| 1343 |
} |
| 1344 |
#endif |
| 1345 |
#undef async_handler |
| 1346 |
} |
| 1347 |
|
| 1348 |
/* |
| 1349 |
* In POSIX systems we may set up a background thread that detects |
| 1350 |
* synchronous signals and spawns a new thread to handle each of them. |
| 1351 |
*/ |
| 1352 |
static void |
| 1353 |
install_signal_handling_thread() |
| 1354 |
{ |
| 1355 |
#if defined(ECL_THREADS) && defined(HAVE_SIGPROCMASK) |
| 1356 |
ecl_process_env()->default_sigmask = &main_thread_sigmask; |
| 1357 |
if (ecl_option_values[ECL_OPT_SIGNAL_HANDLING_THREAD]) { |
| 1358 |
cl_object fun = |
| 1359 |
ecl_make_cfun((cl_objectfn_fixed) |
| 1360 |
asynchronous_signal_servicing_thread, |
| 1361 |
@'si::signal-servicing', |
| 1362 |
ECL_NIL, |
| 1363 |
0); |
| 1364 |
cl_object process = |
| 1365 |
signal_thread_process = |
| 1366 |
mp_process_run_function_wait(2, |
| 1367 |
@'si::signal-servicing', |
| 1368 |
fun); |
| 1369 |
if (Null(process)) { |
| 1370 |
ecl_internal_error("Unable to create signal " |
| 1371 |
"servicing thread"); |
| 1372 |
} |
| 1373 |
} |
| 1374 |
#endif |
| 1375 |
} |
| 1376 |
|
| 1377 |
/* |
| 1378 |
* This routine sets up handlers for all exceptions, such as access to |
| 1379 |
* restricted regions of memory. They have to be set up before we call |
| 1380 |
* init_GC(). |
| 1381 |
*/ |
| 1382 |
static void |
| 1383 |
install_synchronous_signal_handlers() |
| 1384 |
{ |
| 1385 |
#ifdef SIGBUS |
| 1386 |
if (ecl_option_values[ECL_OPT_TRAP_SIGBUS]) { |
| 1387 |
do_catch_signal(SIGBUS, ECL_T, ECL_NIL); |
| 1388 |
} |
| 1389 |
#endif |
| 1390 |
#ifdef SIGSEGV |
| 1391 |
if (ecl_option_values[ECL_OPT_TRAP_SIGSEGV]) { |
| 1392 |
do_catch_signal(SIGSEGV, ECL_T, ECL_NIL); |
| 1393 |
} |
| 1394 |
#endif |
| 1395 |
#ifdef SIGPIPE |
| 1396 |
if (ecl_option_values[ECL_OPT_TRAP_SIGPIPE]) { |
| 1397 |
do_catch_signal(SIGPIPE, ECL_T, ECL_NIL); |
| 1398 |
} |
| 1399 |
#endif |
| 1400 |
#ifdef SIGILL |
| 1401 |
if (ecl_option_values[ECL_OPT_TRAP_SIGILL]) { |
| 1402 |
do_catch_signal(SIGILL, ECL_T, ECL_NIL); |
| 1403 |
} |
| 1404 |
#endif |
| 1405 |
/* In order to implement MP:INTERRUPT-PROCESS, MP:PROCESS-KILL |
| 1406 |
* and the like, we use signals. This sets up a synchronous |
| 1407 |
* signal handler for that particular signal. |
| 1408 |
*/ |
| 1409 |
#ifdef SIGRTMIN |
| 1410 |
# define DEFAULT_THREAD_INTERRUPT_SIGNAL SIGRTMIN + 2 |
| •1411 |
#elif defined(__GNU__) |
| 1412 |
# define DEFAULT_THREAD_INTERRUPT_SIGNAL SIGINFO |
| 1413 |
#else |
| 1414 |
# define DEFAULT_THREAD_INTERRUPT_SIGNAL SIGUSR1 |
| 1415 |
#endif |
| 1416 |
#if defined(ECL_THREADS) && !defined(ECL_MS_WINDOWS_HOST) |
| 1417 |
if (ecl_option_values[ECL_OPT_TRAP_INTERRUPT_SIGNAL]) { |
| 1418 |
int signal = ecl_option_values[ECL_OPT_THREAD_INTERRUPT_SIGNAL]; |
| 1419 |
if (signal == 0) { |
| 1420 |
signal = DEFAULT_THREAD_INTERRUPT_SIGNAL; |
| 1421 |
ecl_set_option(ECL_OPT_THREAD_INTERRUPT_SIGNAL, |
| 1422 |
signal); |
| 1423 |
} |
| 1424 |
mysignal(signal, process_interrupt_handler); |
| 1425 |
#ifdef HAVE_SIGPROCMASK |
| 1426 |
sigdelset(&main_thread_sigmask, signal); |
| 1427 |
pthread_sigmask(SIG_SETMASK, &main_thread_sigmask, NULL); |
| 1428 |
#endif |
| 1429 |
} |
| 1430 |
#endif |
| 1431 |
} |
| 1432 |
|
| 1433 |
/* |
| 1434 |
* This routine sets up handlers for floating point exceptions. We |
| 1435 |
* cannot do it earlier because it requires the memory allocator to |
| 1436 |
* be set up. |
| 1437 |
*/ |
| 1438 |
static void |
| 1439 |
install_fpe_signal_handlers() |
| 1440 |
{ |
| 1441 |
#ifdef SIGFPE |
| 1442 |
if (ecl_option_values[ECL_OPT_TRAP_SIGFPE]) { |
| 1443 |
mysignal(SIGFPE, fpe_signal_handler); |
| 1444 |
si_trap_fpe(ECL_T, ECL_T); |
| 1445 |
|
| 1446 |
/* Don't trap underflows */ |
| 1447 |
si_trap_fpe(@'floating-point-underflow', ECL_NIL); |
| 1448 |
|
| 1449 |
# if 0 && defined(ECL_IEEE_FP) |
| 1450 |
/* By default deactivate errors and accept denormals |
| 1451 |
* in floating point computations. */ |
| 1452 |
si_trap_fpe(@'floating-point-invalid-operation', ECL_NIL); |
| 1453 |
si_trap_fpe(@'division-by-zero', ECL_NIL); |
| 1454 |
si_trap_fpe(@'floating-point-overflow', ECL_NIL); |
| 1455 |
# endif |
| 1456 |
} |
| 1457 |
#endif |
| 1458 |
} |
| 1459 |
|
| 1460 |
/* |
| 1461 |
* Create one Common Lisp constant for each signal that we know, |
| 1462 |
* such as +SIGINT+ for SIGINT, etc. |
| 1463 |
*/ |
| 1464 |
static void |
| 1465 |
add_one_signal(cl_object hash_table, int signal, cl_object name, cl_object handler) |
| 1466 |
{ |
| 1467 |
cl_object code = ecl_make_fixnum(signal); |
| 1468 |
cl_export2(name, cl_core.ext_package); |
| 1469 |
si_Xmake_constant(name, code); |
| 1470 |
ecl_sethash(code, hash_table, handler); |
| 1471 |
} |
| 1472 |
|
| 1473 |
static void |
| 1474 |
create_signal_code_constants() |
| 1475 |
{ |
| 1476 |
cl_object hash = |
| 1477 |
cl_core.known_signals = |
| 1478 |
cl__make_hash_table(@'eql', ecl_make_fixnum(128), |
| 1479 |
cl_core.rehash_size, |
| 1480 |
cl_core.rehash_threshold); |
| 1481 |
int i; |
| 1482 |
for (i = 0; known_signals[i].code >= 0; i++) { |
| 1483 |
add_one_signal(hash, known_signals[i].code, |
| 1484 |
_ecl_intern(known_signals[i].name, |
| 1485 |
cl_core.ext_package), |
| 1486 |
known_signals[i].handler); |
| 1487 |
} |
| 1488 |
#ifdef SIGRTMIN |
| 1489 |
for (i = SIGRTMIN; i <= SIGRTMAX; i++) { |
| 1490 |
int intern_flag[1]; |
| 1491 |
char buffer[64]; |
| 1492 |
cl_object name; |
| 1493 |
sprintf(buffer, "+SIGRT%d+", i-SIGRTMIN); |
| 1494 |
name = ecl_intern(ecl_make_simple_base_string(buffer,-1), |
| 1495 |
cl_core.ext_package, |
| 1496 |
intern_flag); |
| 1497 |
add_one_signal(hash, i, name, ECL_NIL); |
| 1498 |
} |
| 1499 |
add_one_signal(hash, SIGRTMIN, |
| 1500 |
_ecl_intern("+SIGRTMIN+", cl_core.ext_package), |
| 1501 |
ECL_NIL); |
| 1502 |
add_one_signal(hash, SIGRTMAX, |
| 1503 |
_ecl_intern("+SIGRTMAX+", cl_core.ext_package), |
| 1504 |
ECL_NIL); |
| 1505 |
#endif |
| 1506 |
} |
| 1507 |
|
| 1508 |
void |
| 1509 |
init_unixint(int pass) |
| 1510 |
{ |
| 1511 |
if (pass == 0) { |
| 1512 |
install_asynchronous_signal_handlers(); |
| 1513 |
install_synchronous_signal_handlers(); |
| 1514 |
} else { |
| 1515 |
create_signal_code_constants(); |
| 1516 |
install_fpe_signal_handlers(); |
| 1517 |
install_signal_handling_thread(); |
| 1518 |
ECL_SET(@'ext::*interrupts-enabled*', ECL_T); |
| 1519 |
ecl_process_env()->disable_interrupts = 0; |
| 1520 |
} |
| 1521 |
} |