logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Building & Member Downloads View previous topic :: View next topic
Reply to topic   Topic: problem with custom module
Author
scott



Joined: 16 Jun 2009
Posts: 6

PostPosted: Tue 16 Jun '09 9:28    Post subject: problem with custom module Reply with quote

Hello,

I have been working on a module for apache (for windows)
when i found that it was calling the register_hooks function
twice.

I copied a tutorial from the net and compiled it to see if I
got the same problem - and I did.

Here is the tutorial code:
Code:

/* a very simple module: put a header in the reply with the number of hits this process has received */
#include <httpd.h>
#include <http_config.h>
#include <apr_strings.h>

void __declspec(dllimport) __stdcall ap_hook_fixups(int (*)(request_rec*), int, int, int);

static apr_uint32_t hitCount = 0;
static int myHitsCounter(request_rec *request) {
//if main exists, this is an internal redirect, which means we've already acted on this "hit"
if (!request->main) {
char *hitString;
hitCount++;
hitString = apr_itoa(request->pool, hitCount);
apr_table_set(request->headers_out, "X-Process-Hits", hitString);
}
return OK;
}
static void myRegisterHooks(apr_pool_t *p) {
ap_hook_fixups(myHitsCounter, NULL, NULL, APR_HOOK_MIDDLE);
   
}
module AP_MODULE_DECLARE_DATA myhits_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structures */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* command handlers */
myRegisterHooks /* register hooks */
};


I find when debugging using gdb and running httpd -X
the registerHooks function is called twice. This is
a problem for my other module. Does anyone know why?

Note that the tutorial does compile and work as expected
and that calling myRegisterHooks twice does not affect
this module.

Any help much appreciated.
--scott
Back to top
scott



Joined: 16 Jun 2009
Posts: 6

PostPosted: Tue 16 Jun '09 9:52    Post subject: Reply with quote

On second thoughts i believe that the module is being loaded twice.
Is there any reason why apache would do that?
there is only one entry in the httpd.conf file.
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Tue 16 Jun '09 13:44    Post subject: Reply with quote

This is expected. Apache makes two passes of the configuration at startup.

The first pass is just to check for errors. Then, all modules are unloaded and the whole configuration is processed again for *real*.

You need to make sure that anything you do in your startup functions (like RegisterHooks) can safely be done twice.

-tom-
Back to top
scott



Joined: 16 Jun 2009
Posts: 6

PostPosted: Tue 16 Jun '09 15:27    Post subject: Reply with quote

thanks for the reply - in my actual module i was trying to create a thread in the init function, which can not be done twice.

Is there a recommended work around?

I think ill figure something out anyway now that i know the problem!

Thanks again!
--scott
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7294
Location: Germany, Next to Hamburg

PostPosted: Tue 16 Jun '09 20:18    Post subject: Reply with quote

Well I'm not used to C coding, but in PHP I would use a singleton function to make sure that in instance runs only once.
But what is the syntax is incorrect? Does your thread unload with apache? Since apache unload all modules after sysntax check I think it would be good to make sure that your module unloads the thread it creates when it is unload by apache.
Back to top
scott



Joined: 16 Jun 2009
Posts: 6

PostPosted: Wed 17 Jun '09 5:22    Post subject: Reply with quote

A singleton is no good, since unloading the module destroys all the variables created. It does not destroy the thread tho.
--scott
Back to top


Reply to topic   Topic: problem with custom module View previous topic :: View next topic
Post new topic   Forum Index -> Building & Member Downloads