X-Git-Url: https://uglyman.kremlin.cc/gitweb/gitweb.cgi?p=systembsd.git;a=blobdiff_plain;f=src%2Finterfaces%2Ftimedated%2Ftimedated.c;h=a0d536de09c7ad9c7a8813f8339495eaff8c6b98;hp=72d14f8c80e7b67c6f2476ba96b743f025cbe89b;hb=9728ae1fd2b7c7851673a27f698ae2e3c8c03461;hpb=341587dbd94b8845b91baef896d9da2693df19bb diff --git a/src/interfaces/timedated/timedated.c b/src/interfaces/timedated/timedated.c index 72d14f8..a0d536d 100644 --- a/src/interfaces/timedated/timedated.c +++ b/src/interfaces/timedated/timedated.c @@ -133,8 +133,27 @@ 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() { + set_signal_handlers(); + timedated_loop = g_main_loop_new(NULL, TRUE); timedated_freeable = g_ptr_array_new(); @@ -148,11 +167,14 @@ int main() { NULL); g_main_loop_run(timedated_loop); + /* runs until single g_main_loop_quit() call is raised inside _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; }