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

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