(7) add set_signal_handlers() to bind functions during init
[systembsd.git] / src / interfaces / timedated / timedated.c
index 72d14f8c80e7b67c6f2476ba96b743f025cbe89b..32f040a2f775dd1f25048f78c7676eff030c12e4 100644 (file)
@@ -133,6 +133,23 @@ void timedated_mem_clean() {
 
 }
 
+/* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
+gboolean unix_sig_terminate_handler(gpointer data) {
+
+    g_printf("caught SIGINT/HUP/TERM, exiting\n");
+
+    timedated_mem_clean();
+    return G_SOURCE_REMOVE;
+}
+
+void set_signal_handlers() {
+
+    /* we don't care about its descriptor, we never need to unregister these */
+    g_unix_signal_add(SIGINT,  unix_sig_terminate_handler, NULL);
+    g_unix_signal_add(SIGHUP,  unix_sig_terminate_handler, NULL);
+    g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL);
+}
+
 int main() {
 
        timedated_loop = g_main_loop_new(NULL, TRUE);