Re: Graceful Exit?

Stanley Gambarin (
Wed, 05 Mar 1997 10:59:42 -0500

Message-Id: <>
To: Jim Boutcher <>
Subject: Re: Graceful Exit? 
In-Reply-To: Your message of "Tue, 04 Mar 1997 03:51:50 CST."
Date: Wed, 05 Mar 1997 10:59:42 -0500
From: Stanley Gambarin <>

> 2) My problem arises when I wish to kill the web server or for some rea=
son the FastCGI process dies(has never happened to me yet, but I've heard=
 it happens).. If I have buffered 9 log entries, they are all lost. It se=
ems to me that the code outside of the FCGI_Accept while loop is never ex=
ecuted. =

> =

> 3) Am I overlooking something? How do I make sure code is ran on proces=
s termination? It would make my life so much easier (and all my CGIs EXTR=
EMELY quicker) if I could "buffer" all file i/o in my FastCGI processes. =
Has anyone done this? =

	One possible way to implement your idea is to use signals to control
the flow of the execution in the fastcgi application.  The example progra=
provided below show you how to accomplish this.  A signal handler is =

so that it flushes the output and then closes the file descriptor.  In th=
is =

way, any information stored in memory will be written out prior to progra=
termination.  However, there are fallacies with this approach as to what =

signals can you recieve and from whom do you recieve these signals.  For =

example, using C program below caught all SIGTERM's sent to the fastcgi
process from the shell, but did not catch anything when the server termin=
(even though server sends them SIGTERM as well, I think.. need to take a =
look at the code).  In any case, the main idea is:
	Importance of the data dictates your implementation strategy.  =

If you are implementing a counter, then you don't care if you are off by =
10, =

however, if you are writing to a database, you want to make sure each =

transaction is written out.
						Hope that was of some help.

P.S. Code below is provided under same terms as stated in the LICENSE =

which basically states that you can freely use it but I am not responsibl=
e. I =

tested the below with Apache1.2b4, mod_fastcgi1.4.3 on Solaris2.5.
Some modifications maybe necessary:

static FILE *fp;
void sigfunc(int signo)
  fprintf(fp, "Signal %d has been recieved..exiting\n", signo);
  fclose(fp);    /* this does the fflush() */
void main ()
    int count =3D 0;
    /* establish signal handler */
    if (signal(SIGTERM, sigfunc) =3D=3D SIG_ERR)
      { =

	perror("Failed signal");
    fp =3D fopen("/tmp/blahblah", "a+");
    while(FCGI_Accept() >=3D 0) {
        char *contentLength =3D getenv("CONTENT_LENGTH");
        int len;
	printf("Content-type: text/html\r\n"
	       "<title>FastCGI echo</title>"
	       "<h1>FastCGI echo</h1>\n"
               "Request number %d <p>\n", ++count);
	/* log transaction */
	fprintf(fp, "Received request #%d\n", (count)); =

	/* if this is very important transaction, then also */
	/* fflush(fp); */