Browse Source

Fix serializing of hyperscan regexps

tags/1.1.0
Vsevolod Stakhov 8 years ago
parent
commit
b8ef681bf9
3 changed files with 31 additions and 13 deletions
  1. 6
    2
      src/hs_helper.c
  2. 24
    10
      src/libserver/re_cache.c
  3. 1
    1
      src/libserver/re_cache.h

+ 6
- 2
src/hs_helper.c View File

struct hs_helper_ctx *ctx = worker->ctx; struct hs_helper_ctx *ctx = worker->ctx;
GError *err = NULL; GError *err = NULL;
struct rspamd_srv_command srv_cmd; struct rspamd_srv_command srv_cmd;
gint ncompiled;


ctx->ev_base = rspamd_prepare_worker (worker, ctx->ev_base = rspamd_prepare_worker (worker,
"hs_helper", "hs_helper",
msg_warn ("cannot cleanup cache dir '%s'", ctx->hs_dir); msg_warn ("cannot cleanup cache dir '%s'", ctx->hs_dir);
} }


if (!rspamd_re_cache_compile_hyperscan (ctx->cfg->re_cache,
if ((ncompiled = rspamd_re_cache_compile_hyperscan (ctx->cfg->re_cache,
ctx->hs_dir, ctx->hs_dir,
&err)) {
&err)) == -1) {
msg_err ("failed to compile re cache: %e", err); msg_err ("failed to compile re cache: %e", err);
g_error_free (err); g_error_free (err);


exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
} }


msg_info ("compiled %d regular expressions to the hyperscan tree",
ncompiled);

event_base_loop (ctx->ev_base, 0); event_base_loop (ctx->ev_base, 0);
rspamd_worker_block_signals (); rspamd_worker_block_signals ();



+ 24
- 10
src/libserver/re_cache.c View File

return ret; return ret;
} }


gboolean
gint
rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache, rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache,
const char *cache_dir, const char *cache_dir,
GError **err) GError **err)


#ifndef WITH_HYPERSCAN #ifndef WITH_HYPERSCAN
g_set_error (err, rspamd_re_cache_quark (), EINVAL, "hyperscan is disabled"); g_set_error (err, rspamd_re_cache_quark (), EINVAL, "hyperscan is disabled");
return FALSE;
return -1;
#else #else
GHashTableIter it, cit; GHashTableIter it, cit;
gpointer k, v; gpointer k, v;
guint *hs_flags = NULL; guint *hs_flags = NULL;
const gchar **hs_pats = NULL; const gchar **hs_pats = NULL;
gchar *hs_serialized; gchar *hs_serialized;
gsize serialized_len;
gsize serialized_len, total = 0;
struct iovec iov[3];


g_hash_table_iter_init (&it, cache->re_classes); g_hash_table_iter_init (&it, cache->re_classes);


if (fd == -1) { if (fd == -1) {
g_set_error (err, rspamd_re_cache_quark (), errno, "cannot open file " g_set_error (err, rspamd_re_cache_quark (), errno, "cannot open file "
"%s: %s", path, strerror (errno)); "%s: %s", path, strerror (errno));
return FALSE;
return -1;
} }


g_hash_table_iter_init (&cit, re_class->re); g_hash_table_iter_init (&cit, re_class->re);
close (fd); close (fd);
hs_free_compile_error (hs_errors); hs_free_compile_error (hs_errors);


return FALSE;
return -1;
} }


g_free (hs_flags); g_free (hs_flags);
g_free (hs_ids);
g_free (hs_pats); g_free (hs_pats);


if (hs_serialize_database (test_db, &hs_serialized, if (hs_serialize_database (test_db, &hs_serialized,
re_class->hash); re_class->hash);


close (fd); close (fd);
g_free (hs_ids);
hs_free_database (test_db); hs_free_database (test_db);


return FALSE;
return -1;
} }


hs_free_database (test_db); hs_free_database (test_db);


if (write (fd, hs_serialized, serialized_len) != (gssize)serialized_len) {
/* Write N, then all ID's and then the compiled structure */
iov[0].iov_base = &n;
iov[0].iov_len = sizeof (n);
iov[1].iov_base = hs_ids;
iov[1].iov_len = sizeof (*hs_ids) * n;
iov[2].iov_base = hs_serialized;
iov[2].iov_len = serialized_len;

if (writev (fd, iov, 3) !=
(gssize)serialized_len + sizeof (n) + sizeof (*hs_ids) * n) {
g_set_error (err, g_set_error (err,
rspamd_re_cache_quark (), rspamd_re_cache_quark (),
errno, errno,
"cannot serialize tree of regexp to %s: %s", "cannot serialize tree of regexp to %s: %s",
path, strerror (errno)); path, strerror (errno));
close (fd); close (fd);
g_free (hs_ids);
g_free (hs_serialized); g_free (hs_serialized);


return FALSE;
return -1;
} }


total += n;

g_free (hs_serialized); g_free (hs_serialized);
g_free (hs_ids);
} }


close (fd); close (fd);
} }


return TRUE;
return total;
#endif #endif
} }

+ 1
- 1
src/libserver/re_cache.h View File

/** /**
* Compile expressions to the hyperscan tree and store in the `cache_dir` * Compile expressions to the hyperscan tree and store in the `cache_dir`
*/ */
gboolean rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache,
gint rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache,
const char *cache_dir, const char *cache_dir,
GError **err); GError **err);



Loading…
Cancel
Save