2014-09-23 68 views
0

我有一個Apache 2.2可加載模塊,它不能正確處理指令處理。Apache 2可加載模塊無法解析指令

該模塊最初使用靜態配置,但現在使用AP_MODULE_DECLARE_DATA中聲明的服務器配置例程使用每服務器分配。我已經確認操作例程正確映射配置數據。

當httpd.conf中沒有TD_LOGDEBUG指令時,一切正常。

當存在TD_LOGDEBUG指令時,在輸入「static const char * logdebug_cfg」時,看起來調用中的模塊配置指針「mconfig」爲null。如果指針被視爲有效,則模塊在服務器啓動時段發生段錯誤。由於缺乏服務器或請求上下文來生成Apache日誌消息,所以調試一直很困難。

在指令解析代碼周圍添加一個條件「if(scfg){」(如Apache模塊站點所示)可以消除段錯誤,但它顯然也會停止解析和存儲的發生。在運行時,我在日誌中看到:

mod_demotest:demotest - logdebug = 0x00078000

這是在預期0x00000003由於「TD_LOGDEBUG 0x3」指令,在服務器配置插入值,而不是httpd.conf中

再次,這是靜態配置原件中的所有工作代碼。代碼的唯一修改是針對每個服務器的配置。

下面的代碼已經從原始模塊中減少到了顯示該問題的最小值。

如果有人能夠提供有關該問題的見解,我將不勝感激。

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

問題解決了。 Apache項目示例代碼驚人地不正確。下面的代碼解決了這個問題。 /*檢索每服務器配置*/ mod_config * scfg = ap_get_module_config(parms-> server-> module_config,&torcheck_module); scfg-> logdebug = htoi((char *)arg); return NULL; – GoGoCrash 2014-09-24 04:30:34

回答

1

問題解決了。針對這種情況的Apache Project示例非常不正確。 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;