+ in_safe_dumpstack = 1;
+ sig_monitor(0, safe_dumpstack_cb, args);
+ in_safe_dumpstack = 0;
+}
+#endif
+
+/* Handles signals that terminate the process */
+static void on_signal_terminate (int signum)
+{
+ if (!in_safe_dumpstack) {
+ ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
+ if (signum == SIGABRT)
+ safe_dumpstack(3, signum);
+ }
+ safe_exit(1);
+}
+
+/* Handles monitored signals that can be continued */
+static void on_signal_error(int signum)
+{
+ if (!in_safe_dumpstack) {
+ ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
+
+ safe_dumpstack(3, signum);
+ }
+ monitor_raise(signum);
+
+ if (signum != SIG_FOR_TIMER) {
+ ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
+ safe_exit(2);
+ }
+}
+
+/*
+static void disable_signal_handling()
+{
+ set_signals_handler(SIG_DFL, sigerr);
+ set_signals_handler(SIG_DFL, sigterm);
+ enabled = 0;
+}
+*/
+
+static int enable_signal_handling()
+{
+ if (!set_signals_handler(on_signal_error, sigerr)
+ || !set_signals_handler(on_signal_terminate, sigterm)) {
+ return -1;
+ }
+ enabled = 1;
+ return 0;
+}
+#endif
+/******************************************************************************/
+
+int sig_monitor_init(int enable)
+{
+ return enable ? enable_signal_handling() : 0;