]> the.earth.li Git - onak.git/blob - cleanup.c
0.6.3 release
[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, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <signal.h>
20 #include <stdbool.h>
21
22 #include "cleanup.h"
23 #include "log.h"
24 #include "onak-conf.h"
25
26 static bool should_cleanup = false;
27
28 /*
29  *      trytocleanup - say we should try to cleanup.
30  *
31  *      This function sets the cleanup flag indicating we want to try and
32  *      cleanup ASAP.
33  */
34 void trytocleanup(void)
35 {
36         logthing(LOGTHING_INFO, "Setting cleanup flag.");
37         should_cleanup = true;
38
39         return;
40 }
41
42 /*
43  *      cleanup - indicate if we should try to cleanup.
44  *
45  *      This function returns a bool which indicates if we want to cleanup and
46  *      exit ASAP.
47  */
48 bool cleanup(void)
49 {
50         return(should_cleanup);
51 }
52
53 /**
54  *      sig_cleanup - set the cleanup flag when we receive a signal
55  *
56  *      This is our signal handler; all it does it log the fact we got a signal
57  *      and set the cleanup flag.
58  */
59 void sig_cleanup(int signal)
60 {
61         logthing(LOGTHING_INFO, "Got signal %d.", signal);
62         trytocleanup();
63
64         return;
65 }
66
67 /**
68  *      catchsignals - Register signal handlers for various signals.
69  *
70  *      This function registers a signal handler for various signals (PIPE,
71  *      ALRM, INT, TERM, HUP) that sets the cleanup flag so we try to exit
72  *      ASAP, but cleanly.
73  */
74 void catchsignals(void)
75 {
76         if (config.use_keyd) {
77                 return;
78         }
79
80         logthing(LOGTHING_INFO, "Catching signals");
81
82         signal(SIGALRM, &sig_cleanup);
83         signal(SIGPIPE, &sig_cleanup);
84         signal(SIGTERM, &sig_cleanup);
85         signal(SIGINT, &sig_cleanup);
86         signal(SIGHUP, &sig_cleanup);
87
88         return;
89 }