Initialize modules in apache2

I used to write apache modules in apache 1.3, but these days I am ready to upgrade to apache2. The module I'm writing at the moment has its own binary data, not a database for performance purposes. I need to load this data into shared memory, so every child can access it without creating their own copy, and it would be practical to load / create binary data at startup, as I'm used to doing with apache 1.3. The problem is that I did not find the init event in apache2, in 1.3 in the modular structure, right after STANDARD_MODULE_STUFF you find the place for the initializer / ** module * /, in which you can put the function that will be executed earlier, the body of the function that I used is something like:

if ( getppid == 1 ) { // Load global data here // this is the parent process void* data = loadGlobalData( someFilePath ); setGlobalData( config, data ); } else { // this is the init of a child process // do nothing } 

I am looking for a place in apache2 where I can put a similar function.

You can help?

Thanks Benvenuto

+4
source share
3 answers

Since you want the server to create one shared memory segment that will be used by all children, I would recommend initializing it in the post post console (ap_hook_post_config). This is called after the configuration has been read, but before the children are created, so it should work well.

+3
source

Since Apache 2.x loads DSO modules twice, ap_hook_post_config () is called twice during Apache startup.

The following code, added to ap_hook_post_config (), will prevent the initialization of your module during the first call and will continue to work only during the second call.

It's a hack, but a neat hack :)

 void *data = NULL; const char *key = "dummy_post_config"; // This code is used to prevent double initialization of the module during Apache startup apr_pool_userdata_get(&data, key, s->process->pool); if ( data == NULL ) { apr_pool_userdata_set((const void *)1, key, apr_pool_cleanup_null, s->process->pool); return OK; } 

You can learn more about dual dso module loads in the Apache wiki .

+3
source

You can use hook_init to initialize a resource that will last longer than a request or connection.

 typedef struct { apr_pool_t *pool; apr_hash_t *hash; } my_server_config; static void my_child_init(apr_pool_t *p, server_rec *s) { my_server_config cfg = ap_get_module_config(s->module_config, &my_module); /* Create sub-pool: ap_pool_create(&cfg->pool, p); */ /* Create hash: cfg->hash = ap_hash_make(cfg->pool); */ } static void my_register_hooks(apr_pool_t *p) { ap_hook_child_init(my_child_init, NULL, NULL, APR_HOOK_MIDDLE); } module AP_MODULE_DECLARE_DATA my_module = { STANDARD20_MODULE_STUFF, NULL, /* per-directory config creator */ NULL, /* dir config merger */ NULL, /* server config creator */ NULL, /* server config merger */ NULL, /* command table */ my_register_hooks, /* set up other request processing hooks */ }; 

A child initialization hook will be called before apache enters operational mode or before threads are created in streaming MPM. The pool passed to the my_child_init function must be a process pool.

For a better example, you should download the Apache source code and check the modules / experimental / mod_example.c file.

+1
source

Source: https://habr.com/ru/post/1308443/


All Articles