views:

131

answers:

2

Using apache on a windows server with mod_fastcgi, the C code looks like that:

void main() {
    init();
    while (FCGI_Accept() >= 0)
        work();
    cleanup();
}

When the service is taken down (i.e.: net stop apache2), the process terminates without getting to the cleanup code.

What am I missing here?

A: 

The only way to exit prematurely would be to call "exit()" somewhere inside work() (or FCGI_Accept()...)

Edit:

If you think it might be FCGI_Accept(), try using onexit() to set up a callback to be called from "exit()". At the very least this will confirm that "exit()" was called prematurely.

Jimmy J
1. I don't want to exit without the cleanup (which is the case right now), and 2. FCGI_Accept is part of the FastCGI framework and should return with <0 when commanded by apache to go down (at least as I understand it). Instead, it aborts...
Paul Oyster
You could try using "onexit()" - this sets a function to be executed when a program terminates.
Jimmy J
+1  A: 

It seems, from reading the FCGI_Accept manpage and this FAQ entry that FCGI_Accept does not, in fact, return -1 in the case of Apache shutting down. Try setting a signal handler for SIGUSR1 and SIGTERM. There's an example (not Windows-specific, but it's worth a try) posted a while ago on a mailing list, here.

Julian Squires