]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_poll.cpp
5c361a0cbded453bd9a913f957f4ff31ff02ad73
[user/henk/code/inspircd.git] / src / socketengines / socketengine_poll.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
6  *   Copyright (C) 2009 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "exitcodes.h"
25
26 #ifndef SOCKETENGINE_POLL
27 #define SOCKETENGINE_POLL
28
29 #include <iostream>
30 #include <vector>
31 #include <string>
32 #include <map>
33 #include "inspircd_config.h"
34 #include "inspircd.h"
35 #include "socketengine.h"
36
37 #ifndef _WIN32
38         #ifndef __USE_XOPEN
39             #define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/
40         #endif
41         #include <poll.h>
42         #include <sys/poll.h>
43 #else
44         /* *grumble* */
45         #define struct pollfd WSAPOLLFD
46         #define poll WSAPoll
47 #endif
48
49 class InspIRCd;
50
51 /** A specialisation of the SocketEngine class, designed to use poll().
52  */
53 class PollEngine : public SocketEngine
54 {
55 private:
56         /** These are used by poll() to hold socket events
57          */
58         struct pollfd *events;
59         /** This map maps fds to an index in the events array.
60          */
61         std::map<int, unsigned int> fd_mappings;
62 public:
63         /** Create a new PollEngine
64          */
65         PollEngine();
66         /** Delete a PollEngine
67          */
68         virtual ~PollEngine();
69         virtual bool AddFd(EventHandler* eh, int event_mask);
70         virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
71         virtual EventHandler* GetRef(int fd);
72         virtual void DelFd(EventHandler* eh);
73         virtual int DispatchEvents();
74         virtual std::string GetName();
75 };
76
77 #endif
78
79 #ifdef BSD
80         #include <sys/sysctl.h>
81 #else
82         #include <ulimit.h>
83 #endif
84
85 PollEngine::PollEngine()
86 {
87         CurrentSetSize = 0;
88 #ifdef BSD
89         int mib[2];
90         size_t len;
91
92         mib[0] = CTL_KERN;
93 #ifdef KERN_MAXFILESPERPROC
94         mib[1] = KERN_MAXFILESPERPROC;
95 #else
96         mib[1] = KERN_MAXFILES;
97 #endif
98         len = sizeof(MAX_DESCRIPTORS);
99         sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
100 #else
101         int max = ulimit(4, 0);
102         if (max > 0)
103         {
104                 MAX_DESCRIPTORS = max;
105         }
106         else
107         {
108                 ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets: %s", strerror(errno));
109                 std::cout << "ERROR: Can't determine maximum number of open sockets: " << strerror(errno) << std::endl;
110                 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
111         }
112 #endif
113
114         ref = new EventHandler* [GetMaxFds()];
115         events = new struct pollfd[GetMaxFds()];
116
117         memset(events, 0, GetMaxFds() * sizeof(struct pollfd));
118         memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
119 }
120
121 PollEngine::~PollEngine()
122 {
123         // No destruction required, either.
124         delete[] ref;
125         delete[] events;
126 }
127
128 static int mask_to_poll(int event_mask)
129 {
130         int rv = 0;
131         if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
132                 rv |= POLLIN;
133         if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
134                 rv |= POLLOUT;
135         return rv;
136 }
137
138 bool PollEngine::AddFd(EventHandler* eh, int event_mask)
139 {
140         int fd = eh->GetFd();
141         if ((fd < 0) || (fd > GetMaxFds() - 1))
142         {
143                 ServerInstance->Logs->Log("SOCKET",DEBUG,"AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
144                 return false;
145         }
146
147         if (fd_mappings.find(fd) != fd_mappings.end())
148         {
149                 ServerInstance->Logs->Log("SOCKET",DEBUG,"Attempt to add duplicate fd: %d", fd);
150                 return false;
151         }
152
153         unsigned int index = CurrentSetSize;
154
155         fd_mappings[fd] = index;
156         ref[index] = eh;
157         events[index].fd = fd;
158         events[index].events = mask_to_poll(event_mask);
159
160         ServerInstance->Logs->Log("SOCKET", DEBUG,"New file descriptor: %d (%d; index %d)", fd, events[fd].events, index);
161         SocketEngine::SetEventMask(eh, event_mask);
162         CurrentSetSize++;
163         return true;
164 }
165
166 EventHandler* PollEngine::GetRef(int fd)
167 {
168         std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
169         if (it == fd_mappings.end())
170                 return NULL;
171         return ref[it->second];
172 }
173
174 void PollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
175 {
176         std::map<int, unsigned int>::iterator it = fd_mappings.find(eh->GetFd());
177         if (it == fd_mappings.end())
178         {
179                 ServerInstance->Logs->Log("SOCKET",DEBUG,"SetEvents() on unknown fd: %d", eh->GetFd());
180                 return;
181         }
182
183         events[it->second].events = mask_to_poll(new_mask);
184 }
185
186 void PollEngine::DelFd(EventHandler* eh)
187 {
188         int fd = eh->GetFd();
189         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
190         {
191                 ServerInstance->Logs->Log("SOCKET", DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
192                 return;
193         }
194
195         std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
196         if (it == fd_mappings.end())
197         {
198                 ServerInstance->Logs->Log("SOCKET",DEBUG,"DelFd() on unknown fd: %d", fd);
199                 return;
200         }
201
202         unsigned int index = it->second;
203         unsigned int last_index = CurrentSetSize - 1;
204         int last_fd = events[last_index].fd;
205
206         if (index != last_index)
207         {
208                 // We need to move the last fd we got into this gap (gaps are evil!)
209
210                 // So update the mapping for the last fd to its new position
211                 fd_mappings[last_fd] = index;
212
213                 // move last_fd from last_index into index
214                 events[index].fd = last_fd;
215                 events[index].events = events[last_index].events;
216
217                 ref[index] = ref[last_index];
218         }
219
220         // Now remove all data for the last fd we got into out list.
221         // Above code made sure this always is right
222         fd_mappings.erase(it);
223         events[last_index].fd = 0;
224         events[last_index].events = 0;
225         ref[last_index] = NULL;
226
227         CurrentSetSize--;
228
229         ServerInstance->Logs->Log("SOCKET", DEBUG, "Remove file descriptor: %d (index: %d) "
230                         "(Filled gap with: %d (index: %d))", fd, index, last_fd, last_index);
231 }
232
233 int PollEngine::DispatchEvents()
234 {
235         int i = poll(events, CurrentSetSize, 1000);
236         int index;
237         socklen_t codesize = sizeof(int);
238         int errcode;
239         int processed = 0;
240         ServerInstance->UpdateTime();
241
242         if (i > 0)
243         {
244                 for (index = 0; index < CurrentSetSize && processed != i; index++)
245                 {
246                         if (events[index].revents)
247                                 processed++;
248                         EventHandler* eh = ref[index];
249                         if (!eh)
250                                 continue;
251
252                         if (events[index].revents & POLLHUP)
253                         {
254                                 eh->HandleEvent(EVENT_ERROR, 0);
255                                 continue;
256                         }
257
258                         if (events[index].revents & POLLERR)
259                         {
260                                 // Get fd
261                                 int fd = events[index].fd;
262
263                                 // Get error number
264                                 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
265                                         errcode = errno;
266                                 eh->HandleEvent(EVENT_ERROR, errcode);
267                                 continue;
268                         }
269
270                         if (events[index].revents & POLLIN)
271                         {
272                                 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
273                                 eh->HandleEvent(EVENT_READ);
274                                 if (eh != ref[index])
275                                         // whoops, deleted out from under us
276                                         continue;
277                         }
278                         
279                         if (events[index].revents & POLLOUT)
280                         {
281                                 int mask = eh->GetEventMask();
282                                 mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE);
283                                 SetEventMask(eh, mask);
284                                 events[index].events = mask_to_poll(mask);
285                                 eh->HandleEvent(EVENT_WRITE);
286                         }
287                 }
288         }
289
290         return i;
291 }
292
293 std::string PollEngine::GetName()
294 {
295         return "poll";
296 }
297
298 SocketEngine* CreateSocketEngine()
299 {
300         return new PollEngine;
301 }