2 /* ====================================================================
3 * Copyright (c) 1995-1997 The Apache Group. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the Apache Group
20 * for use in the Apache HTTP server project (http://www.apache.org/)."
22 * 4. The names "Apache Server" and "Apache Group" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission.
26 * 5. Redistributions of any form whatsoever must retain the following
28 * "This product includes software developed by the Apache Group
29 * for use in the Apache HTTP server project (http://www.apache.org/)."
31 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
32 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
35 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42 * OF THE POSSIBILITY OF SUCH DAMAGE.
43 * ====================================================================
45 * This software consists of voluntary contributions made by many
46 * individuals on behalf of the Apache Group and was originally based
47 * on public domain software written at the National Center for
48 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
49 * For more information on the Apache Group and the Apache HTTP server
50 * project, please see <http://www.apache.org/>.
55 * Resource allocation routines...
57 * designed so that we don't have to keep track of EVERYTHING so that
58 * it can be explicitly freed later (a fundamentally unsound strategy ---
59 * particularly in the presence of die()).
61 * Instead, we maintain pools, and allocate items (both memory and I/O
62 * handlers) from the pools --- currently there are two, one for per
63 * transaction info, and one for config info. When a transaction is over,
64 * we can delete everything in the per-transaction pool without fear, and
65 * without thinking too hard about it either.
70 /* Arenas for configuration info and transaction info
71 * --- actual layout of the pool structure is private to
75 typedef struct pool pool
;
77 extern pool
*permanent_pool
;
78 void init_alloc(); /* Set up everything */
79 pool
*make_sub_pool(pool
*); /* All pools are subpools of permanent_pool */
80 void destroy_pool(pool
*);
82 /* Clearing out EVERYTHING in an pool... destroys any sub-pools */
84 void clear_pool(struct pool
*);
86 /* Preparing for exec() --- close files, etc., but *don't* flush I/O
87 * buffers, *don't* wait for subprocesses, and *don't* free any memory.
90 void cleanup_for_exec();
92 /* routines to allocate memory from an pool... */
94 void *palloc(struct pool
*, int nbytes
);
95 void *pcalloc(struct pool
*, int nbytes
);
96 extern char *pstrdup(struct pool
*, const char *s
);
97 extern char *pstrndup(struct pool
*, const char *s
, int n
);
98 char *pstrcat(struct pool
*, ...); /* all '...' must be char* */
100 /* array and alist management... keeping lists of things.
101 * Common enough to want common support code ...
112 array_header
*make_array(pool
* p
, int nelts
, int elt_size
);
113 void *push_array(array_header
*);
114 void array_cat(array_header
* dst
, const array_header
* src
);
115 array_header
*append_arrays(pool
*, const array_header
*,
116 const array_header
*);
118 /* copy_array copies the *entire* array. copy_array_hdr just copies
119 * the header, and arranges for the elements to be copied if (and only
120 * if) the code subsequently does a push or arraycat.
123 array_header
*copy_array(pool
* p
, const array_header
* src
);
124 array_header
*copy_array_hdr(pool
* p
, const array_header
* src
);
127 /* Tables. Implemented alist style, for now, though we try to keep
128 * it so that imposing a hash table structure on top in the future
129 * wouldn't be *too* hard...
131 * Note that key comparisons for these are case-insensitive, largely
132 * because that's what's appropriate and convenient everywhere they're
133 * currently being used...
136 typedef array_header table
;
139 char *key
; /* maybe NULL in future;
140 * check when iterating thru table_elts
145 table
*make_table(pool
* p
, int nelts
);
146 table
*copy_table(pool
* p
, const table
*);
147 void clear_table(table
*);
148 char *table_get(const table
*, const char *);
149 void table_set(table
*, const char *name
, const char *val
);
150 void table_merge(table
*, const char *name
, const char *more_val
);
151 void table_unset(table
*, const char *key
);
152 void table_add(table
*, const char *name
, const char *val
);
153 void table_do(int (*comp
) (void *, const char *, const char *), void *rec
,
154 const table
* t
, ...);
156 table
*overlay_tables(pool
* p
, const table
* overlay
, const table
* base
);
158 array_header
*table_elts(table
*);
160 #define is_empty_table(t) (((t) == NULL)||((t)->nelts == 0))
162 /* routines to remember allocation of other sorts of things...
163 * generic interface first. Note that we want to have two separate
164 * cleanup functions in the general case, one for exec() preparation,
165 * to keep CGI scripts and the like from inheriting access to things
166 * they shouldn't be able to touch, and one for actually cleaning up,
167 * when the actual server process wants to get rid of the thing,
170 * kill_cleanup disarms a cleanup, presumably because the resource in
171 * question has been closed, freed, or whatever, and it's scarce
172 * enough to want to reclaim (e.g., descriptors). It arranges for the
173 * resource not to be cleaned up a second time (it might have been
174 * reallocated). run_cleanup does the same, but runs it first.
176 * Cleanups are identified for purposes of finding & running them off by the
177 * plain_cleanup and data, which should presumably be unique.
179 * NB any code which invokes register_cleanup or kill_cleanup directly
180 * is a critical section which should be guarded by block_alarms() and
181 * unblock_alarms() below...
184 void register_cleanup(pool
* p
, void *data
, void (*plain_cleanup
) (void *),
185 void (*child_cleanup
) (void *));
187 void kill_cleanup(pool
* p
, void *data
, void (*plain_cleanup
) (void *));
188 void run_cleanup(pool
* p
, void *data
, void (*cleanup
) (void *));
190 /* The time between when a resource is actually allocated, and when it
191 * its cleanup is registered is a critical section, during which the
192 * resource could leak if we got interrupted or timed out. So, anything
193 * which registers cleanups should bracket resource allocation and the
194 * cleanup registry with these. (This is done internally by run_cleanup).
196 * NB they are actually implemented in http_main.c, since they are bound
197 * up with timeout handling in general...
200 extern void block_alarms();
201 extern void unblock_alarms();
203 /* Common cases which want utility support..
204 * the note_cleanups_for_foo routines are for
207 FILE *pfopen(struct pool
*, const char *name
, const char *fmode
);
208 FILE *pfdopen(struct pool
*, int fd
, const char *fmode
);
209 int popenf(struct pool
*, const char *name
, int flg
, int mode
);
211 void note_cleanups_for_file(pool
*, FILE *);
212 void note_cleanups_for_fd(pool
*, int);
213 void kill_cleanups_for_fd(pool
* p
, int fd
);
215 regex_t
*pregcomp(pool
* p
, const char *pattern
, int cflags
);
216 void pregfree(pool
* p
, regex_t
* reg
);
218 /* routines to note closes... file descriptors are constrained enough
219 * on some systems that we want to support this.
222 int pfclose(struct pool
*, FILE *);
223 int pclosef(struct pool
*, int fd
);
225 /* ... even child processes (which we may want to wait for,
226 * or to kill outright, on unexpected termination).
228 * spawn_child is a utility routine which handles an awful lot of
229 * the rigamarole associated with spawning a child --- it arranges
230 * for pipes to the child's stdin and stdout, if desired (if not,
231 * set the associated args to NULL). It takes as args a function
232 * to call in the child, and an argument to be passed to the function.
235 enum kill_conditions
{ kill_never
, kill_always
, kill_after_timeout
,
239 int spawn_child_err(pool
*, void (*)(void *), void *, enum kill_conditions
,
240 FILE ** pipe_in
, FILE ** pipe_out
, FILE ** pipe_err
);
241 #define spawn_child(p,f,v,k,in,out) spawn_child_err(p,f,v,k,in,out,NULL)
243 /* magic numbers --- min free bytes to consider a free pool block useable,
244 * and the min amount to allocate if we have to go to malloc() */
246 #define BLOCK_MINFREE 4096
247 #define BLOCK_MINALLOC 8192
249 /* Finally, some accounting */
251 long bytes_in_pool(pool
* p
);
252 long bytes_in_free_blocks();