aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rambler-co.ru>2011-08-18 17:32:55 +0400
committerVsevolod Stakhov <vsevolod@rambler-co.ru>2011-08-18 17:32:55 +0400
commit30bad7395e5f45360b5716aeb5e7338f204cd36c (patch)
tree0ef8a9c95bd1f10aa96459f078b8f1df0016d966
parentc4cf1b8310acec912203865f64ccd25751f4af7a (diff)
downloadrspamd-30bad7395e5f45360b5716aeb5e7338f204cd36c.tar.gz
rspamd-30bad7395e5f45360b5716aeb5e7338f204cd36c.zip
* Introduce new system of contexts initialization. It would be used for other things in rspamd as well in future.
Ignore unknown headers in case of http.
-rw-r--r--src/cfg_file.h1
-rw-r--r--src/cfg_xml.c64
-rw-r--r--src/main.c31
-rw-r--r--src/main.h7
-rw-r--r--src/protocol.c6
5 files changed, 73 insertions, 36 deletions
diff --git a/src/cfg_file.h b/src/cfg_file.h
index f58c228c7..fc1ba547a 100644
--- a/src/cfg_file.h
+++ b/src/cfg_file.h
@@ -236,6 +236,7 @@ struct worker_conf {
GHashTable *params; /**< params for worker */
GQueue *active_workers; /**< linked list of spawned workers */
gboolean has_socket; /**< whether we should make listening socket in main process */
+ gpointer *ctx; /**< worker's context */
};
/**
diff --git a/src/cfg_xml.c b/src/cfg_xml.c
index 4acc48673..8fad5f03a 100644
--- a/src/cfg_xml.c
+++ b/src/cfg_xml.c
@@ -826,13 +826,44 @@ options_handle_nameserver (struct config_file *cfg, struct rspamd_xml_userdata *
}
/* Worker section */
+
+struct wrk_cbdata {
+ struct worker_conf *wrk;
+ struct config_file *cfg;
+ struct rspamd_xml_userdata *ctx;
+};
+
+static void
+worker_foreach_callback (gpointer k, gpointer v, gpointer ud)
+{
+ struct wrk_cbdata *cd = ud;
+ struct xml_config_param *cparam;
+ GHashTable *worker_config;
+
+ if (cd->wrk->ctx == NULL) {
+ cd->wrk->ctx = init_workers_ctx (cd->wrk->type);
+ }
+
+ if (!worker_options || (worker_config = g_hash_table_lookup (worker_options, &cd->wrk->type)) == NULL ||
+ (cparam = g_hash_table_lookup (worker_config, k)) == NULL) {
+ msg_warn ("unregistered worker attribute '%s' for worker %s", k, process_to_str (cd->wrk->type));
+ }
+ else {
+
+ if (cd->wrk->ctx != NULL) {
+ cparam->handler (cd->cfg, cd->ctx, NULL, v, NULL, cd->wrk->ctx, cparam->offset);
+ }
+ else {
+ msg_err ("Bad error detected: module %s has not initialized its context", process_to_str (cd->wrk->type));
+ }
+ }
+}
+
gboolean
worker_handle_param (struct config_file *cfg, struct rspamd_xml_userdata *ctx, const gchar *tag, GHashTable *attrs, gchar *data, gpointer user_data, gpointer dest_struct, gint offset)
{
struct worker_conf *wrk = ctx->section_pointer;
const gchar *name;
- struct xml_config_param *cparam;
- GHashTable *worker_config;
if (g_ascii_strcasecmp (tag, "option") == 0 || g_ascii_strcasecmp (tag, "param") == 0) {
if (attrs == NULL || (name = g_hash_table_lookup (attrs, "name")) == NULL) {
@@ -844,18 +875,11 @@ worker_handle_param (struct config_file *cfg, struct rspamd_xml_userdata *ctx, c
name = memory_pool_strdup (cfg->cfg_pool, tag);
}
- if (!worker_options ||
- (worker_config = g_hash_table_lookup (worker_options, &wrk->type)) == NULL ||
- (cparam = g_hash_table_lookup (worker_config, name)) == NULL) {
- msg_warn ("unregistered worker attribute '%s' for worker %s", name, process_to_str (wrk->type));
- g_hash_table_insert (wrk->params, (char *)name, memory_pool_strdup (cfg->cfg_pool, data));
- }
- else {
- return cparam->handler (cfg, ctx, attrs, data, NULL, cparam->user_data, cparam->offset);
- }
+ g_hash_table_insert (wrk->params, (char *)name, memory_pool_strdup (cfg->cfg_pool, data));
return TRUE;
}
+
gboolean
worker_handle_type (struct config_file *cfg, struct rspamd_xml_userdata *ctx, GHashTable *attrs, gchar *data, gpointer user_data, gpointer dest_struct, gint offset)
{
@@ -1764,6 +1788,7 @@ rspamd_xml_end_element (GMarkupParseContext *context, const gchar *element_name,
struct statfile *st;
gboolean res;
gpointer tptr;
+ struct wrk_cbdata wcd;
if (g_ascii_strcasecmp (element_name, "if") == 0) {
tptr = g_queue_pop_head (ud->if_stack);
@@ -1845,6 +1870,12 @@ rspamd_xml_end_element (GMarkupParseContext *context, const gchar *element_name,
case XML_READ_WORKER:
CHECK_TAG ("worker", FALSE);
if (res) {
+ /* Parse params */
+ wcd.wrk = ud->section_pointer;
+ wcd.cfg = ud->cfg;
+ wcd.ctx = ud;
+ g_hash_table_foreach (wcd.wrk->params,
+ worker_foreach_callback, &wcd);
/* Insert object to list */
ud->cfg->workers = g_list_prepend (ud->cfg->workers, ud->section_pointer);
}
@@ -2163,17 +2194,6 @@ register_worker_opt (gint wtype, const gchar *optname, element_handler_func func
param->name = optname;
g_hash_table_insert (worker, (char *)optname, param);
}
- else {
- /* Param already exists replace it */
- msg_warn ("replace old handler for param '%s'", optname);
- g_free (param);
- param = g_malloc (sizeof (struct xml_config_param));
- param->handler = func;
- param->user_data = dest_struct;
- param->offset = offset;
- param->name = optname;
- g_hash_table_insert (worker, (char *)optname, param);
- }
}
/* Register new classifier option */
diff --git a/src/main.c b/src/main.c
index 23c67912d..c6055e2de 100644
--- a/src/main.c
+++ b/src/main.c
@@ -331,7 +331,13 @@ fork_worker (struct rspamd_main *rspamd, struct worker_conf *cf)
cur->type = cf->type;
cur->pid = fork ();
cur->cf = g_malloc (sizeof (struct worker_conf));
- cur->ctx = rspamd->workers_ctx[cf->type];
+ /* Copy or init context */
+ if (cf->ctx) {
+ cur->ctx = cf->ctx;
+ }
+ else {
+ cur->ctx = init_workers_ctx (cf->type);
+ }
memcpy (cur->cf, cf, sizeof (struct worker_conf));
cur->pending = FALSE;
switch (cur->pid) {
@@ -788,13 +794,21 @@ print_symbols_cache (struct config_file *cfg)
}
}
-static void
-init_workers_ctx (struct rspamd_main *main)
+gpointer
+init_workers_ctx (enum process_type type)
{
- main->workers_ctx[TYPE_WORKER] = init_worker ();
- main->workers_ctx[TYPE_CONTROLLER] = init_controller ();
- main->workers_ctx[TYPE_FUZZY] = init_fuzzy_storage ();
- main->workers_ctx[TYPE_SMTP] = init_smtp_worker ();
+ switch (type) {
+ case TYPE_WORKER:
+ return init_worker ();
+ case TYPE_CONTROLLER:
+ return init_controller ();
+ case TYPE_FUZZY:
+ return init_fuzzy_storage ();
+ case TYPE_SMTP:
+ return init_smtp_worker ();
+ default:
+ return NULL;
+ }
}
gint
@@ -866,9 +880,6 @@ main (gint argc, gchar **argv, gchar **env)
counters = rspamd_hash_new_shared (rspamd_main->server_pool, g_str_hash, g_str_equal, 64);
/* Init listen sockets hash */
listen_sockets = g_hash_table_new (g_direct_hash, g_direct_equal);
-
- /* Init contextes */
- init_workers_ctx (rspamd_main);
/* Init classifiers options */
register_classifier_opt ("bayes", "min_tokens");
diff --git a/src/main.h b/src/main.h
index 6ac24e7ad..812877d15 100644
--- a/src/main.h
+++ b/src/main.h
@@ -92,8 +92,6 @@ struct rspamd_main {
guint ev_initialized; /**< is event system is initialized */
struct rspamd_stat *stat; /**< pointer to statistics */
- gpointer workers_ctx[TYPE_MAX]; /** Array of workers' contexts */
-
memory_pool_t *server_pool; /**< server's memory pool */
statfile_pool_t *statfile_pool; /**< shared statfiles pool */
GHashTable *workers; /**< workers pool indexed by pid */
@@ -280,6 +278,11 @@ void start_greylist_storage (struct rspamd_worker *worker);
void register_custom_controller_command (const gchar *name, controller_func_t handler, gboolean privilleged, gboolean require_message);
/**
+ * Initialize context for worker of specified type
+ */
+gpointer init_workers_ctx (enum process_type type);
+
+/**
* If set, reopen log file on next write
*/
extern struct rspamd_main *rspamd_main;
diff --git a/src/protocol.c b/src/protocol.c
index fe9621e21..ebca96bc7 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -582,8 +582,10 @@ parse_header (struct worker_task *task, f_str_t * line)
}
break;
default:
- msg_info ("wrong header: %s", headern);
- return FALSE;
+ if (!task->is_http) {
+ msg_info ("wrong header: %s", headern);
+ return FALSE;
+ }
}
return TRUE;