]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_kqueue.cpp
DOH! Fix my muppetry of a segfault, and fix some warnings
[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         this->RecoverFromFork();
25         ke_list = new struct kevent[GetMaxFds()];
26         ref = new EventHandler* [GetMaxFds()];
27         memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
28 }
29
30 void KQueueEngine::RecoverFromFork()
31 {
32         /*
33          * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
34          * BUM HATS.
35          * 
36          */
37         EngineHandle = kqueue();
38         if (EngineHandle == -1)
39         {
40                 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
41                 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: this is a fatal error, exiting now.");
42                 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.\n");
43                 printf("ERROR: this is a fatal error, exiting now.\n");
44                 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
45         }
46         CurrentSetSize = 0;
47 }
48
49 KQueueEngine::~KQueueEngine()
50 {
51         this->Close(EngineHandle);
52         delete[] ref;
53         delete[] ke_list;
54 }
55
56 bool KQueueEngine::AddFd(EventHandler* eh)
57 {
58         int fd = eh->GetFd();
59
60         if ((fd < 0) || (fd > GetMaxFds() - 1))
61                 return false;
62
63         if (GetRemainingFds() <= 1)
64                 return false;
65
66         if (ref[fd])
67                 return false;
68
69         struct kevent ke;
70         EV_SET(&ke, fd, eh->Readable() ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
71
72         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
73         if (i == -1)
74         {
75                 return false;
76         }
77
78         ref[fd] = eh;
79         CurrentSetSize++;
80
81         ServerInstance->Logs->Log("SOCKET",DEBUG,"New file descriptor: %d", fd);
82         return true;
83 }
84
85 bool KQueueEngine::DelFd(EventHandler* eh, bool force)
86 {
87         int fd = eh->GetFd();
88
89         if ((fd < 0) || (fd > GetMaxFds() - 1))
90                 return false;
91
92         struct kevent ke;
93         EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
94
95         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
96
97         EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
98
99         int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
100
101         if ((j < 0) && (i < 0) && !force)
102                 return false;
103
104         CurrentSetSize--;
105         ref[fd] = NULL;
106
107         ServerInstance->Logs->Log("SOCKET",DEBUG,"Remove file descriptor: %d", fd);
108         return true;
109 }
110
111 void KQueueEngine::WantWrite(EventHandler* eh)
112 {
113         /** When changing an item in a kqueue, there is no 'modify' call
114          * as in epoll. Instead, we add the item again, and this overwrites
115          * the original setting rather than adding it twice. See man kqueue.
116          */
117         struct kevent ke;
118         EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
119         kevent(EngineHandle, &ke, 1, 0, 0, NULL);
120 }
121
122 int KQueueEngine::GetMaxFds()
123 {
124         if (!MAX_DESCRIPTORS)
125         {
126                 int mib[2], maxfiles;
127                 size_t len;
128
129                 mib[0] = CTL_KERN;
130                 mib[1] = KERN_MAXFILES;
131                 len = sizeof(maxfiles);
132                 sysctl(mib, 2, &maxfiles, &len, NULL, 0);
133                 MAX_DESCRIPTORS = maxfiles;
134                 return maxfiles;
135         }
136         return MAX_DESCRIPTORS;
137 }
138
139 int KQueueEngine::GetRemainingFds()
140 {
141         return GetMaxFds() - CurrentSetSize;
142 }
143
144 int KQueueEngine::DispatchEvents()
145 {
146         ts.tv_nsec = 0;
147         ts.tv_sec = 1;
148
149         int i = kevent(EngineHandle, NULL, 0, &ke_list[0], GetMaxFds(), &ts);
150
151         TotalEvents += i;
152
153         for (int j = 0; j < i; j++)
154         {
155                 if (ke_list[j].flags & EV_EOF)
156                 {
157                         /* We love you kqueue, oh yes we do *sings*!
158                          * kqueue gives us the error number directly in the EOF state!
159                          * Unlike smelly epoll and select, where we have to getsockopt
160                          * to get the error, this saves us time and cpu cycles. Go BSD!
161                          */
162                         ErrorEvents++;
163                         if (ref[ke_list[j].ident])
164                                 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
165                         continue;
166                 }
167                 if (ke_list[j].flags & EVFILT_WRITE)
168                 {
169                         /* This looks wrong but its right. As above, theres no modify
170                          * call in kqueue. See the manpage.
171                          */
172                         struct kevent ke;
173                         EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
174                         kevent(EngineHandle, &ke, 1, 0, 0, NULL);
175                         WriteEvents++;
176                         if (ref[ke_list[j].ident])
177                                 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
178                 }
179                 else
180                 {
181                         ReadEvents++;
182                         if (ref[ke_list[j].ident])
183                                 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
184                 }
185         }
186
187         return i;
188 }
189
190 std::string KQueueEngine::GetName()
191 {
192         return "kqueue";
193 }