0797faae937515a5225a36db4a1ec79480d2555cjorton/* Licensed to the Apache Software Foundation (ASF) under one or more
0797faae937515a5225a36db4a1ec79480d2555cjorton * contributor license agreements. See the NOTICE file distributed with
0797faae937515a5225a36db4a1ec79480d2555cjorton * this work for additional information regarding copyright ownership.
0797faae937515a5225a36db4a1ec79480d2555cjorton * The ASF licenses this file to You under the Apache License, Version 2.0
0797faae937515a5225a36db4a1ec79480d2555cjorton * (the "License"); you may not use this file except in compliance with
0797faae937515a5225a36db4a1ec79480d2555cjorton * the License. You may obtain a copy of the License at
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * http://www.apache.org/licenses/LICENSE-2.0
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * Unless required by applicable law or agreed to in writing, software
0797faae937515a5225a36db4a1ec79480d2555cjorton * distributed under the License is distributed on an "AS IS" BASIS,
0797faae937515a5225a36db4a1ec79480d2555cjorton * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0797faae937515a5225a36db4a1ec79480d2555cjorton * See the License for the specific language governing permissions and
0797faae937515a5225a36db4a1ec79480d2555cjorton * limitations under the License.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "httpd.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "http_log.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "http_request.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "http_protocol.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "http_config.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "mpm_common.h"
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung#include "mod_status.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr_strings.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr_time.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#define APR_WANT_STRFUNC
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr_want.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr_dbm.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#if APR_HAVE_UNISTD_H
0797faae937515a5225a36db4a1ec79480d2555cjorton#include <unistd.h>
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "ap_socache.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton
dca6643570b2f028bc7af76329bd7e333bf7493cpquerna#if AP_NEED_SET_MUTEX_PERMS
dca6643570b2f028bc7af76329bd7e333bf7493cpquerna#include "unixd.h"
dca6643570b2f028bc7af76329bd7e333bf7493cpquerna#endif
dca6643570b2f028bc7af76329bd7e333bf7493cpquerna
0797faae937515a5225a36db4a1ec79480d2555cjorton/* Use of the context structure must be thread-safe after the initial
0797faae937515a5225a36db4a1ec79480d2555cjorton * create/init; callers must hold the mutex. */
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstruct ap_socache_instance_t {
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *data_file;
0797faae937515a5225a36db4a1ec79480d2555cjorton /* Pool must only be used with the mutex held. */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_t *pool;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe apr_time_t last_expiry;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe apr_interval_time_t expiry_interval;
0797faae937515a5225a36db4a1ec79480d2555cjorton};
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton/**
0797faae937515a5225a36db4a1ec79480d2555cjorton * Support for DBM library
0797faae937515a5225a36db4a1ec79480d2555cjorton */
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton#define DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
0797faae937515a5225a36db4a1ec79480d2555cjorton
8ba562720ad913bc899d15deba8b83dfe1065c0ejim#define DEFAULT_DBM_PREFIX "socache-dbm-"
8c4aed3f92674325e17d8360ee2797beda3a1472jorton
0797faae937515a5225a36db4a1ec79480d2555cjorton/* ### this should use apr_dbm_usednames. */
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton#if !defined(DBM_FILE_SUFFIX_DIR) && !defined(DBM_FILE_SUFFIX_PAG)
0797faae937515a5225a36db4a1ec79480d2555cjorton#if defined(DBM_SUFFIX)
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton#define DBM_FILE_SUFFIX_DIR DBM_SUFFIX
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton#define DBM_FILE_SUFFIX_PAG DBM_SUFFIX
0797faae937515a5225a36db4a1ec79480d2555cjorton#elif defined(__FreeBSD__) || (defined(DB_LOCK) && defined(DB_SHMEM))
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton#define DBM_FILE_SUFFIX_DIR ".db"
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton#define DBM_FILE_SUFFIX_PAG ".db"
0797faae937515a5225a36db4a1ec79480d2555cjorton#else
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton#define DBM_FILE_SUFFIX_DIR ".dir"
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton#define DBM_FILE_SUFFIX_PAG ".pag"
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s);
0797faae937515a5225a36db4a1ec79480d2555cjorton
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic apr_status_t socache_dbm_remove(ap_socache_instance_t *ctx,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim server_rec *s, const unsigned char *id,
501e60e433e1914c64f642114fbb4fb9be9e2ca9jorton unsigned int idlen, apr_pool_t *p);
0797faae937515a5225a36db4a1ec79480d2555cjorton
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic const char *socache_dbm_create(ap_socache_instance_t **context,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim const char *arg,
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_t *tmp, apr_pool_t *p)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
2685f3814b77577ef7b2523442dab1ca88df1e41jorton ap_socache_instance_t *ctx;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton *context = ctx = apr_pcalloc(p, sizeof *ctx);
0797faae937515a5225a36db4a1ec79480d2555cjorton
8c4aed3f92674325e17d8360ee2797beda3a1472jorton if (arg && *arg) {
92332196b20253794e26ad9c51359d922eaf77d3trawick ctx->data_file = ap_runtime_dir_relative(p, arg);
8c4aed3f92674325e17d8360ee2797beda3a1472jorton if (!ctx->data_file) {
8c4aed3f92674325e17d8360ee2797beda3a1472jorton return apr_psprintf(tmp, "Invalid cache file path %s", arg);
8c4aed3f92674325e17d8360ee2797beda3a1472jorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton }
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_create(&ctx->pool, p);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return NULL;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
7c5ca8842552a2111f8ca6a5591489dd4b66b584fuankg#if AP_NEED_SET_MUTEX_PERMS
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sfstatic int try_chown(apr_pool_t *p, server_rec *s,
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf const char *name, const char *suffix)
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf{
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf if (suffix)
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf name = apr_pstrcat(p, name, suffix, NULL);
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf if (-1 == chown(name, ap_unixd_config.user_id,
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf (gid_t)-1 /* no gid change */ ))
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf {
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf if (errno != ENOENT)
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(errno), s, APLOGNO(00802)
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf "Can't change owner of %s", name);
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf return -1;
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf }
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf return 0;
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf}
7c5ca8842552a2111f8ca6a5591489dd4b66b584fuankg#endif
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic apr_status_t socache_dbm_init(ap_socache_instance_t *ctx,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim const char *namespace,
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton const struct ap_socache_hints *hints,
2685f3814b77577ef7b2523442dab1ca88df1e41jorton server_rec *s, apr_pool_t *p)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* for the DBM we need the data file */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (ctx->data_file == NULL) {
8c4aed3f92674325e17d8360ee2797beda3a1472jorton const char *path = apr_pstrcat(p, DEFAULT_DBM_PREFIX, namespace,
8c4aed3f92674325e17d8360ee2797beda3a1472jorton NULL);
8c4aed3f92674325e17d8360ee2797beda3a1472jorton
8ba562720ad913bc899d15deba8b83dfe1065c0ejim ctx->data_file = ap_runtime_dir_relative(p, path);
8c4aed3f92674325e17d8360ee2797beda3a1472jorton
8c4aed3f92674325e17d8360ee2797beda3a1472jorton if (ctx->data_file == NULL) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00803)
8c4aed3f92674325e17d8360ee2797beda3a1472jorton "could not use default path '%s' for DBM socache",
8c4aed3f92674325e17d8360ee2797beda3a1472jorton path);
8c4aed3f92674325e17d8360ee2797beda3a1472jorton return APR_EINVAL;
8c4aed3f92674325e17d8360ee2797beda3a1472jorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* open it once to create it and to make sure it _can_ be created */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton APR_DBM_RWCREATE, DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00804)
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot create socache DBM file `%s'",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton return rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim ctx->expiry_interval = (hints && hints->expiry_interval
a10d8ce69c26142323c66adaba109be1b4baa379wrowe ? hints->expiry_interval : apr_time_from_sec(30));
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton
0b310e2cb91c7ca69a95637c05fae2fb124d7fcfpquerna#if AP_NEED_SET_MUTEX_PERMS
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * We have to make sure the Apache child processes have access to
0797faae937515a5225a36db4a1ec79480d2555cjorton * the DBM file. But because there are brain-dead platforms where we
0797faae937515a5225a36db4a1ec79480d2555cjorton * cannot exactly determine the suffixes we try all possibilities.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (geteuid() == 0 /* is superuser */) {
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf try_chown(p, s, ctx->data_file, NULL);
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf if (try_chown(p, s, ctx->data_file, DBM_FILE_SUFFIX_DIR))
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf if (try_chown(p, s, ctx->data_file, ".db"))
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf try_chown(p, s, ctx->data_file, ".dir");
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf if (try_chown(p, s, ctx->data_file, DBM_FILE_SUFFIX_PAG))
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf if (try_chown(p, s, ctx->data_file, ".db"))
7bf9cc354cea5d4fda87ab7de3f3effb21bf6180sf try_chown(p, s, ctx->data_file, ".pag");
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_expire(ctx, s);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_SUCCESS;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
4b0e00b3346b3e8fd53219d060f4cf6676847a06jimstatic void socache_dbm_destroy(ap_socache_instance_t *ctx, server_rec *s)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton /* the correct way */
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton unlink(apr_pstrcat(ctx->pool, ctx->data_file, DBM_FILE_SUFFIX_DIR, NULL));
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton unlink(apr_pstrcat(ctx->pool, ctx->data_file, DBM_FILE_SUFFIX_PAG, NULL));
0797faae937515a5225a36db4a1ec79480d2555cjorton /* the additional ways to be sure */
0797faae937515a5225a36db4a1ec79480d2555cjorton unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".dir", NULL));
0797faae937515a5225a36db4a1ec79480d2555cjorton unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".pag", NULL));
0797faae937515a5225a36db4a1ec79480d2555cjorton unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".db", NULL));
0797faae937515a5225a36db4a1ec79480d2555cjorton unlink(ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic apr_status_t socache_dbm_store(ap_socache_instance_t *ctx,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim server_rec *s, const unsigned char *id,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim unsigned int idlen, apr_time_t expiry,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim unsigned char *ucaData,
501e60e433e1914c64f642114fbb4fb9be9e2ca9jorton unsigned int nData, apr_pool_t *pool)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmkey;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmval;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* be careful: do not try to store too much bytes in a DBM file! */
0797faae937515a5225a36db4a1ec79480d2555cjorton#ifdef PAIRMAX
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((idlen + nData) >= PAIRMAX) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00805)
a10d8ce69c26142323c66adaba109be1b4baa379wrowe "data size too large for DBM socache: %d >= %d",
0797faae937515a5225a36db4a1ec79480d2555cjorton (idlen + nData), PAIRMAX);
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_ENOSPC;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton#else
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((idlen + nData) >= 950 /* at least less than approx. 1KB */) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00806)
a10d8ce69c26142323c66adaba109be1b4baa379wrowe "data size too large for DBM socache: %d >= %d",
0797faae937515a5225a36db4a1ec79480d2555cjorton (idlen + nData), 950);
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_ENOSPC;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key */
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dptr = (char *)id;
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dsize = idlen;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM value */
a10d8ce69c26142323c66adaba109be1b4baa379wrowe dbmval.dsize = sizeof(apr_time_t) + nData;
3a59bb90be3bc6246632384c3d885b875ae507d5sf dbmval.dptr = (char *)ap_malloc(dbmval.dsize);
a10d8ce69c26142323c66adaba109be1b4baa379wrowe memcpy((char *)dbmval.dptr, &expiry, sizeof(apr_time_t));
a10d8ce69c26142323c66adaba109be1b4baa379wrowe memcpy((char *)dbmval.dptr+sizeof(apr_time_t), ucaData, nData);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* and store it to the DBM file */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton APR_DBM_RWCREATE, DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00807)
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot open socache DBM file `%s' for writing "
0797faae937515a5225a36db4a1ec79480d2555cjorton "(store)",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton free(dbmval.dptr);
0797faae937515a5225a36db4a1ec79480d2555cjorton return rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_store(dbm, dbmkey, dbmval)) != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00808)
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot store socache object to DBM file `%s'",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton free(dbmval.dptr);
0797faae937515a5225a36db4a1ec79480d2555cjorton return rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* free temporary buffers */
0797faae937515a5225a36db4a1ec79480d2555cjorton free(dbmval.dptr);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* allow the regular expiring to occur */
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_expire(ctx, s);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_SUCCESS;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic apr_status_t socache_dbm_retrieve(ap_socache_instance_t *ctx, server_rec *s,
072f7e449a76d28b580de6e89a1723713ab9adb1jorton const unsigned char *id, unsigned int idlen,
072f7e449a76d28b580de6e89a1723713ab9adb1jorton unsigned char *dest, unsigned int *destlen,
072f7e449a76d28b580de6e89a1723713ab9adb1jorton apr_pool_t *p)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmkey;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmval;
0797faae937515a5225a36db4a1ec79480d2555cjorton unsigned int nData;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe apr_time_t expiry;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe apr_time_t now;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rc;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* allow the regular expiring to occur */
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_expire(ctx, s);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key and values */
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dptr = (char *)id;
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dsize = idlen;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* and fetch it from the DBM file
0797faae937515a5225a36db4a1ec79480d2555cjorton * XXX: Should we open the dbm against r->pool so the cleanup will
0797faae937515a5225a36db4a1ec79480d2555cjorton * do the apr_dbm_close? This would make the code a bit cleaner.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim if ((rc = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, rc, s, APLOGNO(00809)
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot open socache DBM file `%s' for reading "
0797faae937515a5225a36db4a1ec79480d2555cjorton "(fetch)",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton return rc;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton rc = apr_dbm_fetch(dbm, dbmkey, &dbmval);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (rc != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
70003ce816d7851e49ecb0cdc5137becd647ed18niq return APR_NOTFOUND;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
a10d8ce69c26142323c66adaba109be1b4baa379wrowe if (dbmval.dptr == NULL || dbmval.dsize <= sizeof(apr_time_t)) {
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_EGENERAL;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* parse resulting data */
a10d8ce69c26142323c66adaba109be1b4baa379wrowe nData = dbmval.dsize-sizeof(apr_time_t);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (nData > *destlen) {
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_ENOSPC;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton *destlen = nData;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe memcpy(&expiry, dbmval.dptr, sizeof(apr_time_t));
a10d8ce69c26142323c66adaba109be1b4baa379wrowe memcpy(dest, (char *)dbmval.dptr + sizeof(apr_time_t), nData);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* make sure the stuff is still not expired */
a10d8ce69c26142323c66adaba109be1b4baa379wrowe now = apr_time_now();
0797faae937515a5225a36db4a1ec79480d2555cjorton if (expiry <= now) {
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_remove(ctx, s, id, idlen, p);
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_NOTFOUND;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_SUCCESS;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic apr_status_t socache_dbm_remove(ap_socache_instance_t *ctx,
501e60e433e1914c64f642114fbb4fb9be9e2ca9jorton server_rec *s, const unsigned char *id,
501e60e433e1914c64f642114fbb4fb9be9e2ca9jorton unsigned int idlen, apr_pool_t *p)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmkey;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key and values */
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dptr = (char *)id;
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dsize = idlen;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* and delete it from the DBM file */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
0797faae937515a5225a36db4a1ec79480d2555cjorton
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00810)
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot open socache DBM file `%s' for writing "
0797faae937515a5225a36db4a1ec79480d2555cjorton "(delete)",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
501e60e433e1914c64f642114fbb4fb9be9e2ca9jorton return rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_delete(dbm, dbmkey);
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
501e60e433e1914c64f642114fbb4fb9be9e2ca9jorton return APR_SUCCESS;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmkey;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmval;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe apr_time_t expiry;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe int elts = 0;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe int deleted = 0;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe int expired;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t *keylist;
0797faae937515a5225a36db4a1ec79480d2555cjorton int keyidx;
0797faae937515a5225a36db4a1ec79480d2555cjorton int i;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe apr_time_t now;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
a10d8ce69c26142323c66adaba109be1b4baa379wrowe * make sure the expiration for still not-accessed
a10d8ce69c26142323c66adaba109be1b4baa379wrowe * socache entries is done only from time to time
0797faae937515a5225a36db4a1ec79480d2555cjorton */
3e7ce568813f1895b2e8e68e2223653884497bdawrowe now = apr_time_now();
0797faae937515a5225a36db4a1ec79480d2555cjorton
a10d8ce69c26142323c66adaba109be1b4baa379wrowe if (now < ctx->last_expiry + ctx->expiry_interval) {
0797faae937515a5225a36db4a1ec79480d2555cjorton return;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
a10d8ce69c26142323c66adaba109be1b4baa379wrowe ctx->last_expiry = now;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * Here we have to be very carefully: Not all DBM libraries are
0797faae937515a5225a36db4a1ec79480d2555cjorton * smart enough to allow one to iterate over the elements and at the
0797faae937515a5225a36db4a1ec79480d2555cjorton * same time delete expired ones. Some of them get totally crazy
0797faae937515a5225a36db4a1ec79480d2555cjorton * while others have no problems. So we have to do it the slower but
0797faae937515a5225a36db4a1ec79480d2555cjorton * more safe way: we first iterate over all elements and remember
0797faae937515a5225a36db4a1ec79480d2555cjorton * those which have to be expired. Then in a second pass we delete
0797faae937515a5225a36db4a1ec79480d2555cjorton * all those expired elements. Additionally we reopen the DBM file
0797faae937515a5225a36db4a1ec79480d2555cjorton * to be really safe in state.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#define KEYMAX 1024
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton for (;;) {
0797faae937515a5225a36db4a1ec79480d2555cjorton /* allocate the key array in a memory sub pool */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((keylist = apr_palloc(ctx->pool, sizeof(dbmkey)*KEYMAX)) == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton break;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* pass 1: scan DBM database */
0797faae937515a5225a36db4a1ec79480d2555cjorton keyidx = 0;
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00811)
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot open socache DBM file `%s' for "
0797faae937515a5225a36db4a1ec79480d2555cjorton "scanning",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton break;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_firstkey(dbm, &dbmkey);
0797faae937515a5225a36db4a1ec79480d2555cjorton while (dbmkey.dptr != NULL) {
a10d8ce69c26142323c66adaba109be1b4baa379wrowe elts++;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe expired = FALSE;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_fetch(dbm, dbmkey, &dbmval);
a10d8ce69c26142323c66adaba109be1b4baa379wrowe if (dbmval.dsize <= sizeof(apr_time_t) || dbmval.dptr == NULL)
a10d8ce69c26142323c66adaba109be1b4baa379wrowe expired = TRUE;
0797faae937515a5225a36db4a1ec79480d2555cjorton else {
a10d8ce69c26142323c66adaba109be1b4baa379wrowe memcpy(&expiry, dbmval.dptr, sizeof(apr_time_t));
a10d8ce69c26142323c66adaba109be1b4baa379wrowe if (expiry <= now)
a10d8ce69c26142323c66adaba109be1b4baa379wrowe expired = TRUE;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
a10d8ce69c26142323c66adaba109be1b4baa379wrowe if (expired) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((keylist[keyidx].dptr = apr_pmemdup(ctx->pool, dbmkey.dptr, dbmkey.dsize)) != NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton keylist[keyidx].dsize = dbmkey.dsize;
0797faae937515a5225a36db4a1ec79480d2555cjorton keyidx++;
0797faae937515a5225a36db4a1ec79480d2555cjorton if (keyidx == KEYMAX)
0797faae937515a5225a36db4a1ec79480d2555cjorton break;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_nextkey(dbm, &dbmkey);
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* pass 2: delete expired elements */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton DBM_FILE_MODE, ctx->pool) != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00812)
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot re-open socache DBM file `%s' for "
0797faae937515a5225a36db4a1ec79480d2555cjorton "expiring",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton break;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton for (i = 0; i < keyidx; i++) {
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_delete(dbm, keylist[i]);
a10d8ce69c26142323c66adaba109be1b4baa379wrowe deleted++;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if (keyidx < KEYMAX)
0797faae937515a5225a36db4a1ec79480d2555cjorton break;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00813)
8aac9d0a02da972909738eb84a5902199f3298cftrawick "DBM socache expiry: "
0797faae937515a5225a36db4a1ec79480d2555cjorton "old: %d, new: %d, removed: %d",
a10d8ce69c26142323c66adaba109be1b4baa379wrowe elts, elts-deleted, deleted);
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic void socache_dbm_status(ap_socache_instance_t *ctx, request_rec *r,
2685f3814b77577ef7b2523442dab1ca88df1e41jorton int flags)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmkey;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmval;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe int elts;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe long size;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe int avg;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton
a10d8ce69c26142323c66adaba109be1b4baa379wrowe elts = 0;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe size = 0;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00814)
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot open socache DBM file `%s' for status "
0797faae937515a5225a36db4a1ec79480d2555cjorton "retrival",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton return;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * XXX - Check the return value of apr_dbm_firstkey, apr_dbm_fetch - TBD
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_firstkey(dbm, &dbmkey);
0797faae937515a5225a36db4a1ec79480d2555cjorton for ( ; dbmkey.dptr != NULL; apr_dbm_nextkey(dbm, &dbmkey)) {
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_fetch(dbm, dbmkey, &dbmval);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (dbmval.dptr == NULL)
0797faae937515a5225a36db4a1ec79480d2555cjorton continue;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe elts += 1;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe size += dbmval.dsize;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
a10d8ce69c26142323c66adaba109be1b4baa379wrowe if (size > 0 && elts > 0)
a10d8ce69c26142323c66adaba109be1b4baa379wrowe avg = (int)(size / (long)elts);
0797faae937515a5225a36db4a1ec79480d2555cjorton else
a10d8ce69c26142323c66adaba109be1b4baa379wrowe avg = 0;
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung if (!(flags & AP_STATUS_SHORT)) {
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung ap_rprintf(r, "cache type: <b>DBM</b>, maximum size: <b>unlimited</b><br>");
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung ap_rprintf(r, "current entries: <b>%d</b>, current size: <b>%ld</b> bytes<br>", elts, size);
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung ap_rprintf(r, "average entry size: <b>%d</b> bytes<br>", avg);
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung }
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung else {
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung ap_rputs("CacheType: DBM\n", r);
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung ap_rputs("CacheMaximumSize: unlimited\n", r);
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung ap_rprintf(r, "CacheCurrentEntries: %d\n", elts);
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung ap_rprintf(r, "CacheCurrentSize: %ld\n", size);
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung ap_rprintf(r, "CacheAvgEntrySize: %d\n", avg);
d2fcf1d01ba9f36b78eb7b66fa5af8237b79ac70rjung }
0797faae937515a5225a36db4a1ec79480d2555cjorton return;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
250fbbb51225da0dfc973743b795b04dc9740027wrowestatic apr_status_t socache_dbm_iterate(ap_socache_instance_t *ctx,
cf25139efeb705911e931b68a521d8b65ff922eewrowe server_rec *s, void *userctx,
bd2f929f6974a82e4fd33e0393d2b0c91f6f033atrawick ap_socache_iterator_t *iterator,
bd2f929f6974a82e4fd33e0393d2b0c91f6f033atrawick apr_pool_t *pool)
a10d8ce69c26142323c66adaba109be1b4baa379wrowe{
250fbbb51225da0dfc973743b795b04dc9740027wrowe apr_dbm_t *dbm;
250fbbb51225da0dfc973743b795b04dc9740027wrowe apr_datum_t dbmkey;
250fbbb51225da0dfc973743b795b04dc9740027wrowe apr_datum_t dbmval;
250fbbb51225da0dfc973743b795b04dc9740027wrowe apr_time_t expiry;
250fbbb51225da0dfc973743b795b04dc9740027wrowe int expired;
250fbbb51225da0dfc973743b795b04dc9740027wrowe apr_time_t now;
250fbbb51225da0dfc973743b795b04dc9740027wrowe apr_status_t rv;
250fbbb51225da0dfc973743b795b04dc9740027wrowe
250fbbb51225da0dfc973743b795b04dc9740027wrowe /*
3e7ce568813f1895b2e8e68e2223653884497bdawrowe * make sure the expired records are omitted
250fbbb51225da0dfc973743b795b04dc9740027wrowe */
3e7ce568813f1895b2e8e68e2223653884497bdawrowe now = apr_time_now();
250fbbb51225da0dfc973743b795b04dc9740027wrowe if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
250fbbb51225da0dfc973743b795b04dc9740027wrowe DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00815)
250fbbb51225da0dfc973743b795b04dc9740027wrowe "Cannot open socache DBM file `%s' for "
250fbbb51225da0dfc973743b795b04dc9740027wrowe "iterating", ctx->data_file);
250fbbb51225da0dfc973743b795b04dc9740027wrowe return rv;
250fbbb51225da0dfc973743b795b04dc9740027wrowe }
250fbbb51225da0dfc973743b795b04dc9740027wrowe rv = apr_dbm_firstkey(dbm, &dbmkey);
250fbbb51225da0dfc973743b795b04dc9740027wrowe while (rv == APR_SUCCESS && dbmkey.dptr != NULL) {
250fbbb51225da0dfc973743b795b04dc9740027wrowe expired = FALSE;
250fbbb51225da0dfc973743b795b04dc9740027wrowe apr_dbm_fetch(dbm, dbmkey, &dbmval);
250fbbb51225da0dfc973743b795b04dc9740027wrowe if (dbmval.dsize <= sizeof(apr_time_t) || dbmval.dptr == NULL)
250fbbb51225da0dfc973743b795b04dc9740027wrowe expired = TRUE;
250fbbb51225da0dfc973743b795b04dc9740027wrowe else {
250fbbb51225da0dfc973743b795b04dc9740027wrowe memcpy(&expiry, dbmval.dptr, sizeof(apr_time_t));
250fbbb51225da0dfc973743b795b04dc9740027wrowe if (expiry <= now)
250fbbb51225da0dfc973743b795b04dc9740027wrowe expired = TRUE;
250fbbb51225da0dfc973743b795b04dc9740027wrowe }
250fbbb51225da0dfc973743b795b04dc9740027wrowe if (!expired) {
d7da0087671099583748f1da02034fe337c16e30wrowe rv = iterator(ctx, s, userctx,
cf25139efeb705911e931b68a521d8b65ff922eewrowe (unsigned char *)dbmkey.dptr, dbmkey.dsize,
250fbbb51225da0dfc973743b795b04dc9740027wrowe (unsigned char *)dbmval.dptr + sizeof(apr_time_t),
250fbbb51225da0dfc973743b795b04dc9740027wrowe dbmval.dsize - sizeof(apr_time_t), pool);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, s, APLOGNO(00816)
250fbbb51225da0dfc973743b795b04dc9740027wrowe "dbm `%s' entry iterated", ctx->data_file);
250fbbb51225da0dfc973743b795b04dc9740027wrowe if (rv != APR_SUCCESS)
250fbbb51225da0dfc973743b795b04dc9740027wrowe return rv;
250fbbb51225da0dfc973743b795b04dc9740027wrowe }
250fbbb51225da0dfc973743b795b04dc9740027wrowe rv = apr_dbm_nextkey(dbm, &dbmkey);
250fbbb51225da0dfc973743b795b04dc9740027wrowe }
250fbbb51225da0dfc973743b795b04dc9740027wrowe apr_dbm_close(dbm);
250fbbb51225da0dfc973743b795b04dc9740027wrowe
250fbbb51225da0dfc973743b795b04dc9740027wrowe if (rv != APR_SUCCESS && rv != APR_EOF) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00817)
250fbbb51225da0dfc973743b795b04dc9740027wrowe "Failure reading first/next socache DBM file `%s' record",
250fbbb51225da0dfc973743b795b04dc9740027wrowe ctx->data_file);
250fbbb51225da0dfc973743b795b04dc9740027wrowe return rv;
250fbbb51225da0dfc973743b795b04dc9740027wrowe }
250fbbb51225da0dfc973743b795b04dc9740027wrowe return APR_SUCCESS;
a10d8ce69c26142323c66adaba109be1b4baa379wrowe}
a10d8ce69c26142323c66adaba109be1b4baa379wrowe
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic const ap_socache_provider_t socache_dbm = {
0797faae937515a5225a36db4a1ec79480d2555cjorton "dbm",
0797faae937515a5225a36db4a1ec79480d2555cjorton AP_SOCACHE_FLAG_NOTMPSAFE,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_create,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_init,
4b0e00b3346b3e8fd53219d060f4cf6676847a06jim socache_dbm_destroy,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_store,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_retrieve,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_remove,
a10d8ce69c26142323c66adaba109be1b4baa379wrowe socache_dbm_status,
a10d8ce69c26142323c66adaba109be1b4baa379wrowe socache_dbm_iterate
0797faae937515a5225a36db4a1ec79480d2555cjorton};
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void register_hooks(apr_pool_t *p)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim ap_register_provider(p, AP_SOCACHE_PROVIDER_GROUP, "dbm",
0797faae937515a5225a36db4a1ec79480d2555cjorton AP_SOCACHE_PROVIDER_VERSION,
0797faae937515a5225a36db4a1ec79480d2555cjorton &socache_dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asfAP_DECLARE_MODULE(socache_dbm) = {
0797faae937515a5225a36db4a1ec79480d2555cjorton STANDARD20_MODULE_STUFF,
0797faae937515a5225a36db4a1ec79480d2555cjorton NULL, NULL, NULL, NULL, NULL,
0797faae937515a5225a36db4a1ec79480d2555cjorton register_hooks
0797faae937515a5225a36db4a1ec79480d2555cjorton};