Apache 2可加载模块无法解析指令

Apache 2可加载模块无法解析指令,apache,module,directive,Apache,Module,Directive,我有一个Apache2.2可加载模块,它不能正确处理指令处理 该模块最初使用静态配置,但现在使用AP_模块_DECLARE_数据中声明的服务器配置例程进行每服务器分配。我已经确认操作例程正确映射了配置数据 当httpd.conf中没有TD_LOGDEBUG指令时,一切都正常工作 当存在TD_LOGDEBUG指令时,在输入“static const char*LOGDEBUG_cfg”时,调用中的模块配置指针“mconfig”似乎为null。如果指针被视为有效,则模块将在服务器启动时发生故障。由于

我有一个Apache2.2可加载模块,它不能正确处理指令处理

该模块最初使用静态配置,但现在使用AP_模块_DECLARE_数据中声明的服务器配置例程进行每服务器分配。我已经确认操作例程正确映射了配置数据

当httpd.conf中没有TD_LOGDEBUG指令时,一切都正常工作

当存在TD_LOGDEBUG指令时,在输入“static const char*LOGDEBUG_cfg”时,调用中的模块配置指针“mconfig”似乎为null。如果指针被视为有效,则模块将在服务器启动时发生故障。由于此时缺少生成Apache日志消息的服务器或请求上下文,调试一直很困难

在指令解析代码周围添加一个条件“if(scfg){”(如Apache模块站点所示)可以消除segfault,但它显然也会阻止解析和存储的发生。在运行时,我在日志中看到:

mod_demost:demost-logdebug=0x00078000

这是在服务器配置中插入的值,而不是由于httpd.conf中的“TD_LOGDEBUG 0x3”指令而预期的0x00000003

同样,这些都是静态配置中的原始工作代码。代码中唯一的mod用于每服务器配置

下面的代码已从原来的模块中缩减到显示问题的最小值

如果有人能深入了解这个问题,我将不胜感激

#include "httpd.h"
#include "http_config.h"
#include "http_request.h"
#include "http_protocol.h"
#include "http_core.h"
#include "http_main.h"
#include "http_log.h"
#include "ap_mpm.h"
#include "apr_strings.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netdb.h>

#define MODULE_NAME "mod_demotest"
#define MODULE_VERSION "2.0.1"                /* Module revision level */

module AP_MODULE_DECLARE_DATA demotest_module;

static int demotest_handler(request_rec *r);
static int demotest_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s);

typedef struct {
  unsigned long logdebug;
} mod_config;

static void str_to_lower(char *string) {
while (*string) {
  if ( (*string >= 'A') && (*string <= 'Z') ) *string = *string + 32;
  string++;
  }
}

unsigned long htoi(char *ptr) {
  unsigned long value = 0;
  char ch = *ptr;
  str_to_lower(ptr);
  while ( (ch == '0') || (ch == 'x') ) ch = *(++ptr);
  while ( ( (ch >= '0') && (ch <= '9') ) || ( (ch >= 'a') && (ch <= 'f') ) ) {
    if (ch >= '0' && ch <= '9')
      value = (value << 4) + (ch - '0');
    if (ch >= 'a' && ch <= 'f')
      value = (value << 4) + (ch - 'a' + 10);
    ch = *(++ptr);
  }
  return value;
}

static int demotest_handler
       (request_rec *r) {
  mod_config *scfg = ap_get_module_config(r->server->module_config,
                                          &demotest_module);
  ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
                "mod_demotest:  demotest - logdebug = 0x%08x",
                scfg->logdebug);
  return DECLINED;
}

static const char *logdebug_cfg
       (cmd_parms *parms, void *mconfig, const char *arg) {
  mod_config *scfg = (mod_config *)mconfig;
  if (scfg) {
    scfg->logdebug = htoi((char *)arg);
  }
  return NULL;
}

static void *demotest_server_config
       (apr_pool_t *p, server_rec *s) {
  mod_config *scfg;
  scfg = apr_palloc(p, sizeof(*scfg));
  scfg->logdebug = 0x78000;
  return (void *)scfg;
}

static int demotest_post_config
       (apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) {
  const char *userdata_key = "demotest_init";
  void *data = NULL;
  apr_pool_userdata_get(&data, userdata_key, s->process->pool);
  if (data == NULL) {
    apr_pool_userdata_set((const void *)1, userdata_key,
                          apr_pool_cleanup_null, s->process->pool);
    return OK;
  }
  ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
               MODULE_NAME " " MODULE_VERSION " started");
  return OK;
}

static void register_hooks(apr_pool_t *p) {
  ap_hook_post_config(demotest_post_config, NULL, NULL, APR_HOOK_MIDDLE);
  ap_hook_access_checker(demotest_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

static command_rec demotest_directives[] = {
  AP_INIT_TAKE1("TD_LogDebug", logdebug_cfg, NULL, RSRC_CONF,
                "Log internal trace/debug info.  Default: 0x0000 = none"),
  {NULL}
};

module AP_MODULE_DECLARE_DATA demotest_module = {
    STANDARD20_MODULE_STUFF,
    NULL,                       /* create per-dir    config structures */
    NULL,                       /* merge  per-dir    config structures */
    demotest_server_config,     /* create per-server config structures */
    NULL,                       /* merge  per-server config structures */
    demotest_directives,        /* table of config file commands       */
    register_hooks
};
#包括“httpd.h”
#包括“http_config.h”
#包括“http_request.h”
#包括“http_protocol.h”
#包括“http_core.h”
#包括“http_main.h”
#包括“http_log.h”
#包括“ap_mpm.h”
#包括“apr_strings.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义模块名称“mod\u demost”
#定义模块版本“2.0.1”/*模块修订级别*/
模块AP\U模块\U声明\U数据降级\U模块;
静态int demost_处理程序(请求记录*r);
静态int demost\u post\u配置(apr\u pool\u t*p、apr\u pool\u t*plog、apr\u pool\u t*ptemp、服务器记录*s);
类型定义结构{
无符号长logdebug;
}mod_配置;
静态void str_to_lower(字符*字符串){
while(*字符串){
如果((*string>='A')&(*string='0')&&(ch='A')&(ch='0'&&ch logdebug);
回报率下降;
}
静态常量字符*logdebug\u cfg
(cmd_parms*parms,void*mconfig,const char*arg){
mod_config*scfg=(mod_config*)mconfig;
如果(scfg){
scfg->logdebug=htoi((字符*)arg);
}
返回NULL;
}
静态void*demost\u服务器\u配置
(apr_pool_t*p,服务器记录){
模块配置*scfg;
scfg=apr_palloc(p,sizeof(*scfg));
scfg->logdebug=0x78000;
返回(无效*)scfg;
}
静态int demost\u post\u配置
(apr_pool_t*p、apr_pool_t*plog、apr_pool_t*ptemp、服务器记录){
const char*userdata\u key=“demost\u init”;
void*data=NULL;
apr\u pool\u userdata\u get(&data,userdata\u key,s->process->pool);
如果(数据==NULL){
apr\u pool\u userdata\u set((const void*)1,userdata\u key,
apr\u pool\u cleanup\u null,s->process->pool);
返回OK;
}
ap_日志_错误(APLOG_标记,APLOG_临界值,0,s,
模块名称“模块版本”已启动);
返回OK;
}
静态无效寄存器挂钩(apr\U pool\U t*p){
ap\u hook\u post\u config(最差的post\u config,NULL,NULL,APR\u hook\u MIDDLE);
ap_hook_access_checker(最差的处理程序,NULL,NULL,APR_hook_MIDDLE);
}
静态命令_recdemost_指令[]={
AP_INIT_TAKE1(“TD_LogDebug”,LogDebug_cfg,NULL,RSRC_CONF,
“记录内部跟踪/调试信息。默认值:0x0000=无”),
{NULL}
};
模块AP\U模块\U声明\U数据降级\U模块={
标准20_模块_材料,
NULL,/*创建每目录配置结构*/
NULL,/*按目录合并配置结构*/
Demost_server_config,/*创建每服务器配置结构*/
NULL,/*每个服务器合并配置结构*/
Demost_指令,/*配置文件命令表*/
登记钩
};

问题已解决。这种情况下的Apache项目示例明显不正确。mconfig不是指向模块配置的指针;调用时它始终为NULL

分辨率如下所示

static const char *logdebug_cfg
   (cmd_parms *parms, void *mconfig, const char *arg) {

/* Retrieve the per-server configuration */

mod_config *scfg = ap_get_module_config(parms->server->module_config, &torcheck_module);

scfg->logdebug = htoi((char *)arg);

return NULL;

问题已解决。Apache项目示例代码明显错误。下面的代码解决了问题。/*检索每服务器配置*/mod_config*scfg=ap_get_module_config(parms->server->module_config,&torcheck_module);scfg->logdebug=htoi((char*)arg;返回NULL;