2 * cleanup.c - Cleanup and shutdown framework.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2004 Project Purple
17 static bool should_cleanup = false;
20 * trytocleanup - say we should try to cleanup.
22 * This function sets the cleanup flag indicating we want to try and
25 void trytocleanup(void)
27 logthing(LOGTHING_INFO, "Setting cleanup flag.");
28 should_cleanup = true;
34 * cleanup - indicate if we should try to cleanup.
36 * This function returns a bool which indicates if we want to cleanup and
41 return(should_cleanup);
45 * sig_cleanup - set the cleanup flag when we receive a signal
47 * This is our signal handler; all it does it log the fact we got a signal
48 * and set the cleanup flag.
50 void sig_cleanup(int signal)
52 logthing(LOGTHING_INFO, "Got signal %d.", signal);
59 * catchsignals - Register signal handlers for various signals.
61 * This function registers a signal handler for various signals (PIPE,
62 * ALRM, INT, TERM, HUP) that sets the cleanup flag so we try to exit
65 void catchsignals(void)
67 struct sigaction alarmh;
69 logthing(LOGTHING_INFO, "Catching signals");
71 memset(&alarmh, 0, sizeof(alarmh));
72 alarmh.sa_handler = sig_cleanup;
73 sigaction(SIGALRM, &alarmh, NULL);
74 sigaction(SIGPIPE, &alarmh, NULL);
75 sigaction(SIGTERM, &alarmh, NULL);
76 sigaction(SIGINT, &alarmh, NULL);
77 sigaction(SIGHUP, &alarmh, NULL);