1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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"
22 KQueueEngine::KQueueEngine(InspIRCd* Instance) : SocketEngine(Instance)
24 this->RecoverFromFork();
27 void KQueueEngine::RecoverFromFork()
30 * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
34 EngineHandle = kqueue();
35 if (EngineHandle == -1)
37 ServerInstance->Log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
38 ServerInstance->Log(SPARSE,"ERROR: this is a fatal error, exiting now.");
39 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
40 printf("ERROR: this is a fatal error, exiting now.");
41 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
46 KQueueEngine::~KQueueEngine()
48 this->Close(EngineHandle);
51 bool KQueueEngine::AddFd(EventHandler* eh)
55 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
58 if (GetRemainingFds() <= 1)
65 EV_SET(&ke, fd, eh->Readable() ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
67 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
76 ServerInstance->Log(DEBUG,"New file descriptor: %d", fd);
80 bool KQueueEngine::DelFd(EventHandler* eh, bool force)
84 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
88 EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
90 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
92 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
94 int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
96 if ((j < 0) && (i < 0) && !force)
102 ServerInstance->Log(DEBUG,"Remove file descriptor: %d", fd);
106 void KQueueEngine::WantWrite(EventHandler* eh)
108 /** When changing an item in a kqueue, there is no 'modify' call
109 * as in epoll. Instead, we add the item again, and this overwrites
110 * the original setting rather than adding it twice. See man kqueue.
113 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
114 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
117 int KQueueEngine::GetMaxFds()
119 return MAX_DESCRIPTORS;
122 int KQueueEngine::GetRemainingFds()
124 return MAX_DESCRIPTORS - CurrentSetSize;
127 int KQueueEngine::DispatchEvents()
131 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
132 for (int j = 0; j < i; j++)
134 if (ke_list[j].flags & EV_EOF)
136 /* We love you kqueue, oh yes we do *sings*!
137 * kqueue gives us the error number directly in the EOF state!
138 * Unlike smelly epoll and select, where we have to getsockopt
139 * to get the error, this saves us time and cpu cycles. Go BSD!
141 if (ref[ke_list[j].ident])
142 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
145 if (ke_list[j].flags & EVFILT_WRITE)
147 /* This looks wrong but its right. As above, theres no modify
148 * call in kqueue. See the manpage.
151 EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
152 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
153 if (ref[ke_list[j].ident])
154 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
158 if (ref[ke_list[j].ident])
159 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
166 std::string KQueueEngine::GetName()