static gboolean mmap_cache_file (struct symbols_cache *cache, gint fd, rspamd_mempool_t *pool) { guint8 *map; gint i; GList *cur; struct cache_item *item; if (cache->used_items > 0) { map = mmap (NULL, cache->used_items * sizeof (struct saved_cache_item), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (map == MAP_FAILED) { msg_err ("cannot mmap cache file: %d, %s", errno, strerror (errno)); close (fd); return FALSE; } /* Close descriptor as it would never be used */ close (fd); cache->map = map; /* Now free old values for saved cache items and fill them with mmapped ones */ i = 0; cur = g_list_first (cache->negative_items); while (cur) { item = cur->data; item->s = (struct saved_cache_item *)(map + i * sizeof (struct saved_cache_item)); cur = g_list_next (cur); i++; } cur = g_list_first (cache->static_items); while (cur) { item = cur->data; item->s = (struct saved_cache_item *)(map + i * sizeof (struct saved_cache_item)); cur = g_list_next (cur); i++; } post_cache_init (cache); } return TRUE; }
gboolean rspamd_symbols_cache_validate (struct symbols_cache *cache, struct rspamd_config *cfg, gboolean strict) { struct cache_item *item; GList *cur, *metric_symbols; if (cache == NULL) { msg_err ("empty cache is invalid"); return FALSE; } /* Now adjust symbol weights according to default metric */ if (cfg->default_metric != NULL) { g_hash_table_foreach (cfg->default_metric->symbols, rspamd_symbols_cache_metric_validate_cb, cache); } g_hash_table_foreach (cache->items_by_symbol, rspamd_symbols_cache_validate_cb, cache); /* Now check each metric item and find corresponding symbol in a cache */ metric_symbols = g_hash_table_get_keys (cfg->metrics_symbols); cur = metric_symbols; while (cur) { item = g_hash_table_lookup (cache->items_by_symbol, cur->data); if (item == NULL) { msg_warn ( "symbol '%s' has its score defined but there is no " "corresponding rule registered", cur->data); if (strict) { g_list_free (metric_symbols); return FALSE; } } cur = g_list_next (cur); } g_list_free (metric_symbols); post_cache_init (cache); return TRUE; }
gboolean rspamd_symbols_cache_init (struct symbols_cache* cache, struct rspamd_config *cfg) { gboolean res; g_assert (cache != NULL); cache->cfg = cfg; /* Just in-memory cache */ if (cfg->cache_filename == NULL) { post_cache_init (cache); return TRUE; } /* Copy saved cache entries */ res = rspamd_symbols_cache_load_items (cache, cfg->cache_filename); return res; }