#include "cfg_rcl.h"
#include "ucl.h"
#include "regexp.h"
+#include "ref.h"
#define DEFAULT_BIND_PORT 11333
#define DEFAULT_CONTROL_PORT 11334
gchar *profile_path;
#endif
- gboolean no_fork; /**< if 1 do not call daemon() */
- gboolean config_test; /**< if TRUE do only config file test */
gboolean raw_mode; /**< work in raw mode instead of utf one */
gboolean one_shot_mode; /**< rules add only one symbol */
gboolean check_text_attachements; /**< check text attachements as text */
gchar *log_format_str; /**< raw log format string */
struct rspamd_external_libs_ctx *libs_ctx; /**< context for external libraries */
+
+ ref_entry_t ref; /**< reference counter */
};
* Init default values
* @param cfg config file
*/
-void rspamd_config_defaults (struct rspamd_config *cfg);
+struct rspamd_config * rspamd_config_defaults (void);
/**
* Free memory used by config structure
* Do post load actions for config
* @param cfg config file
*/
-void rspamd_config_post_load (struct rspamd_config *cfg);
+gboolean rspamd_config_post_load (struct rspamd_config *cfg,
+ gboolean validate_cache);
/**
* Calculate checksum for config file
*/
gboolean rspamd_init_filters (struct rspamd_config *cfg, bool reconfig);
-/**
- * Init configuration file structure
- * @param cfg
- * @param init_lua
- */
-void rspamd_init_cfg (struct rspamd_config *cfg, gboolean init_lua);
-
/**
* Add new symbol to the metric
* @param metric metric's name (or NULL for the default metric)
return ret;
}
-void
-rspamd_config_defaults (struct rspamd_config *cfg)
+struct rspamd_config *
+rspamd_config_defaults (void)
{
+ struct rspamd_config *cfg;
+
+ cfg = g_slice_alloc0 (sizeof (*cfg));
+ cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "cfg");
cfg->dns_timeout = 1000;
cfg->dns_retransmits = 5;
/* After 20 errors do throttling for 10 seconds */
cfg->words_decay = DEFAULT_WORDS_DECAY;
cfg->min_word_len = DEFAULT_MIN_WORD;
cfg->max_word_len = DEFAULT_MAX_WORD;
+
+ cfg->lua_state = rspamd_lua_init (cfg);
+ cfg->cache = rspamd_symbols_cache_new (cfg);
+
+ REF_INIT_RETAIN (cfg, rspamd_config_free);
}
void
g_list_free (cfg->classifiers);
g_list_free (cfg->metrics_list);
+ lua_close (cfg->lua_state);
+ rspamd_symbols_cache_destroy (cfg->cache);
+ REF_RELEASE (cfg->libs_ctx);
rspamd_mempool_delete (cfg->cfg_pool);
}
/*
* Perform post load actions
*/
-void
-rspamd_config_post_load (struct rspamd_config *cfg)
+gboolean
+rspamd_config_post_load (struct rspamd_config *cfg, gboolean validate_cache)
{
#ifdef HAVE_CLOCK_GETTIME
struct timespec ts;
if (!rspamd_config_parse_log_format (cfg)) {
msg_err_config ("cannot parse log format, task logging will not be available");
}
+
+ /* Init config cache */
+ rspamd_symbols_cache_init (cfg->cache);
+
+ /* Validate cache */
+ if (validate_cache) {
+ return rspamd_symbols_cache_validate (cfg->cache, cfg, FALSE);
+ }
+
+ return TRUE;
}
#if 0
return rspamd_init_lua_filters (cfg);
}
-void
-rspamd_init_cfg (struct rspamd_config *cfg, gboolean init_lua)
-{
- cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "cfg");
- rspamd_config_defaults (cfg);
-
- if (init_lua) {
- cfg->lua_state = rspamd_lua_init (cfg);
- rspamd_mempool_add_destructor (cfg->cfg_pool,
- (rspamd_mempool_destruct_t)lua_close, cfg->lua_state);
- }
-}
-
gboolean
rspamd_config_add_metric_symbol (struct rspamd_config *cfg,
const gchar *metric_name, const gchar *symbol,
static gboolean load_rspamd_config (struct rspamd_main *rspamd_main,
struct rspamd_config *cfg,
- gboolean init_modules);
+ gboolean init_modules,
+ gboolean validate);
/* Control socket */
static gint control_fd;
exit (1);
}
- cfg->no_fork = no_fork;
- cfg->config_test = config_test;
cfg->rspamd_user = rspamd_user;
cfg->rspamd_group = rspamd_group;
cfg_num = cfg_names != NULL ? g_strv_length (cfg_names) : 0;
struct rspamd_config *tmp_cfg;
gchar *cfg_file;
- tmp_cfg = (struct rspamd_config *)g_malloc0 (sizeof (struct rspamd_config));
+ tmp_cfg = rspamd_config_defaults ();
tmp_cfg->c_modules = g_hash_table_ref (rspamd_main->cfg->c_modules);
- tmp_cfg->libs_ctx = rspamd_main->cfg->libs_ctx;
+ tmp_cfg->libs_ctx = REF_RETAIN (rspamd_main->cfg->libs_ctx);
rspamd_set_logger (tmp_cfg, g_quark_try_string ("main"), rspamd_main);
- rspamd_init_cfg (tmp_cfg, TRUE);
cfg_file = rspamd_mempool_strdup (tmp_cfg->cfg_pool,
rspamd_main->cfg->cfg_name);
- tmp_cfg->cache = rspamd_symbols_cache_new (tmp_cfg);
/* Save some variables */
tmp_cfg->cfg_name = cfg_file;
- if (!load_rspamd_config (rspamd_main, tmp_cfg, FALSE)) {
+ if (!load_rspamd_config (rspamd_main, tmp_cfg, FALSE, TRUE)) {
rspamd_set_logger (rspamd_main->cfg, g_quark_try_string (
"main"), rspamd_main);
msg_err_main ("cannot parse new config file, revert to old one");
- rspamd_config_free (tmp_cfg);
+ REF_RELEASE (tmp_cfg);
}
else {
msg_debug_main ("replacing config");
- rspamd_symbols_cache_destroy (rspamd_main->cfg->cache);
- rspamd_config_free (rspamd_main->cfg);
- g_free (rspamd_main->cfg);
+ REF_RELEASE (rspamd_main->cfg);
rspamd_main->cfg = tmp_cfg;
rspamd_set_logger (tmp_cfg, g_quark_try_string ("main"), rspamd_main);
}
rspamd_init_filters (rspamd_main->cfg, TRUE);
- rspamd_symbols_cache_init (rspamd_main->cfg->cache);
msg_info_main ("config has been reread successfully");
}
}
static gboolean
load_rspamd_config (struct rspamd_main *rspamd_main,
- struct rspamd_config *cfg, gboolean init_modules)
+ struct rspamd_config *cfg, gboolean init_modules, gboolean validate)
{
- cfg->cache = rspamd_symbols_cache_new (cfg);
cfg->compiled_modules = modules;
cfg->compiled_workers = workers;
}
/* Do post-load actions */
- rspamd_config_post_load (cfg);
+ rspamd_config_post_load (cfg, validate);
if (init_modules) {
rspamd_init_filters (cfg, FALSE);
"main");
rspamd_main->stat = rspamd_mempool_alloc0_shared (rspamd_main->server_pool,
sizeof (struct rspamd_stat));
- rspamd_main->cfg =
- (struct rspamd_config *) g_malloc0 (sizeof (struct rspamd_config));
+ rspamd_main->cfg = rspamd_config_defaults ();
#ifndef HAVE_SETPROCTITLE
init_title (argc, argv, env);
#endif
rspamd_main->cfg->libs_ctx = rspamd_init_libs ();
- rspamd_init_cfg (rspamd_main->cfg, TRUE);
memset (&signals, 0, sizeof (struct sigaction));
}
}
- if (rspamd_main->cfg->config_test || is_debug) {
+ if (config_test || is_debug) {
rspamd_main->cfg->log_level = G_LOG_LEVEL_DEBUG;
}
else {
exit (EXIT_SUCCESS);
}
- if (rspamd_main->cfg->config_test || dump_cache) {
- if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, FALSE)) {
+ if (config_test || dump_cache) {
+ if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, FALSE, FALSE)) {
exit (EXIT_FAILURE);
}
res = TRUE;
- rspamd_symbols_cache_init (rspamd_main->cfg->cache);
-
if (!rspamd_init_filters (rspamd_main->cfg, FALSE)) {
res = FALSE;
}
- /* Insert classifiers symbols */
- rspamd_config_insert_classify_symbols (rspamd_main->cfg);
-
if (!rspamd_symbols_cache_validate (rspamd_main->cfg->cache,
rspamd_main->cfg,
FALSE)) {
}
/* Load config */
- if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, TRUE)) {
+ if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, TRUE, TRUE)) {
exit (EXIT_FAILURE);
}
rspamd_main->cfg->cfg_name);
/* Daemonize */
- if (!rspamd_main->cfg->no_fork && daemon (0, 0) == -1) {
- fprintf (stderr, "Cannot daemonize\n");
+ if (!no_fork && daemon (0, 0) == -1) {
+ rspamd_fprintf (stderr, "Cannot daemonize\n");
exit (-errno);
}
/* Set title */
setproctitle ("main process");
- /* Init config cache */
- rspamd_symbols_cache_init (rspamd_main->cfg->cache);
-
- /* Validate cache */
- (void) rspamd_symbols_cache_validate (rspamd_main->cfg->cache,
- rspamd_main->cfg,
- FALSE);
/* Flush log */
rspamd_log_flush (rspamd_main->logger);
rspamd_symbols_cache_destroy (rspamd_main->cfg->cache);
rspamd_log_close (rspamd_main->logger);
- rspamd_config_free (rspamd_main->cfg);
- rspamd_deinit_libs (rspamd_main->cfg->libs_ctx);
- g_free (rspamd_main->cfg);
+ REF_RELEASE (rspamd_main->cfg);
g_free (rspamd_main);
event_base_free (ev_base);
- g_mime_shutdown ();
-
-#ifdef HAVE_OPENSSL
- EVP_cleanup ();
- ERR_free_strings ();
-#endif
return (res);
}