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/>.
28 #include "exitcodes.h"
30 #include "socketengine.h"
34 # define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/
37 # include <sys/poll.h>
38 # include <sys/resource.h>
40 # define struct pollfd WSAPOLLFD
44 /** A specialisation of the SocketEngine class, designed to use poll().
48 /** These are used by poll() to hold socket events
50 std::vector<struct pollfd> events(16);
51 /** This vector maps fds to an index in the events array.
53 std::vector<int> fd_mappings(16);
56 void SocketEngine::Init()
59 if (!getrlimit(RLIMIT_NOFILE, &limits))
61 MAX_DESCRIPTORS = limits.rlim_cur;
65 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets: %s", strerror(errno));
66 std::cout << "ERROR: Can't determine maximum number of open sockets: " << strerror(errno) << std::endl;
67 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
71 void SocketEngine::Deinit()
75 void SocketEngine::RecoverFromFork()
79 static int mask_to_poll(int event_mask)
82 if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
84 if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
89 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
92 if ((fd < 0) || (fd > GetMaxFds() - 1))
94 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
98 if (static_cast<unsigned int>(fd) < fd_mappings.size() && fd_mappings[fd] != -1)
100 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Attempt to add duplicate fd: %d", fd);
104 unsigned int index = CurrentSetSize;
106 if (!SocketEngine::AddFdRef(eh))
108 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Attempt to add duplicate fd: %d", fd);
112 while (static_cast<unsigned int>(fd) >= fd_mappings.size())
113 fd_mappings.resize(fd_mappings.size() * 2, -1);
114 fd_mappings[fd] = index;
116 ResizeDouble(events);
117 events[index].fd = fd;
118 events[index].events = mask_to_poll(event_mask);
120 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d (%d; index %d)", fd, events[index].events, index);
121 eh->SetEventMask(event_mask);
125 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
127 int fd = eh->GetFd();
128 if (fd < 0 || static_cast<unsigned int>(fd) >= fd_mappings.size() || fd_mappings[fd] == -1)
130 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "SetEvents() on unknown fd: %d", eh->GetFd());
134 events[fd_mappings[fd]].events = mask_to_poll(new_mask);
137 void SocketEngine::DelFd(EventHandler* eh)
139 int fd = eh->GetFd();
140 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
142 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
146 if (static_cast<unsigned int>(fd) >= fd_mappings.size() || fd_mappings[fd] == -1)
148 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "DelFd() on unknown fd: %d", fd);
152 unsigned int index = fd_mappings[fd];
153 unsigned int last_index = CurrentSetSize - 1;
154 int last_fd = events[last_index].fd;
156 if (index != last_index)
158 // We need to move the last fd we got into this gap (gaps are evil!)
160 // So update the mapping for the last fd to its new position
161 fd_mappings[last_fd] = index;
163 // move last_fd from last_index into index
164 events[index].fd = last_fd;
165 events[index].events = events[last_index].events;
168 // Now remove all data for the last fd we got into out list.
169 // Above code made sure this always is right
170 fd_mappings[fd] = -1;
171 events[last_index].fd = 0;
172 events[last_index].events = 0;
174 SocketEngine::DelFdRef(eh);
176 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d (index: %d) "
177 "(Filled gap with: %d (index: %d))", fd, index, last_fd, last_index);
180 int SocketEngine::DispatchEvents()
182 int i = poll(&events[0], CurrentSetSize, 1000);
184 ServerInstance->UpdateTime();
186 for (int index = 0; index < CurrentSetSize && processed < i; index++)
188 struct pollfd& pfd = events[index];
190 // Copy these in case the vector gets resized and pfd invalidated
191 const int fd = pfd.fd;
192 const short revents = pfd.revents;
197 EventHandler* eh = GetRef(fd);
201 if (revents & POLLHUP)
203 eh->HandleEvent(EVENT_ERROR, 0);
207 if (revents & POLLERR)
210 socklen_t codesize = sizeof(int);
212 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
214 eh->HandleEvent(EVENT_ERROR, errcode);
218 if (revents & POLLIN)
220 eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
221 eh->HandleEvent(EVENT_READ);
222 if (eh != GetRef(fd))
223 // whoops, deleted out from under us
227 if (revents & POLLOUT)
229 int mask = eh->GetEventMask();
230 mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE);
231 eh->SetEventMask(mask);
233 // The vector could've been resized, reference can be invalid by now; don't use it
234 events[index].events = mask_to_poll(mask);
235 eh->HandleEvent(EVENT_WRITE);