2 * cleanup.c - Cleanup and shutdown framework.
4 * Copyright 2004 Jonathan McDowell <noodles@earth.li>
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.
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
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/>.
25 static bool should_cleanup = false;
28 * trytocleanup - say we should try to cleanup.
30 * This function sets the cleanup flag indicating we want to try and
33 void trytocleanup(void)
35 logthing(LOGTHING_INFO, "Setting cleanup flag.");
36 should_cleanup = true;
42 * cleanup - indicate if we should try to cleanup.
44 * This function returns a bool which indicates if we want to cleanup and
49 return(should_cleanup);
53 * sig_cleanup - set the cleanup flag when we receive a signal
55 * This is our signal handler; all it does it log the fact we got a signal
56 * and set the cleanup flag.
58 void sig_cleanup(int signal)
60 logthing(LOGTHING_INFO, "Got signal %d.", signal);
67 * catchsignals - Register signal handlers for various signals.
69 * This function registers a signal handler for various signals (PIPE,
70 * ALRM, INT, TERM, HUP) that sets the cleanup flag so we try to exit
73 void catchsignals(void)
75 logthing(LOGTHING_INFO, "Catching signals");
77 signal(SIGALRM, &sig_cleanup);
78 signal(SIGPIPE, &sig_cleanup);
79 signal(SIGTERM, &sig_cleanup);
80 signal(SIGINT, &sig_cleanup);
81 signal(SIGHUP, &sig_cleanup);