2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Adam <Adam@anope.org>
5 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6 * Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
7 * Copyright (C) 2009 Craig Edwards <craigedwards@brainbox.cc>
8 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #ifndef SOCKETENGINE_POLL
25 #define SOCKETENGINE_POLL
31 #include "exitcodes.h"
33 #include "socketengine.h"
37 # define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/
40 # include <sys/poll.h>
41 # include <sys/resource.h>
43 # define struct pollfd WSAPOLLFD
49 /** A specialisation of the SocketEngine class, designed to use poll().
51 class PollEngine : public SocketEngine
54 /** These are used by poll() to hold socket events
56 std::vector<struct pollfd> events;
57 /** This vector maps fds to an index in the events array.
59 std::vector<int> fd_mappings;
61 /** Create a new PollEngine
64 virtual bool AddFd(EventHandler* eh, int event_mask);
65 virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
66 virtual void DelFd(EventHandler* eh);
67 virtual int DispatchEvents();
68 virtual std::string GetName();
73 PollEngine::PollEngine() : events(1), fd_mappings(1)
77 if (!getrlimit(RLIMIT_NOFILE, &limits))
79 MAX_DESCRIPTORS = limits.rlim_cur;
83 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets: %s", strerror(errno));
84 std::cout << "ERROR: Can't determine maximum number of open sockets: " << strerror(errno) << std::endl;
85 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
89 static int mask_to_poll(int event_mask)
92 if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
94 if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
99 bool PollEngine::AddFd(EventHandler* eh, int event_mask)
101 int fd = eh->GetFd();
102 if ((fd < 0) || (fd > GetMaxFds() - 1))
104 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
108 if (static_cast<unsigned int>(fd) < fd_mappings.size() && fd_mappings[fd] != -1)
110 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Attempt to add duplicate fd: %d", fd);
114 if (!SocketEngine::AddFd(eh))
116 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Attempt to add duplicate fd: %d", fd);
120 unsigned int index = CurrentSetSize;
122 while (static_cast<unsigned int>(fd) >= fd_mappings.size())
123 fd_mappings.resize(fd_mappings.size() * 2, -1);
124 fd_mappings[fd] = index;
126 ResizeDouble(events);
127 events[index].fd = fd;
128 events[index].events = mask_to_poll(event_mask);
130 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d (%d; index %d)", fd, events[index].events, index);
131 SocketEngine::SetEventMask(eh, event_mask);
136 void PollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
138 int fd = eh->GetFd();
139 if (fd < 0 || static_cast<unsigned int>(fd) >= fd_mappings.size() || fd_mappings[fd] == -1)
141 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "SetEvents() on unknown fd: %d", eh->GetFd());
145 events[fd_mappings[fd]].events = mask_to_poll(new_mask);
148 void PollEngine::DelFd(EventHandler* eh)
150 int fd = eh->GetFd();
151 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
153 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
157 if (static_cast<unsigned int>(fd) >= fd_mappings.size() || fd_mappings[fd] == -1)
159 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "DelFd() on unknown fd: %d", fd);
163 unsigned int index = fd_mappings[fd];
164 unsigned int last_index = CurrentSetSize - 1;
165 int last_fd = events[last_index].fd;
167 if (index != last_index)
169 // We need to move the last fd we got into this gap (gaps are evil!)
171 // So update the mapping for the last fd to its new position
172 fd_mappings[last_fd] = index;
174 // move last_fd from last_index into index
175 events[index].fd = last_fd;
176 events[index].events = events[last_index].events;
179 // Now remove all data for the last fd we got into out list.
180 // Above code made sure this always is right
181 fd_mappings[fd] = -1;
182 events[last_index].fd = 0;
183 events[last_index].events = 0;
185 SocketEngine::DelFd(eh);
189 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d (index: %d) "
190 "(Filled gap with: %d (index: %d))", fd, index, last_fd, last_index);
193 int PollEngine::DispatchEvents()
195 int i = poll(&events[0], CurrentSetSize, 1000);
197 socklen_t codesize = sizeof(int);
200 ServerInstance->UpdateTime();
202 for (index = 0; index < CurrentSetSize && processed < i; index++)
204 struct pollfd& pfd = events[index];
209 EventHandler* eh = GetRef(pfd.fd);
213 if (pfd.revents & POLLHUP)
215 eh->HandleEvent(EVENT_ERROR, 0);
219 if (pfd.revents & POLLERR)
222 if (getsockopt(pfd.fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
224 eh->HandleEvent(EVENT_ERROR, errcode);
228 if (pfd.revents & POLLIN)
230 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
231 eh->HandleEvent(EVENT_READ);
232 if (eh != GetRef(pfd.fd))
233 // whoops, deleted out from under us
237 if (pfd.revents & POLLOUT)
239 int mask = eh->GetEventMask();
240 mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE);
241 SetEventMask(eh, mask);
242 pfd.events = mask_to_poll(mask);
243 eh->HandleEvent(EVENT_WRITE);
250 std::string PollEngine::GetName()
255 SocketEngine* CreateSocketEngine()
257 return new PollEngine;