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)
25 this->RecoverFromFork();
26 ke_list = new struct kevent[GetMaxFds()];
27 ref = new EventHandler* [GetMaxFds()];
28 memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
31 void KQueueEngine::RecoverFromFork()
34 * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
38 EngineHandle = kqueue();
39 if (EngineHandle == -1)
41 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
42 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: this is a fatal error, exiting now.");
43 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.\n");
44 printf("ERROR: this is a fatal error, exiting now.\n");
45 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
50 KQueueEngine::~KQueueEngine()
52 this->Close(EngineHandle);
57 bool KQueueEngine::AddFd(EventHandler* eh)
61 if ((fd < 0) || (fd > GetMaxFds() - 1))
64 if (GetRemainingFds() <= 1)
71 EV_SET(&ke, fd, eh->Readable() ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
73 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
82 ServerInstance->Logs->Log("SOCKET",DEBUG,"New file descriptor: %d", fd);
86 bool KQueueEngine::DelFd(EventHandler* eh, bool force)
90 if ((fd < 0) || (fd > GetMaxFds() - 1))
94 EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
96 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
98 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
100 int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
102 if ((j < 0) && (i < 0) && !force)
108 //ServerInstance->Logs->Log("SOCKET",DEBUG,"Remove file descriptor: %d", fd);
112 void KQueueEngine::WantWrite(EventHandler* eh)
114 /** When changing an item in a kqueue, there is no 'modify' call
115 * as in epoll. Instead, we add the item again, and this overwrites
116 * the original setting rather than adding it twice. See man kqueue.
119 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
120 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
123 int KQueueEngine::GetMaxFds()
125 if (!MAX_DESCRIPTORS)
127 int mib[2], maxfiles;
131 mib[1] = KERN_MAXFILES;
132 len = sizeof(maxfiles);
133 sysctl(mib, 2, &maxfiles, &len, NULL, 0);
134 MAX_DESCRIPTORS = maxfiles;
137 return MAX_DESCRIPTORS;
140 int KQueueEngine::GetRemainingFds()
142 return GetMaxFds() - CurrentSetSize;
145 int KQueueEngine::DispatchEvents()
150 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], GetMaxFds(), &ts);
154 for (int j = 0; j < i; j++)
156 if (ke_list[j].flags & EV_EOF)
158 /* We love you kqueue, oh yes we do *sings*!
159 * kqueue gives us the error number directly in the EOF state!
160 * Unlike smelly epoll and select, where we have to getsockopt
161 * to get the error, this saves us time and cpu cycles. Go BSD!
164 if (ref[ke_list[j].ident])
165 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
168 if (ke_list[j].flags & EVFILT_WRITE)
170 /* This looks wrong but its right. As above, theres no modify
171 * call in kqueue. See the manpage.
173 if (ref[ke_list[j].ident]->Readable())
176 EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
177 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
180 if (ref[ke_list[j].ident])
181 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
186 if (ref[ke_list[j].ident])
187 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
194 std::string KQueueEngine::GetName()