]> source.dussan.org Git - rspamd.git/commitdiff
Add rcl parsers for workers options.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 30 Sep 2013 15:43:52 +0000 (16:43 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 30 Sep 2013 15:43:52 +0000 (16:43 +0100)
config.h.in
src/cfg_file.h
src/cfg_rcl.c
src/cfg_rcl.h
src/cfg_xml.c

index dd1af0f550235a0d320f0239bfa6e946d622ed4f..d678e29983c959b6e1e1870a4b246b1a1658ed12 100644 (file)
@@ -494,7 +494,7 @@ typedef struct module_s {
 
 typedef struct worker_s {
        const gchar *name;
-       gpointer (*worker_init_func)(void);
+       gpointer (*worker_init_func)(struct config_file *cfg);
        void (*worker_start_func)(struct rspamd_worker *worker);
        gboolean has_socket;
        gboolean unique;
index bfbc0efa92b75c0a0560ad7b0774144c753e6d2f..89e4870db4afa227de5ec8748026d9dbf36181f3 100644 (file)
@@ -11,7 +11,9 @@
 #include "upstream.h"
 #include "memcached.h"
 #include "symbols_cache.h"
+#include "cfg_rcl.h"
 #include "utlist.h"
+#include "rcl/rcl.h"
 
 #define DEFAULT_BIND_PORT 768
 #define DEFAULT_CONTROL_PORT 7608
@@ -253,6 +255,19 @@ struct rspamd_worker_bind_conf {
        struct rspamd_worker_bind_conf *next;
 };
 
+struct rspamd_worker_param_parser {
+       rspamd_rcl_handler_t handler;                                   /**< handler function                                                                   */
+       struct rspamd_rcl_struct_parser parser;                 /**< parser attributes                                                                  */
+       const gchar *name;                                                              /**< parameter's name                                                                   */
+       UT_hash_handle hh;                                                              /**< hash by name                                                                               */
+};
+
+struct rspamd_worker_cfg_parser {
+       struct rspamd_worker_param_parser *parsers;             /**< parsers hash                                                                               */
+       gint type;                                                                              /**< workers quark                                                                              */
+       UT_hash_handle hh;                                                              /**< hash by type                                                                               */
+};
+
 /**
  * Config params for rspamd worker
  */
@@ -268,6 +283,7 @@ struct worker_conf {
        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                                                                   */
+       rspamd_cl_object_t *options;                                    /**< other worker's options                                                             */
 };
 
 /**
@@ -330,6 +346,7 @@ struct config_file {
 
        GList *filters;                                                                 /**< linked list of all filters                                                 */
        GList *workers;                                                                 /**< linked list of all workers params                                  */
+       struct rspamd_worker_cfg_parser *wrk_parsers;   /**< hash for worker config parsers, indexed by worker quarks */
        gchar *filters_str;                                                             /**< string of filters                                                                  */
        GHashTable* modules_opts;                                               /**< hash for module options indexed by module name             */
        GHashTable* modules_metas;                                              /**< hash for module meta options indexed by module name*/
index 70f51f8cdb774adb955ae9eab114b3445a8cf4c0..5c27a25163ca367d2dd47e4b4808757d0ebc459b 100644 (file)
@@ -388,7 +388,7 @@ rspamd_rcl_worker_handler (struct config_file *cfg, rspamd_cl_object_t *obj,
                        }
                        wrk->type = qtype;
                        if (wrk->worker->worker_init_func) {
-                               wrk->ctx = wrk->worker->worker_init_func ();
+                               wrk->ctx = wrk->worker->worker_init_func (cfg);
                        }
                }
                else {
@@ -897,3 +897,33 @@ rspamd_rcl_parse_struct_boolean (struct config_file *cfg, rspamd_cl_object_t *ob
 
        return TRUE;
 }
+
+void
+rspamd_rcl_register_worker_option (struct config_file *cfg, gint type, const gchar *name,
+               rspamd_rcl_handler_t handler, gpointer target, gsize offset, gint flags)
+{
+       struct rspamd_worker_param_parser *nhandler;
+       struct rspamd_worker_cfg_parser *nparser;
+
+       HASH_FIND_INT (cfg->wrk_parsers, &type, nparser);
+       if (nparser == NULL) {
+               /* Allocate new parser for this worker */
+               nparser = memory_pool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_worker_cfg_parser));
+               nparser->type = type;
+               HASH_ADD_INT (cfg->wrk_parsers, type, nparser);
+       }
+
+       HASH_FIND_STR (nparser->parsers, name, nhandler);
+       if (nhandler != NULL) {
+               msg_warn ("handler for parameter %s is already registered for worker type %s",
+                               name, g_quark_to_string (type));
+               return;
+       }
+       nhandler = memory_pool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_worker_param_parser));
+       nhandler->name = name;
+       nhandler->parser.flags = flags;
+       nhandler->parser.offset = offset;
+       nhandler->parser.user_struct = target;
+       nhandler->handler = handler;
+       HASH_ADD_STR (nparser->parsers, name, nhandler);
+}
index 223431d79a45682893edadaf5a8686e344100605..1b19783cf6ded8e20bc25906483b5fa35e8bfc6d 100644 (file)
@@ -26,7 +26,6 @@
 
 #include "config.h"
 #include "rcl/rcl.h"
-#include "main.h"
 
 #define CFG_RCL_ERROR cfg_rcl_error_quark ()
 static inline GQuark
@@ -36,6 +35,7 @@ cfg_rcl_error_quark (void)
 }
 
 struct rspamd_rcl_section;
+struct config_file;
 
 struct rspamd_rcl_struct_parser {
        gpointer user_struct;
@@ -198,4 +198,19 @@ gboolean rspamd_rcl_parse_struct_string_list (struct config_file *cfg, rspamd_cl
 gboolean rspamd_rcl_parse_struct_boolean (struct config_file *cfg, rspamd_cl_object_t *obj,
                gpointer ud, struct rspamd_rcl_section *section, GError **err);
 
+/**
+ * Utility functions
+ */
+
+/**
+ * Register new parser for a worker type of an option with the specified name
+ * @param cfg config structure
+ * @param type type of worker (GQuark)
+ * @param name name of option
+ * @param handler handler of option
+ * @param target opaque target structure
+ * @param offset offset inside a structure
+ */
+void rspamd_rcl_register_worker_option (struct config_file *cfg, gint type, const gchar *name,
+               rspamd_rcl_handler_t handler, gpointer target, gsize offset, gint flags);
 #endif /* CFG_RCL_H_ */
index fc7245390e339f63a4dda50bfb27c5729876f4dc..215371f2fbe95d288c99bdb3674e7aa627813cfe 100644 (file)
@@ -1022,7 +1022,7 @@ worker_handle_type (struct config_file *cfg, struct rspamd_xml_userdata *ctx, GH
                 }
                 wrk->type = type;
                 if (wrk->worker->worker_init_func) {
-                        wrk->ctx = wrk->worker->worker_init_func ();
+                        wrk->ctx = wrk->worker->worker_init_func (cfg);
                 }
        }
        else {
@@ -2525,28 +2525,7 @@ register_module_opt (const gchar *mname, const gchar *optname, enum module_opt_t
 void
 register_worker_opt (gint wtype, const gchar *optname, element_handler_func func, gpointer dest_struct, gint offset)
 {
-       struct xml_config_param          *param;
-       GHashTable                       *worker;
-       gint                             *new_key;
-
-       if (worker_options == NULL) {
-               worker_options = g_hash_table_new (g_int_hash, g_int_equal);
-       }
-       if ((worker = g_hash_table_lookup (worker_options, &wtype)) == NULL) {
-               worker = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
-               new_key = g_malloc (sizeof (gint));
-               *new_key = wtype;
-               g_hash_table_insert (worker_options, new_key, worker);
-       }
-       if ((param = g_hash_table_lookup (worker, optname)) == NULL) {
-               /* Register new 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);
-       }
+       msg_err ("this function is depreciated and must not be used");
 }
 
 /* Register new classifier option */