2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
6 * Copyright (C) 2009 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "exitcodes.h"
26 #ifndef SOCKETENGINE_POLL
27 #define SOCKETENGINE_POLL
33 #include "inspircd_config.h"
35 #include "socketengine.h"
39 #define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/
45 #define struct pollfd WSAPOLLFD
51 /** A specialisation of the SocketEngine class, designed to use poll().
53 class PollEngine : public SocketEngine
56 /** These are used by poll() to hold socket events
58 struct pollfd *events;
59 /** This map maps fds to an index in the events array.
61 std::map<int, unsigned int> fd_mappings;
63 /** Create a new PollEngine
66 /** Delete a PollEngine
68 virtual ~PollEngine();
69 virtual bool AddFd(EventHandler* eh, int event_mask);
70 virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
71 virtual EventHandler* GetRef(int fd);
72 virtual void DelFd(EventHandler* eh);
73 virtual int DispatchEvents();
74 virtual std::string GetName();
81 #include <sys/sysctl.h>
84 PollEngine::PollEngine()
88 int max = ulimit(4, 0);
91 MAX_DESCRIPTORS = max;
95 ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets: %s", strerror(errno));
96 std::cout << "ERROR: Can't determine maximum number of open sockets: " << strerror(errno) << std::endl;
97 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
104 mib[1] = KERN_MAXFILES;
105 len = sizeof(MAX_DESCRIPTORS);
106 sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
109 ref = new EventHandler* [GetMaxFds()];
110 events = new struct pollfd[GetMaxFds()];
112 memset(events, 0, GetMaxFds() * sizeof(struct pollfd));
113 memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
116 PollEngine::~PollEngine()
118 // No destruction required, either.
123 static int mask_to_poll(int event_mask)
126 if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
128 if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
133 bool PollEngine::AddFd(EventHandler* eh, int event_mask)
135 int fd = eh->GetFd();
136 if ((fd < 0) || (fd > GetMaxFds() - 1))
138 ServerInstance->Logs->Log("SOCKET",DEBUG,"AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
142 if (fd_mappings.find(fd) != fd_mappings.end())
144 ServerInstance->Logs->Log("SOCKET",DEBUG,"Attempt to add duplicate fd: %d", fd);
148 unsigned int index = CurrentSetSize;
150 fd_mappings[fd] = index;
152 events[index].fd = fd;
153 events[index].events = mask_to_poll(event_mask);
155 ServerInstance->Logs->Log("SOCKET", DEBUG,"New file descriptor: %d (%d; index %d)", fd, events[fd].events, index);
156 SocketEngine::SetEventMask(eh, event_mask);
161 EventHandler* PollEngine::GetRef(int fd)
163 std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
164 if (it == fd_mappings.end())
166 return ref[it->second];
169 void PollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
171 std::map<int, unsigned int>::iterator it = fd_mappings.find(eh->GetFd());
172 if (it == fd_mappings.end())
174 ServerInstance->Logs->Log("SOCKET",DEBUG,"SetEvents() on unknown fd: %d", eh->GetFd());
178 events[it->second].events = mask_to_poll(new_mask);
181 void PollEngine::DelFd(EventHandler* eh)
183 int fd = eh->GetFd();
184 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
186 ServerInstance->Logs->Log("SOCKET", DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
190 std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
191 if (it == fd_mappings.end())
193 ServerInstance->Logs->Log("SOCKET",DEBUG,"DelFd() on unknown fd: %d", fd);
197 unsigned int index = it->second;
198 unsigned int last_index = CurrentSetSize - 1;
199 int last_fd = events[last_index].fd;
201 if (index != last_index)
203 // We need to move the last fd we got into this gap (gaps are evil!)
205 // So update the mapping for the last fd to its new position
206 fd_mappings[last_fd] = index;
208 // move last_fd from last_index into index
209 events[index].fd = last_fd;
210 events[index].events = events[last_index].events;
212 ref[index] = ref[last_index];
215 // Now remove all data for the last fd we got into out list.
216 // Above code made sure this always is right
217 fd_mappings.erase(it);
218 events[last_index].fd = 0;
219 events[last_index].events = 0;
220 ref[last_index] = NULL;
224 ServerInstance->Logs->Log("SOCKET", DEBUG, "Remove file descriptor: %d (index: %d) "
225 "(Filled gap with: %d (index: %d))", fd, index, last_fd, last_index);
228 int PollEngine::DispatchEvents()
230 int i = poll(events, CurrentSetSize, 1000);
232 socklen_t codesize = sizeof(int);
235 ServerInstance->UpdateTime();
239 for (index = 0; index < CurrentSetSize && processed != i; index++)
241 if (events[index].revents)
243 EventHandler* eh = ref[index];
247 if (events[index].revents & POLLHUP)
249 eh->HandleEvent(EVENT_ERROR, 0);
253 if (events[index].revents & POLLERR)
256 int fd = events[index].fd;
259 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
261 eh->HandleEvent(EVENT_ERROR, errcode);
265 if (events[index].revents & POLLIN)
267 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
268 eh->HandleEvent(EVENT_READ);
269 if (eh != ref[index])
270 // whoops, deleted out from under us
274 if (events[index].revents & POLLOUT)
276 int mask = eh->GetEventMask();
277 mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE);
278 SetEventMask(eh, mask);
279 events[index].events = mask_to_poll(mask);
280 eh->HandleEvent(EVENT_WRITE);
288 std::string PollEngine::GetName()
293 SocketEngine* CreateSocketEngine()
295 return new PollEngine;