1

Is there a way to catch a signal with sigaction and get back to parent process after handle the error properly? To be more specific, by following code I can do some error handle, but others can not get same crash notification after my sigaction handler. ..Thanks a lot!

static struct sigaction g_defaultsigsegvhndlr;

VOID InvokeDefaultHndlr(
    struct sigaction* pAction,
    INT32             sigNum,
    siginfo_t*        pSigInfo,
    VOID*             pContext)
{
    if (pAction->sa_flags & SA_SIGINFO)
    {
        pAction->sa_sigaction(sigNum, pSigInfo, pContext);
    }
    else
    {
        pAction->sa_handler(sigNum);
    }
}

VOID SignalCatcher(
    INT32      sigNum,
    siginfo_t* pSigInfo,
    VOID*      pContext)
{    
    switch (sigNum)
    {
        case SIGSEGV:
            InvokeDefaultHndlr(&g_defaultsigsegvhndlr, sigNum, pSigInfo, pContext);
            break;
        default:
            {
            }
    }
}

VOID RegisterSignal()
{
    struct sigaction newSigAction;

    memset(&newSigAction, '\0', sizeof(struct sigaction));

    if (sigaction(SIGSEGV, NULL, &g_defaultsigsegvhndlr) < 0)
    {
        ALOGE("Failed to get signal handler for SIGSEGV");
    }

    newSigAction              = g_defaultsigsegvhndlr;
    newSigAction.sa_flags    |= SA_SIGINFO;
    newSigAction.sa_sigaction = SignalCatcher;

    if (sigaction(SIGSEGV, &newSigAction, NULL) < 0)
    {
        ALOGE("Failed to register signal handler for SIGSEGV");
    }
}

// Just a sample, I will invoke RegisterSignal() in main thread loop
int main()
{
    ...
    RegisterSignal();
    ...
}
hismart
  • 405
  • 6
  • 15
  • It looks like a C code, why a C++ flag has been used? – bartolo-otrit Jun 18 '19 at 08:01
  • 1
    @bartolo-otrit in the Android HAL, we write the code by C/C++, for this, it definitely use C-style, however the compiler will be C++14. That`s why I use C++ flag. I can remove the C++ tag if it makes you confuse. – hismart Jun 18 '19 at 08:19

0 Answers0