Fix lack of cleanup.{c,h}
authorJonathan McDowell <noodles@earth.li>
Mon, 6 Sep 2004 15:58:48 +0000 (15:58 +0000)
committerJonathan McDowell <noodles@earth.li>
Mon, 6 Sep 2004 15:58:48 +0000 (15:58 +0000)
Eeep! These never got checked in. Thanks to Ross Burton for pointing out.

cleanup.c [new file with mode: 0644]
cleanup.h [new file with mode: 0644]

diff --git a/cleanup.c b/cleanup.c
new file mode 100644 (file)
index 0000000..9d5e24a
--- /dev/null
+++ b/cleanup.c
@@ -0,0 +1,80 @@
+/*
+ * cleanup.c - Cleanup and shutdown framework.
+ *
+ * Jonathan McDowell <noodles@earth.li>
+ *
+ * Copyright 2004 Project Purple
+ */
+
+#include <signal.h>
+#include <stdbool.h>
+#include <stdlib.h>
+
+#include "cleanup.h"
+#include "keydb.h"
+#include "log.h"
+
+static bool should_cleanup = false;
+
+/*
+ *     trytocleanup - say we should try to cleanup.
+ *
+ *      This function sets the cleanup flag indicating we want to try and
+ *      cleanup ASAP.
+ */
+void trytocleanup(void)
+{
+       logthing(LOGTHING_NOTICE, "Setting cleanup flag.");
+       should_cleanup = true;
+
+       return;
+}
+
+/*
+ *     cleanup - indicate if we should try to cleanup.
+ *
+ *     This function returns a bool which indicates if we want to cleanup and
+ *     exit ASAP.
+ */
+bool cleanup(void)
+{
+       return(should_cleanup);
+}
+
+/**
+ *     sig_cleanup - set the cleanup flag when we receive a signal
+ *
+ *     This is our signal handler; all it does it log the fact we got a signal
+ *     and set the cleanup flag.
+ */
+void sig_cleanup(int signal)
+{
+       logthing(LOGTHING_NOTICE, "Got signal %d.", signal);
+       trytocleanup();
+
+       return;
+}
+
+/**
+ *     catchsignals - Register signal handlers for various signals.
+ *
+ *     This function registers a signal handler for various signals (PIPE,
+ *     ALRM, INT, TERM, HUP) that sets the cleanup flag so we try to exit
+ *     ASAP, but cleanly.
+ */
+void catchsignals(void)
+{
+       struct sigaction alarmh;
+
+       logthing(LOGTHING_NOTICE, "Catching signals");
+
+       memset(&alarmh, 0, sizeof(alarmh));
+       alarmh.sa_handler = sig_cleanup;
+       sigaction(SIGALRM, &alarmh, NULL);
+       sigaction(SIGPIPE, &alarmh, NULL);
+       sigaction(SIGTERM, &alarmh, NULL);
+       sigaction(SIGINT, &alarmh, NULL);
+       sigaction(SIGHUP, &alarmh, NULL);
+
+       return;
+}
diff --git a/cleanup.h b/cleanup.h
new file mode 100644 (file)
index 0000000..c5361c9
--- /dev/null
+++ b/cleanup.h
@@ -0,0 +1,39 @@
+/*
+ * cleanup.h - Cleanup and shutdown framework.
+ *
+ * Jonathan McDowell <noodles@earth.li>
+ *
+ * Copyright 2004 Project Purple
+ */
+
+#ifndef __CLEANUP_H__
+#define __CLEANUP_H__
+
+#include <stdbool.h>
+
+/*
+ *     trytocleanup - say we should try to cleanup.
+ *
+ *      This function sets the cleanup flag indicating we want to try and
+ *      cleanup ASAP.
+ */
+void trytocleanup(void);
+
+/*
+ *     cleanup - indicate if we should try to cleanup.
+ *
+ *     This function returns a bool which indicates if we want to cleanup and
+ *     exit ASAP.
+ */
+bool cleanup(void);
+
+/*
+ *     catchsignals - Register signal handlers for various signals.
+ *
+ *     This function registers a signal handler for various signals (PIPE,
+ *     ALRM, INT, TERM, HUP) that sets the cleanup flag so we try to exit
+ *     ASAP, but cleanly.
+ */
+void catchsignals(void);
+
+#endif /* __CLEANUP_H__ */