How to reset SIGINT to default after specifying some custom handler for a while?

I use signal(SIGINT,my_handler) to point SIGINT to my_handler . After some time, I want to reset this for any default handler that it points to at all. How can i do this?

+6
source share
2 answers

Pass SIG_DFL as the func parameter to signal() - reset the default behavior:

 signal(SIGINT, SIG_DFL); 
+9
source

Today it is recommended to use sigaction .

Moreover, it allows you to automatically reset the signal handler to the default value before your custom handler is called for the first time.

SA_RESETHAND

If set, the signal location should be reset to SIG_DFL and the SA_SIGINFO flag should be cleared when entering the signal handler.

Note: SIGILL and SIGTRAP cannot be automatically reset upon delivery; the system silently applies this restriction.

Otherwise, the location of the signal should not change when entering the signal handler.

In addition, if this flag is set, sigaction() may behave as if the SA_NODEFER flag was also set.

Defining a one-time signal handler

 #include <signal.h> #include <stdio.h> action.sa_handler = my_handler; action.sa_flags = SA_RESETHAND; if (sigaction(SIGINT, &action, NULL) == -1) { perror("Failed to install signal handler for SIGINT"); } 
0
source

All Articles