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_util.h 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #ifndef WORKER_UTIL_H_
  17. #define WORKER_UTIL_H_
  18. #include "config.h"
  19. #include "util.h"
  20. #include "libserver/http/http_connection.h"
  21. #include "rspamd.h"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #ifndef HAVE_SA_SIGINFO
  26. typedef void (*rspamd_sig_handler_t) (gint);
  27. #else
  28. typedef void (*rspamd_sig_handler_t) (gint, siginfo_t *, void *);
  29. #endif
  30. struct rspamd_worker;
  31. struct rspamd_worker_signal_handler;
  32. /**
  33. * Init basic signals for a worker
  34. * @param worker
  35. * @param event_loop
  36. */
  37. void rspamd_worker_init_signals (struct rspamd_worker *worker, struct ev_loop *event_loop);
  38. typedef void (*rspamd_accept_handler) (struct ev_loop *loop, ev_io *w, int revents);
  39. /**
  40. * Prepare worker's startup
  41. * @param worker worker structure
  42. * @param name name of the worker
  43. * @param sig_handler handler of main signals
  44. * @param accept_handler handler of accept event for listen sockets
  45. * @return event base suitable for a worker
  46. */
  47. struct ev_loop *
  48. rspamd_prepare_worker (struct rspamd_worker *worker, const char *name,
  49. rspamd_accept_handler hdl);
  50. /**
  51. * Should be used to validate context for a worker as in assert like invocation
  52. * @param ctx
  53. * @param magic
  54. * @return
  55. */
  56. gboolean rspamd_worker_check_context (gpointer ctx, guint64 magic);
  57. /**
  58. * Set special signal handler for a worker
  59. */
  60. void rspamd_worker_set_signal_handler (int signo,
  61. struct rspamd_worker *worker,
  62. struct ev_loop *event_loop,
  63. rspamd_worker_signal_cb_t handler,
  64. void *handler_data);
  65. /**
  66. * Stop accepting new connections for a worker
  67. * @param worker
  68. */
  69. void rspamd_worker_stop_accept (struct rspamd_worker *worker);
  70. typedef gint (*rspamd_controller_func_t) (
  71. struct rspamd_http_connection_entry *conn_ent,
  72. struct rspamd_http_message *msg,
  73. struct module_ctx *ctx);
  74. struct rspamd_custom_controller_command {
  75. const gchar *command;
  76. struct module_ctx *ctx;
  77. gboolean privileged;
  78. gboolean require_message;
  79. rspamd_controller_func_t handler;
  80. };
  81. struct rspamd_controller_worker_ctx;
  82. struct rspamd_lang_detector;
  83. struct rspamd_controller_session {
  84. struct rspamd_controller_worker_ctx *ctx;
  85. struct rspamd_worker *wrk;
  86. rspamd_mempool_t *pool;
  87. struct rspamd_task *task;
  88. gchar *classifier;
  89. rspamd_inet_addr_t *from_addr;
  90. struct rspamd_config *cfg;
  91. struct rspamd_lang_detector *lang_det;
  92. gboolean is_spam;
  93. gboolean is_enable;
  94. };
  95. /**
  96. * Send error using HTTP and JSON output
  97. * @param entry router entry
  98. * @param code error code
  99. * @param error_msg error message
  100. */
  101. void rspamd_controller_send_error (struct rspamd_http_connection_entry *entry,
  102. gint code, const gchar *error_msg, ...);
  103. /**
  104. * Send openmetrics-formatted strings using HTTP
  105. * @param entry router entry
  106. * @param str rspamd fstring buffer, ownership is transferred
  107. */
  108. void
  109. rspamd_controller_send_openmetrics (struct rspamd_http_connection_entry *entry,
  110. rspamd_fstring_t *str);
  111. /**
  112. * Send a custom string using HTTP
  113. * @param entry router entry
  114. * @param str string to send
  115. */
  116. void rspamd_controller_send_string (struct rspamd_http_connection_entry *entry,
  117. const gchar *str);
  118. /**
  119. * Send UCL using HTTP and JSON serialization
  120. * @param entry router entry
  121. * @param obj object to send
  122. */
  123. void rspamd_controller_send_ucl (struct rspamd_http_connection_entry *entry,
  124. ucl_object_t *obj);
  125. /**
  126. * Return worker's control structure by its type
  127. * @param type
  128. * @return worker's control structure or NULL
  129. */
  130. worker_t *rspamd_get_worker_by_type (struct rspamd_config *cfg, GQuark type);
  131. /**
  132. * Block signals before terminations
  133. */
  134. void rspamd_worker_block_signals (void);
  135. /**
  136. * Unblock signals
  137. */
  138. void rspamd_worker_unblock_signals (void);
  139. /**
  140. * Kill rspamd main and all workers
  141. * @param rspamd_main
  142. */
  143. void rspamd_hard_terminate (struct rspamd_main *rspamd_main) G_GNUC_NORETURN;
  144. /**
  145. * Returns TRUE if a specific worker is a scanner worker
  146. * @param w
  147. * @return
  148. */
  149. gboolean rspamd_worker_is_scanner (struct rspamd_worker *w);
  150. /**
  151. * Returns TRUE if a specific worker is a primary controller
  152. * @param w
  153. * @return
  154. */
  155. gboolean rspamd_worker_is_primary_controller (struct rspamd_worker *w);
  156. /**
  157. * Creates new session cache
  158. * @param w
  159. * @return
  160. */
  161. void *rspamd_worker_session_cache_new (struct rspamd_worker *w,
  162. struct ev_loop *ev_base);
  163. /**
  164. * Adds a new session identified by pointer
  165. * @param cache
  166. * @param tag
  167. * @param pref
  168. * @param ptr
  169. */
  170. void rspamd_worker_session_cache_add (void *cache, const gchar *tag,
  171. guint *pref, void *ptr);
  172. /**
  173. * Removes session from cache
  174. * @param cache
  175. * @param ptr
  176. */
  177. void rspamd_worker_session_cache_remove (void *cache, void *ptr);
  178. /**
  179. * Fork new worker with the specified configuration
  180. */
  181. struct rspamd_worker *rspamd_fork_worker (struct rspamd_main *,
  182. struct rspamd_worker_conf *, guint idx,
  183. struct ev_loop *ev_base,
  184. rspamd_worker_term_cb term_handler,
  185. GHashTable *listen_sockets);
  186. /**
  187. * Sets crash signals handlers if compiled with libunwind
  188. */
  189. RSPAMD_NO_SANITIZE void rspamd_set_crash_handler (struct rspamd_main *);
  190. /**
  191. * Initialise the main monitoring worker
  192. * @param worker
  193. * @param ev_base
  194. * @param resolver
  195. */
  196. void rspamd_worker_init_monitored (struct rspamd_worker *worker,
  197. struct ev_loop *ev_base,
  198. struct rspamd_dns_resolver *resolver);
  199. /**
  200. * Performs throttling for accept events
  201. * @param sock
  202. * @param data struct rspamd_worker_accept_event * list
  203. */
  204. void rspamd_worker_throttle_accept_events (gint sock, void *data);
  205. /**
  206. * Checks (and logs) the worker's termination status. Returns TRUE if a worker
  207. * should be restarted.
  208. * @param rspamd_main
  209. * @param wrk
  210. * @param status waitpid res
  211. * @return TRUE if refork is desired
  212. */
  213. gboolean rspamd_check_termination_clause (struct rspamd_main *rspamd_main,
  214. struct rspamd_worker *wrk, int status);
  215. /**
  216. * Call for final scripts for a worker
  217. * @param worker
  218. * @return
  219. */
  220. gboolean rspamd_worker_call_finish_handlers (struct rspamd_worker *worker);
  221. struct rspamd_rrd_file;
  222. /**
  223. * Terminate controller worker
  224. * @param worker
  225. */
  226. void rspamd_controller_on_terminate (struct rspamd_worker *worker,
  227. struct rspamd_rrd_file *rrd);
  228. /**
  229. * Inits controller worker
  230. * @param worker
  231. * @param ev_base
  232. * @param prrd
  233. */
  234. void rspamd_worker_init_controller (struct rspamd_worker *worker,
  235. struct rspamd_rrd_file **prrd);
  236. /**
  237. * Saves stats
  238. * @param rspamd_main
  239. * @param cfg
  240. */
  241. void rspamd_controller_store_saved_stats (struct rspamd_main *rspamd_main,
  242. struct rspamd_config *cfg);
  243. #ifdef WITH_HYPERSCAN
  244. struct rspamd_control_command;
  245. gboolean rspamd_worker_hyperscan_ready (struct rspamd_main *rspamd_main,
  246. struct rspamd_worker *worker, gint fd,
  247. gint attached_fd,
  248. struct rspamd_control_command *cmd,
  249. gpointer ud);
  250. #endif
  251. #define msg_err_main(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
  252. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  253. RSPAMD_LOG_FUNC, \
  254. __VA_ARGS__)
  255. #define msg_warn_main(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  256. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  257. RSPAMD_LOG_FUNC, \
  258. __VA_ARGS__)
  259. #define msg_notice_main(...) rspamd_default_log_function (G_LOG_LEVEL_MESSAGE, \
  260. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  261. RSPAMD_LOG_FUNC, \
  262. __VA_ARGS__)
  263. #define msg_info_main(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  264. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  265. RSPAMD_LOG_FUNC, \
  266. __VA_ARGS__)
  267. #ifdef __cplusplus
  268. }
  269. #endif
  270. #endif /* WORKER_UTIL_H_ */