選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

rspamd_control.h 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 RSPAMD_RSPAMD_CONTROL_H
  17. #define RSPAMD_RSPAMD_CONTROL_H
  18. #include "config.h"
  19. #include "mem_pool.h"
  20. #include "contrib/libev/ev.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. struct rspamd_main;
  25. struct rspamd_worker;
  26. enum rspamd_control_type {
  27. RSPAMD_CONTROL_STAT = 0,
  28. RSPAMD_CONTROL_RELOAD,
  29. RSPAMD_CONTROL_RERESOLVE,
  30. RSPAMD_CONTROL_RECOMPILE,
  31. RSPAMD_CONTROL_HYPERSCAN_LOADED,
  32. RSPAMD_CONTROL_LOG_PIPE,
  33. RSPAMD_CONTROL_FUZZY_STAT,
  34. RSPAMD_CONTROL_FUZZY_SYNC,
  35. RSPAMD_CONTROL_MONITORED_CHANGE,
  36. RSPAMD_CONTROL_CHILD_CHANGE,
  37. RSPAMD_CONTROL_MAX
  38. };
  39. enum rspamd_srv_type {
  40. RSPAMD_SRV_SOCKETPAIR = 0,
  41. RSPAMD_SRV_HYPERSCAN_LOADED,
  42. RSPAMD_SRV_MONITORED_CHANGE,
  43. RSPAMD_SRV_LOG_PIPE,
  44. RSPAMD_SRV_ON_FORK,
  45. RSPAMD_SRV_HEARTBEAT,
  46. };
  47. enum rspamd_log_pipe_type {
  48. RSPAMD_LOG_PIPE_SYMBOLS = 0,
  49. };
  50. #define CONTROL_PATHLEN 400
  51. struct rspamd_control_command {
  52. enum rspamd_control_type type;
  53. union {
  54. struct {
  55. guint unused;
  56. } stat;
  57. struct {
  58. guint unused;
  59. } reload;
  60. struct {
  61. guint unused;
  62. } reresolve;
  63. struct {
  64. guint unused;
  65. } recompile;
  66. struct {
  67. gchar cache_dir[CONTROL_PATHLEN];
  68. gboolean forced;
  69. } hs_loaded;
  70. struct {
  71. gchar tag[32];
  72. gboolean alive;
  73. pid_t sender;
  74. } monitored_change;
  75. struct {
  76. enum rspamd_log_pipe_type type;
  77. } log_pipe;
  78. struct {
  79. guint unused;
  80. } fuzzy_stat;
  81. struct {
  82. guint unused;
  83. } fuzzy_sync;
  84. struct {
  85. enum {
  86. rspamd_child_offline,
  87. rspamd_child_online,
  88. rspamd_child_terminated,
  89. } what;
  90. pid_t pid;
  91. guint additional;
  92. } child_change;
  93. } cmd;
  94. };
  95. struct rspamd_control_reply {
  96. enum rspamd_control_type type;
  97. union {
  98. struct {
  99. guint conns;
  100. gdouble uptime;
  101. gdouble utime;
  102. gdouble systime;
  103. gulong maxrss;
  104. } stat;
  105. struct {
  106. guint status;
  107. } reload;
  108. struct {
  109. guint status;
  110. } reresolve;
  111. struct {
  112. guint status;
  113. } recompile;
  114. struct {
  115. guint status;
  116. } hs_loaded;
  117. struct {
  118. guint status;
  119. } monitored_change;
  120. struct {
  121. guint status;
  122. } log_pipe;
  123. struct {
  124. guint status;
  125. gchar storage_id[MEMPOOL_UID_LEN];
  126. } fuzzy_stat;
  127. struct {
  128. guint status;
  129. } fuzzy_sync;
  130. } reply;
  131. };
  132. #define PAIR_ID_LEN 16
  133. struct rspamd_srv_command {
  134. enum rspamd_srv_type type;
  135. guint64 id;
  136. union {
  137. struct {
  138. gint af;
  139. gchar pair_id[PAIR_ID_LEN];
  140. guint pair_num;
  141. } spair;
  142. struct {
  143. gchar cache_dir[CONTROL_PATHLEN];
  144. gboolean forced;
  145. } hs_loaded;
  146. struct {
  147. gchar tag[32];
  148. gboolean alive;
  149. pid_t sender;
  150. } monitored_change;
  151. struct {
  152. enum rspamd_log_pipe_type type;
  153. } log_pipe;
  154. struct {
  155. pid_t ppid;
  156. pid_t cpid;
  157. enum {
  158. child_create = 0,
  159. child_dead,
  160. } state;
  161. } on_fork;
  162. struct {
  163. guint status;
  164. /* TODO: add more fields */
  165. } heartbeat;
  166. } cmd;
  167. };
  168. struct rspamd_srv_reply {
  169. enum rspamd_srv_type type;
  170. guint64 id;
  171. union {
  172. struct {
  173. gint code;
  174. } spair;
  175. struct {
  176. gint forced;
  177. } hs_loaded;
  178. struct {
  179. gint status;
  180. };
  181. struct {
  182. enum rspamd_log_pipe_type type;
  183. } log_pipe;
  184. struct {
  185. gint status;
  186. } on_fork;
  187. struct {
  188. gint status;
  189. } heartbeat;
  190. } reply;
  191. };
  192. typedef gboolean (*rspamd_worker_control_handler) (struct rspamd_main *rspamd_main,
  193. struct rspamd_worker *worker,
  194. gint fd,
  195. gint attached_fd,
  196. struct rspamd_control_command *cmd,
  197. gpointer ud);
  198. typedef void (*rspamd_srv_reply_handler) (struct rspamd_worker *worker,
  199. struct rspamd_srv_reply *rep, gint rep_fd,
  200. gpointer ud);
  201. /**
  202. * Process client socket connection
  203. */
  204. void rspamd_control_process_client_socket (struct rspamd_main *rspamd_main,
  205. gint fd, rspamd_inet_addr_t *addr);
  206. /**
  207. * Register default handlers for a worker
  208. */
  209. void rspamd_control_worker_add_default_cmd_handlers (struct rspamd_worker *worker,
  210. struct ev_loop *ev_base);
  211. /**
  212. * Register custom handler for a specific control command for this worker
  213. */
  214. void rspamd_control_worker_add_cmd_handler (struct rspamd_worker *worker,
  215. enum rspamd_control_type type,
  216. rspamd_worker_control_handler handler,
  217. gpointer ud);
  218. /**
  219. * Start watching on srv pipe
  220. */
  221. void rspamd_srv_start_watching (struct rspamd_main *srv,
  222. struct rspamd_worker *worker,
  223. struct ev_loop *ev_base);
  224. /**
  225. * Send command to srv pipe and read reply calling the specified callback at the
  226. * end
  227. */
  228. void rspamd_srv_send_command (struct rspamd_worker *worker,
  229. struct ev_loop *ev_base,
  230. struct rspamd_srv_command *cmd,
  231. gint attached_fd,
  232. rspamd_srv_reply_handler handler,
  233. gpointer ud);
  234. /**
  235. * Broadcast srv cmd from rspamd_main to workers
  236. * @param rspamd_main
  237. * @param cmd
  238. * @param except_pid
  239. */
  240. void rspamd_control_broadcast_srv_cmd (struct rspamd_main *rspamd_main,
  241. struct rspamd_control_command *cmd,
  242. pid_t except_pid);
  243. /**
  244. * Returns command from a specified string (case insensitive)
  245. * @param str
  246. * @return
  247. */
  248. enum rspamd_control_type rspamd_control_command_from_string (const gchar *str);
  249. /**
  250. * Returns command name from it's type
  251. * @param cmd
  252. * @return
  253. */
  254. const gchar *rspamd_control_command_to_string (enum rspamd_control_type cmd);
  255. /**
  256. * Used to cleanup pending events
  257. * @param p
  258. */
  259. void rspamd_pending_control_free (gpointer p);
  260. #ifdef __cplusplus
  261. }
  262. #endif
  263. #endif