Commit 2ab714554f1fd2273c9705450cf192dabb4f6b9d

Stefan Sperling 2022-05-19T07:18:46

Skip poll(2) if an imsgbuf has a non-empty read buffer.

diff --git a/lib/privsep.c b/lib/privsep.c
index 782f94a..135b8b6 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -94,9 +94,15 @@ read_imsg(struct imsgbuf *ibuf)
 	const struct got_error *err;
 	size_t n;
 
-	err = poll_fd(ibuf->fd, POLLIN, INFTIM);
-	if (err)
-		return err;
+	/*
+	 * There is no imsg API function to tell us whether the
+	 * read buffer still contains pending data :-(
+	 */
+	if (ibuf->r.wpos < IMSG_HEADER_SIZE) {
+		err = poll_fd(ibuf->fd, POLLIN, INFTIM);
+		if (err)
+			return err;
+	}
 
 	n = imsg_read(ibuf);
 	if (n == -1) {