2
0

apr_pools.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333
  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. #include "apr.h"
  17. #include "apr_private.h"
  18. #include "apr_atomic.h"
  19. #include "apr_portable.h" /* for get_os_proc */
  20. #include "apr_strings.h"
  21. #include "apr_general.h"
  22. #include "apr_pools.h"
  23. #include "apr_allocator.h"
  24. #include "apr_lib.h"
  25. #include "apr_thread_mutex.h"
  26. #include "apr_hash.h"
  27. #include "apr_time.h"
  28. #define APR_WANT_MEMFUNC
  29. #include "apr_want.h"
  30. #include "apr_env.h"
  31. #if APR_HAVE_STDLIB_H
  32. #include <stdlib.h> /* for malloc, free and abort */
  33. #endif
  34. #if APR_HAVE_UNISTD_H
  35. #include <unistd.h> /* for getpid */
  36. #endif
  37. /*
  38. * Magic numbers
  39. */
  40. #define MIN_ALLOC 8192
  41. #define MAX_INDEX 20
  42. #define BOUNDARY_INDEX 12
  43. #define BOUNDARY_SIZE (1 << BOUNDARY_INDEX)
  44. /*
  45. * Timing constants for killing subprocesses
  46. * There is a total 3-second delay between sending a SIGINT
  47. * and sending of the final SIGKILL.
  48. * TIMEOUT_INTERVAL should be set to TIMEOUT_USECS / 64
  49. * for the exponetial timeout alogrithm.
  50. */
  51. #define TIMEOUT_USECS 3000000
  52. #define TIMEOUT_INTERVAL 46875
  53. /*
  54. * Allocator
  55. */
  56. struct apr_allocator_t {
  57. apr_uint32_t max_index;
  58. apr_uint32_t max_free_index;
  59. apr_uint32_t current_free_index;
  60. #if APR_HAS_THREADS
  61. apr_thread_mutex_t *mutex;
  62. #endif /* APR_HAS_THREADS */
  63. apr_pool_t *owner;
  64. apr_memnode_t *free[MAX_INDEX];
  65. };
  66. #define SIZEOF_ALLOCATOR_T APR_ALIGN_DEFAULT(sizeof(apr_allocator_t))
  67. /*
  68. * Allocator
  69. */
  70. APR_DECLARE(apr_status_t) apr_allocator_create(apr_allocator_t **allocator)
  71. {
  72. apr_allocator_t *new_allocator;
  73. *allocator = NULL;
  74. if ((new_allocator = malloc(SIZEOF_ALLOCATOR_T)) == NULL)
  75. return APR_ENOMEM;
  76. memset(new_allocator, 0, SIZEOF_ALLOCATOR_T);
  77. new_allocator->max_free_index = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
  78. *allocator = new_allocator;
  79. return APR_SUCCESS;
  80. }
  81. APR_DECLARE(void) apr_allocator_destroy(apr_allocator_t *allocator)
  82. {
  83. apr_uint32_t index;
  84. apr_memnode_t *node, **ref;
  85. for (index = 0; index < MAX_INDEX; index++) {
  86. ref = &allocator->free[index];
  87. while ((node = *ref) != NULL) {
  88. *ref = node->next;
  89. free(node);
  90. }
  91. }
  92. free(allocator);
  93. }
  94. #if APR_HAS_THREADS
  95. APR_DECLARE(void) apr_allocator_mutex_set(apr_allocator_t *allocator,
  96. apr_thread_mutex_t *mutex)
  97. {
  98. allocator->mutex = mutex;
  99. }
  100. APR_DECLARE(apr_thread_mutex_t *) apr_allocator_mutex_get(
  101. apr_allocator_t *allocator)
  102. {
  103. return allocator->mutex;
  104. }
  105. #endif /* APR_HAS_THREADS */
  106. APR_DECLARE(void) apr_allocator_owner_set(apr_allocator_t *allocator,
  107. apr_pool_t *pool)
  108. {
  109. allocator->owner = pool;
  110. }
  111. APR_DECLARE(apr_pool_t *) apr_allocator_owner_get(apr_allocator_t *allocator)
  112. {
  113. return allocator->owner;
  114. }
  115. APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator,
  116. apr_size_t in_size)
  117. {
  118. apr_uint32_t max_free_index;
  119. apr_uint32_t size = (APR_UINT32_TRUNC_CAST)in_size;
  120. #if APR_HAS_THREADS
  121. apr_thread_mutex_t *mutex;
  122. mutex = apr_allocator_mutex_get(allocator);
  123. if (mutex != NULL)
  124. apr_thread_mutex_lock(mutex);
  125. #endif /* APR_HAS_THREADS */
  126. max_free_index = APR_ALIGN(size, BOUNDARY_SIZE) >> BOUNDARY_INDEX;
  127. allocator->current_free_index += max_free_index;
  128. allocator->current_free_index -= allocator->max_free_index;
  129. allocator->max_free_index = max_free_index;
  130. if (allocator->current_free_index > max_free_index)
  131. allocator->current_free_index = max_free_index;
  132. #if APR_HAS_THREADS
  133. if (mutex != NULL)
  134. apr_thread_mutex_unlock(mutex);
  135. #endif
  136. }
  137. static APR_INLINE
  138. apr_memnode_t *allocator_alloc(apr_allocator_t *allocator, apr_size_t size)
  139. {
  140. apr_memnode_t *node, **ref;
  141. apr_uint32_t max_index;
  142. apr_size_t i, index;
  143. /* Round up the block size to the next boundary, but always
  144. * allocate at least a certain size (MIN_ALLOC).
  145. */
  146. size = APR_ALIGN(size + APR_MEMNODE_T_SIZE, BOUNDARY_SIZE);
  147. if (size < MIN_ALLOC)
  148. size = MIN_ALLOC;
  149. /* Find the index for this node size by
  150. * dividing its size by the boundary size
  151. */
  152. index = (size >> BOUNDARY_INDEX) - 1;
  153. if (index > APR_UINT32_MAX) {
  154. return NULL;
  155. }
  156. /* First see if there are any nodes in the area we know
  157. * our node will fit into.
  158. */
  159. if (index <= allocator->max_index) {
  160. #if APR_HAS_THREADS
  161. if (allocator->mutex)
  162. apr_thread_mutex_lock(allocator->mutex);
  163. #endif /* APR_HAS_THREADS */
  164. /* Walk the free list to see if there are
  165. * any nodes on it of the requested size
  166. *
  167. * NOTE: an optimization would be to check
  168. * allocator->free[index] first and if no
  169. * node is present, directly use
  170. * allocator->free[max_index]. This seems
  171. * like overkill though and could cause
  172. * memory waste.
  173. */
  174. max_index = allocator->max_index;
  175. ref = &allocator->free[index];
  176. i = index;
  177. while (*ref == NULL && i < max_index) {
  178. ref++;
  179. i++;
  180. }
  181. if ((node = *ref) != NULL) {
  182. /* If we have found a node and it doesn't have any
  183. * nodes waiting in line behind it _and_ we are on
  184. * the highest available index, find the new highest
  185. * available index
  186. */
  187. if ((*ref = node->next) == NULL && i >= max_index) {
  188. do {
  189. ref--;
  190. max_index--;
  191. }
  192. while (*ref == NULL && max_index > 0);
  193. allocator->max_index = max_index;
  194. }
  195. allocator->current_free_index += node->index;
  196. if (allocator->current_free_index > allocator->max_free_index)
  197. allocator->current_free_index = allocator->max_free_index;
  198. #if APR_HAS_THREADS
  199. if (allocator->mutex)
  200. apr_thread_mutex_unlock(allocator->mutex);
  201. #endif /* APR_HAS_THREADS */
  202. node->next = NULL;
  203. node->first_avail = (char *)node + APR_MEMNODE_T_SIZE;
  204. return node;
  205. }
  206. #if APR_HAS_THREADS
  207. if (allocator->mutex)
  208. apr_thread_mutex_unlock(allocator->mutex);
  209. #endif /* APR_HAS_THREADS */
  210. }
  211. /* If we found nothing, seek the sink (at index 0), if
  212. * it is not empty.
  213. */
  214. else if (allocator->free[0]) {
  215. #if APR_HAS_THREADS
  216. if (allocator->mutex)
  217. apr_thread_mutex_lock(allocator->mutex);
  218. #endif /* APR_HAS_THREADS */
  219. /* Walk the free list to see if there are
  220. * any nodes on it of the requested size
  221. */
  222. ref = &allocator->free[0];
  223. while ((node = *ref) != NULL && index > node->index)
  224. ref = &node->next;
  225. if (node) {
  226. *ref = node->next;
  227. allocator->current_free_index += node->index;
  228. if (allocator->current_free_index > allocator->max_free_index)
  229. allocator->current_free_index = allocator->max_free_index;
  230. #if APR_HAS_THREADS
  231. if (allocator->mutex)
  232. apr_thread_mutex_unlock(allocator->mutex);
  233. #endif /* APR_HAS_THREADS */
  234. node->next = NULL;
  235. node->first_avail = (char *)node + APR_MEMNODE_T_SIZE;
  236. return node;
  237. }
  238. #if APR_HAS_THREADS
  239. if (allocator->mutex)
  240. apr_thread_mutex_unlock(allocator->mutex);
  241. #endif /* APR_HAS_THREADS */
  242. }
  243. /* If we haven't got a suitable node, malloc a new one
  244. * and initialize it.
  245. */
  246. if ((node = malloc(size)) == NULL)
  247. return NULL;
  248. node->next = NULL;
  249. node->index = (APR_UINT32_TRUNC_CAST)index;
  250. node->first_avail = (char *)node + APR_MEMNODE_T_SIZE;
  251. node->endp = (char *)node + size;
  252. return node;
  253. }
  254. static APR_INLINE
  255. void allocator_free(apr_allocator_t *allocator, apr_memnode_t *node)
  256. {
  257. apr_memnode_t *next, *freelist = NULL;
  258. apr_uint32_t index, max_index;
  259. apr_uint32_t max_free_index, current_free_index;
  260. #if APR_HAS_THREADS
  261. if (allocator->mutex)
  262. apr_thread_mutex_lock(allocator->mutex);
  263. #endif /* APR_HAS_THREADS */
  264. max_index = allocator->max_index;
  265. max_free_index = allocator->max_free_index;
  266. current_free_index = allocator->current_free_index;
  267. /* Walk the list of submitted nodes and free them one by one,
  268. * shoving them in the right 'size' buckets as we go.
  269. */
  270. do {
  271. next = node->next;
  272. index = node->index;
  273. if (max_free_index != APR_ALLOCATOR_MAX_FREE_UNLIMITED
  274. && index > current_free_index) {
  275. node->next = freelist;
  276. freelist = node;
  277. }
  278. else if (index < MAX_INDEX) {
  279. /* Add the node to the appropiate 'size' bucket. Adjust
  280. * the max_index when appropiate.
  281. */
  282. if ((node->next = allocator->free[index]) == NULL
  283. && index > max_index) {
  284. max_index = index;
  285. }
  286. allocator->free[index] = node;
  287. current_free_index -= index;
  288. }
  289. else {
  290. /* This node is too large to keep in a specific size bucket,
  291. * just add it to the sink (at index 0).
  292. */
  293. node->next = allocator->free[0];
  294. allocator->free[0] = node;
  295. current_free_index -= index;
  296. }
  297. } while ((node = next) != NULL);
  298. allocator->max_index = max_index;
  299. allocator->current_free_index = current_free_index;
  300. #if APR_HAS_THREADS
  301. if (allocator->mutex)
  302. apr_thread_mutex_unlock(allocator->mutex);
  303. #endif /* APR_HAS_THREADS */
  304. while (freelist != NULL) {
  305. node = freelist;
  306. freelist = node->next;
  307. free(node);
  308. }
  309. }
  310. APR_DECLARE(apr_memnode_t *) apr_allocator_alloc(apr_allocator_t *allocator,
  311. apr_size_t size)
  312. {
  313. return allocator_alloc(allocator, size);
  314. }
  315. APR_DECLARE(void) apr_allocator_free(apr_allocator_t *allocator,
  316. apr_memnode_t *node)
  317. {
  318. allocator_free(allocator, node);
  319. }
  320. /*
  321. * Debug level
  322. */
  323. #define APR_POOL_DEBUG_GENERAL 0x01
  324. #define APR_POOL_DEBUG_VERBOSE 0x02
  325. #define APR_POOL_DEBUG_LIFETIME 0x04
  326. #define APR_POOL_DEBUG_OWNER 0x08
  327. #define APR_POOL_DEBUG_VERBOSE_ALLOC 0x10
  328. #define APR_POOL_DEBUG_VERBOSE_ALL (APR_POOL_DEBUG_VERBOSE \
  329. | APR_POOL_DEBUG_VERBOSE_ALLOC)
  330. /*
  331. * Structures
  332. */
  333. typedef struct cleanup_t cleanup_t;
  334. /** A list of processes */
  335. struct process_chain {
  336. /** The process ID */
  337. apr_proc_t *proc;
  338. apr_kill_conditions_e kill_how;
  339. /** The next process in the list */
  340. struct process_chain *next;
  341. };
  342. #if APR_POOL_DEBUG
  343. typedef struct debug_node_t debug_node_t;
  344. struct debug_node_t {
  345. debug_node_t *next;
  346. apr_uint32_t index;
  347. void *beginp[64];
  348. void *endp[64];
  349. };
  350. #define SIZEOF_DEBUG_NODE_T APR_ALIGN_DEFAULT(sizeof(debug_node_t))
  351. #endif /* APR_POOL_DEBUG */
  352. /* The ref field in the apr_pool_t struct holds a
  353. * pointer to the pointer referencing this pool.
  354. * It is used for parent, child, sibling management.
  355. * Look at apr_pool_create_ex() and apr_pool_destroy()
  356. * to see how it is used.
  357. */
  358. struct apr_pool_t {
  359. apr_pool_t *parent;
  360. apr_pool_t *child;
  361. apr_pool_t *sibling;
  362. apr_pool_t **ref;
  363. cleanup_t *cleanups;
  364. cleanup_t *free_cleanups;
  365. apr_allocator_t *allocator;
  366. struct process_chain *subprocesses;
  367. apr_abortfunc_t abort_fn;
  368. apr_hash_t *user_data;
  369. const char *tag;
  370. #if APR_HAS_THREADS
  371. apr_thread_mutex_t *user_mutex;
  372. #endif
  373. #if !APR_POOL_DEBUG
  374. apr_memnode_t *active;
  375. apr_memnode_t *self; /* The node containing the pool itself */
  376. char *self_first_avail;
  377. #else /* APR_POOL_DEBUG */
  378. apr_pool_t *joined; /* the caller has guaranteed that this pool
  379. * will survive as long as ->joined */
  380. debug_node_t *nodes;
  381. const char *file_line;
  382. apr_uint32_t creation_flags;
  383. unsigned int stat_alloc;
  384. unsigned int stat_total_alloc;
  385. unsigned int stat_clear;
  386. #if APR_HAS_THREADS
  387. apr_os_thread_t owner;
  388. apr_thread_mutex_t *mutex;
  389. #endif /* APR_HAS_THREADS */
  390. #endif /* APR_POOL_DEBUG */
  391. #ifdef NETWARE
  392. apr_os_proc_t owner_proc;
  393. #endif /* defined(NETWARE) */
  394. };
  395. #define SIZEOF_POOL_T APR_ALIGN_DEFAULT(sizeof(apr_pool_t))
  396. /*
  397. * Variables
  398. */
  399. static apr_byte_t apr_pools_initialized = 0;
  400. static apr_pool_t *global_pool = NULL;
  401. #if !APR_POOL_DEBUG
  402. static apr_allocator_t *global_allocator = NULL;
  403. #endif /* !APR_POOL_DEBUG */
  404. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  405. static apr_file_t *file_stderr = NULL;
  406. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  407. /*
  408. * Local functions
  409. */
  410. static void run_cleanups(cleanup_t **c);
  411. static void run_child_cleanups(cleanup_t **c);
  412. static void free_proc_chain(struct process_chain *procs);
  413. #if APR_POOL_DEBUG
  414. static void pool_destroy_debug(apr_pool_t *pool, const char *file_line);
  415. #endif
  416. #if !APR_POOL_DEBUG
  417. /*
  418. * Initialization
  419. */
  420. APR_DECLARE(apr_status_t) apr_pool_initialize(void)
  421. {
  422. apr_status_t rv;
  423. if (apr_pools_initialized++)
  424. return APR_SUCCESS;
  425. if ((rv = apr_allocator_create(&global_allocator)) != APR_SUCCESS) {
  426. apr_pools_initialized = 0;
  427. return rv;
  428. }
  429. if ((rv = apr_pool_create_ex(&global_pool, NULL, NULL,
  430. global_allocator)) != APR_SUCCESS) {
  431. apr_allocator_destroy(global_allocator);
  432. global_allocator = NULL;
  433. apr_pools_initialized = 0;
  434. return rv;
  435. }
  436. apr_pool_tag(global_pool, "apr_global_pool");
  437. /* This has to happen here because mutexes might be backed by
  438. * atomics. It used to be snug and safe in apr_initialize().
  439. */
  440. if ((rv = apr_atomic_init(global_pool)) != APR_SUCCESS) {
  441. return rv;
  442. }
  443. #if APR_HAS_THREADS
  444. {
  445. apr_thread_mutex_t *mutex;
  446. if ((rv = apr_thread_mutex_create(&mutex,
  447. APR_THREAD_MUTEX_DEFAULT,
  448. global_pool)) != APR_SUCCESS) {
  449. return rv;
  450. }
  451. apr_allocator_mutex_set(global_allocator, mutex);
  452. }
  453. #endif /* APR_HAS_THREADS */
  454. apr_allocator_owner_set(global_allocator, global_pool);
  455. return APR_SUCCESS;
  456. }
  457. APR_DECLARE(void) apr_pool_terminate(void)
  458. {
  459. if (!apr_pools_initialized)
  460. return;
  461. if (--apr_pools_initialized)
  462. return;
  463. apr_pool_destroy(global_pool); /* This will also destroy the mutex */
  464. global_pool = NULL;
  465. global_allocator = NULL;
  466. }
  467. /* Node list management helper macros; list_insert() inserts 'node'
  468. * before 'point'. */
  469. #define list_insert(node, point) do { \
  470. node->ref = point->ref; \
  471. *node->ref = node; \
  472. node->next = point; \
  473. point->ref = &node->next; \
  474. } while (0)
  475. /* list_remove() removes 'node' from its list. */
  476. #define list_remove(node) do { \
  477. *node->ref = node->next; \
  478. node->next->ref = node->ref; \
  479. } while (0)
  480. /*
  481. * Memory allocation
  482. */
  483. APR_DECLARE(void *) apr_palloc(apr_pool_t *pool, apr_size_t size)
  484. {
  485. apr_memnode_t *active, *node;
  486. void *mem = NULL;
  487. apr_size_t free_index;
  488. #if APR_HAS_THREADS
  489. if (pool->user_mutex) apr_thread_mutex_lock(pool->user_mutex);
  490. #endif
  491. size = APR_ALIGN_DEFAULT(size);
  492. active = pool->active;
  493. /* If the active node has enough bytes left, use it. */
  494. if (size < (apr_size_t)(active->endp - active->first_avail)) {
  495. mem = active->first_avail;
  496. active->first_avail += size;
  497. goto end;
  498. }
  499. node = active->next;
  500. if (size < (apr_size_t)(node->endp - node->first_avail)) {
  501. list_remove(node);
  502. }
  503. else {
  504. if ((node = allocator_alloc(pool->allocator, size)) == NULL) {
  505. if (pool->abort_fn)
  506. pool->abort_fn(APR_ENOMEM);
  507. mem = NULL;
  508. goto end;
  509. }
  510. }
  511. node->free_index = 0;
  512. mem = node->first_avail;
  513. node->first_avail += size;
  514. list_insert(node, active);
  515. pool->active = node;
  516. free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
  517. BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
  518. active->free_index = (APR_UINT32_TRUNC_CAST)free_index;
  519. node = active->next;
  520. if (free_index >= node->free_index)
  521. goto end;
  522. do {
  523. node = node->next;
  524. }
  525. while (free_index < node->free_index);
  526. list_remove(active);
  527. list_insert(active, node);
  528. end:
  529. #if APR_HAS_THREADS
  530. if (pool->user_mutex) apr_thread_mutex_unlock(pool->user_mutex);
  531. #endif
  532. return mem;
  533. }
  534. /* Provide an implementation of apr_pcalloc for backward compatibility
  535. * with code built before apr_pcalloc was a macro
  536. */
  537. #ifdef apr_pcalloc
  538. #undef apr_pcalloc
  539. #endif
  540. APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size);
  541. APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size)
  542. {
  543. void *mem;
  544. size = APR_ALIGN_DEFAULT(size);
  545. if ((mem = apr_palloc(pool, size)) != NULL) {
  546. memset(mem, 0, size);
  547. }
  548. return mem;
  549. }
  550. /*
  551. * Pool creation/destruction
  552. */
  553. APR_DECLARE(void) apr_pool_clear(apr_pool_t *pool)
  554. {
  555. apr_memnode_t *active;
  556. #if APR_HAS_THREADS
  557. if (pool->user_mutex) apr_thread_mutex_lock(pool->user_mutex);
  558. #endif
  559. /* Destroy the subpools. The subpools will detach themselves from
  560. * this pool thus this loop is safe and easy.
  561. */
  562. while (pool->child)
  563. apr_pool_destroy(pool->child);
  564. /* Run cleanups */
  565. run_cleanups(&pool->cleanups);
  566. pool->cleanups = NULL;
  567. pool->free_cleanups = NULL;
  568. /* Free subprocesses */
  569. free_proc_chain(pool->subprocesses);
  570. pool->subprocesses = NULL;
  571. /* Clear the user data. */
  572. pool->user_data = NULL;
  573. /* Find the node attached to the pool structure, reset it, make
  574. * it the active node and free the rest of the nodes.
  575. */
  576. active = pool->active = pool->self;
  577. active->first_avail = pool->self_first_avail;
  578. if (active->next == active)
  579. goto end;
  580. *active->ref = NULL;
  581. allocator_free(pool->allocator, active->next);
  582. active->next = active;
  583. active->ref = &active->next;
  584. end:
  585. #if APR_HAS_THREADS
  586. if (pool->user_mutex) apr_thread_mutex_unlock(pool->user_mutex);
  587. #endif
  588. }
  589. #if APR_HAS_THREADS
  590. APR_DECLARE(void) apr_pool_mutex_set(apr_pool_t *pool,
  591. apr_thread_mutex_t *mutex)
  592. {
  593. pool->user_mutex = mutex;
  594. }
  595. #endif
  596. APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool)
  597. {
  598. apr_memnode_t *active;
  599. apr_allocator_t *allocator;
  600. /* Destroy the subpools. The subpools will detach themselve from
  601. * this pool thus this loop is safe and easy.
  602. */
  603. while (pool->child)
  604. apr_pool_destroy(pool->child);
  605. /* Run cleanups */
  606. run_cleanups(&pool->cleanups);
  607. /* Free subprocesses */
  608. free_proc_chain(pool->subprocesses);
  609. /* Remove the pool from the parents child list */
  610. if (pool->parent) {
  611. #if APR_HAS_THREADS
  612. apr_thread_mutex_t *mutex;
  613. if ((mutex = apr_allocator_mutex_get(pool->parent->allocator)) != NULL)
  614. apr_thread_mutex_lock(mutex);
  615. #endif /* APR_HAS_THREADS */
  616. if ((*pool->ref = pool->sibling) != NULL)
  617. pool->sibling->ref = pool->ref;
  618. #if APR_HAS_THREADS
  619. if (mutex)
  620. apr_thread_mutex_unlock(mutex);
  621. #endif /* APR_HAS_THREADS */
  622. }
  623. /* Find the block attached to the pool structure. Save a copy of the
  624. * allocator pointer, because the pool struct soon will be no more.
  625. */
  626. allocator = pool->allocator;
  627. active = pool->self;
  628. *active->ref = NULL;
  629. #if APR_HAS_THREADS
  630. if (apr_allocator_owner_get(allocator) == pool) {
  631. /* Make sure to remove the lock, since it is highly likely to
  632. * be invalid now.
  633. */
  634. apr_allocator_mutex_set(allocator, NULL);
  635. }
  636. #endif /* APR_HAS_THREADS */
  637. /* Free all the nodes in the pool (including the node holding the
  638. * pool struct), by giving them back to the allocator.
  639. */
  640. allocator_free(allocator, active);
  641. /* If this pool happens to be the owner of the allocator, free
  642. * everything in the allocator (that includes the pool struct
  643. * and the allocator). Don't worry about destroying the optional mutex
  644. * in the allocator, it will have been destroyed by the cleanup function.
  645. */
  646. if (apr_allocator_owner_get(allocator) == pool) {
  647. apr_allocator_destroy(allocator);
  648. }
  649. }
  650. APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
  651. apr_pool_t *parent,
  652. apr_abortfunc_t abort_fn,
  653. apr_allocator_t *allocator)
  654. {
  655. apr_pool_t *pool;
  656. apr_memnode_t *node;
  657. *newpool = NULL;
  658. if (!parent)
  659. parent = global_pool;
  660. if (!abort_fn && parent)
  661. abort_fn = parent->abort_fn;
  662. if (allocator == NULL)
  663. allocator = parent->allocator;
  664. if ((node = allocator_alloc(allocator,
  665. MIN_ALLOC - APR_MEMNODE_T_SIZE)) == NULL) {
  666. if (abort_fn)
  667. abort_fn(APR_ENOMEM);
  668. return APR_ENOMEM;
  669. }
  670. node->next = node;
  671. node->ref = &node->next;
  672. pool = (apr_pool_t *)node->first_avail;
  673. node->first_avail = pool->self_first_avail = (char *)pool + SIZEOF_POOL_T;
  674. pool->allocator = allocator;
  675. pool->active = pool->self = node;
  676. pool->abort_fn = abort_fn;
  677. pool->child = NULL;
  678. pool->cleanups = NULL;
  679. pool->free_cleanups = NULL;
  680. pool->subprocesses = NULL;
  681. pool->user_data = NULL;
  682. pool->tag = NULL;
  683. #if APR_HAS_THREADS
  684. pool->user_mutex = NULL;
  685. #endif
  686. #ifdef NETWARE
  687. pool->owner_proc = (apr_os_proc_t)getnlmhandle();
  688. #endif /* defined(NETWARE) */
  689. if ((pool->parent = parent) != NULL) {
  690. #if APR_HAS_THREADS
  691. apr_thread_mutex_t *mutex;
  692. if ((mutex = apr_allocator_mutex_get(parent->allocator)) != NULL)
  693. apr_thread_mutex_lock(mutex);
  694. #endif /* APR_HAS_THREADS */
  695. if ((pool->sibling = parent->child) != NULL)
  696. pool->sibling->ref = &pool->sibling;
  697. parent->child = pool;
  698. pool->ref = &parent->child;
  699. #if APR_HAS_THREADS
  700. if (mutex)
  701. apr_thread_mutex_unlock(mutex);
  702. #endif /* APR_HAS_THREADS */
  703. }
  704. else {
  705. pool->sibling = NULL;
  706. pool->ref = NULL;
  707. }
  708. *newpool = pool;
  709. return APR_SUCCESS;
  710. }
  711. /*
  712. * "Print" functions
  713. */
  714. /*
  715. * apr_psprintf is implemented by writing directly into the current
  716. * block of the pool, starting right at first_avail. If there's
  717. * insufficient room, then a new block is allocated and the earlier
  718. * output is copied over. The new block isn't linked into the pool
  719. * until all the output is done.
  720. *
  721. * Note that this is completely safe because nothing else can
  722. * allocate in this apr_pool_t while apr_psprintf is running. alarms are
  723. * blocked, and the only thing outside of apr_pools.c that's invoked
  724. * is apr_vformatter -- which was purposefully written to be
  725. * self-contained with no callouts.
  726. */
  727. struct psprintf_data {
  728. apr_vformatter_buff_t vbuff;
  729. apr_memnode_t *node;
  730. apr_pool_t *pool;
  731. apr_byte_t got_a_new_node;
  732. apr_memnode_t *free;
  733. };
  734. #define APR_PSPRINTF_MIN_STRINGSIZE 32
  735. static int psprintf_flush(apr_vformatter_buff_t *vbuff)
  736. {
  737. struct psprintf_data *ps = (struct psprintf_data *)vbuff;
  738. apr_memnode_t *node, *active;
  739. apr_size_t cur_len, size;
  740. char *strp;
  741. apr_pool_t *pool;
  742. apr_size_t free_index;
  743. pool = ps->pool;
  744. active = ps->node;
  745. strp = ps->vbuff.curpos;
  746. cur_len = strp - active->first_avail;
  747. size = cur_len << 1;
  748. /* Make sure that we don't try to use a block that has less
  749. * than APR_PSPRINTF_MIN_STRINGSIZE bytes left in it. This
  750. * also catches the case where size == 0, which would result
  751. * in reusing a block that can't even hold the NUL byte.
  752. */
  753. if (size < APR_PSPRINTF_MIN_STRINGSIZE)
  754. size = APR_PSPRINTF_MIN_STRINGSIZE;
  755. node = active->next;
  756. if (!ps->got_a_new_node
  757. && size < (apr_size_t)(node->endp - node->first_avail)) {
  758. list_remove(node);
  759. list_insert(node, active);
  760. node->free_index = 0;
  761. pool->active = node;
  762. free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
  763. BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
  764. active->free_index = (APR_UINT32_TRUNC_CAST)free_index;
  765. node = active->next;
  766. if (free_index < node->free_index) {
  767. do {
  768. node = node->next;
  769. }
  770. while (free_index < node->free_index);
  771. list_remove(active);
  772. list_insert(active, node);
  773. }
  774. node = pool->active;
  775. }
  776. else {
  777. if ((node = allocator_alloc(pool->allocator, size)) == NULL)
  778. return -1;
  779. if (ps->got_a_new_node) {
  780. active->next = ps->free;
  781. ps->free = active;
  782. }
  783. ps->got_a_new_node = 1;
  784. }
  785. memcpy(node->first_avail, active->first_avail, cur_len);
  786. ps->node = node;
  787. ps->vbuff.curpos = node->first_avail + cur_len;
  788. ps->vbuff.endpos = node->endp - 1; /* Save a byte for NUL terminator */
  789. return 0;
  790. }
  791. APR_DECLARE(char *) apr_pvsprintf(apr_pool_t *pool, const char *fmt, va_list ap)
  792. {
  793. struct psprintf_data ps;
  794. char *strp;
  795. apr_size_t size;
  796. apr_memnode_t *active, *node;
  797. apr_size_t free_index;
  798. #if APR_HAS_THREADS
  799. if (pool->user_mutex) apr_thread_mutex_lock(pool->user_mutex);
  800. #endif
  801. ps.node = active = pool->active;
  802. ps.pool = pool;
  803. ps.vbuff.curpos = ps.node->first_avail;
  804. /* Save a byte for the NUL terminator */
  805. ps.vbuff.endpos = ps.node->endp - 1;
  806. ps.got_a_new_node = 0;
  807. ps.free = NULL;
  808. /* Make sure that the first node passed to apr_vformatter has at least
  809. * room to hold the NUL terminator.
  810. */
  811. if (ps.node->first_avail == ps.node->endp) {
  812. if (psprintf_flush(&ps.vbuff) == -1) {
  813. if (pool->abort_fn) {
  814. pool->abort_fn(APR_ENOMEM);
  815. }
  816. strp = NULL;
  817. goto end;
  818. }
  819. }
  820. if (apr_vformatter(psprintf_flush, &ps.vbuff, fmt, ap) == -1) {
  821. if (pool->abort_fn)
  822. pool->abort_fn(APR_ENOMEM);
  823. strp = NULL;
  824. goto end;
  825. }
  826. strp = ps.vbuff.curpos;
  827. *strp++ = '\0';
  828. size = strp - ps.node->first_avail;
  829. size = APR_ALIGN_DEFAULT(size);
  830. strp = ps.node->first_avail;
  831. ps.node->first_avail += size;
  832. if (ps.free)
  833. allocator_free(pool->allocator, ps.free);
  834. /*
  835. * Link the node in if it's a new one
  836. */
  837. if (!ps.got_a_new_node)
  838. goto end;
  839. active = pool->active;
  840. node = ps.node;
  841. node->free_index = 0;
  842. list_insert(node, active);
  843. pool->active = node;
  844. free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
  845. BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
  846. active->free_index = (APR_UINT32_TRUNC_CAST)free_index;
  847. node = active->next;
  848. if (free_index >= node->free_index)
  849. goto end;
  850. do {
  851. node = node->next;
  852. }
  853. while (free_index < node->free_index);
  854. list_remove(active);
  855. list_insert(active, node);
  856. end:
  857. #if APR_HAS_THREADS
  858. if (pool->user_mutex) apr_thread_mutex_unlock(pool->user_mutex);
  859. #endif
  860. return strp;
  861. }
  862. #else /* APR_POOL_DEBUG */
  863. /*
  864. * Debug helper functions
  865. */
  866. /*
  867. * Walk the pool tree rooted at pool, depth first. When fn returns
  868. * anything other than 0, abort the traversal and return the value
  869. * returned by fn.
  870. */
  871. static int apr_pool_walk_tree(apr_pool_t *pool,
  872. int (*fn)(apr_pool_t *pool, void *data),
  873. void *data)
  874. {
  875. int rv;
  876. apr_pool_t *child;
  877. rv = fn(pool, data);
  878. if (rv)
  879. return rv;
  880. #if APR_HAS_THREADS
  881. if (pool->mutex) {
  882. apr_thread_mutex_lock(pool->mutex);
  883. }
  884. #endif /* APR_HAS_THREADS */
  885. child = pool->child;
  886. while (child) {
  887. rv = apr_pool_walk_tree(child, fn, data);
  888. if (rv)
  889. break;
  890. child = child->sibling;
  891. }
  892. #if APR_HAS_THREADS
  893. if (pool->mutex) {
  894. apr_thread_mutex_unlock(pool->mutex);
  895. }
  896. #endif /* APR_HAS_THREADS */
  897. return rv;
  898. }
  899. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  900. static void apr_pool_log_event(apr_pool_t *pool, const char *event,
  901. const char *file_line, int deref)
  902. {
  903. if (file_stderr) {
  904. if (deref) {
  905. apr_file_printf(file_stderr,
  906. "POOL DEBUG: "
  907. "[%lu"
  908. #if APR_HAS_THREADS
  909. "/%lu"
  910. #endif /* APR_HAS_THREADS */
  911. "] "
  912. "%7s "
  913. "(%10lu/%10lu/%10lu) "
  914. "0x%08X \"%s\" "
  915. "<%s> "
  916. "(%u/%u/%u) "
  917. "\n",
  918. (unsigned long)getpid(),
  919. #if APR_HAS_THREADS
  920. (unsigned long)apr_os_thread_current(),
  921. #endif /* APR_HAS_THREADS */
  922. event,
  923. (unsigned long)apr_pool_num_bytes(pool, 0),
  924. (unsigned long)apr_pool_num_bytes(pool, 1),
  925. (unsigned long)apr_pool_num_bytes(global_pool, 1),
  926. (unsigned int)pool, pool->tag,
  927. file_line,
  928. pool->stat_alloc, pool->stat_total_alloc, pool->stat_clear);
  929. }
  930. else {
  931. apr_file_printf(file_stderr,
  932. "POOL DEBUG: "
  933. "[%lu"
  934. #if APR_HAS_THREADS
  935. "/%lu"
  936. #endif /* APR_HAS_THREADS */
  937. "] "
  938. "%7s "
  939. " "
  940. "0x%08X "
  941. "<%s> "
  942. "\n",
  943. (unsigned long)getpid(),
  944. #if APR_HAS_THREADS
  945. (unsigned long)apr_os_thread_current(),
  946. #endif /* APR_HAS_THREADS */
  947. event,
  948. (unsigned int)pool,
  949. file_line);
  950. }
  951. }
  952. }
  953. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  954. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME)
  955. static int pool_is_child_of(apr_pool_t *parent, void *data)
  956. {
  957. apr_pool_t *pool = (apr_pool_t *)data;
  958. return (pool == parent);
  959. }
  960. static int apr_pool_is_child_of(apr_pool_t *pool, apr_pool_t *parent)
  961. {
  962. if (parent == NULL)
  963. return 0;
  964. return apr_pool_walk_tree(parent, pool_is_child_of, pool);
  965. }
  966. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME) */
  967. static void apr_pool_check_integrity(apr_pool_t *pool)
  968. {
  969. /* Rule of thumb: use of the global pool is always
  970. * ok, since the only user is apr_pools.c. Unless
  971. * people have searched for the top level parent and
  972. * started to use that...
  973. */
  974. if (pool == global_pool || global_pool == NULL)
  975. return;
  976. /* Lifetime
  977. * This basically checks to see if the pool being used is still
  978. * a relative to the global pool. If not it was previously
  979. * destroyed, in which case we abort().
  980. */
  981. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME)
  982. if (!apr_pool_is_child_of(pool, global_pool)) {
  983. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  984. apr_pool_log_event(pool, "LIFE",
  985. __FILE__ ":apr_pool_integrity check", 0);
  986. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  987. abort();
  988. }
  989. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME) */
  990. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_OWNER)
  991. #if APR_HAS_THREADS
  992. if (!apr_os_thread_equal(pool->owner, apr_os_thread_current())) {
  993. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  994. apr_pool_log_event(pool, "THREAD",
  995. __FILE__ ":apr_pool_integrity check", 0);
  996. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  997. abort();
  998. }
  999. #endif /* APR_HAS_THREADS */
  1000. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_OWNER) */
  1001. }
  1002. /*
  1003. * Initialization (debug)
  1004. */
  1005. APR_DECLARE(apr_status_t) apr_pool_initialize(void)
  1006. {
  1007. apr_status_t rv;
  1008. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  1009. char *logpath;
  1010. #endif
  1011. if (apr_pools_initialized++)
  1012. return APR_SUCCESS;
  1013. /* Since the debug code works a bit differently then the
  1014. * regular pools code, we ask for a lock here. The regular
  1015. * pools code has got this lock embedded in the global
  1016. * allocator, a concept unknown to debug mode.
  1017. */
  1018. if ((rv = apr_pool_create_ex(&global_pool, NULL, NULL,
  1019. NULL)) != APR_SUCCESS) {
  1020. return rv;
  1021. }
  1022. apr_pool_tag(global_pool, "APR global pool");
  1023. apr_pools_initialized = 1;
  1024. /* This has to happen here because mutexes might be backed by
  1025. * atomics. It used to be snug and safe in apr_initialize().
  1026. */
  1027. if ((rv = apr_atomic_init(global_pool)) != APR_SUCCESS) {
  1028. return rv;
  1029. }
  1030. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  1031. rv = apr_env_get(&logpath, "APR_POOL_DEBUG_LOG", global_pool);
  1032. if (rv == APR_SUCCESS) {
  1033. apr_file_open(&file_stderr, logpath, APR_APPEND|APR_WRITE|APR_CREATE,
  1034. APR_OS_DEFAULT, global_pool);
  1035. }
  1036. else {
  1037. apr_file_open_stderr(&file_stderr, global_pool);
  1038. }
  1039. if (file_stderr) {
  1040. apr_file_printf(file_stderr,
  1041. "POOL DEBUG: [PID"
  1042. #if APR_HAS_THREADS
  1043. "/TID"
  1044. #endif /* APR_HAS_THREADS */
  1045. "] ACTION (SIZE /POOL SIZE /TOTAL SIZE) "
  1046. "POOL \"TAG\" <__FILE__:__LINE__> (ALLOCS/TOTAL ALLOCS/CLEARS)\n");
  1047. apr_pool_log_event(global_pool, "GLOBAL", __FILE__ ":apr_pool_initialize", 0);
  1048. }
  1049. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  1050. return APR_SUCCESS;
  1051. }
  1052. APR_DECLARE(void) apr_pool_terminate(void)
  1053. {
  1054. if (!apr_pools_initialized)
  1055. return;
  1056. apr_pools_initialized = 0;
  1057. apr_pool_destroy(global_pool); /* This will also destroy the mutex */
  1058. global_pool = NULL;
  1059. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  1060. file_stderr = NULL;
  1061. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  1062. }
  1063. /*
  1064. * Memory allocation (debug)
  1065. */
  1066. static void *pool_alloc(apr_pool_t *pool, apr_size_t size)
  1067. {
  1068. debug_node_t *node;
  1069. void *mem;
  1070. if ((mem = malloc(size)) == NULL) {
  1071. if (pool->abort_fn)
  1072. pool->abort_fn(APR_ENOMEM);
  1073. return NULL;
  1074. }
  1075. node = pool->nodes;
  1076. if (node == NULL || node->index == 64) {
  1077. if ((node = malloc(SIZEOF_DEBUG_NODE_T)) == NULL) {
  1078. if (pool->abort_fn)
  1079. pool->abort_fn(APR_ENOMEM);
  1080. return NULL;
  1081. }
  1082. memset(node, 0, SIZEOF_DEBUG_NODE_T);
  1083. node->next = pool->nodes;
  1084. pool->nodes = node;
  1085. node->index = 0;
  1086. }
  1087. node->beginp[node->index] = mem;
  1088. node->endp[node->index] = (char *)mem + size;
  1089. node->index++;
  1090. pool->stat_alloc++;
  1091. pool->stat_total_alloc++;
  1092. return mem;
  1093. }
  1094. APR_DECLARE(void *) apr_palloc_debug(apr_pool_t *pool, apr_size_t size,
  1095. const char *file_line)
  1096. {
  1097. void *mem;
  1098. apr_pool_check_integrity(pool);
  1099. mem = pool_alloc(pool, size);
  1100. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC)
  1101. apr_pool_log_event(pool, "PALLOC", file_line, 1);
  1102. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC) */
  1103. return mem;
  1104. }
  1105. APR_DECLARE(void *) apr_pcalloc_debug(apr_pool_t *pool, apr_size_t size,
  1106. const char *file_line)
  1107. {
  1108. void *mem;
  1109. apr_pool_check_integrity(pool);
  1110. mem = pool_alloc(pool, size);
  1111. memset(mem, 0, size);
  1112. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC)
  1113. apr_pool_log_event(pool, "PCALLOC", file_line, 1);
  1114. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC) */
  1115. return mem;
  1116. }
  1117. /*
  1118. * Pool creation/destruction (debug)
  1119. */
  1120. #define POOL_POISON_BYTE 'A'
  1121. static void pool_clear_debug(apr_pool_t *pool, const char *file_line)
  1122. {
  1123. debug_node_t *node;
  1124. apr_uint32_t index;
  1125. /* Destroy the subpools. The subpools will detach themselves from
  1126. * this pool thus this loop is safe and easy.
  1127. */
  1128. while (pool->child)
  1129. pool_destroy_debug(pool->child, file_line);
  1130. /* Run cleanups */
  1131. run_cleanups(&pool->cleanups);
  1132. pool->free_cleanups = NULL;
  1133. pool->cleanups = NULL;
  1134. /* If new child pools showed up, this is a reason to raise a flag */
  1135. if (pool->child)
  1136. abort();
  1137. /* Free subprocesses */
  1138. free_proc_chain(pool->subprocesses);
  1139. pool->subprocesses = NULL;
  1140. /* Clear the user data. */
  1141. pool->user_data = NULL;
  1142. /* Free the blocks, scribbling over them first to help highlight
  1143. * use-after-free issues. */
  1144. while ((node = pool->nodes) != NULL) {
  1145. pool->nodes = node->next;
  1146. for (index = 0; index < node->index; index++) {
  1147. memset(node->beginp[index], POOL_POISON_BYTE,
  1148. node->endp[index] - node->beginp[index]);
  1149. free(node->beginp[index]);
  1150. }
  1151. memset(node, POOL_POISON_BYTE, SIZEOF_DEBUG_NODE_T);
  1152. free(node);
  1153. }
  1154. pool->stat_alloc = 0;
  1155. pool->stat_clear++;
  1156. }
  1157. APR_DECLARE(void) apr_pool_clear_debug(apr_pool_t *pool,
  1158. const char *file_line)
  1159. {
  1160. #if APR_HAS_THREADS
  1161. apr_thread_mutex_t *mutex = NULL;
  1162. #endif
  1163. apr_pool_check_integrity(pool);
  1164. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
  1165. apr_pool_log_event(pool, "CLEAR", file_line, 1);
  1166. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
  1167. #if APR_HAS_THREADS
  1168. if (pool->parent != NULL)
  1169. mutex = pool->parent->mutex;
  1170. /* Lock the parent mutex before clearing so that if we have our
  1171. * own mutex it won't be accessed by apr_pool_walk_tree after
  1172. * it has been destroyed.
  1173. */
  1174. if (mutex != NULL && mutex != pool->mutex) {
  1175. apr_thread_mutex_lock(mutex);
  1176. }
  1177. #endif
  1178. pool_clear_debug(pool, file_line);
  1179. #if APR_HAS_THREADS
  1180. /* If we had our own mutex, it will have been destroyed by
  1181. * the registered cleanups. Recreate the mutex. Unlock
  1182. * the mutex we obtained above.
  1183. */
  1184. if (mutex != pool->mutex) {
  1185. (void)apr_thread_mutex_create(&pool->mutex,
  1186. APR_THREAD_MUTEX_NESTED, pool);
  1187. if (mutex != NULL)
  1188. (void)apr_thread_mutex_unlock(mutex);
  1189. }
  1190. #endif /* APR_HAS_THREADS */
  1191. }
  1192. static void pool_destroy_debug(apr_pool_t *pool, const char *file_line)
  1193. {
  1194. apr_pool_check_integrity(pool);
  1195. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
  1196. apr_pool_log_event(pool, "DESTROY", file_line, 1);
  1197. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
  1198. pool_clear_debug(pool, file_line);
  1199. /* Remove the pool from the parents child list */
  1200. if (pool->parent) {
  1201. #if APR_HAS_THREADS
  1202. apr_thread_mutex_t *mutex;
  1203. if ((mutex = pool->parent->mutex) != NULL)
  1204. apr_thread_mutex_lock(mutex);
  1205. #endif /* APR_HAS_THREADS */
  1206. if ((*pool->ref = pool->sibling) != NULL)
  1207. pool->sibling->ref = pool->ref;
  1208. #if APR_HAS_THREADS
  1209. if (mutex)
  1210. apr_thread_mutex_unlock(mutex);
  1211. #endif /* APR_HAS_THREADS */
  1212. }
  1213. if (pool->allocator != NULL
  1214. && apr_allocator_owner_get(pool->allocator) == pool) {
  1215. apr_allocator_destroy(pool->allocator);
  1216. }
  1217. /* Free the pool itself */
  1218. free(pool);
  1219. }
  1220. APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *pool,
  1221. const char *file_line)
  1222. {
  1223. if (pool->joined) {
  1224. /* Joined pools must not be explicitly destroyed; the caller
  1225. * has broken the guarantee. */
  1226. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  1227. apr_pool_log_event(pool, "LIFE",
  1228. __FILE__ ":apr_pool_destroy abort on joined", 0);
  1229. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  1230. abort();
  1231. }
  1232. pool_destroy_debug(pool, file_line);
  1233. }
  1234. APR_DECLARE(apr_status_t) apr_pool_create_ex_debug(apr_pool_t **newpool,
  1235. apr_pool_t *parent,
  1236. apr_abortfunc_t abort_fn,
  1237. apr_allocator_t *allocator,
  1238. const char *file_line)
  1239. {
  1240. apr_pool_t *pool;
  1241. *newpool = NULL;
  1242. if (!parent) {
  1243. parent = global_pool;
  1244. }
  1245. else {
  1246. apr_pool_check_integrity(parent);
  1247. if (!allocator)
  1248. allocator = parent->allocator;
  1249. }
  1250. if (!abort_fn && parent)
  1251. abort_fn = parent->abort_fn;
  1252. if ((pool = malloc(SIZEOF_POOL_T)) == NULL) {
  1253. if (abort_fn)
  1254. abort_fn(APR_ENOMEM);
  1255. return APR_ENOMEM;
  1256. }
  1257. memset(pool, 0, SIZEOF_POOL_T);
  1258. pool->allocator = allocator;
  1259. pool->abort_fn = abort_fn;
  1260. pool->tag = file_line;
  1261. pool->file_line = file_line;
  1262. if ((pool->parent = parent) != NULL) {
  1263. #if APR_HAS_THREADS
  1264. if (parent->mutex)
  1265. apr_thread_mutex_lock(parent->mutex);
  1266. #endif /* APR_HAS_THREADS */
  1267. if ((pool->sibling = parent->child) != NULL)
  1268. pool->sibling->ref = &pool->sibling;
  1269. parent->child = pool;
  1270. pool->ref = &parent->child;
  1271. #if APR_HAS_THREADS
  1272. if (parent->mutex)
  1273. apr_thread_mutex_unlock(parent->mutex);
  1274. #endif /* APR_HAS_THREADS */
  1275. }
  1276. else {
  1277. pool->sibling = NULL;
  1278. pool->ref = NULL;
  1279. }
  1280. #if APR_HAS_THREADS
  1281. pool->owner = apr_os_thread_current();
  1282. #endif /* APR_HAS_THREADS */
  1283. #ifdef NETWARE
  1284. pool->owner_proc = (apr_os_proc_t)getnlmhandle();
  1285. #endif /* defined(NETWARE) */
  1286. if (parent == NULL || parent->allocator != allocator) {
  1287. #if APR_HAS_THREADS
  1288. apr_status_t rv;
  1289. /* No matter what the creation flags say, always create
  1290. * a lock. Without it integrity_check and apr_pool_num_bytes
  1291. * blow up (because they traverse pools child lists that
  1292. * possibly belong to another thread, in combination with
  1293. * the pool having no lock). However, this might actually
  1294. * hide problems like creating a child pool of a pool
  1295. * belonging to another thread.
  1296. */
  1297. if ((rv = apr_thread_mutex_create(&pool->mutex,
  1298. APR_THREAD_MUTEX_NESTED, pool)) != APR_SUCCESS) {
  1299. free(pool);
  1300. return rv;
  1301. }
  1302. #endif /* APR_HAS_THREADS */
  1303. }
  1304. else {
  1305. #if APR_HAS_THREADS
  1306. if (parent)
  1307. pool->mutex = parent->mutex;
  1308. #endif /* APR_HAS_THREADS */
  1309. }
  1310. *newpool = pool;
  1311. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
  1312. apr_pool_log_event(pool, "CREATE", file_line, 1);
  1313. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
  1314. return APR_SUCCESS;
  1315. }
  1316. /*
  1317. * "Print" functions (debug)
  1318. */
  1319. struct psprintf_data {
  1320. apr_vformatter_buff_t vbuff;
  1321. char *mem;
  1322. apr_size_t size;
  1323. };
  1324. static int psprintf_flush(apr_vformatter_buff_t *vbuff)
  1325. {
  1326. struct psprintf_data *ps = (struct psprintf_data *)vbuff;
  1327. apr_size_t size;
  1328. size = ps->vbuff.curpos - ps->mem;
  1329. ps->size <<= 1;
  1330. if ((ps->mem = realloc(ps->mem, ps->size)) == NULL)
  1331. return -1;
  1332. ps->vbuff.curpos = ps->mem + size;
  1333. ps->vbuff.endpos = ps->mem + ps->size - 1;
  1334. return 0;
  1335. }
  1336. APR_DECLARE(char *) apr_pvsprintf(apr_pool_t *pool, const char *fmt, va_list ap)
  1337. {
  1338. struct psprintf_data ps;
  1339. debug_node_t *node;
  1340. apr_pool_check_integrity(pool);
  1341. ps.size = 64;
  1342. ps.mem = malloc(ps.size);
  1343. ps.vbuff.curpos = ps.mem;
  1344. /* Save a byte for the NUL terminator */
  1345. ps.vbuff.endpos = ps.mem + ps.size - 1;
  1346. if (apr_vformatter(psprintf_flush, &ps.vbuff, fmt, ap) == -1) {
  1347. if (pool->abort_fn)
  1348. pool->abort_fn(APR_ENOMEM);
  1349. return NULL;
  1350. }
  1351. *ps.vbuff.curpos++ = '\0';
  1352. /*
  1353. * Link the node in
  1354. */
  1355. node = pool->nodes;
  1356. if (node == NULL || node->index == 64) {
  1357. if ((node = malloc(SIZEOF_DEBUG_NODE_T)) == NULL) {
  1358. if (pool->abort_fn)
  1359. pool->abort_fn(APR_ENOMEM);
  1360. return NULL;
  1361. }
  1362. node->next = pool->nodes;
  1363. pool->nodes = node;
  1364. node->index = 0;
  1365. }
  1366. node->beginp[node->index] = ps.mem;
  1367. node->endp[node->index] = ps.mem + ps.size;
  1368. node->index++;
  1369. return ps.mem;
  1370. }
  1371. /*
  1372. * Debug functions
  1373. */
  1374. APR_DECLARE(void) apr_pool_join(apr_pool_t *p, apr_pool_t *sub)
  1375. {
  1376. #if APR_POOL_DEBUG
  1377. if (sub->parent != p) {
  1378. abort();
  1379. }
  1380. sub->joined = p;
  1381. #endif
  1382. }
  1383. static int pool_find(apr_pool_t *pool, void *data)
  1384. {
  1385. void **pmem = (void **)data;
  1386. debug_node_t *node;
  1387. apr_uint32_t index;
  1388. node = pool->nodes;
  1389. while (node) {
  1390. for (index = 0; index < node->index; index++) {
  1391. if (node->beginp[index] <= *pmem
  1392. && node->endp[index] > *pmem) {
  1393. *pmem = pool;
  1394. return 1;
  1395. }
  1396. }
  1397. node = node->next;
  1398. }
  1399. return 0;
  1400. }
  1401. APR_DECLARE(apr_pool_t *) apr_pool_find(const void *mem)
  1402. {
  1403. void *pool = (void *)mem;
  1404. if (apr_pool_walk_tree(global_pool, pool_find, &pool))
  1405. return pool;
  1406. return NULL;
  1407. }
  1408. static int pool_num_bytes(apr_pool_t *pool, void *data)
  1409. {
  1410. apr_size_t *psize = (apr_size_t *)data;
  1411. debug_node_t *node;
  1412. apr_uint32_t index;
  1413. node = pool->nodes;
  1414. while (node) {
  1415. for (index = 0; index < node->index; index++) {
  1416. *psize += (char *)node->endp[index] - (char *)node->beginp[index];
  1417. }
  1418. node = node->next;
  1419. }
  1420. return 0;
  1421. }
  1422. APR_DECLARE(apr_size_t) apr_pool_num_bytes(apr_pool_t *pool, int recurse)
  1423. {
  1424. apr_size_t size = 0;
  1425. if (!recurse) {
  1426. pool_num_bytes(pool, &size);
  1427. return size;
  1428. }
  1429. apr_pool_walk_tree(pool, pool_num_bytes, &size);
  1430. return size;
  1431. }
  1432. APR_DECLARE(void) apr_pool_lock(apr_pool_t *pool, int flag)
  1433. {
  1434. }
  1435. #endif /* !APR_POOL_DEBUG */
  1436. #ifdef NETWARE
  1437. void netware_pool_proc_cleanup ()
  1438. {
  1439. apr_pool_t *pool = global_pool->child;
  1440. apr_os_proc_t owner_proc = (apr_os_proc_t)getnlmhandle();
  1441. while (pool) {
  1442. if (pool->owner_proc == owner_proc) {
  1443. apr_pool_destroy (pool);
  1444. pool = global_pool->child;
  1445. }
  1446. else {
  1447. pool = pool->sibling;
  1448. }
  1449. }
  1450. return;
  1451. }
  1452. #endif /* defined(NETWARE) */
  1453. /*
  1454. * "Print" functions (common)
  1455. */
  1456. APR_DECLARE_NONSTD(char *) apr_psprintf(apr_pool_t *p, const char *fmt, ...)
  1457. {
  1458. va_list ap;
  1459. char *res;
  1460. va_start(ap, fmt);
  1461. res = apr_pvsprintf(p, fmt, ap);
  1462. va_end(ap);
  1463. return res;
  1464. }
  1465. /*
  1466. * Pool Properties
  1467. */
  1468. APR_DECLARE(void) apr_pool_abort_set(apr_abortfunc_t abort_fn,
  1469. apr_pool_t *pool)
  1470. {
  1471. pool->abort_fn = abort_fn;
  1472. }
  1473. APR_DECLARE(apr_abortfunc_t) apr_pool_abort_get(apr_pool_t *pool)
  1474. {
  1475. return pool->abort_fn;
  1476. }
  1477. APR_DECLARE(apr_pool_t *) apr_pool_parent_get(apr_pool_t *pool)
  1478. {
  1479. #ifdef NETWARE
  1480. /* On NetWare, don't return the global_pool, return the application pool
  1481. as the top most pool */
  1482. if (pool->parent == global_pool)
  1483. return NULL;
  1484. else
  1485. #endif
  1486. return pool->parent;
  1487. }
  1488. APR_DECLARE(apr_allocator_t *) apr_pool_allocator_get(apr_pool_t *pool)
  1489. {
  1490. return pool->allocator;
  1491. }
  1492. /* return TRUE if a is an ancestor of b
  1493. * NULL is considered an ancestor of all pools
  1494. */
  1495. APR_DECLARE(int) apr_pool_is_ancestor(apr_pool_t *a, apr_pool_t *b)
  1496. {
  1497. if (a == NULL)
  1498. return 1;
  1499. #if APR_POOL_DEBUG
  1500. /* Find the pool with the longest lifetime guaranteed by the
  1501. * caller: */
  1502. while (a->joined) {
  1503. a = a->joined;
  1504. }
  1505. #endif
  1506. while (b) {
  1507. if (a == b)
  1508. return 1;
  1509. b = b->parent;
  1510. }
  1511. return 0;
  1512. }
  1513. APR_DECLARE(const char *) apr_pool_tag(apr_pool_t *pool, const char *tag)
  1514. {
  1515. if (tag) {
  1516. pool->tag = tag;
  1517. }
  1518. return pool->tag;
  1519. }
  1520. /*
  1521. * User data management
  1522. */
  1523. APR_DECLARE(apr_status_t) apr_pool_userdata_set(const void *data, const char *key,
  1524. apr_status_t (*cleanup) (void *),
  1525. apr_pool_t *pool)
  1526. {
  1527. #if APR_POOL_DEBUG
  1528. apr_pool_check_integrity(pool);
  1529. #endif /* APR_POOL_DEBUG */
  1530. if (pool->user_data == NULL)
  1531. pool->user_data = apr_hash_make(pool);
  1532. if (apr_hash_get(pool->user_data, key, APR_HASH_KEY_STRING) == NULL) {
  1533. char *new_key = apr_pstrdup(pool, key);
  1534. apr_hash_set(pool->user_data, new_key, APR_HASH_KEY_STRING, data);
  1535. }
  1536. else {
  1537. apr_hash_set(pool->user_data, key, APR_HASH_KEY_STRING, data);
  1538. }
  1539. if (cleanup)
  1540. apr_pool_cleanup_register(pool, data, cleanup, cleanup);
  1541. return APR_SUCCESS;
  1542. }
  1543. APR_DECLARE(apr_status_t) apr_pool_userdata_setn(const void *data,
  1544. const char *key,
  1545. apr_status_t (*cleanup)(void *),
  1546. apr_pool_t *pool)
  1547. {
  1548. #if APR_POOL_DEBUG
  1549. apr_pool_check_integrity(pool);
  1550. #endif /* APR_POOL_DEBUG */
  1551. if (pool->user_data == NULL)
  1552. pool->user_data = apr_hash_make(pool);
  1553. apr_hash_set(pool->user_data, key, APR_HASH_KEY_STRING, data);
  1554. if (cleanup)
  1555. apr_pool_cleanup_register(pool, data, cleanup, cleanup);
  1556. return APR_SUCCESS;
  1557. }
  1558. APR_DECLARE(apr_status_t) apr_pool_userdata_get(void **data, const char *key,
  1559. apr_pool_t *pool)
  1560. {
  1561. #if APR_POOL_DEBUG
  1562. apr_pool_check_integrity(pool);
  1563. #endif /* APR_POOL_DEBUG */
  1564. if (pool->user_data == NULL) {
  1565. *data = NULL;
  1566. }
  1567. else {
  1568. *data = apr_hash_get(pool->user_data, key, APR_HASH_KEY_STRING);
  1569. }
  1570. return APR_SUCCESS;
  1571. }
  1572. /*
  1573. * Cleanup
  1574. */
  1575. struct cleanup_t {
  1576. struct cleanup_t *next;
  1577. const void *data;
  1578. apr_status_t (*plain_cleanup_fn)(void *data);
  1579. apr_status_t (*child_cleanup_fn)(void *data);
  1580. };
  1581. APR_DECLARE(void) apr_pool_cleanup_register(apr_pool_t *p, const void *data,
  1582. apr_status_t (*plain_cleanup_fn)(void *data),
  1583. apr_status_t (*child_cleanup_fn)(void *data))
  1584. {
  1585. cleanup_t *c;
  1586. #if APR_POOL_DEBUG
  1587. apr_pool_check_integrity(p);
  1588. #endif /* APR_POOL_DEBUG */
  1589. if (p != NULL) {
  1590. if (p->free_cleanups) {
  1591. /* reuse a cleanup structure */
  1592. c = p->free_cleanups;
  1593. p->free_cleanups = c->next;
  1594. } else {
  1595. c = apr_palloc(p, sizeof(cleanup_t));
  1596. }
  1597. c->data = data;
  1598. c->plain_cleanup_fn = plain_cleanup_fn;
  1599. c->child_cleanup_fn = child_cleanup_fn;
  1600. c->next = p->cleanups;
  1601. p->cleanups = c;
  1602. }
  1603. }
  1604. APR_DECLARE(void) apr_pool_cleanup_kill(apr_pool_t *p, const void *data,
  1605. apr_status_t (*cleanup_fn)(void *))
  1606. {
  1607. cleanup_t *c, **lastp;
  1608. #if APR_POOL_DEBUG
  1609. apr_pool_check_integrity(p);
  1610. #endif /* APR_POOL_DEBUG */
  1611. if (p == NULL)
  1612. return;
  1613. c = p->cleanups;
  1614. lastp = &p->cleanups;
  1615. while (c) {
  1616. if (c->data == data && c->plain_cleanup_fn == cleanup_fn) {
  1617. *lastp = c->next;
  1618. /* move to freelist */
  1619. c->next = p->free_cleanups;
  1620. p->free_cleanups = c;
  1621. break;
  1622. }
  1623. lastp = &c->next;
  1624. if (c == c->next) {
  1625. c = NULL;
  1626. } else {
  1627. c = c->next;
  1628. }
  1629. }
  1630. }
  1631. APR_DECLARE(void) apr_pool_child_cleanup_set(apr_pool_t *p, const void *data,
  1632. apr_status_t (*plain_cleanup_fn)(void *),
  1633. apr_status_t (*child_cleanup_fn)(void *))
  1634. {
  1635. cleanup_t *c;
  1636. #if APR_POOL_DEBUG
  1637. apr_pool_check_integrity(p);
  1638. #endif /* APR_POOL_DEBUG */
  1639. if (p == NULL)
  1640. return;
  1641. c = p->cleanups;
  1642. while (c) {
  1643. if (c->data == data && c->plain_cleanup_fn == plain_cleanup_fn) {
  1644. c->child_cleanup_fn = child_cleanup_fn;
  1645. break;
  1646. }
  1647. c = c->next;
  1648. }
  1649. }
  1650. APR_DECLARE(apr_status_t) apr_pool_cleanup_run(apr_pool_t *p, void *data,
  1651. apr_status_t (*cleanup_fn)(void *))
  1652. {
  1653. apr_pool_cleanup_kill(p, data, cleanup_fn);
  1654. return (*cleanup_fn)(data);
  1655. }
  1656. static void run_cleanups(cleanup_t **cref)
  1657. {
  1658. cleanup_t *c = *cref;
  1659. while (c) {
  1660. *cref = c->next;
  1661. (*c->plain_cleanup_fn)((void *)c->data);
  1662. c = *cref;
  1663. }
  1664. }
  1665. static void run_child_cleanups(cleanup_t **cref)
  1666. {
  1667. cleanup_t *c = *cref;
  1668. while (c) {
  1669. *cref = c->next;
  1670. (*c->child_cleanup_fn)((void *)c->data);
  1671. c = *cref;
  1672. }
  1673. }
  1674. static void cleanup_pool_for_exec(apr_pool_t *p)
  1675. {
  1676. run_child_cleanups(&p->cleanups);
  1677. for (p = p->child; p; p = p->sibling)
  1678. cleanup_pool_for_exec(p);
  1679. }
  1680. APR_DECLARE(void) apr_pool_cleanup_for_exec(void)
  1681. {
  1682. #if !defined(WIN32) && !defined(OS2)
  1683. /*
  1684. * Don't need to do anything on NT or OS/2, because I
  1685. * am actually going to spawn the new process - not
  1686. * exec it. All handles that are not inheritable, will
  1687. * be automajically closed. The only problem is with
  1688. * file handles that are open, but there isn't much
  1689. * I can do about that (except if the child decides
  1690. * to go out and close them
  1691. */
  1692. cleanup_pool_for_exec(global_pool);
  1693. #endif /* !defined(WIN32) && !defined(OS2) */
  1694. }
  1695. APR_DECLARE_NONSTD(apr_status_t) apr_pool_cleanup_null(void *data)
  1696. {
  1697. /* do nothing cleanup routine */
  1698. return APR_SUCCESS;
  1699. }
  1700. /* Subprocesses don't use the generic cleanup interface because
  1701. * we don't want multiple subprocesses to result in multiple
  1702. * three-second pauses; the subprocesses have to be "freed" all
  1703. * at once. If other resources are introduced with the same property,
  1704. * we might want to fold support for that into the generic interface.
  1705. * For now, it's a special case.
  1706. */
  1707. APR_DECLARE(void) apr_pool_note_subprocess(apr_pool_t *pool, apr_proc_t *proc,
  1708. apr_kill_conditions_e how)
  1709. {
  1710. struct process_chain *pc = apr_palloc(pool, sizeof(struct process_chain));
  1711. pc->proc = proc;
  1712. pc->kill_how = how;
  1713. pc->next = pool->subprocesses;
  1714. pool->subprocesses = pc;
  1715. }
  1716. static void free_proc_chain(struct process_chain *procs)
  1717. {
  1718. /* Dispose of the subprocesses we've spawned off in the course of
  1719. * whatever it was we're cleaning up now. This may involve killing
  1720. * some of them off...
  1721. */
  1722. struct process_chain *pc;
  1723. int need_timeout = 0;
  1724. apr_time_t timeout_interval;
  1725. if (!procs)
  1726. return; /* No work. Whew! */
  1727. /* First, check to see if we need to do the SIGTERM, sleep, SIGKILL
  1728. * dance with any of the processes we're cleaning up. If we've got
  1729. * any kill-on-sight subprocesses, ditch them now as well, so they
  1730. * don't waste any more cycles doing whatever it is that they shouldn't
  1731. * be doing anymore.
  1732. */
  1733. #ifndef NEED_WAITPID
  1734. /* Pick up all defunct processes */
  1735. for (pc = procs; pc; pc = pc->next) {
  1736. if (apr_proc_wait(pc->proc, NULL, NULL, APR_NOWAIT) != APR_CHILD_NOTDONE)
  1737. pc->kill_how = APR_KILL_NEVER;
  1738. }
  1739. #endif /* !defined(NEED_WAITPID) */
  1740. for (pc = procs; pc; pc = pc->next) {
  1741. #ifndef WIN32
  1742. if ((pc->kill_how == APR_KILL_AFTER_TIMEOUT)
  1743. || (pc->kill_how == APR_KILL_ONLY_ONCE)) {
  1744. /*
  1745. * Subprocess may be dead already. Only need the timeout if not.
  1746. * Note: apr_proc_kill on Windows is TerminateProcess(), which is
  1747. * similar to a SIGKILL, so always give the process a timeout
  1748. * under Windows before killing it.
  1749. */
  1750. if (apr_proc_kill(pc->proc, SIGTERM) == APR_SUCCESS)
  1751. need_timeout = 1;
  1752. }
  1753. else if (pc->kill_how == APR_KILL_ALWAYS) {
  1754. #else /* WIN32 knows only one fast, clean method of killing processes today */
  1755. if (pc->kill_how != APR_KILL_NEVER) {
  1756. need_timeout = 1;
  1757. pc->kill_how = APR_KILL_ALWAYS;
  1758. #endif
  1759. apr_proc_kill(pc->proc, SIGKILL);
  1760. }
  1761. }
  1762. /* Sleep only if we have to. The sleep algorithm grows
  1763. * by a factor of two on each iteration. TIMEOUT_INTERVAL
  1764. * is equal to TIMEOUT_USECS / 64.
  1765. */
  1766. if (need_timeout) {
  1767. timeout_interval = TIMEOUT_INTERVAL;
  1768. apr_sleep(timeout_interval);
  1769. do {
  1770. /* check the status of the subprocesses */
  1771. need_timeout = 0;
  1772. for (pc = procs; pc; pc = pc->next) {
  1773. if (pc->kill_how == APR_KILL_AFTER_TIMEOUT) {
  1774. if (apr_proc_wait(pc->proc, NULL, NULL, APR_NOWAIT)
  1775. == APR_CHILD_NOTDONE)
  1776. need_timeout = 1; /* subprocess is still active */
  1777. else
  1778. pc->kill_how = APR_KILL_NEVER; /* subprocess has exited */
  1779. }
  1780. }
  1781. if (need_timeout) {
  1782. if (timeout_interval >= TIMEOUT_USECS) {
  1783. break;
  1784. }
  1785. apr_sleep(timeout_interval);
  1786. timeout_interval *= 2;
  1787. }
  1788. } while (need_timeout);
  1789. }
  1790. /* OK, the scripts we just timed out for have had a chance to clean up
  1791. * --- now, just get rid of them, and also clean up the system accounting
  1792. * goop...
  1793. */
  1794. for (pc = procs; pc; pc = pc->next) {
  1795. if (pc->kill_how == APR_KILL_AFTER_TIMEOUT)
  1796. apr_proc_kill(pc->proc, SIGKILL);
  1797. }
  1798. /* Now wait for all the signaled processes to die */
  1799. for (pc = procs; pc; pc = pc->next) {
  1800. if (pc->kill_how != APR_KILL_NEVER)
  1801. (void)apr_proc_wait(pc->proc, NULL, NULL, APR_WAIT);
  1802. }
  1803. }
  1804. /*
  1805. * Pool creation/destruction stubs, for people who are running
  1806. * mixed release/debug enviroments.
  1807. */
  1808. #if !APR_POOL_DEBUG
  1809. APR_DECLARE(void *) apr_palloc_debug(apr_pool_t *pool, apr_size_t size,
  1810. const char *file_line)
  1811. {
  1812. return apr_palloc(pool, size);
  1813. }
  1814. APR_DECLARE(void *) apr_pcalloc_debug(apr_pool_t *pool, apr_size_t size,
  1815. const char *file_line)
  1816. {
  1817. return apr_pcalloc(pool, size);
  1818. }
  1819. APR_DECLARE(void) apr_pool_clear_debug(apr_pool_t *pool,
  1820. const char *file_line)
  1821. {
  1822. apr_pool_clear(pool);
  1823. }
  1824. APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *pool,
  1825. const char *file_line)
  1826. {
  1827. apr_pool_destroy(pool);
  1828. }
  1829. APR_DECLARE(apr_status_t) apr_pool_create_ex_debug(apr_pool_t **newpool,
  1830. apr_pool_t *parent,
  1831. apr_abortfunc_t abort_fn,
  1832. apr_allocator_t *allocator,
  1833. const char *file_line)
  1834. {
  1835. return apr_pool_create_ex(newpool, parent, abort_fn, allocator);
  1836. }
  1837. #else /* APR_POOL_DEBUG */
  1838. #undef apr_palloc
  1839. APR_DECLARE(void *) apr_palloc(apr_pool_t *pool, apr_size_t size);
  1840. APR_DECLARE(void *) apr_palloc(apr_pool_t *pool, apr_size_t size)
  1841. {
  1842. return apr_palloc_debug(pool, size, "undefined");
  1843. }
  1844. #undef apr_pcalloc
  1845. APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size);
  1846. APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size)
  1847. {
  1848. return apr_pcalloc_debug(pool, size, "undefined");
  1849. }
  1850. #undef apr_pool_clear
  1851. APR_DECLARE(void) apr_pool_clear(apr_pool_t *pool);
  1852. APR_DECLARE(void) apr_pool_clear(apr_pool_t *pool)
  1853. {
  1854. apr_pool_clear_debug(pool, "undefined");
  1855. }
  1856. #undef apr_pool_destroy
  1857. APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool);
  1858. APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool)
  1859. {
  1860. apr_pool_destroy_debug(pool, "undefined");
  1861. }
  1862. #undef apr_pool_create_ex
  1863. APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
  1864. apr_pool_t *parent,
  1865. apr_abortfunc_t abort_fn,
  1866. apr_allocator_t *allocator);
  1867. APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
  1868. apr_pool_t *parent,
  1869. apr_abortfunc_t abort_fn,
  1870. apr_allocator_t *allocator)
  1871. {
  1872. return apr_pool_create_ex_debug(newpool, parent,
  1873. abort_fn, allocator,
  1874. "undefined");
  1875. }
  1876. #endif /* APR_POOL_DEBUG */