From 7b9d9d1e5385e58430f71114eea9994f649d17e5 Mon Sep 17 00:00:00 2001 From: David Delassus Date: Sun, 30 Oct 2011 18:18:03 +0100 Subject: [PATCH] Fix FIFO (close it and reopen it after reading) --- src/fifo.c | 11 +++++++---- src/fifo.h | 2 +- src/wmfs.c | 19 ++++++++++++++----- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/fifo.c b/src/fifo.c index a7723d8..88707e0 100644 --- a/src/fifo.c +++ b/src/fifo.c @@ -17,7 +17,7 @@ fifo_init(void) if(mkfifo(W->fifo.path, 0644) < 0) warnx("Can't create FIFO: %s\n", strerror(errno)); - if(!(W->fifo.fd = open(W->fifo.path, O_NONBLOCK, 0))) + if(!(W->fifo.fd = open(W->fifo.path, O_RDONLY | O_NDELAY, 0))) warnx("Can't open FIFO: %s\n", strerror(errno)); } @@ -42,11 +42,14 @@ fifo_parse(char *cmd) XSync(W->dpy, false); } -void +int fifo_read(void) { - static char buf[256] = { 0 }; + char buf[256] = { 0 }; + int ret = 0; - if(read(W->fifo.fd, buf, sizeof(buf) - 1) > 0) + if((ret = read(W->fifo.fd, buf, sizeof(buf) - 1)) > 0) fifo_parse(buf); + + return ret; } diff --git a/src/fifo.h b/src/fifo.h index 81a6a57..caa03f8 100644 --- a/src/fifo.h +++ b/src/fifo.h @@ -14,6 +14,6 @@ #include void fifo_init(void); -void fifo_read(void); +int fifo_read(void); #endif /* __FIFO_H */ diff --git a/src/wmfs.c b/src/wmfs.c index 7179a04..496275c 100644 --- a/src/wmfs.c +++ b/src/wmfs.c @@ -243,19 +243,20 @@ wmfs_loop(void) { XEvent ev; int fd = ConnectionNumber(W->dpy); - int maxfd = fd + 1; fd_set iset; - if(W->fifo.fd > 0) - maxfd += W->fifo.fd; - while(W->running) { + int maxfd = fd + 1; + FD_ZERO(&iset); FD_SET(fd, &iset); if(W->fifo.fd > 0) + { + maxfd += W->fifo.fd; FD_SET(W->fifo.fd, &iset); + } if(select(maxfd, &iset, NULL, NULL, NULL) > 0) { @@ -268,7 +269,15 @@ wmfs_loop(void) } } else if(W->fifo.fd > 0 && FD_ISSET(W->fifo.fd, &iset)) - fifo_read(); + { + if (fifo_read() == 0) + { + close (W->fifo.fd); + + if (!(W->fifo.fd = open (W->fifo.path, O_RDONLY | O_NDELAY, 0))) + warnx ("Can't reopen FIFO: %s\n", strerror (errno)); + } + } } } }