Skip to content

Delete a Linux signal handler in C

I use:

signal(SIGINT, CtrlHandler);

To add handler of SIGINT event. But how can i delete this handler?



Here is what you do:

signal(SIGINT, SIG_DFL);

That resets the signal handler back to whatever the default behavior was for that signal (including the default disposition if it hasn’t been set). In the case of SIGINT, it’s aborting your process without a core dump.

The manual for signal explains why this works:

signal(signum, handler) sets the disposition of the signal signum to handler, which is either SIG_IGN, SIG_DFL, or the address of a programmer-defined function (a “signal handler”). … If the disposition is set to SIG_DFL, then the default action associated with the signal occurs.

You can also find this information using the man command. If you type man signal on the command line and read through, you should see it.

This is very specific to the case in which you’ve replaced the system default signal handler. In some situations, what you want is to simply restore whatever handler was there in the first place. If you look at the definition of signal it looks like this:

sighandler_t signal(int signum, sighandler_t handler);

So, it returns a sighandler_t. The sighandler_t that it returns represents the previous ‘disposition’ of the signal. So, another way to handle this is to simply save the value it returns and then restore that value when you want to remove your own handler.

User contributions licensed under: CC BY-SA
7 People found this is helpful