minor, s/g_print/g_printf
[systembsd.git] / src / interfaces / timedated / timedated.c
index 3df5fc6e3e1fd956ef15894741f2896eb154680b..ee6cbeb68e1886aa4fe821d35a2a14a36e697f71 100644 (file)
@@ -70,7 +70,7 @@ static void timedated_on_bus_acquired(GDBusConnection *conn,
                                       const gchar *name,
                                       gpointer user_data) {
 
-    g_print("got bus/name, exporting %s's interface...\n", name);
+    g_printf("got bus/name, exporting %s's interface...\n", name);
 
     timedated_interf = timedate1_skeleton_new();
 
@@ -123,12 +123,37 @@ static void timedated_on_name_lost(GDBusConnection *conn,
  * this stops our GMainLoop safely before letting main() return */
 void timedated_mem_clean() {
 
-    g_ptr_array_foreach(timedated_freeable, (GFunc) g_free, NULL);
-       g_ptr_array_free(timedated_freeable, TRUE);
+    g_printf("exiting...\n");
+
+    if(dbus_interface_exported)
+        g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf));
+
+     if(g_main_loop_is_running(timedated_loop))
+        g_main_loop_quit(timedated_loop);
+
+}
+
+/* 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() {
 
+    set_signal_handlers();
+
        timedated_loop = g_main_loop_new(NULL, TRUE);
        timedated_freeable = g_ptr_array_new();
 
@@ -142,11 +167,14 @@ int main() {
                                     NULL);
 
        g_main_loop_run(timedated_loop);
+    /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
        g_main_loop_unref(timedated_loop);
 
+    /* guaranteed unownable */
        g_bus_unown_name(bus_descriptor);
 
-       timedated_mem_clean();
+    /* at this point no operations can occur with our data, it is safe to free it + its container */
+    g_ptr_array_free(timedated_freeable, TRUE);
 
        return 0;
 }