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"
33 #include <sys/resource.h>
35 /** A specialisation of the SocketEngine class, designed to use poll().
39 /** These are used by poll() to hold socket events
41 std::vector<struct pollfd> events(16);
42 /** This vector maps fds to an index in the events array.
44 std::vector<int> fd_mappings(16);
47 void SocketEngine::Init()
50 if (!getrlimit(RLIMIT_NOFILE, &limits))
52 MAX_DESCRIPTORS = limits.rlim_cur;
56 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets: %s", strerror(errno));
57 std::cout << "ERROR: Can't determine maximum number of open sockets: " << strerror(errno) << std::endl;
58 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
62 void SocketEngine::Deinit()
66 void SocketEngine::RecoverFromFork()
70 static int mask_to_poll(int event_mask)
73 if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
75 if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
80 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
83 if ((fd < 0) || (fd > GetMaxFds() - 1))
85 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
89 if (static_cast<unsigned int>(fd) < fd_mappings.size() && fd_mappings[fd] != -1)
91 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Attempt to add duplicate fd: %d", fd);
95 unsigned int index = CurrentSetSize;
97 if (!SocketEngine::AddFdRef(eh))
99 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Attempt to add duplicate fd: %d", fd);
103 while (static_cast<unsigned int>(fd) >= fd_mappings.size())
104 fd_mappings.resize(fd_mappings.size() * 2, -1);
105 fd_mappings[fd] = index;
107 ResizeDouble(events);
108 events[index].fd = fd;
109 events[index].events = mask_to_poll(event_mask);
111 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d (%d; index %d)", fd, events[index].events, index);
112 eh->SetEventMask(event_mask);
116 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
118 int fd = eh->GetFd();
119 if (fd < 0 || static_cast<unsigned int>(fd) >= fd_mappings.size() || fd_mappings[fd] == -1)
121 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "SetEvents() on unknown fd: %d", eh->GetFd());
125 events[fd_mappings[fd]].events = mask_to_poll(new_mask);
128 void SocketEngine::DelFd(EventHandler* eh)
130 int fd = eh->GetFd();
131 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
133 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
137 if (static_cast<unsigned int>(fd) >= fd_mappings.size() || fd_mappings[fd] == -1)
139 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "DelFd() on unknown fd: %d", fd);
143 unsigned int index = fd_mappings[fd];
144 unsigned int last_index = CurrentSetSize - 1;
145 int last_fd = events[last_index].fd;
147 if (index != last_index)
149 // We need to move the last fd we got into this gap (gaps are evil!)
151 // So update the mapping for the last fd to its new position
152 fd_mappings[last_fd] = index;
154 // move last_fd from last_index into index
155 events[index].fd = last_fd;
156 events[index].events = events[last_index].events;
159 // Now remove all data for the last fd we got into out list.
160 // Above code made sure this always is right
161 fd_mappings[fd] = -1;
162 events[last_index].fd = 0;
163 events[last_index].events = 0;
165 SocketEngine::DelFdRef(eh);
167 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d (index: %d) "
168 "(Filled gap with: %d (index: %d))", fd, index, last_fd, last_index);
171 int SocketEngine::DispatchEvents()
173 int i = poll(&events[0], CurrentSetSize, 1000);
175 ServerInstance->UpdateTime();
177 for (int index = 0; index < CurrentSetSize && processed < i; index++)
179 struct pollfd& pfd = events[index];
181 // Copy these in case the vector gets resized and pfd invalidated
182 const int fd = pfd.fd;
183 const short revents = pfd.revents;
188 EventHandler* eh = GetRef(fd);
192 if (revents & POLLHUP)
194 eh->HandleEvent(EVENT_ERROR, 0);
198 if (revents & POLLERR)
201 socklen_t codesize = sizeof(int);
203 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
205 eh->HandleEvent(EVENT_ERROR, errcode);
209 if (revents & POLLIN)
211 eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
212 eh->HandleEvent(EVENT_READ);
213 if (eh != GetRef(fd))
214 // whoops, deleted out from under us
218 if (revents & POLLOUT)
220 int mask = eh->GetEventMask();
221 mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE);
222 eh->SetEventMask(mask);
224 // The vector could've been resized, reference can be invalid by now; don't use it
225 events[index].events = mask_to_poll(mask);
226 eh->HandleEvent(EVENT_WRITE);