remove old #ifdef SMP bits
[ghc-hetmet.git] / rts / posix / Signals.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2005
4  *
5  * Signal processing / handling.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 /* This is non-Posix-compliant.
10    #include "PosixSource.h" 
11 */
12 #include "Rts.h"
13 #include "SchedAPI.h"
14 #include "Schedule.h"
15 #include "RtsSignals.h"
16 #include "posix/Signals.h"
17 #include "RtsUtils.h"
18 #include "RtsFlags.h"
19 #include "Prelude.h"
20 #include "ThrIOManager.h"
21
22 #ifdef alpha_HOST_ARCH
23 # if defined(linux_HOST_OS)
24 #  include <asm/fpu.h>
25 # else
26 #  include <machine/fpu.h>
27 # endif
28 #endif
29
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33
34 #ifdef HAVE_SIGNAL_H
35 # include <signal.h>
36 #endif
37
38 #include <stdlib.h>
39
40 /* This curious flag is provided for the benefit of the Haskell binding
41  * to POSIX.1 to control whether or not to include SA_NOCLDSTOP when
42  * installing a SIGCHLD handler. 
43  */
44 HsInt nocldstop = 0;
45
46 /* -----------------------------------------------------------------------------
47  * The table of signal handlers
48  * -------------------------------------------------------------------------- */
49
50 #if defined(RTS_USER_SIGNALS)
51
52 /* SUP: The type of handlers is a little bit, well, doubtful... */
53 StgInt *signal_handlers = NULL; /* Dynamically grown array of signal handlers */
54 static StgInt nHandlers = 0;    /* Size of handlers array */
55
56 static nat n_haskell_handlers = 0;
57
58 /* -----------------------------------------------------------------------------
59  * Allocate/resize the table of signal handlers.
60  * -------------------------------------------------------------------------- */
61
62 static void
63 more_handlers(I_ sig)
64 {
65     StgInt i;
66
67     if (sig < nHandlers)
68         return;
69
70     if (signal_handlers == NULL)
71         signal_handlers = (StgInt *)stgMallocBytes((sig + 1) * sizeof(StgInt), "more_handlers");
72     else
73         signal_handlers = (StgInt *)stgReallocBytes(signal_handlers, (sig + 1) * sizeof(StgInt), "more_handlers");
74
75     for(i = nHandlers; i <= sig; i++)
76         // Fill in the new slots with default actions
77         signal_handlers[i] = STG_SIG_DFL;
78
79     nHandlers = sig + 1;
80 }
81
82 /* -----------------------------------------------------------------------------
83  * Pending Handlers
84  *
85  * The mechanism for starting handlers differs between the threaded
86  * (THREADED_RTS) and non-threaded versions of the RTS.
87  *
88  * When the RTS is single-threaded, we just write the pending signal
89  * handlers into a buffer, and start a thread for each one in the
90  * scheduler loop.
91  *
92  * When THREADED_RTS, the problem is that signals might be
93  * delivered to multiple threads, so we would need to synchronise
94  * access to pending_handler_buf somehow.  Using thread
95  * synchronisation from a signal handler isn't possible in general
96  * (some OSs support it, eg. MacOS X, but not all).  So instead:
97  *
98  *   - the signal handler writes the signal number into the pipe
99  *     managed by the IO manager thread (see GHC.Conc).
100  *   - the IO manager picks up the signal number and calls
101  *     startSignalHandler() to start the thread.
102  *
103  * This also has the nice property that we don't need to arrange to
104  * wake up a worker task to start the signal handler: the IO manager
105  * wakes up when we write into the pipe.
106  *
107  * -------------------------------------------------------------------------- */
108
109 // Here's the pipe into which we will send our signals
110 static int io_manager_pipe = -1;
111
112 #define IO_MANAGER_WAKEUP 0xff
113 #define IO_MANAGER_DIE    0xfe
114
115 void
116 setIOManagerPipe (int fd)
117 {
118     // only called when THREADED_RTS, but unconditionally
119     // compiled here because GHC.Conc depends on it.
120     io_manager_pipe = fd;
121 }
122
123 #if defined(THREADED_RTS)
124 void
125 ioManagerWakeup (void)
126 {
127     // Wake up the IO Manager thread by sending a byte down its pipe
128     if (io_manager_pipe >= 0) {
129         StgWord8 byte = (StgWord8)IO_MANAGER_WAKEUP;
130         write(io_manager_pipe, &byte, 1);
131     }
132 }
133
134 void
135 ioManagerDie (void)
136 {
137     // Ask the IO Manager thread to exit
138     if (io_manager_pipe >= 0) {
139         StgWord8 byte = (StgWord8)IO_MANAGER_DIE;
140         write(io_manager_pipe, &byte, 1);
141     }
142 }
143
144 void
145 ioManagerStart (void)
146 {
147     // Make sure the IO manager thread is running
148     Capability *cap;
149     if (io_manager_pipe < 0) {
150         cap = rts_lock();
151         rts_evalIO(cap,&base_GHCziConc_ensureIOManagerIsRunning_closure,NULL);
152         rts_unlock(cap);
153     }
154 }
155 #endif
156
157 #if !defined(THREADED_RTS)
158
159 #define N_PENDING_HANDLERS 16
160
161 StgPtr pending_handler_buf[N_PENDING_HANDLERS];
162 StgPtr *next_pending_handler = pending_handler_buf;
163
164 #endif /* THREADED_RTS */
165
166 /* -----------------------------------------------------------------------------
167  * Low-level signal handler
168  *
169  * Places the requested handler on a stack of pending handlers to be
170  * started up at the next context switch.
171  * -------------------------------------------------------------------------- */
172
173 static void
174 generic_handler(int sig)
175 {
176     sigset_t signals;
177
178 #if defined(THREADED_RTS)
179
180     if (io_manager_pipe != -1)
181     {
182         // Write the signal number into the pipe as a single byte.  We
183         // hope that signals fit into a byte...
184         StgWord8 csig = (StgWord8)sig;
185         write(io_manager_pipe, &csig, 1);
186     }
187     // If the IO manager hasn't told us what the FD of the write end
188     // of its pipe is, there's not much we can do here, so just ignore
189     // the signal..
190
191 #else /* not THREADED_RTS */
192
193     /* Can't call allocate from here.  Probably can't call malloc
194        either.  However, we have to schedule a new thread somehow.
195
196        It's probably ok to request a context switch and allow the
197        scheduler to  start the handler thread, but how do we
198        communicate this to the scheduler?
199
200        We need some kind of locking, but with low overhead (i.e. no
201        blocking signals every time around the scheduler).
202        
203        Signal Handlers are atomic (i.e. they can't be interrupted), and
204        we can make use of this.  We just need to make sure the
205        critical section of the scheduler can't be interrupted - the
206        only way to do this is to block signals.  However, we can lower
207        the overhead by only blocking signals when there are any
208        handlers to run, i.e. the set of pending handlers is
209        non-empty.
210     */
211        
212     /* We use a stack to store the pending signals.  We can't
213        dynamically grow this since we can't allocate any memory from
214        within a signal handler.
215
216        Hence unfortunately we have to bomb out if the buffer
217        overflows.  It might be acceptable to carry on in certain
218        circumstances, depending on the signal.  
219     */
220
221     *next_pending_handler++ = deRefStablePtr((StgStablePtr)signal_handlers[sig]);
222
223     // stack full?
224     if (next_pending_handler == &pending_handler_buf[N_PENDING_HANDLERS]) {
225         errorBelch("too many pending signals");
226         stg_exit(EXIT_FAILURE);
227     }
228     
229 #endif /* THREADED_RTS */
230
231     // re-establish the signal handler, and carry on
232     sigemptyset(&signals);
233     sigaddset(&signals, sig);
234     sigprocmask(SIG_UNBLOCK, &signals, NULL);
235
236     context_switch = 1;
237 }
238
239 /* -----------------------------------------------------------------------------
240  * Blocking/Unblocking of the user signals
241  * -------------------------------------------------------------------------- */
242
243 static sigset_t userSignals;
244 static sigset_t savedSignals;
245
246 void
247 initUserSignals(void)
248 {
249     sigemptyset(&userSignals);
250 }
251
252 void
253 blockUserSignals(void)
254 {
255     sigprocmask(SIG_BLOCK, &userSignals, &savedSignals);
256 }
257
258 void
259 unblockUserSignals(void)
260 {
261     sigprocmask(SIG_SETMASK, &savedSignals, NULL);
262 }
263
264 rtsBool
265 anyUserHandlers(void)
266 {
267     return n_haskell_handlers != 0;
268 }
269
270 #if !defined(THREADED_RTS)
271 void
272 awaitUserSignals(void)
273 {
274     while (!signals_pending() && sched_state == SCHED_RUNNING) {
275         pause();
276     }
277 }
278 #endif
279
280 /* -----------------------------------------------------------------------------
281  * Install a Haskell signal handler.
282  * -------------------------------------------------------------------------- */
283
284 int
285 stg_sig_install(int sig, int spi, StgStablePtr *handler, void *mask)
286 {
287     sigset_t signals, osignals;
288     struct sigaction action;
289     StgInt previous_spi;
290
291     // Block the signal until we figure out what to do
292     // Count on this to fail if the signal number is invalid
293     if (sig < 0 || sigemptyset(&signals) ||
294         sigaddset(&signals, sig) || sigprocmask(SIG_BLOCK, &signals, &osignals)) {
295         return STG_SIG_ERR;
296     }
297     
298     more_handlers(sig);
299
300     previous_spi = signal_handlers[sig];
301
302     action.sa_flags = 0;
303     
304     switch(spi) {
305     case STG_SIG_IGN:
306         signal_handlers[sig] = STG_SIG_IGN;
307         sigdelset(&userSignals, sig);
308         action.sa_handler = SIG_IGN;
309         break;
310         
311     case STG_SIG_DFL:
312         signal_handlers[sig] = STG_SIG_DFL;
313         sigdelset(&userSignals, sig);
314         action.sa_handler = SIG_DFL;
315         break;
316
317     case STG_SIG_HAN:
318     case STG_SIG_RST:
319         signal_handlers[sig] = (StgInt)*handler;
320         sigaddset(&userSignals, sig);
321         action.sa_handler = generic_handler;
322         if (spi == STG_SIG_RST) {
323             action.sa_flags = SA_RESETHAND;
324         }
325         n_haskell_handlers++;
326         break;
327
328     default:
329         barf("stg_sig_install: bad spi");
330     }
331
332     if (mask != NULL)
333         action.sa_mask = *(sigset_t *)mask;
334     else
335         sigemptyset(&action.sa_mask);
336
337     action.sa_flags |= sig == SIGCHLD && nocldstop ? SA_NOCLDSTOP : 0;
338
339     if (sigaction(sig, &action, NULL) || 
340         sigprocmask(SIG_SETMASK, &osignals, NULL)) 
341     {
342         // need to return an error code, so avoid a stable pointer leak
343         // by freeing the previous handler if there was one.
344         if (previous_spi >= 0) {
345             freeStablePtr(stgCast(StgStablePtr,signal_handlers[sig]));
346             n_haskell_handlers--;
347         }
348         return STG_SIG_ERR;
349     }
350
351     if (previous_spi == STG_SIG_DFL || previous_spi == STG_SIG_IGN
352         || previous_spi == STG_SIG_ERR) {
353         return previous_spi;
354     } else {
355         *handler = (StgStablePtr)previous_spi;
356         return STG_SIG_HAN;
357     }
358 }
359
360 /* -----------------------------------------------------------------------------
361  * Creating new threads for signal handlers.
362  * -------------------------------------------------------------------------- */
363
364 #if !defined(THREADED_RTS)
365 void
366 startSignalHandlers(Capability *cap)
367 {
368   blockUserSignals();
369   
370   while (next_pending_handler != pending_handler_buf) {
371
372     next_pending_handler--;
373
374     scheduleThread (cap,
375         createIOThread(cap,
376                        RtsFlags.GcFlags.initialStkSize, 
377                        (StgClosure *) *next_pending_handler));
378   }
379
380   unblockUserSignals();
381 }
382 #endif
383
384 /* ----------------------------------------------------------------------------
385  * Mark signal handlers during GC.
386  *
387  * We do this rather than trying to start all the signal handlers
388  * prior to GC, because that requires extra heap for the new threads.
389  * Signals must be blocked (see blockUserSignals() above) during GC to
390  * avoid race conditions.
391  * -------------------------------------------------------------------------- */
392
393 #if !defined(THREADED_RTS)
394 void
395 markSignalHandlers (evac_fn evac, void *user)
396 {
397     StgPtr *p;
398
399     p = next_pending_handler;
400     while (p != pending_handler_buf) {
401         p--;
402         evac(user, (StgClosure **)p);
403     }
404 }
405 #else
406 void
407 markSignalHandlers (evac_fn evac STG_UNUSED, void *user STG_UNUSED)
408 {
409 }
410 #endif
411
412 #else /* !RTS_USER_SIGNALS */
413 StgInt 
414 stg_sig_install(StgInt sig STG_UNUSED,
415                 StgInt spi STG_UNUSED,
416                 StgStablePtr* handler STG_UNUSED,
417                 void* mask STG_UNUSED)
418 {
419   //barf("User signals not supported");
420   return STG_SIG_DFL;
421 }
422
423 #endif
424
425 #if defined(RTS_USER_SIGNALS)
426 /* -----------------------------------------------------------------------------
427  * SIGINT handler.
428  *
429  * We like to shutdown nicely after receiving a SIGINT, write out the
430  * stats, write profiling info, close open files and flush buffers etc.
431  * -------------------------------------------------------------------------- */
432 static void
433 shutdown_handler(int sig STG_UNUSED)
434 {
435     // If we're already trying to interrupt the RTS, terminate with
436     // extreme prejudice.  So the first ^C tries to exit the program
437     // cleanly, and the second one just kills it.
438     if (sched_state >= SCHED_INTERRUPTING) {
439         stg_exit(EXIT_INTERRUPTED);
440     } else {
441         interruptStgRts();
442     }
443 }
444
445 /* -----------------------------------------------------------------------------
446  * Install default signal handlers.
447  *
448  * The RTS installs a default signal handler for catching
449  * SIGINT, so that we can perform an orderly shutdown.
450  *
451  * Haskell code may install their own SIGINT handler, which is
452  * fine, provided they're so kind as to put back the old one
453  * when they de-install.
454  *
455  * In addition to handling SIGINT, the RTS also handles SIGFPE
456  * by ignoring it.  Apparently IEEE requires floating-point
457  * exceptions to be ignored by default, but alpha-dec-osf3
458  * doesn't seem to do so.
459  * -------------------------------------------------------------------------- */
460 void
461 initDefaultHandlers()
462 {
463     struct sigaction action,oact;
464
465     // install the SIGINT handler
466     action.sa_handler = shutdown_handler;
467     sigemptyset(&action.sa_mask);
468     action.sa_flags = 0;
469     if (sigaction(SIGINT, &action, &oact) != 0) {
470         sysErrorBelch("warning: failed to install SIGINT handler");
471     }
472
473 #if defined(HAVE_SIGINTERRUPT)
474     siginterrupt(SIGINT, 1);    // isn't this the default? --SDM
475 #endif
476
477     // install the SIGFPE handler
478
479     // In addition to handling SIGINT, also handle SIGFPE by ignoring it.
480     // Apparently IEEE requires floating-point exceptions to be ignored by
481     // default, but alpha-dec-osf3 doesn't seem to do so.
482
483     // Commented out by SDM 2/7/2002: this causes an infinite loop on
484     // some architectures when an integer division by zero occurs: we
485     // don't recover from the floating point exception, and the
486     // program just generates another one immediately.
487 #if 0
488     action.sa_handler = SIG_IGN;
489     sigemptyset(&action.sa_mask);
490     action.sa_flags = 0;
491     if (sigaction(SIGFPE, &action, &oact) != 0) {
492         sysErrorBelch("warning: failed to install SIGFPE handler");
493     }
494 #endif
495
496 #ifdef alpha_HOST_ARCH
497     ieee_set_fp_control(0);
498 #endif
499 }
500
501 void
502 freeSignalHandlers(void) {
503     if (signal_handlers != NULL) {
504         stgFree(signal_handlers);
505     }
506 }
507
508 #endif /* RTS_USER_SIGNALS */