]> source.dussan.org Git - rspamd.git/commitdiff
Rework filters initialization.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 17 Apr 2015 12:49:11 +0000 (13:49 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 17 Apr 2015 12:49:11 +0000 (13:49 +0100)
src/libserver/cfg_file.h
src/libserver/cfg_rcl.c
src/libserver/cfg_utils.c
src/main.c

index 3e611b3238271354d3f9701724f67afb7fc4ba01..25f19d2161aefe39ec1cfde999d59e73a909368c 100644 (file)
@@ -209,7 +209,6 @@ struct rspamd_config {
        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                                                                  */
        ucl_object_t *rcl_obj;                  /**< rcl object                                                                                 */
        GHashTable * metrics;                            /**< hash of metrics indexed by metric name                            */
        GHashTable * symbols_groups;                     /**< groups of symbols                                                                 */
@@ -388,6 +387,14 @@ void rspamd_ucl_add_conf_macros (struct ucl_parser *parser,
 
 void rspamd_ucl_add_conf_variables (struct ucl_parser *parser);
 
+/**
+ * Initialize rspamd filtering system (lua and C filters)
+ * @param cfg
+ * @param reconfig
+ * @return
+ */
+gboolean rspamd_init_filters (struct rspamd_config *cfg, bool reconfig);
+
 #endif /* ifdef CFG_FILE_H */
 /*
  * vi:ts=4
index 05ba79edac4608c2ba900b2cefc83f8818074b90..19b920c69cc503b111ed96dc24c6c413ea904992 100644 (file)
@@ -1316,8 +1316,8 @@ rspamd_rcl_config_init (void)
                RSPAMD_CL_FLAG_STRING_PATH);
        rspamd_rcl_add_default_handler (sub,
                "filters",
-               rspamd_rcl_parse_struct_string,
-               G_STRUCT_OFFSET (struct rspamd_config, filters_str),
+               rspamd_rcl_parse_struct_string_list,
+               G_STRUCT_OFFSET (struct rspamd_config, filters),
                0);
        rspamd_rcl_add_default_handler (sub,
                "max_diff",
index cf9179d0a79cb820cc4554d02e51fe97b9dbd61f..510a6661a68e43036f7b030baea8ed6468b09272 100644 (file)
@@ -815,6 +815,55 @@ rspamd_ucl_fin_cb (rspamd_mempool_t * pool, struct map_cb_data *data)
        }
 }
 
+gboolean
+rspamd_init_filters (struct rspamd_config *cfg, bool reconfig)
+{
+       GList *cur;
+       module_t *mod, **pmod;
+       struct module_ctx *mod_ctx;
+
+       /* Init all compiled modules */
+       for (pmod = modules; *pmod != NULL; pmod ++) {
+               mod = *pmod;
+               mod_ctx = g_slice_alloc0 (sizeof (struct module_ctx));
+
+               if (mod->module_init_func (cfg, &mod_ctx) == 0) {
+                       g_hash_table_insert (cfg->c_modules,
+                               (gpointer) mod->name,
+                               mod_ctx);
+               }
+       }
+
+       cur = g_list_first (cfg->filters);
+
+       while (cur) {
+               /* Perform modules configuring */
+               mod = NULL;
+               for (pmod = modules; *pmod != NULL; pmod ++) {
+                       if ((*pmod)->name && g_ascii_strcasecmp ((*pmod)->name,
+                                       cur->data) == 0) {
+                               mod = *pmod;
+
+                               if (reconfig) {
+                                       (void)mod->module_reconfig_func (cfg);
+                                       msg_debug ("reconfig of %s", mod->name);
+                               }
+                               else {
+                                       (void)mod->module_config_func (cfg);
+                               }
+                       }
+               }
+
+               if (mod == NULL) {
+                       msg_warn ("requested unknown module %s", cur->data);
+               }
+
+               cur = g_list_next (cur);
+       }
+
+       return rspamd_init_lua_filters (cfg);
+}
+
 /*
  * vi:ts=4
  */
index 505b52343c920e878abbb422d1c67e521c58bbc8..4488f46a269ba0043c0a7d07b6c7232daacad0ba 100644 (file)
@@ -353,61 +353,11 @@ config_logger (rspamd_mempool_t *pool, gpointer ud)
        }
 }
 
-static void
-parse_filters_str (struct rspamd_config *cfg, const gchar *str)
-{
-       gchar **strvec, **p;
-       struct filter *cur;
-       module_t **pmodule;
-
-       if (str == NULL) {
-               return;
-       }
-
-       strvec = g_strsplit_set (str, ",", 0);
-       if (strvec == NULL) {
-               return;
-       }
-
-       p = strvec;
-       while (*p) {
-               cur = NULL;
-               /* Search modules from known C modules */
-               pmodule = &modules[0];
-               while (*pmodule) {
-                       g_strstrip (*p);
-                       if ((*pmodule)->name != NULL &&
-                               g_ascii_strcasecmp ((*pmodule)->name, *p) == 0) {
-                               cur = rspamd_mempool_alloc (cfg->cfg_pool,
-                                               sizeof (struct filter));
-                               cur->type = C_FILTER;
-                               msg_debug ("found C filter %s", *p);
-                               cur->func_name = rspamd_mempool_strdup (cfg->cfg_pool, *p);
-                               cur->module = (*pmodule);
-                               cfg->filters = g_list_prepend (cfg->filters, cur);
-
-                               break;
-                       }
-                       pmodule++;
-               }
-               if (cur != NULL) {
-                       /* Go to next iteration */
-                       p++;
-                       continue;
-               }
-               p++;
-       }
-
-       g_strfreev (strvec);
-}
-
 static void
 reread_config (struct rspamd_main *rspamd)
 {
        struct rspamd_config *tmp_cfg;
        gchar *cfg_file;
-       GList *l;
-       struct filter *filt;
 
        tmp_cfg = (struct rspamd_config *)g_malloc0 (sizeof (struct rspamd_config));
        if (tmp_cfg) {
@@ -436,18 +386,8 @@ reread_config (struct rspamd_main *rspamd)
                        if (is_debug) {
                                rspamd->cfg->log_level = G_LOG_LEVEL_DEBUG;
                        }
-                       /* Perform modules configuring */
-                       l = g_list_first (rspamd->cfg->filters);
-
-                       while (l) {
-                               filt = l->data;
-                               if (filt->module) {
-                                       (void)filt->module->module_reconfig_func (rspamd->cfg);
-                                       msg_debug ("reconfig of %s", filt->module->name);
-                               }
-                               l = g_list_next (l);
-                       }
-                       rspamd_init_lua_filters (rspamd->cfg);
+
+                       rspamd_init_filters (rspamd->cfg, TRUE);
                        init_cfg_cache (rspamd->cfg);
                        msg_info ("config rereaded successfully");
                }
@@ -810,10 +750,6 @@ reopen_log_handler (gpointer key, gpointer value, gpointer unused)
 static gboolean
 load_rspamd_config (struct rspamd_config *cfg, gboolean init_modules)
 {
-       GList *l;
-       struct filter *filt;
-       struct module_ctx *cur_module = NULL;
-
        if (!rspamd_config_read (cfg, cfg->cfg_name, NULL,
                config_logger, rspamd_main)) {
                return FALSE;
@@ -833,24 +769,9 @@ load_rspamd_config (struct rspamd_config *cfg, gboolean init_modules)
 
        /* Do post-load actions */
        rspamd_config_post_load (cfg);
-       parse_filters_str (cfg, cfg->filters_str);
 
        if (init_modules) {
-               /* Init C modules */
-               l = g_list_first (cfg->filters);
-
-               while (l) {
-                       filt = l->data;
-                       if (filt->module) {
-                               cur_module = g_slice_alloc0 (sizeof (struct module_ctx));
-                               if (filt->module->module_init_func (cfg, &cur_module) == 0) {
-                                       g_hash_table_insert (cfg->c_modules,
-                                               (gpointer) filt->module->name,
-                                               cur_module);
-                               }
-                       }
-                       l = g_list_next (l);
-               }
+               rspamd_init_filters (cfg, FALSE);
        }
 
        return TRUE;
@@ -1185,9 +1106,7 @@ main (gint argc, gchar **argv, gchar **env)
        gint res = 0, i;
        struct sigaction signals;
        struct rspamd_worker *cur;
-       struct filter *filt;
        pid_t wrk;
-       GList *l;
        worker_t **pworker;
        GQuark type;
        gpointer keypair;
@@ -1273,40 +1192,19 @@ main (gint argc, gchar **argv, gchar **env)
                exit (EXIT_SUCCESS);
        }
 
-       /* Load config */
-       if (!load_rspamd_config (rspamd_main->cfg, TRUE)) {
-               exit (EXIT_FAILURE);
-       }
-
-       /* Override pidfile from configuration by command line argument */
-       if (rspamd_pidfile != NULL) {
-               rspamd_main->cfg->pid_file = rspamd_pidfile;
-       }
-
-       /* Force debug log */
-       if (is_debug) {
-               rspamd_main->cfg->log_level = G_LOG_LEVEL_DEBUG;
-       }
-
        if (rspamd_main->cfg->config_test || dump_cache) {
+               if (!load_rspamd_config (rspamd_main->cfg, FALSE)) {
+                       exit (EXIT_FAILURE);
+               }
+
                /* Init events to test modules */
                event_init ();
                res = TRUE;
-               if (!rspamd_init_lua_filters (rspamd_main->cfg)) {
+
+               if (!rspamd_init_filters (rspamd_main->cfg, FALSE)) {
                        res = FALSE;
                }
-               /* Perform modules configuring */
-               l = g_list_first (rspamd_main->cfg->filters);
-
-               while (l) {
-                       filt = l->data;
-                       if (filt->module) {
-                               if (!filt->module->module_config_func (rspamd_main->cfg)) {
-                                       res = FALSE;
-                               }
-                       }
-                       l = g_list_next (l);
-               }
+
                /* Insert classifiers symbols */
                (void)rspamd_config_insert_classify_symbols (rspamd_main->cfg);
 
@@ -1322,6 +1220,21 @@ main (gint argc, gchar **argv, gchar **env)
                return res ? EXIT_SUCCESS : EXIT_FAILURE;
        }
 
+       /* Load config */
+       if (!load_rspamd_config (rspamd_main->cfg, TRUE)) {
+               exit (EXIT_FAILURE);
+       }
+
+       /* Override pidfile from configuration by command line argument */
+       if (rspamd_pidfile != NULL) {
+               rspamd_main->cfg->pid_file = rspamd_pidfile;
+       }
+
+       /* Force debug log */
+       if (is_debug) {
+               rspamd_main->cfg->log_level = G_LOG_LEVEL_DEBUG;
+       }
+
        gperf_profiler_init (rspamd_main->cfg, "main");
 
        msg_info ("rspamd " RVERSION " is starting, build id: " RID);
@@ -1353,31 +1266,12 @@ main (gint argc, gchar **argv, gchar **env)
 
        setproctitle ("main process");
 
-       /* Init lua filters */
-       if (!rspamd_init_lua_filters (rspamd_main->cfg)) {
-               msg_err ("error loading lua plugins");
-               exit (EXIT_FAILURE);
-       }
-
        rspamd_stat_init (rspamd_main->cfg);
        rspamd_url_init (rspamd_main->cfg->tld_file);
 
        /* Insert classifiers symbols */
        (void)rspamd_config_insert_classify_symbols (rspamd_main->cfg);
 
-       /* Perform modules configuring */
-       l = g_list_first (rspamd_main->cfg->filters);
-
-       while (l) {
-               filt = l->data;
-               if (filt->module) {
-                       if (!filt->module->module_config_func (rspamd_main->cfg)) {
-                               res = FALSE;
-                       }
-               }
-               l = g_list_next (l);
-       }
-
        /* Init config cache */
        init_cfg_cache (rspamd_main->cfg);