- djm@cvs.openbsd.org 2010/01/12 00:58:25
     [monitor_fdpass.c]
     avoid spinning when fd passing on nonblocking sockets by calling poll()
     in the EINTR/EAGAIN path, much like we do in atomicio; ok dtucker@
diff --git a/monitor_fdpass.c b/monitor_fdpass.c
index 4b9a066..56d3afd 100644
--- a/monitor_fdpass.c
+++ b/monitor_fdpass.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: monitor_fdpass.c,v 1.18 2008/11/30 11:59:26 dtucker Exp $ */
+/* $OpenBSD: monitor_fdpass.c,v 1.19 2010/01/12 00:58:25 djm Exp $ */
 /*
  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
  * All rights reserved.
@@ -34,6 +34,7 @@
 #endif
 
 #include <errno.h>
+#include <poll.h>
 #include <string.h>
 #include <stdarg.h>
 
@@ -55,6 +56,7 @@
 	struct iovec vec;
 	char ch = '\0';
 	ssize_t n;
+	struct pollfd pfd;
 
 	memset(&msg, 0, sizeof(msg));
 #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
@@ -75,9 +77,13 @@
 	msg.msg_iov = &vec;
 	msg.msg_iovlen = 1;
 
-	while ((n = sendmsg(sock, &msg, 0)) == -1 && (errno == EAGAIN ||
-	    errno == EINTR))
+	pfd.fd = sock;
+	pfd.events = POLLOUT;
+	while ((n = sendmsg(sock, &msg, 0)) == -1 &&
+	    (errno == EAGAIN || errno == EINTR)) {
 		debug3("%s: sendmsg(%d): %s", __func__, fd, strerror(errno));
+		(void)poll(&pfd, 1, -1);
+	}
 	if (n == -1) {
 		error("%s: sendmsg(%d): %s", __func__, fd,
 		    strerror(errno));
@@ -112,6 +118,7 @@
 	ssize_t n;
 	char ch;
 	int fd;
+	struct pollfd pfd;
 
 	memset(&msg, 0, sizeof(msg));
 	vec.iov_base = &ch;
@@ -126,9 +133,13 @@
 	msg.msg_controllen = sizeof(cmsgbuf.buf);
 #endif
 
-	while ((n = recvmsg(sock, &msg, 0)) == -1 && (errno == EAGAIN ||
-	    errno == EINTR))
+	pfd.fd = sock;
+	pfd.events = POLLIN;
+	while ((n = recvmsg(sock, &msg, 0)) == -1 &&
+	    (errno == EAGAIN || errno == EINTR)) {
 		debug3("%s: recvmsg: %s", __func__, strerror(errno));
+		(void)poll(&pfd, 1, -1);
+	}
 	if (n == -1) {
 		error("%s: recvmsg: %s", __func__, strerror(errno));
 		return -1;