Update Debian Vcs-* fields to point to git repository
[onak.git] / cleanup.c
1 /*
2  * cleanup.c - Cleanup and shutdown framework.
3  *
4  * Copyright 2004 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <signal.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "cleanup.h"
26 #include "keydb.h"
27 #include "log.h"
28
29 static bool should_cleanup = false;
30
31 /*
32  *      trytocleanup - say we should try to cleanup.
33  *
34  *      This function sets the cleanup flag indicating we want to try and
35  *      cleanup ASAP.
36  */
37 void trytocleanup(void)
38 {
39         logthing(LOGTHING_INFO, "Setting cleanup flag.");
40         should_cleanup = true;
41
42         return;
43 }
44
45 /*
46  *      cleanup - indicate if we should try to cleanup.
47  *
48  *      This function returns a bool which indicates if we want to cleanup and
49  *      exit ASAP.
50  */
51 bool cleanup(void)
52 {
53         return(should_cleanup);
54 }
55
56 /**
57  *      sig_cleanup - set the cleanup flag when we receive a signal
58  *
59  *      This is our signal handler; all it does it log the fact we got a signal
60  *      and set the cleanup flag.
61  */
62 void sig_cleanup(int signal)
63 {
64         logthing(LOGTHING_INFO, "Got signal %d.", signal);
65         trytocleanup();
66
67         return;
68 }
69
70 /**
71  *      catchsignals - Register signal handlers for various signals.
72  *
73  *      This function registers a signal handler for various signals (PIPE,
74  *      ALRM, INT, TERM, HUP) that sets the cleanup flag so we try to exit
75  *      ASAP, but cleanly.
76  */
77 void catchsignals(void)
78 {
79         logthing(LOGTHING_INFO, "Catching signals");
80
81         signal(SIGALRM, &sig_cleanup);
82         signal(SIGPIPE, &sig_cleanup);
83         signal(SIGTERM, &sig_cleanup);
84         signal(SIGINT, &sig_cleanup);
85         signal(SIGHUP, &sig_cleanup);
86
87         return;
88 }