2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
6 * Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "exitcodes.h"
24 #include <sys/types.h>
25 #include <sys/event.h>
27 #include "socketengine.h"
29 #include <sys/sysctl.h>
31 /** A specialisation of the SocketEngine class, designed to use BSD kqueue().
33 class KQueueEngine : public SocketEngine
37 /** These are used by kqueue() to hold socket events
39 std::vector<struct kevent> ke_list;
41 /** Create a new KQueueEngine
44 /** Delete a KQueueEngine
46 virtual ~KQueueEngine();
47 bool AddFd(EventHandler* eh, int event_mask);
48 void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
49 virtual void DelFd(EventHandler* eh);
50 virtual int DispatchEvents();
51 virtual void RecoverFromFork();
54 KQueueEngine::KQueueEngine() : ke_list(1)
61 #ifdef KERN_MAXFILESPERPROC
62 mib[1] = KERN_MAXFILESPERPROC;
64 mib[1] = KERN_MAXFILES;
66 len = sizeof(MAX_DESCRIPTORS);
67 sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
68 if (MAX_DESCRIPTORS <= 0)
70 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets!");
71 std::cout << "ERROR: Can't determine maximum number of open sockets!" << std::endl;
72 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
75 this->RecoverFromFork();
78 void KQueueEngine::RecoverFromFork()
81 * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
85 EngineHandle = kqueue();
86 if (EngineHandle == -1)
88 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
89 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: this is a fatal error, exiting now.");
90 std::cout << "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features." << std::endl;
91 std::cout << "ERROR: this is a fatal error, exiting now." << std::endl;
92 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
96 KQueueEngine::~KQueueEngine()
98 this->Close(EngineHandle);
101 bool KQueueEngine::AddFd(EventHandler* eh, int event_mask)
103 int fd = eh->GetFd();
105 if ((fd < 0) || (fd > GetMaxFds() - 1))
108 if (!SocketEngine::AddFdRef(eh))
111 // We always want to read from the socket...
113 EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
115 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
118 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to add fd: %d %s",
119 fd, strerror(errno));
123 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
125 SocketEngine::SetEventMask(eh, event_mask);
126 OnSetEvent(eh, 0, event_mask);
127 ResizeDouble(ke_list);
132 void KQueueEngine::DelFd(EventHandler* eh)
134 int fd = eh->GetFd();
136 if ((fd < 0) || (fd > GetMaxFds() - 1))
138 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "DelFd() on invalid fd: %d", fd);
144 // First remove the write filter ignoring errors, since we can't be
145 // sure if there are actually any write filters registered.
146 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
147 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
149 // Then remove the read filter.
150 EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
151 int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
155 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to remove fd: %d %s",
156 fd, strerror(errno));
159 SocketEngine::DelFdRef(eh);
161 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
164 void KQueueEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
166 if ((new_mask & FD_WANT_POLL_WRITE) && !(old_mask & FD_WANT_POLL_WRITE))
168 // new poll-style write
170 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, NULL);
171 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
173 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
174 eh->GetFd(), strerror(errno));
176 else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE))
178 // removing poll-style write
180 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
181 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
183 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
184 eh->GetFd(), strerror(errno));
186 if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)))
188 // new one-shot write
190 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
191 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
193 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
194 eh->GetFd(), strerror(errno));
198 int KQueueEngine::DispatchEvents()
204 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], ke_list.size(), &ts);
205 ServerInstance->UpdateTime();
212 for (int j = 0; j < i; j++)
214 struct kevent& kev = ke_list[j];
216 // Copy these in case the vector gets resized and kev invalidated
217 const int fd = kev.ident;
218 const short filter = kev.filter;
220 EventHandler* eh = GetRef(fd);
224 if (kev.flags & EV_EOF)
227 eh->HandleEvent(EVENT_ERROR, kev.fflags);
230 if (filter == EVFILT_WRITE)
233 /* When mask is FD_WANT_FAST_WRITE or FD_WANT_SINGLE_WRITE,
234 * we set a one-shot write, so we need to clear that bit
235 * to detect when it set again.
237 const int bits_to_clr = FD_WANT_SINGLE_WRITE | FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK;
238 SetEventMask(eh, eh->GetEventMask() & ~bits_to_clr);
239 eh->HandleEvent(EVENT_WRITE);
241 else if (filter == EVFILT_READ)
244 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
245 eh->HandleEvent(EVENT_READ);
252 SocketEngine* CreateSocketEngine()
254 return new KQueueEngine;