mod_dbd.c revision 1ec08bba27851a4ffca9e6eb88d5c01b5fb6affb
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
2d2eda71267231c2526be701fe655db125852c1ffielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * http://www.apache.org/licenses/LICENSE-2.0
2d2eda71267231c2526be701fe655db125852c1ffielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * limitations under the License.
2d2eda71267231c2526be701fe655db125852c1ffielding */
2d2eda71267231c2526be701fe655db125852c1ffielding
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb/* Overview of what this is and does:
3ef7956b993aff4b4882c643910fb688a0e707e3ben * http://www.apache.org/~niq/dbd.html
3ef7956b993aff4b4882c643910fb688a0e707e3ben * or
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * http://apache.webthing.com/database/
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh */
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding#include "apr_reslist.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "apr_strings.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "apr_hash.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "apr_tables.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "apr_lib.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "apr_dbd.h"
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding#define APR_WANT_MEMFUNC
2d2eda71267231c2526be701fe655db125852c1ffielding#define APR_WANT_STRFUNC
2d2eda71267231c2526be701fe655db125852c1ffielding#include "apr_want.h"
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding#include "http_protocol.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "http_config.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "http_log.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "http_request.h"
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#include "mod_dbd.h"
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingextern module AP_MODULE_DECLARE_DATA dbd_module;
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh/************ svr cfg: manage db connection pool ****************/
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#define NMIN_SET 0x1
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#define NKEEP_SET 0x2
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#define NMAX_SET 0x4
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#define EXPTIME_SET 0x8
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianhtypedef struct {
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh server_rec *server;
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh const char *name;
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh const char *params;
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh int persist;
6d7983331bf3e1e7dd2ee7c6e5f531d357e7e1aawrowe#if APR_HAS_THREADS
0ec08ecdcb42129d147888bda504787b164da39cerikabele int nmin;
6d7983331bf3e1e7dd2ee7c6e5f531d357e7e1aawrowe int nkeep;
6d7983331bf3e1e7dd2ee7c6e5f531d357e7e1aawrowe int nmax;
6d7983331bf3e1e7dd2ee7c6e5f531d357e7e1aawrowe int exptime;
6d7983331bf3e1e7dd2ee7c6e5f531d357e7e1aawrowe int set;
6d7983331bf3e1e7dd2ee7c6e5f531d357e7e1aawrowe#endif
6d7983331bf3e1e7dd2ee7c6e5f531d357e7e1aawrowe apr_hash_t *queries;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb} dbd_cfg_t;
0ec08ecdcb42129d147888bda504787b164da39cerikabele
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbbtypedef struct dbd_group_t dbd_group_t;
e8eee56a2ea20f673e5942fb58e1426a7561c7acstas
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbbstruct dbd_group_t {
59d7dc153347a9f606c5712f0fae7b65e96682d9rbb dbd_cfg_t *cfg;
0fbbb7938680ecde631ae30271f43a6f6240aa8cwrowe dbd_group_t *next;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb apr_pool_t *pool;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb#if APR_HAS_THREADS
e8eee56a2ea20f673e5942fb58e1426a7561c7acstas apr_thread_mutex_t *mutex;
59d7dc153347a9f606c5712f0fae7b65e96682d9rbb apr_reslist_t *reslist;
59d7dc153347a9f606c5712f0fae7b65e96682d9rbb int destroyed;
59d7dc153347a9f606c5712f0fae7b65e96682d9rbb#else
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb ap_dbd_t *rec;
0ec08ecdcb42129d147888bda504787b164da39cerikabele#endif
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb};
e8eee56a2ea20f673e5942fb58e1426a7561c7acstas
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbbtypedef struct {
59d7dc153347a9f606c5712f0fae7b65e96682d9rbb dbd_cfg_t *cfg;
0fbbb7938680ecde631ae30271f43a6f6240aa8cwrowe dbd_group_t *group;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb} svr_cfg;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
3d96ee83babeec32482c9082c9426340cee8c44dwrowetypedef enum { cmd_name, cmd_params, cmd_persist,
59d7dc153347a9f606c5712f0fae7b65e96682d9rbb cmd_min, cmd_keep, cmd_max, cmd_exp
59d7dc153347a9f606c5712f0fae7b65e96682d9rbb} cmd_parts;
44da058d96ca5577a8b9fe7d6b89d79fe9a0a849wrowe
0ec08ecdcb42129d147888bda504787b164da39cerikabelestatic apr_pool_t *config_pool;
44da058d96ca5577a8b9fe7d6b89d79fe9a0a849wrowestatic dbd_group_t *group_list;
44da058d96ca5577a8b9fe7d6b89d79fe9a0a849wrowe
44da058d96ca5577a8b9fe7d6b89d79fe9a0a849wrowe/* a default DBDriver value that'll generate meaningful error messages */
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowestatic const char *const no_dbdriver = "[DBDriver unset]";
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe/* A default nmin of >0 will help with generating meaningful
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe * startup error messages if the database is down.
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe */
44da058d96ca5577a8b9fe7d6b89d79fe9a0a849wrowe#define DEFAULT_NMIN 1
0fbbb7938680ecde631ae30271f43a6f6240aa8cwrowe#define DEFAULT_NKEEP 2
44da058d96ca5577a8b9fe7d6b89d79fe9a0a849wrowe#define DEFAULT_NMAX 10
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#define DEFAULT_EXPTIME 300
44da058d96ca5577a8b9fe7d6b89d79fe9a0a849wrowe
44da058d96ca5577a8b9fe7d6b89d79fe9a0a849wrowestatic void *create_dbd_config(apr_pool_t *pool, server_rec *s)
44da058d96ca5577a8b9fe7d6b89d79fe9a0a849wrowe{
44da058d96ca5577a8b9fe7d6b89d79fe9a0a849wrowe svr_cfg *svr = apr_pcalloc(pool, sizeof(svr_cfg));
efa1a34b0a7785fc72863eff175b0cfc1ecb0e38wrowe dbd_cfg_t *cfg = svr->cfg = apr_pcalloc(pool, sizeof(dbd_cfg_t));
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe cfg->server = s;
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe cfg->name = no_dbdriver; /* to generate meaningful error messages */
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb cfg->params = ""; /* don't risk segfault on misconfiguration */
0ec08ecdcb42129d147888bda504787b164da39cerikabele cfg->persist = -1;
0ec08ecdcb42129d147888bda504787b164da39cerikabele#if APR_HAS_THREADS
0ec08ecdcb42129d147888bda504787b164da39cerikabele cfg->nmin = DEFAULT_NMIN;
e8eee56a2ea20f673e5942fb58e1426a7561c7acstas cfg->nkeep = DEFAULT_NKEEP;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb cfg->nmax = DEFAULT_NMAX;
59d7dc153347a9f606c5712f0fae7b65e96682d9rbb cfg->exptime = DEFAULT_EXPTIME;
59d7dc153347a9f606c5712f0fae7b65e96682d9rbb#endif
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb cfg->queries = apr_hash_make(pool);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
3d96ee83babeec32482c9082c9426340cee8c44dwrowe return svr;
e8eee56a2ea20f673e5942fb58e1426a7561c7acstas}
59d7dc153347a9f606c5712f0fae7b65e96682d9rbb
59d7dc153347a9f606c5712f0fae7b65e96682d9rbbstatic void *merge_dbd_config(apr_pool_t *pool, void *basev, void *addv)
dd95dce618f5c95c71733a363f7e3c8acb02f8d4rbb{
dd95dce618f5c95c71733a363f7e3c8acb02f8d4rbb dbd_cfg_t *base = ((svr_cfg*) basev)->cfg;
dd95dce618f5c95c71733a363f7e3c8acb02f8d4rbb dbd_cfg_t *add = ((svr_cfg*) addv)->cfg;
dd95dce618f5c95c71733a363f7e3c8acb02f8d4rbb svr_cfg *svr = apr_pcalloc(pool, sizeof(svr_cfg));
dd95dce618f5c95c71733a363f7e3c8acb02f8d4rbb dbd_cfg_t *new = svr->cfg = apr_pcalloc(pool, sizeof(dbd_cfg_t));
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
dd95dce618f5c95c71733a363f7e3c8acb02f8d4rbb new->server = add->server;
f0c7e2f3f183d7c289eb176434d68a922b66a008wrowe new->name = (add->name != no_dbdriver) ? add->name : base->name;
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe new->params = strcmp(add->params, "") ? add->params : base->params;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb new->persist = (add->persist != -1) ? add->persist : base->persist;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb#if APR_HAS_THREADS
0ec08ecdcb42129d147888bda504787b164da39cerikabele new->nmin = (add->set&NMIN_SET) ? add->nmin : base->nmin;
0ec08ecdcb42129d147888bda504787b164da39cerikabele new->nkeep = (add->set&NKEEP_SET) ? add->nkeep : base->nkeep;
0ec08ecdcb42129d147888bda504787b164da39cerikabele new->nmax = (add->set&NMAX_SET) ? add->nmax : base->nmax;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb new->exptime = (add->set&EXPTIME_SET) ? add->exptime : base->exptime;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe#endif
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb new->queries = apr_hash_overlay(pool, add->queries, base->queries);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
0ec08ecdcb42129d147888bda504787b164da39cerikabele return svr;
0ec08ecdcb42129d147888bda504787b164da39cerikabele}
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
3d96ee83babeec32482c9082c9426340cee8c44dwrowe#define ISINT(val) do { \
2d2eda71267231c2526be701fe655db125852c1ffielding const char *p; \
2d2eda71267231c2526be701fe655db125852c1ffielding \
2d2eda71267231c2526be701fe655db125852c1ffielding for (p = val; *p; ++p) { \
2d2eda71267231c2526be701fe655db125852c1ffielding if (!apr_isdigit(*p)) { \
2d2eda71267231c2526be701fe655db125852c1ffielding return "Argument must be numeric!"; \
2d2eda71267231c2526be701fe655db125852c1ffielding } \
2d2eda71267231c2526be701fe655db125852c1ffielding } \
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb } while (0)
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbbstatic const char *dbd_param(cmd_parms *cmd, void *dconf, const char *val)
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb{
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb const apr_dbd_driver_t *driver = NULL;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb &dbd_module);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb dbd_cfg_t *cfg = svr->cfg;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb switch ((long) cmd->info) {
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb case cmd_name:
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb cfg->name = val;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb /* loading the driver involves once-only dlloading that is
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb * best done at server startup. This also guarantees that
3d96ee83babeec32482c9082c9426340cee8c44dwrowe * we won't return an error later.
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb */
299509b1b45b2fc814f1d57bf93171439295b25ewrowe switch (apr_dbd_get_driver(cmd->pool, cfg->name, &driver)) {
299509b1b45b2fc814f1d57bf93171439295b25ewrowe case APR_ENOTIMPL:
299509b1b45b2fc814f1d57bf93171439295b25ewrowe return apr_psprintf(cmd->pool, "DBD: No driver for %s", cfg->name);
299509b1b45b2fc814f1d57bf93171439295b25ewrowe case APR_EDSOOPEN:
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh return apr_psprintf(cmd->pool,
299509b1b45b2fc814f1d57bf93171439295b25ewrowe#ifdef NETWARE
299509b1b45b2fc814f1d57bf93171439295b25ewrowe "DBD: Can't load driver file dbd%s.nlm",
0af60abaab15a77583f712ba8ea5ad0e027a1d21wrowe#else
299509b1b45b2fc814f1d57bf93171439295b25ewrowe "DBD: Can't load driver file apr_dbd_%s.so",
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb#endif
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb cfg->name);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb case APR_ESYMNOTFOUND:
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb return apr_psprintf(cmd->pool,
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb "DBD: Failed to load driver apr_dbd_%s_driver",
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb cfg->name);
3d96ee83babeec32482c9082c9426340cee8c44dwrowe }
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb break;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb case cmd_params:
0ec08ecdcb42129d147888bda504787b164da39cerikabele cfg->params = val;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb break;
0ec08ecdcb42129d147888bda504787b164da39cerikabele#if APR_HAS_THREADS
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb case cmd_min:
3d96ee83babeec32482c9082c9426340cee8c44dwrowe ISINT(val);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb cfg->nmin = atoi(val);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb cfg->set |= NMIN_SET;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb break;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb case cmd_keep:
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb ISINT(val);
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh cfg->nkeep = atoi(val);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb cfg->set |= NKEEP_SET;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe break;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb case cmd_max:
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar ISINT(val);
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar cfg->nmax = atoi(val);
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar cfg->set |= NMAX_SET;
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar break;
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar case cmd_exp:
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar ISINT(val);
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar cfg->exptime = atoi(val);
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar cfg->set |= EXPTIME_SET;
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar break;
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar#endif
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar }
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar return NULL;
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar}
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar
3d96ee83babeec32482c9082c9426340cee8c44dwrowestatic const char *dbd_param_flag(cmd_parms *cmd, void *dconf, int flag)
9589e61f22bc4e08ec23fb7de00c3b91c3d61cc3coar{
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz &dbd_module);
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz switch ((long) cmd->info) {
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz case cmd_persist:
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz svr->cfg->persist = flag;
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz break;
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz }
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz return NULL;
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz}
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantzstatic const char *dbd_prepare(cmd_parms *cmd, void *dconf, const char *query,
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz const char *label)
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz{
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz if (!label) {
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz label = query;
798c7c11dc2fe3b08e591e9c76fc1a84857f2cd4jerenkrantz query = "";
9589e61f22bc4e08ec23fb7de00c3b91c3d61cc3coar }
9589e61f22bc4e08ec23fb7de00c3b91c3d61cc3coar
9589e61f22bc4e08ec23fb7de00c3b91c3d61cc3coar ap_dbd_prepare(cmd->server, query, label);
2d2eda71267231c2526be701fe655db125852c1ffielding
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh return NULL;
a623efbff95aab78da9e030524b0fa69b054f6d0brianp}
a623efbff95aab78da9e030524b0fa69b054f6d0brianp
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianhstatic const command_rec dbd_cmds[] = {
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh AP_INIT_TAKE1("DBDriver", dbd_param, (void*)cmd_name, RSRC_CONF,
2d2eda71267231c2526be701fe655db125852c1ffielding "SQL Driver"),
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh AP_INIT_TAKE1("DBDParams", dbd_param, (void*)cmd_params, RSRC_CONF,
a623efbff95aab78da9e030524b0fa69b054f6d0brianp "SQL Driver Params"),
a623efbff95aab78da9e030524b0fa69b054f6d0brianp AP_INIT_FLAG("DBDPersist", dbd_param_flag, (void*)cmd_persist, RSRC_CONF,
a623efbff95aab78da9e030524b0fa69b054f6d0brianp "Use persistent connection/pool"),
a623efbff95aab78da9e030524b0fa69b054f6d0brianp AP_INIT_TAKE12("DBDPrepareSQL", dbd_prepare, NULL, RSRC_CONF,
a623efbff95aab78da9e030524b0fa69b054f6d0brianp "SQL statement to prepare (or nothing, to override "
a623efbff95aab78da9e030524b0fa69b054f6d0brianp "statement inherited from main server) and label"),
a623efbff95aab78da9e030524b0fa69b054f6d0brianp#if APR_HAS_THREADS
a623efbff95aab78da9e030524b0fa69b054f6d0brianp AP_INIT_TAKE1("DBDMin", dbd_param, (void*)cmd_min, RSRC_CONF,
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb "Minimum number of connections"),
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb /* XXX: note that mod_proxy calls this "smax" */
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb AP_INIT_TAKE1("DBDKeep", dbd_param, (void*)cmd_keep, RSRC_CONF,
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb "Maximum number of sustained connections"),
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb AP_INIT_TAKE1("DBDMax", dbd_param, (void*)cmd_max, RSRC_CONF,
3d96ee83babeec32482c9082c9426340cee8c44dwrowe "Maximum number of connections"),
2d2eda71267231c2526be701fe655db125852c1ffielding /* XXX: note that mod_proxy calls this "ttl" (time to live) */
2d2eda71267231c2526be701fe655db125852c1ffielding AP_INIT_TAKE1("DBDExptime", dbd_param, (void*)cmd_exp, RSRC_CONF,
3ef7956b993aff4b4882c643910fb688a0e707e3ben "Keepalive time for idle connections"),
3ef7956b993aff4b4882c643910fb688a0e707e3ben#endif
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbb {NULL}
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbb};
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbb
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbbstatic int dbd_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbb apr_pool_t *ptemp)
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbb{
c3e342e5b0b9fea6617ee16d2da02c3ef2108126dougm config_pool = pconf;
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbb group_list = NULL;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb return OK;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb}
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbbDBD_DECLARE_NONSTD(void) ap_dbd_prepare(server_rec *s, const char *query,
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb const char *label)
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb{
3ef7956b993aff4b4882c643910fb688a0e707e3ben svr_cfg *svr;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm svr = ap_get_module_config(s->module_config, &dbd_module);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb if (!svr) {
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe /* some modules may call from within config directive handlers, and
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe * if these are called in a server context that contains no mod_dbd
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * config directives, then we have to create our own server config
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe */
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe svr = create_dbd_config(config_pool, s);
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe ap_set_module_config(s->module_config, &dbd_module, svr);
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe }
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe
e8eee56a2ea20f673e5942fb58e1426a7561c7acstas if (apr_hash_get(svr->cfg->queries, label, APR_HASH_KEY_STRING)
e8eee56a2ea20f673e5942fb58e1426a7561c7acstas && strcmp(query, "")) {
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe "conflicting SQL statements with label %s", label);
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe }
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe apr_hash_set(svr->cfg->queries, label, APR_HASH_KEY_STRING, query);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb}
8476daed795d573496033d88015364df09fe9c68gstein
8476daed795d573496033d88015364df09fe9c68gsteintypedef struct {
8476daed795d573496033d88015364df09fe9c68gstein const char *label, *query;
8476daed795d573496033d88015364df09fe9c68gstein} dbd_query_t;
8476daed795d573496033d88015364df09fe9c68gstein
8476daed795d573496033d88015364df09fe9c68gsteinstatic int dbd_post_config(apr_pool_t *pconf, apr_pool_t *plog,
8476daed795d573496033d88015364df09fe9c68gstein apr_pool_t *ptemp, server_rec *s)
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb{
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb server_rec *sp;
3ef7956b993aff4b4882c643910fb688a0e707e3ben apr_array_header_t *add_queries = apr_array_make(ptemp, 10,
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb sizeof(dbd_query_t));
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb for (sp = s; sp; sp = sp->next) {
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb svr_cfg *svr = ap_get_module_config(sp->module_config, &dbd_module);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb dbd_cfg_t *cfg = svr->cfg;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb apr_hash_index_t *hi_first = apr_hash_first(ptemp, cfg->queries);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb dbd_group_t *group;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
3ef7956b993aff4b4882c643910fb688a0e707e3ben /* dbd_setup in 2.2.3 and under was causing spurious error messages
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb * when dbd isn't configured. We can stop that with a quick check here
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm * together with a similar check in ap_dbd_open (where being
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb * unconfigured is a genuine error that must be reported).
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb */
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb if (cfg->name == no_dbdriver || !cfg->persist) {
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb continue;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb }
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb for (group = group_list; group; group = group->next) {
3ef7956b993aff4b4882c643910fb688a0e707e3ben dbd_cfg_t *group_cfg = group->cfg;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb apr_hash_index_t *hi;
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm int group_ok = 1;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb if (strcmp(cfg->name, group_cfg->name)
8476daed795d573496033d88015364df09fe9c68gstein || strcmp(cfg->params, group_cfg->params)) {
8476daed795d573496033d88015364df09fe9c68gstein continue;
8476daed795d573496033d88015364df09fe9c68gstein }
8476daed795d573496033d88015364df09fe9c68gstein
8476daed795d573496033d88015364df09fe9c68gstein#if APR_HAS_THREADS
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb if (cfg->nmin != group_cfg->nmin
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb || cfg->nkeep != group_cfg->nkeep
3ef7956b993aff4b4882c643910fb688a0e707e3ben || cfg->nmax != group_cfg->nmax
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb || cfg->exptime != group_cfg->exptime) {
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm continue;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb }
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb#endif
8476daed795d573496033d88015364df09fe9c68gstein
8476daed795d573496033d88015364df09fe9c68gstein add_queries->nelts = 0;
8476daed795d573496033d88015364df09fe9c68gstein
8476daed795d573496033d88015364df09fe9c68gstein for (hi = hi_first; hi; hi = apr_hash_next(hi)) {
8476daed795d573496033d88015364df09fe9c68gstein const char *label, *query;
8476daed795d573496033d88015364df09fe9c68gstein const char *group_query;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb apr_hash_this(hi, (void*) &label, NULL, (void*) &query);
3ef7956b993aff4b4882c643910fb688a0e707e3ben
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb group_query = apr_hash_get(group_cfg->queries, label,
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm APR_HASH_KEY_STRING);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb if (!group_query) {
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb dbd_query_t *add_query = apr_array_push(add_queries);
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb
3ef7956b993aff4b4882c643910fb688a0e707e3ben add_query->label = label;
4743e6e0c6d5bc44f086d4f0032ae41a68192dd1rbb add_query->query = query;
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm }
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding else if (strcmp(query, group_query)) {
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe group_ok = 0;
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe break;
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
a623efbff95aab78da9e030524b0fa69b054f6d0brianp
a623efbff95aab78da9e030524b0fa69b054f6d0brianp if (group_ok) {
a623efbff95aab78da9e030524b0fa69b054f6d0brianp int i;
a623efbff95aab78da9e030524b0fa69b054f6d0brianp
a623efbff95aab78da9e030524b0fa69b054f6d0brianp for (i = 0; i < add_queries->nelts; ++i) {
a623efbff95aab78da9e030524b0fa69b054f6d0brianp dbd_query_t *add_query = ((dbd_query_t*) add_queries->elts)
a623efbff95aab78da9e030524b0fa69b054f6d0brianp + i;
a623efbff95aab78da9e030524b0fa69b054f6d0brianp
a623efbff95aab78da9e030524b0fa69b054f6d0brianp apr_hash_set(group_cfg->queries, add_query->label,
a623efbff95aab78da9e030524b0fa69b054f6d0brianp APR_HASH_KEY_STRING, add_query->query);
a623efbff95aab78da9e030524b0fa69b054f6d0brianp }
a623efbff95aab78da9e030524b0fa69b054f6d0brianp
a623efbff95aab78da9e030524b0fa69b054f6d0brianp svr->group = group;
a623efbff95aab78da9e030524b0fa69b054f6d0brianp break;
a623efbff95aab78da9e030524b0fa69b054f6d0brianp }
a623efbff95aab78da9e030524b0fa69b054f6d0brianp }
a623efbff95aab78da9e030524b0fa69b054f6d0brianp
a623efbff95aab78da9e030524b0fa69b054f6d0brianp if (!svr->group) {
a623efbff95aab78da9e030524b0fa69b054f6d0brianp svr->group = group = apr_pcalloc(pconf, sizeof(dbd_group_t));
a623efbff95aab78da9e030524b0fa69b054f6d0brianp
a623efbff95aab78da9e030524b0fa69b054f6d0brianp group->cfg = cfg;
a623efbff95aab78da9e030524b0fa69b054f6d0brianp
a623efbff95aab78da9e030524b0fa69b054f6d0brianp group->next = group_list;
a623efbff95aab78da9e030524b0fa69b054f6d0brianp group_list = group;
a623efbff95aab78da9e030524b0fa69b054f6d0brianp }
a623efbff95aab78da9e030524b0fa69b054f6d0brianp }
a623efbff95aab78da9e030524b0fa69b054f6d0brianp
a623efbff95aab78da9e030524b0fa69b054f6d0brianp return OK;
a623efbff95aab78da9e030524b0fa69b054f6d0brianp}
a623efbff95aab78da9e030524b0fa69b054f6d0brianp
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic apr_status_t dbd_prepared_init(apr_pool_t *pool, dbd_cfg_t *cfg,
2d2eda71267231c2526be701fe655db125852c1ffielding ap_dbd_t *rec)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding apr_hash_index_t *hi;
2d2eda71267231c2526be701fe655db125852c1ffielding apr_status_t rv = APR_SUCCESS;
rec->prepared = apr_hash_make(pool);
for (hi = apr_hash_first(pool, cfg->queries); hi;
hi = apr_hash_next(hi)) {
const char *label, *query;
apr_dbd_prepared_t *stmt;
apr_hash_this(hi, (void*) &label, NULL, (void*) &query);
if (!strcmp(query, "")) {
continue;
}
stmt = NULL;
if (apr_dbd_prepare(rec->driver, pool, rec->handle, query,
label, &stmt)) {
rv = APR_EGENERAL;
}
else {
apr_hash_set(rec->prepared, label, APR_HASH_KEY_STRING, stmt);
}
}
return rv;
}
static apr_status_t dbd_close(void *data)
{
ap_dbd_t *rec = data;
return apr_dbd_close(rec->driver, rec->handle);
}
#if APR_HAS_THREADS
static apr_status_t dbd_destruct(void *data, void *params, apr_pool_t *pool)
{
dbd_group_t *group = params;
if (!group->destroyed) {
ap_dbd_t *rec = data;
apr_pool_destroy(rec->pool);
}
return APR_SUCCESS;
}
#endif
/* an apr_reslist_constructor for SQL connections
* Also use this for opening in non-reslist modes, since it gives
* us all the error-handling in one place.
*/
static apr_status_t dbd_construct(void **data_ptr,
void *params, apr_pool_t *pool)
{
dbd_group_t *group = params;
dbd_cfg_t *cfg = group->cfg;
apr_pool_t *rec_pool, *prepared_pool;
ap_dbd_t *rec;
apr_status_t rv;
rv = apr_pool_create(&rec_pool, pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, cfg->server,
"DBD: Failed to create memory pool");
return rv;
}
rec = apr_pcalloc(rec_pool, sizeof(ap_dbd_t));
rec->pool = rec_pool;
/* The driver is loaded at config time now, so this just checks a hash.
* If that changes, the driver DSO could be registered to unload against
* our pool, which is probably not what we want. Error checking isn't
* necessary now, but in case that changes in the future ...
*/
rv = apr_dbd_get_driver(rec->pool, cfg->name, &rec->driver);
if (rv != APR_SUCCESS) {
switch (rv) {
case APR_ENOTIMPL:
ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
"DBD: driver for %s not available", cfg->name);
break;
case APR_EDSOOPEN:
ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
"DBD: can't find driver for %s", cfg->name);
break;
case APR_ESYMNOTFOUND:
ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
"DBD: driver for %s is invalid or corrupted",
cfg->name);
break;
default:
ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
"DBD: mod_dbd not compatible with APR in get_driver");
break;
}
apr_pool_destroy(rec->pool);
return rv;
}
rv = apr_dbd_open(rec->driver, rec->pool, cfg->params, &rec->handle);
if (rv != APR_SUCCESS) {
switch (rv) {
case APR_EGENERAL:
ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
"DBD: Can't connect to %s", cfg->name);
break;
default:
ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
"DBD: mod_dbd not compatible with APR in open");
break;
}
apr_pool_destroy(rec->pool);
return rv;
}
apr_pool_cleanup_register(rec->pool, rec, dbd_close,
apr_pool_cleanup_null);
/* we use a sub-pool for the prepared statements for each connection so
* that they will be cleaned up first, before the connection is closed
*/
rv = apr_pool_create(&prepared_pool, rec->pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, cfg->server,
"DBD: Failed to create memory pool");
apr_pool_destroy(rec->pool);
return rv;
}
rv = dbd_prepared_init(prepared_pool, cfg, rec);
if (rv != APR_SUCCESS) {
const char *errmsg = apr_dbd_error(rec->driver, rec->handle, rv);
ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
"DBD: failed to prepare SQL statements: %s",
(errmsg ? errmsg : "[???]"));
apr_pool_destroy(rec->pool);
return rv;
}
*data_ptr = rec;
return APR_SUCCESS;
}
#if APR_HAS_THREADS
static apr_status_t dbd_destroy(void *data)
{
dbd_group_t *group = data;
group->destroyed = 1;
return APR_SUCCESS;
}
static apr_status_t dbd_setup(server_rec *s, dbd_group_t *group)
{
dbd_cfg_t *cfg = group->cfg;
apr_status_t rv;
/* We create the reslist using a sub-pool of the pool passed to our
* child_init hook. No other threads can be here because we're
* either in the child_init phase or dbd_setup_lock() acquired our mutex.
* No other threads will use this sub-pool after this, except via
* reslist calls, which have an internal mutex.
*
* We need to short-circuit the cleanup registered internally by
* apr_reslist_create(). We do this by registering dbd_destroy()
* as a cleanup afterwards, so that it will run before the reslist's
* internal cleanup.
*
* If we didn't do this, then we could free memory twice when the pool
* was destroyed. When apr_pool_destroy() runs, it first destroys all
* all the per-connection sub-pools created in dbd_construct(), and
* then it runs the reslist's cleanup. The cleanup calls dbd_destruct()
* on each resource, which would then attempt to destroy the sub-pools
* a second time.
*/
rv = apr_reslist_create(&group->reslist,
cfg->nmin, cfg->nkeep, cfg->nmax,
apr_time_from_sec(cfg->exptime),
dbd_construct, dbd_destruct, group,
group->pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
"DBD: failed to initialise");
return rv;
}
apr_pool_cleanup_register(group->pool, group, dbd_destroy,
apr_pool_cleanup_null);
return APR_SUCCESS;
}
#endif
static apr_status_t dbd_setup_init(apr_pool_t *pool, server_rec *s)
{
dbd_group_t *group;
apr_status_t rv = APR_SUCCESS;
for (group = group_list; group; group = group->next) {
apr_status_t rv2;
rv2 = apr_pool_create(&group->pool, pool);
if (rv2 != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv2, s,
"DBD: Failed to create reslist cleanup memory pool");
return rv2;
}
#if APR_HAS_THREADS
rv2 = dbd_setup(s, group);
if (rv2 == APR_SUCCESS) {
continue;
}
else if (rv == APR_SUCCESS) {
rv = rv2;
}
/* we failed, so create a mutex so that subsequent competing callers
* to ap_dbd_open can serialize themselves while they retry
*/
rv2 = apr_thread_mutex_create(&group->mutex,
APR_THREAD_MUTEX_DEFAULT, pool);
if (rv2 != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv2, s,
"DBD: Failed to create thread mutex");
return rv2;
}
#endif
}
return rv;
}
#if APR_HAS_THREADS
static apr_status_t dbd_setup_lock(server_rec *s, dbd_group_t *group)
{
apr_status_t rv = APR_SUCCESS, rv2;
/* several threads could be here at the same time, all trying to
* initialize the reslist because dbd_setup_init failed to do so
*/
if (!group->mutex) {
/* we already logged an error when the mutex couldn't be created */
return APR_EGENERAL;
}
rv2 = apr_thread_mutex_lock(group->mutex);
if (rv2 != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv2, s,
"DBD: Failed to acquire thread mutex");
return rv2;
}
if (!group->reslist) {
rv = dbd_setup(s, group);
}
rv2 = apr_thread_mutex_unlock(group->mutex);
if (rv2 != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv2, s,
"DBD: Failed to release thread mutex");
if (rv == APR_SUCCESS) {
rv = rv2;
}
}
return rv;
}
#endif
/* Functions we export for modules to use:
- open acquires a connection from the pool (opens one if necessary)
- close releases it back in to the pool
*/
DBD_DECLARE_NONSTD(void) ap_dbd_close(server_rec *s, ap_dbd_t *rec)
{
svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
if (!svr->cfg->persist) {
apr_pool_destroy(rec->pool);
}
#if APR_HAS_THREADS
else {
apr_reslist_release(svr->group->reslist, rec);
}
#endif
}
static apr_status_t dbd_check(apr_pool_t *pool, server_rec *s, ap_dbd_t *rec)
{
svr_cfg *svr;
apr_status_t rv = apr_dbd_check_conn(rec->driver, pool, rec->handle);
const char *errmsg;
if ((rv == APR_SUCCESS) || (rv == APR_ENOTIMPL)) {
return APR_SUCCESS;
}
/* we don't have a driver-specific error code, so we'll just pass
* a "success" value and rely on the driver to ignore it
*/
errmsg = apr_dbd_error(rec->driver, rec->handle, 0);
if (!errmsg) {
errmsg = "(unknown)";
}
svr = ap_get_module_config(s->module_config, &dbd_module);
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
"DBD [%s] Error: %s", svr->cfg->name, errmsg);
return rv;
}
DBD_DECLARE_NONSTD(ap_dbd_t*) ap_dbd_open(apr_pool_t *pool, server_rec *s)
{
svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
dbd_group_t *group = svr->group;
dbd_cfg_t *cfg = svr->cfg;
ap_dbd_t *rec = NULL;
#if APR_HAS_THREADS
apr_status_t rv;
#endif
/* If nothing is configured, we shouldn't be here */
if (cfg->name == no_dbdriver) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "DBD: not configured");
return NULL;
}
if (!cfg->persist) {
/* Return a once-only connection */
group = apr_pcalloc(pool, sizeof(dbd_group_t));
group->cfg = cfg;
dbd_construct((void*) &rec, group, pool);
return rec;
}
#if APR_HAS_THREADS
if (!group->reslist) {
if (dbd_setup_lock(s, group) != APR_SUCCESS) {
return NULL;
}
}
rv = apr_reslist_acquire(group->reslist, (void*) &rec);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
"Failed to acquire DBD connection from pool!");
return NULL;
}
if (dbd_check(pool, s, rec) != APR_SUCCESS) {
apr_reslist_invalidate(group->reslist, rec);
return NULL;
}
#else
/* If we have a persistent connection and it's good, we'll use it;
* since this is non-threaded, we can update without a mutex
*/
rec = group->rec;
if (rec) {
if (dbd_check(pool, s, rec) != APR_SUCCESS) {
apr_pool_destroy(rec->pool);
rec = NULL;
}
}
/* We don't have a connection right now, so we'll open one */
if (!rec) {
dbd_construct((void*) &rec, group, group->pool);
group->rec = rec;
}
#endif
return rec;
}
#if APR_HAS_THREADS
typedef struct {
ap_dbd_t *rec;
apr_reslist_t *reslist;
} dbd_acquire_t;
static apr_status_t dbd_release(void *data)
{
dbd_acquire_t *acq = data;
apr_reslist_release(acq->reslist, acq->rec);
return APR_SUCCESS;
}
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
{
dbd_acquire_t *acq;
while (!ap_is_initial_req(r)) {
if (r->prev) {
r = r->prev;
}
else if (r->main) {
r = r->main;
}
}
acq = ap_get_module_config(r->request_config, &dbd_module);
if (!acq) {
acq = apr_palloc(r->pool, sizeof(dbd_acquire_t));
acq->rec = ap_dbd_open(r->pool, r->server);
if (acq->rec) {
svr_cfg *svr = ap_get_module_config(r->server->module_config,
&dbd_module);
ap_set_module_config(r->request_config, &dbd_module, acq);
if (svr->cfg->persist) {
acq->reslist = svr->group->reslist;
apr_pool_cleanup_register(r->pool, acq, dbd_release,
apr_pool_cleanup_null);
}
}
}
return acq->rec;
}
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
{
dbd_acquire_t *acq = ap_get_module_config(c->conn_config, &dbd_module);
if (!acq) {
acq = apr_palloc(c->pool, sizeof(dbd_acquire_t));
acq->rec = ap_dbd_open(c->pool, c->base_server);
if (acq->rec) {
svr_cfg *svr = ap_get_module_config(c->base_server->module_config,
&dbd_module);
ap_set_module_config(c->conn_config, &dbd_module, acq);
if (svr->cfg->persist) {
acq->reslist = svr->group->reslist;
apr_pool_cleanup_register(c->pool, acq, dbd_release,
apr_pool_cleanup_null);
}
}
}
return acq->rec;
}
#else
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
{
ap_dbd_t *rec;
while (!ap_is_initial_req(r)) {
if (r->prev) {
r = r->prev;
}
else if (r->main) {
r = r->main;
}
}
rec = ap_get_module_config(r->request_config, &dbd_module);
if (!rec) {
rec = ap_dbd_open(r->pool, r->server);
if (rec) {
ap_set_module_config(r->request_config, &dbd_module, rec);
}
}
return rec;
}
DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
{
ap_dbd_t *rec = ap_get_module_config(c->conn_config, &dbd_module);
if (!rec) {
rec = ap_dbd_open(c->pool, c->base_server);
if (rec) {
ap_set_module_config(c->conn_config, &dbd_module, rec);
}
}
return rec;
}
#endif
static void dbd_hooks(apr_pool_t *pool)
{
ap_hook_pre_config(dbd_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_post_config(dbd_post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_child_init((void*)dbd_setup_init, NULL, NULL, APR_HOOK_MIDDLE);
APR_REGISTER_OPTIONAL_FN(ap_dbd_prepare);
APR_REGISTER_OPTIONAL_FN(ap_dbd_open);
APR_REGISTER_OPTIONAL_FN(ap_dbd_close);
APR_REGISTER_OPTIONAL_FN(ap_dbd_acquire);
APR_REGISTER_OPTIONAL_FN(ap_dbd_cacquire);
apr_dbd_init(pool);
}
module AP_MODULE_DECLARE_DATA dbd_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
create_dbd_config,
merge_dbd_config,
dbd_cmds,
dbd_hooks
};