From 8712645f5f10e912b03ef64b922ba230b21fc342 Mon Sep 17 00:00:00 2001
From: Chris Dickens <christopher.a.dickens@gmail.com>
Date: Fri, 14 Jun 2013 10:54:29 -0700
Subject: [PATCH 1/2] POSIX: Set usbi_pipe to non-blocking by oring O_NONBLOCK
 to fd flags.

---
 libusb/os/poll_posix.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/libusb/os/poll_posix.c b/libusb/os/poll_posix.c
index bd1c538..eeaf5dc 100644
--- a/libusb/os/poll_posix.c
+++ b/libusb/os/poll_posix.c
@@ -31,11 +31,21 @@ int usbi_pipe(int pipefd[2])
 	if (ret != 0) {
 		return ret;
 	}
-	ret = fcntl(pipefd[1], F_SETFD, O_NONBLOCK);
+	ret = fcntl(pipefd[1], F_GETFL);
+	if (ret == -1) {
+		usbi_dbg("Failed to get pipe fd flags: %d", errno);
+		goto err_close_pipe;
+	}
+	ret = fcntl(pipefd[1], F_SETFL, ret | O_NONBLOCK);
 	if (ret != 0) {
 		usbi_dbg("Failed to set non-blocking on new pipe: %d", errno);
-		usbi_close(pipefd[0]);
-		usbi_close(pipefd[1]);
+		goto err_close_pipe;
 	}
+
+	return 0;
+
+err_close_pipe:
+	usbi_close(pipefd[0]);
+	usbi_close(pipefd[1]);
 	return ret;
 }
-- 
1.8.1.1

