]> source.dussan.org Git - rspamd.git/commitdiff
Implement refcounting for configuration
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 18 Nov 2015 16:01:05 +0000 (16:01 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 18 Nov 2015 16:01:05 +0000 (16:01 +0000)
13 files changed:
src/controller.c
src/libmime/smtp_utils.c
src/libserver/cfg_file.h
src/libserver/cfg_utils.c
src/libserver/task.c
src/libserver/task.h
src/libserver/worker_util.h
src/lua/lua_task.c
src/lua/lua_util.c
src/plugins/fuzzy_check.c
src/rspamadm/rspamadm.c
src/rspamd.c
src/worker.c

index 39312f48c8a12bf1e28e48b081ef48387226aba3..be60050fc4b203c2927cef7ee7dd7fa9f8d737e1 100644 (file)
@@ -1262,7 +1262,7 @@ rspamd_controller_handle_learn_common (
                return 0;
        }
 
-       task = rspamd_task_new (session->ctx->worker);
+       task = rspamd_task_new (session->ctx->worker, session->cfg);
 
        task->resolver = ctx->resolver;
        task->ev_base = ctx->ev_base;
@@ -1271,7 +1271,7 @@ rspamd_controller_handle_learn_common (
        task->s = rspamd_session_create (session->pool,
                        rspamd_controller_learn_fin_task,
                        NULL,
-                       rspamd_task_free_hard,
+                       (event_finalizer_t )rspamd_task_free,
                        task);
        task->fin_arg = conn_ent;
        task->http_conn = rspamd_http_connection_ref (conn_ent->conn);;
@@ -1355,7 +1355,7 @@ rspamd_controller_handle_scan (struct rspamd_http_connection_entry *conn_ent,
                return 0;
        }
 
-       task = rspamd_task_new (session->ctx->worker);
+       task = rspamd_task_new (session->ctx->worker, session->cfg);
        task->ev_base = session->ctx->ev_base;
 
        task->resolver = ctx->resolver;
@@ -1364,7 +1364,7 @@ rspamd_controller_handle_scan (struct rspamd_http_connection_entry *conn_ent,
        task->s = rspamd_session_create (session->pool,
                        rspamd_controller_check_fin_task,
                        NULL,
-                       rspamd_task_free_hard,
+                       (event_finalizer_t )rspamd_task_free,
                        task);
        task->fin_arg = conn_ent;
        task->http_conn = rspamd_http_connection_ref (conn_ent->conn);
@@ -1765,7 +1765,7 @@ rspamd_controller_stat_cleanup_task (void *ud)
 {
        struct rspamd_stat_cbdata *cbdata = ud;
 
-       rspamd_task_free_hard (cbdata->task);
+       rspamd_task_free (cbdata->task);
        ucl_object_unref (cbdata->top);
 }
 
@@ -1794,7 +1794,7 @@ rspamd_controller_handle_stat_common (
        rspamd_mempool_stat (&mem_st);
        memcpy (&stat_copy, session->ctx->worker->srv->stat, sizeof (stat_copy));
        stat = &stat_copy;
-       task = rspamd_task_new (session->ctx->worker);
+       task = rspamd_task_new (session->ctx->worker, session->cfg);
 
        ctx = session->ctx;
        task->resolver = ctx->resolver;
@@ -2033,6 +2033,7 @@ rspamd_controller_finish_handler (struct rspamd_http_connection_entry *conn_ent)
 
        session->wrk->nconns --;
        rspamd_inet_address_destroy (session->from_addr);
+       REF_RELEASE (session->cfg);
        g_slice_free1 (sizeof (struct rspamd_controller_session), session);
 }
 
@@ -2061,6 +2062,8 @@ rspamd_controller_accept_socket (gint fd, short what, void *arg)
        session->pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
                        "csession");
        session->ctx = ctx;
+       session->cfg = ctx->cfg;
+       REF_RETAIN (session->cfg);
 
        session->from_addr = addr;
        session->wrk = worker;
index 0f8a84cc27e8d41d9735362d143bd6e19a3034d1..33750bae5d77f5ae73fb160d3a25f1a11be5aa06 100644 (file)
@@ -33,11 +33,11 @@ free_smtp_session (gpointer arg)
 
        if (session) {
                if (session->task) {
-                       rspamd_task_free (session->task, FALSE);
                        if (session->task->msg.begin) {
                                munmap ((gpointer)session->task->msg.begin,
                                                session->task->msg.len);
                        }
+                       rspamd_task_free (session->task);
                }
                if (session->rcpt) {
                        g_list_free (session->rcpt);
index a2ec190346e0da1c5287fe3c2bd292e586e3cd39..96fc56d52ea3d4572dc3cd4c0cda06ccaba21c83 100644 (file)
@@ -341,7 +341,7 @@ gboolean rspamd_parse_bind_line (struct rspamd_config *cfg,
  * Init default values
  * @param cfg config file
  */
-struct rspamd_config * rspamd_config_defaults (void);
+struct rspamd_config *rspamd_config_new (void);
 
 /**
  * Free memory used by config structure
index 4146d51cb7b6224c9b53f51637cb7ac2a2745737..23de0bef254a8703199ac7bc8edbd7507dd75f4d 100644 (file)
@@ -145,7 +145,7 @@ rspamd_parse_bind_line (struct rspamd_config *cfg,
 }
 
 struct rspamd_config *
-rspamd_config_defaults (void)
+rspamd_config_new (void)
 {
        struct rspamd_config *cfg;
 
index f89ca409768b94ee33f3e8e55d0d4fe6a9d368fe..c4ae1762c808dd881e90c49c732dda0861fbc415 100644 (file)
@@ -42,19 +42,19 @@ rspamd_task_quark (void)
  * Create new task
  */
 struct rspamd_task *
-rspamd_task_new (struct rspamd_worker *worker)
+rspamd_task_new (struct rspamd_worker *worker, struct rspamd_config *cfg)
 {
        struct rspamd_task *new_task;
 
-       new_task = g_slice_alloc0 (sizeof (struct rspamd_task));
+       g_assert (cfg != NULL);
 
+       new_task = g_slice_alloc0 (sizeof (struct rspamd_task));
        new_task->worker = worker;
+       new_task->cfg = cfg;
+       REF_RETAIN (cfg);
 
-       if (worker) {
-               new_task->cfg = worker->srv->cfg;
-               if (new_task->cfg->check_all_filters) {
-                       new_task->flags |= RSPAMD_TASK_FLAG_PASS_ALL;
-               }
+       if (cfg->check_all_filters) {
+               new_task->flags |= RSPAMD_TASK_FLAG_PASS_ALL;
        }
 
        gettimeofday (&new_task->tv, NULL);
@@ -169,7 +169,7 @@ rspamd_task_restore (void *arg)
  * Free all structures of worker_task
  */
 void
-rspamd_task_free (struct rspamd_task *task, gboolean is_soft)
+rspamd_task_free (struct rspamd_task *task)
 {
        struct mime_part *p;
        struct mime_text_part *tp;
@@ -229,27 +229,13 @@ rspamd_task_free (struct rspamd_task *task, gboolean is_soft)
                        event_del (&task->timeout_ev);
                }
 
+               REF_RELEASE (task->cfg);
+
                rspamd_mempool_delete (task->task_pool);
                g_slice_free1 (sizeof (struct rspamd_task), task);
        }
 }
 
-void
-rspamd_task_free_hard (gpointer ud)
-{
-       struct rspamd_task *task = ud;
-
-       rspamd_task_free (task, FALSE);
-}
-
-void
-rspamd_task_free_soft (gpointer ud)
-{
-       struct rspamd_task *task = ud;
-
-       rspamd_task_free (task, FALSE);
-}
-
 static void
 rspamd_task_unmapper (gpointer ud)
 {
index 8a9e913a474160ca43c62019fda77778858d761b..b29bcebf63d7493204ce48a14ced49c5b1c84175 100644 (file)
@@ -180,13 +180,12 @@ struct rspamd_task {
 /**
  * Construct new task for worker
  */
-struct rspamd_task * rspamd_task_new (struct rspamd_worker *worker);
+struct rspamd_task * rspamd_task_new (struct rspamd_worker *worker,
+               struct rspamd_config *cfg);
 /**
  * Destroy task object and remove its IO dispatcher if it exists
  */
-void rspamd_task_free (struct rspamd_task *task, gboolean is_soft);
-void rspamd_task_free_hard (gpointer ud);
-void rspamd_task_free_soft (gpointer ud);
+void rspamd_task_free (struct rspamd_task *task);
 
 /**
  * Called if session was restored inside fin callback
index 3242d1669e0a8c702abf4cce3b38b322a11e6196..21c86f92e0b869354b599574b339474f1e808a04 100644 (file)
@@ -86,6 +86,7 @@ struct rspamd_controller_session {
        struct rspamd_task *task;
        struct rspamd_classifier_config *cl;
        rspamd_inet_addr_t *from_addr;
+       struct rspamd_config *cfg;
        gboolean is_spam;
 };
 
index 3bc40efe4974986d3583373d860c247e766efc1a..e1920002783dbe27f3a39dc4c7dc179ed64ddb15 100644 (file)
@@ -54,20 +54,6 @@ rspamd_config.DATE_IN_PAST = function(task)
 end
  */
 
-/* Task creation */
-/***
- * @function rspamd_task.create_empty()
- * Creates new empty task object.
- * @return {rspamd_task} task object
- */
-LUA_FUNCTION_DEF (task, create_empty);
-/***
- * @function rspamd_task.create_from_buffer(input)
- * Creates new task object and load its content from the string provided.
- * @param {string} input string that contains MIME message
- * @return {rspamd_task} task object
- */
-LUA_FUNCTION_DEF (task, create_from_buffer);
 /* Task methods */
 LUA_FUNCTION_DEF (task, get_message);
 LUA_FUNCTION_DEF (task, process_message);
@@ -492,8 +478,6 @@ LUA_FUNCTION_DEF (task, set_flag);
 LUA_FUNCTION_DEF (task, get_flags);
 
 static const struct luaL_reg tasklib_f[] = {
-       LUA_INTERFACE_DEF (task, create_empty),
-       LUA_INTERFACE_DEF (task, create_from_buffer),
        {NULL, NULL}
 };
 
@@ -612,39 +596,6 @@ lua_check_text (lua_State * L, gint pos)
 }
 
 /* Task methods */
-
-static int
-lua_task_create_empty (lua_State *L)
-{
-       struct rspamd_task **ptask, *task;
-
-       task = rspamd_task_new (NULL);
-       ptask = lua_newuserdata (L, sizeof (gpointer));
-       rspamd_lua_setclass (L, "rspamd{task}", -1);
-       *ptask = task;
-       return 1;
-}
-
-static int
-lua_task_create_from_buffer (lua_State *L)
-{
-       struct rspamd_task **ptask, *task;
-       const gchar *data;
-       size_t len;
-
-       data = luaL_checklstring (L, 1, &len);
-       if (data) {
-               task = rspamd_task_new (NULL);
-               ptask = lua_newuserdata (L, sizeof (gpointer));
-               rspamd_lua_setclass (L, "rspamd{task}", -1);
-               *ptask = task;
-               task->msg.begin = rspamd_mempool_alloc (task->task_pool, len);
-               memcpy ((gpointer)task->msg.begin, data, len);
-               task->msg.len = len;
-       }
-       return 1;
-}
-
 static int
 lua_task_process_message (lua_State *L)
 {
@@ -695,7 +646,7 @@ lua_task_destroy (lua_State *L)
        struct rspamd_task *task = lua_check_task (L, 1);
 
        if (task != NULL) {
-               rspamd_task_free (task, FALSE);
+               rspamd_task_free (task);
        }
 
        return 0;
index 6a43206a5d6866c6b7620d0f7ec2585c0b21d8d0..32fc23fc118ed1ae90643fd1546147ce63659881 100644 (file)
@@ -161,7 +161,7 @@ lua_util_load_rspamd_config (lua_State *L)
        cfg_name = luaL_checkstring (L, 1);
 
        if (cfg_name) {
-               cfg = rspamd_config_defaults ();
+               cfg = rspamd_config_new ();
 
                if (rspamd_config_read (cfg, cfg_name, NULL, NULL, NULL, NULL)) {
                        msg_err_config ("cannot load config from %s", cfg_name);
@@ -190,7 +190,7 @@ lua_util_config_from_ucl (lua_State *L)
 
        if (obj) {
                cfg = g_malloc0 (sizeof (struct rspamd_config));
-               cfg = rspamd_config_defaults ();
+               cfg = rspamd_config_new ();
 
                cfg->rcl_obj = obj;
                cfg->cache = rspamd_symbols_cache_new (cfg);
@@ -238,8 +238,7 @@ lua_util_process_message (lua_State *L)
        if (cfg != NULL && message != NULL) {
                base = event_init ();
                rspamd_init_filters (cfg, FALSE);
-               task = rspamd_task_new (NULL);
-               task->cfg = cfg;
+               task = rspamd_task_new (NULL, cfg);
                task->ev_base = base;
                task->msg.begin = rspamd_mempool_alloc (task->task_pool, mlen);
                rspamd_strlcpy ((gpointer)task->msg.begin, message, mlen);
@@ -248,7 +247,7 @@ lua_util_process_message (lua_State *L)
                task->fin_arg = &res;
                task->resolver = dns_resolver_init (NULL, base, cfg);
                task->s = rspamd_session_create (task->task_pool, rspamd_task_fin,
-                                       rspamd_task_restore, rspamd_task_free_hard, task);
+                                       rspamd_task_restore, (event_finalizer_t)rspamd_task_free, task);
 
                if (!rspamd_task_load_message (task, NULL, message, mlen)) {
                        lua_pushnil (L);
@@ -266,7 +265,7 @@ lua_util_process_message (lua_State *L)
                                        ucl_object_push_lua (L, rspamd_protocol_write_ucl (task),
                                                        true);
                                        rdns_resolver_release (task->resolver->r);
-                                       rspamd_task_free_hard (task);
+                                       rspamd_session_destroy (task->s);
                                }
                        }
                        else {
index 915fee49a013e0ac330461f3eee84f95fb9ee2be..f8379d477b23adbf53f038b13d3be8a98e1be3f1 100644 (file)
@@ -1180,7 +1180,7 @@ cleanup:
        }
 
        if (session->task != NULL) {
-               rspamd_task_free (session->task, TRUE);
+               rspamd_task_free (session->task);
                session->task = NULL;
        }
 
@@ -1205,7 +1205,7 @@ fuzzy_controller_timer_callback (gint fd, short what, void *arg)
                if (*session->saved > 0 ) {
                        (*session->saved)--;
                        if (*session->saved == 0 && session->task != NULL) {
-                               rspamd_task_free (session->task, TRUE);
+                               rspamd_task_free (session->task);
                                session->task = NULL;
                        }
                }
@@ -1489,7 +1489,7 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent,
        gint r, *saved, rules = 0;
 
        /* Prepare task */
-       task = rspamd_task_new (NULL);
+       task = rspamd_task_new (session->wrk, session->cfg);
        task->cfg = ctx->cfg;
        task->ev_base = conn_ent->rt->ev_base;
 
@@ -1505,7 +1505,7 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent,
        if (r == -1) {
                msg_warn_task ("<%s>: cannot process message for fuzzy",
                                task->message_id);
-               rspamd_task_free (task, FALSE);
+               rspamd_task_free (task);
                rspamd_controller_send_error (conn_ent, 400,
                        "Message processing error");
                return;
@@ -1550,7 +1550,7 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent,
                msg_warn_task ("<%s>: cannot send fuzzy request: %s", task->message_id,
                                strerror (errno));
                rspamd_controller_send_error (conn_ent, 400, "Message sending error");
-               rspamd_task_free (task, FALSE);
+               rspamd_task_free (task);
                return;
        }
        else if (!processed) {
@@ -1567,7 +1567,7 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent,
                        rspamd_controller_send_error (conn_ent, 404,
                                "No fuzzy rules matched for flag %d", flag);
                }
-               rspamd_task_free (task, FALSE);
+               rspamd_task_free (task);
                return;
        }
 
index 5751c50d0a619f562db357223dcf6342932dedf5..d0e1caf8b9e6cefac44fc8477eca67bcc1e73997 100644 (file)
@@ -208,7 +208,7 @@ main (gint argc, gchar **argv, gchar **env)
                rspamd_strcase_equal, g_free, g_free);
        ottery_init (NULL);
        process_quark = g_quark_from_static_string ("rspamadm");
-       cfg = rspamd_config_defaults ();
+       cfg = rspamd_config_new ();
        cfg->libs_ctx = rspamd_init_libs ();
        rspamd_main = g_malloc0 (sizeof (*rspamd_main));
        rspamd_main->cfg = cfg;
index 92b17b37e4beb08f71ff55865b5bc8f79114a90d..a283abaa2c1b9ee96b3897ed9b8ba9ecc341db88 100644 (file)
@@ -268,7 +268,7 @@ reread_config (struct rspamd_main *rspamd_main)
        struct rspamd_config *tmp_cfg;
        gchar *cfg_file;
 
-       tmp_cfg = rspamd_config_defaults ();
+       tmp_cfg = rspamd_config_new ();
        tmp_cfg->c_modules = g_hash_table_ref (rspamd_main->cfg->c_modules);
        tmp_cfg->libs_ctx = rspamd_main->cfg->libs_ctx;
        REF_RETAIN (tmp_cfg->libs_ctx);
@@ -805,7 +805,7 @@ main (gint argc, gchar **argv, gchar **env)
                        "main");
        rspamd_main->stat = rspamd_mempool_alloc0_shared (rspamd_main->server_pool,
                        sizeof (struct rspamd_stat));
-       rspamd_main->cfg = rspamd_config_defaults ();
+       rspamd_main->cfg = rspamd_config_new ();
 
 #ifndef HAVE_SETPROCTITLE
        init_title (argc, argv, env);
index 8256b879e6460ef3d4b659f6a22f7cfeddfb63ec..933e8305354ae958eeaf6c1e282828ae95b3b153 100644 (file)
@@ -233,7 +233,7 @@ accept_socket (gint fd, short what, void *arg)
                return;
        }
 
-       task = rspamd_task_new (worker);
+       task = rspamd_task_new (worker, ctx->cfg);
 
        msg_info_task ("accepted connection from %s port %d",
                rspamd_inet_address_to_string (addr),
@@ -267,7 +267,7 @@ accept_socket (gint fd, short what, void *arg)
 
        /* Set up async session */
        task->s = rspamd_session_create (task->task_pool, rspamd_task_fin,
-                       rspamd_task_restore, rspamd_task_free_hard, task);
+                       rspamd_task_restore, (event_finalizer_t )rspamd_task_free, task);
 
        if (ctx->key) {
                rspamd_http_connection_set_key (task->http_conn, ctx->key);