【问题标题】:Apache 2 loadable module fails to parse directiveApache 2 可加载模块无法解析指令
【发布时间】:2014-11-17 05:00:59
【问题描述】:

我有一个 Apache 2.2 可加载模块,它不能正确处理指令。

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

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

当有 TD_LOGDEBUG 指令时,在进入“static const char *logdebug_cfg”时,调用中的模块配置指针“mconfig”似乎为空。如果指针被视为有效,则模块在服务器启动时出现段错误。由于此时缺少服务器或请求上下文来生成 Apache 日志消息,调试一直很困难。

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

mod_demotest: 演示测试-logdebug = 0x00078000

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

同样,这是静态配置原始代码中的所有工作代码。代码的唯一修改是针对每个服务器的配置。

下面的代码已经从原始模块减少到显示问题的最小值。

如果有人能提供有关此问题的见解,我将不胜感激。

#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
};

【问题讨论】:

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

标签: apache module directive


【解决方案1】:

问题解决了。这种情况下的 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;

【讨论】:

    猜你喜欢
    • 2021-07-28
    • 2016-11-04
    • 2017-06-06
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    相关资源
    最近更新 更多