]> granicus.if.org Git - apache/blob - modules/database/mod_dbd.c
9914875caf14be914a725b697011fdaebc7af9a9
[apache] / modules / database / mod_dbd.c
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
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 #include "apr_reslist.h"
24 #include "apr_strings.h"
25 #include "apr_hash.h"
26 #include "apr_tables.h"
27 #include "apr_lib.h"
28 #include "apr_dbd.h"
29
30 #define APR_WANT_MEMFUNC
31 #define APR_WANT_STRFUNC
32 #include "apr_want.h"
33
34 #include "http_protocol.h"
35 #include "http_config.h"
36 #include "http_log.h"
37 #include "http_request.h"
38 #include "mod_dbd.h"
39
40 extern module AP_MODULE_DECLARE_DATA dbd_module;
41
42 /************ svr cfg: manage db connection pool ****************/
43
44 #define NMIN_SET     0x1
45 #define NKEEP_SET    0x2
46 #define NMAX_SET     0x4
47 #define EXPTIME_SET  0x8
48
49 typedef struct {
50     server_rec *server;
51     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     int set;
60 #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 #else
75     ap_dbd_t *rec;
76 #endif
77 };
78
79 typedef struct {
80     dbd_cfg_t *cfg;
81     dbd_group_t *group;
82 } 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 static apr_pool_t *config_pool;
89 static dbd_group_t *group_list;
90
91 /* a default DBDriver value that'll generate meaningful error messages */
92 static const char *const no_dbdriver = "[DBDriver unset]";
93
94 /* 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     dbd_cfg_t *cfg = svr->cfg = apr_pcalloc(pool, sizeof(dbd_cfg_t));
106
107     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 #if APR_HAS_THREADS
112     cfg->nmin = DEFAULT_NMIN;
113     cfg->nkeep = DEFAULT_NKEEP;
114     cfg->nmax = DEFAULT_NMAX;
115     cfg->exptime = DEFAULT_EXPTIME;
116 #endif
117     cfg->queries = apr_hash_make(pool);
118
119     return svr;
120 }
121
122 static void *merge_dbd_config(apr_pool_t *pool, void *basev, void *addv)
123 {
124     dbd_cfg_t *base = ((svr_cfg*) basev)->cfg;
125     dbd_cfg_t *add = ((svr_cfg*) addv)->cfg;
126     svr_cfg *svr = apr_pcalloc(pool, sizeof(svr_cfg));
127     dbd_cfg_t *new = svr->cfg = apr_pcalloc(pool, sizeof(dbd_cfg_t));
128
129     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 #if APR_HAS_THREADS
134     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 #endif
139     new->queries = apr_hash_overlay(pool, add->queries, base->queries);
140
141     return svr;
142 }
143
144 #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
154 static const char *dbd_param(cmd_parms *cmd, void *dconf, const char *val)
155 {
156     const apr_dbd_driver_t *driver = NULL;
157     svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
158                                         &dbd_module);
159     dbd_cfg_t *cfg = svr->cfg;
160
161     switch ((long) cmd->info) {
162     case cmd_name:
163         cfg->name = val;
164         /* loading the driver involves once-only dlloading that is
165          * best done at server startup.  This also guarantees that
166          * we won't return an error later.
167          */
168         switch (apr_dbd_get_driver(cmd->pool, cfg->name, &driver)) {
169         case APR_ENOTIMPL:
170             return apr_psprintf(cmd->pool, "DBD: No driver for %s", cfg->name);
171         case APR_EDSOOPEN:
172             return apr_psprintf(cmd->pool,
173 #ifdef NETWARE
174                                 "DBD: Can't load driver file dbd%s.nlm",
175 #else
176                                 "DBD: Can't load driver file apr_dbd_%s.so",
177 #endif
178                                 cfg->name);
179         case APR_ESYMNOTFOUND:
180             return apr_psprintf(cmd->pool,
181                                 "DBD: Failed to load driver apr_dbd_%s_driver",
182                                 cfg->name);
183         }
184         break;
185     case cmd_params:
186         cfg->params = val;
187         break;
188 #if APR_HAS_THREADS
189     case cmd_min:
190         ISINT(val);
191         cfg->nmin = atoi(val);
192         cfg->set |= NMIN_SET;
193         break;
194     case cmd_keep:
195         ISINT(val);
196         cfg->nkeep = atoi(val);
197         cfg->set |= NKEEP_SET;
198         break;
199     case cmd_max:
200         ISINT(val);
201         cfg->nmax = atoi(val);
202         cfg->set |= NMAX_SET;
203         break;
204     case cmd_exp:
205         ISINT(val);
206         cfg->exptime = atoi(val);
207         cfg->set |= EXPTIME_SET;
208         break;
209 #endif
210     }
211
212     return NULL;
213 }
214
215 static const char *dbd_param_flag(cmd_parms *cmd, void *dconf, int flag)
216 {
217     svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
218                                         &dbd_module);
219
220     switch ((long) cmd->info) {
221     case cmd_persist:
222         svr->cfg->persist = flag;
223         break;
224     }
225
226     return NULL;
227 }
228
229 static const char *dbd_prepare(cmd_parms *cmd, void *dconf, const char *query,
230                                const char *label)
231 {
232     if (!label) {
233         label = query;
234         query = "";
235     }
236
237     ap_dbd_prepare(cmd->server, query, label);
238
239     return NULL;
240 }
241
242 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     AP_INIT_FLAG("DBDPersist", dbd_param_flag, (void*)cmd_persist, RSRC_CONF,
248                  "Use persistent connection/pool"),
249     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 #if APR_HAS_THREADS
253     AP_INIT_TAKE1("DBDMin", dbd_param, (void*)cmd_min, RSRC_CONF,
254                   "Minimum number of connections"),
255     /* XXX: note that mod_proxy calls this "smax" */
256     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     /* XXX: note that mod_proxy calls this "ttl" (time to live) */
261     AP_INIT_TAKE1("DBDExptime", dbd_param, (void*)cmd_exp, RSRC_CONF,
262                   "Keepalive time for idle connections"),
263 #endif
264     {NULL}
265 };
266
267 static int dbd_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
268                           apr_pool_t *ptemp)
269 {
270    config_pool = pconf;
271    group_list = NULL;
272    return OK;
273 }
274
275 DBD_DECLARE_NONSTD(void) ap_dbd_prepare(server_rec *s, const char *query,
276                                         const char *label)
277 {
278     svr_cfg *svr;
279
280     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
290     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 }
298
299 typedef struct {
300     const char *label, *query;
301 } dbd_query_t;
302
303 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     apr_array_header_t *add_queries = apr_array_make(ptemp, 10,
308                                                      sizeof(dbd_query_t));
309
310     for (sp = s; sp; sp = sp->next) {
311         svr_cfg *svr = ap_get_module_config(sp->module_config, &dbd_module);
312         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
316         /* 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     }
392
393     return OK;
394 }
395
396 static apr_status_t dbd_prepared_init(apr_pool_t *pool, dbd_cfg_t *cfg,
397                                       ap_dbd_t *rec)
398 {
399     apr_hash_index_t *hi;
400     apr_status_t rv = APR_SUCCESS;
401
402     rec->prepared = apr_hash_make(pool);
403
404     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         stmt = NULL;
416         if (apr_dbd_prepare(rec->driver, pool, rec->handle, query,
417                             label, &stmt)) {
418             rv = APR_EGENERAL;
419         }
420         else {
421             apr_hash_set(rec->prepared, label, APR_HASH_KEY_STRING, stmt);
422         }
423     }
424
425     return rv;
426 }
427
428 static apr_status_t dbd_close(void *data)
429 {
430     ap_dbd_t *rec = data;
431
432     return apr_dbd_close(rec->driver, rec->handle);
433 }
434
435 #if APR_HAS_THREADS
436 static apr_status_t dbd_destruct(void *data, void *params, apr_pool_t *pool)
437 {
438     dbd_group_t *group = params;
439
440     if (!group->destroyed) {
441         ap_dbd_t *rec = data;
442
443         apr_pool_destroy(rec->pool);
444     }
445
446     return APR_SUCCESS;
447 }
448 #endif
449
450 /* 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 static apr_status_t dbd_construct(void **data_ptr,
455                                   void *params, apr_pool_t *pool)
456 {
457     dbd_group_t *group = params;
458     dbd_cfg_t *cfg = group->cfg;
459     apr_pool_t *rec_pool, *prepared_pool;
460     ap_dbd_t *rec;
461     apr_status_t rv;
462
463     rv = apr_pool_create(&rec_pool, pool);
464     if (rv != APR_SUCCESS) {
465         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, cfg->server,
466                      "DBD: Failed to create memory pool");
467         return rv;
468     }
469
470     rec = apr_pcalloc(rec_pool, sizeof(ap_dbd_t));
471
472     rec->pool = rec_pool;
473
474     /* 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     rv = apr_dbd_get_driver(rec->pool, cfg->name, &rec->driver);
480     if (rv != APR_SUCCESS) {
481         switch (rv) {
482         case APR_ENOTIMPL:
483             ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
484                          "DBD: driver for %s not available", cfg->name);
485             break;
486         case APR_EDSOOPEN:
487             ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
488                          "DBD: can't find driver for %s", cfg->name);
489             break;
490         case APR_ESYMNOTFOUND:
491             ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
492                          "DBD: driver for %s is invalid or corrupted",
493                          cfg->name);
494             break;
495         default:
496             ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
497                          "DBD: mod_dbd not compatible with APR in get_driver");
498             break;
499         }
500
501         apr_pool_destroy(rec->pool);
502         return rv;
503     }
504
505     rv = apr_dbd_open(rec->driver, rec->pool, cfg->params, &rec->handle);
506     if (rv != APR_SUCCESS) {
507         switch (rv) {
508         case APR_EGENERAL:
509             ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
510                          "DBD: Can't connect to %s", cfg->name);
511             break;
512         default:
513             ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
514                          "DBD: mod_dbd not compatible with APR in open");
515             break;
516         }
517
518         apr_pool_destroy(rec->pool);
519         return rv;
520     }
521
522     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     if (rv != APR_SUCCESS) {
530         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, cfg->server,
531                      "DBD: Failed to create memory pool");
532
533         apr_pool_destroy(rec->pool);
534         return rv;
535     }
536
537     rv = dbd_prepared_init(prepared_pool, cfg, rec);
538     if (rv != APR_SUCCESS) {
539         const char *errmsg = apr_dbd_error(rec->driver, rec->handle, rv);
540         ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
541                      "DBD: failed to prepare SQL statements: %s",
542                      (errmsg ? errmsg : "[???]"));
543
544         apr_pool_destroy(rec->pool);
545         return rv;
546     }
547
548     *data_ptr = rec;
549
550     return APR_SUCCESS;
551 }
552
553 #if APR_HAS_THREADS
554 static apr_status_t dbd_destroy(void *data)
555 {
556     dbd_group_t *group = data;
557
558     group->destroyed = 1;
559
560     return APR_SUCCESS;
561 }
562
563 static apr_status_t dbd_setup(server_rec *s, dbd_group_t *group)
564 {
565     dbd_cfg_t *cfg = group->cfg;
566     apr_status_t rv;
567
568     /* 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      */
586     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     if (rv != APR_SUCCESS) {
592         ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
593                      "DBD: failed to initialise");
594         return rv;
595     }
596
597     apr_pool_cleanup_register(group->pool, group, dbd_destroy,
598                               apr_pool_cleanup_null);
599
600     return APR_SUCCESS;
601 }
602 #endif
603
604 static apr_status_t dbd_setup_init(apr_pool_t *pool, server_rec *s)
605 {
606     dbd_group_t *group;
607     apr_status_t rv = APR_SUCCESS;
608
609     for (group = group_list; group; group = group->next) {
610         apr_status_t rv2;
611
612         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
619 #if APR_HAS_THREADS
620         rv2 = dbd_setup(s, group);
621         if (rv2 == APR_SUCCESS) {
622             continue;
623         }
624         else if (rv == APR_SUCCESS) {
625             rv = rv2;
626         }
627
628         /* 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     }
640
641     return rv;
642 }
643
644 #if APR_HAS_THREADS
645 static apr_status_t dbd_setup_lock(server_rec *s, dbd_group_t *group)
646 {
647     apr_status_t rv = APR_SUCCESS, rv2;
648
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     if (!group->mutex) {
653         /* we already logged an error when the mutex couldn't be created */
654         return APR_EGENERAL;
655     }
656
657     rv2 = apr_thread_mutex_lock(group->mutex);
658     if (rv2 != APR_SUCCESS) {
659         ap_log_error(APLOG_MARK, APLOG_ERR, rv2, s,
660                      "DBD: Failed to acquire thread mutex");
661         return rv2;
662     }
663
664     if (!group->reslist) {
665         rv = dbd_setup(s, group);
666     }
667
668     rv2 = apr_thread_mutex_unlock(group->mutex);
669     if (rv2 != APR_SUCCESS) {
670         ap_log_error(APLOG_MARK, APLOG_ERR, rv2, s,
671                      "DBD: Failed to release thread mutex");
672         if (rv == APR_SUCCESS) {
673             rv = rv2;
674         }
675     }
676
677     return rv;
678 }
679 #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 DBD_DECLARE_NONSTD(void) ap_dbd_close(server_rec *s, ap_dbd_t *rec)
686 {
687     svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
688
689     if (!svr->cfg->persist) {
690         apr_pool_destroy(rec->pool);
691     }
692 #if APR_HAS_THREADS
693     else {
694         apr_reslist_release(svr->group->reslist, rec);
695     }
696 #endif
697 }
698
699 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
705     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                  "DBD [%s] Error: %s", svr->cfg->name, errmsg);
717     return rv;
718 }
719
720 DBD_DECLARE_NONSTD(ap_dbd_t*) ap_dbd_open(apr_pool_t *pool, server_rec *s)
721 {
722     svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
723     dbd_group_t *group = svr->group;
724     dbd_cfg_t *cfg = svr->cfg;
725     ap_dbd_t *rec = NULL;
726 #if APR_HAS_THREADS
727     apr_status_t rv;
728 #endif
729
730     /* If nothing is configured, we shouldn't be here */
731     if (cfg->name == no_dbdriver) {
732         ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "DBD: not configured");
733         return NULL;
734     }
735
736     if (!cfg->persist) {
737         /* Return a once-only connection */
738         group = apr_pcalloc(pool, sizeof(dbd_group_t));
739
740         group->cfg = cfg;
741
742         dbd_construct((void*) &rec, group, pool);
743         return rec;
744     }
745
746 #if APR_HAS_THREADS
747     if (!group->reslist) {
748         if (dbd_setup_lock(s, group) != APR_SUCCESS) {
749             return NULL;
750         }
751     }
752
753     rv = apr_reslist_acquire(group->reslist, (void*) &rec);
754     if (rv != APR_SUCCESS) {
755         ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
756                      "Failed to acquire DBD connection from pool!");
757         return NULL;
758     }
759
760     if (dbd_check(pool, s, rec) != APR_SUCCESS) {
761         apr_reslist_invalidate(group->reslist, rec);
762         return NULL;
763     }
764 #else
765     /* 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     rec = group->rec;
769     if (rec) {
770         if (dbd_check(pool, s, rec) != APR_SUCCESS) {
771             apr_pool_destroy(rec->pool);
772             rec = NULL;
773         }
774     }
775
776     /* We don't have a connection right now, so we'll open one */
777     if (!rec) {
778         dbd_construct((void*) &rec, group, group->pool);
779         group->rec = rec;
780     }
781 #endif
782
783     return rec;
784 }
785
786 #if APR_HAS_THREADS
787 typedef struct {
788     ap_dbd_t *rec;
789     apr_reslist_t *reslist;
790 } dbd_acquire_t;
791
792 static apr_status_t dbd_release(void *data)
793 {
794     dbd_acquire_t *acq = data;
795     apr_reslist_release(acq->reslist, acq->rec);
796     return APR_SUCCESS;
797 }
798
799 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
800 {
801     dbd_acquire_t *acq;
802
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     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             if (svr->cfg->persist) {
822                 acq->reslist = svr->group->reslist;
823                 apr_pool_cleanup_register(r->pool, acq, dbd_release,
824                                           apr_pool_cleanup_null);
825             }
826         }
827     }
828
829     return acq->rec;
830 }
831
832 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
833 {
834     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             if (svr->cfg->persist) {
845                 acq->reslist = svr->group->reslist;
846                 apr_pool_cleanup_register(c->pool, acq, dbd_release,
847                                           apr_pool_cleanup_null);
848             }
849         }
850     }
851
852     return acq->rec;
853 }
854 #else
855 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
856 {
857     ap_dbd_t *rec;
858
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     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         }
874     }
875
876     return rec;
877 }
878
879 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
880 {
881     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         }
888     }
889
890     return rec;
891 }
892 #endif
893
894 static void dbd_hooks(apr_pool_t *pool)
895 {
896     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     ap_hook_child_init((void*)dbd_setup_init, NULL, NULL, APR_HOOK_MIDDLE);
899
900     APR_REGISTER_OPTIONAL_FN(ap_dbd_prepare);
901     APR_REGISTER_OPTIONAL_FN(ap_dbd_open);
902     APR_REGISTER_OPTIONAL_FN(ap_dbd_close);
903     APR_REGISTER_OPTIONAL_FN(ap_dbd_acquire);
904     APR_REGISTER_OPTIONAL_FN(ap_dbd_cacquire);
905
906     apr_dbd_init(pool);
907 }
908
909 module AP_MODULE_DECLARE_DATA dbd_module = {
910     STANDARD20_MODULE_STUFF,
911     NULL,
912     NULL,
913     create_dbd_config,
914     merge_dbd_config,
915     dbd_cmds,
916     dbd_hooks
917 };
918