From 71e991f97940818ae4ba3b9acf62e7533de49f3a Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 3 Dec 2017 12:48:04 +0000 Subject: [PATCH] [Minor] Rework initialization order to handle C modules --- src/libserver/cfg_utils.c | 21 +++++++++++++- src/lua/lua_common.c | 59 ++++++++++++++++++--------------------- src/lua/lua_common.h | 12 ++++++++ 3 files changed, 59 insertions(+), 33 deletions(-) diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c index 597d661ae..25ef943b9 100644 --- a/src/libserver/cfg_utils.c +++ b/src/libserver/cfg_utils.c @@ -1522,7 +1522,7 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg, const ucl_object_t *conf, *enabled; GList *cur; struct rspamd_symbols_group *gr; - + lua_State *L = cfg->lua_state; if (g_hash_table_lookup (cfg->c_modules, module_name)) { is_c = TRUE; @@ -1530,6 +1530,10 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg, if (g_hash_table_lookup (cfg->explicit_modules, module_name) != NULL) { /* Always load module */ + rspamd_table_push_global_elt (L, + rspamd_modules_state_global, + "enabled", module_name); + return TRUE; } @@ -1550,6 +1554,9 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg, if (!found) { msg_info_config ("internal module %s is disable in `filters` line", module_name); + rspamd_table_push_global_elt (L, + rspamd_modules_state_global, + "disabled_explicitly", module_name); return FALSE; } @@ -1558,6 +1565,10 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg, conf = ucl_object_lookup (cfg->rcl_obj, module_name); if (conf == NULL) { + rspamd_table_push_global_elt (L, + rspamd_modules_state_global, + "disabled_unconfigured", module_name); + msg_info_config ("%s module %s is enabled but has not been configured", is_c ? "internal" : "lua", module_name); @@ -1571,6 +1582,10 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg, if (enabled && ucl_object_type (enabled) == UCL_BOOLEAN) { if (!ucl_object_toboolean (enabled)) { + rspamd_table_push_global_elt (L, + rspamd_modules_state_global, + "disabled_explicitly", module_name); + msg_info_config ("%s module %s is disabled in the configuration", is_c ? "internal" : "lua", module_name); return FALSE; @@ -1583,9 +1598,13 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg, if (gr) { if (gr->disabled) { + rspamd_table_push_global_elt (L, + rspamd_modules_state_global, + "disabled_explicitly", module_name); msg_info_config ("%s module %s is disabled in the configuration as " "its group has been disabled", is_c ? "internal" : "lua", module_name); + return FALSE; } } diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index c3263bd72..f6fc36aa6 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -49,8 +49,6 @@ const luaL_reg worker_reg[] = { {NULL, NULL} }; -static const char rspamd_modules_state_global[] = "rspamd_plugins_state"; - static GQuark lua_error_quark (void) { @@ -410,6 +408,32 @@ rspamd_lua_init () lua_pcall (L, 1, 0, 0); lua_pop (L, 1); /* math table */ + /* Modules state */ + lua_newtable (L); + /* + * rspamd_plugins_state = { + * enabled = {}, + * disabled_unconfigured = {}, + * disabled_redis = {}, + * disabled_explicitly = {}, + * disabled_failed = {}, + * } + */ +#define ADD_TABLE(name) do { \ + lua_pushstring (L, #name); \ + lua_newtable (L); \ + lua_settable (L, -3); \ +} while (0) + + ADD_TABLE (enabled); + ADD_TABLE (disabled_unconfigured); + ADD_TABLE (disabled_redis); + ADD_TABLE (disabled_explicitly); + ADD_TABLE (disabled_failed); + +#undef ADD_TABLE + lua_setglobal (L, rspamd_modules_state_global); + return L; } @@ -428,7 +452,6 @@ rspamd_init_lua_locked (struct rspamd_config *cfg) return new; } - /** * Free locked state structure */ @@ -444,7 +467,7 @@ rspamd_free_lua_locked (struct lua_locked_state *st) g_free (st); } -static void +void rspamd_table_push_global_elt (lua_State *L, const gchar *global_name, const gchar *field_name, const gchar *new_elt) { @@ -469,31 +492,6 @@ rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load) GString *tb; gint err_idx; - lua_newtable (L); - /* - * rspamd_plugins_state = { - * enabled = {}, - * disabled_unconfigured = {}, - * disabled_redis = {}, - * disabled_explicitly = {}, - * disabled_failed = {}, - * } - */ -#define ADD_TABLE(name) do { \ - lua_pushstring (L, #name); \ - lua_newtable (L); \ - lua_settable (L, -3); \ -} while (0) - - ADD_TABLE (enabled); - ADD_TABLE (disabled_unconfigured); - ADD_TABLE (disabled_redis); - ADD_TABLE (disabled_explicitly); - ADD_TABLE (disabled_failed); - -#undef ADD_TABLE - lua_setglobal (L, rspamd_modules_state_global); - cur = g_list_first (cfg->script_modules); while (cur) { @@ -502,9 +500,6 @@ rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load) if (module->path) { if (!force_load) { if (!rspamd_config_is_module_enabled (cfg, module->name)) { - rspamd_table_push_global_elt (L, - rspamd_modules_state_global, - "disabled_explicitly", module->name); cur = g_list_next (cur); continue; } diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 9dbd8782b..1bc1e0bdf 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -48,6 +48,7 @@ luaL_register (lua_State *L, const gchar *name, const struct luaL_reg *methods) #define LUA_INTERFACE_DEF(class, name) { # name, lua_ ## class ## _ ## name } extern const luaL_reg null_reg[]; +static const char rspamd_modules_state_global[] = "rspamd_plugins_state"; #define RSPAMD_LUA_API_VERSION 12 @@ -159,6 +160,17 @@ gpointer rspamd_lua_check_class (lua_State *L, gint index, const gchar *name); */ lua_State *rspamd_lua_init (void); + +/** + * Sets field in a global variable + * @param L + * @param global_name + * @param field_name + * @param new_elt + */ +void +rspamd_table_push_global_elt (lua_State *L, const gchar *global_name, + const gchar *field_name, const gchar *new_elt); /** * Load and initialize lua plugins */ -- 2.39.5