]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_kqueue.cpp
Merge tag 'v2.0.25' into master.
[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
44 /** Initialize the kqueue engine
45  */
46 void SocketEngine::Init()
47 {
48         LookupMaxFds();
49         RecoverFromFork();
50 }
51
52 void SocketEngine::RecoverFromFork()
53 {
54         /*
55          * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited.
56          * BUM HATS.
57          *
58          */
59         EngineHandle = kqueue();
60         if (EngineHandle == -1)
61                 InitError();
62 }
63
64 /** Shutdown the kqueue engine
65  */
66 void SocketEngine::Deinit()
67 {
68         Close(EngineHandle);
69 }
70
71 static struct kevent* GetChangeKE()
72 {
73         if (ChangePos >= changelist.size())
74                 changelist.resize(changelist.size() * 2);
75         return &changelist[ChangePos++];
76 }
77
78 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
79 {
80         int fd = eh->GetFd();
81
82         if (fd < 0)
83                 return false;
84
85         if (!SocketEngine::AddFdRef(eh))
86                 return false;
87
88         // We always want to read from the socket...
89         struct kevent* ke = GetChangeKE();
90         EV_SET(ke, fd, EVFILT_READ, EV_ADD, 0, 0, static_cast<void*>(eh));
91
92         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
93
94         eh->SetEventMask(event_mask);
95         OnSetEvent(eh, 0, event_mask);
96         ResizeDouble(ke_list);
97
98         return true;
99 }
100
101 void SocketEngine::DelFd(EventHandler* eh)
102 {
103         int fd = eh->GetFd();
104
105         if (fd < 0)
106         {
107                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "DelFd() on invalid fd: %d", fd);
108                 return;
109         }
110
111         // First remove the write filter ignoring errors, since we can't be
112         // sure if there are actually any write filters registered.
113         struct kevent* ke = GetChangeKE();
114         EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
115
116         // Then remove the read filter.
117         ke = GetChangeKE();
118         EV_SET(ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
119
120         SocketEngine::DelFdRef(eh);
121
122         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
123 }
124
125 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
126 {
127         if ((new_mask & FD_WANT_POLL_WRITE) && !(old_mask & FD_WANT_POLL_WRITE))
128         {
129                 // new poll-style write
130                 struct kevent* ke = GetChangeKE();
131                 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, static_cast<void*>(eh));
132         }
133         else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE))
134         {
135                 // removing poll-style write
136                 struct kevent* ke = GetChangeKE();
137                 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
138         }
139         if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)))
140         {
141                 struct kevent* ke = GetChangeKE();
142                 EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, static_cast<void*>(eh));
143         }
144 }
145
146 int SocketEngine::DispatchEvents()
147 {
148         struct timespec ts;
149         ts.tv_nsec = 0;
150         ts.tv_sec = 1;
151
152         int i = kevent(EngineHandle, &changelist.front(), ChangePos, &ke_list.front(), ke_list.size(), &ts);
153         ChangePos = 0;
154         ServerInstance->UpdateTime();
155
156         if (i < 0)
157                 return i;
158
159         stats.TotalEvents += i;
160
161         for (int j = 0; j < i; j++)
162         {
163                 struct kevent& kev = ke_list[j];
164                 EventHandler* eh = static_cast<EventHandler*>(kev.udata);
165                 if (!eh)
166                         continue;
167
168                 // Copy these in case the vector gets resized and kev invalidated
169                 const int fd = eh->GetFd();
170                 const short filter = kev.filter;
171                 if (fd < 0)
172                         continue;
173
174                 if (kev.flags & EV_EOF)
175                 {
176                         stats.ErrorEvents++;
177                         eh->OnEventHandlerError(kev.fflags);
178                         continue;
179                 }
180                 if (filter == EVFILT_WRITE)
181                 {
182                         /* When mask is FD_WANT_FAST_WRITE or FD_WANT_SINGLE_WRITE,
183                          * we set a one-shot write, so we need to clear that bit
184                          * to detect when it set again.
185                          */
186                         const int bits_to_clr = FD_WANT_SINGLE_WRITE | FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK;
187                         eh->SetEventMask(eh->GetEventMask() & ~bits_to_clr);
188                         eh->OnEventHandlerWrite();
189                 }
190                 else if (filter == EVFILT_READ)
191                 {
192                         eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
193                         eh->OnEventHandlerRead();
194                 }
195         }
196
197         return i;
198 }