From 39d7cea180c3571d79d74d4efcd0efd9f820b53a Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Thu, 10 Jun 2021 23:16:22 -0400 Subject: Added the `cleanup()`, and `cleanup_config()` functions. --- config.c | 11 +++++++++++ config.h | 1 + pullreqd.c | 20 +++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/config.c b/config.c index f0622c5..8f5722b 100644 --- a/config.c +++ b/config.c @@ -105,6 +105,17 @@ void set_config_opt(config *conf, config_type type, size_t offset, config_val va } } +void cleanup_config(config *conf) { + char *cfg = (char *)conf; + for (const config_opt *opt = config_opts; opt->name != NULL; opt++) { + /* Is this config option's type a string, and is the string of that config option in conf not NULL? */ + if (opt->type == TYPE_STRING && *(char **)(cfg+opt->offset) != NULL) { + free(*(char **)(cfg+opt->offset)); + } + } + free(conf); +} + config *parse_config(const char *filename) { /* Size of the file, in bytes. */ long filesize = 0; diff --git a/config.h b/config.h index 42b8929..e461369 100644 --- a/config.h +++ b/config.h @@ -52,4 +52,5 @@ static const config_opt config_opts[] = { }; extern config *parse_config(const char *filename); +extern void cleanup_config(config *conf); #endif diff --git a/pullreqd.c b/pullreqd.c index ae61c54..9f0a0f9 100644 --- a/pullreqd.c +++ b/pullreqd.c @@ -110,6 +110,22 @@ int main_loop(config *cfg, int listen_socket) { for (; !done;) { break; } + + return 0; +} + +void cleanup(config *cfg, int listen_socket) { + if (listen_socket > 0) { + close(listen_socket); + /* Is this a unix domain socket? */ + if (!strcasecmp(cfg->sock_type, "unix")) { + unlink(cfg->sock); + } + } + + if (cfg) { + cleanup_config(cfg); + } } int main(int argc, char **argv) { @@ -136,9 +152,11 @@ int main(int argc, char **argv) { exit_status = main_loop(cfg, listen_socket); } + cleanup(cfg, listen_socket); + syslog(LOG_NOTICE, "pullreqd stopped."); closelog(); - return EXIT_SUCCESS; + return exit_status; } -- cgit v1.2.3-13-gbd6f