e1319782e8da9f65141842c836c0b3655b865cbf
[gapil.git] / sources / MQFortuneServer.c
1 /* MQFortuneServer.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 - Using Messag queues
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
27  * Usage: fortuned -h give all info
28  *
29  * $Id: MQFortuneServer.c,v 1.1 2002/08/26 21:11:49 piccardi Exp $
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
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>       /*  */
44
45 #include "macros.h"
46 #include "wrappers.h"
47
48 /* Subroutines declaration */
49 void usage(void);
50 void HandSIGTERM(int signo);
51 int FortuneParse(char *file, char **fortune, int n);
52
53 /* name of well known fifo */
54 int main(int argc, char *argv[])
55 {
56 /* Variables definition */
57     int i, n = 0;
58     char **fortune;
59     struct msgbuf_read {
60         long mtype;      /* message type, must be > 0 */
61         int pid;         /* message data */
62     } msg_read;
63     struct msgbuf_write {
64         long mtype;      /* message type, must be > 0 */
65         char mtext[MSGMAX];  /* message data */
66     } msg_write;
67     int msgid;
68     key_t key;
69     int size;
70     /*
71      * Input section: decode parameters passed in the calling 
72      * Use getopt function
73      */
74     opterr = 0;                              /* don't want writing to stderr */
75     while ( (i = getopt(argc, argv, "hn:f:")) != -1) {
76         switch (i) {
77         /* 
78          * Handling options 
79          */ 
80         case 'h':  
81             printf("Wrong -h option use\n");
82             usage();
83             return(0);
84             break;
85         case 'f':
86             fortunefilename = optarg;
87             break;
88         case 'n':
89             n = strtol(optarg, NULL, 10);
90             fortune = (char **) calloc(sizeof(*fortune), n);
91             break;
92         case '?':                                    /* unrecognized options */
93             printf("Unrecognized options -%c\n",optopt);
94             usage();
95         default:                                       /* should not reached */
96             usage();
97         }
98     }
99     /* ***********************************************************
100      * 
101      *           Options processing completed
102      *
103      *                Main code beginning
104      * 
105      * ***********************************************************/
106     if (n==0) usage();          /* if no pool depth exit printing usage info */
107     Signal(SIGTERM, HandSIGTERM);            /* set handlers for termination */
108     Signal(SIGINT, HandSIGTERM);
109     Signal(SIGQUIT, HandSIGTERM);
110     i = FortuneParse(fortunefilename, fortune, n);          /* parse phrases */
111     for (n=0; n<i; n++) debug("%s%%\n", fortune[n]);
112     /* 
113      * Comunication section 
114      */
115     key = ftok("./MQFortuneServer.c", 1); 
116     msgid = msgget(key, IPC_CREAT|666); 
117     
118     /* Main body: loop over requests */
119     while (1) {
120         msgrcv(msgid, &msg_read, 1, sizeof(int), MSG_NOERROR);
121         n = random() % i;                             /* select random value */
122         strncpy(msgbuf_write.mtext, fortune[n], MSGMAX);
123         size = min(strlen(fortune[n])+1, MSGMAX);  
124         msgsnd(msgid,  &msg_write, size, 0);
125     }
126     debug("Exiting for unknown reasons\n");
127 }
128 /*
129  * routine to print usage info and exit
130  */
131 void usage(void) {
132     printf("Elementary fortune server\n");
133     printf("Usage:\n");
134     printf("  fortuned [-h] [-f] -n XXX \n");
135     printf("  -h   print this help\n");
136     printf("  -f filename   set file for fortunes\n");
137     printf("  -n XXX        set pool depth\n");
138     exit(1);
139 }
140 /*
141  * Signal Handler to manage termination
142  */
143 void HandSIGTERM(int signo) {
144     debug("Terminated by %s\n", strsignal(signo));
145     unlink(fifoname);
146     exit(0);
147 }