commit 97f3650: [Minor] Lua_config: Add config unload scripts

Vsevolod Stakhov vsevolod at highsecure.ru
Sat Jun 15 11:42:03 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-06-15 12:38:14 +0100
URL: https://github.com/rspamd/rspamd/commit/97f3650d65c7a359d2218986c680b29ded51e976 (HEAD -> master)

[Minor] Lua_config: Add config unload scripts

---
 src/libserver/cfg_file.h  |  3 ++-
 src/libserver/cfg_utils.c |  6 ++++++
 src/lua/lua_common.c      | 25 +++++++++++++++++++++++++
 src/lua/lua_common.h      |  1 +
 src/lua/lua_config.c      | 29 +++++++++++++++++++++++++++++
 5 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index 985fd00d7..07e66b826 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -464,8 +464,9 @@ struct rspamd_config {
 	GHashTable *trusted_keys;						/**< list of trusted public keys						*/
 
 	struct rspamd_config_cfg_lua_script *on_load_scripts;	/**< list of scripts executed on workers load			*/
-	struct rspamd_config_cfg_lua_script *post_init_scripts;	/**< list of scripts executed on workers load			*/
+	struct rspamd_config_cfg_lua_script *post_init_scripts;	/**< list of scripts executed on config being fully loaded			*/
 	struct rspamd_config_cfg_lua_script *on_term_scripts; /**< list of callbacks called on worker's termination	*/
+	struct rspamd_config_cfg_lua_script *config_unload_scripts;	/**< list of scripts executed on config unload			*/
 
 	gchar *ssl_ca_path;								/**< path to CA certs									*/
 	gchar *ssl_ciphers;								/**< set of preferred ciphers							*/
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index 10c5b5092..ad61f5777 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -249,6 +249,8 @@ rspamd_config_free (struct rspamd_config *cfg)
 	struct rspamd_config_settings_elt *set, *stmp;
 	struct rspamd_worker_log_pipe *lp, *ltmp;
 
+	rspamd_lua_run_config_unload (cfg->lua_state, cfg);
+
 	/* Scripts part */
 	DL_FOREACH_SAFE (cfg->on_term_scripts, sc, sctmp) {
 		luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
@@ -262,6 +264,10 @@ rspamd_config_free (struct rspamd_config *cfg)
 		luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
 	}
 
+	DL_FOREACH_SAFE (cfg->config_unload_scripts, sc, sctmp) {
+		luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
+	}
+
 	DL_FOREACH_SAFE (cfg->setting_ids, set, stmp) {
 		REF_RELEASE (set);
 	}
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c
index 6ee69c072..a912fb5b5 100644
--- a/src/lua/lua_common.c
+++ b/src/lua/lua_common.c
@@ -1895,6 +1895,31 @@ rspamd_lua_run_config_post_init (lua_State *L, struct rspamd_config *cfg)
 	}
 }
 
+
+void
+rspamd_lua_run_config_unload (lua_State *L, struct rspamd_config *cfg)
+{
+	struct rspamd_config_cfg_lua_script *sc;
+	struct rspamd_config **pcfg;
+
+	LL_FOREACH (cfg->post_init_scripts, sc) {
+		lua_pushcfunction (L, &rspamd_lua_traceback);
+		gint err_idx = lua_gettop (L);
+
+		lua_rawgeti (L, LUA_REGISTRYINDEX, sc->cbref);
+		pcfg = lua_newuserdata (L, sizeof (*pcfg));
+		*pcfg = 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));
+		}
+
+		lua_settop (L, err_idx - 1);
+	}
+}
+
 static void
 rspamd_lua_run_postloads_error (struct thread_entry *thread, int ret, const char *msg)
 {
diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h
index 258c41491..ee6fe9da6 100644
--- a/src/lua/lua_common.h
+++ b/src/lua/lua_common.h
@@ -421,6 +421,7 @@ void rspamd_lua_run_postloads (lua_State *L, struct rspamd_config *cfg,
 		struct event_base *ev_base, struct rspamd_worker *w);
 
 void rspamd_lua_run_config_post_init (lua_State *L, struct rspamd_config *cfg);
+void rspamd_lua_run_config_unload (lua_State *L, struct rspamd_config *cfg);
 
 /**
  * Adds new destructor for a local function for specific pool
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index 9f1c7a12b..27a2e32a8 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -592,6 +592,14 @@ LUA_FUNCTION_DEF (config, add_periodic);
  */
 LUA_FUNCTION_DEF (config, add_post_init);
 
+/***
+ * @method rspamd_config:add_config_unload(function(cfg) ... end)
+ * Registers the following script to be executed when configuration is unloaded
+ * @available 2.0+
+ * @param {function} script function to be executed
+ */
+LUA_FUNCTION_DEF (config, add_config_unload);
+
 /***
  * @method rspamd_config:get_symbols_count()
  * Returns number of symbols registered in rspamd configuration
@@ -843,6 +851,7 @@ static const struct luaL_reg configlib_m[] = {
 	LUA_INTERFACE_DEF (config, add_on_load),
 	LUA_INTERFACE_DEF (config, add_periodic),
 	LUA_INTERFACE_DEF (config, add_post_init),
+	LUA_INTERFACE_DEF (config, add_config_unload),
 	LUA_INTERFACE_DEF (config, get_symbols_count),
 	LUA_INTERFACE_DEF (config, get_symbols_cksum),
 	LUA_INTERFACE_DEF (config, get_symbols_counters),
@@ -3017,6 +3026,26 @@ lua_config_add_post_init (lua_State *L)
 	return 0;
 }
 
+static gint
+lua_config_add_config_unload (lua_State *L)
+{
+	LUA_TRACE_POINT;
+	struct rspamd_config *cfg = lua_check_config (L, 1);
+	struct rspamd_config_cfg_lua_script *sc;
+
+	if (cfg == NULL || lua_type (L, 2) != LUA_TFUNCTION) {
+		return luaL_error (L, "invalid arguments");
+	}
+
+	sc = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*sc));
+	lua_pushvalue (L, 2);
+	sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX);
+	DL_APPEND (cfg->config_unload_scripts, sc);
+
+	return 0;
+}
+
+
 static void lua_periodic_callback_finish (struct thread_entry *thread, int ret);
 static void lua_periodic_callback_error (struct thread_entry *thread, int ret, const char *msg);
 


More information about the Commits mailing list