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>
27 #include "socketengine.h"
29 #include <sys/sysctl.h>
31 /** A specialisation of the SocketEngine class, designed to use BSD kqueue().
36 /** These are used by kqueue() to hold socket events
38 std::vector<struct kevent> ke_list(16);
41 /** Initialize the kqueue engine
43 void SocketEngine::Init()
50 #ifdef KERN_MAXFILESPERPROC
51 mib[1] = KERN_MAXFILESPERPROC;
53 mib[1] = KERN_MAXFILES;
55 len = sizeof(MAX_DESCRIPTORS);
56 sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
57 if (MAX_DESCRIPTORS <= 0)
59 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets!");
60 std::cout << "ERROR: Can't determine maximum number of open sockets!" << std::endl;
61 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
67 void SocketEngine::RecoverFromFork()
70 * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
74 EngineHandle = kqueue();
75 if (EngineHandle == -1)
77 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
78 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: this is a fatal error, exiting now.");
79 std::cout << "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features." << std::endl;
80 std::cout << "ERROR: this is a fatal error, exiting now." << std::endl;
81 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
85 /** Shutdown the kqueue engine
87 void SocketEngine::Deinit()
92 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
96 if ((fd < 0) || (fd > GetMaxFds() - 1))
99 if (!SocketEngine::AddFdRef(eh))
102 // We always want to read from the socket...
104 EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
106 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
109 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to add fd: %d %s",
110 fd, strerror(errno));
114 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
116 eh->SetEventMask(event_mask);
117 OnSetEvent(eh, 0, event_mask);
118 ResizeDouble(ke_list);
123 void SocketEngine::DelFd(EventHandler* eh)
125 int fd = eh->GetFd();
127 if ((fd < 0) || (fd > GetMaxFds() - 1))
129 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "DelFd() on invalid fd: %d", fd);
135 // First remove the write filter ignoring errors, since we can't be
136 // sure if there are actually any write filters registered.
137 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
138 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
140 // Then remove the read filter.
141 EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
142 int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
146 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to remove fd: %d %s",
147 fd, strerror(errno));
150 SocketEngine::DelFdRef(eh);
152 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
155 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
157 if ((new_mask & FD_WANT_POLL_WRITE) && !(old_mask & FD_WANT_POLL_WRITE))
159 // new poll-style write
161 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, NULL);
162 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
164 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
165 eh->GetFd(), strerror(errno));
167 else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE))
169 // removing poll-style write
171 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
172 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
174 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
175 eh->GetFd(), strerror(errno));
177 if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)))
179 // new one-shot write
181 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
182 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
184 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to mark for writing: %d %s",
185 eh->GetFd(), strerror(errno));
189 int SocketEngine::DispatchEvents()
195 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], ke_list.size(), &ts);
196 ServerInstance->UpdateTime();
201 stats.TotalEvents += i;
203 for (int j = 0; j < i; j++)
205 struct kevent& kev = ke_list[j];
207 // Copy these in case the vector gets resized and kev invalidated
208 const int fd = kev.ident;
209 const short filter = kev.filter;
211 EventHandler* eh = GetRef(fd);
215 if (kev.flags & EV_EOF)
218 eh->HandleEvent(EVENT_ERROR, kev.fflags);
221 if (filter == EVFILT_WRITE)
224 /* When mask is FD_WANT_FAST_WRITE or FD_WANT_SINGLE_WRITE,
225 * we set a one-shot write, so we need to clear that bit
226 * to detect when it set again.
228 const int bits_to_clr = FD_WANT_SINGLE_WRITE | FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK;
229 eh->SetEventMask(eh->GetEventMask() & ~bits_to_clr);
230 eh->HandleEvent(EVENT_WRITE);
232 else if (filter == EVFILT_READ)
235 eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
236 eh->HandleEvent(EVENT_READ);