abbb97ae68c43c809e62a487ba07ffb6b4e78139
[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  * SIGCONT handler
168  *
169  * It seems that shells tend to put stdin back into blocking mode
170  * following a suspend/resume of the process.  Here we arrange to put
171  * it back into non-blocking mode.  We don't do anything to
172  * stdout/stderr because these handles don't get put into non-blocking
173  * mode at all - see the comments on stdout/stderr in PrelHandle.hsc.
174  * -------------------------------------------------------------------------- */
175
176 static void
177 cont_handler(int sig STG_UNUSED)
178 {
179     setNonBlockingFd(0);
180 }
181
182 /* -----------------------------------------------------------------------------
183  * Low-level signal handler
184  *
185  * Places the requested handler on a stack of pending handlers to be
186  * started up at the next context switch.
187  * -------------------------------------------------------------------------- */
188
189 static void
190 generic_handler(int sig)
191 {
192     sigset_t signals;
193
194 #if defined(THREADED_RTS)
195
196     if (io_manager_pipe != -1)
197     {
198         // Write the signal number into the pipe as a single byte.  We
199         // hope that signals fit into a byte...
200         StgWord8 csig = (StgWord8)sig;
201         write(io_manager_pipe, &csig, 1);
202     }
203     // If the IO manager hasn't told us what the FD of the write end
204     // of its pipe is, there's not much we can do here, so just ignore
205     // the signal..
206
207 #else /* not THREADED_RTS */
208
209     /* Can't call allocate from here.  Probably can't call malloc
210        either.  However, we have to schedule a new thread somehow.
211
212        It's probably ok to request a context switch and allow the
213        scheduler to  start the handler thread, but how do we
214        communicate this to the scheduler?
215
216        We need some kind of locking, but with low overhead (i.e. no
217        blocking signals every time around the scheduler).
218        
219        Signal Handlers are atomic (i.e. they can't be interrupted), and
220        we can make use of this.  We just need to make sure the
221        critical section of the scheduler can't be interrupted - the
222        only way to do this is to block signals.  However, we can lower
223        the overhead by only blocking signals when there are any
224        handlers to run, i.e. the set of pending handlers is
225        non-empty.
226     */
227        
228     /* We use a stack to store the pending signals.  We can't
229        dynamically grow this since we can't allocate any memory from
230        within a signal handler.
231
232        Hence unfortunately we have to bomb out if the buffer
233        overflows.  It might be acceptable to carry on in certain
234        circumstances, depending on the signal.  
235     */
236
237     *next_pending_handler++ = deRefStablePtr((StgStablePtr)signal_handlers[sig]);
238
239     // stack full?
240     if (next_pending_handler == &pending_handler_buf[N_PENDING_HANDLERS]) {
241         errorBelch("too many pending signals");
242         stg_exit(EXIT_FAILURE);
243     }
244     
245 #endif /* THREADED_RTS */
246
247     // re-establish the signal handler, and carry on
248     sigemptyset(&signals);
249     sigaddset(&signals, sig);
250     sigprocmask(SIG_UNBLOCK, &signals, NULL);
251
252     // *always* do the SIGCONT handler, even if the user overrides it.
253     if (sig == SIGCONT) {
254         cont_handler(sig);
255     }
256
257     context_switch = 1;
258 }
259
260 /* -----------------------------------------------------------------------------
261  * Blocking/Unblocking of the user signals
262  * -------------------------------------------------------------------------- */
263
264 static sigset_t userSignals;
265 static sigset_t savedSignals;
266
267 void
268 initUserSignals(void)
269 {
270     sigemptyset(&userSignals);
271 }
272
273 void
274 blockUserSignals(void)
275 {
276     sigprocmask(SIG_BLOCK, &userSignals, &savedSignals);
277 }
278
279 void
280 unblockUserSignals(void)
281 {
282     sigprocmask(SIG_SETMASK, &savedSignals, NULL);
283 }
284
285 rtsBool
286 anyUserHandlers(void)
287 {
288     return n_haskell_handlers != 0;
289 }
290
291 #if !defined(THREADED_RTS)
292 void
293 awaitUserSignals(void)
294 {
295     while (!signals_pending() && sched_state == SCHED_RUNNING) {
296         pause();
297     }
298 }
299 #endif
300
301 /* -----------------------------------------------------------------------------
302  * Install a Haskell signal handler.
303  * -------------------------------------------------------------------------- */
304
305 int
306 stg_sig_install(int sig, int spi, StgStablePtr *handler, void *mask)
307 {
308     sigset_t signals, osignals;
309     struct sigaction action;
310     StgInt previous_spi;
311
312     // Block the signal until we figure out what to do
313     // Count on this to fail if the signal number is invalid
314     if (sig < 0 || sigemptyset(&signals) ||
315         sigaddset(&signals, sig) || sigprocmask(SIG_BLOCK, &signals, &osignals)) {
316         return STG_SIG_ERR;
317     }
318     
319     more_handlers(sig);
320
321     previous_spi = signal_handlers[sig];
322
323     action.sa_flags = 0;
324     
325     switch(spi) {
326     case STG_SIG_IGN:
327         signal_handlers[sig] = STG_SIG_IGN;
328         sigdelset(&userSignals, sig);
329         action.sa_handler = SIG_IGN;
330         break;
331         
332     case STG_SIG_DFL:
333         signal_handlers[sig] = STG_SIG_DFL;
334         sigdelset(&userSignals, sig);
335         action.sa_handler = SIG_DFL;
336         break;
337
338     case STG_SIG_HAN:
339     case STG_SIG_RST:
340         signal_handlers[sig] = (StgInt)*handler;
341         sigaddset(&userSignals, sig);
342         action.sa_handler = generic_handler;
343         if (spi == STG_SIG_RST) {
344             action.sa_flags = SA_RESETHAND;
345         }
346         n_haskell_handlers++;
347         break;
348
349     default:
350         barf("stg_sig_install: bad spi");
351     }
352
353     if (mask != NULL)
354         action.sa_mask = *(sigset_t *)mask;
355     else
356         sigemptyset(&action.sa_mask);
357
358     action.sa_flags |= sig == SIGCHLD && nocldstop ? SA_NOCLDSTOP : 0;
359
360     if (sigaction(sig, &action, NULL) || 
361         sigprocmask(SIG_SETMASK, &osignals, NULL)) 
362     {
363         // need to return an error code, so avoid a stable pointer leak
364         // by freeing the previous handler if there was one.
365         if (previous_spi >= 0) {
366             freeStablePtr(stgCast(StgStablePtr,signal_handlers[sig]));
367             n_haskell_handlers--;
368         }
369         return STG_SIG_ERR;
370     }
371
372     if (previous_spi == STG_SIG_DFL || previous_spi == STG_SIG_IGN
373         || previous_spi == STG_SIG_ERR) {
374         return previous_spi;
375     } else {
376         *handler = (StgStablePtr)previous_spi;
377         return STG_SIG_HAN;
378     }
379 }
380
381 /* -----------------------------------------------------------------------------
382  * Creating new threads for signal handlers.
383  * -------------------------------------------------------------------------- */
384
385 #if !defined(THREADED_RTS)
386 void
387 startSignalHandlers(Capability *cap)
388 {
389   blockUserSignals();
390   
391   while (next_pending_handler != pending_handler_buf) {
392
393     next_pending_handler--;
394
395     scheduleThread (cap,
396         createIOThread(cap,
397                        RtsFlags.GcFlags.initialStkSize, 
398                        (StgClosure *) *next_pending_handler));
399   }
400
401   unblockUserSignals();
402 }
403 #endif
404
405 /* ----------------------------------------------------------------------------
406  * Mark signal handlers during GC.
407  *
408  * We do this rather than trying to start all the signal handlers
409  * prior to GC, because that requires extra heap for the new threads.
410  * Signals must be blocked (see blockUserSignals() above) during GC to
411  * avoid race conditions.
412  * -------------------------------------------------------------------------- */
413
414 #if !defined(THREADED_RTS)
415 void
416 markSignalHandlers (evac_fn evac)
417 {
418     StgPtr *p;
419
420     p = next_pending_handler;
421     while (p != pending_handler_buf) {
422         p--;
423         evac((StgClosure **)p);
424     }
425 }
426 #else
427 void
428 markSignalHandlers (evac_fn evac STG_UNUSED)
429 {
430 }
431 #endif
432
433 #else /* !RTS_USER_SIGNALS */
434 StgInt 
435 stg_sig_install(StgInt sig STG_UNUSED,
436                 StgInt spi STG_UNUSED,
437                 StgStablePtr* handler STG_UNUSED,
438                 void* mask STG_UNUSED)
439 {
440   //barf("User signals not supported");
441   return STG_SIG_DFL;
442 }
443
444 #endif
445
446 #if defined(RTS_USER_SIGNALS)
447 /* -----------------------------------------------------------------------------
448  * SIGINT handler.
449  *
450  * We like to shutdown nicely after receiving a SIGINT, write out the
451  * stats, write profiling info, close open files and flush buffers etc.
452  * -------------------------------------------------------------------------- */
453 #ifdef SMP
454 pthread_t startup_guy;
455 #endif
456
457 static void
458 shutdown_handler(int sig STG_UNUSED)
459 {
460 #ifdef SMP
461     // if I'm a worker thread, send this signal to the guy who
462     // originally called startupHaskell().  Since we're handling
463     // the signal, it won't be a "send to all threads" type of signal
464     // (according to the POSIX threads spec).
465     if (pthread_self() != startup_guy) {
466         pthread_kill(startup_guy, sig);
467         return;
468     }
469 #endif
470
471     // If we're already trying to interrupt the RTS, terminate with
472     // extreme prejudice.  So the first ^C tries to exit the program
473     // cleanly, and the second one just kills it.
474     if (sched_state >= SCHED_INTERRUPTING) {
475         stg_exit(EXIT_INTERRUPTED);
476     } else {
477         interruptStgRts();
478     }
479 }
480
481 /* -----------------------------------------------------------------------------
482  * Install default signal handlers.
483  *
484  * The RTS installs a default signal handler for catching
485  * SIGINT, so that we can perform an orderly shutdown.
486  *
487  * Haskell code may install their own SIGINT handler, which is
488  * fine, provided they're so kind as to put back the old one
489  * when they de-install.
490  *
491  * In addition to handling SIGINT, the RTS also handles SIGFPE
492  * by ignoring it.  Apparently IEEE requires floating-point
493  * exceptions to be ignored by default, but alpha-dec-osf3
494  * doesn't seem to do so.
495  * -------------------------------------------------------------------------- */
496 void
497 initDefaultHandlers()
498 {
499     struct sigaction action,oact;
500
501 #ifdef SMP
502     startup_guy = pthread_self();
503 #endif
504
505     // install the SIGINT handler
506     action.sa_handler = shutdown_handler;
507     sigemptyset(&action.sa_mask);
508     action.sa_flags = 0;
509     if (sigaction(SIGINT, &action, &oact) != 0) {
510         sysErrorBelch("warning: failed to install SIGINT handler");
511     }
512
513 #if defined(HAVE_SIGINTERRUPT)
514     siginterrupt(SIGINT, 1);    // isn't this the default? --SDM
515 #endif
516
517     // install the SIGCONT handler
518     action.sa_handler = cont_handler;
519     sigemptyset(&action.sa_mask);
520     action.sa_flags = 0;
521     if (sigaction(SIGCONT, &action, &oact) != 0) {
522         sysErrorBelch("warning: failed to install SIGCONT handler");
523     }
524
525     // install the SIGFPE handler
526
527     // In addition to handling SIGINT, also handle SIGFPE by ignoring it.
528     // Apparently IEEE requires floating-point exceptions to be ignored by
529     // default, but alpha-dec-osf3 doesn't seem to do so.
530
531     // Commented out by SDM 2/7/2002: this causes an infinite loop on
532     // some architectures when an integer division by zero occurs: we
533     // don't recover from the floating point exception, and the
534     // program just generates another one immediately.
535 #if 0
536     action.sa_handler = SIG_IGN;
537     sigemptyset(&action.sa_mask);
538     action.sa_flags = 0;
539     if (sigaction(SIGFPE, &action, &oact) != 0) {
540         sysErrorBelch("warning: failed to install SIGFPE handler");
541     }
542 #endif
543
544 #ifdef alpha_HOST_ARCH
545     ieee_set_fp_control(0);
546 #endif
547 }
548
549 void
550 freeSignalHandlers(void) {
551     if (signal_handlers != NULL) {
552         stgFree(signal_handlers);
553     }
554 }
555
556 #endif /* RTS_USER_SIGNALS */