summary refs log tree commit diff
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
parentGenerate tags file (diff)
downloadcatsit-f556af9ad5c90db94ecf01af2b50a22f6c721791.tar.gz
catsit-f556af9ad5c90db94ecf01af2b50a22f6c721791.zip
Implement non-blocking line-buffered reading
-rw-r--r--daemon.c18
-rw-r--r--daemon.h43
2 files changed, 53 insertions, 8 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);
diff --git a/daemon.h b/daemon.h
index db82ee2..7290420 100644
--- a/daemon.h
+++ b/daemon.h
@@ -14,6 +14,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <errno.h>
 #include <grp.h>
 #include <pwd.h>
 #include <stdint.h>
@@ -49,6 +50,42 @@ static inline int prependAdd(const char *command) {
 	return 0;
 }
 
+struct Line {
+	size_t len;
+	char buf[512];
+};
+
+static inline char *lineFlush(struct Line *line) {
+	if (!line->len) return NULL;
+	line->buf[line->len++] = '\0';
+	return line->buf;
+}
+
+static inline char *lineRead(struct Line *line, int fd) {
+	char *nul = memchr(line->buf, '\0', line->len);
+	if (nul) {
+		nul++;
+		line->len -= nul - line->buf;
+		memmove(line->buf, nul, line->len);
+	}
+
+	size_t cap = sizeof(line->buf) - line->len - 1;
+	if (!cap) return lineFlush(line);
+
+	ssize_t len = read(fd, &line->buf[line->len], cap);
+	if (len < 0 && errno != EAGAIN) return NULL;
+	if (len > 0) line->len += len;
+
+	char *nl = memchr(line->buf, '\n', line->len);
+	if (nl) {
+		*nl = '\0';
+		return line->buf;
+	} else {
+		errno = EAGAIN;
+		return NULL;
+	}
+}
+
 enum {
 	SHELL,
 	PATH,
@@ -70,12 +107,6 @@ enum State {
 	Restart,
 };
 
-enum { LineCap = 512 };
-struct Line {
-	size_t len;
-	char buf[LineCap];
-};
-
 struct Service {
 	char *name;
 	char *command;
w/temp.causal.agency/up.c?id=0731998692c67e5a0983107591d572ca7d8c5e4d&follow=1'>Fix all copyright noticesJune McEnroe 2022-08-04Add Conversations With FriendsJune McEnroe The better of the two, but largely the same. I feel like these books are a bit too autobiographical, but I don't know if I'm allowed to accuse an author of that. My real problem is that I read these books as largely uncritical of their characters. They behave in nonsense ways, are mostly uncritical of their own behaviour, and don't really have arcs of growth or change. I suppose this book had a bit of one, but only in the last two chapters. 2022-07-30Add Normal PeopleJune McEnroe Unbearably straight. Eyerolls and sighs per page off the charts. Shout out to Joanna, I guess. I kinda like the lack of quotation marks though to be honest. After half of the Ruth Ozeki novel and now this, I need to get back to some genre fiction. 2022-07-26Rewrite glitch from new pngoJune McEnroe 2022-07-26Update Care with time-to-ID and piercingsJune McEnroe 2022-07-26Add -w to upJune McEnroe 2022-07-13Set push.autoSetupRemoteJune McEnroe 2022-07-08Remove TOURJune McEnroe There is not that much distinct stuff here anymore. 2022-07-03Add The Bone Shard EmperorJune McEnroe Suffers a little bit from middle book but I really enjoyed it. Read it faster than the first one too, despite its length. 2022-06-25Bump xterm font size to 12June McEnroe 2022-06-10Handle subshells (and functions) inside substitutionsJune McEnroe 2022-06-10Switch to jorts Install scriptJune McEnroe 2022-06-08Indicate if still reading or no resultsJune McEnroe 2022-06-08Add Maiden, Mother, CroneJune McEnroe Mixed bag like most collections of short stories. Some of them are pretty good. The author of the worst written story also has the worst written bio. 2022-06-05FIRST SHOW IN 2.5 YEARS BABEY!!!June McEnroe 2022-06-03Set line number on File linesJune McEnroe 2022-06-03Stop polling stdin after EOFJune McEnroe 2022-06-02Set TABSIZE=4June McEnroe Absolutely indiscriminately. 2022-06-02Do basic match highlightingJune McEnroe 2022-06-02Clean up parsing a littleJune McEnroe 2022-06-02Don't duplicate path stringJune McEnroe 2022-06-02Use stderr instead of /dev/tty, realloc buffer if lines too longJune McEnroe For some reason I haven't been able to figure out, trying to poll /dev/tty returns POLLNVAL (and this was using 100% CPU looping), but using stderr instead works fine. 2022-06-02Add initial working version of qfJune McEnroe 2022-05-29Set prompt for okshJune McEnroe