From fac6eb71e67fb77b2ab4718e1b66c4e69cb53f1b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 13 Mar 2017 16:03:02 +0000 Subject: [PATCH] [Minor] Disable internal history if handled by plugins --- src/libserver/roll_history.c | 49 ++++++++++++++++++++++++++++++------ src/libserver/roll_history.h | 4 ++- src/rspamd.c | 2 +- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/libserver/roll_history.c b/src/libserver/roll_history.c index d8b9abdec..ae8ea1e38 100644 --- a/src/libserver/roll_history.c +++ b/src/libserver/roll_history.c @@ -17,6 +17,7 @@ #include "rspamd.h" #include "roll_history.h" #include "ucl.h" +#include "lua/lua_common.h" #include "unix-std.h" static const gchar rspamd_history_magic_old[] = {'r', 's', 'h', '1'}; @@ -27,20 +28,43 @@ static const gchar rspamd_history_magic_old[] = {'r', 's', 'h', '1'}; * @return new structure */ struct roll_history * -rspamd_roll_history_new (rspamd_mempool_t *pool, guint max_rows) +rspamd_roll_history_new (rspamd_mempool_t *pool, guint max_rows, + struct rspamd_config *cfg) { - struct roll_history *new; + struct roll_history *history; + lua_State *L = cfg->lua_state; if (pool == NULL || max_rows == 0) { return NULL; } - new = rspamd_mempool_alloc0_shared (pool, sizeof (struct roll_history)); - new->rows = rspamd_mempool_alloc0_shared (pool, - sizeof (struct roll_history_row) * max_rows); - new->nrows = max_rows; + history = rspamd_mempool_alloc0_shared (pool, sizeof (struct roll_history)); - return new; + /* + * Here, we check if there is any plugin that handles history, + * in this case, we disable this code completely + */ + lua_getglobal (L, "plugins"); + if (lua_istable (L, -1)) { + lua_pushstring (L, "history"); + lua_gettable (L, -2); + + if (lua_istable (L, -1)) { + history->disabled = TRUE; + } + + lua_pop (L, 1); + } + + lua_pop (L, 1); + + if (!history->disabled) { + history->rows = rspamd_mempool_alloc0_shared (pool, + sizeof (struct roll_history_row) * max_rows); + history->nrows = max_rows; + } + + return history; } struct history_metric_callback_data { @@ -76,6 +100,10 @@ rspamd_roll_history_update (struct roll_history *history, struct rspamd_metric_result *metric_res; struct history_metric_callback_data cbdata; + if (history->disabled) { + return; + } + /* First of all obtain check and obtain row number */ g_atomic_int_compare_and_exchange (&history->cur_row, history->nrows, 0); #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) @@ -163,6 +191,9 @@ rspamd_roll_history_load (struct roll_history *history, const gchar *filename) guint n, i; g_assert (history != NULL); + if (history->disabled) { + return TRUE; + } if (stat (filename, &st) == -1) { msg_info ("cannot load history from %s: %s", filename, @@ -330,6 +361,10 @@ rspamd_roll_history_save (struct roll_history *history, const gchar *filename) g_assert (history != NULL); + if (history->disabled) { + return TRUE; + } + if ((fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, 00600)) == -1) { msg_info ("cannot save history to %s: %s", filename, strerror (errno)); return FALSE; diff --git a/src/libserver/roll_history.h b/src/libserver/roll_history.h index 26d2a3be1..d8a77bfd7 100644 --- a/src/libserver/roll_history.h +++ b/src/libserver/roll_history.h @@ -30,6 +30,7 @@ #define HISTORY_MAX_ADDR 32 struct rspamd_task; +struct rspamd_config; struct roll_history_row { struct timeval tv; @@ -47,6 +48,7 @@ struct roll_history_row { struct roll_history { struct roll_history_row *rows; + gboolean disabled; guint nrows; guint cur_row; }; @@ -57,7 +59,7 @@ struct roll_history { * @return new structure */ struct roll_history * rspamd_roll_history_new (rspamd_mempool_t *pool, - guint max_rows); + guint max_rows, struct rspamd_config *cfg); /** * Update roll history with data from task diff --git a/src/rspamd.c b/src/rspamd.c index a7933ff7c..1a08c938f 100644 --- a/src/rspamd.c +++ b/src/rspamd.c @@ -1259,7 +1259,7 @@ main (gint argc, gchar **argv, gchar **env) /* Create rolling history */ rspamd_main->history = rspamd_roll_history_new (rspamd_main->server_pool, - rspamd_main->cfg->history_rows); + rspamd_main->cfg->history_rows, rspamd_main->cfg); gperf_profiler_init (rspamd_main->cfg, "main"); -- 2.39.5