From: Jonathan McDowell Date: Mon, 6 Sep 2004 15:58:48 +0000 (+0000) Subject: Fix lack of cleanup.{c,h} X-Git-Url: https://git.sommitrealweird.co.uk/onak.git/commitdiff_plain/1daf67e8e9262de579ba9d23691482529243cce3 Fix lack of cleanup.{c,h} Eeep! These never got checked in. Thanks to Ross Burton for pointing out. --- diff --git a/cleanup.c b/cleanup.c new file mode 100644 index 0000000..9d5e24a --- /dev/null +++ b/cleanup.c @@ -0,0 +1,80 @@ +/* + * cleanup.c - Cleanup and shutdown framework. + * + * Jonathan McDowell + * + * Copyright 2004 Project Purple + */ + +#include +#include +#include + +#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 index 0000000..c5361c9 --- /dev/null +++ b/cleanup.h @@ -0,0 +1,39 @@ +/* + * cleanup.h - Cleanup and shutdown framework. + * + * Jonathan McDowell + * + * Copyright 2004 Project Purple + */ + +#ifndef __CLEANUP_H__ +#define __CLEANUP_H__ + +#include + +/* + * 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__ */