Aggiornamento note copyright
[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 Message Queues
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 <sys/ipc.h>     /* SysV IPC functions */
42 #include <sys/msg.h>     /* SysV message queues */
43
44 #include "macros.h"
45 #include "Gapil.h"
46
47 /* Maximum message size */
48 #define MSGMAX 8192
49
50 /* Subroutines declaration */
51 void usage(void);
52 void HandSIGTERM(int signo);
53 int FortuneParse(char *file, char **fortune, int n);
54
55 int msgid;                                       /* Message queue identifier */
56 int main(int argc, char *argv[])
57 {
58 /* Variables definition */
59     int i, n = 0;
60     char **fortune;                       /* array of fortune message string */
61     char *fortunefilename = "/usr/share/games/fortunes/linux";  /* file name */
62     struct msgbuf_read {      /* message struct to read request from clients */
63         long mtype;                               /* message type, must be 1 */
64         long pid;             /* message data, must be the pid of the client */
65     } msg_read;
66     struct msgbuf_write {       /* message struct to write result to clients */
67         long mtype;            /* message type, will be the pid of the client*/
68         char mtext[MSGMAX];             /* message data, will be the fortune */
69     } msg_write;
70     key_t key;                                          /* Message queue key */
71     int size;                                                /* message size */
72     /*
73      * Input section: decode parameters passed in the calling 
74      * Use getopt function
75      */
76     opterr = 0;                              /* don't want writing to stderr */
77     while ( (i = getopt(argc, argv, "hn:f:")) != -1) {
78         switch (i) {
79         /* 
80          * Handling options 
81          */ 
82         case 'h':                                       /* print usage infos */
83             usage();
84             return(0);
85             break;
86         case 'f':                                   /* set fortune file name */
87             fortunefilename = optarg;
88             break;
89         case 'n':                     /* set number of fortune string to use */
90             n = strtol(optarg, NULL, 10);
91             fortune = (char **) calloc(sizeof(*fortune), n);
92             break;
93         case '?':                                    /* unrecognized options */
94             printf("Unrecognized options -%c\n", optopt);
95             usage();
96         default:                                       /* should not reached */
97             usage();
98         }
99     }
100     /* ***********************************************************
101      * 
102      *           Options processing completed
103      *
104      *                Main code beginning
105      * 
106      * ***********************************************************/
107     if (n==0) usage();          /* if no pool depth exit printing usage info */
108     Signal(SIGTERM, HandSIGTERM);            /* set handlers for termination */
109     Signal(SIGINT, HandSIGTERM);
110     Signal(SIGQUIT, HandSIGTERM);
111     i = FortuneParse(fortunefilename, fortune, n);          /* parse phrases */
112     for (n=0; n<i; n++) debug("%s%%\n", fortune[n]);
113     /* 
114      * Comunication section 
115      */
116     key = ftok("./MQFortuneServer.c", 1); 
117     msgid = msgget(key, IPC_CREAT|0666);
118     if (msgid < 0) {
119         perror("Cannot create message queue");
120         exit(1);
121     }
122
123     /* Main body: loop over requests */
124     daemon(0, 0);
125     while (1) {
126         msgrcv(msgid, &msg_read, sizeof(int), 1, MSG_NOERROR);
127         debug("received request from %d\n", msg_read.pid);
128         n = random() % i;                             /* select random value */
129         strncpy(msg_write.mtext, fortune[n], MSGMAX);
130         size = min(strlen(fortune[n])+1, MSGMAX);  
131         msg_write.mtype=msg_read.pid;             /* use request pid as type */
132         msgsnd(msgid, &msg_write, size, 0);
133     }
134     debug("Exiting for unknown reasons\n");
135 }
136 /*
137  * routine to print usage info and exit
138  */
139 void usage(void) {
140     printf("Elementary fortune server\n");
141     printf("Usage:\n");
142     printf("  fortuned [-h] [-f] -n XXX \n");
143     printf("  -h   print this help\n");
144     printf("  -f filename   set file for fortunes\n");
145     printf("  -n NNN        set pool depth\n");
146     exit(1);
147 }
148 /*
149  * Signal Handler to manage termination
150  */
151 void HandSIGTERM(int signo) {
152     debug("Terminated by %s\n", strsignal(signo));
153     msgctl(msgid, IPC_RMID, NULL);                   /* remove message queue */
154     exit(0);
155 }