X-Git-Url: https://git.octo.it/?a=blobdiff_plain;ds=sidebyside;f=src%2Fdaemon%2Fcommon.c;h=53c1f08ac5aa87c362844a8189e703e96a699b77;hb=fc6f1e3d33d646391adb5cedc62e7b3904c15003;hp=b2448086c53aadc0641b63bf7eef9b51e8c1decf;hpb=a09f2fdf0450eb76317e1b7575a12ffb926068f4;p=collectd.git diff --git a/src/daemon/common.c b/src/daemon/common.c index b2448086..53c1f08a 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -49,6 +49,8 @@ #include #include +#include + #if HAVE_NETINET_IN_H # include #endif @@ -270,9 +272,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) {