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 /****************************************************************
22 * Fortune server - Using Message Queues
24 * Author: Simone Piccardi
27 * Usage: fortuned -h give all info
29 ****************************************************************/
31 * Include needed headers
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 */
47 /* Maximum message size */
50 /* Subroutines declaration */
52 void HandSIGTERM(int signo);
53 int FortuneParse(char *file, char **fortune, int n);
55 int msgid; /* Message queue identifier */
56 int main(int argc, char *argv[])
58 /* Variables definition */
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 */
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 */
70 key_t key; /* Message queue key */
71 int size; /* message size */
73 * Input section: decode parameters passed in the calling
76 opterr = 0; /* don't want writing to stderr */
77 while ( (i = getopt(argc, argv, "hn:f:")) != -1) {
82 case 'h': /* print usage infos */
86 case 'f': /* set fortune file name */
87 fortunefilename = optarg;
89 case 'n': /* set number of fortune string to use */
90 n = strtol(optarg, NULL, 10);
91 fortune = (char **) calloc(sizeof(*fortune), n);
93 case '?': /* unrecognized options */
94 printf("Unrecognized options -%c\n", optopt);
96 default: /* should not reached */
100 /* ***********************************************************
102 * Options processing completed
104 * Main code beginning
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]);
114 * Comunication section
116 key = ftok("./MQFortuneServer.c", 1);
117 msgid = msgget(key, IPC_CREAT|0666);
119 perror("Cannot create message queue");
123 /* Main body: loop over requests */
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);
134 debug("Exiting for unknown reasons\n");
137 * routine to print usage info and exit
140 printf("Elementary fortune server\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");
149 * Signal Handler to manage termination
151 void HandSIGTERM(int signo) {
152 debug("Terminated by %s\n", strsignal(signo));
153 msgctl(msgid, IPC_RMID, NULL); /* remove message queue */