From 2370da82e74c99fef924a7b28bcc91c93be8e352 Mon Sep 17 00:00:00 2001 From: Georges Da Costa <dacosta@irit.fr> Date: Thu, 14 Mar 2024 11:23:17 +0100 Subject: [PATCH] Calls cleaners and free memory at exit even with SIGINT and SIGTERM --- src/mojitos.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/mojitos.c b/src/mojitos.c index e8cb6fa..7c721cd 100644 --- a/src/mojitos.c +++ b/src/mojitos.c @@ -159,14 +159,6 @@ void flush(int none) } FILE *output; -void flushexit() -{ - if (output != NULL) { - fflush(output); - fclose(output); - } -} - unsigned int nb_sources = 0; void **states = NULL; getter_t *getter = NULL; @@ -176,6 +168,25 @@ unsigned int nb_sensors = 0; char **labels = NULL; uint64_t *values = NULL; +void flushexit() +{ + if (output != NULL) { + fflush(output); + fclose(output); + } + for (unsigned int i = 0; i < nb_sources; i++) { + cleaner[i](states[i]); + } + + if (nb_sources > 0) { + free(getter); + free(cleaner); + free(labels); + free(values); + free(states); + } +} + void add_source(Sensor *cpt, char *arg) { nb_sources++; @@ -227,7 +238,8 @@ int main(int argc, char **argv) output = stdout; atexit(flushexit); - signal(15, flush); + signal(SIGTERM, flush); + signal(SIGINT, flush); int opt; struct optparse options; @@ -369,16 +381,5 @@ int main(int argc, char **argv) usleep(1000 * 1000 / frequency - (ts.tv_nsec / 1000) % (1000 * 1000 / frequency)); } - for (unsigned int i = 0; i < nb_sources; i++) { - cleaner[i](states[i]); - } - - if (nb_sources > 0) { - free(getter); - free(cleaner); - free(labels); - free(values); - free(states); - } } -- GitLab