commit d3bb11a: [Fix] Allow to set priorities between post init scripts

Vsevolod Stakhov vsevolod at highsecure.ru
Wed Dec 30 17:28:07 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-12-30 17:23:31 +0000
URL: https://github.com/rspamd/rspamd/commit/d3bb11ac4358dc6b96044bcb681112520cf9c272

[Fix] Allow to set priorities between post init scripts

---
 src/libserver/cfg_file.h |  1 +
 src/lua/lua_common.c     |  4 ++--
 src/lua/lua_config.c     | 15 +++++++++++++++
 3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index 723f12c16..50649b106 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -311,6 +311,7 @@ struct rspamd_action;
 
 struct rspamd_config_cfg_lua_script {
 	gint cbref;
+	gint priority;
 	struct rspamd_config_cfg_lua_script *prev, *next;
 };
 
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c
index 87474793c..482245ac9 100644
--- a/src/lua/lua_common.c
+++ b/src/lua/lua_common.c
@@ -2159,8 +2159,8 @@ rspamd_lua_run_config_post_init (lua_State *L, struct rspamd_config *cfg)
 		rspamd_lua_setclass (L, "rspamd{config}", -1);
 
 		if (lua_pcall (L, 1, 0, err_idx) != 0) {
-			msg_err_config ("cannot run config post init script: %s",
-					lua_tostring (L, -1));
+			msg_err_config ("cannot run config post init script: %s; priority = %d",
+					lua_tostring (L, -1), sc->priority);
 		}
 
 		lua_settop (L, err_idx - 1);
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index 836654500..89f93730d 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -3155,21 +3155,36 @@ lua_config_add_on_load (lua_State *L)
 	return 0;
 }
 
+static inline int
+rspamd_post_init_sc_sort (const struct rspamd_config_cfg_lua_script *pra,
+				const struct rspamd_config_cfg_lua_script *prb)
+{
+	/* Inverse sort */
+	return prb->priority - pra->priority;
+}
+
 static gint
 lua_config_add_post_init (lua_State *L)
 {
 	LUA_TRACE_POINT;
 	struct rspamd_config *cfg = lua_check_config (L, 1);
 	struct rspamd_config_cfg_lua_script *sc;
+	guint priority = 0;
 
 	if (cfg == NULL || lua_type (L, 2) != LUA_TFUNCTION) {
 		return luaL_error (L, "invalid arguments");
 	}
 
+	if (lua_type (L, 3) == LUA_TNUMBER) {
+		priority = lua_tointeger (L , 3);
+	}
+
 	sc = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*sc));
 	lua_pushvalue (L, 2);
 	sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX);
+	sc->priority = priority;
 	DL_APPEND (cfg->post_init_scripts, sc);
+	DL_SORT (cfg->post_init_scripts, rspamd_post_init_sc_sort);
 
 	return 0;
 }


More information about the Commits mailing list