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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 privilleged;
  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 a custom string using HTTP
  105. * @param entry router entry
  106. * @param str string to send
  107. */
  108. void rspamd_controller_send_string (struct rspamd_http_connection_entry *entry,
  109. const gchar *str);
  110. /**
  111. * Send UCL using HTTP and JSON serialization
  112. * @param entry router entry
  113. * @param obj object to send
  114. */
  115. void rspamd_controller_send_ucl (struct rspamd_http_connection_entry *entry,
  116. ucl_object_t *obj);
  117. /**
  118. * Return worker's control structure by its type
  119. * @param type
  120. * @return worker's control structure or NULL
  121. */
  122. worker_t *rspamd_get_worker_by_type (struct rspamd_config *cfg, GQuark type);
  123. /**
  124. * Block signals before terminations
  125. */
  126. void rspamd_worker_block_signals (void);
  127. /**
  128. * Unblock signals
  129. */
  130. void rspamd_worker_unblock_signals (void);
  131. /**
  132. * Kill rspamd main and all workers
  133. * @param rspamd_main
  134. */
  135. void rspamd_hard_terminate (struct rspamd_main *rspamd_main) G_GNUC_NORETURN;
  136. /**
  137. * Returns TRUE if a specific worker is a scanner worker
  138. * @param w
  139. * @return
  140. */
  141. gboolean rspamd_worker_is_scanner (struct rspamd_worker *w);
  142. /**
  143. * Returns TRUE if a specific worker is a primary controller
  144. * @param w
  145. * @return
  146. */
  147. gboolean rspamd_worker_is_primary_controller (struct rspamd_worker *w);
  148. /**
  149. * Creates new session cache
  150. * @param w
  151. * @return
  152. */
  153. void *rspamd_worker_session_cache_new (struct rspamd_worker *w,
  154. struct ev_loop *ev_base);
  155. /**
  156. * Adds a new session identified by pointer
  157. * @param cache
  158. * @param tag
  159. * @param pref
  160. * @param ptr
  161. */
  162. void rspamd_worker_session_cache_add (void *cache, const gchar *tag,
  163. guint *pref, void *ptr);
  164. /**
  165. * Removes session from cache
  166. * @param cache
  167. * @param ptr
  168. */
  169. void rspamd_worker_session_cache_remove (void *cache, void *ptr);
  170. /**
  171. * Fork new worker with the specified configuration
  172. */
  173. struct rspamd_worker *rspamd_fork_worker (struct rspamd_main *,
  174. struct rspamd_worker_conf *, guint idx,
  175. struct ev_loop *ev_base,
  176. rspamd_worker_term_cb term_handler);
  177. /**
  178. * Sets crash signals handlers if compiled with libunwind
  179. */
  180. void rspamd_set_crash_handler (struct rspamd_main *);
  181. /**
  182. * Initialise the main monitoring worker
  183. * @param worker
  184. * @param ev_base
  185. * @param resolver
  186. */
  187. void rspamd_worker_init_monitored (struct rspamd_worker *worker,
  188. struct ev_loop *ev_base,
  189. struct rspamd_dns_resolver *resolver);
  190. /**
  191. * Performs throttling for accept events
  192. * @param sock
  193. * @param data struct rspamd_worker_accept_event * list
  194. */
  195. void rspamd_worker_throttle_accept_events (gint sock, void *data);
  196. /**
  197. * Checks (and logs) the worker's termination status. Returns TRUE if a worker
  198. * should be restarted.
  199. * @param rspamd_main
  200. * @param wrk
  201. * @param status waitpid res
  202. * @return TRUE if refork is desired
  203. */
  204. gboolean rspamd_check_termination_clause (struct rspamd_main *rspamd_main,
  205. struct rspamd_worker *wrk, int status);
  206. /**
  207. * Call for final scripts for a worker
  208. * @param worker
  209. * @return
  210. */
  211. gboolean rspamd_worker_call_finish_handlers (struct rspamd_worker *worker);
  212. struct rspamd_rrd_file;
  213. /**
  214. * Terminate controller worker
  215. * @param worker
  216. */
  217. void rspamd_controller_on_terminate (struct rspamd_worker *worker,
  218. struct rspamd_rrd_file *rrd);
  219. /**
  220. * Inits controller worker
  221. * @param worker
  222. * @param ev_base
  223. * @param prrd
  224. */
  225. void rspamd_worker_init_controller (struct rspamd_worker *worker,
  226. struct rspamd_rrd_file **prrd);
  227. /**
  228. * Saves stats
  229. * @param rspamd_main
  230. * @param cfg
  231. */
  232. void rspamd_controller_store_saved_stats (struct rspamd_main *rspamd_main,
  233. struct rspamd_config *cfg);
  234. #ifdef WITH_HYPERSCAN
  235. struct rspamd_control_command;
  236. gboolean rspamd_worker_hyperscan_ready (struct rspamd_main *rspamd_main,
  237. struct rspamd_worker *worker, gint fd,
  238. gint attached_fd,
  239. struct rspamd_control_command *cmd,
  240. gpointer ud);
  241. #endif
  242. #define msg_err_main(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
  243. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  244. G_STRFUNC, \
  245. __VA_ARGS__)
  246. #define msg_warn_main(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  247. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  248. G_STRFUNC, \
  249. __VA_ARGS__)
  250. #define msg_info_main(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  251. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  252. G_STRFUNC, \
  253. __VA_ARGS__)
  254. #ifdef __cplusplus
  255. }
  256. #endif
  257. #endif /* WORKER_UTIL_H_ */