mod_dbd.c revision 185aa71728867671e105178b4c66fbc22b65ae26
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Overview of what this is and does:
* or
*/
#include "apr_reslist.h"
#include "apr_strings.h"
#include "apr_hash.h"
#include "apr_tables.h"
#include "apr_lib.h"
#include "apr_dbd.h"
#define APR_WANT_MEMFUNC
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "http_protocol.h"
#include "http_config.h"
#include "http_log.h"
#include "http_request.h"
#include "mod_dbd.h"
/************ svr cfg: manage db connection pool ****************/
#define NMIN_SET 0x1
#define NKEEP_SET 0x2
#define NMAX_SET 0x4
#define EXPTIME_SET 0x8
typedef struct dbd_group_t dbd_group_t;
struct dbd_group_t {
#if APR_HAS_THREADS
int destroyed;
#else
#endif
};
typedef struct {
} svr_cfg;
} cmd_parts;
static apr_pool_t *config_pool;
static dbd_group_t *group_list;
/* a default DBDriver value that'll generate meaningful error messages */
static const char *const no_dbdriver = "[DBDriver unset]";
/* A default nmin of >0 will help with generating meaningful
* startup error messages if the database is down.
*/
#define DEFAULT_NMIN 1
#define DEFAULT_NKEEP 2
#define DEFAULT_NMAX 10
#define DEFAULT_EXPTIME 300
#define DEFAULT_SQL_INIT_ARRAY_SIZE 5
{
#if APR_HAS_THREADS
#endif
sizeof(const char *));
return svr;
}
{
#if APR_HAS_THREADS
#endif
base->init_queries);
return svr;
}
{
const char **arr_item;
if (!svr) {
/* some modules may call from within config directive handlers, and
* if these are called in a server context that contains no mod_dbd
* config directives, then we have to create our own server config
*/
}
if (query) {
}
}
{
&dbd_module);
case cmd_name:
/* loading the driver involves once-only dlloading that is
* best done at server startup. This also guarantees that
* we won't return an error later.
*/
if (APR_STATUS_IS_ENOTIMPL(rv)) {
}
else if (APR_STATUS_IS_EDSOOPEN(rv)) {
#ifdef NETWARE
"Can't load driver file dbd%s.nlm",
#else
"Can't load driver file apr_dbd_%s.so",
#endif
}
else if (APR_STATUS_IS_ESYMNOTFOUND(rv)) {
"Failed to load driver apr_dbd_%s_driver",
}
break;
case cmd_params:
break;
}
return NULL;
}
#if APR_HAS_THREADS
{
&dbd_module);
const char *p;
for (p = val; *p; ++p) {
if (!apr_isdigit(*p)) {
return "Argument must be numeric!";
}
}
case cmd_min:
break;
case cmd_keep:
break;
case cmd_max:
break;
case cmd_exp:
break;
}
return NULL;
}
#endif
{
&dbd_module);
case cmd_persist:
break;
}
return NULL;
}
const char *label)
{
if (!label) {
query = "";
}
return NULL;
}
{
return "You should specify SQL statement";
}
return NULL;
}
static const command_rec dbd_cmds[] = {
"SQL Driver"),
"SQL Driver Params"),
"Use persistent connection/pool"),
"SQL statement to prepare (or nothing, to override "
"statement inherited from main server) and label"),
"SQL statement to be executed after connection is created"),
#if APR_HAS_THREADS
"Minimum number of connections"),
/* XXX: note that mod_proxy calls this "smax" */
"Maximum number of sustained connections"),
"Maximum number of connections"),
/* XXX: note that mod_proxy calls this "ttl" (time to live) */
"Keepalive time for idle connections"),
#endif
{NULL}
};
{
config_pool = pconf;
group_list = NULL;
return OK;
}
const char *label)
{
if (!svr) {
/* some modules may call from within config directive handlers, and
* if these are called in a server context that contains no mod_dbd
* config directives, then we have to create our own server config
*/
}
"conflicting SQL statements with label %s", label);
}
}
typedef struct {
} dbd_query_t;
{
server_rec *sp;
sizeof(dbd_query_t));
/* dbd_setup in 2.2.3 and under was causing spurious error messages
* when dbd isn't configured. We can stop that with a quick check here
* together with a similar check in ap_dbd_open (where being
* unconfigured is a genuine error that must be reported).
*/
continue;
}
int group_ok = 1;
continue;
}
#if APR_HAS_THREADS
continue;
}
#endif
add_queries->nelts = 0;
const char *group_query;
if (!group_query) {
}
group_ok = 0;
break;
}
}
if (group_ok) {
int i;
for (i = 0; i < add_queries->nelts; ++i) {
+ i;
}
break;
}
}
group_list = group;
}
}
return OK;
}
{
continue;
}
return APR_EGENERAL;
}
else {
}
}
return APR_SUCCESS;
}
{
int i;
int nrows;
char **query_p;
rv = APR_EGENERAL;
break;
}
}
return rv;
}
{
}
#if APR_HAS_THREADS
{
}
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.
*/
{
const char *err = "";
if (rv != APR_SUCCESS) {
"Failed to create memory pool");
return rv;
}
/* 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 ...
*/
if (rv != APR_SUCCESS) {
if (APR_STATUS_IS_ENOTIMPL(rv)) {
}
else if (APR_STATUS_IS_EDSOOPEN(rv)) {
}
else if (APR_STATUS_IS_ESYMNOTFOUND(rv)) {
"driver for %s is invalid or corrupted",
}
else {
"mod_dbd not compatible with APR in get_driver");
}
return rv;
}
if (rv != APR_SUCCESS) {
switch (rv) {
case APR_EGENERAL:
break;
default:
"mod_dbd not compatible with APR in open");
break;
}
return rv;
}
/* 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
*/
if (rv != APR_SUCCESS) {
"Failed to create memory pool");
return rv;
}
if (rv != APR_SUCCESS) {
"failed to prepare SQL statements: %s",
return rv;
}
return APR_SUCCESS;
}
#if APR_HAS_THREADS
{
return APR_SUCCESS;
}
{
/* 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.
*/
if (rv != APR_SUCCESS) {
"failed to initialise");
return rv;
}
return APR_SUCCESS;
}
#endif
{
if (rv2 != APR_SUCCESS) {
"Failed to create reslist cleanup memory pool");
return rv2;
}
#if APR_HAS_THREADS
if (rv2 == APR_SUCCESS) {
continue;
}
else if (rv == APR_SUCCESS) {
}
/* we failed, so create a mutex so that subsequent competing callers
* to ap_dbd_open can serialize themselves while they retry
*/
if (rv2 != APR_SUCCESS) {
"Failed to create thread mutex");
return rv2;
}
#endif
}
return rv;
}
{
if (rv) {
"child init failed!");
}
}
#if APR_HAS_THREADS
{
/* several threads could be here at the same time, all trying to
* initialize the reslist because dbd_setup_init failed to do so
*/
/* we already logged an error when the mutex couldn't be created */
return APR_EGENERAL;
}
if (rv2 != APR_SUCCESS) {
"Failed to acquire thread mutex");
return rv2;
}
}
if (rv2 != APR_SUCCESS) {
"Failed to release thread mutex");
if (rv == APR_SUCCESS) {
}
}
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
*/
{
}
#if APR_HAS_THREADS
else {
}
#endif
}
{
const char *errmsg;
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
*/
if (!errmsg) {
errmsg = "(unknown)";
}
return rv;
}
{
#if APR_HAS_THREADS
#endif
/* If nothing is configured, we shouldn't be here */
return NULL;
}
/* Return a once-only connection */
return rec;
}
#if APR_HAS_THREADS
return NULL;
}
}
if (rv != APR_SUCCESS) {
"Failed to acquire DBD connection from pool!");
return NULL;
}
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
*/
if (rec) {
}
}
/* We don't have a connection right now, so we'll open one */
if (!rec) {
}
#endif
return rec;
}
#if APR_HAS_THREADS
typedef struct {
{
return APR_SUCCESS;
}
{
while (!ap_is_initial_req(r)) {
if (r->prev) {
r = r->prev;
}
else if (r->main) {
r = r->main;
}
}
if (!acq) {
&dbd_module);
}
}
}
}
{
if (!acq) {
&dbd_module);
}
}
}
}
#else
{
while (!ap_is_initial_req(r)) {
if (r->prev) {
r = r->prev;
}
else if (r->main) {
r = r->main;
}
}
if (!rec) {
if (rec) {
}
}
return rec;
}
{
if (!rec) {
if (rec) {
}
}
return rec;
}
#endif
{
}
AP_DECLARE_MODULE(dbd) = {
NULL,
NULL,
};