]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_kqueue.cpp
Backport from socket branch: allow read and write events to be triggered on one kqueu...
[user/henk/code/inspircd.git] / src / socketengines / socketengine_kqueue.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "exitcodes.h"
16 #include <sys/types.h>
17 #include <sys/event.h>
18 #include <sys/time.h>
19 #include "socketengines/socketengine_kqueue.h"
20 #include <sys/sysctl.h>
21
22 KQueueEngine::KQueueEngine(InspIRCd* Instance) : SocketEngine(Instance)
23 {
24         MAX_DESCRIPTORS = 0;
25         this->RecoverFromFork();
26         ke_list = new struct kevent[GetMaxFds()];
27         ref = new EventHandler* [GetMaxFds()];
28         memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
29 }
30
31 void KQueueEngine::RecoverFromFork()
32 {
33         /*
34          * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
35          * BUM HATS.
36          * 
37          */
38         EngineHandle = kqueue();
39         if (EngineHandle == -1)
40         {
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);
46         }
47         CurrentSetSize = 0;
48 }
49
50 KQueueEngine::~KQueueEngine()
51 {
52         this->Close(EngineHandle);
53         delete[] ref;
54         delete[] ke_list;
55 }
56
57 bool KQueueEngine::AddFd(EventHandler* eh)
58 {
59         int fd = eh->GetFd();
60
61         if ((fd < 0) || (fd > GetMaxFds() - 1))
62                 return false;
63
64         if (GetRemainingFds() <= 1)
65                 return false;
66
67         if (ref[fd])
68                 return false;
69
70         struct kevent ke;
71         EV_SET(&ke, fd, eh->Readable() ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
72
73         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
74         if (i == -1)
75         {
76                 return false;
77         }
78
79         ref[fd] = eh;
80         CurrentSetSize++;
81
82         ServerInstance->Logs->Log("SOCKET",DEBUG,"New file descriptor: %d", fd);
83         return true;
84 }
85
86 bool KQueueEngine::DelFd(EventHandler* eh, bool force)
87 {
88         int fd = eh->GetFd();
89
90         if ((fd < 0) || (fd > GetMaxFds() - 1))
91                 return false;
92
93         struct kevent ke;
94         EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
95
96         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
97
98         EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
99
100         int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
101
102         if ((j < 0) && (i < 0) && !force)
103                 return false;
104
105         CurrentSetSize--;
106         ref[fd] = NULL;
107
108         //ServerInstance->Logs->Log("SOCKET",DEBUG,"Remove file descriptor: %d", fd);
109         return true;
110 }
111
112 void KQueueEngine::WantWrite(EventHandler* eh)
113 {
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.
117          */
118         struct kevent ke;
119         EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, NULL);
120         EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
121         kevent(EngineHandle, &ke, 1, 0, 0, NULL);
122 }
123
124 int KQueueEngine::GetMaxFds()
125 {
126         if (!MAX_DESCRIPTORS)
127         {
128                 int mib[2], maxfiles;
129                 size_t len;
130
131                 mib[0] = CTL_KERN;
132                 mib[1] = KERN_MAXFILES;
133                 len = sizeof(maxfiles);
134                 sysctl(mib, 2, &maxfiles, &len, NULL, 0);
135                 MAX_DESCRIPTORS = maxfiles;
136                 return maxfiles;
137         }
138         return MAX_DESCRIPTORS;
139 }
140
141 int KQueueEngine::GetRemainingFds()
142 {
143         return GetMaxFds() - CurrentSetSize;
144 }
145
146 int KQueueEngine::DispatchEvents()
147 {
148         ts.tv_nsec = 0;
149         ts.tv_sec = 1;
150
151         int i = kevent(EngineHandle, NULL, 0, &ke_list[0], GetMaxFds(), &ts);
152
153         TotalEvents += i;
154
155         for (int j = 0; j < i; j++)
156         {
157                 if (ke_list[j].flags & EV_EOF)
158                 {
159                         /* We love you kqueue, oh yes we do *sings*!
160                          * kqueue gives us the error number directly in the EOF state!
161                          * Unlike smelly epoll and select, where we have to getsockopt
162                          * to get the error, this saves us time and cpu cycles. Go BSD!
163                          */
164                         ErrorEvents++;
165                         if (ref[ke_list[j].ident])
166                                 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
167                         continue;
168                 }
169                 if (ke_list[j].flags & EVFILT_WRITE)
170                 {
171                         /* This looks wrong but its right. As above, theres no modify
172                          * call in kqueue. See the manpage.
173                          */
174                         if (ref[ke_list[j].ident]->Readable())
175                         {
176                                 struct kevent ke;
177                                 EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
178                                 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
179                         }
180                         WriteEvents++;
181                         if (ref[ke_list[j].ident])
182                                 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
183                 }
184
185                 if (ke_list[j].flags & EVFILT_READ)
186                 {
187                         ReadEvents++;
188                         if (ref[ke_list[j].ident])
189                                 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
190                 }
191         }
192
193         return i;
194 }
195
196 std::string KQueueEngine::GetName()
197 {
198         return "kqueue";
199 }