@@ -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 */ |
@@ -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); | |||
} |
@@ -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) | |||
{ |
@@ -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 |
@@ -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); | |||