}
/* 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) {
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)
{
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);
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);
}
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 */
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) {
}
}
-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
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");