From f556af9ad5c90db94ecf01af2b50a22f6c721791 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sat, 15 Aug 2020 13:05:31 -0400 Subject: Implement non-blocking line-buffered reading --- daemon.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'daemon.c') diff --git a/daemon.c b/daemon.c index 9eb8bb4..42ea78f 100644 --- a/daemon.c +++ b/daemon.c @@ -209,8 +209,11 @@ int main(int argc, char *argv[]) { warn("%s", fifoPath); } - int fifo = open(fifoPath, O_RDONLY | O_NONBLOCK | O_CLOEXEC); + // XXX: Make sure there is always at least one writer open, otherwise we + // get EOF continually. + int fifo = open(fifoPath, O_RDWR | O_NONBLOCK | O_CLOEXEC); if (fifo < 0) err(EX_CANTCREAT, "%s", fifoPath); + struct Line fifoLine = {0}; openlog(getprogname(), LOG_NDELAY | LOG_PID | LOG_PERROR, LOG_DAEMON); @@ -277,8 +280,19 @@ int main(int argc, char *argv[]) { (timespecisset(&deadline) ? &timeout : NULL), &mask ); + if (nfds < 0 && errno != EINTR) { + syslog(LOG_ERR, "ppoll: %m"); + continue; + } + + if (nfds > 0 && fds[0].revents) { + for (char *line; NULL != (line = lineRead(&fifoLine, fifo));) { + syslog(LOG_INFO, "control: %s", line); + } + if (errno != EAGAIN) syslog(LOG_ERR, "read: %m"); + } - // TODO: Handle FIFO and pipes. + // TODO: Handle pipes. if (timespecisset(&deadline)) { clock_gettime(CLOCK_MONOTONIC, &now); -- cgit 1.4.1