2015-02-13 21:02:48 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
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"
|
2016-04-18 14:25:52 +02:00
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/scriptglobal.hpp"
|
2015-02-13 21:02:48 +01:00
|
|
|
#include <boost/thread/once.hpp>
|
|
|
|
#include <map>
|
2016-02-04 16:01:19 +01:00
|
|
|
#ifdef __linux__
|
|
|
|
# include <sys/epoll.h>
|
|
|
|
#endif /* __linux__ */
|
2015-02-13 21:02:48 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
static boost::once_flag l_SocketIOOnceFlag = BOOST_ONCE_INIT;
|
2016-04-18 14:25:52 +02:00
|
|
|
static SocketEventEngine *l_SocketIOEngine;
|
2016-02-02 11:17:33 +01:00
|
|
|
|
|
|
|
int SocketEvents::m_NextID = 0;
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
void SocketEventEngine::Start(void)
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
2016-04-18 14:25:52 +02:00
|
|
|
for (int tid = 0; tid < SOCKET_IOTHREADS; tid++) {
|
|
|
|
Socket::SocketPair(m_EventFDs[tid]);
|
2016-02-02 11:17:33 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
Utility::SetNonBlockingSocket(m_EventFDs[tid][0]);
|
|
|
|
Utility::SetNonBlockingSocket(m_EventFDs[tid][1]);
|
2015-02-13 21:02:48 +01:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2016-04-18 14:25:52 +02:00
|
|
|
Utility::SetCloExec(m_EventFDs[tid][0]);
|
|
|
|
Utility::SetCloExec(m_EventFDs[tid][1]);
|
2015-02-13 21:02:48 +01:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
InitializeThread(tid);
|
2016-02-04 16:01:19 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
m_Threads[tid] = boost::thread(boost::bind(&SocketEventEngine::ThreadProc, this, tid));
|
2016-02-02 11:17:33 +01:00
|
|
|
}
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
void SocketEventEngine::WakeUpThread(int sid, bool wait)
|
2016-02-04 16:01:19 +01:00
|
|
|
{
|
2016-04-18 14:25:52 +02:00
|
|
|
int tid = sid % SOCKET_IOTHREADS;
|
2016-02-04 16:01:19 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
if (boost::this_thread::get_id() == m_Threads[tid].get_id())
|
|
|
|
return;
|
2016-02-04 16:01:19 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
if (wait) {
|
|
|
|
boost::mutex::scoped_lock lock(m_EventMutex[tid]);
|
2016-02-04 16:01:19 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
m_FDChanged[tid] = true;
|
2016-02-04 16:01:19 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
while (m_FDChanged[tid]) {
|
|
|
|
(void) send(m_EventFDs[tid][1], "T", 1, 0);
|
2016-02-04 16:01:19 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
boost::system_time const timeout = boost::get_system_time() + boost::posix_time::milliseconds(50);
|
|
|
|
m_CV[tid].timed_wait(lock, timeout);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(void) send(m_EventFDs[tid][1], "T", 1, 0);
|
|
|
|
}
|
2016-02-04 16:01:19 +01:00
|
|
|
}
|
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
void SocketEvents::InitializeEngine(void)
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
2016-04-18 14:25:52 +02:00
|
|
|
String eventEngine = ScriptGlobal::Get("EventEngine", &Empty);
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
if (eventEngine.IsEmpty())
|
2016-02-04 16:01:19 +01:00
|
|
|
#ifdef __linux__
|
2016-04-18 14:25:52 +02:00
|
|
|
eventEngine = "epoll";
|
|
|
|
#else /* __linux__ */
|
|
|
|
eventEngine = "poll";
|
2016-02-04 16:01:19 +01:00
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
if (eventEngine == "poll")
|
|
|
|
l_SocketIOEngine = new SocketEventEnginePoll();
|
2016-02-04 16:01:19 +01:00
|
|
|
#ifdef __linux__
|
2016-04-18 14:25:52 +02:00
|
|
|
else if (eventEngine == "epoll")
|
|
|
|
l_SocketIOEngine = new SocketEventEngineEpoll();
|
2016-02-04 16:01:19 +01:00
|
|
|
#endif /* __linux__ */
|
2016-04-18 14:25:52 +02:00
|
|
|
else {
|
|
|
|
Log(LogWarning, "SocketEvents")
|
|
|
|
<< "Invalid event engine selected: " << eventEngine << " - Falling back to 'poll'";
|
2015-02-20 14:43:13 +01:00
|
|
|
|
2016-04-18 15:02:02 +02:00
|
|
|
eventEngine = "poll";
|
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
l_SocketIOEngine = new SocketEventEnginePoll();
|
|
|
|
}
|
2015-02-20 14:43:13 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
l_SocketIOEngine->Start();
|
2015-02-20 14:43:13 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
ScriptGlobal::Set("EventEngine", eventEngine);
|
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-04-18 14:25:52 +02:00
|
|
|
: m_ID(m_NextID++), m_FD(socket->GetFD()), m_EnginePrivate(NULL)
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
2016-04-18 14:25:52 +02:00
|
|
|
boost::call_once(l_SocketIOOnceFlag, &SocketEvents::InitializeEngine);
|
2015-02-13 21:02:48 +01:00
|
|
|
|
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-04-18 14:25:52 +02:00
|
|
|
l_SocketIOEngine->Register(this, lifesupportObject);
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketEvents::Unregister(void)
|
|
|
|
{
|
2016-04-18 14:25:52 +02:00
|
|
|
l_SocketIOEngine->Unregister(this);
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketEvents::ChangeEvents(int events)
|
|
|
|
{
|
2016-04-18 14:25:52 +02:00
|
|
|
l_SocketIOEngine->ChangeEvents(this, events);
|
|
|
|
}
|
2015-02-13 21:02:48 +01:00
|
|
|
|
2016-04-18 14:25:52 +02:00
|
|
|
boost::mutex& SocketEventEngine::GetMutex(int tid)
|
|
|
|
{
|
|
|
|
return m_EventMutex[tid];
|
2015-02-13 21:02:48 +01:00
|
|
|
}
|
|
|
|
|
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-04-18 14:25:52 +02:00
|
|
|
boost::mutex::scoped_lock lock(l_SocketIOEngine->GetMutex(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)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|