0

I'm attempting to move a signal handler to be a thread (so I can use mutex's). I found a few pages such as this which describe what I should be doing, however when I try, the signal handler never fires.

The following is my handler:

{ 
    int status;
    // Block current thread from trapping signal:
    sigset_t sigSet;
    sigemptyset(&sigSet);
    sigaddset(&sigSet, SIGSEGV);
    status = pthread_sigmask( SIG_BLOCK, &sigSet, NULL);
    dbgassert(status == 0);
    // create thread to capture signal
    status = pthread_create(&signalThread, NULL, signalThreadFn, NULL);
    dbgassert(status == 0);

    sleep(1);
    debugf("about to die");
    // generate segv:
    rt = posix_memalign((void **)&pool, getpagesize(), getpagesize());
    dbgassert(rt==0);
    mprotect(pool, getpagesize(), PROT_READ);
    pool[0]=10;
    debugf("resurected!!!");
} 


static void *
signalThreadFn(void *parm) {
    sigset_t         sigSet;
    int              sig;
    siginfo_t        sigInfo = {};

    debugf("Starting signal thread");
    sigemptyset(&sigSet);
    sigaddset(&sigSet, SIGSEGV);

    while( 1 ) {
        debugf("waiting for SIGSEGV...");
        sig=sigwaitinfo(&sigSet,&sigInfo);
        debugf("Got signal %d",sig);
        ...
    }
}

but the thread never captures the signal. I get on output:

DEBUG [signalThreadFn.841]: waiting for SIGSEGV...
DEBUG [constructor.240]: about to die
Segmentation fault (core dumped)

I'm not sure why the signal handler is not catching the signal. From what I understand, the child thread should inherit the mask from its parent, and because SIGSEGV was disabled, neither should catch the signal until the thread does its sigwaitinfo -- why isn't the child catching the signal?

HardcoreHenry
  • 5,909
  • 2
  • 19
  • 44
  • 1
    Related: [About catching the SIGSEGV in a multithreaded environment](https://stackoverflow.com/questions/16204271/about-catching-the-sigsegv-in-multithreaded-environment) – Mark Plotnick Jan 22 '19 at 01:44
  • Thanks -- that's a useful thread. According to that, the signal is delivered to the _thread_ that caused the exception, meaning it can't be caught and handled by another thread. This will complicate things for me... – HardcoreHenry Jan 22 '19 at 14:27

0 Answers0