How to add code to the standard signal handler?

核能气质少年 提交于 2019-12-12 13:18:27

问题


I have a C application running on Linux where I need to add some code to the standard signal handler. The idea was to setup my handler saving the pointer to the standard one and call the saved handler from my code. Unfortunately, neither signal() nor sigaction() return pointer to the standard handler. Both of them return NULL instead. Is there any way of doing custom handling and continuing with the standard handling without removal of the custom handler and sending the same signal again?


回答1:


There is no "standard signal handler"; instead, there's a default action performed by the kernel when a signal is unhandled. If you want to do something when the signal is received, then defer to the default action, you can do the following at the end of your signal handler:

sigset_t set;
signal(sig, SIG_DFL);
raise(sig);
sigemptyset(&set);
sigaddset(&set, sig);
sigprocmask(SIG_UNBLOCK, &set, 0);

This is assuming you used sigaction to install your signal handler, and did not specify the SA_NODEFER or SA_RESETHAND flags. It's also possible to achieve what you want using those flags and simply calling raise, but this has ugly race conditions if the signal is delivered twice in rapid succession, so you should not do it; instead use the method I suggested.

Edit: Actually you don't have to do any of the signal mask stuff, since returning from the signal handler will restore the old signal mask. Just this should work:

signal(sig, SIG_DFL);
raise(sig);
return;



回答2:


When the call to signal() or sigaction() returns NULL, it implies SIG_DFL (i.e. default signal handler)

If you're using signal(), the handler is supposed to be reset when the handler is called, you can issue raise(SIGNAL) as the last line in the signal handler and it should invoke the default handler.

If you're using sigaction() then you should pass in SA_RESETHAND as one of the flags, which will allow you to use the raise(SIGNAL) call in that handler.

The vast majority of default signal handlers will terminate the application, so this 'one shot' replacement will work. You can add a sigaction() call after the raise call to store the new handler again e.g.

void sighandler(int signum)
{
    // Do my stuff
    raise(signup);
    signal(signum, sighandler);
}


来源:https://stackoverflow.com/questions/7389105/how-to-add-code-to-the-standard-signal-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!