From: Vsevolod Stakhov Date: Mon, 26 Feb 2018 15:45:33 +0000 (+0000) Subject: [Minor] Allow to detect worker's scanner flag from lua X-Git-Tag: 1.7.0~119 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4d386d0c689ad4607c67dcd634e53aadea940613;p=rspamd.git [Minor] Allow to detect worker's scanner flag from lua --- diff --git a/src/controller.c b/src/controller.c index 61e00fdd5..0443062a1 100644 --- a/src/controller.c +++ b/src/controller.c @@ -117,7 +117,8 @@ worker_t controller_worker = { "controller", /* Name */ init_controller_worker, /* Init function */ start_controller_worker, /* Start function */ - RSPAMD_WORKER_HAS_SOCKET | RSPAMD_WORKER_KILLABLE, + RSPAMD_WORKER_HAS_SOCKET | RSPAMD_WORKER_KILLABLE | + RSPAMD_WORKER_SCANNER | RSPAMD_WORKER_CONTROLLER, RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */ RSPAMD_WORKER_VER /* Version info */ }; diff --git a/src/libserver/symbols_cache.c b/src/libserver/symbols_cache.c index b890578ed..4c15184d4 100644 --- a/src/libserver/symbols_cache.c +++ b/src/libserver/symbols_cache.c @@ -1308,7 +1308,7 @@ rspamd_symbols_cache_check_symbol (struct rspamd_task *task, diff); } - if (rspamd_worker_is_normal (task->worker)) { + if (rspamd_worker_is_scanner (task->worker)) { rspamd_set_counter (item->cd, diff); } @@ -2026,7 +2026,7 @@ rspamd_symbols_cache_resort_cb (gint fd, short what, gpointer ud) double_to_tv (tm, &tv); event_add (&cbdata->resort_ev, &tv); - if (rspamd_worker_is_normal (cbdata->w)) { + if (rspamd_worker_is_scanner (cbdata->w)) { rspamd_mempool_lock_mutex (cache->mtx); /* Gather stats from shared execution times */ diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c index 7c92dc359..f6bc78aaa 100644 --- a/src/libserver/worker_util.c +++ b/src/libserver/worker_util.c @@ -588,6 +588,7 @@ rspamd_fork_worker (struct rspamd_main *rspamd_main, wrk->srv = rspamd_main; wrk->type = cf->type; wrk->cf = cf; + wrk->flags = cf->worker->flags; REF_RETAIN (cf); wrk->index = index; wrk->ctx = cf->ctx; @@ -740,19 +741,11 @@ rspamd_hard_terminate (struct rspamd_main *rspamd_main) } gboolean -rspamd_worker_is_normal (struct rspamd_worker *w) +rspamd_worker_is_scanner (struct rspamd_worker *w) { - static GQuark normal_quark = (GQuark)0; if (w) { - if (normal_quark) { - return w->type == normal_quark; - } - else { - normal_quark = g_quark_from_static_string ("normal"); - } - - return w->type == normal_quark; + return !!(w->flags & RSPAMD_WORKER_SCANNER); } return FALSE; diff --git a/src/libserver/worker_util.h b/src/libserver/worker_util.h index 14ca3ff2a..179907ee4 100644 --- a/src/libserver/worker_util.h +++ b/src/libserver/worker_util.h @@ -147,7 +147,7 @@ void rspamd_hard_terminate (struct rspamd_main *rspamd_main) G_GNUC_NORETURN; * @param w * @return */ -gboolean rspamd_worker_is_normal (struct rspamd_worker *w); +gboolean rspamd_worker_is_scanner (struct rspamd_worker *w); /** * Creates new session cache diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 15e26beda..f61fa6d0b 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -37,6 +37,7 @@ LUA_FUNCTION_DEF (worker, get_name); LUA_FUNCTION_DEF (worker, get_stat); LUA_FUNCTION_DEF (worker, get_index); LUA_FUNCTION_DEF (worker, get_pid); +LUA_FUNCTION_DEF (worker, is_scanner); LUA_FUNCTION_DEF (worker, spawn_process); const luaL_reg worker_reg[] = { @@ -45,6 +46,7 @@ const luaL_reg worker_reg[] = { LUA_INTERFACE_DEF (worker, get_index), LUA_INTERFACE_DEF (worker, get_pid), LUA_INTERFACE_DEF (worker, spawn_process), + LUA_INTERFACE_DEF (worker, is_scanner), {"__tostring", rspamd_lua_class_tostring}, {NULL, NULL} }; @@ -1431,6 +1433,22 @@ lua_worker_get_pid (lua_State *L) return 1; } + +static gint +lua_worker_is_scanner (lua_State *L) +{ + struct rspamd_worker *w = lua_check_worker (L, 1); + + if (w) { + lua_pushboolean (L, rspamd_worker_is_scanner (w)); + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + struct rspamd_lua_process_cbdata { gint sp[2]; gint func_cbref; diff --git a/src/rspamd.h b/src/rspamd.h index adc2576b4..4fdc626da 100644 --- a/src/rspamd.h +++ b/src/rspamd.h @@ -52,6 +52,17 @@ #define CR '\r' #define LF '\n' +enum rspamd_worker_flags { + RSPAMD_WORKER_HAS_SOCKET = (1 << 0), + RSPAMD_WORKER_UNIQUE = (1 << 1), + RSPAMD_WORKER_THREADED = (1 << 2), + RSPAMD_WORKER_KILLABLE = (1 << 3), + RSPAMD_WORKER_ALWAYS_START = (1 << 4), + RSPAMD_WORKER_SCANNER = (1 << 5), + RSPAMD_WORKER_CONTROLLER = (1 << 6), +}; + + /** * Worker process structure */ @@ -68,6 +79,7 @@ struct rspamd_worker { GList *accept_events; /**< socket events */ struct rspamd_worker_conf *cf; /**< worker config data */ gpointer ctx; /**< worker's specific data */ + enum rspamd_worker_flags flags; /**< worker's flags */ gint control_pipe[2]; /**< control pipe. [0] is used by main process, [1] is used by a worker */ gint srv_pipe[2]; /**< used by workers to request something from the @@ -180,14 +192,6 @@ typedef struct module_s { const gchar *rspamd_features; } module_t; -enum rspamd_worker_flags { - RSPAMD_WORKER_HAS_SOCKET = (1 << 0), - RSPAMD_WORKER_UNIQUE = (1 << 1), - RSPAMD_WORKER_THREADED = (1 << 2), - RSPAMD_WORKER_KILLABLE = (1 << 3), - RSPAMD_WORKER_ALWAYS_START = (1 << 4), -}; - enum rspamd_worker_socket_type { RSPAMD_WORKER_SOCKET_NONE = 0, RSPAMD_WORKER_SOCKET_TCP = (1 << 0), diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c index 6dedffff3..a7c20e34b 100644 --- a/src/rspamd_proxy.c +++ b/src/rspamd_proxy.c @@ -72,7 +72,7 @@ worker_t rspamd_proxy_worker = { "rspamd_proxy", /* Name */ init_rspamd_proxy, /* Init function */ start_rspamd_proxy, /* Start function */ - RSPAMD_WORKER_HAS_SOCKET | RSPAMD_WORKER_KILLABLE, + RSPAMD_WORKER_HAS_SOCKET | RSPAMD_WORKER_KILLABLE | RSPAMD_WORKER_SCANNER, RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */ RSPAMD_WORKER_VER }; diff --git a/src/worker.c b/src/worker.c index fc71be1be..6b02b5753 100644 --- a/src/worker.c +++ b/src/worker.c @@ -51,7 +51,7 @@ worker_t normal_worker = { "normal", /* Name */ init_worker, /* Init function */ start_worker, /* Start function */ - RSPAMD_WORKER_HAS_SOCKET|RSPAMD_WORKER_KILLABLE, + RSPAMD_WORKER_HAS_SOCKET|RSPAMD_WORKER_KILLABLE|RSPAMD_WORKER_SCANNER, RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */ RSPAMD_WORKER_VER /* Version info */ };