10ea73
# ./pullrev.sh 1842929 1842931 1852982 1853631 1857731
52592b
http://svn.apache.org/viewvc?view=revision&revision=1842929
52592b
http://svn.apache.org/viewvc?view=revision&revision=1842931
10ea73
http://svn.apache.org/viewvc?view=revision&revision=1852982
10ea73
http://svn.apache.org/viewvc?view=revision&revision=1857731
10ea73
http://svn.apache.org/viewvc?view=revision&revision=1853631
52592b
10ea73
diff --git a/Makefile.in b/Makefile.in
10ea73
index 06b8c5a..9eeb5c7 100644
10ea73
--- a/Makefile.in
10ea73
+++ b/Makefile.in
10ea73
@@ -213,6 +213,7 @@ install-cgi:
10ea73
 install-other:
10ea73
 	@test -d $(DESTDIR)$(logfiledir) || $(MKINSTALLDIRS) $(DESTDIR)$(logfiledir)
10ea73
 	@test -d $(DESTDIR)$(runtimedir) || $(MKINSTALLDIRS) $(DESTDIR)$(runtimedir)
10ea73
+	@test -d $(DESTDIR)$(statedir) || $(MKINSTALLDIRS) $(DESTDIR)$(statedir)
10ea73
 	@for ext in dll x; do \
10ea73
 		file=apachecore.$$ext; \
10ea73
 		if test -f $$file; then \
10ea73
diff --git a/acinclude.m4 b/acinclude.m4
10ea73
index 0ad0c13..a8c2804 100644
10ea73
--- a/acinclude.m4
10ea73
+++ b/acinclude.m4
10ea73
@@ -45,6 +45,7 @@ AC_DEFUN([APACHE_GEN_CONFIG_VARS],[
52592b
   APACHE_SUBST(installbuilddir)
52592b
   APACHE_SUBST(runtimedir)
52592b
   APACHE_SUBST(proxycachedir)
52592b
+  APACHE_SUBST(statedir)
52592b
   APACHE_SUBST(other_targets)
52592b
   APACHE_SUBST(progname)
52592b
   APACHE_SUBST(prefix)
10ea73
@@ -663,6 +664,7 @@ AC_DEFUN([APACHE_EXPORT_ARGUMENTS],[
52592b
   APACHE_SUBST_EXPANDED_ARG(runtimedir)
52592b
   APACHE_SUBST_EXPANDED_ARG(logfiledir)
52592b
   APACHE_SUBST_EXPANDED_ARG(proxycachedir)
52592b
+  APACHE_SUBST_EXPANDED_ARG(statedir)
52592b
 ])
52592b
 
52592b
 dnl 
10ea73
diff --git a/configure.in b/configure.in
10ea73
index a208b53..de6a8ad 100644
10ea73
--- a/configure.in
10ea73
+++ b/configure.in
10ea73
@@ -41,7 +41,7 @@ dnl Something seems broken here.
52592b
 AC_PREFIX_DEFAULT(/usr/local/apache2)
52592b
 
52592b
 dnl Get the layout here, so we can pass the required variables to apr
52592b
-APR_ENABLE_LAYOUT(Apache, [errordir iconsdir htdocsdir cgidir])
52592b
+APR_ENABLE_LAYOUT(Apache, [errordir iconsdir htdocsdir cgidir statedir])
52592b
 
52592b
 dnl reparse the configure arguments.
52592b
 APR_PARSE_ARGUMENTS
10ea73
diff --git a/include/ap_config_layout.h.in b/include/ap_config_layout.h.in
10ea73
index 2b4a70c..e076f41 100644
10ea73
--- a/include/ap_config_layout.h.in
10ea73
+++ b/include/ap_config_layout.h.in
52592b
@@ -60,5 +60,7 @@
52592b
 #define DEFAULT_REL_LOGFILEDIR "@rel_logfiledir@"
52592b
 #define DEFAULT_EXP_PROXYCACHEDIR "@exp_proxycachedir@"
52592b
 #define DEFAULT_REL_PROXYCACHEDIR "@rel_proxycachedir@"
52592b
+#define DEFAULT_EXP_STATEDIR "@exp_statedir@"
52592b
+#define DEFAULT_REL_STATEDIR "@rel_statedir@"
52592b
 
52592b
 #endif /* AP_CONFIG_LAYOUT_H */
10ea73
diff --git a/include/http_config.h b/include/http_config.h
10ea73
index adc5825..effccc1 100644
10ea73
--- a/include/http_config.h
10ea73
+++ b/include/http_config.h
10ea73
@@ -757,6 +757,14 @@ AP_DECLARE(char *) ap_server_root_relative(apr_pool_t *p, const char *fname);
52592b
  */
52592b
 AP_DECLARE(char *) ap_runtime_dir_relative(apr_pool_t *p, const char *fname);
52592b
 
52592b
+/**
52592b
+ * Compute the name of a persistent state file (e.g. a database or
52592b
+ * long-lived cache) relative to the appropriate state directory.
52592b
+ * Absolute paths are returned as-is.  The state directory is
52592b
+ * configured via the DefaultStateDir directive or at build time.
52592b
+ */
52592b
+AP_DECLARE(char *) ap_state_dir_relative(apr_pool_t *p, const char *fname);
52592b
+
52592b
 /* Finally, the hook for dynamically loading modules in... */
52592b
 
52592b
 /**
10ea73
diff --git a/modules/dav/fs/mod_dav_fs.c b/modules/dav/fs/mod_dav_fs.c
10ea73
index addfd7e..2389f8f 100644
10ea73
--- a/modules/dav/fs/mod_dav_fs.c
10ea73
+++ b/modules/dav/fs/mod_dav_fs.c
10ea73
@@ -29,6 +29,10 @@ typedef struct {
52592b
 
52592b
 extern module AP_MODULE_DECLARE_DATA dav_fs_module;
52592b
 
52592b
+#ifndef DEFAULT_DAV_LOCKDB
52592b
+#define DEFAULT_DAV_LOCKDB "davlockdb"
52592b
+#endif
52592b
+
52592b
 const char *dav_get_lockdb_path(const request_rec *r)
52592b
 {
52592b
     dav_fs_server_conf *conf;
10ea73
@@ -57,6 +61,24 @@ static void *dav_fs_merge_server_config(apr_pool_t *p,
10ea73
     return newconf;
10ea73
 }
52592b
 
10ea73
+static apr_status_t dav_fs_post_config(apr_pool_t *p, apr_pool_t *plog,
10ea73
+                                       apr_pool_t *ptemp, server_rec *base_server)
10ea73
+{
10ea73
+    server_rec *s;
52592b
+
10ea73
+    for (s = base_server; s; s = s->next) {
10ea73
+        dav_fs_server_conf *conf;
52592b
+
10ea73
+        conf = ap_get_module_config(s->module_config, &dav_fs_module);
10ea73
+
10ea73
+        if (!conf->lockdb_path) {
10ea73
+            conf->lockdb_path = ap_state_dir_relative(p, DEFAULT_DAV_LOCKDB);
10ea73
+        }
10ea73
+    }
10ea73
+
10ea73
+    return OK;
10ea73
+}
10ea73
+
10ea73
 /*
10ea73
  * Command handler for the DAVLockDB directive, which is TAKE1
10ea73
  */
10ea73
@@ -87,6 +109,8 @@ static const command_rec dav_fs_cmds[] =
52592b
 
10ea73
 static void register_hooks(apr_pool_t *p)
10ea73
 {
10ea73
+    ap_hook_post_config(dav_fs_post_config, NULL, NULL, APR_HOOK_MIDDLE);
10ea73
+
10ea73
     dav_hook_gather_propsets(dav_fs_gather_propsets, NULL, NULL,
10ea73
                              APR_HOOK_MIDDLE);
10ea73
     dav_hook_find_liveprop(dav_fs_find_liveprop, NULL, NULL, APR_HOOK_MIDDLE);
10ea73
diff --git a/modules/md/mod_md_config.c b/modules/md/mod_md_config.c
10ea73
index 336a21b..4d50e26 100644
10ea73
--- a/modules/md/mod_md_config.c
10ea73
+++ b/modules/md/mod_md_config.c
10ea73
@@ -54,10 +54,18 @@
52592b
 
52592b
 #define DEF_VAL     (-1)
52592b
 
52592b
+#ifndef MD_DEFAULT_BASE_DIR
52592b
+#define MD_DEFAULT_BASE_DIR "md"
52592b
+#endif
52592b
+
52592b
 /* Default settings for the global conf */
52592b
 static md_mod_conf_t defmc = {
52592b
     NULL,
52592b
-    "md",
10ea73
+#if 1
10ea73
+    NULL, /* apply default state-dir-relative */
10ea73
+#else
10ea73
+    MD_DEFAULT_BASE_DIR,
10ea73
+#endif
52592b
     NULL,
52592b
     NULL,
52592b
     80,
10ea73
@@ -864,6 +872,12 @@ apr_status_t md_config_post_config(server_rec *s, apr_pool_t *p)
10ea73
     if (mc->hsts_max_age > 0) {
10ea73
         mc->hsts_header = apr_psprintf(p, "max-age=%d", mc->hsts_max_age);
52592b
     }
10ea73
+
10ea73
+#if 1
10ea73
+    if (mc->base_dir == NULL) {
10ea73
+        mc->base_dir = ap_state_dir_relative(p, MD_DEFAULT_BASE_DIR);
10ea73
+    }
10ea73
+#endif
10ea73
     
10ea73
     return APR_SUCCESS;
10ea73
 }
10ea73
diff --git a/server/core.c b/server/core.c
10ea73
index bbe52e0..b5ab429 100644
10ea73
--- a/server/core.c
10ea73
+++ b/server/core.c
10ea73
@@ -133,6 +133,8 @@ AP_DECLARE_DATA int ap_main_state = AP_SQ_MS_INITIAL_STARTUP;
52592b
 AP_DECLARE_DATA int ap_run_mode = AP_SQ_RM_UNKNOWN;
52592b
 AP_DECLARE_DATA int ap_config_generation = 0;
52592b
 
52592b
+static const char *core_state_dir;
52592b
+
52592b
 static void *create_core_dir_config(apr_pool_t *a, char *dir)
52592b
 {
52592b
     core_dir_config *conf;
10ea73
@@ -1411,12 +1413,15 @@ AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
10ea73
     return res_buf;
10ea73
 }
10ea73
 
10ea73
-static int reset_config_defines(void *dummy)
10ea73
+/* pconf cleanup - clear global variables set from config here. */
10ea73
+static apr_status_t reset_config(void *dummy)
10ea73
 {
10ea73
     ap_server_config_defines = saved_server_config_defines;
10ea73
     saved_server_config_defines = NULL;
10ea73
     server_config_defined_vars = NULL;
10ea73
-    return OK;
10ea73
+    core_state_dir = NULL;
10ea73
+
10ea73
+    return APR_SUCCESS;
10ea73
 }
10ea73
 
10ea73
 /*
10ea73
@@ -3108,6 +3113,24 @@ static const char *set_runtime_dir(cmd_parms *cmd, void *dummy, const char *arg)
52592b
     return NULL;
52592b
 }
52592b
 
52592b
+static const char *set_state_dir(cmd_parms *cmd, void *dummy, const char *arg)
52592b
+{
52592b
+    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
52592b
+
52592b
+    if (err != NULL) {
52592b
+        return err;
52592b
+    }
52592b
+
52592b
+    if ((apr_filepath_merge((char**)&core_state_dir, NULL,
52592b
+                            ap_server_root_relative(cmd->temp_pool, arg),
52592b
+                            APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS)
52592b
+        || !ap_is_directory(cmd->temp_pool, core_state_dir)) {
52592b
+        return "DefaultStateDir must be a valid directory, absolute or relative to ServerRoot";
52592b
+    }
52592b
+
52592b
+    return NULL;
52592b
+}
52592b
+
52592b
 static const char *set_timeout(cmd_parms *cmd, void *dummy, const char *arg)
52592b
 {
52592b
     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
10ea73
@@ -4409,6 +4432,8 @@ AP_INIT_TAKE1("ServerRoot", set_server_root, NULL, RSRC_CONF | EXEC_ON_READ,
52592b
   "Common directory of server-related files (logs, confs, etc.)"),
52592b
 AP_INIT_TAKE1("DefaultRuntimeDir", set_runtime_dir, NULL, RSRC_CONF | EXEC_ON_READ,
52592b
   "Common directory for run-time files (shared memory, locks, etc.)"),
52592b
+AP_INIT_TAKE1("DefaultStateDir", set_state_dir, NULL, RSRC_CONF | EXEC_ON_READ,
52592b
+  "Common directory for persistent state (databases, long-lived caches, etc.)"),
52592b
 AP_INIT_TAKE1("ErrorLog", set_server_string_slot,
52592b
   (void *)APR_OFFSETOF(server_rec, error_fname), RSRC_CONF,
52592b
   "The filename of the error log"),
10ea73
@@ -4932,8 +4957,7 @@ static int core_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptem
10ea73
 
10ea73
     if (!saved_server_config_defines)
10ea73
         init_config_defines(pconf);
10ea73
-    apr_pool_cleanup_register(pconf, NULL, reset_config_defines,
10ea73
-                              apr_pool_cleanup_null);
10ea73
+    apr_pool_cleanup_register(pconf, NULL, reset_config, apr_pool_cleanup_null);
10ea73
 
10ea73
     ap_regcomp_set_default_cflags(AP_REG_DOLLAR_ENDONLY);
10ea73
 
10ea73
@@ -5202,6 +5226,27 @@ AP_DECLARE(int) ap_state_query(int query)
52592b
     }
52592b
 }
52592b
 
52592b
+AP_DECLARE(char *) ap_state_dir_relative(apr_pool_t *p, const char *file)
52592b
+{
52592b
+    char *newpath = NULL;
52592b
+    apr_status_t rv;
52592b
+    const char *state_dir;
52592b
+
52592b
+    state_dir = core_state_dir
52592b
+        ? core_state_dir
52592b
+        : ap_server_root_relative(p, DEFAULT_REL_STATEDIR);
52592b
+
52592b
+    rv = apr_filepath_merge(&newpath, state_dir, file, APR_FILEPATH_TRUENAME, p);
52592b
+    if (newpath && (rv == APR_SUCCESS || APR_STATUS_IS_EPATHWILD(rv)
52592b
+                                      || APR_STATUS_IS_ENOENT(rv)
52592b
+                                      || APR_STATUS_IS_ENOTDIR(rv))) {
52592b
+        return newpath;
52592b
+    }
52592b
+    else {
52592b
+        return NULL;
52592b
+    }
52592b
+}
52592b
+
52592b
 static apr_random_t *rng = NULL;
52592b
 #if APR_HAS_THREADS
52592b
 static apr_thread_mutex_t *rng_mutex = NULL;