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