]> granicus.if.org Git - icinga2/blob - lib/base/socketevents.cpp
lib->compat->statusdatawriter: fix notifications_enabled
[icinga2] / lib / base / socketevents.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "base/socketevents.hpp"
21 #include "base/exception.hpp"
22 #include "base/logger.hpp"
23 #include "base/application.hpp"
24 #include "base/scriptglobal.hpp"
25 #include <boost/thread/once.hpp>
26 #include <map>
27 #ifdef __linux__
28 #       include <sys/epoll.h>
29 #endif /* __linux__ */
30
31 using namespace icinga;
32
33 static boost::once_flag l_SocketIOOnceFlag = BOOST_ONCE_INIT;
34 static SocketEventEngine *l_SocketIOEngine;
35
36 int SocketEvents::m_NextID = 0;
37
38 void SocketEventEngine::Start()
39 {
40         for (int tid = 0; tid < SOCKET_IOTHREADS; tid++) {
41                 Socket::SocketPair(m_EventFDs[tid]);
42
43                 Utility::SetNonBlockingSocket(m_EventFDs[tid][0]);
44                 Utility::SetNonBlockingSocket(m_EventFDs[tid][1]);
45
46 #ifndef _WIN32
47                 Utility::SetCloExec(m_EventFDs[tid][0]);
48                 Utility::SetCloExec(m_EventFDs[tid][1]);
49 #endif /* _WIN32 */
50
51                 InitializeThread(tid);
52
53                 m_Threads[tid] = std::thread(std::bind(&SocketEventEngine::ThreadProc, this, tid));
54         }
55 }
56
57 void SocketEventEngine::WakeUpThread(int sid, bool wait)
58 {
59         int tid = sid % SOCKET_IOTHREADS;
60
61         if (std::this_thread::get_id() == m_Threads[tid].get_id())
62                 return;
63
64         if (wait) {
65                 boost::mutex::scoped_lock lock(m_EventMutex[tid]);
66
67                 m_FDChanged[tid] = true;
68
69                 while (m_FDChanged[tid]) {
70                         (void) send(m_EventFDs[tid][1], "T", 1, 0);
71
72                         boost::system_time const timeout = boost::get_system_time() + boost::posix_time::milliseconds(50);
73                         m_CV[tid].timed_wait(lock, timeout);
74                 }
75         } else {
76                 (void) send(m_EventFDs[tid][1], "T", 1, 0);
77         }
78 }
79
80 void SocketEvents::InitializeEngine()
81 {
82         String eventEngine = ScriptGlobal::Get("EventEngine", &Empty);
83
84         if (eventEngine.IsEmpty())
85 #ifdef __linux__
86                 eventEngine = "epoll";
87 #else /* __linux__ */
88                 eventEngine = "poll";
89 #endif /* __linux__ */
90
91         if (eventEngine == "poll")
92                 l_SocketIOEngine = new SocketEventEnginePoll();
93 #ifdef __linux__
94         else if (eventEngine == "epoll")
95                 l_SocketIOEngine = new SocketEventEngineEpoll();
96 #endif /* __linux__ */
97         else {
98                 Log(LogWarning, "SocketEvents")
99                         << "Invalid event engine selected: " << eventEngine << " - Falling back to 'poll'";
100
101                 eventEngine = "poll";
102
103                 l_SocketIOEngine = new SocketEventEnginePoll();
104         }
105
106         l_SocketIOEngine->Start();
107
108         ScriptGlobal::Set("EventEngine", eventEngine);
109 }
110
111 /**
112  * Constructor for the SocketEvents class.
113  */
114 SocketEvents::SocketEvents(const Socket::Ptr& socket, Object *lifesupportObject)
115         : m_ID(m_NextID++), m_FD(socket->GetFD()), m_EnginePrivate(nullptr)
116 {
117         boost::call_once(l_SocketIOOnceFlag, &SocketEvents::InitializeEngine);
118
119         Register(lifesupportObject);
120 }
121
122 SocketEvents::~SocketEvents()
123 {
124         VERIFY(m_FD == INVALID_SOCKET);
125 }
126
127 void SocketEvents::Register(Object *lifesupportObject)
128 {
129         l_SocketIOEngine->Register(this, lifesupportObject);
130 }
131
132 void SocketEvents::Unregister()
133 {
134         l_SocketIOEngine->Unregister(this);
135 }
136
137 void SocketEvents::ChangeEvents(int events)
138 {
139         l_SocketIOEngine->ChangeEvents(this, events);
140 }
141
142 boost::mutex& SocketEventEngine::GetMutex(int tid)
143 {
144         return m_EventMutex[tid];
145 }
146
147 bool SocketEvents::IsHandlingEvents() const
148 {
149         int tid = m_ID % SOCKET_IOTHREADS;
150         boost::mutex::scoped_lock lock(l_SocketIOEngine->GetMutex(tid));
151         return m_Events;
152 }
153
154 void SocketEvents::OnEvent(int revents)
155 {
156
157 }
158