]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_kqueue.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / socketengines / socketengine_kqueue.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2014-2015 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2014 Adam <Adam@anope.org>
6  *   Copyright (C) 2012-2013, 2017, 2019 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2006-2008, 2010 Craig Edwards <brain@inspircd.org>
13  *
14  * This file is part of InspIRCd.  InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27
28 #include "inspircd.h"
29
30 #include <sys/types.h>
31 #include <sys/event.h>
32 #include <sys/time.h>
33 #include <sys/sysctl.h>
34
35 /** A specialisation of the SocketEngine class, designed to use BSD kqueue().
36  */
37 namespace
38 {
39         int EngineHandle;
40         unsigned int ChangePos = 0;
41         /** These are used by kqueue() to hold socket events
42          */
43         std::vector<struct kevent> ke_list(16);
44
45         /** Pending changes
46          */
47         std::vector<struct kevent> changelist(8);
48
49 #if defined __NetBSD__ && __NetBSD_Version__ <= 999001400
50         inline intptr_t udata_cast(EventHandler* eh)
51         {
52                 // On NetBSD <10 the last parameter of EV_SET is intptr_t.
53                 return reinterpret_cast<intptr_t>(eh);
54         }
55 #else
56         inline void* udata_cast(EventHandler* eh)
57         {
58                 // On other platforms the last parameter of EV_SET is void*.
59                 return static_cast<void*>(eh);
60         }
61 #endif
62 }
63
64 /** Initialize the kqueue engine
65  */
66 void SocketEngine::Init()
67 {
68         LookupMaxFds();
69         RecoverFromFork();
70 }
71
72 void SocketEngine::RecoverFromFork()
73 {
74         /*
75          * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
76          * BUM HATS.
77          *
78          */
79         EngineHandle = kqueue();
80         if (EngineHandle == -1)
81                 InitError();
82 }
83
84 /** Shutdown the kqueue engine
85  */
86 void SocketEngine::Deinit()
87 {
88         Close(EngineHandle);
89 }
90
91 static struct kevent* GetChangeKE()
92 {
93         if (ChangePos >= changelist.size())
94                 changelist.resize(changelist.size() * 2);
95         return &changelist[ChangePos++];
96 }
97
98 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
99 {
100         int fd = eh->GetFd();
101
102         if (fd < 0)
103                 return false;
104
105         if (!SocketEngine::AddFdRef(eh))
106                 return false;
107
108         // We always want to read from the socket...
109         struct kevent* ke = GetChangeKE();
110         EV_SET(ke, fd, EVFILT_READ, EV_ADD, 0, 0, udata_cast(eh));
111
112         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
113
114         eh->SetEventMask(event_mask);
115         OnSetEvent(eh, 0, event_mask);
116         ResizeDouble(ke_list);
117
118         return true;
119 }
120
121 void SocketEngine::DelFd(EventHandler* eh)
122 {
123         int fd = eh->GetFd();
124
125         if (fd < 0)
126         {
127                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "DelFd() on invalid fd: %d", fd);
128                 return;
129         }
130
131         // First remove the write filter ignoring errors, since we can't be
132         // sure if there are actually any write filters registered.
133         struct kevent* ke = GetChangeKE();
134         EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
135
136         // Then remove the read filter.
137         ke = GetChangeKE();
138         EV_SET(ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
139
140         SocketEngine::DelFdRef(eh);
141
142         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
143 }
144
145 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
146 {
147         if ((new_mask & FD_WANT_POLL_WRITE) && !(old_mask & FD_WANT_POLL_WRITE))
148         {
149                 // new poll-style write
150                 struct kevent* ke = GetChangeKE();
151                 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, udata_cast(eh));
152         }
153         else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE))
154         {
155                 // removing poll-style write
156                 struct kevent* ke = GetChangeKE();
157                 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
158         }
159         if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)))
160         {
161                 struct kevent* ke = GetChangeKE();
162                 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, udata_cast(eh));
163         }
164 }
165
166 int SocketEngine::DispatchEvents()
167 {
168         struct timespec ts;
169         ts.tv_nsec = 0;
170         ts.tv_sec = 1;
171
172         int i = kevent(EngineHandle, &changelist.front(), ChangePos, &ke_list.front(), ke_list.size(), &ts);
173         ChangePos = 0;
174         ServerInstance->UpdateTime();
175
176         if (i < 0)
177                 return i;
178
179         stats.TotalEvents += i;
180
181         for (int j = 0; j < i; j++)
182         {
183                 // This can't be a static_cast because udata is intptr_t on NetBSD.
184                 struct kevent& kev = ke_list[j];
185                 EventHandler* eh = reinterpret_cast<EventHandler*>(kev.udata);
186                 if (!eh)
187                         continue;
188
189                 // Copy these in case the vector gets resized and kev invalidated
190                 const int fd = eh->GetFd();
191                 const short filter = kev.filter;
192                 if (fd < 0)
193                         continue;
194
195                 if (kev.flags & EV_EOF)
196                 {
197                         stats.ErrorEvents++;
198                         eh->OnEventHandlerError(kev.fflags);
199                         continue;
200                 }
201                 if (filter == EVFILT_WRITE)
202                 {
203                         /* When mask is FD_WANT_FAST_WRITE or FD_WANT_SINGLE_WRITE,
204                          * we set a one-shot write, so we need to clear that bit
205                          * to detect when it set again.
206                          */
207                         const int bits_to_clr = FD_WANT_SINGLE_WRITE | FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK;
208                         eh->SetEventMask(eh->GetEventMask() & ~bits_to_clr);
209                         eh->OnEventHandlerWrite();
210                 }
211                 else if (filter == EVFILT_READ)
212                 {
213                         eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
214                         eh->OnEventHandlerRead();
215                 }
216         }
217
218         return i;
219 }