You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hs_helper.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "libutil/util.h"
  18. #include "libserver/cfg_file.h"
  19. #include "libserver/cfg_rcl.h"
  20. #include "libserver/worker_util.h"
  21. #include "libserver/rspamd_control.h"
  22. #include "unix-std.h"
  23. #ifdef HAVE_GLOB_H
  24. #include <glob.h>
  25. #endif
  26. static gpointer init_hs_helper (struct rspamd_config *cfg);
  27. __attribute__((noreturn)) static void start_hs_helper (struct rspamd_worker *worker);
  28. worker_t hs_helper_worker = {
  29. "hs_helper", /* Name */
  30. init_hs_helper, /* Init function */
  31. start_hs_helper, /* Start function */
  32. RSPAMD_WORKER_UNIQUE|RSPAMD_WORKER_KILLABLE|RSPAMD_WORKER_ALWAYS_START|RSPAMD_WORKER_NO_TERMINATE_DELAY,
  33. RSPAMD_WORKER_SOCKET_NONE,
  34. RSPAMD_WORKER_VER /* Version info */
  35. };
  36. static const gdouble default_max_time = 1.0;
  37. static const gdouble default_recompile_time = 60.0;
  38. static const guint64 rspamd_hs_helper_magic = 0x22d310157a2288a0ULL;
  39. /*
  40. * Worker's context
  41. */
  42. struct hs_helper_ctx {
  43. guint64 magic;
  44. /* Events base */
  45. struct ev_loop *event_loop;
  46. /* DNS resolver */
  47. struct rspamd_dns_resolver *resolver;
  48. /* Config */
  49. struct rspamd_config *cfg;
  50. /* END OF COMMON PART */
  51. gchar *hs_dir;
  52. gboolean loaded;
  53. gdouble max_time;
  54. gdouble recompile_time;
  55. ev_timer recompile_timer;
  56. };
  57. static gpointer
  58. init_hs_helper (struct rspamd_config *cfg)
  59. {
  60. struct hs_helper_ctx *ctx;
  61. GQuark type;
  62. type = g_quark_try_string ("hs_helper");
  63. ctx = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*ctx));
  64. ctx->magic = rspamd_hs_helper_magic;
  65. ctx->cfg = cfg;
  66. ctx->hs_dir = NULL;
  67. ctx->max_time = default_max_time;
  68. ctx->recompile_time = default_recompile_time;
  69. rspamd_rcl_register_worker_option (cfg,
  70. type,
  71. "cache_dir",
  72. rspamd_rcl_parse_struct_string,
  73. ctx,
  74. G_STRUCT_OFFSET (struct hs_helper_ctx, hs_dir),
  75. 0,
  76. "Directory where to save hyperscan compiled expressions");
  77. rspamd_rcl_register_worker_option (cfg,
  78. type,
  79. "max_time",
  80. rspamd_rcl_parse_struct_time,
  81. ctx,
  82. G_STRUCT_OFFSET (struct hs_helper_ctx, max_time),
  83. RSPAMD_CL_FLAG_TIME_FLOAT,
  84. "Maximum time to wait for compilation of a single expression");
  85. rspamd_rcl_register_worker_option (cfg,
  86. type,
  87. "recompile",
  88. rspamd_rcl_parse_struct_time,
  89. ctx,
  90. G_STRUCT_OFFSET (struct hs_helper_ctx, recompile_time),
  91. RSPAMD_CL_FLAG_TIME_FLOAT,
  92. "Time between recompilation checks");
  93. rspamd_rcl_register_worker_option (cfg,
  94. type,
  95. "timeout",
  96. rspamd_rcl_parse_struct_time,
  97. ctx,
  98. G_STRUCT_OFFSET (struct hs_helper_ctx, max_time),
  99. RSPAMD_CL_FLAG_TIME_FLOAT,
  100. "Maximum time to wait for compilation of a single expression");
  101. return ctx;
  102. }
  103. /**
  104. * Clean
  105. */
  106. static gboolean
  107. rspamd_hs_helper_cleanup_dir (struct hs_helper_ctx *ctx, gboolean forced)
  108. {
  109. struct stat st;
  110. glob_t globbuf;
  111. guint len, i;
  112. gint rc;
  113. gchar *pattern;
  114. gboolean ret = TRUE;
  115. pid_t our_pid = getpid ();
  116. if (stat (ctx->hs_dir, &st) == -1) {
  117. msg_err ("cannot stat path %s, %s",
  118. ctx->hs_dir,
  119. strerror (errno));
  120. return FALSE;
  121. }
  122. globbuf.gl_offs = 0;
  123. /*
  124. * We reuse this buffer for .new patterns as well, so allocate with some
  125. * margin
  126. */
  127. len = strlen (ctx->hs_dir) + 1 + sizeof ("*.hs.new") + 2;
  128. pattern = g_malloc (len);
  129. rspamd_snprintf (pattern, len, "%s%c%s", ctx->hs_dir, G_DIR_SEPARATOR, "*.hs");
  130. if ((rc = glob (pattern, 0, NULL, &globbuf)) == 0) {
  131. for (i = 0; i < globbuf.gl_pathc; i++) {
  132. if (forced ||
  133. !rspamd_re_cache_is_valid_hyperscan_file (ctx->cfg->re_cache,
  134. globbuf.gl_pathv[i], TRUE, TRUE)) {
  135. if (unlink (globbuf.gl_pathv[i]) == -1) {
  136. msg_err ("cannot unlink %s: %s", globbuf.gl_pathv[i],
  137. strerror (errno));
  138. ret = FALSE;
  139. }
  140. else {
  141. msg_notice ("successfully removed outdated hyperscan file: %s",
  142. globbuf.gl_pathv[i]);
  143. }
  144. }
  145. }
  146. }
  147. else if (rc != GLOB_NOMATCH) {
  148. msg_err ("glob %s failed: %s", pattern, strerror (errno));
  149. ret = FALSE;
  150. }
  151. globfree (&globbuf);
  152. memset (&globbuf, 0, sizeof (globbuf));
  153. rspamd_snprintf (pattern, len, "%s%c%s", ctx->hs_dir, G_DIR_SEPARATOR, "*.hs.new");
  154. if ((rc = glob (pattern, 0, NULL, &globbuf)) == 0) {
  155. for (i = 0; i < globbuf.gl_pathc; i++) {
  156. /* Check if we have a pid in the filename */
  157. const gchar *end_num = globbuf.gl_pathv[i] +
  158. strlen (globbuf.gl_pathv[i]) - (sizeof (".hs.new") - 1);
  159. const gchar *p = end_num - 1;
  160. pid_t foreign_pid = -1;
  161. while (p > globbuf.gl_pathv[i]) {
  162. if (g_ascii_isdigit (*p)) {
  163. p --;
  164. }
  165. else {
  166. p ++;
  167. break;
  168. }
  169. }
  170. gulong ul;
  171. if (p < end_num && rspamd_strtoul (p, end_num - p, &ul)) {
  172. foreign_pid = ul;
  173. }
  174. /*
  175. * Remove only files that was left by us or some non-existing process
  176. * There could be another race condition but it would just leave
  177. * extra files which is relatively innocent?
  178. */
  179. if (foreign_pid == -1 || foreign_pid == our_pid || kill (foreign_pid, 0) == -1) {
  180. if (unlink(globbuf.gl_pathv[i]) == -1) {
  181. msg_err ("cannot unlink %s: %s", globbuf.gl_pathv[i],
  182. strerror(errno));
  183. ret = FALSE;
  184. }
  185. else {
  186. msg_notice ("successfully removed outdated hyperscan temporary file: %s; "
  187. "pid of the file creator process: %P",
  188. globbuf.gl_pathv[i],
  189. foreign_pid);
  190. }
  191. }
  192. else {
  193. msg_notice ("skip removal of the hyperscan temporary file: %s; "
  194. "pid of the file creator process: %P",
  195. globbuf.gl_pathv[i],
  196. foreign_pid);
  197. }
  198. }
  199. }
  200. else if (rc != GLOB_NOMATCH) {
  201. msg_err ("glob %s failed: %s", pattern, strerror (errno));
  202. ret = FALSE;
  203. }
  204. globfree (&globbuf);
  205. g_free (pattern);
  206. return ret;
  207. }
  208. /* Bad hack, but who cares */
  209. static gboolean hack_global_forced;
  210. static void
  211. rspamd_rs_delayed_cb (EV_P_ ev_timer *w, int revents)
  212. {
  213. struct rspamd_worker *worker = (struct rspamd_worker *)w->data;
  214. static struct rspamd_srv_command srv_cmd;
  215. struct hs_helper_ctx *ctx;
  216. ctx = (struct hs_helper_ctx *)worker->ctx;
  217. memset (&srv_cmd, 0, sizeof (srv_cmd));
  218. srv_cmd.type = RSPAMD_SRV_HYPERSCAN_LOADED;
  219. rspamd_strlcpy (srv_cmd.cmd.hs_loaded.cache_dir, ctx->hs_dir,
  220. sizeof (srv_cmd.cmd.hs_loaded.cache_dir));
  221. srv_cmd.cmd.hs_loaded.forced = hack_global_forced;
  222. hack_global_forced = FALSE;
  223. rspamd_srv_send_command (worker,
  224. ctx->event_loop, &srv_cmd, -1, NULL, NULL);
  225. ev_timer_stop (EV_A_ w);
  226. g_free (w);
  227. ev_timer_again (EV_A_ &ctx->recompile_timer);
  228. }
  229. static void
  230. rspamd_rs_compile_cb (guint ncompiled, GError *err, void *cbd)
  231. {
  232. struct rspamd_worker *worker = (struct rspamd_worker *)cbd;
  233. ev_timer *tm;
  234. ev_tstamp when = 0.0;
  235. struct hs_helper_ctx *ctx;
  236. ctx = (struct hs_helper_ctx *)worker->ctx;
  237. if (err != NULL) {
  238. /* Failed to compile: log and go out */
  239. msg_err ("cannot compile Hyperscan database: %e", err);
  240. return;
  241. }
  242. if (ncompiled > 0) {
  243. /* Enforce update for other workers */
  244. hack_global_forced = TRUE;
  245. }
  246. /*
  247. * Do not send notification unless all other workers are started
  248. * XXX: now we just sleep for 1 seconds to ensure that
  249. */
  250. if (!ctx->loaded) {
  251. when = 1.0; /* Postpone */
  252. ctx->loaded = TRUE;
  253. msg_info ("compiled %d regular expressions to the hyperscan tree, "
  254. "postpone loaded notification for %.0f seconds to avoid races",
  255. ncompiled,
  256. when);
  257. }
  258. else {
  259. msg_info ("compiled %d regular expressions to the hyperscan tree, "
  260. "send loaded notification",
  261. ncompiled);
  262. }
  263. tm = g_malloc0 (sizeof (*tm));
  264. tm->data = (void *)worker;
  265. ev_timer_init (tm, rspamd_rs_delayed_cb, when, 0);
  266. ev_timer_start (ctx->event_loop, tm);
  267. }
  268. static gboolean
  269. rspamd_rs_compile (struct hs_helper_ctx *ctx, struct rspamd_worker *worker,
  270. gboolean forced)
  271. {
  272. #ifndef __aarch64__
  273. if (!(ctx->cfg->libs_ctx->crypto_ctx->cpu_config & CPUID_SSSE3)) {
  274. msg_warn ("CPU doesn't have SSSE3 instructions set "
  275. "required for hyperscan, disable hyperscan compilation");
  276. return FALSE;
  277. }
  278. #endif
  279. if (!rspamd_hs_helper_cleanup_dir (ctx, forced)) {
  280. msg_warn ("cannot cleanup cache dir '%s'", ctx->hs_dir);
  281. }
  282. hack_global_forced = forced; /* killmeplease */
  283. rspamd_re_cache_compile_hyperscan (ctx->cfg->re_cache,
  284. ctx->hs_dir, ctx->max_time, !forced,
  285. ctx->event_loop,
  286. rspamd_rs_compile_cb,
  287. (void *)worker);
  288. return TRUE;
  289. }
  290. static gboolean
  291. rspamd_hs_helper_reload (struct rspamd_main *rspamd_main,
  292. struct rspamd_worker *worker, gint fd,
  293. gint attached_fd,
  294. struct rspamd_control_command *cmd,
  295. gpointer ud)
  296. {
  297. struct rspamd_control_reply rep;
  298. struct hs_helper_ctx *ctx = ud;
  299. msg_info ("recompiling hyperscan expressions after receiving reload command");
  300. memset (&rep, 0, sizeof (rep));
  301. rep.type = RSPAMD_CONTROL_RECOMPILE;
  302. rep.reply.recompile.status = 0;
  303. /* We write reply before actual recompilation as it takes a lot of time */
  304. if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) {
  305. msg_err ("cannot write reply to the control socket: %s",
  306. strerror (errno));
  307. }
  308. /* Stop recompile */
  309. ev_timer_stop (ctx->event_loop, &ctx->recompile_timer);
  310. rspamd_rs_compile (ctx, worker, TRUE);
  311. return TRUE;
  312. }
  313. static void
  314. rspamd_hs_helper_timer (EV_P_ ev_timer *w, int revents)
  315. {
  316. struct rspamd_worker *worker = (struct rspamd_worker *)w->data;
  317. struct hs_helper_ctx *ctx;
  318. double tim;
  319. ctx = worker->ctx;
  320. tim = rspamd_time_jitter (ctx->recompile_time, 0);
  321. w->repeat = tim;
  322. rspamd_rs_compile (ctx, worker, FALSE);
  323. }
  324. static void
  325. start_hs_helper (struct rspamd_worker *worker)
  326. {
  327. struct hs_helper_ctx *ctx = worker->ctx;
  328. double tim;
  329. g_assert (rspamd_worker_check_context (worker->ctx, rspamd_hs_helper_magic));
  330. ctx->cfg = worker->srv->cfg;
  331. if (ctx->hs_dir == NULL) {
  332. ctx->hs_dir = ctx->cfg->hs_cache_dir;
  333. }
  334. if (ctx->hs_dir == NULL) {
  335. ctx->hs_dir = RSPAMD_DBDIR "/";
  336. }
  337. ctx->event_loop = rspamd_prepare_worker (worker,
  338. "hs_helper",
  339. NULL);
  340. if (!rspamd_rs_compile (ctx, worker, FALSE)) {
  341. /* Tell main not to respawn more workers */
  342. exit (EXIT_SUCCESS);
  343. }
  344. rspamd_control_worker_add_cmd_handler (worker, RSPAMD_CONTROL_RECOMPILE,
  345. rspamd_hs_helper_reload, ctx);
  346. ctx->recompile_timer.data = worker;
  347. tim = rspamd_time_jitter (ctx->recompile_time, 0);
  348. ev_timer_init (&ctx->recompile_timer, rspamd_hs_helper_timer, tim, 0.0);
  349. ev_timer_start (ctx->event_loop, &ctx->recompile_timer);
  350. ev_loop (ctx->event_loop, 0);
  351. rspamd_worker_block_signals ();
  352. rspamd_log_close (worker->srv->logger);
  353. REF_RELEASE (ctx->cfg);
  354. exit (EXIT_SUCCESS);
  355. }