5

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!

4

2 回答 2

5

虽然不是一个完整的答案,但我确实设法找到了一种拥有全局变量的方法。

我在进程的全局池(pconf 和 pool)中使用了apr_pool_userdata_getand方法。apr_pool_userdata_set

如需进一步参考:
http : //apr.apache.org/docs/apr/0.9/group_apr_pools.html

例子:

将静态全局数据附加到服务器进程池

char *data = "this is some data";
apr_pool_userdata_setn ((void*) data, "myglobaldata_key", NULL, request->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);

现在检索全局数据:

char *data;
apr_pool_userdata_get ((void**)&data, "myglobaldata_key", request->server->process->pool);
if (data == NULL) {
    // data not set...
}
于 2011-06-17T12:15:07.977 回答
0

此链接表明可以在模块中使用静态/全局变量,它们在从多个线程访问时需要小心。我的观察是,鉴于可能有多个进程(全局变量将存在于一个进程中,由许多线程共享),不应该指望静态初始化。即初始化一次可能是不够的。

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

于 2015-02-20T20:02:53.907 回答