[Mod-auth-commit] mod_dbi_pool/src mod_dbi_pool.c,1.5,1.6
Brought to you by:
firechipmunk,
honx
From: <fir...@us...> - 2004-03-03 06:03:10
|
Update of /cvsroot/mod-auth/mod_dbi_pool/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1188/src Modified Files: mod_dbi_pool.c Log Message: the module now compiles. Index: mod_dbi_pool.c =================================================================== RCS file: /cvsroot/mod-auth/mod_dbi_pool/src/mod_dbi_pool.c,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- mod_dbi_pool.c 29 Feb 2004 08:48:27 -0000 1.5 +++ mod_dbi_pool.c 3 Mar 2004 05:42:56 -0000 1.6 @@ -35,13 +35,13 @@ #include <http_log.h> #include <apr_reslist.h> #include <apr_strings.h> +#include <apr_hash.h> #include "mod_dbi_pool.h" module AP_MODULE_DECLARE_DATA dbi_pool_module; -static int dbi_conn_count = 0; -static apr_hash_t *authn_dbi_config; +static apr_hash_t *dbi_pool_config; dbi_config *create_new_conf(conn_id conn_id, apr_pool_t * p) { @@ -59,9 +59,9 @@ conf->rec.conn_soft = DFLT_CONN_SOFT; conf->rec.conn_max = DFLT_CONN_MAX; conf->rec.conn_ttl = DFLT_CONN_TTL; - apr_hash_set(authn_dbi_config, conn_id, APR_HASH_KEY_STRING, conf); + apr_hash_set(dbi_pool_config, conn_id, APR_HASH_KEY_STRING, conf); ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, p, - "[mod_authn_dbi.c] Creating Config for %s", conn_id); + "[mod_dbi_pool.c] Creating Config for %s", conn_id); return conf; } @@ -81,7 +81,7 @@ return APR_EGENERAL; } } - temp = apr_hash_get(authn_dbi_config, conn_id, APR_HASH_KEY_STRING); + temp = apr_hash_get(dbi_pool_config, conn_id, APR_HASH_KEY_STRING); if (temp == NULL) { /* no such server yet... */ temp = create_new_conf(conn_id, p); @@ -90,11 +90,6 @@ return APR_SUCCESS; } -#define ISINT(val) \ - for ( p = val; *p; ++p) \ - if ( ! isdigit(*p) ) \ - return "Argument must be numeric!" - static const char *set_dbi_switch_conf(cmd_parms * cmd, void *config, const char *conn_id, const char *value) { @@ -122,7 +117,7 @@ temp->rec.dbi_pass = value; break; case CONF_DBI_DBNAME: - temp->rec.dbi_name = value; + temp->rec.dbi_dbname = value; break; case CONF_DBI_CONN_MIN: temp->rec.conn_min = atoi(value); @@ -134,7 +129,6 @@ temp->rec.conn_max = atoi(value); break; case CONF_DBI_CONN_TTL: - ISINT(value); temp->rec.conn_ttl = atoi(value); break; default: @@ -145,14 +139,11 @@ return NULL; } -static const command_rec authn_dbi_cmds[] = { +static const command_rec dbi_pool_cmds[] = { /* global config items */ AP_INIT_TAKE2("PoolDbiDriver", set_dbi_switch_conf, (void *) CONF_DBI_DRIVER, RSRC_CONF, "The DBI Driver"), - AP_INIT_TAKE1("PoolDbiDriverDir", set_dbi_driverdir, - (void *) CONF_DBI_DRIVER_DIR, RSRC_CONF, - "The directory containing the DBI drivers"), AP_INIT_TAKE2("PoolDbiHost", set_dbi_switch_conf, (void *) CONF_DBI_HOST, RSRC_CONF, "The host for the database connection"), @@ -163,7 +154,7 @@ (void *) CONF_DBI_PASSWORD, RSRC_CONF, "The password for the database connection"), AP_INIT_TAKE2("PoolDbiDBName", set_dbi_switch_conf, - (void *) CONF_DBI_NAME, + (void *) CONF_DBI_DBNAME, RSRC_CONF, "The name of the database containing the tables"), AP_INIT_TAKE2("PoolDbiConnMin", set_dbi_switch_conf, @@ -185,26 +176,40 @@ /************ svr cfg: manage db connection pool ****************/ /* an apr_reslist_constructor for DBI connections */ + static apr_status_t dbipool_construct(void **db, void *params, apr_pool_t * pool) { - svr_cfg *svr = (svr_cfg *) params; + dbi_config_rec *conf = params; int err_num = 0; const char *err_str; - *db = dbi_conn_new(svr->dbi_driver); - dbi_conn_set_option(*db, "host", svr->host); - dbi_conn_set_option(*db, "username", svr->user); - dbi_conn_set_option(*db, "password", svr->pass); - dbi_conn_set_option(*db, "dbname", svr->db); + *db = dbi_conn_new(conf->dbi_driver); + + // Test for a Fatal Error on Creating the Driver + if(*db == NULL) { + ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, pool, + "[mod_dbi_pool.c] DBI Connection Failed. dbi_conn_new returned NULL. Insufficient memory or invalid DBD?"); + /** + * modules/ssl/ssl_engine_log.c:103 + * said this was okay. so i do it too. + */ + exit(1); + } + + dbi_conn_set_option(*db, "host", conf->dbi_host); + dbi_conn_set_option(*db, "username", conf->dbi_user); + dbi_conn_set_option(*db, "password", conf->dbi_pass); + dbi_conn_set_option(*db, "dbname", conf->dbi_dbname); if (dbi_conn_connect(*db) != 0) { err_num = dbi_conn_error(*db, &err_str); /* Connetion Failed */ ap_log_perror(APLOG_MARK, APLOG_CRIT, 0, pool, "[mod_dbi_pool.c] DBI Connection to %s://%s@%s/%s Failed. Error: (%d) %s", - svr->dbi_driver, svr->user, svr->host, svr->db, err_num, - err_str); + conf->dbi_driver, conf->dbi_user, + conf->dbi_host, conf->dbi_dbname, + err_num, err_str); return APR_EGENERAL; } @@ -217,36 +222,88 @@ return APR_SUCCESS; } -static int setup_db_pool(apr_pool_t * p, apr_pool_t * plog, +static int dbi_pool_post_config(apr_pool_t * p, apr_pool_t * plog, apr_pool_t * ptemp, server_rec * s) { - svr_cfg *svr = - (svr_cfg *) ap_get_module_config(s->module_config, &dbi_pool_module); + int rval; + apr_hash_index_t *idx; + char *key; + dbi_config *val; + apr_ssize_t len; + void *data; + const char *userdata_key = "mod_dbi_pool_init"; - if (apr_reslist_create(&svr->dbpool, svr->nmin, svr->nkeep, - svr->nmax, svr->exptime, dbipool_construct, - dbipool_destruct, svr, p) != APR_SUCCESS) { - ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, - "DBIPool: failed to initialise"); - return 500; + apr_pool_userdata_get(&data, userdata_key, s->process->pool); + if (!data) { + apr_pool_userdata_set((const void *) 1, userdata_key, + apr_pool_cleanup_null, s->process->pool); + return OK; } - apr_pool_cleanup_register(p, svr->dbpool, - (void *) apr_reslist_destroy, - apr_pool_cleanup_null); + + ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, p, + "[mod_dbi_pool.c] Running DBI Pool Init"); + if ((rval = dbi_initialize(NULL)) > 0) { + ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, p, + "[mod_dbi_pool.c] Initialization of libdbi found %d drivers in default driver directory", + rval); + } + else { /* An error was returned or libdbi found 0 drivers */ + ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, p, + "[mod_dbi_pool.c] - Initlialization of libdbi with default driver directory failed"); + return APR_EDSOOPEN; + } + + /* loop the hashed config stuff... */ + for (idx = apr_hash_first(p, dbi_pool_config); idx; + idx = apr_hash_next(idx)) { + apr_hash_this(idx, (void *) &key, &len, (void *) &val); + if(apr_reslist_create(&val->pool, val->rec.conn_min, /* hard minimum */ + val->rec.conn_soft, /* soft maximum */ + val->rec.conn_max, /* hard maximum */ + val->rec.conn_ttl, /* Time to live -- dbi server might override/disconnect! */ + dbipool_construct, /* Make a New Connection */ + dbipool_destruct, /* Kill Old Connection */ + (void *) &val->rec, p) != APR_SUCCESS) + ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, p, + "[mod_dbi_pool.c] - Creation of dbi connection pool failed for config set %s", + key); + /* XXX: If no new connection could be opened, the reslist will not + contain any resources and reslist_create will fail, giving + val->pool the value NULL and making it unusable. The configuration + set is not really recoverable in this case. For now we log this condition + and the authentication functions protect themselves against this + condition. A much better way would be to free the configuration + set completely. But to do this other changes are necessary, so for + now this is basically a workaround. This has to be fixed before + 1.0 */ + apr_hash_set(dbi_pool_config, key, APR_HASH_KEY_STRING, val); + } + return OK; } +static apr_status_t dbi_pool_pre_config(apr_pool_t * pconf, + apr_pool_t * plog, + apr_pool_t * ptemp) +{ + apr_status_t rv = APR_SUCCESS; + + /* create our globalish config var */ + dbi_pool_config = apr_hash_make(pconf); + return rv; +} static void dbi_pool_hooks(apr_pool_t * p) { - ap_hook_post_config(setup_db_pool, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_pre_config(dbi_pool_pre_config, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_post_config(dbi_pool_post_config, NULL, NULL, APR_HOOK_MIDDLE); } module AP_MODULE_DECLARE_DATA dbi_pool_module = { STANDARD20_MODULE_STUFF, NULL, NULL, - dbi_pool_cfg, + NULL, NULL, dbi_pool_cmds, dbi_pool_hooks @@ -257,22 +314,19 @@ - close releases it back in to the pool */ -dbi_conn *dbipool_open(server_rec * s) +dbi_conn *dbipool_open(conn_id* id) { dbi_conn *ret = NULL; - svr_cfg *svr = - (svr_cfg *) ap_get_module_config(s->module_config, &dbi_pool_module); - if (apr_reslist_acquire(svr->dbpool, (void **) &ret) != APR_SUCCESS) { - ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, - "Failed to acquire DBI connection from pool!"); + dbi_config *conf = apr_hash_get(dbi_pool_config, id, APR_HASH_KEY_STRING); + + if (apr_reslist_acquire(conf->pool, (void **) &ret) != APR_SUCCESS) { return NULL; } return ret; } -void dbipool_close(server_rec * s, dbi_conn * sql) -{ - svr_cfg *svr = - (svr_cfg *) ap_get_module_config(s->module_config, &dbi_pool_module); - apr_reslist_release(svr->dbpool, sql); +void dbipool_close(conn_id* id, dbi_conn* conn) { + dbi_config *conf = apr_hash_get(dbi_pool_config, id, APR_HASH_KEY_STRING); + apr_reslist_release(conf->pool, (void **) conn); } + |