Duncan Coutts pushed to branch wip/dcoutts/signals-cleanup at Glasgow Haskell Compiler / GHC

Commits:

26 changed files:

Changes:

  • libraries/ghc-internal/src/GHC/Internal/System/Posix/Internals.hs
    ... ... @@ -878,7 +878,12 @@ foreign import capi unsafe "signal.h sigemptyset"
    878 878
     foreign import capi unsafe "signal.h sigaddset"
    
    879 879
        c_sigaddset :: Ptr CSigset -> CInt -> IO CInt
    
    880 880
     
    
    881
    -foreign import capi unsafe "signal.h sigprocmask"
    
    881
    +-- Actually uses @pthread_sigmask@, which is the more modern version
    
    882
    +-- specified by Posix. pthread_sigmask() is specified to be per-thread.
    
    883
    +--
    
    884
    +-- Use of this is still very dubious however!
    
    885
    +-- See Note [pthread_sigmask and the ineffectiveness of masking signals]
    
    886
    +foreign import capi unsafe "signal.h pthread_sigmask"
    
    882 887
        c_sigprocmask :: CInt -> Ptr CSigset -> Ptr CSigset -> IO CInt
    
    883 888
     #endif
    
    884 889
     
    

  • rts/IOManager.c
    ... ... @@ -27,6 +27,11 @@
    27 27
     
    
    28 28
     #include "IOManagerInternals.h"
    
    29 29
     
    
    30
    +#if !defined(mingw32_HOST_OS)
    
    31
    +// TODO: make this per-I/O manager, not called from here directly.
    
    32
    +#include "posix/IOManSignals.h"
    
    33
    +#endif
    
    34
    +
    
    30 35
     #if defined(IOMGR_ENABLED_SELECT)
    
    31 36
     #include "Threads.h"
    
    32 37
     #include "posix/Select.h"
    
    ... ... @@ -50,6 +55,7 @@
    50 55
     #if defined(IOMGR_ENABLED_MIO_WIN32)
    
    51 56
     #include "win32/ThrIOManager.h"
    
    52 57
     #include "win32/AsyncMIO.h"
    
    58
    +#include "win32/ConsoleHandler.h"
    
    53 59
     #endif
    
    54 60
     
    
    55 61
     #if defined(IOMGR_ENABLED_WIN32_LEGACY)
    
    ... ... @@ -57,6 +63,7 @@
    57 63
     #include "win32/AsyncMIO.h"
    
    58 64
     #include "win32/AwaitEvent.h"
    
    59 65
     #include "win32/MIOManager.h"
    
    66
    +#include "win32/ConsoleHandler.h"
    
    60 67
     #endif
    
    61 68
     
    
    62 69
     #if defined(IOMGR_ENABLED_WINIO)
    
    ... ... @@ -65,6 +72,7 @@
    65 72
     #if !defined(THREADED_RTS)
    
    66 73
     #include "win32/AwaitEvent.h"
    
    67 74
     #endif
    
    75
    +#include "win32/ConsoleHandler.h"
    
    68 76
     #endif
    
    69 77
     
    
    70 78
     #include <string.h>
    
    ... ... @@ -348,6 +356,15 @@ CapIOManager *allocCapabilityIOManager(Capability *cap)
    348 356
         CapIOManager *iomgr = stgMallocBytes(sizeof(CapIOManager),
    
    349 357
                                              "allocCapabilityIOManager");
    
    350 358
         iomgr->cap = cap; /* link back */
    
    359
    +
    
    360
    +    /* Allocate and/or link the shared I/O manager data structure */
    
    361
    +    if (cap->no == 0) {
    
    362
    +        iomgr->shared = stgMallocBytes(sizeof(CapIOManagerShared),
    
    363
    +                                       "allocCapabilityIOManager");
    
    364
    +    } else {
    
    365
    +        iomgr->shared = MainCapability.iomgr->shared;
    
    366
    +    }
    
    367
    +
    
    351 368
         return iomgr;
    
    352 369
     }
    
    353 370
     
    
    ... ... @@ -364,6 +381,8 @@ void initCapabilityIOManager(CapIOManager *iomgr)
    364 381
         debugTrace(DEBUG_iomanager, "initialising I/O manager %s for cap %d",
    
    365 382
                    showIOManager(), iomgr->cap->no);
    
    366 383
     
    
    384
    +    ASSERT(iomgr->cap && iomgr->shared); /* These are initialised already */
    
    385
    +
    
    367 386
         switch (iomgr_type) {
    
    368 387
     #if defined(IOMGR_ENABLED_SELECT)
    
    369 388
             case IO_MANAGER_SELECT:
    
    ... ... @@ -392,7 +411,7 @@ void initCapabilityIOManager(CapIOManager *iomgr)
    392 411
     
    
    393 412
     #if defined(IOMGR_ENABLED_MIO_POSIX)
    
    394 413
             case IO_MANAGER_MIO_POSIX:
    
    395
    -            iomgr->control_fd = -1;
    
    414
    +            initCapabilityIOManagerMIO(iomgr);
    
    396 415
                 break;
    
    397 416
     #endif
    
    398 417
             default:
    
    ... ... @@ -424,6 +443,13 @@ void freeCapabilityIOManager(CapIOManager *iomgr)
    424 443
             default:
    
    425 444
                 break;
    
    426 445
         }
    
    446
    +
    
    447
    +    /* We expect the per-impl free function (for cap 0) to free anything within
    
    448
    +     * the shared structure.
    
    449
    +     */
    
    450
    +    if (iomgr->cap->no == 0) {
    
    451
    +        stgFree(iomgr->shared);
    
    452
    +    }
    
    427 453
     }
    
    428 454
     
    
    429 455
     
    
    ... ... @@ -449,6 +475,7 @@ void startIOManager(void)
    449 475
                  * TODO: rename ioManagerStart to be less generic, impl specific
    
    450 476
                  */
    
    451 477
                 ioManagerStart();
    
    478
    +            initConsoleEvents(); //TODO: fully integrate
    
    452 479
                 break;
    
    453 480
     #endif
    
    454 481
     #if defined(IOMGR_ENABLED_WINIO)
    
    ... ... @@ -465,11 +492,13 @@ void startIOManager(void)
    465 492
     #else
    
    466 493
                 startupAsyncWinIO();
    
    467 494
     #endif
    
    495
    +            initConsoleEvents(); //TODO: fully integrate
    
    468 496
                 break;
    
    469 497
     #endif
    
    470 498
     #if defined(IOMGR_ENABLED_WIN32_LEGACY)
    
    471 499
             case IO_MANAGER_WIN32_LEGACY:
    
    472 500
                 startupAsyncIO();
    
    501
    +            initConsoleEvents(); //TODO: fully integrate
    
    473 502
                 break;
    
    474 503
     #endif
    
    475 504
             default:
    
    ... ... @@ -566,14 +595,21 @@ void
    566 595
     exitIOManager(bool wait_threads)
    
    567 596
     {
    
    568 597
         switch (iomgr_type) {
    
    598
    +#if defined(IOMGR_ENABLED_MIO_WIN32)
    
    599
    +        case IO_MANAGER_MIO_WIN32:
    
    600
    +            finiConsoleEvents(); //TODO: fully integrate
    
    601
    +            break;
    
    602
    +#endif
    
    569 603
     #if defined(IOMGR_ENABLED_WINIO)
    
    570 604
             case IO_MANAGER_WINIO:
    
    571 605
                 shutdownAsyncWinIO(wait_threads);
    
    606
    +            finiConsoleEvents(); //TODO: fully integrate
    
    572 607
                 break;
    
    573 608
     #endif
    
    574 609
     #if defined(IOMGR_ENABLED_WIN32_LEGACY)
    
    575 610
             case IO_MANAGER_WIN32_LEGACY:
    
    576 611
                 shutdownAsyncIO(wait_threads);
    
    612
    +            finiConsoleEvents(); //TODO: fully integrate
    
    577 613
                 break;
    
    578 614
     #endif
    
    579 615
             default:
    
    ... ... @@ -634,6 +670,67 @@ setIOManagerControlFd(uint32_t cap_no, int fd) {
    634 670
     #endif
    
    635 671
     
    
    636 672
     
    
    673
    +/* This is a plausible candidate for a newer public API, but it isn't yet.
    
    674
    + * It is currently the worker for stg_sig_install below.
    
    675
    + */
    
    676
    +static int installUserSignalHandler(int sig, int spi, sigset_t *mask,
    
    677
    +                                    bool nocldstop)
    
    678
    +{
    
    679
    +#if !defined(RTS_USER_SIGNALS)
    
    680
    +    return STG_SIG_DFL;
    
    681
    +#endif
    
    682
    +
    
    683
    +    debugTrace(DEBUG_iomanager, "Installing a signal handler for sig %d", sig);
    
    684
    +
    
    685
    +    switch (iomgr_type) {
    
    686
    +#if defined(IOMGR_ENABLED_SELECT)
    
    687
    +        case IO_MANAGER_SELECT:
    
    688
    +          return installUserSignalHandlerSelect(sig, spi, mask, nocldstop);
    
    689
    +#endif
    
    690
    +#if defined(IOMGR_ENABLED_SELECTBIS)
    
    691
    +        case IO_MANAGER_SELECTBIS:
    
    692
    +          return installUserSignalHandlerSelectBis(sig, spi, mask, nocldstop);
    
    693
    +#endif
    
    694
    +#if defined(IOMGR_ENABLED_POLL)
    
    695
    +        case IO_MANAGER_POLL:
    
    696
    +          return installUserSignalHandlerPoll(sig, spi, mask, nocldstop);
    
    697
    +#endif
    
    698
    +#if defined(IOMGR_ENABLED_MIO)
    
    699
    +        case IO_MANAGER_MIO:
    
    700
    +          return installUserSignalHandlerMIO(sig, spi, mask, nocldstop);
    
    701
    +#endif
    
    702
    +        default:
    
    703
    +            barf("installUserSignalHandler not implemented");
    
    704
    +    }
    
    705
    +}
    
    706
    +
    
    707
    +
    
    708
    +
    
    709
    +#if !defined(mingw32_HOST_OS)
    
    710
    +/* This flag is a public API, used by the unix package. It is an _implicit_
    
    711
    + * argument to stg_sig_install to control whether or not to include
    
    712
    + * SA_NOCLDSTOP when installing a SIGCHLD handler.
    
    713
    + *
    
    714
    + * Yes this is completely mad, and it should really be a normal explicit
    
    715
    + * parameter. Note also the total lack of protection against data races.
    
    716
    + */
    
    717
    +HsInt nocldstop = 0;
    
    718
    +
    
    719
    +
    
    720
    +/* Declared in rts/IOInterface.h.  This is a public API, used by the unix
    
    721
    + * package for installHandler.
    
    722
    + *
    
    723
    + * This is used as an FFI ccall import, but it would be better as a primop, for
    
    724
    + * consistency and so we can always have a Capability to hand.
    
    725
    + */
    
    726
    +int stg_sig_install(int sig, int spi, void *mask)
    
    727
    +{
    
    728
    +    /* Grab the current value of the nocldstop argument. Yes this is madness. */
    
    729
    +    return installUserSignalHandler(sig, spi, mask, nocldstop);
    
    730
    +}
    
    731
    +#endif
    
    732
    +
    
    733
    +
    
    637 734
     void pollCompletedTimeoutsOrIO(CapIOManager *iomgr)
    
    638 735
     {
    
    639 736
         debugTrace(DEBUG_iomanager, "polling for completed IO or timeouts");
    

  • rts/IOManagerInternals.h
    ... ... @@ -30,6 +30,11 @@
    30 30
     
    
    31 31
     #include "BeginPrivate.h"
    
    32 32
     
    
    33
    +/* Forward reference */
    
    34
    +struct _CapIOManagerShared;
    
    35
    +typedef struct _CapIOManagerShared CapIOManagerShared;
    
    36
    +
    
    37
    +
    
    33 38
     /* The per-capability data structures belonging to the I/O manager.
    
    34 39
      *
    
    35 40
      * It can be accessed as cap->iomgr. Or given just the iomgr, you can access
    
    ... ... @@ -42,9 +47,12 @@
    42 47
      */
    
    43 48
     struct _CapIOManager {
    
    44 49
     
    
    45
    -   /* Back reference to the containing capability */
    
    50
    +    /* Back reference to the containing capability */
    
    46 51
         Capability *cap;
    
    47 52
     
    
    53
    +    /* Data shared between the I/O manager instances across all capabilities */
    
    54
    +    CapIOManagerShared *shared;
    
    55
    +
    
    48 56
     #if defined(IOMGR_ENABLED_SELECT)
    
    49 57
         /* Thread queue for threads blocked on I/O completion. */
    
    50 58
         StgTSO *blocked_queue_hd;
    
    ... ... @@ -96,6 +104,24 @@ struct _CapIOManager {
    96 104
     
    
    97 105
     };
    
    98 106
     
    
    107
    +
    
    108
    +/* Much like above, but for I/O manager data that is shared between the
    
    109
    + * I/O manager instances for all the capabilities.
    
    110
    + *
    
    111
    + * This gets allocated and linked by the shared IOManager.c code, and should be
    
    112
    + * initialised by the individual I/O manager's init function but only for cap 0.
    
    113
    + */
    
    114
    +struct _CapIOManagerShared {
    
    115
    +
    
    116
    +#if defined(IOMGR_ENABLED_MIO_POSIX)
    
    117
    +    /* The timer manager control pipe (write end). This is the pipe into which
    
    118
    +     * we will send our signals.
    
    119
    +     */
    
    120
    +    int timer_manager_control_wr_fd;
    
    121
    +#endif
    
    122
    +};
    
    123
    +
    
    124
    +
    
    99 125
     /* Fill in the outcome and result/error on the TSO's stack frame.
    
    100 126
      * The TSO must be blocked using one of the stg_block_io_* blocking actions.
    
    101 127
      *
    

  • rts/RtsFlags.c
    ... ... @@ -535,9 +535,8 @@ usage_text[] = {
    535 535
     "  --install-seh-handlers=<yes|no>",
    
    536 536
     "             Install exception handlers (default: yes)",
    
    537 537
     "  --generate-crash-dumps",
    
    538
    -"             Generate Windows crash dumps, requires exception handlers",
    
    539
    -"             to be installed. Implies --install-signal-handlers=yes.",
    
    540
    -"             (default: no)",
    
    538
    +"             Generate Windows crash dumps (default: no).",
    
    539
    +"             Implies --install-seh-handlers=yes",
    
    541 540
     "  --generate-stack-traces=<yes|no>",
    
    542 541
     "             Generate a stack trace when your application encounters a",
    
    543 542
     "             fatal error. When symbols are available an attempt will be",
    

  • rts/RtsSignals.h
    ... ... @@ -15,13 +15,6 @@
    15 15
     
    
    16 16
     #include "BeginPrivate.h"
    
    17 17
     
    
    18
    -/*
    
    19
    - * Function: initUserSignals()
    
    20
    - *
    
    21
    - * Initialize the console handling substrate.
    
    22
    - */
    
    23
    -void initUserSignals(void);
    
    24
    -
    
    25 18
     /*
    
    26 19
      * Function: initDefaultHandlers()
    
    27 20
      *
    
    ... ... @@ -31,24 +24,6 @@ void initUserSignals(void);
    31 24
     void initDefaultHandlers(void);
    
    32 25
     void resetDefaultHandlers(void);
    
    33 26
     
    
    34
    -void freeSignalHandlers(void);
    
    35
    -
    
    36
    -/* Tear down and shut down user signal processing.
    
    37
    - * This is called *after* freeSignalHandlers, but unconditionally!
    
    38
    - * TODO: unify this and freeSignalHandlers together, and make them make sense!
    
    39
    - */
    
    40
    -void finiUserSignals(void);
    
    41
    -
    
    42
    -/*
    
    43
    - * Function: startPendingSignalHandlers()
    
    44
    - *
    
    45
    - * If there are any queued up posix signals or win32 console events, run the
    
    46
    - * handlers associated with them. This is used by some in-RTS I/O managers.
    
    47
    - */
    
    48
    -#if !defined(THREADED_RTS)
    
    49
    -void startPendingSignalHandlers(Capability *cap);
    
    50
    -#endif
    
    51
    -
    
    52 27
     #include "EndPrivate.h"
    
    53 28
     
    
    54 29
     #endif /* RTS_USER_SIGNALS */

  • rts/RtsStartup.c
    ... ... @@ -423,12 +423,8 @@ hs_init_ghc(int *argc, char **argv[], RtsConfig rts_config)
    423 423
         unpauseTimer();
    
    424 424
     
    
    425 425
     #if defined(RTS_USER_SIGNALS)
    
    426
    -    if (RtsFlags.MiscFlags.install_signal_handlers) {
    
    427
    -        /* Initialise the user signal handler set */
    
    428
    -        initUserSignals();
    
    429
    -        /* Set up handler to run on SIGINT, etc. */
    
    430
    -        initDefaultHandlers();
    
    431
    -    }
    
    426
    +    /* Set up handler to run on SIGINT, etc. */
    
    427
    +    initDefaultHandlers();
    
    432 428
     #endif
    
    433 429
     
    
    434 430
         startIOManager();
    
    ... ... @@ -510,12 +506,6 @@ hs_exit_(bool wait_foreign)
    510 506
         }
    
    511 507
         runAllCFinalizers(nonmoving_weak_ptr_list);
    
    512 508
     
    
    513
    -#if defined(RTS_USER_SIGNALS)
    
    514
    -    if (RtsFlags.MiscFlags.install_signal_handlers) {
    
    515
    -        freeSignalHandlers();
    
    516
    -    }
    
    517
    -#endif
    
    518
    -
    
    519 509
         /* We rely on the guarantee that exitTimer stops the timer synchronously,
    
    520 510
          * which ensures the timer handler does not get run again after this point.
    
    521 511
          * We are about to start freeing resources used by the timer handler (like
    
    ... ... @@ -540,10 +530,8 @@ hs_exit_(bool wait_foreign)
    540 530
     #endif
    
    541 531
     
    
    542 532
     #if defined(RTS_USER_SIGNALS)
    
    543
    -    if (RtsFlags.MiscFlags.install_signal_handlers) {
    
    544
    -        // uninstall signal handlers
    
    545
    -        resetDefaultHandlers();
    
    546
    -    }
    
    533
    +    // uninstall signal handlers
    
    534
    +    resetDefaultHandlers();
    
    547 535
     #endif
    
    548 536
     
    
    549 537
         /* stop timing the shutdown, we're about to print stats */
    
    ... ... @@ -623,10 +611,6 @@ hs_exit_(bool wait_foreign)
    623 611
           hs_restoreConsoleCP();
    
    624 612
     #endif
    
    625 613
     
    
    626
    -#if defined(RTS_USER_SIGNALS)
    
    627
    -   finiUserSignals ();
    
    628
    -#endif
    
    629
    -
    
    630 614
         /* tear down statistics subsystem */
    
    631 615
         stat_exit();
    
    632 616
     
    
    ... ... @@ -725,10 +709,12 @@ exitBySignal(int sig)
    725 709
         dfl.sa_handler = SIG_DFL;
    
    726 710
         (void)sigaction(sig, &dfl, NULL);
    
    727 711
     
    
    728
    -    // Then we unblock the signal so we can deliver it to ourselves
    
    712
    +    // Then we unmask the signal so we can deliver it to ourselves.
    
    713
    +    // This is a (rare) valid use of signal mask manipulation.
    
    714
    +    // See Note [pthread_sigmask and the ineffectiveness of masking signals].
    
    729 715
         sigemptyset(&sigset);
    
    730 716
         sigaddset(&sigset, sig);
    
    731
    -    sigprocmask(SIG_UNBLOCK, &sigset, NULL);
    
    717
    +    pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
    
    732 718
     
    
    733 719
         switch (sig) {
    
    734 720
           case SIGSTOP: case SIGTSTP: case SIGTTIN: case SIGTTOU: case SIGCONT:
    

  • rts/configure.ac
    ... ... @@ -221,10 +221,6 @@ AC_LINK_IFELSE([AC_LANG_CALL([], [printf\$LDBLStub])],
    221 221
     
    
    222 222
     FP_CHECK_PTHREAD_FUNCS
    
    223 223
     
    
    224
    -dnl ** check for eventfd which is needed by the I/O manager
    
    225
    -AC_CHECK_HEADERS([sys/eventfd.h])
    
    226
    -AC_CHECK_FUNCS([eventfd])
    
    227
    -
    
    228 224
     AC_CHECK_FUNCS([getpid getuid raise])
    
    229 225
     
    
    230 226
     dnl large address space support (see rts/include/rts/storage/MBlock.h)
    
    ... ... @@ -359,6 +355,10 @@ dnl -------------------------------------------------------------------
    359 355
     dnl Here we check for platform constraints. The result is the CPP flag
    
    360 356
     dnl IOMGR_BUILD_<name> and a EnableIOManager<Name> var for use here later.
    
    361 357
     
    
    358
    +dnl Check for functions (optionally) used by several I/O managers
    
    359
    +AC_CHECK_HEADERS([sys/eventfd.h])
    
    360
    +AC_CHECK_FUNCS([eventfd pipe2])
    
    361
    +
    
    362 362
     GHC_IOMANAGER_ENABLE([select], [EnableIOManagerSelect], [IOMGR_BUILD_SELECT],
    
    363 363
       [if test "$HostOS" = "mingw32"; then
    
    364 364
            EnableIOManagerSelect=NO
    

  • rts/include/Rts.h
    ... ... @@ -283,10 +283,6 @@ void exitHeapOverflow(void) STG_NORETURN;;
    283 283
     
    
    284 284
     void stg_exit(int n) STG_NORETURN;
    
    285 285
     
    
    286
    -#if !defined(mingw32_HOST_OS)
    
    287
    -int stg_sig_install (int, int, void *);
    
    288
    -#endif
    
    289
    -
    
    290 286
     /* -----------------------------------------------------------------------------
    
    291 287
        Ways
    
    292 288
        -------------------------------------------------------------------------- */
    

  • rts/include/rts/BlockSignals.h
    ... ... @@ -13,16 +13,25 @@
    13 13
     
    
    14 14
     #pragma once
    
    15 15
     
    
    16
    -/* Used by runProcess() in the process package
    
    16
    +#if !defined(mingw32_HOST_OS)
    
    17
    +
    
    18
    +/* Used by runProcess() in the process package, but this is almost certainly
    
    19
    + * a bad idea.
    
    20
    + *
    
    21
    + * See also:
    
    22
    + *  + https://github.com/haskell/process/issues/384
    
    23
    + *  + https://github.com/haskell/process/issues/385
    
    24
    + *  + GHC issue #27968
    
    25
    + *  + See Note [pthread_sigmask and the ineffectiveness of masking signals]
    
    17 26
      */
    
    18 27
     
    
    19 28
     /*
    
    20 29
      * Function: blockUserSignals()
    
    21 30
      *
    
    22
    - * Temporarily block the delivery of further console events. Needed to
    
    23
    - * avoid race conditions when GCing the queue of outstanding handlers or
    
    24
    - * when emptying the queue by running the handlers.
    
    31
    + * Temporarily block the delivery of further user-registered posix signals.
    
    25 32
      *
    
    33
    + * This is exported from the RTS but should no longer be used. It is likely to
    
    34
    + * be removed in future.
    
    26 35
      */
    
    27 36
     void blockUserSignals(void);
    
    28 37
     
    
    ... ... @@ -32,3 +41,5 @@ void blockUserSignals(void);
    32 41
      * The inverse of blockUserSignals(); re-enable the deliver of console events.
    
    33 42
      */
    
    34 43
     void unblockUserSignals(void);
    
    44
    +
    
    45
    +#endif

  • rts/include/rts/IOInterface.h
    ... ... @@ -33,6 +33,7 @@ void ioManagerFinished (void);
    33 33
     
    
    34 34
     void     setIOManagerControlFd   (uint32_t cap_no, int fd);
    
    35 35
     void     setTimerManagerControlFd(int fd);
    
    36
    +int      stg_sig_install(int sig, int spi, void *mask);
    
    36 37
     
    
    37 38
     #endif
    
    38 39
     

  • rts/posix/IOManSignals.c
    1
    +/* -----------------------------------------------------------------------------
    
    2
    + *
    
    3
    + * (c) The GHC Team, 1998-2005
    
    4
    + *
    
    5
    + * Signal processing / handling.
    
    6
    + *
    
    7
    + * ---------------------------------------------------------------------------*/
    
    8
    +
    
    9
    +#include "rts/PosixSource.h"
    
    10
    +#include "Rts.h"
    
    11
    +
    
    12
    +#if defined(RTS_USER_SIGNALS)
    
    13
    +
    
    14
    +#include "RtsSignals.h"
    
    15
    +#include "IOManSignals.h"
    
    16
    +#include "RtsUtils.h"
    
    17
    +#include "Schedule.h"
    
    18
    +#include "Prelude.h"
    
    19
    +#include "ThreadLabels.h"
    
    20
    +#include "Libdw.h"
    
    21
    +#include "MIO.h"
    
    22
    +
    
    23
    +#include <unistd.h>
    
    24
    +#include <signal.h>
    
    25
    +#include <fcntl.h>
    
    26
    +#include <string.h>
    
    27
    +
    
    28
    +/* -----------------------------------------------------------------------------
    
    29
    + * The table of signal handlers
    
    30
    + * -------------------------------------------------------------------------- */
    
    31
    +
    
    32
    +/* SUP: The type of handlers is a little bit, well, doubtful... */
    
    33
    +StgInt *signal_handlers = NULL; /* Dynamically grown array of signal handlers */
    
    34
    +static StgInt nHandlers = 0;    /* Size of handlers array */
    
    35
    +
    
    36
    +static sigset_t userSignals;
    
    37
    +static sigset_t savedSignals;
    
    38
    +
    
    39
    +#if defined(THREADED_RTS)
    
    40
    +static Mutex sig_mutex; // protects signal_handlers, nHandlers
    
    41
    +#endif
    
    42
    +
    
    43
    +/* -----------------------------------------------------------------------------
    
    44
    + * Initialisation / deinitialisation
    
    45
    + * -------------------------------------------------------------------------- */
    
    46
    +
    
    47
    +void
    
    48
    +initUserSignals(void)
    
    49
    +{
    
    50
    +    sigemptyset(&userSignals);
    
    51
    +#if defined(THREADED_RTS)
    
    52
    +    initMutex(&sig_mutex);
    
    53
    +#endif
    
    54
    +}
    
    55
    +
    
    56
    +void finiUserSignals(void)
    
    57
    +{
    
    58
    +    if (signal_handlers != NULL) {
    
    59
    +        stgFree(signal_handlers);
    
    60
    +        signal_handlers = NULL;
    
    61
    +        nHandlers = 0;
    
    62
    +    }
    
    63
    +#if defined(THREADED_RTS)
    
    64
    +    closeMutex(&sig_mutex);
    
    65
    +#endif
    
    66
    +};
    
    67
    +
    
    68
    +/* -----------------------------------------------------------------------------
    
    69
    + * Allocate/resize the table of signal handlers.
    
    70
    + * -------------------------------------------------------------------------- */
    
    71
    +
    
    72
    +static void
    
    73
    +more_handlers(int sig)
    
    74
    +{
    
    75
    +    StgInt i;
    
    76
    +
    
    77
    +    if (sig < nHandlers)
    
    78
    +        return;
    
    79
    +
    
    80
    +    if (signal_handlers == NULL)
    
    81
    +        signal_handlers = (StgInt *)stgMallocBytes((sig + 1) * sizeof(StgInt),
    
    82
    +                                                   "more_handlers");
    
    83
    +    else
    
    84
    +        signal_handlers = (StgInt *)stgReallocBytes(signal_handlers,
    
    85
    +                                                    (sig + 1) * sizeof(StgInt),
    
    86
    +                                                    "more_handlers");
    
    87
    +
    
    88
    +    for(i = nHandlers; i <= sig; i++)
    
    89
    +        // Fill in the new slots with default actions
    
    90
    +        signal_handlers[i] = STG_SIG_DFL;
    
    91
    +
    
    92
    +    nHandlers = sig + 1;
    
    93
    +}
    
    94
    +
    
    95
    +#if !defined(THREADED_RTS)
    
    96
    +
    
    97
    +#define N_PENDING_HANDLERS 16
    
    98
    +
    
    99
    +siginfo_t pending_handler_buf[N_PENDING_HANDLERS];
    
    100
    +siginfo_t *next_pending_handler = pending_handler_buf;
    
    101
    +
    
    102
    +static inline bool signals_pending(void) {
    
    103
    +    return (next_pending_handler != pending_handler_buf);
    
    104
    +}
    
    105
    +
    
    106
    +#endif /* THREADED_RTS */
    
    107
    +
    
    108
    +/* -----------------------------------------------------------------------------
    
    109
    + * Low-level signal handler
    
    110
    + *
    
    111
    + * Places the requested handler on a stack of pending handlers to be
    
    112
    + * started up at the next context switch.
    
    113
    + * -------------------------------------------------------------------------- */
    
    114
    +
    
    115
    +#if !defined(THREADED_RTS)
    
    116
    +void
    
    117
    +generic_handler(int sig USED_IF_THREADS,
    
    118
    +                siginfo_t *info,
    
    119
    +                void *p STG_UNUSED)
    
    120
    +{
    
    121
    +
    
    122
    +    /* Can't call allocate from here.  Probably can't call malloc
    
    123
    +       either.  However, we have to schedule a new thread somehow.
    
    124
    +
    
    125
    +       It's probably ok to request a context switch and allow the
    
    126
    +       scheduler to  start the handler thread, but how do we
    
    127
    +       communicate this to the scheduler?
    
    128
    +
    
    129
    +       We need some kind of locking, but with low overhead (i.e. no
    
    130
    +       blocking signals every time around the scheduler).
    
    131
    +
    
    132
    +       Signal Handlers are atomic (i.e. they can't be interrupted), and
    
    133
    +       we can make use of this.  We just need to make sure the
    
    134
    +       critical section of the scheduler can't be interrupted - the
    
    135
    +       only way to do this is to block signals.  However, we can lower
    
    136
    +       the overhead by only blocking signals when there are any
    
    137
    +       handlers to run, i.e. the set of pending handlers is
    
    138
    +       non-empty.
    
    139
    +    */
    
    140
    +
    
    141
    +    /* We use a stack to store the pending signals.  We can't
    
    142
    +       dynamically grow this since we can't allocate any memory from
    
    143
    +       within a signal handler.
    
    144
    +
    
    145
    +       Hence unfortunately we have to bomb out if the buffer
    
    146
    +       overflows.  It might be acceptable to carry on in certain
    
    147
    +       circumstances, depending on the signal.
    
    148
    +    */
    
    149
    +
    
    150
    +    memcpy(next_pending_handler, info, sizeof(siginfo_t));
    
    151
    +
    
    152
    +    next_pending_handler++;
    
    153
    +
    
    154
    +    // stack full?
    
    155
    +    if (next_pending_handler == &pending_handler_buf[N_PENDING_HANDLERS]) {
    
    156
    +        errorBelch("too many pending signals");
    
    157
    +        stg_exit(EXIT_FAILURE);
    
    158
    +    }
    
    159
    +
    
    160
    +    interruptCapability(&MainCapability);
    
    161
    +
    
    162
    +}
    
    163
    +#endif /* THREADED_RTS */
    
    164
    +
    
    165
    +/* -----------------------------------------------------------------------------
    
    166
    + * Blocking/Unblocking of the user signals
    
    167
    + * -------------------------------------------------------------------------- */
    
    168
    +
    
    169
    +void
    
    170
    +blockUserSignals(void)
    
    171
    +{
    
    172
    +    pthread_sigmask(SIG_BLOCK, &userSignals, &savedSignals);
    
    173
    +}
    
    174
    +
    
    175
    +void
    
    176
    +unblockUserSignals(void)
    
    177
    +{
    
    178
    +    pthread_sigmask(SIG_SETMASK, &savedSignals, NULL);
    
    179
    +}
    
    180
    +
    
    181
    +/* -----------------------------------------------------------------------------
    
    182
    + * Install a Haskell signal handler.
    
    183
    + *
    
    184
    + * We should really do this in Haskell in GHC.Conc, and share the
    
    185
    + * signal_handlers array with the one there.
    
    186
    + *
    
    187
    + * -------------------------------------------------------------------------- */
    
    188
    +
    
    189
    +int
    
    190
    +installSignalHandler(int sig, int spi, sigset_t *mask, bool nocldstop,
    
    191
    +                     SignalHandler signal_handler)
    
    192
    +{
    
    193
    +    ACQUIRE_LOCK(&sig_mutex);
    
    194
    +
    
    195
    +    // Use sigaddset as a way to validate the signal number.
    
    196
    +    // We do not use the resulting signal set.
    
    197
    +    {
    
    198
    +        sigset_t signals;
    
    199
    +        sigemptyset(&signals);
    
    200
    +        if (sig < 0 || sigaddset(&signals, sig)) {
    
    201
    +            RELEASE_LOCK(&sig_mutex);
    
    202
    +            return STG_SIG_ERR;
    
    203
    +        }
    
    204
    +    }
    
    205
    +
    
    206
    +    more_handlers(sig);
    
    207
    +
    
    208
    +    StgInt previous_spi = signal_handlers[sig];
    
    209
    +
    
    210
    +    struct sigaction action;
    
    211
    +    memset(&action, 0, sizeof(struct sigaction));
    
    212
    +    action.sa_flags = 0;
    
    213
    +
    
    214
    +    switch(spi) {
    
    215
    +    case STG_SIG_IGN:
    
    216
    +        action.sa_handler = SIG_IGN;
    
    217
    +        break;
    
    218
    +
    
    219
    +    case STG_SIG_DFL:
    
    220
    +        action.sa_handler = SIG_DFL;
    
    221
    +        break;
    
    222
    +
    
    223
    +    case STG_SIG_RST:
    
    224
    +        action.sa_flags |= SA_RESETHAND;
    
    225
    +        /* fall through */
    
    226
    +    case STG_SIG_HAN:
    
    227
    +        action.sa_sigaction = signal_handler;
    
    228
    +        action.sa_flags |= SA_SIGINFO;
    
    229
    +        break;
    
    230
    +
    
    231
    +    default:
    
    232
    +        barf("stg_sig_install: bad spi");
    
    233
    +    }
    
    234
    +
    
    235
    +    if (mask != NULL) {
    
    236
    +        action.sa_mask = *mask;
    
    237
    +    } else {
    
    238
    +        sigemptyset(&action.sa_mask);
    
    239
    +    }
    
    240
    +
    
    241
    +    action.sa_flags |= sig == SIGCHLD && nocldstop ? SA_NOCLDSTOP : 0;
    
    242
    +
    
    243
    +    if (sigaction(sig, &action, NULL)) {
    
    244
    +        errorBelch("sigaction");
    
    245
    +        RELEASE_LOCK(&sig_mutex);
    
    246
    +        return STG_SIG_ERR;
    
    247
    +    }
    
    248
    +
    
    249
    +    signal_handlers[sig] = spi;
    
    250
    +
    
    251
    +    switch(spi) {
    
    252
    +    case STG_SIG_RST:
    
    253
    +    case STG_SIG_HAN:
    
    254
    +        sigaddset(&userSignals, sig);
    
    255
    +        break;
    
    256
    +
    
    257
    +    default:
    
    258
    +        sigdelset(&userSignals, sig);
    
    259
    +        break;
    
    260
    +    }
    
    261
    +
    
    262
    +    RELEASE_LOCK(&sig_mutex);
    
    263
    +    return previous_spi;
    
    264
    +}
    
    265
    +
    
    266
    +/* -----------------------------------------------------------------------------
    
    267
    + * Creating new threads for signal handlers.
    
    268
    + * -------------------------------------------------------------------------- */
    
    269
    +
    
    270
    +#if !defined(THREADED_RTS)
    
    271
    +void
    
    272
    +startPendingSignalHandlers(Capability *cap)
    
    273
    +{
    
    274
    +  siginfo_t *info;
    
    275
    +  int sig;
    
    276
    +
    
    277
    +  if (!signals_pending()) return;
    
    278
    +
    
    279
    +  blockUserSignals();
    
    280
    +
    
    281
    +  while (next_pending_handler != pending_handler_buf) {
    
    282
    +
    
    283
    +    next_pending_handler--;
    
    284
    +
    
    285
    +    sig = next_pending_handler->si_signo;
    
    286
    +    if (signal_handlers[sig] == STG_SIG_DFL) {
    
    287
    +        continue; // handler has been changed.
    
    288
    +    }
    
    289
    +
    
    290
    +    info = next_pending_handler;
    
    291
    +    startSignalHandler(cap, sig, info);
    
    292
    +  }
    
    293
    +
    
    294
    +  unblockUserSignals();
    
    295
    +}
    
    296
    +#endif
    
    297
    +
    
    298
    +void startSignalHandler(Capability *cap, int sig, siginfo_t *info)
    
    299
    +{
    
    300
    +    /* Allocate a copy. This is freed by runHandler. */
    
    301
    +    // TODO: this could be improved to use a GC'd pinned byte array, but this
    
    302
    +    // would need coordination with the unix library.
    
    303
    +    siginfo_t *info_copy = stgMallocBytes(sizeof(siginfo_t), "startSignalHandler");
    
    304
    +    *info_copy = *info;
    
    305
    +
    
    306
    +    /* The info structure can be zero on solaris. So at least set the signo. */
    
    307
    +    info_copy->si_signo = sig;
    
    308
    +
    
    309
    +    debugTrace(DEBUG_iomanager,
    
    310
    +               "Handling pending signal, no %d", info_copy->si_signo);
    
    311
    +
    
    312
    +    StgTSO *t =
    
    313
    +        createIOThread(cap,
    
    314
    +                       RtsFlags.GcFlags.initialStkSize,
    
    315
    +                       rts_apply(cap,
    
    316
    +                                 rts_apply(cap,
    
    317
    +                                           runHandlersPtr_closure,
    
    318
    +                                           rts_mkPtr(cap, info_copy)),
    
    319
    +                                 rts_mkInt(cap, info_copy->si_signo)));
    
    320
    +    scheduleThread(cap, t);
    
    321
    +    setThreadLabel(cap, t, "signal handler thread");
    
    322
    +}
    
    323
    +
    
    324
    +void newNonBlockingPipe(int *pipe_fd_r, int *pipe_fd_w)
    
    325
    +{
    
    326
    +    int pipefd[2];
    
    327
    +#if defined(HAVE_PIPE2)
    
    328
    +    int res = pipe2(pipefd, O_CLOEXEC | O_NONBLOCK);
    
    329
    +#else
    
    330
    +    int res = pipe(pipefd);
    
    331
    +#endif
    
    332
    +    if (RTS_UNLIKELY(res < 0)) {
    
    333
    +        sysErrorBelch("newNonBlockingPipe");
    
    334
    +        stg_exit(EXIT_FAILURE);
    
    335
    +    }
    
    336
    +#if !defined(HAVE_PIPE2)
    
    337
    +    for (int i = 0; i < 2; i++) {
    
    338
    +        int res1 = fcntl(pipefd[i], F_SETFD, FD_CLOEXEC);
    
    339
    +        int res2 = fcntl(pipefd[i], F_SETFL, O_NONBLOCK);
    
    340
    +        if (RTS_UNLIKELY(res1 < 0 || res2 < 0)) {
    
    341
    +            sysErrorBelch("newFdWakeup fcntl()");
    
    342
    +            stg_exit(EXIT_FAILURE);
    
    343
    +        }
    
    344
    +    }
    
    345
    +#endif
    
    346
    +    *pipe_fd_r = pipefd[0]; /* read end */
    
    347
    +    *pipe_fd_w = pipefd[1]; /* write end */
    
    348
    +}
    
    349
    +
    
    350
    +/* Write the signal number and siginfo into the given fd (which is
    
    351
    + * typically a pipe in non-blocking mode).
    
    352
    + */
    
    353
    +void writeSignalInfoToPipe(int sig, siginfo_t *info, int fd)
    
    354
    +{
    
    355
    +    StgWord8 buf[sizeof(siginfo_t) + 1];
    
    356
    +
    
    357
    +    buf[0] = sig;
    
    358
    +    if (info) {
    
    359
    +        memcpy(buf+1, info, sizeof(siginfo_t));
    
    360
    +    } else {
    
    361
    +        // info may be NULL on Solaris (see #3790)
    
    362
    +        memset(buf+1, 0, sizeof(siginfo_t));
    
    363
    +    }
    
    364
    +
    
    365
    +    int r = write(fd, buf, sizeof(siginfo_t)+1);
    
    366
    +    if (r == -1 && errno == EAGAIN) {
    
    367
    +        errorBelch("lost signal due to full pipe: %d\n", sig);
    
    368
    +    }
    
    369
    +}
    
    370
    +
    
    371
    +bool readSignalInfoFromPipe(int *sig, siginfo_t *info, int fd)
    
    372
    +{
    
    373
    +    StgWord8 buf[sizeof(siginfo_t) + 1];
    
    374
    +
    
    375
    +    int r = read(fd, buf, sizeof(siginfo_t)+1);
    
    376
    +    
    
    377
    +    if (r == sizeof(siginfo_t)+1) {
    
    378
    +        *sig = (int) buf[0];
    
    379
    +        memcpy(info, buf+1, sizeof(siginfo_t));
    
    380
    +        return true;
    
    381
    +    } else {
    
    382
    +        // TODO: if r is >0 and < expected then we're kind of screwed
    
    383
    +        // it means we're all out of sync with the protocol.
    
    384
    +        // it _should_ never happen of course!
    
    385
    +        return false;
    
    386
    +    }
    
    387
    +}
    
    388
    +
    
    389
    +#endif /* RTS_USER_SIGNALS */

  • rts/posix/IOManSignals.h
    1
    +/* -----------------------------------------------------------------------------
    
    2
    + *
    
    3
    + * (c) The GHC Team, 1998-2005, 2026
    
    4
    + *
    
    5
    + * Shared utilities for I/O managers: signal handling functionality.
    
    6
    + *
    
    7
    + * ---------------------------------------------------------------------------*/
    
    8
    +
    
    9
    +#pragma once
    
    10
    +
    
    11
    +#if defined(RTS_USER_SIGNALS)
    
    12
    +
    
    13
    +#include <signal.h>
    
    14
    +
    
    15
    +#include "BeginPrivate.h"
    
    16
    +
    
    17
    +/*
    
    18
    + * Function: initUserSignals()
    
    19
    + *
    
    20
    + * Initialize the console handling substrate.
    
    21
    + */
    
    22
    +void initUserSignals(void);
    
    23
    +
    
    24
    +/* Finalise signal and console events handling.
    
    25
    + */
    
    26
    +void finiUserSignals(void);
    
    27
    +
    
    28
    +#if !defined(THREADED_RTS)
    
    29
    +/*
    
    30
    + * Function: startPendingSignalHandlers()
    
    31
    + *
    
    32
    + * If there are any queued up posix signals, run the handlers
    
    33
    + * associated with them.
    
    34
    + */
    
    35
    +void startPendingSignalHandlers(Capability *cap);
    
    36
    +#endif
    
    37
    +
    
    38
    +void startSignalHandler(Capability *cap, int sig, siginfo_t *info);
    
    39
    +
    
    40
    +typedef void (*SignalHandler)(int sig, siginfo_t *info, void *p);
    
    41
    +
    
    42
    +int installSignalHandler(int sig, int spi, sigset_t *mask, bool nocldstop,
    
    43
    +                         SignalHandler handler);
    
    44
    +
    
    45
    +/* Utilities to help with the self-pipe trick for signal handling. */
    
    46
    +void newNonBlockingPipe(int *pipe_fd_r, int *pipe_fd_w);
    
    47
    +void writeSignalInfoToPipe (int  sig, siginfo_t *info, int fd);
    
    48
    +bool readSignalInfoFromPipe(int *sig, siginfo_t *info, int fd);
    
    49
    +
    
    50
    +/* Temporary */
    
    51
    +void generic_handler(int sig USED_IF_THREADS,
    
    52
    +                     siginfo_t *info,
    
    53
    +                     void *p STG_UNUSED);
    
    54
    +
    
    55
    +#endif
    
    56
    +
    
    57
    +#include "EndPrivate.h"
    
    58
    +

  • rts/posix/MIO.c
    ... ... @@ -13,15 +13,19 @@
    13 13
     #include "rts/PosixSource.h"
    
    14 14
     #include "Rts.h"
    
    15 15
     
    
    16
    +#include "IOManager.h"
    
    17
    +
    
    18
    +#if defined(IOMGR_ENABLED_MIO_POSIX)
    
    19
    +
    
    20
    +#include "MIO.h"
    
    21
    +#include "IOManagerInternals.h"
    
    22
    +#include "IOManSignals.h"
    
    23
    +
    
    16 24
     #include "Schedule.h"
    
    17 25
     #include "RtsUtils.h"
    
    18 26
     #include "Prelude.h"
    
    19 27
     #include "ThreadLabels.h"
    
    20 28
     
    
    21
    -#include "MIO.h"
    
    22
    -#include "IOManager.h"
    
    23
    -#include "IOManagerInternals.h"
    
    24
    -
    
    25 29
     #if defined(HAVE_ERRNO_H)
    
    26 30
     # include <errno.h>
    
    27 31
     #endif
    
    ... ... @@ -29,62 +33,65 @@
    29 33
     #include <stdlib.h>
    
    30 34
     #include <unistd.h>
    
    31 35
     
    
    32
    -// Here's the pipe into which we will send our signals
    
    33
    -static int timer_manager_control_wr_fd = -1;
    
    34
    -// TODO: Eliminate these globals. Put then into the CapIOManager, but the
    
    35
    -// problem is these are shared across all caps, not per cap.
    
    36
    -
    
    37 36
     #define IO_MANAGER_DIE    0xfe
    
    38 37
     
    
    38
    +void initCapabilityIOManagerMIO(CapIOManager *iomgr)
    
    39
    +{
    
    40
    +    iomgr->control_fd = -1;
    
    41
    +    if (iomgr->cap == 0) {
    
    42
    +        iomgr->shared->timer_manager_control_wr_fd = -1;
    
    43
    +    }
    
    44
    +}
    
    45
    +
    
    39 46
     void setTimerManagerControlFd(int fd) {
    
    40
    -    RELAXED_STORE(&timer_manager_control_wr_fd, fd);
    
    47
    +    int *timer_manager_control_wr_fd =
    
    48
    +            &MainCapability.iomgr->shared->timer_manager_control_wr_fd;
    
    49
    +    RELAXED_STORE(timer_manager_control_wr_fd, fd);
    
    41 50
     }
    
    42 51
     
    
    43
    -#if defined(THREADED_RTS)
    
    44
    -void timerManagerNotifySignal(int sig, siginfo_t *info)
    
    52
    +static void signal_handler(int sig, siginfo_t *info, void *p STG_UNUSED)
    
    45 53
     {
    
    46
    -    StgWord8 buf[sizeof(siginfo_t) + 1];
    
    47
    -    int r;
    
    48
    -
    
    49
    -    buf[0] = sig;
    
    50
    -    if (info == NULL) {
    
    51
    -        // info may be NULL on Solaris (see #3790)
    
    52
    -        memset(buf+1, 0, sizeof(siginfo_t));
    
    53
    -    } else {
    
    54
    -        memcpy(buf+1, info, sizeof(siginfo_t));
    
    55
    -    }
    
    56
    -
    
    57
    -    int timer_control_fd = RELAXED_LOAD(&timer_manager_control_wr_fd);
    
    54
    +    /* In the signal handler we do not have Capability context, so access the
    
    55
    +     * control fd via MainCapability.
    
    56
    +     */
    
    57
    +    int *timer_manager_control_wr_fd =
    
    58
    +            &MainCapability.iomgr->shared->timer_manager_control_wr_fd;
    
    59
    +    int timer_control_fd = RELAXED_LOAD(timer_manager_control_wr_fd);
    
    58 60
         if (0 <= timer_control_fd)
    
    59 61
         {
    
    60
    -        r = write(timer_control_fd, buf, sizeof(siginfo_t)+1);
    
    61
    -        if (r == -1 && errno == EAGAIN) {
    
    62
    -            errorBelch("lost signal due to full pipe: %d\n", sig);
    
    63
    -        }
    
    62
    +        writeSignalInfoToPipe(sig, info, timer_control_fd);
    
    64 63
         }
    
    65 64
     
    
    66 65
         // If the IO manager hasn't told us what the FD of the write end
    
    67 66
         // of its pipe is, there's not much we can do here, so just ignore
    
    68 67
         // the signal..
    
    69 68
     }
    
    70
    -#endif
    
    71 69
     
    
    70
    +int installUserSignalHandlerMIO(int sig, int spi, void *mask,
    
    71
    +                                bool nocldstop)
    
    72
    +{
    
    73
    +    return installSignalHandler(sig, spi, mask, nocldstop, signal_handler);
    
    74
    +}
    
    72 75
     
    
    73
    -#if defined(THREADED_RTS)
    
    74 76
     void
    
    75 77
     ioManagerDie (void)
    
    76 78
     {
    
    79
    +    finiUserSignals();
    
    80
    +
    
    81
    +    int *timer_manager_control_wr_fd =
    
    82
    +            &MainCapability.iomgr->shared->timer_manager_control_wr_fd;
    
    83
    +
    
    77 84
         StgWord8 byte = (StgWord8)IO_MANAGER_DIE;
    
    78 85
         uint32_t i;
    
    79 86
         int r;
    
    80 87
     
    
    81 88
         {
    
    82 89
             // Shut down timer manager
    
    83
    -        const int fd = RELAXED_LOAD(&timer_manager_control_wr_fd);
    
    90
    +        const int fd = RELAXED_LOAD(timer_manager_control_wr_fd);
    
    84 91
             if (0 <= fd) {
    
    85 92
                 r = write(fd, &byte, 1);
    
    86 93
                 if (r == -1) { sysErrorBelch("ioManagerDie: write"); }
    
    87
    -            RELAXED_STORE(&timer_manager_control_wr_fd, -1);
    
    94
    +            RELAXED_STORE(timer_manager_control_wr_fd, -1);
    
    88 95
             }
    
    89 96
         }
    
    90 97
     
    
    ... ... @@ -110,13 +117,23 @@ ioManagerStartCap (Capability **cap)
    110 117
     void
    
    111 118
     ioManagerStart (void)
    
    112 119
     {
    
    120
    +    initUserSignals();
    
    121
    +
    
    122
    +    int *timer_manager_control_wr_fd =
    
    123
    +            &MainCapability.iomgr->shared->timer_manager_control_wr_fd;
    
    124
    +
    
    113 125
         // Make sure the IO manager thread is running
    
    114 126
         Capability *cap;
    
    115
    -    if (SEQ_CST_LOAD(&timer_manager_control_wr_fd) < 0) {
    
    127
    +    if (SEQ_CST_LOAD(timer_manager_control_wr_fd) < 0) {
    
    116 128
             cap = rts_lock();
    
    117 129
             ioManagerStartCap(&cap);
    
    118 130
             rts_unlock(cap);
    
    119 131
         }
    
    120 132
     }
    
    121
    -#endif
    
    122 133
     
    
    134
    +#else // !IOMGR_ENABLED_MIO_POSIX
    
    135
    +
    
    136
    +/* This is exported from the RTS unconditionally, but only used for MIO. */
    
    137
    +void setTimerManagerControlFd(int fd) { /* do nothing */ }
    
    138
    +
    
    139
    +#endif // IOMGR_ENABLED_MIO_POSIX

  • rts/posix/MIO.h
    ... ... @@ -19,11 +19,14 @@
    19 19
     /* Communicating with the IO manager thread (see GHC.Conc).
    
    20 20
      */
    
    21 21
     #if defined(THREADED_RTS)
    
    22
    +void initCapabilityIOManagerMIO(CapIOManager *iomgr);
    
    23
    +
    
    22 24
     void ioManagerDie (void);
    
    23 25
     void ioManagerStart (void);
    
    24 26
     void ioManagerStartCap (/* inout */ Capability **cap);
    
    25 27
     
    
    26
    -void timerManagerNotifySignal(int sig, siginfo_t *info);
    
    28
    +int installUserSignalHandlerMIO(int sig, int spi, void *mask,
    
    29
    +                                bool nocldstop);
    
    27 30
     #endif
    
    28 31
     
    
    29 32
     #include "EndPrivate.h"

  • rts/posix/Poll.c
    ... ... @@ -23,7 +23,6 @@
    23 23
     #include "Trace.h"
    
    24 24
     
    
    25 25
     #include "Poll.h"
    
    26
    -#include "RtsSignals.h"
    
    27 26
     
    
    28 27
     #include <limits.h>
    
    29 28
     #include <errno.h>
    
    ... ... @@ -39,6 +38,7 @@
    39 38
     #endif
    
    40 39
     
    
    41 40
     #include "IOManagerInternals.h"
    
    41
    +#include "IOManSignals.h"
    
    42 42
     #include "Timeout.h"
    
    43 43
     #include "FdWakeup.h"
    
    44 44
     
    
    ... ... @@ -158,11 +158,23 @@ void initCapabilityIOManagerPoll(CapIOManager *iomgr)
    158 158
                                       .revents = 0
    
    159 159
                                     };
    
    160 160
         iomgr->aiop_poll_table = iomgr->full_poll_table+1; /* hence empty */
    
    161
    +
    
    162
    +#if defined(RTS_USER_SIGNALS)
    
    163
    +    /* Signal handlers are shared across all caps. Only init once. */
    
    164
    +    if (iomgr->cap->no == 0) {
    
    165
    +        initUserSignals();
    
    166
    +    }
    
    167
    +#endif
    
    161 168
     }
    
    162 169
     
    
    163 170
     
    
    164 171
     void freeCapabilityIOManagerPoll(CapIOManager *iomgr)
    
    165 172
     {
    
    173
    +#if defined(RTS_USER_SIGNALS)
    
    174
    +    if (iomgr->cap->no == 0) {
    
    175
    +        finiUserSignals();
    
    176
    +    }
    
    177
    +#endif
    
    166 178
         stgFree(iomgr->full_poll_table);
    
    167 179
     #if defined(HAVE_PREEMPTION)
    
    168 180
         closeFdWakeup(iomgr->interrupt_fd_r, iomgr->interrupt_fd_w);
    
    ... ... @@ -573,6 +585,13 @@ void interruptIOManagerPoll(CapIOManager *iomgr USED_IF_PREEMPTION)
    573 585
     }
    
    574 586
     
    
    575 587
     
    
    588
    +int installUserSignalHandlerPoll(int sig, int spi, void *mask,
    
    589
    +                                 bool nocldstop)
    
    590
    +{
    
    591
    +    return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
    
    592
    +}
    
    593
    +
    
    594
    +
    
    576 595
     /* Helper function to double the size of the aiop_table and aiop_poll_table.
    
    577 596
      */
    
    578 597
     static bool enlargeTables(CapIOManager *iomgr)
    

  • rts/posix/Poll.h
    ... ... @@ -34,6 +34,9 @@ void pollCompletedTimeoutsOrIOPoll(CapIOManager *iomgr);
    34 34
     bool awaitCompletedTimeoutsOrIOPoll(CapIOManager *iomgr);
    
    35 35
     void interruptIOManagerPoll(CapIOManager *iomgr);
    
    36 36
     
    
    37
    +/* Signals */
    
    38
    +int  installUserSignalHandlerPoll(int sig, int spi, void *mask, bool nocldstop);
    
    39
    +
    
    37 40
     #endif /* IOMGR_ENABLED_POLL */
    
    38 41
     
    
    39 42
     #include "EndPrivate.h"
    

  • rts/posix/Select.c
    ... ... @@ -12,13 +12,13 @@
    12 12
     #include "rts/PosixSource.h"
    
    13 13
     #include "Rts.h"
    
    14 14
     
    
    15
    -#include "RtsSignals.h"
    
    16 15
     #include "Schedule.h"
    
    17 16
     #include "Prelude.h"
    
    18 17
     #include "RtsUtils.h"
    
    19 18
     #include "Capability.h"
    
    20 19
     #include "Select.h"
    
    21 20
     #include "IOManagerInternals.h"
    
    21
    +#include "IOManSignals.h"
    
    22 22
     #include "Stats.h"
    
    23 23
     #include "GetTime.h"
    
    24 24
     #include "FdWakeup.h"
    
    ... ... @@ -72,10 +72,16 @@ void initCapabilityIOManagerSelect(CapIOManager *iomgr)
    72 72
             barf("initCapabilityIOManagerSelect: fds out of select range");
    
    73 73
         }
    
    74 74
     #endif
    
    75
    +#if defined(RTS_USER_SIGNALS)
    
    76
    +    initUserSignals();
    
    77
    +#endif
    
    75 78
     }
    
    76 79
     
    
    77 80
     void freeCapabilityIOManagerSelect(CapIOManager *iomgr USED_IF_PREEMPTION)
    
    78 81
     {
    
    82
    +#if defined(RTS_USER_SIGNALS)
    
    83
    +    finiUserSignals();
    
    84
    +#endif
    
    79 85
     #if defined(HAVE_PREEMPTION)
    
    80 86
         closeFdWakeup(iomgr->interrupt_fd_r, iomgr->interrupt_fd_w);
    
    81 87
     #endif
    
    ... ... @@ -518,4 +524,10 @@ awaitCompletedTimeoutsOrIOSelect(CapIOManager *iomgr, bool wait)
    518 524
         return !interrupt;
    
    519 525
     }
    
    520 526
     
    
    527
    +int installUserSignalHandlerSelect(int sig, int spi, void *mask,
    
    528
    +                                   bool nocldstop)
    
    529
    +{
    
    530
    +    return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
    
    531
    +}
    
    532
    +
    
    521 533
     #endif /* IOMGR_ENABLED_SELECT */

  • rts/posix/Select.h
    ... ... @@ -21,6 +21,8 @@ void wakeupIOManagerSelect(CapIOManager *iomgr);
    21 21
     
    
    22 22
     bool awaitCompletedTimeoutsOrIOSelect(CapIOManager *iomgr, bool wait);
    
    23 23
     void interruptIOManagerSelect(CapIOManager *iomgr);
    
    24
    +int  installUserSignalHandlerSelect(int sig, int spi, void *mask,
    
    25
    +                                    bool nocldstop);
    
    24 26
     
    
    25 27
     #include "EndPrivate.h"
    
    26 28
     

  • rts/posix/SelectBis.c
    ... ... @@ -25,12 +25,12 @@
    25 25
     #include "Trace.h"
    
    26 26
     
    
    27 27
     #include "SelectBis.h"
    
    28
    -#include "RtsSignals.h"
    
    29 28
     
    
    30 29
     #include <errno.h>
    
    31 30
     #include <sys/select.h>
    
    32 31
     
    
    33 32
     #include "IOManagerInternals.h"
    
    33
    +#include "IOManSignals.h"
    
    34 34
     #include "Timeout.h"
    
    35 35
     #include "FdWakeup.h"
    
    36 36
     
    
    ... ... @@ -153,11 +153,23 @@ void initCapabilityIOManagerSelectBis(CapIOManager *iomgr)
    153 153
     
    
    154 154
         iomgr->rfds = stgMallocBytes(sizeof (fd_set), "IOManagerSelectBis");
    
    155 155
         iomgr->wfds = stgMallocBytes(sizeof (fd_set), "IOManagerSelectBis");
    
    156
    +
    
    157
    +#if defined(RTS_USER_SIGNALS)
    
    158
    +    /* Signal handlers are shared across all caps. Only init once. */
    
    159
    +    if (iomgr->cap->no == 0) {
    
    160
    +        initUserSignals();
    
    161
    +    }
    
    162
    +#endif
    
    156 163
     }
    
    157 164
     
    
    158 165
     
    
    159 166
     void freeCapabilityIOManagerSelectBis(CapIOManager *iomgr)
    
    160 167
     {
    
    168
    +#if defined(RTS_USER_SIGNALS)
    
    169
    +    if (iomgr->cap->no == 0) {
    
    170
    +        finiUserSignals();
    
    171
    +    }
    
    172
    +#endif
    
    161 173
         stgFree(iomgr->rfds);
    
    162 174
         stgFree(iomgr->wfds);
    
    163 175
     #if defined(HAVE_PREEMPTION)
    
    ... ... @@ -583,6 +595,13 @@ void interruptIOManagerSelectBis(CapIOManager *iomgr USED_IF_PREEMPTION)
    583 595
     }
    
    584 596
     
    
    585 597
     
    
    598
    +int installUserSignalHandlerSelectBis(int sig, int spi, void *mask,
    
    599
    +                                      bool nocldstop)
    
    600
    +{
    
    601
    +    return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
    
    602
    +}
    
    603
    +
    
    604
    +
    
    586 605
     /* Helper function to double the size of the aiop_table.
    
    587 606
      */
    
    588 607
     static bool enlargeTables(CapIOManager *iomgr)
    

  • rts/posix/SelectBis.h
    ... ... @@ -55,6 +55,10 @@ void pollCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr);
    55 55
     bool awaitCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr);
    
    56 56
     void interruptIOManagerSelectBis(CapIOManager *iomgr);
    
    57 57
     
    
    58
    +/* Signals */
    
    59
    +int  installUserSignalHandlerSelectBis(int sig, int spi, void *mask,
    
    60
    +                                       bool nocldstop);
    
    61
    +
    
    58 62
     #endif /* IOMGR_ENABLED_SELECTBIS */
    
    59 63
     
    
    60 64
     #include "EndPrivate.h"
    

  • rts/posix/Signals.c
    ... ... @@ -9,6 +9,8 @@
    9 9
     #include "rts/PosixSource.h"
    
    10 10
     #include "Rts.h"
    
    11 11
     
    
    12
    +#if defined(RTS_USER_SIGNALS)
    
    13
    +
    
    12 14
     #include "RtsSignals.h"
    
    13 15
     #include "RtsUtils.h"
    
    14 16
     #include "Schedule.h"
    
    ... ... @@ -25,344 +27,46 @@
    25 27
     # endif
    
    26 28
     #endif
    
    27 29
     
    
    28
    -#if defined(HAVE_UNISTD_H)
    
    29
    -# include <unistd.h>
    
    30
    -#endif
    
    31
    -
    
    32
    -#if defined(HAVE_SIGNAL_H)
    
    33
    -# include <signal.h>
    
    34
    -#endif
    
    35
    -
    
    36
    -#if defined(HAVE_TERMIOS_H)
    
    30
    +#include <unistd.h>
    
    31
    +#include <signal.h>
    
    37 32
     #include <termios.h>
    
    38
    -#endif
    
    39
    -
    
    40 33
     #include <string.h>
    
    41 34
     
    
    42
    -/* This curious flag is provided for the benefit of the Haskell binding
    
    43
    - * to POSIX.1 to control whether or not to include SA_NOCLDSTOP when
    
    44
    - * installing a SIGCHLD handler.
    
    45
    - */
    
    46
    -HsInt nocldstop = 0;
    
    47
    -
    
    48
    -/* -----------------------------------------------------------------------------
    
    49
    - * The table of signal handlers
    
    50
    - * -------------------------------------------------------------------------- */
    
    51
    -
    
    52
    -#if defined(RTS_USER_SIGNALS)
    
    53
    -
    
    54
    -/* SUP: The type of handlers is a little bit, well, doubtful... */
    
    55
    -StgInt *signal_handlers = NULL; /* Dynamically grown array of signal handlers */
    
    56
    -static StgInt nHandlers = 0;    /* Size of handlers array */
    
    57
    -
    
    58
    -static sigset_t userSignals;
    
    59
    -static sigset_t savedSignals;
    
    60
    -
    
    61
    -#if defined(THREADED_RTS)
    
    62
    -static Mutex sig_mutex; // protects signal_handlers, nHandlers
    
    63
    -#endif
    
    64
    -
    
    65
    -/* -----------------------------------------------------------------------------
    
    66
    - * Initialisation / deinitialisation
    
    67
    - * -------------------------------------------------------------------------- */
    
    68
    -
    
    69
    -void
    
    70
    -initUserSignals(void)
    
    71
    -{
    
    72
    -    sigemptyset(&userSignals);
    
    73
    -#if defined(THREADED_RTS)
    
    74
    -    initMutex(&sig_mutex);
    
    75
    -#endif
    
    76
    -}
    
    77
    -
    
    78
    -void
    
    79
    -freeSignalHandlers(void) {
    
    80
    -    if (signal_handlers != NULL) {
    
    81
    -        stgFree(signal_handlers);
    
    82
    -        signal_handlers = NULL;
    
    83
    -        nHandlers = 0;
    
    84
    -    }
    
    85
    -#if defined(THREADED_RTS)
    
    86
    -    closeMutex(&sig_mutex);
    
    87
    -#endif
    
    88
    -}
    
    89
    -
    
    90
    -void finiUserSignals(void)
    
    91
    -{
    
    92
    -    /* nothing */
    
    93
    -};
    
    94
    -
    
    95
    -/* -----------------------------------------------------------------------------
    
    96
    - * Allocate/resize the table of signal handlers.
    
    97
    - * -------------------------------------------------------------------------- */
    
    98
    -
    
    99
    -static void
    
    100
    -more_handlers(int sig)
    
    101
    -{
    
    102
    -    StgInt i;
    
    103
    -
    
    104
    -    if (sig < nHandlers)
    
    105
    -        return;
    
    106
    -
    
    107
    -    if (signal_handlers == NULL)
    
    108
    -        signal_handlers = (StgInt *)stgMallocBytes((sig + 1) * sizeof(StgInt),
    
    109
    -                                                   "more_handlers");
    
    110
    -    else
    
    111
    -        signal_handlers = (StgInt *)stgReallocBytes(signal_handlers,
    
    112
    -                                                    (sig + 1) * sizeof(StgInt),
    
    113
    -                                                    "more_handlers");
    
    114
    -
    
    115
    -    for(i = nHandlers; i <= sig; i++)
    
    116
    -        // Fill in the new slots with default actions
    
    117
    -        signal_handlers[i] = STG_SIG_DFL;
    
    118
    -
    
    119
    -    nHandlers = sig + 1;
    
    120
    -}
    
    121
    -
    
    122
    -#if !defined(THREADED_RTS)
    
    123
    -
    
    124
    -#define N_PENDING_HANDLERS 16
    
    125
    -
    
    126
    -siginfo_t pending_handler_buf[N_PENDING_HANDLERS];
    
    127
    -siginfo_t *next_pending_handler = pending_handler_buf;
    
    128
    -
    
    129
    -static inline bool signals_pending(void) {
    
    130
    -    return (next_pending_handler != pending_handler_buf);
    
    131
    -}
    
    132
    -
    
    133
    -#endif /* THREADED_RTS */
    
    134
    -
    
    135
    -/* -----------------------------------------------------------------------------
    
    136
    - * Low-level signal handler
    
    35
    +/* Note [pthread_sigmask and the ineffectiveness of masking signals]
    
    36
    + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    137 37
      *
    
    138
    - * Places the requested handler on a stack of pending handlers to be
    
    139
    - * started up at the next context switch.
    
    140
    - * -------------------------------------------------------------------------- */
    
    141
    -
    
    142
    -static void
    
    143
    -generic_handler(int sig USED_IF_THREADS,
    
    144
    -                siginfo_t *info,
    
    145
    -                void *p STG_UNUSED)
    
    146
    -{
    
    147
    -#if defined(THREADED_RTS)
    
    148
    -    //TODO: This calls MIO directly. We should go via IOManager API.
    
    149
    -    // The IOManager API should be extended to cover signals.
    
    150
    -    timerManagerNotifySignal(sig, info);
    
    151
    -#else /* not THREADED_RTS */
    
    152
    -
    
    153
    -    /* Can't call allocate from here.  Probably can't call malloc
    
    154
    -       either.  However, we have to schedule a new thread somehow.
    
    155
    -
    
    156
    -       It's probably ok to request a context switch and allow the
    
    157
    -       scheduler to  start the handler thread, but how do we
    
    158
    -       communicate this to the scheduler?
    
    159
    -
    
    160
    -       We need some kind of locking, but with low overhead (i.e. no
    
    161
    -       blocking signals every time around the scheduler).
    
    162
    -
    
    163
    -       Signal Handlers are atomic (i.e. they can't be interrupted), and
    
    164
    -       we can make use of this.  We just need to make sure the
    
    165
    -       critical section of the scheduler can't be interrupted - the
    
    166
    -       only way to do this is to block signals.  However, we can lower
    
    167
    -       the overhead by only blocking signals when there are any
    
    168
    -       handlers to run, i.e. the set of pending handlers is
    
    169
    -       non-empty.
    
    170
    -    */
    
    171
    -
    
    172
    -    /* We use a stack to store the pending signals.  We can't
    
    173
    -       dynamically grow this since we can't allocate any memory from
    
    174
    -       within a signal handler.
    
    175
    -
    
    176
    -       Hence unfortunately we have to bomb out if the buffer
    
    177
    -       overflows.  It might be acceptable to carry on in certain
    
    178
    -       circumstances, depending on the signal.
    
    179
    -    */
    
    180
    -
    
    181
    -    memcpy(next_pending_handler, info, sizeof(siginfo_t));
    
    182
    -
    
    183
    -    next_pending_handler++;
    
    184
    -
    
    185
    -    // stack full?
    
    186
    -    if (next_pending_handler == &pending_handler_buf[N_PENDING_HANDLERS]) {
    
    187
    -        errorBelch("too many pending signals");
    
    188
    -        stg_exit(EXIT_FAILURE);
    
    189
    -    }
    
    190
    -
    
    191
    -    interruptCapability(&MainCapability);
    
    192
    -
    
    193
    -#endif /* THREADED_RTS */
    
    194
    -}
    
    195
    -
    
    196
    -/* -----------------------------------------------------------------------------
    
    197
    - * Blocking/Unblocking of the user signals
    
    198
    - * -------------------------------------------------------------------------- */
    
    199
    -
    
    200
    -void
    
    201
    -blockUserSignals(void)
    
    202
    -{
    
    203
    -    sigprocmask(SIG_BLOCK, &userSignals, &savedSignals);
    
    204
    -}
    
    205
    -
    
    206
    -void
    
    207
    -unblockUserSignals(void)
    
    208
    -{
    
    209
    -    sigprocmask(SIG_SETMASK, &savedSignals, NULL);
    
    210
    -}
    
    211
    -
    
    212
    -/* -----------------------------------------------------------------------------
    
    213
    - * Install a Haskell signal handler.
    
    38
    + * Relevant posix signals facts:
    
    214 39
      *
    
    215
    - * We should really do this in Haskell in GHC.Conc, and share the
    
    216
    - * signal_handlers array with the one there.
    
    40
    + *  + Every (OS) thread has a signal mask, which can be manipulated by that
    
    41
    + *    thread itself (though sigprocmask on macOS breaks this).
    
    42
    + *  + Signal masks are inherited when new threads or processes are created (or
    
    43
    + *    can be set as an attribute in posix spawn on some platforms).
    
    44
    + *  + Most signals are processes directed, while some are thread-directed.
    
    45
    + *  + Process-directed signals will be delivered to _any_ thread that does not
    
    46
    + *    have the signal masked.
    
    217 47
      *
    
    218
    - * -------------------------------------------------------------------------- */
    
    219
    -
    
    220
    -int
    
    221
    -stg_sig_install(int sig, int spi, void *mask)
    
    222
    -{
    
    223
    -    sigset_t signals, osignals;
    
    224
    -    StgInt previous_spi;
    
    225
    -
    
    226
    -    struct sigaction action;
    
    227
    -    memset(&action, 0, sizeof(struct sigaction));
    
    228
    -
    
    229
    -    ACQUIRE_LOCK(&sig_mutex);
    
    230
    -
    
    231
    -    // Block the signal until we figure out what to do
    
    232
    -    // Count on this to fail if the signal number is invalid
    
    233
    -    if (sig < 0 ||
    
    234
    -          sigemptyset(&signals) ||
    
    235
    -          sigaddset(&signals, sig) ||
    
    236
    -          sigprocmask(SIG_BLOCK, &signals, &osignals)) {
    
    237
    -        RELEASE_LOCK(&sig_mutex);
    
    238
    -        return STG_SIG_ERR;
    
    239
    -    }
    
    240
    -
    
    241
    -    more_handlers(sig);
    
    242
    -
    
    243
    -    previous_spi = signal_handlers[sig];
    
    244
    -
    
    245
    -    action.sa_flags = 0;
    
    246
    -
    
    247
    -    switch(spi) {
    
    248
    -    case STG_SIG_IGN:
    
    249
    -        action.sa_handler = SIG_IGN;
    
    250
    -        break;
    
    251
    -
    
    252
    -    case STG_SIG_DFL:
    
    253
    -        action.sa_handler = SIG_DFL;
    
    254
    -        break;
    
    255
    -
    
    256
    -    case STG_SIG_RST:
    
    257
    -        action.sa_flags |= SA_RESETHAND;
    
    258
    -        /* fall through */
    
    259
    -    case STG_SIG_HAN:
    
    260
    -        action.sa_sigaction = generic_handler;
    
    261
    -        action.sa_flags |= SA_SIGINFO;
    
    262
    -        break;
    
    263
    -
    
    264
    -    default:
    
    265
    -        barf("stg_sig_install: bad spi");
    
    266
    -    }
    
    267
    -
    
    268
    -    if (mask != NULL)
    
    269
    -        action.sa_mask = *(sigset_t *)mask;
    
    270
    -    else
    
    271
    -        sigemptyset(&action.sa_mask);
    
    272
    -
    
    273
    -    action.sa_flags |= sig == SIGCHLD && nocldstop ? SA_NOCLDSTOP : 0;
    
    274
    -
    
    275
    -    if (sigaction(sig, &action, NULL))
    
    276
    -    {
    
    277
    -        errorBelch("sigaction");
    
    278
    -        RELEASE_LOCK(&sig_mutex);
    
    279
    -        return STG_SIG_ERR;
    
    280
    -    }
    
    281
    -
    
    282
    -    signal_handlers[sig] = spi;
    
    283
    -
    
    284
    -    switch(spi) {
    
    285
    -    case STG_SIG_RST:
    
    286
    -    case STG_SIG_HAN:
    
    287
    -        sigaddset(&userSignals, sig);
    
    288
    -        break;
    
    289
    -
    
    290
    -    default:
    
    291
    -        sigdelset(&userSignals, sig);
    
    292
    -        break;
    
    293
    -    }
    
    294
    -
    
    295
    -    if (sigprocmask(SIG_SETMASK, &osignals, NULL))
    
    296
    -    {
    
    297
    -        errorBelch("sigprocmask");
    
    298
    -        RELEASE_LOCK(&sig_mutex);
    
    299
    -        return STG_SIG_ERR;
    
    300
    -    }
    
    301
    -
    
    302
    -    RELEASE_LOCK(&sig_mutex);
    
    303
    -    return previous_spi;
    
    304
    -}
    
    305
    -
    
    306
    -/* -----------------------------------------------------------------------------
    
    307
    - * Creating new threads for signal handlers.
    
    308
    - * -------------------------------------------------------------------------- */
    
    309
    -
    
    310
    -#if !defined(THREADED_RTS)
    
    311
    -void
    
    312
    -startPendingSignalHandlers(Capability *cap)
    
    313
    -{
    
    314
    -  siginfo_t *info;
    
    315
    -  int sig;
    
    316
    -
    
    317
    -  if (!signals_pending()) return;
    
    318
    -
    
    319
    -  blockUserSignals();
    
    320
    -
    
    321
    -  while (next_pending_handler != pending_handler_buf) {
    
    322
    -
    
    323
    -    next_pending_handler--;
    
    324
    -
    
    325
    -    sig = next_pending_handler->si_signo;
    
    326
    -    if (signal_handlers[sig] == STG_SIG_DFL) {
    
    327
    -        continue; // handler has been changed.
    
    328
    -    }
    
    329
    -
    
    330
    -    info = stgMallocBytes(sizeof(siginfo_t), "startPendingSignalHandlers");
    
    331
    -           // freed by runHandler
    
    332
    -    memcpy(info, next_pending_handler, sizeof(siginfo_t));
    
    333
    -
    
    334
    -    debugTrace(DEBUG_iomanager,
    
    335
    -               "Handling pending signal, no %d", info->si_signo);
    
    336
    -
    
    337
    -    StgTSO *t =
    
    338
    -        createIOThread(cap,
    
    339
    -                       RtsFlags.GcFlags.initialStkSize,
    
    340
    -                       rts_apply(cap,
    
    341
    -                                 rts_apply(cap,
    
    342
    -                                           runHandlersPtr_closure,
    
    343
    -                                           rts_mkPtr(cap, info)),
    
    344
    -                                 rts_mkInt(cap, info->si_signo)));
    
    345
    -    scheduleThread(cap, t);
    
    346
    -    setThreadLabel(cap, t, "signal handler thread");
    
    347
    -  }
    
    348
    -
    
    349
    -  unblockUserSignals();
    
    350
    -}
    
    351
    -#endif
    
    352
    -
    
    353
    -#else /* !RTS_USER_SIGNALS */
    
    354
    -StgInt
    
    355
    -stg_sig_install(StgInt sig STG_UNUSED,
    
    356
    -                StgInt spi STG_UNUSED,
    
    357
    -                void* mask STG_UNUSED)
    
    358
    -{
    
    359
    -  //barf("User signals not supported");
    
    360
    -  return STG_SIG_DFL;
    
    361
    -}
    
    362
    -
    
    363
    -#endif
    
    48
    + * The important consequence of the "any thread will do" property is that:
    
    49
    + *
    
    50
    + *  + it is generally _effective_ to unmask a signal in a thread, because at
    
    51
    + *    least the calling thread has the signal unmasked; however
    
    52
    + *  + it is generally _ineffective_ to mask a signal in a thread, because
    
    53
    + *    the signal will likely still be unmasked in some other thread and thus
    
    54
    + *    be delivered there.
    
    55
    + *
    
    56
    + * Thus we should avoid use the masking signals as a mechanism that we rely on,
    
    57
    + * since it will not in general be effective. In best-effort cases where there
    
    58
    + * is a reasonable likelyhood that the calling thread is the only thread with
    
    59
    + * signals unmasked then it is ok.
    
    60
    + *
    
    61
    + * Closely related: always avoid sigprocmask, and prefer pthread_sigmask.
    
    62
    + * The older function is sigprocmask but it's meaning in multi-threaded
    
    63
    + * programs is explicitly unspecified by Posix. On Linux it modifies the
    
    64
    + * calling thread's signal mask, but on macOS it modifies the signal mask of
    
    65
    + * every thread in the process, leading to chaos. The new function is
    
    66
    + * pthread_sigmask which is well defined and operates only on the signal mask
    
    67
    + * of the calling thread.
    
    68
    + */
    
    364 69
     
    
    365
    -#if defined(RTS_USER_SIGNALS)
    
    366 70
     /* -----------------------------------------------------------------------------
    
    367 71
      * SIGINT handler.
    
    368 72
      *
    
    ... ... @@ -504,8 +208,8 @@ set_sigtstp_action (bool handle)
    504 208
     void
    
    505 209
     initDefaultHandlers(void)
    
    506 210
     {
    
    507
    -    // N.B. We can't use initializers here as CentOS's ancient toolchain throws
    
    508
    -    // spurious warnings. See #23577.
    
    211
    +    if (!RtsFlags.MiscFlags.install_signal_handlers) return;
    
    212
    +
    
    509 213
         struct sigaction action, oact;
    
    510 214
         memset(&oact, 0, sizeof(struct sigaction));
    
    511 215
         memset(&action, 0, sizeof(struct sigaction));
    
    ... ... @@ -569,6 +273,8 @@ initDefaultHandlers(void)
    569 273
     void
    
    570 274
     resetDefaultHandlers(void)
    
    571 275
     {
    
    276
    +    if (!RtsFlags.MiscFlags.install_signal_handlers) return;
    
    277
    +
    
    572 278
         struct sigaction action;
    
    573 279
     
    
    574 280
         action.sa_handler = SIG_DFL;
    

  • rts/posix/TTY.c
    ... ... @@ -46,14 +46,21 @@ resetTerminalSettings (void)
    46 46
     #if HAVE_TERMIOS_H
    
    47 47
         // Reset the terminal settings on the standard file descriptors,
    
    48 48
         // if we changed them.  See System.Posix.Internals.tcSetAttr for
    
    49
    -    // more details, including the reason we temporarily disable
    
    49
    +    // more details, including the reason we temporarily mask
    
    50 50
         // SIGTTOU here.
    
    51
    +    //
    
    52
    +    // This masking best effort only: it will only be effective if the
    
    53
    +    // calling thread is the only thread left in the process with
    
    54
    +    // signals unmasked. This should typically be true when this is
    
    55
    +    // called during shutdown, but there are many ways to make it false.
    
    56
    +    //
    
    57
    +    // See Note [pthread_sigmask and the ineffectiveness of masking signals].
    
    51 58
         {
    
    52 59
             int fd;
    
    53 60
             sigset_t sigset, old_sigset;
    
    54 61
             sigemptyset(&sigset);
    
    55 62
             sigaddset(&sigset, SIGTTOU);
    
    56
    -        sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
    
    63
    +        pthread_sigmask(SIG_BLOCK, &sigset, &old_sigset);
    
    57 64
             for (fd = 0; fd <= 2; fd++) {
    
    58 65
                 struct termios* ts =
    
    59 66
                     (struct termios*)__hscore_get_saved_termios(fd);
    
    ... ... @@ -61,7 +68,7 @@ resetTerminalSettings (void)
    61 68
                     tcsetattr(fd,TCSANOW,ts);
    
    62 69
                 }
    
    63 70
             }
    
    64
    -        sigprocmask(SIG_SETMASK, &old_sigset, NULL);
    
    71
    +        pthread_sigmask(SIG_SETMASK, &old_sigset, NULL);
    
    65 72
         }
    
    66 73
     #endif
    
    67 74
     }

  • rts/posix/Ticker.c
    ... ... @@ -242,7 +242,7 @@ initTicker (Time interval, TickProc handle_tick)
    242 242
          * Create the thread with all blockable signals blocked, leaving signal
    
    243 243
          * handling to the main and/or other threads.  This is especially useful in
    
    244 244
          * the non-threaded runtime, where applications might expect sigprocmask(2)
    
    245
    -     * to effectively block signals.
    
    245
    +     * or pthread_sigmask(3) to effectively block signals.
    
    246 246
          */
    
    247 247
     #if defined(HAVE_SIGNAL_H)
    
    248 248
         sigfillset(&mask);
    

  • rts/rts.cabal
    ... ... @@ -579,6 +579,7 @@ library
    579 579
              c-sources: posix/GetEnv.c
    
    580 580
                         posix/GetTime.c
    
    581 581
                         posix/Ticker.c
    
    582
    +                    posix/IOManSignals.c
    
    582 583
                         posix/OSMem.c
    
    583 584
                         posix/OSThreads.c
    
    584 585
                         posix/FdWakeup.c
    

  • rts/win32/ConsoleHandler.c
    ... ... @@ -54,11 +54,6 @@ initUserSignals(void)
    54 54
         return;
    
    55 55
     }
    
    56 56
     
    
    57
    -void
    
    58
    -freeSignalHandlers(void) {
    
    59
    -    /* Do nothing */
    
    60
    -}
    
    61
    -
    
    62 57
     /* Called in hs_exit to clean up resources.  */
    
    63 58
     void
    
    64 59
     finiUserSignals(void)
    
    ... ... @@ -115,6 +110,8 @@ static BOOL WINAPI shutdown_handler(DWORD dwCtrlType)
    115 110
      */
    
    116 111
     void initDefaultHandlers(void)
    
    117 112
     {
    
    113
    +    if (!RtsFlags.MiscFlags.install_signal_handlers) return;
    
    114
    +
    
    118 115
         if ( !SetConsoleCtrlHandler(shutdown_handler, true) ) {
    
    119 116
             errorBelch("warning: failed to install default console handler");
    
    120 117
         }
    
    ... ... @@ -122,33 +119,25 @@ void initDefaultHandlers(void)
    122 119
     
    
    123 120
     void resetDefaultHandlers(void)
    
    124 121
     {
    
    122
    +    if (!RtsFlags.MiscFlags.install_signal_handlers) return;
    
    123
    +
    
    125 124
         if ( !SetConsoleCtrlHandler(shutdown_handler, false) ) {
    
    126 125
             errorBelch("warning: failed to uninstall default console handler");
    
    127 126
         }
    
    128 127
     }
    
    129 128
     
    
    130
    -/*
    
    131
    - * Function: blockUserSignals()
    
    132
    - *
    
    133
    - * Temporarily block the delivery of further console events. Needed to
    
    134
    - * avoid race conditions when GCing the stack of outstanding handlers or
    
    135
    - * when emptying the stack by running the handlers.
    
    136
    - *
    
    129
    +/* Temporarily block the delivery of further console events. Needed to
    
    130
    + * avoid race conditions when emptying the stack by running the handlers.
    
    137 131
      */
    
    138
    -void
    
    139
    -blockUserSignals(void)
    
    132
    +static void blockUserSignals(void)
    
    140 133
     {
    
    141 134
         deliver_event = false;
    
    142 135
     }
    
    143 136
     
    
    144 137
     
    
    145
    -/*
    
    146
    - * Function: unblockUserSignals()
    
    147
    - *
    
    148
    - * The inverse of blockUserSignals(); re-enable the deliver of console events.
    
    138
    +/* The inverse of blockUserSignals(); re-enable the deliver of console events.
    
    149 139
      */
    
    150
    -void
    
    151
    -unblockUserSignals(void)
    
    140
    +static void unblockUserSignals(void)
    
    152 141
     {
    
    153 142
         deliver_event = true;
    
    154 143
     }
    

  • rts/win32/ConsoleHandler.h
    ... ... @@ -19,12 +19,31 @@
    19 19
      * handling code also.
    
    20 20
      */
    
    21 21
     
    
    22
    +/*
    
    23
    + * Function: initConsoleEvents()
    
    24
    + *
    
    25
    + * Initialize the console handling substrate.
    
    26
    + */
    
    27
    +void initConsoleEvents(void);
    
    28
    +
    
    29
    +/* Finalise signal and console events handling.
    
    30
    + */
    
    31
    +void finiConsoleEvents(void);
    
    32
    +
    
    22 33
     #if !defined(THREADED_RTS)
    
    23 34
     /*
    
    24 35
      * under THREADED_RTS, console events are passed to the IO manager
    
    25 36
      * thread, which starts up the handler.  See ThrIOManager.c.
    
    26 37
      */
    
    27 38
     
    
    39
    +/*
    
    40
    + * Function: startPendingSignalHandlers()
    
    41
    + *
    
    42
    + * If there are any queued up win32 console events, run the handlers
    
    43
    + * associated with them.
    
    44
    + */
    
    45
    +void startPendingSignalHandlers(Capability *cap);
    
    46
    +
    
    28 47
     /*
    
    29 48
      * Function: rts_waitConsoleHandlerCompletion()
    
    30 49
      *