]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Core: Add methods to enable/disable symbols
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 14 Dec 2018 19:47:34 +0000 (19:47 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 14 Dec 2018 19:47:34 +0000 (19:47 +0000)
src/libserver/rspamd_symcache.c
src/libserver/rspamd_symcache.h
src/lua/lua_config.c

index 42ec9bc85bee68877bf872b8dcf1fda16c4a5e60..2eff4382645e95fb29deedadea18042e2b7554c5 100644 (file)
@@ -56,11 +56,15 @@ INIT_LOG_MODULE(symcache)
        ((dyn_item)->started)
 #define SET_START_BIT(checkpoint, dyn_item) \
        (dyn_item)->started = 1
+#define CLR_START_BIT(checkpoint, dyn_item) \
+       (dyn_item)->started = 0
 
 #define CHECK_FINISH_BIT(checkpoint, dyn_item) \
        ((dyn_item)->finished)
 #define SET_FINISH_BIT(checkpoint, dyn_item) \
        (dyn_item)->finished = 1
+#define CLR_FINISH_BIT(checkpoint, dyn_item) \
+       (dyn_item)->finished = 0
 
 static const guchar rspamd_symcache_magic[8] = {'r', 's', 'c', 2, 0, 0, 0, 0 };
 
@@ -2413,8 +2417,8 @@ rspamd_symcache_is_checked (struct rspamd_task *task,
 }
 
 void
-rspamd_symcache_disable_symbol (struct rspamd_symcache *cache,
-                                                               const gchar *symbol)
+rspamd_symcache_disable_symbol_perm (struct rspamd_symcache *cache,
+                                                                        const gchar *symbol)
 {
        struct rspamd_symcache_item *item;
 
@@ -2429,8 +2433,8 @@ rspamd_symcache_disable_symbol (struct rspamd_symcache *cache,
 }
 
 void
-rspamd_symcache_enable_symbol (struct rspamd_symcache *cache,
-                                                          const gchar *symbol)
+rspamd_symcache_enable_symbol_perm (struct rspamd_symcache *cache,
+                                                                       const gchar *symbol)
 {
        struct rspamd_symcache_item *item;
 
@@ -2506,6 +2510,78 @@ rspamd_symcache_is_symbol_enabled (struct rspamd_task *task,
        return ret;
 }
 
+
+gboolean
+rspamd_symcache_enable_symbol (struct rspamd_task *task,
+                                                          struct rspamd_symcache *cache,
+                                                          const gchar *symbol)
+{
+       struct cache_savepoint *checkpoint;
+       struct rspamd_symcache_item *item;
+       struct rspamd_symcache_dynamic_item *dyn_item;
+       gboolean ret = FALSE;
+
+       g_assert (cache != NULL);
+       g_assert (symbol != NULL);
+
+       checkpoint = task->checkpoint;
+
+       if (checkpoint) {
+               item = rspamd_symcache_find_filter (cache, symbol);
+
+               if (item) {
+                       dyn_item = rspamd_symcache_get_dynamic (checkpoint, item);
+
+                       if (CHECK_FINISH_BIT (checkpoint, dyn_item)) {
+                               ret = TRUE;
+                               CLR_START_BIT (checkpoint, dyn_item);
+                               CLR_FINISH_BIT (checkpoint, dyn_item);
+                       }
+                       else {
+                               msg_debug_task ("cannot enable symbol %s: already started", symbol);
+                       }
+               }
+       }
+
+       return ret;
+}
+
+
+gboolean
+rspamd_symcache_disable_symbol (struct rspamd_task *task,
+                                                               struct rspamd_symcache *cache,
+                                                               const gchar *symbol)
+{
+       struct cache_savepoint *checkpoint;
+       struct rspamd_symcache_item *item;
+       struct rspamd_symcache_dynamic_item *dyn_item;
+       gboolean ret = FALSE;
+
+       g_assert (cache != NULL);
+       g_assert (symbol != NULL);
+
+       checkpoint = task->checkpoint;
+
+       if (checkpoint) {
+               item = rspamd_symcache_find_filter (cache, symbol);
+
+               if (item) {
+                       dyn_item = rspamd_symcache_get_dynamic (checkpoint, item);
+
+                       if (CHECK_START_BIT (checkpoint, dyn_item)) {
+                               ret = TRUE;
+                               SET_START_BIT (checkpoint, dyn_item);
+                               SET_FINISH_BIT (checkpoint, dyn_item);
+                       }
+                       else {
+                               msg_warn_task ("cannot disable symbol %s: already started", symbol);
+                       }
+               }
+       }
+
+       return ret;
+}
+
 void
 rspamd_symcache_foreach (struct rspamd_symcache *cache,
                                                 void (*func) (gint, const gchar *, gint, gpointer),
index 0542f86b45e17397b0a23b11f5b75ff6cd15bfc1..df495fc8ed244e32fe9b32dd164139955fd7bb2d 100644 (file)
@@ -227,16 +227,16 @@ void rspamd_symcache_add_delayed_dependency (struct rspamd_symcache *cache,
  * @param cache
  * @param symbol
  */
-void rspamd_symcache_disable_symbol (struct rspamd_symcache *cache,
-                                                                        const gchar *symbol);
+void rspamd_symcache_disable_symbol_perm (struct rspamd_symcache *cache,
+                                                                                 const gchar *symbol);
 
 /**
  * Enable specific symbol in the cache
  * @param cache
  * @param symbol
  */
-void rspamd_symcache_enable_symbol (struct rspamd_symcache *cache,
-                                                                       const gchar *symbol);
+void rspamd_symcache_enable_symbol_perm (struct rspamd_symcache *cache,
+                                                                                const gchar *symbol);
 /**
  * Get abstract callback data for a symbol (or its parent symbol)
  * @param cache cache object
@@ -285,6 +285,28 @@ guint64 rspamd_symcache_get_cksum (struct rspamd_symcache *cache);
 gboolean rspamd_symcache_is_symbol_enabled (struct rspamd_task *task,
                                                                                        struct rspamd_symcache *cache,
                                                                                        const gchar *symbol);
+
+/**
+ * Enable this symbol for task
+ * @param task
+ * @param cache
+ * @param symbol
+ * @return TRUE if a symbol has been enabled (not executed before)
+ */
+gboolean rspamd_symcache_enable_symbol (struct rspamd_task *task,
+                                                                               struct rspamd_symcache *cache,
+                                                                               const gchar *symbol);
+
+/**
+ * Enable this symbol for task
+ * @param task
+ * @param cache
+ * @param symbol
+ * @return TRUE if a symbol has been disabled (not executed before)
+ */
+gboolean rspamd_symcache_disable_symbol (struct rspamd_task *task,
+                                                                                struct rspamd_symcache *cache,
+                                                                                const gchar *symbol);
 /**
  * Process specific function for each cache element (in order they are added)
  * @param cache
index 60e1076db89555ddb8458065d45169eee8eb45dc..1965b490390c869f5d3962dbf5cbf1eb55bf543f 100644 (file)
@@ -2533,7 +2533,7 @@ lua_config_enable_symbol (lua_State *L)
        const gchar *sym = luaL_checkstring (L, 2);
 
        if (cfg && sym) {
-               rspamd_symcache_enable_symbol (cfg->cache, sym);
+               rspamd_symcache_enable_symbol_perm (cfg->cache, sym);
        }
        else {
                return luaL_error (L, "invalid arguments");
@@ -2550,7 +2550,7 @@ lua_config_disable_symbol (lua_State *L)
        const gchar *sym = luaL_checkstring (L, 2);
 
        if (cfg && sym) {
-               rspamd_symcache_disable_symbol (cfg->cache, sym);
+               rspamd_symcache_disable_symbol_perm (cfg->cache, sym);
        }
        else {
                return luaL_error (L, "invalid arguments");