diff options
author | Vsevolod Stakhov <vsevolod@rambler-co.ru> | 2010-04-20 16:32:23 +0400 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rambler-co.ru> | 2010-04-20 16:32:23 +0400 |
commit | 2d8eebcf7a0951d3d1189ddface7678fea76dd4c (patch) | |
tree | 7a1748e4d3633c3b8def3de8f2a08fb370c7e61d /src | |
parent | d4b35de4315753629ac5b107968e6194eac85d24 (diff) | |
download | rspamd-2d8eebcf7a0951d3d1189ddface7678fea76dd4c.tar.gz rspamd-2d8eebcf7a0951d3d1189ddface7678fea76dd4c.zip |
* Bugfixes:
- handle '\' characters in lua strings correctly
- fix lua initialization
- avoid of using global lua state (global L)
- fix listen sockets hash to allow multiply workers of same type but on different listen sockets
- fix modules options inserting to allow multiply options of the same name
- fix parsing of lua options
- fix lua rules
Diffstat (limited to 'src')
-rw-r--r-- | src/cfg_file.h | 2 | ||||
-rw-r--r-- | src/cfg_utils.c | 2 | ||||
-rw-r--r-- | src/cfg_xml.c | 13 | ||||
-rw-r--r-- | src/classifiers/winnow.c | 2 | ||||
-rw-r--r-- | src/controller.c | 2 | ||||
-rw-r--r-- | src/lua/lua_cfg_file.c | 2 | ||||
-rw-r--r-- | src/lua/lua_common.c | 54 | ||||
-rw-r--r-- | src/lua/lua_common.h | 2 | ||||
-rw-r--r-- | src/main.c | 38 |
9 files changed, 65 insertions, 52 deletions
diff --git a/src/cfg_file.h b/src/cfg_file.h index fb220f486..baf65c377 100644 --- a/src/cfg_file.h +++ b/src/cfg_file.h @@ -161,7 +161,7 @@ struct statfile_binlog_params { uint16_t master_port; }; -typedef double (*statfile_normalize_func)(double score, void *params); +typedef double (*statfile_normalize_func)(struct config_file *cfg, double score, void *params); /** * Statfile config definition diff --git a/src/cfg_utils.c b/src/cfg_utils.c index 4450d6cad..f72bf51a3 100644 --- a/src/cfg_utils.c +++ b/src/cfg_utils.c @@ -719,7 +719,7 @@ check_worker_conf (struct config_file *cfg, struct worker_conf *c) } static double -internal_normalizer_func (double score, void *data) +internal_normalizer_func (struct config_file *cfg, double score, void *data) { double max = *(double *)data; diff --git a/src/cfg_xml.c b/src/cfg_xml.c index 5e2cd9373..1fe9a9fd5 100644 --- a/src/cfg_xml.c +++ b/src/cfg_xml.c @@ -705,18 +705,7 @@ handle_module_opt (struct config_file *cfg, struct rspamd_xml_userdata *ctx, GHa } } cur_opt = ctx->section_pointer; - /* First try to find option with this name */ - while (cur_opt) { - cur = cur_opt->data; - if (strcmp (cur->param, name) == 0) { - /* cur->value is in pool */ - cur->value = data; - cur->is_lua = is_lua; - return TRUE; - } - cur_opt = g_list_next (cur_opt); - } - /* Not found, insert */ + /* Insert option */ cur = memory_pool_alloc0 (cfg->cfg_pool, sizeof (struct module_opt)); cur->param = name; cur->value = data; diff --git a/src/classifiers/winnow.c b/src/classifiers/winnow.c index eaa9343fe..f1551fb6e 100644 --- a/src/classifiers/winnow.c +++ b/src/classifiers/winnow.c @@ -184,7 +184,7 @@ winnow_classify (struct classifier_ctx *ctx, statfile_pool_t * pool, GTree * inp if (data.count != 0) { res = data.sum / data.count; if (st->normalizer != NULL) { - res = st->normalizer (res, st->normalizer_data); + res = st->normalizer (task->cfg, res, st->normalizer_data); } } else { diff --git a/src/controller.c b/src/controller.c index 64df87d6c..875bedbcc 100644 --- a/src/controller.c +++ b/src/controller.c @@ -776,7 +776,7 @@ controller_read_socket (f_str_t * in, void *arg) maybe_write_binlog (session->learn_classifier, st, statfile, tokens); if (st->normalizer != NULL) { - sum = st->normalizer (sum, st->normalizer_data); + sum = st->normalizer (session->cfg, sum, st->normalizer_data); } free_task (task, FALSE); diff --git a/src/lua/lua_cfg_file.c b/src/lua/lua_cfg_file.c index e087600fd..799a603c0 100644 --- a/src/lua/lua_cfg_file.c +++ b/src/lua/lua_cfg_file.c @@ -57,6 +57,7 @@ lua_check_element (memory_pool_t *pool, const gchar *name, GList **options, stru /* New option */ *opt = memory_pool_alloc0 (pool, sizeof (struct module_opt)); (*opt)->is_lua = TRUE; + (*opt)->param = memory_pool_strdup (pool, name); *options = g_list_prepend (*options, *opt); } } @@ -76,7 +77,6 @@ lua_process_module (lua_State *L, const gchar *param, struct config_file *cfg) } /* Now iterate throught module table */ - lua_gettable (L, -1); for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { /* key - -2, value - -1 */ name = luaL_checkstring (L, -2); diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index dd79d9eab..fc5fe0772 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -28,7 +28,6 @@ /* Lua module init function */ #define MODULE_INIT_FUNC "module_init" -lua_State *L = NULL; const luaL_reg null_reg[] = { {"__tostring", lua_class_tostring}, {NULL, NULL} @@ -184,24 +183,25 @@ luaopen_logger (lua_State * L) void init_lua (struct config_file *cfg) { - if (L == NULL) { - L = lua_open (); - luaL_openlibs (L); - - (void)luaopen_rspamd (L); - (void)luaopen_logger (L); - (void)luaopen_config (L); - (void)luaopen_metric (L); - (void)luaopen_radix (L); - (void)luaopen_hash_table (L); - (void)luaopen_task (L); - (void)luaopen_textpart (L); - (void)luaopen_message (L); - (void)luaopen_classifier (L); - (void)luaopen_statfile (L); - cfg->lua_state = L; - memory_pool_add_destructor (cfg->cfg_pool, (pool_destruct_func)lua_close, L); - } + lua_State *L; + + L = lua_open (); + luaL_openlibs (L); + + (void)luaopen_rspamd (L); + (void)luaopen_logger (L); + (void)luaopen_config (L); + (void)luaopen_metric (L); + (void)luaopen_radix (L); + (void)luaopen_hash_table (L); + (void)luaopen_task (L); + (void)luaopen_textpart (L); + (void)luaopen_message (L); + (void)luaopen_classifier (L); + (void)luaopen_statfile (L); + cfg->lua_state = L; + memory_pool_add_destructor (cfg->cfg_pool, (pool_destruct_func)lua_close, L); + } @@ -213,6 +213,7 @@ init_lua_filters (struct config_file *cfg) GList *cur, *tmp; struct script_module *module; struct statfile *st; + lua_State *L = cfg->lua_state; cur = g_list_first (cfg->script_modules); while (cur) { @@ -413,23 +414,12 @@ lua_consolidation_func (struct worker_task *task, const char *metric_name, const return data.score; } -void -add_luabuf (const char *line) -{ - int error; - - error = luaL_loadbuffer (L, line, strlen (line), "config") || lua_pcall (L, 0, 0, 0); - if (error) { - yyerror ("lua error: %s", lua_tostring (L, -1)); - lua_pop (L, 1); /* pop error message from the stack */ - } -} - double -lua_normalizer_func (double score, void *params) +lua_normalizer_func (struct config_file *cfg, double score, void *params) { GList *p = params; double res = score; + lua_State *L = cfg->lua_state; /* Call specified function and put input score on stack */ if (!p->data) { diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index dd4e75a8f..f89ccaa30 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -44,7 +44,7 @@ void add_luabuf (const char *line); GList *call_classifier_pre_callbacks (struct classifier_config *ccf, struct worker_task *task); double call_classifier_post_callbacks (struct classifier_config *ccf, struct worker_task *task, double in); -double lua_normalizer_func (double score, void *params); +double lua_normalizer_func (struct config_file *cfg, double score, void *params); /* Config file functions */ void lua_post_load_config (struct config_file *cfg); diff --git a/src/main.c b/src/main.c index 2c02e82b1..123a69940 100644 --- a/src/main.c +++ b/src/main.c @@ -465,6 +465,37 @@ fork_delayed (struct rspamd_main *rspamd) } } +static inline uintptr_t +make_listen_key (struct in_addr *addr, int port, int family, char *path) +{ + uintptr_t res = 0; + char *key; + + if (family == AF_INET) { + /* Make fnv hash from bytes of addr and port */ + key = (char *)&addr->s_addr; + while (key - (char *)&addr->s_addr < sizeof (addr->s_addr)) { + res ^= (char)*key++; + res += (res << 1) + (res << 4) + (res << 7) + (res << 8) + (res << 24); + } + key = (char *)&port; + while (key - (char *)&port < sizeof (addr->s_addr)) { + res ^= (char)*key++; + res += (res << 1) + (res << 4) + (res << 7) + (res << 8) + (res << 24); + } + } + else { + /* Make fnv hash from bytes of path */ + key = path; + while (*key) { + res ^= (char)*key++; + res += (res << 1) + (res << 4) + (res << 7) + (res << 8) + (res << 24); + } + } + + return res; +} + static void spawn_workers (struct rspamd_main *rspamd) { @@ -479,13 +510,16 @@ spawn_workers (struct rspamd_main *rspamd) cf = cur->data; if (cf->has_socket) { - if ((p = g_hash_table_lookup (listen_sockets, GINT_TO_POINTER (cf->type))) == NULL) { + if ((p = g_hash_table_lookup (listen_sockets, GINT_TO_POINTER ( + make_listen_key (&cf->bind_addr, cf->bind_port, cf->bind_family, cf->bind_host)))) == NULL) { /* Create listen socket */ listen_sock = create_listen_socket (&cf->bind_addr, cf->bind_port, cf->bind_family, cf->bind_host); if (listen_sock == -1) { exit (-errno); } - g_hash_table_insert (listen_sockets, GINT_TO_POINTER (cf->type), GINT_TO_POINTER (listen_sock)); + g_hash_table_insert (listen_sockets, GINT_TO_POINTER ( + make_listen_key (&cf->bind_addr, cf->bind_port, cf->bind_family, cf->bind_host)), + GINT_TO_POINTER (listen_sock)); } else { /* We had socket for this type of worker */ |