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 "socketengine_kqueue.h"
22 KQueueEngine::KQueueEngine(InspIRCd* Instance) : SocketEngine(Instance)
24 EngineHandle = kqueue();
25 if (EngineHandle == -1)
27 ServerInstance->Log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
28 ServerInstance->Log(SPARSE,"ERROR: this is a fatal error, exiting now.");
29 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
30 printf("ERROR: this is a fatal error, exiting now.");
31 InspIRCd::Exit(EXIT_STATUS_SOCKETENGINE);
36 KQueueEngine::~KQueueEngine()
38 ServerInstance->Log(DEBUG,"KQueueEngine::~KQueueEngine()");
42 bool KQueueEngine::AddFd(EventHandler* eh)
46 ServerInstance->Log(DEBUG,"KQueueEngine::AddFd(%d)",fd);
48 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
50 ServerInstance->Log(DEBUG,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
53 if (GetRemainingFds() <= 1)
55 ServerInstance->Log(DEBUG,"ERROR: System out of file descriptors!");
61 ServerInstance->Log(DEBUG,"ERROR: Slot already occupied");
66 ServerInstance->Log(DEBUG,"Add socket %d",fd);
69 ServerInstance->Log(DEBUG,"kqueue: Add socket to events, kq=%d socket=%d",EngineHandle,fd);
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);
75 ServerInstance->Log(DEBUG,"kqueue: List insertion failure!");
83 bool KQueueEngine::DelFd(EventHandler* eh)
87 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
91 EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
93 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
95 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
97 int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
99 if ((j < 0) && (i < 0))
108 void KQueueEngine::WantWrite(EventHandler* eh)
110 /** When changing an item in a kqueue, there is no 'modify' call
111 * as in epoll. Instead, we add the item again, and this overwrites
112 * the original setting rather than adding it twice. See man kqueue.
115 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
116 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
119 ServerInstance->Log(DEBUG,"kqueue: Unable to set fd %d for wanting write", eh->GetFd());
123 int KQueueEngine::GetMaxFds()
125 return MAX_DESCRIPTORS;
128 int KQueueEngine::GetRemainingFds()
130 return MAX_DESCRIPTORS - CurrentSetSize;
133 int KQueueEngine::DispatchEvents()
137 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
138 for (int j = 0; j < i; j++)
140 if (ke_list[j].flags & EV_EOF)
142 ServerInstance->Log(DEBUG,"kqueue: Error on FD %d", ke_list[j].ident);
143 /* We love you kqueue, oh yes we do *sings*!
144 * kqueue gives us the error number directly in the EOF state!
145 * Unlike smelly epoll and select, where we have to getsockopt
146 * to get the error, this saves us time and cpu cycles. Go BSD!
148 if (ref[ke_list[j].ident])
149 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
152 if (ke_list[j].flags & EVFILT_WRITE)
154 /* This looks wrong but its right. As above, theres no modify
155 * call in kqueue. See the manpage.
158 EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
159 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
162 ServerInstance->Log(DEBUG,"kqueue: Unable to set fd %d back to just wanting to read!", ke_list[j].ident);
164 if (ref[ke_list[j].ident])
165 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
169 if (ref[ke_list[j].ident])
170 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
177 std::string KQueueEngine::GetName()