httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bill Stoddard <b...@wstoddard.com>
Subject Re: Apache calls initialize module twice
Date Mon, 10 Nov 2003 14:13:50 GMT
Nasko wrote:

>    
> 
> Hello guys.
>  
>     I'm running Apache2 on WindowsXP as a service (path to executable = 
> "c:\apache2\Apache.exe" -k runservice in my service configuration ) .
>  
>     I have also 
> ap_hook_post_config  (initialize_module,  NULL,NULL,APR_HOOK_MIDDLE ) in 
> my source code...
>  
>     The problem I have is that when run as a service initalize_module() 
> is called twice.
>     However when I debug in Visual C++ and call Apache2 with  -X , 
> initalize_module() is called only once !!!!
> 
>     Any ideas how to avoid the second call to initalize_module when I 
> run as a service ?
>  
> Thanks in advance,
> Nasko
>    
You can't avoid the second call but there are ways to gracefully handle 
it. Here is a snip of code from a module I developed a while back:

     /* Run post_config only on the second config pass */
     void *data;
     apr_pool_userdata_get(&data, userdata_key, s->process->pool);
     if (!data) {
         apr_pool_userdata_set((const void *)1, userdata_key,
                               apr_pool_cleanup_null, s->process->pool);
         return OK;
     }

#ifdef WIN32
     /* Do not run post_config in the Windows parent process
      * The envar AP_PARENT_PID is set in the env list (by mpm_winnt)
      * for the child process.
      * **WARNING**:
      * This check is not as robust as I would like because the name of 
this
      * envar is subject to change from release to release.
      */
     if (!getenv("AP_PARENT_PID")) {
         return OK;
     }
#endif

This code actually does two things:
1. only allows post_config to run on the -second- pass (you can easily 
tweak the code to make your hook run only on the first pass), and
2. only run post_config in the child process on Windows.

The second check is useful if your hook needs to allocate a lot of 
resources; any resources you allocate in the Windows parent process are 
probably not used or needed.

Bill


Mime
View raw message