apr_hooks.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  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 APR_HOOKS_H
  17. #define APR_HOOKS_H
  18. #include "apu.h"
  19. /* For apr_array_header_t */
  20. #include "apr_tables.h"
  21. /**
  22. * @file apr_hooks.h
  23. * @brief Apache hook functions
  24. */
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /**
  29. * @defgroup APR_Util_Hook Hook Functions
  30. * @ingroup APR_Util
  31. * @{
  32. */
  33. /** macro to return the prototype of the hook function */
  34. #define APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name) \
  35. link##_DECLARE(apr_array_header_t *) ns##_hook_get_##name(void)
  36. /** macro to declare the hook correctly */
  37. #define APR_DECLARE_EXTERNAL_HOOK(ns,link,ret,name,args) \
  38. typedef ret ns##_HOOK_##name##_t args; \
  39. link##_DECLARE(void) ns##_hook_##name(ns##_HOOK_##name##_t *pf, \
  40. const char * const *aszPre, \
  41. const char * const *aszSucc, int nOrder); \
  42. link##_DECLARE(ret) ns##_run_##name args; \
  43. APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name); \
  44. typedef struct ns##_LINK_##name##_t \
  45. { \
  46. ns##_HOOK_##name##_t *pFunc; \
  47. const char *szName; \
  48. const char * const *aszPredecessors; \
  49. const char * const *aszSuccessors; \
  50. int nOrder; \
  51. } ns##_LINK_##name##_t;
  52. /** macro to declare the hook structure */
  53. #define APR_HOOK_STRUCT(members) \
  54. static struct { members } _hooks;
  55. /** macro to link the hook structure */
  56. #define APR_HOOK_LINK(name) \
  57. apr_array_header_t *link_##name;
  58. /** macro to implement the hook */
  59. #define APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
  60. link##_DECLARE(void) ns##_hook_##name(ns##_HOOK_##name##_t *pf,const char * const *aszPre, \
  61. const char * const *aszSucc,int nOrder) \
  62. { \
  63. ns##_LINK_##name##_t *pHook; \
  64. if(!_hooks.link_##name) \
  65. { \
  66. _hooks.link_##name=apr_array_make(apr_hook_global_pool,1,sizeof(ns##_LINK_##name##_t)); \
  67. apr_hook_sort_register(#name,&_hooks.link_##name); \
  68. } \
  69. pHook=apr_array_push(_hooks.link_##name); \
  70. pHook->pFunc=pf; \
  71. pHook->aszPredecessors=aszPre; \
  72. pHook->aszSuccessors=aszSucc; \
  73. pHook->nOrder=nOrder; \
  74. pHook->szName=apr_hook_debug_current; \
  75. if(apr_hook_debug_enabled) \
  76. apr_hook_debug_show(#name,aszPre,aszSucc); \
  77. } \
  78. APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name) \
  79. { \
  80. return _hooks.link_##name; \
  81. }
  82. /**
  83. * Implement a hook that has no return code, and therefore runs all of the
  84. * registered functions
  85. * @param ns The namespace prefix of the hook functions
  86. * @param link The linkage declaration prefix of the hook
  87. * @param name The name of the hook
  88. * @param args_decl The declaration of the arguments for the hook
  89. * @param args_use The names for the arguments for the hook
  90. * @note The link prefix FOO corresponds to FOO_DECLARE() macros, which
  91. * provide export linkage from the module that IMPLEMENTs the hook, and
  92. * import linkage from external modules that link to the hook's module.
  93. */
  94. #define APR_IMPLEMENT_EXTERNAL_HOOK_VOID(ns,link,name,args_decl,args_use) \
  95. APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
  96. link##_DECLARE(void) ns##_run_##name args_decl \
  97. { \
  98. ns##_LINK_##name##_t *pHook; \
  99. int n; \
  100. \
  101. if(!_hooks.link_##name) \
  102. return; \
  103. \
  104. pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
  105. for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
  106. pHook[n].pFunc args_use; \
  107. }
  108. /* FIXME: note that this returns ok when nothing is run. I suspect it should
  109. really return decline, but that breaks Apache currently - Ben
  110. */
  111. /**
  112. * Implement a hook that runs until one of the functions returns something
  113. * other than OK or DECLINE
  114. * @param ns The namespace prefix of the hook functions
  115. * @param link The linkage declaration prefix of the hook
  116. * @param ret Type to return
  117. * @param name The name of the hook
  118. * @param args_decl The declaration of the arguments for the hook
  119. * @param args_use The names for the arguments for the hook
  120. * @param ok Success value
  121. * @param decline Decline value
  122. * @note The link prefix FOO corresponds to FOO_DECLARE() macros, which
  123. * provide export linkage from the module that IMPLEMENTs the hook, and
  124. * import linkage from external modules that link to the hook's module.
  125. */
  126. #define APR_IMPLEMENT_EXTERNAL_HOOK_RUN_ALL(ns,link,ret,name,args_decl,args_use,ok,decline) \
  127. APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
  128. link##_DECLARE(ret) ns##_run_##name args_decl \
  129. { \
  130. ns##_LINK_##name##_t *pHook; \
  131. int n; \
  132. ret rv; \
  133. \
  134. if(!_hooks.link_##name) \
  135. return ok; \
  136. \
  137. pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
  138. for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
  139. { \
  140. rv=pHook[n].pFunc args_use; \
  141. \
  142. if(rv != ok && rv != decline) \
  143. return rv; \
  144. } \
  145. return ok; \
  146. }
  147. /**
  148. * Implement a hook that runs until the first function returns something
  149. * other than the value of decline
  150. * @param ns The namespace prefix of the hook functions
  151. * @param link The linkage declaration prefix of the hook
  152. * @param name The name of the hook
  153. * @param ret Type to return
  154. * @param args_decl The declaration of the arguments for the hook
  155. * @param args_use The names for the arguments for the hook
  156. * @param decline Decline value
  157. * @note The link prefix FOO corresponds to FOO_DECLARE() macros, which
  158. * provide export linkage from the module that IMPLEMENTs the hook, and
  159. * import linkage from external modules that link to the hook's module.
  160. */
  161. #define APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(ns,link,ret,name,args_decl,args_use,decline) \
  162. APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
  163. link##_DECLARE(ret) ns##_run_##name args_decl \
  164. { \
  165. ns##_LINK_##name##_t *pHook; \
  166. int n; \
  167. ret rv; \
  168. \
  169. if(!_hooks.link_##name) \
  170. return decline; \
  171. \
  172. pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
  173. for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
  174. { \
  175. rv=pHook[n].pFunc args_use; \
  176. \
  177. if(rv != decline) \
  178. return rv; \
  179. } \
  180. return decline; \
  181. }
  182. /* Hook orderings */
  183. /** run this hook first, before ANYTHING */
  184. #define APR_HOOK_REALLY_FIRST (-10)
  185. /** run this hook first */
  186. #define APR_HOOK_FIRST 0
  187. /** run this hook somewhere */
  188. #define APR_HOOK_MIDDLE 10
  189. /** run this hook after every other hook which is defined*/
  190. #define APR_HOOK_LAST 20
  191. /** run this hook last, after EVERYTHING */
  192. #define APR_HOOK_REALLY_LAST 30
  193. /**
  194. * The global pool used to allocate any memory needed by the hooks.
  195. */
  196. APU_DECLARE_DATA extern apr_pool_t *apr_hook_global_pool;
  197. /**
  198. * A global variable to determine if debugging information about the
  199. * hooks functions should be printed
  200. */
  201. APU_DECLARE_DATA extern int apr_hook_debug_enabled;
  202. /**
  203. * The name of the module that is currently registering a function
  204. */
  205. APU_DECLARE_DATA extern const char *apr_hook_debug_current;
  206. /**
  207. * Register a hook function to be sorted
  208. * @param szHookName The name of the Hook the function is registered for
  209. * @param aHooks The array which stores all of the functions for this hook
  210. */
  211. APU_DECLARE(void) apr_hook_sort_register(const char *szHookName,
  212. apr_array_header_t **aHooks);
  213. /**
  214. * Sort all of the registerd functions for a given hook
  215. */
  216. APU_DECLARE(void) apr_hook_sort_all(void);
  217. /**
  218. * Print all of the information about the current hook. This is used for
  219. * debugging purposes.
  220. * @param szName The name of the hook
  221. * @param aszPre All of the functions in the predecessor array
  222. * @param aszSucc All of the functions in the successor array
  223. */
  224. APU_DECLARE(void) apr_hook_debug_show(const char *szName,
  225. const char * const *aszPre,
  226. const char * const *aszSucc);
  227. /**
  228. * Remove all currently registered functions.
  229. */
  230. APU_DECLARE(void) apr_hook_deregister_all(void);
  231. /** @} */
  232. #ifdef __cplusplus
  233. }
  234. #endif
  235. #endif /* APR_HOOKS_H */