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.

worker.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. /*
  17. * Rspamd worker implementation
  18. */
  19. #include "config.h"
  20. #include "libutil/util.h"
  21. #include "libutil/map.h"
  22. #include "libutil/upstream.h"
  23. #include "libserver/protocol.h"
  24. #include "libserver/cfg_file.h"
  25. #include "libserver/url.h"
  26. #include "libserver/dns.h"
  27. #include "libmime/message.h"
  28. #include "rspamd.h"
  29. #include "keypairs_cache.h"
  30. #include "libstat/stat_api.h"
  31. #include "libserver/worker_util.h"
  32. #include "libserver/rspamd_control.h"
  33. #include "worker_private.h"
  34. #include "utlist.h"
  35. #include "libutil/http_private.h"
  36. #include "libmime/lang_detection.h"
  37. #include "unix-std.h"
  38. #include "lua/lua_common.h"
  39. /* 60 seconds for worker's IO */
  40. #define DEFAULT_WORKER_IO_TIMEOUT 60000
  41. /* Timeout for task processing */
  42. #define DEFAULT_TASK_TIMEOUT 8.0
  43. gpointer init_worker (struct rspamd_config *cfg);
  44. void start_worker (struct rspamd_worker *worker);
  45. worker_t normal_worker = {
  46. "normal", /* Name */
  47. init_worker, /* Init function */
  48. start_worker, /* Start function */
  49. RSPAMD_WORKER_HAS_SOCKET|RSPAMD_WORKER_KILLABLE|RSPAMD_WORKER_SCANNER,
  50. RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */
  51. RSPAMD_WORKER_VER /* Version info */
  52. };
  53. #define msg_err_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
  54. "controller", ctx->cfg->cfg_pool->tag.uid, \
  55. G_STRFUNC, \
  56. __VA_ARGS__)
  57. #define msg_warn_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  58. "controller", ctx->cfg->cfg_pool->tag.uid, \
  59. G_STRFUNC, \
  60. __VA_ARGS__)
  61. #define msg_info_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  62. "controller", ctx->cfg->cfg_pool->tag.uid, \
  63. G_STRFUNC, \
  64. __VA_ARGS__)
  65. static gboolean
  66. rspamd_worker_finalize (gpointer user_data)
  67. {
  68. struct rspamd_task *task = user_data;
  69. struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
  70. if (!(task->flags & RSPAMD_TASK_FLAG_PROCESSING)) {
  71. msg_info_task ("finishing actions has been processed, terminating");
  72. event_base_loopexit (task->ev_base, &tv);
  73. rspamd_session_destroy (task->s);
  74. return TRUE;
  75. }
  76. return FALSE;
  77. }
  78. static gboolean
  79. rspamd_worker_call_finish_handlers (struct rspamd_worker *worker)
  80. {
  81. struct rspamd_task *task;
  82. struct rspamd_config *cfg = worker->srv->cfg;
  83. struct rspamd_abstract_worker_ctx *ctx;
  84. struct rspamd_config_post_load_script *sc;
  85. if (cfg->finish_callbacks) {
  86. ctx = worker->ctx;
  87. /* Create a fake task object for async events */
  88. task = rspamd_task_new (worker, cfg, NULL, NULL);
  89. task->resolver = ctx->resolver;
  90. task->ev_base = ctx->ev_base;
  91. task->flags |= RSPAMD_TASK_FLAG_PROCESSING;
  92. task->s = rspamd_session_create (task->task_pool,
  93. rspamd_worker_finalize,
  94. NULL,
  95. (event_finalizer_t) rspamd_task_free,
  96. task);
  97. DL_FOREACH (cfg->finish_callbacks, sc) {
  98. lua_call_finish_script (sc, task);
  99. }
  100. task->flags &= ~RSPAMD_TASK_FLAG_PROCESSING;
  101. if (rspamd_session_pending (task->s)) {
  102. return TRUE;
  103. }
  104. }
  105. return FALSE;
  106. }
  107. /*
  108. * Reduce number of tasks proceeded
  109. */
  110. static void
  111. reduce_tasks_count (gpointer arg)
  112. {
  113. struct rspamd_worker *worker = arg;
  114. worker->nconns --;
  115. if (worker->wanna_die && worker->nconns == 0) {
  116. msg_info ("performing finishing actions");
  117. rspamd_worker_call_finish_handlers (worker);
  118. }
  119. }
  120. void
  121. rspamd_task_timeout (gint fd, short what, gpointer ud)
  122. {
  123. struct rspamd_task *task = (struct rspamd_task *) ud;
  124. if (!(task->processed_stages & RSPAMD_TASK_STAGE_FILTERS)) {
  125. msg_info_task ("processing of task timed out, forced processing");
  126. task->processed_stages |= RSPAMD_TASK_STAGE_FILTERS;
  127. rspamd_session_cleanup (task->s);
  128. rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL);
  129. rspamd_session_pending (task->s);
  130. }
  131. }
  132. void
  133. rspamd_worker_guard_handler (gint fd, short what, void *data)
  134. {
  135. struct rspamd_task *task = data;
  136. gchar fake_buf[1024];
  137. gssize r;
  138. #ifdef EV_CLOSED
  139. if (what == EV_CLOSED) {
  140. if (!(task->flags & RSPAMD_TASK_FLAG_JSON) &&
  141. task->cfg->enable_shutdown_workaround) {
  142. msg_info_task ("workaround for shutdown enabled, please update "
  143. "your client, this support might be removed in future");
  144. shutdown (fd, SHUT_RD);
  145. event_del (task->guard_ev);
  146. task->guard_ev = NULL;
  147. }
  148. else {
  149. msg_err_task ("the peer has closed connection unexpectedly");
  150. rspamd_session_destroy (task->s);
  151. }
  152. return;
  153. }
  154. #endif
  155. r = read (fd, fake_buf, sizeof (fake_buf));
  156. if (r > 0) {
  157. msg_warn_task ("received extra data after task is loaded, ignoring");
  158. }
  159. else {
  160. if (r == 0) {
  161. /*
  162. * Poor man approach, that might break things in case of
  163. * shutdown (SHUT_WR) but sockets are so bad that there's no
  164. * reliable way to distinguish between shutdown(SHUT_WR) and
  165. * close.
  166. */
  167. if (!(task->flags & RSPAMD_TASK_FLAG_JSON) &&
  168. task->cfg->enable_shutdown_workaround) {
  169. msg_info_task ("workaround for shutdown enabled, please update "
  170. "your client, this support might be removed in future");
  171. shutdown (fd, SHUT_RD);
  172. event_del (task->guard_ev);
  173. task->guard_ev = NULL;
  174. }
  175. else {
  176. msg_err_task ("the peer has closed connection unexpectedly");
  177. rspamd_session_destroy (task->s);
  178. }
  179. }
  180. else if (errno != EAGAIN) {
  181. msg_err_task ("the peer has closed connection unexpectedly: %s",
  182. strerror (errno));
  183. rspamd_session_destroy (task->s);
  184. }
  185. else {
  186. return;
  187. }
  188. }
  189. }
  190. static gint
  191. rspamd_worker_body_handler (struct rspamd_http_connection *conn,
  192. struct rspamd_http_message *msg,
  193. const gchar *chunk, gsize len)
  194. {
  195. struct rspamd_task *task = (struct rspamd_task *) conn->ud;
  196. struct rspamd_worker_ctx *ctx;
  197. struct timeval task_tv;
  198. struct event *guard_ev;
  199. ctx = task->worker->ctx;
  200. if (!rspamd_protocol_handle_request (task, msg)) {
  201. msg_err_task ("cannot handle request: %e", task->err);
  202. task->flags |= RSPAMD_TASK_FLAG_SKIP;
  203. }
  204. else {
  205. if (task->cmd == CMD_PING) {
  206. task->flags |= RSPAMD_TASK_FLAG_SKIP;
  207. }
  208. else {
  209. if (!rspamd_task_load_message (task, msg, chunk, len)) {
  210. msg_err_task ("cannot load message: %e", task->err);
  211. task->flags |= RSPAMD_TASK_FLAG_SKIP;
  212. }
  213. }
  214. }
  215. /* Set global timeout for the task */
  216. if (ctx->task_timeout > 0.0) {
  217. event_set (&task->timeout_ev, -1, EV_TIMEOUT, rspamd_task_timeout,
  218. task);
  219. event_base_set (ctx->ev_base, &task->timeout_ev);
  220. double_to_tv (ctx->task_timeout, &task_tv);
  221. event_add (&task->timeout_ev, &task_tv);
  222. }
  223. /* Set socket guard */
  224. guard_ev = rspamd_mempool_alloc (task->task_pool, sizeof (*guard_ev));
  225. #ifdef EV_CLOSED
  226. event_set (guard_ev, task->sock, EV_READ|EV_PERSIST|EV_CLOSED,
  227. rspamd_worker_guard_handler, task);
  228. #else
  229. event_set (guard_ev, task->sock, EV_READ|EV_PERSIST,
  230. rspamd_worker_guard_handler, task);
  231. #endif
  232. event_base_set (task->ev_base, guard_ev);
  233. event_add (guard_ev, NULL);
  234. task->guard_ev = guard_ev;
  235. rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL);
  236. return 0;
  237. }
  238. static void
  239. rspamd_worker_error_handler (struct rspamd_http_connection *conn, GError *err)
  240. {
  241. struct rspamd_task *task = (struct rspamd_task *) conn->ud;
  242. struct rspamd_http_message *msg;
  243. rspamd_fstring_t *reply;
  244. msg_info_task ("abnormally closing connection from: %s, error: %e",
  245. rspamd_inet_address_to_string (task->client_addr), err);
  246. if (task->processed_stages & RSPAMD_TASK_STAGE_REPLIED) {
  247. /* Terminate session immediately */
  248. rspamd_session_destroy (task->s);
  249. }
  250. else {
  251. task->processed_stages |= RSPAMD_TASK_STAGE_REPLIED;
  252. msg = rspamd_http_new_message (HTTP_RESPONSE);
  253. if (err) {
  254. msg->status = rspamd_fstring_new_init (err->message,
  255. strlen (err->message));
  256. msg->code = err->code;
  257. }
  258. else {
  259. msg->status = rspamd_fstring_new_init ("Internal error",
  260. strlen ("Internal error"));
  261. msg->code = 500;
  262. }
  263. msg->date = time (NULL);
  264. reply = rspamd_fstring_sized_new (msg->status->len + 16);
  265. rspamd_printf_fstring (&reply, "{\"error\":\"%V\"}", msg->status);
  266. rspamd_http_message_set_body_from_fstring_steal (msg, reply);
  267. rspamd_http_connection_reset (task->http_conn);
  268. rspamd_http_connection_write_message (task->http_conn,
  269. msg,
  270. NULL,
  271. "application/json",
  272. task,
  273. task->http_conn->fd,
  274. &task->tv,
  275. task->ev_base);
  276. }
  277. }
  278. static gint
  279. rspamd_worker_finish_handler (struct rspamd_http_connection *conn,
  280. struct rspamd_http_message *msg)
  281. {
  282. struct rspamd_task *task = (struct rspamd_task *) conn->ud;
  283. if (task->processed_stages & RSPAMD_TASK_STAGE_REPLIED) {
  284. /* We are done here */
  285. msg_debug_task ("normally closing connection from: %s",
  286. rspamd_inet_address_to_string (task->client_addr));
  287. rspamd_session_destroy (task->s);
  288. }
  289. else if (task->processed_stages & RSPAMD_TASK_STAGE_DONE) {
  290. rspamd_session_pending (task->s);
  291. }
  292. return 0;
  293. }
  294. /*
  295. * Accept new connection and construct task
  296. */
  297. static void
  298. accept_socket (gint fd, short what, void *arg)
  299. {
  300. struct rspamd_worker *worker = (struct rspamd_worker *) arg;
  301. struct rspamd_worker_ctx *ctx;
  302. struct rspamd_task *task;
  303. rspamd_inet_addr_t *addr;
  304. gint nfd;
  305. ctx = worker->ctx;
  306. if (ctx->max_tasks != 0 && worker->nconns > ctx->max_tasks) {
  307. msg_info_ctx ("current tasks is now: %uD while maximum is: %uD",
  308. worker->nconns,
  309. ctx->max_tasks);
  310. return;
  311. }
  312. if ((nfd =
  313. rspamd_accept_from_socket (fd, &addr, worker->accept_events)) == -1) {
  314. msg_warn_ctx ("accept failed: %s", strerror (errno));
  315. return;
  316. }
  317. /* Check for EAGAIN */
  318. if (nfd == 0) {
  319. return;
  320. }
  321. task = rspamd_task_new (worker, ctx->cfg, NULL, ctx->lang_det);
  322. msg_info_task ("accepted connection from %s port %d, task ptr: %p",
  323. rspamd_inet_address_to_string (addr),
  324. rspamd_inet_address_get_port (addr),
  325. task);
  326. /* Copy some variables */
  327. if (ctx->is_mime) {
  328. task->flags |= RSPAMD_TASK_FLAG_MIME;
  329. }
  330. else {
  331. task->flags &= ~RSPAMD_TASK_FLAG_MIME;
  332. }
  333. task->sock = nfd;
  334. task->client_addr = addr;
  335. worker->srv->stat->connections_count++;
  336. task->resolver = ctx->resolver;
  337. /* TODO: allow to disable autolearn in protocol */
  338. task->flags |= RSPAMD_TASK_FLAG_LEARN_AUTO;
  339. task->http_conn = rspamd_http_connection_new (rspamd_worker_body_handler,
  340. rspamd_worker_error_handler,
  341. rspamd_worker_finish_handler,
  342. 0,
  343. RSPAMD_HTTP_SERVER,
  344. ctx->keys_cache,
  345. NULL);
  346. rspamd_http_connection_set_max_size (task->http_conn, task->cfg->max_message);
  347. task->ev_base = ctx->ev_base;
  348. worker->nconns++;
  349. rspamd_mempool_add_destructor (task->task_pool,
  350. (rspamd_mempool_destruct_t)reduce_tasks_count, worker);
  351. /* Set up async session */
  352. task->s = rspamd_session_create (task->task_pool, rspamd_task_fin,
  353. rspamd_task_restore, (event_finalizer_t )rspamd_task_free, task);
  354. if (ctx->key) {
  355. rspamd_http_connection_set_key (task->http_conn, ctx->key);
  356. }
  357. rspamd_http_connection_read_message (task->http_conn,
  358. task,
  359. nfd,
  360. &ctx->io_tv,
  361. ctx->ev_base);
  362. }
  363. #ifdef WITH_HYPERSCAN
  364. static gboolean
  365. rspamd_worker_hyperscan_ready (struct rspamd_main *rspamd_main,
  366. struct rspamd_worker *worker, gint fd,
  367. gint attached_fd,
  368. struct rspamd_control_command *cmd,
  369. gpointer ud)
  370. {
  371. struct rspamd_control_reply rep;
  372. struct rspamd_re_cache *cache = worker->srv->cfg->re_cache;
  373. memset (&rep, 0, sizeof (rep));
  374. rep.type = RSPAMD_CONTROL_HYPERSCAN_LOADED;
  375. if (!rspamd_re_cache_is_hs_loaded (cache) || cmd->cmd.hs_loaded.forced) {
  376. msg_info ("loading hyperscan expressions after receiving compilation "
  377. "notice: %s",
  378. (!rspamd_re_cache_is_hs_loaded (cache)) ?
  379. "new db" : "forced update");
  380. rep.reply.hs_loaded.status = rspamd_re_cache_load_hyperscan (
  381. worker->srv->cfg->re_cache, cmd->cmd.hs_loaded.cache_dir);
  382. }
  383. if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) {
  384. msg_err ("cannot write reply to the control socket: %s",
  385. strerror (errno));
  386. }
  387. return TRUE;
  388. }
  389. #endif
  390. static gboolean
  391. rspamd_worker_log_pipe_handler (struct rspamd_main *rspamd_main,
  392. struct rspamd_worker *worker, gint fd,
  393. gint attached_fd,
  394. struct rspamd_control_command *cmd,
  395. gpointer ud)
  396. {
  397. struct rspamd_config *cfg = ud;
  398. struct rspamd_worker_log_pipe *lp;
  399. struct rspamd_control_reply rep;
  400. memset (&rep, 0, sizeof (rep));
  401. rep.type = RSPAMD_CONTROL_LOG_PIPE;
  402. if (attached_fd != -1) {
  403. lp = g_malloc0 (sizeof (*lp));
  404. lp->fd = attached_fd;
  405. lp->type = cmd->cmd.log_pipe.type;
  406. DL_APPEND (cfg->log_pipes, lp);
  407. msg_info ("added new log pipe");
  408. }
  409. else {
  410. rep.reply.log_pipe.status = ENOENT;
  411. msg_err ("cannot attach log pipe: invalid fd");
  412. }
  413. if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) {
  414. msg_err ("cannot write reply to the control socket: %s",
  415. strerror (errno));
  416. }
  417. return TRUE;
  418. }
  419. static gboolean
  420. rspamd_worker_monitored_handler (struct rspamd_main *rspamd_main,
  421. struct rspamd_worker *worker, gint fd,
  422. gint attached_fd,
  423. struct rspamd_control_command *cmd,
  424. gpointer ud)
  425. {
  426. struct rspamd_control_reply rep;
  427. struct rspamd_monitored *m;
  428. struct rspamd_monitored_ctx *mctx = worker->srv->cfg->monitored_ctx;
  429. struct rspamd_config *cfg = ud;
  430. memset (&rep, 0, sizeof (rep));
  431. rep.type = RSPAMD_CONTROL_MONITORED_CHANGE;
  432. if (cmd->cmd.monitored_change.sender != getpid ()) {
  433. m = rspamd_monitored_by_tag (mctx, cmd->cmd.monitored_change.tag);
  434. if (m != NULL) {
  435. rspamd_monitored_set_alive (m, cmd->cmd.monitored_change.alive);
  436. rep.reply.monitored_change.status = 1;
  437. msg_info_config ("updated monitored status for %s: %s",
  438. cmd->cmd.monitored_change.tag,
  439. cmd->cmd.monitored_change.alive ? "alive" : "dead");
  440. } else {
  441. msg_err ("cannot find monitored by tag: %*s", 32,
  442. cmd->cmd.monitored_change.tag);
  443. rep.reply.monitored_change.status = 0;
  444. }
  445. }
  446. if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) {
  447. msg_err ("cannot write reply to the control socket: %s",
  448. strerror (errno));
  449. }
  450. return TRUE;
  451. }
  452. gpointer
  453. init_worker (struct rspamd_config *cfg)
  454. {
  455. struct rspamd_worker_ctx *ctx;
  456. GQuark type;
  457. type = g_quark_try_string ("normal");
  458. ctx = rspamd_mempool_alloc0 (cfg->cfg_pool,
  459. sizeof (struct rspamd_worker_ctx));
  460. ctx->magic = rspamd_worker_magic;
  461. ctx->is_mime = TRUE;
  462. ctx->timeout = DEFAULT_WORKER_IO_TIMEOUT;
  463. ctx->cfg = cfg;
  464. ctx->task_timeout = DEFAULT_TASK_TIMEOUT;
  465. rspamd_rcl_register_worker_option (cfg,
  466. type,
  467. "mime",
  468. rspamd_rcl_parse_struct_boolean,
  469. ctx,
  470. G_STRUCT_OFFSET (struct rspamd_worker_ctx, is_mime),
  471. 0,
  472. "Set to `false` if this worker is intended to work with non-MIME messages");
  473. rspamd_rcl_register_worker_option (cfg,
  474. type,
  475. "http",
  476. rspamd_rcl_parse_struct_boolean,
  477. ctx,
  478. G_STRUCT_OFFSET (struct rspamd_worker_ctx, is_http),
  479. 0,
  480. "Deprecated: always true now");
  481. rspamd_rcl_register_worker_option (cfg,
  482. type,
  483. "json",
  484. rspamd_rcl_parse_struct_boolean,
  485. ctx,
  486. G_STRUCT_OFFSET (struct rspamd_worker_ctx, is_json),
  487. 0,
  488. "Deprecated: always true now");
  489. rspamd_rcl_register_worker_option (cfg,
  490. type,
  491. "allow_learn",
  492. rspamd_rcl_parse_struct_boolean,
  493. ctx,
  494. G_STRUCT_OFFSET (struct rspamd_worker_ctx, allow_learn),
  495. 0,
  496. "Deprecated: disabled and forgotten");
  497. rspamd_rcl_register_worker_option (cfg,
  498. type,
  499. "timeout",
  500. rspamd_rcl_parse_struct_time,
  501. ctx,
  502. G_STRUCT_OFFSET (struct rspamd_worker_ctx,
  503. timeout),
  504. RSPAMD_CL_FLAG_TIME_INTEGER,
  505. "Protocol IO timeout");
  506. rspamd_rcl_register_worker_option (cfg,
  507. type,
  508. "task_timeout",
  509. rspamd_rcl_parse_struct_time,
  510. ctx,
  511. G_STRUCT_OFFSET (struct rspamd_worker_ctx,
  512. task_timeout),
  513. RSPAMD_CL_FLAG_TIME_FLOAT,
  514. "Maximum task processing time, default: "
  515. G_STRINGIFY(DEFAULT_TASK_TIMEOUT)
  516. " seconds");
  517. rspamd_rcl_register_worker_option (cfg,
  518. type,
  519. "max_tasks",
  520. rspamd_rcl_parse_struct_integer,
  521. ctx,
  522. G_STRUCT_OFFSET (struct rspamd_worker_ctx,
  523. max_tasks),
  524. RSPAMD_CL_FLAG_INT_32,
  525. "Maximum count of parallel tasks processed by a single worker process");
  526. rspamd_rcl_register_worker_option (cfg,
  527. type,
  528. "keypair",
  529. rspamd_rcl_parse_struct_keypair,
  530. ctx,
  531. G_STRUCT_OFFSET (struct rspamd_worker_ctx,
  532. key),
  533. 0,
  534. "Encryption keypair");
  535. return ctx;
  536. }
  537. static gboolean
  538. rspamd_worker_on_terminate (struct rspamd_worker *worker)
  539. {
  540. if (worker->nconns == 0) {
  541. msg_info ("performing finishing actions");
  542. if (rspamd_worker_call_finish_handlers (worker)) {
  543. return TRUE;
  544. }
  545. }
  546. return FALSE;
  547. }
  548. void
  549. rspamd_worker_init_scanner (struct rspamd_worker *worker,
  550. struct event_base *ev_base,
  551. struct rspamd_dns_resolver *resolver,
  552. struct rspamd_lang_detector **plang_det)
  553. {
  554. rspamd_stat_init (worker->srv->cfg, ev_base);
  555. g_ptr_array_add (worker->finish_actions,
  556. (gpointer) rspamd_worker_on_terminate);
  557. #ifdef WITH_HYPERSCAN
  558. rspamd_control_worker_add_cmd_handler (worker,
  559. RSPAMD_CONTROL_HYPERSCAN_LOADED,
  560. rspamd_worker_hyperscan_ready,
  561. NULL);
  562. #endif
  563. rspamd_control_worker_add_cmd_handler (worker,
  564. RSPAMD_CONTROL_LOG_PIPE,
  565. rspamd_worker_log_pipe_handler,
  566. worker->srv->cfg);
  567. rspamd_control_worker_add_cmd_handler (worker,
  568. RSPAMD_CONTROL_MONITORED_CHANGE,
  569. rspamd_worker_monitored_handler,
  570. worker->srv->cfg);
  571. *plang_det = worker->srv->cfg->lang_det;
  572. }
  573. /*
  574. * Start worker process
  575. */
  576. void
  577. start_worker (struct rspamd_worker *worker)
  578. {
  579. struct rspamd_worker_ctx *ctx = worker->ctx;
  580. ctx->cfg = worker->srv->cfg;
  581. ctx->ev_base = rspamd_prepare_worker (worker, "normal", accept_socket);
  582. msec_to_tv (ctx->timeout, &ctx->io_tv);
  583. rspamd_symbols_cache_start_refresh (worker->srv->cfg->cache, ctx->ev_base,
  584. worker);
  585. ctx->resolver = dns_resolver_init (worker->srv->logger,
  586. ctx->ev_base,
  587. worker->srv->cfg);
  588. rspamd_map_watch (worker->srv->cfg, ctx->ev_base, ctx->resolver, worker, 0);
  589. rspamd_upstreams_library_config (worker->srv->cfg, ctx->cfg->ups_ctx,
  590. ctx->ev_base, ctx->resolver->r);
  591. /* XXX: stupid default */
  592. ctx->keys_cache = rspamd_keypair_cache_new (256);
  593. rspamd_worker_init_scanner (worker, ctx->ev_base, ctx->resolver,
  594. &ctx->lang_det);
  595. rspamd_lua_run_postloads (ctx->cfg->lua_state, ctx->cfg, ctx->ev_base,
  596. worker);
  597. event_base_loop (ctx->ev_base, 0);
  598. rspamd_worker_block_signals ();
  599. rspamd_stat_close ();
  600. rspamd_keypair_cache_destroy (ctx->keys_cache);
  601. REF_RELEASE (ctx->cfg);
  602. rspamd_log_close (worker->srv->logger, TRUE);
  603. exit (EXIT_SUCCESS);
  604. }