Signal () rewriting other signal handlers

Does the signal() function overwrite other signal calls that the process could configure? That is, if the SIGINT handler was configured by the process, and the DLL calls signal(SIGINT,xxx) to process its own termination code, is the original SIGINT handler disabled?

+6
source share
2 answers

The signal() call:

  • Sets the handler that you specify as the new signal handler, and
  • Tells you what the old handler is.

The new handler will be called instead of the old. If you want to link them, you need to do something like:

 typedef void (*Handler)(int signum); static Handler old_int_handler = SIG_IGN; static void int handler(int signum) /* New signal handler */ { ...do your signal handling... if (old_int_handler != SIG_IGN && old_int_handler != SIG_DFL) (*old_int_handler)(signum); } static void set_int_handler(void) /* Install new handler */ { Handler old = signal(SIGINT, SIG_IGN); if (old != SIG_IGN) { old_int_handler = old; signal(SIGINT, int_handler); } } static void rst_int_handler(void) /* Restore original handler */ { Handler old = signal(SIGINT, SIG_IGN); if (old == int_handler) { signal(SIGINT, old_int_handler); old_int_handler = SIG_IGN; } } ...in another function... { ... set_int_handler(); ... rst_int_handler(); ... } 

If interrupts are ignored, it ignores them. If interrupts were handled by a custom interrupt handler, then this calls your signal processing code and the signal processing source code.

Please note that the advice from Christian.K about the lack of signal processing in the DLL (shared library) is also relevant and relevant. The description above assumes that you decide to ignore this advice.

+8
source

This is not a "literal" answer to your question, but a recommendation: you should not do this in a DLL.

This is an unexpected and often annoying application that uses a DLL. The DLL should (usually) be "passive" and provide only functions for invoking the application.

Rather, we provide a public function from your DLL that applications should call, for example. MyDllCleanup() . Then let the application decide how it calls this function (via a signal handler or another). BTW, the same goes for initialization: instead of relying on DllMain (or _init / _fini with libdl on UNIX), they provide explicit functions for invoking applications.

+2
source

Source: https://habr.com/ru/post/916353/


All Articles