httpd-modules-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From michaelr <my...@freenet.de>
Subject apr shared memory handling
Date Mon, 14 Nov 2011 13:18:20 GMT
Hi all,

first excuse my bad english. As a beginner in apache module development
i have a problem understanding the apr shared memeory handling. In the
following example i removed all the locks and error handling to bring my
problem in front. Even the malloc and free calls are only for testing
purposes (in a real module i will use memory pools).

As an example i have the following struct defined in my header:

typedef struct
  {
    unsigned int active ;
    char *test ;
  } shm_seg_t ;


// global defined:
static apr_shm_t *acl_pool_shm = NULL ;                
static shm_seg_t *acl_pool = NULL ;   


In the apache POST-Config-Handler i now do the following:


// just to remove old files from previous segfault httpds - my code
// segfaults often
apr_shm_remove ("/tmp/file" , pconf);

// create the segement:
apr_shm_create(&acl_pool_shm, sizeof(*acl_pool), "/tmp/file", pconf) ;

// get the base address:
acl_pool = (shm_seg_t *) apr_shm_baseaddr_get(acl_pool_shm) ;

// Clear everything.
memset(acl_pool, 0, sizeof(*acl_pool)) ;

// initialize the variables in the struct
acl_pool->active   = 0 ;
acl_pool->test     = NULL ;

// malloc just for testing purposes!
acl_pool->test = (char *) malloc ( 40 * sizeof(char) );  

strcpy(acl_pool->test, "POSTCONFIGHANDLER");
fprintf(stderr," post: --> %s\n", acl_pool->test);
fflush(stderr);

This works. So in the module handler i can access the varaibles 
like this: 

apr_shm_attach(&acl_pool_shm, "/tmp/file", r->pool) ;
acl_pool = (shm_seg_t *) apr_shm_baseaddr_get(acl_pool_shm) ;

printf(stderr, " HANDLER: --> %d - %s\n", 
acl_pool->active, acl_pool->test) ;
fflush(stderr) ;


Everything is fine until here - it prints out:

HANDLER: --> 0 - POSTCONFIGHANDLER

to the error log.


I want to update the data in the shared memeory segement in an
monitor_hook which i set up also. In the monitor_hook i do the
following:

apr_shm_attach(&acl_pool_shm, "/tmp/file", p) ;

acl_pool = (shm_seg_t *) apr_shm_baseaddr_get(acl_pool_shm) ;

acl_pool->active++ ;

if ( acl_pool->test != NULL)
 {
 free(acl_pool->test) ;

 acl_pool->test = (char *) malloc(40 * sizeof(char)) ;
 strcpy(acl_pool->test, "SCHEDULER") ;
 fprintf(stderr, " scheduler: --> %s\n", acl_pool->test) ;
 fflush(stderr) ;
 }


Now i run into the problem that the module_handler did'nt recognise
the changes i have made so far in the monitor_hook. acl_pool->test
did not change in the child process and it always prints out:

HANDLER: --> 1 - POSTCONFIGHANDLER
HANDLER: --> 2 - POSTCONFIGHANDLER
HANDLER: --> 3 - POSTCONFIGHANDLER

The integer varaibles get incremented as expected. The dynamic
variable: char *test did'nt change at all. I set MaxRequestsPerChild 
to zero in this first try.

When i set MaxRequestsPerChild to 4 for something else below and the 
childs get restarted by the root-Server the next call to the
module-handler prints out:

HANDLER: --> 1 - SCHEDULER

So my questions is: Why i can not see the changes which the
monitor hook has done until the childs gets restarted? The integer
variable is readable with the correct update at any time.

Thank's for your help.

Greetings Michael



Mime
View raw message