]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_kqueue.cpp
0fb14c29f0de9789b8d8f0c22dbf54099242cfcf
[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 <ulimit.h>
21
22 KQueueEngine::KQueueEngine(InspIRCd* Instance) : SocketEngine(Instance)
23 {
24         this->RecoverFromFork();
25         ref = new EventHandler* [GetMaxFds()];
26         ke_list = new struct kevent[GetMaxFds()];
27 }
28
29 void KQueueEngine::RecoverFromFork()
30 {
31         /*
32          * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
33          * BUM HATS.
34          * 
35          */
36         EngineHandle = kqueue();
37         if (EngineHandle == -1)
38         {
39                 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
40                 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: this is a fatal error, exiting now.");
41                 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.\n");
42                 printf("ERROR: this is a fatal error, exiting now.\n");
43                 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
44         }
45         CurrentSetSize = 0;
46         memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
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
134                 MAX_DESCRIPTORS = maxfiles;
135                 return max;
136         }
137         return MAX_DESCRIPTORS;
138 }
139
140 int KQueueEngine::GetRemainingFds()
141 {
142         return GetMaxFds() - CurrentSetSize;
143 }
144
145 int KQueueEngine::DispatchEvents()
146 {
147         ts.tv_nsec = 0;
148         ts.tv_sec = 1;
149
150         int i = kevent(EngineHandle, NULL, 0, &ke_list[0], GetMaxFds(), &ts);
151
152         TotalEvents += i;
153
154         for (int j = 0; j < i; j++)
155         {
156                 if (ke_list[j].flags & EV_EOF)
157                 {
158                         /* We love you kqueue, oh yes we do *sings*!
159                          * kqueue gives us the error number directly in the EOF state!
160                          * Unlike smelly epoll and select, where we have to getsockopt
161                          * to get the error, this saves us time and cpu cycles. Go BSD!
162                          */
163                         ErrorEvents++;
164                         if (ref[ke_list[j].ident])
165                                 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
166                         continue;
167                 }
168                 if (ke_list[j].flags & EVFILT_WRITE)
169                 {
170                         /* This looks wrong but its right. As above, theres no modify
171                          * call in kqueue. See the manpage.
172                          */
173                         struct kevent ke;
174                         EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
175                         kevent(EngineHandle, &ke, 1, 0, 0, NULL);
176                         WriteEvents++;
177                         if (ref[ke_list[j].ident])
178                                 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
179                 }
180                 else
181                 {
182                         ReadEvents++;
183                         if (ref[ke_list[j].ident])
184                                 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
185                 }
186         }
187
188         return i;
189 }
190
191 std::string KQueueEngine::GetName()
192 {
193         return "kqueue";
194 }