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>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
# include <poll.h>
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
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
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2015-02-20 14:43:13 +01:00
|
|
|
static boost::thread l_SocketIOThread;
|
2015-02-13 21:02:48 +01:00
|
|
|
static boost::once_flag l_SocketIOOnceFlag = BOOST_ONCE_INIT;
|
|
|
|
static SOCKET l_SocketIOEventFDs[2];
|
|
|
|
static boost::mutex l_SocketIOMutex;
|
2015-02-20 14:43:13 +01:00
|
|
|
static boost::condition_variable l_SocketIOCV;
|
|
|
|
static bool l_SocketIOFDChanged;
|
2015-02-13 21:02:48 +01:00
|
|
|
static std::map<SOCKET, SocketEventDescriptor> l_SocketIOSockets;
|
|
|
|
|
|
|
|
void SocketEvents::InitializeThread(void)
|
|
|
|
{
|
|
|
|
Socket::SocketPair(l_SocketIOEventFDs);
|
|
|
|
|
|
|
|
Utility::SetNonBlockingSocket(l_SocketIOEventFDs[0]);
|
|
|
|
Utility::SetNonBlockingSocket(l_SocketIOEventFDs[1]);
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
Utility::SetCloExec(l_SocketIOEventFDs[0]);
|
|
|
|
Utility::SetCloExec(l_SocketIOEventFDs[1]);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
SocketEventDescriptor sed;
|
|
|
|
sed.Events = POLLIN;
|
|
|
|
|
|
|
|
l_SocketIOSockets[l_SocketIOEventFDs[0]] = sed;
|
|
|
|
|
2015-02-20 14:43:13 +01:00
|
|
|
l_SocketIOThread = boost::thread(&SocketEvents::ThreadProc);
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketEvents::ThreadProc(void)
|
|
|
|
{
|
|
|
|
Utility::SetThreadName("SocketIO");
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pollfd *pfds;
|
|
|
|
int pfdcount;
|
|
|
|
|
|
|
|
typedef std::map<SOCKET, SocketEventDescriptor>::value_type SocketDesc;
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex);
|
|
|
|
|
|
|
|
pfdcount = l_SocketIOSockets.size();
|
|
|
|
pfds = new pollfd[pfdcount];
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const SocketDesc& desc, l_SocketIOSockets) {
|
|
|
|
pfds[i].fd = desc.first;
|
|
|
|
pfds[i].events = desc.second.Events;
|
|
|
|
pfds[i].revents = 0;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
(void) WSAPoll(pfds, pfdcount, -1);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
(void) poll(pfds, pfdcount, -1);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2015-02-20 14:43:13 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex);
|
|
|
|
|
|
|
|
if (l_SocketIOFDChanged) {
|
|
|
|
l_SocketIOFDChanged = false;
|
|
|
|
l_SocketIOCV.notify_all();
|
2015-03-02 09:51:24 +01:00
|
|
|
delete [] pfds;
|
2015-02-20 14:43:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
for (int i = 0; i < pfdcount; i++) {
|
|
|
|
if ((pfds[i].revents & (POLLIN | POLLOUT | POLLHUP | POLLERR)) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pfds[i].fd == l_SocketIOEventFDs[0]) {
|
|
|
|
char buffer[512];
|
|
|
|
if (recv(l_SocketIOEventFDs[0], buffer, sizeof(buffer), 0) < 0)
|
|
|
|
Log(LogCritical, "SocketEvents", "Read from event FD failed.");
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketEventDescriptor desc;
|
|
|
|
Object::Ptr ltref;
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex);
|
|
|
|
|
|
|
|
std::map<SOCKET, SocketEventDescriptor>::const_iterator it = l_SocketIOSockets.find(pfds[i].fd);
|
|
|
|
|
|
|
|
if (it == l_SocketIOSockets.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
desc = it->second;
|
|
|
|
|
|
|
|
/* We must hold a ref-counted reference to the event object to keep it alive. */
|
2015-02-16 11:39:34 +01:00
|
|
|
ltref = desc.LifesupportObject;
|
|
|
|
VERIFY(ltref);
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
2015-02-27 15:53:38 +01:00
|
|
|
try {
|
|
|
|
desc.EventInterface->OnEvent(pfds[i].revents);
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
delete [] pfds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 15:23:06 +01:00
|
|
|
void SocketEvents::WakeUpThread(bool wait)
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
2015-02-20 15:23:06 +01:00
|
|
|
if (wait) {
|
|
|
|
if (boost::this_thread::get_id() != l_SocketIOThread.get_id()) {
|
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex);
|
2015-02-20 14:43:13 +01:00
|
|
|
|
2015-02-20 15:23:06 +01:00
|
|
|
l_SocketIOFDChanged = true;
|
2015-02-20 14:43:13 +01:00
|
|
|
|
2015-09-29 10:31:16 +02:00
|
|
|
while (l_SocketIOFDChanged) {
|
|
|
|
(void) send(l_SocketIOEventFDs[1], "T", 1, 0);
|
2015-02-20 14:43:13 +01:00
|
|
|
|
2015-09-29 10:31:16 +02:00
|
|
|
boost::system_time const timeout = boost::get_system_time() + boost::posix_time::milliseconds(50);
|
|
|
|
l_SocketIOCV.timed_wait(lock, timeout);
|
|
|
|
}
|
2015-02-20 15:23:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(void) send(l_SocketIOEventFDs[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)
|
2015-02-13 21:02:48 +01:00
|
|
|
: m_FD(socket->GetFD())
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2015-02-20 14:43:13 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex);
|
|
|
|
|
2015-02-16 11:39:34 +01:00
|
|
|
VERIFY(m_FD != INVALID_SOCKET);
|
2015-02-15 18:50:25 +01:00
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
SocketEventDescriptor desc;
|
|
|
|
desc.Events = 0;
|
|
|
|
desc.EventInterface = this;
|
2015-02-16 11:39:34 +01:00
|
|
|
desc.LifesupportObject = lifesupportObject;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2015-02-20 14:43:13 +01:00
|
|
|
VERIFY(l_SocketIOSockets.find(m_FD) == l_SocketIOSockets.end());
|
2015-02-15 18:50:25 +01:00
|
|
|
|
2015-02-20 14:43:13 +01:00
|
|
|
l_SocketIOSockets[m_FD] = desc;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
m_Events = true;
|
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
/* There's no need to wake up the I/O thread here. */
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketEvents::Unregister(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex);
|
|
|
|
|
2015-02-20 14:43:13 +01:00
|
|
|
if (m_FD == INVALID_SOCKET)
|
|
|
|
return;
|
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
l_SocketIOSockets.erase(m_FD);
|
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)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex);
|
|
|
|
|
2015-02-20 14:43:13 +01:00
|
|
|
if (m_FD == INVALID_SOCKET)
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Tried to read/write from a closed socket."));
|
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
std::map<SOCKET, SocketEventDescriptor>::iterator it = l_SocketIOSockets.find(m_FD);
|
|
|
|
|
|
|
|
if (it == l_SocketIOSockets.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
it->second.Events = events;
|
|
|
|
}
|
|
|
|
|
|
|
|
WakeUpThread();
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
bool SocketEvents::IsHandlingEvents(void) const
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOMutex);
|
|
|
|
return m_Events;
|
|
|
|
}
|
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
void SocketEvents::OnEvent(int revents)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|