]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Add method to disable symbols execution in the cache
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 Mar 2016 13:26:17 +0000 (14:26 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 Mar 2016 13:26:17 +0000 (14:26 +0100)
src/libserver/symbols_cache.c
src/libserver/symbols_cache.h

index 3868a619826f33b410780b9fb5185328e7b936d1..a18f9fc69d276c25a7cc3bcd3fefea1dfdd4587b 100644 (file)
@@ -1241,6 +1241,42 @@ rspamd_symbols_cache_tm (gint fd, short what, void *data)
                        data);
 }
 
+static struct cache_savepoint *
+rspamd_symbols_cache_make_checkpoint (struct rspamd_task *task,
+               struct symbols_cache *cache)
+{
+       struct cache_savepoint *checkpoint;
+
+       if (cache->items_by_id->len != cache->items_by_order->d->len) {
+               /*
+                * Cache has been modified, need to resort it
+                */
+               msg_info_cache ("symbols cache has been modified since last check:"
+                               " old items: %ud, new items: %ud",
+                               cache->items_by_order->d->len, cache->items_by_id->len);
+               rspamd_symbols_cache_resort (cache);
+       }
+
+       checkpoint = rspamd_mempool_alloc0 (task->task_pool, sizeof (*checkpoint));
+       /* Bit 0: check started, Bit 1: check finished */
+       checkpoint->processed_bits = rspamd_mempool_alloc0 (task->task_pool,
+                       NBYTES (cache->used_items) * 2);
+       checkpoint->waitq = g_ptr_array_new ();
+       g_assert (cache->items_by_order != NULL);
+       checkpoint->version = cache->items_by_order->d->len;
+       checkpoint->order = cache->items_by_order;
+       REF_RETAIN (checkpoint->order);
+       rspamd_mempool_add_destructor (task->task_pool,
+                       rspamd_symbols_cache_order_unref, checkpoint->order);
+       rspamd_mempool_add_destructor (task->task_pool,
+                       rspamd_ptr_array_free_hard, checkpoint->waitq);
+       task->checkpoint = checkpoint;
+
+       rspamd_create_metric_result (task, DEFAULT_METRIC);
+
+       return checkpoint;
+}
+
 gboolean
 rspamd_symbols_cache_process_symbols (struct rspamd_task * task,
        struct symbols_cache *cache)
@@ -1255,48 +1291,24 @@ rspamd_symbols_cache_process_symbols (struct rspamd_task * task,
        g_assert (cache != NULL);
 
        if (task->checkpoint == NULL) {
-
-               if (cache->items_by_id->len != cache->items_by_order->d->len) {
-                       /*
-                        * Cache has been modified, need to resort it
-                        */
-                       msg_info_cache ("symbols cache has been modified since last check:"
-                                       " old items: %ud, new items: %ud",
-                                       cache->items_by_order->d->len, cache->items_by_id->len);
-                       rspamd_symbols_cache_resort (cache);
-               }
-
-               checkpoint = rspamd_mempool_alloc0 (task->task_pool, sizeof (*checkpoint));
-               /* Bit 0: check started, Bit 1: check finished */
-               checkpoint->processed_bits = rspamd_mempool_alloc0 (task->task_pool,
-                               NBYTES (cache->used_items) * 2);
-               checkpoint->waitq = g_ptr_array_new ();
-               g_assert (cache->items_by_order != NULL);
-               checkpoint->version = cache->items_by_order->d->len;
-               checkpoint->order = cache->items_by_order;
-               REF_RETAIN (checkpoint->order);
-               rspamd_mempool_add_destructor (task->task_pool,
-                               rspamd_symbols_cache_order_unref, checkpoint->order);
-               rspamd_mempool_add_destructor (task->task_pool,
-                               rspamd_ptr_array_free_hard, checkpoint->waitq);
+               checkpoint = rspamd_symbols_cache_make_checkpoint (task, cache);
                task->checkpoint = checkpoint;
-
-               rspamd_create_metric_result (task, DEFAULT_METRIC);
-               if (task->settings) {
-                       const ucl_object_t *wl;
-
-                       wl = ucl_object_lookup (task->settings, "whitelist");
-                       if (wl != NULL) {
-                               msg_info_task ("<%s> is whitelisted", task->message_id);
-                               task->flags |= RSPAMD_TASK_FLAG_SKIP;
-                               return TRUE;
-                       }
-               }
        }
        else {
                checkpoint = task->checkpoint;
        }
 
+       if (task->settings) {
+               const ucl_object_t *wl;
+
+               wl = ucl_object_lookup (task->settings, "whitelist");
+               if (wl != NULL) {
+                       msg_info_task ("<%s> is whitelisted", task->message_id);
+                       task->flags |= RSPAMD_TASK_FLAG_SKIP;
+                       return TRUE;
+               }
+       }
+
        msg_debug_task ("symbols processing stage at pass: %d", checkpoint->pass);
        start_events_pending = rspamd_session_events_pending (task->s);
 
@@ -1605,3 +1617,33 @@ rspamd_symbols_cache_find_symbol (struct symbols_cache *cache, const gchar *name
 
        return -1;
 }
+
+void
+rspamd_symbols_cache_disable_symbol (struct rspamd_task *task,
+               struct symbols_cache *cache, const gchar *symbol)
+{
+       struct cache_savepoint *checkpoint;
+       struct cache_item *item;
+
+       if (task->checkpoint == NULL) {
+               checkpoint = rspamd_symbols_cache_make_checkpoint (task, cache);
+               task->checkpoint = checkpoint;
+       }
+       else {
+               checkpoint = task->checkpoint;
+       }
+
+       item = g_hash_table_lookup (cache->items_by_symbol, symbol);
+
+       if (item != NULL) {
+               /* Set executed and finished flags */
+
+               setbit (checkpoint->processed_bits, item->id * 2);
+               setbit (checkpoint->processed_bits, item->id * 2 + 1);
+
+               msg_debug_task ("disable execution of %s", symbol);
+       }
+       else {
+               msg_info_task ("cannot disable %s: not found", symbol);
+       }
+}
index 7be15320bfe622491f4dd7a6c9f30a3f8113c2a6..6dd70f04ca5105e90c63ead201880020da66f0a2 100644 (file)
@@ -168,4 +168,10 @@ void rspamd_symbols_cache_add_dependency (struct symbols_cache *cache,
 void rspamd_symbols_cache_add_delayed_dependency (struct symbols_cache *cache,
                const gchar *from, const gchar *to);
 
+/**
+ * Disable some symbols from execution in the cache
+ */
+void rspamd_symbols_cache_disable_symbol (struct rspamd_task *task,
+               struct symbols_cache *cache, const gchar *symbol);
+
 #endif