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/
25
#include "http_protocol.h"
26
#include "http_config.h"
28
#include "apr_reslist.h"
29
#include "apr_strings.h"
33
extern module AP_MODULE_DECLARE_DATA dbd_module;
35
/************ svr cfg: manage db connection pool ****************/
40
#define EXPTIME_SET 0x8
42
typedef struct dbd_prepared {
45
struct dbd_prepared *next;
47
typedef struct svr_cfg {
51
dbd_prepared *prepared;
53
apr_thread_mutex_t *mutex;
55
apr_reslist_t *dbpool;
66
typedef enum { cmd_name, cmd_params, cmd_persist,
67
cmd_min, cmd_keep, cmd_max, cmd_exp
70
static apr_hash_t *dbd_prepared_defns;
72
/* a default DBDriver value that'll generate meaningful error messages */
73
static const char *const no_dbdriver = "[DBDriver unset]";
76
for (p = val; *p; ++p) \
78
return "Argument must be numeric!"
79
static const char *dbd_param(cmd_parms *cmd, void *cfg, const char *val)
82
const apr_dbd_driver_t *driver = NULL;
83
svr_cfg *svr = (svr_cfg*) ap_get_module_config
84
(cmd->server->module_config, &dbd_module);
86
switch ((long) cmd->info) {
89
/* loading the driver involves once-only dlloading that is
90
* best done at server startup. This also guarantees that
91
* we won't return an error later.
93
switch (apr_dbd_get_driver(cmd->pool, svr->name, &driver)) {
95
return apr_psprintf(cmd->pool, "DBD: No driver for %s", svr->name);
97
return apr_psprintf(cmd->pool,
98
"DBD: Can't load driver file apr_dbd_%s.so",
100
case APR_ESYMNOTFOUND:
101
return apr_psprintf(cmd->pool,
102
"DBD: Failed to load driver apr_dbd_%s_driver",
112
svr->nmin = atoi(val);
113
svr->set |= NMIN_SET;
117
svr->nkeep = atoi(val);
118
svr->set |= NKEEP_SET;
122
svr->nmax = atoi(val);
123
svr->set |= NMAX_SET;
127
svr->exptime = atoi(val);
128
svr->set |= EXPTIME_SET;
134
static const char *dbd_param_flag(cmd_parms *cmd, void *cfg, int flag)
136
svr_cfg *svr = (svr_cfg*) ap_get_module_config
137
(cmd->server->module_config, &dbd_module);
139
switch ((long) cmd->info) {
146
DBD_DECLARE_NONSTD(void) ap_dbd_prepare(server_rec *s, const char *query,
149
dbd_prepared *prepared = apr_pcalloc(s->process->pool, sizeof(dbd_prepared));
150
prepared->label = label;
151
prepared->query = query;
152
prepared->next = apr_hash_get(dbd_prepared_defns, s->server_hostname,
153
APR_HASH_KEY_STRING);
154
apr_hash_set(dbd_prepared_defns, s->server_hostname, APR_HASH_KEY_STRING,
157
static const char *dbd_prepare(cmd_parms *cmd, void *cfg, const char *query,
160
ap_dbd_prepare(cmd->server, query, label);
163
static const command_rec dbd_cmds[] = {
164
AP_INIT_TAKE1("DBDriver", dbd_param, (void*)cmd_name, RSRC_CONF,
166
AP_INIT_TAKE1("DBDParams", dbd_param, (void*)cmd_params, RSRC_CONF,
167
"SQL Driver Params"),
168
AP_INIT_FLAG("DBDPersist", dbd_param_flag, (void*)cmd_persist, RSRC_CONF,
169
"Use persistent connection/pool"),
170
AP_INIT_TAKE2("DBDPrepareSQL", dbd_prepare, NULL, RSRC_CONF,
171
"Prepared SQL statement, label"),
173
AP_INIT_TAKE1("DBDMin", dbd_param, (void*)cmd_min, RSRC_CONF,
174
"Minimum number of connections"),
175
/* XXX: note that mod_proxy calls this "smax" */
176
AP_INIT_TAKE1("DBDKeep", dbd_param, (void*)cmd_keep, RSRC_CONF,
177
"Maximum number of sustained connections"),
178
AP_INIT_TAKE1("DBDMax", dbd_param, (void*)cmd_max, RSRC_CONF,
179
"Maximum number of connections"),
180
/* XXX: note that mod_proxy calls this "ttl" (time to live) */
181
AP_INIT_TAKE1("DBDExptime", dbd_param, (void*)cmd_exp, RSRC_CONF,
182
"Keepalive time for idle connections"),
186
static void *dbd_merge(apr_pool_t *pool, void *BASE, void *ADD) {
187
svr_cfg *base = (svr_cfg*) BASE;
188
svr_cfg *add = (svr_cfg*) ADD;
189
svr_cfg *cfg = apr_pcalloc(pool, sizeof(svr_cfg));
190
cfg->name = (add->name != no_dbdriver) ? add->name : base->name;
191
cfg->params = strcmp(add->params, "") ? add->params : base->params;
192
cfg->persist = (add->persist == -1) ? base->persist : add->persist;
194
cfg->nmin = (add->set&NMIN_SET) ? add->nmin : base->nmin;
195
cfg->nkeep = (add->set&NKEEP_SET) ? add->nkeep : base->nkeep;
196
cfg->nmax = (add->set&NMAX_SET) ? add->nmax : base->nmax;
197
cfg->exptime = (add->set&EXPTIME_SET) ? add->exptime : base->exptime;
199
cfg->set = add->set | base->set;
200
cfg->prepared = (add->prepared != NULL) ? add->prepared : base->prepared;
203
/* A default nmin of >0 will help with generating meaningful
204
* startup error messages if the database is down.
206
#define DEFAULT_NMIN 1
207
#define DEFAULT_NKEEP 2
208
#define DEFAULT_NMAX 10
209
#define DEFAULT_EXPTIME 300
210
static void *dbd_cfg(apr_pool_t *p, server_rec *x)
212
svr_cfg *svr = (svr_cfg*) apr_pcalloc(p, sizeof(svr_cfg));
213
svr->params = ""; /* don't risk segfault on misconfiguration */
214
svr->name = no_dbdriver; /* to generate meaningful error messages */
217
svr->nmin = DEFAULT_NMIN;
218
svr->nkeep = DEFAULT_NKEEP;
219
svr->nmax = DEFAULT_NMAX;
220
svr->exptime = DEFAULT_EXPTIME;
224
static apr_status_t dbd_prepared_init(apr_pool_t *pool, svr_cfg *svr,
228
apr_status_t ret = APR_SUCCESS;
229
apr_dbd_prepared_t *stmt;
230
dbd->prepared = apr_hash_make(pool);
232
for (p = svr->prepared; p; p = p->next) {
234
if (apr_dbd_prepare(dbd->driver, pool, dbd->handle, p->query,
235
p->label, &stmt) == 0) {
236
apr_hash_set(dbd->prepared, p->label, APR_HASH_KEY_STRING, stmt);
244
/************ svr cfg: manage db connection pool ****************/
245
/* an apr_reslist_constructor for SQL connections
246
* Also use this for opening in non-reslist modes, since it gives
247
* us all the error-handling in one place.
249
static apr_status_t dbd_construct(void **db, void *params, apr_pool_t *pool)
251
svr_cfg *svr = (svr_cfg*) params;
252
ap_dbd_t *rec = apr_pcalloc(pool, sizeof(ap_dbd_t));
255
/* this pool is mostly so dbd_close can destroy the prepared stmts */
256
rv = apr_pool_create(&rec->pool, pool);
257
if (rv != APR_SUCCESS) {
258
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
259
"DBD: Failed to create memory pool");
262
/* The driver is loaded at config time now, so this just checks a hash.
263
* If that changes, the driver DSO could be registered to unload against
264
* our pool, which is probably not what we want. Error checking isn't
265
* necessary now, but in case that changes in the future ...
267
rv = apr_dbd_get_driver(rec->pool, svr->name, &rec->driver);
270
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
271
"DBD: driver for %s not available", svr->name);
274
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
275
"DBD: can't find driver for %s", svr->name);
277
case APR_ESYMNOTFOUND:
278
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
279
"DBD: driver for %s is invalid or corrupted", svr->name);
282
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
283
"DBD: mod_dbd not compatible with apr in get_driver");
289
rv = apr_dbd_open(rec->driver, rec->pool, svr->params, &rec->handle);
292
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
293
"DBD: Can't connect to %s", svr->name);
296
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
297
"DBD: mod_dbd not compatible with apr in open");
303
rv = dbd_prepared_init(rec->pool, svr, rec);
304
if (rv != APR_SUCCESS) {
305
const char *errmsg = apr_dbd_error(rec->driver, rec->handle, rv);
306
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
307
"DBD: failed to initialise prepared SQL statements: %s",
308
(errmsg ? errmsg : "[???]"));
312
static apr_status_t dbd_close(void *CONN)
314
ap_dbd_t *conn = CONN;
315
apr_status_t rv = apr_dbd_close(conn->driver, conn->handle);
316
apr_pool_destroy(conn->pool);
320
static apr_status_t dbd_destruct(void *sql, void *params, apr_pool_t *pool)
322
return dbd_close(sql);
325
static apr_status_t dbd_setup(apr_pool_t *pool, svr_cfg *svr)
329
/* create a pool just for the reslist from a process-lifetime pool;
330
* that pool (s->process->pool in the dbd_setup_lock case,
331
* whatever was passed to ap_run_child_init in the dbd_setup_init case)
332
* will be shared with other threads doing other non-mod_dbd things
333
* so we can't use it for the reslist directly
335
rv = apr_pool_create(&svr->pool, pool);
336
if (rv != APR_SUCCESS) {
337
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
338
"DBD: Failed to create reslist memory pool");
342
rv = apr_reslist_create(&svr->dbpool, svr->nmin, svr->nkeep, svr->nmax,
343
apr_time_from_sec(svr->exptime),
344
dbd_construct, dbd_destruct, svr, svr->pool);
345
if (rv == APR_SUCCESS) {
346
apr_pool_cleanup_register(svr->pool, svr->dbpool,
347
(void*)apr_reslist_destroy,
348
apr_pool_cleanup_null);
351
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, svr->pool,
352
"DBD: failed to initialise");
353
apr_pool_destroy(svr->pool);
359
static apr_status_t dbd_setup_init(apr_pool_t *pool, server_rec *s)
361
svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
368
rv = dbd_setup(pool, svr);
369
if (rv == APR_SUCCESS) {
373
/* we failed, so create a mutex so that subsequent competing callers
374
* to ap_dbd_open can serialize themselves while they retry
376
rv = apr_thread_mutex_create(&svr->mutex, APR_THREAD_MUTEX_DEFAULT, pool);
377
if (rv != APR_SUCCESS) {
378
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
379
"DBD: Failed to create thread mutex");
383
static apr_status_t dbd_setup_lock(apr_pool_t *pool, server_rec *s)
385
svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
386
apr_status_t rv, rv2 = APR_SUCCESS;
388
/* several threads could be here at the same time, all trying to
389
* initialize the reslist because dbd_setup_init failed to do so
392
/* we already logged an error when the mutex couldn't be created */
396
rv = apr_thread_mutex_lock(svr->mutex);
397
if (rv != APR_SUCCESS) {
398
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
399
"DBD: Failed to acquire thread mutex");
404
rv2 = dbd_setup(s->process->pool, svr);
407
rv = apr_thread_mutex_unlock(svr->mutex);
408
if (rv != APR_SUCCESS) {
409
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
410
"DBD: Failed to release thread mutex");
411
if (rv2 == APR_SUCCESS) {
420
/* Functions we export for modules to use:
421
- open acquires a connection from the pool (opens one if necessary)
422
- close releases it back in to the pool
424
DBD_DECLARE_NONSTD(void) ap_dbd_close(server_rec *s, ap_dbd_t *sql)
426
svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
428
dbd_close((void*) sql);
432
apr_reslist_release(svr->dbpool, sql);
436
#define arec ((ap_dbd_t*)rec)
438
DBD_DECLARE_NONSTD(ap_dbd_t*) ap_dbd_open(apr_pool_t *pool, server_rec *s)
441
svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
442
apr_status_t rv = APR_SUCCESS;
446
/* Return a once-only connection */
447
rv = dbd_construct(&rec, svr, s->process->pool);
448
return (rv == APR_SUCCESS) ? arec : NULL;
452
if (dbd_setup_lock(pool, s) != APR_SUCCESS) {
456
rv = apr_reslist_acquire(svr->dbpool, &rec);
457
if (rv != APR_SUCCESS) {
458
ap_log_perror(APLOG_MARK, APLOG_ERR, rv, pool,
459
"Failed to acquire DBD connection from pool!");
462
rv = apr_dbd_check_conn(arec->driver, pool, arec->handle);
463
if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
464
errmsg = apr_dbd_error(arec->driver, arec->handle, rv);
466
errmsg = "(unknown)";
468
ap_log_perror(APLOG_MARK, APLOG_ERR, rv, pool,
469
"DBD[%s] Error: %s", svr->name, errmsg );
470
apr_reslist_invalidate(svr->dbpool, rec);
476
DBD_DECLARE_NONSTD(ap_dbd_t*) ap_dbd_open(apr_pool_t *pool, server_rec *s)
478
apr_status_t rv = APR_SUCCESS;
481
svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
484
/* Return a once-only connection */
485
rv = dbd_construct(&rec, svr, s->process->pool);
486
return (rv == APR_SUCCESS) ? arec : NULL;
489
/* since we're in nothread-land, we can mess with svr->conn with impunity */
490
/* If we have a persistent connection and it's good, we'll use it */
492
rv = apr_dbd_check_conn(svr->conn->driver, pool, svr->conn->handle);
493
if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
494
errmsg = apr_dbd_error(arec->driver, arec->handle, rv);
496
errmsg = "(unknown)";
498
ap_log_perror(APLOG_MARK, APLOG_ERR, rv, pool,
499
"DBD[%s] Error: %s", svr->name, errmsg);
503
/* We don't have a connection right now, so we'll open one */
505
if (dbd_construct(&rec, svr, s->process->pool) == APR_SUCCESS) {
507
apr_pool_cleanup_register(s->process->pool, svr->conn,
508
dbd_close, apr_pool_cleanup_null);
517
apr_reslist_t *dbpool;
519
static apr_status_t dbd_release(void *REQ)
521
dbd_pool_rec *req = REQ;
522
apr_reslist_release(req->dbpool, req->conn);
525
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
528
dbd_pool_rec *req = ap_get_module_config(r->request_config, &dbd_module);
530
req = apr_palloc(r->pool, sizeof(dbd_pool_rec));
531
req->conn = ap_dbd_open(r->pool, r->server);
533
svr = ap_get_module_config(r->server->module_config, &dbd_module);
534
ap_set_module_config(r->request_config, &dbd_module, req);
536
req->dbpool = svr->dbpool;
537
apr_pool_cleanup_register(r->pool, req, dbd_release,
538
apr_pool_cleanup_null);
541
apr_pool_cleanup_register(r->pool, req->conn, dbd_close,
542
apr_pool_cleanup_null);
548
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
551
dbd_pool_rec *req = ap_get_module_config(c->conn_config, &dbd_module);
553
req = apr_palloc(c->pool, sizeof(dbd_pool_rec));
554
req->conn = ap_dbd_open(c->pool, c->base_server);
556
svr = ap_get_module_config(c->base_server->module_config, &dbd_module);
557
ap_set_module_config(c->conn_config, &dbd_module, req);
559
req->dbpool = svr->dbpool;
560
apr_pool_cleanup_register(c->pool, req, dbd_release,
561
apr_pool_cleanup_null);
564
apr_pool_cleanup_register(c->pool, req->conn, dbd_close,
565
apr_pool_cleanup_null);
572
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
575
ap_dbd_t *ret = ap_get_module_config(r->request_config, &dbd_module);
577
svr = ap_get_module_config(r->server->module_config, &dbd_module);
578
ret = ap_dbd_open(r->pool, r->server);
580
ap_set_module_config(r->request_config, &dbd_module, ret);
582
apr_pool_cleanup_register(r->pool, svr->conn, dbd_close,
583
apr_pool_cleanup_null);
585
/* if persist then dbd_open registered cleanup on proc pool */
590
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
593
ap_dbd_t *ret = ap_get_module_config(c->conn_config, &dbd_module);
595
svr = ap_get_module_config(c->base_server->module_config, &dbd_module);
596
ret = ap_dbd_open(c->pool, c->base_server);
598
ap_set_module_config(c->conn_config, &dbd_module, ret);
600
apr_pool_cleanup_register(c->pool, svr->conn, dbd_close,
601
apr_pool_cleanup_null);
603
/* if persist then dbd_open registered cleanup on proc pool */
610
static int dbd_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
612
dbd_prepared_defns = apr_hash_make(ptemp);
615
static int dbd_post_config(apr_pool_t *pconf, apr_pool_t *plog,
616
apr_pool_t *ptemp, server_rec *s)
620
for (sp = s; sp; sp = sp->next) {
621
svr = ap_get_module_config(sp->module_config, &dbd_module);
622
svr->prepared = apr_hash_get(dbd_prepared_defns, sp->server_hostname,
623
APR_HASH_KEY_STRING);
627
static void dbd_hooks(apr_pool_t *pool)
630
ap_hook_child_init((void*)dbd_setup_init, NULL, NULL, APR_HOOK_MIDDLE);
632
APR_REGISTER_OPTIONAL_FN(ap_dbd_open);
633
APR_REGISTER_OPTIONAL_FN(ap_dbd_close);
634
APR_REGISTER_OPTIONAL_FN(ap_dbd_acquire);
635
APR_REGISTER_OPTIONAL_FN(ap_dbd_cacquire);
636
APR_REGISTER_OPTIONAL_FN(ap_dbd_prepare);
638
ap_hook_pre_config(dbd_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
639
ap_hook_post_config(dbd_post_config, NULL, NULL, APR_HOOK_MIDDLE);
642
module AP_MODULE_DECLARE_DATA dbd_module = {
643
STANDARD20_MODULE_STUFF,