#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;
}
}
+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);
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);
}
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 */
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);
}
}
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 */
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",
}
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;
};
struct rspamadm_control_cbdata {
- lua_State *L;
const gchar *path;
gint argc;
gchar **argv;
}
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",
rspamd_inet_addr_t *addr;
struct timeval tv;
static struct rspamadm_control_cbdata cbdata;
- lua_State *L;
gint sock;
context = g_option_context_new (
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,
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;
{
GOptionContext *context;
GError *error = NULL;
- lua_State *L;
ucl_object_t *obj;
context = g_option_context_new (
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);
"redis_db", 0, false);
}
- rspamadm_execute_lua_ucl_subr (L,
- argc,
+ rspamadm_execute_lua_ucl_subr (argc,
argv,
obj,
"fuzzy_convert",
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[] = {
{
}
}
-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;
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,
#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"
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);
}
}
- 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;
}
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);
#include <lualib.h>
extern GHashTable *ucl_vars;
-extern lua_State *L;
extern struct rspamd_main *rspamd_main;
GQuark rspamadm_error (void);
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
{
GOptionContext *context;
GError *error = NULL;
- lua_State *L;
ucl_object_t *obj;
context = g_option_context_new (
}
}
- 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);
"expire", 0, false);
}
- rspamadm_execute_lua_ucl_subr (L,
- argc,
+ rspamadm_execute_lua_ucl_subr (argc,
argv,
obj,
"stat_convert",