mod_socache_dbm.c revision 5bfaaf573bacb45c1cf290ce85ecc676587e8a64
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 * 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/* Use of the context structure must be thread-safe after the initial
0797faae937515a5225a36db4a1ec79480d2555cjorton * create/init; callers must hold the mutex. */
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *data_file;
0797faae937515a5225a36db4a1ec79480d2555cjorton /* Pool must only be used with the mutex held. */
0797faae937515a5225a36db4a1ec79480d2555cjorton * Support for DBM library
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton#define DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
8c4aed3f92674325e17d8360ee2797beda3a1472jorton#define DEFAULT_DBM_PREFIX DEFAULT_REL_RUNTIMEDIR "/socache-dbm-"
0797faae937515a5225a36db4a1ec79480d2555cjorton/* ### this should use apr_dbm_usednames. */
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton#if !defined(DBM_FILE_SUFFIX_DIR) && !defined(DBM_FILE_SUFFIX_PAG)
0797faae937515a5225a36db4a1ec79480d2555cjorton#elif defined(__FreeBSD__) || (defined(DB_LOCK) && defined(DB_SHMEM))
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic apr_status_t socache_dbm_remove(ap_socache_instance_t *ctx,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic const char *socache_dbm_create(ap_socache_instance_t **context,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim const char *arg,
8c4aed3f92674325e17d8360ee2797beda3a1472jorton return apr_psprintf(tmp, "Invalid cache file path %s", arg);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic apr_status_t socache_dbm_init(ap_socache_instance_t *ctx,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim const char *namespace,
0797faae937515a5225a36db4a1ec79480d2555cjorton /* for the DBM we need the data file */
8c4aed3f92674325e17d8360ee2797beda3a1472jorton const char *path = apr_pstrcat(p, DEFAULT_DBM_PREFIX, namespace,
8c4aed3f92674325e17d8360ee2797beda3a1472jorton "could not use default path '%s' for DBM socache",
0797faae937515a5225a36db4a1ec79480d2555cjorton /* open it once to create it and to make sure it _can_ be created */
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton APR_DBM_RWCREATE, DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot create socache DBM file `%s'",
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.
a91a59d0b0ceed7cd5621fe8757eda5ff6a043a8pquerna chown(ctx->data_file, ap_unixd_config.user_id, -1 /* no gid change */);
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton if (chown(apr_pstrcat(p, ctx->data_file, DBM_FILE_SUFFIX_DIR, NULL),
0797faae937515a5225a36db4a1ec79480d2555cjorton if (chown(apr_pstrcat(p, ctx->data_file, ".db", NULL),
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton if (chown(apr_pstrcat(p, ctx->data_file, DBM_FILE_SUFFIX_PAG, NULL),
0797faae937515a5225a36db4a1ec79480d2555cjorton if (chown(apr_pstrcat(p, ctx->data_file, ".db", NULL),
4b0e00b3346b3e8fd53219d060f4cf6676847a06jimstatic void socache_dbm_destroy(ap_socache_instance_t *ctx, server_rec *s)
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));
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic apr_status_t socache_dbm_store(ap_socache_instance_t *ctx,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim unsigned char *ucaData,
0797faae937515a5225a36db4a1ec79480d2555cjorton /* be careful: do not try to store too much bytes in a DBM file! */
a10d8ce69c26142323c66adaba109be1b4baa379wrowe "data size too large for DBM socache: %d >= %d",
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((idlen + nData) >= 950 /* at least less than approx. 1KB */) {
a10d8ce69c26142323c66adaba109be1b4baa379wrowe "data size too large for DBM socache: %d >= %d",
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key */
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM value */
a10d8ce69c26142323c66adaba109be1b4baa379wrowe memcpy((char *)dbmval.dptr, &expiry, sizeof(apr_time_t));
a10d8ce69c26142323c66adaba109be1b4baa379wrowe memcpy((char *)dbmval.dptr+sizeof(apr_time_t), ucaData, nData);
0797faae937515a5225a36db4a1ec79480d2555cjorton /* and store it to the DBM file */
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton APR_DBM_RWCREATE, DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot open socache DBM file `%s' for writing "
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_store(dbm, dbmkey, dbmval)) != APR_SUCCESS) {
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot store socache object to DBM file `%s'",
0797faae937515a5225a36db4a1ec79480d2555cjorton /* free temporary buffers */
0797faae937515a5225a36db4a1ec79480d2555cjorton /* allow the regular expiring to occur */
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic apr_status_t socache_dbm_retrieve(ap_socache_instance_t *ctx, server_rec *s,
0797faae937515a5225a36db4a1ec79480d2555cjorton unsigned int nData;
0797faae937515a5225a36db4a1ec79480d2555cjorton /* allow the regular expiring to occur */
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key and values */
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.
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim if ((rc = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot open socache DBM file `%s' for reading "
a10d8ce69c26142323c66adaba109be1b4baa379wrowe if (dbmval.dptr == NULL || dbmval.dsize <= sizeof(apr_time_t)) {
0797faae937515a5225a36db4a1ec79480d2555cjorton /* parse resulting data */
a10d8ce69c26142323c66adaba109be1b4baa379wrowe memcpy(dest, (char *)dbmval.dptr + sizeof(apr_time_t), nData);
0797faae937515a5225a36db4a1ec79480d2555cjorton /* make sure the stuff is still not expired */
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic apr_status_t socache_dbm_remove(ap_socache_instance_t *ctx,
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key and values */
0797faae937515a5225a36db4a1ec79480d2555cjorton /* and delete it from the DBM file */
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot open socache DBM file `%s' for writing "
0797faae937515a5225a36db4a1ec79480d2555cjorton "(delete)",
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s)
a10d8ce69c26142323c66adaba109be1b4baa379wrowe * make sure the expiration for still not-accessed
a10d8ce69c26142323c66adaba109be1b4baa379wrowe * socache entries is done only from time to time
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 /* allocate the key array in a memory sub pool */
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((keylist = apr_palloc(ctx->pool, sizeof(dbmkey)*KEYMAX)) == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton /* pass 1: scan DBM database */
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot open socache DBM file `%s' for "
0797faae937515a5225a36db4a1ec79480d2555cjorton "scanning",
a10d8ce69c26142323c66adaba109be1b4baa379wrowe if (dbmval.dsize <= sizeof(apr_time_t) || dbmval.dptr == NULL)
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((keylist[keyidx].dptr = apr_pmemdup(ctx->pool, dbmkey.dptr, dbmkey.dsize)) != NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton /* pass 2: delete expired elements */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot re-open socache DBM file `%s' for "
0797faae937515a5225a36db4a1ec79480d2555cjorton "expiring",
0797faae937515a5225a36db4a1ec79480d2555cjorton for (i = 0; i < keyidx; i++) {
8aac9d0a02da972909738eb84a5902199f3298cftrawick "DBM socache expiry: "
0797faae937515a5225a36db4a1ec79480d2555cjorton "old: %d, new: %d, removed: %d",
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimstatic void socache_dbm_status(ap_socache_instance_t *ctx, request_rec *r,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton "Cannot open socache DBM file `%s' for status "
0797faae937515a5225a36db4a1ec79480d2555cjorton "retrival",
0797faae937515a5225a36db4a1ec79480d2555cjorton * XXX - Check the return value of apr_dbm_firstkey, apr_dbm_fetch - TBD
0797faae937515a5225a36db4a1ec79480d2555cjorton for ( ; dbmkey.dptr != NULL; apr_dbm_nextkey(dbm, &dbmkey)) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_rprintf(r, "cache type: <b>DBM</b>, maximum size: <b>unlimited</b><br>");
a10d8ce69c26142323c66adaba109be1b4baa379wrowe ap_rprintf(r, "current entries: <b>%d</b>, current size: <b>%ld</b> bytes<br>", elts, size);
a10d8ce69c26142323c66adaba109be1b4baa379wrowe ap_rprintf(r, "average entry size: <b>%d</b> bytes<br>", avg);
250fbbb51225da0dfc973743b795b04dc9740027wrowestatic apr_status_t socache_dbm_iterate(ap_socache_instance_t *ctx,
3e7ce568813f1895b2e8e68e2223653884497bdawrowe * make sure the expired records are omitted
250fbbb51225da0dfc973743b795b04dc9740027wrowe if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
250fbbb51225da0dfc973743b795b04dc9740027wrowe "Cannot open socache DBM file `%s' for "
250fbbb51225da0dfc973743b795b04dc9740027wrowe if (dbmval.dsize <= sizeof(apr_time_t) || dbmval.dptr == NULL)
250fbbb51225da0dfc973743b795b04dc9740027wrowe "Failure reading first/next socache DBM file `%s' record",