2

I have sigaction defined and it works fine. However I want to restore the original signal after my action 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;
}
OceanBlue
  • 9,142
  • 21
  • 62
  • 84
  • You clobber the previous value of `action_old` with each call to `sigaction`. If you want to save the old handler, you need a different "`action_old`" structure for each signal whose old handler you want to save. – R.. GitHub STOP HELPING ICE Sep 14 '11 at 22:49
  • By the way, if you're trying to handle the above signals, it sounds like you have some seriously buggy code. Handling the signals and trying to recover is not the way to fix that; instead, get to work figuring out what's wrong with your code.... – R.. GitHub STOP HELPING ICE Sep 14 '11 at 22:50
  • 1
    @R.. : You are absolutely right... and I am trying to weed out the bugs in the code (not written by me but mine to port on a new platform - Android NDK). However, if some random bugs are generated in the field then I want to be able to look at the logs to see the reason. As it stands, a signal 9 or 11 just crashes the app & I have no idea what happened. – OceanBlue Sep 15 '11 at 19:46

2 Answers2

2

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

0

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.

hari
  • 9,439
  • 27
  • 76
  • 110
  • I have came across that page before when I googled before posting the question. The problem is, I don't know how to implement "restore it later by calling signal" in this scenario. Could you please show a code sample? – OceanBlue Sep 15 '11 at 19:43