mod_watchdog.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. 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 MOD_WATCHDOG_H
  17. #define MOD_WATCHDOG_H
  18. /**
  19. * @file mod_watchdog.h
  20. * @brief Watchdog module for Apache
  21. *
  22. * @defgroup MOD_WATCHDOG mod_watchdog
  23. * @ingroup APACHE_MODS
  24. * @{
  25. */
  26. #include "httpd.h"
  27. #include "http_config.h"
  28. #include "http_log.h"
  29. #include "ap_provider.h"
  30. #include "apr.h"
  31. #include "apr_strings.h"
  32. #include "apr_pools.h"
  33. #include "apr_shm.h"
  34. #include "apr_hash.h"
  35. #include "apr_hooks.h"
  36. #include "apr_optional.h"
  37. #include "apr_file_io.h"
  38. #include "apr_time.h"
  39. #include "apr_thread_proc.h"
  40. #include "apr_global_mutex.h"
  41. #include "apr_thread_mutex.h"
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /**
  46. * Default singleton watchdog instance name.
  47. * Singleton watchdog is protected by mutex and
  48. * guaranteed to be run inside a single child process
  49. * at any time.
  50. */
  51. #define AP_WATCHDOG_SINGLETON "_singleton_"
  52. /**
  53. * Default watchdog instance name
  54. */
  55. #define AP_WATCHDOG_DEFAULT "_default_"
  56. /**
  57. * Default Watchdog interval
  58. */
  59. #define AP_WD_TM_INTERVAL APR_TIME_C(1000000) /* 1 second */
  60. /**
  61. * Watchdog thread timer resolution
  62. */
  63. #define AP_WD_TM_SLICE APR_TIME_C(100000) /* 100 ms */
  64. /* State values for callback */
  65. #define AP_WATCHDOG_STATE_STARTING 1
  66. #define AP_WATCHDOG_STATE_RUNNING 2
  67. #define AP_WATCHDOG_STATE_STOPPING 3
  68. typedef struct ap_watchdog_t ap_watchdog_t;
  69. /* Create a set of AP_WD_DECLARE(type), AP_WD_DECLARE_NONSTD(type) and
  70. * AP_WD_DECLARE_DATA with appropriate export and import tags for the platform
  71. */
  72. #if !defined(AP_WD_DECLARE)
  73. #if !defined(WIN32)
  74. #define AP_WD_DECLARE(type) type
  75. #define AP_WD_DECLARE_NONSTD(type) type
  76. #define AP_WD_DECLARE_DATA
  77. #elif defined(AP_WD_DECLARE_STATIC)
  78. #define AP_WD_DECLARE(type) type __stdcall
  79. #define AP_WD_DECLARE_NONSTD(type) type
  80. #define AP_WD_DECLARE_DATA
  81. #elif defined(AP_WD_DECLARE_EXPORT)
  82. #define AP_WD_DECLARE(type) __declspec(dllexport) type __stdcall
  83. #define AP_WD_DECLARE_NONSTD(type) __declspec(dllexport) type
  84. #define AP_WD_DECLARE_DATA __declspec(dllexport)
  85. #else
  86. #define AP_WD_DECLARE(type) __declspec(dllimport) type __stdcall
  87. #define AP_WD_DECLARE_NONSTD(type) __declspec(dllimport) type
  88. #define AP_WD_DECLARE_DATA __declspec(dllimport)
  89. #endif
  90. #endif
  91. /**
  92. * Callback function used for watchdog.
  93. * @param state Watchdog state function. See @p AP_WATCHDOG_STATE_ .
  94. * @param data is what was passed to @p ap_watchdog_register_callback function.
  95. * @param pool Temporary callback pool destroyed after the call.
  96. * @return APR_SUCCESS to continue calling this callback.
  97. */
  98. typedef apr_status_t ap_watchdog_callback_fn_t(int state, void *data,
  99. apr_pool_t *pool);
  100. /**
  101. * Get watchdog instance.
  102. * @param watchdog Storage for watchdog instance.
  103. * @param name Watchdog name.
  104. * @param parent Non-zero to get the parent process watchdog instance.
  105. * @param singleton Non-zero to get the singleton watchdog instance.
  106. * @param p The pool to use.
  107. * @return APR_SUCCESS if all went well
  108. * @remark Use @p AP_WATCHDOG_DEFAULT to get default watchdog instance.
  109. * If separate watchdog thread is needed provide unique name
  110. * and function will create a new watchdog instance.
  111. * Note that default client process watchdog works in singleton mode.
  112. */
  113. APR_DECLARE_OPTIONAL_FN(apr_status_t, ap_watchdog_get_instance,
  114. (ap_watchdog_t **watchdog, const char *name, int parent,
  115. int singleton, apr_pool_t *p));
  116. /**
  117. * Register watchdog callback.
  118. * @param watchdog Watchdog to use
  119. * @param interval Interval on which the callback function will execute.
  120. * @param callback The function to call on watchdog event.
  121. * @param data The data to pass to the callback function.
  122. * @return APR_SUCCESS if all went well. APR_EEXIST if already registered.
  123. */
  124. APR_DECLARE_OPTIONAL_FN(apr_status_t, ap_watchdog_register_callback,
  125. (ap_watchdog_t *watchdog, apr_interval_time_t interval,
  126. const void *data, ap_watchdog_callback_fn_t *callback));
  127. /**
  128. * Update registered watchdog callback interval.
  129. * @param w Watchdog to use
  130. * @param interval New interval on which the callback function will execute.
  131. * @param callback The function to call on watchdog event.
  132. * @param data The data to pass to the callback function.
  133. * @return APR_SUCCESS if all went well. APR_EOF if callback was not found.
  134. */
  135. APR_DECLARE_OPTIONAL_FN(apr_status_t, ap_watchdog_set_callback_interval,
  136. (ap_watchdog_t *w, apr_interval_time_t interval,
  137. const void *data, ap_watchdog_callback_fn_t *callback));
  138. /**
  139. * Watchdog require hook.
  140. * @param s The server record
  141. * @param name Watchdog name.
  142. * @param parent Non-zero to indicate the parent process watchdog mode.
  143. * @param singleton Non-zero to indicate the singleton watchdog mode.
  144. * @return OK to enable notifications from this watchdog, DECLINED otherwise.
  145. * @remark This is called in post config phase for all watchdog instances
  146. * that have no callbacks registered. Modules using this hook
  147. * should ensure that their post_config hook is called after watchdog
  148. * post_config.
  149. */
  150. APR_DECLARE_EXTERNAL_HOOK(ap, AP_WD, int, watchdog_need, (server_rec *s,
  151. const char *name,
  152. int parent, int singleton))
  153. /**
  154. * Watchdog initialize hook.
  155. * It is called after the watchdog thread is initialized.
  156. * @param s The server record
  157. * @param name Watchdog name.
  158. * @param pool The pool used to create the watchdog.
  159. */
  160. APR_DECLARE_EXTERNAL_HOOK(ap, AP_WD, int, watchdog_init, (
  161. server_rec *s,
  162. const char *name,
  163. apr_pool_t *pool))
  164. /**
  165. * Watchdog terminate hook.
  166. * It is called when the watchdog thread is terminated.
  167. * @param s The server record
  168. * @param name Watchdog name.
  169. * @param pool The pool used to create the watchdog.
  170. */
  171. APR_DECLARE_EXTERNAL_HOOK(ap, AP_WD, int, watchdog_exit, (
  172. server_rec *s,
  173. const char *name,
  174. apr_pool_t *pool))
  175. /**
  176. * Fixed interval watchdog hook.
  177. * It is called regularly on @p AP_WD_TM_INTERVAL interval.
  178. * @param s The server record
  179. * @param name Watchdog name.
  180. * @param pool Temporary pool destroyed after the call.
  181. */
  182. APR_DECLARE_EXTERNAL_HOOK(ap, AP_WD, int, watchdog_step, (
  183. server_rec *s,
  184. const char *name,
  185. apr_pool_t *pool))
  186. #ifdef __cplusplus
  187. }
  188. #endif
  189. #endif /* MOD_WATCHDOG_H */
  190. /** @} */