[PATCH BlueZ] shared/io-mainloop: Try to write to the pipe only if there are readers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



bluetoothd receives a SIGPIPE and terminates if writing to a pipe that
was acquired by AcquireNotify and there are no readers. it can be
reproduced by terminating the reader process without closing the reader
end of the pipe.

poll is used to check for a POLLRDHUP event before write and an EPIPE
error is returned if the peer closed its end of the pipe.
---
 src/shared/io-mainloop.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/shared/io-mainloop.c b/src/shared/io-mainloop.c
index 2306c3479..4a70449fc 100644
--- a/src/shared/io-mainloop.c
+++ b/src/shared/io-mainloop.c
@@ -27,6 +27,7 @@
 
 #include <unistd.h>
 #include <errno.h>
+#include <poll.h>
 #include <sys/socket.h>
 
 #include "src/shared/mainloop.h"
@@ -305,7 +306,14 @@ ssize_t io_send(struct io *io, const struct iovec *iov, int iovcnt)
 	if (!io || io->fd < 0)
 		return -ENOTCONN;
 
+	struct pollfd fds[1];
+	fds[0].fd = io->fd;
+	fds[0].events = POLLRDHUP;
 	do {
+		if (poll(fds, 1, 0) > 0 && fds[0].revents & POLLRDHUP > 0) {
+			return -EPIPE;
+		}
+
 		ret = writev(io->fd, iov, iovcnt);
 	} while (ret < 0 && errno == EINTR);
 
-- 
2.19.1




[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux