Only seed database for Debian install if we're using default config
[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 #include <string.h>
13
14 #include "cleanup.h"
15 #include "keydb.h"
16 #include "log.h"
17
18 static bool should_cleanup = false;
19
20 /*
21  *      trytocleanup - say we should try to cleanup.
22  *
23  *      This function sets the cleanup flag indicating we want to try and
24  *      cleanup ASAP.
25  */
26 void trytocleanup(void)
27 {
28         logthing(LOGTHING_INFO, "Setting cleanup flag.");
29         should_cleanup = true;
30
31         return;
32 }
33
34 /*
35  *      cleanup - indicate if we should try to cleanup.
36  *
37  *      This function returns a bool which indicates if we want to cleanup and
38  *      exit ASAP.
39  */
40 bool cleanup(void)
41 {
42         return(should_cleanup);
43 }
44
45 /**
46  *      sig_cleanup - set the cleanup flag when we receive a signal
47  *
48  *      This is our signal handler; all it does it log the fact we got a signal
49  *      and set the cleanup flag.
50  */
51 void sig_cleanup(int signal)
52 {
53         logthing(LOGTHING_INFO, "Got signal %d.", signal);
54         trytocleanup();
55
56         return;
57 }
58
59 /**
60  *      catchsignals - Register signal handlers for various signals.
61  *
62  *      This function registers a signal handler for various signals (PIPE,
63  *      ALRM, INT, TERM, HUP) that sets the cleanup flag so we try to exit
64  *      ASAP, but cleanly.
65  */
66 void catchsignals(void)
67 {
68         logthing(LOGTHING_INFO, "Catching signals");
69
70         signal(SIGALRM, &sig_cleanup);
71         signal(SIGPIPE, &sig_cleanup);
72         signal(SIGTERM, &sig_cleanup);
73         signal(SIGINT, &sig_cleanup);
74         signal(SIGHUP, &sig_cleanup);
75
76         return;
77 }