Signal catching to force atexit handling.
authorRyan C. Gordon <icculus@icculus.org>
Sat, 26 Apr 2014 16:09:12 -0400
changeset 33 bc1736f8e74d
parent 32 8f45f1e8bcfa
child 34 55a2b89a50cd
Signal catching to force atexit handling.
1pass.c
--- a/1pass.c	Fri Apr 18 22:19:10 2014 -0400
+++ b/1pass.c	Sat Apr 26 16:09:12 2014 -0400
@@ -7,6 +7,7 @@
 #include <errno.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <signal.h>
 
 #include "lua.h"
 #include "lauxlib.h"
@@ -585,8 +586,46 @@
 } // initLua
 
 
+static void deinitAll()
+{
+    deinitPowermate();
+    deinitKeyHook();
+    deinitLua();
+} // deinitAll
+
+
+static void killerSignalCatcher(int sig)
+{
+    static int been_run = 0;
+    if (been_run)
+    {
+        fprintf(stderr, "Caught signal %d, terminating HARD.\n", sig);
+        _exit(0);
+    } // if
+
+    been_run = 1;
+    fprintf(stderr, "Caught signal %d, terminating.\n", sig);
+    exit(0);  // trigger atexit handlers.
+} // killerSignalCatcher
+
+
+static void initSignals(void)
+{
+    signal(SIGINT, killerSignalCatcher);
+    signal(SIGQUIT, killerSignalCatcher);
+    signal(SIGILL, killerSignalCatcher);
+    signal(SIGFPE, killerSignalCatcher);
+    signal(SIGTERM, killerSignalCatcher);
+    signal(SIGPIPE, killerSignalCatcher);
+    signal(SIGSEGV, killerSignalCatcher);
+    signal(SIGABRT, killerSignalCatcher);
+    signal(SIGHUP, killerSignalCatcher);
+} // initSignals
+
+
 int main(int argc, char **argv)
 {
+    initSignals();
     initPowermate(&argc, argv);
     gtk_init(&argc, &argv);