I ran into a problem where the Go program uses a .so lib, and the c code needs to deal with a signal SIGALRM. But it seems that once a signal SIGALRM is released, the program crashes. From "The Go Programming Language" I see this:
If the non-Go code installs any signal handlers, it must use the SA_ONSTACK flag with sigaction. Failing to do so is likely to cause the program to crash if the signal is received.
Can anyone please show the correct usage of SA_ONSTACK flag with sigaction. I would appreciate it very much.
Below is my code:
// LIC_TIMER_CALLBACK_FUNC is a function pointer
static LIC_TIMER_CALLBACK_FUNC s_pfnTmCallBack;
static void _OS_TriggerTask(int iSig)
{
logInfo("_OS_TriggerTask start...");
if (LIC_NULL_PTR != s_pfnTmCallBack)
{
logInfo("call timer callback...");
s_pfnTmCallBack(0);
}
logInfo("_OS_TriggerTask end...");
}
// set the trigger
int OS_StartTrigger(int ulLength, LIC_TIMER_CALLBACK_FUNC pfnTmCallBack)
{
logInfo("OS_StartTrigger start...");
s_pfnTmCallBack = pfnTmCallBack;
struct sigaction stSigact;
stSigact.sa_handler = _OS_TriggerTask;
stSigact.sa_flags = SA_ONSTACK;
sigemptyset(&stSigact.sa_mask);
int iRet = sigaction(SIGALRM, &stSigact, NULL);
if(0 != iRet)
{
logError("set signal failed...");
}
else
{
logInfo("set signal success...");
}
/* set time interval */
struct itimerval stItimerVal;
stItimerVal.it_value.tv_sec = 60;
stItimerVal.it_value.tv_usec = 0;
stItimerVal.it_interval = stItimerVal.it_value;
iRet = setitimer(ITIMER_REAL, &stItimerVal, NULL);
if(0 != iRet)
{
logError("set timer failed...");
return -1;
}
logInfo("OS_StartTrigger end...");
return 0;
}
...sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESTART;
used successfully. – Maladjusted