1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
#include "apr_private.h"
20
#include "apr_atomic.h"
21
#include "apr_portable.h" /* for get_os_proc */
22
#include "apr_strings.h"
23
#include "apr_general.h"
24
#include "apr_pools.h"
25
#include "apr_allocator.h"
27
#include "apr_thread_mutex.h"
30
#define APR_WANT_MEMFUNC
35
#include <stdlib.h> /* for malloc, free and abort */
39
#include <unistd.h> /* for getpid */
47
#define MIN_ALLOC 8192
50
#define BOUNDARY_INDEX 12
51
#define BOUNDARY_SIZE (1 << BOUNDARY_INDEX)
54
* Timing constants for killing subprocesses
55
* There is a total 3-second delay between sending a SIGINT
56
* and sending of the final SIGKILL.
57
* TIMEOUT_INTERVAL should be set to TIMEOUT_USECS / 64
58
* for the exponetial timeout alogrithm.
60
#define TIMEOUT_USECS 3000000
61
#define TIMEOUT_INTERVAL 46875
67
struct apr_allocator_t {
68
apr_uint32_t max_index;
69
apr_uint32_t max_free_index;
70
apr_uint32_t current_free_index;
72
apr_thread_mutex_t *mutex;
73
#endif /* APR_HAS_THREADS */
75
apr_memnode_t *free[MAX_INDEX];
78
#define SIZEOF_ALLOCATOR_T APR_ALIGN_DEFAULT(sizeof(apr_allocator_t))
85
APR_DECLARE(apr_status_t) apr_allocator_create(apr_allocator_t **allocator)
87
apr_allocator_t *new_allocator;
91
if ((new_allocator = malloc(SIZEOF_ALLOCATOR_T)) == NULL)
94
memset(new_allocator, 0, SIZEOF_ALLOCATOR_T);
95
new_allocator->max_free_index = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
97
*allocator = new_allocator;
102
APR_DECLARE(void) apr_allocator_destroy(apr_allocator_t *allocator)
105
apr_memnode_t *node, **ref;
107
for (index = 0; index < MAX_INDEX; index++) {
108
ref = &allocator->free[index];
109
while ((node = *ref) != NULL) {
119
APR_DECLARE(void) apr_allocator_mutex_set(apr_allocator_t *allocator,
120
apr_thread_mutex_t *mutex)
122
allocator->mutex = mutex;
125
APR_DECLARE(apr_thread_mutex_t *) apr_allocator_mutex_get(
126
apr_allocator_t *allocator)
128
return allocator->mutex;
130
#endif /* APR_HAS_THREADS */
132
APR_DECLARE(void) apr_allocator_owner_set(apr_allocator_t *allocator,
135
allocator->owner = pool;
138
APR_DECLARE(apr_pool_t *) apr_allocator_owner_get(apr_allocator_t *allocator)
140
return allocator->owner;
143
APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator,
146
apr_uint32_t max_free_index;
147
apr_uint32_t size = (APR_UINT32_TRUNC_CAST)in_size;
150
apr_thread_mutex_t *mutex;
152
mutex = apr_allocator_mutex_get(allocator);
154
apr_thread_mutex_lock(mutex);
155
#endif /* APR_HAS_THREADS */
157
max_free_index = APR_ALIGN(size, BOUNDARY_SIZE) >> BOUNDARY_INDEX;
158
allocator->current_free_index += max_free_index;
159
allocator->current_free_index -= allocator->max_free_index;
160
allocator->max_free_index = max_free_index;
161
if (allocator->current_free_index > max_free_index)
162
allocator->current_free_index = max_free_index;
166
apr_thread_mutex_unlock(mutex);
171
apr_memnode_t *allocator_alloc(apr_allocator_t *allocator, apr_size_t size)
173
apr_memnode_t *node, **ref;
174
apr_uint32_t max_index;
177
/* Round up the block size to the next boundary, but always
178
* allocate at least a certain size (MIN_ALLOC).
180
size = APR_ALIGN(size + APR_MEMNODE_T_SIZE, BOUNDARY_SIZE);
181
if (size < MIN_ALLOC)
184
/* Find the index for this node size by
185
* dividing its size by the boundary size
187
index = (size >> BOUNDARY_INDEX) - 1;
189
if (index > APR_UINT32_MAX) {
193
/* First see if there are any nodes in the area we know
194
* our node will fit into.
196
if (index <= allocator->max_index) {
198
if (allocator->mutex)
199
apr_thread_mutex_lock(allocator->mutex);
200
#endif /* APR_HAS_THREADS */
202
/* Walk the free list to see if there are
203
* any nodes on it of the requested size
205
* NOTE: an optimization would be to check
206
* allocator->free[index] first and if no
207
* node is present, directly use
208
* allocator->free[max_index]. This seems
209
* like overkill though and could cause
212
max_index = allocator->max_index;
213
ref = &allocator->free[index];
215
while (*ref == NULL && i < max_index) {
220
if ((node = *ref) != NULL) {
221
/* If we have found a node and it doesn't have any
222
* nodes waiting in line behind it _and_ we are on
223
* the highest available index, find the new highest
226
if ((*ref = node->next) == NULL && i >= max_index) {
231
while (*ref == NULL && max_index > 0);
233
allocator->max_index = max_index;
236
allocator->current_free_index += node->index;
237
if (allocator->current_free_index > allocator->max_free_index)
238
allocator->current_free_index = allocator->max_free_index;
241
if (allocator->mutex)
242
apr_thread_mutex_unlock(allocator->mutex);
243
#endif /* APR_HAS_THREADS */
246
node->first_avail = (char *)node + APR_MEMNODE_T_SIZE;
252
if (allocator->mutex)
253
apr_thread_mutex_unlock(allocator->mutex);
254
#endif /* APR_HAS_THREADS */
257
/* If we found nothing, seek the sink (at index 0), if
260
else if (allocator->free[0]) {
262
if (allocator->mutex)
263
apr_thread_mutex_lock(allocator->mutex);
264
#endif /* APR_HAS_THREADS */
266
/* Walk the free list to see if there are
267
* any nodes on it of the requested size
269
ref = &allocator->free[0];
270
while ((node = *ref) != NULL && index > node->index)
276
allocator->current_free_index += node->index;
277
if (allocator->current_free_index > allocator->max_free_index)
278
allocator->current_free_index = allocator->max_free_index;
281
if (allocator->mutex)
282
apr_thread_mutex_unlock(allocator->mutex);
283
#endif /* APR_HAS_THREADS */
286
node->first_avail = (char *)node + APR_MEMNODE_T_SIZE;
292
if (allocator->mutex)
293
apr_thread_mutex_unlock(allocator->mutex);
294
#endif /* APR_HAS_THREADS */
297
/* If we haven't got a suitable node, malloc a new one
300
if ((node = malloc(size)) == NULL)
304
node->index = (APR_UINT32_TRUNC_CAST)index;
305
node->first_avail = (char *)node + APR_MEMNODE_T_SIZE;
306
node->endp = (char *)node + size;
312
void allocator_free(apr_allocator_t *allocator, apr_memnode_t *node)
314
apr_memnode_t *next, *freelist = NULL;
315
apr_uint32_t index, max_index;
316
apr_uint32_t max_free_index, current_free_index;
319
if (allocator->mutex)
320
apr_thread_mutex_lock(allocator->mutex);
321
#endif /* APR_HAS_THREADS */
323
max_index = allocator->max_index;
324
max_free_index = allocator->max_free_index;
325
current_free_index = allocator->current_free_index;
327
/* Walk the list of submitted nodes and free them one by one,
328
* shoving them in the right 'size' buckets as we go.
334
if (max_free_index != APR_ALLOCATOR_MAX_FREE_UNLIMITED
335
&& index > current_free_index) {
336
node->next = freelist;
339
else if (index < MAX_INDEX) {
340
/* Add the node to the appropiate 'size' bucket. Adjust
341
* the max_index when appropiate.
343
if ((node->next = allocator->free[index]) == NULL
344
&& index > max_index) {
347
allocator->free[index] = node;
348
current_free_index -= index;
351
/* This node is too large to keep in a specific size bucket,
352
* just add it to the sink (at index 0).
354
node->next = allocator->free[0];
355
allocator->free[0] = node;
356
current_free_index -= index;
358
} while ((node = next) != NULL);
360
allocator->max_index = max_index;
361
allocator->current_free_index = current_free_index;
364
if (allocator->mutex)
365
apr_thread_mutex_unlock(allocator->mutex);
366
#endif /* APR_HAS_THREADS */
368
while (freelist != NULL) {
370
freelist = node->next;
375
APR_DECLARE(apr_memnode_t *) apr_allocator_alloc(apr_allocator_t *allocator,
378
return allocator_alloc(allocator, size);
381
APR_DECLARE(void) apr_allocator_free(apr_allocator_t *allocator,
384
allocator_free(allocator, node);
393
#define APR_POOL_DEBUG_GENERAL 0x01
394
#define APR_POOL_DEBUG_VERBOSE 0x02
395
#define APR_POOL_DEBUG_LIFETIME 0x04
396
#define APR_POOL_DEBUG_OWNER 0x08
397
#define APR_POOL_DEBUG_VERBOSE_ALLOC 0x10
399
#define APR_POOL_DEBUG_VERBOSE_ALL (APR_POOL_DEBUG_VERBOSE \
400
| APR_POOL_DEBUG_VERBOSE_ALLOC)
407
typedef struct cleanup_t cleanup_t;
409
/** A list of processes */
410
struct process_chain {
411
/** The process ID */
413
apr_kill_conditions_e kill_how;
414
/** The next process in the list */
415
struct process_chain *next;
421
typedef struct debug_node_t debug_node_t;
423
struct debug_node_t {
430
#define SIZEOF_DEBUG_NODE_T APR_ALIGN_DEFAULT(sizeof(debug_node_t))
432
#endif /* APR_POOL_DEBUG */
434
/* The ref field in the apr_pool_t struct holds a
435
* pointer to the pointer referencing this pool.
436
* It is used for parent, child, sibling management.
437
* Look at apr_pool_create_ex() and apr_pool_destroy()
438
* to see how it is used.
446
cleanup_t *free_cleanups;
447
apr_allocator_t *allocator;
448
struct process_chain *subprocesses;
449
apr_abortfunc_t abort_fn;
450
apr_hash_t *user_data;
454
apr_memnode_t *active;
455
apr_memnode_t *self; /* The node containing the pool itself */
456
char *self_first_avail;
458
#else /* APR_POOL_DEBUG */
459
apr_pool_t *joined; /* the caller has guaranteed that this pool
460
* will survive as long as ->joined */
462
const char *file_line;
463
apr_uint32_t creation_flags;
464
unsigned int stat_alloc;
465
unsigned int stat_total_alloc;
466
unsigned int stat_clear;
468
apr_os_thread_t owner;
469
apr_thread_mutex_t *mutex;
470
#endif /* APR_HAS_THREADS */
471
#endif /* APR_POOL_DEBUG */
473
apr_os_proc_t owner_proc;
474
#endif /* defined(NETWARE) */
477
#define SIZEOF_POOL_T APR_ALIGN_DEFAULT(sizeof(apr_pool_t))
484
static apr_byte_t apr_pools_initialized = 0;
485
static apr_pool_t *global_pool = NULL;
488
static apr_allocator_t *global_allocator = NULL;
489
#endif /* !APR_POOL_DEBUG */
491
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
492
static apr_file_t *file_stderr = NULL;
493
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
499
static void run_cleanups(cleanup_t **c);
500
static void run_child_cleanups(cleanup_t **c);
501
static void free_proc_chain(struct process_chain *procs);
504
static void pool_destroy_debug(apr_pool_t *pool, const char *file_line);
512
APR_DECLARE(apr_status_t) apr_pool_initialize(void)
516
if (apr_pools_initialized++)
519
if ((rv = apr_allocator_create(&global_allocator)) != APR_SUCCESS) {
520
apr_pools_initialized = 0;
524
if ((rv = apr_pool_create_ex(&global_pool, NULL, NULL,
525
global_allocator)) != APR_SUCCESS) {
526
apr_allocator_destroy(global_allocator);
527
global_allocator = NULL;
528
apr_pools_initialized = 0;
532
apr_pool_tag(global_pool, "apr_global_pool");
534
/* This has to happen here because mutexes might be backed by
535
* atomics. It used to be snug and safe in apr_initialize().
537
if ((rv = apr_atomic_init(global_pool)) != APR_SUCCESS) {
543
apr_thread_mutex_t *mutex;
545
if ((rv = apr_thread_mutex_create(&mutex,
546
APR_THREAD_MUTEX_DEFAULT,
547
global_pool)) != APR_SUCCESS) {
551
apr_allocator_mutex_set(global_allocator, mutex);
553
#endif /* APR_HAS_THREADS */
555
apr_allocator_owner_set(global_allocator, global_pool);
560
APR_DECLARE(void) apr_pool_terminate(void)
562
if (!apr_pools_initialized)
565
if (--apr_pools_initialized)
568
apr_pool_destroy(global_pool); /* This will also destroy the mutex */
571
global_allocator = NULL;
575
/* Node list management helper macros; list_insert() inserts 'node'
577
#define list_insert(node, point) do { \
578
node->ref = point->ref; \
580
node->next = point; \
581
point->ref = &node->next; \
584
/* list_remove() removes 'node' from its list. */
585
#define list_remove(node) do { \
586
*node->ref = node->next; \
587
node->next->ref = node->ref; \
594
APR_DECLARE(void *) apr_palloc(apr_pool_t *pool, apr_size_t size)
596
apr_memnode_t *active, *node;
598
apr_size_t free_index;
600
size = APR_ALIGN_DEFAULT(size);
601
active = pool->active;
603
/* If the active node has enough bytes left, use it. */
604
if (size < (apr_size_t)(active->endp - active->first_avail)) {
605
mem = active->first_avail;
606
active->first_avail += size;
612
if (size < (apr_size_t)(node->endp - node->first_avail)) {
616
if ((node = allocator_alloc(pool->allocator, size)) == NULL) {
618
pool->abort_fn(APR_ENOMEM);
624
node->free_index = 0;
626
mem = node->first_avail;
627
node->first_avail += size;
629
list_insert(node, active);
633
free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
634
BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
636
active->free_index = (APR_UINT32_TRUNC_CAST)free_index;
638
if (free_index >= node->free_index)
644
while (free_index < node->free_index);
647
list_insert(active, node);
652
/* Provide an implementation of apr_pcalloc for backward compatibility
653
* with code built before apr_pcalloc was a macro
660
APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size);
661
APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size)
665
size = APR_ALIGN_DEFAULT(size);
666
if ((mem = apr_palloc(pool, size)) != NULL) {
667
memset(mem, 0, size);
675
* Pool creation/destruction
678
APR_DECLARE(void) apr_pool_clear(apr_pool_t *pool)
680
apr_memnode_t *active;
682
/* Destroy the subpools. The subpools will detach themselves from
683
* this pool thus this loop is safe and easy.
686
apr_pool_destroy(pool->child);
689
run_cleanups(&pool->cleanups);
690
pool->cleanups = NULL;
691
pool->free_cleanups = NULL;
693
/* Free subprocesses */
694
free_proc_chain(pool->subprocesses);
695
pool->subprocesses = NULL;
697
/* Clear the user data. */
698
pool->user_data = NULL;
700
/* Find the node attached to the pool structure, reset it, make
701
* it the active node and free the rest of the nodes.
703
active = pool->active = pool->self;
704
active->first_avail = pool->self_first_avail;
706
if (active->next == active)
710
allocator_free(pool->allocator, active->next);
711
active->next = active;
712
active->ref = &active->next;
715
APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool)
717
apr_memnode_t *active;
718
apr_allocator_t *allocator;
720
/* Destroy the subpools. The subpools will detach themselve from
721
* this pool thus this loop is safe and easy.
724
apr_pool_destroy(pool->child);
727
run_cleanups(&pool->cleanups);
729
/* Free subprocesses */
730
free_proc_chain(pool->subprocesses);
732
/* Remove the pool from the parents child list */
735
apr_thread_mutex_t *mutex;
737
if ((mutex = apr_allocator_mutex_get(pool->parent->allocator)) != NULL)
738
apr_thread_mutex_lock(mutex);
739
#endif /* APR_HAS_THREADS */
741
if ((*pool->ref = pool->sibling) != NULL)
742
pool->sibling->ref = pool->ref;
746
apr_thread_mutex_unlock(mutex);
747
#endif /* APR_HAS_THREADS */
750
/* Find the block attached to the pool structure. Save a copy of the
751
* allocator pointer, because the pool struct soon will be no more.
753
allocator = pool->allocator;
758
if (apr_allocator_owner_get(allocator) == pool) {
759
/* Make sure to remove the lock, since it is highly likely to
762
apr_allocator_mutex_set(allocator, NULL);
764
#endif /* APR_HAS_THREADS */
766
/* Free all the nodes in the pool (including the node holding the
767
* pool struct), by giving them back to the allocator.
769
allocator_free(allocator, active);
771
/* If this pool happens to be the owner of the allocator, free
772
* everything in the allocator (that includes the pool struct
773
* and the allocator). Don't worry about destroying the optional mutex
774
* in the allocator, it will have been destroyed by the cleanup function.
776
if (apr_allocator_owner_get(allocator) == pool) {
777
apr_allocator_destroy(allocator);
781
APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
783
apr_abortfunc_t abort_fn,
784
apr_allocator_t *allocator)
792
parent = global_pool;
794
if (!abort_fn && parent)
795
abort_fn = parent->abort_fn;
797
if (allocator == NULL)
798
allocator = parent->allocator;
800
if ((node = allocator_alloc(allocator,
801
MIN_ALLOC - APR_MEMNODE_T_SIZE)) == NULL) {
803
abort_fn(APR_ENOMEM);
809
node->ref = &node->next;
811
pool = (apr_pool_t *)node->first_avail;
812
node->first_avail = pool->self_first_avail = (char *)pool + SIZEOF_POOL_T;
814
pool->allocator = allocator;
815
pool->active = pool->self = node;
816
pool->abort_fn = abort_fn;
818
pool->cleanups = NULL;
819
pool->free_cleanups = NULL;
820
pool->subprocesses = NULL;
821
pool->user_data = NULL;
825
pool->owner_proc = (apr_os_proc_t)getnlmhandle();
826
#endif /* defined(NETWARE) */
828
if ((pool->parent = parent) != NULL) {
830
apr_thread_mutex_t *mutex;
832
if ((mutex = apr_allocator_mutex_get(parent->allocator)) != NULL)
833
apr_thread_mutex_lock(mutex);
834
#endif /* APR_HAS_THREADS */
836
if ((pool->sibling = parent->child) != NULL)
837
pool->sibling->ref = &pool->sibling;
839
parent->child = pool;
840
pool->ref = &parent->child;
844
apr_thread_mutex_unlock(mutex);
845
#endif /* APR_HAS_THREADS */
848
pool->sibling = NULL;
863
* apr_psprintf is implemented by writing directly into the current
864
* block of the pool, starting right at first_avail. If there's
865
* insufficient room, then a new block is allocated and the earlier
866
* output is copied over. The new block isn't linked into the pool
867
* until all the output is done.
869
* Note that this is completely safe because nothing else can
870
* allocate in this apr_pool_t while apr_psprintf is running. alarms are
871
* blocked, and the only thing outside of apr_pools.c that's invoked
872
* is apr_vformatter -- which was purposefully written to be
873
* self-contained with no callouts.
876
struct psprintf_data {
877
apr_vformatter_buff_t vbuff;
880
apr_byte_t got_a_new_node;
884
#define APR_PSPRINTF_MIN_STRINGSIZE 32
886
static int psprintf_flush(apr_vformatter_buff_t *vbuff)
888
struct psprintf_data *ps = (struct psprintf_data *)vbuff;
889
apr_memnode_t *node, *active;
890
apr_size_t cur_len, size;
893
apr_size_t free_index;
897
strp = ps->vbuff.curpos;
898
cur_len = strp - active->first_avail;
901
/* Make sure that we don't try to use a block that has less
902
* than APR_PSPRINTF_MIN_STRINGSIZE bytes left in it. This
903
* also catches the case where size == 0, which would result
904
* in reusing a block that can't even hold the NUL byte.
906
if (size < APR_PSPRINTF_MIN_STRINGSIZE)
907
size = APR_PSPRINTF_MIN_STRINGSIZE;
910
if (!ps->got_a_new_node
911
&& size < (apr_size_t)(node->endp - node->first_avail)) {
914
list_insert(node, active);
916
node->free_index = 0;
920
free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
921
BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
923
active->free_index = (APR_UINT32_TRUNC_CAST)free_index;
925
if (free_index < node->free_index) {
929
while (free_index < node->free_index);
932
list_insert(active, node);
938
if ((node = allocator_alloc(pool->allocator, size)) == NULL)
941
if (ps->got_a_new_node) {
942
active->next = ps->free;
946
ps->got_a_new_node = 1;
949
memcpy(node->first_avail, active->first_avail, cur_len);
952
ps->vbuff.curpos = node->first_avail + cur_len;
953
ps->vbuff.endpos = node->endp - 1; /* Save a byte for NUL terminator */
958
APR_DECLARE(char *) apr_pvsprintf(apr_pool_t *pool, const char *fmt, va_list ap)
960
struct psprintf_data ps;
963
apr_memnode_t *active, *node;
964
apr_size_t free_index;
966
ps.node = active = pool->active;
968
ps.vbuff.curpos = ps.node->first_avail;
970
/* Save a byte for the NUL terminator */
971
ps.vbuff.endpos = ps.node->endp - 1;
972
ps.got_a_new_node = 0;
975
/* Make sure that the first node passed to apr_vformatter has at least
976
* room to hold the NUL terminator.
978
if (ps.node->first_avail == ps.node->endp) {
979
if (psprintf_flush(&ps.vbuff) == -1) {
980
if (pool->abort_fn) {
981
pool->abort_fn(APR_ENOMEM);
988
if (apr_vformatter(psprintf_flush, &ps.vbuff, fmt, ap) == -1) {
990
pool->abort_fn(APR_ENOMEM);
995
strp = ps.vbuff.curpos;
998
size = strp - ps.node->first_avail;
999
size = APR_ALIGN_DEFAULT(size);
1000
strp = ps.node->first_avail;
1001
ps.node->first_avail += size;
1004
allocator_free(pool->allocator, ps.free);
1007
* Link the node in if it's a new one
1009
if (!ps.got_a_new_node)
1012
active = pool->active;
1015
node->free_index = 0;
1017
list_insert(node, active);
1019
pool->active = node;
1021
free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
1022
BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
1024
active->free_index = (APR_UINT32_TRUNC_CAST)free_index;
1025
node = active->next;
1027
if (free_index >= node->free_index)
1033
while (free_index < node->free_index);
1035
list_remove(active);
1036
list_insert(active, node);
1042
#else /* APR_POOL_DEBUG */
1044
* Debug helper functions
1049
* Walk the pool tree rooted at pool, depth first. When fn returns
1050
* anything other than 0, abort the traversal and return the value
1053
static int apr_pool_walk_tree(apr_pool_t *pool,
1054
int (*fn)(apr_pool_t *pool, void *data),
1060
rv = fn(pool, data);
1066
apr_thread_mutex_lock(pool->mutex);
1068
#endif /* APR_HAS_THREADS */
1070
child = pool->child;
1072
rv = apr_pool_walk_tree(child, fn, data);
1076
child = child->sibling;
1081
apr_thread_mutex_unlock(pool->mutex);
1083
#endif /* APR_HAS_THREADS */
1088
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1089
static void apr_pool_log_event(apr_pool_t *pool, const char *event,
1090
const char *file_line, int deref)
1094
apr_file_printf(file_stderr,
1099
#endif /* APR_HAS_THREADS */
1102
"(%10lu/%10lu/%10lu) "
1107
(unsigned long)getpid(),
1109
(unsigned long)apr_os_thread_current(),
1110
#endif /* APR_HAS_THREADS */
1112
(unsigned long)apr_pool_num_bytes(pool, 0),
1113
(unsigned long)apr_pool_num_bytes(pool, 1),
1114
(unsigned long)apr_pool_num_bytes(global_pool, 1),
1115
(unsigned int)pool, pool->tag,
1117
pool->stat_alloc, pool->stat_total_alloc, pool->stat_clear);
1120
apr_file_printf(file_stderr,
1125
#endif /* APR_HAS_THREADS */
1132
(unsigned long)getpid(),
1134
(unsigned long)apr_os_thread_current(),
1135
#endif /* APR_HAS_THREADS */
1142
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
1144
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME)
1145
static int pool_is_child_of(apr_pool_t *parent, void *data)
1147
apr_pool_t *pool = (apr_pool_t *)data;
1149
return (pool == parent);
1152
static int apr_pool_is_child_of(apr_pool_t *pool, apr_pool_t *parent)
1157
return apr_pool_walk_tree(parent, pool_is_child_of, pool);
1159
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME) */
1161
static void apr_pool_check_integrity(apr_pool_t *pool)
1163
/* Rule of thumb: use of the global pool is always
1164
* ok, since the only user is apr_pools.c. Unless
1165
* people have searched for the top level parent and
1166
* started to use that...
1168
if (pool == global_pool || global_pool == NULL)
1172
* This basically checks to see if the pool being used is still
1173
* a relative to the global pool. If not it was previously
1174
* destroyed, in which case we abort().
1176
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME)
1177
if (!apr_pool_is_child_of(pool, global_pool)) {
1178
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1179
apr_pool_log_event(pool, "LIFE",
1180
__FILE__ ":apr_pool_integrity check", 0);
1181
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
1184
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME) */
1186
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_OWNER)
1188
if (!apr_os_thread_equal(pool->owner, apr_os_thread_current())) {
1189
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1190
apr_pool_log_event(pool, "THREAD",
1191
__FILE__ ":apr_pool_integrity check", 0);
1192
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
1195
#endif /* APR_HAS_THREADS */
1196
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_OWNER) */
1201
* Initialization (debug)
1204
APR_DECLARE(apr_status_t) apr_pool_initialize(void)
1207
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1211
if (apr_pools_initialized++)
1214
/* Since the debug code works a bit differently then the
1215
* regular pools code, we ask for a lock here. The regular
1216
* pools code has got this lock embedded in the global
1217
* allocator, a concept unknown to debug mode.
1219
if ((rv = apr_pool_create_ex(&global_pool, NULL, NULL,
1220
NULL)) != APR_SUCCESS) {
1224
apr_pool_tag(global_pool, "APR global pool");
1226
apr_pools_initialized = 1;
1228
/* This has to happen here because mutexes might be backed by
1229
* atomics. It used to be snug and safe in apr_initialize().
1231
if ((rv = apr_atomic_init(global_pool)) != APR_SUCCESS) {
1235
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1236
rv = apr_env_get(&logpath, "APR_POOL_DEBUG_LOG", global_pool);
1238
if (rv == APR_SUCCESS) {
1239
apr_file_open(&file_stderr, logpath, APR_APPEND|APR_WRITE|APR_CREATE,
1240
APR_OS_DEFAULT, global_pool);
1243
apr_file_open_stderr(&file_stderr, global_pool);
1247
apr_file_printf(file_stderr,
1251
#endif /* APR_HAS_THREADS */
1252
"] ACTION (SIZE /POOL SIZE /TOTAL SIZE) "
1253
"POOL \"TAG\" <__FILE__:__LINE__> (ALLOCS/TOTAL ALLOCS/CLEARS)\n");
1255
apr_pool_log_event(global_pool, "GLOBAL", __FILE__ ":apr_pool_initialize", 0);
1257
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
1262
APR_DECLARE(void) apr_pool_terminate(void)
1264
if (!apr_pools_initialized)
1267
apr_pools_initialized = 0;
1269
apr_pool_destroy(global_pool); /* This will also destroy the mutex */
1272
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1274
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
1279
* Memory allocation (debug)
1282
static void *pool_alloc(apr_pool_t *pool, apr_size_t size)
1287
if ((mem = malloc(size)) == NULL) {
1289
pool->abort_fn(APR_ENOMEM);
1295
if (node == NULL || node->index == 64) {
1296
if ((node = malloc(SIZEOF_DEBUG_NODE_T)) == NULL) {
1298
pool->abort_fn(APR_ENOMEM);
1303
memset(node, 0, SIZEOF_DEBUG_NODE_T);
1305
node->next = pool->nodes;
1310
node->beginp[node->index] = mem;
1311
node->endp[node->index] = (char *)mem + size;
1315
pool->stat_total_alloc++;
1320
APR_DECLARE(void *) apr_palloc_debug(apr_pool_t *pool, apr_size_t size,
1321
const char *file_line)
1325
apr_pool_check_integrity(pool);
1327
mem = pool_alloc(pool, size);
1329
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC)
1330
apr_pool_log_event(pool, "PALLOC", file_line, 1);
1331
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC) */
1336
APR_DECLARE(void *) apr_pcalloc_debug(apr_pool_t *pool, apr_size_t size,
1337
const char *file_line)
1341
apr_pool_check_integrity(pool);
1343
mem = pool_alloc(pool, size);
1344
memset(mem, 0, size);
1346
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC)
1347
apr_pool_log_event(pool, "PCALLOC", file_line, 1);
1348
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC) */
1355
* Pool creation/destruction (debug)
1358
#define POOL_POISON_BYTE 'A'
1360
static void pool_clear_debug(apr_pool_t *pool, const char *file_line)
1365
/* Destroy the subpools. The subpools will detach themselves from
1366
* this pool thus this loop is safe and easy.
1369
pool_destroy_debug(pool->child, file_line);
1372
run_cleanups(&pool->cleanups);
1373
pool->free_cleanups = NULL;
1374
pool->cleanups = NULL;
1376
/* If new child pools showed up, this is a reason to raise a flag */
1380
/* Free subprocesses */
1381
free_proc_chain(pool->subprocesses);
1382
pool->subprocesses = NULL;
1384
/* Clear the user data. */
1385
pool->user_data = NULL;
1387
/* Free the blocks, scribbling over them first to help highlight
1388
* use-after-free issues. */
1389
while ((node = pool->nodes) != NULL) {
1390
pool->nodes = node->next;
1392
for (index = 0; index < node->index; index++) {
1393
memset(node->beginp[index], POOL_POISON_BYTE,
1394
node->endp[index] - node->beginp[index]);
1395
free(node->beginp[index]);
1398
memset(node, POOL_POISON_BYTE, SIZEOF_DEBUG_NODE_T);
1402
pool->stat_alloc = 0;
1406
APR_DECLARE(void) apr_pool_clear_debug(apr_pool_t *pool,
1407
const char *file_line)
1410
apr_thread_mutex_t *mutex = NULL;
1413
apr_pool_check_integrity(pool);
1415
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
1416
apr_pool_log_event(pool, "CLEAR", file_line, 1);
1417
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
1420
if (pool->parent != NULL)
1421
mutex = pool->parent->mutex;
1423
/* Lock the parent mutex before clearing so that if we have our
1424
* own mutex it won't be accessed by apr_pool_walk_tree after
1425
* it has been destroyed.
1427
if (mutex != NULL && mutex != pool->mutex) {
1428
apr_thread_mutex_lock(mutex);
1432
pool_clear_debug(pool, file_line);
1435
/* If we had our own mutex, it will have been destroyed by
1436
* the registered cleanups. Recreate the mutex. Unlock
1437
* the mutex we obtained above.
1439
if (mutex != pool->mutex) {
1440
(void)apr_thread_mutex_create(&pool->mutex,
1441
APR_THREAD_MUTEX_NESTED, pool);
1444
(void)apr_thread_mutex_unlock(mutex);
1446
#endif /* APR_HAS_THREADS */
1449
static void pool_destroy_debug(apr_pool_t *pool, const char *file_line)
1451
apr_pool_check_integrity(pool);
1453
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
1454
apr_pool_log_event(pool, "DESTROY", file_line, 1);
1455
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
1457
pool_clear_debug(pool, file_line);
1459
/* Remove the pool from the parents child list */
1462
apr_thread_mutex_t *mutex;
1464
if ((mutex = pool->parent->mutex) != NULL)
1465
apr_thread_mutex_lock(mutex);
1466
#endif /* APR_HAS_THREADS */
1468
if ((*pool->ref = pool->sibling) != NULL)
1469
pool->sibling->ref = pool->ref;
1473
apr_thread_mutex_unlock(mutex);
1474
#endif /* APR_HAS_THREADS */
1477
if (pool->allocator != NULL
1478
&& apr_allocator_owner_get(pool->allocator) == pool) {
1479
apr_allocator_destroy(pool->allocator);
1482
/* Free the pool itself */
1486
APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *pool,
1487
const char *file_line)
1490
/* Joined pools must not be explicitly destroyed; the caller
1491
* has broken the guarantee. */
1492
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1493
apr_pool_log_event(pool, "LIFE",
1494
__FILE__ ":apr_pool_destroy abort on joined", 0);
1495
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
1499
pool_destroy_debug(pool, file_line);
1502
APR_DECLARE(apr_status_t) apr_pool_create_ex_debug(apr_pool_t **newpool,
1504
apr_abortfunc_t abort_fn,
1505
apr_allocator_t *allocator,
1506
const char *file_line)
1513
parent = global_pool;
1516
apr_pool_check_integrity(parent);
1519
allocator = parent->allocator;
1522
if (!abort_fn && parent)
1523
abort_fn = parent->abort_fn;
1525
if ((pool = malloc(SIZEOF_POOL_T)) == NULL) {
1527
abort_fn(APR_ENOMEM);
1532
memset(pool, 0, SIZEOF_POOL_T);
1534
pool->allocator = allocator;
1535
pool->abort_fn = abort_fn;
1536
pool->tag = file_line;
1537
pool->file_line = file_line;
1539
if ((pool->parent = parent) != NULL) {
1542
apr_thread_mutex_lock(parent->mutex);
1543
#endif /* APR_HAS_THREADS */
1544
if ((pool->sibling = parent->child) != NULL)
1545
pool->sibling->ref = &pool->sibling;
1547
parent->child = pool;
1548
pool->ref = &parent->child;
1552
apr_thread_mutex_unlock(parent->mutex);
1553
#endif /* APR_HAS_THREADS */
1556
pool->sibling = NULL;
1561
pool->owner = apr_os_thread_current();
1562
#endif /* APR_HAS_THREADS */
1564
pool->owner_proc = (apr_os_proc_t)getnlmhandle();
1565
#endif /* defined(NETWARE) */
1568
if (parent == NULL || parent->allocator != allocator) {
1572
/* No matter what the creation flags say, always create
1573
* a lock. Without it integrity_check and apr_pool_num_bytes
1574
* blow up (because they traverse pools child lists that
1575
* possibly belong to another thread, in combination with
1576
* the pool having no lock). However, this might actually
1577
* hide problems like creating a child pool of a pool
1578
* belonging to another thread.
1580
if ((rv = apr_thread_mutex_create(&pool->mutex,
1581
APR_THREAD_MUTEX_NESTED, pool)) != APR_SUCCESS) {
1585
#endif /* APR_HAS_THREADS */
1590
pool->mutex = parent->mutex;
1591
#endif /* APR_HAS_THREADS */
1596
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
1597
apr_pool_log_event(pool, "CREATE", file_line, 1);
1598
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
1605
* "Print" functions (debug)
1608
struct psprintf_data {
1609
apr_vformatter_buff_t vbuff;
1614
static int psprintf_flush(apr_vformatter_buff_t *vbuff)
1616
struct psprintf_data *ps = (struct psprintf_data *)vbuff;
1619
size = ps->vbuff.curpos - ps->mem;
1622
if ((ps->mem = realloc(ps->mem, ps->size)) == NULL)
1625
ps->vbuff.curpos = ps->mem + size;
1626
ps->vbuff.endpos = ps->mem + ps->size - 1;
1631
APR_DECLARE(char *) apr_pvsprintf(apr_pool_t *pool, const char *fmt, va_list ap)
1633
struct psprintf_data ps;
1636
apr_pool_check_integrity(pool);
1639
ps.mem = malloc(ps.size);
1640
ps.vbuff.curpos = ps.mem;
1642
/* Save a byte for the NUL terminator */
1643
ps.vbuff.endpos = ps.mem + ps.size - 1;
1645
if (apr_vformatter(psprintf_flush, &ps.vbuff, fmt, ap) == -1) {
1647
pool->abort_fn(APR_ENOMEM);
1652
*ps.vbuff.curpos++ = '\0';
1658
if (node == NULL || node->index == 64) {
1659
if ((node = malloc(SIZEOF_DEBUG_NODE_T)) == NULL) {
1661
pool->abort_fn(APR_ENOMEM);
1666
node->next = pool->nodes;
1671
node->beginp[node->index] = ps.mem;
1672
node->endp[node->index] = ps.mem + ps.size;
1683
APR_DECLARE(void) apr_pool_join(apr_pool_t *p, apr_pool_t *sub)
1686
if (sub->parent != p) {
1693
static int pool_find(apr_pool_t *pool, void *data)
1695
void **pmem = (void **)data;
1702
for (index = 0; index < node->index; index++) {
1703
if (node->beginp[index] <= *pmem
1704
&& node->endp[index] > *pmem) {
1716
APR_DECLARE(apr_pool_t *) apr_pool_find(const void *mem)
1718
void *pool = (void *)mem;
1720
if (apr_pool_walk_tree(global_pool, pool_find, &pool))
1726
static int pool_num_bytes(apr_pool_t *pool, void *data)
1728
apr_size_t *psize = (apr_size_t *)data;
1735
for (index = 0; index < node->index; index++) {
1736
*psize += (char *)node->endp[index] - (char *)node->beginp[index];
1745
APR_DECLARE(apr_size_t) apr_pool_num_bytes(apr_pool_t *pool, int recurse)
1747
apr_size_t size = 0;
1750
pool_num_bytes(pool, &size);
1755
apr_pool_walk_tree(pool, pool_num_bytes, &size);
1760
APR_DECLARE(void) apr_pool_lock(apr_pool_t *pool, int flag)
1764
#endif /* !APR_POOL_DEBUG */
1767
void netware_pool_proc_cleanup ()
1769
apr_pool_t *pool = global_pool->child;
1770
apr_os_proc_t owner_proc = (apr_os_proc_t)getnlmhandle();
1773
if (pool->owner_proc == owner_proc) {
1774
apr_pool_destroy (pool);
1775
pool = global_pool->child;
1778
pool = pool->sibling;
1783
#endif /* defined(NETWARE) */
1787
* "Print" functions (common)
1790
APR_DECLARE_NONSTD(char *) apr_psprintf(apr_pool_t *p, const char *fmt, ...)
1796
res = apr_pvsprintf(p, fmt, ap);
1805
APR_DECLARE(void) apr_pool_abort_set(apr_abortfunc_t abort_fn,
1808
pool->abort_fn = abort_fn;
1811
APR_DECLARE(apr_abortfunc_t) apr_pool_abort_get(apr_pool_t *pool)
1813
return pool->abort_fn;
1816
APR_DECLARE(apr_pool_t *) apr_pool_parent_get(apr_pool_t *pool)
1819
/* On NetWare, don't return the global_pool, return the application pool
1820
as the top most pool */
1821
if (pool->parent == global_pool)
1825
return pool->parent;
1828
APR_DECLARE(apr_allocator_t *) apr_pool_allocator_get(apr_pool_t *pool)
1830
return pool->allocator;
1833
/* return TRUE if a is an ancestor of b
1834
* NULL is considered an ancestor of all pools
1836
APR_DECLARE(int) apr_pool_is_ancestor(apr_pool_t *a, apr_pool_t *b)
1842
/* Find the pool with the longest lifetime guaranteed by the
1859
APR_DECLARE(void) apr_pool_tag(apr_pool_t *pool, const char *tag)
1866
* User data management
1869
APR_DECLARE(apr_status_t) apr_pool_userdata_set(const void *data, const char *key,
1870
apr_status_t (*cleanup) (void *),
1874
apr_pool_check_integrity(pool);
1875
#endif /* APR_POOL_DEBUG */
1877
if (pool->user_data == NULL)
1878
pool->user_data = apr_hash_make(pool);
1880
if (apr_hash_get(pool->user_data, key, APR_HASH_KEY_STRING) == NULL) {
1881
char *new_key = apr_pstrdup(pool, key);
1882
apr_hash_set(pool->user_data, new_key, APR_HASH_KEY_STRING, data);
1885
apr_hash_set(pool->user_data, key, APR_HASH_KEY_STRING, data);
1889
apr_pool_cleanup_register(pool, data, cleanup, cleanup);
1894
APR_DECLARE(apr_status_t) apr_pool_userdata_setn(const void *data,
1896
apr_status_t (*cleanup)(void *),
1900
apr_pool_check_integrity(pool);
1901
#endif /* APR_POOL_DEBUG */
1903
if (pool->user_data == NULL)
1904
pool->user_data = apr_hash_make(pool);
1906
apr_hash_set(pool->user_data, key, APR_HASH_KEY_STRING, data);
1909
apr_pool_cleanup_register(pool, data, cleanup, cleanup);
1914
APR_DECLARE(apr_status_t) apr_pool_userdata_get(void **data, const char *key,
1918
apr_pool_check_integrity(pool);
1919
#endif /* APR_POOL_DEBUG */
1921
if (pool->user_data == NULL) {
1925
*data = apr_hash_get(pool->user_data, key, APR_HASH_KEY_STRING);
1937
struct cleanup_t *next;
1939
apr_status_t (*plain_cleanup_fn)(void *data);
1940
apr_status_t (*child_cleanup_fn)(void *data);
1943
APR_DECLARE(void) apr_pool_cleanup_register(apr_pool_t *p, const void *data,
1944
apr_status_t (*plain_cleanup_fn)(void *data),
1945
apr_status_t (*child_cleanup_fn)(void *data))
1950
apr_pool_check_integrity(p);
1951
#endif /* APR_POOL_DEBUG */
1954
if (p->free_cleanups) {
1955
/* reuse a cleanup structure */
1956
c = p->free_cleanups;
1957
p->free_cleanups = c->next;
1959
c = apr_palloc(p, sizeof(cleanup_t));
1962
c->plain_cleanup_fn = plain_cleanup_fn;
1963
c->child_cleanup_fn = child_cleanup_fn;
1964
c->next = p->cleanups;
1969
APR_DECLARE(void) apr_pool_cleanup_kill(apr_pool_t *p, const void *data,
1970
apr_status_t (*cleanup_fn)(void *))
1972
cleanup_t *c, **lastp;
1975
apr_pool_check_integrity(p);
1976
#endif /* APR_POOL_DEBUG */
1982
lastp = &p->cleanups;
1984
if (c->data == data && c->plain_cleanup_fn == cleanup_fn) {
1986
/* move to freelist */
1987
c->next = p->free_cleanups;
1988
p->free_cleanups = c;
1997
APR_DECLARE(void) apr_pool_child_cleanup_set(apr_pool_t *p, const void *data,
1998
apr_status_t (*plain_cleanup_fn)(void *),
1999
apr_status_t (*child_cleanup_fn)(void *))
2004
apr_pool_check_integrity(p);
2005
#endif /* APR_POOL_DEBUG */
2012
if (c->data == data && c->plain_cleanup_fn == plain_cleanup_fn) {
2013
c->child_cleanup_fn = child_cleanup_fn;
2021
APR_DECLARE(apr_status_t) apr_pool_cleanup_run(apr_pool_t *p, void *data,
2022
apr_status_t (*cleanup_fn)(void *))
2024
apr_pool_cleanup_kill(p, data, cleanup_fn);
2025
return (*cleanup_fn)(data);
2028
static void run_cleanups(cleanup_t **cref)
2030
cleanup_t *c = *cref;
2034
(*c->plain_cleanup_fn)((void *)c->data);
2039
static void run_child_cleanups(cleanup_t **cref)
2041
cleanup_t *c = *cref;
2045
(*c->child_cleanup_fn)((void *)c->data);
2050
static void cleanup_pool_for_exec(apr_pool_t *p)
2052
run_child_cleanups(&p->cleanups);
2054
for (p = p->child; p; p = p->sibling)
2055
cleanup_pool_for_exec(p);
2058
APR_DECLARE(void) apr_pool_cleanup_for_exec(void)
2060
#if !defined(WIN32) && !defined(OS2)
2062
* Don't need to do anything on NT or OS/2, because I
2063
* am actually going to spawn the new process - not
2064
* exec it. All handles that are not inheritable, will
2065
* be automajically closed. The only problem is with
2066
* file handles that are open, but there isn't much
2067
* I can do about that (except if the child decides
2068
* to go out and close them
2070
cleanup_pool_for_exec(global_pool);
2071
#endif /* !defined(WIN32) && !defined(OS2) */
2074
APR_DECLARE_NONSTD(apr_status_t) apr_pool_cleanup_null(void *data)
2076
/* do nothing cleanup routine */
2080
/* Subprocesses don't use the generic cleanup interface because
2081
* we don't want multiple subprocesses to result in multiple
2082
* three-second pauses; the subprocesses have to be "freed" all
2083
* at once. If other resources are introduced with the same property,
2084
* we might want to fold support for that into the generic interface.
2085
* For now, it's a special case.
2087
APR_DECLARE(void) apr_pool_note_subprocess(apr_pool_t *pool, apr_proc_t *proc,
2088
apr_kill_conditions_e how)
2090
struct process_chain *pc = apr_palloc(pool, sizeof(struct process_chain));
2094
pc->next = pool->subprocesses;
2095
pool->subprocesses = pc;
2098
static void free_proc_chain(struct process_chain *procs)
2100
/* Dispose of the subprocesses we've spawned off in the course of
2101
* whatever it was we're cleaning up now. This may involve killing
2102
* some of them off...
2104
struct process_chain *pc;
2105
int need_timeout = 0;
2106
apr_time_t timeout_interval;
2109
return; /* No work. Whew! */
2111
/* First, check to see if we need to do the SIGTERM, sleep, SIGKILL
2112
* dance with any of the processes we're cleaning up. If we've got
2113
* any kill-on-sight subprocesses, ditch them now as well, so they
2114
* don't waste any more cycles doing whatever it is that they shouldn't
2118
#ifndef NEED_WAITPID
2119
/* Pick up all defunct processes */
2120
for (pc = procs; pc; pc = pc->next) {
2121
if (apr_proc_wait(pc->proc, NULL, NULL, APR_NOWAIT) != APR_CHILD_NOTDONE)
2122
pc->kill_how = APR_KILL_NEVER;
2124
#endif /* !defined(NEED_WAITPID) */
2126
for (pc = procs; pc; pc = pc->next) {
2128
if ((pc->kill_how == APR_KILL_AFTER_TIMEOUT)
2129
|| (pc->kill_how == APR_KILL_ONLY_ONCE)) {
2131
* Subprocess may be dead already. Only need the timeout if not.
2132
* Note: apr_proc_kill on Windows is TerminateProcess(), which is
2133
* similar to a SIGKILL, so always give the process a timeout
2134
* under Windows before killing it.
2136
if (apr_proc_kill(pc->proc, SIGTERM) == APR_SUCCESS)
2139
else if (pc->kill_how == APR_KILL_ALWAYS) {
2140
#else /* WIN32 knows only one fast, clean method of killing processes today */
2141
if (pc->kill_how != APR_KILL_NEVER) {
2143
pc->kill_how = APR_KILL_ALWAYS;
2145
apr_proc_kill(pc->proc, SIGKILL);
2149
/* Sleep only if we have to. The sleep algorithm grows
2150
* by a factor of two on each iteration. TIMEOUT_INTERVAL
2151
* is equal to TIMEOUT_USECS / 64.
2154
timeout_interval = TIMEOUT_INTERVAL;
2155
apr_sleep(timeout_interval);
2158
/* check the status of the subprocesses */
2160
for (pc = procs; pc; pc = pc->next) {
2161
if (pc->kill_how == APR_KILL_AFTER_TIMEOUT) {
2162
if (apr_proc_wait(pc->proc, NULL, NULL, APR_NOWAIT)
2163
== APR_CHILD_NOTDONE)
2164
need_timeout = 1; /* subprocess is still active */
2166
pc->kill_how = APR_KILL_NEVER; /* subprocess has exited */
2170
if (timeout_interval >= TIMEOUT_USECS) {
2173
apr_sleep(timeout_interval);
2174
timeout_interval *= 2;
2176
} while (need_timeout);
2179
/* OK, the scripts we just timed out for have had a chance to clean up
2180
* --- now, just get rid of them, and also clean up the system accounting
2183
for (pc = procs; pc; pc = pc->next) {
2184
if (pc->kill_how == APR_KILL_AFTER_TIMEOUT)
2185
apr_proc_kill(pc->proc, SIGKILL);
2188
/* Now wait for all the signaled processes to die */
2189
for (pc = procs; pc; pc = pc->next) {
2190
if (pc->kill_how != APR_KILL_NEVER)
2191
(void)apr_proc_wait(pc->proc, NULL, NULL, APR_WAIT);
2197
* Pool creation/destruction stubs, for people who are running
2198
* mixed release/debug enviroments.
2202
APR_DECLARE(void *) apr_palloc_debug(apr_pool_t *pool, apr_size_t size,
2203
const char *file_line)
2205
return apr_palloc(pool, size);
2208
APR_DECLARE(void *) apr_pcalloc_debug(apr_pool_t *pool, apr_size_t size,
2209
const char *file_line)
2211
return apr_pcalloc(pool, size);
2214
APR_DECLARE(void) apr_pool_clear_debug(apr_pool_t *pool,
2215
const char *file_line)
2217
apr_pool_clear(pool);
2220
APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *pool,
2221
const char *file_line)
2223
apr_pool_destroy(pool);
2226
APR_DECLARE(apr_status_t) apr_pool_create_ex_debug(apr_pool_t **newpool,
2228
apr_abortfunc_t abort_fn,
2229
apr_allocator_t *allocator,
2230
const char *file_line)
2232
return apr_pool_create_ex(newpool, parent, abort_fn, allocator);
2235
#else /* APR_POOL_DEBUG */
2238
APR_DECLARE(void *) apr_palloc(apr_pool_t *pool, apr_size_t size);
2240
APR_DECLARE(void *) apr_palloc(apr_pool_t *pool, apr_size_t size)
2242
return apr_palloc_debug(pool, size, "undefined");
2246
APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size);
2248
APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size)
2250
return apr_pcalloc_debug(pool, size, "undefined");
2253
#undef apr_pool_clear
2254
APR_DECLARE(void) apr_pool_clear(apr_pool_t *pool);
2256
APR_DECLARE(void) apr_pool_clear(apr_pool_t *pool)
2258
apr_pool_clear_debug(pool, "undefined");
2261
#undef apr_pool_destroy
2262
APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool);
2264
APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool)
2266
apr_pool_destroy_debug(pool, "undefined");
2269
#undef apr_pool_create_ex
2270
APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
2272
apr_abortfunc_t abort_fn,
2273
apr_allocator_t *allocator);
2275
APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
2277
apr_abortfunc_t abort_fn,
2278
apr_allocator_t *allocator)
2280
return apr_pool_create_ex_debug(newpool, parent,
2281
abort_fn, allocator,
2285
#endif /* APR_POOL_DEBUG */