From dce91079c126cffc7807b2fcf0a8a013e5e79a3a Mon Sep 17 00:00:00 2001 From: Joe Orton Date: Tue, 9 Dec 2008 13:11:45 +0000 Subject: Reduce config overhead for use of socache interface by allowing default paths to be used if none are configured: * include/ap_socache.h (ap_socache_provider_t::create): Allow arg to be NULL to force use of defaults. (ap_socache_provider_t::init): Rename 'namespace' parameter to 'cname' and restrict to allow use in filesystem paths. * modules/cache/mod_socache_dbm.c (socache_dbm_create, socache_dbm_init), modules/cache/mod_socache_shmcb.c (socache_shmcb_create, socache_shmcb_init): Default to use of runtimedir-relative paths if no explicit path is configured. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@724682 13f79535-47bb-0310-9956-ffa450edef68 --- include/ap_socache.h | 19 ++++++++++++------- modules/cache/mod_socache_dbm.c | 24 ++++++++++++++++++------ modules/cache/mod_socache_shmcb.c | 32 ++++++++++++++++++++++++++------ 3 files changed, 56 insertions(+), 19 deletions(-) diff --git a/include/ap_socache.h b/include/ap_socache.h index 1f1befb1a7..b037c0a6ee 100644 --- a/include/ap_socache.h +++ b/include/ap_socache.h @@ -67,7 +67,8 @@ typedef struct ap_socache_provider_t { * passed as the first argument to subsequent invocations. * * @param instance Output parameter to which instance object is written. - * @param arg Used-specified configuration string + * @param arg Used-specified configuration string. May be NULL to + * force use of defaults. * @param tmp Pool to be used for any temporary allocations * @param p Pool to be use for any allocations lasting as long as * the created instance @@ -76,18 +77,22 @@ typedef struct ap_socache_provider_t { const char *(*create)(ap_socache_instance_t **instance, const char *arg, apr_pool_t *tmp, apr_pool_t *p); - /* Initialize the cache. NAMESPACE must given a unique string - * prefix for use with memcached; if hints is non-NULL, it gives a - * set of hints for the provider. Return APR error code. - + /* Initialize the cache. The cname must be of maximum length 16 + * characters, and uniquely identifies the consumer of the cache + * within the server; using the module name is recommended, e.g. + * "mod_ssl-sess". This string may be used within a filesystem + * path so use of only alphanumeric [a-z0-9_-] characters is + * recommended. If hints is non-NULL, it gives a set of hints for + * the provider. Return APR error code. + * * @param instance The cache instance - * @param namespace A unique string identifying the consumer of this API + * @param cname A unique string identifying the consumer of this API * @param hints Optional hints argument describing expected cache use * @param s Server structure to which the cache is associated * @param pool Pool for long-lived allocations * @return APR status value indicating success. */ - apr_status_t (*init)(ap_socache_instance_t *instance, const char *namespace, + apr_status_t (*init)(ap_socache_instance_t *instance, const char *cname, const struct ap_socache_hints *hints, server_rec *s, apr_pool_t *pool); diff --git a/modules/cache/mod_socache_dbm.c b/modules/cache/mod_socache_dbm.c index c73cf5f169..da0f4dc025 100644 --- a/modules/cache/mod_socache_dbm.c +++ b/modules/cache/mod_socache_dbm.c @@ -53,6 +53,8 @@ struct ap_socache_instance_t { */ #define SSL_DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD ) +#define DEFAULT_DBM_PREFIX DEFAULT_REL_RUNTIMEDIR "/socache-dbm-" + /* ### this should use apr_dbm_usednames. */ #if !defined(SSL_DBM_FILE_SUFFIX_DIR) && !defined(SSL_DBM_FILE_SUFFIX_PAG) #if defined(DBM_SUFFIX) @@ -81,9 +83,11 @@ static const char *socache_dbm_create(ap_socache_instance_t **context, *context = ctx = apr_pcalloc(p, sizeof *ctx); - ctx->data_file = ap_server_root_relative(p, arg); - if (!ctx->data_file) { - return apr_psprintf(tmp, "Invalid cache file path %s", arg); + if (arg && *arg) { + ctx->data_file = ap_server_root_relative(p, arg); + if (!ctx->data_file) { + return apr_psprintf(tmp, "Invalid cache file path %s", arg); + } } apr_pool_create(&ctx->pool, p); @@ -101,9 +105,17 @@ static apr_status_t socache_dbm_init(ap_socache_instance_t *ctx, /* for the DBM we need the data file */ if (ctx->data_file == NULL) { - ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, - "SSLSessionCache required"); - return APR_EINVAL; + const char *path = apr_pstrcat(p, DEFAULT_DBM_PREFIX, namespace, + NULL); + + ctx->data_file = ap_server_root_relative(p, path); + + if (ctx->data_file == NULL) { + ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, + "could not use default path '%s' for DBM socache", + path); + return APR_EINVAL; + } } /* open it once to create it and to make sure it _can_ be created */ diff --git a/modules/cache/mod_socache_shmcb.c b/modules/cache/mod_socache_shmcb.c index 394a23cb57..30f5402840 100644 --- a/modules/cache/mod_socache_shmcb.c +++ b/modules/cache/mod_socache_shmcb.c @@ -269,9 +269,15 @@ static const char *socache_shmcb_create(ap_socache_instance_t **context, /* Allocate the context. */ *context = ctx = apr_pcalloc(p, sizeof *ctx); - ctx->data_file = path = ap_server_root_relative(p, arg); ctx->shm_size = 1024*512; /* 512KB */ + if (!arg || *arg == '\0') { + /* Use defaults. */ + return NULL; + } + + ctx->data_file = path = ap_server_root_relative(p, arg); + cp = strchr(path, '('); if (cp) { *cp++ = '\0'; @@ -301,6 +307,9 @@ static const char *socache_shmcb_create(ap_socache_instance_t **context, return NULL; } +#define DEFAULT_SHMCB_PREFIX DEFAULT_REL_RUNTIMEDIR "/socache-shmcb-" +#define DEFAULT_SHMCB_SUFFIX ".cache" + static apr_status_t socache_shmcb_init(ap_socache_instance_t *ctx, const char *namespace, const struct ap_socache_hints *hints, @@ -315,14 +324,25 @@ static apr_status_t socache_shmcb_init(ap_socache_instance_t *ctx, /* Create shared memory segment */ if (ctx->data_file == NULL) { - ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, - "SSLSessionCache required"); - return APR_EINVAL; + const char *path = apr_pstrcat(p, DEFAULT_SHMCB_PREFIX, namespace, + DEFAULT_SHMCB_SUFFIX, NULL); + + ctx->data_file = ap_server_root_relative(p, path); } /* Use anonymous shm by default, fall back on name-based. */ rv = apr_shm_create(&ctx->shm, ctx->shm_size, NULL, p); if (APR_STATUS_IS_ENOTIMPL(rv)) { + /* If anon shm isn't supported, fail if no named file was + * configured successfully; the ap_server_root_relative call + * above will return NULL for invalid paths. */ + if (ctx->data_file == NULL) { + ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, + "Could not use default path '%s' for shmcb socache", + ctx->data_file); + return APR_EINVAL; + } + /* For a name-based segment, remove it first in case of a * previous unclean shutdown. */ apr_shm_remove(ctx->data_file, p); @@ -332,8 +352,8 @@ static apr_status_t socache_shmcb_init(ap_socache_instance_t *ctx, if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, - "could not allocate shared memory for shmcb " - "session cache"); + "Could not allocate shared memory segment for shmcb " + "socache"); return rv; } -- cgit v1.2.3