aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/libserver/cfg_file.h7
-rw-r--r--src/libserver/cfg_utils.c18
-rw-r--r--src/lua/lua_config.c27
3 files changed, 52 insertions, 0 deletions
diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index 2dd6addcb..0986ed035 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -259,6 +259,11 @@ struct metric {
struct metric_action actions[METRIC_ACTION_MAX]; /**< all actions of the metric */
};
+struct rspamd_config_post_load_script {
+ gint cbref;
+ struct rspamd_config_post_load_script *prev, *next;
+};
+
/**
* Structure that stores all config data
*/
@@ -395,6 +400,8 @@ struct rspamd_config {
GHashTable *trusted_keys; /**< list of trusted public keys */
+ struct rspamd_config_post_load_script *on_load; /**< list of scripts executed on config load */
+
ref_entry_t ref; /**< reference counter */
};
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index 0685dc800..279a1b357 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -616,6 +616,8 @@ rspamd_config_post_load (struct rspamd_config *cfg, gboolean validate_cache)
struct timespec ts;
#endif
struct metric *def_metric;
+ struct rspamd_config_post_load_script *sc;
+ struct rspamd_config **pcfg;
#ifdef HAVE_CLOCK_GETTIME
#ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID
@@ -688,6 +690,22 @@ rspamd_config_post_load (struct rspamd_config *cfg, gboolean validate_cache)
/* Config other libraries */
rspamd_config_libs (cfg->libs_ctx, cfg);
+ /* Execute post load scripts */
+ LL_FOREACH (cfg->on_load, sc) {
+ lua_rawgeti (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
+ pcfg = lua_newuserdata (cfg->lua_state, sizeof (*pcfg));
+ *pcfg = cfg;
+ rspamd_lua_setclass (cfg->lua_state, "rspamd{config}", -1);
+
+ if (lua_pcall (cfg->lua_state, 1, 0, 0) != 0) {
+ msg_err_config ("error executing post load code: %s",
+ lua_tostring (cfg->lua_state, -1));
+ lua_pop (cfg->lua_state, 1);
+
+ return FALSE;
+ }
+ }
+
/* Validate cache */
if (validate_cache) {
return rspamd_symbols_cache_validate (cfg->cache, cfg, FALSE);
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index 6e96c900c..cbf68dd07 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -393,9 +393,17 @@ LUA_FUNCTION_DEF (config, replace_regexp);
* of script function depends on worker type
* @param {string} worker_type worker type (e.g. "normal")
* @param {function} script script for a worker
+ * @return {boolean} `true` if a script has been registered
*/
LUA_FUNCTION_DEF (config, register_worker_script);
+/***
+ * @method rspamd_config:add_on_load(function(cfg) ... end)
+ * Registers the following script to be executed when configuration is completely loaded
+ * @param {function} script function to be executed
+ */
+LUA_FUNCTION_DEF (config, add_on_load);
+
static const struct luaL_reg configlib_m[] = {
LUA_INTERFACE_DEF (config, get_module_opt),
LUA_INTERFACE_DEF (config, get_mempool),
@@ -424,6 +432,7 @@ static const struct luaL_reg configlib_m[] = {
LUA_INTERFACE_DEF (config, register_regexp),
LUA_INTERFACE_DEF (config, replace_regexp),
LUA_INTERFACE_DEF (config, register_worker_script),
+ LUA_INTERFACE_DEF (config, add_on_load),
{"__tostring", rspamd_lua_class_tostring},
{"__newindex", lua_config_newindex},
{NULL, NULL}
@@ -1675,6 +1684,24 @@ lua_config_register_worker_script (lua_State *L)
return 1;
}
+static gint
+lua_config_add_on_load (lua_State *L)
+{
+ struct rspamd_config *cfg = lua_check_config (L, 1);
+ struct rspamd_config_post_load_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->on_load, sc);
+
+ return 0;
+}
+
void
luaopen_config (lua_State * L)
{