tags:

views:

131

answers:

1

Our project platform is Linux. In our project we require a webserver and few webpages.

We have to do webpage coding by .cgi files, i have to add dynamically some details to the webpage whenever i gets that information from other process. We have used message queue.

The other process will update the message queue and signals this process.this process has to handle signal and update that information in the webpage.

So i have to continuously wait for the signal. For that i used while loop, so if i execute normally, the required output is coming..but when i tried to load the .cgi file through web browser, it is freezing up.

here is my code...

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include<signal.h>

struct my_msg_st {
long int my_msg_type;
char some_text[BUFSIZ];
};


sig_atomic_t flag = 0;

void sigint_handler(int sig)
{
    printf("signal handled\n");
    flag = 1;
}

int main()
{

    int msgid;
    struct my_msg_st some_data;
    long int msg_to_receive = 0;
    struct sigaction sa;
    sigset_t waitset;
    int sig;
    int result;

    sigemptyset( &waitset );
    sigaddset( &waitset, SIGUSR1 );


    memset (&sa, 0, sizeof (sa));
    sa.sa_flags = 0;
    sa.sa_handler = &sigint_handler;
    sigaction (SIGUSR1, &sa, NULL);

    printf
         ("Content-type: text/html\nPRAGMA:NO-CACHE\nCACHE-CONTROL:NO-CACHE\n\n");
     printf("<HTML>\n");

     printf("<head><META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\"> <META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\"><title>COOPER BUSSMANN MONITORING DATA</title>\n");
     printf("<style type=\"text/css\">\n"
     "body {background-color: #00ffff}\n"

     "</style>\n"


     "</head>\n");


     printf("<body>\n");

     printf("<table ID=\"_Table\" ALIGN=\"CENTER\" BORDER=\"0\" COLS=\"6\" WIDTH=\"650\">\n");
     printf("<TR><TD COLSPAN=\"6\">&nbsp;</TD></TR>\n");
     printf("<TR><TD COLSPAN=\"6\" ALIGN=\"CENTER\"><h1>MONITORING DATA</h1></TD></TR>\n");
     printf("<TR><TD COLSPAN=\"6\" ALIGN=\"CENTER\"><HR></TD></TR>\n");
     printf("<TR><TD COLSPAN=\"6\">&nbsp;</TD></TR>\n");
     printf("</table>\n");

     printf("<table ID=\"_Table\" ALIGN=\"CENTER\" BORDER=\"1\" COLS=\"6\" WIDTH=\"650\">\n");
     printf("<TR>\n");
     printf("<TH>ESN</TH><TH>IsStatic</TH><TH>Alarm Type</TH><TH>TimeStamp</TH><TH>PartNumber</TH><TH>Location</TH><TH>PPE</TH></TR>\n");


  while(1)
 {

    printf("entered waiting for signal\n");
    sleep(10);

      if(flag == 1)
  {

       printf("From siguser just got a sigint signal\n");
       msgid = msgget((key_t)1234, 0666 | IPC_CREAT);

        if(msgid==-1)
        printf("error in opening msg queue\n");

        if (msgrcv(msgid, (void *)&some_data, BUFSIZ,msg_to_receive, 0) == -1)
       {

        fprintf(stderr, ".msgrcv failed with error: %d\n.", errno);
        exit(EXIT_FAILURE);

       }

      printf(".You wrote: %s.", some_data.some_text);

      printf("<TR><TD align = \"center\">%s\n</TD></TR>",some_data.some_text);

      if (msgctl(msgid, IPC_RMID, 0) == -1)
       {

        fprintf(stderr, ".msgctl(IPC_RMID) failed\n.");
         exit(EXIT_FAILURE);

       }

       flag = 0;
   }

}
}
+1  A: 

I think you will find this is not the right approach to update a web page. If the CGI never finished running, the browser will just wait until it times out.

Although i believe you can flush the response and some browsers may start rendering the page, for the page to display properly the CGI needs to finish executing successfully.

With the endless loop, the browser never has a complete page to render and doesn't know the expected content length, and if for some reason a crash occurs during the loop it will result in an internal server error.

CGI's or server side scripts for outputting web pages aren't really designed to run indefinitely.

I would investigate using AJAX in your page to query the server for updates on a regular basis.

I believe there are some server "push" technologies (i think one might have been called Comet from memory) but usually people use AJAX to accomplish this type of thing.

Jarod Elliott