X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fcommon.c;h=dd4f9b15ae39a5036cbb0b40f0b8c21624ee473a;hb=03f81fa2ddcd60a4bdc7ce78775554da2166fc70;hp=92950deded7721646414edcbe5cc9c603f5ef836;hpb=872126c9a7e0a8f8ae2b28217c12c27c35af5237;p=collectd.git diff --git a/src/daemon/common.c b/src/daemon/common.c index 92950ded..dd4f9b15 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -46,9 +46,10 @@ /* for getaddrinfo */ #include -#include #include +#include + #if HAVE_NETINET_IN_H # include #endif @@ -257,8 +258,8 @@ ssize_t sread (int fd, void *buf, size_t count) assert ((0 > status) || (nleft >= (size_t)status)); - nleft = nleft - status; - ptr = ptr + status; + nleft = nleft - ((size_t) status); + ptr = ptr + ((size_t) status); } return (0); @@ -270,9 +271,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) { @@ -284,8 +299,8 @@ ssize_t swrite (int fd, const void *buf, size_t count) if (status < 0) return (status); - nleft = nleft - status; - ptr = ptr + status; + nleft = nleft - ((size_t) status); + ptr = ptr + ((size_t) status); } return (0); @@ -356,7 +371,7 @@ int strjoin (char *buffer, size_t buffer_size, } assert (buffer[buffer_size - 1] == 0); - return (strlen (buffer)); + return ((int) strlen (buffer)); } int strsubstitute (char *str, char c_from, char c_to) @@ -665,8 +680,8 @@ int check_create_dir (const char *file_orig) * Join the components together again */ dir[0] = '/'; - if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute, - fields, i + 1, "/") < 0) + if (strjoin (dir + path_is_absolute, (size_t) (dir_len - path_is_absolute), + fields, (size_t) (i + 1), "/") < 0) { ERROR ("strjoin failed: `%s', component #%i", file_orig, i); return (-1); @@ -1154,6 +1169,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 = 0; dummy = buffer; saveptr = NULL;