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();
80 #include <sys/sysctl.h>
85 PollEngine::PollEngine()
93 #ifdef KERN_MAXFILESPERPROC
94 mib[1] = KERN_MAXFILESPERPROC;
96 mib[1] = KERN_MAXFILES;
98 len = sizeof(MAX_DESCRIPTORS);
99 sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
101 int max = ulimit(4, 0);
104 MAX_DESCRIPTORS = max;
108 ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets: %s", strerror(errno));
109 std::cout << "ERROR: Can't determine maximum number of open sockets: " << strerror(errno) << std::endl;
110 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
114 ref = new EventHandler* [GetMaxFds()];
115 events = new struct pollfd[GetMaxFds()];
117 memset(events, 0, GetMaxFds() * sizeof(struct pollfd));
118 memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
121 PollEngine::~PollEngine()
123 // No destruction required, either.
128 static int mask_to_poll(int event_mask)
131 if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
133 if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
138 bool PollEngine::AddFd(EventHandler* eh, int event_mask)
140 int fd = eh->GetFd();
141 if ((fd < 0) || (fd > GetMaxFds() - 1))
143 ServerInstance->Logs->Log("SOCKET",DEBUG,"AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
147 if (fd_mappings.find(fd) != fd_mappings.end())
149 ServerInstance->Logs->Log("SOCKET",DEBUG,"Attempt to add duplicate fd: %d", fd);
153 unsigned int index = CurrentSetSize;
155 fd_mappings[fd] = index;
157 events[index].fd = fd;
158 events[index].events = mask_to_poll(event_mask);
160 ServerInstance->Logs->Log("SOCKET", DEBUG,"New file descriptor: %d (%d; index %d)", fd, events[fd].events, index);
161 SocketEngine::SetEventMask(eh, event_mask);
166 EventHandler* PollEngine::GetRef(int fd)
168 std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
169 if (it == fd_mappings.end())
171 return ref[it->second];
174 void PollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
176 std::map<int, unsigned int>::iterator it = fd_mappings.find(eh->GetFd());
177 if (it == fd_mappings.end())
179 ServerInstance->Logs->Log("SOCKET",DEBUG,"SetEvents() on unknown fd: %d", eh->GetFd());
183 events[it->second].events = mask_to_poll(new_mask);
186 void PollEngine::DelFd(EventHandler* eh)
188 int fd = eh->GetFd();
189 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
191 ServerInstance->Logs->Log("SOCKET", DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
195 std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
196 if (it == fd_mappings.end())
198 ServerInstance->Logs->Log("SOCKET",DEBUG,"DelFd() on unknown fd: %d", fd);
202 unsigned int index = it->second;
203 unsigned int last_index = CurrentSetSize - 1;
204 int last_fd = events[last_index].fd;
206 if (index != last_index)
208 // We need to move the last fd we got into this gap (gaps are evil!)
210 // So update the mapping for the last fd to its new position
211 fd_mappings[last_fd] = index;
213 // move last_fd from last_index into index
214 events[index].fd = last_fd;
215 events[index].events = events[last_index].events;
217 ref[index] = ref[last_index];
220 // Now remove all data for the last fd we got into out list.
221 // Above code made sure this always is right
222 fd_mappings.erase(it);
223 events[last_index].fd = 0;
224 events[last_index].events = 0;
225 ref[last_index] = NULL;
229 ServerInstance->Logs->Log("SOCKET", DEBUG, "Remove file descriptor: %d (index: %d) "
230 "(Filled gap with: %d (index: %d))", fd, index, last_fd, last_index);
233 int PollEngine::DispatchEvents()
235 int i = poll(events, CurrentSetSize, 1000);
237 socklen_t codesize = sizeof(int);
240 ServerInstance->UpdateTime();
244 for (index = 0; index < CurrentSetSize && processed != i; index++)
246 if (events[index].revents)
248 EventHandler* eh = ref[index];
252 if (events[index].revents & POLLHUP)
254 eh->HandleEvent(EVENT_ERROR, 0);
258 if (events[index].revents & POLLERR)
261 int fd = events[index].fd;
264 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
266 eh->HandleEvent(EVENT_ERROR, errcode);
270 if (events[index].revents & POLLIN)
272 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
273 eh->HandleEvent(EVENT_READ);
274 if (eh != ref[index])
275 // whoops, deleted out from under us
279 if (events[index].revents & POLLOUT)
281 int mask = eh->GetEventMask();
282 mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE);
283 SetEventMask(eh, mask);
284 events[index].events = mask_to_poll(mask);
285 eh->HandleEvent(EVENT_WRITE);
293 std::string PollEngine::GetName()
298 SocketEngine* CreateSocketEngine()
300 return new PollEngine;