Messi gli esempi nel testo e usata daemon dove serve nei server
[gapil.git] / sources / FortuneServer.c
index 5f10062960b8dfddfa5daee88c06add0f5fa7d7d..8ba3a2fc65b158cd48a58d3130e37b9086cb7f02 100644 (file)
@@ -26,7 +26,7 @@
  *
  * Usage: fortuned -h give all info
  *
- * $Id: FortuneServer.c,v 1.3 2002/08/18 23:24:44 piccardi Exp $
+ * $Id: FortuneServer.c,v 1.6 2003/01/12 00:24:28 piccardi Exp $
  *
  ****************************************************************/
 /* 
 #include <stdlib.h>     /* standard library */
 #include <string.h>     /* ANSI C standard string */
 #include <errno.h>      /* errorstring */
+#include <signal.h>     /* signals */
 #include <fcntl.h>      /*  */
 
 #include "macros.h"
+#include "Gapil.h"
 
 /* Subroutines declaration */
 void usage(void);
+void HandSIGTERM(int signo);
 int FortuneParse(char *file, char **fortune, int n);
 
 /* name of well known fifo */
@@ -53,7 +56,7 @@ int main(int argc, char *argv[])
 {
 /* Variables definition */
     int i, n = 0;
-    char *fortunefilename = "/usr/share/games/fortunes/kids";
+    char *fortunefilename = "/usr/share/games/fortunes/linux";
     char **fortune;
     char line[80];
     int fifo_server, fifo_client;
@@ -94,39 +97,54 @@ int main(int argc, char *argv[])
      *               Main code beginning
      * 
      * ***********************************************************/
-    Signal(SIGTERM, HandSIGTERM);
+    if (n==0) usage();          /* if no pool depth exit printing usage info */
+    Signal(SIGTERM, HandSIGTERM);            /* set handlers for termination */
     Signal(SIGINT, HandSIGTERM);
     Signal(SIGQUIT, HandSIGTERM);
-    if (n==0) usage();          /* if no pool depth exit printing usage info */
     i = FortuneParse(fortunefilename, fortune, n);          /* parse phrases */
-    for (n=0; n<i; n++) debug("%s\n\n", fortune[n]);
+    for (n=0; n<i; n++) debug("%s%%\n", fortune[n]);
     /* 
      * Comunication section 
      */
     if (mkfifo(fifoname, 0622)) {  /* create well known fifo if does't exist */
        if (errno!=EEXIST) {
            perror("Cannot create well known fifo");
-           exit(-1);
+           exit(1);
        }
     }
+    daemon(0, 0);
+    /* open fifo two times to avoid EOF */
+    fifo_server = open(fifoname, O_RDONLY);
+    if (fifo_server < 0) {
+       perror("Cannot open read only well known fifo");
+       exit(1);
+    }
+    if (open(fifoname, O_WRONLY) < 0) {
+       perror("Cannot open write only well known fifo");
+       exit(1);
+    }
     /* Main body: loop over requests */
     while (1) {
-       fifo_server = open(fifoname, O_RDONLY);      /* open well known fifo */
-       if (fifo_server < 0) {
-           perror("Cannot open well known fifo");
-           exit(-1);
-       }
        nread = read(fifo_server, line, 79);                 /* read request */
-       line[nread] = 0;
+       if (nread < 0) {
+           perror("Read Error");
+           exit(1);
+       }
+       line[nread] = 0;                       /* terminate fifo name string */
        debug("%s %d\n", line,nread);
        n = random() % i;                             /* select random value */
        debug("fortune[%d]=%s\n", n, fortune[n]);
        fifo_client = open(line, O_WRONLY);              /* open client fifo */
+       if (fifo_client < 0) {
+           debug("Client fifo is %s\n", line);
+           perror("Cannot open");
+           exit(1);
+       }
        nread = write(fifo_client,                           /* write phrase */
                      fortune[n], strlen(fortune[n])+1);
-       close(fifo_client);                         /* close well known fifo */
-       close(fifo_server);                             /* close client fifo */
+       close(fifo_client);                             /* close client fifo */
     }
+    debug("Exiting for unknown reasons\n");
 }
 /*
  * routine to print usage info and exit
@@ -144,6 +162,7 @@ void usage(void) {
  * Signal Handler to manage termination
  */
 void HandSIGTERM(int signo) {
+    debug("Terminated by %s\n", strsignal(signo));
     unlink(fifoname);
     exit(0);
 }