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
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.
17 /* Overview of what this is and does:
18 * http://www.apache.org/~niq/dbd.html
20 * http://apache.webthing.com/database/
23 #include "apr_reslist.h"
24 #include "apr_strings.h"
26 #include "apr_tables.h"
30 #define APR_WANT_MEMFUNC
31 #define APR_WANT_STRFUNC
34 #include "http_protocol.h"
35 #include "http_config.h"
37 #include "http_request.h"
40 extern module AP_MODULE_DECLARE_DATA dbd_module;
42 /************ svr cfg: manage db connection pool ****************/
47 #define EXPTIME_SET 0x8
64 typedef struct dbd_group_t dbd_group_t;
71 apr_thread_mutex_t *mutex;
72 apr_reslist_t *reslist;
84 typedef enum { cmd_name, cmd_params, cmd_persist,
85 cmd_min, cmd_keep, cmd_max, cmd_exp
88 static apr_pool_t *config_pool;
89 static dbd_group_t *group_list;
91 /* a default DBDriver value that'll generate meaningful error messages */
92 static const char *const no_dbdriver = "[DBDriver unset]";
94 /* A default nmin of >0 will help with generating meaningful
95 * startup error messages if the database is down.
97 #define DEFAULT_NMIN 1
98 #define DEFAULT_NKEEP 2
99 #define DEFAULT_NMAX 10
100 #define DEFAULT_EXPTIME 300
102 static void *create_dbd_config(apr_pool_t *pool, server_rec *s)
104 svr_cfg *svr = apr_pcalloc(pool, sizeof(svr_cfg));
105 dbd_cfg_t *cfg = svr->cfg = apr_pcalloc(pool, sizeof(dbd_cfg_t));
108 cfg->name = no_dbdriver; /* to generate meaningful error messages */
109 cfg->params = ""; /* don't risk segfault on misconfiguration */
112 cfg->nmin = DEFAULT_NMIN;
113 cfg->nkeep = DEFAULT_NKEEP;
114 cfg->nmax = DEFAULT_NMAX;
115 cfg->exptime = DEFAULT_EXPTIME;
117 cfg->queries = apr_hash_make(pool);
122 static void *merge_dbd_config(apr_pool_t *pool, void *basev, void *addv)
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));
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;
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;
139 new->queries = apr_hash_overlay(pool, add->queries, base->queries);
144 #define ISINT(val) do { \
147 for (p = val; *p; ++p) { \
148 if (!apr_isdigit(*p)) { \
149 return "Argument must be numeric!"; \
154 static const char *dbd_param(cmd_parms *cmd, void *dconf, const char *val)
156 const apr_dbd_driver_t *driver = NULL;
157 svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
159 dbd_cfg_t *cfg = svr->cfg;
161 switch ((long) cmd->info) {
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.
168 switch (apr_dbd_get_driver(cmd->pool, cfg->name, &driver)) {
170 return apr_psprintf(cmd->pool, "DBD: No driver for %s", cfg->name);
172 return apr_psprintf(cmd->pool,
174 "DBD: Can't load driver file dbd%s.nlm",
176 "DBD: Can't load driver file apr_dbd_%s.so",
179 case APR_ESYMNOTFOUND:
180 return apr_psprintf(cmd->pool,
181 "DBD: Failed to load driver apr_dbd_%s_driver",
191 cfg->nmin = atoi(val);
192 cfg->set |= NMIN_SET;
196 cfg->nkeep = atoi(val);
197 cfg->set |= NKEEP_SET;
201 cfg->nmax = atoi(val);
202 cfg->set |= NMAX_SET;
206 cfg->exptime = atoi(val);
207 cfg->set |= EXPTIME_SET;
215 static const char *dbd_param_flag(cmd_parms *cmd, void *dconf, int flag)
217 svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
220 switch ((long) cmd->info) {
222 svr->cfg->persist = flag;
229 static const char *dbd_prepare(cmd_parms *cmd, void *dconf, const char *query,
237 ap_dbd_prepare(cmd->server, query, label);
242 static const command_rec dbd_cmds[] = {
243 AP_INIT_TAKE1("DBDriver", dbd_param, (void*)cmd_name, RSRC_CONF,
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"),
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"),
267 static int dbd_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
275 DBD_DECLARE_NONSTD(void) ap_dbd_prepare(server_rec *s, const char *query,
280 svr = ap_get_module_config(s->module_config, &dbd_module);
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
286 svr = create_dbd_config(config_pool, s);
287 ap_set_module_config(s->module_config, &dbd_module, svr);
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);
296 apr_hash_set(svr->cfg->queries, label, APR_HASH_KEY_STRING, query);
300 const char *label, *query;
303 static int dbd_post_config(apr_pool_t *pconf, apr_pool_t *plog,
304 apr_pool_t *ptemp, server_rec *s)
307 apr_array_header_t *add_queries = apr_array_make(ptemp, 10,
308 sizeof(dbd_query_t));
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);
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).
321 if (cfg->name == no_dbdriver || !cfg->persist) {
325 for (group = group_list; group; group = group->next) {
326 dbd_cfg_t *group_cfg = group->cfg;
327 apr_hash_index_t *hi;
330 if (strcmp(cfg->name, group_cfg->name)
331 || strcmp(cfg->params, group_cfg->params)) {
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) {
344 add_queries->nelts = 0;
346 for (hi = hi_first; hi; hi = apr_hash_next(hi)) {
347 const char *label, *query;
348 const char *group_query;
350 apr_hash_this(hi, (void*) &label, NULL, (void*) &query);
352 group_query = apr_hash_get(group_cfg->queries, label,
353 APR_HASH_KEY_STRING);
356 dbd_query_t *add_query = apr_array_push(add_queries);
358 add_query->label = label;
359 add_query->query = query;
361 else if (strcmp(query, group_query)) {
370 for (i = 0; i < add_queries->nelts; ++i) {
371 dbd_query_t *add_query = ((dbd_query_t*) add_queries->elts)
374 apr_hash_set(group_cfg->queries, add_query->label,
375 APR_HASH_KEY_STRING, add_query->query);
384 svr->group = group = apr_pcalloc(pconf, sizeof(dbd_group_t));
388 group->next = group_list;
396 static apr_status_t dbd_prepared_init(apr_pool_t *pool, dbd_cfg_t *cfg,
399 apr_hash_index_t *hi;
400 apr_status_t rv = APR_SUCCESS;
402 rec->prepared = apr_hash_make(pool);
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;
409 apr_hash_this(hi, (void*) &label, NULL, (void*) &query);
411 if (!strcmp(query, "")) {
416 if (apr_dbd_prepare(rec->driver, pool, rec->handle, query,
421 apr_hash_set(rec->prepared, label, APR_HASH_KEY_STRING, stmt);
428 static apr_status_t dbd_close(void *data)
430 ap_dbd_t *rec = data;
432 return apr_dbd_close(rec->driver, rec->handle);
436 static apr_status_t dbd_destruct(void *data, void *params, apr_pool_t *pool)
438 dbd_group_t *group = params;
440 if (!group->destroyed) {
441 ap_dbd_t *rec = data;
443 apr_pool_destroy(rec->pool);
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.
454 static apr_status_t dbd_construct(void **data_ptr,
455 void *params, apr_pool_t *pool)
457 dbd_group_t *group = params;
458 dbd_cfg_t *cfg = group->cfg;
459 apr_pool_t *rec_pool, *prepared_pool;
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");
470 rec = apr_pcalloc(rec_pool, sizeof(ap_dbd_t));
472 rec->pool = rec_pool;
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 ...
479 rv = apr_dbd_get_driver(rec->pool, cfg->name, &rec->driver);
480 if (rv != APR_SUCCESS) {
483 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
484 "DBD: driver for %s not available", cfg->name);
487 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
488 "DBD: can't find driver for %s", cfg->name);
490 case APR_ESYMNOTFOUND:
491 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
492 "DBD: driver for %s is invalid or corrupted",
496 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
497 "DBD: mod_dbd not compatible with APR in get_driver");
501 apr_pool_destroy(rec->pool);
505 rv = apr_dbd_open(rec->driver, rec->pool, cfg->params, &rec->handle);
506 if (rv != APR_SUCCESS) {
509 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
510 "DBD: Can't connect to %s", cfg->name);
513 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
514 "DBD: mod_dbd not compatible with APR in open");
518 apr_pool_destroy(rec->pool);
522 apr_pool_cleanup_register(rec->pool, rec, dbd_close,
523 apr_pool_cleanup_null);
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
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");
533 apr_pool_destroy(rec->pool);
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 : "[???]"));
544 apr_pool_destroy(rec->pool);
554 static apr_status_t dbd_destroy(void *data)
556 dbd_group_t *group = data;
558 group->destroyed = 1;
563 static apr_status_t dbd_setup(server_rec *s, dbd_group_t *group)
565 dbd_cfg_t *cfg = group->cfg;
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.
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
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
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,
591 if (rv != APR_SUCCESS) {
592 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
593 "DBD: failed to initialise");
597 apr_pool_cleanup_register(group->pool, group, dbd_destroy,
598 apr_pool_cleanup_null);
604 static apr_status_t dbd_setup_init(apr_pool_t *pool, server_rec *s)
607 apr_status_t rv = APR_SUCCESS;
609 for (group = group_list; group; group = group->next) {
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");
620 rv2 = dbd_setup(s, group);
621 if (rv2 == APR_SUCCESS) {
624 else if (rv == APR_SUCCESS) {
628 /* we failed, so create a mutex so that subsequent competing callers
629 * to ap_dbd_open can serialize themselves while they retry
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");
645 static apr_status_t dbd_setup_lock(server_rec *s, dbd_group_t *group)
647 apr_status_t rv = APR_SUCCESS, rv2;
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
653 /* we already logged an error when the mutex couldn't be created */
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");
664 if (!group->reslist) {
665 rv = dbd_setup(s, group);
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) {
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
685 DBD_DECLARE_NONSTD(void) ap_dbd_close(server_rec *s, ap_dbd_t *rec)
687 svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
689 if (!svr->cfg->persist) {
690 apr_pool_destroy(rec->pool);
694 apr_reslist_release(svr->group->reslist, rec);
699 static apr_status_t dbd_check(apr_pool_t *pool, server_rec *s, ap_dbd_t *rec)
702 apr_status_t rv = apr_dbd_check_conn(rec->driver, pool, rec->handle);
705 if ((rv == APR_SUCCESS) || (rv == APR_ENOTIMPL)) {
709 errmsg = apr_dbd_error(rec->driver, rec->handle, rv);
711 errmsg = "(unknown)";
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);
720 DBD_DECLARE_NONSTD(ap_dbd_t*) ap_dbd_open(apr_pool_t *pool, server_rec *s)
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;
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");
737 /* Return a once-only connection */
738 group = apr_pcalloc(pool, sizeof(dbd_group_t));
742 dbd_construct((void*) &rec, group, pool);
747 if (!group->reslist) {
748 if (dbd_setup_lock(s, group) != APR_SUCCESS) {
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!");
760 if (dbd_check(pool, s, rec) != APR_SUCCESS) {
761 apr_reslist_invalidate(group->reslist, rec);
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
770 if (dbd_check(pool, s, rec) != APR_SUCCESS) {
771 apr_pool_destroy(rec->pool);
776 /* We don't have a connection right now, so we'll open one */
778 dbd_construct((void*) &rec, group, group->pool);
789 apr_reslist_t *reslist;
792 static apr_status_t dbd_release(void *data)
794 dbd_acquire_t *acq = data;
795 apr_reslist_release(acq->reslist, acq->rec);
799 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
803 while (!ap_is_initial_req(r)) {
812 acq = ap_get_module_config(r->request_config, &dbd_module);
814 acq = apr_palloc(r->pool, sizeof(dbd_acquire_t));
815 acq->rec = ap_dbd_open(r->pool, r->server);
817 svr_cfg *svr = ap_get_module_config(r->server->module_config,
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);
832 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
834 dbd_acquire_t *acq = ap_get_module_config(c->conn_config, &dbd_module);
837 acq = apr_palloc(c->pool, sizeof(dbd_acquire_t));
838 acq->rec = ap_dbd_open(c->pool, c->base_server);
840 svr_cfg *svr = ap_get_module_config(c->base_server->module_config,
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);
855 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
859 while (!ap_is_initial_req(r)) {
868 rec = ap_get_module_config(r->request_config, &dbd_module);
870 rec = ap_dbd_open(r->pool, r->server);
872 ap_set_module_config(r->request_config, &dbd_module, rec);
879 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
881 ap_dbd_t *rec = ap_get_module_config(c->conn_config, &dbd_module);
884 rec = ap_dbd_open(c->pool, c->base_server);
886 ap_set_module_config(c->conn_config, &dbd_module, rec);
894 static void dbd_hooks(apr_pool_t *pool)
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);
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);
909 module AP_MODULE_DECLARE_DATA dbd_module = {
910 STANDARD20_MODULE_STUFF,