Module: check_mk
Branch: master
Commit: d5494fdc6f8dffec235c509906341bf4c8b0893a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=d5494fdc6f8dff…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Jul 11 16:17:33 2017 +0200
Encapsulate select() calls, part 10
Move maximum file descriptor calculation into Poller class, simplifying the
call sites quite a bit. Note that we include the implementation detail
<sys/select.h> only in Poller.h now.
Change-Id: I249124fe9f07aaa9b54a381d860e172e99d31d30
---
livestatus/src/InputBuffer.cc | 2 +-
livestatus/src/OutputBuffer.cc | 2 +-
livestatus/src/Poller.h | 16 ++++++++++++----
livestatus/src/module.cc | 4 ++--
livestatus/src/unixcat.cc | 6 +-----
5 files changed, 17 insertions(+), 13 deletions(-)
diff --git a/livestatus/src/InputBuffer.cc b/livestatus/src/InputBuffer.cc
index 889205b..ee04621 100644
--- a/livestatus/src/InputBuffer.cc
+++ b/livestatus/src/InputBuffer.cc
@@ -206,7 +206,7 @@ InputBuffer::Result InputBuffer::readData() {
Poller poller;
poller.addReadFD(_fd);
- int retval = poller.poll(_fd + 1, milliseconds(200));
+ int retval = poller.poll(milliseconds(200));
if (retval > 0 && poller.isReadFDSet(_fd)) {
ssize_t r = read(_fd, &_readahead_buffer[_write_index],
_readahead_buffer.capacity() - _write_index);
diff --git a/livestatus/src/OutputBuffer.cc b/livestatus/src/OutputBuffer.cc
index 83471a2..cbe565b 100644
--- a/livestatus/src/OutputBuffer.cc
+++ b/livestatus/src/OutputBuffer.cc
@@ -79,7 +79,7 @@ void OutputBuffer::writeData(ostringstream &os) {
while (!_termination_flag && bytes_to_write > 0) {
Poller poller;
poller.addWriteFD(_fd);
- int retval = poller.poll(_fd + 1, milliseconds(100));
+ int retval = poller.poll(milliseconds(100));
if (retval > 0 && poller.isWriteFDSet(_fd)) {
ssize_t bytes_written = write(_fd, buffer, bytes_to_write);
if (bytes_written == -1) {
diff --git a/livestatus/src/Poller.h b/livestatus/src/Poller.h
index f80a867..976acf5 100644
--- a/livestatus/src/Poller.h
+++ b/livestatus/src/Poller.h
@@ -27,6 +27,7 @@
#include "config.h" // IWYU pragma: keep
#include <sys/select.h>
+#include <algorithm>
#include <cerrno>
#include "ChronoUtils.h"
@@ -35,10 +36,11 @@ public:
Poller() {
FD_ZERO(&_readfds);
FD_ZERO(&_writefds);
+ _maxfd = -1;
}
template <typename Rep, typename Period>
- int poll(int nfds, std::chrono::duration<Rep, Period> timeout) {
+ int poll(std::chrono::duration<Rep, Period> timeout) {
int retval;
timeval tv = to_timeval(timeout);
// I/O primitives can fail when interrupted by a signal, so we should
@@ -46,13 +48,13 @@ public:
// encapsulated in e.g. glibc's TEMP_FAILURE_RETRY macro, see:
//
https://www.gnu.org/software/libc/manual/html_node/Interrupted-Primitives.h…
do {
- retval = select(nfds, &_readfds, &_writefds, nullptr, &tv);
+ retval = select(_maxfd + 1, &_readfds, &_writefds, nullptr,
&tv);
} while (retval == -1 && errno == EINTR);
return retval;
}
- void addReadFD(int fd) { FD_SET(fd, &_readfds); }
- void addWriteFD(int fd) { FD_SET(fd, &_writefds); }
+ void addReadFD(int fd) { addFD(fd, _readfds); }
+ void addWriteFD(int fd) { addFD(fd, _writefds); }
bool isReadFDSet(int fd) const { return FD_ISSET(fd, &_readfds); }
bool isWriteFDSet(int fd) const { return FD_ISSET(fd, &_writefds); }
@@ -60,6 +62,12 @@ public:
private:
fd_set _readfds;
fd_set _writefds;
+ int _maxfd;
+
+ void addFD(int fd, fd_set &fds) {
+ FD_SET(fd, &fds);
+ _maxfd = std::max(_maxfd, fd);
+ }
};
#endif // Poller_h
diff --git a/livestatus/src/module.cc b/livestatus/src/module.cc
index 22d75af..3e7907b 100644
--- a/livestatus/src/module.cc
+++ b/livestatus/src/module.cc
@@ -30,9 +30,9 @@
#include "config.h"
#include <fcntl.h>
#include <pthread.h>
-#include <sys/time.h>
#include <sys/socket.h>
#include <sys/stat.h>
+#include <sys/time.h>
#include <sys/un.h>
#include <unistd.h>
#include <chrono>
@@ -203,7 +203,7 @@ void *main_thread(void *data) {
Poller poller;
poller.addReadFD(g_unix_socket);
- int retval = poller.poll(g_unix_socket + 1, milliseconds(2500));
+ int retval = poller.poll(milliseconds(2500));
if (retval > 0 && poller.isReadFDSet(g_unix_socket)) {
int cc = accept(g_unix_socket, nullptr, nullptr);
if (cc > g_max_fd_ever) {
diff --git a/livestatus/src/unixcat.cc b/livestatus/src/unixcat.cc
index 8c48d15..eb58200 100644
--- a/livestatus/src/unixcat.cc
+++ b/livestatus/src/unixcat.cc
@@ -61,11 +61,7 @@ ssize_t read_with_timeout(int from, char *buffer, int size,
microseconds timeout) {
Poller poller;
poller.addReadFD(from);
- int retval = poller.poll(from + 1, timeout);
- if (retval > 0) {
- return read(from, buffer, size);
- }
- return -2;
+ return poller.poll(timeout) > 0 ? read(from, buffer, size) : -2;
}
void *copy_thread(void *info) {