~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr-util/dbd/apr_dbd.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * 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
#include <stdio.h>
 
18
 
 
19
#include "apu.h"
 
20
#include "apr_pools.h"
 
21
#include "apr_dbd_internal.h"
 
22
#include "apr_dbd.h"
 
23
#include "apr_hash.h"
 
24
#include "apr_thread_mutex.h"
 
25
#include "apr_dso.h"
 
26
#include "apr_strings.h"
 
27
 
 
28
static apr_hash_t *drivers = NULL;
 
29
 
 
30
#define CLEANUP_CAST (apr_status_t (*)(void*))
 
31
 
 
32
/* Once the autofoo supports building it for dynamic load, we can use
 
33
 * #define APR_DSO_BUILD APR_HAS_DSO
 
34
 */
 
35
 
 
36
#if APR_DSO_BUILD
 
37
#if APR_HAS_THREADS
 
38
static apr_thread_mutex_t* mutex = NULL;
 
39
#endif
 
40
#else
 
41
#define DRIVER_LOAD(name,driver,pool) \
 
42
    {   \
 
43
        extern const apr_dbd_driver_t driver; \
 
44
        apr_hash_set(drivers,name,APR_HASH_KEY_STRING,&driver); \
 
45
        if (driver.init) {     \
 
46
            driver.init(pool); \
 
47
        }  \
 
48
    }
 
49
#endif
 
50
 
 
51
static apr_status_t apr_dbd_term(void *ptr)
 
52
{
 
53
    /* set drivers to NULL so init can work again */
 
54
    drivers = NULL;
 
55
 
 
56
    /* Everything else we need is handled by cleanups registered
 
57
     * when we created mutexes and loaded DSOs
 
58
     */
 
59
    return APR_SUCCESS;
 
60
}
 
61
 
 
62
APU_DECLARE(apr_status_t) apr_dbd_init(apr_pool_t *pool)
 
63
{
 
64
    apr_status_t ret = APR_SUCCESS;
 
65
 
 
66
    if (drivers != NULL) {
 
67
        return APR_SUCCESS;
 
68
    }
 
69
    drivers = apr_hash_make(pool);
 
70
    apr_pool_cleanup_register(pool, NULL, apr_dbd_term,
 
71
                              apr_pool_cleanup_null);
 
72
 
 
73
#if APR_DSO_BUILD
 
74
 
 
75
#if APR_HAS_THREADS
 
76
    ret = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, pool);
 
77
    /* This already registers a pool cleanup */
 
78
#endif
 
79
 
 
80
#else
 
81
 
 
82
#if APU_HAVE_MYSQL
 
83
    DRIVER_LOAD("mysql", apr_dbd_mysql_driver, pool);
 
84
#endif
 
85
#if APU_HAVE_PGSQL
 
86
    DRIVER_LOAD("pgsql", apr_dbd_pgsql_driver, pool);
 
87
#endif
 
88
#if APU_HAVE_SQLITE3
 
89
    DRIVER_LOAD("sqlite3", apr_dbd_sqlite3_driver, pool);
 
90
#endif
 
91
#if APU_HAVE_SQLITE2
 
92
    DRIVER_LOAD("sqlite2", apr_dbd_sqlite2_driver, pool);
 
93
#endif
 
94
#if APU_HAVE_SOME_OTHER_BACKEND
 
95
    DRIVER_LOAD("firebird", apr_dbd_other_driver, pool);
 
96
#endif
 
97
#endif
 
98
    return ret;
 
99
}
 
100
APU_DECLARE(apr_status_t) apr_dbd_get_driver(apr_pool_t *pool, const char *name,
 
101
                                             const apr_dbd_driver_t **driver)
 
102
{
 
103
#if APR_DSO_BUILD
 
104
    char path[80];
 
105
    apr_dso_handle_t *dlhandle = NULL;
 
106
#endif
 
107
    apr_status_t rv;
 
108
 
 
109
   *driver = apr_hash_get(drivers, name, APR_HASH_KEY_STRING);
 
110
    if (*driver) {
 
111
        return APR_SUCCESS;
 
112
    }
 
113
 
 
114
#if APR_DSO_BUILD
 
115
 
 
116
#if APR_HAS_THREADS
 
117
    rv = apr_thread_mutex_lock(mutex);
 
118
    if (rv != APR_SUCCESS) {
 
119
        goto unlock;
 
120
    }
 
121
    *driver = apr_hash_get(drivers, name, APR_HASH_KEY_STRING);
 
122
    if (*driver) {
 
123
        goto unlock;
 
124
    }
 
125
#endif
 
126
 
 
127
#ifdef WIN32
 
128
    sprintf(path, "apr_dbd_%s.dll", name);
 
129
#else
 
130
    sprintf(path, "apr_dbd_%s.so", name);
 
131
#endif
 
132
    rv = apr_dso_load(&dlhandle, path, pool);
 
133
    if (rv != APR_SUCCESS) { /* APR_EDSOOPEN */
 
134
        goto unlock;
 
135
    }
 
136
    sprintf(path, "apr_dbd_%s_driver", name);
 
137
    rv = apr_dso_sym((void*)driver, dlhandle, path);
 
138
    if (rv != APR_SUCCESS) { /* APR_ESYMNOTFOUND */
 
139
        apr_dso_unload(dlhandle);
 
140
        goto unlock;
 
141
    }
 
142
    if ((*driver)->init) {
 
143
        (*driver)->init(pool);
 
144
    }
 
145
    apr_hash_set(drivers, name, APR_HASH_KEY_STRING, *driver);
 
146
 
 
147
unlock:
 
148
#if APR_HAS_THREADS
 
149
    apr_thread_mutex_unlock(mutex);
 
150
#endif
 
151
 
 
152
#else   /* APR_DSO_BUILD - so if it wasn't already loaded, it's NOTIMPL */
 
153
    rv = APR_ENOTIMPL;
 
154
#endif
 
155
 
 
156
    return rv;
 
157
}
 
158
APU_DECLARE(apr_status_t) apr_dbd_open(const apr_dbd_driver_t *driver,
 
159
                                       apr_pool_t *pool, const char *params,
 
160
                                       apr_dbd_t **handle)
 
161
{
 
162
    apr_status_t rv;
 
163
    *handle = driver->open(pool, params);
 
164
    if (*handle == NULL) {
 
165
        return APR_EGENERAL;
 
166
    }
 
167
    rv = apr_dbd_check_conn(driver, pool, *handle);
 
168
    if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
 
169
        apr_dbd_close(driver, *handle);
 
170
        return APR_EGENERAL;
 
171
    }
 
172
    return APR_SUCCESS;
 
173
}
 
174
APU_DECLARE(int) apr_dbd_transaction_start(const apr_dbd_driver_t *driver,
 
175
                                           apr_pool_t *pool, apr_dbd_t *handle,
 
176
                                           apr_dbd_transaction_t **trans)
 
177
{
 
178
    int ret = driver->start_transaction(pool, handle, trans);
 
179
    if (*trans) {
 
180
        apr_pool_cleanup_register(pool, *trans,
 
181
                                  CLEANUP_CAST driver->end_transaction,
 
182
                                  apr_pool_cleanup_null);
 
183
    }
 
184
    return ret;
 
185
}
 
186
APU_DECLARE(int) apr_dbd_transaction_end(const apr_dbd_driver_t *driver,
 
187
                                         apr_pool_t *pool,
 
188
                                         apr_dbd_transaction_t *trans)
 
189
{
 
190
    apr_pool_cleanup_kill(pool, trans, CLEANUP_CAST driver->end_transaction);
 
191
    return driver->end_transaction(trans);
 
192
}
 
193
 
 
194
APU_DECLARE(apr_status_t) apr_dbd_close(const apr_dbd_driver_t *driver,
 
195
                                        apr_dbd_t *handle)
 
196
{
 
197
    return driver->close(handle);
 
198
}
 
199
APU_DECLARE(const char*) apr_dbd_name(const apr_dbd_driver_t *driver)
 
200
{
 
201
    return driver->name;
 
202
}
 
203
APU_DECLARE(void*) apr_dbd_native_handle(const apr_dbd_driver_t *driver,
 
204
                                         apr_dbd_t *handle)
 
205
{
 
206
    return driver->native_handle(handle);
 
207
}
 
208
APU_DECLARE(int) apr_dbd_check_conn(const apr_dbd_driver_t *driver, apr_pool_t *pool,
 
209
                                    apr_dbd_t *handle)
 
210
{
 
211
    return driver->check_conn(pool, handle);
 
212
}
 
213
APU_DECLARE(int) apr_dbd_set_dbname(const apr_dbd_driver_t *driver, apr_pool_t *pool,
 
214
                                   apr_dbd_t *handle, const char *name)
 
215
{
 
216
    return driver->set_dbname(pool,handle,name);
 
217
}
 
218
APU_DECLARE(int) apr_dbd_query(const apr_dbd_driver_t *driver, apr_dbd_t *handle,
 
219
                               int *nrows, const char *statement)
 
220
{
 
221
    return driver->query(handle,nrows,statement);
 
222
}
 
223
APU_DECLARE(int) apr_dbd_select(const apr_dbd_driver_t *driver, apr_pool_t *pool,
 
224
                                apr_dbd_t *handle, apr_dbd_results_t **res,
 
225
                                const char *statement, int random)
 
226
{
 
227
    return driver->select(pool,handle,res,statement,random);
 
228
}
 
229
APU_DECLARE(int) apr_dbd_num_cols(const apr_dbd_driver_t *driver,
 
230
                                  apr_dbd_results_t *res)
 
231
{
 
232
    return driver->num_cols(res);
 
233
}
 
234
APU_DECLARE(int) apr_dbd_num_tuples(const apr_dbd_driver_t *driver,
 
235
                                    apr_dbd_results_t *res)
 
236
{
 
237
    return driver->num_tuples(res);
 
238
}
 
239
APU_DECLARE(int) apr_dbd_get_row(const apr_dbd_driver_t *driver, apr_pool_t *pool,
 
240
                                 apr_dbd_results_t *res, apr_dbd_row_t **row,
 
241
                                 int rownum)
 
242
{
 
243
    return driver->get_row(pool,res,row,rownum);
 
244
}
 
245
APU_DECLARE(const char*) apr_dbd_get_entry(const apr_dbd_driver_t *driver,
 
246
                                           apr_dbd_row_t *row, int col)
 
247
{
 
248
    return driver->get_entry(row,col);
 
249
}
 
250
APU_DECLARE(const char*) apr_dbd_error(const apr_dbd_driver_t *driver,
 
251
                                       apr_dbd_t *handle, int errnum)
 
252
{
 
253
    return driver->error(handle,errnum);
 
254
}
 
255
APU_DECLARE(const char*) apr_dbd_escape(const apr_dbd_driver_t *driver,
 
256
                                        apr_pool_t *pool, const char *string,
 
257
                                        apr_dbd_t *handle)
 
258
{
 
259
    return driver->escape(pool,string,handle);
 
260
}
 
261
APU_DECLARE(int) apr_dbd_prepare(const apr_dbd_driver_t *driver, apr_pool_t *pool,
 
262
                                 apr_dbd_t *handle, const char *query,
 
263
                                 const char *label,
 
264
                                 apr_dbd_prepared_t **statement)
 
265
{
 
266
    return driver->prepare(pool,handle,query,label,statement);
 
267
}
 
268
APU_DECLARE(int) apr_dbd_pquery(const apr_dbd_driver_t *driver, apr_pool_t *pool,
 
269
                                apr_dbd_t *handle, int *nrows,
 
270
                                apr_dbd_prepared_t *statement, int nargs,
 
271
                                const char **args)
 
272
{
 
273
    return driver->pquery(pool,handle,nrows,statement,nargs,args);
 
274
}
 
275
APU_DECLARE(int) apr_dbd_pselect(const apr_dbd_driver_t *driver, apr_pool_t *pool,
 
276
                                 apr_dbd_t *handle, apr_dbd_results_t **res,
 
277
                                 apr_dbd_prepared_t *statement, int random,
 
278
                                 int nargs, const char **args)
 
279
{
 
280
    return driver->pselect(pool,handle,res,statement,random,nargs,args);
 
281
}
 
282
APU_DECLARE(int) apr_dbd_pvquery(const apr_dbd_driver_t *driver, apr_pool_t *pool,
 
283
                                 apr_dbd_t *handle, int *nrows,
 
284
                                 apr_dbd_prepared_t *statement,...)
 
285
{
 
286
    int ret;
 
287
    va_list args;
 
288
    va_start(args, statement);
 
289
    ret = driver->pvquery(pool,handle,nrows,statement,args);
 
290
    va_end(args);
 
291
    return ret;
 
292
}
 
293
APU_DECLARE(int) apr_dbd_pvselect(const apr_dbd_driver_t *driver, apr_pool_t *pool,
 
294
                                  apr_dbd_t *handle, apr_dbd_results_t **res,
 
295
                                  apr_dbd_prepared_t *statement, int random,...)
 
296
{
 
297
    int ret;
 
298
    va_list args;
 
299
    va_start(args, random);
 
300
    ret = driver->pvselect(pool,handle,res,statement,random,args);
 
301
    va_end(args);
 
302
    return ret;
 
303
}