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 */
59 int fifofd, epfd, sigfd;
60 struct epoll_event epev;
64 * Input section: decode parameters passed in the calling
67 opterr = 0; /* don't want writing to stderr */
68 while ( (i = getopt(argc, argv, "ht:f:")) != -1) {
74 printf("Wrong -h option use\n");
82 t = strtol(optarg, NULL, 10);
84 case '?': /* unrecognized options */
85 printf("Unrecognized options -%c\n",optopt);
87 default: /* should not reached */
91 /* ***********************************************************
93 * Options processing completed
97 * ***********************************************************/
101 if (mkfifo(fifoname, 0622)) { // create well known fifo if does't exist
103 perror("Cannot create well known fifo");
107 epfd = epoll_create(5); // initialize epoll
109 perror("Failing on epoll_create");
112 if ((fifofd = open(fifoname, O_RDONLY|O_NONBLOCK)) < 0) { // open fifo
113 perror("Cannot open read only well known fifo");
116 epev.data.fd = fifofd; /* add fifofd to epoll */
117 epev.events = EPOLLIN;
118 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fifofd, &epev)) {
119 perror("Failing in epoll_ctl");
122 sigemptyset(&sigmask);
123 sigaddset(&sigmask, SIGINT);
124 sigaddset(&sigmask, SIGQUIT);
125 sigaddset(&sigmask, SIGTERM);
126 // blocking signal treated by signalfd to avoid default action
127 if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {
128 perror("Failing in sigprocmask");
131 if ((sigfd=signalfd(-1, &sigmask, SFD_NONBLOCK)) == -1) {
132 perror("Failing in signalfd");
135 epev.data.fd = sigfd; /* add sigfd to epoll */
136 epev.events = EPOLLIN;
137 if (epoll_ctl(epfd, EPOLL_CTL_ADD, sigfd, &epev)) {
138 perror("Failing in epoll_ctl");
142 /* Main body: loop over requests */
144 if ((i = epoll_wait(epfd, &epev, 1, -1)) < 0) {
145 perror("error on epoll_wait");
158 default: /* should not reached */
159 printf("something wrong, epoll activity on unknown %i file descriptor\n", epev.data.fd);
164 debug("Exiting for unknown reasons\n");
167 * routine to print usage info and exit
170 printf("Elementary fortune server\n");
172 printf(" fortuned [-h] [-f] -n XXX \n");
173 printf(" -h print this help\n");
174 printf(" -t filename set fifo file\n");
178 * Signal Handler to manage termination
180 void HandSIGTERM(int signo) {
181 debug("Terminated by %s\n", strsignal(signo));