2
0

apr_hooks.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. #include <assert.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include "apr_pools.h"
  20. #include "apr_tables.h"
  21. #include "apr.h"
  22. #include "apr_hooks.h"
  23. #include "apr_hash.h"
  24. #include "apr_optional_hooks.h"
  25. #include "apr_optional.h"
  26. #define APR_WANT_MEMFUNC
  27. #define APR_WANT_STRFUNC
  28. #include "apr_want.h"
  29. #if 0
  30. #define apr_palloc(pool,size) malloc(size)
  31. #endif
  32. APU_DECLARE_DATA apr_pool_t *apr_hook_global_pool = NULL;
  33. APU_DECLARE_DATA int apr_hook_debug_enabled = 0;
  34. APU_DECLARE_DATA const char *apr_hook_debug_current = NULL;
  35. /** @deprecated @see apr_hook_global_pool */
  36. APU_DECLARE_DATA apr_pool_t *apr_global_hook_pool = NULL;
  37. /** @deprecated @see apr_hook_debug_enabled */
  38. APU_DECLARE_DATA int apr_debug_module_hooks = 0;
  39. /** @deprecated @see apr_hook_debug_current */
  40. APU_DECLARE_DATA const char *apr_current_hooking_module = NULL;
  41. /* NB: This must echo the LINK_##name structure */
  42. typedef struct
  43. {
  44. void (*dummy)(void *);
  45. const char *szName;
  46. const char * const *aszPredecessors;
  47. const char * const *aszSuccessors;
  48. int nOrder;
  49. } TSortData;
  50. typedef struct tsort_
  51. {
  52. void *pData;
  53. int nPredecessors;
  54. struct tsort_ **ppPredecessors;
  55. struct tsort_ *pNext;
  56. } TSort;
  57. #ifdef NETWARE
  58. #include "apr_private.h"
  59. #define get_apd APP_DATA* apd = (APP_DATA*)get_app_data(gLibId);
  60. #define s_aHooksToSort ((apr_array_header_t *)(apd->gs_aHooksToSort))
  61. #define s_phOptionalHooks ((apr_hash_t *)(apd->gs_phOptionalHooks))
  62. #define s_phOptionalFunctions ((apr_hash_t *)(apd->gs_phOptionalFunctions))
  63. #endif
  64. static int crude_order(const void *a_,const void *b_)
  65. {
  66. const TSortData *a=a_;
  67. const TSortData *b=b_;
  68. return a->nOrder-b->nOrder;
  69. }
  70. static TSort *prepare(apr_pool_t *p,TSortData *pItems,int nItems)
  71. {
  72. TSort *pData=apr_palloc(p,nItems*sizeof *pData);
  73. int n;
  74. qsort(pItems,nItems,sizeof *pItems,crude_order);
  75. for(n=0 ; n < nItems ; ++n) {
  76. pData[n].nPredecessors=0;
  77. pData[n].ppPredecessors=apr_pcalloc(p,nItems*sizeof *pData[n].ppPredecessors);
  78. pData[n].pNext=NULL;
  79. pData[n].pData=&pItems[n];
  80. }
  81. for(n=0 ; n < nItems ; ++n) {
  82. int i,k;
  83. for(i=0 ; pItems[n].aszPredecessors && pItems[n].aszPredecessors[i] ; ++i)
  84. for(k=0 ; k < nItems ; ++k)
  85. if(!strcmp(pItems[k].szName,pItems[n].aszPredecessors[i])) {
  86. int l;
  87. for(l=0 ; l < pData[n].nPredecessors ; ++l)
  88. if(pData[n].ppPredecessors[l] == &pData[k])
  89. goto got_it;
  90. pData[n].ppPredecessors[pData[n].nPredecessors]=&pData[k];
  91. ++pData[n].nPredecessors;
  92. got_it:
  93. break;
  94. }
  95. for(i=0 ; pItems[n].aszSuccessors && pItems[n].aszSuccessors[i] ; ++i)
  96. for(k=0 ; k < nItems ; ++k)
  97. if(!strcmp(pItems[k].szName,pItems[n].aszSuccessors[i])) {
  98. int l;
  99. for(l=0 ; l < pData[k].nPredecessors ; ++l)
  100. if(pData[k].ppPredecessors[l] == &pData[n])
  101. goto got_it2;
  102. pData[k].ppPredecessors[pData[k].nPredecessors]=&pData[n];
  103. ++pData[k].nPredecessors;
  104. got_it2:
  105. break;
  106. }
  107. }
  108. return pData;
  109. }
  110. /* Topologically sort, dragging out-of-order items to the front. Note that
  111. this tends to preserve things that want to be near the front better, and
  112. changing that behaviour might compromise some of Apache's behaviour (in
  113. particular, mod_log_forensic might otherwise get pushed to the end, and
  114. core.c's log open function used to end up at the end when pushing items
  115. to the back was the methedology). Also note that the algorithm could
  116. go back to its original simplicity by sorting from the back instead of
  117. the front.
  118. */
  119. static TSort *tsort(TSort *pData,int nItems)
  120. {
  121. int nTotal;
  122. TSort *pHead=NULL;
  123. TSort *pTail=NULL;
  124. for(nTotal=0 ; nTotal < nItems ; ++nTotal) {
  125. int n,i,k;
  126. for(n=0 ; ; ++n) {
  127. if(n == nItems)
  128. assert(0); /* we have a loop... */
  129. if(!pData[n].pNext) {
  130. if(pData[n].nPredecessors) {
  131. for(k=0 ; ; ++k) {
  132. assert(k < nItems);
  133. if(pData[n].ppPredecessors[k])
  134. break;
  135. }
  136. for(i=0 ; ; ++i) {
  137. assert(i < nItems);
  138. if(&pData[i] == pData[n].ppPredecessors[k]) {
  139. n=i-1;
  140. break;
  141. }
  142. }
  143. } else
  144. break;
  145. }
  146. }
  147. if(pTail)
  148. pTail->pNext=&pData[n];
  149. else
  150. pHead=&pData[n];
  151. pTail=&pData[n];
  152. pTail->pNext=pTail; /* fudge it so it looks linked */
  153. for(i=0 ; i < nItems ; ++i)
  154. for(k=0 ; k < nItems ; ++k)
  155. if(pData[i].ppPredecessors[k] == &pData[n]) {
  156. --pData[i].nPredecessors;
  157. pData[i].ppPredecessors[k]=NULL;
  158. break;
  159. }
  160. }
  161. pTail->pNext=NULL; /* unfudge the tail */
  162. return pHead;
  163. }
  164. static apr_array_header_t *sort_hook(apr_array_header_t *pHooks,
  165. const char *szName)
  166. {
  167. apr_pool_t *p;
  168. TSort *pSort;
  169. apr_array_header_t *pNew;
  170. int n;
  171. apr_pool_create(&p, apr_hook_global_pool);
  172. pSort=prepare(p,(TSortData *)pHooks->elts,pHooks->nelts);
  173. pSort=tsort(pSort,pHooks->nelts);
  174. pNew=apr_array_make(apr_hook_global_pool,pHooks->nelts,sizeof(TSortData));
  175. if(apr_hook_debug_enabled)
  176. printf("Sorting %s:",szName);
  177. for(n=0 ; pSort ; pSort=pSort->pNext,++n) {
  178. TSortData *pHook;
  179. assert(n < pHooks->nelts);
  180. pHook=apr_array_push(pNew);
  181. memcpy(pHook,pSort->pData,sizeof *pHook);
  182. if(apr_hook_debug_enabled)
  183. printf(" %s",pHook->szName);
  184. }
  185. if(apr_hook_debug_enabled)
  186. fputc('\n',stdout);
  187. return pNew;
  188. }
  189. #ifndef NETWARE
  190. static apr_array_header_t *s_aHooksToSort;
  191. #endif
  192. typedef struct
  193. {
  194. const char *szHookName;
  195. apr_array_header_t **paHooks;
  196. } HookSortEntry;
  197. APU_DECLARE(void) apr_hook_sort_register(const char *szHookName,
  198. apr_array_header_t **paHooks)
  199. {
  200. #ifdef NETWARE
  201. get_apd
  202. #endif
  203. HookSortEntry *pEntry;
  204. if(!s_aHooksToSort)
  205. s_aHooksToSort=apr_array_make(apr_hook_global_pool,1,sizeof(HookSortEntry));
  206. pEntry=apr_array_push(s_aHooksToSort);
  207. pEntry->szHookName=szHookName;
  208. pEntry->paHooks=paHooks;
  209. }
  210. APU_DECLARE(void) apr_hook_sort_all(void)
  211. {
  212. #ifdef NETWARE
  213. get_apd
  214. #endif
  215. int n;
  216. for(n=0 ; n < s_aHooksToSort->nelts ; ++n) {
  217. HookSortEntry *pEntry=&((HookSortEntry *)s_aHooksToSort->elts)[n];
  218. *pEntry->paHooks=sort_hook(*pEntry->paHooks,pEntry->szHookName);
  219. }
  220. }
  221. #ifndef NETWARE
  222. static apr_hash_t *s_phOptionalHooks;
  223. static apr_hash_t *s_phOptionalFunctions;
  224. #endif
  225. APU_DECLARE(void) apr_hook_deregister_all(void)
  226. {
  227. #ifdef NETWARE
  228. get_apd
  229. #endif
  230. int n;
  231. for(n=0 ; n < s_aHooksToSort->nelts ; ++n) {
  232. HookSortEntry *pEntry=&((HookSortEntry *)s_aHooksToSort->elts)[n];
  233. *pEntry->paHooks=NULL;
  234. }
  235. s_aHooksToSort=NULL;
  236. s_phOptionalHooks=NULL;
  237. s_phOptionalFunctions=NULL;
  238. }
  239. APU_DECLARE(void) apr_hook_debug_show(const char *szName,
  240. const char * const *aszPre,
  241. const char * const *aszSucc)
  242. {
  243. int nFirst;
  244. printf(" Hooked %s",szName);
  245. if(aszPre) {
  246. fputs(" pre(",stdout);
  247. nFirst=1;
  248. while(*aszPre) {
  249. if(!nFirst)
  250. fputc(',',stdout);
  251. nFirst=0;
  252. fputs(*aszPre,stdout);
  253. ++aszPre;
  254. }
  255. fputc(')',stdout);
  256. }
  257. if(aszSucc) {
  258. fputs(" succ(",stdout);
  259. nFirst=1;
  260. while(*aszSucc) {
  261. if(!nFirst)
  262. fputc(',',stdout);
  263. nFirst=0;
  264. fputs(*aszSucc,stdout);
  265. ++aszSucc;
  266. }
  267. fputc(')',stdout);
  268. }
  269. fputc('\n',stdout);
  270. }
  271. /* Optional hook support */
  272. APR_DECLARE_EXTERNAL_HOOK(apr,APU,void,_optional,(void))
  273. APU_DECLARE(apr_array_header_t *) apr_optional_hook_get(const char *szName)
  274. {
  275. #ifdef NETWARE
  276. get_apd
  277. #endif
  278. apr_array_header_t **ppArray;
  279. if(!s_phOptionalHooks)
  280. return NULL;
  281. ppArray=apr_hash_get(s_phOptionalHooks,szName,strlen(szName));
  282. if(!ppArray)
  283. return NULL;
  284. return *ppArray;
  285. }
  286. APU_DECLARE(void) apr_optional_hook_add(const char *szName,void (*pfn)(void),
  287. const char * const *aszPre,
  288. const char * const *aszSucc,int nOrder)
  289. {
  290. #ifdef NETWARE
  291. get_apd
  292. #endif
  293. apr_array_header_t *pArray=apr_optional_hook_get(szName);
  294. apr_LINK__optional_t *pHook;
  295. if(!pArray) {
  296. apr_array_header_t **ppArray;
  297. pArray=apr_array_make(apr_hook_global_pool,1,
  298. sizeof(apr_LINK__optional_t));
  299. if(!s_phOptionalHooks)
  300. s_phOptionalHooks=apr_hash_make(apr_hook_global_pool);
  301. ppArray=apr_palloc(apr_hook_global_pool,sizeof *ppArray);
  302. *ppArray=pArray;
  303. apr_hash_set(s_phOptionalHooks,szName,strlen(szName),ppArray);
  304. apr_hook_sort_register(szName,ppArray);
  305. }
  306. pHook=apr_array_push(pArray);
  307. pHook->pFunc=pfn;
  308. pHook->aszPredecessors=aszPre;
  309. pHook->aszSuccessors=aszSucc;
  310. pHook->nOrder=nOrder;
  311. pHook->szName=apr_hook_debug_current;
  312. if(apr_hook_debug_enabled)
  313. apr_hook_debug_show(szName,aszPre,aszSucc);
  314. }
  315. /* optional function support */
  316. APU_DECLARE(apr_opt_fn_t *) apr_dynamic_fn_retrieve(const char *szName)
  317. {
  318. #ifdef NETWARE
  319. get_apd
  320. #endif
  321. if(!s_phOptionalFunctions)
  322. return NULL;
  323. return (void(*)(void))apr_hash_get(s_phOptionalFunctions,szName,strlen(szName));
  324. }
  325. /* Deprecated */
  326. APU_DECLARE_NONSTD(void) apr_dynamic_fn_register(const char *szName,
  327. apr_opt_fn_t *pfn)
  328. {
  329. #ifdef NETWARE
  330. get_apd
  331. #endif
  332. if(!s_phOptionalFunctions)
  333. s_phOptionalFunctions=apr_hash_make(apr_hook_global_pool);
  334. apr_hash_set(s_phOptionalFunctions,szName,strlen(szName),(void *)pfn);
  335. }
  336. #if 0
  337. void main()
  338. {
  339. const char *aszAPre[]={"b","c",NULL};
  340. const char *aszBPost[]={"a",NULL};
  341. const char *aszCPost[]={"b",NULL};
  342. TSortData t1[]=
  343. {
  344. { "a",aszAPre,NULL },
  345. { "b",NULL,aszBPost },
  346. { "c",NULL,aszCPost }
  347. };
  348. TSort *pResult;
  349. pResult=prepare(t1,3);
  350. pResult=tsort(pResult,3);
  351. for( ; pResult ; pResult=pResult->pNext)
  352. printf("%s\n",pResult->pData->szName);
  353. }
  354. #endif