1 /* -----------------------------------------------------------------------------
2 * $Id: Signals.c,v 1.26 2002/07/17 09:21:51 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
6 * Signal processing / handling.
8 * ---------------------------------------------------------------------------*/
10 /* This is non-Posix-compliant.
11 #include "PosixSource.h"
19 #include "StablePriv.h"
21 #ifdef alpha_TARGET_ARCH
22 # include <machine/fpu.h>
35 #ifndef mingw32_TARGET_OS
39 /* SUP: The type of handlers is a little bit, well, doubtful... */
40 static StgInt *handlers = NULL; /* Dynamically grown array of signal handlers */
41 static StgInt nHandlers = 0; /* Size of handlers array */
43 static nat n_haskell_handlers = 0;
45 #define N_PENDING_HANDLERS 16
47 StgPtr pending_handler_buf[N_PENDING_HANDLERS];
48 StgPtr *next_pending_handler = pending_handler_buf;
52 /* -----------------------------------------------------------------------------
53 * Allocate/resize the table of signal handlers.
54 * -------------------------------------------------------------------------- */
65 handlers = (StgInt *) malloc((sig + 1) * sizeof(StgInt));
67 handlers = (StgInt *) realloc(handlers, (sig + 1) * sizeof(StgInt));
69 if (handlers == NULL) {
70 // don't fflush(stdout); WORKAROUND bug in Linux glibc
71 barf("VM exhausted (in more_handlers)");
73 for(i = nHandlers; i <= sig; i++)
74 // Fill in the new slots with default actions
75 handlers[i] = STG_SIG_DFL;
80 /* -----------------------------------------------------------------------------
83 * It seems that shells tend to put stdin back into blocking mode
84 * following a suspend/resume of the process. Here we arrange to put
85 * it back into non-blocking mode. We don't do anything to
86 * stdout/stderr because these handles don't get put into non-blocking
87 * mode at all - see the comments on stdout/stderr in PrelHandle.hsc.
88 * -------------------------------------------------------------------------- */
91 cont_handler(int sig STG_UNUSED)
96 /* -----------------------------------------------------------------------------
97 * Low-level signal handler
99 * Places the requested handler on a stack of pending handlers to be
100 * started up at the next context switch.
101 * -------------------------------------------------------------------------- */
104 generic_handler(int sig)
108 /* Can't call allocate from here. Probably can't call malloc
109 either. However, we have to schedule a new thread somehow.
111 It's probably ok to request a context switch and allow the
112 scheduler to start the handler thread, but how do we
113 communicate this to the scheduler?
115 We need some kind of locking, but with low overhead (i.e. no
116 blocking signals every time around the scheduler).
118 Signal Handlers are atomic (i.e. they can't be interrupted), and
119 we can make use of this. We just need to make sure the
120 critical section of the scheduler can't be interrupted - the
121 only way to do this is to block signals. However, we can lower
122 the overhead by only blocking signals when there are any
123 handlers to run, i.e. the set of pending handlers is
127 /* We use a stack to store the pending signals. We can't
128 dynamically grow this since we can't allocate any memory from
129 within a signal handler.
131 Hence unfortunately we have to bomb out if the buffer
132 overflows. It might be acceptable to carry on in certain
133 circumstances, depending on the signal.
136 *next_pending_handler++ = deRefStablePtr((StgStablePtr)handlers[sig]);
139 if (next_pending_handler == &pending_handler_buf[N_PENDING_HANDLERS]) {
140 barf("too many pending signals");
143 // re-establish the signal handler, and carry on
144 sigemptyset(&signals);
145 sigaddset(&signals, sig);
146 sigprocmask(SIG_UNBLOCK, &signals, NULL);
148 // *always* do the SIGCONT handler, even if the user overrides it.
149 if (sig == SIGCONT) {
156 /* -----------------------------------------------------------------------------
157 * Blocking/Unblocking of the user signals
158 * -------------------------------------------------------------------------- */
160 static sigset_t userSignals;
161 static sigset_t savedSignals;
164 initUserSignals(void)
166 sigemptyset(&userSignals);
170 blockUserSignals(void)
172 sigprocmask(SIG_SETMASK, &userSignals, &savedSignals);
176 unblockUserSignals(void)
178 sigprocmask(SIG_SETMASK, &savedSignals, NULL);
182 anyUserHandlers(void)
184 return n_haskell_handlers != 0;
188 awaitUserSignals(void)
190 while (!signals_pending() && !interrupted) {
195 /* -----------------------------------------------------------------------------
196 * Install a Haskell signal handler.
197 * -------------------------------------------------------------------------- */
200 stg_sig_install(StgInt sig, StgInt spi, StgStablePtr handler, void *mask)
203 struct sigaction action;
206 // Block the signal until we figure out what to do
207 // Count on this to fail if the signal number is invalid
208 if (sig < 0 || sigemptyset(&signals) ||
209 sigaddset(&signals, sig) || sigprocmask(SIG_BLOCK, &signals, NULL)) {
215 previous_spi = handlers[sig];
219 handlers[sig] = STG_SIG_IGN;
220 sigdelset(&userSignals, sig);
221 action.sa_handler = SIG_IGN;
225 handlers[sig] = STG_SIG_DFL;
226 sigdelset(&userSignals, sig);
227 action.sa_handler = SIG_DFL;
231 handlers[sig] = (StgInt)handler;
232 sigaddset(&userSignals, sig);
233 action.sa_handler = generic_handler;
234 n_haskell_handlers++;
238 barf("stg_sig_install: bad spi");
242 action.sa_mask = *(sigset_t *)mask;
244 sigemptyset(&action.sa_mask);
246 action.sa_flags = sig == SIGCHLD && nocldstop ? SA_NOCLDSTOP : 0;
248 if (sigaction(sig, &action, NULL) ||
249 sigprocmask(SIG_UNBLOCK, &signals, NULL))
251 // need to return an error code, so avoid a stable pointer leak
252 // by freeing the previous handler if there was one.
253 if (previous_spi >= 0) {
254 freeStablePtr(stgCast(StgStablePtr,handlers[sig]));
255 n_haskell_handlers--;
263 /* -----------------------------------------------------------------------------
264 * Creating new threads for the pending signal handlers.
265 * -------------------------------------------------------------------------- */
267 startSignalHandlers(void)
271 while (next_pending_handler != pending_handler_buf) {
273 next_pending_handler--;
276 createIOThread(RtsFlags.GcFlags.initialStkSize,
277 (StgClosure *) *next_pending_handler));
280 unblockUserSignals();
285 stg_sig_install(StgInt sig, StgInt spi, StgStablePtr handler, sigset_t *mask)
287 // don't fflush(stdout); WORKAROUND bug in Linux glibc
288 barf("no signal handling support in a parallel implementation");
292 startSignalHandlers(void)
297 /* -----------------------------------------------------------------------------
300 * We like to shutdown nicely after receiving a SIGINT, write out the
301 * stats, write profiling info, close open files and flush buffers etc.
302 * -------------------------------------------------------------------------- */
304 pthread_t startup_guy;
308 shutdown_handler(int sig STG_UNUSED)
311 // if I'm a worker thread, send this signal to the guy who
312 // originally called startupHaskell(). Since we're handling
313 // the signal, it won't be a "send to all threads" type of signal
314 // (according to the POSIX threads spec).
315 if (pthread_self() != startup_guy) {
316 pthread_kill(startup_guy, sig);
321 // If we're already trying to interrupt the RTS, terminate with
322 // extreme prejudice. So the first ^C tries to exit the program
323 // cleanly, and the second one just kills it.
325 exit(EXIT_INTERRUPTED);
331 /* -----------------------------------------------------------------------------
332 * Install default signal handlers.
334 * The RTS installs a default signal handler for catching
335 * SIGINT, so that we can perform an orderly shutdown.
337 * Haskell code may install their own SIGINT handler, which is
338 * fine, provided they're so kind as to put back the old one
339 * when they de-install.
341 * In addition to handling SIGINT, the RTS also handles SIGFPE
342 * by ignoring it. Apparently IEEE requires floating-point
343 * exceptions to be ignored by default, but alpha-dec-osf3
344 * doesn't seem to do so.
345 * -------------------------------------------------------------------------- */
347 initDefaultHandlers()
349 struct sigaction action,oact;
352 startup_guy = pthread_self();
355 // install the SIGINT handler
356 action.sa_handler = shutdown_handler;
357 sigemptyset(&action.sa_mask);
359 if (sigaction(SIGINT, &action, &oact) != 0) {
360 prog_belch("warning: failed to install SIGINT handler");
363 #ifndef cygwin32_TARGET_OS
364 siginterrupt(SIGINT, 1); // isn't this the default? --SDM
367 // install the SIGCONT handler
368 action.sa_handler = cont_handler;
369 sigemptyset(&action.sa_mask);
371 if (sigaction(SIGCONT, &action, &oact) != 0) {
372 prog_belch("warning: failed to install SIGCONT handler");
375 // install the SIGFPE handler
377 // In addition to handling SIGINT, also handle SIGFPE by ignoring it.
378 // Apparently IEEE requires floating-point exceptions to be ignored by
379 // default, but alpha-dec-osf3 doesn't seem to do so.
381 // Commented out by SDM 2/7/2002: this causes an infinite loop on
382 // some architectures when an integer division by zero occurs: we
383 // don't recover from the floating point exception, and the
384 // program just generates another one immediately.
386 action.sa_handler = SIG_IGN;
387 sigemptyset(&action.sa_mask);
389 if (sigaction(SIGFPE, &action, &oact) != 0) {
390 prog_belch("warning: failed to install SIGFPE handler");
394 #ifdef alpha_TARGET_ARCH
395 ieee_set_fp_control(0);
399 #endif /*! mingw32_TARGET_OS */