/[Apache-SVN]/httpd/httpd/trunk/modules/database/mod_dbd.c
ViewVC logotype

Contents of /httpd/httpd/trunk/modules/database/mod_dbd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 521230 - (hide annotations)
Thu Mar 22 11:58:20 2007 UTC (2 years, 8 months ago) by fuankg
File MIME type: text/plain
File size: 27630 byte(s)
added proper error message for NetWare.
1 fielding 420983 /* 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 niq 170729 *
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    
17     /* Overview of what this is and does:
18     * http://www.apache.org/~niq/dbd.html
19     * or
20     * http://apache.webthing.com/database/
21     */
22    
23 chrisd 491884 #include "apr_reslist.h"
24     #include "apr_strings.h"
25     #include "apr_hash.h"
26 chrisd 503931 #include "apr_tables.h"
27 chrisd 491884 #include "apr_lib.h"
28     #include "apr_dbd.h"
29 niq 170729
30 chrisd 491884 #define APR_WANT_MEMFUNC
31     #define APR_WANT_STRFUNC
32     #include "apr_want.h"
33    
34 niq 170729 #include "http_protocol.h"
35     #include "http_config.h"
36     #include "http_log.h"
37 rpluem 483630 #include "http_request.h"
38 niq 170729 #include "mod_dbd.h"
39 niq 290711
40 niq 170729 extern module AP_MODULE_DECLARE_DATA dbd_module;
41    
42     /************ svr cfg: manage db connection pool ****************/
43    
44 niq 348026 #define NMIN_SET 0x1
45     #define NKEEP_SET 0x2
46     #define NMAX_SET 0x4
47     #define EXPTIME_SET 0x8
48    
49 chrisd 491729 typedef struct {
50 chrisd 491884 server_rec *server;
51 niq 170729 const char *name;
52     const char *params;
53     int persist;
54     #if APR_HAS_THREADS
55     int nmin;
56     int nkeep;
57     int nmax;
58     int exptime;
59 chrisd 491884 int set;
60 chrisd 503931 #endif
61     apr_hash_t *queries;
62     } dbd_cfg_t;
63    
64     typedef struct dbd_group_t dbd_group_t;
65    
66     struct dbd_group_t {
67     dbd_cfg_t *cfg;
68     dbd_group_t *next;
69     apr_pool_t *pool;
70     #if APR_HAS_THREADS
71     apr_thread_mutex_t *mutex;
72     apr_reslist_t *reslist;
73     int destroyed;
74 niq 170729 #else
75 chrisd 491729 ap_dbd_t *rec;
76 niq 170729 #endif
77 chrisd 503931 };
78    
79     typedef struct {
80     dbd_cfg_t *cfg;
81     dbd_group_t *group;
82 niq 170729 } svr_cfg;
83    
84     typedef enum { cmd_name, cmd_params, cmd_persist,
85     cmd_min, cmd_keep, cmd_max, cmd_exp
86     } cmd_parts;
87    
88 chrisd 503931 static apr_pool_t *config_pool;
89     static dbd_group_t *group_list;
90 niq 424798
91 niq 413015 /* a default DBDriver value that'll generate meaningful error messages */
92     static const char *const no_dbdriver = "[DBDriver unset]";
93 niq 170729
94 chrisd 491729 /* A default nmin of >0 will help with generating meaningful
95     * startup error messages if the database is down.
96     */
97     #define DEFAULT_NMIN 1
98     #define DEFAULT_NKEEP 2
99     #define DEFAULT_NMAX 10
100     #define DEFAULT_EXPTIME 300
101    
102     static void *create_dbd_config(apr_pool_t *pool, server_rec *s)
103     {
104     svr_cfg *svr = apr_pcalloc(pool, sizeof(svr_cfg));
105 chrisd 503931 dbd_cfg_t *cfg = svr->cfg = apr_pcalloc(pool, sizeof(dbd_cfg_t));
106 chrisd 491729
107 chrisd 503931 cfg->server = s;
108     cfg->name = no_dbdriver; /* to generate meaningful error messages */
109     cfg->params = ""; /* don't risk segfault on misconfiguration */
110     cfg->persist = -1;
111 chrisd 491729 #if APR_HAS_THREADS
112 chrisd 503931 cfg->nmin = DEFAULT_NMIN;
113     cfg->nkeep = DEFAULT_NKEEP;
114     cfg->nmax = DEFAULT_NMAX;
115     cfg->exptime = DEFAULT_EXPTIME;
116 chrisd 491729 #endif
117 chrisd 503931 cfg->queries = apr_hash_make(pool);
118 chrisd 491729
119     return svr;
120     }
121    
122     static void *merge_dbd_config(apr_pool_t *pool, void *basev, void *addv)
123     {
124 chrisd 503931 dbd_cfg_t *base = ((svr_cfg*) basev)->cfg;
125     dbd_cfg_t *add = ((svr_cfg*) addv)->cfg;
126 chrisd 491729 svr_cfg *svr = apr_pcalloc(pool, sizeof(svr_cfg));
127 chrisd 503931 dbd_cfg_t *new = svr->cfg = apr_pcalloc(pool, sizeof(dbd_cfg_t));
128 chrisd 491729
129 chrisd 503931 new->server = add->server;
130     new->name = (add->name != no_dbdriver) ? add->name : base->name;
131     new->params = strcmp(add->params, "") ? add->params : base->params;
132     new->persist = (add->persist != -1) ? add->persist : base->persist;
133 chrisd 491729 #if APR_HAS_THREADS
134 chrisd 503931 new->nmin = (add->set&NMIN_SET) ? add->nmin : base->nmin;
135     new->nkeep = (add->set&NKEEP_SET) ? add->nkeep : base->nkeep;
136     new->nmax = (add->set&NMAX_SET) ? add->nmax : base->nmax;
137     new->exptime = (add->set&EXPTIME_SET) ? add->exptime : base->exptime;
138 chrisd 491729 #endif
139 chrisd 503931 new->queries = apr_hash_overlay(pool, add->queries, base->queries);
140 chrisd 491729
141     return svr;
142     }
143    
144 chrisd 492394 #define ISINT(val) do { \
145     const char *p; \
146     \
147     for (p = val; *p; ++p) { \
148     if (!apr_isdigit(*p)) { \
149     return "Argument must be numeric!"; \
150     } \
151     } \
152     } while (0)
153 chrisd 491729
154     static const char *dbd_param(cmd_parms *cmd, void *dconf, const char *val)
155 niq 170729 {
156 niq 231464 const apr_dbd_driver_t *driver = NULL;
157 chrisd 491729 svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
158     &dbd_module);
159 chrisd 503931 dbd_cfg_t *cfg = svr->cfg;
160 niq 170729
161 niq 291100 switch ((long) cmd->info) {
162 niq 170729 case cmd_name:
163 chrisd 503931 cfg->name = val;
164 niq 170729 /* loading the driver involves once-only dlloading that is
165     * best done at server startup. This also guarantees that
166 niq 345040 * we won't return an error later.
167 niq 170729 */
168 chrisd 503931 switch (apr_dbd_get_driver(cmd->pool, cfg->name, &driver)) {
169 niq 170729 case APR_ENOTIMPL:
170 chrisd 503931 return apr_psprintf(cmd->pool, "DBD: No driver for %s", cfg->name);
171 niq 170729 case APR_EDSOOPEN:
172     return apr_psprintf(cmd->pool,
173 fuankg 521230 #ifdef NETWARE
174     "DBD: Can't load driver file dbd%s.nlm",
175     #else
176 niq 170729 "DBD: Can't load driver file apr_dbd_%s.so",
177 fuankg 521230 #endif
178 chrisd 503931 cfg->name);
179 niq 170729 case APR_ESYMNOTFOUND:
180     return apr_psprintf(cmd->pool,
181     "DBD: Failed to load driver apr_dbd_%s_driver",
182 chrisd 503931 cfg->name);
183 niq 170729 }
184     break;
185     case cmd_params:
186 chrisd 503931 cfg->params = val;
187 niq 170729 break;
188     #if APR_HAS_THREADS
189     case cmd_min:
190     ISINT(val);
191 chrisd 503931 cfg->nmin = atoi(val);
192     cfg->set |= NMIN_SET;
193 niq 170729 break;
194     case cmd_keep:
195     ISINT(val);
196 chrisd 503931 cfg->nkeep = atoi(val);
197     cfg->set |= NKEEP_SET;
198 niq 170729 break;
199     case cmd_max:
200     ISINT(val);
201 chrisd 503931 cfg->nmax = atoi(val);
202     cfg->set |= NMAX_SET;
203 niq 170729 break;
204     case cmd_exp:
205     ISINT(val);
206 chrisd 503931 cfg->exptime = atoi(val);
207     cfg->set |= EXPTIME_SET;
208 niq 170729 break;
209     #endif
210     }
211 chrisd 491729
212 niq 170729 return NULL;
213     }
214 chrisd 491729
215     static const char *dbd_param_flag(cmd_parms *cmd, void *dconf, int flag)
216 niq 348026 {
217 chrisd 491729 svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
218     &dbd_module);
219 niq 348026
220     switch ((long) cmd->info) {
221     case cmd_persist:
222 chrisd 503931 svr->cfg->persist = flag;
223 niq 348026 break;
224     }
225 chrisd 491729
226 niq 348026 return NULL;
227     }
228 chrisd 491729
229     static const char *dbd_prepare(cmd_parms *cmd, void *dconf, const char *query,
230 niq 234017 const char *label)
231     {
232 chrisd 503931 if (!label) {
233     label = query;
234     query = "";
235     }
236    
237 niq 234017 ap_dbd_prepare(cmd->server, query, label);
238 chrisd 491729
239 niq 170729 return NULL;
240     }
241 chrisd 491729
242 niq 170729 static const command_rec dbd_cmds[] = {
243     AP_INIT_TAKE1("DBDriver", dbd_param, (void*)cmd_name, RSRC_CONF,
244     "SQL Driver"),
245     AP_INIT_TAKE1("DBDParams", dbd_param, (void*)cmd_params, RSRC_CONF,
246     "SQL Driver Params"),
247 niq 348026 AP_INIT_FLAG("DBDPersist", dbd_param_flag, (void*)cmd_persist, RSRC_CONF,
248     "Use persistent connection/pool"),
249 chrisd 503931 AP_INIT_TAKE12("DBDPrepareSQL", dbd_prepare, NULL, RSRC_CONF,
250     "SQL statement to prepare (or nothing, to override "
251     "statement inherited from main server) and label"),
252 niq 170729 #if APR_HAS_THREADS
253     AP_INIT_TAKE1("DBDMin", dbd_param, (void*)cmd_min, RSRC_CONF,
254     "Minimum number of connections"),
255 niq 348026 /* XXX: note that mod_proxy calls this "smax" */
256 niq 170729 AP_INIT_TAKE1("DBDKeep", dbd_param, (void*)cmd_keep, RSRC_CONF,
257     "Maximum number of sustained connections"),
258     AP_INIT_TAKE1("DBDMax", dbd_param, (void*)cmd_max, RSRC_CONF,
259     "Maximum number of connections"),
260 niq 348026 /* XXX: note that mod_proxy calls this "ttl" (time to live) */
261 niq 170729 AP_INIT_TAKE1("DBDExptime", dbd_param, (void*)cmd_exp, RSRC_CONF,
262     "Keepalive time for idle connections"),
263     #endif
264     {NULL}
265     };
266 chrisd 491729
267     static int dbd_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
268     apr_pool_t *ptemp)
269     {
270 chrisd 503931 config_pool = pconf;
271     group_list = NULL;
272 chrisd 491729 return OK;
273 niq 348026 }
274 chrisd 491729
275     DBD_DECLARE_NONSTD(void) ap_dbd_prepare(server_rec *s, const char *query,
276     const char *label)
277 niq 170729 {
278 chrisd 503931 svr_cfg *svr;
279 chrisd 491729
280 chrisd 503931 svr = ap_get_module_config(s->module_config, &dbd_module);
281     if (!svr) {
282     /* some modules may call from within config directive handlers, and
283     * if these are called in a server context that contains no mod_dbd
284     * config directives, then we have to create our own server config
285     */
286     svr = create_dbd_config(config_pool, s);
287     ap_set_module_config(s->module_config, &dbd_module, svr);
288     }
289 chrisd 491729
290 chrisd 503931 if (apr_hash_get(svr->cfg->queries, label, APR_HASH_KEY_STRING)
291     && strcmp(query, "")) {
292     ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
293     "conflicting SQL statements with label %s", label);
294     }
295    
296     apr_hash_set(svr->cfg->queries, label, APR_HASH_KEY_STRING, query);
297 niq 170729 }
298 chrisd 491729
299 chrisd 503931 typedef struct {
300     const char *label, *query;
301     } dbd_query_t;
302    
303 chrisd 491729 static int dbd_post_config(apr_pool_t *pconf, apr_pool_t *plog,
304     apr_pool_t *ptemp, server_rec *s)
305     {
306     server_rec *sp;
307 chrisd 503931 apr_array_header_t *add_queries = apr_array_make(ptemp, 10,
308     sizeof(dbd_query_t));
309 chrisd 491729
310     for (sp = s; sp; sp = sp->next) {
311     svr_cfg *svr = ap_get_module_config(sp->module_config, &dbd_module);
312 chrisd 503931 dbd_cfg_t *cfg = svr->cfg;
313     apr_hash_index_t *hi_first = apr_hash_first(ptemp, cfg->queries);
314     dbd_group_t *group;
315 chrisd 491729
316 chrisd 503931 /* dbd_setup in 2.2.3 and under was causing spurious error messages
317     * when dbd isn't configured. We can stop that with a quick check here
318     * together with a similar check in ap_dbd_open (where being
319     * unconfigured is a genuine error that must be reported).
320     */
321     if (cfg->name == no_dbdriver || !cfg->persist) {
322     continue;
323     }
324    
325     for (group = group_list; group; group = group->next) {
326     dbd_cfg_t *group_cfg = group->cfg;
327     apr_hash_index_t *hi;
328     int group_ok = 1;
329    
330     if (strcmp(cfg->name, group_cfg->name)
331     || strcmp(cfg->params, group_cfg->params)) {
332     continue;
333     }
334    
335     #if APR_HAS_THREADS
336     if (cfg->nmin != group_cfg->nmin
337     || cfg->nkeep != group_cfg->nkeep
338     || cfg->nmax != group_cfg->nmax
339     || cfg->exptime != group_cfg->exptime) {
340     continue;
341     }
342     #endif
343    
344     add_queries->nelts = 0;
345    
346     for (hi = hi_first; hi; hi = apr_hash_next(hi)) {
347     const char *label, *query;
348     const char *group_query;
349    
350     apr_hash_this(hi, (void*) &label, NULL, (void*) &query);
351    
352     group_query = apr_hash_get(group_cfg->queries, label,
353     APR_HASH_KEY_STRING);
354    
355     if (!group_query) {
356     dbd_query_t *add_query = apr_array_push(add_queries);
357    
358     add_query->label = label;
359     add_query->query = query;
360     }
361     else if (strcmp(query, group_query)) {
362     group_ok = 0;
363     break;
364     }
365     }
366    
367     if (group_ok) {
368     int i;
369    
370     for (i = 0; i < add_queries->nelts; ++i) {
371     dbd_query_t *add_query = ((dbd_query_t*) add_queries->elts)
372     + i;
373    
374     apr_hash_set(group_cfg->queries, add_query->label,
375     APR_HASH_KEY_STRING, add_query->query);
376     }
377    
378     svr->group = group;
379     break;
380     }
381     }
382    
383     if (!svr->group) {
384     svr->group = group = apr_pcalloc(pconf, sizeof(dbd_group_t));
385    
386     group->cfg = cfg;
387    
388     group->next = group_list;
389     group_list = group;
390     }
391 chrisd 491729 }
392    
393     return OK;
394     }
395    
396 chrisd 503931 static apr_status_t dbd_prepared_init(apr_pool_t *pool, dbd_cfg_t *cfg,
397 chrisd 491729 ap_dbd_t *rec)
398 niq 170729 {
399 chrisd 503931 apr_hash_index_t *hi;
400 chrisd 491729 apr_status_t rv = APR_SUCCESS;
401 niq 170729
402 chrisd 491729 rec->prepared = apr_hash_make(pool);
403    
404 chrisd 503931 for (hi = apr_hash_first(pool, cfg->queries); hi;
405     hi = apr_hash_next(hi)) {
406     const char *label, *query;
407     apr_dbd_prepared_t *stmt;
408    
409     apr_hash_this(hi, (void*) &label, NULL, (void*) &query);
410    
411     if (!strcmp(query, "")) {
412     continue;
413     }
414    
415 niq 344140 stmt = NULL;
416 chrisd 503931 if (apr_dbd_prepare(rec->driver, pool, rec->handle, query,
417     label, &stmt)) {
418     rv = APR_EGENERAL;
419 niq 170729 }
420     else {
421 chrisd 503931 apr_hash_set(rec->prepared, label, APR_HASH_KEY_STRING, stmt);
422 niq 170729 }
423     }
424 chrisd 491729
425     return rv;
426 niq 170729 }
427 chrisd 491729
428     static apr_status_t dbd_close(void *data)
429     {
430     ap_dbd_t *rec = data;
431    
432 chrisd 496831 return apr_dbd_close(rec->driver, rec->handle);
433 chrisd 491729 }
434    
435     #if APR_HAS_THREADS
436     static apr_status_t dbd_destruct(void *data, void *params, apr_pool_t *pool)
437     {
438 chrisd 503931 dbd_group_t *group = params;
439 chrisd 496831
440 chrisd 503931 if (!group->destroyed) {
441 chrisd 496831 ap_dbd_t *rec = data;
442    
443     apr_pool_destroy(rec->pool);
444     }
445    
446     return APR_SUCCESS;
447 chrisd 491729 }
448     #endif
449    
450 niq 234017 /* an apr_reslist_constructor for SQL connections
451     * Also use this for opening in non-reslist modes, since it gives
452     * us all the error-handling in one place.
453     */
454 chrisd 491729 static apr_status_t dbd_construct(void **data_ptr,
455     void *params, apr_pool_t *pool)
456 niq 170729 {
457 chrisd 503931 dbd_group_t *group = params;
458     dbd_cfg_t *cfg = group->cfg;
459 chrisd 496831 apr_pool_t *rec_pool, *prepared_pool;
460     ap_dbd_t *rec;
461 niq 348026 apr_status_t rv;
462 niq 170729
463 chrisd 496831 rv = apr_pool_create(&rec_pool, pool);
464 niq 348026 if (rv != APR_SUCCESS) {
465 chrisd 503931 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, cfg->server,
466 chrisd 491884 "DBD: Failed to create memory pool");
467     return rv;
468 niq 348026 }
469    
470 chrisd 496831 rec = apr_pcalloc(rec_pool, sizeof(ap_dbd_t));
471    
472     rec->pool = rec_pool;
473    
474 chrisd 491729 /* The driver is loaded at config time now, so this just checks a hash.
475     * If that changes, the driver DSO could be registered to unload against
476     * our pool, which is probably not what we want. Error checking isn't
477     * necessary now, but in case that changes in the future ...
478     */
479 chrisd 503931 rv = apr_dbd_get_driver(rec->pool, cfg->name, &rec->driver);
480 chrisd 491729 if (rv != APR_SUCCESS) {
481     switch (rv) {
482     case APR_ENOTIMPL:
483 chrisd 503931 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
484     "DBD: driver for %s not available", cfg->name);
485 chrisd 491729 break;
486     case APR_EDSOOPEN:
487 chrisd 503931 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
488     "DBD: can't find driver for %s", cfg->name);
489 chrisd 491729 break;
490     case APR_ESYMNOTFOUND:
491 chrisd 503931 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
492 chrisd 491884 "DBD: driver for %s is invalid or corrupted",
493 chrisd 503931 cfg->name);
494 chrisd 491729 break;
495     default:
496 chrisd 503931 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
497 chrisd 491884 "DBD: mod_dbd not compatible with APR in get_driver");
498 chrisd 491729 break;
499     }
500    
501 chrisd 491884 apr_pool_destroy(rec->pool);
502 niq 234017 return rv;
503     }
504    
505 chrisd 503931 rv = apr_dbd_open(rec->driver, rec->pool, cfg->params, &rec->handle);
506 chrisd 491729 if (rv != APR_SUCCESS) {
507     switch (rv) {
508     case APR_EGENERAL:
509 chrisd 503931 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
510     "DBD: Can't connect to %s", cfg->name);
511 chrisd 491729 break;
512     default:
513 chrisd 503931 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
514 chrisd 491884 "DBD: mod_dbd not compatible with APR in open");
515 chrisd 491729 break;
516     }
517    
518 chrisd 491884 apr_pool_destroy(rec->pool);
519 niq 170729 return rv;
520     }
521 chrisd 491729
522 chrisd 496831 apr_pool_cleanup_register(rec->pool, rec, dbd_close,
523     apr_pool_cleanup_null);
524    
525     /* we use a sub-pool for the prepared statements for each connection so
526     * that they will be cleaned up first, before the connection is closed
527     */
528     rv = apr_pool_create(&prepared_pool, rec->pool);
529 niq 386321 if (rv != APR_SUCCESS) {
530 chrisd 503931 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, cfg->server,
531 chrisd 496831 "DBD: Failed to create memory pool");
532    
533     apr_pool_destroy(rec->pool);
534     return rv;
535     }
536    
537 chrisd 503931 rv = dbd_prepared_init(prepared_pool, cfg, rec);
538 chrisd 496831 if (rv != APR_SUCCESS) {
539 niq 424798 const char *errmsg = apr_dbd_error(rec->driver, rec->handle, rv);
540 chrisd 503931 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
541 chrisd 491884 "DBD: failed to prepare SQL statements: %s",
542     (errmsg ? errmsg : "[???]"));
543    
544     apr_pool_destroy(rec->pool);
545     return rv;
546 niq 386321 }
547 chrisd 491729
548 chrisd 491884 *data_ptr = rec;
549    
550     return APR_SUCCESS;
551 niq 170729 }
552 chrisd 491729
553 niq 234017 #if APR_HAS_THREADS
554 chrisd 496831 static apr_status_t dbd_destroy(void *data)
555 niq 170729 {
556 chrisd 503931 dbd_group_t *group = data;
557 chrisd 496831
558 chrisd 503931 group->destroyed = 1;
559 chrisd 496831
560     return APR_SUCCESS;
561     }
562    
563 chrisd 503931 static apr_status_t dbd_setup(server_rec *s, dbd_group_t *group)
564 chrisd 496831 {
565 chrisd 503931 dbd_cfg_t *cfg = group->cfg;
566 niq 348026 apr_status_t rv;
567    
568 chrisd 496831 /* We create the reslist using a sub-pool of the pool passed to our
569     * child_init hook. No other threads can be here because we're
570     * either in the child_init phase or dbd_setup_lock() acquired our mutex.
571     * No other threads will use this sub-pool after this, except via
572     * reslist calls, which have an internal mutex.
573     *
574     * We need to short-circuit the cleanup registered internally by
575     * apr_reslist_create(). We do this by registering dbd_destroy()
576     * as a cleanup afterwards, so that it will run before the reslist's
577     * internal cleanup.
578     *
579     * If we didn't do this, then we could free memory twice when the pool
580     * was destroyed. When apr_pool_destroy() runs, it first destroys all
581     * all the per-connection sub-pools created in dbd_construct(), and
582     * then it runs the reslist's cleanup. The cleanup calls dbd_destruct()
583     * on each resource, which would then attempt to destroy the sub-pools
584     * a second time.
585 niq 348026 */
586 chrisd 503931 rv = apr_reslist_create(&group->reslist,
587     cfg->nmin, cfg->nkeep, cfg->nmax,
588     apr_time_from_sec(cfg->exptime),
589     dbd_construct, dbd_destruct, group,
590     group->pool);
591 chrisd 491884 if (rv != APR_SUCCESS) {
592     ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
593     "DBD: failed to initialise");
594     return rv;
595 niq 170729 }
596 niq 348026
597 chrisd 503931 apr_pool_cleanup_register(group->pool, group, dbd_destroy,
598 chrisd 496831 apr_pool_cleanup_null);
599    
600 chrisd 491884 return APR_SUCCESS;
601 niq 170729 }
602 chrisd 496831 #endif
603 chrisd 491729
604 niq 348026 static apr_status_t dbd_setup_init(apr_pool_t *pool, server_rec *s)
605     {
606 chrisd 503931 dbd_group_t *group;
607     apr_status_t rv = APR_SUCCESS;
608 niq 170729
609 chrisd 503931 for (group = group_list; group; group = group->next) {
610     apr_status_t rv2;
611 niq 485311
612 chrisd 503931 rv2 = apr_pool_create(&group->pool, pool);
613     if (rv2 != APR_SUCCESS) {
614     ap_log_error(APLOG_MARK, APLOG_CRIT, rv2, s,
615     "DBD: Failed to create reslist cleanup memory pool");
616     return rv2;
617     }
618 niq 348026
619 chrisd 496831 #if APR_HAS_THREADS
620 chrisd 503931 rv2 = dbd_setup(s, group);
621     if (rv2 == APR_SUCCESS) {
622     continue;
623     }
624     else if (rv == APR_SUCCESS) {
625     rv = rv2;
626     }
627 niq 348026
628 chrisd 503931 /* we failed, so create a mutex so that subsequent competing callers
629     * to ap_dbd_open can serialize themselves while they retry
630     */
631     rv2 = apr_thread_mutex_create(&group->mutex,
632     APR_THREAD_MUTEX_DEFAULT, pool);
633     if (rv2 != APR_SUCCESS) {
634     ap_log_error(APLOG_MARK, APLOG_CRIT, rv2, s,
635     "DBD: Failed to create thread mutex");
636     return rv2;
637     }
638     #endif
639 niq 348026 }
640 chrisd 491729
641 niq 348026 return rv;
642     }
643 chrisd 491729
644 chrisd 496831 #if APR_HAS_THREADS
645 chrisd 503931 static apr_status_t dbd_setup_lock(server_rec *s, dbd_group_t *group)
646 niq 348026 {
647 chrisd 491729 apr_status_t rv = APR_SUCCESS, rv2;
648 niq 348026
649     /* several threads could be here at the same time, all trying to
650     * initialize the reslist because dbd_setup_init failed to do so
651     */
652 chrisd 503931 if (!group->mutex) {
653 niq 348026 /* we already logged an error when the mutex couldn't be created */
654     return APR_EGENERAL;
655     }
656    
657 chrisd 503931 rv2 = apr_thread_mutex_lock(group->mutex);
658 chrisd 491729 if (rv2 != APR_SUCCESS) {
659 chrisd 491884 ap_log_error(APLOG_MARK, APLOG_ERR, rv2, s,
660     "DBD: Failed to acquire thread mutex");
661 chrisd 491729 return rv2;
662 niq 348026 }
663    
664 chrisd 503931 if (!group->reslist) {
665     rv = dbd_setup(s, group);
666 niq 348026 }
667    
668 chrisd 503931 rv2 = apr_thread_mutex_unlock(group->mutex);
669 chrisd 491729 if (rv2 != APR_SUCCESS) {
670 chrisd 491884 ap_log_error(APLOG_MARK, APLOG_ERR, rv2, s,
671     "DBD: Failed to release thread mutex");
672 chrisd 491729 if (rv == APR_SUCCESS) {
673     rv = rv2;
674 niq 348026 }
675     }
676 chrisd 491729
677     return rv;
678 niq 348026 }
679 niq 170729 #endif
680    
681     /* Functions we export for modules to use:
682     - open acquires a connection from the pool (opens one if necessary)
683     - close releases it back in to the pool
684     */
685 chrisd 491729 DBD_DECLARE_NONSTD(void) ap_dbd_close(server_rec *s, ap_dbd_t *rec)
686 niq 345040 {
687     svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
688 chrisd 491729
689 chrisd 503931 if (!svr->cfg->persist) {
690 chrisd 496831 apr_pool_destroy(rec->pool);
691 niq 345040 }
692     #if APR_HAS_THREADS
693     else {
694 chrisd 503931 apr_reslist_release(svr->group->reslist, rec);
695 niq 345040 }
696     #endif
697     }
698 chrisd 491729
699 chrisd 491884 static apr_status_t dbd_check(apr_pool_t *pool, server_rec *s, ap_dbd_t *rec)
700     {
701     svr_cfg *svr;
702     apr_status_t rv = apr_dbd_check_conn(rec->driver, pool, rec->handle);
703     const char *errmsg;
704 chrisd 491729
705 chrisd 491884 if ((rv == APR_SUCCESS) || (rv == APR_ENOTIMPL)) {
706     return APR_SUCCESS;
707     }
708    
709     errmsg = apr_dbd_error(rec->driver, rec->handle, rv);
710     if (!errmsg) {
711     errmsg = "(unknown)";
712     }
713    
714     svr = ap_get_module_config(s->module_config, &dbd_module);
715     ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
716 chrisd 503931 "DBD [%s] Error: %s", svr->cfg->name, errmsg);
717 chrisd 491884 return rv;
718     }
719    
720 colm 349768 DBD_DECLARE_NONSTD(ap_dbd_t*) ap_dbd_open(apr_pool_t *pool, server_rec *s)
721 niq 170729 {
722 chrisd 491729 svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
723 chrisd 503931 dbd_group_t *group = svr->group;
724     dbd_cfg_t *cfg = svr->cfg;
725 chrisd 491884 ap_dbd_t *rec = NULL;
726     #if APR_HAS_THREADS
727     apr_status_t rv;
728     #endif
729 niq 170729
730 niq 485311 /* If nothing is configured, we shouldn't be here */
731 chrisd 503931 if (cfg->name == no_dbdriver) {
732 chrisd 491884 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "DBD: not configured");
733 niq 485311 return NULL;
734     }
735    
736 chrisd 503931 if (!cfg->persist) {
737 niq 234017 /* Return a once-only connection */
738 chrisd 503931 group = apr_pcalloc(pool, sizeof(dbd_group_t));
739    
740     group->cfg = cfg;
741    
742     dbd_construct((void*) &rec, group, pool);
743 chrisd 491884 return rec;
744 niq 170729 }
745    
746 chrisd 491884 #if APR_HAS_THREADS
747 chrisd 503931 if (!group->reslist) {
748     if (dbd_setup_lock(s, group) != APR_SUCCESS) {
749 niq 170729 return NULL;
750     }
751     }
752 chrisd 491729
753 chrisd 503931 rv = apr_reslist_acquire(group->reslist, (void*) &rec);
754 niq 348026 if (rv != APR_SUCCESS) {
755 chrisd 491884 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
756     "Failed to acquire DBD connection from pool!");
757 niq 170729 return NULL;
758     }
759 chrisd 491729
760 chrisd 491884 if (dbd_check(pool, s, rec) != APR_SUCCESS) {
761 chrisd 503931 apr_reslist_invalidate(group->reslist, rec);
762 niq 170729 return NULL;
763     }
764     #else
765 chrisd 491729 /* If we have a persistent connection and it's good, we'll use it;
766     * since this is non-threaded, we can update without a mutex
767     */
768 chrisd 503931 rec = group->rec;
769     if (rec) {
770 chrisd 491884 if (dbd_check(pool, s, rec) != APR_SUCCESS) {
771 chrisd 496831 apr_pool_destroy(rec->pool);
772 chrisd 491884 rec = NULL;
773 niq 170729 }
774     }
775 chrisd 491729
776     /* We don't have a connection right now, so we'll open one */
777 chrisd 491884 if (!rec) {
778 chrisd 503931 dbd_construct((void*) &rec, group, group->pool);
779     group->rec = rec;
780 niq 170729 }
781 chrisd 491884 #endif
782 chrisd 491729
783 chrisd 491884 return rec;
784 niq 170729 }
785 chrisd 491729
786 niq 170729 #if APR_HAS_THREADS
787     typedef struct {
788 chrisd 491729 ap_dbd_t *rec;
789     apr_reslist_t *reslist;
790     } dbd_acquire_t;
791    
792     static apr_status_t dbd_release(void *data)
793 niq 170729 {
794 chrisd 491729 dbd_acquire_t *acq = data;
795     apr_reslist_release(acq->reslist, acq->rec);
796 niq 170729 return APR_SUCCESS;
797     }
798 chrisd 491729
799 colm 349768 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
800 niq 170729 {
801 chrisd 491729 dbd_acquire_t *acq;
802 chrisd 481509
803     while (!ap_is_initial_req(r)) {
804     if (r->prev) {
805     r = r->prev;
806     }
807     else if (r->main) {
808     r = r->main;
809     }
810     }
811    
812 chrisd 491729 acq = ap_get_module_config(r->request_config, &dbd_module);
813     if (!acq) {
814     acq = apr_palloc(r->pool, sizeof(dbd_acquire_t));
815     acq->rec = ap_dbd_open(r->pool, r->server);
816     if (acq->rec) {
817     svr_cfg *svr = ap_get_module_config(r->server->module_config,
818     &dbd_module);
819    
820     ap_set_module_config(r->request_config, &dbd_module, acq);
821 chrisd 503931 if (svr->cfg->persist) {
822     acq->reslist = svr->group->reslist;
823 chrisd 491729 apr_pool_cleanup_register(r->pool, acq, dbd_release,
824 niq 170729 apr_pool_cleanup_null);
825     }
826     }
827     }
828 chrisd 491729
829     return acq->rec;
830 niq 170729 }
831 chrisd 491729
832 colm 349768 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
833 niq 307439 {
834 chrisd 491729 dbd_acquire_t *acq = ap_get_module_config(c->conn_config, &dbd_module);
835    
836     if (!acq) {
837     acq = apr_palloc(c->pool, sizeof(dbd_acquire_t));
838     acq->rec = ap_dbd_open(c->pool, c->base_server);
839     if (acq->rec) {
840     svr_cfg *svr = ap_get_module_config(c->base_server->module_config,
841     &dbd_module);
842    
843     ap_set_module_config(c->conn_config, &dbd_module, acq);
844 chrisd 503931 if (svr->cfg->persist) {
845     acq->reslist = svr->group->reslist;
846 chrisd 491729 apr_pool_cleanup_register(c->pool, acq, dbd_release,
847 niq 307439 apr_pool_cleanup_null);
848     }
849     }
850     }
851 chrisd 491729
852     return acq->rec;
853 niq 307439 }
854 niq 170729 #else
855 colm 349768 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
856 niq 170729 {
857 chrisd 491729 ap_dbd_t *rec;
858 chrisd 481509
859     while (!ap_is_initial_req(r)) {
860     if (r->prev) {
861     r = r->prev;
862     }
863     else if (r->main) {
864     r = r->main;
865     }
866     }
867    
868 chrisd 491729 rec = ap_get_module_config(r->request_config, &dbd_module);
869     if (!rec) {
870     rec = ap_dbd_open(r->pool, r->server);
871     if (rec) {
872     ap_set_module_config(r->request_config, &dbd_module, rec);
873 niq 170729 }
874     }
875 chrisd 491729
876     return rec;
877 niq 170729 }
878 chrisd 491729
879 colm 349768 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
880 niq 307439 {
881 chrisd 491729 ap_dbd_t *rec = ap_get_module_config(c->conn_config, &dbd_module);
882    
883     if (!rec) {
884     rec = ap_dbd_open(c->pool, c->base_server);
885     if (rec) {
886     ap_set_module_config(c->conn_config, &dbd_module, rec);
887 niq 307439 }
888     }
889 chrisd 491729
890     return rec;
891 niq 307439 }
892 niq 170729 #endif
893    
894     static void dbd_hooks(apr_pool_t *pool)
895     {
896 chrisd 491729 ap_hook_pre_config(dbd_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
897     ap_hook_post_config(dbd_post_config, NULL, NULL, APR_HOOK_MIDDLE);
898 niq 348026 ap_hook_child_init((void*)dbd_setup_init, NULL, NULL, APR_HOOK_MIDDLE);
899 chrisd 491729
900     APR_REGISTER_OPTIONAL_FN(ap_dbd_prepare);
901 niq 170729 APR_REGISTER_OPTIONAL_FN(ap_dbd_open);
902     APR_REGISTER_OPTIONAL_FN(ap_dbd_close);
903     APR_REGISTER_OPTIONAL_FN(ap_dbd_acquire);
904 niq 307439 APR_REGISTER_OPTIONAL_FN(ap_dbd_cacquire);
905 chrisd 491729
906 niq 170729 apr_dbd_init(pool);
907     }
908    
909     module AP_MODULE_DECLARE_DATA dbd_module = {
910     STANDARD20_MODULE_STUFF,
911     NULL,
912     NULL,
913 chrisd 491729 create_dbd_config,
914     merge_dbd_config,
915 niq 170729 dbd_cmds,
916     dbd_hooks
917     };
918 chrisd 491729

Properties

Name Value
svn:eol-style native

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2