Global variables in Apache Server

我的梦境 提交于 2020-01-03 17:29:15

问题


I'm writing some apache (2.2) modules in C and I'm pretty new at it, so I was wondering:

I need to know if it's possible to create a global variable that will be initiated whenever the apache server starts to run.

See, I need to have a list of host names (that will be "privileged"), so that every request I get, I need to check if the host name appears in the list (to check if it's "previleged").

So the list should be global (so that every server instance will have the same instance of the list), and I need to initialize it at the beginning.

How do I do that, if it's at all possible?

Thanks!


回答1:


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...
}



回答2:


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



来源:https://stackoverflow.com/questions/6329785/global-variables-in-apache-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!