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