2 #include <sys/epoll.h> /* Linux epoll interface */
3 #include <sys/signalfd.h>/* Linux signalfd interface */
5 /* Subroutines declaration */
9 /* default name for the input fifo */
10 char *fifoname = "/tmp/reporter.fifo";
12 #define MAX_EPOLL_EV 10
14 int main(int argc, char *argv[])
16 /* Variables definition */
17 int i, n, nread, t = 10;
19 int fifofd, epfd, sigfd;
21 struct epoll_event epev, events[MAX_EPOLL_EV];
22 struct signalfd_siginfo siginf;
25 if ((epfd=epoll_create(5)) < 0) // epoll init
26 die("Failing on epoll_create");
27 /* Signal setup for signalfd and epoll use */
28 sigemptyset(&sigmask);
29 sigaddset(&sigmask, SIGINT);
30 sigaddset(&sigmask, SIGQUIT);
31 sigaddset(&sigmask, SIGTERM);
32 if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) // block signals
33 die("Failing in sigprocmask");
34 if ((sigfd=signalfd(-1, &sigmask, SFD_NONBLOCK)) == -1) // take a signalfd
35 die("Failing in signalfd");
36 epev.data.fd = sigfd; // add fd to epoll
37 epev.events = EPOLLIN;
38 if (epoll_ctl(epfd, EPOLL_CTL_ADD, sigfd, &epev))
39 die("Failing in signal epoll_ctl");
40 /* Fifo setup for epoll use */
41 if (mkfifo(fifoname, 0622)) { // create well known fifo if does't exist
43 die("Cannot create well known fifo");
45 if ((fifofd = open(fifoname, O_RDWR|O_NONBLOCK)) < 0) // open fifo
46 die("Cannot open read only well known fifo");
47 epev.data.fd = fifofd; // add fifofd to epoll
48 epev.events = EPOLLIN;
49 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fifofd, &epev))
50 die("Failing in fifo epoll_ctl");
51 /* Main body: wait something to report */