]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Synchronize hyperscan caches via the main process
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sun, 23 Oct 2022 17:37:34 +0000 (18:37 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sun, 23 Oct 2022 17:37:34 +0000 (18:37 +0100)
src/libserver/hyperscan_tools.cxx
src/libserver/rspamd_control.c
src/libserver/rspamd_control.h

index 4f3ac013a01183e0299bd8092ee2fcb79b273a70..3383915c39968311e380217b34c01ac2a01d024b 100644 (file)
@@ -31,6 +31,7 @@
 #include <unistd.h> /* for unlink */
 #include <optional>
 #include "unix-std.h"
+#include "rspamd_control.h"
 
 #define msg_info_hyperscan(...)   rspamd_default_log_function (G_LOG_LEVEL_INFO, \
         "hyperscan", "", \
@@ -66,7 +67,8 @@ private:
 
        virtual ~hs_known_files_cache() {
                // Cleanup cache dir
-               if (rspamd_current_worker != nullptr && rspamd_worker_is_primary_controller(rspamd_current_worker)) {
+               /* We clean dir merely if we are running from the main process */
+               if (rspamd_current_worker == nullptr) {
                        auto cleanup_dir = [&](std::string_view dir) -> void {
                                for (const auto &ext : cache_extensions) {
                                        glob_t globbuf;
@@ -423,6 +425,23 @@ void
 rspamd_hyperscan_notice_known(const char *fname)
 {
        rspamd::util::hs_known_files_cache::get().add_cached_file(fname);
+
+       if (rspamd_current_worker != nullptr) {
+               /* Also notify main process */
+               struct rspamd_srv_command notice_cmd;
+
+               if (strlen(fname) >= sizeof(notice_cmd.cmd.hyperscan_cache_file.path)) {
+                       msg_err("internal error: length of the filename %d ('%s') is larger than control buffer path: %d",
+                               (int)strlen(fname), fname, (int)sizeof(notice_cmd.cmd.hyperscan_cache_file.path));
+               }
+               else {
+                       notice_cmd.type = RSPAMD_NOTICE_HYPERSCAN_CACHE;
+                       rspamd_srv_send_command(rspamd_current_worker,
+                               rspamd_current_worker->srv->event_loop, &notice_cmd, -1,
+                               nullptr,
+                               nullptr);
+               }
+       }
 }
 
 #endif // WITH_HYPERSCAN
\ No newline at end of file
index 199efa948628e4af5fef9192396c5b49fd2cba7d..63999ab6f611f264603091b304f9c3455f8f4153 100644 (file)
 #include <sys/resource.h>
 #endif
 
+#ifdef WITH_HYPERSCAN
+#include "hyperscan_tools.h"
+#endif
+
 static ev_tstamp io_timeout = 30.0;
 static ev_tstamp worker_io_timeout = 0.5;
 
@@ -1014,6 +1018,12 @@ rspamd_srv_handler (EV_P_ ev_io *w, int revents)
                        case RSPAMD_SRV_HEALTH:
                                rspamd_fill_health_reply (srv, &rdata->rep);
                                break;
+                       case RSPAMD_NOTICE_HYPERSCAN_CACHE:
+#ifdef WITH_HYPERSCAN
+                               rspamd_hyperscan_notice_known(cmd.cmd.hyperscan_cache_file.path);
+#endif
+                               rdata->rep.reply.hyperscan_cache_file.unused = 0;
+                               break;
                        default:
                                msg_err ("unknown command type: %d", cmd.type);
                                break;
index 1c0f593e325abffc61251cfd4bdaa4de218576e7..dff48927de8479034285da3a31be0a5bf8e8b408 100644 (file)
@@ -47,12 +47,13 @@ enum rspamd_srv_type {
        RSPAMD_SRV_ON_FORK,
        RSPAMD_SRV_HEARTBEAT,
        RSPAMD_SRV_HEALTH,
+       RSPAMD_NOTICE_HYPERSCAN_CACHE,
 };
 
 enum rspamd_log_pipe_type {
        RSPAMD_LOG_PIPE_SYMBOLS = 0,
 };
-#define CONTROL_PATHLEN MIN(PATH_MAX, PIPE_BUF - sizeof(int) * 2)
+#define CONTROL_PATHLEN MIN(PATH_MAX, PIPE_BUF - sizeof(int) * 2 - sizeof(gint64))
 struct rspamd_control_command {
        enum rspamd_control_type type;
        union {
@@ -174,6 +175,10 @@ struct rspamd_srv_command {
                struct {
                        guint status;
                } health;
+               /* Used when a worker loads a valid hyperscan file */
+               struct {
+                       char path[CONTROL_PATHLEN];
+               } hyperscan_cache_file;
        } cmd;
 };
 
@@ -205,6 +210,9 @@ struct rspamd_srv_reply {
                        guint scanners_count;
                        guint workers_hb_lost;
                } health;
+               struct {
+                       int unused;
+               } hyperscan_cache_file;
        } reply;
 };