Global variables in Apache Server Global variables in Apache Server apache apache

Global variables in Apache Server


Although not a complete answer, I did manage to find a way to have global variables.

I used the apr_pool_userdata_get and apr_pool_userdata_set methods with the process's global pools (pconf and pool).

For further reference:
http://apr.apache.org/docs/apr/0.9/group_apr_pools.html

Examples:

attach static global data to server process pool

char *data = "this is some data";apr_pool_userdata_setn ((void*) data, "myglobaldata_key", NULL, request->server->process->pool);

attach malloced heap data to server process pool

char *data = strdup("this is some data");apr_pool_userdata_setn ((void*) data, "myglobaldata_key", (apr_status_t(*)(void *))free, request->server->process->pool);

Now retrieve the global data:

char *data;apr_pool_userdata_get ((void**)&data, "myglobaldata_key", request->server->process->pool);if (data == NULL) {    // data not set...}


This link indicates one can use static/global variables in a module, they do require care in accessing from multiple threads. My observation is that given there may be multiple processes (the global variable would live in a process, shared by many threads), the static should not be counted on to be initialized. Ie initializing once probably is not enough.

http://httpd.apache.org/docs/2.2/developer/thread_safety.html#variables