apr_hooks.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 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. /**
  34. * @defgroup apr_hook_probes Hook probe capability
  35. * APR hooks provide a trace probe capability for capturing
  36. * the flow of control and return values with hooks.
  37. *
  38. * In order to use this facility, the application must define
  39. * the symbol APR_HOOK_PROBES_ENABLED and the four APR_HOOK_PROBE_
  40. * macros described below before including apr_hooks.h in files
  41. * that use the APR_IMPLEMENT_EXTERNAL_HOOK_* macros.
  42. *
  43. * This probe facility is not provided for APR optional hooks.
  44. * @{
  45. */
  46. #ifdef APR_HOOK_PROBES_ENABLED
  47. #define APR_HOOK_INT_DCL_UD void *ud = NULL
  48. #else
  49. /** internal implementation detail to avoid the ud declaration when
  50. * hook probes are not used
  51. */
  52. #define APR_HOOK_INT_DCL_UD
  53. /**
  54. * User-defined hook probe macro that is invoked when the hook
  55. * is run, before calling any hook functions.
  56. * @param ud A void * user data field that should be filled in by
  57. * this macro, and will be provided to the other hook probe macros.
  58. * @param ns The namespace prefix of the hook functions
  59. * @param name The name of the hook
  60. * @param args The argument list to the hook functions, with enclosing
  61. * parens.
  62. */
  63. #define APR_HOOK_PROBE_ENTRY(ud,ns,name,args)
  64. /**
  65. * User-defined hook probe macro that is invoked after the hook
  66. * has run.
  67. * @param ud A void * user data field that was filled in by the user-
  68. * provided APR_HOOK_PROBE_ENTRY().
  69. * @param ns The namespace prefix of the hook functions
  70. * @param name The name of the hook
  71. * @param rv The return value of the hook, or 0 if the hook is void.
  72. * @param args The argument list to the hook functions, with enclosing
  73. * parens.
  74. */
  75. #define APR_HOOK_PROBE_RETURN(ud,ns,name,rv,args)
  76. /**
  77. * User-defined hook probe macro that is invoked before calling a
  78. * hook function.
  79. * @param ud A void * user data field that was filled in by the user-
  80. * provided APR_HOOK_PROBE_ENTRY().
  81. * @param ns The namespace prefix of the hook functions
  82. * @param name The name of the hook
  83. * @param src The value of apr_hook_debug_current at the time the function
  84. * was hooked (usually the source file implementing the hook function).
  85. * @param args The argument list to the hook functions, with enclosing
  86. * parens.
  87. */
  88. #define APR_HOOK_PROBE_INVOKE(ud,ns,name,src,args)
  89. /**
  90. * User-defined hook probe macro that is invoked after calling a
  91. * hook function.
  92. * @param ud A void * user data field that was filled in by the user-
  93. * provided APR_HOOK_PROBE_ENTRY().
  94. * @param ns The namespace prefix of the hook functions
  95. * @param name The name of the hook
  96. * @param src The value of apr_hook_debug_current at the time the function
  97. * was hooked (usually the source file implementing the hook function).
  98. * @param rv The return value of the hook function, or 0 if the hook is void.
  99. * @param args The argument list to the hook functions, with enclosing
  100. * parens.
  101. */
  102. #define APR_HOOK_PROBE_COMPLETE(ud,ns,name,src,rv,args)
  103. #endif
  104. /** @} */
  105. /** macro to return the prototype of the hook function */
  106. #define APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name) \
  107. link##_DECLARE(apr_array_header_t *) ns##_hook_get_##name(void)
  108. /** macro to declare the hook correctly */
  109. #define APR_DECLARE_EXTERNAL_HOOK(ns,link,ret,name,args) \
  110. typedef ret ns##_HOOK_##name##_t args; \
  111. link##_DECLARE(void) ns##_hook_##name(ns##_HOOK_##name##_t *pf, \
  112. const char * const *aszPre, \
  113. const char * const *aszSucc, int nOrder); \
  114. link##_DECLARE(ret) ns##_run_##name args; \
  115. APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name); \
  116. typedef struct ns##_LINK_##name##_t \
  117. { \
  118. ns##_HOOK_##name##_t *pFunc; \
  119. const char *szName; \
  120. const char * const *aszPredecessors; \
  121. const char * const *aszSuccessors; \
  122. int nOrder; \
  123. } ns##_LINK_##name##_t;
  124. /** macro to declare the hook structure */
  125. #define APR_HOOK_STRUCT(members) \
  126. static struct { members } _hooks;
  127. /** macro to link the hook structure */
  128. #define APR_HOOK_LINK(name) \
  129. apr_array_header_t *link_##name;
  130. /** macro to implement the hook */
  131. #define APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
  132. link##_DECLARE(void) ns##_hook_##name(ns##_HOOK_##name##_t *pf,const char * const *aszPre, \
  133. const char * const *aszSucc,int nOrder) \
  134. { \
  135. ns##_LINK_##name##_t *pHook; \
  136. if(!_hooks.link_##name) \
  137. { \
  138. _hooks.link_##name=apr_array_make(apr_hook_global_pool,1,sizeof(ns##_LINK_##name##_t)); \
  139. apr_hook_sort_register(#name,&_hooks.link_##name); \
  140. } \
  141. pHook=apr_array_push(_hooks.link_##name); \
  142. pHook->pFunc=pf; \
  143. pHook->aszPredecessors=aszPre; \
  144. pHook->aszSuccessors=aszSucc; \
  145. pHook->nOrder=nOrder; \
  146. pHook->szName=apr_hook_debug_current; \
  147. if(apr_hook_debug_enabled) \
  148. apr_hook_debug_show(#name,aszPre,aszSucc); \
  149. } \
  150. APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name) \
  151. { \
  152. return _hooks.link_##name; \
  153. }
  154. /**
  155. * Implement a hook that has no return code, and therefore runs all of the
  156. * registered functions
  157. * @param ns The namespace prefix of the hook functions
  158. * @param link The linkage declaration prefix of the hook
  159. * @param name The name of the hook
  160. * @param args_decl The declaration of the arguments for the hook
  161. * @param args_use The names for the arguments for the hook
  162. * @note The link prefix FOO corresponds to FOO_DECLARE() macros, which
  163. * provide export linkage from the module that IMPLEMENTs the hook, and
  164. * import linkage from external modules that link to the hook's module.
  165. */
  166. #define APR_IMPLEMENT_EXTERNAL_HOOK_VOID(ns,link,name,args_decl,args_use) \
  167. APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
  168. link##_DECLARE(void) ns##_run_##name args_decl \
  169. { \
  170. ns##_LINK_##name##_t *pHook; \
  171. int n; \
  172. APR_HOOK_INT_DCL_UD; \
  173. \
  174. APR_HOOK_PROBE_ENTRY(ud, ns, name, args_use); \
  175. \
  176. if(_hooks.link_##name) \
  177. { \
  178. pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
  179. for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
  180. { \
  181. APR_HOOK_PROBE_INVOKE(ud, ns, name, (char *)pHook[n].szName, args_use); \
  182. pHook[n].pFunc args_use; \
  183. APR_HOOK_PROBE_COMPLETE(ud, ns, name, (char *)pHook[n].szName, 0, args_use); \
  184. } \
  185. } \
  186. \
  187. APR_HOOK_PROBE_RETURN(ud, ns, name, 0, args_use); \
  188. \
  189. }
  190. /* FIXME: note that this returns ok when nothing is run. I suspect it should
  191. really return decline, but that breaks Apache currently - Ben
  192. */
  193. /**
  194. * Implement a hook that runs until one of the functions returns something
  195. * other than OK or DECLINE
  196. * @param ns The namespace prefix of the hook functions
  197. * @param link The linkage declaration prefix of the hook
  198. * @param ret Type to return
  199. * @param name The name of the hook
  200. * @param args_decl The declaration of the arguments for the hook
  201. * @param args_use The names for the arguments for the hook
  202. * @param ok Success value
  203. * @param decline Decline value
  204. * @note The link prefix FOO corresponds to FOO_DECLARE() macros, which
  205. * provide export linkage from the module that IMPLEMENTs the hook, and
  206. * import linkage from external modules that link to the hook's module.
  207. */
  208. #define APR_IMPLEMENT_EXTERNAL_HOOK_RUN_ALL(ns,link,ret,name,args_decl,args_use,ok,decline) \
  209. APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
  210. link##_DECLARE(ret) ns##_run_##name args_decl \
  211. { \
  212. ns##_LINK_##name##_t *pHook; \
  213. int n; \
  214. ret rv = ok; \
  215. APR_HOOK_INT_DCL_UD; \
  216. \
  217. APR_HOOK_PROBE_ENTRY(ud, ns, name, args_use); \
  218. \
  219. if(_hooks.link_##name) \
  220. { \
  221. pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
  222. for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
  223. { \
  224. APR_HOOK_PROBE_INVOKE(ud, ns, name, (char *)pHook[n].szName, args_use); \
  225. rv=pHook[n].pFunc args_use; \
  226. APR_HOOK_PROBE_COMPLETE(ud, ns, name, (char *)pHook[n].szName, rv, args_use); \
  227. if(rv != ok && rv != decline) \
  228. break; \
  229. rv = ok; \
  230. } \
  231. } \
  232. \
  233. APR_HOOK_PROBE_RETURN(ud, ns, name, rv, args_use); \
  234. \
  235. return rv; \
  236. }
  237. /**
  238. * Implement a hook that runs until the first function returns something
  239. * other than the value of decline
  240. * @param ns The namespace prefix of the hook functions
  241. * @param link The linkage declaration prefix of the hook
  242. * @param name The name of the hook
  243. * @param ret Type to return
  244. * @param args_decl The declaration of the arguments for the hook
  245. * @param args_use The names for the arguments for the hook
  246. * @param decline Decline value
  247. * @note The link prefix FOO corresponds to FOO_DECLARE() macros, which
  248. * provide export linkage from the module that IMPLEMENTs the hook, and
  249. * import linkage from external modules that link to the hook's module.
  250. */
  251. #define APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(ns,link,ret,name,args_decl,args_use,decline) \
  252. APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
  253. link##_DECLARE(ret) ns##_run_##name args_decl \
  254. { \
  255. ns##_LINK_##name##_t *pHook; \
  256. int n; \
  257. ret rv = decline; \
  258. APR_HOOK_INT_DCL_UD; \
  259. \
  260. APR_HOOK_PROBE_ENTRY(ud, ns, name, args_use); \
  261. \
  262. if(_hooks.link_##name) \
  263. { \
  264. pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
  265. for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
  266. { \
  267. APR_HOOK_PROBE_INVOKE(ud, ns, name, (char *)pHook[n].szName, args_use); \
  268. rv=pHook[n].pFunc args_use; \
  269. APR_HOOK_PROBE_COMPLETE(ud, ns, name, (char *)pHook[n].szName, rv, args_use); \
  270. \
  271. if(rv != decline) \
  272. break; \
  273. } \
  274. } \
  275. \
  276. APR_HOOK_PROBE_RETURN(ud, ns, name, rv, args_use); \
  277. \
  278. return rv; \
  279. }
  280. /* Hook orderings */
  281. /** run this hook first, before ANYTHING */
  282. #define APR_HOOK_REALLY_FIRST (-10)
  283. /** run this hook first */
  284. #define APR_HOOK_FIRST 0
  285. /** run this hook somewhere */
  286. #define APR_HOOK_MIDDLE 10
  287. /** run this hook after every other hook which is defined*/
  288. #define APR_HOOK_LAST 20
  289. /** run this hook last, after EVERYTHING */
  290. #define APR_HOOK_REALLY_LAST 30
  291. /**
  292. * The global pool used to allocate any memory needed by the hooks.
  293. */
  294. APU_DECLARE_DATA extern apr_pool_t *apr_hook_global_pool;
  295. /**
  296. * A global variable to determine if debugging information about the
  297. * hooks functions should be printed.
  298. */
  299. APU_DECLARE_DATA extern int apr_hook_debug_enabled;
  300. /**
  301. * The name of the module that is currently registering a function.
  302. */
  303. APU_DECLARE_DATA extern const char *apr_hook_debug_current;
  304. /**
  305. * Register a hook function to be sorted.
  306. * @param szHookName The name of the Hook the function is registered for
  307. * @param aHooks The array which stores all of the functions for this hook
  308. */
  309. APU_DECLARE(void) apr_hook_sort_register(const char *szHookName,
  310. apr_array_header_t **aHooks);
  311. /**
  312. * Sort all of the registered functions for a given hook.
  313. */
  314. APU_DECLARE(void) apr_hook_sort_all(void);
  315. /**
  316. * Print all of the information about the current hook. This is used for
  317. * debugging purposes.
  318. * @param szName The name of the hook
  319. * @param aszPre All of the functions in the predecessor array
  320. * @param aszSucc All of the functions in the successor array
  321. */
  322. APU_DECLARE(void) apr_hook_debug_show(const char *szName,
  323. const char * const *aszPre,
  324. const char * const *aszSucc);
  325. /**
  326. * Remove all currently registered functions.
  327. */
  328. APU_DECLARE(void) apr_hook_deregister_all(void);
  329. /** @} */
  330. #ifdef __cplusplus
  331. }
  332. #endif
  333. #endif /* APR_HOOKS_H */