use glib signal handler for gracefully exiting the main loop upon SIGINT
This allows a more sensible checking for memory leaks with valgrind, the
previous exit() left a lot of memory unreleased which shows up as false
positives in valgrind.
#include <pthread.h>
#include <signal.h>
#include <glib.h>
+#include <glib-unix.h>
#include <gio/gio.h>
#include <getopt.h>
#include <poll.h>
@@ -163,17 +164,16 @@ static int open_handlers(void)
return num_good;
}
-static void sighandler(int signal)
+static gboolean sighandler(gpointer user_data)
{
tcmulib_cleanup_all_cmdproc_threads();
tcmu_cancel_log_thread();
tcmu_cancel_config_thread(tcmu_cfg);
- exit(1);
-}
-static struct sigaction tcmu_sigaction = {
- .sa_handler = sighandler,
-};
+ g_main_loop_quit((GMainLoop*)user_data);
+
+ return G_SOURCE_CONTINUE;
+}
gboolean tcmulib_callback(GIOChannel *source,
GIOCondition condition,
@@ -880,9 +880,10 @@ int main(int argc, char **argv)
goto err_out;
}
- ret = sigaction(SIGINT, &tcmu_sigaction, NULL);
- if (ret) {
- tcmu_err("couldn't set sigaction\n");
+ loop = g_main_loop_new(NULL, FALSE);
+ if (g_unix_signal_add(SIGINT, sighandler, loop) <= 0 ||
+ g_unix_signal_add(SIGTERM, sighandler, loop) <= 0) {
+ tcmu_err("couldn't setup signal handlers\n");
goto err_tcmulib_close;
}
@@ -901,7 +902,6 @@ int main(int argc, char **argv)
NULL // user date free func
);
- loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
tcmu_dbg("Exiting...\n");
