]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Disable internal history if handled by plugins
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 13 Mar 2017 16:03:02 +0000 (16:03 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 13 Mar 2017 16:03:02 +0000 (16:03 +0000)
src/libserver/roll_history.c
src/libserver/roll_history.h
src/rspamd.c

index d8b9abdec07aed59d2baeba9ef6a3f3ac6fe1f2d..ae8ea1e38371a5d5b56e006bb266dfa0077800a6 100644 (file)
@@ -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;
index 26d2a3be1083109d6305d0f117a76cfd8cd62433..d8a77bfd7d9009d5670b9cb6425fee7e8bfcf244 100644 (file)
@@ -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
index a7933ff7c4b377914e1cfa1aa93f7695ecf67130..1a08c938f2a706043916825f79fd55285650b32f 100644 (file)
@@ -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");