/[Apache-SVN]/httpd/httpd/trunk/modules/database/mod_dbd.c
ViewVC logotype

Contents of /httpd/httpd/trunk/modules/database/mod_dbd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 766938 - (show annotations)
Tue Apr 21 01:29:15 2009 UTC (7 months ago) by niq
File MIME type: text/plain
File size: 29809 byte(s)
mod_dbd: add DBDInitSQL command.  PR 46827
Patch by Marko Kevac.
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 "apr_reslist.h"
24 #include "apr_strings.h"
25 #include "apr_hash.h"
26 #include "apr_tables.h"
27 #include "apr_lib.h"
28 #include "apr_dbd.h"
29
30 #define APR_WANT_MEMFUNC
31 #define APR_WANT_STRFUNC
32 #include "apr_want.h"
33
34 #include "http_protocol.h"
35 #include "http_config.h"
36 #include "http_log.h"
37 #include "http_request.h"
38 #include "mod_dbd.h"
39
40 extern module AP_MODULE_DECLARE_DATA dbd_module;
41
42 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(dbd, AP, apr_status_t, post_connect,
43 (apr_pool_t *pool, dbd_cfg_t *cfg,
44 ap_dbd_t *dbd),
45 (pool, cfg, dbd), OK, DECLINED)
46
47 /************ svr cfg: manage db connection pool ****************/
48
49 #define NMIN_SET 0x1
50 #define NKEEP_SET 0x2
51 #define NMAX_SET 0x4
52 #define EXPTIME_SET 0x8
53
54 typedef struct dbd_group_t dbd_group_t;
55
56 struct dbd_group_t {
57 dbd_cfg_t *cfg;
58 dbd_group_t *next;
59 apr_pool_t *pool;
60 #if APR_HAS_THREADS
61 apr_thread_mutex_t *mutex;
62 apr_reslist_t *reslist;
63 int destroyed;
64 #else
65 ap_dbd_t *rec;
66 #endif
67 };
68
69 typedef struct {
70 dbd_cfg_t *cfg;
71 dbd_group_t *group;
72 } svr_cfg;
73
74 typedef enum { cmd_name, cmd_params, cmd_persist,
75 cmd_min, cmd_keep, cmd_max, cmd_exp
76 } cmd_parts;
77
78 static apr_pool_t *config_pool;
79 static dbd_group_t *group_list;
80
81 /* a default DBDriver value that'll generate meaningful error messages */
82 static const char *const no_dbdriver = "[DBDriver unset]";
83
84 /* A default nmin of >0 will help with generating meaningful
85 * startup error messages if the database is down.
86 */
87 #define DEFAULT_NMIN 1
88 #define DEFAULT_NKEEP 2
89 #define DEFAULT_NMAX 10
90 #define DEFAULT_EXPTIME 300
91
92 #define DEFAULT_SQL_INIT_ARRAY_SIZE 5
93
94 static void *create_dbd_config(apr_pool_t *pool, server_rec *s)
95 {
96 svr_cfg *svr = apr_pcalloc(pool, sizeof(svr_cfg));
97 dbd_cfg_t *cfg = svr->cfg = apr_pcalloc(pool, sizeof(dbd_cfg_t));
98
99 cfg->server = s;
100 cfg->name = no_dbdriver; /* to generate meaningful error messages */
101 cfg->params = ""; /* don't risk segfault on misconfiguration */
102 cfg->persist = -1;
103 #if APR_HAS_THREADS
104 cfg->nmin = DEFAULT_NMIN;
105 cfg->nkeep = DEFAULT_NKEEP;
106 cfg->nmax = DEFAULT_NMAX;
107 cfg->exptime = DEFAULT_EXPTIME;
108 #endif
109 cfg->queries = apr_hash_make(pool);
110 cfg->init_queries = apr_array_make(pool, DEFAULT_SQL_INIT_ARRAY_SIZE,
111 sizeof(const char *));
112
113 return svr;
114 }
115
116 static void *merge_dbd_config(apr_pool_t *pool, void *basev, void *addv)
117 {
118 dbd_cfg_t *base = ((svr_cfg*) basev)->cfg;
119 dbd_cfg_t *add = ((svr_cfg*) addv)->cfg;
120 svr_cfg *svr = apr_pcalloc(pool, sizeof(svr_cfg));
121 dbd_cfg_t *new = svr->cfg = apr_pcalloc(pool, sizeof(dbd_cfg_t));
122
123 new->server = add->server;
124 new->name = (add->name != no_dbdriver) ? add->name : base->name;
125 new->params = strcmp(add->params, "") ? add->params : base->params;
126 new->persist = (add->persist != -1) ? add->persist : base->persist;
127 #if APR_HAS_THREADS
128 new->nmin = (add->set&NMIN_SET) ? add->nmin : base->nmin;
129 new->nkeep = (add->set&NKEEP_SET) ? add->nkeep : base->nkeep;
130 new->nmax = (add->set&NMAX_SET) ? add->nmax : base->nmax;
131 new->exptime = (add->set&EXPTIME_SET) ? add->exptime : base->exptime;
132 #endif
133 new->queries = apr_hash_overlay(pool, add->queries, base->queries);
134 new->init_queries = apr_array_append(pool, add->init_queries,
135 base->init_queries);
136
137 return svr;
138 }
139
140 static void ap_dbd_sql_init(server_rec *s, const char *query)
141 {
142 svr_cfg *svr;
143 const char **arr_item;
144
145 svr = ap_get_module_config(s->module_config, &dbd_module);
146 if (!svr) {
147 /* some modules may call from within config directive handlers, and
148 * if these are called in a server context that contains no mod_dbd
149 * config directives, then we have to create our own server config
150 */
151 svr = create_dbd_config(config_pool, s);
152 ap_set_module_config(s->module_config, &dbd_module, svr);
153 }
154
155 if (query) {
156 arr_item = apr_array_push(svr->cfg->init_queries);
157 *arr_item = query;
158 }
159 }
160
161 static const char *dbd_param(cmd_parms *cmd, void *dconf, const char *val)
162 {
163 const apr_dbd_driver_t *driver = NULL;
164 svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
165 &dbd_module);
166 dbd_cfg_t *cfg = svr->cfg;
167
168 switch ((long) cmd->info) {
169 case cmd_name:
170 cfg->name = val;
171 /* loading the driver involves once-only dlloading that is
172 * best done at server startup. This also guarantees that
173 * we won't return an error later.
174 */
175 switch (apr_dbd_get_driver(cmd->pool, cfg->name, &driver)) {
176 case APR_ENOTIMPL:
177 return apr_psprintf(cmd->pool, "DBD: No driver for %s", cfg->name);
178 case APR_EDSOOPEN:
179 return apr_psprintf(cmd->pool,
180 #ifdef NETWARE
181 "DBD: Can't load driver file dbd%s.nlm",
182 #else
183 "DBD: Can't load driver file apr_dbd_%s.so",
184 #endif
185 cfg->name);
186 case APR_ESYMNOTFOUND:
187 return apr_psprintf(cmd->pool,
188 "DBD: Failed to load driver apr_dbd_%s_driver",
189 cfg->name);
190 }
191 break;
192 case cmd_params:
193 cfg->params = val;
194 break;
195 }
196
197 return NULL;
198 }
199
200 #if APR_HAS_THREADS
201 static const char *dbd_param_int(cmd_parms *cmd, void *dconf, const char *val)
202 {
203 svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
204 &dbd_module);
205 dbd_cfg_t *cfg = svr->cfg;
206 const char *p;
207
208 for (p = val; *p; ++p) {
209 if (!apr_isdigit(*p)) {
210 return "Argument must be numeric!";
211 }
212 }
213
214 switch ((long) cmd->info) {
215 case cmd_min:
216 cfg->nmin = atoi(val);
217 cfg->set |= NMIN_SET;
218 break;
219 case cmd_keep:
220 cfg->nkeep = atoi(val);
221 cfg->set |= NKEEP_SET;
222 break;
223 case cmd_max:
224 cfg->nmax = atoi(val);
225 cfg->set |= NMAX_SET;
226 break;
227 case cmd_exp:
228 cfg->exptime = atoi(val);
229 cfg->set |= EXPTIME_SET;
230 break;
231 }
232
233 return NULL;
234 }
235 #endif
236
237 static const char *dbd_param_flag(cmd_parms *cmd, void *dconf, int flag)
238 {
239 svr_cfg *svr = ap_get_module_config(cmd->server->module_config,
240 &dbd_module);
241
242 switch ((long) cmd->info) {
243 case cmd_persist:
244 svr->cfg->persist = flag;
245 break;
246 }
247
248 return NULL;
249 }
250
251 static const char *dbd_prepare(cmd_parms *cmd, void *dconf, const char *query,
252 const char *label)
253 {
254 if (!label) {
255 label = query;
256 query = "";
257 }
258
259 ap_dbd_prepare(cmd->server, query, label);
260
261 return NULL;
262 }
263
264 static const char *dbd_init_sql(cmd_parms *cmd, void *dconf, const char *query)
265 {
266 if (!query || *query == '\n') {
267 return "You should specify SQL statement";
268 }
269
270 ap_dbd_sql_init(cmd->server, query);
271
272 return NULL;
273 }
274
275 static const command_rec dbd_cmds[] = {
276 AP_INIT_TAKE1("DBDriver", dbd_param, (void*)cmd_name, RSRC_CONF,
277 "SQL Driver"),
278 AP_INIT_TAKE1("DBDParams", dbd_param, (void*)cmd_params, RSRC_CONF,
279 "SQL Driver Params"),
280 AP_INIT_FLAG("DBDPersist", dbd_param_flag, (void*)cmd_persist, RSRC_CONF,
281 "Use persistent connection/pool"),
282 AP_INIT_TAKE12("DBDPrepareSQL", dbd_prepare, NULL, RSRC_CONF,
283 "SQL statement to prepare (or nothing, to override "
284 "statement inherited from main server) and label"),
285 AP_INIT_TAKE1("DBDInitSQL", dbd_init_sql, NULL, RSRC_CONF,
286 "SQL statement to be executed after connection is created"),
287 #if APR_HAS_THREADS
288 AP_INIT_TAKE1("DBDMin", dbd_param_int, (void*)cmd_min, RSRC_CONF,
289 "Minimum number of connections"),
290 /* XXX: note that mod_proxy calls this "smax" */
291 AP_INIT_TAKE1("DBDKeep", dbd_param_int, (void*)cmd_keep, RSRC_CONF,
292 "Maximum number of sustained connections"),
293 AP_INIT_TAKE1("DBDMax", dbd_param_int, (void*)cmd_max, RSRC_CONF,
294 "Maximum number of connections"),
295 /* XXX: note that mod_proxy calls this "ttl" (time to live) */
296 AP_INIT_TAKE1("DBDExptime", dbd_param_int, (void*)cmd_exp, RSRC_CONF,
297 "Keepalive time for idle connections"),
298 #endif
299 {NULL}
300 };
301
302 static int dbd_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
303 apr_pool_t *ptemp)
304 {
305 config_pool = pconf;
306 group_list = NULL;
307 return OK;
308 }
309
310 DBD_DECLARE_NONSTD(void) ap_dbd_prepare(server_rec *s, const char *query,
311 const char *label)
312 {
313 svr_cfg *svr;
314
315 svr = ap_get_module_config(s->module_config, &dbd_module);
316 if (!svr) {
317 /* some modules may call from within config directive handlers, and
318 * if these are called in a server context that contains no mod_dbd
319 * config directives, then we have to create our own server config
320 */
321 svr = create_dbd_config(config_pool, s);
322 ap_set_module_config(s->module_config, &dbd_module, svr);
323 }
324
325 if (apr_hash_get(svr->cfg->queries, label, APR_HASH_KEY_STRING)
326 && strcmp(query, "")) {
327 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
328 "conflicting SQL statements with label %s", label);
329 }
330
331 apr_hash_set(svr->cfg->queries, label, APR_HASH_KEY_STRING, query);
332 }
333
334 typedef struct {
335 const char *label, *query;
336 } dbd_query_t;
337
338 static int dbd_post_config(apr_pool_t *pconf, apr_pool_t *plog,
339 apr_pool_t *ptemp, server_rec *s)
340 {
341 server_rec *sp;
342 apr_array_header_t *add_queries = apr_array_make(ptemp, 10,
343 sizeof(dbd_query_t));
344
345 for (sp = s; sp; sp = sp->next) {
346 svr_cfg *svr = ap_get_module_config(sp->module_config, &dbd_module);
347 dbd_cfg_t *cfg = svr->cfg;
348 apr_hash_index_t *hi_first = apr_hash_first(ptemp, cfg->queries);
349 dbd_group_t *group;
350
351 /* dbd_setup in 2.2.3 and under was causing spurious error messages
352 * when dbd isn't configured. We can stop that with a quick check here
353 * together with a similar check in ap_dbd_open (where being
354 * unconfigured is a genuine error that must be reported).
355 */
356 if (cfg->name == no_dbdriver || !cfg->persist) {
357 continue;
358 }
359
360 for (group = group_list; group; group = group->next) {
361 dbd_cfg_t *group_cfg = group->cfg;
362 apr_hash_index_t *hi;
363 int group_ok = 1;
364
365 if (strcmp(cfg->name, group_cfg->name)
366 || strcmp(cfg->params, group_cfg->params)) {
367 continue;
368 }
369
370 #if APR_HAS_THREADS
371 if (cfg->nmin != group_cfg->nmin
372 || cfg->nkeep != group_cfg->nkeep
373 || cfg->nmax != group_cfg->nmax
374 || cfg->exptime != group_cfg->exptime) {
375 continue;
376 }
377 #endif
378
379 add_queries->nelts = 0;
380
381 for (hi = hi_first; hi; hi = apr_hash_next(hi)) {
382 const char *label, *query;
383 const char *group_query;
384
385 apr_hash_this(hi, (void*) &label, NULL, (void*) &query);
386
387 group_query = apr_hash_get(group_cfg->queries, label,
388 APR_HASH_KEY_STRING);
389
390 if (!group_query) {
391 dbd_query_t *add_query = apr_array_push(add_queries);
392
393 add_query->label = label;
394 add_query->query = query;
395 }
396 else if (strcmp(query, group_query)) {
397 group_ok = 0;
398 break;
399 }
400 }
401
402 if (group_ok) {
403 int i;
404
405 for (i = 0; i < add_queries->nelts; ++i) {
406 dbd_query_t *add_query = ((dbd_query_t*) add_queries->elts)
407 + i;
408
409 apr_hash_set(group_cfg->queries, add_query->label,
410 APR_HASH_KEY_STRING, add_query->query);
411 }
412
413 svr->group = group;
414 break;
415 }
416 }
417
418 if (!svr->group) {
419 svr->group = group = apr_pcalloc(pconf, sizeof(dbd_group_t));
420
421 group->cfg = cfg;
422
423 group->next = group_list;
424 group_list = group;
425 }
426 }
427
428 return OK;
429 }
430
431 static apr_status_t dbd_prepared_init(apr_pool_t *pool, dbd_cfg_t *cfg,
432 ap_dbd_t *rec)
433 {
434 apr_hash_index_t *hi;
435 apr_status_t rv = APR_SUCCESS;
436
437 rec->prepared = apr_hash_make(pool);
438
439 for (hi = apr_hash_first(pool, cfg->queries); hi;
440 hi = apr_hash_next(hi)) {
441 const char *label, *query;
442 apr_dbd_prepared_t *stmt;
443
444 apr_hash_this(hi, (void*) &label, NULL, (void*) &query);
445
446 if (!strcmp(query, "")) {
447 continue;
448 }
449
450 stmt = NULL;
451 if (apr_dbd_prepare(rec->driver, pool, rec->handle, query,
452 label, &stmt)) {
453 rv = APR_EGENERAL;
454 }
455 else {
456 apr_hash_set(rec->prepared, label, APR_HASH_KEY_STRING, stmt);
457 }
458 }
459
460 return rv;
461 }
462
463 static apr_status_t dbd_init_sql_init(apr_pool_t *pool, dbd_cfg_t *cfg,
464 ap_dbd_t *rec)
465 {
466 int i;
467 apr_status_t rv = APR_SUCCESS;
468
469 for (i = 0; i < cfg->init_queries->nelts; i++) {
470 int nrows;
471 char **query_p;
472
473 query_p = (char **)cfg->init_queries->elts + i;
474
475 if (apr_dbd_query(rec->driver, rec->handle, &nrows, *query_p)) {
476 rv = APR_EGENERAL;
477 break;
478 }
479 }
480
481 return rv;
482 }
483
484 static apr_status_t dbd_close(void *data)
485 {
486 ap_dbd_t *rec = data;
487
488 return apr_dbd_close(rec->driver, rec->handle);
489 }
490
491 #if APR_HAS_THREADS
492 static apr_status_t dbd_destruct(void *data, void *params, apr_pool_t *pool)
493 {
494 dbd_group_t *group = params;
495
496 if (!group->destroyed) {
497 ap_dbd_t *rec = data;
498
499 apr_pool_destroy(rec->pool);
500 }
501
502 return APR_SUCCESS;
503 }
504 #endif
505
506 /* an apr_reslist_constructor for SQL connections
507 * Also use this for opening in non-reslist modes, since it gives
508 * us all the error-handling in one place.
509 */
510 static apr_status_t dbd_construct(void **data_ptr,
511 void *params, apr_pool_t *pool)
512 {
513 dbd_group_t *group = params;
514 dbd_cfg_t *cfg = group->cfg;
515 apr_pool_t *rec_pool, *prepared_pool;
516 ap_dbd_t *rec;
517 apr_status_t rv;
518 const char *err = "";
519
520 rv = apr_pool_create(&rec_pool, pool);
521 if (rv != APR_SUCCESS) {
522 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, cfg->server,
523 "DBD: Failed to create memory pool");
524 return rv;
525 }
526
527 rec = apr_pcalloc(rec_pool, sizeof(ap_dbd_t));
528
529 rec->pool = rec_pool;
530
531 /* The driver is loaded at config time now, so this just checks a hash.
532 * If that changes, the driver DSO could be registered to unload against
533 * our pool, which is probably not what we want. Error checking isn't
534 * necessary now, but in case that changes in the future ...
535 */
536 rv = apr_dbd_get_driver(rec->pool, cfg->name, &rec->driver);
537 if (rv != APR_SUCCESS) {
538 switch (rv) {
539 case APR_ENOTIMPL:
540 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
541 "DBD: driver for %s not available", cfg->name);
542 break;
543 case APR_EDSOOPEN:
544 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
545 "DBD: can't find driver for %s", cfg->name);
546 break;
547 case APR_ESYMNOTFOUND:
548 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
549 "DBD: driver for %s is invalid or corrupted",
550 cfg->name);
551 break;
552 default:
553 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
554 "DBD: mod_dbd not compatible with APR in get_driver");
555 break;
556 }
557
558 apr_pool_destroy(rec->pool);
559 return rv;
560 }
561
562 rv = apr_dbd_open_ex(rec->driver, rec->pool, cfg->params, &rec->handle, &err);
563 if (rv != APR_SUCCESS) {
564 switch (rv) {
565 case APR_EGENERAL:
566 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
567 "DBD: Can't connect to %s: %s", cfg->name, err);
568 break;
569 default:
570 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
571 "DBD: mod_dbd not compatible with APR in open");
572 break;
573 }
574
575 apr_pool_destroy(rec->pool);
576 return rv;
577 }
578
579 apr_pool_cleanup_register(rec->pool, rec, dbd_close,
580 apr_pool_cleanup_null);
581
582 /* we use a sub-pool for the prepared statements for each connection so
583 * that they will be cleaned up first, before the connection is closed
584 */
585 rv = apr_pool_create(&prepared_pool, rec->pool);
586 if (rv != APR_SUCCESS) {
587 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, cfg->server,
588 "DBD: Failed to create memory pool");
589
590 apr_pool_destroy(rec->pool);
591 return rv;
592 }
593
594 rv = dbd_prepared_init(prepared_pool, cfg, rec);
595 if (rv != APR_SUCCESS) {
596 const char *errmsg = apr_dbd_error(rec->driver, rec->handle, rv);
597 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cfg->server,
598 "DBD: failed to prepare SQL statements: %s",
599 (errmsg ? errmsg : "[???]"));
600
601 apr_pool_destroy(rec->pool);
602 return rv;
603 }
604
605 dbd_run_post_connect(prepared_pool, cfg, rec);
606
607 *data_ptr = rec;
608
609 return APR_SUCCESS;
610 }
611
612 #if APR_HAS_THREADS
613 static apr_status_t dbd_destroy(void *data)
614 {
615 dbd_group_t *group = data;
616
617 group->destroyed = 1;
618
619 return APR_SUCCESS;
620 }
621
622 static apr_status_t dbd_setup(server_rec *s, dbd_group_t *group)
623 {
624 dbd_cfg_t *cfg = group->cfg;
625 apr_status_t rv;
626
627 /* We create the reslist using a sub-pool of the pool passed to our
628 * child_init hook. No other threads can be here because we're
629 * either in the child_init phase or dbd_setup_lock() acquired our mutex.
630 * No other threads will use this sub-pool after this, except via
631 * reslist calls, which have an internal mutex.
632 *
633 * We need to short-circuit the cleanup registered internally by
634 * apr_reslist_create(). We do this by registering dbd_destroy()
635 * as a cleanup afterwards, so that it will run before the reslist's
636 * internal cleanup.
637 *
638 * If we didn't do this, then we could free memory twice when the pool
639 * was destroyed. When apr_pool_destroy() runs, it first destroys all
640 * all the per-connection sub-pools created in dbd_construct(), and
641 * then it runs the reslist's cleanup. The cleanup calls dbd_destruct()
642 * on each resource, which would then attempt to destroy the sub-pools
643 * a second time.
644 */
645 rv = apr_reslist_create(&group->reslist,
646 cfg->nmin, cfg->nkeep, cfg->nmax,
647 apr_time_from_sec(cfg->exptime),
648 dbd_construct, dbd_destruct, group,
649 group->pool);
650 if (rv != APR_SUCCESS) {
651 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
652 "DBD: failed to initialise");
653 return rv;
654 }
655
656 apr_pool_cleanup_register(group->pool, group, dbd_destroy,
657 apr_pool_cleanup_null);
658
659 return APR_SUCCESS;
660 }
661 #endif
662
663 static apr_status_t dbd_setup_init(apr_pool_t *pool, server_rec *s)
664 {
665 dbd_group_t *group;
666 apr_status_t rv = APR_SUCCESS;
667
668 for (group = group_list; group; group = group->next) {
669 apr_status_t rv2;
670
671 rv2 = apr_pool_create(&group->pool, pool);
672 if (rv2 != APR_SUCCESS) {
673 ap_log_error(APLOG_MARK, APLOG_CRIT, rv2, s,
674 "DBD: Failed to create reslist cleanup memory pool");
675 return rv2;
676 }
677
678 #if APR_HAS_THREADS
679 rv2 = dbd_setup(s, group);
680 if (rv2 == APR_SUCCESS) {
681 continue;
682 }
683 else if (rv == APR_SUCCESS) {
684 rv = rv2;
685 }
686
687 /* we failed, so create a mutex so that subsequent competing callers
688 * to ap_dbd_open can serialize themselves while they retry
689 */
690 rv2 = apr_thread_mutex_create(&group->mutex,
691 APR_THREAD_MUTEX_DEFAULT, pool);
692 if (rv2 != APR_SUCCESS) {
693 ap_log_error(APLOG_MARK, APLOG_CRIT, rv2, s,
694 "DBD: Failed to create thread mutex");
695 return rv2;
696 }
697 #endif
698 }
699
700 return rv;
701 }
702
703 static void dbd_child_init(apr_pool_t *p, server_rec *s)
704 {
705 apr_status_t rv = dbd_setup_init(p, s);
706 if (rv) {
707 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
708 "DBD: child init failed!");
709 }
710 }
711
712 #if APR_HAS_THREADS
713 static apr_status_t dbd_setup_lock(server_rec *s, dbd_group_t *group)
714 {
715 apr_status_t rv = APR_SUCCESS, rv2;
716
717 /* several threads could be here at the same time, all trying to
718 * initialize the reslist because dbd_setup_init failed to do so
719 */
720 if (!group->mutex) {
721 /* we already logged an error when the mutex couldn't be created */
722 return APR_EGENERAL;
723 }
724
725 rv2 = apr_thread_mutex_lock(group->mutex);
726 if (rv2 != APR_SUCCESS) {
727 ap_log_error(APLOG_MARK, APLOG_ERR, rv2, s,
728 "DBD: Failed to acquire thread mutex");
729 return rv2;
730 }
731
732 if (!group->reslist) {
733 rv = dbd_setup(s, group);
734 }
735
736 rv2 = apr_thread_mutex_unlock(group->mutex);
737 if (rv2 != APR_SUCCESS) {
738 ap_log_error(APLOG_MARK, APLOG_ERR, rv2, s,
739 "DBD: Failed to release thread mutex");
740 if (rv == APR_SUCCESS) {
741 rv = rv2;
742 }
743 }
744
745 return rv;
746 }
747 #endif
748
749 /* Functions we export for modules to use:
750 - open acquires a connection from the pool (opens one if necessary)
751 - close releases it back in to the pool
752 */
753 DBD_DECLARE_NONSTD(void) ap_dbd_close(server_rec *s, ap_dbd_t *rec)
754 {
755 svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
756
757 if (!svr->cfg->persist) {
758 apr_pool_destroy(rec->pool);
759 }
760 #if APR_HAS_THREADS
761 else {
762 apr_reslist_release(svr->group->reslist, rec);
763 }
764 #endif
765 }
766
767 static apr_status_t dbd_check(apr_pool_t *pool, server_rec *s, ap_dbd_t *rec)
768 {
769 svr_cfg *svr;
770 apr_status_t rv = apr_dbd_check_conn(rec->driver, pool, rec->handle);
771 const char *errmsg;
772
773 if ((rv == APR_SUCCESS) || (rv == APR_ENOTIMPL)) {
774 return APR_SUCCESS;
775 }
776
777 /* we don't have a driver-specific error code, so we'll just pass
778 * a "success" value and rely on the driver to ignore it
779 */
780 errmsg = apr_dbd_error(rec->driver, rec->handle, 0);
781 if (!errmsg) {
782 errmsg = "(unknown)";
783 }
784
785 svr = ap_get_module_config(s->module_config, &dbd_module);
786 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
787 "DBD [%s] Error: %s", svr->cfg->name, errmsg);
788 return rv;
789 }
790
791 DBD_DECLARE_NONSTD(ap_dbd_t*) ap_dbd_open(apr_pool_t *pool, server_rec *s)
792 {
793 svr_cfg *svr = ap_get_module_config(s->module_config, &dbd_module);
794 dbd_group_t *group = svr->group;
795 dbd_cfg_t *cfg = svr->cfg;
796 ap_dbd_t *rec = NULL;
797 #if APR_HAS_THREADS
798 apr_status_t rv;
799 #endif
800
801 /* If nothing is configured, we shouldn't be here */
802 if (cfg->name == no_dbdriver) {
803 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "DBD: not configured");
804 return NULL;
805 }
806
807 if (!cfg->persist) {
808 /* Return a once-only connection */
809 group = apr_pcalloc(pool, sizeof(dbd_group_t));
810
811 group->cfg = cfg;
812
813 dbd_construct((void*) &rec, group, pool);
814 return rec;
815 }
816
817 #if APR_HAS_THREADS
818 if (!group->reslist) {
819 if (dbd_setup_lock(s, group) != APR_SUCCESS) {
820 return NULL;
821 }
822 }
823
824 rv = apr_reslist_acquire(group->reslist, (void*) &rec);
825 if (rv != APR_SUCCESS) {
826 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
827 "Failed to acquire DBD connection from pool!");
828 return NULL;
829 }
830
831 if (dbd_check(pool, s, rec) != APR_SUCCESS) {
832 apr_reslist_invalidate(group->reslist, rec);
833 return NULL;
834 }
835 #else
836 /* If we have a persistent connection and it's good, we'll use it;
837 * since this is non-threaded, we can update without a mutex
838 */
839 rec = group->rec;
840 if (rec) {
841 if (dbd_check(pool, s, rec) != APR_SUCCESS) {
842 apr_pool_destroy(rec->pool);
843 rec = NULL;
844 }
845 }
846
847 /* We don't have a connection right now, so we'll open one */
848 if (!rec) {
849 dbd_construct((void*) &rec, group, group->pool);
850 group->rec = rec;
851 }
852 #endif
853
854 return rec;
855 }
856
857 #if APR_HAS_THREADS
858 typedef struct {
859 ap_dbd_t *rec;
860 apr_reslist_t *reslist;
861 } dbd_acquire_t;
862
863 static apr_status_t dbd_release(void *data)
864 {
865 dbd_acquire_t *acq = data;
866 apr_reslist_release(acq->reslist, acq->rec);
867 return APR_SUCCESS;
868 }
869
870 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
871 {
872 dbd_acquire_t *acq;
873
874 while (!ap_is_initial_req(r)) {
875 if (r->prev) {
876 r = r->prev;
877 }
878 else if (r->main) {
879 r = r->main;
880 }
881 }
882
883 acq = ap_get_module_config(r->request_config, &dbd_module);
884 if (!acq) {
885 acq = apr_palloc(r->pool, sizeof(dbd_acquire_t));
886 acq->rec = ap_dbd_open(r->pool, r->server);
887 if (acq->rec) {
888 svr_cfg *svr = ap_get_module_config(r->server->module_config,
889 &dbd_module);
890
891 ap_set_module_config(r->request_config, &dbd_module, acq);
892 if (svr->cfg->persist) {
893 acq->reslist = svr->group->reslist;
894 apr_pool_cleanup_register(r->pool, acq, dbd_release,
895 apr_pool_cleanup_null);
896 }
897 }
898 }
899
900 return acq->rec;
901 }
902
903 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
904 {
905 dbd_acquire_t *acq = ap_get_module_config(c->conn_config, &dbd_module);
906
907 if (!acq) {
908 acq = apr_palloc(c->pool, sizeof(dbd_acquire_t));
909 acq->rec = ap_dbd_open(c->pool, c->base_server);
910 if (acq->rec) {
911 svr_cfg *svr = ap_get_module_config(c->base_server->module_config,
912 &dbd_module);
913
914 ap_set_module_config(c->conn_config, &dbd_module, acq);
915 if (svr->cfg->persist) {
916 acq->reslist = svr->group->reslist;
917 apr_pool_cleanup_register(c->pool, acq, dbd_release,
918 apr_pool_cleanup_null);
919 }
920 }
921 }
922
923 return acq->rec;
924 }
925 #else
926 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_acquire(request_rec *r)
927 {
928 ap_dbd_t *rec;
929
930 while (!ap_is_initial_req(r)) {
931 if (r->prev) {
932 r = r->prev;
933 }
934 else if (r->main) {
935 r = r->main;
936 }
937 }
938
939 rec = ap_get_module_config(r->request_config, &dbd_module);
940 if (!rec) {
941 rec = ap_dbd_open(r->pool, r->server);
942 if (rec) {
943 ap_set_module_config(r->request_config, &dbd_module, rec);
944 }
945 }
946
947 return rec;
948 }
949
950 DBD_DECLARE_NONSTD(ap_dbd_t *) ap_dbd_cacquire(conn_rec *c)
951 {
952 ap_dbd_t *rec = ap_get_module_config(c->conn_config, &dbd_module);
953
954 if (!rec) {
955 rec = ap_dbd_open(c->pool, c->base_server);
956 if (rec) {
957 ap_set_module_config(c->conn_config, &dbd_module, rec);
958 }
959 }
960
961 return rec;
962 }
963 #endif
964
965 static void dbd_hooks(apr_pool_t *pool)
966 {
967 ap_hook_pre_config(dbd_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
968 ap_hook_post_config(dbd_post_config, NULL, NULL, APR_HOOK_MIDDLE);
969 ap_hook_child_init(dbd_child_init, NULL, NULL, APR_HOOK_MIDDLE);
970
971 APR_REGISTER_OPTIONAL_FN(ap_dbd_prepare);
972 APR_REGISTER_OPTIONAL_FN(ap_dbd_open);
973 APR_REGISTER_OPTIONAL_FN(ap_dbd_close);
974 APR_REGISTER_OPTIONAL_FN(ap_dbd_acquire);
975 APR_REGISTER_OPTIONAL_FN(ap_dbd_cacquire);
976
977 APR_OPTIONAL_HOOK(dbd, post_connect, dbd_init_sql_init,
978 NULL, NULL, APR_HOOK_MIDDLE);
979
980 apr_dbd_init(pool);
981 }
982
983 module AP_MODULE_DECLARE_DATA dbd_module = {
984 STANDARD20_MODULE_STUFF,
985 NULL,
986 NULL,
987 create_dbd_config,
988 merge_dbd_config,
989 dbd_cmds,
990 dbd_hooks
991 };
992

Properties

Name Value
svn:eol-style native

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2