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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "http_request.h"
20 #include "http_protocol.h"
21 #include "http_config.h"
22 #include "mpm_common.h"
25 #include "apr_strings.h"
27 #define APR_WANT_STRFUNC
35 #include "ap_socache.h"
37 #if AP_NEED_SET_MUTEX_PERMS
41 /* Use of the context structure must be thread-safe after the initial
42 * create/init; callers must hold the mutex. */
43 struct ap_socache_instance_t {
44 const char *data_file;
45 /* Pool must only be used with the mutex held. */
47 apr_time_t last_expiry;
48 apr_interval_time_t expiry_interval;
52 * Support for DBM library
54 #define DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
56 /* Check for definition of DEFAULT_REL_RUNTIMEDIR */
57 #ifndef DEFAULT_REL_RUNTIMEDIR
58 #define DEFAULT_DBM_PREFIX "logs/socache-dbm-"
60 #define DEFAULT_DBM_PREFIX DEFAULT_REL_RUNTIMEDIR "/socache-dbm-"
63 /* ### this should use apr_dbm_usednames. */
64 #if !defined(DBM_FILE_SUFFIX_DIR) && !defined(DBM_FILE_SUFFIX_PAG)
65 #if defined(DBM_SUFFIX)
66 #define DBM_FILE_SUFFIX_DIR DBM_SUFFIX
67 #define DBM_FILE_SUFFIX_PAG DBM_SUFFIX
68 #elif defined(__FreeBSD__) || (defined(DB_LOCK) && defined(DB_SHMEM))
69 #define DBM_FILE_SUFFIX_DIR ".db"
70 #define DBM_FILE_SUFFIX_PAG ".db"
72 #define DBM_FILE_SUFFIX_DIR ".dir"
73 #define DBM_FILE_SUFFIX_PAG ".pag"
77 static void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s);
79 static apr_status_t socache_dbm_remove(ap_socache_instance_t *ctx,
80 server_rec *s, const unsigned char *id,
81 unsigned int idlen, apr_pool_t *p);
83 static const char *socache_dbm_create(ap_socache_instance_t **context,
85 apr_pool_t *tmp, apr_pool_t *p)
87 ap_socache_instance_t *ctx;
89 *context = ctx = apr_pcalloc(p, sizeof *ctx);
92 ctx->data_file = ap_server_root_relative(p, arg);
93 if (!ctx->data_file) {
94 return apr_psprintf(tmp, "Invalid cache file path %s", arg);
98 apr_pool_create(&ctx->pool, p);
103 static apr_status_t socache_dbm_init(ap_socache_instance_t *ctx,
104 const char *namespace,
105 const struct ap_socache_hints *hints,
106 server_rec *s, apr_pool_t *p)
111 /* for the DBM we need the data file */
112 if (ctx->data_file == NULL) {
113 const char *path = apr_pstrcat(p, DEFAULT_DBM_PREFIX, namespace,
116 ctx->data_file = ap_server_root_relative(p, path);
118 if (ctx->data_file == NULL) {
119 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
120 "could not use default path '%s' for DBM socache",
126 /* open it once to create it and to make sure it _can_ be created */
127 apr_pool_clear(ctx->pool);
129 if ((rv = apr_dbm_open(&dbm, ctx->data_file,
130 APR_DBM_RWCREATE, DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
131 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
132 "Cannot create socache DBM file `%s'",
138 ctx->expiry_interval = (hints && hints->expiry_interval
139 ? hints->expiry_interval : apr_time_from_sec(30));
141 #if AP_NEED_SET_MUTEX_PERMS
143 * We have to make sure the Apache child processes have access to
144 * the DBM file. But because there are brain-dead platforms where we
145 * cannot exactly determine the suffixes we try all possibilities.
147 if (geteuid() == 0 /* is superuser */) {
148 chown(ctx->data_file, ap_unixd_config.user_id, -1 /* no gid change */);
149 if (chown(apr_pstrcat(p, ctx->data_file, DBM_FILE_SUFFIX_DIR, NULL),
150 ap_unixd_config.user_id, -1) == -1) {
151 if (chown(apr_pstrcat(p, ctx->data_file, ".db", NULL),
152 ap_unixd_config.user_id, -1) == -1)
153 chown(apr_pstrcat(p, ctx->data_file, ".dir", NULL),
154 ap_unixd_config.user_id, -1);
156 if (chown(apr_pstrcat(p, ctx->data_file, DBM_FILE_SUFFIX_PAG, NULL),
157 ap_unixd_config.user_id, -1) == -1) {
158 if (chown(apr_pstrcat(p, ctx->data_file, ".db", NULL),
159 ap_unixd_config.user_id, -1) == -1)
160 chown(apr_pstrcat(p, ctx->data_file, ".pag", NULL),
161 ap_unixd_config.user_id, -1);
165 socache_dbm_expire(ctx, s);
170 static void socache_dbm_kill(ap_socache_instance_t *ctx, server_rec *s)
172 /* the correct way */
173 unlink(apr_pstrcat(ctx->pool, ctx->data_file, DBM_FILE_SUFFIX_DIR, NULL));
174 unlink(apr_pstrcat(ctx->pool, ctx->data_file, DBM_FILE_SUFFIX_PAG, NULL));
175 /* the additional ways to be sure */
176 unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".dir", NULL));
177 unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".pag", NULL));
178 unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".db", NULL));
179 unlink(ctx->data_file);
184 static apr_status_t socache_dbm_store(ap_socache_instance_t *ctx,
185 server_rec *s, const unsigned char *id,
186 unsigned int idlen, apr_time_t expiry,
187 unsigned char *ucaData,
188 unsigned int nData, apr_pool_t *pool)
195 /* be careful: do not try to store too much bytes in a DBM file! */
197 if ((idlen + nData) >= PAIRMAX) {
198 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
199 "data size too large for DBM socache: %d >= %d",
200 (idlen + nData), PAIRMAX);
204 if ((idlen + nData) >= 950 /* at least less than approx. 1KB */) {
205 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
206 "data size too large for DBM socache: %d >= %d",
207 (idlen + nData), 950);
213 dbmkey.dptr = (char *)id;
214 dbmkey.dsize = idlen;
216 /* create DBM value */
217 dbmval.dsize = sizeof(apr_time_t) + nData;
218 dbmval.dptr = (char *)malloc(dbmval.dsize);
219 if (dbmval.dptr == NULL) {
220 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
221 "malloc error creating DBM value");
224 memcpy((char *)dbmval.dptr, &expiry, sizeof(apr_time_t));
225 memcpy((char *)dbmval.dptr+sizeof(apr_time_t), ucaData, nData);
227 /* and store it to the DBM file */
228 apr_pool_clear(ctx->pool);
230 if ((rv = apr_dbm_open(&dbm, ctx->data_file,
231 APR_DBM_RWCREATE, DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
232 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
233 "Cannot open socache DBM file `%s' for writing "
239 if ((rv = apr_dbm_store(dbm, dbmkey, dbmval)) != APR_SUCCESS) {
240 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
241 "Cannot store socache object to DBM file `%s'",
249 /* free temporary buffers */
252 /* allow the regular expiring to occur */
253 socache_dbm_expire(ctx, s);
258 static apr_status_t socache_dbm_retrieve(ap_socache_instance_t *ctx, server_rec *s,
259 const unsigned char *id, unsigned int idlen,
260 unsigned char *dest, unsigned int *destlen,
271 /* allow the regular expiring to occur */
272 socache_dbm_expire(ctx, s);
274 /* create DBM key and values */
275 dbmkey.dptr = (char *)id;
276 dbmkey.dsize = idlen;
278 /* and fetch it from the DBM file
279 * XXX: Should we open the dbm against r->pool so the cleanup will
280 * do the apr_dbm_close? This would make the code a bit cleaner.
282 apr_pool_clear(ctx->pool);
283 if ((rc = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
284 DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
285 ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
286 "Cannot open socache DBM file `%s' for reading "
291 rc = apr_dbm_fetch(dbm, dbmkey, &dbmval);
292 if (rc != APR_SUCCESS) {
296 if (dbmval.dptr == NULL || dbmval.dsize <= sizeof(apr_time_t)) {
301 /* parse resulting data */
302 nData = dbmval.dsize-sizeof(apr_time_t);
303 if (nData > *destlen) {
309 memcpy(&expiry, dbmval.dptr, sizeof(apr_time_t));
310 memcpy(dest, (char *)dbmval.dptr + sizeof(apr_time_t), nData);
314 /* make sure the stuff is still not expired */
315 now = apr_time_now();
317 socache_dbm_remove(ctx, s, id, idlen, p);
324 static apr_status_t socache_dbm_remove(ap_socache_instance_t *ctx,
325 server_rec *s, const unsigned char *id,
326 unsigned int idlen, apr_pool_t *p)
332 /* create DBM key and values */
333 dbmkey.dptr = (char *)id;
334 dbmkey.dsize = idlen;
336 /* and delete it from the DBM file */
337 apr_pool_clear(ctx->pool);
339 if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
340 DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
341 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
342 "Cannot open socache DBM file `%s' for writing "
347 apr_dbm_delete(dbm, dbmkey);
353 static void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s)
362 apr_datum_t *keylist;
369 * make sure the expiration for still not-accessed
370 * socache entries is done only from time to time
374 if (now < ctx->last_expiry + ctx->expiry_interval) {
378 ctx->last_expiry = now;
381 * Here we have to be very carefully: Not all DBM libraries are
382 * smart enough to allow one to iterate over the elements and at the
383 * same time delete expired ones. Some of them get totally crazy
384 * while others have no problems. So we have to do it the slower but
385 * more safe way: we first iterate over all elements and remember
386 * those which have to be expired. Then in a second pass we delete
387 * all those expired elements. Additionally we reopen the DBM file
388 * to be really safe in state.
394 /* allocate the key array in a memory sub pool */
395 apr_pool_clear(ctx->pool);
397 if ((keylist = apr_palloc(ctx->pool, sizeof(dbmkey)*KEYMAX)) == NULL) {
401 /* pass 1: scan DBM database */
403 if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
404 DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
405 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
406 "Cannot open socache DBM file `%s' for "
411 apr_dbm_firstkey(dbm, &dbmkey);
412 while (dbmkey.dptr != NULL) {
415 apr_dbm_fetch(dbm, dbmkey, &dbmval);
416 if (dbmval.dsize <= sizeof(apr_time_t) || dbmval.dptr == NULL)
419 memcpy(&expiry, dbmval.dptr, sizeof(apr_time_t));
424 if ((keylist[keyidx].dptr = apr_pmemdup(ctx->pool, dbmkey.dptr, dbmkey.dsize)) != NULL) {
425 keylist[keyidx].dsize = dbmkey.dsize;
427 if (keyidx == KEYMAX)
431 apr_dbm_nextkey(dbm, &dbmkey);
435 /* pass 2: delete expired elements */
436 if (apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
437 DBM_FILE_MODE, ctx->pool) != APR_SUCCESS) {
438 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
439 "Cannot re-open socache DBM file `%s' for "
444 for (i = 0; i < keyidx; i++) {
445 apr_dbm_delete(dbm, keylist[i]);
454 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
455 "DBM socache expiry: "
456 "old: %d, new: %d, removed: %d",
457 elts, elts-deleted, deleted);
460 static void socache_dbm_status(ap_socache_instance_t *ctx, request_rec *r,
474 apr_pool_clear(ctx->pool);
475 if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
476 DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
477 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
478 "Cannot open socache DBM file `%s' for status "
484 * XXX - Check the return value of apr_dbm_firstkey, apr_dbm_fetch - TBD
486 apr_dbm_firstkey(dbm, &dbmkey);
487 for ( ; dbmkey.dptr != NULL; apr_dbm_nextkey(dbm, &dbmkey)) {
488 apr_dbm_fetch(dbm, dbmkey, &dbmval);
489 if (dbmval.dptr == NULL)
492 size += dbmval.dsize;
495 if (size > 0 && elts > 0)
496 avg = (int)(size / (long)elts);
499 ap_rprintf(r, "cache type: <b>DBM</b>, maximum size: <b>unlimited</b><br>");
500 ap_rprintf(r, "current entries: <b>%d</b>, current size: <b>%ld</b> bytes<br>", elts, size);
501 ap_rprintf(r, "average entry size: <b>%d</b> bytes<br>", avg);
505 apr_status_t socache_dbm_iterate(ap_socache_instance_t *instance,
507 ap_socache_iterator_t *iterator,
513 static const ap_socache_provider_t socache_dbm = {
515 AP_SOCACHE_FLAG_NOTMPSAFE,
520 socache_dbm_retrieve,
526 static void register_hooks(apr_pool_t *p)
528 ap_register_provider(p, AP_SOCACHE_PROVIDER_GROUP, "dbm",
529 AP_SOCACHE_PROVIDER_VERSION,
533 module AP_MODULE_DECLARE_DATA socache_dbm_module = {
534 STANDARD20_MODULE_STUFF,
535 NULL, NULL, NULL, NULL, NULL,