Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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