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 * $Id: MQFortuneServer.c,v 1.6 2003/05/02 09:55:14 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 */
49 /* Maximum message size */
52 /* Subroutines declaration */
54 void HandSIGTERM(int signo);
55 int FortuneParse(char *file, char **fortune, int n);
57 int msgid; /* Message queue identifier */
58 int main(int argc, char *argv[])
60 /* Variables definition */
62 char **fortune; /* array of fortune message string */
63 char *fortunefilename = "/usr/share/games/fortunes/linux"; /* file name */
64 struct msgbuf_read { /* message struct to read request from clients */
65 long mtype; /* message type, must be 1 */
66 long pid; /* message data, must be the pid of the client */
68 struct msgbuf_write { /* message struct to write result to clients */
69 long mtype; /* message type, will be the pid of the client*/
70 char mtext[MSGMAX]; /* message data, will be the fortune */
72 key_t key; /* Message queue key */
73 int size; /* message size */
75 * Input section: decode parameters passed in the calling
78 opterr = 0; /* don't want writing to stderr */
79 while ( (i = getopt(argc, argv, "hn:f:")) != -1) {
84 case 'h': /* print usage infos */
88 case 'f': /* set fortune file name */
89 fortunefilename = optarg;
91 case 'n': /* set number of fortune string to use */
92 n = strtol(optarg, NULL, 10);
93 fortune = (char **) calloc(sizeof(*fortune), n);
95 case '?': /* unrecognized options */
96 printf("Unrecognized options -%c\n", optopt);
98 default: /* should not reached */
102 /* ***********************************************************
104 * Options processing completed
106 * Main code beginning
108 * ***********************************************************/
109 if (n==0) usage(); /* if no pool depth exit printing usage info */
110 Signal(SIGTERM, HandSIGTERM); /* set handlers for termination */
111 Signal(SIGINT, HandSIGTERM);
112 Signal(SIGQUIT, HandSIGTERM);
113 i = FortuneParse(fortunefilename, fortune, n); /* parse phrases */
114 for (n=0; n<i; n++) debug("%s%%\n", fortune[n]);
116 * Comunication section
118 key = ftok("./MQFortuneServer.c", 1);
119 msgid = msgget(key, IPC_CREAT|0666);
121 perror("Cannot create message queue");
125 /* Main body: loop over requests */
128 msgrcv(msgid, &msg_read, sizeof(int), 1, MSG_NOERROR);
129 debug("received request from %d\n", msg_read.pid);
130 n = random() % i; /* select random value */
131 strncpy(msg_write.mtext, fortune[n], MSGMAX);
132 size = min(strlen(fortune[n])+1, MSGMAX);
133 msg_write.mtype=msg_read.pid; /* use request pid as type */
134 msgsnd(msgid, &msg_write, size, 0);
136 debug("Exiting for unknown reasons\n");
139 * routine to print usage info and exit
142 printf("Elementary fortune server\n");
144 printf(" fortuned [-h] [-f] -n XXX \n");
145 printf(" -h print this help\n");
146 printf(" -f filename set file for fortunes\n");
147 printf(" -n NNN set pool depth\n");
151 * Signal Handler to manage termination
153 void HandSIGTERM(int signo) {
154 debug("Terminated by %s\n", strsignal(signo));
155 msgctl(msgid, IPC_RMID, NULL); /* remove message queue */