commit df13776: [Minor] Core: Add methods to enable/disable symbols

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Dec 27 18:28:08 UTC 2018


Author: Vsevolod Stakhov
Date: 2018-12-14 19:47:34 +0000
URL: https://github.com/rspamd/rspamd/commit/df1377662731d3f071510a7884ff6f8c759404a8

[Minor] Core: Add methods to enable/disable symbols

---
 src/libserver/rspamd_symcache.c | 84 +++++++++++++++++++++++++++++++++++++++--
 src/libserver/rspamd_symcache.h | 30 +++++++++++++--
 src/lua/lua_config.c            |  4 +-
 3 files changed, 108 insertions(+), 10 deletions(-)

diff --git a/src/libserver/rspamd_symcache.c b/src/libserver/rspamd_symcache.c
index 42ec9bc85..2eff43826 100644
--- a/src/libserver/rspamd_symcache.c
+++ b/src/libserver/rspamd_symcache.c
@@ -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),
diff --git a/src/libserver/rspamd_symcache.h b/src/libserver/rspamd_symcache.h
index 0542f86b4..df495fc8e 100644
--- a/src/libserver/rspamd_symcache.h
+++ b/src/libserver/rspamd_symcache.h
@@ -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
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index 60e1076db..1965b4903 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -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");


More information about the Commits mailing list