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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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)(int);
  27. #else
  28. typedef void (*rspamd_sig_handler_t)(int, siginfo_t *, void *);
  29. #endif
  30. struct rspamd_worker;
  31. struct rspamd_worker_signal_handler;
  32. extern struct rspamd_worker *rspamd_current_worker;
  33. /**
  34. * Init basic signals for a worker
  35. * @param worker
  36. * @param event_loop
  37. */
  38. void rspamd_worker_init_signals(struct rspamd_worker *worker, struct ev_loop *event_loop);
  39. typedef void (*rspamd_accept_handler)(struct ev_loop *loop, ev_io *w, int revents);
  40. /**
  41. * Prepare worker's startup
  42. * @param worker worker structure
  43. * @param name name of the worker
  44. * @param sig_handler handler of main signals
  45. * @param accept_handler handler of accept event for listen sockets
  46. * @return event base suitable for a worker
  47. */
  48. struct ev_loop *
  49. rspamd_prepare_worker(struct rspamd_worker *worker, const char *name,
  50. rspamd_accept_handler hdl);
  51. /**
  52. * Should be used to validate context for a worker as in assert like invocation
  53. * @param ctx
  54. * @param magic
  55. * @return
  56. */
  57. gboolean rspamd_worker_check_context(gpointer ctx, uint64_t magic);
  58. /**
  59. * Set special signal handler for a worker
  60. */
  61. void rspamd_worker_set_signal_handler(int signo,
  62. struct rspamd_worker *worker,
  63. struct ev_loop *event_loop,
  64. rspamd_worker_signal_cb_t handler,
  65. void *handler_data);
  66. /**
  67. * Stop accepting new connections for a worker
  68. * @param worker
  69. */
  70. void rspamd_worker_stop_accept(struct rspamd_worker *worker);
  71. typedef int (*rspamd_controller_func_t)(
  72. struct rspamd_http_connection_entry *conn_ent,
  73. struct rspamd_http_message *msg,
  74. struct module_ctx *ctx);
  75. struct rspamd_custom_controller_command {
  76. const char *command;
  77. struct module_ctx *ctx;
  78. gboolean privileged;
  79. gboolean require_message;
  80. rspamd_controller_func_t handler;
  81. };
  82. struct rspamd_controller_worker_ctx;
  83. struct rspamd_lang_detector;
  84. struct rspamd_controller_session {
  85. struct rspamd_controller_worker_ctx *ctx;
  86. struct rspamd_worker *wrk;
  87. rspamd_mempool_t *pool;
  88. struct rspamd_task *task;
  89. char *classifier;
  90. rspamd_inet_addr_t *from_addr;
  91. struct rspamd_config *cfg;
  92. struct rspamd_lang_detector *lang_det;
  93. gboolean is_spam;
  94. gboolean is_read_only;
  95. };
  96. /**
  97. * Send error using HTTP and JSON output
  98. * @param entry router entry
  99. * @param code error code
  100. * @param error_msg error message
  101. */
  102. void rspamd_controller_send_error(struct rspamd_http_connection_entry *entry,
  103. int code, const char *error_msg, ...);
  104. /**
  105. * Send openmetrics-formatted strings using HTTP
  106. * @param entry router entry
  107. * @param str rspamd fstring buffer, ownership is transferred
  108. */
  109. void 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 char *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. * Checks
  152. * @param cfg
  153. * @param timeout
  154. * @return
  155. */
  156. double rspamd_worker_check_and_adjust_timeout(struct rspamd_config *cfg,
  157. double timeout);
  158. /**
  159. * Returns TRUE if a specific worker is a primary controller
  160. * @param w
  161. * @return
  162. */
  163. gboolean rspamd_worker_is_primary_controller(struct rspamd_worker *w);
  164. /**
  165. * Returns TRUE if a specific worker should take a role of a controller
  166. */
  167. gboolean rspamd_worker_check_controller_presence(struct rspamd_worker *w);
  168. /**
  169. * Creates new session cache
  170. * @param w
  171. * @return
  172. */
  173. void *rspamd_worker_session_cache_new(struct rspamd_worker *w,
  174. struct ev_loop *ev_base);
  175. /**
  176. * Adds a new session identified by pointer
  177. * @param cache
  178. * @param tag
  179. * @param pref
  180. * @param ptr
  181. */
  182. void rspamd_worker_session_cache_add(void *cache, const char *tag,
  183. unsigned int *pref, void *ptr);
  184. /**
  185. * Removes session from cache
  186. * @param cache
  187. * @param ptr
  188. */
  189. void rspamd_worker_session_cache_remove(void *cache, void *ptr);
  190. /**
  191. * Fork new worker with the specified configuration
  192. */
  193. struct rspamd_worker *rspamd_fork_worker(struct rspamd_main *,
  194. struct rspamd_worker_conf *, unsigned int idx,
  195. struct ev_loop *ev_base,
  196. rspamd_worker_term_cb term_handler,
  197. GHashTable *listen_sockets);
  198. /**
  199. * Sets crash signals handlers if compiled with libunwind
  200. */
  201. RSPAMD_NO_SANITIZE void rspamd_set_crash_handler(struct rspamd_main *);
  202. /**
  203. * Restore memory for crash signals
  204. */
  205. RSPAMD_NO_SANITIZE void rspamd_unset_crash_handler(struct rspamd_main *);
  206. /**
  207. * Initialise the main monitoring worker
  208. * @param worker
  209. * @param ev_base
  210. * @param resolver
  211. */
  212. void rspamd_worker_init_monitored(struct rspamd_worker *worker,
  213. struct ev_loop *ev_base,
  214. struct rspamd_dns_resolver *resolver);
  215. /**
  216. * Performs throttling for accept events
  217. * @param sock
  218. * @param data struct rspamd_worker_accept_event * list
  219. */
  220. void rspamd_worker_throttle_accept_events(int sock, void *data);
  221. /**
  222. * Checks (and logs) the worker's termination status. Returns TRUE if a worker
  223. * should be restarted.
  224. * @param rspamd_main
  225. * @param wrk
  226. * @param status waitpid res
  227. * @return TRUE if refork is desired
  228. */
  229. gboolean rspamd_check_termination_clause(struct rspamd_main *rspamd_main,
  230. struct rspamd_worker *wrk, int status);
  231. /**
  232. * Call for final scripts for a worker
  233. * @param worker
  234. * @return
  235. */
  236. gboolean rspamd_worker_call_finish_handlers(struct rspamd_worker *worker);
  237. struct rspamd_rrd_file;
  238. /**
  239. * Terminate controller worker
  240. * @param worker
  241. */
  242. void rspamd_controller_on_terminate(struct rspamd_worker *worker,
  243. struct rspamd_rrd_file *rrd);
  244. /**
  245. * Inits controller worker
  246. * @param worker
  247. * @param ev_base
  248. * @param prrd
  249. */
  250. void rspamd_worker_init_controller(struct rspamd_worker *worker,
  251. struct rspamd_rrd_file **prrd);
  252. /**
  253. * Saves stats
  254. * @param rspamd_main
  255. * @param cfg
  256. */
  257. void rspamd_controller_store_saved_stats(struct rspamd_main *rspamd_main,
  258. struct rspamd_config *cfg);
  259. #ifdef WITH_HYPERSCAN
  260. struct rspamd_control_command;
  261. gboolean rspamd_worker_hyperscan_ready(struct rspamd_main *rspamd_main,
  262. struct rspamd_worker *worker, int fd,
  263. int attached_fd,
  264. struct rspamd_control_command *cmd,
  265. gpointer ud);
  266. #endif
  267. #define msg_err_main(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
  268. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  269. RSPAMD_LOG_FUNC, \
  270. __VA_ARGS__)
  271. #define msg_warn_main(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \
  272. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  273. RSPAMD_LOG_FUNC, \
  274. __VA_ARGS__)
  275. #define msg_notice_main(...) rspamd_default_log_function(G_LOG_LEVEL_MESSAGE, \
  276. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  277. RSPAMD_LOG_FUNC, \
  278. __VA_ARGS__)
  279. #define msg_info_main(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \
  280. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  281. RSPAMD_LOG_FUNC, \
  282. __VA_ARGS__)
  283. #ifdef __cplusplus
  284. }
  285. #endif
  286. #endif /* WORKER_UTIL_H_ */