Hi,

to not depend on where you declare :

 <IfModule mod_kcache.c>
      KcacheEnabled On
 </IfModule>

you need to iterate over the server_rec list in post_config and
child_init hooks.

Please see attached modifications (where only a server_config is used).

Regards,
Yann.


On Thu, Dec 4, 2014 at 5:46 PM, nik600 <nik...@gmail.com> wrote:
> because i've tried with both or with only one.
>
> IE, if i use:
>
> module AP_MODULE_DECLARE_DATA kcache_module = {
> STANDARD20_MODULE_STUFF,NULL , NULL, create_server_conf, merge_conf,
> kcache_directives, register_hooks };
>
> i have the same behaviour
>
>
> 2014-12-04 17:32 GMT+01:00 Eric Johanson <er...@valmarc.com>:
>
>> Okay but why are you managing the kcache_config structure with both the
>> per-directory AND the server-level functions?  You should decide if your
>> kcache_config is server-wide or if it is per-directory, and use only the
>> appropriate functions.
>> -Eric
>>
>>
>> From: nik600 [mailto:nik...@gmail.com]
>> Sent: Thursday, December 04, 2014 11:15 AM
>> To: modules-dev@httpd.apache.org
>> Subject: Re: problem with shared memory and directives for httpd
>>
>> Hi
>>
>> thanks for your reply.
>>
>> i've tried also with a merging function but i have the same behaviour.
>>
>> Attached the same example with also merge function.
>>
>> Maybe i'm doing something wrong in my merge function?
>>
>>
>> 2014-12-04 16:33 GMT+01:00 Eric Johanson <er...@valmarc.com<mailto:
>> er...@valmarc.com>>:
>> You have no merge function defined for your server-level config structure
>> (kcache_config).  I would definitely try implementing that function.
>> Otherwise, when apache goes to combine two levels of the config hierarchy
>> together, the resulting combination is likely to just be an empty version
>> of kcache_config, which sounds like it could be causing your problem.
>>
>> -Eric
>>
>>
>> From: nik600 [mailto:nik...@gmail.com<mailto:nik...@gmail.com>]
>> Sent: Thursday, December 04, 2014 10:05 AM
>> To: modules-dev@httpd.apache.org<mailto:modules-dev@httpd.apache.org>
>> Subject: problem with shared memory and directives for httpd
>>
>> Dear all
>>
>> i'm experiencing a problem with shared memory and i'm not able to figure
>> it out.
>>
>> i've got a segment of shared memory in my module config and seen that if
>> set some settings for the module in my configuration this memory isn't
>> available in the request process.
>>
>> i've also attached an example (very simplified and without any mutex, just
>> to show the case).
>>
>> i've noticed that if i have in my server configuration:
>>
>>  <IfModule mod_kcache.c>
>>       KcacheEnabled On
>> </IfModule>
>>
>> The memory segment is not availabe:
>>
>> >>[Thu Dec 04 15:26:15 2014] [crit] [client 127.0.0.1]
>> kcache_return_result invalid  config->s
>>
>> But if i comment this directive:
>>
>>  <IfModule mod_kcache.c>
>> #      KcacheEnabled On
>> </IfModule>
>>
>> The memory segment is available and gets updated:
>>
>> >>[Thu Dec 04 15:24:47 2014] [debug] src/mod_kcache.c(96): [client
>> 127.0.0.1] config->s->counter=68
>> >>[Thu Dec 04 15:24:48 2014] [debug] src/mod_kcache.c(96): [client
>> 127.0.0.1] config->s->counter=69
>> >>[Thu Dec 04 15:24:48 2014] [debug] src/mod_kcache.c(96): [client
>> 127.0.0.1] config->s->counter=70
>> ...
>>
>> i've attached the full example (100 lines of code)
>>
>> I'll appreciate any help, thank all in advance
>>
>> --
>> /*************/
>> nik600
>> http://www.kumbe.it
>>
>>
>>
>> --
>> /*************/
>> nik600
>> http://www.kumbe.it
>>
>
>
>
> --
> /*************/
> nik600
> http://www.kumbe.it
--- mod_kcache.c        2014-12-04 22:39:18.356881323 +0100
+++ mod_kcache.new.c    2014-12-04 22:49:16.804640988 +0100
@@ -10,7 +10,7 @@ typedef struct {
 } kcache_config_stat;
 
 typedef struct {
-       int enabled;
+       int enabled, enabled_set;
        apr_shm_t *counters_shm; /* the APR shared segment object*/
        kcache_config_stat *s;/*my stats*/
 } kcache_config;
@@ -27,8 +27,15 @@ static const command_rec kcache_directiv
                kcache_set_enabled, NULL, RSRC_CONF,
                "KcacheEnabled must have Off or On value"), { NULL } };
 
-module AP_MODULE_DECLARE_DATA kcache_module = { STANDARD20_MODULE_STUFF, 
create_dir_conf,
-               merge_conf, create_server_conf, merge_conf, kcache_directives, 
register_hooks };
+module AP_MODULE_DECLARE_DATA kcache_module = {
+    STANDARD20_MODULE_STUFF,
+    NULL, /* no create_dir_conf */
+    NULL, /* no merge_dir_conf */
+    create_server_conf,
+    merge_conf,
+    kcache_directives,
+    register_hooks
+};
 
 void* merge_conf(apr_pool_t* pool, void* BASE, void* ADD) {
        kcache_config* base = (kcache_config *) BASE ; /* This is what was set 
in the parent context */
@@ -36,26 +43,18 @@ void* merge_conf(apr_pool_t* pool, void*
        kcache_config* conf = (kcache_config *) create_server_conf(pool, NULL); 
/* This will be the merged configuration */
 
     /* Merge configurations */
-    conf->enabled = ( add->enabled == 0 ) ? base->enabled : add->enabled ;
-    conf->s=base->s;
-    conf->counters_shm=base->counters_shm;
+    conf->enabled = ( add->enabled_set ) ? add->enabled : base->enabled;
 
     return conf ;
 }
-void *create_dir_conf(apr_pool_t *pool, char *context) {
-       kcache_config *cfg = apr_pcalloc(pool, sizeof(kcache_config));
 
-       return cfg;
-}
 /*
  * creating the server cfg
  */
 void *create_server_conf(apr_pool_t *pool, server_rec *s) {
        kcache_config *cfg = apr_pcalloc(pool, sizeof(kcache_config));
 
-       if(s!=NULL){
-               ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, 
s,"kcache:create_server_conf creo la configurazione");
-       }
+       ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,"kcache:create_server_conf 
creo la configurazione");
 
        return cfg;
 }
@@ -66,13 +65,12 @@ const char *kcache_set_enabled(cmd_parms
                        cmd->server->module_config, &kcache_module);
        /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
-       if (conf) {
-               if (!strcasecmp(arg, "on"))
-                       conf->enabled = 1;
-               else
-                       conf->enabled = 0;
+    conf->enabled_set = 1;
+    if (!strcasecmp(arg, "on"))
+        conf->enabled = 1;
+    else
+        conf->enabled = 0;
 
-       }
        return NULL;
 }
 
@@ -83,11 +81,12 @@ static void kcache_child_init(apr_pool_t
 
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,"kcache:kcache_child_init 
imposto l'indirizzo della shared memory");
 
-       kcache_config *scfg =
-                       ap_get_module_config(s->module_config, &kcache_module);
-
-       scfg->s = apr_shm_baseaddr_get(scfg->counters_shm);
+    for (; s; s = s->next) {
+        kcache_config *scfg =
+                ap_get_module_config(s->module_config, &kcache_module);
 
+        scfg->s = apr_shm_baseaddr_get(scfg->counters_shm);
+    }
 }
 /*
  * create the shm
@@ -96,14 +95,30 @@ static int kcache_post_config(apr_pool_t
                apr_pool_t *ptemp, server_rec *s) {
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,"kcache:kcache_post_config 
creo la shared memory");
 
-       kcache_config *scfg = ap_get_module_config(s->module_config, 
&kcache_module);
-
-       apr_shm_create(&scfg->counters_shm, sizeof(*scfg->s), NULL, pconf);
-
-       scfg->s = apr_shm_baseaddr_get(scfg->counters_shm);
-       scfg->s->counter = 0;
+    kcache_config *main_scfg = ap_get_module_config(s->module_config, 
&kcache_module);
+    for (; s; s = s->next) {
+        kcache_config *scfg =
+                ap_get_module_config(s->module_config, &kcache_module);
+
+        if (scfg->enabled) {
+            if (main_scfg->s == NULL) {
+                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                        "kcache:kcache_post_config creo la shared memory");
+                apr_shm_create(&main_scfg->counters_shm, sizeof(*main_scfg->s),
+                               NULL, pconf);
+                main_scfg->s = apr_shm_baseaddr_get(main_scfg->counters_shm);
+                main_scfg->s->counter = 0;
+                if (scfg == main_scfg) {
+                    continue;
+                }
+            }
+
+            scfg->counters_shm = main_scfg->counters_shm;
+            scfg->s = main_scfg->s;
+        }
+    }
 
-       return DECLINED;
+    return OK;
 }
 
 static int kcache_handler_translate_files(request_rec* r) {
#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>
#include <http_log.h>
#include <sys/stat.h>
#include <apr_shm.h>

typedef struct {
	apr_size_t counter; /* my counter */
} kcache_config_stat;

typedef struct {
	int enabled, enabled_set;
	apr_shm_t *counters_shm; /* the APR shared segment object*/
	kcache_config_stat *s;/*my stats*/
} kcache_config;

void* merge_conf(apr_pool_t* pool, void* BASE, void* ADD);
void *create_dir_conf(apr_pool_t *pool, char *context);
void *create_server_conf(apr_pool_t *p, server_rec *s);
static void register_hooks(apr_pool_t *pool);

const char *kcache_set_enabled(cmd_parms *cmd, void *cfg, const char *arg);

static const command_rec kcache_directives[] = {
		AP_INIT_TAKE1("KcacheEnabled",
		kcache_set_enabled, NULL, RSRC_CONF,
		"KcacheEnabled must have Off or On value"), { NULL } };

module AP_MODULE_DECLARE_DATA kcache_module = {
    STANDARD20_MODULE_STUFF,
    NULL, /* no create_dir_conf */
    NULL, /* no merge_dir_conf */
    create_server_conf,
    merge_conf,
    kcache_directives,
    register_hooks
};

void* merge_conf(apr_pool_t* pool, void* BASE, void* ADD) {
	kcache_config* base = (kcache_config *) BASE ; /* This is what was set in the parent context */
	kcache_config* add = (kcache_config *) ADD ;   /* This is what is set in the new context */
	kcache_config* conf = (kcache_config *) create_server_conf(pool, NULL); /* This will be the merged configuration */

    /* Merge configurations */
    conf->enabled = ( add->enabled_set ) ? add->enabled : base->enabled;

    return conf ;
}

/*
 * creating the server cfg
 */
void *create_server_conf(apr_pool_t *pool, server_rec *s) {
	kcache_config *cfg = apr_pcalloc(pool, sizeof(kcache_config));

	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,"kcache:create_server_conf creo la configurazione");

	return cfg;
}
;
const char *kcache_set_enabled(cmd_parms *cmd, void *cfg, const char *arg) {
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	kcache_config *conf = (kcache_config *) ap_get_module_config(
			cmd->server->module_config, &kcache_module);
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    conf->enabled_set = 1;
    if (!strcasecmp(arg, "on"))
        conf->enabled = 1;
    else
        conf->enabled = 0;

	return NULL;
}

/*
 * init child
 */
static void kcache_child_init(apr_pool_t *p, server_rec *s) {

	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,"kcache:kcache_child_init imposto l'indirizzo della shared memory");

    for (; s; s = s->next) {
        kcache_config *scfg =
                ap_get_module_config(s->module_config, &kcache_module);

        scfg->s = apr_shm_baseaddr_get(scfg->counters_shm);
    }
}
/*
 * create the shm
 */
static int kcache_post_config(apr_pool_t *pconf, apr_pool_t *plog,
		apr_pool_t *ptemp, server_rec *s) {
	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,"kcache:kcache_post_config creo la shared memory");

    kcache_config *main_scfg = ap_get_module_config(s->module_config, &kcache_module);
    for (; s; s = s->next) {
        kcache_config *scfg =
                ap_get_module_config(s->module_config, &kcache_module);

        if (scfg->enabled) {
            if (main_scfg->s == NULL) {
                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                        "kcache:kcache_post_config creo la shared memory");
                apr_shm_create(&main_scfg->counters_shm, sizeof(*main_scfg->s),
                               NULL, pconf);
                main_scfg->s = apr_shm_baseaddr_get(main_scfg->counters_shm);
                main_scfg->s->counter = 0;
                if (scfg == main_scfg) {
                    continue;
                }
            }

            scfg->counters_shm = main_scfg->counters_shm;
            scfg->s = main_scfg->s;
        }
    }

    return OK;
}

static int kcache_handler_translate_files(request_rec* r) {

	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	kcache_config *config = (kcache_config *) ap_get_module_config(
			r->server->module_config, &kcache_module);
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	if (!config->s) {
		ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
				"kcache_return_result invalid  config->s");
		return DECLINED;
	}

	config->s->counter++;
	ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
				"config->s->counter=%"APR_SIZE_T_FMT,config->s->counter);

	return DECLINED;

}

static void register_hooks(apr_pool_t* pool) {

	static const char *succ[] = { "mod_proxy.c", "mod_alias.c",
			"mod_userdir.c", NULL };
	ap_hook_child_init(kcache_child_init, NULL, NULL, APR_HOOK_REALLY_FIRST);
	ap_hook_post_config(kcache_post_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
	ap_hook_translate_name(kcache_handler_translate_files, NULL, succ,
			APR_HOOK_REALLY_FIRST);
}

Reply via email to