开发者

How to add code to the standard signal handler?

开发者 https://www.devze.com 2023-04-04 12:04 出处:网络
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

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?


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;


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号