2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
24 #include <sys/types.h>
25 #include <sys/event.h>
27 #include <sys/sysctl.h>
29 /** A specialisation of the SocketEngine class, designed to use BSD kqueue().
34 unsigned int ChangePos = 0;
35 /** These are used by kqueue() to hold socket events
37 std::vector<struct kevent> ke_list(16);
41 std::vector<struct kevent> changelist(8);
43 #if defined __NetBSD__ && __NetBSD_Version__ <= 999001400
44 inline intptr_t udata_cast(EventHandler* eh)
46 // On NetBSD <10 the last parameter of EV_SET is intptr_t.
47 return reinterpret_cast<intptr_t>(eh);
50 inline void* udata_cast(EventHandler* eh)
52 // On other platforms the last parameter of EV_SET is void*.
53 return static_cast<void*>(eh);
58 /** Initialize the kqueue engine
60 void SocketEngine::Init()
66 void SocketEngine::RecoverFromFork()
69 * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
73 EngineHandle = kqueue();
74 if (EngineHandle == -1)
78 /** Shutdown the kqueue engine
80 void SocketEngine::Deinit()
85 static struct kevent* GetChangeKE()
87 if (ChangePos >= changelist.size())
88 changelist.resize(changelist.size() * 2);
89 return &changelist[ChangePos++];
92 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
99 if (!SocketEngine::AddFdRef(eh))
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));
106 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
108 eh->SetEventMask(event_mask);
109 OnSetEvent(eh, 0, event_mask);
110 ResizeDouble(ke_list);
115 void SocketEngine::DelFd(EventHandler* eh)
117 int fd = eh->GetFd();
121 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "DelFd() on invalid fd: %d", fd);
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);
130 // Then remove the read filter.
132 EV_SET(ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
134 SocketEngine::DelFdRef(eh);
136 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
139 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
141 if ((new_mask & FD_WANT_POLL_WRITE) && !(old_mask & FD_WANT_POLL_WRITE))
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));
147 else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE))
149 // removing poll-style write
150 struct kevent* ke = GetChangeKE();
151 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
153 if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)))
155 struct kevent* ke = GetChangeKE();
156 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, udata_cast(eh));
160 int SocketEngine::DispatchEvents()
166 int i = kevent(EngineHandle, &changelist.front(), ChangePos, &ke_list.front(), ke_list.size(), &ts);
168 ServerInstance->UpdateTime();
173 stats.TotalEvents += i;
175 for (int j = 0; j < i; j++)
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);
183 // Copy these in case the vector gets resized and kev invalidated
184 const int fd = eh->GetFd();
185 const short filter = kev.filter;
189 if (kev.flags & EV_EOF)
192 eh->OnEventHandlerError(kev.fflags);
195 if (filter == EVFILT_WRITE)
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.
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();
205 else if (filter == EVFILT_READ)
207 eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
208 eh->OnEventHandlerRead();