1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
18 #include <sys/types.h>
19 #include <sys/event.h>
21 #include "socketengine_kqueue.h"
24 KQueueEngine::KQueueEngine(InspIRCd* Instance) : SocketEngine(Instance)
26 EngineHandle = kqueue();
27 if (EngineHandle == -1)
29 ServerInstance->Log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
30 ServerInstance->Log(SPARSE,"ERROR: this is a fatal error, exiting now.");
31 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
32 printf("ERROR: this is a fatal error, exiting now.");
33 InspIRCd::Exit(ERROR);
38 KQueueEngine::~KQueueEngine()
40 ServerInstance->Log(DEBUG,"KQueueEngine::~KQueueEngine()");
44 bool KQueueEngine::AddFd(EventHandler* eh)
48 ServerInstance->Log(DEBUG,"KQueueEngine::AddFd(%d)",fd);
50 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
52 ServerInstance->Log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
55 if (GetRemainingFds() <= 1)
57 ServerInstance->Log(DEFAULT,"ERROR: System out of file descriptors!");
63 ServerInstance->Log(DEFAULT,"ERROR: Slot already occupied");
68 ServerInstance->Log(DEBUG,"Add socket %d",fd);
71 ServerInstance->Log(DEBUG,"kqueue: Add socket to events, kq=%d socket=%d",EngineHandle,fd);
72 EV_SET(&ke, fd, eh->Readable() ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
74 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
77 ServerInstance->Log(DEBUG,"kqueue: List insertion failure!");
85 bool KQueueEngine::DelFd(EventHandler* eh)
89 ServerInstance->Log(DEBUG,"KQueueEngine::DelFd(%d)",fd);
91 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
95 EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
97 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
99 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
101 int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
103 if ((j < 0) && (i < 0))
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 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
123 ServerInstance->Log(DEBUG,"kqueue: Unable to set fd %d for wanting write", eh->GetFd());
127 int KQueueEngine::GetMaxFds()
129 return MAX_DESCRIPTORS;
132 int KQueueEngine::GetRemainingFds()
134 return MAX_DESCRIPTORS - CurrentSetSize;
137 int KQueueEngine::DispatchEvents()
141 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
142 for (int j = 0; j < i; j++)
144 ServerInstance->Log(DEBUG,"Handle %s event on fd %d",ke_list[j].flags & EVFILT_WRITE ? "write" : "read", ke_list[j].ident);
145 if (ke_list[j].flags & EV_EOF)
147 ServerInstance->Log(DEBUG,"kqueue: Error on FD %d", ke_list[j].ident);
148 /* We love you kqueue, oh yes we do *sings*!
149 * kqueue gives us the error number directly in the EOF state!
150 * Unlike smelly epoll and select, where we have to getsockopt
151 * to get the error, this saves us time and cpu cycles. Go BSD!
153 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
156 if (ke_list[j].flags & EVFILT_WRITE)
158 /* This looks wrong but its right. As above, theres no modify
159 * call in kqueue. See the manpage.
162 EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
163 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
166 ServerInstance->Log(DEBUG,"kqueue: Unable to set fd %d back to just wanting to read!", ke_list[j].ident);
168 if (ref[ke_list[j].ident])
169 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
173 if (ref[ke_list[j].ident])
174 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
181 std::string KQueueEngine::GetName()