]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_kqueue.cpp
Change all socketengine methods to be static
[user/henk/code/inspircd.git] / src / socketengines / socketengine_kqueue.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
6  *   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23 #include "exitcodes.h"
24 #include <sys/types.h>
25 #include <sys/event.h>
26 #include <sys/time.h>
27 #include "socketengine.h"
28 #include <iostream>
29 #include <sys/sysctl.h>
30
31 /** A specialisation of the SocketEngine class, designed to use BSD kqueue().
32  */
33 namespace
34 {
35         int EngineHandle;
36         /** These are used by kqueue() to hold socket events
37          */
38         std::vector<struct kevent> ke_list(16);
39 }
40
41 /** Initialize the kqueue engine
42  */
43 void SocketEngine::Init()
44 {
45         MAX_DESCRIPTORS = 0;
46         int mib[2];
47         size_t len;
48
49         mib[0] = CTL_KERN;
50 #ifdef KERN_MAXFILESPERPROC
51         mib[1] = KERN_MAXFILESPERPROC;
52 #else
53         mib[1] = KERN_MAXFILES;
54 #endif
55         len = sizeof(MAX_DESCRIPTORS);
56         sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
57         if (MAX_DESCRIPTORS <= 0)
58         {
59                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets!");
60                 std::cout << "ERROR: Can't determine maximum number of open sockets!" << std::endl;
61                 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
62         }
63
64         RecoverFromFork();
65 }
66
67 void SocketEngine::RecoverFromFork()
68 {
69         /*
70          * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
71          * BUM HATS.
72          *
73          */
74         EngineHandle = kqueue();
75         if (EngineHandle == -1)
76         {
77                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
78                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: this is a fatal error, exiting now.");
79                 std::cout << "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features." << std::endl;
80                 std::cout << "ERROR: this is a fatal error, exiting now." << std::endl;
81                 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
82         }
83 }
84
85 /** Shutdown the kqueue engine
86  */
87 void SocketEngine::Deinit()
88 {
89         Close(EngineHandle);
90 }
91
92 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
93 {
94         int fd = eh->GetFd();
95
96         if ((fd < 0) || (fd > GetMaxFds() - 1))
97                 return false;
98
99         if (!SocketEngine::AddFdRef(eh))
100                 return false;
101
102         // We always want to read from the socket...
103         struct kevent ke;
104         EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
105
106         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
107         if (i == -1)
108         {
109                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to add fd: %d %s",
110                                           fd, strerror(errno));
111                 return false;
112         }
113
114         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
115
116         eh->SetEventMask(event_mask);
117         OnSetEvent(eh, 0, event_mask);
118         ResizeDouble(ke_list);
119
120         return true;
121 }
122
123 void SocketEngine::DelFd(EventHandler* eh)
124 {
125         int fd = eh->GetFd();
126
127         if ((fd < 0) || (fd > GetMaxFds() - 1))
128         {
129                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "DelFd() on invalid fd: %d", fd);
130                 return;
131         }
132
133         struct kevent ke;
134
135         // First remove the write filter ignoring errors, since we can't be
136         // sure if there are actually any write filters registered.
137         EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
138         kevent(EngineHandle, &ke, 1, 0, 0, NULL);
139
140         // Then remove the read filter.
141         EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
142         int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
143
144         if (j < 0)
145         {
146                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to remove fd: %d %s",
147                                           fd, strerror(errno));
148         }
149
150         SocketEngine::DelFdRef(eh);
151
152         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
153 }
154
155 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
156 {
157         if ((new_mask & FD_WANT_POLL_WRITE) && !(old_mask & FD_WANT_POLL_WRITE))
158         {
159                 // new poll-style write
160                 struct kevent ke;
161                 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, NULL);
162                 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
163                 if (i < 0)
164                         ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
165                                                   eh->GetFd(), strerror(errno));
166         }
167         else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE))
168         {
169                 // removing poll-style write
170                 struct kevent ke;
171                 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
172                 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
173                 if (i < 0)
174                         ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
175                                                   eh->GetFd(), strerror(errno));
176         }
177         if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)))
178         {
179                 // new one-shot write
180                 struct kevent ke;
181                 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
182                 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
183                 if (i < 0)
184                         ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
185                                                   eh->GetFd(), strerror(errno));
186         }
187 }
188
189 int SocketEngine::DispatchEvents()
190 {
191         struct timespec ts;
192         ts.tv_nsec = 0;
193         ts.tv_sec = 1;
194
195         int i = kevent(EngineHandle, NULL, 0, &ke_list[0], ke_list.size(), &ts);
196         ServerInstance->UpdateTime();
197
198         if (i < 0)
199                 return i;
200
201         stats.TotalEvents += i;
202
203         for (int j = 0; j < i; j++)
204         {
205                 struct kevent& kev = ke_list[j];
206
207                 // Copy these in case the vector gets resized and kev invalidated
208                 const int fd = kev.ident;
209                 const short filter = kev.filter;
210
211                 EventHandler* eh = GetRef(fd);
212                 if (!eh)
213                         continue;
214
215                 if (kev.flags & EV_EOF)
216                 {
217                         stats.ErrorEvents++;
218                         eh->HandleEvent(EVENT_ERROR, kev.fflags);
219                         continue;
220                 }
221                 if (filter == EVFILT_WRITE)
222                 {
223                         stats.WriteEvents++;
224                         /* When mask is FD_WANT_FAST_WRITE or FD_WANT_SINGLE_WRITE,
225                          * we set a one-shot write, so we need to clear that bit
226                          * to detect when it set again.
227                          */
228                         const int bits_to_clr = FD_WANT_SINGLE_WRITE | FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK;
229                         eh->SetEventMask(eh->GetEventMask() & ~bits_to_clr);
230                         eh->HandleEvent(EVENT_WRITE);
231                 }
232                 else if (filter == EVFILT_READ)
233                 {
234                         stats.ReadEvents++;
235                         eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
236                         eh->HandleEvent(EVENT_READ);
237                 }
238         }
239
240         return i;
241 }