X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcommon.c;h=64bc42daa3726b067cf6ea27d80f2c1f585ff063;hb=5b392ea020897afbc546274e6a6c64802ccfe546;hp=c2849420502846eabcec0c5ab701778275a0bce1;hpb=6966c22daad028359bac6747d709416a64779d6a;p=collectd.git diff --git a/src/common.c b/src/common.c index c2849420..64bc42da 100644 --- a/src/common.c +++ b/src/common.c @@ -44,6 +44,8 @@ #include #include +#include + #if HAVE_NETINET_IN_H # include #endif @@ -265,9 +267,23 @@ ssize_t swrite (int fd, const void *buf, size_t count) const char *ptr; size_t nleft; ssize_t status; + struct pollfd pfd; ptr = (const char *) buf; nleft = count; + + /* checking for closed peer connection */ + pfd.fd = fd; + pfd.events = POLLIN | POLLHUP; + pfd.revents = 0; + if (poll(&pfd, 1, 0) > 0) { + char buffer[32]; + if (recv(fd, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) { + // if recv returns zero (even though poll() said there is data to be read), + // that means the connection has been closed + return -1; + } + } while (nleft > 0) {