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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "http_connection.h"
  21. #include "rspamd.h"
  22. #ifndef HAVE_SA_SIGINFO
  23. typedef void (*rspamd_sig_handler_t) (gint);
  24. #else
  25. typedef void (*rspamd_sig_handler_t) (gint, siginfo_t *, void *);
  26. #endif
  27. struct rspamd_worker;
  28. struct rspamd_worker_signal_handler;
  29. /**
  30. * Init basic signals for a worker
  31. * @param worker
  32. * @param base
  33. */
  34. void rspamd_worker_init_signals (struct rspamd_worker *worker, struct event_base *base);
  35. /**
  36. * Prepare worker's startup
  37. * @param worker worker structure
  38. * @param name name of the worker
  39. * @param sig_handler handler of main signals
  40. * @param accept_handler handler of accept event for listen sockets
  41. * @return event base suitable for a worker
  42. */
  43. struct event_base *
  44. rspamd_prepare_worker (struct rspamd_worker *worker, const char *name,
  45. void (*accept_handler)(int, short, void *));
  46. /**
  47. * Set special signal handler for a worker
  48. */
  49. void rspamd_worker_set_signal_handler (int signo,
  50. struct rspamd_worker *worker,
  51. struct event_base *base,
  52. rspamd_worker_signal_handler handler,
  53. void *handler_data);
  54. /**
  55. * Stop accepting new connections for a worker
  56. * @param worker
  57. */
  58. void rspamd_worker_stop_accept (struct rspamd_worker *worker);
  59. typedef gint (*rspamd_controller_func_t) (
  60. struct rspamd_http_connection_entry *conn_ent,
  61. struct rspamd_http_message *msg,
  62. struct module_ctx *ctx);
  63. struct rspamd_custom_controller_command {
  64. const gchar *command;
  65. struct module_ctx *ctx;
  66. gboolean privilleged;
  67. gboolean require_message;
  68. rspamd_controller_func_t handler;
  69. };
  70. struct rspamd_controller_worker_ctx;
  71. struct rspamd_lang_detector;
  72. struct rspamd_controller_session {
  73. struct rspamd_controller_worker_ctx *ctx;
  74. struct rspamd_worker *wrk;
  75. rspamd_mempool_t *pool;
  76. struct rspamd_task *task;
  77. gchar *classifier;
  78. rspamd_inet_addr_t *from_addr;
  79. struct rspamd_config *cfg;
  80. struct rspamd_lang_detector *lang_det;
  81. gboolean is_spam;
  82. gboolean is_enable;
  83. };
  84. /**
  85. * Send error using HTTP and JSON output
  86. * @param entry router entry
  87. * @param code error code
  88. * @param error_msg error message
  89. */
  90. void rspamd_controller_send_error (struct rspamd_http_connection_entry *entry,
  91. gint code, const gchar *error_msg, ...);
  92. /**
  93. * Send a custom string using HTTP
  94. * @param entry router entry
  95. * @param str string to send
  96. */
  97. void rspamd_controller_send_string (struct rspamd_http_connection_entry *entry,
  98. const gchar *str);
  99. /**
  100. * Send UCL using HTTP and JSON serialization
  101. * @param entry router entry
  102. * @param obj object to send
  103. */
  104. void rspamd_controller_send_ucl (struct rspamd_http_connection_entry *entry,
  105. ucl_object_t *obj);
  106. /**
  107. * Return worker's control structure by its type
  108. * @param type
  109. * @return worker's control structure or NULL
  110. */
  111. worker_t * rspamd_get_worker_by_type (struct rspamd_config *cfg, GQuark type);
  112. void rspamd_worker_stop_accept (struct rspamd_worker *worker);
  113. /**
  114. * Block signals before terminations
  115. */
  116. void rspamd_worker_block_signals (void);
  117. /**
  118. * Unblock signals
  119. */
  120. void rspamd_worker_unblock_signals (void);
  121. /**
  122. * Kill rspamd main and all workers
  123. * @param rspamd_main
  124. */
  125. void rspamd_hard_terminate (struct rspamd_main *rspamd_main) G_GNUC_NORETURN;
  126. /**
  127. * Returns TRUE if a specific worker is a scanner worker
  128. * @param w
  129. * @return
  130. */
  131. gboolean rspamd_worker_is_scanner (struct rspamd_worker *w);
  132. /**
  133. * Returns TRUE if a specific worker is a primary controller
  134. * @param w
  135. * @return
  136. */
  137. gboolean rspamd_worker_is_primary_controller (struct rspamd_worker *w);
  138. /**
  139. * Creates new session cache
  140. * @param w
  141. * @return
  142. */
  143. void * rspamd_worker_session_cache_new (struct rspamd_worker *w,
  144. struct event_base *ev_base);
  145. /**
  146. * Adds a new session identified by pointer
  147. * @param cache
  148. * @param tag
  149. * @param pref
  150. * @param ptr
  151. */
  152. void rspamd_worker_session_cache_add (void *cache, const gchar *tag,
  153. guint *pref, void *ptr);
  154. /**
  155. * Removes session from cache
  156. * @param cache
  157. * @param ptr
  158. */
  159. void rspamd_worker_session_cache_remove (void *cache, void *ptr);
  160. /**
  161. * Fork new worker with the specified configuration
  162. */
  163. struct rspamd_worker *rspamd_fork_worker (struct rspamd_main *,
  164. struct rspamd_worker_conf *, guint idx, struct event_base *ev_base);
  165. /**
  166. * Sets crash signals handlers if compiled with libunwind
  167. */
  168. void rspamd_set_crash_handler (struct rspamd_main *);
  169. /**
  170. * Initialise the main monitoring worker
  171. * @param worker
  172. * @param ev_base
  173. * @param resolver
  174. */
  175. void rspamd_worker_init_monitored (struct rspamd_worker *worker,
  176. struct event_base *ev_base,
  177. struct rspamd_dns_resolver *resolver);
  178. #define msg_err_main(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
  179. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  180. G_STRFUNC, \
  181. __VA_ARGS__)
  182. #define msg_warn_main(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  183. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  184. G_STRFUNC, \
  185. __VA_ARGS__)
  186. #define msg_info_main(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  187. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  188. G_STRFUNC, \
  189. __VA_ARGS__)
  190. #endif /* WORKER_UTIL_H_ */