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