3 * Copyright (C) 2002 Simone Piccardi
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.
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.
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.
19 /****************************************************************
24 * Author: Simone Piccardi
27 * Usage: fortuned -h give all info
29 * $Id: FortuneServer.c,v 1.4 2002/08/19 17:34:23 piccardi Exp $
31 ****************************************************************/
33 * Include needed headers
35 #include <sys/types.h> /* predefined types */
36 #include <sys/stat.h> /* */
37 #include <unistd.h> /* include unix standard library */
38 #include <stdio.h> /* include standard I/O library */
39 #include <stdlib.h> /* standard library */
40 #include <string.h> /* ANSI C standard string */
41 #include <errno.h> /* errorstring */
42 #include <signal.h> /* signals */
43 #include <fcntl.h> /* */
48 /* Subroutines declaration */
50 void HandSIGTERM(int signo);
51 int FortuneParse(char *file, char **fortune, int n);
53 /* name of well known fifo */
54 char *fifoname = "/tmp/fortune.fifo";
55 int main(int argc, char *argv[])
57 /* Variables definition */
59 char *fortunefilename = "/usr/share/games/fortunes/italia";
62 int fifo_server, fifo_client;
65 * Input section: decode parameters passed in the calling
68 opterr = 0; /* don't want writing to stderr */
69 while ( (i = getopt(argc, argv, "hn:f:")) != -1) {
75 printf("Wrong -h option use\n");
80 fortunefilename = optarg;
83 n = strtol(optarg, NULL, 10);
84 fortune = (char **) calloc(sizeof(*fortune), n);
86 case '?': /* unrecognized options */
87 printf("Unrecognized options -%c\n",optopt);
89 default: /* should not reached */
93 /* ***********************************************************
95 * Options processing completed
99 * ***********************************************************/
100 if (n==0) usage(); /* if no pool depth exit printing usage info */
101 Signal(SIGTERM, HandSIGTERM); /* set handlers for termination */
102 Signal(SIGINT, HandSIGTERM);
103 Signal(SIGQUIT, HandSIGTERM);
104 i = FortuneParse(fortunefilename, fortune, n); /* parse phrases */
105 for (n=0; n<i; n++) debug("%s%%\n", fortune[n]);
107 * Comunication section
109 if (mkfifo(fifoname, 0622)) { /* create well known fifo if does't exist */
111 perror("Cannot create well known fifo");
115 /* open fifo two times to avoid EOF */
116 fifo_server = open(fifoname, O_RDONLY);
117 if (fifo_server < 0) {
118 perror("Cannot open read only well known fifo");
121 if (open(fifoname, O_WRONLY) < 0) {
122 perror("Cannot open write only well known fifo");
125 /* Main body: loop over requests */
127 nread = read(fifo_server, line, 79); /* read request */
129 perror("Read Error");
132 line[nread] = 0; /* terminate fifo name string */
133 debug("%s %d\n", line,nread);
134 n = random() % i; /* select random value */
135 debug("fortune[%d]=%s\n", n, fortune[n]);
136 fifo_client = open(line, O_WRONLY); /* open client fifo */
137 if (fifo_client < 0) {
138 debug("Client fifo is %s\n", line);
139 perror("Cannot open");
142 nread = write(fifo_client, /* write phrase */
143 fortune[n], strlen(fortune[n])+1);
144 close(fifo_client); /* close client fifo */
146 debug("Exiting for unknown reasons\n");
149 * routine to print usage info and exit
152 printf("Elementary fortune server\n");
154 printf(" fortuned [-h] [-f] -n XXX \n");
155 printf(" -h print this help\n");
156 printf(" -f filename set file for fortunes\n");
157 printf(" -n XXX set pool depth\n");
161 * Signal Handler to manage termination
163 void HandSIGTERM(int signo) {
164 debug("Terminated by %s\n", strsignal(signo));