aboutsummaryrefslogtreecommitdiffstats
path: root/src/hs_helper.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2019-10-02 18:41:37 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2019-10-02 18:41:37 +0100
commit3a4c7742435d93b617c8fd8239425d56f95a6639 (patch)
treedeb9071f93571f71dbcb819acc430713665f0a50 /src/hs_helper.c
parent3b92c509518cf97da58bde92231f8745ad07c8d0 (diff)
downloadrspamd-3a4c7742435d93b617c8fd8239425d56f95a6639.tar.gz
rspamd-3a4c7742435d93b617c8fd8239425d56f95a6639.zip
[Rework] Allow execution of async events when hs compiles regexps
Diffstat (limited to 'src/hs_helper.c')
-rw-r--r--src/hs_helper.c82
1 files changed, 54 insertions, 28 deletions
diff --git a/src/hs_helper.c b/src/hs_helper.c
index f83a9d429..3cdc2a439 100644
--- a/src/hs_helper.c
+++ b/src/hs_helper.c
@@ -178,37 +178,44 @@ rspamd_hs_helper_cleanup_dir (struct hs_helper_ctx *ctx, gboolean forced)
return ret;
}
-static gboolean
-rspamd_rs_compile (struct hs_helper_ctx *ctx, struct rspamd_worker *worker,
- gboolean forced)
+/* Bad hack, but who cares */
+static gboolean hack_global_forced;
+
+static void
+rspamd_rs_delayed_cb (EV_P_ ev_timer *w, int revents)
{
- GError *err = NULL;
+ struct rspamd_worker *worker = (struct rspamd_worker *)w->data;
static struct rspamd_srv_command srv_cmd;
- gint ncompiled;
+ struct hs_helper_ctx *ctx;
- if (!(ctx->cfg->libs_ctx->crypto_ctx->cpu_config & CPUID_SSSE3)) {
- msg_warn ("CPU doesn't have SSSE3 instructions set "
- "required for hyperscan, disable hyperscan compilation");
- return FALSE;
- }
+ ctx = (struct hs_helper_ctx *)worker->ctx;
+ memset (&srv_cmd, 0, sizeof (srv_cmd));
+ srv_cmd.type = RSPAMD_SRV_HYPERSCAN_LOADED;
+ rspamd_strlcpy (srv_cmd.cmd.hs_loaded.cache_dir, ctx->hs_dir,
+ sizeof (srv_cmd.cmd.hs_loaded.cache_dir));
+ srv_cmd.cmd.hs_loaded.forced = hack_global_forced;
+ hack_global_forced = FALSE;
- if (!rspamd_hs_helper_cleanup_dir (ctx, forced)) {
- msg_warn ("cannot cleanup cache dir '%s'", ctx->hs_dir);
- }
+ rspamd_srv_send_command (worker,
+ ctx->event_loop, &srv_cmd, -1, NULL, NULL);
+ ev_timer_stop (EV_A_ w);
+ g_free (w);
+}
- if ((ncompiled = rspamd_re_cache_compile_hyperscan (ctx->cfg->re_cache,
- ctx->hs_dir, ctx->max_time, !forced,
- &err)) == -1) {
- msg_err ("failed to compile re cache: %e", err);
- g_error_free (err);
+static void
+rspamd_rs_compile_cb (guint ncompiled, GError *err, void *cbd)
+{
+ struct rspamd_worker *worker = (struct rspamd_worker *)cbd;
+ ev_timer *tm;
+ ev_tstamp when = 0.0;
+ struct hs_helper_ctx *ctx;
- return FALSE;
- }
+ ctx = (struct hs_helper_ctx *)worker->ctx;
if (ncompiled > 0) {
msg_info ("compiled %d regular expressions to the hyperscan tree",
ncompiled);
- forced = TRUE;
+ hack_global_forced = TRUE;
}
/*
@@ -216,17 +223,36 @@ rspamd_rs_compile (struct hs_helper_ctx *ctx, struct rspamd_worker *worker,
* XXX: now we just sleep for 5 seconds to ensure that
*/
if (!ctx->loaded) {
- ev_sleep (5.0);
+ when = 5.0; /* Postpone */
ctx->loaded = TRUE;
}
- memset (&srv_cmd, 0, sizeof (srv_cmd));
- srv_cmd.type = RSPAMD_SRV_HYPERSCAN_LOADED;
- rspamd_strlcpy (srv_cmd.cmd.hs_loaded.cache_dir, ctx->hs_dir,
- sizeof (srv_cmd.cmd.hs_loaded.cache_dir));
- srv_cmd.cmd.hs_loaded.forced = forced;
+ tm = g_malloc0 (sizeof (*tm));
+ tm->data = (void *)worker;
+ ev_timer_init (tm, rspamd_rs_delayed_cb, when, 0);
+ ev_timer_start (ctx->event_loop, tm);
+}
- rspamd_srv_send_command (worker, ctx->event_loop, &srv_cmd, -1, NULL, NULL);
+static gboolean
+rspamd_rs_compile (struct hs_helper_ctx *ctx, struct rspamd_worker *worker,
+ gboolean forced)
+{
+ if (!(ctx->cfg->libs_ctx->crypto_ctx->cpu_config & CPUID_SSSE3)) {
+ msg_warn ("CPU doesn't have SSSE3 instructions set "
+ "required for hyperscan, disable hyperscan compilation");
+ return FALSE;
+ }
+
+ if (!rspamd_hs_helper_cleanup_dir (ctx, forced)) {
+ msg_warn ("cannot cleanup cache dir '%s'", ctx->hs_dir);
+ }
+
+ hack_global_forced = forced; /* killmeplease */
+ rspamd_re_cache_compile_hyperscan (ctx->cfg->re_cache,
+ ctx->hs_dir, ctx->max_time, !forced,
+ ctx->event_loop,
+ rspamd_rs_compile_cb,
+ (void *)worker);
return TRUE;
}