From: Simone Piccardi Date: Sat, 15 Jan 2011 15:41:56 +0000 (+0000) Subject: Prosecuzione lavoro su esempio signalfd X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=commitdiff_plain;h=7d224ad655c0a530877f39411d43a91e3d7318e1;p=gapil.git Prosecuzione lavoro su esempio signalfd --- diff --git a/sources/FifoReporter.c b/sources/FifoReporter.c index 631c56e..5062e84 100644 --- a/sources/FifoReporter.c +++ b/sources/FifoReporter.c @@ -54,11 +54,12 @@ char *fifoname = "/tmp/reporter.fifo"; 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 @@ -103,17 +104,61 @@ int main(int argc, char *argv[]) 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"); @@ -127,7 +172,6 @@ void usage(void) { 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); } /*