commit d96dc9f033f4585f1a73cf8c99908d1a3fb3b8a2
parent 36fa8833a91a20fe2cf0e3f4b0997fb79e40f468
Author: Sweets <Sweets@users.noreply.github.com>
Date: Sun, 26 Apr 2020 14:28:24 -0700
Add: signal handling and loop quitting
Diffstat:
3 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/callbacks.c b/callbacks.c
@@ -88,6 +88,7 @@ void bus_acquired(GDBusConnection *connection, const gchar *name,
#ifdef DEBUG
print("%s\n", "Unable to register.");
#endif
+ stop_main_loop(NULL);
}
}
@@ -107,7 +108,7 @@ void name_lost(GDBusConnection *connection, const gchar *name,
print("%s; %s\n",
"Unable to connect to acquire org.freedesktop.Notifications",
"could not connect to dbus.");
- exit(1);
+ stop_main_loop(NULL);
}
#ifdef DEBUG
else
diff --git a/tiramisu.c b/tiramisu.c
@@ -1,8 +1,10 @@
#include <stdio.h>
#include <string.h>
+#include <signal.h>
#include <gio/gio.h>
#include <glib.h>
+#include <glib-unix.h>
#include "tiramisu.h"
#include "callbacks.h"
@@ -10,6 +12,7 @@
GDBusConnection *dbus_connection = NULL;
GDBusNodeInfo *introspection = NULL;
+GMainLoop *main_loop = NULL;
/* Build introspection XML based on configuration */
@@ -42,9 +45,13 @@ const char *xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
" </interface>\n"
"</node>";
-int main(int argc, char **argv) {
- GMainLoop *main_loop;
+gboolean stop_main_loop(gpointer user_data) {
+ g_main_loop_quit(main_loop);
+
+ return G_SOURCE_CONTINUE;
+}
+int main(int argc, char **argv) {
guint owned_name;
/* Connect to DBUS */
@@ -63,9 +70,15 @@ int main(int argc, char **argv) {
main_loop = g_main_loop_new(NULL, FALSE);
+ guint signal_term = g_unix_signal_add(SIGTERM, stop_main_loop, NULL);
+ guint signal_int = g_unix_signal_add(SIGINT, stop_main_loop, NULL);
+
g_main_loop_run(main_loop);
g_clear_pointer(&main_loop, g_main_loop_unref);
+ g_source_remove(signal_term);
+ g_source_remove(signal_int);
+
g_clear_pointer(&introspection, g_dbus_node_info_unref);
g_bus_unown_name(owned_name);
diff --git a/tiramisu.h b/tiramisu.h
@@ -10,5 +10,8 @@
extern GDBusConnection *dbus_connection;
extern GDBusNodeInfo *introspection;
+extern GMainLoop *main_loop;
#define print(...) fprintf(stderr, __VA_ARGS__);
+
+gboolean stop_main_loop(gpointer);