fspr_pools.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360
  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 "fspr.h"
  17. #include "fspr_private.h"
  18. #include "fspr_atomic.h"
  19. #include "fspr_portable.h" /* for get_os_proc */
  20. #include "fspr_strings.h"
  21. #include "fspr_general.h"
  22. #include "fspr_pools.h"
  23. #include "fspr_allocator.h"
  24. #include "fspr_lib.h"
  25. #include "fspr_thread_mutex.h"
  26. #include "fspr_hash.h"
  27. #include "fspr_time.h"
  28. #define APR_WANT_MEMFUNC
  29. #include "fspr_want.h"
  30. #include "fspr_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 fspr_allocator_t {
  57. fspr_uint32_t max_index;
  58. fspr_uint32_t max_free_index;
  59. fspr_uint32_t current_free_index;
  60. #if APR_HAS_THREADS
  61. fspr_thread_mutex_t *mutex;
  62. #endif /* APR_HAS_THREADS */
  63. fspr_pool_t *owner;
  64. fspr_memnode_t *free[MAX_INDEX];
  65. };
  66. #define SIZEOF_ALLOCATOR_T APR_ALIGN_DEFAULT(sizeof(fspr_allocator_t))
  67. /*
  68. * Allocator
  69. */
  70. APR_DECLARE(fspr_status_t) fspr_allocator_create(fspr_allocator_t **allocator)
  71. {
  72. fspr_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) fspr_allocator_destroy(fspr_allocator_t *allocator)
  82. {
  83. fspr_uint32_t index;
  84. fspr_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) fspr_allocator_mutex_set(fspr_allocator_t *allocator,
  96. fspr_thread_mutex_t *mutex)
  97. {
  98. allocator->mutex = mutex;
  99. }
  100. APR_DECLARE(fspr_thread_mutex_t *) fspr_allocator_mutex_get(
  101. fspr_allocator_t *allocator)
  102. {
  103. return allocator->mutex;
  104. }
  105. #endif /* APR_HAS_THREADS */
  106. APR_DECLARE(void) fspr_allocator_owner_set(fspr_allocator_t *allocator,
  107. fspr_pool_t *pool)
  108. {
  109. allocator->owner = pool;
  110. }
  111. APR_DECLARE(fspr_pool_t *) fspr_allocator_owner_get(fspr_allocator_t *allocator)
  112. {
  113. return allocator->owner;
  114. }
  115. APR_DECLARE(void) fspr_allocator_max_free_set(fspr_allocator_t *allocator,
  116. fspr_size_t in_size)
  117. {
  118. fspr_uint32_t max_free_index;
  119. fspr_uint32_t size = (APR_UINT32_TRUNC_CAST)in_size;
  120. #if APR_HAS_THREADS
  121. fspr_thread_mutex_t *mutex;
  122. mutex = fspr_allocator_mutex_get(allocator);
  123. if (mutex != NULL)
  124. fspr_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. fspr_thread_mutex_unlock(mutex);
  135. #endif
  136. }
  137. static APR_INLINE
  138. fspr_memnode_t *allocator_alloc(fspr_allocator_t *allocator, fspr_size_t size)
  139. {
  140. fspr_memnode_t *node, **ref;
  141. fspr_uint32_t max_index;
  142. fspr_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. fspr_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. fspr_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. fspr_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. fspr_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. fspr_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. fspr_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(fspr_allocator_t *allocator, fspr_memnode_t *node)
  256. {
  257. fspr_memnode_t *next, *freelist = NULL;
  258. fspr_uint32_t index, max_index;
  259. fspr_uint32_t max_free_index, current_free_index;
  260. #if APR_HAS_THREADS
  261. if (allocator->mutex)
  262. fspr_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. fspr_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(fspr_memnode_t *) fspr_allocator_alloc(fspr_allocator_t *allocator,
  311. fspr_size_t size)
  312. {
  313. return allocator_alloc(allocator, size);
  314. }
  315. APR_DECLARE(void) fspr_allocator_free(fspr_allocator_t *allocator,
  316. fspr_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. fspr_proc_t *proc;
  338. fspr_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. fspr_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 fspr_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 fspr_pool_create_ex() and fspr_pool_destroy()
  356. * to see how it is used.
  357. */
  358. struct fspr_pool_t {
  359. fspr_pool_t *parent;
  360. fspr_pool_t *child;
  361. fspr_pool_t *sibling;
  362. fspr_pool_t **ref;
  363. cleanup_t *cleanups;
  364. cleanup_t *free_cleanups;
  365. fspr_allocator_t *allocator;
  366. struct process_chain *subprocesses;
  367. fspr_abortfunc_t abort_fn;
  368. fspr_hash_t *user_data;
  369. const char *tag;
  370. #if APR_HAS_THREADS
  371. fspr_thread_mutex_t *user_mutex;
  372. #endif
  373. #if !APR_POOL_DEBUG
  374. fspr_memnode_t *active;
  375. fspr_memnode_t *self; /* The node containing the pool itself */
  376. char *self_first_avail;
  377. #else /* APR_POOL_DEBUG */
  378. fspr_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. fspr_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. fspr_os_thread_t owner;
  388. fspr_thread_mutex_t *mutex;
  389. #endif /* APR_HAS_THREADS */
  390. #endif /* APR_POOL_DEBUG */
  391. #ifdef NETWARE
  392. fspr_os_proc_t owner_proc;
  393. #endif /* defined(NETWARE) */
  394. };
  395. #define SIZEOF_POOL_T APR_ALIGN_DEFAULT(sizeof(fspr_pool_t))
  396. /*
  397. * Variables
  398. */
  399. static fspr_byte_t fspr_pools_initialized = 0;
  400. static fspr_pool_t *global_pool = NULL;
  401. #if !APR_POOL_DEBUG
  402. static fspr_allocator_t *global_allocator = NULL;
  403. #endif /* !APR_POOL_DEBUG */
  404. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  405. static fspr_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(fspr_pool_t *pool, const char *file_line);
  415. #endif
  416. #if APR_HAS_THREADS
  417. APR_DECLARE(void) fspr_pool_mutex_set(fspr_pool_t *pool,
  418. fspr_thread_mutex_t *mutex)
  419. {
  420. pool->user_mutex = mutex;
  421. }
  422. #endif
  423. #if !APR_POOL_DEBUG
  424. /*
  425. * Initialization
  426. */
  427. APR_DECLARE(fspr_status_t) fspr_pool_initialize(void)
  428. {
  429. fspr_status_t rv;
  430. if (fspr_pools_initialized++)
  431. return APR_SUCCESS;
  432. if ((rv = fspr_allocator_create(&global_allocator)) != APR_SUCCESS) {
  433. fspr_pools_initialized = 0;
  434. return rv;
  435. }
  436. if ((rv = fspr_pool_create_ex(&global_pool, NULL, NULL,
  437. global_allocator)) != APR_SUCCESS) {
  438. fspr_allocator_destroy(global_allocator);
  439. global_allocator = NULL;
  440. fspr_pools_initialized = 0;
  441. return rv;
  442. }
  443. fspr_pool_tag(global_pool, "fspr_global_pool");
  444. /* This has to happen here because mutexes might be backed by
  445. * atomics. It used to be snug and safe in fspr_initialize().
  446. */
  447. if ((rv = fspr_atomic_init(global_pool)) != APR_SUCCESS) {
  448. return rv;
  449. }
  450. #if APR_HAS_THREADS
  451. {
  452. fspr_thread_mutex_t *mutex;
  453. if ((rv = fspr_thread_mutex_create(&mutex,
  454. APR_THREAD_MUTEX_DEFAULT,
  455. global_pool)) != APR_SUCCESS) {
  456. return rv;
  457. }
  458. fspr_allocator_mutex_set(global_allocator, mutex);
  459. }
  460. #endif /* APR_HAS_THREADS */
  461. fspr_allocator_owner_set(global_allocator, global_pool);
  462. return APR_SUCCESS;
  463. }
  464. APR_DECLARE(void) fspr_pool_terminate(void)
  465. {
  466. if (!fspr_pools_initialized)
  467. return;
  468. if (--fspr_pools_initialized)
  469. return;
  470. fspr_pool_destroy(global_pool); /* This will also destroy the mutex */
  471. global_pool = NULL;
  472. global_allocator = NULL;
  473. }
  474. /* Node list management helper macros; list_insert() inserts 'node'
  475. * before 'point'. */
  476. #define list_insert(node, point) do { \
  477. node->ref = point->ref; \
  478. *node->ref = node; \
  479. node->next = point; \
  480. point->ref = &node->next; \
  481. } while (0)
  482. /* list_remove() removes 'node' from its list. */
  483. #define list_remove(node) do { \
  484. *node->ref = node->next; \
  485. node->next->ref = node->ref; \
  486. } while (0)
  487. /*
  488. * Memory allocation
  489. */
  490. APR_DECLARE(void *) fspr_palloc(fspr_pool_t *pool, fspr_size_t size)
  491. {
  492. fspr_memnode_t *active, *node;
  493. void *mem = NULL;
  494. fspr_size_t free_index;
  495. #if APR_HAS_THREADS
  496. if (pool->user_mutex) fspr_thread_mutex_lock(pool->user_mutex);
  497. #endif
  498. size = APR_ALIGN_DEFAULT(size);
  499. active = pool->active;
  500. /* If the active node has enough bytes left, use it. */
  501. if (size < (fspr_size_t)(active->endp - active->first_avail)) {
  502. mem = active->first_avail;
  503. active->first_avail += size;
  504. goto end;
  505. }
  506. node = active->next;
  507. if (size < (fspr_size_t)(node->endp - node->first_avail)) {
  508. list_remove(node);
  509. }
  510. else {
  511. if ((node = allocator_alloc(pool->allocator, size)) == NULL) {
  512. if (pool->abort_fn)
  513. pool->abort_fn(APR_ENOMEM);
  514. mem = NULL;
  515. goto end;
  516. }
  517. }
  518. node->free_index = 0;
  519. mem = node->first_avail;
  520. node->first_avail += size;
  521. list_insert(node, active);
  522. pool->active = node;
  523. free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
  524. BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
  525. active->free_index = (APR_UINT32_TRUNC_CAST)free_index;
  526. node = active->next;
  527. if (free_index >= node->free_index)
  528. goto end;
  529. do {
  530. node = node->next;
  531. }
  532. while (free_index < node->free_index);
  533. list_remove(active);
  534. list_insert(active, node);
  535. end:
  536. #if APR_HAS_THREADS
  537. if (pool->user_mutex) fspr_thread_mutex_unlock(pool->user_mutex);
  538. #endif
  539. return mem;
  540. }
  541. /* Provide an implementation of fspr_pcalloc for backward compatibility
  542. * with code built before fspr_pcalloc was a macro
  543. */
  544. #ifdef fspr_pcalloc
  545. #undef fspr_pcalloc
  546. #endif
  547. APR_DECLARE(void *) fspr_pcalloc(fspr_pool_t *pool, fspr_size_t size);
  548. APR_DECLARE(void *) fspr_pcalloc(fspr_pool_t *pool, fspr_size_t size)
  549. {
  550. void *mem;
  551. size = APR_ALIGN_DEFAULT(size);
  552. if ((mem = fspr_palloc(pool, size)) != NULL) {
  553. memset(mem, 0, size);
  554. }
  555. return mem;
  556. }
  557. /*
  558. * Pool creation/destruction
  559. */
  560. APR_DECLARE(void) fspr_pool_clear(fspr_pool_t *pool)
  561. {
  562. fspr_memnode_t *active;
  563. #if APR_HAS_THREADS
  564. if (pool->user_mutex) fspr_thread_mutex_lock(pool->user_mutex);
  565. #endif
  566. /* Destroy the subpools. The subpools will detach themselves from
  567. * this pool thus this loop is safe and easy.
  568. */
  569. while (pool->child)
  570. fspr_pool_destroy(pool->child);
  571. /* Run cleanups */
  572. run_cleanups(&pool->cleanups);
  573. pool->cleanups = NULL;
  574. pool->free_cleanups = NULL;
  575. /* Free subprocesses */
  576. free_proc_chain(pool->subprocesses);
  577. pool->subprocesses = NULL;
  578. /* Clear the user data. */
  579. pool->user_data = NULL;
  580. /* Find the node attached to the pool structure, reset it, make
  581. * it the active node and free the rest of the nodes.
  582. */
  583. active = pool->active = pool->self;
  584. active->first_avail = pool->self_first_avail;
  585. if (active->next == active)
  586. goto end;
  587. *active->ref = NULL;
  588. allocator_free(pool->allocator, active->next);
  589. active->next = active;
  590. active->ref = &active->next;
  591. end:
  592. #if APR_HAS_THREADS
  593. if (pool->user_mutex) fspr_thread_mutex_unlock(pool->user_mutex);
  594. #endif
  595. }
  596. APR_DECLARE(void) fspr_pool_destroy(fspr_pool_t *pool)
  597. {
  598. fspr_memnode_t *active;
  599. fspr_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. fspr_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. fspr_thread_mutex_t *mutex;
  613. if ((mutex = fspr_allocator_mutex_get(pool->parent->allocator)) != NULL)
  614. fspr_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. fspr_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 (fspr_allocator_owner_get(allocator) == pool) {
  631. /* Make sure to remove the lock, since it is highly likely to
  632. * be invalid now.
  633. */
  634. fspr_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 (fspr_allocator_owner_get(allocator) == pool) {
  647. fspr_allocator_destroy(allocator);
  648. }
  649. }
  650. APR_DECLARE(fspr_status_t) fspr_pool_create_ex(fspr_pool_t **newpool,
  651. fspr_pool_t *parent,
  652. fspr_abortfunc_t abort_fn,
  653. fspr_allocator_t *allocator)
  654. {
  655. fspr_pool_t *pool;
  656. fspr_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. if (!parent) {
  664. /* There is no way to continue without an allocator when no parent */
  665. if (abort_fn)
  666. abort_fn(APR_EINVAL);
  667. return APR_EINVAL;
  668. }
  669. allocator = parent->allocator;
  670. }
  671. if ((node = allocator_alloc(allocator,
  672. MIN_ALLOC - APR_MEMNODE_T_SIZE)) == NULL) {
  673. if (abort_fn)
  674. abort_fn(APR_ENOMEM);
  675. return APR_ENOMEM;
  676. }
  677. node->next = node;
  678. node->ref = &node->next;
  679. pool = (fspr_pool_t *)node->first_avail;
  680. node->first_avail = pool->self_first_avail = (char *)pool + SIZEOF_POOL_T;
  681. pool->allocator = allocator;
  682. pool->active = pool->self = node;
  683. pool->abort_fn = abort_fn;
  684. pool->child = NULL;
  685. pool->cleanups = NULL;
  686. pool->free_cleanups = NULL;
  687. pool->subprocesses = NULL;
  688. pool->user_data = NULL;
  689. pool->tag = NULL;
  690. #if APR_HAS_THREADS
  691. pool->user_mutex = NULL;
  692. #endif
  693. #ifdef NETWARE
  694. pool->owner_proc = (fspr_os_proc_t)getnlmhandle();
  695. #endif /* defined(NETWARE) */
  696. if ((pool->parent = parent) != NULL) {
  697. #if APR_HAS_THREADS
  698. fspr_thread_mutex_t *mutex;
  699. if ((mutex = fspr_allocator_mutex_get(parent->allocator)) != NULL)
  700. fspr_thread_mutex_lock(mutex);
  701. #endif /* APR_HAS_THREADS */
  702. if ((pool->sibling = parent->child) != NULL)
  703. pool->sibling->ref = &pool->sibling;
  704. parent->child = pool;
  705. pool->ref = &parent->child;
  706. #if APR_HAS_THREADS
  707. if (mutex)
  708. fspr_thread_mutex_unlock(mutex);
  709. #endif /* APR_HAS_THREADS */
  710. }
  711. else {
  712. pool->sibling = NULL;
  713. pool->ref = NULL;
  714. }
  715. *newpool = pool;
  716. return APR_SUCCESS;
  717. }
  718. /*
  719. * "Print" functions
  720. */
  721. /*
  722. * fspr_psprintf is implemented by writing directly into the current
  723. * block of the pool, starting right at first_avail. If there's
  724. * insufficient room, then a new block is allocated and the earlier
  725. * output is copied over. The new block isn't linked into the pool
  726. * until all the output is done.
  727. *
  728. * Note that this is completely safe because nothing else can
  729. * allocate in this fspr_pool_t while fspr_psprintf is running. alarms are
  730. * blocked, and the only thing outside of fspr_pools.c that's invoked
  731. * is fspr_vformatter -- which was purposefully written to be
  732. * self-contained with no callouts.
  733. */
  734. struct psprintf_data {
  735. fspr_vformatter_buff_t vbuff;
  736. fspr_memnode_t *node;
  737. fspr_pool_t *pool;
  738. fspr_byte_t got_a_new_node;
  739. fspr_memnode_t *free;
  740. };
  741. #define APR_PSPRINTF_MIN_STRINGSIZE 32
  742. static int psprintf_flush(fspr_vformatter_buff_t *vbuff)
  743. {
  744. struct psprintf_data *ps = (struct psprintf_data *)vbuff;
  745. fspr_memnode_t *node, *active;
  746. fspr_size_t cur_len, size;
  747. char *strp;
  748. fspr_pool_t *pool;
  749. fspr_size_t free_index;
  750. pool = ps->pool;
  751. active = ps->node;
  752. strp = ps->vbuff.curpos;
  753. cur_len = strp - active->first_avail;
  754. size = cur_len << 1;
  755. /* Make sure that we don't try to use a block that has less
  756. * than APR_PSPRINTF_MIN_STRINGSIZE bytes left in it. This
  757. * also catches the case where size == 0, which would result
  758. * in reusing a block that can't even hold the NUL byte.
  759. */
  760. if (size < APR_PSPRINTF_MIN_STRINGSIZE)
  761. size = APR_PSPRINTF_MIN_STRINGSIZE;
  762. node = active->next;
  763. if (!ps->got_a_new_node
  764. && size < (fspr_size_t)(node->endp - node->first_avail)) {
  765. list_remove(node);
  766. list_insert(node, active);
  767. node->free_index = 0;
  768. pool->active = node;
  769. free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
  770. BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
  771. active->free_index = (APR_UINT32_TRUNC_CAST)free_index;
  772. node = active->next;
  773. if (free_index < node->free_index) {
  774. do {
  775. node = node->next;
  776. }
  777. while (free_index < node->free_index);
  778. list_remove(active);
  779. list_insert(active, node);
  780. }
  781. node = pool->active;
  782. }
  783. else {
  784. if ((node = allocator_alloc(pool->allocator, size)) == NULL)
  785. return -1;
  786. if (ps->got_a_new_node) {
  787. active->next = ps->free;
  788. ps->free = active;
  789. }
  790. ps->got_a_new_node = 1;
  791. }
  792. memcpy(node->first_avail, active->first_avail, cur_len);
  793. ps->node = node;
  794. ps->vbuff.curpos = node->first_avail + cur_len;
  795. ps->vbuff.endpos = node->endp - 1; /* Save a byte for NUL terminator */
  796. return 0;
  797. }
  798. APR_DECLARE(char *) fspr_pvsprintf(fspr_pool_t *pool, const char *fmt, va_list ap)
  799. {
  800. struct psprintf_data ps;
  801. char *strp;
  802. fspr_size_t size;
  803. fspr_memnode_t *active, *node;
  804. fspr_size_t free_index;
  805. #if APR_HAS_THREADS
  806. if (pool->user_mutex) fspr_thread_mutex_lock(pool->user_mutex);
  807. #endif
  808. ps.node = pool->active;
  809. ps.pool = pool;
  810. ps.vbuff.curpos = ps.node->first_avail;
  811. /* Save a byte for the NUL terminator */
  812. ps.vbuff.endpos = ps.node->endp - 1;
  813. ps.got_a_new_node = 0;
  814. ps.free = NULL;
  815. /* Make sure that the first node passed to fspr_vformatter has at least
  816. * room to hold the NUL terminator.
  817. */
  818. if (ps.node->first_avail == ps.node->endp) {
  819. if (psprintf_flush(&ps.vbuff) == -1) {
  820. if (pool->abort_fn) {
  821. pool->abort_fn(APR_ENOMEM);
  822. }
  823. strp = NULL;
  824. goto end;
  825. }
  826. }
  827. if (fspr_vformatter(psprintf_flush, &ps.vbuff, fmt, ap) == -1) {
  828. if (pool->abort_fn)
  829. pool->abort_fn(APR_ENOMEM);
  830. strp = NULL;
  831. goto end;
  832. }
  833. strp = ps.vbuff.curpos;
  834. *strp++ = '\0';
  835. size = strp - ps.node->first_avail;
  836. size = APR_ALIGN_DEFAULT(size);
  837. strp = ps.node->first_avail;
  838. ps.node->first_avail += size;
  839. if (ps.free)
  840. allocator_free(pool->allocator, ps.free);
  841. /*
  842. * Link the node in if it's a new one
  843. */
  844. if (!ps.got_a_new_node)
  845. goto end;
  846. active = pool->active;
  847. node = ps.node;
  848. node->free_index = 0;
  849. list_insert(node, active);
  850. pool->active = node;
  851. free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
  852. BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
  853. active->free_index = (APR_UINT32_TRUNC_CAST)free_index;
  854. node = active->next;
  855. if (free_index >= node->free_index)
  856. goto end;
  857. do {
  858. node = node->next;
  859. }
  860. while (free_index < node->free_index);
  861. list_remove(active);
  862. list_insert(active, node);
  863. end:
  864. #if APR_HAS_THREADS
  865. if (pool->user_mutex) fspr_thread_mutex_unlock(pool->user_mutex);
  866. #endif
  867. return strp;
  868. }
  869. #else /* APR_POOL_DEBUG */
  870. /*
  871. * Debug helper functions
  872. */
  873. /*
  874. * Walk the pool tree rooted at pool, depth first. When fn returns
  875. * anything other than 0, abort the traversal and return the value
  876. * returned by fn.
  877. */
  878. static int fspr_pool_walk_tree(fspr_pool_t *pool,
  879. int (*fn)(fspr_pool_t *pool, void *data),
  880. void *data)
  881. {
  882. int rv;
  883. fspr_pool_t *child;
  884. rv = fn(pool, data);
  885. if (rv)
  886. return rv;
  887. #if APR_HAS_THREADS
  888. if (pool->mutex) {
  889. fspr_thread_mutex_lock(pool->mutex);
  890. }
  891. #endif /* APR_HAS_THREADS */
  892. child = pool->child;
  893. while (child) {
  894. rv = fspr_pool_walk_tree(child, fn, data);
  895. if (rv)
  896. break;
  897. child = child->sibling;
  898. }
  899. #if APR_HAS_THREADS
  900. if (pool->mutex) {
  901. fspr_thread_mutex_unlock(pool->mutex);
  902. }
  903. #endif /* APR_HAS_THREADS */
  904. return rv;
  905. }
  906. APR_DECLARE(int) fspr_pool_walk_tree_debug(fspr_pool_t *pool,
  907. int(*fn)(fspr_pool_t *pool, void *data),
  908. void *data)
  909. {
  910. return fspr_pool_walk_tree(pool, fn, data);
  911. }
  912. APR_DECLARE(void) fspr_pool_get_stats(fspr_pool_t *pool, unsigned int *alloc, unsigned int *total_alloc, unsigned int *clear)
  913. {
  914. if (pool) {
  915. *alloc = pool->stat_alloc;
  916. *total_alloc = pool->stat_total_alloc;
  917. *clear = pool->stat_clear;
  918. }
  919. }
  920. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  921. static void fspr_pool_log_event(fspr_pool_t *pool, const char *event,
  922. const char *file_line, int deref)
  923. {
  924. if (file_stderr) {
  925. if (deref) {
  926. fspr_file_printf(file_stderr,
  927. "POOL DEBUG: "
  928. "[%lu"
  929. #if APR_HAS_THREADS
  930. "/%lu"
  931. #endif /* APR_HAS_THREADS */
  932. "] "
  933. "%7s "
  934. "(%10lu/%10lu/%10lu) "
  935. "0x%08X \"%s\" "
  936. "<%s> "
  937. "(%u/%u/%u) "
  938. "\n",
  939. (unsigned long)getpid(),
  940. #if APR_HAS_THREADS
  941. (unsigned long)fspr_os_thread_current(),
  942. #endif /* APR_HAS_THREADS */
  943. event,
  944. (unsigned long)fspr_pool_num_bytes(pool, 0),
  945. (unsigned long)fspr_pool_num_bytes(pool, 1),
  946. (unsigned long)fspr_pool_num_bytes(global_pool, 1),
  947. (unsigned int)pool, pool->tag,
  948. file_line,
  949. pool->stat_alloc, pool->stat_total_alloc, pool->stat_clear);
  950. }
  951. else {
  952. fspr_file_printf(file_stderr,
  953. "POOL DEBUG: "
  954. "[%lu"
  955. #if APR_HAS_THREADS
  956. "/%lu"
  957. #endif /* APR_HAS_THREADS */
  958. "] "
  959. "%7s "
  960. " "
  961. "0x%08X "
  962. "<%s> "
  963. "\n",
  964. (unsigned long)getpid(),
  965. #if APR_HAS_THREADS
  966. (unsigned long)fspr_os_thread_current(),
  967. #endif /* APR_HAS_THREADS */
  968. event,
  969. (unsigned int)pool,
  970. file_line);
  971. }
  972. }
  973. }
  974. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  975. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME)
  976. static int pool_is_child_of(fspr_pool_t *parent, void *data)
  977. {
  978. fspr_pool_t *pool = (fspr_pool_t *)data;
  979. return (pool == parent);
  980. }
  981. static int fspr_pool_is_child_of(fspr_pool_t *pool, fspr_pool_t *parent)
  982. {
  983. if (parent == NULL)
  984. return 0;
  985. return fspr_pool_walk_tree(parent, pool_is_child_of, pool);
  986. }
  987. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME) */
  988. static void fspr_pool_check_integrity(fspr_pool_t *pool)
  989. {
  990. /* Rule of thumb: use of the global pool is always
  991. * ok, since the only user is fspr_pools.c. Unless
  992. * people have searched for the top level parent and
  993. * started to use that...
  994. */
  995. if (pool == global_pool || global_pool == NULL)
  996. return;
  997. /* Lifetime
  998. * This basically checks to see if the pool being used is still
  999. * a relative to the global pool. If not it was previously
  1000. * destroyed, in which case we abort().
  1001. */
  1002. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME)
  1003. if (!fspr_pool_is_child_of(pool, global_pool)) {
  1004. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  1005. fspr_pool_log_event(pool, "LIFE",
  1006. __FILE__ ":fspr_pool_integrity check", 0);
  1007. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  1008. abort();
  1009. }
  1010. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME) */
  1011. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_OWNER)
  1012. #if APR_HAS_THREADS
  1013. if (!fspr_os_thread_equal(pool->owner, fspr_os_thread_current())) {
  1014. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  1015. fspr_pool_log_event(pool, "THREAD",
  1016. __FILE__ ":fspr_pool_integrity check", 0);
  1017. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  1018. abort();
  1019. }
  1020. #endif /* APR_HAS_THREADS */
  1021. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_OWNER) */
  1022. }
  1023. /*
  1024. * Initialization (debug)
  1025. */
  1026. APR_DECLARE(fspr_status_t) fspr_pool_initialize(void)
  1027. {
  1028. fspr_status_t rv;
  1029. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  1030. char *logpath;
  1031. #endif
  1032. if (fspr_pools_initialized++)
  1033. return APR_SUCCESS;
  1034. /* Since the debug code works a bit differently then the
  1035. * regular pools code, we ask for a lock here. The regular
  1036. * pools code has got this lock embedded in the global
  1037. * allocator, a concept unknown to debug mode.
  1038. */
  1039. if ((rv = fspr_pool_create_ex(&global_pool, NULL, NULL,
  1040. NULL)) != APR_SUCCESS) {
  1041. return rv;
  1042. }
  1043. fspr_pool_tag(global_pool, "APR global pool");
  1044. fspr_pools_initialized = 1;
  1045. /* This has to happen here because mutexes might be backed by
  1046. * atomics. It used to be snug and safe in fspr_initialize().
  1047. */
  1048. if ((rv = fspr_atomic_init(global_pool)) != APR_SUCCESS) {
  1049. return rv;
  1050. }
  1051. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  1052. rv = fspr_env_get(&logpath, "APR_POOL_DEBUG_LOG", global_pool);
  1053. if (rv == APR_SUCCESS) {
  1054. fspr_file_open(&file_stderr, logpath, APR_APPEND|APR_WRITE|APR_CREATE,
  1055. APR_OS_DEFAULT, global_pool);
  1056. }
  1057. else {
  1058. fspr_file_open_stderr(&file_stderr, global_pool);
  1059. }
  1060. if (file_stderr) {
  1061. fspr_file_printf(file_stderr,
  1062. "POOL DEBUG: [PID"
  1063. #if APR_HAS_THREADS
  1064. "/TID"
  1065. #endif /* APR_HAS_THREADS */
  1066. "] ACTION (SIZE /POOL SIZE /TOTAL SIZE) "
  1067. "POOL \"TAG\" <__FILE__:__LINE__> (ALLOCS/TOTAL ALLOCS/CLEARS)\n");
  1068. fspr_pool_log_event(global_pool, "GLOBAL", __FILE__ ":fspr_pool_initialize", 0);
  1069. }
  1070. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  1071. return APR_SUCCESS;
  1072. }
  1073. APR_DECLARE(void) fspr_pool_terminate(void)
  1074. {
  1075. if (!fspr_pools_initialized)
  1076. return;
  1077. fspr_pools_initialized = 0;
  1078. fspr_pool_destroy(global_pool); /* This will also destroy the mutex */
  1079. global_pool = NULL;
  1080. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  1081. file_stderr = NULL;
  1082. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  1083. }
  1084. /*
  1085. * Memory allocation (debug)
  1086. */
  1087. static void *pool_alloc(fspr_pool_t *pool, fspr_size_t size)
  1088. {
  1089. debug_node_t *node;
  1090. void *mem;
  1091. if ((mem = malloc(size)) == NULL) {
  1092. if (pool->abort_fn)
  1093. pool->abort_fn(APR_ENOMEM);
  1094. return NULL;
  1095. }
  1096. node = pool->nodes;
  1097. if (node == NULL || node->index == 64) {
  1098. if ((node = malloc(SIZEOF_DEBUG_NODE_T)) == NULL) {
  1099. if (pool->abort_fn)
  1100. pool->abort_fn(APR_ENOMEM);
  1101. return NULL;
  1102. }
  1103. memset(node, 0, SIZEOF_DEBUG_NODE_T);
  1104. node->next = pool->nodes;
  1105. pool->nodes = node;
  1106. node->index = 0;
  1107. }
  1108. node->beginp[node->index] = mem;
  1109. node->endp[node->index] = (char *)mem + size;
  1110. node->index++;
  1111. pool->stat_alloc++;
  1112. pool->stat_total_alloc++;
  1113. return mem;
  1114. }
  1115. APR_DECLARE(void *) fspr_palloc_debug(fspr_pool_t *pool, fspr_size_t size,
  1116. const char *file_line)
  1117. {
  1118. void *mem;
  1119. fspr_pool_check_integrity(pool);
  1120. mem = pool_alloc(pool, size);
  1121. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC)
  1122. fspr_pool_log_event(pool, "PALLOC", file_line, 1);
  1123. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC) */
  1124. return mem;
  1125. }
  1126. APR_DECLARE(void *) fspr_pcalloc_debug(fspr_pool_t *pool, fspr_size_t size,
  1127. const char *file_line)
  1128. {
  1129. void *mem;
  1130. fspr_pool_check_integrity(pool);
  1131. mem = pool_alloc(pool, size);
  1132. memset(mem, 0, size);
  1133. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC)
  1134. fspr_pool_log_event(pool, "PCALLOC", file_line, 1);
  1135. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC) */
  1136. return mem;
  1137. }
  1138. /*
  1139. * Pool creation/destruction (debug)
  1140. */
  1141. #define POOL_POISON_BYTE 'A'
  1142. static void pool_clear_debug(fspr_pool_t *pool, const char *file_line)
  1143. {
  1144. debug_node_t *node;
  1145. fspr_uint32_t index;
  1146. /* Destroy the subpools. The subpools will detach themselves from
  1147. * this pool thus this loop is safe and easy.
  1148. */
  1149. while (pool->child)
  1150. pool_destroy_debug(pool->child, file_line);
  1151. /* Run cleanups */
  1152. run_cleanups(&pool->cleanups);
  1153. pool->free_cleanups = NULL;
  1154. pool->cleanups = NULL;
  1155. /* If new child pools showed up, this is a reason to raise a flag */
  1156. if (pool->child)
  1157. abort();
  1158. /* Free subprocesses */
  1159. free_proc_chain(pool->subprocesses);
  1160. pool->subprocesses = NULL;
  1161. /* Clear the user data. */
  1162. pool->user_data = NULL;
  1163. /* Free the blocks, scribbling over them first to help highlight
  1164. * use-after-free issues. */
  1165. while ((node = pool->nodes) != NULL) {
  1166. pool->nodes = node->next;
  1167. for (index = 0; index < node->index; index++) {
  1168. memset(node->beginp[index], POOL_POISON_BYTE,
  1169. (char *)node->endp[index] - (char *)node->beginp[index]);
  1170. free(node->beginp[index]);
  1171. }
  1172. memset(node, POOL_POISON_BYTE, SIZEOF_DEBUG_NODE_T);
  1173. free(node);
  1174. }
  1175. pool->stat_alloc = 0;
  1176. pool->stat_clear++;
  1177. }
  1178. APR_DECLARE(void) fspr_pool_clear_debug(fspr_pool_t *pool,
  1179. const char *file_line)
  1180. {
  1181. #if APR_HAS_THREADS
  1182. fspr_thread_mutex_t *mutex = NULL;
  1183. #endif
  1184. fspr_pool_check_integrity(pool);
  1185. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
  1186. fspr_pool_log_event(pool, "CLEAR", file_line, 1);
  1187. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
  1188. #if APR_HAS_THREADS
  1189. if (pool->parent != NULL)
  1190. mutex = pool->parent->mutex;
  1191. /* Lock the parent mutex before clearing so that if we have our
  1192. * own mutex it won't be accessed by fspr_pool_walk_tree after
  1193. * it has been destroyed.
  1194. */
  1195. if (mutex != NULL && mutex != pool->mutex) {
  1196. fspr_thread_mutex_lock(mutex);
  1197. }
  1198. #endif
  1199. pool_clear_debug(pool, file_line);
  1200. #if APR_HAS_THREADS
  1201. /* If we had our own mutex, it will have been destroyed by
  1202. * the registered cleanups. Recreate the mutex. Unlock
  1203. * the mutex we obtained above.
  1204. */
  1205. if (mutex != pool->mutex) {
  1206. (void)fspr_thread_mutex_create(&pool->mutex,
  1207. APR_THREAD_MUTEX_NESTED, pool);
  1208. if (mutex != NULL)
  1209. (void)fspr_thread_mutex_unlock(mutex);
  1210. }
  1211. #endif /* APR_HAS_THREADS */
  1212. }
  1213. static void pool_destroy_debug(fspr_pool_t *pool, const char *file_line)
  1214. {
  1215. fspr_pool_check_integrity(pool);
  1216. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
  1217. fspr_pool_log_event(pool, "DESTROY", file_line, 1);
  1218. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
  1219. pool_clear_debug(pool, file_line);
  1220. /* Remove the pool from the parents child list */
  1221. if (pool->parent) {
  1222. #if APR_HAS_THREADS
  1223. fspr_thread_mutex_t *mutex;
  1224. if ((mutex = pool->parent->mutex) != NULL)
  1225. fspr_thread_mutex_lock(mutex);
  1226. #endif /* APR_HAS_THREADS */
  1227. if ((*pool->ref = pool->sibling) != NULL)
  1228. pool->sibling->ref = pool->ref;
  1229. #if APR_HAS_THREADS
  1230. if (mutex)
  1231. fspr_thread_mutex_unlock(mutex);
  1232. #endif /* APR_HAS_THREADS */
  1233. }
  1234. if (pool->allocator != NULL
  1235. && fspr_allocator_owner_get(pool->allocator) == pool) {
  1236. fspr_allocator_destroy(pool->allocator);
  1237. }
  1238. /* Free the pool itself */
  1239. free(pool);
  1240. }
  1241. APR_DECLARE(void) fspr_pool_destroy_debug(fspr_pool_t *pool,
  1242. const char *file_line)
  1243. {
  1244. if (pool->joined) {
  1245. /* Joined pools must not be explicitly destroyed; the caller
  1246. * has broken the guarantee. */
  1247. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
  1248. fspr_pool_log_event(pool, "LIFE",
  1249. __FILE__ ":fspr_pool_destroy abort on joined", 0);
  1250. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
  1251. abort();
  1252. }
  1253. pool_destroy_debug(pool, file_line);
  1254. }
  1255. APR_DECLARE(fspr_status_t) fspr_pool_create_ex_debug(fspr_pool_t **newpool,
  1256. fspr_pool_t *parent,
  1257. fspr_abortfunc_t abort_fn,
  1258. fspr_allocator_t *allocator,
  1259. const char *file_line)
  1260. {
  1261. fspr_pool_t *pool;
  1262. *newpool = NULL;
  1263. if (!parent) {
  1264. parent = global_pool;
  1265. }
  1266. else {
  1267. fspr_pool_check_integrity(parent);
  1268. if (!allocator)
  1269. allocator = parent->allocator;
  1270. }
  1271. if (!abort_fn && parent)
  1272. abort_fn = parent->abort_fn;
  1273. if ((pool = malloc(SIZEOF_POOL_T)) == NULL) {
  1274. if (abort_fn)
  1275. abort_fn(APR_ENOMEM);
  1276. return APR_ENOMEM;
  1277. }
  1278. memset(pool, 0, SIZEOF_POOL_T);
  1279. pool->allocator = allocator;
  1280. pool->abort_fn = abort_fn;
  1281. pool->tag = file_line;
  1282. pool->file_line = file_line;
  1283. if ((pool->parent = parent) != NULL) {
  1284. #if APR_HAS_THREADS
  1285. if (parent->mutex)
  1286. fspr_thread_mutex_lock(parent->mutex);
  1287. #endif /* APR_HAS_THREADS */
  1288. if ((pool->sibling = parent->child) != NULL)
  1289. pool->sibling->ref = &pool->sibling;
  1290. parent->child = pool;
  1291. pool->ref = &parent->child;
  1292. #if APR_HAS_THREADS
  1293. if (parent->mutex)
  1294. fspr_thread_mutex_unlock(parent->mutex);
  1295. #endif /* APR_HAS_THREADS */
  1296. }
  1297. else {
  1298. pool->sibling = NULL;
  1299. pool->ref = NULL;
  1300. }
  1301. #if APR_HAS_THREADS
  1302. pool->owner = fspr_os_thread_current();
  1303. #endif /* APR_HAS_THREADS */
  1304. #ifdef NETWARE
  1305. pool->owner_proc = (fspr_os_proc_t)getnlmhandle();
  1306. #endif /* defined(NETWARE) */
  1307. if (parent == NULL || parent->allocator != allocator) {
  1308. #if APR_HAS_THREADS
  1309. fspr_status_t rv;
  1310. /* No matter what the creation flags say, always create
  1311. * a lock. Without it integrity_check and fspr_pool_num_bytes
  1312. * blow up (because they traverse pools child lists that
  1313. * possibly belong to another thread, in combination with
  1314. * the pool having no lock). However, this might actually
  1315. * hide problems like creating a child pool of a pool
  1316. * belonging to another thread.
  1317. */
  1318. if ((rv = fspr_thread_mutex_create(&pool->mutex,
  1319. APR_THREAD_MUTEX_NESTED, pool)) != APR_SUCCESS) {
  1320. free(pool);
  1321. return rv;
  1322. }
  1323. #endif /* APR_HAS_THREADS */
  1324. }
  1325. else {
  1326. #if APR_HAS_THREADS
  1327. if (parent)
  1328. pool->mutex = parent->mutex;
  1329. #endif /* APR_HAS_THREADS */
  1330. }
  1331. *newpool = pool;
  1332. #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
  1333. fspr_pool_log_event(pool, "CREATE", file_line, 1);
  1334. #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
  1335. return APR_SUCCESS;
  1336. }
  1337. /*
  1338. * "Print" functions (debug)
  1339. */
  1340. struct psprintf_data {
  1341. fspr_vformatter_buff_t vbuff;
  1342. char *mem;
  1343. fspr_size_t size;
  1344. };
  1345. static int psprintf_flush(fspr_vformatter_buff_t *vbuff)
  1346. {
  1347. struct psprintf_data *ps = (struct psprintf_data *)vbuff;
  1348. fspr_size_t size;
  1349. size = ps->vbuff.curpos - ps->mem;
  1350. ps->size <<= 1;
  1351. if ((ps->mem = realloc(ps->mem, ps->size)) == NULL)
  1352. return -1;
  1353. ps->vbuff.curpos = ps->mem + size;
  1354. ps->vbuff.endpos = ps->mem + ps->size - 1;
  1355. return 0;
  1356. }
  1357. APR_DECLARE(char *) fspr_pvsprintf(fspr_pool_t *pool, const char *fmt, va_list ap)
  1358. {
  1359. struct psprintf_data ps;
  1360. debug_node_t *node;
  1361. fspr_pool_check_integrity(pool);
  1362. ps.size = 64;
  1363. ps.mem = malloc(ps.size);
  1364. ps.vbuff.curpos = ps.mem;
  1365. /* Save a byte for the NUL terminator */
  1366. ps.vbuff.endpos = ps.mem + ps.size - 1;
  1367. if (fspr_vformatter(psprintf_flush, &ps.vbuff, fmt, ap) == -1) {
  1368. if (pool->abort_fn)
  1369. pool->abort_fn(APR_ENOMEM);
  1370. return NULL;
  1371. }
  1372. *ps.vbuff.curpos++ = '\0';
  1373. /*
  1374. * Link the node in
  1375. */
  1376. node = pool->nodes;
  1377. if (node == NULL || node->index == 64) {
  1378. if ((node = malloc(SIZEOF_DEBUG_NODE_T)) == NULL) {
  1379. if (pool->abort_fn)
  1380. pool->abort_fn(APR_ENOMEM);
  1381. return NULL;
  1382. }
  1383. node->next = pool->nodes;
  1384. pool->nodes = node;
  1385. node->index = 0;
  1386. }
  1387. node->beginp[node->index] = ps.mem;
  1388. node->endp[node->index] = ps.mem + ps.size;
  1389. node->index++;
  1390. return ps.mem;
  1391. }
  1392. /*
  1393. * Debug functions
  1394. */
  1395. APR_DECLARE(void) fspr_pool_join(fspr_pool_t *p, fspr_pool_t *sub)
  1396. {
  1397. #if APR_POOL_DEBUG
  1398. if (sub->parent != p) {
  1399. abort();
  1400. }
  1401. sub->joined = p;
  1402. #endif
  1403. }
  1404. static int pool_find(fspr_pool_t *pool, void *data)
  1405. {
  1406. void **pmem = (void **)data;
  1407. debug_node_t *node;
  1408. fspr_uint32_t index;
  1409. node = pool->nodes;
  1410. while (node) {
  1411. for (index = 0; index < node->index; index++) {
  1412. if (node->beginp[index] <= *pmem
  1413. && node->endp[index] > *pmem) {
  1414. *pmem = pool;
  1415. return 1;
  1416. }
  1417. }
  1418. node = node->next;
  1419. }
  1420. return 0;
  1421. }
  1422. APR_DECLARE(fspr_pool_t *) fspr_pool_find(const void *mem)
  1423. {
  1424. void *pool = (void *)mem;
  1425. if (fspr_pool_walk_tree(global_pool, pool_find, &pool))
  1426. return pool;
  1427. return NULL;
  1428. }
  1429. static int pool_num_bytes(fspr_pool_t *pool, void *data)
  1430. {
  1431. fspr_size_t *psize = (fspr_size_t *)data;
  1432. debug_node_t *node;
  1433. fspr_uint32_t index;
  1434. node = pool->nodes;
  1435. while (node) {
  1436. for (index = 0; index < node->index; index++) {
  1437. *psize += (char *)node->endp[index] - (char *)node->beginp[index];
  1438. }
  1439. node = node->next;
  1440. }
  1441. return 0;
  1442. }
  1443. APR_DECLARE(fspr_size_t) fspr_pool_num_bytes(fspr_pool_t *pool, int recurse)
  1444. {
  1445. fspr_size_t size = 0;
  1446. if (!recurse) {
  1447. pool_num_bytes(pool, &size);
  1448. return size;
  1449. }
  1450. fspr_pool_walk_tree(pool, pool_num_bytes, &size);
  1451. return size;
  1452. }
  1453. APR_DECLARE(void) fspr_pool_lock(fspr_pool_t *pool, int flag)
  1454. {
  1455. }
  1456. #endif /* !APR_POOL_DEBUG */
  1457. #ifdef NETWARE
  1458. void netware_pool_proc_cleanup ()
  1459. {
  1460. fspr_pool_t *pool = global_pool->child;
  1461. fspr_os_proc_t owner_proc = (fspr_os_proc_t)getnlmhandle();
  1462. while (pool) {
  1463. if (pool->owner_proc == owner_proc) {
  1464. fspr_pool_destroy (pool);
  1465. pool = global_pool->child;
  1466. }
  1467. else {
  1468. pool = pool->sibling;
  1469. }
  1470. }
  1471. return;
  1472. }
  1473. #endif /* defined(NETWARE) */
  1474. /*
  1475. * "Print" functions (common)
  1476. */
  1477. APR_DECLARE_NONSTD(char *) fspr_psprintf(fspr_pool_t *p, const char *fmt, ...)
  1478. {
  1479. va_list ap;
  1480. char *res;
  1481. va_start(ap, fmt);
  1482. res = fspr_pvsprintf(p, fmt, ap);
  1483. va_end(ap);
  1484. return res;
  1485. }
  1486. /*
  1487. * Pool Properties
  1488. */
  1489. APR_DECLARE(void) fspr_pool_abort_set(fspr_abortfunc_t abort_fn,
  1490. fspr_pool_t *pool)
  1491. {
  1492. pool->abort_fn = abort_fn;
  1493. }
  1494. APR_DECLARE(fspr_abortfunc_t) fspr_pool_abort_get(fspr_pool_t *pool)
  1495. {
  1496. return pool->abort_fn;
  1497. }
  1498. APR_DECLARE(fspr_pool_t *) fspr_pool_parent_get(fspr_pool_t *pool)
  1499. {
  1500. #ifdef NETWARE
  1501. /* On NetWare, don't return the global_pool, return the application pool
  1502. as the top most pool */
  1503. if (pool->parent == global_pool)
  1504. return NULL;
  1505. else
  1506. #endif
  1507. return pool->parent;
  1508. }
  1509. APR_DECLARE(fspr_allocator_t *) fspr_pool_allocator_get(fspr_pool_t *pool)
  1510. {
  1511. return pool->allocator;
  1512. }
  1513. /* return TRUE if a is an ancestor of b
  1514. * NULL is considered an ancestor of all pools
  1515. */
  1516. APR_DECLARE(int) fspr_pool_is_ancestor(fspr_pool_t *a, fspr_pool_t *b)
  1517. {
  1518. if (a == NULL)
  1519. return 1;
  1520. #if APR_POOL_DEBUG
  1521. /* Find the pool with the longest lifetime guaranteed by the
  1522. * caller: */
  1523. while (a->joined) {
  1524. a = a->joined;
  1525. }
  1526. #endif
  1527. while (b) {
  1528. if (a == b)
  1529. return 1;
  1530. b = b->parent;
  1531. }
  1532. return 0;
  1533. }
  1534. APR_DECLARE(const char *) fspr_pool_tag(fspr_pool_t *pool, const char *tag)
  1535. {
  1536. if (tag) {
  1537. pool->tag = tag;
  1538. }
  1539. return pool->tag;
  1540. }
  1541. /*
  1542. * User data management
  1543. */
  1544. APR_DECLARE(fspr_status_t) fspr_pool_userdata_set(const void *data, const char *key,
  1545. fspr_status_t (*cleanup) (void *),
  1546. fspr_pool_t *pool)
  1547. {
  1548. #if APR_POOL_DEBUG
  1549. fspr_pool_check_integrity(pool);
  1550. #endif /* APR_POOL_DEBUG */
  1551. if (pool->user_data == NULL)
  1552. pool->user_data = fspr_hash_make(pool);
  1553. if (fspr_hash_get(pool->user_data, key, APR_HASH_KEY_STRING) == NULL) {
  1554. char *new_key = fspr_pstrdup(pool, key);
  1555. fspr_hash_set(pool->user_data, new_key, APR_HASH_KEY_STRING, data);
  1556. }
  1557. else {
  1558. fspr_hash_set(pool->user_data, key, APR_HASH_KEY_STRING, data);
  1559. }
  1560. if (cleanup)
  1561. fspr_pool_cleanup_register(pool, data, cleanup, cleanup);
  1562. return APR_SUCCESS;
  1563. }
  1564. APR_DECLARE(fspr_status_t) fspr_pool_userdata_setn(const void *data,
  1565. const char *key,
  1566. fspr_status_t (*cleanup)(void *),
  1567. fspr_pool_t *pool)
  1568. {
  1569. #if APR_POOL_DEBUG
  1570. fspr_pool_check_integrity(pool);
  1571. #endif /* APR_POOL_DEBUG */
  1572. if (pool->user_data == NULL)
  1573. pool->user_data = fspr_hash_make(pool);
  1574. fspr_hash_set(pool->user_data, key, APR_HASH_KEY_STRING, data);
  1575. if (cleanup)
  1576. fspr_pool_cleanup_register(pool, data, cleanup, cleanup);
  1577. return APR_SUCCESS;
  1578. }
  1579. APR_DECLARE(fspr_status_t) fspr_pool_userdata_get(void **data, const char *key,
  1580. fspr_pool_t *pool)
  1581. {
  1582. #if APR_POOL_DEBUG
  1583. fspr_pool_check_integrity(pool);
  1584. #endif /* APR_POOL_DEBUG */
  1585. if (pool->user_data == NULL) {
  1586. *data = NULL;
  1587. }
  1588. else {
  1589. *data = fspr_hash_get(pool->user_data, key, APR_HASH_KEY_STRING);
  1590. }
  1591. return APR_SUCCESS;
  1592. }
  1593. /*
  1594. * Cleanup
  1595. */
  1596. struct cleanup_t {
  1597. struct cleanup_t *next;
  1598. const void *data;
  1599. fspr_status_t (*plain_cleanup_fn)(void *data);
  1600. fspr_status_t (*child_cleanup_fn)(void *data);
  1601. };
  1602. APR_DECLARE(void) fspr_pool_cleanup_register(fspr_pool_t *p, const void *data,
  1603. fspr_status_t (*plain_cleanup_fn)(void *data),
  1604. fspr_status_t (*child_cleanup_fn)(void *data))
  1605. {
  1606. cleanup_t *c;
  1607. #if APR_POOL_DEBUG
  1608. fspr_pool_check_integrity(p);
  1609. #endif /* APR_POOL_DEBUG */
  1610. if (p != NULL) {
  1611. if (p->free_cleanups) {
  1612. /* reuse a cleanup structure */
  1613. c = p->free_cleanups;
  1614. p->free_cleanups = c->next;
  1615. } else {
  1616. c = fspr_palloc(p, sizeof(cleanup_t));
  1617. }
  1618. c->data = data;
  1619. c->plain_cleanup_fn = plain_cleanup_fn;
  1620. c->child_cleanup_fn = child_cleanup_fn;
  1621. c->next = p->cleanups;
  1622. p->cleanups = c;
  1623. }
  1624. }
  1625. APR_DECLARE(void) fspr_pool_cleanup_kill(fspr_pool_t *p, const void *data,
  1626. fspr_status_t (*cleanup_fn)(void *))
  1627. {
  1628. cleanup_t *c, **lastp;
  1629. #if APR_POOL_DEBUG
  1630. fspr_pool_check_integrity(p);
  1631. #endif /* APR_POOL_DEBUG */
  1632. if (p == NULL)
  1633. return;
  1634. c = p->cleanups;
  1635. lastp = &p->cleanups;
  1636. while (c) {
  1637. if (c->data == data && c->plain_cleanup_fn == cleanup_fn) {
  1638. *lastp = c->next;
  1639. /* move to freelist */
  1640. c->next = p->free_cleanups;
  1641. p->free_cleanups = c;
  1642. break;
  1643. }
  1644. lastp = &c->next;
  1645. if (c == c->next) {
  1646. c = NULL;
  1647. } else {
  1648. c = c->next;
  1649. }
  1650. }
  1651. }
  1652. APR_DECLARE(void) fspr_pool_child_cleanup_set(fspr_pool_t *p, const void *data,
  1653. fspr_status_t (*plain_cleanup_fn)(void *),
  1654. fspr_status_t (*child_cleanup_fn)(void *))
  1655. {
  1656. cleanup_t *c;
  1657. #if APR_POOL_DEBUG
  1658. fspr_pool_check_integrity(p);
  1659. #endif /* APR_POOL_DEBUG */
  1660. if (p == NULL)
  1661. return;
  1662. c = p->cleanups;
  1663. while (c) {
  1664. if (c->data == data && c->plain_cleanup_fn == plain_cleanup_fn) {
  1665. c->child_cleanup_fn = child_cleanup_fn;
  1666. break;
  1667. }
  1668. c = c->next;
  1669. }
  1670. }
  1671. APR_DECLARE(fspr_status_t) fspr_pool_cleanup_run(fspr_pool_t *p, void *data,
  1672. fspr_status_t (*cleanup_fn)(void *))
  1673. {
  1674. fspr_pool_cleanup_kill(p, data, cleanup_fn);
  1675. return (*cleanup_fn)(data);
  1676. }
  1677. static void run_cleanups(cleanup_t **cref)
  1678. {
  1679. cleanup_t *c = *cref;
  1680. while (c) {
  1681. *cref = c->next;
  1682. (*c->plain_cleanup_fn)((void *)c->data);
  1683. c = *cref;
  1684. }
  1685. }
  1686. static void run_child_cleanups(cleanup_t **cref)
  1687. {
  1688. cleanup_t *c = *cref;
  1689. while (c) {
  1690. *cref = c->next;
  1691. (*c->child_cleanup_fn)((void *)c->data);
  1692. c = *cref;
  1693. }
  1694. }
  1695. static void cleanup_pool_for_exec(fspr_pool_t *p)
  1696. {
  1697. run_child_cleanups(&p->cleanups);
  1698. for (p = p->child; p; p = p->sibling)
  1699. cleanup_pool_for_exec(p);
  1700. }
  1701. APR_DECLARE(void) fspr_pool_cleanup_for_exec(void)
  1702. {
  1703. #if !defined(WIN32) && !defined(OS2)
  1704. /*
  1705. * Don't need to do anything on NT or OS/2, because I
  1706. * am actually going to spawn the new process - not
  1707. * exec it. All handles that are not inheritable, will
  1708. * be automajically closed. The only problem is with
  1709. * file handles that are open, but there isn't much
  1710. * I can do about that (except if the child decides
  1711. * to go out and close them
  1712. */
  1713. cleanup_pool_for_exec(global_pool);
  1714. #endif /* !defined(WIN32) && !defined(OS2) */
  1715. }
  1716. APR_DECLARE_NONSTD(fspr_status_t) fspr_pool_cleanup_null(void *data)
  1717. {
  1718. /* do nothing cleanup routine */
  1719. return APR_SUCCESS;
  1720. }
  1721. /* Subprocesses don't use the generic cleanup interface because
  1722. * we don't want multiple subprocesses to result in multiple
  1723. * three-second pauses; the subprocesses have to be "freed" all
  1724. * at once. If other resources are introduced with the same property,
  1725. * we might want to fold support for that into the generic interface.
  1726. * For now, it's a special case.
  1727. */
  1728. APR_DECLARE(void) fspr_pool_note_subprocess(fspr_pool_t *pool, fspr_proc_t *proc,
  1729. fspr_kill_conditions_e how)
  1730. {
  1731. struct process_chain *pc = fspr_palloc(pool, sizeof(struct process_chain));
  1732. pc->proc = proc;
  1733. pc->kill_how = how;
  1734. pc->next = pool->subprocesses;
  1735. pool->subprocesses = pc;
  1736. }
  1737. static void free_proc_chain(struct process_chain *procs)
  1738. {
  1739. /* Dispose of the subprocesses we've spawned off in the course of
  1740. * whatever it was we're cleaning up now. This may involve killing
  1741. * some of them off...
  1742. */
  1743. struct process_chain *pc;
  1744. int need_timeout = 0;
  1745. fspr_time_t timeout_interval;
  1746. if (!procs)
  1747. return; /* No work. Whew! */
  1748. /* First, check to see if we need to do the SIGTERM, sleep, SIGKILL
  1749. * dance with any of the processes we're cleaning up. If we've got
  1750. * any kill-on-sight subprocesses, ditch them now as well, so they
  1751. * don't waste any more cycles doing whatever it is that they shouldn't
  1752. * be doing anymore.
  1753. */
  1754. #ifndef NEED_WAITPID
  1755. /* Pick up all defunct processes */
  1756. for (pc = procs; pc; pc = pc->next) {
  1757. if (fspr_proc_wait(pc->proc, NULL, NULL, APR_NOWAIT) != APR_CHILD_NOTDONE)
  1758. pc->kill_how = APR_KILL_NEVER;
  1759. }
  1760. #endif /* !defined(NEED_WAITPID) */
  1761. for (pc = procs; pc; pc = pc->next) {
  1762. #ifndef WIN32
  1763. if ((pc->kill_how == APR_KILL_AFTER_TIMEOUT)
  1764. || (pc->kill_how == APR_KILL_ONLY_ONCE)) {
  1765. /*
  1766. * Subprocess may be dead already. Only need the timeout if not.
  1767. * Note: fspr_proc_kill on Windows is TerminateProcess(), which is
  1768. * similar to a SIGKILL, so always give the process a timeout
  1769. * under Windows before killing it.
  1770. */
  1771. if (fspr_proc_kill(pc->proc, SIGTERM) == APR_SUCCESS)
  1772. need_timeout = 1;
  1773. }
  1774. else if (pc->kill_how == APR_KILL_ALWAYS) {
  1775. #else /* WIN32 knows only one fast, clean method of killing processes today */
  1776. if (pc->kill_how != APR_KILL_NEVER) {
  1777. need_timeout = 1;
  1778. pc->kill_how = APR_KILL_ALWAYS;
  1779. #endif
  1780. fspr_proc_kill(pc->proc, SIGKILL);
  1781. }
  1782. }
  1783. /* Sleep only if we have to. The sleep algorithm grows
  1784. * by a factor of two on each iteration. TIMEOUT_INTERVAL
  1785. * is equal to TIMEOUT_USECS / 64.
  1786. */
  1787. if (need_timeout) {
  1788. timeout_interval = TIMEOUT_INTERVAL;
  1789. fspr_sleep(timeout_interval);
  1790. do {
  1791. /* check the status of the subprocesses */
  1792. need_timeout = 0;
  1793. for (pc = procs; pc; pc = pc->next) {
  1794. if (pc->kill_how == APR_KILL_AFTER_TIMEOUT) {
  1795. if (fspr_proc_wait(pc->proc, NULL, NULL, APR_NOWAIT)
  1796. == APR_CHILD_NOTDONE)
  1797. need_timeout = 1; /* subprocess is still active */
  1798. else
  1799. pc->kill_how = APR_KILL_NEVER; /* subprocess has exited */
  1800. }
  1801. }
  1802. if (need_timeout) {
  1803. if (timeout_interval >= TIMEOUT_USECS) {
  1804. break;
  1805. }
  1806. fspr_sleep(timeout_interval);
  1807. timeout_interval *= 2;
  1808. }
  1809. } while (need_timeout);
  1810. }
  1811. /* OK, the scripts we just timed out for have had a chance to clean up
  1812. * --- now, just get rid of them, and also clean up the system accounting
  1813. * goop...
  1814. */
  1815. for (pc = procs; pc; pc = pc->next) {
  1816. if (pc->kill_how == APR_KILL_AFTER_TIMEOUT)
  1817. fspr_proc_kill(pc->proc, SIGKILL);
  1818. }
  1819. /* Now wait for all the signaled processes to die */
  1820. for (pc = procs; pc; pc = pc->next) {
  1821. if (pc->kill_how != APR_KILL_NEVER)
  1822. (void)fspr_proc_wait(pc->proc, NULL, NULL, APR_WAIT);
  1823. }
  1824. }
  1825. /*
  1826. * Pool creation/destruction stubs, for people who are running
  1827. * mixed release/debug enviroments.
  1828. */
  1829. #if !APR_POOL_DEBUG
  1830. APR_DECLARE(void *) fspr_palloc_debug(fspr_pool_t *pool, fspr_size_t size,
  1831. const char *file_line)
  1832. {
  1833. return fspr_palloc(pool, size);
  1834. }
  1835. APR_DECLARE(void *) fspr_pcalloc_debug(fspr_pool_t *pool, fspr_size_t size,
  1836. const char *file_line)
  1837. {
  1838. return fspr_pcalloc(pool, size);
  1839. }
  1840. APR_DECLARE(void) fspr_pool_clear_debug(fspr_pool_t *pool,
  1841. const char *file_line)
  1842. {
  1843. fspr_pool_clear(pool);
  1844. }
  1845. APR_DECLARE(void) fspr_pool_destroy_debug(fspr_pool_t *pool,
  1846. const char *file_line)
  1847. {
  1848. fspr_pool_destroy(pool);
  1849. }
  1850. APR_DECLARE(fspr_status_t) fspr_pool_create_ex_debug(fspr_pool_t **newpool,
  1851. fspr_pool_t *parent,
  1852. fspr_abortfunc_t abort_fn,
  1853. fspr_allocator_t *allocator,
  1854. const char *file_line)
  1855. {
  1856. return fspr_pool_create_ex(newpool, parent, abort_fn, allocator);
  1857. }
  1858. #else /* APR_POOL_DEBUG */
  1859. #undef fspr_palloc
  1860. APR_DECLARE(void *) fspr_palloc(fspr_pool_t *pool, fspr_size_t size);
  1861. APR_DECLARE(void *) fspr_palloc(fspr_pool_t *pool, fspr_size_t size)
  1862. {
  1863. return fspr_palloc_debug(pool, size, "undefined");
  1864. }
  1865. #undef fspr_pcalloc
  1866. APR_DECLARE(void *) fspr_pcalloc(fspr_pool_t *pool, fspr_size_t size);
  1867. APR_DECLARE(void *) fspr_pcalloc(fspr_pool_t *pool, fspr_size_t size)
  1868. {
  1869. return fspr_pcalloc_debug(pool, size, "undefined");
  1870. }
  1871. #undef fspr_pool_clear
  1872. APR_DECLARE(void) fspr_pool_clear(fspr_pool_t *pool);
  1873. APR_DECLARE(void) fspr_pool_clear(fspr_pool_t *pool)
  1874. {
  1875. fspr_pool_clear_debug(pool, "undefined");
  1876. }
  1877. #undef fspr_pool_destroy
  1878. APR_DECLARE(void) fspr_pool_destroy(fspr_pool_t *pool);
  1879. APR_DECLARE(void) fspr_pool_destroy(fspr_pool_t *pool)
  1880. {
  1881. fspr_pool_destroy_debug(pool, "undefined");
  1882. }
  1883. #undef fspr_pool_create_ex
  1884. APR_DECLARE(fspr_status_t) fspr_pool_create_ex(fspr_pool_t **newpool,
  1885. fspr_pool_t *parent,
  1886. fspr_abortfunc_t abort_fn,
  1887. fspr_allocator_t *allocator);
  1888. APR_DECLARE(fspr_status_t) fspr_pool_create_ex(fspr_pool_t **newpool,
  1889. fspr_pool_t *parent,
  1890. fspr_abortfunc_t abort_fn,
  1891. fspr_allocator_t *allocator)
  1892. {
  1893. return fspr_pool_create_ex_debug(newpool, parent,
  1894. abort_fn, allocator,
  1895. "undefined");
  1896. }
  1897. #endif /* APR_POOL_DEBUG */