]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_kqueue.cpp
Convert GetMaxFds() to size_t and deduplicate setting code.
[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 <iostream>
28 #include <sys/sysctl.h>
29
30 /** A specialisation of the SocketEngine class, designed to use BSD kqueue().
31  */
32 namespace
33 {
34         int EngineHandle;
35         unsigned int ChangePos = 0;
36         /** These are used by kqueue() to hold socket events
37          */
38         std::vector<struct kevent> ke_list(16);
39
40         /** Pending changes
41          */
42         std::vector<struct kevent> changelist(8);
43 }
44
45 /** Initialize the kqueue engine
46  */
47 void SocketEngine::Init()
48 {
49         LookupMaxFds();
50         RecoverFromFork();
51 }
52
53 void SocketEngine::RecoverFromFork()
54 {
55         /*
56          * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
57          * BUM HATS.
58          *
59          */
60         EngineHandle = kqueue();
61         if (EngineHandle == -1)
62         {
63                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
64                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: this is a fatal error, exiting now.");
65                 std::cout << "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features." << std::endl;
66                 std::cout << "ERROR: this is a fatal error, exiting now." << std::endl;
67                 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
68         }
69 }
70
71 /** Shutdown the kqueue engine
72  */
73 void SocketEngine::Deinit()
74 {
75         Close(EngineHandle);
76 }
77
78 static struct kevent* GetChangeKE()
79 {
80         if (ChangePos >= changelist.size())
81                 changelist.resize(changelist.size() * 2);
82         return &changelist[ChangePos++];
83 }
84
85 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
86 {
87         int fd = eh->GetFd();
88
89         if (fd < 0)
90                 return false;
91
92         if (!SocketEngine::AddFdRef(eh))
93                 return false;
94
95         // We always want to read from the socket...
96         struct kevent* ke = GetChangeKE();
97         EV_SET(ke, fd, EVFILT_READ, EV_ADD, 0, 0, static_cast<void*>(eh));
98
99         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
100
101         eh->SetEventMask(event_mask);
102         OnSetEvent(eh, 0, event_mask);
103         ResizeDouble(ke_list);
104
105         return true;
106 }
107
108 void SocketEngine::DelFd(EventHandler* eh)
109 {
110         int fd = eh->GetFd();
111
112         if (fd < 0)
113         {
114                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "DelFd() on invalid fd: %d", fd);
115                 return;
116         }
117
118         // First remove the write filter ignoring errors, since we can't be
119         // sure if there are actually any write filters registered.
120         struct kevent* ke = GetChangeKE();
121         EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
122
123         // Then remove the read filter.
124         ke = GetChangeKE();
125         EV_SET(ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
126
127         SocketEngine::DelFdRef(eh);
128
129         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
130 }
131
132 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
133 {
134         if ((new_mask & FD_WANT_POLL_WRITE) && !(old_mask & FD_WANT_POLL_WRITE))
135         {
136                 // new poll-style write
137                 struct kevent* ke = GetChangeKE();
138                 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, static_cast<void*>(eh));
139         }
140         else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE))
141         {
142                 // removing poll-style write
143                 struct kevent* ke = GetChangeKE();
144                 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
145         }
146         if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)))
147         {
148                 struct kevent* ke = GetChangeKE();
149                 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, static_cast<void*>(eh));
150         }
151 }
152
153 int SocketEngine::DispatchEvents()
154 {
155         struct timespec ts;
156         ts.tv_nsec = 0;
157         ts.tv_sec = 1;
158
159         int i = kevent(EngineHandle, &changelist.front(), ChangePos, &ke_list.front(), ke_list.size(), &ts);
160         ChangePos = 0;
161         ServerInstance->UpdateTime();
162
163         if (i < 0)
164                 return i;
165
166         stats.TotalEvents += i;
167
168         for (int j = 0; j < i; j++)
169         {
170                 struct kevent& kev = ke_list[j];
171                 EventHandler* eh = static_cast<EventHandler*>(kev.udata);
172                 if (!eh)
173                         continue;
174
175                 // Copy these in case the vector gets resized and kev invalidated
176                 const int fd = eh->GetFd();
177                 const short filter = kev.filter;
178                 if (fd < 0)
179                         continue;
180
181                 if (kev.flags & EV_EOF)
182                 {
183                         stats.ErrorEvents++;
184                         eh->OnEventHandlerError(kev.fflags);
185                         continue;
186                 }
187                 if (filter == EVFILT_WRITE)
188                 {
189                         /* When mask is FD_WANT_FAST_WRITE or FD_WANT_SINGLE_WRITE,
190                          * we set a one-shot write, so we need to clear that bit
191                          * to detect when it set again.
192                          */
193                         const int bits_to_clr = FD_WANT_SINGLE_WRITE | FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK;
194                         eh->SetEventMask(eh->GetEventMask() & ~bits_to_clr);
195                         eh->OnEventHandlerWrite();
196                 }
197                 else if (filter == EVFILT_READ)
198                 {
199                         eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
200                         eh->OnEventHandlerRead();
201                 }
202         }
203
204         return i;
205 }