Prosecuzione lavoro su esempio signalfd
[gapil.git] / sources / FifoReporter.c
1 /* FifoReporter.c
2  * 
3  * Copyright (C) 2011 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /****************************************************************
20  *
21  * Program reporterd
22  * FIFO, signal, event and alarm reporter
23  * an example program for I/O multiplexing with signalfd and Co.
24  *
25  * Author: Simone Piccardi
26  * Jan. 2011
27  *
28  * Usage: reporterd -h give all info
29  *
30  ****************************************************************/
31 /* 
32  * Include needed headers
33  */
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 */
44
45 #include "macros.h"
46 #include "Gapil.h"
47
48 /* Subroutines declaration */
49 void usage(void);
50
51 /* default name for the input fifo */
52 char *fifoname = "/tmp/reporter.fifo";
53
54 int main(int argc, char *argv[])
55 {
56 /* Variables definition */
57     int i, nread, t = 1;
58     char buffer[4096];
59     int fifofd, epfd, sigfd;
60     struct epoll_event epev;
61     sigset_t sigmask;
62     char buffer[1024];
63     /*
64      * Input section: decode parameters passed in the calling 
65      * Use getopt function
66      */
67     opterr = 0;                              /* don't want writing to stderr */
68     while ( (i = getopt(argc, argv, "ht:f:")) != -1) {
69         switch (i) {
70         /* 
71          * Handling options 
72          */ 
73         case 'h':  
74             printf("Wrong -h option use\n");
75             usage();
76             return(0);
77             break;
78         case 'f':
79             fifoname = optarg;
80             break;
81         case 't':
82             t = strtol(optarg, NULL, 10);
83             break;
84         case '?':                                    /* unrecognized options */
85             printf("Unrecognized options -%c\n",optopt);
86             usage();
87         default:                                       /* should not reached */
88             usage();
89         }
90     }
91     /* ***********************************************************
92      * 
93      *           Options processing completed
94      *
95      *                Main code beginning
96      * 
97      * ***********************************************************/
98     /* 
99      * Initial setup 
100      */
101     if (mkfifo(fifoname, 0622)) {  // create well known fifo if does't exist 
102         if (errno!=EEXIST) {
103             perror("Cannot create well known fifo");
104             exit(1);
105         }
106     }
107     epfd = epoll_create(5);       // initialize epoll
108     if (epfd < 0) {
109         perror("Failing on epoll_create");
110         exit(1);
111     }
112     if ((fifofd = open(fifoname, O_RDONLY|O_NONBLOCK)) < 0) {  // open fifo
113         perror("Cannot open read only well known fifo");
114         exit(1);
115     }
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");
120         exit(-1);
121     }
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");
129         exit(1);
130     }
131     if ((sigfd=signalfd(-1, &sigmask, SFD_NONBLOCK)) == -1) {
132         perror("Failing in signalfd");
133         exit(-1);
134     }
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");
139         exit(-1);
140     }
141     
142     /* Main body: loop over requests */
143     while (1) {
144         if ((i = epoll_wait(epfd, &epev, 1, -1)) < 0) {
145             perror("error on epoll_wait");
146             exit(-1);
147         }
148         switch (epev.fd) {
149         /* 
150          * Handling options 
151          */ 
152         case sigfd:
153             // signals
154             break;
155         case fifofd:
156             // fifo
157             break;
158         default:                                       /* should not reached */
159             printf("something wrong, epoll activity on unknown %i file descriptor\n", epev.data.fd);
160             exit(-1);
161         }
162         
163     }
164     debug("Exiting for unknown reasons\n");
165 }
166 /*
167  * routine to print usage info and exit
168  */
169 void usage(void) {
170     printf("Elementary fortune server\n");
171     printf("Usage:\n");
172     printf("  fortuned [-h] [-f] -n XXX \n");
173     printf("  -h   print this help\n");
174     printf("  -t filename   set fifo file\n");
175     exit(1);
176 }
177 /*
178  * Signal Handler to manage termination
179  */
180 void HandSIGTERM(int signo) {
181     debug("Terminated by %s\n", strsignal(signo));
182     unlink(fifoname);
183     exit(0);
184 }