From d8723bd78760c0b0919bbfe50636edce09e80d91 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 15 Mar 2016 09:22:07 +0000 Subject: [PATCH] [Feature] Allways clear maps on exit --- src/libserver/worker_util.c | 14 +++++++++++++ src/libutil/map.c | 39 ++++++++++++++++++++++++++++++++++++- src/libutil/map_private.h | 4 ++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c index 3d62e1096..740c4cd7a 100644 --- a/src/libserver/worker_util.c +++ b/src/libserver/worker_util.c @@ -22,6 +22,8 @@ #include "utlist.h" #include "ottery.h" #include "rspamd_control.h" +#include "libutil/map.h" +#include "libutil/map_private.h" #ifdef WITH_GPERF_TOOLS #include @@ -295,6 +297,7 @@ rspamd_worker_stop_accept (struct rspamd_worker *worker) GHashTableIter it; struct rspamd_worker_signal_handler *sigh; gpointer k, v; + struct rspamd_map *map; /* Remove all events */ cur = worker->accept_events; @@ -310,6 +313,7 @@ rspamd_worker_stop_accept (struct rspamd_worker *worker) } g_hash_table_iter_init (&it, worker->signal_events); + while (g_hash_table_iter_next (&it, &k, &v)) { sigh = (struct rspamd_worker_signal_handler *)v; g_hash_table_iter_steal (&it); @@ -318,7 +322,17 @@ rspamd_worker_stop_accept (struct rspamd_worker *worker) } g_free (sigh); } + g_hash_table_unref (worker->signal_events); + + /* Cleanup maps */ + for (cur = worker->srv->cfg->maps; cur != NULL; cur = g_list_next (cur)) { + map = cur->data; + + if (map->dtor) { + map->dtor (map->dtor_data); + } + } } void diff --git a/src/libutil/map.c b/src/libutil/map.c index ac6d58997..c1fc12690 100644 --- a/src/libutil/map.c +++ b/src/libutil/map.c @@ -26,6 +26,8 @@ #include "http_parser.h" static const gchar *hash_fill = "1"; +static void free_http_cbdata_common (struct http_callback_data *cbd); +static void free_http_cbdata_dtor (gpointer p); static void free_http_cbdata (struct http_callback_data *cbd); /** * Write HTTP request @@ -190,7 +192,7 @@ rspamd_map_check_file_sig (const char *fname, * Callback for destroying HTTP callback data */ static void -free_http_cbdata (struct http_callback_data *cbd) +free_http_cbdata_common (struct http_callback_data *cbd) { char fpath[PATH_MAX]; struct stat st; @@ -235,6 +237,26 @@ free_http_cbdata (struct http_callback_data *cbd) g_slice_free1 (sizeof (struct http_callback_data), cbd); } +static void +free_http_cbdata (struct http_callback_data *cbd) +{ + cbd->map->dtor = NULL; + cbd->map->dtor_data = NULL; + + free_http_cbdata_common (cbd); +} + +static void +free_http_cbdata_dtor (gpointer p) +{ + struct http_callback_data *cbd = p; + rspamd_mempool_t *pool; + + pool = cbd->map->pool; + msg_warn_pool ("connection with http server is terminated: worker is stopping"); + free_http_cbdata_common (cbd); +} + /* * HTTP callbacks */ @@ -681,7 +703,10 @@ http_callback (gint fd, short what, void *ud) data->host, RDNS_REQUEST_AAAA)) { REF_RETAIN (cbd); } + jitter_timeout_event (map, FALSE, FALSE, FALSE); + map->dtor = free_http_cbdata_dtor; + map->dtor_data = cbd; } else { msg_warn_pool ("cannot load map: DNS resolver is not initialized"); @@ -732,8 +757,20 @@ rspamd_map_watch (struct rspamd_config *cfg, void rspamd_map_remove_all (struct rspamd_config *cfg) { + struct rspamd_map *map; + GList *cur; + + for (cur = cfg->maps; cur != NULL; cur = g_list_next (cur)) { + map = cur->data; + + if (map->dtor) { + map->dtor (map->dtor_data); + } + } + g_list_free (cfg->maps); cfg->maps = NULL; + if (cfg->map_pool != NULL) { rspamd_mempool_delete (cfg->map_pool); cfg->map_pool = NULL; diff --git a/src/libutil/map_private.h b/src/libutil/map_private.h index 85e468e4e..4350283c7 100644 --- a/src/libutil/map_private.h +++ b/src/libutil/map_private.h @@ -22,6 +22,8 @@ #include "unix-std.h" #include "ref.h" +typedef void (*rspamd_map_dtor) (gpointer p); + enum fetch_proto { MAP_PROTO_FILE, MAP_PROTO_HTTP, @@ -46,6 +48,8 @@ struct rspamd_map { guint32 checksum; /* Shared lock for temporary disabling of map reading (e.g. when this map is written by UI) */ gint *locked; + rspamd_map_dtor dtor; + gpointer dtor_data; }; /** -- 2.39.5