~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to modules/database/mod_dbd.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <ctype.h>
 
24
 
 
25
#include "http_protocol.h"
 
26
#include "http_config.h"
 
27
#include "http_log.h"
 
28
#include "apr_reslist.h"
 
29
#include "apr_strings.h"
 
30
#include "apr_dbd.h"
 
31
#include "mod_dbd.h"
 
32
 
 
33
extern module AP_MODULE_DECLARE_DATA dbd_module;
 
34
 
 
35
/************ svr cfg: manage db connection pool ****************/
 
36
 
 
37
#define NMIN_SET     0x1
 
38
#define NKEEP_SET    0x2
 
39
#define NMAX_SET     0x4
 
40
#define EXPTIME_SET  0x8
 
41
 
 
42
typedef struct dbd_prepared {
 
43
    const char *label;
 
44
    const char *query;
 
45
    struct dbd_prepared *next;
 
46
} dbd_prepared;
 
47
typedef struct svr_cfg {
 
48
    const char *name;
 
49
    const char *params;
 
50
    int persist;
 
51
    dbd_prepared *prepared;
 
52
#if APR_HAS_THREADS
 
53
    apr_thread_mutex_t *mutex;
 
54
    apr_pool_t *pool;
 
55
    apr_reslist_t *dbpool;
 
56
    int nmin;
 
57
    int nkeep;
 
58
    int nmax;
 
59
    int exptime;
 
60
#else
 
61
    ap_dbd_t *conn;
 
62
#endif
 
63
    unsigned int set;
 
64
} svr_cfg;
 
65
 
 
66
typedef enum { cmd_name, cmd_params, cmd_persist,
 
67
               cmd_min, cmd_keep, cmd_max, cmd_exp
 
68
} cmd_parts;
 
69
 
 
70
static apr_hash_t *dbd_prepared_defns;
 
71
 
 
72
/* a default DBDriver value that'll generate meaningful error messages */
 
73
static const char *const no_dbdriver = "[DBDriver unset]";
 
74
 
 
75
#define ISINT(val) \
 
76
        for (p = val; *p; ++p)        \
 
77
                if (!isdigit(*p))        \
 
78
                        return "Argument must be numeric!"
 
79
static const char *dbd_param(cmd_parms *cmd, void *cfg, const char *val)
 
80
{
 
81
    const char *p;
 
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);
 
85
 
 
86
    switch ((long) cmd->info) {
 
87
    case cmd_name:
 
88
        svr->name = val;
 
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.
 
92
         */
 
93
        switch (apr_dbd_get_driver(cmd->pool, svr->name, &driver)) {
 
94
        case APR_ENOTIMPL:
 
95
            return apr_psprintf(cmd->pool, "DBD: No driver for %s", svr->name);
 
96
        case APR_EDSOOPEN:
 
97
            return apr_psprintf(cmd->pool,
 
98
                                "DBD: Can't load driver file apr_dbd_%s.so",
 
99
                                svr->name);
 
100
        case APR_ESYMNOTFOUND:
 
101
            return apr_psprintf(cmd->pool,
 
102
                                "DBD: Failed to load driver apr_dbd_%s_driver",
 
103
                                svr->name);
 
104
        }
 
105
        break;
 
106
    case cmd_params:
 
107
        svr->params = val;
 
108
        break;
 
109
#if APR_HAS_THREADS
 
110
    case cmd_min:
 
111
        ISINT(val);
 
112
        svr->nmin = atoi(val);
 
113
        svr->set |= NMIN_SET;
 
114
        break;
 
115
    case cmd_keep:
 
116
        ISINT(val);
 
117
        svr->nkeep = atoi(val);
 
118
        svr->set |= NKEEP_SET;
 
119
        break;
 
120
    case cmd_max:
 
121
        ISINT(val);
 
122
        svr->nmax = atoi(val);
 
123
        svr->set |= NMAX_SET;
 
124
        break;
 
125
    case cmd_exp:
 
126
        ISINT(val);
 
127
        svr->exptime = atoi(val);
 
128
        svr->set |= EXPTIME_SET;
 
129
        break;
 
130
#endif
 
131
    }
 
132
    return NULL;
 
133
}
 
134
static const char *dbd_param_flag(cmd_parms *cmd, void *cfg, int flag)
 
135
{
 
136
    svr_cfg *svr = (svr_cfg*) ap_get_module_config
 
137
        (cmd->server->module_config, &dbd_module);
 
138
 
 
139
    switch ((long) cmd->info) {
 
140
    case cmd_persist:
 
141
        svr->persist = flag;
 
142
        break;
 
143
    }
 
144
    return NULL;
 
145
}
 
146
DBD_DECLARE_NONSTD(void) ap_dbd_prepare(server_rec *s, const char *query,
 
147
                                        const char *label)
 
148
{
 
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,
 
155
                 prepared);
 
156
}
 
157
static const char *dbd_prepare(cmd_parms *cmd, void *cfg, const char *query,
 
158
                               const char *label)
 
159
{
 
160
    ap_dbd_prepare(cmd->server, query, label);
 
161
    return NULL;
 
162
}
 
163
static const command_rec dbd_cmds[] = {
 
164
    AP_INIT_TAKE1("DBDriver", dbd_param, (void*)cmd_name, RSRC_CONF,
 
165
                  "SQL Driver"),
 
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"),
 
172
#if APR_HAS_THREADS
 
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"),
 
183
#endif
 
184
    {NULL}
 
185
};
 
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;
 
193
#if APR_HAS_THREADS
 
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;
 
198
#endif
 
199
    cfg->set = add->set | base->set;
 
200
    cfg->prepared = (add->prepared != NULL) ? add->prepared : base->prepared;
 
201
    return (void*) cfg;
 
202
}
 
203
/* A default nmin of >0 will help with generating meaningful
 
204
 * startup error messages if the database is down.
 
205
 */
 
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)
 
211
{
 
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 */
 
215
    svr->persist = -1;
 
216
#if APR_HAS_THREADS
 
217
    svr->nmin = DEFAULT_NMIN;
 
218
    svr->nkeep = DEFAULT_NKEEP;
 
219
    svr->nmax = DEFAULT_NMAX;
 
220
    svr->exptime = DEFAULT_EXPTIME;
 
221
#endif
 
222
    return svr;
 
223
}
 
224
static apr_status_t dbd_prepared_init(apr_pool_t *pool, svr_cfg *svr,
 
225
                                      ap_dbd_t *dbd)
 
226
{
 
227
    dbd_prepared *p;
 
228
    apr_status_t ret = APR_SUCCESS;
 
229
    apr_dbd_prepared_t *stmt;
 
230
    dbd->prepared = apr_hash_make(pool);
 
231
 
 
232
    for (p = svr->prepared; p; p = p->next) {
 
233
        stmt = NULL;
 
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);
 
237
        }
 
238
        else {
 
239
            ret = APR_EGENERAL;
 
240
        }
 
241
    }
 
242
    return ret;
 
243
}
 
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.
 
248
 */
 
249
static apr_status_t dbd_construct(void **db, void *params, apr_pool_t *pool)
 
250
{
 
251
    svr_cfg *svr = (svr_cfg*) params;
 
252
    ap_dbd_t *rec = apr_pcalloc(pool, sizeof(ap_dbd_t));
 
253
    apr_status_t rv;
 
254
 
 
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");
 
260
    }
 
261
 
 
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 ...
 
266
 */
 
267
    rv = apr_dbd_get_driver(rec->pool, svr->name, &rec->driver);
 
268
    switch (rv) {
 
269
    case APR_ENOTIMPL:
 
270
        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
 
271
                      "DBD: driver for %s not available", svr->name);
 
272
        return rv;
 
273
    case APR_EDSOOPEN:
 
274
        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
 
275
                      "DBD: can't find driver for %s", svr->name);
 
276
        return rv;
 
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);
 
280
        return rv;
 
281
    default:
 
282
        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
 
283
                      "DBD: mod_dbd not compatible with apr in get_driver");
 
284
        return rv;
 
285
    case APR_SUCCESS:
 
286
        break;
 
287
    }
 
288
 
 
289
    rv = apr_dbd_open(rec->driver, rec->pool, svr->params, &rec->handle);
 
290
    switch (rv) {
 
291
    case APR_EGENERAL:
 
292
        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
 
293
                      "DBD: Can't connect to %s", svr->name);
 
294
        return rv;
 
295
    default:
 
296
        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
 
297
                      "DBD: mod_dbd not compatible with apr in open");
 
298
        return rv;
 
299
    case APR_SUCCESS:
 
300
        break;
 
301
    }
 
302
    *db = rec;
 
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 : "[???]"));
 
309
    }
 
310
    return rv;
 
311
}
 
312
static apr_status_t dbd_close(void *CONN)
 
313
{
 
314
    ap_dbd_t *conn = CONN;
 
315
    apr_status_t rv = apr_dbd_close(conn->driver, conn->handle);
 
316
    apr_pool_destroy(conn->pool);
 
317
    return rv;
 
318
}
 
319
#if APR_HAS_THREADS
 
320
static apr_status_t dbd_destruct(void *sql, void *params, apr_pool_t *pool)
 
321
{
 
322
    return dbd_close(sql);
 
323
}
 
324
 
 
325
static apr_status_t dbd_setup(apr_pool_t *pool, svr_cfg *svr)
 
326
{
 
327
    apr_status_t rv;
 
328
 
 
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
 
334
     */
 
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");
 
339
        return rv;
 
340
    }
 
341
 
 
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);
 
349
    }
 
350
    else {
 
351
        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, svr->pool,
 
352
                      "DBD: failed to initialise");
 
353
        apr_pool_destroy(svr->pool);
 
354
        svr->pool = NULL;
 
355
    }
 
356
 
 
357
    return rv;
 
358
}
 
359
static apr_status_t dbd_setup_init(apr_pool_t *pool, server_rec *s)
 
360
{
 
361
    svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
 
362
    apr_status_t rv;
 
363
 
 
364
    if (!svr->persist) {
 
365
        return APR_SUCCESS;
 
366
    }
 
367
 
 
368
    rv = dbd_setup(pool, svr);
 
369
    if (rv == APR_SUCCESS) {
 
370
        return rv;
 
371
    }
 
372
 
 
373
    /* we failed, so create a mutex so that subsequent competing callers
 
374
     * to ap_dbd_open can serialize themselves while they retry
 
375
     */
 
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");
 
380
    }
 
381
    return rv;
 
382
}
 
383
static apr_status_t dbd_setup_lock(apr_pool_t *pool, server_rec *s)
 
384
{
 
385
    svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
 
386
    apr_status_t rv, rv2 = APR_SUCCESS;
 
387
 
 
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
 
390
     */
 
391
    if (!svr->mutex) {
 
392
        /* we already logged an error when the mutex couldn't be created */
 
393
        return APR_EGENERAL;
 
394
    }
 
395
 
 
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");
 
400
        return rv;
 
401
    }
 
402
 
 
403
    if (!svr->dbpool) {
 
404
        rv2 = dbd_setup(s->process->pool, svr);
 
405
    }
 
406
 
 
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) {
 
412
            rv2 = rv;
 
413
        }
 
414
    }
 
415
    return rv2;
 
416
}
 
417
#endif
 
418
 
 
419
 
 
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
 
423
*/
 
424
DBD_DECLARE_NONSTD(void) ap_dbd_close(server_rec *s, ap_dbd_t *sql)
 
425
{
 
426
    svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
 
427
    if (!svr->persist) {
 
428
        dbd_close((void*) sql);
 
429
    }
 
430
#if APR_HAS_THREADS
 
431
    else {
 
432
        apr_reslist_release(svr->dbpool, sql);
 
433
    }
 
434
#endif
 
435
}
 
436
#define arec ((ap_dbd_t*)rec)
 
437
#if APR_HAS_THREADS
 
438
DBD_DECLARE_NONSTD(ap_dbd_t*) ap_dbd_open(apr_pool_t *pool, server_rec *s)
 
439
{
 
440
    void *rec = NULL;
 
441
    svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
 
442
    apr_status_t rv = APR_SUCCESS;
 
443
    const char *errmsg;
 
444
 
 
445
    if (!svr->persist) {
 
446
        /* Return a once-only connection */
 
447
        rv = dbd_construct(&rec, svr, s->process->pool);
 
448
        return (rv == APR_SUCCESS) ? arec : NULL;
 
449
    }
 
450
 
 
451
    if (!svr->dbpool) {
 
452
        if (dbd_setup_lock(pool, s) != APR_SUCCESS) {
 
453
            return NULL;
 
454
        }
 
455
    }
 
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!");
 
460
        return NULL;
 
461
    }
 
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);
 
465
        if (!errmsg) {
 
466
            errmsg = "(unknown)";
 
467
        }
 
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);
 
471
        return NULL;
 
472
    }
 
473
    return arec;
 
474
}
 
475
#else
 
476
DBD_DECLARE_NONSTD(ap_dbd_t*) ap_dbd_open(apr_pool_t *pool, server_rec *s)
 
477
{
 
478
    apr_status_t rv = APR_SUCCESS;
 
479
    const char *errmsg;
 
480
    void *rec = NULL;
 
481
    svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
 
482
 
 
483
    if (!svr->persist) {
 
484
        /* Return a once-only connection */
 
485
        rv = dbd_construct(&rec, svr, s->process->pool);
 
486
        return (rv == APR_SUCCESS) ? arec : NULL;
 
487
    }
 
488
 
 
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 */
 
491
    if (svr->conn) {
 
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);
 
495
            if (!errmsg) {
 
496
                errmsg = "(unknown)";
 
497
            }
 
498
            ap_log_perror(APLOG_MARK, APLOG_ERR, rv, pool,
 
499
                          "DBD[%s] Error: %s", svr->name, errmsg);
 
500
            svr->conn = NULL;
 
501
        }
 
502
    }
 
503
/* We don't have a connection right now, so we'll open one */
 
504
    if (!svr->conn) {
 
505
        if (dbd_construct(&rec, svr, s->process->pool) == APR_SUCCESS) {
 
506
            svr->conn = arec ;
 
507
            apr_pool_cleanup_register(s->process->pool, svr->conn,
 
508
                                      dbd_close, apr_pool_cleanup_null);
 
509
        }
 
510
    }
 
511
    return svr->conn;
 
512
}
 
513
#endif
 
514
#if APR_HAS_THREADS
 
515
typedef struct {
 
516
    ap_dbd_t *conn;
 
517
    apr_reslist_t *dbpool;
 
518
} dbd_pool_rec;
 
519
static apr_status_t dbd_release(void *REQ)
 
520
{
 
521
    dbd_pool_rec *req = REQ;
 
522
    apr_reslist_release(req->dbpool, req->conn);
 
523
    return APR_SUCCESS;
 
524
}
 
525
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
 
526
{
 
527
    svr_cfg *svr;
 
528
    dbd_pool_rec *req = ap_get_module_config(r->request_config, &dbd_module);
 
529
    if (!req) {
 
530
        req = apr_palloc(r->pool, sizeof(dbd_pool_rec));
 
531
        req->conn = ap_dbd_open(r->pool, r->server);
 
532
        if (req->conn) {
 
533
            svr = ap_get_module_config(r->server->module_config, &dbd_module);
 
534
            ap_set_module_config(r->request_config, &dbd_module, req);
 
535
            if (svr->persist) {
 
536
                req->dbpool = svr->dbpool;
 
537
                apr_pool_cleanup_register(r->pool, req, dbd_release,
 
538
                                          apr_pool_cleanup_null);
 
539
            }
 
540
            else {
 
541
                apr_pool_cleanup_register(r->pool, req->conn, dbd_close,
 
542
                                          apr_pool_cleanup_null);
 
543
            }
 
544
        }
 
545
    }
 
546
    return req->conn;
 
547
}
 
548
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
 
549
{
 
550
    svr_cfg *svr;
 
551
    dbd_pool_rec *req = ap_get_module_config(c->conn_config, &dbd_module);
 
552
    if (!req) {
 
553
        req = apr_palloc(c->pool, sizeof(dbd_pool_rec));
 
554
        req->conn = ap_dbd_open(c->pool, c->base_server);
 
555
        if (req->conn) {
 
556
            svr = ap_get_module_config(c->base_server->module_config, &dbd_module);
 
557
            ap_set_module_config(c->conn_config, &dbd_module, req);
 
558
            if (svr->persist) {
 
559
                req->dbpool = svr->dbpool;
 
560
                apr_pool_cleanup_register(c->pool, req, dbd_release,
 
561
                                          apr_pool_cleanup_null);
 
562
            }
 
563
            else {
 
564
                apr_pool_cleanup_register(c->pool, req->conn, dbd_close,
 
565
                                          apr_pool_cleanup_null);
 
566
            }
 
567
        }
 
568
    }
 
569
    return req->conn;
 
570
}
 
571
#else
 
572
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
 
573
{
 
574
    svr_cfg *svr;
 
575
    ap_dbd_t *ret = ap_get_module_config(r->request_config, &dbd_module);
 
576
    if (!ret) {
 
577
        svr = ap_get_module_config(r->server->module_config, &dbd_module);
 
578
        ret = ap_dbd_open(r->pool, r->server);
 
579
        if (ret) {
 
580
            ap_set_module_config(r->request_config, &dbd_module, ret);
 
581
            if (!svr->persist) {
 
582
                apr_pool_cleanup_register(r->pool, svr->conn, dbd_close,
 
583
                                          apr_pool_cleanup_null);
 
584
            }
 
585
            /* if persist then dbd_open registered cleanup on proc pool */
 
586
        }
 
587
    }
 
588
    return ret;
 
589
}
 
590
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
 
591
{
 
592
    svr_cfg *svr;
 
593
    ap_dbd_t *ret = ap_get_module_config(c->conn_config, &dbd_module);
 
594
    if (!ret) {
 
595
        svr = ap_get_module_config(c->base_server->module_config, &dbd_module);
 
596
        ret = ap_dbd_open(c->pool, c->base_server);
 
597
        if (ret) {
 
598
            ap_set_module_config(c->conn_config, &dbd_module, ret);
 
599
            if (!svr->persist) {
 
600
                apr_pool_cleanup_register(c->pool, svr->conn, dbd_close,
 
601
                                          apr_pool_cleanup_null);
 
602
            }
 
603
            /* if persist then dbd_open registered cleanup on proc pool */
 
604
        }
 
605
    }
 
606
    return ret;
 
607
}
 
608
#endif
 
609
 
 
610
static int dbd_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
 
611
{
 
612
   dbd_prepared_defns = apr_hash_make(ptemp);
 
613
   return OK;
 
614
}
 
615
static int dbd_post_config(apr_pool_t *pconf, apr_pool_t *plog,
 
616
                           apr_pool_t *ptemp, server_rec *s)
 
617
{
 
618
    svr_cfg *svr;
 
619
    server_rec *sp;
 
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);
 
624
    }
 
625
    return OK;
 
626
}
 
627
static void dbd_hooks(apr_pool_t *pool)
 
628
{
 
629
#if APR_HAS_THREADS
 
630
    ap_hook_child_init((void*)dbd_setup_init, NULL, NULL, APR_HOOK_MIDDLE);
 
631
#endif
 
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);
 
637
    apr_dbd_init(pool);
 
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);
 
640
}
 
641
 
 
642
module AP_MODULE_DECLARE_DATA dbd_module = {
 
643
    STANDARD20_MODULE_STUFF,
 
644
    NULL,
 
645
    NULL,
 
646
    dbd_cfg,
 
647
    dbd_merge,
 
648
    dbd_cmds,
 
649
    dbd_hooks
 
650
};