William A. Rowe, Jr. wrote:
> It is almost worth a totally different hook entry point (before
> post_config) such as vhost_init which *would* be called per-vhost
> (starting from the main server config and working through the list.)
>
> I have several modules with the for (s=_server; s; s = s->next) paradigm
> that would be easier to read using such a hook. Although I'm generally
> against adding more cpu-intensive hook phases, this is an init-only hook
> so it's much easier to implement.
I had some spare time and thought I could help with the grunt work - my try
at a patch attached.
HTH
--Geoff
Index: include/http_config.h
===================================================================
RCS file: /home/cvspublic/httpd-2.0/include/http_config.h,v
retrieving revision 1.103
diff -u -r1.103 http_config.h
--- include/http_config.h 31 Oct 2003 22:00:38 -0000 1.103
+++ include/http_config.h 22 Dec 2003 17:04:39 -0000
@@ -1001,6 +1001,17 @@
/**
+ * Run the vhost_init function for each module
+ * @param pconf The config pool
+ * @param plog The logging streams pool
+ * @param ptemp The temporary pool
+ * @param s The virtual host to initialize
+ * @return OK or DECLINED on success anything else is a error
+ */
+AP_DECLARE_HOOK(int,vhost_init,(apr_pool_t *pconf,apr_pool_t *plog,
+ apr_pool_t *ptemp,server_rec *s))
+
+/**
* Run the post_config function for each module
* @param pconf The config pool
* @param plog The logging streams pool
Index: server/config.c
===================================================================
RCS file: /home/cvspublic/httpd-2.0/server/config.c,v
retrieving revision 1.167
diff -u -r1.167 config.c
--- server/config.c 11 Oct 2003 06:37:45 -0000 1.167
+++ server/config.c 22 Dec 2003 17:04:40 -0000
@@ -109,6 +109,7 @@
APR_HOOK_STRUCT(
APR_HOOK_LINK(header_parser)
APR_HOOK_LINK(pre_config)
+ APR_HOOK_LINK(vhost_init)
APR_HOOK_LINK(post_config)
APR_HOOK_LINK(open_logs)
APR_HOOK_LINK(child_init)
@@ -124,6 +125,11 @@
(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp),
(pconf, plog, ptemp), OK, DECLINED)
+
+AP_IMPLEMENT_HOOK_RUN_ALL(int, vhost_init,
+ (apr_pool_t *pconf, apr_pool_t *plog,
+ apr_pool_t *ptemp, server_rec *s),
+ (pconf, plog, ptemp, s), OK, DECLINED)
AP_IMPLEMENT_HOOK_RUN_ALL(int, post_config,
(apr_pool_t *pconf, apr_pool_t *plog,
Index: server/main.c
===================================================================
RCS file: /home/cvspublic/httpd-2.0/server/main.c,v
retrieving revision 1.149
diff -u -r1.149 main.c
--- server/main.c 10 Dec 2003 13:43:14 -0000 1.149
+++ server/main.c 22 Dec 2003 17:04:41 -0000
@@ -444,6 +444,7 @@
const char *temp_error_log = NULL;
process_rec *process;
server_rec *server_conf;
+ server_rec *s; /* vhost-specific server_rec */
apr_pool_t *pglobal;
apr_pool_t *pconf;
apr_pool_t *plog; /* Pool of log streams, reset _after_ each read of conf */
@@ -649,6 +656,14 @@
destroy_and_exit_process(process, 1);
}
+ for (s = server_conf; s; s = s->next) {
+ if ( ap_run_vhost_init(pconf, plog, ptemp, s) != OK) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
+ 0, NULL, "Unable to initialize virtual hosts\n");
+ destroy_and_exit_process(process, 1);
+ }
+ }
+
if ( ap_run_post_config(pconf, plog, ptemp, server_conf) != OK) {
ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
NULL, "Configuration Failed\n");
@@ -692,6 +707,14 @@
ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
0, NULL, "Unable to open logs\n");
destroy_and_exit_process(process, 1);
+ }
+
+ for (s = server_conf; s; s = s->next) {
+ if ( ap_run_vhost_init(pconf, plog, ptemp, s) != OK) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
+ 0, NULL, "Unable to initialize virtual hosts\n");
+ destroy_and_exit_process(process, 1);
+ }
}
if (ap_run_post_config(pconf, plog, ptemp, server_conf) != OK) {
Index: modules/experimental/mod_example.c
===================================================================
RCS file: /home/cvspublic/httpd-2.0/modules/experimental/mod_example.c,v
retrieving revision 1.44
diff -u -r1.44 mod_example.c
--- modules/experimental/mod_example.c 13 Dec 2003 15:41:33 -0000 1.44
+++ modules/experimental/mod_example.c 22 Dec 2003 17:12:18 -0000
@@ -859,6 +859,25 @@
}
/*
+ * This routine is called to do virtual host initialization.
+ * It is invoked just before the post_config phase.
+ *
+ * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
+ * server will still call any remaining modules with an handler for this
+ * phase.
+ */
+static int x_vhost_init(apr_pool_t *pconf, apr_pool_t *plog,
+ apr_pool_t *ptemp, server_rec *s)
+{
+ /*
+ * Log the call and exit.
+ */
+ trace_add(s, NULL, NULL, apr_psprintf(ptemp, "x_vhost_init() for %s host %s",
+ s->is_virtual ? "virtual" : "",
+ s->server_hostname));
+ return OK;
+}
+/*
* This routine is called to perform any module-specific fixing of header
* fields, et cetera. It is invoked just before any content-handler.
*
@@ -872,13 +891,13 @@
/*
* Log the call and exit.
*/
- trace_add(NULL, NULL, NULL, "x_post_config()");
+ trace_add(s, NULL, NULL, "x_post_config()");
return OK;
}
/*
* This routine is called to perform any module-specific log file
- * openings. It is invoked just before the post_config phase
+ * openings. It is invoked just before the vhost_init phase
*
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
* server will still call any remaining modules with an handler for this
@@ -1306,6 +1325,7 @@
ap_hook_pre_config(x_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_post_config(x_post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_open_logs(x_open_logs, NULL, NULL, APR_HOOK_MIDDLE);
+ ap_hook_vhost_init(x_vhost_init, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_child_init(x_child_init, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_handler(x_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_quick_handler(x_quick_handler, NULL, NULL, APR_HOOK_MIDDLE);