gpt4 book ai didi

c++ - 在读后配置中使用 ap_retained_data_create/get 开发跨平台 Apache 模块

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:55:09 26 4
gpt4 key购买 nike

在构建跨平台模块时,我非常高兴地调试了数小时 Apache 2.4 设计中的一个怪癖。

我遇到的问题是 Apache 在启动时加载配置文件两次,因此加载模块本身两次。一旦我猜想测试配置并读取命令结构,然后下一步填充服务器运行的空白。这通常不会有任何问题,但我需要在为任何客户端提供服务之前在加载时加载我的模块的配置,并且只解析我的模块的需求一次又一次。

由于我使用了很多资源,包括数据库等,我认为多次运行并不是最好的主意,尤其是在数据库服务器上进行访问时。

在线手册(“Developer API 2.5”)说,不要使用旧方法(我会让读者自行查找),而是使用 ap_retained_data_get 和 ap_retained_data_create 在模块卸载期间保留数据。最好将标志传递给您已经通过测试阶段的下一阶段。

这就是头疼的地方。这不是它在 Windows 上的工作方式。

最佳答案

Linux 确实分两次运行,但 Windows 分四次运行。

在配置后读取 Hook 中使用此方法适用于 Linux 但不适用于 Windows

    // variables for base config start
const char *flag = "some_prefixed_flag_to_mashup_with_other_flags";
void *init_flag = NULL;
int dbl = APLOG_TRACE4;
// logger
logging logger(NULL, s, p);
// determine if this is the first time we have loaded
init_flag = ap_retained_data_get(flag);
// check flag result
if (init_flag == NULL)
{
// breakpoint
stdLog(logger, INFX_LOG_DATA, dbl);
// set first time flag local
ap_retained_data_create(flag, 1);
}
// call initization routine
else
{
// do something here

}

请注意,我在我的模块中导出 C++ 代码,因此使用了类。为简洁起见,也没有一些变量声明。

所以这对于 Windows 应该足够了吧?错误的。我必须向后学习这个,因为我首先在 Windows 上构建,但 Windows 有四次而不是两次。你的初始化仍然会运行两次。

我的下一个解决方案如下:

    // variables for base config start
const char *flag = "some_prefixed_flag_to_mashup_with_other_flags";
void *init_flag = NULL;
char *pidname;
int dbl = APLOG_TRACE4;
pid_t pidNKey;
apr_file_t *pidfile;
// logger
logging logger(NULL, s, p);
// determine if this is the first time we have loaded
init_flag = ap_retained_data_get(flag);
// check flag result
if (init_flag == NULL)
{
// breakpoint
stdLog(logger, INFX_LOG_DATA, dbl);
// set first time flag local
ap_retained_data_create(flag, 1);
}
else
{
// break point
stdLog(logger, INFX_LOG_DATA, dbl);
// create a pid if not exists
if (ap_read_pid(p, "logs/httpd.pid", &pidNKey) == OK)
{
// break point
stdLog(logger, INFX_LOG_DATA, dbl);
// create a pid especially for our setup
pidname = apr_psprintf(ptemp, "logs/infx.%d.pid", pidNKey);
// if pidfile does not exist then create it
if (!fileExists(pidname, ptemp))
{
// break point
stdLog(logger, INFX_LOG_DATA, dbl);
// create the pid
apr_file_open(&pidfile, pidname, APR_WRITE|APR_APPEND|APR_CREATE, INFX_BASE_PERM, ptemp);
// add nonsensical data to it
apr_file_puts("1", pidfile);
// cllose the file and wait for run 2
apr_file_close(pidfile);
}
// begin work
else
{
// break point
stdLog(logger, INFX_LOG_DATA, dbl);
// we no longer require the pid file
apr_file_remove(pidname, ptemp);
}
}
}

似乎有效……好的,我已经完成了那部分,对吧?错误的!我转到了 Linux box 和 segfault city。我敢肯定,许多经验丰富的 Apache 开发人员现在可能正在摇头,但正是这些人没有记录这些问题。

我的最终修复是将 Windows 代码包装在定义 block 中。我不确定是否有更好的方法,但它对我有用。因此,以下内容适用于两个平台而没有段错误。

    // variables for base config start
const char *flag = "some_prefixed_flag_to_mashup_with_other_flags";
void *init_flag = NULL;
int dbl = APLOG_TRACE4;
// logger
logging logger(NULL, s, p);
// determine if this is the first time we have loaded
init_flag = ap_retained_data_get(flag);
// check flag result
if (init_flag == NULL)
{
// breakpoint
stdLog(logger, INFX_LOG_DATA, dbl);
// set first time flag local
ap_retained_data_create(flag, 1);
}
// call initization routine
else
{
// break point
stdLog(logger, INFX_LOG_DATA, dbl);
#if defined(WIN32)
// create a pid if not exists
if (ap_read_pid(p, "logs/httpd.pid", &pidNKey) == OK)
{
// break point
stdLog(logger, INFX_LOG_DATA, dbl);
// create a pid especially for our setup
pidname = apr_psprintf(ptemp, "logs/infx.%d.pid", pidNKey);
// if pidfile does not exist then create it
if (!fileExists(pidname, ptemp))
{
// break point
stdLog(logger, INFX_LOG_DATA, dbl);
// create the pid
apr_file_open(&pidfile, pidname, APR_WRITE|APR_APPEND|APR_CREATE, INFX_BASE_PERM, ptemp);
// add nonsensical data to it
apr_file_puts("1", pidfile);
// cllose the file and wait for run 2
apr_file_close(pidfile);
}
// begin work
else
{
// break point
stdLog(logger, INFX_LOG_DATA, dbl);
// we no longer require the pid file
apr_file_remove(pidname, ptemp);
#endif
// do something here for both platforms
#if defined(WIN32)
}
}
// crash if we do get a proper pid
else
{
// breakpoint
stdLog(logger, INFX_LOG_DATA, APLOG_CRIT, "HTTPD File not found? A Bug?");
// set status
return HTTP_INTERNAL_SERVER_ERROR;
}
#endif
}

希望其他人可以从中受益。

关于c++ - 在读后配置中使用 ap_retained_data_create/get 开发跨平台 Apache 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10845256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com