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 <sys/types.h>
16 #include <sys/event.h>
18 #include "socketengine_kqueue.h"
21 KQueueEngine::KQueueEngine(InspIRCd* Instance) : SocketEngine(Instance)
23 EngineHandle = kqueue();
24 if (EngineHandle == -1)
26 ServerInstance->Log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
27 ServerInstance->Log(SPARSE,"ERROR: this is a fatal error, exiting now.");
28 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
29 printf("ERROR: this is a fatal error, exiting now.");
30 InspIRCd::Exit(ERROR);
35 KQueueEngine::~KQueueEngine()
37 ServerInstance->Log(DEBUG,"KQueueEngine::~KQueueEngine()");
41 bool KQueueEngine::AddFd(EventHandler* eh)
45 ServerInstance->Log(DEBUG,"KQueueEngine::AddFd(%d)",fd);
47 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
49 ServerInstance->Log(DEBUG,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
52 if (GetRemainingFds() <= 1)
54 ServerInstance->Log(DEBUG,"ERROR: System out of file descriptors!");
60 ServerInstance->Log(DEBUG,"ERROR: Slot already occupied");
65 ServerInstance->Log(DEBUG,"Add socket %d",fd);
68 ServerInstance->Log(DEBUG,"kqueue: Add socket to events, kq=%d socket=%d",EngineHandle,fd);
69 EV_SET(&ke, fd, eh->Readable() ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
71 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
74 ServerInstance->Log(DEBUG,"kqueue: List insertion failure!");
82 bool KQueueEngine::DelFd(EventHandler* eh)
86 ServerInstance->Log(DEBUG,"KQueueEngine::DelFd(%d)",fd);
88 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
92 EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
94 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
96 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
98 int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
100 if ((j < 0) && (i < 0))
109 void KQueueEngine::WantWrite(EventHandler* eh)
111 /** When changing an item in a kqueue, there is no 'modify' call
112 * as in epoll. Instead, we add the item again, and this overwrites
113 * the original setting rather than adding it twice. See man kqueue.
116 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
117 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
120 ServerInstance->Log(DEBUG,"kqueue: Unable to set fd %d for wanting write", eh->GetFd());
124 int KQueueEngine::GetMaxFds()
126 return MAX_DESCRIPTORS;
129 int KQueueEngine::GetRemainingFds()
131 return MAX_DESCRIPTORS - CurrentSetSize;
134 int KQueueEngine::DispatchEvents()
138 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
139 for (int j = 0; j < i; j++)
141 ServerInstance->Log(DEBUG,"Handle %s event on fd %d",ke_list[j].flags & EVFILT_WRITE ? "write" : "read", ke_list[j].ident);
142 if (ke_list[j].flags & EV_EOF)
144 ServerInstance->Log(DEBUG,"kqueue: Error on FD %d", ke_list[j].ident);
145 /* We love you kqueue, oh yes we do *sings*!
146 * kqueue gives us the error number directly in the EOF state!
147 * Unlike smelly epoll and select, where we have to getsockopt
148 * to get the error, this saves us time and cpu cycles. Go BSD!
150 if (ref[ke_list[j].ident])
151 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
154 if (ke_list[j].flags & EVFILT_WRITE)
156 /* This looks wrong but its right. As above, theres no modify
157 * call in kqueue. See the manpage.
160 EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
161 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
164 ServerInstance->Log(DEBUG,"kqueue: Unable to set fd %d back to just wanting to read!", ke_list[j].ident);
166 if (ref[ke_list[j].ident])
167 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
171 if (ref[ke_list[j].ident])
172 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
179 std::string KQueueEngine::GetName()