echo ""; echo ""; echo ""; echo ""; if(!is_admin){echo "";} » Apache中的Hook(挂钩)剖析(2) 仲子说

2008-04-24

Apache中的Hook(挂钩)剖析(2)

Posted in Apache at 23:33 Author:仲远

标签:

Apache??????(2)

?????? ??:?? (2006-02-21 12:12:23)

5.5.5 ????(APR_IMPLEMENT_EXTERNAL_HOOK_BASE)

???????????????????????????????????????????????????????????????????

#define APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \

link##_DECLARE(void) ns##_hook_##name(ns##_HOOK_##name##_t *pf, \

const char * const *aszPre, const char * const *aszSucc,int nOrder) \

{ \

ns##_LINK_##name##_t *pHook; \

if(!_hooks.link_##name) \

{ \

_hooks.link_##name=apr_array_make(apr_hook_global_pool,1,sizeof(ns##_LINK_##name##_t)); \

apr_hook_sort_register(#name,&_hooks.link_##name); \

} \

pHook=apr_array_push(_hooks.link_##name); \

pHook->pFunc=pf; \

pHook->aszPredecessors=aszPre; \

pHook->aszSuccessors=aszSucc; \

pHook->nOrder=nOrder; \

pHook->szName=apr_hook_debug_current; \

if(apr_hook_debug_enabled) \

apr_hook_debug_show(#name,aszPre,aszSucc); \

} \

APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name) \

{ \

return _hooks.link_##name; \

}

??post_config???????????????

AP_DECLARE(int) ap_hook_post_config(ap_HOOK_post_config_t *pf,

const char * const *aszPre,

const char * const *aszSucc,

int nOrder)

{

ap_LINK_post_config_t *pHook;

if (!_hooks.link_post_config) {

_hooks.link_post_config = apr_array_make(apr_hook_global_pool, 1,

sizeof(ap_LINK_post_config_t));

apr_hook_sort_register(”post_config”, &_hooks.link_post_config);

}

pHook = apr_array_push(_hooks.link_post_config);

pHook->pFunc = pf;

pHook->aszPredecessors = aszPre;

pHook->aszSuccessors = aszSucc;

pHook->nOrder = nOrder;

pHook->szName = apr_hook_debug_current;

if (apr_hook_debug_enabled)

apr_hook_debug_show(”post_config”, aszPre, aszSucc);

}

AP_DECLARE(apr_array_header_t *) ap_hook_get_post_config(void) {

return _hooks.link_post_config;

}

???????????????????APR_IMPLEMENT_EXTERNAL_HOOK_BASE???????????????????????????

???????????????????????????APR_HOOK_STRUCT????????????????????????????????????????????????????????????????????????????????

5.5.6 ????

????????????????????????????????????????????????????????????Apache?????????????ap_run_HOOKNAME?????ap_run_post_config????post_config??????????????????????????????????????????????AP_IMPLEMENT_HOOK_VOID?AP_IMPLEMENT_HOOK_RUN_FIRST??AP_IMPLEMENT_HOOK_RUN_ALL?

(1)???AP_IMPLEMENT_HOOK_VOID???????????????????????????????????????????????????VOID???????????????????

#define APR_IMPLEMENT_EXTERNAL_HOOK_VOID(ns,link,name,args_decl,args_use) \

APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \

link##_DECLARE(void) ns##_run_##name args_decl \

{ \

ns##_LINK_##name##_t *pHook; \

int n; \

\

if(!_hooks.link_##name) \

return; \

\

pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \

for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \

pHook[n].pFunc args_use; \

}

????config.c??child_init??????VOID??????????

AP_IMPLEMENT_HOOK_VOID(child_init,

(apr_pool_t *pchild, server_rec *s),

(pchild, s))

??APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name)???????????????????????

AP_DECLARE(void) ap_run_child_init(apr_pool_t *pchild,server_rec* s)

{

ap_LINK_child_init_t pHook;

int n;

if(!_hooks.link_child_init)

return;

pHook=(ap_LINK_child_init_t)_hooks.link_child_init->elts;

for(n=0;n<_hooks.link_child_init->nelts;++n)

pHook[n].pFunc(pchild, s);

}

?????????????????????????????????????“????????”?

(2)?AP_IMPLEMENT_HOOK_ALL??ALL?????AP_IMPLEMENT_HOOK_VOID????????????ALL??????????????

#define APR_IMPLEMENT_EXTERNAL_HOOK_RUN_ALL(ns,link,ret,name,args_decl,args_use,ok,decline) \

APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \

link##_DECLARE(ret) ns##_run_##name args_decl \

{ \

ns##_LINK_##name##_t *pHook; \

int n; \

ret rv; \

\

if(!_hooks.link_##name) \

return ok; \

\

pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \

for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \

{ \

rv=pHook[n].pFunc args_use; \

\

if(rv != ok && rv != decline) \

return rv; \

} \

return ok; \

}

open_logs?????????????config.c???????

AP_IMPLEMENT_HOOK_RUN_ALL(int, open_logs,

(apr_pool_t *pconf, apr_pool_t *plog,

apr_pool_t *ptemp, server_rec *s),

(pconf, plog, ptemp, s), OK, DECLINED)

?????????????

AP_DECLARE(int) ap_run_open_logs(apr_pool_t *pconf, apr_pool_t *plog,

apr_pool_t *ptemp, server_rec *s)

{

ap_LINK_open_logs_t *pHook;

int n;

ret rv;

if(!_hooks.link_open_logs)

return ok;

pHook=(ap_LINK_open_logs_t *)_hooks.link_open_logs->elts; \

for(n=0 ; n < _hooks.link_open_logs->nelts ; ++n) \

{

rv=pHook[n].pFunc(pconf, plog, ptemp, s);

if(rv != ok && rv != decline) \

return rv;

}

return ok;

}

?????????ALL??????????????????????????“DECLINE”?????????????????????????????????

(3)?AP_IMPLEMENT_HOOK_FIRST???FIRST????????Apache?????????????????????????????????????????????????????????

#define APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(ns,link,ret,name,args_decl,args_use,decline) \

APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \

link##_DECLARE(ret) ns##_run_##name args_decl \

{ \

ns##_LINK_##name##_t *pHook; \

int n; \

ret rv; \

\

if(!_hooks.link_##name) \

return decline; \

\

pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \

for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \

{ \

rv=pHook[n].pFunc args_use; \

\

if(rv != decline) \

return rv; \

} \

return decline; \

}

quick_handler?????????????config.c??????

AP_IMPLEMENT_HOOK_RUN_FIRST(int, quick_handler, (request_rec *r, int lookup),

(r, lookup), DECLINED)

?????????????

AP_DECLARE(ret) ap_run_quick_handler(request_rec *r, int lookup)

{

ap_LINK_quick_handler_t *pHook;

int n;

ret rv;

if(!_hooks.link_quick_handler)

return decline;

pHook=(ap_LINK_quick_handler_t *)_hooks.link_quick_handler->elts;

for(n=0 ; n < _hooks.link_quick_handler->nelts ; ++n) \

{

rv=pHook[n].pFunc(r,look_up);

if(rv != decline)

return rv;

}

return decline;

}

??????????????????????????????rv!=decline???????????????????????????????????

???????????????????????????????????????????????????

5.5.6 ??????

????????????????????????????????????????????????????????????????????????????????????????????????Apache??????????some_hook????????

int some_hook(request_rec* r,int n);

???????????????????

(1)?????

????AP_DECLARE_HOOK????????some_hook?????????

AP_DECLARE_HOOK(int,some_hook,(request_rec* r,int n))

(2)???????

??some_hook?????????????????????????????????????????????????

APR_HOOK_STRUCT(

APR_HOOK_LINK(some_hook)

……

)

(3)?????????

????????????ap_run_name???????????????????????VOID??????FIRST??????ALL?????????some_hook???VOID??????????

AP_IMPLEMENT_HOOK_RUN_VOID(some_hook,(request_rec* r,int n),(r,n))

???????????????????????ap_run_some_hook?

(4)???????

?????????????????????????????????????????????????????som_module??????some_hook??????????????????????????ap_hook_some_hook????????

static void register_hooks()

{

……

ap_hook_some_hook(some_hook_function,NULL,NULL,HOOK_MIDDLE);

}

AP_DECLARE_DATA module core_module = {

……

register_hooks /* register hooks */

};

(5)???????

????????????????????????some_module?????????????????“Hello World”??????(4)??????????some_hook_function?????????????

static void some_hook_function(request_rec* r,int n)

{

ap_rputs(“Hello World\n”);

return;

}

??????????????????AP_IMPLEMENT_HOOK_RUN_XXX???????

本文可以自由转载,转载时请保留全文并注明出处:
转载自仲子说 [ http://www.wangzhongyuan.com/ ]
原文链接:

Leave a Comment

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word