Restoring original signal in sigaction
I have sigaction defined and it works fine. However I want to restore the original signal after my act开发者_StackOverflowion is completed. This is my sigaction:
static void signal_handler(int signal, siginfo_t *info, void *reserved)
{
//Some logging statements
//How do I restore the original signal here??
}
The signal handler is set from JNI_Onload:
extern "C" jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/)
{
struct sigaction handler, action_old;
memset(&handler, 0, sizeof(handler));
handler.sa_sigaction = signal_handler;
handler.sa_flags = SA_SIGINFO;
sigaction(SIGILL, &handler, &action_old);
sigaction(SIGABRT, &handler, &action_old);
sigaction(SIGBUS, &handler, &action_old);
sigaction(SIGFPE, &handler, &action_old);
sigaction(SIGSEGV, &handler, &action_old);
sigaction(SIGSTKFLT, &handler, &action_old);
//Can I restore prior signal here???
return JNI_VERSION_1_6;
}
Save the old actions in global (or file-scope) variables (or an array indexed by signal id) and call sigaction
from inside your signal handler to restore the previous behavior. sigaction
is guaranteed to be async-signal safe.
See also: http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03
http://www.gnu.org/s/hello/manual/libc/Basic-Signal-Handling.html - says:
The signal function returns the action that was previously in effect for the specified signum. You can save this value and restore it later by calling signal again.
精彩评论