httpd-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chr...@apache.org
Subject svn commit: r491729 - /httpd/httpd/trunk/modules/database/mod_dbd.c
Date Tue, 02 Jan 2007 01:19:22 GMT
Author: chrisd
Date: Mon Jan  1 17:19:21 2007
New Revision: 491729

URL: http://svn.apache.org/viewvc?view=rev&rev=491729
Log:
Rename functions and variables for consistency, and move some functions
around for readability, prior to forthcoming functional changes.

Modified:
    httpd/httpd/trunk/modules/database/mod_dbd.c

Modified: httpd/httpd/trunk/modules/database/mod_dbd.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/database/mod_dbd.c?view=diff&rev=491729&r1=491728&r2=491729
==============================================================================
--- httpd/httpd/trunk/modules/database/mod_dbd.c (original)
+++ httpd/httpd/trunk/modules/database/mod_dbd.c Mon Jan  1 17:19:21 2007
@@ -40,12 +40,15 @@
 #define NMAX_SET     0x4
 #define EXPTIME_SET  0x8
 
-typedef struct dbd_prepared {
+typedef struct dbd_prepared dbd_prepared;
+
+struct dbd_prepared {
     const char *label;
     const char *query;
-    struct dbd_prepared *next;
-} dbd_prepared;
-typedef struct svr_cfg {
+    dbd_prepared *next;
+};
+
+typedef struct {
     const char *name;
     const char *params;
     int persist;
@@ -53,13 +56,13 @@
 #if APR_HAS_THREADS
     apr_thread_mutex_t *mutex;
     apr_pool_t *pool;
-    apr_reslist_t *dbpool;
+    apr_reslist_t *reslist;
     int nmin;
     int nkeep;
     int nmax;
     int exptime;
 #else
-    ap_dbd_t *conn;
+    ap_dbd_t *rec;
 #endif
     unsigned int set;
 } svr_cfg;
@@ -73,16 +76,63 @@
 /* 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
+
+static void *create_dbd_config(apr_pool_t *pool, server_rec *s)
+{
+    svr_cfg *svr = apr_pcalloc(pool, sizeof(svr_cfg));
+
+    svr->name = no_dbdriver; /* to generate meaningful error messages */
+    svr->params = ""; /* don't risk segfault on misconfiguration */
+    svr->persist = -1;
+#if APR_HAS_THREADS
+    svr->nmin = DEFAULT_NMIN;
+    svr->nkeep = DEFAULT_NKEEP;
+    svr->nmax = DEFAULT_NMAX;
+    svr->exptime = DEFAULT_EXPTIME;
+#endif
+
+    return svr;
+}
+
+static void *merge_dbd_config(apr_pool_t *pool, void *basev, void *addv)
+{
+    svr_cfg *base = (svr_cfg*) basev;
+    svr_cfg *add = (svr_cfg*) addv;
+    svr_cfg *svr = apr_pcalloc(pool, sizeof(svr_cfg));
+
+    svr->name = (add->name != no_dbdriver) ? add->name : base->name;
+    svr->params = strcmp(add->params, "") ? add->params : base->params;
+    svr->persist = (add->persist == -1) ? base->persist : add->persist;
+#if APR_HAS_THREADS
+    svr->nmin = (add->set&NMIN_SET) ? add->nmin : base->nmin;
+    svr->nkeep = (add->set&NKEEP_SET) ? add->nkeep : base->nkeep;
+    svr->nmax = (add->set&NMAX_SET) ? add->nmax : base->nmax;
+    svr->exptime = (add->set&EXPTIME_SET) ? add->exptime : base->exptime;
+#endif
+    svr->set = add->set | base->set;
+    svr->prepared = (add->prepared != NULL) ? add->prepared : base->prepared;
+
+    return svr;
+}
+
 #define ISINT(val) \
         for (p = val; *p; ++p)        \
                 if (!isdigit(*p))        \
                         return "Argument must be numeric!"
-static const char *dbd_param(cmd_parms *cmd, void *cfg, const char *val)
+
+static const char *dbd_param(cmd_parms *cmd, void *dconf, const char *val)
 {
     const char *p;
     const apr_dbd_driver_t *driver = NULL;
-    svr_cfg *svr = (svr_cfg*) ap_get_module_config
-        (cmd->server->module_config, &dbd_module);
+    svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
+                                        &dbd_module);
 
     switch ((long) cmd->info) {
     case cmd_name:
@@ -130,36 +180,32 @@
         break;
 #endif
     }
+
     return NULL;
 }
-static const char *dbd_param_flag(cmd_parms *cmd, void *cfg, int flag)
+
+static const char *dbd_param_flag(cmd_parms *cmd, void *dconf, int flag)
 {
-    svr_cfg *svr = (svr_cfg*) ap_get_module_config
-        (cmd->server->module_config, &dbd_module);
+    svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
+                                        &dbd_module);
 
     switch ((long) cmd->info) {
     case cmd_persist:
         svr->persist = flag;
         break;
     }
+
     return NULL;
 }
-DBD_DECLARE_NONSTD(void) ap_dbd_prepare(server_rec *s, const char *query,
-                                        const char *label)
-{
-    dbd_prepared *prepared = apr_pcalloc(s->process->pool, sizeof(dbd_prepared));
-    const char *key = apr_psprintf(s->process->pool, "%pp", s);
-    prepared->label = label;
-    prepared->query = query;
-    prepared->next = apr_hash_get(dbd_prepared_defns, key, APR_HASH_KEY_STRING);
-    apr_hash_set(dbd_prepared_defns, key, APR_HASH_KEY_STRING, prepared);
-}
-static const char *dbd_prepare(cmd_parms *cmd, void *cfg, const char *query,
+
+static const char *dbd_prepare(cmd_parms *cmd, void *dconf, const char *query,
                                const char *label)
 {
     ap_dbd_prepare(cmd->server, query, label);
+
     return NULL;
 }
+
 static const command_rec dbd_cmds[] = {
     AP_INIT_TAKE1("DBDriver", dbd_param, (void*)cmd_name, RSRC_CONF,
                   "SQL Driver"),
@@ -168,7 +214,7 @@
     AP_INIT_FLAG("DBDPersist", dbd_param_flag, (void*)cmd_persist, RSRC_CONF,
                  "Use persistent connection/pool"),
     AP_INIT_TAKE2("DBDPrepareSQL", dbd_prepare, NULL, RSRC_CONF,
-                  "Prepared SQL statement, label"),
+                  "SQL statement to prepare and label"),
 #if APR_HAS_THREADS
     AP_INIT_TAKE1("DBDMin", dbd_param, (void*)cmd_min, RSRC_CONF,
                   "Minimum number of connections"),
@@ -183,72 +229,92 @@
 #endif
     {NULL}
 };
-static void *dbd_merge(apr_pool_t *pool, void *BASE, void *ADD) {
-    svr_cfg *base = (svr_cfg*) BASE;
-    svr_cfg *add = (svr_cfg*) ADD;
-    svr_cfg *cfg = apr_pcalloc(pool, sizeof(svr_cfg));
-    cfg->name = (add->name != no_dbdriver) ? add->name : base->name;
-    cfg->params = strcmp(add->params, "") ? add->params : base->params;
-    cfg->persist = (add->persist == -1) ? base->persist : add->persist;
-#if APR_HAS_THREADS
-    cfg->nmin = (add->set&NMIN_SET) ? add->nmin : base->nmin;
-    cfg->nkeep = (add->set&NKEEP_SET) ? add->nkeep : base->nkeep;
-    cfg->nmax = (add->set&NMAX_SET) ? add->nmax : base->nmax;
-    cfg->exptime = (add->set&EXPTIME_SET) ? add->exptime : base->exptime;
-#endif
-    cfg->set = add->set | base->set;
-    cfg->prepared = (add->prepared != NULL) ? add->prepared : base->prepared;
-    return (void*) cfg;
+
+static int dbd_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
+                          apr_pool_t *ptemp)
+{
+   dbd_prepared_defns = apr_hash_make(ptemp);
+   return OK;
 }
-/* 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
-static void *dbd_cfg(apr_pool_t *p, server_rec *x)
+
+DBD_DECLARE_NONSTD(void) ap_dbd_prepare(server_rec *s, const char *query,
+                                        const char *label)
 {
-    svr_cfg *svr = (svr_cfg*) apr_pcalloc(p, sizeof(svr_cfg));
-    svr->params = ""; /* don't risk segfault on misconfiguration */
-    svr->name = no_dbdriver; /* to generate meaningful error messages */
-    svr->persist = -1;
-#if APR_HAS_THREADS
-    svr->nmin = DEFAULT_NMIN;
-    svr->nkeep = DEFAULT_NKEEP;
-    svr->nmax = DEFAULT_NMAX;
-    svr->exptime = DEFAULT_EXPTIME;
-#endif
-    return svr;
+    dbd_prepared *prepared = apr_pcalloc(s->process->pool,
+                                         sizeof(dbd_prepared));
+    const char *key = apr_psprintf(s->process->pool, "%pp", s);
+
+    prepared->label = label;
+    prepared->query = query;
+    prepared->next = apr_hash_get(dbd_prepared_defns, key, APR_HASH_KEY_STRING);
+
+    apr_hash_set(dbd_prepared_defns, key, APR_HASH_KEY_STRING, prepared);
 }
+
+static int dbd_post_config(apr_pool_t *pconf, apr_pool_t *plog,
+                           apr_pool_t *ptemp, server_rec *s)
+{
+    server_rec *sp;
+
+    for (sp = s; sp; sp = sp->next) {
+        svr_cfg *svr = ap_get_module_config(sp->module_config, &dbd_module);
+        const char *key = apr_psprintf(s->process->pool, "%pp", s);
+
+        svr->prepared = apr_hash_get(dbd_prepared_defns, key,
+                                     APR_HASH_KEY_STRING);
+    }
+
+    return OK;
+}
+
 static apr_status_t dbd_prepared_init(apr_pool_t *pool, svr_cfg *svr,
-                                      ap_dbd_t *dbd)
+                                      ap_dbd_t *rec)
 {
     dbd_prepared *p;
-    apr_status_t ret = APR_SUCCESS;
+    apr_status_t rv = APR_SUCCESS;
     apr_dbd_prepared_t *stmt;
-    dbd->prepared = apr_hash_make(pool);
+
+    rec->prepared = apr_hash_make(pool);
 
     for (p = svr->prepared; p; p = p->next) {
         stmt = NULL;
-        if (apr_dbd_prepare(dbd->driver, pool, dbd->handle, p->query,
+        if (apr_dbd_prepare(rec->driver, pool, rec->handle, p->query,
                             p->label, &stmt) == 0) {
-            apr_hash_set(dbd->prepared, p->label, APR_HASH_KEY_STRING, stmt);
+            apr_hash_set(rec->prepared, p->label, APR_HASH_KEY_STRING, stmt);
         }
         else {
-            ret = APR_EGENERAL;
+            rv = APR_EGENERAL;
         }
     }
-    return ret;
+
+    return rv;
+}
+
+static apr_status_t dbd_close(void *data)
+{
+    ap_dbd_t *rec = data;
+    apr_status_t rv = apr_dbd_close(rec->driver, rec->handle);
+
+    apr_pool_destroy(rec->pool);
+
+    return rv;
 }
-/************ svr cfg: manage db connection pool ****************/
+
+#if APR_HAS_THREADS
+static apr_status_t dbd_destruct(void *data, void *params, apr_pool_t *pool)
+{
+    return dbd_close(data);
+}
+#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 **db, void *params, apr_pool_t *pool)
+static apr_status_t dbd_construct(void **data_ptr,
+                                  void *params, apr_pool_t *pool)
 {
-    svr_cfg *svr = (svr_cfg*) params;
+    svr_cfg *svr = params;
     ap_dbd_t *rec = apr_pcalloc(pool, sizeof(ap_dbd_t));
     apr_status_t rv;
 
@@ -259,69 +325,66 @@
                       "DBD: Failed to create memory 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 ...
- */
+    /* 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, svr->name, &rec->driver);
-    switch (rv) {
-    case APR_ENOTIMPL:
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
-                      "DBD: driver for %s not available", svr->name);
-        return rv;
-    case APR_EDSOOPEN:
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
-                      "DBD: can't find driver for %s", svr->name);
-        return rv;
-    case APR_ESYMNOTFOUND:
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
-                      "DBD: driver for %s is invalid or corrupted", svr->name);
-        return rv;
-    default:
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
-                      "DBD: mod_dbd not compatible with apr in get_driver");
+    if (rv != APR_SUCCESS) {
+        switch (rv) {
+        case APR_ENOTIMPL:
+            ap_log_perror(APLOG_MARK, APLOG_ERR, rv, rec->pool,
+                          "DBD: driver for %s not available", svr->name);
+            break;
+        case APR_EDSOOPEN:
+            ap_log_perror(APLOG_MARK, APLOG_ERR, rv, rec->pool,
+                          "DBD: can't find driver for %s", svr->name);
+            break;
+        case APR_ESYMNOTFOUND:
+            ap_log_perror(APLOG_MARK, APLOG_ERR, rv, rec->pool,
+                          "DBD: driver for %s is invalid or corrupted",
+                          svr->name);
+            break;
+        default:
+            ap_log_perror(APLOG_MARK, APLOG_ERR, rv, rec->pool,
+                          "DBD: mod_dbd not compatible with APR in get_driver");
+            break;
+        }
+
         return rv;
-    case APR_SUCCESS:
-        break;
     }
 
     rv = apr_dbd_open(rec->driver, rec->pool, svr->params, &rec->handle);
-    switch (rv) {
-    case APR_EGENERAL:
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
-                      "DBD: Can't connect to %s", svr->name);
-        return rv;
-    default:
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
-                      "DBD: mod_dbd not compatible with apr in open");
+    if (rv != APR_SUCCESS) {
+        switch (rv) {
+        case APR_EGENERAL:
+            ap_log_perror(APLOG_MARK, APLOG_ERR, rv, rec->pool,
+                          "DBD: Can't connect to %s", svr->name);
+            break;
+        default:
+            ap_log_perror(APLOG_MARK, APLOG_ERR, rv, rec->pool,
+                          "DBD: mod_dbd not compatible with APR in open");
+            break;
+        }
+
         return rv;
-    case APR_SUCCESS:
-        break;
     }
-    *db = rec;
+
+    *data_ptr = rec;
+
     rv = dbd_prepared_init(rec->pool, svr, rec);
     if (rv != APR_SUCCESS) {
         const char *errmsg = apr_dbd_error(rec->driver, rec->handle, rv);
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool,
-                      "DBD: failed to initialise prepared SQL statements: %s",
+        ap_log_perror(APLOG_MARK, APLOG_ERR, rv, rec->pool,
+                      "DBD: failed to prepare SQL statements: %s",
                       (errmsg ? errmsg : "[???]"));
     }
+
     return rv;
 }
-static apr_status_t dbd_close(void *CONN)
-{
-    ap_dbd_t *conn = CONN;
-    apr_status_t rv = apr_dbd_close(conn->driver, conn->handle);
-    apr_pool_destroy(conn->pool);
-    return rv;
-}
-#if APR_HAS_THREADS
-static apr_status_t dbd_destruct(void *sql, void *params, apr_pool_t *pool)
-{
-    return dbd_close(sql);
-}
 
+#if APR_HAS_THREADS
 static apr_status_t dbd_setup(apr_pool_t *pool, svr_cfg *svr)
 {
     apr_status_t rv;
@@ -339,16 +402,18 @@
         return rv;
     }
 
-    rv = apr_reslist_create(&svr->dbpool, svr->nmin, svr->nkeep, svr->nmax,
+    rv = apr_reslist_create(&svr->reslist,
+                            svr->nmin, svr->nkeep, svr->nmax,
                             apr_time_from_sec(svr->exptime),
-                            dbd_construct, dbd_destruct, svr, svr->pool);
+                            dbd_construct, dbd_destruct, svr,
+                            svr->pool);
     if (rv == APR_SUCCESS) {
-        apr_pool_cleanup_register(svr->pool, svr->dbpool,
-                                  (void*)apr_reslist_destroy,
+        apr_pool_cleanup_register(svr->pool, svr->reslist,
+                                  (apr_status_t (*)(void*)) apr_reslist_destroy,
                                   apr_pool_cleanup_null);
     }
     else {
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, svr->pool,
+        ap_log_perror(APLOG_MARK, APLOG_ERR, rv, svr->pool,
                       "DBD: failed to initialise");
         apr_pool_destroy(svr->pool);
         svr->pool = NULL;
@@ -356,6 +421,7 @@
 
     return rv;
 }
+
 static apr_status_t dbd_setup_init(apr_pool_t *pool, server_rec *s)
 {
     svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
@@ -382,17 +448,20 @@
     /* we failed, so create a mutex so that subsequent competing callers
      * to ap_dbd_open can serialize themselves while they retry
      */
-    rv = apr_thread_mutex_create(&svr->mutex, APR_THREAD_MUTEX_DEFAULT, pool);
+    rv = apr_thread_mutex_create(&svr->mutex,
+                                 APR_THREAD_MUTEX_DEFAULT, pool);
     if (rv != APR_SUCCESS) {
         ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
                       "DBD: Failed to create thread mutex");
     }
+
     return rv;
 }
+
 static apr_status_t dbd_setup_lock(apr_pool_t *pool, server_rec *s)
 {
     svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
-    apr_status_t rv, rv2 = APR_SUCCESS;
+    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
@@ -402,52 +471,55 @@
         return APR_EGENERAL;
     }
 
-    rv = apr_thread_mutex_lock(svr->mutex);
-    if (rv != APR_SUCCESS) {
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
+    rv2 = apr_thread_mutex_lock(svr->mutex);
+    if (rv2 != APR_SUCCESS) {
+        ap_log_perror(APLOG_MARK, APLOG_ERR, rv2, pool,
                       "DBD: Failed to acquire thread mutex");
-        return rv;
+        return rv2;
     }
 
-    if (!svr->dbpool) {
-        rv2 = dbd_setup(s->process->pool, svr);
+    if (!svr->reslist) {
+        rv = dbd_setup(s->process->pool, svr);
     }
 
-    rv = apr_thread_mutex_unlock(svr->mutex);
-    if (rv != APR_SUCCESS) {
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
+    rv2 = apr_thread_mutex_unlock(svr->mutex);
+    if (rv2 != APR_SUCCESS) {
+        ap_log_perror(APLOG_MARK, APLOG_ERR, rv2, pool,
                       "DBD: Failed to release thread mutex");
-        if (rv2 == APR_SUCCESS) {
-            rv2 = rv;
+        if (rv == APR_SUCCESS) {
+            rv = rv2;
         }
     }
-    return 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 *sql)
+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->persist) {
-        dbd_close((void*) sql);
+        dbd_close((void*) rec);
     }
 #if APR_HAS_THREADS
     else {
-        apr_reslist_release(svr->dbpool, sql);
+        apr_reslist_release(svr->reslist, rec);
     }
 #endif
 }
+
 #define arec ((ap_dbd_t*)rec)
+
 #if APR_HAS_THREADS
 DBD_DECLARE_NONSTD(ap_dbd_t*) ap_dbd_open(apr_pool_t *pool, server_rec *s)
 {
-    void *rec = NULL;
     svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
+    void *rec = NULL;
     apr_status_t rv = APR_SUCCESS;
     const char *errmsg;
 
@@ -463,17 +535,19 @@
         return (rv == APR_SUCCESS) ? arec : NULL;
     }
 
-    if (!svr->dbpool) {
+    if (!svr->reslist) {
         if (dbd_setup_lock(pool, s) != APR_SUCCESS) {
             return NULL;
         }
     }
-    rv = apr_reslist_acquire(svr->dbpool, &rec);
+
+    rv = apr_reslist_acquire(svr->reslist, &rec);
     if (rv != APR_SUCCESS) {
         ap_log_perror(APLOG_MARK, APLOG_ERR, rv, pool,
                       "Failed to acquire DBD connection from pool!");
         return NULL;
     }
+
     rv = apr_dbd_check_conn(arec->driver, pool, arec->handle);
     if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
         errmsg = apr_dbd_error(arec->driver, arec->handle, rv);
@@ -481,8 +555,8 @@
             errmsg = "(unknown)";
         }
         ap_log_perror(APLOG_MARK, APLOG_ERR, rv, pool,
-                      "DBD[%s] Error: %s", svr->name, errmsg );
-        apr_reslist_invalidate(svr->dbpool, rec);
+                      "DBD [%s] Error: %s", svr->name, errmsg );
+        apr_reslist_invalidate(svr->reslist, rec);
         return NULL;
     }
     return arec;
@@ -490,10 +564,10 @@
 #else
 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);
+    void *rec = NULL;
     apr_status_t rv = APR_SUCCESS;
     const char *errmsg;
-    void *rec = NULL;
-    svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
 
     /* If nothing is configured, we shouldn't be here */
     if (svr->name == no_dbdriver) {
@@ -507,46 +581,51 @@
         return (rv == APR_SUCCESS) ? arec : NULL;
     }
 
-/* since we're in nothread-land, we can mess with svr->conn with impunity */
-/* If we have a persistent connection and it's good, we'll use it */
-    if (svr->conn) {
-        rv = apr_dbd_check_conn(svr->conn->driver, pool, svr->conn->handle);
+    /* 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 (svr->rec) {
+        rv = apr_dbd_check_conn(svr->rec->driver, pool, svr->rec->handle);
         if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
             errmsg = apr_dbd_error(arec->driver, arec->handle, rv);
             if (!errmsg) {
                 errmsg = "(unknown)";
             }
             ap_log_perror(APLOG_MARK, APLOG_ERR, rv, pool,
-                          "DBD[%s] Error: %s", svr->name, errmsg);
-            svr->conn = NULL;
+                          "DBD [%s] Error: %s", svr->name, errmsg);
+            svr->rec = NULL;
         }
     }
-/* We don't have a connection right now, so we'll open one */
-    if (!svr->conn) {
+
+    /* We don't have a connection right now, so we'll open one */
+    if (!svr->rec) {
         if (dbd_construct(&rec, svr, s->process->pool) == APR_SUCCESS) {
-            svr->conn = arec ;
-            apr_pool_cleanup_register(s->process->pool, svr->conn,
+            svr->rec = arec ;
+            apr_pool_cleanup_register(s->process->pool, svr->rec,
                                       dbd_close, apr_pool_cleanup_null);
         }
     }
-    return svr->conn;
+
+    return svr->rec;
 }
 #endif
+
 #if APR_HAS_THREADS
 typedef struct {
-    ap_dbd_t *conn;
-    apr_reslist_t *dbpool;
-} dbd_pool_rec;
-static apr_status_t dbd_release(void *REQ)
+    ap_dbd_t *rec;
+    apr_reslist_t *reslist;
+} dbd_acquire_t;
+
+static apr_status_t dbd_release(void *data)
 {
-    dbd_pool_rec *req = REQ;
-    apr_reslist_release(req->dbpool, req->conn);
+    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)
 {
-    svr_cfg *svr;
-    dbd_pool_rec *req;
+    dbd_acquire_t *acq;
 
     while (!ap_is_initial_req(r)) {
         if (r->prev) {
@@ -557,54 +636,60 @@
         }
     }
 
-    req = ap_get_module_config(r->request_config, &dbd_module);
-    if (!req) {
-        req = apr_palloc(r->pool, sizeof(dbd_pool_rec));
-        req->conn = ap_dbd_open(r->pool, r->server);
-        if (req->conn) {
-            svr = ap_get_module_config(r->server->module_config, &dbd_module);
-            ap_set_module_config(r->request_config, &dbd_module, req);
+    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->persist) {
-                req->dbpool = svr->dbpool;
-                apr_pool_cleanup_register(r->pool, req, dbd_release,
+                acq->reslist = svr->reslist;
+                apr_pool_cleanup_register(r->pool, acq, dbd_release,
                                           apr_pool_cleanup_null);
             }
             else {
-                apr_pool_cleanup_register(r->pool, req->conn, dbd_close,
+                apr_pool_cleanup_register(r->pool, acq->rec, dbd_close,
                                           apr_pool_cleanup_null);
             }
         }
     }
-    return req->conn;
+
+    return acq->rec;
 }
+
 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
 {
-    svr_cfg *svr;
-    dbd_pool_rec *req = ap_get_module_config(c->conn_config, &dbd_module);
-    if (!req) {
-        req = apr_palloc(c->pool, sizeof(dbd_pool_rec));
-        req->conn = ap_dbd_open(c->pool, c->base_server);
-        if (req->conn) {
-            svr = ap_get_module_config(c->base_server->module_config, &dbd_module);
-            ap_set_module_config(c->conn_config, &dbd_module, req);
+    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->persist) {
-                req->dbpool = svr->dbpool;
-                apr_pool_cleanup_register(c->pool, req, dbd_release,
+                acq->reslist = svr->reslist;
+                apr_pool_cleanup_register(c->pool, acq, dbd_release,
                                           apr_pool_cleanup_null);
             }
             else {
-                apr_pool_cleanup_register(c->pool, req->conn, dbd_close,
+                apr_pool_cleanup_register(c->pool, acq->rec, dbd_close,
                                           apr_pool_cleanup_null);
             }
         }
     }
-    return req->conn;
+
+    return acq->rec;
 }
 #else
 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
 {
-    svr_cfg *svr;
-    ap_dbd_t *ret;
+    ap_dbd_t *rec;
 
     while (!ap_is_initial_req(r)) {
         if (r->prev) {
@@ -615,80 +700,72 @@
         }
     }
 
-    ret = ap_get_module_config(r->request_config, &dbd_module);
-    if (!ret) {
-        svr = ap_get_module_config(r->server->module_config, &dbd_module);
-        ret = ap_dbd_open(r->pool, r->server);
-        if (ret) {
-            ap_set_module_config(r->request_config, &dbd_module, ret);
+    rec = ap_get_module_config(r->request_config, &dbd_module);
+    if (!rec) {
+        rec = ap_dbd_open(r->pool, r->server);
+        if (rec) {
+            svr_cfg *svr = ap_get_module_config(r->server->module_config,
+                                                &dbd_module);
+
+            ap_set_module_config(r->request_config, &dbd_module, rec);
+            /* if persist then ap_dbd_open registered cleanup on proc pool */
             if (!svr->persist) {
-                apr_pool_cleanup_register(r->pool, svr->conn, dbd_close,
+                apr_pool_cleanup_register(r->pool, svr->rec, dbd_close,
                                           apr_pool_cleanup_null);
             }
-            /* if persist then dbd_open registered cleanup on proc pool */
         }
     }
-    return ret;
+
+    return rec;
 }
+
 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
 {
-    svr_cfg *svr;
-    ap_dbd_t *ret = ap_get_module_config(c->conn_config, &dbd_module);
-    if (!ret) {
-        svr = ap_get_module_config(c->base_server->module_config, &dbd_module);
-        ret = ap_dbd_open(c->pool, c->base_server);
-        if (ret) {
-            ap_set_module_config(c->conn_config, &dbd_module, ret);
+    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) {
+            svr_cfg *svr = ap_get_module_config(c->base_server->module_config,
+                                                &dbd_module);
+
+            ap_set_module_config(c->conn_config, &dbd_module, rec);
+            /* if persist then ap_dbd_open registered cleanup on proc pool */
             if (!svr->persist) {
-                apr_pool_cleanup_register(c->pool, svr->conn, dbd_close,
+                apr_pool_cleanup_register(c->pool, svr->rec, dbd_close,
                                           apr_pool_cleanup_null);
             }
-            /* if persist then dbd_open registered cleanup on proc pool */
         }
     }
-    return ret;
+
+    return rec;
 }
 #endif
 
-static int dbd_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
-{
-   dbd_prepared_defns = apr_hash_make(ptemp);
-   return OK;
-}
-static int dbd_post_config(apr_pool_t *pconf, apr_pool_t *plog,
-                           apr_pool_t *ptemp, server_rec *s)
-{
-    svr_cfg *svr;
-    server_rec *sp;
-    for (sp = s; sp; sp = sp->next) {
-        const char *key = apr_psprintf(s->process->pool, "%pp", s);
-        svr = ap_get_module_config(sp->module_config, &dbd_module);
-        svr->prepared = apr_hash_get(dbd_prepared_defns, key,
-                                     APR_HASH_KEY_STRING);
-    }
-    return OK;
-}
 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);
 #if APR_HAS_THREADS
     ap_hook_child_init((void*)dbd_setup_init, NULL, NULL, APR_HOOK_MIDDLE);
 #endif
+
+    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_REGISTER_OPTIONAL_FN(ap_dbd_prepare);
+
     apr_dbd_init(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);
 }
 
 module AP_MODULE_DECLARE_DATA dbd_module = {
     STANDARD20_MODULE_STUFF,
     NULL,
     NULL,
-    dbd_cfg,
-    dbd_merge,
+    create_dbd_config,
+    merge_dbd_config,
     dbd_cmds,
     dbd_hooks
 };
+



Mime
View raw message