Add signal handlers
authorIra W. Snyder <devel@irasnyder.com>
Sat, 19 Jan 2008 22:50:11 +0000 (14:50 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Sat, 19 Jan 2008 22:50:11 +0000 (14:50 -0800)
This adds a signal handler which calls gtk_main_quit() for the following
signals: SIGINT, SIGQUIT, SIGABRT, SIGTERM, SIGKILL. It gives us a place
to clean up, even if we have a problem.

If handling is needed for more signals, they can be added later. They can
be differentiated in the signal handler by using the signum variable.

tilda.c

diff --git a/tilda.c b/tilda.c
index d1f6a2c..25209fc 100644 (file)
--- a/tilda.c
+++ b/tilda.c
@@ -1,5 +1,6 @@
 #include <glib.h>
 #include <stdlib.h>
+#include <signal.h>
 
 #include "tilda.h"
 #include "tilda-window.h"
@@ -178,6 +179,33 @@ tilda_parse_command_line (gint argc, gchar *argv[])
        }
 }
 
+static void
+tilda_termination_handler (gint signum)
+{
+       debug_enter  ();
+       debug_printf ("signum: %d\n", signum);
+
+       gtk_main_quit ();
+}
+
+/* Hook up all system signal handlers */
+static void
+tilda_signal_handlers_init ()
+{
+       struct sigaction sa;
+
+       /* Hook up signal handlers */
+       sa.sa_handler = tilda_termination_handler;
+       sigemptyset (&sa.sa_mask);
+       sa.sa_flags = 0;
+
+       sigaction (SIGINT,  &sa, NULL);
+       sigaction (SIGQUIT, &sa, NULL);
+       sigaction (SIGABRT, &sa, NULL);
+       sigaction (SIGTERM, &sa, NULL);
+       sigaction (SIGKILL, &sa, NULL);
+}
+
 int main (int argc, char *argv[])
 {
        debug_enter ();
@@ -197,6 +225,9 @@ int main (int argc, char *argv[])
        /* Initialize GTK+ (and the GObject system) */
        gtk_init (&argc, &argv);
 
+       /* Hook up the signal handlers */
+       tilda_signal_handlers_init ();
+
        /* Initialize the keybinder */
        tomboy_keybinder_init ();