From 66f6b702a4a194e317bfaa8c4aa5aaf8f4b588e5 Mon Sep 17 00:00:00 2001 From: Hannes Reinecke Date: Fri, 15 Nov 2013 11:29:36 +0100 Subject: [PATCH] libmultipath: do not stall on recv_packet() The CLI socket might have been closed or the daemon might have been terminated by systemd without closing the CLI socket. Hence we need to poll the socket if further data is avalailable, otherwise the read() call will hang. Signed-off-by: Hannes Reinecke --- libmultipath/uxsock.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/libmultipath/uxsock.c b/libmultipath/uxsock.c index ce89428b..fcad56e5 100644 --- a/libmultipath/uxsock.c +++ b/libmultipath/uxsock.c @@ -107,9 +107,24 @@ size_t write_all(int fd, const void *buf, size_t len) size_t read_all(int fd, void *buf, size_t len) { size_t total = 0; + ssize_t n; + int ret; + struct pollfd pfd; while (len) { - ssize_t n = read(fd, buf, len); + pfd.fd = fd; + pfd.events = POLLIN; + ret = poll(&pfd, 1, 1000); + if (!ret) { + errno = ETIMEDOUT; + return total; + } else if (ret < 0) { + if (errno == EINTR) + continue; + return total; + } else if (!pfd.revents & POLLIN) + continue; + n = read(fd, buf, len); if (n < 0) { if ((errno == EINTR) || (errno == EAGAIN)) continue; -- 2.20.1