apr_hooks.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. if (pTail) {
  162. pTail->pNext = NULL; /* unfudge the tail */
  163. }
  164. return pHead;
  165. }
  166. static apr_array_header_t *sort_hook(apr_array_header_t *pHooks,
  167. const char *szName)
  168. {
  169. apr_pool_t *p;
  170. TSort *pSort;
  171. apr_array_header_t *pNew;
  172. int n;
  173. apr_pool_create(&p, apr_hook_global_pool);
  174. pSort=prepare(p,(TSortData *)pHooks->elts,pHooks->nelts);
  175. pSort=tsort(pSort,pHooks->nelts);
  176. pNew=apr_array_make(apr_hook_global_pool,pHooks->nelts,sizeof(TSortData));
  177. if(apr_hook_debug_enabled)
  178. printf("Sorting %s:",szName);
  179. for(n=0 ; pSort ; pSort=pSort->pNext,++n) {
  180. TSortData *pHook;
  181. assert(n < pHooks->nelts);
  182. pHook=apr_array_push(pNew);
  183. memcpy(pHook,pSort->pData,sizeof *pHook);
  184. if(apr_hook_debug_enabled)
  185. printf(" %s",pHook->szName);
  186. }
  187. if(apr_hook_debug_enabled)
  188. fputc('\n',stdout);
  189. return pNew;
  190. }
  191. #ifndef NETWARE
  192. static apr_array_header_t *s_aHooksToSort;
  193. #endif
  194. typedef struct
  195. {
  196. const char *szHookName;
  197. apr_array_header_t **paHooks;
  198. } HookSortEntry;
  199. APU_DECLARE(void) apr_hook_sort_register(const char *szHookName,
  200. apr_array_header_t **paHooks)
  201. {
  202. #ifdef NETWARE
  203. get_apd
  204. #endif
  205. HookSortEntry *pEntry;
  206. if(!s_aHooksToSort)
  207. s_aHooksToSort=apr_array_make(apr_hook_global_pool,1,sizeof(HookSortEntry));
  208. pEntry=apr_array_push(s_aHooksToSort);
  209. pEntry->szHookName=szHookName;
  210. pEntry->paHooks=paHooks;
  211. }
  212. APU_DECLARE(void) apr_hook_sort_all(void)
  213. {
  214. #ifdef NETWARE
  215. get_apd
  216. #endif
  217. int n;
  218. for(n=0 ; n < s_aHooksToSort->nelts ; ++n) {
  219. HookSortEntry *pEntry=&((HookSortEntry *)s_aHooksToSort->elts)[n];
  220. *pEntry->paHooks=sort_hook(*pEntry->paHooks,pEntry->szHookName);
  221. }
  222. }
  223. #ifndef NETWARE
  224. static apr_hash_t *s_phOptionalHooks;
  225. static apr_hash_t *s_phOptionalFunctions;
  226. #endif
  227. APU_DECLARE(void) apr_hook_deregister_all(void)
  228. {
  229. #ifdef NETWARE
  230. get_apd
  231. #endif
  232. int n;
  233. for(n=0 ; n < s_aHooksToSort->nelts ; ++n) {
  234. HookSortEntry *pEntry=&((HookSortEntry *)s_aHooksToSort->elts)[n];
  235. *pEntry->paHooks=NULL;
  236. }
  237. s_aHooksToSort=NULL;
  238. s_phOptionalHooks=NULL;
  239. s_phOptionalFunctions=NULL;
  240. }
  241. APU_DECLARE(void) apr_hook_debug_show(const char *szName,
  242. const char * const *aszPre,
  243. const char * const *aszSucc)
  244. {
  245. int nFirst;
  246. printf(" Hooked %s",szName);
  247. if(aszPre) {
  248. fputs(" pre(",stdout);
  249. nFirst=1;
  250. while(*aszPre) {
  251. if(!nFirst)
  252. fputc(',',stdout);
  253. nFirst=0;
  254. fputs(*aszPre,stdout);
  255. ++aszPre;
  256. }
  257. fputc(')',stdout);
  258. }
  259. if(aszSucc) {
  260. fputs(" succ(",stdout);
  261. nFirst=1;
  262. while(*aszSucc) {
  263. if(!nFirst)
  264. fputc(',',stdout);
  265. nFirst=0;
  266. fputs(*aszSucc,stdout);
  267. ++aszSucc;
  268. }
  269. fputc(')',stdout);
  270. }
  271. fputc('\n',stdout);
  272. }
  273. /* Optional hook support */
  274. APR_DECLARE_EXTERNAL_HOOK(apr,APU,void,_optional,(void))
  275. APU_DECLARE(apr_array_header_t *) apr_optional_hook_get(const char *szName)
  276. {
  277. #ifdef NETWARE
  278. get_apd
  279. #endif
  280. apr_array_header_t **ppArray;
  281. if(!s_phOptionalHooks)
  282. return NULL;
  283. ppArray=apr_hash_get(s_phOptionalHooks,szName,strlen(szName));
  284. if(!ppArray)
  285. return NULL;
  286. return *ppArray;
  287. }
  288. APU_DECLARE(void) apr_optional_hook_add(const char *szName,void (*pfn)(void),
  289. const char * const *aszPre,
  290. const char * const *aszSucc,int nOrder)
  291. {
  292. #ifdef NETWARE
  293. get_apd
  294. #endif
  295. apr_array_header_t *pArray=apr_optional_hook_get(szName);
  296. apr_LINK__optional_t *pHook;
  297. if(!pArray) {
  298. apr_array_header_t **ppArray;
  299. pArray=apr_array_make(apr_hook_global_pool,1,
  300. sizeof(apr_LINK__optional_t));
  301. if(!s_phOptionalHooks)
  302. s_phOptionalHooks=apr_hash_make(apr_hook_global_pool);
  303. ppArray=apr_palloc(apr_hook_global_pool,sizeof *ppArray);
  304. *ppArray=pArray;
  305. apr_hash_set(s_phOptionalHooks,szName,strlen(szName),ppArray);
  306. apr_hook_sort_register(szName,ppArray);
  307. }
  308. pHook=apr_array_push(pArray);
  309. pHook->pFunc=pfn;
  310. pHook->aszPredecessors=aszPre;
  311. pHook->aszSuccessors=aszSucc;
  312. pHook->nOrder=nOrder;
  313. pHook->szName=apr_hook_debug_current;
  314. if(apr_hook_debug_enabled)
  315. apr_hook_debug_show(szName,aszPre,aszSucc);
  316. }
  317. /* optional function support */
  318. APU_DECLARE(apr_opt_fn_t *) apr_dynamic_fn_retrieve(const char *szName)
  319. {
  320. #ifdef NETWARE
  321. get_apd
  322. #endif
  323. if(!s_phOptionalFunctions)
  324. return NULL;
  325. return (void(*)(void))apr_hash_get(s_phOptionalFunctions,szName,strlen(szName));
  326. }
  327. /* Deprecated */
  328. APU_DECLARE_NONSTD(void) apr_dynamic_fn_register(const char *szName,
  329. apr_opt_fn_t *pfn)
  330. {
  331. #ifdef NETWARE
  332. get_apd
  333. #endif
  334. if(!s_phOptionalFunctions)
  335. s_phOptionalFunctions=apr_hash_make(apr_hook_global_pool);
  336. apr_hash_set(s_phOptionalFunctions,szName,strlen(szName),(void *)pfn);
  337. }
  338. #if 0
  339. void main()
  340. {
  341. const char *aszAPre[]={"b","c",NULL};
  342. const char *aszBPost[]={"a",NULL};
  343. const char *aszCPost[]={"b",NULL};
  344. TSortData t1[]=
  345. {
  346. { "a",aszAPre,NULL },
  347. { "b",NULL,aszBPost },
  348. { "c",NULL,aszCPost }
  349. };
  350. TSort *pResult;
  351. pResult=prepare(t1,3);
  352. pResult=tsort(pResult,3);
  353. for( ; pResult ; pResult=pResult->pNext)
  354. printf("%s\n",pResult->pData->szName);
  355. }
  356. #endif