2015-02-13 21:02:48 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2015-02-13 21:02:48 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "base/socketevents.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include <boost/thread/once.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
struct SocketEventDescriptor
|
|
|
|
{
|
|
|
|
int Events;
|
|
|
|
SocketEvents *EventInterface;
|
2015-02-16 11:39:34 +01:00
|
|
|
Object *LifesupportObject;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
|
|
|
SocketEventDescriptor(void)
|
2015-03-02 09:46:41 +01:00
|
|
|
: Events(0), EventInterface(NULL), LifesupportObject(NULL)
|
2015-02-13 21:02:48 +01:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2016-01-28 11:55:31 +01:00
|
|
|
struct EventDescription
|
|
|
|
{
|
|
|
|
int REvents;
|
|
|
|
SocketEventDescriptor Descriptor;
|
|
|
|
Object::Ptr LifesupportReference;
|
|
|
|
};
|
|
|
|
|
2016-02-16 13:30:55 +01:00
|
|
|
#define SOCKET_IOTHREADS 8
|
2016-02-02 11:17:33 +01:00
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
static boost::once_flag l_SocketIOOnceFlag = BOOST_ONCE_INIT;
|
2016-02-16 13:30:55 +01:00
|
|
|
static boost::thread l_SocketIOThreads[SOCKET_IOTHREADS];
|
|
|
|
#ifdef __linux__
|
|
|
|
static SOCKET l_SocketIOPollFDs[SOCKET_IOTHREADS];
|
|
|
|
#endif /* __linux__ */
|
|
|
|
static SOCKET l_SocketIOEventFDs[SOCKET_IOTHREADS][2];
|
|
|
|
static bool l_SocketIOFDChanged[SOCKET_IOTHREADS];
|
|
|
|
static boost::mutex l_SocketIOMutex[SOCKET_IOTHREADS];
|
|
|
|
static boost::condition_variable l_SocketIOCV[SOCKET_IOTHREADS];
|
|
|
|
static std::map<SOCKET, SocketEventDescriptor> l_SocketIOSockets[SOCKET_IOTHREADS];
|
2016-02-02 11:17:33 +01:00
|
|
|
|
|
|
|
int SocketEvents::m_NextID = 0;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
|
|
|
void SocketEvents::InitializeThread(void)
|
|
|
|
{
|
2016-02-16 13:30:55 +01:00
|
|
|
for (int i = 0; i < SOCKET_IOTHREADS; i++) {
|
|
|
|
#ifdef __linux__
|
2016-02-22 08:12:35 +01:00
|
|
|
l_SocketIOPollFDs[i] = epoll_create(128);
|
|
|
|
Utility::SetCloExec(l_SocketIOPollFDs[i]);
|
2016-02-16 13:30:55 +01:00
|
|
|
#endif /* __linux__ */
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
Socket::SocketPair(l_SocketIOEventFDs[i]);
|
|
|
|
|
|
|
|
Utility::SetNonBlockingSocket(l_SocketIOEventFDs[i][0]);
|
|
|
|
Utility::SetNonBlockingSocket(l_SocketIOEventFDs[i][1]);
|
2015-02-13 21:02:48 +01:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2016-02-02 11:17:33 +01:00
|
|
|
Utility::SetCloExec(l_SocketIOEventFDs[i][0]);
|
|
|
|
Utility::SetCloExec(l_SocketIOEventFDs[i][1]);
|
2015-02-13 21:02:48 +01:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
SocketEventDescriptor sed;
|
|
|
|
sed.Events = POLLIN;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
l_SocketIOSockets[i][l_SocketIOEventFDs[i][0]] = sed;
|
|
|
|
l_SocketIOFDChanged[i] = true;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
l_SocketIOThreads[i] = boost::thread(&SocketEvents::ThreadProc, i);
|
|
|
|
}
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
void SocketEvents::ThreadProc(int tid)
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
|
|
|
Utility::SetThreadName("SocketIO");
|
|
|
|
|
2016-02-01 09:33:33 +01:00
|
|
|
std::vector<pollfd> pfds;
|
|
|
|
std::vector<SocketEventDescriptor> descriptors;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-01-28 07:19:32 +01:00
|
|
|
for (;;) {
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
2016-02-02 11:17:33 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex[tid]);
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
if (l_SocketIOFDChanged[tid]) {
|
|
|
|
pfds.resize(l_SocketIOSockets[tid].size());
|
|
|
|
descriptors.resize(l_SocketIOSockets[tid].size());
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-01-28 07:19:32 +01:00
|
|
|
int i = 0;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-01-28 13:07:09 +01:00
|
|
|
typedef std::map<SOCKET, SocketEventDescriptor>::value_type kv_pair;
|
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
BOOST_FOREACH(const kv_pair& desc, l_SocketIOSockets[tid]) {
|
2016-02-02 09:52:23 +01:00
|
|
|
if (desc.second.EventInterface)
|
|
|
|
desc.second.EventInterface->m_PFD = &pfds[i];
|
|
|
|
|
2016-01-28 07:19:32 +01:00
|
|
|
pfds[i].fd = desc.first;
|
|
|
|
pfds[i].events = desc.second.Events;
|
2016-01-28 13:07:09 +01:00
|
|
|
descriptors[i] = desc.second;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-01-28 07:19:32 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
l_SocketIOFDChanged[tid] = false;
|
|
|
|
l_SocketIOCV[tid].notify_all();
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 09:33:33 +01:00
|
|
|
ASSERT(!pfds.empty());
|
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
#ifdef _WIN32
|
2016-02-01 09:33:33 +01:00
|
|
|
(void) WSAPoll(&pfds[0], pfds.size(), -1);
|
2015-02-13 21:02:48 +01:00
|
|
|
#else /* _WIN32 */
|
2016-02-01 09:33:33 +01:00
|
|
|
(void) poll(&pfds[0], pfds.size(), -1);
|
2015-02-13 21:02:48 +01:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2016-01-28 11:55:31 +01:00
|
|
|
std::vector<EventDescription> events;
|
|
|
|
|
2015-02-20 14:43:13 +01:00
|
|
|
{
|
2016-02-02 11:17:33 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex[tid]);
|
2015-02-20 14:43:13 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
if (l_SocketIOFDChanged[tid])
|
2015-02-20 14:43:13 +01:00
|
|
|
continue;
|
|
|
|
|
2016-02-01 09:33:33 +01:00
|
|
|
for (int i = 0; i < pfds.size(); i++) {
|
2016-01-28 11:55:31 +01:00
|
|
|
if ((pfds[i].revents & (POLLIN | POLLOUT | POLLHUP | POLLERR)) == 0)
|
|
|
|
continue;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
if (pfds[i].fd == l_SocketIOEventFDs[tid][0]) {
|
2016-01-28 11:55:31 +01:00
|
|
|
char buffer[512];
|
2016-02-02 11:17:33 +01:00
|
|
|
if (recv(l_SocketIOEventFDs[tid][0], buffer, sizeof(buffer), 0) < 0)
|
2016-01-28 11:55:31 +01:00
|
|
|
Log(LogCritical, "SocketEvents", "Read from event FD failed.");
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-01-28 11:55:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-01-28 11:55:31 +01:00
|
|
|
EventDescription event;
|
|
|
|
event.REvents = pfds[i].revents;
|
2016-01-28 13:07:09 +01:00
|
|
|
event.Descriptor = descriptors[i];
|
2016-01-28 11:55:31 +01:00
|
|
|
event.LifesupportReference = event.Descriptor.LifesupportObject;
|
|
|
|
VERIFY(event.LifesupportReference);
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-01-28 11:55:31 +01:00
|
|
|
events.push_back(event);
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
2016-01-28 11:55:31 +01:00
|
|
|
}
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-01-28 11:55:31 +01:00
|
|
|
BOOST_FOREACH(const EventDescription& event, events) {
|
2015-02-27 15:53:38 +01:00
|
|
|
try {
|
2016-01-28 11:55:31 +01:00
|
|
|
event.Descriptor.EventInterface->OnEvent(event.REvents);
|
2015-02-27 15:53:38 +01:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "SocketEvents")
|
|
|
|
<< "Exception thrown in socket I/O handler:\n"
|
|
|
|
<< DiagnosticInformation(ex);
|
|
|
|
} catch (...) {
|
|
|
|
Log(LogCritical, "SocketEvents", "Exception of unknown type thrown in socket I/O handler.");
|
|
|
|
}
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 15:23:06 +01:00
|
|
|
void SocketEvents::WakeUpThread(bool wait)
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
2016-02-16 13:30:55 +01:00
|
|
|
int tid = m_ID % SOCKET_IOTHREADS;
|
2016-02-02 11:17:33 +01:00
|
|
|
|
|
|
|
if (boost::this_thread::get_id() == l_SocketIOThreads[tid].get_id())
|
|
|
|
return;
|
|
|
|
|
2015-02-20 15:23:06 +01:00
|
|
|
if (wait) {
|
2016-02-02 11:17:33 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex[tid]);
|
2015-02-20 14:43:13 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
l_SocketIOFDChanged[tid] = true;
|
2015-02-20 14:43:13 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
while (l_SocketIOFDChanged[tid]) {
|
|
|
|
(void) send(l_SocketIOEventFDs[tid][1], "T", 1, 0);
|
2015-02-20 14:43:13 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
boost::system_time const timeout = boost::get_system_time() + boost::posix_time::milliseconds(50);
|
|
|
|
l_SocketIOCV[tid].timed_wait(lock, timeout);
|
2015-02-20 15:23:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-02 11:17:33 +01:00
|
|
|
(void) send(l_SocketIOEventFDs[tid][1], "T", 1, 0);
|
2015-02-20 14:43:13 +01:00
|
|
|
}
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for the SocketEvents class.
|
|
|
|
*/
|
2015-02-16 11:39:34 +01:00
|
|
|
SocketEvents::SocketEvents(const Socket::Ptr& socket, Object *lifesupportObject)
|
2016-02-02 11:17:33 +01:00
|
|
|
: m_ID(m_NextID++), m_FD(socket->GetFD()), m_PFD(NULL)
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
|
|
|
boost::call_once(l_SocketIOOnceFlag, &SocketEvents::InitializeThread);
|
|
|
|
|
2015-02-16 11:39:34 +01:00
|
|
|
Register(lifesupportObject);
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SocketEvents::~SocketEvents(void)
|
|
|
|
{
|
2015-02-16 11:39:34 +01:00
|
|
|
VERIFY(m_FD == INVALID_SOCKET);
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
2015-02-16 11:39:34 +01:00
|
|
|
void SocketEvents::Register(Object *lifesupportObject)
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
2016-02-16 13:30:55 +01:00
|
|
|
int tid = m_ID % SOCKET_IOTHREADS;
|
2016-02-02 11:17:33 +01:00
|
|
|
|
2016-01-28 07:19:32 +01:00
|
|
|
{
|
2016-02-02 11:17:33 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex[tid]);
|
2015-02-20 14:43:13 +01:00
|
|
|
|
2016-01-28 07:19:32 +01:00
|
|
|
VERIFY(m_FD != INVALID_SOCKET);
|
2015-02-15 18:50:25 +01:00
|
|
|
|
2016-01-28 07:19:32 +01:00
|
|
|
SocketEventDescriptor desc;
|
|
|
|
desc.Events = 0;
|
|
|
|
desc.EventInterface = this;
|
|
|
|
desc.LifesupportObject = lifesupportObject;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
VERIFY(l_SocketIOSockets[tid].find(m_FD) == l_SocketIOSockets[tid].end());
|
2015-02-15 18:50:25 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
l_SocketIOSockets[tid][m_FD] = desc;
|
|
|
|
l_SocketIOFDChanged[tid] = true;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-01-28 07:19:32 +01:00
|
|
|
m_Events = true;
|
|
|
|
}
|
2015-08-29 01:16:16 +02:00
|
|
|
|
2016-01-28 07:19:32 +01:00
|
|
|
WakeUpThread(true);
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketEvents::Unregister(void)
|
|
|
|
{
|
2016-02-16 13:30:55 +01:00
|
|
|
int tid = m_ID % SOCKET_IOTHREADS;
|
2016-02-02 11:17:33 +01:00
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
2016-02-02 11:17:33 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex[tid]);
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2015-02-20 14:43:13 +01:00
|
|
|
if (m_FD == INVALID_SOCKET)
|
|
|
|
return;
|
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
l_SocketIOSockets[tid].erase(m_FD);
|
|
|
|
l_SocketIOFDChanged[tid] = true;
|
|
|
|
|
2015-02-15 18:50:25 +01:00
|
|
|
m_FD = INVALID_SOCKET;
|
2015-08-29 01:16:16 +02:00
|
|
|
|
|
|
|
m_Events = false;
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 15:23:06 +01:00
|
|
|
WakeUpThread(true);
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketEvents::ChangeEvents(int events)
|
|
|
|
{
|
2016-02-16 13:30:55 +01:00
|
|
|
if (m_FD == INVALID_SOCKET)
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Tried to read/write from a closed socket."));
|
|
|
|
|
|
|
|
int tid = m_ID % SOCKET_IOTHREADS;
|
2016-02-02 11:17:33 +01:00
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
2016-02-02 11:17:33 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex[tid]);
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
std::map<SOCKET, SocketEventDescriptor>::iterator it = l_SocketIOSockets[tid].find(m_FD);
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
if (it == l_SocketIOSockets[tid].end())
|
2015-02-13 21:02:48 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
it->second.Events = events;
|
2016-02-02 09:52:23 +01:00
|
|
|
|
2016-02-02 11:17:33 +01:00
|
|
|
if (m_PFD && boost::this_thread::get_id() == l_SocketIOThreads[tid].get_id())
|
2016-02-02 09:52:23 +01:00
|
|
|
m_PFD->events = events;
|
|
|
|
else
|
2016-02-02 11:17:33 +01:00
|
|
|
l_SocketIOFDChanged[tid] = true;
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WakeUpThread();
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
bool SocketEvents::IsHandlingEvents(void) const
|
|
|
|
{
|
2016-02-16 13:30:55 +01:00
|
|
|
int tid = m_ID % SOCKET_IOTHREADS;
|
2016-02-02 11:17:33 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex[tid]);
|
2015-08-29 01:16:16 +02:00
|
|
|
return m_Events;
|
|
|
|
}
|
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
void SocketEvents::OnEvent(int revents)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|