Aggiornamento note copyright
[gapil.git] / sources / FortuneServer.c
1 /* FortuneServer.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program fortuned 
22  * Fortune server
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
27  * Usage: fortuned -h give all info
28  *
29  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
33 #include <sys/types.h>   /* primitive system data types */
34 #include <sys/stat.h>    /* file characteristics constants and functions */
35 #include <unistd.h>      /* unix standard library */
36 #include <stdio.h>       /* standard I/O library */
37 #include <stdlib.h>      /* C standard library */
38 #include <string.h>      /* C strings library */
39 #include <errno.h>       /* error definitions and routines */
40 #include <signal.h>      /* signal constants, types and functions */
41 #include <fcntl.h>       /* file control functions */
42
43 #include "macros.h"
44 #include "Gapil.h"
45
46 /* Subroutines declaration */
47 void usage(void);
48 void HandSIGTERM(int signo);
49 int FortuneParse(char *file, char **fortune, int n);
50
51 /* name of well known fifo */
52 char *fifoname = "/tmp/fortune.fifo";
53 int main(int argc, char *argv[])
54 {
55 /* Variables definition */
56     int i, n = 0;
57     char *fortunefilename = "/usr/share/games/fortunes/linux";
58     char **fortune;
59     char line[80];
60     int fifo_server, fifo_client;
61     int nread;
62     /*
63      * Input section: decode parameters passed in the calling 
64      * Use getopt function
65      */
66     opterr = 0;                              /* don't want writing to stderr */
67     while ( (i = getopt(argc, argv, "hn:f:")) != -1) {
68         switch (i) {
69         /* 
70          * Handling options 
71          */ 
72         case 'h':  
73             printf("Wrong -h option use\n");
74             usage();
75             return(0);
76             break;
77         case 'f':
78             fortunefilename = optarg;
79             break;
80         case 'n':
81             n = strtol(optarg, NULL, 10);
82             fortune = (char **) calloc(sizeof(*fortune), n);
83             break;
84         case '?':                                    /* unrecognized options */
85             printf("Unrecognized options -%c\n",optopt);
86             usage();
87         default:                                       /* should not reached */
88             usage();
89         }
90     }
91     /* ***********************************************************
92      * 
93      *           Options processing completed
94      *
95      *                Main code beginning
96      * 
97      * ***********************************************************/
98     if (n==0) usage();          /* if no pool depth exit printing usage info */
99     Signal(SIGTERM, HandSIGTERM);            /* set handlers for termination */
100     Signal(SIGINT, HandSIGTERM);
101     Signal(SIGQUIT, HandSIGTERM);
102     i = FortuneParse(fortunefilename, fortune, n);          /* parse phrases */
103     for (n=0; n<i; n++) debug("%s%%\n", fortune[n]);
104     /* 
105      * Comunication section 
106      */
107     if (mkfifo(fifoname, 0622)) {  /* create well known fifo if does't exist */
108         if (errno!=EEXIST) {
109             perror("Cannot create well known fifo");
110             exit(1);
111         }
112     }
113     daemon(0, 0);
114     /* open fifo two times to avoid EOF */
115     fifo_server = open(fifoname, O_RDONLY);
116     if (fifo_server < 0) {
117         perror("Cannot open read only well known fifo");
118         exit(1);
119     }
120     if (open(fifoname, O_WRONLY) < 0) {
121         perror("Cannot open write only well known fifo");
122         exit(1);
123     }
124     /* Main body: loop over requests */
125     while (1) {
126         nread = read(fifo_server, line, 79);                 /* read request */
127         if (nread < 0) {
128             perror("Read Error");
129             exit(1);
130         }
131         line[nread] = 0;                       /* terminate fifo name string */
132         debug("%s %d\n", line,nread);
133         n = random() % i;                             /* select random value */
134         debug("fortune[%d]=%s\n", n, fortune[n]);
135         fifo_client = open(line, O_WRONLY);              /* open client fifo */
136         if (fifo_client < 0) {
137             debug("Client fifo is %s\n", line);
138             perror("Cannot open");
139             exit(1);
140         }
141         nread = write(fifo_client,                           /* write phrase */
142                       fortune[n], strlen(fortune[n])+1);
143         close(fifo_client);                             /* close client fifo */
144     }
145     debug("Exiting for unknown reasons\n");
146 }
147 /*
148  * routine to print usage info and exit
149  */
150 void usage(void) {
151     printf("Elementary fortune server\n");
152     printf("Usage:\n");
153     printf("  fortuned [-h] [-f] -n XXX \n");
154     printf("  -h   print this help\n");
155     printf("  -f filename   set file for fortunes\n");
156     printf("  -n XXX        set pool depth\n");
157     exit(1);
158 }
159 /*
160  * Signal Handler to manage termination
161  */
162 void HandSIGTERM(int signo) {
163     debug("Terminated by %s\n", strsignal(signo));
164     unlink(fifoname);
165     exit(0);
166 }