configure.ac: fix path when testing for liboconfig/parser.c presence
[collectd.git] / src / common.c
index f41e950..4da8fee 100644 (file)
@@ -44,6 +44,8 @@
 #include <sys/socket.h>
 #include <netdb.h>
 
+#include <poll.h>
+
 #if HAVE_NETINET_IN_H
 # include <netinet/in.h>
 #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)
        {
@@ -445,7 +461,7 @@ int escape_slashes (char *buf, int buf_len)
        if (buf[0] == '/')
                memmove (buf, buf + 1, buf_len - 1);
 
-       for (i = 0; i < buf_len - 1; i++)
+       for (i = 0; i < buf_len; i++)
        {
                if (buf[i] == '\0')
                        break;
@@ -1088,6 +1104,9 @@ int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
        char *ptr;
        char *saveptr;
 
+       if ((buffer == NULL) || (vl == NULL) || (ds == NULL))
+               return EINVAL;
+
        i = -1;
        dummy = buffer;
        saveptr = NULL;