]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Reworked other parts of rspamadm to handle asynchronous events
authorMikhail Galanin <mgalanin@mimecast.com>
Thu, 6 Sep 2018 08:19:57 +0000 (09:19 +0100)
committerMikhail Galanin <mgalanin@mimecast.com>
Thu, 6 Sep 2018 08:19:57 +0000 (09:19 +0100)
src/rspamadm/commands.c
src/rspamadm/configdump.c
src/rspamadm/confighelp.c
src/rspamadm/control.c
src/rspamadm/fuzzy_convert.c
src/rspamadm/lua_repl.c
src/rspamadm/rspamadm.c
src/rspamadm/rspamadm.h
src/rspamadm/stat_convert.c

index 9216c79b6e9486dc15dd6e7cdf3a98eff9e973a0..81aafcdc911c6b7a49d417eb68c4541c4664dee8 100644 (file)
@@ -17,6 +17,7 @@
 #include "libutil/util.h"
 #include "libutil/logger.h"
 #include "lua/lua_common.h"
+#include "lua/lua_thread_pool.h"
 
 extern struct rspamadm_command pw_command;
 extern struct rspamadm_command configtest_command;
@@ -87,16 +88,25 @@ rspamadm_fill_internal_commands (GPtrArray *dest)
        }
 }
 
+static void
+lua_thread_str_error_cb (struct thread_entry *thread, int ret, const char *msg)
+{
+       const struct rspamadm_command *cmd = thread->cd;
+
+       msg_err ("call to rspamadm lua script %s failed (%d): %s", cmd->name,
+                       ret, msg);
+}
+
 static void
 rspamadm_lua_command_run (gint argc, gchar **argv,
                                                  const struct rspamadm_command *cmd)
 {
-       gint table_idx = GPOINTER_TO_INT (cmd->command_data);
-       gint i, err_idx, ret;
-       GString *tb;
+       struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg);
 
-       lua_pushcfunction (L, &rspamd_lua_traceback);
-       err_idx = lua_gettop (L);
+       lua_State *L = thread->lua_state;
+
+       gint table_idx = GPOINTER_TO_INT (cmd->command_data);
+       gint i;
 
        /* Function */
        lua_rawgeti (L, LUA_REGISTRYINDEX, table_idx);
@@ -111,17 +121,7 @@ rspamadm_lua_command_run (gint argc, gchar **argv,
                lua_rawseti (L, -2, i); /* Starting from zero ! */
        }
 
-       if ((ret = lua_pcall (L, 1, 0, err_idx)) != 0) {
-               tb = lua_touserdata (L, -1);
-               msg_err ("call to rspamadm lua script %s failed (%d): %v", cmd->name,
-                               ret, tb);
-
-               if (tb) {
-                       g_string_free (tb, TRUE);
-               }
-
-               lua_settop (L, 0);
-
+       if (lua_repl_thread_call (thread, 1, (void *)cmd, lua_thread_str_error_cb) != 0) {
                exit (EXIT_FAILURE);
        }
 
@@ -132,13 +132,13 @@ static const gchar *
 rspamadm_lua_command_help (gboolean full_help,
                                                  const struct rspamadm_command *cmd)
 {
+       struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg);
+
+       lua_State *L = thread->lua_state;
+
        gint table_idx = GPOINTER_TO_INT (cmd->command_data);
-       gint err_idx, ret;
-       GString *tb;
 
        if (full_help) {
-               lua_pushcfunction (L, &rspamd_lua_traceback);
-               err_idx = lua_gettop (L);
 
                lua_rawgeti (L, LUA_REGISTRYINDEX, table_idx);
                /* Function */
@@ -153,17 +153,7 @@ rspamadm_lua_command_help (gboolean full_help,
                lua_pushstring (L, "--help");
                lua_rawseti (L, -2, 1);
 
-               if ((ret = lua_pcall (L, 1, 0, err_idx)) != 0) {
-                       tb = lua_touserdata (L, -1);
-                       msg_err ("call to rspamadm lua script %s failed (%d): %v", cmd->name,
-                                       ret, tb);
-
-                       if (tb) {
-                               g_string_free (tb, TRUE);
-                       }
-
-                       lua_settop (L, 0);
-
+               if (lua_repl_thread_call (thread, 1, (void *)cmd, lua_thread_str_error_cb) != 0) {
                        exit (EXIT_FAILURE);
                }
        }
index 8e26ef0af91507b6bb43e320d2bd566773e311f0..962089437a6d12ccee4751e9097e7f6b3445200a 100644 (file)
@@ -302,17 +302,13 @@ rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd
 
        if (ret) {
                if (modules_state) {
-                       lua_State *L = cfg->lua_state;
 
-                       rspamadm_execute_lua_ucl_subr (L,
-                                       argc,
+                       rspamadm_execute_lua_ucl_subr (argc,
                                        argv,
                                        cfg->rcl_obj,
                                        "plugins_stats",
                                        FALSE);
 
-                       lua_close (L);
-
                        exit (EXIT_SUCCESS);
                }
                /* Output configuration */
index 85564cf4cd560b46cdc46051d75d4fdf7b46135f..ff80341ea9ec14a527b67f6bdcadc8d6bbd09ac0 100644 (file)
@@ -106,8 +106,7 @@ rspamadm_confighelp_show (struct rspamd_config *cfg, gint argc, gchar **argv,
                        rspamd_fprintf (stdout, "Showing help for all options:\n");
                }
 
-               rspamadm_execute_lua_ucl_subr (cfg->lua_state,
-                               argc,
+               rspamadm_execute_lua_ucl_subr (argc,
                                argv,
                                obj,
                                "confighelp",
@@ -228,7 +227,7 @@ rspamadm_confighelp (gint argc, gchar **argv, const struct rspamadm_command *cmd
        }
 
        cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA);
-       cfg->lua_state = L;
+       cfg->lua_state = rspamd_main->cfg->lua_state;
        cfg->compiled_modules = modules;
        cfg->compiled_workers = workers;
 
index 6d2849cc7897846244bebf4a6078c988fc4a8a18..50f515a5deded8668b350e5672f1abd13c446bbb 100644 (file)
@@ -45,7 +45,6 @@ struct rspamadm_command control_command = {
 };
 
 struct rspamadm_control_cbdata {
-       lua_State *L;
        const gchar *path;
        gint argc;
        gchar **argv;
@@ -133,8 +132,7 @@ rspamd_control_finish_handler (struct rspamd_http_connection *conn,
                }
                else {
                        if (strcmp (cbdata->path, "/fuzzystat") == 0) {
-                               rspamadm_execute_lua_ucl_subr (cbdata->L,
-                                               cbdata->argc,
+                               rspamadm_execute_lua_ucl_subr (cbdata->argc,
                                                cbdata->argv,
                                                obj,
                                                "fuzzy_stat",
@@ -172,7 +170,6 @@ rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd)
        rspamd_inet_addr_t *addr;
        struct timeval tv;
        static struct rspamadm_control_cbdata cbdata;
-       lua_State *L;
        gint sock;
 
        context = g_option_context_new (
@@ -237,9 +234,6 @@ rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd)
                exit (1);
        }
 
-       L = rspamd_lua_init ();
-       rspamd_lua_set_path (L, NULL, ucl_vars);
-
        conn = rspamd_http_connection_new (NULL,
                        rspamd_control_error_handler,
                        rspamd_control_finish_handler,
@@ -251,7 +245,6 @@ rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd)
        msg->url = rspamd_fstring_new_init (path, strlen (path));
        double_to_tv (timeout, &tv);
 
-       cbdata.L = L;
        cbdata.argc = argc;
        cbdata.argv = argv;
        cbdata.path = path;
index 1c5620730a804b86871732209a2ab25211d1eeca..c59200bc7bdfc102d02eb48f44e18a7e9111df1f 100644 (file)
@@ -78,7 +78,6 @@ rspamadm_fuzzyconvert (gint argc, gchar **argv, const struct rspamadm_command *c
 {
        GOptionContext *context;
        GError *error = NULL;
-       lua_State *L;
        ucl_object_t *obj;
 
        context = g_option_context_new (
@@ -110,9 +109,6 @@ rspamadm_fuzzyconvert (gint argc, gchar **argv, const struct rspamadm_command *c
                exit (1);
        }
 
-       L = rspamd_lua_init ();
-       rspamd_lua_set_path (L, NULL, ucl_vars);
-
        obj = ucl_object_typed_new (UCL_OBJECT);
        ucl_object_insert_key (obj, ucl_object_fromstring (source_db),
                        "source_db", 0, false);
@@ -131,8 +127,7 @@ rspamadm_fuzzyconvert (gint argc, gchar **argv, const struct rspamadm_command *c
                                "redis_db", 0, false);
        }
 
-       rspamadm_execute_lua_ucl_subr (L,
-                       argc,
+       rspamadm_execute_lua_ucl_subr (argc,
                        argv,
                        obj,
                        "fuzzy_convert",
index 39fa0748080129605000a8a740bf4da7fc2bf68e..710b9c5ef7909be8de2fa9a61f7859a1bfa71cfa 100644 (file)
@@ -72,23 +72,13 @@ struct rspamadm_lua_dot_command {
        rspamadm_lua_dot_handler handler;
 };
 
-struct lua_call_data {
-       gint top;
-       gint ret;
-       gpointer ud;
-};
-
 static void rspamadm_lua_help_handler (lua_State *L, gint argc, gchar **argv);
 static void rspamadm_lua_load_handler (lua_State *L, gint argc, gchar **argv);
 static void rspamadm_lua_exec_handler (lua_State *L, gint argc, gchar **argv);
 static void rspamadm_lua_message_handler (lua_State *L, gint argc, gchar **argv);
-static void lua_execute_and_wait (gint narg);
 
 static void lua_thread_error_cb (struct thread_entry *thread, int ret, const char *msg);
 static void lua_thread_finish_cb (struct thread_entry *thread, int ret);
-static gint lua_repl_thread_call (struct thread_entry *thread, gint narg,
-               gpointer ud, lua_thread_error_t error_func);
-
 
 static struct rspamadm_lua_dot_command cmds[] = {
        {
@@ -306,12 +296,12 @@ wait_session_events ()
        }
 }
 
-static gint
+gint
 lua_repl_thread_call (struct thread_entry *thread, gint narg, gpointer ud, lua_thread_error_t error_func)
 {
        int ret;
        struct lua_call_data *cd = g_new0 (struct lua_call_data, 1);
-       cd->top = lua_gettop (L);
+       cd->top = lua_gettop (thread->lua_state);
        cd->ud = ud;
 
        thread->finish_callback = lua_thread_finish_cb;
@@ -736,6 +726,7 @@ rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd)
        GError *error = NULL;
        gchar **elt;
        guint i;
+       lua_State *L = rspamd_main->cfg->lua_state;
 
        context = g_option_context_new ("lua - run lua interpreter");
        g_option_context_set_summary (context,
index 092e4ff58e8d8659ca610aa3dbb4f01fde3a8a68..fad9b2fcddce3ef4fbdb3f06f8e122c223efe5a4 100644 (file)
@@ -18,6 +18,7 @@
 #include "rspamd.h"
 #include "ottery.h"
 #include "lua/lua_common.h"
+#include "lua/lua_thread_pool.h"
 #include "lua_ucl.h"
 #include "unix-std.h"
 
@@ -205,15 +206,27 @@ rspamadm_parse_ucl_var (const gchar *option_name,
        return TRUE;
 }
 
+static void
+lua_thread_str_error_cb (struct thread_entry *thread, int ret, const char *msg)
+{
+       struct lua_call_data *cd = thread->cd;
+
+       msg_err ("call to rspamadm lua script failed (%d): %s", ret, msg);
+
+       cd->ret = ret;
+}
+
 gboolean
-rspamadm_execute_lua_ucl_subr (gpointer pL, gint argc, gchar **argv,
+rspamadm_execute_lua_ucl_subr (gint argc, gchar **argv,
                                                           const ucl_object_t *res,
                                                           const gchar *script_name,
                                                           gboolean rspamadm_subcommand)
 {
-       lua_State *L = pL;
-       gint err_idx, i, ret;
-       GString *tb;
+       struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg);
+
+       lua_State *L = thread->lua_state;
+
+       gint i;
        gchar str[PATH_MAX];
 
        g_assert (script_name != NULL);
@@ -251,32 +264,21 @@ rspamadm_execute_lua_ucl_subr (gpointer pL, gint argc, gchar **argv,
                }
        }
 
-       lua_pushcfunction (L, &rspamd_lua_traceback);
-       err_idx = lua_gettop (L);
-
        /* Push function */
-       lua_pushvalue (L, -2);
+       lua_pushvalue (L, -1);
 
        /* Push argv */
        lua_newtable (L);
 
        for (i = 1; i < argc; i ++) {
                lua_pushstring (L, argv[i]);
-               lua_rawseti (L, -2, i);
+               lua_rawseti (L, -1, i);
        }
 
        /* Push results */
        ucl_object_push_lua (L, res, TRUE);
 
-       if ((ret = lua_pcall (L, 2, 0, err_idx)) != 0) {
-               tb = lua_touserdata (L, -1);
-               msg_err ("call to rspamadm lua script failed (%d): %v", ret, tb);
-
-               if (tb) {
-                       g_string_free (tb, TRUE);
-               }
-
-               lua_settop (L, 0);
+       if (lua_repl_thread_call (thread, 2, NULL, lua_thread_str_error_cb) != 0) {
 
                return FALSE;
        }
@@ -444,6 +446,10 @@ main (gint argc, gchar **argv, gchar **env)
        rspamd_lua_set_globals (cfg, L, ucl_vars);
        rspamadm_add_lua_globals();
 
+#ifdef WITH_HIREDIS
+       rspamd_redis_pool_config (cfg->redis_pool, cfg, rspamd_main->ev_base);
+#endif
+
        /* Init rspamadm global */
        lua_newtable (L);
 
index ff12849425ec8525110161ad06df2c56a829b608..cd01cc86bf03742dcb6b454ab75f9b157d15b0a7 100644 (file)
@@ -23,7 +23,6 @@
 #include <lualib.h>
 
 extern GHashTable *ucl_vars;
-extern lua_State *L;
 extern struct rspamd_main *rspamd_main;
 
 GQuark rspamadm_error (void);
@@ -56,9 +55,21 @@ const struct rspamadm_command *rspamadm_search_command (const gchar *name,
 void rspamadm_fill_internal_commands (GPtrArray *dest);
 void rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest);
 
-gboolean rspamadm_execute_lua_ucl_subr (gpointer L, gint argc, gchar **argv,
+gboolean rspamadm_execute_lua_ucl_subr (gint argc, gchar **argv,
                                                                                const ucl_object_t *res,
                                                                                const gchar *script_name,
                                                                                gboolean rspamadm_subcommand);
 
+struct thread_entry;
+typedef void (*lua_thread_error_t) (struct thread_entry *thread, int ret, const char *msg);
+
+
+struct lua_call_data {
+       gint top;
+       gint ret;
+       gpointer ud;
+};
+gint lua_repl_thread_call (struct thread_entry *thread, gint narg,
+               gpointer ud, lua_thread_error_t error_func);
+
 #endif
index acbe1155078373cb204aad201ba5b23806eebe36..e3a3e179bffd416aa8315475dd3be22e5d6b7b7e 100644 (file)
@@ -113,7 +113,6 @@ rspamadm_statconvert (gint argc, gchar **argv, const struct rspamadm_command *cm
 {
        GOptionContext *context;
        GError *error = NULL;
-       lua_State *L;
        ucl_object_t *obj;
 
        context = g_option_context_new (
@@ -237,8 +236,6 @@ rspamadm_statconvert (gint argc, gchar **argv, const struct rspamadm_command *cm
                }
        }
 
-       L = rspamd_lua_init ();
-       rspamd_lua_set_path (L, obj, ucl_vars);
        ucl_object_insert_key (obj, ucl_object_frombool (reset_previous),
                        "reset_previous", 0, false);
 
@@ -247,8 +244,7 @@ rspamadm_statconvert (gint argc, gchar **argv, const struct rspamadm_command *cm
                                "expire", 0, false);
        }
 
-       rspamadm_execute_lua_ucl_subr (L,
-                       argc,
+       rspamadm_execute_lua_ucl_subr (argc,
                        argv,
                        obj,
                        "stat_convert",