]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_kqueue.cpp
Create StreamSocket for IO hooking implementation
[user/henk/code/inspircd.git] / src / socketengines / socketengine_kqueue.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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()
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, bool writeFirst)
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         // We always want to read from the socket...
71         struct kevent ke;
72         EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
73
74         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
75         if (i == -1)
76         {
77                 ServerInstance->Logs->Log("SOCKET",DEFAULT,"Failed to add fd: %d %s",
78                                           fd, strerror(errno));
79                 return false;
80         }
81
82         if (writeFirst) {
83                 // ...and sometimes want to write
84                 WantWrite(eh);
85         }
86
87         ref[fd] = eh;
88         CurrentSetSize++;
89
90         ServerInstance->Logs->Log("SOCKET",DEBUG,"New file descriptor: %d", fd);
91         return true;
92 }
93
94 bool KQueueEngine::DelFd(EventHandler* eh, bool force)
95 {
96         int fd = eh->GetFd();
97
98         if ((fd < 0) || (fd > GetMaxFds() - 1))
99         {
100                 ServerInstance->Logs->Log("SOCKET",DEFAULT,"DelFd() on invalid fd: %d", fd);
101                 return false;
102         }
103
104         struct kevent ke;
105
106         // First remove the write filter ignoring errors, since we can't be
107         // sure if there are actually any write filters registered.
108         EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
109         kevent(EngineHandle, &ke, 1, 0, 0, NULL);
110
111         // Then remove the read filter.
112         EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
113         int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
114
115         if ((j < 0) && !force)
116         {
117                 ServerInstance->Logs->Log("SOCKET",DEFAULT,"Failed to remove fd: %d %s",
118                                           fd, strerror(errno));
119                 return false;
120         }
121
122         CurrentSetSize--;
123         ref[fd] = NULL;
124
125         ServerInstance->Logs->Log("SOCKET",DEBUG,"Remove file descriptor: %d", fd);
126         return true;
127 }
128
129 void KQueueEngine::WantWrite(EventHandler* eh)
130 {
131         struct kevent ke;
132         // EV_ONESHOT since we only ever want one write event
133         EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
134         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
135         if (i < 0) {
136                 ServerInstance->Logs->Log("SOCKET",DEFAULT,"Failed to mark for writing: %d %s",
137                                           eh->GetFd(), strerror(errno));
138         }
139 }
140
141 int KQueueEngine::GetMaxFds()
142 {
143         if (!MAX_DESCRIPTORS)
144         {
145                 int mib[2], maxfiles;
146                 size_t len;
147
148                 mib[0] = CTL_KERN;
149                 mib[1] = KERN_MAXFILES;
150                 len = sizeof(maxfiles);
151                 sysctl(mib, 2, &maxfiles, &len, NULL, 0);
152                 MAX_DESCRIPTORS = maxfiles;
153                 return maxfiles;
154         }
155         return MAX_DESCRIPTORS;
156 }
157
158 int KQueueEngine::GetRemainingFds()
159 {
160         return GetMaxFds() - CurrentSetSize;
161 }
162
163 int KQueueEngine::DispatchEvents()
164 {
165         ts.tv_nsec = 0;
166         ts.tv_sec = 1;
167
168         int i = kevent(EngineHandle, NULL, 0, &ke_list[0], GetMaxFds(), &ts);
169
170         TotalEvents += i;
171
172         for (int j = 0; j < i; j++)
173         {
174                 if (ke_list[j].flags & EV_EOF)
175                 {
176                         /* We love you kqueue, oh yes we do *sings*!
177                          * kqueue gives us the error number directly in the EOF state!
178                          * Unlike smelly epoll and select, where we have to getsockopt
179                          * to get the error, this saves us time and cpu cycles. Go BSD!
180                          */
181                         ErrorEvents++;
182                         if (ref[ke_list[j].ident])
183                                 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
184                         continue;
185                 }
186                 if (ke_list[j].filter == EVFILT_WRITE)
187                 {
188                         /* We only ever add write events with EV_ONESHOT, which
189                          * means they are automatically removed once such a
190                          * event fires, so nothing to do here.
191                          */
192
193                         WriteEvents++;
194                         if (ref[ke_list[j].ident])
195                                 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
196                 }
197                 if (ke_list[j].filter == EVFILT_READ)
198                 {
199                         ReadEvents++;
200                         if (ref[ke_list[j].ident])
201                                 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
202                 }
203         }
204
205         return i;
206 }
207
208 std::string KQueueEngine::GetName()
209 {
210         return "kqueue";
211 }