3 * Copyright (C) 2011 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 * FIFO, signal, event and alarm reporter
23 * an example program for I/O multiplexing with signalfd and Co.
25 * Author: Simone Piccardi
28 * Usage: reporterd -h give all info
30 ****************************************************************/
32 * Include needed headers
34 #include <sys/types.h> /* primitive system data types */
35 #include <sys/stat.h> /* file characteristics constants and functions */
36 #include <unistd.h> /* unix standard library */
37 #include <stdio.h> /* standard I/O library */
38 #include <stdlib.h> /* C standard library */
39 #include <string.h> /* C strings library */
40 #include <errno.h> /* error definitions and routines */
41 #include <signal.h> /* signal constants, types and functions */
42 #include <fcntl.h> /* file control functions */
43 #include <sys/epoll.h> /* Linux epoll interface */
48 /* Subroutines declaration */
51 /* default name for the input fifo */
52 char *fifoname = "/tmp/reporter.fifo";
54 int main(int argc, char *argv[])
56 /* Variables definition */
60 struct epoll_event epev;
63 * Input section: decode parameters passed in the calling
66 opterr = 0; /* don't want writing to stderr */
67 while ( (i = getopt(argc, argv, "ht:f:")) != -1) {
73 printf("Wrong -h option use\n");
81 t = strtol(optarg, NULL, 10);
83 case '?': /* unrecognized options */
84 printf("Unrecognized options -%c\n",optopt);
86 default: /* should not reached */
90 /* ***********************************************************
92 * Options processing completed
96 * ***********************************************************/
100 if (mkfifo(fifoname, 0622)) { // create well known fifo if does't exist
102 perror("Cannot create well known fifo");
106 if ((fifo = open(fifoname, O_RDONLY)) < 0) { // open fifo
107 perror("Cannot open read only well known fifo");
110 epfd = epoll_create(5); // initialize epoll
112 perror("Failing on epoll_create");
115 /* Main body: loop over requests */
119 debug("Exiting for unknown reasons\n");
122 * routine to print usage info and exit
125 printf("Elementary fortune server\n");
127 printf(" fortuned [-h] [-f] -n XXX \n");
128 printf(" -h print this help\n");
129 printf(" -t filename set fifo file\n");
130 printf(" -n XXX set pool depth\n");
134 * Signal Handler to manage termination
136 void HandSIGTERM(int signo) {
137 debug("Terminated by %s\n", strsignal(signo));