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;
40 /** This is a specialised time value used by kqueue()
44 /** Create a new KQueueEngine
47 /** Delete a KQueueEngine
49 virtual ~KQueueEngine();
50 bool AddFd(EventHandler* eh, int event_mask);
51 void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
52 virtual void DelFd(EventHandler* eh);
53 virtual int DispatchEvents();
54 virtual std::string GetName();
55 virtual void RecoverFromFork();
58 KQueueEngine::KQueueEngine() : ke_list(1)
65 #ifdef KERN_MAXFILESPERPROC
66 mib[1] = KERN_MAXFILESPERPROC;
68 mib[1] = KERN_MAXFILES;
70 len = sizeof(MAX_DESCRIPTORS);
71 sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
72 if (MAX_DESCRIPTORS <= 0)
74 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets!");
75 std::cout << "ERROR: Can't determine maximum number of open sockets!" << std::endl;
76 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
79 this->RecoverFromFork();
82 void KQueueEngine::RecoverFromFork()
85 * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
89 EngineHandle = kqueue();
90 if (EngineHandle == -1)
92 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
93 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: this is a fatal error, exiting now.");
94 std::cout << "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features." << std::endl;
95 std::cout << "ERROR: this is a fatal error, exiting now." << std::endl;
96 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
101 KQueueEngine::~KQueueEngine()
103 this->Close(EngineHandle);
106 bool KQueueEngine::AddFd(EventHandler* eh, int event_mask)
108 int fd = eh->GetFd();
110 if ((fd < 0) || (fd > GetMaxFds() - 1))
113 if (!SocketEngine::AddFd(eh))
116 // We always want to read from the socket...
118 EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
120 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
123 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to add fd: %d %s",
124 fd, strerror(errno));
128 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
130 SocketEngine::SetEventMask(eh, event_mask);
131 OnSetEvent(eh, 0, event_mask);
133 ResizeDouble(ke_list);
138 void KQueueEngine::DelFd(EventHandler* eh)
140 int fd = eh->GetFd();
142 if ((fd < 0) || (fd > GetMaxFds() - 1))
144 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "DelFd() on invalid fd: %d", fd);
150 // First remove the write filter ignoring errors, since we can't be
151 // sure if there are actually any write filters registered.
152 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
153 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
155 // Then remove the read filter.
156 EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
157 int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
161 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to remove fd: %d %s",
162 fd, strerror(errno));
165 SocketEngine::DelFd(eh);
168 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
171 void KQueueEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
173 if ((new_mask & FD_WANT_POLL_WRITE) && !(old_mask & FD_WANT_POLL_WRITE))
175 // new poll-style write
177 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, NULL);
178 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
180 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
181 eh->GetFd(), strerror(errno));
183 else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE))
185 // removing poll-style write
187 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
188 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
190 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
191 eh->GetFd(), strerror(errno));
193 if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)))
195 // new one-shot write
197 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
198 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
200 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
201 eh->GetFd(), strerror(errno));
205 int KQueueEngine::DispatchEvents()
210 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], ke_list.size(), &ts);
211 ServerInstance->UpdateTime();
218 for (int j = 0; j < i; j++)
220 struct kevent& kev = ke_list[j];
222 // Copy these in case the vector gets resized and kev invalidated
223 const int fd = kev.ident;
224 const short filter = kev.filter;
226 EventHandler* eh = GetRef(fd);
230 if (kev.flags & EV_EOF)
233 eh->HandleEvent(EVENT_ERROR, kev.fflags);
236 if (filter == EVFILT_WRITE)
239 /* When mask is FD_WANT_FAST_WRITE or FD_WANT_SINGLE_WRITE,
240 * we set a one-shot write, so we need to clear that bit
241 * to detect when it set again.
243 const int bits_to_clr = FD_WANT_SINGLE_WRITE | FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK;
244 SetEventMask(eh, eh->GetEventMask() & ~bits_to_clr);
245 eh->HandleEvent(EVENT_WRITE);
247 else if (filter == EVFILT_READ)
250 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
251 eh->HandleEvent(EVENT_READ);
258 std::string KQueueEngine::GetName()
263 SocketEngine* CreateSocketEngine()
265 return new KQueueEngine;