Apache Portable Runtime Utility Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
apr_hooks.h
Go to the documentation of this file.
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 
17 #ifndef APR_HOOKS_H
18 #define APR_HOOKS_H
19 
20 #include "apu.h"
21 /* For apr_array_header_t */
22 #include "apr_tables.h"
23 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
52 #ifdef APR_HOOK_PROBES_ENABLED
53 #define APR_HOOK_INT_DCL_UD void *ud = NULL
54 #else
55 
58 #define APR_HOOK_INT_DCL_UD
59 
69 #define APR_HOOK_PROBE_ENTRY(ud,ns,name,args)
70 
81 #define APR_HOOK_PROBE_RETURN(ud,ns,name,rv,args)
82 
94 #define APR_HOOK_PROBE_INVOKE(ud,ns,name,src,args)
95 
108 #define APR_HOOK_PROBE_COMPLETE(ud,ns,name,src,rv,args)
109 #endif
110 
114 #define APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name) \
115 link##_DECLARE(apr_array_header_t *) ns##_hook_get_##name(void)
116 
118 #define APR_DECLARE_EXTERNAL_HOOK(ns,link,ret,name,args) \
119 typedef ret ns##_HOOK_##name##_t args; \
120 link##_DECLARE(void) ns##_hook_##name(ns##_HOOK_##name##_t *pf, \
121  const char * const *aszPre, \
122  const char * const *aszSucc, int nOrder); \
123 link##_DECLARE(ret) ns##_run_##name args; \
124 APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name); \
125 typedef struct ns##_LINK_##name##_t \
126  { \
127  ns##_HOOK_##name##_t *pFunc; \
128  const char *szName; \
129  const char * const *aszPredecessors; \
130  const char * const *aszSuccessors; \
131  int nOrder; \
132  } ns##_LINK_##name##_t;
133 
135 #define APR_HOOK_STRUCT(members) \
136 static struct { members } _hooks;
137 
139 #define APR_HOOK_LINK(name) \
140  apr_array_header_t *link_##name;
141 
143 #define APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
144 link##_DECLARE(void) ns##_hook_##name(ns##_HOOK_##name##_t *pf,const char * const *aszPre, \
145  const char * const *aszSucc,int nOrder) \
146  { \
147  ns##_LINK_##name##_t *pHook; \
148  if(!_hooks.link_##name) \
149  { \
150  _hooks.link_##name=apr_array_make(apr_hook_global_pool,1,sizeof(ns##_LINK_##name##_t)); \
151  apr_hook_sort_register(#name,&_hooks.link_##name); \
152  } \
153  pHook=apr_array_push(_hooks.link_##name); \
154  pHook->pFunc=pf; \
155  pHook->aszPredecessors=aszPre; \
156  pHook->aszSuccessors=aszSucc; \
157  pHook->nOrder=nOrder; \
158  pHook->szName=apr_hook_debug_current; \
159  if(apr_hook_debug_enabled) \
160  apr_hook_debug_show(#name,aszPre,aszSucc); \
161  } \
162  APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name) \
163  { \
164  return _hooks.link_##name; \
165  }
166 
179 #define APR_IMPLEMENT_EXTERNAL_HOOK_VOID(ns,link,name,args_decl,args_use) \
180 APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
181 link##_DECLARE(void) ns##_run_##name args_decl \
182  { \
183  ns##_LINK_##name##_t *pHook; \
184  int n; \
185  APR_HOOK_INT_DCL_UD; \
186 \
187  APR_HOOK_PROBE_ENTRY(ud, ns, name, args_use); \
188 \
189  if(_hooks.link_##name) \
190  { \
191  pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
192  for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
193  { \
194  APR_HOOK_PROBE_INVOKE(ud, ns, name, (char *)pHook[n].szName, args_use); \
195  pHook[n].pFunc args_use; \
196  APR_HOOK_PROBE_COMPLETE(ud, ns, name, (char *)pHook[n].szName, 0, args_use); \
197  } \
198  } \
199 \
200  APR_HOOK_PROBE_RETURN(ud, ns, name, 0, args_use); \
201 \
202  }
203 
204 /* FIXME: note that this returns ok when nothing is run. I suspect it should
205  really return decline, but that breaks Apache currently - Ben
206 */
222 #define APR_IMPLEMENT_EXTERNAL_HOOK_RUN_ALL(ns,link,ret,name,args_decl,args_use,ok,decline) \
223 APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
224 link##_DECLARE(ret) ns##_run_##name args_decl \
225  { \
226  ns##_LINK_##name##_t *pHook; \
227  int n; \
228  ret rv = ok; \
229  APR_HOOK_INT_DCL_UD; \
230 \
231  APR_HOOK_PROBE_ENTRY(ud, ns, name, args_use); \
232 \
233  if(_hooks.link_##name) \
234  { \
235  pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
236  for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
237  { \
238  APR_HOOK_PROBE_INVOKE(ud, ns, name, (char *)pHook[n].szName, args_use); \
239  rv=pHook[n].pFunc args_use; \
240  APR_HOOK_PROBE_COMPLETE(ud, ns, name, (char *)pHook[n].szName, rv, args_use); \
241  if(rv != ok && rv != decline) \
242  break; \
243  rv = ok; \
244  } \
245  } \
246 \
247  APR_HOOK_PROBE_RETURN(ud, ns, name, rv, args_use); \
248 \
249  return rv; \
250  }
251 
252 
267 #define APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(ns,link,ret,name,args_decl,args_use,decline) \
268 APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
269 link##_DECLARE(ret) ns##_run_##name args_decl \
270  { \
271  ns##_LINK_##name##_t *pHook; \
272  int n; \
273  ret rv = decline; \
274  APR_HOOK_INT_DCL_UD; \
275 \
276  APR_HOOK_PROBE_ENTRY(ud, ns, name, args_use); \
277 \
278  if(_hooks.link_##name) \
279  { \
280  pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
281  for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
282  { \
283  APR_HOOK_PROBE_INVOKE(ud, ns, name, (char *)pHook[n].szName, args_use); \
284  rv=pHook[n].pFunc args_use; \
285  APR_HOOK_PROBE_COMPLETE(ud, ns, name, (char *)pHook[n].szName, rv, args_use); \
286 \
287  if(rv != decline) \
288  break; \
289  } \
290  } \
291 \
292  APR_HOOK_PROBE_RETURN(ud, ns, name, rv, args_use); \
293 \
294  return rv; \
295  }
296 
297  /* Hook orderings */
299 #define APR_HOOK_REALLY_FIRST (-10)
300 
301 #define APR_HOOK_FIRST 0
302 
303 #define APR_HOOK_MIDDLE 10
304 
305 #define APR_HOOK_LAST 20
306 
307 #define APR_HOOK_REALLY_LAST 30
308 
312 APU_DECLARE_DATA extern apr_pool_t *apr_hook_global_pool;
313 
318 APU_DECLARE_DATA extern int apr_hook_debug_enabled;
319 
323 APU_DECLARE_DATA extern const char *apr_hook_debug_current;
324 
330 APU_DECLARE(void) apr_hook_sort_register(const char *szHookName,
331  apr_array_header_t **aHooks);
335 APU_DECLARE(void) apr_hook_sort_all(void);
336 
344 APU_DECLARE(void) apr_hook_debug_show(const char *szName,
345  const char * const *aszPre,
346  const char * const *aszSucc);
347 
351 APU_DECLARE(void) apr_hook_deregister_all(void);
352 
354 #ifdef __cplusplus
355 }
356 #endif
357 
358 #endif /* APR_HOOKS_H */