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