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/>.
23 #include "exitcodes.h"
24 #include <sys/types.h>
25 #include <sys/event.h>
28 #include <sys/sysctl.h>
30 /** A specialisation of the SocketEngine class, designed to use BSD kqueue().
35 unsigned int ChangePos = 0;
36 /** These are used by kqueue() to hold socket events
38 std::vector<struct kevent> ke_list(16);
42 std::vector<struct kevent> changelist(8);
45 /** Initialize the kqueue engine
47 void SocketEngine::Init()
53 void SocketEngine::RecoverFromFork()
56 * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
60 EngineHandle = kqueue();
61 if (EngineHandle == -1)
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);
71 /** Shutdown the kqueue engine
73 void SocketEngine::Deinit()
78 static struct kevent* GetChangeKE()
80 if (ChangePos >= changelist.size())
81 changelist.resize(changelist.size() * 2);
82 return &changelist[ChangePos++];
85 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
92 if (!SocketEngine::AddFdRef(eh))
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));
99 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
101 eh->SetEventMask(event_mask);
102 OnSetEvent(eh, 0, event_mask);
103 ResizeDouble(ke_list);
108 void SocketEngine::DelFd(EventHandler* eh)
110 int fd = eh->GetFd();
114 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "DelFd() on invalid fd: %d", fd);
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);
123 // Then remove the read filter.
125 EV_SET(ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
127 SocketEngine::DelFdRef(eh);
129 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
132 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
134 if ((new_mask & FD_WANT_POLL_WRITE) && !(old_mask & FD_WANT_POLL_WRITE))
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));
140 else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE))
142 // removing poll-style write
143 struct kevent* ke = GetChangeKE();
144 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
146 if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)))
148 struct kevent* ke = GetChangeKE();
149 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, static_cast<void*>(eh));
153 int SocketEngine::DispatchEvents()
159 int i = kevent(EngineHandle, &changelist.front(), ChangePos, &ke_list.front(), ke_list.size(), &ts);
161 ServerInstance->UpdateTime();
166 stats.TotalEvents += i;
168 for (int j = 0; j < i; j++)
170 struct kevent& kev = ke_list[j];
171 EventHandler* eh = static_cast<EventHandler*>(kev.udata);
175 // Copy these in case the vector gets resized and kev invalidated
176 const int fd = eh->GetFd();
177 const short filter = kev.filter;
181 if (kev.flags & EV_EOF)
184 eh->OnEventHandlerError(kev.fflags);
187 if (filter == EVFILT_WRITE)
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.
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();
197 else if (filter == EVFILT_READ)
199 eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
200 eh->OnEventHandlerRead();