Module develop --nginx segmentation fault

Hi ,
I developed a nginx module, and compile is ok.
When I test the config file without the module directive ,it’s ok.
But when I test the config file with the module directive ,
segmentationo fault.
I add log in the command set function and found the 3rd parameter is
null .
I have set the correct location create&merge conf function in the
module ctx structure.
The directive is flag is
“NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1”

Any idea on the reason?

BR,
DeltaY

What 3rd parameter if you said, that directive can take only one?

NGX_CONF_TAKE1 means, that:

ngx_str_t *value = cf->args->elts;

ngx_str_t *directive_name = &value[0]; // name of the directive
ngx_str_t *directive_param1 = &value[1]; // first (and last) directive
parameter

Hi,
ncache_http_xxxx_enable(ngx_conf_t *cf, ngx_command_t *cmd, void
*conf)

the conf is NULL.

2008/8/21 Sergey B. [email protected]:

I want to refactor the ncache2.0 because it don’t follow the “best
practice” module configuration.
I want to add a on/off flag like the gzip module .
I comment some call back function in module configuration when I 'm
debuging it .

the code segment is :

static ngx_command_t ncache_http_get_cache_commands[] = {

{ ngx_string(“ncache_http_cache”),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ncache_http_cache_enable,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL },

}

static ngx_http_module_t ncache_http_get_cache_module_ctx = {
NULL, /* preconfiguration /
NULL,//ncache_http_upstream_cache_init, /
postconfiguration */

NULL, /* create main configuration /
NULL, /
init main configuration */

NULL, /* create server configuration
/
NULL, /
merge server configuration
*/

ngx_http_ncache_create_conf, /* create location configuration /
ngx_http_ncache_merge_conf /
merge location configuration */
};

ngx_module_t ncache_http_get_cache_module = {
NGX_MODULE_V1,
&ncache_http_get_cache_module_ctx, /* module context /
ncache_http_get_cache_commands, /
module directives /
NGX_HTTP_MODULE, /
module type /
NULL, /
init master /
NULL,//ncache_http_proxy_module_init, /
init module /
NULL,//ncache_http_upstream_cache_process_init,/
init process /
NULL, /
init thread /
NULL, /
exit thread /
NULL, /
exit process /
NULL, /
exit master */
NGX_MODULE_V1_PADDING
};

static void *
ngx_http_ncache_create_conf(ngx_conf_t *cf)
{
ngx_http_ncache_conf_t *conf;

ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
“NCACHE CONF create”);
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ncache_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}

conf->enable = 0;
conf->ignore_client_no_cache=0;
conf->max_hash_size=24;

return conf;

}

static char *
ngx_http_ncache_merge_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_ncache_conf_t *prev = parent;
ngx_http_ncache_conf_t *conf = child;

ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"NCACHE CONF MERGE ");

ngx_conf_merge_value(conf->enable, prev->enable, 0);
ngx_conf_merge_value(conf->ignore_client_no_cache,

prev->ignore_client_no_cache, 0);
ngx_conf_merge_value(conf->max_hash_size, prev->max_hash_size, 24);
if (conf->cache_dirs== NULL) {
conf->cache_dirs = prev->cache_dirs;
cache_dirs=conf->cache_dirs;
}

return NGX_CONF_OK;

}

static char *
ncache_http_cache_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{

ngx_http_ncache_conf_t* ncachecf=conf;
ngx_str_t *value;

value = cf->args->elts;

ngx_strlow(value[1].data, value[1].data, value[1].len);

if(ncachecf == NULL){
ngx_conf_log_error(NGX_LOG_EMERG,cf,0,“NULL conf”);
return NGX_CONF_ERROR;
}
if (ngx_strcasecmp(value[1].data, (u_char *) “on”) == 0) {

ncachecf->enable=1;
ngx_http_core_loc_conf_t  *clcf ;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

clcf->handler = ncache_http_get_cache_handler;

  }else{

    ncachecf->enable=0;
  }
 return NGX_CONF_OK;

}

when I run test the config, the out put is :

sbin/nginx -t -c conf/nginx.conf

2008/08/21 17:52:21 [emerg] 19056#0: NCACHE CONF create in
conf/nginx.conf:14
2008/08/21 17:52:21 [emerg] 19056#0: NCACHE CONF create in
conf/nginx.conf:83
2008/08/21 17:52:21 [emerg] 19056#0: NCACHE CONF create in
conf/nginx.conf:89
2008/08/21 17:52:21 [emerg] 19056#0: NCACHE CONF create in
conf/nginx.conf:94
2008/08/21 17:52:21 [emerg] 19056#0: NCACHE CONF create in
conf/nginx.conf:101
2008/08/21 17:52:21 [emerg] 19056#0: NCACHE CONF create in
conf/nginx.conf:112
2008/08/21 17:52:21 [emerg] 19056#0: NCACHE CONF create in
conf/nginx.conf:120
2008/08/21 17:52:21 [emerg] 19056#0: NCACHE CONF create in
conf/nginx.conf:123
2008/08/21 17:52:21 [emerg] 19056#0: NCACHE CONF create in
conf/nginx.conf:126
2008/08/21 17:52:21 [emerg] 19056#0: NCACHE CONF create in
conf/nginx.conf:130
2008/08/21 17:52:21 [emerg] 19056#0: value is 2 ,1.ncache_http_cache
2.off in conf/nginx.conf:132
2008/08/21 17:52:21 [emerg] 19056#0: NULL conf in conf/nginx.conf:132
2008/08/21 17:52:21 [emerg] 19056#0: the configuration file
conf/nginx.conf test failed

when i test the config file without the ncache_http_cache directive ,
the output is :

sbin/nginx -t -c conf/nginx2.conf

2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF create in
conf/nginx2.conf:14
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF create in
conf/nginx2.conf:83
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF create in
conf/nginx2.conf:89
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF create in
conf/nginx2.conf:94
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF create in
conf/nginx2.conf:101
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF create in
conf/nginx2.conf:110
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF create in
conf/nginx2.conf:118
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF create in
conf/nginx2.conf:121
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF create in
conf/nginx2.conf:124
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF create in
conf/nginx2.conf:128
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF create in
conf/nginx2.conf:134
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF MERGE in
conf/nginx2.conf:150
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF MERGE in
conf/nginx2.conf:150
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF MERGE in
conf/nginx2.conf:150
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF MERGE in
conf/nginx2.conf:150
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF MERGE in
conf/nginx2.conf:150
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF MERGE in
conf/nginx2.conf:150
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF MERGE in
conf/nginx2.conf:150
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF MERGE in
conf/nginx2.conf:150
2008/08/21 19:43:16 [emerg] 19166#0: NCACHE CONF MERGE in
conf/nginx2.conf:150
2008/08/21 19:43:16 [info] 19166#0: the configuration file
conf/nginx2.conf syntax is ok
2008/08/21 19:43:16 [info] 19166#0: the configuration file
conf/nginx2.conf was tested successfully

2008/8/21 Delta Y. [email protected]:

Hi Igor,
You are absolutely the GOD of nginx.
I love you !

BR,
DeltaY

2008/8/21 Igor S. [email protected]:

On Thu, Aug 21, 2008 at 07:30:50PM +0800, Delta Y. wrote:

ncache_http_xxxx_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

the conf is NULL.

This probably means that create conf function has stored conf in wrong
place.
Could you show the full directive definition ?
Do you use NGX_HTTP_LOC_CONF_OFFSET ?

On Thu, Aug 21, 2008 at 07:44:24PM +0800, Delta Y. wrote:

{ ngx_string(“ncache_http_cache”),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ncache_http_cache_enable,
NGX_HTTP_MAIN_CONF_OFFSET,

  • NGX_HTTP_MAIN_CONF_OFFSET,
  • NGX_HTTP_LOC_CONF_OFFSET,

On Thu, Aug 21, 2008 at 07:52:57PM +0800, Delta Y. wrote:

Hi Igor,
You are absolutely the GOD of nginx.

I’m author :slight_smile:

It’s your custom config struct. You should allocate memory for it by
yourself.

Example:

typedef struct {
// fields

} ngx_http_super_loc_conf_t;

static ngx_http_module_t ngx_http_super_module_ctx =
{
NULL, // preconfiguration
ngx_http_super_filter_init, // postconfiguration

NULL, // create main configuration
NULL, // init main configuration

NULL, // create server configuration
NULL, // merge server configuration

ngx_http_super_create_loc_conf, // create location configuration
ngx_http_super_merge_loc_conf // merge location configuration
};

// …

static void *ngx_http_super_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_super_loc_conf_t *conf = ngx_pcalloc(cf->pool,
sizeof(ngx_http_super_loc_conf_t));
if (NULL == conf) return NGX_CONF_ERROR;

return conf;
}

// …

static char *ngx_http_super(ngx_conf_t *cf, ngx_command_t *cmd, void
*conf)
{
ngx_http_super_loc_conf_t *nlcf = conf;

ngx_str_t *value = cf->args->elts;

// …

return NGX_CONF_OK;
}

I think the statement still stands :slight_smile:


From: “Igor S.” [email protected]
Sent: Thursday, August 21, 2008 9:22 PM
To: [email protected]
Subject: Re: module develop --nginx segmentation fault