X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fdaemon%2Fcommon.c;h=7b7353d96685d60b8bbe8fd951fb35f6e67b13cb;hb=5841d1fb13436af787c748a965084cb06e66d326;hp=8f22011ed57c46c94244f8273461fce3fa7af155;hpb=83bd4369fc36851bb9a92d0dd3a9d3bc6a408fdd;p=collectd.git diff --git a/src/daemon/common.c b/src/daemon/common.c index 8f22011e..7b7353d9 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -48,6 +48,8 @@ #include #include +#include + #if HAVE_NETINET_IN_H # include #endif @@ -113,10 +115,9 @@ char *ssnprintf_alloc (char const *format, ...) /* {{{ */ return (strdup (static_buffer)); /* Allocate a buffer large enough to hold the string. */ - alloc_buffer = malloc (alloc_buffer_size); + alloc_buffer = calloc (1, alloc_buffer_size); if (alloc_buffer == NULL) return (NULL); - memset (alloc_buffer, 0, alloc_buffer_size); /* Print again into this new buffer. */ va_start (ap, format); @@ -142,7 +143,7 @@ char *sstrdup (const char *s) /* Do not use `strdup' here, because it's not specified in POSIX. It's * ``only'' an XSI extension. */ sz = strlen (s) + 1; - r = (char *) malloc (sizeof (char) * sz); + r = malloc (sz); if (r == NULL) { ERROR ("sstrdup: Out of memory."); @@ -269,9 +270,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) { @@ -393,10 +408,9 @@ int escape_string (char *buffer, size_t buffer_size) if (buffer_size < 3) return (EINVAL); - temp = (char *) malloc (buffer_size); + temp = calloc (1, buffer_size); if (temp == NULL) return (ENOMEM); - memset (temp, 0, buffer_size); temp[0] = '"'; j = 1; @@ -1153,6 +1167,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;