+/* MQFortuneServer.c
+ *
+ * Copyright (C) 2002 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Program fortuned
+ * Fortune server - Using Messag queues
+ *
+ * Author: Simone Piccardi
+ * Aug. 2002
+ *
+ * Usage: fortuned -h give all info
+ *
+ * $Id: MQFortuneServer.c,v 1.1 2002/08/26 21:11:49 piccardi Exp $
+ *
+ ****************************************************************/
+/*
+ * Include needed headers
+ */
+#include <sys/types.h> /* predefined types */
+#include <sys/stat.h> /* */
+#include <unistd.h> /* include unix standard library */
+#include <stdio.h> /* include standard I/O library */
+#include <stdlib.h> /* standard library */
+#include <string.h> /* ANSI C standard string */
+#include <errno.h> /* errorstring */
+#include <signal.h> /* signals */
+#include <fcntl.h> /* */
+
+#include "macros.h"
+#include "wrappers.h"
+
+/* Subroutines declaration */
+void usage(void);
+void HandSIGTERM(int signo);
+int FortuneParse(char *file, char **fortune, int n);
+
+/* name of well known fifo */
+int main(int argc, char *argv[])
+{
+/* Variables definition */
+ int i, n = 0;
+ char **fortune;
+ struct msgbuf_read {
+ long mtype; /* message type, must be > 0 */
+ int pid; /* message data */
+ } msg_read;
+ struct msgbuf_write {
+ long mtype; /* message type, must be > 0 */
+ char mtext[MSGMAX]; /* message data */
+ } msg_write;
+ int msgid;
+ key_t key;
+ int size;
+ /*
+ * Input section: decode parameters passed in the calling
+ * Use getopt function
+ */
+ opterr = 0; /* don't want writing to stderr */
+ while ( (i = getopt(argc, argv, "hn:f:")) != -1) {
+ switch (i) {
+ /*
+ * Handling options
+ */
+ case 'h':
+ printf("Wrong -h option use\n");
+ usage();
+ return(0);
+ break;
+ case 'f':
+ fortunefilename = optarg;
+ break;
+ case 'n':
+ n = strtol(optarg, NULL, 10);
+ fortune = (char **) calloc(sizeof(*fortune), n);
+ break;
+ case '?': /* unrecognized options */
+ printf("Unrecognized options -%c\n",optopt);
+ usage();
+ default: /* should not reached */
+ usage();
+ }
+ }
+ /* ***********************************************************
+ *
+ * Options processing completed
+ *
+ * Main code beginning
+ *
+ * ***********************************************************/
+ if (n==0) usage(); /* if no pool depth exit printing usage info */
+ Signal(SIGTERM, HandSIGTERM); /* set handlers for termination */
+ Signal(SIGINT, HandSIGTERM);
+ Signal(SIGQUIT, HandSIGTERM);
+ i = FortuneParse(fortunefilename, fortune, n); /* parse phrases */
+ for (n=0; n<i; n++) debug("%s%%\n", fortune[n]);
+ /*
+ * Comunication section
+ */
+ key = ftok("./MQFortuneServer.c", 1);
+ msgid = msgget(key, IPC_CREAT|666);
+
+ /* Main body: loop over requests */
+ while (1) {
+ msgrcv(msgid, &msg_read, 1, sizeof(int), MSG_NOERROR);
+ n = random() % i; /* select random value */
+ strncpy(msgbuf_write.mtext, fortune[n], MSGMAX);
+ size = min(strlen(fortune[n])+1, MSGMAX);
+ msgsnd(msgid, &msg_write, size, 0);
+ }
+ debug("Exiting for unknown reasons\n");
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+ printf("Elementary fortune server\n");
+ printf("Usage:\n");
+ printf(" fortuned [-h] [-f] -n XXX \n");
+ printf(" -h print this help\n");
+ printf(" -f filename set file for fortunes\n");
+ printf(" -n XXX set pool depth\n");
+ exit(1);
+}
+/*
+ * Signal Handler to manage termination
+ */
+void HandSIGTERM(int signo) {
+ debug("Terminated by %s\n", strsignal(signo));
+ unlink(fifoname);
+ exit(0);
+}