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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.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. * Prepare worker's startup
  31. * @param worker worker structure
  32. * @param name name of the worker
  33. * @param sig_handler handler of main signals
  34. * @param accept_handler handler of accept event for listen sockets
  35. * @return event base suitable for a worker
  36. */
  37. struct event_base *
  38. rspamd_prepare_worker (struct rspamd_worker *worker, const char *name,
  39. void (*accept_handler)(int, short, void *));
  40. /**
  41. * Set special signal handler for a worker
  42. */
  43. void rspamd_worker_set_signal_handler (int signo,
  44. struct rspamd_worker *worker,
  45. struct event_base *base,
  46. void (*handler) (struct rspamd_worker_signal_handler *, void *),
  47. void *handler_data);
  48. /**
  49. * Stop accepting new connections for a worker
  50. * @param worker
  51. */
  52. void rspamd_worker_stop_accept (struct rspamd_worker *worker);
  53. typedef gint (*rspamd_controller_func_t) (
  54. struct rspamd_http_connection_entry *conn_ent,
  55. struct rspamd_http_message *msg,
  56. struct module_ctx *ctx);
  57. struct rspamd_custom_controller_command {
  58. const gchar *command;
  59. struct module_ctx *ctx;
  60. gboolean privilleged;
  61. gboolean require_message;
  62. rspamd_controller_func_t handler;
  63. };
  64. struct rspamd_controller_worker_ctx;
  65. struct rspamd_controller_session {
  66. struct rspamd_controller_worker_ctx *ctx;
  67. struct rspamd_worker *wrk;
  68. rspamd_mempool_t *pool;
  69. struct rspamd_task *task;
  70. gchar *classifier;
  71. rspamd_inet_addr_t *from_addr;
  72. struct rspamd_config *cfg;
  73. gboolean is_spam;
  74. };
  75. /**
  76. * Send error using HTTP and JSON output
  77. * @param entry router entry
  78. * @param code error code
  79. * @param error_msg error message
  80. */
  81. void rspamd_controller_send_error (struct rspamd_http_connection_entry *entry,
  82. gint code, const gchar *error_msg, ...);
  83. /**
  84. * Send a custom string using HTTP
  85. * @param entry router entry
  86. * @param str string to send
  87. */
  88. void rspamd_controller_send_string (struct rspamd_http_connection_entry *entry,
  89. const gchar *str);
  90. /**
  91. * Send UCL using HTTP and JSON serialization
  92. * @param entry router entry
  93. * @param obj object to send
  94. */
  95. void rspamd_controller_send_ucl (struct rspamd_http_connection_entry *entry,
  96. ucl_object_t *obj);
  97. /**
  98. * Return worker's control structure by its type
  99. * @param type
  100. * @return worker's control structure or NULL
  101. */
  102. worker_t * rspamd_get_worker_by_type (struct rspamd_config *cfg, GQuark type);
  103. void rspamd_worker_stop_accept (struct rspamd_worker *worker);
  104. /**
  105. * Block signals before terminations
  106. */
  107. void rspamd_worker_block_signals (void);
  108. /**
  109. * Fork new worker with the specified configuration
  110. */
  111. struct rspamd_worker *rspamd_fork_worker (struct rspamd_main *,
  112. struct rspamd_worker_conf *, guint idx, struct event_base *ev_base);
  113. #define msg_err_main(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
  114. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  115. G_STRFUNC, \
  116. __VA_ARGS__)
  117. #define msg_warn_main(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  118. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  119. G_STRFUNC, \
  120. __VA_ARGS__)
  121. #define msg_info_main(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  122. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  123. G_STRFUNC, \
  124. __VA_ARGS__)
  125. #define msg_debug_main(...) rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \
  126. rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
  127. G_STRFUNC, \
  128. __VA_ARGS__)
  129. #endif /* WORKER_UTIL_H_ */