From 6121bfeaf48b68f62c814ca094f5ffb2d32b7dc2 Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Wed, 30 May 2018 20:53:02 +0100 Subject: [PATCH] Add support for clean shutdown via SIGTERM Catch SIGTERM to allow ourselves to do an orderly shutdown. This isn't useful in general operation (as a TERM will do all this cleanup anyway) but allows for testing under valgrind to ensure no leaks or invalid accesses. --- mqtt-arp.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/mqtt-arp.c b/mqtt-arp.c index 114e0ef..19da330 100644 --- a/mqtt-arp.c +++ b/mqtt-arp.c @@ -17,6 +17,7 @@ * along with this program. If not, see . */ #include +#include #include #include #include @@ -64,6 +65,12 @@ struct ma_config { }; bool debug = false; +bool want_shutdown = false; + +void shutdown_request(int signal) +{ + want_shutdown = true; +} bool mac_compare(uint8_t *a, uint8_t *b) { @@ -167,7 +174,7 @@ void main_loop(struct ma_config *config, struct mosquitto *mosq, int sock) hdr = (struct nlmsghdr *) buf; nd = (struct ndmsg *) (hdr + 1); - while (1) { + while (!want_shutdown) { received = recv(sock, buf, sizeof(buf), 0); if (debug) { t = time(NULL); @@ -362,8 +369,16 @@ int main(int argc, char *argv[]) if (!config.location) config.mqtt_host = LOCATION; + signal(SIGTERM, shutdown_request); + sock = netlink_init(); mosq = mqtt_init(&config); main_loop(&config, mosq, sock); + + mosquitto_disconnect(mosq); + mosquitto_loop_stop(mosq, true); + mosquitto_destroy(mosq); + mosquitto_lib_cleanup(); + close(sock); } -- 2.39.5