From 8178bc554d0abe8a6cf95755a266859c84cfd786 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 8 Dec 2015 18:28:30 +0000 Subject: [PATCH] Send notifications by hs_helper process --- src/hs_helper.c | 66 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/src/hs_helper.c b/src/hs_helper.c index 9b0f5b7c8..ab90b8334 100644 --- a/src/hs_helper.c +++ b/src/hs_helper.c @@ -32,6 +32,8 @@ #ifdef HAVE_GLOB_H #include +#include + #endif static gpointer init_hs_helper (struct rspamd_config *cfg); @@ -52,7 +54,7 @@ worker_t hs_helper_worker = { * Worker's context */ struct hs_helper_ctx { - const gchar *hs_dir; + gchar *hs_dir; struct rspamd_config *cfg; struct event_base *ev_base; }; @@ -124,18 +126,13 @@ rspamd_hs_helper_cleanup_dir (struct hs_helper_ctx *ctx) return ret; } -static void -start_hs_helper (struct rspamd_worker *worker) +static gboolean +rspamd_rs_compile (struct hs_helper_ctx *ctx, struct rspamd_worker *worker) { - struct hs_helper_ctx *ctx = worker->ctx; GError *err = NULL; - struct rspamd_srv_command srv_cmd; + static struct rspamd_srv_command srv_cmd; gint ncompiled; - ctx->ev_base = rspamd_prepare_worker (worker, - "hs_helper", - NULL); - if (!rspamd_hs_helper_cleanup_dir (ctx)) { msg_warn ("cannot cleanup cache dir '%s'", ctx->hs_dir); } @@ -146,13 +143,60 @@ start_hs_helper (struct rspamd_worker *worker) msg_err ("failed to compile re cache: %e", err); g_error_free (err); - /* Tell main not to respawn process */ - exit (EXIT_SUCCESS); + return FALSE; } msg_info ("compiled %d regular expressions to the hyperscan tree", ncompiled); + srv_cmd.type = RSPAMD_SRV_HYPERSCAN_LOADED; + srv_cmd.cmd.hs_loaded.cache_dir = ctx->hs_dir; + + rspamd_srv_send_command (worker, ctx->ev_base, &srv_cmd, NULL, NULL); + + return TRUE; +} + +static gboolean +rspamd_hs_helper_reload (struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, gint fd, + struct rspamd_control_command *cmd, + gpointer ud) +{ + struct rspamd_control_reply rep; + struct hs_helper_ctx *ctx = ud; + + msg_info ("recompiling hyperscan expressions after receiving reload command"); + memset (&rep, 0, sizeof (rep)); + rep.type = RSPAMD_CONTROL_RECOMPILE; + + rep.reply.recompile.status = rspamd_rs_compile (ctx, worker); + + if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) { + msg_err ("cannot write reply to the control socket: %s", + strerror (errno)); + } + + return TRUE; +} + +static void +start_hs_helper (struct rspamd_worker *worker) +{ + struct hs_helper_ctx *ctx = worker->ctx; + + ctx->ev_base = rspamd_prepare_worker (worker, + "hs_helper", + NULL); + + if (!rspamd_rs_compile (ctx, worker)) { + /* Tell main not to respawn more workers */ + exit (EXIT_SUCCESS); + } + + rspamd_control_worker_add_cmd_handler (worker, RSPAMD_CONTROL_RECOMPILE, + rspamd_hs_helper_reload, ctx); + event_base_loop (ctx->ev_base, 0); rspamd_worker_block_signals (); -- 2.39.5