Fix lack of cleanup.{c,h}
[onak.git] / cleanup.c
1 /*
2  * cleanup.c - Cleanup and shutdown framework.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2004 Project Purple
7  */
8
9 #include <signal.h>
10 #include <stdbool.h>
11 #include <stdlib.h>
12
13 #include "cleanup.h"
14 #include "keydb.h"
15 #include "log.h"
16
17 static bool should_cleanup = false;
18
19 /*
20  *      trytocleanup - say we should try to cleanup.
21  *
22  *      This function sets the cleanup flag indicating we want to try and
23  *      cleanup ASAP.
24  */
25 void trytocleanup(void)
26 {
27         logthing(LOGTHING_NOTICE, "Setting cleanup flag.");
28         should_cleanup = true;
29
30         return;
31 }
32
33 /*
34  *      cleanup - indicate if we should try to cleanup.
35  *
36  *      This function returns a bool which indicates if we want to cleanup and
37  *      exit ASAP.
38  */
39 bool cleanup(void)
40 {
41         return(should_cleanup);
42 }
43
44 /**
45  *      sig_cleanup - set the cleanup flag when we receive a signal
46  *
47  *      This is our signal handler; all it does it log the fact we got a signal
48  *      and set the cleanup flag.
49  */
50 void sig_cleanup(int signal)
51 {
52         logthing(LOGTHING_NOTICE, "Got signal %d.", signal);
53         trytocleanup();
54
55         return;
56 }
57
58 /**
59  *      catchsignals - Register signal handlers for various signals.
60  *
61  *      This function registers a signal handler for various signals (PIPE,
62  *      ALRM, INT, TERM, HUP) that sets the cleanup flag so we try to exit
63  *      ASAP, but cleanly.
64  */
65 void catchsignals(void)
66 {
67         struct sigaction alarmh;
68
69         logthing(LOGTHING_NOTICE, "Catching signals");
70
71         memset(&alarmh, 0, sizeof(alarmh));
72         alarmh.sa_handler = sig_cleanup;
73         sigaction(SIGALRM, &alarmh, NULL);
74         sigaction(SIGPIPE, &alarmh, NULL);
75         sigaction(SIGTERM, &alarmh, NULL);
76         sigaction(SIGINT, &alarmh, NULL);
77         sigaction(SIGHUP, &alarmh, NULL);
78
79         return;
80 }