aboutsummaryrefslogtreecommitdiffstats
path: root/src/rspamadm
diff options
context:
space:
mode:
authorMikhail Galanin <mgalanin@mimecast.com>2018-09-06 09:19:57 +0100
committerMikhail Galanin <mgalanin@mimecast.com>2018-09-06 09:19:57 +0100
commit4c4c9f45c48992c5969923fa3bd7383f794fdc8a (patch)
treefe70699bf21760a53d226a05f20c37a5c8d665e2 /src/rspamadm
parent49fb6ed4d68e7691b5219c78fd0837ca4e2f7b94 (diff)
downloadrspamd-4c4c9f45c48992c5969923fa3bd7383f794fdc8a.tar.gz
rspamd-4c4c9f45c48992c5969923fa3bd7383f794fdc8a.zip
[Minor] Reworked other parts of rspamadm to handle asynchronous events
Diffstat (limited to 'src/rspamadm')
-rw-r--r--src/rspamadm/commands.c52
-rw-r--r--src/rspamadm/configdump.c6
-rw-r--r--src/rspamadm/confighelp.c5
-rw-r--r--src/rspamadm/control.c9
-rw-r--r--src/rspamadm/fuzzy_convert.c7
-rw-r--r--src/rspamadm/lua_repl.c15
-rw-r--r--src/rspamadm/rspamadm.c42
-rw-r--r--src/rspamadm/rspamadm.h15
-rw-r--r--src/rspamadm/stat_convert.c6
9 files changed, 67 insertions, 90 deletions
diff --git a/src/rspamadm/commands.c b/src/rspamadm/commands.c
index 9216c79b6..81aafcdc9 100644
--- a/src/rspamadm/commands.c
+++ b/src/rspamadm/commands.c
@@ -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;
@@ -88,15 +89,24 @@ 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);
}
}
diff --git a/src/rspamadm/configdump.c b/src/rspamadm/configdump.c
index 8e26ef0af..962089437 100644
--- a/src/rspamadm/configdump.c
+++ b/src/rspamadm/configdump.c
@@ -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 */
diff --git a/src/rspamadm/confighelp.c b/src/rspamadm/confighelp.c
index 85564cf4c..ff80341ea 100644
--- a/src/rspamadm/confighelp.c
+++ b/src/rspamadm/confighelp.c
@@ -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;
diff --git a/src/rspamadm/control.c b/src/rspamadm/control.c
index 6d2849cc7..50f515a5d 100644
--- a/src/rspamadm/control.c
+++ b/src/rspamadm/control.c
@@ -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;
diff --git a/src/rspamadm/fuzzy_convert.c b/src/rspamadm/fuzzy_convert.c
index 1c5620730..c59200bc7 100644
--- a/src/rspamadm/fuzzy_convert.c
+++ b/src/rspamadm/fuzzy_convert.c
@@ -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",
diff --git a/src/rspamadm/lua_repl.c b/src/rspamadm/lua_repl.c
index 39fa07480..710b9c5ef 100644
--- a/src/rspamadm/lua_repl.c
+++ b/src/rspamadm/lua_repl.c
@@ -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,
diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c
index 092e4ff58..fad9b2fcd 100644
--- a/src/rspamadm/rspamadm.c
+++ b/src/rspamadm/rspamadm.c
@@ -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);
diff --git a/src/rspamadm/rspamadm.h b/src/rspamadm/rspamadm.h
index ff1284942..cd01cc86b 100644
--- a/src/rspamadm/rspamadm.h
+++ b/src/rspamadm/rspamadm.h
@@ -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
diff --git a/src/rspamadm/stat_convert.c b/src/rspamadm/stat_convert.c
index acbe11550..e3a3e179b 100644
--- a/src/rspamadm/stat_convert.c
+++ b/src/rspamadm/stat_convert.c
@@ -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",