about summary refs log tree commit diff
path: root/daemon.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-08-15 13:05:31 -0400
committerJune McEnroe <june@causal.agency>2020-08-15 13:05:31 -0400
commitf556af9ad5c90db94ecf01af2b50a22f6c721791 (patch)
treea609c64b296fbce484df0306abd12579a67706fc /daemon.c
parentGenerate tags file (diff)
downloadcatsit-f556af9ad5c90db94ecf01af2b50a22f6c721791.tar.gz
catsit-f556af9ad5c90db94ecf01af2b50a22f6c721791.zip
Implement non-blocking line-buffered reading
Diffstat (limited to '')
-rw-r--r--daemon.c18
1 files changed, 16 insertions, 2 deletions
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);