Module: check_mk
Branch: master
Commit: 9f21a876581ca60a54f6c3dc491a586b7dd1ba57
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=9f21a876581ca6…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Dec 15 14:57:20 2015 +0100
Fixed bug in handling partial writes. clang-format/iwyu/cleanup.
---
livestatus/src/OutputBuffer.cc | 29 ++++++++++++++---------------
livestatus/src/OutputBuffer.h | 4 ++--
livestatus/src/unixcat.c | 35 +++++++++++++++++++----------------
3 files changed, 35 insertions(+), 33 deletions(-)
diff --git a/livestatus/src/OutputBuffer.cc b/livestatus/src/OutputBuffer.cc
index bc1d1fe..3bd5f88 100644
--- a/livestatus/src/OutputBuffer.cc
+++ b/livestatus/src/OutputBuffer.cc
@@ -25,19 +25,19 @@
#include "OutputBuffer.h"
#include <errno.h>
#include <stdarg.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
+#include <cinttypes>
#include "Query.h"
#include "logger.h"
-
#define WRITE_TIMEOUT_USEC 100000
-
OutputBuffer::OutputBuffer()
: _max_size(INITIAL_OUTPUT_BUFFER_SIZE)
{
@@ -127,12 +127,12 @@ void OutputBuffer::flush(int fd, int *termination_flag)
}
-void OutputBuffer::writeData(int fd, int *termination_flag, const char *write_from, int
to_write)
+void OutputBuffer::writeData(int fd, int *termination_flag, const char *buffer,
+ size_t bytes_to_write)
{
- struct timeval tv;
- while (!*termination_flag && to_write > 0)
- {
- tv.tv_sec = WRITE_TIMEOUT_USEC / 1000000;
+ while (!*termination_flag && bytes_to_write > 0) {
+ struct timeval tv;
+ tv.tv_sec = WRITE_TIMEOUT_USEC / 1000000;
tv.tv_usec = WRITE_TIMEOUT_USEC % 1000000;
fd_set fds;
@@ -141,16 +141,15 @@ void OutputBuffer::writeData(int fd, int *termination_flag, const
char *write_fr
int retval = select(fd + 1, NULL, &fds, NULL, &tv);
if (retval > 0 && FD_ISSET(fd, &fds)) {
- ssize_t w = write(fd, write_from, to_write);
- if (w < 0) {
- logger(LG_INFO, "Couldn't write %d bytes to client socket:
%s", to_write, strerror(errno));
+ ssize_t bytes_written = write(fd, buffer, bytes_to_write);
+ if (bytes_written == -1) {
+ logger(LG_INFO,
+ "Couldn't write %" PRIdMAX " bytes to client
socket: %s",
+ static_cast<intmax_t>(bytes_to_write), strerror(errno));
break;
}
- else if (w == 0)
- logger(LG_INFO, "Strange: wrote 0 bytes inspite of positive
select()");
- else {
- to_write -= w;
- }
+ buffer += bytes_written;
+ bytes_to_write -= bytes_written;
}
}
}
diff --git a/livestatus/src/OutputBuffer.h b/livestatus/src/OutputBuffer.h
index eb0fe56..28795f3 100644
--- a/livestatus/src/OutputBuffer.h
+++ b/livestatus/src/OutputBuffer.h
@@ -26,9 +26,9 @@
#define OutputBuffer_h
#include "config.h" // IWYU pragma: keep
+#include <stddef.h>
#include <string>
-
#define INITIAL_OUTPUT_BUFFER_SIZE 1
#define RESPONSE_CODE_OK 200
@@ -76,7 +76,7 @@ public:
private:
void needSpace(unsigned);
- void writeData(int fd, int *, const char *, int);
+ void writeData(int fd, int *, const char *, size_t);
};
#endif // OutputBuffer_h
diff --git a/livestatus/src/unixcat.c b/livestatus/src/unixcat.c
index e926c3c..db53b92 100644
--- a/livestatus/src/unixcat.c
+++ b/livestatus/src/unixcat.c
@@ -27,6 +27,7 @@
#include <inttypes.h>
#include <pthread.h>
#include <signal.h>
+#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -35,7 +36,7 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
-#include <sys/types.h> // IWYU pragma: keep
+#include <sys/types.h> // IWYU pragma: keep
#include <sys/un.h>
#include <unistd.h>
@@ -89,17 +90,16 @@ void *copy_thread(void *info)
int from = ti->from;
int to = ti->to;
- char buffer[65536];
- while (1)
- {
- ssize_t r = read_with_timeout(from, buffer, sizeof(buffer), 1000000);
+ char read_buffer[65536];
+ while (1) {
+ ssize_t r = read_with_timeout(from, read_buffer, sizeof(read_buffer), 1000000);
if (r == -1) {
- fprintf(stderr, "Error reading from %d: %s\n", from,
strerror(errno));
+ fprintf(stderr, "Error reading from %d: %s\n", from,
+ strerror(errno));
break;
}
else if (r == 0) {
- if (ti->should_shutdown)
- shutdown(to, SHUT_WR);
+ if (ti->should_shutdown) shutdown(to, SHUT_WR);
if (ti->terminate_on_read_eof) {
exit(0);
return voidp;
@@ -109,16 +109,19 @@ void *copy_thread(void *info)
else if (r == -2) {
r = 0;
}
- char *write_pos = buffer;
- while (r) {
- ssize_t w = write(to, write_pos, r);
- if (w > 0)
- r -= w;
- else if (w == 0 && r > 0) {
- fprintf(stderr, "Error: Cannot write %" PRIdMAX " bytes to
%d: %s\n",
- (intmax_t)w, to, strerror(errno));
+
+ const char *buffer = read_buffer;
+ size_t bytes_to_write = r;
+ while (bytes_to_write > 0) {
+ ssize_t bytes_written = write(to, buffer, bytes_to_write);
+ if (bytes_written == -1) {
+ fprintf(stderr,
+ "Error: Cannot write %" PRIdMAX " bytes to %d:
%s\n",
+ (intmax_t)bytes_written, to, strerror(errno));
break;
}
+ buffer += bytes_written;
+ bytes_to_write -= bytes_written;
}
}
return voidp;