2 * cleanup.c - Cleanup and shutdown framework.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2004 Project Purple
18 static bool should_cleanup = false;
21 * trytocleanup - say we should try to cleanup.
23 * This function sets the cleanup flag indicating we want to try and
26 void trytocleanup(void)
28 logthing(LOGTHING_INFO, "Setting cleanup flag.");
29 should_cleanup = true;
35 * cleanup - indicate if we should try to cleanup.
37 * This function returns a bool which indicates if we want to cleanup and
42 return(should_cleanup);
46 * sig_cleanup - set the cleanup flag when we receive a signal
48 * This is our signal handler; all it does it log the fact we got a signal
49 * and set the cleanup flag.
51 void sig_cleanup(int signal)
53 logthing(LOGTHING_INFO, "Got signal %d.", signal);
60 * catchsignals - Register signal handlers for various signals.
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
66 void catchsignals(void)
68 struct sigaction alarmh;
70 logthing(LOGTHING_INFO, "Catching signals");
72 memset(&alarmh, 0, sizeof(alarmh));
73 alarmh.sa_handler = sig_cleanup;
74 sigaction(SIGALRM, &alarmh, NULL);
75 sigaction(SIGPIPE, &alarmh, NULL);
76 sigaction(SIGTERM, &alarmh, NULL);
77 sigaction(SIGINT, &alarmh, NULL);
78 sigaction(SIGHUP, &alarmh, NULL);