From: Vsevolod Stakhov Date: Thu, 24 May 2018 18:55:37 +0000 (+0100) Subject: [Project] Rework rspamadm and Lua init path X-Git-Tag: 1.7.6~105 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=25ef6b98215f5518dae638ce0fd6b4710f17b113;p=rspamd.git [Project] Rework rspamadm and Lua init path --- diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h index 520c847f3..777340e16 100644 --- a/src/libserver/cfg_file.h +++ b/src/libserver/cfg_file.h @@ -313,6 +313,7 @@ struct rspamd_config { gboolean enable_experimental; /**< Enable experimental plugins */ gboolean disable_pcre_jit; /**< Disable pcre JIT */ gboolean disable_lua_squeeze; /**< Disable lua rules squeezing */ + gboolean own_lua_state; /**< True if we have created lua_state internally */ gsize max_diff; /**< maximum diff size for text parts */ gsize max_cores_size; /**< maximum size occupied by rspamd core files */ @@ -445,11 +446,16 @@ struct rspamd_config { gboolean rspamd_parse_bind_line (struct rspamd_config *cfg, struct rspamd_worker_conf *cf, const gchar *str); + +enum rspamd_config_init_flags { + RSPAMD_CONFIG_INIT_DEFAULT = 0, + RSPAMD_CONFIG_INIT_SKIP_LUA = (1 << 0) +}; /** * Init default values * @param cfg config file */ -struct rspamd_config *rspamd_config_new (void); +struct rspamd_config *rspamd_config_new (enum rspamd_config_init_flags flags); /** * Free memory used by config structure diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c index bf63b2188..e606139cf 100644 --- a/src/libserver/cfg_utils.c +++ b/src/libserver/cfg_utils.c @@ -114,7 +114,7 @@ rspamd_parse_bind_line (struct rspamd_config *cfg, } struct rspamd_config * -rspamd_config_new (void) +rspamd_config_new (enum rspamd_config_init_flags flags) { struct rspamd_config *cfg; @@ -172,7 +172,11 @@ rspamd_config_new (void) cfg->min_word_len = DEFAULT_MIN_WORD; cfg->max_word_len = DEFAULT_MAX_WORD; - cfg->lua_state = rspamd_lua_init (); + if (!(flags & RSPAMD_CONFIG_INIT_SKIP_LUA)) { + cfg->lua_state = rspamd_lua_init (); + cfg->own_lua_state = TRUE; + } + cfg->cache = rspamd_symbols_cache_new (cfg); cfg->ups_ctx = rspamd_upstreams_library_init (); cfg->re_cache = rspamd_re_cache_new (); @@ -251,7 +255,10 @@ rspamd_config_free (struct rspamd_config *cfg) rspamd_re_cache_unref (cfg->re_cache); rspamd_upstreams_library_unref (cfg->ups_ctx); rspamd_mempool_delete (cfg->cfg_pool); - lua_close (cfg->lua_state); + + if (cfg->lua_state && cfg->own_lua_state) { + lua_close (cfg->lua_state); + } REF_RELEASE (cfg->libs_ctx); DL_FOREACH_SAFE (cfg->log_pipes, lp, ltmp) { diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index fe715f393..f33bc3daa 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -587,9 +587,6 @@ rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load) cur = g_list_next (cur); } - /* Assign state */ - cfg->lua_state = L; - return TRUE; } diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 6c200d1be..47b7106b6 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -626,7 +626,8 @@ lua_util_load_rspamd_config (lua_State *L) cfg_name = luaL_checkstring (L, 1); if (cfg_name) { - cfg = rspamd_config_new (); + cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); + cfg->lua_state = L; if (rspamd_config_read (cfg, cfg_name, NULL, NULL, NULL, NULL)) { msg_err_config ("cannot load config from %s", cfg_name); @@ -655,7 +656,8 @@ lua_util_config_from_ucl (lua_State *L) if (obj) { cfg = g_malloc0 (sizeof (struct rspamd_config)); - cfg = rspamd_config_new (); + cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); + cfg->lua_state = L; cfg->rcl_obj = obj; cfg->cache = rspamd_symbols_cache_new (cfg); diff --git a/src/rspamadm/commands.c b/src/rspamadm/commands.c index 410306fe3..87a3d961e 100644 --- a/src/rspamadm/commands.c +++ b/src/rspamadm/commands.c @@ -14,6 +14,7 @@ * limitations under the License. */ #include "rspamadm.h" +#include "libutil/util.h" extern struct rspamadm_command pw_command; extern struct rspamadm_command keypair_command; @@ -55,23 +56,33 @@ const struct rspamadm_command *commands[] = { const struct rspamadm_command * -rspamadm_search_command (const gchar *name) +rspamadm_search_command (const gchar *name, GPtrArray *all_commands) { - const struct rspamadm_command *ret = NULL; + const struct rspamadm_command *ret = NULL, *cmd; guint i; if (name == NULL) { name = "help"; } - for (i = 0; i < G_N_ELEMENTS (commands); i ++) { - if (commands[i] != NULL) { - if (strcmp (name, commands[i]->name) == 0) { - ret = commands[i]; + PTR_ARRAY_FOREACH (all_commands, i, cmd) { + if (strcmp (name, cmd->name) == 0) { + ret = cmd; break; } - } } return ret; } + +void +rspamadm_fill_internal_commands (GPtrArray *dest) +{ + guint i; + + for (i = 0; i < G_N_ELEMENTS (commands); i ++) { + if (commands[i]) { + g_ptr_array_add (dest, (gpointer)commands[i]); + } + } +} diff --git a/src/rspamadm/configdump.c b/src/rspamadm/configdump.c index d5380bc9d..486a74cef 100644 --- a/src/rspamadm/configdump.c +++ b/src/rspamadm/configdump.c @@ -33,8 +33,8 @@ extern struct rspamd_main *rspamd_main; extern module_t *modules[]; extern worker_t *workers[]; -static void rspamadm_configdump (gint argc, gchar **argv); -static const char *rspamadm_configdump_help (gboolean full_help); +static void rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *); +static const char *rspamadm_configdump_help (gboolean full_help, const struct rspamadm_command *); struct rspamadm_command configdump_command = { .name = "configdump", @@ -61,7 +61,7 @@ static GOptionEntry entries[] = { }; static const char * -rspamadm_configdump_help (gboolean full_help) +rspamadm_configdump_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -237,7 +237,7 @@ rspamadm_dump_section_obj (struct rspamd_config *cfg, } static void -rspamadm_configdump (gint argc, gchar **argv) +rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/confighelp.c b/src/rspamadm/confighelp.c index 8f208b805..7ce72dea9 100644 --- a/src/rspamadm/confighelp.c +++ b/src/rspamadm/confighelp.c @@ -30,9 +30,11 @@ extern struct rspamd_main *rspamd_main; extern module_t *modules[]; extern worker_t *workers[]; -static void rspamadm_confighelp (gint argc, gchar **argv); +static void rspamadm_confighelp (gint argc, gchar **argv, + const struct rspamadm_command *cmd); -static const char *rspamadm_confighelp_help (gboolean full_help); +static const char *rspamadm_confighelp_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command confighelp_command = { .name = "confighelp", @@ -55,7 +57,7 @@ static GOptionEntry entries[] = { }; static const char * -rspamadm_confighelp_help (gboolean full_help) +rspamadm_confighelp_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -189,7 +191,7 @@ rspamadm_confighelp_search_word (const ucl_object_t *obj, const gchar *str) } static void -rspamadm_confighelp (gint argc, gchar **argv) +rspamadm_confighelp (gint argc, gchar **argv, const struct rspamadm_command *cmd) { struct rspamd_config *cfg; ucl_object_t *doc_obj; @@ -224,7 +226,8 @@ rspamadm_confighelp (gint argc, gchar **argv) pworker++; } - cfg = rspamd_config_new (); + cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); + cfg->lua_state = L; cfg->compiled_modules = modules; cfg->compiled_workers = workers; diff --git a/src/rspamadm/configtest.c b/src/rspamadm/configtest.c index 18104c109..94914e817 100644 --- a/src/rspamadm/configtest.c +++ b/src/rspamadm/configtest.c @@ -28,8 +28,10 @@ extern struct rspamd_main *rspamd_main; extern module_t *modules[]; extern worker_t *workers[]; -static void rspamadm_configtest (gint argc, gchar **argv); -static const char *rspamadm_configtest_help (gboolean full_help); +static void rspamadm_configtest (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_configtest_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command configtest_command = { .name = "configtest", @@ -50,7 +52,7 @@ static GOptionEntry entries[] = { }; static const char * -rspamadm_configtest_help (gboolean full_help) +rspamadm_configtest_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -94,7 +96,7 @@ config_logger (rspamd_mempool_t *pool, gpointer ud) } static void -rspamadm_configtest (gint argc, gchar **argv) +rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/configwizard.c b/src/rspamadm/configwizard.c index 12c7b8cc1..5aaa94914 100644 --- a/src/rspamadm/configwizard.c +++ b/src/rspamadm/configwizard.c @@ -28,8 +28,10 @@ extern struct rspamd_main *rspamd_main; extern module_t *modules[]; extern worker_t *workers[]; -static void rspamadm_configwizard (gint argc, gchar **argv); -static const char *rspamadm_configwizard_help (gboolean full_help); +static void rspamadm_configwizard (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_configwizard_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command configwizard_command = { .name = "configwizard", @@ -46,7 +48,7 @@ static GOptionEntry entries[] = { }; static const char * -rspamadm_configwizard_help (gboolean full_help) +rspamadm_configwizard_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -83,7 +85,8 @@ config_logger (rspamd_mempool_t *pool, gpointer ud) } static void -rspamadm_configwizard (gint argc, gchar **argv) +rspamadm_configwizard (gint argc, gchar **argv, + const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/control.c b/src/rspamadm/control.c index 35cecbc69..35b5415e7 100644 --- a/src/rspamadm/control.c +++ b/src/rspamadm/control.c @@ -31,8 +31,10 @@ static gboolean ucl = TRUE; static gboolean compact = FALSE; static gdouble timeout = 1.0; -static void rspamadm_control (gint argc, gchar **argv); -static const char *rspamadm_control_help (gboolean full_help); +static void rspamadm_control (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_control_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command control_command = { .name = "control", @@ -64,7 +66,7 @@ static GOptionEntry entries[] = { }; static const char * -rspamadm_control_help (gboolean full_help) +rspamadm_control_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -158,7 +160,7 @@ rspamd_control_finish_handler (struct rspamd_http_connection *conn, } static void -rspamadm_control (gint argc, gchar **argv) +rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/corpus_test.c b/src/rspamadm/corpus_test.c index d72788d21..305c3a8ad 100644 --- a/src/rspamadm/corpus_test.c +++ b/src/rspamadm/corpus_test.c @@ -24,8 +24,10 @@ static gchar *output_location = "results.log"; static gint connections = 10; static gdouble timeout = 60.0; -static void rspamadm_corpus_test (gint argc, gchar **argv); -static const char *rspamadm_corpus_test_help (gboolean full_help); +static void rspamadm_corpus_test (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_corpus_test_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command corpus_test_command = { .name = "corpus_test", @@ -49,7 +51,7 @@ static GOptionEntry entries[] = { }; static const char * -rspamadm_corpus_test_help (gboolean full_help) +rspamadm_corpus_test_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -74,7 +76,7 @@ rspamadm_corpus_test_help (gboolean full_help) } static void -rspamadm_corpus_test (gint argc, gchar **argv) +rspamadm_corpus_test (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/dkim_keygen.c b/src/rspamadm/dkim_keygen.c index e32f7bf05..86b228d01 100644 --- a/src/rspamadm/dkim_keygen.c +++ b/src/rspamadm/dkim_keygen.c @@ -27,8 +27,10 @@ static gchar *selector = NULL; static gchar *domain = NULL; static guint bits = 1024; -static void rspamadm_dkim_keygen (gint argc, gchar **argv); -static const char *rspamadm_dkim_keygen_help (gboolean full_help); +static void rspamadm_dkim_keygen (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_dkim_keygen_help (gboolean full_help, + const struct rspamadm_command *cmd); static void rspamadm_dkim_keygen_lua_subrs (gpointer pL); struct rspamadm_command dkim_keygen_command = { @@ -52,7 +54,7 @@ static GOptionEntry entries[] = { }; static const char * -rspamadm_dkim_keygen_help (gboolean full_help) +rspamadm_dkim_keygen_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -221,7 +223,7 @@ rspamadm_dkim_keygen_lua_subrs (gpointer pL) } static void -rspamadm_dkim_keygen (gint argc, gchar **argv) +rspamadm_dkim_keygen (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/fuzzy_convert.c b/src/rspamadm/fuzzy_convert.c index 40757d2e4..ebf60bd24 100644 --- a/src/rspamadm/fuzzy_convert.c +++ b/src/rspamadm/fuzzy_convert.c @@ -24,8 +24,10 @@ static gchar *redis_db = NULL; static gchar *redis_password = NULL; static int64_t fuzzy_expiry = 0; -static void rspamadm_fuzzyconvert (gint argc, gchar **argv); -static const char *rspamadm_fuzzyconvert_help (gboolean full_help); +static void rspamadm_fuzzyconvert (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_fuzzyconvert_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command fuzzyconvert_command = { .name = "fuzzyconvert", @@ -51,7 +53,7 @@ static GOptionEntry entries[] = { static const char * -rspamadm_fuzzyconvert_help (gboolean full_help) +rspamadm_fuzzyconvert_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -72,7 +74,7 @@ rspamadm_fuzzyconvert_help (gboolean full_help) } static void -rspamadm_fuzzyconvert (gint argc, gchar **argv) +rspamadm_fuzzyconvert (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/fuzzy_merge.c b/src/rspamadm/fuzzy_merge.c index 94631137c..f5e6847fa 100644 --- a/src/rspamadm/fuzzy_merge.c +++ b/src/rspamadm/fuzzy_merge.c @@ -22,8 +22,10 @@ static gchar *target = NULL; static gchar **sources = NULL; static gboolean quiet; -static void rspamadm_fuzzy_merge (gint argc, gchar **argv); -static const char *rspamadm_fuzzy_merge_help (gboolean full_help); +static void rspamadm_fuzzy_merge (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_fuzzy_merge_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command fuzzy_merge_command = { .name = "fuzzy_merge", @@ -157,7 +159,7 @@ static struct rspamd_sqlite3_prstmt prepared_stmts[STMAX] = { }; static const char * -rspamadm_fuzzy_merge_help (gboolean full_help) +rspamadm_fuzzy_merge_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -218,7 +220,7 @@ rspamadm_op_equal (gconstpointer a, gconstpointer b) } static void -rspamadm_fuzzy_merge (gint argc, gchar **argv) +rspamadm_fuzzy_merge (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/grep.c b/src/rspamadm/grep.c index 5474ecf46..60189f39c 100644 --- a/src/rspamadm/grep.c +++ b/src/rspamadm/grep.c @@ -26,8 +26,10 @@ static gboolean orphans = FALSE; static gboolean partial = FALSE; static gboolean luapat = FALSE; -static void rspamadm_grep (gint argc, gchar **argv); -static const char *rspamadm_grep_help (gboolean full_help); +static void rspamadm_grep (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_grep_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command grep_command = { .name = "grep", @@ -57,7 +59,7 @@ static GOptionEntry entries[] = { static const char * -rspamadm_grep_help (gboolean full_help) +rspamadm_grep_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -81,7 +83,7 @@ rspamadm_grep_help (gboolean full_help) } static void -rspamadm_grep (gint argc, gchar **argv) +rspamadm_grep (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/keypair.c b/src/rspamadm/keypair.c index 4214e0052..a1d9e5d6b 100644 --- a/src/rspamadm/keypair.c +++ b/src/rspamadm/keypair.c @@ -25,8 +25,10 @@ static gboolean openssl = FALSE; static gboolean ucl = FALSE; static gboolean sign = FALSE; -static void rspamadm_keypair (gint argc, gchar **argv); -static const char *rspamadm_keypair_help (gboolean full_help); +static void rspamadm_keypair (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_keypair_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command keypair_command = { .name = "keypair", @@ -51,7 +53,7 @@ static GOptionEntry entries[] = { }; static const char * -rspamadm_keypair_help (gboolean full_help) +rspamadm_keypair_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -74,7 +76,7 @@ rspamadm_keypair_help (gboolean full_help) } static void -rspamadm_keypair (gint argc, gchar **argv) +rspamadm_keypair (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/lua_repl.c b/src/rspamadm/lua_repl.c index e0ee7d9f1..4ec934b91 100644 --- a/src/rspamadm/lua_repl.c +++ b/src/rspamadm/lua_repl.c @@ -47,8 +47,10 @@ static const char *default_history_file = ".rspamd_repl.hist"; #endif #define MULTILINE_PROMPT "... " -static void rspamadm_lua (gint argc, gchar **argv); -static const char *rspamadm_lua_help (gboolean full_help); +static void rspamadm_lua (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_lua_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command lua_command = { .name = "lua", @@ -121,7 +123,7 @@ static GOptionEntry entries[] = { }; static const char * -rspamadm_lua_help (gboolean full_help) +rspamadm_lua_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -658,7 +660,7 @@ rspamadm_lua_handle_exec (struct rspamd_http_connection_entry *conn_ent, } static void -rspamadm_lua (gint argc, gchar **argv) +rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; @@ -689,9 +691,6 @@ rspamadm_lua (gint argc, gchar **argv) } } - L = rspamd_lua_init (); - rspamd_lua_set_path (L, NULL, ucl_vars); - if (paths) { for (elt = paths; *elt != NULL; elt ++) { rspamadm_lua_add_path (L, *elt); diff --git a/src/rspamadm/pw.c b/src/rspamadm/pw.c index 3299c479d..b00aac223 100644 --- a/src/rspamadm/pw.c +++ b/src/rspamadm/pw.c @@ -21,8 +21,10 @@ #include "rspamadm.h" #include "unix-std.h" -static void rspamadm_pw (gint argc, gchar **argv); -static const char *rspamadm_pw_help (gboolean full_help); +static void rspamadm_pw (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_pw_help (gboolean full_help, + const struct rspamadm_command *cmd); static void rspamadm_pw_lua_subrs (gpointer pL); static gboolean do_encrypt = FALSE; @@ -57,7 +59,7 @@ static GOptionEntry entries[] = { }; static const char * -rspamadm_pw_help (gboolean full_help) +rspamadm_pw_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -354,7 +356,7 @@ rspamadm_alg_list (void) } static void -rspamadm_pw (gint argc, gchar **argv) +rspamadm_pw (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/rescore.c b/src/rspamadm/rescore.c index 926740023..e30c6613a 100644 --- a/src/rspamadm/rescore.c +++ b/src/rspamadm/rescore.c @@ -18,12 +18,6 @@ #include "rspamadm.h" #include "lua/lua_common.h" -#if !defined(WITH_TORCH) || !defined(WITH_LUAJIT) -#define HAS_TORCH false -#else -#define HAS_TORCH true -#endif - static gchar *logdir = NULL; static gchar *output = "new.scores"; static gboolean score_diff = false; /* Print score diff flag */ @@ -33,9 +27,11 @@ extern struct rspamd_main *rspamd_main; extern module_t *modules[]; extern worker_t *workers[]; -static void rspamadm_rescore (gint argc, gchar **argv); +static void rspamadm_rescore (gint argc, gchar **argv, + const struct rspamadm_command *cmd); -static const char *rspamadm_rescore_help (gboolean full_help); +static const char *rspamadm_rescore_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command rescore_command = { .name = "rescore", @@ -75,8 +71,8 @@ config_logger (rspamd_mempool_t *pool, gpointer ud) } static const char * -rspamadm_rescore_help (gboolean full_help) { - +rspamadm_rescore_help (gboolean full_help, const struct rspamadm_command *cmd) +{ const char *help_str; if (full_help) { @@ -95,16 +91,21 @@ rspamadm_rescore_help (gboolean full_help) { } static void -rspamadm_rescore (gint argc, gchar **argv) { - +rspamadm_rescore (gint argc, gchar **argv, const struct rspamadm_command *cmd) +{ GOptionContext *context; GError *error = NULL; - lua_State *L; struct rspamd_config *cfg = rspamd_main->cfg, **pcfg; gboolean ret = TRUE; worker_t **pworker; const gchar *confdir; +#ifndef WITH_TORCH + rspamd_fprintf (stderr, "Torch is not enabled. " + "Use -DENABLE_TORCH=ON option while running cmake.\n"); + exit (EXIT_FAILURE); +#endif + context = g_option_context_new ( "rescore - estimate optimal symbol weights from log files"); @@ -123,12 +124,6 @@ rspamadm_rescore (gint argc, gchar **argv) { exit (EXIT_FAILURE); } - if (!HAS_TORCH) { - rspamd_fprintf (stderr, "Torch is not enabled. " - "Use -DENABLE_TORCH=ON option while running cmake.\n"); - exit (EXIT_FAILURE); - } - if (logdir == NULL) { rspamd_fprintf (stderr, "Please specify log directory.\n"); exit (EXIT_FAILURE); @@ -176,8 +171,6 @@ rspamadm_rescore (gint argc, gchar **argv) { } if (ret) { - L = cfg->lua_state; - rspamd_lua_set_path (L, cfg->rcl_obj, ucl_vars); ucl_object_insert_key (cfg->rcl_obj, ucl_object_fromstring (cfg->cfg_name), "config_path", 0, false); ucl_object_insert_key (cfg->rcl_obj, ucl_object_fromstring (logdir), diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c index b39c75897..0ec4ba403 100644 --- a/src/rspamadm/rspamadm.c +++ b/src/rspamadm/rspamadm.c @@ -31,9 +31,10 @@ static gboolean show_help = FALSE; static gboolean show_version = FALSE; GHashTable *ucl_vars = NULL; struct rspamd_main *rspamd_main = NULL; +lua_State *L = NULL; -static void rspamadm_help (gint argc, gchar **argv); -static const char* rspamadm_help_help (gboolean full_help); +static void rspamadm_help (gint argc, gchar **argv, const struct rspamadm_command *); +static const char* rspamadm_help_help (gboolean full_help, const struct rspamadm_command *); struct rspamadm_command help_command = { .name = "help", @@ -83,26 +84,26 @@ rspamadm_usage (GOptionContext *context) } static void -rspamadm_commands (void) +rspamadm_commands (GPtrArray *all_commands) { - const struct rspamadm_command **cmd; + const struct rspamadm_command *cmd; + guint i; printf ("Rspamadm %s\n", RVERSION); printf ("Usage: rspamadm [global_options] command [command_options]\n"); printf ("\nAvailable commands:\n"); - cmd = commands; - - while (*cmd) { - if (!((*cmd)->flags & RSPAMADM_FLAG_NOHELP)) { - printf (" %-18s %-60s\n", (*cmd)->name, (*cmd)->help (FALSE)); + PTR_ARRAY_FOREACH (all_commands, i, cmd) { + if (!(cmd->flags & RSPAMADM_FLAG_NOHELP)) { + printf (" %-18s %-60s\n", cmd->name, cmd->help (FALSE, cmd)); } + cmd ++; } } static const char * -rspamadm_help_help (gboolean full_help) +rspamadm_help_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -118,10 +119,11 @@ rspamadm_help_help (gboolean full_help) } static void -rspamadm_help (gint argc, gchar **argv) +rspamadm_help (gint argc, gchar **argv, const struct rspamadm_command *command) { const gchar *cmd_name; - const struct rspamadm_command *cmd, **cmd_list; + const struct rspamadm_command *cmd; + GPtrArray *all_commands = (GPtrArray *)command->command_data; printf ("Rspamadm %s\n", RVERSION); printf ("Usage: rspamadm [global_options] command [command_options]\n\n"); @@ -134,7 +136,7 @@ rspamadm_help (gint argc, gchar **argv) printf ("Showing help for %s command\n\n", cmd_name); } - cmd = rspamadm_search_command (cmd_name); + cmd = rspamadm_search_command (cmd_name, all_commands); if (cmd == NULL) { fprintf (stderr, "Invalid command name: %s\n", cmd_name); @@ -142,20 +144,18 @@ rspamadm_help (gint argc, gchar **argv) } if (strcmp (cmd_name, "help") == 0) { + guint i; printf ("Available commands:\n"); - cmd_list = commands; - - while (*cmd_list) { - if (!((*cmd_list)->flags & RSPAMADM_FLAG_NOHELP)) { - printf (" %-18s %-60s\n", (*cmd_list)->name, - (*cmd_list)->help (FALSE)); + PTR_ARRAY_FOREACH (all_commands, i, cmd) { + if (!(cmd->flags & RSPAMADM_FLAG_NOHELP)) { + printf (" %-18s %-60s\n", cmd->name, + cmd->help (FALSE, cmd)); } - cmd_list++; } } else { - printf ("%s\n", cmd->help (TRUE)); + printf ("%s\n", cmd->help (TRUE, cmd)); } } @@ -222,6 +222,11 @@ rspamadm_execute_lua_ucl_subr (gpointer pL, gint argc, gchar **argv, return FALSE; } else { + if (lua_type (L, -1) == LUA_TTABLE) { + lua_pushstring (L, "handler"); + lua_gettable (L, -2); + } + if (lua_type (L, -1) != LUA_TFUNCTION) { msg_err ("lua script must return " "function and not %s", @@ -278,12 +283,13 @@ main (gint argc, gchar **argv, gchar **env) gchar **nargv, **targv; const gchar *cmd_name; const struct rspamadm_command *cmd; + GPtrArray *all_commands = g_ptr_array_new (); /* Discovered during check */ gint i, nargc, targc; ucl_vars = g_hash_table_new_full (rspamd_strcase_hash, rspamd_strcase_equal, g_free, g_free); process_quark = g_quark_from_static_string ("rspamadm"); - cfg = rspamd_config_new (); + cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT); cfg->libs_ctx = rspamd_init_libs (); rspamd_main = g_malloc0 (sizeof (*rspamd_main)); rspamd_main->cfg = cfg; @@ -291,6 +297,8 @@ main (gint argc, gchar **argv, gchar **env) rspamd_main->type = process_quark; rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "rspamadm"); + rspamadm_fill_internal_commands (all_commands); + help_command.command_data = all_commands; /* Setup logger */ if (verbose) { @@ -349,6 +357,9 @@ main (gint argc, gchar **argv, gchar **env) exit (1); } + L = cfg->lua_state; + rspamd_lua_set_path (L, NULL, ucl_vars); + g_strfreev (nargv); if (show_version) { @@ -360,7 +371,7 @@ main (gint argc, gchar **argv, gchar **env) exit (EXIT_SUCCESS); } if (list_commands) { - rspamadm_commands (); + rspamadm_commands (all_commands); exit (EXIT_SUCCESS); } @@ -370,7 +381,7 @@ main (gint argc, gchar **argv, gchar **env) cmd_name = "help"; } - cmd = rspamadm_search_command (cmd_name); + cmd = rspamadm_search_command (cmd_name, all_commands); if (cmd == NULL) { fprintf (stderr, "Invalid command name: %s\n", cmd_name); @@ -387,16 +398,18 @@ main (gint argc, gchar **argv, gchar **env) targc = argc - nargc; targv = nargv; - cmd->run (targc, targv); + cmd->run (targc, targv, cmd); g_strfreev (nargv); } else { - cmd->run (0, NULL); + cmd->run (0, NULL, cmd); } rspamd_log_close (rspamd_main->logger); REF_RELEASE (rspamd_main->cfg); + lua_close (L); g_free (rspamd_main); + g_ptr_array_free (all_commands, TRUE); return 0; } diff --git a/src/rspamadm/rspamadm.h b/src/rspamadm/rspamadm.h index 9a209fd91..3f619e52c 100644 --- a/src/rspamadm/rspamadm.h +++ b/src/rspamadm/rspamadm.h @@ -18,16 +18,24 @@ #include "config.h" #include "ucl.h" +#include +#include +#include extern GHashTable *ucl_vars; +extern lua_State *L; GQuark rspamadm_error (void); -typedef const gchar* (*rspamadm_help_func) (gboolean full_help); -typedef void (*rspamadm_run_func) (gint argc, gchar **argv); +struct rspamadm_command; +typedef const gchar* (*rspamadm_help_func) (gboolean full_help, + const struct rspamadm_command *cmd); +typedef void (*rspamadm_run_func) (gint argc, gchar **argv, + const struct rspamadm_command *cmd); typedef void (*rspamadm_lua_exports_func) (gpointer lua_state); #define RSPAMADM_FLAG_NOHELP (1 << 0) +#define RSPAMADM_FLAG_LUA (1 << 0) struct rspamadm_command { const gchar *name; @@ -35,14 +43,19 @@ struct rspamadm_command { rspamadm_help_func help; rspamadm_run_func run; rspamadm_lua_exports_func lua_subrs; + gint cbref; + gpointer command_data; /* Opaque data */ }; extern const struct rspamadm_command *commands[]; extern struct rspamadm_command help_command; -const struct rspamadm_command *rspamadm_search_command (const gchar *name); +const struct rspamadm_command *rspamadm_search_command (const gchar *name, + GPtrArray *all_commands); +void rspamadm_fill_internal_commands (GPtrArray *dest); gboolean rspamadm_execute_lua_ucl_subr (gpointer L, gint argc, gchar **argv, - const ucl_object_t *res, const gchar *script_name); + const ucl_object_t *res, + const gchar *script_name); #endif diff --git a/src/rspamadm/signtool.c b/src/rspamadm/signtool.c index 0ae7e22c5..af8d05c96 100644 --- a/src/rspamadm/signtool.c +++ b/src/rspamadm/signtool.c @@ -39,8 +39,10 @@ static gchar *editor = NULL; static gboolean edit = FALSE; enum rspamd_cryptobox_mode mode = RSPAMD_CRYPTOBOX_MODE_25519; -static void rspamadm_signtool (gint argc, gchar **argv); -static const char *rspamadm_signtool_help (gboolean full_help); +static void rspamadm_signtool (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_signtool_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command signtool_command = { .name = "signtool", @@ -75,7 +77,8 @@ static GOptionEntry entries[] = { }; static const char * -rspamadm_signtool_help (gboolean full_help) +rspamadm_signtool_help (gboolean full_help, + const struct rspamadm_command *cmd) { const char *help_str; @@ -462,7 +465,7 @@ rspamadm_verify_file (const gchar *fname, const guchar *pk) static void -rspamadm_signtool (gint argc, gchar **argv) +rspamadm_signtool (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamadm/stat_convert.c b/src/rspamadm/stat_convert.c index e4cf24629..68723c40f 100644 --- a/src/rspamadm/stat_convert.c +++ b/src/rspamadm/stat_convert.c @@ -37,8 +37,10 @@ static gchar *redis_db = NULL; static gchar *redis_password = NULL; static gboolean reset_previous = FALSE; -static void rspamadm_statconvert (gint argc, gchar **argv); -static const char *rspamadm_statconvert_help (gboolean full_help); +static void rspamadm_statconvert (gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_statconvert_help (gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command statconvert_command = { .name = "statconvert", @@ -77,7 +79,7 @@ static GOptionEntry entries[] = { static const char * -rspamadm_statconvert_help (gboolean full_help) +rspamadm_statconvert_help (gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; @@ -107,7 +109,7 @@ rspamadm_statconvert_help (gboolean full_help) } static void -rspamadm_statconvert (gint argc, gchar **argv) +rspamadm_statconvert (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; diff --git a/src/rspamd.c b/src/rspamd.c index cc3be3357..d6490d6aa 100644 --- a/src/rspamd.c +++ b/src/rspamd.c @@ -276,7 +276,7 @@ reread_config (struct rspamd_main *rspamd_main) gchar *cfg_file; rspamd_symbols_cache_save (rspamd_main->cfg->cache); - tmp_cfg = rspamd_config_new (); + tmp_cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT); g_hash_table_unref (tmp_cfg->c_modules); tmp_cfg->c_modules = g_hash_table_ref (rspamd_main->cfg->c_modules); tmp_cfg->libs_ctx = rspamd_main->cfg->libs_ctx; @@ -1180,7 +1180,7 @@ main (gint argc, gchar **argv, gchar **env) "main"); rspamd_main->stat = rspamd_mempool_alloc0_shared (rspamd_main->server_pool, sizeof (struct rspamd_stat)); - rspamd_main->cfg = rspamd_config_new (); + rspamd_main->cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT); rspamd_main->spairs = g_hash_table_new_full (rspamd_spair_hash, rspamd_spair_equal, g_free, rspamd_spair_close); rspamd_main->start_mtx = rspamd_mempool_get_mutex (rspamd_main->server_pool); diff --git a/test/rspamd_test_suite.c b/test/rspamd_test_suite.c index d6fa237a3..2f8e893de 100644 --- a/test/rspamd_test_suite.c +++ b/test/rspamd_test_suite.c @@ -15,7 +15,7 @@ main (int argc, char **argv) rspamd_main = (struct rspamd_main *)g_malloc (sizeof (struct rspamd_main)); memset (rspamd_main, 0, sizeof (struct rspamd_main)); rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL); - cfg = rspamd_config_new (); + cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT); rspamd_main->cfg = cfg; cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL); cfg->log_type = RSPAMD_LOG_CONSOLE; diff --git a/test/rspamd_upstream_test.c b/test/rspamd_upstream_test.c index 47094398f..cce0008a9 100644 --- a/test/rspamd_upstream_test.c +++ b/test/rspamd_upstream_test.c @@ -64,7 +64,7 @@ rspamd_upstream_test_func (void) struct timeval tv; rspamd_inet_addr_t *addr, *next_addr, *paddr; - cfg = rspamd_config_new (); + cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); cfg->dns_retransmits = 2; cfg->dns_timeout = 0.5; cfg->upstream_max_errors = 1;