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"
30 /** A specialisation of the SocketEngine class, designed to use FreeBSD kqueue().
32 class KQueueEngine : public SocketEngine
36 /** These are used by kqueue() to hold socket events
38 struct kevent* ke_list;
39 /** This is a specialised time value used by kqueue()
43 /** Create a new KQueueEngine
46 /** Delete a KQueueEngine
48 virtual ~KQueueEngine();
49 bool AddFd(EventHandler* eh, int event_mask);
50 void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
51 virtual void DelFd(EventHandler* eh);
52 virtual int DispatchEvents();
53 virtual std::string GetName();
54 virtual void RecoverFromFork();
57 #include <sys/sysctl.h>
59 KQueueEngine::KQueueEngine()
66 mib[1] = KERN_MAXFILESPERPROC;
67 len = sizeof(MAX_DESCRIPTORS);
68 sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
69 if (MAX_DESCRIPTORS <= 0)
71 ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets!");
72 std::cout << "ERROR: Can't determine maximum number of open sockets!" << std::endl;
73 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
76 this->RecoverFromFork();
77 ke_list = new struct kevent[GetMaxFds()];
78 ref = new EventHandler* [GetMaxFds()];
79 memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
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",DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
93 ServerInstance->Logs->Log("SOCKET",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->Exit(EXIT_STATUS_SOCKETENGINE);
101 KQueueEngine::~KQueueEngine()
103 this->Close(EngineHandle);
108 bool KQueueEngine::AddFd(EventHandler* eh, int event_mask)
110 int fd = eh->GetFd();
112 if ((fd < 0) || (fd > GetMaxFds() - 1))
118 // We always want to read from the socket...
120 EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
122 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
125 ServerInstance->Logs->Log("SOCKET",DEFAULT,"Failed to add fd: %d %s",
126 fd, strerror(errno));
131 SocketEngine::SetEventMask(eh, event_mask);
132 OnSetEvent(eh, 0, event_mask);
135 ServerInstance->Logs->Log("SOCKET",DEBUG,"New file descriptor: %d", fd);
139 void KQueueEngine::DelFd(EventHandler* eh)
141 int fd = eh->GetFd();
143 if ((fd < 0) || (fd > GetMaxFds() - 1))
145 ServerInstance->Logs->Log("SOCKET",DEFAULT,"DelFd() on invalid fd: %d", fd);
151 // First remove the write filter ignoring errors, since we can't be
152 // sure if there are actually any write filters registered.
153 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
154 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
156 // Then remove the read filter.
157 EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
158 int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
162 ServerInstance->Logs->Log("SOCKET",DEFAULT,"Failed to remove fd: %d %s",
163 fd, strerror(errno));
169 ServerInstance->Logs->Log("SOCKET",DEBUG,"Remove file descriptor: %d", fd);
172 void KQueueEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
174 if ((new_mask & FD_WANT_POLL_WRITE) && !(old_mask & FD_WANT_POLL_WRITE))
176 // new poll-style write
178 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, NULL);
179 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
181 ServerInstance->Logs->Log("SOCKET",DEFAULT,"Failed to mark for writing: %d %s",
182 eh->GetFd(), strerror(errno));
185 else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE))
187 // removing poll-style write
189 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
190 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
192 ServerInstance->Logs->Log("SOCKET",DEFAULT,"Failed to mark for writing: %d %s",
193 eh->GetFd(), strerror(errno));
196 if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)))
198 // new one-shot write
200 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
201 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
203 ServerInstance->Logs->Log("SOCKET",DEFAULT,"Failed to mark for writing: %d %s",
204 eh->GetFd(), strerror(errno));
209 int KQueueEngine::DispatchEvents()
214 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], GetMaxFds(), &ts);
215 ServerInstance->UpdateTime();
219 for (int j = 0; j < i; j++)
221 EventHandler* eh = ref[ke_list[j].ident];
224 if (ke_list[j].flags & EV_EOF)
227 eh->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
230 if (ke_list[j].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 if (eh != ref[ke_list[j].ident])
242 // whoops, deleted out from under us
245 if (ke_list[j].filter == EVFILT_READ)
248 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
249 eh->HandleEvent(EVENT_READ);
256 std::string KQueueEngine::GetName()
261 SocketEngine* CreateSocketEngine()
263 return new KQueueEngine;