I';我正在开发一个Apache2模块。为什么服务启动时会调用两次create_server_config?

I';我正在开发一个Apache2模块。为什么服务启动时会调用两次create_server_config?,apache,apache-modules,apache2-module,Apache,Apache Modules,Apache2 Module,我编写了如下模块代码: static void *example_create_server_config(apr_pool_t *p, server_rec *s) { syslog(LOG_ERR, "create_server_config"); // create my handler // my_handler_t *handler = (my_handler_t *)apr_palloc(pool, sizeof(my_handler_t)); return NULL

我编写了如下模块代码:

static void *example_create_server_config(apr_pool_t *p, server_rec *s)
{
  syslog(LOG_ERR, "create_server_config");
  // create my handler
  // my_handler_t *handler = (my_handler_t *)apr_palloc(pool, sizeof(my_handler_t));
  return NULL;
}

/* Dispatch list for API hooks */ 
module AP_MODULE_DECLARE_DATA example_module = {
    STANDARD20_MODULE_STUFF,
    NULL,                               /* create per-dir    config structures */
    NULL,                               /* merge  per-dir    config structures */
    example_create_server_config,       /* create per-server config structures */
    NULL,                               /* merge  per-server config structures */
    example_cmds,                       /* table of config file commands       */
    example_register_hooks              /* register hooks                      */
};
重新启动Apache时,
/var/log/syslog
包含以下内容:

Jan 31 14:46:49 su02 apache2: create_server_config
Jan 31 14:46:49 su02 apache2: create_server_config
Jan 31 14:46:49 su02 apache2: child_init
为什么要调用两次
create\u server\u config
函数


我在这个函数中使用了一些全局变量。这是否安全?

使用此函数内部接收的apr\u pool\t指针获取内存是完全安全的。此外,对于httpd.conf文件中的每个服务器/主机配置,该函数将被调用一次,因此您会看到对该函数的多次调用。例如,根服务器配置和一个配置部分将使此函数被调用两次

typedef struct
{
    int value;
} my_srv_cfg;

static void *example_create_server_config(apr_pool_t *pool, server_rec *s)
{
    my_srv_cfg *new = apr_pcalloc(pool, sizeof (*new));
    new->value = 100;
    return new; 
}

使用此函数内部接收的apr\u pool\t指针获取内存是非常安全的。此外,对于httpd.conf文件中的每个服务器/主机配置,该函数将被调用一次,因此您会看到对该函数的多次调用。例如,根服务器配置和一个配置部分将使此函数被调用两次

typedef struct
{
    int value;
} my_srv_cfg;

static void *example_create_server_config(apr_pool_t *pool, server_rec *s)
{
    my_srv_cfg *new = apr_pcalloc(pool, sizeof (*new));
    new->value = 100;
    return new; 
}