Revisione di epoll in vista dell'esempio di FifoReporter.c
[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, t = 1;
58     char buffer[4096];
59     int fifofd, epfd;
60     struct epoll_event epev;
61     int nread;
62     /*
63      * Input section: decode parameters passed in the calling 
64      * Use getopt function
65      */
66     opterr = 0;                              /* don't want writing to stderr */
67     while ( (i = getopt(argc, argv, "ht:f:")) != -1) {
68         switch (i) {
69         /* 
70          * Handling options 
71          */ 
72         case 'h':  
73             printf("Wrong -h option use\n");
74             usage();
75             return(0);
76             break;
77         case 'f':
78             fifoname = optarg;
79             break;
80         case 't':
81             t = strtol(optarg, NULL, 10);
82             break;
83         case '?':                                    /* unrecognized options */
84             printf("Unrecognized options -%c\n",optopt);
85             usage();
86         default:                                       /* should not reached */
87             usage();
88         }
89     }
90     /* ***********************************************************
91      * 
92      *           Options processing completed
93      *
94      *                Main code beginning
95      * 
96      * ***********************************************************/
97     /* 
98      * Initial setup 
99      */
100     if (mkfifo(fifoname, 0622)) {  // create well known fifo if does't exist 
101         if (errno!=EEXIST) {
102             perror("Cannot create well known fifo");
103             exit(1);
104         }
105     }
106     if ((fifo = open(fifoname, O_RDONLY)) < 0) {    // open fifo
107         perror("Cannot open read only well known fifo");
108         exit(1);
109     }
110     epfd = epoll_create(5);       // initialize epoll
111     if (epfd < 0) {
112         perror("Failing on epoll_create");
113         exit(1);
114     }
115     /* Main body: loop over requests */
116     while (1) {
117         
118     }
119     debug("Exiting for unknown reasons\n");
120 }
121 /*
122  * routine to print usage info and exit
123  */
124 void usage(void) {
125     printf("Elementary fortune server\n");
126     printf("Usage:\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");
131     exit(1);
132 }
133 /*
134  * Signal Handler to manage termination
135  */
136 void HandSIGTERM(int signo) {
137     debug("Terminated by %s\n", strsignal(signo));
138     unlink(fifoname);
139     exit(0);
140 }