如何优雅地处理sigaction()并退还给父进程

时间:2019-06-18 07:51:00

标签: android linux signals hal sigaction

有没有一种方法可以捕获信号并返回父进程 正确处理错误后? 更具体地说,通过以下代码,我可以执行一些错误处理,但是其他人在执行sigaction处理程序后无法获得相同的崩溃通知。 ..非常感谢!

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();
    ...
}

0 个答案:

没有答案