1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "exitcodes.h"
16 #include <sys/types.h>
17 #include <sys/event.h>
19 #include "socketengines/socketengine_kqueue.h"
20 #include <sys/sysctl.h>
22 KQueueEngine::KQueueEngine(InspIRCd* Instance) : SocketEngine(Instance)
24 this->RecoverFromFork();
25 ke_list = new struct kevent[GetMaxFds()];
26 ref = new EventHandler* [GetMaxFds()];
27 memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
30 void KQueueEngine::RecoverFromFork()
33 * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
37 EngineHandle = kqueue();
38 if (EngineHandle == -1)
40 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
41 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: this is a fatal error, exiting now.");
42 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.\n");
43 printf("ERROR: this is a fatal error, exiting now.\n");
44 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
49 KQueueEngine::~KQueueEngine()
51 this->Close(EngineHandle);
56 bool KQueueEngine::AddFd(EventHandler* eh)
60 if ((fd < 0) || (fd > GetMaxFds() - 1))
63 if (GetRemainingFds() <= 1)
70 EV_SET(&ke, fd, eh->Readable() ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
72 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
81 ServerInstance->Logs->Log("SOCKET",DEBUG,"New file descriptor: %d", fd);
85 bool KQueueEngine::DelFd(EventHandler* eh, bool force)
89 if ((fd < 0) || (fd > GetMaxFds() - 1))
93 EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
95 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
97 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
99 int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
101 if ((j < 0) && (i < 0) && !force)
107 //ServerInstance->Logs->Log("SOCKET",DEBUG,"Remove file descriptor: %d", fd);
111 void KQueueEngine::WantWrite(EventHandler* eh)
113 /** When changing an item in a kqueue, there is no 'modify' call
114 * as in epoll. Instead, we add the item again, and this overwrites
115 * the original setting rather than adding it twice. See man kqueue.
118 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
119 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
122 int KQueueEngine::GetMaxFds()
124 if (!MAX_DESCRIPTORS)
126 int mib[2], maxfiles;
130 mib[1] = KERN_MAXFILES;
131 len = sizeof(maxfiles);
132 sysctl(mib, 2, &maxfiles, &len, NULL, 0);
133 MAX_DESCRIPTORS = maxfiles;
136 return MAX_DESCRIPTORS;
139 int KQueueEngine::GetRemainingFds()
141 return GetMaxFds() - CurrentSetSize;
144 int KQueueEngine::DispatchEvents()
149 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], GetMaxFds(), &ts);
153 for (int j = 0; j < i; j++)
155 if (ke_list[j].flags & EV_EOF)
157 /* We love you kqueue, oh yes we do *sings*!
158 * kqueue gives us the error number directly in the EOF state!
159 * Unlike smelly epoll and select, where we have to getsockopt
160 * to get the error, this saves us time and cpu cycles. Go BSD!
163 if (ref[ke_list[j].ident])
164 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
167 if (ke_list[j].flags & EVFILT_WRITE)
169 /* This looks wrong but its right. As above, theres no modify
170 * call in kqueue. See the manpage.
172 if (ref[ke_list[j].ident]->Readable())
175 EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
176 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
179 if (ref[ke_list[j].ident])
180 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
185 if (ref[ke_list[j].ident])
186 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
193 std::string KQueueEngine::GetName()