int main(int argc, char *argv[])
{
/* Variables definition */
- int i, t = 1;
+ int i, nread, t = 1;
char buffer[4096];
- int fifofd, epfd;
+ int fifofd, epfd, sigfd;
struct epoll_event epev;
- int nread;
+ sigset_t sigmask;
+ char buffer[1024];
/*
* Input section: decode parameters passed in the calling
* Use getopt function
exit(1);
}
}
- if ((fifo = open(fifoname, O_RDONLY)) < 0) { // open fifo
- perror("Cannot open read only well known fifo");
- exit(1);
- }
epfd = epoll_create(5); // initialize epoll
if (epfd < 0) {
perror("Failing on epoll_create");
exit(1);
}
+ if ((fifofd = open(fifoname, O_RDONLY|O_NONBLOCK)) < 0) { // open fifo
+ perror("Cannot open read only well known fifo");
+ exit(1);
+ }
+ epev.data.fd = fifofd; /* add fifofd to epoll */
+ epev.events = EPOLLIN;
+ if (epoll_ctl(epfd, EPOLL_CTL_ADD, fifofd, &epev)) {
+ perror("Failing in epoll_ctl");
+ exit(-1);
+ }
+ sigemptyset(&sigmask);
+ sigaddset(&sigmask, SIGINT);
+ sigaddset(&sigmask, SIGQUIT);
+ sigaddset(&sigmask, SIGTERM);
+ // blocking signal treated by signalfd to avoid default action
+ if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {
+ perror("Failing in sigprocmask");
+ exit(1);
+ }
+ if ((sigfd=signalfd(-1, &sigmask, SFD_NONBLOCK)) == -1) {
+ perror("Failing in signalfd");
+ exit(-1);
+ }
+ epev.data.fd = sigfd; /* add sigfd to epoll */
+ epev.events = EPOLLIN;
+ if (epoll_ctl(epfd, EPOLL_CTL_ADD, sigfd, &epev)) {
+ perror("Failing in epoll_ctl");
+ exit(-1);
+ }
+
/* Main body: loop over requests */
while (1) {
+ if ((i = epoll_wait(epfd, &epev, 1, -1)) < 0) {
+ perror("error on epoll_wait");
+ exit(-1);
+ }
+ switch (epev.fd) {
+ /*
+ * Handling options
+ */
+ case sigfd:
+ // signals
+ break;
+ case fifofd:
+ // fifo
+ break;
+ default: /* should not reached */
+ printf("something wrong, epoll activity on unknown %i file descriptor\n", epev.data.fd);
+ exit(-1);
+ }
}
debug("Exiting for unknown reasons\n");
printf(" fortuned [-h] [-f] -n XXX \n");
printf(" -h print this help\n");
printf(" -t filename set fifo file\n");
- printf(" -n XXX set pool depth\n");
exit(1);
}
/*